ip6_vti.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. err = register_netdevice(dev);
  169. if (err < 0)
  170. goto out;
  171. strcpy(t->parms.name, dev->name);
  172. dev->rtnl_link_ops = &vti6_link_ops;
  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. vti6_dev_free(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 pkt_len = skb->len;
  381. int err = -1;
  382. int mtu;
  383. if (!dst)
  384. goto tx_err_link_failure;
  385. dst_hold(dst);
  386. dst = xfrm_lookup(t->net, dst, fl, NULL, 0);
  387. if (IS_ERR(dst)) {
  388. err = PTR_ERR(dst);
  389. dst = NULL;
  390. goto tx_err_link_failure;
  391. }
  392. x = dst->xfrm;
  393. if (!vti6_state_check(x, &t->parms.raddr, &t->parms.laddr))
  394. goto tx_err_link_failure;
  395. if (!ip6_tnl_xmit_ctl(t, (const struct in6_addr *)&x->props.saddr,
  396. (const struct in6_addr *)&x->id.daddr))
  397. goto tx_err_link_failure;
  398. tdev = dst->dev;
  399. if (tdev == dev) {
  400. stats->collisions++;
  401. net_warn_ratelimited("%s: Local routing loop detected!\n",
  402. t->parms.name);
  403. goto tx_err_dst_release;
  404. }
  405. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  406. skb_dst_set(skb, dst);
  407. skb->dev = skb_dst(skb)->dev;
  408. mtu = dst_mtu(dst);
  409. if (!skb->ignore_df && skb->len > mtu) {
  410. skb_dst(skb)->ops->update_pmtu(dst, NULL, skb, mtu);
  411. if (skb->protocol == htons(ETH_P_IPV6))
  412. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  413. else
  414. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  415. htonl(mtu));
  416. return -EMSGSIZE;
  417. }
  418. err = dst_output(t->net, skb->sk, skb);
  419. if (net_xmit_eval(err) == 0) {
  420. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  421. u64_stats_update_begin(&tstats->syncp);
  422. tstats->tx_bytes += pkt_len;
  423. tstats->tx_packets++;
  424. u64_stats_update_end(&tstats->syncp);
  425. } else {
  426. stats->tx_errors++;
  427. stats->tx_aborted_errors++;
  428. }
  429. return 0;
  430. tx_err_link_failure:
  431. stats->tx_carrier_errors++;
  432. dst_link_failure(skb);
  433. tx_err_dst_release:
  434. dst_release(dst);
  435. return err;
  436. }
  437. static netdev_tx_t
  438. vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  439. {
  440. struct ip6_tnl *t = netdev_priv(dev);
  441. struct net_device_stats *stats = &t->dev->stats;
  442. struct ipv6hdr *ipv6h;
  443. struct flowi fl;
  444. int ret;
  445. memset(&fl, 0, sizeof(fl));
  446. switch (skb->protocol) {
  447. case htons(ETH_P_IPV6):
  448. ipv6h = ipv6_hdr(skb);
  449. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  450. vti6_addr_conflict(t, ipv6h))
  451. goto tx_err;
  452. xfrm_decode_session(skb, &fl, AF_INET6);
  453. memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
  454. break;
  455. case htons(ETH_P_IP):
  456. xfrm_decode_session(skb, &fl, AF_INET);
  457. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  458. break;
  459. default:
  460. goto tx_err;
  461. }
  462. /* override mark with tunnel output key */
  463. fl.flowi_mark = be32_to_cpu(t->parms.o_key);
  464. ret = vti6_xmit(skb, dev, &fl);
  465. if (ret < 0)
  466. goto tx_err;
  467. return NETDEV_TX_OK;
  468. tx_err:
  469. stats->tx_errors++;
  470. stats->tx_dropped++;
  471. kfree_skb(skb);
  472. return NETDEV_TX_OK;
  473. }
  474. static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  475. u8 type, u8 code, int offset, __be32 info)
  476. {
  477. __be32 spi;
  478. __u32 mark;
  479. struct xfrm_state *x;
  480. struct ip6_tnl *t;
  481. struct ip_esp_hdr *esph;
  482. struct ip_auth_hdr *ah;
  483. struct ip_comp_hdr *ipch;
  484. struct net *net = dev_net(skb->dev);
  485. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  486. int protocol = iph->nexthdr;
  487. t = vti6_tnl_lookup(dev_net(skb->dev), &iph->daddr, &iph->saddr);
  488. if (!t)
  489. return -1;
  490. mark = be32_to_cpu(t->parms.o_key);
  491. switch (protocol) {
  492. case IPPROTO_ESP:
  493. esph = (struct ip_esp_hdr *)(skb->data + offset);
  494. spi = esph->spi;
  495. break;
  496. case IPPROTO_AH:
  497. ah = (struct ip_auth_hdr *)(skb->data + offset);
  498. spi = ah->spi;
  499. break;
  500. case IPPROTO_COMP:
  501. ipch = (struct ip_comp_hdr *)(skb->data + offset);
  502. spi = htonl(ntohs(ipch->cpi));
  503. break;
  504. default:
  505. return 0;
  506. }
  507. if (type != ICMPV6_PKT_TOOBIG &&
  508. type != NDISC_REDIRECT)
  509. return 0;
  510. x = xfrm_state_lookup(net, mark, (const xfrm_address_t *)&iph->daddr,
  511. spi, protocol, AF_INET6);
  512. if (!x)
  513. return 0;
  514. if (type == NDISC_REDIRECT)
  515. ip6_redirect(skb, net, skb->dev->ifindex, 0);
  516. else
  517. ip6_update_pmtu(skb, net, info, 0, 0);
  518. xfrm_state_put(x);
  519. return 0;
  520. }
  521. static void vti6_link_config(struct ip6_tnl *t)
  522. {
  523. struct net_device *dev = t->dev;
  524. struct __ip6_tnl_parm *p = &t->parms;
  525. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  526. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  527. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  528. IP6_TNL_F_CAP_PER_PACKET);
  529. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  530. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  531. dev->flags |= IFF_POINTOPOINT;
  532. else
  533. dev->flags &= ~IFF_POINTOPOINT;
  534. }
  535. /**
  536. * vti6_tnl_change - update the tunnel parameters
  537. * @t: tunnel to be changed
  538. * @p: tunnel configuration parameters
  539. *
  540. * Description:
  541. * vti6_tnl_change() updates the tunnel parameters
  542. **/
  543. static int
  544. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  545. {
  546. t->parms.laddr = p->laddr;
  547. t->parms.raddr = p->raddr;
  548. t->parms.link = p->link;
  549. t->parms.i_key = p->i_key;
  550. t->parms.o_key = p->o_key;
  551. t->parms.proto = p->proto;
  552. dst_cache_reset(&t->dst_cache);
  553. vti6_link_config(t);
  554. return 0;
  555. }
  556. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  557. {
  558. struct net *net = dev_net(t->dev);
  559. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  560. int err;
  561. vti6_tnl_unlink(ip6n, t);
  562. synchronize_net();
  563. err = vti6_tnl_change(t, p);
  564. vti6_tnl_link(ip6n, t);
  565. netdev_state_change(t->dev);
  566. return err;
  567. }
  568. static void
  569. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  570. {
  571. p->laddr = u->laddr;
  572. p->raddr = u->raddr;
  573. p->link = u->link;
  574. p->i_key = u->i_key;
  575. p->o_key = u->o_key;
  576. p->proto = u->proto;
  577. memcpy(p->name, u->name, sizeof(u->name));
  578. }
  579. static void
  580. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  581. {
  582. u->laddr = p->laddr;
  583. u->raddr = p->raddr;
  584. u->link = p->link;
  585. u->i_key = p->i_key;
  586. u->o_key = p->o_key;
  587. if (u->i_key)
  588. u->i_flags |= GRE_KEY;
  589. if (u->o_key)
  590. u->o_flags |= GRE_KEY;
  591. u->proto = p->proto;
  592. memcpy(u->name, p->name, sizeof(u->name));
  593. }
  594. /**
  595. * vti6_tnl_ioctl - configure vti6 tunnels from userspace
  596. * @dev: virtual device associated with tunnel
  597. * @ifr: parameters passed from userspace
  598. * @cmd: command to be performed
  599. *
  600. * Description:
  601. * vti6_ioctl() is used for managing vti6 tunnels
  602. * from userspace.
  603. *
  604. * The possible commands are the following:
  605. * %SIOCGETTUNNEL: get tunnel parameters for device
  606. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  607. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  608. * %SIOCDELTUNNEL: delete tunnel
  609. *
  610. * The fallback device "ip6_vti0", created during module
  611. * initialization, can be used for creating other tunnel devices.
  612. *
  613. * Return:
  614. * 0 on success,
  615. * %-EFAULT if unable to copy data to or from userspace,
  616. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  617. * %-EINVAL if passed tunnel parameters are invalid,
  618. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  619. * %-ENODEV if attempting to change or delete a nonexisting device
  620. **/
  621. static int
  622. vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  623. {
  624. int err = 0;
  625. struct ip6_tnl_parm2 p;
  626. struct __ip6_tnl_parm p1;
  627. struct ip6_tnl *t = NULL;
  628. struct net *net = dev_net(dev);
  629. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  630. switch (cmd) {
  631. case SIOCGETTUNNEL:
  632. if (dev == ip6n->fb_tnl_dev) {
  633. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  634. err = -EFAULT;
  635. break;
  636. }
  637. vti6_parm_from_user(&p1, &p);
  638. t = vti6_locate(net, &p1, 0);
  639. } else {
  640. memset(&p, 0, sizeof(p));
  641. }
  642. if (!t)
  643. t = netdev_priv(dev);
  644. vti6_parm_to_user(&p, &t->parms);
  645. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  646. err = -EFAULT;
  647. break;
  648. case SIOCADDTUNNEL:
  649. case SIOCCHGTUNNEL:
  650. err = -EPERM;
  651. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  652. break;
  653. err = -EFAULT;
  654. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  655. break;
  656. err = -EINVAL;
  657. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  658. break;
  659. vti6_parm_from_user(&p1, &p);
  660. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  661. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  662. if (t) {
  663. if (t->dev != dev) {
  664. err = -EEXIST;
  665. break;
  666. }
  667. } else
  668. t = netdev_priv(dev);
  669. err = vti6_update(t, &p1);
  670. }
  671. if (t) {
  672. err = 0;
  673. vti6_parm_to_user(&p, &t->parms);
  674. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  675. err = -EFAULT;
  676. } else
  677. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  678. break;
  679. case SIOCDELTUNNEL:
  680. err = -EPERM;
  681. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  682. break;
  683. if (dev == ip6n->fb_tnl_dev) {
  684. err = -EFAULT;
  685. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  686. break;
  687. err = -ENOENT;
  688. vti6_parm_from_user(&p1, &p);
  689. t = vti6_locate(net, &p1, 0);
  690. if (!t)
  691. break;
  692. err = -EPERM;
  693. if (t->dev == ip6n->fb_tnl_dev)
  694. break;
  695. dev = t->dev;
  696. }
  697. err = 0;
  698. unregister_netdevice(dev);
  699. break;
  700. default:
  701. err = -EINVAL;
  702. }
  703. return err;
  704. }
  705. /**
  706. * vti6_tnl_change_mtu - change mtu manually for tunnel device
  707. * @dev: virtual device associated with tunnel
  708. * @new_mtu: the new mtu
  709. *
  710. * Return:
  711. * 0 on success,
  712. * %-EINVAL if mtu too small
  713. **/
  714. static int vti6_change_mtu(struct net_device *dev, int new_mtu)
  715. {
  716. if (new_mtu < IPV6_MIN_MTU)
  717. return -EINVAL;
  718. dev->mtu = new_mtu;
  719. return 0;
  720. }
  721. static const struct net_device_ops vti6_netdev_ops = {
  722. .ndo_init = vti6_dev_init,
  723. .ndo_uninit = vti6_dev_uninit,
  724. .ndo_start_xmit = vti6_tnl_xmit,
  725. .ndo_do_ioctl = vti6_ioctl,
  726. .ndo_change_mtu = vti6_change_mtu,
  727. .ndo_get_stats64 = ip_tunnel_get_stats64,
  728. .ndo_get_iflink = ip6_tnl_get_iflink,
  729. };
  730. /**
  731. * vti6_dev_setup - setup virtual tunnel device
  732. * @dev: virtual device associated with tunnel
  733. *
  734. * Description:
  735. * Initialize function pointers and device parameters
  736. **/
  737. static void vti6_dev_setup(struct net_device *dev)
  738. {
  739. dev->netdev_ops = &vti6_netdev_ops;
  740. dev->destructor = vti6_dev_free;
  741. dev->type = ARPHRD_TUNNEL6;
  742. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
  743. dev->mtu = ETH_DATA_LEN;
  744. dev->flags |= IFF_NOARP;
  745. dev->addr_len = sizeof(struct in6_addr);
  746. netif_keep_dst(dev);
  747. }
  748. /**
  749. * vti6_dev_init_gen - general initializer for all tunnel devices
  750. * @dev: virtual device associated with tunnel
  751. **/
  752. static inline int vti6_dev_init_gen(struct net_device *dev)
  753. {
  754. struct ip6_tnl *t = netdev_priv(dev);
  755. t->dev = dev;
  756. t->net = dev_net(dev);
  757. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  758. if (!dev->tstats)
  759. return -ENOMEM;
  760. return 0;
  761. }
  762. /**
  763. * vti6_dev_init - initializer for all non fallback tunnel devices
  764. * @dev: virtual device associated with tunnel
  765. **/
  766. static int vti6_dev_init(struct net_device *dev)
  767. {
  768. struct ip6_tnl *t = netdev_priv(dev);
  769. int err = vti6_dev_init_gen(dev);
  770. if (err)
  771. return err;
  772. vti6_link_config(t);
  773. return 0;
  774. }
  775. /**
  776. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  777. * @dev: fallback device
  778. *
  779. * Return: 0
  780. **/
  781. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  782. {
  783. struct ip6_tnl *t = netdev_priv(dev);
  784. struct net *net = dev_net(dev);
  785. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  786. t->parms.proto = IPPROTO_IPV6;
  787. dev_hold(dev);
  788. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  789. return 0;
  790. }
  791. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
  792. {
  793. return 0;
  794. }
  795. static void vti6_netlink_parms(struct nlattr *data[],
  796. struct __ip6_tnl_parm *parms)
  797. {
  798. memset(parms, 0, sizeof(*parms));
  799. if (!data)
  800. return;
  801. if (data[IFLA_VTI_LINK])
  802. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  803. if (data[IFLA_VTI_LOCAL])
  804. parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
  805. if (data[IFLA_VTI_REMOTE])
  806. parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
  807. if (data[IFLA_VTI_IKEY])
  808. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  809. if (data[IFLA_VTI_OKEY])
  810. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  811. }
  812. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  813. struct nlattr *tb[], struct nlattr *data[])
  814. {
  815. struct net *net = dev_net(dev);
  816. struct ip6_tnl *nt;
  817. nt = netdev_priv(dev);
  818. vti6_netlink_parms(data, &nt->parms);
  819. nt->parms.proto = IPPROTO_IPV6;
  820. if (vti6_locate(net, &nt->parms, 0))
  821. return -EEXIST;
  822. return vti6_tnl_create2(dev);
  823. }
  824. static void vti6_dellink(struct net_device *dev, struct list_head *head)
  825. {
  826. struct net *net = dev_net(dev);
  827. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  828. if (dev != ip6n->fb_tnl_dev)
  829. unregister_netdevice_queue(dev, head);
  830. }
  831. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  832. struct nlattr *data[])
  833. {
  834. struct ip6_tnl *t;
  835. struct __ip6_tnl_parm p;
  836. struct net *net = dev_net(dev);
  837. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  838. if (dev == ip6n->fb_tnl_dev)
  839. return -EINVAL;
  840. vti6_netlink_parms(data, &p);
  841. t = vti6_locate(net, &p, 0);
  842. if (t) {
  843. if (t->dev != dev)
  844. return -EEXIST;
  845. } else
  846. t = netdev_priv(dev);
  847. return vti6_update(t, &p);
  848. }
  849. static size_t vti6_get_size(const struct net_device *dev)
  850. {
  851. return
  852. /* IFLA_VTI_LINK */
  853. nla_total_size(4) +
  854. /* IFLA_VTI_LOCAL */
  855. nla_total_size(sizeof(struct in6_addr)) +
  856. /* IFLA_VTI_REMOTE */
  857. nla_total_size(sizeof(struct in6_addr)) +
  858. /* IFLA_VTI_IKEY */
  859. nla_total_size(4) +
  860. /* IFLA_VTI_OKEY */
  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. goto nla_put_failure;
  874. return 0;
  875. nla_put_failure:
  876. return -EMSGSIZE;
  877. }
  878. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  879. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  880. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  881. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  882. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  883. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  884. };
  885. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  886. .kind = "vti6",
  887. .maxtype = IFLA_VTI_MAX,
  888. .policy = vti6_policy,
  889. .priv_size = sizeof(struct ip6_tnl),
  890. .setup = vti6_dev_setup,
  891. .validate = vti6_validate,
  892. .newlink = vti6_newlink,
  893. .dellink = vti6_dellink,
  894. .changelink = vti6_changelink,
  895. .get_size = vti6_get_size,
  896. .fill_info = vti6_fill_info,
  897. .get_link_net = ip6_tnl_get_link_net,
  898. };
  899. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
  900. {
  901. int h;
  902. struct ip6_tnl *t;
  903. LIST_HEAD(list);
  904. for (h = 0; h < IP6_VTI_HASH_SIZE; h++) {
  905. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  906. while (t) {
  907. unregister_netdevice_queue(t->dev, &list);
  908. t = rtnl_dereference(t->next);
  909. }
  910. }
  911. t = rtnl_dereference(ip6n->tnls_wc[0]);
  912. unregister_netdevice_queue(t->dev, &list);
  913. unregister_netdevice_many(&list);
  914. }
  915. static int __net_init vti6_init_net(struct net *net)
  916. {
  917. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  918. struct ip6_tnl *t = NULL;
  919. int err;
  920. ip6n->tnls[0] = ip6n->tnls_wc;
  921. ip6n->tnls[1] = ip6n->tnls_r_l;
  922. err = -ENOMEM;
  923. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  924. NET_NAME_UNKNOWN, vti6_dev_setup);
  925. if (!ip6n->fb_tnl_dev)
  926. goto err_alloc_dev;
  927. dev_net_set(ip6n->fb_tnl_dev, net);
  928. ip6n->fb_tnl_dev->rtnl_link_ops = &vti6_link_ops;
  929. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  930. if (err < 0)
  931. goto err_register;
  932. err = register_netdev(ip6n->fb_tnl_dev);
  933. if (err < 0)
  934. goto err_register;
  935. t = netdev_priv(ip6n->fb_tnl_dev);
  936. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  937. return 0;
  938. err_register:
  939. vti6_dev_free(ip6n->fb_tnl_dev);
  940. err_alloc_dev:
  941. return err;
  942. }
  943. static void __net_exit vti6_exit_net(struct net *net)
  944. {
  945. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  946. rtnl_lock();
  947. vti6_destroy_tunnels(ip6n);
  948. rtnl_unlock();
  949. }
  950. static struct pernet_operations vti6_net_ops = {
  951. .init = vti6_init_net,
  952. .exit = vti6_exit_net,
  953. .id = &vti6_net_id,
  954. .size = sizeof(struct vti6_net),
  955. };
  956. static struct xfrm6_protocol vti_esp6_protocol __read_mostly = {
  957. .handler = vti6_rcv,
  958. .cb_handler = vti6_rcv_cb,
  959. .err_handler = vti6_err,
  960. .priority = 100,
  961. };
  962. static struct xfrm6_protocol vti_ah6_protocol __read_mostly = {
  963. .handler = vti6_rcv,
  964. .cb_handler = vti6_rcv_cb,
  965. .err_handler = vti6_err,
  966. .priority = 100,
  967. };
  968. static struct xfrm6_protocol vti_ipcomp6_protocol __read_mostly = {
  969. .handler = vti6_rcv,
  970. .cb_handler = vti6_rcv_cb,
  971. .err_handler = vti6_err,
  972. .priority = 100,
  973. };
  974. static bool is_vti6_tunnel(const struct net_device *dev)
  975. {
  976. return dev->netdev_ops == &vti6_netdev_ops;
  977. }
  978. static int vti6_device_event(struct notifier_block *unused,
  979. unsigned long event, void *ptr)
  980. {
  981. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  982. struct ip6_tnl *t = netdev_priv(dev);
  983. if (!is_vti6_tunnel(dev))
  984. return NOTIFY_DONE;
  985. switch (event) {
  986. case NETDEV_DOWN:
  987. if (!net_eq(t->net, dev_net(dev)))
  988. xfrm_garbage_collect(t->net);
  989. break;
  990. }
  991. return NOTIFY_DONE;
  992. }
  993. static struct notifier_block vti6_notifier_block __read_mostly = {
  994. .notifier_call = vti6_device_event,
  995. };
  996. /**
  997. * vti6_tunnel_init - register protocol and reserve needed resources
  998. *
  999. * Return: 0 on success
  1000. **/
  1001. static int __init vti6_tunnel_init(void)
  1002. {
  1003. const char *msg;
  1004. int err;
  1005. register_netdevice_notifier(&vti6_notifier_block);
  1006. msg = "tunnel device";
  1007. err = register_pernet_device(&vti6_net_ops);
  1008. if (err < 0)
  1009. goto pernet_dev_failed;
  1010. msg = "tunnel protocols";
  1011. err = xfrm6_protocol_register(&vti_esp6_protocol, IPPROTO_ESP);
  1012. if (err < 0)
  1013. goto xfrm_proto_esp_failed;
  1014. err = xfrm6_protocol_register(&vti_ah6_protocol, IPPROTO_AH);
  1015. if (err < 0)
  1016. goto xfrm_proto_ah_failed;
  1017. err = xfrm6_protocol_register(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1018. if (err < 0)
  1019. goto xfrm_proto_comp_failed;
  1020. msg = "netlink interface";
  1021. err = rtnl_link_register(&vti6_link_ops);
  1022. if (err < 0)
  1023. goto rtnl_link_failed;
  1024. return 0;
  1025. rtnl_link_failed:
  1026. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1027. xfrm_proto_comp_failed:
  1028. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1029. xfrm_proto_ah_failed:
  1030. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1031. xfrm_proto_esp_failed:
  1032. unregister_pernet_device(&vti6_net_ops);
  1033. pernet_dev_failed:
  1034. unregister_netdevice_notifier(&vti6_notifier_block);
  1035. pr_err("vti6 init: failed to register %s\n", msg);
  1036. return err;
  1037. }
  1038. /**
  1039. * vti6_tunnel_cleanup - free resources and unregister protocol
  1040. **/
  1041. static void __exit vti6_tunnel_cleanup(void)
  1042. {
  1043. rtnl_link_unregister(&vti6_link_ops);
  1044. xfrm6_protocol_deregister(&vti_ipcomp6_protocol, IPPROTO_COMP);
  1045. xfrm6_protocol_deregister(&vti_ah6_protocol, IPPROTO_AH);
  1046. xfrm6_protocol_deregister(&vti_esp6_protocol, IPPROTO_ESP);
  1047. unregister_pernet_device(&vti6_net_ops);
  1048. unregister_netdevice_notifier(&vti6_notifier_block);
  1049. }
  1050. module_init(vti6_tunnel_init);
  1051. module_exit(vti6_tunnel_cleanup);
  1052. MODULE_LICENSE("GPL");
  1053. MODULE_ALIAS_RTNL_LINK("vti6");
  1054. MODULE_ALIAS_NETDEV("ip6_vti0");
  1055. MODULE_AUTHOR("Steffen Klassert");
  1056. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");