ip6_tunnel.c 56 KB

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