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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #ifdef HAVE_NETINET_TCP_H
38 # include <netinet/tcp.h>
40 #ifdef HAVE_NETINET_UDP_H
41 # include <netinet/udp.h>
43 #ifdef HAVE_NETINET_SCTP_H
44 # include <netinet/sctp.h>
46 #include <arpa/inet.h>
48 #include <asm/types.h>
49 #ifdef HAVE_NETIPX_IPX_H
50 # include <netipx/ipx.h>
52 # include <linux/ipx.h>
55 #if defined(HAVE_LINUX_IP_VS_H)
56 # include <linux/ip_vs.h>
58 #include <linux/netlink.h>
59 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
60 # include <linux/netfilter_arp/arp_tables.h>
62 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
63 # include <linux/netfilter_bridge/ebtables.h>
65 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
66 # include <linux/netfilter_ipv4/ip_tables.h>
68 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
69 # include <linux/netfilter_ipv6/ip6_tables.h>
71 #include <linux/if_packet.h>
72 #include <linux/icmp.h>
74 #include "xlat/socktypes.h"
75 #include "xlat/sock_type_flags.h"
76 #ifndef SOCK_TYPE_MASK
77 # define SOCK_TYPE_MASK 0xf
80 #include "xlat/socketlayers.h"
82 #include "xlat/inet_protocols.h"
83 #include "xlat/netlink_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, 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);
614 tprintf(", [%d]", len);
620 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
621 tcp->u_arg[1], tcp->u_arg[2], true);
625 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
626 printaddr(tcp->u_arg[3]);
628 printaddr(tcp->u_arg[4]);
630 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
637 #ifdef IP_ADD_MEMBERSHIP
639 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
640 const unsigned int len)
644 if (len < sizeof(mreq)) {
645 printstrn(tcp, addr, len);
648 if (umove_or_printaddr(tcp, addr, &mreq))
651 tprints("{imr_multiaddr=inet_addr(");
652 print_quoted_string(inet_ntoa(mreq.imr_multiaddr),
653 16, QUOTE_0_TERMINATED);
654 tprints("), imr_interface=inet_addr(");
655 print_quoted_string(inet_ntoa(mreq.imr_interface),
656 16, QUOTE_0_TERMINATED);
659 #endif /* IP_ADD_MEMBERSHIP */
661 #ifdef IPV6_ADD_MEMBERSHIP
663 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
664 const unsigned int len)
666 struct ipv6_mreq mreq;
668 if (len < sizeof(mreq))
671 if (umove_or_printaddr(tcp, addr, &mreq))
674 const struct in6_addr *in6 = &mreq.ipv6mr_multiaddr;
675 char address[INET6_ADDRSTRLEN];
677 if (!inet_ntop(AF_INET6, in6, address, sizeof(address)))
680 tprints("{ipv6mr_multiaddr=inet_pton(");
681 print_quoted_string(address, sizeof(address), QUOTE_0_TERMINATED);
682 tprints("), ipv6mr_interface=");
683 print_ifindex(mreq.ipv6mr_interface);
688 printstrn(tcp, addr, len);
690 #endif /* IPV6_ADD_MEMBERSHIP */
692 #ifdef MCAST_JOIN_GROUP
694 print_group_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
696 struct group_req greq;
698 if (len != sizeof(greq) ||
699 umove(tcp, addr, &greq) < 0) {
704 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
705 print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
709 #endif /* MCAST_JOIN_GROUP */
711 #ifdef PACKET_RX_RING
713 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
715 struct tpacket_req req;
717 if (len != sizeof(req) ||
718 umove(tcp, addr, &req) < 0) {
721 tprintf("{block_size=%u, block_nr=%u, "
722 "frame_size=%u, frame_nr=%u}",
729 #endif /* PACKET_RX_RING */
731 #ifdef PACKET_ADD_MEMBERSHIP
732 # include "xlat/packet_mreq_type.h"
735 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
737 struct packet_mreq mreq;
739 if (len != sizeof(mreq) ||
740 umove(tcp, addr, &mreq) < 0) {
745 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
746 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
747 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
748 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
749 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
750 for (i = 0; i < mreq.mr_alen; ++i)
751 tprintf("%02x", mreq.mr_address[i]);
755 #endif /* PACKET_ADD_MEMBERSHIP */
758 print_setsockopt(struct tcb *const tcp, const unsigned int level,
759 const unsigned int name, const kernel_ulong_t addr,
762 if (addr && verbose(tcp))
767 print_linger(tcp, addr, len);
774 #ifdef IP_ADD_MEMBERSHIP
775 case IP_ADD_MEMBERSHIP:
776 case IP_DROP_MEMBERSHIP:
777 print_mreq(tcp, addr, len);
779 #endif /* IP_ADD_MEMBERSHIP */
780 #ifdef MCAST_JOIN_GROUP
781 case MCAST_JOIN_GROUP:
782 case MCAST_LEAVE_GROUP:
783 print_group_req(tcp, addr, len);
785 #endif /* MCAST_JOIN_GROUP */
791 #ifdef IPV6_ADD_MEMBERSHIP
792 case IPV6_ADD_MEMBERSHIP:
793 case IPV6_DROP_MEMBERSHIP:
794 # ifdef IPV6_JOIN_ANYCAST
795 case IPV6_JOIN_ANYCAST:
797 # ifdef IPV6_LEAVE_ANYCAST
798 case IPV6_LEAVE_ANYCAST:
800 print_mreq6(tcp, addr, len);
802 #endif /* IPV6_ADD_MEMBERSHIP */
808 #ifdef PACKET_RX_RING
810 # ifdef PACKET_TX_RING
813 print_tpacket_req(tcp, addr, len);
815 #endif /* PACKET_RX_RING */
816 #ifdef PACKET_ADD_MEMBERSHIP
817 case PACKET_ADD_MEMBERSHIP:
818 case PACKET_DROP_MEMBERSHIP:
819 print_packet_mreq(tcp, addr, len);
821 #endif /* PACKET_ADD_MEMBERSHIP */
828 print_icmp_filter(tcp, addr, len);
834 /* default arg printing */
837 if (len == sizeof(int)) {
838 printnum_int(tcp, addr, "%d");
840 printstrn(tcp, addr, len);
846 tprintf(", %d", len);
851 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
852 tcp->u_arg[1], tcp->u_arg[2], false);
853 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
854 tcp->u_arg[3], tcp->u_arg[4]);