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