sit.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  8. *
  9. * $Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * Changes:
  17. * Roger Venning <r.venning@telstra.com>: 6to4 support
  18. * Nate Thompson <nate@thebog.net>: 6to4 support
  19. * Fred Templin <fred.l.templin@boeing.com>: isatap support
  20. */
  21. #include <linux/module.h>
  22. #include <linux/capability.h>
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/socket.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/in6.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/icmp.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/init.h>
  34. #include <linux/netfilter_ipv4.h>
  35. #include <linux/if_ether.h>
  36. #include <net/sock.h>
  37. #include <net/snmp.h>
  38. #include <net/ipv6.h>
  39. #include <net/protocol.h>
  40. #include <net/transp_v6.h>
  41. #include <net/ip6_fib.h>
  42. #include <net/ip6_route.h>
  43. #include <net/ndisc.h>
  44. #include <net/addrconf.h>
  45. #include <net/ip.h>
  46. #include <net/udp.h>
  47. #include <net/icmp.h>
  48. #include <net/ipip.h>
  49. #include <net/inet_ecn.h>
  50. #include <net/xfrm.h>
  51. #include <net/dsfield.h>
  52. /*
  53. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  54. For comments look at net/ipv4/ip_gre.c --ANK
  55. */
  56. #define HASH_SIZE 16
  57. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  58. static int ipip6_fb_tunnel_init(struct net_device *dev);
  59. static int ipip6_tunnel_init(struct net_device *dev);
  60. static void ipip6_tunnel_setup(struct net_device *dev);
  61. static struct net_device *ipip6_fb_tunnel_dev;
  62. static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
  63. static struct ip_tunnel *tunnels_r[HASH_SIZE];
  64. static struct ip_tunnel *tunnels_l[HASH_SIZE];
  65. static struct ip_tunnel *tunnels_wc[1];
  66. static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
  67. static DEFINE_RWLOCK(ipip6_lock);
  68. static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
  69. {
  70. unsigned h0 = HASH(remote);
  71. unsigned h1 = HASH(local);
  72. struct ip_tunnel *t;
  73. for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
  74. if (local == t->parms.iph.saddr &&
  75. remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  76. return t;
  77. }
  78. for (t = tunnels_r[h0]; t; t = t->next) {
  79. if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  80. return t;
  81. }
  82. for (t = tunnels_l[h1]; t; t = t->next) {
  83. if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
  84. return t;
  85. }
  86. if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
  87. return t;
  88. return NULL;
  89. }
  90. static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms)
  91. {
  92. __be32 remote = parms->iph.daddr;
  93. __be32 local = parms->iph.saddr;
  94. unsigned h = 0;
  95. int prio = 0;
  96. if (remote) {
  97. prio |= 2;
  98. h ^= HASH(remote);
  99. }
  100. if (local) {
  101. prio |= 1;
  102. h ^= HASH(local);
  103. }
  104. return &tunnels[prio][h];
  105. }
  106. static inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t)
  107. {
  108. return __ipip6_bucket(&t->parms);
  109. }
  110. static void ipip6_tunnel_unlink(struct ip_tunnel *t)
  111. {
  112. struct ip_tunnel **tp;
  113. for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
  114. if (t == *tp) {
  115. write_lock_bh(&ipip6_lock);
  116. *tp = t->next;
  117. write_unlock_bh(&ipip6_lock);
  118. break;
  119. }
  120. }
  121. }
  122. static void ipip6_tunnel_link(struct ip_tunnel *t)
  123. {
  124. struct ip_tunnel **tp = ipip6_bucket(t);
  125. t->next = *tp;
  126. write_lock_bh(&ipip6_lock);
  127. *tp = t;
  128. write_unlock_bh(&ipip6_lock);
  129. }
  130. static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
  131. {
  132. __be32 remote = parms->iph.daddr;
  133. __be32 local = parms->iph.saddr;
  134. struct ip_tunnel *t, **tp, *nt;
  135. struct net_device *dev;
  136. char name[IFNAMSIZ];
  137. for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) {
  138. if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
  139. return t;
  140. }
  141. if (!create)
  142. goto failed;
  143. if (parms->name[0])
  144. strlcpy(name, parms->name, IFNAMSIZ);
  145. else
  146. sprintf(name, "sit%%d");
  147. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  148. if (dev == NULL)
  149. return NULL;
  150. if (strchr(name, '%')) {
  151. if (dev_alloc_name(dev, name) < 0)
  152. goto failed_free;
  153. }
  154. nt = netdev_priv(dev);
  155. dev->init = ipip6_tunnel_init;
  156. nt->parms = *parms;
  157. if (parms->i_flags & SIT_ISATAP)
  158. dev->priv_flags |= IFF_ISATAP;
  159. if (register_netdevice(dev) < 0)
  160. goto failed_free;
  161. dev_hold(dev);
  162. ipip6_tunnel_link(nt);
  163. return nt;
  164. failed_free:
  165. free_netdev(dev);
  166. failed:
  167. return NULL;
  168. }
  169. static struct ip_tunnel_prl_entry *
  170. __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
  171. {
  172. struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL;
  173. for (p = t->prl; p; p = p->next)
  174. if (p->entry.addr == addr)
  175. break;
  176. return p;
  177. }
  178. static int
  179. ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
  180. {
  181. struct ip_tunnel_prl_entry *p;
  182. int err = 0;
  183. write_lock(&ipip6_lock);
  184. for (p = t->prl; p; p = p->next) {
  185. if (p->entry.addr == a->addr) {
  186. if (chg)
  187. goto update;
  188. err = -EEXIST;
  189. goto out;
  190. }
  191. }
  192. if (chg) {
  193. err = -ENXIO;
  194. goto out;
  195. }
  196. p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
  197. if (!p) {
  198. err = -ENOBUFS;
  199. goto out;
  200. }
  201. p->next = t->prl;
  202. t->prl = p;
  203. update:
  204. p->entry = *a;
  205. out:
  206. write_unlock(&ipip6_lock);
  207. return err;
  208. }
  209. static int
  210. ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
  211. {
  212. struct ip_tunnel_prl_entry *x, **p;
  213. int err = 0;
  214. write_lock(&ipip6_lock);
  215. if (a) {
  216. for (p = &t->prl; *p; p = &(*p)->next) {
  217. if ((*p)->entry.addr == a->addr) {
  218. x = *p;
  219. *p = x->next;
  220. kfree(x);
  221. goto out;
  222. }
  223. }
  224. err = -ENXIO;
  225. } else {
  226. while (t->prl) {
  227. x = t->prl;
  228. t->prl = t->prl->next;
  229. kfree(x);
  230. }
  231. }
  232. out:
  233. write_unlock(&ipip6_lock);
  234. return 0;
  235. }
  236. /* copied directly from anycast.c */
  237. static int
  238. ipip6_onlink(struct in6_addr *addr, struct net_device *dev)
  239. {
  240. struct inet6_dev *idev;
  241. struct inet6_ifaddr *ifa;
  242. int onlink;
  243. onlink = 0;
  244. rcu_read_lock();
  245. idev = __in6_dev_get(dev);
  246. if (idev) {
  247. read_lock_bh(&idev->lock);
  248. for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
  249. onlink = ipv6_prefix_equal(addr, &ifa->addr,
  250. ifa->prefix_len);
  251. if (onlink)
  252. break;
  253. }
  254. read_unlock_bh(&idev->lock);
  255. }
  256. rcu_read_unlock();
  257. return onlink;
  258. }
  259. static int
  260. isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
  261. {
  262. struct ip_tunnel_prl_entry *p;
  263. int ok = 1;
  264. read_lock(&ipip6_lock);
  265. p = __ipip6_tunnel_locate_prl(t, iph->saddr);
  266. if (p) {
  267. if (p->entry.flags & PRL_DEFAULT)
  268. skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
  269. else
  270. skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
  271. } else {
  272. struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
  273. if (ipv6_addr_is_isatap(addr6) &&
  274. (addr6->s6_addr32[3] == iph->saddr) &&
  275. ipip6_onlink(addr6, t->dev))
  276. skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
  277. else
  278. ok = 0;
  279. }
  280. read_unlock(&ipip6_lock);
  281. return ok;
  282. }
  283. static void ipip6_tunnel_uninit(struct net_device *dev)
  284. {
  285. if (dev == ipip6_fb_tunnel_dev) {
  286. write_lock_bh(&ipip6_lock);
  287. tunnels_wc[0] = NULL;
  288. write_unlock_bh(&ipip6_lock);
  289. dev_put(dev);
  290. } else {
  291. ipip6_tunnel_unlink(netdev_priv(dev));
  292. ipip6_tunnel_del_prl(netdev_priv(dev), 0);
  293. dev_put(dev);
  294. }
  295. }
  296. static int ipip6_err(struct sk_buff *skb, u32 info)
  297. {
  298. #ifndef I_WISH_WORLD_WERE_PERFECT
  299. /* It is not :-( All the routers (except for Linux) return only
  300. 8 bytes of packet payload. It means, that precise relaying of
  301. ICMP in the real Internet is absolutely infeasible.
  302. */
  303. struct iphdr *iph = (struct iphdr*)skb->data;
  304. const int type = icmp_hdr(skb)->type;
  305. const int code = icmp_hdr(skb)->code;
  306. struct ip_tunnel *t;
  307. int err;
  308. switch (type) {
  309. default:
  310. case ICMP_PARAMETERPROB:
  311. return 0;
  312. case ICMP_DEST_UNREACH:
  313. switch (code) {
  314. case ICMP_SR_FAILED:
  315. case ICMP_PORT_UNREACH:
  316. /* Impossible event. */
  317. return 0;
  318. case ICMP_FRAG_NEEDED:
  319. /* Soft state for pmtu is maintained by IP core. */
  320. return 0;
  321. default:
  322. /* All others are translated to HOST_UNREACH.
  323. rfc2003 contains "deep thoughts" about NET_UNREACH,
  324. I believe they are just ether pollution. --ANK
  325. */
  326. break;
  327. }
  328. break;
  329. case ICMP_TIME_EXCEEDED:
  330. if (code != ICMP_EXC_TTL)
  331. return 0;
  332. break;
  333. }
  334. err = -ENOENT;
  335. read_lock(&ipip6_lock);
  336. t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
  337. if (t == NULL || t->parms.iph.daddr == 0)
  338. goto out;
  339. err = 0;
  340. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  341. goto out;
  342. if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
  343. t->err_count++;
  344. else
  345. t->err_count = 1;
  346. t->err_time = jiffies;
  347. out:
  348. read_unlock(&ipip6_lock);
  349. return err;
  350. #else
  351. struct iphdr *iph = (struct iphdr*)dp;
  352. int hlen = iph->ihl<<2;
  353. struct ipv6hdr *iph6;
  354. const int type = icmp_hdr(skb)->type;
  355. const int code = icmp_hdr(skb)->code;
  356. int rel_type = 0;
  357. int rel_code = 0;
  358. int rel_info = 0;
  359. struct sk_buff *skb2;
  360. struct rt6_info *rt6i;
  361. if (len < hlen + sizeof(struct ipv6hdr))
  362. return;
  363. iph6 = (struct ipv6hdr*)(dp + hlen);
  364. switch (type) {
  365. default:
  366. return;
  367. case ICMP_PARAMETERPROB:
  368. if (icmp_hdr(skb)->un.gateway < hlen)
  369. return;
  370. /* So... This guy found something strange INSIDE encapsulated
  371. packet. Well, he is fool, but what can we do ?
  372. */
  373. rel_type = ICMPV6_PARAMPROB;
  374. rel_info = icmp_hdr(skb)->un.gateway - hlen;
  375. break;
  376. case ICMP_DEST_UNREACH:
  377. switch (code) {
  378. case ICMP_SR_FAILED:
  379. case ICMP_PORT_UNREACH:
  380. /* Impossible event. */
  381. return;
  382. case ICMP_FRAG_NEEDED:
  383. /* Too complicated case ... */
  384. return;
  385. default:
  386. /* All others are translated to HOST_UNREACH.
  387. rfc2003 contains "deep thoughts" about NET_UNREACH,
  388. I believe, it is just ether pollution. --ANK
  389. */
  390. rel_type = ICMPV6_DEST_UNREACH;
  391. rel_code = ICMPV6_ADDR_UNREACH;
  392. break;
  393. }
  394. break;
  395. case ICMP_TIME_EXCEEDED:
  396. if (code != ICMP_EXC_TTL)
  397. return;
  398. rel_type = ICMPV6_TIME_EXCEED;
  399. rel_code = ICMPV6_EXC_HOPLIMIT;
  400. break;
  401. }
  402. /* Prepare fake skb to feed it to icmpv6_send */
  403. skb2 = skb_clone(skb, GFP_ATOMIC);
  404. if (skb2 == NULL)
  405. return 0;
  406. dst_release(skb2->dst);
  407. skb2->dst = NULL;
  408. skb_pull(skb2, skb->data - (u8*)iph6);
  409. skb_reset_network_header(skb2);
  410. /* Try to guess incoming interface */
  411. rt6i = rt6_lookup(&init_net, &iph6->saddr, NULL, NULL, 0);
  412. if (rt6i && rt6i->rt6i_dev) {
  413. skb2->dev = rt6i->rt6i_dev;
  414. rt6i = rt6_lookup(&init_net, &iph6->daddr, &iph6->saddr, NULL, 0);
  415. if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
  416. struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
  417. if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
  418. rel_type = ICMPV6_DEST_UNREACH;
  419. rel_code = ICMPV6_ADDR_UNREACH;
  420. }
  421. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  422. }
  423. }
  424. kfree_skb(skb2);
  425. return 0;
  426. #endif
  427. }
  428. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  429. {
  430. if (INET_ECN_is_ce(iph->tos))
  431. IP6_ECN_set_ce(ipv6_hdr(skb));
  432. }
  433. static int ipip6_rcv(struct sk_buff *skb)
  434. {
  435. struct iphdr *iph;
  436. struct ip_tunnel *tunnel;
  437. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  438. goto out;
  439. iph = ip_hdr(skb);
  440. read_lock(&ipip6_lock);
  441. if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
  442. secpath_reset(skb);
  443. skb->mac_header = skb->network_header;
  444. skb_reset_network_header(skb);
  445. IPCB(skb)->flags = 0;
  446. skb->protocol = htons(ETH_P_IPV6);
  447. skb->pkt_type = PACKET_HOST;
  448. if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
  449. !isatap_chksrc(skb, iph, tunnel)) {
  450. tunnel->stat.rx_errors++;
  451. read_unlock(&ipip6_lock);
  452. kfree_skb(skb);
  453. return 0;
  454. }
  455. tunnel->stat.rx_packets++;
  456. tunnel->stat.rx_bytes += skb->len;
  457. skb->dev = tunnel->dev;
  458. dst_release(skb->dst);
  459. skb->dst = NULL;
  460. nf_reset(skb);
  461. ipip6_ecn_decapsulate(iph, skb);
  462. netif_rx(skb);
  463. read_unlock(&ipip6_lock);
  464. return 0;
  465. }
  466. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  467. kfree_skb(skb);
  468. read_unlock(&ipip6_lock);
  469. out:
  470. return 0;
  471. }
  472. /* Returns the embedded IPv4 address if the IPv6 address
  473. comes from 6to4 (RFC 3056) addr space */
  474. static inline __be32 try_6to4(struct in6_addr *v6dst)
  475. {
  476. __be32 dst = 0;
  477. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  478. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  479. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  480. }
  481. return dst;
  482. }
  483. /*
  484. * This function assumes it is being called from dev_queue_xmit()
  485. * and that skb is filled properly by that function.
  486. */
  487. static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  488. {
  489. struct ip_tunnel *tunnel = netdev_priv(dev);
  490. struct net_device_stats *stats = &tunnel->stat;
  491. struct iphdr *tiph = &tunnel->parms.iph;
  492. struct ipv6hdr *iph6 = ipv6_hdr(skb);
  493. u8 tos = tunnel->parms.iph.tos;
  494. struct rtable *rt; /* Route to the other host */
  495. struct net_device *tdev; /* Device to other host */
  496. struct iphdr *iph; /* Our new IP header */
  497. unsigned int max_headroom; /* The extra header space needed */
  498. __be32 dst = tiph->daddr;
  499. int mtu;
  500. struct in6_addr *addr6;
  501. int addr_type;
  502. if (tunnel->recursion++) {
  503. tunnel->stat.collisions++;
  504. goto tx_error;
  505. }
  506. if (skb->protocol != htons(ETH_P_IPV6))
  507. goto tx_error;
  508. /* ISATAP (RFC4214) - must come before 6to4 */
  509. if (dev->priv_flags & IFF_ISATAP) {
  510. struct neighbour *neigh = NULL;
  511. if (skb->dst)
  512. neigh = skb->dst->neighbour;
  513. if (neigh == NULL) {
  514. if (net_ratelimit())
  515. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  516. goto tx_error;
  517. }
  518. addr6 = (struct in6_addr*)&neigh->primary_key;
  519. addr_type = ipv6_addr_type(addr6);
  520. if ((addr_type & IPV6_ADDR_UNICAST) &&
  521. ipv6_addr_is_isatap(addr6))
  522. dst = addr6->s6_addr32[3];
  523. else
  524. goto tx_error;
  525. }
  526. if (!dst)
  527. dst = try_6to4(&iph6->daddr);
  528. if (!dst) {
  529. struct neighbour *neigh = NULL;
  530. if (skb->dst)
  531. neigh = skb->dst->neighbour;
  532. if (neigh == NULL) {
  533. if (net_ratelimit())
  534. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  535. goto tx_error;
  536. }
  537. addr6 = (struct in6_addr*)&neigh->primary_key;
  538. addr_type = ipv6_addr_type(addr6);
  539. if (addr_type == IPV6_ADDR_ANY) {
  540. addr6 = &ipv6_hdr(skb)->daddr;
  541. addr_type = ipv6_addr_type(addr6);
  542. }
  543. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  544. goto tx_error_icmp;
  545. dst = addr6->s6_addr32[3];
  546. }
  547. {
  548. struct flowi fl = { .nl_u = { .ip4_u =
  549. { .daddr = dst,
  550. .saddr = tiph->saddr,
  551. .tos = RT_TOS(tos) } },
  552. .oif = tunnel->parms.link,
  553. .proto = IPPROTO_IPV6 };
  554. if (ip_route_output_key(&init_net, &rt, &fl)) {
  555. tunnel->stat.tx_carrier_errors++;
  556. goto tx_error_icmp;
  557. }
  558. }
  559. if (rt->rt_type != RTN_UNICAST) {
  560. ip_rt_put(rt);
  561. tunnel->stat.tx_carrier_errors++;
  562. goto tx_error_icmp;
  563. }
  564. tdev = rt->u.dst.dev;
  565. if (tdev == dev) {
  566. ip_rt_put(rt);
  567. tunnel->stat.collisions++;
  568. goto tx_error;
  569. }
  570. if (tiph->frag_off)
  571. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  572. else
  573. mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
  574. if (mtu < 68) {
  575. tunnel->stat.collisions++;
  576. ip_rt_put(rt);
  577. goto tx_error;
  578. }
  579. if (mtu < IPV6_MIN_MTU)
  580. mtu = IPV6_MIN_MTU;
  581. if (tunnel->parms.iph.daddr && skb->dst)
  582. skb->dst->ops->update_pmtu(skb->dst, mtu);
  583. if (skb->len > mtu) {
  584. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  585. ip_rt_put(rt);
  586. goto tx_error;
  587. }
  588. if (tunnel->err_count > 0) {
  589. if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
  590. tunnel->err_count--;
  591. dst_link_failure(skb);
  592. } else
  593. tunnel->err_count = 0;
  594. }
  595. /*
  596. * Okay, now see if we can stuff it in the buffer as-is.
  597. */
  598. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  599. if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
  600. (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
  601. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  602. if (!new_skb) {
  603. ip_rt_put(rt);
  604. stats->tx_dropped++;
  605. dev_kfree_skb(skb);
  606. tunnel->recursion--;
  607. return 0;
  608. }
  609. if (skb->sk)
  610. skb_set_owner_w(new_skb, skb->sk);
  611. dev_kfree_skb(skb);
  612. skb = new_skb;
  613. iph6 = ipv6_hdr(skb);
  614. }
  615. skb->transport_header = skb->network_header;
  616. skb_push(skb, sizeof(struct iphdr));
  617. skb_reset_network_header(skb);
  618. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  619. IPCB(skb)->flags = 0;
  620. dst_release(skb->dst);
  621. skb->dst = &rt->u.dst;
  622. /*
  623. * Push down and install the IPIP header.
  624. */
  625. iph = ip_hdr(skb);
  626. iph->version = 4;
  627. iph->ihl = sizeof(struct iphdr)>>2;
  628. if (mtu > IPV6_MIN_MTU)
  629. iph->frag_off = htons(IP_DF);
  630. else
  631. iph->frag_off = 0;
  632. iph->protocol = IPPROTO_IPV6;
  633. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  634. iph->daddr = rt->rt_dst;
  635. iph->saddr = rt->rt_src;
  636. if ((iph->ttl = tiph->ttl) == 0)
  637. iph->ttl = iph6->hop_limit;
  638. nf_reset(skb);
  639. IPTUNNEL_XMIT();
  640. tunnel->recursion--;
  641. return 0;
  642. tx_error_icmp:
  643. dst_link_failure(skb);
  644. tx_error:
  645. stats->tx_errors++;
  646. dev_kfree_skb(skb);
  647. tunnel->recursion--;
  648. return 0;
  649. }
  650. static void ipip6_tunnel_bind_dev(struct net_device *dev)
  651. {
  652. struct net_device *tdev = NULL;
  653. struct ip_tunnel *tunnel;
  654. struct iphdr *iph;
  655. tunnel = netdev_priv(dev);
  656. iph = &tunnel->parms.iph;
  657. if (iph->daddr) {
  658. struct flowi fl = { .nl_u = { .ip4_u =
  659. { .daddr = iph->daddr,
  660. .saddr = iph->saddr,
  661. .tos = RT_TOS(iph->tos) } },
  662. .oif = tunnel->parms.link,
  663. .proto = IPPROTO_IPV6 };
  664. struct rtable *rt;
  665. if (!ip_route_output_key(&init_net, &rt, &fl)) {
  666. tdev = rt->u.dst.dev;
  667. ip_rt_put(rt);
  668. }
  669. dev->flags |= IFF_POINTOPOINT;
  670. }
  671. if (!tdev && tunnel->parms.link)
  672. tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
  673. if (tdev) {
  674. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  675. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  676. if (dev->mtu < IPV6_MIN_MTU)
  677. dev->mtu = IPV6_MIN_MTU;
  678. }
  679. dev->iflink = tunnel->parms.link;
  680. }
  681. static int
  682. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  683. {
  684. int err = 0;
  685. struct ip_tunnel_parm p;
  686. struct ip_tunnel_prl prl;
  687. struct ip_tunnel *t;
  688. switch (cmd) {
  689. case SIOCGETTUNNEL:
  690. t = NULL;
  691. if (dev == ipip6_fb_tunnel_dev) {
  692. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  693. err = -EFAULT;
  694. break;
  695. }
  696. t = ipip6_tunnel_locate(&p, 0);
  697. }
  698. if (t == NULL)
  699. t = netdev_priv(dev);
  700. memcpy(&p, &t->parms, sizeof(p));
  701. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  702. err = -EFAULT;
  703. break;
  704. case SIOCADDTUNNEL:
  705. case SIOCCHGTUNNEL:
  706. err = -EPERM;
  707. if (!capable(CAP_NET_ADMIN))
  708. goto done;
  709. err = -EFAULT;
  710. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  711. goto done;
  712. err = -EINVAL;
  713. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  714. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  715. goto done;
  716. if (p.iph.ttl)
  717. p.iph.frag_off |= htons(IP_DF);
  718. t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
  719. if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  720. if (t != NULL) {
  721. if (t->dev != dev) {
  722. err = -EEXIST;
  723. break;
  724. }
  725. } else {
  726. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  727. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  728. err = -EINVAL;
  729. break;
  730. }
  731. t = netdev_priv(dev);
  732. ipip6_tunnel_unlink(t);
  733. t->parms.iph.saddr = p.iph.saddr;
  734. t->parms.iph.daddr = p.iph.daddr;
  735. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  736. memcpy(dev->broadcast, &p.iph.daddr, 4);
  737. ipip6_tunnel_link(t);
  738. netdev_state_change(dev);
  739. }
  740. }
  741. if (t) {
  742. err = 0;
  743. if (cmd == SIOCCHGTUNNEL) {
  744. t->parms.iph.ttl = p.iph.ttl;
  745. t->parms.iph.tos = p.iph.tos;
  746. if (t->parms.link != p.link) {
  747. t->parms.link = p.link;
  748. ipip6_tunnel_bind_dev(dev);
  749. netdev_state_change(dev);
  750. }
  751. }
  752. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  753. err = -EFAULT;
  754. } else
  755. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  756. break;
  757. case SIOCDELTUNNEL:
  758. err = -EPERM;
  759. if (!capable(CAP_NET_ADMIN))
  760. goto done;
  761. if (dev == ipip6_fb_tunnel_dev) {
  762. err = -EFAULT;
  763. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  764. goto done;
  765. err = -ENOENT;
  766. if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
  767. goto done;
  768. err = -EPERM;
  769. if (t == netdev_priv(ipip6_fb_tunnel_dev))
  770. goto done;
  771. dev = t->dev;
  772. }
  773. unregister_netdevice(dev);
  774. err = 0;
  775. break;
  776. case SIOCADDPRL:
  777. case SIOCDELPRL:
  778. case SIOCCHGPRL:
  779. err = -EPERM;
  780. if (!capable(CAP_NET_ADMIN))
  781. goto done;
  782. err = -EINVAL;
  783. if (dev == ipip6_fb_tunnel_dev)
  784. goto done;
  785. err = -EFAULT;
  786. if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
  787. goto done;
  788. err = -ENOENT;
  789. if (!(t = netdev_priv(dev)))
  790. goto done;
  791. if (cmd == SIOCDELPRL)
  792. err = ipip6_tunnel_del_prl(t, &prl);
  793. else
  794. err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
  795. netdev_state_change(dev);
  796. break;
  797. default:
  798. err = -EINVAL;
  799. }
  800. done:
  801. return err;
  802. }
  803. static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
  804. {
  805. return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
  806. }
  807. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  808. {
  809. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  810. return -EINVAL;
  811. dev->mtu = new_mtu;
  812. return 0;
  813. }
  814. static void ipip6_tunnel_setup(struct net_device *dev)
  815. {
  816. dev->uninit = ipip6_tunnel_uninit;
  817. dev->destructor = free_netdev;
  818. dev->hard_start_xmit = ipip6_tunnel_xmit;
  819. dev->get_stats = ipip6_tunnel_get_stats;
  820. dev->do_ioctl = ipip6_tunnel_ioctl;
  821. dev->change_mtu = ipip6_tunnel_change_mtu;
  822. dev->type = ARPHRD_SIT;
  823. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  824. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  825. dev->flags = IFF_NOARP;
  826. dev->iflink = 0;
  827. dev->addr_len = 4;
  828. }
  829. static int ipip6_tunnel_init(struct net_device *dev)
  830. {
  831. struct ip_tunnel *tunnel;
  832. tunnel = netdev_priv(dev);
  833. tunnel->dev = dev;
  834. strcpy(tunnel->parms.name, dev->name);
  835. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  836. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  837. ipip6_tunnel_bind_dev(dev);
  838. return 0;
  839. }
  840. static int __init ipip6_fb_tunnel_init(struct net_device *dev)
  841. {
  842. struct ip_tunnel *tunnel = netdev_priv(dev);
  843. struct iphdr *iph = &tunnel->parms.iph;
  844. tunnel->dev = dev;
  845. strcpy(tunnel->parms.name, dev->name);
  846. iph->version = 4;
  847. iph->protocol = IPPROTO_IPV6;
  848. iph->ihl = 5;
  849. iph->ttl = 64;
  850. dev_hold(dev);
  851. tunnels_wc[0] = tunnel;
  852. return 0;
  853. }
  854. static struct xfrm_tunnel sit_handler = {
  855. .handler = ipip6_rcv,
  856. .err_handler = ipip6_err,
  857. .priority = 1,
  858. };
  859. static void __exit sit_destroy_tunnels(void)
  860. {
  861. int prio;
  862. for (prio = 1; prio < 4; prio++) {
  863. int h;
  864. for (h = 0; h < HASH_SIZE; h++) {
  865. struct ip_tunnel *t;
  866. while ((t = tunnels[prio][h]) != NULL)
  867. unregister_netdevice(t->dev);
  868. }
  869. }
  870. }
  871. static void __exit sit_cleanup(void)
  872. {
  873. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  874. rtnl_lock();
  875. sit_destroy_tunnels();
  876. unregister_netdevice(ipip6_fb_tunnel_dev);
  877. rtnl_unlock();
  878. }
  879. static int __init sit_init(void)
  880. {
  881. int err;
  882. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  883. if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
  884. printk(KERN_INFO "sit init: Can't add protocol\n");
  885. return -EAGAIN;
  886. }
  887. ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  888. ipip6_tunnel_setup);
  889. if (!ipip6_fb_tunnel_dev) {
  890. err = -ENOMEM;
  891. goto err1;
  892. }
  893. ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
  894. if ((err = register_netdev(ipip6_fb_tunnel_dev)))
  895. goto err2;
  896. out:
  897. return err;
  898. err2:
  899. free_netdev(ipip6_fb_tunnel_dev);
  900. err1:
  901. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  902. goto out;
  903. }
  904. module_init(sit_init);
  905. module_exit(sit_cleanup);
  906. MODULE_LICENSE("GPL");
  907. MODULE_ALIAS("sit0");