ip6_tunnel.c 36 KB

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