1 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
12 static u32 ipvlan_jhash_secret __read_mostly
;
14 void ipvlan_init_secret(void)
16 net_get_random_once(&ipvlan_jhash_secret
, sizeof(ipvlan_jhash_secret
));
19 void ipvlan_count_rx(const struct ipvl_dev
*ipvlan
,
20 unsigned int len
, bool success
, bool mcast
)
22 if (likely(success
)) {
23 struct ipvl_pcpu_stats
*pcptr
;
25 pcptr
= this_cpu_ptr(ipvlan
->pcpu_stats
);
26 u64_stats_update_begin(&pcptr
->syncp
);
28 pcptr
->rx_bytes
+= len
;
31 u64_stats_update_end(&pcptr
->syncp
);
33 this_cpu_inc(ipvlan
->pcpu_stats
->rx_errs
);
36 EXPORT_SYMBOL_GPL(ipvlan_count_rx
);
38 static u8
ipvlan_get_v6_hash(const void *iaddr
)
40 const struct in6_addr
*ip6_addr
= iaddr
;
42 return __ipv6_addr_jhash(ip6_addr
, ipvlan_jhash_secret
) &
46 static u8
ipvlan_get_v4_hash(const void *iaddr
)
48 const struct in_addr
*ip4_addr
= iaddr
;
50 return jhash_1word(ip4_addr
->s_addr
, ipvlan_jhash_secret
) &
54 static struct ipvl_addr
*ipvlan_ht_addr_lookup(const struct ipvl_port
*port
,
55 const void *iaddr
, bool is_v6
)
57 struct ipvl_addr
*addr
;
60 hash
= is_v6
? ipvlan_get_v6_hash(iaddr
) :
61 ipvlan_get_v4_hash(iaddr
);
62 hlist_for_each_entry_rcu(addr
, &port
->hlhead
[hash
], hlnode
) {
63 if (is_v6
&& addr
->atype
== IPVL_IPV6
&&
64 ipv6_addr_equal(&addr
->ip6addr
, iaddr
))
66 else if (!is_v6
&& addr
->atype
== IPVL_IPV4
&&
67 addr
->ip4addr
.s_addr
==
68 ((struct in_addr
*)iaddr
)->s_addr
)
74 void ipvlan_ht_addr_add(struct ipvl_dev
*ipvlan
, struct ipvl_addr
*addr
)
76 struct ipvl_port
*port
= ipvlan
->port
;
79 hash
= (addr
->atype
== IPVL_IPV6
) ?
80 ipvlan_get_v6_hash(&addr
->ip6addr
) :
81 ipvlan_get_v4_hash(&addr
->ip4addr
);
82 if (hlist_unhashed(&addr
->hlnode
))
83 hlist_add_head_rcu(&addr
->hlnode
, &port
->hlhead
[hash
]);
86 void ipvlan_ht_addr_del(struct ipvl_addr
*addr
)
88 hlist_del_init_rcu(&addr
->hlnode
);
91 struct ipvl_addr
*ipvlan_find_addr(const struct ipvl_dev
*ipvlan
,
92 const void *iaddr
, bool is_v6
)
94 struct ipvl_addr
*addr
;
96 list_for_each_entry(addr
, &ipvlan
->addrs
, anode
) {
97 if ((is_v6
&& addr
->atype
== IPVL_IPV6
&&
98 ipv6_addr_equal(&addr
->ip6addr
, iaddr
)) ||
99 (!is_v6
&& addr
->atype
== IPVL_IPV4
&&
100 addr
->ip4addr
.s_addr
== ((struct in_addr
*)iaddr
)->s_addr
))
106 bool ipvlan_addr_busy(struct ipvl_port
*port
, void *iaddr
, bool is_v6
)
108 struct ipvl_dev
*ipvlan
;
112 list_for_each_entry(ipvlan
, &port
->ipvlans
, pnode
) {
113 if (ipvlan_find_addr(ipvlan
, iaddr
, is_v6
))
119 static void *ipvlan_get_L3_hdr(struct ipvl_port
*port
, struct sk_buff
*skb
, int *type
)
123 switch (skb
->protocol
) {
124 case htons(ETH_P_ARP
): {
127 if (unlikely(!pskb_may_pull(skb
, arp_hdr_len(port
->dev
))))
135 case htons(ETH_P_IP
): {
139 if (unlikely(!pskb_may_pull(skb
, sizeof(*ip4h
))))
143 pktlen
= ntohs(ip4h
->tot_len
);
144 if (ip4h
->ihl
< 5 || ip4h
->version
!= 4)
146 if (skb
->len
< pktlen
|| pktlen
< (ip4h
->ihl
* 4))
153 case htons(ETH_P_IPV6
): {
154 struct ipv6hdr
*ip6h
;
156 if (unlikely(!pskb_may_pull(skb
, sizeof(*ip6h
))))
159 ip6h
= ipv6_hdr(skb
);
160 if (ip6h
->version
!= 6)
165 /* Only Neighbour Solicitation pkts need different treatment */
166 if (ipv6_addr_any(&ip6h
->saddr
) &&
167 ip6h
->nexthdr
== NEXTHDR_ICMP
) {
168 struct icmp6hdr
*icmph
;
170 if (unlikely(!pskb_may_pull(skb
, sizeof(*ip6h
) + sizeof(*icmph
))))
173 ip6h
= ipv6_hdr(skb
);
174 icmph
= (struct icmp6hdr
*)(ip6h
+ 1);
176 if (icmph
->icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
) {
177 /* Need to access the ipv6 address in body */
178 if (unlikely(!pskb_may_pull(skb
, sizeof(*ip6h
) + sizeof(*icmph
)
179 + sizeof(struct in6_addr
))))
182 ip6h
= ipv6_hdr(skb
);
183 icmph
= (struct icmp6hdr
*)(ip6h
+ 1);
198 unsigned int ipvlan_mac_hash(const unsigned char *addr
)
200 u32 hash
= jhash_1word(__get_unaligned_cpu32(addr
+2),
201 ipvlan_jhash_secret
);
203 return hash
& IPVLAN_MAC_FILTER_MASK
;
206 void ipvlan_process_multicast(struct work_struct
*work
)
208 struct ipvl_port
*port
= container_of(work
, struct ipvl_port
, wq
);
210 struct ipvl_dev
*ipvlan
;
211 struct sk_buff
*skb
, *nskb
;
212 struct sk_buff_head list
;
214 unsigned int mac_hash
;
219 __skb_queue_head_init(&list
);
221 spin_lock_bh(&port
->backlog
.lock
);
222 skb_queue_splice_tail_init(&port
->backlog
, &list
);
223 spin_unlock_bh(&port
->backlog
.lock
);
225 while ((skb
= __skb_dequeue(&list
)) != NULL
) {
226 struct net_device
*dev
= skb
->dev
;
227 bool consumed
= false;
230 tx_pkt
= IPVL_SKB_CB(skb
)->tx_pkt
;
231 mac_hash
= ipvlan_mac_hash(ethh
->h_dest
);
233 if (ether_addr_equal(ethh
->h_dest
, port
->dev
->broadcast
))
234 pkt_type
= PACKET_BROADCAST
;
236 pkt_type
= PACKET_MULTICAST
;
239 list_for_each_entry_rcu(ipvlan
, &port
->ipvlans
, pnode
) {
240 if (tx_pkt
&& (ipvlan
->dev
== skb
->dev
))
242 if (!test_bit(mac_hash
, ipvlan
->mac_filters
))
244 if (!(ipvlan
->dev
->flags
& IFF_UP
))
247 len
= skb
->len
+ ETH_HLEN
;
248 nskb
= skb_clone(skb
, GFP_ATOMIC
);
252 nskb
->pkt_type
= pkt_type
;
253 nskb
->dev
= ipvlan
->dev
;
255 ret
= dev_forward_skb(ipvlan
->dev
, nskb
);
257 ret
= netif_rx(nskb
);
259 ipvlan_count_rx(ipvlan
, len
, ret
== NET_RX_SUCCESS
, true);
265 /* If the packet originated here, send it out. */
266 skb
->dev
= port
->dev
;
267 skb
->pkt_type
= pkt_type
;
280 static void ipvlan_skb_crossing_ns(struct sk_buff
*skb
, struct net_device
*dev
)
285 xnet
= !net_eq(dev_net(skb
->dev
), dev_net(dev
));
287 skb_scrub_packet(skb
, xnet
);
292 static int ipvlan_rcv_frame(struct ipvl_addr
*addr
, struct sk_buff
**pskb
,
295 struct ipvl_dev
*ipvlan
= addr
->master
;
296 struct net_device
*dev
= ipvlan
->dev
;
298 rx_handler_result_t ret
= RX_HANDLER_CONSUMED
;
299 bool success
= false;
300 struct sk_buff
*skb
= *pskb
;
302 len
= skb
->len
+ ETH_HLEN
;
303 /* Only packets exchanged between two local slaves need to have
304 * device-up check as well as skb-share check.
307 if (unlikely(!(dev
->flags
& IFF_UP
))) {
312 skb
= skb_share_check(skb
, GFP_ATOMIC
);
320 skb
->pkt_type
= PACKET_HOST
;
321 if (dev_forward_skb(ipvlan
->dev
, skb
) == NET_RX_SUCCESS
)
325 ret
= RX_HANDLER_ANOTHER
;
330 ipvlan_count_rx(ipvlan
, len
, success
, false);
334 static struct ipvl_addr
*ipvlan_addr_lookup(struct ipvl_port
*port
,
335 void *lyr3h
, int addr_type
,
338 struct ipvl_addr
*addr
= NULL
;
340 if (addr_type
== IPVL_IPV6
) {
341 struct ipv6hdr
*ip6h
;
342 struct in6_addr
*i6addr
;
344 ip6h
= (struct ipv6hdr
*)lyr3h
;
345 i6addr
= use_dest
? &ip6h
->daddr
: &ip6h
->saddr
;
346 addr
= ipvlan_ht_addr_lookup(port
, i6addr
, true);
347 } else if (addr_type
== IPVL_ICMPV6
) {
349 struct in6_addr
*i6addr
;
351 /* Make sure that the NeighborSolicitation ICMPv6 packets
352 * are handled to avoid DAD issue.
354 ndmh
= (struct nd_msg
*)lyr3h
;
355 if (ndmh
->icmph
.icmp6_type
== NDISC_NEIGHBOUR_SOLICITATION
) {
356 i6addr
= &ndmh
->target
;
357 addr
= ipvlan_ht_addr_lookup(port
, i6addr
, true);
359 } else if (addr_type
== IPVL_IPV4
) {
363 ip4h
= (struct iphdr
*)lyr3h
;
364 i4addr
= use_dest
? &ip4h
->daddr
: &ip4h
->saddr
;
365 addr
= ipvlan_ht_addr_lookup(port
, i4addr
, false);
366 } else if (addr_type
== IPVL_ARP
) {
368 unsigned char *arp_ptr
;
371 arph
= (struct arphdr
*)lyr3h
;
372 arp_ptr
= (unsigned char *)(arph
+ 1);
374 arp_ptr
+= (2 * port
->dev
->addr_len
) + 4;
376 arp_ptr
+= port
->dev
->addr_len
;
378 memcpy(&dip
, arp_ptr
, 4);
379 addr
= ipvlan_ht_addr_lookup(port
, &dip
, false);
385 static int ipvlan_process_v4_outbound(struct sk_buff
*skb
)
387 const struct iphdr
*ip4h
= ip_hdr(skb
);
388 struct net_device
*dev
= skb
->dev
;
389 struct net
*net
= dev_net(dev
);
391 int err
, ret
= NET_XMIT_DROP
;
392 struct flowi4 fl4
= {
393 .flowi4_oif
= dev
->ifindex
,
394 .flowi4_tos
= RT_TOS(ip4h
->tos
),
395 .flowi4_flags
= FLOWI_FLAG_ANYSRC
,
396 .flowi4_mark
= skb
->mark
,
397 .daddr
= ip4h
->daddr
,
398 .saddr
= ip4h
->saddr
,
401 rt
= ip_route_output_flow(net
, &fl4
, NULL
);
405 if (rt
->rt_type
!= RTN_UNICAST
&& rt
->rt_type
!= RTN_LOCAL
) {
409 skb_dst_set(skb
, &rt
->dst
);
410 err
= ip_local_out(net
, skb
->sk
, skb
);
411 if (unlikely(net_xmit_eval(err
)))
412 dev
->stats
.tx_errors
++;
414 ret
= NET_XMIT_SUCCESS
;
417 dev
->stats
.tx_errors
++;
423 static int ipvlan_process_v6_outbound(struct sk_buff
*skb
)
425 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
426 struct net_device
*dev
= skb
->dev
;
427 struct net
*net
= dev_net(dev
);
428 struct dst_entry
*dst
;
429 int err
, ret
= NET_XMIT_DROP
;
430 struct flowi6 fl6
= {
431 .flowi6_oif
= dev
->ifindex
,
432 .daddr
= ip6h
->daddr
,
433 .saddr
= ip6h
->saddr
,
434 .flowi6_flags
= FLOWI_FLAG_ANYSRC
,
435 .flowlabel
= ip6_flowinfo(ip6h
),
436 .flowi6_mark
= skb
->mark
,
437 .flowi6_proto
= ip6h
->nexthdr
,
440 dst
= ip6_route_output(net
, NULL
, &fl6
);
446 skb_dst_set(skb
, dst
);
447 err
= ip6_local_out(net
, skb
->sk
, skb
);
448 if (unlikely(net_xmit_eval(err
)))
449 dev
->stats
.tx_errors
++;
451 ret
= NET_XMIT_SUCCESS
;
454 dev
->stats
.tx_errors
++;
460 static int ipvlan_process_outbound(struct sk_buff
*skb
)
462 struct ethhdr
*ethh
= eth_hdr(skb
);
463 int ret
= NET_XMIT_DROP
;
465 /* In this mode we dont care about multicast and broadcast traffic */
466 if (is_multicast_ether_addr(ethh
->h_dest
)) {
467 pr_warn_ratelimited("Dropped {multi|broad}cast of type= [%x]\n",
468 ntohs(skb
->protocol
));
473 /* The ipvlan is a pseudo-L2 device, so the packets that we receive
474 * will have L2; which need to discarded and processed further
475 * in the net-ns of the main-device.
477 if (skb_mac_header_was_set(skb
)) {
478 skb_pull(skb
, sizeof(*ethh
));
479 skb
->mac_header
= (typeof(skb
->mac_header
))~0U;
480 skb_reset_network_header(skb
);
483 if (skb
->protocol
== htons(ETH_P_IPV6
))
484 ret
= ipvlan_process_v6_outbound(skb
);
485 else if (skb
->protocol
== htons(ETH_P_IP
))
486 ret
= ipvlan_process_v4_outbound(skb
);
488 pr_warn_ratelimited("Dropped outbound packet type=%x\n",
489 ntohs(skb
->protocol
));
496 static void ipvlan_multicast_enqueue(struct ipvl_port
*port
,
497 struct sk_buff
*skb
, bool tx_pkt
)
499 if (skb
->protocol
== htons(ETH_P_PAUSE
)) {
504 /* Record that the deferred packet is from TX or RX path. By
505 * looking at mac-addresses on packet will lead to erronus decisions.
506 * (This would be true for a loopback-mode on master device or a
507 * hair-pin mode of the switch.)
509 IPVL_SKB_CB(skb
)->tx_pkt
= tx_pkt
;
511 spin_lock(&port
->backlog
.lock
);
512 if (skb_queue_len(&port
->backlog
) < IPVLAN_QBACKLOG_LIMIT
) {
515 __skb_queue_tail(&port
->backlog
, skb
);
516 spin_unlock(&port
->backlog
.lock
);
517 schedule_work(&port
->wq
);
519 spin_unlock(&port
->backlog
.lock
);
520 atomic_long_inc(&skb
->dev
->rx_dropped
);
525 static int ipvlan_xmit_mode_l3(struct sk_buff
*skb
, struct net_device
*dev
)
527 const struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
529 struct ipvl_addr
*addr
;
532 lyr3h
= ipvlan_get_L3_hdr(ipvlan
->port
, skb
, &addr_type
);
536 if (!ipvlan_is_vepa(ipvlan
->port
)) {
537 addr
= ipvlan_addr_lookup(ipvlan
->port
, lyr3h
, addr_type
, true);
539 if (ipvlan_is_private(ipvlan
->port
)) {
541 return NET_XMIT_DROP
;
543 return ipvlan_rcv_frame(addr
, &skb
, true);
547 ipvlan_skb_crossing_ns(skb
, ipvlan
->phy_dev
);
548 return ipvlan_process_outbound(skb
);
551 static int ipvlan_xmit_mode_l2(struct sk_buff
*skb
, struct net_device
*dev
)
553 const struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
554 struct ethhdr
*eth
= eth_hdr(skb
);
555 struct ipvl_addr
*addr
;
559 if (!ipvlan_is_vepa(ipvlan
->port
) &&
560 ether_addr_equal(eth
->h_dest
, eth
->h_source
)) {
561 lyr3h
= ipvlan_get_L3_hdr(ipvlan
->port
, skb
, &addr_type
);
563 addr
= ipvlan_addr_lookup(ipvlan
->port
, lyr3h
, addr_type
, true);
565 if (ipvlan_is_private(ipvlan
->port
)) {
567 return NET_XMIT_DROP
;
569 return ipvlan_rcv_frame(addr
, &skb
, true);
572 skb
= skb_share_check(skb
, GFP_ATOMIC
);
574 return NET_XMIT_DROP
;
576 /* Packet definitely does not belong to any of the
577 * virtual devices, but the dest is local. So forward
578 * the skb for the main-dev. At the RX side we just return
579 * RX_PASS for it to be processed further on the stack.
581 return dev_forward_skb(ipvlan
->phy_dev
, skb
);
583 } else if (is_multicast_ether_addr(eth
->h_dest
)) {
584 ipvlan_skb_crossing_ns(skb
, NULL
);
585 ipvlan_multicast_enqueue(ipvlan
->port
, skb
, true);
586 return NET_XMIT_SUCCESS
;
589 skb
->dev
= ipvlan
->phy_dev
;
590 return dev_queue_xmit(skb
);
593 int ipvlan_queue_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
595 struct ipvl_dev
*ipvlan
= netdev_priv(dev
);
596 struct ipvl_port
*port
= ipvlan_port_get_rcu_bh(ipvlan
->phy_dev
);
601 if (unlikely(!pskb_may_pull(skb
, sizeof(struct ethhdr
))))
606 return ipvlan_xmit_mode_l2(skb
, dev
);
608 case IPVLAN_MODE_L3S
:
609 return ipvlan_xmit_mode_l3(skb
, dev
);
612 /* Should not reach here */
613 WARN_ONCE(true, "ipvlan_queue_xmit() called for mode = [%hx]\n",
617 return NET_XMIT_DROP
;
620 static bool ipvlan_external_frame(struct sk_buff
*skb
, struct ipvl_port
*port
)
622 struct ethhdr
*eth
= eth_hdr(skb
);
623 struct ipvl_addr
*addr
;
627 if (ether_addr_equal(eth
->h_source
, skb
->dev
->dev_addr
)) {
628 lyr3h
= ipvlan_get_L3_hdr(port
, skb
, &addr_type
);
632 addr
= ipvlan_addr_lookup(port
, lyr3h
, addr_type
, false);
640 static rx_handler_result_t
ipvlan_handle_mode_l3(struct sk_buff
**pskb
,
641 struct ipvl_port
*port
)
645 struct ipvl_addr
*addr
;
646 struct sk_buff
*skb
= *pskb
;
647 rx_handler_result_t ret
= RX_HANDLER_PASS
;
649 lyr3h
= ipvlan_get_L3_hdr(port
, skb
, &addr_type
);
653 addr
= ipvlan_addr_lookup(port
, lyr3h
, addr_type
, true);
655 ret
= ipvlan_rcv_frame(addr
, pskb
, false);
661 static rx_handler_result_t
ipvlan_handle_mode_l2(struct sk_buff
**pskb
,
662 struct ipvl_port
*port
)
664 struct sk_buff
*skb
= *pskb
;
665 struct ethhdr
*eth
= eth_hdr(skb
);
666 rx_handler_result_t ret
= RX_HANDLER_PASS
;
668 if (is_multicast_ether_addr(eth
->h_dest
)) {
669 if (ipvlan_external_frame(skb
, port
)) {
670 struct sk_buff
*nskb
= skb_clone(skb
, GFP_ATOMIC
);
672 /* External frames are queued for device local
673 * distribution, but a copy is given to master
674 * straight away to avoid sending duplicates later
675 * when work-queue processes this frame. This is
676 * achieved by returning RX_HANDLER_PASS.
679 ipvlan_skb_crossing_ns(nskb
, NULL
);
680 ipvlan_multicast_enqueue(port
, nskb
, false);
684 /* Perform like l3 mode for non-multicast packet */
685 ret
= ipvlan_handle_mode_l3(pskb
, port
);
691 rx_handler_result_t
ipvlan_handle_frame(struct sk_buff
**pskb
)
693 struct sk_buff
*skb
= *pskb
;
694 struct ipvl_port
*port
= ipvlan_port_get_rcu(skb
->dev
);
697 return RX_HANDLER_PASS
;
699 switch (port
->mode
) {
701 return ipvlan_handle_mode_l2(pskb
, port
);
703 return ipvlan_handle_mode_l3(pskb
, port
);
704 case IPVLAN_MODE_L3S
:
705 return RX_HANDLER_PASS
;
708 /* Should not reach here */
709 WARN_ONCE(true, "ipvlan_handle_frame() called for mode = [%hx]\n",
712 return RX_HANDLER_CONSUMED
;
715 static struct ipvl_addr
*ipvlan_skb_to_addr(struct sk_buff
*skb
,
716 struct net_device
*dev
)
718 struct ipvl_addr
*addr
= NULL
;
719 struct ipvl_port
*port
;
723 if (!dev
|| !netif_is_ipvlan_port(dev
))
726 port
= ipvlan_port_get_rcu(dev
);
727 if (!port
|| port
->mode
!= IPVLAN_MODE_L3S
)
730 lyr3h
= ipvlan_get_L3_hdr(port
, skb
, &addr_type
);
734 addr
= ipvlan_addr_lookup(port
, lyr3h
, addr_type
, true);
739 struct sk_buff
*ipvlan_l3_rcv(struct net_device
*dev
, struct sk_buff
*skb
,
742 struct ipvl_addr
*addr
;
743 struct net_device
*sdev
;
745 addr
= ipvlan_skb_to_addr(skb
, dev
);
749 sdev
= addr
->master
->dev
;
754 struct iphdr
*ip4h
= ip_hdr(skb
);
756 err
= ip_route_input_noref(skb
, ip4h
->daddr
, ip4h
->saddr
,
764 struct dst_entry
*dst
;
765 struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
766 int flags
= RT6_LOOKUP_F_HAS_SADDR
;
767 struct flowi6 fl6
= {
768 .flowi6_iif
= sdev
->ifindex
,
769 .daddr
= ip6h
->daddr
,
770 .saddr
= ip6h
->saddr
,
771 .flowlabel
= ip6_flowinfo(ip6h
),
772 .flowi6_mark
= skb
->mark
,
773 .flowi6_proto
= ip6h
->nexthdr
,
777 dst
= ip6_route_input_lookup(dev_net(sdev
), sdev
, &fl6
, flags
);
778 skb_dst_set(skb
, dst
);
789 unsigned int ipvlan_nf_input(void *priv
, struct sk_buff
*skb
,
790 const struct nf_hook_state
*state
)
792 struct ipvl_addr
*addr
;
795 addr
= ipvlan_skb_to_addr(skb
, skb
->dev
);
799 skb
->dev
= addr
->master
->dev
;
800 len
= skb
->len
+ ETH_HLEN
;
801 ipvlan_count_rx(addr
->master
, len
, true, false);