sit.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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.1.1.1 2007/06/12 07:27:14 eyryu 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. */
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/net.h>
  27. #include <linux/in6.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/icmp.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/init.h>
  33. #include <linux/netfilter_ipv4.h>
  34. #include <linux/if_ether.h>
  35. #include <net/sock.h>
  36. #include <net/snmp.h>
  37. #include <net/ipv6.h>
  38. #include <net/protocol.h>
  39. #include <net/transp_v6.h>
  40. #include <net/ip6_fib.h>
  41. #include <net/ip6_route.h>
  42. #include <net/ndisc.h>
  43. #include <net/addrconf.h>
  44. #include <net/ip.h>
  45. #include <net/udp.h>
  46. #include <net/icmp.h>
  47. #include <net/ipip.h>
  48. #include <net/inet_ecn.h>
  49. #include <net/xfrm.h>
  50. #include <net/dsfield.h>
  51. /*
  52. This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
  53. For comments look at net/ipv4/ip_gre.c --ANK
  54. */
  55. #define HASH_SIZE 16
  56. #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
  57. static int ipip6_fb_tunnel_init(struct net_device *dev);
  58. static int ipip6_tunnel_init(struct net_device *dev);
  59. static void ipip6_tunnel_setup(struct net_device *dev);
  60. static struct net_device *ipip6_fb_tunnel_dev;
  61. static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
  62. static struct ip_tunnel *tunnels_r[HASH_SIZE];
  63. static struct ip_tunnel *tunnels_l[HASH_SIZE];
  64. static struct ip_tunnel *tunnels_wc[1];
  65. static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
  66. static DEFINE_RWLOCK(ipip6_lock);
  67. static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local)
  68. {
  69. unsigned h0 = HASH(remote);
  70. unsigned h1 = HASH(local);
  71. struct ip_tunnel *t;
  72. for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
  73. if (local == t->parms.iph.saddr &&
  74. remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  75. return t;
  76. }
  77. for (t = tunnels_r[h0]; t; t = t->next) {
  78. if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
  79. return t;
  80. }
  81. for (t = tunnels_l[h1]; t; t = t->next) {
  82. if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
  83. return t;
  84. }
  85. if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
  86. return t;
  87. return NULL;
  88. }
  89. static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
  90. {
  91. __be32 remote = t->parms.iph.daddr;
  92. __be32 local = t->parms.iph.saddr;
  93. unsigned h = 0;
  94. int prio = 0;
  95. if (remote) {
  96. prio |= 2;
  97. h ^= HASH(remote);
  98. }
  99. if (local) {
  100. prio |= 1;
  101. h ^= HASH(local);
  102. }
  103. return &tunnels[prio][h];
  104. }
  105. static void ipip6_tunnel_unlink(struct ip_tunnel *t)
  106. {
  107. struct ip_tunnel **tp;
  108. for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
  109. if (t == *tp) {
  110. write_lock_bh(&ipip6_lock);
  111. *tp = t->next;
  112. write_unlock_bh(&ipip6_lock);
  113. break;
  114. }
  115. }
  116. }
  117. static void ipip6_tunnel_link(struct ip_tunnel *t)
  118. {
  119. struct ip_tunnel **tp = ipip6_bucket(t);
  120. t->next = *tp;
  121. write_lock_bh(&ipip6_lock);
  122. *tp = t;
  123. write_unlock_bh(&ipip6_lock);
  124. }
  125. static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
  126. {
  127. __be32 remote = parms->iph.daddr;
  128. __be32 local = parms->iph.saddr;
  129. struct ip_tunnel *t, **tp, *nt;
  130. struct net_device *dev;
  131. unsigned h = 0;
  132. int prio = 0;
  133. char name[IFNAMSIZ];
  134. if (remote) {
  135. prio |= 2;
  136. h ^= HASH(remote);
  137. }
  138. if (local) {
  139. prio |= 1;
  140. h ^= HASH(local);
  141. }
  142. for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
  143. if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
  144. return t;
  145. }
  146. if (!create)
  147. goto failed;
  148. if (parms->name[0])
  149. strlcpy(name, parms->name, IFNAMSIZ);
  150. else {
  151. int i;
  152. for (i=1; i<100; i++) {
  153. sprintf(name, "sit%d", i);
  154. if (__dev_get_by_name(name) == NULL)
  155. break;
  156. }
  157. if (i==100)
  158. goto failed;
  159. }
  160. dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
  161. if (dev == NULL)
  162. return NULL;
  163. nt = netdev_priv(dev);
  164. dev->init = ipip6_tunnel_init;
  165. nt->parms = *parms;
  166. if (register_netdevice(dev) < 0) {
  167. free_netdev(dev);
  168. goto failed;
  169. }
  170. dev_hold(dev);
  171. ipip6_tunnel_link(nt);
  172. return nt;
  173. failed:
  174. return NULL;
  175. }
  176. static void ipip6_tunnel_uninit(struct net_device *dev)
  177. {
  178. if (dev == ipip6_fb_tunnel_dev) {
  179. write_lock_bh(&ipip6_lock);
  180. tunnels_wc[0] = NULL;
  181. write_unlock_bh(&ipip6_lock);
  182. dev_put(dev);
  183. } else {
  184. ipip6_tunnel_unlink(netdev_priv(dev));
  185. dev_put(dev);
  186. }
  187. }
  188. static int ipip6_err(struct sk_buff *skb, u32 info)
  189. {
  190. #ifndef I_WISH_WORLD_WERE_PERFECT
  191. /* It is not :-( All the routers (except for Linux) return only
  192. 8 bytes of packet payload. It means, that precise relaying of
  193. ICMP in the real Internet is absolutely infeasible.
  194. */
  195. struct iphdr *iph = (struct iphdr*)skb->data;
  196. int type = skb->h.icmph->type;
  197. int code = skb->h.icmph->code;
  198. struct ip_tunnel *t;
  199. int err;
  200. switch (type) {
  201. default:
  202. case ICMP_PARAMETERPROB:
  203. return 0;
  204. case ICMP_DEST_UNREACH:
  205. switch (code) {
  206. case ICMP_SR_FAILED:
  207. case ICMP_PORT_UNREACH:
  208. /* Impossible event. */
  209. return 0;
  210. case ICMP_FRAG_NEEDED:
  211. /* Soft state for pmtu is maintained by IP core. */
  212. return 0;
  213. default:
  214. /* All others are translated to HOST_UNREACH.
  215. rfc2003 contains "deep thoughts" about NET_UNREACH,
  216. I believe they are just ether pollution. --ANK
  217. */
  218. break;
  219. }
  220. break;
  221. case ICMP_TIME_EXCEEDED:
  222. if (code != ICMP_EXC_TTL)
  223. return 0;
  224. break;
  225. }
  226. err = -ENOENT;
  227. read_lock(&ipip6_lock);
  228. t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
  229. if (t == NULL || t->parms.iph.daddr == 0)
  230. goto out;
  231. err = 0;
  232. if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
  233. goto out;
  234. if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
  235. t->err_count++;
  236. else
  237. t->err_count = 1;
  238. t->err_time = jiffies;
  239. out:
  240. read_unlock(&ipip6_lock);
  241. return err;
  242. #else
  243. struct iphdr *iph = (struct iphdr*)dp;
  244. int hlen = iph->ihl<<2;
  245. struct ipv6hdr *iph6;
  246. int type = skb->h.icmph->type;
  247. int code = skb->h.icmph->code;
  248. int rel_type = 0;
  249. int rel_code = 0;
  250. int rel_info = 0;
  251. struct sk_buff *skb2;
  252. struct rt6_info *rt6i;
  253. if (len < hlen + sizeof(struct ipv6hdr))
  254. return;
  255. iph6 = (struct ipv6hdr*)(dp + hlen);
  256. switch (type) {
  257. default:
  258. return;
  259. case ICMP_PARAMETERPROB:
  260. if (skb->h.icmph->un.gateway < hlen)
  261. return;
  262. /* So... This guy found something strange INSIDE encapsulated
  263. packet. Well, he is fool, but what can we do ?
  264. */
  265. rel_type = ICMPV6_PARAMPROB;
  266. rel_info = skb->h.icmph->un.gateway - hlen;
  267. break;
  268. case ICMP_DEST_UNREACH:
  269. switch (code) {
  270. case ICMP_SR_FAILED:
  271. case ICMP_PORT_UNREACH:
  272. /* Impossible event. */
  273. return;
  274. case ICMP_FRAG_NEEDED:
  275. /* Too complicated case ... */
  276. return;
  277. default:
  278. /* All others are translated to HOST_UNREACH.
  279. rfc2003 contains "deep thoughts" about NET_UNREACH,
  280. I believe, it is just ether pollution. --ANK
  281. */
  282. rel_type = ICMPV6_DEST_UNREACH;
  283. rel_code = ICMPV6_ADDR_UNREACH;
  284. break;
  285. }
  286. break;
  287. case ICMP_TIME_EXCEEDED:
  288. if (code != ICMP_EXC_TTL)
  289. return;
  290. rel_type = ICMPV6_TIME_EXCEED;
  291. rel_code = ICMPV6_EXC_HOPLIMIT;
  292. break;
  293. }
  294. /* Prepare fake skb to feed it to icmpv6_send */
  295. skb2 = skb_clone(skb, GFP_ATOMIC);
  296. if (skb2 == NULL)
  297. return 0;
  298. dst_release(skb2->dst);
  299. skb2->dst = NULL;
  300. skb_pull(skb2, skb->data - (u8*)iph6);
  301. skb2->nh.raw = skb2->data;
  302. /* Try to guess incoming interface */
  303. rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
  304. if (rt6i && rt6i->rt6i_dev) {
  305. skb2->dev = rt6i->rt6i_dev;
  306. rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
  307. if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
  308. struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
  309. if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
  310. rel_type = ICMPV6_DEST_UNREACH;
  311. rel_code = ICMPV6_ADDR_UNREACH;
  312. }
  313. icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
  314. }
  315. }
  316. kfree_skb(skb2);
  317. return 0;
  318. #endif
  319. }
  320. static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
  321. {
  322. if (INET_ECN_is_ce(iph->tos))
  323. IP6_ECN_set_ce(skb->nh.ipv6h);
  324. }
  325. static int ipip6_rcv(struct sk_buff *skb)
  326. {
  327. struct iphdr *iph;
  328. struct ip_tunnel *tunnel;
  329. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  330. goto out;
  331. iph = skb->nh.iph;
  332. read_lock(&ipip6_lock);
  333. if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
  334. secpath_reset(skb);
  335. skb->mac.raw = skb->nh.raw;
  336. skb->nh.raw = skb->data;
  337. IPCB(skb)->flags = 0;
  338. skb->protocol = htons(ETH_P_IPV6);
  339. skb->pkt_type = PACKET_HOST;
  340. tunnel->stat.rx_packets++;
  341. tunnel->stat.rx_bytes += skb->len;
  342. skb->dev = tunnel->dev;
  343. dst_release(skb->dst);
  344. skb->dst = NULL;
  345. nf_reset(skb);
  346. ipip6_ecn_decapsulate(iph, skb);
  347. netif_rx(skb);
  348. read_unlock(&ipip6_lock);
  349. return 0;
  350. }
  351. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  352. kfree_skb(skb);
  353. read_unlock(&ipip6_lock);
  354. out:
  355. return 0;
  356. }
  357. /* Returns the embedded IPv4 address if the IPv6 address
  358. comes from 6to4 (RFC 3056) addr space */
  359. static inline __be32 try_6to4(struct in6_addr *v6dst)
  360. {
  361. __be32 dst = 0;
  362. if (v6dst->s6_addr16[0] == htons(0x2002)) {
  363. /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
  364. memcpy(&dst, &v6dst->s6_addr16[1], 4);
  365. }
  366. return dst;
  367. }
  368. /*
  369. * This function assumes it is being called from dev_queue_xmit()
  370. * and that skb is filled properly by that function.
  371. */
  372. static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
  373. {
  374. struct ip_tunnel *tunnel = netdev_priv(dev);
  375. struct net_device_stats *stats = &tunnel->stat;
  376. struct iphdr *tiph = &tunnel->parms.iph;
  377. struct ipv6hdr *iph6 = skb->nh.ipv6h;
  378. u8 tos = tunnel->parms.iph.tos;
  379. struct rtable *rt; /* Route to the other host */
  380. struct net_device *tdev; /* Device to other host */
  381. struct iphdr *iph; /* Our new IP header */
  382. int max_headroom; /* The extra header space needed */
  383. __be32 dst = tiph->daddr;
  384. int mtu;
  385. struct in6_addr *addr6;
  386. int addr_type;
  387. if (tunnel->recursion++) {
  388. tunnel->stat.collisions++;
  389. goto tx_error;
  390. }
  391. if (skb->protocol != htons(ETH_P_IPV6))
  392. goto tx_error;
  393. if (!dst)
  394. dst = try_6to4(&iph6->daddr);
  395. if (!dst) {
  396. struct neighbour *neigh = NULL;
  397. if (skb->dst)
  398. neigh = skb->dst->neighbour;
  399. if (neigh == NULL) {
  400. if (net_ratelimit())
  401. printk(KERN_DEBUG "sit: nexthop == NULL\n");
  402. goto tx_error;
  403. }
  404. addr6 = (struct in6_addr*)&neigh->primary_key;
  405. addr_type = ipv6_addr_type(addr6);
  406. if (addr_type == IPV6_ADDR_ANY) {
  407. addr6 = &skb->nh.ipv6h->daddr;
  408. addr_type = ipv6_addr_type(addr6);
  409. }
  410. if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
  411. goto tx_error_icmp;
  412. dst = addr6->s6_addr32[3];
  413. }
  414. {
  415. struct flowi fl = { .nl_u = { .ip4_u =
  416. { .daddr = dst,
  417. .saddr = tiph->saddr,
  418. .tos = RT_TOS(tos) } },
  419. .oif = tunnel->parms.link,
  420. .proto = IPPROTO_IPV6 };
  421. if (ip_route_output_key(&rt, &fl)) {
  422. tunnel->stat.tx_carrier_errors++;
  423. goto tx_error_icmp;
  424. }
  425. }
  426. if (rt->rt_type != RTN_UNICAST) {
  427. ip_rt_put(rt);
  428. tunnel->stat.tx_carrier_errors++;
  429. goto tx_error_icmp;
  430. }
  431. tdev = rt->u.dst.dev;
  432. if (tdev == dev) {
  433. ip_rt_put(rt);
  434. tunnel->stat.collisions++;
  435. goto tx_error;
  436. }
  437. if (tiph->frag_off)
  438. mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
  439. else
  440. mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
  441. if (mtu < 68) {
  442. tunnel->stat.collisions++;
  443. ip_rt_put(rt);
  444. goto tx_error;
  445. }
  446. if (mtu < IPV6_MIN_MTU)
  447. mtu = IPV6_MIN_MTU;
  448. if (tunnel->parms.iph.daddr && skb->dst)
  449. skb->dst->ops->update_pmtu(skb->dst, mtu);
  450. if (skb->len > mtu) {
  451. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
  452. ip_rt_put(rt);
  453. goto tx_error;
  454. }
  455. if (tunnel->err_count > 0) {
  456. if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
  457. tunnel->err_count--;
  458. dst_link_failure(skb);
  459. } else
  460. tunnel->err_count = 0;
  461. }
  462. /*
  463. * Okay, now see if we can stuff it in the buffer as-is.
  464. */
  465. max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
  466. if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
  467. struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
  468. if (!new_skb) {
  469. ip_rt_put(rt);
  470. stats->tx_dropped++;
  471. dev_kfree_skb(skb);
  472. tunnel->recursion--;
  473. return 0;
  474. }
  475. if (skb->sk)
  476. skb_set_owner_w(new_skb, skb->sk);
  477. dev_kfree_skb(skb);
  478. skb = new_skb;
  479. iph6 = skb->nh.ipv6h;
  480. }
  481. skb->h.raw = skb->nh.raw;
  482. skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
  483. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  484. IPCB(skb)->flags = 0;
  485. dst_release(skb->dst);
  486. skb->dst = &rt->u.dst;
  487. /*
  488. * Push down and install the IPIP header.
  489. */
  490. iph = skb->nh.iph;
  491. iph->version = 4;
  492. iph->ihl = sizeof(struct iphdr)>>2;
  493. if (mtu > IPV6_MIN_MTU)
  494. iph->frag_off = htons(IP_DF);
  495. else
  496. iph->frag_off = 0;
  497. iph->protocol = IPPROTO_IPV6;
  498. iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
  499. iph->daddr = rt->rt_dst;
  500. iph->saddr = rt->rt_src;
  501. if ((iph->ttl = tiph->ttl) == 0)
  502. iph->ttl = iph6->hop_limit;
  503. nf_reset(skb);
  504. IPTUNNEL_XMIT();
  505. tunnel->recursion--;
  506. return 0;
  507. tx_error_icmp:
  508. dst_link_failure(skb);
  509. tx_error:
  510. stats->tx_errors++;
  511. dev_kfree_skb(skb);
  512. tunnel->recursion--;
  513. return 0;
  514. }
  515. static int
  516. ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
  517. {
  518. int err = 0;
  519. struct ip_tunnel_parm p;
  520. struct ip_tunnel *t;
  521. switch (cmd) {
  522. case SIOCGETTUNNEL:
  523. t = NULL;
  524. if (dev == ipip6_fb_tunnel_dev) {
  525. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
  526. err = -EFAULT;
  527. break;
  528. }
  529. t = ipip6_tunnel_locate(&p, 0);
  530. }
  531. if (t == NULL)
  532. t = netdev_priv(dev);
  533. memcpy(&p, &t->parms, sizeof(p));
  534. if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
  535. err = -EFAULT;
  536. break;
  537. case SIOCADDTUNNEL:
  538. case SIOCCHGTUNNEL:
  539. err = -EPERM;
  540. if (!capable(CAP_NET_ADMIN))
  541. goto done;
  542. err = -EFAULT;
  543. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  544. goto done;
  545. err = -EINVAL;
  546. if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
  547. p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
  548. goto done;
  549. if (p.iph.ttl)
  550. p.iph.frag_off |= htons(IP_DF);
  551. t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
  552. if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
  553. if (t != NULL) {
  554. if (t->dev != dev) {
  555. err = -EEXIST;
  556. break;
  557. }
  558. } else {
  559. if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
  560. (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
  561. err = -EINVAL;
  562. break;
  563. }
  564. t = netdev_priv(dev);
  565. ipip6_tunnel_unlink(t);
  566. t->parms.iph.saddr = p.iph.saddr;
  567. t->parms.iph.daddr = p.iph.daddr;
  568. memcpy(dev->dev_addr, &p.iph.saddr, 4);
  569. memcpy(dev->broadcast, &p.iph.daddr, 4);
  570. ipip6_tunnel_link(t);
  571. netdev_state_change(dev);
  572. }
  573. }
  574. if (t) {
  575. err = 0;
  576. if (cmd == SIOCCHGTUNNEL) {
  577. t->parms.iph.ttl = p.iph.ttl;
  578. t->parms.iph.tos = p.iph.tos;
  579. }
  580. if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
  581. err = -EFAULT;
  582. } else
  583. err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
  584. break;
  585. case SIOCDELTUNNEL:
  586. err = -EPERM;
  587. if (!capable(CAP_NET_ADMIN))
  588. goto done;
  589. if (dev == ipip6_fb_tunnel_dev) {
  590. err = -EFAULT;
  591. if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
  592. goto done;
  593. err = -ENOENT;
  594. if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
  595. goto done;
  596. err = -EPERM;
  597. if (t == netdev_priv(ipip6_fb_tunnel_dev))
  598. goto done;
  599. dev = t->dev;
  600. }
  601. unregister_netdevice(dev);
  602. err = 0;
  603. break;
  604. default:
  605. err = -EINVAL;
  606. }
  607. done:
  608. return err;
  609. }
  610. static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
  611. {
  612. return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
  613. }
  614. static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
  615. {
  616. if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
  617. return -EINVAL;
  618. dev->mtu = new_mtu;
  619. return 0;
  620. }
  621. static void ipip6_tunnel_setup(struct net_device *dev)
  622. {
  623. SET_MODULE_OWNER(dev);
  624. dev->uninit = ipip6_tunnel_uninit;
  625. dev->destructor = free_netdev;
  626. dev->hard_start_xmit = ipip6_tunnel_xmit;
  627. dev->get_stats = ipip6_tunnel_get_stats;
  628. dev->do_ioctl = ipip6_tunnel_ioctl;
  629. dev->change_mtu = ipip6_tunnel_change_mtu;
  630. dev->type = ARPHRD_SIT;
  631. dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
  632. dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
  633. dev->flags = IFF_NOARP;
  634. dev->iflink = 0;
  635. dev->addr_len = 4;
  636. }
  637. static int ipip6_tunnel_init(struct net_device *dev)
  638. {
  639. struct net_device *tdev = NULL;
  640. struct ip_tunnel *tunnel;
  641. struct iphdr *iph;
  642. tunnel = netdev_priv(dev);
  643. iph = &tunnel->parms.iph;
  644. tunnel->dev = dev;
  645. strcpy(tunnel->parms.name, dev->name);
  646. memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
  647. memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
  648. if (iph->daddr) {
  649. struct flowi fl = { .nl_u = { .ip4_u =
  650. { .daddr = iph->daddr,
  651. .saddr = iph->saddr,
  652. .tos = RT_TOS(iph->tos) } },
  653. .oif = tunnel->parms.link,
  654. .proto = IPPROTO_IPV6 };
  655. struct rtable *rt;
  656. if (!ip_route_output_key(&rt, &fl)) {
  657. tdev = rt->u.dst.dev;
  658. ip_rt_put(rt);
  659. }
  660. dev->flags |= IFF_POINTOPOINT;
  661. }
  662. if (!tdev && tunnel->parms.link)
  663. tdev = __dev_get_by_index(tunnel->parms.link);
  664. if (tdev) {
  665. dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
  666. dev->mtu = tdev->mtu - sizeof(struct iphdr);
  667. if (dev->mtu < IPV6_MIN_MTU)
  668. dev->mtu = IPV6_MIN_MTU;
  669. }
  670. dev->iflink = tunnel->parms.link;
  671. return 0;
  672. }
  673. static int __init ipip6_fb_tunnel_init(struct net_device *dev)
  674. {
  675. struct ip_tunnel *tunnel = netdev_priv(dev);
  676. struct iphdr *iph = &tunnel->parms.iph;
  677. tunnel->dev = dev;
  678. strcpy(tunnel->parms.name, dev->name);
  679. iph->version = 4;
  680. iph->protocol = IPPROTO_IPV6;
  681. iph->ihl = 5;
  682. iph->ttl = 64;
  683. dev_hold(dev);
  684. tunnels_wc[0] = tunnel;
  685. return 0;
  686. }
  687. static struct xfrm_tunnel sit_handler = {
  688. .handler = ipip6_rcv,
  689. .err_handler = ipip6_err,
  690. .priority = 1,
  691. };
  692. static void __exit sit_destroy_tunnels(void)
  693. {
  694. int prio;
  695. for (prio = 1; prio < 4; prio++) {
  696. int h;
  697. for (h = 0; h < HASH_SIZE; h++) {
  698. struct ip_tunnel *t;
  699. while ((t = tunnels[prio][h]) != NULL)
  700. unregister_netdevice(t->dev);
  701. }
  702. }
  703. }
  704. static void __exit sit_cleanup(void)
  705. {
  706. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  707. rtnl_lock();
  708. sit_destroy_tunnels();
  709. unregister_netdevice(ipip6_fb_tunnel_dev);
  710. rtnl_unlock();
  711. }
  712. static int __init sit_init(void)
  713. {
  714. int err;
  715. printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
  716. if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) {
  717. printk(KERN_INFO "sit init: Can't add protocol\n");
  718. return -EAGAIN;
  719. }
  720. ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
  721. ipip6_tunnel_setup);
  722. if (!ipip6_fb_tunnel_dev) {
  723. err = -ENOMEM;
  724. goto err1;
  725. }
  726. ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
  727. if ((err = register_netdev(ipip6_fb_tunnel_dev)))
  728. goto err2;
  729. out:
  730. return err;
  731. err2:
  732. free_netdev(ipip6_fb_tunnel_dev);
  733. err1:
  734. xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
  735. goto out;
  736. }
  737. module_init(sit_init);
  738. module_exit(sit_cleanup);
  739. MODULE_LICENSE("GPL");
  740. MODULE_ALIAS("sit0");