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