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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "defs.h"
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include "netlink.h"
35 #include <linux/sock_diag.h>
36 #include <linux/inet_diag.h>
37 #include <linux/unix_diag.h>
38 #include <linux/netlink_diag.h>
39 #include <linux/rtnetlink.h>
40 #if HAVE_LINUX_GENETLINK_H
41 #include <linux/genetlink.h>
42 #endif
43
44 #include <sys/un.h>
45 #ifndef UNIX_PATH_MAX
46 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
47 #endif
48
49 #include "xstring.h"
50
51 #define XLAT_MACROS_ONLY
52 # include "xlat/inet_protocols.h"
53 #undef XLAT_MACROS_ONLY
54
55 typedef struct {
56 unsigned long inode;
57 char *details;
58 } cache_entry;
59
60 #define CACHE_SIZE 1024U
61 static cache_entry cache[CACHE_SIZE];
62 #define CACHE_MASK (CACHE_SIZE - 1)
63
64 static int
cache_inode_details(const unsigned long inode,char * const details)65 cache_inode_details(const unsigned long inode, char *const details)
66 {
67 cache_entry *e = &cache[inode & CACHE_MASK];
68 free(e->details);
69 e->inode = inode;
70 e->details = details;
71
72 return 1;
73 }
74
75 static const char *
get_sockaddr_by_inode_cached(const unsigned long inode)76 get_sockaddr_by_inode_cached(const unsigned long inode)
77 {
78 const cache_entry *const e = &cache[inode & CACHE_MASK];
79 return (e && inode == e->inode) ? e->details : NULL;
80 }
81
82 static bool
print_sockaddr_by_inode_cached(const unsigned long inode)83 print_sockaddr_by_inode_cached(const unsigned long inode)
84 {
85 const char *const details = get_sockaddr_by_inode_cached(inode);
86 if (details) {
87 tprints(details);
88 return true;
89 }
90 return false;
91 }
92
93 static bool
send_query(struct tcb * tcp,const int fd,void * req,size_t req_size)94 send_query(struct tcb *tcp, const int fd, void *req, size_t req_size)
95 {
96 struct sockaddr_nl nladdr = {
97 .nl_family = AF_NETLINK
98 };
99 struct iovec iov = {
100 .iov_base = req,
101 .iov_len = req_size
102 };
103 const struct msghdr msg = {
104 .msg_name = &nladdr,
105 .msg_namelen = sizeof(nladdr),
106 .msg_iov = &iov,
107 .msg_iovlen = 1
108 };
109
110 for (;;) {
111 if (sendmsg(fd, &msg, 0) < 0) {
112 if (errno == EINTR)
113 continue;
114 return false;
115 }
116 return true;
117 }
118 }
119
120 static bool
inet_send_query(struct tcb * tcp,const int fd,const int family,const int proto)121 inet_send_query(struct tcb *tcp, const int fd, const int family,
122 const int proto)
123 {
124 struct {
125 const struct nlmsghdr nlh;
126 const struct inet_diag_req_v2 idr;
127 } req = {
128 .nlh = {
129 .nlmsg_len = sizeof(req),
130 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
131 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
132 },
133 .idr = {
134 .sdiag_family = family,
135 .sdiag_protocol = proto,
136 .idiag_states = -1
137 }
138 };
139 return send_query(tcp, fd, &req, sizeof(req));
140 }
141
142 static int
inet_parse_response(const void * const data,const int data_len,const unsigned long inode,void * opaque_data)143 inet_parse_response(const void *const data, const int data_len,
144 const unsigned long inode, void *opaque_data)
145 {
146 const char *const proto_name = opaque_data;
147 const struct inet_diag_msg *const diag_msg = data;
148 static const char zero_addr[sizeof(struct in6_addr)];
149 socklen_t addr_size, text_size;
150
151 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
152 return -1;
153 if (diag_msg->idiag_inode != inode)
154 return 0;
155
156 switch (diag_msg->idiag_family) {
157 case AF_INET:
158 addr_size = sizeof(struct in_addr);
159 text_size = INET_ADDRSTRLEN;
160 break;
161 case AF_INET6:
162 addr_size = sizeof(struct in6_addr);
163 text_size = INET6_ADDRSTRLEN;
164 break;
165 default:
166 return -1;
167 }
168
169 char src_buf[text_size];
170 char *details;
171
172 /* open/closing brackets for IPv6 addresses */
173 const char *ob = diag_msg->idiag_family == AF_INET6 ? "[" : "";
174 const char *cb = diag_msg->idiag_family == AF_INET6 ? "]" : "";
175
176 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
177 src_buf, text_size))
178 return -1;
179
180 if (diag_msg->id.idiag_dport ||
181 memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
182 char dst_buf[text_size];
183
184 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
185 dst_buf, text_size))
186 return -1;
187
188 if (asprintf(&details, "%s:[%s%s%s:%u->%s%s%s:%u]", proto_name,
189 ob, src_buf, cb, ntohs(diag_msg->id.idiag_sport),
190 ob, dst_buf, cb, ntohs(diag_msg->id.idiag_dport))
191 < 0)
192 return false;
193 } else {
194 if (asprintf(&details, "%s:[%s%s%s:%u]",
195 proto_name, ob, src_buf, cb,
196 ntohs(diag_msg->id.idiag_sport)) < 0)
197 return false;
198 }
199
200 return cache_inode_details(inode, details);
201 }
202
203 static bool
receive_responses(struct tcb * tcp,const int fd,const unsigned long inode,const unsigned long expected_msg_type,int (* parser)(const void *,int,unsigned long,void *),void * opaque_data)204 receive_responses(struct tcb *tcp, const int fd, const unsigned long inode,
205 const unsigned long expected_msg_type,
206 int (*parser)(const void *, int,
207 unsigned long, void *),
208 void *opaque_data)
209 {
210 static union {
211 struct nlmsghdr hdr;
212 long buf[8192 / sizeof(long)];
213 } hdr_buf;
214
215 struct sockaddr_nl nladdr = {
216 .nl_family = AF_NETLINK
217 };
218 struct iovec iov = {
219 .iov_base = hdr_buf.buf,
220 .iov_len = sizeof(hdr_buf.buf)
221 };
222 int flags = 0;
223
224 for (;;) {
225 struct msghdr msg = {
226 .msg_name = &nladdr,
227 .msg_namelen = sizeof(nladdr),
228 .msg_iov = &iov,
229 .msg_iovlen = 1
230 };
231
232 ssize_t ret = recvmsg(fd, &msg, flags);
233 if (ret < 0) {
234 if (errno == EINTR)
235 continue;
236 return false;
237 }
238
239 const struct nlmsghdr *h = &hdr_buf.hdr;
240 if (!is_nlmsg_ok(h, ret))
241 return false;
242 for (; is_nlmsg_ok(h, ret); h = NLMSG_NEXT(h, ret)) {
243 if (h->nlmsg_type != expected_msg_type)
244 return false;
245 const int rc = parser(NLMSG_DATA(h),
246 h->nlmsg_len, inode, opaque_data);
247 if (rc > 0)
248 return true;
249 if (rc < 0)
250 return false;
251 }
252 flags = MSG_DONTWAIT;
253 }
254 }
255
256 static bool
unix_send_query(struct tcb * tcp,const int fd,const unsigned long inode)257 unix_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
258 {
259 /*
260 * The kernel bug was fixed in mainline by commit v4.5-rc6~35^2~11
261 * and backported to stable/linux-4.4.y by commit v4.4.4~297.
262 */
263 const uint16_t dump_flag =
264 os_release < KERNEL_VERSION(4, 4, 4) ? NLM_F_DUMP : 0;
265
266 struct {
267 const struct nlmsghdr nlh;
268 const struct unix_diag_req udr;
269 } req = {
270 .nlh = {
271 .nlmsg_len = sizeof(req),
272 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
273 .nlmsg_flags = NLM_F_REQUEST | dump_flag
274 },
275 .udr = {
276 .sdiag_family = AF_UNIX,
277 .udiag_ino = inode,
278 .udiag_states = -1,
279 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
280 .udiag_cookie = { ~0U, ~0U }
281 }
282 };
283 return send_query(tcp, fd, &req, sizeof(req));
284 }
285
286 static int
unix_parse_response(const void * data,const int data_len,const unsigned long inode,void * opaque_data)287 unix_parse_response(const void *data, const int data_len,
288 const unsigned long inode, void *opaque_data)
289 {
290 const char *proto_name = opaque_data;
291 const struct unix_diag_msg *diag_msg = data;
292 struct rtattr *attr;
293 int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
294 uint32_t peer = 0;
295 size_t path_len = 0;
296 char path[UNIX_PATH_MAX + 1];
297
298 if (rta_len < 0)
299 return -1;
300 if (diag_msg->udiag_ino != inode)
301 return 0;
302 if (diag_msg->udiag_family != AF_UNIX)
303 return -1;
304
305 for (attr = (struct rtattr *) (diag_msg + 1);
306 RTA_OK(attr, rta_len);
307 attr = RTA_NEXT(attr, rta_len)) {
308 switch (attr->rta_type) {
309 case UNIX_DIAG_NAME:
310 if (!path_len) {
311 path_len = RTA_PAYLOAD(attr);
312 if (path_len > UNIX_PATH_MAX)
313 path_len = UNIX_PATH_MAX;
314 memcpy(path, RTA_DATA(attr), path_len);
315 path[path_len] = '\0';
316 }
317 break;
318 case UNIX_DIAG_PEER:
319 if (RTA_PAYLOAD(attr) >= 4)
320 peer = *(uint32_t *) RTA_DATA(attr);
321 break;
322 }
323 }
324
325 /*
326 * print obtained information in the following format:
327 * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
328 */
329 if (!peer && !path_len)
330 return -1;
331
332 char peer_str[3 + sizeof(peer) * 3];
333 if (peer)
334 xsprintf(peer_str, "->%u", peer);
335 else
336 peer_str[0] = '\0';
337
338 const char *path_str;
339 if (path_len) {
340 char *outstr = alloca(4 * path_len + 4);
341
342 outstr[0] = ',';
343 if (path[0] == '\0') {
344 outstr[1] = '@';
345 string_quote(path + 1, outstr + 2,
346 path_len - 1, QUOTE_0_TERMINATED, NULL);
347 } else {
348 string_quote(path, outstr + 1,
349 path_len, QUOTE_0_TERMINATED, NULL);
350 }
351 path_str = outstr;
352 } else {
353 path_str = "";
354 }
355
356 char *details;
357 if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
358 peer_str, path_str) < 0)
359 return -1;
360
361 return cache_inode_details(inode, details);
362 }
363
364 static bool
netlink_send_query(struct tcb * tcp,const int fd,const unsigned long inode)365 netlink_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
366 {
367 struct {
368 const struct nlmsghdr nlh;
369 const struct netlink_diag_req ndr;
370 } req = {
371 .nlh = {
372 .nlmsg_len = sizeof(req),
373 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
374 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
375 },
376 .ndr = {
377 .sdiag_family = AF_NETLINK,
378 .sdiag_protocol = NDIAG_PROTO_ALL
379 }
380 };
381 return send_query(tcp, fd, &req, sizeof(req));
382 }
383
384 static int
netlink_parse_response(const void * data,const int data_len,const unsigned long inode,void * opaque_data)385 netlink_parse_response(const void *data, const int data_len,
386 const unsigned long inode, void *opaque_data)
387 {
388 const char *proto_name = opaque_data;
389 const struct netlink_diag_msg *const diag_msg = data;
390 const char *netlink_proto;
391 char *details;
392
393 if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
394 return -1;
395 if (diag_msg->ndiag_ino != inode)
396 return 0;
397
398 if (diag_msg->ndiag_family != AF_NETLINK)
399 return -1;
400
401 netlink_proto = xlookup(netlink_protocols,
402 diag_msg->ndiag_protocol);
403
404 if (netlink_proto) {
405 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
406 if (asprintf(&details, "%s:[%s:%u]", proto_name,
407 netlink_proto, diag_msg->ndiag_portid) < 0)
408 return -1;
409 } else {
410 if (asprintf(&details, "%s:[%u]", proto_name,
411 (unsigned) diag_msg->ndiag_protocol) < 0)
412 return -1;
413 }
414
415 return cache_inode_details(inode, details);
416 }
417
418 static const char *
unix_get(struct tcb * tcp,const int fd,const int family,const int proto,const unsigned long inode,const char * name)419 unix_get(struct tcb *tcp, const int fd, const int family, const int proto,
420 const unsigned long inode, const char *name)
421 {
422 return unix_send_query(tcp, fd, inode)
423 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
424 unix_parse_response, (void *) name)
425 ? get_sockaddr_by_inode_cached(inode) : NULL;
426 }
427
428 static const char *
inet_get(struct tcb * tcp,const int fd,const int family,const int protocol,const unsigned long inode,const char * proto_name)429 inet_get(struct tcb *tcp, const int fd, const int family, const int protocol,
430 const unsigned long inode, const char *proto_name)
431 {
432 return inet_send_query(tcp, fd, family, protocol)
433 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
434 inet_parse_response, (void *) proto_name)
435 ? get_sockaddr_by_inode_cached(inode) : NULL;
436 }
437
438 static const char *
netlink_get(struct tcb * tcp,const int fd,const int family,const int protocol,const unsigned long inode,const char * proto_name)439 netlink_get(struct tcb *tcp, const int fd, const int family, const int protocol,
440 const unsigned long inode, const char *proto_name)
441 {
442 return netlink_send_query(tcp, fd, inode)
443 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
444 netlink_parse_response,
445 (void *) proto_name)
446 ? get_sockaddr_by_inode_cached(inode) : NULL;
447 }
448
449 static const struct {
450 const char *const name;
451 const char * (*const get)(struct tcb *, int fd, int family,
452 int protocol, unsigned long inode,
453 const char *proto_name);
454 int family;
455 int proto;
456 } protocols[] = {
457 [SOCK_PROTO_UNIX] = { "UNIX", unix_get, AF_UNIX},
458 /*
459 * inet_diag handlers are currently implemented only for TCP,
460 * UDP(lite), SCTP, RAW, and DCCP, but we try to resolve it for all
461 * protocols anyway, just in case.
462 */
463 [SOCK_PROTO_TCP] =
464 { "TCP", inet_get, AF_INET, IPPROTO_TCP },
465 [SOCK_PROTO_UDP] =
466 { "UDP", inet_get, AF_INET, IPPROTO_UDP },
467 [SOCK_PROTO_UDPLITE] =
468 { "UDPLITE", inet_get, AF_INET, IPPROTO_UDPLITE },
469 [SOCK_PROTO_DCCP] =
470 { "DCCP", inet_get, AF_INET, IPPROTO_DCCP },
471 [SOCK_PROTO_SCTP] =
472 { "SCTP", inet_get, AF_INET, IPPROTO_SCTP },
473 [SOCK_PROTO_L2TP_IP] =
474 { "L2TP/IP", inet_get, AF_INET, IPPROTO_L2TP },
475 [SOCK_PROTO_PING] =
476 { "PING", inet_get, AF_INET, IPPROTO_ICMP },
477 [SOCK_PROTO_RAW] =
478 { "RAW", inet_get, AF_INET, IPPROTO_RAW },
479 [SOCK_PROTO_TCPv6] =
480 { "TCPv6", inet_get, AF_INET6, IPPROTO_TCP },
481 [SOCK_PROTO_UDPv6] =
482 { "UDPv6", inet_get, AF_INET6, IPPROTO_UDP },
483 [SOCK_PROTO_UDPLITEv6] =
484 { "UDPLITEv6", inet_get, AF_INET6, IPPROTO_UDPLITE },
485 [SOCK_PROTO_DCCPv6] =
486 { "DCCPv6", inet_get, AF_INET6, IPPROTO_DCCP },
487 [SOCK_PROTO_SCTPv6] =
488 { "SCTPv6", inet_get, AF_INET6, IPPROTO_SCTP },
489 [SOCK_PROTO_L2TP_IPv6] =
490 { "L2TP/IPv6", inet_get, AF_INET6, IPPROTO_L2TP },
491 [SOCK_PROTO_PINGv6] =
492 { "PINGv6", inet_get, AF_INET6, IPPROTO_ICMP },
493 [SOCK_PROTO_RAWv6] =
494 { "RAWv6", inet_get, AF_INET6, IPPROTO_RAW },
495 [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get, AF_NETLINK },
496 };
497
498 enum sock_proto
get_proto_by_name(const char * const name)499 get_proto_by_name(const char *const name)
500 {
501 unsigned int i;
502 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
503 i < ARRAY_SIZE(protocols); ++i) {
504 if (protocols[i].name && !strcmp(name, protocols[i].name))
505 return (enum sock_proto) i;
506 }
507 return SOCK_PROTO_UNKNOWN;
508 }
509
510 int
get_family_by_proto(enum sock_proto proto)511 get_family_by_proto(enum sock_proto proto)
512 {
513 if ((size_t) proto < ARRAY_SIZE(protocols))
514 return protocols[proto].family;
515
516 return AF_UNSPEC;
517 }
518
519 static const char *
get_sockaddr_by_inode_uncached(struct tcb * tcp,const unsigned long inode,const enum sock_proto proto)520 get_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
521 const enum sock_proto proto)
522 {
523 if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
524 (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
525 return NULL;
526
527 const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
528 if (fd < 0)
529 return NULL;
530 const char *details = NULL;
531
532 if (proto != SOCK_PROTO_UNKNOWN) {
533 details = protocols[proto].get(tcp, fd, protocols[proto].family,
534 protocols[proto].proto, inode,
535 protocols[proto].name);
536 } else {
537 unsigned int i;
538 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
539 i < ARRAY_SIZE(protocols); ++i) {
540 if (!protocols[i].get)
541 continue;
542 details = protocols[i].get(tcp, fd,
543 protocols[proto].family,
544 protocols[proto].proto,
545 inode,
546 protocols[proto].name);
547 if (details)
548 break;
549 }
550 }
551
552 close(fd);
553 return details;
554 }
555
556 static bool
print_sockaddr_by_inode_uncached(struct tcb * tcp,const unsigned long inode,const enum sock_proto proto)557 print_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
558 const enum sock_proto proto)
559 {
560 const char *details = get_sockaddr_by_inode_uncached(tcp, inode, proto);
561
562 if (details) {
563 tprints(details);
564 return true;
565 }
566
567 if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
568 protocols[proto].name) {
569 tprintf("%s:[%lu]", protocols[proto].name, inode);
570 return true;
571 }
572
573 return false;
574 }
575
576 /* Given an inode number of a socket, return its protocol details. */
577 const char *
get_sockaddr_by_inode(struct tcb * const tcp,const int fd,const unsigned long inode)578 get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
579 const unsigned long inode)
580 {
581 const char *details = get_sockaddr_by_inode_cached(inode);
582 return details ? details :
583 get_sockaddr_by_inode_uncached(tcp, inode, getfdproto(tcp, fd));
584 }
585
586 /* Given an inode number of a socket, print out its protocol details. */
587 bool
print_sockaddr_by_inode(struct tcb * const tcp,const int fd,const unsigned long inode)588 print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
589 const unsigned long inode)
590 {
591 return print_sockaddr_by_inode_cached(inode) ? true :
592 print_sockaddr_by_inode_uncached(tcp, inode,
593 getfdproto(tcp, fd));
594 }
595
596 #ifdef HAVE_LINUX_GENETLINK_H
597 /*
598 * Managing the cache for decoding communications of Netlink GENERIC protocol
599 *
600 * As name shown Netlink GENERIC protocol is generic protocol. The
601 * numbers of msg types used in the protocol are not defined
602 * statically. Kernel defines them on demand. So the xlat converted
603 * from header files doesn't help for decoding the protocol. Following
604 * codes are building xlat(dyxlat) at runtime.
605 */
606 static bool
genl_send_dump_families(struct tcb * tcp,const int fd)607 genl_send_dump_families(struct tcb *tcp, const int fd)
608 {
609 struct {
610 const struct nlmsghdr nlh;
611 struct genlmsghdr gnlh;
612 } req = {
613 .nlh = {
614 .nlmsg_len = sizeof(req),
615 .nlmsg_type = GENL_ID_CTRL,
616 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
617 },
618 .gnlh = {
619 .cmd = CTRL_CMD_GETFAMILY,
620 }
621 };
622 return send_query(tcp, fd, &req, sizeof(req));
623 }
624
625 static int
genl_parse_families_response(const void * const data,const int data_len,const unsigned long inode,void * opaque_data)626 genl_parse_families_response(const void *const data,
627 const int data_len, const unsigned long inode,
628 void *opaque_data)
629 {
630 struct dyxlat *const dyxlat = opaque_data;
631 const struct genlmsghdr *const gnlh = data;
632 struct rtattr *attr;
633 int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
634
635 char *name = NULL;
636 unsigned int name_len = 0;
637 uint16_t *id = NULL;
638
639 if (rta_len < 0)
640 return -1;
641 if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
642 return -1;
643 if (gnlh->version != 2)
644 return -1;
645
646 for (attr = (struct rtattr *) (gnlh + 1);
647 RTA_OK(attr, rta_len);
648 attr = RTA_NEXT(attr, rta_len)) {
649 switch (attr->rta_type) {
650 case CTRL_ATTR_FAMILY_NAME:
651 if (!name) {
652 name = RTA_DATA(attr);
653 name_len = RTA_PAYLOAD(attr);
654 }
655 break;
656 case CTRL_ATTR_FAMILY_ID:
657 if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
658 id = RTA_DATA(attr);
659 break;
660 }
661
662 if (name && id) {
663 dyxlat_add_pair(dyxlat, *id, name, name_len);
664 name = NULL;
665 id = NULL;
666 }
667 }
668
669 return 0;
670 }
671
672 const struct xlat *
genl_families_xlat(struct tcb * tcp)673 genl_families_xlat(struct tcb *tcp)
674 {
675 static struct dyxlat *dyxlat;
676
677 if (!dyxlat) {
678 dyxlat = dyxlat_alloc(32);
679
680 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
681 if (fd < 0)
682 goto out;
683
684 if (genl_send_dump_families(tcp, fd))
685 receive_responses(tcp, fd, 0, GENL_ID_CTRL,
686 genl_parse_families_response, dyxlat);
687 close(fd);
688 }
689
690 out:
691 return dyxlat_get(dyxlat);
692 }
693
694 #else /* !HAVE_LINUX_GENETLINK_H */
695
696 const struct xlat *
genl_families_xlat(struct tcb * tcp)697 genl_families_xlat(struct tcb *tcp)
698 {
699 return NULL;
700 }
701 #endif
702