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 iproute.c |
| 21 | */ |
| 22 | /* |
| 23 | * Authors: |
| 24 | * Masahide NAKAMURA @USAGI |
| 25 | */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <netdb.h> |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | #include "xfrm.h" |
| 33 | #include "ip_common.h" |
| 34 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 35 | /* #define NLMSG_DELETEALL_BUF_SIZE (4096-512) */ |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 36 | #define NLMSG_DELETEALL_BUF_SIZE 8192 |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Receiving buffer defines: |
| 40 | * nlmsg |
| 41 | * data = struct xfrm_usersa_info |
| 42 | * rtattr |
| 43 | * rtattr |
12!tgraf | 2534613 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 44 | * ... (max count of rtattr is XFRM_MAX+1 |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 45 | * |
| 46 | * each rtattr data = struct xfrm_algo(dynamic size) or xfrm_address_t |
| 47 | */ |
| 48 | #define NLMSG_BUF_SIZE 4096 |
| 49 | #define RTA_BUF_SIZE 2048 |
| 50 | #define XFRM_ALGO_KEY_BUF_SIZE 512 |
Joy Latten | 0c7a594 | 2011-02-02 17:32:59 -0600 | [diff] [blame] | 51 | #define CTX_BUF_SIZE 256 |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 52 | |
| 53 | static void usage(void) __attribute__((noreturn)); |
| 54 | |
| 55 | static void usage(void) |
| 56 | { |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 57 | fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n"); |
| 58 | fprintf(stderr, " [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n"); |
| 59 | fprintf(stderr, " [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n"); |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 60 | fprintf(stderr, " [ replay-seq-hi SEQ ] [ replay-oseq-hi SEQ ]\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 61 | fprintf(stderr, " [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n"); |
Nicolas Dichtel | dc8867d | 2013-05-17 01:36:38 -0700 | [diff] [blame] | 62 | fprintf(stderr, " [ coa ADDR[/PLEN] ] [ ctx CTX ] [ extra-flag EXTRA-FLAG-LIST ]\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 63 | fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n"); |
| 64 | fprintf(stderr, " [ reqid REQID ] [ seq SEQ ] [ min SPI max SPI ]\n"); |
| 65 | fprintf(stderr, "Usage: ip xfrm state { delete | get } ID [ mark MARK [ mask MASK ] ]\n"); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 66 | fprintf(stderr, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n"); |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 67 | fprintf(stderr, " [ flag FLAG-LIST ]\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 68 | fprintf(stderr, "Usage: ip xfrm state flush [ proto XFRM-PROTO ]\n"); |
| 69 | fprintf(stderr, "Usage: ip xfrm state count\n"); |
| 70 | fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n"); |
| 71 | fprintf(stderr, "XFRM-PROTO := "); |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 72 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP)); |
| 73 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH)); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 74 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP)); |
| 75 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING)); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 76 | fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS)); |
| 77 | fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] ALGO\n"); |
| 78 | fprintf(stderr, "ALGO := { "); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 79 | fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT)); |
David Ward | f3b9aa3 | 2013-03-25 04:23:17 +0000 | [diff] [blame] | 80 | fprintf(stderr, "%s", strxf_algotype(XFRMA_ALG_AUTH)); |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 81 | fprintf(stderr, " } ALGO-NAME ALGO-KEYMAT |\n"); |
David Ward | f3b9aa3 | 2013-03-25 04:23:17 +0000 | [diff] [blame] | 82 | fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC)); |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 83 | fprintf(stderr, " ALGO-NAME ALGO-KEYMAT ALGO-TRUNC-LEN |\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 84 | fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AEAD)); |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 85 | fprintf(stderr, " ALGO-NAME ALGO-KEYMAT ALGO-ICV-LEN |\n"); |
David Ward | f3b9aa3 | 2013-03-25 04:23:17 +0000 | [diff] [blame] | 86 | fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_COMP)); |
| 87 | fprintf(stderr, " ALGO-NAME\n"); |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 88 | fprintf(stderr, "MODE := transport | tunnel | beet | ro | in_trigger\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 89 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 90 | fprintf(stderr, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4 | esn\n"); |
Nicolas Dichtel | dc8867d | 2013-05-17 01:36:38 -0700 | [diff] [blame] | 91 | fprintf(stderr, "EXTRA-FLAG-LIST := [ EXTRA-FLAG-LIST ] EXTRA-FLAG\n"); |
| 92 | fprintf(stderr, "EXTRA-FLAG := dont-encap-dscp\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 93 | fprintf(stderr, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n"); |
| 94 | fprintf(stderr, "UPSPEC := proto { { "); |
| 95 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_TCP)); |
| 96 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_UDP)); |
| 97 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_SCTP)); |
| 98 | fprintf(stderr, "%s", strxf_proto(IPPROTO_DCCP)); |
| 99 | fprintf(stderr, " } [ sport PORT ] [ dport PORT ] |\n"); |
| 100 | fprintf(stderr, " { "); |
| 101 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMP)); |
| 102 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMPV6)); |
| 103 | fprintf(stderr, "%s", strxf_proto(IPPROTO_MH)); |
| 104 | fprintf(stderr, " } [ type NUMBER ] [ code NUMBER ] |\n"); |
| 105 | fprintf(stderr, " %s", strxf_proto(IPPROTO_GRE)); |
| 106 | fprintf(stderr, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n"); |
| 107 | fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n"); |
| 108 | fprintf(stderr, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n"); |
| 109 | fprintf(stderr, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n"); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 110 | fprintf(stderr, "ENCAP := { espinudp | espinudp-nonike } SPORT DPORT OADDR\n"); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 111 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 112 | exit(-1); |
| 113 | } |
| 114 | |
| 115 | static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type, |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 116 | char *name, char *key, char *buf, int max) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 117 | { |
| 118 | int len; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 119 | int slen = strlen(key); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 120 | |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 121 | #if 0 |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 122 | /* XXX: verifying both name and key is required! */ |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 123 | fprintf(stderr, "warning: ALGO-NAME/ALGO-KEYMAT values will be sent to the kernel promiscuously! (verifying them isn't implemented yet)\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
| 126 | strncpy(alg->alg_name, name, sizeof(alg->alg_name)); |
| 127 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 128 | if (slen > 2 && strncmp(key, "0x", 2) == 0) { |
org[shemminger]!nakam | 54f7328 | 2004-09-28 18:36:52 +0000 | [diff] [blame] | 129 | /* split two chars "0x" from the top */ |
| 130 | char *p = key + 2; |
| 131 | int plen = slen - 2; |
| 132 | int i; |
| 133 | int j; |
| 134 | |
| 135 | /* Converting hexadecimal numbered string into real key; |
| 136 | * Convert each two chars into one char(value). If number |
| 137 | * of the length is odd, add zero on the top for rounding. |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 138 | */ |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 139 | |
org[shemminger]!nakam | 54f7328 | 2004-09-28 18:36:52 +0000 | [diff] [blame] | 140 | /* calculate length of the converted values(real key) */ |
| 141 | len = (plen + 1) / 2; |
| 142 | if (len > max) |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 143 | invarg("ALGO-KEYMAT value makes buffer overflow\n", key); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 144 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 145 | for (i = -(plen % 2), j = 0; j < len; i += 2, j++) { |
org[shemminger]!nakam | 54f7328 | 2004-09-28 18:36:52 +0000 | [diff] [blame] | 146 | char vbuf[3]; |
shemminger | 737f15f | 2005-07-08 22:08:47 +0000 | [diff] [blame] | 147 | __u8 val; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 148 | |
org[shemminger]!nakam | 54f7328 | 2004-09-28 18:36:52 +0000 | [diff] [blame] | 149 | vbuf[0] = i >= 0 ? p[i] : '0'; |
| 150 | vbuf[1] = p[i + 1]; |
| 151 | vbuf[2] = '\0'; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 152 | |
org[shemminger]!nakam | 54f7328 | 2004-09-28 18:36:52 +0000 | [diff] [blame] | 153 | if (get_u8(&val, vbuf, 16)) |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 154 | invarg("ALGO-KEYMAT value is invalid", key); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 155 | |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 156 | buf[j] = val; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 157 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 158 | } else { |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 159 | len = slen; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 160 | if (len > 0) { |
| 161 | if (len > max) |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 162 | invarg("ALGO-KEYMAT value makes buffer overflow\n", key); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 163 | |
Fan Du | 99500b5 | 2013-09-30 21:09:05 -0700 | [diff] [blame] | 164 | memcpy(buf, key, len); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | alg->alg_key_len = len * 8; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 173 | static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp) |
| 174 | { |
| 175 | int argc = *argcp; |
| 176 | char **argv = *argvp; |
| 177 | |
Sabrina Dubroca | 9f7401f | 2016-06-03 16:45:46 +0200 | [diff] [blame] | 178 | if (get_be32(seq, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 179 | invarg("SEQ value is invalid", *argv); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 180 | |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 181 | *argcp = argc; |
| 182 | *argvp = argv; |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 187 | static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp) |
| 188 | { |
| 189 | int argc = *argcp; |
| 190 | char **argv = *argvp; |
net[shemminger]!shemminger | 9e566a4 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 191 | int len = strlen(*argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 192 | |
net[shemminger]!shemminger | 9e566a4 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 193 | if (len > 2 && strncmp(*argv, "0x", 2) == 0) { |
| 194 | __u8 val = 0; |
| 195 | |
| 196 | if (get_u8(&val, *argv, 16)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 197 | invarg("FLAG value is invalid", *argv); |
net[shemminger]!shemminger | 9e566a4 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 198 | *flags = val; |
| 199 | } else { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 200 | while (1) { |
| 201 | if (strcmp(*argv, "noecn") == 0) |
| 202 | *flags |= XFRM_STATE_NOECN; |
| 203 | else if (strcmp(*argv, "decap-dscp") == 0) |
| 204 | *flags |= XFRM_STATE_DECAP_DSCP; |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 205 | else if (strcmp(*argv, "nopmtudisc") == 0) |
| 206 | *flags |= XFRM_STATE_NOPMTUDISC; |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 207 | else if (strcmp(*argv, "wildrecv") == 0) |
| 208 | *flags |= XFRM_STATE_WILDRECV; |
Alex Badea | 15bb82c | 2010-01-11 17:23:41 +0200 | [diff] [blame] | 209 | else if (strcmp(*argv, "icmp") == 0) |
| 210 | *flags |= XFRM_STATE_ICMP; |
| 211 | else if (strcmp(*argv, "af-unspec") == 0) |
| 212 | *flags |= XFRM_STATE_AF_UNSPEC; |
Nicolas Dichtel | 98f5519 | 2011-02-01 07:29:54 -0500 | [diff] [blame] | 213 | else if (strcmp(*argv, "align4") == 0) |
| 214 | *flags |= XFRM_STATE_ALIGN4; |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 215 | else if (strcmp(*argv, "esn") == 0) |
| 216 | *flags |= XFRM_STATE_ESN; |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 217 | else { |
| 218 | PREV_ARG(); /* back track */ |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | if (!NEXT_ARG_OK()) |
| 223 | break; |
| 224 | NEXT_ARG(); |
| 225 | } |
net[shemminger]!shemminger | 9e566a4 | 2004-07-30 20:26:15 +0000 | [diff] [blame] | 226 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 227 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 228 | *argcp = argc; |
| 229 | *argvp = argv; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Nicolas Dichtel | dc8867d | 2013-05-17 01:36:38 -0700 | [diff] [blame] | 234 | static int xfrm_state_extra_flag_parse(__u32 *extra_flags, int *argcp, char ***argvp) |
| 235 | { |
| 236 | int argc = *argcp; |
| 237 | char **argv = *argvp; |
| 238 | int len = strlen(*argv); |
| 239 | |
| 240 | if (len > 2 && strncmp(*argv, "0x", 2) == 0) { |
| 241 | __u32 val = 0; |
| 242 | |
| 243 | if (get_u32(&val, *argv, 16)) |
| 244 | invarg("\"EXTRA-FLAG\" is invalid", *argv); |
| 245 | *extra_flags = val; |
| 246 | } else { |
| 247 | while (1) { |
| 248 | if (strcmp(*argv, "dont-encap-dscp") == 0) |
| 249 | *extra_flags |= XFRM_SA_XFLAG_DONT_ENCAP_DSCP; |
| 250 | else { |
| 251 | PREV_ARG(); /* back track */ |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (!NEXT_ARG_OK()) |
| 256 | break; |
| 257 | NEXT_ARG(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | *argcp = argc; |
| 262 | *argvp = argv; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 267 | static int xfrm_state_modify(int cmd, unsigned int flags, int argc, char **argv) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 268 | { |
| 269 | struct rtnl_handle rth; |
| 270 | struct { |
Stephen Hemminger | 4806867 | 2014-02-17 10:56:31 -0800 | [diff] [blame] | 271 | struct nlmsghdr n; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 272 | struct xfrm_usersa_info xsinfo; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 273 | char buf[RTA_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 274 | } req = { |
| 275 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo)), |
| 276 | .n.nlmsg_flags = NLM_F_REQUEST | flags, |
| 277 | .n.nlmsg_type = cmd, |
| 278 | .xsinfo.family = preferred_family, |
| 279 | .xsinfo.lft.soft_byte_limit = XFRM_INF, |
| 280 | .xsinfo.lft.hard_byte_limit = XFRM_INF, |
| 281 | .xsinfo.lft.soft_packet_limit = XFRM_INF, |
| 282 | .xsinfo.lft.hard_packet_limit = XFRM_INF, |
| 283 | }; |
| 284 | struct xfrm_replay_state replay = {}; |
| 285 | struct xfrm_replay_state_esn replay_esn = {}; |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 286 | __u32 replay_window = 0; |
| 287 | __u32 seq = 0, oseq = 0, seq_hi = 0, oseq_hi = 0; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 288 | char *idp = NULL; |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 289 | char *aeadop = NULL; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 290 | char *ealgop = NULL; |
| 291 | char *aalgop = NULL; |
| 292 | char *calgop = NULL; |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 293 | char *coap = NULL; |
Joy Latten | 0c7a594 | 2011-02-02 17:32:59 -0600 | [diff] [blame] | 294 | char *sctxp = NULL; |
Nicolas Dichtel | dc8867d | 2013-05-17 01:36:38 -0700 | [diff] [blame] | 295 | __u32 extra_flags = 0; |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 296 | struct xfrm_mark mark = {0, 0}; |
Joy Latten | 0c7a594 | 2011-02-02 17:32:59 -0600 | [diff] [blame] | 297 | struct { |
| 298 | struct xfrm_user_sec_ctx sctx; |
| 299 | char str[CTX_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 300 | } ctx = {}; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 301 | |
| 302 | while (argc > 0) { |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 303 | if (strcmp(*argv, "mode") == 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 304 | NEXT_ARG(); |
| 305 | xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv); |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 306 | } else if (strcmp(*argv, "mark") == 0) { |
| 307 | xfrm_parse_mark(&mark, &argc, &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 308 | } else if (strcmp(*argv, "reqid") == 0) { |
| 309 | NEXT_ARG(); |
| 310 | xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 311 | } else if (strcmp(*argv, "seq") == 0) { |
| 312 | NEXT_ARG(); |
| 313 | xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv); |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 314 | } else if (strcmp(*argv, "replay-window") == 0) { |
| 315 | NEXT_ARG(); |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 316 | if (get_u32(&replay_window, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 317 | invarg("value after \"replay-window\" is invalid", *argv); |
Herbert Xu | de95ae7 | 2008-04-23 15:42:32 +0800 | [diff] [blame] | 318 | } else if (strcmp(*argv, "replay-seq") == 0) { |
| 319 | NEXT_ARG(); |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 320 | if (get_u32(&seq, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 321 | invarg("value after \"replay-seq\" is invalid", *argv); |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 322 | } else if (strcmp(*argv, "replay-seq-hi") == 0) { |
| 323 | NEXT_ARG(); |
| 324 | if (get_u32(&seq_hi, *argv, 0)) |
| 325 | invarg("value after \"replay-seq-hi\" is invalid", *argv); |
Herbert Xu | de95ae7 | 2008-04-23 15:42:32 +0800 | [diff] [blame] | 326 | } else if (strcmp(*argv, "replay-oseq") == 0) { |
| 327 | NEXT_ARG(); |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 328 | if (get_u32(&oseq, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 329 | invarg("value after \"replay-oseq\" is invalid", *argv); |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 330 | } else if (strcmp(*argv, "replay-oseq-hi") == 0) { |
| 331 | NEXT_ARG(); |
| 332 | if (get_u32(&oseq_hi, *argv, 0)) |
| 333 | invarg("value after \"replay-oseq-hi\" is invalid", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 334 | } else if (strcmp(*argv, "flag") == 0) { |
| 335 | NEXT_ARG(); |
| 336 | xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv); |
Nicolas Dichtel | dc8867d | 2013-05-17 01:36:38 -0700 | [diff] [blame] | 337 | } else if (strcmp(*argv, "extra-flag") == 0) { |
| 338 | NEXT_ARG(); |
| 339 | xfrm_state_extra_flag_parse(&extra_flags, &argc, &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 340 | } else if (strcmp(*argv, "sel") == 0) { |
| 341 | NEXT_ARG(); |
Thomas Egerer | 23d5b0d | 2013-03-17 00:56:01 +0000 | [diff] [blame] | 342 | preferred_family = AF_UNSPEC; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 343 | xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv); |
Thomas Egerer | 0c5982f | 2013-03-20 02:18:43 -0700 | [diff] [blame] | 344 | preferred_family = req.xsinfo.sel.family; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 345 | } else if (strcmp(*argv, "limit") == 0) { |
| 346 | NEXT_ARG(); |
| 347 | xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv); |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 348 | } else if (strcmp(*argv, "encap") == 0) { |
| 349 | struct xfrm_encap_tmpl encap; |
| 350 | inet_prefix oa; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 351 | NEXT_ARG(); |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 352 | xfrm_encap_type_parse(&encap.encap_type, &argc, &argv); |
| 353 | NEXT_ARG(); |
Sabrina Dubroca | 9f7401f | 2016-06-03 16:45:46 +0200 | [diff] [blame] | 354 | if (get_be16(&encap.encap_sport, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 355 | invarg("SPORT value after \"encap\" is invalid", *argv); |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 356 | NEXT_ARG(); |
Sabrina Dubroca | 9f7401f | 2016-06-03 16:45:46 +0200 | [diff] [blame] | 357 | if (get_be16(&encap.encap_dport, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 358 | invarg("DPORT value after \"encap\" is invalid", *argv); |
osdl.net!shemminger | 5cf576d | 2005-03-10 19:04:00 +0000 | [diff] [blame] | 359 | NEXT_ARG(); |
| 360 | get_addr(&oa, *argv, AF_UNSPEC); |
| 361 | memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa)); |
| 362 | addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP, |
| 363 | (void *)&encap, sizeof(encap)); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 364 | } else if (strcmp(*argv, "coa") == 0) { |
| 365 | inet_prefix coa; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 366 | xfrm_address_t xcoa = {}; |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 367 | |
| 368 | if (coap) |
| 369 | duparg("coa", *argv); |
| 370 | coap = *argv; |
| 371 | |
| 372 | NEXT_ARG(); |
| 373 | |
| 374 | get_prefix(&coa, *argv, preferred_family); |
| 375 | if (coa.family == AF_UNSPEC) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 376 | invarg("value after \"coa\" has an unrecognized address family", *argv); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 377 | if (coa.bytelen > sizeof(xcoa)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 378 | invarg("value after \"coa\" is too large", *argv); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 379 | |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 380 | memcpy(&xcoa, &coa.data, coa.bytelen); |
| 381 | |
| 382 | addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR, |
| 383 | (void *)&xcoa, sizeof(xcoa)); |
Joy Latten | 0c7a594 | 2011-02-02 17:32:59 -0600 | [diff] [blame] | 384 | } else if (strcmp(*argv, "ctx") == 0) { |
| 385 | char *context; |
| 386 | |
| 387 | if (sctxp) |
| 388 | duparg("ctx", *argv); |
| 389 | sctxp = *argv; |
| 390 | |
| 391 | NEXT_ARG(); |
| 392 | context = *argv; |
| 393 | |
| 394 | xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx); |
| 395 | addattr_l(&req.n, sizeof(req.buf), XFRMA_SEC_CTX, |
| 396 | (void *)&ctx, ctx.sctx.len); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 397 | } else { |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 398 | /* try to assume ALGO */ |
| 399 | int type = xfrm_algotype_getbyname(*argv); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 400 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 401 | switch (type) { |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 402 | case XFRMA_ALG_AEAD: |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 403 | case XFRMA_ALG_CRYPT: |
| 404 | case XFRMA_ALG_AUTH: |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 405 | case XFRMA_ALG_AUTH_TRUNC: |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 406 | case XFRMA_ALG_COMP: |
| 407 | { |
| 408 | /* ALGO */ |
| 409 | struct { |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 410 | union { |
| 411 | struct xfrm_algo alg; |
| 412 | struct xfrm_algo_aead aead; |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 413 | struct xfrm_algo_auth auth; |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 414 | } u; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 415 | char buf[XFRM_ALGO_KEY_BUF_SIZE]; |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 416 | } alg = {}; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 417 | int len; |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 418 | __u32 icvlen, trunclen; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 419 | char *name; |
David Ward | f3b9aa3 | 2013-03-25 04:23:17 +0000 | [diff] [blame] | 420 | char *key = ""; |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 421 | char *buf; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 422 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 423 | switch (type) { |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 424 | case XFRMA_ALG_AEAD: |
David Ward | ec83952 | 2013-03-25 04:23:14 +0000 | [diff] [blame] | 425 | if (ealgop || aalgop || aeadop) |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 426 | duparg("ALGO-TYPE", *argv); |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 427 | aeadop = *argv; |
| 428 | break; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 429 | case XFRMA_ALG_CRYPT: |
David Ward | ec83952 | 2013-03-25 04:23:14 +0000 | [diff] [blame] | 430 | if (ealgop || aeadop) |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 431 | duparg("ALGO-TYPE", *argv); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 432 | ealgop = *argv; |
| 433 | break; |
| 434 | case XFRMA_ALG_AUTH: |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 435 | case XFRMA_ALG_AUTH_TRUNC: |
David Ward | ec83952 | 2013-03-25 04:23:14 +0000 | [diff] [blame] | 436 | if (aalgop || aeadop) |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 437 | duparg("ALGO-TYPE", *argv); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 438 | aalgop = *argv; |
| 439 | break; |
| 440 | case XFRMA_ALG_COMP: |
| 441 | if (calgop) |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 442 | duparg("ALGO-TYPE", *argv); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 443 | calgop = *argv; |
| 444 | break; |
| 445 | default: |
| 446 | /* not reached */ |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 447 | invarg("ALGO-TYPE value is invalid\n", *argv); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (!NEXT_ARG_OK()) |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 451 | missarg("ALGO-NAME"); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 452 | NEXT_ARG(); |
| 453 | name = *argv; |
| 454 | |
David Ward | f3b9aa3 | 2013-03-25 04:23:17 +0000 | [diff] [blame] | 455 | switch (type) { |
| 456 | case XFRMA_ALG_AEAD: |
| 457 | case XFRMA_ALG_CRYPT: |
| 458 | case XFRMA_ALG_AUTH: |
| 459 | case XFRMA_ALG_AUTH_TRUNC: |
| 460 | if (!NEXT_ARG_OK()) |
David Ward | 29665f9 | 2013-03-25 04:23:18 +0000 | [diff] [blame] | 461 | missarg("ALGO-KEYMAT"); |
David Ward | f3b9aa3 | 2013-03-25 04:23:17 +0000 | [diff] [blame] | 462 | NEXT_ARG(); |
| 463 | key = *argv; |
| 464 | break; |
| 465 | } |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 466 | |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 467 | buf = alg.u.alg.alg_key; |
| 468 | len = sizeof(alg.u.alg); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 469 | |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 470 | switch (type) { |
| 471 | case XFRMA_ALG_AEAD: |
| 472 | if (!NEXT_ARG_OK()) |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 473 | missarg("ALGO-ICV-LEN"); |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 474 | NEXT_ARG(); |
| 475 | if (get_u32(&icvlen, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 476 | invarg("ALGO-ICV-LEN value is invalid", |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 477 | *argv); |
| 478 | alg.u.aead.alg_icv_len = icvlen; |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 479 | |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 480 | buf = alg.u.aead.alg_key; |
| 481 | len = sizeof(alg.u.aead); |
| 482 | break; |
| 483 | case XFRMA_ALG_AUTH_TRUNC: |
| 484 | if (!NEXT_ARG_OK()) |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 485 | missarg("ALGO-TRUNC-LEN"); |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 486 | NEXT_ARG(); |
| 487 | if (get_u32(&trunclen, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 488 | invarg("ALGO-TRUNC-LEN value is invalid", |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 489 | *argv); |
| 490 | alg.u.auth.alg_trunc_len = trunclen; |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 491 | |
Nicolas Dichtel | f323f2a | 2011-01-11 06:32:46 +0000 | [diff] [blame] | 492 | buf = alg.u.auth.alg_key; |
| 493 | len = sizeof(alg.u.auth); |
| 494 | break; |
| 495 | } |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 496 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 497 | xfrm_algo_parse((void *)&alg, type, name, key, |
Herbert Xu | 1758a81 | 2008-09-17 22:09:01 -0700 | [diff] [blame] | 498 | buf, sizeof(alg.buf)); |
| 499 | len += alg.u.alg.alg_key_len; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 500 | |
| 501 | addattr_l(&req.n, sizeof(req.buf), type, |
| 502 | (void *)&alg, len); |
| 503 | break; |
| 504 | } |
| 505 | default: |
| 506 | /* try to assume ID */ |
| 507 | if (idp) |
| 508 | invarg("unknown", *argv); |
| 509 | idp = *argv; |
| 510 | |
| 511 | /* ID */ |
| 512 | xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id, |
| 513 | &req.xsinfo.family, 0, &argc, &argv); |
| 514 | if (preferred_family == AF_UNSPEC) |
| 515 | preferred_family = req.xsinfo.family; |
| 516 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 517 | } |
| 518 | argc--; argv++; |
| 519 | } |
| 520 | |
dingzhi | 0151b56 | 2014-10-20 11:23:04 +0200 | [diff] [blame] | 521 | if (req.xsinfo.flags & XFRM_STATE_ESN && |
| 522 | replay_window == 0) { |
| 523 | fprintf(stderr, "Error: esn flag set without replay-window.\n"); |
| 524 | exit(-1); |
| 525 | } |
| 526 | |
| 527 | if (replay_window > XFRMA_REPLAY_ESN_MAX) { |
| 528 | fprintf(stderr, |
| 529 | "Error: replay-window (%u) > XFRMA_REPLAY_ESN_MAX (%u).\n", |
| 530 | replay_window, XFRMA_REPLAY_ESN_MAX); |
| 531 | exit(-1); |
| 532 | } |
| 533 | |
| 534 | if (req.xsinfo.flags & XFRM_STATE_ESN || |
| 535 | replay_window > (sizeof(replay.bitmap) * 8)) { |
| 536 | replay_esn.seq = seq; |
| 537 | replay_esn.oseq = oseq; |
| 538 | replay_esn.seq_hi = seq_hi; |
| 539 | replay_esn.oseq_hi = oseq_hi; |
| 540 | replay_esn.replay_window = replay_window; |
| 541 | replay_esn.bmp_len = (replay_window + sizeof(__u32) * 8 - 1) / |
| 542 | (sizeof(__u32) * 8); |
| 543 | addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_ESN_VAL, |
| 544 | &replay_esn, sizeof(replay_esn)); |
| 545 | } else { |
| 546 | if (seq || oseq) { |
| 547 | replay.seq = seq; |
| 548 | replay.oseq = oseq; |
| 549 | addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL, |
| 550 | &replay, sizeof(replay)); |
| 551 | } |
| 552 | req.xsinfo.replay_window = replay_window; |
| 553 | } |
Herbert Xu | de95ae7 | 2008-04-23 15:42:32 +0800 | [diff] [blame] | 554 | |
Nicolas Dichtel | dc8867d | 2013-05-17 01:36:38 -0700 | [diff] [blame] | 555 | if (extra_flags) |
| 556 | addattr32(&req.n, sizeof(req.buf), XFRMA_SA_EXTRA_FLAGS, |
| 557 | extra_flags); |
| 558 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 559 | if (!idp) { |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 560 | fprintf(stderr, "Not enough information: ID is required\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 561 | exit(1); |
| 562 | } |
| 563 | |
Christophe Gouault | b557416 | 2013-10-08 05:56:54 -0700 | [diff] [blame] | 564 | if (mark.m) { |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 565 | int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, |
| 566 | (void *)&mark, sizeof(mark)); |
| 567 | if (r < 0) { |
| 568 | fprintf(stderr, "XFRMA_MARK failed\n"); |
| 569 | exit(1); |
| 570 | } |
| 571 | } |
| 572 | |
David Ward | 6128fdf | 2013-03-25 04:23:15 +0000 | [diff] [blame] | 573 | if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) { |
| 574 | switch (req.xsinfo.mode) { |
| 575 | case XFRM_MODE_TRANSPORT: |
| 576 | case XFRM_MODE_TUNNEL: |
| 577 | break; |
| 578 | case XFRM_MODE_BEET: |
| 579 | if (req.xsinfo.id.proto == IPPROTO_ESP) |
| 580 | break; |
| 581 | default: |
| 582 | fprintf(stderr, "MODE value is invalid with XFRM-PROTO value \"%s\"\n", |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 583 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 584 | exit(1); |
| 585 | } |
David Ward | 6128fdf | 2013-03-25 04:23:15 +0000 | [diff] [blame] | 586 | |
| 587 | switch (req.xsinfo.id.proto) { |
| 588 | case IPPROTO_ESP: |
| 589 | if (calgop) { |
| 590 | fprintf(stderr, "ALGO-TYPE value \"%s\" is invalid with XFRM-PROTO value \"%s\"\n", |
| 591 | strxf_algotype(XFRMA_ALG_COMP), |
| 592 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 593 | exit(1); |
| 594 | } |
| 595 | if (!ealgop && !aeadop) { |
| 596 | fprintf(stderr, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n", |
| 597 | strxf_algotype(XFRMA_ALG_CRYPT), |
| 598 | strxf_algotype(XFRMA_ALG_AEAD), |
| 599 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 600 | exit(1); |
| 601 | } |
| 602 | break; |
| 603 | case IPPROTO_AH: |
| 604 | if (ealgop || aeadop || calgop) { |
| 605 | fprintf(stderr, "ALGO-TYPE values \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n", |
| 606 | strxf_algotype(XFRMA_ALG_CRYPT), |
| 607 | strxf_algotype(XFRMA_ALG_AEAD), |
| 608 | strxf_algotype(XFRMA_ALG_COMP), |
| 609 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 610 | exit(1); |
| 611 | } |
| 612 | if (!aalgop) { |
| 613 | fprintf(stderr, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n", |
| 614 | strxf_algotype(XFRMA_ALG_AUTH), |
| 615 | strxf_algotype(XFRMA_ALG_AUTH_TRUNC), |
| 616 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 617 | exit(1); |
| 618 | } |
| 619 | break; |
| 620 | case IPPROTO_COMP: |
| 621 | if (ealgop || aalgop || aeadop) { |
| 622 | fprintf(stderr, "ALGO-TYPE values \"%s\", \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n", |
| 623 | strxf_algotype(XFRMA_ALG_CRYPT), |
| 624 | strxf_algotype(XFRMA_ALG_AUTH), |
| 625 | strxf_algotype(XFRMA_ALG_AUTH_TRUNC), |
| 626 | strxf_algotype(XFRMA_ALG_AEAD), |
| 627 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 628 | exit(1); |
| 629 | } |
| 630 | if (!calgop) { |
| 631 | fprintf(stderr, "ALGO-TYPE value \"%s\" is required with XFRM-PROTO value \"%s\"\n", |
| 632 | strxf_algotype(XFRMA_ALG_COMP), |
| 633 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 634 | exit(1); |
| 635 | } |
| 636 | break; |
| 637 | } |
| 638 | } else { |
| 639 | if (ealgop || aalgop || aeadop || calgop) { |
| 640 | fprintf(stderr, "ALGO is invalid with XFRM-PROTO value \"%s\"\n", |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 641 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 642 | exit(1); |
| 643 | } |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 644 | } |
| 645 | |
David Ward | 6128fdf | 2013-03-25 04:23:15 +0000 | [diff] [blame] | 646 | if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) { |
| 647 | switch (req.xsinfo.mode) { |
| 648 | case XFRM_MODE_ROUTEOPTIMIZATION: |
| 649 | case XFRM_MODE_IN_TRIGGER: |
| 650 | break; |
| 651 | case 0: |
| 652 | fprintf(stderr, "\"mode\" is required with XFRM-PROTO value \"%s\"\n", |
| 653 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 654 | exit(1); |
| 655 | default: |
| 656 | fprintf(stderr, "MODE value is invalid with XFRM-PROTO value \"%s\"\n", |
| 657 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 658 | exit(1); |
| 659 | } |
| 660 | |
| 661 | if (!coap) { |
| 662 | fprintf(stderr, "\"coa\" is required with XFRM-PROTO value \"%s\"\n", |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 663 | strxf_xfrmproto(req.xsinfo.id.proto)); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 664 | exit(1); |
| 665 | } |
| 666 | } else { |
David Ward | 6128fdf | 2013-03-25 04:23:15 +0000 | [diff] [blame] | 667 | if (coap) { |
| 668 | fprintf(stderr, "\"coa\" is invalid with XFRM-PROTO value \"%s\"\n", |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 669 | strxf_xfrmproto(req.xsinfo.id.proto)); |
| 670 | exit(1); |
| 671 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 675 | exit(1); |
| 676 | |
| 677 | if (req.xsinfo.family == AF_UNSPEC) |
| 678 | req.xsinfo.family = AF_INET; |
| 679 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 680 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 681 | exit(2); |
| 682 | |
| 683 | rtnl_close(&rth); |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 688 | static int xfrm_state_allocspi(int argc, char **argv) |
| 689 | { |
| 690 | struct rtnl_handle rth; |
| 691 | struct { |
Stephen Hemminger | 4806867 | 2014-02-17 10:56:31 -0800 | [diff] [blame] | 692 | struct nlmsghdr n; |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 693 | struct xfrm_userspi_info xspi; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 694 | char buf[RTA_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 695 | } req = { |
| 696 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi)), |
| 697 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 698 | .n.nlmsg_type = XFRM_MSG_ALLOCSPI, |
| 699 | .xspi.info.family = preferred_family, |
| 700 | #if 0 |
| 701 | .xspi.lft.soft_byte_limit = XFRM_INF, |
| 702 | .xspi.lft.hard_byte_limit = XFRM_INF, |
| 703 | .xspi.lft.soft_packet_limit = XFRM_INF, |
| 704 | .xspi.lft.hard_packet_limit = XFRM_INF, |
| 705 | #endif |
| 706 | }; |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 707 | char *idp = NULL; |
| 708 | char *minp = NULL; |
| 709 | char *maxp = NULL; |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 710 | struct xfrm_mark mark = {0, 0}; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 711 | char res_buf[NLMSG_BUF_SIZE] = {}; |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 712 | struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf; |
| 713 | |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 714 | while (argc > 0) { |
| 715 | if (strcmp(*argv, "mode") == 0) { |
| 716 | NEXT_ARG(); |
| 717 | xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv); |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 718 | } else if (strcmp(*argv, "mark") == 0) { |
| 719 | xfrm_parse_mark(&mark, &argc, &argv); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 720 | } else if (strcmp(*argv, "reqid") == 0) { |
| 721 | NEXT_ARG(); |
| 722 | xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv); |
| 723 | } else if (strcmp(*argv, "seq") == 0) { |
| 724 | NEXT_ARG(); |
| 725 | xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv); |
| 726 | } else if (strcmp(*argv, "min") == 0) { |
| 727 | if (minp) |
| 728 | duparg("min", *argv); |
| 729 | minp = *argv; |
| 730 | |
| 731 | NEXT_ARG(); |
| 732 | |
| 733 | if (get_u32(&req.xspi.min, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 734 | invarg("value after \"min\" is invalid", *argv); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 735 | } else if (strcmp(*argv, "max") == 0) { |
| 736 | if (maxp) |
| 737 | duparg("max", *argv); |
| 738 | maxp = *argv; |
| 739 | |
| 740 | NEXT_ARG(); |
| 741 | |
| 742 | if (get_u32(&req.xspi.max, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 743 | invarg("value after \"max\" is invalid", *argv); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 744 | } else { |
| 745 | /* try to assume ID */ |
| 746 | if (idp) |
| 747 | invarg("unknown", *argv); |
| 748 | idp = *argv; |
| 749 | |
| 750 | /* ID */ |
| 751 | xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id, |
| 752 | &req.xspi.info.family, 0, &argc, &argv); |
| 753 | if (req.xspi.info.id.spi) { |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 754 | fprintf(stderr, "\"spi\" is invalid\n"); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 755 | exit(1); |
| 756 | } |
| 757 | if (preferred_family == AF_UNSPEC) |
| 758 | preferred_family = req.xspi.info.family; |
| 759 | } |
| 760 | argc--; argv++; |
| 761 | } |
| 762 | |
| 763 | if (!idp) { |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 764 | fprintf(stderr, "Not enough information: ID is required\n"); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 765 | exit(1); |
| 766 | } |
| 767 | |
| 768 | if (minp) { |
| 769 | if (!maxp) { |
| 770 | fprintf(stderr, "\"max\" is missing\n"); |
| 771 | exit(1); |
| 772 | } |
| 773 | if (req.xspi.min > req.xspi.max) { |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 774 | fprintf(stderr, "value after \"min\" is larger than value after \"max\"\n"); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 775 | exit(1); |
| 776 | } |
| 777 | } else { |
| 778 | if (maxp) { |
| 779 | fprintf(stderr, "\"min\" is missing\n"); |
| 780 | exit(1); |
| 781 | } |
| 782 | |
| 783 | /* XXX: Default value defined in PF_KEY; |
| 784 | * See kernel's net/key/af_key.c(pfkey_getspi). |
| 785 | */ |
| 786 | req.xspi.min = 0x100; |
| 787 | req.xspi.max = 0x0fffffff; |
| 788 | |
| 789 | /* XXX: IPCOMP spi is 16-bits; |
| 790 | * See kernel's net/xfrm/xfrm_user(verify_userspi_info). |
| 791 | */ |
| 792 | if (req.xspi.info.id.proto == IPPROTO_COMP) |
| 793 | req.xspi.max = 0xffff; |
| 794 | } |
| 795 | |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 796 | if (mark.m & mark.v) { |
| 797 | int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, |
| 798 | (void *)&mark, sizeof(mark)); |
| 799 | if (r < 0) { |
| 800 | fprintf(stderr, "XFRMA_MARK failed\n"); |
| 801 | exit(1); |
| 802 | } |
| 803 | } |
| 804 | |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 805 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 806 | exit(1); |
| 807 | |
| 808 | if (req.xspi.info.family == AF_UNSPEC) |
| 809 | req.xspi.info.family = AF_INET; |
| 810 | |
| 811 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 812 | if (rtnl_talk(&rth, &req.n, res_n, sizeof(res_buf)) < 0) |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 813 | exit(2); |
| 814 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 815 | if (xfrm_state_print(NULL, res_n, (void *)stdout) < 0) { |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 816 | fprintf(stderr, "An error :-)\n"); |
| 817 | exit(1); |
| 818 | } |
| 819 | |
| 820 | rtnl_close(&rth); |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 825 | static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo) |
| 826 | { |
| 827 | if (!filter.use) |
| 828 | return 1; |
| 829 | |
| 830 | if (filter.id_src_mask) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 831 | if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr, |
| 832 | filter.id_src_mask)) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 833 | return 0; |
| 834 | if (filter.id_dst_mask) |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 835 | if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr, |
| 836 | filter.id_dst_mask)) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 837 | return 0; |
| 838 | if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask) |
| 839 | return 0; |
| 840 | if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask) |
| 841 | return 0; |
| 842 | if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask) |
| 843 | return 0; |
| 844 | if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask) |
| 845 | return 0; |
| 846 | if (filter.state_flags_mask) |
| 847 | if ((xsinfo->flags & filter.xsinfo.flags) == 0) |
| 848 | return 0; |
| 849 | |
| 850 | return 1; |
| 851 | } |
| 852 | |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 853 | int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n, |
| 854 | void *arg) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 855 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 856 | FILE *fp = (FILE *)arg; |
| 857 | struct rtattr *tb[XFRMA_MAX+1]; |
| 858 | struct rtattr *rta; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 859 | struct xfrm_usersa_info *xsinfo = NULL; |
| 860 | struct xfrm_user_expire *xexp = NULL; |
| 861 | struct xfrm_usersa_id *xsid = NULL; |
| 862 | int len = n->nlmsg_len; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 863 | |
| 864 | if (n->nlmsg_type != XFRM_MSG_NEWSA && |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 865 | n->nlmsg_type != XFRM_MSG_DELSA && |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 866 | n->nlmsg_type != XFRM_MSG_UPDSA && |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 867 | n->nlmsg_type != XFRM_MSG_EXPIRE) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 868 | fprintf(stderr, "Not a state: %08x %08x %08x\n", |
| 869 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 870 | return 0; |
| 871 | } |
| 872 | |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 873 | if (n->nlmsg_type == XFRM_MSG_DELSA) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 874 | /* Dont blame me for this .. Herbert made me do it */ |
| 875 | xsid = NLMSG_DATA(n); |
Andy Gay | af1b6a4 | 2006-08-10 20:25:40 -0400 | [diff] [blame] | 876 | len -= NLMSG_SPACE(sizeof(*xsid)); |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 877 | } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) { |
| 878 | xexp = NLMSG_DATA(n); |
| 879 | xsinfo = &xexp->state; |
Andy Gay | af1b6a4 | 2006-08-10 20:25:40 -0400 | [diff] [blame] | 880 | len -= NLMSG_SPACE(sizeof(*xexp)); |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 881 | } else { |
| 882 | xexp = NULL; |
| 883 | xsinfo = NLMSG_DATA(n); |
Andy Gay | af1b6a4 | 2006-08-10 20:25:40 -0400 | [diff] [blame] | 884 | len -= NLMSG_SPACE(sizeof(*xsinfo)); |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 885 | } |
| 886 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 887 | if (len < 0) { |
| 888 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 889 | return -1; |
| 890 | } |
| 891 | |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 892 | if (xsinfo && !xfrm_state_filter_match(xsinfo)) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 893 | return 0; |
| 894 | |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 895 | if (n->nlmsg_type == XFRM_MSG_DELSA) |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 896 | fprintf(fp, "Deleted "); |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 897 | else if (n->nlmsg_type == XFRM_MSG_UPDSA) |
| 898 | fprintf(fp, "Updated "); |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 899 | else if (n->nlmsg_type == XFRM_MSG_EXPIRE) |
| 900 | fprintf(fp, "Expired "); |
| 901 | |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 902 | if (n->nlmsg_type == XFRM_MSG_DELSA) |
| 903 | rta = XFRMSID_RTA(xsid); |
| 904 | else if (n->nlmsg_type == XFRM_MSG_EXPIRE) |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 905 | rta = XFRMEXP_RTA(xexp); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 906 | else |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 907 | rta = XFRMS_RTA(xsinfo); |
| 908 | |
| 909 | parse_rtattr(tb, XFRMA_MAX, rta, len); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 910 | |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 911 | if (n->nlmsg_type == XFRM_MSG_DELSA) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 912 | /* xfrm_policy_id_print(); */ |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 913 | |
| 914 | if (!tb[XFRMA_SA]) { |
| 915 | fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n"); |
| 916 | return -1; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 917 | } |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 918 | if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) { |
| 919 | fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n"); |
| 920 | return -1; |
| 921 | } |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 922 | xsinfo = RTA_DATA(tb[XFRMA_SA]); |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 923 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 924 | |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 925 | xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 926 | |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 927 | if (n->nlmsg_type == XFRM_MSG_EXPIRE) { |
| 928 | fprintf(fp, "\t"); |
| 929 | fprintf(fp, "hard %u", xexp->hard); |
| 930 | fprintf(fp, "%s", _SL_); |
| 931 | } |
| 932 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 933 | if (oneline) |
| 934 | fprintf(fp, "\n"); |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 935 | fflush(fp); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 936 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | static int xfrm_state_get_or_delete(int argc, char **argv, int delete) |
| 941 | { |
| 942 | struct rtnl_handle rth; |
| 943 | struct { |
Stephen Hemminger | 4806867 | 2014-02-17 10:56:31 -0800 | [diff] [blame] | 944 | struct nlmsghdr n; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 945 | struct xfrm_usersa_id xsid; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 946 | char buf[RTA_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 947 | } req = { |
| 948 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid)), |
| 949 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 950 | .n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA, |
| 951 | .xsid.family = preferred_family, |
| 952 | }; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 953 | struct xfrm_id id; |
| 954 | char *idp = NULL; |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 955 | struct xfrm_mark mark = {0, 0}; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 956 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 957 | while (argc > 0) { |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 958 | xfrm_address_t saddr; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 959 | |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 960 | if (strcmp(*argv, "mark") == 0) { |
| 961 | xfrm_parse_mark(&mark, &argc, &argv); |
| 962 | } else { |
| 963 | if (idp) |
| 964 | invarg("unknown", *argv); |
| 965 | idp = *argv; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 966 | |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 967 | /* ID */ |
| 968 | memset(&id, 0, sizeof(id)); |
| 969 | memset(&saddr, 0, sizeof(saddr)); |
| 970 | xfrm_id_parse(&saddr, &id, &req.xsid.family, 0, |
| 971 | &argc, &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 972 | |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 973 | memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr)); |
| 974 | req.xsid.spi = id.spi; |
| 975 | req.xsid.proto = id.proto; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 976 | |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 977 | addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR, |
| 978 | (void *)&saddr, sizeof(saddr)); |
| 979 | } |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 980 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 981 | argc--; argv++; |
| 982 | } |
| 983 | |
Jamal Hadi Salim | c90cda9 | 2010-02-23 03:15:12 +0000 | [diff] [blame] | 984 | if (mark.m & mark.v) { |
| 985 | int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, |
| 986 | (void *)&mark, sizeof(mark)); |
| 987 | if (r < 0) { |
| 988 | fprintf(stderr, "XFRMA_MARK failed\n"); |
| 989 | exit(1); |
| 990 | } |
| 991 | } |
| 992 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 993 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 994 | exit(1); |
| 995 | |
| 996 | if (req.xsid.family == AF_UNSPEC) |
| 997 | req.xsid.family = AF_INET; |
| 998 | |
| 999 | if (delete) { |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 1000 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1001 | exit(2); |
| 1002 | } else { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 1003 | char buf[NLMSG_BUF_SIZE] = {}; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1004 | struct nlmsghdr *res_n = (struct nlmsghdr *)buf; |
| 1005 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 1006 | if (rtnl_talk(&rth, &req.n, res_n, sizeof(req)) < 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1007 | exit(2); |
| 1008 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1009 | if (xfrm_state_print(NULL, res_n, (void *)stdout) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1010 | fprintf(stderr, "An error :-)\n"); |
| 1011 | exit(1); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | rtnl_close(&rth); |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * With an existing state of nlmsg, make new nlmsg for deleting the state |
| 1022 | * and store it to buffer. |
| 1023 | */ |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 1024 | static int xfrm_state_keep(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 1025 | struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 1026 | void *arg) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1027 | { |
| 1028 | struct xfrm_buffer *xb = (struct xfrm_buffer *)arg; |
| 1029 | struct rtnl_handle *rth = xb->rth; |
| 1030 | struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n); |
| 1031 | int len = n->nlmsg_len; |
| 1032 | struct nlmsghdr *new_n; |
| 1033 | struct xfrm_usersa_id *xsid; |
| 1034 | |
| 1035 | if (n->nlmsg_type != XFRM_MSG_NEWSA) { |
| 1036 | fprintf(stderr, "Not a state: %08x %08x %08x\n", |
| 1037 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | len -= NLMSG_LENGTH(sizeof(*xsinfo)); |
| 1042 | if (len < 0) { |
| 1043 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | if (!xfrm_state_filter_match(xsinfo)) |
| 1048 | return 0; |
| 1049 | |
| 1050 | if (xb->offset > xb->size) { |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1051 | fprintf(stderr, "State buffer overflow\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1052 | return -1; |
| 1053 | } |
| 1054 | |
| 1055 | new_n = (struct nlmsghdr *)(xb->buf + xb->offset); |
| 1056 | new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid)); |
| 1057 | new_n->nlmsg_flags = NLM_F_REQUEST; |
| 1058 | new_n->nlmsg_type = XFRM_MSG_DELSA; |
| 1059 | new_n->nlmsg_seq = ++rth->seq; |
| 1060 | |
| 1061 | xsid = NLMSG_DATA(new_n); |
| 1062 | xsid->family = xsinfo->family; |
| 1063 | memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr)); |
| 1064 | xsid->spi = xsinfo->id.spi; |
| 1065 | xsid->proto = xsinfo->id.proto; |
| 1066 | |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 1067 | addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr, |
| 1068 | sizeof(xsid->daddr)); |
| 1069 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1070 | xb->offset += new_n->nlmsg_len; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1071 | xb->nlmsg_count++; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1076 | static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1077 | { |
| 1078 | char *idp = NULL; |
| 1079 | struct rtnl_handle rth; |
| 1080 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1081 | if (argc > 0) |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1082 | filter.use = 1; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1083 | filter.xsinfo.family = preferred_family; |
| 1084 | |
| 1085 | while (argc > 0) { |
| 1086 | if (strcmp(*argv, "mode") == 0) { |
| 1087 | NEXT_ARG(); |
| 1088 | xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv); |
| 1089 | |
| 1090 | filter.mode_mask = XFRM_FILTER_MASK_FULL; |
| 1091 | |
| 1092 | } else if (strcmp(*argv, "reqid") == 0) { |
| 1093 | NEXT_ARG(); |
| 1094 | xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv); |
| 1095 | |
| 1096 | filter.reqid_mask = XFRM_FILTER_MASK_FULL; |
| 1097 | |
| 1098 | } else if (strcmp(*argv, "flag") == 0) { |
| 1099 | NEXT_ARG(); |
| 1100 | xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv); |
| 1101 | |
| 1102 | filter.state_flags_mask = XFRM_FILTER_MASK_FULL; |
| 1103 | |
| 1104 | } else { |
| 1105 | if (idp) |
| 1106 | invarg("unknown", *argv); |
| 1107 | idp = *argv; |
| 1108 | |
| 1109 | /* ID */ |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 1110 | xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id, |
| 1111 | &filter.xsinfo.family, 1, &argc, &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1112 | if (preferred_family == AF_UNSPEC) |
| 1113 | preferred_family = filter.xsinfo.family; |
| 1114 | } |
| 1115 | argc--; argv++; |
| 1116 | } |
| 1117 | |
| 1118 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 1119 | exit(1); |
| 1120 | |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1121 | if (deleteall) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1122 | struct xfrm_buffer xb; |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1123 | char buf[NLMSG_DELETEALL_BUF_SIZE]; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1124 | int i; |
| 1125 | |
| 1126 | xb.buf = buf; |
| 1127 | xb.size = sizeof(buf); |
| 1128 | xb.rth = &rth; |
| 1129 | |
| 1130 | for (i = 0; ; i++) { |
Nicolas Dichtel | 782cf01 | 2015-04-15 14:00:53 +0200 | [diff] [blame] | 1131 | struct { |
| 1132 | struct nlmsghdr n; |
| 1133 | char buf[NLMSG_BUF_SIZE]; |
| 1134 | } req = { |
| 1135 | .n.nlmsg_len = NLMSG_HDRLEN, |
| 1136 | .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 1137 | .n.nlmsg_type = XFRM_MSG_GETSA, |
| 1138 | .n.nlmsg_seq = rth.dump = ++rth.seq, |
| 1139 | }; |
| 1140 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1141 | xb.offset = 0; |
| 1142 | xb.nlmsg_count = 0; |
| 1143 | |
| 1144 | if (show_stats > 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1145 | fprintf(stderr, "Delete-all round = %d\n", i); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1146 | |
Nicolas Dichtel | 782cf01 | 2015-04-15 14:00:53 +0200 | [diff] [blame] | 1147 | if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1148 | perror("Cannot send dump request"); |
| 1149 | exit(1); |
| 1150 | } |
| 1151 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 1152 | if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb) < 0) { |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1153 | fprintf(stderr, "Delete-all terminated\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1154 | exit(1); |
| 1155 | } |
| 1156 | if (xb.nlmsg_count == 0) { |
| 1157 | if (show_stats > 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1158 | fprintf(stderr, "Delete-all completed\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | } |
| 1161 | |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 1162 | if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) { |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1163 | perror("Failed to send delete-all request\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1164 | exit(1); |
| 1165 | } |
| 1166 | if (show_stats > 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1167 | fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1168 | |
| 1169 | xb.offset = 0; |
| 1170 | xb.nlmsg_count = 0; |
| 1171 | } |
| 1172 | |
| 1173 | } else { |
Nicolas Dichtel | f687d73 | 2014-03-21 11:02:43 +0100 | [diff] [blame] | 1174 | struct xfrm_address_filter addrfilter = { |
| 1175 | .saddr = filter.xsinfo.saddr, |
| 1176 | .daddr = filter.xsinfo.id.daddr, |
| 1177 | .family = filter.xsinfo.family, |
| 1178 | .splen = filter.id_src_mask, |
| 1179 | .dplen = filter.id_dst_mask, |
| 1180 | }; |
| 1181 | struct { |
| 1182 | struct nlmsghdr n; |
| 1183 | char buf[NLMSG_BUF_SIZE]; |
| 1184 | } req = { |
| 1185 | .n.nlmsg_len = NLMSG_HDRLEN, |
| 1186 | .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 1187 | .n.nlmsg_type = XFRM_MSG_GETSA, |
| 1188 | .n.nlmsg_seq = rth.dump = ++rth.seq, |
| 1189 | }; |
| 1190 | |
| 1191 | if (filter.xsinfo.id.proto) |
| 1192 | addattr8(&req.n, sizeof(req), XFRMA_PROTO, |
| 1193 | filter.xsinfo.id.proto); |
| 1194 | addattr_l(&req.n, sizeof(req), XFRMA_ADDRESS_FILTER, |
| 1195 | &addrfilter, sizeof(addrfilter)); |
| 1196 | |
| 1197 | if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1198 | perror("Cannot send dump request"); |
| 1199 | exit(1); |
| 1200 | } |
| 1201 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 1202 | if (rtnl_dump_filter(&rth, xfrm_state_print, stdout) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1203 | fprintf(stderr, "Dump terminated\n"); |
| 1204 | exit(1); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | rtnl_close(&rth); |
| 1209 | |
| 1210 | exit(0); |
| 1211 | } |
| 1212 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 1213 | static int print_sadinfo(struct nlmsghdr *n, void *arg) |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1214 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1215 | FILE *fp = (FILE *)arg; |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1216 | __u32 *f = NLMSG_DATA(n); |
| 1217 | struct rtattr *tb[XFRMA_SAD_MAX+1]; |
| 1218 | struct rtattr *rta; |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1219 | __u32 *cnt; |
| 1220 | |
| 1221 | int len = n->nlmsg_len; |
| 1222 | |
| 1223 | len -= NLMSG_LENGTH(sizeof(__u32)); |
| 1224 | if (len < 0) { |
| 1225 | fprintf(stderr, "SADinfo: Wrong len %d\n", len); |
| 1226 | return -1; |
| 1227 | } |
| 1228 | |
| 1229 | rta = XFRMSAPD_RTA(f); |
| 1230 | parse_rtattr(tb, XFRMA_SAD_MAX, rta, len); |
| 1231 | |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 1232 | if (tb[XFRMA_SAD_CNT]) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1233 | fprintf(fp, "\t SAD"); |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 1234 | cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1235 | fprintf(fp, " count %d", *cnt); |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1236 | } else { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1237 | fprintf(fp, "BAD SAD info returned\n"); |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1238 | return -1; |
| 1239 | } |
| 1240 | |
| 1241 | if (show_stats) { |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 1242 | if (tb[XFRMA_SAD_HINFO]) { |
| 1243 | struct xfrmu_sadhinfo *si; |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1244 | |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 1245 | if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1246 | fprintf(fp, "BAD SAD length returned\n"); |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 1247 | return -1; |
| 1248 | } |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 1249 | |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 1250 | si = RTA_DATA(tb[XFRMA_SAD_HINFO]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1251 | fprintf(fp, " (buckets "); |
| 1252 | fprintf(fp, "count %d", si->sadhcnt); |
| 1253 | fprintf(fp, " Max %d", si->sadhmcnt); |
| 1254 | fprintf(fp, ")"); |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 1255 | } |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1256 | } |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1257 | fprintf(fp, "\n"); |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1258 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1259 | return 0; |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static int xfrm_sad_getinfo(int argc, char **argv) |
| 1263 | { |
| 1264 | struct rtnl_handle rth; |
| 1265 | struct { |
| 1266 | struct nlmsghdr n; |
| 1267 | __u32 flags; |
| 1268 | char ans[64]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 1269 | } req = { |
| 1270 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags)), |
| 1271 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 1272 | .n.nlmsg_type = XFRM_MSG_GETSADINFO, |
| 1273 | .flags = 0XFFFFFFFF, |
| 1274 | }; |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1275 | |
| 1276 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 1277 | exit(1); |
| 1278 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 1279 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1280 | exit(2); |
| 1281 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1282 | print_sadinfo(&req.n, (void *)stdout); |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1283 | |
| 1284 | rtnl_close(&rth); |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1289 | static int xfrm_state_flush(int argc, char **argv) |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1290 | { |
| 1291 | struct rtnl_handle rth; |
| 1292 | struct { |
| 1293 | struct nlmsghdr n; |
| 1294 | struct xfrm_usersa_flush xsf; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 1295 | } req = { |
| 1296 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf)), |
| 1297 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 1298 | .n.nlmsg_type = XFRM_MSG_FLUSHSA, |
| 1299 | }; |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1300 | char *protop = NULL; |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1301 | |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1302 | while (argc > 0) { |
| 1303 | if (strcmp(*argv, "proto") == 0) { |
| 1304 | int ret; |
| 1305 | |
| 1306 | if (protop) |
| 1307 | duparg("proto", *argv); |
| 1308 | protop = *argv; |
| 1309 | |
| 1310 | NEXT_ARG(); |
| 1311 | |
| 1312 | ret = xfrm_xfrmproto_getbyname(*argv); |
| 1313 | if (ret < 0) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 1314 | invarg("XFRM-PROTO value is invalid", *argv); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1315 | |
| 1316 | req.xsf.proto = (__u8)ret; |
| 1317 | } else |
| 1318 | invarg("unknown", *argv); |
| 1319 | |
| 1320 | argc--; argv++; |
| 1321 | } |
| 1322 | |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1323 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 1324 | exit(1); |
| 1325 | |
| 1326 | if (show_stats > 1) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 1327 | fprintf(stderr, "Flush state with XFRM-PROTO value \"%s\"\n", |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1328 | strxf_xfrmproto(req.xsf.proto)); |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1329 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 1330 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1331 | exit(2); |
| 1332 | |
| 1333 | rtnl_close(&rth); |
| 1334 | |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1338 | int do_xfrm_state(int argc, char **argv) |
| 1339 | { |
| 1340 | if (argc < 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1341 | return xfrm_state_list_or_deleteall(0, NULL, 0); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1342 | |
| 1343 | if (matches(*argv, "add") == 0) |
| 1344 | return xfrm_state_modify(XFRM_MSG_NEWSA, 0, |
| 1345 | argc-1, argv+1); |
| 1346 | if (matches(*argv, "update") == 0) |
| 1347 | return xfrm_state_modify(XFRM_MSG_UPDSA, 0, |
| 1348 | argc-1, argv+1); |
linux-ipv6.org!nakam | fb7399b | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 1349 | if (matches(*argv, "allocspi") == 0) |
| 1350 | return xfrm_state_allocspi(argc-1, argv+1); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1351 | if (matches(*argv, "delete") == 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1352 | return xfrm_state_get_or_delete(argc-1, argv+1, 1); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1353 | if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0) |
| 1354 | return xfrm_state_list_or_deleteall(argc-1, argv+1, 1); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1355 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 1356 | || matches(*argv, "lst") == 0) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1357 | return xfrm_state_list_or_deleteall(argc-1, argv+1, 0); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1358 | if (matches(*argv, "get") == 0) |
| 1359 | return xfrm_state_get_or_delete(argc-1, argv+1, 0); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1360 | if (matches(*argv, "flush") == 0) |
| 1361 | return xfrm_state_flush(argc-1, argv+1); |
jamal | 0bb4a4c | 2007-05-03 19:09:41 -0400 | [diff] [blame] | 1362 | if (matches(*argv, "count") == 0) { |
| 1363 | return xfrm_sad_getinfo(argc, argv); |
| 1364 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1365 | if (matches(*argv, "help") == 0) |
| 1366 | usage(); |
| 1367 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv); |
| 1368 | exit(-1); |
| 1369 | } |