4 * Copyright (C)2004 USAGI/WIDE Project
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.
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.
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
25 * Masahide NAKAMURA @USAGI
32 #include <linux/netlink.h>
33 #include <linux/xfrm.h>
36 #include "ip_common.h"
38 //#define NLMSG_FLUSH_BUF_SIZE (4096-512)
39 #define NLMSG_FLUSH_BUF_SIZE 8192
42 * Receiving buffer defines:
44 * data = struct xfrm_userpolicy_info
46 * data = struct xfrm_user_tmpl[]
48 #define NLMSG_BUF_SIZE 4096
49 #define RTA_BUF_SIZE 2048
50 #define XFRM_TMPLS_BUF_SIZE 1024
52 static void usage(void) __attribute__((noreturn));
54 static void usage(void)
56 fprintf(stderr, "Usage: ip xfrm policy { add | update } dir DIR sel SELECTOR [ index INDEX ] \n");
57 fprintf(stderr, " [ action ACTION ] [ priority PRIORITY ] [ LIMIT-LIST ] [ TMPL-LIST ]\n");
58 fprintf(stderr, "Usage: ip xfrm policy { delete | get } dir DIR [ sel SELECTOR | index INDEX ]\n");
59 fprintf(stderr, "Usage: ip xfrm policy { flush | list } [ dir DIR ] [ sel SELECTOR ]\n");
60 fprintf(stderr, " [ index INDEX ] [ action ACTION ] [ priority PRIORITY ]\n");
61 fprintf(stderr, "DIR := [ in | out | fwd ]\n");
63 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ upspec UPSPEC ] [ dev DEV ]\n");
65 fprintf(stderr, "UPSPEC := proto PROTO [ sport PORT ] [ dport PORT ]\n");
67 //fprintf(stderr, "DEV - device name(default=none)\n");
69 fprintf(stderr, "ACTION := [ allow | block ](default=allow)\n");
71 //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
73 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
74 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
75 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
77 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
78 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
79 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
81 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | ipcomp ]\n");
82 fprintf(stderr, "XFRM_PROTO := [ ");
83 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ESP));
84 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_AH));
85 fprintf(stderr, "%s", strxf_proto(IPPROTO_COMP));
86 fprintf(stderr, " ]\n");
88 fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n");
89 //fprintf(stderr, "REQID - number(default=0)\n");
90 fprintf(stderr, "LEVEL := [ required | use ](default=required)\n");
95 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
100 if (strcmp(*argv, "in") == 0)
101 *dir = XFRM_POLICY_IN;
102 else if (strcmp(*argv, "out") == 0)
103 *dir = XFRM_POLICY_OUT;
104 else if (strcmp(*argv, "fwd") == 0)
105 *dir = XFRM_POLICY_FWD;
107 invarg("\"DIR\" is invalid", *argv);
115 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
116 int *argcp, char ***argvp)
119 char **argv = *argvp;
123 if (strcmp(*argv, "mode") == 0) {
125 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
126 } else if (strcmp(*argv, "reqid") == 0) {
128 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
129 } else if (strcmp(*argv, "level") == 0) {
132 if (strcmp(*argv, "required") == 0)
134 else if (strcmp(*argv, "use") == 0)
137 invarg("\"level\" value is invalid\n", *argv);
141 PREV_ARG(); /* back track */
145 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
147 if (preferred_family == AF_UNSPEC)
148 preferred_family = tmpl->family;
165 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
167 struct rtnl_handle rth;
170 struct xfrm_userpolicy_info xpinfo;
171 char buf[RTA_BUF_SIZE];
174 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
177 memset(&req, 0, sizeof(req));
178 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
180 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
181 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
182 req.n.nlmsg_type = cmd;
183 req.xpinfo.sel.family = preferred_family;
185 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
186 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
187 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
188 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
191 if (strcmp(*argv, "dir") == 0) {
193 duparg("dir", *argv);
197 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
199 filter.dir_mask = XFRM_FILTER_MASK_FULL;
201 } else if (strcmp(*argv, "sel") == 0) {
203 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
204 if (preferred_family == AF_UNSPEC)
205 preferred_family = req.xpinfo.sel.family;
207 } else if (strcmp(*argv, "index") == 0) {
209 if (get_u32(&req.xpinfo.index, *argv, 0))
210 invarg("\"INDEX\" is invalid", *argv);
212 filter.index_mask = XFRM_FILTER_MASK_FULL;
214 } else if (strcmp(*argv, "action") == 0) {
216 if (strcmp(*argv, "allow") == 0)
217 req.xpinfo.action = XFRM_POLICY_ALLOW;
218 else if (strcmp(*argv, "block") == 0)
219 req.xpinfo.action = XFRM_POLICY_BLOCK;
221 invarg("\"action\" value is invalid\n", *argv);
223 filter.action_mask = XFRM_FILTER_MASK_FULL;
225 } else if (strcmp(*argv, "priority") == 0) {
227 if (get_u32(&req.xpinfo.priority, *argv, 0))
228 invarg("\"PRIORITY\" is invalid", *argv);
230 filter.priority_mask = XFRM_FILTER_MASK_FULL;
232 } else if (strcmp(*argv, "limit") == 0) {
234 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
235 } else if (strcmp(*argv, "tmpl") == 0) {
236 struct xfrm_user_tmpl *tmpl;
238 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
239 fprintf(stderr, "Too many tmpls: buffer overflow\n");
242 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
244 tmpl->family = preferred_family;
245 tmpl->aalgos = (~(__u32)0);
246 tmpl->ealgos = (~(__u32)0);
247 tmpl->calgos = (~(__u32)0);
250 xfrm_tmpl_parse(tmpl, &argc, &argv);
252 tmpls_len += sizeof(*tmpl);
254 invarg("unknown", *argv);
260 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
265 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
266 (void *)tmpls_buf, tmpls_len);
269 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
272 if (req.xpinfo.sel.family == AF_UNSPEC)
273 req.xpinfo.sel.family = AF_INET;
275 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
283 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo)
288 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
291 if (filter.sel_src_mask) {
292 if (memcmp(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
293 filter.sel_src_mask) != 0)
295 if (xpinfo->sel.prefixlen_s != filter.xpinfo.sel.prefixlen_s)
299 if (filter.sel_dst_mask) {
300 if (memcmp(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
301 filter.sel_dst_mask) != 0)
303 if (xpinfo->sel.prefixlen_d != filter.xpinfo.sel.prefixlen_d)
307 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
310 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
313 if (filter.upspec_sport_mask) {
314 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
318 if (filter.upspec_dport_mask) {
319 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
323 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
326 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
329 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
335 int xfrm_policy_print(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
337 FILE *fp = (FILE*)arg;
338 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
339 int len = n->nlmsg_len;
340 struct rtattr * tb[XFRM_MAX_DEPTH];
343 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
344 n->nlmsg_type != XFRM_MSG_DELPOLICY) {
345 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
346 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
350 len -= NLMSG_LENGTH(sizeof(*xpinfo));
352 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
356 if (!xfrm_policy_filter_match(xpinfo))
359 memset(tb, 0, sizeof(tb));
360 ntb = parse_rtattr_byindex(tb, XFRM_MAX_DEPTH, XFRMP_RTA(xpinfo), len);
362 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
363 fprintf(fp, "Deleted ");
366 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, NULL);
370 switch (xpinfo->dir) {
374 case XFRM_POLICY_OUT:
377 case XFRM_POLICY_FWD:
381 fprintf(fp, "%d", xpinfo->dir);
386 fprintf(fp, "action ");
387 switch (xpinfo->action) {
388 case XFRM_POLICY_ALLOW:
389 fprintf(fp, "allow");
391 case XFRM_POLICY_BLOCK:
392 fprintf(fp, "block");
395 fprintf(fp, "%d", xpinfo->action);
400 fprintf(fp, "index %u ", xpinfo->index);
401 fprintf(fp, "priority %u ", xpinfo->priority);
402 if (show_stats > 0) {
403 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
404 fprintf(fp, "flags 0x%s", strxf_flags(xpinfo->flags));
409 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, "\t");
411 xfrm_xfrma_print(tb, ntb, xpinfo->sel.family, fp, "\t");
416 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
419 struct rtnl_handle rth;
422 struct xfrm_userpolicy_id xpid;
428 memset(&req, 0, sizeof(req));
430 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
431 req.n.nlmsg_flags = NLM_F_REQUEST;
432 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
435 if (strcmp(*argv, "dir") == 0) {
437 duparg("dir", *argv);
441 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
443 } else if (strcmp(*argv, "sel") == 0) {
445 duparg("sel", *argv);
449 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
450 if (preferred_family == AF_UNSPEC)
451 preferred_family = req.xpid.sel.family;
453 } else if (strcmp(*argv, "index") == 0) {
455 duparg("index", *argv);
459 if (get_u32(&req.xpid.index, *argv, 0))
460 invarg("\"INDEX\" is invalid", *argv);
463 invarg("unknown", *argv);
469 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
472 if (!selp && !indexp) {
473 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
477 duparg2("SELECTOR", "INDEX");
479 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
482 if (req.xpid.sel.family == AF_UNSPEC)
483 req.xpid.sel.family = AF_INET;
485 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf, NULL, NULL) < 0)
493 static int xfrm_policy_delete(int argc, char **argv)
495 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
498 static int xfrm_policy_get(int argc, char **argv)
500 char buf[NLMSG_BUF_SIZE];
501 struct nlmsghdr *n = (struct nlmsghdr *)buf;
503 memset(buf, 0, sizeof(buf));
505 xfrm_policy_get_or_delete(argc, argv, 0, n);
507 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
508 fprintf(stderr, "An error :-)\n");
516 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
517 * and store it to buffer.
519 int xfrm_policy_keep(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
521 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
522 struct rtnl_handle *rth = xb->rth;
523 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
524 int len = n->nlmsg_len;
525 struct nlmsghdr *new_n;
526 struct xfrm_userpolicy_id *xpid;
528 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
529 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
530 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
534 len -= NLMSG_LENGTH(sizeof(*xpinfo));
536 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
540 if (!xfrm_policy_filter_match(xpinfo))
543 if (xb->offset > xb->size) {
544 fprintf(stderr, "Flush buffer overflow\n");
548 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
549 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
550 new_n->nlmsg_flags = NLM_F_REQUEST;
551 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
552 new_n->nlmsg_seq = ++rth->seq;
554 xpid = NLMSG_DATA(new_n);
555 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
556 xpid->dir = xpinfo->dir;
557 xpid->index = xpinfo->index;
559 xb->offset += new_n->nlmsg_len;
565 static int xfrm_policy_list_or_flush(int argc, char **argv, int flush)
567 struct rtnl_handle rth;
571 filter.xpinfo.sel.family = preferred_family;
574 if (strcmp(*argv, "dir") == 0) {
576 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
578 filter.dir_mask = XFRM_FILTER_MASK_FULL;
580 } else if (strcmp(*argv, "sel") == 0) {
582 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
583 if (preferred_family == AF_UNSPEC)
584 preferred_family = filter.xpinfo.sel.family;
586 } else if (strcmp(*argv, "index") == 0) {
588 if (get_u32(&filter.xpinfo.index, *argv, 0))
589 invarg("\"INDEX\" is invalid", *argv);
591 filter.index_mask = XFRM_FILTER_MASK_FULL;
593 } else if (strcmp(*argv, "action") == 0) {
595 if (strcmp(*argv, "allow") == 0)
596 filter.xpinfo.action = XFRM_POLICY_ALLOW;
597 else if (strcmp(*argv, "block") == 0)
598 filter.xpinfo.action = XFRM_POLICY_BLOCK;
600 invarg("\"action\" value is invalid\n", *argv);
602 filter.action_mask = XFRM_FILTER_MASK_FULL;
604 } else if (strcmp(*argv, "priority") == 0) {
606 if (get_u32(&filter.xpinfo.priority, *argv, 0))
607 invarg("\"PRIORITY\" is invalid", *argv);
609 filter.priority_mask = XFRM_FILTER_MASK_FULL;
612 invarg("unknown", *argv);
617 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
621 struct xfrm_buffer xb;
622 char buf[NLMSG_FLUSH_BUF_SIZE];
626 xb.size = sizeof(buf);
634 fprintf(stderr, "Flush round = %d\n", i);
636 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
637 perror("Cannot send dump request");
641 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb, NULL, NULL) < 0) {
642 fprintf(stderr, "Flush terminated\n");
645 if (xb.nlmsg_count == 0) {
647 fprintf(stderr, "Flush completed\n");
651 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
652 perror("Failed to send flush request\n");
656 fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count);
662 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
663 perror("Cannot send dump request");
667 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout, NULL, NULL) < 0) {
668 fprintf(stderr, "Dump terminated\n");
678 int do_xfrm_policy(int argc, char **argv)
681 return xfrm_policy_list_or_flush(0, NULL, 0);
683 if (matches(*argv, "add") == 0)
684 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
686 if (matches(*argv, "update") == 0)
687 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
689 if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0)
690 return xfrm_policy_delete(argc-1, argv+1);
691 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
692 || matches(*argv, "lst") == 0)
693 return xfrm_policy_list_or_flush(argc-1, argv+1, 0);
694 if (matches(*argv, "get") == 0)
695 return xfrm_policy_get(argc-1, argv+1);
696 if (matches(*argv, "flush") == 0)
697 return xfrm_policy_list_or_flush(argc-1, argv+1, 1);
698 if (matches(*argv, "help") == 0)
700 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);