[IPV6]: Allow to replace skbuff by TLV parser.
[deliverable/linux.git] / net / ipv6 / exthdrs.c
index 9d0ee7f0eeb5296dfb44514295c1c5c53027fb59..1cdd0f0b5d34d28ed68bf32a4e2c3012c3852605 100644 (file)
 #include <net/ndisc.h>
 #include <net/ip6_route.h>
 #include <net/addrconf.h>
+#ifdef CONFIG_IPV6_MIP6
+#include <net/xfrm.h>
+#endif
 
 #include <asm/uaccess.h>
 
+int ipv6_find_tlv(struct sk_buff *skb, int offset, int type)
+{
+       int packet_len = skb->tail - skb->nh.raw;
+       struct ipv6_opt_hdr *hdr;
+       int len;
+
+       if (offset + 2 > packet_len)
+               goto bad;
+       hdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
+       len = ((hdr->hdrlen + 1) << 3);
+
+       if (offset + len > packet_len)
+               goto bad;
+
+       offset += 2;
+       len -= 2;
+
+       while (len > 0) {
+               int opttype = skb->nh.raw[offset];
+               int optlen;
+
+               if (opttype == type)
+                       return offset;
+
+               switch (opttype) {
+               case IPV6_TLV_PAD0:
+                       optlen = 1;
+                       break;
+               default:
+                       optlen = skb->nh.raw[offset + 1] + 2;
+                       if (optlen > len)
+                               goto bad;
+                       break;
+               }
+               offset += optlen;
+               len -= optlen;
+       }
+       /* not_found */
+       return -1;
+ bad:
+       return -1;
+}
+
 /*
  *     Parsing tlv encoded headers.
  *
 
 struct tlvtype_proc {
        int     type;
-       int     (*func)(struct sk_buff *skb, int offset);
+       int     (*func)(struct sk_buff **skbp, int offset);
 };
 
 /*********************
@@ -65,8 +111,10 @@ struct tlvtype_proc {
 
 /* An unknown option is detected, decide what to do */
 
-static int ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
+static int ip6_tlvopt_unknown(struct sk_buff **skbp, int optoff)
 {
+       struct sk_buff *skb = *skbp;
+
        switch ((skb->nh.raw[optoff] & 0xC0) >> 6) {
        case 0: /* ignore */
                return 1;
@@ -91,8 +139,9 @@ static int ip6_tlvopt_unknown(struct sk_buff *skb, int optoff)
 
 /* Parse tlv encoded option header (hop-by-hop or destination) */
 
-static int ip6_parse_tlv(struct tlvtype_proc *procs, struct sk_buff *skb)
+static int ip6_parse_tlv(struct tlvtype_proc *procs, struct sk_buff **skbp)
 {
+       struct sk_buff *skb = *skbp;
        struct tlvtype_proc *curr;
        int off = skb->h.raw - skb->nh.raw;
        int len = ((skb->h.raw[1]+1)<<3);
@@ -122,13 +171,13 @@ static int ip6_parse_tlv(struct tlvtype_proc *procs, struct sk_buff *skb)
                                        /* type specific length/alignment 
                                           checks will be performed in the 
                                           func(). */
-                                       if (curr->func(skb, off) == 0)
+                                       if (curr->func(skbp, off) == 0)
                                                return 0;
                                        break;
                                }
                        }
                        if (curr->type < 0) {
-                               if (ip6_tlvopt_unknown(skb, off) == 0)
+                               if (ip6_tlvopt_unknown(skbp, off) == 0)
                                        return 0;
                        }
                        break;
@@ -167,7 +216,8 @@ static int ipv6_destopt_rcv(struct sk_buff **skbp)
        opt->lastopt = skb->h.raw - skb->nh.raw;
        opt->dst1 = skb->h.raw - skb->nh.raw;
 
-       if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) {
+       if (ip6_parse_tlv(tlvprocdestopt_lst, skbp)) {
+               skb = *skbp;
                skb->h.raw += ((skb->h.raw[1]+1)<<3);
                opt->nhoff = opt->dst1;
                return 1;
@@ -219,7 +269,7 @@ static int ipv6_rthdr_rcv(struct sk_buff **skbp)
 {
        struct sk_buff *skb = *skbp;
        struct inet6_skb_parm *opt = IP6CB(skb);
-       struct in6_addr *addr;
+       struct in6_addr *addr = NULL;
        struct in6_addr daddr;
        int n, i;
 
@@ -244,6 +294,23 @@ static int ipv6_rthdr_rcv(struct sk_buff **skbp)
 
 looped_back:
        if (hdr->segments_left == 0) {
+               switch (hdr->type) {
+#ifdef CONFIG_IPV6_MIP6
+               case IPV6_SRCRT_TYPE_2:
+                       /* Silently discard type 2 header unless it was
+                        * processed by own
+                        */
+                       if (!addr) {
+                               IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
+                               kfree_skb(skb);
+                               return -1;
+                       }
+                       break;
+#endif
+               default:
+                       break;
+               }
+
                opt->lastopt = skb->h.raw - skb->nh.raw;
                opt->srcrt = skb->h.raw - skb->nh.raw;
                skb->h.raw += (hdr->hdrlen + 1) << 3;
@@ -253,17 +320,29 @@ looped_back:
                return 1;
        }
 
-       if (hdr->type != IPV6_SRCRT_TYPE_0) {
+       switch (hdr->type) {
+       case IPV6_SRCRT_TYPE_0:
+               if (hdr->hdrlen & 0x01) {
+                       IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
+                       icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, (&hdr->hdrlen) - skb->nh.raw);
+                       return -1;
+               }
+               break;
+#ifdef CONFIG_IPV6_MIP6
+       case IPV6_SRCRT_TYPE_2:
+               /* Silently discard invalid RTH type 2 */
+               if (hdr->hdrlen != 2 || hdr->segments_left != 1) {
+                       IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
+                       kfree_skb(skb);
+                       return -1;
+               }
+               break;
+#endif
+       default:
                IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
                icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, (&hdr->type) - skb->nh.raw);
                return -1;
        }
-       
-       if (hdr->hdrlen & 0x01) {
-               IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
-               icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, (&hdr->hdrlen) - skb->nh.raw);
-               return -1;
-       }
 
        /*
         *      This is the routing header forwarding algorithm from
@@ -294,7 +373,7 @@ looped_back:
                hdr = (struct ipv6_rt_hdr *) skb2->h.raw;
        }
 
-       if (skb->ip_summed == CHECKSUM_HW)
+       if (skb->ip_summed == CHECKSUM_COMPLETE)
                skb->ip_summed = CHECKSUM_NONE;
 
        i = n - --hdr->segments_left;
@@ -303,6 +382,27 @@ looped_back:
        addr = rthdr->addr;
        addr += i - 1;
 
+       switch (hdr->type) {
+#ifdef CONFIG_IPV6_MIP6
+       case IPV6_SRCRT_TYPE_2:
+               if (xfrm6_input_addr(skb, (xfrm_address_t *)addr,
+                                    (xfrm_address_t *)&skb->nh.ipv6h->saddr,
+                                    IPPROTO_ROUTING) < 0) {
+                       IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
+                       kfree_skb(skb);
+                       return -1;
+               }
+               if (!ipv6_chk_home_addr(addr)) {
+                       IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
+                       kfree_skb(skb);
+                       return -1;
+               }
+               break;
+#endif
+       default:
+               break;
+       }
+
        if (ipv6_addr_is_multicast(addr)) {
                IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
                kfree_skb(skb);
@@ -421,8 +521,10 @@ EXPORT_SYMBOL_GPL(ipv6_invert_rthdr);
 
 /* Router Alert as of RFC 2711 */
 
-static int ipv6_hop_ra(struct sk_buff *skb, int optoff)
+static int ipv6_hop_ra(struct sk_buff **skbp, int optoff)
 {
+       struct sk_buff *skb = *skbp;
+
        if (skb->nh.raw[optoff+1] == 2) {
                IP6CB(skb)->ra = optoff;
                return 1;
@@ -435,8 +537,9 @@ static int ipv6_hop_ra(struct sk_buff *skb, int optoff)
 
 /* Jumbo payload */
 
-static int ipv6_hop_jumbo(struct sk_buff *skb, int optoff)
+static int ipv6_hop_jumbo(struct sk_buff **skbp, int optoff)
 {
+       struct sk_buff *skb = *skbp;
        u32 pkt_len;
 
        if (skb->nh.raw[optoff+1] != 4 || (optoff&3) != 2) {
@@ -485,8 +588,9 @@ static struct tlvtype_proc tlvprochopopt_lst[] = {
        { -1, }
 };
 
-int ipv6_parse_hopopts(struct sk_buff *skb)
+int ipv6_parse_hopopts(struct sk_buff **skbp)
 {
+       struct sk_buff *skb = *skbp;
        struct inet6_skb_parm *opt = IP6CB(skb);
 
        /*
@@ -502,7 +606,8 @@ int ipv6_parse_hopopts(struct sk_buff *skb)
        }
 
        opt->hop = sizeof(struct ipv6hdr);
-       if (ip6_parse_tlv(tlvprochopopt_lst, skb)) {
+       if (ip6_parse_tlv(tlvprochopopt_lst, skbp)) {
+               skb = *skbp;
                skb->h.raw += (skb->h.raw[1]+1)<<3;
                opt->nhoff = sizeof(struct ipv6hdr);
                return 1;
@@ -635,14 +740,17 @@ ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
        struct ipv6_txoptions *opt2;
        int err;
 
-       if (newtype != IPV6_HOPOPTS && opt->hopopt)
-               tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
-       if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
-               tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
-       if (newtype != IPV6_RTHDR && opt->srcrt)
-               tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
-       if (newtype != IPV6_DSTOPTS && opt->dst1opt)
-               tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
+       if (opt) {
+               if (newtype != IPV6_HOPOPTS && opt->hopopt)
+                       tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
+               if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
+                       tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
+               if (newtype != IPV6_RTHDR && opt->srcrt)
+                       tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
+               if (newtype != IPV6_DSTOPTS && opt->dst1opt)
+                       tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
+       }
+
        if (newopt && newoptlen)
                tot_len += CMSG_ALIGN(newoptlen);
 
@@ -659,25 +767,25 @@ ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
        opt2->tot_len = tot_len;
        p = (char *)(opt2 + 1);
 
-       err = ipv6_renew_option(opt->hopopt, newopt, newoptlen,
+       err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
                                newtype != IPV6_HOPOPTS,
                                &opt2->hopopt, &p);
        if (err)
                goto out;
 
-       err = ipv6_renew_option(opt->dst0opt, newopt, newoptlen,
+       err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
                                newtype != IPV6_RTHDRDSTOPTS,
                                &opt2->dst0opt, &p);
        if (err)
                goto out;
 
-       err = ipv6_renew_option(opt->srcrt, newopt, newoptlen,
+       err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
                                newtype != IPV6_RTHDR,
-                               (struct ipv6_opt_hdr **)opt2->srcrt, &p);
+                               (struct ipv6_opt_hdr **)&opt2->srcrt, &p);
        if (err)
                goto out;
 
-       err = ipv6_renew_option(opt->dst1opt, newopt, newoptlen,
+       err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
                                newtype != IPV6_DSTOPTS,
                                &opt2->dst1opt, &p);
        if (err)
This page took 0.031832 seconds and 5 git commands to generate.