Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | /*
* Linux Socket Filter - Kernel level socket filtering
*
* Author:
* Jay Schulist <Jay.Schulist@spacs.k12.wi.us>
*
* Based on the design of:
* - The Berkeley Packet Filter
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include <linux/config.h>
#if defined(CONFIG_FILTER)
#include <linux/module.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/fcntl.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/if_packet.h>
#include <net/ip.h>
#include <net/protocol.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/filter.h>
/*
* Decode and apply filter instructions to the skb->data.
* Return length to keep, 0 for none. skb is the data we are
* filtering, filter is the array of filter instructions, and
* len is the number of filter blocks in the array.
*/
int sk_run_filter(unsigned char *data, int len, struct sock_filter *filter, int flen)
{
struct sock_filter *fentry; /* We walk down these */
u32 A = 0; /* Accumulator */
u32 X = 0; /* Index Register */
u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
int k;
int pc;
int *t;
/*
* Process array of filter instructions.
*/
for(pc = 0; pc < flen; pc++)
{
fentry = &filter[pc];
if(fentry->code & BPF_X)
t=&X;
else
t=&fentry->k;
switch(fentry->code)
{
case BPF_ALU|BPF_ADD|BPF_X:
case BPF_ALU|BPF_ADD|BPF_K:
A += *t;
continue;
case BPF_ALU|BPF_SUB|BPF_X:
case BPF_ALU|BPF_SUB|BPF_K:
A -= *t;
continue;
case BPF_ALU|BPF_MUL|BPF_X:
case BPF_ALU|BPF_MUL|BPF_K:
A *= *t;
continue;
case BPF_ALU|BPF_DIV|BPF_X:
case BPF_ALU|BPF_DIV|BPF_K:
if(*t == 0)
return (0);
A /= *t;
continue;
case BPF_ALU|BPF_AND|BPF_X:
case BPF_ALU|BPF_AND|BPF_K:
A &= *t;
continue;
case BPF_ALU|BPF_OR|BPF_X:
case BPF_ALU|BPF_OR|BPF_K:
A |= *t;
continue;
case BPF_ALU|BPF_LSH|BPF_X:
case BPF_ALU|BPF_LSH|BPF_K:
A <<= *t;
continue;
case BPF_ALU|BPF_RSH|BPF_X:
case BPF_ALU|BPF_RSH|BPF_K:
A >>= *t;
continue;
case BPF_ALU|BPF_NEG:
A = -A;
continue;
case BPF_JMP|BPF_JA:
pc += fentry->k;
continue;
case BPF_JMP|BPF_JGT|BPF_K:
pc += (A > fentry->k) ? fentry->jt : fentry->jf;
continue;
case BPF_JMP|BPF_JGE|BPF_K:
pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
continue;
case BPF_JMP|BPF_JEQ|BPF_K:
pc += (A == fentry->k) ? fentry->jt : fentry->jf;
continue;
case BPF_JMP|BPF_JSET|BPF_K:
pc += (A & fentry->k) ? fentry->jt : fentry->jf;
continue;
case BPF_JMP|BPF_JGT|BPF_X:
pc += (A > X) ? fentry->jt : fentry->jf;
continue;
case BPF_JMP|BPF_JGE|BPF_X:
pc += (A >= X) ? fentry->jt : fentry->jf;
continue;
case BPF_JMP|BPF_JEQ|BPF_X:
pc += (A == X) ? fentry->jt : fentry->jf;
continue;
case BPF_JMP|BPF_JSET|BPF_X:
pc += (A & X) ? fentry->jt : fentry->jf;
continue;
case BPF_LD|BPF_W|BPF_ABS:
k = fentry->k;
if(k + sizeof(long) > len)
return (0);
A = ntohl(*(long*)&data[k]);
continue;
case BPF_LD|BPF_H|BPF_ABS:
k = fentry->k;
if(k + sizeof(short) > len)
return (0);
A = ntohs(*(short*)&data[k]);
continue;
case BPF_LD|BPF_B|BPF_ABS:
k = fentry->k;
if(k >= len)
return (0);
A = data[k];
continue;
case BPF_LD|BPF_W|BPF_LEN:
A = len;
continue;
case BPF_LDX|BPF_W|BPF_LEN:
X = len;
continue;
case BPF_LD|BPF_W|BPF_IND:
k = X + fentry->k;
if(k + sizeof(u32) > len)
return (0);
A = ntohl(*(u32 *)&data[k]);
continue;
case BPF_LD|BPF_H|BPF_IND:
k = X + fentry->k;
if(k + sizeof(u16) > len)
return (0);
A = ntohs(*(u16*)&data[k]);
continue;
case BPF_LD|BPF_B|BPF_IND:
k = X + fentry->k;
if(k >= len)
return (0);
A = data[k];
continue;
case BPF_LDX|BPF_B|BPF_MSH:
/*
* Hack for BPF to handle TOS etc
*/
k = fentry->k;
if(k >= len)
return (0);
X = (data[fentry->k] & 0xf) << 2;
continue;
case BPF_LD|BPF_IMM:
A = fentry->k;
continue;
case BPF_LDX|BPF_IMM:
X = fentry->k;
continue;
case BPF_LD|BPF_MEM:
A = mem[fentry->k];
continue;
case BPF_LDX|BPF_MEM:
X = mem[fentry->k];
continue;
case BPF_MISC|BPF_TAX:
X = A;
continue;
case BPF_MISC|BPF_TXA:
A = X;
continue;
case BPF_RET|BPF_K:
return ((unsigned int)fentry->k);
case BPF_RET|BPF_A:
return ((unsigned int)A);
case BPF_ST:
mem[fentry->k] = A;
continue;
case BPF_STX:
mem[fentry->k] = X;
continue;
default:
/* Invalid instruction counts as RET */
return (0);
}
}
printk(KERN_ERR "Filter ruleset ran off the end.\n");
return (0);
}
/*
* Check the user's filter code. If we let some ugly
* filter code slip through kaboom!
*/
int sk_chk_filter(struct sock_filter *filter, int flen)
{
struct sock_filter *ftest;
int pc;
/*
* Check the filter code now.
*/
for(pc = 0; pc < flen; pc++)
{
/*
* All jumps are forward as they are not signed
*/
ftest = &filter[pc];
if(BPF_CLASS(ftest->code) == BPF_JMP)
{
/*
* But they mustn't jump off the end.
*/
if(BPF_OP(ftest->code) == BPF_JA)
{
if(pc + ftest->k + 1>= (unsigned)flen)
return (-EINVAL);
}
else
{
/*
* For conditionals both must be safe
*/
if(pc + ftest->jt +1 >= flen || pc + ftest->jf +1 >= flen)
return (-EINVAL);
}
}
/*
* Check that memory operations use valid addresses.
*/
if(ftest->k <0 || ftest->k >= BPF_MEMWORDS)
{
/*
* But it might not be a memory operation...
*/
if (BPF_CLASS(ftest->code) == BPF_ST)
return -EINVAL;
if((BPF_CLASS(ftest->code) == BPF_LD) &&
(BPF_MODE(ftest->code) == BPF_MEM))
return (-EINVAL);
}
}
/*
* The program must end with a return. We don't care where they
* jumped within the script (its always forwards) but in the
* end they _will_ hit this.
*/
return (BPF_CLASS(filter[flen - 1].code) == BPF_RET)?0:-EINVAL;
}
/*
* Attach the user's filter code. We first run some sanity checks on
* it to make sure it does not explode on us later.
*/
int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
{
struct sock_filter *fp, *old_filter;
int fsize = sizeof(struct sock_filter) * fprog->len;
int err;
/* Make sure new filter is there and in the right amounts. */
if(fprog->filter == NULL || fprog->len == 0 || fsize > BPF_MAXINSNS)
return (-EINVAL);
if((err = sk_chk_filter(fprog->filter, fprog->len))==0)
{
/* If existing filter, remove it first */
if(sk->filter)
{
old_filter = sk->filter_data;
kfree_s(old_filter, (sizeof(old_filter) * sk->filter));
sk->filter_data = NULL;
}
fp = (struct sock_filter *)kmalloc(fsize, GFP_KERNEL);
if(fp == NULL)
return (-ENOMEM);
memset(fp,0,sizeof(*fp));
memcpy(fp, fprog->filter, fsize); /* Copy instructions */
sk->filter = fprog->len; /* Number of filter blocks */
sk->filter_data = fp; /* Filter instructions */
}
return (err);
}
#endif /* CONFIG_FILTER */
|