ip6_tunnel.c 45 KB

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