]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/xfrm_policy.c
bpf: replace snprintf with asprintf when dealing with long buffers
[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{
8589eb4e
MC
55 fprintf(stderr,
56 "Usage: ip xfrm policy { add | update } SELECTOR dir DIR [ ctx CTX ]\n"
57 " [ mark MARK [ mask MASK ] ] [ index INDEX ] [ ptype PTYPE ]\n"
58 " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n"
59 " [ if_id IF_ID ] [ LIMIT-LIST ] [ TMPL-LIST ]\n"
60 "Usage: ip xfrm policy { delete | get } { SELECTOR | index INDEX } dir DIR\n"
61 " [ ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]\n"
62 "Usage: ip xfrm policy { deleteall | list } [ nosock ] [ SELECTOR ] [ dir DIR ]\n"
63 " [ index INDEX ] [ ptype PTYPE ] [ action ACTION ] [ priority PRIORITY ]\n"
64 " [ flag FLAG-LIST ]\n"
65 "Usage: ip xfrm policy flush [ ptype PTYPE ]\n"
66 "Usage: ip xfrm policy count\n"
67 "Usage: ip xfrm policy set [ hthresh4 LBITS RBITS ] [ hthresh6 LBITS RBITS ]\n"
68 "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n"
69 "UPSPEC := proto { { ");
70 fprintf(stderr, "%s | %s | %s | %s } ",
71 strxf_proto(IPPROTO_TCP),
72 strxf_proto(IPPROTO_UDP),
73 strxf_proto(IPPROTO_SCTP),
74 strxf_proto(IPPROTO_DCCP));
75 fprintf(stderr,
76 "[ sport PORT ] [ dport PORT ] |\n"
77 " { %s | %s | %s } ",
78 strxf_proto(IPPROTO_ICMP),
79 strxf_proto(IPPROTO_ICMPV6),
80 strxf_proto(IPPROTO_MH));
81 fprintf(stderr,
82 "[ type NUMBER ] [ code NUMBER ] |\n"
83 " %s",
84 strxf_proto(IPPROTO_GRE));
85 fprintf(stderr,
86 " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n"
87 "DIR := in | out | fwd\n"
88 "PTYPE := main | sub\n"
89 "ACTION := allow | block\n"
90 "FLAG-LIST := [ FLAG-LIST ] FLAG\n"
91 "FLAG := localok | icmp\n"
92 "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n"
93 "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n"
94 " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n"
95 "TMPL-LIST := [ TMPL-LIST ] tmpl TMPL\n"
96 "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n"
97 "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n"
98 "XFRM-PROTO := ");
99 fprintf(stderr,
100 "%s | %s | %s | %s | %s\n",
101 strxf_xfrmproto(IPPROTO_ESP),
102 strxf_xfrmproto(IPPROTO_AH),
103 strxf_xfrmproto(IPPROTO_COMP),
104 strxf_xfrmproto(IPPROTO_ROUTING),
105 strxf_xfrmproto(IPPROTO_DSTOPTS));
106 fprintf(stderr,
107 "MODE := transport | tunnel | beet | ro | in_trigger\n"
108 "LEVEL := required | use\n");
c7699875 109
110 exit(-1);
111}
112
113static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
114{
115 int argc = *argcp;
116 char **argv = *argvp;
117
118 if (strcmp(*argv, "in") == 0)
119 *dir = XFRM_POLICY_IN;
120 else if (strcmp(*argv, "out") == 0)
121 *dir = XFRM_POLICY_OUT;
122 else if (strcmp(*argv, "fwd") == 0)
123 *dir = XFRM_POLICY_FWD;
124 else
e8740e42 125 invarg("DIR value is invalid", *argv);
c7699875 126
127 *argcp = argc;
128 *argvp = argv;
129
130 return 0;
131}
132
972938e9
MN
133static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp)
134{
135 int argc = *argcp;
136 char **argv = *argvp;
137
138 if (strcmp(*argv, "main") == 0)
139 *ptype = XFRM_POLICY_TYPE_MAIN;
140 else if (strcmp(*argv, "sub") == 0)
141 *ptype = XFRM_POLICY_TYPE_SUB;
142 else
e8740e42 143 invarg("PTYPE value is invalid", *argv);
972938e9
MN
144
145 *argcp = argc;
146 *argvp = argv;
147
148 return 0;
149}
150
c1fa2253
MN
151static int xfrm_policy_flag_parse(__u8 *flags, int *argcp, char ***argvp)
152{
153 int argc = *argcp;
154 char **argv = *argvp;
155 int len = strlen(*argv);
156
157 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
158 __u8 val = 0;
159
160 if (get_u8(&val, *argv, 16))
e8740e42 161 invarg("FLAG value is invalid", *argv);
c1fa2253
MN
162 *flags = val;
163 } else {
164 while (1) {
165 if (strcmp(*argv, "localok") == 0)
166 *flags |= XFRM_POLICY_LOCALOK;
c0635644
UW
167 else if (strcmp(*argv, "icmp") == 0)
168 *flags |= XFRM_POLICY_ICMP;
c1fa2253
MN
169 else {
170 PREV_ARG(); /* back track */
171 break;
172 }
173
174 if (!NEXT_ARG_OK())
175 break;
176 NEXT_ARG();
177 }
178 }
179
180 *argcp = argc;
181 *argvp = argv;
182
183 return 0;
184}
185
c7699875 186static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
187 int *argcp, char ***argvp)
188{
189 int argc = *argcp;
190 char **argv = *argvp;
191 char *idp = NULL;
192
193 while (1) {
194 if (strcmp(*argv, "mode") == 0) {
195 NEXT_ARG();
196 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
197 } else if (strcmp(*argv, "reqid") == 0) {
198 NEXT_ARG();
199 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
200 } else if (strcmp(*argv, "level") == 0) {
201 NEXT_ARG();
202
203 if (strcmp(*argv, "required") == 0)
204 tmpl->optional = 0;
205 else if (strcmp(*argv, "use") == 0)
206 tmpl->optional = 1;
207 else
e8740e42 208 invarg("LEVEL value is invalid\n", *argv);
c7699875 209
210 } else {
211 if (idp) {
212 PREV_ARG(); /* back track */
213 break;
214 }
215 idp = *argv;
e6e0b60f 216 preferred_family = AF_UNSPEC;
c7699875 217 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
7809c616 218 0, &argc, &argv);
e6e0b60f 219 preferred_family = tmpl->family;
c7699875 220 }
221
222 if (!NEXT_ARG_OK())
223 break;
224
225 NEXT_ARG();
226 }
227 if (argc == *argcp)
228 missarg("TMPL");
229
230 *argcp = argc;
231 *argvp = argv;
232
233 return 0;
234}
235
e4f054f0
JL
236int xfrm_sctx_parse(char *ctxstr, char *s,
237 struct xfrm_user_sec_ctx *sctx)
238{
239 int slen;
240
241 slen = strlen(s) + 1;
242
243 sctx->exttype = XFRMA_SEC_CTX;
244 sctx->ctx_doi = 1;
245 sctx->ctx_alg = 1;
246 sctx->ctx_len = slen;
247 sctx->len = sizeof(struct xfrm_user_sec_ctx) + slen;
248 memcpy(ctxstr, s, slen);
249
250 return 0;
251}
252
56f5daac 253static int xfrm_policy_modify(int cmd, unsigned int flags, int argc, char **argv)
c7699875 254{
255 struct rtnl_handle rth;
256 struct {
257 struct nlmsghdr n;
258 struct xfrm_userpolicy_info xpinfo;
259 char buf[RTA_BUF_SIZE];
d17b136f
PS
260 } req = {
261 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo)),
262 .n.nlmsg_flags = NLM_F_REQUEST | flags,
263 .n.nlmsg_type = cmd,
264 .xpinfo.sel.family = preferred_family,
265 .xpinfo.lft.soft_byte_limit = XFRM_INF,
266 .xpinfo.lft.hard_byte_limit = XFRM_INF,
267 .xpinfo.lft.soft_packet_limit = XFRM_INF,
268 .xpinfo.lft.hard_packet_limit = XFRM_INF,
269 };
c7699875 270 char *dirp = NULL;
7809c616 271 char *selp = NULL;
972938e9 272 char *ptypep = NULL;
e4f054f0 273 char *sctxp = NULL;
d17b136f
PS
274 struct xfrm_userpolicy_type upt = {};
275 char tmpls_buf[XFRM_TMPLS_BUF_SIZE] = {};
c7699875 276 int tmpls_len = 0;
ee675e87 277 struct xfrm_mark mark = {0, 0};
e4f054f0
JL
278 struct {
279 struct xfrm_user_sec_ctx sctx;
280 char str[CTX_BUF_SIZE];
d17b136f 281 } ctx = {};
aed63ae1
EB
282 bool is_if_id_set = false;
283 __u32 if_id = 0;
c7699875 284
285 while (argc > 0) {
286 if (strcmp(*argv, "dir") == 0) {
287 if (dirp)
288 duparg("dir", *argv);
289 dirp = *argv;
290
291 NEXT_ARG();
292 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
e4f054f0
JL
293 } else if (strcmp(*argv, "ctx") == 0) {
294 char *context;
295
296 if (sctxp)
297 duparg("ctx", *argv);
298 sctxp = *argv;
299 NEXT_ARG();
300 context = *argv;
301 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
ee675e87
JHS
302 } else if (strcmp(*argv, "mark") == 0) {
303 xfrm_parse_mark(&mark, &argc, &argv);
c7699875 304 } else if (strcmp(*argv, "index") == 0) {
305 NEXT_ARG();
306 if (get_u32(&req.xpinfo.index, *argv, 0))
e8740e42 307 invarg("INDEX value is invalid", *argv);
972938e9
MN
308 } else if (strcmp(*argv, "ptype") == 0) {
309 if (ptypep)
310 duparg("ptype", *argv);
311 ptypep = *argv;
312
313 NEXT_ARG();
314 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
c7699875 315 } else if (strcmp(*argv, "action") == 0) {
316 NEXT_ARG();
317 if (strcmp(*argv, "allow") == 0)
318 req.xpinfo.action = XFRM_POLICY_ALLOW;
319 else if (strcmp(*argv, "block") == 0)
320 req.xpinfo.action = XFRM_POLICY_BLOCK;
321 else
e8740e42 322 invarg("ACTION value is invalid\n", *argv);
c7699875 323 } else if (strcmp(*argv, "priority") == 0) {
324 NEXT_ARG();
325 if (get_u32(&req.xpinfo.priority, *argv, 0))
e8740e42 326 invarg("PRIORITY value is invalid", *argv);
c1fa2253
MN
327 } else if (strcmp(*argv, "flag") == 0) {
328 NEXT_ARG();
329 xfrm_policy_flag_parse(&req.xpinfo.flags, &argc,
330 &argv);
c7699875 331 } else if (strcmp(*argv, "limit") == 0) {
332 NEXT_ARG();
333 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
334 } else if (strcmp(*argv, "tmpl") == 0) {
335 struct xfrm_user_tmpl *tmpl;
336
337 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
338 fprintf(stderr, "Too many tmpls: buffer overflow\n");
339 exit(1);
340 }
341 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
342
343 tmpl->family = preferred_family;
344 tmpl->aalgos = (~(__u32)0);
345 tmpl->ealgos = (~(__u32)0);
346 tmpl->calgos = (~(__u32)0);
347
348 NEXT_ARG();
349 xfrm_tmpl_parse(tmpl, &argc, &argv);
350
351 tmpls_len += sizeof(*tmpl);
aed63ae1
EB
352 } else if (strcmp(*argv, "if_id") == 0) {
353 NEXT_ARG();
354 if (get_u32(&if_id, *argv, 0))
355 invarg("IF_ID value is invalid", *argv);
356 is_if_id_set = true;
7809c616 357 } else {
358 if (selp)
359 duparg("unknown", *argv);
360 selp = *argv;
361
362 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
363 if (preferred_family == AF_UNSPEC)
364 preferred_family = req.xpinfo.sel.family;
365 }
c7699875 366
367 argc--; argv++;
368 }
369
370 if (!dirp) {
e8740e42 371 fprintf(stderr, "Not enough information: DIR is required.\n");
c7699875 372 exit(1);
373 }
374
972938e9
MN
375 if (ptypep) {
376 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
377 (void *)&upt, sizeof(upt));
378 }
379
c7699875 380 if (tmpls_len > 0) {
381 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
382 (void *)tmpls_buf, tmpls_len);
383 }
384
b5574165 385 if (mark.m) {
ee675e87
JHS
386 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
387 (void *)&mark, sizeof(mark));
388 if (r < 0) {
56f5daac 389 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
ee675e87
JHS
390 exit(1);
391 }
392 }
393
e4f054f0
JL
394 if (sctxp) {
395 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
396 (void *)&ctx, ctx.sctx.len);
397 }
ee675e87 398
aed63ae1
EB
399 if (is_if_id_set)
400 addattr32(&req.n, sizeof(req.buf), XFRMA_IF_ID, if_id);
401
c7699875 402 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
403 exit(1);
404
405 if (req.xpinfo.sel.family == AF_UNSPEC)
406 req.xpinfo.sel.family = AF_INET;
407
86bf43c7 408 if (rtnl_talk(&rth, &req.n, NULL) < 0)
c7699875 409 exit(2);
410
411 rtnl_close(&rth);
412
413 return 0;
414}
415
972938e9
MN
416static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo,
417 __u8 ptype)
c7699875 418{
419 if (!filter.use)
420 return 1;
421
cd21ae40
PS
422 if (filter.xpinfo.sel.family != AF_UNSPEC &&
423 filter.xpinfo.sel.family != xpinfo->sel.family)
424 return 0;
425
c7699875 426 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
427 return 0;
428
20e4840a
TE
429 if (filter.filter_socket && (xpinfo->dir >= XFRM_POLICY_MAX))
430 return 0;
431
972938e9
MN
432 if ((ptype^filter.ptype)&filter.ptype_mask)
433 return 0;
434
c7699875 435 if (filter.sel_src_mask) {
eaa34ee3 436 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
437 filter.sel_src_mask))
c7699875 438 return 0;
439 }
440
441 if (filter.sel_dst_mask) {
eaa34ee3 442 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
443 filter.sel_dst_mask))
c7699875 444 return 0;
445 }
446
447 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
448 return 0;
449
450 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
451 return 0;
452
453 if (filter.upspec_sport_mask) {
454 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
455 return 0;
456 }
457
458 if (filter.upspec_dport_mask) {
459 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
460 return 0;
461 }
462
463 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
464 return 0;
465
466 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
467 return 0;
468
469 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
470 return 0;
471
c1fa2253
MN
472 if (filter.policy_flags_mask)
473 if ((xpinfo->flags & filter.xpinfo.flags) == 0)
474 return 0;
475
c7699875 476 return 1;
477}
478
cd554f2c 479int xfrm_policy_print(struct nlmsghdr *n, void *arg)
c7699875 480{
56f5daac
SH
481 struct rtattr *tb[XFRMA_MAX+1];
482 struct rtattr *rta;
c595c790
SH
483 struct xfrm_userpolicy_info *xpinfo = NULL;
484 struct xfrm_user_polexpire *xpexp = NULL;
485 struct xfrm_userpolicy_id *xpid = NULL;
972938e9 486 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
56f5daac 487 FILE *fp = (FILE *)arg;
c595c790 488 int len = n->nlmsg_len;
c7699875 489
490 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
90f93024 491 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
669ae748 492 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
90f93024 493 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
c7699875 494 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
495 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
496 return 0;
497 }
498
669ae748
SH
499 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
500 xpid = NLMSG_DATA(n);
af1b6a41 501 len -= NLMSG_SPACE(sizeof(*xpid));
669ae748 502 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
90f93024
SH
503 xpexp = NLMSG_DATA(n);
504 xpinfo = &xpexp->pol;
af1b6a41 505 len -= NLMSG_SPACE(sizeof(*xpexp));
90f93024
SH
506 } else {
507 xpexp = NULL;
508 xpinfo = NLMSG_DATA(n);
af1b6a41 509 len -= NLMSG_SPACE(sizeof(*xpinfo));
90f93024
SH
510 }
511
c7699875 512 if (len < 0) {
513 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
514 return -1;
515 }
516
669ae748
SH
517 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
518 rta = XFRMPID_RTA(xpid);
519 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
90f93024
SH
520 rta = XFRMPEXP_RTA(xpexp);
521 else
522 rta = XFRMP_RTA(xpinfo);
523
524 parse_rtattr(tb, XFRMA_MAX, rta, len);
c7699875 525
972938e9
MN
526 if (tb[XFRMA_POLICY_TYPE]) {
527 struct xfrm_userpolicy_type *upt;
528
529 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
530 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
531 return -1;
532 }
9f1370c0 533 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
972938e9
MN
534 ptype = upt->type;
535 }
536
537 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype))
538 return 0;
539
540 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
541 fprintf(fp, "Deleted ");
542 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
543 fprintf(fp, "Updated ");
544 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
545 fprintf(fp, "Expired ");
546
c595c790 547 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
56f5daac 548 /* xfrm_policy_id_print(); */
669ae748
SH
549 if (!tb[XFRMA_POLICY]) {
550 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
551 return -1;
c595c790 552 }
669ae748
SH
553 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
554 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
555 return -1;
556 }
9f1370c0 557 xpinfo = RTA_DATA(tb[XFRMA_POLICY]);
669ae748 558 }
c7699875 559
56e8ad38 560 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
c7699875 561
90f93024
SH
562 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
563 fprintf(fp, "\t");
564 fprintf(fp, "hard %u", xpexp->hard);
565 fprintf(fp, "%s", _SL_);
566 }
567
7809c616 568 if (oneline)
569 fprintf(fp, "\n");
669ae748 570 fflush(fp);
7809c616 571
c7699875 572 return 0;
573}
574
575static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
86bf43c7 576 struct nlmsghdr **answer)
c7699875 577{
578 struct rtnl_handle rth;
579 struct {
580 struct nlmsghdr n;
581 struct xfrm_userpolicy_id xpid;
972938e9 582 char buf[RTA_BUF_SIZE];
d17b136f
PS
583 } req = {
584 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid)),
585 .n.nlmsg_flags = NLM_F_REQUEST,
586 .n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY
587 : XFRM_MSG_GETPOLICY,
588 };
c7699875 589 char *dirp = NULL;
590 char *selp = NULL;
591 char *indexp = NULL;
972938e9 592 char *ptypep = NULL;
e4f054f0 593 char *sctxp = NULL;
d17b136f 594 struct xfrm_userpolicy_type upt = {};
ee675e87 595 struct xfrm_mark mark = {0, 0};
e4f054f0
JL
596 struct {
597 struct xfrm_user_sec_ctx sctx;
598 char str[CTX_BUF_SIZE];
d17b136f 599 } ctx = {};
c7699875 600
601 while (argc > 0) {
602 if (strcmp(*argv, "dir") == 0) {
603 if (dirp)
604 duparg("dir", *argv);
605 dirp = *argv;
606
607 NEXT_ARG();
608 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
609
e4f054f0
JL
610 } else if (strcmp(*argv, "ctx") == 0) {
611 char *context;
612
613 if (sctxp)
614 duparg("ctx", *argv);
615 sctxp = *argv;
616 NEXT_ARG();
617 context = *argv;
618 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
ee675e87
JHS
619 } else if (strcmp(*argv, "mark") == 0) {
620 xfrm_parse_mark(&mark, &argc, &argv);
c7699875 621 } else if (strcmp(*argv, "index") == 0) {
622 if (indexp)
623 duparg("index", *argv);
624 indexp = *argv;
625
626 NEXT_ARG();
627 if (get_u32(&req.xpid.index, *argv, 0))
e8740e42 628 invarg("INDEX value is invalid", *argv);
c7699875 629
972938e9
MN
630 } else if (strcmp(*argv, "ptype") == 0) {
631 if (ptypep)
632 duparg("ptype", *argv);
633 ptypep = *argv;
634
635 NEXT_ARG();
636 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
637
7809c616 638 } else {
639 if (selp)
640 invarg("unknown", *argv);
641 selp = *argv;
642
643 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
644 if (preferred_family == AF_UNSPEC)
645 preferred_family = req.xpid.sel.family;
646
647 }
c7699875 648
649 argc--; argv++;
650 }
651
652 if (!dirp) {
e8740e42 653 fprintf(stderr, "Not enough information: DIR is required.\n");
c7699875 654 exit(1);
655 }
972938e9
MN
656 if (ptypep) {
657 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
658 (void *)&upt, sizeof(upt));
659 }
c7699875 660 if (!selp && !indexp) {
e8740e42 661 fprintf(stderr, "Not enough information: either SELECTOR or INDEX is required.\n");
c7699875 662 exit(1);
663 }
664 if (selp && indexp)
665 duparg2("SELECTOR", "INDEX");
666
667 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
668 exit(1);
669
670 if (req.xpid.sel.family == AF_UNSPEC)
671 req.xpid.sel.family = AF_INET;
672
ee675e87
JHS
673 if (mark.m & mark.v) {
674 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
675 (void *)&mark, sizeof(mark));
676 if (r < 0) {
56f5daac 677 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
ee675e87
JHS
678 exit(1);
679 }
680 }
681
e4f054f0
JL
682 if (sctxp) {
683 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
684 (void *)&ctx, ctx.sctx.len);
685 }
686
86bf43c7 687 if (rtnl_talk(&rth, &req.n, answer) < 0)
c7699875 688 exit(2);
689
690 rtnl_close(&rth);
691
692 return 0;
693}
694
695static int xfrm_policy_delete(int argc, char **argv)
696{
86bf43c7 697 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
c7699875 698}
699
700static int xfrm_policy_get(int argc, char **argv)
701{
86bf43c7 702 struct nlmsghdr *n = NULL;
c7699875 703
86bf43c7 704 xfrm_policy_get_or_delete(argc, argv, 0, &n);
c7699875 705
cd554f2c 706 if (xfrm_policy_print(n, (void *)stdout) < 0) {
c7699875 707 fprintf(stderr, "An error :-)\n");
708 exit(1);
709 }
710
86bf43c7 711 free(n);
c7699875 712 return 0;
713}
714
715/*
716 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
717 * and store it to buffer.
718 */
cd554f2c 719static int xfrm_policy_keep(struct nlmsghdr *n, void *arg)
c7699875 720{
721 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
722 struct rtnl_handle *rth = xb->rth;
723 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
724 int len = n->nlmsg_len;
972938e9
MN
725 struct rtattr *tb[XFRMA_MAX+1];
726 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
c7699875 727 struct nlmsghdr *new_n;
728 struct xfrm_userpolicy_id *xpid;
729
730 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
731 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
732 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
733 return 0;
734 }
735
736 len -= NLMSG_LENGTH(sizeof(*xpinfo));
737 if (len < 0) {
738 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
739 return -1;
740 }
741
972938e9
MN
742 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len);
743
744 if (tb[XFRMA_POLICY_TYPE]) {
745 struct xfrm_userpolicy_type *upt;
746
747 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
748 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
749 return -1;
750 }
9f1370c0 751 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
972938e9
MN
752 ptype = upt->type;
753 }
754
755 if (!xfrm_policy_filter_match(xpinfo, ptype))
c7699875 756 return 0;
757
5474d440
TE
758 /* can't delete socket policies */
759 if (xpinfo->dir >= XFRM_POLICY_MAX)
760 return 0;
761
d5eb0564
AH
762 if (xb->offset + NLMSG_LENGTH(sizeof(*xpid)) > xb->size)
763 return 0;
c7699875 764
765 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
766 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
767 new_n->nlmsg_flags = NLM_F_REQUEST;
768 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
769 new_n->nlmsg_seq = ++rth->seq;
770
771 xpid = NLMSG_DATA(new_n);
772 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
773 xpid->dir = xpinfo->dir;
774 xpid->index = xpinfo->index;
775
0c7d651b
TE
776 if (tb[XFRMA_MARK]) {
777 int r = addattr_l(new_n, xb->size, XFRMA_MARK,
778 (void *)RTA_DATA(tb[XFRMA_MARK]), tb[XFRMA_MARK]->rta_len);
779 if (r < 0) {
780 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
781 exit(1);
782 }
783 }
784
c7699875 785 xb->offset += new_n->nlmsg_len;
56f5daac 786 xb->nlmsg_count++;
c7699875 787
788 return 0;
789}
790
9bec1a43 791static 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 943static 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
1033static 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 1099static 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 1127static 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 1174int 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}