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.

socketutils.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
  3. * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
  4. * Copyright (c) 2014-2018 The strace developers.
  5. * All rights reserved.
  6. *
  7. * SPDX-License-Identifier: LGPL-2.1-or-later
  8. */
  9. #include "defs.h"
  10. #include <netinet/in.h>
  11. #include <sys/socket.h>
  12. #include <arpa/inet.h>
  13. #include "netlink.h"
  14. #include <linux/sock_diag.h>
  15. #include <linux/inet_diag.h>
  16. #include <linux/unix_diag.h>
  17. #include <linux/netlink_diag.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/genetlink.h>
  20. #include <sys/un.h>
  21. #ifndef UNIX_PATH_MAX
  22. # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
  23. #endif
  24. #include "xstring.h"
  25. #define XLAT_MACROS_ONLY
  26. #include "xlat/inet_protocols.h"
  27. #undef XLAT_MACROS_ONLY
  28. typedef struct {
  29. unsigned long inode;
  30. char *details;
  31. } cache_entry;
  32. #define CACHE_SIZE 1024U
  33. static cache_entry cache[CACHE_SIZE];
  34. #define CACHE_MASK (CACHE_SIZE - 1)
  35. static int
  36. cache_inode_details(const unsigned long inode, char *const details)
  37. {
  38. cache_entry *e = &cache[inode & CACHE_MASK];
  39. free(e->details);
  40. e->inode = inode;
  41. e->details = details;
  42. return 1;
  43. }
  44. static const char *
  45. get_sockaddr_by_inode_cached(const unsigned long inode)
  46. {
  47. const cache_entry *const e = &cache[inode & CACHE_MASK];
  48. return (e && inode == e->inode) ? e->details : NULL;
  49. }
  50. static bool
  51. print_sockaddr_by_inode_cached(const unsigned long inode)
  52. {
  53. const char *const details = get_sockaddr_by_inode_cached(inode);
  54. if (details) {
  55. tprints(details);
  56. return true;
  57. }
  58. return false;
  59. }
  60. static bool
  61. send_query(struct tcb *tcp, const int fd, void *req, size_t req_size)
  62. {
  63. struct sockaddr_nl nladdr = {
  64. .nl_family = AF_NETLINK
  65. };
  66. struct iovec iov = {
  67. .iov_base = req,
  68. .iov_len = req_size
  69. };
  70. const struct msghdr msg = {
  71. .msg_name = &nladdr,
  72. .msg_namelen = sizeof(nladdr),
  73. .msg_iov = &iov,
  74. .msg_iovlen = 1
  75. };
  76. for (;;) {
  77. if (sendmsg(fd, &msg, 0) < 0) {
  78. if (errno == EINTR)
  79. continue;
  80. return false;
  81. }
  82. return true;
  83. }
  84. }
  85. static bool
  86. inet_send_query(struct tcb *tcp, const int fd, const int family,
  87. const int proto)
  88. {
  89. struct {
  90. const struct nlmsghdr nlh;
  91. const struct inet_diag_req_v2 idr;
  92. } req = {
  93. .nlh = {
  94. .nlmsg_len = sizeof(req),
  95. .nlmsg_type = SOCK_DIAG_BY_FAMILY,
  96. .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
  97. },
  98. .idr = {
  99. .sdiag_family = family,
  100. .sdiag_protocol = proto,
  101. .idiag_states = -1
  102. }
  103. };
  104. return send_query(tcp, fd, &req, sizeof(req));
  105. }
  106. static int
  107. inet_parse_response(const void *const data, const int data_len,
  108. const unsigned long inode, void *opaque_data)
  109. {
  110. const char *const proto_name = opaque_data;
  111. const struct inet_diag_msg *const diag_msg = data;
  112. static const char zero_addr[sizeof(struct in6_addr)];
  113. socklen_t addr_size, text_size;
  114. if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
  115. return -1;
  116. if (diag_msg->idiag_inode != inode)
  117. return 0;
  118. switch (diag_msg->idiag_family) {
  119. case AF_INET:
  120. addr_size = sizeof(struct in_addr);
  121. text_size = INET_ADDRSTRLEN;
  122. break;
  123. case AF_INET6:
  124. addr_size = sizeof(struct in6_addr);
  125. text_size = INET6_ADDRSTRLEN;
  126. break;
  127. default:
  128. return -1;
  129. }
  130. char src_buf[text_size];
  131. char *details;
  132. /* open/closing brackets for IPv6 addresses */
  133. const char *ob = diag_msg->idiag_family == AF_INET6 ? "[" : "";
  134. const char *cb = diag_msg->idiag_family == AF_INET6 ? "]" : "";
  135. if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
  136. src_buf, text_size))
  137. return -1;
  138. if (diag_msg->id.idiag_dport ||
  139. memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
  140. char dst_buf[text_size];
  141. if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
  142. dst_buf, text_size))
  143. return -1;
  144. if (asprintf(&details, "%s:[%s%s%s:%u->%s%s%s:%u]", proto_name,
  145. ob, src_buf, cb, ntohs(diag_msg->id.idiag_sport),
  146. ob, dst_buf, cb, ntohs(diag_msg->id.idiag_dport))
  147. < 0)
  148. return false;
  149. } else {
  150. if (asprintf(&details, "%s:[%s%s%s:%u]",
  151. proto_name, ob, src_buf, cb,
  152. ntohs(diag_msg->id.idiag_sport)) < 0)
  153. return false;
  154. }
  155. return cache_inode_details(inode, details);
  156. }
  157. static bool
  158. receive_responses(struct tcb *tcp, const int fd, const unsigned long inode,
  159. const unsigned long expected_msg_type,
  160. int (*parser)(const void *, int,
  161. unsigned long, void *),
  162. void *opaque_data)
  163. {
  164. static union {
  165. struct nlmsghdr hdr;
  166. long buf[8192 / sizeof(long)];
  167. } hdr_buf;
  168. struct sockaddr_nl nladdr = {
  169. .nl_family = AF_NETLINK
  170. };
  171. struct iovec iov = {
  172. .iov_base = hdr_buf.buf,
  173. .iov_len = sizeof(hdr_buf.buf)
  174. };
  175. int flags = 0;
  176. for (;;) {
  177. struct msghdr msg = {
  178. .msg_name = &nladdr,
  179. .msg_namelen = sizeof(nladdr),
  180. .msg_iov = &iov,
  181. .msg_iovlen = 1
  182. };
  183. ssize_t ret = recvmsg(fd, &msg, flags);
  184. if (ret < 0) {
  185. if (errno == EINTR)
  186. continue;
  187. return false;
  188. }
  189. const struct nlmsghdr *h = &hdr_buf.hdr;
  190. if (!is_nlmsg_ok(h, ret))
  191. return false;
  192. for (; is_nlmsg_ok(h, ret); h = NLMSG_NEXT(h, ret)) {
  193. if (h->nlmsg_type != expected_msg_type)
  194. return false;
  195. const int rc = parser(NLMSG_DATA(h),
  196. h->nlmsg_len, inode, opaque_data);
  197. if (rc > 0)
  198. return true;
  199. if (rc < 0)
  200. return false;
  201. }
  202. flags = MSG_DONTWAIT;
  203. }
  204. }
  205. static bool
  206. unix_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
  207. {
  208. /*
  209. * The kernel bug was fixed in mainline by commit v4.5-rc6~35^2~11
  210. * and backported to stable/linux-4.4.y by commit v4.4.4~297.
  211. */
  212. const uint16_t dump_flag =
  213. os_release < KERNEL_VERSION(4, 4, 4) ? NLM_F_DUMP : 0;
  214. struct {
  215. const struct nlmsghdr nlh;
  216. const struct unix_diag_req udr;
  217. } req = {
  218. .nlh = {
  219. .nlmsg_len = sizeof(req),
  220. .nlmsg_type = SOCK_DIAG_BY_FAMILY,
  221. .nlmsg_flags = NLM_F_REQUEST | dump_flag
  222. },
  223. .udr = {
  224. .sdiag_family = AF_UNIX,
  225. .udiag_ino = inode,
  226. .udiag_states = -1,
  227. .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
  228. .udiag_cookie = { ~0U, ~0U }
  229. }
  230. };
  231. return send_query(tcp, fd, &req, sizeof(req));
  232. }
  233. static int
  234. unix_parse_response(const void *data, const int data_len,
  235. const unsigned long inode, void *opaque_data)
  236. {
  237. const char *proto_name = opaque_data;
  238. const struct unix_diag_msg *diag_msg = data;
  239. struct rtattr *attr;
  240. int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
  241. uint32_t peer = 0;
  242. size_t path_len = 0;
  243. char path[UNIX_PATH_MAX + 1];
  244. if (rta_len < 0)
  245. return -1;
  246. if (diag_msg->udiag_ino != inode)
  247. return 0;
  248. if (diag_msg->udiag_family != AF_UNIX)
  249. return -1;
  250. for (attr = (struct rtattr *) (diag_msg + 1);
  251. RTA_OK(attr, rta_len);
  252. attr = RTA_NEXT(attr, rta_len)) {
  253. switch (attr->rta_type) {
  254. case UNIX_DIAG_NAME:
  255. if (!path_len) {
  256. path_len = RTA_PAYLOAD(attr);
  257. if (path_len > UNIX_PATH_MAX)
  258. path_len = UNIX_PATH_MAX;
  259. memcpy(path, RTA_DATA(attr), path_len);
  260. path[path_len] = '\0';
  261. }
  262. break;
  263. case UNIX_DIAG_PEER:
  264. if (RTA_PAYLOAD(attr) >= 4)
  265. peer = *(uint32_t *) RTA_DATA(attr);
  266. break;
  267. }
  268. }
  269. /*
  270. * print obtained information in the following format:
  271. * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
  272. */
  273. if (!peer && !path_len)
  274. return -1;
  275. char peer_str[3 + sizeof(peer) * 3];
  276. if (peer)
  277. xsprintf(peer_str, "->%u", peer);
  278. else
  279. peer_str[0] = '\0';
  280. const char *path_str;
  281. if (path_len) {
  282. char *outstr = alloca(4 * path_len + 4);
  283. outstr[0] = ',';
  284. if (path[0] == '\0') {
  285. outstr[1] = '@';
  286. string_quote(path + 1, outstr + 2,
  287. path_len - 1, QUOTE_0_TERMINATED, NULL);
  288. } else {
  289. string_quote(path, outstr + 1,
  290. path_len, QUOTE_0_TERMINATED, NULL);
  291. }
  292. path_str = outstr;
  293. } else {
  294. path_str = "";
  295. }
  296. char *details;
  297. if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
  298. peer_str, path_str) < 0)
  299. return -1;
  300. return cache_inode_details(inode, details);
  301. }
  302. static bool
  303. netlink_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
  304. {
  305. struct {
  306. const struct nlmsghdr nlh;
  307. const struct netlink_diag_req ndr;
  308. } req = {
  309. .nlh = {
  310. .nlmsg_len = sizeof(req),
  311. .nlmsg_type = SOCK_DIAG_BY_FAMILY,
  312. .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
  313. },
  314. .ndr = {
  315. .sdiag_family = AF_NETLINK,
  316. .sdiag_protocol = NDIAG_PROTO_ALL
  317. }
  318. };
  319. return send_query(tcp, fd, &req, sizeof(req));
  320. }
  321. static int
  322. netlink_parse_response(const void *data, const int data_len,
  323. const unsigned long inode, void *opaque_data)
  324. {
  325. const char *proto_name = opaque_data;
  326. const struct netlink_diag_msg *const diag_msg = data;
  327. const char *netlink_proto;
  328. char *details;
  329. if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
  330. return -1;
  331. if (diag_msg->ndiag_ino != inode)
  332. return 0;
  333. if (diag_msg->ndiag_family != AF_NETLINK)
  334. return -1;
  335. netlink_proto = xlookup(netlink_protocols,
  336. diag_msg->ndiag_protocol);
  337. if (netlink_proto) {
  338. netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
  339. if (asprintf(&details, "%s:[%s:%u]", proto_name,
  340. netlink_proto, diag_msg->ndiag_portid) < 0)
  341. return -1;
  342. } else {
  343. if (asprintf(&details, "%s:[%u]", proto_name,
  344. (unsigned) diag_msg->ndiag_protocol) < 0)
  345. return -1;
  346. }
  347. return cache_inode_details(inode, details);
  348. }
  349. static const char *
  350. unix_get(struct tcb *tcp, const int fd, const int family, const int proto,
  351. const unsigned long inode, const char *name)
  352. {
  353. return unix_send_query(tcp, fd, inode)
  354. && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
  355. unix_parse_response, (void *) name)
  356. ? get_sockaddr_by_inode_cached(inode) : NULL;
  357. }
  358. static const char *
  359. inet_get(struct tcb *tcp, const int fd, const int family, const int protocol,
  360. const unsigned long inode, const char *proto_name)
  361. {
  362. return inet_send_query(tcp, fd, family, protocol)
  363. && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
  364. inet_parse_response, (void *) proto_name)
  365. ? get_sockaddr_by_inode_cached(inode) : NULL;
  366. }
  367. static const char *
  368. netlink_get(struct tcb *tcp, const int fd, const int family, const int protocol,
  369. const unsigned long inode, const char *proto_name)
  370. {
  371. return netlink_send_query(tcp, fd, inode)
  372. && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
  373. netlink_parse_response,
  374. (void *) proto_name)
  375. ? get_sockaddr_by_inode_cached(inode) : NULL;
  376. }
  377. static const struct {
  378. const char *const name;
  379. const char * (*const get)(struct tcb *, int fd, int family,
  380. int protocol, unsigned long inode,
  381. const char *proto_name);
  382. int family;
  383. int proto;
  384. } protocols[] = {
  385. [SOCK_PROTO_UNIX] = { "UNIX", unix_get, AF_UNIX},
  386. /*
  387. * inet_diag handlers are currently implemented only for TCP,
  388. * UDP(lite), SCTP, RAW, and DCCP, but we try to resolve it for all
  389. * protocols anyway, just in case.
  390. */
  391. [SOCK_PROTO_TCP] =
  392. { "TCP", inet_get, AF_INET, IPPROTO_TCP },
  393. [SOCK_PROTO_UDP] =
  394. { "UDP", inet_get, AF_INET, IPPROTO_UDP },
  395. [SOCK_PROTO_UDPLITE] =
  396. { "UDPLITE", inet_get, AF_INET, IPPROTO_UDPLITE },
  397. [SOCK_PROTO_DCCP] =
  398. { "DCCP", inet_get, AF_INET, IPPROTO_DCCP },
  399. [SOCK_PROTO_SCTP] =
  400. { "SCTP", inet_get, AF_INET, IPPROTO_SCTP },
  401. [SOCK_PROTO_L2TP_IP] =
  402. { "L2TP/IP", inet_get, AF_INET, IPPROTO_L2TP },
  403. [SOCK_PROTO_PING] =
  404. { "PING", inet_get, AF_INET, IPPROTO_ICMP },
  405. [SOCK_PROTO_RAW] =
  406. { "RAW", inet_get, AF_INET, IPPROTO_RAW },
  407. [SOCK_PROTO_TCPv6] =
  408. { "TCPv6", inet_get, AF_INET6, IPPROTO_TCP },
  409. [SOCK_PROTO_UDPv6] =
  410. { "UDPv6", inet_get, AF_INET6, IPPROTO_UDP },
  411. [SOCK_PROTO_UDPLITEv6] =
  412. { "UDPLITEv6", inet_get, AF_INET6, IPPROTO_UDPLITE },
  413. [SOCK_PROTO_DCCPv6] =
  414. { "DCCPv6", inet_get, AF_INET6, IPPROTO_DCCP },
  415. [SOCK_PROTO_SCTPv6] =
  416. { "SCTPv6", inet_get, AF_INET6, IPPROTO_SCTP },
  417. [SOCK_PROTO_L2TP_IPv6] =
  418. { "L2TP/IPv6", inet_get, AF_INET6, IPPROTO_L2TP },
  419. [SOCK_PROTO_PINGv6] =
  420. { "PINGv6", inet_get, AF_INET6, IPPROTO_ICMP },
  421. [SOCK_PROTO_RAWv6] =
  422. { "RAWv6", inet_get, AF_INET6, IPPROTO_RAW },
  423. [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get, AF_NETLINK },
  424. };
  425. enum sock_proto
  426. get_proto_by_name(const char *const name)
  427. {
  428. unsigned int i;
  429. for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
  430. i < ARRAY_SIZE(protocols); ++i) {
  431. if (protocols[i].name && !strcmp(name, protocols[i].name))
  432. return (enum sock_proto) i;
  433. }
  434. return SOCK_PROTO_UNKNOWN;
  435. }
  436. int
  437. get_family_by_proto(enum sock_proto proto)
  438. {
  439. if ((size_t) proto < ARRAY_SIZE(protocols))
  440. return protocols[proto].family;
  441. return AF_UNSPEC;
  442. }
  443. static const char *
  444. get_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
  445. const enum sock_proto proto)
  446. {
  447. if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
  448. (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
  449. return NULL;
  450. const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
  451. if (fd < 0)
  452. return NULL;
  453. const char *details = NULL;
  454. if (proto != SOCK_PROTO_UNKNOWN) {
  455. details = protocols[proto].get(tcp, fd, protocols[proto].family,
  456. protocols[proto].proto, inode,
  457. protocols[proto].name);
  458. } else {
  459. unsigned int i;
  460. for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
  461. i < ARRAY_SIZE(protocols); ++i) {
  462. if (!protocols[i].get)
  463. continue;
  464. details = protocols[i].get(tcp, fd,
  465. protocols[proto].family,
  466. protocols[proto].proto,
  467. inode,
  468. protocols[proto].name);
  469. if (details)
  470. break;
  471. }
  472. }
  473. close(fd);
  474. return details;
  475. }
  476. static bool
  477. print_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
  478. const enum sock_proto proto)
  479. {
  480. const char *details = get_sockaddr_by_inode_uncached(tcp, inode, proto);
  481. if (details) {
  482. tprints(details);
  483. return true;
  484. }
  485. if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
  486. protocols[proto].name) {
  487. tprintf("%s:[%lu]", protocols[proto].name, inode);
  488. return true;
  489. }
  490. return false;
  491. }
  492. /* Given an inode number of a socket, return its protocol details. */
  493. const char *
  494. get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
  495. const unsigned long inode)
  496. {
  497. const char *details = get_sockaddr_by_inode_cached(inode);
  498. return details ? details :
  499. get_sockaddr_by_inode_uncached(tcp, inode, getfdproto(tcp, fd));
  500. }
  501. /* Given an inode number of a socket, print out its protocol details. */
  502. bool
  503. print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
  504. const unsigned long inode)
  505. {
  506. return print_sockaddr_by_inode_cached(inode) ? true :
  507. print_sockaddr_by_inode_uncached(tcp, inode,
  508. getfdproto(tcp, fd));
  509. }
  510. /*
  511. * Managing the cache for decoding communications of Netlink GENERIC protocol
  512. *
  513. * As name shown Netlink GENERIC protocol is generic protocol. The
  514. * numbers of msg types used in the protocol are not defined
  515. * statically. Kernel defines them on demand. So the xlat converted
  516. * from header files doesn't help for decoding the protocol. Following
  517. * codes are building xlat(dyxlat) at runtime.
  518. */
  519. static bool
  520. genl_send_dump_families(struct tcb *tcp, const int fd)
  521. {
  522. struct {
  523. const struct nlmsghdr nlh;
  524. struct genlmsghdr gnlh;
  525. } req = {
  526. .nlh = {
  527. .nlmsg_len = sizeof(req),
  528. .nlmsg_type = GENL_ID_CTRL,
  529. .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
  530. },
  531. .gnlh = {
  532. .cmd = CTRL_CMD_GETFAMILY,
  533. }
  534. };
  535. return send_query(tcp, fd, &req, sizeof(req));
  536. }
  537. static int
  538. genl_parse_families_response(const void *const data,
  539. const int data_len, const unsigned long inode,
  540. void *opaque_data)
  541. {
  542. struct dyxlat *const dyxlat = opaque_data;
  543. const struct genlmsghdr *const gnlh = data;
  544. struct rtattr *attr;
  545. int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
  546. char *name = NULL;
  547. unsigned int name_len = 0;
  548. uint16_t *id = NULL;
  549. if (rta_len < 0)
  550. return -1;
  551. if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
  552. return -1;
  553. if (gnlh->version != 2)
  554. return -1;
  555. for (attr = (struct rtattr *) (gnlh + 1);
  556. RTA_OK(attr, rta_len);
  557. attr = RTA_NEXT(attr, rta_len)) {
  558. switch (attr->rta_type) {
  559. case CTRL_ATTR_FAMILY_NAME:
  560. if (!name) {
  561. name = RTA_DATA(attr);
  562. name_len = RTA_PAYLOAD(attr);
  563. }
  564. break;
  565. case CTRL_ATTR_FAMILY_ID:
  566. if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
  567. id = RTA_DATA(attr);
  568. break;
  569. }
  570. if (name && id) {
  571. dyxlat_add_pair(dyxlat, *id, name, name_len);
  572. name = NULL;
  573. id = NULL;
  574. }
  575. }
  576. return 0;
  577. }
  578. const struct xlat *
  579. genl_families_xlat(struct tcb *tcp)
  580. {
  581. static struct dyxlat *dyxlat;
  582. if (!dyxlat) {
  583. dyxlat = dyxlat_alloc(32);
  584. int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  585. if (fd < 0)
  586. goto out;
  587. if (genl_send_dump_families(tcp, fd))
  588. receive_responses(tcp, fd, 0, GENL_ID_CTRL,
  589. genl_parse_families_response, dyxlat);
  590. close(fd);
  591. }
  592. out:
  593. return dyxlat_get(dyxlat);
  594. }