Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net.c 24KB

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