blob: 0e21203363a9982aacddb3afab57e10e30783f32 [file] [log] [blame]
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001/* $USAGI: $ */
2
3/*
4 * Copyright (C)2004 USAGI/WIDE Project
Stephen Hemmingerae665a52006-12-05 10:10:22 -08005 *
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00006 * 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.
Stephen Hemmingerae665a52006-12-05 10:10:22 -080010 *
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000011 * 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.
Stephen Hemmingerae665a52006-12-05 10:10:22 -080015 *
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000016 * 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
shemminger9bec1a42005-06-07 21:58:25 +000037//#define NLMSG_DELETEALL_BUF_SIZE (4096-512)
38#define NLMSG_DELETEALL_BUF_SIZE 8192
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000039
40/*
41 * Receiving buffer defines:
42 * nlmsg
43 * data = struct xfrm_usersa_info
44 * rtattr
45 * rtattr
12!tgraf25346132005-01-18 22:11:58 +000046 * ... (max count of rtattr is XFRM_MAX+1
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000047 *
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
54static void usage(void) __attribute__((noreturn));
55
56static void usage(void)
57{
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +090058 fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ XFRM_OPT ] [ mode MODE ]\n");
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +000059 fprintf(stderr, " [ reqid REQID ] [ seq SEQ ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n");
osdl.net!shemminger5cf576d2005-03-10 19:04:00 +000060 fprintf(stderr, " [ encap ENCAP ] [ sel SELECTOR ] [ LIMIT-LIST ]\n");
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +000061 fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ reqid REQID ] [ seq SEQ ]\n");
62 fprintf(stderr, " [ min SPI max SPI ]\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000063 fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n");
shemminger9bec1a42005-06-07 21:58:25 +000064 fprintf(stderr, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
Masahide NAKAMURAc1fa2252007-08-24 11:05:18 +090065 fprintf(stderr, " [ flag FLAG-LIST ]\n");
shemminger9bec1a42005-06-07 21:58:25 +000066 fprintf(stderr, "Usage: ip xfrm state flush [ proto XFRM_PROTO ]\n");
jamal0bb4a4c2007-05-03 19:09:41 -040067 fprintf(stderr, "Usage: ip xfrm state count \n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000068
69 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
org[shemminger]!nakam29aa4dd2004-09-28 18:40:49 +000070 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n");
net[shemminger]!shemminger9e566a42004-07-30 20:26:15 +000071 fprintf(stderr, "XFRM_PROTO := [ ");
org[shemminger]!nakam29aa4dd2004-09-28 18:40:49 +000072 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
73 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +090074 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
75 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
76 fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_DSTOPTS));
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +000077 fprintf(stderr, "]\n");
net[shemminger]!shemminger9e566a42004-07-30 20:26:15 +000078
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000079 //fprintf(stderr, "SPI - security parameter index(default=0)\n");
80
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +090081 fprintf(stderr, "MODE := [ transport | tunnel | ro | beet ](default=transport)\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000082 //fprintf(stderr, "REQID - number(default=0)\n");
83
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +000084 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
Masahide NAKAMURAc1fa2252007-08-24 11:05:18 +090085 fprintf(stderr, "FLAG := [ noecn | decap-dscp | nopmtudisc | wildrecv ]\n");
Stephen Hemmingerae665a52006-12-05 10:10:22 -080086
osdl.net!shemminger5cf576d2005-03-10 19:04:00 +000087 fprintf(stderr, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n");
88 fprintf(stderr, "ENCAP-TYPE := espinudp | espinudp-nonike\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000089
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +000090 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
Stephen Hemminger9414cd62008-03-08 13:29:35 -080091 fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +000092 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
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000098 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
99 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
100
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +0000101 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000102
org[shemminger]!nakamc70b36d2004-09-28 18:42:35 +0000103 fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n");
104 fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n");
105
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000106
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
114static 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;
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000118 int slen = strlen(key);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000119
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +0000120#if 0
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000121 /* 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
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000127 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
org[shemminger]!nakam54f73282004-09-28 18:36:52 +0000128 /* 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.
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000137 */
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000138
org[shemminger]!nakam54f73282004-09-28 18:36:52 +0000139 /* 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);
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000143
org[shemminger]!nakam54f73282004-09-28 18:36:52 +0000144 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
145 char vbuf[3];
shemminger737f15f2005-07-08 22:08:47 +0000146 __u8 val;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000147
org[shemminger]!nakam54f73282004-09-28 18:36:52 +0000148 vbuf[0] = i >= 0 ? p[i] : '0';
149 vbuf[1] = p[i + 1];
150 vbuf[2] = '\0';
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000151
org[shemminger]!nakam54f73282004-09-28 18:36:52 +0000152 if (get_u8(&val, vbuf, 16))
153 invarg("\"ALGOKEY\" is invalid", key);
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000154
org[shemminger]!nakam54f73282004-09-28 18:36:52 +0000155 alg->alg_key[j] = val;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000156 }
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000157 } else {
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000158 len = slen;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000159 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
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +0000172static 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
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000188static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
189{
190 int argc = *argcp;
191 char **argv = *argvp;
net[shemminger]!shemminger9e566a42004-07-30 20:26:15 +0000192 int len = strlen(*argv);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000193
net[shemminger]!shemminger9e566a42004-07-30 20:26:15 +0000194 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 {
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +0000201 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;
Masahide NAKAMURAc1fa2252007-08-24 11:05:18 +0900206 else if (strcmp(*argv, "nopmtudisc") == 0)
207 *flags |= XFRM_STATE_NOPMTUDISC;
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900208 else if (strcmp(*argv, "wildrecv") == 0)
209 *flags |= XFRM_STATE_WILDRECV;
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +0000210 else {
211 PREV_ARG(); /* back track */
212 break;
213 }
214
215 if (!NEXT_ARG_OK())
216 break;
217 NEXT_ARG();
218 }
net[shemminger]!shemminger9e566a42004-07-30 20:26:15 +0000219 }
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000220
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000221 *argcp = argc;
222 *argvp = argv;
223
224 return 0;
225}
226
227static 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;
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900239 char *coap = NULL;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000240
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) {
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000254 if (strcmp(*argv, "mode") == 0) {
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000255 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);
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +0000260 } else if (strcmp(*argv, "seq") == 0) {
261 NEXT_ARG();
262 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +0000263 } 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);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000267 } 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);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000273 } else if (strcmp(*argv, "limit") == 0) {
274 NEXT_ARG();
275 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
osdl.net!shemminger5cf576d2005-03-10 19:04:00 +0000276 } 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));
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900294 } 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));
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000315 } else {
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000316 /* 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;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000331
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000332 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 }
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000385 }
386 argc--; argv++;
387 }
388
389 if (!idp) {
390 fprintf(stderr, "Not enough information: \"ID\" is required\n");
391 exit(1);
392 }
393
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900394 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
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000420 if (ealgop || aalgop || calgop) {
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900421 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));
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000424 exit(1);
425 }
426 } else {
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900427 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));
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000444 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
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +0000462static 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
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000592static 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)
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +0000598 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
599 filter.id_src_mask))
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000600 return 0;
601 if (filter.id_dst_mask)
net[shemminger]!shemmingereaa34ee2005-01-17 23:29:39 +0000602 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
603 filter.id_dst_mask))
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000604 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
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +0000620int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
621 void *arg)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000622{
623 FILE *fp = (FILE*)arg;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000624 struct rtattr * tb[XFRMA_MAX+1];
shemminger90f93022005-06-07 21:55:55 +0000625 struct rtattr * rta;
shemmingerc595c792005-11-01 23:03:03 +0000626 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;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000630
631 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
shemminger90f93022005-06-07 21:55:55 +0000632 n->nlmsg_type != XFRM_MSG_DELSA &&
shemminger669ae742005-11-07 18:39:30 +0000633 n->nlmsg_type != XFRM_MSG_UPDSA &&
shemminger90f93022005-06-07 21:55:55 +0000634 n->nlmsg_type != XFRM_MSG_EXPIRE) {
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000635 fprintf(stderr, "Not a state: %08x %08x %08x\n",
636 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
637 return 0;
638 }
639
shemminger669ae742005-11-07 18:39:30 +0000640 if (n->nlmsg_type == XFRM_MSG_DELSA) {
shemmingerc595c792005-11-01 23:03:03 +0000641 /* Dont blame me for this .. Herbert made me do it */
642 xsid = NLMSG_DATA(n);
Andy Gayaf1b6a42006-08-10 20:25:40 -0400643 len -= NLMSG_SPACE(sizeof(*xsid));
shemminger669ae742005-11-07 18:39:30 +0000644 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
645 xexp = NLMSG_DATA(n);
646 xsinfo = &xexp->state;
Andy Gayaf1b6a42006-08-10 20:25:40 -0400647 len -= NLMSG_SPACE(sizeof(*xexp));
shemminger90f93022005-06-07 21:55:55 +0000648 } else {
649 xexp = NULL;
650 xsinfo = NLMSG_DATA(n);
Andy Gayaf1b6a42006-08-10 20:25:40 -0400651 len -= NLMSG_SPACE(sizeof(*xsinfo));
shemminger90f93022005-06-07 21:55:55 +0000652 }
653
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000654 if (len < 0) {
655 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
656 return -1;
657 }
658
shemminger669ae742005-11-07 18:39:30 +0000659 if (xsinfo && !xfrm_state_filter_match(xsinfo))
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000660 return 0;
661
shemminger669ae742005-11-07 18:39:30 +0000662 if (n->nlmsg_type == XFRM_MSG_DELSA)
shemmingerc595c792005-11-01 23:03:03 +0000663 fprintf(fp, "Deleted ");
shemminger669ae742005-11-07 18:39:30 +0000664 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
665 fprintf(fp, "Updated ");
shemmingerc595c792005-11-01 23:03:03 +0000666 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
667 fprintf(fp, "Expired ");
668
shemminger669ae742005-11-07 18:39:30 +0000669 if (n->nlmsg_type == XFRM_MSG_DELSA)
670 rta = XFRMSID_RTA(xsid);
671 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
shemminger90f93022005-06-07 21:55:55 +0000672 rta = XFRMEXP_RTA(xexp);
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800673 else
shemminger90f93022005-06-07 21:55:55 +0000674 rta = XFRMS_RTA(xsinfo);
675
676 parse_rtattr(tb, XFRMA_MAX, rta, len);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000677
shemmingerc595c792005-11-01 23:03:03 +0000678 if (n->nlmsg_type == XFRM_MSG_DELSA) {
shemminger669ae742005-11-07 18:39:30 +0000679 //xfrm_policy_id_print();
680
681 if (!tb[XFRMA_SA]) {
682 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
683 return -1;
shemmingerc595c792005-11-01 23:03:03 +0000684 }
shemminger669ae742005-11-07 18:39:30 +0000685 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 }
Stephen Hemmingerbdf9e862007-06-19 16:24:08 -0700689 xsinfo = RTA_DATA(tb[XFRMA_SA]);
shemmingerc595c792005-11-01 23:03:03 +0000690 }
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000691
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +0000692 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000693
shemminger90f93022005-06-07 21:55:55 +0000694 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
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000700 if (oneline)
701 fprintf(fp, "\n");
shemminger669ae742005-11-07 18:39:30 +0000702 fflush(fp);
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000703
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000704 return 0;
705}
706
707static 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;
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900713 char buf[RTA_BUF_SIZE];
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000714 } 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) {
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900726 xfrm_address_t saddr;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000727
728 if (idp)
729 invarg("unknown", *argv);
730 idp = *argv;
731
732 /* ID */
733 memset(&id, 0, sizeof(id));
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900734 memset(&saddr, 0, sizeof(saddr));
735 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000736 &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
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900742 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
743 (void *)&saddr, sizeof(saddr));
744
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000745 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 */
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000781static int xfrm_state_keep(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000782 struct nlmsghdr *n,
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000783 void *arg)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000784{
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) {
shemminger9bec1a42005-06-07 21:58:25 +0000808 fprintf(stderr, "State buffer overflow\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000809 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
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +0900824 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
825 sizeof(xsid->daddr));
826
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000827 xb->offset += new_n->nlmsg_len;
828 xb->nlmsg_count ++;
829
830 return 0;
831}
832
shemminger9bec1a42005-06-07 21:58:25 +0000833static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000834{
835 char *idp = NULL;
836 struct rtnl_handle rth;
837
org[shemminger]!nakambd641cd2004-09-28 18:38:35 +0000838 if(argc > 0)
839 filter.use = 1;
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000840 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 */
net[shemminger]!shemminger7809c612004-08-11 23:41:38 +0000867 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
868 &filter.xsinfo.family, 1, &argc, &argv);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000869 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
shemminger9bec1a42005-06-07 21:58:25 +0000878 if (deleteall) {
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000879 struct xfrm_buffer xb;
shemminger9bec1a42005-06-07 21:58:25 +0000880 char buf[NLMSG_DELETEALL_BUF_SIZE];
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000881 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)
shemminger9bec1a42005-06-07 21:58:25 +0000892 fprintf(stderr, "Delete-all round = %d\n", i);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000893
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) {
shemminger9bec1a42005-06-07 21:58:25 +0000900 fprintf(stderr, "Delete-all terminated\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000901 exit(1);
902 }
903 if (xb.nlmsg_count == 0) {
904 if (show_stats > 1)
shemminger9bec1a42005-06-07 21:58:25 +0000905 fprintf(stderr, "Delete-all completed\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000906 break;
907 }
908
Stephen Hemmingerf31a37f2008-01-31 21:38:58 -0800909 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
shemminger9bec1a42005-06-07 21:58:25 +0000910 perror("Failed to send delete-all request\n");
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000911 exit(1);
912 }
913 if (show_stats > 1)
shemminger9bec1a42005-06-07 21:58:25 +0000914 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000915
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
jamal0bb4a4c2007-05-03 19:09:41 -0400937int 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;
jamal0bb4a4c2007-05-03 19:09:41 -0400943 __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
Stephen Hemmingerbdf9e862007-06-19 16:24:08 -0700956 if (tb[XFRMA_SAD_CNT]) {
jamal0bb4a4c2007-05-03 19:09:41 -0400957 fprintf(fp,"\t SAD");
Stephen Hemmingerbdf9e862007-06-19 16:24:08 -0700958 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
jamal0bb4a4c2007-05-03 19:09:41 -0400959 fprintf(fp," count %d", *cnt);
960 } else {
961 fprintf(fp,"BAD SAD info returned\n");
962 return -1;
963 }
964
965 if (show_stats) {
Stephen Hemmingerbdf9e862007-06-19 16:24:08 -0700966 if (tb[XFRMA_SAD_HINFO]) {
967 struct xfrmu_sadhinfo *si;
jamal0bb4a4c2007-05-03 19:09:41 -0400968
Stephen Hemmingerbdf9e862007-06-19 16:24:08 -0700969 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 }
jamal0bb4a4c2007-05-03 19:09:41 -0400980 }
981 fprintf(fp,"\n");
982
983 return 0;
984}
985
986static 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
shemminger9bec1a42005-06-07 21:58:25 +00001014static int xfrm_state_flush(int argc, char **argv)
org[shemminger]!nakambd641cd2004-09-28 18:38:35 +00001015{
1016 struct rtnl_handle rth;
1017 struct {
1018 struct nlmsghdr n;
1019 struct xfrm_usersa_flush xsf;
1020 } req;
shemminger9bec1a42005-06-07 21:58:25 +00001021 char *protop = NULL;
org[shemminger]!nakambd641cd2004-09-28 18:38:35 +00001022
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;
Masahide NAKAMURA7ea4f5d2006-12-05 19:15:47 +09001028 req.xsf.proto = 0;
org[shemminger]!nakambd641cd2004-09-28 18:38:35 +00001029
shemminger9bec1a42005-06-07 21:58:25 +00001030 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
org[shemminger]!nakambd641cd2004-09-28 18:38:35 +00001051 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1052 exit(1);
1053
1054 if (show_stats > 1)
shemminger9bec1a42005-06-07 21:58:25 +00001055 fprintf(stderr, "Flush state proto=%s\n",
shemminger9bec1a42005-06-07 21:58:25 +00001056 strxf_xfrmproto(req.xsf.proto));
org[shemminger]!nakambd641cd2004-09-28 18:38:35 +00001057
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
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001066int do_xfrm_state(int argc, char **argv)
1067{
1068 if (argc < 1)
shemminger9bec1a42005-06-07 21:58:25 +00001069 return xfrm_state_list_or_deleteall(0, NULL, 0);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001070
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);
linux-ipv6.org!nakamfb7399b2005-03-22 16:13:21 +00001077 if (matches(*argv, "allocspi") == 0)
1078 return xfrm_state_allocspi(argc-1, argv+1);
shemminger9bec1a42005-06-07 21:58:25 +00001079 if (matches(*argv, "delete") == 0)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001080 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
shemminger9bec1a42005-06-07 21:58:25 +00001081 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1082 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001083 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1084 || matches(*argv, "lst") == 0)
shemminger9bec1a42005-06-07 21:58:25 +00001085 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001086 if (matches(*argv, "get") == 0)
1087 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
shemminger9bec1a42005-06-07 21:58:25 +00001088 if (matches(*argv, "flush") == 0)
1089 return xfrm_state_flush(argc-1, argv+1);
jamal0bb4a4c2007-05-03 19:09:41 -04001090 if (matches(*argv, "count") == 0) {
1091 return xfrm_sad_getinfo(argc, argv);
1092 }
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +00001093 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}