]>
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> | |
16 | #include <syslog.h> | |
17 | #include <fcntl.h> | |
18 | #include <string.h> | |
19 | #include <sys/time.h> | |
20 | #include <sys/socket.h> | |
21 | #include <netinet/in.h> | |
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 | ||
32 | static void usage(void) __attribute__((noreturn)); | |
33 | ||
34 | static void usage(void) | |
35 | { | |
36 | fprintf(stderr, "Usage: ip netconf show [ dev STRING ]\n"); | |
37 | exit(-1); | |
38 | } | |
39 | ||
95c9d0d3 ZS |
40 | static void print_onoff(FILE *f, const char *flag, __u32 val) |
41 | { | |
42 | fprintf(f, "%s %s ", flag, val ? "on" : "off"); | |
43 | } | |
44 | ||
52a4474d SH |
45 | static struct rtattr *netconf_rta(struct netconfmsg *ncm) |
46 | { | |
47 | return (struct rtattr *)((char *)ncm | |
48 | + NLMSG_ALIGN(sizeof(struct netconfmsg))); | |
49 | } | |
4852ba75 | 50 | |
0628cddd ND |
51 | int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl, |
52 | struct nlmsghdr *n, void *arg) | |
4852ba75 | 53 | { |
56f5daac | 54 | FILE *fp = (FILE *)arg; |
4852ba75 ND |
55 | struct netconfmsg *ncm = NLMSG_DATA(n); |
56 | int len = n->nlmsg_len; | |
57 | struct rtattr *tb[NETCONFA_MAX+1]; | |
58 | ||
59 | if (n->nlmsg_type == NLMSG_ERROR) | |
60 | return -1; | |
61 | if (n->nlmsg_type != RTM_NEWNETCONF) { | |
62 | fprintf(stderr, "Not RTM_NEWNETCONF: %08x %08x %08x\n", | |
63 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
64 | ||
65 | return -1; | |
66 | } | |
67 | len -= NLMSG_SPACE(sizeof(*ncm)); | |
68 | if (len < 0) { | |
69 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
70 | return -1; | |
71 | } | |
72 | ||
73 | if (filter.family && filter.family != ncm->ncm_family) | |
74 | return 0; | |
75 | ||
52a4474d | 76 | parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm), |
4852ba75 ND |
77 | NLMSG_PAYLOAD(n, sizeof(*ncm))); |
78 | ||
79 | switch (ncm->ncm_family) { | |
80 | case AF_INET: | |
81 | fprintf(fp, "ipv4 "); | |
82 | break; | |
83 | case AF_INET6: | |
84 | fprintf(fp, "ipv6 "); | |
85 | break; | |
86 | default: | |
87 | fprintf(fp, "unknown "); | |
88 | break; | |
89 | } | |
90 | ||
91 | if (tb[NETCONFA_IFINDEX]) { | |
95c9d0d3 | 92 | int *ifindex = (int *)rta_getattr_str(tb[NETCONFA_IFINDEX]); |
4852ba75 ND |
93 | |
94 | switch (*ifindex) { | |
95 | case NETCONFA_IFINDEX_ALL: | |
96 | fprintf(fp, "all "); | |
97 | break; | |
98 | case NETCONFA_IFINDEX_DEFAULT: | |
99 | fprintf(fp, "default "); | |
100 | break; | |
101 | default: | |
102 | fprintf(fp, "dev %s ", ll_index_to_name(*ifindex)); | |
103 | break; | |
104 | } | |
105 | } | |
106 | ||
107 | if (tb[NETCONFA_FORWARDING]) | |
95c9d0d3 ZS |
108 | print_onoff(fp, "forwarding", |
109 | rta_getattr_u32(tb[NETCONFA_FORWARDING])); | |
4852ba75 | 110 | if (tb[NETCONFA_RP_FILTER]) { |
95c9d0d3 | 111 | __u32 rp_filter = rta_getattr_u32(tb[NETCONFA_RP_FILTER]); |
4852ba75 ND |
112 | |
113 | if (rp_filter == 0) | |
114 | fprintf(fp, "rp_filter off "); | |
115 | else if (rp_filter == 1) | |
116 | fprintf(fp, "rp_filter strict "); | |
117 | else if (rp_filter == 2) | |
118 | fprintf(fp, "rp_filter loose "); | |
119 | else | |
120 | fprintf(fp, "rp_filter unknown mode "); | |
121 | } | |
122 | if (tb[NETCONFA_MC_FORWARDING]) | |
95c9d0d3 ZS |
123 | print_onoff(fp, "mc_forwarding", |
124 | rta_getattr_u32(tb[NETCONFA_MC_FORWARDING])); | |
4852ba75 | 125 | |
29cc8640 | 126 | if (tb[NETCONFA_PROXY_NEIGH]) |
95c9d0d3 ZS |
127 | print_onoff(fp, "proxy_neigh", |
128 | rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH])); | |
129 | ||
130 | if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]) | |
131 | print_onoff(fp, "ignore_routes_with_linkdown", | |
132 | rta_getattr_u32(tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN])); | |
29cc8640 | 133 | |
4852ba75 ND |
134 | fprintf(fp, "\n"); |
135 | fflush(fp); | |
136 | return 0; | |
137 | } | |
138 | ||
0628cddd ND |
139 | static int print_netconf2(const struct sockaddr_nl *who, |
140 | struct nlmsghdr *n, void *arg) | |
141 | { | |
142 | return print_netconf(who, NULL, n, arg); | |
143 | } | |
144 | ||
093b7646 | 145 | void ipnetconf_reset_filter(int ifindex) |
4852ba75 ND |
146 | { |
147 | memset(&filter, 0, sizeof(filter)); | |
093b7646 | 148 | filter.ifindex = ifindex; |
4852ba75 ND |
149 | } |
150 | ||
d1f28cf1 | 151 | static int do_show(int argc, char **argv) |
4852ba75 ND |
152 | { |
153 | struct { | |
154 | struct nlmsghdr n; | |
155 | struct netconfmsg ncm; | |
156 | char buf[1024]; | |
d17b136f PS |
157 | } req = { |
158 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)), | |
159 | .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, | |
160 | .n.nlmsg_type = RTM_GETNETCONF, | |
161 | }; | |
4852ba75 | 162 | |
093b7646 | 163 | ipnetconf_reset_filter(0); |
4852ba75 ND |
164 | filter.family = preferred_family; |
165 | if (filter.family == AF_UNSPEC) | |
166 | filter.family = AF_INET; | |
4852ba75 ND |
167 | |
168 | while (argc > 0) { | |
169 | if (strcmp(*argv, "dev") == 0) { | |
170 | NEXT_ARG(); | |
171 | filter.ifindex = ll_name_to_index(*argv); | |
172 | if (filter.ifindex <= 0) { | |
173 | fprintf(stderr, "Device \"%s\" does not exist.\n", | |
174 | *argv); | |
175 | return -1; | |
176 | } | |
177 | } | |
178 | argv++; argc--; | |
179 | } | |
180 | ||
181 | ll_init_map(&rth); | |
f7431e29 | 182 | if (filter.ifindex) { |
f7431e29 ND |
183 | req.ncm.ncm_family = filter.family; |
184 | if (filter.ifindex) | |
185 | addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX, | |
186 | &filter.ifindex, sizeof(filter.ifindex)); | |
187 | ||
d2468da0 SH |
188 | if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) { |
189 | perror("Can not send request"); | |
190 | exit(1); | |
191 | } | |
f7431e29 ND |
192 | rtnl_listen(&rth, print_netconf, stdout); |
193 | } else { | |
194 | dump: | |
195 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNETCONF) < 0) { | |
196 | perror("Cannot send dump request"); | |
197 | exit(1); | |
198 | } | |
0628cddd | 199 | if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) { |
f7431e29 ND |
200 | fprintf(stderr, "Dump terminated\n"); |
201 | exit(1); | |
202 | } | |
203 | if (preferred_family == AF_UNSPEC) { | |
204 | preferred_family = AF_INET6; | |
205 | filter.family = AF_INET6; | |
206 | goto dump; | |
207 | } | |
208 | } | |
4852ba75 ND |
209 | return 0; |
210 | } | |
211 | ||
212 | int do_ipnetconf(int argc, char **argv) | |
213 | { | |
214 | if (argc > 0) { | |
215 | if (matches(*argv, "show") == 0 || | |
216 | matches(*argv, "lst") == 0 || | |
217 | matches(*argv, "list") == 0) | |
218 | return do_show(argc-1, argv+1); | |
219 | if (matches(*argv, "help") == 0) | |
220 | usage(); | |
221 | } else | |
222 | return do_show(0, NULL); | |
223 | ||
224 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv); | |
225 | exit(-1); | |
226 | } |