]> git.ipfire.org Git - people/ms/rstp.git/blame - bridge_track.c
Revised packet SOCK_RAW code
[people/ms/rstp.git] / bridge_track.c
CommitLineData
ad02a0eb
SH
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"
f2592588 27#include "packet.h"
ad02a0eb
SH
28
29#include <unistd.h>
30#include <net/if.h>
f2592588 31#include <stdlib.h>
ad02a0eb
SH
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
ad02a0eb
SH
47/*------------------------------------------------------------*/
48
11904a35
SH
49struct 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 */
f2592588
SH
85
86 struct epoll_event_handler event;
ad02a0eb
SH
87};
88
89/* Instances */
90struct ifdata *current_br = NULL;
91
92void instance_begin(struct ifdata *br)
93{
11904a35
SH
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);
ad02a0eb
SH
100}
101
102void instance_end(void)
103{
11904a35
SH
104 STP_IN_instance_end(current_br->stp);
105 current_br = NULL;
ad02a0eb
SH
106}
107
108struct ifdata *find_port(int port_index)
109{
11904a35
SH
110 struct ifdata *ifc = current_br->port_list;
111 while (ifc && ifc->port_index != port_index)
112 ifc = ifc->port_next;
113 return ifc;
ad02a0eb
SH
114}
115
ad02a0eb
SH
116/*************************************************************/
117/* Bridge and port defaults */
118
119UID_STP_CFG_T default_bridge_stp_cfg = {
11904a35
SH
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 */
ad02a0eb
SH
126};
127
11904a35 128void update_bridge_stp_config(struct ifdata *br, UID_STP_CFG_T * cfg)
ad02a0eb 129{
11904a35
SH
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;
ad02a0eb
SH
140}
141
142UID_STP_PORT_CFG_T default_port_stp_cfg = {
11904a35
SH
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,
ad02a0eb
SH
149};
150
11904a35 151void update_port_stp_config(struct ifdata *ifc, UID_STP_PORT_CFG_T * cfg)
ad02a0eb 152{
11904a35
SH
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;
ad02a0eb
SH
163}
164
165/**************************************************************/
166
167int add_port_stp(struct ifdata *ifc)
11904a35 168{ /* Bridge is ifc->master */
11904a35
SH
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;
ad02a0eb
SH
185}
186
187void remove_port_stp(struct ifdata *ifc)
188{
11904a35
SH
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 }
ad02a0eb
SH
199}
200
201int init_rstplib_instance(struct ifdata *br)
202{
11904a35
SH
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 }
ad02a0eb 208
11904a35
SH
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 }
ad02a0eb 219
11904a35 220 return 0;
ad02a0eb
SH
221}
222
223void clear_rstplib_instance(struct ifdata *br)
224{
11904a35
SH
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;
ad02a0eb
SH
235}
236
237int init_bridge_stp(struct ifdata *br)
238{
11904a35
SH
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;
ad02a0eb
SH
265}
266
267void clear_bridge_stp(struct ifdata *br)
268{
11904a35
SH
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);
ad02a0eb
SH
279}
280
ad02a0eb
SH
281struct ifdata *if_head = NULL;
282struct ifdata *br_head = NULL;
283
284struct ifdata *find_if(int if_index)
285{
11904a35
SH
286 struct ifdata *p = if_head;
287 while (p && p->if_index != if_index)
288 p = p->next;
289 return p;
ad02a0eb
SH
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 */
311struct ifdata *create_if(int if_index, struct ifdata *br)
312{
11904a35
SH
313 struct ifdata *p;
314 TST((p = malloc(sizeof(*p))) != NULL, NULL);
315
f2592588
SH
316 memset(p, 0, sizeof(*p));
317
11904a35
SH
318 /* Init fields */
319 p->if_index = if_index;
320 p->is_bridge = (br == NULL);
f2592588
SH
321
322 /* TODO: purge use of name, due to issue with renameing */
11904a35 323 if_indextoname(if_index, p->name);
f2592588 324
11904a35
SH
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;
f2592588 342
11904a35
SH
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 */
f2592588 345
11904a35
SH
346 if (br->stp_up) {
347 add_port_stp(p);
348 }
349 }
f2592588 350
11904a35
SH
351 /* Add to interface list */
352 ADD_TO_LIST(if_head, next, p);
353
354 return p;
ad02a0eb
SH
355}
356
357void delete_if(struct ifdata *ifc)
358{
11904a35
SH
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 }
f2592588 378
11904a35
SH
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);
f2592588 383 free(ifc);
ad02a0eb
SH
384}
385
386void set_br_up(struct ifdata *br, int up)
387{
11904a35
SH
388 if (up != br->up) {
389 br->up = up;
390 if (br->do_stp)
391 up ? (void)init_bridge_stp(br) : clear_bridge_stp(br);
392 }
ad02a0eb
SH
393}
394
395void set_if_up(struct ifdata *ifc, int up)
396{
11904a35
SH
397 INFO("Port %s : %s", ifc->name, (up ? "up" : "down"));
398 int speed = -1;
399 int duplex = -1;
400 int notify_flags = 0;
401 const int NOTIFY_UP = 1, NOTIFY_SPEED = 2, NOTIFY_DUPLEX = 4;
402 if (!up) { /* Down */
403 if (ifc->up) {
404 ifc->up = up;
405 notify_flags |= NOTIFY_UP;
406 }
407 } else { /* Up */
408 int r = ethtool_get_speed_duplex(ifc->name, &speed, &duplex);
409 if (r < 0) { /* Didn't succeed */
410 }
411 if (speed < 0)
412 speed = 10;
413 if (duplex < 0)
414 duplex = 0; /* Assume half duplex */
415
416 if (speed != ifc->speed) {
417 ifc->speed = speed;
418 notify_flags |= NOTIFY_SPEED;
419 }
420 if (duplex != ifc->duplex) {
421 ifc->duplex = duplex;
422 notify_flags |= NOTIFY_DUPLEX;
423 }
424 if (!ifc->up) {
425 ifc->up = 1;
426 notify_flags |= NOTIFY_UP;
427 }
428 }
429 if (notify_flags && ifc->master->stp_up) {
430 instance_begin(ifc->master);
431
432 if (notify_flags & NOTIFY_SPEED)
433 STP_IN_changed_port_speed(ifc->port_index, speed);
434 if (notify_flags & NOTIFY_DUPLEX)
435 STP_IN_changed_port_duplex(ifc->port_index);
436 if (notify_flags & NOTIFY_UP)
437 STP_IN_enable_port(ifc->port_index, ifc->up);
438
439 instance_end();
440 }
ad02a0eb
SH
441}
442
443/*------------------------------------------------------------*/
444
445int bridge_notify(int br_index, int if_index, int newlink, int up)
446{
11904a35
SH
447 if (up)
448 up = 1;
449 LOG("br_index %d, if_index %d, up %d", br_index, if_index, up);
450
451 struct ifdata *br = NULL;
452 if (br_index >= 0) {
453 br = find_if(br_index);
454 if (br && !br->is_bridge) {
455 ERROR
456 ("Notification shows non bridge interface %d as bridge.",
457 br_index);
458 return -1;
459 }
460 if (!br)
461 br = create_if(br_index, NULL);
462 if (!br) {
463 ERROR("Couldn't create data for bridge interface %d",
464 br_index);
465 return -1;
466 }
467 /* Bridge must be up if we get such notifications */
468 if (!br->up)
469 set_br_up(br, 1);
470 }
471
472 struct ifdata *ifc = find_if(if_index);
473
474 if (br) {
475 if (ifc) {
476 if (ifc->is_bridge) {
477 ERROR
478 ("Notification shows bridge interface %d as slave of %d",
479 if_index, br_index);
480 return -1;
481 }
482 if (ifc->master != br) {
483 INFO("Device %d has come to bridge %d. "
484 "Missed notify for deletion from bridge %d",
485 if_index, br_index, ifc->master->if_index);
486 delete_if(ifc);
487 ifc = NULL;
488 }
489 }
490 if (!ifc)
491 ifc = create_if(if_index, br);
492 if (!ifc) {
493 ERROR
494 ("Couldn't create data for interface %d (master %d)",
495 if_index, br_index);
496 return -1;
497 }
498 if (!newlink && !is_bridge_slave(br->name, ifc->name)) {
499 /* brctl delif generates a DELLINK, but so does ifconfig <slave> down.
500 So check and delete if it has been removed.
501 */
502 delete_if(ifc);
503 return 0;
504 }
505 if (ifc->up != up)
506 set_if_up(ifc, up); /* And speed and duplex */
507 } else { /* No br_index */
508 if (!newlink) {
509 /* DELLINK not from bridge means interface unregistered. */
510 /* Cleanup removed bridge or removed bridge slave */
511 if (ifc)
512 delete_if(ifc);
513 return 0;
514 } else { /* This may be a new link */
515 if (!ifc) {
516 char ifname[IFNAMSIZ];
517 if (if_indextoname(if_index, ifname)
518 && is_bridge(ifname)) {
519 ifc = create_if(if_index, NULL);
520 if (!ifc) {
521 ERROR
522 ("Couldn't create data for bridge interface %d",
523 if_index);
524 return -1;
525 }
526 }
527 }
528 if (ifc && !ifc->is_bridge &&
529 !is_bridge_slave(ifc->master->name, ifc->name)) {
530 /* Interface might have left bridge and we might have missed deletion */
531 delete_if(ifc);
532 return 0;
533 }
534 if (ifc && ifc->up != up) {
535 if (ifc->is_bridge)
536 set_br_up(ifc, up);
537 else
538 set_if_up(ifc, up);
539 }
540 }
541 }
542 return 0;
ad02a0eb
SH
543}
544
96e20123 545void bridge_bpdu_rcv(int if_index, const unsigned char *data, int len)
ad02a0eb 546{
96e20123 547 struct ifdata *ifc = find_if(if_index);
11904a35 548 BPDU_T bpdu;
f2592588 549
96e20123
SH
550 LOG("ifindex %d, len %d", if_index, len);
551 if (!ifc)
552 return;
553
554 TST(ifc->up && ifc->master->stp_up,);
555
11904a35
SH
556 memset(&bpdu.eth, 0, sizeof(bpdu.eth));
557 if (len > sizeof(bpdu) - sizeof(bpdu.eth))
558 len = sizeof(bpdu) - sizeof(bpdu.eth);
559 memcpy(&bpdu.hdr, data, len);
f2592588 560
11904a35
SH
561 /* Do some validation */
562 TST(len >= 4,);
563 TST(bpdu.hdr.protocol[0] == 0 && bpdu.hdr.protocol[1] == 0,);
564 switch (bpdu.hdr.bpdu_type) {
565 case BPDU_RSTP:
566 TST(len >= 36,);
567 case BPDU_CONFIG_TYPE:
568 TST(len >= 35,);
569 /* 802.1w doesn't ask for this */
570 // TST(ntohs(*(uint16_t*)bpdu.body.message_age)
571 // < ntohs(*(uint16_t*)bpdu.body.max_age), );
572 TST(memcmp(bpdu.body.bridge_id, &ifc->master->bridge_id, 8) != 0
573 || (ntohs(*(uint16_t *) bpdu.body.port_id) & 0xfff) !=
574 ifc->port_index,);
575 break;
576 case BPDU_TOPO_CHANGE_TYPE:
577 break;
578 default:
579 TST(0,);
580 }
581
582 // dump_hex(data, len);
583 instance_begin(ifc->master);
584 int r =
585 STP_IN_rx_bpdu(0, ifc->port_index, &bpdu, len + sizeof(bpdu.eth));
586 if (r)
587 ERROR("STP_IN_rx_bpdu on port %s returned %s", ifc->name,
588 STP_IN_get_error_explanation(r));
589 instance_end();
ad02a0eb
SH
590}
591
592void bridge_one_second(void)
593{
11904a35
SH
594 // LOG("");
595 struct ifdata *br;
596 for (br = br_head; br; br = br->bridge_next) {
597 if (br->stp_up) {
598 instance_begin(br);
599 STP_IN_one_second();
600 instance_end();
601 }
602 }
603
604 /* To get information about port changes when bridge is down */
605 /* But won't work so well since we will not sense deletions */
606 static int count = 0;
607 count++;
608 if (count % 60 == 0)
609 bridge_get_configuration();
ad02a0eb
SH
610
611}
612
613/* Implementing STP_OUT functions */
614
615int flush_port(char *sys_name)
616{
11904a35
SH
617 FILE *f = fopen(sys_name, "w");
618 TSTM(f, -1, "Couldn't open flush file %s for write.", sys_name);
619 int r = fwrite("1", 1, 1, f);
620 fclose(f);
621 TST(r == 1, -1);
622 return 0;
ad02a0eb
SH
623}
624
625int
11904a35
SH
626STP_OUT_flush_lt(IN int port_index, IN int vlan_id,
627 IN LT_FLASH_TYPE_T type, IN char *reason)
628{
629 LOG("port index %d, flash type %d, reason %s", port_index, type,
630 reason);
631 TST(vlan_id == 0, 0);
632
633 char fname[128];
634 if (port_index == 0) { /* i.e. passed port_index was 0 */
635 sprintf(fname, "/sys/class/net/%s/bridge/flush",
636 current_br->name);
637 flush_port(fname);
638 } else if (type == LT_FLASH_ONLY_THE_PORT) {
639 struct ifdata *port = find_port(port_index);
640 TST(port != NULL, 0);
641 sprintf(fname, "/sys/class/net/%s/brif/%s/flush",
642 current_br->name, port->name);
643 flush_port(fname);
644 } else if (type == LT_FLASH_ALL_PORTS_EXCLUDE_THIS) {
645 struct ifdata *port;
646 for (port = current_br->port_list; port; port = port->port_next) {
647 if (port->port_index != port_index) {
648 sprintf(fname,
649 "/sys/class/net/%s/brif/%s/flush",
650 current_br->name, port->name);
651 flush_port(fname);
652 }
653 }
654 } else
655 TST(0, 0);
656
657 return 0;
ad02a0eb
SH
658}
659
11904a35
SH
660void /* for bridge id calculation */ STP_OUT_get_port_mac(IN int port_index,
661 OUT unsigned char
662 *mac)
663{
664 LOG("port index %d", port_index);
665 struct ifdata *port = find_port(port_index);
666 TST(port != NULL,);
667 get_hwaddr(port->name, mac);
ad02a0eb
SH
668}
669
11904a35 670unsigned long STP_OUT_get_port_oper_speed(IN unsigned int port_index)
ad02a0eb 671{
11904a35
SH
672 LOG("port index %d", port_index);
673 struct ifdata *port = find_port(port_index);
674 TST(port != NULL, 0);
675 LOG("Speed: %d", port->speed);
676 return port->speed;
ad02a0eb
SH
677}
678
11904a35 679int /* 1- Up, 0- Down */ STP_OUT_get_port_link_status(IN int port_index)
ad02a0eb 680{
11904a35
SH
681 LOG("port index %d", port_index);
682 struct ifdata *port = find_port(port_index);
683 TST(port != NULL, 0);
684 LOG("Link status: %d", port->up);
685 return port->up;
ad02a0eb
SH
686}
687
11904a35 688int /* 1- Full, 0- Half */ STP_OUT_get_duplex(IN int port_index)
ad02a0eb 689{
11904a35
SH
690 LOG("port index %d", port_index);
691 struct ifdata *port = find_port(port_index);
692 TST(port != NULL, 0);
693 LOG("Duplex: %d", port->duplex);
694 return port->duplex;
695}
696
697int
698STP_OUT_set_port_state(IN int port_index, IN int vlan_id,
699 IN RSTP_PORT_STATE state)
700{
701 LOG("port index %d, state %d", port_index, state);
702 struct ifdata *port = find_port(port_index);
703 TST(port != NULL, 0);
704 TST(vlan_id == 0, 0);
705
706 int br_state;
707 switch (state) {
708 case UID_PORT_DISCARDING:
709 br_state = BR_STATE_BLOCKING;
710 break;
711 case UID_PORT_LEARNING:
712 br_state = BR_STATE_LEARNING;
713 break;
714 case UID_PORT_FORWARDING:
715 br_state = BR_STATE_FORWARDING;
716 break;
717 default:
718 fprintf(stderr, "set_port_state: Unexpected state %d\n", state);
719 return -1;
720 }
721 if (port->up)
722 bridge_set_state(port->if_index, br_state);
723 return 0;
724}
725
726int STP_OUT_set_hardware_mode(int vlan_id, UID_STP_MODE_T mode)
727{
728 LOG("vlan id %d, mode %d", vlan_id, mode);
729 return 0;
ad02a0eb
SH
730}
731
732int
11904a35
SH
733STP_OUT_tx_bpdu(IN int port_index, IN int vlan_id,
734 IN unsigned char *bpdu, IN size_t bpdu_len)
ad02a0eb 735{
11904a35
SH
736 LOG("port index %d, len %zd", port_index, bpdu_len);
737 struct ifdata *port = find_port(port_index);
738 TST(port != NULL, 0);
739 TST(vlan_id == 0, 0);
740 // dump_hex(bpdu + sizeof(MAC_HEADER_T) + sizeof(ETH_HEADER_T),
741 // bpdu_len - (sizeof(MAC_HEADER_T) + sizeof(ETH_HEADER_T)));
96e20123
SH
742 packet_send(port->if_index,
743 bpdu + sizeof(MAC_HEADER_T) + sizeof(ETH_HEADER_T),
744 bpdu_len); // The length we get excludes headers!
11904a35
SH
745 return 0;
746}
ad02a0eb 747
11904a35
SH
748const char *STP_OUT_get_port_name(IN int port_index)
749{
750 LOG("port index %d", port_index);
751 struct ifdata *port = find_port(port_index);
752 TST(port != NULL, 0);
753 return port->name;
754}
ad02a0eb 755
11904a35
SH
756int STP_OUT_get_init_stpm_cfg(IN int vlan_id, INOUT UID_STP_CFG_T * cfg)
757{
758 LOG("");
759 TST(vlan_id == 0, 0);
760
761 cfg->bridge_priority = current_br->bridge_priority;
762 cfg->max_age = current_br->max_age;
763 cfg->hello_time = current_br->hello_time;
764 cfg->forward_delay = current_br->forward_delay;
765 cfg->force_version = current_br->force_version;
766
767 return 0;
ad02a0eb
SH
768}
769
770int
11904a35
SH
771STP_OUT_get_init_port_cfg(IN int vlan_id,
772 IN int port_index, INOUT UID_STP_PORT_CFG_T * cfg)
ad02a0eb 773{
11904a35
SH
774 LOG("port index %d", port_index);
775 struct ifdata *port = find_port(port_index);
776 TST(port != NULL, 0);
777 TST(vlan_id == 0, 0);
ad02a0eb 778
11904a35
SH
779 cfg->port_priority = port->port_priority;
780 cfg->admin_non_stp = port->admin_non_stp;
781 cfg->admin_edge = port->admin_edge;
782 cfg->admin_port_path_cost = port->admin_port_path_cost;
783 cfg->admin_point2point = port->admin_point2point;
ad02a0eb 784
11904a35 785 return 0;
ad02a0eb
SH
786}
787
11904a35 788extern void stp_trace(const char *fmt, ...)
ad02a0eb 789{
11904a35
SH
790 va_list ap;
791 va_start(ap, fmt);
792 vDprintf(LOG_LEVEL_RSTPLIB, fmt, ap);
793 va_end(ap);
ad02a0eb
SH
794}
795
796/* Commands and status */
797#include "ctl_functions.h"
798
799#define CTL_CHECK_BRIDGE \
800 struct ifdata *br = find_if(br_index); \
801 if (br == NULL || !br->is_bridge) return Err_Interface_not_a_bridge; \
802 if (!br->do_stp) return Err_Bridge_RSTP_not_enabled; \
803 if (!br->stp_up) return Err_Bridge_is_down; \
804 do { } while (0)
805
806#define CTL_CHECK_BRIDGE_PORT \
807 CTL_CHECK_BRIDGE; \
808 struct ifdata *port = find_if(port_index); \
809 if (port == NULL || port->is_bridge || port->master != br) \
810 return Err_Port_does_not_belong_to_bridge; \
811 do { } while (0)
812
813int CTL_enable_bridge_rstp(int br_index, int enable)
814{
11904a35
SH
815 INFO("bridge %d, enable %d", br_index, enable);
816 int r = 0;
817 if (enable)
818 enable = 1;
819 struct ifdata *br = find_if(br_index);
820 if (br == NULL) {
821 char ifname[IFNAMSIZ];
822 if (if_indextoname(br_index, ifname) && is_bridge(ifname))
823 br = create_if(br_index, NULL);
824 }
825 if (br == NULL || !br->is_bridge)
826 return Err_Interface_not_a_bridge;
827 if (br->do_stp != enable) {
828 br->do_stp = enable;
829 if (br->up)
830 r = enable ? init_bridge_stp(br)
831 : (clear_bridge_stp(br), 0);
832 }
833 return r;
ad02a0eb
SH
834}
835
836int CTL_get_bridge_state(int br_index,
11904a35
SH
837 UID_STP_CFG_T * cfg, UID_STP_STATE_T * state)
838{
839 LOG("bridge %d", br_index);
840 CTL_CHECK_BRIDGE;
841 int r;
842 instance_begin(br);
843 r = STP_IN_stpm_get_state(0, state);
844 if (r) {
845 ERROR("Error getting bridge state for %d: %s", br_index,
846 STP_IN_get_error_explanation(r));
847 instance_end();
848 return r;
849 }
850 r = STP_IN_stpm_get_cfg(0, cfg);
851 if (r) {
852 ERROR("Error getting bridge config for %d: %s", br_index,
853 STP_IN_get_error_explanation(r));
854 instance_end();
855 return r;
856 }
857 instance_end();
858 return 0;
859}
860
861int CTL_set_bridge_config(int br_index, UID_STP_CFG_T * cfg)
862{
863 INFO("bridge %d, flags %#lx", br_index, cfg->field_mask);
864 CTL_CHECK_BRIDGE;
865 int r;
866 instance_begin(br);
867 r = STP_IN_stpm_set_cfg(0, NULL, cfg);
868 if (r) {
869 ERROR("Error setting bridge config for %d: %s", br_index,
870 STP_IN_get_error_explanation(r));
871 instance_end();
872 return r;
873 }
874 instance_end();
875 /* Change init config in ifdata so it will be applied if we
876 disable and enable rstp */
877 update_bridge_stp_config(br, cfg);
878 return 0;
ad02a0eb
SH
879}
880
881int CTL_get_port_state(int br_index, int port_index,
11904a35
SH
882 UID_STP_PORT_CFG_T * cfg, UID_STP_PORT_STATE_T * state)
883{
884 LOG("bridge %d port %d", br_index, port_index);
885 CTL_CHECK_BRIDGE_PORT;
886 int r;
887 instance_begin(br);
888 state->port_no = port->port_index;
889 r = STP_IN_port_get_state(0, state);
890 if (r) {
891 ERROR("Error getting port state for port %d, bridge %d: %s",
892 port->port_index, br_index,
893 STP_IN_get_error_explanation(r));
894 instance_end();
895 return r;
896 }
897 r = STP_IN_port_get_cfg(0, port->port_index, cfg);
898 if (r) {
899 ERROR("Error getting port config for port %d, bridge %d: %s",
900 port->port_index, br_index,
901 STP_IN_get_error_explanation(r));
902 instance_end();
903 return r;
904 }
905 instance_end();
906 return 0;
907
908}
909
910int CTL_set_port_config(int br_index, int port_index, UID_STP_PORT_CFG_T * cfg)
911{
912 INFO("bridge %d, port %d, flags %#lx", br_index, port_index,
913 cfg->field_mask);
914 CTL_CHECK_BRIDGE_PORT;
915 int r;
916 instance_begin(br);
917 r = STP_IN_set_port_cfg(0, port->port_index, cfg);
918 if (r) {
919 ERROR("Error setting port config for port %d, bridge %d: %s",
920 port->port_index, br_index,
921 STP_IN_get_error_explanation(r));
922 instance_end();
923 return r;
924 }
925 instance_end();
926 /* Change init config in ifdata so it will be applied if we
927 disable and enable rstp */
928 update_port_stp_config(port, cfg);
929 return 0;
ad02a0eb
SH
930}
931
932int CTL_set_debug_level(int level)
933{
11904a35
SH
934 INFO("level %d", level);
935 log_level = level;
936 return 0;
ad02a0eb
SH
937}
938
ad02a0eb
SH
939#undef CTL_CHECK_BRIDGE_PORT
940#undef CTL_CHECK_BRIDGE