ip6_tunnel.c 35 KB

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