File: brcfg.22.c

package info (click to toggle)
bridgex 0.30
  • links: PTS
  • area: main
  • in suites: potato
  • size: 132 kB
  • ctags: 191
  • sloc: ansic: 1,250; makefile: 40; sh: 32
file content (529 lines) | stat: -rw-r--r-- 12,380 bytes parent folder | download
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * This is a bridge configuration tool for Linux Kernels 2.1.X
 * Taken from a free floating sourcecode and revised/improved/hacked upon
 * by Christoph Lameter <clameter@debian.org>
 * 12 September 1996 Revised syntax, expanded the tool to use all options
 *	that the bridge offers. Added protocol filtering support.
 * 10 September 1999 Fixed bugs and extended to support new interface ioctls 
 * I put in the bridge. - Matthew Grant <grantma@anathoth.gen.nz>
 *
 */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <asm/byteorder.h>
#include <sys/types.h>
#include <sys/socket.h>
/* #include <linux/skbuff.h> */
#include <linux/sockios.h>
/* #include <net/br.h> */
#include "/usr/src/linux/include/net/br.h"
/* #include "br.h" */


struct br_stat br_stats;
int fd;
char *bname;

struct prot {
  int nr;
  char *name;
} protocols[] =
{ 0x0060,"LOOP",
  0x0200,"ECHO",
  0x0400,"PUP",
  0x0800,"IP",
  0x0805,"X25",  
  0x0806,"ARP", 
  0x08FF,"BPQ",
  0x6000,"DEC",
  0x6001,"DNA_DL",
  0x6002,"DNA_RC",
  0x6003,"DNA_RT",
  0x6004,"LAT",
  0x6005,"DIAG",
  0x6006,"CUST",
  0x6007,"SCA",
  0x8035,"RARP",
  0x809B,"ATALK",
  0x80F3,"AARP",
  0x8137,"IPX",
  0x86DD,"IPV6",
  0x0001,"802_3",
  0x0002,"AX25",
  0x0004,"802_2",
  0x0005,"SNAP",
  0x0006,"DDCMP",
  0x0011,"TR_802_2",
  0,NULL
};

void disp_protocol(int protocol)
{
  struct prot *p;
  for (p=protocols;p->nr;p++) {
    if (protocol==p->nr) { printf(p->name);return; }
  }
  printf("0x%04X",protocol);
};

int
disp_id(id)
     bridge_id_t *id;
{
  printf("0x%04x %02x:%02x:%02x:%02x:%02x:%02x",
	 id->BRIDGE_PRIORITY,
	 id->BRIDGE_ID_ULA[0],
	 id->BRIDGE_ID_ULA[1],
	 id->BRIDGE_ID_ULA[2],
	 id->BRIDGE_ID_ULA[3],
	 id->BRIDGE_ID_ULA[4],
	 id->BRIDGE_ID_ULA[5]);
  return(0);
};

int disp_ports(ports, n)
     Port_data *ports;
     int n;
{
  int i;
  int flags = 0;

  printf("--- port stats ---\n");
  for (i=0; i<n; i++) {
    if (ports[i].state == Disabled)
      continue;
    printf("port %i\t",i);
    printf("port id 0x%04x\t",ports[i].port_id);
    printf("port state	");
    switch(ports[i].state) {
    case Disabled:
      printf("DISABLED (0x%x)\n",ports[i].state);
      break;
    case Listening:
      printf("LISTENING (0x%x)\n",ports[i].state);
      break;
    case Learning:
      printf("LEARNING (0x%x)\n",ports[i].state);
      break;
    case Forwarding:
      printf("FORWARDING (0x%x)\n",ports[i].state);
      break;
    case Blocking:
      printf("BLOCKING (0x%x)\n",ports[i].state);
      break;
    }	
    printf("designated root		");
    disp_id(&ports[i].designated_root);
    printf("\n");
    printf("designated bridge	");
    disp_id(&ports[i].designated_bridge);
    printf("\n");
    printf("path cost		%i\t",ports[i].path_cost);
    printf("designated cost		%i\n",ports[i].designated_cost);
    printf("designated port		%i\t",ports[i].designated_port);
    printf("flags			");
    if (ports[i].top_change_ack) {
      printf("TOPOLOGY_CHANGE_ACK ");
      flags++;
    }
    if (ports[i].config_pending) {
      printf("CONFIG_PENDING");
      flags++;
    }
    if (!flags)
      printf("NONE");
    printf("\n\n");
  }
  return(0);
};

/*
 * Display current bridge status
 */
 
char *policies[2] = { "Reject all protocols","Accept all protocols"};

int disp_bridge(Bridge_data *bridge)
{
  int flags = 0;

  printf("bridge id		");
  disp_id(&bridge->bridge_id);
  printf("\n");
  printf("designated root		");
  disp_id(&bridge->designated_root);
  printf("\n");
  printf("bridge max age		%i\t",bridge->bridge_max_age);
  printf("max age			%i\n",bridge->max_age);
  printf("bridge hello time	%i\t",bridge->bridge_hello_time);
  printf("hello time		%i\n",bridge->hello_time);
  printf("bridge forward delay	%i\t",bridge->bridge_forward_delay);
  printf("forward delay		%i\n",bridge->forward_delay);
  printf("root path cost		%i\t",bridge->root_path_cost);
  printf("root port		%i\n",bridge->root_port);
  printf("flags			");
  if (bridge->top_change) {
    printf("TOPOLOGY_CHANGE ");
    flags++;
  }
  if (bridge->top_change_detected) {
    printf("TOPOLOGY_CHANGE_DETECTED");
    flags++;
  }
  if (!flags)
    printf("NONE");
  printf("\n");
  return(0);
}

void bridge_status(void)
{	int i;
 if (ioctl(fd, SIOCGIFBR, &br_stats) < 0) {
   printf("Kernel Bridge Module missing.\n");
   exit(1);
 }
 printf("bridging is %sABLED"
#ifdef BRCMD_TOGGLE_STP
	"\tSTP is %sABLED"
#endif
	"\ndebugging is %sABLED\tprot-stats are %sABLED\n", 
	br_stats.flags & BR_UP?"EN":"DIS",
#ifdef BRCMD_TOGGLE_STP
	br_stats.flags & BR_STP_DISABLED?"DIS":"EN",
#endif
	br_stats.flags & BR_DEBUG?"EN":"DIS",
	br_stats.flags & BR_PROT_STATS?"EN":"DIS");
 disp_bridge(&br_stats.bridge_data);
 disp_ports(&br_stats.port_data, 8);
 printf("Policy                  %s\n",policies[br_stats.policy]);
 printf("exempt protocols        %i ",br_stats.exempt_protocols);
 if (br_stats.exempt_protocols)
   { putchar('(');
   for(i=0;i<BR_MAX_PROTOCOLS;i++)
     if (br_stats.protocols[i])
       { disp_protocol(br_stats.protocols[i]);putchar(' ');
       }
   putchar(')');
   }
 printf("\n");
}

void cmd(int cmd,int arg1,int arg2)
{ struct br_cf bcf;

 bcf.cmd = cmd;
 bcf.arg1 = arg1;
 bcf.arg2 = arg2;

	
 if (ioctl(fd, SIOCSIFBR, &bcf) < 0) {
#if 0
   fprintf(stderr, "%d\n", errno);
   fprintf(stderr, "%d\n", arg1);
#endif			
   perror("ioctl(SIOCSIFBR) failed");
   exit(1);
 }
}

void help(void)
{ printf("brcfg - Bridge Configuration tool v0.2\n"
	 "--------------------------------------\n"
	 "brcfg star[t]				Start Bridge\n"
	 "brcfg sto[p]				Stop Bridge\n"
#ifdef BRCMD_TOGGLE_STP
	 "brcfg S[TP]				Toggle Spanning Tree Protocol (on by default)\n"
#endif
	 "brcfg p[ort] x e[nable]			Enable a port\n"
	 "brcfg p[ort] x d[isable]		Disable a port\n"
	 "brcfg p[ort] x pr[iority] y		Set the priority of a port\n"
	 "brcfg p[ort] x pa[thcost] y		Set the pathcost of a port\n"
	 "brcfg pr[iority] y			Set bridge priority\n"
	 "brcfg d[ebug] on			Switch debugging on\n"
	 "brcfg d[ebug] off			Switch debugging off\n"
	 "brcfg pol[icy] r[eject]/a[ccept]	Switch the policy/flush protocol list\n"
	 "brcfg e[xempt] <protocol> ..		Set list of exempt protocols\n"
	 "brcfg l[ist]				List available protocols\n"
	 "brcfg stat[s] z[ero]			Reset Statistics counters\n"
	 "brcfg stat[s] d[isable]			Switch protocol statistics off\n"
	 "brcfg stat[s] e[nable]			Switch keeping protocol statistics on\n"
	 "brcfg stat[s] s[how]			Show protocol statistics\n"
	 "brcfg statu[s]				Show bridge status\n"
	 "\n"
	 "Examples:\n"
	 "---------\n"
	 "brcfg start exempt atalk aarp		Bridge start dont do LocalTalk bridging\n"
	 "\n"
	 "brcfg stop				Bridge stop\n");
}

void debug(char *option)
{ if (strcasecmp(option,"on")==0)
  { cmd(BRCMD_ENABLE_DEBUG,0,0);
  printf("Debug on.\n");
  } else
    if (strcasecmp(option,"off")==0)
      { cmd(BRCMD_DISABLE_DEBUG,0,0);
      printf("Debug off.\n");
      } else
	{ printf("Debug mode can only be on or off!");
	exit(1);
	}
}

void exempt(char *protocol)
{ /* identify protocol */

  struct prot *p;

  for(p=protocols;p->nr && strcasecmp(protocol,p->name)!=0;p++) ;
  if (p->nr)
    { cmd(BRCMD_EXEMPT_PROTOCOL,p->nr,0);
    printf("Exempt protocol ");disp_protocol(p->nr);printf(".\n");
    return;
    }
  printf("Protocol to be exempted %s not in protocol list\n(See /usr/include/net/if_ether.h)\n",protocol);
  exit(1);
}

void listprots(void)
{ struct prot *p;
 printf("Protocol Listing:\n------------------\n");
 for (p=protocols;p->nr;p++) printf("0x%04X %s\n",p->nr,p->name);
}

void policy(char *kind)
{ if (kind[0]=='a')
  { cmd(BRCMD_SET_POLICY,1,0);
  printf("Policy accept all protocols.\n");
  } else
    if (kind[0]=='r')
      { cmd(BRCMD_SET_POLICY,0,0);
      printf("Policy reject all protocols.\n");
      } else
	{ printf("Policy must be either accept or deny.\n");
	exit(1);
	}
}

void pathcost(char *port,char *cost)
{ int p=atoi(port),c=atoi(cost);
 cmd(BRCMD_SET_PATH_COST,p,c);
 printf("Pathcost for port %d set to %d",p,c);
}

void port(char *no,char *mode)
{ int port=atoi(no);
#if 0
 printf("Mode: %s, Port %d\n", mode, port);
#endif
 if (mode[0]=='e')
   { cmd(BRCMD_PORT_ENABLE,port,0);
   printf("Enable Port %d\n",port);
   } else
     if (mode[0]=='d')
       { cmd(BRCMD_PORT_DISABLE,port,0);
       printf("Disable Port %d\n",port);
       } else
	 { printf("Port option can only be enable or disable");
	 }
}

void portprior(char *port,char *prio)
{ int po=atoi(port),pr=atoi(prio);
 cmd(BRCMD_SET_PORT_PRIORITY,po,pr);
 printf("Port Priority for port %d set to %d\n",po,pr);
}

void priority(char *prio)
{ int p=atoi(prio);
 cmd(BRCMD_SET_BRIDGE_PRIORITY,p,0);
 printf("Bridge priority set to %d\n",p);
}

void bridge_stat(char *a)
{	int x;
 switch (a[0]) {
 case 'e' : 
   cmd(BRCMD_ENABLE_PROT_STATS,0,0);
   printf("Protocol Statistics enabled\n");
   break;
 case 'd' : 
   cmd(BRCMD_DISABLE_PROT_STATS,0,0);
   printf("Protocol Statistics disabled\n");
   break;
 case 'z' : 
   cmd(BRCMD_ZERO_PROT_STATS,0,0);
   printf("Protocol Statistics counters reset.\n");
   break;
 case 's' :
   if (ioctl(fd, SIOCGIFBR, &br_stats) < 0) {
     printf("Kernel Bridge Module missing.\n");
     exit(1);
   }
   printf("Protocol statistics are %s\n",(br_stats.flags & BR_PROT_STATS) ? "ENABLED" : "DISABLED");
   if (br_stats.prot_id[0])
     {
       printf("Protocol Number of Packets encountered\n");
       printf("--------------------------------------\n");
       for(x=0;x<BR_MAX_PROT_STATS && br_stats.prot_id[x]!=0;x++) {
	 disp_protocol(br_stats.prot_id[x]);printf(" %d\n",br_stats.prot_counter[x]);
       }
       if (br_stats.prot_id[BR_MAX_PROT_STATS-1]) {
	 printf("Warning: Protocol table too short. Possibly not all protocols listed!n");
       }
     } else {
       printf("No protocol data has been gathered by the kernel\n");
     }
   break;
 default  : printf("stat argument must be enable,disable,zero or show\n");
   exit(1);
   break;
 }
}



void
arg_error(void) {
  fprintf(stdout, "%s: Unrecognised option syntax. See '%s help'\n", bname, bname);
  exit(1);
}

void
option_error(void) {
  fprintf(stdout, "%s: Unknown option. See '%s help'\n", bname, bname);
  exit(1);
}

int main(int argc, char **argv)
{
  bname = strrchr(argv[0], '/')+1;

  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd <= 0) {
    perror("socket failed: Probably bridge not compiled into kernel");
    return(-1);
  };
  if (argc==1) {
    bridge_status();
  }
  else {
    for (argv++;*argv;argv++) {	
      char *p;

      p=*argv; 
      while (*p=='-' || *p=='/') p++;

      switch (*p) {

      case 'h':			/* help */
	help();
	break;

#ifdef BRCMD_TOGGLE_STP
      case 'S':			/* Toggle Spanning Tree Protocol */
	cmd(BRCMD_TOGGLE_STP, 0, 0);
	break;
#endif
      case 'd':			/* Handle debug */
	argv++;
	if( *argv == NULL ) {
	  arg_error();
	  break;
	}
	debug(*argv);
	break;

      case 'e':			/* Exempt Protocols */
	argv++;
	if( *argv == NULL ) {
	  arg_error();
	  break;
	}
	for(;*argv;argv++) 
	  exempt(*argv);
	argv--;
	break;

      case 'p':			/* pathcost/priority/policy/port */
	if (p[1]=='r') {
	  argv++;		/* Bridge priority */
	  if( *argv == NULL ) arg_error;
	  priority(*argv);
	  break;
	}
	else if (p[1]=='o' && p[2]=='l') { 
	  argv++;		/* Bridge policy */
	  if( *argv == NULL ) {
	    arg_error();
	    break;
	  }
	  policy(*argv);
	  break;
	}
	else {  
	  if( argv[1] == NULL || argv[2] == NULL ) 
	    arg_error();
	  if (argv[2][0]=='p' && argv[2][1]=='a') {
	    if( argv[3] ==  NULL) 
	      arg_error();
	    pathcost(argv[1],argv[3]);
	    argv += 3;
	    break;
	  }
	  else if (argv[2][0]=='p' && argv[2][1]=='r') { 
	    if( argv[3] ==  NULL) 
	      arg_error();
	    portprior(argv[1],argv[3]);
	    argv += 3;
	    break;
	  }
	  else {
	    port(argv[1],argv[2]);
	    argv += 2;
	    break;
	  }
	}
	break;
 
      case 's': 
	if (p[3]=='r') {
	  cmd(BRCMD_BRIDGE_ENABLE,0,0);
	  printf("Bridge started.\n");
	  break;
	}
	else if (p[2]=='o') {
	  cmd(BRCMD_BRIDGE_DISABLE,0,0);
	  printf("Bridge stopped.\n");
	  break;
	}
	else if (p[3]=='t') {
	  if (argv[1]==NULL) 
	    bridge_stat("show");
	  else {
	    argv++;
	    if( *argv == NULL ) arg_error;
	    bridge_stat(*argv);
	  }
	} else
	  bridge_status();
	break;

      case 'l': 
	listprots();
	break;

      default: 
	option_error();
	break;

      }
    }
  }  
  
  close(fd);
  return 0;
}