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