]> granicus.if.org Git - strace/blob - net.c
msghdr.c: print unrecognized struct cmsghdr.cmsg_type in hex
[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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32 #include "msghdr.h"
33 #include <sys/stat.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <sys/un.h>
37 #include <netinet/in.h>
38 #ifdef HAVE_NETINET_TCP_H
39 # include <netinet/tcp.h>
40 #endif
41 #ifdef HAVE_NETINET_UDP_H
42 # include <netinet/udp.h>
43 #endif
44 #ifdef HAVE_NETINET_SCTP_H
45 # include <netinet/sctp.h>
46 #endif
47 #include <arpa/inet.h>
48 #include <net/if.h>
49 #include <asm/types.h>
50 #ifdef HAVE_NETIPX_IPX_H
51 # include <netipx/ipx.h>
52 #else
53 # include <linux/ipx.h>
54 #endif
55
56 #if defined(HAVE_LINUX_IP_VS_H)
57 # include <linux/ip_vs.h>
58 #endif
59 #include <linux/netlink.h>
60 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
61 # include <linux/netfilter_arp/arp_tables.h>
62 #endif
63 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
64 # include <linux/netfilter_bridge/ebtables.h>
65 #endif
66 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
67 # include <linux/netfilter_ipv4/ip_tables.h>
68 #endif
69 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
70 # include <linux/netfilter_ipv6/ip6_tables.h>
71 #endif
72 #include <linux/if_packet.h>
73 #include <linux/icmp.h>
74
75 #include "xlat/socktypes.h"
76 #include "xlat/sock_type_flags.h"
77 #ifndef SOCK_TYPE_MASK
78 # define SOCK_TYPE_MASK 0xf
79 #endif
80
81 #include "xlat/socketlayers.h"
82
83 #include "xlat/inet_protocols.h"
84
85 #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
86 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
87 #endif
88 #include "xlat/netlink_protocols.h"
89
90 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
91 # include <bluetooth/bluetooth.h>
92 # include "xlat/bt_protocols.h"
93 #endif
94
95 void
96 print_ifindex(unsigned int ifindex)
97 {
98 #ifdef HAVE_IF_INDEXTONAME
99         char buf[IFNAMSIZ + 1];
100
101         if (if_indextoname(ifindex, buf)) {
102                 tprints("if_nametoindex(");
103                 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
104                 tprints(")");
105                 return;
106         }
107 #endif
108         tprintf("%u", ifindex);
109 }
110
111 /*
112  * low bits of the socket type define real socket type,
113  * other bits are socket type flags.
114  */
115 static void
116 tprint_sock_type(unsigned int flags)
117 {
118         const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
119
120         if (str) {
121                 tprints(str);
122                 flags &= ~SOCK_TYPE_MASK;
123                 if (!flags)
124                         return;
125                 tprints("|");
126         }
127         printflags(sock_type_flags, flags, "SOCK_???");
128 }
129
130 SYS_FUNC(socket)
131 {
132         printxval(addrfams, tcp->u_arg[0], "AF_???");
133         tprints(", ");
134         tprint_sock_type(tcp->u_arg[1]);
135         tprints(", ");
136         switch (tcp->u_arg[0]) {
137         case AF_INET:
138         case AF_INET6:
139                 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
140                 break;
141
142         case AF_NETLINK:
143                 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
144                 break;
145
146 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
147         case AF_BLUETOOTH:
148                 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
149                 break;
150 #endif
151
152         default:
153                 tprintf("%lu", tcp->u_arg[2]);
154                 break;
155         }
156
157         return RVAL_DECODED | RVAL_FD;
158 }
159
160 SYS_FUNC(bind)
161 {
162         printfd(tcp, tcp->u_arg[0]);
163         tprints(", ");
164         decode_sockaddr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
165         tprintf(", %lu", tcp->u_arg[2]);
166
167         return RVAL_DECODED;
168 }
169
170 SYS_FUNC(listen)
171 {
172         printfd(tcp, tcp->u_arg[0]);
173         tprints(", ");
174         tprintf("%lu", tcp->u_arg[1]);
175
176         return RVAL_DECODED;
177 }
178
179 static int
180 do_sockname(struct tcb *tcp, int flags_arg)
181 {
182         if (entering(tcp)) {
183                 printfd(tcp, tcp->u_arg[0]);
184                 tprints(", ");
185                 return 0;
186         }
187
188         int len;
189         if (!tcp->u_arg[2] || !verbose(tcp) || syserror(tcp) ||
190             umove(tcp, tcp->u_arg[2], &len) < 0) {
191                 printaddr(tcp->u_arg[1]);
192                 tprints(", ");
193                 printaddr(tcp->u_arg[2]);
194         } else {
195                 decode_sockaddr(tcp, tcp->u_arg[1], len);
196                 tprintf(", [%d]", len);
197         }
198
199         if (flags_arg >= 0) {
200                 tprints(", ");
201                 printflags(sock_type_flags, tcp->u_arg[flags_arg],
202                            "SOCK_???");
203         }
204         return 0;
205 }
206
207 SYS_FUNC(accept)
208 {
209         do_sockname(tcp, -1);
210         return RVAL_FD;
211 }
212
213 SYS_FUNC(accept4)
214 {
215         do_sockname(tcp, 3);
216         return RVAL_FD;
217 }
218
219 SYS_FUNC(send)
220 {
221         printfd(tcp, tcp->u_arg[0]);
222         tprints(", ");
223         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
224         tprintf(", %lu, ", tcp->u_arg[2]);
225         /* flags */
226         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
227
228         return RVAL_DECODED;
229 }
230
231 SYS_FUNC(sendto)
232 {
233         printfd(tcp, tcp->u_arg[0]);
234         tprints(", ");
235         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
236         tprintf(", %lu, ", tcp->u_arg[2]);
237         /* flags */
238         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
239         /* to address */
240         tprints(", ");
241         decode_sockaddr(tcp, tcp->u_arg[4], tcp->u_arg[5]);
242         /* to length */
243         tprintf(", %lu", tcp->u_arg[5]);
244
245         return RVAL_DECODED;
246 }
247
248 SYS_FUNC(sendmsg)
249 {
250         printfd(tcp, tcp->u_arg[0]);
251         tprints(", ");
252         decode_msghdr(tcp, tcp->u_arg[1], (unsigned long) -1L);
253         /* flags */
254         tprints(", ");
255         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
256
257         return RVAL_DECODED;
258 }
259
260 SYS_FUNC(sendmmsg)
261 {
262         if (entering(tcp)) {
263                 /* sockfd */
264                 printfd(tcp, tcp->u_arg[0]);
265                 tprints(", ");
266                 if (!verbose(tcp)) {
267                         printaddr(tcp->u_arg[1]);
268                         /* vlen */
269                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
270                         /* flags */
271                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
272                         return RVAL_DECODED;
273                 }
274         } else {
275                 decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_rval, false);
276                 /* vlen */
277                 tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
278                 /* flags */
279                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
280         }
281         return 0;
282 }
283
284 SYS_FUNC(recv)
285 {
286         if (entering(tcp)) {
287                 printfd(tcp, tcp->u_arg[0]);
288                 tprints(", ");
289         } else {
290                 if (syserror(tcp))
291                         printaddr(tcp->u_arg[1]);
292                 else
293                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
294
295                 tprintf(", %lu, ", tcp->u_arg[2]);
296                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
297         }
298         return 0;
299 }
300
301 SYS_FUNC(recvfrom)
302 {
303         int fromlen;
304
305         if (entering(tcp)) {
306                 printfd(tcp, tcp->u_arg[0]);
307                 tprints(", ");
308         } else {
309                 /* buf */
310                 if (syserror(tcp)) {
311                         printaddr(tcp->u_arg[1]);
312                 } else {
313                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
314                 }
315                 /* len */
316                 tprintf(", %lu, ", tcp->u_arg[2]);
317                 /* flags */
318                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
319                 tprints(", ");
320                 if (syserror(tcp) || !tcp->u_arg[4] || !tcp->u_arg[5] ||
321                     umove(tcp, tcp->u_arg[5], &fromlen) < 0) {
322                         /* from address, len */
323                         printaddr(tcp->u_arg[4]);
324                         tprints(", ");
325                         printaddr(tcp->u_arg[5]);
326                         return 0;
327                 }
328                 /* from address */
329                 decode_sockaddr(tcp, tcp->u_arg[4], fromlen);
330                 /* from length */
331                 tprintf(", [%u]", fromlen);
332         }
333         return 0;
334 }
335
336 SYS_FUNC(recvmsg)
337 {
338         if (entering(tcp)) {
339                 printfd(tcp, tcp->u_arg[0]);
340                 tprints(", ");
341         } else {
342                 if (syserror(tcp))
343                         printaddr(tcp->u_arg[1]);
344                 else
345                         decode_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
346                 /* flags */
347                 tprints(", ");
348                 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
349         }
350         return 0;
351 }
352
353 SYS_FUNC(recvmmsg)
354 {
355         static char str[sizeof("left") + TIMESPEC_TEXT_BUFSIZE];
356
357         if (entering(tcp)) {
358                 printfd(tcp, tcp->u_arg[0]);
359                 tprints(", ");
360                 if (verbose(tcp)) {
361                         /* Abusing tcp->auxstr as temp storage.
362                          * Will be used and cleared on syscall exit.
363                          */
364                         tcp->auxstr = sprint_timespec(tcp, tcp->u_arg[4]);
365                 } else {
366                         printaddr(tcp->u_arg[1]);
367                         /* vlen */
368                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
369                         /* flags */
370                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
371                         tprints(", ");
372                         print_timespec(tcp, tcp->u_arg[4]);
373                 }
374                 return 0;
375         } else {
376                 if (verbose(tcp)) {
377                         decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_rval, true);
378                         /* vlen */
379                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
380                         /* flags */
381                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
382                         tprints(", ");
383                         /* timeout on entrance */
384                         tprints(tcp->auxstr);
385                         tcp->auxstr = NULL;
386                 }
387                 if (syserror(tcp))
388                         return 0;
389                 if (tcp->u_rval == 0) {
390                         tcp->auxstr = "Timeout";
391                         return RVAL_STR;
392                 }
393                 if (!verbose(tcp))
394                         return 0;
395                 /* timeout on exit */
396                 snprintf(str, sizeof(str), "left %s",
397                          sprint_timespec(tcp, tcp->u_arg[4]));
398                 tcp->auxstr = str;
399                 return RVAL_STR;
400         }
401 }
402
403 #include "xlat/shutdown_modes.h"
404
405 SYS_FUNC(shutdown)
406 {
407         printfd(tcp, tcp->u_arg[0]);
408         tprints(", ");
409         printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
410
411         return RVAL_DECODED;
412 }
413
414 SYS_FUNC(getsockname)
415 {
416         return do_sockname(tcp, -1);
417 }
418
419 static void
420 printpair_fd(struct tcb *tcp, const int i0, const int i1)
421 {
422         tprints("[");
423         printfd(tcp, i0);
424         tprints(", ");
425         printfd(tcp, i1);
426         tprints("]");
427 }
428
429 static void
430 decode_pair_fd(struct tcb *tcp, const long addr)
431 {
432         int pair[2];
433
434         if (umove_or_printaddr(tcp, addr, &pair))
435                 return;
436
437         printpair_fd(tcp, pair[0], pair[1]);
438 }
439
440 static int
441 do_pipe(struct tcb *tcp, int flags_arg)
442 {
443         if (exiting(tcp)) {
444                 decode_pair_fd(tcp, tcp->u_arg[0]);
445                 if (flags_arg >= 0) {
446                         tprints(", ");
447                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
448                 }
449         }
450         return 0;
451 }
452
453 SYS_FUNC(pipe)
454 {
455 #ifdef HAVE_GETRVAL2
456         if (exiting(tcp) && !syserror(tcp))
457                 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
458         return 0;
459 #else
460         return do_pipe(tcp, -1);
461 #endif
462 }
463
464 SYS_FUNC(pipe2)
465 {
466         return do_pipe(tcp, 1);
467 }
468
469 SYS_FUNC(socketpair)
470 {
471         if (entering(tcp)) {
472                 printxval(addrfams, tcp->u_arg[0], "AF_???");
473                 tprints(", ");
474                 tprint_sock_type(tcp->u_arg[1]);
475                 tprintf(", %lu", tcp->u_arg[2]);
476         } else {
477                 tprints(", ");
478                 decode_pair_fd(tcp, tcp->u_arg[3]);
479         }
480         return 0;
481 }
482
483 #include "xlat/sockoptions.h"
484 #include "xlat/sockipoptions.h"
485 #include "xlat/getsockipoptions.h"
486 #include "xlat/setsockipoptions.h"
487 #include "xlat/sockipv6options.h"
488 #include "xlat/getsockipv6options.h"
489 #include "xlat/setsockipv6options.h"
490 #include "xlat/sockipxoptions.h"
491 #include "xlat/sockrawoptions.h"
492 #include "xlat/sockpacketoptions.h"
493 #include "xlat/socksctpoptions.h"
494 #include "xlat/socktcpoptions.h"
495
496 static void
497 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
498                             unsigned int name, bool is_getsockopt)
499 {
500         printfd(tcp, fd);
501         tprints(", ");
502         printxval(socketlayers, level, "SOL_??");
503         tprints(", ");
504
505         switch (level) {
506         case SOL_SOCKET:
507                 printxval(sockoptions, name, "SO_???");
508                 break;
509         case SOL_IP:
510                 printxvals(name, "IP_???", sockipoptions,
511                         is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
512                 break;
513         case SOL_IPV6:
514                 printxvals(name, "IPV6_???", sockipv6options,
515                         is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
516                 break;
517         case SOL_IPX:
518                 printxval(sockipxoptions, name, "IPX_???");
519                 break;
520         case SOL_PACKET:
521                 printxval(sockpacketoptions, name, "PACKET_???");
522                 break;
523         case SOL_TCP:
524                 printxval(socktcpoptions, name, "TCP_???");
525                 break;
526         case SOL_SCTP:
527                 printxval(socksctpoptions, name, "SCTP_???");
528                 break;
529         case SOL_RAW:
530                 printxval(sockrawoptions, name, "RAW_???");
531                 break;
532
533                 /* Other SOL_* protocol levels still need work. */
534
535         default:
536                 tprintf("%u", name);
537         }
538
539         tprints(", ");
540 }
541
542 static void
543 print_linger(struct tcb *tcp, long addr, int len)
544 {
545         struct linger linger;
546
547         if (len != sizeof(linger) ||
548             umove(tcp, addr, &linger) < 0) {
549                 printaddr(addr);
550                 return;
551         }
552
553         tprintf("{onoff=%d, linger=%d}",
554                 linger.l_onoff,
555                 linger.l_linger);
556 }
557
558 #ifdef SO_PEERCRED
559 static void
560 print_ucred(struct tcb *tcp, long addr, int len)
561 {
562         struct ucred uc;
563
564         if (len != sizeof(uc) ||
565             umove(tcp, addr, &uc) < 0) {
566                 printaddr(addr);
567         } else {
568                 tprintf("{pid=%u, uid=%u, gid=%u}",
569                         (unsigned) uc.pid,
570                         (unsigned) uc.uid,
571                         (unsigned) uc.gid);
572         }
573 }
574 #endif /* SO_PEERCRED */
575
576 #ifdef PACKET_STATISTICS
577 static void
578 print_tpacket_stats(struct tcb *tcp, long addr, int len)
579 {
580         struct tpacket_stats stats;
581
582         if (len != sizeof(stats) ||
583             umove(tcp, addr, &stats) < 0) {
584                 printaddr(addr);
585         } else {
586                 tprintf("{packets=%u, drops=%u}",
587                         stats.tp_packets,
588                         stats.tp_drops);
589         }
590 }
591 #endif /* PACKET_STATISTICS */
592
593 #include "xlat/icmpfilterflags.h"
594
595 static void
596 print_icmp_filter(struct tcb *tcp, const long addr, int len)
597 {
598         struct icmp_filter filter = {};
599
600         if (len > (int) sizeof(filter))
601                 len = sizeof(filter);
602         else if (len <= 0) {
603                 printaddr(addr);
604                 return;
605         }
606
607         if (umoven_or_printaddr(tcp, addr, len, &filter))
608                 return;
609
610         tprints("~(");
611         printflags(icmpfilterflags, ~filter.data, "ICMP_???");
612         tprints(")");
613 }
614
615 static void
616 print_getsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
617                  long addr, int len)
618 {
619         if (addr && verbose(tcp))
620         switch (level) {
621         case SOL_SOCKET:
622                 switch (name) {
623                 case SO_LINGER:
624                         print_linger(tcp, addr, len);
625                         goto done;
626 #ifdef SO_PEERCRED
627                 case SO_PEERCRED:
628                         print_ucred(tcp, addr, len);
629                         goto done;
630 #endif
631                 }
632                 break;
633
634         case SOL_PACKET:
635                 switch (name) {
636 #ifdef PACKET_STATISTICS
637                 case PACKET_STATISTICS:
638                         print_tpacket_stats(tcp, addr, len);
639                         goto done;
640 #endif
641                 }
642                 break;
643
644         case SOL_RAW:
645                 switch (name) {
646                 case ICMP_FILTER:
647                         print_icmp_filter(tcp, addr, len);
648                         goto done;
649                 }
650                 break;
651         }
652
653         /* default arg printing */
654
655         if (verbose(tcp)) {
656                 if (len == sizeof(int)) {
657                         printnum_int(tcp, addr, "%d");
658                 } else {
659                         printstr(tcp, addr, len);
660                 }
661         } else {
662                 printaddr(addr);
663         }
664 done:
665         tprintf(", [%d]", len);
666 }
667
668 SYS_FUNC(getsockopt)
669 {
670         if (entering(tcp)) {
671                 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
672                                             tcp->u_arg[1], tcp->u_arg[2], true);
673         } else {
674                 int len;
675
676                 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
677                         printaddr(tcp->u_arg[3]);
678                         tprints(", ");
679                         printaddr(tcp->u_arg[4]);
680                 } else {
681                         print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
682                                          tcp->u_arg[3], len);
683                 }
684         }
685         return 0;
686 }
687
688 #ifdef IP_ADD_MEMBERSHIP
689 static void
690 print_mreq(struct tcb *tcp, long addr, unsigned int len)
691 {
692         struct ip_mreq mreq;
693
694         if (len < sizeof(mreq)) {
695                 printstr(tcp, addr, len);
696                 return;
697         }
698         if (umove_or_printaddr(tcp, addr, &mreq))
699                 return;
700
701         tprints("{imr_multiaddr=inet_addr(");
702         print_quoted_string(inet_ntoa(mreq.imr_multiaddr),
703                             16, QUOTE_0_TERMINATED);
704         tprints("), imr_interface=inet_addr(");
705         print_quoted_string(inet_ntoa(mreq.imr_interface),
706                             16, QUOTE_0_TERMINATED);
707         tprints(")}");
708 }
709 #endif /* IP_ADD_MEMBERSHIP */
710
711 #ifdef IPV6_ADD_MEMBERSHIP
712 static void
713 print_mreq6(struct tcb *tcp, long addr, unsigned int len)
714 {
715         struct ipv6_mreq mreq;
716
717         if (len < sizeof(mreq))
718                 goto fail;
719
720         if (umove_or_printaddr(tcp, addr, &mreq))
721                 return;
722
723         const struct in6_addr *in6 = &mreq.ipv6mr_multiaddr;
724         char address[INET6_ADDRSTRLEN];
725
726         if (!inet_ntop(AF_INET6, in6, address, sizeof(address)))
727                 goto fail;
728
729         tprints("{ipv6mr_multiaddr=inet_pton(");
730         print_quoted_string(address, sizeof(address), QUOTE_0_TERMINATED);
731         tprints("), ipv6mr_interface=");
732         print_ifindex(mreq.ipv6mr_interface);
733         tprints("}");
734         return;
735
736 fail:
737         printstr(tcp, addr, len);
738 }
739 #endif /* IPV6_ADD_MEMBERSHIP */
740
741 #ifdef MCAST_JOIN_GROUP
742 static void
743 print_group_req(struct tcb *tcp, long addr, int len)
744 {
745         struct group_req greq;
746
747         if (len != sizeof(greq) ||
748             umove(tcp, addr, &greq) < 0) {
749                 printaddr(addr);
750                 return;
751         }
752
753         tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
754         print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
755         tprintf("}");
756
757 }
758 #endif /* MCAST_JOIN_GROUP */
759
760 #ifdef PACKET_RX_RING
761 static void
762 print_tpacket_req(struct tcb *tcp, long addr, int len)
763 {
764         struct tpacket_req req;
765
766         if (len != sizeof(req) ||
767             umove(tcp, addr, &req) < 0) {
768                 printaddr(addr);
769         } else {
770                 tprintf("{block_size=%u, block_nr=%u, "
771                         "frame_size=%u, frame_nr=%u}",
772                         req.tp_block_size,
773                         req.tp_block_nr,
774                         req.tp_frame_size,
775                         req.tp_frame_nr);
776         }
777 }
778 #endif /* PACKET_RX_RING */
779
780 #ifdef PACKET_ADD_MEMBERSHIP
781 # include "xlat/packet_mreq_type.h"
782
783 static void
784 print_packet_mreq(struct tcb *tcp, long addr, int len)
785 {
786         struct packet_mreq mreq;
787
788         if (len != sizeof(mreq) ||
789             umove(tcp, addr, &mreq) < 0) {
790                 printaddr(addr);
791         } else {
792                 unsigned int i;
793
794                 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
795                 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
796                 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
797                 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
798                         mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
799                 for (i = 0; i < mreq.mr_alen; ++i)
800                         tprintf("%02x", mreq.mr_address[i]);
801                 tprints("}");
802         }
803 }
804 #endif /* PACKET_ADD_MEMBERSHIP */
805
806 static void
807 print_setsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
808                  long addr, int len)
809 {
810         if (addr && verbose(tcp))
811         switch (level) {
812         case SOL_SOCKET:
813                 switch (name) {
814                 case SO_LINGER:
815                         print_linger(tcp, addr, len);
816                         goto done;
817                 }
818                 break;
819
820         case SOL_IP:
821                 switch (name) {
822 #ifdef IP_ADD_MEMBERSHIP
823                 case IP_ADD_MEMBERSHIP:
824                 case IP_DROP_MEMBERSHIP:
825                         print_mreq(tcp, addr, len);
826                         goto done;
827 #endif /* IP_ADD_MEMBERSHIP */
828 #ifdef MCAST_JOIN_GROUP
829                 case MCAST_JOIN_GROUP:
830                 case MCAST_LEAVE_GROUP:
831                         print_group_req(tcp, addr, len);
832                         goto done;
833 #endif /* MCAST_JOIN_GROUP */
834                 }
835                 break;
836
837         case SOL_IPV6:
838                 switch (name) {
839 #ifdef IPV6_ADD_MEMBERSHIP
840                 case IPV6_ADD_MEMBERSHIP:
841                 case IPV6_DROP_MEMBERSHIP:
842 # ifdef IPV6_JOIN_ANYCAST
843                 case IPV6_JOIN_ANYCAST:
844 # endif
845 # ifdef IPV6_LEAVE_ANYCAST
846                 case IPV6_LEAVE_ANYCAST:
847 # endif
848                         print_mreq6(tcp, addr, len);
849                         goto done;
850 #endif /* IPV6_ADD_MEMBERSHIP */
851                 }
852                 break;
853
854         case SOL_PACKET:
855                 switch (name) {
856 #ifdef PACKET_RX_RING
857                 case PACKET_RX_RING:
858 # ifdef PACKET_TX_RING
859                 case PACKET_TX_RING:
860 # endif
861                         print_tpacket_req(tcp, addr, len);
862                         goto done;
863 #endif /* PACKET_RX_RING */
864 #ifdef PACKET_ADD_MEMBERSHIP
865                 case PACKET_ADD_MEMBERSHIP:
866                 case PACKET_DROP_MEMBERSHIP:
867                         print_packet_mreq(tcp, addr, len);
868                         goto done;
869 #endif /* PACKET_ADD_MEMBERSHIP */
870                 }
871                 break;
872
873         case SOL_RAW:
874                 switch (name) {
875                 case ICMP_FILTER:
876                         print_icmp_filter(tcp, addr, len);
877                         goto done;
878                 }
879                 break;
880         }
881
882         /* default arg printing */
883
884         if (verbose(tcp)) {
885                 if (len == sizeof(int)) {
886                         printnum_int(tcp, addr, "%d");
887                 } else {
888                         printstr(tcp, addr, len);
889                 }
890         } else {
891                 printaddr(addr);
892         }
893 done:
894         tprintf(", %d", len);
895 }
896
897 SYS_FUNC(setsockopt)
898 {
899         print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
900                                     tcp->u_arg[1], tcp->u_arg[2], false);
901         print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
902                          tcp->u_arg[3], tcp->u_arg[4]);
903
904         return RVAL_DECODED;
905 }