xref: /aosp_15_r20/external/strace/net.c (revision cf84ac9a129d8ea9952db616b4e9b904c4bdde56)
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <[email protected]>
3  * Copyright (c) 1993 Branko Lankester <[email protected]>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <[email protected]>
5  * Copyright (c) 1996-2000 Wichert Akkerman <[email protected]>
6  * Copyright (c) 1999-2018 The strace developers.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "defs.h"
33 #include "print_fields.h"
34 
35 #include <sys/stat.h>
36 #include <sys/socket.h>
37 #include <sys/uio.h>
38 #include <sys/un.h>
39 #include <netinet/in.h>
40 #ifdef HAVE_NETINET_TCP_H
41 # include <netinet/tcp.h>
42 #endif
43 #ifdef HAVE_NETINET_UDP_H
44 # include <netinet/udp.h>
45 #endif
46 #ifdef HAVE_NETINET_SCTP_H
47 # include <netinet/sctp.h>
48 #endif
49 #include <arpa/inet.h>
50 #include <net/if.h>
51 #include <asm/types.h>
52 #ifdef HAVE_NETIPX_IPX_H
53 # include <netipx/ipx.h>
54 #endif
55 
56 #if defined(HAVE_LINUX_IP_VS_H)
57 # include <linux/ip_vs.h>
58 #endif
59 #include "netlink.h"
60 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
61 # include <linux/netfilter_arp/arp_tables.h>
62 #endif
63 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
64 # include <linux/netfilter_bridge/ebtables.h>
65 #endif
66 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
67 # include <linux/netfilter_ipv4/ip_tables.h>
68 #endif
69 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
70 # include <linux/netfilter_ipv6/ip6_tables.h>
71 #endif
72 #include <linux/if_packet.h>
73 #include <linux/icmp.h>
74 
75 #include "xlat/socktypes.h"
76 #include "xlat/sock_type_flags.h"
77 #ifndef SOCK_TYPE_MASK
78 # define SOCK_TYPE_MASK 0xf
79 #endif
80 
81 #include "xlat/socketlayers.h"
82 
83 #include "xlat/inet_protocols.h"
84 
85 #define XLAT_MACROS_ONLY
86 # include "xlat/addrfams.h"
87 # include "xlat/ethernet_protocols.h"
88 #undef XLAT_MACROS_ONLY
89 #include "xlat/ax25_protocols.h"
90 #include "xlat/irda_protocols.h"
91 #include "xlat/can_protocols.h"
92 #include "xlat/bt_protocols.h"
93 #include "xlat/isdn_protocols.h"
94 #include "xlat/phonet_protocols.h"
95 #include "xlat/caif_protocols.h"
96 #include "xlat/nfc_protocols.h"
97 #include "xlat/kcm_protocols.h"
98 #include "xlat/smc_protocols.h"
99 
100 const size_t inet_protocols_size = ARRAY_SIZE(inet_protocols) - 1;
101 
102 static void
decode_sockbuf(struct tcb * const tcp,const int fd,const kernel_ulong_t addr,const kernel_ulong_t addrlen)103 decode_sockbuf(struct tcb *const tcp, const int fd, const kernel_ulong_t addr,
104 	       const kernel_ulong_t addrlen)
105 {
106 
107 	switch (verbose(tcp) ? getfdproto(tcp, fd) : SOCK_PROTO_UNKNOWN) {
108 	case SOCK_PROTO_NETLINK:
109 		decode_netlink(tcp, fd, addr, addrlen);
110 		break;
111 	default:
112 		printstrn(tcp, addr, addrlen);
113 	}
114 }
115 
116 /*
117  * low bits of the socket type define real socket type,
118  * other bits are socket type flags.
119  */
120 static void
tprint_sock_type(unsigned int flags)121 tprint_sock_type(unsigned int flags)
122 {
123 	const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
124 
125 	if (str) {
126 		print_xlat_ex(flags & SOCK_TYPE_MASK, str, XLAT_STYLE_DEFAULT);
127 		flags &= ~SOCK_TYPE_MASK;
128 		if (!flags)
129 			return;
130 		tprints("|");
131 	}
132 	printflags(sock_type_flags, flags, "SOCK_???");
133 }
134 
SYS_FUNC(socket)135 SYS_FUNC(socket)
136 {
137 	printxval(addrfams, tcp->u_arg[0], "AF_???");
138 	tprints(", ");
139 	tprint_sock_type(tcp->u_arg[1]);
140 	tprints(", ");
141 	switch (tcp->u_arg[0]) {
142 	case AF_INET:
143 	case AF_INET6:
144 		printxval_search(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
145 		break;
146 
147 	case AF_AX25:
148 		/* Those are not available in public headers.  */
149 		printxval_searchn_ex(ARRSZ_PAIR(ax25_protocols), tcp->u_arg[2],
150 				     "AX25_P_???", XLAT_STYLE_VERBOSE);
151 		break;
152 
153 	case AF_NETLINK:
154 		printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
155 		break;
156 
157 	case AF_PACKET:
158 		tprints("htons(");
159 		printxval_searchn(ethernet_protocols, ethernet_protocols_size,
160 				  ntohs(tcp->u_arg[2]), "ETH_P_???");
161 		tprints(")");
162 		break;
163 
164 	case AF_IRDA:
165 		printxval_index(can_protocols, tcp->u_arg[2], "IRDAPROTO_???");
166 		break;
167 
168 	case AF_CAN:
169 		printxval_index(can_protocols, tcp->u_arg[2], "CAN_???");
170 		break;
171 
172 	case AF_BLUETOOTH:
173 		printxval_index(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
174 		break;
175 
176 	case AF_RXRPC:
177 		printxval(addrfams, tcp->u_arg[2], "AF_???");
178 		break;
179 
180 	case AF_ISDN:
181 		printxval(isdn_protocols, tcp->u_arg[2], "ISDN_P_???");
182 		break;
183 
184 	case AF_PHONET:
185 		printxval_index(phonet_protocols, tcp->u_arg[2], "PN_PROTO_???");
186 		break;
187 
188 	case AF_CAIF:
189 		printxval_index(caif_protocols, tcp->u_arg[2], "CAIFPROTO_???");
190 		break;
191 
192 	case AF_NFC:
193 		printxval_index(nfc_protocols, tcp->u_arg[2],
194 				"NFC_SOCKPROTO_???");
195 		break;
196 
197 	case AF_KCM:
198 		printxval_index(kcm_protocols, tcp->u_arg[2], "KCMPROTO_???");
199 		break;
200 
201 	case AF_SMC:
202 		printxval_index(smc_protocols, tcp->u_arg[2], "SMCPROTO_???");
203 		break;
204 
205 	default:
206 		tprintf("%" PRI_klu, tcp->u_arg[2]);
207 		break;
208 	}
209 
210 	return RVAL_DECODED | RVAL_FD;
211 }
212 
213 static bool
fetch_socklen(struct tcb * const tcp,int * const plen,const kernel_ulong_t sockaddr,const kernel_ulong_t socklen)214 fetch_socklen(struct tcb *const tcp, int *const plen,
215 	      const kernel_ulong_t sockaddr, const kernel_ulong_t socklen)
216 {
217 	return verbose(tcp) && sockaddr && socklen
218 	       && umove(tcp, socklen, plen) == 0;
219 }
220 
221 static int
decode_sockname(struct tcb * tcp)222 decode_sockname(struct tcb *tcp)
223 {
224 	int ulen, rlen;
225 
226 	if (entering(tcp)) {
227 		printfd(tcp, tcp->u_arg[0]);
228 		tprints(", ");
229 		if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
230 			set_tcb_priv_ulong(tcp, ulen);
231 			return 0;
232 		} else {
233 			printaddr(tcp->u_arg[1]);
234 			tprints(", ");
235 			printaddr(tcp->u_arg[2]);
236 			return RVAL_DECODED;
237 		}
238 	}
239 
240 	ulen = get_tcb_priv_ulong(tcp);
241 
242 	if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
243 		printaddr(tcp->u_arg[1]);
244 		tprintf(", [%d]", ulen);
245 	} else {
246 		decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
247 		if (ulen != rlen)
248 			tprintf(", [%d->%d]", ulen, rlen);
249 		else
250 			tprintf(", [%d]", rlen);
251 	}
252 
253 	return RVAL_DECODED;
254 }
255 
SYS_FUNC(accept)256 SYS_FUNC(accept)
257 {
258 	return decode_sockname(tcp) | RVAL_FD;
259 }
260 
SYS_FUNC(accept4)261 SYS_FUNC(accept4)
262 {
263 	int rc = decode_sockname(tcp);
264 
265 	if (rc & RVAL_DECODED) {
266 		tprints(", ");
267 		printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
268 	}
269 
270 	return rc | RVAL_FD;
271 }
272 
SYS_FUNC(send)273 SYS_FUNC(send)
274 {
275 	printfd(tcp, tcp->u_arg[0]);
276 	tprints(", ");
277 	decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
278 	tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
279 	/* flags */
280 	printflags(msg_flags, tcp->u_arg[3], "MSG_???");
281 
282 	return RVAL_DECODED;
283 }
284 
SYS_FUNC(sendto)285 SYS_FUNC(sendto)
286 {
287 	printfd(tcp, tcp->u_arg[0]);
288 	tprints(", ");
289 	decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
290 	tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
291 	/* flags */
292 	printflags(msg_flags, tcp->u_arg[3], "MSG_???");
293 	/* to address */
294 	const int addrlen = tcp->u_arg[5];
295 	tprints(", ");
296 	decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
297 	/* to length */
298 	tprintf(", %d", addrlen);
299 
300 	return RVAL_DECODED;
301 }
302 
SYS_FUNC(recv)303 SYS_FUNC(recv)
304 {
305 	if (entering(tcp)) {
306 		printfd(tcp, tcp->u_arg[0]);
307 		tprints(", ");
308 	} else {
309 		if (syserror(tcp)) {
310 			printaddr(tcp->u_arg[1]);
311 		} else {
312 			decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
313 				     tcp->u_rval);
314 		}
315 
316 		tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
317 		printflags(msg_flags, tcp->u_arg[3], "MSG_???");
318 	}
319 	return 0;
320 }
321 
SYS_FUNC(recvfrom)322 SYS_FUNC(recvfrom)
323 {
324 	int ulen, rlen;
325 
326 	if (entering(tcp)) {
327 		printfd(tcp, tcp->u_arg[0]);
328 		tprints(", ");
329 		if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
330 			set_tcb_priv_ulong(tcp, ulen);
331 		}
332 	} else {
333 		/* buf */
334 		if (syserror(tcp)) {
335 			printaddr(tcp->u_arg[1]);
336 		} else {
337 			decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
338 				     tcp->u_rval);
339 		}
340 		/* size */
341 		tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
342 		/* flags */
343 		printflags(msg_flags, tcp->u_arg[3], "MSG_???");
344 		tprints(", ");
345 
346 		ulen = get_tcb_priv_ulong(tcp);
347 
348 		if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
349 			/* from address */
350 			printaddr(tcp->u_arg[4]);
351 			tprints(", ");
352 			/* from length */
353 			printaddr(tcp->u_arg[5]);
354 			return 0;
355 		}
356 		if (syserror(tcp)) {
357 			/* from address */
358 			printaddr(tcp->u_arg[4]);
359 			/* from length */
360 			tprintf(", [%d]", ulen);
361 			return 0;
362 		}
363 		/* from address */
364 		decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
365 		/* from length */
366 		if (ulen != rlen)
367 			tprintf(", [%d->%d]", ulen, rlen);
368 		else
369 			tprintf(", [%d]", rlen);
370 	}
371 	return 0;
372 }
373 
SYS_FUNC(getsockname)374 SYS_FUNC(getsockname)
375 {
376 	return decode_sockname(tcp);
377 }
378 
379 static void
printpair_fd(struct tcb * tcp,const int i0,const int i1)380 printpair_fd(struct tcb *tcp, const int i0, const int i1)
381 {
382 	tprints("[");
383 	printfd(tcp, i0);
384 	tprints(", ");
385 	printfd(tcp, i1);
386 	tprints("]");
387 }
388 
389 static void
decode_pair_fd(struct tcb * const tcp,const kernel_ulong_t addr)390 decode_pair_fd(struct tcb *const tcp, const kernel_ulong_t addr)
391 {
392 	int pair[2];
393 
394 	if (umove_or_printaddr(tcp, addr, &pair))
395 		return;
396 
397 	printpair_fd(tcp, pair[0], pair[1]);
398 }
399 
400 static int
do_pipe(struct tcb * tcp,int flags_arg)401 do_pipe(struct tcb *tcp, int flags_arg)
402 {
403 	if (exiting(tcp)) {
404 		decode_pair_fd(tcp, tcp->u_arg[0]);
405 		if (flags_arg >= 0) {
406 			tprints(", ");
407 			printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
408 		}
409 	}
410 	return 0;
411 }
412 
SYS_FUNC(pipe)413 SYS_FUNC(pipe)
414 {
415 #if HAVE_ARCH_GETRVAL2
416 	if (exiting(tcp) && !syserror(tcp))
417 		printpair_fd(tcp, tcp->u_rval, getrval2(tcp));
418 	return 0;
419 #else
420 	return do_pipe(tcp, -1);
421 #endif
422 }
423 
SYS_FUNC(pipe2)424 SYS_FUNC(pipe2)
425 {
426 	return do_pipe(tcp, 1);
427 }
428 
SYS_FUNC(socketpair)429 SYS_FUNC(socketpair)
430 {
431 	if (entering(tcp)) {
432 		printxval(addrfams, tcp->u_arg[0], "AF_???");
433 		tprints(", ");
434 		tprint_sock_type(tcp->u_arg[1]);
435 		tprintf(", %" PRI_klu, tcp->u_arg[2]);
436 	} else {
437 		tprints(", ");
438 		decode_pair_fd(tcp, tcp->u_arg[3]);
439 	}
440 	return 0;
441 }
442 
443 #include "xlat/sock_options.h"
444 #include "xlat/getsock_options.h"
445 #include "xlat/setsock_options.h"
446 #include "xlat/sock_ip_options.h"
447 #include "xlat/getsock_ip_options.h"
448 #include "xlat/setsock_ip_options.h"
449 #include "xlat/sock_ipv6_options.h"
450 #include "xlat/getsock_ipv6_options.h"
451 #include "xlat/setsock_ipv6_options.h"
452 #include "xlat/sock_ipx_options.h"
453 #include "xlat/sock_ax25_options.h"
454 #include "xlat/sock_netlink_options.h"
455 #include "xlat/sock_packet_options.h"
456 #include "xlat/sock_raw_options.h"
457 #include "xlat/sock_sctp_options.h"
458 #include "xlat/sock_tcp_options.h"
459 #include "xlat/sock_udp_options.h"
460 #include "xlat/sock_irda_options.h"
461 #include "xlat/sock_llc_options.h"
462 #include "xlat/sock_dccp_options.h"
463 #include "xlat/sock_tipc_options.h"
464 #include "xlat/sock_rxrpc_options.h"
465 #include "xlat/sock_pppol2tp_options.h"
466 #include "xlat/sock_bluetooth_options.h"
467 #include "xlat/sock_pnp_options.h"
468 #include "xlat/sock_rds_options.h"
469 #include "xlat/sock_iucv_options.h"
470 #include "xlat/sock_caif_options.h"
471 #include "xlat/sock_alg_options.h"
472 #include "xlat/sock_nfcllcp_options.h"
473 #include "xlat/sock_kcm_options.h"
474 #include "xlat/sock_tls_options.h"
475 #include "xlat/sock_xdp_options.h"
476 
477 static void
print_sockopt_fd_level_name(struct tcb * tcp,int fd,unsigned int level,unsigned int name,bool is_getsockopt)478 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
479 			    unsigned int name, bool is_getsockopt)
480 {
481 	printfd(tcp, fd);
482 	tprints(", ");
483 	printxval_search(socketlayers, level, "SOL_??");
484 	tprints(", ");
485 
486 	switch (level) {
487 	case SOL_SOCKET:
488 		printxvals(name, "SO_???", sock_options,
489 			   is_getsockopt ? getsock_options :
490 					   setsock_options, NULL);
491 		break;
492 	case SOL_IP:
493 		printxvals(name, "IP_???", sock_ip_options,
494 			   is_getsockopt ? getsock_ip_options :
495 					   setsock_ip_options, NULL);
496 		break;
497 	case SOL_IPV6:
498 		printxvals(name, "IPV6_???", sock_ipv6_options,
499 			   is_getsockopt ? getsock_ipv6_options :
500 					   setsock_ipv6_options, NULL);
501 		break;
502 	case SOL_IPX:
503 		printxval(sock_ipx_options, name, "IPX_???");
504 		break;
505 	case SOL_AX25:
506 		printxval_search(sock_ax25_options, name, "AX25_???");
507 		break;
508 	case SOL_PACKET:
509 		printxval(sock_packet_options, name, "PACKET_???");
510 		break;
511 	case SOL_TCP:
512 		printxval_index(sock_tcp_options, name, "TCP_???");
513 		break;
514 	case SOL_SCTP:
515 		printxval(sock_sctp_options, name, "SCTP_???");
516 		break;
517 	case SOL_RAW:
518 		printxval(sock_raw_options, name, "RAW_???");
519 		break;
520 	case SOL_NETLINK:
521 		printxval(sock_netlink_options, name, "NETLINK_???");
522 		break;
523 	case SOL_UDP:
524 		printxval(sock_udp_options, name, "UDP_???");
525 		break;
526 	case SOL_IRDA:
527 		printxval_index(sock_irda_options, name, "IRLMP_???");
528 		break;
529 	case SOL_LLC:
530 		printxval_index(sock_llc_options, name, "LLC_OPT_???");
531 		break;
532 	case SOL_DCCP:
533 		printxval_search(sock_dccp_options, name, "DCCP_SOCKOPT_???");
534 		break;
535 	case SOL_TIPC:
536 		printxval_search(sock_tipc_options, name, "TIPC_???");
537 		break;
538 	case SOL_RXRPC:
539 		printxval_index(sock_rxrpc_options, name, "RXRPC_???");
540 		break;
541 	case SOL_PPPOL2TP:
542 		printxval_index(sock_pppol2tp_options, name, "PPPOL2TP_SO_???");
543 		break;
544 	case SOL_BLUETOOTH:
545 		printxval_search(sock_bluetooth_options, name, "BT_???");
546 		break;
547 	case SOL_PNPIPE:
548 		printxval(sock_pnp_options, name, "PNPIPE_???");
549 		break;
550 	case SOL_RDS:
551 		printxval_search(sock_rds_options, name, "RDS_???");
552 		break;
553 	case SOL_IUCV:
554 		printxval(sock_iucv_options, name, "SO_???");
555 		break;
556 	case SOL_CAIF:
557 		printxval(sock_caif_options, name, "CAIFSO_???");
558 		break;
559 	case SOL_ALG:
560 		printxval_index(sock_alg_options, name, "ALG_???");
561 		break;
562 	case SOL_NFC:
563 		printxval_index(sock_nfcllcp_options, name, "NFC_LLCP_???");
564 		break;
565 	case SOL_KCM:
566 		printxval(sock_kcm_options, name, "KCM_???");
567 		break;
568 	case SOL_TLS:
569 		printxval(sock_tls_options, name, "TLS_???");
570 		break;
571 	case SOL_XDP:
572 		printxval_index(sock_xdp_options, name, "XDP_???");
573 		break;
574 
575 		/* Other SOL_* protocol levels still need work. */
576 
577 	default:
578 		tprintf("%u", name);
579 	}
580 
581 	tprints(", ");
582 }
583 
584 static void
print_get_linger(struct tcb * const tcp,const kernel_ulong_t addr,unsigned int len)585 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
586 		 unsigned int len)
587 {
588 	struct linger linger;
589 
590 	/*
591 	 * The kernel cannot return len > sizeof(linger) because struct linger
592 	 * cannot change, but extra safety won't harm either.
593 	 */
594 	if (len > sizeof(linger))
595 		len = sizeof(linger);
596 	if (umoven_or_printaddr(tcp, addr, len, &linger))
597 		return;
598 
599 	if (len < sizeof(linger.l_onoff)) {
600 		tprints("{l_onoff=");
601 		print_quoted_string((void *) &linger.l_onoff,
602 				    len, QUOTE_FORCE_HEX);
603 	} else {
604 		PRINT_FIELD_D("{", linger, l_onoff);
605 
606 		if (len > offsetof(struct linger, l_linger)) {
607 			len -= offsetof(struct linger, l_linger);
608 			if (len < sizeof(linger.l_linger)) {
609 				tprints(", l_linger=");
610 				print_quoted_string((void *) &linger.l_linger,
611 						    len, QUOTE_FORCE_HEX);
612 			} else {
613 				PRINT_FIELD_D(", ", linger, l_linger);
614 			}
615 		}
616 	}
617 	tprints("}");
618 }
619 
620 static void
print_get_ucred(struct tcb * const tcp,const kernel_ulong_t addr,unsigned int len)621 print_get_ucred(struct tcb *const tcp, const kernel_ulong_t addr,
622 		unsigned int len)
623 {
624 	struct ucred uc;
625 
626 	/*
627 	 * The kernel is very unlikely to return len > sizeof(uc)
628 	 * because struct ucred is very unlikely to change,
629 	 * but extra safety won't harm either.
630 	 */
631 	if (len > sizeof(uc))
632 		len = sizeof(uc);
633 
634 	if (umoven_or_printaddr(tcp, addr, len, &uc))
635 		return;
636 
637 	if (len < sizeof(uc.pid)) {
638 		tprints("{pid=");
639 		print_quoted_string((void *) &uc.pid,
640 				    len, QUOTE_FORCE_HEX);
641 	} else {
642 		PRINT_FIELD_D("{", uc, pid);
643 
644 		if (len > offsetof(struct ucred, uid)) {
645 			len -= offsetof(struct ucred, uid);
646 			if (len < sizeof(uc.uid)) {
647 				tprints(", uid=");
648 				print_quoted_string((void *) &uc.uid,
649 						    len, QUOTE_FORCE_HEX);
650 			} else {
651 				PRINT_FIELD_UID(", ", uc, uid);
652 
653 				if (len > offsetof(struct ucred, gid) -
654 					  offsetof(struct ucred, uid)) {
655 					len -= offsetof(struct ucred, gid) -
656 					       offsetof(struct ucred, uid);
657 					if (len < sizeof(uc.gid)) {
658 						tprints(", gid=");
659 						print_quoted_string((void *) &uc.gid,
660 								    len,
661 								    QUOTE_FORCE_HEX);
662 					} else {
663 						PRINT_FIELD_UID(", ", uc, gid);
664 					}
665 				}
666 			}
667 		}
668 	}
669 	tprints("}");
670 }
671 
672 #ifdef PACKET_STATISTICS
673 static void
print_tpacket_stats(struct tcb * const tcp,const kernel_ulong_t addr,unsigned int len)674 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
675 		    unsigned int len)
676 {
677 	struct tp_stats {
678 		unsigned int tp_packets, tp_drops, tp_freeze_q_cnt;
679 	} stats;
680 
681 	/*
682 	 * The kernel may return len > sizeof(stats) if the kernel structure
683 	 * grew as it happened when tpacket_stats_v3 was introduced.
684 	 */
685 	if (len > sizeof(stats))
686 		len = sizeof(stats);
687 
688 	if (umoven_or_printaddr(tcp, addr, len, &stats))
689 		return;
690 
691 	if (len < sizeof(stats.tp_packets)) {
692 		tprints("{tp_packets=");
693 		print_quoted_string((void *) &stats.tp_packets,
694 				    len, QUOTE_FORCE_HEX);
695 	} else {
696 		PRINT_FIELD_U("{", stats, tp_packets);
697 
698 		if (len > offsetof(struct tp_stats, tp_drops)) {
699 			len -= offsetof(struct tp_stats, tp_drops);
700 			if (len < sizeof(stats.tp_drops)) {
701 				tprints(", tp_drops=");
702 				print_quoted_string((void *) &stats.tp_drops,
703 						    len, QUOTE_FORCE_HEX);
704 			} else {
705 				PRINT_FIELD_U(", ", stats, tp_drops);
706 
707 				if (len > offsetof(struct tp_stats, tp_freeze_q_cnt) -
708 					  offsetof(struct tp_stats, tp_drops)) {
709 					len -= offsetof(struct tp_stats, tp_freeze_q_cnt) -
710 					       offsetof(struct tp_stats, tp_drops);
711 					if (len < sizeof(stats.tp_freeze_q_cnt)) {
712 						tprints(", tp_freeze_q_cnt=");
713 						print_quoted_string((void *) &stats.tp_freeze_q_cnt,
714 								    len,
715 								    QUOTE_FORCE_HEX);
716 					} else {
717 						PRINT_FIELD_U(", ", stats, tp_freeze_q_cnt);
718 					}
719 				}
720 			}
721 		}
722 	}
723 	tprints("}");
724 }
725 #endif /* PACKET_STATISTICS */
726 
727 #include "xlat/icmpfilterflags.h"
728 
729 static void
print_icmp_filter(struct tcb * const tcp,const kernel_ulong_t addr,int len)730 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
731 {
732 	struct icmp_filter filter = {};
733 
734 	if (len > (int) sizeof(filter))
735 		len = sizeof(filter);
736 	else if (len <= 0) {
737 		printaddr(addr);
738 		return;
739 	}
740 
741 	if (umoven_or_printaddr(tcp, addr, len, &filter))
742 		return;
743 
744 	tprints("~(");
745 	printflags(icmpfilterflags, ~filter.data, "ICMP_???");
746 	tprints(")");
747 }
748 
749 static bool
print_uint32(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)750 print_uint32(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
751 {
752 	tprintf("%u", *(uint32_t *) elem_buf);
753 
754 	return true;
755 }
756 
757 static void
print_getsockopt(struct tcb * const tcp,const unsigned int level,const unsigned int name,const kernel_ulong_t addr,const int ulen,const int rlen)758 print_getsockopt(struct tcb *const tcp, const unsigned int level,
759 		 const unsigned int name, const kernel_ulong_t addr,
760 		 const int ulen, const int rlen)
761 {
762 	if (ulen <= 0 || rlen <= 0) {
763 		/*
764 		 * As the kernel neither accepts nor returns a negative
765 		 * length in case of successful getsockopt syscall
766 		 * invocation, negative values must have been forged
767 		 * by userspace.
768 		 */
769 		printaddr(addr);
770 		return;
771 	}
772 
773 	if (addr && verbose(tcp))
774 	switch (level) {
775 	case SOL_SOCKET:
776 		switch (name) {
777 		case SO_LINGER:
778 			print_get_linger(tcp, addr, rlen);
779 			return;
780 		case SO_PEERCRED:
781 			print_get_ucred(tcp, addr, rlen);
782 			return;
783 		case SO_ATTACH_FILTER:
784 			/*
785 			 * The length returned by the kernel in case of
786 			 * successful getsockopt syscall invocation is struct
787 			 * sock_fprog.len that has type unsigned short,
788 			 * anything else must have been forged by userspace.
789 			 */
790 			if ((unsigned short) rlen == (unsigned int) rlen)
791 				print_sock_fprog(tcp, addr, rlen);
792 			else
793 				printaddr(addr);
794 			return;
795 		}
796 		break;
797 
798 	case SOL_PACKET:
799 		switch (name) {
800 #ifdef PACKET_STATISTICS
801 		case PACKET_STATISTICS:
802 			print_tpacket_stats(tcp, addr, rlen);
803 			return;
804 #endif
805 		}
806 		break;
807 
808 	case SOL_RAW:
809 		switch (name) {
810 		case ICMP_FILTER:
811 			print_icmp_filter(tcp, addr, rlen);
812 			return;
813 		}
814 		break;
815 
816 	case SOL_NETLINK:
817 		switch (name) {
818 		case NETLINK_LIST_MEMBERSHIPS: {
819 			uint32_t buf;
820 			print_array(tcp, addr, MIN(ulen, rlen) / sizeof(buf),
821 				    &buf, sizeof(buf),
822 				    tfetch_mem, print_uint32, 0);
823 			break;
824 			}
825 		default:
826 			printnum_int(tcp, addr, "%d");
827 			break;
828 		}
829 		return;
830 	}
831 
832 	/* default arg printing */
833 
834 	if (verbose(tcp)) {
835 		if (rlen == sizeof(int)) {
836 			printnum_int(tcp, addr, "%d");
837 		} else {
838 			printstrn(tcp, addr, rlen);
839 		}
840 	} else {
841 		printaddr(addr);
842 	}
843 }
844 
SYS_FUNC(getsockopt)845 SYS_FUNC(getsockopt)
846 {
847 	int ulen, rlen;
848 
849 	if (entering(tcp)) {
850 		print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
851 					    tcp->u_arg[1], tcp->u_arg[2], true);
852 
853 		if (verbose(tcp) && tcp->u_arg[4]
854 		    && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
855 			set_tcb_priv_ulong(tcp, ulen);
856 			return 0;
857 		} else {
858 			printaddr(tcp->u_arg[3]);
859 			tprints(", ");
860 			printaddr(tcp->u_arg[4]);
861 			return RVAL_DECODED;
862 		}
863 	} else {
864 		ulen = get_tcb_priv_ulong(tcp);
865 
866 		if (syserror(tcp) || umove(tcp, tcp->u_arg[4], &rlen) < 0) {
867 			printaddr(tcp->u_arg[3]);
868 			tprintf(", [%d]", ulen);
869 		} else {
870 			print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
871 					 tcp->u_arg[3], ulen, rlen);
872 			if (ulen != rlen)
873 				tprintf(", [%d->%d]", ulen, rlen);
874 			else
875 				tprintf(", [%d]", rlen);
876 		}
877 	}
878 	return 0;
879 }
880 
881 static void
print_set_linger(struct tcb * const tcp,const kernel_ulong_t addr,const int len)882 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
883 		 const int len)
884 {
885 	struct linger linger;
886 
887 	if (len < (int) sizeof(linger)) {
888 		printaddr(addr);
889 	} else if (!umove_or_printaddr(tcp, addr, &linger)) {
890 		PRINT_FIELD_D("{", linger, l_onoff);
891 		PRINT_FIELD_D(", ", linger, l_linger);
892 		tprints("}");
893 	}
894 }
895 
896 #ifdef IP_ADD_MEMBERSHIP
897 static void
print_mreq(struct tcb * const tcp,const kernel_ulong_t addr,const int len)898 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
899 	   const int len)
900 {
901 	struct ip_mreq mreq;
902 
903 	if (len < (int) sizeof(mreq)) {
904 		printaddr(addr);
905 	} else if (!umove_or_printaddr(tcp, addr, &mreq)) {
906 		PRINT_FIELD_INET4_ADDR("{", mreq, imr_multiaddr);
907 		PRINT_FIELD_INET4_ADDR(", ", mreq, imr_interface);
908 		tprints("}");
909 	}
910 }
911 #endif /* IP_ADD_MEMBERSHIP */
912 
913 #ifdef IPV6_ADD_MEMBERSHIP
914 static void
print_mreq6(struct tcb * const tcp,const kernel_ulong_t addr,const int len)915 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
916 	    const int len)
917 {
918 	struct ipv6_mreq mreq;
919 
920 	if (len < (int) sizeof(mreq)) {
921 		printaddr(addr);
922 	} else if (!umove_or_printaddr(tcp, addr, &mreq)) {
923 		PRINT_FIELD_INET_ADDR("{", mreq, ipv6mr_multiaddr, AF_INET6);
924 		PRINT_FIELD_IFINDEX(", ", mreq, ipv6mr_interface);
925 		tprints("}");
926 	}
927 }
928 #endif /* IPV6_ADD_MEMBERSHIP */
929 
930 #ifdef PACKET_RX_RING
931 static void
print_tpacket_req(struct tcb * const tcp,const kernel_ulong_t addr,const int len)932 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
933 {
934 	struct tpacket_req req;
935 
936 	if (len != sizeof(req) ||
937 	    umove(tcp, addr, &req) < 0) {
938 		printaddr(addr);
939 	} else {
940 		PRINT_FIELD_U("{", req, tp_block_size);
941 		PRINT_FIELD_U(", ", req, tp_block_nr);
942 		PRINT_FIELD_U(", ", req, tp_frame_size);
943 		PRINT_FIELD_U(", ", req, tp_frame_nr);
944 		tprints("}");
945 	}
946 }
947 #endif /* PACKET_RX_RING */
948 
949 #ifdef PACKET_ADD_MEMBERSHIP
950 # include "xlat/packet_mreq_type.h"
951 
952 static void
print_packet_mreq(struct tcb * const tcp,const kernel_ulong_t addr,const int len)953 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
954 {
955 	struct packet_mreq mreq;
956 
957 	if (len != sizeof(mreq) ||
958 	    umove(tcp, addr, &mreq) < 0) {
959 		printaddr(addr);
960 	} else {
961 		unsigned int i;
962 
963 		PRINT_FIELD_IFINDEX("{", mreq, mr_ifindex);
964 		PRINT_FIELD_XVAL(", ", mreq, mr_type, packet_mreq_type,
965 				 "PACKET_MR_???");
966 		PRINT_FIELD_U(", ", mreq, mr_alen);
967 		tprints(", mr_address=");
968 		if (mreq.mr_alen > ARRAY_SIZE(mreq.mr_address))
969 			mreq.mr_alen = ARRAY_SIZE(mreq.mr_address);
970 		for (i = 0; i < mreq.mr_alen; ++i)
971 			tprintf("%02x", mreq.mr_address[i]);
972 		tprints("}");
973 	}
974 }
975 #endif /* PACKET_ADD_MEMBERSHIP */
976 
977 static void
print_setsockopt(struct tcb * const tcp,const unsigned int level,const unsigned int name,const kernel_ulong_t addr,const int len)978 print_setsockopt(struct tcb *const tcp, const unsigned int level,
979 		 const unsigned int name, const kernel_ulong_t addr,
980 		 const int len)
981 {
982 	if (addr && verbose(tcp))
983 	switch (level) {
984 	case SOL_SOCKET:
985 		switch (name) {
986 		case SO_LINGER:
987 			print_set_linger(tcp, addr, len);
988 			return;
989 		case SO_ATTACH_FILTER:
990 		case SO_ATTACH_REUSEPORT_CBPF:
991 			if ((unsigned int) len == get_sock_fprog_size())
992 				decode_sock_fprog(tcp, addr);
993 			else
994 				printaddr(addr);
995 			return;
996 		}
997 		break;
998 
999 	case SOL_IP:
1000 		switch (name) {
1001 #ifdef IP_ADD_MEMBERSHIP
1002 		case IP_ADD_MEMBERSHIP:
1003 		case IP_DROP_MEMBERSHIP:
1004 			print_mreq(tcp, addr, len);
1005 			return;
1006 #endif /* IP_ADD_MEMBERSHIP */
1007 #ifdef MCAST_JOIN_GROUP
1008 		case MCAST_JOIN_GROUP:
1009 		case MCAST_LEAVE_GROUP:
1010 			print_group_req(tcp, addr, len);
1011 			return;
1012 #endif /* MCAST_JOIN_GROUP */
1013 		}
1014 		break;
1015 
1016 	case SOL_IPV6:
1017 		switch (name) {
1018 #ifdef IPV6_ADD_MEMBERSHIP
1019 		case IPV6_ADD_MEMBERSHIP:
1020 		case IPV6_DROP_MEMBERSHIP:
1021 # ifdef IPV6_JOIN_ANYCAST
1022 		case IPV6_JOIN_ANYCAST:
1023 # endif
1024 # ifdef IPV6_LEAVE_ANYCAST
1025 		case IPV6_LEAVE_ANYCAST:
1026 # endif
1027 			print_mreq6(tcp, addr, len);
1028 			return;
1029 #endif /* IPV6_ADD_MEMBERSHIP */
1030 #ifdef MCAST_JOIN_GROUP
1031 		case MCAST_JOIN_GROUP:
1032 		case MCAST_LEAVE_GROUP:
1033 			print_group_req(tcp, addr, len);
1034 			return;
1035 #endif /* MCAST_JOIN_GROUP */
1036 		}
1037 		break;
1038 
1039 	case SOL_PACKET:
1040 		switch (name) {
1041 #ifdef PACKET_RX_RING
1042 		case PACKET_RX_RING:
1043 # ifdef PACKET_TX_RING
1044 		case PACKET_TX_RING:
1045 # endif
1046 			print_tpacket_req(tcp, addr, len);
1047 			return;
1048 #endif /* PACKET_RX_RING */
1049 #ifdef PACKET_ADD_MEMBERSHIP
1050 		case PACKET_ADD_MEMBERSHIP:
1051 		case PACKET_DROP_MEMBERSHIP:
1052 			print_packet_mreq(tcp, addr, len);
1053 			return;
1054 #endif /* PACKET_ADD_MEMBERSHIP */
1055 		}
1056 		break;
1057 
1058 	case SOL_RAW:
1059 		switch (name) {
1060 		case ICMP_FILTER:
1061 			print_icmp_filter(tcp, addr, len);
1062 			return;
1063 		}
1064 		break;
1065 
1066 	case SOL_NETLINK:
1067 		if (len < (int) sizeof(int))
1068 			printaddr(addr);
1069 		else
1070 			printnum_int(tcp, addr, "%d");
1071 		return;
1072 	}
1073 
1074 	/* default arg printing */
1075 
1076 	if (verbose(tcp)) {
1077 		if (len == sizeof(int)) {
1078 			printnum_int(tcp, addr, "%d");
1079 		} else {
1080 			printstrn(tcp, addr, len);
1081 		}
1082 	} else {
1083 		printaddr(addr);
1084 	}
1085 }
1086 
SYS_FUNC(setsockopt)1087 SYS_FUNC(setsockopt)
1088 {
1089 	print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1090 				    tcp->u_arg[1], tcp->u_arg[2], false);
1091 	print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1092 			 tcp->u_arg[3], tcp->u_arg[4]);
1093 	tprintf(", %d", (int) tcp->u_arg[4]);
1094 
1095 	return RVAL_DECODED;
1096 }
1097