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 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 [ SELECTOR | index INDEX ]\n");
59 fprintf(stderr, "Usage: ip xfrm policy { flush | list } [ dir DIR ] [ 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 ] [ dev DEV ]\n");
65 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
66 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n");
68 //fprintf(stderr, "DEV - device name(default=none)\n");
70 fprintf(stderr, "ACTION := [ allow | block ](default=allow)\n");
72 //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
74 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
75 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
76 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
78 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
79 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
80 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
82 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
83 fprintf(stderr, "XFRM_PROTO := [ ");
84 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
85 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
86 fprintf(stderr, "%s", strxf_xfrmproto(IPPROTO_COMP));
87 fprintf(stderr, " ]\n");
89 fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n");
90 //fprintf(stderr, "REQID - number(default=0)\n");
91 fprintf(stderr, "LEVEL := [ required | use ](default=required)\n");
96 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
101 if (strcmp(*argv, "in") == 0)
102 *dir = XFRM_POLICY_IN;
103 else if (strcmp(*argv, "out") == 0)
104 *dir = XFRM_POLICY_OUT;
105 else if (strcmp(*argv, "fwd") == 0)
106 *dir = XFRM_POLICY_FWD;
108 invarg("\"DIR\" is invalid", *argv);
116 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
117 int *argcp, char ***argvp)
120 char **argv = *argvp;
124 if (strcmp(*argv, "mode") == 0) {
126 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
127 } else if (strcmp(*argv, "reqid") == 0) {
129 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
130 } else if (strcmp(*argv, "level") == 0) {
133 if (strcmp(*argv, "required") == 0)
135 else if (strcmp(*argv, "use") == 0)
138 invarg("\"LEVEL\" is invalid\n", *argv);
142 PREV_ARG(); /* back track */
146 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
148 if (preferred_family == AF_UNSPEC)
149 preferred_family = tmpl->family;
166 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
168 struct rtnl_handle rth;
171 struct xfrm_userpolicy_info xpinfo;
172 char buf[RTA_BUF_SIZE];
176 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
179 memset(&req, 0, sizeof(req));
180 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
182 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
183 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
184 req.n.nlmsg_type = cmd;
185 req.xpinfo.sel.family = preferred_family;
187 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
188 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
189 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
190 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
193 if (strcmp(*argv, "dir") == 0) {
195 duparg("dir", *argv);
199 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
201 filter.dir_mask = XFRM_FILTER_MASK_FULL;
203 } else if (strcmp(*argv, "index") == 0) {
205 if (get_u32(&req.xpinfo.index, *argv, 0))
206 invarg("\"INDEX\" is invalid", *argv);
208 filter.index_mask = XFRM_FILTER_MASK_FULL;
210 } else if (strcmp(*argv, "action") == 0) {
212 if (strcmp(*argv, "allow") == 0)
213 req.xpinfo.action = XFRM_POLICY_ALLOW;
214 else if (strcmp(*argv, "block") == 0)
215 req.xpinfo.action = XFRM_POLICY_BLOCK;
217 invarg("\"action\" value is invalid\n", *argv);
219 filter.action_mask = XFRM_FILTER_MASK_FULL;
221 } else if (strcmp(*argv, "priority") == 0) {
223 if (get_u32(&req.xpinfo.priority, *argv, 0))
224 invarg("\"PRIORITY\" is invalid", *argv);
226 filter.priority_mask = XFRM_FILTER_MASK_FULL;
228 } else if (strcmp(*argv, "limit") == 0) {
230 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
231 } else if (strcmp(*argv, "tmpl") == 0) {
232 struct xfrm_user_tmpl *tmpl;
234 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
235 fprintf(stderr, "Too many tmpls: buffer overflow\n");
238 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
240 tmpl->family = preferred_family;
241 tmpl->aalgos = (~(__u32)0);
242 tmpl->ealgos = (~(__u32)0);
243 tmpl->calgos = (~(__u32)0);
246 xfrm_tmpl_parse(tmpl, &argc, &argv);
248 tmpls_len += sizeof(*tmpl);
251 duparg("unknown", *argv);
254 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
255 if (preferred_family == AF_UNSPEC)
256 preferred_family = req.xpinfo.sel.family;
263 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
268 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
269 (void *)tmpls_buf, tmpls_len);
272 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
275 if (req.xpinfo.sel.family == AF_UNSPEC)
276 req.xpinfo.sel.family = AF_INET;
278 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
286 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo)
291 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
294 if (filter.sel_src_mask) {
295 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
296 filter.sel_src_mask))
300 if (filter.sel_dst_mask) {
301 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
302 filter.sel_dst_mask))
306 if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
309 if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
312 if (filter.upspec_sport_mask) {
313 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
317 if (filter.upspec_dport_mask) {
318 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
322 if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
325 if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
328 if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
334 int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
337 FILE *fp = (FILE*)arg;
338 struct xfrm_userpolicy_info *xpinfo;
339 struct xfrm_user_polexpire *xpexp;
340 int len = n->nlmsg_len;
341 struct rtattr * tb[XFRMA_MAX+1];
344 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
345 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
346 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
347 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
348 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
352 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
353 xpexp = NLMSG_DATA(n);
354 xpinfo = &xpexp->pol;
356 len -= NLMSG_LENGTH(sizeof(*xpexp));
359 xpinfo = NLMSG_DATA(n);
361 len -= NLMSG_LENGTH(sizeof(*xpinfo));
365 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
369 if (!xfrm_policy_filter_match(xpinfo))
372 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
373 rta = XFRMPEXP_RTA(xpexp);
375 rta = XFRMP_RTA(xpinfo);
377 parse_rtattr(tb, XFRMA_MAX, rta, len);
379 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
380 fprintf(fp, "Deleted ");
381 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
382 fprintf(fp, "Expired ");
384 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
386 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
388 fprintf(fp, "hard %u", xpexp->hard);
389 fprintf(fp, "%s", _SL_);
398 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
401 struct rtnl_handle rth;
404 struct xfrm_userpolicy_id xpid;
410 memset(&req, 0, sizeof(req));
412 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
413 req.n.nlmsg_flags = NLM_F_REQUEST;
414 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
417 if (strcmp(*argv, "dir") == 0) {
419 duparg("dir", *argv);
423 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
425 } else if (strcmp(*argv, "index") == 0) {
427 duparg("index", *argv);
431 if (get_u32(&req.xpid.index, *argv, 0))
432 invarg("\"INDEX\" is invalid", *argv);
436 invarg("unknown", *argv);
439 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
440 if (preferred_family == AF_UNSPEC)
441 preferred_family = req.xpid.sel.family;
449 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
452 if (!selp && !indexp) {
453 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
457 duparg2("SELECTOR", "INDEX");
459 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
462 if (req.xpid.sel.family == AF_UNSPEC)
463 req.xpid.sel.family = AF_INET;
465 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf, NULL, NULL) < 0)
473 static int xfrm_policy_delete(int argc, char **argv)
475 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
478 static int xfrm_policy_get(int argc, char **argv)
480 char buf[NLMSG_BUF_SIZE];
481 struct nlmsghdr *n = (struct nlmsghdr *)buf;
483 memset(buf, 0, sizeof(buf));
485 xfrm_policy_get_or_delete(argc, argv, 0, n);
487 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
488 fprintf(stderr, "An error :-)\n");
496 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
497 * and store it to buffer.
499 static int xfrm_policy_keep(const struct sockaddr_nl *who,
503 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
504 struct rtnl_handle *rth = xb->rth;
505 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
506 int len = n->nlmsg_len;
507 struct nlmsghdr *new_n;
508 struct xfrm_userpolicy_id *xpid;
510 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
511 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
512 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
516 len -= NLMSG_LENGTH(sizeof(*xpinfo));
518 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
522 if (!xfrm_policy_filter_match(xpinfo))
525 if (xb->offset > xb->size) {
526 fprintf(stderr, "Flush buffer overflow\n");
530 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
531 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
532 new_n->nlmsg_flags = NLM_F_REQUEST;
533 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
534 new_n->nlmsg_seq = ++rth->seq;
536 xpid = NLMSG_DATA(new_n);
537 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
538 xpid->dir = xpinfo->dir;
539 xpid->index = xpinfo->index;
541 xb->offset += new_n->nlmsg_len;
547 static int xfrm_policy_list_or_flush(int argc, char **argv, int flush)
550 struct rtnl_handle rth;
554 filter.xpinfo.sel.family = preferred_family;
557 if (strcmp(*argv, "dir") == 0) {
559 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
561 filter.dir_mask = XFRM_FILTER_MASK_FULL;
563 } else if (strcmp(*argv, "index") == 0) {
565 if (get_u32(&filter.xpinfo.index, *argv, 0))
566 invarg("\"INDEX\" is invalid", *argv);
568 filter.index_mask = XFRM_FILTER_MASK_FULL;
570 } else if (strcmp(*argv, "action") == 0) {
572 if (strcmp(*argv, "allow") == 0)
573 filter.xpinfo.action = XFRM_POLICY_ALLOW;
574 else if (strcmp(*argv, "block") == 0)
575 filter.xpinfo.action = XFRM_POLICY_BLOCK;
577 invarg("\"ACTION\" is invalid\n", *argv);
579 filter.action_mask = XFRM_FILTER_MASK_FULL;
581 } else if (strcmp(*argv, "priority") == 0) {
583 if (get_u32(&filter.xpinfo.priority, *argv, 0))
584 invarg("\"PRIORITY\" is invalid", *argv);
586 filter.priority_mask = XFRM_FILTER_MASK_FULL;
590 invarg("unknown", *argv);
593 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
594 if (preferred_family == AF_UNSPEC)
595 preferred_family = filter.xpinfo.sel.family;
602 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
606 struct xfrm_buffer xb;
607 char buf[NLMSG_FLUSH_BUF_SIZE];
611 xb.size = sizeof(buf);
619 fprintf(stderr, "Flush round = %d\n", i);
621 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
622 perror("Cannot send dump request");
626 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb, NULL, NULL) < 0) {
627 fprintf(stderr, "Flush terminated\n");
630 if (xb.nlmsg_count == 0) {
632 fprintf(stderr, "Flush completed\n");
636 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
637 perror("Failed to send flush request\n");
641 fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count);
647 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
648 perror("Cannot send dump request");
652 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout, NULL, NULL) < 0) {
653 fprintf(stderr, "Dump terminated\n");
663 static int xfrm_policy_flush_all(void)
665 struct rtnl_handle rth;
670 memset(&req, 0, sizeof(req));
672 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
673 req.n.nlmsg_flags = NLM_F_REQUEST;
674 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
676 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
680 fprintf(stderr, "Flush all\n");
682 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
690 int do_xfrm_policy(int argc, char **argv)
693 return xfrm_policy_list_or_flush(0, NULL, 0);
695 if (matches(*argv, "add") == 0)
696 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
698 if (matches(*argv, "update") == 0)
699 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
701 if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0)
702 return xfrm_policy_delete(argc-1, argv+1);
703 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
704 || matches(*argv, "lst") == 0)
705 return xfrm_policy_list_or_flush(argc-1, argv+1, 0);
706 if (matches(*argv, "get") == 0)
707 return xfrm_policy_get(argc-1, argv+1);
708 if (matches(*argv, "flush") == 0) {
710 return xfrm_policy_flush_all();
712 return xfrm_policy_list_or_flush(argc-1, argv+1, 1);
714 if (matches(*argv, "help") == 0)
716 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);