| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* m_skbmod.c skb modifier action 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_skbmod.h> |
| 22 |
|
| 23 |
static void skbmod_explain(void) |
| 24 |
{ |
| 25 |
fprintf(stderr, |
| 26 |
"Usage:... skbmod { set <SETTABLE> | swap <SWAPPABLE> | ecn } [CONTROL] [index INDEX]\n" |
| 27 |
"where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE]\n" |
| 28 |
"where SWAPPABLE is: \"mac\" to swap mac addresses\n" |
| 29 |
"\tDMAC := 6 byte Destination MAC address\n" |
| 30 |
"\tSMAC := optional 6 byte Source MAC address\n" |
| 31 |
"\tETYPE := optional 16 bit ethertype\n" |
| 32 |
"\tCONTROL := reclassify | pipe | drop | continue | ok |\n" |
| 33 |
"\t goto chain <CHAIN_INDEX>\n" |
| 34 |
"\tINDEX := skbmod index value to use\n"); |
| 35 |
} |
| 36 |
|
| 37 |
static void skbmod_usage(void) |
| 38 |
{ |
| 39 |
skbmod_explain(); |
| 40 |
exit(-1); |
| 41 |
} |
| 42 |
|
| 43 |
static int parse_skbmod(const struct action_util *a, int *argc_p, char ***argv_p, |
| 44 |
int tca_id, struct nlmsghdr *n) |
| 45 |
{ |
| 46 |
int argc = *argc_p; |
| 47 |
char **argv = *argv_p; |
| 48 |
int ok = 0; |
| 49 |
struct tc_skbmod p; |
| 50 |
struct rtattr *tail; |
| 51 |
char dbuf[ETH_ALEN]; |
| 52 |
char sbuf[ETH_ALEN]; |
| 53 |
__u16 skbmod_etype = 0; |
| 54 |
char *daddr = NULL; |
| 55 |
char *saddr = NULL; |
| 56 |
|
| 57 |
memset(&p, 0, sizeof(p)); |
| 58 |
|
| 59 |
if (argc <= 0) |
| 60 |
return -1; |
| 61 |
|
| 62 |
while (argc > 0) { |
| 63 |
if (matches(*argv, "skbmod") == 0) { |
| 64 |
NEXT_ARG(); |
| 65 |
continue; |
| 66 |
} else if (matches(*argv, "swap") == 0) { |
| 67 |
NEXT_ARG(); |
| 68 |
continue; |
| 69 |
} else if (matches(*argv, "mac") == 0) { |
| 70 |
p.flags |= SKBMOD_F_SWAPMAC; |
| 71 |
ok += 1; |
| 72 |
} else if (matches(*argv, "set") == 0) { |
| 73 |
NEXT_ARG(); |
| 74 |
continue; |
| 75 |
} else if (matches(*argv, "etype") == 0) { |
| 76 |
NEXT_ARG(); |
| 77 |
if (get_u16(&skbmod_etype, *argv, 0)) |
| 78 |
invarg("ethertype is invalid", *argv); |
| 79 |
fprintf(stderr, "skbmod etype 0x%x\n", skbmod_etype); |
| 80 |
p.flags |= SKBMOD_F_ETYPE; |
| 81 |
ok += 1; |
| 82 |
} else if (matches(*argv, "dmac") == 0) { |
| 83 |
NEXT_ARG(); |
| 84 |
daddr = *argv; |
| 85 |
if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 86 |
dbuf, dbuf + 1, dbuf + 2, |
| 87 |
dbuf + 3, dbuf + 4, dbuf + 5) != 6) { |
| 88 |
fprintf(stderr, "Invalid dst mac address %s\n", |
| 89 |
daddr); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
p.flags |= SKBMOD_F_DMAC; |
| 93 |
fprintf(stderr, "dst MAC address <%s>\n", daddr); |
| 94 |
ok += 1; |
| 95 |
|
| 96 |
} else if (matches(*argv, "smac") == 0) { |
| 97 |
NEXT_ARG(); |
| 98 |
saddr = *argv; |
| 99 |
if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", |
| 100 |
sbuf, sbuf + 1, sbuf + 2, |
| 101 |
sbuf + 3, sbuf + 4, sbuf + 5) != 6) { |
| 102 |
fprintf(stderr, "Invalid smac address %s\n", |
| 103 |
saddr); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
p.flags |= SKBMOD_F_SMAC; |
| 107 |
fprintf(stderr, "src MAC address <%s>\n", saddr); |
| 108 |
ok += 1; |
| 109 |
} else if (matches(*argv, "ecn") == 0) { |
| 110 |
p.flags |= SKBMOD_F_ECN; |
| 111 |
ok += 1; |
| 112 |
} else if (matches(*argv, "help") == 0) { |
| 113 |
skbmod_usage(); |
| 114 |
} else { |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
argc--; |
| 119 |
argv++; |
| 120 |
} |
| 121 |
|
| 122 |
parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE); |
| 123 |
|
| 124 |
if (argc) { |
| 125 |
if (matches(*argv, "index") == 0) { |
| 126 |
NEXT_ARG(); |
| 127 |
if (get_u32(&p.index, *argv, 0)) { |
| 128 |
fprintf(stderr, "skbmod: Illegal \"index\"\n"); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
ok++; |
| 132 |
argc--; |
| 133 |
argv++; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (!ok) { |
| 138 |
fprintf(stderr, "skbmod requires at least one option\n"); |
| 139 |
skbmod_usage(); |
| 140 |
} |
| 141 |
|
| 142 |
tail = addattr_nest(n, MAX_MSG, tca_id); |
| 143 |
addattr_l(n, MAX_MSG, TCA_SKBMOD_PARMS, &p, sizeof(p)); |
| 144 |
|
| 145 |
if (daddr) |
| 146 |
addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN); |
| 147 |
if (skbmod_etype) |
| 148 |
addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype); |
| 149 |
if (saddr) |
| 150 |
addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN); |
| 151 |
|
| 152 |
addattr_nest_end(n, tail); |
| 153 |
|
| 154 |
*argc_p = argc; |
| 155 |
*argv_p = argv; |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
static int print_skbmod(const struct action_util *au, FILE *f, struct rtattr *arg) |
| 160 |
{ |
| 161 |
struct tc_skbmod *p; |
| 162 |
struct rtattr *tb[TCA_SKBMOD_MAX + 1]; |
| 163 |
__u16 skbmod_etype = 0; |
| 164 |
int has_optional = 0; |
| 165 |
SPRINT_BUF(b1); |
| 166 |
SPRINT_BUF(b2); |
| 167 |
|
| 168 |
if (arg == NULL) |
| 169 |
return 0; |
| 170 |
|
| 171 |
parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg); |
| 172 |
|
| 173 |
if (tb[TCA_SKBMOD_PARMS] == NULL) { |
| 174 |
fprintf(stderr, "Missing skbmod parameters\n"); |
| 175 |
return -1; |
| 176 |
} |
| 177 |
|
| 178 |
p = RTA_DATA(tb[TCA_SKBMOD_PARMS]); |
| 179 |
|
| 180 |
print_string(PRINT_FP, NULL, "skbmod ", NULL); |
| 181 |
print_action_control("", p->action, " "); |
| 182 |
|
| 183 |
if (tb[TCA_SKBMOD_ETYPE]) { |
| 184 |
skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]); |
| 185 |
has_optional = 1; |
| 186 |
print_0xhex(PRINT_ANY, "etype", "set etype 0x%X ", skbmod_etype); |
| 187 |
} |
| 188 |
|
| 189 |
if (has_optional) |
| 190 |
print_string(PRINT_FP, NULL, "%s\t ", _SL_); |
| 191 |
|
| 192 |
if (tb[TCA_SKBMOD_DMAC]) { |
| 193 |
has_optional = 1; |
| 194 |
print_color_string(PRINT_ANY, COLOR_MAC, "dmac", |
| 195 |
"set dmac %s ", |
| 196 |
ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_DMAC]), |
| 197 |
RTA_PAYLOAD(tb[TCA_SKBMOD_DMAC]), 0, b1, |
| 198 |
sizeof(b1))); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
if (tb[TCA_SKBMOD_SMAC]) { |
| 203 |
has_optional = 1; |
| 204 |
print_color_string(PRINT_ANY, COLOR_MAC, "smac", |
| 205 |
"set smac %s ", |
| 206 |
ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_SMAC]), |
| 207 |
RTA_PAYLOAD(tb[TCA_SKBMOD_SMAC]), 0, b2, |
| 208 |
sizeof(b2))); |
| 209 |
} |
| 210 |
|
| 211 |
if (p->flags & SKBMOD_F_SWAPMAC) |
| 212 |
print_null(PRINT_ANY, "swapmac", "swap mac ", NULL); |
| 213 |
|
| 214 |
if (p->flags & SKBMOD_F_ECN) |
| 215 |
print_null(PRINT_ANY, "ecn", "ecn ", NULL); |
| 216 |
|
| 217 |
print_nl(); |
| 218 |
print_uint(PRINT_ANY, "index", "\t index %u ", p->index); |
| 219 |
print_int(PRINT_ANY, "ref", "ref %d ", p->refcnt); |
| 220 |
print_int(PRINT_ANY, "bind", "bind %d", p->bindcnt); |
| 221 |
|
| 222 |
if (show_stats) { |
| 223 |
if (tb[TCA_SKBMOD_TM]) { |
| 224 |
struct tcf_t *tm = RTA_DATA(tb[TCA_SKBMOD_TM]); |
| 225 |
|
| 226 |
print_tm(tm); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
print_string(PRINT_FP, NULL, "\n", NULL); |
| 231 |
|
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
struct action_util skbmod_action_util = { |
| 236 |
.id = "skbmod", |
| 237 |
.parse_aopt = parse_skbmod, |
| 238 |
.print_aopt = print_skbmod, |
| 239 |
}; |