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