import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / net / core / filter.c
1 /*
2  * Linux Socket Filter - Kernel level socket filtering
3  *
4  * Author:
5  *     Jay Schulist <jschlst@samba.org>
6  *
7  * Based on the design of:
8  *     - The Berkeley Packet Filter
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version
13  * 2 of the License, or (at your option) any later version.
14  *
15  * Andi Kleen - Fix a few bad bugs and races.
16  */
17
18 #include <linux/config.h>
19 #if defined(CONFIG_FILTER)
20
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_packet.h>
31 #include <net/ip.h>
32 #include <net/protocol.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <linux/errno.h>
36 #include <linux/timer.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include <linux/filter.h>
40
41 /* No hurry in this branch */
42
43 static u8 *load_pointer(struct sk_buff *skb, int k)
44 {
45         u8 *ptr = NULL;
46
47         if (k>=SKF_NET_OFF)
48                 ptr = skb->nh.raw + k - SKF_NET_OFF;
49         else if (k>=SKF_LL_OFF)
50                 ptr = skb->mac.raw + k - SKF_LL_OFF;
51
52         if (ptr >= skb->head && ptr < skb->tail)
53                 return ptr;
54         return NULL;
55 }
56
57 /**
58  *      sk_run_filter   -       run a filter on a socket
59  *      @skb: buffer to run the filter on
60  *      @filter: filter to apply
61  *      @flen: length of filter
62  *
63  * Decode and apply filter instructions to the skb->data.
64  * Return length to keep, 0 for none. skb is the data we are
65  * filtering, filter is the array of filter instructions, and
66  * len is the number of filter blocks in the array.
67  */
68  
69 int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
70 {
71         unsigned char *data = skb->data;
72         /* len is UNSIGNED. Byte wide insns relies only on implicit
73            type casts to prevent reading arbitrary memory locations.
74          */
75         unsigned int len = skb->len-skb->data_len;
76         struct sock_filter *fentry;     /* We walk down these */
77         u32 A = 0;                      /* Accumulator */
78         u32 X = 0;                      /* Index Register */
79         u32 mem[BPF_MEMWORDS];          /* Scratch Memory Store */
80         int k;
81         int pc;
82
83         /*
84          * Process array of filter instructions.
85          */
86
87         for(pc = 0; pc < flen; pc++)
88         {
89                 fentry = &filter[pc];
90                         
91                 switch(fentry->code)
92                 {
93                         case BPF_ALU|BPF_ADD|BPF_X:
94                                 A += X;
95                                 continue;
96
97                         case BPF_ALU|BPF_ADD|BPF_K:
98                                 A += fentry->k;
99                                 continue;
100
101                         case BPF_ALU|BPF_SUB|BPF_X:
102                                 A -= X;
103                                 continue;
104
105                         case BPF_ALU|BPF_SUB|BPF_K:
106                                 A -= fentry->k;
107                                 continue;
108
109                         case BPF_ALU|BPF_MUL|BPF_X:
110                                 A *= X;
111                                 continue;
112
113                         case BPF_ALU|BPF_MUL|BPF_K:
114                                 A *= fentry->k;
115                                 continue;
116
117                         case BPF_ALU|BPF_DIV|BPF_X:
118                                 if(X == 0)
119                                         return (0);
120                                 A /= X;
121                                 continue;
122
123                         case BPF_ALU|BPF_DIV|BPF_K:
124                                 if(fentry->k == 0)
125                                         return (0);
126                                 A /= fentry->k;
127                                 continue;
128
129                         case BPF_ALU|BPF_AND|BPF_X:
130                                 A &= X;
131                                 continue;
132
133                         case BPF_ALU|BPF_AND|BPF_K:
134                                 A &= fentry->k;
135                                 continue;
136
137                         case BPF_ALU|BPF_OR|BPF_X:
138                                 A |= X;
139                                 continue;
140
141                         case BPF_ALU|BPF_OR|BPF_K:
142                                 A |= fentry->k;
143                                 continue;
144
145                         case BPF_ALU|BPF_LSH|BPF_X:
146                                 A <<= X;
147                                 continue;
148
149                         case BPF_ALU|BPF_LSH|BPF_K:
150                                 A <<= fentry->k;
151                                 continue;
152
153                         case BPF_ALU|BPF_RSH|BPF_X:
154                                 A >>= X;
155                                 continue;
156
157                         case BPF_ALU|BPF_RSH|BPF_K:
158                                 A >>= fentry->k;
159                                 continue;
160
161                         case BPF_ALU|BPF_NEG:
162                                 A = -A;
163                                 continue;
164
165                         case BPF_JMP|BPF_JA:
166                                 pc += fentry->k;
167                                 continue;
168
169                         case BPF_JMP|BPF_JGT|BPF_K:
170                                 pc += (A > fentry->k) ? fentry->jt : fentry->jf;
171                                 continue;
172
173                         case BPF_JMP|BPF_JGE|BPF_K:
174                                 pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
175                                 continue;
176
177                         case BPF_JMP|BPF_JEQ|BPF_K:
178                                 pc += (A == fentry->k) ? fentry->jt : fentry->jf;
179                                 continue;
180
181                         case BPF_JMP|BPF_JSET|BPF_K:
182                                 pc += (A & fentry->k) ? fentry->jt : fentry->jf;
183                                 continue;
184
185                         case BPF_JMP|BPF_JGT|BPF_X:
186                                 pc += (A > X) ? fentry->jt : fentry->jf;
187                                 continue;
188
189                         case BPF_JMP|BPF_JGE|BPF_X:
190                                 pc += (A >= X) ? fentry->jt : fentry->jf;
191                                 continue;
192
193                         case BPF_JMP|BPF_JEQ|BPF_X:
194                                 pc += (A == X) ? fentry->jt : fentry->jf;
195                                 continue;
196
197                         case BPF_JMP|BPF_JSET|BPF_X:
198                                 pc += (A & X) ? fentry->jt : fentry->jf;
199                                 continue;
200
201                         case BPF_LD|BPF_W|BPF_ABS:
202                                 k = fentry->k;
203 load_w:
204                                 if(k >= 0 && (unsigned int)(k+sizeof(u32)) <= len) {
205                                         A = ntohl(*(u32*)&data[k]);
206                                         continue;
207                                 }
208                                 if (k<0) {
209                                         u8 *ptr;
210
211                                         if (k>=SKF_AD_OFF)
212                                                 break;
213                                         if ((ptr = load_pointer(skb, k)) != NULL) {
214                                                 A = ntohl(*(u32*)ptr);
215                                                 continue;
216                                         }
217                                 } else {
218                                         u32 tmp;
219                                         if (!skb_copy_bits(skb, k, &tmp, 4)) {
220                                                 A = ntohl(tmp);
221                                                 continue;
222                                         }
223                                 }
224                                 return 0;
225
226                         case BPF_LD|BPF_H|BPF_ABS:
227                                 k = fentry->k;
228 load_h:
229                                 if(k >= 0 && (unsigned int) (k + sizeof(u16)) <= len) {
230                                         A = ntohs(*(u16*)&data[k]);
231                                         continue;
232                                 }
233                                 if (k<0) {
234                                         u8 *ptr;
235
236                                         if (k>=SKF_AD_OFF)
237                                                 break;
238                                         if ((ptr = load_pointer(skb, k)) != NULL) {
239                                                 A = ntohs(*(u16*)ptr);
240                                                 continue;
241                                         }
242                                 } else {
243                                         u16 tmp;
244                                         if (!skb_copy_bits(skb, k, &tmp, 2)) {
245                                                 A = ntohs(tmp);
246                                                 continue;
247                                         }
248                                 }
249                                 return 0;
250
251                         case BPF_LD|BPF_B|BPF_ABS:
252                                 k = fentry->k;
253 load_b:
254                                 if(k >= 0 && (unsigned int)k < len) {
255                                         A = data[k];
256                                         continue;
257                                 }
258                                 if (k<0) {
259                                         u8 *ptr;
260
261                                         if (k>=SKF_AD_OFF)
262                                                 break;
263                                         if ((ptr = load_pointer(skb, k)) != NULL) {
264                                                 A = *ptr;
265                                                 continue;
266                                         }
267                                 } else {
268                                         u8 tmp;
269                                         if (!skb_copy_bits(skb, k, &tmp, 1)) {
270                                                 A = tmp;
271                                                 continue;
272                                         }
273                                 }
274                                 return 0;
275
276                         case BPF_LD|BPF_W|BPF_LEN:
277                                 A = len;
278                                 continue;
279
280                         case BPF_LDX|BPF_W|BPF_LEN:
281                                 X = len;
282                                 continue;
283
284                         case BPF_LD|BPF_W|BPF_IND:
285                                 k = X + fentry->k;
286                                 goto load_w;
287
288                        case BPF_LD|BPF_H|BPF_IND:
289                                 k = X + fentry->k;
290                                 goto load_h;
291
292                        case BPF_LD|BPF_B|BPF_IND:
293                                 k = X + fentry->k;
294                                 goto load_b;
295
296                         case BPF_LDX|BPF_B|BPF_MSH:
297                                 k = fentry->k;
298                                 if(k >= 0 && (unsigned int)k >= len)
299                                         return (0);
300                                 X = (data[k] & 0xf) << 2;
301                                 continue;
302
303                         case BPF_LD|BPF_IMM:
304                                 A = fentry->k;
305                                 continue;
306
307                         case BPF_LDX|BPF_IMM:
308                                 X = fentry->k;
309                                 continue;
310
311                         case BPF_LD|BPF_MEM:
312                                 A = mem[fentry->k];
313                                 continue;
314
315                         case BPF_LDX|BPF_MEM:
316                                 X = mem[fentry->k];
317                                 continue;
318
319                         case BPF_MISC|BPF_TAX:
320                                 X = A;
321                                 continue;
322
323                         case BPF_MISC|BPF_TXA:
324                                 A = X;
325                                 continue;
326
327                         case BPF_RET|BPF_K:
328                                 return ((unsigned int)fentry->k);
329
330                         case BPF_RET|BPF_A:
331                                 return ((unsigned int)A);
332
333                         case BPF_ST:
334                                 mem[fentry->k] = A;
335                                 continue;
336
337                         case BPF_STX:
338                                 mem[fentry->k] = X;
339                                 continue;
340
341                         default:
342                                 /* Invalid instruction counts as RET */
343                                 return (0);
344                 }
345
346                 /* Handle ancillary data, which are impossible
347                    (or very difficult) to get parsing packet contents.
348                  */
349                 switch (k-SKF_AD_OFF) {
350                 case SKF_AD_PROTOCOL:
351                         A = htons(skb->protocol);
352                         continue;
353                 case SKF_AD_PKTTYPE:
354                         A = skb->pkt_type;
355                         continue;
356                 case SKF_AD_IFINDEX:
357                         A = skb->dev->ifindex;
358                         continue;
359                 default:
360                         return 0;
361                 }
362         }
363
364         return (0);
365 }
366
367 /**
368  *      sk_chk_filter - verify socket filter code
369  *      @filter: filter to verify
370  *      @flen: length of filter
371  *
372  * Check the user's filter code. If we let some ugly
373  * filter code slip through kaboom! The filter must contain
374  * no references or jumps that are out of range, no illegal instructions
375  * and no backward jumps. It must end with a RET instruction
376  *
377  * Returns 0 if the rule set is legal or a negative errno code if not.
378  */
379
380 int sk_chk_filter(struct sock_filter *filter, int flen)
381 {
382         struct sock_filter *ftest;
383         int pc;
384
385         if ((unsigned int) flen >= (~0U / sizeof(struct sock_filter)))
386                 return -EINVAL;
387
388        /*
389         * Check the filter code now.
390         */
391         for(pc = 0; pc < flen; pc++)
392         {
393                 /*
394                  *      All jumps are forward as they are not signed
395                  */
396                  
397                 ftest = &filter[pc];
398                 if(BPF_CLASS(ftest->code) == BPF_JMP)
399                 {
400                         /*
401                          *      But they mustn't jump off the end.
402                          */
403                         if(BPF_OP(ftest->code) == BPF_JA)
404                         {
405                                 /* Note, the large ftest->k might cause
406                                    loops. Compare this with conditional
407                                    jumps below, where offsets are limited. --ANK (981016)
408                                  */
409                                 if (ftest->k >= (unsigned)(flen-pc-1))
410                                         return -EINVAL;
411                         }
412                         else
413                         {
414                                 /*
415                                  *      For conditionals both must be safe
416                                  */
417                                 if(pc + ftest->jt +1 >= flen || pc + ftest->jf +1 >= flen)
418                                         return -EINVAL;
419                         }
420                 }
421
422                 /*
423                  *      Check that memory operations use valid addresses.
424                  */
425                  
426                 if (ftest->k >= BPF_MEMWORDS)
427                 {
428                         /*
429                          *      But it might not be a memory operation...
430                          */
431                         switch (ftest->code) {
432                         case BPF_ST:    
433                         case BPF_STX:   
434                         case BPF_LD|BPF_MEM:    
435                         case BPF_LDX|BPF_MEM:   
436                                 return -EINVAL;
437                         }
438                 }
439         }
440
441         /*
442          *      The program must end with a return. We don't care where they
443          *      jumped within the script (its always forwards) but in the
444          *      end they _will_ hit this.
445          */
446          
447         return (BPF_CLASS(filter[flen - 1].code) == BPF_RET)?0:-EINVAL;
448 }
449
450 /**
451  *      sk_attach_filter - attach a socket filter
452  *      @fprog: the filter program
453  *      @sk: the socket to use
454  *
455  * Attach the user's filter code. We first run some sanity checks on
456  * it to make sure it does not explode on us later. If an error
457  * occurs or there is insufficient memory for the filter a negative
458  * errno code is returned. On success the return is zero.
459  */
460
461 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
462 {
463         struct sk_filter *fp; 
464         unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
465         int err;
466
467         /* Make sure new filter is there and in the right amounts. */
468         if (fprog->filter == NULL || fprog->len > BPF_MAXINSNS)
469                 return (-EINVAL);
470
471         fp = (struct sk_filter *)sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
472         if(fp == NULL)
473                 return (-ENOMEM);
474
475         if (copy_from_user(fp->insns, fprog->filter, fsize)) {
476                 sock_kfree_s(sk, fp, fsize+sizeof(*fp)); 
477                 return -EFAULT;
478         }
479
480         atomic_set(&fp->refcnt, 1);
481         fp->len = fprog->len;
482
483         if ((err = sk_chk_filter(fp->insns, fp->len))==0) {
484                 struct sk_filter *old_fp;
485
486                 spin_lock_bh(&sk->lock.slock);
487                 old_fp = sk->filter;
488                 sk->filter = fp;
489                 spin_unlock_bh(&sk->lock.slock);
490                 fp = old_fp;
491         }
492
493         if (fp)
494                 sk_filter_release(sk, fp);
495
496         return (err);
497 }
498 #endif /* CONFIG_FILTER */