ip6_tunnel.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  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/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 <linux/hash.h>
  41. #include <linux/etherdevice.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. #include <net/dst_metadata.h>
  57. MODULE_AUTHOR("Ville Nuorvala");
  58. MODULE_DESCRIPTION("IPv6 tunneling device");
  59. MODULE_LICENSE("GPL");
  60. MODULE_ALIAS_RTNL_LINK("ip6tnl");
  61. MODULE_ALIAS_NETDEV("ip6tnl0");
  62. #define IP6_TUNNEL_HASH_SIZE_SHIFT 5
  63. #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
  64. static bool log_ecn_error = true;
  65. module_param(log_ecn_error, bool, 0644);
  66. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  67. static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
  68. {
  69. u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
  70. return hash_32(hash, IP6_TUNNEL_HASH_SIZE_SHIFT);
  71. }
  72. static int ip6_tnl_dev_init(struct net_device *dev);
  73. static void ip6_tnl_dev_setup(struct net_device *dev);
  74. static struct rtnl_link_ops ip6_link_ops __read_mostly;
  75. static int ip6_tnl_net_id __read_mostly;
  76. struct ip6_tnl_net {
  77. /* the IPv6 tunnel fallback device */
  78. struct net_device *fb_tnl_dev;
  79. /* lists for storing tunnels in use */
  80. struct ip6_tnl __rcu *tnls_r_l[IP6_TUNNEL_HASH_SIZE];
  81. struct ip6_tnl __rcu *tnls_wc[1];
  82. struct ip6_tnl __rcu **tnls[2];
  83. struct ip6_tnl __rcu *collect_md_tun;
  84. };
  85. static struct net_device_stats *ip6_get_stats(struct net_device *dev)
  86. {
  87. struct pcpu_sw_netstats tmp, sum = { 0 };
  88. int i;
  89. for_each_possible_cpu(i) {
  90. unsigned int start;
  91. const struct pcpu_sw_netstats *tstats =
  92. per_cpu_ptr(dev->tstats, i);
  93. do {
  94. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  95. tmp.rx_packets = tstats->rx_packets;
  96. tmp.rx_bytes = tstats->rx_bytes;
  97. tmp.tx_packets = tstats->tx_packets;
  98. tmp.tx_bytes = tstats->tx_bytes;
  99. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  100. sum.rx_packets += tmp.rx_packets;
  101. sum.rx_bytes += tmp.rx_bytes;
  102. sum.tx_packets += tmp.tx_packets;
  103. sum.tx_bytes += tmp.tx_bytes;
  104. }
  105. dev->stats.rx_packets = sum.rx_packets;
  106. dev->stats.rx_bytes = sum.rx_bytes;
  107. dev->stats.tx_packets = sum.tx_packets;
  108. dev->stats.tx_bytes = sum.tx_bytes;
  109. return &dev->stats;
  110. }
  111. /**
  112. * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
  113. * @remote: the address of the tunnel exit-point
  114. * @local: the address of the tunnel entry-point
  115. *
  116. * Return:
  117. * tunnel matching given end-points if found,
  118. * else fallback tunnel if its device is up,
  119. * else %NULL
  120. **/
  121. #define for_each_ip6_tunnel_rcu(start) \
  122. for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
  123. static struct ip6_tnl *
  124. ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
  125. {
  126. unsigned int hash = HASH(remote, local);
  127. struct ip6_tnl *t;
  128. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  129. struct in6_addr any;
  130. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  131. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  132. ipv6_addr_equal(remote, &t->parms.raddr) &&
  133. (t->dev->flags & IFF_UP))
  134. return t;
  135. }
  136. memset(&any, 0, sizeof(any));
  137. hash = HASH(&any, local);
  138. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  139. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  140. ipv6_addr_any(&t->parms.raddr) &&
  141. (t->dev->flags & IFF_UP))
  142. return t;
  143. }
  144. hash = HASH(remote, &any);
  145. for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
  146. if (ipv6_addr_equal(remote, &t->parms.raddr) &&
  147. ipv6_addr_any(&t->parms.laddr) &&
  148. (t->dev->flags & IFF_UP))
  149. return t;
  150. }
  151. t = rcu_dereference(ip6n->collect_md_tun);
  152. if (t)
  153. return t;
  154. t = rcu_dereference(ip6n->tnls_wc[0]);
  155. if (t && (t->dev->flags & IFF_UP))
  156. return t;
  157. return NULL;
  158. }
  159. /**
  160. * ip6_tnl_bucket - get head of list matching given tunnel parameters
  161. * @p: parameters containing tunnel end-points
  162. *
  163. * Description:
  164. * ip6_tnl_bucket() returns the head of the list matching the
  165. * &struct in6_addr entries laddr and raddr in @p.
  166. *
  167. * Return: head of IPv6 tunnel list
  168. **/
  169. static struct ip6_tnl __rcu **
  170. ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
  171. {
  172. const struct in6_addr *remote = &p->raddr;
  173. const struct in6_addr *local = &p->laddr;
  174. unsigned int h = 0;
  175. int prio = 0;
  176. if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
  177. prio = 1;
  178. h = HASH(remote, local);
  179. }
  180. return &ip6n->tnls[prio][h];
  181. }
  182. /**
  183. * ip6_tnl_link - add tunnel to hash table
  184. * @t: tunnel to be added
  185. **/
  186. static void
  187. ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  188. {
  189. struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
  190. if (t->parms.collect_md)
  191. rcu_assign_pointer(ip6n->collect_md_tun, t);
  192. rcu_assign_pointer(t->next , rtnl_dereference(*tp));
  193. rcu_assign_pointer(*tp, t);
  194. }
  195. /**
  196. * ip6_tnl_unlink - remove tunnel from hash table
  197. * @t: tunnel to be removed
  198. **/
  199. static void
  200. ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
  201. {
  202. struct ip6_tnl __rcu **tp;
  203. struct ip6_tnl *iter;
  204. if (t->parms.collect_md)
  205. rcu_assign_pointer(ip6n->collect_md_tun, NULL);
  206. for (tp = ip6_tnl_bucket(ip6n, &t->parms);
  207. (iter = rtnl_dereference(*tp)) != NULL;
  208. tp = &iter->next) {
  209. if (t == iter) {
  210. rcu_assign_pointer(*tp, t->next);
  211. break;
  212. }
  213. }
  214. }
  215. static void ip6_dev_free(struct net_device *dev)
  216. {
  217. struct ip6_tnl *t = netdev_priv(dev);
  218. gro_cells_destroy(&t->gro_cells);
  219. dst_cache_destroy(&t->dst_cache);
  220. free_percpu(dev->tstats);
  221. free_netdev(dev);
  222. }
  223. static int ip6_tnl_create2(struct net_device *dev)
  224. {
  225. struct ip6_tnl *t = netdev_priv(dev);
  226. struct net *net = dev_net(dev);
  227. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  228. int err;
  229. t = netdev_priv(dev);
  230. dev->rtnl_link_ops = &ip6_link_ops;
  231. err = register_netdevice(dev);
  232. if (err < 0)
  233. goto out;
  234. strcpy(t->parms.name, dev->name);
  235. dev_hold(dev);
  236. ip6_tnl_link(ip6n, t);
  237. return 0;
  238. out:
  239. return err;
  240. }
  241. /**
  242. * ip6_tnl_create - create a new tunnel
  243. * @p: tunnel parameters
  244. * @pt: pointer to new tunnel
  245. *
  246. * Description:
  247. * Create tunnel matching given parameters.
  248. *
  249. * Return:
  250. * created tunnel or error pointer
  251. **/
  252. static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
  253. {
  254. struct net_device *dev;
  255. struct ip6_tnl *t;
  256. char name[IFNAMSIZ];
  257. int err = -E2BIG;
  258. if (p->name[0]) {
  259. if (!dev_valid_name(p->name))
  260. goto failed;
  261. strlcpy(name, p->name, IFNAMSIZ);
  262. } else {
  263. sprintf(name, "ip6tnl%%d");
  264. }
  265. err = -ENOMEM;
  266. dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
  267. ip6_tnl_dev_setup);
  268. if (!dev)
  269. goto failed;
  270. dev_net_set(dev, net);
  271. t = netdev_priv(dev);
  272. t->parms = *p;
  273. t->net = dev_net(dev);
  274. err = ip6_tnl_create2(dev);
  275. if (err < 0)
  276. goto failed_free;
  277. return t;
  278. failed_free:
  279. ip6_dev_free(dev);
  280. failed:
  281. return ERR_PTR(err);
  282. }
  283. /**
  284. * ip6_tnl_locate - find or create tunnel matching given parameters
  285. * @p: tunnel parameters
  286. * @create: != 0 if allowed to create new tunnel if no match found
  287. *
  288. * Description:
  289. * ip6_tnl_locate() first tries to locate an existing tunnel
  290. * based on @parms. If this is unsuccessful, but @create is set a new
  291. * tunnel device is created and registered for use.
  292. *
  293. * Return:
  294. * matching tunnel or error pointer
  295. **/
  296. static struct ip6_tnl *ip6_tnl_locate(struct net *net,
  297. struct __ip6_tnl_parm *p, int create)
  298. {
  299. const struct in6_addr *remote = &p->raddr;
  300. const struct in6_addr *local = &p->laddr;
  301. struct ip6_tnl __rcu **tp;
  302. struct ip6_tnl *t;
  303. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  304. for (tp = ip6_tnl_bucket(ip6n, p);
  305. (t = rtnl_dereference(*tp)) != NULL;
  306. tp = &t->next) {
  307. if (ipv6_addr_equal(local, &t->parms.laddr) &&
  308. ipv6_addr_equal(remote, &t->parms.raddr)) {
  309. if (create)
  310. return ERR_PTR(-EEXIST);
  311. return t;
  312. }
  313. }
  314. if (!create)
  315. return ERR_PTR(-ENODEV);
  316. return ip6_tnl_create(net, p);
  317. }
  318. /**
  319. * ip6_tnl_dev_uninit - tunnel device uninitializer
  320. * @dev: the device to be destroyed
  321. *
  322. * Description:
  323. * ip6_tnl_dev_uninit() removes tunnel from its list
  324. **/
  325. static void
  326. ip6_tnl_dev_uninit(struct net_device *dev)
  327. {
  328. struct ip6_tnl *t = netdev_priv(dev);
  329. struct net *net = t->net;
  330. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  331. if (dev == ip6n->fb_tnl_dev)
  332. RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
  333. else
  334. ip6_tnl_unlink(ip6n, t);
  335. dst_cache_reset(&t->dst_cache);
  336. dev_put(dev);
  337. }
  338. /**
  339. * parse_tvl_tnl_enc_lim - handle encapsulation limit option
  340. * @skb: received socket buffer
  341. *
  342. * Return:
  343. * 0 if none was found,
  344. * else index to encapsulation limit
  345. **/
  346. __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
  347. {
  348. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)raw;
  349. unsigned int nhoff = raw - skb->data;
  350. unsigned int off = nhoff + sizeof(*ipv6h);
  351. u8 next, nexthdr = ipv6h->nexthdr;
  352. while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
  353. struct ipv6_opt_hdr *hdr;
  354. u16 optlen;
  355. if (!pskb_may_pull(skb, off + sizeof(*hdr)))
  356. break;
  357. hdr = (struct ipv6_opt_hdr *)(skb->data + off);
  358. if (nexthdr == NEXTHDR_FRAGMENT) {
  359. struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
  360. if (frag_hdr->frag_off)
  361. break;
  362. optlen = 8;
  363. } else if (nexthdr == NEXTHDR_AUTH) {
  364. optlen = (hdr->hdrlen + 2) << 2;
  365. } else {
  366. optlen = ipv6_optlen(hdr);
  367. }
  368. /* cache hdr->nexthdr, since pskb_may_pull() might
  369. * invalidate hdr
  370. */
  371. next = hdr->nexthdr;
  372. if (nexthdr == NEXTHDR_DEST) {
  373. u16 i = 2;
  374. /* Remember : hdr is no longer valid at this point. */
  375. if (!pskb_may_pull(skb, off + optlen))
  376. break;
  377. while (1) {
  378. struct ipv6_tlv_tnl_enc_lim *tel;
  379. /* No more room for encapsulation limit */
  380. if (i + sizeof(*tel) > optlen)
  381. break;
  382. tel = (struct ipv6_tlv_tnl_enc_lim *)(skb->data + off + i);
  383. /* return index of option if found and valid */
  384. if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
  385. tel->length == 1)
  386. return i + off - nhoff;
  387. /* else jump to next option */
  388. if (tel->type)
  389. i += tel->length + 2;
  390. else
  391. i++;
  392. }
  393. }
  394. nexthdr = next;
  395. off += optlen;
  396. }
  397. return 0;
  398. }
  399. EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
  400. /**
  401. * ip6_tnl_err - tunnel error handler
  402. *
  403. * Description:
  404. * ip6_tnl_err() should handle errors in the tunnel according
  405. * to the specifications in RFC 2473.
  406. **/
  407. static int
  408. ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
  409. u8 *type, u8 *code, int *msg, __u32 *info, int offset)
  410. {
  411. const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
  412. struct ip6_tnl *t;
  413. int rel_msg = 0;
  414. u8 rel_type = ICMPV6_DEST_UNREACH;
  415. u8 rel_code = ICMPV6_ADDR_UNREACH;
  416. u8 tproto;
  417. __u32 rel_info = 0;
  418. __u16 len;
  419. int err = -ENOENT;
  420. /* If the packet doesn't contain the original IPv6 header we are
  421. in trouble since we might need the source address for further
  422. processing of the error. */
  423. rcu_read_lock();
  424. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
  425. if (!t)
  426. goto out;
  427. tproto = ACCESS_ONCE(t->parms.proto);
  428. if (tproto != ipproto && tproto != 0)
  429. goto out;
  430. err = 0;
  431. switch (*type) {
  432. __u32 teli;
  433. struct ipv6_tlv_tnl_enc_lim *tel;
  434. __u32 mtu;
  435. case ICMPV6_DEST_UNREACH:
  436. net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
  437. t->parms.name);
  438. rel_msg = 1;
  439. break;
  440. case ICMPV6_TIME_EXCEED:
  441. if ((*code) == ICMPV6_EXC_HOPLIMIT) {
  442. net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
  443. t->parms.name);
  444. rel_msg = 1;
  445. }
  446. break;
  447. case ICMPV6_PARAMPROB:
  448. teli = 0;
  449. if ((*code) == ICMPV6_HDR_FIELD)
  450. teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
  451. if (teli && teli == *info - 2) {
  452. tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
  453. if (tel->encap_limit == 0) {
  454. net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
  455. t->parms.name);
  456. rel_msg = 1;
  457. }
  458. } else {
  459. net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
  460. t->parms.name);
  461. }
  462. break;
  463. case ICMPV6_PKT_TOOBIG:
  464. mtu = *info - offset;
  465. if (mtu < IPV6_MIN_MTU)
  466. mtu = IPV6_MIN_MTU;
  467. t->dev->mtu = mtu;
  468. len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len);
  469. if (len > mtu) {
  470. rel_type = ICMPV6_PKT_TOOBIG;
  471. rel_code = 0;
  472. rel_info = mtu;
  473. rel_msg = 1;
  474. }
  475. break;
  476. }
  477. *type = rel_type;
  478. *code = rel_code;
  479. *info = rel_info;
  480. *msg = rel_msg;
  481. out:
  482. rcu_read_unlock();
  483. return err;
  484. }
  485. static int
  486. ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  487. u8 type, u8 code, int offset, __be32 info)
  488. {
  489. int rel_msg = 0;
  490. u8 rel_type = type;
  491. u8 rel_code = code;
  492. __u32 rel_info = ntohl(info);
  493. int err;
  494. struct sk_buff *skb2;
  495. const struct iphdr *eiph;
  496. struct rtable *rt;
  497. struct flowi4 fl4;
  498. err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
  499. &rel_msg, &rel_info, offset);
  500. if (err < 0)
  501. return err;
  502. if (rel_msg == 0)
  503. return 0;
  504. switch (rel_type) {
  505. case ICMPV6_DEST_UNREACH:
  506. if (rel_code != ICMPV6_ADDR_UNREACH)
  507. return 0;
  508. rel_type = ICMP_DEST_UNREACH;
  509. rel_code = ICMP_HOST_UNREACH;
  510. break;
  511. case ICMPV6_PKT_TOOBIG:
  512. if (rel_code != 0)
  513. return 0;
  514. rel_type = ICMP_DEST_UNREACH;
  515. rel_code = ICMP_FRAG_NEEDED;
  516. break;
  517. case NDISC_REDIRECT:
  518. rel_type = ICMP_REDIRECT;
  519. rel_code = ICMP_REDIR_HOST;
  520. default:
  521. return 0;
  522. }
  523. if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
  524. return 0;
  525. skb2 = skb_clone(skb, GFP_ATOMIC);
  526. if (!skb2)
  527. return 0;
  528. skb_dst_drop(skb2);
  529. skb_pull(skb2, offset);
  530. skb_reset_network_header(skb2);
  531. eiph = ip_hdr(skb2);
  532. /* Try to guess incoming interface */
  533. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  534. eiph->saddr, 0,
  535. 0, 0,
  536. IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
  537. if (IS_ERR(rt))
  538. goto out;
  539. skb2->dev = rt->dst.dev;
  540. /* route "incoming" packet */
  541. if (rt->rt_flags & RTCF_LOCAL) {
  542. ip_rt_put(rt);
  543. rt = NULL;
  544. rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
  545. eiph->daddr, eiph->saddr,
  546. 0, 0,
  547. IPPROTO_IPIP,
  548. RT_TOS(eiph->tos), 0);
  549. if (IS_ERR(rt) ||
  550. rt->dst.dev->type != ARPHRD_TUNNEL) {
  551. if (!IS_ERR(rt))
  552. ip_rt_put(rt);
  553. goto out;
  554. }
  555. skb_dst_set(skb2, &rt->dst);
  556. } else {
  557. ip_rt_put(rt);
  558. if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
  559. skb2->dev) ||
  560. skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
  561. goto out;
  562. }
  563. /* change mtu on this route */
  564. if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
  565. if (rel_info > dst_mtu(skb_dst(skb2)))
  566. goto out;
  567. skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
  568. }
  569. if (rel_type == ICMP_REDIRECT)
  570. skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
  571. icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
  572. out:
  573. kfree_skb(skb2);
  574. return 0;
  575. }
  576. static int
  577. ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  578. u8 type, u8 code, int offset, __be32 info)
  579. {
  580. int rel_msg = 0;
  581. u8 rel_type = type;
  582. u8 rel_code = code;
  583. __u32 rel_info = ntohl(info);
  584. int err;
  585. err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
  586. &rel_msg, &rel_info, offset);
  587. if (err < 0)
  588. return err;
  589. if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
  590. struct rt6_info *rt;
  591. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  592. if (!skb2)
  593. return 0;
  594. skb_dst_drop(skb2);
  595. skb_pull(skb2, offset);
  596. skb_reset_network_header(skb2);
  597. /* Try to guess incoming interface */
  598. rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
  599. NULL, 0, 0);
  600. if (rt && rt->dst.dev)
  601. skb2->dev = rt->dst.dev;
  602. icmpv6_send(skb2, rel_type, rel_code, rel_info);
  603. ip6_rt_put(rt);
  604. kfree_skb(skb2);
  605. }
  606. return 0;
  607. }
  608. static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  609. const struct ipv6hdr *ipv6h,
  610. struct sk_buff *skb)
  611. {
  612. __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
  613. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  614. ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
  615. return IP6_ECN_decapsulate(ipv6h, skb);
  616. }
  617. static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
  618. const struct ipv6hdr *ipv6h,
  619. struct sk_buff *skb)
  620. {
  621. if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
  622. ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
  623. return IP6_ECN_decapsulate(ipv6h, skb);
  624. }
  625. __u32 ip6_tnl_get_cap(struct ip6_tnl *t,
  626. const struct in6_addr *laddr,
  627. const struct in6_addr *raddr)
  628. {
  629. struct __ip6_tnl_parm *p = &t->parms;
  630. int ltype = ipv6_addr_type(laddr);
  631. int rtype = ipv6_addr_type(raddr);
  632. __u32 flags = 0;
  633. if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
  634. flags = IP6_TNL_F_CAP_PER_PACKET;
  635. } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  636. rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
  637. !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
  638. (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
  639. if (ltype&IPV6_ADDR_UNICAST)
  640. flags |= IP6_TNL_F_CAP_XMIT;
  641. if (rtype&IPV6_ADDR_UNICAST)
  642. flags |= IP6_TNL_F_CAP_RCV;
  643. }
  644. return flags;
  645. }
  646. EXPORT_SYMBOL(ip6_tnl_get_cap);
  647. /* called with rcu_read_lock() */
  648. int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
  649. const struct in6_addr *laddr,
  650. const struct in6_addr *raddr)
  651. {
  652. struct __ip6_tnl_parm *p = &t->parms;
  653. int ret = 0;
  654. struct net *net = t->net;
  655. if ((p->flags & IP6_TNL_F_CAP_RCV) ||
  656. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  657. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
  658. struct net_device *ldev = NULL;
  659. if (p->link)
  660. ldev = dev_get_by_index_rcu(net, p->link);
  661. if ((ipv6_addr_is_multicast(laddr) ||
  662. likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
  663. likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
  664. ret = 1;
  665. }
  666. return ret;
  667. }
  668. EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
  669. static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
  670. const struct tnl_ptk_info *tpi,
  671. struct metadata_dst *tun_dst,
  672. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  673. const struct ipv6hdr *ipv6h,
  674. struct sk_buff *skb),
  675. bool log_ecn_err)
  676. {
  677. struct pcpu_sw_netstats *tstats;
  678. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  679. int err;
  680. if ((!(tpi->flags & TUNNEL_CSUM) &&
  681. (tunnel->parms.i_flags & TUNNEL_CSUM)) ||
  682. ((tpi->flags & TUNNEL_CSUM) &&
  683. !(tunnel->parms.i_flags & TUNNEL_CSUM))) {
  684. tunnel->dev->stats.rx_crc_errors++;
  685. tunnel->dev->stats.rx_errors++;
  686. goto drop;
  687. }
  688. if (tunnel->parms.i_flags & TUNNEL_SEQ) {
  689. if (!(tpi->flags & TUNNEL_SEQ) ||
  690. (tunnel->i_seqno &&
  691. (s32)(ntohl(tpi->seq) - tunnel->i_seqno) < 0)) {
  692. tunnel->dev->stats.rx_fifo_errors++;
  693. tunnel->dev->stats.rx_errors++;
  694. goto drop;
  695. }
  696. tunnel->i_seqno = ntohl(tpi->seq) + 1;
  697. }
  698. skb->protocol = tpi->proto;
  699. /* Warning: All skb pointers will be invalidated! */
  700. if (tunnel->dev->type == ARPHRD_ETHER) {
  701. if (!pskb_may_pull(skb, ETH_HLEN)) {
  702. tunnel->dev->stats.rx_length_errors++;
  703. tunnel->dev->stats.rx_errors++;
  704. goto drop;
  705. }
  706. ipv6h = ipv6_hdr(skb);
  707. skb->protocol = eth_type_trans(skb, tunnel->dev);
  708. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  709. } else {
  710. skb->dev = tunnel->dev;
  711. }
  712. skb_reset_network_header(skb);
  713. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  714. __skb_tunnel_rx(skb, tunnel->dev, tunnel->net);
  715. err = dscp_ecn_decapsulate(tunnel, ipv6h, skb);
  716. if (unlikely(err)) {
  717. if (log_ecn_err)
  718. net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
  719. &ipv6h->saddr,
  720. ipv6_get_dsfield(ipv6h));
  721. if (err > 1) {
  722. ++tunnel->dev->stats.rx_frame_errors;
  723. ++tunnel->dev->stats.rx_errors;
  724. goto drop;
  725. }
  726. }
  727. tstats = this_cpu_ptr(tunnel->dev->tstats);
  728. u64_stats_update_begin(&tstats->syncp);
  729. tstats->rx_packets++;
  730. tstats->rx_bytes += skb->len;
  731. u64_stats_update_end(&tstats->syncp);
  732. skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
  733. if (tun_dst)
  734. skb_dst_set(skb, (struct dst_entry *)tun_dst);
  735. gro_cells_receive(&tunnel->gro_cells, skb);
  736. return 0;
  737. drop:
  738. kfree_skb(skb);
  739. return 0;
  740. }
  741. int ip6_tnl_rcv(struct ip6_tnl *t, struct sk_buff *skb,
  742. const struct tnl_ptk_info *tpi,
  743. struct metadata_dst *tun_dst,
  744. bool log_ecn_err)
  745. {
  746. return __ip6_tnl_rcv(t, skb, tpi, NULL, ip6ip6_dscp_ecn_decapsulate,
  747. log_ecn_err);
  748. }
  749. EXPORT_SYMBOL(ip6_tnl_rcv);
  750. static const struct tnl_ptk_info tpi_v6 = {
  751. /* no tunnel info required for ipxip6. */
  752. .proto = htons(ETH_P_IPV6),
  753. };
  754. static const struct tnl_ptk_info tpi_v4 = {
  755. /* no tunnel info required for ipxip6. */
  756. .proto = htons(ETH_P_IP),
  757. };
  758. static int ipxip6_rcv(struct sk_buff *skb, u8 ipproto,
  759. const struct tnl_ptk_info *tpi,
  760. int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
  761. const struct ipv6hdr *ipv6h,
  762. struct sk_buff *skb))
  763. {
  764. struct ip6_tnl *t;
  765. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  766. struct metadata_dst *tun_dst = NULL;
  767. int ret = -1;
  768. rcu_read_lock();
  769. t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
  770. if (t) {
  771. u8 tproto = ACCESS_ONCE(t->parms.proto);
  772. if (tproto != ipproto && tproto != 0)
  773. goto drop;
  774. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  775. goto drop;
  776. if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr))
  777. goto drop;
  778. if (iptunnel_pull_header(skb, 0, tpi->proto, false))
  779. goto drop;
  780. if (t->parms.collect_md) {
  781. tun_dst = ipv6_tun_rx_dst(skb, 0, 0, 0);
  782. if (!tun_dst)
  783. goto drop;
  784. }
  785. ret = __ip6_tnl_rcv(t, skb, tpi, tun_dst, dscp_ecn_decapsulate,
  786. log_ecn_error);
  787. }
  788. rcu_read_unlock();
  789. return ret;
  790. drop:
  791. rcu_read_unlock();
  792. kfree_skb(skb);
  793. return 0;
  794. }
  795. static int ip4ip6_rcv(struct sk_buff *skb)
  796. {
  797. return ipxip6_rcv(skb, IPPROTO_IPIP, &tpi_v4,
  798. ip4ip6_dscp_ecn_decapsulate);
  799. }
  800. static int ip6ip6_rcv(struct sk_buff *skb)
  801. {
  802. return ipxip6_rcv(skb, IPPROTO_IPV6, &tpi_v6,
  803. ip6ip6_dscp_ecn_decapsulate);
  804. }
  805. struct ipv6_tel_txoption {
  806. struct ipv6_txoptions ops;
  807. __u8 dst_opt[8];
  808. };
  809. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  810. {
  811. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  812. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  813. opt->dst_opt[3] = 1;
  814. opt->dst_opt[4] = encap_limit;
  815. opt->dst_opt[5] = IPV6_TLV_PADN;
  816. opt->dst_opt[6] = 1;
  817. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  818. opt->ops.opt_nflen = 8;
  819. }
  820. /**
  821. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  822. * @t: the outgoing tunnel device
  823. * @hdr: IPv6 header from the incoming packet
  824. *
  825. * Description:
  826. * Avoid trivial tunneling loop by checking that tunnel exit-point
  827. * doesn't match source of incoming packet.
  828. *
  829. * Return:
  830. * 1 if conflict,
  831. * 0 else
  832. **/
  833. static inline bool
  834. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  835. {
  836. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  837. }
  838. int ip6_tnl_xmit_ctl(struct ip6_tnl *t,
  839. const struct in6_addr *laddr,
  840. const struct in6_addr *raddr)
  841. {
  842. struct __ip6_tnl_parm *p = &t->parms;
  843. int ret = 0;
  844. struct net *net = t->net;
  845. if ((p->flags & IP6_TNL_F_CAP_XMIT) ||
  846. ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
  847. (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) {
  848. struct net_device *ldev = NULL;
  849. rcu_read_lock();
  850. if (p->link)
  851. ldev = dev_get_by_index_rcu(net, p->link);
  852. if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0)))
  853. pr_warn("%s xmit: Local address not yet configured!\n",
  854. p->name);
  855. else if (!ipv6_addr_is_multicast(raddr) &&
  856. unlikely(ipv6_chk_addr(net, raddr, NULL, 0)))
  857. pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
  858. p->name);
  859. else
  860. ret = 1;
  861. rcu_read_unlock();
  862. }
  863. return ret;
  864. }
  865. EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  866. /**
  867. * ip6_tnl_xmit - encapsulate packet and send
  868. * @skb: the outgoing socket buffer
  869. * @dev: the outgoing tunnel device
  870. * @dsfield: dscp code for outer header
  871. * @fl6: flow of tunneled packet
  872. * @encap_limit: encapsulation limit
  873. * @pmtu: Path MTU is stored if packet is too big
  874. * @proto: next header value
  875. *
  876. * Description:
  877. * Build new header and do some sanity checks on the packet before sending
  878. * it.
  879. *
  880. * Return:
  881. * 0 on success
  882. * -1 fail
  883. * %-EMSGSIZE message too big. return mtu in this case.
  884. **/
  885. int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
  886. struct flowi6 *fl6, int encap_limit, __u32 *pmtu,
  887. __u8 proto)
  888. {
  889. struct ip6_tnl *t = netdev_priv(dev);
  890. struct net *net = t->net;
  891. struct net_device_stats *stats = &t->dev->stats;
  892. struct ipv6hdr *ipv6h;
  893. struct ipv6_tel_txoption opt;
  894. struct dst_entry *dst = NULL, *ndst = NULL;
  895. struct net_device *tdev;
  896. int mtu;
  897. unsigned int eth_hlen = t->dev->type == ARPHRD_ETHER ? ETH_HLEN : 0;
  898. unsigned int psh_hlen = sizeof(struct ipv6hdr) + t->encap_hlen;
  899. unsigned int max_headroom = psh_hlen;
  900. bool use_cache = false;
  901. u8 hop_limit;
  902. int err = -1;
  903. if (t->parms.collect_md) {
  904. hop_limit = skb_tunnel_info(skb)->key.ttl;
  905. goto route_lookup;
  906. } else {
  907. hop_limit = t->parms.hop_limit;
  908. }
  909. /* NBMA tunnel */
  910. if (ipv6_addr_any(&t->parms.raddr)) {
  911. if (skb->protocol == htons(ETH_P_IPV6)) {
  912. struct in6_addr *addr6;
  913. struct neighbour *neigh;
  914. int addr_type;
  915. if (!skb_dst(skb))
  916. goto tx_err_link_failure;
  917. neigh = dst_neigh_lookup(skb_dst(skb),
  918. &ipv6_hdr(skb)->daddr);
  919. if (!neigh)
  920. goto tx_err_link_failure;
  921. addr6 = (struct in6_addr *)&neigh->primary_key;
  922. addr_type = ipv6_addr_type(addr6);
  923. if (addr_type == IPV6_ADDR_ANY)
  924. addr6 = &ipv6_hdr(skb)->daddr;
  925. memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr));
  926. neigh_release(neigh);
  927. }
  928. } else if (t->parms.proto != 0 && !(t->parms.flags &
  929. (IP6_TNL_F_USE_ORIG_TCLASS |
  930. IP6_TNL_F_USE_ORIG_FWMARK))) {
  931. /* enable the cache only if neither the outer protocol nor the
  932. * routing decision depends on the current inner header value
  933. */
  934. use_cache = true;
  935. }
  936. if (use_cache)
  937. dst = dst_cache_get(&t->dst_cache);
  938. if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr))
  939. goto tx_err_link_failure;
  940. if (!dst) {
  941. route_lookup:
  942. /* add dsfield to flowlabel for route lookup */
  943. fl6->flowlabel = ip6_make_flowinfo(dsfield, fl6->flowlabel);
  944. dst = ip6_route_output(net, NULL, fl6);
  945. if (dst->error)
  946. goto tx_err_link_failure;
  947. dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0);
  948. if (IS_ERR(dst)) {
  949. err = PTR_ERR(dst);
  950. dst = NULL;
  951. goto tx_err_link_failure;
  952. }
  953. if (t->parms.collect_md &&
  954. ipv6_dev_get_saddr(net, ip6_dst_idev(dst)->dev,
  955. &fl6->daddr, 0, &fl6->saddr))
  956. goto tx_err_link_failure;
  957. ndst = dst;
  958. }
  959. tdev = dst->dev;
  960. if (tdev == dev) {
  961. stats->collisions++;
  962. net_warn_ratelimited("%s: Local routing loop detected!\n",
  963. t->parms.name);
  964. goto tx_err_dst_release;
  965. }
  966. mtu = dst_mtu(dst) - eth_hlen - psh_hlen - t->tun_hlen;
  967. if (encap_limit >= 0) {
  968. max_headroom += 8;
  969. mtu -= 8;
  970. }
  971. if (skb->protocol == htons(ETH_P_IPV6)) {
  972. if (mtu < IPV6_MIN_MTU)
  973. mtu = IPV6_MIN_MTU;
  974. } else if (mtu < 576) {
  975. mtu = 576;
  976. }
  977. if (skb_dst(skb) && !t->parms.collect_md)
  978. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  979. if (skb->len - t->tun_hlen - eth_hlen > mtu && !skb_is_gso(skb)) {
  980. *pmtu = mtu;
  981. err = -EMSGSIZE;
  982. goto tx_err_dst_release;
  983. }
  984. if (t->err_count > 0) {
  985. if (time_before(jiffies,
  986. t->err_time + IP6TUNNEL_ERR_TIMEO)) {
  987. t->err_count--;
  988. dst_link_failure(skb);
  989. } else {
  990. t->err_count = 0;
  991. }
  992. }
  993. skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev)));
  994. /*
  995. * Okay, now see if we can stuff it in the buffer as-is.
  996. */
  997. max_headroom += LL_RESERVED_SPACE(tdev);
  998. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  999. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  1000. struct sk_buff *new_skb;
  1001. new_skb = skb_realloc_headroom(skb, max_headroom);
  1002. if (!new_skb)
  1003. goto tx_err_dst_release;
  1004. if (skb->sk)
  1005. skb_set_owner_w(new_skb, skb->sk);
  1006. consume_skb(skb);
  1007. skb = new_skb;
  1008. }
  1009. if (t->parms.collect_md) {
  1010. if (t->encap.type != TUNNEL_ENCAP_NONE)
  1011. goto tx_err_dst_release;
  1012. } else {
  1013. if (use_cache && ndst)
  1014. dst_cache_set_ip6(&t->dst_cache, ndst, &fl6->saddr);
  1015. }
  1016. skb_dst_set(skb, dst);
  1017. if (encap_limit >= 0) {
  1018. init_tel_txopt(&opt, encap_limit);
  1019. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  1020. }
  1021. /* Calculate max headroom for all the headers and adjust
  1022. * needed_headroom if necessary.
  1023. */
  1024. max_headroom = LL_RESERVED_SPACE(dst->dev) + sizeof(struct ipv6hdr)
  1025. + dst->header_len + t->hlen;
  1026. if (max_headroom > dev->needed_headroom)
  1027. dev->needed_headroom = max_headroom;
  1028. err = ip6_tnl_encap(skb, t, &proto, fl6);
  1029. if (err)
  1030. return err;
  1031. skb_push(skb, sizeof(struct ipv6hdr));
  1032. skb_reset_network_header(skb);
  1033. ipv6h = ipv6_hdr(skb);
  1034. ip6_flow_hdr(ipv6h, dsfield,
  1035. ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6));
  1036. ipv6h->hop_limit = hop_limit;
  1037. ipv6h->nexthdr = proto;
  1038. ipv6h->saddr = fl6->saddr;
  1039. ipv6h->daddr = fl6->daddr;
  1040. ip6tunnel_xmit(NULL, skb, dev);
  1041. return 0;
  1042. tx_err_link_failure:
  1043. stats->tx_carrier_errors++;
  1044. dst_link_failure(skb);
  1045. tx_err_dst_release:
  1046. dst_release(dst);
  1047. return err;
  1048. }
  1049. EXPORT_SYMBOL(ip6_tnl_xmit);
  1050. static inline int
  1051. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1052. {
  1053. struct ip6_tnl *t = netdev_priv(dev);
  1054. const struct iphdr *iph = ip_hdr(skb);
  1055. int encap_limit = -1;
  1056. struct flowi6 fl6;
  1057. __u8 dsfield;
  1058. __u32 mtu;
  1059. u8 tproto;
  1060. int err;
  1061. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  1062. tproto = ACCESS_ONCE(t->parms.proto);
  1063. if (tproto != IPPROTO_IPIP && tproto != 0)
  1064. return -1;
  1065. if (t->parms.collect_md) {
  1066. struct ip_tunnel_info *tun_info;
  1067. const struct ip_tunnel_key *key;
  1068. tun_info = skb_tunnel_info(skb);
  1069. if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  1070. ip_tunnel_info_af(tun_info) != AF_INET6))
  1071. return -1;
  1072. key = &tun_info->key;
  1073. memset(&fl6, 0, sizeof(fl6));
  1074. fl6.flowi6_proto = IPPROTO_IPIP;
  1075. fl6.daddr = key->u.ipv6.dst;
  1076. fl6.flowlabel = key->label;
  1077. dsfield = ip6_tclass(key->label);
  1078. } else {
  1079. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1080. encap_limit = t->parms.encap_limit;
  1081. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1082. fl6.flowi6_proto = IPPROTO_IPIP;
  1083. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1084. dsfield = ipv4_get_dsfield(iph);
  1085. else
  1086. dsfield = ip6_tclass(t->parms.flowinfo);
  1087. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1088. fl6.flowi6_mark = skb->mark;
  1089. }
  1090. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1091. return -1;
  1092. dsfield = INET_ECN_encapsulate(dsfield, ipv4_get_dsfield(iph));
  1093. skb_set_inner_ipproto(skb, IPPROTO_IPIP);
  1094. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1095. IPPROTO_IPIP);
  1096. if (err != 0) {
  1097. /* XXX: send ICMP error even if DF is not set. */
  1098. if (err == -EMSGSIZE)
  1099. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  1100. htonl(mtu));
  1101. return -1;
  1102. }
  1103. return 0;
  1104. }
  1105. static inline int
  1106. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1107. {
  1108. struct ip6_tnl *t = netdev_priv(dev);
  1109. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  1110. int encap_limit = -1;
  1111. __u16 offset;
  1112. struct flowi6 fl6;
  1113. __u8 dsfield;
  1114. __u32 mtu;
  1115. u8 tproto;
  1116. int err;
  1117. tproto = ACCESS_ONCE(t->parms.proto);
  1118. if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
  1119. ip6_tnl_addr_conflict(t, ipv6h))
  1120. return -1;
  1121. if (t->parms.collect_md) {
  1122. struct ip_tunnel_info *tun_info;
  1123. const struct ip_tunnel_key *key;
  1124. tun_info = skb_tunnel_info(skb);
  1125. if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
  1126. ip_tunnel_info_af(tun_info) != AF_INET6))
  1127. return -1;
  1128. key = &tun_info->key;
  1129. memset(&fl6, 0, sizeof(fl6));
  1130. fl6.flowi6_proto = IPPROTO_IPV6;
  1131. fl6.daddr = key->u.ipv6.dst;
  1132. fl6.flowlabel = key->label;
  1133. dsfield = ip6_tclass(key->label);
  1134. } else {
  1135. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  1136. /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
  1137. ipv6h = ipv6_hdr(skb);
  1138. if (offset > 0) {
  1139. struct ipv6_tlv_tnl_enc_lim *tel;
  1140. tel = (void *)&skb_network_header(skb)[offset];
  1141. if (tel->encap_limit == 0) {
  1142. icmpv6_send(skb, ICMPV6_PARAMPROB,
  1143. ICMPV6_HDR_FIELD, offset + 2);
  1144. return -1;
  1145. }
  1146. encap_limit = tel->encap_limit - 1;
  1147. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) {
  1148. encap_limit = t->parms.encap_limit;
  1149. }
  1150. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1151. fl6.flowi6_proto = IPPROTO_IPV6;
  1152. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1153. dsfield = ipv6_get_dsfield(ipv6h);
  1154. else
  1155. dsfield = ip6_tclass(t->parms.flowinfo);
  1156. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  1157. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  1158. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1159. fl6.flowi6_mark = skb->mark;
  1160. }
  1161. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1162. return -1;
  1163. dsfield = INET_ECN_encapsulate(dsfield, ipv6_get_dsfield(ipv6h));
  1164. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  1165. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1166. IPPROTO_IPV6);
  1167. if (err != 0) {
  1168. if (err == -EMSGSIZE)
  1169. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  1170. return -1;
  1171. }
  1172. return 0;
  1173. }
  1174. static netdev_tx_t
  1175. ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1176. {
  1177. struct ip6_tnl *t = netdev_priv(dev);
  1178. struct net_device_stats *stats = &t->dev->stats;
  1179. int ret;
  1180. switch (skb->protocol) {
  1181. case htons(ETH_P_IP):
  1182. ret = ip4ip6_tnl_xmit(skb, dev);
  1183. break;
  1184. case htons(ETH_P_IPV6):
  1185. ret = ip6ip6_tnl_xmit(skb, dev);
  1186. break;
  1187. default:
  1188. goto tx_err;
  1189. }
  1190. if (ret < 0)
  1191. goto tx_err;
  1192. return NETDEV_TX_OK;
  1193. tx_err:
  1194. stats->tx_errors++;
  1195. stats->tx_dropped++;
  1196. kfree_skb(skb);
  1197. return NETDEV_TX_OK;
  1198. }
  1199. static void ip6_tnl_link_config(struct ip6_tnl *t)
  1200. {
  1201. struct net_device *dev = t->dev;
  1202. struct __ip6_tnl_parm *p = &t->parms;
  1203. struct flowi6 *fl6 = &t->fl.u.ip6;
  1204. int t_hlen;
  1205. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  1206. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  1207. /* Set up flowi template */
  1208. fl6->saddr = p->laddr;
  1209. fl6->daddr = p->raddr;
  1210. fl6->flowi6_oif = p->link;
  1211. fl6->flowlabel = 0;
  1212. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  1213. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  1214. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  1215. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  1216. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  1217. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  1218. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  1219. dev->flags |= IFF_POINTOPOINT;
  1220. else
  1221. dev->flags &= ~IFF_POINTOPOINT;
  1222. t->tun_hlen = 0;
  1223. t->hlen = t->encap_hlen + t->tun_hlen;
  1224. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1225. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  1226. int strict = (ipv6_addr_type(&p->raddr) &
  1227. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1228. struct rt6_info *rt = rt6_lookup(t->net,
  1229. &p->raddr, &p->laddr,
  1230. p->link, strict);
  1231. if (!rt)
  1232. return;
  1233. if (rt->dst.dev) {
  1234. dev->hard_header_len = rt->dst.dev->hard_header_len +
  1235. t_hlen;
  1236. dev->mtu = rt->dst.dev->mtu - t_hlen;
  1237. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1238. dev->mtu -= 8;
  1239. if (dev->mtu < IPV6_MIN_MTU)
  1240. dev->mtu = IPV6_MIN_MTU;
  1241. }
  1242. ip6_rt_put(rt);
  1243. }
  1244. }
  1245. /**
  1246. * ip6_tnl_change - update the tunnel parameters
  1247. * @t: tunnel to be changed
  1248. * @p: tunnel configuration parameters
  1249. *
  1250. * Description:
  1251. * ip6_tnl_change() updates the tunnel parameters
  1252. **/
  1253. static int
  1254. ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  1255. {
  1256. t->parms.laddr = p->laddr;
  1257. t->parms.raddr = p->raddr;
  1258. t->parms.flags = p->flags;
  1259. t->parms.hop_limit = p->hop_limit;
  1260. t->parms.encap_limit = p->encap_limit;
  1261. t->parms.flowinfo = p->flowinfo;
  1262. t->parms.link = p->link;
  1263. t->parms.proto = p->proto;
  1264. dst_cache_reset(&t->dst_cache);
  1265. ip6_tnl_link_config(t);
  1266. return 0;
  1267. }
  1268. static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1269. {
  1270. struct net *net = t->net;
  1271. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1272. int err;
  1273. ip6_tnl_unlink(ip6n, t);
  1274. synchronize_net();
  1275. err = ip6_tnl_change(t, p);
  1276. ip6_tnl_link(ip6n, t);
  1277. netdev_state_change(t->dev);
  1278. return err;
  1279. }
  1280. static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1281. {
  1282. /* for default tnl0 device allow to change only the proto */
  1283. t->parms.proto = p->proto;
  1284. netdev_state_change(t->dev);
  1285. return 0;
  1286. }
  1287. static void
  1288. ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
  1289. {
  1290. p->laddr = u->laddr;
  1291. p->raddr = u->raddr;
  1292. p->flags = u->flags;
  1293. p->hop_limit = u->hop_limit;
  1294. p->encap_limit = u->encap_limit;
  1295. p->flowinfo = u->flowinfo;
  1296. p->link = u->link;
  1297. p->proto = u->proto;
  1298. memcpy(p->name, u->name, sizeof(u->name));
  1299. }
  1300. static void
  1301. ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
  1302. {
  1303. u->laddr = p->laddr;
  1304. u->raddr = p->raddr;
  1305. u->flags = p->flags;
  1306. u->hop_limit = p->hop_limit;
  1307. u->encap_limit = p->encap_limit;
  1308. u->flowinfo = p->flowinfo;
  1309. u->link = p->link;
  1310. u->proto = p->proto;
  1311. memcpy(u->name, p->name, sizeof(u->name));
  1312. }
  1313. /**
  1314. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1315. * @dev: virtual device associated with tunnel
  1316. * @ifr: parameters passed from userspace
  1317. * @cmd: command to be performed
  1318. *
  1319. * Description:
  1320. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1321. * from userspace.
  1322. *
  1323. * The possible commands are the following:
  1324. * %SIOCGETTUNNEL: get tunnel parameters for device
  1325. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1326. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1327. * %SIOCDELTUNNEL: delete tunnel
  1328. *
  1329. * The fallback device "ip6tnl0", created during module
  1330. * initialization, can be used for creating other tunnel devices.
  1331. *
  1332. * Return:
  1333. * 0 on success,
  1334. * %-EFAULT if unable to copy data to or from userspace,
  1335. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1336. * %-EINVAL if passed tunnel parameters are invalid,
  1337. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1338. * %-ENODEV if attempting to change or delete a nonexisting device
  1339. **/
  1340. static int
  1341. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1342. {
  1343. int err = 0;
  1344. struct ip6_tnl_parm p;
  1345. struct __ip6_tnl_parm p1;
  1346. struct ip6_tnl *t = netdev_priv(dev);
  1347. struct net *net = t->net;
  1348. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1349. memset(&p1, 0, sizeof(p1));
  1350. switch (cmd) {
  1351. case SIOCGETTUNNEL:
  1352. if (dev == ip6n->fb_tnl_dev) {
  1353. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1354. err = -EFAULT;
  1355. break;
  1356. }
  1357. ip6_tnl_parm_from_user(&p1, &p);
  1358. t = ip6_tnl_locate(net, &p1, 0);
  1359. if (IS_ERR(t))
  1360. t = netdev_priv(dev);
  1361. } else {
  1362. memset(&p, 0, sizeof(p));
  1363. }
  1364. ip6_tnl_parm_to_user(&p, &t->parms);
  1365. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
  1366. err = -EFAULT;
  1367. }
  1368. break;
  1369. case SIOCADDTUNNEL:
  1370. case SIOCCHGTUNNEL:
  1371. err = -EPERM;
  1372. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1373. break;
  1374. err = -EFAULT;
  1375. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1376. break;
  1377. err = -EINVAL;
  1378. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1379. p.proto != 0)
  1380. break;
  1381. ip6_tnl_parm_from_user(&p1, &p);
  1382. t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1383. if (cmd == SIOCCHGTUNNEL) {
  1384. if (!IS_ERR(t)) {
  1385. if (t->dev != dev) {
  1386. err = -EEXIST;
  1387. break;
  1388. }
  1389. } else
  1390. t = netdev_priv(dev);
  1391. if (dev == ip6n->fb_tnl_dev)
  1392. err = ip6_tnl0_update(t, &p1);
  1393. else
  1394. err = ip6_tnl_update(t, &p1);
  1395. }
  1396. if (!IS_ERR(t)) {
  1397. err = 0;
  1398. ip6_tnl_parm_to_user(&p, &t->parms);
  1399. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1400. err = -EFAULT;
  1401. } else {
  1402. err = PTR_ERR(t);
  1403. }
  1404. break;
  1405. case SIOCDELTUNNEL:
  1406. err = -EPERM;
  1407. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1408. break;
  1409. if (dev == ip6n->fb_tnl_dev) {
  1410. err = -EFAULT;
  1411. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1412. break;
  1413. err = -ENOENT;
  1414. ip6_tnl_parm_from_user(&p1, &p);
  1415. t = ip6_tnl_locate(net, &p1, 0);
  1416. if (IS_ERR(t))
  1417. break;
  1418. err = -EPERM;
  1419. if (t->dev == ip6n->fb_tnl_dev)
  1420. break;
  1421. dev = t->dev;
  1422. }
  1423. err = 0;
  1424. unregister_netdevice(dev);
  1425. break;
  1426. default:
  1427. err = -EINVAL;
  1428. }
  1429. return err;
  1430. }
  1431. /**
  1432. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1433. * @dev: virtual device associated with tunnel
  1434. * @new_mtu: the new mtu
  1435. *
  1436. * Return:
  1437. * 0 on success,
  1438. * %-EINVAL if mtu too small
  1439. **/
  1440. int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1441. {
  1442. struct ip6_tnl *tnl = netdev_priv(dev);
  1443. if (tnl->parms.proto == IPPROTO_IPIP) {
  1444. if (new_mtu < 68)
  1445. return -EINVAL;
  1446. } else {
  1447. if (new_mtu < IPV6_MIN_MTU)
  1448. return -EINVAL;
  1449. }
  1450. if (new_mtu > 0xFFF8 - dev->hard_header_len)
  1451. return -EINVAL;
  1452. dev->mtu = new_mtu;
  1453. return 0;
  1454. }
  1455. EXPORT_SYMBOL(ip6_tnl_change_mtu);
  1456. int ip6_tnl_get_iflink(const struct net_device *dev)
  1457. {
  1458. struct ip6_tnl *t = netdev_priv(dev);
  1459. return t->parms.link;
  1460. }
  1461. EXPORT_SYMBOL(ip6_tnl_get_iflink);
  1462. int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
  1463. unsigned int num)
  1464. {
  1465. if (num >= MAX_IPTUN_ENCAP_OPS)
  1466. return -ERANGE;
  1467. return !cmpxchg((const struct ip6_tnl_encap_ops **)
  1468. &ip6tun_encaps[num],
  1469. NULL, ops) ? 0 : -1;
  1470. }
  1471. EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
  1472. int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
  1473. unsigned int num)
  1474. {
  1475. int ret;
  1476. if (num >= MAX_IPTUN_ENCAP_OPS)
  1477. return -ERANGE;
  1478. ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
  1479. &ip6tun_encaps[num],
  1480. ops, NULL) == ops) ? 0 : -1;
  1481. synchronize_net();
  1482. return ret;
  1483. }
  1484. EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
  1485. int ip6_tnl_encap_setup(struct ip6_tnl *t,
  1486. struct ip_tunnel_encap *ipencap)
  1487. {
  1488. int hlen;
  1489. memset(&t->encap, 0, sizeof(t->encap));
  1490. hlen = ip6_encap_hlen(ipencap);
  1491. if (hlen < 0)
  1492. return hlen;
  1493. t->encap.type = ipencap->type;
  1494. t->encap.sport = ipencap->sport;
  1495. t->encap.dport = ipencap->dport;
  1496. t->encap.flags = ipencap->flags;
  1497. t->encap_hlen = hlen;
  1498. t->hlen = t->encap_hlen + t->tun_hlen;
  1499. return 0;
  1500. }
  1501. EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
  1502. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1503. .ndo_init = ip6_tnl_dev_init,
  1504. .ndo_uninit = ip6_tnl_dev_uninit,
  1505. .ndo_start_xmit = ip6_tnl_start_xmit,
  1506. .ndo_do_ioctl = ip6_tnl_ioctl,
  1507. .ndo_change_mtu = ip6_tnl_change_mtu,
  1508. .ndo_get_stats = ip6_get_stats,
  1509. .ndo_get_iflink = ip6_tnl_get_iflink,
  1510. };
  1511. #define IPXIPX_FEATURES (NETIF_F_SG | \
  1512. NETIF_F_FRAGLIST | \
  1513. NETIF_F_HIGHDMA | \
  1514. NETIF_F_GSO_SOFTWARE | \
  1515. NETIF_F_HW_CSUM)
  1516. /**
  1517. * ip6_tnl_dev_setup - setup virtual tunnel device
  1518. * @dev: virtual device associated with tunnel
  1519. *
  1520. * Description:
  1521. * Initialize function pointers and device parameters
  1522. **/
  1523. static void ip6_tnl_dev_setup(struct net_device *dev)
  1524. {
  1525. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1526. dev->destructor = ip6_dev_free;
  1527. dev->type = ARPHRD_TUNNEL6;
  1528. dev->flags |= IFF_NOARP;
  1529. dev->addr_len = sizeof(struct in6_addr);
  1530. dev->features |= NETIF_F_LLTX;
  1531. netif_keep_dst(dev);
  1532. dev->features |= IPXIPX_FEATURES;
  1533. dev->hw_features |= IPXIPX_FEATURES;
  1534. /* This perm addr will be used as interface identifier by IPv6 */
  1535. dev->addr_assign_type = NET_ADDR_RANDOM;
  1536. eth_random_addr(dev->perm_addr);
  1537. }
  1538. /**
  1539. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1540. * @dev: virtual device associated with tunnel
  1541. **/
  1542. static inline int
  1543. ip6_tnl_dev_init_gen(struct net_device *dev)
  1544. {
  1545. struct ip6_tnl *t = netdev_priv(dev);
  1546. int ret;
  1547. int t_hlen;
  1548. t->dev = dev;
  1549. t->net = dev_net(dev);
  1550. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1551. if (!dev->tstats)
  1552. return -ENOMEM;
  1553. ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
  1554. if (ret)
  1555. goto free_stats;
  1556. ret = gro_cells_init(&t->gro_cells, dev);
  1557. if (ret)
  1558. goto destroy_dst;
  1559. t->tun_hlen = 0;
  1560. t->hlen = t->encap_hlen + t->tun_hlen;
  1561. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1562. dev->type = ARPHRD_TUNNEL6;
  1563. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1564. dev->mtu = ETH_DATA_LEN - t_hlen;
  1565. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1566. dev->mtu -= 8;
  1567. return 0;
  1568. destroy_dst:
  1569. dst_cache_destroy(&t->dst_cache);
  1570. free_stats:
  1571. free_percpu(dev->tstats);
  1572. dev->tstats = NULL;
  1573. return ret;
  1574. }
  1575. /**
  1576. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1577. * @dev: virtual device associated with tunnel
  1578. **/
  1579. static int ip6_tnl_dev_init(struct net_device *dev)
  1580. {
  1581. struct ip6_tnl *t = netdev_priv(dev);
  1582. int err = ip6_tnl_dev_init_gen(dev);
  1583. if (err)
  1584. return err;
  1585. ip6_tnl_link_config(t);
  1586. if (t->parms.collect_md) {
  1587. dev->features |= NETIF_F_NETNS_LOCAL;
  1588. netif_keep_dst(dev);
  1589. }
  1590. return 0;
  1591. }
  1592. /**
  1593. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1594. * @dev: fallback device
  1595. *
  1596. * Return: 0
  1597. **/
  1598. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1599. {
  1600. struct ip6_tnl *t = netdev_priv(dev);
  1601. struct net *net = dev_net(dev);
  1602. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1603. t->parms.proto = IPPROTO_IPV6;
  1604. dev_hold(dev);
  1605. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1606. return 0;
  1607. }
  1608. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
  1609. {
  1610. u8 proto;
  1611. if (!data || !data[IFLA_IPTUN_PROTO])
  1612. return 0;
  1613. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1614. if (proto != IPPROTO_IPV6 &&
  1615. proto != IPPROTO_IPIP &&
  1616. proto != 0)
  1617. return -EINVAL;
  1618. return 0;
  1619. }
  1620. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1621. struct __ip6_tnl_parm *parms)
  1622. {
  1623. memset(parms, 0, sizeof(*parms));
  1624. if (!data)
  1625. return;
  1626. if (data[IFLA_IPTUN_LINK])
  1627. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1628. if (data[IFLA_IPTUN_LOCAL])
  1629. parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
  1630. if (data[IFLA_IPTUN_REMOTE])
  1631. parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
  1632. if (data[IFLA_IPTUN_TTL])
  1633. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1634. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1635. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1636. if (data[IFLA_IPTUN_FLOWINFO])
  1637. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1638. if (data[IFLA_IPTUN_FLAGS])
  1639. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1640. if (data[IFLA_IPTUN_PROTO])
  1641. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1642. if (data[IFLA_IPTUN_COLLECT_METADATA])
  1643. parms->collect_md = true;
  1644. }
  1645. static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
  1646. struct ip_tunnel_encap *ipencap)
  1647. {
  1648. bool ret = false;
  1649. memset(ipencap, 0, sizeof(*ipencap));
  1650. if (!data)
  1651. return ret;
  1652. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1653. ret = true;
  1654. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1655. }
  1656. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1657. ret = true;
  1658. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1659. }
  1660. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1661. ret = true;
  1662. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1663. }
  1664. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1665. ret = true;
  1666. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1667. }
  1668. return ret;
  1669. }
  1670. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1671. struct nlattr *tb[], struct nlattr *data[])
  1672. {
  1673. struct net *net = dev_net(dev);
  1674. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1675. struct ip_tunnel_encap ipencap;
  1676. struct ip6_tnl *nt, *t;
  1677. int err;
  1678. nt = netdev_priv(dev);
  1679. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1680. err = ip6_tnl_encap_setup(nt, &ipencap);
  1681. if (err < 0)
  1682. return err;
  1683. }
  1684. ip6_tnl_netlink_parms(data, &nt->parms);
  1685. if (nt->parms.collect_md) {
  1686. if (rtnl_dereference(ip6n->collect_md_tun))
  1687. return -EEXIST;
  1688. } else {
  1689. t = ip6_tnl_locate(net, &nt->parms, 0);
  1690. if (!IS_ERR(t))
  1691. return -EEXIST;
  1692. }
  1693. err = ip6_tnl_create2(dev);
  1694. if (!err && tb[IFLA_MTU])
  1695. ip6_tnl_change_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
  1696. return err;
  1697. }
  1698. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1699. struct nlattr *data[])
  1700. {
  1701. struct ip6_tnl *t = netdev_priv(dev);
  1702. struct __ip6_tnl_parm p;
  1703. struct net *net = t->net;
  1704. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1705. struct ip_tunnel_encap ipencap;
  1706. if (dev == ip6n->fb_tnl_dev)
  1707. return -EINVAL;
  1708. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1709. int err = ip6_tnl_encap_setup(t, &ipencap);
  1710. if (err < 0)
  1711. return err;
  1712. }
  1713. ip6_tnl_netlink_parms(data, &p);
  1714. if (p.collect_md)
  1715. return -EINVAL;
  1716. t = ip6_tnl_locate(net, &p, 0);
  1717. if (!IS_ERR(t)) {
  1718. if (t->dev != dev)
  1719. return -EEXIST;
  1720. } else
  1721. t = netdev_priv(dev);
  1722. return ip6_tnl_update(t, &p);
  1723. }
  1724. static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
  1725. {
  1726. struct net *net = dev_net(dev);
  1727. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1728. if (dev != ip6n->fb_tnl_dev)
  1729. unregister_netdevice_queue(dev, head);
  1730. }
  1731. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1732. {
  1733. return
  1734. /* IFLA_IPTUN_LINK */
  1735. nla_total_size(4) +
  1736. /* IFLA_IPTUN_LOCAL */
  1737. nla_total_size(sizeof(struct in6_addr)) +
  1738. /* IFLA_IPTUN_REMOTE */
  1739. nla_total_size(sizeof(struct in6_addr)) +
  1740. /* IFLA_IPTUN_TTL */
  1741. nla_total_size(1) +
  1742. /* IFLA_IPTUN_ENCAP_LIMIT */
  1743. nla_total_size(1) +
  1744. /* IFLA_IPTUN_FLOWINFO */
  1745. nla_total_size(4) +
  1746. /* IFLA_IPTUN_FLAGS */
  1747. nla_total_size(4) +
  1748. /* IFLA_IPTUN_PROTO */
  1749. nla_total_size(1) +
  1750. /* IFLA_IPTUN_ENCAP_TYPE */
  1751. nla_total_size(2) +
  1752. /* IFLA_IPTUN_ENCAP_FLAGS */
  1753. nla_total_size(2) +
  1754. /* IFLA_IPTUN_ENCAP_SPORT */
  1755. nla_total_size(2) +
  1756. /* IFLA_IPTUN_ENCAP_DPORT */
  1757. nla_total_size(2) +
  1758. /* IFLA_IPTUN_COLLECT_METADATA */
  1759. nla_total_size(0) +
  1760. 0;
  1761. }
  1762. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1763. {
  1764. struct ip6_tnl *tunnel = netdev_priv(dev);
  1765. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1766. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1767. nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
  1768. nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
  1769. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1770. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1771. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1772. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1773. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
  1774. goto nla_put_failure;
  1775. if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE, tunnel->encap.type) ||
  1776. nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT, tunnel->encap.sport) ||
  1777. nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT, tunnel->encap.dport) ||
  1778. nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS, tunnel->encap.flags))
  1779. goto nla_put_failure;
  1780. if (parm->collect_md)
  1781. if (nla_put_flag(skb, IFLA_IPTUN_COLLECT_METADATA))
  1782. goto nla_put_failure;
  1783. return 0;
  1784. nla_put_failure:
  1785. return -EMSGSIZE;
  1786. }
  1787. struct net *ip6_tnl_get_link_net(const struct net_device *dev)
  1788. {
  1789. struct ip6_tnl *tunnel = netdev_priv(dev);
  1790. return tunnel->net;
  1791. }
  1792. EXPORT_SYMBOL(ip6_tnl_get_link_net);
  1793. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1794. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1795. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1796. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1797. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1798. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1799. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1800. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1801. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1802. [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
  1803. [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
  1804. [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
  1805. [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
  1806. [IFLA_IPTUN_COLLECT_METADATA] = { .type = NLA_FLAG },
  1807. };
  1808. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1809. .kind = "ip6tnl",
  1810. .maxtype = IFLA_IPTUN_MAX,
  1811. .policy = ip6_tnl_policy,
  1812. .priv_size = sizeof(struct ip6_tnl),
  1813. .setup = ip6_tnl_dev_setup,
  1814. .validate = ip6_tnl_validate,
  1815. .newlink = ip6_tnl_newlink,
  1816. .changelink = ip6_tnl_changelink,
  1817. .dellink = ip6_tnl_dellink,
  1818. .get_size = ip6_tnl_get_size,
  1819. .fill_info = ip6_tnl_fill_info,
  1820. .get_link_net = ip6_tnl_get_link_net,
  1821. };
  1822. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1823. .handler = ip4ip6_rcv,
  1824. .err_handler = ip4ip6_err,
  1825. .priority = 1,
  1826. };
  1827. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1828. .handler = ip6ip6_rcv,
  1829. .err_handler = ip6ip6_err,
  1830. .priority = 1,
  1831. };
  1832. static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
  1833. {
  1834. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1835. struct net_device *dev, *aux;
  1836. int h;
  1837. struct ip6_tnl *t;
  1838. LIST_HEAD(list);
  1839. for_each_netdev_safe(net, dev, aux)
  1840. if (dev->rtnl_link_ops == &ip6_link_ops)
  1841. unregister_netdevice_queue(dev, &list);
  1842. for (h = 0; h < IP6_TUNNEL_HASH_SIZE; h++) {
  1843. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1844. while (t) {
  1845. /* If dev is in the same netns, it has already
  1846. * been added to the list by the previous loop.
  1847. */
  1848. if (!net_eq(dev_net(t->dev), net))
  1849. unregister_netdevice_queue(t->dev, &list);
  1850. t = rtnl_dereference(t->next);
  1851. }
  1852. }
  1853. unregister_netdevice_many(&list);
  1854. }
  1855. static int __net_init ip6_tnl_init_net(struct net *net)
  1856. {
  1857. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1858. struct ip6_tnl *t = NULL;
  1859. int err;
  1860. ip6n->tnls[0] = ip6n->tnls_wc;
  1861. ip6n->tnls[1] = ip6n->tnls_r_l;
  1862. err = -ENOMEM;
  1863. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1864. NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
  1865. if (!ip6n->fb_tnl_dev)
  1866. goto err_alloc_dev;
  1867. dev_net_set(ip6n->fb_tnl_dev, net);
  1868. ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
  1869. /* FB netdevice is special: we have one, and only one per netns.
  1870. * Allowing to move it to another netns is clearly unsafe.
  1871. */
  1872. ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
  1873. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1874. if (err < 0)
  1875. goto err_register;
  1876. err = register_netdev(ip6n->fb_tnl_dev);
  1877. if (err < 0)
  1878. goto err_register;
  1879. t = netdev_priv(ip6n->fb_tnl_dev);
  1880. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1881. return 0;
  1882. err_register:
  1883. ip6_dev_free(ip6n->fb_tnl_dev);
  1884. err_alloc_dev:
  1885. return err;
  1886. }
  1887. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1888. {
  1889. rtnl_lock();
  1890. ip6_tnl_destroy_tunnels(net);
  1891. rtnl_unlock();
  1892. }
  1893. static struct pernet_operations ip6_tnl_net_ops = {
  1894. .init = ip6_tnl_init_net,
  1895. .exit = ip6_tnl_exit_net,
  1896. .id = &ip6_tnl_net_id,
  1897. .size = sizeof(struct ip6_tnl_net),
  1898. };
  1899. /**
  1900. * ip6_tunnel_init - register protocol and reserve needed resources
  1901. *
  1902. * Return: 0 on success
  1903. **/
  1904. static int __init ip6_tunnel_init(void)
  1905. {
  1906. int err;
  1907. if (!ipv6_mod_enabled())
  1908. return -EOPNOTSUPP;
  1909. err = register_pernet_device(&ip6_tnl_net_ops);
  1910. if (err < 0)
  1911. goto out_pernet;
  1912. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1913. if (err < 0) {
  1914. pr_err("%s: can't register ip4ip6\n", __func__);
  1915. goto out_ip4ip6;
  1916. }
  1917. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1918. if (err < 0) {
  1919. pr_err("%s: can't register ip6ip6\n", __func__);
  1920. goto out_ip6ip6;
  1921. }
  1922. err = rtnl_link_register(&ip6_link_ops);
  1923. if (err < 0)
  1924. goto rtnl_link_failed;
  1925. return 0;
  1926. rtnl_link_failed:
  1927. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1928. out_ip6ip6:
  1929. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1930. out_ip4ip6:
  1931. unregister_pernet_device(&ip6_tnl_net_ops);
  1932. out_pernet:
  1933. return err;
  1934. }
  1935. /**
  1936. * ip6_tunnel_cleanup - free resources and unregister protocol
  1937. **/
  1938. static void __exit ip6_tunnel_cleanup(void)
  1939. {
  1940. rtnl_link_unregister(&ip6_link_ops);
  1941. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1942. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1943. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1944. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1945. unregister_pernet_device(&ip6_tnl_net_ops);
  1946. }
  1947. module_init(ip6_tunnel_init);
  1948. module_exit(ip6_tunnel_cleanup);