Debian Codegraph @

tc/em_ipset.c

raw
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * em_ipset.c IPset Ematch
4 *
5 * (C) 2012 Florian Westphal <[email protected]>
6 *
7 * Parts taken from iptables libxt_set.h:
8 * Copyright (C) 2000-2002 Joakim Axelsson <[email protected]>
9 * Patrick Schaaf <[email protected]>
10 * Martin Josefsson <[email protected]>
11 * Copyright (C) 2003-2010 Jozsef Kadlecsik <[email protected]>
12 */
13
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <netdb.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <getopt.h>
22
23 #include <xtables.h>
24 #include <linux/netfilter/ipset/ip_set.h>
25
26 #ifndef IPSET_INVALID_ID
27 typedef __u16 ip_set_id_t;
28
29 enum ip_set_dim {
30 IPSET_DIM_ZERO = 0,
31 IPSET_DIM_ONE,
32 IPSET_DIM_TWO,
33 IPSET_DIM_THREE,
34 IPSET_DIM_MAX = 6,
35 };
36 #endif /* IPSET_INVALID_ID */
37
38 #include <linux/netfilter/xt_set.h>
39 #include "m_ematch.h"
40
41 #ifndef IPSET_INVALID_ID
42 #define IPSET_INVALID_ID 65535
43 #define SO_IP_SET 83
44
45 union ip_set_name_index {
46 char name[IPSET_MAXNAMELEN];
47 __u16 index;
48 };
49
50 #define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
51 struct ip_set_req_get_set {
52 unsigned int op;
53 unsigned int version;
54 union ip_set_name_index set;
55 };
56
57 #define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
58 /* Uses ip_set_req_get_set */
59
60 #define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
61 struct ip_set_req_version {
62 unsigned int op;
63 unsigned int version;
64 };
65 #endif /* IPSET_INVALID_ID */
66
67 extern struct ematch_util ipset_ematch_util;
68
69 static int get_version(unsigned int *version)
70 {
71 int res, sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
72 struct ip_set_req_version req_version;
73 socklen_t size = sizeof(req_version);
74
75 if (sockfd < 0) {
76 fputs("Can't open socket to ipset.\n", stderr);
77 return -1;
78 }
79
80 req_version.op = IP_SET_OP_VERSION;
81 res = getsockopt(sockfd, SOL_IP, SO_IP_SET, &req_version, &size);
82 if (res != 0) {
83 perror("xt_set getsockopt");
84 close(sockfd);
85 return -1;
86 }
87
88 *version = req_version.version;
89 return sockfd;
90 }
91
92 static int do_getsockopt(struct ip_set_req_get_set *req)
93 {
94 int sockfd, res;
95 socklen_t size = sizeof(struct ip_set_req_get_set);
96
97 sockfd = get_version(&req->version);
98 if (sockfd < 0)
99 return -1;
100 res = getsockopt(sockfd, SOL_IP, SO_IP_SET, req, &size);
101 if (res != 0)
102 perror("Problem when communicating with ipset");
103 close(sockfd);
104 if (res != 0)
105 return -1;
106
107 if (size != sizeof(struct ip_set_req_get_set)) {
108 fprintf(stderr,
109 "Incorrect return size from kernel during ipset lookup, (want %zu, got %zu)\n",
110 sizeof(struct ip_set_req_get_set), (size_t)size);
111 return -1;
112 }
113
114 return res;
115 }
116
117 static int
118 get_set_byid(char *setname, unsigned int idx)
119 {
120 struct ip_set_req_get_set req;
121 int res;
122
123 req.op = IP_SET_OP_GET_BYINDEX;
124 req.set.index = idx;
125 res = do_getsockopt(&req);
126 if (res != 0)
127 return -1;
128 if (req.set.name[0] == '\0') {
129 fprintf(stderr,
130 "Set with index %i in kernel doesn't exist.\n", idx);
131 return -1;
132 }
133
134 strncpy(setname, req.set.name, IPSET_MAXNAMELEN);
135 return 0;
136 }
137
138 static int
139 get_set_byname(const char *setname, struct xt_set_info *info)
140 {
141 struct ip_set_req_get_set req;
142 int res;
143
144 req.op = IP_SET_OP_GET_BYNAME;
145 strlcpy(req.set.name, setname, IPSET_MAXNAMELEN);
146 res = do_getsockopt(&req);
147 if (res != 0)
148 return -1;
149 if (req.set.index == IPSET_INVALID_ID)
150 return -1;
151 info->index = req.set.index;
152 return 0;
153 }
154
155 static int
156 parse_dirs(const char *opt_arg, struct xt_set_info *info)
157 {
158 char *saved = strdup(opt_arg);
159 char *ptr, *tmp = saved;
160
161 if (!tmp) {
162 perror("strdup");
163 return -1;
164 }
165
166 while (info->dim < IPSET_DIM_MAX && tmp != NULL) {
167 info->dim++;
168 ptr = strsep(&tmp, ",");
169 if (strncmp(ptr, "src", 3) == 0)
170 info->flags |= (1 << info->dim);
171 else if (strncmp(ptr, "dst", 3) != 0) {
172 fputs("You must specify (the comma separated list of) 'src' or 'dst'\n", stderr);
173 free(saved);
174 return -1;
175 }
176 }
177
178 if (tmp)
179 fprintf(stderr, "Can't be more src/dst options than %u", IPSET_DIM_MAX);
180 free(saved);
181 return tmp ? -1 : 0;
182 }
183
184 static void ipset_print_usage(FILE *fd)
185 {
186 fprintf(fd,
187 "Usage: ipset(SETNAME FLAGS)\n" \
188 "where: SETNAME:= string\n" \
189 " FLAGS := { FLAG[,FLAGS] }\n" \
190 " FLAG := { src | dst }\n" \
191 "\n" \
192 "Example: 'ipset(bulk src,dst)'\n");
193 }
194
195 static int ipset_parse_eopt(struct nlmsghdr *n, struct tcf_ematch_hdr *hdr,
196 struct bstr *args)
197 {
198 struct xt_set_info set_info = {};
199 int ret;
200
201 #define PARSE_ERR(CARG, FMT, ARGS...) \
202 em_parse_error(EINVAL, args, CARG, &ipset_ematch_util, FMT, ##ARGS)
203
204 if (args == NULL)
205 return PARSE_ERR(args, "ipset: missing set name");
206
207 if (args->len >= IPSET_MAXNAMELEN)
208 return PARSE_ERR(args, "ipset: set name too long (max %u)", IPSET_MAXNAMELEN - 1);
209 ret = get_set_byname(args->data, &set_info);
210 if (ret < 0)
211 return PARSE_ERR(args, "ipset: unknown set name '%s'", args->data);
212
213 if (args->next == NULL)
214 return PARSE_ERR(args, "ipset: missing set flags");
215
216 args = bstr_next(args);
217 if (parse_dirs(args->data, &set_info))
218 return PARSE_ERR(args, "ipset: error parsing set flags");
219
220 if (args->next) {
221 args = bstr_next(args);
222 return PARSE_ERR(args, "ipset: unknown parameter");
223 }
224
225 addraw_l(n, MAX_MSG, hdr, sizeof(*hdr));
226 addraw_l(n, MAX_MSG, &set_info, sizeof(set_info));
227
228 #undef PARSE_ERR
229 return 0;
230 }
231
232 static int ipset_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
233 int data_len)
234 {
235 int i;
236 char setname[IPSET_MAXNAMELEN];
237 const struct xt_set_info *set_info = data;
238
239 if (data_len != sizeof(*set_info)) {
240 fprintf(stderr, "xt_set_info struct size mismatch\n");
241 return -1;
242 }
243
244 if (get_set_byid(setname, set_info->index))
245 return -1;
246 fputs(setname, fd);
247 for (i = 1; i <= set_info->dim; i++) {
248 fprintf(fd, "%s%s", i == 1 ? " " : ",", set_info->flags & (1 << i) ? "src" : "dst");
249 }
250
251 return 0;
252 }
253
254 struct ematch_util ipset_ematch_util = {
255 .kind = "ipset",
256 .kind_num = TCF_EM_IPSET,
257 .parse_eopt = ipset_parse_eopt,
258 .print_eopt = ipset_print_eopt,
259 .print_usage = ipset_print_usage
260 };