ip6_tunnel.c 43 KB

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