| 1 |
/* |
| 2 |
* (C) 2005-2011 by Pablo Neira Ayuso <[email protected]> |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "internal/internal.h" |
| 11 |
#include "internal/stack.h" |
| 12 |
#include <endian.h> |
| 13 |
#include <linux/filter.h> |
| 14 |
#include <stddef.h> /* offsetof */ |
| 15 |
|
| 16 |
#ifndef SKF_AD_NLATTR |
| 17 |
#define SKF_AD_NLATTR 12 |
| 18 |
#endif |
| 19 |
|
| 20 |
/* this requires a Linux kernel >= 2.6.29 */ |
| 21 |
#ifndef SKF_AD_NLATTR_NEST |
| 22 |
#define SKF_AD_NLATTR_NEST 16 |
| 23 |
#endif |
| 24 |
|
| 25 |
#define NFCT_FILTER_REJECT 0U |
| 26 |
#define NFCT_FILTER_ACCEPT ~0U |
| 27 |
|
| 28 |
#if 0 |
| 29 |
static char *code2str(uint16_t code) |
| 30 |
{ |
| 31 |
switch(code) { |
| 32 |
case BPF_LD|BPF_IMM: |
| 33 |
return "BPF_LD|BPF_IMM"; |
| 34 |
break; |
| 35 |
case BPF_LDX|BPF_IMM: |
| 36 |
return "BPF_LDX|BPF_IMM"; |
| 37 |
break; |
| 38 |
case BPF_LD|BPF_B|BPF_ABS: |
| 39 |
return "BPF_LD|BPF_B|BPF_ABS"; |
| 40 |
break; |
| 41 |
case BPF_JMP|BPF_JEQ|BPF_K: |
| 42 |
return "BPF_JMP|BPF_JEQ|BPF_K"; |
| 43 |
break; |
| 44 |
case BPF_ALU|BPF_AND|BPF_K: |
| 45 |
return "BPF_ALU|BPF_AND|BPF_K"; |
| 46 |
break; |
| 47 |
case BPF_JMP|BPF_JA: |
| 48 |
return "BPF_JMP|BPF_JA"; |
| 49 |
break; |
| 50 |
case BPF_RET|BPF_K: |
| 51 |
return "BPF_RET|BPF_K"; |
| 52 |
break; |
| 53 |
case BPF_ALU|BPF_ADD|BPF_K: |
| 54 |
return "BPF_ALU|BPF_ADD|BPF_K"; |
| 55 |
break; |
| 56 |
case BPF_MISC|BPF_TAX: |
| 57 |
return "BPF_MISC|BPF_TAX"; |
| 58 |
break; |
| 59 |
case BPF_MISC|BPF_TXA: |
| 60 |
return "BPF_MISC|BPF_TXA"; |
| 61 |
break; |
| 62 |
case BPF_LD|BPF_B|BPF_IND: |
| 63 |
return "BPF_LD|BPF_B|BPF_IND"; |
| 64 |
break; |
| 65 |
case BPF_LD|BPF_H|BPF_IND: |
| 66 |
return "BPF_LD|BPF_H|BPF_IND"; |
| 67 |
break; |
| 68 |
case BPF_LD|BPF_W|BPF_IND: |
| 69 |
return "BPF_LD|BPF_W|BPF_IND"; |
| 70 |
break; |
| 71 |
} |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
static void show_filter(struct sock_filter *this, int from, int to, char *str) |
| 76 |
{ |
| 77 |
int i; |
| 78 |
|
| 79 |
printf("%s\n", str); |
| 80 |
|
| 81 |
for(i=from; i<to; i++) { |
| 82 |
char *code_str = code2str(this[i].code & 0xFFFF); |
| 83 |
|
| 84 |
if (!code_str) { |
| 85 |
printf("(%.4x) code=%.4x\t\t\tjt=%.2x jf=%.2x k=%.8x\n", |
| 86 |
i, |
| 87 |
this[i].code & 0xFFFF, |
| 88 |
this[i].jt & 0xFF, |
| 89 |
this[i].jf & 0xFF, |
| 90 |
this[i].k & 0xFFFFFFFF); |
| 91 |
} else { |
| 92 |
printf("(%.4x) code=%30s\tjt=%.2x jf=%.2x k=%.8x\n", |
| 93 |
i, |
| 94 |
code_str, |
| 95 |
this[i].jt & 0xFF, |
| 96 |
this[i].jf & 0xFF, |
| 97 |
this[i].k & 0xFFFFFFFF); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
#else |
| 102 |
static inline void |
| 103 |
show_filter(struct sock_filter *this, int from, int to, char *str) {} |
| 104 |
#endif |
| 105 |
|
| 106 |
#define NEW_POS(x) (sizeof(x)/sizeof(struct sock_filter)) |
| 107 |
|
| 108 |
static int |
| 109 |
nfct_bsf_load_payload_offset(struct sock_filter *this, int pos) |
| 110 |
{ |
| 111 |
struct sock_filter __code = { |
| 112 |
.code = BPF_LD|BPF_IMM, |
| 113 |
.k = sizeof(struct nlmsghdr) + sizeof(struct nfgenmsg), |
| 114 |
}; |
| 115 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 116 |
return NEW_POS(__code); |
| 117 |
} |
| 118 |
|
| 119 |
static int |
| 120 |
nfct_bsf_find_attr(struct sock_filter *this, int attr, int pos) |
| 121 |
{ |
| 122 |
struct sock_filter __code[] = { |
| 123 |
[0] = { |
| 124 |
/* X = attribute type */ |
| 125 |
.code = BPF_LDX|BPF_IMM, |
| 126 |
.k = attr, |
| 127 |
}, |
| 128 |
[1] = { |
| 129 |
/* A = netlink attribute offset */ |
| 130 |
.code = BPF_LD|BPF_B|BPF_ABS, |
| 131 |
.k = SKF_AD_OFF + SKF_AD_NLATTR, |
| 132 |
} |
| 133 |
}; |
| 134 |
memcpy(&this[pos], __code, sizeof(__code)); |
| 135 |
return NEW_POS(__code); |
| 136 |
} |
| 137 |
|
| 138 |
/* like the previous, but limit the search to the bound of the nest */ |
| 139 |
static int |
| 140 |
nfct_bsf_find_attr_nest(struct sock_filter *this, int attr, int pos) |
| 141 |
{ |
| 142 |
struct sock_filter __code[] = { |
| 143 |
[0] = { |
| 144 |
/* X = attribute type */ |
| 145 |
.code = BPF_LDX|BPF_IMM, |
| 146 |
.k = attr, |
| 147 |
}, |
| 148 |
[1] = { |
| 149 |
/* A = netlink attribute offset */ |
| 150 |
.code = BPF_LD|BPF_B|BPF_ABS, |
| 151 |
.k = SKF_AD_OFF + SKF_AD_NLATTR_NEST, |
| 152 |
} |
| 153 |
}; |
| 154 |
memcpy(&this[pos], __code, sizeof(__code)); |
| 155 |
return NEW_POS(__code); |
| 156 |
} |
| 157 |
|
| 158 |
struct jump { |
| 159 |
int line; |
| 160 |
uint8_t jt; |
| 161 |
uint8_t jf; |
| 162 |
}; |
| 163 |
|
| 164 |
static int |
| 165 |
nfct_bsf_cmp_k_stack(struct sock_filter *this, int k, |
| 166 |
int jump_true, int pos, struct stack *s) |
| 167 |
{ |
| 168 |
struct sock_filter __code = { |
| 169 |
.code = BPF_JMP|BPF_JEQ|BPF_K, |
| 170 |
.k = k, |
| 171 |
}; |
| 172 |
struct jump jmp = { |
| 173 |
.line = pos, |
| 174 |
.jt = jump_true - 1, |
| 175 |
.jf = 0, |
| 176 |
}; |
| 177 |
stack_push(s, &jmp); |
| 178 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 179 |
return NEW_POS(__code); |
| 180 |
} |
| 181 |
|
| 182 |
/* like previous, but use jf instead of jt. We can merge both functions */ |
| 183 |
static int |
| 184 |
nfct_bsf_cmp_k_stack_jf(struct sock_filter *this, int k, |
| 185 |
int jump_false, int pos, struct stack *s) |
| 186 |
{ |
| 187 |
struct sock_filter __code = { |
| 188 |
.code = BPF_JMP|BPF_JEQ|BPF_K, |
| 189 |
.k = k, |
| 190 |
}; |
| 191 |
struct jump jmp = { |
| 192 |
.line = pos, |
| 193 |
.jt = 0, |
| 194 |
.jf = jump_false - 1, |
| 195 |
}; |
| 196 |
stack_push(s, &jmp); |
| 197 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 198 |
return NEW_POS(__code); |
| 199 |
} |
| 200 |
|
| 201 |
static int |
| 202 |
nfct_bsf_alu_and(struct sock_filter *this, int k, int pos) |
| 203 |
{ |
| 204 |
struct sock_filter __code = { |
| 205 |
.code = BPF_ALU|BPF_AND|BPF_K, |
| 206 |
.k = k, |
| 207 |
}; |
| 208 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 209 |
return NEW_POS(__code); |
| 210 |
} |
| 211 |
|
| 212 |
static int |
| 213 |
nfct_bsf_add_attr_data_offset(struct sock_filter *this, int pos) |
| 214 |
{ |
| 215 |
struct sock_filter __code = { |
| 216 |
/* A += sizeof(struct nfattr) */ |
| 217 |
.code = BPF_ALU|BPF_ADD|BPF_K, |
| 218 |
.k = sizeof(struct nfattr), |
| 219 |
}; |
| 220 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 221 |
return NEW_POS(__code); |
| 222 |
} |
| 223 |
|
| 224 |
static int |
| 225 |
nfct_bsf_x_equal_a(struct sock_filter *this, int pos) |
| 226 |
{ |
| 227 |
struct sock_filter __code = { |
| 228 |
.code = BPF_MISC|BPF_TAX, |
| 229 |
}; |
| 230 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 231 |
return NEW_POS(__code); |
| 232 |
} |
| 233 |
|
| 234 |
static int |
| 235 |
nfct_bsf_a_equal_x(struct sock_filter *this, int pos) |
| 236 |
{ |
| 237 |
struct sock_filter __code = { |
| 238 |
.code = BPF_MISC|BPF_TXA, |
| 239 |
}; |
| 240 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 241 |
return NEW_POS(__code); |
| 242 |
} |
| 243 |
|
| 244 |
static int |
| 245 |
nfct_bsf_load_attr(struct sock_filter *this, int word_size, int pos) |
| 246 |
{ |
| 247 |
struct sock_filter __code = { |
| 248 |
/* A = skb->data[X + k:word_size] */ |
| 249 |
.code = BPF_LD|word_size|BPF_IND, |
| 250 |
.k = sizeof(struct nfattr), |
| 251 |
|
| 252 |
}; |
| 253 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 254 |
return NEW_POS(__code); |
| 255 |
} |
| 256 |
|
| 257 |
static int |
| 258 |
nfct_bsf_load_attr_offset(struct sock_filter *this, int word_size, |
| 259 |
int offset, int pos) |
| 260 |
{ |
| 261 |
struct sock_filter __code = { |
| 262 |
/* A = skb->data[X + k:word_size] */ |
| 263 |
.code = BPF_LD|word_size|BPF_IND, |
| 264 |
.k = sizeof(struct nfattr) + offset, |
| 265 |
}; |
| 266 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 267 |
return NEW_POS(__code); |
| 268 |
} |
| 269 |
|
| 270 |
static int |
| 271 |
nfct_bsf_ret_verdict(struct sock_filter *this, int verdict, int pos) |
| 272 |
{ |
| 273 |
struct sock_filter __code = { |
| 274 |
.code = BPF_RET|BPF_K, |
| 275 |
.k = verdict, |
| 276 |
}; |
| 277 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 278 |
return NEW_POS(__code); |
| 279 |
} |
| 280 |
|
| 281 |
static int |
| 282 |
nfct_bsf_jump_to(struct sock_filter *this, int line, int pos) |
| 283 |
{ |
| 284 |
struct sock_filter __code = { |
| 285 |
.code = BPF_JMP|BPF_JA, |
| 286 |
.k = line, |
| 287 |
}; |
| 288 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 289 |
return NEW_POS(__code); |
| 290 |
}; |
| 291 |
|
| 292 |
/* this helps to skip messages coming from the ctnetlink expectation subsys. */ |
| 293 |
static int |
| 294 |
bsf_cmp_subsys(struct sock_filter *this, int pos, uint8_t subsys) |
| 295 |
{ |
| 296 |
struct sock_filter __code[] = { |
| 297 |
[0] = { |
| 298 |
/* X = offset to nlh->nlmsg_type */ |
| 299 |
.code = BPF_LDX|BPF_IMM, |
| 300 |
.k = offsetof(struct nlmsghdr, nlmsg_type), |
| 301 |
}, |
| 302 |
[1] = { |
| 303 |
/* A = skb->data[X+k:B] (subsys_id) */ |
| 304 |
.code = BPF_LD|BPF_B|BPF_IND, |
| 305 |
#if BYTE_ORDER == BIG_ENDIAN |
| 306 |
.k = 0, |
| 307 |
#else |
| 308 |
.k = sizeof(uint8_t), |
| 309 |
#endif |
| 310 |
}, |
| 311 |
[2] = { |
| 312 |
/* A == subsys ? jump + 1 : accept */ |
| 313 |
.code = BPF_JMP|BPF_JEQ|BPF_K, |
| 314 |
.k = subsys, |
| 315 |
.jt = 1, |
| 316 |
.jf = 0, |
| 317 |
}, |
| 318 |
}; |
| 319 |
memcpy(&this[pos], &__code, sizeof(__code)); |
| 320 |
return NEW_POS(__code); |
| 321 |
} |
| 322 |
|
| 323 |
static int |
| 324 |
add_state_filter_cta(struct sock_filter *this, |
| 325 |
unsigned int cta_protoinfo_proto, |
| 326 |
unsigned int cta_protoinfo_state, |
| 327 |
uint16_t state_flags, |
| 328 |
unsigned int logic) |
| 329 |
{ |
| 330 |
unsigned int i, j; |
| 331 |
unsigned int label_continue, jt; |
| 332 |
struct stack *s; |
| 333 |
struct jump jmp; |
| 334 |
|
| 335 |
/* XXX: 32 maximum states + 3 jumps in the three-level iteration */ |
| 336 |
s = stack_create(sizeof(struct jump), 3 + 32); |
| 337 |
if (s == NULL) { |
| 338 |
errno = ENOMEM; |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
jt = 1; |
| 343 |
if (logic == NFCT_FILTER_LOGIC_POSITIVE) |
| 344 |
label_continue = 1; |
| 345 |
else |
| 346 |
label_continue = 2; |
| 347 |
|
| 348 |
j = 0; |
| 349 |
j += nfct_bsf_load_payload_offset(this, j); |
| 350 |
j += nfct_bsf_find_attr(this, CTA_PROTOINFO, j); |
| 351 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 352 |
j += nfct_bsf_add_attr_data_offset(this, j); |
| 353 |
j += nfct_bsf_find_attr(this, cta_protoinfo_proto, j); |
| 354 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 355 |
j += nfct_bsf_add_attr_data_offset(this, j); |
| 356 |
j += nfct_bsf_find_attr(this, cta_protoinfo_state, j); |
| 357 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 358 |
j += nfct_bsf_x_equal_a(this, j); |
| 359 |
j += nfct_bsf_load_attr(this, BPF_B, j); |
| 360 |
|
| 361 |
for (i = 0; i < sizeof(state_flags) * 8; i++) { |
| 362 |
if (state_flags & (1 << i)) { |
| 363 |
j += nfct_bsf_cmp_k_stack(this, i, jt - j, j, s); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
while (stack_pop(s, &jmp) != -1) |
| 368 |
this[jmp.line].jt += jmp.jt + j; |
| 369 |
|
| 370 |
if (logic == NFCT_FILTER_LOGIC_NEGATIVE) |
| 371 |
j += nfct_bsf_jump_to(this, 1, j); |
| 372 |
|
| 373 |
j += nfct_bsf_ret_verdict(this, NFCT_FILTER_REJECT, j); |
| 374 |
|
| 375 |
stack_destroy(s); |
| 376 |
|
| 377 |
return j; |
| 378 |
} |
| 379 |
|
| 380 |
static int |
| 381 |
add_state_filter(struct sock_filter *this, |
| 382 |
int proto, |
| 383 |
uint16_t flags, |
| 384 |
unsigned int logic) |
| 385 |
{ |
| 386 |
static const struct { |
| 387 |
unsigned int cta_protoinfo; |
| 388 |
unsigned int cta_state; |
| 389 |
} cta[IPPROTO_MAX] = { |
| 390 |
[IPPROTO_TCP] = { |
| 391 |
.cta_protoinfo = CTA_PROTOINFO_TCP, |
| 392 |
.cta_state = CTA_PROTOINFO_TCP_STATE, |
| 393 |
}, |
| 394 |
[IPPROTO_SCTP] = { |
| 395 |
.cta_protoinfo = CTA_PROTOINFO_SCTP, |
| 396 |
.cta_state = CTA_PROTOINFO_SCTP_STATE, |
| 397 |
}, |
| 398 |
[IPPROTO_DCCP] = { |
| 399 |
.cta_protoinfo = CTA_PROTOINFO_DCCP, |
| 400 |
.cta_state = CTA_PROTOINFO_DCCP_STATE, |
| 401 |
}, |
| 402 |
}; |
| 403 |
|
| 404 |
if (cta[proto].cta_protoinfo == 0 && cta[proto].cta_state == 0) { |
| 405 |
errno = ENOTSUP; |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
return add_state_filter_cta(this, |
| 410 |
cta[proto].cta_protoinfo, |
| 411 |
cta[proto].cta_state, |
| 412 |
flags, |
| 413 |
logic); |
| 414 |
} |
| 415 |
|
| 416 |
static int |
| 417 |
bsf_add_state_filter(const struct nfct_filter *filter, struct sock_filter *this) |
| 418 |
{ |
| 419 |
unsigned int i, j; |
| 420 |
|
| 421 |
for (i = 0, j = 0; i < IPPROTO_MAX; i++) { |
| 422 |
if (filter->l4proto_state[i].map && |
| 423 |
filter->l4proto_state[i].len > 0) { |
| 424 |
j += add_state_filter( |
| 425 |
this, |
| 426 |
i, |
| 427 |
filter->l4proto_state[i].map, |
| 428 |
filter->logic[NFCT_FILTER_L4PROTO_STATE]); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
return j; |
| 433 |
} |
| 434 |
|
| 435 |
static int |
| 436 |
bsf_add_proto_filter(const struct nfct_filter *f, struct sock_filter *this) |
| 437 |
{ |
| 438 |
unsigned int i, j; |
| 439 |
unsigned int label_continue, jt; |
| 440 |
struct stack *s; |
| 441 |
struct jump jmp; |
| 442 |
|
| 443 |
/* nothing to filter, skip */ |
| 444 |
if (f->l4proto_len == 0) |
| 445 |
return 0; |
| 446 |
|
| 447 |
/* XXX: 255 maximum proto + 3 jumps in the three-level iteration */ |
| 448 |
s = stack_create(sizeof(struct jump), 3 + 255); |
| 449 |
if (s == NULL) { |
| 450 |
errno = ENOMEM; |
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
jt = 1; |
| 455 |
if (f->logic[NFCT_FILTER_L4PROTO] == NFCT_FILTER_LOGIC_POSITIVE) |
| 456 |
label_continue = 1; |
| 457 |
else |
| 458 |
label_continue = 2; |
| 459 |
|
| 460 |
j = 0; |
| 461 |
j += nfct_bsf_load_payload_offset(this, j); |
| 462 |
j += nfct_bsf_find_attr(this, CTA_TUPLE_ORIG, j); |
| 463 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 464 |
j += nfct_bsf_add_attr_data_offset(this, j); |
| 465 |
j += nfct_bsf_find_attr(this, CTA_TUPLE_PROTO, j); |
| 466 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 467 |
j += nfct_bsf_add_attr_data_offset(this, j); |
| 468 |
j += nfct_bsf_find_attr(this, CTA_PROTO_NUM, j); |
| 469 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 470 |
j += nfct_bsf_x_equal_a(this, j); |
| 471 |
j += nfct_bsf_load_attr(this, BPF_B, j); |
| 472 |
|
| 473 |
for (i = 0; i < IPPROTO_MAX; i++) { |
| 474 |
if (test_bit(i, f->l4proto_map)) { |
| 475 |
j += nfct_bsf_cmp_k_stack(this, i, jt - j, j, s); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
while (stack_pop(s, &jmp) != -1) |
| 480 |
this[jmp.line].jt += jmp.jt + j; |
| 481 |
|
| 482 |
if (f->logic[NFCT_FILTER_L4PROTO] == NFCT_FILTER_LOGIC_NEGATIVE) |
| 483 |
j += nfct_bsf_jump_to(this, 1, j); |
| 484 |
|
| 485 |
j += nfct_bsf_ret_verdict(this, NFCT_FILTER_REJECT, j); |
| 486 |
|
| 487 |
stack_destroy(s); |
| 488 |
|
| 489 |
return j; |
| 490 |
} |
| 491 |
|
| 492 |
static int |
| 493 |
bsf_add_addr_ipv4_filter(const struct nfct_filter *f, |
| 494 |
struct sock_filter *this, |
| 495 |
unsigned int type) |
| 496 |
{ |
| 497 |
unsigned int i, j, dir, attr; |
| 498 |
unsigned int label_continue, jt; |
| 499 |
struct stack *s; |
| 500 |
struct jump jmp; |
| 501 |
|
| 502 |
switch(type) { |
| 503 |
case CTA_IP_V4_SRC: |
| 504 |
dir = __FILTER_ADDR_SRC; |
| 505 |
attr = NFCT_FILTER_SRC_IPV4; |
| 506 |
break; |
| 507 |
case CTA_IP_V4_DST: |
| 508 |
dir = __FILTER_ADDR_DST; |
| 509 |
attr = NFCT_FILTER_DST_IPV4; |
| 510 |
break; |
| 511 |
default: |
| 512 |
return 0; |
| 513 |
} |
| 514 |
|
| 515 |
/* nothing to filter, skip */ |
| 516 |
if (f->l3proto_elems[dir] == 0) |
| 517 |
return 0; |
| 518 |
|
| 519 |
/* XXX: 127 maximum IPs + 3 jumps in the three-level iteration */ |
| 520 |
s = stack_create(sizeof(struct jump), 3 + 127); |
| 521 |
if (s == NULL) { |
| 522 |
errno = ENOMEM; |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
jt = 1; |
| 527 |
if (f->logic[attr] == NFCT_FILTER_LOGIC_POSITIVE) |
| 528 |
label_continue = 1; |
| 529 |
else |
| 530 |
label_continue = 2; |
| 531 |
|
| 532 |
j = 0; |
| 533 |
j += nfct_bsf_load_payload_offset(this, j); |
| 534 |
j += nfct_bsf_find_attr(this, CTA_TUPLE_ORIG, j); |
| 535 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 536 |
j += nfct_bsf_add_attr_data_offset(this, j); |
| 537 |
j += nfct_bsf_find_attr(this, CTA_TUPLE_IP, j); |
| 538 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 539 |
j += nfct_bsf_add_attr_data_offset(this, j); |
| 540 |
j += nfct_bsf_find_attr(this, type, j); |
| 541 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 542 |
j += nfct_bsf_x_equal_a(this, j); |
| 543 |
|
| 544 |
for (i = 0; i < f->l3proto_elems[dir]; i++) { |
| 545 |
int ip = f->l3proto[dir][i].addr & f->l3proto[dir][i].mask; |
| 546 |
|
| 547 |
j += nfct_bsf_load_attr(this, BPF_W, j); |
| 548 |
j += nfct_bsf_alu_and(this, f->l3proto[dir][i].mask, j); |
| 549 |
j += nfct_bsf_cmp_k_stack(this, ip, jt - j, j, s); |
| 550 |
} |
| 551 |
|
| 552 |
while (stack_pop(s, &jmp) != -1) |
| 553 |
this[jmp.line].jt += jmp.jt + j; |
| 554 |
|
| 555 |
if (f->logic[attr] == NFCT_FILTER_LOGIC_NEGATIVE) |
| 556 |
j += nfct_bsf_jump_to(this, 1, j); |
| 557 |
|
| 558 |
j += nfct_bsf_ret_verdict(this, NFCT_FILTER_REJECT, j); |
| 559 |
|
| 560 |
stack_destroy(s); |
| 561 |
|
| 562 |
return j; |
| 563 |
} |
| 564 |
|
| 565 |
static int |
| 566 |
bsf_add_saddr_ipv4_filter(const struct nfct_filter *f, struct sock_filter *this) |
| 567 |
{ |
| 568 |
return bsf_add_addr_ipv4_filter(f, this, CTA_IP_V4_SRC); |
| 569 |
} |
| 570 |
|
| 571 |
static int |
| 572 |
bsf_add_daddr_ipv4_filter(const struct nfct_filter *f, struct sock_filter *this) |
| 573 |
{ |
| 574 |
return bsf_add_addr_ipv4_filter(f, this, CTA_IP_V4_DST); |
| 575 |
} |
| 576 |
|
| 577 |
static int |
| 578 |
bsf_add_addr_ipv6_filter(const struct nfct_filter *f, |
| 579 |
struct sock_filter *this, |
| 580 |
unsigned int type) |
| 581 |
{ |
| 582 |
unsigned int i, j, dir, attr; |
| 583 |
unsigned int label_continue, jf; |
| 584 |
struct stack *s; |
| 585 |
struct jump jmp; |
| 586 |
|
| 587 |
switch(type) { |
| 588 |
case CTA_IP_V6_SRC: |
| 589 |
dir = __FILTER_ADDR_SRC; |
| 590 |
attr = NFCT_FILTER_SRC_IPV6; |
| 591 |
break; |
| 592 |
case CTA_IP_V6_DST: |
| 593 |
dir = __FILTER_ADDR_DST; |
| 594 |
attr = NFCT_FILTER_DST_IPV6; |
| 595 |
break; |
| 596 |
default: |
| 597 |
return 0; |
| 598 |
} |
| 599 |
|
| 600 |
/* nothing to filter, skip */ |
| 601 |
if (f->l3proto_elems_ipv6[dir] == 0) |
| 602 |
return 0; |
| 603 |
|
| 604 |
/* XXX: 80 jumps (4*20) + 3 jumps in the three-level iteration */ |
| 605 |
s = stack_create(sizeof(struct jump), 3 + 80); |
| 606 |
if (s == NULL) { |
| 607 |
errno = ENOMEM; |
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
jf = 1; |
| 612 |
if (f->logic[attr] == NFCT_FILTER_LOGIC_POSITIVE) { |
| 613 |
label_continue = 1; |
| 614 |
} else { |
| 615 |
label_continue = 2; |
| 616 |
} |
| 617 |
|
| 618 |
j = 0; |
| 619 |
j += nfct_bsf_load_payload_offset(this, j); |
| 620 |
j += nfct_bsf_find_attr(this, CTA_TUPLE_ORIG, j); |
| 621 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 622 |
/* no need to access attribute payload, we are using nest-based finder |
| 623 |
* j += nfct_bsf_add_attr_data_offset(this, j); */ |
| 624 |
j += nfct_bsf_find_attr_nest(this, CTA_TUPLE_IP, j); |
| 625 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 626 |
j += nfct_bsf_find_attr_nest(this, type, j); |
| 627 |
j += nfct_bsf_cmp_k_stack(this, 0, label_continue - j, j, s); |
| 628 |
j += nfct_bsf_x_equal_a(this, j); |
| 629 |
|
| 630 |
for (i = 0; i < f->l3proto_elems_ipv6[dir]; i++) { |
| 631 |
int k, offset; |
| 632 |
|
| 633 |
for (k = 0, offset = 0; k < 4; k++, offset += 4) { |
| 634 |
int ip = f->l3proto_ipv6[dir][i].addr[k] & |
| 635 |
f->l3proto_ipv6[dir][i].mask[k]; |
| 636 |
|
| 637 |
j += nfct_bsf_load_attr_offset(this, BPF_W, offset, j); |
| 638 |
j += nfct_bsf_alu_and(this, |
| 639 |
f->l3proto_ipv6[dir][i].mask[k], |
| 640 |
j); |
| 641 |
if (k < 3) { |
| 642 |
j += nfct_bsf_cmp_k_stack_jf(this, ip, |
| 643 |
(3 - k) * 3 + 1, |
| 644 |
j, s); |
| 645 |
} else { |
| 646 |
/* last word: jump if true */ |
| 647 |
j += nfct_bsf_cmp_k_stack(this, ip, jf - j, |
| 648 |
j, s); |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
while (stack_pop(s, &jmp) != -1) { |
| 654 |
if (jmp.jt) { |
| 655 |
this[jmp.line].jt += jmp.jt + j; |
| 656 |
} |
| 657 |
if (jmp.jf) { |
| 658 |
this[jmp.line].jf += jmp.jf; |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
if (f->logic[attr] == NFCT_FILTER_LOGIC_NEGATIVE) |
| 663 |
j += nfct_bsf_jump_to(this, 1, j); |
| 664 |
|
| 665 |
j += nfct_bsf_ret_verdict(this, NFCT_FILTER_REJECT, j); |
| 666 |
|
| 667 |
stack_destroy(s); |
| 668 |
|
| 669 |
return j; |
| 670 |
} |
| 671 |
|
| 672 |
static int |
| 673 |
bsf_add_saddr_ipv6_filter(const struct nfct_filter *f, struct sock_filter *this) |
| 674 |
{ |
| 675 |
return bsf_add_addr_ipv6_filter(f, this, CTA_IP_V6_SRC); |
| 676 |
} |
| 677 |
|
| 678 |
static int |
| 679 |
bsf_add_daddr_ipv6_filter(const struct nfct_filter *f, struct sock_filter *this) |
| 680 |
{ |
| 681 |
return bsf_add_addr_ipv6_filter(f, this, CTA_IP_V6_DST); |
| 682 |
} |
| 683 |
|
| 684 |
static int |
| 685 |
bsf_add_mark_filter(const struct nfct_filter *f, struct sock_filter *this) |
| 686 |
{ |
| 687 |
unsigned int i, j; |
| 688 |
unsigned int jt; |
| 689 |
struct stack *s; |
| 690 |
struct jump jmp; |
| 691 |
struct sock_filter __code = { |
| 692 |
/* if (A == 0) skip next two */ |
| 693 |
.code = BPF_JMP|BPF_JEQ|BPF_K, |
| 694 |
.k = 0, |
| 695 |
.jt = 2, |
| 696 |
.jf = 0, |
| 697 |
}; |
| 698 |
|
| 699 |
/* nothing to filter, skip */ |
| 700 |
if (f->mark_elems == 0) |
| 701 |
return 0; |
| 702 |
|
| 703 |
/* XXX: see bsf_add_addr_ipv4_filter() */ |
| 704 |
s = stack_create(sizeof(struct jump), 3 + 127); |
| 705 |
if (s == NULL) { |
| 706 |
errno = ENOMEM; |
| 707 |
return 0; |
| 708 |
} |
| 709 |
|
| 710 |
jt = 1; |
| 711 |
j = 0; |
| 712 |
j += nfct_bsf_load_payload_offset(this, j); /* A = nla header offset */ |
| 713 |
j += nfct_bsf_find_attr(this, CTA_MARK, j); /* A = CTA_MARK offset, started from A */ |
| 714 |
memcpy(&this[j], &__code, sizeof(__code)); /* if A == 0 skip next two op */ |
| 715 |
j += NEW_POS(__code); |
| 716 |
j += nfct_bsf_x_equal_a(this, j); /* X = A <CTA_MARK offset> */ |
| 717 |
j += nfct_bsf_load_attr(this, BPF_W, j); /* A = skb->data[X:X + BPF_W] */ |
| 718 |
j += nfct_bsf_x_equal_a(this, j); /* X = A <CTA_MARK value> */ |
| 719 |
|
| 720 |
for (i = 0; i < f->mark_elems; i++) { |
| 721 |
int mark = f->mark[i].val & f->mark[i].mask; |
| 722 |
|
| 723 |
j += nfct_bsf_alu_and(this, f->mark[i].mask, j); |
| 724 |
j += nfct_bsf_cmp_k_stack(this, mark, jt - j, j, s); |
| 725 |
j += nfct_bsf_a_equal_x(this, j); |
| 726 |
} |
| 727 |
|
| 728 |
while (stack_pop(s, &jmp) != -1) |
| 729 |
this[jmp.line].jt += jmp.jt + j; |
| 730 |
|
| 731 |
if (f->logic[NFCT_FILTER_MARK] == NFCT_FILTER_LOGIC_NEGATIVE) |
| 732 |
j += nfct_bsf_jump_to(this, 1, j); |
| 733 |
|
| 734 |
j += nfct_bsf_ret_verdict(this, NFCT_FILTER_REJECT, j); |
| 735 |
|
| 736 |
stack_destroy(s); |
| 737 |
|
| 738 |
return j; |
| 739 |
} |
| 740 |
|
| 741 |
static int |
| 742 |
bsf_add_zone_filter(const struct nfct_filter *f, struct sock_filter *this) |
| 743 |
{ |
| 744 |
unsigned int i, j; |
| 745 |
unsigned int jt; |
| 746 |
struct stack *s; |
| 747 |
struct jump jmp; |
| 748 |
struct sock_filter __code = { |
| 749 |
/* if (A == 0) skip next two */ |
| 750 |
.code = BPF_JMP|BPF_JEQ|BPF_K, |
| 751 |
.k = 0, |
| 752 |
.jt = 2, |
| 753 |
.jf = 0, |
| 754 |
}; |
| 755 |
|
| 756 |
/* nothing to filter, skip */ |
| 757 |
if (f->zone_elems == 0) |
| 758 |
return 0; |
| 759 |
|
| 760 |
/* 127 max filterable zones. One JMP instruction per zone. */ |
| 761 |
s = stack_create(sizeof(struct jump), 127); |
| 762 |
if (s == NULL) { |
| 763 |
errno = ENOMEM; |
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
jt = 1; |
| 768 |
j = 0; |
| 769 |
j += nfct_bsf_load_payload_offset(this, j); /* A = nla header offset */ |
| 770 |
j += nfct_bsf_find_attr(this, CTA_ZONE, j); /* A = CTA_ZONE offset, started from A */ |
| 771 |
memcpy(&this[j], &__code, sizeof(__code)); /* if A == 0 skip next two op */ |
| 772 |
j += NEW_POS(__code); |
| 773 |
j += nfct_bsf_x_equal_a(this, j); /* X = A <CTA_ZONE offset> */ |
| 774 |
j += nfct_bsf_load_attr(this, BPF_H, j); /* A = skb->data[X:X + BPF_H] */ |
| 775 |
|
| 776 |
for (i = 0; i < f->zone_elems; i++) { |
| 777 |
j += nfct_bsf_cmp_k_stack(this, f->zone[i], jt - j, j, s); |
| 778 |
} |
| 779 |
|
| 780 |
while (stack_pop(s, &jmp) != -1) |
| 781 |
this[jmp.line].jt += jmp.jt + j; |
| 782 |
|
| 783 |
if (f->logic[NFCT_FILTER_ZONE] == NFCT_FILTER_LOGIC_NEGATIVE) |
| 784 |
j += nfct_bsf_jump_to(this, 1, j); |
| 785 |
|
| 786 |
j += nfct_bsf_ret_verdict(this, NFCT_FILTER_REJECT, j); |
| 787 |
|
| 788 |
stack_destroy(s); |
| 789 |
|
| 790 |
return j; |
| 791 |
} |
| 792 |
|
| 793 |
/* this buffer must be big enough to store all the autogenerated lines */ |
| 794 |
#define BSF_BUFFER_SIZE 2048 |
| 795 |
|
| 796 |
int __setup_netlink_socket_filter(int fd, struct nfct_filter *f) |
| 797 |
{ |
| 798 |
struct sock_filter bsf[BSF_BUFFER_SIZE]; |
| 799 |
struct sock_fprog sf; |
| 800 |
unsigned int j = 0, from = 0; |
| 801 |
|
| 802 |
memset(bsf, 0, sizeof(bsf)); |
| 803 |
|
| 804 |
j += bsf_cmp_subsys(&bsf[j], j, NFNL_SUBSYS_CTNETLINK); |
| 805 |
j += nfct_bsf_ret_verdict(bsf, NFCT_FILTER_ACCEPT, j); |
| 806 |
show_filter(bsf, from, j, "--- check subsys ---"); |
| 807 |
from = j; |
| 808 |
j += bsf_add_proto_filter(f, &bsf[j]); |
| 809 |
show_filter(bsf, from, j, "---- check proto ----"); |
| 810 |
from = j; |
| 811 |
j += bsf_add_saddr_ipv4_filter(f, &bsf[j]); |
| 812 |
show_filter(bsf, from, j, "---- check src IPv4 ----"); |
| 813 |
from = j; |
| 814 |
j += bsf_add_daddr_ipv4_filter(f, &bsf[j]); |
| 815 |
show_filter(bsf, from, j, "---- check dst IPv4 ----"); |
| 816 |
from = j; |
| 817 |
j += bsf_add_saddr_ipv6_filter(f, &bsf[j]); |
| 818 |
show_filter(bsf, from, j, "---- check src IPv6 ----"); |
| 819 |
from = j; |
| 820 |
j += bsf_add_daddr_ipv6_filter(f, &bsf[j]); |
| 821 |
show_filter(bsf, from, j, "---- check dst IPv6 ----"); |
| 822 |
from = j; |
| 823 |
j += bsf_add_state_filter(f, &bsf[j]); |
| 824 |
show_filter(bsf, from, j, "---- check state ----"); |
| 825 |
from = j; |
| 826 |
j += bsf_add_mark_filter(f, &bsf[j]); |
| 827 |
show_filter(bsf, from, j, "---- check mark ----"); |
| 828 |
from = j; |
| 829 |
j += bsf_add_zone_filter(f, &bsf[j]); |
| 830 |
show_filter(bsf, from, j, "---- check zone ----"); |
| 831 |
from = j; |
| 832 |
|
| 833 |
/* nothing to filter, skip */ |
| 834 |
if (j == 0) |
| 835 |
return 0; |
| 836 |
|
| 837 |
j += nfct_bsf_ret_verdict(bsf, NFCT_FILTER_ACCEPT, j); |
| 838 |
show_filter(bsf, from, j, "---- final verdict ----"); |
| 839 |
from = j; |
| 840 |
|
| 841 |
sf.len = j; |
| 842 |
sf.filter = bsf; |
| 843 |
|
| 844 |
return setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &sf, sizeof(sf)); |
| 845 |
} |