]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/iplink_ipvlan.c
ip: iplink: Convert to use parse_on_off()
[mirror_iproute2.git] / ip / iplink_ipvlan.c
CommitLineData
88272775 1/* iplink_ipvlan.c IPVLAN/IPVTAP device support
81eaf677
MB
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
7 *
8 * Authors: Mahesh Bandewar <maheshb@google.com>
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/socket.h>
15#include <linux/if_link.h>
16
17#include "rt_names.h"
18#include "utils.h"
19#include "ip_common.h"
20
88272775 21static void print_explain(struct link_util *lu, FILE *f)
81eaf677 22{
1ef5c952 23 fprintf(f,
88272775 24 "Usage: ... %s [ mode MODE ] [ FLAGS ]\n"
1ef5c952
MB
25 "\n"
26 "MODE: l3 | l3s | l2\n"
27 "FLAGS: bridge | private | vepa\n"
88272775
HL
28 "(first values are the defaults if nothing is specified).\n",
29 lu->id);
81eaf677
MB
30}
31
32static int ipvlan_parse_opt(struct link_util *lu, int argc, char **argv,
33 struct nlmsghdr *n)
34{
1ef5c952
MB
35 __u16 flags = 0;
36 bool mflag_given = false;
37
81eaf677
MB
38 while (argc > 0) {
39 if (matches(*argv, "mode") == 0) {
40 __u16 mode = 0;
56f5daac 41
81eaf677
MB
42 NEXT_ARG();
43
44 if (strcmp(*argv, "l2") == 0)
45 mode = IPVLAN_MODE_L2;
46 else if (strcmp(*argv, "l3") == 0)
47 mode = IPVLAN_MODE_L3;
b7c14880
MB
48 else if (strcmp(*argv, "l3s") == 0)
49 mode = IPVLAN_MODE_L3S;
50 else {
51 fprintf(stderr, "Error: argument of \"mode\" must be either \"l2\", \"l3\" or \"l3s\"\n");
52 return -1;
53 }
81eaf677 54 addattr16(n, 1024, IFLA_IPVLAN_MODE, mode);
1ef5c952
MB
55 } else if (matches(*argv, "private") == 0 && !mflag_given) {
56 flags |= IPVLAN_F_PRIVATE;
57 mflag_given = true;
58 } else if (matches(*argv, "vepa") == 0 && !mflag_given) {
59 flags |= IPVLAN_F_VEPA;
60 mflag_given = true;
61 } else if (matches(*argv, "bridge") == 0 && !mflag_given) {
62 mflag_given = true;
81eaf677 63 } else if (matches(*argv, "help") == 0) {
88272775 64 print_explain(lu, stderr);
81eaf677
MB
65 return -1;
66 } else {
88272775
HL
67 fprintf(stderr, "%s: unknown option \"%s\"?\n",
68 lu->id, *argv);
69 print_explain(lu, stderr);
81eaf677
MB
70 return -1;
71 }
b7c14880
MB
72 argc--;
73 argv++;
81eaf677 74 }
1ef5c952 75 addattr16(n, 1024, IFLA_IPVLAN_FLAGS, flags);
81eaf677
MB
76
77 return 0;
78}
79
80static void ipvlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
81{
82
83 if (!tb)
84 return;
85
86 if (tb[IFLA_IPVLAN_MODE]) {
87 if (RTA_PAYLOAD(tb[IFLA_IPVLAN_MODE]) == sizeof(__u16)) {
88 __u16 mode = rta_getattr_u16(tb[IFLA_IPVLAN_MODE]);
8f24afc9 89 const char *mode_str = mode == IPVLAN_MODE_L2 ? "l2" :
b7c14880 90 mode == IPVLAN_MODE_L3 ? "l3" :
8f24afc9
JF
91 mode == IPVLAN_MODE_L3S ? "l3s" : "unknown";
92
93 print_string(PRINT_ANY, "mode", " mode %s ", mode_str);
81eaf677
MB
94 }
95 }
1ef5c952
MB
96 if (tb[IFLA_IPVLAN_FLAGS]) {
97 if (RTA_PAYLOAD(tb[IFLA_IPVLAN_FLAGS]) == sizeof(__u16)) {
98 __u16 flags = rta_getattr_u16(tb[IFLA_IPVLAN_FLAGS]);
99
100 if (flags & IPVLAN_F_PRIVATE)
101 print_bool(PRINT_ANY, "private", "private ",
102 true);
103 else if (flags & IPVLAN_F_VEPA)
104 print_bool(PRINT_ANY, "vepa", "vepa ",
105 true);
106 else
107 print_bool(PRINT_ANY, "bridge", "bridge ",
108 true);
109 }
110 }
81eaf677
MB
111}
112
113static void ipvlan_print_help(struct link_util *lu, int argc, char **argv,
114 FILE *f)
115{
88272775 116 print_explain(lu, f);
81eaf677
MB
117}
118
119struct link_util ipvlan_link_util = {
120 .id = "ipvlan",
121 .maxattr = IFLA_IPVLAN_MAX,
122 .parse_opt = ipvlan_parse_opt,
123 .print_opt = ipvlan_print_opt,
124 .print_help = ipvlan_print_help,
125};
88272775
HL
126
127struct link_util ipvtap_link_util = {
128 .id = "ipvtap",
129 .maxattr = IFLA_IPVLAN_MAX,
130 .parse_opt = ipvlan_parse_opt,
131 .print_opt = ipvlan_print_opt,
132 .print_help = ipvlan_print_help,
133};