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