Debian Codegraph @

tc/em_u32.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * em_u32.c U32 Ematch
4 *
5 * Authors: Thomas Graf <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include "m_ematch.h"
19
20 extern struct ematch_util u32_ematch_util;
21
22 static void u32_print_usage(FILE *fd)
23 {
24 fprintf(fd,
25 "Usage: u32(ALIGN VALUE MASK at [ nexthdr+ ] OFFSET)\n" \
26 "where: ALIGN := { u8 | u16 | u32 }\n" \
27 "\n" \
28 "Example: u32(u16 0x1122 0xffff at nexthdr+4)\n");
29 }
30
31 static int u32_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
32 struct bstr *args)
33 {
34 struct bstr *a;
35 int align, nh_len;
36 unsigned long key, mask, offmask = 0, offset;
37 struct tc_u32_key u_key = {};
38
39 #define PARSE_ERR(CARG, FMT, ARGS...) \
40 em_parse_error(EINVAL, args, CARG, &u32_ematch_util, FMT, ##ARGS)
41
42 if (args == NULL)
43 return PARSE_ERR(args, "u32: missing arguments");
44
45 if (!bstrcmp(args, "u8"))
46 align = 1;
47 else if (!bstrcmp(args, "u16"))
48 align = 2;
49 else if (!bstrcmp(args, "u32"))
50 align = 4;
51 else
52 return PARSE_ERR(args, "u32: invalid alignment");
53
54 a = bstr_next(args);
55 if (a == NULL)
56 return PARSE_ERR(a, "u32: missing key");
57
58 key = bstrtoul(a);
59 if (key == ULONG_MAX)
60 return PARSE_ERR(a, "u32: invalid key, must be numeric");
61
62 a = bstr_next(a);
63 if (a == NULL)
64 return PARSE_ERR(a, "u32: missing mask");
65
66 mask = bstrtoul(a);
67 if (mask == ULONG_MAX)
68 return PARSE_ERR(a, "u32: invalid mask, must be numeric");
69
70 a = bstr_next(a);
71 if (a == NULL || bstrcmp(a, "at") != 0)
72 return PARSE_ERR(a, "u32: missing \"at\"");
73
74 a = bstr_next(a);
75 if (a == NULL)
76 return PARSE_ERR(a, "u32: missing offset");
77
78 nh_len = strlen("nexthdr+");
79 if (a->len > nh_len && !memcmp(a->data, "nexthdr+", nh_len)) {
80 char buf[a->len - nh_len + 1];
81
82 offmask = -1;
83 strncpy(buf, a->data + nh_len, a->len - nh_len + 1);
84 offset = strtoul(buf, NULL, 0);
85 } else if (!bstrcmp(a, "nexthdr+")) {
86 a = bstr_next(a);
87 if (a == NULL)
88 return PARSE_ERR(a, "u32: missing offset");
89 offset = bstrtoul(a);
90 } else
91 offset = bstrtoul(a);
92
93 if (offset == ULONG_MAX)
94 return PARSE_ERR(a, "u32: invalid offset");
95
96 if (a->next)
97 return PARSE_ERR(a->next, "u32: unexpected trailer");
98
99 switch (align) {
100 case 1:
101 if (key > 0xFF)
102 return PARSE_ERR(a, "Illegal key (>0xFF)");
103 if (mask > 0xFF)
104 return PARSE_ERR(a, "Illegal mask (>0xFF)");
105
106 key <<= 24 - ((offset & 3) * 8);
107 mask <<= 24 - ((offset & 3) * 8);
108 offset &= ~3;
109 break;
110
111 case 2:
112 if (key > 0xFFFF)
113 return PARSE_ERR(a, "Illegal key (>0xFFFF)");
114 if (mask > 0xFFFF)
115 return PARSE_ERR(a, "Illegal mask (>0xFFFF)");
116
117 if ((offset & 3) == 0) {
118 key <<= 16;
119 mask <<= 16;
120 }
121 offset &= ~3;
122 break;
123 }
124
125 key = htonl(key);
126 mask = htonl(mask);
127
128 if (offset % 4)
129 return PARSE_ERR(a, "u32: invalid offset alignment, " \
130 "must be aligned to 4.");
131
132 key &= mask;
133
134 u_key.mask = mask;
135 u_key.val = key;
136 u_key.off = offset;
137 u_key.offmask = offmask;
138
139 addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
140 addraw_l(n, MAX_MSG, &u_key, sizeof(u_key));
141
142 #undef PARSE_ERR
143 return 0;
144 }
145
146 static int u32_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
147 int data_len)
148 {
149 struct tc_u32_key *u_key = data;
150
151 if (data_len < sizeof(*u_key)) {
152 fprintf(stderr, "U32 header size mismatch\n");
153 return -1;
154 }
155
156 fprintf(fd, "%08x/%08x at %s%d",
157 (unsigned int) ntohl(u_key->val),
158 (unsigned int) ntohl(u_key->mask),
159 u_key->offmask ? "nexthdr+" : "",
160 u_key->off);
161
162 return 0;
163 }
164
165 struct ematch_util u32_ematch_util = {
166 .kind = "u32",
167 .kind_num = TCF_EM_U32,
168 .parse_eopt = u32_parse_eopt,
169 .print_eopt = u32_print_eopt,
170 .print_usage = u32_print_usage
171 };