ip6_tunnel.c 47 KB

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