ipvlan_core.c 22.0 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
static u32 ipvlan_jhash_secret __read_mostly;
8 9 10 11 12 13

void ipvlan_init_secret(void)
{
	net_get_random_once(&ipvlan_jhash_secret, sizeof(ipvlan_jhash_secret));
}

14
void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
			    unsigned int len, bool success, bool mcast)
{
	if (likely(success)) {
		struct ipvl_pcpu_stats *pcptr;

		pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
		u64_stats_update_begin(&pcptr->syncp);
		pcptr->rx_pkts++;
		pcptr->rx_bytes += len;
		if (mcast)
			pcptr->rx_mcast++;
		u64_stats_update_end(&pcptr->syncp);
	} else {
		this_cpu_inc(ipvlan->pcpu_stats->rx_errs);
	}
}
31
EXPORT_SYMBOL_GPL(ipvlan_count_rx);
32

M
Matteo Croce 已提交
33
#if IS_ENABLED(CONFIG_IPV6)
34 35 36 37 38 39 40
static u8 ipvlan_get_v6_hash(const void *iaddr)
{
	const struct in6_addr *ip6_addr = iaddr;

	return __ipv6_addr_jhash(ip6_addr, ipvlan_jhash_secret) &
	       IPVLAN_HASH_MASK;
}
M
Matteo Croce 已提交
41 42 43 44 45 46
#else
static u8 ipvlan_get_v6_hash(const void *iaddr)
{
	return 0;
}
#endif
47 48 49 50 51 52 53 54 55

static u8 ipvlan_get_v4_hash(const void *iaddr)
{
	const struct in_addr *ip4_addr = iaddr;

	return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret) &
	       IPVLAN_HASH_MASK;
}

M
Matteo Croce 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
static bool addr_equal(bool is_v6, struct ipvl_addr *addr, const void *iaddr)
{
	if (!is_v6 && addr->atype == IPVL_IPV4) {
		struct in_addr *i4addr = (struct in_addr *)iaddr;

		return addr->ip4addr.s_addr == i4addr->s_addr;
#if IS_ENABLED(CONFIG_IPV6)
	} else if (is_v6 && addr->atype == IPVL_IPV6) {
		struct in6_addr *i6addr = (struct in6_addr *)iaddr;

		return ipv6_addr_equal(&addr->ip6addr, i6addr);
#endif
	}

	return false;
}

M
Mahesh Bandewar 已提交
73 74
static struct ipvl_addr *ipvlan_ht_addr_lookup(const struct ipvl_port *port,
					       const void *iaddr, bool is_v6)
75 76 77 78 79 80
{
	struct ipvl_addr *addr;
	u8 hash;

	hash = is_v6 ? ipvlan_get_v6_hash(iaddr) :
	       ipvlan_get_v4_hash(iaddr);
M
Matteo Croce 已提交
81 82
	hlist_for_each_entry_rcu(addr, &port->hlhead[hash], hlnode)
		if (addr_equal(is_v6, addr, iaddr))
83 84 85 86 87 88 89 90 91 92 93 94
			return addr;
	return NULL;
}

void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr)
{
	struct ipvl_port *port = ipvlan->port;
	u8 hash;

	hash = (addr->atype == IPVL_IPV6) ?
	       ipvlan_get_v6_hash(&addr->ip6addr) :
	       ipvlan_get_v4_hash(&addr->ip4addr);
95 96
	if (hlist_unhashed(&addr->hlnode))
		hlist_add_head_rcu(&addr->hlnode, &port->hlhead[hash]);
97 98
}

99
void ipvlan_ht_addr_del(struct ipvl_addr *addr)
100
{
101
	hlist_del_init_rcu(&addr->hlnode);
102 103
}

104 105
struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
				   const void *iaddr, bool is_v6)
106
{
107 108 109 110 111 112 113 114 115 116 117
	struct ipvl_addr *addr, *ret = NULL;

	rcu_read_lock();
	list_for_each_entry_rcu(addr, &ipvlan->addrs, anode) {
		if (addr_equal(is_v6, addr, iaddr)) {
			ret = addr;
			break;
		}
	}
	rcu_read_unlock();
	return ret;
118
}
119

120 121 122
bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6)
{
	struct ipvl_dev *ipvlan;
123 124 125 126 127 128 129 130
	bool ret = false;

	rcu_read_lock();
	list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
		if (ipvlan_find_addr(ipvlan, iaddr, is_v6)) {
			ret = true;
			break;
		}
131
	}
132 133
	rcu_read_unlock();
	return ret;
134 135
}

136
void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type)
137 138 139 140 141 142 143
{
	void *lyr3h = NULL;

	switch (skb->protocol) {
	case htons(ETH_P_ARP): {
		struct arphdr *arph;

144
		if (unlikely(!pskb_may_pull(skb, arp_hdr_len(port->dev))))
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
			return NULL;

		arph = arp_hdr(skb);
		*type = IPVL_ARP;
		lyr3h = arph;
		break;
	}
	case htons(ETH_P_IP): {
		u32 pktlen;
		struct iphdr *ip4h;

		if (unlikely(!pskb_may_pull(skb, sizeof(*ip4h))))
			return NULL;

		ip4h = ip_hdr(skb);
		pktlen = ntohs(ip4h->tot_len);
		if (ip4h->ihl < 5 || ip4h->version != 4)
			return NULL;
		if (skb->len < pktlen || pktlen < (ip4h->ihl * 4))
			return NULL;

		*type = IPVL_IPV4;
		lyr3h = ip4h;
		break;
	}
M
Matteo Croce 已提交
170
#if IS_ENABLED(CONFIG_IPV6)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	case htons(ETH_P_IPV6): {
		struct ipv6hdr *ip6h;

		if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h))))
			return NULL;

		ip6h = ipv6_hdr(skb);
		if (ip6h->version != 6)
			return NULL;

		*type = IPVL_IPV6;
		lyr3h = ip6h;
		/* Only Neighbour Solicitation pkts need different treatment */
		if (ipv6_addr_any(&ip6h->saddr) &&
		    ip6h->nexthdr == NEXTHDR_ICMP) {
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
			struct icmp6hdr	*icmph;

			if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h) + sizeof(*icmph))))
				return NULL;

			ip6h = ipv6_hdr(skb);
			icmph = (struct icmp6hdr *)(ip6h + 1);

			if (icmph->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
				/* Need to access the ipv6 address in body */
				if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h) + sizeof(*icmph)
						+ sizeof(struct in6_addr))))
					return NULL;

				ip6h = ipv6_hdr(skb);
				icmph = (struct icmp6hdr *)(ip6h + 1);
			}

204
			*type = IPVL_ICMPV6;
205
			lyr3h = icmph;
206 207 208
		}
		break;
	}
M
Matteo Croce 已提交
209
#endif
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	default:
		return NULL;
	}

	return lyr3h;
}

unsigned int ipvlan_mac_hash(const unsigned char *addr)
{
	u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
			       ipvlan_jhash_secret);

	return hash & IPVLAN_MAC_FILTER_MASK;
}

225
void ipvlan_process_multicast(struct work_struct *work)
226
{
227 228
	struct ipvl_port *port = container_of(work, struct ipvl_port, wq);
	struct ethhdr *ethh;
229
	struct ipvl_dev *ipvlan;
230 231
	struct sk_buff *skb, *nskb;
	struct sk_buff_head list;
232 233 234
	unsigned int len;
	unsigned int mac_hash;
	int ret;
235
	u8 pkt_type;
M
Mahesh Bandewar 已提交
236
	bool tx_pkt;
237

238
	__skb_queue_head_init(&list);
239

240 241 242
	spin_lock_bh(&port->backlog.lock);
	skb_queue_splice_tail_init(&port->backlog, &list);
	spin_unlock_bh(&port->backlog.lock);
243

244
	while ((skb = __skb_dequeue(&list)) != NULL) {
245 246 247
		struct net_device *dev = skb->dev;
		bool consumed = false;

248
		ethh = eth_hdr(skb);
M
Mahesh Bandewar 已提交
249
		tx_pkt = IPVL_SKB_CB(skb)->tx_pkt;
250
		mac_hash = ipvlan_mac_hash(ethh->h_dest);
251

252 253
		if (ether_addr_equal(ethh->h_dest, port->dev->broadcast))
			pkt_type = PACKET_BROADCAST;
254
		else
255 256 257 258
			pkt_type = PACKET_MULTICAST;

		rcu_read_lock();
		list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
M
Mahesh Bandewar 已提交
259
			if (tx_pkt && (ipvlan->dev == skb->dev))
260 261 262
				continue;
			if (!test_bit(mac_hash, ipvlan->mac_filters))
				continue;
263 264
			if (!(ipvlan->dev->flags & IFF_UP))
				continue;
265 266 267
			ret = NET_RX_DROP;
			len = skb->len + ETH_HLEN;
			nskb = skb_clone(skb, GFP_ATOMIC);
268 269 270 271 272
			local_bh_disable();
			if (nskb) {
				consumed = true;
				nskb->pkt_type = pkt_type;
				nskb->dev = ipvlan->dev;
M
Mahesh Bandewar 已提交
273
				if (tx_pkt)
274 275 276 277
					ret = dev_forward_skb(ipvlan->dev, nskb);
				else
					ret = netif_rx(nskb);
			}
278
			ipvlan_count_rx(ipvlan, len, ret == NET_RX_SUCCESS, true);
279
			local_bh_enable();
280 281 282
		}
		rcu_read_unlock();

M
Mahesh Bandewar 已提交
283
		if (tx_pkt) {
284 285 286 287 288
			/* If the packet originated here, send it out. */
			skb->dev = port->dev;
			skb->pkt_type = pkt_type;
			dev_queue_xmit(skb);
		} else {
289 290 291 292
			if (consumed)
				consume_skb(skb);
			else
				kfree_skb(skb);
293
		}
294 295
		if (dev)
			dev_put(dev);
296
		cond_resched();
297 298 299
	}
}

300 301 302 303 304 305 306 307 308 309 310 311
static void ipvlan_skb_crossing_ns(struct sk_buff *skb, struct net_device *dev)
{
	bool xnet = true;

	if (dev)
		xnet = !net_eq(dev_net(skb->dev), dev_net(dev));

	skb_scrub_packet(skb, xnet);
	if (dev)
		skb->dev = dev;
}

312
static int ipvlan_rcv_frame(struct ipvl_addr *addr, struct sk_buff **pskb,
313 314 315 316 317 318 319
			    bool local)
{
	struct ipvl_dev *ipvlan = addr->master;
	struct net_device *dev = ipvlan->dev;
	unsigned int len;
	rx_handler_result_t ret = RX_HANDLER_CONSUMED;
	bool success = false;
320
	struct sk_buff *skb = *pskb;
321 322

	len = skb->len + ETH_HLEN;
M
Mahesh Bandewar 已提交
323 324 325 326 327 328 329 330
	/* Only packets exchanged between two local slaves need to have
	 * device-up check as well as skb-share check.
	 */
	if (local) {
		if (unlikely(!(dev->flags & IFF_UP))) {
			kfree_skb(skb);
			goto out;
		}
331

M
Mahesh Bandewar 已提交
332 333 334
		skb = skb_share_check(skb, GFP_ATOMIC);
		if (!skb)
			goto out;
335

M
Mahesh Bandewar 已提交
336 337
		*pskb = skb;
	}
338 339

	if (local) {
M
Mahesh Bandewar 已提交
340
		skb->pkt_type = PACKET_HOST;
341 342 343
		if (dev_forward_skb(ipvlan->dev, skb) == NET_RX_SUCCESS)
			success = true;
	} else {
344
		skb->dev = dev;
345 346 347 348 349 350 351 352 353
		ret = RX_HANDLER_ANOTHER;
		success = true;
	}

out:
	ipvlan_count_rx(ipvlan, len, success, false);
	return ret;
}

354 355
struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,
				     int addr_type, bool use_dest)
356 357 358
{
	struct ipvl_addr *addr = NULL;

M
Matteo Croce 已提交
359 360 361
	switch (addr_type) {
#if IS_ENABLED(CONFIG_IPV6)
	case IPVL_IPV6: {
362 363 364 365 366 367
		struct ipv6hdr *ip6h;
		struct in6_addr *i6addr;

		ip6h = (struct ipv6hdr *)lyr3h;
		i6addr = use_dest ? &ip6h->daddr : &ip6h->saddr;
		addr = ipvlan_ht_addr_lookup(port, i6addr, true);
M
Matteo Croce 已提交
368 369 370
		break;
	}
	case IPVL_ICMPV6: {
371 372 373 374 375 376 377 378 379 380 381
		struct nd_msg *ndmh;
		struct in6_addr *i6addr;

		/* Make sure that the NeighborSolicitation ICMPv6 packets
		 * are handled to avoid DAD issue.
		 */
		ndmh = (struct nd_msg *)lyr3h;
		if (ndmh->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
			i6addr = &ndmh->target;
			addr = ipvlan_ht_addr_lookup(port, i6addr, true);
		}
M
Matteo Croce 已提交
382 383 384 385
		break;
	}
#endif
	case IPVL_IPV4: {
386 387 388 389 390 391
		struct iphdr *ip4h;
		__be32 *i4addr;

		ip4h = (struct iphdr *)lyr3h;
		i4addr = use_dest ? &ip4h->daddr : &ip4h->saddr;
		addr = ipvlan_ht_addr_lookup(port, i4addr, false);
M
Matteo Croce 已提交
392 393 394
		break;
	}
	case IPVL_ARP: {
395 396 397 398 399 400 401 402 403 404 405 406 407
		struct arphdr *arph;
		unsigned char *arp_ptr;
		__be32 dip;

		arph = (struct arphdr *)lyr3h;
		arp_ptr = (unsigned char *)(arph + 1);
		if (use_dest)
			arp_ptr += (2 * port->dev->addr_len) + 4;
		else
			arp_ptr += port->dev->addr_len;

		memcpy(&dip, arp_ptr, 4);
		addr = ipvlan_ht_addr_lookup(port, &dip, false);
M
Matteo Croce 已提交
408 409
		break;
	}
410 411 412 413 414
	}

	return addr;
}

415
static int ipvlan_process_v4_outbound(struct sk_buff *skb)
416 417 418
{
	const struct iphdr *ip4h = ip_hdr(skb);
	struct net_device *dev = skb->dev;
419
	struct net *net = dev_net(dev);
420 421 422
	struct rtable *rt;
	int err, ret = NET_XMIT_DROP;
	struct flowi4 fl4 = {
423
		.flowi4_oif = dev->ifindex,
424 425
		.flowi4_tos = RT_TOS(ip4h->tos),
		.flowi4_flags = FLOWI_FLAG_ANYSRC,
426
		.flowi4_mark = skb->mark,
427 428 429 430
		.daddr = ip4h->daddr,
		.saddr = ip4h->saddr,
	};

431
	rt = ip_route_output_flow(net, &fl4, NULL);
432 433 434 435 436 437 438 439
	if (IS_ERR(rt))
		goto err;

	if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
		ip_rt_put(rt);
		goto err;
	}
	skb_dst_set(skb, &rt->dst);
440
	err = ip_local_out(net, skb->sk, skb);
441 442 443 444 445 446 447 448 449 450 451 452
	if (unlikely(net_xmit_eval(err)))
		dev->stats.tx_errors++;
	else
		ret = NET_XMIT_SUCCESS;
	goto out;
err:
	dev->stats.tx_errors++;
	kfree_skb(skb);
out:
	return ret;
}

M
Matteo Croce 已提交
453
#if IS_ENABLED(CONFIG_IPV6)
454
static int ipvlan_process_v6_outbound(struct sk_buff *skb)
455 456 457
{
	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
	struct net_device *dev = skb->dev;
458
	struct net *net = dev_net(dev);
459 460 461
	struct dst_entry *dst;
	int err, ret = NET_XMIT_DROP;
	struct flowi6 fl6 = {
K
Keefe Liu 已提交
462
		.flowi6_oif = dev->ifindex,
463 464 465 466 467 468 469 470
		.daddr = ip6h->daddr,
		.saddr = ip6h->saddr,
		.flowi6_flags = FLOWI_FLAG_ANYSRC,
		.flowlabel = ip6_flowinfo(ip6h),
		.flowi6_mark = skb->mark,
		.flowi6_proto = ip6h->nexthdr,
	};

471
	dst = ip6_route_output(net, NULL, &fl6);
472 473 474
	if (dst->error) {
		ret = dst->error;
		dst_release(dst);
475
		goto err;
476
	}
477
	skb_dst_set(skb, dst);
478
	err = ip6_local_out(net, skb->sk, skb);
479 480 481 482 483 484 485 486 487 488 489
	if (unlikely(net_xmit_eval(err)))
		dev->stats.tx_errors++;
	else
		ret = NET_XMIT_SUCCESS;
	goto out;
err:
	dev->stats.tx_errors++;
	kfree_skb(skb);
out:
	return ret;
}
M
Matteo Croce 已提交
490 491 492 493 494 495
#else
static int ipvlan_process_v6_outbound(struct sk_buff *skb)
{
	return NET_XMIT_DROP;
}
#endif
496

497
static int ipvlan_process_outbound(struct sk_buff *skb)
498 499 500 501 502 503 504 505 506
{
	struct ethhdr *ethh = eth_hdr(skb);
	int ret = NET_XMIT_DROP;

	/* The ipvlan is a pseudo-L2 device, so the packets that we receive
	 * will have L2; which need to discarded and processed further
	 * in the net-ns of the main-device.
	 */
	if (skb_mac_header_was_set(skb)) {
507 508 509 510 511 512 513 514 515 516
		/* In this mode we dont care about
		 * multicast and broadcast traffic */
		if (is_multicast_ether_addr(ethh->h_dest)) {
			pr_debug_ratelimited(
				"Dropped {multi|broad}cast of type=[%x]\n",
				ntohs(skb->protocol));
			kfree_skb(skb);
			goto out;
		}

517 518 519 520 521 522
		skb_pull(skb, sizeof(*ethh));
		skb->mac_header = (typeof(skb->mac_header))~0U;
		skb_reset_network_header(skb);
	}

	if (skb->protocol == htons(ETH_P_IPV6))
523
		ret = ipvlan_process_v6_outbound(skb);
524
	else if (skb->protocol == htons(ETH_P_IP))
525
		ret = ipvlan_process_v4_outbound(skb);
526 527 528 529 530 531 532 533 534
	else {
		pr_warn_ratelimited("Dropped outbound packet type=%x\n",
				    ntohs(skb->protocol));
		kfree_skb(skb);
	}
out:
	return ret;
}

K
Keefe LIU 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
static int ipvlan_process_v4_forward(struct sk_buff *skb)
{
	const struct iphdr *ip4h = ip_hdr(skb);
	struct net_device *dev = skb->dev;
	struct net *net = dev_net(dev);
	struct rtable *rt;
	int err, ret = NET_XMIT_DROP;
	struct flowi4 fl4 = {
		.flowi4_tos = RT_TOS(ip4h->tos),
		.flowi4_flags = FLOWI_FLAG_ANYSRC,
		.flowi4_mark = skb->mark,
		.daddr = ip4h->daddr,
		.saddr = ip4h->saddr,
	};

	rt = ip_route_output_flow(net, &fl4, NULL);
	if (IS_ERR(rt))
		goto err;

	if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
		ip_rt_put(rt);
		goto err;
	}
	skb_dst_set(skb, &rt->dst);
	err = ip_local_out(net, skb->sk, skb);
	if (unlikely(net_xmit_eval(err)))
		dev->stats.tx_errors++;
	else
		ret = NET_XMIT_SUCCESS;
	goto out;
err:
	dev->stats.tx_errors++;
	kfree_skb(skb);
out:
	return ret;
}

#if IS_ENABLED(CONFIG_IPV6)
static int ipvlan_process_v6_forward(struct sk_buff *skb)
{
	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
	struct net_device *dev = skb->dev;
	struct net *net = dev_net(dev);
	struct dst_entry *dst;
	int err, ret = NET_XMIT_DROP;
	struct flowi6 fl6 = {
		.daddr = ip6h->daddr,
		.saddr = ip6h->saddr,
		.flowi6_flags = FLOWI_FLAG_ANYSRC,
		.flowlabel = ip6_flowinfo(ip6h),
		.flowi6_mark = skb->mark,
		.flowi6_proto = ip6h->nexthdr,
	};

	dst = ip6_route_output(net, NULL, &fl6);
	if (dst->error) {
		ret = dst->error;
		dst_release(dst);
		goto err;
	}
	skb_dst_set(skb, dst);
	err = ip6_local_out(net, skb->sk, skb);
	if (unlikely(net_xmit_eval(err)))
		dev->stats.tx_errors++;
	else
		ret = NET_XMIT_SUCCESS;
	goto out;
err:
	dev->stats.tx_errors++;
	kfree_skb(skb);
out:
	return ret;
}
#else
static int ipvlan_process_v6_forward(struct sk_buff *skb)
{
	return NET_XMIT_DROP;
}
#endif

static int ipvlan_process_forward(struct sk_buff *skb)
{
	struct ethhdr *ethh = eth_hdr(skb);
	int ret = NET_XMIT_DROP;

	/* In this mode we dont care about multicast and broadcast traffic */
	if (is_multicast_ether_addr(ethh->h_dest)) {
		pr_debug_ratelimited("Dropped {multi|broad}cast of type=[%x]\n",
				     ntohs(skb->protocol));
		kfree_skb(skb);
		goto out;
	}

	/* The ipvlan is a pseudo-L2 device, so the packets that we receive
	 * will have L2; which need to discarded and processed further
	 * in the net-ns of the main-device.
	 */
	if (skb_mac_header_was_set(skb)) {
		skb_pull(skb, sizeof(*ethh));
		skb->mac_header = (typeof(skb->mac_header))~0U;
		skb_reset_network_header(skb);
	}

	if (skb->protocol == htons(ETH_P_IPV6)) {
		ret = ipvlan_process_v6_forward(skb);
	} else if (skb->protocol == htons(ETH_P_IP)) {
		ret = ipvlan_process_v4_forward(skb);
	} else {
		pr_warn_ratelimited("Dropped outbound packet type=%x\n",
				    ntohs(skb->protocol));
		kfree_skb(skb);
	}
out:
	return ret;
}

651
static void ipvlan_multicast_enqueue(struct ipvl_port *port,
M
Mahesh Bandewar 已提交
652
				     struct sk_buff *skb, bool tx_pkt)
653 654 655 656 657 658
{
	if (skb->protocol == htons(ETH_P_PAUSE)) {
		kfree_skb(skb);
		return;
	}

M
Mahesh Bandewar 已提交
659 660 661 662 663 664 665
	/* Record that the deferred packet is from TX or RX path. By
	 * looking at mac-addresses on packet will lead to erronus decisions.
	 * (This would be true for a loopback-mode on master device or a
	 * hair-pin mode of the switch.)
	 */
	IPVL_SKB_CB(skb)->tx_pkt = tx_pkt;

666 667
	spin_lock(&port->backlog.lock);
	if (skb_queue_len(&port->backlog) < IPVLAN_QBACKLOG_LIMIT) {
668 669
		if (skb->dev)
			dev_hold(skb->dev);
670 671 672 673 674 675 676 677 678 679
		__skb_queue_tail(&port->backlog, skb);
		spin_unlock(&port->backlog.lock);
		schedule_work(&port->wq);
	} else {
		spin_unlock(&port->backlog.lock);
		atomic_long_inc(&skb->dev->rx_dropped);
		kfree_skb(skb);
	}
}

680 681 682 683 684 685 686
static int ipvlan_xmit_mode_l3(struct sk_buff *skb, struct net_device *dev)
{
	const struct ipvl_dev *ipvlan = netdev_priv(dev);
	void *lyr3h;
	struct ipvl_addr *addr;
	int addr_type;

687
	lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
688 689 690
	if (!lyr3h)
		goto out;

M
Mahesh Bandewar 已提交
691 692 693 694 695 696 697 698
	if (!ipvlan_is_vepa(ipvlan->port)) {
		addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
		if (addr) {
			if (ipvlan_is_private(ipvlan->port)) {
				consume_skb(skb);
				return NET_XMIT_DROP;
			}
			return ipvlan_rcv_frame(addr, &skb, true);
699 700
		}
	}
701
out:
702 703
	ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
	return ipvlan_process_outbound(skb);
704 705 706 707 708 709 710 711 712 713
}

static int ipvlan_xmit_mode_l2(struct sk_buff *skb, struct net_device *dev)
{
	const struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct ethhdr *eth = eth_hdr(skb);
	struct ipvl_addr *addr;
	void *lyr3h;
	int addr_type;

M
Mahesh Bandewar 已提交
714 715
	if (!ipvlan_is_vepa(ipvlan->port) &&
	    ether_addr_equal(eth->h_dest, eth->h_source)) {
716
		lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
717 718
		if (lyr3h) {
			addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
719 720 721 722 723
			if (addr) {
				if (ipvlan_is_private(ipvlan->port)) {
					consume_skb(skb);
					return NET_XMIT_DROP;
				}
724
				return ipvlan_rcv_frame(addr, &skb, true);
725
			}
726 727 728 729 730 731 732 733 734 735 736 737 738
		}
		skb = skb_share_check(skb, GFP_ATOMIC);
		if (!skb)
			return NET_XMIT_DROP;

		/* Packet definitely does not belong to any of the
		 * virtual devices, but the dest is local. So forward
		 * the skb for the main-dev. At the RX side we just return
		 * RX_PASS for it to be processed further on the stack.
		 */
		return dev_forward_skb(ipvlan->phy_dev, skb);

	} else if (is_multicast_ether_addr(eth->h_dest)) {
739
		ipvlan_skb_crossing_ns(skb, NULL);
M
Mahesh Bandewar 已提交
740
		ipvlan_multicast_enqueue(ipvlan->port, skb, true);
741
		return NET_XMIT_SUCCESS;
742 743
	}

744
	skb->dev = ipvlan->phy_dev;
745 746 747
	return dev_queue_xmit(skb);
}

K
Keefe LIU 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
static int ipvlan_xmit_mode_l2e(struct sk_buff *skb, struct net_device *dev)
{
	const struct ipvl_dev *ipvlan = netdev_priv(dev);
	struct ethhdr *eth = eth_hdr(skb);
	struct ipvl_addr *addr;
	void *lyr3h;
	int addr_type;

	if (!ipvlan_is_vepa(ipvlan->port) &&
	    ether_addr_equal(eth->h_dest, eth->h_source)) {
		lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
		if (lyr3h) {
			addr = ipvlan_addr_lookup(ipvlan->port, lyr3h,
						  addr_type, true);
			if (addr) {
				if (ipvlan_is_private(ipvlan->port)) {
					consume_skb(skb);
					return NET_XMIT_DROP;
				}
				return ipvlan_rcv_frame(addr, &skb, true);
			}
		}
		skb = skb_share_check(skb, GFP_ATOMIC);
		if (!skb)
			return NET_XMIT_DROP;

		/* maybe the packet need been forward */
		skb->dev = ipvlan->phy_dev;
		ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
		return ipvlan_process_forward(skb);
	} else if (is_multicast_ether_addr(eth->h_dest)) {
		ipvlan_skb_crossing_ns(skb, NULL);
		ipvlan_multicast_enqueue(ipvlan->port, skb, true);
		return NET_XMIT_SUCCESS;
	}

	skb->dev = ipvlan->phy_dev;
	return dev_queue_xmit(skb);
}

788 789 790
int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct ipvl_dev *ipvlan = netdev_priv(dev);
791
	struct ipvl_port *port = ipvlan_port_get_rcu_bh(ipvlan->phy_dev);
792 793 794 795 796 797 798 799 800 801 802

	if (!port)
		goto out;

	if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
		goto out;

	switch(port->mode) {
	case IPVLAN_MODE_L2:
		return ipvlan_xmit_mode_l2(skb, dev);
	case IPVLAN_MODE_L3:
803
#ifdef CONFIG_IPVLAN_L3S
M
Mahesh Bandewar 已提交
804
	case IPVLAN_MODE_L3S:
805
#endif
806
		return ipvlan_xmit_mode_l3(skb, dev);
K
Keefe LIU 已提交
807 808
	case IPVLAN_MODE_L2E:
		return ipvlan_xmit_mode_l2e(skb, dev);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	}

	/* Should not reach here */
	WARN_ONCE(true, "ipvlan_queue_xmit() called for mode = [%hx]\n",
			  port->mode);
out:
	kfree_skb(skb);
	return NET_XMIT_DROP;
}

static bool ipvlan_external_frame(struct sk_buff *skb, struct ipvl_port *port)
{
	struct ethhdr *eth = eth_hdr(skb);
	struct ipvl_addr *addr;
	void *lyr3h;
	int addr_type;

	if (ether_addr_equal(eth->h_source, skb->dev->dev_addr)) {
827
		lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
		if (!lyr3h)
			return true;

		addr = ipvlan_addr_lookup(port, lyr3h, addr_type, false);
		if (addr)
			return false;
	}

	return true;
}

static rx_handler_result_t ipvlan_handle_mode_l3(struct sk_buff **pskb,
						 struct ipvl_port *port)
{
	void *lyr3h;
	int addr_type;
	struct ipvl_addr *addr;
	struct sk_buff *skb = *pskb;
	rx_handler_result_t ret = RX_HANDLER_PASS;

848
	lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
849 850 851 852 853
	if (!lyr3h)
		goto out;

	addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
	if (addr)
854
		ret = ipvlan_rcv_frame(addr, pskb, false);
855 856 857 858 859 860 861 862 863 864 865 866 867

out:
	return ret;
}

static rx_handler_result_t ipvlan_handle_mode_l2(struct sk_buff **pskb,
						 struct ipvl_port *port)
{
	struct sk_buff *skb = *pskb;
	struct ethhdr *eth = eth_hdr(skb);
	rx_handler_result_t ret = RX_HANDLER_PASS;

	if (is_multicast_ether_addr(eth->h_dest)) {
868 869 870 871 872 873 874 875 876
		if (ipvlan_external_frame(skb, port)) {
			struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);

			/* External frames are queued for device local
			 * distribution, but a copy is given to master
			 * straight away to avoid sending duplicates later
			 * when work-queue processes this frame. This is
			 * achieved by returning RX_HANDLER_PASS.
			 */
877 878
			if (nskb) {
				ipvlan_skb_crossing_ns(nskb, NULL);
M
Mahesh Bandewar 已提交
879
				ipvlan_multicast_enqueue(port, nskb, false);
880
			}
881
		}
882
	} else {
883 884
		/* Perform like l3 mode for non-multicast packet */
		ret = ipvlan_handle_mode_l3(pskb, port);
885 886 887 888 889
	}

	return ret;
}

K
Keefe LIU 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
static rx_handler_result_t ipvlan_handle_mode_l2e(struct sk_buff **pskb,
						  struct ipvl_port *port)
{
	struct sk_buff *skb = *pskb;
	struct ethhdr *eth = eth_hdr(skb);
	rx_handler_result_t ret = RX_HANDLER_PASS;

	if (is_multicast_ether_addr(eth->h_dest)) {
		if (ipvlan_external_frame(skb, port)) {
			struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);

			/* External frames are queued for device local
			 * distribution, but a copy is given to master
			 * straight away to avoid sending duplicates later
			 * when work-queue processes this frame. This is
			 * achieved by returning RX_HANDLER_PASS.
			 */
			if (nskb) {
				ipvlan_skb_crossing_ns(nskb, NULL);
				ipvlan_multicast_enqueue(port, nskb, false);
			}
		}
	} else {
		/* Perform like l3 mode for non-multicast packet */
		ret = ipvlan_handle_mode_l3(pskb, port);
	}

	return ret;
}

920 921 922 923 924 925 926 927 928 929 930 931 932
rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
{
	struct sk_buff *skb = *pskb;
	struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);

	if (!port)
		return RX_HANDLER_PASS;

	switch (port->mode) {
	case IPVLAN_MODE_L2:
		return ipvlan_handle_mode_l2(pskb, port);
	case IPVLAN_MODE_L3:
		return ipvlan_handle_mode_l3(pskb, port);
933
#ifdef CONFIG_IPVLAN_L3S
M
Mahesh Bandewar 已提交
934 935
	case IPVLAN_MODE_L3S:
		return RX_HANDLER_PASS;
936
#endif
K
Keefe LIU 已提交
937 938
	case IPVLAN_MODE_L2E:
		return ipvlan_handle_mode_l2e(pskb, port);
939 940 941 942 943 944
	}

	/* Should not reach here */
	WARN_ONCE(true, "ipvlan_handle_frame() called for mode = [%hx]\n",
			  port->mode);
	kfree_skb(skb);
945
	return RX_HANDLER_CONSUMED;
946
}