2 This file is part of systemd.
4 Copyright (C) 2016 BISDN GmbH. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/in.h>
21 #include <linux/if_bridge.h>
24 #include "alloc-util.h"
25 #include "conf-parser.h"
26 #include "netlink-util.h"
27 #include "networkd-brvlan.h"
29 #include "parse-util.h"
30 #include "vlan-util.h"
32 static bool is_bit_set(unsigned bit
, uint32_t scope
) {
33 assert(bit
< sizeof(scope
)*8);
34 return scope
& (1 << bit
);
37 static inline void set_bit(unsigned nr
, uint32_t *addr
) {
38 if (nr
< BRIDGE_VLAN_BITMAP_MAX
)
39 addr
[nr
/ 32] |= (((uint32_t) 1) << (nr
% 32));
42 static int find_next_bit(int i
, uint32_t x
) {
50 return BUILTIN_FFS_U32(x
);
52 /* mask off prior finds to get next */
53 j
= __builtin_ffs(x
>> i
);
57 static int append_vlan_info_data(Link
*const link
, sd_netlink_message
*req
, uint16_t pvid
, const uint32_t *br_vid_bitmap
, const uint32_t *br_untagged_bitmap
) {
58 struct bridge_vlan_info br_vlan
;
59 int i
, j
, k
, r
, done
, cnt
;
61 bool untagged
= false;
65 assert(br_vid_bitmap
);
66 assert(br_untagged_bitmap
);
70 begin
= end
= UINT16_MAX
;
71 for (k
= 0; k
< BRIDGE_VLAN_BITMAP_LEN
; k
++) {
73 uint32_t vid_map
= br_vid_bitmap
[k
];
74 uint32_t untagged_map
= br_untagged_bitmap
[k
];
80 j
= find_next_bit(i
, vid_map
);
82 /* first hit of any bit */
83 if (begin
== UINT16_MAX
&& end
== UINT16_MAX
) {
84 begin
= end
= j
- 1 + base_bit
;
85 untagged
= is_bit_set(j
- 1, untagged_map
);
89 /* this bit is a continuation of prior bits */
90 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
) {
97 if (begin
!= UINT16_MAX
) {
99 if (done
&& k
< BRIDGE_VLAN_BITMAP_LEN
- 1)
104 br_vlan
.flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
110 br_vlan
.flags
|= BRIDGE_VLAN_INFO_PVID
;
112 r
= sd_netlink_message_append_data(req
, IFLA_BRIDGE_VLAN_INFO
, &br_vlan
, sizeof(br_vlan
));
114 return log_link_error_errno(link
, r
, "Could not append IFLA_BRIDGE_VLAN_INFO attribute: %m");
117 br_vlan
.flags
|= BRIDGE_VLAN_INFO_RANGE_BEGIN
;
119 r
= sd_netlink_message_append_data(req
, IFLA_BRIDGE_VLAN_INFO
, &br_vlan
, sizeof(br_vlan
));
121 return log_link_error_errno(link
, r
, "Could not append IFLA_BRIDGE_VLAN_INFO attribute: %m");
124 br_vlan
.flags
&= ~BRIDGE_VLAN_INFO_RANGE_BEGIN
;
125 br_vlan
.flags
|= BRIDGE_VLAN_INFO_RANGE_END
;
127 r
= sd_netlink_message_append_data(req
, IFLA_BRIDGE_VLAN_INFO
, &br_vlan
, sizeof(br_vlan
));
129 return log_link_error_errno(link
, r
, "Could not append IFLA_BRIDGE_VLAN_INFO attribute: %m");
136 begin
= end
= j
- 1 + base_bit
;
137 untagged
= is_bit_set(j
- 1, untagged_map
);
150 static int set_brvlan_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
151 Link
*link
= userdata
;
156 r
= sd_netlink_message_get_errno(m
);
157 if (r
< 0 && r
!= -EEXIST
)
158 log_link_error_errno(link
, r
, "Could not add VLAN to bridge port: %m");
163 int br_vlan_configure(Link
*link
, uint16_t pvid
, uint32_t *br_vid_bitmap
, uint32_t *br_untagged_bitmap
) {
164 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
170 assert(link
->manager
);
171 assert(br_vid_bitmap
);
172 assert(br_untagged_bitmap
);
173 assert(link
->network
);
175 /* pvid might not be in br_vid_bitmap yet */
177 set_bit(pvid
, br_vid_bitmap
);
179 rtnl
= link
->manager
->rtnl
;
181 /* create new RTM message */
182 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
184 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
186 r
= sd_rtnl_message_link_set_family(req
, PF_BRIDGE
);
188 return log_link_error_errno(link
, r
, "Could not set message family: %m");
190 r
= sd_netlink_message_open_container(req
, IFLA_AF_SPEC
);
192 return log_link_error_errno(link
, r
, "Could not open IFLA_AF_SPEC container: %m");
194 /* master needs flag self */
195 if (!link
->network
->bridge
) {
196 flags
= BRIDGE_FLAGS_SELF
;
197 sd_netlink_message_append_data(req
, IFLA_BRIDGE_FLAGS
, &flags
, sizeof(uint16_t));
201 r
= append_vlan_info_data(link
, req
, pvid
, br_vid_bitmap
, br_untagged_bitmap
);
203 return log_link_error_errno(link
, r
, "Could not append VLANs: %m");
205 r
= sd_netlink_message_close_container(req
);
207 return log_link_error_errno(link
, r
, "Could not close IFLA_AF_SPEC container: %m");
209 /* send message to the kernel */
210 r
= sd_netlink_call_async(rtnl
, req
, set_brvlan_handler
, link
, 0, NULL
);
212 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
217 static int parse_vid_range(const char *rvalue
, uint16_t *vid
, uint16_t *vid_end
) {
220 char *_rvalue
= NULL
;
221 uint16_t _vid
= UINT16_MAX
;
222 uint16_t _vid_end
= UINT16_MAX
;
228 _rvalue
= strdupa(rvalue
);
229 p
= strchr(_rvalue
, '-');
233 r
= parse_vlanid(_rvalue
, &_vid
);
240 r
= parse_vlanid(p
, &_vid_end
);
247 r
= parse_vlanid(_rvalue
, &_vid
);
260 int config_parse_brvlan_vlan(const char *unit
, const char *filename
,
261 unsigned line
, const char *section
,
262 unsigned section_line
, const char *lvalue
,
263 int ltype
, const char *rvalue
, void *data
,
265 Network
*network
= userdata
;
267 uint16_t vid
, vid_end
;
275 r
= parse_vid_range(rvalue
, &vid
, &vid_end
);
277 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse VLAN, ignoring: %s", rvalue
);
281 if (UINT16_MAX
== vid_end
)
282 set_bit(vid
++, network
->br_vid_bitmap
);
284 if (vid
>= vid_end
) {
285 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid VLAN range, ignoring %s", rvalue
);
288 for (; vid
<= vid_end
; vid
++)
289 set_bit(vid
, network
->br_vid_bitmap
);
294 int config_parse_brvlan_untagged(const char *unit
, const char *filename
,
295 unsigned line
, const char *section
,
296 unsigned section_line
, const char *lvalue
,
297 int ltype
, const char *rvalue
, void *data
,
299 Network
*network
= userdata
;
301 uint16_t vid
, vid_end
;
309 r
= parse_vid_range(rvalue
, &vid
, &vid_end
);
311 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Could not parse VLAN: %s", rvalue
);
315 if (UINT16_MAX
== vid_end
) {
316 set_bit(vid
, network
->br_vid_bitmap
);
317 set_bit(vid
, network
->br_untagged_bitmap
);
319 if (vid
>= vid_end
) {
320 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid VLAN range, ignoring %s", rvalue
);
323 for (; vid
<= vid_end
; vid
++) {
324 set_bit(vid
, network
->br_vid_bitmap
);
325 set_bit(vid
, network
->br_untagged_bitmap
);