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_DELETEALL_BUF_SIZE (4096-512)
39 #define NLMSG_DELETEALL_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 { deleteall | list } [ dir DIR ] [ SELECTOR ]\n");
60 fprintf(stderr, " [ index INDEX ] [ action ACTION ] [ priority PRIORITY ]\n");
61 fprintf(stderr, "Usage: ip xfrm policy flush\n");
62 fprintf(stderr, "DIR := [ in | out | fwd ]\n");
64 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
66 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
67 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n");
69 //fprintf(stderr, "DEV - device name(default=none)\n");
71 fprintf(stderr, "ACTION := [ allow | block ](default=allow)\n");
73 //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
75 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
76 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
77 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] NUMBER ]\n");
79 fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] | [ tmpl TMPL ]\n");
80 fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n");
81 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
83 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
84 fprintf(stderr, "XFRM_PROTO := [ ");
85 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
86 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
87 fprintf(stderr, "%s", strxf_xfrmproto(IPPROTO_COMP));
88 fprintf(stderr, " ]\n");
90 fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n");
91 //fprintf(stderr, "REQID - number(default=0)\n");
92 fprintf(stderr, "LEVEL := [ required | use ](default=required)\n");
97 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
100 char **argv = *argvp;
102 if (strcmp(*argv, "in") == 0)
103 *dir = XFRM_POLICY_IN;
104 else if (strcmp(*argv, "out") == 0)
105 *dir = XFRM_POLICY_OUT;
106 else if (strcmp(*argv, "fwd") == 0)
107 *dir = XFRM_POLICY_FWD;
109 invarg("\"DIR\" is invalid", *argv);
117 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
118 int *argcp, char ***argvp)
121 char **argv = *argvp;
125 if (strcmp(*argv, "mode") == 0) {
127 xfrm_mode_parse(&tmpl->mode, &argc, &argv);
128 } else if (strcmp(*argv, "reqid") == 0) {
130 xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
131 } else if (strcmp(*argv, "level") == 0) {
134 if (strcmp(*argv, "required") == 0)
136 else if (strcmp(*argv, "use") == 0)
139 invarg("\"LEVEL\" is invalid\n", *argv);
143 PREV_ARG(); /* back track */
147 xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
149 if (preferred_family == AF_UNSPEC)
150 preferred_family = tmpl->family;
167 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
169 struct rtnl_handle rth;
172 struct xfrm_userpolicy_info xpinfo;
173 char buf[RTA_BUF_SIZE];
177 char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
180 memset(&req, 0, sizeof(req));
181 memset(&tmpls_buf, 0, sizeof(tmpls_buf));
183 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo));
184 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
185 req.n.nlmsg_type = cmd;
186 req.xpinfo.sel.family = preferred_family;
188 req.xpinfo.lft.soft_byte_limit = XFRM_INF;
189 req.xpinfo.lft.hard_byte_limit = XFRM_INF;
190 req.xpinfo.lft.soft_packet_limit = XFRM_INF;
191 req.xpinfo.lft.hard_packet_limit = XFRM_INF;
194 if (strcmp(*argv, "dir") == 0) {
196 duparg("dir", *argv);
200 xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
202 filter.dir_mask = XFRM_FILTER_MASK_FULL;
204 } else if (strcmp(*argv, "index") == 0) {
206 if (get_u32(&req.xpinfo.index, *argv, 0))
207 invarg("\"INDEX\" is invalid", *argv);
209 filter.index_mask = XFRM_FILTER_MASK_FULL;
211 } else if (strcmp(*argv, "action") == 0) {
213 if (strcmp(*argv, "allow") == 0)
214 req.xpinfo.action = XFRM_POLICY_ALLOW;
215 else if (strcmp(*argv, "block") == 0)
216 req.xpinfo.action = XFRM_POLICY_BLOCK;
218 invarg("\"action\" value is invalid\n", *argv);
220 filter.action_mask = XFRM_FILTER_MASK_FULL;
222 } else if (strcmp(*argv, "priority") == 0) {
224 if (get_u32(&req.xpinfo.priority, *argv, 0))
225 invarg("\"PRIORITY\" is invalid", *argv);
227 filter.priority_mask = XFRM_FILTER_MASK_FULL;
229 } else if (strcmp(*argv, "limit") == 0) {
231 xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
232 } else if (strcmp(*argv, "tmpl") == 0) {
233 struct xfrm_user_tmpl *tmpl;
235 if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
236 fprintf(stderr, "Too many tmpls: buffer overflow\n");
239 tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
241 tmpl->family = preferred_family;
242 tmpl->aalgos = (~(__u32)0);
243 tmpl->ealgos = (~(__u32)0);
244 tmpl->calgos = (~(__u32)0);
247 xfrm_tmpl_parse(tmpl, &argc, &argv);
249 tmpls_len += sizeof(*tmpl);
252 duparg("unknown", *argv);
255 xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
256 if (preferred_family == AF_UNSPEC)
257 preferred_family = req.xpinfo.sel.family;
264 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
269 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
270 (void *)tmpls_buf, tmpls_len);
273 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
276 if (req.xpinfo.sel.family == AF_UNSPEC)
277 req.xpinfo.sel.family = AF_INET;
279 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
287 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo)
292 if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
295 if (filter.sel_src_mask) {
296 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
297 filter.sel_src_mask))
301 if (filter.sel_dst_mask) {
302 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
303 filter.sel_dst_mask))
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(const struct sockaddr_nl *who, struct nlmsghdr *n,
338 struct rtattr * tb[XFRMA_MAX+1];
340 struct xfrm_userpolicy_info *xpinfo = NULL;
341 struct xfrm_user_polexpire *xpexp = NULL;
342 struct xfrm_userpolicy_id *xpid = NULL;
343 FILE *fp = (FILE*)arg;
344 int len = n->nlmsg_len;
346 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY &&
347 n->nlmsg_type != XFRM_MSG_DELPOLICY &&
348 n->nlmsg_type != XFRM_MSG_UPDPOLICY &&
349 n->nlmsg_type != XFRM_MSG_POLEXPIRE) {
350 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
351 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
355 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
356 xpid = NLMSG_DATA(n);
357 len -= NLMSG_LENGTH(sizeof(*xpid));
358 } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
359 xpexp = NLMSG_DATA(n);
360 xpinfo = &xpexp->pol;
361 len -= NLMSG_LENGTH(sizeof(*xpexp));
364 xpinfo = NLMSG_DATA(n);
365 len -= NLMSG_LENGTH(sizeof(*xpinfo));
369 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
373 if (xpinfo && !xfrm_policy_filter_match(xpinfo))
376 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
377 fprintf(fp, "Deleted ");
378 else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY)
379 fprintf(fp, "Updated ");
380 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
381 fprintf(fp, "Expired ");
383 if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
384 rta = XFRMPID_RTA(xpid);
385 else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE)
386 rta = XFRMPEXP_RTA(xpexp);
388 rta = XFRMP_RTA(xpinfo);
390 parse_rtattr(tb, XFRMA_MAX, rta, len);
392 if (n->nlmsg_type == XFRM_MSG_DELPOLICY) {
393 //xfrm_policy_id_print();
394 if (!tb[XFRMA_POLICY]) {
395 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n");
398 if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) {
399 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
402 xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]);
405 xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL);
407 if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
409 fprintf(fp, "hard %u", xpexp->hard);
410 fprintf(fp, "%s", _SL_);
420 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
423 struct rtnl_handle rth;
426 struct xfrm_userpolicy_id xpid;
432 memset(&req, 0, sizeof(req));
434 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
435 req.n.nlmsg_flags = NLM_F_REQUEST;
436 req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
439 if (strcmp(*argv, "dir") == 0) {
441 duparg("dir", *argv);
445 xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
447 } else if (strcmp(*argv, "index") == 0) {
449 duparg("index", *argv);
453 if (get_u32(&req.xpid.index, *argv, 0))
454 invarg("\"INDEX\" is invalid", *argv);
458 invarg("unknown", *argv);
461 xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
462 if (preferred_family == AF_UNSPEC)
463 preferred_family = req.xpid.sel.family;
471 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
474 if (!selp && !indexp) {
475 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
479 duparg2("SELECTOR", "INDEX");
481 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
484 if (req.xpid.sel.family == AF_UNSPEC)
485 req.xpid.sel.family = AF_INET;
487 if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf, NULL, NULL) < 0)
495 static int xfrm_policy_delete(int argc, char **argv)
497 return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
500 static int xfrm_policy_get(int argc, char **argv)
502 char buf[NLMSG_BUF_SIZE];
503 struct nlmsghdr *n = (struct nlmsghdr *)buf;
505 memset(buf, 0, sizeof(buf));
507 xfrm_policy_get_or_delete(argc, argv, 0, n);
509 if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
510 fprintf(stderr, "An error :-)\n");
518 * With an existing policy of nlmsg, make new nlmsg for deleting the policy
519 * and store it to buffer.
521 static int xfrm_policy_keep(const struct sockaddr_nl *who,
525 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
526 struct rtnl_handle *rth = xb->rth;
527 struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
528 int len = n->nlmsg_len;
529 struct nlmsghdr *new_n;
530 struct xfrm_userpolicy_id *xpid;
532 if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
533 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
534 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
538 len -= NLMSG_LENGTH(sizeof(*xpinfo));
540 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
544 if (!xfrm_policy_filter_match(xpinfo))
547 if (xb->offset > xb->size) {
548 fprintf(stderr, "Policy buffer overflow\n");
552 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
553 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
554 new_n->nlmsg_flags = NLM_F_REQUEST;
555 new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
556 new_n->nlmsg_seq = ++rth->seq;
558 xpid = NLMSG_DATA(new_n);
559 memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
560 xpid->dir = xpinfo->dir;
561 xpid->index = xpinfo->index;
563 xb->offset += new_n->nlmsg_len;
569 static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall)
572 struct rtnl_handle rth;
576 filter.xpinfo.sel.family = preferred_family;
579 if (strcmp(*argv, "dir") == 0) {
581 xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
583 filter.dir_mask = XFRM_FILTER_MASK_FULL;
585 } else if (strcmp(*argv, "index") == 0) {
587 if (get_u32(&filter.xpinfo.index, *argv, 0))
588 invarg("\"INDEX\" is invalid", *argv);
590 filter.index_mask = XFRM_FILTER_MASK_FULL;
592 } else if (strcmp(*argv, "action") == 0) {
594 if (strcmp(*argv, "allow") == 0)
595 filter.xpinfo.action = XFRM_POLICY_ALLOW;
596 else if (strcmp(*argv, "block") == 0)
597 filter.xpinfo.action = XFRM_POLICY_BLOCK;
599 invarg("\"ACTION\" is invalid\n", *argv);
601 filter.action_mask = XFRM_FILTER_MASK_FULL;
603 } else if (strcmp(*argv, "priority") == 0) {
605 if (get_u32(&filter.xpinfo.priority, *argv, 0))
606 invarg("\"PRIORITY\" is invalid", *argv);
608 filter.priority_mask = XFRM_FILTER_MASK_FULL;
612 invarg("unknown", *argv);
615 xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
616 if (preferred_family == AF_UNSPEC)
617 preferred_family = filter.xpinfo.sel.family;
624 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
628 struct xfrm_buffer xb;
629 char buf[NLMSG_DELETEALL_BUF_SIZE];
633 xb.size = sizeof(buf);
641 fprintf(stderr, "Delete-all round = %d\n", i);
643 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
644 perror("Cannot send dump request");
648 if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb, NULL, NULL) < 0) {
649 fprintf(stderr, "Delete-all terminated\n");
652 if (xb.nlmsg_count == 0) {
654 fprintf(stderr, "Delete-all completed\n");
658 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
659 perror("Failed to send delete-all request\n");
663 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
669 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
670 perror("Cannot send dump request");
674 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout, NULL, NULL) < 0) {
675 fprintf(stderr, "Dump terminated\n");
685 static int xfrm_policy_flush(void)
687 struct rtnl_handle rth;
692 memset(&req, 0, sizeof(req));
694 req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
695 req.n.nlmsg_flags = NLM_F_REQUEST;
696 req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
698 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
702 fprintf(stderr, "Flush policy\n");
704 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
712 int do_xfrm_policy(int argc, char **argv)
715 return xfrm_policy_list_or_deleteall(0, NULL, 0);
717 if (matches(*argv, "add") == 0)
718 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
720 if (matches(*argv, "update") == 0)
721 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
723 if (matches(*argv, "delete") == 0)
724 return xfrm_policy_delete(argc-1, argv+1);
725 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
726 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1);
727 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
728 || matches(*argv, "lst") == 0)
729 return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0);
730 if (matches(*argv, "get") == 0)
731 return xfrm_policy_get(argc-1, argv+1);
732 if (matches(*argv, "flush") == 0)
733 return xfrm_policy_flush();
734 if (matches(*argv, "help") == 0)
736 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);