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