sit.c 19 KB

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