Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 1 | /* |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 2 | * iplink_macvlan.c macvlan/macvtap device support |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 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 | * Arnd Bergmann <arnd@arndb.de> |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <linux/if_link.h> |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 18 | #include <linux/if_ether.h> |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 19 | |
| 20 | #include "rt_names.h" |
| 21 | #include "utils.h" |
| 22 | #include "ip_common.h" |
| 23 | |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 24 | #define pfx_err(lu, ...) { \ |
| 25 | fprintf(stderr, "%s: ", lu->id); \ |
| 26 | fprintf(stderr, __VA_ARGS__); \ |
| 27 | fprintf(stderr, "\n"); \ |
| 28 | } |
| 29 | |
| 30 | static void print_explain(struct link_util *lu, FILE *f) |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 31 | { |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 32 | fprintf(f, |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 33 | "Usage: ... %s mode MODE [flag MODE_FLAG] MODE_OPTS\n" |
| 34 | "MODE: private | vepa | bridge | passthru | source\n" |
| 35 | "MODE_FLAG: null | nopromisc | unicast | unicast_all\n" |
| 36 | "MODE_OPTS: for mode \"source\":\n" |
| 37 | "\tmacaddr { add <macaddr> | del <macaddr> | flush }\n", |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 38 | lu->id |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 39 | ); |
| 40 | } |
| 41 | |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 42 | static void explain(struct link_util *lu) |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 43 | { |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 44 | print_explain(lu, stderr); |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 45 | } |
| 46 | |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 47 | static int mode_arg(const char *arg) |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 48 | { |
Stephen Hemminger | 7409334 | 2016-10-12 15:23:27 -0700 | [diff] [blame^] | 49 | fprintf(stderr, |
| 50 | "Error: argument of \"mode\" must be \"private\", \"vepa\", \"bridge\", \"passthru\" or \"source\", not \"%s\"\n", |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 51 | arg); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | static int flag_arg(const char *arg) |
| 56 | { |
Stephen Hemminger | 7409334 | 2016-10-12 15:23:27 -0700 | [diff] [blame^] | 57 | fprintf(stderr, |
| 58 | "Error: argument of \"flag\" must be \"nopromisc\", \"unicast\", \"unicast_all\" or \"null\", not \"%s\"\n", |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 59 | arg); |
| 60 | return -1; |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int macvlan_parse_opt(struct link_util *lu, int argc, char **argv, |
| 64 | struct nlmsghdr *n) |
| 65 | { |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 66 | __u32 mode = 0; |
| 67 | __u16 flags = 0; |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 68 | __u32 mac_mode = 0; |
| 69 | int len = 0; |
| 70 | char abuf[32]; |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 71 | |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 72 | while (argc > 0) { |
| 73 | if (matches(*argv, "mode") == 0) { |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 74 | NEXT_ARG(); |
| 75 | |
| 76 | if (strcmp(*argv, "private") == 0) |
| 77 | mode = MACVLAN_MODE_PRIVATE; |
| 78 | else if (strcmp(*argv, "vepa") == 0) |
| 79 | mode = MACVLAN_MODE_VEPA; |
| 80 | else if (strcmp(*argv, "bridge") == 0) |
| 81 | mode = MACVLAN_MODE_BRIDGE; |
Sridhar Samudrala | f0612d5 | 2011-03-16 17:01:58 -0700 | [diff] [blame] | 82 | else if (strcmp(*argv, "passthru") == 0) |
| 83 | mode = MACVLAN_MODE_PASSTHRU; |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 84 | else if (strcmp(*argv, "source") == 0) |
| 85 | mode = MACVLAN_MODE_SOURCE; |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 86 | else |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 87 | return mode_arg(*argv); |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 88 | } else if (matches(*argv, "flag") == 0) { |
| 89 | NEXT_ARG(); |
| 90 | |
| 91 | if (strcmp(*argv, "nopromisc") == 0) |
| 92 | flags |= MACVLAN_FLAG_NOPROMISC; |
| 93 | else if (strcmp(*argv, "unicast") == 0) |
| 94 | flags |= MACVLAN_FLAG_UNICAST; |
| 95 | else if (strcmp(*argv, "unicast_all") == 0) |
| 96 | flags |= MACVLAN_FLAG_UNICAST_ALL; |
| 97 | else if (strcmp(*argv, "null") == 0) |
| 98 | flags |= 0; |
| 99 | else |
| 100 | return flag_arg(*argv); |
| 101 | |
| 102 | } else if (matches(*argv, "macaddr") == 0) { |
| 103 | NEXT_ARG(); |
| 104 | |
| 105 | if (strcmp(*argv, "add") == 0) { |
| 106 | mac_mode = MACVLAN_MACADDR_ADD; |
| 107 | } else if (strcmp(*argv, "del") == 0) { |
| 108 | mac_mode = MACVLAN_MACADDR_DEL; |
| 109 | } else if (strcmp(*argv, "flush") == 0) { |
| 110 | mac_mode = MACVLAN_MACADDR_FLUSH; |
| 111 | } else { |
| 112 | explain(lu); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | if (mac_mode != MACVLAN_MACADDR_FLUSH) { |
| 117 | NEXT_ARG(); |
| 118 | |
| 119 | len = ll_addr_a2n(abuf, sizeof(abuf), *argv); |
| 120 | if (len < 0) |
| 121 | return -1; |
| 122 | } |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 123 | } else if (matches(*argv, "nopromisc") == 0) { |
| 124 | flags |= MACVLAN_FLAG_NOPROMISC; |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 125 | } else if (matches(*argv, "help") == 0) { |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 126 | explain(lu); |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 127 | return -1; |
| 128 | } else { |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 129 | pfx_err(lu, "unknown option \"%s\"?", *argv); |
| 130 | explain(lu); |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 131 | return -1; |
| 132 | } |
| 133 | argc--, argv++; |
| 134 | } |
| 135 | |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 136 | if (mode) |
| 137 | addattr32(n, 1024, IFLA_MACVLAN_MODE, mode); |
| 138 | |
| 139 | if (flags) { |
| 140 | if (flags & MACVLAN_FLAG_NOPROMISC && |
| 141 | mode != MACVLAN_MODE_PASSTHRU) { |
| 142 | pfx_err(lu, "nopromisc flag only valid in passthru mode"); |
| 143 | explain(lu); |
| 144 | return -1; |
| 145 | } |
| 146 | addattr16(n, 1024, IFLA_MACVLAN_FLAGS, flags); |
| 147 | } |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 148 | |
| 149 | if (mac_mode) { |
| 150 | addattr32(n, 1024, IFLA_MACVLAN_MACADDR_MODE, mac_mode); |
| 151 | if (mac_mode != MACVLAN_MACADDR_FLUSH && len > 0) |
| 152 | addattr_l(n, 1024, IFLA_MACVLAN_MACADDR, abuf, len); |
| 153 | } |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static void macvlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 158 | { |
| 159 | __u32 mode; |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 160 | __u16 flags; |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 161 | __u32 count; |
| 162 | unsigned char *addr; |
| 163 | int len; |
| 164 | struct rtattr *rta; |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 165 | |
| 166 | if (!tb) |
| 167 | return; |
| 168 | |
| 169 | if (!tb[IFLA_MACVLAN_MODE] || |
| 170 | RTA_PAYLOAD(tb[IFLA_MACVLAN_MODE]) < sizeof(__u32)) |
| 171 | return; |
| 172 | |
Lutz Jaenicke | 7dc0481 | 2013-08-29 09:50:28 +0200 | [diff] [blame] | 173 | mode = rta_getattr_u32(tb[IFLA_MACVLAN_MODE]); |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 174 | fprintf(f, " mode %s ", |
| 175 | mode == MACVLAN_MODE_PRIVATE ? "private" |
| 176 | : mode == MACVLAN_MODE_VEPA ? "vepa" |
| 177 | : mode == MACVLAN_MODE_BRIDGE ? "bridge" |
Sridhar Samudrala | f0612d5 | 2011-03-16 17:01:58 -0700 | [diff] [blame] | 178 | : mode == MACVLAN_MODE_PASSTHRU ? "passthru" |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 179 | : mode == MACVLAN_MODE_SOURCE ? "source" |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 180 | : "unknown"); |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 181 | |
| 182 | if (!tb[IFLA_MACVLAN_FLAGS] || |
| 183 | RTA_PAYLOAD(tb[IFLA_MACVLAN_FLAGS]) < sizeof(__u16)) |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 184 | flags = 0; |
| 185 | else |
| 186 | flags = rta_getattr_u16(tb[IFLA_MACVLAN_FLAGS]); |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 187 | |
Phil Sutter | 3cf8ba5 | 2015-09-25 14:09:50 +0200 | [diff] [blame] | 188 | if (flags & MACVLAN_FLAG_NOPROMISC) |
| 189 | fprintf(f, "nopromisc "); |
michael-dev@fami-braun.de | f33b727 | 2016-09-25 21:08:55 +0200 | [diff] [blame] | 190 | if (flags & MACVLAN_FLAG_UNICAST) |
| 191 | fprintf(f, "flag unicast "); |
| 192 | if (flags & MACVLAN_FLAG_UNICAST_ALL) |
| 193 | fprintf(f, "flag unicast_all "); |
| 194 | |
| 195 | /* in source mode, there are more options to print */ |
| 196 | |
| 197 | if (mode != MACVLAN_MODE_SOURCE) |
| 198 | return; |
| 199 | |
| 200 | if (!tb[IFLA_MACVLAN_MACADDR_COUNT] || |
| 201 | RTA_PAYLOAD(tb[IFLA_MACVLAN_MACADDR_COUNT]) < sizeof(__u32)) |
| 202 | return; |
| 203 | |
| 204 | count = rta_getattr_u32(tb[IFLA_MACVLAN_MACADDR_COUNT]); |
| 205 | fprintf(f, " remotes (%d)", count); |
| 206 | |
| 207 | if (!tb[IFLA_MACVLAN_MACADDR_DATA]) |
| 208 | return; |
| 209 | |
| 210 | rta = RTA_DATA(tb[IFLA_MACVLAN_MACADDR_DATA]); |
| 211 | len = RTA_PAYLOAD(tb[IFLA_MACVLAN_MACADDR_DATA]); |
| 212 | |
| 213 | for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { |
| 214 | if (rta->rta_type != IFLA_MACVLAN_MACADDR || |
| 215 | RTA_PAYLOAD(rta) < 6) |
| 216 | continue; |
| 217 | addr = RTA_DATA(rta); |
| 218 | fprintf(f, " %.2x:%.2x:%.2x:%.2x:%.2x:%.2x", addr[0], |
| 219 | addr[1], addr[2], addr[3], addr[4], addr[5]); |
| 220 | } |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 221 | } |
| 222 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 223 | static void macvlan_print_help(struct link_util *lu, int argc, char **argv, |
| 224 | FILE *f) |
| 225 | { |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 226 | print_explain(lu, f); |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 227 | } |
| 228 | |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 229 | struct link_util macvlan_link_util = { |
| 230 | .id = "macvlan", |
| 231 | .maxattr = IFLA_MACVLAN_MAX, |
| 232 | .parse_opt = macvlan_parse_opt, |
| 233 | .print_opt = macvlan_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 234 | .print_help = macvlan_print_help, |
Arnd Bergmann | d63a9b2 | 2009-12-26 11:22:57 -0800 | [diff] [blame] | 235 | }; |
Phil Sutter | 541f1b3 | 2015-09-25 14:09:49 +0200 | [diff] [blame] | 236 | |
| 237 | struct link_util macvtap_link_util = { |
| 238 | .id = "macvtap", |
| 239 | .maxattr = IFLA_MACVLAN_MAX, |
| 240 | .parse_opt = macvlan_parse_opt, |
| 241 | .print_opt = macvlan_print_opt, |
| 242 | .print_help = macvlan_print_help, |
| 243 | }; |