net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1 | /* $USAGI: $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C)2004 USAGI/WIDE Project |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 5 | * |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 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. |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 10 | * |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 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. |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 15 | * |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 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> |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 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 | |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 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 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 55 | struct xfrm_filter filter; |
| 56 | |
| 57 | static void usage(void) __attribute__((noreturn)); |
| 58 | |
| 59 | static void usage(void) |
| 60 | { |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 61 | fprintf(stderr, |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 62 | "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n" |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 63 | "where XFRM_OBJECT := { state | policy | monitor }\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 64 | exit(-1); |
| 65 | } |
| 66 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 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 | |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 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 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 109 | struct typeent { |
| 110 | const char *t_name; |
| 111 | int t_type; |
| 112 | }; |
| 113 | |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 114 | static const struct typeent xfrmproto_types[]= { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 115 | { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP }, |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 116 | { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS }, |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 117 | { "ipsec-any", IPSEC_PROTO_ANY }, |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 118 | { NULL, -1 } |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 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 | { |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 139 | static char str[16]; |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 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 | |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 151 | sprintf(str, "%u", proto); |
| 152 | return str; |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 153 | } |
| 154 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 155 | static const struct typeent algo_types[]= { |
| 156 | { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH }, |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 157 | { "comp", XFRMA_ALG_COMP }, { "aead", XFRMA_ALG_AEAD }, |
| 158 | { NULL, -1 } |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 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 | { |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 179 | static char str[32]; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 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 | |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 191 | sprintf(str, "%d", type); |
| 192 | return str; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 193 | } |
| 194 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 195 | const char *strxf_mask8(__u8 mask) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 196 | { |
| 197 | static char str[16]; |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 198 | const int sn = sizeof(mask) * 8 - 1; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 199 | __u8 b; |
| 200 | int i = 0; |
| 201 | |
| 202 | for (b = (1 << sn); b > 0; b >>= 1) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 203 | str[i++] = ((b & mask) ? '1' : '0'); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 204 | str[i] = '\0'; |
| 205 | |
| 206 | return str; |
| 207 | } |
| 208 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 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 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 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: |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 236 | sprintf(str, "%u", share); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | |
| 240 | return str; |
| 241 | } |
| 242 | |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 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 { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 253 | sprintf(buf, "%u", proto); |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 254 | p = buf; |
| 255 | } |
| 256 | |
| 257 | return p; |
| 258 | } |
| 259 | |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 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 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 279 | void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id, |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 280 | __u8 mode, __u32 reqid, __u16 family, int force_spi, |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 281 | FILE *fp, const char *prefix, const char *title) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 282 | { |
| 283 | char abuf[256]; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 284 | |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 285 | if (title) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 286 | fputs(title, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 287 | |
| 288 | memset(abuf, '\0', sizeof(abuf)); |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 289 | fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr), |
| 290 | saddr, abuf, sizeof(abuf))); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 291 | memset(abuf, '\0', sizeof(abuf)); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 292 | fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr), |
| 293 | &id->daddr, abuf, sizeof(abuf))); |
| 294 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 295 | |
| 296 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 297 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 298 | fprintf(fp, "\t"); |
| 299 | |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 300 | fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto)); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 301 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 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 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 309 | |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 310 | fprintf(fp, "reqid %u", reqid); |
| 311 | if (show_stats > 0) |
| 312 | fprintf(fp, "(0x%08x)", reqid); |
| 313 | fprintf(fp, " "); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 314 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 315 | fprintf(fp, "mode "); |
| 316 | switch (mode) { |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 317 | case XFRM_MODE_TRANSPORT: |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 318 | fprintf(fp, "transport"); |
| 319 | break; |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 320 | case XFRM_MODE_TUNNEL: |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 321 | fprintf(fp, "tunnel"); |
| 322 | break; |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 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: |
Stephen Hemminger | 34e099e | 2006-10-19 13:15:35 -0700 | [diff] [blame] | 330 | fprintf(fp, "beet"); |
| 331 | break; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 332 | default: |
| 333 | fprintf(fp, "%u", mode); |
| 334 | break; |
| 335 | } |
| 336 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 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 |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 345 | sprintf(str, "%llu", (unsigned long long) limit); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 346 | |
| 347 | return str; |
| 348 | } |
| 349 | |
| 350 | void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix) |
| 351 | { |
| 352 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 353 | fputs(prefix, fp); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 354 | fprintf(fp, "stats:"); |
| 355 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 356 | |
| 357 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 358 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 359 | fprintf(fp, " "); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 360 | fprintf(fp, "replay-window %u ", s->replay_window); |
| 361 | fprintf(fp, "replay %u ", s->replay); |
| 362 | fprintf(fp, "failed %u", s->integrity_failed); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 363 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static const char *strxf_time(__u64 time) |
| 367 | { |
| 368 | static char str[32]; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 369 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 370 | if (time == 0) |
| 371 | strcpy(str, "-"); |
| 372 | else { |
| 373 | time_t t; |
| 374 | struct tm *tp; |
| 375 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 376 | /* XXX: treat time in the same manner of kernel's |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 377 | * net/xfrm/xfrm_{user,state}.c |
| 378 | */ |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 379 | t = (long)time; |
| 380 | tp = localtime(&t); |
| 381 | |
net[shemminger]!shemminger | 44d3eb2 | 2004-10-07 18:55:51 +0000 | [diff] [blame] | 382 | strftime(str, sizeof(str), "%Y-%m-%d %T", tp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return str; |
| 386 | } |
| 387 | |
| 388 | void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg, |
| 389 | struct xfrm_lifetime_cur *cur, |
| 390 | FILE *fp, const char *prefix) |
| 391 | { |
| 392 | if (cfg) { |
| 393 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 394 | fputs(prefix, fp); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 395 | fprintf(fp, "lifetime config:"); |
| 396 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 397 | |
| 398 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 399 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 400 | fprintf(fp, " "); |
| 401 | fprintf(fp, "limit: "); |
| 402 | fprintf(fp, "soft "); |
| 403 | fprintf(fp, strxf_limit(cfg->soft_byte_limit)); |
| 404 | fprintf(fp, "(bytes), hard "); |
| 405 | fprintf(fp, strxf_limit(cfg->hard_byte_limit)); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 406 | fprintf(fp, "(bytes)"); |
| 407 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 408 | |
| 409 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 410 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 411 | fprintf(fp, " "); |
| 412 | fprintf(fp, "limit: "); |
| 413 | fprintf(fp, "soft "); |
| 414 | fprintf(fp, strxf_limit(cfg->soft_packet_limit)); |
| 415 | fprintf(fp, "(packets), hard "); |
| 416 | fprintf(fp, strxf_limit(cfg->hard_packet_limit)); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 417 | fprintf(fp, "(packets)"); |
| 418 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 419 | |
| 420 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 421 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 422 | fprintf(fp, " "); |
| 423 | fprintf(fp, "expire add: "); |
| 424 | fprintf(fp, "soft "); |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 425 | fprintf(fp, "%llu", (unsigned long long) cfg->soft_add_expires_seconds); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 426 | fprintf(fp, "(sec), hard "); |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 427 | fprintf(fp, "%llu", (unsigned long long) cfg->hard_add_expires_seconds); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 428 | fprintf(fp, "(sec)"); |
| 429 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 430 | |
| 431 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 432 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 433 | fprintf(fp, " "); |
| 434 | fprintf(fp, "expire use: "); |
| 435 | fprintf(fp, "soft "); |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 436 | fprintf(fp, "%llu", (unsigned long long) cfg->soft_use_expires_seconds); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 437 | fprintf(fp, "(sec), hard "); |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 438 | fprintf(fp, "%llu", (unsigned long long) cfg->hard_use_expires_seconds); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 439 | fprintf(fp, "(sec)"); |
| 440 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 441 | } |
| 442 | if (cur) { |
| 443 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 444 | fputs(prefix, fp); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 445 | fprintf(fp, "lifetime current:"); |
| 446 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 447 | |
| 448 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 449 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 450 | fprintf(fp, " "); |
net[shemminger]!shemminger | b906243 | 2005-01-17 23:28:16 +0000 | [diff] [blame] | 451 | fprintf(fp, "%llu(bytes), ", (unsigned long long) cur->bytes); |
| 452 | fprintf(fp, "%llu(packets)", (unsigned long long) cur->packets); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 453 | fprintf(fp, "%s", _SL_); |
| 454 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 455 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 456 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 457 | fprintf(fp, " "); |
| 458 | fprintf(fp, "add %s ", strxf_time(cur->add_time)); |
| 459 | fprintf(fp, "use %s", strxf_time(cur->use_time)); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 460 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | void xfrm_selector_print(struct xfrm_selector *sel, __u16 family, |
| 465 | FILE *fp, const char *prefix) |
| 466 | { |
| 467 | char abuf[256]; |
| 468 | __u16 f; |
| 469 | |
| 470 | f = sel->family; |
| 471 | if (f == AF_UNSPEC) |
| 472 | f = family; |
| 473 | if (f == AF_UNSPEC) |
| 474 | f = preferred_family; |
| 475 | |
| 476 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 477 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 478 | |
| 479 | memset(abuf, '\0', sizeof(abuf)); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 480 | fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr), |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 481 | &sel->saddr, abuf, sizeof(abuf)), |
| 482 | sel->prefixlen_s); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 483 | |
| 484 | memset(abuf, '\0', sizeof(abuf)); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 485 | fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr), |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 486 | &sel->daddr, abuf, sizeof(abuf)), |
| 487 | sel->prefixlen_d); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 488 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 489 | if (sel->proto) |
| 490 | fprintf(fp, "proto %s ", strxf_proto(sel->proto)); |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 491 | switch (sel->proto) { |
| 492 | case IPPROTO_TCP: |
| 493 | case IPPROTO_UDP: |
| 494 | case IPPROTO_SCTP: |
shemminger | 27356a5 | 2006-01-12 18:31:36 +0000 | [diff] [blame] | 495 | case IPPROTO_DCCP: |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 496 | default: /* XXX */ |
| 497 | if (sel->sport_mask) |
| 498 | fprintf(fp, "sport %u ", ntohs(sel->sport)); |
| 499 | if (sel->dport_mask) |
| 500 | fprintf(fp, "dport %u ", ntohs(sel->dport)); |
| 501 | break; |
| 502 | case IPPROTO_ICMP: |
| 503 | case IPPROTO_ICMPV6: |
| 504 | /* type/code is stored at sport/dport in selector */ |
| 505 | if (sel->sport_mask) |
| 506 | fprintf(fp, "type %u ", ntohs(sel->sport)); |
| 507 | if (sel->dport_mask) |
| 508 | fprintf(fp, "code %u ", ntohs(sel->dport)); |
| 509 | break; |
Masahide NAKAMURA | 0bf0fbc | 2006-12-05 19:16:12 +0900 | [diff] [blame] | 510 | case IPPROTO_MH: |
| 511 | if (sel->sport_mask) |
| 512 | fprintf(fp, "type %u ", ntohs(sel->sport)); |
| 513 | if (sel->dport_mask) { |
| 514 | if (show_stats > 0) |
| 515 | fprintf(fp, "(dport) 0x%.4x ", sel->dport); |
| 516 | } |
| 517 | break; |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 518 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 519 | |
Thomas Graf | dcb283c | 2007-06-19 16:40:40 -0700 | [diff] [blame] | 520 | if (sel->ifindex > 0) |
| 521 | fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex)); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 522 | |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 523 | if (show_stats > 0) |
| 524 | fprintf(fp, "uid %u", sel->user); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 525 | |
| 526 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 529 | static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len, |
| 530 | FILE *fp, const char *prefix, int newline) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 531 | { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 532 | int keylen; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 533 | int i; |
| 534 | |
| 535 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 536 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 537 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 538 | fprintf(fp, "%s ", strxf_algotype(type)); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 539 | |
| 540 | if (len < sizeof(*algo)) { |
| 541 | fprintf(fp, "(ERROR truncated)"); |
| 542 | goto fin; |
| 543 | } |
| 544 | len -= sizeof(*algo); |
| 545 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 546 | fprintf(fp, "%s ", algo->alg_name); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 547 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 548 | keylen = algo->alg_key_len / 8; |
| 549 | if (len < keylen) { |
| 550 | fprintf(fp, "(ERROR truncated)"); |
| 551 | goto fin; |
| 552 | } |
| 553 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 554 | fprintf(fp, "0x"); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 555 | for (i = 0; i < keylen; i ++) |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 556 | fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 557 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 558 | if (show_stats > 0) |
| 559 | fprintf(fp, " (%d bits)", algo->alg_key_len); |
| 560 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 561 | fin: |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 562 | if (newline) |
| 563 | fprintf(fp, "%s", _SL_); |
| 564 | } |
| 565 | |
| 566 | static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len, |
| 567 | FILE *fp, const char *prefix) |
| 568 | { |
| 569 | return __xfrm_algo_print(algo, type, len, fp, prefix, 1); |
| 570 | } |
| 571 | |
| 572 | static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len, |
| 573 | FILE *fp, const char *prefix) |
| 574 | { |
| 575 | struct { |
| 576 | struct xfrm_algo algo; |
| 577 | char key[algo->alg_key_len / 8]; |
| 578 | } base; |
| 579 | |
| 580 | memcpy(base.algo.alg_name, algo->alg_name, sizeof(base.algo.alg_name)); |
| 581 | base.algo.alg_key_len = algo->alg_key_len; |
| 582 | memcpy(base.algo.alg_key, algo->alg_key, algo->alg_key_len / 8); |
| 583 | |
| 584 | __xfrm_algo_print(&base.algo, XFRMA_ALG_AEAD, len, fp, prefix, 0); |
| 585 | |
| 586 | fprintf(fp, " %d", algo->alg_icv_len); |
| 587 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 588 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 589 | } |
| 590 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 591 | static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len, |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 592 | __u16 family, FILE *fp, const char *prefix) |
| 593 | { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 594 | int ntmpls = len / sizeof(struct xfrm_user_tmpl); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 595 | int i; |
| 596 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 597 | if (ntmpls <= 0) { |
| 598 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 599 | fputs(prefix, fp); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 600 | fprintf(fp, "(ERROR \"tmpl\" truncated)"); |
| 601 | fprintf(fp, "%s", _SL_); |
| 602 | return; |
| 603 | } |
| 604 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 605 | for (i = 0; i < ntmpls; i++) { |
| 606 | struct xfrm_user_tmpl *tmpl = &tmpls[i]; |
| 607 | |
| 608 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 609 | fputs(prefix, fp); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 610 | |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 611 | xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode, |
Thomas Egerer | b9ab720 | 2009-04-14 16:15:17 -0700 | [diff] [blame^] | 612 | tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl "); |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 613 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 614 | if (show_stats > 0 || tmpl->optional) { |
| 615 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 616 | fputs(prefix, fp); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 617 | fprintf(fp, "\t"); |
| 618 | switch (tmpl->optional) { |
| 619 | case 0: |
| 620 | if (show_stats > 0) |
| 621 | fprintf(fp, "level required "); |
| 622 | break; |
| 623 | case 1: |
| 624 | fprintf(fp, "level use "); |
| 625 | break; |
| 626 | default: |
| 627 | fprintf(fp, "level %u ", tmpl->optional); |
| 628 | break; |
| 629 | } |
| 630 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 631 | if (show_stats > 0) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 632 | fprintf(fp, "share %s ", strxf_share(tmpl->share)); |
| 633 | |
| 634 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 635 | } |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 636 | |
| 637 | if (show_stats > 0) { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 638 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 639 | fputs(prefix, fp); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 640 | fprintf(fp, "\t"); |
| 641 | fprintf(fp, "%s-mask %s ", |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 642 | strxf_algotype(XFRMA_ALG_CRYPT), |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 643 | strxf_mask32(tmpl->ealgos)); |
| 644 | fprintf(fp, "%s-mask %s ", |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 645 | strxf_algotype(XFRMA_ALG_AUTH), |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 646 | strxf_mask32(tmpl->aalgos)); |
| 647 | fprintf(fp, "%s-mask %s", |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 648 | strxf_algotype(XFRMA_ALG_COMP), |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 649 | strxf_mask32(tmpl->calgos)); |
| 650 | |
| 651 | fprintf(fp, "%s", _SL_); |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 652 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 656 | void xfrm_xfrma_print(struct rtattr *tb[], __u16 family, |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 657 | FILE *fp, const char *prefix) |
| 658 | { |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 659 | if (tb[XFRMA_ALG_AUTH]) { |
| 660 | struct rtattr *rta = tb[XFRMA_ALG_AUTH]; |
| 661 | xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta), |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 662 | XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix); |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 663 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 664 | |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 665 | if (tb[XFRMA_ALG_AEAD]) { |
| 666 | struct rtattr *rta = tb[XFRMA_ALG_AEAD]; |
| 667 | xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta), |
| 668 | RTA_PAYLOAD(rta), fp, prefix); |
| 669 | } |
| 670 | |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 671 | if (tb[XFRMA_ALG_CRYPT]) { |
| 672 | struct rtattr *rta = tb[XFRMA_ALG_CRYPT]; |
| 673 | xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta), |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 674 | XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix); |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 675 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 676 | |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 677 | if (tb[XFRMA_ALG_COMP]) { |
| 678 | struct rtattr *rta = tb[XFRMA_ALG_COMP]; |
| 679 | xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta), |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 680 | XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix); |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 681 | } |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 682 | |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 683 | if (tb[XFRMA_ENCAP]) { |
| 684 | struct xfrm_encap_tmpl *e; |
| 685 | char abuf[256]; |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 686 | |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 687 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 688 | fputs(prefix, fp); |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 689 | fprintf(fp, "encap "); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 690 | |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 691 | if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) { |
| 692 | fprintf(fp, "(ERROR truncated)"); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 693 | fprintf(fp, "%s", _SL_); |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 694 | return; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 695 | } |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 696 | e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]); |
| 697 | |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 698 | fprintf(fp, "type "); |
| 699 | switch (e->encap_type) { |
| 700 | case 1: |
| 701 | fprintf(fp, "espinudp-nonike "); |
| 702 | break; |
| 703 | case 2: |
| 704 | fprintf(fp, "espinudp "); |
| 705 | break; |
| 706 | default: |
| 707 | fprintf(fp, "%u ", e->encap_type); |
| 708 | break; |
| 709 | } |
12!tgraf | bcf3281 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 710 | fprintf(fp, "sport %u ", ntohs(e->encap_sport)); |
| 711 | fprintf(fp, "dport %u ", ntohs(e->encap_dport)); |
| 712 | |
| 713 | memset(abuf, '\0', sizeof(abuf)); |
| 714 | fprintf(fp, "addr %s", |
| 715 | rt_addr_n2a(family, sizeof(e->encap_oa), |
| 716 | &e->encap_oa, abuf, sizeof(abuf))); |
| 717 | fprintf(fp, "%s", _SL_); |
| 718 | } |
| 719 | |
| 720 | if (tb[XFRMA_TMPL]) { |
| 721 | struct rtattr *rta = tb[XFRMA_TMPL]; |
| 722 | xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta), |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 723 | RTA_PAYLOAD(rta), family, fp, prefix); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 724 | } |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 725 | |
| 726 | if (tb[XFRMA_COADDR]) { |
| 727 | char abuf[256]; |
| 728 | xfrm_address_t *coa; |
| 729 | |
| 730 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 731 | fputs(prefix, fp); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 732 | fprintf(fp, "coa "); |
| 733 | |
| 734 | coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]); |
| 735 | |
| 736 | if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) { |
| 737 | fprintf(fp, "(ERROR truncated)"); |
| 738 | fprintf(fp, "%s", _SL_); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | memset(abuf, '\0', sizeof(abuf)); |
| 743 | fprintf(fp, "%s", |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 744 | rt_addr_n2a(family, sizeof(*coa), coa, |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 745 | abuf, sizeof(abuf))); |
| 746 | fprintf(fp, "%s", _SL_); |
| 747 | } |
| 748 | |
| 749 | if (tb[XFRMA_LASTUSED]) { |
| 750 | __u64 lastused; |
| 751 | |
| 752 | if (prefix) |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 753 | fputs(prefix, fp); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 754 | fprintf(fp, "lastused "); |
| 755 | |
| 756 | if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) { |
| 757 | fprintf(fp, "(ERROR truncated)"); |
| 758 | fprintf(fp, "%s", _SL_); |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | lastused = *(__u64 *)RTA_DATA(tb[XFRMA_LASTUSED]); |
| 763 | |
| 764 | fprintf(fp, "%s", strxf_time(lastused)); |
| 765 | fprintf(fp, "%s", _SL_); |
| 766 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 767 | } |
| 768 | |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 769 | static int xfrm_selector_iszero(struct xfrm_selector *s) |
| 770 | { |
| 771 | struct xfrm_selector s0; |
| 772 | |
| 773 | memset(&s0, 0, sizeof(s0)); |
| 774 | |
| 775 | return (memcmp(&s0, s, sizeof(s0)) == 0); |
| 776 | } |
| 777 | |
| 778 | void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo, |
| 779 | struct rtattr *tb[], FILE *fp, const char *prefix, |
| 780 | const char *title) |
| 781 | { |
| 782 | char buf[STRBUF_SIZE]; |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 783 | int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 784 | |
| 785 | memset(buf, '\0', sizeof(buf)); |
| 786 | |
| 787 | xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode, |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 788 | xsinfo->reqid, xsinfo->family, force_spi, fp, |
| 789 | prefix, title); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 790 | |
| 791 | if (prefix) |
| 792 | STRBUF_CAT(buf, prefix); |
| 793 | STRBUF_CAT(buf, "\t"); |
| 794 | |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 795 | fputs(buf, fp); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 796 | fprintf(fp, "replay-window %u ", xsinfo->replay_window); |
| 797 | if (show_stats > 0) |
| 798 | fprintf(fp, "seq 0x%08u ", xsinfo->seq); |
| 799 | if (show_stats > 0 || xsinfo->flags) { |
| 800 | __u8 flags = xsinfo->flags; |
| 801 | |
| 802 | fprintf(fp, "flag "); |
| 803 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn"); |
| 804 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp"); |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 805 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc"); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 806 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv"); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 807 | if (flags) |
| 808 | fprintf(fp, "%x", flags); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 809 | } |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 810 | if (show_stats > 0) |
| 811 | fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags)); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 812 | fprintf(fp, "%s", _SL_); |
| 813 | |
| 814 | xfrm_xfrma_print(tb, xsinfo->family, fp, buf); |
| 815 | |
| 816 | if (!xfrm_selector_iszero(&xsinfo->sel)) { |
| 817 | char sbuf[STRBUF_SIZE]; |
| 818 | |
| 819 | memcpy(sbuf, buf, sizeof(sbuf)); |
| 820 | STRBUF_CAT(sbuf, "sel "); |
| 821 | |
| 822 | xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf); |
| 823 | } |
| 824 | |
| 825 | if (show_stats > 0) { |
| 826 | xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf); |
| 827 | xfrm_stats_print(&xsinfo->stats, fp, buf); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo, |
| 832 | struct rtattr *tb[], FILE *fp, const char *prefix, |
| 833 | const char *title) |
| 834 | { |
| 835 | char buf[STRBUF_SIZE]; |
| 836 | |
| 837 | memset(buf, '\0', sizeof(buf)); |
| 838 | |
| 839 | xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title); |
| 840 | |
| 841 | if (prefix) |
| 842 | STRBUF_CAT(buf, prefix); |
| 843 | STRBUF_CAT(buf, "\t"); |
| 844 | |
Stephen Hemminger | 9a73e17 | 2009-01-07 17:20:43 -0800 | [diff] [blame] | 845 | fputs(buf, fp); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 846 | fprintf(fp, "dir "); |
| 847 | switch (xpinfo->dir) { |
| 848 | case XFRM_POLICY_IN: |
| 849 | fprintf(fp, "in"); |
| 850 | break; |
| 851 | case XFRM_POLICY_OUT: |
| 852 | fprintf(fp, "out"); |
| 853 | break; |
| 854 | case XFRM_POLICY_FWD: |
| 855 | fprintf(fp, "fwd"); |
| 856 | break; |
| 857 | default: |
| 858 | fprintf(fp, "%u", xpinfo->dir); |
| 859 | break; |
| 860 | } |
| 861 | fprintf(fp, " "); |
| 862 | |
| 863 | switch (xpinfo->action) { |
| 864 | case XFRM_POLICY_ALLOW: |
| 865 | if (show_stats > 0) |
| 866 | fprintf(fp, "action allow "); |
| 867 | break; |
| 868 | case XFRM_POLICY_BLOCK: |
| 869 | fprintf(fp, "action block "); |
| 870 | break; |
| 871 | default: |
| 872 | fprintf(fp, "action %u ", xpinfo->action); |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | if (show_stats) |
| 877 | fprintf(fp, "index %u ", xpinfo->index); |
| 878 | fprintf(fp, "priority %u ", xpinfo->priority); |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 879 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 880 | if (tb[XFRMA_POLICY_TYPE]) { |
| 881 | struct xfrm_userpolicy_type *upt; |
| 882 | |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 883 | fprintf(fp, "ptype "); |
| 884 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 885 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) |
| 886 | fprintf(fp, "(ERROR truncated)"); |
| 887 | |
| 888 | upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 889 | fprintf(fp, "%s ", strxf_ptype(upt->type)); |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 890 | } |
| 891 | |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 892 | if (show_stats > 0) |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 893 | fprintf(fp, "share %s ", strxf_share(xpinfo->share)); |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 894 | |
| 895 | if (show_stats > 0 || xpinfo->flags) { |
| 896 | __u8 flags = xpinfo->flags; |
| 897 | |
| 898 | fprintf(fp, "flag "); |
| 899 | XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok"); |
| 900 | if (flags) |
| 901 | fprintf(fp, "%x", flags); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 902 | } |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 903 | if (show_stats > 0) |
| 904 | fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags)); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 905 | fprintf(fp, "%s", _SL_); |
| 906 | |
| 907 | if (show_stats > 0) |
| 908 | xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf); |
| 909 | |
| 910 | xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf); |
| 911 | } |
| 912 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 913 | int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family, |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 914 | int loose, int *argcp, char ***argvp) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 915 | { |
| 916 | int argc = *argcp; |
| 917 | char **argv = *argvp; |
| 918 | inet_prefix dst; |
| 919 | inet_prefix src; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 920 | |
| 921 | memset(&dst, 0, sizeof(dst)); |
| 922 | memset(&src, 0, sizeof(src)); |
| 923 | |
| 924 | while (1) { |
| 925 | if (strcmp(*argv, "src") == 0) { |
| 926 | NEXT_ARG(); |
| 927 | |
| 928 | get_prefix(&src, *argv, preferred_family); |
| 929 | if (src.family == AF_UNSPEC) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 930 | invarg("\"src\" address family is AF_UNSPEC", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 931 | if (family) |
| 932 | *family = src.family; |
| 933 | |
| 934 | memcpy(saddr, &src.data, sizeof(*saddr)); |
| 935 | |
| 936 | filter.id_src_mask = src.bitlen; |
| 937 | |
| 938 | } else if (strcmp(*argv, "dst") == 0) { |
| 939 | NEXT_ARG(); |
| 940 | |
| 941 | get_prefix(&dst, *argv, preferred_family); |
| 942 | if (dst.family == AF_UNSPEC) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 943 | invarg("\"dst\" address family is AF_UNSPEC", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 944 | if (family) |
| 945 | *family = dst.family; |
| 946 | |
| 947 | memcpy(&id->daddr, &dst.data, sizeof(id->daddr)); |
| 948 | |
| 949 | filter.id_dst_mask = dst.bitlen; |
| 950 | |
| 951 | } else if (strcmp(*argv, "proto") == 0) { |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 952 | int ret; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 953 | |
| 954 | NEXT_ARG(); |
| 955 | |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 956 | ret = xfrm_xfrmproto_getbyname(*argv); |
| 957 | if (ret < 0) |
| 958 | invarg("\"XFRM_PROTO\" is invalid", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 959 | |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 960 | id->proto = (__u8)ret; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 961 | |
| 962 | filter.id_proto_mask = XFRM_FILTER_MASK_FULL; |
| 963 | |
| 964 | } else if (strcmp(*argv, "spi") == 0) { |
| 965 | __u32 spi; |
| 966 | |
| 967 | NEXT_ARG(); |
| 968 | if (get_u32(&spi, *argv, 0)) |
| 969 | invarg("\"SPI\" is invalid", *argv); |
| 970 | |
| 971 | spi = htonl(spi); |
| 972 | id->spi = spi; |
| 973 | |
| 974 | filter.id_spi_mask = XFRM_FILTER_MASK_FULL; |
| 975 | |
| 976 | } else { |
| 977 | PREV_ARG(); /* back track */ |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | if (!NEXT_ARG_OK()) |
| 982 | break; |
| 983 | NEXT_ARG(); |
| 984 | } |
| 985 | |
| 986 | if (src.family && dst.family && (src.family != dst.family)) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 987 | invarg("the same address family is required between \"src\" and \"dst\"", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 988 | |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 989 | if (loose == 0 && id->proto == 0) |
| 990 | missarg("XFRM_PROTO"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 991 | if (argc == *argcp) |
| 992 | missarg("ID"); |
| 993 | |
| 994 | *argcp = argc; |
| 995 | *argvp = argv; |
| 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp) |
| 1001 | { |
| 1002 | int argc = *argcp; |
| 1003 | char **argv = *argvp; |
| 1004 | |
| 1005 | if (matches(*argv, "transport") == 0) |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 1006 | *mode = XFRM_MODE_TRANSPORT; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1007 | else if (matches(*argv, "tunnel") == 0) |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 1008 | *mode = XFRM_MODE_TUNNEL; |
| 1009 | else if (matches(*argv, "ro") == 0) |
| 1010 | *mode = XFRM_MODE_ROUTEOPTIMIZATION; |
| 1011 | else if (matches(*argv, "in_trigger") == 0) |
| 1012 | *mode = XFRM_MODE_IN_TRIGGER; |
Stephen Hemminger | 34e099e | 2006-10-19 13:15:35 -0700 | [diff] [blame] | 1013 | else if (matches(*argv, "beet") == 0) |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 1014 | *mode = XFRM_MODE_BEET; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1015 | else |
| 1016 | invarg("\"MODE\" is invalid", *argv); |
| 1017 | |
| 1018 | *argcp = argc; |
| 1019 | *argvp = argv; |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 1024 | int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp) |
| 1025 | { |
| 1026 | int argc = *argcp; |
| 1027 | char **argv = *argvp; |
| 1028 | |
| 1029 | if (strcmp(*argv, "espinudp-nonike") == 0) |
| 1030 | *type = 1; |
| 1031 | else if (strcmp(*argv, "espinudp") == 0) |
| 1032 | *type = 2; |
| 1033 | else |
| 1034 | invarg("\"ENCAP-TYPE\" is invalid", *argv); |
| 1035 | |
| 1036 | *argcp = argc; |
| 1037 | *argvp = argv; |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1042 | /* NOTE: reqid is used by host-byte order */ |
| 1043 | int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp) |
| 1044 | { |
| 1045 | int argc = *argcp; |
| 1046 | char **argv = *argvp; |
| 1047 | |
| 1048 | if (get_u32(reqid, *argv, 0)) |
| 1049 | invarg("\"REQID\" is invalid", *argv); |
| 1050 | |
| 1051 | *argcp = argc; |
| 1052 | *argvp = argv; |
| 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | static int xfrm_selector_upspec_parse(struct xfrm_selector *sel, |
| 1058 | int *argcp, char ***argvp) |
| 1059 | { |
| 1060 | int argc = *argcp; |
| 1061 | char **argv = *argvp; |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 1062 | char *sportp = NULL; |
| 1063 | char *dportp = NULL; |
| 1064 | char *typep = NULL; |
| 1065 | char *codep = NULL; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1066 | |
| 1067 | while (1) { |
| 1068 | if (strcmp(*argv, "proto") == 0) { |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 1069 | __u8 upspec; |
| 1070 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1071 | NEXT_ARG(); |
| 1072 | |
| 1073 | if (strcmp(*argv, "any") == 0) |
| 1074 | upspec = 0; |
| 1075 | else { |
| 1076 | struct protoent *pp; |
| 1077 | pp = getprotobyname(*argv); |
| 1078 | if (pp) |
| 1079 | upspec = pp->p_proto; |
| 1080 | else { |
| 1081 | if (get_u8(&upspec, *argv, 0)) |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 1082 | invarg("\"PROTO\" is invalid", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | sel->proto = upspec; |
| 1086 | |
| 1087 | filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL; |
| 1088 | |
| 1089 | } else if (strcmp(*argv, "sport") == 0) { |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 1090 | sportp = *argv; |
| 1091 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1092 | NEXT_ARG(); |
| 1093 | |
| 1094 | if (get_u16(&sel->sport, *argv, 0)) |
| 1095 | invarg("\"PORT\" is invalid", *argv); |
| 1096 | sel->sport = htons(sel->sport); |
| 1097 | if (sel->sport) |
| 1098 | sel->sport_mask = ~((__u16)0); |
| 1099 | |
| 1100 | filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL; |
| 1101 | |
| 1102 | } else if (strcmp(*argv, "dport") == 0) { |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 1103 | dportp = *argv; |
| 1104 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1105 | NEXT_ARG(); |
| 1106 | |
| 1107 | if (get_u16(&sel->dport, *argv, 0)) |
| 1108 | invarg("\"PORT\" is invalid", *argv); |
| 1109 | sel->dport = htons(sel->dport); |
| 1110 | if (sel->dport) |
| 1111 | sel->dport_mask = ~((__u16)0); |
| 1112 | |
| 1113 | filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; |
| 1114 | |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 1115 | } else if (strcmp(*argv, "type") == 0) { |
| 1116 | typep = *argv; |
| 1117 | |
| 1118 | NEXT_ARG(); |
| 1119 | |
| 1120 | if (get_u16(&sel->sport, *argv, 0) || |
| 1121 | (sel->sport & ~((__u16)0xff))) |
| 1122 | invarg("\"type\" value is invalid", *argv); |
| 1123 | sel->sport = htons(sel->sport); |
| 1124 | sel->sport_mask = ~((__u16)0); |
| 1125 | |
| 1126 | filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL; |
| 1127 | |
| 1128 | |
| 1129 | } else if (strcmp(*argv, "code") == 0) { |
| 1130 | codep = *argv; |
| 1131 | |
| 1132 | NEXT_ARG(); |
| 1133 | |
| 1134 | if (get_u16(&sel->dport, *argv, 0) || |
| 1135 | (sel->dport & ~((__u16)0xff))) |
| 1136 | invarg("\"code\" value is invalid", *argv); |
| 1137 | sel->dport = htons(sel->dport); |
| 1138 | sel->dport_mask = ~((__u16)0); |
| 1139 | |
| 1140 | filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL; |
| 1141 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1142 | } else { |
| 1143 | PREV_ARG(); /* back track */ |
| 1144 | break; |
| 1145 | } |
| 1146 | |
| 1147 | if (!NEXT_ARG_OK()) |
| 1148 | break; |
| 1149 | NEXT_ARG(); |
| 1150 | } |
| 1151 | if (argc == *argcp) |
| 1152 | missarg("UPSPEC"); |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 1153 | if (sportp || dportp) { |
| 1154 | switch (sel->proto) { |
| 1155 | case IPPROTO_TCP: |
| 1156 | case IPPROTO_UDP: |
| 1157 | case IPPROTO_SCTP: |
shemminger | 27356a5 | 2006-01-12 18:31:36 +0000 | [diff] [blame] | 1158 | case IPPROTO_DCCP: |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | default: |
| 1161 | fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto)); |
| 1162 | exit(1); |
| 1163 | } |
| 1164 | } |
| 1165 | if (typep || codep) { |
| 1166 | switch (sel->proto) { |
| 1167 | case IPPROTO_ICMP: |
| 1168 | case IPPROTO_ICMPV6: |
Masahide NAKAMURA | 0bf0fbc | 2006-12-05 19:16:12 +0900 | [diff] [blame] | 1169 | case IPPROTO_MH: |
org[shemminger]!nakam | c70b36d | 2004-09-28 18:42:35 +0000 | [diff] [blame] | 1170 | break; |
| 1171 | default: |
| 1172 | fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto)); |
| 1173 | exit(1); |
| 1174 | } |
| 1175 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1176 | |
| 1177 | *argcp = argc; |
| 1178 | *argvp = argv; |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp) |
| 1184 | { |
| 1185 | int argc = *argcp; |
| 1186 | char **argv = *argvp; |
| 1187 | inet_prefix dst; |
| 1188 | inet_prefix src; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 1189 | char *upspecp = NULL; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1190 | |
| 1191 | memset(&dst, 0, sizeof(dst)); |
| 1192 | memset(&src, 0, sizeof(src)); |
| 1193 | |
| 1194 | while (1) { |
| 1195 | if (strcmp(*argv, "src") == 0) { |
| 1196 | NEXT_ARG(); |
| 1197 | |
| 1198 | get_prefix(&src, *argv, preferred_family); |
| 1199 | if (src.family == AF_UNSPEC) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 1200 | invarg("\"src\" address family is AF_UNSPEC", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1201 | sel->family = src.family; |
| 1202 | |
| 1203 | memcpy(&sel->saddr, &src.data, sizeof(sel->saddr)); |
| 1204 | sel->prefixlen_s = src.bitlen; |
| 1205 | |
| 1206 | filter.sel_src_mask = src.bitlen; |
| 1207 | |
| 1208 | } else if (strcmp(*argv, "dst") == 0) { |
| 1209 | NEXT_ARG(); |
| 1210 | |
| 1211 | get_prefix(&dst, *argv, preferred_family); |
| 1212 | if (dst.family == AF_UNSPEC) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 1213 | invarg("\"dst\" address family is AF_UNSPEC", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1214 | sel->family = dst.family; |
| 1215 | |
| 1216 | memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr)); |
| 1217 | sel->prefixlen_d = dst.bitlen; |
| 1218 | |
| 1219 | filter.sel_dst_mask = dst.bitlen; |
| 1220 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1221 | } else if (strcmp(*argv, "dev") == 0) { |
| 1222 | int ifindex; |
| 1223 | |
| 1224 | NEXT_ARG(); |
| 1225 | |
| 1226 | if (strcmp(*argv, "none") == 0) |
| 1227 | ifindex = 0; |
| 1228 | else { |
Thomas Graf | dcb283c | 2007-06-19 16:40:40 -0700 | [diff] [blame] | 1229 | ifindex = ll_name_to_index(*argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1230 | if (ifindex <= 0) |
| 1231 | invarg("\"DEV\" is invalid", *argv); |
| 1232 | } |
| 1233 | sel->ifindex = ifindex; |
| 1234 | |
| 1235 | filter.sel_dev_mask = XFRM_FILTER_MASK_FULL; |
| 1236 | |
| 1237 | } else { |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 1238 | if (upspecp) { |
| 1239 | PREV_ARG(); /* back track */ |
| 1240 | break; |
| 1241 | } else { |
| 1242 | upspecp = *argv; |
| 1243 | xfrm_selector_upspec_parse(sel, &argc, &argv); |
| 1244 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | if (!NEXT_ARG_OK()) |
| 1248 | break; |
| 1249 | |
| 1250 | NEXT_ARG(); |
| 1251 | } |
| 1252 | |
| 1253 | if (src.family && dst.family && (src.family != dst.family)) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 1254 | invarg("the same address family is required between \"src\" and \"dst\"", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1255 | |
| 1256 | if (argc == *argcp) |
| 1257 | missarg("SELECTOR"); |
| 1258 | |
| 1259 | *argcp = argc; |
| 1260 | *argvp = argv; |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft, |
| 1266 | int *argcp, char ***argvp) |
| 1267 | { |
| 1268 | int argc = *argcp; |
| 1269 | char **argv = *argvp; |
| 1270 | int ret; |
| 1271 | |
| 1272 | if (strcmp(*argv, "time-soft") == 0) { |
| 1273 | NEXT_ARG(); |
| 1274 | ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0); |
| 1275 | if (ret) |
| 1276 | invarg("\"time-soft\" value is invalid", *argv); |
| 1277 | } else if (strcmp(*argv, "time-hard") == 0) { |
| 1278 | NEXT_ARG(); |
| 1279 | ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0); |
| 1280 | if (ret) |
| 1281 | invarg("\"time-hard\" value is invalid", *argv); |
| 1282 | } else if (strcmp(*argv, "time-use-soft") == 0) { |
| 1283 | NEXT_ARG(); |
| 1284 | ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0); |
| 1285 | if (ret) |
| 1286 | invarg("\"time-use-soft\" value is invalid", *argv); |
| 1287 | } else if (strcmp(*argv, "time-use-hard") == 0) { |
| 1288 | NEXT_ARG(); |
| 1289 | ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0); |
| 1290 | if (ret) |
| 1291 | invarg("\"time-use-hard\" value is invalid", *argv); |
| 1292 | } else if (strcmp(*argv, "byte-soft") == 0) { |
| 1293 | NEXT_ARG(); |
| 1294 | ret = get_u64(&lft->soft_byte_limit, *argv, 0); |
| 1295 | if (ret) |
| 1296 | invarg("\"byte-soft\" value is invalid", *argv); |
| 1297 | } else if (strcmp(*argv, "byte-hard") == 0) { |
| 1298 | NEXT_ARG(); |
| 1299 | ret = get_u64(&lft->hard_byte_limit, *argv, 0); |
| 1300 | if (ret) |
| 1301 | invarg("\"byte-hard\" value is invalid", *argv); |
| 1302 | } else if (strcmp(*argv, "packet-soft") == 0) { |
| 1303 | NEXT_ARG(); |
| 1304 | ret = get_u64(&lft->soft_packet_limit, *argv, 0); |
| 1305 | if (ret) |
| 1306 | invarg("\"packet-soft\" value is invalid", *argv); |
| 1307 | } else if (strcmp(*argv, "packet-hard") == 0) { |
| 1308 | NEXT_ARG(); |
| 1309 | ret = get_u64(&lft->hard_packet_limit, *argv, 0); |
| 1310 | if (ret) |
| 1311 | invarg("\"packet-hard\" value is invalid", *argv); |
| 1312 | } else |
| 1313 | invarg("\"LIMIT\" is invalid", *argv); |
| 1314 | |
| 1315 | *argcp = argc; |
| 1316 | *argvp = argv; |
| 1317 | |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
| 1321 | int do_xfrm(int argc, char **argv) |
| 1322 | { |
| 1323 | memset(&filter, 0, sizeof(filter)); |
| 1324 | |
| 1325 | if (argc < 1) |
| 1326 | usage(); |
| 1327 | |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 1328 | if (matches(*argv, "state") == 0 || |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 1329 | matches(*argv, "sa") == 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1330 | return do_xfrm_state(argc-1, argv+1); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 1331 | else if (matches(*argv, "policy") == 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1332 | return do_xfrm_policy(argc-1, argv+1); |
linux-ipv6.org!nakam | 15ac4cd | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 1333 | else if (matches(*argv, "monitor") == 0) |
| 1334 | return do_xfrm_monitor(argc-1, argv+1); |
net[shemminger]!shemminger | ad27396 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 1335 | else if (matches(*argv, "help") == 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1336 | usage(); |
| 1337 | fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv); |
| 1338 | exit(-1); |
| 1339 | } |
| 1340 | usage(); |
| 1341 | } |