| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_ife.c IFE actions module |
| 4 |
* |
| 5 |
* Authors: J Hadi Salim ([email protected]) |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <sys/socket.h> |
| 13 |
#include <netinet/in.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <string.h> |
| 16 |
#include <linux/netdevice.h> |
| 17 |
|
| 18 |
#include "rt_names.h" |
| 19 |
#include "utils.h" |
| 20 |
#include "tc_util.h" |
| 21 |
#include <linux/tc_act/tc_ife.h> |
| 22 |
|
| 23 |
static void ife_explain(void) |
| 24 |
{ |
| 25 |
fprintf(stderr, |
| 26 |
"Usage:... ife {decode|encode} [{ALLOW|USE} ATTR] [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n" |
| 27 |
"\tALLOW := Encode direction. Allows encoding specified metadata\n" |
| 28 |
"\t\t e.g \"allow mark\"\n" |
| 29 |
"\tUSE := Encode direction. Enforce Static encoding of specified metadata\n" |
| 30 |
"\t\t e.g \"use mark 0x12\"\n" |
| 31 |
"\tATTR := mark (32-bit), prio (32-bit), tcindex (16-bit)\n" |
| 32 |
"\tDMAC := 6 byte Destination MAC address to encode\n" |
| 33 |
"\tSMAC := optional 6 byte Source MAC address to encode\n" |
| 34 |
"\tTYPE := optional 16 bit ethertype to encode\n" |
| 35 |
"\tCONTROL := reclassify|pipe|drop|continue|ok\n" |
| 36 |
"\tINDEX := optional IFE table index value used\n" |
| 37 |
"encode is used for sending IFE packets\n" |
| 38 |
"decode is used for receiving IFE packets\n"); |
| 39 |
} |
| 40 |
|
| 41 |
static void ife_usage(void) |
| 42 |
{ |
| 43 |
ife_explain(); |
| 44 |
exit(-1); |
| 45 |
} |
| 46 |
|
| 47 |
static int parse_ife(const struct action_util *a, int *argc_p, char ***argv_p, |
| 48 |
int tca_id, struct nlmsghdr *n) |
| 49 |
{ |
| 50 |
int argc = *argc_p; |
| 51 |
char **argv = *argv_p; |
| 52 |
int ok = 0; |
| 53 |
struct tc_ife p = { 0 }; |
| 54 |
struct rtattr *tail; |
| 55 |
struct rtattr *tail2; |
| 56 |
char dbuf[ETH_ALEN]; |
| 57 |
char sbuf[ETH_ALEN]; |
| 58 |
__u16 ife_type = 0; |
| 59 |
int user_type = 0; |
| 60 |
__u32 ife_prio = 0; |
| 61 |
__u32 ife_prio_v = 0; |
| 62 |
__u32 ife_mark = 0; |
| 63 |
__u32 ife_mark_v = 0; |
| 64 |
__u16 ife_tcindex = 0; |
| 65 |
__u16 ife_tcindex_v = 0; |
| 66 |
char *daddr = NULL; |
| 67 |
char *saddr = NULL; |
| 68 |
|
| 69 |
if (argc <= 0) |
| 70 |
return -1; |
| 71 |
|
| 72 |
while (argc > 0) { |
| 73 |
if (matches(*argv, "ife") == 0) { |
| 74 |
NEXT_ARG(); |
| 75 |
continue; |
| 76 |
} else if (matches(*argv, "decode") == 0) { |
| 77 |
p.flags = IFE_DECODE; /* readability aid */ |
| 78 |
ok++; |
| 79 |
} else if (matches(*argv, "encode") == 0) { |
| 80 |
p.flags = IFE_ENCODE; |
| 81 |
ok++; |
| 82 |
} else if (matches(*argv, "allow") == 0) { |
| 83 |
NEXT_ARG(); |
| 84 |
if (matches(*argv, "mark") == 0) { |
| 85 |
ife_mark = IFE_META_SKBMARK; |
| 86 |
} else if (matches(*argv, "prio") == 0) { |
| 87 |
ife_prio = IFE_META_PRIO; |
| 88 |
} else if (matches(*argv, "tcindex") == 0) { |
| 89 |
ife_tcindex = IFE_META_TCINDEX; |
| 90 |
} else { |
| 91 |
invarg("Illegal meta define", *argv); |
| 92 |
} |
| 93 |
} else if (matches(*argv, "use") == 0) { |
| 94 |
NEXT_ARG(); |
| 95 |
if (matches(*argv, "mark") == 0) { |
| 96 |
NEXT_ARG(); |
| 97 |
if (get_u32(&ife_mark_v, *argv, 0)) |
| 98 |
invarg("ife mark val is invalid", |
| 99 |
*argv); |
| 100 |
} else if (matches(*argv, "prio") == 0) { |
| 101 |
NEXT_ARG(); |
| 102 |
if (get_u32(&ife_prio_v, *argv, 0)) |
| 103 |
invarg("ife prio val is invalid", |
| 104 |
*argv); |
| 105 |
} else if (matches(*argv, "tcindex") == 0) { |
| 106 |
NEXT_ARG(); |
| 107 |
if (get_u16(&ife_tcindex_v, *argv, 0)) |
| 108 |
invarg("ife tcindex val is invalid", |
| 109 |
*argv); |
| 110 |
} else { |
| 111 |
invarg("Illegal meta use type", *argv); |
| 112 |
} |
| 113 |
} else if (matches(*argv, "type") == 0) { |
| 114 |
NEXT_ARG(); |
| 115 |
if (get_u16(&ife_type, *argv, 0)) |
| 116 |
invarg("ife type is invalid", *argv); |
| 117 |
fprintf(stderr, "IFE type 0x%04X\n", ife_type); |
| 118 |
user_type = 1; |
| 119 |
} else if (matches(*argv, "dst") == 0) { |
| 120 |
NEXT_ARG(); |
| 121 |
daddr = *argv; |
| 122 |
if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 123 |
dbuf, dbuf + 1, dbuf + 2, |
| 124 |
dbuf + 3, dbuf + 4, dbuf + 5) != 6) { |
| 125 |
invarg("Invalid mac address", *argv); |
| 126 |
} |
| 127 |
fprintf(stderr, "dst MAC address <%s>\n", daddr); |
| 128 |
|
| 129 |
} else if (matches(*argv, "src") == 0) { |
| 130 |
NEXT_ARG(); |
| 131 |
saddr = *argv; |
| 132 |
if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 133 |
sbuf, sbuf + 1, sbuf + 2, |
| 134 |
sbuf + 3, sbuf + 4, sbuf + 5) != 6) { |
| 135 |
invarg("Invalid mac address", *argv); |
| 136 |
} |
| 137 |
fprintf(stderr, "src MAC address <%s>\n", saddr); |
| 138 |
} else if (matches(*argv, "help") == 0) { |
| 139 |
ife_usage(); |
| 140 |
} else { |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
argc--; |
| 145 |
argv++; |
| 146 |
} |
| 147 |
|
| 148 |
parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE); |
| 149 |
|
| 150 |
if (argc) { |
| 151 |
if (matches(*argv, "index") == 0) { |
| 152 |
NEXT_ARG(); |
| 153 |
if (get_u32(&p.index, *argv, 0)) { |
| 154 |
fprintf(stderr, "ife: Illegal \"index\"\n"); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
ok++; |
| 158 |
argc--; |
| 159 |
argv++; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (!ok) { |
| 164 |
fprintf(stderr, "IFE requires decode/encode specified\n"); |
| 165 |
ife_usage(); |
| 166 |
} |
| 167 |
|
| 168 |
tail = addattr_nest(n, MAX_MSG, tca_id); |
| 169 |
addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p)); |
| 170 |
|
| 171 |
if (!(p.flags & IFE_ENCODE)) |
| 172 |
goto skip_encode; |
| 173 |
|
| 174 |
if (daddr) |
| 175 |
addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN); |
| 176 |
if (user_type) |
| 177 |
addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2); |
| 178 |
else |
| 179 |
fprintf(stderr, "IFE type 0x%04X\n", ETH_P_IFE); |
| 180 |
if (saddr) |
| 181 |
addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN); |
| 182 |
|
| 183 |
tail2 = addattr_nest(n, MAX_MSG, TCA_IFE_METALST); |
| 184 |
if (ife_mark || ife_mark_v) { |
| 185 |
if (ife_mark_v) |
| 186 |
addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4); |
| 187 |
else |
| 188 |
addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0); |
| 189 |
} |
| 190 |
if (ife_prio || ife_prio_v) { |
| 191 |
if (ife_prio_v) |
| 192 |
addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4); |
| 193 |
else |
| 194 |
addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0); |
| 195 |
} |
| 196 |
if (ife_tcindex || ife_tcindex_v) { |
| 197 |
if (ife_tcindex_v) |
| 198 |
addattr_l(n, MAX_MSG, IFE_META_TCINDEX, &ife_tcindex_v, |
| 199 |
2); |
| 200 |
else |
| 201 |
addattr_l(n, MAX_MSG, IFE_META_TCINDEX, NULL, 0); |
| 202 |
} |
| 203 |
|
| 204 |
addattr_nest_end(n, tail2); |
| 205 |
|
| 206 |
skip_encode: |
| 207 |
addattr_nest_end(n, tail); |
| 208 |
|
| 209 |
*argc_p = argc; |
| 210 |
*argv_p = argv; |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static int print_ife(const struct action_util *au, FILE *f, struct rtattr *arg) |
| 215 |
{ |
| 216 |
struct tc_ife *p; |
| 217 |
struct rtattr *tb[TCA_IFE_MAX + 1]; |
| 218 |
__u16 ife_type = 0; |
| 219 |
__u32 mmark = 0; |
| 220 |
__u16 mtcindex = 0; |
| 221 |
__u32 mprio = 0; |
| 222 |
int has_optional = 0; |
| 223 |
SPRINT_BUF(b2); |
| 224 |
|
| 225 |
print_string(PRINT_ANY, "kind", "%s ", "ife"); |
| 226 |
if (arg == NULL) |
| 227 |
return 0; |
| 228 |
|
| 229 |
parse_rtattr_nested(tb, TCA_IFE_MAX, arg); |
| 230 |
|
| 231 |
if (tb[TCA_IFE_PARMS] == NULL) { |
| 232 |
fprintf(stderr, "Missing ife parameters\n"); |
| 233 |
return -1; |
| 234 |
} |
| 235 |
p = RTA_DATA(tb[TCA_IFE_PARMS]); |
| 236 |
|
| 237 |
print_string(PRINT_ANY, "mode", "%s ", |
| 238 |
p->flags & IFE_ENCODE ? "encode" : "decode"); |
| 239 |
print_action_control("action ", p->action, " "); |
| 240 |
|
| 241 |
if (tb[TCA_IFE_TYPE]) { |
| 242 |
ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]); |
| 243 |
has_optional = 1; |
| 244 |
print_0xhex(PRINT_ANY, "type", "type %#llX ", ife_type); |
| 245 |
} |
| 246 |
|
| 247 |
if (has_optional) |
| 248 |
print_string(PRINT_FP, NULL, "%s\t", _SL_); |
| 249 |
|
| 250 |
if (tb[TCA_IFE_METALST]) { |
| 251 |
struct rtattr *metalist[IFE_META_MAX + 1]; |
| 252 |
int len = 0; |
| 253 |
|
| 254 |
parse_rtattr_nested(metalist, IFE_META_MAX, |
| 255 |
tb[TCA_IFE_METALST]); |
| 256 |
|
| 257 |
if (metalist[IFE_META_SKBMARK]) { |
| 258 |
len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]); |
| 259 |
if (len) { |
| 260 |
mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]); |
| 261 |
print_uint(PRINT_ANY, "mark", "use mark %u ", |
| 262 |
mmark); |
| 263 |
} else |
| 264 |
print_string(PRINT_ANY, "mark", "%s mark ", |
| 265 |
"allow"); |
| 266 |
} |
| 267 |
|
| 268 |
if (metalist[IFE_META_TCINDEX]) { |
| 269 |
len = RTA_PAYLOAD(metalist[IFE_META_TCINDEX]); |
| 270 |
if (len) { |
| 271 |
mtcindex = |
| 272 |
rta_getattr_u16(metalist[IFE_META_TCINDEX]); |
| 273 |
print_uint(PRINT_ANY, "tcindex", |
| 274 |
"use tcindex %u ", mtcindex); |
| 275 |
} else |
| 276 |
print_string(PRINT_ANY, "tcindex", |
| 277 |
"%s tcindex ", "allow"); |
| 278 |
} |
| 279 |
|
| 280 |
if (metalist[IFE_META_PRIO]) { |
| 281 |
len = RTA_PAYLOAD(metalist[IFE_META_PRIO]); |
| 282 |
if (len) { |
| 283 |
mprio = rta_getattr_u32(metalist[IFE_META_PRIO]); |
| 284 |
print_uint(PRINT_ANY, "prio", "use prio %u ", |
| 285 |
mprio); |
| 286 |
} else |
| 287 |
print_string(PRINT_ANY, "prio", "%s prio ", |
| 288 |
"allow"); |
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
if (tb[TCA_IFE_DMAC]) { |
| 294 |
has_optional = 1; |
| 295 |
print_string(PRINT_ANY, "dst", "dst %s ", |
| 296 |
ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]), |
| 297 |
RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2, |
| 298 |
sizeof(b2))); |
| 299 |
} |
| 300 |
|
| 301 |
if (tb[TCA_IFE_SMAC]) { |
| 302 |
has_optional = 1; |
| 303 |
print_string(PRINT_ANY, "src", "src %s ", |
| 304 |
ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]), |
| 305 |
RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2, |
| 306 |
sizeof(b2))); |
| 307 |
} |
| 308 |
|
| 309 |
print_nl(); |
| 310 |
print_uint(PRINT_ANY, "index", "\t index %u", p->index); |
| 311 |
print_int(PRINT_ANY, "ref", " ref %d", p->refcnt); |
| 312 |
print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt); |
| 313 |
|
| 314 |
if (show_stats) { |
| 315 |
if (tb[TCA_IFE_TM]) { |
| 316 |
struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]); |
| 317 |
|
| 318 |
print_tm(tm); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
print_nl(); |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
struct action_util ife_action_util = { |
| 328 |
.id = "ife", |
| 329 |
.parse_aopt = parse_ife, |
| 330 |
.print_aopt = print_ife, |
| 331 |
}; |