Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * iplink_vlan.c VLAN device support |
| 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: Patrick McHardy <kaber@trash.net> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <linux/if_vlan.h> |
| 16 | |
| 17 | #include "rt_names.h" |
| 18 | #include "utils.h" |
| 19 | #include "ip_common.h" |
| 20 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 21 | static void print_explain(FILE *f) |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 22 | { |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 23 | fprintf(f, |
Phil Sutter | 8b47135 | 2017-03-28 23:19:38 +0200 | [diff] [blame] | 24 | "Usage: ... vlan id VLANID\n" |
| 25 | " [ protocol VLANPROTO ]\n" |
| 26 | " [ reorder_hdr { on | off } ]\n" |
| 27 | " [ gvrp { on | off } ]\n" |
| 28 | " [ mvrp { on | off } ]\n" |
| 29 | " [ loose_binding { on | off } ]\n" |
| 30 | " [ ingress-qos-map QOS-MAP ]\n" |
| 31 | " [ egress-qos-map QOS-MAP ]\n" |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 32 | "\n" |
| 33 | "VLANID := 0-4095\n" |
Phil Sutter | 8b47135 | 2017-03-28 23:19:38 +0200 | [diff] [blame] | 34 | "VLANPROTO: [ 802.1Q / 802.1ad ]\n" |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 35 | "QOS-MAP := [ QOS-MAP ] QOS-MAPPING\n" |
| 36 | "QOS-MAPPING := FROM:TO\n" |
| 37 | ); |
| 38 | } |
| 39 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 40 | static void explain(void) |
| 41 | { |
| 42 | print_explain(stderr); |
| 43 | } |
| 44 | |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 45 | static int on_off(const char *msg, const char *arg) |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 46 | { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 47 | fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n", msg, arg); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | static int vlan_parse_qos_map(int *argcp, char ***argvp, struct nlmsghdr *n, |
| 52 | int attrtype) |
| 53 | { |
| 54 | int argc = *argcp; |
| 55 | char **argv = *argvp; |
| 56 | struct ifla_vlan_qos_mapping m; |
| 57 | struct rtattr *tail; |
| 58 | |
| 59 | tail = NLMSG_TAIL(n); |
| 60 | addattr_l(n, 1024, attrtype, NULL, 0); |
| 61 | |
| 62 | while (argc > 0) { |
| 63 | char *colon = strchr(*argv, ':'); |
| 64 | |
| 65 | if (!colon) |
| 66 | break; |
| 67 | *colon = '\0'; |
| 68 | |
| 69 | if (get_u32(&m.from, *argv, 0)) |
| 70 | return 1; |
| 71 | if (get_u32(&m.to, colon + 1, 0)) |
| 72 | return 1; |
| 73 | argc--, argv++; |
| 74 | |
| 75 | addattr_l(n, 1024, IFLA_VLAN_QOS_MAPPING, &m, sizeof(m)); |
| 76 | } |
| 77 | |
| 78 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *)tail; |
| 79 | |
| 80 | *argcp = argc; |
| 81 | *argvp = argv; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int vlan_parse_opt(struct link_util *lu, int argc, char **argv, |
| 86 | struct nlmsghdr *n) |
| 87 | { |
| 88 | struct ifla_vlan_flags flags = { 0 }; |
Patric McHardy | 8fd8f6e | 2013-06-21 08:59:24 -0700 | [diff] [blame] | 89 | __u16 id, proto; |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 90 | |
| 91 | while (argc > 0) { |
Patric McHardy | 8fd8f6e | 2013-06-21 08:59:24 -0700 | [diff] [blame] | 92 | if (matches(*argv, "protocol") == 0) { |
| 93 | NEXT_ARG(); |
| 94 | if (ll_proto_a2n(&proto, *argv)) |
| 95 | invarg("protocol is invalid", *argv); |
| 96 | addattr_l(n, 1024, IFLA_VLAN_PROTOCOL, &proto, 2); |
| 97 | } else if (matches(*argv, "id") == 0) { |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 98 | NEXT_ARG(); |
| 99 | if (get_u16(&id, *argv, 0)) |
| 100 | invarg("id is invalid", *argv); |
| 101 | addattr_l(n, 1024, IFLA_VLAN_ID, &id, 2); |
| 102 | } else if (matches(*argv, "reorder_hdr") == 0) { |
| 103 | NEXT_ARG(); |
| 104 | flags.mask |= VLAN_FLAG_REORDER_HDR; |
| 105 | if (strcmp(*argv, "on") == 0) |
| 106 | flags.flags |= VLAN_FLAG_REORDER_HDR; |
| 107 | else if (strcmp(*argv, "off") == 0) |
| 108 | flags.flags &= ~VLAN_FLAG_REORDER_HDR; |
| 109 | else |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 110 | return on_off("reorder_hdr", *argv); |
Patrick McHardy | 4742064 | 2008-07-03 19:06:29 +0200 | [diff] [blame] | 111 | } else if (matches(*argv, "gvrp") == 0) { |
| 112 | NEXT_ARG(); |
| 113 | flags.mask |= VLAN_FLAG_GVRP; |
| 114 | if (strcmp(*argv, "on") == 0) |
| 115 | flags.flags |= VLAN_FLAG_GVRP; |
| 116 | else if (strcmp(*argv, "off") == 0) |
| 117 | flags.flags &= ~VLAN_FLAG_GVRP; |
| 118 | else |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 119 | return on_off("gvrp", *argv); |
David Ward | 4e9a686 | 2013-02-27 13:00:40 +0000 | [diff] [blame] | 120 | } else if (matches(*argv, "mvrp") == 0) { |
| 121 | NEXT_ARG(); |
| 122 | flags.mask |= VLAN_FLAG_MVRP; |
| 123 | if (strcmp(*argv, "on") == 0) |
| 124 | flags.flags |= VLAN_FLAG_MVRP; |
| 125 | else if (strcmp(*argv, "off") == 0) |
| 126 | flags.flags &= ~VLAN_FLAG_MVRP; |
| 127 | else |
| 128 | return on_off("mvrp", *argv); |
Patrick McHardy | 2180b6b | 2009-12-01 01:21:39 +0000 | [diff] [blame] | 129 | } else if (matches(*argv, "loose_binding") == 0) { |
| 130 | NEXT_ARG(); |
| 131 | flags.mask |= VLAN_FLAG_LOOSE_BINDING; |
| 132 | if (strcmp(*argv, "on") == 0) |
| 133 | flags.flags |= VLAN_FLAG_LOOSE_BINDING; |
| 134 | else if (strcmp(*argv, "off") == 0) |
| 135 | flags.flags &= ~VLAN_FLAG_LOOSE_BINDING; |
| 136 | else |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 137 | return on_off("loose_binding", *argv); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 138 | } else if (matches(*argv, "ingress-qos-map") == 0) { |
| 139 | NEXT_ARG(); |
| 140 | if (vlan_parse_qos_map(&argc, &argv, n, |
| 141 | IFLA_VLAN_INGRESS_QOS)) |
| 142 | invarg("invalid ingress-qos-map", *argv); |
| 143 | continue; |
| 144 | } else if (matches(*argv, "egress-qos-map") == 0) { |
| 145 | NEXT_ARG(); |
| 146 | if (vlan_parse_qos_map(&argc, &argv, n, |
| 147 | IFLA_VLAN_EGRESS_QOS)) |
| 148 | invarg("invalid egress-qos-map", *argv); |
| 149 | continue; |
| 150 | } else if (matches(*argv, "help") == 0) { |
| 151 | explain(); |
| 152 | return -1; |
| 153 | } else { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 154 | fprintf(stderr, "vlan: unknown command \"%s\"?\n", *argv); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 155 | explain(); |
| 156 | return -1; |
| 157 | } |
| 158 | argc--, argv++; |
| 159 | } |
| 160 | |
| 161 | if (flags.mask) |
| 162 | addattr_l(n, 1024, IFLA_VLAN_FLAGS, &flags, sizeof(flags)); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 167 | static void vlan_print_map(FILE *f, |
| 168 | const char *name_json, |
| 169 | const char *name_fp, |
| 170 | struct rtattr *attr) |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 171 | { |
| 172 | struct ifla_vlan_qos_mapping *m; |
| 173 | struct rtattr *i; |
| 174 | int rem; |
| 175 | |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 176 | open_json_array(PRINT_JSON, name_json); |
| 177 | print_string(PRINT_FP, NULL, "\n %s { ", name_fp); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 178 | |
| 179 | rem = RTA_PAYLOAD(attr); |
| 180 | for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { |
| 181 | m = RTA_DATA(i); |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 182 | |
| 183 | if (is_json_context()) { |
| 184 | open_json_object(NULL); |
| 185 | print_uint(PRINT_JSON, "from", NULL, m->from); |
| 186 | print_uint(PRINT_JSON, "to", NULL, m->to); |
| 187 | close_json_object(); |
| 188 | } else { |
| 189 | fprintf(f, "%u:%u ", m->from, m->to); |
| 190 | } |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 191 | } |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 192 | |
| 193 | close_json_array(PRINT_JSON, NULL); |
| 194 | print_string(PRINT_FP, NULL, "%s ", "}"); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void vlan_print_flags(FILE *fp, __u32 flags) |
| 198 | { |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 199 | open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<"); |
| 200 | #define _PF(f) if (flags & VLAN_FLAG_##f) { \ |
| 201 | flags &= ~VLAN_FLAG_##f; \ |
| 202 | print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); \ |
| 203 | } |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 204 | _PF(REORDER_HDR); |
Patrick McHardy | 4742064 | 2008-07-03 19:06:29 +0200 | [diff] [blame] | 205 | _PF(GVRP); |
David Ward | 4e9a686 | 2013-02-27 13:00:40 +0000 | [diff] [blame] | 206 | _PF(MVRP); |
Patrick McHardy | 2180b6b | 2009-12-01 01:21:39 +0000 | [diff] [blame] | 207 | _PF(LOOSE_BINDING); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 208 | #undef _PF |
| 209 | if (flags) |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 210 | print_hex(PRINT_ANY, NULL, "%x", flags); |
| 211 | close_json_array(PRINT_ANY, "> "); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 215 | { |
| 216 | struct ifla_vlan_flags *flags; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 217 | |
Patric McHardy | 8fd8f6e | 2013-06-21 08:59:24 -0700 | [diff] [blame] | 218 | SPRINT_BUF(b1); |
| 219 | |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 220 | if (!tb) |
| 221 | return; |
| 222 | |
Patric McHardy | 8fd8f6e | 2013-06-21 08:59:24 -0700 | [diff] [blame] | 223 | if (tb[IFLA_VLAN_PROTOCOL] && |
| 224 | RTA_PAYLOAD(tb[IFLA_VLAN_PROTOCOL]) < sizeof(__u16)) |
| 225 | return; |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 226 | if (!tb[IFLA_VLAN_ID] || |
| 227 | RTA_PAYLOAD(tb[IFLA_VLAN_ID]) < sizeof(__u16)) |
| 228 | return; |
| 229 | |
Patric McHardy | 8fd8f6e | 2013-06-21 08:59:24 -0700 | [diff] [blame] | 230 | if (tb[IFLA_VLAN_PROTOCOL]) |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 231 | print_string(PRINT_ANY, |
| 232 | "protocol", |
| 233 | "protocol %s ", |
| 234 | ll_proto_n2a( |
| 235 | rta_getattr_u16(tb[IFLA_VLAN_PROTOCOL]), |
Patric McHardy | 8fd8f6e | 2013-06-21 08:59:24 -0700 | [diff] [blame] | 236 | b1, sizeof(b1))); |
| 237 | else |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 238 | print_string(PRINT_ANY, "protocol", "protocol %s ", "802.1q"); |
Patric McHardy | 8fd8f6e | 2013-06-21 08:59:24 -0700 | [diff] [blame] | 239 | |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 240 | print_uint(PRINT_ANY, |
| 241 | "id", |
| 242 | "id %u ", |
| 243 | rta_getattr_u16(tb[IFLA_VLAN_ID])); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 244 | |
| 245 | if (tb[IFLA_VLAN_FLAGS]) { |
| 246 | if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags)) |
| 247 | return; |
| 248 | flags = RTA_DATA(tb[IFLA_VLAN_FLAGS]); |
| 249 | vlan_print_flags(f, flags->flags); |
| 250 | } |
| 251 | if (tb[IFLA_VLAN_INGRESS_QOS]) |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 252 | vlan_print_map(f, |
| 253 | "ingress_qos", |
| 254 | "ingress-qos-map", |
| 255 | tb[IFLA_VLAN_INGRESS_QOS]); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 256 | if (tb[IFLA_VLAN_EGRESS_QOS]) |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 257 | vlan_print_map(f, |
| 258 | "egress_qos", |
| 259 | "egress-qos-map", |
| 260 | tb[IFLA_VLAN_EGRESS_QOS]); |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 261 | } |
| 262 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 263 | static void vlan_print_help(struct link_util *lu, int argc, char **argv, |
Julien Fortin | 43bc20a | 2017-08-17 10:36:14 -0700 | [diff] [blame] | 264 | FILE *f) |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 265 | { |
| 266 | print_explain(f); |
| 267 | } |
| 268 | |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 269 | struct link_util vlan_link_util = { |
| 270 | .id = "vlan", |
| 271 | .maxattr = IFLA_VLAN_MAX, |
| 272 | .parse_opt = vlan_parse_opt, |
| 273 | .print_opt = vlan_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 274 | .print_help = vlan_print_help, |
Patrick McHardy | 5c302d5 | 2007-09-01 18:47:19 +0200 | [diff] [blame] | 275 | }; |