ip6_tunnel.c 37 KB

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