ip6_tunnel.c 35 KB

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