net: rtnetlink: decouple rtnetlink address families from real address families
[linux-2.6.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  * Kris Katterjohn - Added many additional checks in sk_chk_filter()
17  */
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/mm.h>
22 #include <linux/fcntl.h>
23 #include <linux/socket.h>
24 #include <linux/in.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_packet.h>
28 #include <linux/gfp.h>
29 #include <net/ip.h>
30 #include <net/protocol.h>
31 #include <net/netlink.h>
32 #include <linux/skbuff.h>
33 #include <net/sock.h>
34 #include <linux/errno.h>
35 #include <linux/timer.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <asm/unaligned.h>
39 #include <linux/filter.h>
40
41 /* No hurry in this branch */
42 static void *__load_pointer(struct sk_buff *skb, int k)
43 {
44         u8 *ptr = NULL;
45
46         if (k >= SKF_NET_OFF)
47                 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
48         else if (k >= SKF_LL_OFF)
49                 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
50
51         if (ptr >= skb->head && ptr < skb_tail_pointer(skb))
52                 return ptr;
53         return NULL;
54 }
55
56 static inline void *load_pointer(struct sk_buff *skb, int k,
57                                  unsigned int size, void *buffer)
58 {
59         if (k >= 0)
60                 return skb_header_pointer(skb, k, size, buffer);
61         else {
62                 if (k >= SKF_AD_OFF)
63                         return NULL;
64                 return __load_pointer(skb, k);
65         }
66 }
67
68 /**
69  *      sk_filter - run a packet through a socket filter
70  *      @sk: sock associated with &sk_buff
71  *      @skb: buffer to filter
72  *
73  * Run the filter code and then cut skb->data to correct size returned by
74  * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
75  * than pkt_len we keep whole skb->data. This is the socket level
76  * wrapper to sk_run_filter. It returns 0 if the packet should
77  * be accepted or -EPERM if the packet should be tossed.
78  *
79  */
80 int sk_filter(struct sock *sk, struct sk_buff *skb)
81 {
82         int err;
83         struct sk_filter *filter;
84
85         err = security_sock_rcv_skb(sk, skb);
86         if (err)
87                 return err;
88
89         rcu_read_lock_bh();
90         filter = rcu_dereference_bh(sk->sk_filter);
91         if (filter) {
92                 unsigned int pkt_len = sk_run_filter(skb, filter->insns,
93                                 filter->len);
94                 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
95         }
96         rcu_read_unlock_bh();
97
98         return err;
99 }
100 EXPORT_SYMBOL(sk_filter);
101
102 /**
103  *      sk_run_filter - run a filter on a socket
104  *      @skb: buffer to run the filter on
105  *      @filter: filter to apply
106  *      @flen: length of filter
107  *
108  * Decode and apply filter instructions to the skb->data.
109  * Return length to keep, 0 for none. skb is the data we are
110  * filtering, filter is the array of filter instructions, and
111  * len is the number of filter blocks in the array.
112  */
113 unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
114 {
115         struct sock_filter *fentry;     /* We walk down these */
116         void *ptr;
117         u32 A = 0;                      /* Accumulator */
118         u32 X = 0;                      /* Index Register */
119         u32 mem[BPF_MEMWORDS];          /* Scratch Memory Store */
120         u32 tmp;
121         int k;
122         int pc;
123
124         /*
125          * Process array of filter instructions.
126          */
127         for (pc = 0; pc < flen; pc++) {
128                 fentry = &filter[pc];
129
130                 switch (fentry->code) {
131                 case BPF_ALU|BPF_ADD|BPF_X:
132                         A += X;
133                         continue;
134                 case BPF_ALU|BPF_ADD|BPF_K:
135                         A += fentry->k;
136                         continue;
137                 case BPF_ALU|BPF_SUB|BPF_X:
138                         A -= X;
139                         continue;
140                 case BPF_ALU|BPF_SUB|BPF_K:
141                         A -= fentry->k;
142                         continue;
143                 case BPF_ALU|BPF_MUL|BPF_X:
144                         A *= X;
145                         continue;
146                 case BPF_ALU|BPF_MUL|BPF_K:
147                         A *= fentry->k;
148                         continue;
149                 case BPF_ALU|BPF_DIV|BPF_X:
150                         if (X == 0)
151                                 return 0;
152                         A /= X;
153                         continue;
154                 case BPF_ALU|BPF_DIV|BPF_K:
155                         A /= fentry->k;
156                         continue;
157                 case BPF_ALU|BPF_AND|BPF_X:
158                         A &= X;
159                         continue;
160                 case BPF_ALU|BPF_AND|BPF_K:
161                         A &= fentry->k;
162                         continue;
163                 case BPF_ALU|BPF_OR|BPF_X:
164                         A |= X;
165                         continue;
166                 case BPF_ALU|BPF_OR|BPF_K:
167                         A |= fentry->k;
168                         continue;
169                 case BPF_ALU|BPF_LSH|BPF_X:
170                         A <<= X;
171                         continue;
172                 case BPF_ALU|BPF_LSH|BPF_K:
173                         A <<= fentry->k;
174                         continue;
175                 case BPF_ALU|BPF_RSH|BPF_X:
176                         A >>= X;
177                         continue;
178                 case BPF_ALU|BPF_RSH|BPF_K:
179                         A >>= fentry->k;
180                         continue;
181                 case BPF_ALU|BPF_NEG:
182                         A = -A;
183                         continue;
184                 case BPF_JMP|BPF_JA:
185                         pc += fentry->k;
186                         continue;
187                 case BPF_JMP|BPF_JGT|BPF_K:
188                         pc += (A > fentry->k) ? fentry->jt : fentry->jf;
189                         continue;
190                 case BPF_JMP|BPF_JGE|BPF_K:
191                         pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
192                         continue;
193                 case BPF_JMP|BPF_JEQ|BPF_K:
194                         pc += (A == fentry->k) ? fentry->jt : fentry->jf;
195                         continue;
196                 case BPF_JMP|BPF_JSET|BPF_K:
197                         pc += (A & fentry->k) ? fentry->jt : fentry->jf;
198                         continue;
199                 case BPF_JMP|BPF_JGT|BPF_X:
200                         pc += (A > X) ? fentry->jt : fentry->jf;
201                         continue;
202                 case BPF_JMP|BPF_JGE|BPF_X:
203                         pc += (A >= X) ? fentry->jt : fentry->jf;
204                         continue;
205                 case BPF_JMP|BPF_JEQ|BPF_X:
206                         pc += (A == X) ? fentry->jt : fentry->jf;
207                         continue;
208                 case BPF_JMP|BPF_JSET|BPF_X:
209                         pc += (A & X) ? fentry->jt : fentry->jf;
210                         continue;
211                 case BPF_LD|BPF_W|BPF_ABS:
212                         k = fentry->k;
213 load_w:
214                         ptr = load_pointer(skb, k, 4, &tmp);
215                         if (ptr != NULL) {
216                                 A = get_unaligned_be32(ptr);
217                                 continue;
218                         }
219                         break;
220                 case BPF_LD|BPF_H|BPF_ABS:
221                         k = fentry->k;
222 load_h:
223                         ptr = load_pointer(skb, k, 2, &tmp);
224                         if (ptr != NULL) {
225                                 A = get_unaligned_be16(ptr);
226                                 continue;
227                         }
228                         break;
229                 case BPF_LD|BPF_B|BPF_ABS:
230                         k = fentry->k;
231 load_b:
232                         ptr = load_pointer(skb, k, 1, &tmp);
233                         if (ptr != NULL) {
234                                 A = *(u8 *)ptr;
235                                 continue;
236                         }
237                         break;
238                 case BPF_LD|BPF_W|BPF_LEN:
239                         A = skb->len;
240                         continue;
241                 case BPF_LDX|BPF_W|BPF_LEN:
242                         X = skb->len;
243                         continue;
244                 case BPF_LD|BPF_W|BPF_IND:
245                         k = X + fentry->k;
246                         goto load_w;
247                 case BPF_LD|BPF_H|BPF_IND:
248                         k = X + fentry->k;
249                         goto load_h;
250                 case BPF_LD|BPF_B|BPF_IND:
251                         k = X + fentry->k;
252                         goto load_b;
253                 case BPF_LDX|BPF_B|BPF_MSH:
254                         ptr = load_pointer(skb, fentry->k, 1, &tmp);
255                         if (ptr != NULL) {
256                                 X = (*(u8 *)ptr & 0xf) << 2;
257                                 continue;
258                         }
259                         return 0;
260                 case BPF_LD|BPF_IMM:
261                         A = fentry->k;
262                         continue;
263                 case BPF_LDX|BPF_IMM:
264                         X = fentry->k;
265                         continue;
266                 case BPF_LD|BPF_MEM:
267                         A = mem[fentry->k];
268                         continue;
269                 case BPF_LDX|BPF_MEM:
270                         X = mem[fentry->k];
271                         continue;
272                 case BPF_MISC|BPF_TAX:
273                         X = A;
274                         continue;
275                 case BPF_MISC|BPF_TXA:
276                         A = X;
277                         continue;
278                 case BPF_RET|BPF_K:
279                         return fentry->k;
280                 case BPF_RET|BPF_A:
281                         return A;
282                 case BPF_ST:
283                         mem[fentry->k] = A;
284                         continue;
285                 case BPF_STX:
286                         mem[fentry->k] = X;
287                         continue;
288                 default:
289                         WARN_ON(1);
290                         return 0;
291                 }
292
293                 /*
294                  * Handle ancillary data, which are impossible
295                  * (or very difficult) to get parsing packet contents.
296                  */
297                 switch (k-SKF_AD_OFF) {
298                 case SKF_AD_PROTOCOL:
299                         A = ntohs(skb->protocol);
300                         continue;
301                 case SKF_AD_PKTTYPE:
302                         A = skb->pkt_type;
303                         continue;
304                 case SKF_AD_IFINDEX:
305                         if (!skb->dev)
306                                 return 0;
307                         A = skb->dev->ifindex;
308                         continue;
309                 case SKF_AD_MARK:
310                         A = skb->mark;
311                         continue;
312                 case SKF_AD_QUEUE:
313                         A = skb->queue_mapping;
314                         continue;
315                 case SKF_AD_HATYPE:
316                         if (!skb->dev)
317                                 return 0;
318                         A = skb->dev->type;
319                         continue;
320                 case SKF_AD_NLATTR: {
321                         struct nlattr *nla;
322
323                         if (skb_is_nonlinear(skb))
324                                 return 0;
325                         if (A > skb->len - sizeof(struct nlattr))
326                                 return 0;
327
328                         nla = nla_find((struct nlattr *)&skb->data[A],
329                                        skb->len - A, X);
330                         if (nla)
331                                 A = (void *)nla - (void *)skb->data;
332                         else
333                                 A = 0;
334                         continue;
335                 }
336                 case SKF_AD_NLATTR_NEST: {
337                         struct nlattr *nla;
338
339                         if (skb_is_nonlinear(skb))
340                                 return 0;
341                         if (A > skb->len - sizeof(struct nlattr))
342                                 return 0;
343
344                         nla = (struct nlattr *)&skb->data[A];
345                         if (nla->nla_len > A - skb->len)
346                                 return 0;
347
348                         nla = nla_find_nested(nla, X);
349                         if (nla)
350                                 A = (void *)nla - (void *)skb->data;
351                         else
352                                 A = 0;
353                         continue;
354                 }
355                 default:
356                         return 0;
357                 }
358         }
359
360         return 0;
361 }
362 EXPORT_SYMBOL(sk_run_filter);
363
364 /**
365  *      sk_chk_filter - verify socket filter code
366  *      @filter: filter to verify
367  *      @flen: length of filter
368  *
369  * Check the user's filter code. If we let some ugly
370  * filter code slip through kaboom! The filter must contain
371  * no references or jumps that are out of range, no illegal
372  * instructions, and must end with a RET instruction.
373  *
374  * All jumps are forward as they are not signed.
375  *
376  * Returns 0 if the rule set is legal or -EINVAL if not.
377  */
378 int sk_chk_filter(struct sock_filter *filter, int flen)
379 {
380         struct sock_filter *ftest;
381         int pc;
382
383         if (flen == 0 || flen > BPF_MAXINSNS)
384                 return -EINVAL;
385
386         /* check the filter code now */
387         for (pc = 0; pc < flen; pc++) {
388                 ftest = &filter[pc];
389
390                 /* Only allow valid instructions */
391                 switch (ftest->code) {
392                 case BPF_ALU|BPF_ADD|BPF_K:
393                 case BPF_ALU|BPF_ADD|BPF_X:
394                 case BPF_ALU|BPF_SUB|BPF_K:
395                 case BPF_ALU|BPF_SUB|BPF_X:
396                 case BPF_ALU|BPF_MUL|BPF_K:
397                 case BPF_ALU|BPF_MUL|BPF_X:
398                 case BPF_ALU|BPF_DIV|BPF_X:
399                 case BPF_ALU|BPF_AND|BPF_K:
400                 case BPF_ALU|BPF_AND|BPF_X:
401                 case BPF_ALU|BPF_OR|BPF_K:
402                 case BPF_ALU|BPF_OR|BPF_X:
403                 case BPF_ALU|BPF_LSH|BPF_K:
404                 case BPF_ALU|BPF_LSH|BPF_X:
405                 case BPF_ALU|BPF_RSH|BPF_K:
406                 case BPF_ALU|BPF_RSH|BPF_X:
407                 case BPF_ALU|BPF_NEG:
408                 case BPF_LD|BPF_W|BPF_ABS:
409                 case BPF_LD|BPF_H|BPF_ABS:
410                 case BPF_LD|BPF_B|BPF_ABS:
411                 case BPF_LD|BPF_W|BPF_LEN:
412                 case BPF_LD|BPF_W|BPF_IND:
413                 case BPF_LD|BPF_H|BPF_IND:
414                 case BPF_LD|BPF_B|BPF_IND:
415                 case BPF_LD|BPF_IMM:
416                 case BPF_LDX|BPF_W|BPF_LEN:
417                 case BPF_LDX|BPF_B|BPF_MSH:
418                 case BPF_LDX|BPF_IMM:
419                 case BPF_MISC|BPF_TAX:
420                 case BPF_MISC|BPF_TXA:
421                 case BPF_RET|BPF_K:
422                 case BPF_RET|BPF_A:
423                         break;
424
425                 /* Some instructions need special checks */
426
427                 case BPF_ALU|BPF_DIV|BPF_K:
428                         /* check for division by zero */
429                         if (ftest->k == 0)
430                                 return -EINVAL;
431                         break;
432
433                 case BPF_LD|BPF_MEM:
434                 case BPF_LDX|BPF_MEM:
435                 case BPF_ST:
436                 case BPF_STX:
437                         /* check for invalid memory addresses */
438                         if (ftest->k >= BPF_MEMWORDS)
439                                 return -EINVAL;
440                         break;
441
442                 case BPF_JMP|BPF_JA:
443                         /*
444                          * Note, the large ftest->k might cause loops.
445                          * Compare this with conditional jumps below,
446                          * where offsets are limited. --ANK (981016)
447                          */
448                         if (ftest->k >= (unsigned)(flen-pc-1))
449                                 return -EINVAL;
450                         break;
451
452                 case BPF_JMP|BPF_JEQ|BPF_K:
453                 case BPF_JMP|BPF_JEQ|BPF_X:
454                 case BPF_JMP|BPF_JGE|BPF_K:
455                 case BPF_JMP|BPF_JGE|BPF_X:
456                 case BPF_JMP|BPF_JGT|BPF_K:
457                 case BPF_JMP|BPF_JGT|BPF_X:
458                 case BPF_JMP|BPF_JSET|BPF_K:
459                 case BPF_JMP|BPF_JSET|BPF_X:
460                         /* for conditionals both must be safe */
461                         if (pc + ftest->jt + 1 >= flen ||
462                             pc + ftest->jf + 1 >= flen)
463                                 return -EINVAL;
464                         break;
465
466                 default:
467                         return -EINVAL;
468                 }
469         }
470
471         return (BPF_CLASS(filter[flen - 1].code) == BPF_RET) ? 0 : -EINVAL;
472 }
473 EXPORT_SYMBOL(sk_chk_filter);
474
475 /**
476  *      sk_filter_rcu_release: Release a socket filter by rcu_head
477  *      @rcu: rcu_head that contains the sk_filter to free
478  */
479 static void sk_filter_rcu_release(struct rcu_head *rcu)
480 {
481         struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
482
483         sk_filter_release(fp);
484 }
485
486 static void sk_filter_delayed_uncharge(struct sock *sk, struct sk_filter *fp)
487 {
488         unsigned int size = sk_filter_len(fp);
489
490         atomic_sub(size, &sk->sk_omem_alloc);
491         call_rcu_bh(&fp->rcu, sk_filter_rcu_release);
492 }
493
494 /**
495  *      sk_attach_filter - attach a socket filter
496  *      @fprog: the filter program
497  *      @sk: the socket to use
498  *
499  * Attach the user's filter code. We first run some sanity checks on
500  * it to make sure it does not explode on us later. If an error
501  * occurs or there is insufficient memory for the filter a negative
502  * errno code is returned. On success the return is zero.
503  */
504 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
505 {
506         struct sk_filter *fp, *old_fp;
507         unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
508         int err;
509
510         /* Make sure new filter is there and in the right amounts. */
511         if (fprog->filter == NULL)
512                 return -EINVAL;
513
514         fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
515         if (!fp)
516                 return -ENOMEM;
517         if (copy_from_user(fp->insns, fprog->filter, fsize)) {
518                 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
519                 return -EFAULT;
520         }
521
522         atomic_set(&fp->refcnt, 1);
523         fp->len = fprog->len;
524
525         err = sk_chk_filter(fp->insns, fp->len);
526         if (err) {
527                 sk_filter_uncharge(sk, fp);
528                 return err;
529         }
530
531         rcu_read_lock_bh();
532         old_fp = rcu_dereference_bh(sk->sk_filter);
533         rcu_assign_pointer(sk->sk_filter, fp);
534         rcu_read_unlock_bh();
535
536         if (old_fp)
537                 sk_filter_delayed_uncharge(sk, old_fp);
538         return 0;
539 }
540 EXPORT_SYMBOL_GPL(sk_attach_filter);
541
542 int sk_detach_filter(struct sock *sk)
543 {
544         int ret = -ENOENT;
545         struct sk_filter *filter;
546
547         rcu_read_lock_bh();
548         filter = rcu_dereference_bh(sk->sk_filter);
549         if (filter) {
550                 rcu_assign_pointer(sk->sk_filter, NULL);
551                 sk_filter_delayed_uncharge(sk, filter);
552                 ret = 0;
553         }
554         rcu_read_unlock_bh();
555         return ret;
556 }
557 EXPORT_SYMBOL_GPL(sk_detach_filter);