ip6_tunnel.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. /*
  2. * IPv6 tunneling device
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Ville Nuorvala <vnuorval@tcs.hut.fi>
  7. * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
  8. *
  9. * Based on:
  10. * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
  11. *
  12. * RFC 2473
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/sockios.h>
  25. #include <linux/icmp.h>
  26. #include <linux/if.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/if_tunnel.h>
  30. #include <linux/net.h>
  31. #include <linux/in6.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/if_arp.h>
  34. #include <linux/icmpv6.h>
  35. #include <linux/init.h>
  36. #include <linux/route.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <linux/slab.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/atomic.h>
  42. #include <net/icmp.h>
  43. #include <net/ip.h>
  44. #include <net/ipv6.h>
  45. #include <net/ip6_route.h>
  46. #include <net/addrconf.h>
  47. #include <net/ip6_tunnel.h>
  48. #include <net/xfrm.h>
  49. #include <net/dsfield.h>
  50. #include <net/inet_ecn.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. MODULE_AUTHOR("Ville Nuorvala");
  54. MODULE_DESCRIPTION("IPv6 tunneling device");
  55. MODULE_LICENSE("GPL");
  56. #define IPV6_TLV_TEL_DST_SIZE 8
  57. #ifdef IP6_TNL_DEBUG
  58. #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __func__)
  59. #else
  60. #define IP6_TNL_TRACE(x...) do {;} while(0)
  61. #endif
  62. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  63. #define IPV6_TCLASS_SHIFT 20
  64. #define HASH_SIZE 32
  65. #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
  66. (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
  67. (HASH_SIZE - 1))
  68. static void ip6_tnl_dev_init(struct net_device *dev);
  69. static void ip6_tnl_dev_setup(struct net_device *dev);
  70. static int ip6_tnl_net_id __read_mostly;
  71. struct ip6_tnl_net {
  72. /* the IPv6 tunnel fallback device */
  73. struct net_device *fb_tnl_dev;
  74. /* lists for storing tunnels in use */
  75. struct ip6_tnl *tnls_r_l[HASH_SIZE];
  76. struct ip6_tnl *tnls_wc[1];
  77. struct ip6_tnl **tnls[2];
  78. };
  79. /*
  80. * Locking : hash tables are protected by RCU and a spinlock
  81. */
  82. static DEFINE_SPINLOCK(ip6_tnl_lock);
  83. static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
  84. {
  85. struct dst_entry *dst = t->dst_cache;
  86. if (dst && dst->obsolete &&
  87. dst->ops->check(dst, t->dst_cookie) == NULL) {
  88. t->dst_cache = NULL;
  89. dst_release(dst);
  90. return NULL;
  91. }
  92. return dst;
  93. }
  94. static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
  95. {
  96. dst_release(t->dst_cache);
  97. t->dst_cache = NULL;
  98. }
  99. static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
  100. {
  101. struct rt6_info *rt = (struct rt6_info *) dst;
  102. t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
  103. dst_release(t->dst_cache);
  104. t->dst_cache = dst;
  105. }
  106. /**
  107. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  108. * @remote: the address of the tunnel exit-point
  109. * @local: the address of the tunnel entry-point
  110. *
  111. * Return:
  112. * tunnel matching given end-points if found,
  113. * else fallback tunnel if its device is up,
  114. * else %NULL
  115. **/
  116. #define for_each_ip6_tunnel_rcu(start) \
  117. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  118. static struct ip6_tnl *
  119. ip6_tnl_lookup(struct net *net, struct in6_addr *remote, struct in6_addr *local)
  120. {
  121. unsigned h0 = HASH(remote);
  122. unsigned h1 = HASH(local);
  123. struct ip6_tnl *t;
  124. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  125. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[h0 ^ h1]) {
  126. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  127. ipv6_addr_equal(remote, &t->parms.raddr) &&
  128. (t->dev->flags & IFF_UP))
  129. return t;
  130. }
  131. t = rcu_dereference(ip6n->tnls_wc[0]);
  132. if (t && (t->dev->flags & IFF_UP))
  133. return t;
  134. return NULL;
  135. }
  136. /**
  137. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  138. * @p: parameters containing tunnel end-points
  139. *
  140. * Description:
  141. * ip6_tnl_bucket() returns the head of the list matching the
  142. * &struct in6_addr entries laddr and raddr in @p.
  143. *
  144. * Return: head of IPv6 tunnel list
  145. **/
  146. static struct ip6_tnl **
  147. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, struct ip6_tnl_parm *p)
  148. {
  149. struct in6_addr *remote = &p->raddr;
  150. struct in6_addr *local = &p->laddr;
  151. unsigned h = 0;
  152. int prio = 0;
  153. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  154. prio = 1;
  155. h = HASH(remote) ^ HASH(local);
  156. }
  157. return &ip6n->tnls[prio][h];
  158. }
  159. /**
  160. * ip6_tnl_link - add tunnel to hash table
  161. * @t: tunnel to be added
  162. **/
  163. static void
  164. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  165. {
  166. struct ip6_tnl **tp = ip6_tnl_bucket(ip6n, &t->parms);
  167. spin_lock_bh(&ip6_tnl_lock);
  168. t->next = *tp;
  169. rcu_assign_pointer(*tp, t);
  170. spin_unlock_bh(&ip6_tnl_lock);
  171. }
  172. /**
  173. * ip6_tnl_unlink - remove tunnel from hash table
  174. * @t: tunnel to be removed
  175. **/
  176. static void
  177. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  178. {
  179. struct ip6_tnl **tp;
  180. for (tp = ip6_tnl_bucket(ip6n, &t->parms); *tp; tp = &(*tp)->next) {
  181. if (t == *tp) {
  182. spin_lock_bh(&ip6_tnl_lock);
  183. *tp = t->next;
  184. spin_unlock_bh(&ip6_tnl_lock);
  185. break;
  186. }
  187. }
  188. }
  189. /**
  190. * ip6_tnl_create() - create a new tunnel
  191. * @p: tunnel parameters
  192. * @pt: pointer to new tunnel
  193. *
  194. * Description:
  195. * Create tunnel matching given parameters.
  196. *
  197. * Return:
  198. * created tunnel or NULL
  199. **/
  200. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct ip6_tnl_parm *p)
  201. {
  202. struct net_device *dev;
  203. struct ip6_tnl *t;
  204. char name[IFNAMSIZ];
  205. int err;
  206. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  207. if (p->name[0])
  208. strlcpy(name, p->name, IFNAMSIZ);
  209. else
  210. sprintf(name, "ip6tnl%%d");
  211. dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
  212. if (dev == NULL)
  213. goto failed;
  214. dev_net_set(dev, net);
  215. if (strchr(name, '%')) {
  216. if (dev_alloc_name(dev, name) < 0)
  217. goto failed_free;
  218. }
  219. t = netdev_priv(dev);
  220. t->parms = *p;
  221. ip6_tnl_dev_init(dev);
  222. if ((err = register_netdevice(dev)) < 0)
  223. goto failed_free;
  224. dev_hold(dev);
  225. ip6_tnl_link(ip6n, t);
  226. return t;
  227. failed_free:
  228. free_netdev(dev);
  229. failed:
  230. return NULL;
  231. }
  232. /**
  233. * ip6_tnl_locate - find or create tunnel matching given parameters
  234. * @p: tunnel parameters
  235. * @create: != 0 if allowed to create new tunnel if no match found
  236. *
  237. * Description:
  238. * ip6_tnl_locate() first tries to locate an existing tunnel
  239. * based on @parms. If this is unsuccessful, but @create is set a new
  240. * tunnel device is created and registered for use.
  241. *
  242. * Return:
  243. * matching tunnel or NULL
  244. **/
  245. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  246. struct ip6_tnl_parm *p, int create)
  247. {
  248. struct in6_addr *remote = &p->raddr;
  249. struct in6_addr *local = &p->laddr;
  250. struct ip6_tnl *t;
  251. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  252. for (t = *ip6_tnl_bucket(ip6n, p); t; t = t->next) {
  253. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  254. ipv6_addr_equal(remote, &t->parms.raddr))
  255. return t;
  256. }
  257. if (!create)
  258. return NULL;
  259. return ip6_tnl_create(net, p);
  260. }
  261. /**
  262. * ip6_tnl_dev_uninit - tunnel device uninitializer
  263. * @dev: the device to be destroyed
  264. *
  265. * Description:
  266. * ip6_tnl_dev_uninit() removes tunnel from its list
  267. **/
  268. static void
  269. ip6_tnl_dev_uninit(struct net_device *dev)
  270. {
  271. struct ip6_tnl *t = netdev_priv(dev);
  272. struct net *net = dev_net(dev);
  273. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  274. if (dev == ip6n->fb_tnl_dev) {
  275. spin_lock_bh(&ip6_tnl_lock);
  276. ip6n->tnls_wc[0] = NULL;
  277. spin_unlock_bh(&ip6_tnl_lock);
  278. } else {
  279. ip6_tnl_unlink(ip6n, t);
  280. }
  281. ip6_tnl_dst_reset(t);
  282. dev_put(dev);
  283. }
  284. /**
  285. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  286. * @skb: received socket buffer
  287. *
  288. * Return:
  289. * 0 if none was found,
  290. * else index to encapsulation limit
  291. **/
  292. static __u16
  293. parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
  294. {
  295. struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
  296. __u8 nexthdr = ipv6h->nexthdr;
  297. __u16 off = sizeof (*ipv6h);
  298. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  299. __u16 optlen = 0;
  300. struct ipv6_opt_hdr *hdr;
  301. if (raw + off + sizeof (*hdr) > skb->data &&
  302. !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
  303. break;
  304. hdr = (struct ipv6_opt_hdr *) (raw + off);
  305. if (nexthdr == NEXTHDR_FRAGMENT) {
  306. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  307. if (frag_hdr->frag_off)
  308. break;
  309. optlen = 8;
  310. } else if (nexthdr == NEXTHDR_AUTH) {
  311. optlen = (hdr->hdrlen + 2) << 2;
  312. } else {
  313. optlen = ipv6_optlen(hdr);
  314. }
  315. if (nexthdr == NEXTHDR_DEST) {
  316. __u16 i = off + 2;
  317. while (1) {
  318. struct ipv6_tlv_tnl_enc_lim *tel;
  319. /* No more room for encapsulation limit */
  320. if (i + sizeof (*tel) > off + optlen)
  321. break;
  322. tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
  323. /* return index of option if found and valid */
  324. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  325. tel->length == 1)
  326. return i;
  327. /* else jump to next option */
  328. if (tel->type)
  329. i += tel->length + 2;
  330. else
  331. i++;
  332. }
  333. }
  334. nexthdr = hdr->nexthdr;
  335. off += optlen;
  336. }
  337. return 0;
  338. }
  339. /**
  340. * ip6_tnl_err - tunnel error handler
  341. *
  342. * Description:
  343. * ip6_tnl_err() should handle errors in the tunnel according
  344. * to the specifications in RFC 2473.
  345. **/
  346. static int
  347. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  348. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  349. {
  350. struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
  351. struct ip6_tnl *t;
  352. int rel_msg = 0;
  353. u8 rel_type = ICMPV6_DEST_UNREACH;
  354. u8 rel_code = ICMPV6_ADDR_UNREACH;
  355. __u32 rel_info = 0;
  356. __u16 len;
  357. int err = -ENOENT;
  358. /* If the packet doesn't contain the original IPv6 header we are
  359. in trouble since we might need the source address for further
  360. processing of the error. */
  361. rcu_read_lock();
  362. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
  363. &ipv6h->saddr)) == NULL)
  364. goto out;
  365. if (t->parms.proto != ipproto && t->parms.proto != 0)
  366. goto out;
  367. err = 0;
  368. switch (*type) {
  369. __u32 teli;
  370. struct ipv6_tlv_tnl_enc_lim *tel;
  371. __u32 mtu;
  372. case ICMPV6_DEST_UNREACH:
  373. if (net_ratelimit())
  374. printk(KERN_WARNING
  375. "%s: Path to destination invalid "
  376. "or inactive!\n", t->parms.name);
  377. rel_msg = 1;
  378. break;
  379. case ICMPV6_TIME_EXCEED:
  380. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  381. if (net_ratelimit())
  382. printk(KERN_WARNING
  383. "%s: Too small hop limit or "
  384. "routing loop in tunnel!\n",
  385. t->parms.name);
  386. rel_msg = 1;
  387. }
  388. break;
  389. case ICMPV6_PARAMPROB:
  390. teli = 0;
  391. if ((*code) == ICMPV6_HDR_FIELD)
  392. teli = parse_tlv_tnl_enc_lim(skb, skb->data);
  393. if (teli && teli == *info - 2) {
  394. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  395. if (tel->encap_limit == 0) {
  396. if (net_ratelimit())
  397. printk(KERN_WARNING
  398. "%s: Too small encapsulation "
  399. "limit or routing loop in "
  400. "tunnel!\n", t->parms.name);
  401. rel_msg = 1;
  402. }
  403. } else if (net_ratelimit()) {
  404. printk(KERN_WARNING
  405. "%s: Recipient unable to parse tunneled "
  406. "packet!\n ", t->parms.name);
  407. }
  408. break;
  409. case ICMPV6_PKT_TOOBIG:
  410. mtu = *info - offset;
  411. if (mtu < IPV6_MIN_MTU)
  412. mtu = IPV6_MIN_MTU;
  413. t->dev->mtu = mtu;
  414. if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
  415. rel_type = ICMPV6_PKT_TOOBIG;
  416. rel_code = 0;
  417. rel_info = mtu;
  418. rel_msg = 1;
  419. }
  420. break;
  421. }
  422. *type = rel_type;
  423. *code = rel_code;
  424. *info = rel_info;
  425. *msg = rel_msg;
  426. out:
  427. rcu_read_unlock();
  428. return err;
  429. }
  430. static int
  431. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  432. u8 type, u8 code, int offset, __be32 info)
  433. {
  434. int rel_msg = 0;
  435. u8 rel_type = type;
  436. u8 rel_code = code;
  437. __u32 rel_info = ntohl(info);
  438. int err;
  439. struct sk_buff *skb2;
  440. struct iphdr *eiph;
  441. struct flowi fl;
  442. struct rtable *rt;
  443. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  444. &rel_msg, &rel_info, offset);
  445. if (err < 0)
  446. return err;
  447. if (rel_msg == 0)
  448. return 0;
  449. switch (rel_type) {
  450. case ICMPV6_DEST_UNREACH:
  451. if (rel_code != ICMPV6_ADDR_UNREACH)
  452. return 0;
  453. rel_type = ICMP_DEST_UNREACH;
  454. rel_code = ICMP_HOST_UNREACH;
  455. break;
  456. case ICMPV6_PKT_TOOBIG:
  457. if (rel_code != 0)
  458. return 0;
  459. rel_type = ICMP_DEST_UNREACH;
  460. rel_code = ICMP_FRAG_NEEDED;
  461. break;
  462. default:
  463. return 0;
  464. }
  465. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  466. return 0;
  467. skb2 = skb_clone(skb, GFP_ATOMIC);
  468. if (!skb2)
  469. return 0;
  470. skb_dst_drop(skb2);
  471. skb_pull(skb2, offset);
  472. skb_reset_network_header(skb2);
  473. eiph = ip_hdr(skb2);
  474. /* Try to guess incoming interface */
  475. memset(&fl, 0, sizeof(fl));
  476. fl.fl4_dst = eiph->saddr;
  477. fl.fl4_tos = RT_TOS(eiph->tos);
  478. fl.proto = IPPROTO_IPIP;
  479. if (ip_route_output_key(dev_net(skb->dev), &rt, &fl))
  480. goto out;
  481. skb2->dev = rt->u.dst.dev;
  482. /* route "incoming" packet */
  483. if (rt->rt_flags & RTCF_LOCAL) {
  484. ip_rt_put(rt);
  485. rt = NULL;
  486. fl.fl4_dst = eiph->daddr;
  487. fl.fl4_src = eiph->saddr;
  488. fl.fl4_tos = eiph->tos;
  489. if (ip_route_output_key(dev_net(skb->dev), &rt, &fl) ||
  490. rt->u.dst.dev->type != ARPHRD_TUNNEL) {
  491. ip_rt_put(rt);
  492. goto out;
  493. }
  494. skb_dst_set(skb2, (struct dst_entry *)rt);
  495. } else {
  496. ip_rt_put(rt);
  497. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  498. skb2->dev) ||
  499. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  500. goto out;
  501. }
  502. /* change mtu on this route */
  503. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  504. if (rel_info > dst_mtu(skb_dst(skb2)))
  505. goto out;
  506. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), rel_info);
  507. }
  508. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  509. out:
  510. kfree_skb(skb2);
  511. return 0;
  512. }
  513. static int
  514. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  515. u8 type, u8 code, int offset, __be32 info)
  516. {
  517. int rel_msg = 0;
  518. u8 rel_type = type;
  519. u8 rel_code = code;
  520. __u32 rel_info = ntohl(info);
  521. int err;
  522. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  523. &rel_msg, &rel_info, offset);
  524. if (err < 0)
  525. return err;
  526. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  527. struct rt6_info *rt;
  528. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  529. if (!skb2)
  530. return 0;
  531. skb_dst_drop(skb2);
  532. skb_pull(skb2, offset);
  533. skb_reset_network_header(skb2);
  534. /* Try to guess incoming interface */
  535. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  536. NULL, 0, 0);
  537. if (rt && rt->rt6i_dev)
  538. skb2->dev = rt->rt6i_dev;
  539. icmpv6_send(skb2, rel_type, rel_code, rel_info);
  540. if (rt)
  541. dst_release(&rt->u.dst);
  542. kfree_skb(skb2);
  543. }
  544. return 0;
  545. }
  546. static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
  547. struct ipv6hdr *ipv6h,
  548. struct sk_buff *skb)
  549. {
  550. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  551. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  552. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  553. if (INET_ECN_is_ce(dsfield))
  554. IP_ECN_set_ce(ip_hdr(skb));
  555. }
  556. static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
  557. struct ipv6hdr *ipv6h,
  558. struct sk_buff *skb)
  559. {
  560. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  561. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  562. if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
  563. IP6_ECN_set_ce(ipv6_hdr(skb));
  564. }
  565. /* called with rcu_read_lock() */
  566. static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
  567. {
  568. struct ip6_tnl_parm *p = &t->parms;
  569. int ret = 0;
  570. struct net *net = dev_net(t->dev);
  571. if (p->flags & IP6_TNL_F_CAP_RCV) {
  572. struct net_device *ldev = NULL;
  573. if (p->link)
  574. ldev = dev_get_by_index_rcu(net, p->link);
  575. if ((ipv6_addr_is_multicast(&p->laddr) ||
  576. likely(ipv6_chk_addr(net, &p->laddr, ldev, 0))) &&
  577. likely(!ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  578. ret = 1;
  579. }
  580. return ret;
  581. }
  582. /**
  583. * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
  584. * @skb: received socket buffer
  585. * @protocol: ethernet protocol ID
  586. * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
  587. *
  588. * Return: 0
  589. **/
  590. static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
  591. __u8 ipproto,
  592. void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
  593. struct ipv6hdr *ipv6h,
  594. struct sk_buff *skb))
  595. {
  596. struct ip6_tnl *t;
  597. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  598. rcu_read_lock();
  599. if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
  600. &ipv6h->daddr)) != NULL) {
  601. if (t->parms.proto != ipproto && t->parms.proto != 0) {
  602. rcu_read_unlock();
  603. goto discard;
  604. }
  605. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  606. rcu_read_unlock();
  607. goto discard;
  608. }
  609. if (!ip6_tnl_rcv_ctl(t)) {
  610. t->dev->stats.rx_dropped++;
  611. rcu_read_unlock();
  612. goto discard;
  613. }
  614. secpath_reset(skb);
  615. skb->mac_header = skb->network_header;
  616. skb_reset_network_header(skb);
  617. skb->protocol = htons(protocol);
  618. skb->pkt_type = PACKET_HOST;
  619. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  620. skb->dev = t->dev;
  621. skb_dst_drop(skb);
  622. nf_reset(skb);
  623. dscp_ecn_decapsulate(t, ipv6h, skb);
  624. t->dev->stats.rx_packets++;
  625. t->dev->stats.rx_bytes += skb->len;
  626. netif_rx(skb);
  627. rcu_read_unlock();
  628. return 0;
  629. }
  630. rcu_read_unlock();
  631. return 1;
  632. discard:
  633. kfree_skb(skb);
  634. return 0;
  635. }
  636. static int ip4ip6_rcv(struct sk_buff *skb)
  637. {
  638. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  639. ip4ip6_dscp_ecn_decapsulate);
  640. }
  641. static int ip6ip6_rcv(struct sk_buff *skb)
  642. {
  643. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  644. ip6ip6_dscp_ecn_decapsulate);
  645. }
  646. struct ipv6_tel_txoption {
  647. struct ipv6_txoptions ops;
  648. __u8 dst_opt[8];
  649. };
  650. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  651. {
  652. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  653. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  654. opt->dst_opt[3] = 1;
  655. opt->dst_opt[4] = encap_limit;
  656. opt->dst_opt[5] = IPV6_TLV_PADN;
  657. opt->dst_opt[6] = 1;
  658. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  659. opt->ops.opt_nflen = 8;
  660. }
  661. /**
  662. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  663. * @t: the outgoing tunnel device
  664. * @hdr: IPv6 header from the incoming packet
  665. *
  666. * Description:
  667. * Avoid trivial tunneling loop by checking that tunnel exit-point
  668. * doesn't match source of incoming packet.
  669. *
  670. * Return:
  671. * 1 if conflict,
  672. * 0 else
  673. **/
  674. static inline int
  675. ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
  676. {
  677. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  678. }
  679. static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  680. {
  681. struct ip6_tnl_parm *p = &t->parms;
  682. int ret = 0;
  683. struct net *net = dev_net(t->dev);
  684. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  685. struct net_device *ldev = NULL;
  686. rcu_read_lock();
  687. if (p->link)
  688. ldev = dev_get_by_index_rcu(net, p->link);
  689. if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
  690. printk(KERN_WARNING
  691. "%s xmit: Local address not yet configured!\n",
  692. p->name);
  693. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  694. unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  695. printk(KERN_WARNING
  696. "%s xmit: Routing loop! "
  697. "Remote address found on this node!\n",
  698. p->name);
  699. else
  700. ret = 1;
  701. rcu_read_unlock();
  702. }
  703. return ret;
  704. }
  705. /**
  706. * ip6_tnl_xmit2 - encapsulate packet and send
  707. * @skb: the outgoing socket buffer
  708. * @dev: the outgoing tunnel device
  709. * @dsfield: dscp code for outer header
  710. * @fl: flow of tunneled packet
  711. * @encap_limit: encapsulation limit
  712. * @pmtu: Path MTU is stored if packet is too big
  713. *
  714. * Description:
  715. * Build new header and do some sanity checks on the packet before sending
  716. * it.
  717. *
  718. * Return:
  719. * 0 on success
  720. * -1 fail
  721. * %-EMSGSIZE message too big. return mtu in this case.
  722. **/
  723. static int ip6_tnl_xmit2(struct sk_buff *skb,
  724. struct net_device *dev,
  725. __u8 dsfield,
  726. struct flowi *fl,
  727. int encap_limit,
  728. __u32 *pmtu)
  729. {
  730. struct net *net = dev_net(dev);
  731. struct ip6_tnl *t = netdev_priv(dev);
  732. struct net_device_stats *stats = &t->dev->stats;
  733. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  734. struct ipv6_tel_txoption opt;
  735. struct dst_entry *dst;
  736. struct net_device *tdev;
  737. int mtu;
  738. unsigned int max_headroom = sizeof(struct ipv6hdr);
  739. u8 proto;
  740. int err = -1;
  741. int pkt_len;
  742. if ((dst = ip6_tnl_dst_check(t)) != NULL)
  743. dst_hold(dst);
  744. else {
  745. dst = ip6_route_output(net, NULL, fl);
  746. if (dst->error || xfrm_lookup(net, &dst, fl, NULL, 0) < 0)
  747. goto tx_err_link_failure;
  748. }
  749. tdev = dst->dev;
  750. if (tdev == dev) {
  751. stats->collisions++;
  752. if (net_ratelimit())
  753. printk(KERN_WARNING
  754. "%s: Local routing loop detected!\n",
  755. t->parms.name);
  756. goto tx_err_dst_release;
  757. }
  758. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  759. if (encap_limit >= 0) {
  760. max_headroom += 8;
  761. mtu -= 8;
  762. }
  763. if (mtu < IPV6_MIN_MTU)
  764. mtu = IPV6_MIN_MTU;
  765. if (skb_dst(skb))
  766. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
  767. if (skb->len > mtu) {
  768. *pmtu = mtu;
  769. err = -EMSGSIZE;
  770. goto tx_err_dst_release;
  771. }
  772. /*
  773. * Okay, now see if we can stuff it in the buffer as-is.
  774. */
  775. max_headroom += LL_RESERVED_SPACE(tdev);
  776. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  777. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  778. struct sk_buff *new_skb;
  779. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  780. goto tx_err_dst_release;
  781. if (skb->sk)
  782. skb_set_owner_w(new_skb, skb->sk);
  783. kfree_skb(skb);
  784. skb = new_skb;
  785. }
  786. skb_dst_drop(skb);
  787. skb_dst_set(skb, dst_clone(dst));
  788. skb->transport_header = skb->network_header;
  789. proto = fl->proto;
  790. if (encap_limit >= 0) {
  791. init_tel_txopt(&opt, encap_limit);
  792. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  793. }
  794. skb_push(skb, sizeof(struct ipv6hdr));
  795. skb_reset_network_header(skb);
  796. ipv6h = ipv6_hdr(skb);
  797. *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
  798. dsfield = INET_ECN_encapsulate(0, dsfield);
  799. ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
  800. ipv6h->hop_limit = t->parms.hop_limit;
  801. ipv6h->nexthdr = proto;
  802. ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
  803. ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
  804. nf_reset(skb);
  805. pkt_len = skb->len;
  806. err = ip6_local_out(skb);
  807. if (net_xmit_eval(err) == 0) {
  808. stats->tx_bytes += pkt_len;
  809. stats->tx_packets++;
  810. } else {
  811. stats->tx_errors++;
  812. stats->tx_aborted_errors++;
  813. }
  814. ip6_tnl_dst_store(t, dst);
  815. return 0;
  816. tx_err_link_failure:
  817. stats->tx_carrier_errors++;
  818. dst_link_failure(skb);
  819. tx_err_dst_release:
  820. dst_release(dst);
  821. return err;
  822. }
  823. static inline int
  824. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  825. {
  826. struct ip6_tnl *t = netdev_priv(dev);
  827. struct iphdr *iph = ip_hdr(skb);
  828. int encap_limit = -1;
  829. struct flowi fl;
  830. __u8 dsfield;
  831. __u32 mtu;
  832. int err;
  833. if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
  834. !ip6_tnl_xmit_ctl(t))
  835. return -1;
  836. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  837. encap_limit = t->parms.encap_limit;
  838. memcpy(&fl, &t->fl, sizeof (fl));
  839. fl.proto = IPPROTO_IPIP;
  840. dsfield = ipv4_get_dsfield(iph);
  841. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  842. fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  843. & IPV6_TCLASS_MASK;
  844. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
  845. if (err != 0) {
  846. /* XXX: send ICMP error even if DF is not set. */
  847. if (err == -EMSGSIZE)
  848. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  849. htonl(mtu));
  850. return -1;
  851. }
  852. return 0;
  853. }
  854. static inline int
  855. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  856. {
  857. struct ip6_tnl *t = netdev_priv(dev);
  858. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  859. int encap_limit = -1;
  860. __u16 offset;
  861. struct flowi fl;
  862. __u8 dsfield;
  863. __u32 mtu;
  864. int err;
  865. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  866. !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
  867. return -1;
  868. offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
  869. if (offset > 0) {
  870. struct ipv6_tlv_tnl_enc_lim *tel;
  871. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  872. if (tel->encap_limit == 0) {
  873. icmpv6_send(skb, ICMPV6_PARAMPROB,
  874. ICMPV6_HDR_FIELD, offset + 2);
  875. return -1;
  876. }
  877. encap_limit = tel->encap_limit - 1;
  878. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  879. encap_limit = t->parms.encap_limit;
  880. memcpy(&fl, &t->fl, sizeof (fl));
  881. fl.proto = IPPROTO_IPV6;
  882. dsfield = ipv6_get_dsfield(ipv6h);
  883. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
  884. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  885. if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
  886. fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  887. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
  888. if (err != 0) {
  889. if (err == -EMSGSIZE)
  890. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  891. return -1;
  892. }
  893. return 0;
  894. }
  895. static netdev_tx_t
  896. ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  897. {
  898. struct ip6_tnl *t = netdev_priv(dev);
  899. struct net_device_stats *stats = &t->dev->stats;
  900. int ret;
  901. switch (skb->protocol) {
  902. case htons(ETH_P_IP):
  903. ret = ip4ip6_tnl_xmit(skb, dev);
  904. break;
  905. case htons(ETH_P_IPV6):
  906. ret = ip6ip6_tnl_xmit(skb, dev);
  907. break;
  908. default:
  909. goto tx_err;
  910. }
  911. if (ret < 0)
  912. goto tx_err;
  913. return NETDEV_TX_OK;
  914. tx_err:
  915. stats->tx_errors++;
  916. stats->tx_dropped++;
  917. kfree_skb(skb);
  918. return NETDEV_TX_OK;
  919. }
  920. static void ip6_tnl_set_cap(struct ip6_tnl *t)
  921. {
  922. struct ip6_tnl_parm *p = &t->parms;
  923. int ltype = ipv6_addr_type(&p->laddr);
  924. int rtype = ipv6_addr_type(&p->raddr);
  925. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
  926. if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  927. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  928. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  929. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  930. if (ltype&IPV6_ADDR_UNICAST)
  931. p->flags |= IP6_TNL_F_CAP_XMIT;
  932. if (rtype&IPV6_ADDR_UNICAST)
  933. p->flags |= IP6_TNL_F_CAP_RCV;
  934. }
  935. }
  936. static void ip6_tnl_link_config(struct ip6_tnl *t)
  937. {
  938. struct net_device *dev = t->dev;
  939. struct ip6_tnl_parm *p = &t->parms;
  940. struct flowi *fl = &t->fl;
  941. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  942. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  943. /* Set up flowi template */
  944. ipv6_addr_copy(&fl->fl6_src, &p->laddr);
  945. ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
  946. fl->oif = p->link;
  947. fl->fl6_flowlabel = 0;
  948. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  949. fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  950. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  951. fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  952. ip6_tnl_set_cap(t);
  953. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  954. dev->flags |= IFF_POINTOPOINT;
  955. else
  956. dev->flags &= ~IFF_POINTOPOINT;
  957. dev->iflink = p->link;
  958. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  959. int strict = (ipv6_addr_type(&p->raddr) &
  960. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  961. struct rt6_info *rt = rt6_lookup(dev_net(dev),
  962. &p->raddr, &p->laddr,
  963. p->link, strict);
  964. if (rt == NULL)
  965. return;
  966. if (rt->rt6i_dev) {
  967. dev->hard_header_len = rt->rt6i_dev->hard_header_len +
  968. sizeof (struct ipv6hdr);
  969. dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
  970. if (dev->mtu < IPV6_MIN_MTU)
  971. dev->mtu = IPV6_MIN_MTU;
  972. }
  973. dst_release(&rt->u.dst);
  974. }
  975. }
  976. /**
  977. * ip6_tnl_change - update the tunnel parameters
  978. * @t: tunnel to be changed
  979. * @p: tunnel configuration parameters
  980. *
  981. * Description:
  982. * ip6_tnl_change() updates the tunnel parameters
  983. **/
  984. static int
  985. ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
  986. {
  987. ipv6_addr_copy(&t->parms.laddr, &p->laddr);
  988. ipv6_addr_copy(&t->parms.raddr, &p->raddr);
  989. t->parms.flags = p->flags;
  990. t->parms.hop_limit = p->hop_limit;
  991. t->parms.encap_limit = p->encap_limit;
  992. t->parms.flowinfo = p->flowinfo;
  993. t->parms.link = p->link;
  994. t->parms.proto = p->proto;
  995. ip6_tnl_dst_reset(t);
  996. ip6_tnl_link_config(t);
  997. return 0;
  998. }
  999. /**
  1000. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1001. * @dev: virtual device associated with tunnel
  1002. * @ifr: parameters passed from userspace
  1003. * @cmd: command to be performed
  1004. *
  1005. * Description:
  1006. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1007. * from userspace.
  1008. *
  1009. * The possible commands are the following:
  1010. * %SIOCGETTUNNEL: get tunnel parameters for device
  1011. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1012. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1013. * %SIOCDELTUNNEL: delete tunnel
  1014. *
  1015. * The fallback device "ip6tnl0", created during module
  1016. * initialization, can be used for creating other tunnel devices.
  1017. *
  1018. * Return:
  1019. * 0 on success,
  1020. * %-EFAULT if unable to copy data to or from userspace,
  1021. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1022. * %-EINVAL if passed tunnel parameters are invalid,
  1023. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1024. * %-ENODEV if attempting to change or delete a nonexisting device
  1025. **/
  1026. static int
  1027. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1028. {
  1029. int err = 0;
  1030. struct ip6_tnl_parm p;
  1031. struct ip6_tnl *t = NULL;
  1032. struct net *net = dev_net(dev);
  1033. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1034. switch (cmd) {
  1035. case SIOCGETTUNNEL:
  1036. if (dev == ip6n->fb_tnl_dev) {
  1037. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  1038. err = -EFAULT;
  1039. break;
  1040. }
  1041. t = ip6_tnl_locate(net, &p, 0);
  1042. }
  1043. if (t == NULL)
  1044. t = netdev_priv(dev);
  1045. memcpy(&p, &t->parms, sizeof (p));
  1046. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  1047. err = -EFAULT;
  1048. }
  1049. break;
  1050. case SIOCADDTUNNEL:
  1051. case SIOCCHGTUNNEL:
  1052. err = -EPERM;
  1053. if (!capable(CAP_NET_ADMIN))
  1054. break;
  1055. err = -EFAULT;
  1056. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1057. break;
  1058. err = -EINVAL;
  1059. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1060. p.proto != 0)
  1061. break;
  1062. t = ip6_tnl_locate(net, &p, cmd == SIOCADDTUNNEL);
  1063. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  1064. if (t != NULL) {
  1065. if (t->dev != dev) {
  1066. err = -EEXIST;
  1067. break;
  1068. }
  1069. } else
  1070. t = netdev_priv(dev);
  1071. ip6_tnl_unlink(ip6n, t);
  1072. err = ip6_tnl_change(t, &p);
  1073. ip6_tnl_link(ip6n, t);
  1074. netdev_state_change(dev);
  1075. }
  1076. if (t) {
  1077. err = 0;
  1078. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
  1079. err = -EFAULT;
  1080. } else
  1081. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1082. break;
  1083. case SIOCDELTUNNEL:
  1084. err = -EPERM;
  1085. if (!capable(CAP_NET_ADMIN))
  1086. break;
  1087. if (dev == ip6n->fb_tnl_dev) {
  1088. err = -EFAULT;
  1089. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1090. break;
  1091. err = -ENOENT;
  1092. if ((t = ip6_tnl_locate(net, &p, 0)) == NULL)
  1093. break;
  1094. err = -EPERM;
  1095. if (t->dev == ip6n->fb_tnl_dev)
  1096. break;
  1097. dev = t->dev;
  1098. }
  1099. err = 0;
  1100. unregister_netdevice(dev);
  1101. break;
  1102. default:
  1103. err = -EINVAL;
  1104. }
  1105. return err;
  1106. }
  1107. /**
  1108. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1109. * @dev: virtual device associated with tunnel
  1110. * @new_mtu: the new mtu
  1111. *
  1112. * Return:
  1113. * 0 on success,
  1114. * %-EINVAL if mtu too small
  1115. **/
  1116. static int
  1117. ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1118. {
  1119. if (new_mtu < IPV6_MIN_MTU) {
  1120. return -EINVAL;
  1121. }
  1122. dev->mtu = new_mtu;
  1123. return 0;
  1124. }
  1125. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1126. .ndo_uninit = ip6_tnl_dev_uninit,
  1127. .ndo_start_xmit = ip6_tnl_xmit,
  1128. .ndo_do_ioctl = ip6_tnl_ioctl,
  1129. .ndo_change_mtu = ip6_tnl_change_mtu,
  1130. };
  1131. /**
  1132. * ip6_tnl_dev_setup - setup virtual tunnel device
  1133. * @dev: virtual device associated with tunnel
  1134. *
  1135. * Description:
  1136. * Initialize function pointers and device parameters
  1137. **/
  1138. static void ip6_tnl_dev_setup(struct net_device *dev)
  1139. {
  1140. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1141. dev->destructor = free_netdev;
  1142. dev->type = ARPHRD_TUNNEL6;
  1143. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  1144. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  1145. dev->flags |= IFF_NOARP;
  1146. dev->addr_len = sizeof(struct in6_addr);
  1147. dev->features |= NETIF_F_NETNS_LOCAL;
  1148. }
  1149. /**
  1150. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1151. * @dev: virtual device associated with tunnel
  1152. **/
  1153. static inline void
  1154. ip6_tnl_dev_init_gen(struct net_device *dev)
  1155. {
  1156. struct ip6_tnl *t = netdev_priv(dev);
  1157. t->dev = dev;
  1158. strcpy(t->parms.name, dev->name);
  1159. }
  1160. /**
  1161. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1162. * @dev: virtual device associated with tunnel
  1163. **/
  1164. static void ip6_tnl_dev_init(struct net_device *dev)
  1165. {
  1166. struct ip6_tnl *t = netdev_priv(dev);
  1167. ip6_tnl_dev_init_gen(dev);
  1168. ip6_tnl_link_config(t);
  1169. }
  1170. /**
  1171. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1172. * @dev: fallback device
  1173. *
  1174. * Return: 0
  1175. **/
  1176. static void __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1177. {
  1178. struct ip6_tnl *t = netdev_priv(dev);
  1179. struct net *net = dev_net(dev);
  1180. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1181. ip6_tnl_dev_init_gen(dev);
  1182. t->parms.proto = IPPROTO_IPV6;
  1183. dev_hold(dev);
  1184. ip6n->tnls_wc[0] = t;
  1185. }
  1186. static struct xfrm6_tunnel ip4ip6_handler = {
  1187. .handler = ip4ip6_rcv,
  1188. .err_handler = ip4ip6_err,
  1189. .priority = 1,
  1190. };
  1191. static struct xfrm6_tunnel ip6ip6_handler = {
  1192. .handler = ip6ip6_rcv,
  1193. .err_handler = ip6ip6_err,
  1194. .priority = 1,
  1195. };
  1196. static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
  1197. {
  1198. int h;
  1199. struct ip6_tnl *t;
  1200. LIST_HEAD(list);
  1201. for (h = 0; h < HASH_SIZE; h++) {
  1202. t = ip6n->tnls_r_l[h];
  1203. while (t != NULL) {
  1204. unregister_netdevice_queue(t->dev, &list);
  1205. t = t->next;
  1206. }
  1207. }
  1208. t = ip6n->tnls_wc[0];
  1209. unregister_netdevice_queue(t->dev, &list);
  1210. unregister_netdevice_many(&list);
  1211. }
  1212. static int __net_init ip6_tnl_init_net(struct net *net)
  1213. {
  1214. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1215. int err;
  1216. ip6n->tnls[0] = ip6n->tnls_wc;
  1217. ip6n->tnls[1] = ip6n->tnls_r_l;
  1218. err = -ENOMEM;
  1219. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1220. ip6_tnl_dev_setup);
  1221. if (!ip6n->fb_tnl_dev)
  1222. goto err_alloc_dev;
  1223. dev_net_set(ip6n->fb_tnl_dev, net);
  1224. ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1225. err = register_netdev(ip6n->fb_tnl_dev);
  1226. if (err < 0)
  1227. goto err_register;
  1228. return 0;
  1229. err_register:
  1230. free_netdev(ip6n->fb_tnl_dev);
  1231. err_alloc_dev:
  1232. return err;
  1233. }
  1234. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1235. {
  1236. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1237. rtnl_lock();
  1238. ip6_tnl_destroy_tunnels(ip6n);
  1239. rtnl_unlock();
  1240. }
  1241. static struct pernet_operations ip6_tnl_net_ops = {
  1242. .init = ip6_tnl_init_net,
  1243. .exit = ip6_tnl_exit_net,
  1244. .id = &ip6_tnl_net_id,
  1245. .size = sizeof(struct ip6_tnl_net),
  1246. };
  1247. /**
  1248. * ip6_tunnel_init - register protocol and reserve needed resources
  1249. *
  1250. * Return: 0 on success
  1251. **/
  1252. static int __init ip6_tunnel_init(void)
  1253. {
  1254. int err;
  1255. err = register_pernet_device(&ip6_tnl_net_ops);
  1256. if (err < 0)
  1257. goto out_pernet;
  1258. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1259. if (err < 0) {
  1260. printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
  1261. goto out_ip4ip6;
  1262. }
  1263. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1264. if (err < 0) {
  1265. printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
  1266. goto out_ip6ip6;
  1267. }
  1268. return 0;
  1269. out_ip6ip6:
  1270. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1271. out_ip4ip6:
  1272. unregister_pernet_device(&ip6_tnl_net_ops);
  1273. out_pernet:
  1274. return err;
  1275. }
  1276. /**
  1277. * ip6_tunnel_cleanup - free resources and unregister protocol
  1278. **/
  1279. static void __exit ip6_tunnel_cleanup(void)
  1280. {
  1281. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1282. printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
  1283. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1284. printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
  1285. unregister_pernet_device(&ip6_tnl_net_ops);
  1286. }
  1287. module_init(ip6_tunnel_init);
  1288. module_exit(ip6_tunnel_cleanup);