Source-Changes-HG archive

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

[src/trunk]: src/sys/net/npf Few fixes, KNF/style, bump the NPF version.



details:   https://anonhg.NetBSD.org/src/rev/2121e438b85a
branches:  trunk
changeset: 771011:2121e438b85a
user:      rmind <rmind%NetBSD.org@localhost>
date:      Sun Nov 06 02:49:03 2011 +0000

description:
Few fixes, KNF/style, bump the NPF version.

diffstat:

 sys/net/npf/npf.h           |  36 ++++++++++++++++++------------------
 sys/net/npf/npf_ctl.c       |   9 +++++----
 sys/net/npf/npf_handler.c   |  27 +++++++++++++--------------
 sys/net/npf/npf_inet.c      |  37 ++++++++++++++++++++-----------------
 sys/net/npf/npf_instr.c     |  12 +++++-------
 sys/net/npf/npf_processor.c |  16 ++++++++++------
 sys/net/npf/npf_sendpkt.c   |  24 ++++++++++++------------
 sys/net/npf/npf_tableset.c  |  42 ++++++++++++++++++------------------------
 8 files changed, 101 insertions(+), 102 deletions(-)

diffs (truncated from 577 to 300 lines):

diff -r b809894a683b -r 2121e438b85a sys/net/npf/npf.h
--- a/sys/net/npf/npf.h Sun Nov 06 02:30:06 2011 +0000
+++ b/sys/net/npf/npf.h Sun Nov 06 02:49:03 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf.h,v 1.9 2011/11/04 01:00:27 zoltan Exp $   */
+/*     $NetBSD: npf.h,v 1.10 2011/11/06 02:49:03 rmind Exp $   */
 
 /*-
  * Copyright (c) 2009-2011 The NetBSD Foundation, Inc.
@@ -49,7 +49,7 @@
 #include "testing.h"
 #endif
 
-#define        NPF_VERSION             2
+#define        NPF_VERSION             3
 
 /*
  * Public declarations and definitions.
@@ -57,9 +57,9 @@
 
 /* Storage of address (both for IPv4 and IPv6) and netmask */
 typedef struct in6_addr                npf_addr_t;
-typedef uint_fast8_t           npf_netmask_t;
+typedef uint8_t                        npf_netmask_t;
 
-#define        NPF_NO_NETMASK          (npf_netmask_t)~0
+#define        NPF_NO_NETMASK          ((npf_netmask_t)~0)
 
 #if defined(_KERNEL) || defined(_NPF_TESTING)
 
@@ -116,14 +116,14 @@
        } npc_l4;
 } npf_cache_t;
 
-/* Max length is 32 for IPv4 and 128 for IPv6 */
 static inline void
 npf_generate_mask(npf_addr_t *dst, const npf_netmask_t omask)
 {
        uint_fast8_t length = omask;
 
+       /* Note: maximum length is 32 for IPv4 and 128 for IPv6. */
        KASSERT(length <= 128);
-       memset(dst, 0, sizeof(npf_addr_t));
+
        for (int i = 0; i < 4; i++) {
                if (length >= 32) {
                        dst->s6_addr32[i] = htonl(0xffffffff);
@@ -131,29 +131,30 @@
                } else {
                        dst->s6_addr32[i] = htonl(0xffffffff << (32 - length));
                        length = 0;
-               }  
+               }
        }
 }
 
 static inline void
-npf_calculate_masked_addr(npf_addr_t *dst, const npf_addr_t *src, const npf_netmask_t omask)
+npf_calculate_masked_addr(npf_addr_t *dst, const npf_addr_t *src,
+    const npf_netmask_t omask)
 {
        npf_addr_t mask;
 
        npf_generate_mask(&mask, omask);
        for (int i = 0; i < 4; i++) {
-               dst->s6_addr32[i] =
-                       src->s6_addr32[i] & mask.s6_addr32[i];
+               dst->s6_addr32[i] = src->s6_addr32[i] & mask.s6_addr32[i];
        }
 }
 
 /*
- * compare two addresses, either v4 or v6
- * if the mask is NULL, ignore it
+ * npf_compare_cidr: compare two addresses, either IPv4 or IPv6.
+ *
+ * => If the mask is NULL, ignore it.
  */
 static inline int
 npf_compare_cidr(const npf_addr_t *addr1, const npf_netmask_t mask1,
-                const npf_addr_t *addr2, const npf_netmask_t mask2)
+    const npf_addr_t *addr2, const npf_netmask_t mask2)
 {
        npf_addr_t realmask1, realmask2;
 
@@ -165,11 +166,11 @@
        }
        for (int i = 0; i < 4; i++) {
                const uint32_t x = mask1 != NPF_NO_NETMASK ?
-                               addr1->s6_addr32[i] & realmask1.s6_addr32[i] : 
-                               addr1->s6_addr32[i];
+                   addr1->s6_addr32[i] & realmask1.s6_addr32[i] :
+                   addr1->s6_addr32[i];
                const uint32_t y = mask2 != NPF_NO_NETMASK ?
-                               addr2->s6_addr32[i] & realmask2.s6_addr32[i] :
-                               addr2->s6_addr32[i];
+                   addr2->s6_addr32[i] & realmask2.s6_addr32[i] :
+                   addr2->s6_addr32[i];
                if (x < y) {
                        return -1;
                }
@@ -267,7 +268,6 @@
        u_int                   nct_tid;
        npf_addr_t              nct_addr;
        npf_netmask_t           nct_mask;
-       int                     _reserved;
 } npf_ioctl_table_t;
 
 typedef enum {
diff -r b809894a683b -r 2121e438b85a sys/net/npf/npf_ctl.c
--- a/sys/net/npf/npf_ctl.c     Sun Nov 06 02:30:06 2011 +0000
+++ b/sys/net/npf/npf_ctl.c     Sun Nov 06 02:49:03 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_ctl.c,v 1.8 2011/11/04 02:57:28 jakllsch Exp $     */
+/*     $NetBSD: npf_ctl.c,v 1.9 2011/11/06 02:49:03 rmind Exp $        */
 
 /*-
  * Copyright (c) 2009-2011 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.8 2011/11/04 02:57:28 jakllsch Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_ctl.c,v 1.9 2011/11/06 02:49:03 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/conf.h>
@@ -121,10 +121,11 @@
                eit = prop_array_iterator(entries);
                while ((ent = prop_object_iterator_next(eit)) != NULL) {
                        const npf_addr_t *addr;
-                       uint8_t mask; /* XXX should be npf_netmask_t */
+                       npf_netmask_t mask;
 
                        /* Get address and mask.  Add a table entry. */
-                       addr = (const npf_addr_t *)prop_data_data_nocopy(prop_dictionary_get(ent, "addr"));
+                       addr = (const npf_addr_t *)prop_data_data_nocopy(
+                           prop_dictionary_get(ent, "addr"));
                        prop_dictionary_get_uint8(ent, "mask", &mask);
                        error = npf_table_add_cidr(tblset, tid, addr, mask);
                        if (error)
diff -r b809894a683b -r 2121e438b85a sys/net/npf/npf_handler.c
--- a/sys/net/npf/npf_handler.c Sun Nov 06 02:30:06 2011 +0000
+++ b/sys/net/npf/npf_handler.c Sun Nov 06 02:49:03 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_handler.c,v 1.9 2011/11/05 10:23:26 zoltan Exp $   */
+/*     $NetBSD: npf_handler.c,v 1.10 2011/11/06 02:49:03 rmind Exp $   */
 
 /*-
  * Copyright (c) 2009-2010 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.9 2011/11/05 10:23:26 zoltan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_handler.c,v 1.10 2011/11/06 02:49:03 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -101,29 +101,29 @@
        rp = NULL;
        ret = 0;
 
-       /* Cache everything.  Determine whether it is an IPv4 fragment. */
-       /* Cache IP information */
+       /* Cache everything.  Determine whether it is an IP fragment. */
        npf_cache_all(&npc, nbuf);
 
        if (npf_iscached(&npc, NPC_IPFRAG)) {
+               /* Pass to IPv4 or IPv6 reassembly mechanism. */
                if (npf_iscached(&npc, NPC_IP4)) {
                        struct ip *ip = nbuf_dataptr(*mp);
-                       /* Pass to IPv4 reassembly mechanism. */
                        ret = ip_reass_packet(mp, ip);
                } else {
                        KASSERT(npf_iscached(&npc, NPC_IP6));
 #ifdef INET6
-                       /* frag6_input's offset is the start of the fragment header */
+                       /*
+                        * Note: frag6_input() offset is the start of the
+                        * fragment header.
+                        */
                        size_t hlen = npf_cache_hlen(&npc, nbuf);
-
-                       /* Pass to IPv6 reassembly mechanism. */
                        ret = ip6_reass_packet(mp, hlen);
 #else
-                       KASSERT(false);
+                       ret = -1;
 #endif
                }
 
-               if (ret != 0) {
+               if (ret) {
                        error = EINVAL;
                        se = NULL;
                        goto out;
@@ -132,13 +132,12 @@
                        /* More fragments should come; return. */
                        return 0;
                }
-               /* Reassembly is complete, we have the final packet. */
-               nbuf = (nbuf_t *)*mp;
 
                /*
-                * Before reassembly, we can't cache anything above layer3,
-                * but at this point, it's reassembled - let's cache it again
+                * Reassembly is complete, we have the final packet.
+                * Cache again, since layer 3 daya is accessible now.
                 */
+               nbuf = (nbuf_t *)*mp;
                npc.npc_info = 0;
                npf_cache_all(&npc, nbuf);
        }
diff -r b809894a683b -r 2121e438b85a sys/net/npf/npf_inet.c
--- a/sys/net/npf/npf_inet.c    Sun Nov 06 02:30:06 2011 +0000
+++ b/sys/net/npf/npf_inet.c    Sun Nov 06 02:49:03 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_inet.c,v 1.7 2011/11/04 01:00:27 zoltan Exp $      */
+/*     $NetBSD: npf_inet.c,v 1.8 2011/11/06 02:49:03 rmind Exp $       */
 
 /*-
  * Copyright (c) 2009-2011 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.7 2011/11/04 01:00:27 zoltan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.8 2011/11/06 02:49:03 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -126,28 +126,27 @@
  * Returns all values in host byte-order.
  */
 int
-npf_tcpsaw(npf_cache_t *npc, nbuf_t *nbuf, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
+npf_tcpsaw(npf_cache_t *npc, nbuf_t *nbuf, tcp_seq *seq, tcp_seq *ack,
+    uint32_t *win)
 {
        struct tcphdr *th = &npc->npc_l4.tcp;
+       u_int thlen;
 
        KASSERT(npf_iscached(npc, NPC_TCP));
 
        *seq = ntohl(th->th_seq);
        *ack = ntohl(th->th_ack);
        *win = (uint32_t)ntohs(th->th_win);
+       thlen = th->th_off << 2;
 
-       /*
-        * total length of packet - header length - tcp header length
-        */
        if (npf_iscached(npc, NPC_IP4)) {
                struct ip *ip = &npc->npc_ip.v4;
-               return ntohs(ip->ip_len) - npf_cache_hlen(npc, nbuf) - (th->th_off << 2);
+               return ntohs(ip->ip_len) - npf_cache_hlen(npc, nbuf) - thlen;
        } else {
                KASSERT(npf_iscached(npc, NPC_IP6));
                struct ip6_hdr *ip6 = &npc->npc_ip.v6;
-               return ntohs(ip6->ip6_plen) - (th->th_off << 2);
+               return ntohs(ip6->ip6_plen) - thlen;
        }
-
        return 0;
 }
 
@@ -276,16 +275,20 @@
                        return false;
                }
 
-               struct ip6_ext ip6e;
                size_t toskip = sizeof(struct ip6_hdr);
                bool processing_ends = false;
                npc->npc_next_proto = ip6->ip6_nxt;
-               npc->npc_hlen = 0; 
+               npc->npc_hlen = 0;
 
                do {
-                       /* advance the length of the previous known header,
-                          and fetch the next extension header's length */
-                       if (nbuf_advfetch(&nbuf, &n_ptr, toskip, sizeof(struct ip6_ext), &ip6e)) {
+                       struct ip6_ext ip6e;
+
+                       /*
+                        * Advance by the length of the previous known header
+                        * and fetch the next extension header's length.
+                        */
+                       if (nbuf_advfetch(&nbuf, &n_ptr, toskip,
+                           sizeof(struct ip6_ext), &ip6e)) {
                                return false;
                        }
 
@@ -307,7 +310,7 @@
                        }
 
                        npc->npc_hlen += toskip;
-                       
+
                        if (!processing_ends) {



Home | Main Index | Thread Index | Old Index