af.c revision 11820
1/* 2 * Copyright (c) 1985, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Copyright (c) 1995 John Hay. All rights reserved. 6 * 7 * This file includes significant work done at Cornell University by 8 * Bill Nesheim. That work included by permission. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * $Id: af.c,v 1.4 1995/10/11 18:57:05 jhay Exp $ 39 */ 40 41#ifndef lint 42static char sccsid[] = "@(#)af.c 8.1 (Berkeley) 6/5/93"; 43#endif /* not lint */ 44 45#include "defs.h" 46 47/* 48 * Address family support routines 49 */ 50af_hash_t null_hash; 51af_netmatch_t null_netmatch; 52af_output_t null_output; 53af_portmatch_t null_portmatch; 54af_portcheck_t null_portcheck; 55af_checkhost_t null_checkhost; 56af_ishost_t null_ishost; 57af_canon_t null_canon; 58 59void ipxnet_hash(struct sockaddr_ipx *, struct afhash *); 60int ipxnet_netmatch(struct sockaddr_ipx *, struct sockaddr_ipx *); 61void ipxnet_output(int, int, struct sockaddr_ipx *, int); 62int ipxnet_portmatch(struct sockaddr_ipx *); 63int ipxnet_checkhost(struct sockaddr_ipx *); 64int ipxnet_ishost(struct sockaddr_ipx *); 65void ipxnet_canon(struct sockaddr_ipx *); 66 67#define NIL \ 68 { null_hash, null_netmatch, null_output, \ 69 null_portmatch, null_portcheck, null_checkhost, \ 70 null_ishost, null_canon } 71#define IPXNET \ 72 { (af_hash_t *)ipxnet_hash, \ 73 (af_netmatch_t *)ipxnet_netmatch, \ 74 (af_output_t *)ipxnet_output, \ 75 (af_portmatch_t *)ipxnet_portmatch, \ 76 (af_portcheck_t *)ipxnet_portmatch, \ 77 (af_checkhost_t *)ipxnet_checkhost, \ 78 (af_ishost_t *)ipxnet_ishost, \ 79 (af_canon_t *)ipxnet_canon } 80 81struct afswitch afswitch[AF_MAX] = 82 { NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, 83 NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, 84 NIL, NIL, NIL, IPXNET, NIL, NIL }; 85 86struct sockaddr_ipx ipxnet_default = { sizeof(struct sockaddr_ipx), AF_IPX }; 87 88union ipx_net ipx_anynet; 89union ipx_net ipx_zeronet; 90 91void 92ipxnet_hash(sipx, hp) 93 register struct sockaddr_ipx *sipx; 94 struct afhash *hp; 95{ 96 register long hash = 0; 97 register u_short *s = sipx->sipx_addr.x_host.s_host; 98 union ipx_net_u net; 99 100 net.net_e = sipx->sipx_addr.x_net; 101 hp->afh_nethash = net.long_e; 102 hash = *s++; hash <<= 8; hash += *s++; hash <<= 8; hash += *s; 103 hp->afh_hosthash = hash; 104} 105 106int 107ipxnet_netmatch(sxn1, sxn2) 108 struct sockaddr_ipx *sxn1, *sxn2; 109{ 110 return (ipx_neteq(sxn1->sipx_addr, sxn2->sipx_addr)); 111} 112 113/* 114 * Verify the message is from the right port. 115 */ 116int 117ipxnet_portmatch(sipx) 118 register struct sockaddr_ipx *sipx; 119{ 120 121 return (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP ); 122} 123 124 125/* 126 * ipx output routine. 127 */ 128#ifdef DEBUG 129int do_output = 0; 130#endif 131void 132ipxnet_output(s, flags, sipx, size) 133 int s; 134 int flags; 135 struct sockaddr_ipx *sipx; 136 int size; 137{ 138 struct sockaddr_ipx dst; 139 140 dst = *sipx; 141 sipx = &dst; 142 if (sipx->sipx_addr.x_port == 0) 143 sipx->sipx_addr.x_port = htons(IPXPORT_RIP); 144#ifdef DEBUG 145 if(do_output || ntohs(msg->rip_cmd) == RIPCMD_REQUEST) 146#endif 147 /* 148 * Kludge to allow us to get routes out to machines that 149 * don't know their addresses yet; send to that address on 150 * ALL connected nets 151 */ 152 if (ipx_neteqnn(sipx->sipx_addr.x_net, ipx_zeronet)) { 153 extern struct interface *ifnet; 154 register struct interface *ifp; 155 156 for (ifp = ifnet; ifp; ifp = ifp->int_next) { 157 sipx->sipx_addr.x_net = 158 satoipx_addr(ifp->int_addr).x_net; 159 (void) sendto(s, msg, size, flags, 160 (struct sockaddr *)sipx, sizeof (*sipx)); 161 } 162 return; 163 } 164 165 (void) sendto(s, msg, size, flags, 166 (struct sockaddr *)sipx, sizeof (*sipx)); 167} 168 169/* 170 * Return 1 if we want this route. 171 * We use this to disallow route net G entries for one for multiple 172 * point to point links. 173 */ 174int 175ipxnet_checkhost(sipx) 176 struct sockaddr_ipx *sipx; 177{ 178 register struct interface *ifp = if_ifwithnet((struct sockaddr *)sipx); 179 /* 180 * We want this route if there is no more than one 181 * point to point interface with this network. 182 */ 183 if (ifp == 0 || (ifp->int_flags & IFF_POINTOPOINT)==0) return (1); 184 return (ifp->int_sq.n == ifp->int_sq.p); 185} 186 187/* 188 * Return 1 if the address is 189 * for a host, 0 for a network. 190 */ 191int 192ipxnet_ishost(sipx) 193struct sockaddr_ipx *sipx; 194{ 195 register u_short *s = sipx->sipx_addr.x_host.s_host; 196 197 if ((s[0]==0xffff) && (s[1]==0xffff) && (s[2]==0xffff)) 198 return (0); 199 else 200 return (1); 201} 202 203void 204ipxnet_canon(sipx) 205 struct sockaddr_ipx *sipx; 206{ 207 208 sipx->sipx_addr.x_port = 0; 209} 210 211void 212null_hash(addr, hp) 213 struct sockaddr *addr; 214 struct afhash *hp; 215{ 216 217 hp->afh_nethash = hp->afh_hosthash = 0; 218} 219 220int 221null_netmatch(a1, a2) 222 struct sockaddr *a1, *a2; 223{ 224 225 return (0); 226} 227 228void 229null_output(s, f, a1, n) 230 int s; 231 int f; 232 struct sockaddr *a1; 233 int n; 234{ 235 236 ; 237} 238 239int 240null_portmatch(a1) 241 struct sockaddr *a1; 242{ 243 244 return (0); 245} 246 247int 248null_portcheck(a1) 249 struct sockaddr *a1; 250{ 251 252 return (0); 253} 254 255int 256null_ishost(a1) 257 struct sockaddr *a1; 258{ 259 260 return (0); 261} 262 263int 264null_checkhost(a1) 265 struct sockaddr *a1; 266{ 267 268 return (0); 269} 270 271void 272null_canon(a1) 273 struct sockaddr *a1; 274{ 275 276 ; 277} 278 279