]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_geneve.c
make format_host non-reentrant by default
[mirror_iproute2.git] / ip / iplink_geneve.c
1 /*
2 * iplink_geneve.c GENEVE 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: John W. Linville <linville@tuxdriver.com>
10 */
11
12 #include <stdio.h>
13
14 #include "rt_names.h"
15 #include "utils.h"
16 #include "ip_common.h"
17
18 static void print_explain(FILE *f)
19 {
20 fprintf(f, "Usage: ... geneve id VNI remote ADDR\n");
21 fprintf(f, " [ ttl TTL ] [ tos TOS ]\n");
22 fprintf(f, " [ dstport PORT ] [ [no]external ]\n");
23 fprintf(f, "\n");
24 fprintf(f, "Where: VNI := 0-16777215\n");
25 fprintf(f, " ADDR := IP_ADDRESS\n");
26 fprintf(f, " TOS := { NUMBER | inherit }\n");
27 fprintf(f, " TTL := { 1..255 | inherit }\n");
28 }
29
30 static void explain(void)
31 {
32 print_explain(stderr);
33 }
34
35 static int geneve_parse_opt(struct link_util *lu, int argc, char **argv,
36 struct nlmsghdr *n)
37 {
38 __u32 vni = 0;
39 int vni_set = 0;
40 __u32 daddr = 0;
41 struct in6_addr daddr6 = IN6ADDR_ANY_INIT;
42 __u8 ttl = 0;
43 __u8 tos = 0;
44 __u16 dstport = 0;
45 bool metadata = 0;
46
47 while (argc > 0) {
48 if (!matches(*argv, "id") ||
49 !matches(*argv, "vni")) {
50 NEXT_ARG();
51 if (get_u32(&vni, *argv, 0) ||
52 vni >= 1u << 24)
53 invarg("invalid id", *argv);
54 vni_set = 1;
55 } else if (!matches(*argv, "remote")) {
56 NEXT_ARG();
57 if (!inet_get_addr(*argv, &daddr, &daddr6)) {
58 fprintf(stderr, "Invalid address \"%s\"\n", *argv);
59 return -1;
60 }
61 if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
62 invarg("invalid remote address", *argv);
63 } else if (!matches(*argv, "ttl") ||
64 !matches(*argv, "hoplimit")) {
65 unsigned int uval;
66
67 NEXT_ARG();
68 if (strcmp(*argv, "inherit") != 0) {
69 if (get_unsigned(&uval, *argv, 0))
70 invarg("invalid TTL", *argv);
71 if (uval > 255)
72 invarg("TTL must be <= 255", *argv);
73 ttl = uval;
74 }
75 } else if (!matches(*argv, "tos") ||
76 !matches(*argv, "dsfield")) {
77 __u32 uval;
78
79 NEXT_ARG();
80 if (strcmp(*argv, "inherit") != 0) {
81 if (rtnl_dsfield_a2n(&uval, *argv))
82 invarg("bad TOS value", *argv);
83 tos = uval;
84 } else
85 tos = 1;
86 } else if (!matches(*argv, "dstport")) {
87 NEXT_ARG();
88 if (get_u16(&dstport, *argv, 0))
89 invarg("dstport", *argv);
90 } else if (!matches(*argv, "external")) {
91 metadata = true;
92 } else if (!matches(*argv, "noexternal")) {
93 metadata = false;
94 } else if (matches(*argv, "help") == 0) {
95 explain();
96 return -1;
97 } else {
98 fprintf(stderr, "geneve: unknown command \"%s\"?\n", *argv);
99 explain();
100 return -1;
101 }
102 argc--, argv++;
103 }
104
105 if (metadata && vni_set) {
106 fprintf(stderr, "geneve: both 'external' and vni cannot be specified\n");
107 return -1;
108 }
109
110 if (!metadata) {
111 /* parameter checking make sense only for full geneve tunnels */
112 if (!vni_set) {
113 fprintf(stderr, "geneve: missing virtual network identifier\n");
114 return -1;
115 }
116
117 if (!daddr && memcmp(&daddr6, &in6addr_any, sizeof(daddr6)) == 0) {
118 fprintf(stderr, "geneve: remote link partner not specified\n");
119 return -1;
120 }
121 }
122
123 addattr32(n, 1024, IFLA_GENEVE_ID, vni);
124 if (daddr)
125 addattr_l(n, 1024, IFLA_GENEVE_REMOTE, &daddr, 4);
126 if (memcmp(&daddr6, &in6addr_any, sizeof(daddr6)) != 0)
127 addattr_l(n, 1024, IFLA_GENEVE_REMOTE6, &daddr6, sizeof(struct in6_addr));
128 addattr8(n, 1024, IFLA_GENEVE_TTL, ttl);
129 addattr8(n, 1024, IFLA_GENEVE_TOS, tos);
130 if (dstport)
131 addattr16(n, 1024, IFLA_GENEVE_PORT, htons(dstport));
132 if (metadata)
133 addattr(n, 1024, IFLA_GENEVE_COLLECT_METADATA);
134
135 return 0;
136 }
137
138 static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
139 {
140 __u32 vni;
141 __u8 tos;
142
143 if (!tb)
144 return;
145
146 if (!tb[IFLA_GENEVE_ID] ||
147 RTA_PAYLOAD(tb[IFLA_GENEVE_ID]) < sizeof(__u32))
148 return;
149
150 vni = rta_getattr_u32(tb[IFLA_GENEVE_ID]);
151 fprintf(f, "id %u ", vni);
152
153 if (tb[IFLA_GENEVE_REMOTE]) {
154 __be32 addr = rta_getattr_u32(tb[IFLA_GENEVE_REMOTE]);
155
156 if (addr)
157 fprintf(f, "remote %s ",
158 format_host(AF_INET, 4, &addr));
159 } else if (tb[IFLA_GENEVE_REMOTE6]) {
160 struct in6_addr addr;
161
162 memcpy(&addr, RTA_DATA(tb[IFLA_GENEVE_REMOTE6]), sizeof(struct in6_addr));
163 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
164 if (IN6_IS_ADDR_MULTICAST(&addr))
165 fprintf(f, "remote %s ",
166 format_host(AF_INET6, sizeof(struct in6_addr), &addr));
167 }
168 }
169
170 if (tb[IFLA_GENEVE_TTL]) {
171 __u8 ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]);
172
173 if (ttl)
174 fprintf(f, "ttl %d ", ttl);
175 }
176
177 if (tb[IFLA_GENEVE_TOS] &&
178 (tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]))) {
179 if (tos == 1)
180 fprintf(f, "tos inherit ");
181 else
182 fprintf(f, "tos %#x ", tos);
183 }
184
185 if (tb[IFLA_GENEVE_PORT])
186 fprintf(f, "dstport %u ",
187 ntohs(rta_getattr_u16(tb[IFLA_GENEVE_PORT])));
188
189 if (tb[IFLA_GENEVE_COLLECT_METADATA])
190 fputs("external ", f);
191
192 }
193
194 static void geneve_print_help(struct link_util *lu, int argc, char **argv,
195 FILE *f)
196 {
197 print_explain(f);
198 }
199
200 struct link_util geneve_link_util = {
201 .id = "geneve",
202 .maxattr = IFLA_GENEVE_MAX,
203 .parse_opt = geneve_parse_opt,
204 .print_opt = geneve_print_opt,
205 .print_help = geneve_print_help,
206 };