]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ifconfig/ifpfsync.c
sqlite3: Vendor import of sqlite3 3.41.0
[FreeBSD/FreeBSD.git] / sbin / ifconfig / ifpfsync.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Ryan McBride. All rights reserved.
5  * Copyright (c) 2004 Max Laier. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/ioctl.h>
34 #include <sys/nv.h>
35 #include <sys/socket.h>
36
37 #include <net/if.h>
38 #include <netinet/in.h>
39 #include <net/pfvar.h>
40 #include <net/if_pfsync.h>
41 #include <net/route.h>
42 #include <arpa/inet.h>
43
44 #include <err.h>
45 #include <netdb.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "ifconfig.h"
52
53 void setpfsync_syncdev(const char *, int, int, const struct afswtch *);
54 void unsetpfsync_syncdev(const char *, int, int, const struct afswtch *);
55 void setpfsync_syncpeer(const char *, int, int, const struct afswtch *);
56 void unsetpfsync_syncpeer(const char *, int, int, const struct afswtch *);
57 void setpfsync_syncpeer(const char *, int, int, const struct afswtch *);
58 void setpfsync_maxupd(const char *, int, int, const struct afswtch *);
59 void setpfsync_defer(const char *, int, int, const struct afswtch *);
60 void pfsync_status(int);
61
62 static int
63 pfsync_do_ioctl(int s, uint cmd, nvlist_t **nvl)
64 {
65         void *data;
66         size_t nvlen;
67
68         data = nvlist_pack(*nvl, &nvlen);
69
70         ifr.ifr_cap_nv.buffer = malloc(IFR_CAP_NV_MAXBUFSIZE);
71         memcpy(ifr.ifr_cap_nv.buffer, data, nvlen);
72         ifr.ifr_cap_nv.buf_length = IFR_CAP_NV_MAXBUFSIZE;
73         ifr.ifr_cap_nv.length = nvlen;
74         free(data);
75
76         if (ioctl(s, cmd, (caddr_t)&ifr) == -1) {
77                 free(ifr.ifr_cap_nv.buffer);
78                 return -1;
79         }
80
81         nvlist_destroy(*nvl);
82         *nvl = NULL;
83
84         *nvl = nvlist_unpack(ifr.ifr_cap_nv.buffer, ifr.ifr_cap_nv.length, 0);
85         if (*nvl == NULL) {
86                 free(ifr.ifr_cap_nv.buffer);
87                 return (EIO);
88         }
89
90         free(ifr.ifr_cap_nv.buffer);
91         return (errno);
92 }
93
94 static nvlist_t *
95 pfsync_sockaddr_to_syncpeer_nvlist(struct sockaddr_storage *sa)
96 {
97         nvlist_t *nvl;
98
99         nvl = nvlist_create(0);
100         if (nvl == NULL) {
101                 return (nvl);
102         }
103
104         switch (sa->ss_family) {
105 #ifdef INET
106         case AF_INET: {
107                 struct sockaddr_in *in = (struct sockaddr_in *)sa;
108                 nvlist_add_number(nvl, "af", in->sin_family);
109                 nvlist_add_binary(nvl, "address", in, sizeof(*in));
110                 break;
111         }
112 #endif
113 #ifdef INET6
114         case AF_INET6: {
115                 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
116                 nvlist_add_number(nvl, "af", in6->sin6_family);
117                 nvlist_add_binary(nvl, "address", in6, sizeof(*in6));
118                 break;
119         }
120 #endif
121         default:
122                 nvlist_add_number(nvl, "af", AF_UNSPEC);
123                 nvlist_add_binary(nvl, "address", sa, sizeof(*sa));
124                 break;
125         }
126
127         return (nvl);
128 }
129
130 static int
131 pfsync_syncpeer_nvlist_to_sockaddr(const nvlist_t *nvl,
132     struct sockaddr_storage *sa)
133 {
134         int af;
135
136         if (!nvlist_exists_number(nvl, "af"))
137                 return (EINVAL);
138         if (!nvlist_exists_binary(nvl, "address"))
139                 return (EINVAL);
140
141         af = nvlist_get_number(nvl, "af");
142
143         switch (af) {
144 #ifdef INET
145         case AF_INET: {
146                 struct sockaddr_in *in = (struct sockaddr_in *)sa;
147                 size_t len;
148                 const void *addr = nvlist_get_binary(nvl, "address", &len);
149                 in->sin_family = af;
150                 if (len != sizeof(*in))
151                         return (EINVAL);
152
153                 memcpy(in, addr, sizeof(*in));
154                 break;
155         }
156 #endif
157 #ifdef INET6
158         case AF_INET6: {
159                 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
160                 size_t len;
161                 const void *addr = nvlist_get_binary(nvl, "address", &len);
162                 if (len != sizeof(*in6))
163                         return (EINVAL);
164
165                 memcpy(in6, addr, sizeof(*in6));
166                 break;
167         }
168 #endif
169         default:
170                 return (EINVAL);
171         }
172
173         return (0);
174 }
175
176 void
177 setpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
178 {
179         nvlist_t *nvl = nvlist_create(0);
180
181         if (strlen(val) > IFNAMSIZ)
182                 errx(1, "interface name %s is too long", val);
183
184         if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
185                 err(1, "SIOCGETPFSYNCNV");
186
187         if (nvlist_exists_string(nvl, "syncdev"))
188                 nvlist_free_string(nvl, "syncdev");
189
190         nvlist_add_string(nvl, "syncdev", val);
191
192         if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
193                 err(1, "SIOCSETPFSYNCNV");
194 }
195
196 /* ARGSUSED */
197 void
198 unsetpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
199 {
200         nvlist_t *nvl = nvlist_create(0);
201
202         if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
203                 err(1, "SIOCGETPFSYNCNV");
204
205         if (nvlist_exists_string(nvl, "syncdev"))
206                 nvlist_free_string(nvl, "syncdev");
207
208         nvlist_add_string(nvl, "syncdev", "");
209
210         if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
211                 err(1, "SIOCSETPFSYNCNV");
212 }
213
214 /* ARGSUSED */
215 void
216 setpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
217 {
218         struct addrinfo *peerres;
219         struct sockaddr_storage addr;
220         int ecode;
221
222         nvlist_t *nvl = nvlist_create(0);
223
224         if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
225                 err(1, "SIOCGETPFSYNCNV");
226
227         if ((ecode = getaddrinfo(val, NULL, NULL, &peerres)) != 0)
228                 errx(1, "error in parsing address string: %s",
229                     gai_strerror(ecode));
230
231         switch (peerres->ai_family) {
232 #ifdef INET
233         case AF_INET: {
234                 struct sockaddr_in *sin = (struct sockaddr_in *)
235                                               peerres->ai_addr;
236
237                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
238                         errx(1, "syncpeer address cannot be multicast");
239
240                 memcpy(&addr, sin, sizeof(*sin));
241                 break;
242         }
243 #endif
244         default:
245                 errx(1, "syncpeer address %s not supported", val);
246         }
247
248         if (nvlist_exists_nvlist(nvl, "syncpeer"))
249                 nvlist_free_nvlist(nvl, "syncpeer");
250
251         nvlist_add_nvlist(nvl, "syncpeer",
252             pfsync_sockaddr_to_syncpeer_nvlist(&addr));
253
254         if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
255                 err(1, "SIOCSETPFSYNCNV");
256
257         nvlist_destroy(nvl);
258         freeaddrinfo(peerres);
259 }
260
261 /* ARGSUSED */
262 void
263 unsetpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
264 {
265         struct sockaddr_storage addr;
266         memset(&addr, 0, sizeof(addr));
267
268         nvlist_t *nvl = nvlist_create(0);
269
270         if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
271                 err(1, "SIOCGETPFSYNCNV");
272
273         if (nvlist_exists_nvlist(nvl, "syncpeer"))
274                 nvlist_free_nvlist(nvl, "syncpeer");
275
276         nvlist_add_nvlist(nvl, "syncpeer",
277             pfsync_sockaddr_to_syncpeer_nvlist(&addr));
278
279         if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
280                 err(1, "SIOCSETPFSYNCNV");
281
282         nvlist_destroy(nvl);
283 }
284
285 /* ARGSUSED */
286 void
287 setpfsync_maxupd(const char *val, int d, int s, const struct afswtch *rafp)
288 {
289         int maxupdates;
290         nvlist_t *nvl = nvlist_create(0);
291
292         maxupdates = atoi(val);
293         if ((maxupdates < 0) || (maxupdates > 255))
294                 errx(1, "maxupd %s: out of range", val);
295
296         if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
297                 err(1, "SIOCGETPFSYNCNV");
298
299         nvlist_free_number(nvl, "maxupdates");
300         nvlist_add_number(nvl, "maxupdates", maxupdates);
301
302         if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
303                 err(1, "SIOCSETPFSYNCNV");
304
305         nvlist_destroy(nvl);
306 }
307
308 /* ARGSUSED */
309 void
310 setpfsync_defer(const char *val, int d, int s, const struct afswtch *rafp)
311 {
312         nvlist_t *nvl = nvlist_create(0);
313
314         if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
315                 err(1, "SIOCGETPFSYNCNV");
316
317         nvlist_free_number(nvl, "flags");
318         nvlist_add_number(nvl, "flags", d ? PFSYNCF_DEFER : 0);
319
320         if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
321                 err(1, "SIOCSETPFSYNCNV");
322
323         nvlist_destroy(nvl);
324 }
325
326 void
327 pfsync_status(int s)
328 {
329         nvlist_t *nvl;
330         char syncdev[IFNAMSIZ];
331         char syncpeer_str[NI_MAXHOST];
332         struct sockaddr_storage syncpeer;
333         int maxupdates;
334         int flags;
335         int error;
336
337         nvl = nvlist_create(0);
338
339         if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1) {
340                 nvlist_destroy(nvl);
341                 return;
342         }
343
344         memset((char *)&syncdev, 0, IFNAMSIZ);
345         if (nvlist_exists_string(nvl, "syncdev"))
346                 strlcpy(syncdev, nvlist_get_string(nvl, "syncdev"),
347                     IFNAMSIZ);
348         if (nvlist_exists_number(nvl, "maxupdates"))
349                 maxupdates = nvlist_get_number(nvl, "maxupdates");
350         if (nvlist_exists_number(nvl, "flags"))
351                 flags = nvlist_get_number(nvl, "flags");
352         if (nvlist_exists_nvlist(nvl, "syncpeer")) {
353                 pfsync_syncpeer_nvlist_to_sockaddr(nvlist_get_nvlist(nvl,
354                                                              "syncpeer"),
355                     &syncpeer);
356         }
357
358         nvlist_destroy(nvl);
359
360         if (syncdev[0] != '\0' || syncpeer.ss_family != AF_UNSPEC)
361                 printf("\t");
362
363         if (syncdev[0] != '\0')
364                 printf("syncdev: %s ", syncdev);
365
366         if (syncpeer.ss_family == AF_INET &&
367             ((struct sockaddr_in *)&syncpeer)->sin_addr.s_addr !=
368                 htonl(INADDR_PFSYNC_GROUP)) {
369
370                 struct sockaddr *syncpeer_sa =
371                     (struct sockaddr *)&syncpeer;
372                 if ((error = getnameinfo(syncpeer_sa, syncpeer_sa->sa_len,
373                          syncpeer_str, sizeof(syncpeer_str), NULL, 0,
374                          NI_NUMERICHOST)) != 0)
375                         errx(1, "getnameinfo: %s", gai_strerror(error));
376                 printf("syncpeer: %s ", syncpeer_str);
377         }
378
379         printf("maxupd: %d ", maxupdates);
380         printf("defer: %s\n", (flags & PFSYNCF_DEFER) ? "on" : "off");
381         printf("\tsyncok: %d\n", (flags & PFSYNCF_OK) ? 1 : 0);
382 }
383
384 static struct cmd pfsync_cmds[] = {
385         DEF_CMD_ARG("syncdev",          setpfsync_syncdev),
386         DEF_CMD("-syncdev",     1,      unsetpfsync_syncdev),
387         DEF_CMD_ARG("syncif",           setpfsync_syncdev),
388         DEF_CMD("-syncif",      1,      unsetpfsync_syncdev),
389         DEF_CMD_ARG("syncpeer",         setpfsync_syncpeer),
390         DEF_CMD("-syncpeer",    1,      unsetpfsync_syncpeer),
391         DEF_CMD_ARG("maxupd",           setpfsync_maxupd),
392         DEF_CMD("defer",        1,      setpfsync_defer),
393         DEF_CMD("-defer",       0,      setpfsync_defer),
394 };
395 static struct afswtch af_pfsync = {
396         .af_name        = "af_pfsync",
397         .af_af          = AF_UNSPEC,
398         .af_other_status = pfsync_status,
399 };
400
401 static __constructor void
402 pfsync_ctor(void)
403 {
404         int i;
405
406         for (i = 0; i < nitems(pfsync_cmds);  i++)
407                 cmd_register(&pfsync_cmds[i]);
408         af_register(&af_pfsync);
409 }