ip6_tunnel.c 35 KB

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