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 static void ipvlan_count_rx(const struct ipvl_dev *ipvlan,
20 unsigned int len, bool success, bool mcast)
25 if (likely(success)) {
26 struct ipvl_pcpu_stats *pcptr;
28 pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
29 u64_stats_update_begin(&pcptr->syncp);
31 pcptr->rx_bytes += len;
34 u64_stats_update_end(&pcptr->syncp);
36 this_cpu_inc(ipvlan->pcpu_stats->rx_errs);
40 static u8 ipvlan_get_v6_hash(const void *iaddr)
42 const struct in6_addr *ip6_addr = iaddr;
44 return __ipv6_addr_jhash(ip6_addr, ipvlan_jhash_secret) &
48 static u8 ipvlan_get_v4_hash(const void *iaddr)
50 const struct in_addr *ip4_addr = iaddr;
52 return jhash_1word(ip4_addr->s_addr, ipvlan_jhash_secret) &
56 struct ipvl_addr *ipvlan_ht_addr_lookup(const struct ipvl_port *port,
57 const void *iaddr, bool is_v6)
59 struct ipvl_addr *addr;
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))
68 else if (!is_v6 && addr->atype == IPVL_IPV4 &&
69 addr->ip4addr.s_addr ==
70 ((struct in_addr *)iaddr)->s_addr)
76 void ipvlan_ht_addr_add(struct ipvl_dev *ipvlan, struct ipvl_addr *addr)
78 struct ipvl_port *port = ipvlan->port;
81 hash = (addr->atype == IPVL_IPV6) ?
82 ipvlan_get_v6_hash(&addr->ip6addr) :
83 ipvlan_get_v4_hash(&addr->ip4addr);
84 if (hlist_unhashed(&addr->hlnode))
85 hlist_add_head_rcu(&addr->hlnode, &port->hlhead[hash]);
88 void ipvlan_ht_addr_del(struct ipvl_addr *addr, bool sync)
90 hlist_del_init_rcu(&addr->hlnode);
95 bool ipvlan_addr_busy(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
97 struct ipvl_port *port = ipvlan->port;
98 struct ipvl_addr *addr;
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))
108 if (ipvlan_ht_addr_lookup(port, iaddr, is_v6))
114 static void *ipvlan_get_L3_hdr(struct sk_buff *skb, int *type)
118 switch (skb->protocol) {
119 case htons(ETH_P_ARP): {
122 if (unlikely(!pskb_may_pull(skb, sizeof(*arph))))
130 case htons(ETH_P_IP): {
134 if (unlikely(!pskb_may_pull(skb, sizeof(*ip4h))))
138 pktlen = ntohs(ip4h->tot_len);
139 if (ip4h->ihl < 5 || ip4h->version != 4)
141 if (skb->len < pktlen || pktlen < (ip4h->ihl * 4))
148 case htons(ETH_P_IPV6): {
149 struct ipv6hdr *ip6h;
151 if (unlikely(!pskb_may_pull(skb, sizeof(*ip6h))))
154 ip6h = ipv6_hdr(skb);
155 if (ip6h->version != 6)
160 /* Only Neighbour Solicitation pkts need different treatment */
161 if (ipv6_addr_any(&ip6h->saddr) &&
162 ip6h->nexthdr == NEXTHDR_ICMP) {
175 unsigned int ipvlan_mac_hash(const unsigned char *addr)
177 u32 hash = jhash_1word(__get_unaligned_cpu32(addr+2),
178 ipvlan_jhash_secret);
180 return hash & IPVLAN_MAC_FILTER_MASK;
183 static void ipvlan_multicast_frame(struct ipvl_port *port, struct sk_buff *skb,
184 const struct ipvl_dev *in_dev, bool local)
186 struct ethhdr *eth = eth_hdr(skb);
187 struct ipvl_dev *ipvlan;
188 struct sk_buff *nskb;
190 unsigned int mac_hash;
193 if (skb->protocol == htons(ETH_P_PAUSE))
197 list_for_each_entry_rcu(ipvlan, &port->ipvlans, pnode) {
198 if (local && (ipvlan == in_dev))
201 mac_hash = ipvlan_mac_hash(eth->h_dest);
202 if (!test_bit(mac_hash, ipvlan->mac_filters))
206 len = skb->len + ETH_HLEN;
207 nskb = skb_clone(skb, GFP_ATOMIC);
211 if (ether_addr_equal(eth->h_dest, ipvlan->phy_dev->broadcast))
212 nskb->pkt_type = PACKET_BROADCAST;
214 nskb->pkt_type = PACKET_MULTICAST;
216 nskb->dev = ipvlan->dev;
218 ret = dev_forward_skb(ipvlan->dev, nskb);
220 ret = netif_rx(nskb);
222 ipvlan_count_rx(ipvlan, len, ret == NET_RX_SUCCESS, true);
226 /* Locally generated? ...Forward a copy to the main-device as
227 * well. On the RX side we'll ignore it (wont give it to any
228 * of the virtual devices.
231 nskb = skb_clone(skb, GFP_ATOMIC);
233 if (ether_addr_equal(eth->h_dest, port->dev->broadcast))
234 nskb->pkt_type = PACKET_BROADCAST;
236 nskb->pkt_type = PACKET_MULTICAST;
238 dev_forward_skb(port->dev, nskb);
243 static int ipvlan_rcv_frame(struct ipvl_addr *addr, struct sk_buff *skb,
246 struct ipvl_dev *ipvlan = addr->master;
247 struct net_device *dev = ipvlan->dev;
249 rx_handler_result_t ret = RX_HANDLER_CONSUMED;
250 bool success = false;
252 len = skb->len + ETH_HLEN;
253 if (unlikely(!(dev->flags & IFF_UP))) {
258 skb = skb_share_check(skb, GFP_ATOMIC);
263 skb->pkt_type = PACKET_HOST;
266 if (dev_forward_skb(ipvlan->dev, skb) == NET_RX_SUCCESS)
269 ret = RX_HANDLER_ANOTHER;
274 ipvlan_count_rx(ipvlan, len, success, false);
278 static struct ipvl_addr *ipvlan_addr_lookup(struct ipvl_port *port,
279 void *lyr3h, int addr_type,
282 struct ipvl_addr *addr = NULL;
284 if (addr_type == IPVL_IPV6) {
285 struct ipv6hdr *ip6h;
286 struct in6_addr *i6addr;
288 ip6h = (struct ipv6hdr *)lyr3h;
289 i6addr = use_dest ? &ip6h->daddr : &ip6h->saddr;
290 addr = ipvlan_ht_addr_lookup(port, i6addr, true);
291 } else if (addr_type == IPVL_ICMPV6) {
293 struct in6_addr *i6addr;
295 /* Make sure that the NeighborSolicitation ICMPv6 packets
296 * are handled to avoid DAD issue.
298 ndmh = (struct nd_msg *)lyr3h;
299 if (ndmh->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
300 i6addr = &ndmh->target;
301 addr = ipvlan_ht_addr_lookup(port, i6addr, true);
303 } else if (addr_type == IPVL_IPV4) {
307 ip4h = (struct iphdr *)lyr3h;
308 i4addr = use_dest ? &ip4h->daddr : &ip4h->saddr;
309 addr = ipvlan_ht_addr_lookup(port, i4addr, false);
310 } else if (addr_type == IPVL_ARP) {
312 unsigned char *arp_ptr;
315 arph = (struct arphdr *)lyr3h;
316 arp_ptr = (unsigned char *)(arph + 1);
318 arp_ptr += (2 * port->dev->addr_len) + 4;
320 arp_ptr += port->dev->addr_len;
322 memcpy(&dip, arp_ptr, 4);
323 addr = ipvlan_ht_addr_lookup(port, &dip, false);
329 static int ipvlan_process_v4_outbound(struct sk_buff *skb)
331 const struct iphdr *ip4h = ip_hdr(skb);
332 struct net_device *dev = skb->dev;
334 int err, ret = NET_XMIT_DROP;
335 struct flowi4 fl4 = {
336 .flowi4_oif = dev->iflink,
337 .flowi4_tos = RT_TOS(ip4h->tos),
338 .flowi4_flags = FLOWI_FLAG_ANYSRC,
339 .daddr = ip4h->daddr,
340 .saddr = ip4h->saddr,
343 rt = ip_route_output_flow(dev_net(dev), &fl4, NULL);
347 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
352 skb_dst_set(skb, &rt->dst);
353 err = ip_local_out(skb);
354 if (unlikely(net_xmit_eval(err)))
355 dev->stats.tx_errors++;
357 ret = NET_XMIT_SUCCESS;
360 dev->stats.tx_errors++;
366 static int ipvlan_process_v6_outbound(struct sk_buff *skb)
368 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
369 struct net_device *dev = skb->dev;
370 struct dst_entry *dst;
371 int err, ret = NET_XMIT_DROP;
372 struct flowi6 fl6 = {
373 .flowi6_iif = skb->dev->ifindex,
374 .daddr = ip6h->daddr,
375 .saddr = ip6h->saddr,
376 .flowi6_flags = FLOWI_FLAG_ANYSRC,
377 .flowlabel = ip6_flowinfo(ip6h),
378 .flowi6_mark = skb->mark,
379 .flowi6_proto = ip6h->nexthdr,
382 dst = ip6_route_output(dev_net(dev), NULL, &fl6);
389 skb_dst_set(skb, dst);
390 err = ip6_local_out(skb);
391 if (unlikely(net_xmit_eval(err)))
392 dev->stats.tx_errors++;
394 ret = NET_XMIT_SUCCESS;
397 dev->stats.tx_errors++;
403 static int ipvlan_process_outbound(struct sk_buff *skb,
404 const struct ipvl_dev *ipvlan)
406 struct ethhdr *ethh = eth_hdr(skb);
407 int ret = NET_XMIT_DROP;
409 /* In this mode we dont care about multicast and broadcast traffic */
410 if (is_multicast_ether_addr(ethh->h_dest)) {
411 pr_warn_ratelimited("Dropped {multi|broad}cast of type= [%x]\n",
412 ntohs(skb->protocol));
417 /* The ipvlan is a pseudo-L2 device, so the packets that we receive
418 * will have L2; which need to discarded and processed further
419 * in the net-ns of the main-device.
421 if (skb_mac_header_was_set(skb)) {
422 skb_pull(skb, sizeof(*ethh));
423 skb->mac_header = (typeof(skb->mac_header))~0U;
424 skb_reset_network_header(skb);
427 if (skb->protocol == htons(ETH_P_IPV6))
428 ret = ipvlan_process_v6_outbound(skb);
429 else if (skb->protocol == htons(ETH_P_IP))
430 ret = ipvlan_process_v4_outbound(skb);
432 pr_warn_ratelimited("Dropped outbound packet type=%x\n",
433 ntohs(skb->protocol));
440 static int ipvlan_xmit_mode_l3(struct sk_buff *skb, struct net_device *dev)
442 const struct ipvl_dev *ipvlan = netdev_priv(dev);
444 struct ipvl_addr *addr;
447 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
451 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
453 return ipvlan_rcv_frame(addr, skb, true);
456 skb->dev = ipvlan->phy_dev;
457 return ipvlan_process_outbound(skb, ipvlan);
460 static int ipvlan_xmit_mode_l2(struct sk_buff *skb, struct net_device *dev)
462 const struct ipvl_dev *ipvlan = netdev_priv(dev);
463 struct ethhdr *eth = eth_hdr(skb);
464 struct ipvl_addr *addr;
468 if (ether_addr_equal(eth->h_dest, eth->h_source)) {
469 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
471 addr = ipvlan_addr_lookup(ipvlan->port, lyr3h, addr_type, true);
473 return ipvlan_rcv_frame(addr, skb, true);
475 skb = skb_share_check(skb, GFP_ATOMIC);
477 return NET_XMIT_DROP;
479 /* Packet definitely does not belong to any of the
480 * virtual devices, but the dest is local. So forward
481 * the skb for the main-dev. At the RX side we just return
482 * RX_PASS for it to be processed further on the stack.
484 return dev_forward_skb(ipvlan->phy_dev, skb);
486 } else if (is_multicast_ether_addr(eth->h_dest)) {
487 u8 ip_summed = skb->ip_summed;
489 skb->ip_summed = CHECKSUM_UNNECESSARY;
490 ipvlan_multicast_frame(ipvlan->port, skb, ipvlan, true);
491 skb->ip_summed = ip_summed;
494 skb->dev = ipvlan->phy_dev;
495 return dev_queue_xmit(skb);
498 int ipvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
500 struct ipvl_dev *ipvlan = netdev_priv(dev);
501 struct ipvl_port *port = ipvlan_port_get_rcu(ipvlan->phy_dev);
506 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
511 return ipvlan_xmit_mode_l2(skb, dev);
513 return ipvlan_xmit_mode_l3(skb, dev);
516 /* Should not reach here */
517 WARN_ONCE(true, "ipvlan_queue_xmit() called for mode = [%hx]\n",
521 return NET_XMIT_DROP;
524 static bool ipvlan_external_frame(struct sk_buff *skb, struct ipvl_port *port)
526 struct ethhdr *eth = eth_hdr(skb);
527 struct ipvl_addr *addr;
531 if (ether_addr_equal(eth->h_source, skb->dev->dev_addr)) {
532 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
536 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, false);
544 static rx_handler_result_t ipvlan_handle_mode_l3(struct sk_buff **pskb,
545 struct ipvl_port *port)
549 struct ipvl_addr *addr;
550 struct sk_buff *skb = *pskb;
551 rx_handler_result_t ret = RX_HANDLER_PASS;
553 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
557 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
559 ret = ipvlan_rcv_frame(addr, skb, false);
565 static rx_handler_result_t ipvlan_handle_mode_l2(struct sk_buff **pskb,
566 struct ipvl_port *port)
568 struct sk_buff *skb = *pskb;
569 struct ethhdr *eth = eth_hdr(skb);
570 rx_handler_result_t ret = RX_HANDLER_PASS;
574 if (is_multicast_ether_addr(eth->h_dest)) {
575 if (ipvlan_external_frame(skb, port))
576 ipvlan_multicast_frame(port, skb, NULL, false);
578 struct ipvl_addr *addr;
580 lyr3h = ipvlan_get_L3_hdr(skb, &addr_type);
584 addr = ipvlan_addr_lookup(port, lyr3h, addr_type, true);
586 ret = ipvlan_rcv_frame(addr, skb, false);
592 rx_handler_result_t ipvlan_handle_frame(struct sk_buff **pskb)
594 struct sk_buff *skb = *pskb;
595 struct ipvl_port *port = ipvlan_port_get_rcu(skb->dev);
598 return RX_HANDLER_PASS;
600 switch (port->mode) {
602 return ipvlan_handle_mode_l2(pskb, port);
604 return ipvlan_handle_mode_l3(pskb, port);
607 /* Should not reach here */
608 WARN_ONCE(true, "ipvlan_handle_frame() called for mode = [%hx]\n",