]> git.ipfire.org Git - people/ms/rstp.git/blob - bridge_track.c
67f6252a1bebc51102efa67be49570bf118adbe9
[people/ms/rstp.git] / bridge_track.c
1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc., 59
16 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 The full GNU General Public License is included in this distribution in the
19 file called LICENSE.
20
21 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
22
23 ******************************************************************************/
24
25 #include "bridge_ctl.h"
26 #include "netif_utils.h"
27 #include "packet.h"
28
29 #include <unistd.h>
30 #include <net/if.h>
31 #include <stdlib.h>
32 #include <linux/if_bridge.h>
33 #include <arpa/inet.h>
34 #include <sys/types.h>
35
36 #include <bitmap.h>
37 #include <uid_stp.h>
38 #include <stp_bpdu.h>
39 #include <stp_in.h>
40 #include <stp_to.h>
41
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "log.h"
46
47 /*------------------------------------------------------------*/
48
49 struct ifdata {
50 int if_index;
51 struct ifdata *next;
52 int up;
53 char name[IFNAMSIZ];
54
55 int is_bridge;
56 /* If bridge */
57 struct ifdata *bridge_next;
58 struct ifdata *port_list;
59 int do_stp;
60 int stp_up;
61 struct stp_instance *stp;
62 UID_BRIDGE_ID_T bridge_id;
63 /* Bridge config */
64 UID_STP_MODE_T stp_enabled;
65 int bridge_priority;
66 int max_age;
67 int hello_time;
68 int forward_delay;
69 int force_version;
70 int hold_time;
71
72 /* If port */
73 int speed;
74 int duplex;
75 struct ifdata *master;
76 struct ifdata *port_next;
77 /* STP port index */
78 int port_index;
79 /* STP port config */
80 int port_priority;
81 int admin_port_path_cost;
82 ADMIN_P2P_T admin_point2point;
83 unsigned char admin_edge;
84 unsigned char admin_non_stp; /* 1- doesn't participate in STP, 1 - regular */
85
86 struct epoll_event_handler event;
87 };
88
89 /* Instances */
90 struct ifdata *current_br = NULL;
91
92 void instance_begin(struct ifdata *br)
93 {
94 if (current_br) {
95 ERROR("BUG: Trying to set instance over existing instance.");
96 ERROR("%d", *(int *)0); /* ABORT */
97 }
98 current_br = br;
99 STP_IN_instance_begin(br->stp);
100 }
101
102 void instance_end(void)
103 {
104 STP_IN_instance_end(current_br->stp);
105 current_br = NULL;
106 }
107
108 struct ifdata *find_port(int port_index)
109 {
110 struct ifdata *ifc = current_br->port_list;
111 while (ifc && ifc->port_index != port_index)
112 ifc = ifc->port_next;
113 return ifc;
114 }
115
116 /*************************************************************/
117 /* Bridge and port defaults */
118
119 UID_STP_CFG_T default_bridge_stp_cfg = {
120 .field_mask = BR_CFG_ALL,
121 .bridge_priority = DEF_BR_PRIO,
122 .max_age = DEF_BR_MAXAGE,
123 .hello_time = DEF_BR_HELLOT,
124 .forward_delay = DEF_BR_FWDELAY,
125 .force_version = DEF_FORCE_VERS, /*NORMAL_RSTP */
126 };
127
128 void update_bridge_stp_config(struct ifdata *br, UID_STP_CFG_T * cfg)
129 {
130 if (cfg->field_mask & BR_CFG_PRIO)
131 br->bridge_priority = cfg->bridge_priority;
132 if (cfg->field_mask & BR_CFG_AGE)
133 br->max_age = cfg->max_age;
134 if (cfg->field_mask & BR_CFG_HELLO)
135 br->hello_time = cfg->hello_time;
136 if (cfg->field_mask & BR_CFG_DELAY)
137 br->forward_delay = cfg->forward_delay;
138 if (cfg->field_mask & BR_CFG_FORCE_VER)
139 br->force_version = cfg->force_version;
140 }
141
142 UID_STP_PORT_CFG_T default_port_stp_cfg = {
143 .field_mask = PT_CFG_ALL,
144 .port_priority = DEF_PORT_PRIO,
145 .admin_non_stp = DEF_ADMIN_NON_STP,
146 .admin_edge = False, // DEF_ADMIN_EDGE,
147 .admin_port_path_cost = ADMIN_PORT_PATH_COST_AUTO,
148 .admin_point2point = DEF_P2P,
149 };
150
151 void update_port_stp_config(struct ifdata *ifc, UID_STP_PORT_CFG_T * cfg)
152 {
153 if (cfg->field_mask & PT_CFG_PRIO)
154 ifc->port_priority = cfg->port_priority;
155 if (cfg->field_mask & PT_CFG_NON_STP)
156 ifc->admin_non_stp = cfg->admin_non_stp;
157 if (cfg->field_mask & PT_CFG_EDGE)
158 ifc->admin_edge = cfg->admin_edge;
159 if (cfg->field_mask & PT_CFG_COST)
160 ifc->admin_port_path_cost = cfg->admin_port_path_cost;
161 if (cfg->field_mask & PT_CFG_P2P)
162 ifc->admin_point2point = cfg->admin_point2point;
163 }
164
165 /**************************************************************/
166
167 int add_port_stp(struct ifdata *ifc)
168 { /* Bridge is ifc->master */
169 TST((ifc->port_index = get_bridge_portno(ifc->name)) >= 0, -1);
170
171 /* Add port to STP */
172 instance_begin(ifc->master);
173 int r = STP_IN_port_create(0, ifc->port_index);
174 if (r == 0) { /* Update bridge ID */
175 UID_STP_STATE_T state;
176 STP_IN_stpm_get_state(0, &state);
177 ifc->master->bridge_id = state.bridge_id;
178 }
179 instance_end();
180 if (r /* check for failure */ ) {
181 ERROR("Couldn't add port for ifindex %d to STP", ifc->if_index);
182 return -1;
183 }
184 return 0;
185 }
186
187 void remove_port_stp(struct ifdata *ifc)
188 {
189 /* Remove port from STP */
190 instance_begin(ifc->master);
191 int r = STP_IN_port_delete(0, ifc->port_index);
192 instance_end();
193 ifc->port_index = -1;
194 if (r != 0) {
195 ERROR("removing port %s failed for bridge %s: %s",
196 ifc->name, ifc->master->name,
197 STP_IN_get_error_explanation(r));
198 }
199 }
200
201 int init_rstplib_instance(struct ifdata *br)
202 {
203 br->stp = STP_IN_instance_create();
204 if (br->stp == NULL) {
205 ERROR("Couldn't create STP instance for bridge %s", br->name);
206 return -1;
207 }
208
209 BITMAP_T ports;
210 BitmapClear(&ports);
211 instance_begin(br);
212 int r = STP_IN_stpm_create(0, br->name, &ports);
213 instance_end();
214 if (r != 0) {
215 ERROR("stpm create failed for bridge %s: %s",
216 br->name, STP_IN_get_error_explanation(r));
217 return -1;
218 }
219
220 return 0;
221 }
222
223 void clear_rstplib_instance(struct ifdata *br)
224 {
225 instance_begin(br);
226 int r = STP_IN_delete_all();
227 instance_end();
228 if (r != 0) {
229 ERROR("stpm delete failed for bridge %s: %s",
230 br->name, STP_IN_get_error_explanation(r));
231 }
232
233 STP_IN_instance_delete(br->stp);
234 br->stp = NULL;
235 }
236
237 int init_bridge_stp(struct ifdata *br)
238 {
239 if (br->stp_up) {
240 ERROR("STP already started");
241 return 0;
242 }
243
244 /* Init STP state */
245 TST(init_rstplib_instance(br) == 0, -1);
246
247 struct ifdata *p = br->port_list;
248 while (p) {
249 if (add_port_stp(p) != 0)
250 break;
251 p = p->port_next;
252 }
253 if (p) {
254 struct ifdata *q = br->port_list;
255 while (q != p) {
256 remove_port_stp(q);
257 q = q->port_next;
258 }
259 /* Clear bridge STP state */
260 clear_rstplib_instance(br);
261 return -1;
262 }
263 br->stp_up = 1;
264 return 0;
265 }
266
267 void clear_bridge_stp(struct ifdata *br)
268 {
269 if (!br->stp_up)
270 return;
271 br->stp_up = 0;
272 struct ifdata *p = br->port_list;
273 while (p) {
274 remove_port_stp(p);
275 p = p->port_next;
276 }
277 /* Clear bridge STP state */
278 clear_rstplib_instance(br);
279 }
280
281 struct ifdata *if_head = NULL;
282 struct ifdata *br_head = NULL;
283
284 struct ifdata *find_if(int if_index)
285 {
286 struct ifdata *p = if_head;
287 while (p && p->if_index != if_index)
288 p = p->next;
289 return p;
290 }
291
292 #define ADD_TO_LIST(_list, _next, _ifc) \
293 do { \
294 (_ifc)->_next = (_list); \
295 (_list) = (_ifc); \
296 } while (0)
297
298 #define REMOVE_FROM_LIST(_list, _next, _ifc, _error_fmt, _args...) \
299 do { \
300 struct ifdata **_prev = &(_list); \
301 while (*_prev && *_prev != (_ifc)) \
302 _prev = &(*_prev)->_next; \
303 if (*_prev != (_ifc)) \
304 ERROR(_error_fmt, ##_args); \
305 else \
306 *_prev = (_ifc)->_next; \
307 } while (0)
308
309 /* Caller ensures that there isn't any ifdata with this index */
310 /* If br is NULL, new interface is a bridge, else it is a port of br */
311 struct ifdata *create_if(int if_index, struct ifdata *br)
312 {
313 struct ifdata *p;
314 TST((p = malloc(sizeof(*p))) != NULL, NULL);
315
316 memset(p, 0, sizeof(*p));
317
318 /* Init fields */
319 p->if_index = if_index;
320 p->is_bridge = (br == NULL);
321
322 /* TODO: purge use of name, due to issue with renameing */
323 if_indextoname(if_index, p->name);
324
325 if (p->is_bridge) {
326 INFO("Add bridge %s", p->name);
327 /* Init slave list */
328 p->port_list = NULL;
329
330 p->do_stp = 0;
331 p->up = 0;
332 p->stp_up = 0;
333 p->stp = NULL;
334 update_bridge_stp_config(p, &default_bridge_stp_cfg);
335 ADD_TO_LIST(br_head, bridge_next, p); /* Add to bridge list */
336 } else {
337 INFO("Add iface %s to bridge %s", p->name, br->name);
338 p->up = 0;
339 p->speed = 0;
340 p->duplex = 0;
341 p->master = br;
342
343 update_port_stp_config(p, &default_port_stp_cfg);
344 ADD_TO_LIST(br->port_list, port_next, p); /* Add to bridge port list */
345
346 if (br->stp_up) {
347 add_port_stp(p);
348 }
349 }
350
351 /* Add to interface list */
352 ADD_TO_LIST(if_head, next, p);
353
354 return p;
355 }
356
357 void delete_if(struct ifdata *ifc)
358 {
359 INFO("Delete iface %s", ifc->name);
360 if (ifc->is_bridge) { /* Bridge: */
361 /* Stop STP */
362 clear_bridge_stp(ifc);
363 /* Delete ports */
364 while (ifc->port_list)
365 delete_if(ifc->port_list);
366 /* Remove from bridge list */
367 REMOVE_FROM_LIST(br_head, bridge_next, ifc,
368 "Can't find interface ifindex %d bridge list",
369 ifc->if_index);
370 } else { /* Port */
371 if (ifc->master->stp_up)
372 remove_port_stp(ifc);
373 /* Remove from bridge port list */
374 REMOVE_FROM_LIST(ifc->master->port_list, port_next, ifc,
375 "Can't find interface ifindex %d on br %d's port list",
376 ifc->if_index, ifc->master->if_index);
377 }
378
379 /* Remove from bridge interface list */
380 REMOVE_FROM_LIST(if_head, next, ifc,
381 "Can't find interface ifindex %d on iflist",
382 ifc->if_index);
383 free(ifc);
384 }
385
386 static int stp_enabled(struct ifdata *br)
387 {
388 char path[40 + IFNAMSIZ];
389 sprintf(path, "/sys/class/net/%s/bridge/stp_state", br->name);
390 FILE *f = fopen(path, "r");
391 if (!f) {
392 LOG("Open %s failed", path);
393 return 0;
394 }
395 int enabled = 0;
396 fscanf(f, "%d", &enabled);
397 fclose(f);
398 INFO("STP on %s state %d", br->name, enabled);
399
400 return enabled == 2; /* ie user mode STP */
401 }
402
403 void set_br_up(struct ifdata *br, int up)
404 {
405 int stp_up = stp_enabled(br);
406 INFO("%s was %s stp was %s", br->name,up ? "up" : "down", br->stp_up ? "up" : "down");
407 INFO("Set bridge %s %s stp %s" , br->name,
408 up ? "up" : "down", stp_up ? "up" : "down");
409
410 if (up != br->up)
411 br->up = up;
412
413 if (br->stp_up != stp_up) {
414 if (stp_up)
415 init_bridge_stp(br);
416 else
417 clear_bridge_stp(br);
418 }
419 }
420
421 void set_if_up(struct ifdata *ifc, int up)
422 {
423 INFO("Port %s : %s", ifc->name, (up ? "up" : "down"));
424 int speed = -1;
425 int duplex = -1;
426 int notify_flags = 0;
427 const int NOTIFY_UP = 1, NOTIFY_SPEED = 2, NOTIFY_DUPLEX = 4;
428 if (!up) { /* Down */
429 if (ifc->up) {
430 ifc->up = up;
431 notify_flags |= NOTIFY_UP;
432 }
433 } else { /* Up */
434 int r = ethtool_get_speed_duplex(ifc->name, &speed, &duplex);
435 if (r < 0) { /* Didn't succeed */
436 }
437 if (speed < 0)
438 speed = 10;
439 if (duplex < 0)
440 duplex = 0; /* Assume half duplex */
441
442 if (speed != ifc->speed) {
443 ifc->speed = speed;
444 notify_flags |= NOTIFY_SPEED;
445 }
446 if (duplex != ifc->duplex) {
447 ifc->duplex = duplex;
448 notify_flags |= NOTIFY_DUPLEX;
449 }
450 if (!ifc->up) {
451 ifc->up = 1;
452 notify_flags |= NOTIFY_UP;
453 }
454 }
455 if (notify_flags && ifc->master->stp_up) {
456 instance_begin(ifc->master);
457
458 if (notify_flags & NOTIFY_SPEED)
459 STP_IN_changed_port_speed(ifc->port_index, speed);
460 if (notify_flags & NOTIFY_DUPLEX)
461 STP_IN_changed_port_duplex(ifc->port_index);
462 if (notify_flags & NOTIFY_UP)
463 STP_IN_enable_port(ifc->port_index, ifc->up);
464
465 instance_end();
466 }
467 }
468
469 /*------------------------------------------------------------*/
470
471 int bridge_notify(int br_index, int if_index, int newlink, int up)
472 {
473 if (up)
474 up = 1;
475 LOG("br_index %d, if_index %d, up %d", br_index, if_index, up);
476
477 struct ifdata *br = NULL;
478 if (br_index >= 0) {
479 br = find_if(br_index);
480 if (br && !br->is_bridge) {
481 ERROR
482 ("Notification shows non bridge interface %d as bridge.",
483 br_index);
484 return -1;
485 }
486 if (!br)
487 br = create_if(br_index, NULL);
488 if (!br) {
489 ERROR("Couldn't create data for bridge interface %d",
490 br_index);
491 return -1;
492 }
493 /* Bridge must be up if we get such notifications */
494 if (!br->up)
495 set_br_up(br, 1);
496 }
497
498 struct ifdata *ifc = find_if(if_index);
499
500 if (br) {
501 if (ifc) {
502 if (ifc->is_bridge) {
503 ERROR
504 ("Notification shows bridge interface %d as slave of %d",
505 if_index, br_index);
506 return -1;
507 }
508 if (ifc->master != br) {
509 INFO("Device %d has come to bridge %d. "
510 "Missed notify for deletion from bridge %d",
511 if_index, br_index, ifc->master->if_index);
512 delete_if(ifc);
513 ifc = NULL;
514 }
515 }
516 if (!ifc)
517 ifc = create_if(if_index, br);
518 if (!ifc) {
519 ERROR
520 ("Couldn't create data for interface %d (master %d)",
521 if_index, br_index);
522 return -1;
523 }
524 if (!newlink && !is_bridge_slave(br->name, ifc->name)) {
525 /* brctl delif generates a DELLINK, but so does ifconfig <slave> down.
526 So check and delete if it has been removed.
527 */
528 delete_if(ifc);
529 return 0;
530 }
531 if (ifc->up != up)
532 set_if_up(ifc, up); /* And speed and duplex */
533 } else { /* No br_index */
534 if (!newlink) {
535 /* DELLINK not from bridge means interface unregistered. */
536 /* Cleanup removed bridge or removed bridge slave */
537 if (ifc)
538 delete_if(ifc);
539 return 0;
540 } else { /* This may be a new link */
541 if (!ifc) {
542 char ifname[IFNAMSIZ];
543 if (if_indextoname(if_index, ifname)
544 && is_bridge(ifname)) {
545 ifc = create_if(if_index, NULL);
546 if (!ifc) {
547 ERROR
548 ("Couldn't create data for bridge interface %d",
549 if_index);
550 return -1;
551 }
552 }
553 }
554 if (ifc && !ifc->is_bridge &&
555 !is_bridge_slave(ifc->master->name, ifc->name)) {
556 /* Interface might have left bridge and we might have missed deletion */
557 delete_if(ifc);
558 return 0;
559 }
560 if (ifc && ifc->up != up) {
561 if (ifc->is_bridge)
562 set_br_up(ifc, up);
563 else
564 set_if_up(ifc, up);
565 }
566 }
567 }
568 return 0;
569 }
570
571 void bridge_bpdu_rcv(int if_index, const unsigned char *data, int len)
572 {
573 struct ifdata *ifc = find_if(if_index);
574 BPDU_T *bpdu = (BPDU_T *) (data + sizeof(MAC_HEADER_T));
575
576 LOG("ifindex %d, len %d", if_index, len);
577 if (!ifc)
578 return;
579
580 TST(ifc->up,);
581 TST(ifc->master->stp_up,);
582 TST(len > sizeof(MAC_HEADER_T) + sizeof(ETH_HEADER_T) + sizeof(BPDU_HEADER_T),);
583
584 /* Do some validation */
585 if (bpdu->hdr.protocol[0] || bpdu->hdr.protocol[1])
586 return;
587
588 switch (bpdu->hdr.bpdu_type) {
589 case BPDU_RSTP:
590 TST(len >= 36,);
591 case BPDU_CONFIG_TYPE:
592 TST(len >= 35,);
593 /* 802.1w doesn't ask for this */
594 // TST(ntohs(*(uint16_t*)bpdu.body.message_age)
595 // < ntohs(*(uint16_t*)bpdu.body.max_age), );
596 TST(memcmp(bpdu->body.bridge_id, &ifc->master->bridge_id, 8) != 0
597 || (ntohs(*(uint16_t *) bpdu->body.port_id) & 0xfff) !=
598 ifc->port_index,);
599 break;
600 case BPDU_TOPO_CHANGE_TYPE:
601 break;
602 default:
603 LOG("Receive unknown bpdu type %x", bpdu->hdr.bpdu_type);
604 return;
605 }
606
607 // dump_hex(data, len);
608 instance_begin(ifc->master);
609 int r = STP_IN_rx_bpdu(0, ifc->port_index, bpdu, len);
610 if (r)
611 ERROR("STP_IN_rx_bpdu on port %s returned %s", ifc->name,
612 STP_IN_get_error_explanation(r));
613 instance_end();
614 }
615
616 void bridge_one_second(void)
617 {
618 // LOG("");
619 struct ifdata *br;
620 for (br = br_head; br; br = br->bridge_next) {
621 if (br->stp_up) {
622 instance_begin(br);
623 STP_IN_one_second();
624 instance_end();
625 }
626 }
627
628 /* To get information about port changes when bridge is down */
629 /* But won't work so well since we will not sense deletions */
630 static int count = 0;
631 count++;
632 if (count % 60 == 0)
633 bridge_get_configuration();
634
635 }
636
637 /* Implementing STP_OUT functions */
638
639 int flush_port(char *sys_name)
640 {
641 FILE *f = fopen(sys_name, "w");
642 TSTM(f, -1, "Couldn't open flush file %s for write.", sys_name);
643 int r = fwrite("1", 1, 1, f);
644 fclose(f);
645 TST(r == 1, -1);
646 return 0;
647 }
648
649 int
650 STP_OUT_flush_lt(IN int port_index, IN int vlan_id,
651 IN LT_FLASH_TYPE_T type, IN char *reason)
652 {
653 LOG("port index %d, flash type %d, reason %s", port_index, type,
654 reason);
655 TST(vlan_id == 0, 0);
656
657 char fname[128];
658 if (port_index == 0) { /* i.e. passed port_index was 0 */
659 sprintf(fname, "/sys/class/net/%s/bridge/flush",
660 current_br->name);
661 flush_port(fname);
662 } else if (type == LT_FLASH_ONLY_THE_PORT) {
663 struct ifdata *port = find_port(port_index);
664 TST(port != NULL, 0);
665 sprintf(fname, "/sys/class/net/%s/brif/%s/flush",
666 current_br->name, port->name);
667 flush_port(fname);
668 } else if (type == LT_FLASH_ALL_PORTS_EXCLUDE_THIS) {
669 struct ifdata *port;
670 for (port = current_br->port_list; port; port = port->port_next) {
671 if (port->port_index != port_index) {
672 sprintf(fname,
673 "/sys/class/net/%s/brif/%s/flush",
674 current_br->name, port->name);
675 flush_port(fname);
676 }
677 }
678 } else
679 TST(0, 0);
680
681 return 0;
682 }
683
684 void /* for bridge id calculation */ STP_OUT_get_port_mac(IN int port_index,
685 OUT unsigned char
686 *mac)
687 {
688 LOG("port index %d", port_index);
689 struct ifdata *port = find_port(port_index);
690 TST(port != NULL,);
691 get_hwaddr(port->name, mac);
692 }
693
694 unsigned long STP_OUT_get_port_oper_speed(IN unsigned int port_index)
695 {
696 LOG("port index %d", port_index);
697 struct ifdata *port = find_port(port_index);
698 TST(port != NULL, 0);
699 LOG("Speed: %d", port->speed);
700 return port->speed;
701 }
702
703 int /* 1- Up, 0- Down */ STP_OUT_get_port_link_status(IN int port_index)
704 {
705 LOG("port index %d", port_index);
706 struct ifdata *port = find_port(port_index);
707 TST(port != NULL, 0);
708 LOG("Link status: %d", port->up);
709 return port->up;
710 }
711
712 int /* 1- Full, 0- Half */ STP_OUT_get_duplex(IN int port_index)
713 {
714 LOG("port index %d", port_index);
715 struct ifdata *port = find_port(port_index);
716 TST(port != NULL, 0);
717 LOG("Duplex: %d", port->duplex);
718 return port->duplex;
719 }
720
721 int
722 STP_OUT_set_port_state(IN int port_index, IN int vlan_id,
723 IN RSTP_PORT_STATE state)
724 {
725 LOG("port index %d, state %d", port_index, state);
726 struct ifdata *port = find_port(port_index);
727 TST(port != NULL, 0);
728 TST(vlan_id == 0, 0);
729
730 int br_state;
731 switch (state) {
732 case UID_PORT_DISCARDING:
733 br_state = BR_STATE_BLOCKING;
734 break;
735 case UID_PORT_LEARNING:
736 br_state = BR_STATE_LEARNING;
737 break;
738 case UID_PORT_FORWARDING:
739 br_state = BR_STATE_FORWARDING;
740 break;
741 default:
742 fprintf(stderr, "set_port_state: Unexpected state %d\n", state);
743 return -1;
744 }
745 if (port->up)
746 bridge_set_state(port->if_index, br_state);
747 return 0;
748 }
749
750 int STP_OUT_set_hardware_mode(int vlan_id, UID_STP_MODE_T mode)
751 {
752 LOG("vlan id %d, mode %d", vlan_id, mode);
753 return 0;
754 }
755
756 int
757 STP_OUT_tx_bpdu(IN int port_index, IN int vlan_id,
758 IN unsigned char *bpdu, IN size_t bpdu_len)
759 {
760 LOG("port index %d, len %zd", port_index, bpdu_len);
761 struct ifdata *port = find_port(port_index);
762 TST(port != NULL, 0);
763 TST(vlan_id == 0, 0);
764
765 packet_send(port->if_index, bpdu,
766 bpdu_len + sizeof(ETH_HEADER_T)
767 + sizeof(BPDU_HEADER_T) + sizeof(BPDU_BODY_T));
768 return 0;
769 }
770
771 const char *STP_OUT_get_port_name(IN int port_index)
772 {
773 LOG("port index %d", port_index);
774 struct ifdata *port = find_port(port_index);
775 TST(port != NULL, 0);
776 return port->name;
777 }
778
779 int STP_OUT_get_init_stpm_cfg(IN int vlan_id, INOUT UID_STP_CFG_T * cfg)
780 {
781 LOG("");
782 TST(vlan_id == 0, 0);
783
784 cfg->bridge_priority = current_br->bridge_priority;
785 cfg->max_age = current_br->max_age;
786 cfg->hello_time = current_br->hello_time;
787 cfg->forward_delay = current_br->forward_delay;
788 cfg->force_version = current_br->force_version;
789
790 return 0;
791 }
792
793 int
794 STP_OUT_get_init_port_cfg(IN int vlan_id,
795 IN int port_index, INOUT UID_STP_PORT_CFG_T * cfg)
796 {
797 LOG("port index %d", port_index);
798 struct ifdata *port = find_port(port_index);
799 TST(port != NULL, 0);
800 TST(vlan_id == 0, 0);
801
802 cfg->port_priority = port->port_priority;
803 cfg->admin_non_stp = port->admin_non_stp;
804 cfg->admin_edge = port->admin_edge;
805 cfg->admin_port_path_cost = port->admin_port_path_cost;
806 cfg->admin_point2point = port->admin_point2point;
807
808 return 0;
809 }
810
811 extern void stp_trace(const char *fmt, ...)
812 {
813 va_list ap;
814 va_start(ap, fmt);
815 vDprintf(LOG_LEVEL_RSTPLIB, fmt, ap);
816 va_end(ap);
817 }
818
819 /* Commands and status */
820 #include "ctl_functions.h"
821
822 #define CTL_CHECK_BRIDGE \
823 struct ifdata *br = find_if(br_index); \
824 if (br == NULL || !br->is_bridge) return Err_Interface_not_a_bridge; \
825 if (!br->do_stp) return Err_Bridge_RSTP_not_enabled; \
826 if (!br->stp_up) return Err_Bridge_is_down; \
827 do { } while (0)
828
829 #define CTL_CHECK_BRIDGE_PORT \
830 CTL_CHECK_BRIDGE; \
831 struct ifdata *port = find_if(port_index); \
832 if (port == NULL || port->is_bridge || port->master != br) \
833 return Err_Port_does_not_belong_to_bridge; \
834 do { } while (0)
835
836 int CTL_enable_bridge_rstp(int br_index, int enable)
837 {
838 INFO("bridge %d, enable %d", br_index, enable);
839 int r = 0;
840 if (enable)
841 enable = 1;
842 struct ifdata *br = find_if(br_index);
843 if (br == NULL) {
844 char ifname[IFNAMSIZ];
845 if (if_indextoname(br_index, ifname) && is_bridge(ifname))
846 br = create_if(br_index, NULL);
847 }
848 if (br == NULL || !br->is_bridge)
849 return Err_Interface_not_a_bridge;
850 if (br->do_stp != enable) {
851 br->do_stp = enable;
852 if (br->up)
853 r = enable ? init_bridge_stp(br)
854 : (clear_bridge_stp(br), 0);
855 }
856 return r;
857 }
858
859 int CTL_get_bridge_state(int br_index,
860 UID_STP_CFG_T * cfg, UID_STP_STATE_T * state)
861 {
862 LOG("bridge %d", br_index);
863 CTL_CHECK_BRIDGE;
864 int r;
865 instance_begin(br);
866 r = STP_IN_stpm_get_state(0, state);
867 if (r) {
868 ERROR("Error getting bridge state for %d: %s", br_index,
869 STP_IN_get_error_explanation(r));
870 instance_end();
871 return r;
872 }
873 r = STP_IN_stpm_get_cfg(0, cfg);
874 if (r) {
875 ERROR("Error getting bridge config for %d: %s", br_index,
876 STP_IN_get_error_explanation(r));
877 instance_end();
878 return r;
879 }
880 instance_end();
881 return 0;
882 }
883
884 int CTL_set_bridge_config(int br_index, UID_STP_CFG_T * cfg)
885 {
886 INFO("bridge %d, flags %#lx", br_index, cfg->field_mask);
887 CTL_CHECK_BRIDGE;
888 int r;
889 instance_begin(br);
890 r = STP_IN_stpm_set_cfg(0, NULL, cfg);
891 if (r) {
892 ERROR("Error setting bridge config for %d: %s", br_index,
893 STP_IN_get_error_explanation(r));
894 instance_end();
895 return r;
896 }
897 instance_end();
898 /* Change init config in ifdata so it will be applied if we
899 disable and enable rstp */
900 update_bridge_stp_config(br, cfg);
901 return 0;
902 }
903
904 int CTL_get_port_state(int br_index, int port_index,
905 UID_STP_PORT_CFG_T * cfg, UID_STP_PORT_STATE_T * state)
906 {
907 LOG("bridge %d port %d", br_index, port_index);
908 CTL_CHECK_BRIDGE_PORT;
909 int r;
910 instance_begin(br);
911 state->port_no = port->port_index;
912 r = STP_IN_port_get_state(0, state);
913 if (r) {
914 ERROR("Error getting port state for port %d, bridge %d: %s",
915 port->port_index, br_index,
916 STP_IN_get_error_explanation(r));
917 instance_end();
918 return r;
919 }
920 r = STP_IN_port_get_cfg(0, port->port_index, cfg);
921 if (r) {
922 ERROR("Error getting port config for port %d, bridge %d: %s",
923 port->port_index, br_index,
924 STP_IN_get_error_explanation(r));
925 instance_end();
926 return r;
927 }
928 instance_end();
929 return 0;
930
931 }
932
933 int CTL_set_port_config(int br_index, int port_index, UID_STP_PORT_CFG_T * cfg)
934 {
935 INFO("bridge %d, port %d, flags %#lx", br_index, port_index,
936 cfg->field_mask);
937 CTL_CHECK_BRIDGE_PORT;
938 int r;
939 instance_begin(br);
940 r = STP_IN_set_port_cfg(0, port->port_index, cfg);
941 if (r) {
942 ERROR("Error setting port config for port %d, bridge %d: %s",
943 port->port_index, br_index,
944 STP_IN_get_error_explanation(r));
945 instance_end();
946 return r;
947 }
948 instance_end();
949 /* Change init config in ifdata so it will be applied if we
950 disable and enable rstp */
951 update_port_stp_config(port, cfg);
952 return 0;
953 }
954
955 int CTL_set_debug_level(int level)
956 {
957 INFO("level %d", level);
958 log_level = level;
959 return 0;
960 }
961
962 #undef CTL_CHECK_BRIDGE_PORT
963 #undef CTL_CHECK_BRIDGE