File Index
Files and directories
Sign in
FILE

ipvlan_main.c

File preview with syntax highlighting.

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