sit.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:19k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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