| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_arp.h> |
| 4 |
#include <linux/if_bridge.h> |
| 5 |
|
| 6 |
#include "sd-netlink.h" |
| 7 |
|
| 8 |
#include "bridge.h" |
| 9 |
#include "conf-parser.h" |
| 10 |
#include "netlink-util.h" |
| 11 |
#include "networkd-manager.h" |
| 12 |
#include "string-table.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "vlan-util.h" |
| 15 |
|
| 16 |
assert_cc((int) MULTICAST_ROUTER_NONE == (int) MDB_RTR_TYPE_DISABLED); |
| 17 |
assert_cc((int) MULTICAST_ROUTER_TEMPORARY_QUERY == (int) MDB_RTR_TYPE_TEMP_QUERY); |
| 18 |
assert_cc((int) MULTICAST_ROUTER_PERMANENT == (int) MDB_RTR_TYPE_PERM); |
| 19 |
assert_cc((int) MULTICAST_ROUTER_TEMPORARY == (int) MDB_RTR_TYPE_TEMP); |
| 20 |
|
| 21 |
static const char* const multicast_router_table[_MULTICAST_ROUTER_MAX] = { |
| 22 |
[MULTICAST_ROUTER_NONE] = "no", |
| 23 |
[MULTICAST_ROUTER_TEMPORARY_QUERY] = "query", |
| 24 |
[MULTICAST_ROUTER_PERMANENT] = "permanent", |
| 25 |
[MULTICAST_ROUTER_TEMPORARY] = "temporary", |
| 26 |
}; |
| 27 |
|
| 28 |
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(multicast_router, MulticastRouter, _MULTICAST_ROUTER_INVALID); |
| 29 |
DEFINE_CONFIG_PARSE_ENUM(config_parse_multicast_router, multicast_router, MulticastRouter); |
| 30 |
|
| 31 |
/* callback for bridge netdev's parameter set */ |
| 32 |
static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) { |
| 33 |
int r; |
| 34 |
|
| 35 |
assert(netdev); |
| 36 |
assert(m); |
| 37 |
|
| 38 |
r = sd_netlink_message_get_errno(m); |
| 39 |
if (r < 0) { |
| 40 |
log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); |
| 41 |
return 1; |
| 42 |
} |
| 43 |
|
| 44 |
log_netdev_debug(netdev, "Bridge parameters set success"); |
| 45 |
|
| 46 |
return 1; |
| 47 |
} |
| 48 |
|
| 49 |
static int netdev_bridge_post_create_message(NetDev *netdev, sd_netlink_message *req) { |
| 50 |
Bridge *b = BRIDGE(netdev); |
| 51 |
struct br_boolopt_multi bm = {}; |
| 52 |
int r; |
| 53 |
|
| 54 |
r = sd_netlink_message_open_container(req, IFLA_LINKINFO); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
|
| 58 |
r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); |
| 59 |
if (r < 0) |
| 60 |
return r; |
| 61 |
|
| 62 |
/* convert to jiffes */ |
| 63 |
if (b->forward_delay != USEC_INFINITY) { |
| 64 |
r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay)); |
| 65 |
if (r < 0) |
| 66 |
return r; |
| 67 |
} |
| 68 |
|
| 69 |
if (b->hello_time > 0) { |
| 70 |
r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time)); |
| 71 |
if (r < 0) |
| 72 |
return r; |
| 73 |
} |
| 74 |
|
| 75 |
if (b->max_age > 0) { |
| 76 |
r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age)); |
| 77 |
if (r < 0) |
| 78 |
return r; |
| 79 |
} |
| 80 |
|
| 81 |
if (b->ageing_time != USEC_INFINITY) { |
| 82 |
r = sd_netlink_message_append_u32(req, IFLA_BR_AGEING_TIME, usec_to_jiffies(b->ageing_time)); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
} |
| 86 |
|
| 87 |
if (b->priority > 0) { |
| 88 |
r = sd_netlink_message_append_u16(req, IFLA_BR_PRIORITY, b->priority); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
} |
| 92 |
|
| 93 |
if (b->group_fwd_mask > 0) { |
| 94 |
r = sd_netlink_message_append_u16(req, IFLA_BR_GROUP_FWD_MASK, b->group_fwd_mask); |
| 95 |
if (r < 0) |
| 96 |
return r; |
| 97 |
} |
| 98 |
|
| 99 |
if (b->default_pvid != VLANID_INVALID) { |
| 100 |
r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_DEFAULT_PVID, b->default_pvid); |
| 101 |
if (r < 0) |
| 102 |
return r; |
| 103 |
} |
| 104 |
|
| 105 |
if (b->mcast_querier >= 0) { |
| 106 |
r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_QUERIER, b->mcast_querier); |
| 107 |
if (r < 0) |
| 108 |
return r; |
| 109 |
} |
| 110 |
|
| 111 |
if (b->mcast_snooping >= 0) { |
| 112 |
r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_SNOOPING, b->mcast_snooping); |
| 113 |
if (r < 0) |
| 114 |
return r; |
| 115 |
} |
| 116 |
|
| 117 |
if (b->vlan_filtering >= 0) { |
| 118 |
r = sd_netlink_message_append_u8(req, IFLA_BR_VLAN_FILTERING, b->vlan_filtering); |
| 119 |
if (r < 0) |
| 120 |
return r; |
| 121 |
} |
| 122 |
|
| 123 |
if (b->vlan_protocol >= 0) { |
| 124 |
r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_PROTOCOL, htobe16(b->vlan_protocol)); |
| 125 |
if (r < 0) |
| 126 |
return r; |
| 127 |
} |
| 128 |
|
| 129 |
if (b->stp >= 0) { |
| 130 |
r = sd_netlink_message_append_u32(req, IFLA_BR_STP_STATE, b->stp); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
} |
| 134 |
|
| 135 |
if (b->igmp_version > 0) { |
| 136 |
r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_IGMP_VERSION, b->igmp_version); |
| 137 |
if (r < 0) |
| 138 |
return r; |
| 139 |
} |
| 140 |
|
| 141 |
if (b->fdb_max_learned_set) { |
| 142 |
r = sd_netlink_message_append_u32(req, IFLA_BR_FDB_MAX_LEARNED, b->fdb_max_learned); |
| 143 |
if (r < 0) |
| 144 |
return r; |
| 145 |
} |
| 146 |
|
| 147 |
if (b->linklocal_learn >= 0) { |
| 148 |
bm.optmask |= 1 << BR_BOOLOPT_NO_LL_LEARN; |
| 149 |
SET_FLAG(bm.optval, 1 << BR_BOOLOPT_NO_LL_LEARN, !b->linklocal_learn); |
| 150 |
} |
| 151 |
|
| 152 |
if (bm.optmask != 0) { |
| 153 |
r = sd_netlink_message_append_data(req, IFLA_BR_MULTI_BOOLOPT, &bm, sizeof(bm)); |
| 154 |
if (r < 0) |
| 155 |
return r; |
| 156 |
} |
| 157 |
|
| 158 |
r = sd_netlink_message_close_container(req); |
| 159 |
if (r < 0) |
| 160 |
return r; |
| 161 |
|
| 162 |
r = sd_netlink_message_close_container(req); |
| 163 |
if (r < 0) |
| 164 |
return r; |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static int netdev_bridge_post_create(NetDev *netdev, Link *link) { |
| 170 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
| 171 |
int r; |
| 172 |
|
| 173 |
assert(netdev); |
| 174 |
|
| 175 |
if (!netdev_is_managed(netdev)) |
| 176 |
return 0; /* Already detached, due to e.g. reloading .netdev files. */ |
| 177 |
|
| 178 |
r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex); |
| 179 |
if (r < 0) |
| 180 |
return log_netdev_error_errno(netdev, r, "Could not allocate netlink message: %m"); |
| 181 |
|
| 182 |
r = netdev_bridge_post_create_message(netdev, req); |
| 183 |
if (r < 0) |
| 184 |
return log_netdev_error_errno(netdev, r, "Could not create netlink message: %m"); |
| 185 |
|
| 186 |
r = netlink_call_async(netdev->manager->rtnl, NULL, req, netdev_bridge_set_handler, |
| 187 |
netdev_destroy_callback, netdev); |
| 188 |
if (r < 0) |
| 189 |
return log_netdev_error_errno(netdev, r, "Could not send netlink message: %m"); |
| 190 |
|
| 191 |
netdev_ref(netdev); |
| 192 |
|
| 193 |
return r; |
| 194 |
} |
| 195 |
|
| 196 |
int config_parse_bridge_igmp_version( |
| 197 |
const char *unit, |
| 198 |
const char *filename, |
| 199 |
unsigned line, |
| 200 |
const char *section, |
| 201 |
unsigned section_line, |
| 202 |
const char *lvalue, |
| 203 |
int ltype, |
| 204 |
const char *rvalue, |
| 205 |
void *data, |
| 206 |
void *userdata) { |
| 207 |
|
| 208 |
assert(filename); |
| 209 |
assert(lvalue); |
| 210 |
assert(rvalue); |
| 211 |
assert(data); |
| 212 |
|
| 213 |
Bridge *b = ASSERT_PTR(userdata); |
| 214 |
|
| 215 |
if (isempty(rvalue)) { |
| 216 |
b->igmp_version = 0; /* 0 means unset. */ |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
return config_parse_uint8_bounded( |
| 221 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 222 |
2, 3, true, |
| 223 |
&b->igmp_version); |
| 224 |
} |
| 225 |
|
| 226 |
int config_parse_bridge_port_priority( |
| 227 |
const char *unit, |
| 228 |
const char *filename, |
| 229 |
unsigned line, |
| 230 |
const char *section, |
| 231 |
unsigned section_line, |
| 232 |
const char *lvalue, |
| 233 |
int ltype, |
| 234 |
const char *rvalue, |
| 235 |
void *data, |
| 236 |
void *userdata) { |
| 237 |
|
| 238 |
assert(filename); |
| 239 |
assert(lvalue); |
| 240 |
assert(rvalue); |
| 241 |
|
| 242 |
uint16_t *prio = ASSERT_PTR(data); |
| 243 |
|
| 244 |
return config_parse_uint16_bounded( |
| 245 |
unit, filename, line, section, section_line, lvalue, rvalue, |
| 246 |
0, LINK_BRIDGE_PORT_PRIORITY_MAX, true, |
| 247 |
prio); |
| 248 |
} |
| 249 |
|
| 250 |
int config_parse_bridge_fdb_max_learned( |
| 251 |
const char *unit, |
| 252 |
const char *filename, |
| 253 |
unsigned line, |
| 254 |
const char *section, |
| 255 |
unsigned section_line, |
| 256 |
const char *lvalue, |
| 257 |
int ltype, |
| 258 |
const char *rvalue, |
| 259 |
void *data, |
| 260 |
void *userdata) { |
| 261 |
|
| 262 |
Bridge *b = ASSERT_PTR(userdata); |
| 263 |
int r; |
| 264 |
|
| 265 |
assert(filename); |
| 266 |
assert(lvalue); |
| 267 |
assert(rvalue); |
| 268 |
assert(data); |
| 269 |
|
| 270 |
if (isempty(rvalue)) { |
| 271 |
b->fdb_max_learned_set = false; |
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
r = config_parse_uint32_bounded(unit, filename, line, section, section_line, lvalue, rvalue, |
| 276 |
0, UINT32_MAX, true, &b->fdb_max_learned); |
| 277 |
if (r <= 0) |
| 278 |
return r; |
| 279 |
|
| 280 |
b->fdb_max_learned_set = true; |
| 281 |
return 1; |
| 282 |
} |
| 283 |
|
| 284 |
static void bridge_init(NetDev *netdev) { |
| 285 |
Bridge *b = BRIDGE(netdev); |
| 286 |
|
| 287 |
b->mcast_querier = -1; |
| 288 |
b->mcast_snooping = -1; |
| 289 |
b->vlan_filtering = -1; |
| 290 |
b->vlan_protocol = -1; |
| 291 |
b->stp = -1; |
| 292 |
b->default_pvid = VLANID_INVALID; |
| 293 |
b->forward_delay = USEC_INFINITY; |
| 294 |
b->ageing_time = USEC_INFINITY; |
| 295 |
b->linklocal_learn = -1; |
| 296 |
} |
| 297 |
|
| 298 |
static bool bridge_can_set_mac(NetDev *netdev, const struct hw_addr_data *hw_addr) { |
| 299 |
return true; |
| 300 |
} |
| 301 |
|
| 302 |
const NetDevVTable bridge_vtable = { |
| 303 |
.object_size = sizeof(Bridge), |
| 304 |
.init = bridge_init, |
| 305 |
.sections = NETDEV_COMMON_SECTIONS "Bridge\0", |
| 306 |
.post_create = netdev_bridge_post_create, |
| 307 |
.create_type = NETDEV_CREATE_INDEPENDENT, |
| 308 |
.can_set_mac = bridge_can_set_mac, |
| 309 |
.iftype = ARPHRD_ETHER, |
| 310 |
.generate_mac = true, |
| 311 |
}; |