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 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);
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_???");
141 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
143 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
148 tprintf("%" PRI_klu, tcp->u_arg[2]);
152 return RVAL_DECODED | RVAL_FD;
156 fetch_socklen(struct tcb *const tcp, int *const plen,
157 const kernel_ulong_t sockaddr, const kernel_ulong_t socklen)
159 return verbose(tcp) && sockaddr && socklen
160 && umove(tcp, socklen, plen) == 0;
164 decode_sockname(struct tcb *tcp)
169 printfd(tcp, tcp->u_arg[0]);
171 if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
172 set_tcb_priv_ulong(tcp, ulen);
175 printaddr(tcp->u_arg[1]);
177 printaddr(tcp->u_arg[2]);
182 ulen = get_tcb_priv_ulong(tcp);
184 if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
185 printaddr(tcp->u_arg[1]);
186 tprintf(", [%d]", ulen);
188 decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
190 tprintf(", [%d->%d]", ulen, rlen);
192 tprintf(", [%d]", rlen);
200 return decode_sockname(tcp) | RVAL_FD;
205 int rc = decode_sockname(tcp);
207 if (rc & RVAL_DECODED) {
209 printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
217 printfd(tcp, tcp->u_arg[0]);
219 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
220 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
222 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
229 printfd(tcp, tcp->u_arg[0]);
231 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
232 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
234 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
236 const int addrlen = tcp->u_arg[5];
238 decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
240 tprintf(", %d", addrlen);
248 printfd(tcp, tcp->u_arg[0]);
252 printaddr(tcp->u_arg[1]);
254 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
258 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
259 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
269 printfd(tcp, tcp->u_arg[0]);
271 if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
272 set_tcb_priv_ulong(tcp, ulen);
277 printaddr(tcp->u_arg[1]);
279 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
283 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
285 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
288 ulen = get_tcb_priv_ulong(tcp);
290 if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
292 printaddr(tcp->u_arg[4]);
295 printaddr(tcp->u_arg[5]);
300 printaddr(tcp->u_arg[4]);
302 tprintf(", [%d]", ulen);
306 decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
309 tprintf(", [%d->%d]", ulen, rlen);
311 tprintf(", [%d]", rlen);
316 SYS_FUNC(getsockname)
318 return decode_sockname(tcp);
322 printpair_fd(struct tcb *tcp, const int i0, const int i1)
332 decode_pair_fd(struct tcb *const tcp, const kernel_ulong_t addr)
336 if (umove_or_printaddr(tcp, addr, &pair))
339 printpair_fd(tcp, pair[0], pair[1]);
343 do_pipe(struct tcb *tcp, int flags_arg)
346 decode_pair_fd(tcp, tcp->u_arg[0]);
347 if (flags_arg >= 0) {
349 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
358 if (exiting(tcp) && !syserror(tcp))
359 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
362 return do_pipe(tcp, -1);
368 return do_pipe(tcp, 1);
374 printxval(addrfams, tcp->u_arg[0], "AF_???");
376 tprint_sock_type(tcp->u_arg[1]);
377 tprintf(", %" PRI_klu, tcp->u_arg[2]);
380 decode_pair_fd(tcp, tcp->u_arg[3]);
385 #include "xlat/sockoptions.h"
386 #include "xlat/sockipoptions.h"
387 #include "xlat/getsockipoptions.h"
388 #include "xlat/setsockipoptions.h"
389 #include "xlat/sockipv6options.h"
390 #include "xlat/getsockipv6options.h"
391 #include "xlat/setsockipv6options.h"
392 #include "xlat/sockipxoptions.h"
393 #include "xlat/socknetlinkoptions.h"
394 #include "xlat/sockpacketoptions.h"
395 #include "xlat/sockrawoptions.h"
396 #include "xlat/socksctpoptions.h"
397 #include "xlat/socktcpoptions.h"
400 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
401 unsigned int name, bool is_getsockopt)
405 printxval(socketlayers, level, "SOL_??");
410 printxval(sockoptions, name, "SO_???");
413 printxvals(name, "IP_???", sockipoptions,
414 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
417 printxvals(name, "IPV6_???", sockipv6options,
418 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
421 printxval(sockipxoptions, name, "IPX_???");
424 printxval(sockpacketoptions, name, "PACKET_???");
427 printxval(socktcpoptions, name, "TCP_???");
430 printxval(socksctpoptions, name, "SCTP_???");
433 printxval(sockrawoptions, name, "RAW_???");
436 printxval(socknetlinkoptions, name, "NETLINK_???");
439 /* Other SOL_* protocol levels still need work. */
449 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
452 struct linger linger;
454 if (len < (int) sizeof(linger)) {
456 } else if (!umove_or_printaddr(tcp, addr, &linger)) {
457 PRINT_FIELD_D("{", linger, l_onoff);
458 PRINT_FIELD_D(", ", linger, l_linger);
464 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
467 struct linger linger;
469 if (len < sizeof(linger)) {
470 if (len != sizeof(linger.l_onoff)) {
471 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
475 len = sizeof(linger);
478 if (umoven(tcp, addr, len, &linger) < 0) {
483 PRINT_FIELD_D("{", linger, l_onoff);
484 if (len == sizeof(linger))
485 PRINT_FIELD_D(", ", linger, l_linger);
491 print_ucred(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int len)
495 if (len < sizeof(uc)) {
496 if (len != sizeof(uc.pid)
497 && len != offsetofend(struct ucred, uid)) {
498 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
505 if (umoven(tcp, addr, len, &uc) < 0) {
510 PRINT_FIELD_D("{", uc, pid);
511 if (len > sizeof(uc.pid))
512 PRINT_FIELD_UID(", ", uc, uid);
513 if (len == sizeof(uc))
514 PRINT_FIELD_UID(", ", uc, gid);
517 #endif /* SO_PEERCRED */
519 #ifdef PACKET_STATISTICS
521 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
524 struct tpacket_stats stats;
526 if (len != sizeof(stats) ||
527 umove(tcp, addr, &stats) < 0) {
530 PRINT_FIELD_U("{", stats, tp_packets);
531 PRINT_FIELD_U("{", stats, tp_drops);
535 #endif /* PACKET_STATISTICS */
537 #include "xlat/icmpfilterflags.h"
540 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
542 struct icmp_filter filter = {};
544 if (len > (int) sizeof(filter))
545 len = sizeof(filter);
551 if (umoven_or_printaddr(tcp, addr, len, &filter))
555 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
560 print_uint32(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
562 tprintf("%u", *(uint32_t *) elem_buf);
568 print_getsockopt(struct tcb *const tcp, const unsigned int level,
569 const unsigned int name, const kernel_ulong_t addr,
570 const int ulen, const int rlen)
572 if (addr && verbose(tcp))
577 print_get_linger(tcp, addr, rlen);
581 print_ucred(tcp, addr, rlen);
584 #ifdef SO_ATTACH_FILTER
585 case SO_ATTACH_FILTER:
586 if (rlen && (unsigned short) rlen == (unsigned int) rlen)
587 print_sock_fprog(tcp, addr, rlen);
591 #endif /* SO_ATTACH_FILTER */
597 #ifdef PACKET_STATISTICS
598 case PACKET_STATISTICS:
599 print_tpacket_stats(tcp, addr, rlen);
608 print_icmp_filter(tcp, addr, rlen);
614 if (ulen < 0 || rlen < 0) {
616 * As the kernel neither accepts nor returns a negative
617 * length, in case of successful getsockopt syscall
618 * invocation these negative values must have come
625 case NETLINK_LIST_MEMBERSHIPS: {
627 print_array(tcp, addr, MIN(ulen, rlen) / sizeof(buf),
629 umoven_or_printaddr, print_uint32, 0);
633 printnum_int(tcp, addr, "%d");
639 /* default arg printing */
642 if (rlen == sizeof(int)) {
643 printnum_int(tcp, addr, "%d");
645 printstrn(tcp, addr, rlen);
657 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
658 tcp->u_arg[1], tcp->u_arg[2], true);
660 if (verbose(tcp) && tcp->u_arg[4]
661 && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
662 set_tcb_priv_ulong(tcp, ulen);
665 printaddr(tcp->u_arg[3]);
667 printaddr(tcp->u_arg[4]);
671 ulen = get_tcb_priv_ulong(tcp);
673 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
674 printaddr(tcp->u_arg[3]);
675 tprintf(", [%d]", ulen);
677 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
678 tcp->u_arg[3], ulen, rlen);
680 tprintf(", [%d->%d]", ulen, rlen);
682 tprintf(", [%d]", rlen);
688 #ifdef IP_ADD_MEMBERSHIP
690 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
695 if (len < (int) sizeof(mreq)) {
697 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
698 PRINT_FIELD_INET4_ADDR("{", mreq, imr_multiaddr);
699 PRINT_FIELD_INET4_ADDR(", ", mreq, imr_interface);
703 #endif /* IP_ADD_MEMBERSHIP */
705 #ifdef IPV6_ADD_MEMBERSHIP
707 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
710 struct ipv6_mreq mreq;
712 if (len < (int) sizeof(mreq)) {
714 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
715 PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
716 PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
720 #endif /* IPV6_ADD_MEMBERSHIP */
722 #ifdef PACKET_RX_RING
724 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
726 struct tpacket_req req;
728 if (len != sizeof(req) ||
729 umove(tcp, addr, &req) < 0) {
732 PRINT_FIELD_U("{", req, tp_block_size);
733 PRINT_FIELD_U(", ", req, tp_block_nr);
734 PRINT_FIELD_U(", ", req, tp_frame_size);
735 PRINT_FIELD_U(", ", req, tp_frame_nr);
739 #endif /* PACKET_RX_RING */
741 #ifdef PACKET_ADD_MEMBERSHIP
742 # include "xlat/packet_mreq_type.h"
745 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
747 struct packet_mreq mreq;
749 if (len != sizeof(mreq) ||
750 umove(tcp, addr, &mreq) < 0) {
755 PRINT_FIELD_IFINDEX("{", mreq, mr_ifindex);
756 PRINT_FIELD_XVAL(", ", mreq, mr_type, packet_mreq_type,
758 PRINT_FIELD_U(", ", mreq, mr_alen);
759 tprints(", mr_address=");
760 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
761 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
762 for (i = 0; i < mreq.mr_alen; ++i)
763 tprintf("%02x", mreq.mr_address[i]);
767 #endif /* PACKET_ADD_MEMBERSHIP */
770 print_setsockopt(struct tcb *const tcp, const unsigned int level,
771 const unsigned int name, const kernel_ulong_t addr,
774 if (addr && verbose(tcp))
779 print_set_linger(tcp, addr, len);
781 #ifdef SO_ATTACH_FILTER
782 case SO_ATTACH_FILTER:
783 # ifdef SO_ATTACH_REUSEPORT_CBPF
784 case SO_ATTACH_REUSEPORT_CBPF:
786 if ((unsigned int) len == get_sock_fprog_size())
787 decode_sock_fprog(tcp, addr);
791 #endif /* SO_ATTACH_FILTER */
797 #ifdef IP_ADD_MEMBERSHIP
798 case IP_ADD_MEMBERSHIP:
799 case IP_DROP_MEMBERSHIP:
800 print_mreq(tcp, addr, len);
802 #endif /* IP_ADD_MEMBERSHIP */
803 #ifdef MCAST_JOIN_GROUP
804 case MCAST_JOIN_GROUP:
805 case MCAST_LEAVE_GROUP:
806 print_group_req(tcp, addr, len);
808 #endif /* MCAST_JOIN_GROUP */
814 #ifdef IPV6_ADD_MEMBERSHIP
815 case IPV6_ADD_MEMBERSHIP:
816 case IPV6_DROP_MEMBERSHIP:
817 # ifdef IPV6_JOIN_ANYCAST
818 case IPV6_JOIN_ANYCAST:
820 # ifdef IPV6_LEAVE_ANYCAST
821 case IPV6_LEAVE_ANYCAST:
823 print_mreq6(tcp, addr, len);
825 #endif /* IPV6_ADD_MEMBERSHIP */
826 #ifdef MCAST_JOIN_GROUP
827 case MCAST_JOIN_GROUP:
828 case MCAST_LEAVE_GROUP:
829 print_group_req(tcp, addr, len);
831 #endif /* MCAST_JOIN_GROUP */
837 #ifdef PACKET_RX_RING
839 # ifdef PACKET_TX_RING
842 print_tpacket_req(tcp, addr, len);
844 #endif /* PACKET_RX_RING */
845 #ifdef PACKET_ADD_MEMBERSHIP
846 case PACKET_ADD_MEMBERSHIP:
847 case PACKET_DROP_MEMBERSHIP:
848 print_packet_mreq(tcp, addr, len);
850 #endif /* PACKET_ADD_MEMBERSHIP */
857 print_icmp_filter(tcp, addr, len);
863 if (len < (int) sizeof(int))
866 printnum_int(tcp, addr, "%d");
870 /* default arg printing */
873 if (len == sizeof(int)) {
874 printnum_int(tcp, addr, "%d");
876 printstrn(tcp, addr, len);
885 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
886 tcp->u_arg[1], tcp->u_arg[2], false);
887 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
888 tcp->u_arg[3], tcp->u_arg[4]);
889 tprintf(", %d", (int) tcp->u_arg[4]);