vrf.c 31 KB

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