ip6_tunnel.c 45 KB

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