2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2017 The strace developers.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/socket.h>
33 #include <linux/netlink.h>
34 #include <linux/unix_diag.h>
36 #include "xlat/tcp_states.h"
37 #include "xlat/tcp_state_flags.h"
39 #include "xlat/unix_diag_show.h"
42 decode_family(struct tcb *const tcp, const uint8_t family,
43 const kernel_ulong_t addr, const kernel_ulong_t len)
46 printxval(addrfams, family, "AF_???");
47 if (len > sizeof(family)) {
49 printstrn(tcp, addr + sizeof(family),
50 len - sizeof(family));
56 decode_unix_diag_req(struct tcb *const tcp,
57 const struct nlmsghdr *const nlmsghdr,
59 const kernel_ulong_t addr,
60 const kernel_ulong_t len)
62 struct unix_diag_req req = { .sdiag_family = family };
63 const size_t offset = sizeof(req.sdiag_family);
65 tprints("{sdiag_family=");
66 printxval(addrfams, req.sdiag_family, "AF_???");
69 if (len >= sizeof(req)) {
70 if (!umoven_or_printaddr(tcp, addr + offset,
72 (void *) &req + offset)) {
73 tprintf("sdiag_protocol=%" PRIu8 ", udiag_states=",
75 printflags(tcp_state_flags, req.udiag_states,
77 tprintf(", udiag_ino=%" PRIu32 ", udiag_show=",
79 printflags(unix_diag_show, req.udiag_show,
81 tprintf(", udiag_cookie=[%" PRIu32 ", %" PRIu32 "]",
82 req.udiag_cookie[0], req.udiag_cookie[1]);
90 decode_unix_diag_msg(struct tcb *const tcp,
91 const struct nlmsghdr *const nlmsghdr,
93 const kernel_ulong_t addr,
94 const kernel_ulong_t len)
96 struct unix_diag_msg msg = { .udiag_family = family };
97 const size_t offset = sizeof(msg.udiag_family);
99 tprints("{udiag_family=");
100 printxval(addrfams, msg.udiag_family, "AF_???");
103 if (len >= sizeof(msg)) {
104 if (!umoven_or_printaddr(tcp, addr + offset,
105 sizeof(msg) - offset,
106 (void *) &msg + offset)) {
107 tprints("udiag_type=");
108 printxval(socktypes, msg.udiag_type, "SOCK_???");
109 tprintf(", udiag_state=");
110 printxval(tcp_states, msg.udiag_state, "TCP_???");
111 tprintf(", udiag_ino=%" PRIu32
112 ", udiag_cookie=[%" PRIu32 ", %" PRIu32 "]",
114 msg.udiag_cookie[0], msg.udiag_cookie[1]);
121 typedef void (*netlink_diag_decoder_t)(struct tcb *,
122 const struct nlmsghdr *,
127 static const struct {
128 const netlink_diag_decoder_t request, response;
129 } diag_decoders[] = {
130 [AF_UNIX] = { decode_unix_diag_req, decode_unix_diag_msg }
134 decode_netlink_sock_diag(struct tcb *const tcp,
135 const struct nlmsghdr *const nlmsghdr,
136 const kernel_ulong_t addr,
137 const kernel_ulong_t len)
141 if (!umove_or_printaddr(tcp, addr, &family)) {
142 if (family < ARRAY_SIZE(diag_decoders)
143 && len > sizeof(family)) {
144 const netlink_diag_decoder_t decoder =
145 (nlmsghdr->nlmsg_flags & NLM_F_REQUEST)
146 ? diag_decoders[family].request
147 : diag_decoders[family].response;
150 decoder(tcp, nlmsghdr, family, addr, len);
155 decode_family(tcp, family, addr, len);