Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/net/npf Fix two consecutive mistakes.



details:   https://anonhg.NetBSD.org/src/rev/25c53290a27f
branches:  trunk
changeset: 321363:25c53290a27f
user:      maxv <maxv%NetBSD.org@localhost>
date:      Tue Mar 13 09:04:02 2018 +0000

description:
Fix two consecutive mistakes.

The first mistake was npf_inet.c rev1.37:

        "Don't reassemble ipv6 fragments, instead treat the first fragment
        as a regular packet (subject to filtering rules), and pass
        subsequent fragments in the same group unconditionally."

Doing this was entirely wrong, because then a packet just had to push
the L4 payload in a secondary fragment, and NPF wouldn't apply rules on
it - meaning any IPv6 packet could bypass >=L4 filtering. This mistake
was supposed to be a fix for the second mistake.

The second mistake was that ip6_reass_packet (in npf_reassembly) was
getting called with npc->npc_hlen. But npc_hlen pointed to the last
encountered header in the IPv6 chain, which was not necessarily the
fragment header. So ip6_reass_packet was given garbage, and would fail,
resulting in the packet getting kicked. So basically IPv6 was broken by
NPF.

The first mistake is reverted, and the second one is fixed by doing:

-                       hlen = sizeof(struct ip6_frag);
+                       hlen = 0;

Now the iteration stops on the fragment header, and the call to
ip6_reass_packet is valid.

My npf_inet.c rev1.38 is partially reverted: we don't need to worry
about failing properly to advance; once the packet is reassembled
npf_cache_ip gets called again, and this time the whole chain should be
there.

Tested with a simple UDPv6 server - send a 3000-byte-sized buffer, the
packet gets correctly reassembled by NPF now.

diffstat:

 sys/net/npf/npf_handler.c |  13 +++----------
 sys/net/npf/npf_inet.c    |  44 ++++----------------------------------------
 2 files changed, 7 insertions(+), 50 deletions(-)

diffs (125 lines):

diff -r 1ec5dda82a36 -r 25c53290a27f sys/net/npf/npf_handler.c
--- a/sys/net/npf/npf_handler.c Tue Mar 13 06:41:53 2018 +0000
+++ b/sys/net/npf/npf_handler.c Tue Mar 13 09:04:02 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_handler.c,v 1.38 2018/03/08 07:06:13 maxv Exp $    */
+/*     $NetBSD: npf_handler.c,v 1.39 2018/03/13 09:04:02 maxv Exp $    */
 
 /*-
  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #ifdef _KERNEL
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.38 2018/03/08 07:06:13 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.39 2018/03/13 09:04:02 maxv Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -166,14 +166,7 @@
        /* Determine whether it is an IP fragment. */
        if (__predict_false(flags & NPC_IPFRAG)) {
                /*
-                * We pass IPv6 fragments unconditionally
-                * The first IPv6 fragment is not marked as such
-                * and passes through the filter
-                */
-               if (flags & NPC_IP6)
-                       return 0;
-               /*
-                * Pass to IPv4 reassembly mechanism.
+                * Pass to IPv4/IPv6 reassembly mechanism.
                 */
                error = npf_reassembly(npf, &npc, mp);
                if (error) {
diff -r 1ec5dda82a36 -r 25c53290a27f sys/net/npf/npf_inet.c
--- a/sys/net/npf/npf_inet.c    Tue Mar 13 06:41:53 2018 +0000
+++ b/sys/net/npf/npf_inet.c    Tue Mar 13 09:04:02 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_inet.c,v 1.39 2018/03/08 07:54:14 maxv Exp $       */
+/*     $NetBSD: npf_inet.c,v 1.40 2018/03/13 09:04:02 maxv Exp $       */
 
 /*-
  * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 
 #ifdef _KERNEL
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.39 2018/03/08 07:54:14 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.40 2018/03/13 09:04:02 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -358,9 +358,6 @@
                struct ip6_frag *ip6f;
                size_t off, hlen;
                int frag_present;
-               bool is_frag;
-               uint8_t onxt;
-               int fragoff;
 
                ip6 = nbuf_ensure_contig(nbuf, sizeof(struct ip6_hdr));
                if (ip6 == NULL) {
@@ -373,7 +370,6 @@
                npc->npc_hlen = hlen;
 
                frag_present = 0;
-               is_frag = false;
 
                /*
                 * Advance by the length of the current header.
@@ -396,27 +392,8 @@
                                if (ip6f == NULL)
                                        return NPC_FMTERR;
 
-                               hlen = sizeof(struct ip6_frag);
-
-                               /* RFC6946: Skip dummy fragments. */
-                               fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
-                               if (fragoff == 0 &&
-                                   !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
-                                       break;
-                               }
-
-                               is_frag = true;
-
-                               /*
-                                * We treat the first fragment as a regular
-                                * packet and then we pass the rest of the
-                                * fragments unconditionally. This way if
-                                * the first packet passes the rest will
-                                * be able to reassembled, if not they will
-                                * be ignored. We can do better later.
-                                */
-                               if (fragoff != 0)
-                                       flags |= NPC_IPFRAG;
+                               hlen = 0;
+                               flags |= NPC_IPFRAG;
 
                                break;
                        case IPPROTO_AH:
@@ -430,24 +407,11 @@
                        if (!hlen) {
                                break;
                        }
-                       onxt = npc->npc_proto;
                        npc->npc_proto = ip6e->ip6e_nxt;
                        npc->npc_hlen += hlen;
                }
 
                /*
-                * We failed to advance. If we are not a fragment, that's
-                * a format error and we leave. Otherwise, restore npc_hlen
-                * and npc_proto to their previous (and correct) values.
-                */
-               if (ip6e == NULL) {
-                       if (!is_frag)
-                               return NPC_FMTERR;
-                       npc->npc_proto = onxt;
-                       npc->npc_hlen -= hlen;
-               }
-
-               /*
                 * Re-fetch the header pointers (nbufs might have been
                 * reallocated).  Restore the original offset (if any).
                 */



Home | Main Index | Thread Index | Old Index