1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 static int 54 pfsync_do_ioctl(if_ctx *ctx, uint cmd, nvlist_t **nvl) 55 { 56 void *data; 57 size_t nvlen; 58 struct ifreq ifr = {}; 59 60 data = nvlist_pack(*nvl, &nvlen); 61 62 ifr.ifr_cap_nv.buffer = malloc(IFR_CAP_NV_MAXBUFSIZE); 63 memcpy(ifr.ifr_cap_nv.buffer, data, nvlen); 64 ifr.ifr_cap_nv.buf_length = IFR_CAP_NV_MAXBUFSIZE; 65 ifr.ifr_cap_nv.length = nvlen; 66 free(data); 67 68 if (ioctl_ctx_ifr(ctx, cmd, &ifr) == -1) { 69 free(ifr.ifr_cap_nv.buffer); 70 return -1; 71 } 72 73 nvlist_destroy(*nvl); 74 *nvl = NULL; 75 76 *nvl = nvlist_unpack(ifr.ifr_cap_nv.buffer, ifr.ifr_cap_nv.length, 0); 77 if (*nvl == NULL) { 78 free(ifr.ifr_cap_nv.buffer); 79 return (EIO); 80 } 81 82 free(ifr.ifr_cap_nv.buffer); 83 return (errno); 84 } 85 86 static nvlist_t * 87 pfsync_sockaddr_to_syncpeer_nvlist(struct sockaddr_storage *sa) 88 { 89 nvlist_t *nvl; 90 91 nvl = nvlist_create(0); 92 if (nvl == NULL) { 93 return (nvl); 94 } 95 96 switch (sa->ss_family) { 97 #ifdef INET 98 case AF_INET: { 99 struct sockaddr_in *in = (struct sockaddr_in *)sa; 100 nvlist_add_number(nvl, "af", in->sin_family); 101 nvlist_add_binary(nvl, "address", in, sizeof(*in)); 102 break; 103 } 104 #endif 105 #ifdef INET6 106 case AF_INET6: { 107 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa; 108 nvlist_add_number(nvl, "af", in6->sin6_family); 109 nvlist_add_binary(nvl, "address", in6, sizeof(*in6)); 110 break; 111 } 112 #endif 113 default: 114 nvlist_add_number(nvl, "af", AF_UNSPEC); 115 nvlist_add_binary(nvl, "address", sa, sizeof(*sa)); 116 break; 117 } 118 119 return (nvl); 120 } 121 122 static int 123 pfsync_syncpeer_nvlist_to_sockaddr(const nvlist_t *nvl, 124 struct sockaddr_storage *sa) 125 { 126 int af; 127 128 if (!nvlist_exists_number(nvl, "af")) 129 return (EINVAL); 130 if (!nvlist_exists_binary(nvl, "address")) 131 return (EINVAL); 132 133 af = nvlist_get_number(nvl, "af"); 134 135 switch (af) { 136 #ifdef INET 137 case AF_INET: { 138 struct sockaddr_in *in = (struct sockaddr_in *)sa; 139 size_t len; 140 const void *addr = nvlist_get_binary(nvl, "address", &len); 141 in->sin_family = af; 142 if (len != sizeof(*in)) 143 return (EINVAL); 144 145 memcpy(in, addr, sizeof(*in)); 146 break; 147 } 148 #endif 149 #ifdef INET6 150 case AF_INET6: { 151 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa; 152 size_t len; 153 const void *addr = nvlist_get_binary(nvl, "address", &len); 154 if (len != sizeof(*in6)) 155 return (EINVAL); 156 157 memcpy(in6, addr, sizeof(*in6)); 158 break; 159 } 160 #endif 161 default: 162 return (EINVAL); 163 } 164 165 return (0); 166 } 167 168 static void 169 setpfsync_syncdev(if_ctx *ctx, const char *val, int dummy __unused) 170 { 171 nvlist_t *nvl = nvlist_create(0); 172 173 if (strlen(val) > IFNAMSIZ) 174 errx(1, "interface name %s is too long", val); 175 176 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) 177 err(1, "SIOCGETPFSYNCNV"); 178 179 if (nvlist_exists_string(nvl, "syncdev")) 180 nvlist_free_string(nvl, "syncdev"); 181 182 nvlist_add_string(nvl, "syncdev", val); 183 184 if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) 185 err(1, "SIOCSETPFSYNCNV"); 186 } 187 188 static void 189 unsetpfsync_syncdev(if_ctx *ctx, const char *val __unused, int dummy __unused) 190 { 191 nvlist_t *nvl = nvlist_create(0); 192 193 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) 194 err(1, "SIOCGETPFSYNCNV"); 195 196 if (nvlist_exists_string(nvl, "syncdev")) 197 nvlist_free_string(nvl, "syncdev"); 198 199 nvlist_add_string(nvl, "syncdev", ""); 200 201 if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) 202 err(1, "SIOCSETPFSYNCNV"); 203 } 204 205 static void 206 setpfsync_syncpeer(if_ctx *ctx, const char *val, int dummy __unused) 207 { 208 struct addrinfo *peerres; 209 struct sockaddr_storage addr; 210 int ecode; 211 212 nvlist_t *nvl = nvlist_create(0); 213 214 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) 215 err(1, "SIOCGETPFSYNCNV"); 216 217 if ((ecode = getaddrinfo(val, NULL, NULL, &peerres)) != 0) 218 errx(1, "error in parsing address string: %s", 219 gai_strerror(ecode)); 220 221 switch (peerres->ai_family) { 222 #ifdef INET 223 case AF_INET: { 224 struct sockaddr_in *sin = satosin(peerres->ai_addr); 225 226 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 227 errx(1, "syncpeer address cannot be multicast"); 228 229 memcpy(&addr, sin, sizeof(*sin)); 230 break; 231 } 232 #endif 233 default: 234 errx(1, "syncpeer address %s not supported", val); 235 } 236 237 if (nvlist_exists_nvlist(nvl, "syncpeer")) 238 nvlist_free_nvlist(nvl, "syncpeer"); 239 240 nvlist_add_nvlist(nvl, "syncpeer", 241 pfsync_sockaddr_to_syncpeer_nvlist(&addr)); 242 243 if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) 244 err(1, "SIOCSETPFSYNCNV"); 245 246 nvlist_destroy(nvl); 247 freeaddrinfo(peerres); 248 } 249 250 static void 251 unsetpfsync_syncpeer(if_ctx *ctx, const char *val __unused, int dummy __unused) 252 { 253 struct sockaddr_storage addr; 254 memset(&addr, 0, sizeof(addr)); 255 256 nvlist_t *nvl = nvlist_create(0); 257 258 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) 259 err(1, "SIOCGETPFSYNCNV"); 260 261 if (nvlist_exists_nvlist(nvl, "syncpeer")) 262 nvlist_free_nvlist(nvl, "syncpeer"); 263 264 nvlist_add_nvlist(nvl, "syncpeer", 265 pfsync_sockaddr_to_syncpeer_nvlist(&addr)); 266 267 if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) 268 err(1, "SIOCSETPFSYNCNV"); 269 270 nvlist_destroy(nvl); 271 } 272 273 static void 274 setpfsync_maxupd(if_ctx *ctx, const char *val, int dummy __unused) 275 { 276 int maxupdates; 277 nvlist_t *nvl = nvlist_create(0); 278 279 maxupdates = atoi(val); 280 if ((maxupdates < 0) || (maxupdates > 255)) 281 errx(1, "maxupd %s: out of range", val); 282 283 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) 284 err(1, "SIOCGETPFSYNCNV"); 285 286 nvlist_free_number(nvl, "maxupdates"); 287 nvlist_add_number(nvl, "maxupdates", maxupdates); 288 289 if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) 290 err(1, "SIOCSETPFSYNCNV"); 291 292 nvlist_destroy(nvl); 293 } 294 295 static void 296 setpfsync_defer(if_ctx *ctx, const char *val __unused, int d) 297 { 298 nvlist_t *nvl = nvlist_create(0); 299 300 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) 301 err(1, "SIOCGETPFSYNCNV"); 302 303 nvlist_free_number(nvl, "flags"); 304 nvlist_add_number(nvl, "flags", d ? PFSYNCF_DEFER : 0); 305 306 if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) 307 err(1, "SIOCSETPFSYNCNV"); 308 309 nvlist_destroy(nvl); 310 } 311 312 static void 313 setpfsync_version(if_ctx *ctx, const char *val, int dummy __unused) 314 { 315 int version; 316 nvlist_t *nvl = nvlist_create(0); 317 318 /* Don't verify, kernel knows which versions are supported.*/ 319 version = atoi(val); 320 321 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) 322 err(1, "SIOCGETPFSYNCNV"); 323 324 nvlist_free_number(nvl, "version"); 325 nvlist_add_number(nvl, "version", version); 326 327 if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) 328 err(1, "SIOCSETPFSYNCNV"); 329 330 nvlist_destroy(nvl); 331 } 332 333 static void 334 pfsync_status(if_ctx *ctx) 335 { 336 nvlist_t *nvl; 337 char syncdev[IFNAMSIZ]; 338 char syncpeer_str[NI_MAXHOST]; 339 struct sockaddr_storage syncpeer; 340 int maxupdates = 0; 341 int flags = 0; 342 int version; 343 int error; 344 345 nvl = nvlist_create(0); 346 347 if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) { 348 nvlist_destroy(nvl); 349 return; 350 } 351 352 memset((char *)&syncdev, 0, IFNAMSIZ); 353 if (nvlist_exists_string(nvl, "syncdev")) 354 strlcpy(syncdev, nvlist_get_string(nvl, "syncdev"), 355 IFNAMSIZ); 356 if (nvlist_exists_number(nvl, "maxupdates")) 357 maxupdates = nvlist_get_number(nvl, "maxupdates"); 358 if (nvlist_exists_number(nvl, "version")) 359 version = nvlist_get_number(nvl, "version"); 360 if (nvlist_exists_number(nvl, "flags")) 361 flags = nvlist_get_number(nvl, "flags"); 362 if (nvlist_exists_nvlist(nvl, "syncpeer")) { 363 pfsync_syncpeer_nvlist_to_sockaddr(nvlist_get_nvlist(nvl, 364 "syncpeer"), 365 &syncpeer); 366 } 367 368 nvlist_destroy(nvl); 369 370 if (syncdev[0] != '\0' || syncpeer.ss_family != AF_UNSPEC) 371 printf("\t"); 372 373 if (syncdev[0] != '\0') 374 printf("syncdev: %s ", syncdev); 375 376 if (syncpeer.ss_family == AF_INET && 377 ((struct sockaddr_in *)&syncpeer)->sin_addr.s_addr != 378 htonl(INADDR_PFSYNC_GROUP)) { 379 380 struct sockaddr *syncpeer_sa = 381 (struct sockaddr *)&syncpeer; 382 if ((error = getnameinfo(syncpeer_sa, syncpeer_sa->sa_len, 383 syncpeer_str, sizeof(syncpeer_str), NULL, 0, 384 NI_NUMERICHOST)) != 0) 385 errx(1, "getnameinfo: %s", gai_strerror(error)); 386 printf("syncpeer: %s ", syncpeer_str); 387 } 388 389 printf("maxupd: %d ", maxupdates); 390 printf("defer: %s ", (flags & PFSYNCF_DEFER) ? "on" : "off"); 391 printf("version: %d\n", version); 392 printf("\tsyncok: %d\n", (flags & PFSYNCF_OK) ? 1 : 0); 393 } 394 395 static struct cmd pfsync_cmds[] = { 396 DEF_CMD_ARG("syncdev", setpfsync_syncdev), 397 DEF_CMD("-syncdev", 1, unsetpfsync_syncdev), 398 DEF_CMD_ARG("syncif", setpfsync_syncdev), 399 DEF_CMD("-syncif", 1, unsetpfsync_syncdev), 400 DEF_CMD_ARG("syncpeer", setpfsync_syncpeer), 401 DEF_CMD("-syncpeer", 1, unsetpfsync_syncpeer), 402 DEF_CMD_ARG("maxupd", setpfsync_maxupd), 403 DEF_CMD("defer", 1, setpfsync_defer), 404 DEF_CMD("-defer", 0, setpfsync_defer), 405 DEF_CMD_ARG("version", setpfsync_version), 406 }; 407 static struct afswtch af_pfsync = { 408 .af_name = "af_pfsync", 409 .af_af = AF_UNSPEC, 410 .af_other_status = pfsync_status, 411 }; 412 413 static __constructor void 414 pfsync_ctor(void) 415 { 416 for (size_t i = 0; i < nitems(pfsync_cmds); i++) 417 cmd_register(&pfsync_cmds[i]); 418 af_register(&af_pfsync); 419 } 420