ip6_vti.c 28 KB

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