]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/xfrm_state.c
revert earlier mistake ALGO_NAME is okay
[lisovros/iproute2_canprio.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_DELETEALL_BUF_SIZE (4096-512)
38 #define NLMSG_DELETEALL_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 [ XFRM_OPT ] [ mode MODE ]\n");
59         fprintf(stderr, "        [ reqid REQID ] [ seq SEQ ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n");
60         fprintf(stderr, "        [ encap ENCAP ] [ sel SELECTOR ] [ LIMIT-LIST ]\n");
61         fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ reqid REQID ] [ seq SEQ ]\n");
62         fprintf(stderr, "        [ min SPI max SPI ]\n");
63         fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n");
64         fprintf(stderr, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
65         fprintf(stderr, "        [ flag FLAG-LIST ]\n");
66         fprintf(stderr, "Usage: ip xfrm state flush [ proto XFRM_PROTO ]\n");
67         fprintf(stderr, "Usage: ip xfrm state count \n");
68
69         fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
70         //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
71         fprintf(stderr, "XFRM_PROTO := [ ");
72         fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
73         fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
74         fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
75         fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
76         fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS));
77         fprintf(stderr, "]\n");
78
79         //fprintf(stderr, "SPI - security parameter index(default=0)\n");
80
81         fprintf(stderr, "MODE := [ transport | tunnel | ro | beet ](default=transport)\n");
82         //fprintf(stderr, "REQID - number(default=0)\n");
83
84         fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
85         fprintf(stderr, "FLAG := [ noecn | decap-dscp | nopmtudisc | wildrecv ]\n");
86
87         fprintf(stderr, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
88         fprintf(stderr, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
89
90         fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
91         fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
92         fprintf(stderr, "ALGO_TYPE := [ ");
93         fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
94         fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH));
95         fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP));
96         fprintf(stderr, "]\n");
97
98         //fprintf(stderr, "ALGO_NAME - algorithm name\n");
99         //fprintf(stderr, "ALGO_KEY - algorithm key\n");
100
101         fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
102
103         fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
104         fprintf(stderr, "                        [ type NUMBER ] [ code NUMBER ] ]\n");
105
106
107         //fprintf(stderr, "DEV - device name(default=none)\n");
108         fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
109         fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
110         fprintf(stderr, "         [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
111         exit(-1);
112 }
113
114 static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
115                            char *name, char *key, int max)
116 {
117         int len;
118         int slen = strlen(key);
119
120 #if 0
121         /* XXX: verifying both name and key is required! */
122         fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
123 #endif
124
125         strncpy(alg->alg_name, name, sizeof(alg->alg_name));
126
127         if (slen > 2 && strncmp(key, "0x", 2) == 0) {
128                 /* split two chars "0x" from the top */
129                 char *p = key + 2;
130                 int plen = slen - 2;
131                 int i;
132                 int j;
133
134                 /* Converting hexadecimal numbered string into real key;
135                  * Convert each two chars into one char(value). If number
136                  * of the length is odd, add zero on the top for rounding.
137                  */
138
139                 /* calculate length of the converted values(real key) */
140                 len = (plen + 1) / 2;
141                 if (len > max)
142                         invarg("\"ALGOKEY\" makes buffer overflow\n", key);
143
144                 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
145                         char vbuf[3];
146                         __u8 val;
147
148                         vbuf[0] = i >= 0 ? p[i] : '0';
149                         vbuf[1] = p[i + 1];
150                         vbuf[2] = '\0';
151
152                         if (get_u8(&val, vbuf, 16))
153                                 invarg("\"ALGOKEY\" is invalid", key);
154
155                         alg->alg_key[j] = val;
156                 }
157         } else {
158                 len = slen;
159                 if (len > 0) {
160                         if (len > max)
161                                 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
162
163                         strncpy(alg->alg_key, key, len);
164                 }
165         }
166
167         alg->alg_key_len = len * 8;
168
169         return 0;
170 }
171
172 static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp)
173 {
174         int argc = *argcp;
175         char **argv = *argvp;
176
177         if (get_u32(seq, *argv, 0))
178                 invarg("\"SEQ\" is invalid", *argv);
179
180         *seq = htonl(*seq);
181
182         *argcp = argc;
183         *argvp = argv;
184
185         return 0;
186 }
187
188 static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
189 {
190         int argc = *argcp;
191         char **argv = *argvp;
192         int len = strlen(*argv);
193
194         if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
195                 __u8 val = 0;
196
197                 if (get_u8(&val, *argv, 16))
198                         invarg("\"FLAG\" is invalid", *argv);
199                 *flags = val;
200         } else {
201                 while (1) {
202                         if (strcmp(*argv, "noecn") == 0)
203                                 *flags |= XFRM_STATE_NOECN;
204                         else if (strcmp(*argv, "decap-dscp") == 0)
205                                 *flags |= XFRM_STATE_DECAP_DSCP;
206                         else if (strcmp(*argv, "nopmtudisc") == 0)
207                                 *flags |= XFRM_STATE_NOPMTUDISC;
208                         else if (strcmp(*argv, "wildrecv") == 0)
209                                 *flags |= XFRM_STATE_WILDRECV;
210                         else {
211                                 PREV_ARG(); /* back track */
212                                 break;
213                         }
214
215                         if (!NEXT_ARG_OK())
216                                 break;
217                         NEXT_ARG();
218                 }
219         }
220
221         *argcp = argc;
222         *argvp = argv;
223
224         return 0;
225 }
226
227 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
228 {
229         struct rtnl_handle rth;
230         struct {
231                 struct nlmsghdr         n;
232                 struct xfrm_usersa_info xsinfo;
233                 char                    buf[RTA_BUF_SIZE];
234         } req;
235         char *idp = NULL;
236         char *ealgop = NULL;
237         char *aalgop = NULL;
238         char *calgop = NULL;
239         char *coap = NULL;
240
241         memset(&req, 0, sizeof(req));
242
243         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
244         req.n.nlmsg_flags = NLM_F_REQUEST|flags;
245         req.n.nlmsg_type = cmd;
246         req.xsinfo.family = preferred_family;
247
248         req.xsinfo.lft.soft_byte_limit = XFRM_INF;
249         req.xsinfo.lft.hard_byte_limit = XFRM_INF;
250         req.xsinfo.lft.soft_packet_limit = XFRM_INF;
251         req.xsinfo.lft.hard_packet_limit = XFRM_INF;
252
253         while (argc > 0) {
254                 if (strcmp(*argv, "mode") == 0) {
255                         NEXT_ARG();
256                         xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
257                 } else if (strcmp(*argv, "reqid") == 0) {
258                         NEXT_ARG();
259                         xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
260                 } else if (strcmp(*argv, "seq") == 0) {
261                         NEXT_ARG();
262                         xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
263                 } else if (strcmp(*argv, "replay-window") == 0) {
264                         NEXT_ARG();
265                         if (get_u8(&req.xsinfo.replay_window, *argv, 0))
266                                 invarg("\"replay-window\" value is invalid", *argv);
267                 } else if (strcmp(*argv, "flag") == 0) {
268                         NEXT_ARG();
269                         xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
270                 } else if (strcmp(*argv, "sel") == 0) {
271                         NEXT_ARG();
272                         xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
273                 } else if (strcmp(*argv, "limit") == 0) {
274                         NEXT_ARG();
275                         xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
276                 } else if (strcmp(*argv, "encap") == 0) {
277                         struct xfrm_encap_tmpl encap;
278                         inet_prefix oa;
279                         NEXT_ARG();
280                         xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
281                         NEXT_ARG();
282                         if (get_u16(&encap.encap_sport, *argv, 0))
283                                 invarg("\"encap\" sport value is invalid", *argv);
284                         encap.encap_sport = htons(encap.encap_sport);
285                         NEXT_ARG();
286                         if (get_u16(&encap.encap_dport, *argv, 0))
287                                 invarg("\"encap\" dport value is invalid", *argv);
288                         encap.encap_dport = htons(encap.encap_dport);
289                         NEXT_ARG();
290                         get_addr(&oa, *argv, AF_UNSPEC);
291                         memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
292                         addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
293                                   (void *)&encap, sizeof(encap));
294                 } else if (strcmp(*argv, "coa") == 0) {
295                         inet_prefix coa;
296                         xfrm_address_t xcoa;
297
298                         if (coap)
299                                 duparg("coa", *argv);
300                         coap = *argv;
301
302                         NEXT_ARG();
303
304                         get_prefix(&coa, *argv, preferred_family);
305                         if (coa.family == AF_UNSPEC)
306                                 invarg("\"coa\" address family is AF_UNSPEC", *argv);
307                         if (coa.bytelen > sizeof(xcoa))
308                                 invarg("\"coa\" address length is too large", *argv);
309
310                         memset(&xcoa, 0, sizeof(xcoa));
311                         memcpy(&xcoa, &coa.data, coa.bytelen);
312
313                         addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
314                                   (void *)&xcoa, sizeof(xcoa));
315                 } else {
316                         /* try to assume ALGO */
317                         int type = xfrm_algotype_getbyname(*argv);
318                         switch (type) {
319                         case XFRMA_ALG_CRYPT:
320                         case XFRMA_ALG_AUTH:
321                         case XFRMA_ALG_COMP:
322                         {
323                                 /* ALGO */
324                                 struct {
325                                         struct xfrm_algo alg;
326                                         char buf[XFRM_ALGO_KEY_BUF_SIZE];
327                                 } alg;
328                                 int len;
329                                 char *name;
330                                 char *key;
331
332                                 switch (type) {
333                                 case XFRMA_ALG_CRYPT:
334                                         if (ealgop)
335                                                 duparg("ALGOTYPE", *argv);
336                                         ealgop = *argv;
337                                         break;
338                                 case XFRMA_ALG_AUTH:
339                                         if (aalgop)
340                                                 duparg("ALGOTYPE", *argv);
341                                         aalgop = *argv;
342                                         break;
343                                 case XFRMA_ALG_COMP:
344                                         if (calgop)
345                                                 duparg("ALGOTYPE", *argv);
346                                         calgop = *argv;
347                                         break;
348                                 default:
349                                         /* not reached */
350                                         invarg("\"ALGOTYPE\" is invalid\n", *argv);
351                                 }
352
353                                 if (!NEXT_ARG_OK())
354                                         missarg("ALGONAME");
355                                 NEXT_ARG();
356                                 name = *argv;
357
358                                 if (!NEXT_ARG_OK())
359                                         missarg("ALGOKEY");
360                                 NEXT_ARG();
361                                 key = *argv;
362
363                                 memset(&alg, 0, sizeof(alg));
364
365                                 xfrm_algo_parse((void *)&alg, type, name, key,
366                                                 sizeof(alg.buf));
367                                 len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len;
368
369                                 addattr_l(&req.n, sizeof(req.buf), type,
370                                           (void *)&alg, len);
371                                 break;
372                         }
373                         default:
374                                 /* try to assume ID */
375                                 if (idp)
376                                         invarg("unknown", *argv);
377                                 idp = *argv;
378
379                                 /* ID */
380                                 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
381                                               &req.xsinfo.family, 0, &argc, &argv);
382                                 if (preferred_family == AF_UNSPEC)
383                                         preferred_family = req.xsinfo.family;
384                         }
385                 }
386                 argc--; argv++;
387         }
388
389         if (!idp) {
390                 fprintf(stderr, "Not enough information: \"ID\" is required\n");
391                 exit(1);
392         }
393
394         switch (req.xsinfo.mode) {
395         case XFRM_MODE_TRANSPORT:
396         case XFRM_MODE_TUNNEL:
397                 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
398                         fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
399                                 strxf_xfrmproto(req.xsinfo.id.proto));
400                         exit(1);
401                 }
402                 break;
403         case XFRM_MODE_ROUTEOPTIMIZATION:
404         case XFRM_MODE_IN_TRIGGER:
405                 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
406                         fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
407                                 strxf_xfrmproto(req.xsinfo.id.proto));
408                         exit(1);
409                 }
410                 if (req.xsinfo.id.spi != 0) {
411                         fprintf(stderr, "\"spi\" must be 0 with proto=%s\n",
412                                 strxf_xfrmproto(req.xsinfo.id.proto));
413                         exit(1);
414                 }
415                 break;
416         default:
417                 break;
418         }
419
420         if (ealgop || aalgop || calgop) {
421                 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
422                         fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n",
423                                 strxf_xfrmproto(req.xsinfo.id.proto));
424                         exit(1);
425                 }
426         } else {
427                 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
428                         fprintf(stderr, "\"ALGO\" is required with proto=%s\n",
429                                 strxf_xfrmproto(req.xsinfo.id.proto));
430                         exit (1);
431                 }
432         }
433
434         if (coap) {
435                 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
436                         fprintf(stderr, "\"coa\" is invalid with proto=%s\n",
437                                 strxf_xfrmproto(req.xsinfo.id.proto));
438                         exit(1);
439                 }
440         } else {
441                 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
442                         fprintf(stderr, "\"coa\" is required with proto=%s\n",
443                                 strxf_xfrmproto(req.xsinfo.id.proto));
444                         exit (1);
445                 }
446         }
447
448         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
449                 exit(1);
450
451         if (req.xsinfo.family == AF_UNSPEC)
452                 req.xsinfo.family = AF_INET;
453
454         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
455                 exit(2);
456
457         rtnl_close(&rth);
458
459         return 0;
460 }
461
462 static int xfrm_state_allocspi(int argc, char **argv)
463 {
464         struct rtnl_handle rth;
465         struct {
466                 struct nlmsghdr         n;
467                 struct xfrm_userspi_info xspi;
468                 char                    buf[RTA_BUF_SIZE];
469         } req;
470         char *idp = NULL;
471         char *minp = NULL;
472         char *maxp = NULL;
473         char res_buf[NLMSG_BUF_SIZE];
474         struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
475
476         memset(res_buf, 0, sizeof(res_buf));
477
478         memset(&req, 0, sizeof(req));
479
480         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
481         req.n.nlmsg_flags = NLM_F_REQUEST;
482         req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
483         req.xspi.info.family = preferred_family;
484
485 #if 0
486         req.xsinfo.lft.soft_byte_limit = XFRM_INF;
487         req.xsinfo.lft.hard_byte_limit = XFRM_INF;
488         req.xsinfo.lft.soft_packet_limit = XFRM_INF;
489         req.xsinfo.lft.hard_packet_limit = XFRM_INF;
490 #endif
491
492         while (argc > 0) {
493                 if (strcmp(*argv, "mode") == 0) {
494                         NEXT_ARG();
495                         xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
496                 } else if (strcmp(*argv, "reqid") == 0) {
497                         NEXT_ARG();
498                         xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
499                 } else if (strcmp(*argv, "seq") == 0) {
500                         NEXT_ARG();
501                         xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
502                 } else if (strcmp(*argv, "min") == 0) {
503                         if (minp)
504                                 duparg("min", *argv);
505                         minp = *argv;
506
507                         NEXT_ARG();
508
509                         if (get_u32(&req.xspi.min, *argv, 0))
510                                 invarg("\"min\" value is invalid", *argv);
511                 } else if (strcmp(*argv, "max") == 0) {
512                         if (maxp)
513                                 duparg("max", *argv);
514                         maxp = *argv;
515
516                         NEXT_ARG();
517
518                         if (get_u32(&req.xspi.max, *argv, 0))
519                                 invarg("\"max\" value is invalid", *argv);
520                 } else {
521                         /* try to assume ID */
522                         if (idp)
523                                 invarg("unknown", *argv);
524                         idp = *argv;
525
526                         /* ID */
527                         xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
528                                       &req.xspi.info.family, 0, &argc, &argv);
529                         if (req.xspi.info.id.spi) {
530                                 fprintf(stderr, "\"SPI\" must be zero\n");
531                                 exit(1);
532                         }
533                         if (preferred_family == AF_UNSPEC)
534                                 preferred_family = req.xspi.info.family;
535                 }
536                 argc--; argv++;
537         }
538
539         if (!idp) {
540                 fprintf(stderr, "Not enough information: \"ID\" is required\n");
541                 exit(1);
542         }
543
544         if (minp) {
545                 if (!maxp) {
546                         fprintf(stderr, "\"max\" is missing\n");
547                         exit(1);
548                 }
549                 if (req.xspi.min > req.xspi.max) {
550                         fprintf(stderr, "\"min\" valie is larger than \"max\" one\n");
551                         exit(1);
552                 }
553         } else {
554                 if (maxp) {
555                         fprintf(stderr, "\"min\" is missing\n");
556                         exit(1);
557                 }
558
559                 /* XXX: Default value defined in PF_KEY;
560                  * See kernel's net/key/af_key.c(pfkey_getspi).
561                  */
562                 req.xspi.min = 0x100;
563                 req.xspi.max = 0x0fffffff;
564
565                 /* XXX: IPCOMP spi is 16-bits;
566                  * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
567                  */
568                 if (req.xspi.info.id.proto == IPPROTO_COMP)
569                         req.xspi.max = 0xffff;
570         }
571
572         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
573                 exit(1);
574
575         if (req.xspi.info.family == AF_UNSPEC)
576                 req.xspi.info.family = AF_INET;
577
578
579         if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
580                 exit(2);
581
582         if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
583                 fprintf(stderr, "An error :-)\n");
584                 exit(1);
585         }
586
587         rtnl_close(&rth);
588
589         return 0;
590 }
591
592 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
593 {
594         if (!filter.use)
595                 return 1;
596
597         if (filter.id_src_mask)
598                 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
599                                     filter.id_src_mask))
600                         return 0;
601         if (filter.id_dst_mask)
602                 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
603                                     filter.id_dst_mask))
604                         return 0;
605         if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
606                 return 0;
607         if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
608                 return 0;
609         if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
610                 return 0;
611         if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
612                 return 0;
613         if (filter.state_flags_mask)
614                 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
615                         return 0;
616
617         return 1;
618 }
619
620 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
621                      void *arg)
622 {
623         FILE *fp = (FILE*)arg;
624         struct rtattr * tb[XFRMA_MAX+1];
625         struct rtattr * rta;
626         struct xfrm_usersa_info *xsinfo = NULL;
627         struct xfrm_user_expire *xexp = NULL;
628         struct xfrm_usersa_id   *xsid = NULL;
629         int len = n->nlmsg_len;
630
631         if (n->nlmsg_type != XFRM_MSG_NEWSA &&
632             n->nlmsg_type != XFRM_MSG_DELSA &&
633             n->nlmsg_type != XFRM_MSG_UPDSA &&
634             n->nlmsg_type != XFRM_MSG_EXPIRE) {
635                 fprintf(stderr, "Not a state: %08x %08x %08x\n",
636                         n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
637                 return 0;
638         }
639
640         if (n->nlmsg_type == XFRM_MSG_DELSA) {
641                 /* Dont blame me for this .. Herbert made me do it */
642                 xsid = NLMSG_DATA(n);
643                 len -= NLMSG_SPACE(sizeof(*xsid));
644         } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
645                 xexp = NLMSG_DATA(n);
646                 xsinfo = &xexp->state;
647                 len -= NLMSG_SPACE(sizeof(*xexp));
648         } else {
649                 xexp = NULL;
650                 xsinfo = NLMSG_DATA(n);
651                 len -= NLMSG_SPACE(sizeof(*xsinfo));
652         }
653
654         if (len < 0) {
655                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
656                 return -1;
657         }
658
659         if (xsinfo && !xfrm_state_filter_match(xsinfo))
660                 return 0;
661
662         if (n->nlmsg_type == XFRM_MSG_DELSA)
663                 fprintf(fp, "Deleted ");
664         else if (n->nlmsg_type == XFRM_MSG_UPDSA)
665                 fprintf(fp, "Updated ");
666         else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
667                 fprintf(fp, "Expired ");
668
669         if (n->nlmsg_type == XFRM_MSG_DELSA)
670                 rta = XFRMSID_RTA(xsid);
671         else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
672                 rta = XFRMEXP_RTA(xexp);
673         else
674                 rta = XFRMS_RTA(xsinfo);
675
676         parse_rtattr(tb, XFRMA_MAX, rta, len);
677
678         if (n->nlmsg_type == XFRM_MSG_DELSA) {
679                 //xfrm_policy_id_print();
680
681                 if (!tb[XFRMA_SA]) {
682                         fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
683                         return -1;
684                 }
685                 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
686                         fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
687                         return -1;
688                 }
689                 xsinfo = RTA_DATA(tb[XFRMA_SA]);
690         }
691
692         xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
693
694         if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
695                 fprintf(fp, "\t");
696                 fprintf(fp, "hard %u", xexp->hard);
697                 fprintf(fp, "%s", _SL_);
698         }
699
700         if (oneline)
701                 fprintf(fp, "\n");
702         fflush(fp);
703
704         return 0;
705 }
706
707 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
708 {
709         struct rtnl_handle rth;
710         struct {
711                 struct nlmsghdr         n;
712                 struct xfrm_usersa_id   xsid;
713                 char                    buf[RTA_BUF_SIZE];
714         } req;
715         struct xfrm_id id;
716         char *idp = NULL;
717
718         memset(&req, 0, sizeof(req));
719
720         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
721         req.n.nlmsg_flags = NLM_F_REQUEST;
722         req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
723         req.xsid.family = preferred_family;
724
725         while (argc > 0) {
726                 xfrm_address_t saddr;
727
728                 if (idp)
729                         invarg("unknown", *argv);
730                 idp = *argv;
731
732                 /* ID */
733                 memset(&id, 0, sizeof(id));
734                 memset(&saddr, 0, sizeof(saddr));
735                 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
736                               &argc, &argv);
737
738                 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
739                 req.xsid.spi = id.spi;
740                 req.xsid.proto = id.proto;
741
742                 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
743                           (void *)&saddr, sizeof(saddr));
744
745                 argc--; argv++;
746         }
747
748         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
749                 exit(1);
750
751         if (req.xsid.family == AF_UNSPEC)
752                 req.xsid.family = AF_INET;
753
754         if (delete) {
755                 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
756                         exit(2);
757         } else {
758                 char buf[NLMSG_BUF_SIZE];
759                 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
760
761                 memset(buf, 0, sizeof(buf));
762
763                 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
764                         exit(2);
765
766                 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
767                         fprintf(stderr, "An error :-)\n");
768                         exit(1);
769                 }
770         }
771
772         rtnl_close(&rth);
773
774         return 0;
775 }
776
777 /*
778  * With an existing state of nlmsg, make new nlmsg for deleting the state
779  * and store it to buffer.
780  */
781 static int xfrm_state_keep(const struct sockaddr_nl *who,
782                            struct nlmsghdr *n,
783                            void *arg)
784 {
785         struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
786         struct rtnl_handle *rth = xb->rth;
787         struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
788         int len = n->nlmsg_len;
789         struct nlmsghdr *new_n;
790         struct xfrm_usersa_id *xsid;
791
792         if (n->nlmsg_type != XFRM_MSG_NEWSA) {
793                 fprintf(stderr, "Not a state: %08x %08x %08x\n",
794                         n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
795                 return 0;
796         }
797
798         len -= NLMSG_LENGTH(sizeof(*xsinfo));
799         if (len < 0) {
800                 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
801                 return -1;
802         }
803
804         if (!xfrm_state_filter_match(xsinfo))
805                 return 0;
806
807         if (xb->offset > xb->size) {
808                 fprintf(stderr, "State buffer overflow\n");
809                 return -1;
810         }
811
812         new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
813         new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
814         new_n->nlmsg_flags = NLM_F_REQUEST;
815         new_n->nlmsg_type = XFRM_MSG_DELSA;
816         new_n->nlmsg_seq = ++rth->seq;
817
818         xsid = NLMSG_DATA(new_n);
819         xsid->family = xsinfo->family;
820         memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
821         xsid->spi = xsinfo->id.spi;
822         xsid->proto = xsinfo->id.proto;
823
824         addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
825                   sizeof(xsid->daddr));
826
827         xb->offset += new_n->nlmsg_len;
828         xb->nlmsg_count ++;
829
830         return 0;
831 }
832
833 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
834 {
835         char *idp = NULL;
836         struct rtnl_handle rth;
837
838         if(argc > 0)
839                 filter.use = 1;
840         filter.xsinfo.family = preferred_family;
841
842         while (argc > 0) {
843                 if (strcmp(*argv, "mode") == 0) {
844                         NEXT_ARG();
845                         xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
846
847                         filter.mode_mask = XFRM_FILTER_MASK_FULL;
848
849                 } else if (strcmp(*argv, "reqid") == 0) {
850                         NEXT_ARG();
851                         xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
852
853                         filter.reqid_mask = XFRM_FILTER_MASK_FULL;
854
855                 } else if (strcmp(*argv, "flag") == 0) {
856                         NEXT_ARG();
857                         xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
858
859                         filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
860
861                 } else {
862                         if (idp)
863                                 invarg("unknown", *argv);
864                         idp = *argv;
865
866                         /* ID */
867                         xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
868                                       &filter.xsinfo.family, 1, &argc, &argv);
869                         if (preferred_family == AF_UNSPEC)
870                                 preferred_family = filter.xsinfo.family;
871                 }
872                 argc--; argv++;
873         }
874
875         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
876                 exit(1);
877
878         if (deleteall) {
879                 struct xfrm_buffer xb;
880                 char buf[NLMSG_DELETEALL_BUF_SIZE];
881                 int i;
882
883                 xb.buf = buf;
884                 xb.size = sizeof(buf);
885                 xb.rth = &rth;
886
887                 for (i = 0; ; i++) {
888                         xb.offset = 0;
889                         xb.nlmsg_count = 0;
890
891                         if (show_stats > 1)
892                                 fprintf(stderr, "Delete-all round = %d\n", i);
893
894                         if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
895                                 perror("Cannot send dump request");
896                                 exit(1);
897                         }
898
899                         if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) {
900                                 fprintf(stderr, "Delete-all terminated\n");
901                                 exit(1);
902                         }
903                         if (xb.nlmsg_count == 0) {
904                                 if (show_stats > 1)
905                                         fprintf(stderr, "Delete-all completed\n");
906                                 break;
907                         }
908
909                         if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
910                                 perror("Failed to send delete-all request\n");
911                                 exit(1);
912                         }
913                         if (show_stats > 1)
914                                 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
915
916                         xb.offset = 0;
917                         xb.nlmsg_count = 0;
918                 }
919
920         } else {
921                 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
922                         perror("Cannot send dump request");
923                         exit(1);
924                 }
925
926                 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) {
927                         fprintf(stderr, "Dump terminated\n");
928                         exit(1);
929                 }
930         }
931
932         rtnl_close(&rth);
933
934         exit(0);
935 }
936
937 int print_sadinfo(struct nlmsghdr *n, void *arg)
938 {
939         FILE *fp = (FILE*)arg;
940         __u32 *f = NLMSG_DATA(n);
941         struct rtattr *tb[XFRMA_SAD_MAX+1];
942         struct rtattr *rta;
943         __u32 *cnt;
944
945         int len = n->nlmsg_len;
946
947         len -= NLMSG_LENGTH(sizeof(__u32));
948         if (len < 0) {
949                 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
950                 return -1;
951         }
952
953         rta = XFRMSAPD_RTA(f);
954         parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
955
956         if (tb[XFRMA_SAD_CNT]) {
957                 fprintf(fp,"\t SAD");
958                 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
959                 fprintf(fp," count %d", *cnt);
960         } else {
961                 fprintf(fp,"BAD SAD info returned\n");
962                 return -1;
963         }
964
965         if (show_stats) {
966                 if (tb[XFRMA_SAD_HINFO]) {
967                         struct xfrmu_sadhinfo *si;
968
969                         if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
970                                 fprintf(fp,"BAD SAD length returned\n");
971                                 return -1;
972                         }
973                                 
974                         si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
975                         fprintf(fp," (buckets ");
976                         fprintf(fp,"count %d", si->sadhcnt);
977                         fprintf(fp," Max %d", si->sadhmcnt);
978                         fprintf(fp,")");
979                 }
980         }
981         fprintf(fp,"\n");
982
983         return 0;
984 }
985
986 static int xfrm_sad_getinfo(int argc, char **argv)
987 {
988         struct rtnl_handle rth;
989         struct {
990                 struct nlmsghdr                 n;
991                 __u32                           flags;
992                 char                            ans[64];
993         } req;
994
995         memset(&req, 0, sizeof(req));
996         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
997         req.n.nlmsg_flags = NLM_F_REQUEST;
998         req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
999         req.flags = 0XFFFFFFFF;
1000
1001         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1002                 exit(1);
1003
1004         if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0)
1005                 exit(2);
1006
1007         print_sadinfo(&req.n, (void*)stdout);
1008
1009         rtnl_close(&rth);
1010
1011         return 0;
1012 }
1013
1014 static int xfrm_state_flush(int argc, char **argv)
1015 {
1016         struct rtnl_handle rth;
1017         struct {
1018                 struct nlmsghdr                 n;
1019                 struct xfrm_usersa_flush        xsf;
1020         } req;
1021         char *protop = NULL;
1022
1023         memset(&req, 0, sizeof(req));
1024
1025         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1026         req.n.nlmsg_flags = NLM_F_REQUEST;
1027         req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1028         req.xsf.proto = 0;
1029
1030         while (argc > 0) {
1031                 if (strcmp(*argv, "proto") == 0) {
1032                         int ret;
1033
1034                         if (protop)
1035                                 duparg("proto", *argv);
1036                         protop = *argv;
1037
1038                         NEXT_ARG();
1039
1040                         ret = xfrm_xfrmproto_getbyname(*argv);
1041                         if (ret < 0)
1042                                 invarg("\"XFRM_PROTO\" is invalid", *argv);
1043
1044                         req.xsf.proto = (__u8)ret;
1045                 } else
1046                         invarg("unknown", *argv);
1047
1048                 argc--; argv++;
1049         }
1050
1051         if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1052                 exit(1);
1053
1054         if (show_stats > 1)
1055                 fprintf(stderr, "Flush state proto=%s\n",
1056                         strxf_xfrmproto(req.xsf.proto));
1057
1058         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
1059                 exit(2);
1060
1061         rtnl_close(&rth);
1062
1063         return 0;
1064 }
1065
1066 int do_xfrm_state(int argc, char **argv)
1067 {
1068         if (argc < 1)
1069                 return xfrm_state_list_or_deleteall(0, NULL, 0);
1070
1071         if (matches(*argv, "add") == 0)
1072                 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1073                                          argc-1, argv+1);
1074         if (matches(*argv, "update") == 0)
1075                 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1076                                          argc-1, argv+1);
1077         if (matches(*argv, "allocspi") == 0)
1078                 return xfrm_state_allocspi(argc-1, argv+1);
1079         if (matches(*argv, "delete") == 0)
1080                 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1081         if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1082                 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1083         if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1084             || matches(*argv, "lst") == 0)
1085                 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1086         if (matches(*argv, "get") == 0)
1087                 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1088         if (matches(*argv, "flush") == 0)
1089                 return xfrm_state_flush(argc-1, argv+1);
1090         if (matches(*argv, "count") == 0) {
1091                 return xfrm_sad_getinfo(argc, argv);
1092         }
1093         if (matches(*argv, "help") == 0)
1094                 usage();
1095         fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
1096         exit(-1);
1097 }