1 /*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-2000 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999-2023 The strace developers.
7 * All rights reserved.
8 *
9 * SPDX-License-Identifier: LGPL-2.1-or-later
10 */
11
12 #include "defs.h"
13
14 #include <sys/stat.h>
15 #include <sys/socket.h>
16 #include <sys/uio.h>
17 #include <sys/un.h>
18 #include <netinet/in.h>
19 #ifdef HAVE_NETINET_TCP_H
20 # include <netinet/tcp.h>
21 #endif
22 #ifdef HAVE_NETINET_UDP_H
23 # include <netinet/udp.h>
24 #endif
25 #ifdef HAVE_NETINET_SCTP_H
26 # include <netinet/sctp.h>
27 #endif
28 #include <arpa/inet.h>
29 #include <net/if.h>
30 #include <asm/types.h>
31
32 #include <linux/ip_vs.h>
33 #include "netlink.h"
34 #if defined(HAVE_LINUX_NETFILTER_ARP_ARP_TABLES_H)
35 # include <linux/netfilter_arp/arp_tables.h>
36 #endif
37 #if defined(HAVE_LINUX_NETFILTER_BRIDGE_EBTABLES_H)
38 # include <linux/netfilter_bridge/ebtables.h>
39 #endif
40 #if defined(HAVE_LINUX_NETFILTER_IPV4_IP_TABLES_H)
41 # include <linux/netfilter_ipv4/ip_tables.h>
42 #endif
43 #if defined(HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H)
44 # include <linux/netfilter_ipv6/ip6_tables.h>
45 #endif
46 #include <linux/if_packet.h>
47 #include <linux/icmp.h>
48 #include <linux/vm_sockets.h>
49
50 #include "xlat/socktypes.h"
51 #include "xlat/sock_type_flags.h"
52 #ifndef SOCK_TYPE_MASK
53 # define SOCK_TYPE_MASK 0xf
54 #endif
55
56 #include "xlat/socketlayers.h"
57
58 #include "xlat/inet_protocols.h"
59
60 #define XLAT_MACROS_ONLY
61 #include "xlat/addrfams.h"
62 #include "xlat/ethernet_protocols.h"
63 #undef XLAT_MACROS_ONLY
64 #include "xlat/ax25_protocols.h"
65 #include "xlat/irda_protocols.h"
66 #include "xlat/can_protocols.h"
67 #include "xlat/bt_protocols.h"
68 #include "xlat/isdn_protocols.h"
69 #include "xlat/phonet_protocols.h"
70 #include "xlat/caif_protocols.h"
71 #include "xlat/nfc_protocols.h"
72 #include "xlat/kcm_protocols.h"
73 #include "xlat/smc_protocols.h"
74
75 static void
76 decode_sockbuf(struct tcb *const tcp, const int fd, const kernel_ulong_t addr,
77 const kernel_ulong_t addrlen)
78 {
79
80 switch (verbose(tcp) ? getfdproto(tcp, fd) : SOCK_PROTO_UNKNOWN) {
81 case SOCK_PROTO_NETLINK:
82 decode_netlink(tcp, fd, addr, addrlen);
83 break;
84 default:
85 printstrn(tcp, addr, addrlen);
86 }
87 }
88
89 /*
90 * low bits of the socket type define real socket type,
91 * other bits are socket type flags.
92 */
93 static void
94 tprint_sock_type(unsigned int flags)
95 {
96 const char *str = xlookup(socktypes, flags & SOCK_TYPE_MASK);
97
98 tprint_flags_begin();
99 if (str) {
100 print_xlat_ex(flags & SOCK_TYPE_MASK, str, XLAT_STYLE_DEFAULT);
101 flags &= ~SOCK_TYPE_MASK;
102 if (!flags)
103 return;
104 tprint_flags_or();
105 }
106 printflags_in(sock_type_flags, flags, "SOCK_???");
107 tprint_flags_end();
108 }
109
110 SYS_FUNC(socket)
111 {
112 /* domain */
113 printxval(addrfams, tcp->u_arg[0], "AF_???");
114 tprint_arg_next();
115
116 /* type */
117 tprint_sock_type(tcp->u_arg[1]);
118 tprint_arg_next();
119
120 /* protocol */
121 switch (tcp->u_arg[0]) {
122 case AF_INET:
123 case AF_INET6:
124 printxval(inet_protocols, tcp->u_arg[2], "IPPROTO_???");
125 break;
126
127 case AF_AX25:
128 /* Those are not available in public headers. */
129 printxval_ex(ax25_protocols, tcp->u_arg[2], "AX25_P_???",
130 XLAT_STYLE_VERBOSE);
131 break;
132
133 case AF_NETLINK:
134 printxval(netlink_protocols, tcp->u_arg[2], "NETLINK_???");
135 break;
136
137 case AF_PACKET:
138 tprints_arg_begin("htons");
139 printxval(ethernet_protocols, ntohs(tcp->u_arg[2]),
140 "ETH_P_???");
141 tprint_arg_end();
142 break;
143
144 case AF_IRDA:
145 printxval(can_protocols, tcp->u_arg[2], "IRDAPROTO_???");
146 break;
147
148 case AF_CAN:
149 printxval(can_protocols, tcp->u_arg[2], "CAN_???");
150 break;
151
152 case AF_BLUETOOTH:
153 printxval(bt_protocols, tcp->u_arg[2], "BTPROTO_???");
154 break;
155
156 case AF_RXRPC:
157 printxval(addrfams, tcp->u_arg[2], "AF_???");
158 break;
159
160 case AF_ISDN:
161 printxval(isdn_protocols, tcp->u_arg[2], "ISDN_P_???");
162 break;
163
164 case AF_PHONET:
165 printxval(phonet_protocols, tcp->u_arg[2], "PN_PROTO_???");
166 break;
167
168 case AF_CAIF:
169 printxval(caif_protocols, tcp->u_arg[2], "CAIFPROTO_???");
170 break;
171
172 case AF_NFC:
173 printxval(nfc_protocols, tcp->u_arg[2], "NFC_SOCKPROTO_???");
174 break;
175
176 case AF_KCM:
177 printxval(kcm_protocols, tcp->u_arg[2], "KCMPROTO_???");
178 break;
179
180 case AF_SMC:
181 printxval(smc_protocols, tcp->u_arg[2], "SMCPROTO_???");
182 break;
183
184 default:
185 PRINT_VAL_U(tcp->u_arg[2]);
186 break;
187 }
188
189 return RVAL_DECODED | RVAL_FD;
190 }
191
192 static bool
193 fetch_socklen(struct tcb *const tcp, int *const plen,
194 const kernel_ulong_t sockaddr, const kernel_ulong_t socklen)
195 {
196 return verbose(tcp) && sockaddr && socklen
197 && umove(tcp, socklen, plen) == 0;
198 }
199
200 static int
201 decode_sockname(struct tcb *tcp)
202 {
203 int ulen, rlen;
204
205 if (entering(tcp)) {
206 /* sockfd */
207 printfd(tcp, tcp->u_arg[0]);
208 tprint_arg_next();
209
210 if (fetch_socklen(tcp, &ulen, tcp->u_arg[1], tcp->u_arg[2])) {
211 set_tcb_priv_ulong(tcp, ulen);
212 return 0;
213 } else {
214 /* addr */
215 printaddr(tcp->u_arg[1]);
216 tprint_arg_next();
217
218 /* addrlen */
219 printaddr(tcp->u_arg[2]);
220
221 return RVAL_DECODED;
222 }
223 }
224
225 ulen = get_tcb_priv_ulong(tcp);
226
227 if (syserror(tcp) || umove(tcp, tcp->u_arg[2], &rlen) < 0) {
228 /* addr */
229 printaddr(tcp->u_arg[1]);
230 tprint_arg_next();
231
232 /* addrlen */
233 tprint_indirect_begin();
234 PRINT_VAL_D(ulen);
235 tprint_indirect_end();
236 } else {
237 /* addr */
238 decode_sockaddr(tcp, tcp->u_arg[1], ulen > rlen ? rlen : ulen);
239 tprint_arg_next();
240
241 /* addrlen */
242 tprint_indirect_begin();
243 if (ulen != rlen) {
244 PRINT_VAL_D(ulen);
245 tprint_value_changed();
246 }
247 PRINT_VAL_D(rlen);
248 tprint_indirect_end();
249 }
250
251 return RVAL_DECODED;
252 }
253
254 SYS_FUNC(accept)
255 {
256 return decode_sockname(tcp) | RVAL_FD;
257 }
258
259 SYS_FUNC(accept4)
260 {
261 int rc = decode_sockname(tcp);
262
263 if (rc & RVAL_DECODED) {
264 /* flags */
265 tprint_arg_next();
266 printflags(sock_type_flags, tcp->u_arg[3], "SOCK_???");
267 }
268
269 return rc | RVAL_FD;
270 }
271
272 SYS_FUNC(send)
273 {
274 /* sockfd */
275 printfd(tcp, tcp->u_arg[0]);
276 tprint_arg_next();
277
278 /* buf */
279 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
280 tprint_arg_next();
281
282 /* len */
283 PRINT_VAL_U(tcp->u_arg[2]);
284 tprint_arg_next();
285
286 /* flags */
287 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
288
289 return RVAL_DECODED;
290 }
291
292 SYS_FUNC(sendto)
293 {
294 /* sockfd */
295 printfd(tcp, tcp->u_arg[0]);
296 tprint_arg_next();
297
298 /* buf */
299 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1], tcp->u_arg[2]);
300 tprint_arg_next();
301
302 /* len */
303 PRINT_VAL_U(tcp->u_arg[2]);
304 tprint_arg_next();
305
306 /* flags */
307 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
308 tprint_arg_next();
309
310 /* dest_addr */
311 const int addrlen = tcp->u_arg[5];
312 decode_sockaddr(tcp, tcp->u_arg[4], addrlen);
313 tprint_arg_next();
314
315 /* addrlen */
316 PRINT_VAL_D(addrlen);
317
318 return RVAL_DECODED;
319 }
320
321 SYS_FUNC(recv)
322 {
323 if (entering(tcp)) {
324 /* sockfd */
325 printfd(tcp, tcp->u_arg[0]);
326 tprint_arg_next();
327 } else {
328 /* buf */
329 if (syserror(tcp)) {
330 printaddr(tcp->u_arg[1]);
331 } else {
332 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
333 MIN((kernel_ulong_t) tcp->u_rval,
334 tcp->u_arg[2]));
335 }
336 tprint_arg_next();
337
338 /* len */
339 PRINT_VAL_U(tcp->u_arg[2]);
340 tprint_arg_next();
341
342 /* flags */
343 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
344 }
345 return 0;
346 }
347
348 SYS_FUNC(recvfrom)
349 {
350 int ulen, rlen;
351
352 if (entering(tcp)) {
353 /* sockfd */
354 printfd(tcp, tcp->u_arg[0]);
355 tprint_arg_next();
356
357 if (fetch_socklen(tcp, &ulen, tcp->u_arg[4], tcp->u_arg[5])) {
358 set_tcb_priv_ulong(tcp, ulen);
359 }
360 } else {
361 /* buf */
362 if (syserror(tcp)) {
363 printaddr(tcp->u_arg[1]);
364 } else {
365 decode_sockbuf(tcp, tcp->u_arg[0], tcp->u_arg[1],
366 MIN((kernel_ulong_t) tcp->u_rval,
367 tcp->u_arg[2]));
368 }
369 tprint_arg_next();
370
371 /* len */
372 PRINT_VAL_U(tcp->u_arg[2]);
373 tprint_arg_next();
374
375 /* flags */
376 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
377 tprint_arg_next();
378
379 ulen = get_tcb_priv_ulong(tcp);
380
381 if (!fetch_socklen(tcp, &rlen, tcp->u_arg[4], tcp->u_arg[5])) {
382 /* src_addr */
383 printaddr(tcp->u_arg[4]);
384 tprint_arg_next();
385
386 /* addrlen */
387 printaddr(tcp->u_arg[5]);
388
389 return 0;
390 }
391 if (syserror(tcp)) {
392 /* src_addr */
393 printaddr(tcp->u_arg[4]);
394 tprint_arg_next();
395
396 /* addrlen */
397 tprint_indirect_begin();
398 PRINT_VAL_D(ulen);
399 tprint_indirect_end();
400
401 return 0;
402 }
403
404 /* src_addr */
405 decode_sockaddr(tcp, tcp->u_arg[4], ulen > rlen ? rlen : ulen);
406 tprint_arg_next();
407
408 /* addrlen */
409 tprint_indirect_begin();
410 if (ulen != rlen) {
411 PRINT_VAL_D(ulen);
412 tprint_value_changed();
413 }
414 PRINT_VAL_D(rlen);
415 tprint_indirect_end();
416 }
417 return 0;
418 }
419
420 SYS_FUNC(getsockname)
421 {
422 return decode_sockname(tcp);
423 }
424
425 static void
426 decode_pair_fd(struct tcb *const tcp, const kernel_ulong_t addr)
427 {
428 int fd;
429 print_array(tcp, addr, 2, &fd, sizeof(fd),
430 tfetch_mem, print_fd_array_member, NULL);
431 }
432
433 static int
434 do_pipe(struct tcb *tcp, int flags_arg)
435 {
436 if (exiting(tcp)) {
437 /* pipefd */
438 decode_pair_fd(tcp, tcp->u_arg[0]);
439 if (flags_arg >= 0) {
440 /* flags */
441 tprint_arg_next();
442 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
443 }
444 }
445 return 0;
446 }
447
448 SYS_FUNC(pipe)
449 {
450 #if HAVE_ARCH_GETRVAL2
451 if (exiting(tcp) && !syserror(tcp)) {
452 tprint_array_begin();
453 printfd(tcp, tcp->u_rval);
454 tprint_array_next();
455 printfd(tcp, getrval2(tcp));
456 tprint_array_end();
457 }
458 return 0;
459 #else
460 return do_pipe(tcp, -1);
461 #endif
462 }
463
464 SYS_FUNC(pipe2)
465 {
466 return do_pipe(tcp, 1);
467 }
468
469 SYS_FUNC(socketpair)
470 {
471 if (entering(tcp)) {
472 /* domain */
473 printxval(addrfams, tcp->u_arg[0], "AF_???");
474 tprint_arg_next();
475
476 /* type */
477 tprint_sock_type(tcp->u_arg[1]);
478 tprint_arg_next();
479
480 /* protocol */
481 PRINT_VAL_U(tcp->u_arg[2]);
482 tprint_arg_next();
483 } else {
484 /* sv */
485 decode_pair_fd(tcp, tcp->u_arg[3]);
486 }
487 return 0;
488 }
489
490 #include "xlat/sock_options.h"
491 #include "xlat/getsock_options.h"
492 #include "xlat/setsock_options.h"
493 #include "xlat/sock_ip_options.h"
494 #include "xlat/getsock_ip_options.h"
495 #include "xlat/setsock_ip_options.h"
496 #include "xlat/sock_vsock_options.h"
497 #include "xlat/sock_ipv6_options.h"
498 #include "xlat/getsock_ipv6_options.h"
499 #include "xlat/setsock_ipv6_options.h"
500 #include "xlat/sock_ipx_options.h"
501 #include "xlat/sock_ax25_options.h"
502 #include "xlat/sock_netlink_options.h"
503 #include "xlat/sock_packet_options.h"
504 #include "xlat/sock_raw_options.h"
505 #include "xlat/sock_sctp_options.h"
506 #include "xlat/sock_tcp_options.h"
507 #include "xlat/sock_udp_options.h"
508 #include "xlat/sock_can_raw_options.h"
509 #include "xlat/sock_irda_options.h"
510 #include "xlat/sock_llc_options.h"
511 #include "xlat/sock_dccp_options.h"
512 #include "xlat/sock_tipc_options.h"
513 #include "xlat/sock_rxrpc_options.h"
514 #include "xlat/sock_pppol2tp_options.h"
515 #include "xlat/sock_bluetooth_options.h"
516 #include "xlat/sock_pnp_options.h"
517 #include "xlat/sock_rds_options.h"
518 #include "xlat/sock_iucv_options.h"
519 #include "xlat/sock_caif_options.h"
520 #include "xlat/sock_alg_options.h"
521 #include "xlat/sock_nfcllcp_options.h"
522 #include "xlat/sock_kcm_options.h"
523 #include "xlat/sock_tls_options.h"
524 #include "xlat/sock_xdp_options.h"
525
526 static void
527 print_sockopt_fd_level_name(struct tcb *tcp, int fd, unsigned int level,
528 unsigned int name, bool is_getsockopt)
529 {
530 /* sockfd */
531 printfd(tcp, fd);
532 tprint_arg_next();
533
534 /* level */
535 printxval(socketlayers, level, "SOL_??");
536 tprint_arg_next();
537
538 /* optname */
539 switch (level) {
540 case SOL_SOCKET:
541 printxvals(name, "SO_???", sock_options,
542 is_getsockopt ? getsock_options :
543 setsock_options, NULL);
544 break;
545 case SOL_IP:
546 printxvals(name, "IP_???", sock_ip_options,
547 is_getsockopt ? getsock_ip_options :
548 setsock_ip_options, NULL);
549 break;
550 /*
551 * Yes, VMWare in their infinite wisdom have decided to use address
552 * family instead of a socket option layer for the socket option layer
553 * check, see net/vmw_vsock/af_vsock.c:vsock_connectible_[gs]etsockopt.
554 */
555 case AF_VSOCK:
556 printxval(sock_vsock_options, name, "SO_VM_???");
557 break;
558 case SOL_IPV6:
559 printxvals(name, "IPV6_???", sock_ipv6_options,
560 is_getsockopt ? getsock_ipv6_options :
561 setsock_ipv6_options, NULL);
562 break;
563 case SOL_IPX:
564 printxval(sock_ipx_options, name, "IPX_???");
565 break;
566 case SOL_AX25:
567 printxval(sock_ax25_options, name, "AX25_???");
568 break;
569 case SOL_PACKET:
570 printxval(sock_packet_options, name, "PACKET_???");
571 break;
572 case SOL_TCP:
573 printxval(sock_tcp_options, name, "TCP_???");
574 break;
575 case SOL_CAN_RAW:
576 printxval(sock_can_raw_options, name, "CAN_RAW_???");
577 break;
578 case SOL_SCTP:
579 printxval(sock_sctp_options, name, "SCTP_???");
580 break;
581 case SOL_RAW:
582 printxval(sock_raw_options, name, "RAW_???");
583 break;
584 case SOL_NETLINK:
585 printxval(sock_netlink_options, name, "NETLINK_???");
586 break;
587 case SOL_UDP:
588 printxval(sock_udp_options, name, "UDP_???");
589 break;
590 case SOL_IRDA:
591 printxval(sock_irda_options, name, "IRLMP_???");
592 break;
593 case SOL_LLC:
594 printxval(sock_llc_options, name, "LLC_OPT_???");
595 break;
596 case SOL_DCCP:
597 printxval(sock_dccp_options, name, "DCCP_SOCKOPT_???");
598 break;
599 case SOL_TIPC:
600 printxval(sock_tipc_options, name, "TIPC_???");
601 break;
602 case SOL_RXRPC:
603 printxval(sock_rxrpc_options, name, "RXRPC_???");
604 break;
605 case SOL_PPPOL2TP:
606 printxval(sock_pppol2tp_options, name, "PPPOL2TP_SO_???");
607 break;
608 case SOL_BLUETOOTH:
609 printxval(sock_bluetooth_options, name, "BT_???");
610 break;
611 case SOL_PNPIPE:
612 printxval(sock_pnp_options, name, "PNPIPE_???");
613 break;
614 case SOL_RDS:
615 printxval(sock_rds_options, name, "RDS_???");
616 break;
617 case SOL_IUCV:
618 printxval(sock_iucv_options, name, "SO_???");
619 break;
620 case SOL_CAIF:
621 printxval(sock_caif_options, name, "CAIFSO_???");
622 break;
623 case SOL_ALG:
624 printxval(sock_alg_options, name, "ALG_???");
625 break;
626 case SOL_NFC:
627 printxval(sock_nfcllcp_options, name, "NFC_LLCP_???");
628 break;
629 case SOL_KCM:
630 printxval(sock_kcm_options, name, "KCM_???");
631 break;
632 case SOL_TLS:
633 printxval(sock_tls_options, name, "TLS_???");
634 break;
635 case SOL_XDP:
636 printxval(sock_xdp_options, name, "XDP_???");
637 break;
638
639 /* Other SOL_* protocol levels still need work. */
640
641 default:
642 PRINT_VAL_U(name);
643 }
644 }
645
646 static void
647 print_get_linger(struct tcb *const tcp, const kernel_ulong_t addr,
648 unsigned int len)
649 {
650 struct linger linger;
651
652 /*
653 * The kernel cannot return len > sizeof(linger) because struct linger
654 * cannot change, but extra safety won't harm either.
655 */
656 if (len > sizeof(linger))
657 len = sizeof(linger);
658 if (umoven_or_printaddr(tcp, addr, len, &linger))
659 return;
660
661 MAYBE_PRINT_FIELD_LEN(tprint_struct_begin(),
662 linger, l_onoff, len, PRINT_FIELD_D);
663 MAYBE_PRINT_FIELD_LEN(tprint_struct_next(),
664 linger, l_linger, len, PRINT_FIELD_D);
665 tprint_struct_end();
666 }
667
668 static void
669 print_get_ucred(struct tcb *const tcp, const kernel_ulong_t addr,
670 unsigned int len)
671 {
672 struct ucred uc;
673
674 /*
675 * The kernel is very unlikely to return len > sizeof(uc)
676 * because struct ucred is very unlikely to change,
677 * but extra safety won't harm either.
678 */
679 if (len > sizeof(uc))
680 len = sizeof(uc);
681
682 if (umoven_or_printaddr(tcp, addr, len, &uc))
683 return;
684
685 MAYBE_PRINT_FIELD_LEN(tprint_struct_begin(),
686 uc, pid, len, PRINT_FIELD_TGID, tcp);
687 MAYBE_PRINT_FIELD_LEN(tprint_struct_next(),
688 uc, uid, len, PRINT_FIELD_ID);
689 MAYBE_PRINT_FIELD_LEN(tprint_struct_next(),
690 uc, gid, len, PRINT_FIELD_ID);
691 tprint_struct_end();
692 }
693
694 static void
695 print_get_error(struct tcb *const tcp, const kernel_ulong_t addr,
696 const unsigned int len)
697 {
698 unsigned int err;
699
700 if (len < sizeof(err)) {
701 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
702 return;
703 }
704
705 if (umove_or_printaddr(tcp, addr, &err))
706 return;
707
708 tprint_indirect_begin();
709 print_err(err, false);
710 tprint_indirect_end();
711 }
712
713 #include "xlat/sockopt_txrehash_vals.h"
714
715 static void
716 print_txrehash(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
717 {
718 int val = 0;
719
720 if (len < (int) sizeof(val)) {
721 if (entering(tcp))
722 printaddr(addr);
723 else
724 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
725 return;
726 }
727
728 if (umove_or_printaddr(tcp, addr, &val))
729 return;
730
731 tprint_indirect_begin();
732 printxval_d(sockopt_txrehash_vals, val, "SOCK_TXREHASH_???");
733 tprint_indirect_end();
734 }
735
736 static void
737 print_get_fd(struct tcb *const tcp, const kernel_ulong_t addr,
738 const unsigned int len)
739 {
740 int fd;
741
742 if (len < sizeof(fd)) {
743 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
744 return;
745 }
746
747 if (umove_or_printaddr(tcp, addr, &fd))
748 return;
749
750 tprint_indirect_begin();
751 printfd(tcp, fd);
752 tprint_indirect_end();
753 }
754
755 static void
756 print_port_range(struct tcb *const tcp, const kernel_ulong_t addr,
757 const unsigned int len)
758 {
759 unsigned int ports;
760
761 if (len != sizeof(ports)) {
762 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
763 return;
764 }
765
766 if (umove_or_printaddr(tcp, addr, &ports))
767 return;
768
769 tprint_indirect_begin();
770 PRINT_VAL_X(ports);
771 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_RAW) {
772 unsigned short lo = ports & 0xffff;
773 unsigned short hi = ports >> 16;
774
775 if (ports && (!lo || !hi || lo <= hi))
776 tprintf_comment("%.0hu..%.0hu", lo, hi);
777 }
778 tprint_indirect_end();
779 }
780
781
782 static void
783 print_ip_protocol(struct tcb *const tcp, const kernel_ulong_t addr,
784 const unsigned int len)
785 {
786 unsigned int protocol;
787
788 if (len != sizeof(protocol)) {
789 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
790 return;
791 }
792
793 if (umove_or_printaddr(tcp, addr, &protocol))
794 return;
795
796 tprint_indirect_begin();
797 printxval(inet_protocols, protocol, "IPPROTO_???");
798 tprint_indirect_end();
799 }
800
801
802 static void
803 print_tpacket_stats(struct tcb *const tcp, const kernel_ulong_t addr,
804 unsigned int len)
805 {
806 struct tp_stats {
807 unsigned int tp_packets, tp_drops, tp_freeze_q_cnt;
808 } stats;
809
810 /*
811 * The kernel may return len > sizeof(stats) if the kernel structure
812 * grew as it happened when tpacket_stats_v3 was introduced.
813 */
814 if (len > sizeof(stats))
815 len = sizeof(stats);
816
817 if (umoven_or_printaddr(tcp, addr, len, &stats))
818 return;
819
820 MAYBE_PRINT_FIELD_LEN(tprint_struct_begin(),
821 stats, tp_packets, len, PRINT_FIELD_U);
822 MAYBE_PRINT_FIELD_LEN(tprint_struct_next(),
823 stats, tp_drops, len, PRINT_FIELD_U);
824 MAYBE_PRINT_FIELD_LEN(tprint_struct_next(),
825 stats, tp_freeze_q_cnt, len, PRINT_FIELD_U);
826 tprint_struct_end();
827 }
828
829 #include "xlat/icmp_filter_flags.h"
830
831 static void
832 print_icmp_filter(struct tcb *const tcp, const kernel_ulong_t addr, int len)
833 {
834 struct icmp_filter filter = { ~0U };
835
836 if (len > (int) sizeof(filter))
837 len = sizeof(filter);
838 else if (len <= 0) {
839 printaddr(addr);
840 return;
841 }
842
843 if (umoven_or_printaddr(tcp, addr, len, &filter))
844 return;
845
846 uint32_t data32 = filter.data;
847 static_assert(sizeof(filter.data) == sizeof(data32),
848 "struct icmp_filter.data is not 32-bit long");
849
850 /* check whether more than half of the bits are set */
851 if (popcount32(&data32, 1) > sizeof(data32) * 8 / 2) {
852 /* show those bits that are NOT in the set */
853 data32 = ~data32;
854 tprints_string("~");
855 }
856
857 /* next_set_bit operates on current_wordsize words */
858 unsigned long data;
859 void *p;
860 if (current_wordsize > sizeof(data32)) {
861 data = data32;
862 p = &data;
863 } else {
864 p = &data32;
865 }
866
867 tprint_bitset_begin();
868 bool next = false;
869 for (int i = 0;; ++i) {
870 i = next_set_bit(p, i, sizeof(data32) * 8);
871 if (i < 0)
872 break;
873 if (next)
874 tprint_bitset_next();
875 else
876 next = true;
877 printxval(icmp_filter_flags, i, "ICMP_???");
878 }
879 tprint_bitset_end();
880 }
881
882 static void
883 print_getsockopt(struct tcb *const tcp, const unsigned int level,
884 const unsigned int name, const kernel_ulong_t addr,
885 const int ulen, const int rlen)
886 {
887 if (ulen <= 0 || rlen <= 0) {
888 /*
889 * As the kernel neither accepts nor returns a negative
890 * length in case of successful getsockopt syscall
891 * invocation, negative values must have been forged
892 * by userspace.
893 */
894 printaddr(addr);
895 return;
896 }
897
898 if (addr && verbose(tcp))
899 switch (level) {
900 case SOL_SOCKET:
901 switch (name) {
902 case SO_LINGER:
903 print_get_linger(tcp, addr, rlen);
904 return;
905 case SO_PEERCRED:
906 print_get_ucred(tcp, addr, rlen);
907 return;
908 case SO_ATTACH_FILTER:
909 /*
910 * The length returned by the kernel in case of
911 * successful getsockopt syscall invocation is struct
912 * sock_fprog.len that has type unsigned short,
913 * anything else must have been forged by userspace.
914 */
915 if ((unsigned short) rlen == (unsigned int) rlen)
916 print_sock_fprog(tcp, addr, rlen);
917 else
918 printaddr(addr);
919 return;
920 case SO_ERROR:
921 print_get_error(tcp, addr, rlen);
922 return;
923 case SO_TXREHASH:
924 print_txrehash(tcp, addr, rlen);
925 return;
926 case SO_PEERPIDFD:
927 print_get_fd(tcp, addr, rlen);
928 return;
929
930 /* All known int-like options */
931 case SO_DEBUG:
932 case SO_REUSEADDR:
933 case SO_DONTROUTE:
934 case SO_BROADCAST:
935 case SO_SNDBUF:
936 case SO_RCVBUF:
937 case SO_KEEPALIVE:
938 case SO_OOBINLINE:
939 case SO_NO_CHECK:
940 case SO_PRIORITY:
941 case SO_BSDCOMPAT:
942 case SO_REUSEPORT:
943 case SO_PASSCRED:
944 case SO_RCVLOWAT:
945 case SO_SNDLOWAT:
946 case SO_DETACH_FILTER:
947 case SO_TIMESTAMP_OLD:
948 case SO_ACCEPTCONN:
949 case SO_SNDBUFFORCE:
950 case SO_RCVBUFFORCE:
951 case SO_PASSSEC:
952 case SO_TIMESTAMPNS_OLD:
953 case SO_MARK:
954 case SO_TIMESTAMPING_OLD:
955 case SO_RXQ_OVFL:
956 case SO_WIFI_STATUS:
957 case SO_PEEK_OFF:
958 case SO_NOFCS:
959 case SO_LOCK_FILTER:
960 case SO_SELECT_ERR_QUEUE:
961 case SO_BUSY_POLL:
962 case SO_INCOMING_CPU:
963 case SO_CNX_ADVICE:
964 case SO_INCOMING_NAPI_ID:
965 case SO_ZEROCOPY:
966 case SO_TIMESTAMP_NEW:
967 case SO_TIMESTAMPNS_NEW:
968 case SO_TIMESTAMPING_NEW:
969 case SO_DETACH_REUSEPORT_BPF:
970 case SO_PREFER_BUSY_POLL:
971 case SO_BUSY_POLL_BUDGET:
972 case SO_RESERVE_MEM:
973 case SO_RCVMARK:
974 case SO_PASSPIDFD:
975 if (rlen >= (int) sizeof(int))
976 printnum_int(tcp, addr, "%d");
977 else
978 printstr_ex(tcp, addr, rlen, QUOTE_FORCE_HEX);
979 return;
980 }
981 break;
982
983 case SOL_IP:
984 switch (name) {
985 case IP_LOCAL_PORT_RANGE:
986 print_port_range(tcp, addr, rlen);
987 return;
988 case IP_PROTOCOL:
989 print_ip_protocol(tcp, addr, rlen);
990 return;
991 }
992 break;
993
994 case SOL_PACKET:
995 switch (name) {
996 case PACKET_STATISTICS:
997 print_tpacket_stats(tcp, addr, rlen);
998 return;
999 }
1000 break;
1001
1002 case SOL_RAW:
1003 switch (name) {
1004 case ICMP_FILTER:
1005 print_icmp_filter(tcp, addr, rlen);
1006 return;
1007 }
1008 break;
1009
1010 case SOL_NETLINK:
1011 switch (name) {
1012 case NETLINK_LIST_MEMBERSHIPS: {
1013 uint32_t buf;
1014 print_array(tcp, addr, MIN(ulen, rlen) / sizeof(buf),
1015 &buf, sizeof(buf),
1016 tfetch_mem, print_uint_array_member, 0);
1017 break;
1018 }
1019 default:
1020 printnum_int(tcp, addr, "%d");
1021 break;
1022 }
1023 return;
1024 }
1025
1026 /* default arg printing */
1027
1028 if (verbose(tcp)) {
1029 if (rlen == sizeof(int)) {
1030 printnum_int(tcp, addr, "%d");
1031 } else {
1032 printstrn(tcp, addr, rlen);
1033 }
1034 } else {
1035 printaddr(addr);
1036 }
1037 }
1038
1039 SYS_FUNC(getsockopt)
1040 {
1041 int ulen, rlen;
1042
1043 if (entering(tcp)) {
1044 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1045 tcp->u_arg[1], tcp->u_arg[2], true);
1046 tprint_arg_next();
1047
1048 if (verbose(tcp) && tcp->u_arg[4]
1049 && umove(tcp, tcp->u_arg[4], &ulen) == 0) {
1050 set_tcb_priv_ulong(tcp, ulen);
1051 return 0;
1052 } else {
1053 /* optval */
1054 printaddr(tcp->u_arg[3]);
1055 tprint_arg_next();
1056
1057 /* optlen */
1058 printaddr(tcp->u_arg[4]);
1059 return RVAL_DECODED;
1060 }
1061 } else {
1062 ulen = get_tcb_priv_ulong(tcp);
1063
1064 if (umove(tcp, tcp->u_arg[4], &rlen) < 0) {
1065 /* optval */
1066 printaddr(tcp->u_arg[3]);
1067 tprint_arg_next();
1068
1069 /* optlen */
1070 tprint_indirect_begin();
1071 PRINT_VAL_D(ulen);
1072 tprint_indirect_end();
1073 } else if (syserror(tcp)) {
1074 /* optval */
1075 printaddr(tcp->u_arg[3]);
1076 tprint_arg_next();
1077
1078 /* optlen */
1079 tprint_indirect_begin();
1080 if (ulen != rlen) {
1081 PRINT_VAL_D(ulen);
1082 tprint_value_changed();
1083 }
1084 PRINT_VAL_D(rlen);
1085 tprint_indirect_end();
1086 } else {
1087 /* optval */
1088 print_getsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1089 tcp->u_arg[3], ulen, rlen);
1090 tprint_arg_next();
1091
1092 /* optlen */
1093 tprint_indirect_begin();
1094 if (ulen != rlen) {
1095 PRINT_VAL_D(ulen);
1096 tprint_value_changed();
1097 }
1098 PRINT_VAL_D(rlen);
1099 tprint_indirect_end();
1100 }
1101 }
1102 return 0;
1103 }
1104
1105 static void
1106 print_set_linger(struct tcb *const tcp, const kernel_ulong_t addr,
1107 const int len)
1108 {
1109 struct linger linger;
1110
1111 if (len < (int) sizeof(linger)) {
1112 printaddr(addr);
1113 } else if (!umove_or_printaddr(tcp, addr, &linger)) {
1114 tprint_struct_begin();
1115 PRINT_FIELD_D(linger, l_onoff);
1116 tprint_struct_next();
1117 PRINT_FIELD_D(linger, l_linger);
1118 tprint_struct_end();
1119 }
1120 }
1121
1122 static void
1123 print_mreq(struct tcb *const tcp, const kernel_ulong_t addr,
1124 const int len)
1125 {
1126 struct ip_mreq mreq;
1127
1128 if (len < (int) sizeof(mreq)) {
1129 printaddr(addr);
1130 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
1131 tprint_struct_begin();
1132 PRINT_FIELD_INET_ADDR(mreq, imr_multiaddr, AF_INET);
1133 tprint_struct_next();
1134 PRINT_FIELD_INET_ADDR(mreq, imr_interface, AF_INET);
1135 tprint_struct_end();
1136 }
1137 }
1138
1139 static void
1140 print_mreq6(struct tcb *const tcp, const kernel_ulong_t addr,
1141 const int len)
1142 {
1143 struct ipv6_mreq mreq;
1144
1145 if (len < (int) sizeof(mreq)) {
1146 printaddr(addr);
1147 } else if (!umove_or_printaddr(tcp, addr, &mreq)) {
1148 tprint_struct_begin();
1149 PRINT_FIELD_INET_ADDR(mreq, ipv6mr_multiaddr, AF_INET6);
1150 tprint_struct_next();
1151 PRINT_FIELD_IFINDEX(mreq, ipv6mr_interface);
1152 tprint_struct_end();
1153 }
1154 }
1155
1156 static void
1157 print_tpacket_req(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
1158 {
1159 struct tpacket_req req;
1160
1161 if (len != sizeof(req) ||
1162 umove(tcp, addr, &req) < 0) {
1163 printaddr(addr);
1164 } else {
1165 tprint_struct_begin();
1166 PRINT_FIELD_U(req, tp_block_size);
1167 tprint_struct_next();
1168 PRINT_FIELD_U(req, tp_block_nr);
1169 tprint_struct_next();
1170 PRINT_FIELD_U(req, tp_frame_size);
1171 tprint_struct_next();
1172 PRINT_FIELD_U(req, tp_frame_nr);
1173 tprint_struct_end();
1174 }
1175 }
1176
1177 #include "xlat/packet_mreq_type.h"
1178
1179 static void
1180 print_packet_mreq(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
1181 {
1182 struct packet_mreq mreq;
1183
1184 if (len != sizeof(mreq) ||
1185 umove(tcp, addr, &mreq) < 0) {
1186 printaddr(addr);
1187 } else {
1188 tprint_struct_begin();
1189 PRINT_FIELD_IFINDEX(mreq, mr_ifindex);
1190 tprint_struct_next();
1191 PRINT_FIELD_XVAL(mreq, mr_type, packet_mreq_type,
1192 "PACKET_MR_???");
1193 tprint_struct_next();
1194 PRINT_FIELD_U(mreq, mr_alen);
1195 tprint_struct_next();
1196 PRINT_FIELD_MAC_SZ(mreq, mr_address, mreq.mr_alen);
1197 tprint_struct_end();
1198 }
1199 }
1200
1201 static void
1202 print_setsockopt(struct tcb *const tcp, const unsigned int level,
1203 const unsigned int name, const kernel_ulong_t addr,
1204 const int len)
1205 {
1206 if (addr && verbose(tcp))
1207 switch (level) {
1208 case SOL_SOCKET:
1209 switch (name) {
1210 case SO_LINGER:
1211 print_set_linger(tcp, addr, len);
1212 return;
1213 case SO_ATTACH_FILTER:
1214 case SO_ATTACH_REUSEPORT_CBPF:
1215 if ((unsigned int) len == get_sock_fprog_size())
1216 decode_sock_fprog(tcp, addr);
1217 else
1218 printaddr(addr);
1219 return;
1220 case SO_TXREHASH:
1221 print_txrehash(tcp, addr, len);
1222 return;
1223
1224 /* All known int-like options */
1225 case SO_DEBUG:
1226 case SO_REUSEADDR:
1227 case SO_DONTROUTE:
1228 case SO_BROADCAST:
1229 case SO_SNDBUF:
1230 case SO_RCVBUF:
1231 case SO_KEEPALIVE:
1232 case SO_OOBINLINE:
1233 case SO_NO_CHECK:
1234 case SO_PRIORITY:
1235 case SO_BSDCOMPAT:
1236 case SO_REUSEPORT:
1237 case SO_PASSCRED:
1238 case SO_RCVLOWAT:
1239 case SO_SNDLOWAT:
1240 case SO_DETACH_FILTER:
1241 case SO_TIMESTAMP_OLD:
1242 case SO_ACCEPTCONN:
1243 case SO_SNDBUFFORCE:
1244 case SO_RCVBUFFORCE:
1245 case SO_PASSSEC:
1246 case SO_TIMESTAMPNS_OLD:
1247 case SO_MARK:
1248 case SO_TIMESTAMPING_OLD:
1249 case SO_RXQ_OVFL:
1250 case SO_WIFI_STATUS:
1251 case SO_PEEK_OFF:
1252 case SO_NOFCS:
1253 case SO_LOCK_FILTER:
1254 case SO_SELECT_ERR_QUEUE:
1255 case SO_BUSY_POLL:
1256 case SO_INCOMING_CPU:
1257 case SO_CNX_ADVICE:
1258 case SO_INCOMING_NAPI_ID:
1259 case SO_ZEROCOPY:
1260 case SO_TIMESTAMP_NEW:
1261 case SO_TIMESTAMPNS_NEW:
1262 case SO_TIMESTAMPING_NEW:
1263 case SO_DETACH_REUSEPORT_BPF:
1264 case SO_PREFER_BUSY_POLL:
1265 case SO_BUSY_POLL_BUDGET:
1266 case SO_RESERVE_MEM:
1267 case SO_RCVMARK:
1268 case SO_PASSPIDFD:
1269 if (len < (int) sizeof(int))
1270 printaddr(addr);
1271 else
1272 printnum_int(tcp, addr, "%d");
1273 return;
1274 }
1275 break;
1276
1277 case SOL_IP:
1278 switch (name) {
1279 case IP_ADD_MEMBERSHIP:
1280 case IP_DROP_MEMBERSHIP:
1281 print_mreq(tcp, addr, len);
1282 return;
1283 case MCAST_JOIN_GROUP:
1284 case MCAST_LEAVE_GROUP:
1285 print_group_req(tcp, addr, len);
1286 return;
1287 case IP_LOCAL_PORT_RANGE:
1288 print_port_range(tcp, addr, len);
1289 return;
1290 }
1291 break;
1292
1293 case SOL_IPV6:
1294 switch (name) {
1295 case IPV6_ADD_MEMBERSHIP:
1296 case IPV6_DROP_MEMBERSHIP:
1297 case IPV6_JOIN_ANYCAST:
1298 case IPV6_LEAVE_ANYCAST:
1299 print_mreq6(tcp, addr, len);
1300 return;
1301 case MCAST_JOIN_GROUP:
1302 case MCAST_LEAVE_GROUP:
1303 print_group_req(tcp, addr, len);
1304 return;
1305 }
1306 break;
1307
1308 case SOL_PACKET:
1309 switch (name) {
1310 case PACKET_RX_RING:
1311 case PACKET_TX_RING:
1312 print_tpacket_req(tcp, addr, len);
1313 return;
1314 case PACKET_ADD_MEMBERSHIP:
1315 case PACKET_DROP_MEMBERSHIP:
1316 print_packet_mreq(tcp, addr, len);
1317 return;
1318 }
1319 break;
1320
1321 case SOL_RAW:
1322 switch (name) {
1323 case ICMP_FILTER:
1324 print_icmp_filter(tcp, addr, len);
1325 return;
1326 }
1327 break;
1328
1329 case SOL_NETLINK:
1330 if (len < (int) sizeof(int))
1331 printaddr(addr);
1332 else
1333 printnum_int(tcp, addr, "%d");
1334 return;
1335 }
1336
1337 /* default arg printing */
1338
1339 if (verbose(tcp)) {
1340 if (len == sizeof(int)) {
1341 printnum_int(tcp, addr, "%d");
1342 } else {
1343 printstrn(tcp, addr, len);
1344 }
1345 } else {
1346 printaddr(addr);
1347 }
1348 }
1349
1350 SYS_FUNC(setsockopt)
1351 {
1352 print_sockopt_fd_level_name(tcp, tcp->u_arg[0],
1353 tcp->u_arg[1], tcp->u_arg[2], false);
1354 tprint_arg_next();
1355
1356 /* optval */
1357 print_setsockopt(tcp, tcp->u_arg[1], tcp->u_arg[2],
1358 tcp->u_arg[3], tcp->u_arg[4]);
1359 tprint_arg_next();
1360
1361 /* optlen */
1362 PRINT_VAL_D((int) tcp->u_arg[4]);
1363
1364 return RVAL_DECODED;
1365 }