ip6_tunnel.c 44 KB

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