ip6_vti.c 24 KB

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