1 #include <stdio.h>
2 #include <xtables.h>
3 #include <linux/netfilter/xt_length.h>
4
5 enum {
6 O_LENGTH = 0,
7 };
8
length_help(void)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
length_parse(struct xt_option_call * cb)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
length_print(const void * ip,const struct xt_entry_match * match,int numeric)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
length_save(const void * ip,const struct xt_entry_match * match)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
length_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)59 static int length_xlate(struct xt_xlate *xl,
60 const struct xt_xlate_mt_params *params)
61 {
62 const struct xt_length_info *info = (void *)params->match->data;
63
64 xt_xlate_add(xl, "meta length %s", info->invert ? "!= " : "");
65 if (info->min == info->max)
66 xt_xlate_add(xl, "%u", info->min);
67 else
68 xt_xlate_add(xl, "%u-%u", info->min, info->max);
69
70 return 1;
71 }
72
73
74 static struct xtables_match length_match = {
75 .family = NFPROTO_UNSPEC,
76 .name = "length",
77 .version = XTABLES_VERSION,
78 .size = XT_ALIGN(sizeof(struct xt_length_info)),
79 .userspacesize = XT_ALIGN(sizeof(struct xt_length_info)),
80 .help = length_help,
81 .print = length_print,
82 .save = length_save,
83 .x6_parse = length_parse,
84 .x6_options = length_opts,
85 .xlate = length_xlate,
86 };
87
_init(void)88 void _init(void)
89 {
90 xtables_register_match(&length_match);
91 }
92