ip6_tunnel.c 33 KB

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