]>
Commit | Line | Data |
---|---|---|
4852ba75 ND |
1 | /* |
2 | * ipnetconf.c "ip netconf". | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Nicolas Dichtel, <nicolas.dichtel@6wind.com> | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
4852ba75 ND |
16 | #include <fcntl.h> |
17 | #include <string.h> | |
18 | #include <sys/time.h> | |
19 | #include <sys/socket.h> | |
20 | #include <netinet/in.h> | |
f052f5df | 21 | #include <errno.h> |
4852ba75 ND |
22 | |
23 | #include "rt_names.h" | |
24 | #include "utils.h" | |
25 | #include "ip_common.h" | |
26 | ||
52a4474d | 27 | static struct { |
4852ba75 ND |
28 | int family; |
29 | int ifindex; | |
30 | } filter; | |
31 | ||
96032aaf SH |
32 | static const char * const rp_filter_names[] = { |
33 | "off", "strict", "loose" | |
34 | }; | |
35 | ||
4852ba75 ND |
36 | static void usage(void) __attribute__((noreturn)); |
37 | ||
38 | static void usage(void) | |
39 | { | |
40 | fprintf(stderr, "Usage: ip netconf show [ dev STRING ]\n"); | |
41 | exit(-1); | |
42 | } | |
43 | ||
96032aaf | 44 | static void print_onoff(FILE *fp, const char *flag, __u32 val) |
95c9d0d3 | 45 | { |
96032aaf SH |
46 | if (is_json_context()) |
47 | print_bool(PRINT_JSON, flag, NULL, val); | |
48 | else | |
49 | fprintf(fp, "%s %s ", flag, val ? "on" : "off"); | |
95c9d0d3 ZS |
50 | } |
51 | ||
52a4474d SH |
52 | static struct rtattr *netconf_rta(struct netconfmsg *ncm) |
53 | { | |
54 | return (struct rtattr *)((char *)ncm | |
55 | + NLMSG_ALIGN(sizeof(struct netconfmsg))); | |
56 | } | |
4852ba75 | 57 | |
0628cddd ND |
58 | int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl, |
59 | struct nlmsghdr *n, void *arg) | |
4852ba75 | 60 | { |
56f5daac | 61 | FILE *fp = (FILE *)arg; |
4852ba75 ND |
62 | struct netconfmsg *ncm = NLMSG_DATA(n); |
63 | int len = n->nlmsg_len; | |
64 | struct rtattr *tb[NETCONFA_MAX+1]; | |
c6858ef4 | 65 | int ifindex = 0; |
4852ba75 ND |
66 | |
67 | if (n->nlmsg_type == NLMSG_ERROR) | |
68 | return -1; | |
e09d21f6 SH |
69 | |
70 | if (n->nlmsg_type != RTM_NEWNETCONF && | |
71 | n->nlmsg_type != RTM_DELNETCONF) { | |
72 | fprintf(stderr, "Not a netconf message: %08x %08x %08x\n", | |
4852ba75 ND |
73 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
74 | ||
75 | return -1; | |
76 | } | |
77 | len -= NLMSG_SPACE(sizeof(*ncm)); | |
78 | if (len < 0) { | |
79 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
80 | return -1; | |
81 | } | |
82 | ||
83 | if (filter.family && filter.family != ncm->ncm_family) | |
84 | return 0; | |
85 | ||
52a4474d | 86 | parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm), |
4852ba75 ND |
87 | NLMSG_PAYLOAD(n, sizeof(*ncm))); |
88 | ||
c6858ef4 DA |
89 | if (tb[NETCONFA_IFINDEX]) |
90 | ifindex = rta_getattr_u32(tb[NETCONFA_IFINDEX]); | |
91 | ||
92 | if (filter.ifindex && filter.ifindex != ifindex) | |
93 | return 0; | |
94 | ||
96032aaf | 95 | open_json_object(NULL); |
e09d21f6 SH |
96 | if (n->nlmsg_type == RTM_DELNETCONF) |
97 | print_bool(PRINT_ANY, "deleted", "Deleted ", true); | |
98 | ||
96032aaf SH |
99 | print_string(PRINT_ANY, "family", |
100 | "%s ", family_name(ncm->ncm_family)); | |
4852ba75 ND |
101 | |
102 | if (tb[NETCONFA_IFINDEX]) { | |
96032aaf SH |
103 | const char *dev; |
104 | ||
c6858ef4 | 105 | switch (ifindex) { |
4852ba75 | 106 | case NETCONFA_IFINDEX_ALL: |
96032aaf | 107 | dev = "all"; |
4852ba75 ND |
108 | break; |
109 | case NETCONFA_IFINDEX_DEFAULT: | |
96032aaf | 110 | dev = "default"; |
4852ba75 ND |
111 | break; |
112 | default: | |
96032aaf | 113 | dev = ll_index_to_name(ifindex); |
4852ba75 ND |
114 | break; |
115 | } | |
96032aaf SH |
116 | print_color_string(PRINT_ANY, COLOR_IFNAME, |
117 | "interface", "%s ", dev); | |
4852ba75 ND |
118 | } |
119 | ||
120 | if (tb[NETCONFA_FORWARDING]) | |
95c9d0d3 ZS |
121 | print_onoff(fp, "forwarding", |
122 | rta_getattr_u32(tb[NETCONFA_FORWARDING])); | |
96032aaf | 123 | |
4852ba75 | 124 | if (tb[NETCONFA_RP_FILTER]) { |
95c9d0d3 | 125 | __u32 rp_filter = rta_getattr_u32(tb[NETCONFA_RP_FILTER]); |
4852ba75 | 126 | |
96032aaf SH |
127 | if (rp_filter < ARRAY_SIZE(rp_filter_names)) |
128 | print_string(PRINT_ANY, "rp_filter", | |
129 | "rp_filter %s ", | |
130 | rp_filter_names[rp_filter]); | |
4852ba75 | 131 | else |
96032aaf SH |
132 | print_uint(PRINT_ANY, "rp_filter", |
133 | "rp_filter %u ", rp_filter); | |
4852ba75 | 134 | } |
96032aaf | 135 | |
4852ba75 | 136 | if (tb[NETCONFA_MC_FORWARDING]) |
95c9d0d3 ZS |
137 | print_onoff(fp, "mc_forwarding", |
138 | rta_getattr_u32(tb[NETCONFA_MC_FORWARDING])); | |
4852ba75 | 139 | |
29cc8640 | 140 | if (tb[NETCONFA_PROXY_NEIGH]) |
95c9d0d3 ZS |
141 | print_onoff(fp, "proxy_neigh", |
142 | rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH])); | |
143 | ||
144 | if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]) | |
145 | print_onoff(fp, "ignore_routes_with_linkdown", | |
146 | rta_getattr_u32(tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN])); | |
29cc8640 | 147 | |
76f7d89d DA |
148 | if (tb[NETCONFA_INPUT]) |
149 | print_onoff(fp, "input", rta_getattr_u32(tb[NETCONFA_INPUT])); | |
150 | ||
96032aaf SH |
151 | close_json_object(); |
152 | print_string(PRINT_FP, NULL, "\n", NULL); | |
4852ba75 ND |
153 | fflush(fp); |
154 | return 0; | |
155 | } | |
156 | ||
0628cddd ND |
157 | static int print_netconf2(const struct sockaddr_nl *who, |
158 | struct nlmsghdr *n, void *arg) | |
159 | { | |
160 | return print_netconf(who, NULL, n, arg); | |
161 | } | |
162 | ||
093b7646 | 163 | void ipnetconf_reset_filter(int ifindex) |
4852ba75 ND |
164 | { |
165 | memset(&filter, 0, sizeof(filter)); | |
093b7646 | 166 | filter.ifindex = ifindex; |
4852ba75 ND |
167 | } |
168 | ||
d1f28cf1 | 169 | static int do_show(int argc, char **argv) |
4852ba75 ND |
170 | { |
171 | struct { | |
172 | struct nlmsghdr n; | |
173 | struct netconfmsg ncm; | |
174 | char buf[1024]; | |
d17b136f PS |
175 | } req = { |
176 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)), | |
177 | .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, | |
178 | .n.nlmsg_type = RTM_GETNETCONF, | |
179 | }; | |
4852ba75 | 180 | |
093b7646 | 181 | ipnetconf_reset_filter(0); |
4852ba75 | 182 | filter.family = preferred_family; |
4852ba75 ND |
183 | |
184 | while (argc > 0) { | |
185 | if (strcmp(*argv, "dev") == 0) { | |
186 | NEXT_ARG(); | |
187 | filter.ifindex = ll_name_to_index(*argv); | |
188 | if (filter.ifindex <= 0) { | |
96032aaf SH |
189 | fprintf(stderr, |
190 | "Device \"%s\" does not exist.\n", | |
4852ba75 ND |
191 | *argv); |
192 | return -1; | |
193 | } | |
194 | } | |
195 | argv++; argc--; | |
196 | } | |
197 | ||
198 | ll_init_map(&rth); | |
c6858ef4 DA |
199 | |
200 | if (filter.ifindex && filter.family != AF_UNSPEC) { | |
f7431e29 | 201 | req.ncm.ncm_family = filter.family; |
c6858ef4 DA |
202 | addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX, |
203 | &filter.ifindex, sizeof(filter.ifindex)); | |
f7431e29 | 204 | |
d2468da0 SH |
205 | if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) { |
206 | perror("Can not send request"); | |
207 | exit(1); | |
208 | } | |
f7431e29 ND |
209 | rtnl_listen(&rth, print_netconf, stdout); |
210 | } else { | |
f052f5df | 211 | rth.flags = RTNL_HANDLE_F_SUPPRESS_NLERR; |
f7431e29 | 212 | dump: |
ddee16bc | 213 | if (rtnl_netconfdump_req(&rth, filter.family) < 0) { |
f7431e29 ND |
214 | perror("Cannot send dump request"); |
215 | exit(1); | |
216 | } | |
96032aaf SH |
217 | |
218 | new_json_obj(json); | |
0628cddd | 219 | if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) { |
f052f5df DA |
220 | /* kernel does not support netconf dump on AF_UNSPEC; |
221 | * fall back to requesting by family | |
222 | */ | |
223 | if (errno == EOPNOTSUPP && | |
224 | filter.family == AF_UNSPEC) { | |
225 | filter.family = AF_INET; | |
226 | goto dump; | |
227 | } | |
228 | perror("RTNETLINK answers"); | |
f7431e29 ND |
229 | fprintf(stderr, "Dump terminated\n"); |
230 | exit(1); | |
231 | } | |
96032aaf | 232 | delete_json_obj(); |
f052f5df | 233 | if (preferred_family == AF_UNSPEC && filter.family == AF_INET) { |
f7431e29 ND |
234 | preferred_family = AF_INET6; |
235 | filter.family = AF_INET6; | |
236 | goto dump; | |
237 | } | |
238 | } | |
4852ba75 ND |
239 | return 0; |
240 | } | |
241 | ||
242 | int do_ipnetconf(int argc, char **argv) | |
243 | { | |
244 | if (argc > 0) { | |
245 | if (matches(*argv, "show") == 0 || | |
246 | matches(*argv, "lst") == 0 || | |
247 | matches(*argv, "list") == 0) | |
248 | return do_show(argc-1, argv+1); | |
249 | if (matches(*argv, "help") == 0) | |
250 | usage(); | |
251 | } else | |
252 | return do_show(0, NULL); | |
253 | ||
96032aaf SH |
254 | fprintf(stderr, |
255 | "Command \"%s\" is unknown, try \"ip netconf help\".\n", | |
256 | *argv); | |
4852ba75 ND |
257 | exit(-1); |
258 | } |