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.
34 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #ifdef HAVE_NETINET_TCP_H
39 # include <netinet/tcp.h>
41 #ifdef HAVE_NETINET_UDP_H
42 # include <netinet/udp.h>
44 #ifdef HAVE_NETINET_SCTP_H
45 # include <netinet/sctp.h>
47 #include <arpa/inet.h>
49 #include <asm/types.h>
50 #ifdef HAVE_NETIPX_IPX_H
51 # include <netipx/ipx.h>
53 # include <linux/ipx.h>
56 #if defined(HAVE_LINUX_IP_VS_H)
57 # include <linux/ip_vs.h>
59 #include <linux/netlink.h>
60 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
61 # include <linux/netfilter_arp/arp_tables.h>
63 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
64 # include <linux/netfilter_bridge/ebtables.h>
66 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
67 # include <linux/netfilter_ipv4/ip_tables.h>
69 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
70 # include <linux/netfilter_ipv6/ip6_tables.h>
72 #include <linux/if_packet.h>
73 #include <linux/icmp.h>
75 #include "xlat/socktypes.h"
76 #include "xlat/sock_type_flags.h"
77 #ifndef SOCK_TYPE_MASK
78 # define SOCK_TYPE_MASK 0xf
81 #include "xlat/socketlayers.h"
83 #include "xlat/inet_protocols.h"
85 #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
86 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
88 #include "xlat/netlink_protocols.h"
90 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
91 # include <bluetooth/bluetooth.h>
92 # include "xlat/bt_protocols.h"
96 print_ifindex(unsigned int ifindex)
98 #ifdef HAVE_IF_INDEXTONAME
99 char buf[IFNAMSIZ + 1];
101 if (if_indextoname(ifindex, buf)) {
102 tprints("if_nametoindex(");
103 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
108 tprintf("%u", ifindex);
112 * low bits of the socket type define real socket type,
113 * other bits are socket type flags.
116 tprint_sock_type(unsigned int flags)
118 const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
122 flags &= ~SOCK_TYPE_MASK;
127 printflags(sock_type_flags, flags, "SOCK_???");
132 printxval(addrfams, tcp->u_arg[0], "AF_???");
134 tprint_sock_type(tcp->u_arg[1]);
136 switch (tcp->u_arg[0]) {
139 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
143 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
146 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
148 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
153 tprintf("%lu", tcp->u_arg[2]);
157 return RVAL_DECODED | RVAL_FD;
162 printfd(tcp, tcp->u_arg[0]);
164 decode_sockaddr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
165 tprintf(", %lu", tcp->u_arg[2]);
172 printfd(tcp, tcp->u_arg[0]);
174 tprintf("%lu", tcp->u_arg[1]);
180 do_sockname(struct tcb *tcp, int flags_arg)
183 printfd(tcp, tcp->u_arg[0]);
189 if (!tcp->u_arg[2] || !verbose(tcp) || syserror(tcp) ||
190 umove(tcp, tcp->u_arg[2], &len) < 0) {
191 printaddr(tcp->u_arg[1]);
193 printaddr(tcp->u_arg[2]);
195 decode_sockaddr(tcp, tcp->u_arg[1], len);
196 tprintf(", [%d]", len);
199 if (flags_arg >= 0) {
201 printflags(sock_type_flags, tcp->u_arg[flags_arg],
209 do_sockname(tcp, -1);
221 printfd(tcp, tcp->u_arg[0]);
223 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
224 tprintf(", %lu, ", tcp->u_arg[2]);
226 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
233 printfd(tcp, tcp->u_arg[0]);
235 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
236 tprintf(", %lu, ", tcp->u_arg[2]);
238 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
241 decode_sockaddr(tcp, tcp->u_arg[4], tcp->u_arg[5]);
243 tprintf(", %lu", tcp->u_arg[5]);
250 printfd(tcp, tcp->u_arg[0]);
252 decode_msghdr(tcp, tcp->u_arg[1], (unsigned long) -1L);
255 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
264 printfd(tcp, tcp->u_arg[0]);
267 printaddr(tcp->u_arg[1]);
269 tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
271 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
275 decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_rval, false);
277 tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
279 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
287 printfd(tcp, tcp->u_arg[0]);
291 printaddr(tcp->u_arg[1]);
293 printstr(tcp, tcp->u_arg[1], tcp->u_rval);
295 tprintf(", %lu, ", tcp->u_arg[2]);
296 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
306 printfd(tcp, tcp->u_arg[0]);
311 printaddr(tcp->u_arg[1]);
313 printstr(tcp, tcp->u_arg[1], tcp->u_rval);
316 tprintf(", %lu, ", tcp->u_arg[2]);
318 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
320 if (syserror(tcp) || !tcp->u_arg[4] || !tcp->u_arg[5] ||
321 umove(tcp, tcp->u_arg[5], &fromlen) < 0) {
322 /* from address, len */
323 printaddr(tcp->u_arg[4]);
325 printaddr(tcp->u_arg[5]);
329 decode_sockaddr(tcp, tcp->u_arg[4], fromlen);
331 tprintf(", [%u]", fromlen);
339 printfd(tcp, tcp->u_arg[0]);
343 printaddr(tcp->u_arg[1]);
345 decode_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
348 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
355 static char str[sizeof("left") + TIMESPEC_TEXT_BUFSIZE];
358 printfd(tcp, tcp->u_arg[0]);
361 /* Abusing tcp->auxstr as temp storage.
362 * Will be used and cleared on syscall exit.
364 tcp->auxstr = sprint_timespec(tcp, tcp->u_arg[4]);
366 printaddr(tcp->u_arg[1]);
368 tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
370 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
372 print_timespec(tcp, tcp->u_arg[4]);
377 decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_rval, true);
379 tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
381 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
383 /* timeout on entrance */
384 tprints(tcp->auxstr);
389 if (tcp->u_rval == 0) {
390 tcp->auxstr = "Timeout";
395 /* timeout on exit */
396 snprintf(str, sizeof(str), "left %s",
397 sprint_timespec(tcp, tcp->u_arg[4]));
403 #include "xlat/shutdown_modes.h"
407 printfd(tcp, tcp->u_arg[0]);
409 printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
414 SYS_FUNC(getsockname)
416 return do_sockname(tcp, -1);
420 printpair_fd(struct tcb *tcp, const int i0, const int i1)
430 decode_pair_fd(struct tcb *tcp, const long addr)
434 if (umove_or_printaddr(tcp, addr, &pair))
437 printpair_fd(tcp, pair[0], pair[1]);
441 do_pipe(struct tcb *tcp, int flags_arg)
444 decode_pair_fd(tcp, tcp->u_arg[0]);
445 if (flags_arg >= 0) {
447 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
456 if (exiting(tcp) && !syserror(tcp))
457 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
460 return do_pipe(tcp, -1);
466 return do_pipe(tcp, 1);
472 printxval(addrfams, tcp->u_arg[0], "AF_???");
474 tprint_sock_type(tcp->u_arg[1]);
475 tprintf(", %lu", tcp->u_arg[2]);
478 decode_pair_fd(tcp, tcp->u_arg[3]);
483 #include "xlat/sockoptions.h"
484 #include "xlat/sockipoptions.h"
485 #include "xlat/getsockipoptions.h"
486 #include "xlat/setsockipoptions.h"
487 #include "xlat/sockipv6options.h"
488 #include "xlat/getsockipv6options.h"
489 #include "xlat/setsockipv6options.h"
490 #include "xlat/sockipxoptions.h"
491 #include "xlat/sockrawoptions.h"
492 #include "xlat/sockpacketoptions.h"
493 #include "xlat/socksctpoptions.h"
494 #include "xlat/socktcpoptions.h"
497 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
498 unsigned int name, bool is_getsockopt)
502 printxval(socketlayers, level, "SOL_??");
507 printxval(sockoptions, name, "SO_???");
510 printxvals(name, "IP_???", sockipoptions,
511 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
514 printxvals(name, "IPV6_???", sockipv6options,
515 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
518 printxval(sockipxoptions, name, "IPX_???");
521 printxval(sockpacketoptions, name, "PACKET_???");
524 printxval(socktcpoptions, name, "TCP_???");
527 printxval(socksctpoptions, name, "SCTP_???");
530 printxval(sockrawoptions, name, "RAW_???");
533 /* Other SOL_* protocol levels still need work. */
543 print_linger(struct tcb *tcp, long addr, int len)
545 struct linger linger;
547 if (len != sizeof(linger) ||
548 umove(tcp, addr, &linger) < 0) {
553 tprintf("{onoff=%d, linger=%d}",
560 print_ucred(struct tcb *tcp, long addr, int len)
564 if (len != sizeof(uc) ||
565 umove(tcp, addr, &uc) < 0) {
568 tprintf("{pid=%u, uid=%u, gid=%u}",
574 #endif /* SO_PEERCRED */
576 #ifdef PACKET_STATISTICS
578 print_tpacket_stats(struct tcb *tcp, long addr, int len)
580 struct tpacket_stats stats;
582 if (len != sizeof(stats) ||
583 umove(tcp, addr, &stats) < 0) {
586 tprintf("{packets=%u, drops=%u}",
591 #endif /* PACKET_STATISTICS */
593 #include "xlat/icmpfilterflags.h"
596 print_icmp_filter(struct tcb *tcp, const long addr, int len)
598 struct icmp_filter filter = {};
600 if (len > (int) sizeof(filter))
601 len = sizeof(filter);
607 if (umoven_or_printaddr(tcp, addr, len, &filter))
611 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
616 print_getsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
619 if (addr && verbose(tcp))
624 print_linger(tcp, addr, len);
628 print_ucred(tcp, addr, len);
636 #ifdef PACKET_STATISTICS
637 case PACKET_STATISTICS:
638 print_tpacket_stats(tcp, addr, len);
647 print_icmp_filter(tcp, addr, len);
653 /* default arg printing */
656 if (len == sizeof(int)) {
657 printnum_int(tcp, addr, "%d");
659 printstr(tcp, addr, len);
665 tprintf(", [%d]", len);
671 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
672 tcp->u_arg[1], tcp->u_arg[2], true);
676 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
677 printaddr(tcp->u_arg[3]);
679 printaddr(tcp->u_arg[4]);
681 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
688 #ifdef IP_ADD_MEMBERSHIP
690 print_mreq(struct tcb *tcp, long addr, unsigned int len)
694 if (len < sizeof(mreq)) {
695 printstr(tcp, addr, len);
698 if (umove_or_printaddr(tcp, addr, &mreq))
701 tprints("{imr_multiaddr=inet_addr(");
702 print_quoted_string(inet_ntoa(mreq.imr_multiaddr),
703 16, QUOTE_0_TERMINATED);
704 tprints("), imr_interface=inet_addr(");
705 print_quoted_string(inet_ntoa(mreq.imr_interface),
706 16, QUOTE_0_TERMINATED);
709 #endif /* IP_ADD_MEMBERSHIP */
711 #ifdef IPV6_ADD_MEMBERSHIP
713 print_mreq6(struct tcb *tcp, long addr, unsigned int len)
715 struct ipv6_mreq mreq;
717 if (len < sizeof(mreq))
720 if (umove_or_printaddr(tcp, addr, &mreq))
723 const struct in6_addr *in6 = &mreq.ipv6mr_multiaddr;
724 char address[INET6_ADDRSTRLEN];
726 if (!inet_ntop(AF_INET6, in6, address, sizeof(address)))
729 tprints("{ipv6mr_multiaddr=inet_pton(");
730 print_quoted_string(address, sizeof(address), QUOTE_0_TERMINATED);
731 tprints("), ipv6mr_interface=");
732 print_ifindex(mreq.ipv6mr_interface);
737 printstr(tcp, addr, len);
739 #endif /* IPV6_ADD_MEMBERSHIP */
741 #ifdef MCAST_JOIN_GROUP
743 print_group_req(struct tcb *tcp, long addr, int len)
745 struct group_req greq;
747 if (len != sizeof(greq) ||
748 umove(tcp, addr, &greq) < 0) {
753 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
754 print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
758 #endif /* MCAST_JOIN_GROUP */
760 #ifdef PACKET_RX_RING
762 print_tpacket_req(struct tcb *tcp, long addr, int len)
764 struct tpacket_req req;
766 if (len != sizeof(req) ||
767 umove(tcp, addr, &req) < 0) {
770 tprintf("{block_size=%u, block_nr=%u, "
771 "frame_size=%u, frame_nr=%u}",
778 #endif /* PACKET_RX_RING */
780 #ifdef PACKET_ADD_MEMBERSHIP
781 # include "xlat/packet_mreq_type.h"
784 print_packet_mreq(struct tcb *tcp, long addr, int len)
786 struct packet_mreq mreq;
788 if (len != sizeof(mreq) ||
789 umove(tcp, addr, &mreq) < 0) {
794 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
795 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
796 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
797 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
798 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
799 for (i = 0; i < mreq.mr_alen; ++i)
800 tprintf("%02x", mreq.mr_address[i]);
804 #endif /* PACKET_ADD_MEMBERSHIP */
807 print_setsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
810 if (addr && verbose(tcp))
815 print_linger(tcp, addr, len);
822 #ifdef IP_ADD_MEMBERSHIP
823 case IP_ADD_MEMBERSHIP:
824 case IP_DROP_MEMBERSHIP:
825 print_mreq(tcp, addr, len);
827 #endif /* IP_ADD_MEMBERSHIP */
828 #ifdef MCAST_JOIN_GROUP
829 case MCAST_JOIN_GROUP:
830 case MCAST_LEAVE_GROUP:
831 print_group_req(tcp, addr, len);
833 #endif /* MCAST_JOIN_GROUP */
839 #ifdef IPV6_ADD_MEMBERSHIP
840 case IPV6_ADD_MEMBERSHIP:
841 case IPV6_DROP_MEMBERSHIP:
842 # ifdef IPV6_JOIN_ANYCAST
843 case IPV6_JOIN_ANYCAST:
845 # ifdef IPV6_LEAVE_ANYCAST
846 case IPV6_LEAVE_ANYCAST:
848 print_mreq6(tcp, addr, len);
850 #endif /* IPV6_ADD_MEMBERSHIP */
856 #ifdef PACKET_RX_RING
858 # ifdef PACKET_TX_RING
861 print_tpacket_req(tcp, addr, len);
863 #endif /* PACKET_RX_RING */
864 #ifdef PACKET_ADD_MEMBERSHIP
865 case PACKET_ADD_MEMBERSHIP:
866 case PACKET_DROP_MEMBERSHIP:
867 print_packet_mreq(tcp, addr, len);
869 #endif /* PACKET_ADD_MEMBERSHIP */
876 print_icmp_filter(tcp, addr, len);
882 /* default arg printing */
885 if (len == sizeof(int)) {
886 printnum_int(tcp, addr, "%d");
888 printstr(tcp, addr, len);
894 tprintf(", %d", len);
899 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
900 tcp->u_arg[1], tcp->u_arg[2], false);
901 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
902 tcp->u_arg[3], tcp->u_arg[4]);