ip_flow.c revision 1.15 1 1.15 mrg /* $NetBSD: ip_flow.c,v 1.15 2000/06/28 03:01:16 mrg Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by the 3am Software Foundry ("3am"). It was developed by Matt Thomas.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt * 3. All advertising materials mentioning features or use of this software
19 1.1 matt * must display the following acknowledgement:
20 1.1 matt * This product includes software developed by the NetBSD
21 1.1 matt * Foundation, Inc. and its contributors.
22 1.1 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 matt * contributors may be used to endorse or promote products derived
24 1.1 matt * from this software without specific prior written permission.
25 1.1 matt *
26 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
37 1.1 matt */
38 1.1 matt
39 1.1 matt #include <sys/param.h>
40 1.1 matt #include <sys/systm.h>
41 1.1 matt #include <sys/malloc.h>
42 1.1 matt #include <sys/mbuf.h>
43 1.1 matt #include <sys/domain.h>
44 1.1 matt #include <sys/protosw.h>
45 1.1 matt #include <sys/socket.h>
46 1.1 matt #include <sys/socketvar.h>
47 1.1 matt #include <sys/errno.h>
48 1.1 matt #include <sys/time.h>
49 1.1 matt #include <sys/kernel.h>
50 1.1 matt #include <sys/proc.h>
51 1.7 thorpej #include <sys/pool.h>
52 1.1 matt
53 1.15 mrg #include <uvm/uvm_extern.h>
54 1.15 mrg
55 1.1 matt #include <sys/sysctl.h>
56 1.1 matt
57 1.1 matt #include <net/if.h>
58 1.1 matt #include <net/if_dl.h>
59 1.1 matt #include <net/route.h>
60 1.1 matt #include <net/pfil.h>
61 1.1 matt
62 1.1 matt #include <netinet/in.h>
63 1.1 matt #include <netinet/in_systm.h>
64 1.1 matt #include <netinet/ip.h>
65 1.1 matt #include <netinet/in_pcb.h>
66 1.1 matt #include <netinet/in_var.h>
67 1.1 matt #include <netinet/ip_var.h>
68 1.1 matt
69 1.7 thorpej struct pool ipflow_pool;
70 1.7 thorpej
71 1.5 thorpej LIST_HEAD(ipflowhead, ipflow);
72 1.5 thorpej
73 1.1 matt #define IPFLOW_TIMER (5 * PR_SLOWHZ)
74 1.1 matt #define IPFLOW_HASHSIZE (1 << IPFLOW_HASHBITS)
75 1.5 thorpej
76 1.5 thorpej static struct ipflowhead ipflowtable[IPFLOW_HASHSIZE];
77 1.5 thorpej static struct ipflowhead ipflowlist;
78 1.1 matt static int ipflow_inuse;
79 1.5 thorpej
80 1.5 thorpej #define IPFLOW_INSERT(bucket, ipf) \
81 1.5 thorpej do { \
82 1.5 thorpej LIST_INSERT_HEAD((bucket), (ipf), ipf_hash); \
83 1.5 thorpej LIST_INSERT_HEAD(&ipflowlist, (ipf), ipf_list); \
84 1.5 thorpej } while (0)
85 1.5 thorpej
86 1.5 thorpej #define IPFLOW_REMOVE(ipf) \
87 1.5 thorpej do { \
88 1.5 thorpej LIST_REMOVE((ipf), ipf_hash); \
89 1.5 thorpej LIST_REMOVE((ipf), ipf_list); \
90 1.5 thorpej } while (0)
91 1.5 thorpej
92 1.3 matt #ifndef IPFLOW_MAX
93 1.1 matt #define IPFLOW_MAX 256
94 1.3 matt #endif
95 1.3 matt int ip_maxflows = IPFLOW_MAX;
96 1.1 matt
97 1.1 matt static unsigned
98 1.1 matt ipflow_hash(
99 1.1 matt struct in_addr dst,
100 1.1 matt struct in_addr src,
101 1.1 matt unsigned tos)
102 1.1 matt {
103 1.1 matt unsigned hash = tos;
104 1.1 matt int idx;
105 1.1 matt for (idx = 0; idx < 32; idx += IPFLOW_HASHBITS)
106 1.1 matt hash += (dst.s_addr >> (32 - idx)) + (src.s_addr >> idx);
107 1.1 matt return hash & (IPFLOW_HASHSIZE-1);
108 1.1 matt }
109 1.1 matt
110 1.1 matt static struct ipflow *
111 1.1 matt ipflow_lookup(
112 1.1 matt const struct ip *ip)
113 1.1 matt {
114 1.1 matt unsigned hash;
115 1.1 matt struct ipflow *ipf;
116 1.1 matt
117 1.1 matt hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
118 1.1 matt
119 1.5 thorpej ipf = LIST_FIRST(&ipflowtable[hash]);
120 1.1 matt while (ipf != NULL) {
121 1.1 matt if (ip->ip_dst.s_addr == ipf->ipf_dst.s_addr
122 1.1 matt && ip->ip_src.s_addr == ipf->ipf_src.s_addr
123 1.1 matt && ip->ip_tos == ipf->ipf_tos)
124 1.1 matt break;
125 1.5 thorpej ipf = LIST_NEXT(ipf, ipf_hash);
126 1.1 matt }
127 1.1 matt return ipf;
128 1.1 matt }
129 1.1 matt
130 1.7 thorpej void
131 1.7 thorpej ipflow_init()
132 1.7 thorpej {
133 1.7 thorpej int i;
134 1.7 thorpej
135 1.7 thorpej pool_init(&ipflow_pool, sizeof(struct ipflow), 0, 0, 0, "ipflowpl",
136 1.7 thorpej 0, NULL, NULL, M_IPFLOW);
137 1.7 thorpej
138 1.7 thorpej LIST_INIT(&ipflowlist);
139 1.7 thorpej for (i = 0; i < IPFLOW_HASHSIZE; i++)
140 1.7 thorpej LIST_INIT(&ipflowtable[i]);
141 1.7 thorpej }
142 1.7 thorpej
143 1.1 matt int
144 1.1 matt ipflow_fastforward(
145 1.1 matt struct mbuf *m)
146 1.1 matt {
147 1.1 matt struct ip *ip;
148 1.1 matt struct ipflow *ipf;
149 1.1 matt struct rtentry *rt;
150 1.1 matt int error;
151 1.6 sommerfe int iplen;
152 1.1 matt
153 1.1 matt /*
154 1.1 matt * Are we forwarding packets? Big enough for an IP packet?
155 1.1 matt */
156 1.3 matt if (!ipforwarding || ipflow_inuse == 0 || m->m_len < sizeof(struct ip))
157 1.1 matt return 0;
158 1.14 sommerfe
159 1.14 sommerfe /*
160 1.14 sommerfe * Was packet recieved as a link-level multicast or broadcast?
161 1.14 sommerfe * If so, don't try to fast forward..
162 1.14 sommerfe */
163 1.14 sommerfe if ((m->m_flags & (M_BCAST|M_MCAST)) != 0)
164 1.14 sommerfe return 0;
165 1.14 sommerfe
166 1.1 matt /*
167 1.1 matt * IP header with no option and valid version and length
168 1.1 matt */
169 1.1 matt ip = mtod(m, struct ip *);
170 1.6 sommerfe iplen = ntohs(ip->ip_len);
171 1.5 thorpej if (ip->ip_v != IPVERSION || ip->ip_hl != (sizeof(struct ip) >> 2) ||
172 1.13 proff iplen < sizeof(struct ip) || iplen > m->m_pkthdr.len)
173 1.1 matt return 0;
174 1.1 matt /*
175 1.1 matt * Find a flow.
176 1.1 matt */
177 1.1 matt if ((ipf = ipflow_lookup(ip)) == NULL)
178 1.1 matt return 0;
179 1.1 matt
180 1.1 matt /*
181 1.2 thorpej * Veryify the IP header checksum.
182 1.2 thorpej */
183 1.2 thorpej if (in_cksum(m, sizeof(struct ip)) != 0)
184 1.2 thorpej return 0;
185 1.2 thorpej
186 1.2 thorpej /*
187 1.1 matt * Route and interface still up?
188 1.1 matt */
189 1.1 matt rt = ipf->ipf_ro.ro_rt;
190 1.5 thorpej if ((rt->rt_flags & RTF_UP) == 0 ||
191 1.5 thorpej (rt->rt_ifp->if_flags & IFF_UP) == 0)
192 1.1 matt return 0;
193 1.1 matt
194 1.1 matt /*
195 1.1 matt * Packet size OK? TTL?
196 1.1 matt */
197 1.1 matt if (m->m_pkthdr.len > rt->rt_ifp->if_mtu || ip->ip_ttl <= IPTTLDEC)
198 1.1 matt return 0;
199 1.1 matt
200 1.1 matt /*
201 1.1 matt * Everything checks out and so we can forward this packet.
202 1.1 matt * Modify the TTL and incrementally change the checksum.
203 1.9 mycroft *
204 1.9 mycroft * This method of adding the checksum works on either endian CPU.
205 1.9 mycroft * If htons() is inlined, all the arithmetic is folded; otherwise
206 1.9 mycroft * the htons()s are combined by CSE due to the __const__ attribute.
207 1.1 matt */
208 1.1 matt ip->ip_ttl -= IPTTLDEC;
209 1.12 itohy if (ip->ip_sum >= (u_int16_t) ~htons(IPTTLDEC << 8))
210 1.11 mycroft ip->ip_sum -= ~htons(IPTTLDEC << 8);
211 1.8 mycroft else
212 1.2 thorpej ip->ip_sum += htons(IPTTLDEC << 8);
213 1.6 sommerfe
214 1.6 sommerfe /*
215 1.6 sommerfe * Trim the packet in case it's too long..
216 1.6 sommerfe */
217 1.6 sommerfe if (m->m_pkthdr.len > iplen) {
218 1.6 sommerfe if (m->m_len == m->m_pkthdr.len) {
219 1.6 sommerfe m->m_len = iplen;
220 1.6 sommerfe m->m_pkthdr.len = iplen;
221 1.6 sommerfe } else
222 1.6 sommerfe m_adj(m, iplen - m->m_pkthdr.len);
223 1.2 thorpej }
224 1.1 matt
225 1.1 matt /*
226 1.1 matt * Send the packet on it's way. All we can get back is ENOBUFS
227 1.1 matt */
228 1.1 matt ipf->ipf_uses++;
229 1.5 thorpej PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
230 1.5 thorpej if ((error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
231 1.5 thorpej &ipf->ipf_ro.ro_dst, rt)) != 0) {
232 1.1 matt if (error == ENOBUFS)
233 1.1 matt ipf->ipf_dropped++;
234 1.1 matt else
235 1.1 matt ipf->ipf_errors++;
236 1.1 matt }
237 1.1 matt return 1;
238 1.1 matt }
239 1.1 matt
240 1.1 matt static void
242 1.1 matt ipflow_addstats(
243 1.1 matt struct ipflow *ipf)
244 1.1 matt {
245 1.1 matt ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
246 1.1 matt ipstat.ips_cantforward += ipf->ipf_errors + ipf->ipf_dropped;
247 1.1 matt ipstat.ips_forward += ipf->ipf_uses;
248 1.1 matt ipstat.ips_fastforward += ipf->ipf_uses;
249 1.1 matt }
250 1.1 matt
251 1.1 matt static void
252 1.1 matt ipflow_free(
253 1.1 matt struct ipflow *ipf)
254 1.1 matt {
255 1.1 matt int s;
256 1.1 matt /*
257 1.1 matt * Remove the flow from the hash table (at elevated IPL).
258 1.1 matt * Once it's off the list, we can deal with it at normal
259 1.1 matt * network IPL.
260 1.1 matt */
261 1.5 thorpej s = splimp();
262 1.1 matt IPFLOW_REMOVE(ipf);
263 1.1 matt splx(s);
264 1.1 matt ipflow_addstats(ipf);
265 1.1 matt RTFREE(ipf->ipf_ro.ro_rt);
266 1.7 thorpej ipflow_inuse--;
267 1.1 matt pool_put(&ipflow_pool, ipf);
268 1.1 matt }
269 1.3 matt
270 1.1 matt struct ipflow *
271 1.3 matt ipflow_reap(
272 1.1 matt int just_one)
273 1.3 matt {
274 1.3 matt while (just_one || ipflow_inuse > ip_maxflows) {
275 1.3 matt struct ipflow *ipf, *maybe_ipf = NULL;
276 1.3 matt int s;
277 1.5 thorpej
278 1.5 thorpej ipf = LIST_FIRST(&ipflowlist);
279 1.5 thorpej while (ipf != NULL) {
280 1.5 thorpej /*
281 1.5 thorpej * If this no longer points to a valid route
282 1.5 thorpej * reclaim it.
283 1.5 thorpej */
284 1.5 thorpej if ((ipf->ipf_ro.ro_rt->rt_flags & RTF_UP) == 0)
285 1.5 thorpej goto done;
286 1.5 thorpej /*
287 1.5 thorpej * choose the one that's been least recently
288 1.5 thorpej * used or has had the least uses in the
289 1.5 thorpej * last 1.5 intervals.
290 1.5 thorpej */
291 1.5 thorpej if (maybe_ipf == NULL ||
292 1.5 thorpej ipf->ipf_timer < maybe_ipf->ipf_timer ||
293 1.5 thorpej (ipf->ipf_timer == maybe_ipf->ipf_timer &&
294 1.5 thorpej ipf->ipf_last_uses + ipf->ipf_uses <
295 1.5 thorpej maybe_ipf->ipf_last_uses +
296 1.5 thorpej maybe_ipf->ipf_uses))
297 1.5 thorpej maybe_ipf = ipf;
298 1.1 matt ipf = LIST_NEXT(ipf, ipf_list);
299 1.3 matt }
300 1.3 matt ipf = maybe_ipf;
301 1.3 matt done:
302 1.3 matt /*
303 1.3 matt * Remove the entry from the flow table.
304 1.3 matt */
305 1.5 thorpej s = splimp();
306 1.3 matt IPFLOW_REMOVE(ipf);
307 1.3 matt splx(s);
308 1.3 matt ipflow_addstats(ipf);
309 1.3 matt RTFREE(ipf->ipf_ro.ro_rt);
310 1.3 matt if (just_one)
311 1.7 thorpej return ipf;
312 1.3 matt pool_put(&ipflow_pool, ipf);
313 1.1 matt ipflow_inuse--;
314 1.3 matt }
315 1.1 matt return NULL;
316 1.1 matt }
317 1.1 matt
318 1.1 matt void
319 1.1 matt ipflow_slowtimo(
320 1.1 matt void)
321 1.5 thorpej {
322 1.2 thorpej struct ipflow *ipf, *next_ipf;
323 1.5 thorpej
324 1.5 thorpej ipf = LIST_FIRST(&ipflowlist);
325 1.5 thorpej while (ipf != NULL) {
326 1.5 thorpej next_ipf = LIST_NEXT(ipf, ipf_list);
327 1.5 thorpej if (PRT_SLOW_ISEXPIRED(ipf->ipf_timer)) {
328 1.5 thorpej ipflow_free(ipf);
329 1.5 thorpej } else {
330 1.5 thorpej ipf->ipf_last_uses = ipf->ipf_uses;
331 1.5 thorpej ipf->ipf_ro.ro_rt->rt_use += ipf->ipf_uses;
332 1.5 thorpej ipstat.ips_forward += ipf->ipf_uses;
333 1.5 thorpej ipstat.ips_fastforward += ipf->ipf_uses;
334 1.1 matt ipf->ipf_uses = 0;
335 1.5 thorpej }
336 1.1 matt ipf = next_ipf;
337 1.1 matt }
338 1.1 matt }
339 1.1 matt
340 1.1 matt void
341 1.1 matt ipflow_create(
342 1.1 matt const struct route *ro,
343 1.1 matt struct mbuf *m)
344 1.1 matt {
345 1.1 matt const struct ip *const ip = mtod(m, struct ip *);
346 1.1 matt struct ipflow *ipf;
347 1.1 matt unsigned hash;
348 1.1 matt int s;
349 1.1 matt
350 1.1 matt /*
351 1.1 matt * Don't create cache entries for ICMP messages.
352 1.3 matt */
353 1.1 matt if (ip_maxflows == 0 || ip->ip_p == IPPROTO_ICMP)
354 1.1 matt return;
355 1.1 matt /*
356 1.1 matt * See if an existing flow struct exists. If so remove it from it's
357 1.1 matt * list and free the old route. If not, try to malloc a new one
358 1.1 matt * (if we aren't at our limit).
359 1.1 matt */
360 1.1 matt ipf = ipflow_lookup(ip);
361 1.3 matt if (ipf == NULL) {
362 1.3 matt if (ipflow_inuse >= ip_maxflows) {
363 1.1 matt ipf = ipflow_reap(1);
364 1.7 thorpej } else {
365 1.1 matt ipf = pool_get(&ipflow_pool, PR_NOWAIT);
366 1.1 matt if (ipf == NULL)
367 1.1 matt return;
368 1.1 matt ipflow_inuse++;
369 1.1 matt }
370 1.1 matt bzero((caddr_t) ipf, sizeof(*ipf));
371 1.1 matt } else {
372 1.5 thorpej s = splimp();
373 1.1 matt IPFLOW_REMOVE(ipf);
374 1.1 matt splx(s);
375 1.1 matt ipflow_addstats(ipf);
376 1.1 matt RTFREE(ipf->ipf_ro.ro_rt);
377 1.1 matt ipf->ipf_uses = ipf->ipf_last_uses = 0;
378 1.1 matt ipf->ipf_errors = ipf->ipf_dropped = 0;
379 1.1 matt }
380 1.1 matt
381 1.1 matt /*
382 1.1 matt * Fill in the updated information.
383 1.1 matt */
384 1.1 matt ipf->ipf_ro = *ro;
385 1.1 matt ro->ro_rt->rt_refcnt++;
386 1.1 matt ipf->ipf_dst = ip->ip_dst;
387 1.1 matt ipf->ipf_src = ip->ip_src;
388 1.5 thorpej ipf->ipf_tos = ip->ip_tos;
389 1.1 matt PRT_SLOW_ARM(ipf->ipf_timer, IPFLOW_TIMER);
390 1.1 matt ipf->ipf_start = time.tv_sec;
391 1.1 matt /*
392 1.1 matt * Insert into the approriate bucket of the flow table.
393 1.1 matt */
394 1.1 matt hash = ipflow_hash(ip->ip_dst, ip->ip_src, ip->ip_tos);
395 1.5 thorpej s = splimp();
396 1.1 matt IPFLOW_INSERT(&ipflowtable[hash], ipf);
397 1.1 matt splx(s);
398 }
399