ip6_tunnel.c 37 KB

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