ip6_vti.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. ip6_tnl_dst_reset(t);
  244. dev_put(dev);
  245. }
  246. static int vti6_rcv(struct sk_buff *skb)
  247. {
  248. struct ip6_tnl *t;
  249. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  250. rcu_read_lock();
  251. if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
  252. &ipv6h->daddr)) != NULL) {
  253. struct pcpu_sw_netstats *tstats;
  254. if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
  255. rcu_read_unlock();
  256. goto discard;
  257. }
  258. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  259. rcu_read_unlock();
  260. return 0;
  261. }
  262. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
  263. t->dev->stats.rx_dropped++;
  264. rcu_read_unlock();
  265. goto discard;
  266. }
  267. tstats = this_cpu_ptr(t->dev->tstats);
  268. u64_stats_update_begin(&tstats->syncp);
  269. tstats->rx_packets++;
  270. tstats->rx_bytes += skb->len;
  271. u64_stats_update_end(&tstats->syncp);
  272. skb->mark = 0;
  273. secpath_reset(skb);
  274. skb->dev = t->dev;
  275. rcu_read_unlock();
  276. return 0;
  277. }
  278. rcu_read_unlock();
  279. return 1;
  280. discard:
  281. kfree_skb(skb);
  282. return 0;
  283. }
  284. /**
  285. * vti6_addr_conflict - compare packet addresses to tunnel's own
  286. * @t: the outgoing tunnel device
  287. * @hdr: IPv6 header from the incoming packet
  288. *
  289. * Description:
  290. * Avoid trivial tunneling loop by checking that tunnel exit-point
  291. * doesn't match source of incoming packet.
  292. *
  293. * Return:
  294. * 1 if conflict,
  295. * 0 else
  296. **/
  297. static inline bool
  298. vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  299. {
  300. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  301. }
  302. /**
  303. * vti6_xmit - send a packet
  304. * @skb: the outgoing socket buffer
  305. * @dev: the outgoing tunnel device
  306. **/
  307. static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
  308. {
  309. struct net *net = dev_net(dev);
  310. struct ip6_tnl *t = netdev_priv(dev);
  311. struct net_device_stats *stats = &t->dev->stats;
  312. struct dst_entry *dst = NULL, *ndst = NULL;
  313. struct flowi6 fl6;
  314. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  315. struct net_device *tdev;
  316. int err = -1;
  317. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  318. !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
  319. return err;
  320. dst = ip6_tnl_dst_check(t);
  321. if (!dst) {
  322. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  323. ndst = ip6_route_output(net, NULL, &fl6);
  324. if (ndst->error)
  325. goto tx_err_link_failure;
  326. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(&fl6), NULL, 0);
  327. if (IS_ERR(ndst)) {
  328. err = PTR_ERR(ndst);
  329. ndst = NULL;
  330. goto tx_err_link_failure;
  331. }
  332. dst = ndst;
  333. }
  334. if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
  335. goto tx_err_link_failure;
  336. tdev = dst->dev;
  337. if (tdev == dev) {
  338. stats->collisions++;
  339. net_warn_ratelimited("%s: Local routing loop detected!\n",
  340. t->parms.name);
  341. goto tx_err_dst_release;
  342. }
  343. skb_dst_drop(skb);
  344. skb_dst_set_noref(skb, dst);
  345. ip6tunnel_xmit(skb, dev);
  346. if (ndst) {
  347. dev->mtu = dst_mtu(ndst);
  348. ip6_tnl_dst_store(t, ndst);
  349. }
  350. return 0;
  351. tx_err_link_failure:
  352. stats->tx_carrier_errors++;
  353. dst_link_failure(skb);
  354. tx_err_dst_release:
  355. dst_release(ndst);
  356. return err;
  357. }
  358. static netdev_tx_t
  359. vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  360. {
  361. struct ip6_tnl *t = netdev_priv(dev);
  362. struct net_device_stats *stats = &t->dev->stats;
  363. int ret;
  364. switch (skb->protocol) {
  365. case htons(ETH_P_IPV6):
  366. ret = vti6_xmit(skb, dev);
  367. break;
  368. default:
  369. goto tx_err;
  370. }
  371. if (ret < 0)
  372. goto tx_err;
  373. return NETDEV_TX_OK;
  374. tx_err:
  375. stats->tx_errors++;
  376. stats->tx_dropped++;
  377. kfree_skb(skb);
  378. return NETDEV_TX_OK;
  379. }
  380. static void vti6_link_config(struct ip6_tnl *t)
  381. {
  382. struct dst_entry *dst;
  383. struct net_device *dev = t->dev;
  384. struct __ip6_tnl_parm *p = &t->parms;
  385. struct flowi6 *fl6 = &t->fl.u.ip6;
  386. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  387. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  388. /* Set up flowi template */
  389. fl6->saddr = p->laddr;
  390. fl6->daddr = p->raddr;
  391. fl6->flowi6_oif = p->link;
  392. fl6->flowi6_mark = be32_to_cpu(p->i_key);
  393. fl6->flowi6_proto = p->proto;
  394. fl6->flowlabel = 0;
  395. p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
  396. IP6_TNL_F_CAP_PER_PACKET);
  397. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  398. if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
  399. dev->flags |= IFF_POINTOPOINT;
  400. else
  401. dev->flags &= ~IFF_POINTOPOINT;
  402. dev->iflink = p->link;
  403. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  404. dst = ip6_route_output(dev_net(dev), NULL, fl6);
  405. if (dst->error)
  406. return;
  407. dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
  408. NULL, 0);
  409. if (IS_ERR(dst))
  410. return;
  411. if (dst->dev) {
  412. dev->hard_header_len = dst->dev->hard_header_len;
  413. dev->mtu = dst_mtu(dst);
  414. if (dev->mtu < IPV6_MIN_MTU)
  415. dev->mtu = IPV6_MIN_MTU;
  416. }
  417. dst_release(dst);
  418. }
  419. }
  420. /**
  421. * vti6_tnl_change - update the tunnel parameters
  422. * @t: tunnel to be changed
  423. * @p: tunnel configuration parameters
  424. *
  425. * Description:
  426. * vti6_tnl_change() updates the tunnel parameters
  427. **/
  428. static int
  429. vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  430. {
  431. t->parms.laddr = p->laddr;
  432. t->parms.raddr = p->raddr;
  433. t->parms.link = p->link;
  434. t->parms.i_key = p->i_key;
  435. t->parms.o_key = p->o_key;
  436. t->parms.proto = p->proto;
  437. ip6_tnl_dst_reset(t);
  438. vti6_link_config(t);
  439. return 0;
  440. }
  441. static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  442. {
  443. struct net *net = dev_net(t->dev);
  444. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  445. int err;
  446. vti6_tnl_unlink(ip6n, t);
  447. synchronize_net();
  448. err = vti6_tnl_change(t, p);
  449. vti6_tnl_link(ip6n, t);
  450. netdev_state_change(t->dev);
  451. return err;
  452. }
  453. static void
  454. vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
  455. {
  456. p->laddr = u->laddr;
  457. p->raddr = u->raddr;
  458. p->link = u->link;
  459. p->i_key = u->i_key;
  460. p->o_key = u->o_key;
  461. p->proto = u->proto;
  462. memcpy(p->name, u->name, sizeof(u->name));
  463. }
  464. static void
  465. vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
  466. {
  467. u->laddr = p->laddr;
  468. u->raddr = p->raddr;
  469. u->link = p->link;
  470. u->i_key = p->i_key;
  471. u->o_key = p->o_key;
  472. u->proto = p->proto;
  473. memcpy(u->name, p->name, sizeof(u->name));
  474. }
  475. /**
  476. * vti6_tnl_ioctl - configure vti6 tunnels from userspace
  477. * @dev: virtual device associated with tunnel
  478. * @ifr: parameters passed from userspace
  479. * @cmd: command to be performed
  480. *
  481. * Description:
  482. * vti6_ioctl() is used for managing vti6 tunnels
  483. * from userspace.
  484. *
  485. * The possible commands are the following:
  486. * %SIOCGETTUNNEL: get tunnel parameters for device
  487. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  488. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  489. * %SIOCDELTUNNEL: delete tunnel
  490. *
  491. * The fallback device "ip6_vti0", created during module
  492. * initialization, can be used for creating other tunnel devices.
  493. *
  494. * Return:
  495. * 0 on success,
  496. * %-EFAULT if unable to copy data to or from userspace,
  497. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  498. * %-EINVAL if passed tunnel parameters are invalid,
  499. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  500. * %-ENODEV if attempting to change or delete a nonexisting device
  501. **/
  502. static int
  503. vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  504. {
  505. int err = 0;
  506. struct ip6_tnl_parm2 p;
  507. struct __ip6_tnl_parm p1;
  508. struct ip6_tnl *t = NULL;
  509. struct net *net = dev_net(dev);
  510. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  511. switch (cmd) {
  512. case SIOCGETTUNNEL:
  513. if (dev == ip6n->fb_tnl_dev) {
  514. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  515. err = -EFAULT;
  516. break;
  517. }
  518. vti6_parm_from_user(&p1, &p);
  519. t = vti6_locate(net, &p1, 0);
  520. } else {
  521. memset(&p, 0, sizeof(p));
  522. }
  523. if (t == NULL)
  524. t = netdev_priv(dev);
  525. vti6_parm_to_user(&p, &t->parms);
  526. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  527. err = -EFAULT;
  528. break;
  529. case SIOCADDTUNNEL:
  530. case SIOCCHGTUNNEL:
  531. err = -EPERM;
  532. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  533. break;
  534. err = -EFAULT;
  535. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  536. break;
  537. err = -EINVAL;
  538. if (p.proto != IPPROTO_IPV6 && p.proto != 0)
  539. break;
  540. vti6_parm_from_user(&p1, &p);
  541. t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
  542. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  543. if (t != NULL) {
  544. if (t->dev != dev) {
  545. err = -EEXIST;
  546. break;
  547. }
  548. } else
  549. t = netdev_priv(dev);
  550. err = vti6_update(t, &p1);
  551. }
  552. if (t) {
  553. err = 0;
  554. vti6_parm_to_user(&p, &t->parms);
  555. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  556. err = -EFAULT;
  557. } else
  558. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  559. break;
  560. case SIOCDELTUNNEL:
  561. err = -EPERM;
  562. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  563. break;
  564. if (dev == ip6n->fb_tnl_dev) {
  565. err = -EFAULT;
  566. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  567. break;
  568. err = -ENOENT;
  569. vti6_parm_from_user(&p1, &p);
  570. t = vti6_locate(net, &p1, 0);
  571. if (t == NULL)
  572. break;
  573. err = -EPERM;
  574. if (t->dev == ip6n->fb_tnl_dev)
  575. break;
  576. dev = t->dev;
  577. }
  578. err = 0;
  579. unregister_netdevice(dev);
  580. break;
  581. default:
  582. err = -EINVAL;
  583. }
  584. return err;
  585. }
  586. /**
  587. * vti6_tnl_change_mtu - change mtu manually for tunnel device
  588. * @dev: virtual device associated with tunnel
  589. * @new_mtu: the new mtu
  590. *
  591. * Return:
  592. * 0 on success,
  593. * %-EINVAL if mtu too small
  594. **/
  595. static int vti6_change_mtu(struct net_device *dev, int new_mtu)
  596. {
  597. if (new_mtu < IPV6_MIN_MTU)
  598. return -EINVAL;
  599. dev->mtu = new_mtu;
  600. return 0;
  601. }
  602. static const struct net_device_ops vti6_netdev_ops = {
  603. .ndo_uninit = vti6_dev_uninit,
  604. .ndo_start_xmit = vti6_tnl_xmit,
  605. .ndo_do_ioctl = vti6_ioctl,
  606. .ndo_change_mtu = vti6_change_mtu,
  607. .ndo_get_stats64 = ip_tunnel_get_stats64,
  608. };
  609. /**
  610. * vti6_dev_setup - setup virtual tunnel device
  611. * @dev: virtual device associated with tunnel
  612. *
  613. * Description:
  614. * Initialize function pointers and device parameters
  615. **/
  616. static void vti6_dev_setup(struct net_device *dev)
  617. {
  618. struct ip6_tnl *t;
  619. dev->netdev_ops = &vti6_netdev_ops;
  620. dev->destructor = vti6_dev_free;
  621. dev->type = ARPHRD_TUNNEL6;
  622. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
  623. dev->mtu = ETH_DATA_LEN;
  624. t = netdev_priv(dev);
  625. dev->flags |= IFF_NOARP;
  626. dev->addr_len = sizeof(struct in6_addr);
  627. dev->features |= NETIF_F_NETNS_LOCAL;
  628. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  629. }
  630. /**
  631. * vti6_dev_init_gen - general initializer for all tunnel devices
  632. * @dev: virtual device associated with tunnel
  633. **/
  634. static inline int vti6_dev_init_gen(struct net_device *dev)
  635. {
  636. struct ip6_tnl *t = netdev_priv(dev);
  637. int i;
  638. t->dev = dev;
  639. t->net = dev_net(dev);
  640. dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
  641. if (!dev->tstats)
  642. return -ENOMEM;
  643. for_each_possible_cpu(i) {
  644. struct pcpu_sw_netstats *stats;
  645. stats = per_cpu_ptr(dev->tstats, i);
  646. u64_stats_init(&stats->syncp);
  647. }
  648. return 0;
  649. }
  650. /**
  651. * vti6_dev_init - initializer for all non fallback tunnel devices
  652. * @dev: virtual device associated with tunnel
  653. **/
  654. static int vti6_dev_init(struct net_device *dev)
  655. {
  656. struct ip6_tnl *t = netdev_priv(dev);
  657. int err = vti6_dev_init_gen(dev);
  658. if (err)
  659. return err;
  660. vti6_link_config(t);
  661. return 0;
  662. }
  663. /**
  664. * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
  665. * @dev: fallback device
  666. *
  667. * Return: 0
  668. **/
  669. static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
  670. {
  671. struct ip6_tnl *t = netdev_priv(dev);
  672. struct net *net = dev_net(dev);
  673. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  674. int err = vti6_dev_init_gen(dev);
  675. if (err)
  676. return err;
  677. t->parms.proto = IPPROTO_IPV6;
  678. dev_hold(dev);
  679. vti6_link_config(t);
  680. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  681. return 0;
  682. }
  683. static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
  684. {
  685. return 0;
  686. }
  687. static void vti6_netlink_parms(struct nlattr *data[],
  688. struct __ip6_tnl_parm *parms)
  689. {
  690. memset(parms, 0, sizeof(*parms));
  691. if (!data)
  692. return;
  693. if (data[IFLA_VTI_LINK])
  694. parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
  695. if (data[IFLA_VTI_LOCAL])
  696. nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
  697. sizeof(struct in6_addr));
  698. if (data[IFLA_VTI_REMOTE])
  699. nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
  700. sizeof(struct in6_addr));
  701. if (data[IFLA_VTI_IKEY])
  702. parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
  703. if (data[IFLA_VTI_OKEY])
  704. parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
  705. }
  706. static int vti6_newlink(struct net *src_net, struct net_device *dev,
  707. struct nlattr *tb[], struct nlattr *data[])
  708. {
  709. struct net *net = dev_net(dev);
  710. struct ip6_tnl *nt;
  711. nt = netdev_priv(dev);
  712. vti6_netlink_parms(data, &nt->parms);
  713. nt->parms.proto = IPPROTO_IPV6;
  714. if (vti6_locate(net, &nt->parms, 0))
  715. return -EEXIST;
  716. return vti6_tnl_create2(dev);
  717. }
  718. static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
  719. struct nlattr *data[])
  720. {
  721. struct ip6_tnl *t;
  722. struct __ip6_tnl_parm p;
  723. struct net *net = dev_net(dev);
  724. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  725. if (dev == ip6n->fb_tnl_dev)
  726. return -EINVAL;
  727. vti6_netlink_parms(data, &p);
  728. t = vti6_locate(net, &p, 0);
  729. if (t) {
  730. if (t->dev != dev)
  731. return -EEXIST;
  732. } else
  733. t = netdev_priv(dev);
  734. return vti6_update(t, &p);
  735. }
  736. static size_t vti6_get_size(const struct net_device *dev)
  737. {
  738. return
  739. /* IFLA_VTI_LINK */
  740. nla_total_size(4) +
  741. /* IFLA_VTI_LOCAL */
  742. nla_total_size(sizeof(struct in6_addr)) +
  743. /* IFLA_VTI_REMOTE */
  744. nla_total_size(sizeof(struct in6_addr)) +
  745. /* IFLA_VTI_IKEY */
  746. nla_total_size(4) +
  747. /* IFLA_VTI_OKEY */
  748. nla_total_size(4) +
  749. 0;
  750. }
  751. static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
  752. {
  753. struct ip6_tnl *tunnel = netdev_priv(dev);
  754. struct __ip6_tnl_parm *parm = &tunnel->parms;
  755. if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
  756. nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
  757. &parm->laddr) ||
  758. nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
  759. &parm->raddr) ||
  760. nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
  761. nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
  762. goto nla_put_failure;
  763. return 0;
  764. nla_put_failure:
  765. return -EMSGSIZE;
  766. }
  767. static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
  768. [IFLA_VTI_LINK] = { .type = NLA_U32 },
  769. [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
  770. [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
  771. [IFLA_VTI_IKEY] = { .type = NLA_U32 },
  772. [IFLA_VTI_OKEY] = { .type = NLA_U32 },
  773. };
  774. static struct rtnl_link_ops vti6_link_ops __read_mostly = {
  775. .kind = "vti6",
  776. .maxtype = IFLA_VTI_MAX,
  777. .policy = vti6_policy,
  778. .priv_size = sizeof(struct ip6_tnl),
  779. .setup = vti6_dev_setup,
  780. .validate = vti6_validate,
  781. .newlink = vti6_newlink,
  782. .changelink = vti6_changelink,
  783. .get_size = vti6_get_size,
  784. .fill_info = vti6_fill_info,
  785. };
  786. static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
  787. .handler = vti6_rcv,
  788. .priority = 1,
  789. };
  790. static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
  791. {
  792. int h;
  793. struct ip6_tnl *t;
  794. LIST_HEAD(list);
  795. for (h = 0; h < HASH_SIZE; h++) {
  796. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  797. while (t != NULL) {
  798. unregister_netdevice_queue(t->dev, &list);
  799. t = rtnl_dereference(t->next);
  800. }
  801. }
  802. t = rtnl_dereference(ip6n->tnls_wc[0]);
  803. unregister_netdevice_queue(t->dev, &list);
  804. unregister_netdevice_many(&list);
  805. }
  806. static int __net_init vti6_init_net(struct net *net)
  807. {
  808. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  809. struct ip6_tnl *t = NULL;
  810. int err;
  811. ip6n->tnls[0] = ip6n->tnls_wc;
  812. ip6n->tnls[1] = ip6n->tnls_r_l;
  813. err = -ENOMEM;
  814. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
  815. vti6_dev_setup);
  816. if (!ip6n->fb_tnl_dev)
  817. goto err_alloc_dev;
  818. dev_net_set(ip6n->fb_tnl_dev, net);
  819. err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  820. if (err < 0)
  821. goto err_register;
  822. err = register_netdev(ip6n->fb_tnl_dev);
  823. if (err < 0)
  824. goto err_register;
  825. t = netdev_priv(ip6n->fb_tnl_dev);
  826. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  827. return 0;
  828. err_register:
  829. vti6_dev_free(ip6n->fb_tnl_dev);
  830. err_alloc_dev:
  831. return err;
  832. }
  833. static void __net_exit vti6_exit_net(struct net *net)
  834. {
  835. struct vti6_net *ip6n = net_generic(net, vti6_net_id);
  836. rtnl_lock();
  837. vti6_destroy_tunnels(ip6n);
  838. rtnl_unlock();
  839. }
  840. static struct pernet_operations vti6_net_ops = {
  841. .init = vti6_init_net,
  842. .exit = vti6_exit_net,
  843. .id = &vti6_net_id,
  844. .size = sizeof(struct vti6_net),
  845. };
  846. /**
  847. * vti6_tunnel_init - register protocol and reserve needed resources
  848. *
  849. * Return: 0 on success
  850. **/
  851. static int __init vti6_tunnel_init(void)
  852. {
  853. int err;
  854. err = register_pernet_device(&vti6_net_ops);
  855. if (err < 0)
  856. goto out_pernet;
  857. err = xfrm6_mode_tunnel_input_register(&vti6_handler);
  858. if (err < 0) {
  859. pr_err("%s: can't register vti6\n", __func__);
  860. goto out;
  861. }
  862. err = rtnl_link_register(&vti6_link_ops);
  863. if (err < 0)
  864. goto rtnl_link_failed;
  865. return 0;
  866. rtnl_link_failed:
  867. xfrm6_mode_tunnel_input_deregister(&vti6_handler);
  868. out:
  869. unregister_pernet_device(&vti6_net_ops);
  870. out_pernet:
  871. return err;
  872. }
  873. /**
  874. * vti6_tunnel_cleanup - free resources and unregister protocol
  875. **/
  876. static void __exit vti6_tunnel_cleanup(void)
  877. {
  878. rtnl_link_unregister(&vti6_link_ops);
  879. if (xfrm6_mode_tunnel_input_deregister(&vti6_handler))
  880. pr_info("%s: can't deregister vti6\n", __func__);
  881. unregister_pernet_device(&vti6_net_ops);
  882. }
  883. module_init(vti6_tunnel_init);
  884. module_exit(vti6_tunnel_cleanup);
  885. MODULE_LICENSE("GPL");
  886. MODULE_ALIAS_RTNL_LINK("vti6");
  887. MODULE_ALIAS_NETDEV("ip6_vti0");
  888. MODULE_AUTHOR("Steffen Klassert");
  889. MODULE_DESCRIPTION("IPv6 virtual tunnel interface");