1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
5 #include <net/inet_dscp.h>
9 static u32 ipvlan_jhash_secret __read_mostly;
11 void ipvlan_init_secret(void)
13 net_get_random_once(&ipvlan_jhash_secret, sizeof(ipvlan_jhash_secret));
16 void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
17 unsigned int len, bool success, bool mcast)
19 if (likely(success)) {
20 struct ipvl_pcpu_stats *pcptr;
22 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
23 u64_stats_update_begin(&pcptr->syncp);
24 u64_stats_inc(&pcptr->rx_pkts);
25 u64_stats_add(&pcptr->rx_bytes, len);
27 u64_stats_inc(&pcptr->rx_mcast);
28 u64_stats_update_end(&pcptr->syncp);
30 this_cpu_inc(ipvlan->pcpu_stats->rx_errs);
33 EXPORT_SYMBOL_GPL(ipvlan_count_rx);
35 #if IS_ENABLED(CONFIG_IPV6)
36 static u8 ipvlan_get_v6_hash(const void *iaddr)
38 const struct in6_addr *ip6_addr = iaddr;
40 return __ipv6_addr_jhash(ip6_addr, ipvlan_jhash_secret) &
44 static u8 ipvlan_get_v6_hash(const void *iaddr)
50 static u8 ipvlan_get_v4_hash(const void *iaddr)
52 const struct in_addr *ip4_addr = iaddr;
54 return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret) &
58 static bool addr_equal(bool is_v6, struct ipvl_addr *addr, const void *iaddr)
60 if (!is_v6 && addr->atype == IPVL_IPV4) {
61 struct in_addr *i4addr = (struct in_addr *)iaddr;
63 return addr->ip4addr.s_addr == i4addr->s_addr;
64 #if IS_ENABLED(CONFIG_IPV6)
65 } else if (is_v6 && addr->atype == IPVL_IPV6) {
66 struct in6_addr *i6addr = (struct in6_addr *)iaddr;
68 return ipv6_addr_equal(&addr->ip6addr, i6addr);
75 static struct ipvl_addr *ipvlan_ht_addr_lookup(const struct ipvl_port *port,
76 const void *iaddr, bool is_v6)
78 struct ipvl_addr *addr;
81 hash = is_v6 ? ipvlan_get_v6_hash(iaddr) :
82 ipvlan_get_v4_hash(iaddr);
83 hlist_for_each_entry_rcu(addr, &port->hlhead[hash], hlnode)
84 if (addr_equal(is_v6, addr, iaddr))
89 void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr)
91 struct ipvl_port *port = ipvlan->port;
94 hash = (addr->atype == IPVL_IPV6) ?
95 ipvlan_get_v6_hash(&addr->ip6addr) :
96 ipvlan_get_v4_hash(&addr->ip4addr);
97 if (hlist_unhashed(&addr->hlnode))
98 hlist_add_head_rcu(&addr->hlnode, &port->hlhead[hash]);
101 void ipvlan_ht_addr_del(struct ipvl_addr *addr)
103 hlist_del_init_rcu(&addr->hlnode);
106 struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
107 const void *iaddr, bool is_v6)
109 struct ipvl_addr *addr, *ret = NULL;
112 list_for_each_entry_rcu(addr, &ipvlan->addrs, anode) {
113 if (addr_equal(is_v6, addr, iaddr)) {
122 bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6)
124 struct ipvl_dev *ipvlan;
128 list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
129 if (ipvlan_find_addr(ipvlan, iaddr, is_v6)) {
138 void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type)
142 switch (skb->protocol) {
143 case htons(ETH_P_ARP): {
146 if (unlikely(!pskb_may_pull(skb, arp_hdr_len(port->dev))))
154 case htons(ETH_P_IP): {
158 if (unlikely(!pskb_may_pull(skb, sizeof(*ip4h))))
162 pktlen = skb_ip_totlen(skb);
163 if (ip4h->ihl < 5 || ip4h->version != 4)
165 if (skb->len < pktlen || pktlen < (ip4h->ihl * 4))
172 #if IS_ENABLED(CONFIG_IPV6)
173 case htons(ETH_P_IPV6): {
174 struct ipv6hdr *ip6h;
176 if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h))))
179 ip6h = ipv6_hdr(skb);
180 if (ip6h->version != 6)
185 /* Only Neighbour Solicitation pkts need different treatment */
186 if (ipv6_addr_any(&ip6h->saddr) &&
187 ip6h->nexthdr == NEXTHDR_ICMP) {
188 struct icmp6hdr *icmph;
190 if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h) + sizeof(*icmph))))
193 ip6h = ipv6_hdr(skb);
194 icmph = (struct icmp6hdr *)(ip6h + 1);
196 if (icmph->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
197 /* Need to access the ipv6 address in body */
198 if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h) + sizeof(*icmph)
199 + sizeof(struct in6_addr))))
202 ip6h = ipv6_hdr(skb);
203 icmph = (struct icmp6hdr *)(ip6h + 1);
219 unsigned int ipvlan_mac_hash(const unsigned char *addr)
221 u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
222 ipvlan_jhash_secret);
224 return hash & IPVLAN_MAC_FILTER_MASK;
227 void ipvlan_process_multicast(struct work_struct *work)
229 struct ipvl_port *port = container_of(work, struct ipvl_port, wq);
231 struct ipvl_dev *ipvlan;
232 struct sk_buff *skb, *nskb;
233 struct sk_buff_head list;
235 unsigned int mac_hash;
240 __skb_queue_head_init(&list);
242 spin_lock_bh(&port->backlog.lock);
243 skb_queue_splice_tail_init(&port->backlog, &list);
244 spin_unlock_bh(&port->backlog.lock);
246 while ((skb = __skb_dequeue(&list)) != NULL) {
247 struct net_device *dev = skb->dev;
248 bool consumed = false;
251 tx_pkt = IPVL_SKB_CB(skb)->tx_pkt;
252 mac_hash = ipvlan_mac_hash(ethh->h_dest);
254 if (ether_addr_equal(ethh->h_dest, port->dev->broadcast))
255 pkt_type = PACKET_BROADCAST;
257 pkt_type = PACKET_MULTICAST;
260 list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
261 if (tx_pkt && (ipvlan->dev == skb->dev))
263 if (!test_bit(mac_hash, ipvlan->mac_filters))
265 if (!(ipvlan->dev->flags & IFF_UP))
268 len = skb->len + ETH_HLEN;
269 nskb = skb_clone(skb, GFP_ATOMIC);
273 nskb->pkt_type = pkt_type;
274 nskb->dev = ipvlan->dev;
276 ret = dev_forward_skb(ipvlan->dev, nskb);
278 ret = netif_rx(nskb);
280 ipvlan_count_rx(ipvlan, len, ret == NET_RX_SUCCESS, true);
286 /* If the packet originated here, send it out. */
287 skb->dev = port->dev;
288 skb->pkt_type = pkt_type;
301 static void ipvlan_skb_crossing_ns(struct sk_buff *skb, struct net_device *dev)
306 xnet = !net_eq(dev_net(skb->dev), dev_net(dev));
308 skb_scrub_packet(skb, xnet);
313 static int ipvlan_rcv_frame(struct ipvl_addr *addr, struct sk_buff **pskb,
316 struct ipvl_dev *ipvlan = addr->master;
317 struct net_device *dev = ipvlan->dev;
319 rx_handler_result_t ret = RX_HANDLER_CONSUMED;
320 bool success = false;
321 struct sk_buff *skb = *pskb;
323 len = skb->len + ETH_HLEN;
324 /* Only packets exchanged between two local slaves need to have
325 * device-up check as well as skb-share check.
328 if (unlikely(!(dev->flags & IFF_UP))) {
333 skb = skb_share_check(skb, GFP_ATOMIC);
341 skb->pkt_type = PACKET_HOST;
342 if (dev_forward_skb(ipvlan->dev, skb) == NET_RX_SUCCESS)
346 ret = RX_HANDLER_ANOTHER;
351 ipvlan_count_rx(ipvlan, len, success, false);
355 struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port, void *lyr3h,
356 int addr_type, bool use_dest)
358 struct ipvl_addr *addr = NULL;
361 #if IS_ENABLED(CONFIG_IPV6)
363 struct ipv6hdr *ip6h;
364 struct in6_addr *i6addr;
366 ip6h = (struct ipv6hdr *)lyr3h;
367 i6addr = use_dest ? &ip6h->daddr : &ip6h->saddr;
368 addr = ipvlan_ht_addr_lookup(port, i6addr, true);
373 struct in6_addr *i6addr;
375 /* Make sure that the NeighborSolicitation ICMPv6 packets
376 * are handled to avoid DAD issue.
378 ndmh = (struct nd_msg *)lyr3h;
379 if (ndmh->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
380 i6addr = &ndmh->target;
381 addr = ipvlan_ht_addr_lookup(port, i6addr, true);
390 ip4h = (struct iphdr *)lyr3h;
391 i4addr = use_dest ? &ip4h->daddr : &ip4h->saddr;
392 addr = ipvlan_ht_addr_lookup(port, i4addr, false);
397 unsigned char *arp_ptr;
400 arph = (struct arphdr *)lyr3h;
401 arp_ptr = (unsigned char *)(arph + 1);
403 arp_ptr += (2 * port->dev->addr_len) + 4;
405 arp_ptr += port->dev->addr_len;
407 memcpy(&dip, arp_ptr, 4);
408 addr = ipvlan_ht_addr_lookup(port, &dip, false);
416 static noinline_for_stack int ipvlan_process_v4_outbound(struct sk_buff *skb)
418 const struct iphdr *ip4h = ip_hdr(skb);
419 struct net_device *dev = skb->dev;
420 struct net *net = dev_net(dev);
422 int err, ret = NET_XMIT_DROP;
423 struct flowi4 fl4 = {
424 .flowi4_oif = dev->ifindex,
425 .flowi4_tos = ip4h->tos & INET_DSCP_MASK,
426 .flowi4_flags = FLOWI_FLAG_ANYSRC,
427 .flowi4_mark = skb->mark,
428 .daddr = ip4h->daddr,
429 .saddr = ip4h->saddr,
432 rt = ip_route_output_flow(net, &fl4, NULL);
436 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
440 skb_dst_set(skb, &rt->dst);
442 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
444 err = ip_local_out(net, NULL, skb);
445 if (unlikely(net_xmit_eval(err)))
446 DEV_STATS_INC(dev, tx_errors);
448 ret = NET_XMIT_SUCCESS;
451 DEV_STATS_INC(dev, tx_errors);
457 #if IS_ENABLED(CONFIG_IPV6)
459 static noinline_for_stack int
460 ipvlan_route_v6_outbound(struct net_device *dev, struct sk_buff *skb)
462 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
463 struct flowi6 fl6 = {
464 .flowi6_oif = dev->ifindex,
465 .daddr = ip6h->daddr,
466 .saddr = ip6h->saddr,
467 .flowi6_flags = FLOWI_FLAG_ANYSRC,
468 .flowlabel = ip6_flowinfo(ip6h),
469 .flowi6_mark = skb->mark,
470 .flowi6_proto = ip6h->nexthdr,
472 struct dst_entry *dst;
475 dst = ip6_route_output(dev_net(dev), NULL, &fl6);
481 skb_dst_set(skb, dst);
485 static int ipvlan_process_v6_outbound(struct sk_buff *skb)
487 struct net_device *dev = skb->dev;
488 int err, ret = NET_XMIT_DROP;
490 err = ipvlan_route_v6_outbound(dev, skb);
492 DEV_STATS_INC(dev, tx_errors);
497 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
499 err = ip6_local_out(dev_net(dev), NULL, skb);
500 if (unlikely(net_xmit_eval(err)))
501 DEV_STATS_INC(dev, tx_errors);
503 ret = NET_XMIT_SUCCESS;
507 static int ipvlan_process_v6_outbound(struct sk_buff *skb)
509 return NET_XMIT_DROP;
513 static int ipvlan_process_outbound(struct sk_buff *skb)
515 int ret = NET_XMIT_DROP;
517 /* The ipvlan is a pseudo-L2 device, so the packets that we receive
518 * will have L2; which need to discarded and processed further
519 * in the net-ns of the main-device.
521 if (skb_mac_header_was_set(skb)) {
522 /* In this mode we dont care about
523 * multicast and broadcast traffic */
524 struct ethhdr *ethh = eth_hdr(skb);
526 if (is_multicast_ether_addr(ethh->h_dest)) {
527 pr_debug_ratelimited(
528 "Dropped {multi|broad}cast of type=[%x]\n",
529 ntohs(skb->protocol));
534 skb_pull(skb, sizeof(*ethh));
535 skb->mac_header = (typeof(skb->mac_header))~0U;
536 skb_reset_network_header(skb);
539 if (skb->protocol == htons(ETH_P_IPV6))
540 ret = ipvlan_process_v6_outbound(skb);
541 else if (skb->protocol == htons(ETH_P_IP))
542 ret = ipvlan_process_v4_outbound(skb);
544 pr_warn_ratelimited("Dropped outbound packet type=%x\n",
545 ntohs(skb->protocol));
552 static void ipvlan_multicast_enqueue(struct ipvl_port *port,
553 struct sk_buff *skb, bool tx_pkt)
555 if (skb->protocol == htons(ETH_P_PAUSE)) {
560 /* Record that the deferred packet is from TX or RX path. By
561 * looking at mac-addresses on packet will lead to erronus decisions.
562 * (This would be true for a loopback-mode on master device or a
563 * hair-pin mode of the switch.)
565 IPVL_SKB_CB(skb)->tx_pkt = tx_pkt;
567 spin_lock(&port->backlog.lock);
568 if (skb_queue_len(&port->backlog) < IPVLAN_QBACKLOG_LIMIT) {
570 __skb_queue_tail(&port->backlog, skb);
571 spin_unlock(&port->backlog.lock);
572 schedule_work(&port->wq);
574 spin_unlock(&port->backlog.lock);
575 dev_core_stats_rx_dropped_inc(skb->dev);
580 static int ipvlan_xmit_mode_l3(struct sk_buff *skb, struct net_device *dev)
582 const struct ipvl_dev *ipvlan = netdev_priv(dev);
584 struct ipvl_addr *addr;
587 lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
591 if (!ipvlan_is_vepa(ipvlan->port)) {
592 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
594 if (ipvlan_is_private(ipvlan->port)) {
596 return NET_XMIT_DROP;
598 ipvlan_rcv_frame(addr, &skb, true);
599 return NET_XMIT_SUCCESS;
603 ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
604 return ipvlan_process_outbound(skb);
607 static int ipvlan_xmit_mode_l2(struct sk_buff *skb, struct net_device *dev)
609 const struct ipvl_dev *ipvlan = netdev_priv(dev);
610 struct ethhdr *eth = skb_eth_hdr(skb);
611 struct ipvl_addr *addr;
615 if (!ipvlan_is_vepa(ipvlan->port) &&
616 ether_addr_equal(eth->h_dest, eth->h_source)) {
617 lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
619 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
621 if (ipvlan_is_private(ipvlan->port)) {
623 return NET_XMIT_DROP;
625 ipvlan_rcv_frame(addr, &skb, true);
626 return NET_XMIT_SUCCESS;
629 skb = skb_share_check(skb, GFP_ATOMIC);
631 return NET_XMIT_DROP;
633 /* Packet definitely does not belong to any of the
634 * virtual devices, but the dest is local. So forward
635 * the skb for the main-dev. At the RX side we just return
636 * RX_PASS for it to be processed further on the stack.
638 dev_forward_skb(ipvlan->phy_dev, skb);
639 return NET_XMIT_SUCCESS;
641 } else if (is_multicast_ether_addr(eth->h_dest)) {
642 skb_reset_mac_header(skb);
643 ipvlan_skb_crossing_ns(skb, NULL);
644 ipvlan_multicast_enqueue(ipvlan->port, skb, true);
645 return NET_XMIT_SUCCESS;
648 skb->dev = ipvlan->phy_dev;
649 return dev_queue_xmit(skb);
652 int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
654 struct ipvl_dev *ipvlan = netdev_priv(dev);
655 struct ipvl_port *port = ipvlan_port_get_rcu_bh(ipvlan->phy_dev);
660 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
665 return ipvlan_xmit_mode_l2(skb, dev);
667 #ifdef CONFIG_IPVLAN_L3S
668 case IPVLAN_MODE_L3S:
670 return ipvlan_xmit_mode_l3(skb, dev);
673 /* Should not reach here */
674 WARN_ONCE(true, "%s called for mode = [%x]\n", __func__, port->mode);
677 return NET_XMIT_DROP;
680 static bool ipvlan_external_frame(struct sk_buff *skb, struct ipvl_port *port)
682 struct ethhdr *eth = eth_hdr(skb);
683 struct ipvl_addr *addr;
687 if (ether_addr_equal(eth->h_source, skb->dev->dev_addr)) {
688 lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
692 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, false);
700 static rx_handler_result_t ipvlan_handle_mode_l3(struct sk_buff **pskb,
701 struct ipvl_port *port)
705 struct ipvl_addr *addr;
706 struct sk_buff *skb = *pskb;
707 rx_handler_result_t ret = RX_HANDLER_PASS;
709 lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
713 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
715 ret = ipvlan_rcv_frame(addr, pskb, false);
721 static rx_handler_result_t ipvlan_handle_mode_l2(struct sk_buff **pskb,
722 struct ipvl_port *port)
724 struct sk_buff *skb = *pskb;
725 struct ethhdr *eth = eth_hdr(skb);
726 rx_handler_result_t ret = RX_HANDLER_PASS;
728 if (is_multicast_ether_addr(eth->h_dest)) {
729 if (ipvlan_external_frame(skb, port)) {
730 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
732 /* External frames are queued for device local
733 * distribution, but a copy is given to master
734 * straight away to avoid sending duplicates later
735 * when work-queue processes this frame. This is
736 * achieved by returning RX_HANDLER_PASS.
739 ipvlan_skb_crossing_ns(nskb, NULL);
740 ipvlan_multicast_enqueue(port, nskb, false);
744 /* Perform like l3 mode for non-multicast packet */
745 ret = ipvlan_handle_mode_l3(pskb, port);
751 rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
753 struct sk_buff *skb = *pskb;
754 struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);
757 return RX_HANDLER_PASS;
759 switch (port->mode) {
761 return ipvlan_handle_mode_l2(pskb, port);
763 return ipvlan_handle_mode_l3(pskb, port);
764 #ifdef CONFIG_IPVLAN_L3S
765 case IPVLAN_MODE_L3S:
766 return RX_HANDLER_PASS;
770 /* Should not reach here */
771 WARN_ONCE(true, "%s called for mode = [%x]\n", __func__, port->mode);
773 return RX_HANDLER_CONSUMED;