]> granicus.if.org Git - strace/blob - net.c
58296b9c4e3d49681d68d6854d77cb85a3cc7483
[strace] / net.c
1 /*
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-2018 The strace developers.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
19  *
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.
30  */
31
32 #include "defs.h"
33 #include "print_fields.h"
34
35 #include <sys/stat.h>
36 #include <sys/socket.h>
37 #include <sys/uio.h>
38 #include <sys/un.h>
39 #include <netinet/in.h>
40 #ifdef HAVE_NETINET_TCP_H
41 # include <netinet/tcp.h>
42 #endif
43 #ifdef HAVE_NETINET_UDP_H
44 # include <netinet/udp.h>
45 #endif
46 #ifdef HAVE_NETINET_SCTP_H
47 # include <netinet/sctp.h>
48 #endif
49 #include <arpa/inet.h>
50 #include <net/if.h>
51 #include <asm/types.h>
52 #ifdef HAVE_NETIPX_IPX_H
53 # include <netipx/ipx.h>
54 #else
55 # include <linux/ipx.h>
56 #endif
57
58 #if defined(HAVE_LINUX_IP_VS_H)
59 # include <linux/ip_vs.h>
60 #endif
61 #include "netlink.h"
62 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
63 # include <linux/netfilter_arp/arp_tables.h>
64 #endif
65 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
66 # include <linux/netfilter_bridge/ebtables.h>
67 #endif
68 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
69 # include <linux/netfilter_ipv4/ip_tables.h>
70 #endif
71 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
72 # include <linux/netfilter_ipv6/ip6_tables.h>
73 #endif
74 #include <linux/if_packet.h>
75 #include <linux/icmp.h>
76
77 #include "xlat/socktypes.h"
78 #include "xlat/sock_type_flags.h"
79 #ifndef SOCK_TYPE_MASK
80 # define SOCK_TYPE_MASK 0xf
81 #endif
82
83 #include "xlat/socketlayers.h"
84
85 #include "xlat/inet_protocols.h"
86
87 #define XLAT_MACROS_ONLY
88 # include "xlat/addrfams.h"
89 # include "xlat/ethernet_protocols.h"
90 #undef XLAT_MACROS_ONLY
91 #include "xlat/irda_protocols.h"
92 #include "xlat/can_protocols.h"
93 #include "xlat/bt_protocols.h"
94 #include "xlat/isdn_protocols.h"
95 #include "xlat/phonet_protocols.h"
96 #include "xlat/caif_protocols.h"
97 #include "xlat/nfc_protocols.h"
98 #include "xlat/kcm_protocols.h"
99 #include "xlat/smc_protocols.h"
100
101 const size_t inet_protocols_size = ARRAY_SIZE(inet_protocols) - 1;
102
103 static void
104 decode_sockbuf(struct tcb *const tcp, const int fd, const kernel_ulong_t addr,
105                const kernel_ulong_t addrlen)
106 {
107
108         switch (verbose(tcp) ? getfdproto(tcp, fd) : SOCK_PROTO_UNKNOWN) {
109         case SOCK_PROTO_NETLINK:
110                 decode_netlink(tcp, fd, addr, addrlen);
111                 break;
112         default:
113                 printstrn(tcp, addr, addrlen);
114         }
115 }
116
117 /*
118  * low bits of the socket type define real socket type,
119  * other bits are socket type flags.
120  */
121 static void
122 tprint_sock_type(unsigned int flags)
123 {
124         const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
125
126         if (str) {
127                 print_xlat_ex(flags & SOCK_TYPE_MASK, str, XLAT_STYLE_DEFAULT);
128                 flags &= ~SOCK_TYPE_MASK;
129                 if (!flags)
130                         return;
131                 tprints("|");
132         }
133         printflags(sock_type_flags, flags, "SOCK_???");
134 }
135
136 SYS_FUNC(socket)
137 {
138         printxval(addrfams, tcp->u_arg[0], "AF_???");
139         tprints(", ");
140         tprint_sock_type(tcp->u_arg[1]);
141         tprints(", ");
142         switch (tcp->u_arg[0]) {
143         case AF_INET:
144         case AF_INET6:
145                 printxval_search(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
146                 break;
147
148         case AF_NETLINK:
149                 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
150                 break;
151
152         case AF_PACKET:
153                 tprints("htons(");
154                 printxval_searchn(ethernet_protocols, ethernet_protocols_size,
155                                   ntohs(tcp->u_arg[2]), "ETH_P_???");
156                 tprints(")");
157                 break;
158
159         case AF_IRDA:
160                 printxval_index(can_protocols, tcp->u_arg[2], "IRDAPROTO_???");
161                 break;
162
163         case AF_CAN:
164                 printxval_index(can_protocols, tcp->u_arg[2], "CAN_???");
165                 break;
166
167         case AF_BLUETOOTH:
168                 printxval_index(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
169                 break;
170
171         case AF_RXRPC:
172                 printxval(addrfams, tcp->u_arg[2], "AF_???");
173                 break;
174
175         case AF_ISDN:
176                 printxval(isdn_protocols, tcp->u_arg[2], "ISDN_P_???");
177                 break;
178
179         case AF_PHONET:
180                 printxval_index(phonet_protocols, tcp->u_arg[2], "PN_PROTO_???");
181                 break;
182
183         case AF_CAIF:
184                 printxval_index(caif_protocols, tcp->u_arg[2], "CAIFPROTO_???");
185                 break;
186
187         case AF_NFC:
188                 printxval_index(nfc_protocols, tcp->u_arg[2],
189                                 "NFC_SOCKPROTO_???");
190                 break;
191
192         case AF_KCM:
193                 printxval_index(kcm_protocols, tcp->u_arg[2], "KCMPROTO_???");
194                 break;
195
196         case AF_SMC:
197                 printxval_index(smc_protocols, tcp->u_arg[2], "SMCPROTO_???");
198                 break;
199
200         default:
201                 tprintf("%" PRI_klu, tcp->u_arg[2]);
202                 break;
203         }
204
205         return RVAL_DECODED | RVAL_FD;
206 }
207
208 static bool
209 fetch_socklen(struct tcb *const tcp, int *const plen,
210               const kernel_ulong_t sockaddr, const kernel_ulong_t socklen)
211 {
212         return verbose(tcp) && sockaddr && socklen
213                && umove(tcp, socklen, plen) == 0;
214 }
215
216 static int
217 decode_sockname(struct tcb *tcp)
218 {
219         int ulen, rlen;
220
221         if (entering(tcp)) {
222                 printfd(tcp, tcp->u_arg[0]);
223                 tprints(", ");
224                 if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
225                         set_tcb_priv_ulong(tcp, ulen);
226                         return 0;
227                 } else {
228                         printaddr(tcp->u_arg[1]);
229                         tprints(", ");
230                         printaddr(tcp->u_arg[2]);
231                         return RVAL_DECODED;
232                 }
233         }
234
235         ulen = get_tcb_priv_ulong(tcp);
236
237         if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
238                 printaddr(tcp->u_arg[1]);
239                 tprintf(", [%d]", ulen);
240         } else {
241                 decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
242                 if (ulen != rlen)
243                         tprintf(", [%d->%d]", ulen, rlen);
244                 else
245                         tprintf(", [%d]", rlen);
246         }
247
248         return RVAL_DECODED;
249 }
250
251 SYS_FUNC(accept)
252 {
253         return decode_sockname(tcp) | RVAL_FD;
254 }
255
256 SYS_FUNC(accept4)
257 {
258         int rc = decode_sockname(tcp);
259
260         if (rc & RVAL_DECODED) {
261                 tprints(", ");
262                 printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
263         }
264
265         return rc | RVAL_FD;
266 }
267
268 SYS_FUNC(send)
269 {
270         printfd(tcp, tcp->u_arg[0]);
271         tprints(", ");
272         decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
273         tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
274         /* flags */
275         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
276
277         return RVAL_DECODED;
278 }
279
280 SYS_FUNC(sendto)
281 {
282         printfd(tcp, tcp->u_arg[0]);
283         tprints(", ");
284         decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
285         tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
286         /* flags */
287         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
288         /* to address */
289         const int addrlen = tcp->u_arg[5];
290         tprints(", ");
291         decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
292         /* to length */
293         tprintf(", %d", addrlen);
294
295         return RVAL_DECODED;
296 }
297
298 SYS_FUNC(recv)
299 {
300         if (entering(tcp)) {
301                 printfd(tcp, tcp->u_arg[0]);
302                 tprints(", ");
303         } else {
304                 if (syserror(tcp)) {
305                         printaddr(tcp->u_arg[1]);
306                 } else {
307                         decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
308                                      tcp->u_rval);
309                 }
310
311                 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
312                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
313         }
314         return 0;
315 }
316
317 SYS_FUNC(recvfrom)
318 {
319         int ulen, rlen;
320
321         if (entering(tcp)) {
322                 printfd(tcp, tcp->u_arg[0]);
323                 tprints(", ");
324                 if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
325                         set_tcb_priv_ulong(tcp, ulen);
326                 }
327         } else {
328                 /* buf */
329                 if (syserror(tcp)) {
330                         printaddr(tcp->u_arg[1]);
331                 } else {
332                         decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
333                                      tcp->u_rval);
334                 }
335                 /* size */
336                 tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
337                 /* flags */
338                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
339                 tprints(", ");
340
341                 ulen = get_tcb_priv_ulong(tcp);
342
343                 if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
344                         /* from address */
345                         printaddr(tcp->u_arg[4]);
346                         tprints(", ");
347                         /* from length */
348                         printaddr(tcp->u_arg[5]);
349                         return 0;
350                 }
351                 if (syserror(tcp)) {
352                         /* from address */
353                         printaddr(tcp->u_arg[4]);
354                         /* from length */
355                         tprintf(", [%d]", ulen);
356                         return 0;
357                 }
358                 /* from address */
359                 decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
360                 /* from length */
361                 if (ulen != rlen)
362                         tprintf(", [%d->%d]", ulen, rlen);
363                 else
364                         tprintf(", [%d]", rlen);
365         }
366         return 0;
367 }
368
369 SYS_FUNC(getsockname)
370 {
371         return decode_sockname(tcp);
372 }
373
374 static void
375 printpair_fd(struct tcb *tcp, const int i0, const int i1)
376 {
377         tprints("[");
378         printfd(tcp, i0);
379         tprints(", ");
380         printfd(tcp, i1);
381         tprints("]");
382 }
383
384 static void
385 decode_pair_fd(struct tcb *const tcp, const kernel_ulong_t addr)
386 {
387         int pair[2];
388
389         if (umove_or_printaddr(tcp, addr, &pair))
390                 return;
391
392         printpair_fd(tcp, pair[0], pair[1]);
393 }
394
395 static int
396 do_pipe(struct tcb *tcp, int flags_arg)
397 {
398         if (exiting(tcp)) {
399                 decode_pair_fd(tcp, tcp->u_arg[0]);
400                 if (flags_arg >= 0) {
401                         tprints(", ");
402                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
403                 }
404         }
405         return 0;
406 }
407
408 SYS_FUNC(pipe)
409 {
410 #if HAVE_ARCH_GETRVAL2
411         if (exiting(tcp) && !syserror(tcp))
412                 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
413         return 0;
414 #else
415         return do_pipe(tcp, -1);
416 #endif
417 }
418
419 SYS_FUNC(pipe2)
420 {
421         return do_pipe(tcp, 1);
422 }
423
424 SYS_FUNC(socketpair)
425 {
426         if (entering(tcp)) {
427                 printxval(addrfams, tcp->u_arg[0], "AF_???");
428                 tprints(", ");
429                 tprint_sock_type(tcp->u_arg[1]);
430                 tprintf(", %" PRI_klu, tcp->u_arg[2]);
431         } else {
432                 tprints(", ");
433                 decode_pair_fd(tcp, tcp->u_arg[3]);
434         }
435         return 0;
436 }
437
438 #include "xlat/sock_options.h"
439 #include "xlat/getsock_options.h"
440 #include "xlat/setsock_options.h"
441 #include "xlat/sock_ip_options.h"
442 #include "xlat/getsock_ip_options.h"
443 #include "xlat/setsock_ip_options.h"
444 #include "xlat/sock_ipv6_options.h"
445 #include "xlat/getsock_ipv6_options.h"
446 #include "xlat/setsock_ipv6_options.h"
447 #include "xlat/sock_ipx_options.h"
448 #include "xlat/sock_netlink_options.h"
449 #include "xlat/sock_packet_options.h"
450 #include "xlat/sock_raw_options.h"
451 #include "xlat/sock_sctp_options.h"
452 #include "xlat/sock_tcp_options.h"
453 #include "xlat/sock_udp_options.h"
454 #include "xlat/sock_irda_options.h"
455 #include "xlat/sock_llc_options.h"
456 #include "xlat/sock_dccp_options.h"
457 #include "xlat/sock_tipc_options.h"
458 #include "xlat/sock_rxrpc_options.h"
459 #include "xlat/sock_pppol2tp_options.h"
460 #include "xlat/sock_bluetooth_options.h"
461 #include "xlat/sock_pnp_options.h"
462 #include "xlat/sock_rds_options.h"
463 #include "xlat/sock_iucv_options.h"
464 #include "xlat/sock_caif_options.h"
465 #include "xlat/sock_alg_options.h"
466 #include "xlat/sock_nfcllcp_options.h"
467 #include "xlat/sock_kcm_options.h"
468 #include "xlat/sock_tls_options.h"
469 #include "xlat/sock_xdp_options.h"
470
471 static void
472 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
473                             unsigned int name, bool is_getsockopt)
474 {
475         printfd(tcp, fd);
476         tprints(", ");
477         printxval_search(socketlayers, level, "SOL_??");
478         tprints(", ");
479
480         switch (level) {
481         case SOL_SOCKET:
482                 printxvals(name, "SO_???", sock_options,
483                            is_getsockopt ? getsock_options :
484                                            setsock_options, NULL);
485                 break;
486         case SOL_IP:
487                 printxvals(name, "IP_???", sock_ip_options,
488                            is_getsockopt ? getsock_ip_options :
489                                            setsock_ip_options, NULL);
490                 break;
491         case SOL_IPV6:
492                 printxvals(name, "IPV6_???", sock_ipv6_options,
493                            is_getsockopt ? getsock_ipv6_options :
494                                            setsock_ipv6_options, NULL);
495                 break;
496         case SOL_IPX:
497                 printxval(sock_ipx_options, name, "IPX_???");
498                 break;
499         case SOL_PACKET:
500                 printxval(sock_packet_options, name, "PACKET_???");
501                 break;
502         case SOL_TCP:
503                 printxval_index(sock_tcp_options, name, "TCP_???");
504                 break;
505         case SOL_SCTP:
506                 printxval(sock_sctp_options, name, "SCTP_???");
507                 break;
508         case SOL_RAW:
509                 printxval(sock_raw_options, name, "RAW_???");
510                 break;
511         case SOL_NETLINK:
512                 printxval(sock_netlink_options, name, "NETLINK_???");
513                 break;
514         case SOL_UDP:
515                 printxval(sock_udp_options, name, "UDP_???");
516                 break;
517         case SOL_IRDA:
518                 printxval_index(sock_irda_options, name, "IRLMP_???");
519                 break;
520         case SOL_LLC:
521                 printxval_index(sock_llc_options, name, "LLC_OPT_???");
522                 break;
523         case SOL_DCCP:
524                 printxval_search(sock_dccp_options, name, "DCCP_SOCKOPT_???");
525                 break;
526         case SOL_TIPC:
527                 printxval_search(sock_tipc_options, name, "TIPC_???");
528                 break;
529         case SOL_RXRPC:
530                 printxval_index(sock_rxrpc_options, name, "RXRPC_???");
531                 break;
532         case SOL_PPPOL2TP:
533                 printxval_index(sock_pppol2tp_options, name, "PPPOL2TP_SO_???");
534                 break;
535         case SOL_BLUETOOTH:
536                 printxval_search(sock_bluetooth_options, name, "BT_???");
537                 break;
538         case SOL_PNPIPE:
539                 printxval(sock_pnp_options, name, "PNPIPE_???");
540                 break;
541         case SOL_RDS:
542                 printxval_search(sock_rds_options, name, "RDS_???");
543                 break;
544         case SOL_IUCV:
545                 printxval(sock_iucv_options, name, "SO_???");
546                 break;
547         case SOL_CAIF:
548                 printxval(sock_caif_options, name, "CAIFSO_???");
549                 break;
550         case SOL_ALG:
551                 printxval_index(sock_alg_options, name, "ALG_???");
552                 break;
553         case SOL_NFC:
554                 printxval_index(sock_nfcllcp_options, name, "NFC_LLCP_???");
555                 break;
556         case SOL_KCM:
557                 printxval(sock_kcm_options, name, "KCM_???");
558                 break;
559         case SOL_TLS:
560                 printxval(sock_tls_options, name, "TLS_???");
561                 break;
562         case SOL_XDP:
563                 printxval_index(sock_xdp_options, name, "XDP_???");
564                 break;
565
566                 /* Other SOL_* protocol levels still need work. */
567
568         default:
569                 tprintf("%u", name);
570         }
571
572         tprints(", ");
573 }
574
575 static void
576 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
577                  unsigned int len)
578 {
579         struct linger linger;
580
581         /*
582          * The kernel cannot return len > sizeof(linger) because struct linger
583          * cannot change, but extra safety won't harm either.
584          */
585         if (len > sizeof(linger))
586                 len = sizeof(linger);
587         if (umoven_or_printaddr(tcp, addr, len, &linger))
588                 return;
589
590         if (len < sizeof(linger.l_onoff)) {
591                 tprints("{l_onoff=");
592                 print_quoted_string((void *) &linger.l_onoff,
593                                     len, QUOTE_FORCE_HEX);
594         } else {
595                 PRINT_FIELD_D("{", linger, l_onoff);
596
597                 if (len > offsetof(struct linger, l_linger)) {
598                         len -= offsetof(struct linger, l_linger);
599                         if (len < sizeof(linger.l_linger)) {
600                                 tprints(", l_linger=");
601                                 print_quoted_string((void *) &linger.l_linger,
602                                                     len, QUOTE_FORCE_HEX);
603                         } else {
604                                 PRINT_FIELD_D(", ", linger, l_linger);
605                         }
606                 }
607         }
608         tprints("}");
609 }
610
611 static void
612 print_get_ucred(struct tcb *const tcp, const kernel_ulong_t addr,
613                 unsigned int len)
614 {
615         struct ucred uc;
616
617         /*
618          * The kernel is very unlikely to return len > sizeof(uc)
619          * because struct ucred is very unlikely to change,
620          * but extra safety won't harm either.
621          */
622         if (len > sizeof(uc))
623                 len = sizeof(uc);
624
625         if (umoven_or_printaddr(tcp, addr, len, &uc))
626                 return;
627
628         if (len < sizeof(uc.pid)) {
629                 tprints("{pid=");
630                 print_quoted_string((void *) &uc.pid,
631                                     len, QUOTE_FORCE_HEX);
632         } else {
633                 PRINT_FIELD_D("{", uc, pid);
634
635                 if (len > offsetof(struct ucred, uid)) {
636                         len -= offsetof(struct ucred, uid);
637                         if (len < sizeof(uc.uid)) {
638                                 tprints(", uid=");
639                                 print_quoted_string((void *) &uc.uid,
640                                                     len, QUOTE_FORCE_HEX);
641                         } else {
642                                 PRINT_FIELD_UID(", ", uc, uid);
643
644                                 if (len > offsetof(struct ucred, gid) -
645                                           offsetof(struct ucred, uid)) {
646                                         len -= offsetof(struct ucred, gid) -
647                                                offsetof(struct ucred, uid);
648                                         if (len < sizeof(uc.gid)) {
649                                                 tprints(", gid=");
650                                                 print_quoted_string((void *) &uc.gid,
651                                                                     len,
652                                                                     QUOTE_FORCE_HEX);
653                                         } else {
654                                                 PRINT_FIELD_UID(", ", uc, gid);
655                                         }
656                                 }
657                         }
658                 }
659         }
660         tprints("}");
661 }
662
663 #ifdef PACKET_STATISTICS
664 static void
665 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
666                     unsigned int len)
667 {
668         struct tp_stats {
669                 unsigned int tp_packets, tp_drops, tp_freeze_q_cnt;
670         } stats;
671
672         /*
673          * The kernel may return len > sizeof(stats) if the kernel structure
674          * grew as it happened when tpacket_stats_v3 was introduced.
675          */
676         if (len > sizeof(stats))
677                 len = sizeof(stats);
678
679         if (umoven_or_printaddr(tcp, addr, len, &stats))
680                 return;
681
682         if (len < sizeof(stats.tp_packets)) {
683                 tprints("{tp_packets=");
684                 print_quoted_string((void *) &stats.tp_packets,
685                                     len, QUOTE_FORCE_HEX);
686         } else {
687                 PRINT_FIELD_U("{", stats, tp_packets);
688
689                 if (len > offsetof(struct tp_stats, tp_drops)) {
690                         len -= offsetof(struct tp_stats, tp_drops);
691                         if (len < sizeof(stats.tp_drops)) {
692                                 tprints(", tp_drops=");
693                                 print_quoted_string((void *) &stats.tp_drops,
694                                                     len, QUOTE_FORCE_HEX);
695                         } else {
696                                 PRINT_FIELD_U(", ", stats, tp_drops);
697
698                                 if (len > offsetof(struct tp_stats, tp_freeze_q_cnt) -
699                                           offsetof(struct tp_stats, tp_drops)) {
700                                         len -= offsetof(struct tp_stats, tp_freeze_q_cnt) -
701                                                offsetof(struct tp_stats, tp_drops);
702                                         if (len < sizeof(stats.tp_freeze_q_cnt)) {
703                                                 tprints(", tp_freeze_q_cnt=");
704                                                 print_quoted_string((void *) &stats.tp_freeze_q_cnt,
705                                                                     len,
706                                                                     QUOTE_FORCE_HEX);
707                                         } else {
708                                                 PRINT_FIELD_U(", ", stats, tp_freeze_q_cnt);
709                                         }
710                                 }
711                         }
712                 }
713         }
714         tprints("}");
715 }
716 #endif /* PACKET_STATISTICS */
717
718 #include "xlat/icmpfilterflags.h"
719
720 static void
721 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
722 {
723         struct icmp_filter filter = {};
724
725         if (len > (int) sizeof(filter))
726                 len = sizeof(filter);
727         else if (len <= 0) {
728                 printaddr(addr);
729                 return;
730         }
731
732         if (umoven_or_printaddr(tcp, addr, len, &filter))
733                 return;
734
735         tprints("~(");
736         printflags(icmpfilterflags, ~filter.data, "ICMP_???");
737         tprints(")");
738 }
739
740 static bool
741 print_uint32(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
742 {
743         tprintf("%u", *(uint32_t *) elem_buf);
744
745         return true;
746 }
747
748 static void
749 print_getsockopt(struct tcb *const tcp, const unsigned int level,
750                  const unsigned int name, const kernel_ulong_t addr,
751                  const int ulen, const int rlen)
752 {
753         if (ulen <= 0 || rlen <= 0) {
754                 /*
755                  * As the kernel neither accepts nor returns a negative
756                  * length in case of successful getsockopt syscall
757                  * invocation, negative values must have been forged
758                  * by userspace.
759                  */
760                 printaddr(addr);
761                 return;
762         }
763
764         if (addr && verbose(tcp))
765         switch (level) {
766         case SOL_SOCKET:
767                 switch (name) {
768                 case SO_LINGER:
769                         print_get_linger(tcp, addr, rlen);
770                         return;
771                 case SO_PEERCRED:
772                         print_get_ucred(tcp, addr, rlen);
773                         return;
774                 case SO_ATTACH_FILTER:
775                         /*
776                          * The length returned by the kernel in case of
777                          * successful getsockopt syscall invocation is struct
778                          * sock_fprog.len that has type unsigned short,
779                          * anything else must have been forged by userspace.
780                          */
781                         if ((unsigned short) rlen == (unsigned int) rlen)
782                                 print_sock_fprog(tcp, addr, rlen);
783                         else
784                                 printaddr(addr);
785                         return;
786                 }
787                 break;
788
789         case SOL_PACKET:
790                 switch (name) {
791 #ifdef PACKET_STATISTICS
792                 case PACKET_STATISTICS:
793                         print_tpacket_stats(tcp, addr, rlen);
794                         return;
795 #endif
796                 }
797                 break;
798
799         case SOL_RAW:
800                 switch (name) {
801                 case ICMP_FILTER:
802                         print_icmp_filter(tcp, addr, rlen);
803                         return;
804                 }
805                 break;
806
807         case SOL_NETLINK:
808                 switch (name) {
809                 case NETLINK_LIST_MEMBERSHIPS: {
810                         uint32_t buf;
811                         print_array(tcp, addr, MIN(ulen, rlen) / sizeof(buf),
812                                     &buf, sizeof(buf),
813                                     tfetch_mem, print_uint32, 0);
814                         break;
815                         }
816                 default:
817                         printnum_int(tcp, addr, "%d");
818                         break;
819                 }
820                 return;
821         }
822
823         /* default arg printing */
824
825         if (verbose(tcp)) {
826                 if (rlen == sizeof(int)) {
827                         printnum_int(tcp, addr, "%d");
828                 } else {
829                         printstrn(tcp, addr, rlen);
830                 }
831         } else {
832                 printaddr(addr);
833         }
834 }
835
836 SYS_FUNC(getsockopt)
837 {
838         int ulen, rlen;
839
840         if (entering(tcp)) {
841                 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
842                                             tcp->u_arg[1], tcp->u_arg[2], true);
843
844                 if (verbose(tcp) && tcp->u_arg[4]
845                     && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
846                         set_tcb_priv_ulong(tcp, ulen);
847                         return 0;
848                 } else {
849                         printaddr(tcp->u_arg[3]);
850                         tprints(", ");
851                         printaddr(tcp->u_arg[4]);
852                         return RVAL_DECODED;
853                 }
854         } else {
855                 ulen = get_tcb_priv_ulong(tcp);
856
857                 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
858                         printaddr(tcp->u_arg[3]);
859                         tprintf(", [%d]", ulen);
860                 } else {
861                         print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
862                                          tcp->u_arg[3], ulen, rlen);
863                         if (ulen != rlen)
864                                 tprintf(", [%d->%d]", ulen, rlen);
865                         else
866                                 tprintf(", [%d]", rlen);
867                 }
868         }
869         return 0;
870 }
871
872 static void
873 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
874                  const int len)
875 {
876         struct linger linger;
877
878         if (len < (int) sizeof(linger)) {
879                 printaddr(addr);
880         } else if (!umove_or_printaddr(tcp, addr, &linger)) {
881                 PRINT_FIELD_D("{", linger, l_onoff);
882                 PRINT_FIELD_D(", ", linger, l_linger);
883                 tprints("}");
884         }
885 }
886
887 #ifdef IP_ADD_MEMBERSHIP
888 static void
889 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
890            const int len)
891 {
892         struct ip_mreq mreq;
893
894         if (len < (int) sizeof(mreq)) {
895                 printaddr(addr);
896         } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
897                 PRINT_FIELD_INET4_ADDR("{", mreq, imr_multiaddr);
898                 PRINT_FIELD_INET4_ADDR(", ", mreq, imr_interface);
899                 tprints("}");
900         }
901 }
902 #endif /* IP_ADD_MEMBERSHIP */
903
904 #ifdef IPV6_ADD_MEMBERSHIP
905 static void
906 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
907             const int len)
908 {
909         struct ipv6_mreq mreq;
910
911         if (len < (int) sizeof(mreq)) {
912                 printaddr(addr);
913         } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
914                 PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
915                 PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
916                 tprints("}");
917         }
918 }
919 #endif /* IPV6_ADD_MEMBERSHIP */
920
921 #ifdef PACKET_RX_RING
922 static void
923 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
924 {
925         struct tpacket_req req;
926
927         if (len != sizeof(req) ||
928             umove(tcp, addr, &req) < 0) {
929                 printaddr(addr);
930         } else {
931                 PRINT_FIELD_U("{", req, tp_block_size);
932                 PRINT_FIELD_U(", ", req, tp_block_nr);
933                 PRINT_FIELD_U(", ", req, tp_frame_size);
934                 PRINT_FIELD_U(", ", req, tp_frame_nr);
935                 tprints("}");
936         }
937 }
938 #endif /* PACKET_RX_RING */
939
940 #ifdef PACKET_ADD_MEMBERSHIP
941 # include "xlat/packet_mreq_type.h"
942
943 static void
944 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
945 {
946         struct packet_mreq mreq;
947
948         if (len != sizeof(mreq) ||
949             umove(tcp, addr, &mreq) < 0) {
950                 printaddr(addr);
951         } else {
952                 unsigned int i;
953
954                 PRINT_FIELD_IFINDEX("{", mreq, mr_ifindex);
955                 PRINT_FIELD_XVAL(", ", mreq, mr_type, packet_mreq_type,
956                                  "PACKET_MR_???");
957                 PRINT_FIELD_U(", ", mreq, mr_alen);
958                 tprints(", mr_address=");
959                 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
960                         mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
961                 for (i = 0; i < mreq.mr_alen; ++i)
962                         tprintf("%02x", mreq.mr_address[i]);
963                 tprints("}");
964         }
965 }
966 #endif /* PACKET_ADD_MEMBERSHIP */
967
968 static void
969 print_setsockopt(struct tcb *const tcp, const unsigned int level,
970                  const unsigned int name, const kernel_ulong_t addr,
971                  const int len)
972 {
973         if (addr && verbose(tcp))
974         switch (level) {
975         case SOL_SOCKET:
976                 switch (name) {
977                 case SO_LINGER:
978                         print_set_linger(tcp, addr, len);
979                         return;
980                 case SO_ATTACH_FILTER:
981                 case SO_ATTACH_REUSEPORT_CBPF:
982                         if ((unsigned int) len == get_sock_fprog_size())
983                                 decode_sock_fprog(tcp, addr);
984                         else
985                                 printaddr(addr);
986                         return;
987                 }
988                 break;
989
990         case SOL_IP:
991                 switch (name) {
992 #ifdef IP_ADD_MEMBERSHIP
993                 case IP_ADD_MEMBERSHIP:
994                 case IP_DROP_MEMBERSHIP:
995                         print_mreq(tcp, addr, len);
996                         return;
997 #endif /* IP_ADD_MEMBERSHIP */
998 #ifdef MCAST_JOIN_GROUP
999                 case MCAST_JOIN_GROUP:
1000                 case MCAST_LEAVE_GROUP:
1001                         print_group_req(tcp, addr, len);
1002                         return;
1003 #endif /* MCAST_JOIN_GROUP */
1004                 }
1005                 break;
1006
1007         case SOL_IPV6:
1008                 switch (name) {
1009 #ifdef IPV6_ADD_MEMBERSHIP
1010                 case IPV6_ADD_MEMBERSHIP:
1011                 case IPV6_DROP_MEMBERSHIP:
1012 # ifdef IPV6_JOIN_ANYCAST
1013                 case IPV6_JOIN_ANYCAST:
1014 # endif
1015 # ifdef IPV6_LEAVE_ANYCAST
1016                 case IPV6_LEAVE_ANYCAST:
1017 # endif
1018                         print_mreq6(tcp, addr, len);
1019                         return;
1020 #endif /* IPV6_ADD_MEMBERSHIP */
1021 #ifdef MCAST_JOIN_GROUP
1022                 case MCAST_JOIN_GROUP:
1023                 case MCAST_LEAVE_GROUP:
1024                         print_group_req(tcp, addr, len);
1025                         return;
1026 #endif /* MCAST_JOIN_GROUP */
1027                 }
1028                 break;
1029
1030         case SOL_PACKET:
1031                 switch (name) {
1032 #ifdef PACKET_RX_RING
1033                 case PACKET_RX_RING:
1034 # ifdef PACKET_TX_RING
1035                 case PACKET_TX_RING:
1036 # endif
1037                         print_tpacket_req(tcp, addr, len);
1038                         return;
1039 #endif /* PACKET_RX_RING */
1040 #ifdef PACKET_ADD_MEMBERSHIP
1041                 case PACKET_ADD_MEMBERSHIP:
1042                 case PACKET_DROP_MEMBERSHIP:
1043                         print_packet_mreq(tcp, addr, len);
1044                         return;
1045 #endif /* PACKET_ADD_MEMBERSHIP */
1046                 }
1047                 break;
1048
1049         case SOL_RAW:
1050                 switch (name) {
1051                 case ICMP_FILTER:
1052                         print_icmp_filter(tcp, addr, len);
1053                         return;
1054                 }
1055                 break;
1056
1057         case SOL_NETLINK:
1058                 if (len < (int) sizeof(int))
1059                         printaddr(addr);
1060                 else
1061                         printnum_int(tcp, addr, "%d");
1062                 return;
1063         }
1064
1065         /* default arg printing */
1066
1067         if (verbose(tcp)) {
1068                 if (len == sizeof(int)) {
1069                         printnum_int(tcp, addr, "%d");
1070                 } else {
1071                         printstrn(tcp, addr, len);
1072                 }
1073         } else {
1074                 printaddr(addr);
1075         }
1076 }
1077
1078 SYS_FUNC(setsockopt)
1079 {
1080         print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1081                                     tcp->u_arg[1], tcp->u_arg[2], false);
1082         print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1083                          tcp->u_arg[3], tcp->u_arg[4]);
1084         tprintf(", %d", (int) tcp->u_arg[4]);
1085
1086         return RVAL_DECODED;
1087 }