]> granicus.if.org Git - strace/blob - net.c
nlattr: add UID/GID netlink attribute decoders
[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(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_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
565                  const int len)
566 {
567         struct linger linger;
568
569         if (len < (int) sizeof(linger)) {
570                 printaddr(addr);
571         } else if (!umove_or_printaddr(tcp, addr, &linger)) {
572                 PRINT_FIELD_D("{", linger, l_onoff);
573                 PRINT_FIELD_D(", ", linger, l_linger);
574                 tprints("}");
575         }
576 }
577
578 static void
579 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
580                  unsigned int len)
581 {
582         struct linger linger;
583
584         if (len < sizeof(linger)) {
585                 if (len != sizeof(linger.l_onoff)) {
586                         printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
587                         return;
588                 }
589         } else {
590                 len = sizeof(linger);
591         }
592
593         if (umoven(tcp, addr, len, &linger) < 0) {
594                 printaddr(addr);
595                 return;
596         }
597
598         PRINT_FIELD_D("{", linger, l_onoff);
599         if (len == sizeof(linger))
600                 PRINT_FIELD_D(", ", linger, l_linger);
601         tprints("}");
602 }
603
604 #ifdef SO_PEERCRED
605 static void
606 print_ucred(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int len)
607 {
608         struct ucred uc;
609
610         if (len < sizeof(uc)) {
611                 if (len != sizeof(uc.pid)
612                     && len != offsetofend(struct ucred, uid)) {
613                         printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
614                         return;
615                 }
616         } else {
617                 len = sizeof(uc);
618         }
619
620         if (umoven(tcp, addr, len, &uc) < 0) {
621                 printaddr(addr);
622                 return;
623         }
624
625         PRINT_FIELD_D("{", uc, pid);
626         if (len > sizeof(uc.pid))
627                 PRINT_FIELD_UID(", ", uc, uid);
628         if (len == sizeof(uc))
629                 PRINT_FIELD_UID(", ", uc, gid);
630         tprints("}");
631 }
632 #endif /* SO_PEERCRED */
633
634 #ifdef PACKET_STATISTICS
635 static void
636 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
637                     const int len)
638 {
639         struct tpacket_stats stats;
640
641         if (len != sizeof(stats) ||
642             umove(tcp, addr, &stats) < 0) {
643                 printaddr(addr);
644         } else {
645                 PRINT_FIELD_U("{", stats, tp_packets);
646                 PRINT_FIELD_U("{", stats, tp_drops);
647                 tprints("}");
648         }
649 }
650 #endif /* PACKET_STATISTICS */
651
652 #include "xlat/icmpfilterflags.h"
653
654 static void
655 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
656 {
657         struct icmp_filter filter = {};
658
659         if (len > (int) sizeof(filter))
660                 len = sizeof(filter);
661         else if (len <= 0) {
662                 printaddr(addr);
663                 return;
664         }
665
666         if (umoven_or_printaddr(tcp, addr, len, &filter))
667                 return;
668
669         tprints("~(");
670         printflags(icmpfilterflags, ~filter.data, "ICMP_???");
671         tprints(")");
672 }
673
674 static bool
675 print_uint32(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
676 {
677         tprintf("%u", *(uint32_t *) elem_buf);
678
679         return true;
680 }
681
682 static void
683 print_getsockopt(struct tcb *const tcp, const unsigned int level,
684                  const unsigned int name, const kernel_ulong_t addr,
685                  const int ulen, const int rlen)
686 {
687         if (addr && verbose(tcp))
688         switch (level) {
689         case SOL_SOCKET:
690                 switch (name) {
691                 case SO_LINGER:
692                         print_get_linger(tcp, addr, rlen);
693                         return;
694 #ifdef SO_PEERCRED
695                 case SO_PEERCRED:
696                         print_ucred(tcp, addr, rlen);
697                         return;
698 #endif
699 #ifdef SO_ATTACH_FILTER
700                 case SO_ATTACH_FILTER:
701                         if (rlen && (unsigned short) rlen == (unsigned int) rlen)
702                                 print_sock_fprog(tcp, addr, rlen);
703                         else
704                                 printaddr(addr);
705                         return;
706 #endif /* SO_ATTACH_FILTER */
707                 }
708                 break;
709
710         case SOL_PACKET:
711                 switch (name) {
712 #ifdef PACKET_STATISTICS
713                 case PACKET_STATISTICS:
714                         print_tpacket_stats(tcp, addr, rlen);
715                         return;
716 #endif
717                 }
718                 break;
719
720         case SOL_RAW:
721                 switch (name) {
722                 case ICMP_FILTER:
723                         print_icmp_filter(tcp, addr, rlen);
724                         return;
725                 }
726                 break;
727
728         case SOL_NETLINK:
729                 if (ulen < 0 || rlen < 0) {
730                         /*
731                          * As the kernel neither accepts nor returns a negative
732                          * length, in case of successful getsockopt syscall
733                          * invocation these negative values must have come
734                          * from userspace.
735                          */
736                         printaddr(addr);
737                         return;
738                 }
739                 switch (name) {
740                 case NETLINK_LIST_MEMBERSHIPS: {
741                         uint32_t buf;
742                         print_array(tcp, addr, MIN(ulen, rlen) / sizeof(buf),
743                                     &buf, sizeof(buf),
744                                     tfetch_mem, print_uint32, 0);
745                         break;
746                         }
747                 default:
748                         printnum_int(tcp, addr, "%d");
749                         break;
750                 }
751                 return;
752         }
753
754         /* default arg printing */
755
756         if (verbose(tcp)) {
757                 if (rlen == sizeof(int)) {
758                         printnum_int(tcp, addr, "%d");
759                 } else {
760                         printstrn(tcp, addr, rlen);
761                 }
762         } else {
763                 printaddr(addr);
764         }
765 }
766
767 SYS_FUNC(getsockopt)
768 {
769         int ulen, rlen;
770
771         if (entering(tcp)) {
772                 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
773                                             tcp->u_arg[1], tcp->u_arg[2], true);
774
775                 if (verbose(tcp) && tcp->u_arg[4]
776                     && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
777                         set_tcb_priv_ulong(tcp, ulen);
778                         return 0;
779                 } else {
780                         printaddr(tcp->u_arg[3]);
781                         tprints(", ");
782                         printaddr(tcp->u_arg[4]);
783                         return RVAL_DECODED;
784                 }
785         } else {
786                 ulen = get_tcb_priv_ulong(tcp);
787
788                 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
789                         printaddr(tcp->u_arg[3]);
790                         tprintf(", [%d]", ulen);
791                 } else {
792                         print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
793                                          tcp->u_arg[3], ulen, rlen);
794                         if (ulen != rlen)
795                                 tprintf(", [%d->%d]", ulen, rlen);
796                         else
797                                 tprintf(", [%d]", rlen);
798                 }
799         }
800         return 0;
801 }
802
803 #ifdef IP_ADD_MEMBERSHIP
804 static void
805 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
806            const int len)
807 {
808         struct ip_mreq mreq;
809
810         if (len < (int) sizeof(mreq)) {
811                 printaddr(addr);
812         } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
813                 PRINT_FIELD_INET4_ADDR("{", mreq, imr_multiaddr);
814                 PRINT_FIELD_INET4_ADDR(", ", mreq, imr_interface);
815                 tprints("}");
816         }
817 }
818 #endif /* IP_ADD_MEMBERSHIP */
819
820 #ifdef IPV6_ADD_MEMBERSHIP
821 static void
822 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
823             const int len)
824 {
825         struct ipv6_mreq mreq;
826
827         if (len < (int) sizeof(mreq)) {
828                 printaddr(addr);
829         } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
830                 PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
831                 PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
832                 tprints("}");
833         }
834 }
835 #endif /* IPV6_ADD_MEMBERSHIP */
836
837 #ifdef PACKET_RX_RING
838 static void
839 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
840 {
841         struct tpacket_req req;
842
843         if (len != sizeof(req) ||
844             umove(tcp, addr, &req) < 0) {
845                 printaddr(addr);
846         } else {
847                 PRINT_FIELD_U("{", req, tp_block_size);
848                 PRINT_FIELD_U(", ", req, tp_block_nr);
849                 PRINT_FIELD_U(", ", req, tp_frame_size);
850                 PRINT_FIELD_U(", ", req, tp_frame_nr);
851                 tprints("}");
852         }
853 }
854 #endif /* PACKET_RX_RING */
855
856 #ifdef PACKET_ADD_MEMBERSHIP
857 # include "xlat/packet_mreq_type.h"
858
859 static void
860 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
861 {
862         struct packet_mreq mreq;
863
864         if (len != sizeof(mreq) ||
865             umove(tcp, addr, &mreq) < 0) {
866                 printaddr(addr);
867         } else {
868                 unsigned int i;
869
870                 PRINT_FIELD_IFINDEX("{", mreq, mr_ifindex);
871                 PRINT_FIELD_XVAL(", ", mreq, mr_type, packet_mreq_type,
872                                  "PACKET_MR_???");
873                 PRINT_FIELD_U(", ", mreq, mr_alen);
874                 tprints(", mr_address=");
875                 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
876                         mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
877                 for (i = 0; i < mreq.mr_alen; ++i)
878                         tprintf("%02x", mreq.mr_address[i]);
879                 tprints("}");
880         }
881 }
882 #endif /* PACKET_ADD_MEMBERSHIP */
883
884 static void
885 print_setsockopt(struct tcb *const tcp, const unsigned int level,
886                  const unsigned int name, const kernel_ulong_t addr,
887                  const int len)
888 {
889         if (addr && verbose(tcp))
890         switch (level) {
891         case SOL_SOCKET:
892                 switch (name) {
893                 case SO_LINGER:
894                         print_set_linger(tcp, addr, len);
895                         return;
896 #ifdef SO_ATTACH_FILTER
897                 case SO_ATTACH_FILTER:
898 # ifdef SO_ATTACH_REUSEPORT_CBPF
899                 case SO_ATTACH_REUSEPORT_CBPF:
900 # endif
901                         if ((unsigned int) len == get_sock_fprog_size())
902                                 decode_sock_fprog(tcp, addr);
903                         else
904                                 printaddr(addr);
905                         return;
906 #endif /* SO_ATTACH_FILTER */
907                 }
908                 break;
909
910         case SOL_IP:
911                 switch (name) {
912 #ifdef IP_ADD_MEMBERSHIP
913                 case IP_ADD_MEMBERSHIP:
914                 case IP_DROP_MEMBERSHIP:
915                         print_mreq(tcp, addr, len);
916                         return;
917 #endif /* IP_ADD_MEMBERSHIP */
918 #ifdef MCAST_JOIN_GROUP
919                 case MCAST_JOIN_GROUP:
920                 case MCAST_LEAVE_GROUP:
921                         print_group_req(tcp, addr, len);
922                         return;
923 #endif /* MCAST_JOIN_GROUP */
924                 }
925                 break;
926
927         case SOL_IPV6:
928                 switch (name) {
929 #ifdef IPV6_ADD_MEMBERSHIP
930                 case IPV6_ADD_MEMBERSHIP:
931                 case IPV6_DROP_MEMBERSHIP:
932 # ifdef IPV6_JOIN_ANYCAST
933                 case IPV6_JOIN_ANYCAST:
934 # endif
935 # ifdef IPV6_LEAVE_ANYCAST
936                 case IPV6_LEAVE_ANYCAST:
937 # endif
938                         print_mreq6(tcp, addr, len);
939                         return;
940 #endif /* IPV6_ADD_MEMBERSHIP */
941 #ifdef MCAST_JOIN_GROUP
942                 case MCAST_JOIN_GROUP:
943                 case MCAST_LEAVE_GROUP:
944                         print_group_req(tcp, addr, len);
945                         return;
946 #endif /* MCAST_JOIN_GROUP */
947                 }
948                 break;
949
950         case SOL_PACKET:
951                 switch (name) {
952 #ifdef PACKET_RX_RING
953                 case PACKET_RX_RING:
954 # ifdef PACKET_TX_RING
955                 case PACKET_TX_RING:
956 # endif
957                         print_tpacket_req(tcp, addr, len);
958                         return;
959 #endif /* PACKET_RX_RING */
960 #ifdef PACKET_ADD_MEMBERSHIP
961                 case PACKET_ADD_MEMBERSHIP:
962                 case PACKET_DROP_MEMBERSHIP:
963                         print_packet_mreq(tcp, addr, len);
964                         return;
965 #endif /* PACKET_ADD_MEMBERSHIP */
966                 }
967                 break;
968
969         case SOL_RAW:
970                 switch (name) {
971                 case ICMP_FILTER:
972                         print_icmp_filter(tcp, addr, len);
973                         return;
974                 }
975                 break;
976
977         case SOL_NETLINK:
978                 if (len < (int) sizeof(int))
979                         printaddr(addr);
980                 else
981                         printnum_int(tcp, addr, "%d");
982                 return;
983         }
984
985         /* default arg printing */
986
987         if (verbose(tcp)) {
988                 if (len == sizeof(int)) {
989                         printnum_int(tcp, addr, "%d");
990                 } else {
991                         printstrn(tcp, addr, len);
992                 }
993         } else {
994                 printaddr(addr);
995         }
996 }
997
998 SYS_FUNC(setsockopt)
999 {
1000         print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1001                                     tcp->u_arg[1], tcp->u_arg[2], false);
1002         print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1003                          tcp->u_arg[3], tcp->u_arg[4]);
1004         tprintf(", %d", (int) tcp->u_arg[4]);
1005
1006         return RVAL_DECODED;
1007 }