ip6_tunnel.c 51 KB

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