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