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