]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_policy.c
XFRM: sub policy support.
[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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 /*
21 * based on iproute.c
22 */
23 /*
24 * Authors:
25 * Masahide NAKAMURA @USAGI
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <netdb.h>
32 #include <linux/netlink.h>
33 #include <linux/xfrm.h>
34 #include "utils.h"
35 #include "xfrm.h"
36 #include "ip_common.h"
37
38 //#define NLMSG_DELETEALL_BUF_SIZE (4096-512)
39 #define NLMSG_DELETEALL_BUF_SIZE 8192
40
41 /*
42 * Receiving buffer defines:
43 * nlmsg
44 * data = struct xfrm_userpolicy_info
45 * rtattr
46 * data = struct xfrm_user_tmpl[]
47 */
48 #define NLMSG_BUF_SIZE 4096
49 #define RTA_BUF_SIZE 2048
50 #define XFRM_TMPLS_BUF_SIZE 1024
51
52 static void usage(void) __attribute__((noreturn));
53
54 static void usage(void)
55 {
56 fprintf(stderr, "Usage: ip xfrm policy { add | update } dir DIR SELECTOR [ index INDEX ] [ ptype PTYPE ]\n");
57 fprintf(stderr, " [ action ACTION ] [ priority PRIORITY ] [ LIMIT-LIST ] [ TMPL-LIST ]\n");
58 fprintf(stderr, "Usage: ip xfrm policy { delete | get } dir DIR [ SELECTOR | index INDEX ] [ ptype PTYPE ]\n");
59 fprintf(stderr, "Usage: ip xfrm policy { deleteall | list } [ dir DIR ] [ SELECTOR ]\n");
60 fprintf(stderr, " [ index INDEX ] [ action ACTION ] [ priority PRIORITY ]\n");
61 fprintf(stderr, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n");
62
63 fprintf(stderr, "PTYPE := [ main | sub ](default=main)\n");
64 fprintf(stderr, "DIR := [ in | out | fwd ]\n");
65
66 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
67
68 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
69 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n");
70
71 //fprintf(stderr, "DEV - device name(default=none)\n");
72
73 fprintf(stderr, "ACTION := [ allow | block ](default=allow)\n");
74
75 //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
76
77 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
78 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
79 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
80
81 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
82 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
83 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
84
85 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
86 fprintf(stderr, "XFRM_PROTO := [ ");
87 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
88 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
89 fprintf(stderr, "%s", strxf_xfrmproto(IPPROTO_COMP));
90 fprintf(stderr, " ]\n");
91
92 fprintf(stderr, "MODE := [ transport | tunnel | beet ](default=transport)\n");
93 //fprintf(stderr, "REQID - number(default=0)\n");
94 fprintf(stderr, "LEVEL := [ required | use ](default=required)\n");
95
96 exit(-1);
97 }
98
99 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
100 {
101 int argc = *argcp;
102 char **argv = *argvp;
103
104 if (strcmp(*argv, "in") == 0)
105 *dir = XFRM_POLICY_IN;
106 else if (strcmp(*argv, "out") == 0)
107 *dir = XFRM_POLICY_OUT;
108 else if (strcmp(*argv, "fwd") == 0)
109 *dir = XFRM_POLICY_FWD;
110 else
111 invarg("\"DIR\" is invalid", *argv);
112
113 *argcp = argc;
114 *argvp = argv;
115
116 return 0;
117 }
118
119 static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp)
120 {
121 int argc = *argcp;
122 char **argv = *argvp;
123
124 if (strcmp(*argv, "main") == 0)
125 *ptype = XFRM_POLICY_TYPE_MAIN;
126 else if (strcmp(*argv, "sub") == 0)
127 *ptype = XFRM_POLICY_TYPE_SUB;
128 else
129 invarg("\"PTYPE\" is invalid", *argv);
130
131 *argcp = argc;
132 *argvp = argv;
133
134 return 0;
135 }
136
137 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
138 int *argcp, char ***argvp)
139 {
140 int argc = *argcp;
141 char **argv = *argvp;
142 char *idp = NULL;
143
144 while (1) {
145 if (strcmp(*argv, "mode") == 0) {
146 NEXT_ARG();
147 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
148 } else if (strcmp(*argv, "reqid") == 0) {
149 NEXT_ARG();
150 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
151 } else if (strcmp(*argv, "level") == 0) {
152 NEXT_ARG();
153
154 if (strcmp(*argv, "required") == 0)
155 tmpl->optional = 0;
156 else if (strcmp(*argv, "use") == 0)
157 tmpl->optional = 1;
158 else
159 invarg("\"LEVEL\" is invalid\n", *argv);
160
161 } else {
162 if (idp) {
163 PREV_ARG(); /* back track */
164 break;
165 }
166 idp = *argv;
167 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
168 0, &argc, &argv);
169 if (preferred_family == AF_UNSPEC)
170 preferred_family = tmpl->family;
171 }
172
173 if (!NEXT_ARG_OK())
174 break;
175
176 NEXT_ARG();
177 }
178 if (argc == *argcp)
179 missarg("TMPL");
180
181 *argcp = argc;
182 *argvp = argv;
183
184 return 0;
185 }
186
187 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
188 {
189 struct rtnl_handle rth;
190 struct {
191 struct nlmsghdr n;
192 struct xfrm_userpolicy_info xpinfo;
193 char buf[RTA_BUF_SIZE];
194 } req;
195 char *dirp = NULL;
196 char *selp = NULL;
197 char *ptypep = NULL;
198 struct xfrm_userpolicy_type upt;
199 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
200 int tmpls_len = 0;
201
202 memset(&req, 0, sizeof(req));
203 memset(&upt, 0, sizeof(upt));
204 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
205
206 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
207 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
208 req.n.nlmsg_type = cmd;
209 req.xpinfo.sel.family = preferred_family;
210
211 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
212 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
213 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
214 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
215
216 while (argc > 0) {
217 if (strcmp(*argv, "dir") == 0) {
218 if (dirp)
219 duparg("dir", *argv);
220 dirp = *argv;
221
222 NEXT_ARG();
223 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
224
225 filter.dir_mask = XFRM_FILTER_MASK_FULL;
226
227 } else if (strcmp(*argv, "index") == 0) {
228 NEXT_ARG();
229 if (get_u32(&req.xpinfo.index, *argv, 0))
230 invarg("\"INDEX\" is invalid", *argv);
231
232 filter.index_mask = XFRM_FILTER_MASK_FULL;
233
234 } else if (strcmp(*argv, "ptype") == 0) {
235 if (ptypep)
236 duparg("ptype", *argv);
237 ptypep = *argv;
238
239 NEXT_ARG();
240 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
241
242 filter.dir_mask = XFRM_FILTER_MASK_FULL;
243
244 } else if (strcmp(*argv, "action") == 0) {
245 NEXT_ARG();
246 if (strcmp(*argv, "allow") == 0)
247 req.xpinfo.action = XFRM_POLICY_ALLOW;
248 else if (strcmp(*argv, "block") == 0)
249 req.xpinfo.action = XFRM_POLICY_BLOCK;
250 else
251 invarg("\"action\" value is invalid\n", *argv);
252
253 filter.action_mask = XFRM_FILTER_MASK_FULL;
254
255 } else if (strcmp(*argv, "priority") == 0) {
256 NEXT_ARG();
257 if (get_u32(&req.xpinfo.priority, *argv, 0))
258 invarg("\"PRIORITY\" is invalid", *argv);
259
260 filter.priority_mask = XFRM_FILTER_MASK_FULL;
261
262 } else if (strcmp(*argv, "limit") == 0) {
263 NEXT_ARG();
264 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
265 } else if (strcmp(*argv, "tmpl") == 0) {
266 struct xfrm_user_tmpl *tmpl;
267
268 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
269 fprintf(stderr, "Too many tmpls: buffer overflow\n");
270 exit(1);
271 }
272 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
273
274 tmpl->family = preferred_family;
275 tmpl->aalgos = (~(__u32)0);
276 tmpl->ealgos = (~(__u32)0);
277 tmpl->calgos = (~(__u32)0);
278
279 NEXT_ARG();
280 xfrm_tmpl_parse(tmpl, &argc, &argv);
281
282 tmpls_len += sizeof(*tmpl);
283 } else {
284 if (selp)
285 duparg("unknown", *argv);
286 selp = *argv;
287
288 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
289 if (preferred_family == AF_UNSPEC)
290 preferred_family = req.xpinfo.sel.family;
291 }
292
293 argc--; argv++;
294 }
295
296 if (!dirp) {
297 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
298 exit(1);
299 }
300
301 if (ptypep) {
302 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
303 (void *)&upt, sizeof(upt));
304 }
305
306 if (tmpls_len > 0) {
307 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
308 (void *)tmpls_buf, tmpls_len);
309 }
310
311 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
312 exit(1);
313
314 if (req.xpinfo.sel.family == AF_UNSPEC)
315 req.xpinfo.sel.family = AF_INET;
316
317 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
318 exit(2);
319
320 rtnl_close(&rth);
321
322 return 0;
323 }
324
325 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo,
326 __u8 ptype)
327 {
328 if (!filter.use)
329 return 1;
330
331 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
332 return 0;
333
334 if ((ptype^filter.ptype)&filter.ptype_mask)
335 return 0;
336
337 if (filter.sel_src_mask) {
338 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
339 filter.sel_src_mask))
340 return 0;
341 }
342
343 if (filter.sel_dst_mask) {
344 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
345 filter.sel_dst_mask))
346 return 0;
347 }
348
349 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
350 return 0;
351
352 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
353 return 0;
354
355 if (filter.upspec_sport_mask) {
356 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
357 return 0;
358 }
359
360 if (filter.upspec_dport_mask) {
361 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
362 return 0;
363 }
364
365 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
366 return 0;
367
368 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
369 return 0;
370
371 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
372 return 0;
373
374 return 1;
375 }
376
377 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
378 void *arg)
379 {
380 struct rtattr * tb[XFRMA_MAX+1];
381 struct rtattr * rta;
382 struct xfrm_userpolicy_info *xpinfo = NULL;
383 struct xfrm_user_polexpire *xpexp = NULL;
384 struct xfrm_userpolicy_id *xpid = NULL;
385 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
386 FILE *fp = (FILE*)arg;
387 int len = n->nlmsg_len;
388
389 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
390 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
391 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
392 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
393 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
394 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
395 return 0;
396 }
397
398 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
399 xpid = NLMSG_DATA(n);
400 len -= NLMSG_SPACE(sizeof(*xpid));
401 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
402 xpexp = NLMSG_DATA(n);
403 xpinfo = &xpexp->pol;
404 len -= NLMSG_SPACE(sizeof(*xpexp));
405 } else {
406 xpexp = NULL;
407 xpinfo = NLMSG_DATA(n);
408 len -= NLMSG_SPACE(sizeof(*xpinfo));
409 }
410
411 if (len < 0) {
412 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
413 return -1;
414 }
415
416 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
417 rta = XFRMPID_RTA(xpid);
418 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
419 rta = XFRMPEXP_RTA(xpexp);
420 else
421 rta = XFRMP_RTA(xpinfo);
422
423 parse_rtattr(tb, XFRMA_MAX, rta, len);
424
425 if (tb[XFRMA_POLICY_TYPE]) {
426 struct xfrm_userpolicy_type *upt;
427
428 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
429 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
430 return -1;
431 }
432 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
433 ptype = upt->type;
434 }
435
436 if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype))
437 return 0;
438
439 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
440 fprintf(fp, "Deleted ");
441 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
442 fprintf(fp, "Updated ");
443 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
444 fprintf(fp, "Expired ");
445
446 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
447 //xfrm_policy_id_print();
448 if (!tb[XFRMA_POLICY]) {
449 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
450 return -1;
451 }
452 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
453 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
454 return -1;
455 }
456 xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]);
457 }
458
459 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
460
461 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
462 fprintf(fp, "\t");
463 fprintf(fp, "hard %u", xpexp->hard);
464 fprintf(fp, "%s", _SL_);
465 }
466
467 if (oneline)
468 fprintf(fp, "\n");
469 fflush(fp);
470
471 return 0;
472 }
473
474 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
475 void *res_nlbuf)
476 {
477 struct rtnl_handle rth;
478 struct {
479 struct nlmsghdr n;
480 struct xfrm_userpolicy_id xpid;
481 char buf[RTA_BUF_SIZE];
482 } req;
483 char *dirp = NULL;
484 char *selp = NULL;
485 char *indexp = NULL;
486 char *ptypep = NULL;
487 struct xfrm_userpolicy_type upt;
488
489 memset(&req, 0, sizeof(req));
490 memset(&upt, 0, sizeof(upt));
491
492 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
493 req.n.nlmsg_flags = NLM_F_REQUEST;
494 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
495
496 while (argc > 0) {
497 if (strcmp(*argv, "dir") == 0) {
498 if (dirp)
499 duparg("dir", *argv);
500 dirp = *argv;
501
502 NEXT_ARG();
503 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
504
505 } else if (strcmp(*argv, "index") == 0) {
506 if (indexp)
507 duparg("index", *argv);
508 indexp = *argv;
509
510 NEXT_ARG();
511 if (get_u32(&req.xpid.index, *argv, 0))
512 invarg("\"INDEX\" is invalid", *argv);
513
514 } else if (strcmp(*argv, "ptype") == 0) {
515 if (ptypep)
516 duparg("ptype", *argv);
517 ptypep = *argv;
518
519 NEXT_ARG();
520 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
521
522 } else {
523 if (selp)
524 invarg("unknown", *argv);
525 selp = *argv;
526
527 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
528 if (preferred_family == AF_UNSPEC)
529 preferred_family = req.xpid.sel.family;
530
531 }
532
533 argc--; argv++;
534 }
535
536 if (!dirp) {
537 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
538 exit(1);
539 }
540 if (ptypep) {
541 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
542 (void *)&upt, sizeof(upt));
543 }
544 if (!selp && !indexp) {
545 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
546 exit(1);
547 }
548 if (selp && indexp)
549 duparg2("SELECTOR", "INDEX");
550
551 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
552 exit(1);
553
554 if (req.xpid.sel.family == AF_UNSPEC)
555 req.xpid.sel.family = AF_INET;
556
557 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf, NULL, NULL) < 0)
558 exit(2);
559
560 rtnl_close(&rth);
561
562 return 0;
563 }
564
565 static int xfrm_policy_delete(int argc, char **argv)
566 {
567 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
568 }
569
570 static int xfrm_policy_get(int argc, char **argv)
571 {
572 char buf[NLMSG_BUF_SIZE];
573 struct nlmsghdr *n = (struct nlmsghdr *)buf;
574
575 memset(buf, 0, sizeof(buf));
576
577 xfrm_policy_get_or_delete(argc, argv, 0, n);
578
579 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
580 fprintf(stderr, "An error :-)\n");
581 exit(1);
582 }
583
584 return 0;
585 }
586
587 /*
588 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
589 * and store it to buffer.
590 */
591 static int xfrm_policy_keep(const struct sockaddr_nl *who,
592 struct nlmsghdr *n,
593 void *arg)
594 {
595 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
596 struct rtnl_handle *rth = xb->rth;
597 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
598 int len = n->nlmsg_len;
599 struct rtattr *tb[XFRMA_MAX+1];
600 __u8 ptype = XFRM_POLICY_TYPE_MAIN;
601 struct nlmsghdr *new_n;
602 struct xfrm_userpolicy_id *xpid;
603
604 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
605 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
606 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
607 return 0;
608 }
609
610 len -= NLMSG_LENGTH(sizeof(*xpinfo));
611 if (len < 0) {
612 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
613 return -1;
614 }
615
616 parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len);
617
618 if (tb[XFRMA_POLICY_TYPE]) {
619 struct xfrm_userpolicy_type *upt;
620
621 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) {
622 fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n");
623 return -1;
624 }
625 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
626 ptype = upt->type;
627 }
628
629 if (!xfrm_policy_filter_match(xpinfo, ptype))
630 return 0;
631
632 if (xb->offset > xb->size) {
633 fprintf(stderr, "Policy buffer overflow\n");
634 return -1;
635 }
636
637 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
638 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
639 new_n->nlmsg_flags = NLM_F_REQUEST;
640 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
641 new_n->nlmsg_seq = ++rth->seq;
642
643 xpid = NLMSG_DATA(new_n);
644 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
645 xpid->dir = xpinfo->dir;
646 xpid->index = xpinfo->index;
647
648 xb->offset += new_n->nlmsg_len;
649 xb->nlmsg_count ++;
650
651 return 0;
652 }
653
654 static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall)
655 {
656 char *selp = NULL;
657 struct rtnl_handle rth;
658
659 if (argc > 0)
660 filter.use = 1;
661 filter.xpinfo.sel.family = preferred_family;
662
663 while (argc > 0) {
664 if (strcmp(*argv, "dir") == 0) {
665 NEXT_ARG();
666 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
667
668 filter.dir_mask = XFRM_FILTER_MASK_FULL;
669
670 } else if (strcmp(*argv, "index") == 0) {
671 NEXT_ARG();
672 if (get_u32(&filter.xpinfo.index, *argv, 0))
673 invarg("\"INDEX\" is invalid", *argv);
674
675 filter.index_mask = XFRM_FILTER_MASK_FULL;
676
677 } else if (strcmp(*argv, "ptype") == 0) {
678 NEXT_ARG();
679 xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv);
680
681 filter.ptype_mask = XFRM_FILTER_MASK_FULL;
682
683 } else if (strcmp(*argv, "action") == 0) {
684 NEXT_ARG();
685 if (strcmp(*argv, "allow") == 0)
686 filter.xpinfo.action = XFRM_POLICY_ALLOW;
687 else if (strcmp(*argv, "block") == 0)
688 filter.xpinfo.action = XFRM_POLICY_BLOCK;
689 else
690 invarg("\"ACTION\" is invalid\n", *argv);
691
692 filter.action_mask = XFRM_FILTER_MASK_FULL;
693
694 } else if (strcmp(*argv, "priority") == 0) {
695 NEXT_ARG();
696 if (get_u32(&filter.xpinfo.priority, *argv, 0))
697 invarg("\"PRIORITY\" is invalid", *argv);
698
699 filter.priority_mask = XFRM_FILTER_MASK_FULL;
700
701 } else {
702 if (selp)
703 invarg("unknown", *argv);
704 selp = *argv;
705
706 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
707 if (preferred_family == AF_UNSPEC)
708 preferred_family = filter.xpinfo.sel.family;
709
710 }
711
712 argc--; argv++;
713 }
714
715 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
716 exit(1);
717
718 if (deleteall) {
719 struct xfrm_buffer xb;
720 char buf[NLMSG_DELETEALL_BUF_SIZE];
721 int i;
722
723 xb.buf = buf;
724 xb.size = sizeof(buf);
725 xb.rth = &rth;
726
727 for (i = 0; ; i++) {
728 xb.offset = 0;
729 xb.nlmsg_count = 0;
730
731 if (show_stats > 1)
732 fprintf(stderr, "Delete-all round = %d\n", i);
733
734 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
735 perror("Cannot send dump request");
736 exit(1);
737 }
738
739 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb, NULL, NULL) < 0) {
740 fprintf(stderr, "Delete-all terminated\n");
741 exit(1);
742 }
743 if (xb.nlmsg_count == 0) {
744 if (show_stats > 1)
745 fprintf(stderr, "Delete-all completed\n");
746 break;
747 }
748
749 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
750 perror("Failed to send delete-all request\n");
751 exit(1);
752 }
753 if (show_stats > 1)
754 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
755
756 xb.offset = 0;
757 xb.nlmsg_count = 0;
758 }
759 } else {
760 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
761 perror("Cannot send dump request");
762 exit(1);
763 }
764
765 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout, NULL, NULL) < 0) {
766 fprintf(stderr, "Dump terminated\n");
767 exit(1);
768 }
769 }
770
771 rtnl_close(&rth);
772
773 exit(0);
774 }
775
776 static int xfrm_policy_flush(int argc, char **argv)
777 {
778 struct rtnl_handle rth;
779 struct {
780 struct nlmsghdr n;
781 char buf[RTA_BUF_SIZE];
782 } req;
783 char *ptypep = NULL;
784 struct xfrm_userpolicy_type upt;
785
786 memset(&req, 0, sizeof(req));
787 memset(&upt, 0, sizeof(upt));
788
789 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
790 req.n.nlmsg_flags = NLM_F_REQUEST;
791 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
792
793 while (argc > 0) {
794 if (strcmp(*argv, "ptype") == 0) {
795 if (ptypep)
796 duparg("ptype", *argv);
797 ptypep = *argv;
798
799 NEXT_ARG();
800 xfrm_policy_ptype_parse(&upt.type, &argc, &argv);
801
802 filter.dir_mask = XFRM_FILTER_MASK_FULL;
803 } else
804 invarg("unknown", *argv);
805
806 argc--; argv++;
807 }
808
809 if (ptypep) {
810 addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE,
811 (void *)&upt, sizeof(upt));
812 }
813
814 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
815 exit(1);
816
817 if (show_stats > 1)
818 fprintf(stderr, "Flush policy\n");
819
820 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
821 exit(2);
822
823 rtnl_close(&rth);
824
825 return 0;
826 }
827
828 int do_xfrm_policy(int argc, char **argv)
829 {
830 if (argc < 1)
831 return xfrm_policy_list_or_deleteall(0, NULL, 0);
832
833 if (matches(*argv, "add") == 0)
834 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
835 argc-1, argv+1);
836 if (matches(*argv, "update") == 0)
837 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
838 argc-1, argv+1);
839 if (matches(*argv, "delete") == 0)
840 return xfrm_policy_delete(argc-1, argv+1);
841 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
842 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1);
843 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
844 || matches(*argv, "lst") == 0)
845 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0);
846 if (matches(*argv, "get") == 0)
847 return xfrm_policy_get(argc-1, argv+1);
848 if (matches(*argv, "flush") == 0)
849 return xfrm_policy_flush(argc-1, argv+1);
850 if (matches(*argv, "help") == 0)
851 usage();
852 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);
853 exit(-1);
854 }