]> granicus.if.org Git - strace/blob - net.c
net.c: skip details of too short socket addresses
[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 <sys/stat.h>
33 #include <sys/socket.h>
34 #include <sys/uio.h>
35 #include <sys/un.h>
36 #include <netinet/in.h>
37 #ifdef HAVE_NETINET_TCP_H
38 # include <netinet/tcp.h>
39 #endif
40 #ifdef HAVE_NETINET_UDP_H
41 # include <netinet/udp.h>
42 #endif
43 #ifdef HAVE_NETINET_SCTP_H
44 # include <netinet/sctp.h>
45 #endif
46 #include <arpa/inet.h>
47 #include <net/if.h>
48 #include <asm/types.h>
49 #ifdef HAVE_NETIPX_IPX_H
50 # include <netipx/ipx.h>
51 #else
52 # include <linux/ipx.h>
53 #endif
54
55 #if defined(HAVE_LINUX_IP_VS_H)
56 # include <linux/ip_vs.h>
57 #endif
58 #include <linux/netlink.h>
59 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
60 # include <linux/netfilter_arp/arp_tables.h>
61 #endif
62 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
63 # include <linux/netfilter_bridge/ebtables.h>
64 #endif
65 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
66 # include <linux/netfilter_ipv4/ip_tables.h>
67 #endif
68 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
69 # include <linux/netfilter_ipv6/ip6_tables.h>
70 #endif
71 #include <linux/if_packet.h>
72 #include <linux/icmp.h>
73 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
74 # include <bluetooth/bluetooth.h>
75 # include <bluetooth/hci.h>
76 # include <bluetooth/l2cap.h>
77 # include <bluetooth/rfcomm.h>
78 # include <bluetooth/sco.h>
79 #endif
80
81 #include "xlat/addrfams.h"
82 #include "xlat/socktypes.h"
83 #include "xlat/sock_type_flags.h"
84 #ifndef SOCK_TYPE_MASK
85 # define SOCK_TYPE_MASK 0xf
86 #endif
87
88 #include "xlat/socketlayers.h"
89
90 #include "xlat/inet_protocols.h"
91
92 #if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
93 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
94 #endif
95 #include "xlat/netlink_protocols.h"
96
97 #if defined(HAVE_BLUETOOTH_BLUETOOTH_H)
98 # include "xlat/bt_protocols.h"
99 #endif
100
101 #include "xlat/msg_flags.h"
102
103 #include "xlat/af_packet_types.h"
104
105 static void
106 print_sockaddr_data_un(const void *const buf, const int addrlen)
107 {
108         const struct sockaddr_un *const sa_un = buf;
109
110         tprints("sun_path=");
111         if (sa_un->sun_path[0]) {
112                 print_quoted_string(sa_un->sun_path,
113                                     sizeof(sa_un->sun_path) + 1,
114                                     QUOTE_0_TERMINATED);
115         } else {
116                 tprints("@");
117                 print_quoted_string(sa_un->sun_path + 1,
118                                     sizeof(sa_un->sun_path),
119                                     QUOTE_0_TERMINATED);
120         }
121 }
122
123 static void
124 print_sockaddr_data_in(const void *const buf, const int addrlen)
125 {
126         const struct sockaddr_in *const sa_in = buf;
127
128         tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")",
129                 ntohs(sa_in->sin_port), inet_ntoa(sa_in->sin_addr));
130 }
131
132 static void
133 print_ifindex(unsigned int ifindex)
134 {
135 #ifdef HAVE_IF_INDEXTONAME
136         char buf[IFNAMSIZ + 1];
137
138         if (if_indextoname(ifindex, buf)) {
139                 tprints("if_nametoindex(");
140                 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
141                 tprints(")");
142                 return;
143         }
144 #endif
145         tprintf("%u", ifindex);
146 }
147
148 static void
149 print_sockaddr_data_in6(const void *const buf, const int addrlen)
150 {
151         const struct sockaddr_in6 *const sa_in6 = buf;
152
153         char string_addr[100];
154         inet_ntop(AF_INET6, &sa_in6->sin6_addr,
155                   string_addr, sizeof(string_addr));
156         tprintf("sin6_port=htons(%u), inet_pton(AF_INET6"
157                 ", \"%s\", &sin6_addr), sin6_flowinfo=%u",
158                 ntohs(sa_in6->sin6_port), string_addr,
159                 sa_in6->sin6_flowinfo);
160         tprints(", sin6_scope_id=");
161 #if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
162         if (IN6_IS_ADDR_LINKLOCAL(&sa_in6->sin6_addr)
163             || IN6_IS_ADDR_MC_LINKLOCAL(&sa_in6->sin6_addr))
164                 print_ifindex(sa_in6->sin6_scope_id);
165         else
166 #endif
167                 tprintf("%u", sa_in6->sin6_scope_id);
168 }
169
170 static void
171 print_sockaddr_data_ipx(const void *const buf, const int addrlen)
172 {
173         const struct sockaddr_ipx *const sa_ipx = buf;
174         unsigned int i;
175
176         tprintf("sipx_port=htons(%u)"
177                 ", sipx_network=htonl(%08x)"
178                 ", sipx_node=[",
179                 ntohs(sa_ipx->sipx_port),
180                 ntohl(sa_ipx->sipx_network));
181         for (i = 0; i < IPX_NODE_LEN; ++i) {
182                 tprintf("%s%02x", i ? ", " : "",
183                         sa_ipx->sipx_node[i]);
184         }
185         tprintf("], sipx_type=%02x", sa_ipx->sipx_type);
186 }
187
188 static void
189 print_sockaddr_data_nl(const void *const buf, const int addrlen)
190 {
191         const struct sockaddr_nl *const sa_nl = buf;
192
193         tprintf("pid=%d, groups=%08x", sa_nl->nl_pid, sa_nl->nl_groups);
194 }
195
196 static void
197 print_sockaddr_data_ll(const void *const buf, const int addrlen)
198 {
199         const struct sockaddr_ll *const sa_ll = buf;
200         unsigned int i;
201
202         tprintf("proto=%#04x, if%d, pkttype=",
203                 ntohs(sa_ll->sll_protocol),
204                 sa_ll->sll_ifindex);
205         printxval(af_packet_types, sa_ll->sll_pkttype, "PACKET_???");
206         tprintf(", addr(%d)={%d, ", sa_ll->sll_halen, sa_ll->sll_hatype);
207         for (i = 0; i < sa_ll->sll_halen; i++)
208                 tprintf("%02x", sa_ll->sll_addr[i]);
209 }
210
211 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
212 static void
213 print_sockaddr_data_bt(const void *const buf, const int addrlen)
214 {
215         const union {
216                 struct sockaddr_hci hci;
217                 struct sockaddr_l2 l2;
218                 struct sockaddr_rc rc;
219                 struct sockaddr_sco sco;
220         } *const addr = buf;
221
222         tprintf("{sco_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X} or "
223                 "{rc_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, rc_channel=%d} or "
224                 "{l2_psm=htobs(%d), l2_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, l2_cid=htobs(%d)} or "
225                 "{hci_dev=htobs(%d)}",
226                 addr->sco.sco_bdaddr.b[0], addr->sco.sco_bdaddr.b[1],
227                 addr->sco.sco_bdaddr.b[2], addr->sco.sco_bdaddr.b[3],
228                 addr->sco.sco_bdaddr.b[4], addr->sco.sco_bdaddr.b[5],
229                 addr->rc.rc_bdaddr.b[0], addr->rc.rc_bdaddr.b[1],
230                 addr->rc.rc_bdaddr.b[2], addr->rc.rc_bdaddr.b[3],
231                 addr->rc.rc_bdaddr.b[4], addr->rc.rc_bdaddr.b[5],
232                 addr->rc.rc_channel,
233                 btohs(addr->l2.l2_psm), addr->l2.l2_bdaddr.b[0],
234                 addr->l2.l2_bdaddr.b[1], addr->l2.l2_bdaddr.b[2],
235                 addr->l2.l2_bdaddr.b[3], addr->l2.l2_bdaddr.b[4],
236                 addr->l2.l2_bdaddr.b[5], btohs(addr->l2.l2_cid),
237                 btohs(addr->hci.hci_dev));
238 }
239 #endif /* HAVE_BLUETOOTH_BLUETOOTH_H */
240
241 typedef void (* const sockaddr_printer)(const void *const, const int);
242
243 static const sockaddr_printer sa_printers[] = {
244         [AF_UNIX] = print_sockaddr_data_un,
245         [AF_INET] = print_sockaddr_data_in,
246         [AF_IPX] = print_sockaddr_data_ipx,
247         [AF_INET6] = print_sockaddr_data_in6,
248         [AF_NETLINK] = print_sockaddr_data_nl,
249         [AF_PACKET] = print_sockaddr_data_ll,
250 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
251         [AF_BLUETOOTH] = print_sockaddr_data_bt,
252 #endif
253 };
254
255 void
256 print_sockaddr(struct tcb *tcp, const void *const buf, const int addrlen)
257 {
258         const struct sockaddr *const sa = buf;
259
260         tprints("{sa_family=");
261         printxval(addrfams, sa->sa_family, "AF_???");
262
263         if (addrlen > (int) sizeof(sa->sa_family)) {
264                 tprints(", ");
265
266                 if (sa->sa_family < ARRAY_SIZE(sa_printers)
267                     && sa_printers[sa->sa_family]) {
268                         sa_printers[sa->sa_family](buf, addrlen);
269                 } else {
270                         tprints("sa_data=");
271                         print_quoted_string(sa->sa_data, sizeof(sa->sa_data), 0);
272                 }
273         }
274
275         tprints("}");
276 }
277
278 int
279 printsock(struct tcb *tcp, long addr, int addrlen)
280 {
281         if (addrlen < 2) {
282                 printaddr(addr);
283                 return -1;
284         }
285
286         union {
287                 struct sockaddr sa;
288                 struct sockaddr_storage storage;
289                 char pad[sizeof(struct sockaddr_storage) + 1];
290         } addrbuf;
291
292         if ((unsigned) addrlen > sizeof(addrbuf.storage))
293                 addrlen = sizeof(addrbuf.storage);
294
295         if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
296                 return -1;
297
298         memset(&addrbuf.pad[addrlen], 0, sizeof(addrbuf.pad) - addrlen);
299
300         print_sockaddr(tcp, &addrbuf, addrlen);
301
302         return addrbuf.sa.sa_family;
303 }
304
305 #include "xlat/scmvals.h"
306 #include "xlat/ip_cmsg_types.h"
307
308 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
309 struct cmsghdr32 {
310         uint32_t cmsg_len;
311         int cmsg_level;
312         int cmsg_type;
313 };
314 #endif
315
316 typedef union {
317         char *ptr;
318         struct cmsghdr *cmsg;
319 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
320         struct cmsghdr32 *cmsg32;
321 #endif
322 } union_cmsghdr;
323
324 static void
325 print_scm_rights(struct tcb *tcp, const void *cmsg_data,
326                  const size_t data_len)
327 {
328         const int *fds = cmsg_data;
329         const char *end = (const char *) cmsg_data + data_len;
330         bool seen = false;
331
332         if (sizeof(*fds) > data_len)
333                 return;
334
335         tprints(", [");
336         while ((const char *) fds < end) {
337                 if (seen)
338                         tprints(", ");
339                 else
340                         seen = true;
341                 printfd(tcp, *fds++);
342         }
343         tprints("]");
344 }
345
346 static void
347 print_scm_creds(struct tcb *tcp, const void *cmsg_data,
348                 const size_t data_len)
349 {
350         const struct ucred *uc = cmsg_data;
351
352         if (sizeof(*uc) > data_len)
353                 return;
354
355         tprintf(", {pid=%u, uid=%u, gid=%u}",
356                 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
357 }
358
359 static void
360 print_scm_security(struct tcb *tcp, const void *cmsg_data,
361                    const size_t data_len)
362 {
363         if (!data_len)
364                 return;
365
366         tprints(", ");
367         print_quoted_string(cmsg_data, data_len, 0);
368 }
369
370 static void
371 print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
372                       const size_t data_len)
373 {
374         const struct in_pktinfo *info = cmsg_data;
375
376         if (sizeof(*info) > data_len)
377                 return;
378
379         tprints(", {ipi_ifindex=");
380         print_ifindex(info->ipi_ifindex);
381         tprintf(", ipi_spec_dst=inet_addr(\"%s\"), ipi_addr=inet_addr(\"%s\")}",
382                 inet_ntoa(info->ipi_spec_dst), inet_ntoa(info->ipi_addr));
383 }
384
385 static void
386 print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
387                   const size_t data_len)
388 {
389         const unsigned int *ttl = cmsg_data;
390
391         if (sizeof(*ttl) > data_len)
392                 return;
393
394         tprintf(", {ttl=%u}", *ttl);
395 }
396
397 static void
398 print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
399                   const size_t data_len)
400 {
401         const uint8_t *tos = cmsg_data;
402
403         if (sizeof(*tos) > data_len)
404                 return;
405
406         tprintf(", {tos=%x}", *tos);
407 }
408
409 static void
410 print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
411                        const size_t data_len)
412 {
413         const uint32_t *csum = cmsg_data;
414
415         if (sizeof(*csum) > data_len)
416                 return;
417
418         tprintf(", {csum=%u}", *csum);
419 }
420
421 static void
422 print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
423                    const size_t data_len)
424 {
425         const unsigned char *opts = cmsg_data;
426         size_t i;
427
428         if (!data_len)
429                 return;
430
431         tprints(", {opts=0x");
432         for (i = 0; i < data_len; ++i)
433                 tprintf("%02x", opts[i]);
434         tprints("}");
435 }
436
437 static void
438 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
439                       const size_t data_len)
440 {
441         const struct {
442                 uint32_t ee_errno;
443                 uint8_t  ee_origin;
444                 uint8_t  ee_type;
445                 uint8_t  ee_code;
446                 uint8_t  ee_pad;
447                 uint32_t ee_info;
448                 uint32_t ee_data;
449                 struct sockaddr_in offender;
450         } *err = cmsg_data;
451
452         if (sizeof(*err) > data_len)
453                 return;
454
455         tprintf(", {ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
456                 ", ee_info=%u, ee_data=%u, offender=",
457                 err->ee_errno, err->ee_origin, err->ee_type,
458                 err->ee_code, err->ee_info, err->ee_data);
459         print_sockaddr(tcp, &err->offender, sizeof(err->offender));
460         tprints("}");
461 }
462
463 static void
464 print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
465                           const size_t data_len)
466 {
467         if (sizeof(struct sockaddr_in) > data_len)
468                 return;
469
470         tprints(", ");
471         print_sockaddr(tcp, cmsg_data, data_len);
472 }
473
474 static void
475 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
476                      const void *cmsg_data, const size_t data_len)
477 {
478         switch (cmsg_level) {
479         case SOL_SOCKET:
480                 printxval(scmvals, cmsg_type, "SCM_???");
481                 switch (cmsg_type) {
482                 case SCM_RIGHTS:
483                         print_scm_rights(tcp, cmsg_data, data_len);
484                         break;
485                 case SCM_CREDENTIALS:
486                         print_scm_creds(tcp, cmsg_data, data_len);
487                         break;
488                 case SCM_SECURITY:
489                         print_scm_security(tcp, cmsg_data, data_len);
490                         break;
491                 }
492                 break;
493         case SOL_IP:
494                 printxval(ip_cmsg_types, cmsg_type, "IP_???");
495                 switch (cmsg_type) {
496                 case IP_PKTINFO:
497                         print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
498                         break;
499                 case IP_TTL:
500                         print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
501                         break;
502                 case IP_TOS:
503                         print_cmsg_ip_tos(tcp, cmsg_data, data_len);
504                         break;
505                 case IP_RECVOPTS:
506                 case IP_RETOPTS:
507                         print_cmsg_ip_opts(tcp, cmsg_data, data_len);
508                         break;
509                 case IP_RECVERR:
510                         print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
511                         break;
512                 case IP_ORIGDSTADDR:
513                         print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
514                         break;
515                 case IP_CHECKSUM:
516                         print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
517                         break;
518                 case SCM_SECURITY:
519                         print_scm_security(tcp, cmsg_data, data_len);
520                         break;
521                 }
522                 break;
523         default:
524                 tprintf("%u", cmsg_type);
525         }
526 }
527
528 static void
529 printcmsghdr(struct tcb *tcp, unsigned long addr, size_t len)
530 {
531         const size_t cmsg_size =
532 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
533                 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
534 #endif
535                         sizeof(struct cmsghdr);
536
537         char *buf = len < cmsg_size ? NULL : malloc(len);
538         if (!buf || umoven(tcp, addr, len, buf) < 0) {
539                 tprints(", msg_control=");
540                 printaddr(addr);
541                 free(buf);
542                 return;
543         }
544
545         union_cmsghdr u = { .ptr = buf };
546
547         tprints(", [");
548         while (len >= cmsg_size) {
549                 size_t cmsg_len =
550 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
551                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
552 #endif
553                                 u.cmsg->cmsg_len;
554                 int cmsg_level =
555 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
556                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
557 #endif
558                                 u.cmsg->cmsg_level;
559                 int cmsg_type =
560 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
561                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
562 #endif
563                                 u.cmsg->cmsg_type;
564
565                 if (u.ptr != buf)
566                         tprints(", ");
567                 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
568                 printxval(socketlayers, cmsg_level, "SOL_???");
569                 tprints(", cmsg_type=");
570
571                 if (cmsg_len > len)
572                         cmsg_len = len;
573
574                 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
575                                      (const void *) (u.ptr + cmsg_size),
576                                      cmsg_len > cmsg_size ? cmsg_len - cmsg_size: 0);
577                 tprints("}");
578
579                 if (cmsg_len < cmsg_size) {
580                         len -= cmsg_size;
581                         break;
582                 }
583                 cmsg_len = (cmsg_len + current_wordsize - 1) &
584                         (size_t) ~(current_wordsize - 1);
585                 if (cmsg_len >= len) {
586                         len = 0;
587                         break;
588                 }
589                 u.ptr += cmsg_len;
590                 len -= cmsg_len;
591         }
592         if (len)
593                 tprints(", ...");
594         tprints("]");
595         free(buf);
596 }
597
598 static void
599 do_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
600 {
601         tprintf("{msg_name(%d)=", msg->msg_namelen);
602         printsock(tcp, (long)msg->msg_name, msg->msg_namelen);
603
604         tprintf(", msg_iov(%lu)=", (unsigned long)msg->msg_iovlen);
605
606         tprint_iov_upto(tcp, (unsigned long)msg->msg_iovlen,
607                         (unsigned long)msg->msg_iov, IOV_DECODE_STR, data_size);
608
609 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
610         tprintf(", msg_controllen=%lu", (unsigned long)msg->msg_controllen);
611         if (msg->msg_controllen)
612                 printcmsghdr(tcp, (unsigned long) msg->msg_control,
613                              msg->msg_controllen);
614         tprints(", msg_flags=");
615         printflags(msg_flags, msg->msg_flags, "MSG_???");
616 #else /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
617         tprintf("msg_accrights=%#lx, msg_accrightslen=%u",
618                 (unsigned long) msg->msg_accrights, msg->msg_accrightslen);
619 #endif /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
620         tprints("}");
621 }
622
623 struct msghdr32 {
624         uint32_t /* void* */    msg_name;
625         uint32_t /* socklen_t */msg_namelen;
626         uint32_t /* iovec* */   msg_iov;
627         uint32_t /* size_t */   msg_iovlen;
628         uint32_t /* void* */    msg_control;
629         uint32_t /* size_t */   msg_controllen;
630         uint32_t /* int */      msg_flags;
631 };
632 struct mmsghdr32 {
633         struct msghdr32         msg_hdr;
634         uint32_t /* unsigned */ msg_len;
635 };
636
637 #ifndef HAVE_STRUCT_MMSGHDR
638 struct mmsghdr {
639         struct msghdr msg_hdr;
640         unsigned msg_len;
641 };
642 #endif
643
644 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
645 static void
646 copy_from_msghdr32(struct msghdr *to_msg, struct msghdr32 *from_msg32)
647 {
648         to_msg->msg_name       = (void*)(long)from_msg32->msg_name;
649         to_msg->msg_namelen    =              from_msg32->msg_namelen;
650         to_msg->msg_iov        = (void*)(long)from_msg32->msg_iov;
651         to_msg->msg_iovlen     =              from_msg32->msg_iovlen;
652         to_msg->msg_control    = (void*)(long)from_msg32->msg_control;
653         to_msg->msg_controllen =              from_msg32->msg_controllen;
654         to_msg->msg_flags      =              from_msg32->msg_flags;
655 }
656 #endif
657
658 static bool
659 extractmsghdr(struct tcb *tcp, long addr, struct msghdr *msg)
660 {
661 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
662         if (current_wordsize == 4) {
663                 struct msghdr32 msg32;
664
665                 if (umove(tcp, addr, &msg32) < 0)
666                         return false;
667                 copy_from_msghdr32(msg, &msg32);
668         } else
669 #endif
670         if (umove(tcp, addr, msg) < 0)
671                 return false;
672         return true;
673 }
674
675 static bool
676 extractmmsghdr(struct tcb *tcp, long addr, unsigned int idx, struct mmsghdr *mmsg)
677 {
678 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
679         if (current_wordsize == 4) {
680                 struct mmsghdr32 mmsg32;
681
682                 addr += sizeof(struct mmsghdr32) * idx;
683                 if (umove(tcp, addr, &mmsg32) < 0)
684                         return false;
685
686                 copy_from_msghdr32(&mmsg->msg_hdr, &mmsg32.msg_hdr);
687                 mmsg->msg_len = mmsg32.msg_len;
688         } else
689 #endif
690         {
691                 addr += sizeof(*mmsg) * idx;
692                 if (umove(tcp, addr, mmsg) < 0)
693                         return false;
694         }
695         return true;
696 }
697
698 static void
699 printmsghdr(struct tcb *tcp, long addr, unsigned long data_size)
700 {
701         struct msghdr msg;
702
703         if (verbose(tcp) && extractmsghdr(tcp, addr, &msg))
704                 do_msghdr(tcp, &msg, data_size);
705         else
706                 printaddr(addr);
707 }
708
709 void
710 dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
711 {
712         struct msghdr msg;
713
714         if (extractmsghdr(tcp, addr, &msg))
715                 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
716 }
717
718 static void
719 printmmsghdr(struct tcb *tcp, long addr, unsigned int idx, unsigned long msg_len)
720 {
721         struct mmsghdr mmsg;
722
723         if (extractmmsghdr(tcp, addr, idx, &mmsg)) {
724                 tprints("{");
725                 do_msghdr(tcp, &mmsg.msg_hdr, msg_len ? msg_len : mmsg.msg_len);
726                 tprintf(", %u}", mmsg.msg_len);
727         }
728         else
729                 printaddr(addr);
730 }
731
732 static void
733 decode_mmsg(struct tcb *tcp, unsigned long msg_len)
734 {
735         /* mmsgvec */
736         if (syserror(tcp)) {
737                 printaddr(tcp->u_arg[1]);
738         } else {
739                 unsigned int len = tcp->u_rval;
740                 unsigned int i;
741
742                 tprints("{");
743                 for (i = 0; i < len; ++i) {
744                         if (i)
745                                 tprints(", ");
746                         printmmsghdr(tcp, tcp->u_arg[1], i, msg_len);
747                 }
748                 tprints("}");
749         }
750         /* vlen */
751         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
752         /* flags */
753         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
754 }
755
756 void
757 dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
758 {
759         unsigned int len = tcp->u_rval;
760         unsigned int i;
761         struct mmsghdr mmsg;
762
763         for (i = 0; i < len; ++i) {
764                 if (extractmmsghdr(tcp, addr, i, &mmsg)) {
765                         tprintf(" = %lu buffers in vector %u\n",
766                                 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
767                         dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
768                                 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);
769                 }
770         }
771 }
772
773 /*
774  * low bits of the socket type define real socket type,
775  * other bits are socket type flags.
776  */
777 static void
778 tprint_sock_type(unsigned int flags)
779 {
780         const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
781
782         if (str) {
783                 tprints(str);
784                 flags &= ~SOCK_TYPE_MASK;
785                 if (!flags)
786                         return;
787                 tprints("|");
788         }
789         printflags(sock_type_flags, flags, "SOCK_???");
790 }
791
792 SYS_FUNC(socket)
793 {
794         printxval(addrfams, tcp->u_arg[0], "AF_???");
795         tprints(", ");
796         tprint_sock_type(tcp->u_arg[1]);
797         tprints(", ");
798         switch (tcp->u_arg[0]) {
799         case AF_INET:
800         case AF_INET6:
801                 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
802                 break;
803
804         case AF_NETLINK:
805                 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
806                 break;
807
808 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
809         case AF_BLUETOOTH:
810                 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
811                 break;
812 #endif
813
814         default:
815                 tprintf("%lu", tcp->u_arg[2]);
816                 break;
817         }
818
819         return RVAL_DECODED | RVAL_FD;
820 }
821
822 SYS_FUNC(bind)
823 {
824         printfd(tcp, tcp->u_arg[0]);
825         tprints(", ");
826         printsock(tcp, tcp->u_arg[1], tcp->u_arg[2]);
827         tprintf(", %lu", tcp->u_arg[2]);
828
829         return RVAL_DECODED;
830 }
831
832 SYS_FUNC(listen)
833 {
834         printfd(tcp, tcp->u_arg[0]);
835         tprints(", ");
836         tprintf("%lu", tcp->u_arg[1]);
837
838         return RVAL_DECODED;
839 }
840
841 static int
842 do_sockname(struct tcb *tcp, int flags_arg)
843 {
844         if (entering(tcp)) {
845                 printfd(tcp, tcp->u_arg[0]);
846                 tprints(", ");
847                 return 0;
848         }
849
850         int len;
851         if (!tcp->u_arg[2] || !verbose(tcp) || syserror(tcp) ||
852             umove(tcp, tcp->u_arg[2], &len) < 0) {
853                 printaddr(tcp->u_arg[1]);
854                 tprints(", ");
855                 printaddr(tcp->u_arg[2]);
856         } else {
857                 printsock(tcp, tcp->u_arg[1], len);
858                 tprintf(", [%d]", len);
859         }
860
861         if (flags_arg >= 0) {
862                 tprints(", ");
863                 printflags(sock_type_flags, tcp->u_arg[flags_arg],
864                            "SOCK_???");
865         }
866         return 0;
867 }
868
869 SYS_FUNC(accept)
870 {
871         do_sockname(tcp, -1);
872         return RVAL_FD;
873 }
874
875 SYS_FUNC(accept4)
876 {
877         do_sockname(tcp, 3);
878         return RVAL_FD;
879 }
880
881 SYS_FUNC(send)
882 {
883         printfd(tcp, tcp->u_arg[0]);
884         tprints(", ");
885         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
886         tprintf(", %lu, ", tcp->u_arg[2]);
887         /* flags */
888         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
889
890         return RVAL_DECODED;
891 }
892
893 SYS_FUNC(sendto)
894 {
895         printfd(tcp, tcp->u_arg[0]);
896         tprints(", ");
897         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
898         tprintf(", %lu, ", tcp->u_arg[2]);
899         /* flags */
900         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
901         /* to address */
902         tprints(", ");
903         printsock(tcp, tcp->u_arg[4], tcp->u_arg[5]);
904         /* to length */
905         tprintf(", %lu", tcp->u_arg[5]);
906
907         return RVAL_DECODED;
908 }
909
910 SYS_FUNC(sendmsg)
911 {
912         printfd(tcp, tcp->u_arg[0]);
913         tprints(", ");
914         printmsghdr(tcp, tcp->u_arg[1], (unsigned long) -1L);
915         /* flags */
916         tprints(", ");
917         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
918
919         return RVAL_DECODED;
920 }
921
922 SYS_FUNC(sendmmsg)
923 {
924         if (entering(tcp)) {
925                 /* sockfd */
926                 printfd(tcp, tcp->u_arg[0]);
927                 tprints(", ");
928                 if (!verbose(tcp)) {
929                         printaddr(tcp->u_arg[1]);
930                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
931                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
932                 }
933         } else {
934                 if (verbose(tcp))
935                         decode_mmsg(tcp, (unsigned long) -1L);
936         }
937         return 0;
938 }
939
940 SYS_FUNC(recv)
941 {
942         if (entering(tcp)) {
943                 printfd(tcp, tcp->u_arg[0]);
944                 tprints(", ");
945         } else {
946                 if (syserror(tcp))
947                         printaddr(tcp->u_arg[1]);
948                 else
949                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
950
951                 tprintf(", %lu, ", tcp->u_arg[2]);
952                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
953         }
954         return 0;
955 }
956
957 SYS_FUNC(recvfrom)
958 {
959         int fromlen;
960
961         if (entering(tcp)) {
962                 printfd(tcp, tcp->u_arg[0]);
963                 tprints(", ");
964         } else {
965                 /* buf */
966                 if (syserror(tcp)) {
967                         printaddr(tcp->u_arg[1]);
968                 } else {
969                         printstr(tcp, tcp->u_arg[1], tcp->u_rval);
970                 }
971                 /* len */
972                 tprintf(", %lu, ", tcp->u_arg[2]);
973                 /* flags */
974                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
975                 tprints(", ");
976                 if (syserror(tcp) || !tcp->u_arg[4] || !tcp->u_arg[5] ||
977                     umove(tcp, tcp->u_arg[5], &fromlen) < 0) {
978                         /* from address, len */
979                         printaddr(tcp->u_arg[4]);
980                         tprints(", ");
981                         printaddr(tcp->u_arg[5]);
982                         return 0;
983                 }
984                 /* from address */
985                 printsock(tcp, tcp->u_arg[4], fromlen);
986                 /* from length */
987                 tprintf(", [%u]", fromlen);
988         }
989         return 0;
990 }
991
992 SYS_FUNC(recvmsg)
993 {
994         if (entering(tcp)) {
995                 printfd(tcp, tcp->u_arg[0]);
996                 tprints(", ");
997         } else {
998                 if (syserror(tcp))
999                         printaddr(tcp->u_arg[1]);
1000                 else
1001                         printmsghdr(tcp, tcp->u_arg[1], tcp->u_rval);
1002                 /* flags */
1003                 tprints(", ");
1004                 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
1005         }
1006         return 0;
1007 }
1008
1009 SYS_FUNC(recvmmsg)
1010 {
1011         static char str[sizeof("left") + TIMESPEC_TEXT_BUFSIZE];
1012
1013         if (entering(tcp)) {
1014                 printfd(tcp, tcp->u_arg[0]);
1015                 tprints(", ");
1016                 if (verbose(tcp)) {
1017                         /* Abusing tcp->auxstr as temp storage.
1018                          * Will be used and cleared on syscall exit.
1019                          */
1020                         tcp->auxstr = sprint_timespec(tcp, tcp->u_arg[4]);
1021                 } else {
1022                         printaddr(tcp->u_arg[1]);
1023                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
1024                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
1025                         tprints(", ");
1026                         print_timespec(tcp, tcp->u_arg[4]);
1027                 }
1028                 return 0;
1029         } else {
1030                 if (verbose(tcp)) {
1031                         decode_mmsg(tcp, 0);
1032                         tprints(", ");
1033                         /* timeout on entrance */
1034                         tprints(tcp->auxstr);
1035                         tcp->auxstr = NULL;
1036                 }
1037                 if (syserror(tcp))
1038                         return 0;
1039                 if (tcp->u_rval == 0) {
1040                         tcp->auxstr = "Timeout";
1041                         return RVAL_STR;
1042                 }
1043                 if (!verbose(tcp))
1044                         return 0;
1045                 /* timeout on exit */
1046                 snprintf(str, sizeof(str), "left %s",
1047                          sprint_timespec(tcp, tcp->u_arg[4]));
1048                 tcp->auxstr = str;
1049                 return RVAL_STR;
1050         }
1051 }
1052
1053 #include "xlat/shutdown_modes.h"
1054
1055 SYS_FUNC(shutdown)
1056 {
1057         printfd(tcp, tcp->u_arg[0]);
1058         tprints(", ");
1059         printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
1060
1061         return RVAL_DECODED;
1062 }
1063
1064 SYS_FUNC(getsockname)
1065 {
1066         return do_sockname(tcp, -1);
1067 }
1068
1069 static void
1070 printpair_fd(struct tcb *tcp, const int i0, const int i1)
1071 {
1072         tprints("[");
1073         printfd(tcp, i0);
1074         tprints(", ");
1075         printfd(tcp, i1);
1076         tprints("]");
1077 }
1078
1079 static void
1080 decode_pair_fd(struct tcb *tcp, const long addr)
1081 {
1082         int pair[2];
1083
1084         if (umove_or_printaddr(tcp, addr, &pair))
1085                 return;
1086
1087         printpair_fd(tcp, pair[0], pair[1]);
1088 }
1089
1090 static int
1091 do_pipe(struct tcb *tcp, int flags_arg)
1092 {
1093         if (exiting(tcp)) {
1094                 decode_pair_fd(tcp, tcp->u_arg[0]);
1095                 if (flags_arg >= 0) {
1096                         tprints(", ");
1097                         printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1098                 }
1099         }
1100         return 0;
1101 }
1102
1103 SYS_FUNC(pipe)
1104 {
1105 #ifdef HAVE_GETRVAL2
1106         if (exiting(tcp) && !syserror(tcp))
1107                 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
1108         return 0;
1109 #else
1110         return do_pipe(tcp, -1);
1111 #endif
1112 }
1113
1114 SYS_FUNC(pipe2)
1115 {
1116         return do_pipe(tcp, 1);
1117 }
1118
1119 SYS_FUNC(socketpair)
1120 {
1121         if (entering(tcp)) {
1122                 printxval(addrfams, tcp->u_arg[0], "AF_???");
1123                 tprints(", ");
1124                 tprint_sock_type(tcp->u_arg[1]);
1125                 tprintf(", %lu", tcp->u_arg[2]);
1126         } else {
1127                 tprints(", ");
1128                 decode_pair_fd(tcp, tcp->u_arg[3]);
1129         }
1130         return 0;
1131 }
1132
1133 #include "xlat/sockoptions.h"
1134 #include "xlat/sockipoptions.h"
1135 #include "xlat/getsockipoptions.h"
1136 #include "xlat/setsockipoptions.h"
1137 #include "xlat/sockipv6options.h"
1138 #include "xlat/getsockipv6options.h"
1139 #include "xlat/setsockipv6options.h"
1140 #include "xlat/sockipxoptions.h"
1141 #include "xlat/sockrawoptions.h"
1142 #include "xlat/sockpacketoptions.h"
1143 #include "xlat/socksctpoptions.h"
1144 #include "xlat/socktcpoptions.h"
1145
1146 static void
1147 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
1148                             unsigned int name, bool is_getsockopt)
1149 {
1150         printfd(tcp, fd);
1151         tprints(", ");
1152         printxval(socketlayers, level, "SOL_??");
1153         tprints(", ");
1154
1155         switch (level) {
1156         case SOL_SOCKET:
1157                 printxval(sockoptions, name, "SO_???");
1158                 break;
1159         case SOL_IP:
1160                 printxvals(name, "IP_???", sockipoptions,
1161                         is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
1162                 break;
1163         case SOL_IPV6:
1164                 printxvals(name, "IPV6_???", sockipv6options,
1165                         is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
1166                 break;
1167         case SOL_IPX:
1168                 printxval(sockipxoptions, name, "IPX_???");
1169                 break;
1170         case SOL_PACKET:
1171                 printxval(sockpacketoptions, name, "PACKET_???");
1172                 break;
1173         case SOL_TCP:
1174                 printxval(socktcpoptions, name, "TCP_???");
1175                 break;
1176         case SOL_SCTP:
1177                 printxval(socksctpoptions, name, "SCTP_???");
1178                 break;
1179         case SOL_RAW:
1180                 printxval(sockrawoptions, name, "RAW_???");
1181                 break;
1182
1183                 /* Other SOL_* protocol levels still need work. */
1184
1185         default:
1186                 tprintf("%u", name);
1187         }
1188
1189         tprints(", ");
1190 }
1191
1192 static void
1193 print_linger(struct tcb *tcp, long addr, int len)
1194 {
1195         struct linger linger;
1196
1197         if (len != sizeof(linger) ||
1198             umove(tcp, addr, &linger) < 0) {
1199                 printaddr(addr);
1200                 return;
1201         }
1202
1203         tprintf("{onoff=%d, linger=%d}",
1204                 linger.l_onoff,
1205                 linger.l_linger);
1206 }
1207
1208 #ifdef SO_PEERCRED
1209 static void
1210 print_ucred(struct tcb *tcp, long addr, int len)
1211 {
1212         struct ucred uc;
1213
1214         if (len != sizeof(uc) ||
1215             umove(tcp, addr, &uc) < 0) {
1216                 printaddr(addr);
1217         } else {
1218                 tprintf("{pid=%u, uid=%u, gid=%u}",
1219                         (unsigned) uc.pid,
1220                         (unsigned) uc.uid,
1221                         (unsigned) uc.gid);
1222         }
1223 }
1224 #endif /* SO_PEERCRED */
1225
1226 #ifdef PACKET_STATISTICS
1227 static void
1228 print_tpacket_stats(struct tcb *tcp, long addr, int len)
1229 {
1230         struct tpacket_stats stats;
1231
1232         if (len != sizeof(stats) ||
1233             umove(tcp, addr, &stats) < 0) {
1234                 printaddr(addr);
1235         } else {
1236                 tprintf("{packets=%u, drops=%u}",
1237                         stats.tp_packets,
1238                         stats.tp_drops);
1239         }
1240 }
1241 #endif /* PACKET_STATISTICS */
1242
1243 #include "xlat/icmpfilterflags.h"
1244
1245 static void
1246 print_icmp_filter(struct tcb *tcp, const long addr, int len)
1247 {
1248         struct icmp_filter filter = {};
1249
1250         if (len > (int) sizeof(filter))
1251                 len = sizeof(filter);
1252         else if (len <= 0) {
1253                 printaddr(addr);
1254                 return;
1255         }
1256
1257         if (umoven_or_printaddr(tcp, addr, len, &filter))
1258                 return;
1259
1260         tprints("~(");
1261         printflags(icmpfilterflags, ~filter.data, "ICMP_???");
1262         tprints(")");
1263 }
1264
1265 static void
1266 print_getsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
1267                  long addr, int len)
1268 {
1269         if (addr && verbose(tcp))
1270         switch (level) {
1271         case SOL_SOCKET:
1272                 switch (name) {
1273                 case SO_LINGER:
1274                         print_linger(tcp, addr, len);
1275                         goto done;
1276 #ifdef SO_PEERCRED
1277                 case SO_PEERCRED:
1278                         print_ucred(tcp, addr, len);
1279                         goto done;
1280 #endif
1281                 }
1282                 break;
1283
1284         case SOL_PACKET:
1285                 switch (name) {
1286 #ifdef PACKET_STATISTICS
1287                 case PACKET_STATISTICS:
1288                         print_tpacket_stats(tcp, addr, len);
1289                         goto done;
1290 #endif
1291                 }
1292                 break;
1293
1294         case SOL_RAW:
1295                 switch (name) {
1296                 case ICMP_FILTER:
1297                         print_icmp_filter(tcp, addr, len);
1298                         goto done;
1299                 }
1300                 break;
1301         }
1302
1303         /* default arg printing */
1304
1305         if (verbose(tcp)) {
1306                 if (len == sizeof(int)) {
1307                         printnum_int(tcp, addr, "%d");
1308                 } else {
1309                         printstr(tcp, addr, len);
1310                 }
1311         } else {
1312                 printaddr(addr);
1313         }
1314 done:
1315         tprintf(", [%d]", len);
1316 }
1317
1318 SYS_FUNC(getsockopt)
1319 {
1320         if (entering(tcp)) {
1321                 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1322                                             tcp->u_arg[1], tcp->u_arg[2], true);
1323         } else {
1324                 int len;
1325
1326                 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
1327                         printaddr(tcp->u_arg[3]);
1328                         tprints(", ");
1329                         printaddr(tcp->u_arg[4]);
1330                 } else {
1331                         print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1332                                          tcp->u_arg[3], len);
1333                 }
1334         }
1335         return 0;
1336 }
1337
1338 #ifdef IP_ADD_MEMBERSHIP
1339 static void
1340 print_mreq(struct tcb *tcp, long addr, unsigned int len)
1341 {
1342         struct ip_mreq mreq;
1343
1344         if (len < sizeof(mreq)) {
1345                 printstr(tcp, addr, len);
1346                 return;
1347         }
1348         if (umove_or_printaddr(tcp, addr, &mreq))
1349                 return;
1350
1351         tprints("{imr_multiaddr=inet_addr(");
1352         print_quoted_string(inet_ntoa(mreq.imr_multiaddr),
1353                             16, QUOTE_0_TERMINATED);
1354         tprints("), imr_interface=inet_addr(");
1355         print_quoted_string(inet_ntoa(mreq.imr_interface),
1356                             16, QUOTE_0_TERMINATED);
1357         tprints(")}");
1358 }
1359 #endif /* IP_ADD_MEMBERSHIP */
1360
1361 #ifdef IPV6_ADD_MEMBERSHIP
1362 static void
1363 print_mreq6(struct tcb *tcp, long addr, unsigned int len)
1364 {
1365         struct ipv6_mreq mreq;
1366
1367         if (len < sizeof(mreq))
1368                 goto fail;
1369
1370         if (umove_or_printaddr(tcp, addr, &mreq))
1371                 return;
1372
1373         const struct in6_addr *in6 = &mreq.ipv6mr_multiaddr;
1374         char address[INET6_ADDRSTRLEN];
1375
1376         if (!inet_ntop(AF_INET6, in6, address, sizeof(address)))
1377                 goto fail;
1378
1379         tprints("{ipv6mr_multiaddr=inet_pton(");
1380         print_quoted_string(address, sizeof(address), QUOTE_0_TERMINATED);
1381         tprints("), ipv6mr_interface=");
1382         print_ifindex(mreq.ipv6mr_interface);
1383         tprints("}");
1384         return;
1385
1386 fail:
1387         printstr(tcp, addr, len);
1388 }
1389 #endif /* IPV6_ADD_MEMBERSHIP */
1390
1391 #ifdef MCAST_JOIN_GROUP
1392 static void
1393 print_group_req(struct tcb *tcp, long addr, int len)
1394 {
1395         struct group_req greq;
1396
1397         if (len != sizeof(greq) ||
1398             umove(tcp, addr, &greq) < 0) {
1399                 printaddr(addr);
1400                 return;
1401         }
1402
1403         tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
1404         print_sockaddr(tcp, &greq.gr_group, sizeof(greq.gr_group));
1405         tprintf("}");
1406
1407 }
1408 #endif /* MCAST_JOIN_GROUP */
1409
1410 #ifdef PACKET_RX_RING
1411 static void
1412 print_tpacket_req(struct tcb *tcp, long addr, int len)
1413 {
1414         struct tpacket_req req;
1415
1416         if (len != sizeof(req) ||
1417             umove(tcp, addr, &req) < 0) {
1418                 printaddr(addr);
1419         } else {
1420                 tprintf("{block_size=%u, block_nr=%u, "
1421                         "frame_size=%u, frame_nr=%u}",
1422                         req.tp_block_size,
1423                         req.tp_block_nr,
1424                         req.tp_frame_size,
1425                         req.tp_frame_nr);
1426         }
1427 }
1428 #endif /* PACKET_RX_RING */
1429
1430 #ifdef PACKET_ADD_MEMBERSHIP
1431 # include "xlat/packet_mreq_type.h"
1432
1433 static void
1434 print_packet_mreq(struct tcb *tcp, long addr, int len)
1435 {
1436         struct packet_mreq mreq;
1437
1438         if (len != sizeof(mreq) ||
1439             umove(tcp, addr, &mreq) < 0) {
1440                 printaddr(addr);
1441         } else {
1442                 unsigned int i;
1443
1444                 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
1445                 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
1446                 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
1447                 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
1448                         mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
1449                 for (i = 0; i < mreq.mr_alen; ++i)
1450                         tprintf("%02x", mreq.mr_address[i]);
1451                 tprints("}");
1452         }
1453 }
1454 #endif /* PACKET_ADD_MEMBERSHIP */
1455
1456 static void
1457 print_setsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
1458                  long addr, int len)
1459 {
1460         if (addr && verbose(tcp))
1461         switch (level) {
1462         case SOL_SOCKET:
1463                 switch (name) {
1464                 case SO_LINGER:
1465                         print_linger(tcp, addr, len);
1466                         goto done;
1467                 }
1468                 break;
1469
1470         case SOL_IP:
1471                 switch (name) {
1472 #ifdef IP_ADD_MEMBERSHIP
1473                 case IP_ADD_MEMBERSHIP:
1474                 case IP_DROP_MEMBERSHIP:
1475                         print_mreq(tcp, addr, len);
1476                         goto done;
1477 #endif /* IP_ADD_MEMBERSHIP */
1478 #ifdef MCAST_JOIN_GROUP
1479                 case MCAST_JOIN_GROUP:
1480                 case MCAST_LEAVE_GROUP:
1481                         print_group_req(tcp, addr, len);
1482                         goto done;
1483 #endif /* MCAST_JOIN_GROUP */
1484                 }
1485                 break;
1486
1487         case SOL_IPV6:
1488                 switch (name) {
1489 #ifdef IPV6_ADD_MEMBERSHIP
1490                 case IPV6_ADD_MEMBERSHIP:
1491                 case IPV6_DROP_MEMBERSHIP:
1492 # ifdef IPV6_JOIN_ANYCAST
1493                 case IPV6_JOIN_ANYCAST:
1494 # endif
1495 # ifdef IPV6_LEAVE_ANYCAST
1496                 case IPV6_LEAVE_ANYCAST:
1497 # endif
1498                         print_mreq6(tcp, addr, len);
1499                         goto done;
1500 #endif /* IPV6_ADD_MEMBERSHIP */
1501                 }
1502                 break;
1503
1504         case SOL_PACKET:
1505                 switch (name) {
1506 #ifdef PACKET_RX_RING
1507                 case PACKET_RX_RING:
1508 # ifdef PACKET_TX_RING
1509                 case PACKET_TX_RING:
1510 # endif
1511                         print_tpacket_req(tcp, addr, len);
1512                         goto done;
1513 #endif /* PACKET_RX_RING */
1514 #ifdef PACKET_ADD_MEMBERSHIP
1515                 case PACKET_ADD_MEMBERSHIP:
1516                 case PACKET_DROP_MEMBERSHIP:
1517                         print_packet_mreq(tcp, addr, len);
1518                         goto done;
1519 #endif /* PACKET_ADD_MEMBERSHIP */
1520                 }
1521                 break;
1522
1523         case SOL_RAW:
1524                 switch (name) {
1525                 case ICMP_FILTER:
1526                         print_icmp_filter(tcp, addr, len);
1527                         goto done;
1528                 }
1529                 break;
1530         }
1531
1532         /* default arg printing */
1533
1534         if (verbose(tcp)) {
1535                 if (len == sizeof(int)) {
1536                         printnum_int(tcp, addr, "%d");
1537                 } else {
1538                         printstr(tcp, addr, len);
1539                 }
1540         } else {
1541                 printaddr(addr);
1542         }
1543 done:
1544         tprintf(", %d", len);
1545 }
1546
1547 SYS_FUNC(setsockopt)
1548 {
1549         print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1550                                     tcp->u_arg[1], tcp->u_arg[2], false);
1551         print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1552                          tcp->u_arg[3], tcp->u_arg[4]);
1553
1554         return RVAL_DECODED;
1555 }