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