ip6_tunnel.c 45 KB

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