]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/ipvlan/ipvlan_core.c
Merge back PM core material for v4.16.
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ipvlan / ipvlan_core.c
CommitLineData
2ad7bf36
MB
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
207895fd 12static u32 ipvlan_jhash_secret __read_mostly;
2ad7bf36
MB
13
14void ipvlan_init_secret(void)
15{
16 net_get_random_once(&ipvlan_jhash_secret, sizeof(ipvlan_jhash_secret));
17}
18
235a9d89 19void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
2ad7bf36
MB
20 unsigned int len, bool success, bool mcast)
21{
2ad7bf36
MB
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}
235a9d89 36EXPORT_SYMBOL_GPL(ipvlan_count_rx);
2ad7bf36
MB
37
38static 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
46static 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
ab5b7013
MB
54static struct ipvl_addr *ipvlan_ht_addr_lookup(const struct ipvl_port *port,
55 const void *iaddr, bool is_v6)
2ad7bf36
MB
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
74void 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);
27705f70
JB
82 if (hlist_unhashed(&addr->hlnode))
83 hlist_add_head_rcu(&addr->hlnode, &port->hlhead[hash]);
2ad7bf36
MB
84}
85
6640e673 86void ipvlan_ht_addr_del(struct ipvl_addr *addr)
2ad7bf36 87{
27705f70 88 hlist_del_init_rcu(&addr->hlnode);
2ad7bf36
MB
89}
90
e9997c29
JB
91struct ipvl_addr *ipvlan_find_addr(const struct ipvl_dev *ipvlan,
92 const void *iaddr, bool is_v6)
2ad7bf36 93{
2ad7bf36
MB
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))
e9997c29 101 return addr;
2ad7bf36 102 }
e9997c29
JB
103 return NULL;
104}
2ad7bf36 105
e9997c29
JB
106bool ipvlan_addr_busy(struct ipvl_port *port, void *iaddr, bool is_v6)
107{
108 struct ipvl_dev *ipvlan;
2ad7bf36 109
e9997c29 110 ASSERT_RTNL();
2ad7bf36 111
e9997c29
JB
112 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
113 if (ipvlan_find_addr(ipvlan, iaddr, is_v6))
114 return true;
115 }
2ad7bf36
MB
116 return false;
117}
118
5fc9220a 119static void *ipvlan_get_L3_hdr(struct ipvl_port *port, struct sk_buff *skb, int *type)
2ad7bf36
MB
120{
121 void *lyr3h = NULL;
122
123 switch (skb->protocol) {
124 case htons(ETH_P_ARP): {
125 struct arphdr *arph;
126
5fc9220a 127 if (unlikely(!pskb_may_pull(skb, arp_hdr_len(port->dev))))
2ad7bf36
MB
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) {
747a7135
GF
168 struct icmp6hdr *icmph;
169
170 if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h) + sizeof(*icmph))))
171 return NULL;
172
173 ip6h = ipv6_hdr(skb);
174 icmph = (struct icmp6hdr *)(ip6h + 1);
175
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))))
180 return NULL;
181
182 ip6h = ipv6_hdr(skb);
183 icmph = (struct icmp6hdr *)(ip6h + 1);
184 }
185
2ad7bf36 186 *type = IPVL_ICMPV6;
747a7135 187 lyr3h = icmph;
2ad7bf36
MB
188 }
189 break;
190 }
191 default:
192 return NULL;
193 }
194
195 return lyr3h;
196}
197
198unsigned int ipvlan_mac_hash(const unsigned char *addr)
199{
200 u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
201 ipvlan_jhash_secret);
202
203 return hash & IPVLAN_MAC_FILTER_MASK;
204}
205
ba35f858 206void ipvlan_process_multicast(struct work_struct *work)
2ad7bf36 207{
ba35f858
MB
208 struct ipvl_port *port = container_of(work, struct ipvl_port, wq);
209 struct ethhdr *ethh;
2ad7bf36 210 struct ipvl_dev *ipvlan;
ba35f858
MB
211 struct sk_buff *skb, *nskb;
212 struct sk_buff_head list;
2ad7bf36
MB
213 unsigned int len;
214 unsigned int mac_hash;
215 int ret;
ba35f858 216 u8 pkt_type;
e2525360 217 bool tx_pkt;
2ad7bf36 218
ba35f858 219 __skb_queue_head_init(&list);
2ad7bf36 220
ba35f858
MB
221 spin_lock_bh(&port->backlog.lock);
222 skb_queue_splice_tail_init(&port->backlog, &list);
223 spin_unlock_bh(&port->backlog.lock);
2ad7bf36 224
ba35f858 225 while ((skb = __skb_dequeue(&list)) != NULL) {
b1227d01
ED
226 struct net_device *dev = skb->dev;
227 bool consumed = false;
228
ba35f858 229 ethh = eth_hdr(skb);
e2525360 230 tx_pkt = IPVL_SKB_CB(skb)->tx_pkt;
ba35f858 231 mac_hash = ipvlan_mac_hash(ethh->h_dest);
2ad7bf36 232
ba35f858
MB
233 if (ether_addr_equal(ethh->h_dest, port->dev->broadcast))
234 pkt_type = PACKET_BROADCAST;
2ad7bf36 235 else
ba35f858
MB
236 pkt_type = PACKET_MULTICAST;
237
ba35f858
MB
238 rcu_read_lock();
239 list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
e2525360 240 if (tx_pkt && (ipvlan->dev == skb->dev))
ba35f858 241 continue;
ba35f858
MB
242 if (!test_bit(mac_hash, ipvlan->mac_filters))
243 continue;
b1227d01
ED
244 if (!(ipvlan->dev->flags & IFF_UP))
245 continue;
ba35f858
MB
246 ret = NET_RX_DROP;
247 len = skb->len + ETH_HLEN;
248 nskb = skb_clone(skb, GFP_ATOMIC);
b1227d01
ED
249 local_bh_disable();
250 if (nskb) {
251 consumed = true;
252 nskb->pkt_type = pkt_type;
253 nskb->dev = ipvlan->dev;
e2525360 254 if (tx_pkt)
b1227d01
ED
255 ret = dev_forward_skb(ipvlan->dev, nskb);
256 else
257 ret = netif_rx(nskb);
258 }
ba35f858 259 ipvlan_count_rx(ipvlan, len, ret == NET_RX_SUCCESS, true);
b1227d01 260 local_bh_enable();
ba35f858
MB
261 }
262 rcu_read_unlock();
263
e2525360 264 if (tx_pkt) {
ba35f858
MB
265 /* If the packet originated here, send it out. */
266 skb->dev = port->dev;
267 skb->pkt_type = pkt_type;
268 dev_queue_xmit(skb);
269 } else {
b1227d01
ED
270 if (consumed)
271 consume_skb(skb);
272 else
273 kfree_skb(skb);
2ad7bf36 274 }
b1227d01
ED
275 if (dev)
276 dev_put(dev);
2ad7bf36
MB
277 }
278}
279
b93dd49c
MB
280static void ipvlan_skb_crossing_ns(struct sk_buff *skb, struct net_device *dev)
281{
282 bool xnet = true;
283
284 if (dev)
285 xnet = !net_eq(dev_net(skb->dev), dev_net(dev));
286
287 skb_scrub_packet(skb, xnet);
288 if (dev)
289 skb->dev = dev;
290}
291
cf554ada 292static int ipvlan_rcv_frame(struct ipvl_addr *addr, struct sk_buff **pskb,
2ad7bf36
MB
293 bool local)
294{
295 struct ipvl_dev *ipvlan = addr->master;
296 struct net_device *dev = ipvlan->dev;
297 unsigned int len;
298 rx_handler_result_t ret = RX_HANDLER_CONSUMED;
299 bool success = false;
cf554ada 300 struct sk_buff *skb = *pskb;
2ad7bf36
MB
301
302 len = skb->len + ETH_HLEN;
ab5b7013
MB
303 /* Only packets exchanged between two local slaves need to have
304 * device-up check as well as skb-share check.
305 */
306 if (local) {
307 if (unlikely(!(dev->flags & IFF_UP))) {
308 kfree_skb(skb);
309 goto out;
310 }
2ad7bf36 311
ab5b7013
MB
312 skb = skb_share_check(skb, GFP_ATOMIC);
313 if (!skb)
314 goto out;
2ad7bf36 315
ab5b7013
MB
316 *pskb = skb;
317 }
b93dd49c 318 ipvlan_skb_crossing_ns(skb, dev);
2ad7bf36
MB
319
320 if (local) {
ab5b7013 321 skb->pkt_type = PACKET_HOST;
2ad7bf36
MB
322 if (dev_forward_skb(ipvlan->dev, skb) == NET_RX_SUCCESS)
323 success = true;
324 } else {
325 ret = RX_HANDLER_ANOTHER;
326 success = true;
327 }
328
329out:
330 ipvlan_count_rx(ipvlan, len, success, false);
331 return ret;
332}
333
334static struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port,
335 void *lyr3h, int addr_type,
336 bool use_dest)
337{
338 struct ipvl_addr *addr = NULL;
339
340 if (addr_type == IPVL_IPV6) {
341 struct ipv6hdr *ip6h;
342 struct in6_addr *i6addr;
343
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) {
348 struct nd_msg *ndmh;
349 struct in6_addr *i6addr;
350
351 /* Make sure that the NeighborSolicitation ICMPv6 packets
352 * are handled to avoid DAD issue.
353 */
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);
358 }
359 } else if (addr_type == IPVL_IPV4) {
360 struct iphdr *ip4h;
361 __be32 *i4addr;
362
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) {
367 struct arphdr *arph;
368 unsigned char *arp_ptr;
369 __be32 dip;
370
371 arph = (struct arphdr *)lyr3h;
372 arp_ptr = (unsigned char *)(arph + 1);
373 if (use_dest)
374 arp_ptr += (2 * port->dev->addr_len) + 4;
375 else
376 arp_ptr += port->dev->addr_len;
377
378 memcpy(&dip, arp_ptr, 4);
379 addr = ipvlan_ht_addr_lookup(port, &dip, false);
380 }
381
382 return addr;
383}
384
b93dd49c 385static int ipvlan_process_v4_outbound(struct sk_buff *skb)
2ad7bf36
MB
386{
387 const struct iphdr *ip4h = ip_hdr(skb);
388 struct net_device *dev = skb->dev;
57c4bf85 389 struct net *net = dev_net(dev);
2ad7bf36
MB
390 struct rtable *rt;
391 int err, ret = NET_XMIT_DROP;
392 struct flowi4 fl4 = {
63b11e75 393 .flowi4_oif = dev->ifindex,
2ad7bf36
MB
394 .flowi4_tos = RT_TOS(ip4h->tos),
395 .flowi4_flags = FLOWI_FLAG_ANYSRC,
a98a4ebc 396 .flowi4_mark = skb->mark,
2ad7bf36
MB
397 .daddr = ip4h->daddr,
398 .saddr = ip4h->saddr,
399 };
400
57c4bf85 401 rt = ip_route_output_flow(net, &fl4, NULL);
2ad7bf36
MB
402 if (IS_ERR(rt))
403 goto err;
404
405 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
406 ip_rt_put(rt);
407 goto err;
408 }
2ad7bf36 409 skb_dst_set(skb, &rt->dst);
33224b16 410 err = ip_local_out(net, skb->sk, skb);
2ad7bf36
MB
411 if (unlikely(net_xmit_eval(err)))
412 dev->stats.tx_errors++;
413 else
414 ret = NET_XMIT_SUCCESS;
415 goto out;
416err:
417 dev->stats.tx_errors++;
418 kfree_skb(skb);
419out:
420 return ret;
421}
422
b93dd49c 423static int ipvlan_process_v6_outbound(struct sk_buff *skb)
2ad7bf36
MB
424{
425 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
426 struct net_device *dev = skb->dev;
57c4bf85 427 struct net *net = dev_net(dev);
2ad7bf36
MB
428 struct dst_entry *dst;
429 int err, ret = NET_XMIT_DROP;
430 struct flowi6 fl6 = {
ca29fd7c 431 .flowi6_oif = dev->ifindex,
2ad7bf36
MB
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,
438 };
439
57c4bf85 440 dst = ip6_route_output(net, NULL, &fl6);
2aab9525
MB
441 if (dst->error) {
442 ret = dst->error;
443 dst_release(dst);
2ad7bf36 444 goto err;
2aab9525 445 }
2ad7bf36 446 skb_dst_set(skb, dst);
33224b16 447 err = ip6_local_out(net, skb->sk, skb);
2ad7bf36
MB
448 if (unlikely(net_xmit_eval(err)))
449 dev->stats.tx_errors++;
450 else
451 ret = NET_XMIT_SUCCESS;
452 goto out;
453err:
454 dev->stats.tx_errors++;
455 kfree_skb(skb);
456out:
457 return ret;
458}
459
b93dd49c 460static int ipvlan_process_outbound(struct sk_buff *skb)
2ad7bf36
MB
461{
462 struct ethhdr *ethh = eth_hdr(skb);
463 int ret = NET_XMIT_DROP;
464
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));
469 kfree_skb(skb);
470 goto out;
471 }
472
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.
476 */
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);
481 }
482
483 if (skb->protocol == htons(ETH_P_IPV6))
b93dd49c 484 ret = ipvlan_process_v6_outbound(skb);
2ad7bf36 485 else if (skb->protocol == htons(ETH_P_IP))
b93dd49c 486 ret = ipvlan_process_v4_outbound(skb);
2ad7bf36
MB
487 else {
488 pr_warn_ratelimited("Dropped outbound packet type=%x\n",
489 ntohs(skb->protocol));
490 kfree_skb(skb);
491 }
492out:
493 return ret;
494}
495
ba35f858 496static void ipvlan_multicast_enqueue(struct ipvl_port *port,
e2525360 497 struct sk_buff *skb, bool tx_pkt)
ba35f858
MB
498{
499 if (skb->protocol == htons(ETH_P_PAUSE)) {
500 kfree_skb(skb);
501 return;
502 }
503
e2525360
MB
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.)
508 */
509 IPVL_SKB_CB(skb)->tx_pkt = tx_pkt;
510
ba35f858
MB
511 spin_lock(&port->backlog.lock);
512 if (skb_queue_len(&port->backlog) < IPVLAN_QBACKLOG_LIMIT) {
b1227d01
ED
513 if (skb->dev)
514 dev_hold(skb->dev);
ba35f858
MB
515 __skb_queue_tail(&port->backlog, skb);
516 spin_unlock(&port->backlog.lock);
517 schedule_work(&port->wq);
518 } else {
519 spin_unlock(&port->backlog.lock);
520 atomic_long_inc(&skb->dev->rx_dropped);
521 kfree_skb(skb);
522 }
523}
524
2ad7bf36
MB
525static int ipvlan_xmit_mode_l3(struct sk_buff *skb, struct net_device *dev)
526{
527 const struct ipvl_dev *ipvlan = netdev_priv(dev);
528 void *lyr3h;
529 struct ipvl_addr *addr;
530 int addr_type;
531
5fc9220a 532 lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
2ad7bf36
MB
533 if (!lyr3h)
534 goto out;
535
fe89aa6b
MB
536 if (!ipvlan_is_vepa(ipvlan->port)) {
537 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
538 if (addr) {
539 if (ipvlan_is_private(ipvlan->port)) {
540 consume_skb(skb);
541 return NET_XMIT_DROP;
542 }
543 return ipvlan_rcv_frame(addr, &skb, true);
a190d04d 544 }
a190d04d 545 }
2ad7bf36 546out:
b93dd49c
MB
547 ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
548 return ipvlan_process_outbound(skb);
2ad7bf36
MB
549}
550
551static int ipvlan_xmit_mode_l2(struct sk_buff *skb, struct net_device *dev)
552{
553 const struct ipvl_dev *ipvlan = netdev_priv(dev);
554 struct ethhdr *eth = eth_hdr(skb);
555 struct ipvl_addr *addr;
556 void *lyr3h;
557 int addr_type;
558
fe89aa6b
MB
559 if (!ipvlan_is_vepa(ipvlan->port) &&
560 ether_addr_equal(eth->h_dest, eth->h_source)) {
5fc9220a 561 lyr3h = ipvlan_get_L3_hdr(ipvlan->port, skb, &addr_type);
2ad7bf36
MB
562 if (lyr3h) {
563 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
a190d04d
MB
564 if (addr) {
565 if (ipvlan_is_private(ipvlan->port)) {
566 consume_skb(skb);
567 return NET_XMIT_DROP;
568 }
cf554ada 569 return ipvlan_rcv_frame(addr, &skb, true);
a190d04d 570 }
2ad7bf36
MB
571 }
572 skb = skb_share_check(skb, GFP_ATOMIC);
573 if (!skb)
574 return NET_XMIT_DROP;
575
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.
580 */
581 return dev_forward_skb(ipvlan->phy_dev, skb);
582
583 } else if (is_multicast_ether_addr(eth->h_dest)) {
b93dd49c 584 ipvlan_skb_crossing_ns(skb, NULL);
e2525360 585 ipvlan_multicast_enqueue(ipvlan->port, skb, true);
ba35f858 586 return NET_XMIT_SUCCESS;
2ad7bf36
MB
587 }
588
b93dd49c 589 ipvlan_skb_crossing_ns(skb, ipvlan->phy_dev);
2ad7bf36
MB
590 return dev_queue_xmit(skb);
591}
592
593int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
594{
595 struct ipvl_dev *ipvlan = netdev_priv(dev);
0fba37a3 596 struct ipvl_port *port = ipvlan_port_get_rcu_bh(ipvlan->phy_dev);
2ad7bf36
MB
597
598 if (!port)
599 goto out;
600
601 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
602 goto out;
603
604 switch(port->mode) {
605 case IPVLAN_MODE_L2:
606 return ipvlan_xmit_mode_l2(skb, dev);
607 case IPVLAN_MODE_L3:
4fbae7d8 608 case IPVLAN_MODE_L3S:
2ad7bf36
MB
609 return ipvlan_xmit_mode_l3(skb, dev);
610 }
611
612 /* Should not reach here */
613 WARN_ONCE(true, "ipvlan_queue_xmit() called for mode = [%hx]\n",
614 port->mode);
615out:
616 kfree_skb(skb);
617 return NET_XMIT_DROP;
618}
619
620static bool ipvlan_external_frame(struct sk_buff *skb, struct ipvl_port *port)
621{
622 struct ethhdr *eth = eth_hdr(skb);
623 struct ipvl_addr *addr;
624 void *lyr3h;
625 int addr_type;
626
627 if (ether_addr_equal(eth->h_source, skb->dev->dev_addr)) {
5fc9220a 628 lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
2ad7bf36
MB
629 if (!lyr3h)
630 return true;
631
632 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, false);
633 if (addr)
634 return false;
635 }
636
637 return true;
638}
639
640static rx_handler_result_t ipvlan_handle_mode_l3(struct sk_buff **pskb,
641 struct ipvl_port *port)
642{
643 void *lyr3h;
644 int addr_type;
645 struct ipvl_addr *addr;
646 struct sk_buff *skb = *pskb;
647 rx_handler_result_t ret = RX_HANDLER_PASS;
648
5fc9220a 649 lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
2ad7bf36
MB
650 if (!lyr3h)
651 goto out;
652
653 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
654 if (addr)
cf554ada 655 ret = ipvlan_rcv_frame(addr, pskb, false);
2ad7bf36
MB
656
657out:
658 return ret;
659}
660
661static rx_handler_result_t ipvlan_handle_mode_l2(struct sk_buff **pskb,
662 struct ipvl_port *port)
663{
664 struct sk_buff *skb = *pskb;
665 struct ethhdr *eth = eth_hdr(skb);
666 rx_handler_result_t ret = RX_HANDLER_PASS;
667 void *lyr3h;
668 int addr_type;
669
670 if (is_multicast_ether_addr(eth->h_dest)) {
ba35f858
MB
671 if (ipvlan_external_frame(skb, port)) {
672 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
673
674 /* External frames are queued for device local
675 * distribution, but a copy is given to master
676 * straight away to avoid sending duplicates later
677 * when work-queue processes this frame. This is
678 * achieved by returning RX_HANDLER_PASS.
679 */
b93dd49c
MB
680 if (nskb) {
681 ipvlan_skb_crossing_ns(nskb, NULL);
e2525360 682 ipvlan_multicast_enqueue(port, nskb, false);
b93dd49c 683 }
ba35f858 684 }
2ad7bf36
MB
685 } else {
686 struct ipvl_addr *addr;
687
5fc9220a 688 lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
2ad7bf36
MB
689 if (!lyr3h)
690 return ret;
691
692 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
693 if (addr)
cf554ada 694 ret = ipvlan_rcv_frame(addr, pskb, false);
2ad7bf36
MB
695 }
696
697 return ret;
698}
699
700rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
701{
702 struct sk_buff *skb = *pskb;
703 struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);
704
705 if (!port)
706 return RX_HANDLER_PASS;
707
708 switch (port->mode) {
709 case IPVLAN_MODE_L2:
710 return ipvlan_handle_mode_l2(pskb, port);
711 case IPVLAN_MODE_L3:
712 return ipvlan_handle_mode_l3(pskb, port);
4fbae7d8
MB
713 case IPVLAN_MODE_L3S:
714 return RX_HANDLER_PASS;
2ad7bf36
MB
715 }
716
717 /* Should not reach here */
718 WARN_ONCE(true, "ipvlan_handle_frame() called for mode = [%hx]\n",
719 port->mode);
720 kfree_skb(skb);
a534dc52 721 return RX_HANDLER_CONSUMED;
2ad7bf36 722}
4fbae7d8
MB
723
724static struct ipvl_addr *ipvlan_skb_to_addr(struct sk_buff *skb,
725 struct net_device *dev)
726{
727 struct ipvl_addr *addr = NULL;
728 struct ipvl_port *port;
729 void *lyr3h;
730 int addr_type;
731
732 if (!dev || !netif_is_ipvlan_port(dev))
733 goto out;
734
735 port = ipvlan_port_get_rcu(dev);
736 if (!port || port->mode != IPVLAN_MODE_L3S)
737 goto out;
738
5fc9220a 739 lyr3h = ipvlan_get_L3_hdr(port, skb, &addr_type);
4fbae7d8
MB
740 if (!lyr3h)
741 goto out;
742
743 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
744out:
745 return addr;
746}
747
748struct sk_buff *ipvlan_l3_rcv(struct net_device *dev, struct sk_buff *skb,
749 u16 proto)
750{
751 struct ipvl_addr *addr;
752 struct net_device *sdev;
753
754 addr = ipvlan_skb_to_addr(skb, dev);
755 if (!addr)
756 goto out;
757
758 sdev = addr->master->dev;
759 switch (proto) {
760 case AF_INET:
761 {
762 int err;
763 struct iphdr *ip4h = ip_hdr(skb);
764
765 err = ip_route_input_noref(skb, ip4h->daddr, ip4h->saddr,
766 ip4h->tos, sdev);
767 if (unlikely(err))
768 goto out;
769 break;
770 }
771 case AF_INET6:
772 {
773 struct dst_entry *dst;
774 struct ipv6hdr *ip6h = ipv6_hdr(skb);
775 int flags = RT6_LOOKUP_F_HAS_SADDR;
776 struct flowi6 fl6 = {
777 .flowi6_iif = sdev->ifindex,
778 .daddr = ip6h->daddr,
779 .saddr = ip6h->saddr,
780 .flowlabel = ip6_flowinfo(ip6h),
781 .flowi6_mark = skb->mark,
782 .flowi6_proto = ip6h->nexthdr,
783 };
784
785 skb_dst_drop(skb);
786 dst = ip6_route_input_lookup(dev_net(sdev), sdev, &fl6, flags);
787 skb_dst_set(skb, dst);
788 break;
789 }
790 default:
791 break;
792 }
793
794out:
795 return skb;
796}
797
798unsigned int ipvlan_nf_input(void *priv, struct sk_buff *skb,
799 const struct nf_hook_state *state)
800{
801 struct ipvl_addr *addr;
802 unsigned int len;
803
804 addr = ipvlan_skb_to_addr(skb, skb->dev);
805 if (!addr)
806 goto out;
807
808 skb->dev = addr->master->dev;
809 len = skb->len + ETH_HLEN;
810 ipvlan_count_rx(addr->master, len, true, false);
811out:
812 return NF_ACCEPT;
813}