]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_policy.c
Fix FSF address in file headers
[mirror_iproute2.git] / ip / xfrm_policy.c
1 /* $USAGI: $ */
2
3 /*
4 * Copyright (C)2004 USAGI/WIDE Project
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses>.
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
37 //#define NLMSG_DELETEALL_BUF_SIZE (4096-512)
38 #define NLMSG_DELETEALL_BUF_SIZE 8192
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
50 #define CTX_BUF_SIZE 256
51
52 static void usage(void) __attribute__((noreturn));
53
54 static void usage(void)
55 {
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");
65 fprintf(stderr, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n");
66 fprintf(stderr, "Usage: ip xfrm count\n");
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");
84 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
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");
90 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
91 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n");
92 fprintf(stderr, "XFRM-PROTO := ");
93 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
94 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
95 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
96 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
97 fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS));
98 fprintf(stderr, "MODE := transport | tunnel | beet | ro | in_trigger\n");
99 fprintf(stderr, "LEVEL := required | use\n");
100
101 exit(-1);
102 }
103
104 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
105 {
106 int argc = *argcp;
107 char **argv = *argvp;
108
109 if (strcmp(*argv, "in") == 0)
110 *dir = XFRM_POLICY_IN;
111 else if (strcmp(*argv, "out") == 0)
112 *dir = XFRM_POLICY_OUT;
113 else if (strcmp(*argv, "fwd") == 0)
114 *dir = XFRM_POLICY_FWD;
115 else
116 invarg("DIR value is invalid", *argv);
117
118 *argcp = argc;
119 *argvp = argv;
120
121 return 0;
122 }
123
124 static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp)
125 {
126 int argc = *argcp;
127 char **argv = *argvp;
128
129 if (strcmp(*argv, "main") == 0)
130 *ptype = XFRM_POLICY_TYPE_MAIN;
131 else if (strcmp(*argv, "sub") == 0)
132 *ptype = XFRM_POLICY_TYPE_SUB;
133 else
134 invarg("PTYPE value is invalid", *argv);
135
136 *argcp = argc;
137 *argvp = argv;
138
139 return 0;
140 }
141
142 static int xfrm_policy_flag_parse(__u8 *flags, int *argcp, char ***argvp)
143 {
144 int argc = *argcp;
145 char **argv = *argvp;
146 int len = strlen(*argv);
147
148 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
149 __u8 val = 0;
150
151 if (get_u8(&val, *argv, 16))
152 invarg("FLAG value is invalid", *argv);
153 *flags = val;
154 } else {
155 while (1) {
156 if (strcmp(*argv, "localok") == 0)
157 *flags |= XFRM_POLICY_LOCALOK;
158 else if (strcmp(*argv, "icmp") == 0)
159 *flags |= XFRM_POLICY_ICMP;
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
177 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
178 int *argcp, char ***argvp)
179 {
180 int argc = *argcp;
181 char **argv = *argvp;
182 char *idp = NULL;
183
184 while (1) {
185 if (strcmp(*argv, "mode") == 0) {
186 NEXT_ARG();
187 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
188 } else if (strcmp(*argv, "reqid") == 0) {
189 NEXT_ARG();
190 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
191 } else if (strcmp(*argv, "level") == 0) {
192 NEXT_ARG();
193
194 if (strcmp(*argv, "required") == 0)
195 tmpl->optional = 0;
196 else if (strcmp(*argv, "use") == 0)
197 tmpl->optional = 1;
198 else
199 invarg("LEVEL value is invalid\n", *argv);
200
201 } else {
202 if (idp) {
203 PREV_ARG(); /* back track */
204 break;
205 }
206 idp = *argv;
207 preferred_family = AF_UNSPEC;
208 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
209 0, &argc, &argv);
210 preferred_family = tmpl->family;
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
227 int xfrm_sctx_parse(char *ctxstr, char *s,
228 struct xfrm_user_sec_ctx *sctx)
229 {
230 int slen;
231
232 slen = strlen(s) + 1;
233
234 sctx->exttype = XFRMA_SEC_CTX;
235 sctx->ctx_doi = 1;
236 sctx->ctx_alg = 1;
237 sctx->ctx_len = slen;
238 sctx->len = sizeof(struct xfrm_user_sec_ctx) + slen;
239 memcpy(ctxstr, s, slen);
240
241 return 0;
242 }
243
244 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
245 {
246 struct rtnl_handle rth;
247 struct {
248 struct nlmsghdr n;
249 struct xfrm_userpolicy_info xpinfo;
250 char buf[RTA_BUF_SIZE];
251 } req;
252 char *dirp = NULL;
253 char *selp = NULL;
254 char *ptypep = NULL;
255 char *sctxp = NULL;
256 struct xfrm_userpolicy_type upt;
257 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
258 int tmpls_len = 0;
259 struct xfrm_mark mark = {0, 0};
260 struct {
261 struct xfrm_user_sec_ctx sctx;
262 char str[CTX_BUF_SIZE];
263 } ctx;
264
265 memset(&req, 0, sizeof(req));
266 memset(&upt, 0, sizeof(upt));
267 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
268 memset(&ctx, 0, sizeof(ctx));
269
270 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
271 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
272 req.n.nlmsg_type = cmd;
273 req.xpinfo.sel.family = preferred_family;
274
275 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
276 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
277 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
278 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
279
280 while (argc > 0) {
281 if (strcmp(*argv, "dir") == 0) {
282 if (dirp)
283 duparg("dir", *argv);
284 dirp = *argv;
285
286 NEXT_ARG();
287 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
288 } else if (strcmp(*argv, "ctx") == 0) {
289 char *context;
290
291 if (sctxp)
292 duparg("ctx", *argv);
293 sctxp = *argv;
294 NEXT_ARG();
295 context = *argv;
296 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
297 } else if (strcmp(*argv, "mark") == 0) {
298 xfrm_parse_mark(&mark, &argc, &argv);
299 } else if (strcmp(*argv, "index") == 0) {
300 NEXT_ARG();
301 if (get_u32(&req.xpinfo.index, *argv, 0))
302 invarg("INDEX value is invalid", *argv);
303 } else if (strcmp(*argv, "ptype") == 0) {
304 if (ptypep)
305 duparg("ptype", *argv);
306 ptypep = *argv;
307
308 NEXT_ARG();
309 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
310 } else if (strcmp(*argv, "action") == 0) {
311 NEXT_ARG();
312 if (strcmp(*argv, "allow") == 0)
313 req.xpinfo.action = XFRM_POLICY_ALLOW;
314 else if (strcmp(*argv, "block") == 0)
315 req.xpinfo.action = XFRM_POLICY_BLOCK;
316 else
317 invarg("ACTION value is invalid\n", *argv);
318 } else if (strcmp(*argv, "priority") == 0) {
319 NEXT_ARG();
320 if (get_u32(&req.xpinfo.priority, *argv, 0))
321 invarg("PRIORITY value is invalid", *argv);
322 } else if (strcmp(*argv, "flag") == 0) {
323 NEXT_ARG();
324 xfrm_policy_flag_parse(&req.xpinfo.flags, &argc,
325 &argv);
326 } else if (strcmp(*argv, "limit") == 0) {
327 NEXT_ARG();
328 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
329 } else if (strcmp(*argv, "tmpl") == 0) {
330 struct xfrm_user_tmpl *tmpl;
331
332 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
333 fprintf(stderr, "Too many tmpls: buffer overflow\n");
334 exit(1);
335 }
336 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
337
338 tmpl->family = preferred_family;
339 tmpl->aalgos = (~(__u32)0);
340 tmpl->ealgos = (~(__u32)0);
341 tmpl->calgos = (~(__u32)0);
342
343 NEXT_ARG();
344 xfrm_tmpl_parse(tmpl, &argc, &argv);
345
346 tmpls_len += sizeof(*tmpl);
347 } else {
348 if (selp)
349 duparg("unknown", *argv);
350 selp = *argv;
351
352 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
353 if (preferred_family == AF_UNSPEC)
354 preferred_family = req.xpinfo.sel.family;
355 }
356
357 argc--; argv++;
358 }
359
360 if (!dirp) {
361 fprintf(stderr, "Not enough information: DIR is required.\n");
362 exit(1);
363 }
364
365 if (ptypep) {
366 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
367 (void *)&upt, sizeof(upt));
368 }
369
370 if (tmpls_len > 0) {
371 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
372 (void *)tmpls_buf, tmpls_len);
373 }
374
375 if (mark.m) {
376 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
377 (void *)&mark, sizeof(mark));
378 if (r < 0) {
379 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
380 exit(1);
381 }
382 }
383
384 if (sctxp) {
385 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
386 (void *)&ctx, ctx.sctx.len);
387 }
388
389 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
390 exit(1);
391
392 if (req.xpinfo.sel.family == AF_UNSPEC)
393 req.xpinfo.sel.family = AF_INET;
394
395 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
396 exit(2);
397
398 rtnl_close(&rth);
399
400 return 0;
401 }
402
403 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo,
404 __u8 ptype)
405 {
406 if (!filter.use)
407 return 1;
408
409 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
410 return 0;
411
412 if ((ptype^filter.ptype)&filter.ptype_mask)
413 return 0;
414
415 if (filter.sel_src_mask) {
416 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
417 filter.sel_src_mask))
418 return 0;
419 }
420
421 if (filter.sel_dst_mask) {
422 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
423 filter.sel_dst_mask))
424 return 0;
425 }
426
427 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
428 return 0;
429
430 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
431 return 0;
432
433 if (filter.upspec_sport_mask) {
434 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
435 return 0;
436 }
437
438 if (filter.upspec_dport_mask) {
439 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
440 return 0;
441 }
442
443 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
444 return 0;
445
446 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
447 return 0;
448
449 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
450 return 0;
451
452 if (filter.policy_flags_mask)
453 if ((xpinfo->flags & filter.xpinfo.flags) == 0)
454 return 0;
455
456 return 1;
457 }
458
459 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
460 void *arg)
461 {
462 struct rtattr * tb[XFRMA_MAX+1];
463 struct rtattr * rta;
464 struct xfrm_userpolicy_info *xpinfo = NULL;
465 struct xfrm_user_polexpire *xpexp = NULL;
466 struct xfrm_userpolicy_id *xpid = NULL;
467 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
468 FILE *fp = (FILE*)arg;
469 int len = n->nlmsg_len;
470
471 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
472 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
473 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
474 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
475 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
476 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
477 return 0;
478 }
479
480 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
481 xpid = NLMSG_DATA(n);
482 len -= NLMSG_SPACE(sizeof(*xpid));
483 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
484 xpexp = NLMSG_DATA(n);
485 xpinfo = &xpexp->pol;
486 len -= NLMSG_SPACE(sizeof(*xpexp));
487 } else {
488 xpexp = NULL;
489 xpinfo = NLMSG_DATA(n);
490 len -= NLMSG_SPACE(sizeof(*xpinfo));
491 }
492
493 if (len < 0) {
494 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
495 return -1;
496 }
497
498 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
499 rta = XFRMPID_RTA(xpid);
500 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
501 rta = XFRMPEXP_RTA(xpexp);
502 else
503 rta = XFRMP_RTA(xpinfo);
504
505 parse_rtattr(tb, XFRMA_MAX, rta, len);
506
507 if (tb[XFRMA_POLICY_TYPE]) {
508 struct xfrm_userpolicy_type *upt;
509
510 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
511 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
512 return -1;
513 }
514 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
515 ptype = upt->type;
516 }
517
518 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype))
519 return 0;
520
521 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
522 fprintf(fp, "Deleted ");
523 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
524 fprintf(fp, "Updated ");
525 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
526 fprintf(fp, "Expired ");
527
528 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
529 //xfrm_policy_id_print();
530 if (!tb[XFRMA_POLICY]) {
531 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
532 return -1;
533 }
534 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
535 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
536 return -1;
537 }
538 xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]);
539 }
540
541 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
542
543 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
544 fprintf(fp, "\t");
545 fprintf(fp, "hard %u", xpexp->hard);
546 fprintf(fp, "%s", _SL_);
547 }
548
549 if (oneline)
550 fprintf(fp, "\n");
551 fflush(fp);
552
553 return 0;
554 }
555
556 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
557 void *res_nlbuf)
558 {
559 struct rtnl_handle rth;
560 struct {
561 struct nlmsghdr n;
562 struct xfrm_userpolicy_id xpid;
563 char buf[RTA_BUF_SIZE];
564 } req;
565 char *dirp = NULL;
566 char *selp = NULL;
567 char *indexp = NULL;
568 char *ptypep = NULL;
569 char *sctxp = NULL;
570 struct xfrm_userpolicy_type upt;
571 struct xfrm_mark mark = {0, 0};
572 struct {
573 struct xfrm_user_sec_ctx sctx;
574 char str[CTX_BUF_SIZE];
575 } ctx;
576
577
578 memset(&req, 0, sizeof(req));
579 memset(&upt, 0, sizeof(upt));
580 memset(&ctx, 0, sizeof(ctx));
581
582 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
583 req.n.nlmsg_flags = NLM_F_REQUEST;
584 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
585
586 while (argc > 0) {
587 if (strcmp(*argv, "dir") == 0) {
588 if (dirp)
589 duparg("dir", *argv);
590 dirp = *argv;
591
592 NEXT_ARG();
593 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
594
595 } else if (strcmp(*argv, "ctx") == 0) {
596 char *context;
597
598 if (sctxp)
599 duparg("ctx", *argv);
600 sctxp = *argv;
601 NEXT_ARG();
602 context = *argv;
603 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
604 } else if (strcmp(*argv, "mark") == 0) {
605 xfrm_parse_mark(&mark, &argc, &argv);
606 } else if (strcmp(*argv, "index") == 0) {
607 if (indexp)
608 duparg("index", *argv);
609 indexp = *argv;
610
611 NEXT_ARG();
612 if (get_u32(&req.xpid.index, *argv, 0))
613 invarg("INDEX value is invalid", *argv);
614
615 } else if (strcmp(*argv, "ptype") == 0) {
616 if (ptypep)
617 duparg("ptype", *argv);
618 ptypep = *argv;
619
620 NEXT_ARG();
621 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
622
623 } else {
624 if (selp)
625 invarg("unknown", *argv);
626 selp = *argv;
627
628 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
629 if (preferred_family == AF_UNSPEC)
630 preferred_family = req.xpid.sel.family;
631
632 }
633
634 argc--; argv++;
635 }
636
637 if (!dirp) {
638 fprintf(stderr, "Not enough information: DIR is required.\n");
639 exit(1);
640 }
641 if (ptypep) {
642 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
643 (void *)&upt, sizeof(upt));
644 }
645 if (!selp && !indexp) {
646 fprintf(stderr, "Not enough information: either SELECTOR or INDEX is required.\n");
647 exit(1);
648 }
649 if (selp && indexp)
650 duparg2("SELECTOR", "INDEX");
651
652 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
653 exit(1);
654
655 if (req.xpid.sel.family == AF_UNSPEC)
656 req.xpid.sel.family = AF_INET;
657
658 if (mark.m & mark.v) {
659 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
660 (void *)&mark, sizeof(mark));
661 if (r < 0) {
662 fprintf(stderr, "%s: XFRMA_MARK failed\n",__func__);
663 exit(1);
664 }
665 }
666
667 if (sctxp) {
668 addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX,
669 (void *)&ctx, ctx.sctx.len);
670 }
671
672 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf) < 0)
673 exit(2);
674
675 rtnl_close(&rth);
676
677 return 0;
678 }
679
680 static int xfrm_policy_delete(int argc, char **argv)
681 {
682 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
683 }
684
685 static int xfrm_policy_get(int argc, char **argv)
686 {
687 char buf[NLMSG_BUF_SIZE];
688 struct nlmsghdr *n = (struct nlmsghdr *)buf;
689
690 memset(buf, 0, sizeof(buf));
691
692 xfrm_policy_get_or_delete(argc, argv, 0, n);
693
694 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
695 fprintf(stderr, "An error :-)\n");
696 exit(1);
697 }
698
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 */
706 static int xfrm_policy_keep(const struct sockaddr_nl *who,
707 struct nlmsghdr *n,
708 void *arg)
709 {
710 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
711 struct rtnl_handle *rth = xb->rth;
712 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
713 int len = n->nlmsg_len;
714 struct rtattr *tb[XFRMA_MAX+1];
715 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
716 struct nlmsghdr *new_n;
717 struct xfrm_userpolicy_id *xpid;
718
719 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
720 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
721 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
722 return 0;
723 }
724
725 len -= NLMSG_LENGTH(sizeof(*xpinfo));
726 if (len < 0) {
727 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
728 return -1;
729 }
730
731 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len);
732
733 if (tb[XFRMA_POLICY_TYPE]) {
734 struct xfrm_userpolicy_type *upt;
735
736 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
737 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
738 return -1;
739 }
740 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
741 ptype = upt->type;
742 }
743
744 if (!xfrm_policy_filter_match(xpinfo, ptype))
745 return 0;
746
747 if (xb->offset > xb->size) {
748 fprintf(stderr, "Policy buffer overflow\n");
749 return -1;
750 }
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
763 xb->offset += new_n->nlmsg_len;
764 xb->nlmsg_count ++;
765
766 return 0;
767 }
768
769 static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall)
770 {
771 char *selp = NULL;
772 struct rtnl_handle rth;
773
774 if (argc > 0)
775 filter.use = 1;
776 filter.xpinfo.sel.family = preferred_family;
777
778 while (argc > 0) {
779 if (strcmp(*argv, "dir") == 0) {
780 NEXT_ARG();
781 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
782
783 filter.dir_mask = XFRM_FILTER_MASK_FULL;
784
785 } else if (strcmp(*argv, "index") == 0) {
786 NEXT_ARG();
787 if (get_u32(&filter.xpinfo.index, *argv, 0))
788 invarg("INDEX value is invalid", *argv);
789
790 filter.index_mask = XFRM_FILTER_MASK_FULL;
791
792 } else if (strcmp(*argv, "ptype") == 0) {
793 NEXT_ARG();
794 xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv);
795
796 filter.ptype_mask = XFRM_FILTER_MASK_FULL;
797
798 } else if (strcmp(*argv, "action") == 0) {
799 NEXT_ARG();
800 if (strcmp(*argv, "allow") == 0)
801 filter.xpinfo.action = XFRM_POLICY_ALLOW;
802 else if (strcmp(*argv, "block") == 0)
803 filter.xpinfo.action = XFRM_POLICY_BLOCK;
804 else
805 invarg("ACTION value is invalid\n", *argv);
806
807 filter.action_mask = XFRM_FILTER_MASK_FULL;
808
809 } else if (strcmp(*argv, "priority") == 0) {
810 NEXT_ARG();
811 if (get_u32(&filter.xpinfo.priority, *argv, 0))
812 invarg("PRIORITY value is invalid", *argv);
813
814 filter.priority_mask = XFRM_FILTER_MASK_FULL;
815
816 } else if (strcmp(*argv, "flag") == 0) {
817 NEXT_ARG();
818 xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc,
819 &argv);
820
821 filter.policy_flags_mask = XFRM_FILTER_MASK_FULL;
822
823 } else {
824 if (selp)
825 invarg("unknown", *argv);
826 selp = *argv;
827
828 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
829 if (preferred_family == AF_UNSPEC)
830 preferred_family = filter.xpinfo.sel.family;
831
832 }
833
834 argc--; argv++;
835 }
836
837 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
838 exit(1);
839
840 if (deleteall) {
841 struct xfrm_buffer xb;
842 char buf[NLMSG_DELETEALL_BUF_SIZE];
843 int i;
844
845 xb.buf = buf;
846 xb.size = sizeof(buf);
847 xb.rth = &rth;
848
849 for (i = 0; ; i++) {
850 xb.offset = 0;
851 xb.nlmsg_count = 0;
852
853 if (show_stats > 1)
854 fprintf(stderr, "Delete-all round = %d\n", i);
855
856 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
857 perror("Cannot send dump request");
858 exit(1);
859 }
860
861 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb) < 0) {
862 fprintf(stderr, "Delete-all terminated\n");
863 exit(1);
864 }
865 if (xb.nlmsg_count == 0) {
866 if (show_stats > 1)
867 fprintf(stderr, "Delete-all completed\n");
868 break;
869 }
870
871 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
872 perror("Failed to send delete-all request");
873 exit(1);
874 }
875 if (show_stats > 1)
876 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
877
878 xb.offset = 0;
879 xb.nlmsg_count = 0;
880 }
881 } else {
882 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
883 perror("Cannot send dump request");
884 exit(1);
885 }
886
887 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout) < 0) {
888 fprintf(stderr, "Dump terminated\n");
889 exit(1);
890 }
891 }
892
893 rtnl_close(&rth);
894
895 exit(0);
896 }
897
898 static int print_spdinfo( struct nlmsghdr *n, void *arg)
899 {
900 FILE *fp = (FILE*)arg;
901 __u32 *f = NLMSG_DATA(n);
902 struct rtattr * tb[XFRMA_SPD_MAX+1];
903 struct rtattr * rta;
904
905 int len = n->nlmsg_len;
906
907 len -= NLMSG_LENGTH(sizeof(__u32));
908 if (len < 0) {
909 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
910 return -1;
911 }
912
913 rta = XFRMSAPD_RTA(f);
914 parse_rtattr(tb, XFRMA_SPD_MAX, rta, len);
915
916 fprintf(fp,"\t SPD");
917 if (tb[XFRMA_SPD_INFO]) {
918 struct xfrmu_spdinfo *si;
919
920 if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) {
921 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
922 return -1;
923 }
924 si = RTA_DATA(tb[XFRMA_SPD_INFO]);
925 fprintf(fp," IN %d", si->incnt);
926 fprintf(fp," OUT %d", si->outcnt);
927 fprintf(fp," FWD %d", si->fwdcnt);
928
929 if (show_stats) {
930 fprintf(fp," (Sock:");
931 fprintf(fp," IN %d", si->inscnt);
932 fprintf(fp," OUT %d", si->outscnt);
933 fprintf(fp," FWD %d", si->fwdscnt);
934 fprintf(fp,")");
935 }
936
937 fprintf(fp,"\n");
938 }
939 if (show_stats > 1) {
940 struct xfrmu_spdhinfo *sh;
941
942 if (tb[XFRMA_SPD_HINFO]) {
943 if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) {
944 fprintf(stderr, "SPDinfo: Wrong len %d\n", len);
945 return -1;
946 }
947 sh = RTA_DATA(tb[XFRMA_SPD_HINFO]);
948 fprintf(fp,"\t SPD buckets:");
949 fprintf(fp," count %d", sh->spdhcnt);
950 fprintf(fp," Max %d", sh->spdhmcnt);
951 }
952 }
953 fprintf(fp,"\n");
954
955 return 0;
956 }
957
958 static int xfrm_spd_getinfo(int argc, char **argv)
959 {
960 struct rtnl_handle rth;
961 struct {
962 struct nlmsghdr n;
963 __u32 flags;
964 char ans[128];
965 } req;
966
967 memset(&req, 0, sizeof(req));
968
969 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32));
970 req.n.nlmsg_flags = NLM_F_REQUEST;
971 req.n.nlmsg_type = XFRM_MSG_GETSPDINFO;
972 req.flags = 0XFFFFFFFF;
973
974 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
975 exit(1);
976
977 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0)
978 exit(2);
979
980 print_spdinfo(&req.n, (void*)stdout);
981
982 rtnl_close(&rth);
983
984 return 0;
985 }
986
987 static int xfrm_policy_flush(int argc, char **argv)
988 {
989 struct rtnl_handle rth;
990 struct {
991 struct nlmsghdr n;
992 char buf[RTA_BUF_SIZE];
993 } req;
994 char *ptypep = NULL;
995 struct xfrm_userpolicy_type upt;
996
997 memset(&req, 0, sizeof(req));
998 memset(&upt, 0, sizeof(upt));
999
1000 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
1001 req.n.nlmsg_flags = NLM_F_REQUEST;
1002 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
1003
1004 while (argc > 0) {
1005 if (strcmp(*argv, "ptype") == 0) {
1006 if (ptypep)
1007 duparg("ptype", *argv);
1008 ptypep = *argv;
1009
1010 NEXT_ARG();
1011 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
1012 } else
1013 invarg("unknown", *argv);
1014
1015 argc--; argv++;
1016 }
1017
1018 if (ptypep) {
1019 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
1020 (void *)&upt, sizeof(upt));
1021 }
1022
1023 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1024 exit(1);
1025
1026 if (show_stats > 1)
1027 fprintf(stderr, "Flush policy\n");
1028
1029 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1030 exit(2);
1031
1032 rtnl_close(&rth);
1033
1034 return 0;
1035 }
1036
1037 int do_xfrm_policy(int argc, char **argv)
1038 {
1039 if (argc < 1)
1040 return xfrm_policy_list_or_deleteall(0, NULL, 0);
1041
1042 if (matches(*argv, "add") == 0)
1043 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
1044 argc-1, argv+1);
1045 if (matches(*argv, "update") == 0)
1046 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
1047 argc-1, argv+1);
1048 if (matches(*argv, "delete") == 0)
1049 return xfrm_policy_delete(argc-1, argv+1);
1050 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1051 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1);
1052 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1053 || matches(*argv, "lst") == 0)
1054 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0);
1055 if (matches(*argv, "get") == 0)
1056 return xfrm_policy_get(argc-1, argv+1);
1057 if (matches(*argv, "flush") == 0)
1058 return xfrm_policy_flush(argc-1, argv+1);
1059 if (matches(*argv, "count") == 0)
1060 return xfrm_spd_getinfo(argc, argv);
1061 if (matches(*argv, "help") == 0)
1062 usage();
1063 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);
1064 exit(-1);
1065 }