2 * em_cmp.c Simple comparison Ematch
4 * This program is free software; you can distribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Thomas Graf <tgraf@suug.ch>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
23 #include <linux/tc_ematch/tc_em_cmp.h>
25 extern struct ematch_util cmp_ematch_util
;
27 static void cmp_print_usage(FILE *fd
)
30 "Usage: cmp(ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE)\n" \
31 "where: ALIGN := { u8 | u16 | u32 }\n" \
32 " ATTRS := [ layer LAYER ] [ mask MASK ] [ trans ]\n" \
33 " LAYER := { link | network | transport | 0..%d }\n" \
35 "Example: cmp(u16 at 3 layer 2 mask 0xff00 gt 20)\n",
39 static int cmp_parse_eopt(struct nlmsghdr
*n
, struct tcf_ematch_hdr
*hdr
,
44 unsigned long offset
= 0, layer
= TCF_LAYER_NETWORK
, mask
= 0, value
= 0;
45 int offset_present
= 0, value_present
= 0;
46 struct tcf_em_cmp cmp
= {};
48 #define PARSE_ERR(CARG, FMT, ARGS...) \
49 em_parse_error(EINVAL, args, CARG, &cmp_ematch_util, FMT, ##ARGS)
52 return PARSE_ERR(args
, "cmp: missing arguments");
54 if (!bstrcmp(args
, "u8"))
55 align
= TCF_EM_ALIGN_U8
;
56 else if (!bstrcmp(args
, "u16"))
57 align
= TCF_EM_ALIGN_U16
;
58 else if (!bstrcmp(args
, "u32"))
59 align
= TCF_EM_ALIGN_U32
;
61 return PARSE_ERR(args
, "cmp: invalid alignment");
63 for (a
= bstr_next(args
); a
; a
= bstr_next(a
)) {
64 if (!bstrcmp(a
, "at")) {
66 return PARSE_ERR(a
, "cmp: missing argument");
70 if (offset
== ULONG_MAX
)
71 return PARSE_ERR(a
, "cmp: invalid offset, " \
75 } else if (!bstrcmp(a
, "layer")) {
77 return PARSE_ERR(a
, "cmp: missing argument");
80 layer
= parse_layer(a
);
81 if (layer
== INT_MAX
) {
83 if (layer
== ULONG_MAX
)
84 return PARSE_ERR(a
, "cmp: invalid " \
88 if (layer
> TCF_LAYER_MAX
)
89 return PARSE_ERR(a
, "cmp: illegal layer, " \
90 "must be in 0..%d", TCF_LAYER_MAX
);
91 } else if (!bstrcmp(a
, "mask")) {
93 return PARSE_ERR(a
, "cmp: missing argument");
97 if (mask
== ULONG_MAX
)
98 return PARSE_ERR(a
, "cmp: invalid mask");
99 } else if (!bstrcmp(a
, "trans")) {
100 cmp
.flags
|= TCF_EM_CMP_TRANS
;
101 } else if (!bstrcmp(a
, "eq") || !bstrcmp(a
, "gt") ||
104 if (!bstrcmp(a
, "eq"))
105 opnd
= TCF_EM_OPND_EQ
;
106 else if (!bstrcmp(a
, "gt"))
107 opnd
= TCF_EM_OPND_GT
;
108 else if (!bstrcmp(a
, "lt"))
109 opnd
= TCF_EM_OPND_LT
;
112 return PARSE_ERR(a
, "cmp: missing argument");
116 if (value
== ULONG_MAX
)
117 return PARSE_ERR(a
, "cmp: invalid value");
121 return PARSE_ERR(a
, "nbyte: unknown parameter");
124 if (offset_present
== 0 || value_present
== 0)
125 return PARSE_ERR(a
, "cmp: offset and value required");
127 cmp
.val
= (__u32
) value
;
128 cmp
.mask
= (__u32
) mask
;
129 cmp
.off
= (__u16
) offset
;
130 cmp
.align
= (__u8
) align
;
131 cmp
.layer
= (__u8
) layer
;
132 cmp
.opnd
= (__u8
) opnd
;
134 addraw_l(n
, MAX_MSG
, hdr
, sizeof(*hdr
));
135 addraw_l(n
, MAX_MSG
, &cmp
, sizeof(cmp
));
141 static int cmp_print_eopt(FILE *fd
, struct tcf_ematch_hdr
*hdr
, void *data
,
144 struct tcf_em_cmp
*cmp
= data
;
146 if (data_len
< sizeof(*cmp
)) {
147 fprintf(stderr
, "CMP header size mismatch\n");
151 if (cmp
->align
== TCF_EM_ALIGN_U8
)
153 else if (cmp
->align
== TCF_EM_ALIGN_U16
)
155 else if (cmp
->align
== TCF_EM_ALIGN_U32
)
158 fprintf(fd
, "at %d layer %d ", cmp
->off
, cmp
->layer
);
161 fprintf(fd
, "mask 0x%x ", cmp
->mask
);
163 if (cmp
->flags
& TCF_EM_CMP_TRANS
)
164 fprintf(fd
, "trans ");
166 if (cmp
->opnd
== TCF_EM_OPND_EQ
)
168 else if (cmp
->opnd
== TCF_EM_OPND_LT
)
170 else if (cmp
->opnd
== TCF_EM_OPND_GT
)
173 fprintf(fd
, "%d", cmp
->val
);
178 struct ematch_util cmp_ematch_util
= {
180 .kind_num
= TCF_EM_CMP
,
181 .parse_eopt
= cmp_parse_eopt
,
182 .print_eopt
= cmp_print_eopt
,
183 .print_usage
= cmp_print_usage