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