vrf.c 33 KB

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