1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com> 3 */ 4 5 #include <linux/ethtool.h> 6 #include <net/netdev_lock.h> 7 8 #include "ipvlan.h" 9 10 #if IS_ENABLED(CONFIG_IPVTAP) 11 void (*__ipvtap_dellink_ptr)(struct net *net, struct net_device *dev, 12 struct list_head *head); 13 EXPORT_SYMBOL(__ipvtap_dellink_ptr); 14 #endif 15 16 static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval, 17 struct netlink_ext_ack *extack) 18 { 19 struct ipvl_dev *ipvlan; 20 unsigned int flags; 21 int err; 22 23 ASSERT_RTNL(); 24 if (port->mode != nval) { 25 mutex_lock(&port->pnodes_lock); 26 27 list_for_each_entry(ipvlan, &port->ipvlans, pnode) { 28 flags = ipvlan->dev->flags; 29 if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) { 30 err = dev_change_flags(ipvlan->dev, 31 flags | IFF_NOARP, 32 extack); 33 } else { 34 err = dev_change_flags(ipvlan->dev, 35 flags & ~IFF_NOARP, 36 extack); 37 } 38 if (unlikely(err)) 39 goto fail; 40 } 41 if (nval == IPVLAN_MODE_L3S) { 42 /* New mode is L3S */ 43 err = ipvlan_l3s_register(port); 44 if (err) 45 goto fail; 46 } else if (port->mode == IPVLAN_MODE_L3S) { 47 /* Old mode was L3S */ 48 ipvlan_l3s_unregister(port); 49 } 50 port->mode = nval; 51 52 mutex_unlock(&port->pnodes_lock); 53 } 54 return 0; 55 56 fail: 57 /* Undo the flags changes that have been done so far. */ 58 list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) { 59 flags = ipvlan->dev->flags; 60 if (port->mode == IPVLAN_MODE_L3 || 61 port->mode == IPVLAN_MODE_L3S) 62 dev_change_flags(ipvlan->dev, flags | IFF_NOARP, 63 NULL); 64 else 65 dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP, 66 NULL); 67 } 68 69 mutex_unlock(&port->pnodes_lock); 70 71 return err; 72 } 73 74 static int ipvlan_port_create(struct net_device *dev) 75 { 76 struct ipvl_port *port; 77 int err, idx; 78 79 port = kzalloc_obj(struct ipvl_port); 80 if (!port) 81 return -ENOMEM; 82 83 write_pnet(&port->pnet, dev_net(dev)); 84 port->dev = dev; 85 port->mode = IPVLAN_MODE_L3; 86 INIT_LIST_HEAD(&port->ipvlans); 87 for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++) 88 INIT_HLIST_HEAD(&port->hlhead[idx]); 89 90 spin_lock_init(&port->addrs_lock); 91 mutex_init(&port->pnodes_lock); 92 skb_queue_head_init(&port->backlog); 93 INIT_WORK(&port->wq, ipvlan_process_multicast); 94 ida_init(&port->ida); 95 port->dev_id_start = 1; 96 97 err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port); 98 if (err) 99 goto err; 100 101 netdev_hold(dev, &port->dev_tracker, GFP_KERNEL); 102 103 return 0; 104 105 err: 106 kfree(port); 107 return err; 108 } 109 110 static void ipvlan_port_destroy(struct ipvl_port *port) 111 { 112 struct net_device *dev = port->dev; 113 struct sk_buff *skb; 114 115 if (port->mode == IPVLAN_MODE_L3S) 116 ipvlan_l3s_unregister(port); 117 118 netdev_rx_handler_unregister(dev); 119 cancel_work_sync(&port->wq); 120 netdev_put(dev, &port->dev_tracker); 121 122 while ((skb = __skb_dequeue(&port->backlog)) != NULL) { 123 dev_put(skb->dev); 124 kfree_skb(skb); 125 } 126 ida_destroy(&port->ida); 127 kfree(port); 128 } 129 130 static void ipvlan_port_put(struct ipvl_port *port) 131 { 132 if (refcount_dec_and_test(&port->count)) 133 ipvlan_port_destroy(port); 134 } 135 136 static struct ipvl_port *ipvlan_port_get(struct net_device *dev) 137 { 138 struct ipvl_port *port = NULL; 139 140 rcu_read_lock(); 141 if (netif_is_ipvlan_port(dev)) { 142 port = ipvlan_port_get_rcu(dev); 143 if (!refcount_inc_not_zero(&port->count)) 144 port = NULL; 145 } 146 rcu_read_unlock(); 147 148 return port; 149 } 150 151 #define IPVLAN_ALWAYS_ON_OFLOADS \ 152 (NETIF_F_SG | NETIF_F_HW_CSUM | \ 153 NETIF_F_GSO_ROBUST | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL) 154 155 #define IPVLAN_ALWAYS_ON \ 156 (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_VLAN_CHALLENGED) 157 158 #define IPVLAN_FEATURES \ 159 (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \ 160 NETIF_F_GSO | NETIF_F_ALL_TSO | NETIF_F_GSO_ROBUST | \ 161 NETIF_F_GRO | NETIF_F_RXCSUM | \ 162 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER) 163 164 /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */ 165 166 #define IPVLAN_STATE_MASK \ 167 ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT)) 168 169 static int ipvlan_init(struct net_device *dev) 170 { 171 struct ipvl_dev *ipvlan = netdev_priv(dev); 172 struct net_device *phy_dev = ipvlan->phy_dev; 173 struct ipvl_port *port; 174 int err; 175 176 dev->state = (dev->state & ~IPVLAN_STATE_MASK) | 177 (phy_dev->state & IPVLAN_STATE_MASK); 178 dev->features = phy_dev->features & IPVLAN_FEATURES; 179 dev->features |= IPVLAN_ALWAYS_ON; 180 dev->vlan_features = phy_dev->vlan_features & IPVLAN_FEATURES; 181 dev->vlan_features |= IPVLAN_ALWAYS_ON_OFLOADS; 182 dev->hw_enc_features |= dev->features; 183 dev->lltx = true; 184 netif_inherit_tso_max(dev, phy_dev); 185 dev->hard_header_len = phy_dev->hard_header_len; 186 187 netdev_lockdep_set_classes(dev); 188 189 ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats); 190 if (!ipvlan->pcpu_stats) 191 return -ENOMEM; 192 193 netdev_lock(phy_dev); 194 195 if (!netif_is_ipvlan_port(phy_dev)) { 196 err = ipvlan_port_create(phy_dev); 197 if (err < 0) { 198 netdev_unlock(phy_dev); 199 free_percpu(ipvlan->pcpu_stats); 200 return err; 201 } 202 port = ipvlan_port_get_rtnl(phy_dev); 203 refcount_set(&port->count, 1); 204 } else { 205 port = ipvlan_port_get_rtnl(phy_dev); 206 refcount_inc(&port->count); 207 } 208 209 netdev_unlock(phy_dev); 210 211 ipvlan->port = port; 212 213 return 0; 214 } 215 216 static void ipvlan_uninit(struct net_device *dev) 217 { 218 struct ipvl_dev *ipvlan = netdev_priv(dev); 219 netdevice_tracker dev_tracker; 220 struct net_device *phy_dev; 221 222 free_percpu(ipvlan->pcpu_stats); 223 224 phy_dev = ipvlan->phy_dev; 225 netdev_hold(phy_dev, &dev_tracker, GFP_KERNEL); 226 netdev_lock(phy_dev); 227 228 ipvlan_port_put(ipvlan->port); 229 230 netdev_unlock(phy_dev); 231 netdev_put(phy_dev, &dev_tracker); 232 } 233 234 static int ipvlan_open(struct net_device *dev) 235 { 236 struct ipvl_dev *ipvlan = netdev_priv(dev); 237 struct ipvl_port *port = ipvlan->port; 238 struct ipvl_addr *addr; 239 240 if (ipvlan->port->mode == IPVLAN_MODE_L3 || 241 ipvlan->port->mode == IPVLAN_MODE_L3S) 242 dev->flags |= IFF_NOARP; 243 else 244 dev->flags &= ~IFF_NOARP; 245 246 spin_lock_bh(&port->addrs_lock); 247 list_for_each_entry(addr, &ipvlan->addrs, anode) 248 ipvlan_ht_addr_add(ipvlan, addr); 249 spin_unlock_bh(&port->addrs_lock); 250 251 return 0; 252 } 253 254 static int ipvlan_stop(struct net_device *dev) 255 { 256 struct ipvl_dev *ipvlan = netdev_priv(dev); 257 struct net_device *phy_dev = ipvlan->phy_dev; 258 struct ipvl_addr *addr; 259 260 dev_uc_unsync(phy_dev, dev); 261 dev_mc_unsync(phy_dev, dev); 262 263 spin_lock_bh(&ipvlan->port->addrs_lock); 264 list_for_each_entry(addr, &ipvlan->addrs, anode) 265 ipvlan_ht_addr_del(addr); 266 spin_unlock_bh(&ipvlan->port->addrs_lock); 267 268 return 0; 269 } 270 271 static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb, 272 struct net_device *dev) 273 { 274 const struct ipvl_dev *ipvlan = netdev_priv(dev); 275 int skblen = skb->len; 276 int ret; 277 278 ret = ipvlan_queue_xmit(skb, dev); 279 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { 280 struct ipvl_pcpu_stats *pcptr; 281 282 pcptr = this_cpu_ptr(ipvlan->pcpu_stats); 283 284 u64_stats_update_begin(&pcptr->syncp); 285 u64_stats_inc(&pcptr->tx_pkts); 286 u64_stats_add(&pcptr->tx_bytes, skblen); 287 u64_stats_update_end(&pcptr->syncp); 288 } else { 289 this_cpu_inc(ipvlan->pcpu_stats->tx_drps); 290 } 291 return ret; 292 } 293 294 static netdev_features_t ipvlan_fix_features(struct net_device *dev, 295 netdev_features_t features) 296 { 297 struct ipvl_dev *ipvlan = netdev_priv(dev); 298 299 features |= NETIF_F_ALL_FOR_ALL; 300 features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES); 301 features = netdev_increment_features(ipvlan->phy_dev->features, 302 features, features); 303 features |= IPVLAN_ALWAYS_ON; 304 features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON); 305 306 return features; 307 } 308 309 static void ipvlan_change_rx_flags(struct net_device *dev, int change) 310 { 311 struct ipvl_dev *ipvlan = netdev_priv(dev); 312 struct net_device *phy_dev = ipvlan->phy_dev; 313 314 if (change & IFF_ALLMULTI) 315 dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1); 316 } 317 318 static void ipvlan_set_multicast_mac_filter(struct net_device *dev) 319 { 320 struct ipvl_dev *ipvlan = netdev_priv(dev); 321 322 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 323 bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE); 324 } else { 325 struct netdev_hw_addr *ha; 326 DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE); 327 328 bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE); 329 netdev_for_each_mc_addr(ha, dev) 330 __set_bit(ipvlan_mac_hash(ha->addr), mc_filters); 331 332 /* Turn-on broadcast bit irrespective of address family, 333 * since broadcast is deferred to a work-queue, hence no 334 * impact on fast-path processing. 335 */ 336 __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters); 337 338 bitmap_copy(ipvlan->mac_filters, mc_filters, 339 IPVLAN_MAC_FILTER_SIZE); 340 } 341 dev_uc_sync(ipvlan->phy_dev, dev); 342 dev_mc_sync(ipvlan->phy_dev, dev); 343 } 344 345 static void ipvlan_get_stats64(struct net_device *dev, 346 struct rtnl_link_stats64 *s) 347 { 348 struct ipvl_dev *ipvlan = netdev_priv(dev); 349 350 if (ipvlan->pcpu_stats) { 351 struct ipvl_pcpu_stats *pcptr; 352 u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes; 353 u32 rx_errs = 0, tx_drps = 0; 354 u32 strt; 355 int idx; 356 357 for_each_possible_cpu(idx) { 358 pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx); 359 do { 360 strt = u64_stats_fetch_begin(&pcptr->syncp); 361 rx_pkts = u64_stats_read(&pcptr->rx_pkts); 362 rx_bytes = u64_stats_read(&pcptr->rx_bytes); 363 rx_mcast = u64_stats_read(&pcptr->rx_mcast); 364 tx_pkts = u64_stats_read(&pcptr->tx_pkts); 365 tx_bytes = u64_stats_read(&pcptr->tx_bytes); 366 } while (u64_stats_fetch_retry(&pcptr->syncp, 367 strt)); 368 369 s->rx_packets += rx_pkts; 370 s->rx_bytes += rx_bytes; 371 s->multicast += rx_mcast; 372 s->tx_packets += tx_pkts; 373 s->tx_bytes += tx_bytes; 374 375 /* u32 values are updated without syncp protection. */ 376 rx_errs += READ_ONCE(pcptr->rx_errs); 377 tx_drps += READ_ONCE(pcptr->tx_drps); 378 } 379 s->rx_errors = rx_errs; 380 s->rx_dropped = rx_errs; 381 s->tx_dropped = tx_drps; 382 } 383 s->tx_errors = DEV_STATS_READ(dev, tx_errors); 384 } 385 386 static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid) 387 { 388 struct ipvl_dev *ipvlan = netdev_priv(dev); 389 struct net_device *phy_dev = ipvlan->phy_dev; 390 391 return vlan_vid_add(phy_dev, proto, vid); 392 } 393 394 static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 395 u16 vid) 396 { 397 struct ipvl_dev *ipvlan = netdev_priv(dev); 398 struct net_device *phy_dev = ipvlan->phy_dev; 399 400 vlan_vid_del(phy_dev, proto, vid); 401 return 0; 402 } 403 404 static int ipvlan_get_iflink(const struct net_device *dev) 405 { 406 struct ipvl_dev *ipvlan = netdev_priv(dev); 407 408 return READ_ONCE(ipvlan->phy_dev->ifindex); 409 } 410 411 static const struct net_device_ops ipvlan_netdev_ops = { 412 .ndo_init = ipvlan_init, 413 .ndo_uninit = ipvlan_uninit, 414 .ndo_open = ipvlan_open, 415 .ndo_stop = ipvlan_stop, 416 .ndo_start_xmit = ipvlan_start_xmit, 417 .ndo_fix_features = ipvlan_fix_features, 418 .ndo_change_rx_flags = ipvlan_change_rx_flags, 419 .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter, 420 .ndo_get_stats64 = ipvlan_get_stats64, 421 .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid, 422 .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid, 423 .ndo_get_iflink = ipvlan_get_iflink, 424 }; 425 426 static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev, 427 unsigned short type, const void *daddr, 428 const void *saddr, unsigned len) 429 { 430 const struct ipvl_dev *ipvlan = netdev_priv(dev); 431 struct net_device *phy_dev = ipvlan->phy_dev; 432 433 /* TODO Probably use a different field than dev_addr so that the 434 * mac-address on the virtual device is portable and can be carried 435 * while the packets use the mac-addr on the physical device. 436 */ 437 return dev_hard_header(skb, phy_dev, type, daddr, 438 saddr ? : phy_dev->dev_addr, len); 439 } 440 441 static const struct header_ops ipvlan_header_ops = { 442 .create = ipvlan_hard_header, 443 .parse = eth_header_parse, 444 .cache = eth_header_cache, 445 .cache_update = eth_header_cache_update, 446 .parse_protocol = eth_header_parse_protocol, 447 }; 448 449 static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev) 450 { 451 ipvlan->dev->mtu = dev->mtu; 452 } 453 454 static bool netif_is_ipvlan(const struct net_device *dev) 455 { 456 /* both ipvlan and ipvtap devices use the same netdev_ops */ 457 return dev->netdev_ops == &ipvlan_netdev_ops; 458 } 459 460 static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev, 461 struct ethtool_link_ksettings *cmd) 462 { 463 const struct ipvl_dev *ipvlan = netdev_priv(dev); 464 465 return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd); 466 } 467 468 static void ipvlan_ethtool_get_drvinfo(struct net_device *dev, 469 struct ethtool_drvinfo *drvinfo) 470 { 471 strscpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver)); 472 strscpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version)); 473 } 474 475 static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev) 476 { 477 const struct ipvl_dev *ipvlan = netdev_priv(dev); 478 479 return ipvlan->msg_enable; 480 } 481 482 static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value) 483 { 484 struct ipvl_dev *ipvlan = netdev_priv(dev); 485 486 ipvlan->msg_enable = value; 487 } 488 489 static const struct ethtool_ops ipvlan_ethtool_ops = { 490 .get_link = ethtool_op_get_link, 491 .get_link_ksettings = ipvlan_ethtool_get_link_ksettings, 492 .get_drvinfo = ipvlan_ethtool_get_drvinfo, 493 .get_msglevel = ipvlan_ethtool_get_msglevel, 494 .set_msglevel = ipvlan_ethtool_set_msglevel, 495 }; 496 497 static int ipvlan_nl_changelink(struct net_device *dev, 498 struct nlattr *tb[], struct nlattr *data[], 499 struct netlink_ext_ack *extack) 500 { 501 struct ipvl_dev *ipvlan = netdev_priv(dev); 502 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev); 503 int err = 0; 504 505 if (!data) 506 return 0; 507 if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN)) 508 return -EPERM; 509 510 if (data[IFLA_IPVLAN_MODE]) { 511 u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]); 512 513 err = ipvlan_set_port_mode(port, nmode, extack); 514 } 515 516 if (!err && data[IFLA_IPVLAN_FLAGS]) { 517 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]); 518 519 if (flags & IPVLAN_F_PRIVATE) 520 ipvlan_mark_private(port); 521 else 522 ipvlan_clear_private(port); 523 524 if (flags & IPVLAN_F_VEPA) 525 ipvlan_mark_vepa(port); 526 else 527 ipvlan_clear_vepa(port); 528 } 529 530 return err; 531 } 532 533 static size_t ipvlan_nl_getsize(const struct net_device *dev) 534 { 535 return (0 536 + nla_total_size(2) /* IFLA_IPVLAN_MODE */ 537 + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */ 538 ); 539 } 540 541 static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[], 542 struct netlink_ext_ack *extack) 543 { 544 if (!data) 545 return 0; 546 547 if (data[IFLA_IPVLAN_MODE]) { 548 u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]); 549 550 if (mode >= IPVLAN_MODE_MAX) 551 return -EINVAL; 552 } 553 if (data[IFLA_IPVLAN_FLAGS]) { 554 u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]); 555 556 /* Only two bits are used at this moment. */ 557 if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) 558 return -EINVAL; 559 /* Also both flags can't be active at the same time. */ 560 if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) == 561 (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) 562 return -EINVAL; 563 } 564 565 return 0; 566 } 567 568 static int ipvlan_nl_fillinfo(struct sk_buff *skb, 569 const struct net_device *dev) 570 { 571 struct ipvl_dev *ipvlan = netdev_priv(dev); 572 struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev); 573 int ret = -EINVAL; 574 575 if (!port) 576 goto err; 577 578 ret = -EMSGSIZE; 579 if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode)) 580 goto err; 581 if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags)) 582 goto err; 583 584 return 0; 585 586 err: 587 return ret; 588 } 589 590 int ipvlan_link_new(struct net_device *dev, struct rtnl_newlink_params *params, 591 struct netlink_ext_ack *extack) 592 { 593 struct net *link_net = rtnl_newlink_link_net(params); 594 struct ipvl_dev *ipvlan = netdev_priv(dev); 595 struct nlattr **data = params->data; 596 struct nlattr **tb = params->tb; 597 struct ipvl_port *port; 598 struct net_device *phy_dev; 599 int err; 600 u16 mode = IPVLAN_MODE_L3; 601 602 if (!tb[IFLA_LINK]) 603 return -EINVAL; 604 605 phy_dev = __dev_get_by_index(link_net, nla_get_u32(tb[IFLA_LINK])); 606 if (!phy_dev) 607 return -ENODEV; 608 609 if (netif_is_ipvlan(phy_dev)) { 610 struct ipvl_dev *tmp = netdev_priv(phy_dev); 611 612 phy_dev = tmp->phy_dev; 613 if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN)) 614 return -EPERM; 615 } else if (!netif_is_ipvlan_port(phy_dev)) { 616 /* Exit early if the underlying link is invalid or busy */ 617 if (phy_dev->type != ARPHRD_ETHER || 618 phy_dev->flags & IFF_LOOPBACK) { 619 netdev_err(phy_dev, 620 "Master is either lo or non-ether device\n"); 621 return -EINVAL; 622 } 623 624 if (netdev_is_rx_handler_busy(phy_dev)) { 625 netdev_err(phy_dev, "Device is already in use.\n"); 626 return -EBUSY; 627 } 628 } 629 630 ipvlan->phy_dev = phy_dev; 631 ipvlan->dev = dev; 632 ipvlan->sfeatures = IPVLAN_FEATURES; 633 if (!tb[IFLA_MTU]) 634 ipvlan_adjust_mtu(ipvlan, phy_dev); 635 INIT_LIST_HEAD(&ipvlan->addrs); 636 637 /* TODO Probably put random address here to be presented to the 638 * world but keep using the physical-dev address for the outgoing 639 * packets. 640 */ 641 eth_hw_addr_set(dev, phy_dev->dev_addr); 642 643 dev->priv_flags |= IFF_NO_RX_HANDLER; 644 645 err = register_netdevice(dev); 646 if (err < 0) 647 return err; 648 649 port = ipvlan->port; 650 651 /* If the port-id base is at the MAX value, then wrap it around and 652 * begin from 0x1 again. This may be due to a busy system where lots 653 * of slaves are getting created and deleted. 654 */ 655 if (port->dev_id_start == 0xFFFE) 656 port->dev_id_start = 0x1; 657 658 /* Since L2 address is shared among all IPvlan slaves including 659 * master, use unique 16 bit dev-ids to differentiate among them. 660 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each 661 * slave link [see addrconf_ifid_eui48()]. 662 */ 663 err = ida_alloc_range(&port->ida, port->dev_id_start, 0xFFFD, 664 GFP_KERNEL); 665 if (err < 0) 666 err = ida_alloc_range(&port->ida, 0x1, port->dev_id_start - 1, 667 GFP_KERNEL); 668 if (err < 0) 669 goto unregister_netdev; 670 dev->dev_id = err; 671 672 /* Increment id-base to the next slot for the future assignment */ 673 port->dev_id_start = err + 1; 674 675 err = netdev_upper_dev_link(phy_dev, dev, extack); 676 if (err) 677 goto remove_ida; 678 679 /* Flags are per port and latest update overrides. User has 680 * to be consistent in setting it just like the mode attribute. 681 */ 682 if (data && data[IFLA_IPVLAN_FLAGS]) 683 port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]); 684 685 if (data && data[IFLA_IPVLAN_MODE]) 686 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]); 687 688 err = ipvlan_set_port_mode(port, mode, extack); 689 if (err) 690 goto unlink_netdev; 691 692 mutex_lock(&port->pnodes_lock); 693 list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans); 694 mutex_unlock(&port->pnodes_lock); 695 696 netif_stacked_transfer_operstate(phy_dev, dev); 697 return 0; 698 699 unlink_netdev: 700 netdev_upper_dev_unlink(phy_dev, dev); 701 remove_ida: 702 ida_free(&port->ida, dev->dev_id); 703 unregister_netdev: 704 unregister_netdevice(dev); 705 return err; 706 } 707 EXPORT_SYMBOL_GPL(ipvlan_link_new); 708 709 void __ipvlan_link_delete(struct net *net, struct net_device *dev, 710 struct list_head *head) 711 { 712 struct ipvl_dev *ipvlan = netdev_priv(dev); 713 struct ipvl_addr *addr, *next; 714 715 spin_lock_bh(&ipvlan->port->addrs_lock); 716 list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) { 717 ipvlan_ht_addr_del(addr); 718 list_del_rcu(&addr->anode); 719 kfree_rcu(addr, rcu); 720 } 721 spin_unlock_bh(&ipvlan->port->addrs_lock); 722 723 ida_free(&ipvlan->port->ida, dev->dev_id); 724 list_del_rcu(&ipvlan->pnode); 725 unregister_netdevice_queue_net(net, dev, head); 726 netdev_upper_dev_unlink(ipvlan->phy_dev, dev); 727 } 728 EXPORT_SYMBOL(__ipvlan_link_delete); 729 730 static void ipvlan_link_delete(struct net_device *dev, struct list_head *head) 731 { 732 struct ipvl_dev *ipvlan = netdev_priv(dev); 733 734 mutex_lock(&ipvlan->port->pnodes_lock); 735 if (!ipvlan->dying) 736 __ipvlan_link_delete(dev_net(dev), dev, head); 737 mutex_unlock(&ipvlan->port->pnodes_lock); 738 } 739 740 void ipvlan_link_setup(struct net_device *dev) 741 { 742 ether_setup(dev); 743 744 dev->max_mtu = ETH_MAX_MTU; 745 dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING); 746 dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE; 747 dev->netdev_ops = &ipvlan_netdev_ops; 748 dev->needs_free_netdev = true; 749 dev->header_ops = &ipvlan_header_ops; 750 dev->ethtool_ops = &ipvlan_ethtool_ops; 751 } 752 EXPORT_SYMBOL_GPL(ipvlan_link_setup); 753 754 static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] = 755 { 756 [IFLA_IPVLAN_MODE] = { .type = NLA_U16 }, 757 [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 }, 758 }; 759 760 static struct net *ipvlan_get_link_net(const struct net_device *dev) 761 { 762 struct ipvl_dev *ipvlan = netdev_priv(dev); 763 764 return dev_net(ipvlan->phy_dev); 765 } 766 767 static struct rtnl_link_ops ipvlan_link_ops = { 768 .kind = "ipvlan", 769 .priv_size = sizeof(struct ipvl_dev), 770 771 .setup = ipvlan_link_setup, 772 .newlink = ipvlan_link_new, 773 .dellink = ipvlan_link_delete, 774 .get_link_net = ipvlan_get_link_net, 775 }; 776 777 int ipvlan_link_register(struct rtnl_link_ops *ops) 778 { 779 ops->get_size = ipvlan_nl_getsize; 780 ops->policy = ipvlan_nl_policy; 781 ops->validate = ipvlan_nl_validate; 782 ops->fill_info = ipvlan_nl_fillinfo; 783 ops->changelink = ipvlan_nl_changelink; 784 ops->maxtype = IFLA_IPVLAN_MAX; 785 return rtnl_link_register(ops); 786 } 787 EXPORT_SYMBOL_GPL(ipvlan_link_register); 788 789 static int ipvlan_device_event(struct notifier_block *unused, 790 unsigned long event, void *ptr) 791 { 792 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr); 793 struct netdev_notifier_pre_changeaddr_info *prechaddr_info; 794 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 795 struct ipvl_dev *ipvlan, *next; 796 int err, ret = NOTIFY_DONE; 797 struct ipvl_port *port; 798 LIST_HEAD(lst_kill); 799 800 if (event == NETDEV_PRECHANGEUPPER || 801 event == NETDEV_CHANGEUPPER) 802 return ret; 803 804 port = ipvlan_port_get(dev); 805 if (!port) 806 return ret; 807 808 mutex_lock(&port->pnodes_lock); 809 810 switch (event) { 811 case NETDEV_UP: 812 case NETDEV_DOWN: 813 case NETDEV_CHANGE: 814 list_for_each_entry(ipvlan, &port->ipvlans, pnode) 815 netif_stacked_transfer_operstate(ipvlan->phy_dev, 816 ipvlan->dev); 817 break; 818 819 case NETDEV_REGISTER: { 820 struct net *oldnet, *newnet = dev_net(dev); 821 822 oldnet = read_pnet(&port->pnet); 823 if (net_eq(newnet, oldnet)) 824 break; 825 826 write_pnet(&port->pnet, newnet); 827 828 if (port->mode == IPVLAN_MODE_L3S) 829 ipvlan_migrate_l3s_hook(oldnet, newnet); 830 break; 831 } 832 case NETDEV_UNREGISTER: { 833 struct net *net = dev_net(dev); 834 835 if (dev->reg_state != NETREG_UNREGISTERING) 836 break; 837 838 list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode) { 839 ipvlan->dying = true; 840 841 #if IS_ENABLED(CONFIG_IPVTAP) 842 if (ipvlan->dev->rtnl_link_ops != &ipvlan_link_ops) 843 __ipvtap_dellink_ptr(net, ipvlan->dev, &lst_kill); 844 else 845 #endif 846 __ipvlan_link_delete(net, ipvlan->dev, &lst_kill); 847 } 848 849 unregister_netdevice_many(&lst_kill); 850 break; 851 } 852 case NETDEV_FEAT_CHANGE: 853 list_for_each_entry(ipvlan, &port->ipvlans, pnode) { 854 netif_inherit_tso_max(ipvlan->dev, dev); 855 netdev_update_features(ipvlan->dev); 856 } 857 break; 858 859 case NETDEV_CHANGEMTU: 860 list_for_each_entry(ipvlan, &port->ipvlans, pnode) 861 ipvlan_adjust_mtu(ipvlan, dev); 862 break; 863 864 case NETDEV_PRE_CHANGEADDR: 865 prechaddr_info = ptr; 866 list_for_each_entry(ipvlan, &port->ipvlans, pnode) { 867 err = netif_pre_changeaddr_notify(ipvlan->dev, 868 prechaddr_info->dev_addr, 869 extack); 870 if (err) { 871 ret = notifier_from_errno(err); 872 break; 873 } 874 } 875 break; 876 877 case NETDEV_CHANGEADDR: 878 list_for_each_entry(ipvlan, &port->ipvlans, pnode) { 879 eth_hw_addr_set(ipvlan->dev, dev->dev_addr); 880 call_netdevice_notifiers(NETDEV_CHANGEADDR, ipvlan->dev); 881 } 882 break; 883 884 case NETDEV_PRE_TYPE_CHANGE: 885 /* Forbid underlying device to change its type. */ 886 ret = NOTIFY_BAD; 887 break; 888 889 case NETDEV_NOTIFY_PEERS: 890 case NETDEV_BONDING_FAILOVER: 891 case NETDEV_RESEND_IGMP: 892 list_for_each_entry(ipvlan, &port->ipvlans, pnode) 893 call_netdevice_notifiers(event, ipvlan->dev); 894 } 895 896 mutex_unlock(&port->pnodes_lock); 897 898 ipvlan_port_put(port); 899 900 return ret; 901 } 902 903 /* the caller must held the addrs lock */ 904 static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6) 905 { 906 struct ipvl_addr *addr; 907 908 assert_spin_locked(&ipvlan->port->addrs_lock); 909 910 addr = kzalloc_obj(struct ipvl_addr, GFP_ATOMIC); 911 if (!addr) 912 return -ENOMEM; 913 914 addr->master = ipvlan; 915 if (!is_v6) { 916 memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr)); 917 addr->atype = IPVL_IPV4; 918 #if IS_ENABLED(CONFIG_IPV6) 919 } else { 920 memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr)); 921 addr->atype = IPVL_IPV6; 922 #endif 923 } 924 925 list_add_tail_rcu(&addr->anode, &ipvlan->addrs); 926 927 /* If the interface is not up, the address will be added to the hash 928 * list by ipvlan_open. 929 */ 930 if (netif_running(ipvlan->dev)) 931 ipvlan_ht_addr_add(ipvlan, addr); 932 933 return 0; 934 } 935 936 static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6) 937 { 938 struct ipvl_addr *addr; 939 940 spin_lock_bh(&ipvlan->port->addrs_lock); 941 addr = ipvlan_find_addr(ipvlan, iaddr, is_v6); 942 if (!addr) { 943 spin_unlock_bh(&ipvlan->port->addrs_lock); 944 return; 945 } 946 947 ipvlan_ht_addr_del(addr); 948 list_del_rcu(&addr->anode); 949 spin_unlock_bh(&ipvlan->port->addrs_lock); 950 kfree_rcu(addr, rcu); 951 } 952 953 static bool ipvlan_is_valid_dev(const struct net_device *dev) 954 { 955 struct ipvl_dev *ipvlan = netdev_priv(dev); 956 957 if (!netif_is_ipvlan(dev)) 958 return false; 959 960 if (!ipvlan || !ipvlan->port) 961 return false; 962 963 return true; 964 } 965 966 #if IS_ENABLED(CONFIG_IPV6) 967 static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr) 968 { 969 int ret = -EINVAL; 970 971 spin_lock_bh(&ipvlan->port->addrs_lock); 972 if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) 973 netif_err(ipvlan, ifup, ipvlan->dev, 974 "Failed to add IPv6=%pI6c addr for %s intf\n", 975 ip6_addr, ipvlan->dev->name); 976 else 977 ret = ipvlan_add_addr(ipvlan, ip6_addr, true); 978 spin_unlock_bh(&ipvlan->port->addrs_lock); 979 return ret; 980 } 981 982 static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr) 983 { 984 return ipvlan_del_addr(ipvlan, ip6_addr, true); 985 } 986 987 static int ipvlan_addr6_event(struct notifier_block *unused, 988 unsigned long event, void *ptr) 989 { 990 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr; 991 struct net_device *dev = (struct net_device *)if6->idev->dev; 992 struct ipvl_dev *ipvlan = netdev_priv(dev); 993 994 if (!ipvlan_is_valid_dev(dev)) 995 return NOTIFY_DONE; 996 997 switch (event) { 998 case NETDEV_UP: 999 if (ipvlan_add_addr6(ipvlan, &if6->addr)) 1000 return NOTIFY_BAD; 1001 break; 1002 1003 case NETDEV_DOWN: 1004 ipvlan_del_addr6(ipvlan, &if6->addr); 1005 break; 1006 } 1007 1008 return NOTIFY_OK; 1009 } 1010 1011 static int ipvlan_addr6_validator_event(struct notifier_block *unused, 1012 unsigned long event, void *ptr) 1013 { 1014 struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr; 1015 struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev; 1016 struct ipvl_dev *ipvlan = netdev_priv(dev); 1017 int ret = NOTIFY_OK; 1018 1019 if (!ipvlan_is_valid_dev(dev)) 1020 return NOTIFY_DONE; 1021 1022 switch (event) { 1023 case NETDEV_UP: 1024 spin_lock_bh(&ipvlan->port->addrs_lock); 1025 if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) { 1026 NL_SET_ERR_MSG(i6vi->extack, 1027 "Address already assigned to an ipvlan device"); 1028 ret = notifier_from_errno(-EADDRINUSE); 1029 } 1030 spin_unlock_bh(&ipvlan->port->addrs_lock); 1031 break; 1032 } 1033 1034 return ret; 1035 } 1036 #endif 1037 1038 static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr) 1039 { 1040 int ret = -EINVAL; 1041 1042 spin_lock_bh(&ipvlan->port->addrs_lock); 1043 if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) 1044 netif_err(ipvlan, ifup, ipvlan->dev, 1045 "Failed to add IPv4=%pI4 on %s intf.\n", 1046 ip4_addr, ipvlan->dev->name); 1047 else 1048 ret = ipvlan_add_addr(ipvlan, ip4_addr, false); 1049 spin_unlock_bh(&ipvlan->port->addrs_lock); 1050 return ret; 1051 } 1052 1053 static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr) 1054 { 1055 return ipvlan_del_addr(ipvlan, ip4_addr, false); 1056 } 1057 1058 static int ipvlan_addr4_event(struct notifier_block *unused, 1059 unsigned long event, void *ptr) 1060 { 1061 struct in_ifaddr *if4 = (struct in_ifaddr *)ptr; 1062 struct net_device *dev = (struct net_device *)if4->ifa_dev->dev; 1063 struct ipvl_dev *ipvlan = netdev_priv(dev); 1064 struct in_addr ip4_addr; 1065 1066 if (!ipvlan_is_valid_dev(dev)) 1067 return NOTIFY_DONE; 1068 1069 switch (event) { 1070 case NETDEV_UP: 1071 ip4_addr.s_addr = if4->ifa_address; 1072 if (ipvlan_add_addr4(ipvlan, &ip4_addr)) 1073 return NOTIFY_BAD; 1074 break; 1075 1076 case NETDEV_DOWN: 1077 ip4_addr.s_addr = if4->ifa_address; 1078 ipvlan_del_addr4(ipvlan, &ip4_addr); 1079 break; 1080 } 1081 1082 return NOTIFY_OK; 1083 } 1084 1085 static int ipvlan_addr4_validator_event(struct notifier_block *unused, 1086 unsigned long event, void *ptr) 1087 { 1088 struct in_validator_info *ivi = (struct in_validator_info *)ptr; 1089 struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev; 1090 struct ipvl_dev *ipvlan = netdev_priv(dev); 1091 int ret = NOTIFY_OK; 1092 1093 if (!ipvlan_is_valid_dev(dev)) 1094 return NOTIFY_DONE; 1095 1096 switch (event) { 1097 case NETDEV_UP: 1098 spin_lock_bh(&ipvlan->port->addrs_lock); 1099 if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) { 1100 NL_SET_ERR_MSG(ivi->extack, 1101 "Address already assigned to an ipvlan device"); 1102 ret = notifier_from_errno(-EADDRINUSE); 1103 } 1104 spin_unlock_bh(&ipvlan->port->addrs_lock); 1105 break; 1106 } 1107 1108 return ret; 1109 } 1110 1111 static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = { 1112 .notifier_call = ipvlan_addr4_event, 1113 }; 1114 1115 static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = { 1116 .notifier_call = ipvlan_addr4_validator_event, 1117 }; 1118 1119 static struct notifier_block ipvlan_notifier_block __read_mostly = { 1120 .notifier_call = ipvlan_device_event, 1121 }; 1122 1123 #if IS_ENABLED(CONFIG_IPV6) 1124 static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = { 1125 .notifier_call = ipvlan_addr6_event, 1126 }; 1127 1128 static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = { 1129 .notifier_call = ipvlan_addr6_validator_event, 1130 }; 1131 #endif 1132 1133 static int __init ipvlan_init_module(void) 1134 { 1135 int err; 1136 1137 ipvlan_init_secret(); 1138 register_netdevice_notifier(&ipvlan_notifier_block); 1139 #if IS_ENABLED(CONFIG_IPV6) 1140 register_inet6addr_notifier(&ipvlan_addr6_notifier_block); 1141 register_inet6addr_validator_notifier( 1142 &ipvlan_addr6_vtor_notifier_block); 1143 #endif 1144 register_inetaddr_notifier(&ipvlan_addr4_notifier_block); 1145 register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block); 1146 1147 err = ipvlan_l3s_init(); 1148 if (err < 0) 1149 goto error; 1150 1151 err = ipvlan_link_register(&ipvlan_link_ops); 1152 if (err < 0) { 1153 ipvlan_l3s_cleanup(); 1154 goto error; 1155 } 1156 1157 return 0; 1158 error: 1159 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block); 1160 unregister_inetaddr_validator_notifier( 1161 &ipvlan_addr4_vtor_notifier_block); 1162 #if IS_ENABLED(CONFIG_IPV6) 1163 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block); 1164 unregister_inet6addr_validator_notifier( 1165 &ipvlan_addr6_vtor_notifier_block); 1166 #endif 1167 unregister_netdevice_notifier(&ipvlan_notifier_block); 1168 return err; 1169 } 1170 1171 static void __exit ipvlan_cleanup_module(void) 1172 { 1173 rtnl_link_unregister(&ipvlan_link_ops); 1174 ipvlan_l3s_cleanup(); 1175 unregister_netdevice_notifier(&ipvlan_notifier_block); 1176 unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block); 1177 unregister_inetaddr_validator_notifier( 1178 &ipvlan_addr4_vtor_notifier_block); 1179 #if IS_ENABLED(CONFIG_IPV6) 1180 unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block); 1181 unregister_inet6addr_validator_notifier( 1182 &ipvlan_addr6_vtor_notifier_block); 1183 #endif 1184 } 1185 1186 module_init(ipvlan_init_module); 1187 module_exit(ipvlan_cleanup_module); 1188 1189 MODULE_LICENSE("GPL"); 1190 MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>"); 1191 MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs"); 1192 MODULE_ALIAS_RTNL_LINK("ipvlan"); 1193 MODULE_IMPORT_NS("NETDEV_INTERNAL"); 1194