]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/iplink_geneve.c
tc: code cleanup
[mirror_iproute2.git] / ip / iplink_geneve.c
CommitLineData
908755dc
JL
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
f4739b2e 14#include "rt_names.h"
908755dc
JL
15#include "utils.h"
16#include "ip_common.h"
17
18static void print_explain(FILE *f)
19{
20 fprintf(f, "Usage: ... geneve id VNI remote ADDR\n");
f4739b2e 21 fprintf(f, " [ ttl TTL ] [ tos TOS ]\n");
9450c5ec 22 fprintf(f, " [ dstport PORT ] [ [no]external ]\n");
908755dc
JL
23 fprintf(f, "\n");
24 fprintf(f, "Where: VNI := 0-16777215\n");
25 fprintf(f, " ADDR := IP_ADDRESS\n");
f4739b2e 26 fprintf(f, " TOS := { NUMBER | inherit }\n");
f4c05c2e 27 fprintf(f, " TTL := { 1..255 | inherit }\n");
908755dc
JL
28}
29
30static void explain(void)
31{
32 print_explain(stderr);
33}
34
35static 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;
f4c05c2e 42 __u8 ttl = 0;
f4739b2e 43 __u8 tos = 0;
9450c5ec
PA
44 __u16 dstport = 0;
45 bool metadata = 0;
908755dc
JL
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 }
906ac543 61 if (IN6_IS_ADDR_MULTICAST(&daddr6) || IN_MULTICAST(ntohl(daddr)))
908755dc 62 invarg("invalid remote address", *argv);
f4c05c2e
JL
63 } else if (!matches(*argv, "ttl") ||
64 !matches(*argv, "hoplimit")) {
65 unsigned 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 }
f4739b2e
JL
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;
9450c5ec
PA
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;
908755dc
JL
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
9450c5ec
PA
105 if (metadata && vni_set) {
106 fprintf(stderr, "geneve: both 'external' and vni cannot be specified\n");
908755dc
JL
107 return -1;
108 }
109
9450c5ec
PA
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 }
908755dc
JL
121 }
122
123 addattr32(n, 1024, IFLA_GENEVE_ID, vni);
124 if (daddr)
125 addattr_l(n, 1024, IFLA_GENEVE_REMOTE, &daddr, 4);
906ac543
JL
126 if (memcmp(&daddr6, &in6addr_any, sizeof(daddr6)) != 0)
127 addattr_l(n, 1024, IFLA_GENEVE_REMOTE6, &daddr6, sizeof(struct in6_addr));
f4c05c2e 128 addattr8(n, 1024, IFLA_GENEVE_TTL, ttl);
f4739b2e 129 addattr8(n, 1024, IFLA_GENEVE_TOS, tos);
9450c5ec
PA
130 if (dstport)
131 addattr16(n, 1024, IFLA_GENEVE_PORT, htons(dstport));
132 if (metadata)
133 addattr(n, 1024, IFLA_GENEVE_COLLECT_METADATA);
908755dc
JL
134
135 return 0;
136}
137
138static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
139{
140 __u32 vni;
141 char s1[1024];
f4739b2e 142 __u8 tos;
908755dc
JL
143
144 if (!tb)
145 return;
146
147 if (!tb[IFLA_GENEVE_ID] ||
148 RTA_PAYLOAD(tb[IFLA_GENEVE_ID]) < sizeof(__u32))
149 return;
150
151 vni = rta_getattr_u32(tb[IFLA_GENEVE_ID]);
152 fprintf(f, "id %u ", vni);
153
154 if (tb[IFLA_GENEVE_REMOTE]) {
155 __be32 addr = rta_getattr_u32(tb[IFLA_GENEVE_REMOTE]);
156 if (addr)
157 fprintf(f, "remote %s ",
158 format_host(AF_INET, 4, &addr, s1, sizeof(s1)));
906ac543
JL
159 } else if (tb[IFLA_GENEVE_REMOTE6]) {
160 struct in6_addr addr;
161 memcpy(&addr, RTA_DATA(tb[IFLA_GENEVE_REMOTE6]), sizeof(struct in6_addr));
162 if (memcmp(&addr, &in6addr_any, sizeof(addr)) != 0) {
163 if (IN6_IS_ADDR_MULTICAST(&addr))
164 fprintf(f, "remote %s ",
165 format_host(AF_INET6, sizeof(struct in6_addr), &addr, s1, sizeof(s1)));
166 }
908755dc 167 }
f4c05c2e
JL
168
169 if (tb[IFLA_GENEVE_TTL]) {
170 __u8 ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]);
171 if (ttl)
172 fprintf(f, "ttl %d ", ttl);
173 }
f4739b2e
JL
174
175 if (tb[IFLA_GENEVE_TOS] &&
176 (tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]))) {
177 if (tos == 1)
178 fprintf(f, "tos inherit ");
179 else
180 fprintf(f, "tos %#x ", tos);
181 }
9450c5ec
PA
182
183 if (tb[IFLA_GENEVE_PORT])
184 fprintf(f, "dstport %u ",
185 ntohs(rta_getattr_u16(tb[IFLA_GENEVE_PORT])));
186
187 if (tb[IFLA_GENEVE_COLLECT_METADATA])
188 fputs("external ", f);
189
908755dc
JL
190}
191
192static void geneve_print_help(struct link_util *lu, int argc, char **argv,
193 FILE *f)
194{
195 print_explain(f);
196}
197
198struct link_util geneve_link_util = {
199 .id = "geneve",
200 .maxattr = IFLA_GENEVE_MAX,
201 .parse_opt = geneve_parse_opt,
202 .print_opt = geneve_print_opt,
203 .print_help = geneve_print_help,
204};