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, const int len)
540 if (len != sizeof(uc) ||
541 umove(tcp, addr, &uc) < 0) {
544 tprintf("{pid=%u, uid=%u, gid=%u}",
550 #endif /* SO_PEERCRED */
552 #ifdef PACKET_STATISTICS
554 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
557 struct tpacket_stats stats;
559 if (len != sizeof(stats) ||
560 umove(tcp, addr, &stats) < 0) {
563 tprintf("{packets=%u, drops=%u}",
568 #endif /* PACKET_STATISTICS */
570 #include "xlat/icmpfilterflags.h"
573 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
575 struct icmp_filter filter = {};
577 if (len > (int) sizeof(filter))
578 len = sizeof(filter);
584 if (umoven_or_printaddr(tcp, addr, len, &filter))
588 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
593 print_getsockopt(struct tcb *const tcp, const unsigned int level,
594 const unsigned int name, const kernel_ulong_t addr,
597 if (addr && verbose(tcp))
602 print_get_linger(tcp, addr, len);
606 print_ucred(tcp, addr, len);
614 #ifdef PACKET_STATISTICS
615 case PACKET_STATISTICS:
616 print_tpacket_stats(tcp, addr, len);
625 print_icmp_filter(tcp, addr, len);
631 /* default arg printing */
634 if (len == sizeof(int)) {
635 printnum_int(tcp, addr, "%d");
637 printstrn(tcp, addr, len);
649 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
650 tcp->u_arg[1], tcp->u_arg[2], true);
652 if (verbose(tcp) && tcp->u_arg[4]
653 && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
654 set_tcb_priv_ulong(tcp, ulen);
657 printaddr(tcp->u_arg[3]);
659 printaddr(tcp->u_arg[4]);
663 ulen = get_tcb_priv_ulong(tcp);
665 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
666 printaddr(tcp->u_arg[3]);
667 tprintf(", [%d]", ulen);
669 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
670 tcp->u_arg[3], rlen);
672 tprintf(", [%d->%d]", ulen, rlen);
674 tprintf(", [%d]", rlen);
680 #ifdef IP_ADD_MEMBERSHIP
682 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
683 const unsigned int len)
687 if (len < sizeof(mreq)) {
688 printstrn(tcp, addr, len);
691 if (umove_or_printaddr(tcp, addr, &mreq))
694 tprintf("{imr_multiaddr=inet_addr(\"%s\")",
695 inet_ntoa(mreq.imr_multiaddr));
696 tprintf(", imr_interface=inet_addr(\"%s\")}",
697 inet_ntoa(mreq.imr_interface));
699 #endif /* IP_ADD_MEMBERSHIP */
701 #ifdef IPV6_ADD_MEMBERSHIP
703 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
704 const unsigned int len)
706 struct ipv6_mreq mreq;
708 if (len < sizeof(mreq)) {
709 printstrn(tcp, addr, len);
712 if (umove_or_printaddr(tcp, addr, &mreq))
716 print_inet_addr(AF_INET6, &mreq.ipv6mr_multiaddr,
717 sizeof(mreq.ipv6mr_multiaddr), "ipv6mr_multiaddr");
719 tprints(", ipv6mr_interface=");
720 print_ifindex(mreq.ipv6mr_interface);
723 #endif /* IPV6_ADD_MEMBERSHIP */
725 #ifdef MCAST_JOIN_GROUP
727 print_group_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
729 struct group_req greq;
731 if (len != sizeof(greq) ||
732 umove(tcp, addr, &greq) < 0) {
737 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
738 print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
742 #endif /* MCAST_JOIN_GROUP */
744 #ifdef PACKET_RX_RING
746 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
748 struct tpacket_req req;
750 if (len != sizeof(req) ||
751 umove(tcp, addr, &req) < 0) {
754 tprintf("{block_size=%u, block_nr=%u, "
755 "frame_size=%u, frame_nr=%u}",
762 #endif /* PACKET_RX_RING */
764 #ifdef PACKET_ADD_MEMBERSHIP
765 # include "xlat/packet_mreq_type.h"
768 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
770 struct packet_mreq mreq;
772 if (len != sizeof(mreq) ||
773 umove(tcp, addr, &mreq) < 0) {
778 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
779 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
780 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
781 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
782 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
783 for (i = 0; i < mreq.mr_alen; ++i)
784 tprintf("%02x", mreq.mr_address[i]);
788 #endif /* PACKET_ADD_MEMBERSHIP */
791 print_setsockopt(struct tcb *const tcp, const unsigned int level,
792 const unsigned int name, const kernel_ulong_t addr,
795 if (addr && verbose(tcp))
800 print_set_linger(tcp, addr, len);
807 #ifdef IP_ADD_MEMBERSHIP
808 case IP_ADD_MEMBERSHIP:
809 case IP_DROP_MEMBERSHIP:
810 print_mreq(tcp, addr, len);
812 #endif /* IP_ADD_MEMBERSHIP */
813 #ifdef MCAST_JOIN_GROUP
814 case MCAST_JOIN_GROUP:
815 case MCAST_LEAVE_GROUP:
816 print_group_req(tcp, addr, len);
818 #endif /* MCAST_JOIN_GROUP */
824 #ifdef IPV6_ADD_MEMBERSHIP
825 case IPV6_ADD_MEMBERSHIP:
826 case IPV6_DROP_MEMBERSHIP:
827 # ifdef IPV6_JOIN_ANYCAST
828 case IPV6_JOIN_ANYCAST:
830 # ifdef IPV6_LEAVE_ANYCAST
831 case IPV6_LEAVE_ANYCAST:
833 print_mreq6(tcp, addr, len);
835 #endif /* IPV6_ADD_MEMBERSHIP */
841 #ifdef PACKET_RX_RING
843 # ifdef PACKET_TX_RING
846 print_tpacket_req(tcp, addr, len);
848 #endif /* PACKET_RX_RING */
849 #ifdef PACKET_ADD_MEMBERSHIP
850 case PACKET_ADD_MEMBERSHIP:
851 case PACKET_DROP_MEMBERSHIP:
852 print_packet_mreq(tcp, addr, len);
854 #endif /* PACKET_ADD_MEMBERSHIP */
861 print_icmp_filter(tcp, addr, len);
867 /* default arg printing */
870 if (len == sizeof(int)) {
871 printnum_int(tcp, addr, "%d");
873 printstrn(tcp, addr, len);
882 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
883 tcp->u_arg[1], tcp->u_arg[2], false);
884 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
885 tcp->u_arg[3], tcp->u_arg[4]);
886 tprintf(", %d", (int) tcp->u_arg[4]);