ip6_tunnel.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  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 <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_NETDEV("ip6tnl0");
  60. #ifdef IP6_TNL_DEBUG
  61. #define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
  62. #else
  63. #define IP6_TNL_TRACE(x...) do {;} while(0)
  64. #endif
  65. #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
  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. secpath_reset(skb);
  687. skb->mac_header = skb->network_header;
  688. skb_reset_network_header(skb);
  689. skb->protocol = htons(protocol);
  690. skb->pkt_type = PACKET_HOST;
  691. memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
  692. __skb_tunnel_rx(skb, t->dev);
  693. err = dscp_ecn_decapsulate(t, ipv6h, skb);
  694. if (unlikely(err)) {
  695. if (log_ecn_error)
  696. net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
  697. &ipv6h->saddr,
  698. ipv6_get_dsfield(ipv6h));
  699. if (err > 1) {
  700. ++t->dev->stats.rx_frame_errors;
  701. ++t->dev->stats.rx_errors;
  702. rcu_read_unlock();
  703. goto discard;
  704. }
  705. }
  706. tstats = this_cpu_ptr(t->dev->tstats);
  707. tstats->rx_packets++;
  708. tstats->rx_bytes += skb->len;
  709. if (!net_eq(t->net, dev_net(t->dev)))
  710. skb_scrub_packet(skb);
  711. netif_rx(skb);
  712. rcu_read_unlock();
  713. return 0;
  714. }
  715. rcu_read_unlock();
  716. return 1;
  717. discard:
  718. kfree_skb(skb);
  719. return 0;
  720. }
  721. static int ip4ip6_rcv(struct sk_buff *skb)
  722. {
  723. return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
  724. ip4ip6_dscp_ecn_decapsulate);
  725. }
  726. static int ip6ip6_rcv(struct sk_buff *skb)
  727. {
  728. return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
  729. ip6ip6_dscp_ecn_decapsulate);
  730. }
  731. struct ipv6_tel_txoption {
  732. struct ipv6_txoptions ops;
  733. __u8 dst_opt[8];
  734. };
  735. static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
  736. {
  737. memset(opt, 0, sizeof(struct ipv6_tel_txoption));
  738. opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
  739. opt->dst_opt[3] = 1;
  740. opt->dst_opt[4] = encap_limit;
  741. opt->dst_opt[5] = IPV6_TLV_PADN;
  742. opt->dst_opt[6] = 1;
  743. opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
  744. opt->ops.opt_nflen = 8;
  745. }
  746. /**
  747. * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
  748. * @t: the outgoing tunnel device
  749. * @hdr: IPv6 header from the incoming packet
  750. *
  751. * Description:
  752. * Avoid trivial tunneling loop by checking that tunnel exit-point
  753. * doesn't match source of incoming packet.
  754. *
  755. * Return:
  756. * 1 if conflict,
  757. * 0 else
  758. **/
  759. static inline bool
  760. ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
  761. {
  762. return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
  763. }
  764. int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
  765. {
  766. struct __ip6_tnl_parm *p = &t->parms;
  767. int ret = 0;
  768. struct net *net = t->net;
  769. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  770. struct net_device *ldev = NULL;
  771. rcu_read_lock();
  772. if (p->link)
  773. ldev = dev_get_by_index_rcu(net, p->link);
  774. if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
  775. pr_warn("%s xmit: Local address not yet configured!\n",
  776. p->name);
  777. else if (!ipv6_addr_is_multicast(&p->raddr) &&
  778. unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
  779. pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
  780. p->name);
  781. else
  782. ret = 1;
  783. rcu_read_unlock();
  784. }
  785. return ret;
  786. }
  787. EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
  788. /**
  789. * ip6_tnl_xmit2 - encapsulate packet and send
  790. * @skb: the outgoing socket buffer
  791. * @dev: the outgoing tunnel device
  792. * @dsfield: dscp code for outer header
  793. * @fl: flow of tunneled packet
  794. * @encap_limit: encapsulation limit
  795. * @pmtu: Path MTU is stored if packet is too big
  796. *
  797. * Description:
  798. * Build new header and do some sanity checks on the packet before sending
  799. * it.
  800. *
  801. * Return:
  802. * 0 on success
  803. * -1 fail
  804. * %-EMSGSIZE message too big. return mtu in this case.
  805. **/
  806. static int ip6_tnl_xmit2(struct sk_buff *skb,
  807. struct net_device *dev,
  808. __u8 dsfield,
  809. struct flowi6 *fl6,
  810. int encap_limit,
  811. __u32 *pmtu)
  812. {
  813. struct ip6_tnl *t = netdev_priv(dev);
  814. struct net *net = t->net;
  815. struct net_device_stats *stats = &t->dev->stats;
  816. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  817. struct ipv6_tel_txoption opt;
  818. struct dst_entry *dst = NULL, *ndst = NULL;
  819. struct net_device *tdev;
  820. int mtu;
  821. unsigned int max_headroom = sizeof(struct ipv6hdr);
  822. u8 proto;
  823. int err = -1;
  824. if (!fl6->flowi6_mark)
  825. dst = ip6_tnl_dst_check(t);
  826. if (!dst) {
  827. ndst = ip6_route_output(net, NULL, fl6);
  828. if (ndst->error)
  829. goto tx_err_link_failure;
  830. ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
  831. if (IS_ERR(ndst)) {
  832. err = PTR_ERR(ndst);
  833. ndst = NULL;
  834. goto tx_err_link_failure;
  835. }
  836. dst = ndst;
  837. }
  838. tdev = dst->dev;
  839. if (tdev == dev) {
  840. stats->collisions++;
  841. net_warn_ratelimited("%s: Local routing loop detected!\n",
  842. t->parms.name);
  843. goto tx_err_dst_release;
  844. }
  845. mtu = dst_mtu(dst) - sizeof (*ipv6h);
  846. if (encap_limit >= 0) {
  847. max_headroom += 8;
  848. mtu -= 8;
  849. }
  850. if (mtu < IPV6_MIN_MTU)
  851. mtu = IPV6_MIN_MTU;
  852. if (skb_dst(skb))
  853. skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
  854. if (skb->len > mtu) {
  855. *pmtu = mtu;
  856. err = -EMSGSIZE;
  857. goto tx_err_dst_release;
  858. }
  859. if (!net_eq(t->net, dev_net(dev)))
  860. skb_scrub_packet(skb);
  861. /*
  862. * Okay, now see if we can stuff it in the buffer as-is.
  863. */
  864. max_headroom += LL_RESERVED_SPACE(tdev);
  865. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  866. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  867. struct sk_buff *new_skb;
  868. if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
  869. goto tx_err_dst_release;
  870. if (skb->sk)
  871. skb_set_owner_w(new_skb, skb->sk);
  872. consume_skb(skb);
  873. skb = new_skb;
  874. }
  875. skb_dst_drop(skb);
  876. if (fl6->flowi6_mark) {
  877. skb_dst_set(skb, dst);
  878. ndst = NULL;
  879. } else {
  880. skb_dst_set_noref(skb, dst);
  881. }
  882. skb->transport_header = skb->network_header;
  883. proto = fl6->flowi6_proto;
  884. if (encap_limit >= 0) {
  885. init_tel_txopt(&opt, encap_limit);
  886. ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
  887. }
  888. skb_push(skb, sizeof(struct ipv6hdr));
  889. skb_reset_network_header(skb);
  890. ipv6h = ipv6_hdr(skb);
  891. ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel);
  892. ipv6h->hop_limit = t->parms.hop_limit;
  893. ipv6h->nexthdr = proto;
  894. ipv6h->saddr = fl6->saddr;
  895. ipv6h->daddr = fl6->daddr;
  896. ip6tunnel_xmit(skb, dev);
  897. if (ndst)
  898. ip6_tnl_dst_store(t, ndst);
  899. return 0;
  900. tx_err_link_failure:
  901. stats->tx_carrier_errors++;
  902. dst_link_failure(skb);
  903. tx_err_dst_release:
  904. dst_release(ndst);
  905. return err;
  906. }
  907. static inline int
  908. ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  909. {
  910. struct ip6_tnl *t = netdev_priv(dev);
  911. const struct iphdr *iph = ip_hdr(skb);
  912. int encap_limit = -1;
  913. struct flowi6 fl6;
  914. __u8 dsfield;
  915. __u32 mtu;
  916. int err;
  917. if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
  918. !ip6_tnl_xmit_ctl(t))
  919. return -1;
  920. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  921. encap_limit = t->parms.encap_limit;
  922. memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
  923. fl6.flowi6_proto = IPPROTO_IPIP;
  924. dsfield = ipv4_get_dsfield(iph);
  925. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  926. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  927. & IPV6_TCLASS_MASK;
  928. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  929. fl6.flowi6_mark = skb->mark;
  930. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  931. if (err != 0) {
  932. /* XXX: send ICMP error even if DF is not set. */
  933. if (err == -EMSGSIZE)
  934. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  935. htonl(mtu));
  936. return -1;
  937. }
  938. return 0;
  939. }
  940. static inline int
  941. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  942. {
  943. struct ip6_tnl *t = netdev_priv(dev);
  944. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  945. int encap_limit = -1;
  946. __u16 offset;
  947. struct flowi6 fl6;
  948. __u8 dsfield;
  949. __u32 mtu;
  950. int err;
  951. if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
  952. !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
  953. return -1;
  954. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  955. if (offset > 0) {
  956. struct ipv6_tlv_tnl_enc_lim *tel;
  957. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  958. if (tel->encap_limit == 0) {
  959. icmpv6_send(skb, ICMPV6_PARAMPROB,
  960. ICMPV6_HDR_FIELD, offset + 2);
  961. return -1;
  962. }
  963. encap_limit = tel->encap_limit - 1;
  964. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  965. encap_limit = t->parms.encap_limit;
  966. memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
  967. fl6.flowi6_proto = IPPROTO_IPV6;
  968. dsfield = ipv6_get_dsfield(ipv6h);
  969. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  970. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  971. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  972. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
  973. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  974. fl6.flowi6_mark = skb->mark;
  975. err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
  976. if (err != 0) {
  977. if (err == -EMSGSIZE)
  978. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  979. return -1;
  980. }
  981. return 0;
  982. }
  983. static netdev_tx_t
  984. ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  985. {
  986. struct ip6_tnl *t = netdev_priv(dev);
  987. struct net_device_stats *stats = &t->dev->stats;
  988. int ret;
  989. switch (skb->protocol) {
  990. case htons(ETH_P_IP):
  991. ret = ip4ip6_tnl_xmit(skb, dev);
  992. break;
  993. case htons(ETH_P_IPV6):
  994. ret = ip6ip6_tnl_xmit(skb, dev);
  995. break;
  996. default:
  997. goto tx_err;
  998. }
  999. if (ret < 0)
  1000. goto tx_err;
  1001. return NETDEV_TX_OK;
  1002. tx_err:
  1003. stats->tx_errors++;
  1004. stats->tx_dropped++;
  1005. kfree_skb(skb);
  1006. return NETDEV_TX_OK;
  1007. }
  1008. static void ip6_tnl_link_config(struct ip6_tnl *t)
  1009. {
  1010. struct net_device *dev = t->dev;
  1011. struct __ip6_tnl_parm *p = &t->parms;
  1012. struct flowi6 *fl6 = &t->fl.u.ip6;
  1013. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  1014. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  1015. /* Set up flowi template */
  1016. fl6->saddr = p->laddr;
  1017. fl6->daddr = p->raddr;
  1018. fl6->flowi6_oif = p->link;
  1019. fl6->flowlabel = 0;
  1020. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  1021. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  1022. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  1023. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  1024. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  1025. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  1026. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  1027. dev->flags |= IFF_POINTOPOINT;
  1028. else
  1029. dev->flags &= ~IFF_POINTOPOINT;
  1030. dev->iflink = p->link;
  1031. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  1032. int strict = (ipv6_addr_type(&p->raddr) &
  1033. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1034. struct rt6_info *rt = rt6_lookup(t->net,
  1035. &p->raddr, &p->laddr,
  1036. p->link, strict);
  1037. if (rt == NULL)
  1038. return;
  1039. if (rt->dst.dev) {
  1040. dev->hard_header_len = rt->dst.dev->hard_header_len +
  1041. sizeof (struct ipv6hdr);
  1042. dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr);
  1043. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1044. dev->mtu-=8;
  1045. if (dev->mtu < IPV6_MIN_MTU)
  1046. dev->mtu = IPV6_MIN_MTU;
  1047. }
  1048. ip6_rt_put(rt);
  1049. }
  1050. }
  1051. /**
  1052. * ip6_tnl_change - update the tunnel parameters
  1053. * @t: tunnel to be changed
  1054. * @p: tunnel configuration parameters
  1055. *
  1056. * Description:
  1057. * ip6_tnl_change() updates the tunnel parameters
  1058. **/
  1059. static int
  1060. ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  1061. {
  1062. t->parms.laddr = p->laddr;
  1063. t->parms.raddr = p->raddr;
  1064. t->parms.flags = p->flags;
  1065. t->parms.hop_limit = p->hop_limit;
  1066. t->parms.encap_limit = p->encap_limit;
  1067. t->parms.flowinfo = p->flowinfo;
  1068. t->parms.link = p->link;
  1069. t->parms.proto = p->proto;
  1070. ip6_tnl_dst_reset(t);
  1071. ip6_tnl_link_config(t);
  1072. return 0;
  1073. }
  1074. static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1075. {
  1076. struct net *net = t->net;
  1077. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1078. int err;
  1079. ip6_tnl_unlink(ip6n, t);
  1080. synchronize_net();
  1081. err = ip6_tnl_change(t, p);
  1082. ip6_tnl_link(ip6n, t);
  1083. netdev_state_change(t->dev);
  1084. return err;
  1085. }
  1086. static void
  1087. ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
  1088. {
  1089. p->laddr = u->laddr;
  1090. p->raddr = u->raddr;
  1091. p->flags = u->flags;
  1092. p->hop_limit = u->hop_limit;
  1093. p->encap_limit = u->encap_limit;
  1094. p->flowinfo = u->flowinfo;
  1095. p->link = u->link;
  1096. p->proto = u->proto;
  1097. memcpy(p->name, u->name, sizeof(u->name));
  1098. }
  1099. static void
  1100. ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
  1101. {
  1102. u->laddr = p->laddr;
  1103. u->raddr = p->raddr;
  1104. u->flags = p->flags;
  1105. u->hop_limit = p->hop_limit;
  1106. u->encap_limit = p->encap_limit;
  1107. u->flowinfo = p->flowinfo;
  1108. u->link = p->link;
  1109. u->proto = p->proto;
  1110. memcpy(u->name, p->name, sizeof(u->name));
  1111. }
  1112. /**
  1113. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1114. * @dev: virtual device associated with tunnel
  1115. * @ifr: parameters passed from userspace
  1116. * @cmd: command to be performed
  1117. *
  1118. * Description:
  1119. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1120. * from userspace.
  1121. *
  1122. * The possible commands are the following:
  1123. * %SIOCGETTUNNEL: get tunnel parameters for device
  1124. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1125. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1126. * %SIOCDELTUNNEL: delete tunnel
  1127. *
  1128. * The fallback device "ip6tnl0", created during module
  1129. * initialization, can be used for creating other tunnel devices.
  1130. *
  1131. * Return:
  1132. * 0 on success,
  1133. * %-EFAULT if unable to copy data to or from userspace,
  1134. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1135. * %-EINVAL if passed tunnel parameters are invalid,
  1136. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1137. * %-ENODEV if attempting to change or delete a nonexisting device
  1138. **/
  1139. static int
  1140. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1141. {
  1142. int err = 0;
  1143. struct ip6_tnl_parm p;
  1144. struct __ip6_tnl_parm p1;
  1145. struct ip6_tnl *t = NULL;
  1146. struct net *net = dev_net(dev);
  1147. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1148. switch (cmd) {
  1149. case SIOCGETTUNNEL:
  1150. if (dev == ip6n->fb_tnl_dev) {
  1151. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
  1152. err = -EFAULT;
  1153. break;
  1154. }
  1155. ip6_tnl_parm_from_user(&p1, &p);
  1156. t = ip6_tnl_locate(net, &p1, 0);
  1157. } else {
  1158. memset(&p, 0, sizeof(p));
  1159. }
  1160. if (t == NULL)
  1161. t = netdev_priv(dev);
  1162. ip6_tnl_parm_to_user(&p, &t->parms);
  1163. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
  1164. err = -EFAULT;
  1165. }
  1166. break;
  1167. case SIOCADDTUNNEL:
  1168. case SIOCCHGTUNNEL:
  1169. err = -EPERM;
  1170. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1171. break;
  1172. err = -EFAULT;
  1173. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1174. break;
  1175. err = -EINVAL;
  1176. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1177. p.proto != 0)
  1178. break;
  1179. ip6_tnl_parm_from_user(&p1, &p);
  1180. t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1181. if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
  1182. if (t != NULL) {
  1183. if (t->dev != dev) {
  1184. err = -EEXIST;
  1185. break;
  1186. }
  1187. } else
  1188. t = netdev_priv(dev);
  1189. err = ip6_tnl_update(t, &p1);
  1190. }
  1191. if (t) {
  1192. err = 0;
  1193. ip6_tnl_parm_to_user(&p, &t->parms);
  1194. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1195. err = -EFAULT;
  1196. } else
  1197. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  1198. break;
  1199. case SIOCDELTUNNEL:
  1200. err = -EPERM;
  1201. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1202. break;
  1203. if (dev == ip6n->fb_tnl_dev) {
  1204. err = -EFAULT;
  1205. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
  1206. break;
  1207. err = -ENOENT;
  1208. ip6_tnl_parm_from_user(&p1, &p);
  1209. t = ip6_tnl_locate(net, &p1, 0);
  1210. if (t == NULL)
  1211. break;
  1212. err = -EPERM;
  1213. if (t->dev == ip6n->fb_tnl_dev)
  1214. break;
  1215. dev = t->dev;
  1216. }
  1217. err = 0;
  1218. unregister_netdevice(dev);
  1219. break;
  1220. default:
  1221. err = -EINVAL;
  1222. }
  1223. return err;
  1224. }
  1225. /**
  1226. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1227. * @dev: virtual device associated with tunnel
  1228. * @new_mtu: the new mtu
  1229. *
  1230. * Return:
  1231. * 0 on success,
  1232. * %-EINVAL if mtu too small
  1233. **/
  1234. static int
  1235. ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1236. {
  1237. if (new_mtu < IPV6_MIN_MTU) {
  1238. return -EINVAL;
  1239. }
  1240. dev->mtu = new_mtu;
  1241. return 0;
  1242. }
  1243. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1244. .ndo_uninit = ip6_tnl_dev_uninit,
  1245. .ndo_start_xmit = ip6_tnl_xmit,
  1246. .ndo_do_ioctl = ip6_tnl_ioctl,
  1247. .ndo_change_mtu = ip6_tnl_change_mtu,
  1248. .ndo_get_stats = ip6_get_stats,
  1249. };
  1250. /**
  1251. * ip6_tnl_dev_setup - setup virtual tunnel device
  1252. * @dev: virtual device associated with tunnel
  1253. *
  1254. * Description:
  1255. * Initialize function pointers and device parameters
  1256. **/
  1257. static void ip6_tnl_dev_setup(struct net_device *dev)
  1258. {
  1259. struct ip6_tnl *t;
  1260. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1261. dev->destructor = ip6_dev_free;
  1262. dev->type = ARPHRD_TUNNEL6;
  1263. dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
  1264. dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
  1265. t = netdev_priv(dev);
  1266. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1267. dev->mtu-=8;
  1268. dev->flags |= IFF_NOARP;
  1269. dev->addr_len = sizeof(struct in6_addr);
  1270. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1271. }
  1272. /**
  1273. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1274. * @dev: virtual device associated with tunnel
  1275. **/
  1276. static inline int
  1277. ip6_tnl_dev_init_gen(struct net_device *dev)
  1278. {
  1279. struct ip6_tnl *t = netdev_priv(dev);
  1280. t->dev = dev;
  1281. t->net = dev_net(dev);
  1282. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1283. if (!dev->tstats)
  1284. return -ENOMEM;
  1285. return 0;
  1286. }
  1287. /**
  1288. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1289. * @dev: virtual device associated with tunnel
  1290. **/
  1291. static int ip6_tnl_dev_init(struct net_device *dev)
  1292. {
  1293. struct ip6_tnl *t = netdev_priv(dev);
  1294. int err = ip6_tnl_dev_init_gen(dev);
  1295. if (err)
  1296. return err;
  1297. ip6_tnl_link_config(t);
  1298. return 0;
  1299. }
  1300. /**
  1301. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1302. * @dev: fallback device
  1303. *
  1304. * Return: 0
  1305. **/
  1306. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1307. {
  1308. struct ip6_tnl *t = netdev_priv(dev);
  1309. struct net *net = dev_net(dev);
  1310. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1311. int err = ip6_tnl_dev_init_gen(dev);
  1312. if (err)
  1313. return err;
  1314. t->parms.proto = IPPROTO_IPV6;
  1315. dev_hold(dev);
  1316. ip6_tnl_link_config(t);
  1317. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1318. return 0;
  1319. }
  1320. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
  1321. {
  1322. u8 proto;
  1323. if (!data)
  1324. return 0;
  1325. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1326. if (proto != IPPROTO_IPV6 &&
  1327. proto != IPPROTO_IPIP &&
  1328. proto != 0)
  1329. return -EINVAL;
  1330. return 0;
  1331. }
  1332. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1333. struct __ip6_tnl_parm *parms)
  1334. {
  1335. memset(parms, 0, sizeof(*parms));
  1336. if (!data)
  1337. return;
  1338. if (data[IFLA_IPTUN_LINK])
  1339. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1340. if (data[IFLA_IPTUN_LOCAL])
  1341. nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL],
  1342. sizeof(struct in6_addr));
  1343. if (data[IFLA_IPTUN_REMOTE])
  1344. nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE],
  1345. sizeof(struct in6_addr));
  1346. if (data[IFLA_IPTUN_TTL])
  1347. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1348. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1349. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1350. if (data[IFLA_IPTUN_FLOWINFO])
  1351. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1352. if (data[IFLA_IPTUN_FLAGS])
  1353. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1354. if (data[IFLA_IPTUN_PROTO])
  1355. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1356. }
  1357. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1358. struct nlattr *tb[], struct nlattr *data[])
  1359. {
  1360. struct net *net = dev_net(dev);
  1361. struct ip6_tnl *nt;
  1362. nt = netdev_priv(dev);
  1363. ip6_tnl_netlink_parms(data, &nt->parms);
  1364. if (ip6_tnl_locate(net, &nt->parms, 0))
  1365. return -EEXIST;
  1366. return ip6_tnl_create2(dev);
  1367. }
  1368. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1369. struct nlattr *data[])
  1370. {
  1371. struct ip6_tnl *t = netdev_priv(dev);
  1372. struct __ip6_tnl_parm p;
  1373. struct net *net = t->net;
  1374. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1375. if (dev == ip6n->fb_tnl_dev)
  1376. return -EINVAL;
  1377. ip6_tnl_netlink_parms(data, &p);
  1378. t = ip6_tnl_locate(net, &p, 0);
  1379. if (t) {
  1380. if (t->dev != dev)
  1381. return -EEXIST;
  1382. } else
  1383. t = netdev_priv(dev);
  1384. return ip6_tnl_update(t, &p);
  1385. }
  1386. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1387. {
  1388. return
  1389. /* IFLA_IPTUN_LINK */
  1390. nla_total_size(4) +
  1391. /* IFLA_IPTUN_LOCAL */
  1392. nla_total_size(sizeof(struct in6_addr)) +
  1393. /* IFLA_IPTUN_REMOTE */
  1394. nla_total_size(sizeof(struct in6_addr)) +
  1395. /* IFLA_IPTUN_TTL */
  1396. nla_total_size(1) +
  1397. /* IFLA_IPTUN_ENCAP_LIMIT */
  1398. nla_total_size(1) +
  1399. /* IFLA_IPTUN_FLOWINFO */
  1400. nla_total_size(4) +
  1401. /* IFLA_IPTUN_FLAGS */
  1402. nla_total_size(4) +
  1403. /* IFLA_IPTUN_PROTO */
  1404. nla_total_size(1) +
  1405. 0;
  1406. }
  1407. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1408. {
  1409. struct ip6_tnl *tunnel = netdev_priv(dev);
  1410. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1411. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1412. nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
  1413. &parm->raddr) ||
  1414. nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
  1415. &parm->laddr) ||
  1416. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1417. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1418. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1419. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1420. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
  1421. goto nla_put_failure;
  1422. return 0;
  1423. nla_put_failure:
  1424. return -EMSGSIZE;
  1425. }
  1426. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1427. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1428. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1429. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1430. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1431. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1432. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1433. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1434. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1435. };
  1436. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1437. .kind = "ip6tnl",
  1438. .maxtype = IFLA_IPTUN_MAX,
  1439. .policy = ip6_tnl_policy,
  1440. .priv_size = sizeof(struct ip6_tnl),
  1441. .setup = ip6_tnl_dev_setup,
  1442. .validate = ip6_tnl_validate,
  1443. .newlink = ip6_tnl_newlink,
  1444. .changelink = ip6_tnl_changelink,
  1445. .get_size = ip6_tnl_get_size,
  1446. .fill_info = ip6_tnl_fill_info,
  1447. };
  1448. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1449. .handler = ip4ip6_rcv,
  1450. .err_handler = ip4ip6_err,
  1451. .priority = 1,
  1452. };
  1453. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1454. .handler = ip6ip6_rcv,
  1455. .err_handler = ip6ip6_err,
  1456. .priority = 1,
  1457. };
  1458. static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
  1459. {
  1460. struct net *net = dev_net(ip6n->fb_tnl_dev);
  1461. struct net_device *dev, *aux;
  1462. int h;
  1463. struct ip6_tnl *t;
  1464. LIST_HEAD(list);
  1465. for_each_netdev_safe(net, dev, aux)
  1466. if (dev->rtnl_link_ops == &ip6_link_ops)
  1467. unregister_netdevice_queue(dev, &list);
  1468. for (h = 0; h < HASH_SIZE; h++) {
  1469. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1470. while (t != NULL) {
  1471. /* If dev is in the same netns, it has already
  1472. * been added to the list by the previous loop.
  1473. */
  1474. if (!net_eq(dev_net(t->dev), net))
  1475. unregister_netdevice_queue(t->dev, &list);
  1476. t = rtnl_dereference(t->next);
  1477. }
  1478. }
  1479. t = rtnl_dereference(ip6n->tnls_wc[0]);
  1480. unregister_netdevice_queue(t->dev, &list);
  1481. unregister_netdevice_many(&list);
  1482. }
  1483. static int __net_init ip6_tnl_init_net(struct net *net)
  1484. {
  1485. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1486. struct ip6_tnl *t = NULL;
  1487. int err;
  1488. ip6n->tnls[0] = ip6n->tnls_wc;
  1489. ip6n->tnls[1] = ip6n->tnls_r_l;
  1490. err = -ENOMEM;
  1491. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1492. ip6_tnl_dev_setup);
  1493. if (!ip6n->fb_tnl_dev)
  1494. goto err_alloc_dev;
  1495. dev_net_set(ip6n->fb_tnl_dev, net);
  1496. /* FB netdevice is special: we have one, and only one per netns.
  1497. * Allowing to move it to another netns is clearly unsafe.
  1498. */
  1499. ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
  1500. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1501. if (err < 0)
  1502. goto err_register;
  1503. err = register_netdev(ip6n->fb_tnl_dev);
  1504. if (err < 0)
  1505. goto err_register;
  1506. t = netdev_priv(ip6n->fb_tnl_dev);
  1507. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1508. return 0;
  1509. err_register:
  1510. ip6_dev_free(ip6n->fb_tnl_dev);
  1511. err_alloc_dev:
  1512. return err;
  1513. }
  1514. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1515. {
  1516. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1517. rtnl_lock();
  1518. ip6_tnl_destroy_tunnels(ip6n);
  1519. rtnl_unlock();
  1520. }
  1521. static struct pernet_operations ip6_tnl_net_ops = {
  1522. .init = ip6_tnl_init_net,
  1523. .exit = ip6_tnl_exit_net,
  1524. .id = &ip6_tnl_net_id,
  1525. .size = sizeof(struct ip6_tnl_net),
  1526. };
  1527. /**
  1528. * ip6_tunnel_init - register protocol and reserve needed resources
  1529. *
  1530. * Return: 0 on success
  1531. **/
  1532. static int __init ip6_tunnel_init(void)
  1533. {
  1534. int err;
  1535. err = register_pernet_device(&ip6_tnl_net_ops);
  1536. if (err < 0)
  1537. goto out_pernet;
  1538. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1539. if (err < 0) {
  1540. pr_err("%s: can't register ip4ip6\n", __func__);
  1541. goto out_ip4ip6;
  1542. }
  1543. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1544. if (err < 0) {
  1545. pr_err("%s: can't register ip6ip6\n", __func__);
  1546. goto out_ip6ip6;
  1547. }
  1548. err = rtnl_link_register(&ip6_link_ops);
  1549. if (err < 0)
  1550. goto rtnl_link_failed;
  1551. return 0;
  1552. rtnl_link_failed:
  1553. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1554. out_ip6ip6:
  1555. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1556. out_ip4ip6:
  1557. unregister_pernet_device(&ip6_tnl_net_ops);
  1558. out_pernet:
  1559. return err;
  1560. }
  1561. /**
  1562. * ip6_tunnel_cleanup - free resources and unregister protocol
  1563. **/
  1564. static void __exit ip6_tunnel_cleanup(void)
  1565. {
  1566. rtnl_link_unregister(&ip6_link_ops);
  1567. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1568. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1569. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1570. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1571. unregister_pernet_device(&ip6_tnl_net_ops);
  1572. }
  1573. module_init(ip6_tunnel_init);
  1574. module_exit(ip6_tunnel_cleanup);