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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* bridged - bridging control daemon. This module provides functions related
* to the librstp (Rapid Spanning Tree Protocol) library.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <syslog.h>
#include <kstat.h>
#include <libdlpi.h>
#include <libdladm.h>
#include <libdllink.h>
#include <libdlstat.h>
#include <stp_in.h>
#include <stp_vectors.h>
#include <net/if_types.h>
#include <net/bridge.h>
#include <sys/ethernet.h>
#include "global.h"
/* current engine configuration; access protected by engine_lock */
static UID_STP_CFG_T uid_cfg;
/*
* Our implementation doesn't have per-VLAN forwarding entries, so we just
* flush by the port. If port number is zero, then flush entries.
*/
/*ARGSUSED1*/
static int
flush_lt(int port_index, int vlan_id, LT_FLASH_TYPE_T type, char *reason)
{
struct portdata *pd;
const char *portname;
bridge_flushfwd_t bff;
if (port_index > nextport || port_index < 0)
return (0);
if (port_index == 0) {
type = LT_FLASH_ONLY_THE_PORT;
portname = "all";
bff.bff_linkid = DATALINK_INVALID_LINKID;
} else {
pd = allports[port_index - 1];
portname = pd->name;
bff.bff_linkid = pd->linkid;
}
if (debugging) {
syslog(LOG_DEBUG, "flush forwarding %s %s: %s",
type == LT_FLASH_ONLY_THE_PORT ? "to" : "except for",
portname, reason);
}
bff.bff_exclude = (type == LT_FLASH_ALL_PORTS_EXCLUDE_THIS);
/*
* If flushing fails, we can't return. The only safe thing to do is to
* tear down the bridge so that we're not harming the network.
*/
if (strioctl(control_fd, BRIOC_FLUSHFWD, &bff, sizeof (bff)) == -1) {
syslog(LOG_ERR, "cannot flush forwarding entries on %s %s: %m",
instance_name, portname);
unlock_engine();
exit(EXIT_FAILURE);
}
return (0);
}
static void
get_port_mac(int port_index, unsigned char *mac)
{
struct portdata *pd;
if (port_index > nextport || port_index <= 0)
return;
pd = allports[port_index - 1];
(void) memcpy(mac, pd->mac_addr, ETHERADDRL);
}
/* Returns speed in megabits per second */
static unsigned long
get_port_oper_speed(unsigned int port_index)
{
if (port_index > nextport || port_index == 0)
return (1000UL);
else
return (allports[port_index - 1]->speed);
}
static int
get_port_link_status(int port_index)
{
struct portdata *pd;
if (port_index > nextport || port_index <= 0) {
return (0);
} else {
pd = allports[port_index - 1];
return (pd->phys_status && pd->admin_status &&
protect == DLADM_BRIDGE_PROT_STP && !pd->sdu_failed ?
1 : 0);
}
}
static int
get_duplex(int port_index)
{
struct portdata *pd;
link_duplex_t link_duplex;
dladm_status_t status;
if (port_index > nextport || port_index <= 0)
return (False);
pd = allports[port_index - 1];
status = dladm_get_single_mac_stat(dlhandle, pd->linkid, "link_duplex",
KSTAT_DATA_UINT32, &link_duplex);
if (status == DLADM_STATUS_OK && link_duplex == LINK_DUPLEX_FULL)
return (True);
else
return (False);
}
static const char *
bls_state(bridge_state_t bstate)
{
switch (bstate) {
case BLS_LEARNING:
return ("learning");
case BLS_FORWARDING:
return ("forwarding");
default:
return ("block/listen");
}
}
/*ARGSUSED1*/
static int
set_port_state(int port_index, int vlan_id, RSTP_PORT_STATE state)
{
struct portdata *pd;
bridge_setstate_t bss;
if (port_index > nextport || port_index <= 0)
return (1);
pd = allports[port_index - 1];
if (debugging)
syslog(LOG_DEBUG, "setting port state on port %d (%s) to %d",
port_index, pd->name, state);
switch (state) {
case UID_PORT_LEARNING:
bss.bss_state = BLS_LEARNING;
break;
case UID_PORT_FORWARDING:
bss.bss_state = BLS_FORWARDING;
break;
default:
bss.bss_state = BLS_BLOCKLISTEN;
break;
}
bss.bss_linkid = pd->linkid;
if (strioctl(control_fd, BRIOC_SETSTATE, &bss, sizeof (bss)) == -1) {
syslog(LOG_ERR, "cannot set STP state on %s from %s to %s: %m",
pd->name, bls_state(pd->state), bls_state(bss.bss_state));
/*
* If we've been unsuccessful in disabling forwarding, then the
* only safe thing to do is to make the daemon exit, so that
* the kernel will be forced to destroy the bridge state and
* terminate all forwarding.
*/
if (pd->state == BLS_FORWARDING &&
bss.bss_state != BLS_FORWARDING) {
unlock_engine();
exit(EXIT_FAILURE);
}
} else {
pd->state = bss.bss_state;
}
return (0);
}
/*
* Our hardware doesn't actually do anything different when STP is enabled or
* disabled, so this function does nothing. It would be possible to open and
* close the DLPI stream here, if such a thing were necessary.
*/
static int
set_hardware_mode(int vlan_id, UID_STP_MODE_T mode)
{
if (debugging)
syslog(LOG_DEBUG, "setting hardware mode on vlan %d to %d",
vlan_id, mode);
return (0);
}
/*ARGSUSED1*/
static int
tx_bpdu(int port_index, int vlan_id, unsigned char *bpdu, size_t bpdu_len)
{
struct portdata *pdp;
int rc;
if (port_index > nextport || port_index <= 0)
return (1);
pdp = allports[port_index - 1];
rc = dlpi_send(pdp->dlpi, NULL, 0, bpdu, bpdu_len, NULL);
if (rc == DLPI_SUCCESS) {
if (debugging)
syslog(LOG_DEBUG, "transmitted %d byte BPDU on %s",
bpdu_len, pdp->name);
return (0);
} else {
syslog(LOG_WARNING, "failed to send to %s: %s", pdp->name,
dlpi_strerror(rc));
return (1);
}
}
static const char *
get_port_name(int port_index)
{
if (port_index > nextport || port_index <= 0)
return ("unknown");
else
return (allports[port_index - 1]->name);
}
/*ARGSUSED*/
static int
get_init_stpm_cfg(int vlan_id, UID_STP_CFG_T *cfg)
{
/* under engine_lock because it's a callback from the engine */
*cfg = uid_cfg;
return (0);
}
/*ARGSUSED*/
static int
get_init_port_cfg(int vlan_id, int port_index, UID_STP_PORT_CFG_T *cfg)
{
struct portdata *pdp;
uint_t propval, valcnt;
datalink_id_t linkid;
dladm_status_t status;
if (port_index > nextport || port_index <= 0)
return (1);
pdp = allports[port_index - 1];
cfg->field_mask = 0;
cfg->port_priority = DEF_PORT_PRIO;
cfg->admin_non_stp = DEF_ADMIN_NON_STP;
cfg->admin_edge = DEF_ADMIN_EDGE;
cfg->admin_port_path_cost = ADMIN_PORT_PATH_COST_AUTO;
cfg->admin_point2point = DEF_P2P;
valcnt = 1;
linkid = pdp->linkid;
status = dladm_get_linkprop_values(dlhandle, linkid,
DLADM_PROP_VAL_PERSISTENT, "stp_priority", &propval, &valcnt);
if (status == DLADM_STATUS_OK) {
cfg->port_priority = propval;
cfg->field_mask |= PT_CFG_PRIO;
}
status = dladm_get_linkprop_values(dlhandle, linkid,
DLADM_PROP_VAL_PERSISTENT, "stp", &propval, &valcnt);
if (status == DLADM_STATUS_OK) {
cfg->admin_non_stp = !propval;
cfg->field_mask |= PT_CFG_NON_STP;
}
status = dladm_get_linkprop_values(dlhandle, linkid,
DLADM_PROP_VAL_PERSISTENT, "stp_edge", &propval, &valcnt);
if (status == DLADM_STATUS_OK) {
cfg->admin_edge = propval;
cfg->field_mask |= PT_CFG_EDGE;
}
status = dladm_get_linkprop_values(dlhandle, linkid,
DLADM_PROP_VAL_PERSISTENT, "stp_cost", &propval, &valcnt);
if (status == DLADM_STATUS_OK) {
cfg->admin_port_path_cost = propval;
cfg->field_mask |= PT_CFG_COST;
}
status = dladm_get_linkprop_values(dlhandle, linkid,
DLADM_PROP_VAL_PERSISTENT, "stp_p2p", &propval, &valcnt);
if (status == DLADM_STATUS_OK) {
cfg->admin_point2point = propval;
cfg->field_mask |= PT_CFG_P2P;
}
/*
* mcheck is special. It is actually a command, but the 802 documents
* define it as a variable that spontaneously resets itself. We need
* to handle that behavior here.
*/
status = dladm_get_linkprop_values(dlhandle, linkid,
DLADM_PROP_VAL_PERSISTENT, "stp_mcheck", &propval, &valcnt);
if (status == DLADM_STATUS_OK && propval != 0) {
char *pval = "0";
cfg->field_mask |= PT_CFG_MCHECK;
(void) dladm_set_linkprop(dlhandle, linkid, "stp_mcheck", &pval,
1, DLADM_OPT_ACTIVE|DLADM_OPT_PERSIST|DLADM_OPT_NOREFRESH);
}
pdp->admin_non_stp = cfg->admin_non_stp;
if (!pdp->admin_non_stp)
pdp->bpdu_protect = B_FALSE;
return (0);
}
static void
trace(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsyslog(LOG_DEBUG, fmt, ap);
va_end(ap);
}
static STP_VECTORS_T stp_vectors = {
flush_lt,
get_port_mac,
get_port_oper_speed,
get_port_link_status,
get_duplex,
set_port_state,
set_hardware_mode,
tx_bpdu,
get_port_name,
get_init_stpm_cfg,
get_init_port_cfg,
trace
};
void
rstp_init(void)
{
dladm_status_t status;
char buf[DLADM_STRSIZE];
STP_IN_init(&stp_vectors);
status = dladm_bridge_get_properties(instance_name, &uid_cfg, &protect);
if (status != DLADM_STATUS_OK) {
syslog(LOG_ERR, "%s: unable to read properties: %s",
instance_name, dladm_status2str(status, buf));
exit(EXIT_FAILURE);
}
}
/*
* This is called by a normal refresh operation. It gets the engine properties
* and resets.
*/
void
rstp_refresh(void)
{
dladm_status_t status;
int rc;
char buf[DLADM_STRSIZE];
UID_STP_CFG_T new_cfg;
dladm_bridge_prot_t new_prot;
status = dladm_bridge_get_properties(instance_name, &new_cfg,
&new_prot);
if (status != DLADM_STATUS_OK) {
syslog(LOG_ERR, "%s: unable to refresh bridge properties: %s",
instance_name, dladm_status2str(status, buf));
} else {
if (debugging && (protect != new_prot ||
uid_cfg.stp_enabled != new_cfg.stp_enabled)) {
syslog(LOG_DEBUG, "loop protection %s->%s, STP %d->%d",
dladm_bridge_prot2str(protect),
dladm_bridge_prot2str(new_prot),
uid_cfg.stp_enabled, new_cfg.stp_enabled);
}
/*
* The engine doesn't take kindly to parameter changes while
* running. Disable first if we must do this.
*/
if (uid_cfg.stp_enabled &&
memcmp(&uid_cfg, &new_cfg, sizeof (uid_cfg)) != 0) {
syslog(LOG_DEBUG, "resetting state machine");
uid_cfg.stp_enabled = STP_DISABLED;
rc = STP_IN_stpm_set_cfg(0, &uid_cfg);
if (rc != 0)
syslog(LOG_ERR, "STP machine reset config: %s",
STP_IN_get_error_explanation(rc));
}
uid_cfg = new_cfg;
protect = new_prot;
rc = STP_IN_stpm_set_cfg(0, &uid_cfg);
if (rc != 0)
syslog(LOG_ERR, "STP machine set config: %s",
STP_IN_get_error_explanation(rc));
}
}
/*
* This is called when a port changes its MAC address. If it's the main port,
* the one that supplies us our bridge ID, then we must choose a new ID, and to
* do that we shut the bridge down and bring it back up.
*/
void
rstp_change_mac(struct portdata *port, const unsigned char *newaddr)
{
unsigned short prio;
unsigned char mac[ETHERADDRL];
int rc;
char curid[ETHERADDRL * 3];
char newmac[ETHERADDRL * 3];
(void) _link_ntoa(port->mac_addr, curid, ETHERADDRL, IFT_OTHER);
(void) _link_ntoa(newaddr, newmac, ETHERADDRL, IFT_OTHER);
STP_IN_get_bridge_id(port->vlan_id, &prio, mac);
if (memcmp(port->mac_addr, mac, ETHERADDRL) == 0) {
syslog(LOG_NOTICE, "bridge ID must change: ID %s on %s changed "
"to %s", curid, port->name, newmac);
uid_cfg.stp_enabled = STP_DISABLED;
if ((rc = STP_IN_stpm_set_cfg(0, &uid_cfg)) != 0)
syslog(LOG_ERR, "STP machine set config: %s",
STP_IN_get_error_explanation(rc));
(void) memcpy(port->mac_addr, newaddr, ETHERADDRL);
uid_cfg.stp_enabled = STP_ENABLED;
if ((rc = STP_IN_stpm_set_cfg(0, &uid_cfg)) != 0)
syslog(LOG_ERR, "STP machine set config: %s",
STP_IN_get_error_explanation(rc));
} else {
syslog(LOG_DEBUG,
"MAC address on %s changed from %s to %s", port->name,
curid, newmac);
(void) memcpy(port->mac_addr, newaddr, ETHERADDRL);
}
}
boolean_t
rstp_add_port(struct portdata *port)
{
int rc;
UID_STP_PORT_CFG_T portcfg;
bridge_vlanenab_t bve;
bridge_setstate_t bss;
if (!port->stp_added &&
(rc = STP_IN_port_add(port->vlan_id, port->port_index)) != 0) {
syslog(LOG_ERR, "STP add %s %d: %s", port->name,
port->port_index, STP_IN_get_error_explanation(rc));
return (B_FALSE);
}
port->stp_added = B_TRUE;
/* guaranteed to succeed at this point */
(void) get_init_port_cfg(port->vlan_id, port->port_index, &portcfg);
/*
* Restore state when reenabling STP engine, set fixed state when
* disabling. For TRILL, we don't control forwarding at all, but we
* need to turn off our controls for TRILL to do its thing.
*/
bss.bss_linkid = port->linkid;
if (protect != DLADM_BRIDGE_PROT_STP) {
bss.bss_state = port->state = BLS_BLOCKLISTEN;
} else if (portcfg.admin_non_stp) {
bss.bss_state = port->admin_status && !port->sdu_failed &&
!port->bpdu_protect ? BLS_FORWARDING : BLS_BLOCKLISTEN;
} else {
bss.bss_state = port->state;
}
if (strioctl(control_fd, BRIOC_SETSTATE, &bss, sizeof (bss)) == -1) {
syslog(LOG_ERR, "cannot set STP state on %s: %m", port->name);
goto failure;
}
rc = STP_IN_enable_port(port->port_index,
port->admin_status && port->phys_status && !port->sdu_failed &&
protect == DLADM_BRIDGE_PROT_STP);
if (rc != 0) {
syslog(LOG_ERR, "STP enable %s %d: %s", port->name,
port->port_index, STP_IN_get_error_explanation(rc));
goto failure;
}
if (debugging) {
rc = STP_IN_dbg_set_port_trace("all", True, 0,
port->port_index);
} else {
/* return to default debug state */
rc = STP_IN_dbg_set_port_trace("all", False, 0,
port->port_index);
if (rc == 0)
rc = STP_IN_dbg_set_port_trace("sttrans", True, 0,
port->port_index);
}
if (rc != 0) {
syslog(LOG_ERR, "STP trace %s %d: %s", port->name,
port->port_index, STP_IN_get_error_explanation(rc));
goto failure;
}
/* Clear out the kernel's allowed VLAN set; second walk will set */
bve.bve_linkid = port->linkid;
bve.bve_vlan = 0;
bve.bve_onoff = B_FALSE;
if (strioctl(control_fd, BRIOC_VLANENAB, &bve, sizeof (bve)) == -1) {
syslog(LOG_ERR, "unable to disable VLANs on %s: %m",
port->name);
goto failure;
}
if ((rc = STP_IN_port_set_cfg(0, port->port_index, &portcfg)) != 0) {
syslog(LOG_ERR, "STP port configure %s %d: %s", port->name,
port->port_index, STP_IN_get_error_explanation(rc));
goto failure;
}
return (B_TRUE);
failure:
(void) STP_IN_port_remove(port->vlan_id, port->port_index);
port->stp_added = B_FALSE;
return (B_FALSE);
}
|