vrf.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * vrf.c: device driver to encapsulate a VRF space
  4. *
  5. * Copyright (c) 2015 Cumulus Networks. All rights reserved.
  6. * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
  7. * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
  8. *
  9. * Based on dummy, team and ipvlan drivers
  10. */
  11. #include <linux/ethtool.h>
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/ip.h>
  17. #include <linux/init.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/netfilter.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/rtnetlink.h>
  22. #include <linux/u64_stats_sync.h>
  23. #include <linux/hashtable.h>
  24. #include <linux/spinlock_types.h>
  25. #include <linux/inetdevice.h>
  26. #include <net/arp.h>
  27. #include <net/flow.h>
  28. #include <net/ip.h>
  29. #include <net/ip_fib.h>
  30. #include <net/ip6_fib.h>
  31. #include <net/ip6_route.h>
  32. #include <net/route.h>
  33. #include <net/addrconf.h>
  34. #include <net/l3mdev.h>
  35. #include <net/fib_rules.h>
  36. #include <net/netdev_lock.h>
  37. #include <net/sch_generic.h>
  38. #include <net/netns/generic.h>
  39. #include <net/netfilter/nf_conntrack.h>
  40. #define DRV_NAME "vrf"
  41. #define DRV_VERSION "1.1"
  42. #define FIB_RULE_PREF 1000 /* default preference for FIB rules */
  43. #define HT_MAP_BITS 4
  44. #define HASH_INITVAL ((u32)0xcafef00d)
  45. struct vrf_map {
  46. DECLARE_HASHTABLE(ht, HT_MAP_BITS);
  47. spinlock_t vmap_lock;
  48. /* shared_tables:
  49. * count how many distinct tables do not comply with the strict mode
  50. * requirement.
  51. * shared_tables value must be 0 in order to enable the strict mode.
  52. *
  53. * example of the evolution of shared_tables:
  54. * | time
  55. * add vrf0 --> table 100 shared_tables = 0 | t0
  56. * add vrf1 --> table 101 shared_tables = 0 | t1
  57. * add vrf2 --> table 100 shared_tables = 1 | t2
  58. * add vrf3 --> table 100 shared_tables = 1 | t3
  59. * add vrf4 --> table 101 shared_tables = 2 v t4
  60. *
  61. * shared_tables is a "step function" (or "staircase function")
  62. * and it is increased by one when the second vrf is associated to a
  63. * table.
  64. *
  65. * at t2, vrf0 and vrf2 are bound to table 100: shared_tables = 1.
  66. *
  67. * at t3, another dev (vrf3) is bound to the same table 100 but the
  68. * value of shared_tables is still 1.
  69. * This means that no matter how many new vrfs will register on the
  70. * table 100, the shared_tables will not increase (considering only
  71. * table 100).
  72. *
  73. * at t4, vrf4 is bound to table 101, and shared_tables = 2.
  74. *
  75. * Looking at the value of shared_tables we can immediately know if
  76. * the strict_mode can or cannot be enforced. Indeed, strict_mode
  77. * can be enforced iff shared_tables = 0.
  78. *
  79. * Conversely, shared_tables is decreased when a vrf is de-associated
  80. * from a table with exactly two associated vrfs.
  81. */
  82. u32 shared_tables;
  83. bool strict_mode;
  84. };
  85. struct vrf_map_elem {
  86. struct hlist_node hnode;
  87. struct list_head vrf_list; /* VRFs registered to this table */
  88. u32 table_id;
  89. int users;
  90. int ifindex;
  91. };
  92. static unsigned int vrf_net_id;
  93. /* per netns vrf data */
  94. struct netns_vrf {
  95. /* protected by rtnl lock */
  96. bool add_fib_rules;
  97. struct vrf_map vmap;
  98. struct ctl_table_header *ctl_hdr;
  99. };
  100. struct net_vrf {
  101. struct rtable __rcu *rth;
  102. struct rt6_info __rcu *rt6;
  103. #if IS_ENABLED(CONFIG_IPV6)
  104. struct fib6_table *fib6_table;
  105. #endif
  106. u32 tb_id;
  107. struct list_head me_list; /* entry in vrf_map_elem */
  108. int ifindex;
  109. };
  110. static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
  111. {
  112. vrf_dev->stats.tx_errors++;
  113. kfree_skb(skb);
  114. }
  115. static struct vrf_map *netns_vrf_map(struct net *net)
  116. {
  117. struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id);
  118. return &nn_vrf->vmap;
  119. }
  120. static struct vrf_map *netns_vrf_map_by_dev(struct net_device *dev)
  121. {
  122. return netns_vrf_map(dev_net(dev));
  123. }
  124. static int vrf_map_elem_get_vrf_ifindex(struct vrf_map_elem *me)
  125. {
  126. struct list_head *me_head = &me->vrf_list;
  127. struct net_vrf *vrf;
  128. if (list_empty(me_head))
  129. return -ENODEV;
  130. vrf = list_first_entry(me_head, struct net_vrf, me_list);
  131. return vrf->ifindex;
  132. }
  133. static struct vrf_map_elem *vrf_map_elem_alloc(gfp_t flags)
  134. {
  135. struct vrf_map_elem *me;
  136. me = kmalloc_obj(*me, flags);
  137. if (!me)
  138. return NULL;
  139. return me;
  140. }
  141. static void vrf_map_elem_free(struct vrf_map_elem *me)
  142. {
  143. kfree(me);
  144. }
  145. static void vrf_map_elem_init(struct vrf_map_elem *me, int table_id,
  146. int ifindex, int users)
  147. {
  148. me->table_id = table_id;
  149. me->ifindex = ifindex;
  150. me->users = users;
  151. INIT_LIST_HEAD(&me->vrf_list);
  152. }
  153. static struct vrf_map_elem *vrf_map_lookup_elem(struct vrf_map *vmap,
  154. u32 table_id)
  155. {
  156. struct vrf_map_elem *me;
  157. u32 key;
  158. key = jhash_1word(table_id, HASH_INITVAL);
  159. hash_for_each_possible(vmap->ht, me, hnode, key) {
  160. if (me->table_id == table_id)
  161. return me;
  162. }
  163. return NULL;
  164. }
  165. static void vrf_map_add_elem(struct vrf_map *vmap, struct vrf_map_elem *me)
  166. {
  167. u32 table_id = me->table_id;
  168. u32 key;
  169. key = jhash_1word(table_id, HASH_INITVAL);
  170. hash_add(vmap->ht, &me->hnode, key);
  171. }
  172. static void vrf_map_del_elem(struct vrf_map_elem *me)
  173. {
  174. hash_del(&me->hnode);
  175. }
  176. static void vrf_map_lock(struct vrf_map *vmap) __acquires(&vmap->vmap_lock)
  177. {
  178. spin_lock(&vmap->vmap_lock);
  179. }
  180. static void vrf_map_unlock(struct vrf_map *vmap) __releases(&vmap->vmap_lock)
  181. {
  182. spin_unlock(&vmap->vmap_lock);
  183. }
  184. /* called with rtnl lock held */
  185. static int
  186. vrf_map_register_dev(struct net_device *dev, struct netlink_ext_ack *extack)
  187. {
  188. struct vrf_map *vmap = netns_vrf_map_by_dev(dev);
  189. struct net_vrf *vrf = netdev_priv(dev);
  190. struct vrf_map_elem *new_me, *me;
  191. u32 table_id = vrf->tb_id;
  192. bool free_new_me = false;
  193. int users;
  194. int res;
  195. /* we pre-allocate elements used in the spin-locked section (so that we
  196. * keep the spinlock as short as possible).
  197. */
  198. new_me = vrf_map_elem_alloc(GFP_KERNEL);
  199. if (!new_me)
  200. return -ENOMEM;
  201. vrf_map_elem_init(new_me, table_id, dev->ifindex, 0);
  202. vrf_map_lock(vmap);
  203. me = vrf_map_lookup_elem(vmap, table_id);
  204. if (!me) {
  205. me = new_me;
  206. vrf_map_add_elem(vmap, me);
  207. goto link_vrf;
  208. }
  209. /* we already have an entry in the vrf_map, so it means there is (at
  210. * least) a vrf registered on the specific table.
  211. */
  212. free_new_me = true;
  213. if (vmap->strict_mode) {
  214. /* vrfs cannot share the same table */
  215. NL_SET_ERR_MSG(extack, "Table is used by another VRF");
  216. res = -EBUSY;
  217. goto unlock;
  218. }
  219. link_vrf:
  220. users = ++me->users;
  221. if (users == 2)
  222. ++vmap->shared_tables;
  223. list_add(&vrf->me_list, &me->vrf_list);
  224. res = 0;
  225. unlock:
  226. vrf_map_unlock(vmap);
  227. /* clean-up, if needed */
  228. if (free_new_me)
  229. vrf_map_elem_free(new_me);
  230. return res;
  231. }
  232. /* called with rtnl lock held */
  233. static void vrf_map_unregister_dev(struct net_device *dev)
  234. {
  235. struct vrf_map *vmap = netns_vrf_map_by_dev(dev);
  236. struct net_vrf *vrf = netdev_priv(dev);
  237. u32 table_id = vrf->tb_id;
  238. struct vrf_map_elem *me;
  239. int users;
  240. vrf_map_lock(vmap);
  241. me = vrf_map_lookup_elem(vmap, table_id);
  242. if (!me)
  243. goto unlock;
  244. list_del(&vrf->me_list);
  245. users = --me->users;
  246. if (users == 1) {
  247. --vmap->shared_tables;
  248. } else if (users == 0) {
  249. vrf_map_del_elem(me);
  250. /* no one will refer to this element anymore */
  251. vrf_map_elem_free(me);
  252. }
  253. unlock:
  254. vrf_map_unlock(vmap);
  255. }
  256. /* return the vrf device index associated with the table_id */
  257. static int vrf_ifindex_lookup_by_table_id(struct net *net, u32 table_id)
  258. {
  259. struct vrf_map *vmap = netns_vrf_map(net);
  260. struct vrf_map_elem *me;
  261. int ifindex;
  262. vrf_map_lock(vmap);
  263. if (!vmap->strict_mode) {
  264. ifindex = -EPERM;
  265. goto unlock;
  266. }
  267. me = vrf_map_lookup_elem(vmap, table_id);
  268. if (!me) {
  269. ifindex = -ENODEV;
  270. goto unlock;
  271. }
  272. ifindex = vrf_map_elem_get_vrf_ifindex(me);
  273. unlock:
  274. vrf_map_unlock(vmap);
  275. return ifindex;
  276. }
  277. /* by default VRF devices do not have a qdisc and are expected
  278. * to be created with only a single queue.
  279. */
  280. static bool qdisc_tx_is_default(const struct net_device *dev)
  281. {
  282. struct netdev_queue *txq;
  283. if (dev->num_tx_queues > 1)
  284. return false;
  285. txq = netdev_get_tx_queue(dev, 0);
  286. return qdisc_txq_has_no_queue(txq);
  287. }
  288. /* Local traffic destined to local address. Reinsert the packet to rx
  289. * path, similar to loopback handling.
  290. */
  291. static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
  292. struct dst_entry *dst)
  293. {
  294. unsigned int len = skb->len;
  295. skb_orphan(skb);
  296. skb_dst_set(skb, dst);
  297. /* set pkt_type to avoid skb hitting packet taps twice -
  298. * once on Tx and again in Rx processing
  299. */
  300. skb->pkt_type = PACKET_LOOPBACK;
  301. skb->protocol = eth_type_trans(skb, dev);
  302. if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
  303. dev_dstats_rx_add(dev, len);
  304. else
  305. dev_dstats_rx_dropped(dev);
  306. return NETDEV_TX_OK;
  307. }
  308. static void vrf_nf_set_untracked(struct sk_buff *skb)
  309. {
  310. if (skb_get_nfct(skb) == 0)
  311. nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
  312. }
  313. static void vrf_nf_reset_ct(struct sk_buff *skb)
  314. {
  315. if (skb_get_nfct(skb) == IP_CT_UNTRACKED)
  316. nf_reset_ct(skb);
  317. }
  318. #if IS_ENABLED(CONFIG_IPV6)
  319. static int vrf_ip6_local_out(struct net *net, struct sock *sk,
  320. struct sk_buff *skb)
  321. {
  322. int err;
  323. vrf_nf_reset_ct(skb);
  324. err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net,
  325. sk, skb, NULL, skb_dst(skb)->dev, dst_output);
  326. if (likely(err == 1))
  327. err = dst_output(net, sk, skb);
  328. return err;
  329. }
  330. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  331. struct net_device *dev)
  332. {
  333. const struct ipv6hdr *iph;
  334. struct net *net = dev_net(skb->dev);
  335. struct flowi6 fl6;
  336. int ret = NET_XMIT_DROP;
  337. struct dst_entry *dst;
  338. struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
  339. if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
  340. goto err;
  341. iph = ipv6_hdr(skb);
  342. memset(&fl6, 0, sizeof(fl6));
  343. /* needed to match OIF rule */
  344. fl6.flowi6_l3mdev = dev->ifindex;
  345. fl6.flowi6_iif = LOOPBACK_IFINDEX;
  346. fl6.daddr = iph->daddr;
  347. fl6.saddr = iph->saddr;
  348. fl6.flowlabel = ip6_flowinfo(iph);
  349. fl6.flowi6_mark = skb->mark;
  350. fl6.flowi6_proto = iph->nexthdr;
  351. dst = ip6_dst_lookup_flow(net, NULL, &fl6, NULL);
  352. if (IS_ERR(dst) || dst == dst_null)
  353. goto err;
  354. skb_dst_drop(skb);
  355. /* if dst.dev is the VRF device again this is locally originated traffic
  356. * destined to a local address. Short circuit to Rx path.
  357. */
  358. if (dst->dev == dev)
  359. return vrf_local_xmit(skb, dev, dst);
  360. skb_dst_set(skb, dst);
  361. /* strip the ethernet header added for pass through VRF device */
  362. __skb_pull(skb, skb_network_offset(skb));
  363. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  364. ret = vrf_ip6_local_out(net, skb->sk, skb);
  365. if (unlikely(net_xmit_eval(ret)))
  366. dev->stats.tx_errors++;
  367. else
  368. ret = NET_XMIT_SUCCESS;
  369. return ret;
  370. err:
  371. vrf_tx_error(dev, skb);
  372. return NET_XMIT_DROP;
  373. }
  374. #else
  375. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  376. struct net_device *dev)
  377. {
  378. vrf_tx_error(dev, skb);
  379. return NET_XMIT_DROP;
  380. }
  381. #endif
  382. /* based on ip_local_out; can't use it b/c the dst is switched pointing to us */
  383. static int vrf_ip_local_out(struct net *net, struct sock *sk,
  384. struct sk_buff *skb)
  385. {
  386. int err;
  387. vrf_nf_reset_ct(skb);
  388. err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
  389. skb, NULL, skb_dst(skb)->dev, dst_output);
  390. if (likely(err == 1))
  391. err = dst_output(net, sk, skb);
  392. return err;
  393. }
  394. static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
  395. struct net_device *vrf_dev)
  396. {
  397. struct iphdr *ip4h;
  398. int ret = NET_XMIT_DROP;
  399. struct flowi4 fl4;
  400. struct net *net = dev_net(vrf_dev);
  401. struct rtable *rt;
  402. if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
  403. goto err;
  404. ip4h = ip_hdr(skb);
  405. memset(&fl4, 0, sizeof(fl4));
  406. /* needed to match OIF rule */
  407. fl4.flowi4_l3mdev = vrf_dev->ifindex;
  408. fl4.flowi4_iif = LOOPBACK_IFINDEX;
  409. fl4.flowi4_dscp = ip4h_dscp(ip4h);
  410. fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
  411. fl4.flowi4_proto = ip4h->protocol;
  412. fl4.daddr = ip4h->daddr;
  413. fl4.saddr = ip4h->saddr;
  414. rt = ip_route_output_flow(net, &fl4, NULL);
  415. if (IS_ERR(rt))
  416. goto err;
  417. skb_dst_drop(skb);
  418. /* if dst.dev is the VRF device again this is locally originated traffic
  419. * destined to a local address. Short circuit to Rx path.
  420. */
  421. if (rt->dst.dev == vrf_dev)
  422. return vrf_local_xmit(skb, vrf_dev, &rt->dst);
  423. skb_dst_set(skb, &rt->dst);
  424. /* strip the ethernet header added for pass through VRF device */
  425. __skb_pull(skb, skb_network_offset(skb));
  426. if (!ip4h->saddr) {
  427. ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
  428. RT_SCOPE_LINK);
  429. }
  430. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  431. ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  432. if (unlikely(net_xmit_eval(ret)))
  433. vrf_dev->stats.tx_errors++;
  434. else
  435. ret = NET_XMIT_SUCCESS;
  436. out:
  437. return ret;
  438. err:
  439. vrf_tx_error(vrf_dev, skb);
  440. goto out;
  441. }
  442. static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
  443. {
  444. switch (skb->protocol) {
  445. case htons(ETH_P_IP):
  446. return vrf_process_v4_outbound(skb, dev);
  447. case htons(ETH_P_IPV6):
  448. return vrf_process_v6_outbound(skb, dev);
  449. default:
  450. vrf_tx_error(dev, skb);
  451. return NET_XMIT_DROP;
  452. }
  453. }
  454. static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
  455. {
  456. unsigned int len = skb->len;
  457. netdev_tx_t ret;
  458. ret = is_ip_tx_frame(skb, dev);
  459. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN))
  460. dev_dstats_tx_add(dev, len);
  461. else
  462. dev_dstats_tx_dropped(dev);
  463. return ret;
  464. }
  465. static void vrf_finish_direct(struct sk_buff *skb)
  466. {
  467. struct net_device *vrf_dev = skb->dev;
  468. if (!list_empty(&vrf_dev->ptype_all) &&
  469. likely(skb_headroom(skb) >= ETH_HLEN)) {
  470. struct ethhdr *eth = skb_push(skb, ETH_HLEN);
  471. ether_addr_copy(eth->h_source, vrf_dev->dev_addr);
  472. eth_zero_addr(eth->h_dest);
  473. eth->h_proto = skb->protocol;
  474. rcu_read_lock_bh();
  475. dev_queue_xmit_nit(skb, vrf_dev);
  476. rcu_read_unlock_bh();
  477. skb_pull(skb, ETH_HLEN);
  478. }
  479. vrf_nf_reset_ct(skb);
  480. }
  481. #if IS_ENABLED(CONFIG_IPV6)
  482. /* modelled after ip6_finish_output2 */
  483. static int vrf_finish_output6(struct net *net, struct sock *sk,
  484. struct sk_buff *skb)
  485. {
  486. struct dst_entry *dst = skb_dst(skb);
  487. struct net_device *dev = dst->dev;
  488. const struct in6_addr *nexthop;
  489. struct neighbour *neigh;
  490. int ret;
  491. vrf_nf_reset_ct(skb);
  492. skb->protocol = htons(ETH_P_IPV6);
  493. skb->dev = dev;
  494. rcu_read_lock();
  495. nexthop = rt6_nexthop(dst_rt6_info(dst), &ipv6_hdr(skb)->daddr);
  496. neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
  497. if (unlikely(!neigh))
  498. neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
  499. if (!IS_ERR(neigh)) {
  500. sock_confirm_neigh(skb, neigh);
  501. ret = neigh_output(neigh, skb, false);
  502. rcu_read_unlock();
  503. return ret;
  504. }
  505. rcu_read_unlock();
  506. IP6_INC_STATS(dev_net(dst->dev),
  507. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  508. kfree_skb(skb);
  509. return -EINVAL;
  510. }
  511. /* modelled after ip6_output */
  512. static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
  513. {
  514. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  515. net, sk, skb, NULL, skb_dst(skb)->dev,
  516. vrf_finish_output6,
  517. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  518. }
  519. /* set dst on skb to send packet to us via dev_xmit path. Allows
  520. * packet to go through device based features such as qdisc, netfilter
  521. * hooks and packet sockets with skb->dev set to vrf device.
  522. */
  523. static struct sk_buff *vrf_ip6_out_redirect(struct net_device *vrf_dev,
  524. struct sk_buff *skb)
  525. {
  526. struct net_vrf *vrf = netdev_priv(vrf_dev);
  527. struct dst_entry *dst = NULL;
  528. struct rt6_info *rt6;
  529. rcu_read_lock();
  530. rt6 = rcu_dereference(vrf->rt6);
  531. if (likely(rt6)) {
  532. dst = &rt6->dst;
  533. dst_hold(dst);
  534. }
  535. rcu_read_unlock();
  536. if (unlikely(!dst)) {
  537. vrf_tx_error(vrf_dev, skb);
  538. return NULL;
  539. }
  540. skb_dst_drop(skb);
  541. skb_dst_set(skb, dst);
  542. return skb;
  543. }
  544. static int vrf_output6_direct_finish(struct net *net, struct sock *sk,
  545. struct sk_buff *skb)
  546. {
  547. vrf_finish_direct(skb);
  548. return vrf_ip6_local_out(net, sk, skb);
  549. }
  550. static int vrf_output6_direct(struct net *net, struct sock *sk,
  551. struct sk_buff *skb)
  552. {
  553. int err = 1;
  554. skb->protocol = htons(ETH_P_IPV6);
  555. if (!(IPCB(skb)->flags & IPSKB_REROUTED))
  556. err = nf_hook(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb,
  557. NULL, skb->dev, vrf_output6_direct_finish);
  558. if (likely(err == 1))
  559. vrf_finish_direct(skb);
  560. return err;
  561. }
  562. static int vrf_ip6_out_direct_finish(struct net *net, struct sock *sk,
  563. struct sk_buff *skb)
  564. {
  565. int err;
  566. err = vrf_output6_direct(net, sk, skb);
  567. if (likely(err == 1))
  568. err = vrf_ip6_local_out(net, sk, skb);
  569. return err;
  570. }
  571. static struct sk_buff *vrf_ip6_out_direct(struct net_device *vrf_dev,
  572. struct sock *sk,
  573. struct sk_buff *skb)
  574. {
  575. struct net *net = dev_net(vrf_dev);
  576. int err;
  577. skb->dev = vrf_dev;
  578. err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk,
  579. skb, NULL, vrf_dev, vrf_ip6_out_direct_finish);
  580. if (likely(err == 1))
  581. err = vrf_output6_direct(net, sk, skb);
  582. if (likely(err == 1))
  583. return skb;
  584. return NULL;
  585. }
  586. static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
  587. struct sock *sk,
  588. struct sk_buff *skb)
  589. {
  590. /* don't divert link scope packets */
  591. if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
  592. return skb;
  593. vrf_nf_set_untracked(skb);
  594. if (qdisc_tx_is_default(vrf_dev) ||
  595. IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
  596. return vrf_ip6_out_direct(vrf_dev, sk, skb);
  597. return vrf_ip6_out_redirect(vrf_dev, skb);
  598. }
  599. /* holding rtnl */
  600. static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
  601. {
  602. struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
  603. struct net *net = dev_net(dev);
  604. struct dst_entry *dst;
  605. RCU_INIT_POINTER(vrf->rt6, NULL);
  606. synchronize_rcu();
  607. /* move dev in dst's to loopback so this VRF device can be deleted
  608. * - based on dst_ifdown
  609. */
  610. if (rt6) {
  611. dst = &rt6->dst;
  612. netdev_ref_replace(dst->dev, net->loopback_dev,
  613. &dst->dev_tracker, GFP_KERNEL);
  614. dst->dev = net->loopback_dev;
  615. dst_release(dst);
  616. }
  617. }
  618. static int vrf_rt6_create(struct net_device *dev)
  619. {
  620. int flags = DST_NOPOLICY | DST_NOXFRM;
  621. struct net_vrf *vrf = netdev_priv(dev);
  622. struct net *net = dev_net(dev);
  623. struct rt6_info *rt6;
  624. int rc = -ENOMEM;
  625. /* IPv6 can be CONFIG enabled and then disabled runtime */
  626. if (!ipv6_mod_enabled())
  627. return 0;
  628. vrf->fib6_table = fib6_new_table(net, vrf->tb_id);
  629. if (!vrf->fib6_table)
  630. goto out;
  631. /* create a dst for routing packets out a VRF device */
  632. rt6 = ip6_dst_alloc(net, dev, flags);
  633. if (!rt6)
  634. goto out;
  635. rt6->dst.output = vrf_output6;
  636. rcu_assign_pointer(vrf->rt6, rt6);
  637. rc = 0;
  638. out:
  639. return rc;
  640. }
  641. #else
  642. static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
  643. struct sock *sk,
  644. struct sk_buff *skb)
  645. {
  646. return skb;
  647. }
  648. static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
  649. {
  650. }
  651. static int vrf_rt6_create(struct net_device *dev)
  652. {
  653. return 0;
  654. }
  655. #endif
  656. /* modelled after ip_finish_output2 */
  657. static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  658. {
  659. struct dst_entry *dst = skb_dst(skb);
  660. struct rtable *rt = dst_rtable(dst);
  661. struct net_device *dev = dst->dev;
  662. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  663. struct neighbour *neigh;
  664. bool is_v6gw = false;
  665. vrf_nf_reset_ct(skb);
  666. /* Be paranoid, rather than too clever. */
  667. if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
  668. skb = skb_expand_head(skb, hh_len);
  669. if (!skb) {
  670. dev->stats.tx_errors++;
  671. return -ENOMEM;
  672. }
  673. }
  674. rcu_read_lock();
  675. neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
  676. if (!IS_ERR(neigh)) {
  677. int ret;
  678. sock_confirm_neigh(skb, neigh);
  679. /* if crossing protocols, can not use the cached header */
  680. ret = neigh_output(neigh, skb, is_v6gw);
  681. rcu_read_unlock();
  682. return ret;
  683. }
  684. rcu_read_unlock();
  685. vrf_tx_error(skb->dev, skb);
  686. return -EINVAL;
  687. }
  688. static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  689. {
  690. struct net_device *dev = skb_dst(skb)->dev;
  691. IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
  692. skb->dev = dev;
  693. skb->protocol = htons(ETH_P_IP);
  694. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  695. net, sk, skb, NULL, dev,
  696. vrf_finish_output,
  697. !(IPCB(skb)->flags & IPSKB_REROUTED));
  698. }
  699. /* set dst on skb to send packet to us via dev_xmit path. Allows
  700. * packet to go through device based features such as qdisc, netfilter
  701. * hooks and packet sockets with skb->dev set to vrf device.
  702. */
  703. static struct sk_buff *vrf_ip_out_redirect(struct net_device *vrf_dev,
  704. struct sk_buff *skb)
  705. {
  706. struct net_vrf *vrf = netdev_priv(vrf_dev);
  707. struct dst_entry *dst = NULL;
  708. struct rtable *rth;
  709. rcu_read_lock();
  710. rth = rcu_dereference(vrf->rth);
  711. if (likely(rth)) {
  712. dst = &rth->dst;
  713. dst_hold(dst);
  714. }
  715. rcu_read_unlock();
  716. if (unlikely(!dst)) {
  717. vrf_tx_error(vrf_dev, skb);
  718. return NULL;
  719. }
  720. skb_dst_drop(skb);
  721. skb_dst_set(skb, dst);
  722. return skb;
  723. }
  724. static int vrf_output_direct_finish(struct net *net, struct sock *sk,
  725. struct sk_buff *skb)
  726. {
  727. vrf_finish_direct(skb);
  728. return vrf_ip_local_out(net, sk, skb);
  729. }
  730. static int vrf_output_direct(struct net *net, struct sock *sk,
  731. struct sk_buff *skb)
  732. {
  733. int err = 1;
  734. skb->protocol = htons(ETH_P_IP);
  735. if (!(IPCB(skb)->flags & IPSKB_REROUTED))
  736. err = nf_hook(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb,
  737. NULL, skb->dev, vrf_output_direct_finish);
  738. if (likely(err == 1))
  739. vrf_finish_direct(skb);
  740. return err;
  741. }
  742. static int vrf_ip_out_direct_finish(struct net *net, struct sock *sk,
  743. struct sk_buff *skb)
  744. {
  745. int err;
  746. err = vrf_output_direct(net, sk, skb);
  747. if (likely(err == 1))
  748. err = vrf_ip_local_out(net, sk, skb);
  749. return err;
  750. }
  751. static struct sk_buff *vrf_ip_out_direct(struct net_device *vrf_dev,
  752. struct sock *sk,
  753. struct sk_buff *skb)
  754. {
  755. struct net *net = dev_net(vrf_dev);
  756. int err;
  757. skb->dev = vrf_dev;
  758. err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
  759. skb, NULL, vrf_dev, vrf_ip_out_direct_finish);
  760. if (likely(err == 1))
  761. err = vrf_output_direct(net, sk, skb);
  762. if (likely(err == 1))
  763. return skb;
  764. return NULL;
  765. }
  766. static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
  767. struct sock *sk,
  768. struct sk_buff *skb)
  769. {
  770. /* don't divert multicast or local broadcast */
  771. if (ipv4_is_multicast(ip_hdr(skb)->daddr) ||
  772. ipv4_is_lbcast(ip_hdr(skb)->daddr))
  773. return skb;
  774. vrf_nf_set_untracked(skb);
  775. if (qdisc_tx_is_default(vrf_dev) ||
  776. IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
  777. return vrf_ip_out_direct(vrf_dev, sk, skb);
  778. return vrf_ip_out_redirect(vrf_dev, skb);
  779. }
  780. /* called with rcu lock held */
  781. static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
  782. struct sock *sk,
  783. struct sk_buff *skb,
  784. u16 proto)
  785. {
  786. switch (proto) {
  787. case AF_INET:
  788. return vrf_ip_out(vrf_dev, sk, skb);
  789. case AF_INET6:
  790. return vrf_ip6_out(vrf_dev, sk, skb);
  791. }
  792. return skb;
  793. }
  794. /* holding rtnl */
  795. static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
  796. {
  797. struct rtable *rth = rtnl_dereference(vrf->rth);
  798. struct net *net = dev_net(dev);
  799. struct dst_entry *dst;
  800. RCU_INIT_POINTER(vrf->rth, NULL);
  801. synchronize_rcu();
  802. /* move dev in dst's to loopback so this VRF device can be deleted
  803. * - based on dst_ifdown
  804. */
  805. if (rth) {
  806. dst = &rth->dst;
  807. netdev_ref_replace(dst->dev, net->loopback_dev,
  808. &dst->dev_tracker, GFP_KERNEL);
  809. dst->dev = net->loopback_dev;
  810. dst_release(dst);
  811. }
  812. }
  813. static int vrf_rtable_create(struct net_device *dev)
  814. {
  815. struct net_vrf *vrf = netdev_priv(dev);
  816. struct rtable *rth;
  817. if (!fib_new_table(dev_net(dev), vrf->tb_id))
  818. return -ENOMEM;
  819. /* create a dst for routing packets out through a VRF device */
  820. rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1);
  821. if (!rth)
  822. return -ENOMEM;
  823. rth->dst.output = vrf_output;
  824. rcu_assign_pointer(vrf->rth, rth);
  825. return 0;
  826. }
  827. /**************************** device handling ********************/
  828. /* cycle interface to flush neighbor cache and move routes across tables */
  829. static void cycle_netdev(struct net_device *dev,
  830. struct netlink_ext_ack *extack)
  831. {
  832. unsigned int flags = dev->flags;
  833. int ret;
  834. if (!netif_running(dev))
  835. return;
  836. ret = dev_change_flags(dev, flags & ~IFF_UP, extack);
  837. if (ret >= 0)
  838. ret = dev_change_flags(dev, flags, extack);
  839. if (ret < 0) {
  840. netdev_err(dev,
  841. "Failed to cycle device %s; route tables might be wrong!\n",
  842. dev->name);
  843. }
  844. }
  845. static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev,
  846. struct netlink_ext_ack *extack)
  847. {
  848. int ret;
  849. /* do not allow loopback device to be enslaved to a VRF.
  850. * The vrf device acts as the loopback for the vrf.
  851. */
  852. if (port_dev == dev_net(dev)->loopback_dev) {
  853. NL_SET_ERR_MSG(extack,
  854. "Can not enslave loopback device to a VRF");
  855. return -EOPNOTSUPP;
  856. }
  857. port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
  858. ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL, extack);
  859. if (ret < 0)
  860. goto err;
  861. cycle_netdev(port_dev, extack);
  862. return 0;
  863. err:
  864. port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
  865. return ret;
  866. }
  867. static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev,
  868. struct netlink_ext_ack *extack)
  869. {
  870. if (netif_is_l3_master(port_dev)) {
  871. NL_SET_ERR_MSG(extack,
  872. "Can not enslave an L3 master device to a VRF");
  873. return -EINVAL;
  874. }
  875. if (netif_is_l3_slave(port_dev))
  876. return -EINVAL;
  877. return do_vrf_add_slave(dev, port_dev, extack);
  878. }
  879. /* inverse of do_vrf_add_slave */
  880. static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  881. {
  882. netdev_upper_dev_unlink(port_dev, dev);
  883. port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
  884. cycle_netdev(port_dev, NULL);
  885. return 0;
  886. }
  887. static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  888. {
  889. return do_vrf_del_slave(dev, port_dev);
  890. }
  891. static void vrf_dev_uninit(struct net_device *dev)
  892. {
  893. struct net_vrf *vrf = netdev_priv(dev);
  894. vrf_rtable_release(dev, vrf);
  895. vrf_rt6_release(dev, vrf);
  896. }
  897. static int vrf_dev_init(struct net_device *dev)
  898. {
  899. struct net_vrf *vrf = netdev_priv(dev);
  900. /* create the default dst which points back to us */
  901. if (vrf_rtable_create(dev) != 0)
  902. goto out_nomem;
  903. if (vrf_rt6_create(dev) != 0)
  904. goto out_rth;
  905. dev->flags = IFF_MASTER | IFF_NOARP;
  906. /* similarly, oper state is irrelevant; set to up to avoid confusion */
  907. dev->operstate = IF_OPER_UP;
  908. netdev_lockdep_set_classes(dev);
  909. return 0;
  910. out_rth:
  911. vrf_rtable_release(dev, vrf);
  912. out_nomem:
  913. return -ENOMEM;
  914. }
  915. static const struct net_device_ops vrf_netdev_ops = {
  916. .ndo_init = vrf_dev_init,
  917. .ndo_uninit = vrf_dev_uninit,
  918. .ndo_start_xmit = vrf_xmit,
  919. .ndo_set_mac_address = eth_mac_addr,
  920. .ndo_add_slave = vrf_add_slave,
  921. .ndo_del_slave = vrf_del_slave,
  922. };
  923. static u32 vrf_fib_table(const struct net_device *dev)
  924. {
  925. struct net_vrf *vrf = netdev_priv(dev);
  926. return vrf->tb_id;
  927. }
  928. static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  929. {
  930. kfree_skb(skb);
  931. return 0;
  932. }
  933. static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook,
  934. struct sk_buff *skb,
  935. struct net_device *dev)
  936. {
  937. struct net *net = dev_net(dev);
  938. if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1)
  939. skb = NULL; /* kfree_skb(skb) handled by nf code */
  940. return skb;
  941. }
  942. static int vrf_prepare_mac_header(struct sk_buff *skb,
  943. struct net_device *vrf_dev, u16 proto)
  944. {
  945. struct ethhdr *eth;
  946. int err;
  947. /* in general, we do not know if there is enough space in the head of
  948. * the packet for hosting the mac header.
  949. */
  950. err = skb_cow_head(skb, LL_RESERVED_SPACE(vrf_dev));
  951. if (unlikely(err))
  952. /* no space in the skb head */
  953. return -ENOBUFS;
  954. __skb_push(skb, ETH_HLEN);
  955. eth = (struct ethhdr *)skb->data;
  956. skb_reset_mac_header(skb);
  957. skb_reset_mac_len(skb);
  958. /* we set the ethernet destination and the source addresses to the
  959. * address of the VRF device.
  960. */
  961. ether_addr_copy(eth->h_dest, vrf_dev->dev_addr);
  962. ether_addr_copy(eth->h_source, vrf_dev->dev_addr);
  963. eth->h_proto = htons(proto);
  964. /* the destination address of the Ethernet frame corresponds to the
  965. * address set on the VRF interface; therefore, the packet is intended
  966. * to be processed locally.
  967. */
  968. skb->protocol = eth->h_proto;
  969. skb->pkt_type = PACKET_HOST;
  970. skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
  971. skb_pull_inline(skb, ETH_HLEN);
  972. return 0;
  973. }
  974. /* prepare and add the mac header to the packet if it was not set previously.
  975. * In this way, packet sniffers such as tcpdump can parse the packet correctly.
  976. * If the mac header was already set, the original mac header is left
  977. * untouched and the function returns immediately.
  978. */
  979. static int vrf_add_mac_header_if_unset(struct sk_buff *skb,
  980. struct net_device *vrf_dev,
  981. u16 proto, struct net_device *orig_dev)
  982. {
  983. if (skb_mac_header_was_set(skb) && dev_has_header(orig_dev))
  984. return 0;
  985. return vrf_prepare_mac_header(skb, vrf_dev, proto);
  986. }
  987. #if IS_ENABLED(CONFIG_IPV6)
  988. /* neighbor handling is done with actual device; do not want
  989. * to flip skb->dev for those ndisc packets. This really fails
  990. * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
  991. * a start.
  992. */
  993. static bool ipv6_ndisc_frame(const struct sk_buff *skb)
  994. {
  995. const struct ipv6hdr *iph = ipv6_hdr(skb);
  996. bool rc = false;
  997. if (iph->nexthdr == NEXTHDR_ICMP) {
  998. const struct icmp6hdr *icmph;
  999. struct icmp6hdr _icmph;
  1000. icmph = skb_header_pointer(skb, sizeof(*iph),
  1001. sizeof(_icmph), &_icmph);
  1002. if (!icmph)
  1003. goto out;
  1004. switch (icmph->icmp6_type) {
  1005. case NDISC_ROUTER_SOLICITATION:
  1006. case NDISC_ROUTER_ADVERTISEMENT:
  1007. case NDISC_NEIGHBOUR_SOLICITATION:
  1008. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  1009. case NDISC_REDIRECT:
  1010. rc = true;
  1011. break;
  1012. }
  1013. }
  1014. out:
  1015. return rc;
  1016. }
  1017. static struct rt6_info *vrf_ip6_route_lookup(struct net *net,
  1018. const struct net_device *dev,
  1019. struct flowi6 *fl6,
  1020. int ifindex,
  1021. const struct sk_buff *skb,
  1022. int flags)
  1023. {
  1024. struct net_vrf *vrf = netdev_priv(dev);
  1025. return ip6_pol_route(net, vrf->fib6_table, ifindex, fl6, skb, flags);
  1026. }
  1027. static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev,
  1028. int ifindex)
  1029. {
  1030. const struct ipv6hdr *iph = ipv6_hdr(skb);
  1031. struct flowi6 fl6 = {
  1032. .flowi6_iif = ifindex,
  1033. .flowi6_mark = skb->mark,
  1034. .flowi6_proto = iph->nexthdr,
  1035. .daddr = iph->daddr,
  1036. .saddr = iph->saddr,
  1037. .flowlabel = ip6_flowinfo(iph),
  1038. };
  1039. struct net *net = dev_net(vrf_dev);
  1040. struct rt6_info *rt6;
  1041. skb_dst_drop(skb);
  1042. rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex, skb,
  1043. RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE);
  1044. if (unlikely(!rt6))
  1045. return;
  1046. if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst))
  1047. return;
  1048. skb_dst_set(skb, &rt6->dst);
  1049. }
  1050. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  1051. struct sk_buff *skb)
  1052. {
  1053. int orig_iif = skb->skb_iif;
  1054. bool need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr);
  1055. bool is_ndisc = ipv6_ndisc_frame(skb);
  1056. /* loopback, multicast & non-ND link-local traffic; do not push through
  1057. * packet taps again. Reset pkt_type for upper layers to process skb.
  1058. * For non-loopback strict packets, determine the dst using the original
  1059. * ifindex.
  1060. */
  1061. if (skb->pkt_type == PACKET_LOOPBACK || (need_strict && !is_ndisc)) {
  1062. skb->dev = vrf_dev;
  1063. skb->skb_iif = vrf_dev->ifindex;
  1064. IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
  1065. if (skb->pkt_type == PACKET_LOOPBACK)
  1066. skb->pkt_type = PACKET_HOST;
  1067. else
  1068. vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
  1069. goto out;
  1070. }
  1071. /* if packet is NDISC then keep the ingress interface */
  1072. if (!is_ndisc) {
  1073. struct net_device *orig_dev = skb->dev;
  1074. dev_dstats_rx_add(vrf_dev, skb->len);
  1075. skb->dev = vrf_dev;
  1076. skb->skb_iif = vrf_dev->ifindex;
  1077. if (!list_empty(&vrf_dev->ptype_all)) {
  1078. int err;
  1079. err = vrf_add_mac_header_if_unset(skb, vrf_dev,
  1080. ETH_P_IPV6,
  1081. orig_dev);
  1082. if (likely(!err)) {
  1083. skb_push(skb, skb->mac_len);
  1084. dev_queue_xmit_nit(skb, vrf_dev);
  1085. skb_pull(skb, skb->mac_len);
  1086. }
  1087. }
  1088. IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
  1089. }
  1090. if (need_strict)
  1091. vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
  1092. skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev);
  1093. out:
  1094. return skb;
  1095. }
  1096. #else
  1097. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  1098. struct sk_buff *skb)
  1099. {
  1100. return skb;
  1101. }
  1102. #endif
  1103. static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
  1104. struct sk_buff *skb)
  1105. {
  1106. struct net_device *orig_dev = skb->dev;
  1107. skb->dev = vrf_dev;
  1108. skb->skb_iif = vrf_dev->ifindex;
  1109. IPCB(skb)->flags |= IPSKB_L3SLAVE;
  1110. if (ipv4_is_multicast(ip_hdr(skb)->daddr))
  1111. goto out;
  1112. /* loopback traffic; do not push through packet taps again.
  1113. * Reset pkt_type for upper layers to process skb
  1114. */
  1115. if (skb->pkt_type == PACKET_LOOPBACK) {
  1116. skb->pkt_type = PACKET_HOST;
  1117. goto out;
  1118. }
  1119. dev_dstats_rx_add(vrf_dev, skb->len);
  1120. if (!list_empty(&vrf_dev->ptype_all)) {
  1121. int err;
  1122. err = vrf_add_mac_header_if_unset(skb, vrf_dev, ETH_P_IP,
  1123. orig_dev);
  1124. if (likely(!err)) {
  1125. skb_push(skb, skb->mac_len);
  1126. dev_queue_xmit_nit(skb, vrf_dev);
  1127. skb_pull(skb, skb->mac_len);
  1128. }
  1129. }
  1130. skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
  1131. out:
  1132. return skb;
  1133. }
  1134. /* called with rcu lock held */
  1135. static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
  1136. struct sk_buff *skb,
  1137. u16 proto)
  1138. {
  1139. switch (proto) {
  1140. case AF_INET:
  1141. return vrf_ip_rcv(vrf_dev, skb);
  1142. case AF_INET6:
  1143. return vrf_ip6_rcv(vrf_dev, skb);
  1144. }
  1145. return skb;
  1146. }
  1147. #if IS_ENABLED(CONFIG_IPV6)
  1148. /* send to link-local or multicast address via interface enslaved to
  1149. * VRF device. Force lookup to VRF table without changing flow struct
  1150. * Note: Caller to this function must hold rcu_read_lock() and no refcnt
  1151. * is taken on the dst by this function.
  1152. */
  1153. static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev,
  1154. struct flowi6 *fl6)
  1155. {
  1156. struct net *net = dev_net(dev);
  1157. int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_DST_NOREF;
  1158. struct dst_entry *dst = NULL;
  1159. struct rt6_info *rt;
  1160. /* VRF device does not have a link-local address and
  1161. * sending packets to link-local or mcast addresses over
  1162. * a VRF device does not make sense
  1163. */
  1164. if (fl6->flowi6_oif == dev->ifindex) {
  1165. dst = &net->ipv6.ip6_null_entry->dst;
  1166. return dst;
  1167. }
  1168. if (!ipv6_addr_any(&fl6->saddr))
  1169. flags |= RT6_LOOKUP_F_HAS_SADDR;
  1170. rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, NULL, flags);
  1171. if (rt)
  1172. dst = &rt->dst;
  1173. return dst;
  1174. }
  1175. #endif
  1176. static const struct l3mdev_ops vrf_l3mdev_ops = {
  1177. .l3mdev_fib_table = vrf_fib_table,
  1178. .l3mdev_l3_rcv = vrf_l3_rcv,
  1179. .l3mdev_l3_out = vrf_l3_out,
  1180. #if IS_ENABLED(CONFIG_IPV6)
  1181. .l3mdev_link_scope_lookup = vrf_link_scope_lookup,
  1182. #endif
  1183. };
  1184. static void vrf_get_drvinfo(struct net_device *dev,
  1185. struct ethtool_drvinfo *info)
  1186. {
  1187. strscpy(info->driver, DRV_NAME, sizeof(info->driver));
  1188. strscpy(info->version, DRV_VERSION, sizeof(info->version));
  1189. }
  1190. static const struct ethtool_ops vrf_ethtool_ops = {
  1191. .get_drvinfo = vrf_get_drvinfo,
  1192. };
  1193. static inline size_t vrf_fib_rule_nl_size(void)
  1194. {
  1195. size_t sz;
  1196. sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr));
  1197. sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */
  1198. sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */
  1199. sz += nla_total_size(sizeof(u8)); /* FRA_PROTOCOL */
  1200. return sz;
  1201. }
  1202. static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
  1203. {
  1204. struct fib_rule_hdr *frh;
  1205. struct nlmsghdr *nlh;
  1206. struct sk_buff *skb;
  1207. int err;
  1208. if ((family == AF_INET6 || family == RTNL_FAMILY_IP6MR) &&
  1209. !ipv6_mod_enabled())
  1210. return 0;
  1211. skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL);
  1212. if (!skb)
  1213. return -ENOMEM;
  1214. nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0);
  1215. if (!nlh)
  1216. goto nla_put_failure;
  1217. /* rule only needs to appear once */
  1218. nlh->nlmsg_flags |= NLM_F_EXCL;
  1219. frh = nlmsg_data(nlh);
  1220. memset(frh, 0, sizeof(*frh));
  1221. frh->family = family;
  1222. frh->action = FR_ACT_TO_TBL;
  1223. if (nla_put_u8(skb, FRA_PROTOCOL, RTPROT_KERNEL))
  1224. goto nla_put_failure;
  1225. if (nla_put_u8(skb, FRA_L3MDEV, 1))
  1226. goto nla_put_failure;
  1227. if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF))
  1228. goto nla_put_failure;
  1229. nlmsg_end(skb, nlh);
  1230. if (add_it) {
  1231. err = fib_newrule(dev_net(dev), skb, nlh, NULL, true);
  1232. if (err == -EEXIST)
  1233. err = 0;
  1234. } else {
  1235. err = fib_delrule(dev_net(dev), skb, nlh, NULL, true);
  1236. if (err == -ENOENT)
  1237. err = 0;
  1238. }
  1239. nlmsg_free(skb);
  1240. return err;
  1241. nla_put_failure:
  1242. nlmsg_free(skb);
  1243. return -EMSGSIZE;
  1244. }
  1245. static int vrf_add_fib_rules(const struct net_device *dev)
  1246. {
  1247. int err;
  1248. err = vrf_fib_rule(dev, AF_INET, true);
  1249. if (err < 0)
  1250. goto out_err;
  1251. err = vrf_fib_rule(dev, AF_INET6, true);
  1252. if (err < 0)
  1253. goto ipv6_err;
  1254. #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
  1255. err = vrf_fib_rule(dev, RTNL_FAMILY_IPMR, true);
  1256. if (err < 0)
  1257. goto ipmr_err;
  1258. #endif
  1259. #if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES)
  1260. err = vrf_fib_rule(dev, RTNL_FAMILY_IP6MR, true);
  1261. if (err < 0)
  1262. goto ip6mr_err;
  1263. #endif
  1264. return 0;
  1265. #if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES)
  1266. ip6mr_err:
  1267. vrf_fib_rule(dev, RTNL_FAMILY_IPMR, false);
  1268. #endif
  1269. #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
  1270. ipmr_err:
  1271. vrf_fib_rule(dev, AF_INET6, false);
  1272. #endif
  1273. ipv6_err:
  1274. vrf_fib_rule(dev, AF_INET, false);
  1275. out_err:
  1276. netdev_err(dev, "Failed to add FIB rules.\n");
  1277. return err;
  1278. }
  1279. static void vrf_setup(struct net_device *dev)
  1280. {
  1281. ether_setup(dev);
  1282. /* Initialize the device structure. */
  1283. dev->netdev_ops = &vrf_netdev_ops;
  1284. dev->l3mdev_ops = &vrf_l3mdev_ops;
  1285. dev->ethtool_ops = &vrf_ethtool_ops;
  1286. dev->needs_free_netdev = true;
  1287. /* Fill in device structure with ethernet-generic values. */
  1288. eth_hw_addr_random(dev);
  1289. /* don't acquire vrf device's netif_tx_lock when transmitting */
  1290. dev->lltx = true;
  1291. /* don't allow vrf devices to change network namespaces. */
  1292. dev->netns_immutable = true;
  1293. /* does not make sense for a VLAN to be added to a vrf device */
  1294. dev->features |= NETIF_F_VLAN_CHALLENGED;
  1295. /* enable offload features */
  1296. dev->features |= NETIF_F_GSO_SOFTWARE;
  1297. dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM | NETIF_F_SCTP_CRC;
  1298. dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
  1299. dev->hw_features = dev->features;
  1300. dev->hw_enc_features = dev->features;
  1301. /* default to no qdisc; user can add if desired */
  1302. dev->priv_flags |= IFF_NO_QUEUE;
  1303. dev->priv_flags |= IFF_NO_RX_HANDLER;
  1304. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1305. /* VRF devices do not care about MTU, but if the MTU is set
  1306. * too low then the ipv4 and ipv6 protocols are disabled
  1307. * which breaks networking.
  1308. */
  1309. dev->min_mtu = IPV6_MIN_MTU;
  1310. dev->max_mtu = IP6_MAX_MTU;
  1311. dev->mtu = dev->max_mtu;
  1312. dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
  1313. }
  1314. static int vrf_validate(struct nlattr *tb[], struct nlattr *data[],
  1315. struct netlink_ext_ack *extack)
  1316. {
  1317. if (tb[IFLA_ADDRESS]) {
  1318. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1319. NL_SET_ERR_MSG(extack, "Invalid hardware address");
  1320. return -EINVAL;
  1321. }
  1322. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1323. NL_SET_ERR_MSG(extack, "Invalid hardware address");
  1324. return -EADDRNOTAVAIL;
  1325. }
  1326. }
  1327. return 0;
  1328. }
  1329. static void vrf_dellink(struct net_device *dev, struct list_head *head)
  1330. {
  1331. struct net_device *port_dev;
  1332. struct list_head *iter;
  1333. netdev_for_each_lower_dev(dev, port_dev, iter)
  1334. vrf_del_slave(dev, port_dev);
  1335. vrf_map_unregister_dev(dev);
  1336. unregister_netdevice_queue(dev, head);
  1337. }
  1338. static int vrf_newlink(struct net_device *dev,
  1339. struct rtnl_newlink_params *params,
  1340. struct netlink_ext_ack *extack)
  1341. {
  1342. struct net_vrf *vrf = netdev_priv(dev);
  1343. struct nlattr **data = params->data;
  1344. struct netns_vrf *nn_vrf;
  1345. bool *add_fib_rules;
  1346. struct net *net;
  1347. int err;
  1348. if (!data || !data[IFLA_VRF_TABLE]) {
  1349. NL_SET_ERR_MSG(extack, "VRF table id is missing");
  1350. return -EINVAL;
  1351. }
  1352. vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
  1353. if (vrf->tb_id == RT_TABLE_UNSPEC) {
  1354. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VRF_TABLE],
  1355. "Invalid VRF table id");
  1356. return -EINVAL;
  1357. }
  1358. dev->priv_flags |= IFF_L3MDEV_MASTER;
  1359. err = register_netdevice(dev);
  1360. if (err)
  1361. goto out;
  1362. /* mapping between table_id and vrf;
  1363. * note: such binding could not be done in the dev init function
  1364. * because dev->ifindex id is not available yet.
  1365. */
  1366. vrf->ifindex = dev->ifindex;
  1367. err = vrf_map_register_dev(dev, extack);
  1368. if (err) {
  1369. unregister_netdevice(dev);
  1370. goto out;
  1371. }
  1372. net = dev_net(dev);
  1373. nn_vrf = net_generic(net, vrf_net_id);
  1374. add_fib_rules = &nn_vrf->add_fib_rules;
  1375. if (*add_fib_rules) {
  1376. err = vrf_add_fib_rules(dev);
  1377. if (err) {
  1378. vrf_map_unregister_dev(dev);
  1379. unregister_netdevice(dev);
  1380. goto out;
  1381. }
  1382. *add_fib_rules = false;
  1383. }
  1384. out:
  1385. return err;
  1386. }
  1387. static size_t vrf_nl_getsize(const struct net_device *dev)
  1388. {
  1389. return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
  1390. }
  1391. static int vrf_fillinfo(struct sk_buff *skb,
  1392. const struct net_device *dev)
  1393. {
  1394. struct net_vrf *vrf = netdev_priv(dev);
  1395. return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
  1396. }
  1397. static size_t vrf_get_slave_size(const struct net_device *bond_dev,
  1398. const struct net_device *slave_dev)
  1399. {
  1400. return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
  1401. }
  1402. static int vrf_fill_slave_info(struct sk_buff *skb,
  1403. const struct net_device *vrf_dev,
  1404. const struct net_device *slave_dev)
  1405. {
  1406. struct net_vrf *vrf = netdev_priv(vrf_dev);
  1407. if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
  1408. return -EMSGSIZE;
  1409. return 0;
  1410. }
  1411. static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
  1412. [IFLA_VRF_TABLE] = { .type = NLA_U32 },
  1413. };
  1414. static struct rtnl_link_ops vrf_link_ops __read_mostly = {
  1415. .kind = DRV_NAME,
  1416. .priv_size = sizeof(struct net_vrf),
  1417. .get_size = vrf_nl_getsize,
  1418. .policy = vrf_nl_policy,
  1419. .validate = vrf_validate,
  1420. .fill_info = vrf_fillinfo,
  1421. .get_slave_size = vrf_get_slave_size,
  1422. .fill_slave_info = vrf_fill_slave_info,
  1423. .newlink = vrf_newlink,
  1424. .dellink = vrf_dellink,
  1425. .setup = vrf_setup,
  1426. .maxtype = IFLA_VRF_MAX,
  1427. };
  1428. static int vrf_device_event(struct notifier_block *unused,
  1429. unsigned long event, void *ptr)
  1430. {
  1431. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1432. /* only care about unregister events to drop slave references */
  1433. if (event == NETDEV_UNREGISTER) {
  1434. struct net_device *vrf_dev;
  1435. if (!netif_is_l3_slave(dev))
  1436. goto out;
  1437. vrf_dev = netdev_master_upper_dev_get(dev);
  1438. vrf_del_slave(vrf_dev, dev);
  1439. }
  1440. out:
  1441. return NOTIFY_DONE;
  1442. }
  1443. static struct notifier_block vrf_notifier_block __read_mostly = {
  1444. .notifier_call = vrf_device_event,
  1445. };
  1446. static int vrf_map_init(struct vrf_map *vmap)
  1447. {
  1448. spin_lock_init(&vmap->vmap_lock);
  1449. hash_init(vmap->ht);
  1450. vmap->strict_mode = false;
  1451. return 0;
  1452. }
  1453. #ifdef CONFIG_SYSCTL
  1454. static bool vrf_strict_mode(struct vrf_map *vmap)
  1455. {
  1456. bool strict_mode;
  1457. vrf_map_lock(vmap);
  1458. strict_mode = vmap->strict_mode;
  1459. vrf_map_unlock(vmap);
  1460. return strict_mode;
  1461. }
  1462. static int vrf_strict_mode_change(struct vrf_map *vmap, bool new_mode)
  1463. {
  1464. bool *cur_mode;
  1465. int res = 0;
  1466. vrf_map_lock(vmap);
  1467. cur_mode = &vmap->strict_mode;
  1468. if (*cur_mode == new_mode)
  1469. goto unlock;
  1470. if (*cur_mode) {
  1471. /* disable strict mode */
  1472. *cur_mode = false;
  1473. } else {
  1474. if (vmap->shared_tables) {
  1475. /* we cannot allow strict_mode because there are some
  1476. * vrfs that share one or more tables.
  1477. */
  1478. res = -EBUSY;
  1479. goto unlock;
  1480. }
  1481. /* no tables are shared among vrfs, so we can go back
  1482. * to 1:1 association between a vrf with its table.
  1483. */
  1484. *cur_mode = true;
  1485. }
  1486. unlock:
  1487. vrf_map_unlock(vmap);
  1488. return res;
  1489. }
  1490. static int vrf_shared_table_handler(const struct ctl_table *table, int write,
  1491. void *buffer, size_t *lenp, loff_t *ppos)
  1492. {
  1493. struct net *net = (struct net *)table->extra1;
  1494. struct vrf_map *vmap = netns_vrf_map(net);
  1495. int proc_strict_mode = 0;
  1496. struct ctl_table tmp = {
  1497. .procname = table->procname,
  1498. .data = &proc_strict_mode,
  1499. .maxlen = sizeof(int),
  1500. .mode = table->mode,
  1501. .extra1 = SYSCTL_ZERO,
  1502. .extra2 = SYSCTL_ONE,
  1503. };
  1504. int ret;
  1505. if (!write)
  1506. proc_strict_mode = vrf_strict_mode(vmap);
  1507. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1508. if (write && ret == 0)
  1509. ret = vrf_strict_mode_change(vmap, (bool)proc_strict_mode);
  1510. return ret;
  1511. }
  1512. static const struct ctl_table vrf_table[] = {
  1513. {
  1514. .procname = "strict_mode",
  1515. .data = NULL,
  1516. .maxlen = sizeof(int),
  1517. .mode = 0644,
  1518. .proc_handler = vrf_shared_table_handler,
  1519. /* set by the vrf_netns_init */
  1520. .extra1 = NULL,
  1521. },
  1522. };
  1523. static int vrf_netns_init_sysctl(struct net *net, struct netns_vrf *nn_vrf)
  1524. {
  1525. struct ctl_table *table;
  1526. table = kmemdup(vrf_table, sizeof(vrf_table), GFP_KERNEL);
  1527. if (!table)
  1528. return -ENOMEM;
  1529. /* init the extra1 parameter with the reference to current netns */
  1530. table[0].extra1 = net;
  1531. nn_vrf->ctl_hdr = register_net_sysctl_sz(net, "net/vrf", table,
  1532. ARRAY_SIZE(vrf_table));
  1533. if (!nn_vrf->ctl_hdr) {
  1534. kfree(table);
  1535. return -ENOMEM;
  1536. }
  1537. return 0;
  1538. }
  1539. static void vrf_netns_exit_sysctl(struct net *net)
  1540. {
  1541. struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id);
  1542. const struct ctl_table *table;
  1543. table = nn_vrf->ctl_hdr->ctl_table_arg;
  1544. unregister_net_sysctl_table(nn_vrf->ctl_hdr);
  1545. kfree(table);
  1546. }
  1547. #else
  1548. static int vrf_netns_init_sysctl(struct net *net, struct netns_vrf *nn_vrf)
  1549. {
  1550. return 0;
  1551. }
  1552. static void vrf_netns_exit_sysctl(struct net *net)
  1553. {
  1554. }
  1555. #endif
  1556. /* Initialize per network namespace state */
  1557. static int __net_init vrf_netns_init(struct net *net)
  1558. {
  1559. struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id);
  1560. nn_vrf->add_fib_rules = true;
  1561. vrf_map_init(&nn_vrf->vmap);
  1562. return vrf_netns_init_sysctl(net, nn_vrf);
  1563. }
  1564. static void __net_exit vrf_netns_exit(struct net *net)
  1565. {
  1566. vrf_netns_exit_sysctl(net);
  1567. }
  1568. static struct pernet_operations vrf_net_ops __net_initdata = {
  1569. .init = vrf_netns_init,
  1570. .exit = vrf_netns_exit,
  1571. .id = &vrf_net_id,
  1572. .size = sizeof(struct netns_vrf),
  1573. };
  1574. static int __init vrf_init_module(void)
  1575. {
  1576. int rc;
  1577. register_netdevice_notifier(&vrf_notifier_block);
  1578. rc = register_pernet_subsys(&vrf_net_ops);
  1579. if (rc < 0)
  1580. goto error;
  1581. rc = l3mdev_table_lookup_register(L3MDEV_TYPE_VRF,
  1582. vrf_ifindex_lookup_by_table_id);
  1583. if (rc < 0)
  1584. goto unreg_pernet;
  1585. rc = rtnl_link_register(&vrf_link_ops);
  1586. if (rc < 0)
  1587. goto table_lookup_unreg;
  1588. return 0;
  1589. table_lookup_unreg:
  1590. l3mdev_table_lookup_unregister(L3MDEV_TYPE_VRF,
  1591. vrf_ifindex_lookup_by_table_id);
  1592. unreg_pernet:
  1593. unregister_pernet_subsys(&vrf_net_ops);
  1594. error:
  1595. unregister_netdevice_notifier(&vrf_notifier_block);
  1596. return rc;
  1597. }
  1598. module_init(vrf_init_module);
  1599. MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
  1600. MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
  1601. MODULE_LICENSE("GPL");
  1602. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  1603. MODULE_VERSION(DRV_VERSION);