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