1 1.1 joe /*- 2 1.1 joe * Copyright (c) 2025 The NetBSD Foundation, Inc. 3 1.1 joe * All rights reserved. 4 1.1 joe * 5 1.1 joe * This code is derived from software contributed to The NetBSD Foundation 6 1.1 joe * by Emmanuel Nyarko. 7 1.1 joe * 8 1.1 joe * Redistribution and use in source and binary forms, with or without 9 1.1 joe * modification, are permitted provided that the following conditions 10 1.1 joe * are met: 11 1.1 joe * 1. Redistributions of source code must retain the above copyright 12 1.1 joe * notice, this list of conditions and the following disclaimer. 13 1.1 joe * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 joe * notice, this list of conditions and the following disclaimer in the 15 1.1 joe * documentation and/or other materials provided with the distribution. 16 1.1 joe * 17 1.1 joe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 joe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 joe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 joe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 joe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 joe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 joe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 joe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 joe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 joe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 joe * POSSIBILITY OF SUCH DAMAGE. 28 1.1 joe */ 29 1.1 joe 30 1.1 joe #include <sys/cdefs.h> 31 1.3 joe __RCSID("$NetBSD: npfext_route.c,v 1.3 2026/10/08 09:28:41 joe Exp $"); 32 1.1 joe 33 1.1 joe #include <sys/types.h> 34 1.1 joe #include <sys/socket.h> 35 1.1 joe #include <sys/ioctl.h> 36 1.1 joe #include <net/if.h> 37 1.1 joe 38 1.1 joe #include <stdio.h> 39 1.1 joe #include <string.h> 40 1.1 joe #include <assert.h> 41 1.1 joe #include <errno.h> 42 1.1 joe #include <err.h> 43 1.1 joe #include <unistd.h> 44 1.2 joe #include <arpa/inet.h> 45 1.1 joe 46 1.1 joe #include <npf.h> 47 1.1 joe 48 1.1 joe int npfext_route_init(void); 49 1.1 joe nl_ext_t * npfext_route_construct(const char *); 50 1.1 joe int npfext_route_param(nl_ext_t *, const char *, const char *); 51 1.1 joe 52 1.1 joe int 53 1.1 joe npfext_route_init(void) 54 1.1 joe { 55 1.1 joe /* Nothing to initialise. */ 56 1.1 joe return 0; 57 1.1 joe } 58 1.1 joe 59 1.1 joe nl_ext_t * 60 1.1 joe npfext_route_construct(const char *name) 61 1.1 joe { 62 1.1 joe assert(strcmp(name, "route") == 0); 63 1.1 joe return npf_ext_construct(name); 64 1.1 joe } 65 1.1 joe 66 1.1 joe int 67 1.2 joe npfext_route_param(nl_ext_t *ext, const char *param, const char *val) 68 1.1 joe { 69 1.2 joe enum ptype { 70 1.2 joe PARAM_INTERFACE, 71 1.2 joe PARAM_INET4, 72 1.2 joe PARAM_INET6, 73 1.2 joe PARAM_DUP, 74 1.2 joe }; 75 1.2 joe static const struct param { 76 1.2 joe const char * name; 77 1.2 joe enum ptype type; 78 1.2 joe bool reqval; 79 1.2 joe } params[] = { 80 1.2 joe { "interface", PARAM_INTERFACE, true }, 81 1.2 joe { "via", PARAM_INET4, true }, 82 1.2 joe { "via6", PARAM_INET6, true }, 83 1.2 joe { "dup-to", PARAM_DUP, false }, 84 1.2 joe }; 85 1.2 joe 86 1.2 joe for (unsigned i = 0; i < __arraycount(params); i++) { 87 1.2 joe const char *name = params[i].name; 88 1.2 joe struct in_addr addr; 89 1.2 joe struct in6_addr addr6; 90 1.2 joe 91 1.2 joe if (strcmp(name, param) != 0) { 92 1.2 joe continue; 93 1.2 joe } 94 1.2 joe if (val == NULL && params[i].reqval) { 95 1.2 joe return EINVAL; 96 1.2 joe } 97 1.2 joe 98 1.2 joe switch (params[i].type) { 99 1.2 joe case PARAM_DUP: 100 1.2 joe npf_ext_param_bool(ext, name, true); 101 1.2 joe break; 102 1.2 joe case PARAM_INTERFACE: 103 1.3 joe npf_ext_param_string(ext, "route-interface", val); 104 1.2 joe break; 105 1.2 joe /* catch invalid addresses early */ 106 1.2 joe case PARAM_INET4: 107 1.2 joe if (inet_pton(AF_INET, val, &addr) != 1) { 108 1.2 joe warn("Invalid IPv4 address `%s'", val); 109 1.2 joe return EINVAL; 110 1.2 joe } 111 1.2 joe npf_ext_param_binary(ext, name, &addr, sizeof(struct in_addr)); 112 1.2 joe break; 113 1.2 joe case PARAM_INET6: 114 1.2 joe if (inet_pton(AF_INET6, val, &addr6) != 1) { 115 1.2 joe warn("Invalid IPv6 address `%s'", val); 116 1.2 joe return EINVAL; 117 1.2 joe } 118 1.2 joe npf_ext_param_binary(ext, name, &addr6, sizeof(struct in6_addr)); 119 1.2 joe break; 120 1.2 joe 121 1.2 joe default: 122 1.2 joe break; 123 1.2 joe } 124 1.2 joe } 125 1.1 joe return 0; 126 1.1 joe } 127