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 extern struct ematch_util u32_ematch_util
;
27 static void u32_print_usage(FILE *fd
)
30 "Usage: u32(ALIGN VALUE MASK at [ nexthdr+ ] OFFSET)\n" \
31 "where: ALIGN := { u8 | u16 | u32 }\n" \
33 "Example: u32(u16 0x1122 0xffff at nexthdr+4)\n");
36 static int u32_parse_eopt(struct nlmsghdr
*n
, struct tcf_ematch_hdr
*hdr
,
41 unsigned long key
, mask
, offmask
= 0, offset
;
42 struct tc_u32_key u_key
;
44 memset(&u_key
, 0, sizeof(u_key
));
46 #define PARSE_ERR(CARG, FMT, ARGS...) \
47 em_parse_error(EINVAL, args, CARG, &u32_ematch_util, FMT ,##ARGS)
50 return PARSE_ERR(args
, "u32: missing arguments");
52 if (!bstrcmp(args
, "u8"))
54 else if (!bstrcmp(args
, "u16"))
56 else if (!bstrcmp(args
, "u32"))
59 return PARSE_ERR(args
, "u32: invalid alignment");
63 return PARSE_ERR(a
, "u32: missing key");
67 return PARSE_ERR(a
, "u32: invalid key, must be numeric");
71 return PARSE_ERR(a
, "u32: missing mask");
74 if (mask
== ULONG_MAX
)
75 return PARSE_ERR(a
, "u32: invalid mask, must be numeric");
78 if (a
== NULL
|| bstrcmp(a
, "at") != 0)
79 return PARSE_ERR(a
, "u32: missing \"at\"");
83 return PARSE_ERR(a
, "u32: missing offset");
85 nh_len
= strlen("nexthdr+");
86 if (a
->len
> nh_len
&& !memcmp(a
->data
, "nexthdr+", nh_len
)) {
87 char buf
[a
->len
- nh_len
+ 1];
89 memcpy(buf
, a
->data
+ nh_len
, a
->len
- nh_len
);
90 offset
= strtoul(buf
, NULL
, 0);
91 } else if (!bstrcmp(a
, "nexthdr+")) {
94 return PARSE_ERR(a
, "u32: missing offset");
99 if (offset
== ULONG_MAX
)
100 return PARSE_ERR(a
, "u32: invalid offset");
103 return PARSE_ERR(a
->next
, "u32: unexpected trailer");
108 return PARSE_ERR(a
, "Illegal key (>0xFF)");
110 return PARSE_ERR(a
, "Illegal mask (>0xFF)");
112 key
<<= 24 - ((offset
& 3) * 8);
113 mask
<<= 24 - ((offset
& 3) * 8);
119 return PARSE_ERR(a
, "Illegal key (>0xFFFF)");
121 return PARSE_ERR(a
, "Illegal mask (>0xFFFF)");
123 if ((offset
& 3) == 0) {
135 return PARSE_ERR(a
, "u32: invalid offset alignment, " \
136 "must be aligned to 4.");
143 u_key
.offmask
= offmask
;
145 addraw_l(n
, MAX_MSG
, hdr
, sizeof(*hdr
));
146 addraw_l(n
, MAX_MSG
, &u_key
, sizeof(u_key
));
152 static int u32_print_eopt(FILE *fd
, struct tcf_ematch_hdr
*hdr
, void *data
,
155 struct tc_u32_key
*u_key
= data
;
157 if (data_len
< sizeof(*u_key
)) {
158 fprintf(stderr
, "U32 header size mismatch\n");
162 fprintf(fd
, "%08x/%08x at %s%d",
163 (unsigned int) ntohl(u_key
->val
),
164 (unsigned int) ntohl(u_key
->mask
),
165 u_key
->offmask
? "nexthdr+" : "",
171 struct ematch_util u32_ematch_util
= {
173 .kind_num
= TCF_EM_U32
,
174 .parse_eopt
= u32_parse_eopt
,
175 .print_eopt
= u32_print_eopt
,
176 .print_usage
= u32_print_usage