| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_bridge.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "conf-parser.h" |
| 8 |
#include "hashmap.h" |
| 9 |
#include "networkd-bridge-mdb.h" |
| 10 |
#include "networkd-link.h" |
| 11 |
#include "networkd-manager.h" |
| 12 |
#include "networkd-network.h" |
| 13 |
#include "networkd-queue.h" |
| 14 |
#include "networkd-util.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "vlan-util.h" |
| 17 |
|
| 18 |
#define STATIC_BRIDGE_MDB_ENTRIES_PER_NETWORK_MAX 1024U |
| 19 |
|
| 20 |
static BridgeMDB* bridge_mdb_free(BridgeMDB *mdb) { |
| 21 |
if (!mdb) |
| 22 |
return NULL; |
| 23 |
|
| 24 |
if (mdb->network) { |
| 25 |
assert(mdb->section); |
| 26 |
hashmap_remove(mdb->network->bridge_mdb_entries_by_section, mdb->section); |
| 27 |
} |
| 28 |
|
| 29 |
config_section_free(mdb->section); |
| 30 |
|
| 31 |
return mfree(mdb); |
| 32 |
} |
| 33 |
|
| 34 |
DEFINE_SECTION_CLEANUP_FUNCTIONS(BridgeMDB, bridge_mdb_free); |
| 35 |
|
| 36 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 37 |
bridge_mdb_hash_ops_by_section, |
| 38 |
ConfigSection, config_section_hash_func, config_section_compare_func, |
| 39 |
BridgeMDB, bridge_mdb_free); |
| 40 |
|
| 41 |
static int bridge_mdb_new_static( |
| 42 |
Network *network, |
| 43 |
const char *filename, |
| 44 |
unsigned section_line, |
| 45 |
BridgeMDB **ret) { |
| 46 |
|
| 47 |
_cleanup_(config_section_freep) ConfigSection *n = NULL; |
| 48 |
_cleanup_(bridge_mdb_freep) BridgeMDB *mdb = NULL; |
| 49 |
int r; |
| 50 |
|
| 51 |
assert(network); |
| 52 |
assert(ret); |
| 53 |
assert(filename); |
| 54 |
assert(section_line > 0); |
| 55 |
|
| 56 |
r = config_section_new(filename, section_line, &n); |
| 57 |
if (r < 0) |
| 58 |
return r; |
| 59 |
|
| 60 |
/* search entry in hashmap first. */ |
| 61 |
mdb = hashmap_get(network->bridge_mdb_entries_by_section, n); |
| 62 |
if (mdb) { |
| 63 |
*ret = TAKE_PTR(mdb); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
if (hashmap_size(network->bridge_mdb_entries_by_section) >= STATIC_BRIDGE_MDB_ENTRIES_PER_NETWORK_MAX) |
| 68 |
return -E2BIG; |
| 69 |
|
| 70 |
/* allocate space for an MDB entry. */ |
| 71 |
mdb = new(BridgeMDB, 1); |
| 72 |
if (!mdb) |
| 73 |
return -ENOMEM; |
| 74 |
|
| 75 |
/* init MDB structure. */ |
| 76 |
*mdb = (BridgeMDB) { |
| 77 |
.network = network, |
| 78 |
.section = TAKE_PTR(n), |
| 79 |
.type = _BRIDGE_MDB_ENTRY_TYPE_INVALID, |
| 80 |
}; |
| 81 |
|
| 82 |
r = hashmap_ensure_put(&network->bridge_mdb_entries_by_section, &bridge_mdb_hash_ops_by_section, mdb->section, mdb); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
/* return allocated MDB structure. */ |
| 87 |
*ret = TAKE_PTR(mdb); |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
static int bridge_mdb_configure_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, void *userdata) { |
| 92 |
int r; |
| 93 |
|
| 94 |
assert(m); |
| 95 |
assert(link); |
| 96 |
|
| 97 |
r = sd_netlink_message_get_errno(m); |
| 98 |
if (r < 0 && r != -EEXIST) { |
| 99 |
log_link_message_warning_errno(link, m, r, "Could not add MDB entry"); |
| 100 |
link_enter_failed(link); |
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
if (link->static_bridge_mdb_messages == 0) { |
| 105 |
link->static_bridge_mdb_configured = true; |
| 106 |
link_check_ready(link); |
| 107 |
} |
| 108 |
|
| 109 |
return 1; |
| 110 |
} |
| 111 |
|
| 112 |
/* send a request to the kernel to add an MDB entry */ |
| 113 |
static int bridge_mdb_configure(BridgeMDB *mdb, Link *link, Request *req) { |
| 114 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
| 115 |
struct br_mdb_entry entry; |
| 116 |
int r; |
| 117 |
|
| 118 |
assert(mdb); |
| 119 |
assert(link); |
| 120 |
assert(link->manager); |
| 121 |
assert(req); |
| 122 |
|
| 123 |
if (DEBUG_LOGGING) |
| 124 |
log_link_debug(link, "Configuring bridge MDB entry: MulticastGroupAddress=%s, VLANId=%u", |
| 125 |
IN_ADDR_TO_STRING(mdb->family, &mdb->group_addr), mdb->vlan_id); |
| 126 |
|
| 127 |
entry = (struct br_mdb_entry) { |
| 128 |
/* If MDB entry is added on bridge master, then the state must be MDB_TEMPORARY, |
| 129 |
* except on L2 routes, where they must always be permanent. |
| 130 |
* See br_mdb_add_group() in net/bridge/br_mdb.c of kernel. */ |
| 131 |
.state = link->master_ifindex <= 0 && mdb->type == BRIDGE_MDB_ENTRY_TYPE_L3 ? MDB_TEMPORARY : MDB_PERMANENT, |
| 132 |
.ifindex = link->ifindex, |
| 133 |
.vid = mdb->vlan_id, |
| 134 |
}; |
| 135 |
|
| 136 |
switch (mdb->type) { |
| 137 |
case BRIDGE_MDB_ENTRY_TYPE_L2: |
| 138 |
memcpy(entry.addr.u.mac_addr, &mdb->l2_addr.ether_addr_octet, ETH_ALEN); |
| 139 |
entry.addr.proto = 0; |
| 140 |
break; |
| 141 |
case BRIDGE_MDB_ENTRY_TYPE_L3: |
| 142 |
switch (mdb->family) { |
| 143 |
case AF_INET: |
| 144 |
entry.addr.u.ip4 = mdb->group_addr.in.s_addr; |
| 145 |
entry.addr.proto = htobe16(ETH_P_IP); |
| 146 |
break; |
| 147 |
|
| 148 |
case AF_INET6: |
| 149 |
entry.addr.u.ip6 = mdb->group_addr.in6; |
| 150 |
entry.addr.proto = htobe16(ETH_P_IPV6); |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
assert_not_reached(); |
| 155 |
} |
| 156 |
break; |
| 157 |
default: |
| 158 |
assert_not_reached(); |
| 159 |
} |
| 160 |
|
| 161 |
r = sd_rtnl_message_new_mdb(link->manager->rtnl, &m, RTM_NEWMDB, |
| 162 |
link->master_ifindex > 0 ? link->master_ifindex : link->ifindex); |
| 163 |
if (r < 0) |
| 164 |
return r; |
| 165 |
|
| 166 |
r = sd_netlink_message_append_data(m, MDBA_SET_ENTRY, &entry, sizeof(entry)); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
return request_call_netlink_async(link->manager->rtnl, m, req); |
| 171 |
} |
| 172 |
|
| 173 |
static bool bridge_mdb_is_ready_to_configure(Link *link) { |
| 174 |
Link *master; |
| 175 |
|
| 176 |
assert(link); |
| 177 |
|
| 178 |
if (!link_is_ready_to_configure(link, false)) |
| 179 |
return false; |
| 180 |
|
| 181 |
if (!link->master_set) |
| 182 |
return false; |
| 183 |
|
| 184 |
if (link->master_ifindex <= 0 && streq_ptr(link->kind, "bridge")) |
| 185 |
return true; /* The interface is bridge master. */ |
| 186 |
|
| 187 |
if (link_get_master(link, &master) < 0) |
| 188 |
return false; |
| 189 |
|
| 190 |
if (!streq_ptr(master->kind, "bridge")) |
| 191 |
return false; |
| 192 |
|
| 193 |
if (!IN_SET(master->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) |
| 194 |
return false; |
| 195 |
|
| 196 |
if (master->set_flags_messages > 0) |
| 197 |
return false; |
| 198 |
|
| 199 |
if (!link_has_carrier(master)) |
| 200 |
return false; |
| 201 |
|
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
static int bridge_mdb_process_request(Request *req, Link *link, void *userdata) { |
| 206 |
BridgeMDB *mdb = ASSERT_PTR(userdata); |
| 207 |
int r; |
| 208 |
|
| 209 |
assert(req); |
| 210 |
assert(link); |
| 211 |
|
| 212 |
if (!bridge_mdb_is_ready_to_configure(link)) |
| 213 |
return 0; |
| 214 |
|
| 215 |
r = bridge_mdb_configure(mdb, link, req); |
| 216 |
if (r < 0) |
| 217 |
return log_link_warning_errno(link, r, "Failed to configure bridge MDB: %m"); |
| 218 |
|
| 219 |
return 1; |
| 220 |
} |
| 221 |
|
| 222 |
int link_request_static_bridge_mdb(Link *link) { |
| 223 |
BridgeMDB *mdb; |
| 224 |
int r; |
| 225 |
|
| 226 |
assert(link); |
| 227 |
assert(link->manager); |
| 228 |
|
| 229 |
link->static_bridge_mdb_configured = false; |
| 230 |
|
| 231 |
if (!link->network) |
| 232 |
return 0; |
| 233 |
|
| 234 |
if (hashmap_isempty(link->network->bridge_mdb_entries_by_section)) |
| 235 |
goto finish; |
| 236 |
|
| 237 |
HASHMAP_FOREACH(mdb, link->network->bridge_mdb_entries_by_section) { |
| 238 |
r = link_queue_request_full(link, REQUEST_TYPE_BRIDGE_MDB, |
| 239 |
mdb, NULL, |
| 240 |
trivial_hash_func, |
| 241 |
trivial_compare_func, |
| 242 |
bridge_mdb_process_request, |
| 243 |
&link->static_bridge_mdb_messages, |
| 244 |
bridge_mdb_configure_handler, |
| 245 |
NULL); |
| 246 |
if (r < 0) |
| 247 |
return log_link_error_errno(link, r, "Failed to request MDB entry to multicast group database: %m"); |
| 248 |
} |
| 249 |
|
| 250 |
finish: |
| 251 |
if (link->static_bridge_mdb_messages == 0) { |
| 252 |
link->static_bridge_mdb_configured = true; |
| 253 |
link_check_ready(link); |
| 254 |
} else { |
| 255 |
log_link_debug(link, "Setting bridge MDB entries."); |
| 256 |
link_set_state(link, LINK_STATE_CONFIGURING); |
| 257 |
} |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
static int bridge_mdb_verify(BridgeMDB *mdb) { |
| 263 |
if (section_is_invalid(mdb->section)) |
| 264 |
return -EINVAL; |
| 265 |
|
| 266 |
switch (mdb->type) { |
| 267 |
case BRIDGE_MDB_ENTRY_TYPE_L2: |
| 268 |
if (!ether_addr_is_multicast(&mdb->l2_addr)) |
| 269 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 270 |
"%s: MulticastGroupAddress= is not an L2 multicast address. " |
| 271 |
"Ignoring [BridgeMDB] section from line %u.", |
| 272 |
mdb->section->filename, mdb->section->line); |
| 273 |
break; |
| 274 |
case BRIDGE_MDB_ENTRY_TYPE_L3: |
| 275 |
if (mdb->family == AF_UNSPEC) |
| 276 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 277 |
"%s: [BridgeMDB] section without MulticastGroupAddress= field configured. " |
| 278 |
"Ignoring [BridgeMDB] section from line %u.", |
| 279 |
mdb->section->filename, mdb->section->line); |
| 280 |
|
| 281 |
if (!in_addr_is_multicast(mdb->family, &mdb->group_addr)) |
| 282 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 283 |
"%s: MulticastGroupAddress= is not a multicast address. " |
| 284 |
"Ignoring [BridgeMDB] section from line %u.", |
| 285 |
mdb->section->filename, mdb->section->line); |
| 286 |
|
| 287 |
switch (mdb->family) { |
| 288 |
case AF_INET: |
| 289 |
if (in4_addr_is_local_multicast(&mdb->group_addr.in)) |
| 290 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 291 |
"%s: MulticastGroupAddress= is a local multicast address. " |
| 292 |
"Ignoring [BridgeMDB] section from line %u.", |
| 293 |
mdb->section->filename, mdb->section->line); |
| 294 |
break; |
| 295 |
default: |
| 296 |
if (in6_addr_is_link_local_all_nodes(&mdb->group_addr.in6)) |
| 297 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 298 |
"%s: MulticastGroupAddress= is the multicast all nodes address. " |
| 299 |
"Ignoring [BridgeMDB] section from line %u.", |
| 300 |
mdb->section->filename, mdb->section->line); |
| 301 |
} |
| 302 |
break; |
| 303 |
default: |
| 304 |
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), |
| 305 |
"%s: [BridgeMDB] section without MulticastGroupAddress= field configured. " |
| 306 |
"Ignoring [BridgeMDB] section from line %u.", |
| 307 |
mdb->section->filename, mdb->section->line); |
| 308 |
} |
| 309 |
|
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
void network_drop_invalid_bridge_mdb_entries(Network *network) { |
| 314 |
BridgeMDB *mdb; |
| 315 |
|
| 316 |
assert(network); |
| 317 |
|
| 318 |
HASHMAP_FOREACH(mdb, network->bridge_mdb_entries_by_section) |
| 319 |
if (bridge_mdb_verify(mdb) < 0) |
| 320 |
bridge_mdb_free(mdb); |
| 321 |
} |
| 322 |
|
| 323 |
/* parse the VLAN Id from config files. */ |
| 324 |
int config_parse_mdb_vlan_id( |
| 325 |
const char *unit, |
| 326 |
const char *filename, |
| 327 |
unsigned line, |
| 328 |
const char *section, |
| 329 |
unsigned section_line, |
| 330 |
const char *lvalue, |
| 331 |
int ltype, |
| 332 |
const char *rvalue, |
| 333 |
void *data, |
| 334 |
void *userdata) { |
| 335 |
|
| 336 |
_cleanup_(bridge_mdb_free_or_set_invalidp) BridgeMDB *mdb = NULL; |
| 337 |
Network *network = userdata; |
| 338 |
int r; |
| 339 |
|
| 340 |
assert(filename); |
| 341 |
assert(section); |
| 342 |
assert(lvalue); |
| 343 |
assert(rvalue); |
| 344 |
assert(data); |
| 345 |
|
| 346 |
r = bridge_mdb_new_static(network, filename, section_line, &mdb); |
| 347 |
if (r < 0) |
| 348 |
return log_oom(); |
| 349 |
|
| 350 |
r = config_parse_vlanid(unit, filename, line, section, |
| 351 |
section_line, lvalue, ltype, |
| 352 |
rvalue, &mdb->vlan_id, userdata); |
| 353 |
if (r < 0) |
| 354 |
return r; |
| 355 |
|
| 356 |
TAKE_PTR(mdb); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
/* parse the multicast group from config files. */ |
| 361 |
int config_parse_mdb_group_address( |
| 362 |
const char *unit, |
| 363 |
const char *filename, |
| 364 |
unsigned line, |
| 365 |
const char *section, |
| 366 |
unsigned section_line, |
| 367 |
const char *lvalue, |
| 368 |
int ltype, |
| 369 |
const char *rvalue, |
| 370 |
void *data, |
| 371 |
void *userdata) { |
| 372 |
|
| 373 |
_cleanup_(bridge_mdb_free_or_set_invalidp) BridgeMDB *mdb = NULL; |
| 374 |
Network *network = userdata; |
| 375 |
int r; |
| 376 |
|
| 377 |
assert(filename); |
| 378 |
assert(section); |
| 379 |
assert(lvalue); |
| 380 |
assert(rvalue); |
| 381 |
assert(data); |
| 382 |
|
| 383 |
r = bridge_mdb_new_static(network, filename, section_line, &mdb); |
| 384 |
if (r < 0) |
| 385 |
return log_oom(); |
| 386 |
|
| 387 |
r = parse_ether_addr(rvalue, &mdb->l2_addr); |
| 388 |
if (r >= 0) |
| 389 |
mdb->type = BRIDGE_MDB_ENTRY_TYPE_L2; |
| 390 |
else { |
| 391 |
r = in_addr_from_string_auto(rvalue, &mdb->family, &mdb->group_addr); |
| 392 |
if (r < 0) { |
| 393 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 394 |
"Cannot parse multicast group address as either L2 MAC, IPv4 or IPv6, ignoring: %m"); |
| 395 |
return 0; |
| 396 |
} |
| 397 |
mdb->type = BRIDGE_MDB_ENTRY_TYPE_L3; |
| 398 |
} |
| 399 |
|
| 400 |
TAKE_PTR(mdb); |
| 401 |
return 0; |
| 402 |
} |