ip6_tunnel.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  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. fl6.flowi6_proto = IPPROTO_IPIP;
  1004. dsfield = ipv4_get_dsfield(iph);
  1005. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1006. fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
  1007. & IPV6_TCLASS_MASK;
  1008. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1009. fl6.flowi6_mark = skb->mark;
  1010. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1011. return -1;
  1012. skb_set_inner_ipproto(skb, IPPROTO_IPIP);
  1013. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1014. IPPROTO_IPIP);
  1015. if (err != 0) {
  1016. /* XXX: send ICMP error even if DF is not set. */
  1017. if (err == -EMSGSIZE)
  1018. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
  1019. htonl(mtu));
  1020. return -1;
  1021. }
  1022. return 0;
  1023. }
  1024. static inline int
  1025. ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
  1026. {
  1027. struct ip6_tnl *t = netdev_priv(dev);
  1028. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  1029. int encap_limit = -1;
  1030. __u16 offset;
  1031. struct flowi6 fl6;
  1032. __u8 dsfield;
  1033. __u32 mtu;
  1034. u8 tproto;
  1035. int err;
  1036. tproto = ACCESS_ONCE(t->parms.proto);
  1037. if ((tproto != IPPROTO_IPV6 && tproto != 0) ||
  1038. ip6_tnl_addr_conflict(t, ipv6h))
  1039. return -1;
  1040. offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
  1041. if (offset > 0) {
  1042. struct ipv6_tlv_tnl_enc_lim *tel;
  1043. tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
  1044. if (tel->encap_limit == 0) {
  1045. icmpv6_send(skb, ICMPV6_PARAMPROB,
  1046. ICMPV6_HDR_FIELD, offset + 2);
  1047. return -1;
  1048. }
  1049. encap_limit = tel->encap_limit - 1;
  1050. } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1051. encap_limit = t->parms.encap_limit;
  1052. memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
  1053. fl6.flowi6_proto = IPPROTO_IPV6;
  1054. dsfield = ipv6_get_dsfield(ipv6h);
  1055. if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
  1056. fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
  1057. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
  1058. fl6.flowlabel |= ip6_flowlabel(ipv6h);
  1059. if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
  1060. fl6.flowi6_mark = skb->mark;
  1061. if (iptunnel_handle_offloads(skb, SKB_GSO_IPXIP6))
  1062. return -1;
  1063. skb_set_inner_ipproto(skb, IPPROTO_IPV6);
  1064. err = ip6_tnl_xmit(skb, dev, dsfield, &fl6, encap_limit, &mtu,
  1065. IPPROTO_IPV6);
  1066. if (err != 0) {
  1067. if (err == -EMSGSIZE)
  1068. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  1069. return -1;
  1070. }
  1071. return 0;
  1072. }
  1073. static netdev_tx_t
  1074. ip6_tnl_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1075. {
  1076. struct ip6_tnl *t = netdev_priv(dev);
  1077. struct net_device_stats *stats = &t->dev->stats;
  1078. int ret;
  1079. switch (skb->protocol) {
  1080. case htons(ETH_P_IP):
  1081. ret = ip4ip6_tnl_xmit(skb, dev);
  1082. break;
  1083. case htons(ETH_P_IPV6):
  1084. ret = ip6ip6_tnl_xmit(skb, dev);
  1085. break;
  1086. default:
  1087. goto tx_err;
  1088. }
  1089. if (ret < 0)
  1090. goto tx_err;
  1091. return NETDEV_TX_OK;
  1092. tx_err:
  1093. stats->tx_errors++;
  1094. stats->tx_dropped++;
  1095. kfree_skb(skb);
  1096. return NETDEV_TX_OK;
  1097. }
  1098. static void ip6_tnl_link_config(struct ip6_tnl *t)
  1099. {
  1100. struct net_device *dev = t->dev;
  1101. struct __ip6_tnl_parm *p = &t->parms;
  1102. struct flowi6 *fl6 = &t->fl.u.ip6;
  1103. int t_hlen;
  1104. memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
  1105. memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
  1106. /* Set up flowi template */
  1107. fl6->saddr = p->laddr;
  1108. fl6->daddr = p->raddr;
  1109. fl6->flowi6_oif = p->link;
  1110. fl6->flowlabel = 0;
  1111. if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
  1112. fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
  1113. if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
  1114. fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
  1115. p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
  1116. p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
  1117. if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
  1118. dev->flags |= IFF_POINTOPOINT;
  1119. else
  1120. dev->flags &= ~IFF_POINTOPOINT;
  1121. t->tun_hlen = 0;
  1122. t->hlen = t->encap_hlen + t->tun_hlen;
  1123. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1124. if (p->flags & IP6_TNL_F_CAP_XMIT) {
  1125. int strict = (ipv6_addr_type(&p->raddr) &
  1126. (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
  1127. struct rt6_info *rt = rt6_lookup(t->net,
  1128. &p->raddr, &p->laddr,
  1129. p->link, strict);
  1130. if (!rt)
  1131. return;
  1132. if (rt->dst.dev) {
  1133. dev->hard_header_len = rt->dst.dev->hard_header_len +
  1134. t_hlen;
  1135. dev->mtu = rt->dst.dev->mtu - t_hlen;
  1136. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1137. dev->mtu -= 8;
  1138. if (dev->mtu < IPV6_MIN_MTU)
  1139. dev->mtu = IPV6_MIN_MTU;
  1140. }
  1141. ip6_rt_put(rt);
  1142. }
  1143. }
  1144. /**
  1145. * ip6_tnl_change - update the tunnel parameters
  1146. * @t: tunnel to be changed
  1147. * @p: tunnel configuration parameters
  1148. *
  1149. * Description:
  1150. * ip6_tnl_change() updates the tunnel parameters
  1151. **/
  1152. static int
  1153. ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
  1154. {
  1155. t->parms.laddr = p->laddr;
  1156. t->parms.raddr = p->raddr;
  1157. t->parms.flags = p->flags;
  1158. t->parms.hop_limit = p->hop_limit;
  1159. t->parms.encap_limit = p->encap_limit;
  1160. t->parms.flowinfo = p->flowinfo;
  1161. t->parms.link = p->link;
  1162. t->parms.proto = p->proto;
  1163. dst_cache_reset(&t->dst_cache);
  1164. ip6_tnl_link_config(t);
  1165. return 0;
  1166. }
  1167. static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1168. {
  1169. struct net *net = t->net;
  1170. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1171. int err;
  1172. ip6_tnl_unlink(ip6n, t);
  1173. synchronize_net();
  1174. err = ip6_tnl_change(t, p);
  1175. ip6_tnl_link(ip6n, t);
  1176. netdev_state_change(t->dev);
  1177. return err;
  1178. }
  1179. static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
  1180. {
  1181. /* for default tnl0 device allow to change only the proto */
  1182. t->parms.proto = p->proto;
  1183. netdev_state_change(t->dev);
  1184. return 0;
  1185. }
  1186. static void
  1187. ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
  1188. {
  1189. p->laddr = u->laddr;
  1190. p->raddr = u->raddr;
  1191. p->flags = u->flags;
  1192. p->hop_limit = u->hop_limit;
  1193. p->encap_limit = u->encap_limit;
  1194. p->flowinfo = u->flowinfo;
  1195. p->link = u->link;
  1196. p->proto = u->proto;
  1197. memcpy(p->name, u->name, sizeof(u->name));
  1198. }
  1199. static void
  1200. ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
  1201. {
  1202. u->laddr = p->laddr;
  1203. u->raddr = p->raddr;
  1204. u->flags = p->flags;
  1205. u->hop_limit = p->hop_limit;
  1206. u->encap_limit = p->encap_limit;
  1207. u->flowinfo = p->flowinfo;
  1208. u->link = p->link;
  1209. u->proto = p->proto;
  1210. memcpy(u->name, p->name, sizeof(u->name));
  1211. }
  1212. /**
  1213. * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
  1214. * @dev: virtual device associated with tunnel
  1215. * @ifr: parameters passed from userspace
  1216. * @cmd: command to be performed
  1217. *
  1218. * Description:
  1219. * ip6_tnl_ioctl() is used for managing IPv6 tunnels
  1220. * from userspace.
  1221. *
  1222. * The possible commands are the following:
  1223. * %SIOCGETTUNNEL: get tunnel parameters for device
  1224. * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
  1225. * %SIOCCHGTUNNEL: change tunnel parameters to those given
  1226. * %SIOCDELTUNNEL: delete tunnel
  1227. *
  1228. * The fallback device "ip6tnl0", created during module
  1229. * initialization, can be used for creating other tunnel devices.
  1230. *
  1231. * Return:
  1232. * 0 on success,
  1233. * %-EFAULT if unable to copy data to or from userspace,
  1234. * %-EPERM if current process hasn't %CAP_NET_ADMIN set
  1235. * %-EINVAL if passed tunnel parameters are invalid,
  1236. * %-EEXIST if changing a tunnel's parameters would cause a conflict
  1237. * %-ENODEV if attempting to change or delete a nonexisting device
  1238. **/
  1239. static int
  1240. ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1241. {
  1242. int err = 0;
  1243. struct ip6_tnl_parm p;
  1244. struct __ip6_tnl_parm p1;
  1245. struct ip6_tnl *t = netdev_priv(dev);
  1246. struct net *net = t->net;
  1247. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1248. memset(&p1, 0, sizeof(p1));
  1249. switch (cmd) {
  1250. case SIOCGETTUNNEL:
  1251. if (dev == ip6n->fb_tnl_dev) {
  1252. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  1253. err = -EFAULT;
  1254. break;
  1255. }
  1256. ip6_tnl_parm_from_user(&p1, &p);
  1257. t = ip6_tnl_locate(net, &p1, 0);
  1258. if (IS_ERR(t))
  1259. t = netdev_priv(dev);
  1260. } else {
  1261. memset(&p, 0, sizeof(p));
  1262. }
  1263. ip6_tnl_parm_to_user(&p, &t->parms);
  1264. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) {
  1265. err = -EFAULT;
  1266. }
  1267. break;
  1268. case SIOCADDTUNNEL:
  1269. case SIOCCHGTUNNEL:
  1270. err = -EPERM;
  1271. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1272. break;
  1273. err = -EFAULT;
  1274. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1275. break;
  1276. err = -EINVAL;
  1277. if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
  1278. p.proto != 0)
  1279. break;
  1280. ip6_tnl_parm_from_user(&p1, &p);
  1281. t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
  1282. if (cmd == SIOCCHGTUNNEL) {
  1283. if (!IS_ERR(t)) {
  1284. if (t->dev != dev) {
  1285. err = -EEXIST;
  1286. break;
  1287. }
  1288. } else
  1289. t = netdev_priv(dev);
  1290. if (dev == ip6n->fb_tnl_dev)
  1291. err = ip6_tnl0_update(t, &p1);
  1292. else
  1293. err = ip6_tnl_update(t, &p1);
  1294. }
  1295. if (!IS_ERR(t)) {
  1296. err = 0;
  1297. ip6_tnl_parm_to_user(&p, &t->parms);
  1298. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  1299. err = -EFAULT;
  1300. } else {
  1301. err = PTR_ERR(t);
  1302. }
  1303. break;
  1304. case SIOCDELTUNNEL:
  1305. err = -EPERM;
  1306. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1307. break;
  1308. if (dev == ip6n->fb_tnl_dev) {
  1309. err = -EFAULT;
  1310. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  1311. break;
  1312. err = -ENOENT;
  1313. ip6_tnl_parm_from_user(&p1, &p);
  1314. t = ip6_tnl_locate(net, &p1, 0);
  1315. if (IS_ERR(t))
  1316. break;
  1317. err = -EPERM;
  1318. if (t->dev == ip6n->fb_tnl_dev)
  1319. break;
  1320. dev = t->dev;
  1321. }
  1322. err = 0;
  1323. unregister_netdevice(dev);
  1324. break;
  1325. default:
  1326. err = -EINVAL;
  1327. }
  1328. return err;
  1329. }
  1330. /**
  1331. * ip6_tnl_change_mtu - change mtu manually for tunnel device
  1332. * @dev: virtual device associated with tunnel
  1333. * @new_mtu: the new mtu
  1334. *
  1335. * Return:
  1336. * 0 on success,
  1337. * %-EINVAL if mtu too small
  1338. **/
  1339. int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
  1340. {
  1341. struct ip6_tnl *tnl = netdev_priv(dev);
  1342. if (tnl->parms.proto == IPPROTO_IPIP) {
  1343. if (new_mtu < 68)
  1344. return -EINVAL;
  1345. } else {
  1346. if (new_mtu < IPV6_MIN_MTU)
  1347. return -EINVAL;
  1348. }
  1349. if (new_mtu > 0xFFF8 - dev->hard_header_len)
  1350. return -EINVAL;
  1351. dev->mtu = new_mtu;
  1352. return 0;
  1353. }
  1354. EXPORT_SYMBOL(ip6_tnl_change_mtu);
  1355. int ip6_tnl_get_iflink(const struct net_device *dev)
  1356. {
  1357. struct ip6_tnl *t = netdev_priv(dev);
  1358. return t->parms.link;
  1359. }
  1360. EXPORT_SYMBOL(ip6_tnl_get_iflink);
  1361. int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
  1362. unsigned int num)
  1363. {
  1364. if (num >= MAX_IPTUN_ENCAP_OPS)
  1365. return -ERANGE;
  1366. return !cmpxchg((const struct ip6_tnl_encap_ops **)
  1367. &ip6tun_encaps[num],
  1368. NULL, ops) ? 0 : -1;
  1369. }
  1370. EXPORT_SYMBOL(ip6_tnl_encap_add_ops);
  1371. int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
  1372. unsigned int num)
  1373. {
  1374. int ret;
  1375. if (num >= MAX_IPTUN_ENCAP_OPS)
  1376. return -ERANGE;
  1377. ret = (cmpxchg((const struct ip6_tnl_encap_ops **)
  1378. &ip6tun_encaps[num],
  1379. ops, NULL) == ops) ? 0 : -1;
  1380. synchronize_net();
  1381. return ret;
  1382. }
  1383. EXPORT_SYMBOL(ip6_tnl_encap_del_ops);
  1384. int ip6_tnl_encap_setup(struct ip6_tnl *t,
  1385. struct ip_tunnel_encap *ipencap)
  1386. {
  1387. int hlen;
  1388. memset(&t->encap, 0, sizeof(t->encap));
  1389. hlen = ip6_encap_hlen(ipencap);
  1390. if (hlen < 0)
  1391. return hlen;
  1392. t->encap.type = ipencap->type;
  1393. t->encap.sport = ipencap->sport;
  1394. t->encap.dport = ipencap->dport;
  1395. t->encap.flags = ipencap->flags;
  1396. t->encap_hlen = hlen;
  1397. t->hlen = t->encap_hlen + t->tun_hlen;
  1398. return 0;
  1399. }
  1400. EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup);
  1401. static const struct net_device_ops ip6_tnl_netdev_ops = {
  1402. .ndo_init = ip6_tnl_dev_init,
  1403. .ndo_uninit = ip6_tnl_dev_uninit,
  1404. .ndo_start_xmit = ip6_tnl_start_xmit,
  1405. .ndo_do_ioctl = ip6_tnl_ioctl,
  1406. .ndo_change_mtu = ip6_tnl_change_mtu,
  1407. .ndo_get_stats = ip6_get_stats,
  1408. .ndo_get_iflink = ip6_tnl_get_iflink,
  1409. };
  1410. #define IPXIPX_FEATURES (NETIF_F_SG | \
  1411. NETIF_F_FRAGLIST | \
  1412. NETIF_F_HIGHDMA | \
  1413. NETIF_F_GSO_SOFTWARE | \
  1414. NETIF_F_HW_CSUM)
  1415. /**
  1416. * ip6_tnl_dev_setup - setup virtual tunnel device
  1417. * @dev: virtual device associated with tunnel
  1418. *
  1419. * Description:
  1420. * Initialize function pointers and device parameters
  1421. **/
  1422. static void ip6_tnl_dev_setup(struct net_device *dev)
  1423. {
  1424. dev->netdev_ops = &ip6_tnl_netdev_ops;
  1425. dev->destructor = ip6_dev_free;
  1426. dev->type = ARPHRD_TUNNEL6;
  1427. dev->flags |= IFF_NOARP;
  1428. dev->addr_len = sizeof(struct in6_addr);
  1429. dev->features |= NETIF_F_LLTX;
  1430. netif_keep_dst(dev);
  1431. dev->features |= IPXIPX_FEATURES;
  1432. dev->hw_features |= IPXIPX_FEATURES;
  1433. /* This perm addr will be used as interface identifier by IPv6 */
  1434. dev->addr_assign_type = NET_ADDR_RANDOM;
  1435. eth_random_addr(dev->perm_addr);
  1436. }
  1437. /**
  1438. * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
  1439. * @dev: virtual device associated with tunnel
  1440. **/
  1441. static inline int
  1442. ip6_tnl_dev_init_gen(struct net_device *dev)
  1443. {
  1444. struct ip6_tnl *t = netdev_priv(dev);
  1445. int ret;
  1446. int t_hlen;
  1447. t->dev = dev;
  1448. t->net = dev_net(dev);
  1449. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1450. if (!dev->tstats)
  1451. return -ENOMEM;
  1452. ret = dst_cache_init(&t->dst_cache, GFP_KERNEL);
  1453. if (ret)
  1454. goto free_stats;
  1455. ret = gro_cells_init(&t->gro_cells, dev);
  1456. if (ret)
  1457. goto destroy_dst;
  1458. t->tun_hlen = 0;
  1459. t->hlen = t->encap_hlen + t->tun_hlen;
  1460. t_hlen = t->hlen + sizeof(struct ipv6hdr);
  1461. dev->type = ARPHRD_TUNNEL6;
  1462. dev->hard_header_len = LL_MAX_HEADER + t_hlen;
  1463. dev->mtu = ETH_DATA_LEN - t_hlen;
  1464. if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
  1465. dev->mtu -= 8;
  1466. return 0;
  1467. destroy_dst:
  1468. dst_cache_destroy(&t->dst_cache);
  1469. free_stats:
  1470. free_percpu(dev->tstats);
  1471. dev->tstats = NULL;
  1472. return ret;
  1473. }
  1474. /**
  1475. * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
  1476. * @dev: virtual device associated with tunnel
  1477. **/
  1478. static int ip6_tnl_dev_init(struct net_device *dev)
  1479. {
  1480. struct ip6_tnl *t = netdev_priv(dev);
  1481. int err = ip6_tnl_dev_init_gen(dev);
  1482. if (err)
  1483. return err;
  1484. ip6_tnl_link_config(t);
  1485. return 0;
  1486. }
  1487. /**
  1488. * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
  1489. * @dev: fallback device
  1490. *
  1491. * Return: 0
  1492. **/
  1493. static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
  1494. {
  1495. struct ip6_tnl *t = netdev_priv(dev);
  1496. struct net *net = dev_net(dev);
  1497. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1498. t->parms.proto = IPPROTO_IPV6;
  1499. dev_hold(dev);
  1500. rcu_assign_pointer(ip6n->tnls_wc[0], t);
  1501. return 0;
  1502. }
  1503. static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
  1504. {
  1505. u8 proto;
  1506. if (!data || !data[IFLA_IPTUN_PROTO])
  1507. return 0;
  1508. proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1509. if (proto != IPPROTO_IPV6 &&
  1510. proto != IPPROTO_IPIP &&
  1511. proto != 0)
  1512. return -EINVAL;
  1513. return 0;
  1514. }
  1515. static void ip6_tnl_netlink_parms(struct nlattr *data[],
  1516. struct __ip6_tnl_parm *parms)
  1517. {
  1518. memset(parms, 0, sizeof(*parms));
  1519. if (!data)
  1520. return;
  1521. if (data[IFLA_IPTUN_LINK])
  1522. parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
  1523. if (data[IFLA_IPTUN_LOCAL])
  1524. parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
  1525. if (data[IFLA_IPTUN_REMOTE])
  1526. parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
  1527. if (data[IFLA_IPTUN_TTL])
  1528. parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
  1529. if (data[IFLA_IPTUN_ENCAP_LIMIT])
  1530. parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
  1531. if (data[IFLA_IPTUN_FLOWINFO])
  1532. parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
  1533. if (data[IFLA_IPTUN_FLAGS])
  1534. parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
  1535. if (data[IFLA_IPTUN_PROTO])
  1536. parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
  1537. }
  1538. static bool ip6_tnl_netlink_encap_parms(struct nlattr *data[],
  1539. struct ip_tunnel_encap *ipencap)
  1540. {
  1541. bool ret = false;
  1542. memset(ipencap, 0, sizeof(*ipencap));
  1543. if (!data)
  1544. return ret;
  1545. if (data[IFLA_IPTUN_ENCAP_TYPE]) {
  1546. ret = true;
  1547. ipencap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
  1548. }
  1549. if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
  1550. ret = true;
  1551. ipencap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
  1552. }
  1553. if (data[IFLA_IPTUN_ENCAP_SPORT]) {
  1554. ret = true;
  1555. ipencap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
  1556. }
  1557. if (data[IFLA_IPTUN_ENCAP_DPORT]) {
  1558. ret = true;
  1559. ipencap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
  1560. }
  1561. return ret;
  1562. }
  1563. static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
  1564. struct nlattr *tb[], struct nlattr *data[])
  1565. {
  1566. struct net *net = dev_net(dev);
  1567. struct ip6_tnl *nt, *t;
  1568. struct ip_tunnel_encap ipencap;
  1569. nt = netdev_priv(dev);
  1570. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1571. int err = ip6_tnl_encap_setup(nt, &ipencap);
  1572. if (err < 0)
  1573. return err;
  1574. }
  1575. ip6_tnl_netlink_parms(data, &nt->parms);
  1576. t = ip6_tnl_locate(net, &nt->parms, 0);
  1577. if (!IS_ERR(t))
  1578. return -EEXIST;
  1579. return ip6_tnl_create2(dev);
  1580. }
  1581. static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
  1582. struct nlattr *data[])
  1583. {
  1584. struct ip6_tnl *t = netdev_priv(dev);
  1585. struct __ip6_tnl_parm p;
  1586. struct net *net = t->net;
  1587. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1588. struct ip_tunnel_encap ipencap;
  1589. if (dev == ip6n->fb_tnl_dev)
  1590. return -EINVAL;
  1591. if (ip6_tnl_netlink_encap_parms(data, &ipencap)) {
  1592. int err = ip6_tnl_encap_setup(t, &ipencap);
  1593. if (err < 0)
  1594. return err;
  1595. }
  1596. ip6_tnl_netlink_parms(data, &p);
  1597. t = ip6_tnl_locate(net, &p, 0);
  1598. if (!IS_ERR(t)) {
  1599. if (t->dev != dev)
  1600. return -EEXIST;
  1601. } else
  1602. t = netdev_priv(dev);
  1603. return ip6_tnl_update(t, &p);
  1604. }
  1605. static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
  1606. {
  1607. struct net *net = dev_net(dev);
  1608. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1609. if (dev != ip6n->fb_tnl_dev)
  1610. unregister_netdevice_queue(dev, head);
  1611. }
  1612. static size_t ip6_tnl_get_size(const struct net_device *dev)
  1613. {
  1614. return
  1615. /* IFLA_IPTUN_LINK */
  1616. nla_total_size(4) +
  1617. /* IFLA_IPTUN_LOCAL */
  1618. nla_total_size(sizeof(struct in6_addr)) +
  1619. /* IFLA_IPTUN_REMOTE */
  1620. nla_total_size(sizeof(struct in6_addr)) +
  1621. /* IFLA_IPTUN_TTL */
  1622. nla_total_size(1) +
  1623. /* IFLA_IPTUN_ENCAP_LIMIT */
  1624. nla_total_size(1) +
  1625. /* IFLA_IPTUN_FLOWINFO */
  1626. nla_total_size(4) +
  1627. /* IFLA_IPTUN_FLAGS */
  1628. nla_total_size(4) +
  1629. /* IFLA_IPTUN_PROTO */
  1630. nla_total_size(1) +
  1631. /* IFLA_IPTUN_ENCAP_TYPE */
  1632. nla_total_size(2) +
  1633. /* IFLA_IPTUN_ENCAP_FLAGS */
  1634. nla_total_size(2) +
  1635. /* IFLA_IPTUN_ENCAP_SPORT */
  1636. nla_total_size(2) +
  1637. /* IFLA_IPTUN_ENCAP_DPORT */
  1638. nla_total_size(2) +
  1639. 0;
  1640. }
  1641. static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1642. {
  1643. struct ip6_tnl *tunnel = netdev_priv(dev);
  1644. struct __ip6_tnl_parm *parm = &tunnel->parms;
  1645. if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
  1646. nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
  1647. nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
  1648. nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
  1649. nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
  1650. nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
  1651. nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
  1652. nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
  1653. goto nla_put_failure;
  1654. if (nla_put_u16(skb, IFLA_IPTUN_ENCAP_TYPE,
  1655. tunnel->encap.type) ||
  1656. nla_put_be16(skb, IFLA_IPTUN_ENCAP_SPORT,
  1657. tunnel->encap.sport) ||
  1658. nla_put_be16(skb, IFLA_IPTUN_ENCAP_DPORT,
  1659. tunnel->encap.dport) ||
  1660. nla_put_u16(skb, IFLA_IPTUN_ENCAP_FLAGS,
  1661. tunnel->encap.flags))
  1662. goto nla_put_failure;
  1663. return 0;
  1664. nla_put_failure:
  1665. return -EMSGSIZE;
  1666. }
  1667. struct net *ip6_tnl_get_link_net(const struct net_device *dev)
  1668. {
  1669. struct ip6_tnl *tunnel = netdev_priv(dev);
  1670. return tunnel->net;
  1671. }
  1672. EXPORT_SYMBOL(ip6_tnl_get_link_net);
  1673. static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
  1674. [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
  1675. [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
  1676. [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
  1677. [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
  1678. [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
  1679. [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
  1680. [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
  1681. [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
  1682. [IFLA_IPTUN_ENCAP_TYPE] = { .type = NLA_U16 },
  1683. [IFLA_IPTUN_ENCAP_FLAGS] = { .type = NLA_U16 },
  1684. [IFLA_IPTUN_ENCAP_SPORT] = { .type = NLA_U16 },
  1685. [IFLA_IPTUN_ENCAP_DPORT] = { .type = NLA_U16 },
  1686. };
  1687. static struct rtnl_link_ops ip6_link_ops __read_mostly = {
  1688. .kind = "ip6tnl",
  1689. .maxtype = IFLA_IPTUN_MAX,
  1690. .policy = ip6_tnl_policy,
  1691. .priv_size = sizeof(struct ip6_tnl),
  1692. .setup = ip6_tnl_dev_setup,
  1693. .validate = ip6_tnl_validate,
  1694. .newlink = ip6_tnl_newlink,
  1695. .changelink = ip6_tnl_changelink,
  1696. .dellink = ip6_tnl_dellink,
  1697. .get_size = ip6_tnl_get_size,
  1698. .fill_info = ip6_tnl_fill_info,
  1699. .get_link_net = ip6_tnl_get_link_net,
  1700. };
  1701. static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
  1702. .handler = ip4ip6_rcv,
  1703. .err_handler = ip4ip6_err,
  1704. .priority = 1,
  1705. };
  1706. static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
  1707. .handler = ip6ip6_rcv,
  1708. .err_handler = ip6ip6_err,
  1709. .priority = 1,
  1710. };
  1711. static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
  1712. {
  1713. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1714. struct net_device *dev, *aux;
  1715. int h;
  1716. struct ip6_tnl *t;
  1717. LIST_HEAD(list);
  1718. for_each_netdev_safe(net, dev, aux)
  1719. if (dev->rtnl_link_ops == &ip6_link_ops)
  1720. unregister_netdevice_queue(dev, &list);
  1721. for (h = 0; h < HASH_SIZE; h++) {
  1722. t = rtnl_dereference(ip6n->tnls_r_l[h]);
  1723. while (t) {
  1724. /* If dev is in the same netns, it has already
  1725. * been added to the list by the previous loop.
  1726. */
  1727. if (!net_eq(dev_net(t->dev), net))
  1728. unregister_netdevice_queue(t->dev, &list);
  1729. t = rtnl_dereference(t->next);
  1730. }
  1731. }
  1732. unregister_netdevice_many(&list);
  1733. }
  1734. static int __net_init ip6_tnl_init_net(struct net *net)
  1735. {
  1736. struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
  1737. struct ip6_tnl *t = NULL;
  1738. int err;
  1739. ip6n->tnls[0] = ip6n->tnls_wc;
  1740. ip6n->tnls[1] = ip6n->tnls_r_l;
  1741. err = -ENOMEM;
  1742. ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
  1743. NET_NAME_UNKNOWN, ip6_tnl_dev_setup);
  1744. if (!ip6n->fb_tnl_dev)
  1745. goto err_alloc_dev;
  1746. dev_net_set(ip6n->fb_tnl_dev, net);
  1747. ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops;
  1748. /* FB netdevice is special: we have one, and only one per netns.
  1749. * Allowing to move it to another netns is clearly unsafe.
  1750. */
  1751. ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL;
  1752. err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
  1753. if (err < 0)
  1754. goto err_register;
  1755. err = register_netdev(ip6n->fb_tnl_dev);
  1756. if (err < 0)
  1757. goto err_register;
  1758. t = netdev_priv(ip6n->fb_tnl_dev);
  1759. strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
  1760. return 0;
  1761. err_register:
  1762. ip6_dev_free(ip6n->fb_tnl_dev);
  1763. err_alloc_dev:
  1764. return err;
  1765. }
  1766. static void __net_exit ip6_tnl_exit_net(struct net *net)
  1767. {
  1768. rtnl_lock();
  1769. ip6_tnl_destroy_tunnels(net);
  1770. rtnl_unlock();
  1771. }
  1772. static struct pernet_operations ip6_tnl_net_ops = {
  1773. .init = ip6_tnl_init_net,
  1774. .exit = ip6_tnl_exit_net,
  1775. .id = &ip6_tnl_net_id,
  1776. .size = sizeof(struct ip6_tnl_net),
  1777. };
  1778. /**
  1779. * ip6_tunnel_init - register protocol and reserve needed resources
  1780. *
  1781. * Return: 0 on success
  1782. **/
  1783. static int __init ip6_tunnel_init(void)
  1784. {
  1785. int err;
  1786. err = register_pernet_device(&ip6_tnl_net_ops);
  1787. if (err < 0)
  1788. goto out_pernet;
  1789. err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
  1790. if (err < 0) {
  1791. pr_err("%s: can't register ip4ip6\n", __func__);
  1792. goto out_ip4ip6;
  1793. }
  1794. err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
  1795. if (err < 0) {
  1796. pr_err("%s: can't register ip6ip6\n", __func__);
  1797. goto out_ip6ip6;
  1798. }
  1799. err = rtnl_link_register(&ip6_link_ops);
  1800. if (err < 0)
  1801. goto rtnl_link_failed;
  1802. return 0;
  1803. rtnl_link_failed:
  1804. xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
  1805. out_ip6ip6:
  1806. xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
  1807. out_ip4ip6:
  1808. unregister_pernet_device(&ip6_tnl_net_ops);
  1809. out_pernet:
  1810. return err;
  1811. }
  1812. /**
  1813. * ip6_tunnel_cleanup - free resources and unregister protocol
  1814. **/
  1815. static void __exit ip6_tunnel_cleanup(void)
  1816. {
  1817. rtnl_link_unregister(&ip6_link_ops);
  1818. if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
  1819. pr_info("%s: can't deregister ip4ip6\n", __func__);
  1820. if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
  1821. pr_info("%s: can't deregister ip6ip6\n", __func__);
  1822. unregister_pernet_device(&ip6_tnl_net_ops);
  1823. }
  1824. module_init(ip6_tunnel_init);
  1825. module_exit(ip6_tunnel_cleanup);