ip6_vti.c 27 KB

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