1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org> 5 * 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 */ 29 #include <sys/cdefs.h> 30 #ifdef _KERNEL 31 #include <sys/libkern.h> 32 #include <sys/param.h> 33 #include <sys/lock.h> 34 #include <sys/rwlock.h> 35 #else 36 #include <stdio.h> 37 #include <string.h> 38 #include <sys/types.h> 39 #include <errno.h> 40 #endif 41 42 #include <netinet/in_systm.h> 43 #include <netinet/in.h> 44 #include <netinet/ip.h> 45 46 #ifdef _KERNEL 47 #include <netinet/libalias/alias_local.h> 48 #include <netinet/libalias/alias_mod.h> 49 #else 50 #include "alias_local.h" 51 #include "alias_mod.h" 52 #endif 53 54 /* Protocol and userland module handlers chains. */ 55 static TAILQ_HEAD(handler_chain, proto_handler) handler_chain = 56 TAILQ_HEAD_INITIALIZER(handler_chain); 57 58 static int 59 attach_handler(struct proto_handler *p) 60 { 61 struct proto_handler *b; 62 63 TAILQ_FOREACH(b, &handler_chain, link) { 64 if ((b->pri == p->pri) && 65 (b->dir == p->dir) && 66 (b->proto == p->proto)) 67 return (EEXIST); 68 if (b->pri > p->pri) { 69 TAILQ_INSERT_BEFORE(b, p, link); 70 return (0); 71 } 72 } 73 74 TAILQ_INSERT_TAIL(&handler_chain, p, link); 75 76 return (0); 77 } 78 79 int 80 LibAliasAttachHandlers(struct proto_handler *p) 81 { 82 int error; 83 84 while (p->dir != NODIR) { 85 error = attach_handler(p); 86 if (error) 87 return (error); 88 p++; 89 } 90 91 return (0); 92 } 93 94 /* XXXGL: should be void, but no good reason to break ABI */ 95 int 96 LibAliasDetachHandlers(struct proto_handler *p) 97 { 98 while (p->dir != NODIR) { 99 TAILQ_REMOVE(&handler_chain, p, link); 100 p++; 101 } 102 103 return (0); 104 } 105 106 int 107 find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *ip, 108 struct alias_data *ad) 109 { 110 struct proto_handler *p; 111 112 TAILQ_FOREACH(p, &handler_chain, link) 113 if ((p->dir & dir) && (p->proto & proto) && 114 p->fingerprint(la, ad) == 0) 115 return (p->protohandler(la, ip, ad)); 116 117 return (ENOENT); 118 } 119 120 struct proto_handler * 121 first_handler(void) 122 { 123 return (TAILQ_FIRST(&handler_chain)); 124 } 125 126 #ifndef _KERNEL 127 /* Dll manipulation code - this code is not thread safe... */ 128 static SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain); 129 int 130 attach_dll(struct dll *p) 131 { 132 struct dll *b; 133 134 SLIST_FOREACH(b, &dll_chain, next) { 135 if (!strncmp(b->name, p->name, DLL_LEN)) 136 return (EEXIST); /* Dll name conflict. */ 137 } 138 SLIST_INSERT_HEAD(&dll_chain, p, next); 139 return (0); 140 } 141 142 void * 143 detach_dll(char *p) 144 { 145 struct dll *b, *b_tmp; 146 void *error; 147 148 b = NULL; 149 error = NULL; 150 SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp) 151 if (!strncmp(b->name, p, DLL_LEN)) { 152 SLIST_REMOVE(&dll_chain, b, dll, next); 153 error = b; 154 break; 155 } 156 return (error); 157 } 158 159 struct dll * 160 walk_dll_chain(void) 161 { 162 struct dll *t; 163 164 t = SLIST_FIRST(&dll_chain); 165 if (t == NULL) 166 return (NULL); 167 SLIST_REMOVE_HEAD(&dll_chain, next); 168 return (t); 169 } 170 #endif /* !_KERNEL */ 171