ip6_tunnel.c 46 KB

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