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>
6 * Copyright (c) 1999-2017 The strace developers.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "print_fields.h"
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #ifdef HAVE_NETINET_TCP_H
41 # include <netinet/tcp.h>
43 #ifdef HAVE_NETINET_UDP_H
44 # include <netinet/udp.h>
46 #ifdef HAVE_NETINET_SCTP_H
47 # include <netinet/sctp.h>
49 #include <arpa/inet.h>
51 #include <asm/types.h>
52 #ifdef HAVE_NETIPX_IPX_H
53 # include <netipx/ipx.h>
55 # include <linux/ipx.h>
58 #if defined(HAVE_LINUX_IP_VS_H)
59 # include <linux/ip_vs.h>
62 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
63 # include <linux/netfilter_arp/arp_tables.h>
65 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
66 # include <linux/netfilter_bridge/ebtables.h>
68 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
69 # include <linux/netfilter_ipv4/ip_tables.h>
71 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
72 # include <linux/netfilter_ipv6/ip6_tables.h>
74 #include <linux/if_packet.h>
75 #include <linux/icmp.h>
77 #include "xlat/socktypes.h"
78 #include "xlat/sock_type_flags.h"
79 #ifndef SOCK_TYPE_MASK
80 # define SOCK_TYPE_MASK 0xf
83 #include "xlat/socketlayers.h"
85 #include "xlat/inet_protocols.h"
87 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
88 # include <bluetooth/bluetooth.h>
89 # include "xlat/bt_protocols.h"
93 print_ifindex(unsigned int ifindex)
95 #ifdef HAVE_IF_INDEXTONAME
96 char buf[IFNAMSIZ + 1];
98 if (if_indextoname(ifindex, buf)) {
99 tprints("if_nametoindex(");
100 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
105 tprintf("%u", ifindex);
109 decode_sockbuf(struct tcb *const tcp, const int fd, const kernel_ulong_t addr,
110 const kernel_ulong_t addrlen)
113 switch (verbose(tcp) ? getfdproto(tcp, fd) : SOCK_PROTO_UNKNOWN) {
114 case SOCK_PROTO_NETLINK:
115 decode_netlink(tcp, fd, addr, addrlen);
118 printstrn(tcp, addr, addrlen);
123 * low bits of the socket type define real socket type,
124 * other bits are socket type flags.
127 tprint_sock_type(unsigned int flags)
129 const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
133 flags &= ~SOCK_TYPE_MASK;
138 printflags(sock_type_flags, flags, "SOCK_???");
143 printxval(addrfams, tcp->u_arg[0], "AF_???");
145 tprint_sock_type(tcp->u_arg[1]);
147 switch (tcp->u_arg[0]) {
150 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
154 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
157 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
159 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
164 tprintf("%" PRI_klu, tcp->u_arg[2]);
168 return RVAL_DECODED | RVAL_FD;
173 printfd(tcp, tcp->u_arg[0]);
175 const int addrlen = tcp->u_arg[2];
176 decode_sockaddr(tcp, tcp->u_arg[1], addrlen);
177 tprintf(", %d", addrlen);
184 printfd(tcp, tcp->u_arg[0]);
186 tprintf("%" PRI_klu, tcp->u_arg[1]);
192 fetch_socklen(struct tcb *const tcp, int *const plen,
193 const kernel_ulong_t sockaddr, const kernel_ulong_t socklen)
195 return verbose(tcp) && sockaddr && socklen
196 && umove(tcp, socklen, plen) == 0;
200 decode_sockname(struct tcb *tcp)
205 printfd(tcp, tcp->u_arg[0]);
207 if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
208 set_tcb_priv_ulong(tcp, ulen);
211 printaddr(tcp->u_arg[1]);
213 printaddr(tcp->u_arg[2]);
218 ulen = get_tcb_priv_ulong(tcp);
220 if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
221 printaddr(tcp->u_arg[1]);
222 tprintf(", [%d]", ulen);
224 decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
226 tprintf(", [%d->%d]", ulen, rlen);
228 tprintf(", [%d]", rlen);
236 return decode_sockname(tcp) | RVAL_FD;
241 int rc = decode_sockname(tcp);
243 if (rc & RVAL_DECODED) {
245 printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
253 printfd(tcp, tcp->u_arg[0]);
255 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
256 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
258 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
265 printfd(tcp, tcp->u_arg[0]);
267 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
268 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
270 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
272 const int addrlen = tcp->u_arg[5];
274 decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
276 tprintf(", %d", addrlen);
284 printfd(tcp, tcp->u_arg[0]);
288 printaddr(tcp->u_arg[1]);
290 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
294 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
295 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
305 printfd(tcp, tcp->u_arg[0]);
307 if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
308 set_tcb_priv_ulong(tcp, ulen);
313 printaddr(tcp->u_arg[1]);
315 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
319 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
321 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
324 ulen = get_tcb_priv_ulong(tcp);
326 if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
328 printaddr(tcp->u_arg[4]);
331 printaddr(tcp->u_arg[5]);
336 printaddr(tcp->u_arg[4]);
338 tprintf(", [%d]", ulen);
342 decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
345 tprintf(", [%d->%d]", ulen, rlen);
347 tprintf(", [%d]", rlen);
352 #include "xlat/shutdown_modes.h"
356 printfd(tcp, tcp->u_arg[0]);
358 printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
363 SYS_FUNC(getsockname)
365 return decode_sockname(tcp);
369 printpair_fd(struct tcb *tcp, const int i0, const int i1)
379 decode_pair_fd(struct tcb *const tcp, const kernel_ulong_t addr)
383 if (umove_or_printaddr(tcp, addr, &pair))
386 printpair_fd(tcp, pair[0], pair[1]);
390 do_pipe(struct tcb *tcp, int flags_arg)
393 decode_pair_fd(tcp, tcp->u_arg[0]);
394 if (flags_arg >= 0) {
396 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
405 if (exiting(tcp) && !syserror(tcp))
406 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
409 return do_pipe(tcp, -1);
415 return do_pipe(tcp, 1);
421 printxval(addrfams, tcp->u_arg[0], "AF_???");
423 tprint_sock_type(tcp->u_arg[1]);
424 tprintf(", %" PRI_klu, tcp->u_arg[2]);
427 decode_pair_fd(tcp, tcp->u_arg[3]);
432 #include "xlat/sockoptions.h"
433 #include "xlat/sockipoptions.h"
434 #include "xlat/getsockipoptions.h"
435 #include "xlat/setsockipoptions.h"
436 #include "xlat/sockipv6options.h"
437 #include "xlat/getsockipv6options.h"
438 #include "xlat/setsockipv6options.h"
439 #include "xlat/sockipxoptions.h"
440 #include "xlat/sockrawoptions.h"
441 #include "xlat/sockpacketoptions.h"
442 #include "xlat/socksctpoptions.h"
443 #include "xlat/socktcpoptions.h"
446 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
447 unsigned int name, bool is_getsockopt)
451 printxval(socketlayers, level, "SOL_??");
456 printxval(sockoptions, name, "SO_???");
459 printxvals(name, "IP_???", sockipoptions,
460 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
463 printxvals(name, "IPV6_???", sockipv6options,
464 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
467 printxval(sockipxoptions, name, "IPX_???");
470 printxval(sockpacketoptions, name, "PACKET_???");
473 printxval(socktcpoptions, name, "TCP_???");
476 printxval(socksctpoptions, name, "SCTP_???");
479 printxval(sockrawoptions, name, "RAW_???");
482 /* Other SOL_* protocol levels still need work. */
492 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
493 const unsigned int len)
495 struct linger linger;
497 if (len < sizeof(linger) ||
498 umove(tcp, addr, &linger) < 0) {
503 PRINT_FIELD_D("{", linger, l_onoff);
504 PRINT_FIELD_D(", ", linger, l_linger);
509 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
512 struct linger linger;
514 if (len < sizeof(linger)) {
515 if (len != sizeof(linger.l_onoff)) {
516 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
520 len = sizeof(linger);
523 if (umoven(tcp, addr, len, &linger) < 0) {
528 PRINT_FIELD_D("{", linger, l_onoff);
529 if (len == sizeof(linger))
530 PRINT_FIELD_D(", ", linger, l_linger);
536 print_ucred(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int len)
540 if (len < sizeof(uc)) {
541 if (len != sizeof(uc.pid)
542 && len != offsetofend(struct ucred, uid)) {
543 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
550 if (umoven(tcp, addr, len, &uc) < 0) {
555 PRINT_FIELD_D("{", uc, pid);
556 if (len > sizeof(uc.pid))
557 PRINT_FIELD_UID(", ", uc, uid);
558 if (len == sizeof(uc))
559 PRINT_FIELD_UID(", ", uc, gid);
562 #endif /* SO_PEERCRED */
564 #ifdef PACKET_STATISTICS
566 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
569 struct tpacket_stats stats;
571 if (len != sizeof(stats) ||
572 umove(tcp, addr, &stats) < 0) {
575 tprintf("{packets=%u, drops=%u}",
580 #endif /* PACKET_STATISTICS */
582 #include "xlat/icmpfilterflags.h"
585 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
587 struct icmp_filter filter = {};
589 if (len > (int) sizeof(filter))
590 len = sizeof(filter);
596 if (umoven_or_printaddr(tcp, addr, len, &filter))
600 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
605 print_getsockopt(struct tcb *const tcp, const unsigned int level,
606 const unsigned int name, const kernel_ulong_t addr,
609 if (addr && verbose(tcp))
614 print_get_linger(tcp, addr, len);
618 print_ucred(tcp, addr, len);
621 #ifdef SO_ATTACH_FILTER
622 case SO_ATTACH_FILTER:
623 if (len && (unsigned short) len == (unsigned int) len)
624 print_sock_fprog(tcp, addr, len);
628 #endif /* SO_ATTACH_FILTER */
634 #ifdef PACKET_STATISTICS
635 case PACKET_STATISTICS:
636 print_tpacket_stats(tcp, addr, len);
645 print_icmp_filter(tcp, addr, len);
651 /* default arg printing */
654 if (len == sizeof(int)) {
655 printnum_int(tcp, addr, "%d");
657 printstrn(tcp, addr, len);
669 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
670 tcp->u_arg[1], tcp->u_arg[2], true);
672 if (verbose(tcp) && tcp->u_arg[4]
673 && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
674 set_tcb_priv_ulong(tcp, ulen);
677 printaddr(tcp->u_arg[3]);
679 printaddr(tcp->u_arg[4]);
683 ulen = get_tcb_priv_ulong(tcp);
685 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
686 printaddr(tcp->u_arg[3]);
687 tprintf(", [%d]", ulen);
689 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
690 tcp->u_arg[3], rlen);
692 tprintf(", [%d->%d]", ulen, rlen);
694 tprintf(", [%d]", rlen);
700 #ifdef IP_ADD_MEMBERSHIP
702 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
703 const unsigned int len)
707 if (len < sizeof(mreq)) {
708 printstrn(tcp, addr, len);
711 if (umove_or_printaddr(tcp, addr, &mreq))
714 tprintf("{imr_multiaddr=inet_addr(\"%s\")",
715 inet_ntoa(mreq.imr_multiaddr));
716 tprintf(", imr_interface=inet_addr(\"%s\")}",
717 inet_ntoa(mreq.imr_interface));
719 #endif /* IP_ADD_MEMBERSHIP */
721 #ifdef IPV6_ADD_MEMBERSHIP
723 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
724 const unsigned int len)
726 struct ipv6_mreq mreq;
728 if (len < sizeof(mreq)) {
729 printstrn(tcp, addr, len);
732 if (umove_or_printaddr(tcp, addr, &mreq))
736 print_inet_addr(AF_INET6, &mreq.ipv6mr_multiaddr,
737 sizeof(mreq.ipv6mr_multiaddr), "ipv6mr_multiaddr");
739 tprints(", ipv6mr_interface=");
740 print_ifindex(mreq.ipv6mr_interface);
743 #endif /* IPV6_ADD_MEMBERSHIP */
745 #ifdef MCAST_JOIN_GROUP
747 print_group_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
749 struct group_req greq;
751 if (len != sizeof(greq) ||
752 umove(tcp, addr, &greq) < 0) {
757 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
758 print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
762 #endif /* MCAST_JOIN_GROUP */
764 #ifdef PACKET_RX_RING
766 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
768 struct tpacket_req req;
770 if (len != sizeof(req) ||
771 umove(tcp, addr, &req) < 0) {
774 tprintf("{block_size=%u, block_nr=%u, "
775 "frame_size=%u, frame_nr=%u}",
782 #endif /* PACKET_RX_RING */
784 #ifdef PACKET_ADD_MEMBERSHIP
785 # include "xlat/packet_mreq_type.h"
788 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
790 struct packet_mreq mreq;
792 if (len != sizeof(mreq) ||
793 umove(tcp, addr, &mreq) < 0) {
798 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
799 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
800 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
801 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
802 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
803 for (i = 0; i < mreq.mr_alen; ++i)
804 tprintf("%02x", mreq.mr_address[i]);
808 #endif /* PACKET_ADD_MEMBERSHIP */
811 print_setsockopt(struct tcb *const tcp, const unsigned int level,
812 const unsigned int name, const kernel_ulong_t addr,
815 if (addr && verbose(tcp))
820 print_set_linger(tcp, addr, len);
822 #ifdef SO_ATTACH_FILTER
823 case SO_ATTACH_FILTER:
824 # ifdef SO_ATTACH_REUSEPORT_CBPF
825 case SO_ATTACH_REUSEPORT_CBPF:
827 if ((unsigned int) len == get_sock_fprog_size())
828 decode_sock_fprog(tcp, addr);
832 #endif /* SO_ATTACH_FILTER */
838 #ifdef IP_ADD_MEMBERSHIP
839 case IP_ADD_MEMBERSHIP:
840 case IP_DROP_MEMBERSHIP:
841 print_mreq(tcp, addr, len);
843 #endif /* IP_ADD_MEMBERSHIP */
844 #ifdef MCAST_JOIN_GROUP
845 case MCAST_JOIN_GROUP:
846 case MCAST_LEAVE_GROUP:
847 print_group_req(tcp, addr, len);
849 #endif /* MCAST_JOIN_GROUP */
855 #ifdef IPV6_ADD_MEMBERSHIP
856 case IPV6_ADD_MEMBERSHIP:
857 case IPV6_DROP_MEMBERSHIP:
858 # ifdef IPV6_JOIN_ANYCAST
859 case IPV6_JOIN_ANYCAST:
861 # ifdef IPV6_LEAVE_ANYCAST
862 case IPV6_LEAVE_ANYCAST:
864 print_mreq6(tcp, addr, len);
866 #endif /* IPV6_ADD_MEMBERSHIP */
872 #ifdef PACKET_RX_RING
874 # ifdef PACKET_TX_RING
877 print_tpacket_req(tcp, addr, len);
879 #endif /* PACKET_RX_RING */
880 #ifdef PACKET_ADD_MEMBERSHIP
881 case PACKET_ADD_MEMBERSHIP:
882 case PACKET_DROP_MEMBERSHIP:
883 print_packet_mreq(tcp, addr, len);
885 #endif /* PACKET_ADD_MEMBERSHIP */
892 print_icmp_filter(tcp, addr, len);
898 /* default arg printing */
901 if (len == sizeof(int)) {
902 printnum_int(tcp, addr, "%d");
904 printstrn(tcp, addr, len);
913 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
914 tcp->u_arg[1], tcp->u_arg[2], false);
915 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
916 tcp->u_arg[3], tcp->u_arg[4]);
917 tprintf(", %d", (int) tcp->u_arg[4]);