Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 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 | |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 22 | static const char * const action_names[] = { |
| 23 | [TCA_VLAN_ACT_POP] = "pop", |
| 24 | [TCA_VLAN_ACT_PUSH] = "push", |
| 25 | [TCA_VLAN_ACT_MODIFY] = "modify", |
| 26 | }; |
| 27 | |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 28 | static void explain(void) |
| 29 | { |
| 30 | fprintf(stderr, "Usage: vlan pop\n"); |
Hadar Hen Zion | 0e43ed9 | 2016-09-01 09:45:48 +0300 | [diff] [blame] | 31 | fprintf(stderr, " vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"); |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 32 | fprintf(stderr, " vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"); |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 33 | fprintf(stderr, " VLANPROTO is one of 802.1Q or 802.1AD\n"); |
| 34 | fprintf(stderr, " with default: 802.1Q\n"); |
Phil Sutter | 51011da | 2016-03-22 15:48:38 +0100 | [diff] [blame] | 35 | fprintf(stderr, " CONTROL := reclassify | pipe | drop | continue | pass\n"); |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static void usage(void) |
| 39 | { |
| 40 | explain(); |
| 41 | exit(-1); |
| 42 | } |
| 43 | |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 44 | static bool has_push_attribs(int action) |
| 45 | { |
| 46 | return action == TCA_VLAN_ACT_PUSH || action == TCA_VLAN_ACT_MODIFY; |
| 47 | } |
| 48 | |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 49 | static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p, |
| 50 | int tca_id, struct nlmsghdr *n) |
| 51 | { |
| 52 | int argc = *argc_p; |
| 53 | char **argv = *argv_p; |
| 54 | struct rtattr *tail; |
| 55 | int action = 0; |
| 56 | __u16 id; |
| 57 | int id_set = 0; |
| 58 | __u16 proto; |
| 59 | int proto_set = 0; |
Hadar Hen Zion | 0e43ed9 | 2016-09-01 09:45:48 +0300 | [diff] [blame] | 60 | __u8 prio; |
| 61 | int prio_set = 0; |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 62 | struct tc_vlan parm = { 0 }; |
| 63 | |
| 64 | if (matches(*argv, "vlan") != 0) |
| 65 | return -1; |
| 66 | |
| 67 | NEXT_ARG(); |
| 68 | |
| 69 | while (argc > 0) { |
| 70 | if (matches(*argv, "pop") == 0) { |
| 71 | if (action) { |
| 72 | fprintf(stderr, "unexpected \"%s\" - action already specified\n", |
| 73 | *argv); |
| 74 | explain(); |
| 75 | return -1; |
| 76 | } |
| 77 | action = TCA_VLAN_ACT_POP; |
| 78 | } else if (matches(*argv, "push") == 0) { |
| 79 | if (action) { |
| 80 | fprintf(stderr, "unexpected \"%s\" - action already specified\n", |
| 81 | *argv); |
| 82 | explain(); |
| 83 | return -1; |
| 84 | } |
| 85 | action = TCA_VLAN_ACT_PUSH; |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 86 | } else if (matches(*argv, "modify") == 0) { |
| 87 | if (action) { |
| 88 | fprintf(stderr, "unexpected \"%s\" - action already specified\n", |
| 89 | *argv); |
| 90 | explain(); |
| 91 | return -1; |
| 92 | } |
| 93 | action = TCA_VLAN_ACT_MODIFY; |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 94 | } else if (matches(*argv, "id") == 0) { |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 95 | if (!has_push_attribs(action)) { |
| 96 | fprintf(stderr, "\"%s\" is only valid for push/modify\n", |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 97 | *argv); |
| 98 | explain(); |
| 99 | return -1; |
| 100 | } |
| 101 | NEXT_ARG(); |
| 102 | if (get_u16(&id, *argv, 0)) |
| 103 | invarg("id is invalid", *argv); |
| 104 | id_set = 1; |
| 105 | } else if (matches(*argv, "protocol") == 0) { |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 106 | if (!has_push_attribs(action)) { |
| 107 | fprintf(stderr, "\"%s\" is only valid for push/modify\n", |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 108 | *argv); |
| 109 | explain(); |
| 110 | return -1; |
| 111 | } |
| 112 | NEXT_ARG(); |
| 113 | if (ll_proto_a2n(&proto, *argv)) |
| 114 | invarg("protocol is invalid", *argv); |
| 115 | proto_set = 1; |
Hadar Hen Zion | 0e43ed9 | 2016-09-01 09:45:48 +0300 | [diff] [blame] | 116 | } else if (matches(*argv, "priority") == 0) { |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 117 | if (!has_push_attribs(action)) { |
| 118 | fprintf(stderr, "\"%s\" is only valid for push/modify\n", |
Hadar Hen Zion | 0e43ed9 | 2016-09-01 09:45:48 +0300 | [diff] [blame] | 119 | *argv); |
| 120 | explain(); |
| 121 | return -1; |
| 122 | } |
| 123 | NEXT_ARG(); |
| 124 | if (get_u8(&prio, *argv, 0) || (prio & ~0x7)) |
| 125 | invarg("prio is invalid", *argv); |
| 126 | prio_set = 1; |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 127 | } else if (matches(*argv, "help") == 0) { |
| 128 | usage(); |
| 129 | } else { |
| 130 | break; |
| 131 | } |
| 132 | argc--; |
| 133 | argv++; |
| 134 | } |
| 135 | |
| 136 | parm.action = TC_ACT_PIPE; |
Phil Sutter | 69f5aff | 2016-07-23 13:28:09 +0200 | [diff] [blame] | 137 | if (argc && !action_a2n(*argv, &parm.action, false)) |
| 138 | NEXT_ARG_FWD(); |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 139 | |
| 140 | if (argc) { |
| 141 | if (matches(*argv, "index") == 0) { |
| 142 | NEXT_ARG(); |
| 143 | if (get_u32(&parm.index, *argv, 10)) { |
| 144 | fprintf(stderr, "vlan: Illegal \"index\"\n"); |
| 145 | return -1; |
| 146 | } |
| 147 | argc--; |
| 148 | argv++; |
| 149 | } |
| 150 | } |
| 151 | |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 152 | if (has_push_attribs(action) && !id_set) { |
| 153 | fprintf(stderr, "id needs to be set for %s\n", |
| 154 | action_names[action]); |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 155 | explain(); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | parm.v_action = action; |
| 160 | tail = NLMSG_TAIL(n); |
| 161 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 162 | addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm)); |
| 163 | if (id_set) |
| 164 | addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_ID, &id, 2); |
| 165 | if (proto_set) { |
| 166 | if (proto != htons(ETH_P_8021Q) && |
| 167 | proto != htons(ETH_P_8021AD)) { |
| 168 | fprintf(stderr, "protocol not supported\n"); |
| 169 | explain(); |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PROTOCOL, &proto, 2); |
| 174 | } |
Hadar Hen Zion | 0e43ed9 | 2016-09-01 09:45:48 +0300 | [diff] [blame] | 175 | if (prio_set) |
| 176 | addattr8(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PRIORITY, prio); |
| 177 | |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 178 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
| 179 | |
| 180 | *argc_p = argc; |
| 181 | *argv_p = argv; |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg) |
| 186 | { |
| 187 | SPRINT_BUF(b1); |
| 188 | struct rtattr *tb[TCA_VLAN_MAX + 1]; |
| 189 | __u16 val; |
| 190 | struct tc_vlan *parm; |
| 191 | |
| 192 | if (arg == NULL) |
| 193 | return -1; |
| 194 | |
| 195 | parse_rtattr_nested(tb, TCA_VLAN_MAX, arg); |
| 196 | |
| 197 | if (!tb[TCA_VLAN_PARMS]) { |
| 198 | fprintf(f, "[NULL vlan parameters]"); |
| 199 | return -1; |
| 200 | } |
| 201 | parm = RTA_DATA(tb[TCA_VLAN_PARMS]); |
| 202 | |
| 203 | fprintf(f, " vlan"); |
| 204 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 205 | switch (parm->v_action) { |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 206 | case TCA_VLAN_ACT_POP: |
| 207 | fprintf(f, " pop"); |
| 208 | break; |
| 209 | case TCA_VLAN_ACT_PUSH: |
Shmulik Ladkani | 4654173 | 2016-09-22 21:01:05 +0300 | [diff] [blame] | 210 | case TCA_VLAN_ACT_MODIFY: |
| 211 | fprintf(f, " %s", action_names[parm->v_action]); |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 212 | if (tb[TCA_VLAN_PUSH_VLAN_ID]) { |
| 213 | val = rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_ID]); |
| 214 | fprintf(f, " id %u", val); |
| 215 | } |
| 216 | if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { |
| 217 | fprintf(f, " protocol %s", |
| 218 | ll_proto_n2a(rta_getattr_u16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]), |
| 219 | b1, sizeof(b1))); |
| 220 | } |
Hadar Hen Zion | 0e43ed9 | 2016-09-01 09:45:48 +0300 | [diff] [blame] | 221 | if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) { |
| 222 | val = rta_getattr_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); |
| 223 | fprintf(f, " priority %u", val); |
| 224 | } |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 225 | break; |
| 226 | } |
Phil Sutter | 7093200 | 2016-07-23 13:28:10 +0200 | [diff] [blame] | 227 | fprintf(f, " %s", action_n2a(parm->action)); |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 228 | |
| 229 | fprintf(f, "\n\t index %d ref %d bind %d", parm->index, parm->refcnt, |
| 230 | parm->bindcnt); |
| 231 | |
| 232 | if (show_stats) { |
| 233 | if (tb[TCA_VLAN_TM]) { |
| 234 | struct tcf_t *tm = RTA_DATA(tb[TCA_VLAN_TM]); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 235 | |
Jiri Pirko | 8b1c021 | 2014-11-21 12:31:30 +0100 | [diff] [blame] | 236 | print_tm(f, tm); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | fprintf(f, "\n "); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | struct action_util vlan_action_util = { |
| 246 | .id = "vlan", |
| 247 | .parse_aopt = parse_vlan, |
| 248 | .print_aopt = print_vlan, |
| 249 | }; |