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> |
| 31 | #include <linux/netlink.h> |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 32 | #include "utils.h" |
| 33 | #include "xfrm.h" |
| 34 | #include "ip_common.h" |
| 35 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 36 | /* #define NLMSG_DELETEALL_BUF_SIZE (4096-512) */ |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 37 | #define NLMSG_DELETEALL_BUF_SIZE 8192 |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Receiving buffer defines: |
| 41 | * nlmsg |
| 42 | * data = struct xfrm_userpolicy_info |
| 43 | * rtattr |
| 44 | * data = struct xfrm_user_tmpl[] |
| 45 | */ |
| 46 | #define NLMSG_BUF_SIZE 4096 |
| 47 | #define RTA_BUF_SIZE 2048 |
| 48 | #define XFRM_TMPLS_BUF_SIZE 1024 |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 49 | #define CTX_BUF_SIZE 256 |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 50 | |
| 51 | static void usage(void) __attribute__((noreturn)); |
| 52 | |
| 53 | static void usage(void) |
| 54 | { |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 55 | fprintf(stderr, "Usage: ip xfrm policy { add | update } SELECTOR dir DIR [ ctx CTX ]\n"); |
| 56 | fprintf(stderr, " [ mark MARK [ mask MASK ] ] [ index INDEX ] [ ptype PTYPE ]\n"); |
| 57 | fprintf(stderr, " [ action ACTION ] [ priority PRIORITY ] [ flag FLAG-LIST ]\n"); |
| 58 | fprintf(stderr, " [ LIMIT-LIST ] [ TMPL-LIST ]\n"); |
| 59 | fprintf(stderr, "Usage: ip xfrm policy { delete | get } { SELECTOR | index INDEX } dir DIR\n"); |
| 60 | fprintf(stderr, " [ ctx CTX ] [ mark MARK [ mask MASK ] ] [ ptype PTYPE ]\n"); |
| 61 | fprintf(stderr, "Usage: ip xfrm policy { deleteall | list } [ SELECTOR ] [ dir DIR ]\n"); |
| 62 | fprintf(stderr, " [ index INDEX ] [ ptype PTYPE ] [ action ACTION ] [ priority PRIORITY ]\n"); |
| 63 | fprintf(stderr, " [ flag FLAG-LIST ]\n"); |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 64 | fprintf(stderr, "Usage: ip xfrm policy flush [ ptype PTYPE ]\n"); |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 65 | fprintf(stderr, "Usage: ip xfrm policy count\n"); |
| 66 | fprintf(stderr, "Usage: ip xfrm policy set [ hthresh4 LBITS RBITS ] [ hthresh6 LBITS RBITS ]\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 67 | fprintf(stderr, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n"); |
| 68 | fprintf(stderr, "UPSPEC := proto { { "); |
| 69 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_TCP)); |
| 70 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_UDP)); |
| 71 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_SCTP)); |
| 72 | fprintf(stderr, "%s", strxf_proto(IPPROTO_DCCP)); |
| 73 | fprintf(stderr, " } [ sport PORT ] [ dport PORT ] |\n"); |
| 74 | fprintf(stderr, " { "); |
| 75 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMP)); |
| 76 | fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMPV6)); |
| 77 | fprintf(stderr, "%s", strxf_proto(IPPROTO_MH)); |
| 78 | fprintf(stderr, " } [ type NUMBER ] [ code NUMBER ] |\n"); |
| 79 | fprintf(stderr, " %s", strxf_proto(IPPROTO_GRE)); |
| 80 | fprintf(stderr, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n"); |
| 81 | fprintf(stderr, "DIR := in | out | fwd\n"); |
| 82 | fprintf(stderr, "PTYPE := main | sub\n"); |
| 83 | fprintf(stderr, "ACTION := allow | block\n"); |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 84 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 85 | fprintf(stderr, "FLAG := localok | icmp\n"); |
| 86 | fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n"); |
| 87 | fprintf(stderr, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n"); |
| 88 | fprintf(stderr, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n"); |
| 89 | fprintf(stderr, "TMPL-LIST := [ TMPL-LIST ] tmpl TMPL\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 90 | fprintf(stderr, "TMPL := ID [ mode MODE ] [ reqid REQID ] [ level LEVEL ]\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 91 | fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n"); |
| 92 | fprintf(stderr, "XFRM-PROTO := "); |
org[shemminger]!nakam | 29aa4dd | 2004-09-28 18:40:49 +0000 | [diff] [blame] | 93 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP)); |
| 94 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH)); |
Masahide NAKAMURA | 7ea4f5d | 2006-12-05 19:15:47 +0900 | [diff] [blame] | 95 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP)); |
| 96 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING)); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 97 | fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS)); |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 98 | fprintf(stderr, "MODE := transport | tunnel | beet | ro | in_trigger\n"); |
David Ward | cbec021 | 2011-06-11 16:13:30 +0000 | [diff] [blame] | 99 | fprintf(stderr, "LEVEL := required | use\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 100 | |
| 101 | exit(-1); |
| 102 | } |
| 103 | |
| 104 | static int xfrm_policy_dir_parse(__u8 *dir, int *argcp, char ***argvp) |
| 105 | { |
| 106 | int argc = *argcp; |
| 107 | char **argv = *argvp; |
| 108 | |
| 109 | if (strcmp(*argv, "in") == 0) |
| 110 | *dir = XFRM_POLICY_IN; |
| 111 | else if (strcmp(*argv, "out") == 0) |
| 112 | *dir = XFRM_POLICY_OUT; |
| 113 | else if (strcmp(*argv, "fwd") == 0) |
| 114 | *dir = XFRM_POLICY_FWD; |
| 115 | else |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 116 | invarg("DIR value is invalid", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 117 | |
| 118 | *argcp = argc; |
| 119 | *argvp = argv; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 124 | static int xfrm_policy_ptype_parse(__u8 *ptype, int *argcp, char ***argvp) |
| 125 | { |
| 126 | int argc = *argcp; |
| 127 | char **argv = *argvp; |
| 128 | |
| 129 | if (strcmp(*argv, "main") == 0) |
| 130 | *ptype = XFRM_POLICY_TYPE_MAIN; |
| 131 | else if (strcmp(*argv, "sub") == 0) |
| 132 | *ptype = XFRM_POLICY_TYPE_SUB; |
| 133 | else |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 134 | invarg("PTYPE value is invalid", *argv); |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 135 | |
| 136 | *argcp = argc; |
| 137 | *argvp = argv; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 142 | static int xfrm_policy_flag_parse(__u8 *flags, int *argcp, char ***argvp) |
| 143 | { |
| 144 | int argc = *argcp; |
| 145 | char **argv = *argvp; |
| 146 | int len = strlen(*argv); |
| 147 | |
| 148 | if (len > 2 && strncmp(*argv, "0x", 2) == 0) { |
| 149 | __u8 val = 0; |
| 150 | |
| 151 | if (get_u8(&val, *argv, 16)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 152 | invarg("FLAG value is invalid", *argv); |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 153 | *flags = val; |
| 154 | } else { |
| 155 | while (1) { |
| 156 | if (strcmp(*argv, "localok") == 0) |
| 157 | *flags |= XFRM_POLICY_LOCALOK; |
Ulrich Weber | c063564 | 2011-04-07 09:37:05 +0200 | [diff] [blame] | 158 | else if (strcmp(*argv, "icmp") == 0) |
| 159 | *flags |= XFRM_POLICY_ICMP; |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 160 | else { |
| 161 | PREV_ARG(); /* back track */ |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | if (!NEXT_ARG_OK()) |
| 166 | break; |
| 167 | NEXT_ARG(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | *argcp = argc; |
| 172 | *argvp = argv; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 177 | static int xfrm_tmpl_parse(struct xfrm_user_tmpl *tmpl, |
| 178 | int *argcp, char ***argvp) |
| 179 | { |
| 180 | int argc = *argcp; |
| 181 | char **argv = *argvp; |
| 182 | char *idp = NULL; |
| 183 | |
| 184 | while (1) { |
| 185 | if (strcmp(*argv, "mode") == 0) { |
| 186 | NEXT_ARG(); |
| 187 | xfrm_mode_parse(&tmpl->mode, &argc, &argv); |
| 188 | } else if (strcmp(*argv, "reqid") == 0) { |
| 189 | NEXT_ARG(); |
| 190 | xfrm_reqid_parse(&tmpl->reqid, &argc, &argv); |
| 191 | } else if (strcmp(*argv, "level") == 0) { |
| 192 | NEXT_ARG(); |
| 193 | |
| 194 | if (strcmp(*argv, "required") == 0) |
| 195 | tmpl->optional = 0; |
| 196 | else if (strcmp(*argv, "use") == 0) |
| 197 | tmpl->optional = 1; |
| 198 | else |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 199 | invarg("LEVEL value is invalid\n", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 200 | |
| 201 | } else { |
| 202 | if (idp) { |
| 203 | PREV_ARG(); /* back track */ |
| 204 | break; |
| 205 | } |
| 206 | idp = *argv; |
Alex Badea | e6e0b60 | 2010-01-11 17:24:04 +0200 | [diff] [blame] | 207 | preferred_family = AF_UNSPEC; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 208 | xfrm_id_parse(&tmpl->saddr, &tmpl->id, &tmpl->family, |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 209 | 0, &argc, &argv); |
Alex Badea | e6e0b60 | 2010-01-11 17:24:04 +0200 | [diff] [blame] | 210 | preferred_family = tmpl->family; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (!NEXT_ARG_OK()) |
| 214 | break; |
| 215 | |
| 216 | NEXT_ARG(); |
| 217 | } |
| 218 | if (argc == *argcp) |
| 219 | missarg("TMPL"); |
| 220 | |
| 221 | *argcp = argc; |
| 222 | *argvp = argv; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 227 | int xfrm_sctx_parse(char *ctxstr, char *s, |
| 228 | struct xfrm_user_sec_ctx *sctx) |
| 229 | { |
| 230 | int slen; |
| 231 | |
| 232 | slen = strlen(s) + 1; |
| 233 | |
| 234 | sctx->exttype = XFRMA_SEC_CTX; |
| 235 | sctx->ctx_doi = 1; |
| 236 | sctx->ctx_alg = 1; |
| 237 | sctx->ctx_len = slen; |
| 238 | sctx->len = sizeof(struct xfrm_user_sec_ctx) + slen; |
| 239 | memcpy(ctxstr, s, slen); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 244 | static int xfrm_policy_modify(int cmd, unsigned int flags, int argc, char **argv) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 245 | { |
| 246 | struct rtnl_handle rth; |
| 247 | struct { |
| 248 | struct nlmsghdr n; |
| 249 | struct xfrm_userpolicy_info xpinfo; |
| 250 | char buf[RTA_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 251 | } req = { |
| 252 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpinfo)), |
| 253 | .n.nlmsg_flags = NLM_F_REQUEST | flags, |
| 254 | .n.nlmsg_type = cmd, |
| 255 | .xpinfo.sel.family = preferred_family, |
| 256 | .xpinfo.lft.soft_byte_limit = XFRM_INF, |
| 257 | .xpinfo.lft.hard_byte_limit = XFRM_INF, |
| 258 | .xpinfo.lft.soft_packet_limit = XFRM_INF, |
| 259 | .xpinfo.lft.hard_packet_limit = XFRM_INF, |
| 260 | }; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 261 | char *dirp = NULL; |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 262 | char *selp = NULL; |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 263 | char *ptypep = NULL; |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 264 | char *sctxp = NULL; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 265 | struct xfrm_userpolicy_type upt = {}; |
| 266 | char tmpls_buf[XFRM_TMPLS_BUF_SIZE] = {}; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 267 | int tmpls_len = 0; |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 268 | struct xfrm_mark mark = {0, 0}; |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 269 | struct { |
| 270 | struct xfrm_user_sec_ctx sctx; |
| 271 | char str[CTX_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 272 | } ctx = {}; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 273 | |
| 274 | while (argc > 0) { |
| 275 | if (strcmp(*argv, "dir") == 0) { |
| 276 | if (dirp) |
| 277 | duparg("dir", *argv); |
| 278 | dirp = *argv; |
| 279 | |
| 280 | NEXT_ARG(); |
| 281 | xfrm_policy_dir_parse(&req.xpinfo.dir, &argc, &argv); |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 282 | } else if (strcmp(*argv, "ctx") == 0) { |
| 283 | char *context; |
| 284 | |
| 285 | if (sctxp) |
| 286 | duparg("ctx", *argv); |
| 287 | sctxp = *argv; |
| 288 | NEXT_ARG(); |
| 289 | context = *argv; |
| 290 | xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx); |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 291 | } else if (strcmp(*argv, "mark") == 0) { |
| 292 | xfrm_parse_mark(&mark, &argc, &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 293 | } else if (strcmp(*argv, "index") == 0) { |
| 294 | NEXT_ARG(); |
| 295 | if (get_u32(&req.xpinfo.index, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 296 | invarg("INDEX value is invalid", *argv); |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 297 | } else if (strcmp(*argv, "ptype") == 0) { |
| 298 | if (ptypep) |
| 299 | duparg("ptype", *argv); |
| 300 | ptypep = *argv; |
| 301 | |
| 302 | NEXT_ARG(); |
| 303 | xfrm_policy_ptype_parse(&upt.type, &argc, &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 304 | } else if (strcmp(*argv, "action") == 0) { |
| 305 | NEXT_ARG(); |
| 306 | if (strcmp(*argv, "allow") == 0) |
| 307 | req.xpinfo.action = XFRM_POLICY_ALLOW; |
| 308 | else if (strcmp(*argv, "block") == 0) |
| 309 | req.xpinfo.action = XFRM_POLICY_BLOCK; |
| 310 | else |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 311 | invarg("ACTION value is invalid\n", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 312 | } else if (strcmp(*argv, "priority") == 0) { |
| 313 | NEXT_ARG(); |
| 314 | if (get_u32(&req.xpinfo.priority, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 315 | invarg("PRIORITY value is invalid", *argv); |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 316 | } else if (strcmp(*argv, "flag") == 0) { |
| 317 | NEXT_ARG(); |
| 318 | xfrm_policy_flag_parse(&req.xpinfo.flags, &argc, |
| 319 | &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 320 | } else if (strcmp(*argv, "limit") == 0) { |
| 321 | NEXT_ARG(); |
| 322 | xfrm_lifetime_cfg_parse(&req.xpinfo.lft, &argc, &argv); |
| 323 | } else if (strcmp(*argv, "tmpl") == 0) { |
| 324 | struct xfrm_user_tmpl *tmpl; |
| 325 | |
| 326 | if (tmpls_len + sizeof(*tmpl) > sizeof(tmpls_buf)) { |
| 327 | fprintf(stderr, "Too many tmpls: buffer overflow\n"); |
| 328 | exit(1); |
| 329 | } |
| 330 | tmpl = (struct xfrm_user_tmpl *)((char *)tmpls_buf + tmpls_len); |
| 331 | |
| 332 | tmpl->family = preferred_family; |
| 333 | tmpl->aalgos = (~(__u32)0); |
| 334 | tmpl->ealgos = (~(__u32)0); |
| 335 | tmpl->calgos = (~(__u32)0); |
| 336 | |
| 337 | NEXT_ARG(); |
| 338 | xfrm_tmpl_parse(tmpl, &argc, &argv); |
| 339 | |
| 340 | tmpls_len += sizeof(*tmpl); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 341 | } else { |
| 342 | if (selp) |
| 343 | duparg("unknown", *argv); |
| 344 | selp = *argv; |
| 345 | |
| 346 | xfrm_selector_parse(&req.xpinfo.sel, &argc, &argv); |
| 347 | if (preferred_family == AF_UNSPEC) |
| 348 | preferred_family = req.xpinfo.sel.family; |
| 349 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 350 | |
| 351 | argc--; argv++; |
| 352 | } |
| 353 | |
| 354 | if (!dirp) { |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 355 | fprintf(stderr, "Not enough information: DIR is required.\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 356 | exit(1); |
| 357 | } |
| 358 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 359 | if (ptypep) { |
| 360 | addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, |
| 361 | (void *)&upt, sizeof(upt)); |
| 362 | } |
| 363 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 364 | if (tmpls_len > 0) { |
| 365 | addattr_l(&req.n, sizeof(req), XFRMA_TMPL, |
| 366 | (void *)tmpls_buf, tmpls_len); |
| 367 | } |
| 368 | |
Christophe Gouault | b557416 | 2013-10-08 05:56:54 -0700 | [diff] [blame] | 369 | if (mark.m) { |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 370 | int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, |
| 371 | (void *)&mark, sizeof(mark)); |
| 372 | if (r < 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 373 | fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__); |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 374 | exit(1); |
| 375 | } |
| 376 | } |
| 377 | |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 378 | if (sctxp) { |
| 379 | addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX, |
| 380 | (void *)&ctx, ctx.sctx.len); |
| 381 | } |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 382 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 383 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 384 | exit(1); |
| 385 | |
| 386 | if (req.xpinfo.sel.family == AF_UNSPEC) |
| 387 | req.xpinfo.sel.family = AF_INET; |
| 388 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 389 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 390 | exit(2); |
| 391 | |
| 392 | rtnl_close(&rth); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 397 | static int xfrm_policy_filter_match(struct xfrm_userpolicy_info *xpinfo, |
| 398 | __u8 ptype) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 399 | { |
| 400 | if (!filter.use) |
| 401 | return 1; |
| 402 | |
| 403 | if ((xpinfo->dir^filter.xpinfo.dir)&filter.dir_mask) |
| 404 | return 0; |
| 405 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 406 | if ((ptype^filter.ptype)&filter.ptype_mask) |
| 407 | return 0; |
| 408 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 409 | if (filter.sel_src_mask) { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 410 | if (xfrm_addr_match(&xpinfo->sel.saddr, &filter.xpinfo.sel.saddr, |
| 411 | filter.sel_src_mask)) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | if (filter.sel_dst_mask) { |
net[shemminger]!shemminger | eaa34ee | 2005-01-17 23:29:39 +0000 | [diff] [blame] | 416 | if (xfrm_addr_match(&xpinfo->sel.daddr, &filter.xpinfo.sel.daddr, |
| 417 | filter.sel_dst_mask)) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | if ((xpinfo->sel.ifindex^filter.xpinfo.sel.ifindex)&filter.sel_dev_mask) |
| 422 | return 0; |
| 423 | |
| 424 | if ((xpinfo->sel.proto^filter.xpinfo.sel.proto)&filter.upspec_proto_mask) |
| 425 | return 0; |
| 426 | |
| 427 | if (filter.upspec_sport_mask) { |
| 428 | if ((xpinfo->sel.sport^filter.xpinfo.sel.sport)&filter.upspec_sport_mask) |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | if (filter.upspec_dport_mask) { |
| 433 | if ((xpinfo->sel.dport^filter.xpinfo.sel.dport)&filter.upspec_dport_mask) |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | if ((xpinfo->index^filter.xpinfo.index)&filter.index_mask) |
| 438 | return 0; |
| 439 | |
| 440 | if ((xpinfo->action^filter.xpinfo.action)&filter.action_mask) |
| 441 | return 0; |
| 442 | |
| 443 | if ((xpinfo->priority^filter.xpinfo.priority)&filter.priority_mask) |
| 444 | return 0; |
| 445 | |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 446 | if (filter.policy_flags_mask) |
| 447 | if ((xpinfo->flags & filter.xpinfo.flags) == 0) |
| 448 | return 0; |
| 449 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 450 | return 1; |
| 451 | } |
| 452 | |
linux-ipv6.org!nakam | 56e8ad3 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 453 | int xfrm_policy_print(const struct sockaddr_nl *who, struct nlmsghdr *n, |
| 454 | void *arg) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 455 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 456 | struct rtattr *tb[XFRMA_MAX+1]; |
| 457 | struct rtattr *rta; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 458 | struct xfrm_userpolicy_info *xpinfo = NULL; |
| 459 | struct xfrm_user_polexpire *xpexp = NULL; |
| 460 | struct xfrm_userpolicy_id *xpid = NULL; |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 461 | __u8 ptype = XFRM_POLICY_TYPE_MAIN; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 462 | FILE *fp = (FILE *)arg; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 463 | int len = n->nlmsg_len; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 464 | |
| 465 | if (n->nlmsg_type != XFRM_MSG_NEWPOLICY && |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 466 | n->nlmsg_type != XFRM_MSG_DELPOLICY && |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 467 | n->nlmsg_type != XFRM_MSG_UPDPOLICY && |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 468 | n->nlmsg_type != XFRM_MSG_POLEXPIRE) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 469 | fprintf(stderr, "Not a policy: %08x %08x %08x\n", |
| 470 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 471 | return 0; |
| 472 | } |
| 473 | |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 474 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 475 | xpid = NLMSG_DATA(n); |
Andy Gay | af1b6a4 | 2006-08-10 20:25:40 -0400 | [diff] [blame] | 476 | len -= NLMSG_SPACE(sizeof(*xpid)); |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 477 | } else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) { |
| 478 | xpexp = NLMSG_DATA(n); |
| 479 | xpinfo = &xpexp->pol; |
Andy Gay | af1b6a4 | 2006-08-10 20:25:40 -0400 | [diff] [blame] | 480 | len -= NLMSG_SPACE(sizeof(*xpexp)); |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 481 | } else { |
| 482 | xpexp = NULL; |
| 483 | xpinfo = NLMSG_DATA(n); |
Andy Gay | af1b6a4 | 2006-08-10 20:25:40 -0400 | [diff] [blame] | 484 | len -= NLMSG_SPACE(sizeof(*xpinfo)); |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 485 | } |
| 486 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 487 | if (len < 0) { |
| 488 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 489 | return -1; |
| 490 | } |
| 491 | |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 492 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) |
| 493 | rta = XFRMPID_RTA(xpid); |
| 494 | else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 495 | rta = XFRMPEXP_RTA(xpexp); |
| 496 | else |
| 497 | rta = XFRMP_RTA(xpinfo); |
| 498 | |
| 499 | parse_rtattr(tb, XFRMA_MAX, rta, len); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 500 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 501 | if (tb[XFRMA_POLICY_TYPE]) { |
| 502 | struct xfrm_userpolicy_type *upt; |
| 503 | |
| 504 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) { |
| 505 | fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n"); |
| 506 | return -1; |
| 507 | } |
| 508 | upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); |
| 509 | ptype = upt->type; |
| 510 | } |
| 511 | |
| 512 | if (xpinfo && !xfrm_policy_filter_match(xpinfo, ptype)) |
| 513 | return 0; |
| 514 | |
| 515 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) |
| 516 | fprintf(fp, "Deleted "); |
| 517 | else if (n->nlmsg_type == XFRM_MSG_UPDPOLICY) |
| 518 | fprintf(fp, "Updated "); |
| 519 | else if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) |
| 520 | fprintf(fp, "Expired "); |
| 521 | |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 522 | if (n->nlmsg_type == XFRM_MSG_DELPOLICY) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 523 | /* xfrm_policy_id_print(); */ |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 524 | if (!tb[XFRMA_POLICY]) { |
| 525 | fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: no XFRMA_POLICY\n"); |
| 526 | return -1; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 527 | } |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 528 | if (RTA_PAYLOAD(tb[XFRMA_POLICY]) < sizeof(*xpinfo)) { |
| 529 | fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n"); |
| 530 | return -1; |
| 531 | } |
| 532 | xpinfo = (struct xfrm_userpolicy_info *)RTA_DATA(tb[XFRMA_POLICY]); |
| 533 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 534 | |
linux-ipv6.org!nakam | 56e8ad3 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 535 | xfrm_policy_info_print(xpinfo, tb, fp, NULL, NULL); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 536 | |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 537 | if (n->nlmsg_type == XFRM_MSG_POLEXPIRE) { |
| 538 | fprintf(fp, "\t"); |
| 539 | fprintf(fp, "hard %u", xpexp->hard); |
| 540 | fprintf(fp, "%s", _SL_); |
| 541 | } |
| 542 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 543 | if (oneline) |
| 544 | fprintf(fp, "\n"); |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 545 | fflush(fp); |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 546 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int xfrm_policy_get_or_delete(int argc, char **argv, int delete, |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 551 | void *res_nlbuf, size_t res_size) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 552 | { |
| 553 | struct rtnl_handle rth; |
| 554 | struct { |
| 555 | struct nlmsghdr n; |
| 556 | struct xfrm_userpolicy_id xpid; |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 557 | char buf[RTA_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 558 | } req = { |
| 559 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xpid)), |
| 560 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 561 | .n.nlmsg_type = delete ? XFRM_MSG_DELPOLICY |
| 562 | : XFRM_MSG_GETPOLICY, |
| 563 | }; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 564 | char *dirp = NULL; |
| 565 | char *selp = NULL; |
| 566 | char *indexp = NULL; |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 567 | char *ptypep = NULL; |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 568 | char *sctxp = NULL; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 569 | struct xfrm_userpolicy_type upt = {}; |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 570 | struct xfrm_mark mark = {0, 0}; |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 571 | struct { |
| 572 | struct xfrm_user_sec_ctx sctx; |
| 573 | char str[CTX_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 574 | } ctx = {}; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 575 | |
| 576 | while (argc > 0) { |
| 577 | if (strcmp(*argv, "dir") == 0) { |
| 578 | if (dirp) |
| 579 | duparg("dir", *argv); |
| 580 | dirp = *argv; |
| 581 | |
| 582 | NEXT_ARG(); |
| 583 | xfrm_policy_dir_parse(&req.xpid.dir, &argc, &argv); |
| 584 | |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 585 | } else if (strcmp(*argv, "ctx") == 0) { |
| 586 | char *context; |
| 587 | |
| 588 | if (sctxp) |
| 589 | duparg("ctx", *argv); |
| 590 | sctxp = *argv; |
| 591 | NEXT_ARG(); |
| 592 | context = *argv; |
| 593 | xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx); |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 594 | } else if (strcmp(*argv, "mark") == 0) { |
| 595 | xfrm_parse_mark(&mark, &argc, &argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 596 | } else if (strcmp(*argv, "index") == 0) { |
| 597 | if (indexp) |
| 598 | duparg("index", *argv); |
| 599 | indexp = *argv; |
| 600 | |
| 601 | NEXT_ARG(); |
| 602 | if (get_u32(&req.xpid.index, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 603 | invarg("INDEX value is invalid", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 604 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 605 | } else if (strcmp(*argv, "ptype") == 0) { |
| 606 | if (ptypep) |
| 607 | duparg("ptype", *argv); |
| 608 | ptypep = *argv; |
| 609 | |
| 610 | NEXT_ARG(); |
| 611 | xfrm_policy_ptype_parse(&upt.type, &argc, &argv); |
| 612 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 613 | } else { |
| 614 | if (selp) |
| 615 | invarg("unknown", *argv); |
| 616 | selp = *argv; |
| 617 | |
| 618 | xfrm_selector_parse(&req.xpid.sel, &argc, &argv); |
| 619 | if (preferred_family == AF_UNSPEC) |
| 620 | preferred_family = req.xpid.sel.family; |
| 621 | |
| 622 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 623 | |
| 624 | argc--; argv++; |
| 625 | } |
| 626 | |
| 627 | if (!dirp) { |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 628 | fprintf(stderr, "Not enough information: DIR is required.\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 629 | exit(1); |
| 630 | } |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 631 | if (ptypep) { |
| 632 | addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, |
| 633 | (void *)&upt, sizeof(upt)); |
| 634 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 635 | if (!selp && !indexp) { |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 636 | fprintf(stderr, "Not enough information: either SELECTOR or INDEX is required.\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 637 | exit(1); |
| 638 | } |
| 639 | if (selp && indexp) |
| 640 | duparg2("SELECTOR", "INDEX"); |
| 641 | |
| 642 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 643 | exit(1); |
| 644 | |
| 645 | if (req.xpid.sel.family == AF_UNSPEC) |
| 646 | req.xpid.sel.family = AF_INET; |
| 647 | |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 648 | if (mark.m & mark.v) { |
| 649 | int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK, |
| 650 | (void *)&mark, sizeof(mark)); |
| 651 | if (r < 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 652 | fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__); |
Jamal Hadi Salim | ee675e8 | 2010-02-23 03:15:11 +0000 | [diff] [blame] | 653 | exit(1); |
| 654 | } |
| 655 | } |
| 656 | |
Joy Latten | e4f054f | 2011-02-02 17:32:18 -0600 | [diff] [blame] | 657 | if (sctxp) { |
| 658 | addattr_l(&req.n, sizeof(req), XFRMA_SEC_CTX, |
| 659 | (void *)&ctx, ctx.sctx.len); |
| 660 | } |
| 661 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 662 | if (rtnl_talk(&rth, &req.n, res_nlbuf, res_size) < 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 663 | exit(2); |
| 664 | |
| 665 | rtnl_close(&rth); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int xfrm_policy_delete(int argc, char **argv) |
| 671 | { |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 672 | return xfrm_policy_get_or_delete(argc, argv, 1, NULL, 0); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static int xfrm_policy_get(int argc, char **argv) |
| 676 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 677 | char buf[NLMSG_BUF_SIZE] = {}; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 678 | struct nlmsghdr *n = (struct nlmsghdr *)buf; |
| 679 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 680 | xfrm_policy_get_or_delete(argc, argv, 0, n, sizeof(buf)); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 681 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 682 | if (xfrm_policy_print(NULL, n, (void *)stdout) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 683 | fprintf(stderr, "An error :-)\n"); |
| 684 | exit(1); |
| 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * With an existing policy of nlmsg, make new nlmsg for deleting the policy |
| 692 | * and store it to buffer. |
| 693 | */ |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 694 | static int xfrm_policy_keep(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 695 | struct nlmsghdr *n, |
osdl.net!shemminger | 6dc9f01 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 696 | void *arg) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 697 | { |
| 698 | struct xfrm_buffer *xb = (struct xfrm_buffer *)arg; |
| 699 | struct rtnl_handle *rth = xb->rth; |
| 700 | struct xfrm_userpolicy_info *xpinfo = NLMSG_DATA(n); |
| 701 | int len = n->nlmsg_len; |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 702 | struct rtattr *tb[XFRMA_MAX+1]; |
| 703 | __u8 ptype = XFRM_POLICY_TYPE_MAIN; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 704 | struct nlmsghdr *new_n; |
| 705 | struct xfrm_userpolicy_id *xpid; |
| 706 | |
| 707 | if (n->nlmsg_type != XFRM_MSG_NEWPOLICY) { |
| 708 | fprintf(stderr, "Not a policy: %08x %08x %08x\n", |
| 709 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | len -= NLMSG_LENGTH(sizeof(*xpinfo)); |
| 714 | if (len < 0) { |
| 715 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 716 | return -1; |
| 717 | } |
| 718 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 719 | parse_rtattr(tb, XFRMA_MAX, XFRMP_RTA(xpinfo), len); |
| 720 | |
| 721 | if (tb[XFRMA_POLICY_TYPE]) { |
| 722 | struct xfrm_userpolicy_type *upt; |
| 723 | |
| 724 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) { |
| 725 | fprintf(stderr, "too short XFRMA_POLICY_TYPE len\n"); |
| 726 | return -1; |
| 727 | } |
| 728 | upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); |
| 729 | ptype = upt->type; |
| 730 | } |
| 731 | |
| 732 | if (!xfrm_policy_filter_match(xpinfo, ptype)) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 733 | return 0; |
| 734 | |
| 735 | if (xb->offset > xb->size) { |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 736 | fprintf(stderr, "Policy buffer overflow\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 737 | return -1; |
| 738 | } |
| 739 | |
| 740 | new_n = (struct nlmsghdr *)(xb->buf + xb->offset); |
| 741 | new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xpid)); |
| 742 | new_n->nlmsg_flags = NLM_F_REQUEST; |
| 743 | new_n->nlmsg_type = XFRM_MSG_DELPOLICY; |
| 744 | new_n->nlmsg_seq = ++rth->seq; |
| 745 | |
| 746 | xpid = NLMSG_DATA(new_n); |
| 747 | memcpy(&xpid->sel, &xpinfo->sel, sizeof(xpid->sel)); |
| 748 | xpid->dir = xpinfo->dir; |
| 749 | xpid->index = xpinfo->index; |
| 750 | |
| 751 | xb->offset += new_n->nlmsg_len; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 752 | xb->nlmsg_count++; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 757 | static int xfrm_policy_list_or_deleteall(int argc, char **argv, int deleteall) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 758 | { |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 759 | char *selp = NULL; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 760 | struct rtnl_handle rth; |
| 761 | |
| 762 | if (argc > 0) |
| 763 | filter.use = 1; |
| 764 | filter.xpinfo.sel.family = preferred_family; |
| 765 | |
| 766 | while (argc > 0) { |
| 767 | if (strcmp(*argv, "dir") == 0) { |
| 768 | NEXT_ARG(); |
| 769 | xfrm_policy_dir_parse(&filter.xpinfo.dir, &argc, &argv); |
| 770 | |
| 771 | filter.dir_mask = XFRM_FILTER_MASK_FULL; |
| 772 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 773 | } else if (strcmp(*argv, "index") == 0) { |
| 774 | NEXT_ARG(); |
| 775 | if (get_u32(&filter.xpinfo.index, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 776 | invarg("INDEX value is invalid", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 777 | |
| 778 | filter.index_mask = XFRM_FILTER_MASK_FULL; |
| 779 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 780 | } else if (strcmp(*argv, "ptype") == 0) { |
| 781 | NEXT_ARG(); |
| 782 | xfrm_policy_ptype_parse(&filter.ptype, &argc, &argv); |
| 783 | |
| 784 | filter.ptype_mask = XFRM_FILTER_MASK_FULL; |
| 785 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 786 | } else if (strcmp(*argv, "action") == 0) { |
| 787 | NEXT_ARG(); |
| 788 | if (strcmp(*argv, "allow") == 0) |
| 789 | filter.xpinfo.action = XFRM_POLICY_ALLOW; |
| 790 | else if (strcmp(*argv, "block") == 0) |
| 791 | filter.xpinfo.action = XFRM_POLICY_BLOCK; |
| 792 | else |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 793 | invarg("ACTION value is invalid\n", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 794 | |
| 795 | filter.action_mask = XFRM_FILTER_MASK_FULL; |
| 796 | |
| 797 | } else if (strcmp(*argv, "priority") == 0) { |
| 798 | NEXT_ARG(); |
| 799 | if (get_u32(&filter.xpinfo.priority, *argv, 0)) |
David Ward | e8740e4 | 2013-03-25 04:23:19 +0000 | [diff] [blame] | 800 | invarg("PRIORITY value is invalid", *argv); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 801 | |
| 802 | filter.priority_mask = XFRM_FILTER_MASK_FULL; |
| 803 | |
Masahide NAKAMURA | c1fa225 | 2007-08-24 11:05:18 +0900 | [diff] [blame] | 804 | } else if (strcmp(*argv, "flag") == 0) { |
| 805 | NEXT_ARG(); |
| 806 | xfrm_policy_flag_parse(&filter.xpinfo.flags, &argc, |
| 807 | &argv); |
| 808 | |
| 809 | filter.policy_flags_mask = XFRM_FILTER_MASK_FULL; |
| 810 | |
net[shemminger]!shemminger | 7809c61 | 2004-08-11 23:41:38 +0000 | [diff] [blame] | 811 | } else { |
| 812 | if (selp) |
| 813 | invarg("unknown", *argv); |
| 814 | selp = *argv; |
| 815 | |
| 816 | xfrm_selector_parse(&filter.xpinfo.sel, &argc, &argv); |
| 817 | if (preferred_family == AF_UNSPEC) |
| 818 | preferred_family = filter.xpinfo.sel.family; |
| 819 | |
| 820 | } |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 821 | |
| 822 | argc--; argv++; |
| 823 | } |
| 824 | |
| 825 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 826 | exit(1); |
| 827 | |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 828 | if (deleteall) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 829 | struct xfrm_buffer xb; |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 830 | char buf[NLMSG_DELETEALL_BUF_SIZE]; |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 831 | int i; |
| 832 | |
| 833 | xb.buf = buf; |
| 834 | xb.size = sizeof(buf); |
| 835 | xb.rth = &rth; |
| 836 | |
| 837 | for (i = 0; ; i++) { |
Nicolas Dichtel | 782cf01 | 2015-04-15 14:00:53 +0200 | [diff] [blame] | 838 | struct { |
| 839 | struct nlmsghdr n; |
| 840 | char buf[NLMSG_BUF_SIZE]; |
| 841 | } req = { |
| 842 | .n.nlmsg_len = NLMSG_HDRLEN, |
| 843 | .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 844 | .n.nlmsg_type = XFRM_MSG_GETPOLICY, |
| 845 | .n.nlmsg_seq = rth.dump = ++rth.seq, |
| 846 | }; |
| 847 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 848 | xb.offset = 0; |
| 849 | xb.nlmsg_count = 0; |
| 850 | |
| 851 | if (show_stats > 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 852 | fprintf(stderr, "Delete-all round = %d\n", i); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 853 | |
Nicolas Dichtel | 782cf01 | 2015-04-15 14:00:53 +0200 | [diff] [blame] | 854 | if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 855 | perror("Cannot send dump request"); |
| 856 | exit(1); |
| 857 | } |
| 858 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 859 | if (rtnl_dump_filter(&rth, xfrm_policy_keep, &xb) < 0) { |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 860 | fprintf(stderr, "Delete-all terminated\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 861 | exit(1); |
| 862 | } |
| 863 | if (xb.nlmsg_count == 0) { |
| 864 | if (show_stats > 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 865 | fprintf(stderr, "Delete-all completed\n"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 866 | break; |
| 867 | } |
| 868 | |
Stephen Hemminger | f31a37f | 2008-01-31 21:38:58 -0800 | [diff] [blame] | 869 | if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) { |
Stephen Hemminger | 1fb0a99 | 2008-01-26 11:08:31 -0800 | [diff] [blame] | 870 | perror("Failed to send delete-all request"); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 871 | exit(1); |
| 872 | } |
| 873 | if (show_stats > 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 874 | fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 875 | |
| 876 | xb.offset = 0; |
| 877 | xb.nlmsg_count = 0; |
| 878 | } |
| 879 | } else { |
Nicolas Dichtel | 782cf01 | 2015-04-15 14:00:53 +0200 | [diff] [blame] | 880 | struct { |
| 881 | struct nlmsghdr n; |
| 882 | char buf[NLMSG_BUF_SIZE]; |
| 883 | } req = { |
| 884 | .n.nlmsg_len = NLMSG_HDRLEN, |
| 885 | .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST, |
| 886 | .n.nlmsg_type = XFRM_MSG_GETPOLICY, |
| 887 | .n.nlmsg_seq = rth.dump = ++rth.seq, |
| 888 | }; |
| 889 | |
| 890 | if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 891 | perror("Cannot send dump request"); |
| 892 | exit(1); |
| 893 | } |
| 894 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 895 | if (rtnl_dump_filter(&rth, xfrm_policy_print, stdout) < 0) { |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 896 | fprintf(stderr, "Dump terminated\n"); |
| 897 | exit(1); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | rtnl_close(&rth); |
| 902 | |
| 903 | exit(0); |
| 904 | } |
| 905 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 906 | static int print_spdinfo(struct nlmsghdr *n, void *arg) |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 907 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 908 | FILE *fp = (FILE *)arg; |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 909 | __u32 *f = NLMSG_DATA(n); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 910 | struct rtattr *tb[XFRMA_SPD_MAX+1]; |
| 911 | struct rtattr *rta; |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 912 | |
| 913 | int len = n->nlmsg_len; |
| 914 | |
| 915 | len -= NLMSG_LENGTH(sizeof(__u32)); |
| 916 | if (len < 0) { |
| 917 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); |
| 918 | return -1; |
| 919 | } |
| 920 | |
| 921 | rta = XFRMSAPD_RTA(f); |
| 922 | parse_rtattr(tb, XFRMA_SPD_MAX, rta, len); |
| 923 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 924 | fprintf(fp, "\t SPD"); |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 925 | if (tb[XFRMA_SPD_INFO]) { |
| 926 | struct xfrmu_spdinfo *si; |
| 927 | |
| 928 | if (RTA_PAYLOAD(tb[XFRMA_SPD_INFO]) < sizeof(*si)) { |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 929 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); |
| 930 | return -1; |
| 931 | } |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 932 | si = RTA_DATA(tb[XFRMA_SPD_INFO]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 933 | fprintf(fp, " IN %d", si->incnt); |
| 934 | fprintf(fp, " OUT %d", si->outcnt); |
| 935 | fprintf(fp, " FWD %d", si->fwdcnt); |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 936 | |
| 937 | if (show_stats) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 938 | fprintf(fp, " (Sock:"); |
| 939 | fprintf(fp, " IN %d", si->inscnt); |
| 940 | fprintf(fp, " OUT %d", si->outscnt); |
| 941 | fprintf(fp, " FWD %d", si->fwdscnt); |
| 942 | fprintf(fp, ")"); |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 943 | } |
| 944 | |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 945 | fprintf(fp, "%s", _SL_); |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 946 | } |
| 947 | if (show_stats > 1) { |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 948 | struct xfrmu_spdhinfo *sh; |
| 949 | |
| 950 | if (tb[XFRMA_SPD_HINFO]) { |
| 951 | if (RTA_PAYLOAD(tb[XFRMA_SPD_HINFO]) < sizeof(*sh)) { |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 952 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); |
| 953 | return -1; |
| 954 | } |
Stephen Hemminger | bdf9e86 | 2007-06-19 16:24:08 -0700 | [diff] [blame] | 955 | sh = RTA_DATA(tb[XFRMA_SPD_HINFO]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 956 | fprintf(fp, "\t SPD buckets:"); |
| 957 | fprintf(fp, " count %d", sh->spdhcnt); |
| 958 | fprintf(fp, " Max %d", sh->spdhmcnt); |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 959 | fprintf(fp, "%s", _SL_); |
| 960 | } |
| 961 | if (tb[XFRMA_SPD_IPV4_HTHRESH]) { |
| 962 | struct xfrmu_spdhthresh *th; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 963 | |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 964 | if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV4_HTHRESH]) < sizeof(*th)) { |
| 965 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); |
| 966 | return -1; |
| 967 | } |
| 968 | th = RTA_DATA(tb[XFRMA_SPD_IPV4_HTHRESH]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 969 | fprintf(fp, "\t SPD IPv4 thresholds:"); |
| 970 | fprintf(fp, " local %d", th->lbits); |
| 971 | fprintf(fp, " remote %d", th->rbits); |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 972 | fprintf(fp, "%s", _SL_); |
| 973 | |
| 974 | } |
| 975 | if (tb[XFRMA_SPD_IPV6_HTHRESH]) { |
| 976 | struct xfrmu_spdhthresh *th; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 977 | |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 978 | if (RTA_PAYLOAD(tb[XFRMA_SPD_IPV6_HTHRESH]) < sizeof(*th)) { |
| 979 | fprintf(stderr, "SPDinfo: Wrong len %d\n", len); |
| 980 | return -1; |
| 981 | } |
| 982 | th = RTA_DATA(tb[XFRMA_SPD_IPV6_HTHRESH]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 983 | fprintf(fp, "\t SPD IPv6 thresholds:"); |
| 984 | fprintf(fp, " local %d", th->lbits); |
| 985 | fprintf(fp, " remote %d", th->rbits); |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 986 | fprintf(fp, "%s", _SL_); |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 987 | } |
| 988 | } |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 989 | |
| 990 | if (oneline) |
| 991 | fprintf(fp, "\n"); |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 992 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 993 | return 0; |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 994 | } |
| 995 | |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 996 | static int xfrm_spd_setinfo(int argc, char **argv) |
| 997 | { |
| 998 | struct rtnl_handle rth; |
| 999 | struct { |
| 1000 | struct nlmsghdr n; |
| 1001 | __u32 flags; |
| 1002 | char buf[RTA_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 1003 | } req = { |
| 1004 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)), |
| 1005 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 1006 | .n.nlmsg_type = XFRM_MSG_NEWSPDINFO, |
| 1007 | .flags = 0XFFFFFFFF, |
| 1008 | }; |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 1009 | |
| 1010 | char *thr4 = NULL; |
| 1011 | char *thr6 = NULL; |
| 1012 | |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 1013 | while (argc > 0) { |
| 1014 | if (strcmp(*argv, "hthresh4") == 0) { |
| 1015 | struct xfrmu_spdhthresh thr; |
| 1016 | |
| 1017 | if (thr4) |
| 1018 | duparg("hthresh4", *argv); |
| 1019 | thr4 = *argv; |
| 1020 | NEXT_ARG(); |
| 1021 | if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 32) |
| 1022 | invarg("hthresh4 LBITS value is invalid", *argv); |
| 1023 | NEXT_ARG(); |
| 1024 | if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 32) |
| 1025 | invarg("hthresh4 RBITS value is invalid", *argv); |
| 1026 | |
| 1027 | addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV4_HTHRESH, |
| 1028 | (void *)&thr, sizeof(thr)); |
| 1029 | } else if (strcmp(*argv, "hthresh6") == 0) { |
| 1030 | struct xfrmu_spdhthresh thr; |
| 1031 | |
| 1032 | if (thr6) |
| 1033 | duparg("hthresh6", *argv); |
| 1034 | thr6 = *argv; |
| 1035 | NEXT_ARG(); |
| 1036 | if (get_u8(&thr.lbits, *argv, 0) || thr.lbits > 128) |
| 1037 | invarg("hthresh6 LBITS value is invalid", *argv); |
| 1038 | NEXT_ARG(); |
| 1039 | if (get_u8(&thr.rbits, *argv, 0) || thr.rbits > 128) |
| 1040 | invarg("hthresh6 RBITS value is invalid", *argv); |
| 1041 | |
| 1042 | addattr_l(&req.n, sizeof(req), XFRMA_SPD_IPV6_HTHRESH, |
| 1043 | (void *)&thr, sizeof(thr)); |
| 1044 | } else { |
| 1045 | invarg("unknown", *argv); |
| 1046 | } |
| 1047 | |
| 1048 | argc--; argv++; |
| 1049 | } |
| 1050 | |
| 1051 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 1052 | exit(1); |
| 1053 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 1054 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 1055 | exit(2); |
| 1056 | |
| 1057 | rtnl_close(&rth); |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 1062 | static int xfrm_spd_getinfo(int argc, char **argv) |
| 1063 | { |
| 1064 | struct rtnl_handle rth; |
| 1065 | struct { |
| 1066 | struct nlmsghdr n; |
| 1067 | __u32 flags; |
Stephen Hemminger | 4806867 | 2014-02-17 10:56:31 -0800 | [diff] [blame] | 1068 | char ans[128]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 1069 | } req = { |
| 1070 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(__u32)), |
| 1071 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 1072 | .n.nlmsg_type = XFRM_MSG_GETSPDINFO, |
| 1073 | .flags = 0XFFFFFFFF, |
| 1074 | }; |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 1075 | |
| 1076 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 1077 | exit(1); |
| 1078 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 1079 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 1080 | exit(2); |
| 1081 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 1082 | print_spdinfo(&req.n, (void *)stdout); |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 1083 | |
| 1084 | rtnl_close(&rth); |
| 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 1089 | static int xfrm_policy_flush(int argc, char **argv) |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1090 | { |
| 1091 | struct rtnl_handle rth; |
| 1092 | struct { |
| 1093 | struct nlmsghdr n; |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 1094 | char buf[RTA_BUF_SIZE]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 1095 | } req = { |
| 1096 | .n.nlmsg_len = NLMSG_LENGTH(0), /* nlmsg data is nothing */ |
| 1097 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 1098 | .n.nlmsg_type = XFRM_MSG_FLUSHPOLICY, |
| 1099 | }; |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 1100 | char *ptypep = NULL; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 1101 | struct xfrm_userpolicy_type upt = {}; |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1102 | |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 1103 | while (argc > 0) { |
| 1104 | if (strcmp(*argv, "ptype") == 0) { |
| 1105 | if (ptypep) |
| 1106 | duparg("ptype", *argv); |
| 1107 | ptypep = *argv; |
| 1108 | |
| 1109 | NEXT_ARG(); |
| 1110 | xfrm_policy_ptype_parse(&upt.type, &argc, &argv); |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 1111 | } else |
| 1112 | invarg("unknown", *argv); |
| 1113 | |
| 1114 | argc--; argv++; |
| 1115 | } |
| 1116 | |
| 1117 | if (ptypep) { |
| 1118 | addattr_l(&req.n, sizeof(req), XFRMA_POLICY_TYPE, |
| 1119 | (void *)&upt, sizeof(upt)); |
| 1120 | } |
| 1121 | |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1122 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) |
| 1123 | exit(1); |
| 1124 | |
| 1125 | if (show_stats > 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1126 | fprintf(stderr, "Flush policy\n"); |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1127 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 1128 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
org[shemminger]!nakam | bd641cd | 2004-09-28 18:38:35 +0000 | [diff] [blame] | 1129 | exit(2); |
| 1130 | |
| 1131 | rtnl_close(&rth); |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1136 | int do_xfrm_policy(int argc, char **argv) |
| 1137 | { |
| 1138 | if (argc < 1) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1139 | return xfrm_policy_list_or_deleteall(0, NULL, 0); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1140 | |
| 1141 | if (matches(*argv, "add") == 0) |
| 1142 | return xfrm_policy_modify(XFRM_MSG_NEWPOLICY, 0, |
| 1143 | argc-1, argv+1); |
| 1144 | if (matches(*argv, "update") == 0) |
| 1145 | return xfrm_policy_modify(XFRM_MSG_UPDPOLICY, 0, |
| 1146 | argc-1, argv+1); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1147 | if (matches(*argv, "delete") == 0) |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1148 | return xfrm_policy_delete(argc-1, argv+1); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1149 | if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0) |
| 1150 | return xfrm_policy_list_or_deleteall(argc-1, argv+1, 1); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1151 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 1152 | || matches(*argv, "lst") == 0) |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1153 | return xfrm_policy_list_or_deleteall(argc-1, argv+1, 0); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1154 | if (matches(*argv, "get") == 0) |
| 1155 | return xfrm_policy_get(argc-1, argv+1); |
shemminger | 9bec1a4 | 2005-06-07 21:58:25 +0000 | [diff] [blame] | 1156 | if (matches(*argv, "flush") == 0) |
Masahide NAKAMURA | 972938e | 2006-12-05 19:15:38 +0900 | [diff] [blame] | 1157 | return xfrm_policy_flush(argc-1, argv+1); |
jamal | f90c4f4 | 2007-05-03 19:20:29 -0400 | [diff] [blame] | 1158 | if (matches(*argv, "count") == 0) |
| 1159 | return xfrm_spd_getinfo(argc, argv); |
Christophe Gouault | 025fa9d | 2015-04-09 17:39:32 +0200 | [diff] [blame] | 1160 | if (matches(*argv, "set") == 0) |
| 1161 | return xfrm_spd_setinfo(argc-1, argv+1); |
net[shemminger]!shemminger | c769987 | 2004-07-07 17:05:56 +0000 | [diff] [blame] | 1162 | if (matches(*argv, "help") == 0) |
| 1163 | usage(); |
| 1164 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm policy help\".\n", *argv); |
| 1165 | exit(-1); |
| 1166 | } |