ip6_tunnel.c 44 KB

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