Debian Codegraph @

src/network/networkd-bridge-vlan.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2016 BISDN GmbH. All rights reserved.
4 ***/
5
6 #include <linux/if_bridge.h>
7
8 #include "sd-netlink.h"
9
10 #include "alloc-util.h"
11 #include "netlink-util.h"
12 #include "networkd-bridge-vlan.h"
13 #include "networkd-link.h"
14 #include "networkd-network.h"
15 #include "parse-util.h"
16 #include "string-util.h"
17 #include "vlan-util.h"
18
19 static bool is_bit_set(unsigned nr, const uint32_t *addr) {
20 assert(nr < BRIDGE_VLAN_BITMAP_MAX);
21 return addr[nr / 32] & (UINT32_C(1) << (nr % 32));
22 }
23
24 static void set_bit(unsigned nr, uint32_t *addr) {
25 assert(nr < BRIDGE_VLAN_BITMAP_MAX);
26 addr[nr / 32] |= (UINT32_C(1) << (nr % 32));
27 }
28
29 static int add_single(sd_netlink_message *m, uint16_t id, bool untagged, bool is_pvid, char **str) {
30 assert(m);
31 assert(id < BRIDGE_VLAN_BITMAP_MAX);
32
33 if (DEBUG_LOGGING)
34 (void) strextendf_with_separator(str, ",", "%u%s%s%s%s%s", id,
35 (untagged || is_pvid) ? "(" : "",
36 untagged ? "untagged" : "",
37 (untagged && is_pvid) ? "," : "",
38 is_pvid ? "pvid" : "",
39 (untagged || is_pvid) ? ")" : "");
40
41 return sd_netlink_message_append_data(m, IFLA_BRIDGE_VLAN_INFO,
42 &(struct bridge_vlan_info) {
43 .vid = id,
44 .flags = (untagged ? BRIDGE_VLAN_INFO_UNTAGGED : 0) |
45 (is_pvid ? BRIDGE_VLAN_INFO_PVID : 0),
46 },
47 sizeof(struct bridge_vlan_info));
48 }
49
50 static int add_range(sd_netlink_message *m, uint16_t begin, uint16_t end, bool untagged, char **str) {
51 int r;
52
53 assert(m);
54 assert(begin <= end);
55 assert(end < BRIDGE_VLAN_BITMAP_MAX);
56
57 if (begin == end)
58 return add_single(m, begin, untagged, /* is_pvid= */ false, str);
59
60 if (DEBUG_LOGGING)
61 (void) strextendf_with_separator(str, ",", "%u-%u%s", begin, end, untagged ? "(untagged)" : "");
62
63 r = sd_netlink_message_append_data(m, IFLA_BRIDGE_VLAN_INFO,
64 &(struct bridge_vlan_info) {
65 .vid = begin,
66 .flags = (untagged ? BRIDGE_VLAN_INFO_UNTAGGED : 0) |
67 BRIDGE_VLAN_INFO_RANGE_BEGIN,
68 },
69 sizeof(struct bridge_vlan_info));
70 if (r < 0)
71 return r;
72
73 r = sd_netlink_message_append_data(m, IFLA_BRIDGE_VLAN_INFO,
74 &(struct bridge_vlan_info) {
75 .vid = end,
76 .flags = (untagged ? BRIDGE_VLAN_INFO_UNTAGGED : 0) |
77 BRIDGE_VLAN_INFO_RANGE_END,
78 },
79 sizeof(struct bridge_vlan_info));
80 if (r < 0)
81 return r;
82
83 return 0;
84 }
85
86 static uint16_t link_get_pvid(Link *link, bool *ret_untagged) {
87 assert(link);
88 assert(link->network);
89
90 if (vlanid_is_valid(link->network->bridge_vlan_pvid)) {
91 if (ret_untagged)
92 *ret_untagged = is_bit_set(link->network->bridge_vlan_pvid,
93 link->network->bridge_vlan_untagged_bitmap);
94 return link->network->bridge_vlan_pvid;
95 }
96
97 if (link->network->bridge_vlan_pvid == BRIDGE_VLAN_KEEP_PVID) {
98 if (ret_untagged)
99 *ret_untagged = link->bridge_vlan_pvid_is_untagged;
100 return link->bridge_vlan_pvid;
101 }
102
103 if (ret_untagged)
104 *ret_untagged = false;
105 return UINT16_MAX;
106 }
107
108 static int bridge_vlan_append_set_info(Link *link, sd_netlink_message *m) {
109 _cleanup_free_ char *str = NULL;
110 uint16_t pvid, begin = UINT16_MAX;
111 bool untagged, pvid_is_untagged;
112 int r;
113
114 assert(link);
115 assert(link->network);
116 assert(m);
117
118 pvid = link_get_pvid(link, &pvid_is_untagged);
119
120 for (uint16_t k = 0; k < BRIDGE_VLAN_BITMAP_MAX; k++) {
121
122 if (k == pvid) {
123 /* PVID needs to be sent alone. Finish previous bits. */
124 if (begin != UINT16_MAX) {
125 assert(begin < k);
126
127 r = add_range(m, begin, k - 1, untagged, &str);
128 if (r < 0)
129 return r;
130
131 begin = UINT16_MAX;
132 }
133
134 r = add_single(m, pvid, pvid_is_untagged, /* is_pvid= */ true, &str);
135 if (r < 0)
136 return r;
137
138 continue;
139 }
140
141 if (!is_bit_set(k, link->network->bridge_vlan_bitmap)) {
142 /* This bit is not set. Finish previous bits. */
143 if (begin != UINT16_MAX) {
144 assert(begin < k);
145
146 r = add_range(m, begin, k - 1, untagged, &str);
147 if (r < 0)
148 return r;
149
150 begin = UINT16_MAX;
151 }
152
153 continue;
154 }
155
156 if (begin != UINT16_MAX) {
157 bool u;
158
159 assert(begin < k);
160
161 u = is_bit_set(k, link->network->bridge_vlan_untagged_bitmap);
162 if (untagged == u)
163 continue;
164
165 /* Tagging flag is changed from the previous bits. Finish them. */
166 r = add_range(m, begin, k - 1, untagged, &str);
167 if (r < 0)
168 return r;
169
170 begin = k;
171 untagged = u;
172 continue;
173 }
174
175 /* This is the starting point of a new bit sequence. Save the position and the tagging flag. */
176 begin = k;
177 untagged = is_bit_set(k, link->network->bridge_vlan_untagged_bitmap);
178 }
179
180 /* No pending bit sequence.
181 * Why? There is a trick. The conf parsers below only accepts vlan ID in the range 0…4094, but in
182 * the above loop, we run 0…4095. */
183 assert_cc(BRIDGE_VLAN_BITMAP_MAX > VLANID_MAX);
184 assert(begin == UINT16_MAX);
185
186 log_link_debug(link, "Setting Bridge VLAN IDs: %s", strna(str));
187 return 0;
188 }
189
190 static int bridge_vlan_append_del_info(Link *link, sd_netlink_message *m) {
191 _cleanup_free_ char *str = NULL;
192 uint16_t pvid, begin = UINT16_MAX;
193 int r;
194
195 assert(link);
196 assert(link->network);
197 assert(m);
198
199 pvid = link_get_pvid(link, NULL);
200
201 for (uint16_t k = 0; k < BRIDGE_VLAN_BITMAP_MAX; k++) {
202
203 if (k == pvid ||
204 !is_bit_set(k, link->bridge_vlan_bitmap) ||
205 is_bit_set(k, link->network->bridge_vlan_bitmap)) {
206 /* This bit is not necessary to be removed. Finish previous bits. */
207 if (begin != UINT16_MAX) {
208 assert(begin < k);
209
210 r = add_range(m, begin, k - 1, /* untagged= */ false, &str);
211 if (r < 0)
212 return r;
213
214 begin = UINT16_MAX;
215 }
216
217 continue;
218 }
219
220 if (begin != UINT16_MAX)
221 continue;
222
223 /* This is the starting point of a new bit sequence. Save the position. */
224 begin = k;
225 }
226
227 /* No pending bit sequence. */
228 assert(begin == UINT16_MAX);
229
230 log_link_debug(link, "Removing Bridge VLAN IDs: %s", strna(str));
231 return 0;
232 }
233
234 int bridge_vlan_set_message(Link *link, sd_netlink_message *m, bool is_set) {
235 int r;
236
237 assert(link);
238 assert(m);
239
240 r = sd_rtnl_message_link_set_family(m, AF_BRIDGE);
241 if (r < 0)
242 return r;
243
244 r = sd_netlink_message_open_container(m, IFLA_AF_SPEC);
245 if (r < 0)
246 return r;
247
248 if (link->master_ifindex <= 0 || streq_ptr(link->kind, "bridge")) {
249 /* If the setting is requested in a .network file for a bridge master (or a physical master)
250 * interface, then BRIDGE_FLAGS_SELF flag needs to be set. */
251 r = sd_netlink_message_append_u16(m, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF);
252 if (r < 0)
253 return r;
254 }
255
256 if (is_set)
257 r = bridge_vlan_append_set_info(link, m);
258 else
259 r = bridge_vlan_append_del_info(link, m);
260 if (r < 0)
261 return r;
262
263 r = sd_netlink_message_close_container(m);
264 if (r < 0)
265 return r;
266
267 return 0;
268 }
269
270 int link_update_bridge_vlan(Link *link, sd_netlink_message *m) {
271 _cleanup_free_ void *data = NULL;
272 size_t len;
273 uint16_t begin = UINT16_MAX;
274 int r, family;
275
276 assert(link);
277 assert(m);
278
279 r = sd_rtnl_message_get_family(m, &family);
280 if (r < 0)
281 return r;
282
283 if (family != AF_BRIDGE)
284 return 0;
285
286 r = sd_netlink_message_read_data(m, IFLA_AF_SPEC, &len, &data);
287 if (r == -ENODATA)
288 return 0;
289 if (r < 0)
290 return r;
291
292 memzero(link->bridge_vlan_bitmap, sizeof(link->bridge_vlan_bitmap));
293
294 for (struct rtattr *rta = data; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
295 struct bridge_vlan_info *p;
296
297 if (RTA_TYPE(rta) != IFLA_BRIDGE_VLAN_INFO)
298 continue;
299 if (RTA_PAYLOAD(rta) != sizeof(struct bridge_vlan_info))
300 continue;
301
302 p = RTA_DATA(rta);
303
304 if (FLAGS_SET(p->flags, BRIDGE_VLAN_INFO_RANGE_BEGIN)) {
305 begin = p->vid;
306 continue;
307 }
308
309 if (FLAGS_SET(p->flags, BRIDGE_VLAN_INFO_RANGE_END)) {
310 for (uint16_t k = begin; k <= p->vid; k++)
311 set_bit(k, link->bridge_vlan_bitmap);
312
313 begin = UINT16_MAX;
314 continue;
315 }
316
317 if (FLAGS_SET(p->flags, BRIDGE_VLAN_INFO_PVID)) {
318 link->bridge_vlan_pvid = p->vid;
319 link->bridge_vlan_pvid_is_untagged = FLAGS_SET(p->flags, BRIDGE_VLAN_INFO_UNTAGGED);
320 }
321
322 set_bit(p->vid, link->bridge_vlan_bitmap);
323 begin = UINT16_MAX;
324 }
325
326 return 0;
327 }
328
329 void network_adjust_bridge_vlan(Network *network) {
330 assert(network);
331
332 for (uint16_t k = 0; k < BRIDGE_VLAN_BITMAP_MAX; k++)
333 if (is_bit_set(k, network->bridge_vlan_untagged_bitmap))
334 set_bit(k, network->bridge_vlan_bitmap);
335
336 if (vlanid_is_valid(network->bridge_vlan_pvid))
337 set_bit(network->bridge_vlan_pvid, network->bridge_vlan_bitmap);
338 }
339
340 int config_parse_bridge_vlan_id(
341 const char *unit,
342 const char *filename,
343 unsigned line,
344 const char *section,
345 unsigned section_line,
346 const char *lvalue,
347 int ltype,
348 const char *rvalue,
349 void *data,
350 void *userdata) {
351
352 uint16_t v, *id = ASSERT_PTR(data);
353 int r;
354
355 assert(filename);
356 assert(section);
357 assert(lvalue);
358 assert(rvalue);
359
360 if (isempty(rvalue)) {
361 *id = BRIDGE_VLAN_KEEP_PVID;
362 return 0;
363 }
364
365 if (parse_boolean(rvalue) == 0) {
366 *id = BRIDGE_VLAN_REMOVE_PVID;
367 return 0;
368 }
369
370 r = parse_vlanid(rvalue, &v);
371 if (r < 0) {
372 log_syntax(unit, LOG_WARNING, filename, line, r,
373 "Failed to parse %s=, ignoring: %s",
374 lvalue, rvalue);
375 return 0;
376 }
377
378 *id = v;
379 return 0;
380 }
381
382 int config_parse_bridge_vlan_id_range(
383 const char *unit,
384 const char *filename,
385 unsigned line,
386 const char *section,
387 unsigned section_line,
388 const char *lvalue,
389 int ltype,
390 const char *rvalue,
391 void *data,
392 void *userdata) {
393
394 uint32_t *bitmap = ASSERT_PTR(data);
395 uint16_t vid, vid_end;
396 int r;
397
398 assert(filename);
399 assert(section);
400 assert(lvalue);
401 assert(rvalue);
402
403 if (isempty(rvalue)) {
404 memzero(bitmap, BRIDGE_VLAN_BITMAP_LEN * sizeof(uint32_t));
405 return 0;
406 }
407
408 r = parse_vid_range(rvalue, &vid, &vid_end);
409 if (r < 0) {
410 log_syntax(unit, LOG_WARNING, filename, line, r,
411 "Failed to parse %s=, ignoring: %s",
412 lvalue, rvalue);
413 return 0;
414 }
415
416 for (; vid <= vid_end; vid++)
417 set_bit(vid, bitmap);
418
419 return 0;
420 }