Module Name:    src
Committed By:   rmind
Date:           Fri Nov 22 00:25:51 UTC 2013

Modified Files:
        src/lib/libnpf: npf.c npf.h
        src/sys/net/npf: npf_conf.c npf_impl.h npf_tableset.c
        src/usr.sbin/npf/npfctl: npf_build.c npf_data.c npf_show.c

Log Message:
Add npf_tableset_syncdict() to sync the table IDs in the proplib dictionary,
as they can change on reload now.  Also, fix table name checking in npfctl.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/lib/libnpf/npf.c
cvs rdiff -u -r1.20 -r1.21 src/lib/libnpf/npf.h
cvs rdiff -u -r1.4 -r1.5 src/sys/net/npf/npf_conf.c
cvs rdiff -u -r1.40 -r1.41 src/sys/net/npf/npf_impl.h
cvs rdiff -u -r1.19 -r1.20 src/sys/net/npf/npf_tableset.c
cvs rdiff -u -r1.30 -r1.31 src/usr.sbin/npf/npfctl/npf_build.c
cvs rdiff -u -r1.22 -r1.23 src/usr.sbin/npf/npfctl/npf_data.c
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/npf/npfctl/npf_show.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libnpf/npf.c
diff -u src/lib/libnpf/npf.c:1.23 src/lib/libnpf/npf.c:1.24
--- src/lib/libnpf/npf.c:1.23	Tue Nov 12 00:46:34 2013
+++ src/lib/libnpf/npf.c	Fri Nov 22 00:25:51 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: npf.c,v 1.23 2013/11/12 00:46:34 rmind Exp $	*/
+/*	$NetBSD: npf.c,v 1.24 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2010-2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.23 2013/11/12 00:46:34 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.24 2013/11/22 00:25:51 rmind Exp $");
 
 #include <sys/types.h>
 #include <netinet/in_systm.h>
@@ -962,15 +962,17 @@ npf_table_add_entry(nl_table_t *tl, int 
 }
 
 bool
-npf_table_exists_p(nl_config_t *ncf, u_int tid)
+npf_table_exists_p(nl_config_t *ncf, const char *name)
 {
 	prop_dictionary_t tldict;
 	prop_object_iterator_t it;
-	u_int i;
 
 	it = prop_array_iterator(ncf->ncf_table_list);
 	while ((tldict = prop_object_iterator_next(it)) != NULL) {
-		if (prop_dictionary_get_uint32(tldict, "id", &i) && tid == i)
+		const char *tname = NULL;
+
+		if (prop_dictionary_get_cstring_nocopy(tldict, "name", &tname)
+		    && strcmp(tname, name) == 0)
 			break;
 	}
 	prop_object_iterator_release(it);
@@ -981,12 +983,12 @@ int
 npf_table_insert(nl_config_t *ncf, nl_table_t *tl)
 {
 	prop_dictionary_t tldict = tl->ntl_dict;
-	u_int tid;
+	const char *name = NULL;
 
-	if (!prop_dictionary_get_uint32(tldict, "id", &tid)) {
+	if (!prop_dictionary_get_cstring_nocopy(tldict, "name", &name)) {
 		return EINVAL;
 	}
-	if (npf_table_exists_p(ncf, tid)) {
+	if (npf_table_exists_p(ncf, name)) {
 		return EEXIST;
 	}
 	prop_array_add(ncf->ncf_table_list, tldict);

Index: src/lib/libnpf/npf.h
diff -u src/lib/libnpf/npf.h:1.20 src/lib/libnpf/npf.h:1.21
--- src/lib/libnpf/npf.h:1.20	Tue Nov 12 00:46:34 2013
+++ src/lib/libnpf/npf.h	Fri Nov 22 00:25:51 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: npf.h,v 1.20 2013/11/12 00:46:34 rmind Exp $	*/
+/*	$NetBSD: npf.h,v 1.21 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2011-2013 The NetBSD Foundation, Inc.
@@ -111,7 +111,7 @@ int		npf_nat_insert(nl_config_t *, nl_na
 nl_table_t *	npf_table_create(const char *, u_int, int);
 int		npf_table_add_entry(nl_table_t *, int,
 		    const npf_addr_t *, const npf_netmask_t);
-bool		npf_table_exists_p(nl_config_t *, u_int);
+bool		npf_table_exists_p(nl_config_t *, const char *);
 int		npf_table_insert(nl_config_t *, nl_table_t *);
 void		npf_table_destroy(nl_table_t *);
 

Index: src/sys/net/npf/npf_conf.c
diff -u src/sys/net/npf/npf_conf.c:1.4 src/sys/net/npf/npf_conf.c:1.5
--- src/sys/net/npf/npf_conf.c:1.4	Tue Nov 12 00:46:34 2013
+++ src/sys/net/npf/npf_conf.c	Fri Nov 22 00:25:51 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: npf_conf.c,v 1.4 2013/11/12 00:46:34 rmind Exp $	*/
+/*	$NetBSD: npf_conf.c,v 1.5 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -48,7 +48,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.4 2013/11/12 00:46:34 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_conf.c,v 1.5 2013/11/22 00:25:51 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -166,6 +166,9 @@ npf_config_reload(prop_dictionary_t dict
 	if (flush) {
 		npf_ifmap_flush();
 	}
+
+	/* Sync the config proplib data. */
+	npf_tableset_syncdict(tset, dict);
 	mutex_exit(&npf_config_lock);
 
 	/* Finally, it is safe to destroy the old config. */

Index: src/sys/net/npf/npf_impl.h
diff -u src/sys/net/npf/npf_impl.h:1.40 src/sys/net/npf/npf_impl.h:1.41
--- src/sys/net/npf/npf_impl.h:1.40	Sat Nov 16 01:18:58 2013
+++ src/sys/net/npf/npf_impl.h	Fri Nov 22 00:25:51 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: npf_impl.h,v 1.40 2013/11/16 01:18:58 rmind Exp $	*/
+/*	$NetBSD: npf_impl.h,v 1.41 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
@@ -224,6 +224,7 @@ int		npf_tableset_insert(npf_tableset_t 
 npf_table_t *	npf_tableset_getbyname(npf_tableset_t *, const char *);
 npf_table_t *	npf_tableset_getbyid(npf_tableset_t *, u_int);
 void		npf_tableset_reload(npf_tableset_t *, npf_tableset_t *);
+void		npf_tableset_syncdict(const npf_tableset_t *, prop_dictionary_t);
 
 npf_table_t *	npf_table_create(const char *, u_int, int, size_t);
 void		npf_table_destroy(npf_table_t *);

Index: src/sys/net/npf/npf_tableset.c
diff -u src/sys/net/npf/npf_tableset.c:1.19 src/sys/net/npf/npf_tableset.c:1.20
--- src/sys/net/npf/npf_tableset.c:1.19	Tue Nov 12 00:46:34 2013
+++ src/sys/net/npf/npf_tableset.c	Fri Nov 22 00:25:51 2013
@@ -1,7 +1,7 @@
-/*	$NetBSD: npf_tableset.c,v 1.19 2013/11/12 00:46:34 rmind Exp $	*/
+/*	$NetBSD: npf_tableset.c,v 1.20 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
- * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
+ * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This material is based upon work partially supported by The
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.19 2013/11/12 00:46:34 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_tableset.c,v 1.20 2013/11/22 00:25:51 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -243,6 +243,31 @@ npf_tableset_reload(npf_tableset_t *nts,
 	}
 }
 
+void
+npf_tableset_syncdict(const npf_tableset_t *ts, prop_dictionary_t ndict)
+{
+	prop_array_t tables = prop_array_create();
+	const npf_table_t *t;
+
+	KASSERT(npf_config_locked_p());
+
+	for (u_int tid = 0; tid < ts->ts_nitems; tid++) {
+		if ((t = ts->ts_map[tid]) == NULL) {
+			continue;
+		}
+		prop_dictionary_t tdict = prop_dictionary_create();
+		prop_dictionary_set_cstring(tdict, "name", t->t_name);
+		prop_dictionary_set_uint32(tdict, "type", t->t_type);
+		prop_dictionary_set_uint32(tdict, "id", tid);
+
+		prop_array_add(tables, tdict);
+		prop_object_release(tdict);
+	}
+	prop_dictionary_remove(ndict, "tables");
+	prop_dictionary_set(ndict, "tables", tables);
+	prop_object_release(tables);
+}
+
 /*
  * Few helper routines.
  */
@@ -377,7 +402,7 @@ npf_table_check(npf_tableset_t *ts, cons
 		return ENAMETOOLONG;
 	}
 	if (npf_tableset_getbyname(ts, name)) {
-		return EINVAL;
+		return EEXIST;
 	}
 	return 0;
 }

Index: src/usr.sbin/npf/npfctl/npf_build.c
diff -u src/usr.sbin/npf/npfctl/npf_build.c:1.30 src/usr.sbin/npf/npfctl/npf_build.c:1.31
--- src/usr.sbin/npf/npfctl/npf_build.c:1.30	Tue Nov 19 00:28:41 2013
+++ src/usr.sbin/npf/npfctl/npf_build.c	Fri Nov 22 00:25:51 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: npf_build.c,v 1.30 2013/11/19 00:28:41 rmind Exp $	*/
+/*	$NetBSD: npf_build.c,v 1.31 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2011-2013 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: npf_build.c,v 1.30 2013/11/19 00:28:41 rmind Exp $");
+__RCSID("$NetBSD: npf_build.c,v 1.31 2013/11/22 00:25:51 rmind Exp $");
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
@@ -124,9 +124,9 @@ npfctl_debug_addif(const char *ifname)
 }
 
 bool
-npfctl_table_exists_p(const char *id)
+npfctl_table_exists_p(const char *name)
 {
-	return npf_table_exists_p(npf_conf, atoi(id));
+	return npf_conf ? npf_table_exists_p(npf_conf, name) : false;
 }
 
 static in_port_t

Index: src/usr.sbin/npf/npfctl/npf_data.c
diff -u src/usr.sbin/npf/npfctl/npf_data.c:1.22 src/usr.sbin/npf/npfctl/npf_data.c:1.23
--- src/usr.sbin/npf/npfctl/npf_data.c:1.22	Tue Nov 19 00:28:41 2013
+++ src/usr.sbin/npf/npfctl/npf_data.c	Fri Nov 22 00:25:51 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: npf_data.c,v 1.22 2013/11/19 00:28:41 rmind Exp $	*/
+/*	$NetBSD: npf_data.c,v 1.23 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: npf_data.c,v 1.22 2013/11/19 00:28:41 rmind Exp $");
+__RCSID("$NetBSD: npf_data.c,v 1.23 2013/11/22 00:25:51 rmind Exp $");
 
 #include <sys/types.h>
 #include <sys/null.h>
@@ -221,13 +221,13 @@ npfctl_parse_fam_addr_mask(const char *a
 }
 
 npfvar_t *
-npfctl_parse_table_id(const char *id)
+npfctl_parse_table_id(const char *name)
 {
-	if (!npfctl_table_exists_p(id)) {
-		yyerror("table '%s' is not defined", id);
+	if (!npfctl_table_exists_p(name)) {
+		yyerror("table '%s' is not defined", name);
 		return NULL;
 	}
-	return npfvar_create_from_string(NPFVAR_TABLE, id);
+	return npfvar_create_from_string(NPFVAR_TABLE, name);
 }
 
 /*

Index: src/usr.sbin/npf/npfctl/npf_show.c
diff -u src/usr.sbin/npf/npfctl/npf_show.c:1.6 src/usr.sbin/npf/npfctl/npf_show.c:1.7
--- src/usr.sbin/npf/npfctl/npf_show.c:1.6	Tue Nov 19 17:01:45 2013
+++ src/usr.sbin/npf/npfctl/npf_show.c	Fri Nov 22 00:25:51 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: npf_show.c,v 1.6 2013/11/19 17:01:45 christos Exp $	*/
+/*	$NetBSD: npf_show.c,v 1.7 2013/11/22 00:25:51 rmind Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: npf_show.c,v 1.6 2013/11/19 17:01:45 christos Exp $");
+__RCSID("$NetBSD: npf_show.c,v 1.7 2013/11/22 00:25:51 rmind Exp $");
 
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -158,13 +158,12 @@ print_table(npf_conf_info_t *ctx, const 
 	char *p;
 
 	while ((tl = npf_table_iterate(ctx->conf)) != NULL) {
-		if (npf_table_getid(tl) == tid)
-			break;
+		if (npf_table_getid(tl) == tid) {
+			easprintf(&p, "%s", npf_table_getname(tl));
+			return p;
+		}
 	}
-	if (tl == NULL)
-		errx(EXIT_FAILURE, "table id %u not found", tid);
-	easprintf(&p, "%s", npf_table_getname(tl));
-	return p;
+	abort();
 }
 
 static char *

Reply via email to