ip6_tunnel.c 54 KB

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