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