ip6_tunnel.c 33 KB

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