]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/xfrm_policy.c
ipnetns: use-after-free problem in get_netnsid_from_name func
[mirror_iproute2.git] / ip / xfrm_policy.c
CommitLineData
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
51static void usage(void) __attribute__((noreturn));
52
53static 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");
aed63ae1 58 fprintf(stderr, " [ if_id IF_ID ] [ LIMIT-LIST ] [ TMPL-LIST ]\n");
cbec0219
DW
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
104static 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
124static 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
142static 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 177static 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
227int 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 244static 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 = {};
aed63ae1
EB
273 bool is_if_id_set = false;
274 __u32 if_id = 0;
c7699875 275
276 while (argc > 0) {
277 if (strcmp(*argv, "dir") == 0) {
278 if (dirp)
279 duparg("dir", *argv);
280 dirp = *argv;
281
282 NEXT_ARG();
283 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
e4f054f0
JL
284 } else if (strcmp(*argv, "ctx") == 0) {
285 char *context;
286
287 if (sctxp)
288 duparg("ctx", *argv);
289 sctxp = *argv;
290 NEXT_ARG();
291 context = *argv;
292 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
ee675e87
JHS
293 } else if (strcmp(*argv, "mark") == 0) {
294 xfrm_parse_mark(&mark, &argc, &argv);
c7699875 295 } else if (strcmp(*argv, "index") == 0) {
296 NEXT_ARG();
297 if (get_u32(&req.xpinfo.index, *argv, 0))
e8740e42 298 invarg("INDEX value is invalid", *argv);
972938e9
MN
299 } else if (strcmp(*argv, "ptype") == 0) {
300 if (ptypep)
301 duparg("ptype", *argv);
302 ptypep = *argv;
303
304 NEXT_ARG();
305 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
c7699875 306 } else if (strcmp(*argv, "action") == 0) {
307 NEXT_ARG();
308 if (strcmp(*argv, "allow") == 0)
309 req.xpinfo.action = XFRM_POLICY_ALLOW;
310 else if (strcmp(*argv, "block") == 0)
311 req.xpinfo.action = XFRM_POLICY_BLOCK;
312 else
e8740e42 313 invarg("ACTION value is invalid\n", *argv);
c7699875 314 } else if (strcmp(*argv, "priority") == 0) {
315 NEXT_ARG();
316 if (get_u32(&req.xpinfo.priority, *argv, 0))
e8740e42 317 invarg("PRIORITY value is invalid", *argv);
c1fa2253
MN
318 } else if (strcmp(*argv, "flag") == 0) {
319 NEXT_ARG();
320 xfrm_policy_flag_parse(&req.xpinfo.flags, &argc,
321 &argv);
c7699875 322 } else if (strcmp(*argv, "limit") == 0) {
323 NEXT_ARG();
324 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
325 } else if (strcmp(*argv, "tmpl") == 0) {
326 struct xfrm_user_tmpl *tmpl;
327
328 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
329 fprintf(stderr, "Too many tmpls: buffer overflow\n");
330 exit(1);
331 }
332 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
333
334 tmpl->family = preferred_family;
335 tmpl->aalgos = (~(__u32)0);
336 tmpl->ealgos = (~(__u32)0);
337 tmpl->calgos = (~(__u32)0);
338
339 NEXT_ARG();
340 xfrm_tmpl_parse(tmpl, &argc, &argv);
341
342 tmpls_len += sizeof(*tmpl);
aed63ae1
EB
343 } else if (strcmp(*argv, "if_id") == 0) {
344 NEXT_ARG();
345 if (get_u32(&if_id, *argv, 0))
346 invarg("IF_ID value is invalid", *argv);
347 is_if_id_set = true;
7809c616 348 } else {
349 if (selp)
350 duparg("unknown", *argv);
351 selp = *argv;
352
353 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
354 if (preferred_family == AF_UNSPEC)
355 preferred_family = req.xpinfo.sel.family;
356 }
c7699875 357
358 argc--; argv++;
359 }
360
361 if (!dirp) {
e8740e42 362 fprintf(stderr, "Not enough information: DIR is required.\n");
c7699875 363 exit(1);
364 }
365
972938e9
MN
366 if (ptypep) {
367 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
368 (void *)&upt, sizeof(upt));
369 }
370
c7699875 371 if (tmpls_len > 0) {
372 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
373 (void *)tmpls_buf, tmpls_len);
374 }
375
b5574165 376 if (mark.m) {
ee675e87
JHS
377 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
378 (void *)&mark, sizeof(mark));
379 if (r < 0) {
56f5daac 380 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
ee675e87
JHS
381 exit(1);
382 }
383 }
384
e4f054f0
JL
385 if (sctxp) {
386 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
387 (void *)&ctx, ctx.sctx.len);
388 }
ee675e87 389
aed63ae1
EB
390 if (is_if_id_set)
391 addattr32(&req.n, sizeof(req.buf), XFRMA_IF_ID, if_id);
392
c7699875 393 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
394 exit(1);
395
396 if (req.xpinfo.sel.family == AF_UNSPEC)
397 req.xpinfo.sel.family = AF_INET;
398
86bf43c7 399 if (rtnl_talk(&rth, &req.n, NULL) < 0)
c7699875 400 exit(2);
401
402 rtnl_close(&rth);
403
404 return 0;
405}
406
972938e9
MN
407static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo,
408 __u8 ptype)
c7699875 409{
410 if (!filter.use)
411 return 1;
412
413 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
414 return 0;
415
20e4840a
TE
416 if (filter.filter_socket && (xpinfo->dir >= XFRM_POLICY_MAX))
417 return 0;
418
972938e9
MN
419 if ((ptype^filter.ptype)&filter.ptype_mask)
420 return 0;
421
c7699875 422 if (filter.sel_src_mask) {
eaa34ee3 423 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
424 filter.sel_src_mask))
c7699875 425 return 0;
426 }
427
428 if (filter.sel_dst_mask) {
eaa34ee3 429 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
430 filter.sel_dst_mask))
c7699875 431 return 0;
432 }
433
434 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
435 return 0;
436
437 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
438 return 0;
439
440 if (filter.upspec_sport_mask) {
441 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
442 return 0;
443 }
444
445 if (filter.upspec_dport_mask) {
446 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
447 return 0;
448 }
449
450 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
451 return 0;
452
453 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
454 return 0;
455
456 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
457 return 0;
458
c1fa2253
MN
459 if (filter.policy_flags_mask)
460 if ((xpinfo->flags & filter.xpinfo.flags) == 0)
461 return 0;
462
c7699875 463 return 1;
464}
465
cd554f2c 466int xfrm_policy_print(struct nlmsghdr *n, void *arg)
c7699875 467{
56f5daac
SH
468 struct rtattr *tb[XFRMA_MAX+1];
469 struct rtattr *rta;
c595c790
SH
470 struct xfrm_userpolicy_info *xpinfo = NULL;
471 struct xfrm_user_polexpire *xpexp = NULL;
472 struct xfrm_userpolicy_id *xpid = NULL;
972938e9 473 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
56f5daac 474 FILE *fp = (FILE *)arg;
c595c790 475 int len = n->nlmsg_len;
c7699875 476
477 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
90f93024 478 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
669ae748 479 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
90f93024 480 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
c7699875 481 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
482 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
483 return 0;
484 }
485
669ae748
SH
486 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
487 xpid = NLMSG_DATA(n);
af1b6a41 488 len -= NLMSG_SPACE(sizeof(*xpid));
669ae748 489 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
90f93024
SH
490 xpexp = NLMSG_DATA(n);
491 xpinfo = &xpexp->pol;
af1b6a41 492 len -= NLMSG_SPACE(sizeof(*xpexp));
90f93024
SH
493 } else {
494 xpexp = NULL;
495 xpinfo = NLMSG_DATA(n);
af1b6a41 496 len -= NLMSG_SPACE(sizeof(*xpinfo));
90f93024
SH
497 }
498
c7699875 499 if (len < 0) {
500 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
501 return -1;
502 }
503
669ae748
SH
504 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
505 rta = XFRMPID_RTA(xpid);
506 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
90f93024
SH
507 rta = XFRMPEXP_RTA(xpexp);
508 else
509 rta = XFRMP_RTA(xpinfo);
510
511 parse_rtattr(tb, XFRMA_MAX, rta, len);
c7699875 512
972938e9
MN
513 if (tb[XFRMA_POLICY_TYPE]) {
514 struct xfrm_userpolicy_type *upt;
515
516 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
517 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
518 return -1;
519 }
9f1370c0 520 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
972938e9
MN
521 ptype = upt->type;
522 }
523
524 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype))
525 return 0;
526
527 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
528 fprintf(fp, "Deleted ");
529 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
530 fprintf(fp, "Updated ");
531 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
532 fprintf(fp, "Expired ");
533
c595c790 534 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
56f5daac 535 /* xfrm_policy_id_print(); */
669ae748
SH
536 if (!tb[XFRMA_POLICY]) {
537 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
538 return -1;
c595c790 539 }
669ae748
SH
540 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
541 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
542 return -1;
543 }
9f1370c0 544 xpinfo = RTA_DATA(tb[XFRMA_POLICY]);
669ae748 545 }
c7699875 546
56e8ad38 547 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
c7699875 548
90f93024
SH
549 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
550 fprintf(fp, "\t");
551 fprintf(fp, "hard %u", xpexp->hard);
552 fprintf(fp, "%s", _SL_);
553 }
554
7809c616 555 if (oneline)
556 fprintf(fp, "\n");
669ae748 557 fflush(fp);
7809c616 558
c7699875 559 return 0;
560}
561
562static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
86bf43c7 563 struct nlmsghdr **answer)
c7699875 564{
565 struct rtnl_handle rth;
566 struct {
567 struct nlmsghdr n;
568 struct xfrm_userpolicy_id xpid;
972938e9 569 char buf[RTA_BUF_SIZE];
d17b136f
PS
570 } req = {
571 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid)),
572 .n.nlmsg_flags = NLM_F_REQUEST,
573 .n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY
574 : XFRM_MSG_GETPOLICY,
575 };
c7699875 576 char *dirp = NULL;
577 char *selp = NULL;
578 char *indexp = NULL;
972938e9 579 char *ptypep = NULL;
e4f054f0 580 char *sctxp = NULL;
d17b136f 581 struct xfrm_userpolicy_type upt = {};
ee675e87 582 struct xfrm_mark mark = {0, 0};
e4f054f0
JL
583 struct {
584 struct xfrm_user_sec_ctx sctx;
585 char str[CTX_BUF_SIZE];
d17b136f 586 } ctx = {};
c7699875 587
588 while (argc > 0) {
589 if (strcmp(*argv, "dir") == 0) {
590 if (dirp)
591 duparg("dir", *argv);
592 dirp = *argv;
593
594 NEXT_ARG();
595 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
596
e4f054f0
JL
597 } else if (strcmp(*argv, "ctx") == 0) {
598 char *context;
599
600 if (sctxp)
601 duparg("ctx", *argv);
602 sctxp = *argv;
603 NEXT_ARG();
604 context = *argv;
605 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
ee675e87
JHS
606 } else if (strcmp(*argv, "mark") == 0) {
607 xfrm_parse_mark(&mark, &argc, &argv);
c7699875 608 } else if (strcmp(*argv, "index") == 0) {
609 if (indexp)
610 duparg("index", *argv);
611 indexp = *argv;
612
613 NEXT_ARG();
614 if (get_u32(&req.xpid.index, *argv, 0))
e8740e42 615 invarg("INDEX value is invalid", *argv);
c7699875 616
972938e9
MN
617 } else if (strcmp(*argv, "ptype") == 0) {
618 if (ptypep)
619 duparg("ptype", *argv);
620 ptypep = *argv;
621
622 NEXT_ARG();
623 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
624
7809c616 625 } else {
626 if (selp)
627 invarg("unknown", *argv);
628 selp = *argv;
629
630 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
631 if (preferred_family == AF_UNSPEC)
632 preferred_family = req.xpid.sel.family;
633
634 }
c7699875 635
636 argc--; argv++;
637 }
638
639 if (!dirp) {
e8740e42 640 fprintf(stderr, "Not enough information: DIR is required.\n");
c7699875 641 exit(1);
642 }
972938e9
MN
643 if (ptypep) {
644 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
645 (void *)&upt, sizeof(upt));
646 }
c7699875 647 if (!selp && !indexp) {
e8740e42 648 fprintf(stderr, "Not enough information: either SELECTOR or INDEX is required.\n");
c7699875 649 exit(1);
650 }
651 if (selp && indexp)
652 duparg2("SELECTOR", "INDEX");
653
654 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
655 exit(1);
656
657 if (req.xpid.sel.family == AF_UNSPEC)
658 req.xpid.sel.family = AF_INET;
659
ee675e87
JHS
660 if (mark.m & mark.v) {
661 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
662 (void *)&mark, sizeof(mark));
663 if (r < 0) {
56f5daac 664 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
ee675e87
JHS
665 exit(1);
666 }
667 }
668
e4f054f0
JL
669 if (sctxp) {
670 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
671 (void *)&ctx, ctx.sctx.len);
672 }
673
86bf43c7 674 if (rtnl_talk(&rth, &req.n, answer) < 0)
c7699875 675 exit(2);
676
677 rtnl_close(&rth);
678
679 return 0;
680}
681
682static int xfrm_policy_delete(int argc, char **argv)
683{
86bf43c7 684 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
c7699875 685}
686
687static int xfrm_policy_get(int argc, char **argv)
688{
86bf43c7 689 struct nlmsghdr *n = NULL;
c7699875 690
86bf43c7 691 xfrm_policy_get_or_delete(argc, argv, 0, &n);
c7699875 692
cd554f2c 693 if (xfrm_policy_print(n, (void *)stdout) < 0) {
c7699875 694 fprintf(stderr, "An error :-)\n");
695 exit(1);
696 }
697
86bf43c7 698 free(n);
c7699875 699 return 0;
700}
701
702/*
703 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
704 * and store it to buffer.
705 */
cd554f2c 706static int xfrm_policy_keep(struct nlmsghdr *n, void *arg)
c7699875 707{
708 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
709 struct rtnl_handle *rth = xb->rth;
710 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
711 int len = n->nlmsg_len;
972938e9
MN
712 struct rtattr *tb[XFRMA_MAX+1];
713 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
c7699875 714 struct nlmsghdr *new_n;
715 struct xfrm_userpolicy_id *xpid;
716
717 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
718 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
719 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
720 return 0;
721 }
722
723 len -= NLMSG_LENGTH(sizeof(*xpinfo));
724 if (len < 0) {
725 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
726 return -1;
727 }
728
972938e9
MN
729 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len);
730
731 if (tb[XFRMA_POLICY_TYPE]) {
732 struct xfrm_userpolicy_type *upt;
733
734 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
735 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
736 return -1;
737 }
9f1370c0 738 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
972938e9
MN
739 ptype = upt->type;
740 }
741
742 if (!xfrm_policy_filter_match(xpinfo, ptype))
c7699875 743 return 0;
744
5474d440
TE
745 /* can't delete socket policies */
746 if (xpinfo->dir >= XFRM_POLICY_MAX)
747 return 0;
748
d5eb0564
AH
749 if (xb->offset + NLMSG_LENGTH(sizeof(*xpid)) > xb->size)
750 return 0;
c7699875 751
752 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
753 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
754 new_n->nlmsg_flags = NLM_F_REQUEST;
755 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
756 new_n->nlmsg_seq = ++rth->seq;
757
758 xpid = NLMSG_DATA(new_n);
759 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
760 xpid->dir = xpinfo->dir;
761 xpid->index = xpinfo->index;
762
0c7d651b
TE
763 if (tb[XFRMA_MARK]) {
764 int r = addattr_l(new_n, xb->size, XFRMA_MARK,
765 (void *)RTA_DATA(tb[XFRMA_MARK]), tb[XFRMA_MARK]->rta_len);
766 if (r < 0) {
767 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
768 exit(1);
769 }
770 }
771
c7699875 772 xb->offset += new_n->nlmsg_len;
56f5daac 773 xb->nlmsg_count++;
c7699875 774
775 return 0;
776}
777
9bec1a43 778static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall)
c7699875 779{
7809c616 780 char *selp = NULL;
c7699875 781 struct rtnl_handle rth;
782
783 if (argc > 0)
784 filter.use = 1;
785 filter.xpinfo.sel.family = preferred_family;
786
787 while (argc > 0) {
788 if (strcmp(*argv, "dir") == 0) {
789 NEXT_ARG();
790 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
791
792 filter.dir_mask = XFRM_FILTER_MASK_FULL;
793
c7699875 794 } else if (strcmp(*argv, "index") == 0) {
795 NEXT_ARG();
796 if (get_u32(&filter.xpinfo.index, *argv, 0))
e8740e42 797 invarg("INDEX value is invalid", *argv);
c7699875 798
799 filter.index_mask = XFRM_FILTER_MASK_FULL;
800
972938e9
MN
801 } else if (strcmp(*argv, "ptype") == 0) {
802 NEXT_ARG();
803 xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv);
804
805 filter.ptype_mask = XFRM_FILTER_MASK_FULL;
806
c7699875 807 } else if (strcmp(*argv, "action") == 0) {
808 NEXT_ARG();
809 if (strcmp(*argv, "allow") == 0)
810 filter.xpinfo.action = XFRM_POLICY_ALLOW;
811 else if (strcmp(*argv, "block") == 0)
812 filter.xpinfo.action = XFRM_POLICY_BLOCK;
813 else
e8740e42 814 invarg("ACTION value is invalid\n", *argv);
c7699875 815
816 filter.action_mask = XFRM_FILTER_MASK_FULL;
817
818 } else if (strcmp(*argv, "priority") == 0) {
819 NEXT_ARG();
820 if (get_u32(&filter.xpinfo.priority, *argv, 0))
e8740e42 821 invarg("PRIORITY value is invalid", *argv);
c7699875 822
823 filter.priority_mask = XFRM_FILTER_MASK_FULL;
824
c1fa2253
MN
825 } else if (strcmp(*argv, "flag") == 0) {
826 NEXT_ARG();
827 xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc,
828 &argv);
829
830 filter.policy_flags_mask = XFRM_FILTER_MASK_FULL;
831
20e4840a
TE
832 } else if (strcmp(*argv, "nosock") == 0) {
833 /* filter all socket-based policies */
834 filter.filter_socket = 1;
7809c616 835 } else {
836 if (selp)
837 invarg("unknown", *argv);
838 selp = *argv;
839
840 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
841 if (preferred_family == AF_UNSPEC)
842 preferred_family = filter.xpinfo.sel.family;
843
844 }
c7699875 845
846 argc--; argv++;
847 }
848
849 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
850 exit(1);
851
9bec1a43 852 if (deleteall) {
c7699875 853 struct xfrm_buffer xb;
9bec1a43 854 char buf[NLMSG_DELETEALL_BUF_SIZE];
c7699875 855 int i;
856
857 xb.buf = buf;
858 xb.size = sizeof(buf);
859 xb.rth = &rth;
860
861 for (i = 0; ; i++) {
782cf01d
ND
862 struct {
863 struct nlmsghdr n;
864 char buf[NLMSG_BUF_SIZE];
865 } req = {
866 .n.nlmsg_len = NLMSG_HDRLEN,
867 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
868 .n.nlmsg_type = XFRM_MSG_GETPOLICY,
869 .n.nlmsg_seq = rth.dump = ++rth.seq,
870 };
871
c7699875 872 xb.offset = 0;
873 xb.nlmsg_count = 0;
874
875 if (show_stats > 1)
9bec1a43 876 fprintf(stderr, "Delete-all round = %d\n", i);
c7699875 877
782cf01d 878 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) {
c7699875 879 perror("Cannot send dump request");
880 exit(1);
881 }
882
cd70f3f5 883 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb) < 0) {
9bec1a43 884 fprintf(stderr, "Delete-all terminated\n");
c7699875 885 exit(1);
886 }
887 if (xb.nlmsg_count == 0) {
888 if (show_stats > 1)
9bec1a43 889 fprintf(stderr, "Delete-all completed\n");
c7699875 890 break;
891 }
892
f31a37f7 893 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
1fb0a998 894 perror("Failed to send delete-all request");
c7699875 895 exit(1);
896 }
897 if (show_stats > 1)
9bec1a43 898 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
c7699875 899
900 xb.offset = 0;
901 xb.nlmsg_count = 0;
902 }
903 } else {
782cf01d
ND
904 struct {
905 struct nlmsghdr n;
906 char buf[NLMSG_BUF_SIZE];
907 } req = {
908 .n.nlmsg_len = NLMSG_HDRLEN,
909 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
910 .n.nlmsg_type = XFRM_MSG_GETPOLICY,
911 .n.nlmsg_seq = rth.dump = ++rth.seq,
912 };
913
914 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) {
c7699875 915 perror("Cannot send dump request");
916 exit(1);
917 }
918
cd70f3f5 919 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout) < 0) {
c7699875 920 fprintf(stderr, "Dump terminated\n");
921 exit(1);
922 }
923 }
924
925 rtnl_close(&rth);
926
927 exit(0);
928}
929
56f5daac 930static int print_spdinfo(struct nlmsghdr *n, void *arg)
f90c4f4e 931{
56f5daac 932 FILE *fp = (FILE *)arg;
f90c4f4e 933 __u32 *f = NLMSG_DATA(n);
56f5daac
SH
934 struct rtattr *tb[XFRMA_SPD_MAX+1];
935 struct rtattr *rta;
f90c4f4e 936
937 int len = n->nlmsg_len;
938
939 len -= NLMSG_LENGTH(sizeof(__u32));
940 if (len < 0) {
941 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
942 return -1;
943 }
944
945 rta = XFRMSAPD_RTA(f);
946 parse_rtattr(tb, XFRMA_SPD_MAX, rta, len);
947
56f5daac 948 fprintf(fp, "\t SPD");
bdf9e86d
SH
949 if (tb[XFRMA_SPD_INFO]) {
950 struct xfrmu_spdinfo *si;
951
952 if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) {
f90c4f4e 953 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
954 return -1;
955 }
bdf9e86d 956 si = RTA_DATA(tb[XFRMA_SPD_INFO]);
56f5daac
SH
957 fprintf(fp, " IN %d", si->incnt);
958 fprintf(fp, " OUT %d", si->outcnt);
959 fprintf(fp, " FWD %d", si->fwdcnt);
f90c4f4e 960
961 if (show_stats) {
56f5daac
SH
962 fprintf(fp, " (Sock:");
963 fprintf(fp, " IN %d", si->inscnt);
964 fprintf(fp, " OUT %d", si->outscnt);
965 fprintf(fp, " FWD %d", si->fwdscnt);
966 fprintf(fp, ")");
f90c4f4e 967 }
968
025fa9dc 969 fprintf(fp, "%s", _SL_);
f90c4f4e 970 }
971 if (show_stats > 1) {
bdf9e86d
SH
972 struct xfrmu_spdhinfo *sh;
973
974 if (tb[XFRMA_SPD_HINFO]) {
975 if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) {
f90c4f4e 976 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
977 return -1;
978 }
bdf9e86d 979 sh = RTA_DATA(tb[XFRMA_SPD_HINFO]);
56f5daac
SH
980 fprintf(fp, "\t SPD buckets:");
981 fprintf(fp, " count %d", sh->spdhcnt);
982 fprintf(fp, " Max %d", sh->spdhmcnt);
025fa9dc
CG
983 fprintf(fp, "%s", _SL_);
984 }
985 if (tb[XFRMA_SPD_IPV4_HTHRESH]) {
986 struct xfrmu_spdhthresh *th;
56f5daac 987
025fa9dc
CG
988 if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV4_HTHRESH]) < sizeof(*th)) {
989 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
990 return -1;
991 }
992 th = RTA_DATA(tb[XFRMA_SPD_IPV4_HTHRESH]);
56f5daac
SH
993 fprintf(fp, "\t SPD IPv4 thresholds:");
994 fprintf(fp, " local %d", th->lbits);
995 fprintf(fp, " remote %d", th->rbits);
025fa9dc
CG
996 fprintf(fp, "%s", _SL_);
997
998 }
999 if (tb[XFRMA_SPD_IPV6_HTHRESH]) {
1000 struct xfrmu_spdhthresh *th;
56f5daac 1001
025fa9dc
CG
1002 if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV6_HTHRESH]) < sizeof(*th)) {
1003 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
1004 return -1;
1005 }
1006 th = RTA_DATA(tb[XFRMA_SPD_IPV6_HTHRESH]);
56f5daac
SH
1007 fprintf(fp, "\t SPD IPv6 thresholds:");
1008 fprintf(fp, " local %d", th->lbits);
1009 fprintf(fp, " remote %d", th->rbits);
025fa9dc 1010 fprintf(fp, "%s", _SL_);
f90c4f4e 1011 }
1012 }
025fa9dc
CG
1013
1014 if (oneline)
1015 fprintf(fp, "\n");
f90c4f4e 1016
56f5daac 1017 return 0;
f90c4f4e 1018}
1019
025fa9dc
CG
1020static int xfrm_spd_setinfo(int argc, char **argv)
1021{
1022 struct rtnl_handle rth;
1023 struct {
1024 struct nlmsghdr n;
1025 __u32 flags;
1026 char buf[RTA_BUF_SIZE];
d17b136f
PS
1027 } req = {
1028 .n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)),
1029 .n.nlmsg_flags = NLM_F_REQUEST,
1030 .n.nlmsg_type = XFRM_MSG_NEWSPDINFO,
1031 .flags = 0XFFFFFFFF,
1032 };
025fa9dc
CG
1033
1034 char *thr4 = NULL;
1035 char *thr6 = NULL;
1036
025fa9dc
CG
1037 while (argc > 0) {
1038 if (strcmp(*argv, "hthresh4") == 0) {
1039 struct xfrmu_spdhthresh thr;
1040
1041 if (thr4)
1042 duparg("hthresh4", *argv);
1043 thr4 = *argv;
1044 NEXT_ARG();
1045 if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 32)
1046 invarg("hthresh4 LBITS value is invalid", *argv);
1047 NEXT_ARG();
1048 if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 32)
1049 invarg("hthresh4 RBITS value is invalid", *argv);
1050
1051 addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV4_HTHRESH,
1052 (void *)&thr, sizeof(thr));
1053 } else if (strcmp(*argv, "hthresh6") == 0) {
1054 struct xfrmu_spdhthresh thr;
1055
1056 if (thr6)
1057 duparg("hthresh6", *argv);
1058 thr6 = *argv;
1059 NEXT_ARG();
1060 if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 128)
1061 invarg("hthresh6 LBITS value is invalid", *argv);
1062 NEXT_ARG();
1063 if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 128)
1064 invarg("hthresh6 RBITS value is invalid", *argv);
1065
1066 addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV6_HTHRESH,
1067 (void *)&thr, sizeof(thr));
1068 } else {
1069 invarg("unknown", *argv);
1070 }
1071
1072 argc--; argv++;
1073 }
1074
1075 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1076 exit(1);
1077
86bf43c7 1078 if (rtnl_talk(&rth, &req.n, NULL) < 0)
025fa9dc
CG
1079 exit(2);
1080
1081 rtnl_close(&rth);
1082
1083 return 0;
1084}
1085
f90c4f4e 1086static int xfrm_spd_getinfo(int argc, char **argv)
1087{
1088 struct rtnl_handle rth;
1089 struct {
1090 struct nlmsghdr n;
1091 __u32 flags;
d17b136f
PS
1092 } req = {
1093 .n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)),
1094 .n.nlmsg_flags = NLM_F_REQUEST,
1095 .n.nlmsg_type = XFRM_MSG_GETSPDINFO,
1096 .flags = 0XFFFFFFFF,
1097 };
86bf43c7 1098 struct nlmsghdr *answer;
f90c4f4e 1099
1100 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1101 exit(1);
1102
86bf43c7 1103 if (rtnl_talk(&rth, &req.n, &answer) < 0)
f90c4f4e 1104 exit(2);
1105
86bf43c7 1106 print_spdinfo(answer, (void *)stdout);
f90c4f4e 1107
86bf43c7 1108 free(answer);
f90c4f4e 1109 rtnl_close(&rth);
1110
1111 return 0;
1112}
1113
972938e9 1114static int xfrm_policy_flush(int argc, char **argv)
bd641cd6 1115{
1116 struct rtnl_handle rth;
1117 struct {
1118 struct nlmsghdr n;
972938e9 1119 char buf[RTA_BUF_SIZE];
d17b136f
PS
1120 } req = {
1121 .n.nlmsg_len = NLMSG_LENGTH(0), /* nlmsg data is nothing */
1122 .n.nlmsg_flags = NLM_F_REQUEST,
1123 .n.nlmsg_type = XFRM_MSG_FLUSHPOLICY,
1124 };
972938e9 1125 char *ptypep = NULL;
d17b136f 1126 struct xfrm_userpolicy_type upt = {};
bd641cd6 1127
972938e9
MN
1128 while (argc > 0) {
1129 if (strcmp(*argv, "ptype") == 0) {
1130 if (ptypep)
1131 duparg("ptype", *argv);
1132 ptypep = *argv;
1133
1134 NEXT_ARG();
1135 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
972938e9
MN
1136 } else
1137 invarg("unknown", *argv);
1138
1139 argc--; argv++;
1140 }
1141
1142 if (ptypep) {
1143 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
1144 (void *)&upt, sizeof(upt));
1145 }
1146
bd641cd6 1147 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1148 exit(1);
1149
1150 if (show_stats > 1)
9bec1a43 1151 fprintf(stderr, "Flush policy\n");
bd641cd6 1152
86bf43c7 1153 if (rtnl_talk(&rth, &req.n, NULL) < 0)
bd641cd6 1154 exit(2);
1155
1156 rtnl_close(&rth);
1157
1158 return 0;
1159}
1160
c7699875 1161int do_xfrm_policy(int argc, char **argv)
1162{
1163 if (argc < 1)
9bec1a43 1164 return xfrm_policy_list_or_deleteall(0, NULL, 0);
c7699875 1165
1166 if (matches(*argv, "add") == 0)
1167 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
1168 argc-1, argv+1);
1169 if (matches(*argv, "update") == 0)
1170 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
1171 argc-1, argv+1);
9bec1a43 1172 if (matches(*argv, "delete") == 0)
c7699875 1173 return xfrm_policy_delete(argc-1, argv+1);
9bec1a43
SH
1174 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1175 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1);
c7699875 1176 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1177 || matches(*argv, "lst") == 0)
9bec1a43 1178 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0);
c7699875 1179 if (matches(*argv, "get") == 0)
1180 return xfrm_policy_get(argc-1, argv+1);
9bec1a43 1181 if (matches(*argv, "flush") == 0)
972938e9 1182 return xfrm_policy_flush(argc-1, argv+1);
f90c4f4e 1183 if (matches(*argv, "count") == 0)
1184 return xfrm_spd_getinfo(argc, argv);
025fa9dc
CG
1185 if (matches(*argv, "set") == 0)
1186 return xfrm_spd_setinfo(argc-1, argv+1);
c7699875 1187 if (matches(*argv, "help") == 0)
1188 usage();
1189 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);
1190 exit(-1);
1191}