blob: f6d8c7df35c401f6978eb61e53654edb790b6070 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
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>
Wichert Akkerman7987cdf2000-07-05 16:05:39 +00005 * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * 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.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032#include <sys/stat.h>
33#include <sys/socket.h>
Dmitry V. Levinb2fa2be2014-11-21 20:46:16 +000034#include <sys/uio.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/un.h>
36#include <netinet/in.h>
Wichert Akkerman8c7122c2001-02-16 19:59:55 +000037#ifdef HAVE_NETINET_TCP_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010038# include <netinet/tcp.h>
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000039#endif
Wichert Akkerman8c7122c2001-02-16 19:59:55 +000040#ifdef HAVE_NETINET_UDP_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010041# include <netinet/udp.h>
Wichert Akkerman8c7122c2001-02-16 19:59:55 +000042#endif
Holger Hans Peter Freyther7fea79b2011-01-14 11:08:12 +010043#ifdef HAVE_NETINET_SCTP_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010044# include <netinet/sctp.h>
Holger Hans Peter Freyther7fea79b2011-01-14 11:08:12 +010045#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000046#include <arpa/inet.h>
Wichert Akkermanf1850652001-02-16 20:29:03 +000047#include <net/if.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000048#include <asm/types.h>
Denys Vlasenko99aa1812013-02-08 18:49:06 +010049#if defined(__GLIBC__)
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010050# include <netipx/ipx.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000051#else
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010052# include <linux/ipx.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000053#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000054
Mike Frysinger54646b82015-08-19 13:29:27 -040055#if defined(HAVE_LINUX_IP_VS_H)
56# include <linux/ip_vs.h>
57#endif
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +000058#if defined(HAVE_LINUX_NETLINK_H)
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010059# include <linux/netlink.h>
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +000060#endif
Mike Frysinger54646b82015-08-19 13:29:27 -040061#if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
62# include <linux/netfilter_arp/arp_tables.h>
63#endif
64#if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
65# include <linux/netfilter_bridge/ebtables.h>
66#endif
67#if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
68# include <linux/netfilter_ipv4/ip_tables.h>
69#endif
70#if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
71# include <linux/netfilter_ipv6/ip6_tables.h>
72#endif
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +000073#if defined(HAVE_LINUX_IF_PACKET_H)
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010074# include <linux/if_packet.h>
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +000075#endif
Wichert Akkerman7987cdf2000-07-05 16:05:39 +000076#if defined(HAVE_LINUX_ICMP_H)
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010077# include <linux/icmp.h>
Wichert Akkerman7987cdf2000-07-05 16:05:39 +000078#endif
Lubomir Rintelc400a1c2014-10-03 11:40:28 +020079#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
80# include <bluetooth/bluetooth.h>
81# include <bluetooth/hci.h>
82# include <bluetooth/l2cap.h>
83# include <bluetooth/rfcomm.h>
84# include <bluetooth/sco.h>
85#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000087#include "xlat/addrfams.h"
88#include "xlat/socktypes.h"
89#include "xlat/sock_type_flags.h"
Dmitry V. Levin8a550d72008-11-10 17:21:23 +000090#ifndef SOCK_TYPE_MASK
91# define SOCK_TYPE_MASK 0xf
92#endif
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +000093
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000094#include "xlat/socketlayers.h"
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +000095
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000096#include "xlat/inet_protocols.h"
Masatake YAMATO2394a3d2014-03-11 23:37:37 +090097
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +000098#ifdef AF_NETLINK
Dmitry V. Levind9f7e7a2015-01-09 03:03:39 +000099# if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
100# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
101# endif
102# include "xlat/netlink_protocols.h"
Masatake YAMATO2394a3d2014-03-11 23:37:37 +0900103#endif
104
Lubomir Rintelc400a1c2014-10-03 11:40:28 +0200105#if defined(HAVE_BLUETOOTH_BLUETOOTH_H)
106# include "xlat/bt_protocols.h"
107#endif
108
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000109#include "xlat/msg_flags.h"
Wichert Akkerman7987cdf2000-07-05 16:05:39 +0000110
Wichert Akkermanb0c598f2002-04-01 12:48:06 +0000111#if defined(AF_PACKET) /* from e.g. linux/if_packet.h */
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +0000112# include "xlat/af_packet_types.h"
113#endif
Wichert Akkermanb0c598f2002-04-01 12:48:06 +0000114
Dmitry V. Levinae280932015-06-05 20:13:21 +0000115static void
116print_ifindex(unsigned int ifindex)
117{
118#ifdef HAVE_IF_INDEXTONAME
119 char buf[IFNAMSIZ + 1];
120
121 if (if_indextoname(ifindex, buf)) {
122 tprints("if_nametoindex(");
123 print_quoted_string(buf, sizeof(buf), QUOTE_0_TERMINATED);
124 tprints(")");
125 return;
126 }
127#endif
128 tprintf("%u", ifindex);
129}
130
Dmitry V. Levin1019f002015-11-21 02:38:59 +0000131typedef union {
132 char pad[128];
133 struct sockaddr sa;
134 struct sockaddr_in sin;
135 struct sockaddr_un sau;
136#ifdef HAVE_INET_NTOP
137 struct sockaddr_in6 sa6;
138#endif
139#if defined(AF_IPX)
140 struct sockaddr_ipx sipx;
141#endif
142#ifdef AF_PACKET
143 struct sockaddr_ll ll;
144#endif
145#ifdef AF_NETLINK
146 struct sockaddr_nl nl;
147#endif
148#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
149 struct sockaddr_hci hci;
150 struct sockaddr_l2 l2;
151 struct sockaddr_rc rc;
152 struct sockaddr_sco sco;
153#endif
154} sockaddr_buf_t;
155
156static void
157print_sockaddr(struct tcb *tcp, const sockaddr_buf_t *addr, const int addrlen)
158{
159 tprints("{sa_family=");
160 printxval(addrfams, addr->sa.sa_family, "AF_???");
161 tprints(", ");
162
163 switch (addr->sa.sa_family) {
164 case AF_UNIX:
165 if (addrlen == 2) {
166 tprints("NULL");
167 } else if (addr->sau.sun_path[0]) {
168 tprints("sun_path=");
169 print_quoted_string(addr->sau.sun_path,
170 sizeof(addr->sau.sun_path) + 1,
171 QUOTE_0_TERMINATED);
172 } else {
173 tprints("sun_path=@");
174 print_quoted_string(addr->sau.sun_path + 1,
175 sizeof(addr->sau.sun_path),
176 QUOTE_0_TERMINATED);
177 }
178 break;
179 case AF_INET:
180 tprintf("sin_port=htons(%u), sin_addr=inet_addr(\"%s\")",
181 ntohs(addr->sin.sin_port), inet_ntoa(addr->sin.sin_addr));
182 break;
183#ifdef HAVE_INET_NTOP
184 case AF_INET6:
185 {
186 char string_addr[100];
187 inet_ntop(AF_INET6, &addr->sa6.sin6_addr,
188 string_addr, sizeof(string_addr));
189 tprintf("sin6_port=htons(%u), inet_pton(AF_INET6"
190 ", \"%s\", &sin6_addr), sin6_flowinfo=%u",
191 ntohs(addr->sa6.sin6_port), string_addr,
192 addr->sa6.sin6_flowinfo);
193# ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
194 tprints(", sin6_scope_id=");
195# if defined IN6_IS_ADDR_LINKLOCAL && defined IN6_IS_ADDR_MC_LINKLOCAL
196 if (IN6_IS_ADDR_LINKLOCAL(&addr->sa6.sin6_addr)
197 || IN6_IS_ADDR_MC_LINKLOCAL(&addr->sa6.sin6_addr))
198 print_ifindex(addr->sa6.sin6_scope_id);
199 else
200# endif
201 tprintf("%u", addr->sa6.sin6_scope_id);
202# endif /* HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID */
203 }
204 break;
205#endif
206#if defined(AF_IPX)
207 case AF_IPX:
208 {
209 int i;
210 tprintf("sipx_port=htons(%u), ",
211 ntohs(addr->sipx.sipx_port));
212 /* Yes, I know, this does not look too
213 * strace-ish, but otherwise the IPX
214 * addresses just look monstrous...
215 * Anyways, feel free if you don't like
216 * this way.. :)
217 */
218 tprintf("%08lx:", (unsigned long)ntohl(addr->sipx.sipx_network));
219 for (i = 0; i < IPX_NODE_LEN; i++)
220 tprintf("%02x", addr->sipx.sipx_node[i]);
221 tprintf("/[%02x]", addr->sipx.sipx_type);
222 }
223 break;
224#endif /* AF_IPX */
225#ifdef AF_PACKET
226 case AF_PACKET:
227 {
228 int i;
229 tprintf("proto=%#04x, if%d, pkttype=",
230 ntohs(addr->ll.sll_protocol),
231 addr->ll.sll_ifindex);
232 printxval(af_packet_types, addr->ll.sll_pkttype, "PACKET_???");
233 tprintf(", addr(%d)={%d, ",
234 addr->ll.sll_halen,
235 addr->ll.sll_hatype);
236 for (i = 0; i < addr->ll.sll_halen; i++)
237 tprintf("%02x", addr->ll.sll_addr[i]);
238 }
239 break;
240
241#endif /* AF_PACKET */
242#ifdef AF_NETLINK
243 case AF_NETLINK:
244 tprintf("pid=%d, groups=%08x", addr->nl.nl_pid, addr->nl.nl_groups);
245 break;
246#endif /* AF_NETLINK */
247#if defined(AF_BLUETOOTH) && defined(HAVE_BLUETOOTH_BLUETOOTH_H)
248 case AF_BLUETOOTH:
249 tprintf("{sco_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X} or "
250 "{rc_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, rc_channel=%d} or "
251 "{l2_psm=htobs(%d), l2_bdaddr=%02X:%02X:%02X:%02X:%02X:%02X, l2_cid=htobs(%d)} or "
252 "{hci_dev=htobs(%d)}",
253 addr->sco.sco_bdaddr.b[0], addr->sco.sco_bdaddr.b[1],
254 addr->sco.sco_bdaddr.b[2], addr->sco.sco_bdaddr.b[3],
255 addr->sco.sco_bdaddr.b[4], addr->sco.sco_bdaddr.b[5],
256 addr->rc.rc_bdaddr.b[0], addr->rc.rc_bdaddr.b[1],
257 addr->rc.rc_bdaddr.b[2], addr->rc.rc_bdaddr.b[3],
258 addr->rc.rc_bdaddr.b[4], addr->rc.rc_bdaddr.b[5],
259 addr->rc.rc_channel,
260 btohs(addr->l2.l2_psm), addr->l2.l2_bdaddr.b[0],
261 addr->l2.l2_bdaddr.b[1], addr->l2.l2_bdaddr.b[2],
262 addr->l2.l2_bdaddr.b[3], addr->l2.l2_bdaddr.b[4],
263 addr->l2.l2_bdaddr.b[5], btohs(addr->l2.l2_cid),
264 btohs(addr->hci.hci_dev));
265 break;
266#endif /* AF_BLUETOOTH && HAVE_BLUETOOTH_BLUETOOTH_H */
267 /* AF_AX25 AF_APPLETALK AF_NETROM AF_BRIDGE AF_AAL5
268 AF_X25 AF_ROSE etc. still need to be done */
269
270 default:
271 tprints("sa_data=");
272 print_quoted_string(addr->sa.sa_data,
273 sizeof(addr->sa.sa_data), 0);
274 break;
275 }
276 tprints("}");
277}
278
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000279void
Dmitry V. Levinb6c32f42007-10-08 23:31:19 +0000280printsock(struct tcb *tcp, long addr, int addrlen)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000281{
Dmitry V. Levin1019f002015-11-21 02:38:59 +0000282 sockaddr_buf_t addrbuf;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283
Denys Vlasenko383386d2015-07-30 13:29:20 +0200284 if (addrlen < 2) {
Dmitry V. Levin52dc1502015-07-30 13:53:42 +0000285 printaddr(addr);
Denys Vlasenko383386d2015-07-30 13:29:20 +0200286 return;
287 }
288
289 if (addrlen > (int) sizeof(addrbuf))
Dmitry V. Levinb6c32f42007-10-08 23:31:19 +0000290 addrlen = sizeof(addrbuf);
291
292 memset(&addrbuf, 0, sizeof(addrbuf));
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000293 if (umoven_or_printaddr(tcp, addr, addrlen, addrbuf.pad))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000294 return;
Dmitry V. Levinb6c32f42007-10-08 23:31:19 +0000295 addrbuf.pad[sizeof(addrbuf.pad) - 1] = '\0';
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000296
Dmitry V. Levin1019f002015-11-21 02:38:59 +0000297 print_sockaddr(tcp, &addrbuf, addrlen);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298}
299
Dmitry V. Levinb36d1322015-11-18 23:18:17 +0000300#include "xlat/scmvals.h"
Dmitry V. Levin1f111cf2015-11-21 03:03:54 +0300301#include "xlat/ip_cmsg_types.h"
Roland McGrath50770822004-10-06 22:11:51 +0000302
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000303#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
304struct cmsghdr32 {
305 uint32_t cmsg_len;
306 int cmsg_level;
307 int cmsg_type;
308};
309#endif
310
311typedef union {
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000312 char *ptr;
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000313 struct cmsghdr *cmsg;
314#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
315 struct cmsghdr32 *cmsg32;
316#endif
317} union_cmsghdr;
318
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000319static void
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000320print_scm_rights(struct tcb *tcp, const void *cmsg_data,
321 const size_t data_len)
Roland McGrath50770822004-10-06 22:11:51 +0000322{
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000323 const int *fds = cmsg_data;
324 const char *end = (const char *) cmsg_data + data_len;
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000325 bool seen = false;
326
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000327 if (sizeof(*fds) > data_len)
328 return;
329
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000330 tprints(", [");
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000331 while ((const char *) fds < end) {
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000332 if (seen)
333 tprints(", ");
334 else
335 seen = true;
336 printfd(tcp, *fds++);
337 }
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000338 tprints("]");
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000339}
340
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000341static void
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000342print_scm_creds(struct tcb *tcp, const void *cmsg_data,
343 const size_t data_len)
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000344{
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000345 const struct ucred *uc = cmsg_data;
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000346
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000347 if (sizeof(*uc) > data_len)
348 return;
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000349
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000350 tprintf(", {pid=%u, uid=%u, gid=%u}",
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000351 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000352}
353
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000354static void
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000355print_scm_security(struct tcb *tcp, const void *cmsg_data,
356 const size_t data_len)
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000357{
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000358 if (!data_len)
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000359 return;
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000360
Dmitry V. Levin513e96e2015-01-26 01:17:08 +0000361 tprints(", ");
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000362 print_quoted_string(cmsg_data, data_len, 0);
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000363}
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000364
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000365static void
Dmitry V. Levin1f111cf2015-11-21 03:03:54 +0300366print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
367 const size_t data_len)
368{
369 const struct in_pktinfo *info = cmsg_data;
370
371 if (sizeof(*info) > data_len)
372 return;
373
374 tprints(", {ipi_ifindex=");
375 print_ifindex(info->ipi_ifindex);
376 tprintf(", ipi_spec_dst=inet_addr(\"%s\"), ipi_addr=inet_addr(\"%s\")}",
377 inet_ntoa(info->ipi_spec_dst), inet_ntoa(info->ipi_addr));
378}
379
380static void
381print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
382 const size_t data_len)
383{
384 const unsigned int *ttl = cmsg_data;
385
386 if (sizeof(*ttl) > data_len)
387 return;
388
389 tprintf(", {ttl=%u}", *ttl);
390}
391
392static void
393print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
394 const size_t data_len)
395{
396 const uint8_t *tos = cmsg_data;
397
398 if (sizeof(*tos) > data_len)
399 return;
400
401 tprintf(", {tos=%x}", *tos);
402}
403
404static void
405print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
406 const size_t data_len)
407{
408 const uint32_t *csum = cmsg_data;
409
410 if (sizeof(*csum) > data_len)
411 return;
412
413 tprintf(", {csum=%u}", *csum);
414}
415
416static void
417print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
418 const size_t data_len)
419{
Dmitry V. Levina17f5932015-11-22 23:07:52 +0000420 const unsigned char *opts = cmsg_data;
Dmitry V. Levin1f111cf2015-11-21 03:03:54 +0300421 size_t i;
422
423 if (!data_len)
424 return;
425
426 tprints(", {opts=0x");
427 for (i = 0; i < data_len; ++i)
428 tprintf("%02x", opts[i]);
429 tprints("}");
430}
431
432static void
433print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
434 const size_t data_len)
435{
436 const struct {
437 uint32_t ee_errno;
438 uint8_t ee_origin;
439 uint8_t ee_type;
440 uint8_t ee_code;
441 uint8_t ee_pad;
442 uint32_t ee_info;
443 uint32_t ee_data;
444 struct sockaddr_in offender;
445 } *err = cmsg_data;
446
447 if (sizeof(*err) > data_len)
448 return;
449
450 tprintf(", {ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
451 ", ee_info=%u, ee_data=%u, offender=",
452 err->ee_errno, err->ee_origin, err->ee_type,
453 err->ee_code, err->ee_info, err->ee_data);
454 print_sockaddr(tcp, (const void *) &err->offender,
455 sizeof(err->offender));
456 tprints("}");
457}
458
459static void
460print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
461 const size_t data_len)
462{
463 if (sizeof(struct sockaddr_in) > data_len)
464 return;
465
466 tprints(", ");
467 print_sockaddr(tcp, cmsg_data, data_len);
468}
469
470static void
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000471print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000472 const void *cmsg_data, const size_t data_len)
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000473{
474 switch (cmsg_level) {
475 case SOL_SOCKET:
476 printxval(scmvals, cmsg_type, "SCM_???");
477 switch (cmsg_type) {
478 case SCM_RIGHTS:
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000479 print_scm_rights(tcp, cmsg_data, data_len);
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000480 break;
481 case SCM_CREDENTIALS:
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000482 print_scm_creds(tcp, cmsg_data, data_len);
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000483 break;
484 case SCM_SECURITY:
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000485 print_scm_security(tcp, cmsg_data, data_len);
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000486 break;
487 }
488 break;
Dmitry V. Levin1f111cf2015-11-21 03:03:54 +0300489 case SOL_IP:
490 printxval(ip_cmsg_types, cmsg_type, "IP_???");
491 switch (cmsg_type) {
492 case IP_PKTINFO:
493 print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
494 break;
495 case IP_TTL:
496 print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
497 break;
498 case IP_TOS:
499 print_cmsg_ip_tos(tcp, cmsg_data, data_len);
500 break;
501 case IP_RECVOPTS:
502 case IP_RETOPTS:
503 print_cmsg_ip_opts(tcp, cmsg_data, data_len);
504 break;
505 case IP_RECVERR:
506 print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
507 break;
508 case IP_ORIGDSTADDR:
509 print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
510 break;
511 case IP_CHECKSUM:
512 print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
513 break;
514 case SCM_SECURITY:
515 print_scm_security(tcp, cmsg_data, data_len);
516 break;
517 }
518 break;
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000519 default:
520 tprintf("%u", cmsg_type);
521 }
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000522}
523
524static void
525printcmsghdr(struct tcb *tcp, unsigned long addr, size_t len)
526{
527 const size_t cmsg_size =
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000528#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
529 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
530#endif
531 sizeof(struct cmsghdr);
532
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000533 char *buf = len < cmsg_size ? NULL : malloc(len);
534 if (!buf || umoven(tcp, addr, len, buf) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000535 tprints(", msg_control=");
536 printaddr(addr);
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000537 free(buf);
Roland McGrath50770822004-10-06 22:11:51 +0000538 return;
539 }
540
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000541 union_cmsghdr u = { .ptr = buf };
542
543 tprints(", [");
544 while (len >= cmsg_size) {
545 size_t cmsg_len =
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000546#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000547 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000548#endif
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000549 u.cmsg->cmsg_len;
550 int cmsg_level =
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000551#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000552 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000553#endif
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000554 u.cmsg->cmsg_level;
555 int cmsg_type =
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000556#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000557 (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000558#endif
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000559 u.cmsg->cmsg_type;
Dmitry V. Levin05884d82015-01-24 01:08:57 +0000560
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000561 if (u.ptr != buf)
562 tprints(", ");
563 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
564 printxval(socketlayers, cmsg_level, "SOL_???");
565 tprints(", cmsg_type=");
Roland McGrath50770822004-10-06 22:11:51 +0000566
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000567 if (cmsg_len > len)
568 cmsg_len = len;
Roland McGrath96ad7b82005-02-02 03:11:32 +0000569
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000570 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
Dmitry V. Levin1103b4e2015-11-21 03:59:09 +0000571 (const void *) (u.ptr + cmsg_size),
572 cmsg_len > cmsg_size ? cmsg_len - cmsg_size: 0);
Dmitry V. Levinf54cb962015-11-19 00:29:19 +0000573 tprints("}");
574
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000575 if (cmsg_len < cmsg_size) {
576 len -= cmsg_size;
577 break;
Roland McGrath50770822004-10-06 22:11:51 +0000578 }
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000579 cmsg_len = (cmsg_len + current_wordsize - 1) &
580 (size_t) ~(current_wordsize - 1);
581 if (cmsg_len >= len) {
582 len = 0;
583 break;
584 }
585 u.ptr += cmsg_len;
586 len -= cmsg_len;
Roland McGrath50770822004-10-06 22:11:51 +0000587 }
Dmitry V. Levinb85a7f32015-01-24 15:20:31 +0000588 if (len)
589 tprints(", ...");
590 tprints("]");
591 free(buf);
Roland McGrath50770822004-10-06 22:11:51 +0000592}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000593
594static void
Denys Vlasenkoe0bc2222012-04-28 14:26:18 +0200595do_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
Andreas Schwab0873f292010-02-12 21:39:12 +0100596{
597 tprintf("{msg_name(%d)=", msg->msg_namelen);
598 printsock(tcp, (long)msg->msg_name, msg->msg_namelen);
599
600 tprintf(", msg_iov(%lu)=", (unsigned long)msg->msg_iovlen);
Denys Vlasenkoe0bc2222012-04-28 14:26:18 +0200601 tprint_iov_upto(tcp, (unsigned long)msg->msg_iovlen,
602 (unsigned long)msg->msg_iov, 1, data_size);
Andreas Schwab0873f292010-02-12 21:39:12 +0100603
604#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
605 tprintf(", msg_controllen=%lu", (unsigned long)msg->msg_controllen);
606 if (msg->msg_controllen)
607 printcmsghdr(tcp, (unsigned long) msg->msg_control,
608 msg->msg_controllen);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200609 tprints(", msg_flags=");
Andreas Schwab0873f292010-02-12 21:39:12 +0100610 printflags(msg_flags, msg->msg_flags, "MSG_???");
611#else /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
612 tprintf("msg_accrights=%#lx, msg_accrightslen=%u",
613 (unsigned long) msg->msg_accrights, msg->msg_accrightslen);
614#endif /* !HAVE_STRUCT_MSGHDR_MSG_CONTROL */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200615 tprints("}");
Andreas Schwab0873f292010-02-12 21:39:12 +0100616}
617
Denys Vlasenko3e759d42013-02-12 11:57:48 +0100618struct msghdr32 {
619 uint32_t /* void* */ msg_name;
620 uint32_t /* socklen_t */msg_namelen;
621 uint32_t /* iovec* */ msg_iov;
622 uint32_t /* size_t */ msg_iovlen;
623 uint32_t /* void* */ msg_control;
624 uint32_t /* size_t */ msg_controllen;
625 uint32_t /* int */ msg_flags;
626};
627struct mmsghdr32 {
628 struct msghdr32 msg_hdr;
629 uint32_t /* unsigned */ msg_len;
630};
631
Masatake YAMATOcaf6a432014-11-07 01:23:25 +0900632#ifndef HAVE_STRUCT_MMSGHDR
633struct mmsghdr {
634 struct msghdr msg_hdr;
635 unsigned msg_len;
636};
637#endif
638
Masatake YAMATOb2485432014-11-07 01:23:24 +0900639#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
640static void
641copy_from_msghdr32(struct msghdr *to_msg, struct msghdr32 *from_msg32)
642{
643 to_msg->msg_name = (void*)(long)from_msg32->msg_name;
644 to_msg->msg_namelen = from_msg32->msg_namelen;
645 to_msg->msg_iov = (void*)(long)from_msg32->msg_iov;
646 to_msg->msg_iovlen = from_msg32->msg_iovlen;
647 to_msg->msg_control = (void*)(long)from_msg32->msg_control;
648 to_msg->msg_controllen = from_msg32->msg_controllen;
649 to_msg->msg_flags = from_msg32->msg_flags;
650}
651#endif
652
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +0900653static bool
654extractmsghdr(struct tcb *tcp, long addr, struct msghdr *msg)
655{
656#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
657 if (current_wordsize == 4) {
658 struct msghdr32 msg32;
659
660 if (umove(tcp, addr, &msg32) < 0)
661 return false;
Masatake YAMATOb2485432014-11-07 01:23:24 +0900662 copy_from_msghdr32(msg, &msg32);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +0900663 } else
664#endif
665 if (umove(tcp, addr, msg) < 0)
666 return false;
667 return true;
668}
669
Masatake YAMATOa807dce2014-11-07 01:23:26 +0900670static bool
671extractmmsghdr(struct tcb *tcp, long addr, unsigned int idx, struct mmsghdr *mmsg)
672{
673#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
674 if (current_wordsize == 4) {
675 struct mmsghdr32 mmsg32;
676
677 addr += sizeof(struct mmsghdr32) * idx;
678 if (umove(tcp, addr, &mmsg32) < 0)
679 return false;
680
681 copy_from_msghdr32(&mmsg->msg_hdr, &mmsg32.msg_hdr);
682 mmsg->msg_len = mmsg32.msg_len;
683 } else
684#endif
685 {
686 addr += sizeof(*mmsg) * idx;
687 if (umove(tcp, addr, mmsg) < 0)
688 return false;
689 }
690 return true;
691}
692
Andreas Schwab0873f292010-02-12 21:39:12 +0100693static void
Denys Vlasenkoe0bc2222012-04-28 14:26:18 +0200694printmsghdr(struct tcb *tcp, long addr, unsigned long data_size)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000695{
696 struct msghdr msg;
697
Dmitry V. Levin00244b92015-01-28 01:52:25 +0000698 if (verbose(tcp) && extractmsghdr(tcp, addr, &msg))
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +0900699 do_msghdr(tcp, &msg, data_size);
700 else
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000701 printaddr(addr);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +0900702}
703
704void
Dmitry V. Levin93c9d1c2016-01-20 03:26:37 +0000705dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +0900706{
707 struct msghdr msg;
708
709 if (extractmsghdr(tcp, addr, &msg))
Dmitry V. Levin93c9d1c2016-01-20 03:26:37 +0000710 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000711}
712
Andreas Schwab0873f292010-02-12 21:39:12 +0100713static void
Dmitry V. Levin5ea97652012-05-01 20:41:40 +0000714printmmsghdr(struct tcb *tcp, long addr, unsigned int idx, unsigned long msg_len)
Andreas Schwab0873f292010-02-12 21:39:12 +0100715{
Masatake YAMATOcaf6a432014-11-07 01:23:25 +0900716 struct mmsghdr mmsg;
Andreas Schwab0873f292010-02-12 21:39:12 +0100717
Masatake YAMATOa807dce2014-11-07 01:23:26 +0900718 if (extractmmsghdr(tcp, addr, idx, &mmsg)) {
719 tprints("{");
720 do_msghdr(tcp, &mmsg.msg_hdr, msg_len ? msg_len : mmsg.msg_len);
721 tprintf(", %u}", mmsg.msg_len);
Andreas Schwab0873f292010-02-12 21:39:12 +0100722 }
Masatake YAMATOa807dce2014-11-07 01:23:26 +0900723 else
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000724 printaddr(addr);
Andreas Schwab0873f292010-02-12 21:39:12 +0100725}
Andreas Schwab0873f292010-02-12 21:39:12 +0100726
Dmitry V. Levin7af9f352012-03-11 23:59:29 +0000727static void
Dmitry V. Levin5ea97652012-05-01 20:41:40 +0000728decode_mmsg(struct tcb *tcp, unsigned long msg_len)
Dmitry V. Levin7af9f352012-03-11 23:59:29 +0000729{
730 /* mmsgvec */
731 if (syserror(tcp)) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000732 printaddr(tcp->u_arg[1]);
Dmitry V. Levin7af9f352012-03-11 23:59:29 +0000733 } else {
734 unsigned int len = tcp->u_rval;
735 unsigned int i;
736
737 tprints("{");
738 for (i = 0; i < len; ++i) {
739 if (i)
740 tprints(", ");
Dmitry V. Levin5ea97652012-05-01 20:41:40 +0000741 printmmsghdr(tcp, tcp->u_arg[1], i, msg_len);
Dmitry V. Levin7af9f352012-03-11 23:59:29 +0000742 }
743 tprints("}");
744 }
745 /* vlen */
746 tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
747 /* flags */
748 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
749}
750
Masatake YAMATOa807dce2014-11-07 01:23:26 +0900751void
752dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
753{
754 unsigned int len = tcp->u_rval;
755 unsigned int i;
756 struct mmsghdr mmsg;
757
758 for (i = 0; i < len; ++i) {
759 if (extractmmsghdr(tcp, addr, i, &mmsg)) {
760 tprintf(" = %lu buffers in vector %u\n",
761 (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
Dmitry V. Levind44707d2016-01-20 04:56:25 +0000762 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
763 (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);
Masatake YAMATOa807dce2014-11-07 01:23:26 +0900764 }
765 }
766}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000767
Dmitry V. Levin8a550d72008-11-10 17:21:23 +0000768/*
769 * low bits of the socket type define real socket type,
770 * other bits are socket type flags.
771 */
772static void
Dmitry V. Levin1e42f2d2014-09-10 17:48:28 +0000773tprint_sock_type(int flags)
Dmitry V. Levin8a550d72008-11-10 17:21:23 +0000774{
775 const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
776
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200777 if (str) {
Denys Vlasenko5940e652011-09-01 09:55:05 +0200778 tprints(str);
Dmitry V. Levin8a550d72008-11-10 17:21:23 +0000779 flags &= ~SOCK_TYPE_MASK;
780 if (!flags)
781 return;
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200782 tprints("|");
Dmitry V. Levin8a550d72008-11-10 17:21:23 +0000783 }
784 printflags(sock_type_flags, flags, "SOCK_???");
785}
786
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000787SYS_FUNC(socket)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000788{
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000789 printxval(addrfams, tcp->u_arg[0], "AF_???");
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000790 tprints(", ");
791 tprint_sock_type(tcp->u_arg[1]);
792 tprints(", ");
793 switch (tcp->u_arg[0]) {
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000794 case AF_INET:
795#ifdef AF_INET6
796 case AF_INET6:
Roland McGrath8758e542003-06-23 23:39:59 +0000797#endif
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000798 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
799 break;
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000800#ifdef AF_IPX
801 case AF_IPX:
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000802 /* BTW: I don't believe this.. */
803 tprints("[");
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000804 printxval(addrfams, tcp->u_arg[2], "AF_???");
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000805 tprints("]");
806 break;
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000807#endif /* AF_IPX */
808#ifdef AF_NETLINK
809 case AF_NETLINK:
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000810 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
811 break;
Masatake YAMATO2394a3d2014-03-11 23:37:37 +0900812#endif
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +0000813#if defined(AF_BLUETOOTH) && defined(HAVE_BLUETOOTH_BLUETOOTH_H)
814 case AF_BLUETOOTH:
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000815 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
816 break;
Lubomir Rintelc400a1c2014-10-03 11:40:28 +0200817#endif
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000818 default:
819 tprintf("%lu", tcp->u_arg[2]);
820 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000821 }
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000822
Dmitry V. Levin3d463be2015-08-02 01:41:26 +0000823 return RVAL_DECODED | RVAL_FD;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000824}
825
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000826SYS_FUNC(bind)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000827{
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000828 printfd(tcp, tcp->u_arg[0]);
829 tprints(", ");
830 printsock(tcp, tcp->u_arg[1], tcp->u_arg[2]);
831 tprintf(", %lu", tcp->u_arg[2]);
832
833 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000834}
835
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000836SYS_FUNC(listen)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000837{
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000838 printfd(tcp, tcp->u_arg[0]);
839 tprints(", ");
840 tprintf("%lu", tcp->u_arg[1]);
841
842 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000843}
844
Paolo Bonzini705ff102009-08-14 12:34:05 +0200845static int
Dmitry V. Levin15114ec2014-08-06 16:46:13 +0000846do_sockname(struct tcb *tcp, int flags_arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000847{
848 if (entering(tcp)) {
Philippe Ombredanne894c7e32014-02-01 09:57:45 -0800849 printfd(tcp, tcp->u_arg[0]);
850 tprints(", ");
Paolo Bonzini705ff102009-08-14 12:34:05 +0200851 return 0;
852 }
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000853
854 int len;
855 if (!tcp->u_arg[2] || !verbose(tcp) || syserror(tcp) ||
856 umove(tcp, tcp->u_arg[2], &len) < 0) {
857 printaddr(tcp->u_arg[1]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200858 tprints(", ");
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000859 printaddr(tcp->u_arg[2]);
860 } else {
861 printsock(tcp, tcp->u_arg[1], len);
862 tprintf(", [%d]", len);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000863 }
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000864
Paolo Bonzini705ff102009-08-14 12:34:05 +0200865 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200866 tprints(", ");
Paolo Bonzini705ff102009-08-14 12:34:05 +0200867 printflags(sock_type_flags, tcp->u_arg[flags_arg],
868 "SOCK_???");
869 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000870 return 0;
871}
872
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000873SYS_FUNC(accept)
Paolo Bonzini705ff102009-08-14 12:34:05 +0200874{
Dmitry V. Levin15114ec2014-08-06 16:46:13 +0000875 do_sockname(tcp, -1);
876 return RVAL_FD;
Paolo Bonzini705ff102009-08-14 12:34:05 +0200877}
878
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000879SYS_FUNC(accept4)
Paolo Bonzini705ff102009-08-14 12:34:05 +0200880{
Dmitry V. Levin15114ec2014-08-06 16:46:13 +0000881 do_sockname(tcp, 3);
882 return RVAL_FD;
Paolo Bonzini705ff102009-08-14 12:34:05 +0200883}
Paolo Bonzini705ff102009-08-14 12:34:05 +0200884
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000885SYS_FUNC(send)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000886{
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000887 printfd(tcp, tcp->u_arg[0]);
888 tprints(", ");
889 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
890 tprintf(", %lu, ", tcp->u_arg[2]);
891 /* flags */
892 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
893
894 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000895}
896
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000897SYS_FUNC(sendto)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000898{
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000899 printfd(tcp, tcp->u_arg[0]);
900 tprints(", ");
901 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
902 tprintf(", %lu, ", tcp->u_arg[2]);
903 /* flags */
904 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
905 /* to address */
906 tprints(", ");
907 printsock(tcp, tcp->u_arg[4], tcp->u_arg[5]);
908 /* to length */
909 tprintf(", %lu", tcp->u_arg[5]);
910
911 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000912}
913
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000914SYS_FUNC(sendmsg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000915{
Dmitry V. Levin219b3a72015-07-19 22:52:47 +0000916 printfd(tcp, tcp->u_arg[0]);
917 tprints(", ");
918 printmsghdr(tcp, tcp->u_arg[1], (unsigned long) -1L);
919 /* flags */
920 tprints(", ");
921 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
922
923 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000924}
925
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000926SYS_FUNC(sendmmsg)
Dmitry V. Levin7af9f352012-03-11 23:59:29 +0000927{
928 if (entering(tcp)) {
929 /* sockfd */
Philippe Ombredanne894c7e32014-02-01 09:57:45 -0800930 printfd(tcp, tcp->u_arg[0]);
931 tprints(", ");
Dmitry V. Levin7af9f352012-03-11 23:59:29 +0000932 if (!verbose(tcp)) {
933 tprintf("%#lx, %u, ",
934 tcp->u_arg[1], (unsigned int) tcp->u_arg[2]);
935 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
936 }
937 } else {
938 if (verbose(tcp))
Dmitry V. Levin5ea97652012-05-01 20:41:40 +0000939 decode_mmsg(tcp, (unsigned long) -1L);
Dmitry V. Levin7af9f352012-03-11 23:59:29 +0000940 }
941 return 0;
942}
943
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000944SYS_FUNC(recv)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000945{
946 if (entering(tcp)) {
Philippe Ombredanne894c7e32014-02-01 09:57:45 -0800947 printfd(tcp, tcp->u_arg[0]);
948 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000949 } else {
950 if (syserror(tcp))
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000951 printaddr(tcp->u_arg[1]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000952 else
953 printstr(tcp, tcp->u_arg[1], tcp->u_rval);
954
955 tprintf(", %lu, ", tcp->u_arg[2]);
Roland McGrathb2dee132005-06-01 19:02:36 +0000956 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000957 }
958 return 0;
959}
960
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000961SYS_FUNC(recvfrom)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000962{
963 int fromlen;
964
965 if (entering(tcp)) {
Philippe Ombredanne894c7e32014-02-01 09:57:45 -0800966 printfd(tcp, tcp->u_arg[0]);
967 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000968 } else {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000969 /* buf */
Denys Vlasenko383386d2015-07-30 13:29:20 +0200970 if (syserror(tcp)) {
Dmitry V. Levin52dc1502015-07-30 13:53:42 +0000971 printaddr(tcp->u_arg[1]);
Denys Vlasenko383386d2015-07-30 13:29:20 +0200972 } else {
973 printstr(tcp, tcp->u_arg[1], tcp->u_rval);
974 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000975 /* len */
976 tprintf(", %lu, ", tcp->u_arg[2]);
977 /* flags */
Roland McGrathb2dee132005-06-01 19:02:36 +0000978 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200979 tprints(", ");
Dmitry V. Levin52dc1502015-07-30 13:53:42 +0000980 if (syserror(tcp) || !tcp->u_arg[4] || !tcp->u_arg[5] ||
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000981 umove(tcp, tcp->u_arg[5], &fromlen) < 0) {
Denys Vlasenko383386d2015-07-30 13:29:20 +0200982 /* from address, len */
Dmitry V. Levin6c277da2015-07-19 22:36:46 +0000983 printaddr(tcp->u_arg[4]);
984 tprints(", ");
985 printaddr(tcp->u_arg[5]);
986 return 0;
987 }
Denys Vlasenko383386d2015-07-30 13:29:20 +0200988 /* from address */
989 printsock(tcp, tcp->u_arg[4], fromlen);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000990 /* from length */
991 tprintf(", [%u]", fromlen);
992 }
993 return 0;
994}
995
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000996SYS_FUNC(recvmsg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000997{
998 if (entering(tcp)) {
Philippe Ombredanne894c7e32014-02-01 09:57:45 -0800999 printfd(tcp, tcp->u_arg[0]);
1000 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001001 } else {
Dmitry V. Levin00244b92015-01-28 01:52:25 +00001002 if (syserror(tcp))
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001003 printaddr(tcp->u_arg[1]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001004 else
Denys Vlasenkoe0bc2222012-04-28 14:26:18 +02001005 printmsghdr(tcp, tcp->u_arg[1], tcp->u_rval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001006 /* flags */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001007 tprints(", ");
Roland McGrathb2dee132005-06-01 19:02:36 +00001008 printflags(msg_flags, tcp->u_arg[2], "MSG_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001009 }
1010 return 0;
1011}
1012
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001013SYS_FUNC(recvmmsg)
Andreas Schwab0873f292010-02-12 21:39:12 +01001014{
Dmitry V. Levin2950de32015-09-18 17:44:16 +00001015 static char str[sizeof("left") + TIMESPEC_TEXT_BUFSIZE];
Dmitry V. Levine6591032010-03-29 20:45:48 +04001016
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +01001017 if (entering(tcp)) {
Philippe Ombredanne894c7e32014-02-01 09:57:45 -08001018 printfd(tcp, tcp->u_arg[0]);
1019 tprints(", ");
Dmitry V. Levine6591032010-03-29 20:45:48 +04001020 if (verbose(tcp)) {
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +01001021 /* Abusing tcp->auxstr as temp storage.
Dmitry V. Levin2950de32015-09-18 17:44:16 +00001022 * Will be used and cleared on syscall exit.
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +01001023 */
Dmitry V. Levin2950de32015-09-18 17:44:16 +00001024 tcp->auxstr = sprint_timespec(tcp, tcp->u_arg[4]);
Dmitry V. Levine6591032010-03-29 20:45:48 +04001025 } else {
1026 tprintf("%#lx, %ld, ", tcp->u_arg[1], tcp->u_arg[2]);
1027 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001028 tprints(", ");
Dmitry V. Levine6591032010-03-29 20:45:48 +04001029 print_timespec(tcp, tcp->u_arg[4]);
1030 }
1031 return 0;
1032 } else {
1033 if (verbose(tcp)) {
Dmitry V. Levin5ea97652012-05-01 20:41:40 +00001034 decode_mmsg(tcp, 0);
Dmitry V. Levin2950de32015-09-18 17:44:16 +00001035 tprints(", ");
Dmitry V. Levine6591032010-03-29 20:45:48 +04001036 /* timeout on entrance */
Dmitry V. Levin2950de32015-09-18 17:44:16 +00001037 tprints(tcp->auxstr);
Dmitry V. Levine6591032010-03-29 20:45:48 +04001038 tcp->auxstr = NULL;
1039 }
1040 if (syserror(tcp))
1041 return 0;
1042 if (tcp->u_rval == 0) {
1043 tcp->auxstr = "Timeout";
1044 return RVAL_STR;
1045 }
1046 if (!verbose(tcp))
1047 return 0;
1048 /* timeout on exit */
Dmitry V. Levin2950de32015-09-18 17:44:16 +00001049 snprintf(str, sizeof(str), "left %s",
1050 sprint_timespec(tcp, tcp->u_arg[4]));
Dmitry V. Levine6591032010-03-29 20:45:48 +04001051 tcp->auxstr = str;
1052 return RVAL_STR;
Andreas Schwab0873f292010-02-12 21:39:12 +01001053 }
Andreas Schwab0873f292010-02-12 21:39:12 +01001054}
Andreas Schwab0873f292010-02-12 21:39:12 +01001055
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +00001056#include "xlat/shutdown_modes.h"
Sebastian Pipping9cd38502011-03-03 01:12:25 +01001057
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001058SYS_FUNC(shutdown)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001059{
Dmitry V. Levin219b3a72015-07-19 22:52:47 +00001060 printfd(tcp, tcp->u_arg[0]);
1061 tprints(", ");
1062 printxval(shutdown_modes, tcp->u_arg[1], "SHUT_???");
1063
1064 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001065}
1066
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001067SYS_FUNC(getsockname)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001068{
Dmitry V. Levin15114ec2014-08-06 16:46:13 +00001069 return do_sockname(tcp, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001070}
1071
Dmitry V. Levin2a4f0522015-08-02 01:54:48 +00001072static void
1073printpair_fd(struct tcb *tcp, const int i0, const int i1)
1074{
1075 tprints("[");
1076 printfd(tcp, i0);
1077 tprints(", ");
1078 printfd(tcp, i1);
1079 tprints("]");
1080}
1081
1082static void
1083decode_pair_fd(struct tcb *tcp, const long addr)
1084{
1085 int pair[2];
1086
1087 if (umove_or_printaddr(tcp, addr, &pair))
1088 return;
1089
1090 printpair_fd(tcp, pair[0], pair[1]);
1091}
1092
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001093static int
1094do_pipe(struct tcb *tcp, int flags_arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001095{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001096 if (exiting(tcp)) {
Dmitry V. Levin9b388852016-02-12 16:37:31 +00001097 decode_pair_fd(tcp, tcp->u_arg[0]);
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001098 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001099 tprints(", ");
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001100 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1101 }
1102 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001103 return 0;
1104}
1105
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001106SYS_FUNC(pipe)
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001107{
Dmitry V. Levin9b388852016-02-12 16:37:31 +00001108#ifdef HAVE_GETRVAL2
1109 if (exiting(tcp) && !syserror(tcp))
1110 printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
1111 return 0;
1112#else
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001113 return do_pipe(tcp, -1);
Dmitry V. Levin9b388852016-02-12 16:37:31 +00001114#endif
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001115}
1116
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001117SYS_FUNC(pipe2)
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001118{
1119 return do_pipe(tcp, 1);
1120}
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001121
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001122SYS_FUNC(socketpair)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001123{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124 if (entering(tcp)) {
Dmitry V. Levin71fe62e2016-04-04 01:35:28 +00001125 printxval(addrfams, tcp->u_arg[0], "AF_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001126 tprints(", ");
Dmitry V. Levin1e42f2d2014-09-10 17:48:28 +00001127 tprint_sock_type(tcp->u_arg[1]);
Dmitry V. Levin033fb912014-03-11 22:50:39 +00001128 tprintf(", %lu", tcp->u_arg[2]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001129 } else {
Dmitry V. Levinb6795082015-07-06 22:33:39 +00001130 tprints(", ");
Dmitry V. Levin2a4f0522015-08-02 01:54:48 +00001131 decode_pair_fd(tcp, tcp->u_arg[3]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001132 }
1133 return 0;
1134}
1135
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001136#include "xlat/sockoptions.h"
1137#include "xlat/sockipoptions.h"
Mike Frysinger54646b82015-08-19 13:29:27 -04001138#include "xlat/getsockipoptions.h"
1139#include "xlat/setsockipoptions.h"
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001140#include "xlat/sockipv6options.h"
Mike Frysinger54646b82015-08-19 13:29:27 -04001141#include "xlat/getsockipv6options.h"
1142#include "xlat/setsockipv6options.h"
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001143#include "xlat/sockipxoptions.h"
1144#include "xlat/sockrawoptions.h"
1145#include "xlat/sockpacketoptions.h"
1146#include "xlat/socksctpoptions.h"
1147#include "xlat/socktcpoptions.h"
1148
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001149static void
Dmitry V. Levinf40ea792016-04-01 01:03:20 +00001150print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
1151 unsigned int name, bool is_getsockopt)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001152{
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001153 printfd(tcp, fd);
1154 tprints(", ");
John Hughes38ae88d2002-05-23 11:48:58 +00001155 printxval(socketlayers, level, "SOL_??");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001156 tprints(", ");
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001157
John Hughes38ae88d2002-05-23 11:48:58 +00001158 switch (level) {
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001159 case SOL_SOCKET:
John Hughes38ae88d2002-05-23 11:48:58 +00001160 printxval(sockoptions, name, "SO_???");
John Hughes38ae88d2002-05-23 11:48:58 +00001161 break;
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001162 case SOL_IP:
Mike Frysinger54646b82015-08-19 13:29:27 -04001163 printxvals(name, "IP_???", sockipoptions,
1164 is_getsockopt ? getsockipoptions : setsockipoptions, NULL);
John Hughes38ae88d2002-05-23 11:48:58 +00001165 break;
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001166 case SOL_IPV6:
Mike Frysinger54646b82015-08-19 13:29:27 -04001167 printxvals(name, "IPV6_???", sockipv6options,
1168 is_getsockopt ? getsockipv6options : setsockipv6options, NULL);
Roland McGrath4f6ba692004-08-31 07:01:26 +00001169 break;
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001170 case SOL_IPX:
John Hughes38ae88d2002-05-23 11:48:58 +00001171 printxval(sockipxoptions, name, "IPX_???");
1172 break;
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001173 case SOL_PACKET:
John Hughes38ae88d2002-05-23 11:48:58 +00001174 printxval(sockpacketoptions, name, "PACKET_???");
John Hughes38ae88d2002-05-23 11:48:58 +00001175 break;
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001176 case SOL_TCP:
John Hughes38ae88d2002-05-23 11:48:58 +00001177 printxval(socktcpoptions, name, "TCP_???");
1178 break;
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001179 case SOL_SCTP:
Holger Hans Peter Freyther7fea79b2011-01-14 11:08:12 +01001180 printxval(socksctpoptions, name, "SCTP_???");
1181 break;
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001182 case SOL_RAW:
John Hughes38ae88d2002-05-23 11:48:58 +00001183 printxval(sockrawoptions, name, "RAW_???");
John Hughes38ae88d2002-05-23 11:48:58 +00001184 break;
John Hughes38ae88d2002-05-23 11:48:58 +00001185
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001186 /* Other SOL_* protocol levels still need work. */
John Hughes38ae88d2002-05-23 11:48:58 +00001187
Denys Vlasenko989ebc92012-03-17 04:42:07 +01001188 default:
John Hughes38ae88d2002-05-23 11:48:58 +00001189 tprintf("%u", name);
1190 }
1191
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001192 tprints(", ");
1193}
1194
1195#ifdef SO_LINGER
1196static void
1197print_linger(struct tcb *tcp, long addr, int len)
1198{
1199 struct linger linger;
1200
1201 if (len != sizeof(linger) ||
1202 umove(tcp, addr, &linger) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001203 printaddr(addr);
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001204 return;
1205 }
1206
1207 tprintf("{onoff=%d, linger=%d}",
1208 linger.l_onoff,
1209 linger.l_linger);
1210}
1211#endif /* SO_LINGER */
1212
1213#ifdef SO_PEERCRED
1214static void
1215print_ucred(struct tcb *tcp, long addr, int len)
1216{
1217 struct ucred uc;
1218
1219 if (len != sizeof(uc) ||
1220 umove(tcp, addr, &uc) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001221 printaddr(addr);
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001222 } else {
1223 tprintf("{pid=%u, uid=%u, gid=%u}",
1224 (unsigned) uc.pid,
1225 (unsigned) uc.uid,
1226 (unsigned) uc.gid);
1227 }
1228}
1229#endif /* SO_PEERCRED */
1230
1231#ifdef PACKET_STATISTICS
1232static void
1233print_tpacket_stats(struct tcb *tcp, long addr, int len)
1234{
1235 struct tpacket_stats stats;
1236
1237 if (len != sizeof(stats) ||
1238 umove(tcp, addr, &stats) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001239 printaddr(addr);
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001240 } else {
1241 tprintf("{packets=%u, drops=%u}",
1242 stats.tp_packets,
1243 stats.tp_drops);
1244 }
1245}
1246#endif /* PACKET_STATISTICS */
1247
1248#ifdef ICMP_FILTER
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001249# include "xlat/icmpfilterflags.h"
1250
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001251static void
1252print_icmp_filter(struct tcb *tcp, long addr, int len)
1253{
1254 struct icmp_filter filter;
1255
1256 if (len != sizeof(filter) ||
1257 umove(tcp, addr, &filter) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001258 printaddr(addr);
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001259 return;
1260 }
1261
1262 tprints("~(");
1263 printflags(icmpfilterflags, ~filter.data, "ICMP_???");
1264 tprints(")");
1265}
1266#endif /* ICMP_FILTER */
1267
1268static void
Dmitry V. Levinf40ea792016-04-01 01:03:20 +00001269print_getsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
1270 long addr, int len)
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001271{
1272 if (addr && verbose(tcp))
1273 switch (level) {
1274 case SOL_SOCKET:
1275 switch (name) {
1276#ifdef SO_LINGER
1277 case SO_LINGER:
1278 print_linger(tcp, addr, len);
1279 goto done;
1280#endif
1281#ifdef SO_PEERCRED
1282 case SO_PEERCRED:
1283 print_ucred(tcp, addr, len);
1284 goto done;
1285#endif
1286 }
1287 break;
1288
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001289 case SOL_PACKET:
1290 switch (name) {
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001291#ifdef PACKET_STATISTICS
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001292 case PACKET_STATISTICS:
1293 print_tpacket_stats(tcp, addr, len);
1294 goto done;
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001295#endif
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001296 }
1297 break;
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001298
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001299 case SOL_RAW:
1300 switch (name) {
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001301#ifdef ICMP_FILTER
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001302 case ICMP_FILTER:
1303 print_icmp_filter(tcp, addr, len);
1304 goto done;
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001305#endif
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001306 }
1307 break;
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001308 }
1309
John Hughes38ae88d2002-05-23 11:48:58 +00001310 /* default arg printing */
1311
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001312 if (verbose(tcp)) {
1313 if (len == sizeof(int)) {
1314 printnum_int(tcp, addr, "%d");
1315 } else {
1316 printstr(tcp, addr, len);
1317 }
1318 } else {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001319 printaddr(addr);
John Hughes38ae88d2002-05-23 11:48:58 +00001320 }
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001321done:
1322 tprintf(", [%d]", len);
1323}
1324
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001325SYS_FUNC(getsockopt)
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001326{
1327 if (entering(tcp)) {
1328 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
Mike Frysinger54646b82015-08-19 13:29:27 -04001329 tcp->u_arg[1], tcp->u_arg[2], true);
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001330 } else {
1331 int len;
1332
1333 if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &len) < 0) {
1334 tprintf("%#lx, %#lx",
1335 tcp->u_arg[3], tcp->u_arg[4]);
1336 } else {
1337 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1338 tcp->u_arg[3], len);
1339 }
John Hughes38ae88d2002-05-23 11:48:58 +00001340 }
1341 return 0;
1342}
1343
Dmitry V. Levin7bee4622015-06-08 14:19:46 +00001344#ifdef IP_ADD_MEMBERSHIP
1345static void
1346print_mreq(struct tcb *tcp, long addr, unsigned int len)
1347{
1348 struct ip_mreq mreq;
1349
1350 if (len < sizeof(mreq)) {
1351 printstr(tcp, addr, len);
1352 return;
1353 }
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001354 if (umove_or_printaddr(tcp, addr, &mreq))
Dmitry V. Levin7bee4622015-06-08 14:19:46 +00001355 return;
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001356
Dmitry V. Levin7bee4622015-06-08 14:19:46 +00001357 tprints("{imr_multiaddr=inet_addr(");
1358 print_quoted_string(inet_ntoa(mreq.imr_multiaddr),
1359 16, QUOTE_0_TERMINATED);
1360 tprints("), imr_interface=inet_addr(");
1361 print_quoted_string(inet_ntoa(mreq.imr_interface),
1362 16, QUOTE_0_TERMINATED);
1363 tprints(")}");
1364}
1365#endif /* IP_ADD_MEMBERSHIP */
1366
1367#ifdef IPV6_ADD_MEMBERSHIP
1368static void
1369print_mreq6(struct tcb *tcp, long addr, unsigned int len)
1370{
1371 struct ipv6_mreq mreq;
1372
1373 if (len < sizeof(mreq))
1374 goto fail;
1375
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001376 if (umove_or_printaddr(tcp, addr, &mreq))
Dmitry V. Levin7bee4622015-06-08 14:19:46 +00001377 return;
Dmitry V. Levin7bee4622015-06-08 14:19:46 +00001378
1379#ifdef HAVE_INET_NTOP
1380 const struct in6_addr *in6 = &mreq.ipv6mr_multiaddr;
1381 char address[INET6_ADDRSTRLEN];
1382
1383 if (!inet_ntop(AF_INET6, in6, address, sizeof(address)))
1384 goto fail;
1385
1386 tprints("{ipv6mr_multiaddr=inet_pton(");
1387 print_quoted_string(address, sizeof(address), QUOTE_0_TERMINATED);
1388 tprints("), ipv6mr_interface=");
1389 print_ifindex(mreq.ipv6mr_interface);
1390 tprints("}");
1391 return;
1392#endif /* HAVE_INET_NTOP */
1393
1394fail:
1395 printstr(tcp, addr, len);
1396}
1397#endif /* IPV6_ADD_MEMBERSHIP */
1398
Dmitry V. Levina0a49092015-01-30 01:55:20 +00001399#ifdef MCAST_JOIN_GROUP
1400static void
1401print_group_req(struct tcb *tcp, long addr, int len)
1402{
1403 struct group_req greq;
1404
1405 if (len != sizeof(greq) ||
1406 umove(tcp, addr, &greq) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001407 printaddr(addr);
Dmitry V. Levina0a49092015-01-30 01:55:20 +00001408 return;
1409 }
1410
Dmitry V. Levin1019f002015-11-21 02:38:59 +00001411 tprintf("{gr_interface=%u, gr_group=", greq.gr_interface);
1412 print_sockaddr(tcp, (const void *) &greq.gr_group,
1413 sizeof(greq.gr_group));
1414 tprintf("}");
Dmitry V. Levina0a49092015-01-30 01:55:20 +00001415
1416}
1417#endif /* MCAST_JOIN_GROUP */
1418
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001419#ifdef PACKET_RX_RING
1420static void
1421print_tpacket_req(struct tcb *tcp, long addr, int len)
1422{
1423 struct tpacket_req req;
1424
1425 if (len != sizeof(req) ||
1426 umove(tcp, addr, &req) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001427 printaddr(addr);
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001428 } else {
1429 tprintf("{block_size=%u, block_nr=%u, "
1430 "frame_size=%u, frame_nr=%u}",
1431 req.tp_block_size,
1432 req.tp_block_nr,
1433 req.tp_frame_size,
1434 req.tp_frame_nr);
1435 }
1436}
1437#endif /* PACKET_RX_RING */
1438
Dmitry V. Levin02f2f092015-01-29 03:26:53 +00001439#ifdef PACKET_ADD_MEMBERSHIP
1440# include "xlat/packet_mreq_type.h"
1441
1442static void
1443print_packet_mreq(struct tcb *tcp, long addr, int len)
1444{
1445 struct packet_mreq mreq;
1446
1447 if (len != sizeof(mreq) ||
1448 umove(tcp, addr, &mreq) < 0) {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001449 printaddr(addr);
Dmitry V. Levin02f2f092015-01-29 03:26:53 +00001450 } else {
1451 unsigned int i;
1452
1453 tprintf("{mr_ifindex=%u, mr_type=", mreq.mr_ifindex);
1454 printxval(packet_mreq_type, mreq.mr_type, "PACKET_MR_???");
1455 tprintf(", mr_alen=%u, mr_address=", mreq.mr_alen);
1456 if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
1457 mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
1458 for (i = 0; i < mreq.mr_alen; ++i)
1459 tprintf("%02x", mreq.mr_address[i]);
1460 tprints("}");
1461 }
1462}
1463#endif /* PACKET_ADD_MEMBERSHIP */
1464
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001465static void
Dmitry V. Levinf40ea792016-04-01 01:03:20 +00001466print_setsockopt(struct tcb *tcp, unsigned int level, unsigned int name,
1467 long addr, int len)
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001468{
1469 if (addr && verbose(tcp))
1470 switch (level) {
1471 case SOL_SOCKET:
1472 switch (name) {
1473#ifdef SO_LINGER
1474 case SO_LINGER:
1475 print_linger(tcp, addr, len);
1476 goto done;
1477#endif
1478 }
1479 break;
1480
Dmitry V. Levina0a49092015-01-30 01:55:20 +00001481 case SOL_IP:
1482 switch (name) {
Dmitry V. Levin7bee4622015-06-08 14:19:46 +00001483#ifdef IP_ADD_MEMBERSHIP
1484 case IP_ADD_MEMBERSHIP:
1485 case IP_DROP_MEMBERSHIP:
1486 print_mreq(tcp, addr, len);
1487 goto done;
1488#endif /* IP_ADD_MEMBERSHIP */
Dmitry V. Levina0a49092015-01-30 01:55:20 +00001489#ifdef MCAST_JOIN_GROUP
1490 case MCAST_JOIN_GROUP:
1491 case MCAST_LEAVE_GROUP:
1492 print_group_req(tcp, addr, len);
1493 goto done;
1494#endif /* MCAST_JOIN_GROUP */
1495 }
Dmitry V. Levin3910d0f2015-02-08 01:19:06 +00001496 break;
Dmitry V. Levina0a49092015-01-30 01:55:20 +00001497
Dmitry V. Levin7bee4622015-06-08 14:19:46 +00001498 case SOL_IPV6:
1499 switch (name) {
1500#ifdef IPV6_ADD_MEMBERSHIP
1501 case IPV6_ADD_MEMBERSHIP:
1502 case IPV6_DROP_MEMBERSHIP:
1503# ifdef IPV6_JOIN_ANYCAST
1504 case IPV6_JOIN_ANYCAST:
1505# endif
1506# ifdef IPV6_LEAVE_ANYCAST
1507 case IPV6_LEAVE_ANYCAST:
1508# endif
1509 print_mreq6(tcp, addr, len);
1510 goto done;
1511#endif /* IPV6_ADD_MEMBERSHIP */
1512 }
1513 break;
1514
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001515 case SOL_PACKET:
1516 switch (name) {
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001517#ifdef PACKET_RX_RING
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001518 case PACKET_RX_RING:
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001519# ifdef PACKET_TX_RING
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001520 case PACKET_TX_RING:
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001521# endif
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001522 print_tpacket_req(tcp, addr, len);
1523 goto done;
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001524#endif /* PACKET_RX_RING */
1525#ifdef PACKET_ADD_MEMBERSHIP
Dmitry V. Levin02f2f092015-01-29 03:26:53 +00001526 case PACKET_ADD_MEMBERSHIP:
1527 case PACKET_DROP_MEMBERSHIP:
1528 print_packet_mreq(tcp, addr, len);
1529 goto done;
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001530#endif /* PACKET_ADD_MEMBERSHIP */
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001531 }
1532 break;
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001533
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001534 case SOL_RAW:
1535 switch (name) {
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001536#ifdef ICMP_FILTER
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001537 case ICMP_FILTER:
1538 print_icmp_filter(tcp, addr, len);
1539 goto done;
Dmitry V. Levin6d31c7a2015-01-29 04:42:37 +00001540#endif
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001541 }
1542 break;
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001543 }
1544
1545 /* default arg printing */
1546
1547 if (verbose(tcp)) {
1548 if (len == sizeof(int)) {
1549 printnum_int(tcp, addr, "%d");
1550 } else {
1551 printstr(tcp, addr, len);
1552 }
1553 } else {
Dmitry V. Levin6c277da2015-07-19 22:36:46 +00001554 printaddr(addr);
Dmitry V. Levin0509c8e2015-01-29 03:11:41 +00001555 }
1556done:
1557 tprintf(", %d", len);
1558}
1559
Dmitry V. Levina0bd3742015-04-07 01:36:50 +00001560SYS_FUNC(setsockopt)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001561{
Dmitry V. Levin219b3a72015-07-19 22:52:47 +00001562 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
Mike Frysinger54646b82015-08-19 13:29:27 -04001563 tcp->u_arg[1], tcp->u_arg[2], false);
Dmitry V. Levin219b3a72015-07-19 22:52:47 +00001564 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1565 tcp->u_arg[3], tcp->u_arg[4]);
1566
1567 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001568}