]> granicus.if.org Git - strace/blob - netlink_route.c
nlattr: add UID/GID netlink attribute decoders
[strace] / netlink_route.c
1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4  * Copyright (c) 2016-2017 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 "netlink.h"
32 #include "netlink_route.h"
33
34 #include <linux/rtnetlink.h>
35
36 #include "xlat/nl_route_types.h"
37
38 static void
39 decode_family(struct tcb *const tcp, const uint8_t family,
40               const kernel_ulong_t addr, const unsigned int len)
41 {
42         tprints("{family=");
43         printxval(addrfams, family, "AF_???");
44         if (len > sizeof(family)) {
45                 tprints(", ");
46                 printstr_ex(tcp, addr + sizeof(family),
47                             len - sizeof(family), QUOTE_FORCE_HEX);
48         }
49         tprints("}");
50 }
51
52 typedef DECL_NETLINK_ROUTE_DECODER((*netlink_route_decoder_t));
53
54 static const netlink_route_decoder_t route_decoders[] = {
55         [RTM_DELLINK - RTM_BASE] = decode_ifinfomsg,
56         [RTM_GETLINK - RTM_BASE] = decode_ifinfomsg,
57         [RTM_NEWLINK - RTM_BASE] = decode_ifinfomsg,
58         [RTM_SETLINK - RTM_BASE] = decode_ifinfomsg,
59
60         [RTM_DELADDR - RTM_BASE] = decode_ifaddrmsg,
61         [RTM_GETADDR - RTM_BASE] = decode_ifaddrmsg,
62         [RTM_GETANYCAST - RTM_BASE] = decode_ifaddrmsg,
63         [RTM_GETMULTICAST - RTM_BASE] = decode_ifaddrmsg,
64         [RTM_NEWADDR - RTM_BASE] = decode_ifaddrmsg,
65
66         [RTM_DELROUTE - RTM_BASE] = decode_rtmsg,
67         [RTM_GETROUTE - RTM_BASE] = decode_rtmsg,
68         [RTM_NEWROUTE - RTM_BASE] = decode_rtmsg,
69
70         [RTM_DELRULE - RTM_BASE] = decode_fib_rule_hdr,
71         [RTM_GETRULE - RTM_BASE] = decode_fib_rule_hdr,
72         [RTM_NEWRULE - RTM_BASE] = decode_fib_rule_hdr,
73
74         [RTM_DELNEIGH - RTM_BASE] = decode_ndmsg,
75         [RTM_GETNEIGH - RTM_BASE] = decode_rtm_getneigh,
76         [RTM_NEWNEIGH - RTM_BASE] = decode_ndmsg,
77
78         [RTM_GETNEIGHTBL - RTM_BASE] = decode_ndtmsg,
79         [RTM_NEWNEIGHTBL - RTM_BASE] = decode_ndtmsg,
80         [RTM_SETNEIGHTBL - RTM_BASE] = decode_ndtmsg,
81
82         [RTM_DELQDISC - RTM_BASE] = decode_tcmsg,
83         [RTM_GETQDISC - RTM_BASE] = decode_tcmsg,
84         [RTM_NEWQDISC - RTM_BASE] = decode_tcmsg,
85         [RTM_DELTCLASS - RTM_BASE] = decode_tcmsg,
86         [RTM_GETTCLASS - RTM_BASE] = decode_tcmsg,
87         [RTM_NEWTCLASS - RTM_BASE] = decode_tcmsg,
88         [RTM_DELTFILTER - RTM_BASE] = decode_tcmsg,
89         [RTM_GETTFILTER - RTM_BASE] = decode_tcmsg,
90         [RTM_NEWTFILTER - RTM_BASE] = decode_tcmsg,
91
92         [RTM_DELACTION - RTM_BASE] = decode_tcamsg,
93         [RTM_GETACTION - RTM_BASE] = decode_tcamsg,
94         [RTM_NEWACTION - RTM_BASE] = decode_tcamsg,
95
96 #ifdef HAVE_STRUCT_IFADDRLBLMSG
97         [RTM_DELADDRLABEL - RTM_BASE] = decode_ifaddrlblmsg,
98         [RTM_GETADDRLABEL - RTM_BASE] = decode_ifaddrlblmsg,
99         [RTM_NEWADDRLABEL - RTM_BASE] = decode_ifaddrlblmsg,
100 #endif
101
102 #ifdef HAVE_STRUCT_DCBMSG
103         [RTM_GETDCB - RTM_BASE] = decode_dcbmsg,
104         [RTM_SETDCB - RTM_BASE] = decode_dcbmsg,
105 #endif
106
107 #ifdef HAVE_STRUCT_NETCONFMSG
108         [RTM_DELNETCONF - RTM_BASE] = decode_netconfmsg,
109         [RTM_GETNETCONF - RTM_BASE] = decode_netconfmsg,
110         [RTM_NEWNETCONF - RTM_BASE] = decode_netconfmsg,
111 #endif
112
113 #ifdef HAVE_STRUCT_BR_PORT_MSG
114         [RTM_DELMDB - RTM_BASE] = decode_br_port_msg,
115         [RTM_GETMDB - RTM_BASE] = decode_br_port_msg,
116         [RTM_NEWMDB - RTM_BASE] = decode_br_port_msg,
117 #endif
118
119         [RTM_DELNSID - RTM_BASE] = decode_rtgenmsg,
120         [RTM_GETNSID - RTM_BASE] = decode_rtgenmsg,
121         [RTM_NEWNSID - RTM_BASE] = decode_rtgenmsg
122 };
123
124 bool
125 decode_netlink_route(struct tcb *const tcp,
126                      const struct nlmsghdr *const nlmsghdr,
127                      const kernel_ulong_t addr,
128                      const unsigned int len)
129 {
130         uint8_t family;
131
132         if (nlmsghdr->nlmsg_type == NLMSG_DONE)
133                 return false;
134
135         if (!umove_or_printaddr(tcp, addr, &family)) {
136                 const unsigned int index = nlmsghdr->nlmsg_type - RTM_BASE;
137
138                 if (index < ARRAY_SIZE(route_decoders)
139                     && route_decoders[index]) {
140                         route_decoders[index](tcp, nlmsghdr, family, addr, len);
141                 } else {
142                         decode_family(tcp, family, addr, len);
143                 }
144         }
145
146         return true;
147 }