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