| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: npf.c,v 1.3 2011/11/04 01:00:28 zoltan Exp $ */ | | 1 | /* $NetBSD: npf.c,v 1.4 2011/11/26 23:11:19 rmind Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2010-2011 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2010-2011 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This material is based upon work partially supported by The | | 7 | * This material is based upon work partially supported by The |
8 | * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. | | 8 | * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -20,50 +20,52 @@ | | | @@ -20,50 +20,52 @@ |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.3 2011/11/04 01:00:28 zoltan Exp $"); | | 33 | __KERNEL_RCSID(0, "$NetBSD: npf.c,v 1.4 2011/11/26 23:11:19 rmind Exp $"); |
34 | | | 34 | |
35 | #include <sys/types.h> | | 35 | #include <sys/types.h> |
36 | #include <netinet/in_systm.h> | | 36 | #include <netinet/in_systm.h> |
37 | #include <netinet/in.h> | | 37 | #include <netinet/in.h> |
38 | #include <prop/proplib.h> | | 38 | #include <prop/proplib.h> |
39 | | | 39 | |
40 | #include <stdlib.h> | | 40 | #include <stdlib.h> |
41 | #include <string.h> | | 41 | #include <string.h> |
42 | #include <errno.h> | | 42 | #include <errno.h> |
43 | #include <err.h> | | 43 | #include <err.h> |
44 | | | 44 | |
45 | #define _NPF_PRIVATE | | 45 | #define _NPF_PRIVATE |
46 | #include "npf.h" | | 46 | #include "npf.h" |
47 | | | 47 | |
48 | struct nl_config { | | 48 | struct nl_config { |
49 | /* Rules, translations, tables, procedures. */ | | 49 | /* Rules, translations, tables, procedures. */ |
50 | prop_array_t ncf_rules_list; | | 50 | prop_array_t ncf_rules_list; |
51 | prop_array_t ncf_rproc_list; | | 51 | prop_array_t ncf_rproc_list; |
52 | prop_array_t ncf_table_list; | | 52 | prop_array_t ncf_table_list; |
53 | prop_array_t ncf_nat_list; | | 53 | prop_array_t ncf_nat_list; |
54 | /* Priority counters. */ | | 54 | /* Priority counters. */ |
55 | pri_t ncf_rule_pri; | | 55 | pri_t ncf_rule_pri; |
56 | pri_t ncf_nat_pri; | | 56 | pri_t ncf_nat_pri; |
| | | 57 | /* Custom file to externalise property-list. */ |
| | | 58 | const char * ncf_plist; |
57 | }; | | 59 | }; |
58 | | | 60 | |
59 | struct nl_rule { | | 61 | struct nl_rule { |
60 | prop_dictionary_t nrl_dict; | | 62 | prop_dictionary_t nrl_dict; |
61 | }; | | 63 | }; |
62 | | | 64 | |
63 | struct nl_rproc { | | 65 | struct nl_rproc { |
64 | prop_dictionary_t nrp_dict; | | 66 | prop_dictionary_t nrp_dict; |
65 | }; | | 67 | }; |
66 | | | 68 | |
67 | struct nl_table { | | 69 | struct nl_table { |
68 | prop_dictionary_t ntl_dict; | | 70 | prop_dictionary_t ntl_dict; |
69 | }; | | 71 | }; |
| @@ -79,66 +81,76 @@ npf_config_create(void) | | | @@ -79,66 +81,76 @@ npf_config_create(void) |
79 | | | 81 | |
80 | ncf = malloc(sizeof(nl_config_t)); | | 82 | ncf = malloc(sizeof(nl_config_t)); |
81 | if (ncf == NULL) { | | 83 | if (ncf == NULL) { |
82 | return NULL; | | 84 | return NULL; |
83 | } | | 85 | } |
84 | ncf->ncf_rules_list = prop_array_create(); | | 86 | ncf->ncf_rules_list = prop_array_create(); |
85 | ncf->ncf_rproc_list = prop_array_create(); | | 87 | ncf->ncf_rproc_list = prop_array_create(); |
86 | ncf->ncf_table_list = prop_array_create(); | | 88 | ncf->ncf_table_list = prop_array_create(); |
87 | ncf->ncf_nat_list = prop_array_create(); | | 89 | ncf->ncf_nat_list = prop_array_create(); |
88 | | | 90 | |
89 | ncf->ncf_rule_pri = 1; | | 91 | ncf->ncf_rule_pri = 1; |
90 | ncf->ncf_nat_pri = 1; | | 92 | ncf->ncf_nat_pri = 1; |
91 | | | 93 | |
| | | 94 | ncf->ncf_plist = NULL; |
| | | 95 | |
92 | return ncf; | | 96 | return ncf; |
93 | } | | 97 | } |
94 | | | 98 | |
95 | int | | 99 | int |
96 | npf_config_submit(nl_config_t *ncf, int fd) | | 100 | npf_config_submit(nl_config_t *ncf, int fd) |
97 | { | | 101 | { |
98 | prop_dictionary_t npf_dict; | | 102 | prop_dictionary_t npf_dict; |
| | | 103 | const char *plist = ncf->ncf_plist; |
99 | int error = 0; | | 104 | int error = 0; |
100 | | | 105 | |
101 | npf_dict = prop_dictionary_create(); | | 106 | npf_dict = prop_dictionary_create(); |
102 | if (npf_dict == NULL) { | | 107 | if (npf_dict == NULL) { |
103 | return ENOMEM; | | 108 | return ENOMEM; |
104 | } | | 109 | } |
105 | prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list); | | 110 | prop_dictionary_set(npf_dict, "rules", ncf->ncf_rules_list); |
106 | prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list); | | 111 | prop_dictionary_set(npf_dict, "rprocs", ncf->ncf_rproc_list); |
107 | prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list); | | 112 | prop_dictionary_set(npf_dict, "tables", ncf->ncf_table_list); |
108 | prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list); | | 113 | prop_dictionary_set(npf_dict, "translation", ncf->ncf_nat_list); |
109 | | | 114 | |
110 | #ifndef _NPF_TESTING | | 115 | if (plist) { |
111 | error = prop_dictionary_send_ioctl(npf_dict, fd, IOC_NPF_RELOAD); | | 116 | if (!prop_dictionary_externalize_to_file(npf_dict, plist)) { |
112 | #else | | 117 | error = errno; |
113 | if (!prop_dictionary_externalize_to_file(npf_dict, "./npf.plist")) { | | 118 | } |
114 | error = errno; | | 119 | } else { |
| | | 120 | error = prop_dictionary_send_ioctl(npf_dict, fd, IOC_NPF_RELOAD); |
115 | } | | 121 | } |
116 | #endif | | | |
117 | prop_object_release(npf_dict); | | 122 | prop_object_release(npf_dict); |
118 | return error; | | 123 | return error; |
119 | } | | 124 | } |
120 | | | 125 | |
121 | void | | 126 | void |
122 | npf_config_destroy(nl_config_t *ncf) | | 127 | npf_config_destroy(nl_config_t *ncf) |
123 | { | | 128 | { |
124 | | | 129 | |
125 | prop_object_release(ncf->ncf_rules_list); | | 130 | prop_object_release(ncf->ncf_rules_list); |
126 | prop_object_release(ncf->ncf_rproc_list); | | 131 | prop_object_release(ncf->ncf_rproc_list); |
127 | prop_object_release(ncf->ncf_table_list); | | 132 | prop_object_release(ncf->ncf_table_list); |
128 | prop_object_release(ncf->ncf_nat_list); | | 133 | prop_object_release(ncf->ncf_nat_list); |
129 | free(ncf); | | 134 | free(ncf); |
130 | } | | 135 | } |
131 | | | 136 | |
| | | 137 | void |
| | | 138 | _npf_config_setsubmit(nl_config_t *ncf, const char *plist_file) |
| | | 139 | { |
| | | 140 | |
| | | 141 | ncf->ncf_plist = plist_file; |
| | | 142 | } |
| | | 143 | |
132 | static bool | | 144 | static bool |
133 | _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name) | | 145 | _npf_prop_array_lookup(prop_array_t array, const char *key, const char *name) |
134 | { | | 146 | { |
135 | prop_dictionary_t dict; | | 147 | prop_dictionary_t dict; |
136 | prop_object_iterator_t it; | | 148 | prop_object_iterator_t it; |
137 | | | 149 | |
138 | it = prop_array_iterator(array); | | 150 | it = prop_array_iterator(array); |
139 | while ((dict = prop_object_iterator_next(it)) != NULL) { | | 151 | while ((dict = prop_object_iterator_next(it)) != NULL) { |
140 | const char *lname; | | 152 | const char *lname; |
141 | prop_dictionary_get_cstring_nocopy(dict, key, &lname); | | 153 | prop_dictionary_get_cstring_nocopy(dict, key, &lname); |
142 | if (strcmp(name, lname) == 0) | | 154 | if (strcmp(name, lname) == 0) |
143 | break; | | 155 | break; |
144 | } | | 156 | } |