1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
/*
* Copyright (C) 2000 Lennert Buytenhek
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>
#include <asm/param.h>
#include "libbridge.h"
#include "brctl.h"
static int strtotimeval(struct timeval *tv, const char *time)
{
double secs;
if (sscanf(time, "%lf", &secs) != 1)
return -1;
tv->tv_sec = secs;
tv->tv_usec = 1000000 * (secs - tv->tv_sec);
return 0;
}
static int br_cmd_addbr(int argc, char*const* argv)
{
int err;
switch (err = br_add_bridge(argv[1])) {
case 0:
return 0;
case EEXIST:
fprintf(stderr, "device %s already exists; can't create "
"bridge with the same name\n", argv[1]);
return 1;
default:
fprintf(stderr, "add bridge failed: %s\n",
strerror(err));
return 1;
}
}
static int br_cmd_delbr(int argc, char*const* argv)
{
int err;
switch (err = br_del_bridge(argv[1])){
case 0:
return 0;
case ENXIO:
fprintf(stderr, "bridge %s doesn't exist; can't delete it\n",
argv[1]);
return 1;
case EBUSY:
fprintf(stderr, "bridge %s is still up; can't delete it\n",
argv[1]);
return 1;
default:
fprintf(stderr, "can't delete bridge %s: %s\n",
argv[1], strerror(err));
return 1;
}
}
static int br_cmd_addif(int argc, char *const* argv)
{
const char *brname;
int err;
argc -= 2;
brname = *++argv;
while (argc-- > 0) {
const char *ifname = *++argv;
err = br_add_interface(brname, ifname);
switch(err) {
case 0:
continue;
case ENODEV:
if (if_nametoindex(ifname) == 0)
fprintf(stderr, "interface %s does not exist!\n", ifname);
else
fprintf(stderr, "bridge %s does not exist!\n", brname);
break;
case EBUSY:
fprintf(stderr, "device %s is already a member of a bridge; "
"can't enslave it to bridge %s.\n", ifname,
brname);
break;
case ELOOP:
fprintf(stderr, "device %s is a bridge device itself; "
"can't enslave a bridge device to a bridge device.\n",
ifname);
break;
default:
fprintf(stderr, "can't add %s to bridge %s: %s\n",
ifname, brname, strerror(err));
}
return 1;
}
return 0;
}
static int br_cmd_delif(int argc, char *const* argv)
{
const char *brname;
int err;
argc -= 2;
brname = *++argv;
while (argc-- > 0) {
const char *ifname = *++argv;
err = br_del_interface(brname, ifname);
switch (err) {
case 0:
continue;
case ENODEV:
if (if_nametoindex(ifname) == 0)
fprintf(stderr, "interface %s does not exist!\n", ifname);
else
fprintf(stderr, "bridge %s does not exist!\n", brname);
break;
case EINVAL:
fprintf(stderr, "device %s is not a slave of %s\n",
ifname, brname);
break;
default:
fprintf(stderr, "can't delete %s from %s: %s\n",
ifname, brname, strerror(err));
}
return 1;
}
return 0;
}
static int br_cmd_setageing(int argc, char *const* argv)
{
int err;
struct timeval tv;
if (strtotimeval(&tv, argv[2])) {
fprintf(stderr, "bad ageing time value\n");
return 1;
}
err = br_set_ageing_time(argv[1], &tv);
if (err)
fprintf(stderr, "set ageing time failed: %s\n",
strerror(err));
return err != 0;
}
static int br_cmd_setbridgeprio(int argc, char *const* argv)
{
int prio;
int err;
if (sscanf(argv[2], "%i", &prio) != 1) {
fprintf(stderr,"bad priority\n");
return 1;
}
err = br_set_bridge_priority(argv[1], prio);
if (err)
fprintf(stderr, "set bridge priority failed: %s\n",
strerror(err));
return err != 0;
}
static int br_cmd_setfd(int argc, char *const* argv)
{
struct timeval tv;
int err;
if (strtotimeval(&tv, argv[2])) {
fprintf(stderr, "bad forward delay value\n");
return 1;
}
err = br_set_bridge_forward_delay(argv[1], &tv);
if (err)
fprintf(stderr, "set forward delay failed: %s\n",
strerror(err));
return err != 0;
}
static int br_cmd_sethello(int argc, char *const* argv)
{
struct timeval tv;
int err;
if (strtotimeval(&tv, argv[2])) {
fprintf(stderr, "bad hello timer value\n");
return 1;
}
err = br_set_bridge_hello_time(argv[1], &tv);
if (err)
fprintf(stderr, "set hello timer failed: %s\n",
strerror(err));
return err != 0;
}
static int br_cmd_setmaxage(int argc, char *const* argv)
{
struct timeval tv;
int err;
if (strtotimeval(&tv, argv[2])) {
fprintf(stderr, "bad max age value\n");
return 1;
}
err = br_set_bridge_max_age(argv[1], &tv);
if (err)
fprintf(stderr, "set max age failed: %s\n",
strerror(err));
return err != 0;
}
static int br_cmd_setpathcost(int argc, char *const* argv)
{
int cost, err;
if (sscanf(argv[3], "%i", &cost) != 1) {
fprintf(stderr, "bad path cost value\n");
return 1;
}
err = br_set_path_cost(argv[1], argv[2], cost);
if (err)
fprintf(stderr, "set path cost failed: %s\n",
strerror(err));
return err != 0;
}
static int br_cmd_setportprio(int argc, char *const* argv)
{
int cost, err;
if (sscanf(argv[3], "%i", &cost) != 1) {
fprintf(stderr, "bad path priority value\n");
return 1;
}
err = br_set_port_priority(argv[1], argv[2], cost);
if (err)
fprintf(stderr, "set port priority failed: %s\n",
strerror(errno));
return err != 0;
}
static int br_cmd_stp(int argc, char *const* argv)
{
int stp, err;
if (!strcmp(argv[2], "on") || !strcmp(argv[2], "yes")
|| !strcmp(argv[2], "1"))
stp = 1;
else if (!strcmp(argv[2], "off") || !strcmp(argv[2], "no")
|| !strcmp(argv[2], "0"))
stp = 0;
else {
fprintf(stderr, "expect on/off for argument\n");
return 1;
}
err = br_set_stp_state(argv[1], stp);
if (err)
fprintf(stderr, "set stp status failed: %s\n",
strerror(errno));
return err != 0;
}
static int br_cmd_showstp(int argc, char *const* argv)
{
struct bridge_info info;
const char *name = argv[1];
if (br_get_bridge_info(name, &info)) {
if (errno == ENODEV)
fprintf(stderr, "bridge %s does not exist!\n", name);
else
fprintf(stderr, "device %s is not a bridge!\n", name);
return 1;
}
br_dump_info(name, &info);
return 0;
}
static int show_bridge(const char *name, void *arg)
{
struct bridge_info info;
static int show_header = 1;
if (br_get_bridge_info(name, &info)) {
if (errno == ENODEV)
fprintf(stderr, "bridge %s does not exist!\n", name);
else
fprintf(stderr, "device %s is not a bridge!\n", name);
return 1;
}
if (show_header) {
printf("bridge name\tbridge id\t\tSTP enabled\tinterfaces\n");
show_header = 0;
}
printf("%s\t\t", name);
br_dump_bridge_id((unsigned char *)&info.bridge_id);
printf("\t%s\t\t", info.stp_enabled?"yes":"no");
br_dump_interface_list(name);
return 0;
}
static int br_cmd_show(int argc, char *const* argv)
{
int i, errs = 0;
if (argc == 1)
br_foreach_bridge(show_bridge, NULL);
else
for(i = 2; i <= argc; i++)
errs += show_bridge(argv[i - 1], NULL);
return errs > 0;
}
static int compare_fdbs(const void *_f0, const void *_f1)
{
const struct fdb_entry *f0 = _f0;
const struct fdb_entry *f1 = _f1;
return memcmp(f0->mac_addr, f1->mac_addr, 6);
}
static int br_cmd_showmacs(int argc, char *const* argv)
{
const char *brname = argv[1];
#define CHUNK 128
int i, n;
struct fdb_entry *fdb = NULL;
int offset = 0;
for(;;) {
fdb = realloc(fdb, (offset + CHUNK) * sizeof(struct fdb_entry));
if (!fdb) {
fprintf(stderr, "Out of memory\n");
return 1;
}
n = br_read_fdb(brname, fdb+offset, offset, CHUNK);
if (n == 0)
break;
if (n < 0) {
fprintf(stderr, "read of forward table failed: %s\n",
strerror(errno));
return 1;
}
offset += n;
}
qsort(fdb, offset, sizeof(struct fdb_entry), compare_fdbs);
printf("port no\tmac addr\t\tis local?\tageing timer\n");
for (i = 0; i < offset; i++) {
const struct fdb_entry *f = fdb + i;
printf("%3i\t", f->port_no);
printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\t",
f->mac_addr[0], f->mac_addr[1], f->mac_addr[2],
f->mac_addr[3], f->mac_addr[4], f->mac_addr[5]);
printf("%s\t\t", f->is_local?"yes":"no");
br_show_timer(&f->ageing_timer_value);
printf("\n");
}
return 0;
}
static int br_cmd_hairpin(int argc, char *const* argv)
{
int hairpin, err;
const char *brname = *++argv;
const char *ifname = *++argv;
const char *hpmode = *++argv;
if (!strcmp(hpmode, "on") || !strcmp(hpmode, "yes")
|| !strcmp(hpmode, "1"))
hairpin = 1;
else if (!strcmp(hpmode, "off") || !strcmp(hpmode, "no")
|| !strcmp(hpmode, "0"))
hairpin = 0;
else {
fprintf(stderr, "expect on/off for argument\n");
return 1;
}
if (if_nametoindex(ifname) == 0) {
fprintf(stderr, "interface %s does not exist!\n",
ifname);
return 1;
} else if (if_nametoindex(brname) == 0) {
fprintf(stderr, "bridge %s does not exist!\n",
brname);
return 1;
}
err = br_set_hairpin_mode(brname, ifname, hairpin);
if (err) {
fprintf(stderr, "can't set %s to hairpin on bridge %s: %s\n",
ifname, brname, strerror(err));
}
return err != 0;
}
static const struct command commands[] = {
{ 1, "addbr", br_cmd_addbr, "<bridge>\t\tadd bridge" },
{ 1, "delbr", br_cmd_delbr, "<bridge>\t\tdelete bridge" },
{ 2, "addif", br_cmd_addif,
"<bridge> <device>\tadd interface to bridge" },
{ 2, "delif", br_cmd_delif,
"<bridge> <device>\tdelete interface from bridge" },
{ 3, "hairpin", br_cmd_hairpin,
"<bridge> <port> {on|off}\tturn hairpin on/off" },
{ 2, "setageing", br_cmd_setageing,
"<bridge> <time>\t\tset ageing time" },
{ 2, "setbridgeprio", br_cmd_setbridgeprio,
"<bridge> <prio>\t\tset bridge priority" },
{ 2, "setfd", br_cmd_setfd,
"<bridge> <time>\t\tset bridge forward delay" },
{ 2, "sethello", br_cmd_sethello,
"<bridge> <time>\t\tset hello time" },
{ 2, "setmaxage", br_cmd_setmaxage,
"<bridge> <time>\t\tset max message age" },
{ 3, "setpathcost", br_cmd_setpathcost,
"<bridge> <port> <cost>\tset path cost" },
{ 3, "setportprio", br_cmd_setportprio,
"<bridge> <port> <prio>\tset port priority" },
{ 0, "show", br_cmd_show,
"[ <bridge> ]\t\tshow a list of bridges" },
{ 1, "showmacs", br_cmd_showmacs,
"<bridge>\t\tshow a list of mac addrs"},
{ 1, "showstp", br_cmd_showstp,
"<bridge>\t\tshow bridge stp info"},
{ 2, "stp", br_cmd_stp,
"<bridge> {on|off}\tturn stp on/off" },
};
const struct command *command_lookup(const char *cmd)
{
int i;
for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) {
if (!strcmp(cmd, commands[i].name))
return &commands[i];
}
return NULL;
}
void command_helpall(void)
{
int i;
for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) {
printf("\t%-10s\t%s\n", commands[i].name, commands[i].help);
}
}
|