npf_ext_normalize.c revision 1.1.6.4 1 1.1.6.2 tls /* $NetBSD: npf_ext_normalize.c,v 1.1.6.4 2017/12/03 11:39:03 jdolecek Exp $ */
2 1.1.6.2 tls
3 1.1.6.2 tls /*-
4 1.1.6.2 tls * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
5 1.1.6.2 tls * All rights reserved.
6 1.1.6.2 tls *
7 1.1.6.2 tls * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 tls * modification, are permitted provided that the following conditions
9 1.1.6.2 tls * are met:
10 1.1.6.2 tls * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 tls * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 tls * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 tls * documentation and/or other materials provided with the distribution.
15 1.1.6.2 tls *
16 1.1.6.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.6.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.6.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.6.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.6.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.6.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.6.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.6.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.6.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.6.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.6.2 tls * POSSIBILITY OF SUCH DAMAGE.
27 1.1.6.2 tls */
28 1.1.6.2 tls
29 1.1.6.4 jdolecek #ifdef _KERNEL
30 1.1.6.2 tls #include <sys/cdefs.h>
31 1.1.6.2 tls __KERNEL_RCSID(0, "$NetBSD: npf_ext_normalize.c,v 1.1.6.4 2017/12/03 11:39:03 jdolecek Exp $");
32 1.1.6.2 tls
33 1.1.6.2 tls #include <sys/types.h>
34 1.1.6.2 tls #include <sys/module.h>
35 1.1.6.2 tls #include <sys/kmem.h>
36 1.1.6.2 tls
37 1.1.6.2 tls #include <net/if.h>
38 1.1.6.2 tls #include <netinet/in_systm.h>
39 1.1.6.2 tls #include <netinet/in.h>
40 1.1.6.2 tls #include <netinet/in_var.h>
41 1.1.6.4 jdolecek #endif
42 1.1.6.2 tls
43 1.1.6.2 tls #include "npf.h"
44 1.1.6.2 tls #include "npf_impl.h"
45 1.1.6.2 tls
46 1.1.6.2 tls /*
47 1.1.6.2 tls * NPF extension module definition and the identifier.
48 1.1.6.2 tls */
49 1.1.6.2 tls NPF_EXT_MODULE(npf_ext_normalize, "");
50 1.1.6.2 tls
51 1.1.6.2 tls #define NPFEXT_NORMALIZE_VER 1
52 1.1.6.2 tls
53 1.1.6.2 tls static void * npf_ext_normalize_id;
54 1.1.6.2 tls
55 1.1.6.2 tls /*
56 1.1.6.2 tls * Normalisation parameters.
57 1.1.6.2 tls */
58 1.1.6.2 tls typedef struct {
59 1.1.6.2 tls u_int n_minttl;
60 1.1.6.2 tls u_int n_maxmss;
61 1.1.6.2 tls bool n_random_id;
62 1.1.6.2 tls bool n_no_df;
63 1.1.6.2 tls } npf_normalize_t;
64 1.1.6.2 tls
65 1.1.6.2 tls /*
66 1.1.6.2 tls * npf_normalize_ctor: a constructor for the normalisation rule procedure
67 1.1.6.2 tls * with the given parameters.
68 1.1.6.2 tls */
69 1.1.6.2 tls static int
70 1.1.6.2 tls npf_normalize_ctor(npf_rproc_t *rp, prop_dictionary_t params)
71 1.1.6.2 tls {
72 1.1.6.2 tls npf_normalize_t *np;
73 1.1.6.2 tls
74 1.1.6.2 tls /* Create a structure for normalisation parameters. */
75 1.1.6.2 tls np = kmem_zalloc(sizeof(npf_normalize_t), KM_SLEEP);
76 1.1.6.2 tls
77 1.1.6.2 tls /* IP ID randomisation and IP_DF flag cleansing. */
78 1.1.6.2 tls prop_dictionary_get_bool(params, "random-id", &np->n_random_id);
79 1.1.6.2 tls prop_dictionary_get_bool(params, "no-df", &np->n_no_df);
80 1.1.6.2 tls
81 1.1.6.2 tls /* Minimum IP TTL and maximum TCP MSS. */
82 1.1.6.2 tls prop_dictionary_get_uint32(params, "min-ttl", &np->n_minttl);
83 1.1.6.2 tls prop_dictionary_get_uint32(params, "max-mss", &np->n_maxmss);
84 1.1.6.2 tls
85 1.1.6.2 tls /* Assign the parameters for this rule procedure. */
86 1.1.6.2 tls npf_rproc_assign(rp, np);
87 1.1.6.2 tls return 0;
88 1.1.6.2 tls }
89 1.1.6.2 tls
90 1.1.6.2 tls /*
91 1.1.6.2 tls * npf_normalize_dtor: a destructor for a normalisation rule procedure.
92 1.1.6.2 tls */
93 1.1.6.2 tls static void
94 1.1.6.2 tls npf_normalize_dtor(npf_rproc_t *rp, void *params)
95 1.1.6.2 tls {
96 1.1.6.2 tls /* Free our meta-data, associated with the procedure. */
97 1.1.6.2 tls kmem_free(params, sizeof(npf_normalize_t));
98 1.1.6.2 tls }
99 1.1.6.2 tls
100 1.1.6.2 tls /*
101 1.1.6.2 tls * npf_normalize_ip4: routine to normalize IPv4 header (randomise ID,
102 1.1.6.2 tls * clear "don't fragment" and/or enforce minimum TTL).
103 1.1.6.2 tls */
104 1.1.6.2 tls static inline void
105 1.1.6.2 tls npf_normalize_ip4(npf_cache_t *npc, npf_normalize_t *np)
106 1.1.6.2 tls {
107 1.1.6.2 tls struct ip *ip = npc->npc_ip.v4;
108 1.1.6.2 tls uint16_t cksum = ip->ip_sum;
109 1.1.6.2 tls uint16_t ip_off = ip->ip_off;
110 1.1.6.2 tls uint8_t ttl = ip->ip_ttl;
111 1.1.6.2 tls u_int minttl = np->n_minttl;
112 1.1.6.2 tls
113 1.1.6.2 tls KASSERT(np->n_random_id || np->n_no_df || minttl);
114 1.1.6.2 tls
115 1.1.6.2 tls /* Randomise IPv4 ID. */
116 1.1.6.2 tls if (np->n_random_id) {
117 1.1.6.2 tls uint16_t oid = ip->ip_id, nid;
118 1.1.6.2 tls
119 1.1.6.2 tls nid = htons(ip_randomid(ip_ids, 0));
120 1.1.6.2 tls cksum = npf_fixup16_cksum(cksum, oid, nid);
121 1.1.6.2 tls ip->ip_id = nid;
122 1.1.6.2 tls }
123 1.1.6.2 tls
124 1.1.6.2 tls /* IP_DF flag cleansing. */
125 1.1.6.2 tls if (np->n_no_df && (ip_off & htons(IP_DF)) != 0) {
126 1.1.6.2 tls uint16_t nip_off = ip_off & ~htons(IP_DF);
127 1.1.6.2 tls
128 1.1.6.2 tls cksum = npf_fixup16_cksum(cksum, ip_off, nip_off);
129 1.1.6.2 tls ip->ip_off = nip_off;
130 1.1.6.2 tls }
131 1.1.6.2 tls
132 1.1.6.2 tls /* Enforce minimum TTL. */
133 1.1.6.2 tls if (minttl && ttl < minttl) {
134 1.1.6.2 tls cksum = npf_fixup16_cksum(cksum, ttl, minttl);
135 1.1.6.2 tls ip->ip_ttl = minttl;
136 1.1.6.2 tls }
137 1.1.6.2 tls
138 1.1.6.2 tls /* Update IPv4 checksum. */
139 1.1.6.2 tls ip->ip_sum = cksum;
140 1.1.6.2 tls }
141 1.1.6.2 tls
142 1.1.6.2 tls /*
143 1.1.6.2 tls * npf_normalize: the main routine to normalize IPv4 and/or TCP headers.
144 1.1.6.2 tls */
145 1.1.6.3 tls static bool
146 1.1.6.4 jdolecek npf_normalize(npf_cache_t *npc, void *params, const npf_match_info_t *mi,
147 1.1.6.4 jdolecek int *decision)
148 1.1.6.2 tls {
149 1.1.6.2 tls npf_normalize_t *np = params;
150 1.1.6.2 tls struct tcphdr *th = npc->npc_l4.tcp;
151 1.1.6.2 tls uint16_t cksum, mss, maxmss = np->n_maxmss;
152 1.1.6.2 tls int wscale;
153 1.1.6.2 tls
154 1.1.6.2 tls /* Skip, if already blocking. */
155 1.1.6.2 tls if (*decision == NPF_DECISION_BLOCK) {
156 1.1.6.3 tls return true;
157 1.1.6.2 tls }
158 1.1.6.2 tls
159 1.1.6.2 tls /* Normalise IPv4. Nothing to do for IPv6. */
160 1.1.6.2 tls if (npf_iscached(npc, NPC_IP4) && (np->n_random_id || np->n_minttl)) {
161 1.1.6.2 tls npf_normalize_ip4(npc, np);
162 1.1.6.2 tls }
163 1.1.6.2 tls
164 1.1.6.2 tls /*
165 1.1.6.2 tls * TCP Maximum Segment Size (MSS) "clamping". Only if SYN packet.
166 1.1.6.2 tls * Fetch MSS and check whether rewrite to lower is needed.
167 1.1.6.2 tls */
168 1.1.6.2 tls if (maxmss == 0 || !npf_iscached(npc, NPC_TCP) ||
169 1.1.6.2 tls (th->th_flags & TH_SYN) == 0) {
170 1.1.6.2 tls /* Not required; done. */
171 1.1.6.3 tls return true;
172 1.1.6.2 tls }
173 1.1.6.2 tls mss = 0;
174 1.1.6.3 tls if (!npf_fetch_tcpopts(npc, &mss, &wscale)) {
175 1.1.6.3 tls return true;
176 1.1.6.2 tls }
177 1.1.6.2 tls if (ntohs(mss) <= maxmss) {
178 1.1.6.2 tls /* Nothing else to do. */
179 1.1.6.3 tls return true;
180 1.1.6.2 tls }
181 1.1.6.2 tls maxmss = htons(maxmss);
182 1.1.6.2 tls
183 1.1.6.2 tls /* Store new MSS, calculate TCP checksum and update it. */
184 1.1.6.3 tls if (npf_fetch_tcpopts(npc, &maxmss, &wscale)) {
185 1.1.6.2 tls cksum = npf_fixup16_cksum(th->th_sum, mss, maxmss);
186 1.1.6.2 tls th->th_sum = cksum;
187 1.1.6.2 tls }
188 1.1.6.3 tls
189 1.1.6.3 tls return true;
190 1.1.6.2 tls }
191 1.1.6.2 tls
192 1.1.6.2 tls static int
193 1.1.6.2 tls npf_ext_normalize_modcmd(modcmd_t cmd, void *arg)
194 1.1.6.2 tls {
195 1.1.6.2 tls static const npf_ext_ops_t npf_normalize_ops = {
196 1.1.6.2 tls .version = NPFEXT_NORMALIZE_VER,
197 1.1.6.2 tls .ctx = NULL,
198 1.1.6.2 tls .ctor = npf_normalize_ctor,
199 1.1.6.2 tls .dtor = npf_normalize_dtor,
200 1.1.6.2 tls .proc = npf_normalize
201 1.1.6.2 tls };
202 1.1.6.4 jdolecek npf_t *npf = npf_getkernctx();
203 1.1.6.2 tls
204 1.1.6.2 tls switch (cmd) {
205 1.1.6.2 tls case MODULE_CMD_INIT:
206 1.1.6.2 tls /*
207 1.1.6.2 tls * Initialise normalisation module. Register the "normalize"
208 1.1.6.2 tls * extension and its calls.
209 1.1.6.2 tls */
210 1.1.6.2 tls npf_ext_normalize_id =
211 1.1.6.4 jdolecek npf_ext_register(npf, "normalize", &npf_normalize_ops);
212 1.1.6.2 tls return npf_ext_normalize_id ? 0 : EEXIST;
213 1.1.6.2 tls
214 1.1.6.2 tls case MODULE_CMD_FINI:
215 1.1.6.2 tls /* Unregister the normalisation rule procedure. */
216 1.1.6.4 jdolecek return npf_ext_unregister(npf, npf_ext_normalize_id);
217 1.1.6.2 tls
218 1.1.6.2 tls case MODULE_CMD_AUTOUNLOAD:
219 1.1.6.2 tls return npf_autounload_p() ? 0 : EBUSY;
220 1.1.6.2 tls
221 1.1.6.2 tls default:
222 1.1.6.2 tls return ENOTTY;
223 1.1.6.2 tls }
224 1.1.6.2 tls return 0;
225 1.1.6.2 tls }
226