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 <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. #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. else
  525. ip6_update_pmtu(skb, net, info, 0, 0);
  526. xfrm_state_put(x);
  527. return 0;
  528. }
  529. static void vti6_link_config(struct ip6_tnl *t)
  530. {
  531. struct net_device *dev = t->dev;
  532. struct __ip6_tnl_parm *p = &t->parms;
  533. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  534. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  535. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  536. IP6_TNL_F_CAP_PER_PACKET);
  537. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  538. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  539. dev->flags |= IFF_POINTOPOINT;
  540. else
  541. dev->flags &= ~IFF_POINTOPOINT;
  542. }
  543. /**
  544. * vti6_tnl_change - update the tunnel parameters
  545. * @t: tunnel to be changed
  546. * @p: tunnel configuration parameters
  547. *
  548. * Description:
  549. * vti6_tnl_change() updates the tunnel parameters
  550. **/
  551. static int
  552. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  553. {
  554. t->parms.laddr = p->laddr;
  555. t->parms.raddr = p->raddr;
  556. t->parms.link = p->link;
  557. t->parms.i_key = p->i_key;
  558. t->parms.o_key = p->o_key;
  559. t->parms.proto = p->proto;
  560. dst_cache_reset(&t->dst_cache);
  561. vti6_link_config(t);
  562. return 0;
  563. }
  564. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  565. {
  566. struct net *net = dev_net(t->dev);
  567. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  568. int err;
  569. vti6_tnl_unlink(ip6n, t);
  570. synchronize_net();
  571. err = vti6_tnl_change(t, p);
  572. vti6_tnl_link(ip6n, t);
  573. netdev_state_change(t->dev);
  574. return err;
  575. }
  576. static void
  577. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  578. {
  579. p->laddr = u->laddr;
  580. p->raddr = u->raddr;
  581. p->link = u->link;
  582. p->i_key = u->i_key;
  583. p->o_key = u->o_key;
  584. p->proto = u->proto;
  585. memcpy(p->name, u->name, sizeof(u->name));
  586. }
  587. static void
  588. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  589. {
  590. u->laddr = p->laddr;
  591. u->raddr = p->raddr;
  592. u->link = p->link;
  593. u->i_key = p->i_key;
  594. u->o_key = p->o_key;
  595. if (u->i_key)
  596. u->i_flags |= GRE_KEY;
  597. if (u->o_key)
  598. u->o_flags |= GRE_KEY;
  599. u->proto = p->proto;
  600. memcpy(u->name, p->name, sizeof(u->name));
  601. }
  602. /**
  603. * vti6_tnl_ioctl - configure vti6 tunnels from userspace
  604. * @dev: virtual device associated with tunnel
  605. * @ifr: parameters passed from userspace
  606. * @cmd: command to be performed
  607. *
  608. * Description:
  609. * vti6_ioctl() is used for managing vti6 tunnels
  610. * from userspace.
  611. *
  612. * The possible commands are the following:
  613. * %SIOCGETTUNNEL: get tunnel parameters for device
  614. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  615. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  616. * %SIOCDELTUNNEL: delete tunnel
  617. *
  618. * The fallback device "ip6_vti0", created during module
  619. * initialization, can be used for creating other tunnel devices.
  620. *
  621. * Return:
  622. * 0 on success,
  623. * %-EFAULT if unable to copy data to or from userspace,
  624. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  625. * %-EINVAL if passed tunnel parameters are invalid,
  626. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  627. * %-ENODEV if attempting to change or delete a nonexisting device
  628. **/
  629. static int
  630. vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  631. {
  632. int err = 0;
  633. struct ip6_tnl_parm2 p;
  634. struct __ip6_tnl_parm p1;
  635. struct ip6_tnl *t = NULL;
  636. struct net *net = dev_net(dev);
  637. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  638. switch (cmd) {
  639. case SIOCGETTUNNEL:
  640. if (dev == ip6n->fb_tnl_dev) {
  641. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  642. err = -EFAULT;
  643. break;
  644. }
  645. vti6_parm_from_user(&p1, &p);
  646. t = vti6_locate(net, &p1, 0);
  647. } else {
  648. memset(&p, 0, sizeof(p));
  649. }
  650. if (!t)
  651. t = netdev_priv(dev);
  652. vti6_parm_to_user(&p, &t->parms);
  653. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  654. err = -EFAULT;
  655. break;
  656. case SIOCADDTUNNEL:
  657. case SIOCCHGTUNNEL:
  658. err = -EPERM;
  659. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  660. break;
  661. err = -EFAULT;
  662. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  663. break;
  664. err = -EINVAL;
  665. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  666. break;
  667. vti6_parm_from_user(&p1, &p);
  668. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  669. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  670. if (t) {
  671. if (t->dev != dev) {
  672. err = -EEXIST;
  673. break;
  674. }
  675. } else
  676. t = netdev_priv(dev);
  677. err = vti6_update(t, &p1);
  678. }
  679. if (t) {
  680. err = 0;
  681. vti6_parm_to_user(&p, &t->parms);
  682. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  683. err = -EFAULT;
  684. } else
  685. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  686. break;
  687. case SIOCDELTUNNEL:
  688. err = -EPERM;
  689. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  690. break;
  691. if (dev == ip6n->fb_tnl_dev) {
  692. err = -EFAULT;
  693. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  694. break;
  695. err = -ENOENT;
  696. vti6_parm_from_user(&p1, &p);
  697. t = vti6_locate(net, &p1, 0);
  698. if (!t)
  699. break;
  700. err = -EPERM;
  701. if (t->dev == ip6n->fb_tnl_dev)
  702. break;
  703. dev = t->dev;
  704. }
  705. err = 0;
  706. unregister_netdevice(dev);
  707. break;
  708. default:
  709. err = -EINVAL;
  710. }
  711. return err;
  712. }
  713. /**
  714. * vti6_tnl_change_mtu - change mtu manually for tunnel device
  715. * @dev: virtual device associated with tunnel
  716. * @new_mtu: the new mtu
  717. *
  718. * Return:
  719. * 0 on success,
  720. * %-EINVAL if mtu too small
  721. **/
  722. static int vti6_change_mtu(struct net_device *dev, int new_mtu)
  723. {
  724. if (new_mtu < IPV6_MIN_MTU)
  725. return -EINVAL;
  726. dev->mtu = new_mtu;
  727. return 0;
  728. }
  729. static const struct net_device_ops vti6_netdev_ops = {
  730. .ndo_init = vti6_dev_init,
  731. .ndo_uninit = vti6_dev_uninit,
  732. .ndo_start_xmit = vti6_tnl_xmit,
  733. .ndo_do_ioctl = vti6_ioctl,
  734. .ndo_change_mtu = vti6_change_mtu,
  735. .ndo_get_stats64 = ip_tunnel_get_stats64,
  736. .ndo_get_iflink = ip6_tnl_get_iflink,
  737. };
  738. /**
  739. * vti6_dev_setup - setup virtual tunnel device
  740. * @dev: virtual device associated with tunnel
  741. *
  742. * Description:
  743. * Initialize function pointers and device parameters
  744. **/
  745. static void vti6_dev_setup(struct net_device *dev)
  746. {
  747. dev->netdev_ops = &vti6_netdev_ops;
  748. dev->destructor = vti6_dev_free;
  749. dev->type = ARPHRD_TUNNEL6;
  750. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
  751. dev->mtu = ETH_DATA_LEN;
  752. dev->flags |= IFF_NOARP;
  753. dev->addr_len = sizeof(struct in6_addr);
  754. netif_keep_dst(dev);
  755. }
  756. /**
  757. * vti6_dev_init_gen - general initializer for all tunnel devices
  758. * @dev: virtual device associated with tunnel
  759. **/
  760. static inline int vti6_dev_init_gen(struct net_device *dev)
  761. {
  762. struct ip6_tnl *t = netdev_priv(dev);
  763. t->dev = dev;
  764. t->net = dev_net(dev);
  765. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  766. if (!dev->tstats)
  767. return -ENOMEM;
  768. return 0;
  769. }
  770. /**
  771. * vti6_dev_init - initializer for all non fallback tunnel devices
  772. * @dev: virtual device associated with tunnel
  773. **/
  774. static int vti6_dev_init(struct net_device *dev)
  775. {
  776. struct ip6_tnl *t = netdev_priv(dev);
  777. int err = vti6_dev_init_gen(dev);
  778. if (err)
  779. return err;
  780. vti6_link_config(t);
  781. return 0;
  782. }
  783. /**
  784. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  785. * @dev: fallback device
  786. *
  787. * Return: 0
  788. **/
  789. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  790. {
  791. struct ip6_tnl *t = netdev_priv(dev);
  792. struct net *net = dev_net(dev);
  793. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  794. t->parms.proto = IPPROTO_IPV6;
  795. dev_hold(dev);
  796. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  797. return 0;
  798. }
  799. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
  800. {
  801. return 0;
  802. }
  803. static void vti6_netlink_parms(struct nlattr *data[],
  804. struct __ip6_tnl_parm *parms)
  805. {
  806. memset(parms, 0, sizeof(*parms));
  807. if (!data)
  808. return;
  809. if (data[IFLA_VTI_LINK])
  810. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  811. if (data[IFLA_VTI_LOCAL])
  812. parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
  813. if (data[IFLA_VTI_REMOTE])
  814. parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
  815. if (data[IFLA_VTI_IKEY])
  816. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  817. if (data[IFLA_VTI_OKEY])
  818. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  819. }
  820. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  821. struct nlattr *tb[], struct nlattr *data[])
  822. {
  823. struct net *net = dev_net(dev);
  824. struct ip6_tnl *nt;
  825. nt = netdev_priv(dev);
  826. vti6_netlink_parms(data, &nt->parms);
  827. nt->parms.proto = IPPROTO_IPV6;
  828. if (vti6_locate(net, &nt->parms, 0))
  829. return -EEXIST;
  830. return vti6_tnl_create2(dev);
  831. }
  832. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  833. {
  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. unregister_netdevice_queue(dev, head);
  838. }
  839. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  840. struct nlattr *data[])
  841. {
  842. struct ip6_tnl *t;
  843. struct __ip6_tnl_parm p;
  844. struct net *net = dev_net(dev);
  845. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  846. if (dev == ip6n->fb_tnl_dev)
  847. return -EINVAL;
  848. vti6_netlink_parms(data, &p);
  849. t = vti6_locate(net, &p, 0);
  850. if (t) {
  851. if (t->dev != dev)
  852. return -EEXIST;
  853. } else
  854. t = netdev_priv(dev);
  855. return vti6_update(t, &p);
  856. }
  857. static size_t vti6_get_size(const struct net_device *dev)
  858. {
  859. return
  860. /* IFLA_VTI_LINK */
  861. nla_total_size(4) +
  862. /* IFLA_VTI_LOCAL */
  863. nla_total_size(sizeof(struct in6_addr)) +
  864. /* IFLA_VTI_REMOTE */
  865. nla_total_size(sizeof(struct in6_addr)) +
  866. /* IFLA_VTI_IKEY */
  867. nla_total_size(4) +
  868. /* IFLA_VTI_OKEY */
  869. nla_total_size(4) +
  870. 0;
  871. }
  872. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  873. {
  874. struct ip6_tnl *tunnel = netdev_priv(dev);
  875. struct __ip6_tnl_parm *parm = &tunnel->parms;
  876. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  877. nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
  878. nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
  879. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  880. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
  881. goto nla_put_failure;
  882. return 0;
  883. nla_put_failure:
  884. return -EMSGSIZE;
  885. }
  886. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  887. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  888. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  889. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  890. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  891. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  892. };
  893. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  894. .kind = "vti6",
  895. .maxtype = IFLA_VTI_MAX,
  896. .policy = vti6_policy,
  897. .priv_size = sizeof(struct ip6_tnl),
  898. .setup = vti6_dev_setup,
  899. .validate = vti6_validate,
  900. .newlink = vti6_newlink,
  901. .dellink = vti6_dellink,
  902. .changelink = vti6_changelink,
  903. .get_size = vti6_get_size,
  904. .fill_info = vti6_fill_info,
  905. .get_link_net = ip6_tnl_get_link_net,
  906. };
  907. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
  908. {
  909. int h;
  910. struct ip6_tnl *t;
  911. LIST_HEAD(list);
  912. for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
  913. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  914. while (t) {
  915. unregister_netdevice_queue(t->dev, &list);
  916. t = rtnl_dereference(t->next);
  917. }
  918. }
  919. t = rtnl_dereference(ip6n->tnls_wc[0]);
  920. unregister_netdevice_queue(t->dev, &list);
  921. unregister_netdevice_many(&list);
  922. }
  923. static int __net_init vti6_init_net(struct net *net)
  924. {
  925. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  926. struct ip6_tnl *t = NULL;
  927. int err;
  928. ip6n->tnls[0] = ip6n->tnls_wc;
  929. ip6n->tnls[1] = ip6n->tnls_r_l;
  930. err = -ENOMEM;
  931. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  932. NET_NAME_UNKNOWN, vti6_dev_setup);
  933. if (!ip6n->fb_tnl_dev)
  934. goto err_alloc_dev;
  935. dev_net_set(ip6n->fb_tnl_dev, net);
  936. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  937. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  938. if (err < 0)
  939. goto err_register;
  940. err = register_netdev(ip6n->fb_tnl_dev);
  941. if (err < 0)
  942. goto err_register;
  943. t = netdev_priv(ip6n->fb_tnl_dev);
  944. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  945. return 0;
  946. err_register:
  947. vti6_dev_free(ip6n->fb_tnl_dev);
  948. err_alloc_dev:
  949. return err;
  950. }
  951. static void __net_exit vti6_exit_net(struct net *net)
  952. {
  953. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  954. rtnl_lock();
  955. vti6_destroy_tunnels(ip6n);
  956. rtnl_unlock();
  957. }
  958. static struct pernet_operations vti6_net_ops = {
  959. .init = vti6_init_net,
  960. .exit = vti6_exit_net,
  961. .id = &vti6_net_id,
  962. .size = sizeof(struct vti6_net),
  963. };
  964. static struct xfrm6_protocol vti_esp6_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_ah6_protocol __read_mostly = {
  971. .handler = vti6_rcv,
  972. .cb_handler = vti6_rcv_cb,
  973. .err_handler = vti6_err,
  974. .priority = 100,
  975. };
  976. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  977. .handler = vti6_rcv,
  978. .cb_handler = vti6_rcv_cb,
  979. .err_handler = vti6_err,
  980. .priority = 100,
  981. };
  982. static bool is_vti6_tunnel(const struct net_device *dev)
  983. {
  984. return dev->netdev_ops == &vti6_netdev_ops;
  985. }
  986. static int vti6_device_event(struct notifier_block *unused,
  987. unsigned long event, void *ptr)
  988. {
  989. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  990. struct ip6_tnl *t = netdev_priv(dev);
  991. if (!is_vti6_tunnel(dev))
  992. return NOTIFY_DONE;
  993. switch (event) {
  994. case NETDEV_DOWN:
  995. if (!net_eq(t->net, dev_net(dev)))
  996. xfrm_garbage_collect(t->net);
  997. break;
  998. }
  999. return NOTIFY_DONE;
  1000. }
  1001. static struct notifier_block vti6_notifier_block __read_mostly = {
  1002. .notifier_call = vti6_device_event,
  1003. };
  1004. /**
  1005. * vti6_tunnel_init - register protocol and reserve needed resources
  1006. *
  1007. * Return: 0 on success
  1008. **/
  1009. static int __init vti6_tunnel_init(void)
  1010. {
  1011. const char *msg;
  1012. int err;
  1013. register_netdevice_notifier(&vti6_notifier_block);
  1014. msg = "tunnel device";
  1015. err = register_pernet_device(&vti6_net_ops);
  1016. if (err < 0)
  1017. goto pernet_dev_failed;
  1018. msg = "tunnel protocols";
  1019. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  1020. if (err < 0)
  1021. goto xfrm_proto_esp_failed;
  1022. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  1023. if (err < 0)
  1024. goto xfrm_proto_ah_failed;
  1025. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1026. if (err < 0)
  1027. goto xfrm_proto_comp_failed;
  1028. msg = "netlink interface";
  1029. err = rtnl_link_register(&vti6_link_ops);
  1030. if (err < 0)
  1031. goto rtnl_link_failed;
  1032. return 0;
  1033. rtnl_link_failed:
  1034. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1035. xfrm_proto_comp_failed:
  1036. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1037. xfrm_proto_ah_failed:
  1038. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1039. xfrm_proto_esp_failed:
  1040. unregister_pernet_device(&vti6_net_ops);
  1041. pernet_dev_failed:
  1042. unregister_netdevice_notifier(&vti6_notifier_block);
  1043. pr_err("vti6 init: failed to register %s\n", msg);
  1044. return err;
  1045. }
  1046. /**
  1047. * vti6_tunnel_cleanup - free resources and unregister protocol
  1048. **/
  1049. static void __exit vti6_tunnel_cleanup(void)
  1050. {
  1051. rtnl_link_unregister(&vti6_link_ops);
  1052. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1053. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1054. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1055. unregister_pernet_device(&vti6_net_ops);
  1056. unregister_netdevice_notifier(&vti6_notifier_block);
  1057. }
  1058. module_init(vti6_tunnel_init);
  1059. module_exit(vti6_tunnel_cleanup);
  1060. MODULE_LICENSE("GPL");
  1061. MODULE_ALIAS_RTNL_LINK("vti6");
  1062. MODULE_ALIAS_NETDEV("ip6_vti0");
  1063. MODULE_AUTHOR("Steffen Klassert");
  1064. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");