ip6_tunnel.c 37 KB

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