vrf.c 31 KB

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