1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008 Andrew Thompson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/sockio.h>
32 #include <net/if.h>
33 #include <net/if_gre.h>
34
35 #include <ctype.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <err.h>
41
42 #include "ifconfig.h"
43
44 #ifndef WITHOUT_NETLINK
45 #include "ifconfig_netlink.h"
46 #endif
47
48 static const char *GREBITS[] = {
49 [0] = "ENABLE_CSUM",
50 [1] = "ENABLE_SEQ",
51 [2] = "UDPENCAP",
52 };
53
54 #ifndef WITHOUT_NETLINK
55 struct nl_parsed_gre {
56 uint32_t ifla_flags;
57 uint32_t ifla_okey;
58 uint32_t ifla_encap_type;
59 uint16_t ifla_encap_sport;
60 };
61
62 struct nla_gre_info {
63 const char *kind;
64 struct nl_parsed_gre data;
65 };
66
67 struct nla_gre_link {
68 uint32_t ifi_index;
69 struct nla_gre_info linkinfo;
70 };
71
72 static inline void
gre_nl_init(if_ctx * ctx,struct snl_writer * nw,uint32_t flags)73 gre_nl_init(if_ctx *ctx, struct snl_writer *nw, uint32_t flags)
74 {
75 struct nlmsghdr *hdr;
76
77 snl_init_writer(ctx->io_ss, nw);
78 hdr = snl_create_msg_request(nw, NL_RTM_NEWLINK);
79 hdr->nlmsg_flags |= flags;
80 snl_reserve_msg_object(nw, struct ifinfomsg);
81 snl_add_msg_attr_string(nw, IFLA_IFNAME, ctx->ifname);
82 }
83
84 static inline void
gre_nl_fini(if_ctx * ctx,struct snl_writer * nw)85 gre_nl_fini(if_ctx *ctx, struct snl_writer *nw)
86 {
87 struct nlmsghdr *hdr;
88 struct snl_errmsg_data errmsg = {};
89
90 hdr = snl_finalize_msg(nw);
91 if (hdr == NULL || !snl_send_message(ctx->io_ss, hdr))
92 err(1, "unable to send netlink message");
93
94 if (!snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &errmsg))
95 errx(errmsg.error, "%s", errmsg.error_str);
96 }
97
98 #define _OUT(_field) offsetof(struct nl_parsed_gre, _field)
99 static const struct snl_attr_parser nla_p_gre[] = {
100 { .type = IFLA_GRE_FLAGS, .off = _OUT(ifla_flags), .cb = snl_attr_get_uint32 },
101 { .type = IFLA_GRE_OKEY, .off = _OUT(ifla_okey), .cb = snl_attr_get_uint32 },
102 { .type = IFLA_GRE_ENCAP_TYPE, .off = _OUT(ifla_encap_type),
103 .cb = snl_attr_get_uint32 },
104 { .type = IFLA_GRE_ENCAP_SPORT, .off = _OUT(ifla_encap_sport),
105 .cb = snl_attr_get_uint16 },
106 };
107 #undef _OUT
108 SNL_DECLARE_ATTR_PARSER(gre_linkinfo_data_parser, nla_p_gre);
109
110 #define _OUT(_field) offsetof(struct nla_gre_info, _field)
111 static const struct snl_attr_parser ap_gre_linkinfo[] = {
112 { .type = IFLA_INFO_KIND, .off = _OUT(kind), .cb = snl_attr_get_string },
113 { .type = IFLA_INFO_DATA, .off = _OUT(data),
114 .arg = &gre_linkinfo_data_parser, .cb = snl_attr_get_nested },
115 };
116 #undef _OUT
117 SNL_DECLARE_ATTR_PARSER(gre_linkinfo_parser, ap_gre_linkinfo);
118
119 #define _IN(_field) offsetof(struct ifinfomsg, _field)
120 #define _OUT(_field) offsetof(struct nla_gre_link, _field)
121 static const struct snl_attr_parser ap_gre_link[] = {
122 { .type = IFLA_LINKINFO, .off = _OUT(linkinfo),
123 .arg = &gre_linkinfo_parser, .cb = snl_attr_get_nested },
124 };
125
126 static const struct snl_field_parser fp_geneve_link[] = {
127 { .off_in = _IN(ifi_index), .off_out = _OUT(ifi_index),
128 .cb = snl_field_get_uint32 },
129 };
130 #undef _IN
131 #undef _OUT
132 SNL_DECLARE_PARSER(gre_parser, struct ifinfomsg, fp_geneve_link, ap_gre_link);
133
134 static const struct snl_hdr_parser *all_parsers[] = {
135 &gre_linkinfo_data_parser,
136 &gre_linkinfo_parser,
137 &gre_parser,
138 };
139
140 static void
gre_status_nl(if_ctx * ctx)141 gre_status_nl(if_ctx *ctx)
142 {
143 struct snl_writer nw = {};
144 struct nlmsghdr *hdr;
145 struct snl_errmsg_data errmsg = {};
146 struct nla_gre_link gre_link = {};
147
148 if (strncmp(ctx->ifname, "gre", sizeof("gre") - 1) != 0)
149 return;
150
151 snl_init_writer(ctx->io_ss, &nw);
152 hdr = snl_create_msg_request(&nw, NL_RTM_GETLINK);
153 hdr->nlmsg_flags |= NLM_F_DUMP;
154 snl_reserve_msg_object(&nw, struct ifinfomsg);
155 snl_add_msg_attr_string(&nw, IFLA_IFNAME, ctx->ifname);
156
157 hdr = snl_finalize_msg(&nw);
158 if (hdr == NULL || !snl_send_message(ctx->io_ss, hdr))
159 err(1, "unable to send netlink message");
160
161 hdr = snl_read_reply(ctx->io_ss, hdr->nlmsg_seq);
162 if (hdr->nlmsg_type != NL_RTM_NEWLINK) {
163 if (!snl_parse_errmsg(ctx->io_ss, hdr, &errmsg))
164 errx(EINVAL, "(NETLINK)");
165 if (errmsg.error_str != NULL)
166 errx(errmsg.error, "(NETLINK) %s", errmsg.error_str);
167 }
168
169 if (!snl_parse_nlmsg(ctx->io_ss, hdr, &gre_parser, &gre_link))
170 return;
171
172 struct nla_gre_info gre_info = gre_link.linkinfo;
173 struct nl_parsed_gre gre_data = gre_info.data;
174
175 if (gre_data.ifla_okey != 0)
176 printf("\tgrekey: 0x%x (%u)\n",
177 gre_data.ifla_okey, gre_data.ifla_okey);
178
179 if (gre_data.ifla_flags == 0)
180 return;
181
182 if (gre_data.ifla_encap_sport != 0)
183 printf("\tudpport: %u\n", gre_data.ifla_encap_sport);
184
185 printf("\toptions=%x", gre_data.ifla_flags);
186 print_bits("options", &gre_data.ifla_flags, 1, GREBITS, nitems(GREBITS));
187 putchar('\n');
188 }
189
190 static void
setifgrekey_nl(if_ctx * ctx,const char * val,int dummy __unused)191 setifgrekey_nl(if_ctx *ctx, const char *val, int dummy __unused)
192 {
193 struct snl_writer nw = {};
194 int off, off2;
195 uint32_t grekey = strtol(val, NULL, 0);
196
197 gre_nl_init(ctx, &nw, 0);
198 off = snl_add_msg_attr_nested(&nw, IFLA_LINKINFO);
199 snl_add_msg_attr_string(&nw, IFLA_INFO_KIND, "gre");
200
201 off2 = snl_add_msg_attr_nested(&nw, IFLA_INFO_DATA);
202 snl_add_msg_attr_u32(&nw, IFLA_GRE_OKEY, grekey);
203
204 snl_end_attr_nested(&nw, off2);
205 snl_end_attr_nested(&nw, off);
206
207 gre_nl_fini(ctx, &nw);
208 }
209
210 static void
setifgreport_nl(if_ctx * ctx,const char * val,int dummy __unused)211 setifgreport_nl(if_ctx *ctx, const char *val, int dummy __unused)
212 {
213 struct snl_writer nw = {};
214 int off, off2;
215 uint16_t greport = strtol(val, NULL, 0);
216
217 gre_nl_init(ctx, &nw, 0);
218 off = snl_add_msg_attr_nested(&nw, IFLA_LINKINFO);
219 snl_add_msg_attr_string(&nw, IFLA_INFO_KIND, "gre");
220
221 off2 = snl_add_msg_attr_nested(&nw, IFLA_INFO_DATA);
222 snl_add_msg_attr_u16(&nw, IFLA_GRE_ENCAP_SPORT, greport);
223
224 snl_end_attr_nested(&nw, off2);
225 snl_end_attr_nested(&nw, off);
226
227 gre_nl_fini(ctx, &nw);
228 }
229
230 static void
setifgreopts_nl(if_ctx * ctx,const char * val __unused,int d)231 setifgreopts_nl(if_ctx *ctx, const char *val __unused, int d)
232 {
233 struct snl_writer nw = {};
234 struct nlmsghdr *hdr;
235 struct snl_errmsg_data errmsg;
236 struct nla_gre_link gre_link;
237 int off, off2;
238
239 snl_init_writer(ctx->io_ss, &nw);
240 hdr = snl_create_msg_request(&nw, NL_RTM_GETLINK);
241 hdr->nlmsg_flags |= NLM_F_DUMP;
242 snl_reserve_msg_object(&nw, struct ifinfomsg);
243 snl_add_msg_attr_string(&nw, IFLA_IFNAME, ctx->ifname);
244
245 hdr = snl_finalize_msg(&nw);
246 if (hdr == NULL || (!snl_send_message(ctx->io_ss, hdr)))
247 err(1, "unable to send netlink message");
248
249 hdr = snl_read_reply(ctx->io_ss, hdr->nlmsg_seq);
250 if (hdr->nlmsg_type != NL_RTM_NEWLINK) {
251 if (!snl_parse_errmsg(ctx->io_ss, hdr, &errmsg))
252 errx(EINVAL, "(NETLINK)");
253 if (errmsg.error_str != NULL)
254 errx(errmsg.error, "(NETLINK) %s", errmsg.error_str);
255 }
256
257 if (!snl_parse_nlmsg(ctx->io_ss, hdr, &gre_parser, &gre_link))
258 return;
259
260 struct nla_gre_info gre_info = gre_link.linkinfo;
261 struct nl_parsed_gre gre_data = gre_info.data;
262
263 if (d < 0)
264 gre_data.ifla_flags &= ~(-d);
265 else
266 gre_data.ifla_flags |= d;
267
268 gre_nl_init(ctx, &nw, 0);
269 off = snl_add_msg_attr_nested(&nw, IFLA_LINKINFO);
270 snl_add_msg_attr_string(&nw, IFLA_INFO_KIND, "gre");
271
272 off2 = snl_add_msg_attr_nested(&nw, IFLA_INFO_DATA);
273 snl_add_msg_attr_u32(&nw, IFLA_GRE_FLAGS, gre_data.ifla_flags);
274
275 snl_end_attr_nested(&nw, off2);
276 snl_end_attr_nested(&nw, off);
277
278 gre_nl_fini(ctx, &nw);
279 }
280
281 static void
setifgretype_nl(if_ctx * ctx,const char * val __unused,int d)282 setifgretype_nl(if_ctx *ctx, const char *val __unused, int d)
283 {
284 struct snl_writer nw = {};
285 int off, off2;
286 uint32_t type;
287
288 gre_nl_init(ctx, &nw, 0);
289 off = snl_add_msg_attr_nested(&nw, IFLA_LINKINFO);
290 snl_add_msg_attr_string(&nw, IFLA_INFO_KIND, "gre");
291
292 off2 = snl_add_msg_attr_nested(&nw, IFLA_INFO_DATA);
293 type = d < 0 ? IFLA_TUNNEL_NONE : IFLA_TUNNEL_GRE_UDP;
294 snl_add_msg_attr_u32(&nw, IFLA_GRE_ENCAP_TYPE, type);
295
296 snl_end_attr_nested(&nw, off2);
297 snl_end_attr_nested(&nw, off);
298
299 gre_nl_fini(ctx, &nw);
300 }
301
302
303 static struct cmd gre_cmds[] = {
304 DEF_CMD_ARG("grekey", setifgrekey_nl),
305 DEF_CMD_ARG("udpport", setifgreport_nl),
306 DEF_CMD("enable_csum", GRE_ENABLE_CSUM, setifgreopts_nl),
307 DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts_nl),
308 DEF_CMD("enable_seq", GRE_ENABLE_SEQ, setifgreopts_nl),
309 DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ, setifgreopts_nl),
310 DEF_CMD("udpencap", GRE_UDPENCAP, setifgretype_nl),
311 DEF_CMD("-udpencap",-GRE_UDPENCAP, setifgretype_nl),
312 };
313
314 #else
315 static void
gre_status(if_ctx * ctx)316 gre_status(if_ctx *ctx)
317 {
318 uint32_t opts = 0, port;
319 struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
320
321 if (ioctl_ctx_ifr(ctx, GREGKEY, &ifr) == 0)
322 if (opts != 0)
323 printf("\tgrekey: 0x%x (%u)\n", opts, opts);
324 opts = 0;
325 if (ioctl_ctx_ifr(ctx, GREGOPTS, &ifr) != 0 || opts == 0)
326 return;
327
328 port = 0;
329 ifr.ifr_data = (caddr_t)&port;
330 if (ioctl_ctx_ifr(ctx, GREGPORT, &ifr) == 0 && port != 0)
331 printf("\tudpport: %u\n", port);
332 printf("\toptions=%x", opts);
333 print_bits("options", &opts, 1, GREBITS, nitems(GREBITS));
334 putchar('\n');
335 }
336
337 static void
setifgrekey(if_ctx * ctx,const char * val,int dummy __unused)338 setifgrekey(if_ctx *ctx, const char *val, int dummy __unused)
339 {
340 uint32_t grekey = strtol(val, NULL, 0);
341 struct ifreq ifr = { .ifr_data = (caddr_t)&grekey };
342
343 ifr.ifr_data = (caddr_t)&grekey;
344 if (ioctl_ctx_ifr(ctx, GRESKEY, &ifr) < 0)
345 warn("ioctl (set grekey)");
346 }
347
348 static void
setifgreport(if_ctx * ctx,const char * val,int dummy __unused)349 setifgreport(if_ctx *ctx, const char *val, int dummy __unused)
350 {
351 uint32_t udpport = strtol(val, NULL, 0);
352 struct ifreq ifr = { .ifr_data = (caddr_t)&udpport };
353
354 if (ioctl_ctx_ifr(ctx, GRESPORT, &ifr) < 0)
355 warn("ioctl (set udpport)");
356 }
357
358 static void
setifgreopts(if_ctx * ctx,const char * val __unused,int d)359 setifgreopts(if_ctx *ctx, const char *val __unused, int d)
360 {
361 uint32_t opts;
362 struct ifreq ifr = { .ifr_data = (caddr_t)&opts };
363
364 if (ioctl_ctx_ifr(ctx, GREGOPTS, &ifr) == -1) {
365 warn("ioctl(GREGOPTS)");
366 return;
367 }
368
369 if (d < 0)
370 opts &= ~(-d);
371 else
372 opts |= d;
373
374 if (ioctl_ctx(ctx, GRESOPTS, &ifr) == -1) {
375 warn("ioctl(GIFSOPTS)");
376 return;
377 }
378 }
379
380
381 static struct cmd gre_cmds[] = {
382 DEF_CMD_ARG("grekey", setifgrekey),
383 DEF_CMD_ARG("udpport", setifgreport),
384 DEF_CMD("enable_csum", GRE_ENABLE_CSUM, setifgreopts),
385 DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts),
386 DEF_CMD("enable_seq", GRE_ENABLE_SEQ, setifgreopts),
387 DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ, setifgreopts),
388 DEF_CMD("udpencap", GRE_UDPENCAP, setifgreopts),
389 DEF_CMD("-udpencap",-GRE_UDPENCAP, setifgreopts),
390 };
391 #endif /* !WITHOUT_NETLINK */
392
393 static struct afswtch af_gre = {
394 .af_name = "af_gre",
395 .af_af = AF_UNSPEC,
396 #ifndef WITHOUT_NETLINK
397 .af_other_status = gre_status_nl,
398 #else
399 .af_other_status = gre_status,
400 #endif
401 };
402
403 static __constructor void
gre_ctor(void)404 gre_ctor(void)
405 {
406 size_t i;
407
408 for (i = 0; i < nitems(gre_cmds); i++)
409 cmd_register(&gre_cmds[i]);
410 af_register(&af_gre);
411 #ifndef WITHOUT_NETLINK
412 SNL_VERIFY_PARSERS(all_parsers);
413 #endif
414 }
415