ip6_tunnel.c 58 KB

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