]> git.proxmox.com Git - mirror_iproute2.git/blob - bridge/bridge.c
mroute: Add table id attribute for kernel side filtering
[mirror_iproute2.git] / bridge / bridge.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Get/set/delete bridge with netlink
4 *
5 * Authors: Stephen Hemminger <shemminger@vyatta.com>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/socket.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include "SNAPSHOT.h"
16 #include "utils.h"
17 #include "br_common.h"
18 #include "namespace.h"
19 #include "color.h"
20
21 struct rtnl_handle rth = { .fd = -1 };
22 int preferred_family = AF_UNSPEC;
23 int oneline;
24 int show_stats;
25 int show_details;
26 static int color;
27 int compress_vlans;
28 int json;
29 int timestamp;
30 static const char *batch_file;
31 int force;
32
33 static void usage(void) __attribute__((noreturn));
34
35 static void usage(void)
36 {
37 fprintf(stderr,
38 "Usage: bridge [ OPTIONS ] OBJECT { COMMAND | help }\n"
39 " bridge [ -force ] -batch filename\n"
40 "where OBJECT := { link | fdb | mdb | vlan | monitor }\n"
41 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
42 " -o[neline] | -t[imestamp] | -n[etns] name |\n"
43 " -c[ompressvlans] -color -p[retty] -j[son] }\n");
44 exit(-1);
45 }
46
47 static int do_help(int argc, char **argv)
48 {
49 usage();
50 }
51
52
53 static const struct cmd {
54 const char *cmd;
55 int (*func)(int argc, char **argv);
56 } cmds[] = {
57 { "link", do_link },
58 { "fdb", do_fdb },
59 { "mdb", do_mdb },
60 { "vlan", do_vlan },
61 { "monitor", do_monitor },
62 { "help", do_help },
63 { 0 }
64 };
65
66 static int do_cmd(const char *argv0, int argc, char **argv)
67 {
68 const struct cmd *c;
69
70 for (c = cmds; c->cmd; ++c) {
71 if (matches(argv0, c->cmd) == 0)
72 return c->func(argc-1, argv+1);
73 }
74
75 fprintf(stderr,
76 "Object \"%s\" is unknown, try \"bridge help\".\n", argv0);
77 return -1;
78 }
79
80 static int batch(const char *name)
81 {
82 char *line = NULL;
83 size_t len = 0;
84 int ret = EXIT_SUCCESS;
85
86 if (name && strcmp(name, "-") != 0) {
87 if (freopen(name, "r", stdin) == NULL) {
88 fprintf(stderr,
89 "Cannot open file \"%s\" for reading: %s\n",
90 name, strerror(errno));
91 return EXIT_FAILURE;
92 }
93 }
94
95 if (rtnl_open(&rth, 0) < 0) {
96 fprintf(stderr, "Cannot open rtnetlink\n");
97 return EXIT_FAILURE;
98 }
99
100 cmdlineno = 0;
101 while (getcmdline(&line, &len, stdin) != -1) {
102 char *largv[100];
103 int largc;
104
105 largc = makeargs(line, largv, 100);
106 if (largc == 0)
107 continue; /* blank line */
108
109 if (do_cmd(largv[0], largc, largv)) {
110 fprintf(stderr, "Command failed %s:%d\n",
111 name, cmdlineno);
112 ret = EXIT_FAILURE;
113 if (!force)
114 break;
115 }
116 }
117 if (line)
118 free(line);
119
120 rtnl_close(&rth);
121 return ret;
122 }
123
124 int
125 main(int argc, char **argv)
126 {
127 while (argc > 1) {
128 const char *opt = argv[1];
129
130 if (strcmp(opt, "--") == 0) {
131 argc--; argv++;
132 break;
133 }
134 if (opt[0] != '-')
135 break;
136 if (opt[1] == '-')
137 opt++;
138
139 if (matches(opt, "-help") == 0) {
140 usage();
141 } else if (matches(opt, "-Version") == 0) {
142 printf("bridge utility, 0.0\n");
143 exit(0);
144 } else if (matches(opt, "-stats") == 0 ||
145 matches(opt, "-statistics") == 0) {
146 ++show_stats;
147 } else if (matches(opt, "-details") == 0) {
148 ++show_details;
149 } else if (matches(opt, "-oneline") == 0) {
150 ++oneline;
151 } else if (matches(opt, "-timestamp") == 0) {
152 ++timestamp;
153 } else if (matches(opt, "-family") == 0) {
154 argc--;
155 argv++;
156 if (argc <= 1)
157 usage();
158 if (strcmp(argv[1], "inet") == 0)
159 preferred_family = AF_INET;
160 else if (strcmp(argv[1], "inet6") == 0)
161 preferred_family = AF_INET6;
162 else if (strcmp(argv[1], "help") == 0)
163 usage();
164 else
165 invarg("invalid protocol family", argv[1]);
166 } else if (strcmp(opt, "-4") == 0) {
167 preferred_family = AF_INET;
168 } else if (strcmp(opt, "-6") == 0) {
169 preferred_family = AF_INET6;
170 } else if (matches(opt, "-netns") == 0) {
171 NEXT_ARG();
172 if (netns_switch(argv[1]))
173 exit(-1);
174 } else if (matches(opt, "-compressvlans") == 0) {
175 ++compress_vlans;
176 } else if (matches_color(opt, &color)) {
177 } else if (matches(opt, "-force") == 0) {
178 ++force;
179 } else if (matches(opt, "-json") == 0) {
180 ++json;
181 } else if (matches(opt, "-pretty") == 0) {
182 ++pretty;
183 } else if (matches(opt, "-batch") == 0) {
184 argc--;
185 argv++;
186 if (argc <= 1)
187 usage();
188 batch_file = argv[1];
189 } else {
190 fprintf(stderr,
191 "Option \"%s\" is unknown, try \"bridge help\".\n",
192 opt);
193 exit(-1);
194 }
195 argc--; argv++;
196 }
197
198 _SL_ = oneline ? "\\" : "\n";
199
200 check_enable_color(color, json);
201
202 if (batch_file)
203 return batch(batch_file);
204
205 if (rtnl_open(&rth, 0) < 0)
206 exit(1);
207
208 if (argc > 1)
209 return do_cmd(argv[1], argc-1, argv+1);
210
211 rtnl_close(&rth);
212 usage();
213 }