netns xfrm: ipcomp support
[safe/jmp/linux-2.6] / net / ipv4 / ip_options.c
index 9f02917..94bf105 100644 (file)
@@ -5,10 +5,8 @@
  *
  *             The options processing module for ip.c
  *
- * Version:    $Id: ip_options.c,v 1.21 2001/09/01 00:31:50 davem Exp $
- *
  * Authors:    A.N.Kuznetsov
- *             
+ *
  */
 
 #include <linux/capability.h>
@@ -26,7 +24,7 @@
 #include <net/route.h>
 #include <net/cipso_ipv4.h>
 
-/* 
+/*
  * Write options to IP header, record destination address to
  * source route option, address of outgoing interface
  * (we should already know it, so that this  function is allowed be
 void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
                            __be32 daddr, struct rtable *rt, int is_frag)
 {
-       unsigned char * iph = skb->nh.raw;
+       unsigned char *iph = skb_network_header(skb);
 
        memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
        memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
        opt = &(IPCB(skb)->opt);
-       opt->is_data = 0;
 
        if (opt->srr)
                memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);
@@ -56,10 +53,10 @@ void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
                if (opt->ts_needaddr)
                        ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, rt);
                if (opt->ts_needtime) {
-                       struct timeval tv;
+                       struct timespec tv;
                        __be32 midtime;
-                       do_gettimeofday(&tv);
-                       midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
+                       getnstimeofday(&tv);
+                       midtime = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC);
                        memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
                }
                return;
@@ -76,7 +73,7 @@ void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
        }
 }
 
-/* 
+/*
  * Provided (sopt, skb) points to received options,
  * build in dopt compiled option set appropriate for answering.
  * i.e. invert SRR option, copy anothers,
@@ -85,7 +82,7 @@ void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
  * NOTE: dopt cannot point to skb.
  */
 
-int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb) 
+int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
 {
        struct ip_options *sopt;
        unsigned char *sptr, *dptr;
@@ -95,8 +92,6 @@ int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
 
        memset(dopt, 0, sizeof(struct ip_options));
 
-       dopt->is_data = 1;
-
        sopt = &(IPCB(skb)->opt);
 
        if (sopt->optlen == 0) {
@@ -104,13 +99,10 @@ int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
                return 0;
        }
 
-       sptr = skb->nh.raw;
+       sptr = skb_network_header(skb);
        dptr = dopt->__data;
 
-       if (skb->dst)
-               daddr = ((struct rtable*)skb->dst)->rt_spec_dst;
-       else
-               daddr = skb->nh.iph->daddr;
+       daddr = skb_rtable(skb)->rt_spec_dst;
 
        if (sopt->rr) {
                optlen  = sptr[sopt->rr+1];
@@ -151,7 +143,7 @@ int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
                                                __be32 addr;
 
                                                memcpy(&addr, sptr+soffset-1, 4);
-                                               if (inet_addr_type(addr) != RTN_LOCAL) {
+                                               if (inet_addr_type(dev_net(skb_dst(skb)->dev), addr) != RTN_LOCAL) {
                                                        dopt->ts_needtime = 1;
                                                        soffset += 8;
                                                }
@@ -180,7 +172,8 @@ int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
                        /*
                         * RFC1812 requires to fix illegal source routes.
                         */
-                       if (memcmp(&skb->nh.iph->saddr, &start[soffset+3], 4) == 0)
+                       if (memcmp(&ip_hdr(skb)->saddr,
+                                  &start[soffset + 3], 4) == 0)
                                doffset -= 4;
                }
                if (doffset > 3) {
@@ -215,9 +208,9 @@ int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
  *     Simple and stupid 8), but the most efficient way.
  */
 
-void ip_options_fragment(struct sk_buff * skb) 
+void ip_options_fragment(struct sk_buff * skb)
 {
-       unsigned char * optptr = skb->nh.raw + sizeof(struct iphdr);
+       unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr);
        struct ip_options * opt = &(IPCB(skb)->opt);
        int  l = opt->optlen;
        int  optlen;
@@ -253,25 +246,22 @@ void ip_options_fragment(struct sk_buff * skb)
  * If opt == NULL, then skb->data should point to IP header.
  */
 
-int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
+int ip_options_compile(struct net *net,
+                      struct ip_options * opt, struct sk_buff * skb)
 {
        int l;
        unsigned char * iph;
        unsigned char * optptr;
        int optlen;
        unsigned char * pp_ptr = NULL;
-       struct rtable *rt = skb ? (struct rtable*)skb->dst : NULL;
-
-       if (!opt) {
-               opt = &(IPCB(skb)->opt);
-               iph = skb->nh.raw;
-               opt->optlen = ((struct iphdr *)iph)->ihl*4 - sizeof(struct iphdr);
-               optptr = iph + sizeof(struct iphdr);
-               opt->is_data = 0;
-       } else {
-               optptr = opt->is_data ? opt->__data : (unsigned char*)&(skb->nh.iph[1]);
-               iph = optptr - sizeof(struct iphdr);
-       }
+       struct rtable *rt = NULL;
+
+       if (skb != NULL) {
+               rt = skb_rtable(skb);
+               optptr = (unsigned char *)&(ip_hdr(skb)[1]);
+       } else
+               optptr = opt->__data;
+       iph = optptr - sizeof(struct iphdr);
 
        for (l = opt->optlen; l > 0; ) {
                switch (*optptr) {
@@ -370,7 +360,7 @@ int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
                                switch (optptr[3]&0xF) {
                                      case IPOPT_TS_TSONLY:
                                        opt->ts = optptr - iph;
-                                       if (skb) 
+                                       if (skb)
                                                timeptr = (__be32*)&optptr[optptr[2]-1];
                                        opt->ts_needtime = 1;
                                        optptr[2] += 4;
@@ -398,7 +388,7 @@ int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
                                        {
                                                __be32 addr;
                                                memcpy(&addr, &optptr[optptr[2]-1], 4);
-                                               if (inet_addr_type(addr) == RTN_UNICAST)
+                                               if (inet_addr_type(net, addr) == RTN_UNICAST)
                                                        break;
                                                if (skb)
                                                        timeptr = (__be32*)&optptr[optptr[2]+3];
@@ -414,10 +404,10 @@ int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
                                        break;
                                }
                                if (timeptr) {
-                                       struct timeval tv;
+                                       struct timespec tv;
                                        __be32  midtime;
-                                       do_gettimeofday(&tv);
-                                       midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
+                                       getnstimeofday(&tv);
+                                       midtime = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC);
                                        memcpy(timeptr, &midtime, sizeof(__be32));
                                        opt->is_changed = 1;
                                }
@@ -448,7 +438,7 @@ int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
                                goto error;
                        }
                        opt->cipso = optptr - iph;
-                       if (cipso_v4_validate(&optptr)) {
+                       if (cipso_v4_validate(skb, &optptr)) {
                                pp_ptr = optptr;
                                goto error;
                        }
@@ -511,21 +501,17 @@ void ip_options_undo(struct ip_options * opt)
 
 static struct ip_options *ip_options_get_alloc(const int optlen)
 {
-       struct ip_options *opt = kmalloc(sizeof(*opt) + ((optlen + 3) & ~3),
-                                        GFP_KERNEL);
-       if (opt)
-               memset(opt, 0, sizeof(*opt));
-       return opt;
+       return kzalloc(sizeof(struct ip_options) + ((optlen + 3) & ~3),
+                      GFP_KERNEL);
 }
 
-static int ip_options_get_finish(struct ip_options **optp,
+static int ip_options_get_finish(struct net *net, struct ip_options **optp,
                                 struct ip_options *opt, int optlen)
 {
        while (optlen & 3)
                opt->__data[optlen++] = IPOPT_END;
        opt->optlen = optlen;
-       opt->is_data = 1;
-       if (optlen && ip_options_compile(opt, NULL)) {
+       if (optlen && ip_options_compile(net, opt, NULL)) {
                kfree(opt);
                return -EINVAL;
        }
@@ -534,7 +520,8 @@ static int ip_options_get_finish(struct ip_options **optp,
        return 0;
 }
 
-int ip_options_get_from_user(struct ip_options **optp, unsigned char __user *data, int optlen)
+int ip_options_get_from_user(struct net *net, struct ip_options **optp,
+                            unsigned char __user *data, int optlen)
 {
        struct ip_options *opt = ip_options_get_alloc(optlen);
 
@@ -544,10 +531,11 @@ int ip_options_get_from_user(struct ip_options **optp, unsigned char __user *dat
                kfree(opt);
                return -EFAULT;
        }
-       return ip_options_get_finish(optp, opt, optlen);
+       return ip_options_get_finish(net, optp, opt, optlen);
 }
 
-int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen)
+int ip_options_get(struct net *net, struct ip_options **optp,
+                  unsigned char *data, int optlen)
 {
        struct ip_options *opt = ip_options_get_alloc(optlen);
 
@@ -555,15 +543,15 @@ int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen)
                return -ENOMEM;
        if (optlen)
                memcpy(opt->__data, data, optlen);
-       return ip_options_get_finish(optp, opt, optlen);
+       return ip_options_get_finish(net, optp, opt, optlen);
 }
 
 void ip_forward_options(struct sk_buff *skb)
 {
        struct   ip_options * opt       = &(IPCB(skb)->opt);
        unsigned char * optptr;
-       struct rtable *rt = (struct rtable*)skb->dst;
-       unsigned char *raw = skb->nh.raw;
+       struct rtable *rt = skb_rtable(skb);
+       unsigned char *raw = skb_network_header(skb);
 
        if (opt->rr_needaddr) {
                optptr = (unsigned char *)raw + opt->rr;
@@ -587,7 +575,7 @@ void ip_forward_options(struct sk_buff *skb)
                if (srrptr + 3 <= srrspace) {
                        opt->is_changed = 1;
                        ip_rt_get_source(&optptr[srrptr-1], rt);
-                       skb->nh.iph->daddr = rt->rt_dst;
+                       ip_hdr(skb)->daddr = rt->rt_dst;
                        optptr[2] = srrptr+4;
                } else if (net_ratelimit())
                        printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
@@ -599,7 +587,7 @@ void ip_forward_options(struct sk_buff *skb)
        }
        if (opt->is_changed) {
                opt->is_changed = 0;
-               ip_send_check(skb->nh.iph);
+               ip_send_check(ip_hdr(skb));
        }
 }
 
@@ -608,9 +596,9 @@ int ip_options_rcv_srr(struct sk_buff *skb)
        struct ip_options *opt = &(IPCB(skb)->opt);
        int srrspace, srrptr;
        __be32 nexthop;
-       struct iphdr *iph = skb->nh.iph;
-       unsigned char * optptr = skb->nh.raw + opt->srr;
-       struct rtable *rt = (struct rtable*)skb->dst;
+       struct iphdr *iph = ip_hdr(skb);
+       unsigned char *optptr = skb_network_header(skb) + opt->srr;
+       struct rtable *rt = skb_rtable(skb);
        struct rtable *rt2;
        int err;
 
@@ -635,13 +623,13 @@ int ip_options_rcv_srr(struct sk_buff *skb)
                }
                memcpy(&nexthop, &optptr[srrptr-1], 4);
 
-               rt = (struct rtable*)skb->dst;
-               skb->dst = NULL;
+               rt = skb_rtable(skb);
+               skb_dst_set(skb, NULL);
                err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
-               rt2 = (struct rtable*)skb->dst;
+               rt2 = skb_rtable(skb);
                if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
                        ip_rt_put(rt2);
-                       skb->dst = &rt->u.dst;
+                       skb_dst_set(skb, &rt->u.dst);
                        return -EINVAL;
                }
                ip_rt_put(rt);