d1b5b3d1f8b4ee1d2c61866731c74f70ffed4e3e
[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. 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  * $FreeBSD: src/usr.sbin/IPXrouted/af.c,v 1.6 1999/08/28 01:15:01 peter Exp $
39  * $DragonFly: src/usr.sbin/IPXrouted/af.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
40  *
41  * @(#)af.c     8.1 (Berkeley) 6/5/93
42  */
43
44 #include "defs.h"
45
46 /*
47  * Address family support routines
48  */
49 af_hash_t       null_hash;
50 af_netmatch_t   null_netmatch;
51 af_output_t     null_output;
52 af_portmatch_t  null_portmatch;
53 af_portcheck_t  null_portcheck;
54 af_checkhost_t  null_checkhost;
55 af_ishost_t     null_ishost;
56 af_canon_t      null_canon;
57
58 void    ipxnet_hash(struct sockaddr_ipx *, struct afhash *);
59 int     ipxnet_netmatch(struct sockaddr_ipx *, struct sockaddr_ipx *);
60 void    ipxnet_output(int, int, struct sockaddr_ipx *, int);
61 int     ipxnet_portmatch(struct sockaddr_ipx *);
62 int     ipxnet_checkhost(struct sockaddr_ipx *);
63 int     ipxnet_ishost(struct sockaddr_ipx *);
64 void    ipxnet_canon(struct sockaddr_ipx *);
65
66 #define NIL \
67         { null_hash,            null_netmatch,          null_output, \
68           null_portmatch,       null_portcheck,         null_checkhost, \
69           null_ishost,          null_canon }
70 #define IPXNET \
71         { (af_hash_t *)ipxnet_hash, \
72           (af_netmatch_t *)ipxnet_netmatch, \
73           (af_output_t *)ipxnet_output, \
74           (af_portmatch_t *)ipxnet_portmatch, \
75           (af_portcheck_t *)ipxnet_portmatch, \
76           (af_checkhost_t *)ipxnet_checkhost, \
77           (af_ishost_t *)ipxnet_ishost, \
78           (af_canon_t *)ipxnet_canon }
79
80 struct afswitch afswitch[AF_MAX] =
81         { NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
82           NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
83           NIL, NIL, NIL, IPXNET, NIL, NIL };
84
85 struct sockaddr_ipx ipxnet_default = { sizeof(struct sockaddr_ipx), AF_IPX };
86
87 union ipx_net ipx_anynet;
88 union ipx_net ipx_zeronet;
89
90 void
91 ipxnet_hash(sipx, hp)
92         register struct sockaddr_ipx *sipx;
93         struct afhash *hp;
94 {
95         long hash;
96 #if 0
97         u_short *s = sipx->sipx_addr.x_host.s_host;
98 #endif
99         u_char *c;
100
101         c = sipx->sipx_addr.x_net.c_net;
102
103 #define IMVAL   33
104         hash = 0;
105         hash = hash * IMVAL + *c++;
106         hash = hash * IMVAL + *c++;
107         hash = hash * IMVAL + *c++;
108         hash = hash * IMVAL + *c++;
109 #undef IMVAL
110
111         hp->afh_nethash = hash;
112         hp->afh_nethash ^= (hash >> 8);
113         hp->afh_nethash ^= (hash >> 16);
114         hp->afh_nethash ^= (hash >> 24);
115
116 #if 0
117         hash = 0;
118         hash = *s++; hash <<= 8; hash += *s++; hash <<= 8; hash += *s;
119         hp->afh_hosthash = hash;
120 #endif
121 }
122
123 int
124 ipxnet_netmatch(sxn1, sxn2)
125         struct sockaddr_ipx *sxn1, *sxn2;
126 {
127         return (ipx_neteq(sxn1->sipx_addr, sxn2->sipx_addr));
128 }
129
130 /*
131  * Verify the message is from the right port.
132  */
133 int
134 ipxnet_portmatch(sipx)
135         register struct sockaddr_ipx *sipx;
136 {
137         
138         return (ntohs(sipx->sipx_addr.x_port) == IPXPORT_RIP );
139 }
140
141
142 /*
143  * ipx output routine.
144  */
145 #ifdef DEBUG
146 int do_output = 0;
147 #endif
148 void
149 ipxnet_output(s, flags, sipx, size)
150         int s;
151         int flags;
152         struct sockaddr_ipx *sipx;
153         int size;
154 {
155         struct sockaddr_ipx dst;
156
157         dst = *sipx;
158         sipx = &dst;
159         if (sipx->sipx_addr.x_port == 0)
160                 sipx->sipx_addr.x_port = htons(IPXPORT_RIP);
161 #ifdef DEBUG
162         if(do_output || ntohs(msg->rip_cmd) == RIPCMD_REQUEST)
163 #endif  
164         /*
165          * Kludge to allow us to get routes out to machines that
166          * don't know their addresses yet; send to that address on
167          * ALL connected nets
168          */
169          if (ipx_neteqnn(sipx->sipx_addr.x_net, ipx_zeronet)) {
170                 extern  struct interface *ifnet;
171                 register struct interface *ifp;
172                 
173                 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
174                         sipx->sipx_addr.x_net = 
175                                 satoipx_addr(ifp->int_addr).x_net;
176                         (void) sendto(s, msg, size, flags,
177                             (struct sockaddr *)sipx, sizeof (*sipx));
178                 }
179                 return;
180         }
181         
182         (void) sendto(s, msg, size, flags,
183             (struct sockaddr *)sipx, sizeof (*sipx));
184 }
185
186 /*
187  * Return 1 if we want this route.
188  * We use this to disallow route net G entries for one for multiple
189  * point to point links.
190  */
191 int
192 ipxnet_checkhost(sipx)
193         struct sockaddr_ipx *sipx;
194 {
195         register struct interface *ifp = if_ifwithnet((struct sockaddr *)sipx);
196         /*
197          * We want this route if there is no more than one 
198          * point to point interface with this network.
199          */
200         if (ifp == 0 || (ifp->int_flags & IFF_POINTOPOINT)==0) return (1);
201         return (ifp->int_sq.n == ifp->int_sq.p);
202 }
203
204 /*
205  * Return 1 if the address is
206  * for a host, 0 for a network.
207  */
208 int
209 ipxnet_ishost(sipx)
210 struct sockaddr_ipx *sipx;
211 {
212         register u_short *s = sipx->sipx_addr.x_host.s_host;
213
214         if ((s[0]==0x0000) && (s[1]==0x0000) && (s[2]==0x0000))
215                 return (0);
216         if ((s[0]==0xffff) && (s[1]==0xffff) && (s[2]==0xffff))
217                 return (0);
218
219         return (1);
220 }
221
222 void
223 ipxnet_canon(sipx)
224         struct sockaddr_ipx *sipx;
225 {
226
227         sipx->sipx_addr.x_port = 0;
228 }
229
230 void
231 null_hash(addr, hp)
232         struct sockaddr *addr;
233         struct afhash *hp;
234 {
235
236         hp->afh_nethash = hp->afh_hosthash = 0;
237 }
238
239 int
240 null_netmatch(a1, a2)
241         struct sockaddr *a1, *a2;
242 {
243
244         return (0);
245 }
246
247 void
248 null_output(s, f, a1, n)
249         int s;
250         int f;
251         struct sockaddr *a1;
252         int n;
253 {
254
255         ;
256 }
257
258 int
259 null_portmatch(a1)
260         struct sockaddr *a1;
261 {
262
263         return (0);
264 }
265
266 int
267 null_portcheck(a1)
268         struct sockaddr *a1;
269 {
270
271         return (0);
272 }
273
274 int
275 null_ishost(a1)
276         struct sockaddr *a1;
277 {
278
279         return (0);
280 }
281
282 int
283 null_checkhost(a1)
284         struct sockaddr *a1;
285 {
286
287         return (0);
288 }
289
290 void
291 null_canon(a1)
292         struct sockaddr *a1;
293 {
294
295         ;
296 }
297