]>
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 | ||
52a4474d SH |
44 | static struct rtattr *netconf_rta(struct netconfmsg *ncm) |
45 | { | |
46 | return (struct rtattr *)((char *)ncm | |
47 | + NLMSG_ALIGN(sizeof(struct netconfmsg))); | |
48 | } | |
4852ba75 | 49 | |
cd554f2c | 50 | int print_netconf(struct rtnl_ctrl_data *ctrl, struct nlmsghdr *n, void *arg) |
4852ba75 | 51 | { |
56f5daac | 52 | FILE *fp = (FILE *)arg; |
4852ba75 ND |
53 | struct netconfmsg *ncm = NLMSG_DATA(n); |
54 | int len = n->nlmsg_len; | |
55 | struct rtattr *tb[NETCONFA_MAX+1]; | |
c6858ef4 | 56 | int ifindex = 0; |
4852ba75 ND |
57 | |
58 | if (n->nlmsg_type == NLMSG_ERROR) | |
59 | return -1; | |
e09d21f6 SH |
60 | |
61 | if (n->nlmsg_type != RTM_NEWNETCONF && | |
62 | n->nlmsg_type != RTM_DELNETCONF) { | |
63 | fprintf(stderr, "Not a netconf message: %08x %08x %08x\n", | |
4852ba75 ND |
64 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
65 | ||
66 | return -1; | |
67 | } | |
68 | len -= NLMSG_SPACE(sizeof(*ncm)); | |
69 | if (len < 0) { | |
70 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
71 | return -1; | |
72 | } | |
73 | ||
74 | if (filter.family && filter.family != ncm->ncm_family) | |
75 | return 0; | |
76 | ||
52a4474d | 77 | parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm), |
4852ba75 ND |
78 | NLMSG_PAYLOAD(n, sizeof(*ncm))); |
79 | ||
c6858ef4 DA |
80 | if (tb[NETCONFA_IFINDEX]) |
81 | ifindex = rta_getattr_u32(tb[NETCONFA_IFINDEX]); | |
82 | ||
83 | if (filter.ifindex && filter.ifindex != ifindex) | |
84 | return 0; | |
85 | ||
96032aaf | 86 | open_json_object(NULL); |
e09d21f6 SH |
87 | if (n->nlmsg_type == RTM_DELNETCONF) |
88 | print_bool(PRINT_ANY, "deleted", "Deleted ", true); | |
89 | ||
96032aaf SH |
90 | print_string(PRINT_ANY, "family", |
91 | "%s ", family_name(ncm->ncm_family)); | |
4852ba75 ND |
92 | |
93 | if (tb[NETCONFA_IFINDEX]) { | |
96032aaf SH |
94 | const char *dev; |
95 | ||
c6858ef4 | 96 | switch (ifindex) { |
4852ba75 | 97 | case NETCONFA_IFINDEX_ALL: |
96032aaf | 98 | dev = "all"; |
4852ba75 ND |
99 | break; |
100 | case NETCONFA_IFINDEX_DEFAULT: | |
96032aaf | 101 | dev = "default"; |
4852ba75 ND |
102 | break; |
103 | default: | |
96032aaf | 104 | dev = ll_index_to_name(ifindex); |
4852ba75 ND |
105 | break; |
106 | } | |
96032aaf SH |
107 | print_color_string(PRINT_ANY, COLOR_IFNAME, |
108 | "interface", "%s ", dev); | |
4852ba75 ND |
109 | } |
110 | ||
111 | if (tb[NETCONFA_FORWARDING]) | |
66e574c4 PM |
112 | print_on_off(PRINT_ANY, "forwarding", "forwarding %s ", |
113 | rta_getattr_u32(tb[NETCONFA_FORWARDING])); | |
96032aaf | 114 | |
4852ba75 | 115 | if (tb[NETCONFA_RP_FILTER]) { |
95c9d0d3 | 116 | __u32 rp_filter = rta_getattr_u32(tb[NETCONFA_RP_FILTER]); |
4852ba75 | 117 | |
96032aaf SH |
118 | if (rp_filter < ARRAY_SIZE(rp_filter_names)) |
119 | print_string(PRINT_ANY, "rp_filter", | |
120 | "rp_filter %s ", | |
121 | rp_filter_names[rp_filter]); | |
4852ba75 | 122 | else |
96032aaf SH |
123 | print_uint(PRINT_ANY, "rp_filter", |
124 | "rp_filter %u ", rp_filter); | |
4852ba75 | 125 | } |
96032aaf | 126 | |
4852ba75 | 127 | if (tb[NETCONFA_MC_FORWARDING]) |
66e574c4 PM |
128 | print_on_off(PRINT_ANY, "mc_forwarding", "mc_forwarding %s ", |
129 | rta_getattr_u32(tb[NETCONFA_MC_FORWARDING])); | |
4852ba75 | 130 | |
29cc8640 | 131 | if (tb[NETCONFA_PROXY_NEIGH]) |
66e574c4 PM |
132 | print_on_off(PRINT_ANY, "proxy_neigh", "proxy_neigh %s ", |
133 | rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH])); | |
95c9d0d3 ZS |
134 | |
135 | if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]) | |
66e574c4 PM |
136 | print_on_off(PRINT_ANY, "ignore_routes_with_linkdown", |
137 | "ignore_routes_with_linkdown %s ", | |
138 | rta_getattr_u32(tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN])); | |
29cc8640 | 139 | |
76f7d89d | 140 | if (tb[NETCONFA_INPUT]) |
66e574c4 PM |
141 | print_on_off(PRINT_ANY, "input", "input %s ", |
142 | rta_getattr_u32(tb[NETCONFA_INPUT])); | |
76f7d89d | 143 | |
96032aaf SH |
144 | close_json_object(); |
145 | print_string(PRINT_FP, NULL, "\n", NULL); | |
4852ba75 ND |
146 | fflush(fp); |
147 | return 0; | |
148 | } | |
149 | ||
cd554f2c | 150 | static int print_netconf2(struct nlmsghdr *n, void *arg) |
0628cddd | 151 | { |
cd554f2c | 152 | return print_netconf(NULL, n, arg); |
0628cddd ND |
153 | } |
154 | ||
093b7646 | 155 | void ipnetconf_reset_filter(int ifindex) |
4852ba75 ND |
156 | { |
157 | memset(&filter, 0, sizeof(filter)); | |
093b7646 | 158 | filter.ifindex = ifindex; |
4852ba75 ND |
159 | } |
160 | ||
d1f28cf1 | 161 | static int do_show(int argc, char **argv) |
4852ba75 ND |
162 | { |
163 | struct { | |
164 | struct nlmsghdr n; | |
165 | struct netconfmsg ncm; | |
166 | char buf[1024]; | |
d17b136f PS |
167 | } req = { |
168 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)), | |
169 | .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, | |
170 | .n.nlmsg_type = RTM_GETNETCONF, | |
171 | }; | |
4852ba75 | 172 | |
093b7646 | 173 | ipnetconf_reset_filter(0); |
4852ba75 | 174 | filter.family = preferred_family; |
4852ba75 ND |
175 | |
176 | while (argc > 0) { | |
177 | if (strcmp(*argv, "dev") == 0) { | |
178 | NEXT_ARG(); | |
179 | filter.ifindex = ll_name_to_index(*argv); | |
180 | if (filter.ifindex <= 0) { | |
96032aaf SH |
181 | fprintf(stderr, |
182 | "Device \"%s\" does not exist.\n", | |
4852ba75 ND |
183 | *argv); |
184 | return -1; | |
185 | } | |
186 | } | |
187 | argv++; argc--; | |
188 | } | |
189 | ||
190 | ll_init_map(&rth); | |
c6858ef4 DA |
191 | |
192 | if (filter.ifindex && filter.family != AF_UNSPEC) { | |
f7431e29 | 193 | req.ncm.ncm_family = filter.family; |
c6858ef4 DA |
194 | addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX, |
195 | &filter.ifindex, sizeof(filter.ifindex)); | |
f7431e29 | 196 | |
d2468da0 SH |
197 | if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) { |
198 | perror("Can not send request"); | |
199 | exit(1); | |
200 | } | |
f7431e29 ND |
201 | rtnl_listen(&rth, print_netconf, stdout); |
202 | } else { | |
f052f5df | 203 | rth.flags = RTNL_HANDLE_F_SUPPRESS_NLERR; |
f7431e29 | 204 | dump: |
ddee16bc | 205 | if (rtnl_netconfdump_req(&rth, filter.family) < 0) { |
f7431e29 ND |
206 | perror("Cannot send dump request"); |
207 | exit(1); | |
208 | } | |
96032aaf SH |
209 | |
210 | new_json_obj(json); | |
0628cddd | 211 | if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) { |
f052f5df DA |
212 | /* kernel does not support netconf dump on AF_UNSPEC; |
213 | * fall back to requesting by family | |
214 | */ | |
215 | if (errno == EOPNOTSUPP && | |
216 | filter.family == AF_UNSPEC) { | |
217 | filter.family = AF_INET; | |
218 | goto dump; | |
219 | } | |
220 | perror("RTNETLINK answers"); | |
f7431e29 ND |
221 | fprintf(stderr, "Dump terminated\n"); |
222 | exit(1); | |
223 | } | |
96032aaf | 224 | delete_json_obj(); |
f052f5df | 225 | if (preferred_family == AF_UNSPEC && filter.family == AF_INET) { |
f7431e29 ND |
226 | preferred_family = AF_INET6; |
227 | filter.family = AF_INET6; | |
228 | goto dump; | |
229 | } | |
230 | } | |
4852ba75 ND |
231 | return 0; |
232 | } | |
233 | ||
234 | int do_ipnetconf(int argc, char **argv) | |
235 | { | |
236 | if (argc > 0) { | |
237 | if (matches(*argv, "show") == 0 || | |
238 | matches(*argv, "lst") == 0 || | |
239 | matches(*argv, "list") == 0) | |
240 | return do_show(argc-1, argv+1); | |
241 | if (matches(*argv, "help") == 0) | |
242 | usage(); | |
243 | } else | |
244 | return do_show(0, NULL); | |
245 | ||
96032aaf SH |
246 | fprintf(stderr, |
247 | "Command \"%s\" is unknown, try \"ip netconf help\".\n", | |
248 | *argv); | |
4852ba75 ND |
249 | exit(-1); |
250 | } |