]>
Commit | Line | Data |
---|---|---|
d04bc300 SH |
1 | |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | #include <unistd.h> | |
5 | #include <time.h> | |
6 | #include <sys/socket.h> | |
7 | #include <sys/time.h> | |
8 | #include <netinet/in.h> | |
9 | #include <linux/if.h> | |
10 | #include <linux/if_bridge.h> | |
11 | #include <string.h> | |
64108901 | 12 | #include <stdbool.h> |
d04bc300 | 13 | |
64108901 | 14 | #include "libnetlink.h" |
d04bc300 SH |
15 | #include "utils.h" |
16 | #include "br_common.h" | |
17 | ||
64108901 VY |
18 | unsigned int filter_index; |
19 | ||
d04bc300 SH |
20 | static const char *port_states[] = { |
21 | [BR_STATE_DISABLED] = "disabled", | |
22 | [BR_STATE_LISTENING] = "listening", | |
23 | [BR_STATE_LEARNING] = "learning", | |
24 | [BR_STATE_FORWARDING] = "forwarding", | |
25 | [BR_STATE_BLOCKING] = "blocking", | |
26 | }; | |
27 | ||
28 | extern char *if_indextoname (unsigned int __ifindex, char *__ifname); | |
29 | ||
30 | static void print_link_flags(FILE *fp, unsigned flags) | |
31 | { | |
32 | fprintf(fp, "<"); | |
33 | if (flags & IFF_UP && !(flags & IFF_RUNNING)) | |
34 | fprintf(fp, "NO-CARRIER%s", flags ? "," : ""); | |
35 | flags &= ~IFF_RUNNING; | |
36 | #define _PF(f) if (flags&IFF_##f) { \ | |
37 | flags &= ~IFF_##f ; \ | |
38 | fprintf(fp, #f "%s", flags ? "," : ""); } | |
39 | _PF(LOOPBACK); | |
40 | _PF(BROADCAST); | |
41 | _PF(POINTOPOINT); | |
42 | _PF(MULTICAST); | |
43 | _PF(NOARP); | |
44 | _PF(ALLMULTI); | |
45 | _PF(PROMISC); | |
46 | _PF(MASTER); | |
47 | _PF(SLAVE); | |
48 | _PF(DEBUG); | |
49 | _PF(DYNAMIC); | |
50 | _PF(AUTOMEDIA); | |
51 | _PF(PORTSEL); | |
52 | _PF(NOTRAILERS); | |
53 | _PF(UP); | |
54 | _PF(LOWER_UP); | |
55 | _PF(DORMANT); | |
56 | _PF(ECHO); | |
57 | #undef _PF | |
58 | if (flags) | |
59 | fprintf(fp, "%x", flags); | |
60 | fprintf(fp, "> "); | |
61 | } | |
62 | ||
63 | static const char *oper_states[] = { | |
38df7ac9 | 64 | "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN", |
d04bc300 SH |
65 | "TESTING", "DORMANT", "UP" |
66 | }; | |
67 | ||
68 | static void print_operstate(FILE *f, __u8 state) | |
69 | { | |
70 | if (state >= sizeof(oper_states)/sizeof(oper_states[0])) | |
71 | fprintf(f, "state %#x ", state); | |
72 | else | |
73 | fprintf(f, "state %s ", oper_states[state]); | |
74 | } | |
75 | ||
76 | int print_linkinfo(const struct sockaddr_nl *who, | |
77 | struct nlmsghdr *n, void *arg) | |
78 | { | |
79 | FILE *fp = arg; | |
80 | int len = n->nlmsg_len; | |
81 | struct ifinfomsg *ifi = NLMSG_DATA(n); | |
82 | struct rtattr * tb[IFLA_MAX+1]; | |
83 | char b1[IFNAMSIZ]; | |
84 | ||
85 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
86 | if (len < 0) { | |
87 | fprintf(stderr, "Message too short!\n"); | |
88 | return -1; | |
89 | } | |
90 | ||
91 | if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC)) | |
92 | return 0; | |
93 | ||
64108901 VY |
94 | if (filter_index && filter_index != ifi->ifi_index) |
95 | return 0; | |
96 | ||
d04bc300 SH |
97 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
98 | ||
99 | if (tb[IFLA_IFNAME] == NULL) { | |
100 | fprintf(stderr, "BUG: nil ifname\n"); | |
101 | return -1; | |
102 | } | |
103 | ||
104 | if (n->nlmsg_type == RTM_DELLINK) | |
105 | fprintf(fp, "Deleted "); | |
106 | ||
107 | fprintf(fp, "%d: %s ", ifi->ifi_index, | |
1465db1a | 108 | tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>"); |
d04bc300 | 109 | |
38df7ac9 | 110 | if (tb[IFLA_OPERSTATE]) |
1465db1a | 111 | print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); |
38df7ac9 | 112 | |
d04bc300 SH |
113 | if (tb[IFLA_LINK]) { |
114 | SPRINT_BUF(b1); | |
1465db1a | 115 | int iflink = rta_getattr_u32(tb[IFLA_LINK]); |
d04bc300 SH |
116 | if (iflink == 0) |
117 | fprintf(fp, "@NONE: "); | |
1465db1a | 118 | else |
38df7ac9 | 119 | fprintf(fp, "@%s: ", |
d04bc300 | 120 | if_indextoname(iflink, b1)); |
1465db1a | 121 | } else |
d04bc300 | 122 | fprintf(fp, ": "); |
d04bc300 SH |
123 | |
124 | print_link_flags(fp, ifi->ifi_flags); | |
125 | ||
126 | if (tb[IFLA_MTU]) | |
1465db1a | 127 | fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU])); |
d04bc300 | 128 | |
1465db1a | 129 | if (tb[IFLA_MASTER]) |
38df7ac9 | 130 | fprintf(fp, "master %s ", |
1465db1a | 131 | if_indextoname(rta_getattr_u32(tb[IFLA_MASTER]), b1)); |
d04bc300 SH |
132 | |
133 | if (tb[IFLA_PROTINFO]) { | |
1465db1a | 134 | __u8 state = rta_getattr_u8(tb[IFLA_PROTINFO]); |
d04bc300 SH |
135 | if (state <= BR_STATE_BLOCKING) |
136 | fprintf(fp, "state %s", port_states[state]); | |
137 | else | |
138 | fprintf(fp, "state (%d)", state); | |
139 | } | |
140 | ||
141 | ||
142 | fprintf(fp, "\n"); | |
143 | fflush(fp); | |
144 | return 0; | |
145 | } | |
64108901 VY |
146 | |
147 | static void usage(void) | |
148 | { | |
149 | fprintf(stderr, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n"); | |
150 | fprintf(stderr, " [ guard {on | off} ]\n"); | |
151 | fprintf(stderr, " [ hairpin {on | off} ] \n"); | |
152 | fprintf(stderr, " [ fastleave {on | off} ]\n"); | |
153 | fprintf(stderr, " [ root_block {on | off} ]\n"); | |
154 | fprintf(stderr, " [ hwmode {vepa | veb} ]\n"); | |
155 | fprintf(stderr, " bridge link show [dev DEV]\n"); | |
156 | exit(-1); | |
157 | } | |
158 | ||
159 | static bool on_off(char *arg, __s8 *attr, char *val) | |
160 | { | |
161 | if (strcmp(val, "on") == 0) | |
162 | *attr = 1; | |
163 | else if (strcmp(val, "off") == 0) | |
164 | *attr = 0; | |
165 | else { | |
166 | fprintf(stderr, | |
167 | "Error: argument of \"%s\" must be \"on\" or \"off\"\n", | |
168 | arg); | |
169 | return false; | |
170 | } | |
171 | ||
172 | return true; | |
173 | } | |
174 | ||
175 | static int brlink_modify(int argc, char **argv) | |
176 | { | |
177 | struct { | |
178 | struct nlmsghdr n; | |
179 | struct ifinfomsg ifm; | |
180 | char buf[512]; | |
181 | } req; | |
182 | char *d = NULL; | |
183 | __s8 hairpin = -1; | |
184 | __s8 bpdu_guard = -1; | |
185 | __s8 fast_leave = -1; | |
186 | __u32 cost = 0; | |
187 | __s16 priority = -1; | |
188 | __s8 state = -1; | |
189 | __s16 mode = -1; | |
190 | __u16 flags = BRIDGE_FLAGS_MASTER; | |
191 | struct rtattr *nest; | |
192 | ||
193 | memset(&req, 0, sizeof(req)); | |
194 | ||
195 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
196 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
197 | req.n.nlmsg_type = RTM_SETLINK; | |
198 | req.ifm.ifi_family = PF_BRIDGE; | |
199 | ||
200 | while (argc > 0) { | |
201 | if (strcmp(*argv, "dev") == 0) { | |
202 | NEXT_ARG(); | |
203 | d = *argv; | |
204 | } else if (strcmp(*argv, "guard") == 0) { | |
205 | NEXT_ARG(); | |
206 | if (!on_off("guard", &bpdu_guard, *argv)) | |
207 | exit(-1); | |
208 | } else if (strcmp(*argv, "hairpin") == 0) { | |
209 | NEXT_ARG(); | |
210 | if (!on_off("hairping", &hairpin, *argv)) | |
211 | exit(-1); | |
212 | } else if (strcmp(*argv, "fastleave") == 0) { | |
213 | NEXT_ARG(); | |
214 | if (!on_off("fastleave", &fast_leave, *argv)) | |
215 | exit(-1); | |
216 | } else if (strcmp(*argv, "cost")) { | |
217 | NEXT_ARG(); | |
218 | cost = atoi(*argv); | |
219 | } else if (strcmp(*argv, "priority")) { | |
220 | NEXT_ARG(); | |
221 | priority = atoi(*argv); | |
222 | } else if (strcmp(*argv, "state")) { | |
223 | NEXT_ARG(); | |
224 | state = atoi(*argv); | |
225 | } else if (strcmp(*argv, "mode")) { | |
226 | NEXT_ARG(); | |
227 | flags |= BRIDGE_FLAGS_SELF; | |
228 | if (strcmp(*argv, "vepa") == 0) | |
229 | mode = BRIDGE_MODE_VEPA; | |
230 | else if (strcmp(*argv, "veb") == 0) | |
231 | mode = BRIDGE_MODE_VEB; | |
232 | else { | |
233 | fprintf(stderr, | |
234 | "Mode argument must be \"vepa\" or " | |
235 | "\"veb\".\n"); | |
236 | exit(-1); | |
237 | } | |
238 | } else { | |
239 | usage(); | |
240 | } | |
241 | argc--; argv++; | |
242 | } | |
243 | if (d == NULL) { | |
244 | fprintf(stderr, "Device is a required argument.\n"); | |
245 | exit(-1); | |
246 | } | |
247 | ||
248 | ||
249 | req.ifm.ifi_index = ll_name_to_index(d); | |
250 | if (req.ifm.ifi_index == 0) { | |
251 | fprintf(stderr, "Cannot find bridge device \"%s\"\n", d); | |
252 | exit(-1); | |
253 | } | |
254 | ||
255 | /* Nested PROTINFO attribute. Contains: port flags, cost, priority and | |
256 | * state. | |
257 | */ | |
258 | nest = addattr_nest(&req.n, sizeof(req), | |
259 | IFLA_PROTINFO | NLA_F_NESTED); | |
260 | /* Flags first */ | |
261 | if (bpdu_guard >= 0) | |
262 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_GUARD, bpdu_guard); | |
263 | if (hairpin >= 0) | |
264 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_MODE, hairpin); | |
265 | if (fast_leave >= 0) | |
266 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_FAST_LEAVE, | |
267 | fast_leave); | |
268 | ||
269 | if (cost > 0) | |
270 | addattr32(&req.n, sizeof(req), IFLA_BRPORT_COST, cost); | |
271 | ||
272 | if (priority >= 0) | |
273 | addattr16(&req.n, sizeof(req), IFLA_BRPORT_PRIORITY, priority); | |
274 | ||
275 | if (state >= 0) | |
276 | addattr8(&req.n, sizeof(req), IFLA_BRPORT_STATE, state); | |
277 | ||
278 | addattr_nest_end(&req.n, nest); | |
279 | ||
280 | /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that | |
281 | * designates master or self operation as well as 'vepa' or 'veb' | |
282 | * operation modes. These are only valid in 'self' mode on some | |
283 | * devices so far. Thus we only need to include the flags attribute | |
284 | * if we are setting the hw mode. | |
285 | */ | |
286 | if (mode >= 0) { | |
287 | nest = addattr_nest(&req.n, sizeof(req), IFLA_AF_SPEC); | |
288 | ||
289 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_FLAGS, flags); | |
290 | ||
291 | if (mode >= 0) | |
292 | addattr16(&req.n, sizeof(req), IFLA_BRIDGE_MODE, mode); | |
293 | ||
294 | addattr_nest_end(&req.n, nest); | |
295 | } | |
296 | ||
297 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) | |
298 | exit(2); | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
303 | static int brlink_show(int argc, char **argv) | |
304 | { | |
305 | char *filter_dev = NULL; | |
306 | ||
307 | while (argc > 0) { | |
308 | if (strcmp(*argv, "dev") == 0) { | |
309 | NEXT_ARG(); | |
310 | if (filter_dev) | |
311 | duparg("dev", *argv); | |
312 | filter_dev = *argv; | |
313 | } | |
314 | argc--; argv++; | |
315 | } | |
316 | ||
317 | if (filter_dev) { | |
318 | if ((filter_index = ll_name_to_index(filter_dev)) == 0) { | |
319 | fprintf(stderr, "Cannot find device \"%s\"\n", | |
320 | filter_dev); | |
321 | return -1; | |
322 | } | |
323 | } | |
324 | ||
325 | if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0) { | |
326 | perror("Cannon send dump request"); | |
327 | exit(1); | |
328 | } | |
329 | ||
330 | if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) { | |
331 | fprintf(stderr, "Dump terminated\n"); | |
332 | exit(1); | |
333 | } | |
334 | return 0; | |
335 | } | |
336 | ||
337 | int do_link(int argc, char **argv) | |
338 | { | |
339 | ll_init_map(&rth); | |
340 | if (argc > 0) { | |
341 | if (matches(*argv, "set") == 0 || | |
342 | matches(*argv, "change") == 0) | |
343 | return brlink_modify(argc-1, argv+1); | |
344 | if (matches(*argv, "show") == 0 || | |
345 | matches(*argv, "lst") == 0 || | |
346 | matches(*argv, "list") == 0) | |
347 | return brlink_show(argc-1, argv+1); | |
348 | if (matches(*argv, "help") == 0) | |
349 | usage(); | |
350 | } else | |
351 | return brlink_show(0, NULL); | |
352 | ||
353 | fprintf(stderr, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv); | |
354 | exit(-1); | |
355 | } |