ip6_tunnel.c 43 KB

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