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