ipvlan_main.c 27.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6
/* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
 */

#include "ipvlan.h"

7 8
static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval,
				struct netlink_ext_ack *extack)
9 10
{
	struct ipvl_dev *ipvlan;
11 12
	unsigned int flags;
	int err;
13

M
Mahesh Bandewar 已提交
14
	ASSERT_RTNL();
15
	if (port->mode != nval) {
16 17 18 19
		list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
			flags = ipvlan->dev->flags;
			if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
				err = dev_change_flags(ipvlan->dev,
20 21
						       flags | IFF_NOARP,
						       extack);
22 23
			} else {
				err = dev_change_flags(ipvlan->dev,
24 25
						       flags & ~IFF_NOARP,
						       extack);
26 27 28 29
			}
			if (unlikely(err))
				goto fail;
		}
M
Mahesh Bandewar 已提交
30 31
		if (nval == IPVLAN_MODE_L3S) {
			/* New mode is L3S */
32 33
			err = ipvlan_l3s_register(port);
			if (err)
34
				goto fail;
M
Mahesh Bandewar 已提交
35 36
		} else if (port->mode == IPVLAN_MODE_L3S) {
			/* Old mode was L3S */
37
			ipvlan_l3s_unregister(port);
M
Mahesh Bandewar 已提交
38
		}
39 40
		port->mode = nval;
	}
41 42 43 44 45 46 47 48
	return 0;

fail:
	/* Undo the flags changes that have been done so far. */
	list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
		flags = ipvlan->dev->flags;
		if (port->mode == IPVLAN_MODE_L3 ||
		    port->mode == IPVLAN_MODE_L3S)
49 50
			dev_change_flags(ipvlan->dev, flags | IFF_NOARP,
					 NULL);
51
		else
52 53
			dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP,
					 NULL);
54 55
	}

M
Mahesh Bandewar 已提交
56
	return err;
57 58 59 60 61 62 63 64 65 66 67
}

static int ipvlan_port_create(struct net_device *dev)
{
	struct ipvl_port *port;
	int err, idx;

	port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
	if (!port)
		return -ENOMEM;

68
	write_pnet(&port->pnet, dev_net(dev));
69 70 71 72 73 74
	port->dev = dev;
	port->mode = IPVLAN_MODE_L3;
	INIT_LIST_HEAD(&port->ipvlans);
	for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
		INIT_HLIST_HEAD(&port->hlhead[idx]);

75 76
	skb_queue_head_init(&port->backlog);
	INIT_WORK(&port->wq, ipvlan_process_multicast);
77
	ida_init(&port->ida);
78
	port->dev_id_start = 1;
79

80 81 82 83 84 85 86
	err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
	if (err)
		goto err;

	return 0;

err:
87
	kfree(port);
88 89 90 91 92 93
	return err;
}

static void ipvlan_port_destroy(struct net_device *dev)
{
	struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
94
	struct sk_buff *skb;
95

96 97
	if (port->mode == IPVLAN_MODE_L3S)
		ipvlan_l3s_unregister(port);
98
	netdev_rx_handler_unregister(dev);
99
	cancel_work_sync(&port->wq);
100 101 102 103 104
	while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
		if (skb->dev)
			dev_put(skb->dev);
		kfree_skb(skb);
	}
105
	ida_destroy(&port->ida);
106
	kfree(port);
107 108 109
}

#define IPVLAN_FEATURES \
110
	(NETIF_F_SG | NETIF_F_CSUM_MASK | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
111
	 NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
112 113 114 115 116 117 118 119 120
	 NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
	 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)

#define IPVLAN_STATE_MASK \
	((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))

static int ipvlan_init(struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
121 122 123
	struct net_device *phy_dev = ipvlan->phy_dev;
	struct ipvl_port *port;
	int err;
124 125 126 127

	dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
		     (phy_dev->state & IPVLAN_STATE_MASK);
	dev->features = phy_dev->features & IPVLAN_FEATURES;
128
	dev->features |= NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED;
129
	dev->hw_enc_features |= dev->features;
130
	dev->gso_max_size = phy_dev->gso_max_size;
E
Eric Dumazet 已提交
131
	dev->gso_max_segs = phy_dev->gso_max_segs;
132 133
	dev->hard_header_len = phy_dev->hard_header_len;

134
	netdev_lockdep_set_classes(dev);
135

136
	ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
137 138 139
	if (!ipvlan->pcpu_stats)
		return -ENOMEM;

140 141 142 143 144 145 146 147
	if (!netif_is_ipvlan_port(phy_dev)) {
		err = ipvlan_port_create(phy_dev);
		if (err < 0) {
			free_percpu(ipvlan->pcpu_stats);
			return err;
		}
	}
	port = ipvlan_port_get_rtnl(phy_dev);
148
	port->count += 1;
149 150 151 152 153 154
	return 0;
}

static void ipvlan_uninit(struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
155 156
	struct net_device *phy_dev = ipvlan->phy_dev;
	struct ipvl_port *port;
157

158
	free_percpu(ipvlan->pcpu_stats);
159

160
	port = ipvlan_port_get_rtnl(phy_dev);
161 162 163 164 165 166 167 168 169 170 171
	port->count -= 1;
	if (!port->count)
		ipvlan_port_destroy(port->dev);
}

static int ipvlan_open(struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct net_device *phy_dev = ipvlan->phy_dev;
	struct ipvl_addr *addr;

M
Mahesh Bandewar 已提交
172 173
	if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
	    ipvlan->port->mode == IPVLAN_MODE_L3S)
174 175 176 177
		dev->flags |= IFF_NOARP;
	else
		dev->flags &= ~IFF_NOARP;

178 179
	rcu_read_lock();
	list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
180
		ipvlan_ht_addr_add(ipvlan, addr);
181
	rcu_read_unlock();
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196
	return dev_uc_add(phy_dev, phy_dev->dev_addr);
}

static int ipvlan_stop(struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct net_device *phy_dev = ipvlan->phy_dev;
	struct ipvl_addr *addr;

	dev_uc_unsync(phy_dev, dev);
	dev_mc_unsync(phy_dev, dev);

	dev_uc_del(phy_dev, phy_dev->dev_addr);

197 198
	rcu_read_lock();
	list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
199
		ipvlan_ht_addr_del(addr);
200
	rcu_read_unlock();
201

202 203 204
	return 0;
}

M
Mahesh Bandewar 已提交
205 206
static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
				     struct net_device *dev)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
{
	const struct ipvl_dev *ipvlan = netdev_priv(dev);
	int skblen = skb->len;
	int ret;

	ret = ipvlan_queue_xmit(skb, dev);
	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
		struct ipvl_pcpu_stats *pcptr;

		pcptr = this_cpu_ptr(ipvlan->pcpu_stats);

		u64_stats_update_begin(&pcptr->syncp);
		pcptr->tx_pkts++;
		pcptr->tx_bytes += skblen;
		u64_stats_update_end(&pcptr->syncp);
	} else {
		this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
	}
	return ret;
}

static netdev_features_t ipvlan_fix_features(struct net_device *dev,
					     netdev_features_t features)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);

	return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
}

static void ipvlan_change_rx_flags(struct net_device *dev, int change)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct net_device *phy_dev = ipvlan->phy_dev;

	if (change & IFF_ALLMULTI)
		dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
}

static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);

	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
		bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
	} else {
		struct netdev_hw_addr *ha;
		DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);

		bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
		netdev_for_each_mc_addr(ha, dev)
			__set_bit(ipvlan_mac_hash(ha->addr), mc_filters);

259 260 261 262 263 264
		/* Turn-on broadcast bit irrespective of address family,
		 * since broadcast is deferred to a work-queue, hence no
		 * impact on fast-path processing.
		 */
		__set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);

265 266 267 268 269 270 271
		bitmap_copy(ipvlan->mac_filters, mc_filters,
			    IPVLAN_MAC_FILTER_SIZE);
	}
	dev_uc_sync(ipvlan->phy_dev, dev);
	dev_mc_sync(ipvlan->phy_dev, dev);
}

272 273
static void ipvlan_get_stats64(struct net_device *dev,
			       struct rtnl_link_stats64 *s)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);

	if (ipvlan->pcpu_stats) {
		struct ipvl_pcpu_stats *pcptr;
		u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
		u32 rx_errs = 0, tx_drps = 0;
		u32 strt;
		int idx;

		for_each_possible_cpu(idx) {
			pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
			do {
				strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
				rx_pkts = pcptr->rx_pkts;
				rx_bytes = pcptr->rx_bytes;
				rx_mcast = pcptr->rx_mcast;
				tx_pkts = pcptr->tx_pkts;
				tx_bytes = pcptr->tx_bytes;
			} while (u64_stats_fetch_retry_irq(&pcptr->syncp,
							   strt));

			s->rx_packets += rx_pkts;
			s->rx_bytes += rx_bytes;
			s->multicast += rx_mcast;
			s->tx_packets += tx_pkts;
			s->tx_bytes += tx_bytes;

			/* u32 values are updated without syncp protection. */
			rx_errs += pcptr->rx_errs;
			tx_drps += pcptr->tx_drps;
		}
		s->rx_errors = rx_errs;
		s->rx_dropped = rx_errs;
		s->tx_dropped = tx_drps;
	}
}

static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct net_device *phy_dev = ipvlan->phy_dev;

	return vlan_vid_add(phy_dev, proto, vid);
}

static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
				   u16 vid)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct net_device *phy_dev = ipvlan->phy_dev;

	vlan_vid_del(phy_dev, proto, vid);
	return 0;
}

330 331 332 333 334 335 336
static int ipvlan_get_iflink(const struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);

	return ipvlan->phy_dev->ifindex;
}

337 338 339 340 341 342 343 344 345 346 347 348
static const struct net_device_ops ipvlan_netdev_ops = {
	.ndo_init		= ipvlan_init,
	.ndo_uninit		= ipvlan_uninit,
	.ndo_open		= ipvlan_open,
	.ndo_stop		= ipvlan_stop,
	.ndo_start_xmit		= ipvlan_start_xmit,
	.ndo_fix_features	= ipvlan_fix_features,
	.ndo_change_rx_flags	= ipvlan_change_rx_flags,
	.ndo_set_rx_mode	= ipvlan_set_multicast_mac_filter,
	.ndo_get_stats64	= ipvlan_get_stats64,
	.ndo_vlan_rx_add_vid	= ipvlan_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid	= ipvlan_vlan_rx_kill_vid,
349
	.ndo_get_iflink		= ipvlan_get_iflink,
350 351 352 353 354 355 356 357 358 359 360 361 362 363
};

static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
			      unsigned short type, const void *daddr,
			      const void *saddr, unsigned len)
{
	const struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct net_device *phy_dev = ipvlan->phy_dev;

	/* TODO Probably use a different field than dev_addr so that the
	 * mac-address on the virtual device is portable and can be carried
	 * while the packets use the mac-addr on the physical device.
	 */
	return dev_hard_header(skb, phy_dev, type, daddr,
364
			       saddr ? : phy_dev->dev_addr, len);
365 366 367 368 369 370 371 372 373
}

static const struct header_ops ipvlan_header_ops = {
	.create  	= ipvlan_hard_header,
	.parse		= eth_header_parse,
	.cache		= eth_header_cache,
	.cache_update	= eth_header_cache_update,
};

374 375 376 377 378
static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
{
	ipvlan->dev->mtu = dev->mtu;
}

P
Paolo Abeni 已提交
379 380 381 382 383 384
static bool netif_is_ipvlan(const struct net_device *dev)
{
	/* both ipvlan and ipvtap devices use the same netdev_ops */
	return dev->netdev_ops == &ipvlan_netdev_ops;
}

385 386
static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
					     struct ethtool_link_ksettings *cmd)
387 388 389
{
	const struct ipvl_dev *ipvlan = netdev_priv(dev);

390
	return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
}

static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
				       struct ethtool_drvinfo *drvinfo)
{
	strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
}

static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
{
	const struct ipvl_dev *ipvlan = netdev_priv(dev);

	return ipvlan->msg_enable;
}

static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);

	ipvlan->msg_enable = value;
}

static const struct ethtool_ops ipvlan_ethtool_ops = {
	.get_link	= ethtool_op_get_link,
416
	.get_link_ksettings	= ipvlan_ethtool_get_link_ksettings,
417 418 419 420 421 422
	.get_drvinfo	= ipvlan_ethtool_get_drvinfo,
	.get_msglevel	= ipvlan_ethtool_get_msglevel,
	.set_msglevel	= ipvlan_ethtool_set_msglevel,
};

static int ipvlan_nl_changelink(struct net_device *dev,
423 424
				struct nlattr *tb[], struct nlattr *data[],
				struct netlink_ext_ack *extack)
425 426 427
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
M
Mahesh Bandewar 已提交
428
	int err = 0;
429

430 431
	if (!data)
		return 0;
432 433
	if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
		return -EPERM;
434 435

	if (data[IFLA_IPVLAN_MODE]) {
436 437
		u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);

438
		err = ipvlan_set_port_mode(port, nmode, extack);
439
	}
440 441 442 443 444 445 446 447

	if (!err && data[IFLA_IPVLAN_FLAGS]) {
		u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);

		if (flags & IPVLAN_F_PRIVATE)
			ipvlan_mark_private(port);
		else
			ipvlan_clear_private(port);
M
Mahesh Bandewar 已提交
448 449 450 451 452

		if (flags & IPVLAN_F_VEPA)
			ipvlan_mark_vepa(port);
		else
			ipvlan_clear_vepa(port);
453 454
	}

M
Mahesh Bandewar 已提交
455
	return err;
456 457 458 459 460 461
}

static size_t ipvlan_nl_getsize(const struct net_device *dev)
{
	return (0
		+ nla_total_size(2) /* IFLA_IPVLAN_MODE */
462
		+ nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
463 464 465
		);
}

466 467
static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
			      struct netlink_ext_ack *extack)
468
{
469 470 471 472
	if (!data)
		return 0;

	if (data[IFLA_IPVLAN_MODE]) {
473 474
		u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);

Y
YueHaibing 已提交
475
		if (mode >= IPVLAN_MODE_MAX)
476 477
			return -EINVAL;
	}
478 479 480
	if (data[IFLA_IPVLAN_FLAGS]) {
		u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);

M
Mahesh Bandewar 已提交
481 482 483 484 485 486
		/* Only two bits are used at this moment. */
		if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
			return -EINVAL;
		/* Also both flags can't be active at the same time. */
		if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
		    (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
487 488 489
			return -EINVAL;
	}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	return 0;
}

static int ipvlan_nl_fillinfo(struct sk_buff *skb,
			      const struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
	int ret = -EINVAL;

	if (!port)
		goto err;

	ret = -EMSGSIZE;
	if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
		goto err;
506 507
	if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
		goto err;
508 509 510 511 512 513 514

	return 0;

err:
	return ret;
}

515
int ipvlan_link_new(struct net *src_net, struct net_device *dev,
516 517
		    struct nlattr *tb[], struct nlattr *data[],
		    struct netlink_ext_ack *extack)
518 519 520 521 522
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct ipvl_port *port;
	struct net_device *phy_dev;
	int err;
M
Mahesh Bandewar 已提交
523
	u16 mode = IPVLAN_MODE_L3;
524 525 526 527 528 529 530 531

	if (!tb[IFLA_LINK])
		return -EINVAL;

	phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
	if (!phy_dev)
		return -ENODEV;

532
	if (netif_is_ipvlan(phy_dev)) {
533 534 535
		struct ipvl_dev *tmp = netdev_priv(phy_dev);

		phy_dev = tmp->phy_dev;
536 537
		if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
			return -EPERM;
538
	} else if (!netif_is_ipvlan_port(phy_dev)) {
539 540 541 542 543 544 545
		/* Exit early if the underlying link is invalid or busy */
		if (phy_dev->type != ARPHRD_ETHER ||
		    phy_dev->flags & IFF_LOOPBACK) {
			netdev_err(phy_dev,
				   "Master is either lo or non-ether device\n");
			return -EINVAL;
		}
546

547 548 549 550 551
		if (netdev_is_rx_handler_busy(phy_dev)) {
			netdev_err(phy_dev, "Device is already in use.\n");
			return -EBUSY;
		}
	}
552 553 554 555

	ipvlan->phy_dev = phy_dev;
	ipvlan->dev = dev;
	ipvlan->sfeatures = IPVLAN_FEATURES;
556 557
	if (!tb[IFLA_MTU])
		ipvlan_adjust_mtu(ipvlan, phy_dev);
558
	INIT_LIST_HEAD(&ipvlan->addrs);
559
	spin_lock_init(&ipvlan->addrs_lock);
560

561 562 563
	/* TODO Probably put random address here to be presented to the
	 * world but keep using the physical-dev address for the outgoing
	 * packets.
564
	 */
565 566
	memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);

P
Paolo Abeni 已提交
567 568
	dev->priv_flags |= IFF_NO_RX_HANDLER;

569 570 571 572 573 574 575
	err = register_netdevice(dev);
	if (err < 0)
		return err;

	/* ipvlan_init() would have created the port, if required */
	port = ipvlan_port_get_rtnl(phy_dev);
	ipvlan->port = port;
576

577 578 579 580 581 582 583
	/* If the port-id base is at the MAX value, then wrap it around and
	 * begin from 0x1 again. This may be due to a busy system where lots
	 * of slaves are getting created and deleted.
	 */
	if (port->dev_id_start == 0xFFFE)
		port->dev_id_start = 0x1;

584 585 586 587 588
	/* Since L2 address is shared among all IPvlan slaves including
	 * master, use unique 16 bit dev-ids to diffentiate among them.
	 * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
	 * slave link [see addrconf_ifid_eui48()].
	 */
589 590
	err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
			     GFP_KERNEL);
591 592 593
	if (err < 0)
		err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
				     GFP_KERNEL);
594
	if (err < 0)
595
		goto unregister_netdev;
596
	dev->dev_id = err;
597

598 599
	/* Increment id-base to the next slot for the future assignment */
	port->dev_id_start = err + 1;
600

601 602 603
	err = netdev_upper_dev_link(phy_dev, dev, extack);
	if (err)
		goto remove_ida;
604

605 606 607 608 609
	/* Flags are per port and latest update overrides. User has
	 * to be consistent in setting it just like the mode attribute.
	 */
	if (data && data[IFLA_IPVLAN_FLAGS])
		port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
610

611 612
	if (data && data[IFLA_IPVLAN_MODE])
		mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
613

614
	err = ipvlan_set_port_mode(port, mode, extack);
615
	if (err)
616
		goto unlink_netdev;
617 618 619 620

	list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
	netif_stacked_transfer_operstate(phy_dev, dev);
	return 0;
621

622 623
unlink_netdev:
	netdev_upper_dev_unlink(phy_dev, dev);
624 625
remove_ida:
	ida_simple_remove(&port->ida, dev->dev_id);
626 627
unregister_netdev:
	unregister_netdevice(dev);
628
	return err;
629
}
630
EXPORT_SYMBOL_GPL(ipvlan_link_new);
631

632
void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
633 634 635 636
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct ipvl_addr *addr, *next;

637
	spin_lock_bh(&ipvlan->addrs_lock);
638
	list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
639
		ipvlan_ht_addr_del(addr);
640
		list_del_rcu(&addr->anode);
641
		kfree_rcu(addr, rcu);
642
	}
643
	spin_unlock_bh(&ipvlan->addrs_lock);
644

645
	ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
646 647 648 649
	list_del_rcu(&ipvlan->pnode);
	unregister_netdevice_queue(dev, head);
	netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
}
650
EXPORT_SYMBOL_GPL(ipvlan_link_delete);
651

652
void ipvlan_link_setup(struct net_device *dev)
653 654 655
{
	ether_setup(dev);

X
Xin Long 已提交
656
	dev->max_mtu = ETH_MAX_MTU;
657
	dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
658
	dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
659
	dev->netdev_ops = &ipvlan_netdev_ops;
660
	dev->needs_free_netdev = true;
661 662 663
	dev->header_ops = &ipvlan_header_ops;
	dev->ethtool_ops = &ipvlan_ethtool_ops;
}
664
EXPORT_SYMBOL_GPL(ipvlan_link_setup);
665 666 667 668

static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
{
	[IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
669
	[IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
670 671 672 673 674 675 676 677 678 679 680
};

static struct rtnl_link_ops ipvlan_link_ops = {
	.kind		= "ipvlan",
	.priv_size	= sizeof(struct ipvl_dev),

	.setup		= ipvlan_link_setup,
	.newlink	= ipvlan_link_new,
	.dellink	= ipvlan_link_delete,
};

681
int ipvlan_link_register(struct rtnl_link_ops *ops)
682
{
683 684 685 686 687 688
	ops->get_size	= ipvlan_nl_getsize;
	ops->policy	= ipvlan_nl_policy;
	ops->validate	= ipvlan_nl_validate;
	ops->fill_info	= ipvlan_nl_fillinfo;
	ops->changelink = ipvlan_nl_changelink;
	ops->maxtype	= IFLA_IPVLAN_MAX;
689 690
	return rtnl_link_register(ops);
}
691
EXPORT_SYMBOL_GPL(ipvlan_link_register);
692 693 694 695

static int ipvlan_device_event(struct notifier_block *unused,
			       unsigned long event, void *ptr)
{
696 697
	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
	struct netdev_notifier_pre_changeaddr_info *prechaddr_info;
698 699 700 701
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
	struct ipvl_dev *ipvlan, *next;
	struct ipvl_port *port;
	LIST_HEAD(lst_kill);
702
	int err;
703

704
	if (!netif_is_ipvlan_port(dev))
705 706 707 708 709 710 711 712 713 714 715
		return NOTIFY_DONE;

	port = ipvlan_port_get_rtnl(dev);

	switch (event) {
	case NETDEV_CHANGE:
		list_for_each_entry(ipvlan, &port->ipvlans, pnode)
			netif_stacked_transfer_operstate(ipvlan->phy_dev,
							 ipvlan->dev);
		break;

716 717 718 719 720 721 722 723 724
	case NETDEV_REGISTER: {
		struct net *oldnet, *newnet = dev_net(dev);

		oldnet = read_pnet(&port->pnet);
		if (net_eq(newnet, oldnet))
			break;

		write_pnet(&port->pnet, newnet);

725
		ipvlan_migrate_l3s_hook(oldnet, newnet);
726 727
		break;
	}
728 729 730 731
	case NETDEV_UNREGISTER:
		if (dev->reg_state != NETREG_UNREGISTERING)
			break;

732
		list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
733 734 735 736 737 738 739 740 741
			ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
							    &lst_kill);
		unregister_netdevice_many(&lst_kill);
		break;

	case NETDEV_FEAT_CHANGE:
		list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
			ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
			ipvlan->dev->gso_max_size = dev->gso_max_size;
E
Eric Dumazet 已提交
742
			ipvlan->dev->gso_max_segs = dev->gso_max_segs;
743 744 745 746 747 748 749 750 751
			netdev_features_change(ipvlan->dev);
		}
		break;

	case NETDEV_CHANGEMTU:
		list_for_each_entry(ipvlan, &port->ipvlans, pnode)
			ipvlan_adjust_mtu(ipvlan, dev);
		break;

752 753 754 755 756 757 758 759 760 761 762
	case NETDEV_PRE_CHANGEADDR:
		prechaddr_info = ptr;
		list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
			err = dev_pre_changeaddr_notify(ipvlan->dev,
						    prechaddr_info->dev_addr,
						    extack);
			if (err)
				return notifier_from_errno(err);
		}
		break;

763
	case NETDEV_CHANGEADDR:
764
		list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
765
			ether_addr_copy(ipvlan->dev->dev_addr, dev->dev_addr);
766 767
			call_netdevice_notifiers(NETDEV_CHANGEADDR, ipvlan->dev);
		}
768 769
		break;

770 771 772 773 774 775 776
	case NETDEV_PRE_TYPE_CHANGE:
		/* Forbid underlying device to change its type. */
		return NOTIFY_BAD;
	}
	return NOTIFY_DONE;
}

777
/* the caller must held the addrs lock */
778
static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
779 780 781 782 783 784 785 786
{
	struct ipvl_addr *addr;

	addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
	if (!addr)
		return -ENOMEM;

	addr->master = ipvlan;
M
Matteo Croce 已提交
787
	if (!is_v6) {
788 789
		memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
		addr->atype = IPVL_IPV4;
M
Matteo Croce 已提交
790 791 792 793 794
#if IS_ENABLED(CONFIG_IPV6)
	} else {
		memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
		addr->atype = IPVL_IPV6;
#endif
795
	}
796 797

	list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
798

799 800 801 802 803
	/* If the interface is not up, the address will be added to the hash
	 * list by ipvlan_open.
	 */
	if (netif_running(ipvlan->dev))
		ipvlan_ht_addr_add(ipvlan, addr);
804 805 806 807

	return 0;
}

808
static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
809 810 811
{
	struct ipvl_addr *addr;

812
	spin_lock_bh(&ipvlan->addrs_lock);
813
	addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
814 815
	if (!addr) {
		spin_unlock_bh(&ipvlan->addrs_lock);
816
		return;
817
	}
818

819
	ipvlan_ht_addr_del(addr);
820 821
	list_del_rcu(&addr->anode);
	spin_unlock_bh(&ipvlan->addrs_lock);
822 823 824
	kfree_rcu(addr, rcu);
}

M
Matteo Croce 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838
static bool ipvlan_is_valid_dev(const struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);

	if (!netif_is_ipvlan(dev))
		return false;

	if (!ipvlan || !ipvlan->port)
		return false;

	return true;
}

#if IS_ENABLED(CONFIG_IPV6)
839 840
static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
{
841 842 843 844
	int ret = -EINVAL;

	spin_lock_bh(&ipvlan->addrs_lock);
	if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true))
845 846 847
		netif_err(ipvlan, ifup, ipvlan->dev,
			  "Failed to add IPv6=%pI6c addr for %s intf\n",
			  ip6_addr, ipvlan->dev->name);
848 849 850 851
	else
		ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
	spin_unlock_bh(&ipvlan->addrs_lock);
	return ret;
852 853 854 855 856 857 858
}

static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
{
	return ipvlan_del_addr(ipvlan, ip6_addr, true);
}

859 860 861 862 863 864 865
static int ipvlan_addr6_event(struct notifier_block *unused,
			      unsigned long event, void *ptr)
{
	struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
	struct net_device *dev = (struct net_device *)if6->idev->dev;
	struct ipvl_dev *ipvlan = netdev_priv(dev);

866
	if (!ipvlan_is_valid_dev(dev))
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
		return NOTIFY_DONE;

	switch (event) {
	case NETDEV_UP:
		if (ipvlan_add_addr6(ipvlan, &if6->addr))
			return NOTIFY_BAD;
		break;

	case NETDEV_DOWN:
		ipvlan_del_addr6(ipvlan, &if6->addr);
		break;
	}

	return NOTIFY_OK;
}

883 884 885 886 887 888 889
static int ipvlan_addr6_validator_event(struct notifier_block *unused,
					unsigned long event, void *ptr)
{
	struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
	struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
	struct ipvl_dev *ipvlan = netdev_priv(dev);

890
	if (!ipvlan_is_valid_dev(dev))
891 892 893 894
		return NOTIFY_DONE;

	switch (event) {
	case NETDEV_UP:
895 896 897
		if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
			NL_SET_ERR_MSG(i6vi->extack,
				       "Address already assigned to an ipvlan device");
898
			return notifier_from_errno(-EADDRINUSE);
899
		}
900 901 902 903 904
		break;
	}

	return NOTIFY_OK;
}
M
Matteo Croce 已提交
905
#endif
906

907 908
static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
{
909 910 911 912
	int ret = -EINVAL;

	spin_lock_bh(&ipvlan->addrs_lock);
	if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false))
913 914 915
		netif_err(ipvlan, ifup, ipvlan->dev,
			  "Failed to add IPv4=%pI4 on %s intf.\n",
			  ip4_addr, ipvlan->dev->name);
916 917 918 919
	else
		ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
	spin_unlock_bh(&ipvlan->addrs_lock);
	return ret;
920 921 922 923
}

static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
{
924
	return ipvlan_del_addr(ipvlan, ip4_addr, false);
925 926 927 928 929 930 931 932 933 934
}

static int ipvlan_addr4_event(struct notifier_block *unused,
			      unsigned long event, void *ptr)
{
	struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
	struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
	struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct in_addr ip4_addr;

935
	if (!ipvlan_is_valid_dev(dev))
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
		return NOTIFY_DONE;

	switch (event) {
	case NETDEV_UP:
		ip4_addr.s_addr = if4->ifa_address;
		if (ipvlan_add_addr4(ipvlan, &ip4_addr))
			return NOTIFY_BAD;
		break;

	case NETDEV_DOWN:
		ip4_addr.s_addr = if4->ifa_address;
		ipvlan_del_addr4(ipvlan, &ip4_addr);
		break;
	}

	return NOTIFY_OK;
}

954 955 956 957 958 959 960
static int ipvlan_addr4_validator_event(struct notifier_block *unused,
					unsigned long event, void *ptr)
{
	struct in_validator_info *ivi = (struct in_validator_info *)ptr;
	struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
	struct ipvl_dev *ipvlan = netdev_priv(dev);

961
	if (!ipvlan_is_valid_dev(dev))
962 963 964 965
		return NOTIFY_DONE;

	switch (event) {
	case NETDEV_UP:
966 967 968
		if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
			NL_SET_ERR_MSG(ivi->extack,
				       "Address already assigned to an ipvlan device");
969
			return notifier_from_errno(-EADDRINUSE);
970
		}
971 972 973 974 975 976
		break;
	}

	return NOTIFY_OK;
}

977 978 979 980
static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
	.notifier_call = ipvlan_addr4_event,
};

981 982 983 984
static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
	.notifier_call = ipvlan_addr4_validator_event,
};

985 986 987 988
static struct notifier_block ipvlan_notifier_block __read_mostly = {
	.notifier_call = ipvlan_device_event,
};

M
Matteo Croce 已提交
989
#if IS_ENABLED(CONFIG_IPV6)
990 991 992 993
static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
	.notifier_call = ipvlan_addr6_event,
};

994 995 996
static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
	.notifier_call = ipvlan_addr6_validator_event,
};
M
Matteo Croce 已提交
997
#endif
998

999 1000 1001 1002 1003 1004
static int __init ipvlan_init_module(void)
{
	int err;

	ipvlan_init_secret();
	register_netdevice_notifier(&ipvlan_notifier_block);
M
Matteo Croce 已提交
1005
#if IS_ENABLED(CONFIG_IPV6)
1006
	register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1007 1008
	register_inet6addr_validator_notifier(
	    &ipvlan_addr6_vtor_notifier_block);
M
Matteo Croce 已提交
1009
#endif
1010
	register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1011
	register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
1012

1013
	err = ipvlan_l3s_init();
1014 1015 1016
	if (err < 0)
		goto error;

1017 1018
	err = ipvlan_link_register(&ipvlan_link_ops);
	if (err < 0) {
1019
		ipvlan_l3s_cleanup();
1020 1021 1022
		goto error;
	}

1023 1024 1025
	return 0;
error:
	unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1026 1027
	unregister_inetaddr_validator_notifier(
	    &ipvlan_addr4_vtor_notifier_block);
M
Matteo Croce 已提交
1028
#if IS_ENABLED(CONFIG_IPV6)
1029
	unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1030 1031
	unregister_inet6addr_validator_notifier(
	    &ipvlan_addr6_vtor_notifier_block);
M
Matteo Croce 已提交
1032
#endif
1033 1034 1035 1036 1037 1038 1039
	unregister_netdevice_notifier(&ipvlan_notifier_block);
	return err;
}

static void __exit ipvlan_cleanup_module(void)
{
	rtnl_link_unregister(&ipvlan_link_ops);
1040
	ipvlan_l3s_cleanup();
1041 1042
	unregister_netdevice_notifier(&ipvlan_notifier_block);
	unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
1043 1044
	unregister_inetaddr_validator_notifier(
	    &ipvlan_addr4_vtor_notifier_block);
M
Matteo Croce 已提交
1045
#if IS_ENABLED(CONFIG_IPV6)
1046
	unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
1047 1048
	unregister_inet6addr_validator_notifier(
	    &ipvlan_addr6_vtor_notifier_block);
M
Matteo Croce 已提交
1049
#endif
1050 1051 1052 1053 1054 1055 1056 1057 1058
}

module_init(ipvlan_init_module);
module_exit(ipvlan_cleanup_module);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
MODULE_ALIAS_RTNL_LINK("ipvlan");