]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/xfrm_policy.c
Import patch iproute2.117
[lisovros/iproute2_canprio.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, 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 "utils.h"
35 #include "xfrm.h"
36 #include "ip_common.h"
37
38 //#define NLMSG_FLUSH_BUF_SIZE (4096-512)
39 #define NLMSG_FLUSH_BUF_SIZE 8192
40
41 /*
42  * Receiving buffer defines:
43  * nlmsg
44  *   data = struct xfrm_userpolicy_info
45  *   rtattr
46  *     data = struct xfrm_user_tmpl[]
47  */
48 #define NLMSG_BUF_SIZE 4096
49 #define RTA_BUF_SIZE 2048
50 #define XFRM_TMPLS_BUF_SIZE 1024
51
52 static void usage(void) __attribute__((noreturn));
53
54 static void usage(void)
55 {
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");
62
63         fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
64
65         fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
66         fprintf(stderr, "                        [ type NUMBER ] [ code NUMBER ] ]\n");
67
68         //fprintf(stderr, "DEV - device name(default=none)\n");
69
70         fprintf(stderr, "ACTION := [ allow | block ](default=allow)\n");
71
72         //fprintf(stderr, "PRIORITY - priority value(default=0)\n");
73
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");
77
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");
81
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");
88
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");
92
93         exit(-1);
94 }
95
96 static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp)
97 {
98         int argc = *argcp;
99         char **argv = *argvp;
100
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;
107         else
108                 invarg("\"DIR\" is invalid", *argv);
109
110         *argcp = argc;
111         *argvp = argv;
112
113         return 0;
114 }
115
116 static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl,
117                            int *argcp, char ***argvp)
118 {
119         int argc = *argcp;
120         char **argv = *argvp;
121         char *idp = NULL;
122
123         while (1) {
124                 if (strcmp(*argv, "mode") == 0) {
125                         NEXT_ARG();
126                         xfrm_mode_parse(&tmpl->mode,  &argc, &argv);
127                 } else if (strcmp(*argv, "reqid") == 0) {
128                         NEXT_ARG();
129                         xfrm_reqid_parse(&tmpl->reqid, &argc, &argv);
130                 } else if (strcmp(*argv, "level") == 0) {
131                         NEXT_ARG();
132
133                         if (strcmp(*argv, "required") == 0)
134                                 tmpl->optional = 0;
135                         else if (strcmp(*argv, "use") == 0)
136                                 tmpl->optional = 1;
137                         else
138                                 invarg("\"LEVEL\" is invalid\n", *argv);
139
140                 } else {
141                         if (idp) {
142                                 PREV_ARG(); /* back track */
143                                 break;
144                         }
145                         idp = *argv;
146                         xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family,
147                                       0, &argc, &argv);
148                         if (preferred_family == AF_UNSPEC)
149                                 preferred_family = tmpl->family;
150                 }
151
152                 if (!NEXT_ARG_OK())
153                         break;
154
155                 NEXT_ARG();
156         }
157         if (argc == *argcp)
158                 missarg("TMPL");
159
160         *argcp = argc;
161         *argvp = argv;
162
163         return 0;
164 }
165
166 static int xfrm_policy_modify(int cmd, unsigned flags, int argc, char **argv)
167 {
168         struct rtnl_handle rth;
169         struct {
170                 struct nlmsghdr                 n;
171                 struct xfrm_userpolicy_info     xpinfo;
172                 char                            buf[RTA_BUF_SIZE];
173         } req;
174         char *dirp = NULL;
175         char *selp = NULL;
176         char tmpls_buf[XFRM_TMPLS_BUF_SIZE];
177         int tmpls_len = 0;
178
179         memset(&req, 0, sizeof(req));
180         memset(&tmpls_buf, 0, sizeof(tmpls_buf));
181
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;
186
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;
191
192         while (argc > 0) {
193                 if (strcmp(*argv, "dir") == 0) {
194                         if (dirp)
195                                 duparg("dir", *argv);
196                         dirp = *argv;
197
198                         NEXT_ARG();
199                         xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv);
200
201                         filter.dir_mask = XFRM_FILTER_MASK_FULL;
202
203                 } else if (strcmp(*argv, "index") == 0) {
204                         NEXT_ARG();
205                         if (get_u32(&req.xpinfo.index, *argv, 0))
206                                 invarg("\"INDEX\" is invalid", *argv);
207
208                         filter.index_mask = XFRM_FILTER_MASK_FULL;
209
210                 } else if (strcmp(*argv, "action") == 0) {
211                         NEXT_ARG();
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;
216                         else
217                                 invarg("\"action\" value is invalid\n", *argv);
218
219                         filter.action_mask = XFRM_FILTER_MASK_FULL;
220
221                 } else if (strcmp(*argv, "priority") == 0) {
222                         NEXT_ARG();
223                         if (get_u32(&req.xpinfo.priority, *argv, 0))
224                                 invarg("\"PRIORITY\" is invalid", *argv);
225
226                         filter.priority_mask = XFRM_FILTER_MASK_FULL;
227
228                 } else if (strcmp(*argv, "limit") == 0) {
229                         NEXT_ARG();
230                         xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv);
231                 } else if (strcmp(*argv, "tmpl") == 0) {
232                         struct xfrm_user_tmpl *tmpl;
233
234                         if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) {
235                                 fprintf(stderr, "Too many tmpls: buffer overflow\n");
236                                 exit(1);
237                         }
238                         tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len);
239
240                         tmpl->family = preferred_family;
241                         tmpl->aalgos = (~(__u32)0);
242                         tmpl->ealgos = (~(__u32)0);
243                         tmpl->calgos = (~(__u32)0);
244
245                         NEXT_ARG();
246                         xfrm_tmpl_parse(tmpl, &argc, &argv);
247
248                         tmpls_len += sizeof(*tmpl);
249                 } else {
250                         if (selp)
251                                 duparg("unknown", *argv);
252                         selp = *argv;
253
254                         xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv);
255                         if (preferred_family == AF_UNSPEC)
256                                 preferred_family = req.xpinfo.sel.family;
257                 }
258
259                 argc--; argv++;
260         }
261
262         if (!dirp) {
263                 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
264                 exit(1);
265         }
266
267         if (tmpls_len > 0) {
268                 addattr_l(&req.n, sizeof(req), XFRMA_TMPL,
269                           (void *)tmpls_buf, tmpls_len);
270         }
271
272         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
273                 exit(1);
274
275         if (req.xpinfo.sel.family == AF_UNSPEC)
276                 req.xpinfo.sel.family = AF_INET;
277
278         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
279                 exit(2);
280
281         rtnl_close(&rth);
282
283         return 0;
284 }
285
286 static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo)
287 {
288         if (!filter.use)
289                 return 1;
290
291         if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask)
292                 return 0;
293
294         if (filter.sel_src_mask) {
295                 if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr,
296                                     filter.sel_src_mask))
297                         return 0;
298         }
299
300         if (filter.sel_dst_mask) {
301                 if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr,
302                                     filter.sel_dst_mask))
303                         return 0;
304         }
305
306         if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask)
307                 return 0;
308
309         if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask)
310                 return 0;
311
312         if (filter.upspec_sport_mask) {
313                 if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask)
314                         return 0;
315         }
316
317         if (filter.upspec_dport_mask) {
318                 if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask)
319                         return 0;
320         }
321
322         if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask)
323                 return 0;
324
325         if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask)
326                 return 0;
327
328         if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask)
329                 return 0;
330
331         return 1;
332 }
333
334 static int xfrm_policy_print(const struct sockaddr_nl *who, 
335                              struct nlmsghdr *n, void *arg)
336 {
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];
341         int ntb;
342
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);
347                 return 0;
348         }
349
350         len -= NLMSG_LENGTH(sizeof(*xpinfo));
351         if (len < 0) {
352                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
353                 return -1;
354         }
355
356         if (!xfrm_policy_filter_match(xpinfo))
357                 return 0;
358
359         memset(tb, 0, sizeof(tb));
360         ntb = parse_rtattr_byindex(tb, XFRM_MAX_DEPTH, XFRMP_RTA(xpinfo), len);
361
362         if (n->nlmsg_type == XFRM_MSG_DELPOLICY)
363                 fprintf(fp, "Deleted ");
364
365         xfrm_selector_print(&xpinfo->sel, preferred_family, fp, NULL);
366
367         fprintf(fp, "\t");
368         fprintf(fp, "dir ");
369         switch (xpinfo->dir) {
370         case XFRM_POLICY_IN:
371                 fprintf(fp, "in");
372                 break;
373         case XFRM_POLICY_OUT:
374                 fprintf(fp, "out");
375                 break;
376         case XFRM_POLICY_FWD:
377                 fprintf(fp, "fwd");
378                 break;
379         default:
380                 fprintf(fp, "%u", xpinfo->dir);
381                 break;
382         }
383         fprintf(fp, " ");
384
385         switch (xpinfo->action) {
386         case XFRM_POLICY_ALLOW:
387                 if (show_stats > 0)
388                         fprintf(fp, "action allow ");
389                 break;
390         case XFRM_POLICY_BLOCK:
391                 fprintf(fp, "action block ");
392                 break;
393         default:
394                 fprintf(fp, "action %u ", xpinfo->action);
395                 break;
396         }
397
398         if (show_stats)
399                 fprintf(fp, "index %u ", xpinfo->index);
400         fprintf(fp, "priority %u ", xpinfo->priority);
401         if (show_stats > 0) {
402                 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
403                 fprintf(fp, "flag 0x%s", strxf_mask8(xpinfo->flags));
404         }
405         fprintf(fp, "%s", _SL_);
406
407         if (show_stats > 0)
408                 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, "\t");
409
410         xfrm_xfrma_print(tb, ntb, xpinfo->sel.family, fp, "\t");
411
412         if (oneline)
413                 fprintf(fp, "\n");
414
415         return 0;
416 }
417
418 static int xfrm_policy_get_or_delete(int argc, char **argv, int delete,
419                                      void *res_nlbuf)
420 {
421         struct rtnl_handle rth;
422         struct {
423                 struct nlmsghdr                 n;
424                 struct xfrm_userpolicy_id       xpid;
425         } req;
426         char *dirp = NULL;
427         char *selp = NULL;
428         char *indexp = NULL;
429
430         memset(&req, 0, sizeof(req));
431
432         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid));
433         req.n.nlmsg_flags = NLM_F_REQUEST;
434         req.n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY : XFRM_MSG_GETPOLICY;
435
436         while (argc > 0) {
437                 if (strcmp(*argv, "dir") == 0) {
438                         if (dirp)
439                                 duparg("dir", *argv);
440                         dirp = *argv;
441
442                         NEXT_ARG();
443                         xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv);
444
445                 } else if (strcmp(*argv, "index") == 0) {
446                         if (indexp)
447                                 duparg("index", *argv);
448                         indexp = *argv;
449
450                         NEXT_ARG();
451                         if (get_u32(&req.xpid.index, *argv, 0))
452                                 invarg("\"INDEX\" is invalid", *argv);
453
454                 } else {
455                         if (selp)
456                                 invarg("unknown", *argv);
457                         selp = *argv;
458
459                         xfrm_selector_parse(&req.xpid.sel, &argc, &argv);
460                         if (preferred_family == AF_UNSPEC)
461                                 preferred_family = req.xpid.sel.family;
462
463                 }
464
465                 argc--; argv++;
466         }
467
468         if (!dirp) {
469                 fprintf(stderr, "Not enough information: \"DIR\" is required.\n");
470                 exit(1);
471         }
472         if (!selp && !indexp) {
473                 fprintf(stderr, "Not enough information: either \"SELECTOR\" or \"INDEX\" is required.\n");
474                 exit(1);
475         }
476         if (selp && indexp)
477                 duparg2("SELECTOR", "INDEX");
478
479         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
480                 exit(1);
481
482         if (req.xpid.sel.family == AF_UNSPEC)
483                 req.xpid.sel.family = AF_INET;
484
485         if (rtnl_talk(&rth, &req.n, 0, 0, res_nlbuf, NULL, NULL) < 0)
486                 exit(2);
487
488         rtnl_close(&rth);
489
490         return 0;
491 }
492
493 static int xfrm_policy_delete(int argc, char **argv)
494 {
495         return xfrm_policy_get_or_delete(argc, argv, 1, NULL);
496 }
497
498 static int xfrm_policy_get(int argc, char **argv)
499 {
500         char buf[NLMSG_BUF_SIZE];
501         struct nlmsghdr *n = (struct nlmsghdr *)buf;
502
503         memset(buf, 0, sizeof(buf));
504
505         xfrm_policy_get_or_delete(argc, argv, 0, n);
506
507         if (xfrm_policy_print(NULL, n, (void*)stdout) < 0) {
508                 fprintf(stderr, "An error :-)\n");
509                 exit(1);
510         }
511
512         return 0;
513 }
514
515 /*
516  * With an existing policy of nlmsg, make new nlmsg for deleting the policy
517  * and store it to buffer.
518  */
519 static int xfrm_policy_keep(const struct sockaddr_nl *who,
520                             struct nlmsghdr *n,
521                             void *arg)
522 {
523         struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
524         struct rtnl_handle *rth = xb->rth;
525         struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n);
526         int len = n->nlmsg_len;
527         struct nlmsghdr *new_n;
528         struct xfrm_userpolicy_id *xpid;
529
530         if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) {
531                 fprintf(stderr, "Not a policy: %08x %08x %08x\n",
532                         n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
533                 return 0;
534         }
535
536         len -= NLMSG_LENGTH(sizeof(*xpinfo));
537         if (len < 0) {
538                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
539                 return -1;
540         }
541
542         if (!xfrm_policy_filter_match(xpinfo))
543                 return 0;
544
545         if (xb->offset > xb->size) {
546                 fprintf(stderr, "Flush buffer overflow\n");
547                 return -1;
548         }
549
550         new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
551         new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid));
552         new_n->nlmsg_flags = NLM_F_REQUEST;
553         new_n->nlmsg_type = XFRM_MSG_DELPOLICY;
554         new_n->nlmsg_seq = ++rth->seq;
555
556         xpid = NLMSG_DATA(new_n);
557         memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel));
558         xpid->dir = xpinfo->dir;
559         xpid->index = xpinfo->index;
560
561         xb->offset += new_n->nlmsg_len;
562         xb->nlmsg_count ++;
563
564         return 0;
565 }
566
567 static int xfrm_policy_list_or_flush(int argc, char **argv, int flush)
568 {
569         char *selp = NULL;
570         struct rtnl_handle rth;
571
572         if (argc > 0)
573                 filter.use = 1;
574         filter.xpinfo.sel.family = preferred_family;
575
576         while (argc > 0) {
577                 if (strcmp(*argv, "dir") == 0) {
578                         NEXT_ARG();
579                         xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv);
580
581                         filter.dir_mask = XFRM_FILTER_MASK_FULL;
582
583                 } else if (strcmp(*argv, "index") == 0) {
584                         NEXT_ARG();
585                         if (get_u32(&filter.xpinfo.index, *argv, 0))
586                                 invarg("\"INDEX\" is invalid", *argv);
587
588                         filter.index_mask = XFRM_FILTER_MASK_FULL;
589
590                 } else if (strcmp(*argv, "action") == 0) {
591                         NEXT_ARG();
592                         if (strcmp(*argv, "allow") == 0)
593                                 filter.xpinfo.action = XFRM_POLICY_ALLOW;
594                         else if (strcmp(*argv, "block") == 0)
595                                 filter.xpinfo.action = XFRM_POLICY_BLOCK;
596                         else
597                                 invarg("\"ACTION\" is invalid\n", *argv);
598
599                         filter.action_mask = XFRM_FILTER_MASK_FULL;
600
601                 } else if (strcmp(*argv, "priority") == 0) {
602                         NEXT_ARG();
603                         if (get_u32(&filter.xpinfo.priority, *argv, 0))
604                                 invarg("\"PRIORITY\" is invalid", *argv);
605
606                         filter.priority_mask = XFRM_FILTER_MASK_FULL;
607
608                 } else {
609                         if (selp)
610                                 invarg("unknown", *argv);
611                         selp = *argv;
612
613                         xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv);
614                         if (preferred_family == AF_UNSPEC)
615                                 preferred_family = filter.xpinfo.sel.family;
616
617                 }
618
619                 argc--; argv++;
620         }
621
622         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
623                 exit(1);
624
625         if (flush) {
626                 struct xfrm_buffer xb;
627                 char buf[NLMSG_FLUSH_BUF_SIZE];
628                 int i;
629
630                 xb.buf = buf;
631                 xb.size = sizeof(buf);
632                 xb.rth = &rth;
633
634                 for (i = 0; ; i++) {
635                         xb.offset = 0;
636                         xb.nlmsg_count = 0;
637
638                         if (show_stats > 1)
639                                 fprintf(stderr, "Flush round = %d\n", i);
640
641                         if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
642                                 perror("Cannot send dump request");
643                                 exit(1);
644                         }
645
646                         if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb, NULL, NULL) < 0) {
647                                 fprintf(stderr, "Flush terminated\n");
648                                 exit(1);
649                         }
650                         if (xb.nlmsg_count == 0) {
651                                 if (show_stats > 1)
652                                         fprintf(stderr, "Flush completed\n");
653                                 break;
654                         }
655
656                         if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
657                                 perror("Failed to send flush request\n");
658                                 exit(1);
659                         }
660                         if (show_stats > 1)
661                                 fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count);
662
663                         xb.offset = 0;
664                         xb.nlmsg_count = 0;
665                 }
666         } else {
667                 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETPOLICY) < 0) {
668                         perror("Cannot send dump request");
669                         exit(1);
670                 }
671
672                 if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout, NULL, NULL) < 0) {
673                         fprintf(stderr, "Dump terminated\n");
674                         exit(1);
675                 }
676         }
677
678         rtnl_close(&rth);
679
680         exit(0);
681 }
682
683 static int xfrm_policy_flush_all(void)
684 {
685         struct rtnl_handle rth;
686         struct {
687                 struct nlmsghdr n;
688         } req;
689
690         memset(&req, 0, sizeof(req));
691
692         req.n.nlmsg_len = NLMSG_LENGTH(0); /* nlmsg data is nothing */
693         req.n.nlmsg_flags = NLM_F_REQUEST;
694         req.n.nlmsg_type = XFRM_MSG_FLUSHPOLICY;
695
696         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
697                 exit(1);
698
699         if (show_stats > 1)
700                 fprintf(stderr, "Flush all\n");
701
702         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
703                 exit(2);
704
705         rtnl_close(&rth);
706
707         return 0;
708 }
709
710 int do_xfrm_policy(int argc, char **argv)
711 {
712         if (argc < 1)
713                 return xfrm_policy_list_or_flush(0, NULL, 0);
714
715         if (matches(*argv, "add") == 0)
716                 return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0,
717                                           argc-1, argv+1);
718         if (matches(*argv, "update") == 0)
719                 return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0,
720                                           argc-1, argv+1);
721         if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0)
722                 return xfrm_policy_delete(argc-1, argv+1);
723         if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
724             || matches(*argv, "lst") == 0)
725                 return xfrm_policy_list_or_flush(argc-1, argv+1, 0);
726         if (matches(*argv, "get") == 0)
727                 return xfrm_policy_get(argc-1, argv+1);
728         if (matches(*argv, "flush") == 0) {
729                 if (argc-1 < 1)
730                         return xfrm_policy_flush_all();
731                 else
732                         return xfrm_policy_list_or_flush(argc-1, argv+1, 1);
733         }
734         if (matches(*argv, "help") == 0)
735                 usage();
736         fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv);
737         exit(-1);
738 }