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