tcp: add helper for AI algorithm
[safe/jmp/linux-2.6] / net / ipv4 / ipip.c
index 9c2939b..9054139 100644 (file)
@@ -1,8 +1,6 @@
 /*
  *     Linux NET3:     IP/IP protocol decoder.
  *
- *     Version: $Id: ipip.c,v 1.50 2001/10/02 02:22:36 davem Exp $
- *
  *     Authors:
  *             Sam Lantinga (slouken@cs.ucdavis.edu)  02/01/95
  *
@@ -43,7 +41,7 @@
                Made the tunnels use dev->name not tunnel: when error reporting.
                Added tx_dropped stat
 
-               -Alan Cox       (Alan.Cox@linux.org) 21 March 95
+               -Alan Cox       (alan@lxorguk.ukuu.org.uk) 21 March 95
 
        Reworked:
                Changed to tunnel to destination gateway in addition to the
@@ -132,8 +130,8 @@ struct ipip_net {
        struct net_device *fb_tunnel_dev;
 };
 
-static int ipip_fb_tunnel_init(struct net_device *dev);
-static int ipip_tunnel_init(struct net_device *dev);
+static void ipip_fb_tunnel_init(struct net_device *dev);
+static void ipip_tunnel_init(struct net_device *dev);
 static void ipip_tunnel_setup(struct net_device *dev);
 
 static DEFINE_RWLOCK(ipip_lock);
@@ -239,15 +237,18 @@ static struct ip_tunnel * ipip_tunnel_locate(struct net *net,
        if (dev == NULL)
                return NULL;
 
+       dev_net_set(dev, net);
+
        if (strchr(name, '%')) {
                if (dev_alloc_name(dev, name) < 0)
                        goto failed_free;
        }
 
        nt = netdev_priv(dev);
-       dev->init = ipip_tunnel_init;
        nt->parms = *parms;
 
+       ipip_tunnel_init(dev);
+
        if (register_netdevice(dev) < 0)
                goto failed_free;
 
@@ -276,13 +277,12 @@ static void ipip_tunnel_uninit(struct net_device *dev)
 
 static int ipip_err(struct sk_buff *skb, u32 info)
 {
-#ifndef I_WISH_WORLD_WERE_PERFECT
 
-/* It is not :-( All the routers (except for Linux) return only
+/* All the routers (except for Linux) return only
    8 bytes of packet payload. It means, that precise relaying of
    ICMP in the real Internet is absolutely infeasible.
  */
-       struct iphdr *iph = (struct iphdr*)skb->data;
+       struct iphdr *iph = (struct iphdr *)skb->data;
        const int type = icmp_hdr(skb)->type;
        const int code = icmp_hdr(skb)->code;
        struct ip_tunnel *t;
@@ -327,7 +327,7 @@ static int ipip_err(struct sk_buff *skb, u32 info)
        if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
                goto out;
 
-       if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
+       if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
                t->err_count++;
        else
                t->err_count = 1;
@@ -335,133 +335,6 @@ static int ipip_err(struct sk_buff *skb, u32 info)
 out:
        read_unlock(&ipip_lock);
        return err;
-#else
-       struct iphdr *iph = (struct iphdr*)dp;
-       int hlen = iph->ihl<<2;
-       struct iphdr *eiph;
-       const int type = icmp_hdr(skb)->type;
-       const int code = icmp_hdr(skb)->code;
-       int rel_type = 0;
-       int rel_code = 0;
-       __be32 rel_info = 0;
-       __u32 n = 0;
-       struct sk_buff *skb2;
-       struct flowi fl;
-       struct rtable *rt;
-
-       if (len < hlen + sizeof(struct iphdr))
-               return 0;
-       eiph = (struct iphdr*)(dp + hlen);
-
-       switch (type) {
-       default:
-               return 0;
-       case ICMP_PARAMETERPROB:
-               n = ntohl(icmp_hdr(skb)->un.gateway) >> 24;
-               if (n < hlen)
-                       return 0;
-
-               /* So... This guy found something strange INSIDE encapsulated
-                  packet. Well, he is fool, but what can we do ?
-                */
-               rel_type = ICMP_PARAMETERPROB;
-               rel_info = htonl((n - hlen) << 24);
-               break;
-
-       case ICMP_DEST_UNREACH:
-               switch (code) {
-               case ICMP_SR_FAILED:
-               case ICMP_PORT_UNREACH:
-                       /* Impossible event. */
-                       return 0;
-               case ICMP_FRAG_NEEDED:
-                       /* And it is the only really necessary thing :-) */
-                       n = ntohs(icmp_hdr(skb)->un.frag.mtu);
-                       if (n < hlen+68)
-                               return 0;
-                       n -= hlen;
-                       /* BSD 4.2 MORE DOES NOT EXIST IN NATURE. */
-                       if (n > ntohs(eiph->tot_len))
-                               return 0;
-                       rel_info = htonl(n);
-                       break;
-               default:
-                       /* All others are translated to HOST_UNREACH.
-                          rfc2003 contains "deep thoughts" about NET_UNREACH,
-                          I believe, it is just ether pollution. --ANK
-                        */
-                       rel_type = ICMP_DEST_UNREACH;
-                       rel_code = ICMP_HOST_UNREACH;
-                       break;
-               }
-               break;
-       case ICMP_TIME_EXCEEDED:
-               if (code != ICMP_EXC_TTL)
-                       return 0;
-               break;
-       }
-
-       /* Prepare fake skb to feed it to icmp_send */
-       skb2 = skb_clone(skb, GFP_ATOMIC);
-       if (skb2 == NULL)
-               return 0;
-       dst_release(skb2->dst);
-       skb2->dst = NULL;
-       skb_pull(skb2, skb->data - (u8*)eiph);
-       skb_reset_network_header(skb2);
-
-       /* Try to guess incoming interface */
-       memset(&fl, 0, sizeof(fl));
-       fl.fl4_daddr = eiph->saddr;
-       fl.fl4_tos = RT_TOS(eiph->tos);
-       fl.proto = IPPROTO_IPIP;
-       if (ip_route_output_key(&init_net, &rt, &key)) {
-               kfree_skb(skb2);
-               return 0;
-       }
-       skb2->dev = rt->u.dst.dev;
-
-       /* route "incoming" packet */
-       if (rt->rt_flags&RTCF_LOCAL) {
-               ip_rt_put(rt);
-               rt = NULL;
-               fl.fl4_daddr = eiph->daddr;
-               fl.fl4_src = eiph->saddr;
-               fl.fl4_tos = eiph->tos;
-               if (ip_route_output_key(&init_net, &rt, &fl) ||
-                   rt->u.dst.dev->type != ARPHRD_TUNNEL) {
-                       ip_rt_put(rt);
-                       kfree_skb(skb2);
-                       return 0;
-               }
-       } else {
-               ip_rt_put(rt);
-               if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, skb2->dev) ||
-                   skb2->dst->dev->type != ARPHRD_TUNNEL) {
-                       kfree_skb(skb2);
-                       return 0;
-               }
-       }
-
-       /* change mtu on this route */
-       if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
-               if (n > dst_mtu(skb2->dst)) {
-                       kfree_skb(skb2);
-                       return 0;
-               }
-               skb2->dst->ops->update_pmtu(skb2->dst, n);
-       } else if (type == ICMP_TIME_EXCEEDED) {
-               struct ip_tunnel *t = netdev_priv(skb2->dev);
-               if (t->parms.iph.ttl) {
-                       rel_type = ICMP_DEST_UNREACH;
-                       rel_code = ICMP_HOST_UNREACH;
-               }
-       }
-
-       icmp_send(skb2, rel_type, rel_code, rel_info);
-       kfree_skb(skb2);
-       return 0;
-#endif
 }
 
 static inline void ipip_ecn_decapsulate(const struct iphdr *outer_iph,
@@ -494,8 +367,8 @@ static int ipip_rcv(struct sk_buff *skb)
                skb->protocol = htons(ETH_P_IP);
                skb->pkt_type = PACKET_HOST;
 
-               tunnel->stat.rx_packets++;
-               tunnel->stat.rx_bytes += skb->len;
+               tunnel->dev->stats.rx_packets++;
+               tunnel->dev->stats.rx_bytes += skb->len;
                skb->dev = tunnel->dev;
                dst_release(skb->dst);
                skb->dst = NULL;
@@ -518,7 +391,7 @@ static int ipip_rcv(struct sk_buff *skb)
 static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct ip_tunnel *tunnel = netdev_priv(dev);
-       struct net_device_stats *stats = &tunnel->stat;
+       struct net_device_stats *stats = &tunnel->dev->stats;
        struct iphdr  *tiph = &tunnel->parms.iph;
        u8     tos = tunnel->parms.iph.tos;
        __be16 df = tiph->frag_off;
@@ -531,7 +404,7 @@ static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
        int    mtu;
 
        if (tunnel->recursion++) {
-               tunnel->stat.collisions++;
+               stats->collisions++;
                goto tx_error;
        }
 
@@ -544,7 +417,7 @@ static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
        if (!dst) {
                /* NBMA tunnel */
                if ((rt = skb->rtable) == NULL) {
-                       tunnel->stat.tx_fifo_errors++;
+                       stats->tx_fifo_errors++;
                        goto tx_error;
                }
                if ((dst = rt->rt_gateway) == 0)
@@ -558,8 +431,8 @@ static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                                                .saddr = tiph->saddr,
                                                .tos = RT_TOS(tos) } },
                                    .proto = IPPROTO_IPIP };
-               if (ip_route_output_key(&init_net, &rt, &fl)) {
-                       tunnel->stat.tx_carrier_errors++;
+               if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
+                       stats->tx_carrier_errors++;
                        goto tx_error_icmp;
                }
        }
@@ -567,7 +440,7 @@ static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
 
        if (tdev == dev) {
                ip_rt_put(rt);
-               tunnel->stat.collisions++;
+               stats->collisions++;
                goto tx_error;
        }
 
@@ -577,7 +450,7 @@ static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
                mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
 
        if (mtu < 68) {
-               tunnel->stat.collisions++;
+               stats->collisions++;
                ip_rt_put(rt);
                goto tx_error;
        }
@@ -593,7 +466,8 @@ static int ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
        }
 
        if (tunnel->err_count > 0) {
-               if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
+               if (time_before(jiffies,
+                               tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
                        tunnel->err_count--;
                        dst_link_failure(skb);
                } else
@@ -679,7 +553,7 @@ static void ipip_tunnel_bind_dev(struct net_device *dev)
                                                .tos = RT_TOS(iph->tos) } },
                                    .proto = IPPROTO_IPIP };
                struct rtable *rt;
-               if (!ip_route_output_key(&init_net, &rt, &fl)) {
+               if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
                        tdev = rt->u.dst.dev;
                        ip_rt_put(rt);
                }
@@ -687,7 +561,7 @@ static void ipip_tunnel_bind_dev(struct net_device *dev)
        }
 
        if (!tdev && tunnel->parms.link)
-               tdev = __dev_get_by_index(&init_net, tunnel->parms.link);
+               tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
 
        if (tdev) {
                dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
@@ -811,11 +685,6 @@ done:
        return err;
 }
 
-static struct net_device_stats *ipip_tunnel_get_stats(struct net_device *dev)
-{
-       return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
-}
-
 static int ipip_tunnel_change_mtu(struct net_device *dev, int new_mtu)
 {
        if (new_mtu < 68 || new_mtu > 0xFFF8 - sizeof(struct iphdr))
@@ -824,13 +693,17 @@ static int ipip_tunnel_change_mtu(struct net_device *dev, int new_mtu)
        return 0;
 }
 
+static const struct net_device_ops ipip_netdev_ops = {
+       .ndo_uninit     = ipip_tunnel_uninit,
+       .ndo_start_xmit = ipip_tunnel_xmit,
+       .ndo_do_ioctl   = ipip_tunnel_ioctl,
+       .ndo_change_mtu = ipip_tunnel_change_mtu,
+
+};
+
 static void ipip_tunnel_setup(struct net_device *dev)
 {
-       dev->uninit             = ipip_tunnel_uninit;
-       dev->hard_start_xmit    = ipip_tunnel_xmit;
-       dev->get_stats          = ipip_tunnel_get_stats;
-       dev->do_ioctl           = ipip_tunnel_ioctl;
-       dev->change_mtu         = ipip_tunnel_change_mtu;
+       dev->netdev_ops         = &ipip_netdev_ops;
        dev->destructor         = free_netdev;
 
        dev->type               = ARPHRD_TUNNEL;
@@ -839,13 +712,12 @@ static void ipip_tunnel_setup(struct net_device *dev)
        dev->flags              = IFF_NOARP;
        dev->iflink             = 0;
        dev->addr_len           = 4;
+       dev->features           |= NETIF_F_NETNS_LOCAL;
 }
 
-static int ipip_tunnel_init(struct net_device *dev)
+static void ipip_tunnel_init(struct net_device *dev)
 {
-       struct ip_tunnel *tunnel;
-
-       tunnel = netdev_priv(dev);
+       struct ip_tunnel *tunnel = netdev_priv(dev);
 
        tunnel->dev = dev;
        strcpy(tunnel->parms.name, dev->name);
@@ -854,11 +726,9 @@ static int ipip_tunnel_init(struct net_device *dev)
        memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
 
        ipip_tunnel_bind_dev(dev);
-
-       return 0;
 }
 
-static int ipip_fb_tunnel_init(struct net_device *dev)
+static void ipip_fb_tunnel_init(struct net_device *dev)
 {
        struct ip_tunnel *tunnel = netdev_priv(dev);
        struct iphdr *iph = &tunnel->parms.iph;
@@ -873,7 +743,6 @@ static int ipip_fb_tunnel_init(struct net_device *dev)
 
        dev_hold(dev);
        ipn->tunnels_wc[0]      = tunnel;
-       return 0;
 }
 
 static struct xfrm_tunnel ipip_handler = {
@@ -882,7 +751,7 @@ static struct xfrm_tunnel ipip_handler = {
        .priority       =       1,
 };
 
-static char banner[] __initdata =
+static const char banner[] __initconst =
        KERN_INFO "IPv4 over IPv4 tunneling driver\n";
 
 static void ipip_destroy_tunnels(struct ipip_net *ipn)
@@ -925,10 +794,10 @@ static int ipip_init_net(struct net *net)
                err = -ENOMEM;
                goto err_alloc_dev;
        }
-
-       ipn->fb_tunnel_dev->init = ipip_fb_tunnel_init;
        dev_net_set(ipn->fb_tunnel_dev, net);
 
+       ipip_fb_tunnel_init(ipn->fb_tunnel_dev);
+
        if ((err = register_netdev(ipn->fb_tunnel_dev)))
                goto err_reg_dev;