1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* m_vlan.c vlan manipulation module
*
* Authors: Jiri Pirko <jiri@resnulli.us>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <linux/if_ether.h>
#include "utils.h"
#include "rt_names.h"
#include "tc_util.h"
#include <linux/tc_act/tc_vlan.h>
static const char * const action_names[] = {
[TCA_VLAN_ACT_POP] = "pop",
[TCA_VLAN_ACT_PUSH] = "push",
[TCA_VLAN_ACT_MODIFY] = "modify",
[TCA_VLAN_ACT_POP_ETH] = "pop_eth",
[TCA_VLAN_ACT_PUSH_ETH] = "push_eth",
};
static void explain(void)
{
fprintf(stderr,
"Usage: vlan pop [CONTROL]\n"
" vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
" vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
" vlan pop_eth [CONTROL]\n"
" vlan push_eth dst_mac LLADDR src_mac LLADDR [CONTROL]\n"
" VLANPROTO is one of 802.1Q or 802.1AD\n"
" with default: 802.1Q\n"
" CONTROL := reclassify | pipe | drop | continue | pass |\n"
" goto chain <CHAIN_INDEX>\n");
}
static void usage(void)
{
explain();
exit(-1);
}
static bool has_push_attribs(int action)
{
return action == TCA_VLAN_ACT_PUSH || action == TCA_VLAN_ACT_MODIFY;
}
static void unexpected(const char *arg)
{
fprintf(stderr,
"unexpected \"%s\" - action already specified\n",
arg);
explain();
}
static int parse_vlan(const struct action_util *a, int *argc_p, char ***argv_p,
int tca_id, struct nlmsghdr *n)
{
int argc = *argc_p;
char **argv = *argv_p;
struct rtattr *tail;
int action = 0;
char dst_mac[ETH_ALEN] = {};
int dst_mac_set = 0;
char src_mac[ETH_ALEN] = {};
int src_mac_set = 0;
__u16 id;
int id_set = 0;
__u16 proto;
int proto_set = 0;
__u8 prio;
int prio_set = 0;
struct tc_vlan parm = {};
if (matches(*argv, "vlan") != 0)
return -1;
NEXT_ARG();
while (argc > 0) {
if (matches(*argv, "pop") == 0) {
if (action) {
unexpected(*argv);
return -1;
}
action = TCA_VLAN_ACT_POP;
} else if (matches(*argv, "push") == 0) {
if (action) {
unexpected(*argv);
return -1;
}
action = TCA_VLAN_ACT_PUSH;
} else if (matches(*argv, "modify") == 0) {
if (action) {
unexpected(*argv);
return -1;
}
action = TCA_VLAN_ACT_MODIFY;
} else if (matches(*argv, "pop_eth") == 0) {
if (action) {
unexpected(*argv);
return -1;
}
action = TCA_VLAN_ACT_POP_ETH;
} else if (matches(*argv, "push_eth") == 0) {
if (action) {
unexpected(*argv);
return -1;
}
action = TCA_VLAN_ACT_PUSH_ETH;
} else if (matches(*argv, "id") == 0) {
if (!has_push_attribs(action))
invarg("only valid for push/modify", *argv);
NEXT_ARG();
if (get_u16(&id, *argv, 0))
invarg("id is invalid", *argv);
id_set = 1;
} else if (matches(*argv, "protocol") == 0) {
if (!has_push_attribs(action))
invarg("only valid for push/modify", *argv);
NEXT_ARG();
if (ll_proto_a2n(&proto, *argv))
invarg("protocol is invalid", *argv);
proto_set = 1;
} else if (matches(*argv, "priority") == 0) {
if (!has_push_attribs(action))
invarg("only valid for push/modify", *argv);
NEXT_ARG();
if (get_u8(&prio, *argv, 0) || (prio & ~0x7))
invarg("prio is invalid", *argv);
prio_set = 1;
} else if (matches(*argv, "dst_mac") == 0) {
if (action != TCA_VLAN_ACT_PUSH_ETH)
invarg("only valid for push_eth", *argv);
NEXT_ARG();
if (ll_addr_a2n(dst_mac, sizeof(dst_mac), *argv) < 0)
invarg("dst_mac is invalid", *argv);
dst_mac_set = 1;
} else if (matches(*argv, "src_mac") == 0) {
if (action != TCA_VLAN_ACT_PUSH_ETH)
invarg("only valid for push_eth", *argv);
NEXT_ARG();
if (ll_addr_a2n(src_mac, sizeof(src_mac), *argv) < 0)
invarg("src_mac is invalid", *argv);
src_mac_set = 1;
} else if (matches(*argv, "help") == 0) {
usage();
} else {
break;
}
argc--;
argv++;
}
parse_action_control_dflt(&argc, &argv, &parm.action,
false, TC_ACT_PIPE);
if (argc) {
if (matches(*argv, "index") == 0) {
NEXT_ARG();
if (get_u32(&parm.index, *argv, 10)) {
fprintf(stderr, "vlan: Illegal \"index\"\n");
return -1;
}
argc--;
argv++;
}
}
if (has_push_attribs(action) && !id_set) {
fprintf(stderr, "id needs to be set for %s\n",
action_names[action]);
explain();
return -1;
}
if (action == TCA_VLAN_ACT_PUSH_ETH) {
if (!dst_mac_set) {
fprintf(stderr, "dst_mac needs to be set for %s\n",
action_names[action]);
explain();
return -1;
} else if (!src_mac_set) {
fprintf(stderr, "src_mac needs to be set for %s\n",
action_names[action]);
explain();
return -1;
}
}
parm.v_action = action;
tail = addattr_nest(n, MAX_MSG, tca_id);
addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm));
if (id_set)
addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_ID, &id, 2);
if (proto_set) {
if (proto != htons(ETH_P_8021Q) &&
proto != htons(ETH_P_8021AD)) {
fprintf(stderr, "protocol not supported\n");
explain();
return -1;
}
addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PROTOCOL, &proto, 2);
}
if (prio_set)
addattr8(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
if (dst_mac_set)
addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_ETH_DST, dst_mac,
sizeof(dst_mac));
if (src_mac_set)
addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_ETH_SRC, src_mac,
sizeof(src_mac));
addattr_nest_end(n, tail);
*argc_p = argc;
*argv_p = argv;
return 0;
}
static int print_vlan(const struct action_util *au, FILE *f, struct rtattr *arg)
{
SPRINT_BUF(b1);
struct rtattr *tb[TCA_VLAN_MAX + 1];
__u16 val;
struct tc_vlan *parm;
print_string(PRINT_ANY, "kind", "%s ", "vlan");
if (arg == NULL)
return 0;
parse_rtattr_nested(tb, TCA_VLAN_MAX, arg);
if (!tb[TCA_VLAN_PARMS]) {
fprintf(stderr, "Missing vlan parameters\n");
return -1;
}
parm = RTA_DATA(tb[TCA_VLAN_PARMS]);
print_string(PRINT_ANY, "vlan_action", " %s",
action_names[parm->v_action]);
switch (parm->v_action) {
case TCA_VLAN_ACT_PUSH:
case TCA_VLAN_ACT_MODIFY:
if (tb[TCA_VLAN_PUSH_VLAN_ID]) {
val = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
print_uint(PRINT_ANY, "id", " id %u", val);
}
if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
__u16 proto;
proto = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
print_string(PRINT_ANY, "protocol", " protocol %s",
ll_proto_n2a(proto, b1, sizeof(b1)));
}
if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) {
val = rta_getattr_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
print_uint(PRINT_ANY, "priority", " priority %u", val);
}
break;
case TCA_VLAN_ACT_PUSH_ETH:
if (tb[TCA_VLAN_PUSH_ETH_DST] &&
RTA_PAYLOAD(tb[TCA_VLAN_PUSH_ETH_DST]) == ETH_ALEN) {
ll_addr_n2a(RTA_DATA(tb[TCA_VLAN_PUSH_ETH_DST]),
ETH_ALEN, 0, b1, sizeof(b1));
print_string(PRINT_ANY, "dst_mac", " dst_mac %s", b1);
}
if (tb[TCA_VLAN_PUSH_ETH_SRC] &&
RTA_PAYLOAD(tb[TCA_VLAN_PUSH_ETH_SRC]) == ETH_ALEN) {
ll_addr_n2a(RTA_DATA(tb[TCA_VLAN_PUSH_ETH_SRC]),
ETH_ALEN, 0, b1, sizeof(b1));
print_string(PRINT_ANY, "src_mac", " src_mac %s", b1);
}
}
print_action_control(" ", parm->action, "");
print_nl();
print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
if (show_stats) {
if (tb[TCA_VLAN_TM]) {
struct tcf_t *tm = RTA_DATA(tb[TCA_VLAN_TM]);
print_tm(tm);
}
}
print_nl();
return 0;
}
struct action_util vlan_action_util = {
.id = "vlan",
.parse_aopt = parse_vlan,
.print_aopt = print_vlan,
};
|