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-2018 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"
88 # define AF_BLUETOOTH 31
90 #include "xlat/bt_protocols.h"
93 decode_sockbuf(struct tcb *const tcp, const int fd, const kernel_ulong_t addr,
94 const kernel_ulong_t addrlen)
97 switch (verbose(tcp) ? getfdproto(tcp, fd) : SOCK_PROTO_UNKNOWN) {
98 case SOCK_PROTO_NETLINK:
99 decode_netlink(tcp, fd, addr, addrlen);
102 printstrn(tcp, addr, addrlen);
107 * low bits of the socket type define real socket type,
108 * other bits are socket type flags.
111 tprint_sock_type(unsigned int flags)
113 const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
116 print_xlat_ex(flags & SOCK_TYPE_MASK, str, XLAT_STYLE_DEFAULT);
117 flags &= ~SOCK_TYPE_MASK;
122 printflags(sock_type_flags, flags, "SOCK_???");
127 printxval(addrfams, tcp->u_arg[0], "AF_???");
129 tprint_sock_type(tcp->u_arg[1]);
131 switch (tcp->u_arg[0]) {
134 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
138 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
142 printxval_index(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
146 tprintf("%" PRI_klu, tcp->u_arg[2]);
150 return RVAL_DECODED | RVAL_FD;
154 fetch_socklen(struct tcb *const tcp, int *const plen,
155 const kernel_ulong_t sockaddr, const kernel_ulong_t socklen)
157 return verbose(tcp) && sockaddr && socklen
158 && umove(tcp, socklen, plen) == 0;
162 decode_sockname(struct tcb *tcp)
167 printfd(tcp, tcp->u_arg[0]);
169 if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
170 set_tcb_priv_ulong(tcp, ulen);
173 printaddr(tcp->u_arg[1]);
175 printaddr(tcp->u_arg[2]);
180 ulen = get_tcb_priv_ulong(tcp);
182 if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
183 printaddr(tcp->u_arg[1]);
184 tprintf(", [%d]", ulen);
186 decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
188 tprintf(", [%d->%d]", ulen, rlen);
190 tprintf(", [%d]", rlen);
198 return decode_sockname(tcp) | RVAL_FD;
203 int rc = decode_sockname(tcp);
205 if (rc & RVAL_DECODED) {
207 printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
215 printfd(tcp, tcp->u_arg[0]);
217 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
218 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
220 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
227 printfd(tcp, tcp->u_arg[0]);
229 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
230 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
232 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
234 const int addrlen = tcp->u_arg[5];
236 decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
238 tprintf(", %d", addrlen);
246 printfd(tcp, tcp->u_arg[0]);
250 printaddr(tcp->u_arg[1]);
252 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
256 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
257 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
267 printfd(tcp, tcp->u_arg[0]);
269 if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
270 set_tcb_priv_ulong(tcp, ulen);
275 printaddr(tcp->u_arg[1]);
277 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
281 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
283 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
286 ulen = get_tcb_priv_ulong(tcp);
288 if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
290 printaddr(tcp->u_arg[4]);
293 printaddr(tcp->u_arg[5]);
298 printaddr(tcp->u_arg[4]);
300 tprintf(", [%d]", ulen);
304 decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
307 tprintf(", [%d->%d]", ulen, rlen);
309 tprintf(", [%d]", rlen);
314 SYS_FUNC(getsockname)
316 return decode_sockname(tcp);
320 printpair_fd(struct tcb *tcp, const int i0, const int i1)
330 decode_pair_fd(struct tcb *const tcp, const kernel_ulong_t addr)
334 if (umove_or_printaddr(tcp, addr, &pair))
337 printpair_fd(tcp, pair[0], pair[1]);
341 do_pipe(struct tcb *tcp, int flags_arg)
344 decode_pair_fd(tcp, tcp->u_arg[0]);
345 if (flags_arg >= 0) {
347 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
355 #if HAVE_ARCH_GETRVAL2
356 if (exiting(tcp) && !syserror(tcp))
357 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
360 return do_pipe(tcp, -1);
366 return do_pipe(tcp, 1);
372 printxval(addrfams, tcp->u_arg[0], "AF_???");
374 tprint_sock_type(tcp->u_arg[1]);
375 tprintf(", %" PRI_klu, tcp->u_arg[2]);
378 decode_pair_fd(tcp, tcp->u_arg[3]);
383 #include "xlat/sockoptions.h"
384 #include "xlat/sockipoptions.h"
385 #include "xlat/getsockipoptions.h"
386 #include "xlat/setsockipoptions.h"
387 #include "xlat/sockipv6options.h"
388 #include "xlat/getsockipv6options.h"
389 #include "xlat/setsockipv6options.h"
390 #include "xlat/sockipxoptions.h"
391 #include "xlat/socknetlinkoptions.h"
392 #include "xlat/sockpacketoptions.h"
393 #include "xlat/sockrawoptions.h"
394 #include "xlat/socksctpoptions.h"
395 #include "xlat/socktcpoptions.h"
398 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
399 unsigned int name, bool is_getsockopt)
403 printxval_search(socketlayers, level, "SOL_??");
408 printxval(sockoptions, name, "SO_???");
411 printxvals(name, "IP_???", sockipoptions,
412 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
415 printxvals(name, "IPV6_???", sockipv6options,
416 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
419 printxval(sockipxoptions, name, "IPX_???");
422 printxval(sockpacketoptions, name, "PACKET_???");
425 printxval(socktcpoptions, name, "TCP_???");
428 printxval(socksctpoptions, name, "SCTP_???");
431 printxval(sockrawoptions, name, "RAW_???");
434 printxval(socknetlinkoptions, name, "NETLINK_???");
437 /* Other SOL_* protocol levels still need work. */
447 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
450 struct linger linger;
452 if (len < (int) sizeof(linger)) {
454 } else if (!umove_or_printaddr(tcp, addr, &linger)) {
455 PRINT_FIELD_D("{", linger, l_onoff);
456 PRINT_FIELD_D(", ", linger, l_linger);
462 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
465 struct linger linger;
467 if (len < sizeof(linger)) {
468 if (len != sizeof(linger.l_onoff)) {
469 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
473 len = sizeof(linger);
476 if (umoven(tcp, addr, len, &linger) < 0) {
481 PRINT_FIELD_D("{", linger, l_onoff);
482 if (len == sizeof(linger))
483 PRINT_FIELD_D(", ", linger, l_linger);
489 print_ucred(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int len)
493 if (len < sizeof(uc)) {
494 if (len != sizeof(uc.pid)
495 && len != offsetofend(struct ucred, uid)) {
496 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
503 if (umoven(tcp, addr, len, &uc) < 0) {
508 PRINT_FIELD_D("{", uc, pid);
509 if (len > sizeof(uc.pid))
510 PRINT_FIELD_UID(", ", uc, uid);
511 if (len == sizeof(uc))
512 PRINT_FIELD_UID(", ", uc, gid);
515 #endif /* SO_PEERCRED */
517 #ifdef PACKET_STATISTICS
519 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
522 struct tpacket_stats stats;
524 if (len != sizeof(stats) ||
525 umove(tcp, addr, &stats) < 0) {
528 PRINT_FIELD_U("{", stats, tp_packets);
529 PRINT_FIELD_U("{", stats, tp_drops);
533 #endif /* PACKET_STATISTICS */
535 #include "xlat/icmpfilterflags.h"
538 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
540 struct icmp_filter filter = {};
542 if (len > (int) sizeof(filter))
543 len = sizeof(filter);
549 if (umoven_or_printaddr(tcp, addr, len, &filter))
553 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
558 print_uint32(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
560 tprintf("%u", *(uint32_t *) elem_buf);
566 print_getsockopt(struct tcb *const tcp, const unsigned int level,
567 const unsigned int name, const kernel_ulong_t addr,
568 const int ulen, const int rlen)
570 if (addr && verbose(tcp))
575 print_get_linger(tcp, addr, rlen);
579 print_ucred(tcp, addr, rlen);
582 #ifdef SO_ATTACH_FILTER
583 case SO_ATTACH_FILTER:
584 if (rlen && (unsigned short) rlen == (unsigned int) rlen)
585 print_sock_fprog(tcp, addr, rlen);
589 #endif /* SO_ATTACH_FILTER */
595 #ifdef PACKET_STATISTICS
596 case PACKET_STATISTICS:
597 print_tpacket_stats(tcp, addr, rlen);
606 print_icmp_filter(tcp, addr, rlen);
612 if (ulen < 0 || rlen < 0) {
614 * As the kernel neither accepts nor returns a negative
615 * length, in case of successful getsockopt syscall
616 * invocation these negative values must have come
623 case NETLINK_LIST_MEMBERSHIPS: {
625 print_array(tcp, addr, MIN(ulen, rlen) / sizeof(buf),
627 umoven_or_printaddr, print_uint32, 0);
631 printnum_int(tcp, addr, "%d");
637 /* default arg printing */
640 if (rlen == sizeof(int)) {
641 printnum_int(tcp, addr, "%d");
643 printstrn(tcp, addr, rlen);
655 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
656 tcp->u_arg[1], tcp->u_arg[2], true);
658 if (verbose(tcp) && tcp->u_arg[4]
659 && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
660 set_tcb_priv_ulong(tcp, ulen);
663 printaddr(tcp->u_arg[3]);
665 printaddr(tcp->u_arg[4]);
669 ulen = get_tcb_priv_ulong(tcp);
671 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
672 printaddr(tcp->u_arg[3]);
673 tprintf(", [%d]", ulen);
675 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
676 tcp->u_arg[3], ulen, rlen);
678 tprintf(", [%d->%d]", ulen, rlen);
680 tprintf(", [%d]", rlen);
686 #ifdef IP_ADD_MEMBERSHIP
688 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
693 if (len < (int) sizeof(mreq)) {
695 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
696 PRINT_FIELD_INET4_ADDR("{", mreq, imr_multiaddr);
697 PRINT_FIELD_INET4_ADDR(", ", mreq, imr_interface);
701 #endif /* IP_ADD_MEMBERSHIP */
703 #ifdef IPV6_ADD_MEMBERSHIP
705 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
708 struct ipv6_mreq mreq;
710 if (len < (int) sizeof(mreq)) {
712 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
713 PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
714 PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
718 #endif /* IPV6_ADD_MEMBERSHIP */
720 #ifdef PACKET_RX_RING
722 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
724 struct tpacket_req req;
726 if (len != sizeof(req) ||
727 umove(tcp, addr, &req) < 0) {
730 PRINT_FIELD_U("{", req, tp_block_size);
731 PRINT_FIELD_U(", ", req, tp_block_nr);
732 PRINT_FIELD_U(", ", req, tp_frame_size);
733 PRINT_FIELD_U(", ", req, tp_frame_nr);
737 #endif /* PACKET_RX_RING */
739 #ifdef PACKET_ADD_MEMBERSHIP
740 # include "xlat/packet_mreq_type.h"
743 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
745 struct packet_mreq mreq;
747 if (len != sizeof(mreq) ||
748 umove(tcp, addr, &mreq) < 0) {
753 PRINT_FIELD_IFINDEX("{", mreq, mr_ifindex);
754 PRINT_FIELD_XVAL(", ", mreq, mr_type, packet_mreq_type,
756 PRINT_FIELD_U(", ", mreq, mr_alen);
757 tprints(", mr_address=");
758 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
759 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
760 for (i = 0; i < mreq.mr_alen; ++i)
761 tprintf("%02x", mreq.mr_address[i]);
765 #endif /* PACKET_ADD_MEMBERSHIP */
768 print_setsockopt(struct tcb *const tcp, const unsigned int level,
769 const unsigned int name, const kernel_ulong_t addr,
772 if (addr && verbose(tcp))
777 print_set_linger(tcp, addr, len);
779 #ifdef SO_ATTACH_FILTER
780 case SO_ATTACH_FILTER:
781 # ifdef SO_ATTACH_REUSEPORT_CBPF
782 case SO_ATTACH_REUSEPORT_CBPF:
784 if ((unsigned int) len == get_sock_fprog_size())
785 decode_sock_fprog(tcp, addr);
789 #endif /* SO_ATTACH_FILTER */
795 #ifdef IP_ADD_MEMBERSHIP
796 case IP_ADD_MEMBERSHIP:
797 case IP_DROP_MEMBERSHIP:
798 print_mreq(tcp, addr, len);
800 #endif /* IP_ADD_MEMBERSHIP */
801 #ifdef MCAST_JOIN_GROUP
802 case MCAST_JOIN_GROUP:
803 case MCAST_LEAVE_GROUP:
804 print_group_req(tcp, addr, len);
806 #endif /* MCAST_JOIN_GROUP */
812 #ifdef IPV6_ADD_MEMBERSHIP
813 case IPV6_ADD_MEMBERSHIP:
814 case IPV6_DROP_MEMBERSHIP:
815 # ifdef IPV6_JOIN_ANYCAST
816 case IPV6_JOIN_ANYCAST:
818 # ifdef IPV6_LEAVE_ANYCAST
819 case IPV6_LEAVE_ANYCAST:
821 print_mreq6(tcp, addr, len);
823 #endif /* IPV6_ADD_MEMBERSHIP */
824 #ifdef MCAST_JOIN_GROUP
825 case MCAST_JOIN_GROUP:
826 case MCAST_LEAVE_GROUP:
827 print_group_req(tcp, addr, len);
829 #endif /* MCAST_JOIN_GROUP */
835 #ifdef PACKET_RX_RING
837 # ifdef PACKET_TX_RING
840 print_tpacket_req(tcp, addr, len);
842 #endif /* PACKET_RX_RING */
843 #ifdef PACKET_ADD_MEMBERSHIP
844 case PACKET_ADD_MEMBERSHIP:
845 case PACKET_DROP_MEMBERSHIP:
846 print_packet_mreq(tcp, addr, len);
848 #endif /* PACKET_ADD_MEMBERSHIP */
855 print_icmp_filter(tcp, addr, len);
861 if (len < (int) sizeof(int))
864 printnum_int(tcp, addr, "%d");
868 /* default arg printing */
871 if (len == sizeof(int)) {
872 printnum_int(tcp, addr, "%d");
874 printstrn(tcp, addr, len);
883 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
884 tcp->u_arg[1], tcp->u_arg[2], false);
885 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
886 tcp->u_arg[3], tcp->u_arg[4]);
887 tprintf(", %d", (int) tcp->u_arg[4]);