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