ip6_tunnel.c 37 KB

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