]> granicus.if.org Git - strace/blob - net.c
66dde0f40ca3490043bdc101cb0e6de629bd27c1
[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 #include "xlat/sock_xdp_options.h"
462
463 static void
464 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
465                             unsigned int name, bool is_getsockopt)
466 {
467         printfd(tcp, fd);
468         tprints(", ");
469         printxval_search(socketlayers, level, "SOL_??");
470         tprints(", ");
471
472         switch (level) {
473         case SOL_SOCKET:
474                 printxvals(name, "SO_???", sock_options,
475                            is_getsockopt ? getsock_options :
476                                            setsock_options, NULL);
477                 break;
478         case SOL_IP:
479                 printxvals(name, "IP_???", sock_ip_options,
480                            is_getsockopt ? getsock_ip_options :
481                                            setsock_ip_options, NULL);
482                 break;
483         case SOL_IPV6:
484                 printxvals(name, "IPV6_???", sock_ipv6_options,
485                            is_getsockopt ? getsock_ipv6_options :
486                                            setsock_ipv6_options, NULL);
487                 break;
488         case SOL_IPX:
489                 printxval(sock_ipx_options, name, "IPX_???");
490                 break;
491         case SOL_PACKET:
492                 printxval(sock_packet_options, name, "PACKET_???");
493                 break;
494         case SOL_TCP:
495                 printxval_index(sock_tcp_options, name, "TCP_???");
496                 break;
497         case SOL_SCTP:
498                 printxval(sock_sctp_options, name, "SCTP_???");
499                 break;
500         case SOL_RAW:
501                 printxval(sock_raw_options, name, "RAW_???");
502                 break;
503         case SOL_NETLINK:
504                 printxval(sock_netlink_options, name, "NETLINK_???");
505                 break;
506         case SOL_UDP:
507                 printxval(sock_udp_options, name, "UDP_???");
508                 break;
509         case SOL_IRDA:
510                 printxval_index(sock_irda_options, name, "IRLMP_???");
511                 break;
512         case SOL_LLC:
513                 printxval_index(sock_llc_options, name, "LLC_OPT_???");
514                 break;
515         case SOL_DCCP:
516                 printxval_search(sock_dccp_options, name, "DCCP_SOCKOPT_???");
517                 break;
518         case SOL_TIPC:
519                 printxval_search(sock_tipc_options, name, "TIPC_???");
520                 break;
521         case SOL_RXRPC:
522                 printxval_index(sock_rxrpc_options, name, "RXRPC_???");
523                 break;
524         case SOL_PPPOL2TP:
525                 printxval_index(sock_pppol2tp_options, name, "PPPOL2TP_SO_???");
526                 break;
527         case SOL_BLUETOOTH:
528                 printxval_search(sock_bluetooth_options, name, "BT_???");
529                 break;
530         case SOL_PNPIPE:
531                 printxval(sock_pnp_options, name, "PNPIPE_???");
532                 break;
533         case SOL_RDS:
534                 printxval_search(sock_rds_options, name, "RDS_???");
535                 break;
536         case SOL_IUCV:
537                 printxval(sock_iucv_options, name, "SO_???");
538                 break;
539         case SOL_CAIF:
540                 printxval(sock_caif_options, name, "CAIFSO_???");
541                 break;
542         case SOL_ALG:
543                 printxval_index(sock_alg_options, name, "ALG_???");
544                 break;
545         case SOL_NFC:
546                 printxval_index(sock_nfcllcp_options, name, "NFC_LLCP_???");
547                 break;
548         case SOL_KCM:
549                 printxval(sock_kcm_options, name, "KCM_???");
550                 break;
551         case SOL_TLS:
552                 printxval(sock_tls_options, name, "TLS_???");
553                 break;
554         case SOL_XDP:
555                 printxval_index(sock_xdp_options, name, "XDP_???");
556                 break;
557
558                 /* Other SOL_* protocol levels still need work. */
559
560         default:
561                 tprintf("%u", name);
562         }
563
564         tprints(", ");
565 }
566
567 static void
568 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
569                  unsigned int len)
570 {
571         struct linger linger;
572
573         /*
574          * The kernel cannot return len > sizeof(linger) because struct linger
575          * cannot change, but extra safety won't harm either.
576          */
577         if (len > sizeof(linger))
578                 len = sizeof(linger);
579         if (umoven_or_printaddr(tcp, addr, len, &linger))
580                 return;
581
582         if (len < sizeof(linger.l_onoff)) {
583                 tprints("{l_onoff=");
584                 print_quoted_string((void *) &linger.l_onoff,
585                                     len, QUOTE_FORCE_HEX);
586         } else {
587                 PRINT_FIELD_D("{", linger, l_onoff);
588
589                 if (len > offsetof(struct linger, l_linger)) {
590                         len -= offsetof(struct linger, l_linger);
591                         if (len < sizeof(linger.l_linger)) {
592                                 tprints(", l_linger=");
593                                 print_quoted_string((void *) &linger.l_linger,
594                                                     len, QUOTE_FORCE_HEX);
595                         } else {
596                                 PRINT_FIELD_D(", ", linger, l_linger);
597                         }
598                 }
599         }
600         tprints("}");
601 }
602
603 static void
604 print_get_ucred(struct tcb *const tcp, const kernel_ulong_t addr,
605                 unsigned int len)
606 {
607         struct ucred uc;
608
609         /*
610          * The kernel is very unlikely to return len > sizeof(uc)
611          * because struct ucred is very unlikely to change,
612          * but extra safety won't harm either.
613          */
614         if (len > sizeof(uc))
615                 len = sizeof(uc);
616
617         if (umoven_or_printaddr(tcp, addr, len, &uc))
618                 return;
619
620         if (len < sizeof(uc.pid)) {
621                 tprints("{pid=");
622                 print_quoted_string((void *) &uc.pid,
623                                     len, QUOTE_FORCE_HEX);
624         } else {
625                 PRINT_FIELD_D("{", uc, pid);
626
627                 if (len > offsetof(struct ucred, uid)) {
628                         len -= offsetof(struct ucred, uid);
629                         if (len < sizeof(uc.uid)) {
630                                 tprints(", uid=");
631                                 print_quoted_string((void *) &uc.uid,
632                                                     len, QUOTE_FORCE_HEX);
633                         } else {
634                                 PRINT_FIELD_UID(", ", uc, uid);
635
636                                 if (len > offsetof(struct ucred, gid) -
637                                           offsetof(struct ucred, uid)) {
638                                         len -= offsetof(struct ucred, gid) -
639                                                offsetof(struct ucred, uid);
640                                         if (len < sizeof(uc.gid)) {
641                                                 tprints(", gid=");
642                                                 print_quoted_string((void *) &uc.gid,
643                                                                     len,
644                                                                     QUOTE_FORCE_HEX);
645                                         } else {
646                                                 PRINT_FIELD_UID(", ", uc, gid);
647                                         }
648                                 }
649                         }
650                 }
651         }
652         tprints("}");
653 }
654
655 #ifdef PACKET_STATISTICS
656 static void
657 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
658                     unsigned int len)
659 {
660         struct tp_stats {
661                 unsigned int tp_packets, tp_drops, tp_freeze_q_cnt;
662         } stats;
663
664         /*
665          * The kernel may return len > sizeof(stats) if the kernel structure
666          * grew as it happened when tpacket_stats_v3 was introduced.
667          */
668         if (len > sizeof(stats))
669                 len = sizeof(stats);
670
671         if (umoven_or_printaddr(tcp, addr, len, &stats))
672                 return;
673
674         if (len < sizeof(stats.tp_packets)) {
675                 tprints("{tp_packets=");
676                 print_quoted_string((void *) &stats.tp_packets,
677                                     len, QUOTE_FORCE_HEX);
678         } else {
679                 PRINT_FIELD_U("{", stats, tp_packets);
680
681                 if (len > offsetof(struct tp_stats, tp_drops)) {
682                         len -= offsetof(struct tp_stats, tp_drops);
683                         if (len < sizeof(stats.tp_drops)) {
684                                 tprints(", tp_drops=");
685                                 print_quoted_string((void *) &stats.tp_drops,
686                                                     len, QUOTE_FORCE_HEX);
687                         } else {
688                                 PRINT_FIELD_U(", ", stats, tp_drops);
689
690                                 if (len > offsetof(struct tp_stats, tp_freeze_q_cnt) -
691                                           offsetof(struct tp_stats, tp_drops)) {
692                                         len -= offsetof(struct tp_stats, tp_freeze_q_cnt) -
693                                                offsetof(struct tp_stats, tp_drops);
694                                         if (len < sizeof(stats.tp_freeze_q_cnt)) {
695                                                 tprints(", tp_freeze_q_cnt=");
696                                                 print_quoted_string((void *) &stats.tp_freeze_q_cnt,
697                                                                     len,
698                                                                     QUOTE_FORCE_HEX);
699                                         } else {
700                                                 PRINT_FIELD_U(", ", stats, tp_freeze_q_cnt);
701                                         }
702                                 }
703                         }
704                 }
705         }
706         tprints("}");
707 }
708 #endif /* PACKET_STATISTICS */
709
710 #include "xlat/icmpfilterflags.h"
711
712 static void
713 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
714 {
715         struct icmp_filter filter = {};
716
717         if (len > (int) sizeof(filter))
718                 len = sizeof(filter);
719         else if (len <= 0) {
720                 printaddr(addr);
721                 return;
722         }
723
724         if (umoven_or_printaddr(tcp, addr, len, &filter))
725                 return;
726
727         tprints("~(");
728         printflags(icmpfilterflags, ~filter.data, "ICMP_???");
729         tprints(")");
730 }
731
732 static bool
733 print_uint32(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
734 {
735         tprintf("%u", *(uint32_t *) elem_buf);
736
737         return true;
738 }
739
740 static void
741 print_getsockopt(struct tcb *const tcp, const unsigned int level,
742                  const unsigned int name, const kernel_ulong_t addr,
743                  const int ulen, const int rlen)
744 {
745         if (ulen <= 0 || rlen <= 0) {
746                 /*
747                  * As the kernel neither accepts nor returns a negative
748                  * length in case of successful getsockopt syscall
749                  * invocation, negative values must have been forged
750                  * by userspace.
751                  */
752                 printaddr(addr);
753                 return;
754         }
755
756         if (addr && verbose(tcp))
757         switch (level) {
758         case SOL_SOCKET:
759                 switch (name) {
760                 case SO_LINGER:
761                         print_get_linger(tcp, addr, rlen);
762                         return;
763                 case SO_PEERCRED:
764                         print_get_ucred(tcp, addr, rlen);
765                         return;
766                 case SO_ATTACH_FILTER:
767                         /*
768                          * The length returned by the kernel in case of
769                          * successful getsockopt syscall invocation is struct
770                          * sock_fprog.len that has type unsigned short,
771                          * anything else must have been forged by userspace.
772                          */
773                         if ((unsigned short) rlen == (unsigned int) rlen)
774                                 print_sock_fprog(tcp, addr, rlen);
775                         else
776                                 printaddr(addr);
777                         return;
778                 }
779                 break;
780
781         case SOL_PACKET:
782                 switch (name) {
783 #ifdef PACKET_STATISTICS
784                 case PACKET_STATISTICS:
785                         print_tpacket_stats(tcp, addr, rlen);
786                         return;
787 #endif
788                 }
789                 break;
790
791         case SOL_RAW:
792                 switch (name) {
793                 case ICMP_FILTER:
794                         print_icmp_filter(tcp, addr, rlen);
795                         return;
796                 }
797                 break;
798
799         case SOL_NETLINK:
800                 switch (name) {
801                 case NETLINK_LIST_MEMBERSHIPS: {
802                         uint32_t buf;
803                         print_array(tcp, addr, MIN(ulen, rlen) / sizeof(buf),
804                                     &buf, sizeof(buf),
805                                     tfetch_mem, print_uint32, 0);
806                         break;
807                         }
808                 default:
809                         printnum_int(tcp, addr, "%d");
810                         break;
811                 }
812                 return;
813         }
814
815         /* default arg printing */
816
817         if (verbose(tcp)) {
818                 if (rlen == sizeof(int)) {
819                         printnum_int(tcp, addr, "%d");
820                 } else {
821                         printstrn(tcp, addr, rlen);
822                 }
823         } else {
824                 printaddr(addr);
825         }
826 }
827
828 SYS_FUNC(getsockopt)
829 {
830         int ulen, rlen;
831
832         if (entering(tcp)) {
833                 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
834                                             tcp->u_arg[1], tcp->u_arg[2], true);
835
836                 if (verbose(tcp) && tcp->u_arg[4]
837                     && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
838                         set_tcb_priv_ulong(tcp, ulen);
839                         return 0;
840                 } else {
841                         printaddr(tcp->u_arg[3]);
842                         tprints(", ");
843                         printaddr(tcp->u_arg[4]);
844                         return RVAL_DECODED;
845                 }
846         } else {
847                 ulen = get_tcb_priv_ulong(tcp);
848
849                 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
850                         printaddr(tcp->u_arg[3]);
851                         tprintf(", [%d]", ulen);
852                 } else {
853                         print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
854                                          tcp->u_arg[3], ulen, rlen);
855                         if (ulen != rlen)
856                                 tprintf(", [%d->%d]", ulen, rlen);
857                         else
858                                 tprintf(", [%d]", rlen);
859                 }
860         }
861         return 0;
862 }
863
864 static void
865 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
866                  const int len)
867 {
868         struct linger linger;
869
870         if (len < (int) sizeof(linger)) {
871                 printaddr(addr);
872         } else if (!umove_or_printaddr(tcp, addr, &linger)) {
873                 PRINT_FIELD_D("{", linger, l_onoff);
874                 PRINT_FIELD_D(", ", linger, l_linger);
875                 tprints("}");
876         }
877 }
878
879 #ifdef IP_ADD_MEMBERSHIP
880 static void
881 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
882            const int len)
883 {
884         struct ip_mreq mreq;
885
886         if (len < (int) sizeof(mreq)) {
887                 printaddr(addr);
888         } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
889                 PRINT_FIELD_INET4_ADDR("{", mreq, imr_multiaddr);
890                 PRINT_FIELD_INET4_ADDR(", ", mreq, imr_interface);
891                 tprints("}");
892         }
893 }
894 #endif /* IP_ADD_MEMBERSHIP */
895
896 #ifdef IPV6_ADD_MEMBERSHIP
897 static void
898 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
899             const int len)
900 {
901         struct ipv6_mreq mreq;
902
903         if (len < (int) sizeof(mreq)) {
904                 printaddr(addr);
905         } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
906                 PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
907                 PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
908                 tprints("}");
909         }
910 }
911 #endif /* IPV6_ADD_MEMBERSHIP */
912
913 #ifdef PACKET_RX_RING
914 static void
915 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
916 {
917         struct tpacket_req req;
918
919         if (len != sizeof(req) ||
920             umove(tcp, addr, &req) < 0) {
921                 printaddr(addr);
922         } else {
923                 PRINT_FIELD_U("{", req, tp_block_size);
924                 PRINT_FIELD_U(", ", req, tp_block_nr);
925                 PRINT_FIELD_U(", ", req, tp_frame_size);
926                 PRINT_FIELD_U(", ", req, tp_frame_nr);
927                 tprints("}");
928         }
929 }
930 #endif /* PACKET_RX_RING */
931
932 #ifdef PACKET_ADD_MEMBERSHIP
933 # include "xlat/packet_mreq_type.h"
934
935 static void
936 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
937 {
938         struct packet_mreq mreq;
939
940         if (len != sizeof(mreq) ||
941             umove(tcp, addr, &mreq) < 0) {
942                 printaddr(addr);
943         } else {
944                 unsigned int i;
945
946                 PRINT_FIELD_IFINDEX("{", mreq, mr_ifindex);
947                 PRINT_FIELD_XVAL(", ", mreq, mr_type, packet_mreq_type,
948                                  "PACKET_MR_???");
949                 PRINT_FIELD_U(", ", mreq, mr_alen);
950                 tprints(", mr_address=");
951                 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
952                         mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
953                 for (i = 0; i < mreq.mr_alen; ++i)
954                         tprintf("%02x", mreq.mr_address[i]);
955                 tprints("}");
956         }
957 }
958 #endif /* PACKET_ADD_MEMBERSHIP */
959
960 static void
961 print_setsockopt(struct tcb *const tcp, const unsigned int level,
962                  const unsigned int name, const kernel_ulong_t addr,
963                  const int len)
964 {
965         if (addr && verbose(tcp))
966         switch (level) {
967         case SOL_SOCKET:
968                 switch (name) {
969                 case SO_LINGER:
970                         print_set_linger(tcp, addr, len);
971                         return;
972                 case SO_ATTACH_FILTER:
973                 case SO_ATTACH_REUSEPORT_CBPF:
974                         if ((unsigned int) len == get_sock_fprog_size())
975                                 decode_sock_fprog(tcp, addr);
976                         else
977                                 printaddr(addr);
978                         return;
979                 }
980                 break;
981
982         case SOL_IP:
983                 switch (name) {
984 #ifdef IP_ADD_MEMBERSHIP
985                 case IP_ADD_MEMBERSHIP:
986                 case IP_DROP_MEMBERSHIP:
987                         print_mreq(tcp, addr, len);
988                         return;
989 #endif /* IP_ADD_MEMBERSHIP */
990 #ifdef MCAST_JOIN_GROUP
991                 case MCAST_JOIN_GROUP:
992                 case MCAST_LEAVE_GROUP:
993                         print_group_req(tcp, addr, len);
994                         return;
995 #endif /* MCAST_JOIN_GROUP */
996                 }
997                 break;
998
999         case SOL_IPV6:
1000                 switch (name) {
1001 #ifdef IPV6_ADD_MEMBERSHIP
1002                 case IPV6_ADD_MEMBERSHIP:
1003                 case IPV6_DROP_MEMBERSHIP:
1004 # ifdef IPV6_JOIN_ANYCAST
1005                 case IPV6_JOIN_ANYCAST:
1006 # endif
1007 # ifdef IPV6_LEAVE_ANYCAST
1008                 case IPV6_LEAVE_ANYCAST:
1009 # endif
1010                         print_mreq6(tcp, addr, len);
1011                         return;
1012 #endif /* IPV6_ADD_MEMBERSHIP */
1013 #ifdef MCAST_JOIN_GROUP
1014                 case MCAST_JOIN_GROUP:
1015                 case MCAST_LEAVE_GROUP:
1016                         print_group_req(tcp, addr, len);
1017                         return;
1018 #endif /* MCAST_JOIN_GROUP */
1019                 }
1020                 break;
1021
1022         case SOL_PACKET:
1023                 switch (name) {
1024 #ifdef PACKET_RX_RING
1025                 case PACKET_RX_RING:
1026 # ifdef PACKET_TX_RING
1027                 case PACKET_TX_RING:
1028 # endif
1029                         print_tpacket_req(tcp, addr, len);
1030                         return;
1031 #endif /* PACKET_RX_RING */
1032 #ifdef PACKET_ADD_MEMBERSHIP
1033                 case PACKET_ADD_MEMBERSHIP:
1034                 case PACKET_DROP_MEMBERSHIP:
1035                         print_packet_mreq(tcp, addr, len);
1036                         return;
1037 #endif /* PACKET_ADD_MEMBERSHIP */
1038                 }
1039                 break;
1040
1041         case SOL_RAW:
1042                 switch (name) {
1043                 case ICMP_FILTER:
1044                         print_icmp_filter(tcp, addr, len);
1045                         return;
1046                 }
1047                 break;
1048
1049         case SOL_NETLINK:
1050                 if (len < (int) sizeof(int))
1051                         printaddr(addr);
1052                 else
1053                         printnum_int(tcp, addr, "%d");
1054                 return;
1055         }
1056
1057         /* default arg printing */
1058
1059         if (verbose(tcp)) {
1060                 if (len == sizeof(int)) {
1061                         printnum_int(tcp, addr, "%d");
1062                 } else {
1063                         printstrn(tcp, addr, len);
1064                 }
1065         } else {
1066                 printaddr(addr);
1067         }
1068 }
1069
1070 SYS_FUNC(setsockopt)
1071 {
1072         print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1073                                     tcp->u_arg[1], tcp->u_arg[2], false);
1074         print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1075                          tcp->u_arg[3], tcp->u_arg[4]);
1076         tprintf(", %d", (int) tcp->u_arg[4]);
1077
1078         return RVAL_DECODED;
1079 }