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,
448 struct linger linger;
450 if (len < (int) sizeof(linger)) {
452 } else if (!umove_or_printaddr(tcp, addr, &linger)) {
453 PRINT_FIELD_D("{", linger, l_onoff);
454 PRINT_FIELD_D(", ", linger, l_linger);
460 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
463 struct linger linger;
465 if (len < sizeof(linger)) {
466 if (len != sizeof(linger.l_onoff)) {
467 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
471 len = sizeof(linger);
474 if (umoven(tcp, addr, len, &linger) < 0) {
479 PRINT_FIELD_D("{", linger, l_onoff);
480 if (len == sizeof(linger))
481 PRINT_FIELD_D(", ", linger, l_linger);
487 print_ucred(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int len)
491 if (len < sizeof(uc)) {
492 if (len != sizeof(uc.pid)
493 && len != offsetofend(struct ucred, uid)) {
494 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
501 if (umoven(tcp, addr, len, &uc) < 0) {
506 PRINT_FIELD_D("{", uc, pid);
507 if (len > sizeof(uc.pid))
508 PRINT_FIELD_UID(", ", uc, uid);
509 if (len == sizeof(uc))
510 PRINT_FIELD_UID(", ", uc, gid);
513 #endif /* SO_PEERCRED */
515 #ifdef PACKET_STATISTICS
517 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
520 struct tpacket_stats stats;
522 if (len != sizeof(stats) ||
523 umove(tcp, addr, &stats) < 0) {
526 PRINT_FIELD_U("{", stats, tp_packets);
527 PRINT_FIELD_U("{", stats, tp_drops);
531 #endif /* PACKET_STATISTICS */
533 #include "xlat/icmpfilterflags.h"
536 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
538 struct icmp_filter filter = {};
540 if (len > (int) sizeof(filter))
541 len = sizeof(filter);
547 if (umoven_or_printaddr(tcp, addr, len, &filter))
551 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
556 print_getsockopt(struct tcb *const tcp, const unsigned int level,
557 const unsigned int name, const kernel_ulong_t addr,
560 if (addr && verbose(tcp))
565 print_get_linger(tcp, addr, len);
569 print_ucred(tcp, addr, len);
572 #ifdef SO_ATTACH_FILTER
573 case SO_ATTACH_FILTER:
574 if (len && (unsigned short) len == (unsigned int) len)
575 print_sock_fprog(tcp, addr, len);
579 #endif /* SO_ATTACH_FILTER */
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);
620 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
621 tcp->u_arg[1], tcp->u_arg[2], true);
623 if (verbose(tcp) && tcp->u_arg[4]
624 && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
625 set_tcb_priv_ulong(tcp, ulen);
628 printaddr(tcp->u_arg[3]);
630 printaddr(tcp->u_arg[4]);
634 ulen = get_tcb_priv_ulong(tcp);
636 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
637 printaddr(tcp->u_arg[3]);
638 tprintf(", [%d]", ulen);
640 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
641 tcp->u_arg[3], rlen);
643 tprintf(", [%d->%d]", ulen, rlen);
645 tprintf(", [%d]", rlen);
651 #ifdef IP_ADD_MEMBERSHIP
653 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
658 if (len < (int) sizeof(mreq)) {
660 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
661 PRINT_FIELD_INET4_ADDR("{", mreq, imr_multiaddr);
662 PRINT_FIELD_INET4_ADDR(", ", mreq, imr_interface);
666 #endif /* IP_ADD_MEMBERSHIP */
668 #ifdef IPV6_ADD_MEMBERSHIP
670 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
673 struct ipv6_mreq mreq;
675 if (len < (int) sizeof(mreq)) {
677 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
678 PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
679 PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
683 #endif /* IPV6_ADD_MEMBERSHIP */
685 #ifdef PACKET_RX_RING
687 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
689 struct tpacket_req req;
691 if (len != sizeof(req) ||
692 umove(tcp, addr, &req) < 0) {
695 PRINT_FIELD_U("{", req, tp_block_size);
696 PRINT_FIELD_U(", ", req, tp_block_nr);
697 PRINT_FIELD_U(", ", req, tp_frame_size);
698 PRINT_FIELD_U(", ", req, tp_frame_nr);
702 #endif /* PACKET_RX_RING */
704 #ifdef PACKET_ADD_MEMBERSHIP
705 # include "xlat/packet_mreq_type.h"
708 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
710 struct packet_mreq mreq;
712 if (len != sizeof(mreq) ||
713 umove(tcp, addr, &mreq) < 0) {
718 PRINT_FIELD_IFINDEX("{", mreq, mr_ifindex);
719 PRINT_FIELD_XVAL(", ", mreq, mr_type, packet_mreq_type,
721 PRINT_FIELD_U(", ", mreq, mr_alen);
722 tprints(", mr_address=");
723 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
724 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
725 for (i = 0; i < mreq.mr_alen; ++i)
726 tprintf("%02x", mreq.mr_address[i]);
730 #endif /* PACKET_ADD_MEMBERSHIP */
733 print_setsockopt(struct tcb *const tcp, const unsigned int level,
734 const unsigned int name, const kernel_ulong_t addr,
737 if (addr && verbose(tcp))
742 print_set_linger(tcp, addr, len);
744 #ifdef SO_ATTACH_FILTER
745 case SO_ATTACH_FILTER:
746 # ifdef SO_ATTACH_REUSEPORT_CBPF
747 case SO_ATTACH_REUSEPORT_CBPF:
749 if ((unsigned int) len == get_sock_fprog_size())
750 decode_sock_fprog(tcp, addr);
754 #endif /* SO_ATTACH_FILTER */
760 #ifdef IP_ADD_MEMBERSHIP
761 case IP_ADD_MEMBERSHIP:
762 case IP_DROP_MEMBERSHIP:
763 print_mreq(tcp, addr, len);
765 #endif /* IP_ADD_MEMBERSHIP */
766 #ifdef MCAST_JOIN_GROUP
767 case MCAST_JOIN_GROUP:
768 case MCAST_LEAVE_GROUP:
769 print_group_req(tcp, addr, len);
771 #endif /* MCAST_JOIN_GROUP */
777 #ifdef IPV6_ADD_MEMBERSHIP
778 case IPV6_ADD_MEMBERSHIP:
779 case IPV6_DROP_MEMBERSHIP:
780 # ifdef IPV6_JOIN_ANYCAST
781 case IPV6_JOIN_ANYCAST:
783 # ifdef IPV6_LEAVE_ANYCAST
784 case IPV6_LEAVE_ANYCAST:
786 print_mreq6(tcp, addr, len);
788 #endif /* IPV6_ADD_MEMBERSHIP */
789 #ifdef MCAST_JOIN_GROUP
790 case MCAST_JOIN_GROUP:
791 case MCAST_LEAVE_GROUP:
792 print_group_req(tcp, addr, len);
794 #endif /* MCAST_JOIN_GROUP */
800 #ifdef PACKET_RX_RING
802 # ifdef PACKET_TX_RING
805 print_tpacket_req(tcp, addr, len);
807 #endif /* PACKET_RX_RING */
808 #ifdef PACKET_ADD_MEMBERSHIP
809 case PACKET_ADD_MEMBERSHIP:
810 case PACKET_DROP_MEMBERSHIP:
811 print_packet_mreq(tcp, addr, len);
813 #endif /* PACKET_ADD_MEMBERSHIP */
820 print_icmp_filter(tcp, addr, len);
826 /* default arg printing */
829 if (len == sizeof(int)) {
830 printnum_int(tcp, addr, "%d");
832 printstrn(tcp, addr, len);
841 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
842 tcp->u_arg[1], tcp->u_arg[2], false);
843 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
844 tcp->u_arg[3], tcp->u_arg[4]);
845 tprintf(", %d", (int) tcp->u_arg[4]);