xfrm: merge dstopt and routing hdroff functions
authorFlorian Westphal <fw@strlen.de>
Fri, 11 Jun 2021 10:50:14 +0000 (12:50 +0200)
committerSteffen Klassert <steffen.klassert@secunet.com>
Fri, 11 Jun 2021 12:48:51 +0000 (14:48 +0200)
Both functions are very similar, so merge them into one.

The nexthdr is passed as argument to break the loop in the
ROUTING case, this is the only header type where slightly different
rules apply.

While at it, the merged function is realigned with
ip6_find_1stfragopt().  That function received bug fixes for an infinite
loop, but neither dstopt nor rh parsing functions (copy-pasted from
ip6_find_1stfragopt) were changed.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>
net/xfrm/xfrm_output.c

index 10842d5cf6e1642e1ec6c4fc28573a4b2c7de956..e14fca1fb00333765e36f941b5df13b268cc4dc8 100644 (file)
@@ -78,24 +78,30 @@ static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
 }
 
 #if IS_ENABLED(CONFIG_IPV6_MIP6)
-static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
-                              u8 **nexthdr)
+static int mip6_rthdr_offset(struct sk_buff *skb, u8 **nexthdr, int type)
 {
-       u16 offset = sizeof(struct ipv6hdr);
-       struct ipv6_opt_hdr *exthdr =
-                                  (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
        const unsigned char *nh = skb_network_header(skb);
-       unsigned int packet_len = skb_tail_pointer(skb) -
-               skb_network_header(skb);
+       unsigned int offset = sizeof(struct ipv6hdr);
+       unsigned int packet_len;
        int found_rhdr = 0;
 
+       packet_len = skb_tail_pointer(skb) - nh;
        *nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-       while (offset + 1 <= packet_len) {
+       while (offset <= packet_len) {
+               struct ipv6_opt_hdr *exthdr;
+
                switch (**nexthdr) {
                case NEXTHDR_HOP:
                        break;
                case NEXTHDR_ROUTING:
+                       if (type == IPPROTO_ROUTING && offset + 3 <= packet_len) {
+                               struct ipv6_rt_hdr *rt;
+
+                               rt = (struct ipv6_rt_hdr *)(nh + offset);
+                               if (rt->type != 0)
+                                       return offset;
+                       }
                        found_rhdr = 1;
                        break;
                case NEXTHDR_DEST:
@@ -116,59 +122,18 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                        return offset;
                }
 
-               offset += ipv6_optlen(exthdr);
-               *nexthdr = &exthdr->nexthdr;
-               exthdr = (struct ipv6_opt_hdr *)(nh + offset);
-       }
-
-       return offset;
-}
-
-static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
-                            u8 **nexthdr)
-{
-       u16 offset = sizeof(struct ipv6hdr);
-       struct ipv6_opt_hdr *exthdr =
-                                  (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
-       const unsigned char *nh = skb_network_header(skb);
-       unsigned int packet_len = skb_tail_pointer(skb) -
-               skb_network_header(skb);
-       int found_rhdr = 0;
-
-       *nexthdr = &ipv6_hdr(skb)->nexthdr;
-
-       while (offset + 1 <= packet_len) {
-               switch (**nexthdr) {
-               case NEXTHDR_HOP:
-                       break;
-               case NEXTHDR_ROUTING:
-                       if (offset + 3 <= packet_len) {
-                               struct ipv6_rt_hdr *rt;
-
-                               rt = (struct ipv6_rt_hdr *)(nh + offset);
-                               if (rt->type != 0)
-                                       return offset;
-                       }
-                       found_rhdr = 1;
-                       break;
-               case NEXTHDR_DEST:
-                       if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
-                               return offset;
-
-                       if (found_rhdr)
-                               return offset;
-
-                       break;
-               default:
-                       return offset;
-               }
+               if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+                       return -EINVAL;
 
+               exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
+                                                offset);
                offset += ipv6_optlen(exthdr);
+               if (offset > IPV6_MAXPLEN)
+                       return -EINVAL;
                *nexthdr = &exthdr->nexthdr;
-               exthdr = (struct ipv6_opt_hdr *)(nh + offset);
        }
 
-       return offset;
+       return -EINVAL;
 }
 #endif
 
@@ -177,9 +142,8 @@ static int xfrm6_hdr_offset(struct xfrm_state *x, struct sk_buff *skb, u8 **prev
        switch (x->type->proto) {
 #if IS_ENABLED(CONFIG_IPV6_MIP6)
        case IPPROTO_DSTOPTS:
-               return mip6_destopt_offset(x, skb, prevhdr);
        case IPPROTO_ROUTING:
-               return mip6_rthdr_offset(x, skb, prevhdr);
+               return mip6_rthdr_offset(skb, prevhdr, x->type->proto);
 #endif
        default:
                break;