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