]>
Commit | Line | Data |
---|---|---|
c7699875 | 1 | /* $USAGI: $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C)2004 USAGI/WIDE Project | |
ae665a52 | 5 | * |
c7699875 | 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. | |
ae665a52 | 10 | * |
c7699875 | 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. | |
ae665a52 | 15 | * |
c7699875 | 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 ip.c, 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 <sys/types.h> | |
32 | #include <sys/socket.h> | |
33 | #include <time.h> | |
34 | #include <netdb.h> | |
35 | #include <net/if.h> | |
36 | #include <linux/netlink.h> | |
37 | #include <linux/rtnetlink.h> | |
38 | #include <linux/xfrm.h> | |
39 | ||
40 | #include "utils.h" | |
41 | #include "xfrm.h" | |
42 | ||
15ac4cdc | 43 | #define STRBUF_SIZE (128) |
44 | #define STRBUF_CAT(buf, str) \ | |
45 | do { \ | |
46 | int rest = sizeof(buf) - 1 - strlen(buf); \ | |
47 | if (rest > 0) { \ | |
48 | int len = strlen(str); \ | |
49 | if (len > rest) \ | |
50 | len = rest; \ | |
51 | strncat(buf, str, len); \ | |
52 | buf[sizeof(buf) - 1] = '\0'; \ | |
53 | } \ | |
54 | } while(0); | |
55 | ||
c7699875 | 56 | struct xfrm_filter filter; |
57 | ||
58 | static void usage(void) __attribute__((noreturn)); | |
59 | ||
60 | static void usage(void) | |
61 | { | |
ae665a52 | 62 | fprintf(stderr, |
c7699875 | 63 | "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n" |
15ac4cdc | 64 | "where XFRM_OBJECT := { state | policy | monitor }\n"); |
c7699875 | 65 | exit(-1); |
66 | } | |
67 | ||
eaa34ee3 | 68 | /* This is based on utils.c(inet_addr_match) */ |
69 | int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits) | |
70 | { | |
71 | __u32 *a1 = (__u32 *)x1; | |
72 | __u32 *a2 = (__u32 *)x2; | |
73 | int words = bits >> 0x05; | |
74 | ||
75 | bits &= 0x1f; | |
76 | ||
77 | if (words) | |
78 | if (memcmp(a1, a2, words << 2)) | |
79 | return -1; | |
80 | ||
81 | if (bits) { | |
82 | __u32 w1, w2; | |
83 | __u32 mask; | |
84 | ||
85 | w1 = a1[words]; | |
86 | w2 = a2[words]; | |
87 | ||
88 | mask = htonl((0xffffffff) << (0x20 - bits)); | |
89 | ||
90 | if ((w1 ^ w2) & mask) | |
91 | return 1; | |
92 | } | |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
7ea4f5d3 MN |
97 | int xfrm_xfrmproto_is_ipsec(__u8 proto) |
98 | { | |
99 | return (proto == IPPROTO_ESP || | |
100 | proto == IPPROTO_AH || | |
101 | proto == IPPROTO_COMP); | |
102 | } | |
103 | ||
104 | int xfrm_xfrmproto_is_ro(__u8 proto) | |
105 | { | |
106 | return (proto == IPPROTO_ROUTING || | |
107 | proto == IPPROTO_DSTOPTS); | |
108 | } | |
109 | ||
7809c616 | 110 | struct typeent { |
111 | const char *t_name; | |
112 | int t_type; | |
113 | }; | |
114 | ||
29aa4dd7 | 115 | static const struct typeent xfrmproto_types[]= { |
eaa34ee3 | 116 | { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP }, |
7ea4f5d3 | 117 | { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS }, |
eaa34ee3 | 118 | { NULL, -1 } |
29aa4dd7 | 119 | }; |
120 | ||
121 | int xfrm_xfrmproto_getbyname(char *name) | |
122 | { | |
123 | int i; | |
124 | ||
125 | for (i = 0; ; i++) { | |
126 | const struct typeent *t = &xfrmproto_types[i]; | |
127 | if (!t->t_name || t->t_type == -1) | |
128 | break; | |
129 | ||
130 | if (strcmp(t->t_name, name) == 0) | |
131 | return t->t_type; | |
132 | } | |
133 | ||
134 | return -1; | |
135 | } | |
136 | ||
137 | const char *strxf_xfrmproto(__u8 proto) | |
138 | { | |
139 | int i; | |
140 | ||
141 | for (i = 0; ; i++) { | |
142 | const struct typeent *t = &xfrmproto_types[i]; | |
143 | if (!t->t_name || t->t_type == -1) | |
144 | break; | |
145 | ||
146 | if (t->t_type == proto) | |
147 | return t->t_name; | |
148 | } | |
149 | ||
150 | return NULL; | |
151 | } | |
152 | ||
7809c616 | 153 | static const struct typeent algo_types[]= { |
154 | { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH }, | |
155 | { "comp", XFRMA_ALG_COMP }, { NULL, -1 } | |
156 | }; | |
157 | ||
158 | int xfrm_algotype_getbyname(char *name) | |
159 | { | |
160 | int i; | |
161 | ||
162 | for (i = 0; ; i++) { | |
163 | const struct typeent *t = &algo_types[i]; | |
164 | if (!t->t_name || t->t_type == -1) | |
165 | break; | |
166 | ||
167 | if (strcmp(t->t_name, name) == 0) | |
168 | return t->t_type; | |
169 | } | |
170 | ||
171 | return -1; | |
172 | } | |
173 | ||
174 | const char *strxf_algotype(int type) | |
175 | { | |
176 | int i; | |
177 | ||
178 | for (i = 0; ; i++) { | |
179 | const struct typeent *t = &algo_types[i]; | |
180 | if (!t->t_name || t->t_type == -1) | |
181 | break; | |
182 | ||
183 | if (t->t_type == type) | |
184 | return t->t_name; | |
185 | } | |
186 | ||
187 | return NULL; | |
188 | } | |
189 | ||
eaa34ee3 | 190 | const char *strxf_mask8(__u8 mask) |
c7699875 | 191 | { |
192 | static char str[16]; | |
eaa34ee3 | 193 | const int sn = sizeof(mask) * 8 - 1; |
c7699875 | 194 | __u8 b; |
195 | int i = 0; | |
196 | ||
197 | for (b = (1 << sn); b > 0; b >>= 1) | |
eaa34ee3 | 198 | str[i++] = ((b & mask) ? '1' : '0'); |
c7699875 | 199 | str[i] = '\0'; |
200 | ||
201 | return str; | |
202 | } | |
203 | ||
eaa34ee3 | 204 | const char *strxf_mask32(__u32 mask) |
205 | { | |
206 | static char str[16]; | |
207 | ||
208 | sprintf(str, "%.8x", mask); | |
209 | ||
210 | return str; | |
211 | } | |
212 | ||
c7699875 | 213 | const char *strxf_share(__u8 share) |
214 | { | |
215 | static char str[32]; | |
216 | ||
217 | switch (share) { | |
218 | case XFRM_SHARE_ANY: | |
219 | strcpy(str, "any"); | |
220 | break; | |
221 | case XFRM_SHARE_SESSION: | |
222 | strcpy(str, "session"); | |
223 | break; | |
224 | case XFRM_SHARE_USER: | |
225 | strcpy(str, "user"); | |
226 | break; | |
227 | case XFRM_SHARE_UNIQUE: | |
228 | strcpy(str, "unique"); | |
229 | break; | |
230 | default: | |
eaa34ee3 | 231 | sprintf(str, "%u", share); |
c7699875 | 232 | break; |
233 | } | |
234 | ||
235 | return str; | |
236 | } | |
237 | ||
ad273962 | 238 | const char *strxf_proto(__u8 proto) |
239 | { | |
240 | static char buf[32]; | |
241 | struct protoent *pp; | |
242 | const char *p; | |
243 | ||
244 | pp = getprotobynumber(proto); | |
245 | if (pp) | |
246 | p = pp->p_name; | |
247 | else { | |
eaa34ee3 | 248 | sprintf(buf, "%u", proto); |
ad273962 | 249 | p = buf; |
250 | } | |
251 | ||
252 | return p; | |
253 | } | |
254 | ||
c7699875 | 255 | void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id, |
eaa34ee3 | 256 | __u8 mode, __u32 reqid, __u16 family, int force_spi, |
15ac4cdc | 257 | FILE *fp, const char *prefix, const char *title) |
c7699875 | 258 | { |
259 | char abuf[256]; | |
c7699875 | 260 | |
15ac4cdc | 261 | if (title) |
262 | fprintf(fp, title); | |
c7699875 | 263 | |
264 | memset(abuf, '\0', sizeof(abuf)); | |
ad273962 | 265 | fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr), |
266 | saddr, abuf, sizeof(abuf))); | |
c7699875 | 267 | memset(abuf, '\0', sizeof(abuf)); |
7809c616 | 268 | fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr), |
269 | &id->daddr, abuf, sizeof(abuf))); | |
270 | fprintf(fp, "%s", _SL_); | |
c7699875 | 271 | |
272 | if (prefix) | |
273 | fprintf(fp, prefix); | |
274 | fprintf(fp, "\t"); | |
275 | ||
29aa4dd7 | 276 | fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto)); |
c7699875 | 277 | |
eaa34ee3 | 278 | if (show_stats > 0 || force_spi || id->spi) { |
279 | __u32 spi = ntohl(id->spi); | |
280 | fprintf(fp, "spi 0x%08x", spi); | |
281 | if (show_stats > 0) | |
282 | fprintf(fp, "(%u)", spi); | |
283 | fprintf(fp, " "); | |
284 | } | |
c7699875 | 285 | |
ad273962 | 286 | fprintf(fp, "reqid %u", reqid); |
287 | if (show_stats > 0) | |
288 | fprintf(fp, "(0x%08x)", reqid); | |
289 | fprintf(fp, " "); | |
c7699875 | 290 | |
7809c616 | 291 | fprintf(fp, "mode "); |
292 | switch (mode) { | |
7ea4f5d3 | 293 | case XFRM_MODE_TRANSPORT: |
7809c616 | 294 | fprintf(fp, "transport"); |
295 | break; | |
7ea4f5d3 | 296 | case XFRM_MODE_TUNNEL: |
7809c616 | 297 | fprintf(fp, "tunnel"); |
298 | break; | |
7ea4f5d3 MN |
299 | case XFRM_MODE_ROUTEOPTIMIZATION: |
300 | fprintf(fp, "ro"); | |
301 | break; | |
302 | case XFRM_MODE_IN_TRIGGER: | |
303 | fprintf(fp, "in_trigger"); | |
304 | break; | |
305 | case XFRM_MODE_BEET: | |
34e099e2 SH |
306 | fprintf(fp, "beet"); |
307 | break; | |
7809c616 | 308 | default: |
309 | fprintf(fp, "%u", mode); | |
310 | break; | |
311 | } | |
312 | fprintf(fp, "%s", _SL_); | |
c7699875 | 313 | } |
314 | ||
315 | static const char *strxf_limit(__u64 limit) | |
316 | { | |
317 | static char str[32]; | |
318 | if (limit == XFRM_INF) | |
319 | strcpy(str, "(INF)"); | |
320 | else | |
b906243b | 321 | sprintf(str, "%llu", (unsigned long long) limit); |
c7699875 | 322 | |
323 | return str; | |
324 | } | |
325 | ||
326 | void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix) | |
327 | { | |
328 | if (prefix) | |
329 | fprintf(fp, prefix); | |
7809c616 | 330 | fprintf(fp, "stats:"); |
331 | fprintf(fp, "%s", _SL_); | |
c7699875 | 332 | |
333 | if (prefix) | |
334 | fprintf(fp, prefix); | |
335 | fprintf(fp, " "); | |
eaa34ee3 | 336 | fprintf(fp, "replay-window %u ", s->replay_window); |
337 | fprintf(fp, "replay %u ", s->replay); | |
338 | fprintf(fp, "failed %u", s->integrity_failed); | |
7809c616 | 339 | fprintf(fp, "%s", _SL_); |
c7699875 | 340 | } |
341 | ||
342 | static const char *strxf_time(__u64 time) | |
343 | { | |
344 | static char str[32]; | |
c7699875 | 345 | |
7809c616 | 346 | if (time == 0) |
347 | strcpy(str, "-"); | |
348 | else { | |
349 | time_t t; | |
350 | struct tm *tp; | |
351 | ||
ae665a52 | 352 | /* XXX: treat time in the same manner of kernel's |
7809c616 | 353 | * net/xfrm/xfrm_{user,state}.c |
354 | */ | |
c7699875 | 355 | t = (long)time; |
356 | tp = localtime(&t); | |
357 | ||
44d3eb25 | 358 | strftime(str, sizeof(str), "%Y-%m-%d %T", tp); |
c7699875 | 359 | } |
360 | ||
361 | return str; | |
362 | } | |
363 | ||
364 | void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg, | |
365 | struct xfrm_lifetime_cur *cur, | |
366 | FILE *fp, const char *prefix) | |
367 | { | |
368 | if (cfg) { | |
369 | if (prefix) | |
370 | fprintf(fp, prefix); | |
7809c616 | 371 | fprintf(fp, "lifetime config:"); |
372 | fprintf(fp, "%s", _SL_); | |
c7699875 | 373 | |
374 | if (prefix) | |
375 | fprintf(fp, prefix); | |
376 | fprintf(fp, " "); | |
377 | fprintf(fp, "limit: "); | |
378 | fprintf(fp, "soft "); | |
379 | fprintf(fp, strxf_limit(cfg->soft_byte_limit)); | |
380 | fprintf(fp, "(bytes), hard "); | |
381 | fprintf(fp, strxf_limit(cfg->hard_byte_limit)); | |
7809c616 | 382 | fprintf(fp, "(bytes)"); |
383 | fprintf(fp, "%s", _SL_); | |
c7699875 | 384 | |
385 | if (prefix) | |
386 | fprintf(fp, prefix); | |
387 | fprintf(fp, " "); | |
388 | fprintf(fp, "limit: "); | |
389 | fprintf(fp, "soft "); | |
390 | fprintf(fp, strxf_limit(cfg->soft_packet_limit)); | |
391 | fprintf(fp, "(packets), hard "); | |
392 | fprintf(fp, strxf_limit(cfg->hard_packet_limit)); | |
7809c616 | 393 | fprintf(fp, "(packets)"); |
394 | fprintf(fp, "%s", _SL_); | |
c7699875 | 395 | |
396 | if (prefix) | |
397 | fprintf(fp, prefix); | |
398 | fprintf(fp, " "); | |
399 | fprintf(fp, "expire add: "); | |
400 | fprintf(fp, "soft "); | |
b906243b | 401 | fprintf(fp, "%llu", (unsigned long long) cfg->soft_add_expires_seconds); |
c7699875 | 402 | fprintf(fp, "(sec), hard "); |
b906243b | 403 | fprintf(fp, "%llu", (unsigned long long) cfg->hard_add_expires_seconds); |
7809c616 | 404 | fprintf(fp, "(sec)"); |
405 | fprintf(fp, "%s", _SL_); | |
c7699875 | 406 | |
407 | if (prefix) | |
408 | fprintf(fp, prefix); | |
409 | fprintf(fp, " "); | |
410 | fprintf(fp, "expire use: "); | |
411 | fprintf(fp, "soft "); | |
b906243b | 412 | fprintf(fp, "%llu", (unsigned long long) cfg->soft_use_expires_seconds); |
c7699875 | 413 | fprintf(fp, "(sec), hard "); |
b906243b | 414 | fprintf(fp, "%llu", (unsigned long long) cfg->hard_use_expires_seconds); |
7809c616 | 415 | fprintf(fp, "(sec)"); |
416 | fprintf(fp, "%s", _SL_); | |
c7699875 | 417 | } |
418 | if (cur) { | |
419 | if (prefix) | |
420 | fprintf(fp, prefix); | |
7809c616 | 421 | fprintf(fp, "lifetime current:"); |
422 | fprintf(fp, "%s", _SL_); | |
c7699875 | 423 | |
424 | if (prefix) | |
425 | fprintf(fp, prefix); | |
426 | fprintf(fp, " "); | |
b906243b | 427 | fprintf(fp, "%llu(bytes), ", (unsigned long long) cur->bytes); |
428 | fprintf(fp, "%llu(packets)", (unsigned long long) cur->packets); | |
7809c616 | 429 | fprintf(fp, "%s", _SL_); |
430 | ||
c7699875 | 431 | if (prefix) |
432 | fprintf(fp, prefix); | |
433 | fprintf(fp, " "); | |
434 | fprintf(fp, "add %s ", strxf_time(cur->add_time)); | |
435 | fprintf(fp, "use %s", strxf_time(cur->use_time)); | |
7809c616 | 436 | fprintf(fp, "%s", _SL_); |
c7699875 | 437 | } |
438 | } | |
439 | ||
440 | void xfrm_selector_print(struct xfrm_selector *sel, __u16 family, | |
441 | FILE *fp, const char *prefix) | |
442 | { | |
443 | char abuf[256]; | |
444 | __u16 f; | |
445 | ||
446 | f = sel->family; | |
447 | if (f == AF_UNSPEC) | |
448 | f = family; | |
449 | if (f == AF_UNSPEC) | |
450 | f = preferred_family; | |
451 | ||
452 | if (prefix) | |
453 | fprintf(fp, prefix); | |
454 | ||
455 | memset(abuf, '\0', sizeof(abuf)); | |
eaa34ee3 | 456 | fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr), |
ad273962 | 457 | &sel->saddr, abuf, sizeof(abuf)), |
458 | sel->prefixlen_s); | |
c7699875 | 459 | |
460 | memset(abuf, '\0', sizeof(abuf)); | |
eaa34ee3 | 461 | fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr), |
ad273962 | 462 | &sel->daddr, abuf, sizeof(abuf)), |
463 | sel->prefixlen_d); | |
c7699875 | 464 | |
7809c616 | 465 | if (sel->proto) |
466 | fprintf(fp, "proto %s ", strxf_proto(sel->proto)); | |
c70b36d2 | 467 | switch (sel->proto) { |
468 | case IPPROTO_TCP: | |
469 | case IPPROTO_UDP: | |
470 | case IPPROTO_SCTP: | |
27356a5e | 471 | case IPPROTO_DCCP: |
c70b36d2 | 472 | default: /* XXX */ |
473 | if (sel->sport_mask) | |
474 | fprintf(fp, "sport %u ", ntohs(sel->sport)); | |
475 | if (sel->dport_mask) | |
476 | fprintf(fp, "dport %u ", ntohs(sel->dport)); | |
477 | break; | |
478 | case IPPROTO_ICMP: | |
479 | case IPPROTO_ICMPV6: | |
480 | /* type/code is stored at sport/dport in selector */ | |
481 | if (sel->sport_mask) | |
482 | fprintf(fp, "type %u ", ntohs(sel->sport)); | |
483 | if (sel->dport_mask) | |
484 | fprintf(fp, "code %u ", ntohs(sel->dport)); | |
485 | break; | |
0bf0fbc4 MN |
486 | case IPPROTO_MH: |
487 | if (sel->sport_mask) | |
488 | fprintf(fp, "type %u ", ntohs(sel->sport)); | |
489 | if (sel->dport_mask) { | |
490 | if (show_stats > 0) | |
491 | fprintf(fp, "(dport) 0x%.4x ", sel->dport); | |
492 | } | |
493 | break; | |
c70b36d2 | 494 | } |
c7699875 | 495 | |
496 | if (sel->ifindex > 0) { | |
44d3eb25 | 497 | char buf[IFNAMSIZ]; |
c7699875 | 498 | |
499 | memset(buf, '\0', sizeof(buf)); | |
500 | if_indextoname(sel->ifindex, buf); | |
501 | fprintf(fp, "dev %s ", buf); | |
ad273962 | 502 | } |
c7699875 | 503 | |
ad273962 | 504 | if (show_stats > 0) |
505 | fprintf(fp, "uid %u", sel->user); | |
7809c616 | 506 | |
507 | fprintf(fp, "%s", _SL_); | |
c7699875 | 508 | } |
509 | ||
eaa34ee3 | 510 | static void xfrm_algo_print(struct xfrm_algo *algo, int type, int len, |
511 | FILE *fp, const char *prefix) | |
c7699875 | 512 | { |
eaa34ee3 | 513 | int keylen; |
c7699875 | 514 | int i; |
515 | ||
516 | if (prefix) | |
517 | fprintf(fp, prefix); | |
518 | ||
7809c616 | 519 | fprintf(fp, "%s ", strxf_algotype(type)); |
eaa34ee3 | 520 | |
521 | if (len < sizeof(*algo)) { | |
522 | fprintf(fp, "(ERROR truncated)"); | |
523 | goto fin; | |
524 | } | |
525 | len -= sizeof(*algo); | |
526 | ||
7809c616 | 527 | fprintf(fp, "%s ", algo->alg_name); |
c7699875 | 528 | |
eaa34ee3 | 529 | keylen = algo->alg_key_len / 8; |
530 | if (len < keylen) { | |
531 | fprintf(fp, "(ERROR truncated)"); | |
532 | goto fin; | |
533 | } | |
534 | ||
7809c616 | 535 | fprintf(fp, "0x"); |
eaa34ee3 | 536 | for (i = 0; i < keylen; i ++) |
7809c616 | 537 | fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]); |
c7699875 | 538 | |
7809c616 | 539 | if (show_stats > 0) |
540 | fprintf(fp, " (%d bits)", algo->alg_key_len); | |
541 | ||
eaa34ee3 | 542 | fin: |
7809c616 | 543 | fprintf(fp, "%s", _SL_); |
c7699875 | 544 | } |
545 | ||
eaa34ee3 | 546 | static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len, |
c7699875 | 547 | __u16 family, FILE *fp, const char *prefix) |
548 | { | |
eaa34ee3 | 549 | int ntmpls = len / sizeof(struct xfrm_user_tmpl); |
c7699875 | 550 | int i; |
551 | ||
eaa34ee3 | 552 | if (ntmpls <= 0) { |
553 | if (prefix) | |
554 | fprintf(fp, prefix); | |
555 | fprintf(fp, "(ERROR \"tmpl\" truncated)"); | |
556 | fprintf(fp, "%s", _SL_); | |
557 | return; | |
558 | } | |
559 | ||
c7699875 | 560 | for (i = 0; i < ntmpls; i++) { |
561 | struct xfrm_user_tmpl *tmpl = &tmpls[i]; | |
562 | ||
563 | if (prefix) | |
564 | fprintf(fp, prefix); | |
ad273962 | 565 | |
c7699875 | 566 | xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode, |
15ac4cdc | 567 | tmpl->reqid, family, 0, fp, prefix, "tmpl "); |
eaa34ee3 | 568 | |
569 | if (show_stats > 0 || tmpl->optional) { | |
570 | if (prefix) | |
571 | fprintf(fp, prefix); | |
572 | fprintf(fp, "\t"); | |
573 | switch (tmpl->optional) { | |
574 | case 0: | |
575 | if (show_stats > 0) | |
576 | fprintf(fp, "level required "); | |
577 | break; | |
578 | case 1: | |
579 | fprintf(fp, "level use "); | |
580 | break; | |
581 | default: | |
582 | fprintf(fp, "level %u ", tmpl->optional); | |
583 | break; | |
584 | } | |
c7699875 | 585 | |
7809c616 | 586 | if (show_stats > 0) |
eaa34ee3 | 587 | fprintf(fp, "share %s ", strxf_share(tmpl->share)); |
588 | ||
589 | fprintf(fp, "%s", _SL_); | |
ad273962 | 590 | } |
ad273962 | 591 | |
592 | if (show_stats > 0) { | |
eaa34ee3 | 593 | if (prefix) |
594 | fprintf(fp, prefix); | |
595 | fprintf(fp, "\t"); | |
596 | fprintf(fp, "%s-mask %s ", | |
7809c616 | 597 | strxf_algotype(XFRMA_ALG_CRYPT), |
eaa34ee3 | 598 | strxf_mask32(tmpl->ealgos)); |
599 | fprintf(fp, "%s-mask %s ", | |
7809c616 | 600 | strxf_algotype(XFRMA_ALG_AUTH), |
eaa34ee3 | 601 | strxf_mask32(tmpl->aalgos)); |
602 | fprintf(fp, "%s-mask %s", | |
7809c616 | 603 | strxf_algotype(XFRMA_ALG_COMP), |
eaa34ee3 | 604 | strxf_mask32(tmpl->calgos)); |
605 | ||
606 | fprintf(fp, "%s", _SL_); | |
ad273962 | 607 | } |
c7699875 | 608 | } |
609 | } | |
610 | ||
bcf32819 | 611 | void xfrm_xfrma_print(struct rtattr *tb[], __u16 family, |
c7699875 | 612 | FILE *fp, const char *prefix) |
613 | { | |
bcf32819 | 614 | if (tb[XFRMA_ALG_AUTH]) { |
615 | struct rtattr *rta = tb[XFRMA_ALG_AUTH]; | |
616 | xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta), | |
5cf576d9 | 617 | XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix); |
bcf32819 | 618 | } |
c7699875 | 619 | |
bcf32819 | 620 | if (tb[XFRMA_ALG_CRYPT]) { |
621 | struct rtattr *rta = tb[XFRMA_ALG_CRYPT]; | |
622 | xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta), | |
5cf576d9 | 623 | XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix); |
bcf32819 | 624 | } |
eaa34ee3 | 625 | |
bcf32819 | 626 | if (tb[XFRMA_ALG_COMP]) { |
627 | struct rtattr *rta = tb[XFRMA_ALG_COMP]; | |
628 | xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta), | |
5cf576d9 | 629 | XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix); |
bcf32819 | 630 | } |
eaa34ee3 | 631 | |
bcf32819 | 632 | if (tb[XFRMA_ENCAP]) { |
633 | struct xfrm_encap_tmpl *e; | |
634 | char abuf[256]; | |
eaa34ee3 | 635 | |
bcf32819 | 636 | if (prefix) |
637 | fprintf(fp, prefix); | |
638 | fprintf(fp, "encap "); | |
eaa34ee3 | 639 | |
bcf32819 | 640 | if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) { |
641 | fprintf(fp, "(ERROR truncated)"); | |
7809c616 | 642 | fprintf(fp, "%s", _SL_); |
bcf32819 | 643 | return; |
c7699875 | 644 | } |
bcf32819 | 645 | e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]); |
646 | ||
5cf576d9 SH |
647 | fprintf(fp, "type "); |
648 | switch (e->encap_type) { | |
649 | case 1: | |
650 | fprintf(fp, "espinudp-nonike "); | |
651 | break; | |
652 | case 2: | |
653 | fprintf(fp, "espinudp "); | |
654 | break; | |
655 | default: | |
656 | fprintf(fp, "%u ", e->encap_type); | |
657 | break; | |
658 | } | |
bcf32819 | 659 | fprintf(fp, "sport %u ", ntohs(e->encap_sport)); |
660 | fprintf(fp, "dport %u ", ntohs(e->encap_dport)); | |
661 | ||
662 | memset(abuf, '\0', sizeof(abuf)); | |
663 | fprintf(fp, "addr %s", | |
664 | rt_addr_n2a(family, sizeof(e->encap_oa), | |
665 | &e->encap_oa, abuf, sizeof(abuf))); | |
666 | fprintf(fp, "%s", _SL_); | |
667 | } | |
668 | ||
669 | if (tb[XFRMA_TMPL]) { | |
670 | struct rtattr *rta = tb[XFRMA_TMPL]; | |
671 | xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta), | |
5cf576d9 | 672 | RTA_PAYLOAD(rta), family, fp, prefix); |
c7699875 | 673 | } |
7ea4f5d3 MN |
674 | |
675 | if (tb[XFRMA_COADDR]) { | |
676 | char abuf[256]; | |
677 | xfrm_address_t *coa; | |
678 | ||
679 | if (prefix) | |
680 | fprintf(fp, prefix); | |
681 | fprintf(fp, "coa "); | |
682 | ||
683 | coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]); | |
684 | ||
685 | if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) { | |
686 | fprintf(fp, "(ERROR truncated)"); | |
687 | fprintf(fp, "%s", _SL_); | |
688 | return; | |
689 | } | |
690 | ||
691 | memset(abuf, '\0', sizeof(abuf)); | |
692 | fprintf(fp, "%s", | |
ae665a52 | 693 | rt_addr_n2a(family, sizeof(*coa), coa, |
7ea4f5d3 MN |
694 | abuf, sizeof(abuf))); |
695 | fprintf(fp, "%s", _SL_); | |
696 | } | |
697 | ||
698 | if (tb[XFRMA_LASTUSED]) { | |
699 | __u64 lastused; | |
700 | ||
701 | if (prefix) | |
702 | fprintf(fp, prefix); | |
703 | fprintf(fp, "lastused "); | |
704 | ||
705 | if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) { | |
706 | fprintf(fp, "(ERROR truncated)"); | |
707 | fprintf(fp, "%s", _SL_); | |
708 | return; | |
709 | } | |
710 | ||
711 | lastused = *(__u64 *)RTA_DATA(tb[XFRMA_LASTUSED]); | |
712 | ||
713 | fprintf(fp, "%s", strxf_time(lastused)); | |
714 | fprintf(fp, "%s", _SL_); | |
715 | } | |
c7699875 | 716 | } |
717 | ||
15ac4cdc | 718 | static int xfrm_selector_iszero(struct xfrm_selector *s) |
719 | { | |
720 | struct xfrm_selector s0; | |
721 | ||
722 | memset(&s0, 0, sizeof(s0)); | |
723 | ||
724 | return (memcmp(&s0, s, sizeof(s0)) == 0); | |
725 | } | |
726 | ||
727 | void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo, | |
728 | struct rtattr *tb[], FILE *fp, const char *prefix, | |
729 | const char *title) | |
730 | { | |
731 | char buf[STRBUF_SIZE]; | |
7ea4f5d3 | 732 | int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto); |
15ac4cdc | 733 | |
734 | memset(buf, '\0', sizeof(buf)); | |
735 | ||
736 | xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode, | |
7ea4f5d3 MN |
737 | xsinfo->reqid, xsinfo->family, force_spi, fp, |
738 | prefix, title); | |
15ac4cdc | 739 | |
740 | if (prefix) | |
741 | STRBUF_CAT(buf, prefix); | |
742 | STRBUF_CAT(buf, "\t"); | |
743 | ||
744 | fprintf(fp, buf); | |
745 | fprintf(fp, "replay-window %u ", xsinfo->replay_window); | |
746 | if (show_stats > 0) | |
747 | fprintf(fp, "seq 0x%08u ", xsinfo->seq); | |
748 | if (show_stats > 0 || xsinfo->flags) { | |
749 | __u8 flags = xsinfo->flags; | |
750 | ||
751 | fprintf(fp, "flag "); | |
752 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn"); | |
753 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp"); | |
7ea4f5d3 | 754 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv"); |
15ac4cdc | 755 | if (flags) |
756 | fprintf(fp, "%x", flags); | |
757 | if (show_stats > 0) | |
758 | fprintf(fp, " (0x%s)", strxf_mask8(flags)); | |
759 | } | |
760 | fprintf(fp, "%s", _SL_); | |
761 | ||
762 | xfrm_xfrma_print(tb, xsinfo->family, fp, buf); | |
763 | ||
764 | if (!xfrm_selector_iszero(&xsinfo->sel)) { | |
765 | char sbuf[STRBUF_SIZE]; | |
766 | ||
767 | memcpy(sbuf, buf, sizeof(sbuf)); | |
768 | STRBUF_CAT(sbuf, "sel "); | |
769 | ||
770 | xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf); | |
771 | } | |
772 | ||
773 | if (show_stats > 0) { | |
774 | xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf); | |
775 | xfrm_stats_print(&xsinfo->stats, fp, buf); | |
776 | } | |
777 | } | |
778 | ||
779 | void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo, | |
780 | struct rtattr *tb[], FILE *fp, const char *prefix, | |
781 | const char *title) | |
782 | { | |
783 | char buf[STRBUF_SIZE]; | |
972938e9 | 784 | __u8 ptype = XFRM_POLICY_TYPE_MAIN; |
15ac4cdc | 785 | |
786 | memset(buf, '\0', sizeof(buf)); | |
787 | ||
788 | xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title); | |
789 | ||
790 | if (prefix) | |
791 | STRBUF_CAT(buf, prefix); | |
792 | STRBUF_CAT(buf, "\t"); | |
793 | ||
794 | fprintf(fp, buf); | |
795 | fprintf(fp, "dir "); | |
796 | switch (xpinfo->dir) { | |
797 | case XFRM_POLICY_IN: | |
798 | fprintf(fp, "in"); | |
799 | break; | |
800 | case XFRM_POLICY_OUT: | |
801 | fprintf(fp, "out"); | |
802 | break; | |
803 | case XFRM_POLICY_FWD: | |
804 | fprintf(fp, "fwd"); | |
805 | break; | |
806 | default: | |
807 | fprintf(fp, "%u", xpinfo->dir); | |
808 | break; | |
809 | } | |
810 | fprintf(fp, " "); | |
811 | ||
812 | switch (xpinfo->action) { | |
813 | case XFRM_POLICY_ALLOW: | |
814 | if (show_stats > 0) | |
815 | fprintf(fp, "action allow "); | |
816 | break; | |
817 | case XFRM_POLICY_BLOCK: | |
818 | fprintf(fp, "action block "); | |
819 | break; | |
820 | default: | |
821 | fprintf(fp, "action %u ", xpinfo->action); | |
822 | break; | |
823 | } | |
824 | ||
825 | if (show_stats) | |
826 | fprintf(fp, "index %u ", xpinfo->index); | |
827 | fprintf(fp, "priority %u ", xpinfo->priority); | |
972938e9 MN |
828 | |
829 | fprintf(fp, "ptype "); | |
830 | ||
831 | if (tb[XFRMA_POLICY_TYPE]) { | |
832 | struct xfrm_userpolicy_type *upt; | |
833 | ||
834 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) | |
835 | fprintf(fp, "(ERROR truncated)"); | |
836 | ||
837 | upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); | |
838 | ptype = upt->type; | |
839 | } | |
840 | ||
841 | switch (ptype) { | |
842 | case XFRM_POLICY_TYPE_MAIN: | |
843 | fprintf(fp, "main"); | |
844 | break; | |
845 | case XFRM_POLICY_TYPE_SUB: | |
846 | fprintf(fp, "sub"); | |
847 | break; | |
848 | default: | |
849 | fprintf(fp, "%u", ptype); | |
850 | break; | |
851 | } | |
852 | fprintf(fp, " "); | |
853 | ||
15ac4cdc | 854 | if (show_stats > 0) { |
855 | fprintf(fp, "share %s ", strxf_share(xpinfo->share)); | |
856 | fprintf(fp, "flag 0x%s", strxf_mask8(xpinfo->flags)); | |
857 | } | |
858 | fprintf(fp, "%s", _SL_); | |
859 | ||
860 | if (show_stats > 0) | |
861 | xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf); | |
862 | ||
863 | xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf); | |
864 | } | |
865 | ||
c7699875 | 866 | int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family, |
7809c616 | 867 | int loose, int *argcp, char ***argvp) |
c7699875 | 868 | { |
869 | int argc = *argcp; | |
870 | char **argv = *argvp; | |
871 | inet_prefix dst; | |
872 | inet_prefix src; | |
c7699875 | 873 | |
874 | memset(&dst, 0, sizeof(dst)); | |
875 | memset(&src, 0, sizeof(src)); | |
876 | ||
877 | while (1) { | |
878 | if (strcmp(*argv, "src") == 0) { | |
879 | NEXT_ARG(); | |
880 | ||
881 | get_prefix(&src, *argv, preferred_family); | |
882 | if (src.family == AF_UNSPEC) | |
eaa34ee3 | 883 | invarg("\"src\" address family is AF_UNSPEC", *argv); |
c7699875 | 884 | if (family) |
885 | *family = src.family; | |
886 | ||
887 | memcpy(saddr, &src.data, sizeof(*saddr)); | |
888 | ||
889 | filter.id_src_mask = src.bitlen; | |
890 | ||
891 | } else if (strcmp(*argv, "dst") == 0) { | |
892 | NEXT_ARG(); | |
893 | ||
894 | get_prefix(&dst, *argv, preferred_family); | |
895 | if (dst.family == AF_UNSPEC) | |
eaa34ee3 | 896 | invarg("\"dst\" address family is AF_UNSPEC", *argv); |
c7699875 | 897 | if (family) |
898 | *family = dst.family; | |
899 | ||
900 | memcpy(&id->daddr, &dst.data, sizeof(id->daddr)); | |
901 | ||
902 | filter.id_dst_mask = dst.bitlen; | |
903 | ||
904 | } else if (strcmp(*argv, "proto") == 0) { | |
29aa4dd7 | 905 | int ret; |
c7699875 | 906 | |
907 | NEXT_ARG(); | |
908 | ||
29aa4dd7 | 909 | ret = xfrm_xfrmproto_getbyname(*argv); |
910 | if (ret < 0) | |
911 | invarg("\"XFRM_PROTO\" is invalid", *argv); | |
c7699875 | 912 | |
29aa4dd7 | 913 | id->proto = (__u8)ret; |
c7699875 | 914 | |
915 | filter.id_proto_mask = XFRM_FILTER_MASK_FULL; | |
916 | ||
917 | } else if (strcmp(*argv, "spi") == 0) { | |
918 | __u32 spi; | |
919 | ||
920 | NEXT_ARG(); | |
921 | if (get_u32(&spi, *argv, 0)) | |
922 | invarg("\"SPI\" is invalid", *argv); | |
923 | ||
924 | spi = htonl(spi); | |
925 | id->spi = spi; | |
926 | ||
927 | filter.id_spi_mask = XFRM_FILTER_MASK_FULL; | |
928 | ||
929 | } else { | |
930 | PREV_ARG(); /* back track */ | |
931 | break; | |
932 | } | |
933 | ||
934 | if (!NEXT_ARG_OK()) | |
935 | break; | |
936 | NEXT_ARG(); | |
937 | } | |
938 | ||
939 | if (src.family && dst.family && (src.family != dst.family)) | |
eaa34ee3 | 940 | invarg("the same address family is required between \"src\" and \"dst\"", *argv); |
c7699875 | 941 | |
29aa4dd7 | 942 | if (loose == 0 && id->proto == 0) |
943 | missarg("XFRM_PROTO"); | |
c7699875 | 944 | if (argc == *argcp) |
945 | missarg("ID"); | |
946 | ||
947 | *argcp = argc; | |
948 | *argvp = argv; | |
949 | ||
950 | return 0; | |
951 | } | |
952 | ||
953 | int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp) | |
954 | { | |
955 | int argc = *argcp; | |
956 | char **argv = *argvp; | |
957 | ||
958 | if (matches(*argv, "transport") == 0) | |
7ea4f5d3 | 959 | *mode = XFRM_MODE_TRANSPORT; |
c7699875 | 960 | else if (matches(*argv, "tunnel") == 0) |
7ea4f5d3 MN |
961 | *mode = XFRM_MODE_TUNNEL; |
962 | else if (matches(*argv, "ro") == 0) | |
963 | *mode = XFRM_MODE_ROUTEOPTIMIZATION; | |
964 | else if (matches(*argv, "in_trigger") == 0) | |
965 | *mode = XFRM_MODE_IN_TRIGGER; | |
34e099e2 | 966 | else if (matches(*argv, "beet") == 0) |
7ea4f5d3 | 967 | *mode = XFRM_MODE_BEET; |
c7699875 | 968 | else |
969 | invarg("\"MODE\" is invalid", *argv); | |
970 | ||
971 | *argcp = argc; | |
972 | *argvp = argv; | |
973 | ||
974 | return 0; | |
975 | } | |
976 | ||
5cf576d9 SH |
977 | int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp) |
978 | { | |
979 | int argc = *argcp; | |
980 | char **argv = *argvp; | |
981 | ||
982 | if (strcmp(*argv, "espinudp-nonike") == 0) | |
983 | *type = 1; | |
984 | else if (strcmp(*argv, "espinudp") == 0) | |
985 | *type = 2; | |
986 | else | |
987 | invarg("\"ENCAP-TYPE\" is invalid", *argv); | |
988 | ||
989 | *argcp = argc; | |
990 | *argvp = argv; | |
991 | ||
992 | return 0; | |
993 | } | |
994 | ||
c7699875 | 995 | /* NOTE: reqid is used by host-byte order */ |
996 | int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp) | |
997 | { | |
998 | int argc = *argcp; | |
999 | char **argv = *argvp; | |
1000 | ||
1001 | if (get_u32(reqid, *argv, 0)) | |
1002 | invarg("\"REQID\" is invalid", *argv); | |
1003 | ||
1004 | *argcp = argc; | |
1005 | *argvp = argv; | |
1006 | ||
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | static int xfrm_selector_upspec_parse(struct xfrm_selector *sel, | |
1011 | int *argcp, char ***argvp) | |
1012 | { | |
1013 | int argc = *argcp; | |
1014 | char **argv = *argvp; | |
c70b36d2 | 1015 | char *sportp = NULL; |
1016 | char *dportp = NULL; | |
1017 | char *typep = NULL; | |
1018 | char *codep = NULL; | |
c7699875 | 1019 | |
1020 | while (1) { | |
1021 | if (strcmp(*argv, "proto") == 0) { | |
7809c616 | 1022 | __u8 upspec; |
1023 | ||
c7699875 | 1024 | NEXT_ARG(); |
1025 | ||
1026 | if (strcmp(*argv, "any") == 0) | |
1027 | upspec = 0; | |
1028 | else { | |
1029 | struct protoent *pp; | |
1030 | pp = getprotobyname(*argv); | |
1031 | if (pp) | |
1032 | upspec = pp->p_proto; | |
1033 | else { | |
1034 | if (get_u8(&upspec, *argv, 0)) | |
7809c616 | 1035 | invarg("\"PROTO\" is invalid", *argv); |
c7699875 | 1036 | } |
1037 | } | |
1038 | sel->proto = upspec; | |
1039 | ||
1040 | filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL; | |
1041 | ||
1042 | } else if (strcmp(*argv, "sport") == 0) { | |
c70b36d2 | 1043 | sportp = *argv; |
1044 | ||
c7699875 | 1045 | NEXT_ARG(); |
1046 | ||
1047 | if (get_u16(&sel->sport, *argv, 0)) | |
1048 | invarg("\"PORT\" is invalid", *argv); | |
1049 | sel->sport = htons(sel->sport); | |
1050 | if (sel->sport) | |
1051 | sel->sport_mask = ~((__u16)0); | |
1052 | ||
1053 | filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL; | |
1054 | ||
1055 | } else if (strcmp(*argv, "dport") == 0) { | |
c70b36d2 | 1056 | dportp = *argv; |
1057 | ||
c7699875 | 1058 | NEXT_ARG(); |
1059 | ||
1060 | if (get_u16(&sel->dport, *argv, 0)) | |
1061 | invarg("\"PORT\" is invalid", *argv); | |
1062 | sel->dport = htons(sel->dport); | |
1063 | if (sel->dport) | |
1064 | sel->dport_mask = ~((__u16)0); | |
1065 | ||
1066 | filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; | |
1067 | ||
c70b36d2 | 1068 | } else if (strcmp(*argv, "type") == 0) { |
1069 | typep = *argv; | |
1070 | ||
1071 | NEXT_ARG(); | |
1072 | ||
1073 | if (get_u16(&sel->sport, *argv, 0) || | |
1074 | (sel->sport & ~((__u16)0xff))) | |
1075 | invarg("\"type\" value is invalid", *argv); | |
1076 | sel->sport = htons(sel->sport); | |
1077 | sel->sport_mask = ~((__u16)0); | |
1078 | ||
1079 | filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL; | |
1080 | ||
1081 | ||
1082 | } else if (strcmp(*argv, "code") == 0) { | |
1083 | codep = *argv; | |
1084 | ||
1085 | NEXT_ARG(); | |
1086 | ||
1087 | if (get_u16(&sel->dport, *argv, 0) || | |
1088 | (sel->dport & ~((__u16)0xff))) | |
1089 | invarg("\"code\" value is invalid", *argv); | |
1090 | sel->dport = htons(sel->dport); | |
1091 | sel->dport_mask = ~((__u16)0); | |
1092 | ||
1093 | filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; | |
1094 | ||
c7699875 | 1095 | } else { |
1096 | PREV_ARG(); /* back track */ | |
1097 | break; | |
1098 | } | |
1099 | ||
1100 | if (!NEXT_ARG_OK()) | |
1101 | break; | |
1102 | NEXT_ARG(); | |
1103 | } | |
1104 | if (argc == *argcp) | |
1105 | missarg("UPSPEC"); | |
c70b36d2 | 1106 | if (sportp || dportp) { |
1107 | switch (sel->proto) { | |
1108 | case IPPROTO_TCP: | |
1109 | case IPPROTO_UDP: | |
1110 | case IPPROTO_SCTP: | |
27356a5e | 1111 | case IPPROTO_DCCP: |
c70b36d2 | 1112 | break; |
1113 | default: | |
1114 | fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto)); | |
1115 | exit(1); | |
1116 | } | |
1117 | } | |
1118 | if (typep || codep) { | |
1119 | switch (sel->proto) { | |
1120 | case IPPROTO_ICMP: | |
1121 | case IPPROTO_ICMPV6: | |
0bf0fbc4 | 1122 | case IPPROTO_MH: |
c70b36d2 | 1123 | break; |
1124 | default: | |
1125 | fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto)); | |
1126 | exit(1); | |
1127 | } | |
1128 | } | |
c7699875 | 1129 | |
1130 | *argcp = argc; | |
1131 | *argvp = argv; | |
1132 | ||
1133 | return 0; | |
1134 | } | |
1135 | ||
1136 | int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp) | |
1137 | { | |
1138 | int argc = *argcp; | |
1139 | char **argv = *argvp; | |
1140 | inet_prefix dst; | |
1141 | inet_prefix src; | |
7809c616 | 1142 | char *upspecp = NULL; |
c7699875 | 1143 | |
1144 | memset(&dst, 0, sizeof(dst)); | |
1145 | memset(&src, 0, sizeof(src)); | |
1146 | ||
1147 | while (1) { | |
1148 | if (strcmp(*argv, "src") == 0) { | |
1149 | NEXT_ARG(); | |
1150 | ||
1151 | get_prefix(&src, *argv, preferred_family); | |
1152 | if (src.family == AF_UNSPEC) | |
eaa34ee3 | 1153 | invarg("\"src\" address family is AF_UNSPEC", *argv); |
c7699875 | 1154 | sel->family = src.family; |
1155 | ||
1156 | memcpy(&sel->saddr, &src.data, sizeof(sel->saddr)); | |
1157 | sel->prefixlen_s = src.bitlen; | |
1158 | ||
1159 | filter.sel_src_mask = src.bitlen; | |
1160 | ||
1161 | } else if (strcmp(*argv, "dst") == 0) { | |
1162 | NEXT_ARG(); | |
1163 | ||
1164 | get_prefix(&dst, *argv, preferred_family); | |
1165 | if (dst.family == AF_UNSPEC) | |
eaa34ee3 | 1166 | invarg("\"dst\" address family is AF_UNSPEC", *argv); |
c7699875 | 1167 | sel->family = dst.family; |
1168 | ||
1169 | memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr)); | |
1170 | sel->prefixlen_d = dst.bitlen; | |
1171 | ||
1172 | filter.sel_dst_mask = dst.bitlen; | |
1173 | ||
c7699875 | 1174 | } else if (strcmp(*argv, "dev") == 0) { |
1175 | int ifindex; | |
1176 | ||
1177 | NEXT_ARG(); | |
1178 | ||
1179 | if (strcmp(*argv, "none") == 0) | |
1180 | ifindex = 0; | |
1181 | else { | |
1182 | ifindex = if_nametoindex(*argv); | |
1183 | if (ifindex <= 0) | |
1184 | invarg("\"DEV\" is invalid", *argv); | |
1185 | } | |
1186 | sel->ifindex = ifindex; | |
1187 | ||
1188 | filter.sel_dev_mask = XFRM_FILTER_MASK_FULL; | |
1189 | ||
1190 | } else { | |
7809c616 | 1191 | if (upspecp) { |
1192 | PREV_ARG(); /* back track */ | |
1193 | break; | |
1194 | } else { | |
1195 | upspecp = *argv; | |
1196 | xfrm_selector_upspec_parse(sel, &argc, &argv); | |
1197 | } | |
c7699875 | 1198 | } |
1199 | ||
1200 | if (!NEXT_ARG_OK()) | |
1201 | break; | |
1202 | ||
1203 | NEXT_ARG(); | |
1204 | } | |
1205 | ||
1206 | if (src.family && dst.family && (src.family != dst.family)) | |
eaa34ee3 | 1207 | invarg("the same address family is required between \"src\" and \"dst\"", *argv); |
c7699875 | 1208 | |
1209 | if (argc == *argcp) | |
1210 | missarg("SELECTOR"); | |
1211 | ||
1212 | *argcp = argc; | |
1213 | *argvp = argv; | |
1214 | ||
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft, | |
1219 | int *argcp, char ***argvp) | |
1220 | { | |
1221 | int argc = *argcp; | |
1222 | char **argv = *argvp; | |
1223 | int ret; | |
1224 | ||
1225 | if (strcmp(*argv, "time-soft") == 0) { | |
1226 | NEXT_ARG(); | |
1227 | ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0); | |
1228 | if (ret) | |
1229 | invarg("\"time-soft\" value is invalid", *argv); | |
1230 | } else if (strcmp(*argv, "time-hard") == 0) { | |
1231 | NEXT_ARG(); | |
1232 | ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0); | |
1233 | if (ret) | |
1234 | invarg("\"time-hard\" value is invalid", *argv); | |
1235 | } else if (strcmp(*argv, "time-use-soft") == 0) { | |
1236 | NEXT_ARG(); | |
1237 | ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0); | |
1238 | if (ret) | |
1239 | invarg("\"time-use-soft\" value is invalid", *argv); | |
1240 | } else if (strcmp(*argv, "time-use-hard") == 0) { | |
1241 | NEXT_ARG(); | |
1242 | ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0); | |
1243 | if (ret) | |
1244 | invarg("\"time-use-hard\" value is invalid", *argv); | |
1245 | } else if (strcmp(*argv, "byte-soft") == 0) { | |
1246 | NEXT_ARG(); | |
1247 | ret = get_u64(&lft->soft_byte_limit, *argv, 0); | |
1248 | if (ret) | |
1249 | invarg("\"byte-soft\" value is invalid", *argv); | |
1250 | } else if (strcmp(*argv, "byte-hard") == 0) { | |
1251 | NEXT_ARG(); | |
1252 | ret = get_u64(&lft->hard_byte_limit, *argv, 0); | |
1253 | if (ret) | |
1254 | invarg("\"byte-hard\" value is invalid", *argv); | |
1255 | } else if (strcmp(*argv, "packet-soft") == 0) { | |
1256 | NEXT_ARG(); | |
1257 | ret = get_u64(&lft->soft_packet_limit, *argv, 0); | |
1258 | if (ret) | |
1259 | invarg("\"packet-soft\" value is invalid", *argv); | |
1260 | } else if (strcmp(*argv, "packet-hard") == 0) { | |
1261 | NEXT_ARG(); | |
1262 | ret = get_u64(&lft->hard_packet_limit, *argv, 0); | |
1263 | if (ret) | |
1264 | invarg("\"packet-hard\" value is invalid", *argv); | |
1265 | } else | |
1266 | invarg("\"LIMIT\" is invalid", *argv); | |
1267 | ||
1268 | *argcp = argc; | |
1269 | *argvp = argv; | |
1270 | ||
1271 | return 0; | |
1272 | } | |
1273 | ||
1274 | int do_xfrm(int argc, char **argv) | |
1275 | { | |
1276 | memset(&filter, 0, sizeof(filter)); | |
1277 | ||
1278 | if (argc < 1) | |
1279 | usage(); | |
1280 | ||
ad273962 | 1281 | if (matches(*argv, "state") == 0 || |
15ac4cdc | 1282 | matches(*argv, "sa") == 0) |
c7699875 | 1283 | return do_xfrm_state(argc-1, argv+1); |
15ac4cdc | 1284 | else if (matches(*argv, "policy") == 0) |
c7699875 | 1285 | return do_xfrm_policy(argc-1, argv+1); |
15ac4cdc | 1286 | else if (matches(*argv, "monitor") == 0) |
1287 | return do_xfrm_monitor(argc-1, argv+1); | |
ad273962 | 1288 | else if (matches(*argv, "help") == 0) { |
c7699875 | 1289 | usage(); |
1290 | fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv); | |
1291 | exit(-1); | |
1292 | } | |
1293 | usage(); | |
1294 | } |