ipvlan_core.c 14 KB

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