vrf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * vrf.c: device driver to encapsulate a VRF space
  3. *
  4. * Copyright (c) 2015 Cumulus Networks. All rights reserved.
  5. * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
  6. * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
  7. *
  8. * Based on dummy, team and ipvlan drivers
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/ip.h>
  20. #include <linux/init.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/netfilter.h>
  23. #include <linux/rtnetlink.h>
  24. #include <net/rtnetlink.h>
  25. #include <linux/u64_stats_sync.h>
  26. #include <linux/hashtable.h>
  27. #include <linux/inetdevice.h>
  28. #include <net/arp.h>
  29. #include <net/ip.h>
  30. #include <net/ip_fib.h>
  31. #include <net/ip6_fib.h>
  32. #include <net/ip6_route.h>
  33. #include <net/route.h>
  34. #include <net/addrconf.h>
  35. #include <net/l3mdev.h>
  36. #define RT_FL_TOS(oldflp4) \
  37. ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
  38. #define DRV_NAME "vrf"
  39. #define DRV_VERSION "1.0"
  40. struct net_vrf {
  41. struct rtable __rcu *rth;
  42. struct rt6_info __rcu *rt6;
  43. u32 tb_id;
  44. };
  45. struct pcpu_dstats {
  46. u64 tx_pkts;
  47. u64 tx_bytes;
  48. u64 tx_drps;
  49. u64 rx_pkts;
  50. u64 rx_bytes;
  51. struct u64_stats_sync syncp;
  52. };
  53. static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
  54. {
  55. vrf_dev->stats.tx_errors++;
  56. kfree_skb(skb);
  57. }
  58. static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
  59. struct rtnl_link_stats64 *stats)
  60. {
  61. int i;
  62. for_each_possible_cpu(i) {
  63. const struct pcpu_dstats *dstats;
  64. u64 tbytes, tpkts, tdrops, rbytes, rpkts;
  65. unsigned int start;
  66. dstats = per_cpu_ptr(dev->dstats, i);
  67. do {
  68. start = u64_stats_fetch_begin_irq(&dstats->syncp);
  69. tbytes = dstats->tx_bytes;
  70. tpkts = dstats->tx_pkts;
  71. tdrops = dstats->tx_drps;
  72. rbytes = dstats->rx_bytes;
  73. rpkts = dstats->rx_pkts;
  74. } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
  75. stats->tx_bytes += tbytes;
  76. stats->tx_packets += tpkts;
  77. stats->tx_dropped += tdrops;
  78. stats->rx_bytes += rbytes;
  79. stats->rx_packets += rpkts;
  80. }
  81. return stats;
  82. }
  83. #if IS_ENABLED(CONFIG_IPV6)
  84. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  85. struct net_device *dev)
  86. {
  87. const struct ipv6hdr *iph = ipv6_hdr(skb);
  88. struct net *net = dev_net(skb->dev);
  89. struct flowi6 fl6 = {
  90. /* needed to match OIF rule */
  91. .flowi6_oif = dev->ifindex,
  92. .flowi6_iif = LOOPBACK_IFINDEX,
  93. .daddr = iph->daddr,
  94. .saddr = iph->saddr,
  95. .flowlabel = ip6_flowinfo(iph),
  96. .flowi6_mark = skb->mark,
  97. .flowi6_proto = iph->nexthdr,
  98. .flowi6_flags = FLOWI_FLAG_L3MDEV_SRC | FLOWI_FLAG_SKIP_NH_OIF,
  99. };
  100. int ret = NET_XMIT_DROP;
  101. struct dst_entry *dst;
  102. struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
  103. dst = ip6_route_output(net, NULL, &fl6);
  104. if (dst == dst_null)
  105. goto err;
  106. skb_dst_drop(skb);
  107. skb_dst_set(skb, dst);
  108. ret = ip6_local_out(net, skb->sk, skb);
  109. if (unlikely(net_xmit_eval(ret)))
  110. dev->stats.tx_errors++;
  111. else
  112. ret = NET_XMIT_SUCCESS;
  113. return ret;
  114. err:
  115. vrf_tx_error(dev, skb);
  116. return NET_XMIT_DROP;
  117. }
  118. #else
  119. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  120. struct net_device *dev)
  121. {
  122. vrf_tx_error(dev, skb);
  123. return NET_XMIT_DROP;
  124. }
  125. #endif
  126. static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
  127. struct net_device *vrf_dev)
  128. {
  129. struct rtable *rt;
  130. int err = 1;
  131. rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
  132. if (IS_ERR(rt))
  133. goto out;
  134. /* TO-DO: what about broadcast ? */
  135. if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
  136. ip_rt_put(rt);
  137. goto out;
  138. }
  139. skb_dst_drop(skb);
  140. skb_dst_set(skb, &rt->dst);
  141. err = 0;
  142. out:
  143. return err;
  144. }
  145. static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
  146. struct net_device *vrf_dev)
  147. {
  148. struct iphdr *ip4h = ip_hdr(skb);
  149. int ret = NET_XMIT_DROP;
  150. struct flowi4 fl4 = {
  151. /* needed to match OIF rule */
  152. .flowi4_oif = vrf_dev->ifindex,
  153. .flowi4_iif = LOOPBACK_IFINDEX,
  154. .flowi4_tos = RT_TOS(ip4h->tos),
  155. .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC |
  156. FLOWI_FLAG_SKIP_NH_OIF,
  157. .daddr = ip4h->daddr,
  158. };
  159. if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
  160. goto err;
  161. if (!ip4h->saddr) {
  162. ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
  163. RT_SCOPE_LINK);
  164. }
  165. ret = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  166. if (unlikely(net_xmit_eval(ret)))
  167. vrf_dev->stats.tx_errors++;
  168. else
  169. ret = NET_XMIT_SUCCESS;
  170. out:
  171. return ret;
  172. err:
  173. vrf_tx_error(vrf_dev, skb);
  174. goto out;
  175. }
  176. static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
  177. {
  178. /* strip the ethernet header added for pass through VRF device */
  179. __skb_pull(skb, skb_network_offset(skb));
  180. switch (skb->protocol) {
  181. case htons(ETH_P_IP):
  182. return vrf_process_v4_outbound(skb, dev);
  183. case htons(ETH_P_IPV6):
  184. return vrf_process_v6_outbound(skb, dev);
  185. default:
  186. vrf_tx_error(dev, skb);
  187. return NET_XMIT_DROP;
  188. }
  189. }
  190. static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
  191. {
  192. netdev_tx_t ret = is_ip_tx_frame(skb, dev);
  193. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  194. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  195. u64_stats_update_begin(&dstats->syncp);
  196. dstats->tx_pkts++;
  197. dstats->tx_bytes += skb->len;
  198. u64_stats_update_end(&dstats->syncp);
  199. } else {
  200. this_cpu_inc(dev->dstats->tx_drps);
  201. }
  202. return ret;
  203. }
  204. #if IS_ENABLED(CONFIG_IPV6)
  205. /* modelled after ip6_finish_output2 */
  206. static int vrf_finish_output6(struct net *net, struct sock *sk,
  207. struct sk_buff *skb)
  208. {
  209. struct dst_entry *dst = skb_dst(skb);
  210. struct net_device *dev = dst->dev;
  211. struct neighbour *neigh;
  212. struct in6_addr *nexthop;
  213. int ret;
  214. skb->protocol = htons(ETH_P_IPV6);
  215. skb->dev = dev;
  216. rcu_read_lock_bh();
  217. nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
  218. neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
  219. if (unlikely(!neigh))
  220. neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
  221. if (!IS_ERR(neigh)) {
  222. ret = dst_neigh_output(dst, neigh, skb);
  223. rcu_read_unlock_bh();
  224. return ret;
  225. }
  226. rcu_read_unlock_bh();
  227. IP6_INC_STATS(dev_net(dst->dev),
  228. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  229. kfree_skb(skb);
  230. return -EINVAL;
  231. }
  232. /* modelled after ip6_output */
  233. static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
  234. {
  235. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  236. net, sk, skb, NULL, skb_dst(skb)->dev,
  237. vrf_finish_output6,
  238. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  239. }
  240. /* holding rtnl */
  241. static void vrf_rt6_release(struct net_vrf *vrf)
  242. {
  243. struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
  244. rcu_assign_pointer(vrf->rt6, NULL);
  245. if (rt6)
  246. dst_release(&rt6->dst);
  247. }
  248. static int vrf_rt6_create(struct net_device *dev)
  249. {
  250. struct net_vrf *vrf = netdev_priv(dev);
  251. struct net *net = dev_net(dev);
  252. struct fib6_table *rt6i_table;
  253. struct rt6_info *rt6;
  254. int rc = -ENOMEM;
  255. rt6i_table = fib6_new_table(net, vrf->tb_id);
  256. if (!rt6i_table)
  257. goto out;
  258. rt6 = ip6_dst_alloc(net, dev,
  259. DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE);
  260. if (!rt6)
  261. goto out;
  262. dst_hold(&rt6->dst);
  263. rt6->rt6i_table = rt6i_table;
  264. rt6->dst.output = vrf_output6;
  265. rcu_assign_pointer(vrf->rt6, rt6);
  266. rc = 0;
  267. out:
  268. return rc;
  269. }
  270. #else
  271. static void vrf_rt6_release(struct net_vrf *vrf)
  272. {
  273. }
  274. static int vrf_rt6_create(struct net_device *dev)
  275. {
  276. return 0;
  277. }
  278. #endif
  279. /* modelled after ip_finish_output2 */
  280. static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  281. {
  282. struct dst_entry *dst = skb_dst(skb);
  283. struct rtable *rt = (struct rtable *)dst;
  284. struct net_device *dev = dst->dev;
  285. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  286. struct neighbour *neigh;
  287. u32 nexthop;
  288. int ret = -EINVAL;
  289. /* Be paranoid, rather than too clever. */
  290. if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
  291. struct sk_buff *skb2;
  292. skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
  293. if (!skb2) {
  294. ret = -ENOMEM;
  295. goto err;
  296. }
  297. if (skb->sk)
  298. skb_set_owner_w(skb2, skb->sk);
  299. consume_skb(skb);
  300. skb = skb2;
  301. }
  302. rcu_read_lock_bh();
  303. nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
  304. neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
  305. if (unlikely(!neigh))
  306. neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
  307. if (!IS_ERR(neigh))
  308. ret = dst_neigh_output(dst, neigh, skb);
  309. rcu_read_unlock_bh();
  310. err:
  311. if (unlikely(ret < 0))
  312. vrf_tx_error(skb->dev, skb);
  313. return ret;
  314. }
  315. static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  316. {
  317. struct net_device *dev = skb_dst(skb)->dev;
  318. IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
  319. skb->dev = dev;
  320. skb->protocol = htons(ETH_P_IP);
  321. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  322. net, sk, skb, NULL, dev,
  323. vrf_finish_output,
  324. !(IPCB(skb)->flags & IPSKB_REROUTED));
  325. }
  326. /* holding rtnl */
  327. static void vrf_rtable_release(struct net_vrf *vrf)
  328. {
  329. struct rtable *rth = rtnl_dereference(vrf->rth);
  330. rcu_assign_pointer(vrf->rth, NULL);
  331. if (rth)
  332. dst_release(&rth->dst);
  333. }
  334. static int vrf_rtable_create(struct net_device *dev)
  335. {
  336. struct net_vrf *vrf = netdev_priv(dev);
  337. struct rtable *rth;
  338. if (!fib_new_table(dev_net(dev), vrf->tb_id))
  339. return -ENOMEM;
  340. rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
  341. if (!rth)
  342. return -ENOMEM;
  343. rth->dst.output = vrf_output;
  344. rth->rt_table_id = vrf->tb_id;
  345. rcu_assign_pointer(vrf->rth, rth);
  346. return 0;
  347. }
  348. /**************************** device handling ********************/
  349. /* cycle interface to flush neighbor cache and move routes across tables */
  350. static void cycle_netdev(struct net_device *dev)
  351. {
  352. unsigned int flags = dev->flags;
  353. int ret;
  354. if (!netif_running(dev))
  355. return;
  356. ret = dev_change_flags(dev, flags & ~IFF_UP);
  357. if (ret >= 0)
  358. ret = dev_change_flags(dev, flags);
  359. if (ret < 0) {
  360. netdev_err(dev,
  361. "Failed to cycle device %s; route tables might be wrong!\n",
  362. dev->name);
  363. }
  364. }
  365. static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  366. {
  367. int ret;
  368. ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
  369. if (ret < 0)
  370. return ret;
  371. port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
  372. cycle_netdev(port_dev);
  373. return 0;
  374. }
  375. static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  376. {
  377. if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
  378. return -EINVAL;
  379. return do_vrf_add_slave(dev, port_dev);
  380. }
  381. /* inverse of do_vrf_add_slave */
  382. static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  383. {
  384. netdev_upper_dev_unlink(port_dev, dev);
  385. port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
  386. cycle_netdev(port_dev);
  387. return 0;
  388. }
  389. static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  390. {
  391. return do_vrf_del_slave(dev, port_dev);
  392. }
  393. static void vrf_dev_uninit(struct net_device *dev)
  394. {
  395. struct net_vrf *vrf = netdev_priv(dev);
  396. struct net_device *port_dev;
  397. struct list_head *iter;
  398. vrf_rtable_release(vrf);
  399. vrf_rt6_release(vrf);
  400. netdev_for_each_lower_dev(dev, port_dev, iter)
  401. vrf_del_slave(dev, port_dev);
  402. free_percpu(dev->dstats);
  403. dev->dstats = NULL;
  404. }
  405. static int vrf_dev_init(struct net_device *dev)
  406. {
  407. struct net_vrf *vrf = netdev_priv(dev);
  408. dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
  409. if (!dev->dstats)
  410. goto out_nomem;
  411. /* create the default dst which points back to us */
  412. if (vrf_rtable_create(dev) != 0)
  413. goto out_stats;
  414. if (vrf_rt6_create(dev) != 0)
  415. goto out_rth;
  416. dev->flags = IFF_MASTER | IFF_NOARP;
  417. return 0;
  418. out_rth:
  419. vrf_rtable_release(vrf);
  420. out_stats:
  421. free_percpu(dev->dstats);
  422. dev->dstats = NULL;
  423. out_nomem:
  424. return -ENOMEM;
  425. }
  426. static const struct net_device_ops vrf_netdev_ops = {
  427. .ndo_init = vrf_dev_init,
  428. .ndo_uninit = vrf_dev_uninit,
  429. .ndo_start_xmit = vrf_xmit,
  430. .ndo_get_stats64 = vrf_get_stats64,
  431. .ndo_add_slave = vrf_add_slave,
  432. .ndo_del_slave = vrf_del_slave,
  433. };
  434. static u32 vrf_fib_table(const struct net_device *dev)
  435. {
  436. struct net_vrf *vrf = netdev_priv(dev);
  437. return vrf->tb_id;
  438. }
  439. static struct rtable *vrf_get_rtable(const struct net_device *dev,
  440. const struct flowi4 *fl4)
  441. {
  442. struct rtable *rth = NULL;
  443. if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) {
  444. struct net_vrf *vrf = netdev_priv(dev);
  445. rcu_read_lock();
  446. rth = rcu_dereference(vrf->rth);
  447. if (likely(rth))
  448. dst_hold(&rth->dst);
  449. rcu_read_unlock();
  450. }
  451. return rth;
  452. }
  453. /* called under rcu_read_lock */
  454. static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4)
  455. {
  456. struct fib_result res = { .tclassid = 0 };
  457. struct net *net = dev_net(dev);
  458. u32 orig_tos = fl4->flowi4_tos;
  459. u8 flags = fl4->flowi4_flags;
  460. u8 scope = fl4->flowi4_scope;
  461. u8 tos = RT_FL_TOS(fl4);
  462. int rc;
  463. if (unlikely(!fl4->daddr))
  464. return 0;
  465. fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF;
  466. fl4->flowi4_iif = LOOPBACK_IFINDEX;
  467. /* make sure oif is set to VRF device for lookup */
  468. fl4->flowi4_oif = dev->ifindex;
  469. fl4->flowi4_tos = tos & IPTOS_RT_MASK;
  470. fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
  471. RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
  472. rc = fib_lookup(net, fl4, &res, 0);
  473. if (!rc) {
  474. if (res.type == RTN_LOCAL)
  475. fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr;
  476. else
  477. fib_select_path(net, &res, fl4, -1);
  478. }
  479. fl4->flowi4_flags = flags;
  480. fl4->flowi4_tos = orig_tos;
  481. fl4->flowi4_scope = scope;
  482. return rc;
  483. }
  484. #if IS_ENABLED(CONFIG_IPV6)
  485. /* neighbor handling is done with actual device; do not want
  486. * to flip skb->dev for those ndisc packets. This really fails
  487. * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
  488. * a start.
  489. */
  490. static bool ipv6_ndisc_frame(const struct sk_buff *skb)
  491. {
  492. const struct ipv6hdr *iph = ipv6_hdr(skb);
  493. bool rc = false;
  494. if (iph->nexthdr == NEXTHDR_ICMP) {
  495. const struct icmp6hdr *icmph;
  496. struct icmp6hdr _icmph;
  497. icmph = skb_header_pointer(skb, sizeof(*iph),
  498. sizeof(_icmph), &_icmph);
  499. if (!icmph)
  500. goto out;
  501. switch (icmph->icmp6_type) {
  502. case NDISC_ROUTER_SOLICITATION:
  503. case NDISC_ROUTER_ADVERTISEMENT:
  504. case NDISC_NEIGHBOUR_SOLICITATION:
  505. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  506. case NDISC_REDIRECT:
  507. rc = true;
  508. break;
  509. }
  510. }
  511. out:
  512. return rc;
  513. }
  514. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  515. struct sk_buff *skb)
  516. {
  517. /* if packet is NDISC keep the ingress interface */
  518. if (!ipv6_ndisc_frame(skb)) {
  519. skb->dev = vrf_dev;
  520. skb->skb_iif = vrf_dev->ifindex;
  521. skb_push(skb, skb->mac_len);
  522. dev_queue_xmit_nit(skb, vrf_dev);
  523. skb_pull(skb, skb->mac_len);
  524. IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
  525. }
  526. return skb;
  527. }
  528. #else
  529. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  530. struct sk_buff *skb)
  531. {
  532. return skb;
  533. }
  534. #endif
  535. static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
  536. struct sk_buff *skb)
  537. {
  538. skb->dev = vrf_dev;
  539. skb->skb_iif = vrf_dev->ifindex;
  540. skb_push(skb, skb->mac_len);
  541. dev_queue_xmit_nit(skb, vrf_dev);
  542. skb_pull(skb, skb->mac_len);
  543. return skb;
  544. }
  545. /* called with rcu lock held */
  546. static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
  547. struct sk_buff *skb,
  548. u16 proto)
  549. {
  550. switch (proto) {
  551. case AF_INET:
  552. return vrf_ip_rcv(vrf_dev, skb);
  553. case AF_INET6:
  554. return vrf_ip6_rcv(vrf_dev, skb);
  555. }
  556. return skb;
  557. }
  558. #if IS_ENABLED(CONFIG_IPV6)
  559. static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
  560. const struct flowi6 *fl6)
  561. {
  562. struct dst_entry *dst = NULL;
  563. if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) {
  564. struct net_vrf *vrf = netdev_priv(dev);
  565. struct rt6_info *rt;
  566. rcu_read_lock();
  567. rt = rcu_dereference(vrf->rt6);
  568. if (likely(rt)) {
  569. dst = &rt->dst;
  570. dst_hold(dst);
  571. }
  572. rcu_read_unlock();
  573. }
  574. return dst;
  575. }
  576. #endif
  577. static const struct l3mdev_ops vrf_l3mdev_ops = {
  578. .l3mdev_fib_table = vrf_fib_table,
  579. .l3mdev_get_rtable = vrf_get_rtable,
  580. .l3mdev_get_saddr = vrf_get_saddr,
  581. .l3mdev_l3_rcv = vrf_l3_rcv,
  582. #if IS_ENABLED(CONFIG_IPV6)
  583. .l3mdev_get_rt6_dst = vrf_get_rt6_dst,
  584. #endif
  585. };
  586. static void vrf_get_drvinfo(struct net_device *dev,
  587. struct ethtool_drvinfo *info)
  588. {
  589. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  590. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  591. }
  592. static const struct ethtool_ops vrf_ethtool_ops = {
  593. .get_drvinfo = vrf_get_drvinfo,
  594. };
  595. static void vrf_setup(struct net_device *dev)
  596. {
  597. ether_setup(dev);
  598. /* Initialize the device structure. */
  599. dev->netdev_ops = &vrf_netdev_ops;
  600. dev->l3mdev_ops = &vrf_l3mdev_ops;
  601. dev->ethtool_ops = &vrf_ethtool_ops;
  602. dev->destructor = free_netdev;
  603. /* Fill in device structure with ethernet-generic values. */
  604. eth_hw_addr_random(dev);
  605. /* don't acquire vrf device's netif_tx_lock when transmitting */
  606. dev->features |= NETIF_F_LLTX;
  607. /* don't allow vrf devices to change network namespaces. */
  608. dev->features |= NETIF_F_NETNS_LOCAL;
  609. }
  610. static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
  611. {
  612. if (tb[IFLA_ADDRESS]) {
  613. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  614. return -EINVAL;
  615. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  616. return -EADDRNOTAVAIL;
  617. }
  618. return 0;
  619. }
  620. static void vrf_dellink(struct net_device *dev, struct list_head *head)
  621. {
  622. unregister_netdevice_queue(dev, head);
  623. }
  624. static int vrf_newlink(struct net *src_net, struct net_device *dev,
  625. struct nlattr *tb[], struct nlattr *data[])
  626. {
  627. struct net_vrf *vrf = netdev_priv(dev);
  628. if (!data || !data[IFLA_VRF_TABLE])
  629. return -EINVAL;
  630. vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
  631. dev->priv_flags |= IFF_L3MDEV_MASTER;
  632. return register_netdevice(dev);
  633. }
  634. static size_t vrf_nl_getsize(const struct net_device *dev)
  635. {
  636. return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
  637. }
  638. static int vrf_fillinfo(struct sk_buff *skb,
  639. const struct net_device *dev)
  640. {
  641. struct net_vrf *vrf = netdev_priv(dev);
  642. return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
  643. }
  644. static size_t vrf_get_slave_size(const struct net_device *bond_dev,
  645. const struct net_device *slave_dev)
  646. {
  647. return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
  648. }
  649. static int vrf_fill_slave_info(struct sk_buff *skb,
  650. const struct net_device *vrf_dev,
  651. const struct net_device *slave_dev)
  652. {
  653. struct net_vrf *vrf = netdev_priv(vrf_dev);
  654. if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
  655. return -EMSGSIZE;
  656. return 0;
  657. }
  658. static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
  659. [IFLA_VRF_TABLE] = { .type = NLA_U32 },
  660. };
  661. static struct rtnl_link_ops vrf_link_ops __read_mostly = {
  662. .kind = DRV_NAME,
  663. .priv_size = sizeof(struct net_vrf),
  664. .get_size = vrf_nl_getsize,
  665. .policy = vrf_nl_policy,
  666. .validate = vrf_validate,
  667. .fill_info = vrf_fillinfo,
  668. .get_slave_size = vrf_get_slave_size,
  669. .fill_slave_info = vrf_fill_slave_info,
  670. .newlink = vrf_newlink,
  671. .dellink = vrf_dellink,
  672. .setup = vrf_setup,
  673. .maxtype = IFLA_VRF_MAX,
  674. };
  675. static int vrf_device_event(struct notifier_block *unused,
  676. unsigned long event, void *ptr)
  677. {
  678. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  679. /* only care about unregister events to drop slave references */
  680. if (event == NETDEV_UNREGISTER) {
  681. struct net_device *vrf_dev;
  682. if (!netif_is_l3_slave(dev))
  683. goto out;
  684. vrf_dev = netdev_master_upper_dev_get(dev);
  685. vrf_del_slave(vrf_dev, dev);
  686. }
  687. out:
  688. return NOTIFY_DONE;
  689. }
  690. static struct notifier_block vrf_notifier_block __read_mostly = {
  691. .notifier_call = vrf_device_event,
  692. };
  693. static int __init vrf_init_module(void)
  694. {
  695. int rc;
  696. register_netdevice_notifier(&vrf_notifier_block);
  697. rc = rtnl_link_register(&vrf_link_ops);
  698. if (rc < 0)
  699. goto error;
  700. return 0;
  701. error:
  702. unregister_netdevice_notifier(&vrf_notifier_block);
  703. return rc;
  704. }
  705. module_init(vrf_init_module);
  706. MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
  707. MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
  708. MODULE_LICENSE("GPL");
  709. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  710. MODULE_VERSION(DRV_VERSION);