1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2020, Nikolay Aleksandrov <nikolay@cumulusnetworks.com> 3 #include <linux/kernel.h> 4 #include <linux/netdevice.h> 5 #include <linux/rtnetlink.h> 6 #include <linux/slab.h> 7 #include <net/ip_tunnels.h> 8 9 #include "br_private.h" 10 #include "br_private_tunnel.h" 11 12 static bool __vlan_tun_put(struct sk_buff *skb, const struct net_bridge_vlan *v) 13 { 14 __be32 tid = tunnel_id_to_key32(v->tinfo.tunnel_id); 15 struct nlattr *nest; 16 17 if (!v->tinfo.tunnel_dst) 18 return true; 19 20 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_TUNNEL_INFO); 21 if (!nest) 22 return false; 23 if (nla_put_u32(skb, BRIDGE_VLANDB_TINFO_ID, be32_to_cpu(tid))) { 24 nla_nest_cancel(skb, nest); 25 return false; 26 } 27 nla_nest_end(skb, nest); 28 29 return true; 30 } 31 32 static bool __vlan_tun_can_enter_range(const struct net_bridge_vlan *v_curr, 33 const struct net_bridge_vlan *range_end) 34 { 35 return (!v_curr->tinfo.tunnel_dst && !range_end->tinfo.tunnel_dst) || 36 vlan_tunid_inrange(v_curr, range_end); 37 } 38 39 /* check if the options' state of v_curr allow it to enter the range */ 40 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr, 41 const struct net_bridge_vlan *range_end) 42 { 43 return v_curr->state == range_end->state && 44 __vlan_tun_can_enter_range(v_curr, range_end); 45 } 46 47 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v) 48 { 49 return !nla_put_u8(skb, BRIDGE_VLANDB_ENTRY_STATE, 50 br_vlan_get_state(v)) && 51 __vlan_tun_put(skb, v); 52 } 53 54 size_t br_vlan_opts_nl_size(void) 55 { 56 return nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_ENTRY_STATE */ 57 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY_TUNNEL_INFO */ 58 + nla_total_size(sizeof(u32)); /* BRIDGE_VLANDB_TINFO_ID */ 59 } 60 61 static int br_vlan_modify_state(struct net_bridge_vlan_group *vg, 62 struct net_bridge_vlan *v, 63 u8 state, 64 bool *changed, 65 struct netlink_ext_ack *extack) 66 { 67 struct net_bridge *br; 68 69 ASSERT_RTNL(); 70 71 if (state > BR_STATE_BLOCKING) { 72 NL_SET_ERR_MSG_MOD(extack, "Invalid vlan state"); 73 return -EINVAL; 74 } 75 76 if (br_vlan_is_brentry(v)) 77 br = v->br; 78 else 79 br = v->port->br; 80 81 if (br->stp_enabled == BR_KERNEL_STP) { 82 NL_SET_ERR_MSG_MOD(extack, "Can't modify vlan state when using kernel STP"); 83 return -EBUSY; 84 } 85 86 if (v->state == state) 87 return 0; 88 89 if (v->vid == br_get_pvid(vg)) 90 br_vlan_set_pvid_state(vg, state); 91 92 br_vlan_set_state(v, state); 93 *changed = true; 94 95 return 0; 96 } 97 98 static const struct nla_policy br_vlandb_tinfo_pol[BRIDGE_VLANDB_TINFO_MAX + 1] = { 99 [BRIDGE_VLANDB_TINFO_ID] = { .type = NLA_U32 }, 100 [BRIDGE_VLANDB_TINFO_CMD] = { .type = NLA_U32 }, 101 }; 102 103 static int br_vlan_modify_tunnel(const struct net_bridge_port *p, 104 struct net_bridge_vlan *v, 105 struct nlattr **tb, 106 bool *changed, 107 struct netlink_ext_ack *extack) 108 { 109 struct nlattr *tun_tb[BRIDGE_VLANDB_TINFO_MAX + 1], *attr; 110 struct bridge_vlan_info *vinfo; 111 u32 tun_id = 0; 112 int cmd, err; 113 114 if (!p) { 115 NL_SET_ERR_MSG_MOD(extack, "Can't modify tunnel mapping of non-port vlans"); 116 return -EINVAL; 117 } 118 if (!(p->flags & BR_VLAN_TUNNEL)) { 119 NL_SET_ERR_MSG_MOD(extack, "Port doesn't have tunnel flag set"); 120 return -EINVAL; 121 } 122 123 attr = tb[BRIDGE_VLANDB_ENTRY_TUNNEL_INFO]; 124 err = nla_parse_nested(tun_tb, BRIDGE_VLANDB_TINFO_MAX, attr, 125 br_vlandb_tinfo_pol, extack); 126 if (err) 127 return err; 128 129 if (!tun_tb[BRIDGE_VLANDB_TINFO_CMD]) { 130 NL_SET_ERR_MSG_MOD(extack, "Missing tunnel command attribute"); 131 return -ENOENT; 132 } 133 cmd = nla_get_u32(tun_tb[BRIDGE_VLANDB_TINFO_CMD]); 134 switch (cmd) { 135 case RTM_SETLINK: 136 if (!tun_tb[BRIDGE_VLANDB_TINFO_ID]) { 137 NL_SET_ERR_MSG_MOD(extack, "Missing tunnel id attribute"); 138 return -ENOENT; 139 } 140 /* when working on vlan ranges this is the starting tunnel id */ 141 tun_id = nla_get_u32(tun_tb[BRIDGE_VLANDB_TINFO_ID]); 142 /* vlan info attr is guaranteed by br_vlan_rtm_process_one */ 143 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]); 144 /* tunnel ids are mapped to each vlan in increasing order, 145 * the starting vlan is in BRIDGE_VLANDB_ENTRY_INFO and v is the 146 * current vlan, so we compute: tun_id + v - vinfo->vid 147 */ 148 tun_id += v->vid - vinfo->vid; 149 break; 150 case RTM_DELLINK: 151 break; 152 default: 153 NL_SET_ERR_MSG_MOD(extack, "Unsupported tunnel command"); 154 return -EINVAL; 155 } 156 157 return br_vlan_tunnel_info(p, cmd, v->vid, tun_id, changed); 158 } 159 160 static int br_vlan_process_one_opts(const struct net_bridge *br, 161 const struct net_bridge_port *p, 162 struct net_bridge_vlan_group *vg, 163 struct net_bridge_vlan *v, 164 struct nlattr **tb, 165 bool *changed, 166 struct netlink_ext_ack *extack) 167 { 168 int err; 169 170 *changed = false; 171 if (tb[BRIDGE_VLANDB_ENTRY_STATE]) { 172 u8 state = nla_get_u8(tb[BRIDGE_VLANDB_ENTRY_STATE]); 173 174 err = br_vlan_modify_state(vg, v, state, changed, extack); 175 if (err) 176 return err; 177 } 178 if (tb[BRIDGE_VLANDB_ENTRY_TUNNEL_INFO]) { 179 err = br_vlan_modify_tunnel(p, v, tb, changed, extack); 180 if (err) 181 return err; 182 } 183 184 return 0; 185 } 186 187 int br_vlan_process_options(const struct net_bridge *br, 188 const struct net_bridge_port *p, 189 struct net_bridge_vlan *range_start, 190 struct net_bridge_vlan *range_end, 191 struct nlattr **tb, 192 struct netlink_ext_ack *extack) 193 { 194 struct net_bridge_vlan *v, *curr_start = NULL, *curr_end = NULL; 195 struct net_bridge_vlan_group *vg; 196 int vid, err = 0; 197 u16 pvid; 198 199 if (p) 200 vg = nbp_vlan_group(p); 201 else 202 vg = br_vlan_group(br); 203 204 if (!range_start || !br_vlan_should_use(range_start)) { 205 NL_SET_ERR_MSG_MOD(extack, "Vlan range start doesn't exist, can't process options"); 206 return -ENOENT; 207 } 208 if (!range_end || !br_vlan_should_use(range_end)) { 209 NL_SET_ERR_MSG_MOD(extack, "Vlan range end doesn't exist, can't process options"); 210 return -ENOENT; 211 } 212 213 pvid = br_get_pvid(vg); 214 for (vid = range_start->vid; vid <= range_end->vid; vid++) { 215 bool changed = false; 216 217 v = br_vlan_find(vg, vid); 218 if (!v || !br_vlan_should_use(v)) { 219 NL_SET_ERR_MSG_MOD(extack, "Vlan in range doesn't exist, can't process options"); 220 err = -ENOENT; 221 break; 222 } 223 224 err = br_vlan_process_one_opts(br, p, vg, v, tb, &changed, 225 extack); 226 if (err) 227 break; 228 229 if (changed) { 230 /* vlan options changed, check for range */ 231 if (!curr_start) { 232 curr_start = v; 233 curr_end = v; 234 continue; 235 } 236 237 if (v->vid == pvid || 238 !br_vlan_can_enter_range(v, curr_end)) { 239 br_vlan_notify(br, p, curr_start->vid, 240 curr_end->vid, RTM_NEWVLAN); 241 curr_start = v; 242 } 243 curr_end = v; 244 } else { 245 /* nothing changed and nothing to notify yet */ 246 if (!curr_start) 247 continue; 248 249 br_vlan_notify(br, p, curr_start->vid, curr_end->vid, 250 RTM_NEWVLAN); 251 curr_start = NULL; 252 curr_end = NULL; 253 } 254 } 255 if (curr_start) 256 br_vlan_notify(br, p, curr_start->vid, curr_end->vid, 257 RTM_NEWVLAN); 258 259 return err; 260 } 261 262 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr, 263 const struct net_bridge_vlan *r_end) 264 { 265 return v_curr->vid - r_end->vid == 1 && 266 ((v_curr->priv_flags ^ r_end->priv_flags) & 267 BR_VLFLAG_GLOBAL_MCAST_ENABLED) == 0 && 268 br_multicast_ctx_options_equal(&v_curr->br_mcast_ctx, 269 &r_end->br_mcast_ctx); 270 } 271 272 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range, 273 const struct net_bridge_vlan *v_opts) 274 { 275 struct nlattr *nest; 276 277 nest = nla_nest_start(skb, BRIDGE_VLANDB_GLOBAL_OPTIONS); 278 if (!nest) 279 return false; 280 281 if (nla_put_u16(skb, BRIDGE_VLANDB_GOPTS_ID, vid)) 282 goto out_err; 283 284 if (vid_range && vid < vid_range && 285 nla_put_u16(skb, BRIDGE_VLANDB_GOPTS_RANGE, vid_range)) 286 goto out_err; 287 288 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 289 if (nla_put_u8(skb, BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING, 290 !!(v_opts->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) || 291 nla_put_u8(skb, BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION, 292 v_opts->br_mcast_ctx.multicast_igmp_version) || 293 nla_put_u32(skb, BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT, 294 v_opts->br_mcast_ctx.multicast_last_member_count)) 295 goto out_err; 296 297 #if IS_ENABLED(CONFIG_IPV6) 298 if (nla_put_u8(skb, BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION, 299 v_opts->br_mcast_ctx.multicast_mld_version)) 300 goto out_err; 301 #endif 302 #endif 303 304 nla_nest_end(skb, nest); 305 306 return true; 307 308 out_err: 309 nla_nest_cancel(skb, nest); 310 return false; 311 } 312 313 static size_t rtnl_vlan_global_opts_nlmsg_size(void) 314 { 315 return NLMSG_ALIGN(sizeof(struct br_vlan_msg)) 316 + nla_total_size(0) /* BRIDGE_VLANDB_GLOBAL_OPTIONS */ 317 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_GOPTS_ID */ 318 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 319 + nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING */ 320 + nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION */ 321 + nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION */ 322 + nla_total_size(sizeof(u32)) /* BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT */ 323 #endif 324 + nla_total_size(sizeof(u16)); /* BRIDGE_VLANDB_GOPTS_RANGE */ 325 } 326 327 static void br_vlan_global_opts_notify(const struct net_bridge *br, 328 u16 vid, u16 vid_range) 329 { 330 struct net_bridge_vlan *v; 331 struct br_vlan_msg *bvm; 332 struct nlmsghdr *nlh; 333 struct sk_buff *skb; 334 int err = -ENOBUFS; 335 336 /* right now notifications are done only with rtnl held */ 337 ASSERT_RTNL(); 338 339 skb = nlmsg_new(rtnl_vlan_global_opts_nlmsg_size(), GFP_KERNEL); 340 if (!skb) 341 goto out_err; 342 343 err = -EMSGSIZE; 344 nlh = nlmsg_put(skb, 0, 0, RTM_NEWVLAN, sizeof(*bvm), 0); 345 if (!nlh) 346 goto out_err; 347 bvm = nlmsg_data(nlh); 348 memset(bvm, 0, sizeof(*bvm)); 349 bvm->family = AF_BRIDGE; 350 bvm->ifindex = br->dev->ifindex; 351 352 /* need to find the vlan due to flags/options */ 353 v = br_vlan_find(br_vlan_group(br), vid); 354 if (!v) 355 goto out_kfree; 356 357 if (!br_vlan_global_opts_fill(skb, vid, vid_range, v)) 358 goto out_err; 359 360 nlmsg_end(skb, nlh); 361 rtnl_notify(skb, dev_net(br->dev), 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL); 362 return; 363 364 out_err: 365 rtnl_set_sk_err(dev_net(br->dev), RTNLGRP_BRVLAN, err); 366 out_kfree: 367 kfree_skb(skb); 368 } 369 370 static int br_vlan_process_global_one_opts(const struct net_bridge *br, 371 struct net_bridge_vlan_group *vg, 372 struct net_bridge_vlan *v, 373 struct nlattr **tb, 374 bool *changed, 375 struct netlink_ext_ack *extack) 376 { 377 int err __maybe_unused; 378 379 *changed = false; 380 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING 381 if (tb[BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING]) { 382 u8 mc_snooping; 383 384 mc_snooping = nla_get_u8(tb[BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING]); 385 if (br_multicast_toggle_global_vlan(v, !!mc_snooping)) 386 *changed = true; 387 } 388 if (tb[BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION]) { 389 u8 ver; 390 391 ver = nla_get_u8(tb[BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION]); 392 err = br_multicast_set_igmp_version(&v->br_mcast_ctx, ver); 393 if (err) 394 return err; 395 *changed = true; 396 } 397 if (tb[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT]) { 398 u32 cnt; 399 400 cnt = nla_get_u32(tb[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT]); 401 v->br_mcast_ctx.multicast_last_member_count = cnt; 402 *changed = true; 403 } 404 #if IS_ENABLED(CONFIG_IPV6) 405 if (tb[BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION]) { 406 u8 ver; 407 408 ver = nla_get_u8(tb[BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION]); 409 err = br_multicast_set_mld_version(&v->br_mcast_ctx, ver); 410 if (err) 411 return err; 412 *changed = true; 413 } 414 #endif 415 #endif 416 417 return 0; 418 } 419 420 static const struct nla_policy br_vlan_db_gpol[BRIDGE_VLANDB_GOPTS_MAX + 1] = { 421 [BRIDGE_VLANDB_GOPTS_ID] = { .type = NLA_U16 }, 422 [BRIDGE_VLANDB_GOPTS_RANGE] = { .type = NLA_U16 }, 423 [BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING] = { .type = NLA_U8 }, 424 [BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION] = { .type = NLA_U8 }, 425 [BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION] = { .type = NLA_U8 }, 426 [BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 }, 427 }; 428 429 int br_vlan_rtm_process_global_options(struct net_device *dev, 430 const struct nlattr *attr, 431 int cmd, 432 struct netlink_ext_ack *extack) 433 { 434 struct net_bridge_vlan *v, *curr_start = NULL, *curr_end = NULL; 435 struct nlattr *tb[BRIDGE_VLANDB_GOPTS_MAX + 1]; 436 struct net_bridge_vlan_group *vg; 437 u16 vid, vid_range = 0; 438 struct net_bridge *br; 439 int err = 0; 440 441 if (cmd != RTM_NEWVLAN) { 442 NL_SET_ERR_MSG_MOD(extack, "Global vlan options support only set operation"); 443 return -EINVAL; 444 } 445 if (!netif_is_bridge_master(dev)) { 446 NL_SET_ERR_MSG_MOD(extack, "Global vlan options can only be set on bridge device"); 447 return -EINVAL; 448 } 449 br = netdev_priv(dev); 450 vg = br_vlan_group(br); 451 if (WARN_ON(!vg)) 452 return -ENODEV; 453 454 err = nla_parse_nested(tb, BRIDGE_VLANDB_GOPTS_MAX, attr, 455 br_vlan_db_gpol, extack); 456 if (err) 457 return err; 458 459 if (!tb[BRIDGE_VLANDB_GOPTS_ID]) { 460 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry id"); 461 return -EINVAL; 462 } 463 vid = nla_get_u16(tb[BRIDGE_VLANDB_GOPTS_ID]); 464 if (!br_vlan_valid_id(vid, extack)) 465 return -EINVAL; 466 467 if (tb[BRIDGE_VLANDB_GOPTS_RANGE]) { 468 vid_range = nla_get_u16(tb[BRIDGE_VLANDB_GOPTS_RANGE]); 469 if (!br_vlan_valid_id(vid_range, extack)) 470 return -EINVAL; 471 if (vid >= vid_range) { 472 NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id"); 473 return -EINVAL; 474 } 475 } else { 476 vid_range = vid; 477 } 478 479 for (; vid <= vid_range; vid++) { 480 bool changed = false; 481 482 v = br_vlan_find(vg, vid); 483 if (!v) { 484 NL_SET_ERR_MSG_MOD(extack, "Vlan in range doesn't exist, can't process global options"); 485 err = -ENOENT; 486 break; 487 } 488 489 err = br_vlan_process_global_one_opts(br, vg, v, tb, &changed, 490 extack); 491 if (err) 492 break; 493 494 if (changed) { 495 /* vlan options changed, check for range */ 496 if (!curr_start) { 497 curr_start = v; 498 curr_end = v; 499 continue; 500 } 501 502 if (!br_vlan_global_opts_can_enter_range(v, curr_end)) { 503 br_vlan_global_opts_notify(br, curr_start->vid, 504 curr_end->vid); 505 curr_start = v; 506 } 507 curr_end = v; 508 } else { 509 /* nothing changed and nothing to notify yet */ 510 if (!curr_start) 511 continue; 512 513 br_vlan_global_opts_notify(br, curr_start->vid, 514 curr_end->vid); 515 curr_start = NULL; 516 curr_end = NULL; 517 } 518 } 519 if (curr_start) 520 br_vlan_global_opts_notify(br, curr_start->vid, curr_end->vid); 521 522 return err; 523 } 524