ip6_tunnel.c 46 KB

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