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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2016 BISDN GmbH. All rights reserved.
***/
#include <netinet/in.h>
#include <linux/if_bridge.h>
#include <stdbool.h>
#include "alloc-util.h"
#include "conf-parser.h"
#include "netlink-util.h"
#include "networkd-bridge-vlan.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "parse-util.h"
#include "vlan-util.h"
static bool is_bit_set(unsigned bit, uint32_t scope) {
assert(bit < sizeof(scope)*8);
return scope & (UINT32_C(1) << bit);
}
static void set_bit(unsigned nr, uint32_t *addr) {
if (nr < BRIDGE_VLAN_BITMAP_MAX)
addr[nr / 32] |= (UINT32_C(1) << (nr % 32));
}
static int find_next_bit(int i, uint32_t x) {
int j;
if (i >= 32)
return -1;
/* find first bit */
if (i < 0)
return BUILTIN_FFS_U32(x);
/* mask off prior finds to get next */
j = __builtin_ffs(x >> i);
return j ? j + i : 0;
}
int bridge_vlan_append_info(
const Link *link,
sd_netlink_message *req,
uint16_t pvid,
const uint32_t *br_vid_bitmap,
const uint32_t *br_untagged_bitmap) {
struct bridge_vlan_info br_vlan;
bool done, untagged = false;
uint16_t begin, end;
int r, cnt;
assert(link);
assert(req);
assert(br_vid_bitmap);
assert(br_untagged_bitmap);
cnt = 0;
begin = end = UINT16_MAX;
for (int k = 0; k < BRIDGE_VLAN_BITMAP_LEN; k++) {
uint32_t untagged_map = br_untagged_bitmap[k];
uint32_t vid_map = br_vid_bitmap[k];
unsigned base_bit;
int i;
base_bit = k * 32;
i = -1;
done = false;
do {
int j = find_next_bit(i, vid_map);
if (j > 0) {
/* first hit of any bit */
if (begin == UINT16_MAX && end == UINT16_MAX) {
begin = end = j - 1 + base_bit;
untagged = is_bit_set(j - 1, untagged_map);
goto next;
}
/* this bit is a continuation of prior bits */
if (j - 2 + base_bit == end && untagged == is_bit_set(j - 1, untagged_map) && (uint16_t)j - 1 + base_bit != pvid && (uint16_t)begin != pvid) {
end++;
goto next;
}
} else
done = true;
if (begin != UINT16_MAX) {
cnt++;
if (done && k < BRIDGE_VLAN_BITMAP_LEN - 1)
break;
br_vlan.flags = 0;
if (untagged)
br_vlan.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
if (begin == end) {
br_vlan.vid = begin;
if (begin == pvid)
br_vlan.flags |= BRIDGE_VLAN_INFO_PVID;
r = sd_netlink_message_append_data(req, IFLA_BRIDGE_VLAN_INFO, &br_vlan, sizeof(br_vlan));
if (r < 0)
return log_link_error_errno(link, r, "Could not append IFLA_BRIDGE_VLAN_INFO attribute: %m");
} else {
br_vlan.vid = begin;
br_vlan.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
r = sd_netlink_message_append_data(req, IFLA_BRIDGE_VLAN_INFO, &br_vlan, sizeof(br_vlan));
if (r < 0)
return log_link_error_errno(link, r, "Could not append IFLA_BRIDGE_VLAN_INFO attribute: %m");
br_vlan.vid = end;
br_vlan.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
br_vlan.flags |= BRIDGE_VLAN_INFO_RANGE_END;
r = sd_netlink_message_append_data(req, IFLA_BRIDGE_VLAN_INFO, &br_vlan, sizeof(br_vlan));
if (r < 0)
return log_link_error_errno(link, r, "Could not append IFLA_BRIDGE_VLAN_INFO attribute: %m");
}
if (done)
break;
}
if (j > 0) {
begin = end = j - 1 + base_bit;
untagged = is_bit_set(j - 1, untagged_map);
}
next:
i = j;
} while (!done);
}
assert(cnt > 0);
return cnt;
}
void network_adjust_bridge_vlan(Network *network) {
assert(network);
if (!network->use_br_vlan)
return;
/* pvid might not be in br_vid_bitmap yet */
if (network->pvid)
set_bit(network->pvid, network->br_vid_bitmap);
}
int config_parse_brvlan_pvid(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
uint16_t pvid;
int r;
r = parse_vlanid(rvalue, &pvid);
if (r < 0)
return r;
network->pvid = pvid;
network->use_br_vlan = true;
return 0;
}
int config_parse_brvlan_vlan(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
uint16_t vid, vid_end;
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
r = parse_vid_range(rvalue, &vid, &vid_end);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse VLAN, ignoring: %s", rvalue);
return 0;
}
for (; vid <= vid_end; vid++)
set_bit(vid, network->br_vid_bitmap);
network->use_br_vlan = true;
return 0;
}
int config_parse_brvlan_untagged(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Network *network = userdata;
uint16_t vid, vid_end;
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
r = parse_vid_range(rvalue, &vid, &vid_end);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r, "Could not parse VLAN: %s", rvalue);
return 0;
}
for (; vid <= vid_end; vid++) {
set_bit(vid, network->br_vid_bitmap);
set_bit(vid, network->br_untagged_bitmap);
}
network->use_br_vlan = true;
return 0;
}
|