This commit was generated by cvs2svn to compensate for changes in r1650,
[iproute2.git] / ip / xfrm_state.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/xfrm.h>
33 #include "utils.h"
34 #include "xfrm.h"
35 #include "ip_common.h"
36
37 //#define NLMSG_FLUSH_BUF_SIZE (4096-512)
38 #define NLMSG_FLUSH_BUF_SIZE 8192
39
40 /*
41  * Receiving buffer defines:
42  * nlmsg
43  *   data = struct xfrm_usersa_info
44  *   rtattr
45  *   rtattr
46  *   ... (max count of rtattr is XFRM_MAX+1
47  *
48  *  each rtattr data = struct xfrm_algo(dynamic size) or xfrm_address_t
49  */
50 #define NLMSG_BUF_SIZE 4096
51 #define RTA_BUF_SIZE 2048
52 #define XFRM_ALGO_KEY_BUF_SIZE 512
53
54 static void usage(void) __attribute__((noreturn));
55
56 static void usage(void)
57 {
58         fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n");
59         fprintf(stderr, "        [ reqid REQID ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n");
60         fprintf(stderr, "        [ encap ENCAP ] [ sel SELECTOR ] [ LIMIT-LIST ]\n");
61         fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n");
62         fprintf(stderr, "Usage: ip xfrm state { flush | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
63         fprintf(stderr, "        [ flag FLAG_LIST ]\n");
64
65         fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
66         //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
67         fprintf(stderr, "XFRM_PROTO := [ ");
68         fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
69         fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
70         fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_COMP));
71         fprintf(stderr, "]\n");
72
73         //fprintf(stderr, "SPI - security parameter index(default=0)\n");
74
75         fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n");
76         //fprintf(stderr, "REQID - number(default=0)\n");
77
78         fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
79         fprintf(stderr, "FLAG := [ noecn | decap-dscp ]\n");
80  
81         fprintf(stderr, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
82         fprintf(stderr, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
83
84         fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
85         fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
86         fprintf(stderr, "ALGO_TYPE := [ ");
87         fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
88         fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH));
89         fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP));
90         fprintf(stderr, "]\n");
91
92         //fprintf(stderr, "ALGO_NAME - algorithm name\n");
93         //fprintf(stderr, "ALGO_KEY - algorithm key\n");
94
95         fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
96
97         fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
98         fprintf(stderr, "                        [ type NUMBER ] [ code NUMBER ] ]\n");
99
100
101         //fprintf(stderr, "DEV - device name(default=none)\n");
102         fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
103         fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
104         fprintf(stderr, "         [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
105         exit(-1);
106 }
107
108 static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
109                            char *name, char *key, int max)
110 {
111         int len;
112         int slen = strlen(key);
113
114 #if 0
115         /* XXX: verifying both name and key is required! */
116         fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
117 #endif
118
119         strncpy(alg->alg_name, name, sizeof(alg->alg_name));
120
121         if (slen > 2 && strncmp(key, "0x", 2) == 0) {
122                 /* split two chars "0x" from the top */
123                 char *p = key + 2;
124                 int plen = slen - 2;
125                 int i;
126                 int j;
127
128                 /* Converting hexadecimal numbered string into real key;
129                  * Convert each two chars into one char(value). If number
130                  * of the length is odd, add zero on the top for rounding.
131                  */
132
133                 /* calculate length of the converted values(real key) */
134                 len = (plen + 1) / 2;
135                 if (len > max)
136                         invarg("\"ALGOKEY\" makes buffer overflow\n", key);
137
138                 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
139                         char vbuf[3];
140                         char val;
141
142                         vbuf[0] = i >= 0 ? p[i] : '0';
143                         vbuf[1] = p[i + 1];
144                         vbuf[2] = '\0';
145
146                         if (get_u8(&val, vbuf, 16))
147                                 invarg("\"ALGOKEY\" is invalid", key);
148
149                         alg->alg_key[j] = val;
150                 }
151         } else {
152                 len = slen;
153                 if (len > 0) {
154                         if (len > max)
155                                 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
156
157                         strncpy(alg->alg_key, key, len);
158                 }
159         }
160
161         alg->alg_key_len = len * 8;
162
163         return 0;
164 }
165
166 static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
167 {
168         int argc = *argcp;
169         char **argv = *argvp;
170         int len = strlen(*argv);
171
172         if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
173                 __u8 val = 0;
174
175                 if (get_u8(&val, *argv, 16))
176                         invarg("\"FLAG\" is invalid", *argv);
177                 *flags = val;
178         } else {
179                 while (1) {
180                         if (strcmp(*argv, "noecn") == 0)
181                                 *flags |= XFRM_STATE_NOECN;
182                         else if (strcmp(*argv, "decap-dscp") == 0)
183                                 *flags |= XFRM_STATE_DECAP_DSCP;
184                         else {
185                                 PREV_ARG(); /* back track */
186                                 break;
187                         }
188
189                         if (!NEXT_ARG_OK())
190                                 break;
191                         NEXT_ARG();
192                 }
193         }
194
195         filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
196
197         *argcp = argc;
198         *argvp = argv;
199
200         return 0;
201 }
202
203 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
204 {
205         struct rtnl_handle rth;
206         struct {
207                 struct nlmsghdr         n;
208                 struct xfrm_usersa_info xsinfo;
209                 char                    buf[RTA_BUF_SIZE];
210         } req;
211         char *idp = NULL;
212         char *ealgop = NULL;
213         char *aalgop = NULL;
214         char *calgop = NULL;
215
216         memset(&req, 0, sizeof(req));
217
218         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
219         req.n.nlmsg_flags = NLM_F_REQUEST|flags;
220         req.n.nlmsg_type = cmd;
221         req.xsinfo.family = preferred_family;
222
223         req.xsinfo.lft.soft_byte_limit = XFRM_INF;
224         req.xsinfo.lft.hard_byte_limit = XFRM_INF;
225         req.xsinfo.lft.soft_packet_limit = XFRM_INF;
226         req.xsinfo.lft.hard_packet_limit = XFRM_INF;
227
228         while (argc > 0) {
229                 if (strcmp(*argv, "mode") == 0) {
230                         NEXT_ARG();
231                         xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
232                 } else if (strcmp(*argv, "reqid") == 0) {
233                         NEXT_ARG();
234                         xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
235                 } else if (strcmp(*argv, "replay-window") == 0) {
236                         NEXT_ARG();
237                         if (get_u8(&req.xsinfo.replay_window, *argv, 0))
238                                 invarg("\"replay-window\" value is invalid", *argv);
239                 } else if (strcmp(*argv, "flag") == 0) {
240                         NEXT_ARG();
241                         xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
242                 } else if (strcmp(*argv, "sel") == 0) {
243                         NEXT_ARG();
244                         xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
245                 } else if (strcmp(*argv, "limit") == 0) {
246                         NEXT_ARG();
247                         xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
248                 } else if (strcmp(*argv, "encap") == 0) {
249                         struct xfrm_encap_tmpl encap;
250                         inet_prefix oa;
251                         NEXT_ARG();
252                         xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
253                         NEXT_ARG();
254                         if (get_u16(&encap.encap_sport, *argv, 0))
255                                 invarg("\"encap\" sport value is invalid", *argv);
256                         encap.encap_sport = htons(encap.encap_sport);
257                         NEXT_ARG();
258                         if (get_u16(&encap.encap_dport, *argv, 0))
259                                 invarg("\"encap\" dport value is invalid", *argv);
260                         encap.encap_dport = htons(encap.encap_dport);
261                         NEXT_ARG();
262                         get_addr(&oa, *argv, AF_UNSPEC);
263                         memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
264                         addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
265                                   (void *)&encap, sizeof(encap));
266                 } else {
267                         /* try to assume ALGO */
268                         int type = xfrm_algotype_getbyname(*argv);
269                         switch (type) {
270                         case XFRMA_ALG_CRYPT:
271                         case XFRMA_ALG_AUTH:
272                         case XFRMA_ALG_COMP:
273                         {
274                                 /* ALGO */
275                                 struct {
276                                         struct xfrm_algo alg;
277                                         char buf[XFRM_ALGO_KEY_BUF_SIZE];
278                                 } alg;
279                                 int len;
280                                 char *name;
281                                 char *key;
282
283                                 switch (type) {
284                                 case XFRMA_ALG_CRYPT:
285                                         if (ealgop)
286                                                 duparg("ALGOTYPE", *argv);
287                                         ealgop = *argv;
288                                         break;
289                                 case XFRMA_ALG_AUTH:
290                                         if (aalgop)
291                                                 duparg("ALGOTYPE", *argv);
292                                         aalgop = *argv;
293                                         break;
294                                 case XFRMA_ALG_COMP:
295                                         if (calgop)
296                                                 duparg("ALGOTYPE", *argv);
297                                         calgop = *argv;
298                                         break;
299                                 default:
300                                         /* not reached */
301                                         invarg("\"ALGOTYPE\" is invalid\n", *argv);
302                                 }
303
304                                 if (!NEXT_ARG_OK())
305                                         missarg("ALGONAME");
306                                 NEXT_ARG();
307                                 name = *argv;
308
309                                 if (!NEXT_ARG_OK())
310                                         missarg("ALGOKEY");
311                                 NEXT_ARG();
312                                 key = *argv;
313
314                                 memset(&alg, 0, sizeof(alg));
315
316                                 xfrm_algo_parse((void *)&alg, type, name, key,
317                                                 sizeof(alg.buf));
318                                 len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len;
319
320                                 addattr_l(&req.n, sizeof(req.buf), type,
321                                           (void *)&alg, len);
322                                 break;
323                         }
324                         default:
325                                 /* try to assume ID */
326                                 if (idp)
327                                         invarg("unknown", *argv);
328                                 idp = *argv;
329
330                                 /* ID */
331                                 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
332                                               &req.xsinfo.family, 0, &argc, &argv);
333                                 if (preferred_family == AF_UNSPEC)
334                                         preferred_family = req.xsinfo.family;
335                         }
336                 }
337                 argc--; argv++;
338         }
339
340         if (!idp) {
341                 fprintf(stderr, "Not enough information: \"ID\" is required\n");
342                 exit(1);
343         }
344
345         if (ealgop || aalgop || calgop) {
346                 if (req.xsinfo.id.proto != IPPROTO_ESP &&
347                     req.xsinfo.id.proto != IPPROTO_AH &&
348                     req.xsinfo.id.proto != IPPROTO_COMP) {
349                         fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto));
350                         exit(1);
351                 }
352         } else {
353                 if (req.xsinfo.id.proto == IPPROTO_ESP ||
354                     req.xsinfo.id.proto == IPPROTO_AH ||
355                     req.xsinfo.id.proto == IPPROTO_COMP) {
356                         fprintf(stderr, "\"ALGO\" is required with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto));
357                         exit (1);
358                 }
359         }
360
361         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
362                 exit(1);
363
364         if (req.xsinfo.family == AF_UNSPEC)
365                 req.xsinfo.family = AF_INET;
366
367         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
368                 exit(2);
369
370         rtnl_close(&rth);
371
372         return 0;
373 }
374
375 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
376 {
377         if (!filter.use)
378                 return 1;
379
380         if (filter.id_src_mask)
381                 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
382                                     filter.id_src_mask))
383                         return 0;
384         if (filter.id_dst_mask)
385                 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
386                                     filter.id_dst_mask))
387                         return 0;
388         if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
389                 return 0;
390         if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
391                 return 0;
392         if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
393                 return 0;
394         if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
395                 return 0;
396         if (filter.state_flags_mask)
397                 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
398                         return 0;
399
400         return 1;
401 }
402
403 static int xfrm_selector_iszero(struct xfrm_selector *s)
404 {
405         struct xfrm_selector s0;
406
407         memset(&s0, 0, sizeof(s0));
408
409         return (memcmp(&s0, s, sizeof(s0)) == 0);
410 }
411
412 static int xfrm_state_print(const struct sockaddr_nl *who,
413                             struct nlmsghdr *n,
414                             void *arg)
415 {
416         FILE *fp = (FILE*)arg;
417         struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
418         int len = n->nlmsg_len;
419         struct rtattr * tb[XFRMA_MAX+1];
420
421         if (n->nlmsg_type != XFRM_MSG_NEWSA &&
422             n->nlmsg_type != XFRM_MSG_DELSA) {
423                 fprintf(stderr, "Not a state: %08x %08x %08x\n",
424                         n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
425                 return 0;
426         }
427
428         len -= NLMSG_LENGTH(sizeof(*xsinfo));
429         if (len < 0) {
430                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
431                 return -1;
432         }
433
434         if (!xfrm_state_filter_match(xsinfo))
435                 return 0;
436
437         parse_rtattr(tb, XFRMA_MAX, XFRMS_RTA(xsinfo), len);
438
439         if (n->nlmsg_type == XFRM_MSG_DELSA)
440                 fprintf(fp, "Deleted ");
441
442         xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
443                            xsinfo->reqid, xsinfo->family, 1, fp, NULL);
444
445         fprintf(fp, "\t");
446         fprintf(fp, "replay-window %u ", xsinfo->replay_window);
447         if (show_stats > 0)
448                 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
449         if (show_stats > 0 || xsinfo->flags) {
450                 __u8 flags = xsinfo->flags;
451
452                 fprintf(fp, "flag ");
453                 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
454                 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
455                 if (flags)
456                         fprintf(fp, "%x", flags);
457                 if (show_stats > 0)
458                         fprintf(fp, " (0x%s)", strxf_mask8(flags));
459         }
460         fprintf(fp, "%s", _SL_);
461
462         xfrm_xfrma_print(tb, xsinfo->family, fp, "\t");
463
464         if (!xfrm_selector_iszero(&xsinfo->sel))
465                 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, "\tsel ");
466
467         if (show_stats > 0) {
468                 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, "\t");
469                 xfrm_stats_print(&xsinfo->stats, fp, "\t");
470         }
471
472         if (oneline)
473                 fprintf(fp, "\n");
474
475         return 0;
476 }
477
478 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
479 {
480         struct rtnl_handle rth;
481         struct {
482                 struct nlmsghdr         n;
483                 struct xfrm_usersa_id   xsid;
484         } req;
485         struct xfrm_id id;
486         char *idp = NULL;
487
488         memset(&req, 0, sizeof(req));
489
490         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
491         req.n.nlmsg_flags = NLM_F_REQUEST;
492         req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
493         req.xsid.family = preferred_family;
494
495         while (argc > 0) {
496                 /*
497                  * XXX: Source address is not used and ignore it to follow
498                  * XXX: a manner of setkey e.g. in the case of deleting/getting
499                  * XXX: message of IPsec SA.
500                  */
501                 xfrm_address_t ignore_saddr;
502
503                 if (idp)
504                         invarg("unknown", *argv);
505                 idp = *argv;
506
507                 /* ID */
508                 memset(&id, 0, sizeof(id));
509                 xfrm_id_parse(&ignore_saddr, &id, &req.xsid.family, 0,
510                               &argc, &argv);
511
512                 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
513                 req.xsid.spi = id.spi;
514                 req.xsid.proto = id.proto;
515
516                 argc--; argv++;
517         }
518
519         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
520                 exit(1);
521
522         if (req.xsid.family == AF_UNSPEC)
523                 req.xsid.family = AF_INET;
524
525         if (delete) {
526                 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
527                         exit(2);
528         } else {
529                 char buf[NLMSG_BUF_SIZE];
530                 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
531
532                 memset(buf, 0, sizeof(buf));
533
534                 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
535                         exit(2);
536
537                 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
538                         fprintf(stderr, "An error :-)\n");
539                         exit(1);
540                 }
541         }
542
543         rtnl_close(&rth);
544
545         return 0;
546 }
547
548 /*
549  * With an existing state of nlmsg, make new nlmsg for deleting the state
550  * and store it to buffer.
551  */
552 static int xfrm_state_keep(const struct sockaddr_nl *who,
553                            struct nlmsghdr *n,
554                            void *arg)
555 {
556         struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
557         struct rtnl_handle *rth = xb->rth;
558         struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
559         int len = n->nlmsg_len;
560         struct nlmsghdr *new_n;
561         struct xfrm_usersa_id *xsid;
562
563         if (n->nlmsg_type != XFRM_MSG_NEWSA) {
564                 fprintf(stderr, "Not a state: %08x %08x %08x\n",
565                         n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
566                 return 0;
567         }
568
569         len -= NLMSG_LENGTH(sizeof(*xsinfo));
570         if (len < 0) {
571                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
572                 return -1;
573         }
574
575         if (!xfrm_state_filter_match(xsinfo))
576                 return 0;
577
578         if (xb->offset > xb->size) {
579                 fprintf(stderr, "Flush buffer overflow\n");
580                 return -1;
581         }
582
583         new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
584         new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
585         new_n->nlmsg_flags = NLM_F_REQUEST;
586         new_n->nlmsg_type = XFRM_MSG_DELSA;
587         new_n->nlmsg_seq = ++rth->seq;
588
589         xsid = NLMSG_DATA(new_n);
590         xsid->family = xsinfo->family;
591         memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
592         xsid->spi = xsinfo->id.spi;
593         xsid->proto = xsinfo->id.proto;
594
595         xb->offset += new_n->nlmsg_len;
596         xb->nlmsg_count ++;
597
598         return 0;
599 }
600
601 static int xfrm_state_list_or_flush(int argc, char **argv, int flush)
602 {
603         char *idp = NULL;
604         struct rtnl_handle rth;
605
606         if(argc > 0)
607                 filter.use = 1;
608         filter.xsinfo.family = preferred_family;
609
610         while (argc > 0) {
611                 if (strcmp(*argv, "mode") == 0) {
612                         NEXT_ARG();
613                         xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
614
615                         filter.mode_mask = XFRM_FILTER_MASK_FULL;
616
617                 } else if (strcmp(*argv, "reqid") == 0) {
618                         NEXT_ARG();
619                         xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
620
621                         filter.reqid_mask = XFRM_FILTER_MASK_FULL;
622
623                 } else if (strcmp(*argv, "flag") == 0) {
624                         NEXT_ARG();
625                         xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
626
627                         filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
628
629                 } else {
630                         if (idp)
631                                 invarg("unknown", *argv);
632                         idp = *argv;
633
634                         /* ID */
635                         xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
636                                       &filter.xsinfo.family, 1, &argc, &argv);
637                         if (preferred_family == AF_UNSPEC)
638                                 preferred_family = filter.xsinfo.family;
639                 }
640                 argc--; argv++;
641         }
642
643         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
644                 exit(1);
645
646         if (flush) {
647                 struct xfrm_buffer xb;
648                 char buf[NLMSG_FLUSH_BUF_SIZE];
649                 int i;
650
651                 xb.buf = buf;
652                 xb.size = sizeof(buf);
653                 xb.rth = &rth;
654
655                 for (i = 0; ; i++) {
656                         xb.offset = 0;
657                         xb.nlmsg_count = 0;
658
659                         if (show_stats > 1)
660                                 fprintf(stderr, "Flush round = %d\n", i);
661
662                         if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
663                                 perror("Cannot send dump request");
664                                 exit(1);
665                         }
666
667                         if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) {
668                                 fprintf(stderr, "Flush terminated\n");
669                                 exit(1);
670                         }
671                         if (xb.nlmsg_count == 0) {
672                                 if (show_stats > 1)
673                                         fprintf(stderr, "Flush completed\n");
674                                 break;
675                         }
676
677                         if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
678                                 perror("Failed to send flush request\n");
679                                 exit(1);
680                         }
681                         if (show_stats > 1)
682                                 fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count);
683
684                         xb.offset = 0;
685                         xb.nlmsg_count = 0;
686                 }
687
688         } else {
689                 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
690                         perror("Cannot send dump request");
691                         exit(1);
692                 }
693
694                 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) {
695                         fprintf(stderr, "Dump terminated\n");
696                         exit(1);
697                 }
698         }
699
700         rtnl_close(&rth);
701
702         exit(0);
703 }
704
705 static int xfrm_state_flush_all(void)
706 {
707         struct rtnl_handle rth;
708         struct {
709                 struct nlmsghdr                 n;
710                 struct xfrm_usersa_flush        xsf;
711         } req;
712
713         memset(&req, 0, sizeof(req));
714
715         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
716         req.n.nlmsg_flags = NLM_F_REQUEST;
717         req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
718         req.xsf.proto = IPSEC_PROTO_ANY;
719
720         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
721                 exit(1);
722
723         if (show_stats > 1)
724                 fprintf(stderr, "Flush all\n");
725
726         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
727                 exit(2);
728
729         rtnl_close(&rth);
730
731         return 0;
732 }
733
734 int do_xfrm_state(int argc, char **argv)
735 {
736         if (argc < 1)
737                 return xfrm_state_list_or_flush(0, NULL, 0);
738
739         if (matches(*argv, "add") == 0)
740                 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
741                                          argc-1, argv+1);
742         if (matches(*argv, "update") == 0)
743                 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
744                                          argc-1, argv+1);
745         if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0)
746                 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
747         if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
748             || matches(*argv, "lst") == 0)
749                 return xfrm_state_list_or_flush(argc-1, argv+1, 0);
750         if (matches(*argv, "get") == 0)
751                 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
752         if (matches(*argv, "flush") == 0) {
753                 if (argc-1 < 1)
754                         return xfrm_state_flush_all();
755                 else
756                         return xfrm_state_list_or_flush(argc-1, argv+1, 1);
757         }
758         if (matches(*argv, "help") == 0)
759                 usage();
760         fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
761         exit(-1);
762 }