ipvlan_core.c 18 KB

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