2 * em_cmp.c Simle coparison 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>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
25 #include <linux/tc_ematch/tc_em_cmp.h>
27 extern struct ematch_util cmp_ematch_util;
29 static void cmp_print_usage(FILE *fd)
32 "Usage: cmp(ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE)\n" \
33 "where: ALIGN := { u8 | u16 | u32 }\n" \
34 " ATTRS := [ layer LAYER ] [ mask MASK ] [ trans ]\n" \
35 " LAYER := { link | header | next-header | 0..%d }\n" \
37 "Example: cmp(u16 at 3 layer 2 mask 0xff00 gt 20)\n",
41 static int cmp_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
46 unsigned long offset = 0, layer = TCF_LAYER_NETWORK, mask = 0, value = 0;
47 int offset_present = 0, value_present = 0;
48 struct tcf_em_cmp cmp;
50 memset(&cmp, 0, sizeof(cmp));
52 #define PARSE_ERR(CARG, FMT, ARGS...) \
53 em_parse_error(EINVAL, args, CARG, &cmp_ematch_util, FMT ,##ARGS)
56 return PARSE_ERR(args, "cmp: missing arguments");
58 if (!bstrcmp(args, "u8"))
59 align = TCF_EM_ALIGN_U8;
60 else if (!bstrcmp(args, "u16"))
61 align = TCF_EM_ALIGN_U16;
62 else if (!bstrcmp(args, "u32"))
63 align = TCF_EM_ALIGN_U32;
65 return PARSE_ERR(args, "cmp: invalid alignment");
67 for (a = bstr_next(args); a; a = bstr_next(a)) {
68 if (!bstrcmp(a, "at")) {
70 return PARSE_ERR(a, "cmp: missing argument");
74 if (offset == ULONG_MAX)
75 return PARSE_ERR(a, "cmp: invalid offset, " \
79 } else if (!bstrcmp(a, "layer")) {
81 return PARSE_ERR(a, "cmp: missing argument");
84 layer = parse_layer(a);
85 if (layer == INT_MAX) {
87 if (layer == ULONG_MAX)
88 return PARSE_ERR(a, "cmp: invalid " \
92 if (layer > TCF_LAYER_MAX)
93 return PARSE_ERR(a, "cmp: illegal layer, " \
94 "must be in 0..%d", TCF_LAYER_MAX);
95 } else if (!bstrcmp(a, "mask")) {
97 return PARSE_ERR(a, "cmp: missing argument");
101 if (mask == ULONG_MAX)
102 return PARSE_ERR(a, "cmp: invalid mask");
103 } else if (!bstrcmp(a, "trans")) {
104 cmp.flags |= TCF_EM_CMP_TRANS;
105 } else if (!bstrcmp(a, "eq") || !bstrcmp(a, "gt") ||
108 if (!bstrcmp(a, "eq"))
109 opnd = TCF_EM_OPND_EQ;
110 else if (!bstrcmp(a, "gt"))
111 opnd = TCF_EM_OPND_GT;
112 else if (!bstrcmp(a, "lt"))
113 opnd = TCF_EM_OPND_LT;
116 return PARSE_ERR(a, "cmp: missing argument");
120 if (value == ULONG_MAX)
121 return PARSE_ERR(a, "cmp: invalid value");
125 return PARSE_ERR(a, "nbyte: unknown parameter");
128 if (offset_present == 0 || value_present == 0)
129 return PARSE_ERR(a, "cmp: offset and value required");
131 cmp.val = (__u32) value;
132 cmp.mask = (__u32) mask;
133 cmp.off = (__u16) offset;
134 cmp.align = (__u8) align;
135 cmp.layer = (__u8) layer;
136 cmp.opnd = (__u8) opnd;
138 addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
139 addraw_l(n, MAX_MSG, &cmp, sizeof(cmp));
145 static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
148 struct tcf_em_cmp *cmp = data;
150 if (data_len < sizeof(*cmp)) {
151 fprintf(stderr, "CMP header size mismatch\n");
155 if (cmp->align == TCF_EM_ALIGN_U8)
157 else if (cmp->align == TCF_EM_ALIGN_U16)
159 else if (cmp->align == TCF_EM_ALIGN_U16)
162 fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer);
165 fprintf(fd, "mask 0x%x ", cmp->mask);
167 if (cmp->flags & TCF_EM_CMP_TRANS)
168 fprintf(fd, "trans ");
170 if (cmp->opnd == TCF_EM_OPND_EQ)
172 else if (cmp->opnd == TCF_EM_OPND_LT)
174 else if (cmp->opnd == TCF_EM_OPND_GT)
177 fprintf(fd, "%d", cmp->val);
182 struct ematch_util cmp_ematch_util = {
184 .kind_num = TCF_EM_CMP,
185 .parse_eopt = cmp_parse_eopt,
186 .print_eopt = cmp_print_eopt,
187 .print_usage = cmp_print_usage