ip6_tunnel.c 37 KB

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