| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_egress.c ingress/egress packet mirror/redir actions module |
| 4 |
* |
| 5 |
* Authors: J Hadi Salim ([email protected]) |
| 6 |
* |
| 7 |
* TODO: Add Ingress support |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <fcntl.h> |
| 14 |
#include <sys/socket.h> |
| 15 |
#include <netinet/in.h> |
| 16 |
#include <arpa/inet.h> |
| 17 |
#include <string.h> |
| 18 |
#include "utils.h" |
| 19 |
#include "tc_util.h" |
| 20 |
#include "tc_common.h" |
| 21 |
#include <linux/tc_act/tc_mirred.h> |
| 22 |
|
| 23 |
static void |
| 24 |
explain(void) |
| 25 |
{ |
| 26 |
fprintf(stderr, |
| 27 |
"Usage: mirred <DIRECTION> <ACTION> [index INDEX] <TARGET>\n" |
| 28 |
"where:\n" |
| 29 |
"\tDIRECTION := <ingress | egress>\n" |
| 30 |
"\tACTION := <mirror | redirect>\n" |
| 31 |
"\tINDEX is the specific policy instance id\n" |
| 32 |
"\tTARGET := <BLOCK | DEVICE>\n" |
| 33 |
"\tDEVICE := dev DEVICENAME\n" |
| 34 |
"\tDEVICENAME is the devicename\n" |
| 35 |
"\tBLOCK := blockid BLOCKID\n" |
| 36 |
"\tBLOCKID := 32-bit unsigned block ID\n"); |
| 37 |
} |
| 38 |
|
| 39 |
static void |
| 40 |
usage(void) |
| 41 |
{ |
| 42 |
explain(); |
| 43 |
exit(-1); |
| 44 |
} |
| 45 |
|
| 46 |
static const char *mirred_n2a(int action) |
| 47 |
{ |
| 48 |
switch (action) { |
| 49 |
case TCA_EGRESS_REDIR: |
| 50 |
return "Egress Redirect"; |
| 51 |
case TCA_INGRESS_REDIR: |
| 52 |
return "Ingress Redirect"; |
| 53 |
case TCA_EGRESS_MIRROR: |
| 54 |
return "Egress Mirror"; |
| 55 |
case TCA_INGRESS_MIRROR: |
| 56 |
return "Ingress Mirror"; |
| 57 |
default: |
| 58 |
return "unknown"; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static const char *mirred_direction(int action) |
| 63 |
{ |
| 64 |
switch (action) { |
| 65 |
case TCA_EGRESS_REDIR: |
| 66 |
case TCA_EGRESS_MIRROR: |
| 67 |
return "egress"; |
| 68 |
case TCA_INGRESS_REDIR: |
| 69 |
case TCA_INGRESS_MIRROR: |
| 70 |
return "ingress"; |
| 71 |
default: |
| 72 |
return "unknown"; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
static const char *mirred_action(int action) |
| 77 |
{ |
| 78 |
switch (action) { |
| 79 |
case TCA_EGRESS_REDIR: |
| 80 |
case TCA_INGRESS_REDIR: |
| 81 |
return "redirect"; |
| 82 |
case TCA_EGRESS_MIRROR: |
| 83 |
case TCA_INGRESS_MIRROR: |
| 84 |
return "mirror"; |
| 85 |
default: |
| 86 |
return "unknown"; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
static int |
| 91 |
parse_direction(const struct action_util *a, int *argc_p, char ***argv_p, |
| 92 |
int tca_id, struct nlmsghdr *n) |
| 93 |
{ |
| 94 |
|
| 95 |
int argc = *argc_p; |
| 96 |
char **argv = *argv_p; |
| 97 |
int ok = 0, iok = 0, mirror = 0, redir = 0, ingress = 0, egress = 0; |
| 98 |
struct tc_mirred p = {}; |
| 99 |
struct rtattr *tail; |
| 100 |
char d[IFNAMSIZ] = {}; |
| 101 |
__u32 blockid = 0; |
| 102 |
|
| 103 |
while (argc > 0) { |
| 104 |
|
| 105 |
if (matches(*argv, "action") == 0) { |
| 106 |
NEXT_ARG(); |
| 107 |
break; |
| 108 |
} else if (!egress && matches(*argv, "egress") == 0) { |
| 109 |
egress = 1; |
| 110 |
if (ingress) { |
| 111 |
fprintf(stderr, |
| 112 |
"Can't have both egress and ingress\n"); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
NEXT_ARG(); |
| 116 |
ok++; |
| 117 |
continue; |
| 118 |
} else if (!ingress && matches(*argv, "ingress") == 0) { |
| 119 |
ingress = 1; |
| 120 |
if (egress) { |
| 121 |
fprintf(stderr, |
| 122 |
"Can't have both ingress and egress\n"); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
NEXT_ARG(); |
| 126 |
ok++; |
| 127 |
continue; |
| 128 |
} else { |
| 129 |
|
| 130 |
if (matches(*argv, "index") == 0) { |
| 131 |
NEXT_ARG(); |
| 132 |
if (get_u32(&p.index, *argv, 10)) { |
| 133 |
fprintf(stderr, "Illegal \"index\"\n"); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
iok++; |
| 137 |
if (!ok) { |
| 138 |
argc--; |
| 139 |
argv++; |
| 140 |
break; |
| 141 |
} |
| 142 |
} else if (!ok) { |
| 143 |
fprintf(stderr, |
| 144 |
"was expecting egress or ingress (%s)\n", |
| 145 |
*argv); |
| 146 |
break; |
| 147 |
|
| 148 |
} else if (!mirror && matches(*argv, "mirror") == 0) { |
| 149 |
mirror = 1; |
| 150 |
if (redir) { |
| 151 |
fprintf(stderr, |
| 152 |
"Can't have both mirror and redir\n"); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
p.eaction = egress ? TCA_EGRESS_MIRROR : |
| 156 |
TCA_INGRESS_MIRROR; |
| 157 |
p.action = TC_ACT_PIPE; |
| 158 |
ok++; |
| 159 |
} else if (!redir && matches(*argv, "redirect") == 0) { |
| 160 |
redir = 1; |
| 161 |
if (mirror) { |
| 162 |
fprintf(stderr, |
| 163 |
"Can't have both mirror and redir\n"); |
| 164 |
return -1; |
| 165 |
} |
| 166 |
p.eaction = egress ? TCA_EGRESS_REDIR : |
| 167 |
TCA_INGRESS_REDIR; |
| 168 |
p.action = TC_ACT_STOLEN; |
| 169 |
ok++; |
| 170 |
} else if ((redir || mirror)) { |
| 171 |
if (strcmp(*argv, "blockid") == 0) { |
| 172 |
if (strlen(d)) { |
| 173 |
fprintf(stderr, |
| 174 |
"blockid and device are mutually exclusive.\n"); |
| 175 |
return -1; |
| 176 |
} |
| 177 |
NEXT_ARG(); |
| 178 |
if (get_u32(&blockid, *argv, 0) || |
| 179 |
!blockid) { |
| 180 |
fprintf(stderr, |
| 181 |
"invalid block ID"); |
| 182 |
return -1; |
| 183 |
} |
| 184 |
argc--; |
| 185 |
argv++; |
| 186 |
} |
| 187 |
if (argc && matches(*argv, "dev") == 0) { |
| 188 |
if (blockid) { |
| 189 |
fprintf(stderr, |
| 190 |
"blockid and device are mutually exclusive.\n"); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
NEXT_ARG(); |
| 194 |
if (strlen(d)) |
| 195 |
duparg("dev", *argv); |
| 196 |
|
| 197 |
strncpy(d, *argv, sizeof(d)-1); |
| 198 |
argc--; |
| 199 |
argv++; |
| 200 |
} |
| 201 |
|
| 202 |
break; |
| 203 |
|
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
NEXT_ARG(); |
| 208 |
} |
| 209 |
|
| 210 |
if (!ok && !iok) |
| 211 |
return -1; |
| 212 |
|
| 213 |
if (d[0]) { |
| 214 |
int idx; |
| 215 |
|
| 216 |
ll_init_map(&rth); |
| 217 |
|
| 218 |
idx = ll_name_to_index(d); |
| 219 |
if (!idx) |
| 220 |
return nodev(d); |
| 221 |
|
| 222 |
p.ifindex = idx; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
if (p.eaction == TCA_EGRESS_MIRROR || p.eaction == TCA_INGRESS_MIRROR) |
| 227 |
parse_action_control_dflt(&argc, &argv, &p.action, false, |
| 228 |
TC_ACT_PIPE); |
| 229 |
|
| 230 |
if (argc) { |
| 231 |
if (iok && matches(*argv, "index") == 0) { |
| 232 |
fprintf(stderr, "mirred: Illegal double index\n"); |
| 233 |
return -1; |
| 234 |
} |
| 235 |
|
| 236 |
if (matches(*argv, "index") == 0) { |
| 237 |
NEXT_ARG(); |
| 238 |
if (get_u32(&p.index, *argv, 10)) { |
| 239 |
fprintf(stderr, |
| 240 |
"mirred: Illegal \"index\"\n"); |
| 241 |
return -1; |
| 242 |
} |
| 243 |
argc--; |
| 244 |
argv++; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
tail = addattr_nest(n, MAX_MSG, tca_id); |
| 249 |
addattr_l(n, MAX_MSG, TCA_MIRRED_PARMS, &p, sizeof(p)); |
| 250 |
if (blockid) |
| 251 |
addattr32(n, MAX_MSG, TCA_MIRRED_BLOCKID, blockid); |
| 252 |
addattr_nest_end(n, tail); |
| 253 |
|
| 254 |
*argc_p = argc; |
| 255 |
*argv_p = argv; |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
static int |
| 261 |
parse_mirred(const struct action_util *a, int *argc_p, char ***argv_p, |
| 262 |
int tca_id, struct nlmsghdr *n) |
| 263 |
{ |
| 264 |
|
| 265 |
int argc = *argc_p; |
| 266 |
char **argv = *argv_p; |
| 267 |
|
| 268 |
if (argc < 0) { |
| 269 |
fprintf(stderr, "mirred bad argument count %d\n", argc); |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
if (matches(*argv, "mirred") == 0) { |
| 274 |
NEXT_ARG(); |
| 275 |
} else { |
| 276 |
fprintf(stderr, "mirred bad argument %s\n", *argv); |
| 277 |
return -1; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
if (matches(*argv, "egress") == 0 || matches(*argv, "ingress") == 0 || |
| 282 |
matches(*argv, "index") == 0) { |
| 283 |
int ret = parse_direction(a, &argc, &argv, tca_id, n); |
| 284 |
|
| 285 |
if (ret == 0) { |
| 286 |
*argc_p = argc; |
| 287 |
*argv_p = argv; |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
} else if (matches(*argv, "help") == 0) { |
| 292 |
usage(); |
| 293 |
} else { |
| 294 |
fprintf(stderr, "mirred option not supported %s\n", *argv); |
| 295 |
} |
| 296 |
|
| 297 |
return -1; |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
static int |
| 302 |
print_mirred(const struct action_util *au, FILE *f, struct rtattr *arg) |
| 303 |
{ |
| 304 |
struct tc_mirred *p; |
| 305 |
struct rtattr *tb[TCA_MIRRED_MAX + 1]; |
| 306 |
const char *dev; |
| 307 |
|
| 308 |
print_string(PRINT_ANY, "kind", "%s ", "mirred"); |
| 309 |
if (arg == NULL) |
| 310 |
return 0; |
| 311 |
|
| 312 |
parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg); |
| 313 |
|
| 314 |
if (tb[TCA_MIRRED_PARMS] == NULL) { |
| 315 |
fprintf(stderr, "Missing mirred parameters\n"); |
| 316 |
return -1; |
| 317 |
} |
| 318 |
p = RTA_DATA(tb[TCA_MIRRED_PARMS]); |
| 319 |
|
| 320 |
dev = ll_index_to_name(p->ifindex); |
| 321 |
if (dev == 0) { |
| 322 |
fprintf(stderr, "Cannot find device %d\n", p->ifindex); |
| 323 |
return -1; |
| 324 |
} |
| 325 |
|
| 326 |
print_string(PRINT_FP, NULL, "(%s", mirred_n2a(p->eaction)); |
| 327 |
print_string(PRINT_JSON, "mirred_action", NULL, |
| 328 |
mirred_action(p->eaction)); |
| 329 |
print_string(PRINT_JSON, "direction", NULL, |
| 330 |
mirred_direction(p->eaction)); |
| 331 |
if (tb[TCA_MIRRED_BLOCKID]) { |
| 332 |
const __u32 *blockid = RTA_DATA(tb[TCA_MIRRED_BLOCKID]); |
| 333 |
|
| 334 |
print_uint(PRINT_ANY, "to_blockid", " to blockid %u)", |
| 335 |
*blockid); |
| 336 |
} else { |
| 337 |
print_string(PRINT_ANY, "to_dev", " to device %s)", dev); |
| 338 |
} |
| 339 |
|
| 340 |
print_action_control(" ", p->action, ""); |
| 341 |
|
| 342 |
print_nl(); |
| 343 |
print_uint(PRINT_ANY, "index", "\tindex %u", p->index); |
| 344 |
print_int(PRINT_ANY, "ref", " ref %d", p->refcnt); |
| 345 |
print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt); |
| 346 |
|
| 347 |
if (show_stats) { |
| 348 |
if (tb[TCA_MIRRED_TM]) { |
| 349 |
struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]); |
| 350 |
|
| 351 |
print_tm(tm); |
| 352 |
} |
| 353 |
} |
| 354 |
print_nl(); |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
struct action_util mirred_action_util = { |
| 359 |
.id = "mirred", |
| 360 |
.parse_aopt = parse_mirred, |
| 361 |
.print_aopt = print_mirred, |
| 362 |
}; |