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/sockrawoptions.h"
394 #include "xlat/sockpacketoptions.h"
395 #include "xlat/socksctpoptions.h"
396 #include "xlat/socktcpoptions.h"
399 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
400 unsigned int name, bool is_getsockopt)
404 printxval(socketlayers, level, "SOL_??");
409 printxval(sockoptions, name, "SO_???");
412 printxvals(name, "IP_???", sockipoptions,
413 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
416 printxvals(name, "IPV6_???", sockipv6options,
417 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
420 printxval(sockipxoptions, name, "IPX_???");
423 printxval(sockpacketoptions, name, "PACKET_???");
426 printxval(socktcpoptions, name, "TCP_???");
429 printxval(socksctpoptions, name, "SCTP_???");
432 printxval(sockrawoptions, name, "RAW_???");
435 /* Other SOL_* protocol levels still need work. */
445 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
446 const unsigned int len)
448 struct linger linger;
450 if (len < sizeof(linger) ||
451 umove(tcp, addr, &linger) < 0) {
456 PRINT_FIELD_D("{", linger, l_onoff);
457 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 tprintf("{packets=%u, drops=%u}",
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_getsockopt(struct tcb *const tcp, const unsigned int level,
559 const unsigned int name, const kernel_ulong_t addr,
562 if (addr && verbose(tcp))
567 print_get_linger(tcp, addr, len);
571 print_ucred(tcp, addr, len);
574 #ifdef SO_ATTACH_FILTER
575 case SO_ATTACH_FILTER:
576 if (len && (unsigned short) len == (unsigned int) len)
577 print_sock_fprog(tcp, addr, len);
581 #endif /* SO_ATTACH_FILTER */
587 #ifdef PACKET_STATISTICS
588 case PACKET_STATISTICS:
589 print_tpacket_stats(tcp, addr, len);
598 print_icmp_filter(tcp, addr, len);
604 /* default arg printing */
607 if (len == sizeof(int)) {
608 printnum_int(tcp, addr, "%d");
610 printstrn(tcp, addr, len);
622 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
623 tcp->u_arg[1], tcp->u_arg[2], true);
625 if (verbose(tcp) && tcp->u_arg[4]
626 && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
627 set_tcb_priv_ulong(tcp, ulen);
630 printaddr(tcp->u_arg[3]);
632 printaddr(tcp->u_arg[4]);
636 ulen = get_tcb_priv_ulong(tcp);
638 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
639 printaddr(tcp->u_arg[3]);
640 tprintf(", [%d]", ulen);
642 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
643 tcp->u_arg[3], rlen);
645 tprintf(", [%d->%d]", ulen, rlen);
647 tprintf(", [%d]", rlen);
653 #ifdef IP_ADD_MEMBERSHIP
655 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
656 const unsigned int len)
660 if (len < sizeof(mreq)) {
661 printstrn(tcp, addr, len);
664 if (umove_or_printaddr(tcp, addr, &mreq))
667 tprintf("{imr_multiaddr=inet_addr(\"%s\")",
668 inet_ntoa(mreq.imr_multiaddr));
669 tprintf(", imr_interface=inet_addr(\"%s\")}",
670 inet_ntoa(mreq.imr_interface));
672 #endif /* IP_ADD_MEMBERSHIP */
674 #ifdef IPV6_ADD_MEMBERSHIP
676 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
677 const unsigned int len)
679 struct ipv6_mreq mreq;
681 if (len < sizeof(mreq)) {
682 printstrn(tcp, addr, len);
685 if (umove_or_printaddr(tcp, addr, &mreq))
688 PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
689 PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
692 #endif /* IPV6_ADD_MEMBERSHIP */
694 #ifdef MCAST_JOIN_GROUP
696 print_group_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
698 struct group_req greq;
700 if (len != sizeof(greq) ||
701 umove(tcp, addr, &greq) < 0) {
706 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
707 print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
711 #endif /* MCAST_JOIN_GROUP */
713 #ifdef PACKET_RX_RING
715 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
717 struct tpacket_req req;
719 if (len != sizeof(req) ||
720 umove(tcp, addr, &req) < 0) {
723 tprintf("{block_size=%u, block_nr=%u, "
724 "frame_size=%u, frame_nr=%u}",
731 #endif /* PACKET_RX_RING */
733 #ifdef PACKET_ADD_MEMBERSHIP
734 # include "xlat/packet_mreq_type.h"
737 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
739 struct packet_mreq mreq;
741 if (len != sizeof(mreq) ||
742 umove(tcp, addr, &mreq) < 0) {
747 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
748 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
749 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
750 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
751 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
752 for (i = 0; i < mreq.mr_alen; ++i)
753 tprintf("%02x", mreq.mr_address[i]);
757 #endif /* PACKET_ADD_MEMBERSHIP */
760 print_setsockopt(struct tcb *const tcp, const unsigned int level,
761 const unsigned int name, const kernel_ulong_t addr,
764 if (addr && verbose(tcp))
769 print_set_linger(tcp, addr, len);
771 #ifdef SO_ATTACH_FILTER
772 case SO_ATTACH_FILTER:
773 # ifdef SO_ATTACH_REUSEPORT_CBPF
774 case SO_ATTACH_REUSEPORT_CBPF:
776 if ((unsigned int) len == get_sock_fprog_size())
777 decode_sock_fprog(tcp, addr);
781 #endif /* SO_ATTACH_FILTER */
787 #ifdef IP_ADD_MEMBERSHIP
788 case IP_ADD_MEMBERSHIP:
789 case IP_DROP_MEMBERSHIP:
790 print_mreq(tcp, addr, len);
792 #endif /* IP_ADD_MEMBERSHIP */
793 #ifdef MCAST_JOIN_GROUP
794 case MCAST_JOIN_GROUP:
795 case MCAST_LEAVE_GROUP:
796 print_group_req(tcp, addr, len);
798 #endif /* MCAST_JOIN_GROUP */
804 #ifdef IPV6_ADD_MEMBERSHIP
805 case IPV6_ADD_MEMBERSHIP:
806 case IPV6_DROP_MEMBERSHIP:
807 # ifdef IPV6_JOIN_ANYCAST
808 case IPV6_JOIN_ANYCAST:
810 # ifdef IPV6_LEAVE_ANYCAST
811 case IPV6_LEAVE_ANYCAST:
813 print_mreq6(tcp, addr, len);
815 #endif /* IPV6_ADD_MEMBERSHIP */
821 #ifdef PACKET_RX_RING
823 # ifdef PACKET_TX_RING
826 print_tpacket_req(tcp, addr, len);
828 #endif /* PACKET_RX_RING */
829 #ifdef PACKET_ADD_MEMBERSHIP
830 case PACKET_ADD_MEMBERSHIP:
831 case PACKET_DROP_MEMBERSHIP:
832 print_packet_mreq(tcp, addr, len);
834 #endif /* PACKET_ADD_MEMBERSHIP */
841 print_icmp_filter(tcp, addr, len);
847 /* default arg printing */
850 if (len == sizeof(int)) {
851 printnum_int(tcp, addr, "%d");
853 printstrn(tcp, addr, len);
862 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
863 tcp->u_arg[1], tcp->u_arg[2], false);
864 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
865 tcp->u_arg[3], tcp->u_arg[4]);
866 tprintf(", %d", (int) tcp->u_arg[4]);