ip6_tunnel.c 46 KB

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