GNU Linux-libre 4.14.295-gnu1
[releases.git] / drivers / net / ipvlan / ipvlan_core.c
1 /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
2  *
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.
7  *
8  */
9
10 #include "ipvlan.h"
11
12 static u32 ipvlan_jhash_secret __read_mostly;
13
14 void ipvlan_init_secret(void)
15 {
16         net_get_random_once(&ipvlan_jhash_secret, sizeof(ipvlan_jhash_secret));
17 }
18
19 void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
20                             unsigned int len, bool success, bool mcast)
21 {
22         if (likely(success)) {
23                 struct ipvl_pcpu_stats *pcptr;
24
25                 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
26                 u64_stats_update_begin(&pcptr->syncp);
27                 pcptr->rx_pkts++;
28                 pcptr->rx_bytes += len;
29                 if (mcast)
30                         pcptr->rx_mcast++;
31                 u64_stats_update_end(&pcptr->syncp);
32         } else {
33                 this_cpu_inc(ipvlan->pcpu_stats->rx_errs);
34         }
35 }
36 EXPORT_SYMBOL_GPL(ipvlan_count_rx);
37
38 static u8 ipvlan_get_v6_hash(const void *iaddr)
39 {
40         const struct in6_addr *ip6_addr = iaddr;
41
42         return __ipv6_addr_jhash(ip6_addr, ipvlan_jhash_secret) &
43                IPVLAN_HASH_MASK;
44 }
45
46 static u8 ipvlan_get_v4_hash(const void *iaddr)
47 {
48         const struct in_addr *ip4_addr = iaddr;
49
50         return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret) &
51                IPVLAN_HASH_MASK;
52 }
53
54 static struct ipvl_addr *ipvlan_ht_addr_lookup(const struct ipvl_port *port,
55                                                const void *iaddr, bool is_v6)
56 {
57         struct ipvl_addr *addr;
58         u8 hash;
59
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))
65                         return addr;
66                 else if (!is_v6 && addr->atype == IPVL_IPV4 &&
67                          addr->ip4addr.s_addr ==
68                                 ((struct in_addr *)iaddr)->s_addr)
69                         return addr;
70         }
71         return NULL;
72 }
73
74 void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr)
75 {
76         struct ipvl_port *port = ipvlan->port;
77         u8 hash;
78
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]);
84 }
85
86 void ipvlan_ht_addr_del(struct ipvl_addr *addr)
87 {
88         hlist_del_init_rcu(&addr->hlnode);
89 }
90
91 struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
92                                    const void *iaddr, bool is_v6)
93 {
94         struct ipvl_addr *addr;
95
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))
101                         return addr;
102         }
103         return NULL;
104 }
105
106 bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6)
107 {
108         struct ipvl_dev *ipvlan;
109
110         ASSERT_RTNL();
111
112         list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
113                 if (ipvlan_find_addr(ipvlan, iaddr, is_v6))
114                         return true;
115         }
116         return false;
117 }
118
119 static void *ipvlan_get_L3_hdr(struct sk_buff *skb, int *type)
120 {
121         void *lyr3h = NULL;
122
123         switch (skb->protocol) {
124         case htons(ETH_P_ARP): {
125                 struct arphdr *arph;
126
127                 if (unlikely(!pskb_may_pull(skb, sizeof(*arph))))
128                         return NULL;
129
130                 arph = arp_hdr(skb);
131                 *type = IPVL_ARP;
132                 lyr3h = arph;
133                 break;
134         }
135         case htons(ETH_P_IP): {
136                 u32 pktlen;
137                 struct iphdr *ip4h;
138
139                 if (unlikely(!pskb_may_pull(skb, sizeof(*ip4h))))
140                         return NULL;
141
142                 ip4h = ip_hdr(skb);
143                 pktlen = ntohs(ip4h->tot_len);
144                 if (ip4h->ihl < 5 || ip4h->version != 4)
145                         return NULL;
146                 if (skb->len < pktlen || pktlen < (ip4h->ihl * 4))
147                         return NULL;
148
149                 *type = IPVL_IPV4;
150                 lyr3h = ip4h;
151                 break;
152         }
153         case htons(ETH_P_IPV6): {
154                 struct ipv6hdr *ip6h;
155
156                 if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h))))
157                         return NULL;
158
159                 ip6h = ipv6_hdr(skb);
160                 if (ip6h->version != 6)
161                         return NULL;
162
163                 *type = IPVL_IPV6;
164                 lyr3h = ip6h;
165                 /* Only Neighbour Solicitation pkts need different treatment */
166                 if (ipv6_addr_any(&ip6h->saddr) &&
167                     ip6h->nexthdr == NEXTHDR_ICMP) {
168                         *type = IPVL_ICMPV6;
169                         lyr3h = ip6h + 1;
170                 }
171                 break;
172         }
173         default:
174                 return NULL;
175         }
176
177         return lyr3h;
178 }
179
180 unsigned int ipvlan_mac_hash(const unsigned char *addr)
181 {
182         u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
183                                ipvlan_jhash_secret);
184
185         return hash & IPVLAN_MAC_FILTER_MASK;
186 }
187
188 void ipvlan_process_multicast(struct work_struct *work)
189 {
190         struct ipvl_port *port = container_of(work, struct ipvl_port, wq);
191         struct ethhdr *ethh;
192         struct ipvl_dev *ipvlan;
193         struct sk_buff *skb, *nskb;
194         struct sk_buff_head list;
195         unsigned int len;
196         unsigned int mac_hash;
197         int ret;
198         u8 pkt_type;
199         bool tx_pkt;
200
201         __skb_queue_head_init(&list);
202
203         spin_lock_bh(&port->backlog.lock);
204         skb_queue_splice_tail_init(&port->backlog, &list);
205         spin_unlock_bh(&port->backlog.lock);
206
207         while ((skb = __skb_dequeue(&list)) != NULL) {
208                 struct net_device *dev = skb->dev;
209                 bool consumed = false;
210
211                 ethh = eth_hdr(skb);
212                 tx_pkt = IPVL_SKB_CB(skb)->tx_pkt;
213                 mac_hash = ipvlan_mac_hash(ethh->h_dest);
214
215                 if (ether_addr_equal(ethh->h_dest, port->dev->broadcast))
216                         pkt_type = PACKET_BROADCAST;
217                 else
218                         pkt_type = PACKET_MULTICAST;
219
220                 rcu_read_lock();
221                 list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
222                         if (tx_pkt && (ipvlan->dev == skb->dev))
223                                 continue;
224                         if (!test_bit(mac_hash, ipvlan->mac_filters))
225                                 continue;
226                         if (!(ipvlan->dev->flags & IFF_UP))
227                                 continue;
228                         ret = NET_RX_DROP;
229                         len = skb->len + ETH_HLEN;
230                         nskb = skb_clone(skb, GFP_ATOMIC);
231                         local_bh_disable();
232                         if (nskb) {
233                                 consumed = true;
234                                 nskb->pkt_type = pkt_type;
235                                 nskb->dev = ipvlan->dev;
236                                 if (tx_pkt)
237                                         ret = dev_forward_skb(ipvlan->dev, nskb);
238                                 else
239                                         ret = netif_rx(nskb);
240                         }
241                         ipvlan_count_rx(ipvlan, len, ret == NET_RX_SUCCESS, true);
242                         local_bh_enable();
243                 }
244                 rcu_read_unlock();
245
246                 if (tx_pkt) {
247                         /* If the packet originated here, send it out. */
248                         skb->dev = port->dev;
249                         skb->pkt_type = pkt_type;
250                         dev_queue_xmit(skb);
251                 } else {
252                         if (consumed)
253                                 consume_skb(skb);
254                         else
255                                 kfree_skb(skb);
256                 }
257                 if (dev)
258                         dev_put(dev);
259                 cond_resched();
260         }
261 }
262
263 static void ipvlan_skb_crossing_ns(struct sk_buff *skb, struct net_device *dev)
264 {
265         bool xnet = true;
266
267         if (dev)
268                 xnet = !net_eq(dev_net(skb->dev), dev_net(dev));
269
270         skb_scrub_packet(skb, xnet);
271         if (dev)
272                 skb->dev = dev;
273 }
274
275 static int ipvlan_rcv_frame(struct ipvl_addr *addr, struct sk_buff **pskb,
276                             bool local)
277 {
278         struct ipvl_dev *ipvlan = addr->master;
279         struct net_device *dev = ipvlan->dev;
280         unsigned int len;
281         rx_handler_result_t ret = RX_HANDLER_CONSUMED;
282         bool success = false;
283         struct sk_buff *skb = *pskb;
284
285         len = skb->len + ETH_HLEN;
286         /* Only packets exchanged between two local slaves need to have
287          * device-up check as well as skb-share check.
288          */
289         if (local) {
290                 if (unlikely(!(dev->flags & IFF_UP))) {
291                         kfree_skb(skb);
292                         goto out;
293                 }
294
295                 skb = skb_share_check(skb, GFP_ATOMIC);
296                 if (!skb)
297                         goto out;
298
299                 *pskb = skb;
300         }
301         ipvlan_skb_crossing_ns(skb, dev);
302
303         if (local) {
304                 skb->pkt_type = PACKET_HOST;
305                 if (dev_forward_skb(ipvlan->dev, skb) == NET_RX_SUCCESS)
306                         success = true;
307         } else {
308                 if (!ether_addr_equal_64bits(eth_hdr(skb)->h_dest,
309                                              ipvlan->phy_dev->dev_addr))
310                         skb->pkt_type = PACKET_OTHERHOST;
311
312                 ret = RX_HANDLER_ANOTHER;
313                 success = true;
314         }
315
316 out:
317         ipvlan_count_rx(ipvlan, len, success, false);
318         return ret;
319 }
320
321 static struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port,
322                                             void *lyr3h, int addr_type,
323                                             bool use_dest)
324 {
325         struct ipvl_addr *addr = NULL;
326
327         if (addr_type == IPVL_IPV6) {
328                 struct ipv6hdr *ip6h;
329                 struct in6_addr *i6addr;
330
331                 ip6h = (struct ipv6hdr *)lyr3h;
332                 i6addr = use_dest ? &ip6h->daddr : &ip6h->saddr;
333                 addr = ipvlan_ht_addr_lookup(port, i6addr, true);
334         } else if (addr_type == IPVL_ICMPV6) {
335                 struct nd_msg *ndmh;
336                 struct in6_addr *i6addr;
337
338                 /* Make sure that the NeighborSolicitation ICMPv6 packets
339                  * are handled to avoid DAD issue.
340                  */
341                 ndmh = (struct nd_msg *)lyr3h;
342                 if (ndmh->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
343                         i6addr = &ndmh->target;
344                         addr = ipvlan_ht_addr_lookup(port, i6addr, true);
345                 }
346         } else if (addr_type == IPVL_IPV4) {
347                 struct iphdr *ip4h;
348                 __be32 *i4addr;
349
350                 ip4h = (struct iphdr *)lyr3h;
351                 i4addr = use_dest ? &ip4h->daddr : &ip4h->saddr;
352                 addr = ipvlan_ht_addr_lookup(port, i4addr, false);
353         } else if (addr_type == IPVL_ARP) {
354                 struct arphdr *arph;
355                 unsigned char *arp_ptr;
356                 __be32 dip;
357
358                 arph = (struct arphdr *)lyr3h;
359                 arp_ptr = (unsigned char *)(arph + 1);
360                 if (use_dest)
361                         arp_ptr += (2 * port->dev->addr_len) + 4;
362                 else
363                         arp_ptr += port->dev->addr_len;
364
365                 memcpy(&dip, arp_ptr, 4);
366                 addr = ipvlan_ht_addr_lookup(port, &dip, false);
367         }
368
369         return addr;
370 }
371
372 static int ipvlan_process_v4_outbound(struct sk_buff *skb)
373 {
374         const struct iphdr *ip4h = ip_hdr(skb);
375         struct net_device *dev = skb->dev;
376         struct net *net = dev_net(dev);
377         struct rtable *rt;
378         int err, ret = NET_XMIT_DROP;
379         struct flowi4 fl4 = {
380                 .flowi4_oif = dev->ifindex,
381                 .flowi4_tos = RT_TOS(ip4h->tos),
382                 .flowi4_flags = FLOWI_FLAG_ANYSRC,
383                 .flowi4_mark = skb->mark,
384                 .daddr = ip4h->daddr,
385                 .saddr = ip4h->saddr,
386         };
387
388         rt = ip_route_output_flow(net, &fl4, NULL);
389         if (IS_ERR(rt))
390                 goto err;
391
392         if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
393                 ip_rt_put(rt);
394                 goto err;
395         }
396         skb_dst_set(skb, &rt->dst);
397         err = ip_local_out(net, skb->sk, skb);
398         if (unlikely(net_xmit_eval(err)))
399                 dev->stats.tx_errors++;
400         else
401                 ret = NET_XMIT_SUCCESS;
402         goto out;
403 err:
404         dev->stats.tx_errors++;
405         kfree_skb(skb);
406 out:
407         return ret;
408 }
409
410 static int ipvlan_process_v6_outbound(struct sk_buff *skb)
411 {
412         const struct ipv6hdr *ip6h = ipv6_hdr(skb);
413         struct net_device *dev = skb->dev;
414         struct net *net = dev_net(dev);
415         struct dst_entry *dst;
416         int err, ret = NET_XMIT_DROP;
417         struct flowi6 fl6 = {
418                 .flowi6_oif = dev->ifindex,
419                 .daddr = ip6h->daddr,
420                 .saddr = ip6h->saddr,
421                 .flowi6_flags = FLOWI_FLAG_ANYSRC,
422                 .flowlabel = ip6_flowinfo(ip6h),
423                 .flowi6_mark = skb->mark,
424                 .flowi6_proto = ip6h->nexthdr,
425         };
426
427         dst = ip6_route_output(net, NULL, &fl6);
428         if (dst->error) {
429                 ret = dst->error;
430                 dst_release(dst);
431                 goto err;
432         }
433         skb_dst_set(skb, dst);
434         err = ip6_local_out(net, skb->sk, skb);
435         if (unlikely(net_xmit_eval(err)))
436                 dev->stats.tx_errors++;
437         else
438                 ret = NET_XMIT_SUCCESS;
439         goto out;
440 err:
441         dev->stats.tx_errors++;
442         kfree_skb(skb);
443 out:
444         return ret;
445 }
446
447 static int ipvlan_process_outbound(struct sk_buff *skb)
448 {
449         int ret = NET_XMIT_DROP;
450
451         /* The ipvlan is a pseudo-L2 device, so the packets that we receive
452          * will have L2; which need to discarded and processed further
453          * in the net-ns of the main-device.
454          */
455         if (skb_mac_header_was_set(skb)) {
456                 /* In this mode we dont care about
457                  * multicast and broadcast traffic */
458                 struct ethhdr *ethh = eth_hdr(skb);
459
460                 if (is_multicast_ether_addr(ethh->h_dest)) {
461                         pr_debug_ratelimited(
462                                 "Dropped {multi|broad}cast of type=[%x]\n",
463                                 ntohs(skb->protocol));
464                         kfree_skb(skb);
465                         goto out;
466                 }
467
468                 skb_pull(skb, sizeof(*ethh));
469                 skb->mac_header = (typeof(skb->mac_header))~0U;
470                 skb_reset_network_header(skb);
471         }
472
473         if (skb->protocol == htons(ETH_P_IPV6))
474                 ret = ipvlan_process_v6_outbound(skb);
475         else if (skb->protocol == htons(ETH_P_IP))
476                 ret = ipvlan_process_v4_outbound(skb);
477         else {
478                 pr_warn_ratelimited("Dropped outbound packet type=%x\n",
479                                     ntohs(skb->protocol));
480                 kfree_skb(skb);
481         }
482 out:
483         return ret;
484 }
485
486 static void ipvlan_multicast_enqueue(struct ipvl_port *port,
487                                      struct sk_buff *skb, bool tx_pkt)
488 {
489         if (skb->protocol == htons(ETH_P_PAUSE)) {
490                 kfree_skb(skb);
491                 return;
492         }
493
494         /* Record that the deferred packet is from TX or RX path. By
495          * looking at mac-addresses on packet will lead to erronus decisions.
496          * (This would be true for a loopback-mode on master device or a
497          * hair-pin mode of the switch.)
498          */
499         IPVL_SKB_CB(skb)->tx_pkt = tx_pkt;
500
501         spin_lock(&port->backlog.lock);
502         if (skb_queue_len(&port->backlog) < IPVLAN_QBACKLOG_LIMIT) {
503                 if (skb->dev)
504                         dev_hold(skb->dev);
505                 __skb_queue_tail(&port->backlog, skb);
506                 spin_unlock(&port->backlog.lock);
507                 schedule_work(&port->wq);
508         } else {
509                 spin_unlock(&port->backlog.lock);
510                 atomic_long_inc(&skb->dev->rx_dropped);
511                 kfree_skb(skb);
512         }
513 }
514
515 static int ipvlan_xmit_mode_l3(struct sk_buff *skb, struct net_device *dev)
516 {
517         const struct ipvl_dev *ipvlan = netdev_priv(dev);
518         void *lyr3h;
519         struct ipvl_addr *addr;
520         int addr_type;
521
522         lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
523         if (!lyr3h)
524                 goto out;
525
526         addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
527         if (addr)
528                 return ipvlan_rcv_frame(addr, &skb, true);
529
530 out:
531         ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
532         return ipvlan_process_outbound(skb);
533 }
534
535 static int ipvlan_xmit_mode_l2(struct sk_buff *skb, struct net_device *dev)
536 {
537         const struct ipvl_dev *ipvlan = netdev_priv(dev);
538         struct ethhdr *eth = skb_eth_hdr(skb);
539         struct ipvl_addr *addr;
540         void *lyr3h;
541         int addr_type;
542
543         if (ether_addr_equal(eth->h_dest, eth->h_source)) {
544                 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
545                 if (lyr3h) {
546                         addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
547                         if (addr)
548                                 return ipvlan_rcv_frame(addr, &skb, true);
549                 }
550                 skb = skb_share_check(skb, GFP_ATOMIC);
551                 if (!skb)
552                         return NET_XMIT_DROP;
553
554                 /* Packet definitely does not belong to any of the
555                  * virtual devices, but the dest is local. So forward
556                  * the skb for the main-dev. At the RX side we just return
557                  * RX_PASS for it to be processed further on the stack.
558                  */
559                 return dev_forward_skb(ipvlan->phy_dev, skb);
560
561         } else if (is_multicast_ether_addr(eth->h_dest)) {
562                 skb_reset_mac_header(skb);
563                 ipvlan_skb_crossing_ns(skb, NULL);
564                 ipvlan_multicast_enqueue(ipvlan->port, skb, true);
565                 return NET_XMIT_SUCCESS;
566         }
567
568         ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
569         return dev_queue_xmit(skb);
570 }
571
572 int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
573 {
574         struct ipvl_dev *ipvlan = netdev_priv(dev);
575         struct ipvl_port *port = ipvlan_port_get_rcu_bh(ipvlan->phy_dev);
576
577         if (!port)
578                 goto out;
579
580         if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
581                 goto out;
582
583         switch(port->mode) {
584         case IPVLAN_MODE_L2:
585                 return ipvlan_xmit_mode_l2(skb, dev);
586         case IPVLAN_MODE_L3:
587         case IPVLAN_MODE_L3S:
588                 return ipvlan_xmit_mode_l3(skb, dev);
589         }
590
591         /* Should not reach here */
592         WARN_ONCE(true, "ipvlan_queue_xmit() called for mode = [%hx]\n",
593                           port->mode);
594 out:
595         kfree_skb(skb);
596         return NET_XMIT_DROP;
597 }
598
599 static bool ipvlan_external_frame(struct sk_buff *skb, struct ipvl_port *port)
600 {
601         struct ethhdr *eth = eth_hdr(skb);
602         struct ipvl_addr *addr;
603         void *lyr3h;
604         int addr_type;
605
606         if (ether_addr_equal(eth->h_source, skb->dev->dev_addr)) {
607                 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
608                 if (!lyr3h)
609                         return true;
610
611                 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, false);
612                 if (addr)
613                         return false;
614         }
615
616         return true;
617 }
618
619 static rx_handler_result_t ipvlan_handle_mode_l3(struct sk_buff **pskb,
620                                                  struct ipvl_port *port)
621 {
622         void *lyr3h;
623         int addr_type;
624         struct ipvl_addr *addr;
625         struct sk_buff *skb = *pskb;
626         rx_handler_result_t ret = RX_HANDLER_PASS;
627
628         lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
629         if (!lyr3h)
630                 goto out;
631
632         addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
633         if (addr)
634                 ret = ipvlan_rcv_frame(addr, pskb, false);
635
636 out:
637         return ret;
638 }
639
640 static rx_handler_result_t ipvlan_handle_mode_l2(struct sk_buff **pskb,
641                                                  struct ipvl_port *port)
642 {
643         struct sk_buff *skb = *pskb;
644         struct ethhdr *eth = eth_hdr(skb);
645         rx_handler_result_t ret = RX_HANDLER_PASS;
646         void *lyr3h;
647         int addr_type;
648
649         if (is_multicast_ether_addr(eth->h_dest)) {
650                 if (ipvlan_external_frame(skb, port)) {
651                         struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
652
653                         /* External frames are queued for device local
654                          * distribution, but a copy is given to master
655                          * straight away to avoid sending duplicates later
656                          * when work-queue processes this frame. This is
657                          * achieved by returning RX_HANDLER_PASS.
658                          */
659                         if (nskb) {
660                                 ipvlan_skb_crossing_ns(nskb, NULL);
661                                 ipvlan_multicast_enqueue(port, nskb, false);
662                         }
663                 }
664         } else {
665                 struct ipvl_addr *addr;
666
667                 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
668                 if (!lyr3h)
669                         return ret;
670
671                 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
672                 if (addr)
673                         ret = ipvlan_rcv_frame(addr, pskb, false);
674         }
675
676         return ret;
677 }
678
679 rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
680 {
681         struct sk_buff *skb = *pskb;
682         struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);
683
684         if (!port)
685                 return RX_HANDLER_PASS;
686
687         switch (port->mode) {
688         case IPVLAN_MODE_L2:
689                 return ipvlan_handle_mode_l2(pskb, port);
690         case IPVLAN_MODE_L3:
691                 return ipvlan_handle_mode_l3(pskb, port);
692         case IPVLAN_MODE_L3S:
693                 return RX_HANDLER_PASS;
694         }
695
696         /* Should not reach here */
697         WARN_ONCE(true, "ipvlan_handle_frame() called for mode = [%hx]\n",
698                           port->mode);
699         kfree_skb(skb);
700         return RX_HANDLER_CONSUMED;
701 }
702
703 static struct ipvl_addr *ipvlan_skb_to_addr(struct sk_buff *skb,
704                                             struct net_device *dev)
705 {
706         struct ipvl_addr *addr = NULL;
707         struct ipvl_port *port;
708         void *lyr3h;
709         int addr_type;
710
711         if (!dev || !netif_is_ipvlan_port(dev))
712                 goto out;
713
714         port = ipvlan_port_get_rcu(dev);
715         if (!port || port->mode != IPVLAN_MODE_L3S)
716                 goto out;
717
718         lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
719         if (!lyr3h)
720                 goto out;
721
722         addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
723 out:
724         return addr;
725 }
726
727 struct sk_buff *ipvlan_l3_rcv(struct net_device *dev, struct sk_buff *skb,
728                               u16 proto)
729 {
730         struct ipvl_addr *addr;
731         struct net_device *sdev;
732
733         addr = ipvlan_skb_to_addr(skb, dev);
734         if (!addr)
735                 goto out;
736
737         sdev = addr->master->dev;
738         switch (proto) {
739         case AF_INET:
740         {
741                 int err;
742                 struct iphdr *ip4h = ip_hdr(skb);
743
744                 err = ip_route_input_noref(skb, ip4h->daddr, ip4h->saddr,
745                                            ip4h->tos, sdev);
746                 if (unlikely(err))
747                         goto out;
748                 break;
749         }
750         case AF_INET6:
751         {
752                 struct dst_entry *dst;
753                 struct ipv6hdr *ip6h = ipv6_hdr(skb);
754                 int flags = RT6_LOOKUP_F_HAS_SADDR;
755                 struct flowi6 fl6 = {
756                         .flowi6_iif   = sdev->ifindex,
757                         .daddr        = ip6h->daddr,
758                         .saddr        = ip6h->saddr,
759                         .flowlabel    = ip6_flowinfo(ip6h),
760                         .flowi6_mark  = skb->mark,
761                         .flowi6_proto = ip6h->nexthdr,
762                 };
763
764                 skb_dst_drop(skb);
765                 dst = ip6_route_input_lookup(dev_net(sdev), sdev, &fl6, flags);
766                 skb_dst_set(skb, dst);
767                 break;
768         }
769         default:
770                 break;
771         }
772
773 out:
774         return skb;
775 }
776
777 unsigned int ipvlan_nf_input(void *priv, struct sk_buff *skb,
778                              const struct nf_hook_state *state)
779 {
780         struct ipvl_addr *addr;
781         unsigned int len;
782
783         addr = ipvlan_skb_to_addr(skb, skb->dev);
784         if (!addr)
785                 goto out;
786
787         skb->dev = addr->master->dev;
788         len = skb->len + ETH_HLEN;
789         ipvlan_count_rx(addr->master, len, true, false);
790 out:
791         return NF_ACCEPT;
792 }