2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #ifdef HAVE_NETINET_TCP_H
38 # include <netinet/tcp.h>
40 #ifdef HAVE_NETINET_UDP_H
41 # include <netinet/udp.h>
43 #ifdef HAVE_NETINET_SCTP_H
44 # include <netinet/sctp.h>
46 #include <arpa/inet.h>
48 #include <asm/types.h>
49 #ifdef HAVE_NETIPX_IPX_H
50 # include <netipx/ipx.h>
52 # include <linux/ipx.h>
55 #if defined(HAVE_LINUX_IP_VS_H)
56 # include <linux/ip_vs.h>
58 #include <linux/netlink.h>
59 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
60 # include <linux/netfilter_arp/arp_tables.h>
62 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
63 # include <linux/netfilter_bridge/ebtables.h>
65 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
66 # include <linux/netfilter_ipv4/ip_tables.h>
68 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
69 # include <linux/netfilter_ipv6/ip6_tables.h>
71 #include <linux/if_packet.h>
72 #include <linux/icmp.h>
74 #include "xlat/socktypes.h"
75 #include "xlat/sock_type_flags.h"
76 #ifndef SOCK_TYPE_MASK
77 # define SOCK_TYPE_MASK 0xf
80 #include "xlat/socketlayers.h"
82 #include "xlat/inet_protocols.h"
84 #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
85 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
87 #include "xlat/netlink_protocols.h"
89 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
90 # include <bluetooth/bluetooth.h>
91 # include "xlat/bt_protocols.h"
95 print_ifindex(unsigned int ifindex)
97 #ifdef HAVE_IF_INDEXTONAME
98 char buf[IFNAMSIZ + 1];
100 if (if_indextoname(ifindex, buf)) {
101 tprints("if_nametoindex(");
102 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
107 tprintf("%u", ifindex);
111 decode_sockbuf(struct tcb *tcp, int fd, long addr, long addrlen)
114 switch (verbose(tcp) ? getfdproto(tcp, fd) : SOCK_PROTO_UNKNOWN) {
115 case SOCK_PROTO_NETLINK:
116 decode_netlink(tcp, addr, addrlen);
119 printstr(tcp, addr, addrlen);
124 * low bits of the socket type define real socket type,
125 * other bits are socket type flags.
128 tprint_sock_type(unsigned int flags)
130 const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
134 flags &= ~SOCK_TYPE_MASK;
139 printflags(sock_type_flags, flags, "SOCK_???");
144 printxval(addrfams, tcp->u_arg[0], "AF_???");
146 tprint_sock_type(tcp->u_arg[1]);
148 switch (tcp->u_arg[0]) {
151 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
155 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
158 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
160 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
165 tprintf("%lu", tcp->u_arg[2]);
169 return RVAL_DECODED | RVAL_FD;
174 printfd(tcp, tcp->u_arg[0]);
176 const int addrlen = tcp->u_arg[2];
177 decode_sockaddr(tcp, tcp->u_arg[1], addrlen);
178 tprintf(", %d", addrlen);
185 printfd(tcp, tcp->u_arg[0]);
187 tprintf("%lu", tcp->u_arg[1]);
193 fetch_socklen(struct tcb *tcp, int *plen,
194 const unsigned long sockaddr, const unsigned long socklen)
196 return verbose(tcp) && sockaddr && socklen
197 && umove(tcp, socklen, plen) == 0;
201 decode_sockname(struct tcb *tcp)
206 printfd(tcp, tcp->u_arg[0]);
208 if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
209 set_tcb_priv_ulong(tcp, ulen);
212 printaddr(tcp->u_arg[1]);
214 printaddr(tcp->u_arg[2]);
219 ulen = get_tcb_priv_ulong(tcp);
221 if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
222 printaddr(tcp->u_arg[1]);
223 tprintf(", [%d]", ulen);
225 decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
227 tprintf(", [%d->%d]", ulen, rlen);
229 tprintf(", [%d]", rlen);
237 return decode_sockname(tcp) | RVAL_FD;
242 int rc = decode_sockname(tcp);
244 if (rc & RVAL_DECODED) {
246 printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
254 printfd(tcp, tcp->u_arg[0]);
256 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
257 tprintf(", %lu, ", tcp->u_arg[2]);
259 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
266 printfd(tcp, tcp->u_arg[0]);
268 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
269 tprintf(", %lu, ", tcp->u_arg[2]);
271 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
273 const int addrlen = tcp->u_arg[5];
275 decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
277 tprintf(", %d", addrlen);
285 printfd(tcp, tcp->u_arg[0]);
289 printaddr(tcp->u_arg[1]);
291 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
295 tprintf(", %lu, ", tcp->u_arg[2]);
296 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
306 printfd(tcp, tcp->u_arg[0]);
308 if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
309 set_tcb_priv_ulong(tcp, ulen);
314 printaddr(tcp->u_arg[1]);
316 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
320 tprintf(", %lu, ", tcp->u_arg[2]);
322 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
325 ulen = get_tcb_priv_ulong(tcp);
327 if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
329 printaddr(tcp->u_arg[4]);
332 printaddr(tcp->u_arg[5]);
337 printaddr(tcp->u_arg[4]);
339 tprintf(", [%d]", ulen);
343 decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
346 tprintf(", [%d->%d]", ulen, rlen);
348 tprintf(", [%d]", rlen);
353 #include "xlat/shutdown_modes.h"
357 printfd(tcp, tcp->u_arg[0]);
359 printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
364 SYS_FUNC(getsockname)
366 return decode_sockname(tcp);
370 printpair_fd(struct tcb *tcp, const int i0, const int i1)
380 decode_pair_fd(struct tcb *tcp, const long addr)
384 if (umove_or_printaddr(tcp, addr, &pair))
387 printpair_fd(tcp, pair[0], pair[1]);
391 do_pipe(struct tcb *tcp, int flags_arg)
394 decode_pair_fd(tcp, tcp->u_arg[0]);
395 if (flags_arg >= 0) {
397 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
406 if (exiting(tcp) && !syserror(tcp))
407 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
410 return do_pipe(tcp, -1);
416 return do_pipe(tcp, 1);
422 printxval(addrfams, tcp->u_arg[0], "AF_???");
424 tprint_sock_type(tcp->u_arg[1]);
425 tprintf(", %lu", tcp->u_arg[2]);
428 decode_pair_fd(tcp, tcp->u_arg[3]);
433 #include "xlat/sockoptions.h"
434 #include "xlat/sockipoptions.h"
435 #include "xlat/getsockipoptions.h"
436 #include "xlat/setsockipoptions.h"
437 #include "xlat/sockipv6options.h"
438 #include "xlat/getsockipv6options.h"
439 #include "xlat/setsockipv6options.h"
440 #include "xlat/sockipxoptions.h"
441 #include "xlat/sockrawoptions.h"
442 #include "xlat/sockpacketoptions.h"
443 #include "xlat/socksctpoptions.h"
444 #include "xlat/socktcpoptions.h"
447 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
448 unsigned int name, bool is_getsockopt)
452 printxval(socketlayers, level, "SOL_??");
457 printxval(sockoptions, name, "SO_???");
460 printxvals(name, "IP_???", sockipoptions,
461 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
464 printxvals(name, "IPV6_???", sockipv6options,
465 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
468 printxval(sockipxoptions, name, "IPX_???");
471 printxval(sockpacketoptions, name, "PACKET_???");
474 printxval(socktcpoptions, name, "TCP_???");
477 printxval(socksctpoptions, name, "SCTP_???");
480 printxval(sockrawoptions, name, "RAW_???");
483 /* Other SOL_* protocol levels still need work. */
493 print_linger(struct tcb *tcp, long addr, int len)
495 struct linger linger;
497 if (len != sizeof(linger) ||
498 umove(tcp, addr, &linger) < 0) {
503 tprintf("{onoff=%d, linger=%d}",
510 print_ucred(struct tcb *tcp, long addr, int len)
514 if (len != sizeof(uc) ||
515 umove(tcp, addr, &uc) < 0) {
518 tprintf("{pid=%u, uid=%u, gid=%u}",
524 #endif /* SO_PEERCRED */
526 #ifdef PACKET_STATISTICS
528 print_tpacket_stats(struct tcb *tcp, long addr, int len)
530 struct tpacket_stats stats;
532 if (len != sizeof(stats) ||
533 umove(tcp, addr, &stats) < 0) {
536 tprintf("{packets=%u, drops=%u}",
541 #endif /* PACKET_STATISTICS */
543 #include "xlat/icmpfilterflags.h"
546 print_icmp_filter(struct tcb *tcp, const long addr, int len)
548 struct icmp_filter filter = {};
550 if (len > (int) sizeof(filter))
551 len = sizeof(filter);
557 if (umoven_or_printaddr(tcp, addr, len, &filter))
561 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
566 print_getsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
569 if (addr && verbose(tcp))
574 print_linger(tcp, addr, len);
578 print_ucred(tcp, addr, len);
586 #ifdef PACKET_STATISTICS
587 case PACKET_STATISTICS:
588 print_tpacket_stats(tcp, addr, len);
597 print_icmp_filter(tcp, addr, len);
603 /* default arg printing */
606 if (len == sizeof(int)) {
607 printnum_int(tcp, addr, "%d");
609 printstr(tcp, addr, len);
615 tprintf(", [%d]", len);
621 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
622 tcp->u_arg[1], tcp->u_arg[2], true);
626 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
627 printaddr(tcp->u_arg[3]);
629 printaddr(tcp->u_arg[4]);
631 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
638 #ifdef IP_ADD_MEMBERSHIP
640 print_mreq(struct tcb *tcp, long addr, unsigned int len)
644 if (len < sizeof(mreq)) {
645 printstr(tcp, addr, len);
648 if (umove_or_printaddr(tcp, addr, &mreq))
651 tprints("{imr_multiaddr=inet_addr(");
652 print_quoted_string(inet_ntoa(mreq.imr_multiaddr),
653 16, QUOTE_0_TERMINATED);
654 tprints("), imr_interface=inet_addr(");
655 print_quoted_string(inet_ntoa(mreq.imr_interface),
656 16, QUOTE_0_TERMINATED);
659 #endif /* IP_ADD_MEMBERSHIP */
661 #ifdef IPV6_ADD_MEMBERSHIP
663 print_mreq6(struct tcb *tcp, long addr, unsigned int len)
665 struct ipv6_mreq mreq;
667 if (len < sizeof(mreq))
670 if (umove_or_printaddr(tcp, addr, &mreq))
673 const struct in6_addr *in6 = &mreq.ipv6mr_multiaddr;
674 char address[INET6_ADDRSTRLEN];
676 if (!inet_ntop(AF_INET6, in6, address, sizeof(address)))
679 tprints("{ipv6mr_multiaddr=inet_pton(");
680 print_quoted_string(address, sizeof(address), QUOTE_0_TERMINATED);
681 tprints("), ipv6mr_interface=");
682 print_ifindex(mreq.ipv6mr_interface);
687 printstr(tcp, addr, len);
689 #endif /* IPV6_ADD_MEMBERSHIP */
691 #ifdef MCAST_JOIN_GROUP
693 print_group_req(struct tcb *tcp, long addr, int len)
695 struct group_req greq;
697 if (len != sizeof(greq) ||
698 umove(tcp, addr, &greq) < 0) {
703 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
704 print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
708 #endif /* MCAST_JOIN_GROUP */
710 #ifdef PACKET_RX_RING
712 print_tpacket_req(struct tcb *tcp, long addr, int len)
714 struct tpacket_req req;
716 if (len != sizeof(req) ||
717 umove(tcp, addr, &req) < 0) {
720 tprintf("{block_size=%u, block_nr=%u, "
721 "frame_size=%u, frame_nr=%u}",
728 #endif /* PACKET_RX_RING */
730 #ifdef PACKET_ADD_MEMBERSHIP
731 # include "xlat/packet_mreq_type.h"
734 print_packet_mreq(struct tcb *tcp, long addr, int len)
736 struct packet_mreq mreq;
738 if (len != sizeof(mreq) ||
739 umove(tcp, addr, &mreq) < 0) {
744 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
745 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
746 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
747 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
748 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
749 for (i = 0; i < mreq.mr_alen; ++i)
750 tprintf("%02x", mreq.mr_address[i]);
754 #endif /* PACKET_ADD_MEMBERSHIP */
757 print_setsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
760 if (addr && verbose(tcp))
765 print_linger(tcp, addr, len);
772 #ifdef IP_ADD_MEMBERSHIP
773 case IP_ADD_MEMBERSHIP:
774 case IP_DROP_MEMBERSHIP:
775 print_mreq(tcp, addr, len);
777 #endif /* IP_ADD_MEMBERSHIP */
778 #ifdef MCAST_JOIN_GROUP
779 case MCAST_JOIN_GROUP:
780 case MCAST_LEAVE_GROUP:
781 print_group_req(tcp, addr, len);
783 #endif /* MCAST_JOIN_GROUP */
789 #ifdef IPV6_ADD_MEMBERSHIP
790 case IPV6_ADD_MEMBERSHIP:
791 case IPV6_DROP_MEMBERSHIP:
792 # ifdef IPV6_JOIN_ANYCAST
793 case IPV6_JOIN_ANYCAST:
795 # ifdef IPV6_LEAVE_ANYCAST
796 case IPV6_LEAVE_ANYCAST:
798 print_mreq6(tcp, addr, len);
800 #endif /* IPV6_ADD_MEMBERSHIP */
806 #ifdef PACKET_RX_RING
808 # ifdef PACKET_TX_RING
811 print_tpacket_req(tcp, addr, len);
813 #endif /* PACKET_RX_RING */
814 #ifdef PACKET_ADD_MEMBERSHIP
815 case PACKET_ADD_MEMBERSHIP:
816 case PACKET_DROP_MEMBERSHIP:
817 print_packet_mreq(tcp, addr, len);
819 #endif /* PACKET_ADD_MEMBERSHIP */
826 print_icmp_filter(tcp, addr, len);
832 /* default arg printing */
835 if (len == sizeof(int)) {
836 printnum_int(tcp, addr, "%d");
838 printstr(tcp, addr, len);
844 tprintf(", %d", len);
849 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
850 tcp->u_arg[1], tcp->u_arg[2], false);
851 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
852 tcp->u_arg[3], tcp->u_arg[4]);