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