vrf.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. #include <net/fib_rules.h>
  37. #include <net/netns/generic.h>
  38. #define DRV_NAME "vrf"
  39. #define DRV_VERSION "1.0"
  40. #define FIB_RULE_PREF 1000 /* default preference for FIB rules */
  41. static unsigned int vrf_net_id;
  42. struct net_vrf {
  43. struct rtable __rcu *rth;
  44. struct rtable __rcu *rth_local;
  45. struct rt6_info __rcu *rt6;
  46. struct rt6_info __rcu *rt6_local;
  47. u32 tb_id;
  48. };
  49. struct pcpu_dstats {
  50. u64 tx_pkts;
  51. u64 tx_bytes;
  52. u64 tx_drps;
  53. u64 rx_pkts;
  54. u64 rx_bytes;
  55. u64 rx_drps;
  56. struct u64_stats_sync syncp;
  57. };
  58. static void vrf_rx_stats(struct net_device *dev, int len)
  59. {
  60. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  61. u64_stats_update_begin(&dstats->syncp);
  62. dstats->rx_pkts++;
  63. dstats->rx_bytes += len;
  64. u64_stats_update_end(&dstats->syncp);
  65. }
  66. static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
  67. {
  68. vrf_dev->stats.tx_errors++;
  69. kfree_skb(skb);
  70. }
  71. static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
  72. struct rtnl_link_stats64 *stats)
  73. {
  74. int i;
  75. for_each_possible_cpu(i) {
  76. const struct pcpu_dstats *dstats;
  77. u64 tbytes, tpkts, tdrops, rbytes, rpkts;
  78. unsigned int start;
  79. dstats = per_cpu_ptr(dev->dstats, i);
  80. do {
  81. start = u64_stats_fetch_begin_irq(&dstats->syncp);
  82. tbytes = dstats->tx_bytes;
  83. tpkts = dstats->tx_pkts;
  84. tdrops = dstats->tx_drps;
  85. rbytes = dstats->rx_bytes;
  86. rpkts = dstats->rx_pkts;
  87. } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
  88. stats->tx_bytes += tbytes;
  89. stats->tx_packets += tpkts;
  90. stats->tx_dropped += tdrops;
  91. stats->rx_bytes += rbytes;
  92. stats->rx_packets += rpkts;
  93. }
  94. return stats;
  95. }
  96. /* Local traffic destined to local address. Reinsert the packet to rx
  97. * path, similar to loopback handling.
  98. */
  99. static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
  100. struct dst_entry *dst)
  101. {
  102. int len = skb->len;
  103. skb_orphan(skb);
  104. skb_dst_set(skb, dst);
  105. skb_dst_force(skb);
  106. /* set pkt_type to avoid skb hitting packet taps twice -
  107. * once on Tx and again in Rx processing
  108. */
  109. skb->pkt_type = PACKET_LOOPBACK;
  110. skb->protocol = eth_type_trans(skb, dev);
  111. if (likely(netif_rx(skb) == NET_RX_SUCCESS))
  112. vrf_rx_stats(dev, len);
  113. else
  114. this_cpu_inc(dev->dstats->rx_drps);
  115. return NETDEV_TX_OK;
  116. }
  117. #if IS_ENABLED(CONFIG_IPV6)
  118. static int vrf_ip6_local_out(struct net *net, struct sock *sk,
  119. struct sk_buff *skb)
  120. {
  121. int err;
  122. err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net,
  123. sk, skb, NULL, skb_dst(skb)->dev, dst_output);
  124. if (likely(err == 1))
  125. err = dst_output(net, sk, skb);
  126. return err;
  127. }
  128. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  129. struct net_device *dev)
  130. {
  131. const struct ipv6hdr *iph = ipv6_hdr(skb);
  132. struct net *net = dev_net(skb->dev);
  133. struct flowi6 fl6 = {
  134. /* needed to match OIF rule */
  135. .flowi6_oif = dev->ifindex,
  136. .flowi6_iif = LOOPBACK_IFINDEX,
  137. .daddr = iph->daddr,
  138. .saddr = iph->saddr,
  139. .flowlabel = ip6_flowinfo(iph),
  140. .flowi6_mark = skb->mark,
  141. .flowi6_proto = iph->nexthdr,
  142. .flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF,
  143. };
  144. int ret = NET_XMIT_DROP;
  145. struct dst_entry *dst;
  146. struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
  147. dst = ip6_route_output(net, NULL, &fl6);
  148. if (dst == dst_null)
  149. goto err;
  150. skb_dst_drop(skb);
  151. /* if dst.dev is loopback or the VRF device again this is locally
  152. * originated traffic destined to a local address. Short circuit
  153. * to Rx path using our local dst
  154. */
  155. if (dst->dev == net->loopback_dev || dst->dev == dev) {
  156. struct net_vrf *vrf = netdev_priv(dev);
  157. struct rt6_info *rt6_local;
  158. /* release looked up dst and use cached local dst */
  159. dst_release(dst);
  160. rcu_read_lock();
  161. rt6_local = rcu_dereference(vrf->rt6_local);
  162. if (unlikely(!rt6_local)) {
  163. rcu_read_unlock();
  164. goto err;
  165. }
  166. /* Ordering issue: cached local dst is created on newlink
  167. * before the IPv6 initialization. Using the local dst
  168. * requires rt6i_idev to be set so make sure it is.
  169. */
  170. if (unlikely(!rt6_local->rt6i_idev)) {
  171. rt6_local->rt6i_idev = in6_dev_get(dev);
  172. if (!rt6_local->rt6i_idev) {
  173. rcu_read_unlock();
  174. goto err;
  175. }
  176. }
  177. dst = &rt6_local->dst;
  178. dst_hold(dst);
  179. rcu_read_unlock();
  180. return vrf_local_xmit(skb, dev, &rt6_local->dst);
  181. }
  182. skb_dst_set(skb, dst);
  183. /* strip the ethernet header added for pass through VRF device */
  184. __skb_pull(skb, skb_network_offset(skb));
  185. ret = vrf_ip6_local_out(net, skb->sk, skb);
  186. if (unlikely(net_xmit_eval(ret)))
  187. dev->stats.tx_errors++;
  188. else
  189. ret = NET_XMIT_SUCCESS;
  190. return ret;
  191. err:
  192. vrf_tx_error(dev, skb);
  193. return NET_XMIT_DROP;
  194. }
  195. #else
  196. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  197. struct net_device *dev)
  198. {
  199. vrf_tx_error(dev, skb);
  200. return NET_XMIT_DROP;
  201. }
  202. #endif
  203. /* based on ip_local_out; can't use it b/c the dst is switched pointing to us */
  204. static int vrf_ip_local_out(struct net *net, struct sock *sk,
  205. struct sk_buff *skb)
  206. {
  207. int err;
  208. err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
  209. skb, NULL, skb_dst(skb)->dev, dst_output);
  210. if (likely(err == 1))
  211. err = dst_output(net, sk, skb);
  212. return err;
  213. }
  214. static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
  215. struct net_device *vrf_dev)
  216. {
  217. struct iphdr *ip4h = ip_hdr(skb);
  218. int ret = NET_XMIT_DROP;
  219. struct flowi4 fl4 = {
  220. /* needed to match OIF rule */
  221. .flowi4_oif = vrf_dev->ifindex,
  222. .flowi4_iif = LOOPBACK_IFINDEX,
  223. .flowi4_tos = RT_TOS(ip4h->tos),
  224. .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_SKIP_NH_OIF,
  225. .flowi4_proto = ip4h->protocol,
  226. .daddr = ip4h->daddr,
  227. .saddr = ip4h->saddr,
  228. };
  229. struct net *net = dev_net(vrf_dev);
  230. struct rtable *rt;
  231. rt = ip_route_output_flow(net, &fl4, NULL);
  232. if (IS_ERR(rt))
  233. goto err;
  234. if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
  235. ip_rt_put(rt);
  236. goto err;
  237. }
  238. skb_dst_drop(skb);
  239. /* if dst.dev is loopback or the VRF device again this is locally
  240. * originated traffic destined to a local address. Short circuit
  241. * to Rx path using our local dst
  242. */
  243. if (rt->dst.dev == net->loopback_dev || rt->dst.dev == vrf_dev) {
  244. struct net_vrf *vrf = netdev_priv(vrf_dev);
  245. struct rtable *rth_local;
  246. struct dst_entry *dst = NULL;
  247. ip_rt_put(rt);
  248. rcu_read_lock();
  249. rth_local = rcu_dereference(vrf->rth_local);
  250. if (likely(rth_local)) {
  251. dst = &rth_local->dst;
  252. dst_hold(dst);
  253. }
  254. rcu_read_unlock();
  255. if (unlikely(!dst))
  256. goto err;
  257. return vrf_local_xmit(skb, vrf_dev, dst);
  258. }
  259. skb_dst_set(skb, &rt->dst);
  260. /* strip the ethernet header added for pass through VRF device */
  261. __skb_pull(skb, skb_network_offset(skb));
  262. if (!ip4h->saddr) {
  263. ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
  264. RT_SCOPE_LINK);
  265. }
  266. ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  267. if (unlikely(net_xmit_eval(ret)))
  268. vrf_dev->stats.tx_errors++;
  269. else
  270. ret = NET_XMIT_SUCCESS;
  271. out:
  272. return ret;
  273. err:
  274. vrf_tx_error(vrf_dev, skb);
  275. goto out;
  276. }
  277. static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
  278. {
  279. switch (skb->protocol) {
  280. case htons(ETH_P_IP):
  281. return vrf_process_v4_outbound(skb, dev);
  282. case htons(ETH_P_IPV6):
  283. return vrf_process_v6_outbound(skb, dev);
  284. default:
  285. vrf_tx_error(dev, skb);
  286. return NET_XMIT_DROP;
  287. }
  288. }
  289. static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
  290. {
  291. int len = skb->len;
  292. netdev_tx_t ret = is_ip_tx_frame(skb, dev);
  293. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  294. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  295. u64_stats_update_begin(&dstats->syncp);
  296. dstats->tx_pkts++;
  297. dstats->tx_bytes += len;
  298. u64_stats_update_end(&dstats->syncp);
  299. } else {
  300. this_cpu_inc(dev->dstats->tx_drps);
  301. }
  302. return ret;
  303. }
  304. #if IS_ENABLED(CONFIG_IPV6)
  305. /* modelled after ip6_finish_output2 */
  306. static int vrf_finish_output6(struct net *net, struct sock *sk,
  307. struct sk_buff *skb)
  308. {
  309. struct dst_entry *dst = skb_dst(skb);
  310. struct net_device *dev = dst->dev;
  311. struct neighbour *neigh;
  312. struct in6_addr *nexthop;
  313. int ret;
  314. nf_reset(skb);
  315. skb->protocol = htons(ETH_P_IPV6);
  316. skb->dev = dev;
  317. rcu_read_lock_bh();
  318. nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
  319. neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
  320. if (unlikely(!neigh))
  321. neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
  322. if (!IS_ERR(neigh)) {
  323. ret = dst_neigh_output(dst, neigh, skb);
  324. rcu_read_unlock_bh();
  325. return ret;
  326. }
  327. rcu_read_unlock_bh();
  328. IP6_INC_STATS(dev_net(dst->dev),
  329. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  330. kfree_skb(skb);
  331. return -EINVAL;
  332. }
  333. /* modelled after ip6_output */
  334. static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
  335. {
  336. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  337. net, sk, skb, NULL, skb_dst(skb)->dev,
  338. vrf_finish_output6,
  339. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  340. }
  341. /* set dst on skb to send packet to us via dev_xmit path. Allows
  342. * packet to go through device based features such as qdisc, netfilter
  343. * hooks and packet sockets with skb->dev set to vrf device.
  344. */
  345. static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
  346. struct sock *sk,
  347. struct sk_buff *skb)
  348. {
  349. struct net_vrf *vrf = netdev_priv(vrf_dev);
  350. struct dst_entry *dst = NULL;
  351. struct rt6_info *rt6;
  352. /* don't divert link scope packets */
  353. if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
  354. return skb;
  355. rcu_read_lock();
  356. rt6 = rcu_dereference(vrf->rt6);
  357. if (likely(rt6)) {
  358. dst = &rt6->dst;
  359. dst_hold(dst);
  360. }
  361. rcu_read_unlock();
  362. if (unlikely(!dst)) {
  363. vrf_tx_error(vrf_dev, skb);
  364. return NULL;
  365. }
  366. skb_dst_drop(skb);
  367. skb_dst_set(skb, dst);
  368. return skb;
  369. }
  370. /* holding rtnl */
  371. static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
  372. {
  373. struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
  374. struct rt6_info *rt6_local = rtnl_dereference(vrf->rt6_local);
  375. struct net *net = dev_net(dev);
  376. struct dst_entry *dst;
  377. RCU_INIT_POINTER(vrf->rt6, NULL);
  378. RCU_INIT_POINTER(vrf->rt6_local, NULL);
  379. synchronize_rcu();
  380. /* move dev in dst's to loopback so this VRF device can be deleted
  381. * - based on dst_ifdown
  382. */
  383. if (rt6) {
  384. dst = &rt6->dst;
  385. dev_put(dst->dev);
  386. dst->dev = net->loopback_dev;
  387. dev_hold(dst->dev);
  388. dst_release(dst);
  389. }
  390. if (rt6_local) {
  391. if (rt6_local->rt6i_idev) {
  392. in6_dev_put(rt6_local->rt6i_idev);
  393. rt6_local->rt6i_idev = NULL;
  394. }
  395. dst = &rt6_local->dst;
  396. dev_put(dst->dev);
  397. dst->dev = net->loopback_dev;
  398. dev_hold(dst->dev);
  399. dst_release(dst);
  400. }
  401. }
  402. static int vrf_rt6_create(struct net_device *dev)
  403. {
  404. int flags = DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE;
  405. struct net_vrf *vrf = netdev_priv(dev);
  406. struct net *net = dev_net(dev);
  407. struct fib6_table *rt6i_table;
  408. struct rt6_info *rt6, *rt6_local;
  409. int rc = -ENOMEM;
  410. /* IPv6 can be CONFIG enabled and then disabled runtime */
  411. if (!ipv6_mod_enabled())
  412. return 0;
  413. rt6i_table = fib6_new_table(net, vrf->tb_id);
  414. if (!rt6i_table)
  415. goto out;
  416. /* create a dst for routing packets out a VRF device */
  417. rt6 = ip6_dst_alloc(net, dev, flags);
  418. if (!rt6)
  419. goto out;
  420. dst_hold(&rt6->dst);
  421. rt6->rt6i_table = rt6i_table;
  422. rt6->dst.output = vrf_output6;
  423. /* create a dst for local routing - packets sent locally
  424. * to local address via the VRF device as a loopback
  425. */
  426. rt6_local = ip6_dst_alloc(net, dev, flags);
  427. if (!rt6_local) {
  428. dst_release(&rt6->dst);
  429. goto out;
  430. }
  431. dst_hold(&rt6_local->dst);
  432. rt6_local->rt6i_idev = in6_dev_get(dev);
  433. rt6_local->rt6i_flags = RTF_UP | RTF_NONEXTHOP | RTF_LOCAL;
  434. rt6_local->rt6i_table = rt6i_table;
  435. rt6_local->dst.input = ip6_input;
  436. rcu_assign_pointer(vrf->rt6, rt6);
  437. rcu_assign_pointer(vrf->rt6_local, rt6_local);
  438. rc = 0;
  439. out:
  440. return rc;
  441. }
  442. #else
  443. static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
  444. struct sock *sk,
  445. struct sk_buff *skb)
  446. {
  447. return skb;
  448. }
  449. static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
  450. {
  451. }
  452. static int vrf_rt6_create(struct net_device *dev)
  453. {
  454. return 0;
  455. }
  456. #endif
  457. /* modelled after ip_finish_output2 */
  458. static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  459. {
  460. struct dst_entry *dst = skb_dst(skb);
  461. struct rtable *rt = (struct rtable *)dst;
  462. struct net_device *dev = dst->dev;
  463. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  464. struct neighbour *neigh;
  465. u32 nexthop;
  466. int ret = -EINVAL;
  467. nf_reset(skb);
  468. /* Be paranoid, rather than too clever. */
  469. if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
  470. struct sk_buff *skb2;
  471. skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
  472. if (!skb2) {
  473. ret = -ENOMEM;
  474. goto err;
  475. }
  476. if (skb->sk)
  477. skb_set_owner_w(skb2, skb->sk);
  478. consume_skb(skb);
  479. skb = skb2;
  480. }
  481. rcu_read_lock_bh();
  482. nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
  483. neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
  484. if (unlikely(!neigh))
  485. neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
  486. if (!IS_ERR(neigh)) {
  487. ret = dst_neigh_output(dst, neigh, skb);
  488. rcu_read_unlock_bh();
  489. return ret;
  490. }
  491. rcu_read_unlock_bh();
  492. err:
  493. vrf_tx_error(skb->dev, skb);
  494. return ret;
  495. }
  496. static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  497. {
  498. struct net_device *dev = skb_dst(skb)->dev;
  499. IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
  500. skb->dev = dev;
  501. skb->protocol = htons(ETH_P_IP);
  502. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  503. net, sk, skb, NULL, dev,
  504. vrf_finish_output,
  505. !(IPCB(skb)->flags & IPSKB_REROUTED));
  506. }
  507. /* set dst on skb to send packet to us via dev_xmit path. Allows
  508. * packet to go through device based features such as qdisc, netfilter
  509. * hooks and packet sockets with skb->dev set to vrf device.
  510. */
  511. static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
  512. struct sock *sk,
  513. struct sk_buff *skb)
  514. {
  515. struct net_vrf *vrf = netdev_priv(vrf_dev);
  516. struct dst_entry *dst = NULL;
  517. struct rtable *rth;
  518. rcu_read_lock();
  519. rth = rcu_dereference(vrf->rth);
  520. if (likely(rth)) {
  521. dst = &rth->dst;
  522. dst_hold(dst);
  523. }
  524. rcu_read_unlock();
  525. if (unlikely(!dst)) {
  526. vrf_tx_error(vrf_dev, skb);
  527. return NULL;
  528. }
  529. skb_dst_drop(skb);
  530. skb_dst_set(skb, dst);
  531. return skb;
  532. }
  533. /* called with rcu lock held */
  534. static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
  535. struct sock *sk,
  536. struct sk_buff *skb,
  537. u16 proto)
  538. {
  539. switch (proto) {
  540. case AF_INET:
  541. return vrf_ip_out(vrf_dev, sk, skb);
  542. case AF_INET6:
  543. return vrf_ip6_out(vrf_dev, sk, skb);
  544. }
  545. return skb;
  546. }
  547. /* holding rtnl */
  548. static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
  549. {
  550. struct rtable *rth = rtnl_dereference(vrf->rth);
  551. struct rtable *rth_local = rtnl_dereference(vrf->rth_local);
  552. struct net *net = dev_net(dev);
  553. struct dst_entry *dst;
  554. RCU_INIT_POINTER(vrf->rth, NULL);
  555. RCU_INIT_POINTER(vrf->rth_local, NULL);
  556. synchronize_rcu();
  557. /* move dev in dst's to loopback so this VRF device can be deleted
  558. * - based on dst_ifdown
  559. */
  560. if (rth) {
  561. dst = &rth->dst;
  562. dev_put(dst->dev);
  563. dst->dev = net->loopback_dev;
  564. dev_hold(dst->dev);
  565. dst_release(dst);
  566. }
  567. if (rth_local) {
  568. dst = &rth_local->dst;
  569. dev_put(dst->dev);
  570. dst->dev = net->loopback_dev;
  571. dev_hold(dst->dev);
  572. dst_release(dst);
  573. }
  574. }
  575. static int vrf_rtable_create(struct net_device *dev)
  576. {
  577. struct net_vrf *vrf = netdev_priv(dev);
  578. struct rtable *rth, *rth_local;
  579. if (!fib_new_table(dev_net(dev), vrf->tb_id))
  580. return -ENOMEM;
  581. /* create a dst for routing packets out through a VRF device */
  582. rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
  583. if (!rth)
  584. return -ENOMEM;
  585. /* create a dst for local ingress routing - packets sent locally
  586. * to local address via the VRF device as a loopback
  587. */
  588. rth_local = rt_dst_alloc(dev, RTCF_LOCAL, RTN_LOCAL, 1, 1, 0);
  589. if (!rth_local) {
  590. dst_release(&rth->dst);
  591. return -ENOMEM;
  592. }
  593. rth->dst.output = vrf_output;
  594. rth->rt_table_id = vrf->tb_id;
  595. rth_local->rt_table_id = vrf->tb_id;
  596. rcu_assign_pointer(vrf->rth, rth);
  597. rcu_assign_pointer(vrf->rth_local, rth_local);
  598. return 0;
  599. }
  600. /**************************** device handling ********************/
  601. /* cycle interface to flush neighbor cache and move routes across tables */
  602. static void cycle_netdev(struct net_device *dev)
  603. {
  604. unsigned int flags = dev->flags;
  605. int ret;
  606. if (!netif_running(dev))
  607. return;
  608. ret = dev_change_flags(dev, flags & ~IFF_UP);
  609. if (ret >= 0)
  610. ret = dev_change_flags(dev, flags);
  611. if (ret < 0) {
  612. netdev_err(dev,
  613. "Failed to cycle device %s; route tables might be wrong!\n",
  614. dev->name);
  615. }
  616. }
  617. static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  618. {
  619. int ret;
  620. ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
  621. if (ret < 0)
  622. return ret;
  623. port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
  624. cycle_netdev(port_dev);
  625. return 0;
  626. }
  627. static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  628. {
  629. if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
  630. return -EINVAL;
  631. return do_vrf_add_slave(dev, port_dev);
  632. }
  633. /* inverse of do_vrf_add_slave */
  634. static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  635. {
  636. netdev_upper_dev_unlink(port_dev, dev);
  637. port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
  638. cycle_netdev(port_dev);
  639. return 0;
  640. }
  641. static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  642. {
  643. return do_vrf_del_slave(dev, port_dev);
  644. }
  645. static void vrf_dev_uninit(struct net_device *dev)
  646. {
  647. struct net_vrf *vrf = netdev_priv(dev);
  648. vrf_rtable_release(dev, vrf);
  649. vrf_rt6_release(dev, vrf);
  650. free_percpu(dev->dstats);
  651. dev->dstats = NULL;
  652. }
  653. static int vrf_dev_init(struct net_device *dev)
  654. {
  655. struct net_vrf *vrf = netdev_priv(dev);
  656. dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
  657. if (!dev->dstats)
  658. goto out_nomem;
  659. /* create the default dst which points back to us */
  660. if (vrf_rtable_create(dev) != 0)
  661. goto out_stats;
  662. if (vrf_rt6_create(dev) != 0)
  663. goto out_rth;
  664. dev->flags = IFF_MASTER | IFF_NOARP;
  665. /* MTU is irrelevant for VRF device; set to 64k similar to lo */
  666. dev->mtu = 64 * 1024;
  667. /* similarly, oper state is irrelevant; set to up to avoid confusion */
  668. dev->operstate = IF_OPER_UP;
  669. netdev_lockdep_set_classes(dev);
  670. return 0;
  671. out_rth:
  672. vrf_rtable_release(dev, vrf);
  673. out_stats:
  674. free_percpu(dev->dstats);
  675. dev->dstats = NULL;
  676. out_nomem:
  677. return -ENOMEM;
  678. }
  679. static const struct net_device_ops vrf_netdev_ops = {
  680. .ndo_init = vrf_dev_init,
  681. .ndo_uninit = vrf_dev_uninit,
  682. .ndo_start_xmit = vrf_xmit,
  683. .ndo_get_stats64 = vrf_get_stats64,
  684. .ndo_add_slave = vrf_add_slave,
  685. .ndo_del_slave = vrf_del_slave,
  686. };
  687. static u32 vrf_fib_table(const struct net_device *dev)
  688. {
  689. struct net_vrf *vrf = netdev_priv(dev);
  690. return vrf->tb_id;
  691. }
  692. static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  693. {
  694. kfree_skb(skb);
  695. return 0;
  696. }
  697. static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook,
  698. struct sk_buff *skb,
  699. struct net_device *dev)
  700. {
  701. struct net *net = dev_net(dev);
  702. if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1)
  703. skb = NULL; /* kfree_skb(skb) handled by nf code */
  704. return skb;
  705. }
  706. #if IS_ENABLED(CONFIG_IPV6)
  707. /* neighbor handling is done with actual device; do not want
  708. * to flip skb->dev for those ndisc packets. This really fails
  709. * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
  710. * a start.
  711. */
  712. static bool ipv6_ndisc_frame(const struct sk_buff *skb)
  713. {
  714. const struct ipv6hdr *iph = ipv6_hdr(skb);
  715. bool rc = false;
  716. if (iph->nexthdr == NEXTHDR_ICMP) {
  717. const struct icmp6hdr *icmph;
  718. struct icmp6hdr _icmph;
  719. icmph = skb_header_pointer(skb, sizeof(*iph),
  720. sizeof(_icmph), &_icmph);
  721. if (!icmph)
  722. goto out;
  723. switch (icmph->icmp6_type) {
  724. case NDISC_ROUTER_SOLICITATION:
  725. case NDISC_ROUTER_ADVERTISEMENT:
  726. case NDISC_NEIGHBOUR_SOLICITATION:
  727. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  728. case NDISC_REDIRECT:
  729. rc = true;
  730. break;
  731. }
  732. }
  733. out:
  734. return rc;
  735. }
  736. static struct rt6_info *vrf_ip6_route_lookup(struct net *net,
  737. const struct net_device *dev,
  738. struct flowi6 *fl6,
  739. int ifindex,
  740. int flags)
  741. {
  742. struct net_vrf *vrf = netdev_priv(dev);
  743. struct fib6_table *table = NULL;
  744. struct rt6_info *rt6;
  745. rcu_read_lock();
  746. /* fib6_table does not have a refcnt and can not be freed */
  747. rt6 = rcu_dereference(vrf->rt6);
  748. if (likely(rt6))
  749. table = rt6->rt6i_table;
  750. rcu_read_unlock();
  751. if (!table)
  752. return NULL;
  753. return ip6_pol_route(net, table, ifindex, fl6, flags);
  754. }
  755. static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev,
  756. int ifindex)
  757. {
  758. const struct ipv6hdr *iph = ipv6_hdr(skb);
  759. struct flowi6 fl6 = {
  760. .daddr = iph->daddr,
  761. .saddr = iph->saddr,
  762. .flowlabel = ip6_flowinfo(iph),
  763. .flowi6_mark = skb->mark,
  764. .flowi6_proto = iph->nexthdr,
  765. .flowi6_iif = ifindex,
  766. };
  767. struct net *net = dev_net(vrf_dev);
  768. struct rt6_info *rt6;
  769. rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex,
  770. RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE);
  771. if (unlikely(!rt6))
  772. return;
  773. if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst))
  774. return;
  775. skb_dst_set(skb, &rt6->dst);
  776. }
  777. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  778. struct sk_buff *skb)
  779. {
  780. int orig_iif = skb->skb_iif;
  781. bool need_strict;
  782. /* loopback traffic; do not push through packet taps again.
  783. * Reset pkt_type for upper layers to process skb
  784. */
  785. if (skb->pkt_type == PACKET_LOOPBACK) {
  786. skb->dev = vrf_dev;
  787. skb->skb_iif = vrf_dev->ifindex;
  788. IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
  789. skb->pkt_type = PACKET_HOST;
  790. goto out;
  791. }
  792. /* if packet is NDISC or addressed to multicast or link-local
  793. * then keep the ingress interface
  794. */
  795. need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr);
  796. if (!ipv6_ndisc_frame(skb) && !need_strict) {
  797. vrf_rx_stats(vrf_dev, skb->len);
  798. skb->dev = vrf_dev;
  799. skb->skb_iif = vrf_dev->ifindex;
  800. skb_push(skb, skb->mac_len);
  801. dev_queue_xmit_nit(skb, vrf_dev);
  802. skb_pull(skb, skb->mac_len);
  803. IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
  804. }
  805. if (need_strict)
  806. vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
  807. skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev);
  808. out:
  809. return skb;
  810. }
  811. #else
  812. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  813. struct sk_buff *skb)
  814. {
  815. return skb;
  816. }
  817. #endif
  818. static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
  819. struct sk_buff *skb)
  820. {
  821. skb->dev = vrf_dev;
  822. skb->skb_iif = vrf_dev->ifindex;
  823. IPCB(skb)->flags |= IPSKB_L3SLAVE;
  824. /* loopback traffic; do not push through packet taps again.
  825. * Reset pkt_type for upper layers to process skb
  826. */
  827. if (skb->pkt_type == PACKET_LOOPBACK) {
  828. skb->pkt_type = PACKET_HOST;
  829. goto out;
  830. }
  831. vrf_rx_stats(vrf_dev, skb->len);
  832. skb_push(skb, skb->mac_len);
  833. dev_queue_xmit_nit(skb, vrf_dev);
  834. skb_pull(skb, skb->mac_len);
  835. skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
  836. out:
  837. return skb;
  838. }
  839. /* called with rcu lock held */
  840. static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
  841. struct sk_buff *skb,
  842. u16 proto)
  843. {
  844. switch (proto) {
  845. case AF_INET:
  846. return vrf_ip_rcv(vrf_dev, skb);
  847. case AF_INET6:
  848. return vrf_ip6_rcv(vrf_dev, skb);
  849. }
  850. return skb;
  851. }
  852. #if IS_ENABLED(CONFIG_IPV6)
  853. /* send to link-local or multicast address via interface enslaved to
  854. * VRF device. Force lookup to VRF table without changing flow struct
  855. */
  856. static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev,
  857. struct flowi6 *fl6)
  858. {
  859. struct net *net = dev_net(dev);
  860. int flags = RT6_LOOKUP_F_IFACE;
  861. struct dst_entry *dst = NULL;
  862. struct rt6_info *rt;
  863. /* VRF device does not have a link-local address and
  864. * sending packets to link-local or mcast addresses over
  865. * a VRF device does not make sense
  866. */
  867. if (fl6->flowi6_oif == dev->ifindex) {
  868. dst = &net->ipv6.ip6_null_entry->dst;
  869. dst_hold(dst);
  870. return dst;
  871. }
  872. if (!ipv6_addr_any(&fl6->saddr))
  873. flags |= RT6_LOOKUP_F_HAS_SADDR;
  874. rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, flags);
  875. if (rt)
  876. dst = &rt->dst;
  877. return dst;
  878. }
  879. #endif
  880. static const struct l3mdev_ops vrf_l3mdev_ops = {
  881. .l3mdev_fib_table = vrf_fib_table,
  882. .l3mdev_l3_rcv = vrf_l3_rcv,
  883. .l3mdev_l3_out = vrf_l3_out,
  884. #if IS_ENABLED(CONFIG_IPV6)
  885. .l3mdev_link_scope_lookup = vrf_link_scope_lookup,
  886. #endif
  887. };
  888. static void vrf_get_drvinfo(struct net_device *dev,
  889. struct ethtool_drvinfo *info)
  890. {
  891. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  892. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  893. }
  894. static const struct ethtool_ops vrf_ethtool_ops = {
  895. .get_drvinfo = vrf_get_drvinfo,
  896. };
  897. static inline size_t vrf_fib_rule_nl_size(void)
  898. {
  899. size_t sz;
  900. sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr));
  901. sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */
  902. sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */
  903. return sz;
  904. }
  905. static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
  906. {
  907. struct fib_rule_hdr *frh;
  908. struct nlmsghdr *nlh;
  909. struct sk_buff *skb;
  910. int err;
  911. if (family == AF_INET6 && !ipv6_mod_enabled())
  912. return 0;
  913. skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL);
  914. if (!skb)
  915. return -ENOMEM;
  916. nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0);
  917. if (!nlh)
  918. goto nla_put_failure;
  919. /* rule only needs to appear once */
  920. nlh->nlmsg_flags |= NLM_F_EXCL;
  921. frh = nlmsg_data(nlh);
  922. memset(frh, 0, sizeof(*frh));
  923. frh->family = family;
  924. frh->action = FR_ACT_TO_TBL;
  925. if (nla_put_u8(skb, FRA_L3MDEV, 1))
  926. goto nla_put_failure;
  927. if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF))
  928. goto nla_put_failure;
  929. nlmsg_end(skb, nlh);
  930. /* fib_nl_{new,del}rule handling looks for net from skb->sk */
  931. skb->sk = dev_net(dev)->rtnl;
  932. if (add_it) {
  933. err = fib_nl_newrule(skb, nlh);
  934. if (err == -EEXIST)
  935. err = 0;
  936. } else {
  937. err = fib_nl_delrule(skb, nlh);
  938. if (err == -ENOENT)
  939. err = 0;
  940. }
  941. nlmsg_free(skb);
  942. return err;
  943. nla_put_failure:
  944. nlmsg_free(skb);
  945. return -EMSGSIZE;
  946. }
  947. static int vrf_add_fib_rules(const struct net_device *dev)
  948. {
  949. int err;
  950. err = vrf_fib_rule(dev, AF_INET, true);
  951. if (err < 0)
  952. goto out_err;
  953. err = vrf_fib_rule(dev, AF_INET6, true);
  954. if (err < 0)
  955. goto ipv6_err;
  956. return 0;
  957. ipv6_err:
  958. vrf_fib_rule(dev, AF_INET, false);
  959. out_err:
  960. netdev_err(dev, "Failed to add FIB rules.\n");
  961. return err;
  962. }
  963. static void vrf_setup(struct net_device *dev)
  964. {
  965. ether_setup(dev);
  966. /* Initialize the device structure. */
  967. dev->netdev_ops = &vrf_netdev_ops;
  968. dev->l3mdev_ops = &vrf_l3mdev_ops;
  969. dev->ethtool_ops = &vrf_ethtool_ops;
  970. dev->destructor = free_netdev;
  971. /* Fill in device structure with ethernet-generic values. */
  972. eth_hw_addr_random(dev);
  973. /* don't acquire vrf device's netif_tx_lock when transmitting */
  974. dev->features |= NETIF_F_LLTX;
  975. /* don't allow vrf devices to change network namespaces. */
  976. dev->features |= NETIF_F_NETNS_LOCAL;
  977. /* does not make sense for a VLAN to be added to a vrf device */
  978. dev->features |= NETIF_F_VLAN_CHALLENGED;
  979. /* enable offload features */
  980. dev->features |= NETIF_F_GSO_SOFTWARE;
  981. dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM;
  982. dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
  983. dev->hw_features = dev->features;
  984. dev->hw_enc_features = dev->features;
  985. /* default to no qdisc; user can add if desired */
  986. dev->priv_flags |= IFF_NO_QUEUE;
  987. }
  988. static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
  989. {
  990. if (tb[IFLA_ADDRESS]) {
  991. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  992. return -EINVAL;
  993. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  994. return -EADDRNOTAVAIL;
  995. }
  996. return 0;
  997. }
  998. static void vrf_dellink(struct net_device *dev, struct list_head *head)
  999. {
  1000. struct net_device *port_dev;
  1001. struct list_head *iter;
  1002. netdev_for_each_lower_dev(dev, port_dev, iter)
  1003. vrf_del_slave(dev, port_dev);
  1004. unregister_netdevice_queue(dev, head);
  1005. }
  1006. static int vrf_newlink(struct net *src_net, struct net_device *dev,
  1007. struct nlattr *tb[], struct nlattr *data[])
  1008. {
  1009. struct net_vrf *vrf = netdev_priv(dev);
  1010. bool *add_fib_rules;
  1011. struct net *net;
  1012. int err;
  1013. if (!data || !data[IFLA_VRF_TABLE])
  1014. return -EINVAL;
  1015. vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
  1016. if (vrf->tb_id == RT_TABLE_UNSPEC)
  1017. return -EINVAL;
  1018. dev->priv_flags |= IFF_L3MDEV_MASTER;
  1019. err = register_netdevice(dev);
  1020. if (err)
  1021. goto out;
  1022. net = dev_net(dev);
  1023. add_fib_rules = net_generic(net, vrf_net_id);
  1024. if (*add_fib_rules) {
  1025. err = vrf_add_fib_rules(dev);
  1026. if (err) {
  1027. unregister_netdevice(dev);
  1028. goto out;
  1029. }
  1030. *add_fib_rules = false;
  1031. }
  1032. out:
  1033. return err;
  1034. }
  1035. static size_t vrf_nl_getsize(const struct net_device *dev)
  1036. {
  1037. return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
  1038. }
  1039. static int vrf_fillinfo(struct sk_buff *skb,
  1040. const struct net_device *dev)
  1041. {
  1042. struct net_vrf *vrf = netdev_priv(dev);
  1043. return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
  1044. }
  1045. static size_t vrf_get_slave_size(const struct net_device *bond_dev,
  1046. const struct net_device *slave_dev)
  1047. {
  1048. return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
  1049. }
  1050. static int vrf_fill_slave_info(struct sk_buff *skb,
  1051. const struct net_device *vrf_dev,
  1052. const struct net_device *slave_dev)
  1053. {
  1054. struct net_vrf *vrf = netdev_priv(vrf_dev);
  1055. if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
  1056. return -EMSGSIZE;
  1057. return 0;
  1058. }
  1059. static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
  1060. [IFLA_VRF_TABLE] = { .type = NLA_U32 },
  1061. };
  1062. static struct rtnl_link_ops vrf_link_ops __read_mostly = {
  1063. .kind = DRV_NAME,
  1064. .priv_size = sizeof(struct net_vrf),
  1065. .get_size = vrf_nl_getsize,
  1066. .policy = vrf_nl_policy,
  1067. .validate = vrf_validate,
  1068. .fill_info = vrf_fillinfo,
  1069. .get_slave_size = vrf_get_slave_size,
  1070. .fill_slave_info = vrf_fill_slave_info,
  1071. .newlink = vrf_newlink,
  1072. .dellink = vrf_dellink,
  1073. .setup = vrf_setup,
  1074. .maxtype = IFLA_VRF_MAX,
  1075. };
  1076. static int vrf_device_event(struct notifier_block *unused,
  1077. unsigned long event, void *ptr)
  1078. {
  1079. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1080. /* only care about unregister events to drop slave references */
  1081. if (event == NETDEV_UNREGISTER) {
  1082. struct net_device *vrf_dev;
  1083. if (!netif_is_l3_slave(dev))
  1084. goto out;
  1085. vrf_dev = netdev_master_upper_dev_get(dev);
  1086. vrf_del_slave(vrf_dev, dev);
  1087. }
  1088. out:
  1089. return NOTIFY_DONE;
  1090. }
  1091. static struct notifier_block vrf_notifier_block __read_mostly = {
  1092. .notifier_call = vrf_device_event,
  1093. };
  1094. /* Initialize per network namespace state */
  1095. static int __net_init vrf_netns_init(struct net *net)
  1096. {
  1097. bool *add_fib_rules = net_generic(net, vrf_net_id);
  1098. *add_fib_rules = true;
  1099. return 0;
  1100. }
  1101. static struct pernet_operations vrf_net_ops __net_initdata = {
  1102. .init = vrf_netns_init,
  1103. .id = &vrf_net_id,
  1104. .size = sizeof(bool),
  1105. };
  1106. static int __init vrf_init_module(void)
  1107. {
  1108. int rc;
  1109. register_netdevice_notifier(&vrf_notifier_block);
  1110. rc = register_pernet_subsys(&vrf_net_ops);
  1111. if (rc < 0)
  1112. goto error;
  1113. rc = rtnl_link_register(&vrf_link_ops);
  1114. if (rc < 0) {
  1115. unregister_pernet_subsys(&vrf_net_ops);
  1116. goto error;
  1117. }
  1118. return 0;
  1119. error:
  1120. unregister_netdevice_notifier(&vrf_notifier_block);
  1121. return rc;
  1122. }
  1123. module_init(vrf_init_module);
  1124. MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
  1125. MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
  1126. MODULE_LICENSE("GPL");
  1127. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  1128. MODULE_VERSION(DRV_VERSION);