ip6_tunnel.c 36 KB

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