ip6_tunnel.c 33 KB

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