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.
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>
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 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
86 # include <bluetooth/bluetooth.h>
87 # include "xlat/bt_protocols.h"
91 print_ifindex(unsigned int ifindex)
93 #ifdef HAVE_IF_INDEXTONAME
94 char buf[IFNAMSIZ + 1];
96 if (if_indextoname(ifindex, buf)) {
97 tprints("if_nametoindex(");
98 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
103 tprintf("%u", ifindex);
107 decode_sockbuf(struct tcb *const tcp, const int fd, const kernel_ulong_t addr,
108 const kernel_ulong_t addrlen)
111 switch (verbose(tcp) ? getfdproto(tcp, fd) : SOCK_PROTO_UNKNOWN) {
112 case SOCK_PROTO_NETLINK:
113 decode_netlink(tcp, fd, addr, addrlen);
116 printstrn(tcp, addr, addrlen);
121 * low bits of the socket type define real socket type,
122 * other bits are socket type flags.
125 tprint_sock_type(unsigned int flags)
127 const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
131 flags &= ~SOCK_TYPE_MASK;
136 printflags(sock_type_flags, flags, "SOCK_???");
141 printxval(addrfams, tcp->u_arg[0], "AF_???");
143 tprint_sock_type(tcp->u_arg[1]);
145 switch (tcp->u_arg[0]) {
148 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
152 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
155 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
157 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
162 tprintf("%" PRI_klu, tcp->u_arg[2]);
166 return RVAL_DECODED | RVAL_FD;
171 printfd(tcp, tcp->u_arg[0]);
173 const int addrlen = tcp->u_arg[2];
174 decode_sockaddr(tcp, tcp->u_arg[1], addrlen);
175 tprintf(", %d", addrlen);
182 printfd(tcp, tcp->u_arg[0]);
184 tprintf("%" PRI_klu, tcp->u_arg[1]);
190 fetch_socklen(struct tcb *const tcp, int *const plen,
191 const kernel_ulong_t sockaddr, const kernel_ulong_t socklen)
193 return verbose(tcp) && sockaddr && socklen
194 && umove(tcp, socklen, plen) == 0;
198 decode_sockname(struct tcb *tcp)
203 printfd(tcp, tcp->u_arg[0]);
205 if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
206 set_tcb_priv_ulong(tcp, ulen);
209 printaddr(tcp->u_arg[1]);
211 printaddr(tcp->u_arg[2]);
216 ulen = get_tcb_priv_ulong(tcp);
218 if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
219 printaddr(tcp->u_arg[1]);
220 tprintf(", [%d]", ulen);
222 decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
224 tprintf(", [%d->%d]", ulen, rlen);
226 tprintf(", [%d]", rlen);
234 return decode_sockname(tcp) | RVAL_FD;
239 int rc = decode_sockname(tcp);
241 if (rc & RVAL_DECODED) {
243 printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
251 printfd(tcp, tcp->u_arg[0]);
253 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
254 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
256 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
263 printfd(tcp, tcp->u_arg[0]);
265 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
266 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
268 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
270 const int addrlen = tcp->u_arg[5];
272 decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
274 tprintf(", %d", addrlen);
282 printfd(tcp, tcp->u_arg[0]);
286 printaddr(tcp->u_arg[1]);
288 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
292 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
293 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
303 printfd(tcp, tcp->u_arg[0]);
305 if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
306 set_tcb_priv_ulong(tcp, ulen);
311 printaddr(tcp->u_arg[1]);
313 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
317 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
319 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
322 ulen = get_tcb_priv_ulong(tcp);
324 if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
326 printaddr(tcp->u_arg[4]);
329 printaddr(tcp->u_arg[5]);
334 printaddr(tcp->u_arg[4]);
336 tprintf(", [%d]", ulen);
340 decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
343 tprintf(", [%d->%d]", ulen, rlen);
345 tprintf(", [%d]", rlen);
350 #include "xlat/shutdown_modes.h"
354 printfd(tcp, tcp->u_arg[0]);
356 printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
361 SYS_FUNC(getsockname)
363 return decode_sockname(tcp);
367 printpair_fd(struct tcb *tcp, const int i0, const int i1)
377 decode_pair_fd(struct tcb *const tcp, const kernel_ulong_t addr)
381 if (umove_or_printaddr(tcp, addr, &pair))
384 printpair_fd(tcp, pair[0], pair[1]);
388 do_pipe(struct tcb *tcp, int flags_arg)
391 decode_pair_fd(tcp, tcp->u_arg[0]);
392 if (flags_arg >= 0) {
394 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
403 if (exiting(tcp) && !syserror(tcp))
404 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
407 return do_pipe(tcp, -1);
413 return do_pipe(tcp, 1);
419 printxval(addrfams, tcp->u_arg[0], "AF_???");
421 tprint_sock_type(tcp->u_arg[1]);
422 tprintf(", %" PRI_klu, tcp->u_arg[2]);
425 decode_pair_fd(tcp, tcp->u_arg[3]);
430 #include "xlat/sockoptions.h"
431 #include "xlat/sockipoptions.h"
432 #include "xlat/getsockipoptions.h"
433 #include "xlat/setsockipoptions.h"
434 #include "xlat/sockipv6options.h"
435 #include "xlat/getsockipv6options.h"
436 #include "xlat/setsockipv6options.h"
437 #include "xlat/sockipxoptions.h"
438 #include "xlat/sockrawoptions.h"
439 #include "xlat/sockpacketoptions.h"
440 #include "xlat/socksctpoptions.h"
441 #include "xlat/socktcpoptions.h"
444 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
445 unsigned int name, bool is_getsockopt)
449 printxval(socketlayers, level, "SOL_??");
454 printxval(sockoptions, name, "SO_???");
457 printxvals(name, "IP_???", sockipoptions,
458 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
461 printxvals(name, "IPV6_???", sockipv6options,
462 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
465 printxval(sockipxoptions, name, "IPX_???");
468 printxval(sockpacketoptions, name, "PACKET_???");
471 printxval(socktcpoptions, name, "TCP_???");
474 printxval(socksctpoptions, name, "SCTP_???");
477 printxval(sockrawoptions, name, "RAW_???");
480 /* Other SOL_* protocol levels still need work. */
490 print_linger(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
492 struct linger linger;
494 if (len != sizeof(linger) ||
495 umove(tcp, addr, &linger) < 0) {
500 tprintf("{onoff=%d, linger=%d}",
507 print_ucred(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
511 if (len != sizeof(uc) ||
512 umove(tcp, addr, &uc) < 0) {
515 tprintf("{pid=%u, uid=%u, gid=%u}",
521 #endif /* SO_PEERCRED */
523 #ifdef PACKET_STATISTICS
525 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
528 struct tpacket_stats stats;
530 if (len != sizeof(stats) ||
531 umove(tcp, addr, &stats) < 0) {
534 tprintf("{packets=%u, drops=%u}",
539 #endif /* PACKET_STATISTICS */
541 #include "xlat/icmpfilterflags.h"
544 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
546 struct icmp_filter filter = {};
548 if (len > (int) sizeof(filter))
549 len = sizeof(filter);
555 if (umoven_or_printaddr(tcp, addr, len, &filter))
559 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
564 print_getsockopt(struct tcb *const tcp, const unsigned int level,
565 const unsigned int name, const kernel_ulong_t addr,
568 if (addr && verbose(tcp))
573 print_linger(tcp, addr, len);
577 print_ucred(tcp, addr, len);
585 #ifdef PACKET_STATISTICS
586 case PACKET_STATISTICS:
587 print_tpacket_stats(tcp, addr, len);
596 print_icmp_filter(tcp, addr, len);
602 /* default arg printing */
605 if (len == sizeof(int)) {
606 printnum_int(tcp, addr, "%d");
608 printstrn(tcp, addr, len);
618 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
619 tcp->u_arg[1], tcp->u_arg[2], true);
623 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
624 printaddr(tcp->u_arg[3]);
626 printaddr(tcp->u_arg[4]);
628 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
630 tprintf(", [%d]", len);
636 #ifdef IP_ADD_MEMBERSHIP
638 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
639 const unsigned int len)
643 if (len < sizeof(mreq)) {
644 printstrn(tcp, addr, len);
647 if (umove_or_printaddr(tcp, addr, &mreq))
650 tprintf("{imr_multiaddr=inet_addr(\"%s\")",
651 inet_ntoa(mreq.imr_multiaddr));
652 tprintf(", imr_interface=inet_addr(\"%s\")}",
653 inet_ntoa(mreq.imr_interface));
655 #endif /* IP_ADD_MEMBERSHIP */
657 #ifdef IPV6_ADD_MEMBERSHIP
659 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
660 const unsigned int len)
662 struct ipv6_mreq mreq;
664 if (len < sizeof(mreq)) {
665 printstrn(tcp, addr, len);
668 if (umove_or_printaddr(tcp, addr, &mreq))
672 print_inet_addr(AF_INET6, &mreq.ipv6mr_multiaddr,
673 sizeof(mreq.ipv6mr_multiaddr), "ipv6mr_multiaddr");
675 tprints(", ipv6mr_interface=");
676 print_ifindex(mreq.ipv6mr_interface);
679 #endif /* IPV6_ADD_MEMBERSHIP */
681 #ifdef MCAST_JOIN_GROUP
683 print_group_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
685 struct group_req greq;
687 if (len != sizeof(greq) ||
688 umove(tcp, addr, &greq) < 0) {
693 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
694 print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
698 #endif /* MCAST_JOIN_GROUP */
700 #ifdef PACKET_RX_RING
702 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
704 struct tpacket_req req;
706 if (len != sizeof(req) ||
707 umove(tcp, addr, &req) < 0) {
710 tprintf("{block_size=%u, block_nr=%u, "
711 "frame_size=%u, frame_nr=%u}",
718 #endif /* PACKET_RX_RING */
720 #ifdef PACKET_ADD_MEMBERSHIP
721 # include "xlat/packet_mreq_type.h"
724 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
726 struct packet_mreq mreq;
728 if (len != sizeof(mreq) ||
729 umove(tcp, addr, &mreq) < 0) {
734 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
735 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
736 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
737 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
738 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
739 for (i = 0; i < mreq.mr_alen; ++i)
740 tprintf("%02x", mreq.mr_address[i]);
744 #endif /* PACKET_ADD_MEMBERSHIP */
747 print_setsockopt(struct tcb *const tcp, const unsigned int level,
748 const unsigned int name, const kernel_ulong_t addr,
751 if (addr && verbose(tcp))
756 print_linger(tcp, addr, len);
763 #ifdef IP_ADD_MEMBERSHIP
764 case IP_ADD_MEMBERSHIP:
765 case IP_DROP_MEMBERSHIP:
766 print_mreq(tcp, addr, len);
768 #endif /* IP_ADD_MEMBERSHIP */
769 #ifdef MCAST_JOIN_GROUP
770 case MCAST_JOIN_GROUP:
771 case MCAST_LEAVE_GROUP:
772 print_group_req(tcp, addr, len);
774 #endif /* MCAST_JOIN_GROUP */
780 #ifdef IPV6_ADD_MEMBERSHIP
781 case IPV6_ADD_MEMBERSHIP:
782 case IPV6_DROP_MEMBERSHIP:
783 # ifdef IPV6_JOIN_ANYCAST
784 case IPV6_JOIN_ANYCAST:
786 # ifdef IPV6_LEAVE_ANYCAST
787 case IPV6_LEAVE_ANYCAST:
789 print_mreq6(tcp, addr, len);
791 #endif /* IPV6_ADD_MEMBERSHIP */
797 #ifdef PACKET_RX_RING
799 # ifdef PACKET_TX_RING
802 print_tpacket_req(tcp, addr, len);
804 #endif /* PACKET_RX_RING */
805 #ifdef PACKET_ADD_MEMBERSHIP
806 case PACKET_ADD_MEMBERSHIP:
807 case PACKET_DROP_MEMBERSHIP:
808 print_packet_mreq(tcp, addr, len);
810 #endif /* PACKET_ADD_MEMBERSHIP */
817 print_icmp_filter(tcp, addr, len);
823 /* default arg printing */
826 if (len == sizeof(int)) {
827 printnum_int(tcp, addr, "%d");
829 printstrn(tcp, addr, len);
838 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
839 tcp->u_arg[1], tcp->u_arg[2], false);
840 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
841 tcp->u_arg[3], tcp->u_arg[4]);
842 tprintf(", %d", (int) tcp->u_arg[4]);