2 * iplink_macvlan.c macvlan/macvtap device support
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.
9 * Authors: Patrick McHardy <kaber@trash.net>
10 * Arnd Bergmann <arnd@arndb.de>
16 #include <sys/socket.h>
17 #include <linux/if_link.h>
21 #include "ip_common.h"
23 #define pfx_err(lu, ...) { \
24 fprintf(stderr, "%s: ", lu->id); \
25 fprintf(stderr, __VA_ARGS__); \
26 fprintf(stderr, "\n"); \
29 static void print_explain(struct link_util
*lu
, FILE *f
)
32 "Usage: ... %s mode { private | vepa | bridge | passthru [nopromisc] }\n",
37 static void explain(struct link_util
*lu
)
39 print_explain(lu
, stderr
);
42 static int mode_arg(const char *arg
)
44 fprintf(stderr
, "Error: argument of \"mode\" must be \"private\", "
45 "\"vepa\", \"bridge\" or \"passthru\", not \"%s\"\n", arg
);
49 static int macvlan_parse_opt(struct link_util
*lu
, int argc
, char **argv
,
56 if (matches(*argv
, "mode") == 0) {
59 if (strcmp(*argv
, "private") == 0)
60 mode
= MACVLAN_MODE_PRIVATE
;
61 else if (strcmp(*argv
, "vepa") == 0)
62 mode
= MACVLAN_MODE_VEPA
;
63 else if (strcmp(*argv
, "bridge") == 0)
64 mode
= MACVLAN_MODE_BRIDGE
;
65 else if (strcmp(*argv
, "passthru") == 0)
66 mode
= MACVLAN_MODE_PASSTHRU
;
68 return mode_arg(*argv
);
69 } else if (matches(*argv
, "nopromisc") == 0) {
70 flags
|= MACVLAN_FLAG_NOPROMISC
;
71 } else if (matches(*argv
, "help") == 0) {
75 pfx_err(lu
, "unknown option \"%s\"?", *argv
);
83 addattr32(n
, 1024, IFLA_MACVLAN_MODE
, mode
);
86 if (flags
& MACVLAN_FLAG_NOPROMISC
&&
87 mode
!= MACVLAN_MODE_PASSTHRU
) {
88 pfx_err(lu
, "nopromisc flag only valid in passthru mode");
92 addattr16(n
, 1024, IFLA_MACVLAN_FLAGS
, flags
);
97 static void macvlan_print_opt(struct link_util
*lu
, FILE *f
, struct rtattr
*tb
[])
105 if (!tb
[IFLA_MACVLAN_MODE
] ||
106 RTA_PAYLOAD(tb
[IFLA_MACVLAN_MODE
]) < sizeof(__u32
))
109 mode
= rta_getattr_u32(tb
[IFLA_MACVLAN_MODE
]);
110 fprintf(f
, " mode %s ",
111 mode
== MACVLAN_MODE_PRIVATE
? "private"
112 : mode
== MACVLAN_MODE_VEPA
? "vepa"
113 : mode
== MACVLAN_MODE_BRIDGE
? "bridge"
114 : mode
== MACVLAN_MODE_PASSTHRU
? "passthru"
117 if (!tb
[IFLA_MACVLAN_FLAGS
] ||
118 RTA_PAYLOAD(tb
[IFLA_MACVLAN_FLAGS
]) < sizeof(__u16
))
121 flags
= rta_getattr_u16(tb
[IFLA_MACVLAN_FLAGS
]);
122 if (flags
& MACVLAN_FLAG_NOPROMISC
)
123 fprintf(f
, "nopromisc ");
126 static void macvlan_print_help(struct link_util
*lu
, int argc
, char **argv
,
129 print_explain(lu
, f
);
132 struct link_util macvlan_link_util
= {
134 .maxattr
= IFLA_MACVLAN_MAX
,
135 .parse_opt
= macvlan_parse_opt
,
136 .print_opt
= macvlan_print_opt
,
137 .print_help
= macvlan_print_help
,
140 struct link_util macvtap_link_util
= {
142 .maxattr
= IFLA_MACVLAN_MAX
,
143 .parse_opt
= macvlan_parse_opt
,
144 .print_opt
= macvlan_print_opt
,
145 .print_help
= macvlan_print_help
,