ip6_vti.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * IPv6 virtual tunneling interface
  3. *
  4. * Copyright (C) 2013 secunet Security Networks AG
  5. *
  6. * Author:
  7. * Steffen Klassert <[email protected]>
  8. *
  9. * Based on:
  10. * net/ipv6/ip6_tunnel.c
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/capability.h>
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/sockios.h>
  22. #include <linux/icmp.h>
  23. #include <linux/if.h>
  24. #include <linux/in.h>
  25. #include <linux/ip.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmpv6.h>
  31. #include <linux/init.h>
  32. #include <linux/route.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/netfilter_ipv6.h>
  35. #include <linux/slab.h>
  36. #include <linux/hash.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/atomic.h>
  39. #include <net/icmp.h>
  40. #include <net/ip.h>
  41. #include <net/ip_tunnels.h>
  42. #include <net/ipv6.h>
  43. #include <net/ip6_route.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip6_tunnel.h>
  46. #include <net/xfrm.h>
  47. #include <net/net_namespace.h>
  48. #include <net/netns/generic.h>
  49. #define IP6_VTI_HASH_SIZE_SHIFT 5
  50. #define IP6_VTI_HASH_SIZE (1 << IP6_VTI_HASH_SIZE_SHIFT)
  51. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  52. {
  53. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  54. return hash_32(hash, IP6_VTI_HASH_SIZE_SHIFT);
  55. }
  56. static int vti6_dev_init(struct net_device *dev);
  57. static void vti6_dev_setup(struct net_device *dev);
  58. static struct rtnl_link_ops vti6_link_ops __read_mostly;
  59. static int vti6_net_id __read_mostly;
  60. struct vti6_net {
  61. /* the vti6 tunnel fallback device */
  62. struct net_device *fb_tnl_dev;
  63. /* lists for storing tunnels in use */
  64. struct ip6_tnl __rcu *tnls_r_l[IP6_VTI_HASH_SIZE];
  65. struct ip6_tnl __rcu *tnls_wc[1];
  66. struct ip6_tnl __rcu **tnls[2];
  67. };
  68. #define for_each_vti6_tunnel_rcu(start) \
  69. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  70. /**
  71. * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
  72. * @net: network namespace
  73. * @remote: the address of the tunnel exit-point
  74. * @local: the address of the tunnel entry-point
  75. *
  76. * Return:
  77. * tunnel matching given end-points if found,
  78. * else fallback tunnel if its device is up,
  79. * else %NULL
  80. **/
  81. static struct ip6_tnl *
  82. vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
  83. const struct in6_addr *local)
  84. {
  85. unsigned int hash = HASH(remote, local);
  86. struct ip6_tnl *t;
  87. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  88. struct in6_addr any;
  89. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  90. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  91. ipv6_addr_equal(remote, &t->parms.raddr) &&
  92. (t->dev->flags & IFF_UP))
  93. return t;
  94. }
  95. memset(&any, 0, sizeof(any));
  96. hash = HASH(&any, local);
  97. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  98. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  99. (t->dev->flags & IFF_UP))
  100. return t;
  101. }
  102. hash = HASH(remote, &any);
  103. for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  104. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  105. (t->dev->flags & IFF_UP))
  106. return t;
  107. }
  108. t = rcu_dereference(ip6n->tnls_wc[0]);
  109. if (t && (t->dev->flags & IFF_UP))
  110. return t;
  111. return NULL;
  112. }
  113. /**
  114. * vti6_tnl_bucket - get head of list matching given tunnel parameters
  115. * @p: parameters containing tunnel end-points
  116. *
  117. * Description:
  118. * vti6_tnl_bucket() returns the head of the list matching the
  119. * &struct in6_addr entries laddr and raddr in @p.
  120. *
  121. * Return: head of IPv6 tunnel list
  122. **/
  123. static struct ip6_tnl __rcu **
  124. vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
  125. {
  126. const struct in6_addr *remote = &p->raddr;
  127. const struct in6_addr *local = &p->laddr;
  128. unsigned int h = 0;
  129. int prio = 0;
  130. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  131. prio = 1;
  132. h = HASH(remote, local);
  133. }
  134. return &ip6n->tnls[prio][h];
  135. }
  136. static void
  137. vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
  138. {
  139. struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
  140. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  141. rcu_assign_pointer(*tp, t);
  142. }
  143. static void
  144. vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
  145. {
  146. struct ip6_tnl __rcu **tp;
  147. struct ip6_tnl *iter;
  148. for (tp = vti6_tnl_bucket(ip6n, &t->parms);
  149. (iter = rtnl_dereference(*tp)) != NULL;
  150. tp = &iter->next) {
  151. if (t == iter) {
  152. rcu_assign_pointer(*tp, t->next);
  153. break;
  154. }
  155. }
  156. }
  157. static void vti6_dev_free(struct net_device *dev)
  158. {
  159. free_percpu(dev->tstats);
  160. free_netdev(dev);
  161. }
  162. static int vti6_tnl_create2(struct net_device *dev)
  163. {
  164. struct ip6_tnl *t = netdev_priv(dev);
  165. struct net *net = dev_net(dev);
  166. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  167. int err;
  168. dev->rtnl_link_ops = &vti6_link_ops;
  169. err = register_netdevice(dev);
  170. if (err < 0)
  171. goto out;
  172. strcpy(t->parms.name, dev->name);
  173. dev_hold(dev);
  174. vti6_tnl_link(ip6n, t);
  175. return 0;
  176. out:
  177. return err;
  178. }
  179. static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  180. {
  181. struct net_device *dev;
  182. struct ip6_tnl *t;
  183. char name[IFNAMSIZ];
  184. int err;
  185. if (p->name[0]) {
  186. if (!dev_valid_name(p->name))
  187. goto failed;
  188. strlcpy(name, p->name, IFNAMSIZ);
  189. } else {
  190. sprintf(name, "ip6_vti%%d");
  191. }
  192. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
  193. if (!dev)
  194. goto failed;
  195. dev_net_set(dev, net);
  196. t = netdev_priv(dev);
  197. t->parms = *p;
  198. t->net = dev_net(dev);
  199. err = vti6_tnl_create2(dev);
  200. if (err < 0)
  201. goto failed_free;
  202. return t;
  203. failed_free:
  204. vti6_dev_free(dev);
  205. failed:
  206. return NULL;
  207. }
  208. /**
  209. * vti6_locate - find or create tunnel matching given parameters
  210. * @net: network namespace
  211. * @p: tunnel parameters
  212. * @create: != 0 if allowed to create new tunnel if no match found
  213. *
  214. * Description:
  215. * vti6_locate() first tries to locate an existing tunnel
  216. * based on @parms. If this is unsuccessful, but @create is set a new
  217. * tunnel device is created and registered for use.
  218. *
  219. * Return:
  220. * matching tunnel or NULL
  221. **/
  222. static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
  223. int create)
  224. {
  225. const struct in6_addr *remote = &p->raddr;
  226. const struct in6_addr *local = &p->laddr;
  227. struct ip6_tnl __rcu **tp;
  228. struct ip6_tnl *t;
  229. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  230. for (tp = vti6_tnl_bucket(ip6n, p);
  231. (t = rtnl_dereference(*tp)) != NULL;
  232. tp = &t->next) {
  233. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  234. ipv6_addr_equal(remote, &t->parms.raddr)) {
  235. if (create)
  236. return NULL;
  237. return t;
  238. }
  239. }
  240. if (!create)
  241. return NULL;
  242. return vti6_tnl_create(net, p);
  243. }
  244. /**
  245. * vti6_dev_uninit - tunnel device uninitializer
  246. * @dev: the device to be destroyed
  247. *
  248. * Description:
  249. * vti6_dev_uninit() removes tunnel from its list
  250. **/
  251. static void vti6_dev_uninit(struct net_device *dev)
  252. {
  253. struct ip6_tnl *t = netdev_priv(dev);
  254. struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
  255. if (dev == ip6n->fb_tnl_dev)
  256. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  257. else
  258. vti6_tnl_unlink(ip6n, t);
  259. dev_put(dev);
  260. }
  261. static int vti6_rcv(struct sk_buff *skb)
  262. {
  263. struct ip6_tnl *t;
  264. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  265. rcu_read_lock();
  266. t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  267. if (t) {
  268. if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
  269. rcu_read_unlock();
  270. goto discard;
  271. }
  272. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  273. rcu_read_unlock();
  274. return 0;
  275. }
  276. ipv6h = ipv6_hdr(skb);
  277. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  278. t->dev->stats.rx_dropped++;
  279. rcu_read_unlock();
  280. goto discard;
  281. }
  282. rcu_read_unlock();
  283. return xfrm6_rcv_tnl(skb, t);
  284. }
  285. rcu_read_unlock();
  286. return -EINVAL;
  287. discard:
  288. kfree_skb(skb);
  289. return 0;
  290. }
  291. static int vti6_rcv_cb(struct sk_buff *skb, int err)
  292. {
  293. unsigned short family;
  294. struct net_device *dev;
  295. struct pcpu_sw_netstats *tstats;
  296. struct xfrm_state *x;
  297. struct xfrm_mode *inner_mode;
  298. struct ip6_tnl *t = XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6;
  299. u32 orig_mark = skb->mark;
  300. int ret;
  301. if (!t)
  302. return 1;
  303. dev = t->dev;
  304. if (err) {
  305. dev->stats.rx_errors++;
  306. dev->stats.rx_dropped++;
  307. return 0;
  308. }
  309. x = xfrm_input_state(skb);
  310. inner_mode = x->inner_mode;
  311. if (x->sel.family == AF_UNSPEC) {
  312. inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
  313. if (inner_mode == NULL) {
  314. XFRM_INC_STATS(dev_net(skb->dev),
  315. LINUX_MIB_XFRMINSTATEMODEERROR);
  316. return -EINVAL;
  317. }
  318. }
  319. family = inner_mode->afinfo->family;
  320. skb->mark = be32_to_cpu(t->parms.i_key);
  321. ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
  322. skb->mark = orig_mark;
  323. if (!ret)
  324. return -EPERM;
  325. skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
  326. skb->dev = dev;
  327. tstats = this_cpu_ptr(dev->tstats);
  328. u64_stats_update_begin(&tstats->syncp);
  329. tstats->rx_packets++;
  330. tstats->rx_bytes += skb->len;
  331. u64_stats_update_end(&tstats->syncp);
  332. return 0;
  333. }
  334. /**
  335. * vti6_addr_conflict - compare packet addresses to tunnel's own
  336. * @t: the outgoing tunnel device
  337. * @hdr: IPv6 header from the incoming packet
  338. *
  339. * Description:
  340. * Avoid trivial tunneling loop by checking that tunnel exit-point
  341. * doesn't match source of incoming packet.
  342. *
  343. * Return:
  344. * 1 if conflict,
  345. * 0 else
  346. **/
  347. static inline bool
  348. vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  349. {
  350. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  351. }
  352. static bool vti6_state_check(const struct xfrm_state *x,
  353. const struct in6_addr *dst,
  354. const struct in6_addr *src)
  355. {
  356. xfrm_address_t *daddr = (xfrm_address_t *)dst;
  357. xfrm_address_t *saddr = (xfrm_address_t *)src;
  358. /* if there is no transform then this tunnel is not functional.
  359. * Or if the xfrm is not mode tunnel.
  360. */
  361. if (!x || x->props.mode != XFRM_MODE_TUNNEL ||
  362. x->props.family != AF_INET6)
  363. return false;
  364. if (ipv6_addr_any(dst))
  365. return xfrm_addr_equal(saddr, &x->props.saddr, AF_INET6);
  366. if (!xfrm_state_addr_check(x, daddr, saddr, AF_INET6))
  367. return false;
  368. return true;
  369. }
  370. /**
  371. * vti6_xmit - send a packet
  372. * @skb: the outgoing socket buffer
  373. * @dev: the outgoing tunnel device
  374. * @fl: the flow informations for the xfrm_lookup
  375. **/
  376. static int
  377. vti6_xmit(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
  378. {
  379. struct ip6_tnl *t = netdev_priv(dev);
  380. struct net_device_stats *stats = &t->dev->stats;
  381. struct dst_entry *dst = skb_dst(skb);
  382. struct net_device *tdev;
  383. struct xfrm_state *x;
  384. int pkt_len = skb->len;
  385. int err = -1;
  386. int mtu;
  387. if (!dst)
  388. goto tx_err_link_failure;
  389. dst_hold(dst);
  390. dst = xfrm_lookup(t->net, dst, fl, NULL, 0);
  391. if (IS_ERR(dst)) {
  392. err = PTR_ERR(dst);
  393. dst = NULL;
  394. goto tx_err_link_failure;
  395. }
  396. x = dst->xfrm;
  397. if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
  398. goto tx_err_link_failure;
  399. if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
  400. (const struct in6_addr *)&x->id.daddr))
  401. goto tx_err_link_failure;
  402. tdev = dst->dev;
  403. if (tdev == dev) {
  404. stats->collisions++;
  405. net_warn_ratelimited("%s: Local routing loop detected!\n",
  406. t->parms.name);
  407. goto tx_err_dst_release;
  408. }
  409. mtu = dst_mtu(dst);
  410. if (skb->len > mtu) {
  411. skb_dst(skb)->ops->update_pmtu(dst, NULL, skb, mtu);
  412. if (skb->protocol == htons(ETH_P_IPV6)) {
  413. if (mtu < IPV6_MIN_MTU)
  414. mtu = IPV6_MIN_MTU;
  415. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  416. } else {
  417. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  418. htonl(mtu));
  419. }
  420. err = -EMSGSIZE;
  421. goto tx_err_dst_release;
  422. }
  423. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  424. skb_dst_set(skb, dst);
  425. skb->dev = skb_dst(skb)->dev;
  426. err = dst_output(t->net, skb->sk, skb);
  427. if (net_xmit_eval(err) == 0) {
  428. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  429. u64_stats_update_begin(&tstats->syncp);
  430. tstats->tx_bytes += pkt_len;
  431. tstats->tx_packets++;
  432. u64_stats_update_end(&tstats->syncp);
  433. } else {
  434. stats->tx_errors++;
  435. stats->tx_aborted_errors++;
  436. }
  437. return 0;
  438. tx_err_link_failure:
  439. stats->tx_carrier_errors++;
  440. dst_link_failure(skb);
  441. tx_err_dst_release:
  442. dst_release(dst);
  443. return err;
  444. }
  445. static netdev_tx_t
  446. vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  447. {
  448. struct ip6_tnl *t = netdev_priv(dev);
  449. struct net_device_stats *stats = &t->dev->stats;
  450. struct ipv6hdr *ipv6h;
  451. struct flowi fl;
  452. int ret;
  453. memset(&fl, 0, sizeof(fl));
  454. switch (skb->protocol) {
  455. case htons(ETH_P_IPV6):
  456. ipv6h = ipv6_hdr(skb);
  457. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  458. vti6_addr_conflict(t, ipv6h))
  459. goto tx_err;
  460. xfrm_decode_session(skb, &fl, AF_INET6);
  461. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  462. break;
  463. case htons(ETH_P_IP):
  464. xfrm_decode_session(skb, &fl, AF_INET);
  465. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  466. break;
  467. default:
  468. goto tx_err;
  469. }
  470. /* override mark with tunnel output key */
  471. fl.flowi_mark = be32_to_cpu(t->parms.o_key);
  472. ret = vti6_xmit(skb, dev, &fl);
  473. if (ret < 0)
  474. goto tx_err;
  475. return NETDEV_TX_OK;
  476. tx_err:
  477. stats->tx_errors++;
  478. stats->tx_dropped++;
  479. kfree_skb(skb);
  480. return NETDEV_TX_OK;
  481. }
  482. static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  483. u8 type, u8 code, int offset, __be32 info)
  484. {
  485. __be32 spi;
  486. __u32 mark;
  487. struct xfrm_state *x;
  488. struct ip6_tnl *t;
  489. struct ip_esp_hdr *esph;
  490. struct ip_auth_hdr *ah;
  491. struct ip_comp_hdr *ipch;
  492. struct net *net = dev_net(skb->dev);
  493. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  494. int protocol = iph->nexthdr;
  495. t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
  496. if (!t)
  497. return -1;
  498. mark = be32_to_cpu(t->parms.o_key);
  499. switch (protocol) {
  500. case IPPROTO_ESP:
  501. esph = (struct ip_esp_hdr *)(skb->data + offset);
  502. spi = esph->spi;
  503. break;
  504. case IPPROTO_AH:
  505. ah = (struct ip_auth_hdr *)(skb->data + offset);
  506. spi = ah->spi;
  507. break;
  508. case IPPROTO_COMP:
  509. ipch = (struct ip_comp_hdr *)(skb->data + offset);
  510. spi = htonl(ntohs(ipch->cpi));
  511. break;
  512. default:
  513. return 0;
  514. }
  515. if (type != ICMPV6_PKT_TOOBIG &&
  516. type != NDISC_REDIRECT)
  517. return 0;
  518. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  519. spi, protocol, AF_INET6);
  520. if (!x)
  521. return 0;
  522. if (type == NDISC_REDIRECT)
  523. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  524. sock_net_uid(net, NULL));
  525. else
  526. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  527. xfrm_state_put(x);
  528. return 0;
  529. }
  530. static void vti6_link_config(struct ip6_tnl *t)
  531. {
  532. struct net_device *dev = t->dev;
  533. struct __ip6_tnl_parm *p = &t->parms;
  534. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  535. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  536. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  537. IP6_TNL_F_CAP_PER_PACKET);
  538. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  539. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  540. dev->flags |= IFF_POINTOPOINT;
  541. else
  542. dev->flags &= ~IFF_POINTOPOINT;
  543. }
  544. /**
  545. * vti6_tnl_change - update the tunnel parameters
  546. * @t: tunnel to be changed
  547. * @p: tunnel configuration parameters
  548. *
  549. * Description:
  550. * vti6_tnl_change() updates the tunnel parameters
  551. **/
  552. static int
  553. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  554. {
  555. t->parms.laddr = p->laddr;
  556. t->parms.raddr = p->raddr;
  557. t->parms.link = p->link;
  558. t->parms.i_key = p->i_key;
  559. t->parms.o_key = p->o_key;
  560. t->parms.proto = p->proto;
  561. dst_cache_reset(&t->dst_cache);
  562. vti6_link_config(t);
  563. return 0;
  564. }
  565. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  566. {
  567. struct net *net = dev_net(t->dev);
  568. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  569. int err;
  570. vti6_tnl_unlink(ip6n, t);
  571. synchronize_net();
  572. err = vti6_tnl_change(t, p);
  573. vti6_tnl_link(ip6n, t);
  574. netdev_state_change(t->dev);
  575. return err;
  576. }
  577. static void
  578. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  579. {
  580. p->laddr = u->laddr;
  581. p->raddr = u->raddr;
  582. p->link = u->link;
  583. p->i_key = u->i_key;
  584. p->o_key = u->o_key;
  585. p->proto = u->proto;
  586. memcpy(p->name, u->name, sizeof(u->name));
  587. }
  588. static void
  589. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  590. {
  591. u->laddr = p->laddr;
  592. u->raddr = p->raddr;
  593. u->link = p->link;
  594. u->i_key = p->i_key;
  595. u->o_key = p->o_key;
  596. if (u->i_key)
  597. u->i_flags |= GRE_KEY;
  598. if (u->o_key)
  599. u->o_flags |= GRE_KEY;
  600. u->proto = p->proto;
  601. memcpy(u->name, p->name, sizeof(u->name));
  602. }
  603. /**
  604. * vti6_tnl_ioctl - configure vti6 tunnels from userspace
  605. * @dev: virtual device associated with tunnel
  606. * @ifr: parameters passed from userspace
  607. * @cmd: command to be performed
  608. *
  609. * Description:
  610. * vti6_ioctl() is used for managing vti6 tunnels
  611. * from userspace.
  612. *
  613. * The possible commands are the following:
  614. * %SIOCGETTUNNEL: get tunnel parameters for device
  615. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  616. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  617. * %SIOCDELTUNNEL: delete tunnel
  618. *
  619. * The fallback device "ip6_vti0", created during module
  620. * initialization, can be used for creating other tunnel devices.
  621. *
  622. * Return:
  623. * 0 on success,
  624. * %-EFAULT if unable to copy data to or from userspace,
  625. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  626. * %-EINVAL if passed tunnel parameters are invalid,
  627. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  628. * %-ENODEV if attempting to change or delete a nonexisting device
  629. **/
  630. static int
  631. vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  632. {
  633. int err = 0;
  634. struct ip6_tnl_parm2 p;
  635. struct __ip6_tnl_parm p1;
  636. struct ip6_tnl *t = NULL;
  637. struct net *net = dev_net(dev);
  638. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  639. switch (cmd) {
  640. case SIOCGETTUNNEL:
  641. if (dev == ip6n->fb_tnl_dev) {
  642. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  643. err = -EFAULT;
  644. break;
  645. }
  646. vti6_parm_from_user(&p1, &p);
  647. t = vti6_locate(net, &p1, 0);
  648. } else {
  649. memset(&p, 0, sizeof(p));
  650. }
  651. if (!t)
  652. t = netdev_priv(dev);
  653. vti6_parm_to_user(&p, &t->parms);
  654. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  655. err = -EFAULT;
  656. break;
  657. case SIOCADDTUNNEL:
  658. case SIOCCHGTUNNEL:
  659. err = -EPERM;
  660. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  661. break;
  662. err = -EFAULT;
  663. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  664. break;
  665. err = -EINVAL;
  666. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  667. break;
  668. vti6_parm_from_user(&p1, &p);
  669. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  670. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  671. if (t) {
  672. if (t->dev != dev) {
  673. err = -EEXIST;
  674. break;
  675. }
  676. } else
  677. t = netdev_priv(dev);
  678. err = vti6_update(t, &p1);
  679. }
  680. if (t) {
  681. err = 0;
  682. vti6_parm_to_user(&p, &t->parms);
  683. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  684. err = -EFAULT;
  685. } else
  686. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  687. break;
  688. case SIOCDELTUNNEL:
  689. err = -EPERM;
  690. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  691. break;
  692. if (dev == ip6n->fb_tnl_dev) {
  693. err = -EFAULT;
  694. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  695. break;
  696. err = -ENOENT;
  697. vti6_parm_from_user(&p1, &p);
  698. t = vti6_locate(net, &p1, 0);
  699. if (!t)
  700. break;
  701. err = -EPERM;
  702. if (t->dev == ip6n->fb_tnl_dev)
  703. break;
  704. dev = t->dev;
  705. }
  706. err = 0;
  707. unregister_netdevice(dev);
  708. break;
  709. default:
  710. err = -EINVAL;
  711. }
  712. return err;
  713. }
  714. /**
  715. * vti6_tnl_change_mtu - change mtu manually for tunnel device
  716. * @dev: virtual device associated with tunnel
  717. * @new_mtu: the new mtu
  718. *
  719. * Return:
  720. * 0 on success,
  721. * %-EINVAL if mtu too small
  722. **/
  723. static int vti6_change_mtu(struct net_device *dev, int new_mtu)
  724. {
  725. if (new_mtu < IPV6_MIN_MTU)
  726. return -EINVAL;
  727. dev->mtu = new_mtu;
  728. return 0;
  729. }
  730. static const struct net_device_ops vti6_netdev_ops = {
  731. .ndo_init = vti6_dev_init,
  732. .ndo_uninit = vti6_dev_uninit,
  733. .ndo_start_xmit = vti6_tnl_xmit,
  734. .ndo_do_ioctl = vti6_ioctl,
  735. .ndo_change_mtu = vti6_change_mtu,
  736. .ndo_get_stats64 = ip_tunnel_get_stats64,
  737. .ndo_get_iflink = ip6_tnl_get_iflink,
  738. };
  739. /**
  740. * vti6_dev_setup - setup virtual tunnel device
  741. * @dev: virtual device associated with tunnel
  742. *
  743. * Description:
  744. * Initialize function pointers and device parameters
  745. **/
  746. static void vti6_dev_setup(struct net_device *dev)
  747. {
  748. dev->netdev_ops = &vti6_netdev_ops;
  749. dev->destructor = vti6_dev_free;
  750. dev->type = ARPHRD_TUNNEL6;
  751. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
  752. dev->mtu = ETH_DATA_LEN;
  753. dev->flags |= IFF_NOARP;
  754. dev->addr_len = sizeof(struct in6_addr);
  755. netif_keep_dst(dev);
  756. }
  757. /**
  758. * vti6_dev_init_gen - general initializer for all tunnel devices
  759. * @dev: virtual device associated with tunnel
  760. **/
  761. static inline int vti6_dev_init_gen(struct net_device *dev)
  762. {
  763. struct ip6_tnl *t = netdev_priv(dev);
  764. t->dev = dev;
  765. t->net = dev_net(dev);
  766. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  767. if (!dev->tstats)
  768. return -ENOMEM;
  769. return 0;
  770. }
  771. /**
  772. * vti6_dev_init - initializer for all non fallback tunnel devices
  773. * @dev: virtual device associated with tunnel
  774. **/
  775. static int vti6_dev_init(struct net_device *dev)
  776. {
  777. struct ip6_tnl *t = netdev_priv(dev);
  778. int err = vti6_dev_init_gen(dev);
  779. if (err)
  780. return err;
  781. vti6_link_config(t);
  782. return 0;
  783. }
  784. /**
  785. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  786. * @dev: fallback device
  787. *
  788. * Return: 0
  789. **/
  790. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  791. {
  792. struct ip6_tnl *t = netdev_priv(dev);
  793. struct net *net = dev_net(dev);
  794. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  795. t->parms.proto = IPPROTO_IPV6;
  796. dev_hold(dev);
  797. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  798. return 0;
  799. }
  800. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
  801. {
  802. return 0;
  803. }
  804. static void vti6_netlink_parms(struct nlattr *data[],
  805. struct __ip6_tnl_parm *parms)
  806. {
  807. memset(parms, 0, sizeof(*parms));
  808. if (!data)
  809. return;
  810. if (data[IFLA_VTI_LINK])
  811. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  812. if (data[IFLA_VTI_LOCAL])
  813. parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
  814. if (data[IFLA_VTI_REMOTE])
  815. parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
  816. if (data[IFLA_VTI_IKEY])
  817. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  818. if (data[IFLA_VTI_OKEY])
  819. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  820. }
  821. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  822. struct nlattr *tb[], struct nlattr *data[])
  823. {
  824. struct net *net = dev_net(dev);
  825. struct ip6_tnl *nt;
  826. nt = netdev_priv(dev);
  827. vti6_netlink_parms(data, &nt->parms);
  828. nt->parms.proto = IPPROTO_IPV6;
  829. if (vti6_locate(net, &nt->parms, 0))
  830. return -EEXIST;
  831. return vti6_tnl_create2(dev);
  832. }
  833. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  834. {
  835. struct net *net = dev_net(dev);
  836. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  837. if (dev != ip6n->fb_tnl_dev)
  838. unregister_netdevice_queue(dev, head);
  839. }
  840. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  841. struct nlattr *data[])
  842. {
  843. struct ip6_tnl *t;
  844. struct __ip6_tnl_parm p;
  845. struct net *net = dev_net(dev);
  846. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  847. if (dev == ip6n->fb_tnl_dev)
  848. return -EINVAL;
  849. vti6_netlink_parms(data, &p);
  850. t = vti6_locate(net, &p, 0);
  851. if (t) {
  852. if (t->dev != dev)
  853. return -EEXIST;
  854. } else
  855. t = netdev_priv(dev);
  856. return vti6_update(t, &p);
  857. }
  858. static size_t vti6_get_size(const struct net_device *dev)
  859. {
  860. return
  861. /* IFLA_VTI_LINK */
  862. nla_total_size(4) +
  863. /* IFLA_VTI_LOCAL */
  864. nla_total_size(sizeof(struct in6_addr)) +
  865. /* IFLA_VTI_REMOTE */
  866. nla_total_size(sizeof(struct in6_addr)) +
  867. /* IFLA_VTI_IKEY */
  868. nla_total_size(4) +
  869. /* IFLA_VTI_OKEY */
  870. nla_total_size(4) +
  871. 0;
  872. }
  873. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  874. {
  875. struct ip6_tnl *tunnel = netdev_priv(dev);
  876. struct __ip6_tnl_parm *parm = &tunnel->parms;
  877. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  878. nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
  879. nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
  880. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  881. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
  882. goto nla_put_failure;
  883. return 0;
  884. nla_put_failure:
  885. return -EMSGSIZE;
  886. }
  887. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  888. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  889. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  890. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  891. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  892. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  893. };
  894. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  895. .kind = "vti6",
  896. .maxtype = IFLA_VTI_MAX,
  897. .policy = vti6_policy,
  898. .priv_size = sizeof(struct ip6_tnl),
  899. .setup = vti6_dev_setup,
  900. .validate = vti6_validate,
  901. .newlink = vti6_newlink,
  902. .dellink = vti6_dellink,
  903. .changelink = vti6_changelink,
  904. .get_size = vti6_get_size,
  905. .fill_info = vti6_fill_info,
  906. .get_link_net = ip6_tnl_get_link_net,
  907. };
  908. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
  909. {
  910. int h;
  911. struct ip6_tnl *t;
  912. LIST_HEAD(list);
  913. for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
  914. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  915. while (t) {
  916. unregister_netdevice_queue(t->dev, &list);
  917. t = rtnl_dereference(t->next);
  918. }
  919. }
  920. t = rtnl_dereference(ip6n->tnls_wc[0]);
  921. unregister_netdevice_queue(t->dev, &list);
  922. unregister_netdevice_many(&list);
  923. }
  924. static int __net_init vti6_init_net(struct net *net)
  925. {
  926. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  927. struct ip6_tnl *t = NULL;
  928. int err;
  929. ip6n->tnls[0] = ip6n->tnls_wc;
  930. ip6n->tnls[1] = ip6n->tnls_r_l;
  931. err = -ENOMEM;
  932. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  933. NET_NAME_UNKNOWN, vti6_dev_setup);
  934. if (!ip6n->fb_tnl_dev)
  935. goto err_alloc_dev;
  936. dev_net_set(ip6n->fb_tnl_dev, net);
  937. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  938. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  939. if (err < 0)
  940. goto err_register;
  941. err = register_netdev(ip6n->fb_tnl_dev);
  942. if (err < 0)
  943. goto err_register;
  944. t = netdev_priv(ip6n->fb_tnl_dev);
  945. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  946. return 0;
  947. err_register:
  948. vti6_dev_free(ip6n->fb_tnl_dev);
  949. err_alloc_dev:
  950. return err;
  951. }
  952. static void __net_exit vti6_exit_net(struct net *net)
  953. {
  954. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  955. rtnl_lock();
  956. vti6_destroy_tunnels(ip6n);
  957. rtnl_unlock();
  958. }
  959. static struct pernet_operations vti6_net_ops = {
  960. .init = vti6_init_net,
  961. .exit = vti6_exit_net,
  962. .id = &vti6_net_id,
  963. .size = sizeof(struct vti6_net),
  964. };
  965. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  966. .handler = vti6_rcv,
  967. .cb_handler = vti6_rcv_cb,
  968. .err_handler = vti6_err,
  969. .priority = 100,
  970. };
  971. static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
  972. .handler = vti6_rcv,
  973. .cb_handler = vti6_rcv_cb,
  974. .err_handler = vti6_err,
  975. .priority = 100,
  976. };
  977. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  978. .handler = vti6_rcv,
  979. .cb_handler = vti6_rcv_cb,
  980. .err_handler = vti6_err,
  981. .priority = 100,
  982. };
  983. static bool is_vti6_tunnel(const struct net_device *dev)
  984. {
  985. return dev->netdev_ops == &vti6_netdev_ops;
  986. }
  987. static int vti6_device_event(struct notifier_block *unused,
  988. unsigned long event, void *ptr)
  989. {
  990. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  991. struct ip6_tnl *t = netdev_priv(dev);
  992. if (!is_vti6_tunnel(dev))
  993. return NOTIFY_DONE;
  994. switch (event) {
  995. case NETDEV_DOWN:
  996. if (!net_eq(t->net, dev_net(dev)))
  997. xfrm_garbage_collect(t->net);
  998. break;
  999. }
  1000. return NOTIFY_DONE;
  1001. }
  1002. static struct notifier_block vti6_notifier_block __read_mostly = {
  1003. .notifier_call = vti6_device_event,
  1004. };
  1005. /**
  1006. * vti6_tunnel_init - register protocol and reserve needed resources
  1007. *
  1008. * Return: 0 on success
  1009. **/
  1010. static int __init vti6_tunnel_init(void)
  1011. {
  1012. const char *msg;
  1013. int err;
  1014. register_netdevice_notifier(&vti6_notifier_block);
  1015. msg = "tunnel device";
  1016. err = register_pernet_device(&vti6_net_ops);
  1017. if (err < 0)
  1018. goto pernet_dev_failed;
  1019. msg = "tunnel protocols";
  1020. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  1021. if (err < 0)
  1022. goto xfrm_proto_esp_failed;
  1023. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  1024. if (err < 0)
  1025. goto xfrm_proto_ah_failed;
  1026. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1027. if (err < 0)
  1028. goto xfrm_proto_comp_failed;
  1029. msg = "netlink interface";
  1030. err = rtnl_link_register(&vti6_link_ops);
  1031. if (err < 0)
  1032. goto rtnl_link_failed;
  1033. return 0;
  1034. rtnl_link_failed:
  1035. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1036. xfrm_proto_comp_failed:
  1037. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1038. xfrm_proto_ah_failed:
  1039. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1040. xfrm_proto_esp_failed:
  1041. unregister_pernet_device(&vti6_net_ops);
  1042. pernet_dev_failed:
  1043. unregister_netdevice_notifier(&vti6_notifier_block);
  1044. pr_err("vti6 init: failed to register %s\n", msg);
  1045. return err;
  1046. }
  1047. /**
  1048. * vti6_tunnel_cleanup - free resources and unregister protocol
  1049. **/
  1050. static void __exit vti6_tunnel_cleanup(void)
  1051. {
  1052. rtnl_link_unregister(&vti6_link_ops);
  1053. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1054. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1055. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1056. unregister_pernet_device(&vti6_net_ops);
  1057. unregister_netdevice_notifier(&vti6_notifier_block);
  1058. }
  1059. module_init(vti6_tunnel_init);
  1060. module_exit(vti6_tunnel_cleanup);
  1061. MODULE_LICENSE("GPL");
  1062. MODULE_ALIAS_RTNL_LINK("vti6");
  1063. MODULE_ALIAS_NETDEV("ip6_vti0");
  1064. MODULE_AUTHOR("Steffen Klassert");
  1065. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");