vrf.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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/rtnetlink.h>
  34. #include <net/route.h>
  35. #include <net/addrconf.h>
  36. #include <net/l3mdev.h>
  37. #define RT_FL_TOS(oldflp4) \
  38. ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
  39. #define DRV_NAME "vrf"
  40. #define DRV_VERSION "1.0"
  41. #define vrf_master_get_rcu(dev) \
  42. ((struct net_device *)rcu_dereference(dev->rx_handler_data))
  43. struct slave {
  44. struct list_head list;
  45. struct net_device *dev;
  46. };
  47. struct slave_queue {
  48. struct list_head all_slaves;
  49. };
  50. struct net_vrf {
  51. struct slave_queue queue;
  52. struct rtable *rth;
  53. struct rt6_info *rt6;
  54. u32 tb_id;
  55. };
  56. struct pcpu_dstats {
  57. u64 tx_pkts;
  58. u64 tx_bytes;
  59. u64 tx_drps;
  60. u64 rx_pkts;
  61. u64 rx_bytes;
  62. struct u64_stats_sync syncp;
  63. };
  64. static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie)
  65. {
  66. return dst;
  67. }
  68. static int vrf_ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  69. {
  70. return ip_local_out(net, sk, skb);
  71. }
  72. static unsigned int vrf_v4_mtu(const struct dst_entry *dst)
  73. {
  74. /* TO-DO: return max ethernet size? */
  75. return dst->dev->mtu;
  76. }
  77. static void vrf_dst_destroy(struct dst_entry *dst)
  78. {
  79. /* our dst lives forever - or until the device is closed */
  80. }
  81. static unsigned int vrf_default_advmss(const struct dst_entry *dst)
  82. {
  83. return 65535 - 40;
  84. }
  85. static struct dst_ops vrf_dst_ops = {
  86. .family = AF_INET,
  87. .local_out = vrf_ip_local_out,
  88. .check = vrf_ip_check,
  89. .mtu = vrf_v4_mtu,
  90. .destroy = vrf_dst_destroy,
  91. .default_advmss = vrf_default_advmss,
  92. };
  93. /* neighbor handling is done with actual device; do not want
  94. * to flip skb->dev for those ndisc packets. This really fails
  95. * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
  96. * a start.
  97. */
  98. #if IS_ENABLED(CONFIG_IPV6)
  99. static bool check_ipv6_frame(const struct sk_buff *skb)
  100. {
  101. const struct ipv6hdr *ipv6h;
  102. struct ipv6hdr _ipv6h;
  103. bool rc = true;
  104. ipv6h = skb_header_pointer(skb, 0, sizeof(_ipv6h), &_ipv6h);
  105. if (!ipv6h)
  106. goto out;
  107. if (ipv6h->nexthdr == NEXTHDR_ICMP) {
  108. const struct icmp6hdr *icmph;
  109. struct icmp6hdr _icmph;
  110. icmph = skb_header_pointer(skb, sizeof(_ipv6h),
  111. sizeof(_icmph), &_icmph);
  112. if (!icmph)
  113. goto out;
  114. switch (icmph->icmp6_type) {
  115. case NDISC_ROUTER_SOLICITATION:
  116. case NDISC_ROUTER_ADVERTISEMENT:
  117. case NDISC_NEIGHBOUR_SOLICITATION:
  118. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  119. case NDISC_REDIRECT:
  120. rc = false;
  121. break;
  122. }
  123. }
  124. out:
  125. return rc;
  126. }
  127. #else
  128. static bool check_ipv6_frame(const struct sk_buff *skb)
  129. {
  130. return false;
  131. }
  132. #endif
  133. static bool is_ip_rx_frame(struct sk_buff *skb)
  134. {
  135. switch (skb->protocol) {
  136. case htons(ETH_P_IP):
  137. return true;
  138. case htons(ETH_P_IPV6):
  139. return check_ipv6_frame(skb);
  140. }
  141. return false;
  142. }
  143. static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
  144. {
  145. vrf_dev->stats.tx_errors++;
  146. kfree_skb(skb);
  147. }
  148. /* note: already called with rcu_read_lock */
  149. static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb)
  150. {
  151. struct sk_buff *skb = *pskb;
  152. if (is_ip_rx_frame(skb)) {
  153. struct net_device *dev = vrf_master_get_rcu(skb->dev);
  154. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  155. u64_stats_update_begin(&dstats->syncp);
  156. dstats->rx_pkts++;
  157. dstats->rx_bytes += skb->len;
  158. u64_stats_update_end(&dstats->syncp);
  159. skb->dev = dev;
  160. return RX_HANDLER_ANOTHER;
  161. }
  162. return RX_HANDLER_PASS;
  163. }
  164. static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
  165. struct rtnl_link_stats64 *stats)
  166. {
  167. int i;
  168. for_each_possible_cpu(i) {
  169. const struct pcpu_dstats *dstats;
  170. u64 tbytes, tpkts, tdrops, rbytes, rpkts;
  171. unsigned int start;
  172. dstats = per_cpu_ptr(dev->dstats, i);
  173. do {
  174. start = u64_stats_fetch_begin_irq(&dstats->syncp);
  175. tbytes = dstats->tx_bytes;
  176. tpkts = dstats->tx_pkts;
  177. tdrops = dstats->tx_drps;
  178. rbytes = dstats->rx_bytes;
  179. rpkts = dstats->rx_pkts;
  180. } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
  181. stats->tx_bytes += tbytes;
  182. stats->tx_packets += tpkts;
  183. stats->tx_dropped += tdrops;
  184. stats->rx_bytes += rbytes;
  185. stats->rx_packets += rpkts;
  186. }
  187. return stats;
  188. }
  189. #if IS_ENABLED(CONFIG_IPV6)
  190. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  191. struct net_device *dev)
  192. {
  193. const struct ipv6hdr *iph = ipv6_hdr(skb);
  194. struct net *net = dev_net(skb->dev);
  195. struct flowi6 fl6 = {
  196. /* needed to match OIF rule */
  197. .flowi6_oif = dev->ifindex,
  198. .flowi6_iif = LOOPBACK_IFINDEX,
  199. .daddr = iph->daddr,
  200. .saddr = iph->saddr,
  201. .flowlabel = ip6_flowinfo(iph),
  202. .flowi6_mark = skb->mark,
  203. .flowi6_proto = iph->nexthdr,
  204. .flowi6_flags = FLOWI_FLAG_L3MDEV_SRC | FLOWI_FLAG_SKIP_NH_OIF,
  205. };
  206. int ret = NET_XMIT_DROP;
  207. struct dst_entry *dst;
  208. struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
  209. dst = ip6_route_output(net, NULL, &fl6);
  210. if (dst == dst_null)
  211. goto err;
  212. skb_dst_drop(skb);
  213. skb_dst_set(skb, dst);
  214. ret = ip6_local_out(net, skb->sk, skb);
  215. if (unlikely(net_xmit_eval(ret)))
  216. dev->stats.tx_errors++;
  217. else
  218. ret = NET_XMIT_SUCCESS;
  219. return ret;
  220. err:
  221. vrf_tx_error(dev, skb);
  222. return NET_XMIT_DROP;
  223. }
  224. #else
  225. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  226. struct net_device *dev)
  227. {
  228. vrf_tx_error(dev, skb);
  229. return NET_XMIT_DROP;
  230. }
  231. #endif
  232. static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
  233. struct net_device *vrf_dev)
  234. {
  235. struct rtable *rt;
  236. int err = 1;
  237. rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
  238. if (IS_ERR(rt))
  239. goto out;
  240. /* TO-DO: what about broadcast ? */
  241. if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
  242. ip_rt_put(rt);
  243. goto out;
  244. }
  245. skb_dst_drop(skb);
  246. skb_dst_set(skb, &rt->dst);
  247. err = 0;
  248. out:
  249. return err;
  250. }
  251. static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
  252. struct net_device *vrf_dev)
  253. {
  254. struct iphdr *ip4h = ip_hdr(skb);
  255. int ret = NET_XMIT_DROP;
  256. struct flowi4 fl4 = {
  257. /* needed to match OIF rule */
  258. .flowi4_oif = vrf_dev->ifindex,
  259. .flowi4_iif = LOOPBACK_IFINDEX,
  260. .flowi4_tos = RT_TOS(ip4h->tos),
  261. .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC |
  262. FLOWI_FLAG_SKIP_NH_OIF,
  263. .flowi4_proto = ip4h->protocol,
  264. .daddr = ip4h->daddr,
  265. .saddr = ip4h->saddr,
  266. };
  267. if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
  268. goto err;
  269. if (!ip4h->saddr) {
  270. ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
  271. RT_SCOPE_LINK);
  272. }
  273. ret = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  274. if (unlikely(net_xmit_eval(ret)))
  275. vrf_dev->stats.tx_errors++;
  276. else
  277. ret = NET_XMIT_SUCCESS;
  278. out:
  279. return ret;
  280. err:
  281. vrf_tx_error(vrf_dev, skb);
  282. goto out;
  283. }
  284. static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
  285. {
  286. /* strip the ethernet header added for pass through VRF device */
  287. __skb_pull(skb, skb_network_offset(skb));
  288. switch (skb->protocol) {
  289. case htons(ETH_P_IP):
  290. return vrf_process_v4_outbound(skb, dev);
  291. case htons(ETH_P_IPV6):
  292. return vrf_process_v6_outbound(skb, dev);
  293. default:
  294. vrf_tx_error(dev, skb);
  295. return NET_XMIT_DROP;
  296. }
  297. }
  298. static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
  299. {
  300. int len = skb->len;
  301. netdev_tx_t ret = is_ip_tx_frame(skb, dev);
  302. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  303. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  304. u64_stats_update_begin(&dstats->syncp);
  305. dstats->tx_pkts++;
  306. dstats->tx_bytes += len;
  307. u64_stats_update_end(&dstats->syncp);
  308. } else {
  309. this_cpu_inc(dev->dstats->tx_drps);
  310. }
  311. return ret;
  312. }
  313. #if IS_ENABLED(CONFIG_IPV6)
  314. static struct dst_entry *vrf_ip6_check(struct dst_entry *dst, u32 cookie)
  315. {
  316. return dst;
  317. }
  318. static struct dst_ops vrf_dst_ops6 = {
  319. .family = AF_INET6,
  320. .local_out = ip6_local_out,
  321. .check = vrf_ip6_check,
  322. .mtu = vrf_v4_mtu,
  323. .destroy = vrf_dst_destroy,
  324. .default_advmss = vrf_default_advmss,
  325. };
  326. static int init_dst_ops6_kmem_cachep(void)
  327. {
  328. vrf_dst_ops6.kmem_cachep = kmem_cache_create("vrf_ip6_dst_cache",
  329. sizeof(struct rt6_info),
  330. 0,
  331. SLAB_HWCACHE_ALIGN,
  332. NULL);
  333. if (!vrf_dst_ops6.kmem_cachep)
  334. return -ENOMEM;
  335. return 0;
  336. }
  337. static void free_dst_ops6_kmem_cachep(void)
  338. {
  339. kmem_cache_destroy(vrf_dst_ops6.kmem_cachep);
  340. }
  341. static int vrf_input6(struct sk_buff *skb)
  342. {
  343. skb->dev->stats.rx_errors++;
  344. kfree_skb(skb);
  345. return 0;
  346. }
  347. /* modelled after ip6_finish_output2 */
  348. static int vrf_finish_output6(struct net *net, struct sock *sk,
  349. struct sk_buff *skb)
  350. {
  351. struct dst_entry *dst = skb_dst(skb);
  352. struct net_device *dev = dst->dev;
  353. struct neighbour *neigh;
  354. struct in6_addr *nexthop;
  355. int ret;
  356. nf_reset(skb);
  357. skb->protocol = htons(ETH_P_IPV6);
  358. skb->dev = dev;
  359. rcu_read_lock_bh();
  360. nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
  361. neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
  362. if (unlikely(!neigh))
  363. neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
  364. if (!IS_ERR(neigh)) {
  365. ret = dst_neigh_output(dst, neigh, skb);
  366. rcu_read_unlock_bh();
  367. return ret;
  368. }
  369. rcu_read_unlock_bh();
  370. IP6_INC_STATS(dev_net(dst->dev),
  371. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  372. kfree_skb(skb);
  373. return -EINVAL;
  374. }
  375. /* modelled after ip6_output */
  376. static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
  377. {
  378. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  379. net, sk, skb, NULL, skb_dst(skb)->dev,
  380. vrf_finish_output6,
  381. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  382. }
  383. static void vrf_rt6_destroy(struct net_vrf *vrf)
  384. {
  385. dst_destroy(&vrf->rt6->dst);
  386. free_percpu(vrf->rt6->rt6i_pcpu);
  387. vrf->rt6 = NULL;
  388. }
  389. static int vrf_rt6_create(struct net_device *dev)
  390. {
  391. struct net_vrf *vrf = netdev_priv(dev);
  392. struct dst_entry *dst;
  393. struct rt6_info *rt6;
  394. int cpu;
  395. int rc = -ENOMEM;
  396. rt6 = dst_alloc(&vrf_dst_ops6, dev, 0,
  397. DST_OBSOLETE_NONE,
  398. (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
  399. if (!rt6)
  400. goto out;
  401. dst = &rt6->dst;
  402. rt6->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_KERNEL);
  403. if (!rt6->rt6i_pcpu) {
  404. dst_destroy(dst);
  405. goto out;
  406. }
  407. for_each_possible_cpu(cpu) {
  408. struct rt6_info **p = per_cpu_ptr(rt6->rt6i_pcpu, cpu);
  409. *p = NULL;
  410. }
  411. memset(dst + 1, 0, sizeof(*rt6) - sizeof(*dst));
  412. INIT_LIST_HEAD(&rt6->rt6i_siblings);
  413. INIT_LIST_HEAD(&rt6->rt6i_uncached);
  414. rt6->dst.input = vrf_input6;
  415. rt6->dst.output = vrf_output6;
  416. rt6->rt6i_table = fib6_get_table(dev_net(dev), vrf->tb_id);
  417. atomic_set(&rt6->dst.__refcnt, 2);
  418. vrf->rt6 = rt6;
  419. rc = 0;
  420. out:
  421. return rc;
  422. }
  423. #else
  424. static int init_dst_ops6_kmem_cachep(void)
  425. {
  426. return 0;
  427. }
  428. static void free_dst_ops6_kmem_cachep(void)
  429. {
  430. }
  431. static void vrf_rt6_destroy(struct net_vrf *vrf)
  432. {
  433. }
  434. static int vrf_rt6_create(struct net_device *dev)
  435. {
  436. return 0;
  437. }
  438. #endif
  439. /* modelled after ip_finish_output2 */
  440. static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  441. {
  442. struct dst_entry *dst = skb_dst(skb);
  443. struct rtable *rt = (struct rtable *)dst;
  444. struct net_device *dev = dst->dev;
  445. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  446. struct neighbour *neigh;
  447. u32 nexthop;
  448. int ret = -EINVAL;
  449. nf_reset(skb);
  450. /* Be paranoid, rather than too clever. */
  451. if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
  452. struct sk_buff *skb2;
  453. skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
  454. if (!skb2) {
  455. ret = -ENOMEM;
  456. goto err;
  457. }
  458. if (skb->sk)
  459. skb_set_owner_w(skb2, skb->sk);
  460. consume_skb(skb);
  461. skb = skb2;
  462. }
  463. rcu_read_lock_bh();
  464. nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
  465. neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
  466. if (unlikely(!neigh))
  467. neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
  468. if (!IS_ERR(neigh)) {
  469. ret = dst_neigh_output(dst, neigh, skb);
  470. rcu_read_unlock_bh();
  471. return ret;
  472. }
  473. rcu_read_unlock_bh();
  474. err:
  475. vrf_tx_error(skb->dev, skb);
  476. return ret;
  477. }
  478. static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  479. {
  480. struct net_device *dev = skb_dst(skb)->dev;
  481. IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
  482. skb->dev = dev;
  483. skb->protocol = htons(ETH_P_IP);
  484. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  485. net, sk, skb, NULL, dev,
  486. vrf_finish_output,
  487. !(IPCB(skb)->flags & IPSKB_REROUTED));
  488. }
  489. static void vrf_rtable_destroy(struct net_vrf *vrf)
  490. {
  491. struct dst_entry *dst = (struct dst_entry *)vrf->rth;
  492. dst_destroy(dst);
  493. vrf->rth = NULL;
  494. }
  495. static struct rtable *vrf_rtable_create(struct net_device *dev)
  496. {
  497. struct net_vrf *vrf = netdev_priv(dev);
  498. struct rtable *rth;
  499. rth = dst_alloc(&vrf_dst_ops, dev, 2,
  500. DST_OBSOLETE_NONE,
  501. (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
  502. if (rth) {
  503. rth->dst.output = vrf_output;
  504. rth->rt_genid = rt_genid_ipv4(dev_net(dev));
  505. rth->rt_flags = 0;
  506. rth->rt_type = RTN_UNICAST;
  507. rth->rt_is_input = 0;
  508. rth->rt_iif = 0;
  509. rth->rt_pmtu = 0;
  510. rth->rt_gateway = 0;
  511. rth->rt_uses_gateway = 0;
  512. rth->rt_table_id = vrf->tb_id;
  513. INIT_LIST_HEAD(&rth->rt_uncached);
  514. rth->rt_uncached_list = NULL;
  515. }
  516. return rth;
  517. }
  518. /**************************** device handling ********************/
  519. /* cycle interface to flush neighbor cache and move routes across tables */
  520. static void cycle_netdev(struct net_device *dev)
  521. {
  522. unsigned int flags = dev->flags;
  523. int ret;
  524. if (!netif_running(dev))
  525. return;
  526. ret = dev_change_flags(dev, flags & ~IFF_UP);
  527. if (ret >= 0)
  528. ret = dev_change_flags(dev, flags);
  529. if (ret < 0) {
  530. netdev_err(dev,
  531. "Failed to cycle device %s; route tables might be wrong!\n",
  532. dev->name);
  533. }
  534. }
  535. static struct slave *__vrf_find_slave_dev(struct slave_queue *queue,
  536. struct net_device *dev)
  537. {
  538. struct list_head *head = &queue->all_slaves;
  539. struct slave *slave;
  540. list_for_each_entry(slave, head, list) {
  541. if (slave->dev == dev)
  542. return slave;
  543. }
  544. return NULL;
  545. }
  546. /* inverse of __vrf_insert_slave */
  547. static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave)
  548. {
  549. list_del(&slave->list);
  550. }
  551. static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave)
  552. {
  553. list_add(&slave->list, &queue->all_slaves);
  554. }
  555. static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  556. {
  557. struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  558. struct net_vrf *vrf = netdev_priv(dev);
  559. struct slave_queue *queue = &vrf->queue;
  560. int ret = -ENOMEM;
  561. if (!slave)
  562. goto out_fail;
  563. slave->dev = port_dev;
  564. /* register the packet handler for slave ports */
  565. ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev);
  566. if (ret) {
  567. netdev_err(port_dev,
  568. "Device %s failed to register rx_handler\n",
  569. port_dev->name);
  570. goto out_fail;
  571. }
  572. ret = netdev_master_upper_dev_link(port_dev, dev);
  573. if (ret < 0)
  574. goto out_unregister;
  575. port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
  576. __vrf_insert_slave(queue, slave);
  577. cycle_netdev(port_dev);
  578. return 0;
  579. out_unregister:
  580. netdev_rx_handler_unregister(port_dev);
  581. out_fail:
  582. kfree(slave);
  583. return ret;
  584. }
  585. static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  586. {
  587. if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
  588. return -EINVAL;
  589. return do_vrf_add_slave(dev, port_dev);
  590. }
  591. /* inverse of do_vrf_add_slave */
  592. static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  593. {
  594. struct net_vrf *vrf = netdev_priv(dev);
  595. struct slave_queue *queue = &vrf->queue;
  596. struct slave *slave;
  597. netdev_upper_dev_unlink(port_dev, dev);
  598. port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
  599. netdev_rx_handler_unregister(port_dev);
  600. cycle_netdev(port_dev);
  601. slave = __vrf_find_slave_dev(queue, port_dev);
  602. if (slave)
  603. __vrf_remove_slave(queue, slave);
  604. kfree(slave);
  605. return 0;
  606. }
  607. static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  608. {
  609. return do_vrf_del_slave(dev, port_dev);
  610. }
  611. static void vrf_dev_uninit(struct net_device *dev)
  612. {
  613. struct net_vrf *vrf = netdev_priv(dev);
  614. // struct slave_queue *queue = &vrf->queue;
  615. // struct list_head *head = &queue->all_slaves;
  616. // struct slave *slave, *next;
  617. vrf_rtable_destroy(vrf);
  618. vrf_rt6_destroy(vrf);
  619. // list_for_each_entry_safe(slave, next, head, list)
  620. // vrf_del_slave(dev, slave->dev);
  621. free_percpu(dev->dstats);
  622. dev->dstats = NULL;
  623. }
  624. static int vrf_dev_init(struct net_device *dev)
  625. {
  626. struct net_vrf *vrf = netdev_priv(dev);
  627. INIT_LIST_HEAD(&vrf->queue.all_slaves);
  628. dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
  629. if (!dev->dstats)
  630. goto out_nomem;
  631. /* create the default dst which points back to us */
  632. vrf->rth = vrf_rtable_create(dev);
  633. if (!vrf->rth)
  634. goto out_stats;
  635. if (vrf_rt6_create(dev) != 0)
  636. goto out_rth;
  637. dev->flags = IFF_MASTER | IFF_NOARP;
  638. return 0;
  639. out_rth:
  640. vrf_rtable_destroy(vrf);
  641. out_stats:
  642. free_percpu(dev->dstats);
  643. dev->dstats = NULL;
  644. out_nomem:
  645. return -ENOMEM;
  646. }
  647. static const struct net_device_ops vrf_netdev_ops = {
  648. .ndo_init = vrf_dev_init,
  649. .ndo_uninit = vrf_dev_uninit,
  650. .ndo_start_xmit = vrf_xmit,
  651. .ndo_get_stats64 = vrf_get_stats64,
  652. .ndo_add_slave = vrf_add_slave,
  653. .ndo_del_slave = vrf_del_slave,
  654. };
  655. static u32 vrf_fib_table(const struct net_device *dev)
  656. {
  657. struct net_vrf *vrf = netdev_priv(dev);
  658. return vrf->tb_id;
  659. }
  660. static struct rtable *vrf_get_rtable(const struct net_device *dev,
  661. const struct flowi4 *fl4)
  662. {
  663. struct rtable *rth = NULL;
  664. if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) {
  665. struct net_vrf *vrf = netdev_priv(dev);
  666. rth = vrf->rth;
  667. atomic_inc(&rth->dst.__refcnt);
  668. }
  669. return rth;
  670. }
  671. /* called under rcu_read_lock */
  672. static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4)
  673. {
  674. struct fib_result res = { .tclassid = 0 };
  675. struct net *net = dev_net(dev);
  676. u32 orig_tos = fl4->flowi4_tos;
  677. u8 flags = fl4->flowi4_flags;
  678. u8 scope = fl4->flowi4_scope;
  679. u8 tos = RT_FL_TOS(fl4);
  680. int rc;
  681. if (unlikely(!fl4->daddr))
  682. return 0;
  683. fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF;
  684. fl4->flowi4_iif = LOOPBACK_IFINDEX;
  685. fl4->flowi4_tos = tos & IPTOS_RT_MASK;
  686. fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
  687. RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
  688. rc = fib_lookup(net, fl4, &res, 0);
  689. if (!rc) {
  690. if (res.type == RTN_LOCAL)
  691. fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr;
  692. else
  693. fib_select_path(net, &res, fl4, -1);
  694. }
  695. fl4->flowi4_flags = flags;
  696. fl4->flowi4_tos = orig_tos;
  697. fl4->flowi4_scope = scope;
  698. return rc;
  699. }
  700. #if IS_ENABLED(CONFIG_IPV6)
  701. static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
  702. const struct flowi6 *fl6)
  703. {
  704. struct rt6_info *rt = NULL;
  705. if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) {
  706. struct net_vrf *vrf = netdev_priv(dev);
  707. rt = vrf->rt6;
  708. atomic_inc(&rt->dst.__refcnt);
  709. }
  710. return (struct dst_entry *)rt;
  711. }
  712. #endif
  713. static const struct l3mdev_ops vrf_l3mdev_ops = {
  714. .l3mdev_fib_table = vrf_fib_table,
  715. .l3mdev_get_rtable = vrf_get_rtable,
  716. .l3mdev_get_saddr = vrf_get_saddr,
  717. #if IS_ENABLED(CONFIG_IPV6)
  718. .l3mdev_get_rt6_dst = vrf_get_rt6_dst,
  719. #endif
  720. };
  721. static void vrf_get_drvinfo(struct net_device *dev,
  722. struct ethtool_drvinfo *info)
  723. {
  724. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  725. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  726. }
  727. static const struct ethtool_ops vrf_ethtool_ops = {
  728. .get_drvinfo = vrf_get_drvinfo,
  729. };
  730. static void vrf_setup(struct net_device *dev)
  731. {
  732. ether_setup(dev);
  733. /* Initialize the device structure. */
  734. dev->netdev_ops = &vrf_netdev_ops;
  735. dev->l3mdev_ops = &vrf_l3mdev_ops;
  736. dev->ethtool_ops = &vrf_ethtool_ops;
  737. dev->destructor = free_netdev;
  738. /* Fill in device structure with ethernet-generic values. */
  739. eth_hw_addr_random(dev);
  740. /* don't acquire vrf device's netif_tx_lock when transmitting */
  741. dev->features |= NETIF_F_LLTX;
  742. /* don't allow vrf devices to change network namespaces. */
  743. dev->features |= NETIF_F_NETNS_LOCAL;
  744. }
  745. static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
  746. {
  747. if (tb[IFLA_ADDRESS]) {
  748. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  749. return -EINVAL;
  750. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  751. return -EADDRNOTAVAIL;
  752. }
  753. return 0;
  754. }
  755. static void vrf_dellink(struct net_device *dev, struct list_head *head)
  756. {
  757. struct net_vrf *vrf = netdev_priv(dev);
  758. struct slave_queue *queue = &vrf->queue;
  759. struct list_head *all_slaves = &queue->all_slaves;
  760. struct slave *slave, *next;
  761. list_for_each_entry_safe(slave, next, all_slaves, list)
  762. vrf_del_slave(dev, slave->dev);
  763. unregister_netdevice_queue(dev, head);
  764. }
  765. static int vrf_newlink(struct net *src_net, struct net_device *dev,
  766. struct nlattr *tb[], struct nlattr *data[])
  767. {
  768. struct net_vrf *vrf = netdev_priv(dev);
  769. if (!data || !data[IFLA_VRF_TABLE])
  770. return -EINVAL;
  771. vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
  772. if (vrf->tb_id == RT_TABLE_UNSPEC)
  773. return -EINVAL;
  774. dev->priv_flags |= IFF_L3MDEV_MASTER;
  775. return register_netdevice(dev);
  776. }
  777. static size_t vrf_nl_getsize(const struct net_device *dev)
  778. {
  779. return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
  780. }
  781. static int vrf_fillinfo(struct sk_buff *skb,
  782. const struct net_device *dev)
  783. {
  784. struct net_vrf *vrf = netdev_priv(dev);
  785. return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
  786. }
  787. static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
  788. [IFLA_VRF_TABLE] = { .type = NLA_U32 },
  789. };
  790. static struct rtnl_link_ops vrf_link_ops __read_mostly = {
  791. .kind = DRV_NAME,
  792. .priv_size = sizeof(struct net_vrf),
  793. .get_size = vrf_nl_getsize,
  794. .policy = vrf_nl_policy,
  795. .validate = vrf_validate,
  796. .fill_info = vrf_fillinfo,
  797. .newlink = vrf_newlink,
  798. .dellink = vrf_dellink,
  799. .setup = vrf_setup,
  800. .maxtype = IFLA_VRF_MAX,
  801. };
  802. static int vrf_device_event(struct notifier_block *unused,
  803. unsigned long event, void *ptr)
  804. {
  805. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  806. /* only care about unregister events to drop slave references */
  807. if (event == NETDEV_UNREGISTER) {
  808. struct net_device *vrf_dev;
  809. if (!netif_is_l3_slave(dev))
  810. goto out;
  811. vrf_dev = netdev_master_upper_dev_get(dev);
  812. vrf_del_slave(vrf_dev, dev);
  813. }
  814. out:
  815. return NOTIFY_DONE;
  816. }
  817. static struct notifier_block vrf_notifier_block __read_mostly = {
  818. .notifier_call = vrf_device_event,
  819. };
  820. static int __init vrf_init_module(void)
  821. {
  822. int rc;
  823. vrf_dst_ops.kmem_cachep =
  824. kmem_cache_create("vrf_ip_dst_cache",
  825. sizeof(struct rtable), 0,
  826. SLAB_HWCACHE_ALIGN,
  827. NULL);
  828. if (!vrf_dst_ops.kmem_cachep)
  829. return -ENOMEM;
  830. rc = init_dst_ops6_kmem_cachep();
  831. if (rc != 0)
  832. goto error2;
  833. register_netdevice_notifier(&vrf_notifier_block);
  834. rc = rtnl_link_register(&vrf_link_ops);
  835. if (rc < 0)
  836. goto error;
  837. return 0;
  838. error:
  839. unregister_netdevice_notifier(&vrf_notifier_block);
  840. free_dst_ops6_kmem_cachep();
  841. error2:
  842. kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
  843. return rc;
  844. }
  845. static void __exit vrf_cleanup_module(void)
  846. {
  847. rtnl_link_unregister(&vrf_link_ops);
  848. unregister_netdevice_notifier(&vrf_notifier_block);
  849. kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
  850. free_dst_ops6_kmem_cachep();
  851. }
  852. module_init(vrf_init_module);
  853. module_exit(vrf_cleanup_module);
  854. MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
  855. MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
  856. MODULE_LICENSE("GPL");
  857. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  858. MODULE_VERSION(DRV_VERSION);