npf_ext_normalize.c revision 1.1.6.2 1 1.1.6.2 tls /* $NetBSD: npf_ext_normalize.c,v 1.1.6.2 2013/06/23 06:20:25 tls 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.2 tls #include <sys/cdefs.h>
30 1.1.6.2 tls __KERNEL_RCSID(0, "$NetBSD: npf_ext_normalize.c,v 1.1.6.2 2013/06/23 06:20:25 tls Exp $");
31 1.1.6.2 tls
32 1.1.6.2 tls #include <sys/types.h>
33 1.1.6.2 tls #include <sys/module.h>
34 1.1.6.2 tls #include <sys/kmem.h>
35 1.1.6.2 tls
36 1.1.6.2 tls #include <net/if.h>
37 1.1.6.2 tls #include <netinet/in_systm.h>
38 1.1.6.2 tls #include <netinet/in.h>
39 1.1.6.2 tls #include <netinet/in_var.h>
40 1.1.6.2 tls
41 1.1.6.2 tls #include "npf.h"
42 1.1.6.2 tls #include "npf_impl.h"
43 1.1.6.2 tls
44 1.1.6.2 tls /*
45 1.1.6.2 tls * NPF extension module definition and the identifier.
46 1.1.6.2 tls */
47 1.1.6.2 tls NPF_EXT_MODULE(npf_ext_normalize, "");
48 1.1.6.2 tls
49 1.1.6.2 tls #define NPFEXT_NORMALIZE_VER 1
50 1.1.6.2 tls
51 1.1.6.2 tls static void * npf_ext_normalize_id;
52 1.1.6.2 tls
53 1.1.6.2 tls /*
54 1.1.6.2 tls * Normalisation parameters.
55 1.1.6.2 tls */
56 1.1.6.2 tls typedef struct {
57 1.1.6.2 tls u_int n_minttl;
58 1.1.6.2 tls u_int n_maxmss;
59 1.1.6.2 tls bool n_random_id;
60 1.1.6.2 tls bool n_no_df;
61 1.1.6.2 tls } npf_normalize_t;
62 1.1.6.2 tls
63 1.1.6.2 tls /*
64 1.1.6.2 tls * npf_normalize_ctor: a constructor for the normalisation rule procedure
65 1.1.6.2 tls * with the given parameters.
66 1.1.6.2 tls */
67 1.1.6.2 tls static int
68 1.1.6.2 tls npf_normalize_ctor(npf_rproc_t *rp, prop_dictionary_t params)
69 1.1.6.2 tls {
70 1.1.6.2 tls npf_normalize_t *np;
71 1.1.6.2 tls
72 1.1.6.2 tls /* Create a structure for normalisation parameters. */
73 1.1.6.2 tls np = kmem_zalloc(sizeof(npf_normalize_t), KM_SLEEP);
74 1.1.6.2 tls
75 1.1.6.2 tls /* IP ID randomisation and IP_DF flag cleansing. */
76 1.1.6.2 tls prop_dictionary_get_bool(params, "random-id", &np->n_random_id);
77 1.1.6.2 tls prop_dictionary_get_bool(params, "no-df", &np->n_no_df);
78 1.1.6.2 tls
79 1.1.6.2 tls /* Minimum IP TTL and maximum TCP MSS. */
80 1.1.6.2 tls prop_dictionary_get_uint32(params, "min-ttl", &np->n_minttl);
81 1.1.6.2 tls prop_dictionary_get_uint32(params, "max-mss", &np->n_maxmss);
82 1.1.6.2 tls
83 1.1.6.2 tls /* Assign the parameters for this rule procedure. */
84 1.1.6.2 tls npf_rproc_assign(rp, np);
85 1.1.6.2 tls return 0;
86 1.1.6.2 tls }
87 1.1.6.2 tls
88 1.1.6.2 tls /*
89 1.1.6.2 tls * npf_normalize_dtor: a destructor for a normalisation rule procedure.
90 1.1.6.2 tls */
91 1.1.6.2 tls static void
92 1.1.6.2 tls npf_normalize_dtor(npf_rproc_t *rp, void *params)
93 1.1.6.2 tls {
94 1.1.6.2 tls /* Free our meta-data, associated with the procedure. */
95 1.1.6.2 tls kmem_free(params, sizeof(npf_normalize_t));
96 1.1.6.2 tls }
97 1.1.6.2 tls
98 1.1.6.2 tls /*
99 1.1.6.2 tls * npf_normalize_ip4: routine to normalize IPv4 header (randomise ID,
100 1.1.6.2 tls * clear "don't fragment" and/or enforce minimum TTL).
101 1.1.6.2 tls */
102 1.1.6.2 tls static inline void
103 1.1.6.2 tls npf_normalize_ip4(npf_cache_t *npc, npf_normalize_t *np)
104 1.1.6.2 tls {
105 1.1.6.2 tls struct ip *ip = npc->npc_ip.v4;
106 1.1.6.2 tls uint16_t cksum = ip->ip_sum;
107 1.1.6.2 tls uint16_t ip_off = ip->ip_off;
108 1.1.6.2 tls uint8_t ttl = ip->ip_ttl;
109 1.1.6.2 tls u_int minttl = np->n_minttl;
110 1.1.6.2 tls
111 1.1.6.2 tls KASSERT(np->n_random_id || np->n_no_df || minttl);
112 1.1.6.2 tls
113 1.1.6.2 tls /* Randomise IPv4 ID. */
114 1.1.6.2 tls if (np->n_random_id) {
115 1.1.6.2 tls uint16_t oid = ip->ip_id, nid;
116 1.1.6.2 tls
117 1.1.6.2 tls nid = htons(ip_randomid(ip_ids, 0));
118 1.1.6.2 tls cksum = npf_fixup16_cksum(cksum, oid, nid);
119 1.1.6.2 tls ip->ip_id = nid;
120 1.1.6.2 tls }
121 1.1.6.2 tls
122 1.1.6.2 tls /* IP_DF flag cleansing. */
123 1.1.6.2 tls if (np->n_no_df && (ip_off & htons(IP_DF)) != 0) {
124 1.1.6.2 tls uint16_t nip_off = ip_off & ~htons(IP_DF);
125 1.1.6.2 tls
126 1.1.6.2 tls cksum = npf_fixup16_cksum(cksum, ip_off, nip_off);
127 1.1.6.2 tls ip->ip_off = nip_off;
128 1.1.6.2 tls }
129 1.1.6.2 tls
130 1.1.6.2 tls /* Enforce minimum TTL. */
131 1.1.6.2 tls if (minttl && ttl < minttl) {
132 1.1.6.2 tls cksum = npf_fixup16_cksum(cksum, ttl, minttl);
133 1.1.6.2 tls ip->ip_ttl = minttl;
134 1.1.6.2 tls }
135 1.1.6.2 tls
136 1.1.6.2 tls /* Update IPv4 checksum. */
137 1.1.6.2 tls ip->ip_sum = cksum;
138 1.1.6.2 tls }
139 1.1.6.2 tls
140 1.1.6.2 tls /*
141 1.1.6.2 tls * npf_normalize: the main routine to normalize IPv4 and/or TCP headers.
142 1.1.6.2 tls */
143 1.1.6.2 tls static void
144 1.1.6.2 tls npf_normalize(npf_cache_t *npc, nbuf_t *nbuf, void *params, int *decision)
145 1.1.6.2 tls {
146 1.1.6.2 tls npf_normalize_t *np = params;
147 1.1.6.2 tls struct tcphdr *th = npc->npc_l4.tcp;
148 1.1.6.2 tls uint16_t cksum, mss, maxmss = np->n_maxmss;
149 1.1.6.2 tls int wscale;
150 1.1.6.2 tls
151 1.1.6.2 tls /* Skip, if already blocking. */
152 1.1.6.2 tls if (*decision == NPF_DECISION_BLOCK) {
153 1.1.6.2 tls return;
154 1.1.6.2 tls }
155 1.1.6.2 tls
156 1.1.6.2 tls /* Normalise IPv4. Nothing to do for IPv6. */
157 1.1.6.2 tls if (npf_iscached(npc, NPC_IP4) && (np->n_random_id || np->n_minttl)) {
158 1.1.6.2 tls npf_normalize_ip4(npc, np);
159 1.1.6.2 tls }
160 1.1.6.2 tls
161 1.1.6.2 tls /*
162 1.1.6.2 tls * TCP Maximum Segment Size (MSS) "clamping". Only if SYN packet.
163 1.1.6.2 tls * Fetch MSS and check whether rewrite to lower is needed.
164 1.1.6.2 tls */
165 1.1.6.2 tls if (maxmss == 0 || !npf_iscached(npc, NPC_TCP) ||
166 1.1.6.2 tls (th->th_flags & TH_SYN) == 0) {
167 1.1.6.2 tls /* Not required; done. */
168 1.1.6.2 tls return;
169 1.1.6.2 tls }
170 1.1.6.2 tls mss = 0;
171 1.1.6.2 tls if (!npf_fetch_tcpopts(npc, nbuf, &mss, &wscale)) {
172 1.1.6.2 tls return;
173 1.1.6.2 tls }
174 1.1.6.2 tls if (ntohs(mss) <= maxmss) {
175 1.1.6.2 tls /* Nothing else to do. */
176 1.1.6.2 tls return;
177 1.1.6.2 tls }
178 1.1.6.2 tls maxmss = htons(maxmss);
179 1.1.6.2 tls
180 1.1.6.2 tls /* Store new MSS, calculate TCP checksum and update it. */
181 1.1.6.2 tls if (npf_fetch_tcpopts(npc, nbuf, &maxmss, &wscale)) {
182 1.1.6.2 tls cksum = npf_fixup16_cksum(th->th_sum, mss, maxmss);
183 1.1.6.2 tls th->th_sum = cksum;
184 1.1.6.2 tls }
185 1.1.6.2 tls }
186 1.1.6.2 tls
187 1.1.6.2 tls static int
188 1.1.6.2 tls npf_ext_normalize_modcmd(modcmd_t cmd, void *arg)
189 1.1.6.2 tls {
190 1.1.6.2 tls static const npf_ext_ops_t npf_normalize_ops = {
191 1.1.6.2 tls .version = NPFEXT_NORMALIZE_VER,
192 1.1.6.2 tls .ctx = NULL,
193 1.1.6.2 tls .ctor = npf_normalize_ctor,
194 1.1.6.2 tls .dtor = npf_normalize_dtor,
195 1.1.6.2 tls .proc = npf_normalize
196 1.1.6.2 tls };
197 1.1.6.2 tls
198 1.1.6.2 tls switch (cmd) {
199 1.1.6.2 tls case MODULE_CMD_INIT:
200 1.1.6.2 tls /*
201 1.1.6.2 tls * Initialise normalisation module. Register the "normalize"
202 1.1.6.2 tls * extension and its calls.
203 1.1.6.2 tls */
204 1.1.6.2 tls npf_ext_normalize_id =
205 1.1.6.2 tls npf_ext_register("normalize", &npf_normalize_ops);
206 1.1.6.2 tls return npf_ext_normalize_id ? 0 : EEXIST;
207 1.1.6.2 tls
208 1.1.6.2 tls case MODULE_CMD_FINI:
209 1.1.6.2 tls /* Unregister the normalisation rule procedure. */
210 1.1.6.2 tls return npf_ext_unregister(npf_ext_normalize_id);
211 1.1.6.2 tls
212 1.1.6.2 tls case MODULE_CMD_AUTOUNLOAD:
213 1.1.6.2 tls return npf_autounload_p() ? 0 : EBUSY;
214 1.1.6.2 tls
215 1.1.6.2 tls default:
216 1.1.6.2 tls return ENOTTY;
217 1.1.6.2 tls }
218 1.1.6.2 tls return 0;
219 1.1.6.2 tls }
220