]> git.ozlabs.org Git - ppp.git/blob - modules/bpf_filter.c
use netbsd-1.1 vers
[ppp.git] / modules / bpf_filter.c
1 /*      From NetBSD: bpf_filter.c,v 1.12 1996/02/13 22:00:00 christos Exp */
2
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from the Stanford/CMU enet packet filter,
8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10  * Berkeley Laboratory.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the University of
23  *      California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *      From: @(#)bpf_filter.c  8.1 (Berkeley) 6/10/93
41  *      $Id: bpf_filter.c,v 1.1 1996/04/04 02:45:45 paulus Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/stream.h>
47 #include <net/ppp_defs.h>
48 #include "ppp_mod.h"
49
50 #ifdef SVR4
51 #ifndef __GNUC__
52 #include <sys/byteorder.h>      /* for ntohl, etc. */
53 #else
54 /* make sure we don't get the gnu "fixed" one! */
55 #include "/usr/include/sys/byteorder.h"
56 #endif
57 #endif
58
59 #ifdef OSF1
60 #include <net/net_globals.h>
61 #endif
62 #include <netinet/in.h>
63
64 #ifdef AIX4
65 #define _NETINET_IN_SYSTM_H_
66 typedef u_long  n_long;
67 #else
68 #include <netinet/in_systm.h>
69 #endif
70
71 #if !(defined(__i386__) || defined(__m68k__))   /* any others? */
72 #define BPF_ALIGN
73 #endif
74
75 #ifndef BPF_ALIGN
76 #define EXTRACT_SHORT(p)        ((ushort)ntohs(*(ushort *)p))
77 #define EXTRACT_LONG(p)         (ntohl(*(uint *)p))
78 #else
79 #define EXTRACT_SHORT(p)\
80         ((ushort)\
81                 ((ushort)*((u_char *)p+0)<<8|\
82                  (ushort)*((u_char *)p+1)<<0))
83 #define EXTRACT_LONG(p)\
84                 ((uint)*((u_char *)p+0)<<24|\
85                  (uint)*((u_char *)p+1)<<16|\
86                  (uint)*((u_char *)p+2)<<8|\
87                  (uint)*((u_char *)p+3)<<0)
88 #endif
89
90 #ifdef _KERNEL
91 #define MINDEX(len, m, k) \
92 { \
93         len = m->b_wptr - m->b_rptr; \
94         while (k >= len) { \
95                 k -= len; \
96                 m = m->b_cont; \
97                 if (m == 0) \
98                         return 0; \
99                 len = m->b_wptr - m->b_rptr; \
100         } \
101 }
102
103 static int m_xword __P((mblk_t *, int, int *));
104 static int m_xhalf __P((mblk_t *, int, int *));
105
106 static int
107 m_xword(m, k, err)
108         register mblk_t *m;
109         register int k, *err;
110 {
111         register int len;
112         register uchar_t *cp, *np;
113         register mblk_t *m0;
114
115         MINDEX(len, m, k);
116         cp = m->b_rptr + k;
117         if (len - k >= 4) {
118                 *err = 0;
119                 return EXTRACT_LONG(cp);
120         }
121         m0 = m->b_cont;
122         if (m0 == 0 || m0->b_wptr - m0->b_rptr + len - k < 4)
123                 goto bad;
124         *err = 0;
125         np = m0->b_rptr;
126         switch (len - k) {
127
128         case 1:
129                 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
130
131         case 2:
132                 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
133
134         default:
135                 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
136         }
137     bad:
138         *err = 1;
139         return 0;
140 }
141
142 static int
143 m_xhalf(m, k, err)
144         register mblk_t *m;
145         register int k, *err;
146 {
147         register int len;
148         register uchar_t *cp;
149         register mblk_t *m0;
150
151         MINDEX(len, m, k);
152         cp = m->b_rptr + k;
153         if (len - k >= 2) {
154                 *err = 0;
155                 return EXTRACT_SHORT(cp);
156         }
157         m0 = m->b_cont;
158         if (m0 == 0)
159                 goto bad;
160         *err = 0;
161         return (cp[0] << 8) | m0->b_rptr[0];
162  bad:
163         *err = 1;
164         return 0;
165 }
166 #endif
167
168 #include <net/bpf.h>
169
170 /*
171  * Execute the filter program starting at pc on the packet p
172  * wirelen is the length of the original packet
173  * buflen is the amount of data present
174  */
175 uint
176 bpf_filter(pc, p, wirelen, buflen)
177         register struct bpf_insn *pc;
178         register uchar_t *p;
179         uint wirelen;
180         register uint buflen;
181 {
182         register uint A = 0, X = 0;
183         register int k;
184         int mem[BPF_MEMWORDS];
185
186         if (pc == 0)
187                 /*
188                  * No filter means accept all.
189                  */
190                 return (uint)-1;
191         --pc;
192         while (1) {
193                 ++pc;
194                 switch (pc->code) {
195
196                 default:
197 #ifdef _KERNEL
198                         return 0;
199 #else
200                         abort();
201 #endif                  
202                 case BPF_RET|BPF_K:
203                         return (uint)pc->k;
204
205                 case BPF_RET|BPF_A:
206                         return (uint)A;
207
208                 case BPF_LD|BPF_W|BPF_ABS:
209                         k = pc->k;
210                         if (k + sizeof(int) > buflen) {
211 #ifdef _KERNEL
212                                 int merr;
213
214                                 if (buflen != 0)
215                                         return 0;
216                                 A = m_xword((mblk_t *)p, k, &merr);
217                                 if (merr != 0)
218                                         return 0;
219                                 continue;
220 #else
221                                 return 0;
222 #endif
223                         }
224                         A = EXTRACT_LONG(&p[k]);
225                         continue;
226
227                 case BPF_LD|BPF_H|BPF_ABS:
228                         k = pc->k;
229                         if (k + sizeof(short int) > buflen) {
230 #ifdef _KERNEL
231                                 int merr;
232
233                                 if (buflen != 0)
234                                         return 0;
235                                 A = m_xhalf((mblk_t *)p, k, &merr);
236                                 continue;
237 #else
238                                 return 0;
239 #endif
240                         }
241                         A = EXTRACT_SHORT(&p[k]);
242                         continue;
243
244                 case BPF_LD|BPF_B|BPF_ABS:
245                         k = pc->k;
246                         if (k >= buflen) {
247 #ifdef _KERNEL
248                                 register mblk_t *m;
249                                 register int len;
250
251                                 if (buflen != 0)
252                                         return 0;
253                                 m = (mblk_t *)p;
254                                 MINDEX(len, m, k);
255                                 A = m->b_rptr[k];
256                                 continue;
257 #else
258                                 return 0;
259 #endif
260                         }
261                         A = p[k];
262                         continue;
263
264                 case BPF_LD|BPF_W|BPF_LEN:
265                         A = wirelen;
266                         continue;
267
268                 case BPF_LDX|BPF_W|BPF_LEN:
269                         X = wirelen;
270                         continue;
271
272                 case BPF_LD|BPF_W|BPF_IND:
273                         k = X + pc->k;
274                         if (k + sizeof(int) > buflen) {
275 #ifdef _KERNEL
276                                 int merr;
277
278                                 if (buflen != 0)
279                                         return 0;
280                                 A = m_xword((mblk_t *)p, k, &merr);
281                                 if (merr != 0)
282                                         return 0;
283                                 continue;
284 #else
285                                 return 0;
286 #endif
287                         }
288                         A = EXTRACT_LONG(&p[k]);
289                         continue;
290
291                 case BPF_LD|BPF_H|BPF_IND:
292                         k = X + pc->k;
293                         if (k + sizeof(short int) > buflen) {
294 #ifdef _KERNEL
295                                 int merr;
296
297                                 if (buflen != 0)
298                                         return 0;
299                                 A = m_xhalf((mblk_t *)p, k, &merr);
300                                 if (merr != 0)
301                                         return 0;
302                                 continue;
303 #else
304                                 return 0;
305 #endif
306                         }
307                         A = EXTRACT_SHORT(&p[k]);
308                         continue;
309
310                 case BPF_LD|BPF_B|BPF_IND:
311                         k = X + pc->k;
312                         if (k >= buflen) {
313 #ifdef _KERNEL
314                                 register mblk_t *m;
315                                 register int len;
316
317                                 if (buflen != 0)
318                                         return 0;
319                                 m = (mblk_t *)p;
320                                 MINDEX(len, m, k);
321                                 A = m->b_rptr[k];
322                                 continue;
323 #else
324                                 return 0;
325 #endif
326                         }
327                         A = p[k];
328                         continue;
329
330                 case BPF_LDX|BPF_MSH|BPF_B:
331                         k = pc->k;
332                         if (k >= buflen) {
333 #ifdef _KERNEL
334                                 register mblk_t *m;
335                                 register int len;
336
337                                 if (buflen != 0)
338                                         return 0;
339                                 m = (mblk_t *)p;
340                                 MINDEX(len, m, k);
341                                 X = (m->b_rptr[k] & 0xf) << 2;
342                                 continue;
343 #else
344                                 return 0;
345 #endif
346                         }
347                         X = (p[pc->k] & 0xf) << 2;
348                         continue;
349
350                 case BPF_LD|BPF_IMM:
351                         A = pc->k;
352                         continue;
353
354                 case BPF_LDX|BPF_IMM:
355                         X = pc->k;
356                         continue;
357
358                 case BPF_LD|BPF_MEM:
359                         A = mem[pc->k];
360                         continue;
361                         
362                 case BPF_LDX|BPF_MEM:
363                         X = mem[pc->k];
364                         continue;
365
366                 case BPF_ST:
367                         mem[pc->k] = A;
368                         continue;
369
370                 case BPF_STX:
371                         mem[pc->k] = X;
372                         continue;
373
374                 case BPF_JMP|BPF_JA:
375                         pc += pc->k;
376                         continue;
377
378                 case BPF_JMP|BPF_JGT|BPF_K:
379                         pc += (A > pc->k) ? pc->jt : pc->jf;
380                         continue;
381
382                 case BPF_JMP|BPF_JGE|BPF_K:
383                         pc += (A >= pc->k) ? pc->jt : pc->jf;
384                         continue;
385
386                 case BPF_JMP|BPF_JEQ|BPF_K:
387                         pc += (A == pc->k) ? pc->jt : pc->jf;
388                         continue;
389
390                 case BPF_JMP|BPF_JSET|BPF_K:
391                         pc += (A & pc->k) ? pc->jt : pc->jf;
392                         continue;
393
394                 case BPF_JMP|BPF_JGT|BPF_X:
395                         pc += (A > X) ? pc->jt : pc->jf;
396                         continue;
397
398                 case BPF_JMP|BPF_JGE|BPF_X:
399                         pc += (A >= X) ? pc->jt : pc->jf;
400                         continue;
401
402                 case BPF_JMP|BPF_JEQ|BPF_X:
403                         pc += (A == X) ? pc->jt : pc->jf;
404                         continue;
405
406                 case BPF_JMP|BPF_JSET|BPF_X:
407                         pc += (A & X) ? pc->jt : pc->jf;
408                         continue;
409
410                 case BPF_ALU|BPF_ADD|BPF_X:
411                         A += X;
412                         continue;
413                         
414                 case BPF_ALU|BPF_SUB|BPF_X:
415                         A -= X;
416                         continue;
417                         
418                 case BPF_ALU|BPF_MUL|BPF_X:
419                         A *= X;
420                         continue;
421                         
422                 case BPF_ALU|BPF_DIV|BPF_X:
423                         if (X == 0)
424                                 return 0;
425                         A /= X;
426                         continue;
427                         
428                 case BPF_ALU|BPF_AND|BPF_X:
429                         A &= X;
430                         continue;
431                         
432                 case BPF_ALU|BPF_OR|BPF_X:
433                         A |= X;
434                         continue;
435
436                 case BPF_ALU|BPF_LSH|BPF_X:
437                         A <<= X;
438                         continue;
439
440                 case BPF_ALU|BPF_RSH|BPF_X:
441                         A >>= X;
442                         continue;
443
444                 case BPF_ALU|BPF_ADD|BPF_K:
445                         A += pc->k;
446                         continue;
447                         
448                 case BPF_ALU|BPF_SUB|BPF_K:
449                         A -= pc->k;
450                         continue;
451                         
452                 case BPF_ALU|BPF_MUL|BPF_K:
453                         A *= pc->k;
454                         continue;
455                         
456                 case BPF_ALU|BPF_DIV|BPF_K:
457                         A /= pc->k;
458                         continue;
459                         
460                 case BPF_ALU|BPF_AND|BPF_K:
461                         A &= pc->k;
462                         continue;
463                         
464                 case BPF_ALU|BPF_OR|BPF_K:
465                         A |= pc->k;
466                         continue;
467
468                 case BPF_ALU|BPF_LSH|BPF_K:
469                         A <<= pc->k;
470                         continue;
471
472                 case BPF_ALU|BPF_RSH|BPF_K:
473                         A >>= pc->k;
474                         continue;
475
476                 case BPF_ALU|BPF_NEG:
477                         A = -A;
478                         continue;
479
480                 case BPF_MISC|BPF_TAX:
481                         X = A;
482                         continue;
483
484                 case BPF_MISC|BPF_TXA:
485                         A = X;
486                         continue;
487                 }
488         }
489 }
490
491 #ifdef _KERNEL
492 /*
493  * Return true if the 'fcode' is a valid filter program.
494  * The constraints are that each jump be forward and to a valid
495  * code.  The code must terminate with either an accept or reject. 
496  * 'valid' is an array for use by the routine (it must be at least
497  * 'len' bytes long).  
498  *
499  * The kernel needs to be able to verify an application's filter code.
500  * Otherwise, a bogus program could easily crash the system.
501  */
502 int
503 bpf_validate(f, len)
504         struct bpf_insn *f;
505         int len;
506 {
507         register int i;
508         register struct bpf_insn *p;
509
510         for (i = 0; i < len; ++i) {
511                 /*
512                  * Check that that jumps are forward, and within 
513                  * the code block.
514                  */
515                 p = &f[i];
516                 if (BPF_CLASS(p->code) == BPF_JMP) {
517                         register int from = i + 1;
518
519                         if (BPF_OP(p->code) == BPF_JA) {
520                                 if (from + p->k >= len)
521                                         return 0;
522                         }
523                         else if (from + p->jt >= len || from + p->jf >= len)
524                                 return 0;
525                 }
526                 /*
527                  * Check that memory operations use valid addresses.
528                  */
529                 if ((BPF_CLASS(p->code) == BPF_ST ||
530                      (BPF_CLASS(p->code) == BPF_LD && 
531                       (p->code & 0xe0) == BPF_MEM)) &&
532                     (p->k >= BPF_MEMWORDS || p->k < 0))
533                         return 0;
534                 /*
535                  * Check for constant division by 0.
536                  */
537                 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
538                         return 0;
539         }
540         return BPF_CLASS(f[len - 1].code) == BPF_RET;
541 }
542 #endif