1 #include <stdio.h>
2 #include <xtables.h>
3 #include <linux/netfilter/xt_ipcomp.h>
4
5 enum {
6 O_compSPI = 0,
7 O_compRES,
8 };
9
comp_help(void)10 static void comp_help(void)
11 {
12 printf(
13 "comp match options:\n"
14 "[!] --ipcompspi spi[:spi]\n"
15 " match spi (range)\n");
16 }
17
18 static const struct xt_option_entry comp_opts[] = {
19 {.name = "ipcompspi", .id = O_compSPI, .type = XTTYPE_UINT32RC,
20 .flags = XTOPT_INVERT | XTOPT_PUT,
21 XTOPT_POINTER(struct xt_ipcomp, spis)},
22 {.name = "compres", .id = O_compRES, .type = XTTYPE_NONE},
23 XTOPT_TABLEEND,
24 };
25 #undef s
26
comp_parse(struct xt_option_call * cb)27 static void comp_parse(struct xt_option_call *cb)
28 {
29 struct xt_ipcomp *compinfo = cb->data;
30
31 xtables_option_parse(cb);
32 switch (cb->entry->id) {
33 case O_compSPI:
34 if (cb->nvals == 1)
35 compinfo->spis[1] = compinfo->spis[0];
36 if (cb->invert)
37 compinfo->invflags |= XT_IPCOMP_INV_SPI;
38 break;
39 case O_compRES:
40 compinfo->hdrres = 1;
41 break;
42 }
43 }
44
45 static void
print_spis(const char * name,uint32_t min,uint32_t max,int invert)46 print_spis(const char *name, uint32_t min, uint32_t max,
47 int invert)
48 {
49 const char *inv = invert ? "!" : "";
50
51 if (min != 0 || max != 0xFFFFFFFF || invert) {
52 if (min == max)
53 printf("%s:%s%u", name, inv, min);
54 else
55 printf("%ss:%s%u:%u", name, inv, min, max);
56 }
57 }
58
comp_print(const void * ip,const struct xt_entry_match * match,int numeric)59 static void comp_print(const void *ip, const struct xt_entry_match *match,
60 int numeric)
61 {
62 const struct xt_ipcomp *comp = (struct xt_ipcomp *)match->data;
63
64 printf(" comp ");
65 print_spis("spi", comp->spis[0], comp->spis[1],
66 comp->invflags & XT_IPCOMP_INV_SPI);
67
68 if (comp->hdrres)
69 printf(" reserved");
70
71 if (comp->invflags & ~XT_IPCOMP_INV_MASK)
72 printf(" Unknown invflags: 0x%X",
73 comp->invflags & ~XT_IPCOMP_INV_MASK);
74 }
75
comp_save(const void * ip,const struct xt_entry_match * match)76 static void comp_save(const void *ip, const struct xt_entry_match *match)
77 {
78 const struct xt_ipcomp *compinfo = (struct xt_ipcomp *)match->data;
79 bool inv_spi = compinfo->invflags & XT_IPCOMP_INV_SPI;
80
81 if (!(compinfo->spis[0] == 0
82 && compinfo->spis[1] == UINT32_MAX
83 && !inv_spi)) {
84 printf("%s --ipcompspi ", inv_spi ? " !" : "");
85 if (compinfo->spis[0]
86 != compinfo->spis[1])
87 printf("%u:%u",
88 compinfo->spis[0],
89 compinfo->spis[1]);
90 else
91 printf("%u",
92 compinfo->spis[0]);
93 }
94
95 if (compinfo->hdrres != 0 )
96 printf(" --compres");
97 }
98
comp_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)99 static int comp_xlate(struct xt_xlate *xl,
100 const struct xt_xlate_mt_params *params)
101 {
102 const struct xt_ipcomp *compinfo =
103 (struct xt_ipcomp *)params->match->data;
104
105 /* ignore compinfo->hdrres like kernel's xt_ipcomp.c does */
106
107 xt_xlate_add(xl, "comp cpi %s",
108 compinfo->invflags & XT_IPCOMP_INV_SPI ? "!= " : "");
109 if (compinfo->spis[0] != compinfo->spis[1])
110 xt_xlate_add(xl, "%u-%u", compinfo->spis[0],
111 compinfo->spis[1]);
112 else
113 xt_xlate_add(xl, "%u", compinfo->spis[0]);
114
115 return 1;
116 }
117
118 static struct xtables_match comp_mt_reg = {
119 .name = "ipcomp",
120 .version = XTABLES_VERSION,
121 .family = NFPROTO_UNSPEC,
122 .size = XT_ALIGN(sizeof(struct xt_ipcomp)),
123 .userspacesize = XT_ALIGN(sizeof(struct xt_ipcomp)),
124 .help = comp_help,
125 .print = comp_print,
126 .save = comp_save,
127 .x6_parse = comp_parse,
128 .x6_options = comp_opts,
129 .xlate = comp_xlate,
130 };
131
132 void
_init(void)133 _init(void)
134 {
135 xtables_register_match(&comp_mt_reg);
136 };
137
138