ip6_tunnel.c 33 KB

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