]>
Commit | Line | Data |
---|---|---|
c7699875 | 1 | /* $USAGI: $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C)2004 USAGI/WIDE Project | |
ae665a52 | 5 | * |
c7699875 | 6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
ae665a52 | 10 | * |
c7699875 | 11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
ae665a52 | 15 | * |
c7699875 | 16 | * You should have received a copy of the GNU General Public License |
4d98ab00 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
c7699875 | 18 | */ |
19 | /* | |
20 | * based on iproute.c | |
21 | */ | |
22 | /* | |
23 | * Authors: | |
24 | * Masahide NAKAMURA @USAGI | |
25 | */ | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <netdb.h> | |
31 | #include <linux/netlink.h> | |
c7699875 | 32 | #include "utils.h" |
33 | #include "xfrm.h" | |
34 | #include "ip_common.h" | |
35 | ||
56f5daac | 36 | /* #define NLMSG_DELETEALL_BUF_SIZE (4096-512) */ |
9bec1a43 | 37 | #define NLMSG_DELETEALL_BUF_SIZE 8192 |
c7699875 | 38 | |
39 | /* | |
40 | * Receiving buffer defines: | |
41 | * nlmsg | |
42 | * data = struct xfrm_userpolicy_info | |
43 | * rtattr | |
44 | * data = struct xfrm_user_tmpl[] | |
45 | */ | |
46 | #define NLMSG_BUF_SIZE 4096 | |
47 | #define RTA_BUF_SIZE 2048 | |
48 | #define XFRM_TMPLS_BUF_SIZE 1024 | |
e4f054f0 | 49 | #define CTX_BUF_SIZE 256 |
c7699875 | 50 | |
51 | static void usage(void) __attribute__((noreturn)); | |
52 | ||
53 | static void usage(void) | |
54 | { | |
8589eb4e MC |
55 | fprintf(stderr, |
56 | "Usage: ip xfrm policy { add | update } SELECTOR dir DIR [ ctx CTX ]\n" | |
57 | " [ mark MARK [ mask MASK ] ] [ index INDEX ] [ ptype PTYPE ]\n" | |
58 | " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n" | |
59 | " [ if_id IF_ID ] [ LIMIT-LIST ] [ TMPL-LIST ]\n" | |
60 | "Usage: ip xfrm policy { delete | get } { SELECTOR | index INDEX } dir DIR\n" | |
61 | " [ ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]\n" | |
62 | "Usage: ip xfrm policy { deleteall | list } [ nosock ] [ SELECTOR ] [ dir DIR ]\n" | |
63 | " [ index INDEX ] [ ptype PTYPE ] [ action ACTION ] [ priority PRIORITY ]\n" | |
64 | " [ flag FLAG-LIST ]\n" | |
65 | "Usage: ip xfrm policy flush [ ptype PTYPE ]\n" | |
66 | "Usage: ip xfrm policy count\n" | |
67 | "Usage: ip xfrm policy set [ hthresh4 LBITS RBITS ] [ hthresh6 LBITS RBITS ]\n" | |
68 | "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n" | |
38dd041b AC |
69 | "UPSPEC := proto { { tcp | udp | sctp | dccp } [ sport PORT ] [ dport PORT ] |\n" |
70 | " { icmp | ipv6-icmp | mobility-header } [ type NUMBER ] [ code NUMBER ] |\n" | |
71 | " gre [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n" | |
8589eb4e MC |
72 | "DIR := in | out | fwd\n" |
73 | "PTYPE := main | sub\n" | |
74 | "ACTION := allow | block\n" | |
75 | "FLAG-LIST := [ FLAG-LIST ] FLAG\n" | |
76 | "FLAG := localok | icmp\n" | |
77 | "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n" | |
78 | "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n" | |
79 | " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n" | |
80 | "TMPL-LIST := [ TMPL-LIST ] tmpl TMPL\n" | |
81 | "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n" | |
82 | "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n" | |
83 | "XFRM-PROTO := "); | |
84 | fprintf(stderr, | |
85 | "%s | %s | %s | %s | %s\n", | |
86 | strxf_xfrmproto(IPPROTO_ESP), | |
87 | strxf_xfrmproto(IPPROTO_AH), | |
88 | strxf_xfrmproto(IPPROTO_COMP), | |
89 | strxf_xfrmproto(IPPROTO_ROUTING), | |
90 | strxf_xfrmproto(IPPROTO_DSTOPTS)); | |
91 | fprintf(stderr, | |
92 | "MODE := transport | tunnel | beet | ro | in_trigger\n" | |
93 | "LEVEL := required | use\n"); | |
c7699875 | 94 | |
95 | exit(-1); | |
96 | } | |
97 | ||
98 | static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp) | |
99 | { | |
100 | int argc = *argcp; | |
101 | char **argv = *argvp; | |
102 | ||
103 | if (strcmp(*argv, "in") == 0) | |
104 | *dir = XFRM_POLICY_IN; | |
105 | else if (strcmp(*argv, "out") == 0) | |
106 | *dir = XFRM_POLICY_OUT; | |
107 | else if (strcmp(*argv, "fwd") == 0) | |
108 | *dir = XFRM_POLICY_FWD; | |
109 | else | |
e8740e42 | 110 | invarg("DIR value is invalid", *argv); |
c7699875 | 111 | |
112 | *argcp = argc; | |
113 | *argvp = argv; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
972938e9 MN |
118 | static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp) |
119 | { | |
120 | int argc = *argcp; | |
121 | char **argv = *argvp; | |
122 | ||
123 | if (strcmp(*argv, "main") == 0) | |
124 | *ptype = XFRM_POLICY_TYPE_MAIN; | |
125 | else if (strcmp(*argv, "sub") == 0) | |
126 | *ptype = XFRM_POLICY_TYPE_SUB; | |
127 | else | |
e8740e42 | 128 | invarg("PTYPE value is invalid", *argv); |
972938e9 MN |
129 | |
130 | *argcp = argc; | |
131 | *argvp = argv; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
c1fa2253 MN |
136 | static int xfrm_policy_flag_parse(__u8 *flags, int *argcp, char ***argvp) |
137 | { | |
138 | int argc = *argcp; | |
139 | char **argv = *argvp; | |
140 | int len = strlen(*argv); | |
141 | ||
142 | if (len > 2 && strncmp(*argv, "0x", 2) == 0) { | |
143 | __u8 val = 0; | |
144 | ||
145 | if (get_u8(&val, *argv, 16)) | |
e8740e42 | 146 | invarg("FLAG value is invalid", *argv); |
c1fa2253 MN |
147 | *flags = val; |
148 | } else { | |
149 | while (1) { | |
150 | if (strcmp(*argv, "localok") == 0) | |
151 | *flags |= XFRM_POLICY_LOCALOK; | |
c0635644 UW |
152 | else if (strcmp(*argv, "icmp") == 0) |
153 | *flags |= XFRM_POLICY_ICMP; | |
c1fa2253 MN |
154 | else { |
155 | PREV_ARG(); /* back track */ | |
156 | break; | |
157 | } | |
158 | ||
159 | if (!NEXT_ARG_OK()) | |
160 | break; | |
161 | NEXT_ARG(); | |
162 | } | |
163 | } | |
164 | ||
165 | *argcp = argc; | |
166 | *argvp = argv; | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
c7699875 | 171 | static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl, |
172 | int *argcp, char ***argvp) | |
173 | { | |
174 | int argc = *argcp; | |
175 | char **argv = *argvp; | |
176 | char *idp = NULL; | |
177 | ||
178 | while (1) { | |
179 | if (strcmp(*argv, "mode") == 0) { | |
180 | NEXT_ARG(); | |
181 | xfrm_mode_parse(&tmpl->mode, &argc, &argv); | |
182 | } else if (strcmp(*argv, "reqid") == 0) { | |
183 | NEXT_ARG(); | |
184 | xfrm_reqid_parse(&tmpl->reqid, &argc, &argv); | |
185 | } else if (strcmp(*argv, "level") == 0) { | |
186 | NEXT_ARG(); | |
187 | ||
188 | if (strcmp(*argv, "required") == 0) | |
189 | tmpl->optional = 0; | |
190 | else if (strcmp(*argv, "use") == 0) | |
191 | tmpl->optional = 1; | |
192 | else | |
e8740e42 | 193 | invarg("LEVEL value is invalid\n", *argv); |
c7699875 | 194 | |
195 | } else { | |
196 | if (idp) { | |
197 | PREV_ARG(); /* back track */ | |
198 | break; | |
199 | } | |
200 | idp = *argv; | |
e6e0b60f | 201 | preferred_family = AF_UNSPEC; |
c7699875 | 202 | xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family, |
7809c616 | 203 | 0, &argc, &argv); |
e6e0b60f | 204 | preferred_family = tmpl->family; |
c7699875 | 205 | } |
206 | ||
207 | if (!NEXT_ARG_OK()) | |
208 | break; | |
209 | ||
210 | NEXT_ARG(); | |
211 | } | |
212 | if (argc == *argcp) | |
213 | missarg("TMPL"); | |
214 | ||
215 | *argcp = argc; | |
216 | *argvp = argv; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
e4f054f0 JL |
221 | int xfrm_sctx_parse(char *ctxstr, char *s, |
222 | struct xfrm_user_sec_ctx *sctx) | |
223 | { | |
224 | int slen; | |
225 | ||
226 | slen = strlen(s) + 1; | |
227 | ||
228 | sctx->exttype = XFRMA_SEC_CTX; | |
229 | sctx->ctx_doi = 1; | |
230 | sctx->ctx_alg = 1; | |
231 | sctx->ctx_len = slen; | |
232 | sctx->len = sizeof(struct xfrm_user_sec_ctx) + slen; | |
233 | memcpy(ctxstr, s, slen); | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
56f5daac | 238 | static int xfrm_policy_modify(int cmd, unsigned int flags, int argc, char **argv) |
c7699875 | 239 | { |
240 | struct rtnl_handle rth; | |
241 | struct { | |
242 | struct nlmsghdr n; | |
243 | struct xfrm_userpolicy_info xpinfo; | |
244 | char buf[RTA_BUF_SIZE]; | |
d17b136f PS |
245 | } req = { |
246 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo)), | |
247 | .n.nlmsg_flags = NLM_F_REQUEST | flags, | |
248 | .n.nlmsg_type = cmd, | |
249 | .xpinfo.sel.family = preferred_family, | |
250 | .xpinfo.lft.soft_byte_limit = XFRM_INF, | |
251 | .xpinfo.lft.hard_byte_limit = XFRM_INF, | |
252 | .xpinfo.lft.soft_packet_limit = XFRM_INF, | |
253 | .xpinfo.lft.hard_packet_limit = XFRM_INF, | |
254 | }; | |
c7699875 | 255 | char *dirp = NULL; |
7809c616 | 256 | char *selp = NULL; |
972938e9 | 257 | char *ptypep = NULL; |
e4f054f0 | 258 | char *sctxp = NULL; |
d17b136f PS |
259 | struct xfrm_userpolicy_type upt = {}; |
260 | char tmpls_buf[XFRM_TMPLS_BUF_SIZE] = {}; | |
c7699875 | 261 | int tmpls_len = 0; |
ee675e87 | 262 | struct xfrm_mark mark = {0, 0}; |
e4f054f0 JL |
263 | struct { |
264 | struct xfrm_user_sec_ctx sctx; | |
265 | char str[CTX_BUF_SIZE]; | |
d17b136f | 266 | } ctx = {}; |
aed63ae1 EB |
267 | bool is_if_id_set = false; |
268 | __u32 if_id = 0; | |
c7699875 | 269 | |
270 | while (argc > 0) { | |
271 | if (strcmp(*argv, "dir") == 0) { | |
272 | if (dirp) | |
273 | duparg("dir", *argv); | |
274 | dirp = *argv; | |
275 | ||
276 | NEXT_ARG(); | |
277 | xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv); | |
e4f054f0 JL |
278 | } else if (strcmp(*argv, "ctx") == 0) { |
279 | char *context; | |
280 | ||
281 | if (sctxp) | |
282 | duparg("ctx", *argv); | |
283 | sctxp = *argv; | |
284 | NEXT_ARG(); | |
285 | context = *argv; | |
286 | xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx); | |
ee675e87 JHS |
287 | } else if (strcmp(*argv, "mark") == 0) { |
288 | xfrm_parse_mark(&mark, &argc, &argv); | |
c7699875 | 289 | } else if (strcmp(*argv, "index") == 0) { |
290 | NEXT_ARG(); | |
291 | if (get_u32(&req.xpinfo.index, *argv, 0)) | |
e8740e42 | 292 | invarg("INDEX value is invalid", *argv); |
972938e9 MN |
293 | } else if (strcmp(*argv, "ptype") == 0) { |
294 | if (ptypep) | |
295 | duparg("ptype", *argv); | |
296 | ptypep = *argv; | |
297 | ||
298 | NEXT_ARG(); | |
299 | xfrm_policy_ptype_parse(&upt.type, &argc, &argv); | |
c7699875 | 300 | } else if (strcmp(*argv, "action") == 0) { |
301 | NEXT_ARG(); | |
302 | if (strcmp(*argv, "allow") == 0) | |
303 | req.xpinfo.action = XFRM_POLICY_ALLOW; | |
304 | else if (strcmp(*argv, "block") == 0) | |
305 | req.xpinfo.action = XFRM_POLICY_BLOCK; | |
306 | else | |
e8740e42 | 307 | invarg("ACTION value is invalid\n", *argv); |
c7699875 | 308 | } else if (strcmp(*argv, "priority") == 0) { |
309 | NEXT_ARG(); | |
310 | if (get_u32(&req.xpinfo.priority, *argv, 0)) | |
e8740e42 | 311 | invarg("PRIORITY value is invalid", *argv); |
c1fa2253 MN |
312 | } else if (strcmp(*argv, "flag") == 0) { |
313 | NEXT_ARG(); | |
314 | xfrm_policy_flag_parse(&req.xpinfo.flags, &argc, | |
315 | &argv); | |
c7699875 | 316 | } else if (strcmp(*argv, "limit") == 0) { |
317 | NEXT_ARG(); | |
318 | xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv); | |
319 | } else if (strcmp(*argv, "tmpl") == 0) { | |
320 | struct xfrm_user_tmpl *tmpl; | |
321 | ||
322 | if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) { | |
323 | fprintf(stderr, "Too many tmpls: buffer overflow\n"); | |
324 | exit(1); | |
325 | } | |
326 | tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len); | |
327 | ||
328 | tmpl->family = preferred_family; | |
329 | tmpl->aalgos = (~(__u32)0); | |
330 | tmpl->ealgos = (~(__u32)0); | |
331 | tmpl->calgos = (~(__u32)0); | |
332 | ||
333 | NEXT_ARG(); | |
334 | xfrm_tmpl_parse(tmpl, &argc, &argv); | |
335 | ||
336 | tmpls_len += sizeof(*tmpl); | |
aed63ae1 EB |
337 | } else if (strcmp(*argv, "if_id") == 0) { |
338 | NEXT_ARG(); | |
339 | if (get_u32(&if_id, *argv, 0)) | |
340 | invarg("IF_ID value is invalid", *argv); | |
341 | is_if_id_set = true; | |
7809c616 | 342 | } else { |
343 | if (selp) | |
344 | duparg("unknown", *argv); | |
345 | selp = *argv; | |
346 | ||
347 | xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv); | |
348 | if (preferred_family == AF_UNSPEC) | |
349 | preferred_family = req.xpinfo.sel.family; | |
350 | } | |
c7699875 | 351 | |
352 | argc--; argv++; | |
353 | } | |
354 | ||
355 | if (!dirp) { | |
e8740e42 | 356 | fprintf(stderr, "Not enough information: DIR is required.\n"); |
c7699875 | 357 | exit(1); |
358 | } | |
359 | ||
972938e9 MN |
360 | if (ptypep) { |
361 | addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, | |
362 | (void *)&upt, sizeof(upt)); | |
363 | } | |
364 | ||
c7699875 | 365 | if (tmpls_len > 0) { |
366 | addattr_l(&req.n, sizeof(req), XFRMA_TMPL, | |
367 | (void *)tmpls_buf, tmpls_len); | |
368 | } | |
369 | ||
b5574165 | 370 | if (mark.m) { |
ee675e87 JHS |
371 | int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, |
372 | (void *)&mark, sizeof(mark)); | |
373 | if (r < 0) { | |
56f5daac | 374 | fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__); |
ee675e87 JHS |
375 | exit(1); |
376 | } | |
377 | } | |
378 | ||
e4f054f0 JL |
379 | if (sctxp) { |
380 | addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX, | |
381 | (void *)&ctx, ctx.sctx.len); | |
382 | } | |
ee675e87 | 383 | |
aed63ae1 EB |
384 | if (is_if_id_set) |
385 | addattr32(&req.n, sizeof(req.buf), XFRMA_IF_ID, if_id); | |
386 | ||
c7699875 | 387 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
388 | exit(1); | |
389 | ||
390 | if (req.xpinfo.sel.family == AF_UNSPEC) | |
391 | req.xpinfo.sel.family = AF_INET; | |
392 | ||
86bf43c7 | 393 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
c7699875 | 394 | exit(2); |
395 | ||
396 | rtnl_close(&rth); | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
972938e9 MN |
401 | static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo, |
402 | __u8 ptype) | |
c7699875 | 403 | { |
404 | if (!filter.use) | |
405 | return 1; | |
406 | ||
cd21ae40 PS |
407 | if (filter.xpinfo.sel.family != AF_UNSPEC && |
408 | filter.xpinfo.sel.family != xpinfo->sel.family) | |
409 | return 0; | |
410 | ||
c7699875 | 411 | if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask) |
412 | return 0; | |
413 | ||
20e4840a TE |
414 | if (filter.filter_socket && (xpinfo->dir >= XFRM_POLICY_MAX)) |
415 | return 0; | |
416 | ||
972938e9 MN |
417 | if ((ptype^filter.ptype)&filter.ptype_mask) |
418 | return 0; | |
419 | ||
c7699875 | 420 | if (filter.sel_src_mask) { |
eaa34ee3 | 421 | if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr, |
422 | filter.sel_src_mask)) | |
c7699875 | 423 | return 0; |
424 | } | |
425 | ||
426 | if (filter.sel_dst_mask) { | |
eaa34ee3 | 427 | if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr, |
428 | filter.sel_dst_mask)) | |
c7699875 | 429 | return 0; |
430 | } | |
431 | ||
432 | if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask) | |
433 | return 0; | |
434 | ||
435 | if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask) | |
436 | return 0; | |
437 | ||
438 | if (filter.upspec_sport_mask) { | |
439 | if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask) | |
440 | return 0; | |
441 | } | |
442 | ||
443 | if (filter.upspec_dport_mask) { | |
444 | if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask) | |
445 | return 0; | |
446 | } | |
447 | ||
448 | if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask) | |
449 | return 0; | |
450 | ||
451 | if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask) | |
452 | return 0; | |
453 | ||
454 | if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask) | |
455 | return 0; | |
456 | ||
c1fa2253 MN |
457 | if (filter.policy_flags_mask) |
458 | if ((xpinfo->flags & filter.xpinfo.flags) == 0) | |
459 | return 0; | |
460 | ||
c7699875 | 461 | return 1; |
462 | } | |
463 | ||
cd554f2c | 464 | int xfrm_policy_print(struct nlmsghdr *n, void *arg) |
c7699875 | 465 | { |
56f5daac SH |
466 | struct rtattr *tb[XFRMA_MAX+1]; |
467 | struct rtattr *rta; | |
c595c790 SH |
468 | struct xfrm_userpolicy_info *xpinfo = NULL; |
469 | struct xfrm_user_polexpire *xpexp = NULL; | |
470 | struct xfrm_userpolicy_id *xpid = NULL; | |
972938e9 | 471 | __u8 ptype = XFRM_POLICY_TYPE_MAIN; |
56f5daac | 472 | FILE *fp = (FILE *)arg; |
c595c790 | 473 | int len = n->nlmsg_len; |
c7699875 | 474 | |
475 | if (n->nlmsg_type != XFRM_MSG_NEWPOLICY && | |
90f93024 | 476 | n->nlmsg_type != XFRM_MSG_DELPOLICY && |
669ae748 | 477 | n->nlmsg_type != XFRM_MSG_UPDPOLICY && |
90f93024 | 478 | n->nlmsg_type != XFRM_MSG_POLEXPIRE) { |
c7699875 | 479 | fprintf(stderr, "Not a policy: %08x %08x %08x\n", |
480 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
481 | return 0; | |
482 | } | |
483 | ||
669ae748 SH |
484 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) { |
485 | xpid = NLMSG_DATA(n); | |
af1b6a41 | 486 | len -= NLMSG_SPACE(sizeof(*xpid)); |
669ae748 | 487 | } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) { |
90f93024 SH |
488 | xpexp = NLMSG_DATA(n); |
489 | xpinfo = &xpexp->pol; | |
af1b6a41 | 490 | len -= NLMSG_SPACE(sizeof(*xpexp)); |
90f93024 SH |
491 | } else { |
492 | xpexp = NULL; | |
493 | xpinfo = NLMSG_DATA(n); | |
af1b6a41 | 494 | len -= NLMSG_SPACE(sizeof(*xpinfo)); |
90f93024 SH |
495 | } |
496 | ||
c7699875 | 497 | if (len < 0) { |
498 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
499 | return -1; | |
500 | } | |
501 | ||
669ae748 SH |
502 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) |
503 | rta = XFRMPID_RTA(xpid); | |
504 | else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) | |
90f93024 SH |
505 | rta = XFRMPEXP_RTA(xpexp); |
506 | else | |
507 | rta = XFRMP_RTA(xpinfo); | |
508 | ||
509 | parse_rtattr(tb, XFRMA_MAX, rta, len); | |
c7699875 | 510 | |
972938e9 MN |
511 | if (tb[XFRMA_POLICY_TYPE]) { |
512 | struct xfrm_userpolicy_type *upt; | |
513 | ||
514 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) { | |
515 | fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n"); | |
516 | return -1; | |
517 | } | |
9f1370c0 | 518 | upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]); |
972938e9 MN |
519 | ptype = upt->type; |
520 | } | |
521 | ||
522 | if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype)) | |
523 | return 0; | |
524 | ||
525 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) | |
526 | fprintf(fp, "Deleted "); | |
527 | else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY) | |
528 | fprintf(fp, "Updated "); | |
529 | else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) | |
530 | fprintf(fp, "Expired "); | |
531 | ||
c595c790 | 532 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) { |
56f5daac | 533 | /* xfrm_policy_id_print(); */ |
669ae748 SH |
534 | if (!tb[XFRMA_POLICY]) { |
535 | fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n"); | |
536 | return -1; | |
c595c790 | 537 | } |
669ae748 SH |
538 | if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) { |
539 | fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n"); | |
540 | return -1; | |
541 | } | |
9f1370c0 | 542 | xpinfo = RTA_DATA(tb[XFRMA_POLICY]); |
669ae748 | 543 | } |
c7699875 | 544 | |
56e8ad38 | 545 | xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL); |
c7699875 | 546 | |
90f93024 SH |
547 | if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) { |
548 | fprintf(fp, "\t"); | |
549 | fprintf(fp, "hard %u", xpexp->hard); | |
550 | fprintf(fp, "%s", _SL_); | |
551 | } | |
552 | ||
7809c616 | 553 | if (oneline) |
554 | fprintf(fp, "\n"); | |
669ae748 | 555 | fflush(fp); |
7809c616 | 556 | |
c7699875 | 557 | return 0; |
558 | } | |
559 | ||
560 | static int xfrm_policy_get_or_delete(int argc, char **argv, int delete, | |
86bf43c7 | 561 | struct nlmsghdr **answer) |
c7699875 | 562 | { |
563 | struct rtnl_handle rth; | |
564 | struct { | |
565 | struct nlmsghdr n; | |
566 | struct xfrm_userpolicy_id xpid; | |
972938e9 | 567 | char buf[RTA_BUF_SIZE]; |
d17b136f PS |
568 | } req = { |
569 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid)), | |
570 | .n.nlmsg_flags = NLM_F_REQUEST, | |
571 | .n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY | |
572 | : XFRM_MSG_GETPOLICY, | |
573 | }; | |
c7699875 | 574 | char *dirp = NULL; |
575 | char *selp = NULL; | |
576 | char *indexp = NULL; | |
972938e9 | 577 | char *ptypep = NULL; |
e4f054f0 | 578 | char *sctxp = NULL; |
d17b136f | 579 | struct xfrm_userpolicy_type upt = {}; |
ee675e87 | 580 | struct xfrm_mark mark = {0, 0}; |
e4f054f0 JL |
581 | struct { |
582 | struct xfrm_user_sec_ctx sctx; | |
583 | char str[CTX_BUF_SIZE]; | |
d17b136f | 584 | } ctx = {}; |
c7699875 | 585 | |
586 | while (argc > 0) { | |
587 | if (strcmp(*argv, "dir") == 0) { | |
588 | if (dirp) | |
589 | duparg("dir", *argv); | |
590 | dirp = *argv; | |
591 | ||
592 | NEXT_ARG(); | |
593 | xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv); | |
594 | ||
e4f054f0 JL |
595 | } else if (strcmp(*argv, "ctx") == 0) { |
596 | char *context; | |
597 | ||
598 | if (sctxp) | |
599 | duparg("ctx", *argv); | |
600 | sctxp = *argv; | |
601 | NEXT_ARG(); | |
602 | context = *argv; | |
603 | xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx); | |
ee675e87 JHS |
604 | } else if (strcmp(*argv, "mark") == 0) { |
605 | xfrm_parse_mark(&mark, &argc, &argv); | |
c7699875 | 606 | } else if (strcmp(*argv, "index") == 0) { |
607 | if (indexp) | |
608 | duparg("index", *argv); | |
609 | indexp = *argv; | |
610 | ||
611 | NEXT_ARG(); | |
612 | if (get_u32(&req.xpid.index, *argv, 0)) | |
e8740e42 | 613 | invarg("INDEX value is invalid", *argv); |
c7699875 | 614 | |
972938e9 MN |
615 | } else if (strcmp(*argv, "ptype") == 0) { |
616 | if (ptypep) | |
617 | duparg("ptype", *argv); | |
618 | ptypep = *argv; | |
619 | ||
620 | NEXT_ARG(); | |
621 | xfrm_policy_ptype_parse(&upt.type, &argc, &argv); | |
622 | ||
7809c616 | 623 | } else { |
624 | if (selp) | |
625 | invarg("unknown", *argv); | |
626 | selp = *argv; | |
627 | ||
628 | xfrm_selector_parse(&req.xpid.sel, &argc, &argv); | |
629 | if (preferred_family == AF_UNSPEC) | |
630 | preferred_family = req.xpid.sel.family; | |
631 | ||
632 | } | |
c7699875 | 633 | |
634 | argc--; argv++; | |
635 | } | |
636 | ||
637 | if (!dirp) { | |
e8740e42 | 638 | fprintf(stderr, "Not enough information: DIR is required.\n"); |
c7699875 | 639 | exit(1); |
640 | } | |
972938e9 MN |
641 | if (ptypep) { |
642 | addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, | |
643 | (void *)&upt, sizeof(upt)); | |
644 | } | |
c7699875 | 645 | if (!selp && !indexp) { |
e8740e42 | 646 | fprintf(stderr, "Not enough information: either SELECTOR or INDEX is required.\n"); |
c7699875 | 647 | exit(1); |
648 | } | |
649 | if (selp && indexp) | |
650 | duparg2("SELECTOR", "INDEX"); | |
651 | ||
652 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
653 | exit(1); | |
654 | ||
655 | if (req.xpid.sel.family == AF_UNSPEC) | |
656 | req.xpid.sel.family = AF_INET; | |
657 | ||
ee675e87 JHS |
658 | if (mark.m & mark.v) { |
659 | int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, | |
660 | (void *)&mark, sizeof(mark)); | |
661 | if (r < 0) { | |
56f5daac | 662 | fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__); |
ee675e87 JHS |
663 | exit(1); |
664 | } | |
665 | } | |
666 | ||
e4f054f0 JL |
667 | if (sctxp) { |
668 | addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX, | |
669 | (void *)&ctx, ctx.sctx.len); | |
670 | } | |
671 | ||
86bf43c7 | 672 | if (rtnl_talk(&rth, &req.n, answer) < 0) |
c7699875 | 673 | exit(2); |
674 | ||
675 | rtnl_close(&rth); | |
676 | ||
677 | return 0; | |
678 | } | |
679 | ||
680 | static int xfrm_policy_delete(int argc, char **argv) | |
681 | { | |
86bf43c7 | 682 | return xfrm_policy_get_or_delete(argc, argv, 1, NULL); |
c7699875 | 683 | } |
684 | ||
685 | static int xfrm_policy_get(int argc, char **argv) | |
686 | { | |
86bf43c7 | 687 | struct nlmsghdr *n = NULL; |
c7699875 | 688 | |
86bf43c7 | 689 | xfrm_policy_get_or_delete(argc, argv, 0, &n); |
c7699875 | 690 | |
cd554f2c | 691 | if (xfrm_policy_print(n, (void *)stdout) < 0) { |
c7699875 | 692 | fprintf(stderr, "An error :-)\n"); |
693 | exit(1); | |
694 | } | |
695 | ||
86bf43c7 | 696 | free(n); |
c7699875 | 697 | return 0; |
698 | } | |
699 | ||
700 | /* | |
701 | * With an existing policy of nlmsg, make new nlmsg for deleting the policy | |
702 | * and store it to buffer. | |
703 | */ | |
cd554f2c | 704 | static int xfrm_policy_keep(struct nlmsghdr *n, void *arg) |
c7699875 | 705 | { |
706 | struct xfrm_buffer *xb = (struct xfrm_buffer *)arg; | |
707 | struct rtnl_handle *rth = xb->rth; | |
708 | struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n); | |
709 | int len = n->nlmsg_len; | |
972938e9 MN |
710 | struct rtattr *tb[XFRMA_MAX+1]; |
711 | __u8 ptype = XFRM_POLICY_TYPE_MAIN; | |
c7699875 | 712 | struct nlmsghdr *new_n; |
713 | struct xfrm_userpolicy_id *xpid; | |
714 | ||
715 | if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) { | |
716 | fprintf(stderr, "Not a policy: %08x %08x %08x\n", | |
717 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
718 | return 0; | |
719 | } | |
720 | ||
721 | len -= NLMSG_LENGTH(sizeof(*xpinfo)); | |
722 | if (len < 0) { | |
723 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
724 | return -1; | |
725 | } | |
726 | ||
972938e9 MN |
727 | parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len); |
728 | ||
729 | if (tb[XFRMA_POLICY_TYPE]) { | |
730 | struct xfrm_userpolicy_type *upt; | |
731 | ||
732 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) { | |
733 | fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n"); | |
734 | return -1; | |
735 | } | |
9f1370c0 | 736 | upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]); |
972938e9 MN |
737 | ptype = upt->type; |
738 | } | |
739 | ||
740 | if (!xfrm_policy_filter_match(xpinfo, ptype)) | |
c7699875 | 741 | return 0; |
742 | ||
5474d440 TE |
743 | /* can't delete socket policies */ |
744 | if (xpinfo->dir >= XFRM_POLICY_MAX) | |
745 | return 0; | |
746 | ||
d5eb0564 AH |
747 | if (xb->offset + NLMSG_LENGTH(sizeof(*xpid)) > xb->size) |
748 | return 0; | |
c7699875 | 749 | |
750 | new_n = (struct nlmsghdr *)(xb->buf + xb->offset); | |
751 | new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid)); | |
752 | new_n->nlmsg_flags = NLM_F_REQUEST; | |
753 | new_n->nlmsg_type = XFRM_MSG_DELPOLICY; | |
754 | new_n->nlmsg_seq = ++rth->seq; | |
755 | ||
756 | xpid = NLMSG_DATA(new_n); | |
757 | memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel)); | |
758 | xpid->dir = xpinfo->dir; | |
759 | xpid->index = xpinfo->index; | |
760 | ||
0c7d651b TE |
761 | if (tb[XFRMA_MARK]) { |
762 | int r = addattr_l(new_n, xb->size, XFRMA_MARK, | |
763 | (void *)RTA_DATA(tb[XFRMA_MARK]), tb[XFRMA_MARK]->rta_len); | |
764 | if (r < 0) { | |
765 | fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__); | |
766 | exit(1); | |
767 | } | |
768 | } | |
769 | ||
c7699875 | 770 | xb->offset += new_n->nlmsg_len; |
56f5daac | 771 | xb->nlmsg_count++; |
c7699875 | 772 | |
773 | return 0; | |
774 | } | |
775 | ||
9bec1a43 | 776 | static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall) |
c7699875 | 777 | { |
7809c616 | 778 | char *selp = NULL; |
c7699875 | 779 | struct rtnl_handle rth; |
780 | ||
cd21ae40 | 781 | if (argc > 0 || preferred_family != AF_UNSPEC) |
c7699875 | 782 | filter.use = 1; |
783 | filter.xpinfo.sel.family = preferred_family; | |
784 | ||
785 | while (argc > 0) { | |
786 | if (strcmp(*argv, "dir") == 0) { | |
787 | NEXT_ARG(); | |
788 | xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv); | |
789 | ||
790 | filter.dir_mask = XFRM_FILTER_MASK_FULL; | |
791 | ||
c7699875 | 792 | } else if (strcmp(*argv, "index") == 0) { |
793 | NEXT_ARG(); | |
794 | if (get_u32(&filter.xpinfo.index, *argv, 0)) | |
e8740e42 | 795 | invarg("INDEX value is invalid", *argv); |
c7699875 | 796 | |
797 | filter.index_mask = XFRM_FILTER_MASK_FULL; | |
798 | ||
972938e9 MN |
799 | } else if (strcmp(*argv, "ptype") == 0) { |
800 | NEXT_ARG(); | |
801 | xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv); | |
802 | ||
803 | filter.ptype_mask = XFRM_FILTER_MASK_FULL; | |
804 | ||
c7699875 | 805 | } else if (strcmp(*argv, "action") == 0) { |
806 | NEXT_ARG(); | |
807 | if (strcmp(*argv, "allow") == 0) | |
808 | filter.xpinfo.action = XFRM_POLICY_ALLOW; | |
809 | else if (strcmp(*argv, "block") == 0) | |
810 | filter.xpinfo.action = XFRM_POLICY_BLOCK; | |
811 | else | |
e8740e42 | 812 | invarg("ACTION value is invalid\n", *argv); |
c7699875 | 813 | |
814 | filter.action_mask = XFRM_FILTER_MASK_FULL; | |
815 | ||
816 | } else if (strcmp(*argv, "priority") == 0) { | |
817 | NEXT_ARG(); | |
818 | if (get_u32(&filter.xpinfo.priority, *argv, 0)) | |
e8740e42 | 819 | invarg("PRIORITY value is invalid", *argv); |
c7699875 | 820 | |
821 | filter.priority_mask = XFRM_FILTER_MASK_FULL; | |
822 | ||
c1fa2253 MN |
823 | } else if (strcmp(*argv, "flag") == 0) { |
824 | NEXT_ARG(); | |
825 | xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc, | |
826 | &argv); | |
827 | ||
828 | filter.policy_flags_mask = XFRM_FILTER_MASK_FULL; | |
829 | ||
20e4840a TE |
830 | } else if (strcmp(*argv, "nosock") == 0) { |
831 | /* filter all socket-based policies */ | |
832 | filter.filter_socket = 1; | |
7809c616 | 833 | } else { |
834 | if (selp) | |
835 | invarg("unknown", *argv); | |
836 | selp = *argv; | |
837 | ||
838 | xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv); | |
839 | if (preferred_family == AF_UNSPEC) | |
840 | preferred_family = filter.xpinfo.sel.family; | |
841 | ||
842 | } | |
c7699875 | 843 | |
844 | argc--; argv++; | |
845 | } | |
846 | ||
847 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
848 | exit(1); | |
849 | ||
9bec1a43 | 850 | if (deleteall) { |
c7699875 | 851 | struct xfrm_buffer xb; |
9bec1a43 | 852 | char buf[NLMSG_DELETEALL_BUF_SIZE]; |
c7699875 | 853 | int i; |
854 | ||
855 | xb.buf = buf; | |
856 | xb.size = sizeof(buf); | |
857 | xb.rth = &rth; | |
858 | ||
859 | for (i = 0; ; i++) { | |
782cf01d ND |
860 | struct { |
861 | struct nlmsghdr n; | |
862 | char buf[NLMSG_BUF_SIZE]; | |
863 | } req = { | |
864 | .n.nlmsg_len = NLMSG_HDRLEN, | |
865 | .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
866 | .n.nlmsg_type = XFRM_MSG_GETPOLICY, | |
867 | .n.nlmsg_seq = rth.dump = ++rth.seq, | |
868 | }; | |
869 | ||
c7699875 | 870 | xb.offset = 0; |
871 | xb.nlmsg_count = 0; | |
872 | ||
873 | if (show_stats > 1) | |
9bec1a43 | 874 | fprintf(stderr, "Delete-all round = %d\n", i); |
c7699875 | 875 | |
782cf01d | 876 | if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { |
c7699875 | 877 | perror("Cannot send dump request"); |
878 | exit(1); | |
879 | } | |
880 | ||
cd70f3f5 | 881 | if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb) < 0) { |
9bec1a43 | 882 | fprintf(stderr, "Delete-all terminated\n"); |
c7699875 | 883 | exit(1); |
884 | } | |
885 | if (xb.nlmsg_count == 0) { | |
886 | if (show_stats > 1) | |
9bec1a43 | 887 | fprintf(stderr, "Delete-all completed\n"); |
c7699875 | 888 | break; |
889 | } | |
890 | ||
f31a37f7 | 891 | if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) { |
1fb0a998 | 892 | perror("Failed to send delete-all request"); |
c7699875 | 893 | exit(1); |
894 | } | |
895 | if (show_stats > 1) | |
9bec1a43 | 896 | fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count); |
c7699875 | 897 | |
898 | xb.offset = 0; | |
899 | xb.nlmsg_count = 0; | |
900 | } | |
901 | } else { | |
782cf01d ND |
902 | struct { |
903 | struct nlmsghdr n; | |
904 | char buf[NLMSG_BUF_SIZE]; | |
905 | } req = { | |
906 | .n.nlmsg_len = NLMSG_HDRLEN, | |
907 | .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, | |
908 | .n.nlmsg_type = XFRM_MSG_GETPOLICY, | |
909 | .n.nlmsg_seq = rth.dump = ++rth.seq, | |
910 | }; | |
911 | ||
912 | if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { | |
c7699875 | 913 | perror("Cannot send dump request"); |
914 | exit(1); | |
915 | } | |
916 | ||
cd70f3f5 | 917 | if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout) < 0) { |
c7699875 | 918 | fprintf(stderr, "Dump terminated\n"); |
919 | exit(1); | |
920 | } | |
921 | } | |
922 | ||
923 | rtnl_close(&rth); | |
924 | ||
925 | exit(0); | |
926 | } | |
927 | ||
56f5daac | 928 | static int print_spdinfo(struct nlmsghdr *n, void *arg) |
f90c4f4e | 929 | { |
56f5daac | 930 | FILE *fp = (FILE *)arg; |
f90c4f4e | 931 | __u32 *f = NLMSG_DATA(n); |
56f5daac SH |
932 | struct rtattr *tb[XFRMA_SPD_MAX+1]; |
933 | struct rtattr *rta; | |
f90c4f4e | 934 | |
935 | int len = n->nlmsg_len; | |
936 | ||
937 | len -= NLMSG_LENGTH(sizeof(__u32)); | |
938 | if (len < 0) { | |
939 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); | |
940 | return -1; | |
941 | } | |
942 | ||
943 | rta = XFRMSAPD_RTA(f); | |
944 | parse_rtattr(tb, XFRMA_SPD_MAX, rta, len); | |
945 | ||
56f5daac | 946 | fprintf(fp, "\t SPD"); |
bdf9e86d SH |
947 | if (tb[XFRMA_SPD_INFO]) { |
948 | struct xfrmu_spdinfo *si; | |
949 | ||
950 | if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) { | |
f90c4f4e | 951 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); |
952 | return -1; | |
953 | } | |
bdf9e86d | 954 | si = RTA_DATA(tb[XFRMA_SPD_INFO]); |
56f5daac SH |
955 | fprintf(fp, " IN %d", si->incnt); |
956 | fprintf(fp, " OUT %d", si->outcnt); | |
957 | fprintf(fp, " FWD %d", si->fwdcnt); | |
f90c4f4e | 958 | |
959 | if (show_stats) { | |
56f5daac SH |
960 | fprintf(fp, " (Sock:"); |
961 | fprintf(fp, " IN %d", si->inscnt); | |
962 | fprintf(fp, " OUT %d", si->outscnt); | |
963 | fprintf(fp, " FWD %d", si->fwdscnt); | |
964 | fprintf(fp, ")"); | |
f90c4f4e | 965 | } |
966 | ||
025fa9dc | 967 | fprintf(fp, "%s", _SL_); |
f90c4f4e | 968 | } |
969 | if (show_stats > 1) { | |
bdf9e86d SH |
970 | struct xfrmu_spdhinfo *sh; |
971 | ||
972 | if (tb[XFRMA_SPD_HINFO]) { | |
973 | if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) { | |
f90c4f4e | 974 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); |
975 | return -1; | |
976 | } | |
bdf9e86d | 977 | sh = RTA_DATA(tb[XFRMA_SPD_HINFO]); |
56f5daac SH |
978 | fprintf(fp, "\t SPD buckets:"); |
979 | fprintf(fp, " count %d", sh->spdhcnt); | |
980 | fprintf(fp, " Max %d", sh->spdhmcnt); | |
025fa9dc CG |
981 | fprintf(fp, "%s", _SL_); |
982 | } | |
983 | if (tb[XFRMA_SPD_IPV4_HTHRESH]) { | |
984 | struct xfrmu_spdhthresh *th; | |
56f5daac | 985 | |
025fa9dc CG |
986 | if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV4_HTHRESH]) < sizeof(*th)) { |
987 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); | |
988 | return -1; | |
989 | } | |
990 | th = RTA_DATA(tb[XFRMA_SPD_IPV4_HTHRESH]); | |
56f5daac SH |
991 | fprintf(fp, "\t SPD IPv4 thresholds:"); |
992 | fprintf(fp, " local %d", th->lbits); | |
993 | fprintf(fp, " remote %d", th->rbits); | |
025fa9dc CG |
994 | fprintf(fp, "%s", _SL_); |
995 | ||
996 | } | |
997 | if (tb[XFRMA_SPD_IPV6_HTHRESH]) { | |
998 | struct xfrmu_spdhthresh *th; | |
56f5daac | 999 | |
025fa9dc CG |
1000 | if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV6_HTHRESH]) < sizeof(*th)) { |
1001 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); | |
1002 | return -1; | |
1003 | } | |
1004 | th = RTA_DATA(tb[XFRMA_SPD_IPV6_HTHRESH]); | |
56f5daac SH |
1005 | fprintf(fp, "\t SPD IPv6 thresholds:"); |
1006 | fprintf(fp, " local %d", th->lbits); | |
1007 | fprintf(fp, " remote %d", th->rbits); | |
025fa9dc | 1008 | fprintf(fp, "%s", _SL_); |
f90c4f4e | 1009 | } |
1010 | } | |
025fa9dc CG |
1011 | |
1012 | if (oneline) | |
1013 | fprintf(fp, "\n"); | |
f90c4f4e | 1014 | |
56f5daac | 1015 | return 0; |
f90c4f4e | 1016 | } |
1017 | ||
025fa9dc CG |
1018 | static int xfrm_spd_setinfo(int argc, char **argv) |
1019 | { | |
1020 | struct rtnl_handle rth; | |
1021 | struct { | |
1022 | struct nlmsghdr n; | |
1023 | __u32 flags; | |
1024 | char buf[RTA_BUF_SIZE]; | |
d17b136f PS |
1025 | } req = { |
1026 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)), | |
1027 | .n.nlmsg_flags = NLM_F_REQUEST, | |
1028 | .n.nlmsg_type = XFRM_MSG_NEWSPDINFO, | |
1029 | .flags = 0XFFFFFFFF, | |
1030 | }; | |
025fa9dc CG |
1031 | |
1032 | char *thr4 = NULL; | |
1033 | char *thr6 = NULL; | |
1034 | ||
025fa9dc CG |
1035 | while (argc > 0) { |
1036 | if (strcmp(*argv, "hthresh4") == 0) { | |
1037 | struct xfrmu_spdhthresh thr; | |
1038 | ||
1039 | if (thr4) | |
1040 | duparg("hthresh4", *argv); | |
1041 | thr4 = *argv; | |
1042 | NEXT_ARG(); | |
1043 | if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 32) | |
1044 | invarg("hthresh4 LBITS value is invalid", *argv); | |
1045 | NEXT_ARG(); | |
1046 | if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 32) | |
1047 | invarg("hthresh4 RBITS value is invalid", *argv); | |
1048 | ||
1049 | addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV4_HTHRESH, | |
1050 | (void *)&thr, sizeof(thr)); | |
1051 | } else if (strcmp(*argv, "hthresh6") == 0) { | |
1052 | struct xfrmu_spdhthresh thr; | |
1053 | ||
1054 | if (thr6) | |
1055 | duparg("hthresh6", *argv); | |
1056 | thr6 = *argv; | |
1057 | NEXT_ARG(); | |
1058 | if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 128) | |
1059 | invarg("hthresh6 LBITS value is invalid", *argv); | |
1060 | NEXT_ARG(); | |
1061 | if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 128) | |
1062 | invarg("hthresh6 RBITS value is invalid", *argv); | |
1063 | ||
1064 | addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV6_HTHRESH, | |
1065 | (void *)&thr, sizeof(thr)); | |
1066 | } else { | |
1067 | invarg("unknown", *argv); | |
1068 | } | |
1069 | ||
1070 | argc--; argv++; | |
1071 | } | |
1072 | ||
1073 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
1074 | exit(1); | |
1075 | ||
86bf43c7 | 1076 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
025fa9dc CG |
1077 | exit(2); |
1078 | ||
1079 | rtnl_close(&rth); | |
1080 | ||
1081 | return 0; | |
1082 | } | |
1083 | ||
f90c4f4e | 1084 | static int xfrm_spd_getinfo(int argc, char **argv) |
1085 | { | |
1086 | struct rtnl_handle rth; | |
1087 | struct { | |
1088 | struct nlmsghdr n; | |
1089 | __u32 flags; | |
d17b136f PS |
1090 | } req = { |
1091 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)), | |
1092 | .n.nlmsg_flags = NLM_F_REQUEST, | |
1093 | .n.nlmsg_type = XFRM_MSG_GETSPDINFO, | |
1094 | .flags = 0XFFFFFFFF, | |
1095 | }; | |
86bf43c7 | 1096 | struct nlmsghdr *answer; |
f90c4f4e | 1097 | |
1098 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
1099 | exit(1); | |
1100 | ||
86bf43c7 | 1101 | if (rtnl_talk(&rth, &req.n, &answer) < 0) |
f90c4f4e | 1102 | exit(2); |
1103 | ||
86bf43c7 | 1104 | print_spdinfo(answer, (void *)stdout); |
f90c4f4e | 1105 | |
86bf43c7 | 1106 | free(answer); |
f90c4f4e | 1107 | rtnl_close(&rth); |
1108 | ||
1109 | return 0; | |
1110 | } | |
1111 | ||
972938e9 | 1112 | static int xfrm_policy_flush(int argc, char **argv) |
bd641cd6 | 1113 | { |
1114 | struct rtnl_handle rth; | |
1115 | struct { | |
1116 | struct nlmsghdr n; | |
972938e9 | 1117 | char buf[RTA_BUF_SIZE]; |
d17b136f PS |
1118 | } req = { |
1119 | .n.nlmsg_len = NLMSG_LENGTH(0), /* nlmsg data is nothing */ | |
1120 | .n.nlmsg_flags = NLM_F_REQUEST, | |
1121 | .n.nlmsg_type = XFRM_MSG_FLUSHPOLICY, | |
1122 | }; | |
972938e9 | 1123 | char *ptypep = NULL; |
d17b136f | 1124 | struct xfrm_userpolicy_type upt = {}; |
bd641cd6 | 1125 | |
972938e9 MN |
1126 | while (argc > 0) { |
1127 | if (strcmp(*argv, "ptype") == 0) { | |
1128 | if (ptypep) | |
1129 | duparg("ptype", *argv); | |
1130 | ptypep = *argv; | |
1131 | ||
1132 | NEXT_ARG(); | |
1133 | xfrm_policy_ptype_parse(&upt.type, &argc, &argv); | |
972938e9 MN |
1134 | } else |
1135 | invarg("unknown", *argv); | |
1136 | ||
1137 | argc--; argv++; | |
1138 | } | |
1139 | ||
1140 | if (ptypep) { | |
1141 | addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, | |
1142 | (void *)&upt, sizeof(upt)); | |
1143 | } | |
1144 | ||
bd641cd6 | 1145 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
1146 | exit(1); | |
1147 | ||
1148 | if (show_stats > 1) | |
9bec1a43 | 1149 | fprintf(stderr, "Flush policy\n"); |
bd641cd6 | 1150 | |
86bf43c7 | 1151 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
bd641cd6 | 1152 | exit(2); |
1153 | ||
1154 | rtnl_close(&rth); | |
1155 | ||
1156 | return 0; | |
1157 | } | |
1158 | ||
c7699875 | 1159 | int do_xfrm_policy(int argc, char **argv) |
1160 | { | |
1161 | if (argc < 1) | |
9bec1a43 | 1162 | return xfrm_policy_list_or_deleteall(0, NULL, 0); |
c7699875 | 1163 | |
1164 | if (matches(*argv, "add") == 0) | |
1165 | return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0, | |
1166 | argc-1, argv+1); | |
1167 | if (matches(*argv, "update") == 0) | |
1168 | return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0, | |
1169 | argc-1, argv+1); | |
9bec1a43 | 1170 | if (matches(*argv, "delete") == 0) |
c7699875 | 1171 | return xfrm_policy_delete(argc-1, argv+1); |
9bec1a43 SH |
1172 | if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0) |
1173 | return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1); | |
c7699875 | 1174 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
1175 | || matches(*argv, "lst") == 0) | |
9bec1a43 | 1176 | return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0); |
c7699875 | 1177 | if (matches(*argv, "get") == 0) |
1178 | return xfrm_policy_get(argc-1, argv+1); | |
9bec1a43 | 1179 | if (matches(*argv, "flush") == 0) |
972938e9 | 1180 | return xfrm_policy_flush(argc-1, argv+1); |
f90c4f4e | 1181 | if (matches(*argv, "count") == 0) |
1182 | return xfrm_spd_getinfo(argc, argv); | |
025fa9dc CG |
1183 | if (matches(*argv, "set") == 0) |
1184 | return xfrm_spd_setinfo(argc-1, argv+1); | |
c7699875 | 1185 | if (matches(*argv, "help") == 0) |
1186 | usage(); | |
1187 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv); | |
1188 | exit(-1); | |
1189 | } |