npf_alg_icmp.c revision 1.3 1 1.3 rmind /* $NetBSD: npf_alg_icmp.c,v 1.3 2010/09/25 00:25:31 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.1 rmind * NPF ALG for ICMP and traceroute translations.
34 1.1 rmind */
35 1.1 rmind
36 1.1 rmind #ifdef _KERNEL
37 1.1 rmind #include <sys/cdefs.h>
38 1.3 rmind __KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.3 2010/09/25 00:25:31 rmind Exp $");
39 1.1 rmind
40 1.1 rmind #include <sys/param.h>
41 1.1 rmind #include <sys/kernel.h>
42 1.1 rmind #endif
43 1.1 rmind #include <sys/module.h>
44 1.1 rmind #include <sys/pool.h>
45 1.1 rmind
46 1.1 rmind #include <netinet/in_systm.h>
47 1.1 rmind #include <netinet/in.h>
48 1.1 rmind #include <netinet/ip.h>
49 1.1 rmind #include <netinet/tcp.h>
50 1.1 rmind #include <netinet/udp.h>
51 1.1 rmind #include <netinet/ip_icmp.h>
52 1.1 rmind #include <net/pfil.h>
53 1.1 rmind
54 1.1 rmind #include "npf_impl.h"
55 1.1 rmind
56 1.1 rmind MODULE(MODULE_CLASS_MISC, npf_alg_icmp, "npf");
57 1.1 rmind
58 1.1 rmind /*
59 1.1 rmind * Traceroute criteria.
60 1.1 rmind *
61 1.1 rmind * IANA assigned base port: 33434. However, common practice is to increase
62 1.1 rmind * the port, thus monitor [33434-33484] range. Additional filter is TTL < 50.
63 1.1 rmind */
64 1.1 rmind
65 1.1 rmind #define TR_BASE_PORT 33434
66 1.1 rmind #define TR_PORT_RANGE 33484
67 1.1 rmind #define TR_MAX_TTL 50
68 1.1 rmind
69 1.1 rmind static npf_alg_t * alg_icmp;
70 1.1 rmind
71 1.1 rmind static bool npfa_icmp_match(npf_cache_t *, nbuf_t *, void *);
72 1.1 rmind static bool npfa_icmp_natin(npf_cache_t *, nbuf_t *, void *);
73 1.1 rmind static bool npfa_icmp_session(npf_cache_t *, nbuf_t *, void *);
74 1.1 rmind
75 1.1 rmind /*
76 1.1 rmind * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
77 1.1 rmind * and module interface.
78 1.1 rmind */
79 1.1 rmind
80 1.1 rmind static int
81 1.1 rmind npf_alg_icmp_init(void)
82 1.1 rmind {
83 1.1 rmind
84 1.1 rmind alg_icmp = npf_alg_register(npfa_icmp_match, NULL,
85 1.1 rmind npfa_icmp_natin, npfa_icmp_session);
86 1.1 rmind KASSERT(alg_icmp != NULL);
87 1.1 rmind return 0;
88 1.1 rmind }
89 1.1 rmind
90 1.1 rmind static int
91 1.1 rmind npf_alg_icmp_fini(void)
92 1.1 rmind {
93 1.1 rmind
94 1.1 rmind KASSERT(alg_icmp != NULL);
95 1.1 rmind return npf_alg_unregister(alg_icmp);
96 1.1 rmind }
97 1.1 rmind
98 1.1 rmind static int
99 1.1 rmind npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
100 1.1 rmind {
101 1.1 rmind
102 1.1 rmind switch (cmd) {
103 1.1 rmind case MODULE_CMD_INIT:
104 1.1 rmind return npf_alg_icmp_init();
105 1.1 rmind case MODULE_CMD_FINI:
106 1.1 rmind return npf_alg_icmp_fini();
107 1.1 rmind default:
108 1.1 rmind return ENOTTY;
109 1.1 rmind }
110 1.1 rmind return 0;
111 1.1 rmind }
112 1.1 rmind
113 1.1 rmind /*
114 1.1 rmind * npfa_icmp_match: ALG matching inspector, determines ALG case and
115 1.1 rmind * establishes a session for "backwards" stream.
116 1.1 rmind */
117 1.1 rmind static bool
118 1.1 rmind npfa_icmp_match(npf_cache_t *npc, nbuf_t *nbuf, void *ntptr)
119 1.1 rmind {
120 1.1 rmind const int proto = npc->npc_proto;
121 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
122 1.3 rmind u_int offby;
123 1.3 rmind uint8_t ttl;
124 1.1 rmind
125 1.1 rmind /* Handle TCP/UDP traceroute - check for port range. */
126 1.1 rmind if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
127 1.1 rmind return false;
128 1.1 rmind }
129 1.1 rmind KASSERT(npf_iscached(npc, NPC_PORTS));
130 1.1 rmind in_port_t dport = ntohs(npc->npc_dport);
131 1.1 rmind if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
132 1.1 rmind return false;
133 1.1 rmind }
134 1.1 rmind
135 1.1 rmind /* Check for low TTL. */
136 1.3 rmind offby = offsetof(struct ip, ip_ttl);
137 1.3 rmind if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint8_t), &ttl))
138 1.1 rmind return false;
139 1.3 rmind if (ttl > TR_MAX_TTL)
140 1.1 rmind return false;
141 1.1 rmind
142 1.1 rmind /* Associate ALG with translation entry. */
143 1.1 rmind npf_nat_t *nt = ntptr;
144 1.1 rmind npf_nat_setalg(nt, alg_icmp, 0);
145 1.1 rmind return true;
146 1.1 rmind }
147 1.1 rmind
148 1.1 rmind /*
149 1.1 rmind * npf_icmp_uniqid: retrieve unique identifiers - either ICMP query ID
150 1.1 rmind * or TCP/UDP ports of the original packet, which is embedded.
151 1.1 rmind */
152 1.1 rmind static inline bool
153 1.1 rmind npf_icmp_uniqid(const int type, npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
154 1.1 rmind {
155 1.1 rmind u_int offby;
156 1.1 rmind
157 1.1 rmind /* Per RFC 792. */
158 1.1 rmind switch (type) {
159 1.1 rmind case ICMP_UNREACH:
160 1.1 rmind case ICMP_SOURCEQUENCH:
161 1.1 rmind case ICMP_REDIRECT:
162 1.1 rmind case ICMP_TIMXCEED:
163 1.1 rmind case ICMP_PARAMPROB:
164 1.1 rmind /* Should contain original IP header. */
165 1.1 rmind offby = offsetof(struct icmp, icmp_ip);
166 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL) {
167 1.1 rmind return false;
168 1.1 rmind }
169 1.1 rmind /* Fetch into the cache. */
170 1.1 rmind if (!npf_ip4_proto(npc, nbuf, n_ptr)) {
171 1.1 rmind return false;
172 1.1 rmind }
173 1.1 rmind const int proto = npc->npc_proto;
174 1.1 rmind if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
175 1.1 rmind return false;
176 1.1 rmind }
177 1.1 rmind if (!npf_fetch_ip4addrs(npc, nbuf, n_ptr)) {
178 1.1 rmind return false;
179 1.1 rmind }
180 1.1 rmind if (!npf_fetch_ports(npc, nbuf, n_ptr, proto)) {
181 1.1 rmind return false;
182 1.1 rmind }
183 1.1 rmind return true;
184 1.1 rmind
185 1.1 rmind case ICMP_ECHOREPLY:
186 1.1 rmind case ICMP_ECHO:
187 1.1 rmind case ICMP_TSTAMP:
188 1.1 rmind case ICMP_TSTAMPREPLY:
189 1.1 rmind case ICMP_IREQ:
190 1.1 rmind case ICMP_IREQREPLY:
191 1.1 rmind /* Should contain ICMP query ID. */
192 1.1 rmind offby = offsetof(struct icmp, icmp_id);
193 1.3 rmind if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint16_t),
194 1.1 rmind &npc->npc_icmp_id)) {
195 1.1 rmind return false;
196 1.1 rmind }
197 1.1 rmind npc->npc_info |= NPC_ICMP_ID;
198 1.1 rmind return true;
199 1.1 rmind default:
200 1.1 rmind break;
201 1.1 rmind }
202 1.1 rmind /* No unique IDs. */
203 1.1 rmind return false;
204 1.1 rmind }
205 1.1 rmind
206 1.1 rmind /*
207 1.1 rmind * npfa_icmp_session: ALG session inspector, determines unique identifiers.
208 1.1 rmind */
209 1.1 rmind static bool
210 1.1 rmind npfa_icmp_session(npf_cache_t *npc, nbuf_t *nbuf, void *keyptr)
211 1.1 rmind {
212 1.1 rmind npf_cache_t *key = keyptr;
213 1.1 rmind void *n_ptr;
214 1.1 rmind
215 1.1 rmind /* ICMP? Get unique identifiers from ICMP packet. */
216 1.1 rmind if (npc->npc_proto != IPPROTO_ICMP) {
217 1.1 rmind return false;
218 1.1 rmind }
219 1.1 rmind KASSERT(npf_iscached(npc, NPC_IP46 | NPC_ICMP));
220 1.1 rmind key->npc_info = NPC_ICMP;
221 1.1 rmind
222 1.1 rmind /* Advance to ICMP header. */
223 1.1 rmind n_ptr = nbuf_dataptr(nbuf);
224 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, npc->npc_hlen)) == NULL) {
225 1.1 rmind return false;
226 1.1 rmind }
227 1.1 rmind
228 1.1 rmind /* Fetch into the separate (key) cache. */
229 1.1 rmind if (!npf_icmp_uniqid(npc->npc_icmp_type, key, nbuf, n_ptr)) {
230 1.1 rmind return false;
231 1.1 rmind }
232 1.1 rmind
233 1.1 rmind if (npf_iscached(key, NPC_ICMP_ID)) {
234 1.1 rmind /* Construct the key. */
235 1.1 rmind key->npc_proto = npc->npc_proto;
236 1.1 rmind key->npc_dir = npc->npc_dir;
237 1.1 rmind /* Save IP addresses. */
238 1.1 rmind key->npc_srcip = npc->npc_srcip;
239 1.1 rmind key->npc_dstip = npc->npc_dstip;
240 1.1 rmind key->npc_info |= NPC_IP46 | NPC_ADDRS | NPC_PORTS;
241 1.1 rmind /* Fake ports with ICMP query IDs. */
242 1.1 rmind key->npc_sport = key->npc_icmp_id;
243 1.1 rmind key->npc_dport = key->npc_icmp_id;
244 1.1 rmind } else {
245 1.1 rmind in_addr_t addr;
246 1.1 rmind in_port_t port;
247 1.1 rmind /*
248 1.1 rmind * Embedded IP packet is the original of "forwards" stream.
249 1.1 rmind * We should imitate the "backwards" stream for inspection.
250 1.1 rmind */
251 1.1 rmind KASSERT(npf_iscached(key, NPC_IP46 | NPC_ADDRS | NPC_PORTS));
252 1.1 rmind addr = key->npc_srcip;
253 1.1 rmind port = key->npc_sport;
254 1.1 rmind key->npc_srcip = key->npc_dstip;
255 1.1 rmind key->npc_dstip = addr;
256 1.1 rmind key->npc_sport = key->npc_dport;
257 1.1 rmind key->npc_dport = port;
258 1.1 rmind }
259 1.1 rmind return true;
260 1.1 rmind }
261 1.1 rmind
262 1.1 rmind /*
263 1.1 rmind * npfa_icmp_natin: ALG inbound translation inspector, rewrite IP address
264 1.1 rmind * in the IP header, which is embedded in ICMP packet.
265 1.1 rmind */
266 1.1 rmind static bool
267 1.1 rmind npfa_icmp_natin(npf_cache_t *npc, nbuf_t *nbuf, void *ntptr)
268 1.1 rmind {
269 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
270 1.1 rmind npf_cache_t enpc;
271 1.1 rmind u_int offby;
272 1.1 rmind uint16_t cksum;
273 1.1 rmind
274 1.1 rmind /* XXX: Duplicated work. */
275 1.1 rmind if (!npfa_icmp_session(npc, nbuf, &enpc)) {
276 1.1 rmind return false;
277 1.1 rmind }
278 1.1 rmind KASSERT(npf_iscached(&enpc, NPC_IP46 | NPC_ADDRS | NPC_PORTS));
279 1.1 rmind
280 1.1 rmind /* Advance to ICMP checksum and fetch it. */
281 1.1 rmind offby = npc->npc_hlen + offsetof(struct icmp, icmp_cksum);
282 1.3 rmind if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint16_t), &cksum)) {
283 1.1 rmind return false;
284 1.1 rmind }
285 1.1 rmind
286 1.1 rmind /* Save the data for checksum update later. */
287 1.1 rmind void *cnbuf = nbuf, *cnptr = n_ptr;
288 1.1 rmind uint16_t ecksum = enpc.npc_ipsum;
289 1.1 rmind
290 1.1 rmind /* Advance to the original IP header, which is embedded after ICMP. */
291 1.1 rmind offby = offsetof(struct icmp, icmp_ip) -
292 1.1 rmind offsetof(struct icmp, icmp_cksum);
293 1.1 rmind if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL) {
294 1.1 rmind return false;
295 1.1 rmind }
296 1.1 rmind
297 1.1 rmind /*
298 1.1 rmind * Rewrite source IP address and port of the embedded IP header,
299 1.1 rmind * which represents original packet - therefore passing PFIL_OUT.
300 1.1 rmind */
301 1.1 rmind npf_nat_t *nt = ntptr;
302 1.1 rmind in_addr_t addr;
303 1.1 rmind in_port_t port;
304 1.1 rmind
305 1.2 rmind npf_nat_getorig(nt, &addr, &port);
306 1.1 rmind
307 1.1 rmind if (!npf_rwrip(&enpc, nbuf, n_ptr, PFIL_OUT, addr)) {
308 1.1 rmind return false;
309 1.1 rmind }
310 1.1 rmind if (!npf_rwrport(&enpc, nbuf, n_ptr, PFIL_OUT, port, addr)) {
311 1.1 rmind return false;
312 1.1 rmind }
313 1.1 rmind
314 1.1 rmind /*
315 1.1 rmind * Fixup and update ICMP checksum.
316 1.1 rmind * Note: npf_rwrip() has updated the IP checksum.
317 1.1 rmind */
318 1.1 rmind cksum = npf_fixup32_cksum(cksum, enpc.npc_srcip, addr);
319 1.1 rmind cksum = npf_fixup16_cksum(cksum, enpc.npc_sport, port);
320 1.1 rmind cksum = npf_fixup16_cksum(cksum, ecksum, enpc.npc_ipsum);
321 1.1 rmind /* FIXME: Updated UDP/TCP checksum joins-in too., when != 0, sigh. */
322 1.1 rmind if (nbuf_store_datum(cnbuf, cnptr, sizeof(uint16_t), &cksum)){
323 1.1 rmind return false;
324 1.1 rmind }
325 1.1 rmind return true;
326 1.1 rmind }
327