1 #include <stdio.h> 2 #include <xtables.h> 3 #include <linux/netfilter/xt_length.h> 4 5 enum { 6 O_LENGTH = 0, 7 }; 8 9 static void length_help(void) 10 { 11 printf( 12 "length match options:\n" 13 "[!] --length length[:length] Match packet length against value or range\n" 14 " of values (inclusive)\n"); 15 } 16 17 static const struct xt_option_entry length_opts[] = { 18 {.name = "length", .id = O_LENGTH, .type = XTTYPE_UINT16RC, 19 .flags = XTOPT_MAND | XTOPT_INVERT}, 20 XTOPT_TABLEEND, 21 }; 22 23 static void length_parse(struct xt_option_call *cb) 24 { 25 struct xt_length_info *info = cb->data; 26 27 xtables_option_parse(cb); 28 info->min = cb->val.u16_range[0]; 29 info->max = cb->val.u16_range[0]; 30 if (cb->nvals >= 2) 31 info->max = cb->val.u16_range[1]; 32 if (cb->invert) 33 info->invert = 1; 34 } 35 36 static void 37 length_print(const void *ip, const struct xt_entry_match *match, int numeric) 38 { 39 const struct xt_length_info *info = (void *)match->data; 40 41 printf(" length %s", info->invert ? "!" : ""); 42 if (info->min == info->max) 43 printf("%u", info->min); 44 else 45 printf("%u:%u", info->min, info->max); 46 } 47 48 static void length_save(const void *ip, const struct xt_entry_match *match) 49 { 50 const struct xt_length_info *info = (void *)match->data; 51 52 printf("%s --length ", info->invert ? " !" : ""); 53 if (info->min == info->max) 54 printf("%u", info->min); 55 else 56 printf("%u:%u", info->min, info->max); 57 } 58 59 static struct xtables_match length_match = { 60 .family = NFPROTO_UNSPEC, 61 .name = "length", 62 .version = XTABLES_VERSION, 63 .size = XT_ALIGN(sizeof(struct xt_length_info)), 64 .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)), 65 .help = length_help, 66 .print = length_print, 67 .save = length_save, 68 .x6_parse = length_parse, 69 .x6_options = length_opts, 70 }; 71 72 void _init(void) 73 { 74 xtables_register_match(&length_match); 75 } 76