ipvlan: fix addr hash list corruption
[deliverable/linux.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
19static void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
20 unsigned int len, bool success, bool mcast)
21{
22 if (!ipvlan)
23 return;
24
25 if (likely(success)) {
26 struct ipvl_pcpu_stats *pcptr;
27
28 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
29 u64_stats_update_begin(&pcptr->syncp);
30 pcptr->rx_pkts++;
31 pcptr->rx_bytes += len;
32 if (mcast)
33 pcptr->rx_mcast++;
34 u64_stats_update_end(&pcptr->syncp);
35 } else {
36 this_cpu_inc(ipvlan->pcpu_stats->rx_errs);
37 }
38}
39
40static u8 ipvlan_get_v6_hash(const void *iaddr)
41{
42 const struct in6_addr *ip6_addr = iaddr;
43
44 return __ipv6_addr_jhash(ip6_addr, ipvlan_jhash_secret) &
45 IPVLAN_HASH_MASK;
46}
47
48static u8 ipvlan_get_v4_hash(const void *iaddr)
49{
50 const struct in_addr *ip4_addr = iaddr;
51
52 return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret) &
53 IPVLAN_HASH_MASK;
54}
55
56struct ipvl_addr *ipvlan_ht_addr_lookup(const struct ipvl_port *port,
57 const void *iaddr, bool is_v6)
58{
59 struct ipvl_addr *addr;
60 u8 hash;
61
62 hash = is_v6 ? ipvlan_get_v6_hash(iaddr) :
63 ipvlan_get_v4_hash(iaddr);
64 hlist_for_each_entry_rcu(addr, &port->hlhead[hash], hlnode) {
65 if (is_v6 && addr->atype == IPVL_IPV6 &&
66 ipv6_addr_equal(&addr->ip6addr, iaddr))
67 return addr;
68 else if (!is_v6 && addr->atype == IPVL_IPV4 &&
69 addr->ip4addr.s_addr ==
70 ((struct in_addr *)iaddr)->s_addr)
71 return addr;
72 }
73 return NULL;
74}
75
76void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr)
77{
78 struct ipvl_port *port = ipvlan->port;
79 u8 hash;
80
81 hash = (addr->atype == IPVL_IPV6) ?
82 ipvlan_get_v6_hash(&addr->ip6addr) :
83 ipvlan_get_v4_hash(&addr->ip4addr);
27705f70
JB
84 if (hlist_unhashed(&addr->hlnode))
85 hlist_add_head_rcu(&addr->hlnode, &port->hlhead[hash]);
2ad7bf36
MB
86}
87
88void ipvlan_ht_addr_del(struct ipvl_addr *addr, bool sync)
89{
27705f70 90 hlist_del_init_rcu(&addr->hlnode);
2ad7bf36
MB
91 if (sync)
92 synchronize_rcu();
93}
94
95bool ipvlan_addr_busy(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
96{
97 struct ipvl_port *port = ipvlan->port;
98 struct ipvl_addr *addr;
99
100 list_for_each_entry(addr, &ipvlan->addrs, anode) {
101 if ((is_v6 && addr->atype == IPVL_IPV6 &&
102 ipv6_addr_equal(&addr->ip6addr, iaddr)) ||
103 (!is_v6 && addr->atype == IPVL_IPV4 &&
104 addr->ip4addr.s_addr == ((struct in_addr *)iaddr)->s_addr))
105 return true;
106 }
107
108 if (ipvlan_ht_addr_lookup(port, iaddr, is_v6))
109 return true;
110
111 return false;
112}
113
114static void *ipvlan_get_L3_hdr(struct sk_buff *skb, int *type)
115{
116 void *lyr3h = NULL;
117
118 switch (skb->protocol) {
119 case htons(ETH_P_ARP): {
120 struct arphdr *arph;
121
122 if (unlikely(!pskb_may_pull(skb, sizeof(*arph))))
123 return NULL;
124
125 arph = arp_hdr(skb);
126 *type = IPVL_ARP;
127 lyr3h = arph;
128 break;
129 }
130 case htons(ETH_P_IP): {
131 u32 pktlen;
132 struct iphdr *ip4h;
133
134 if (unlikely(!pskb_may_pull(skb, sizeof(*ip4h))))
135 return NULL;
136
137 ip4h = ip_hdr(skb);
138 pktlen = ntohs(ip4h->tot_len);
139 if (ip4h->ihl < 5 || ip4h->version != 4)
140 return NULL;
141 if (skb->len < pktlen || pktlen < (ip4h->ihl * 4))
142 return NULL;
143
144 *type = IPVL_IPV4;
145 lyr3h = ip4h;
146 break;
147 }
148 case htons(ETH_P_IPV6): {
149 struct ipv6hdr *ip6h;
150
151 if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h))))
152 return NULL;
153
154 ip6h = ipv6_hdr(skb);
155 if (ip6h->version != 6)
156 return NULL;
157
158 *type = IPVL_IPV6;
159 lyr3h = ip6h;
160 /* Only Neighbour Solicitation pkts need different treatment */
161 if (ipv6_addr_any(&ip6h->saddr) &&
162 ip6h->nexthdr == NEXTHDR_ICMP) {
163 *type = IPVL_ICMPV6;
164 lyr3h = ip6h + 1;
165 }
166 break;
167 }
168 default:
169 return NULL;
170 }
171
172 return lyr3h;
173}
174
175unsigned int ipvlan_mac_hash(const unsigned char *addr)
176{
177 u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
178 ipvlan_jhash_secret);
179
180 return hash & IPVLAN_MAC_FILTER_MASK;
181}
182
183static void ipvlan_multicast_frame(struct ipvl_port *port, struct sk_buff *skb,
184 const struct ipvl_dev *in_dev, bool local)
185{
186 struct ethhdr *eth = eth_hdr(skb);
187 struct ipvl_dev *ipvlan;
188 struct sk_buff *nskb;
189 unsigned int len;
190 unsigned int mac_hash;
191 int ret;
192
193 if (skb->protocol == htons(ETH_P_PAUSE))
194 return;
195
196 list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
197 if (local && (ipvlan == in_dev))
198 continue;
199
200 mac_hash = ipvlan_mac_hash(eth->h_dest);
201 if (!test_bit(mac_hash, ipvlan->mac_filters))
202 continue;
203
204 ret = NET_RX_DROP;
205 len = skb->len + ETH_HLEN;
206 nskb = skb_clone(skb, GFP_ATOMIC);
207 if (!nskb)
208 goto mcast_acct;
209
210 if (ether_addr_equal(eth->h_dest, ipvlan->phy_dev->broadcast))
211 nskb->pkt_type = PACKET_BROADCAST;
212 else
213 nskb->pkt_type = PACKET_MULTICAST;
214
215 nskb->dev = ipvlan->dev;
216 if (local)
217 ret = dev_forward_skb(ipvlan->dev, nskb);
218 else
219 ret = netif_rx(nskb);
220mcast_acct:
221 ipvlan_count_rx(ipvlan, len, ret == NET_RX_SUCCESS, true);
222 }
223
224 /* Locally generated? ...Forward a copy to the main-device as
225 * well. On the RX side we'll ignore it (wont give it to any
226 * of the virtual devices.
227 */
228 if (local) {
229 nskb = skb_clone(skb, GFP_ATOMIC);
230 if (nskb) {
231 if (ether_addr_equal(eth->h_dest, port->dev->broadcast))
232 nskb->pkt_type = PACKET_BROADCAST;
233 else
234 nskb->pkt_type = PACKET_MULTICAST;
235
236 dev_forward_skb(port->dev, nskb);
237 }
238 }
239}
240
241static int ipvlan_rcv_frame(struct ipvl_addr *addr, struct sk_buff *skb,
242 bool local)
243{
244 struct ipvl_dev *ipvlan = addr->master;
245 struct net_device *dev = ipvlan->dev;
246 unsigned int len;
247 rx_handler_result_t ret = RX_HANDLER_CONSUMED;
248 bool success = false;
249
250 len = skb->len + ETH_HLEN;
251 if (unlikely(!(dev->flags & IFF_UP))) {
252 kfree_skb(skb);
253 goto out;
254 }
255
256 skb = skb_share_check(skb, GFP_ATOMIC);
257 if (!skb)
258 goto out;
259
260 skb->dev = dev;
261 skb->pkt_type = PACKET_HOST;
262
263 if (local) {
264 if (dev_forward_skb(ipvlan->dev, skb) == NET_RX_SUCCESS)
265 success = true;
266 } else {
267 ret = RX_HANDLER_ANOTHER;
268 success = true;
269 }
270
271out:
272 ipvlan_count_rx(ipvlan, len, success, false);
273 return ret;
274}
275
276static struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port,
277 void *lyr3h, int addr_type,
278 bool use_dest)
279{
280 struct ipvl_addr *addr = NULL;
281
282 if (addr_type == IPVL_IPV6) {
283 struct ipv6hdr *ip6h;
284 struct in6_addr *i6addr;
285
286 ip6h = (struct ipv6hdr *)lyr3h;
287 i6addr = use_dest ? &ip6h->daddr : &ip6h->saddr;
288 addr = ipvlan_ht_addr_lookup(port, i6addr, true);
289 } else if (addr_type == IPVL_ICMPV6) {
290 struct nd_msg *ndmh;
291 struct in6_addr *i6addr;
292
293 /* Make sure that the NeighborSolicitation ICMPv6 packets
294 * are handled to avoid DAD issue.
295 */
296 ndmh = (struct nd_msg *)lyr3h;
297 if (ndmh->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
298 i6addr = &ndmh->target;
299 addr = ipvlan_ht_addr_lookup(port, i6addr, true);
300 }
301 } else if (addr_type == IPVL_IPV4) {
302 struct iphdr *ip4h;
303 __be32 *i4addr;
304
305 ip4h = (struct iphdr *)lyr3h;
306 i4addr = use_dest ? &ip4h->daddr : &ip4h->saddr;
307 addr = ipvlan_ht_addr_lookup(port, i4addr, false);
308 } else if (addr_type == IPVL_ARP) {
309 struct arphdr *arph;
310 unsigned char *arp_ptr;
311 __be32 dip;
312
313 arph = (struct arphdr *)lyr3h;
314 arp_ptr = (unsigned char *)(arph + 1);
315 if (use_dest)
316 arp_ptr += (2 * port->dev->addr_len) + 4;
317 else
318 arp_ptr += port->dev->addr_len;
319
320 memcpy(&dip, arp_ptr, 4);
321 addr = ipvlan_ht_addr_lookup(port, &dip, false);
322 }
323
324 return addr;
325}
326
327static int ipvlan_process_v4_outbound(struct sk_buff *skb)
328{
329 const struct iphdr *ip4h = ip_hdr(skb);
330 struct net_device *dev = skb->dev;
331 struct rtable *rt;
332 int err, ret = NET_XMIT_DROP;
333 struct flowi4 fl4 = {
334 .flowi4_oif = dev->iflink,
335 .flowi4_tos = RT_TOS(ip4h->tos),
336 .flowi4_flags = FLOWI_FLAG_ANYSRC,
337 .daddr = ip4h->daddr,
338 .saddr = ip4h->saddr,
339 };
340
341 rt = ip_route_output_flow(dev_net(dev), &fl4, NULL);
342 if (IS_ERR(rt))
343 goto err;
344
345 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
346 ip_rt_put(rt);
347 goto err;
348 }
349 skb_dst_drop(skb);
350 skb_dst_set(skb, &rt->dst);
351 err = ip_local_out(skb);
352 if (unlikely(net_xmit_eval(err)))
353 dev->stats.tx_errors++;
354 else
355 ret = NET_XMIT_SUCCESS;
356 goto out;
357err:
358 dev->stats.tx_errors++;
359 kfree_skb(skb);
360out:
361 return ret;
362}
363
364static int ipvlan_process_v6_outbound(struct sk_buff *skb)
365{
366 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
367 struct net_device *dev = skb->dev;
368 struct dst_entry *dst;
369 int err, ret = NET_XMIT_DROP;
370 struct flowi6 fl6 = {
371 .flowi6_iif = skb->dev->ifindex,
372 .daddr = ip6h->daddr,
373 .saddr = ip6h->saddr,
374 .flowi6_flags = FLOWI_FLAG_ANYSRC,
375 .flowlabel = ip6_flowinfo(ip6h),
376 .flowi6_mark = skb->mark,
377 .flowi6_proto = ip6h->nexthdr,
378 };
379
380 dst = ip6_route_output(dev_net(dev), NULL, &fl6);
2aab9525
MB
381 if (dst->error) {
382 ret = dst->error;
383 dst_release(dst);
2ad7bf36 384 goto err;
2aab9525 385 }
2ad7bf36
MB
386 skb_dst_drop(skb);
387 skb_dst_set(skb, dst);
388 err = ip6_local_out(skb);
389 if (unlikely(net_xmit_eval(err)))
390 dev->stats.tx_errors++;
391 else
392 ret = NET_XMIT_SUCCESS;
393 goto out;
394err:
395 dev->stats.tx_errors++;
396 kfree_skb(skb);
397out:
398 return ret;
399}
400
401static int ipvlan_process_outbound(struct sk_buff *skb,
402 const struct ipvl_dev *ipvlan)
403{
404 struct ethhdr *ethh = eth_hdr(skb);
405 int ret = NET_XMIT_DROP;
406
407 /* In this mode we dont care about multicast and broadcast traffic */
408 if (is_multicast_ether_addr(ethh->h_dest)) {
409 pr_warn_ratelimited("Dropped {multi|broad}cast of type= [%x]\n",
410 ntohs(skb->protocol));
411 kfree_skb(skb);
412 goto out;
413 }
414
415 /* The ipvlan is a pseudo-L2 device, so the packets that we receive
416 * will have L2; which need to discarded and processed further
417 * in the net-ns of the main-device.
418 */
419 if (skb_mac_header_was_set(skb)) {
420 skb_pull(skb, sizeof(*ethh));
421 skb->mac_header = (typeof(skb->mac_header))~0U;
422 skb_reset_network_header(skb);
423 }
424
425 if (skb->protocol == htons(ETH_P_IPV6))
426 ret = ipvlan_process_v6_outbound(skb);
427 else if (skb->protocol == htons(ETH_P_IP))
428 ret = ipvlan_process_v4_outbound(skb);
429 else {
430 pr_warn_ratelimited("Dropped outbound packet type=%x\n",
431 ntohs(skb->protocol));
432 kfree_skb(skb);
433 }
434out:
435 return ret;
436}
437
438static int ipvlan_xmit_mode_l3(struct sk_buff *skb, struct net_device *dev)
439{
440 const struct ipvl_dev *ipvlan = netdev_priv(dev);
441 void *lyr3h;
442 struct ipvl_addr *addr;
443 int addr_type;
444
445 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
446 if (!lyr3h)
447 goto out;
448
449 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
450 if (addr)
451 return ipvlan_rcv_frame(addr, skb, true);
452
453out:
454 skb->dev = ipvlan->phy_dev;
455 return ipvlan_process_outbound(skb, ipvlan);
456}
457
458static int ipvlan_xmit_mode_l2(struct sk_buff *skb, struct net_device *dev)
459{
460 const struct ipvl_dev *ipvlan = netdev_priv(dev);
461 struct ethhdr *eth = eth_hdr(skb);
462 struct ipvl_addr *addr;
463 void *lyr3h;
464 int addr_type;
465
466 if (ether_addr_equal(eth->h_dest, eth->h_source)) {
467 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
468 if (lyr3h) {
469 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
470 if (addr)
471 return ipvlan_rcv_frame(addr, skb, true);
472 }
473 skb = skb_share_check(skb, GFP_ATOMIC);
474 if (!skb)
475 return NET_XMIT_DROP;
476
477 /* Packet definitely does not belong to any of the
478 * virtual devices, but the dest is local. So forward
479 * the skb for the main-dev. At the RX side we just return
480 * RX_PASS for it to be processed further on the stack.
481 */
482 return dev_forward_skb(ipvlan->phy_dev, skb);
483
484 } else if (is_multicast_ether_addr(eth->h_dest)) {
485 u8 ip_summed = skb->ip_summed;
486
487 skb->ip_summed = CHECKSUM_UNNECESSARY;
488 ipvlan_multicast_frame(ipvlan->port, skb, ipvlan, true);
489 skb->ip_summed = ip_summed;
490 }
491
492 skb->dev = ipvlan->phy_dev;
493 return dev_queue_xmit(skb);
494}
495
496int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
497{
498 struct ipvl_dev *ipvlan = netdev_priv(dev);
499 struct ipvl_port *port = ipvlan_port_get_rcu(ipvlan->phy_dev);
500
501 if (!port)
502 goto out;
503
504 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
505 goto out;
506
507 switch(port->mode) {
508 case IPVLAN_MODE_L2:
509 return ipvlan_xmit_mode_l2(skb, dev);
510 case IPVLAN_MODE_L3:
511 return ipvlan_xmit_mode_l3(skb, dev);
512 }
513
514 /* Should not reach here */
515 WARN_ONCE(true, "ipvlan_queue_xmit() called for mode = [%hx]\n",
516 port->mode);
517out:
518 kfree_skb(skb);
519 return NET_XMIT_DROP;
520}
521
522static bool ipvlan_external_frame(struct sk_buff *skb, struct ipvl_port *port)
523{
524 struct ethhdr *eth = eth_hdr(skb);
525 struct ipvl_addr *addr;
526 void *lyr3h;
527 int addr_type;
528
529 if (ether_addr_equal(eth->h_source, skb->dev->dev_addr)) {
530 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
531 if (!lyr3h)
532 return true;
533
534 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, false);
535 if (addr)
536 return false;
537 }
538
539 return true;
540}
541
542static rx_handler_result_t ipvlan_handle_mode_l3(struct sk_buff **pskb,
543 struct ipvl_port *port)
544{
545 void *lyr3h;
546 int addr_type;
547 struct ipvl_addr *addr;
548 struct sk_buff *skb = *pskb;
549 rx_handler_result_t ret = RX_HANDLER_PASS;
550
551 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
552 if (!lyr3h)
553 goto out;
554
555 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
556 if (addr)
557 ret = ipvlan_rcv_frame(addr, skb, false);
558
559out:
560 return ret;
561}
562
563static rx_handler_result_t ipvlan_handle_mode_l2(struct sk_buff **pskb,
564 struct ipvl_port *port)
565{
566 struct sk_buff *skb = *pskb;
567 struct ethhdr *eth = eth_hdr(skb);
568 rx_handler_result_t ret = RX_HANDLER_PASS;
569 void *lyr3h;
570 int addr_type;
571
572 if (is_multicast_ether_addr(eth->h_dest)) {
573 if (ipvlan_external_frame(skb, port))
574 ipvlan_multicast_frame(port, skb, NULL, false);
575 } else {
576 struct ipvl_addr *addr;
577
578 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
579 if (!lyr3h)
580 return ret;
581
582 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
583 if (addr)
584 ret = ipvlan_rcv_frame(addr, skb, false);
585 }
586
587 return ret;
588}
589
590rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
591{
592 struct sk_buff *skb = *pskb;
593 struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);
594
595 if (!port)
596 return RX_HANDLER_PASS;
597
598 switch (port->mode) {
599 case IPVLAN_MODE_L2:
600 return ipvlan_handle_mode_l2(pskb, port);
601 case IPVLAN_MODE_L3:
602 return ipvlan_handle_mode_l3(pskb, port);
603 }
604
605 /* Should not reach here */
606 WARN_ONCE(true, "ipvlan_handle_frame() called for mode = [%hx]\n",
607 port->mode);
608 kfree_skb(skb);
609 return NET_RX_DROP;
610}
This page took 0.06829 seconds and 5 git commands to generate.