1/*
2 * Copyright (c) 1995 John Hay.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 *    must display the following acknowledgement:
14 *	This product includes software developed by John Hay.
15 * 4. Neither the name of the author nor the names of any co-contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY John Hay AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL John Hay OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $FreeBSD$
32 */
33
34/*
35 * Routing Table Management Daemon
36 */
37#include <unistd.h>
38#include "defs.h"
39
40/*
41 * Apply the function "f" to all non-passive
42 * interfaces.  If the interface supports the
43 * use of broadcasting use it, otherwise address
44 * the output to the known router.
45 */
46void
47sap_supply_toall(changesonly)
48	int changesonly;
49{
50	register struct interface *ifp;
51	struct sockaddr dst;
52	register struct sockaddr_ipx *ipx_dst;
53	register int flags;
54	extern struct interface *ifnet;
55
56	ipx_dst = (struct sockaddr_ipx *)&dst;
57
58	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
59		if (ifp->int_flags & IFF_PASSIVE)
60			continue;
61
62		dst = ifp->int_flags & IFF_BROADCAST ? ifp->int_broadaddr :
63		      ifp->int_flags & IFF_POINTOPOINT ? ifp->int_dstaddr :
64		      ifp->int_addr;
65
66		ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);
67
68		flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
69		sap_supply(&dst, flags, ifp, SAP_WILDCARD, changesonly);
70	}
71}
72
73void
74sapsndmsg(dst, flags, ifp, changesonly)
75	struct sockaddr *dst;
76	int flags;
77	struct interface *ifp;
78	int changesonly;
79{
80	struct sockaddr t_dst;
81	struct sockaddr_ipx *ipx_dst;
82
83	t_dst = *dst;
84	ipx_dst = (struct sockaddr_ipx *)&t_dst;
85
86	if (ipx_dst->sipx_addr.x_port == 0)
87		ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);
88
89        (*afswitch[dst->sa_family].af_output)
90		(sapsock, flags, &t_dst,
91		sizeof (struct sap_packet) + sizeof(u_short));
92	TRACE_SAP_OUTPUT(ifp, &t_dst,
93			 sizeof (struct sap_packet) + sizeof(u_short));
94}
95
96/*
97 * Supply dst with the contents of the SAP tables. If the ServType ==
98 * SAP_WILDCARD (0xFFFF) supply the whole table, otherwise only the
99 * services that are of ServType. If this won't fit in one packet, chop
100 * it up into several.
101 *
102 * This must be done using the split horizon algorithm.
103 * 1. Don't send SAP info to the interface from where it was received.
104 * 2. If a service is received from more than one interface and the cost is
105 *    the same, don't publish it on either interface. I am calling this
106 *    clones.
107 */
108void
109sap_supply(dst, flags, ifp, ServType, changesonly)
110	struct sockaddr *dst;
111	int flags;
112	struct interface *ifp;
113	int ServType;
114	int changesonly;
115{
116	register struct sap_entry *sap;
117	register struct sap_entry *csap; /* Clone route */
118	register struct sap_hash *sh;
119	register struct sap_info *n = sap_msg->sap;
120	struct sap_hash *base = sap_head;
121	struct sockaddr_ipx *sipx =  (struct sockaddr_ipx *) dst;
122	af_output_t *output = afswitch[dst->sa_family].af_output;
123	int size, metric;
124	int delay = 0;
125
126	if (sipx->sipx_port == 0)
127		sipx->sipx_port = htons(IPXPORT_SAP);
128
129	sap_msg->sap_cmd = ntohs(SAP_RESP);
130
131	for (sh = base; sh < &base[SAPHASHSIZ]; sh++)
132	for (sap = sh->forw; sap != (struct sap_entry *)sh; sap = sap->forw) {
133		size = (char *)n - (char *)sap_msg;
134		if (size >= ((MAXSAPENTRIES * sizeof (struct sap_info)) +
135				sizeof (sap_msg->sap_cmd))) {
136			(*output)(sapsock, flags, dst, size);
137			TRACE_SAP_OUTPUT(ifp, dst, size);
138			n = sap_msg->sap;
139			delay++;
140			if(delay == 2) {
141				usleep(50000);
142				delay = 0;
143			}
144		}
145
146		if (changesonly && !(sap->state & RTS_CHANGED))
147			continue;
148
149		/*
150		 * Check for the servicetype except if the ServType is
151		 * a wildcard (0xFFFF).
152		 */
153		if ((ServType != SAP_WILDCARD) &&
154		    (ServType != sap->sap.ServType))
155			continue;
156
157		/*
158		 * This should do rule one and two of the split horizon
159		 * algorithm.
160		 */
161		if (sap->ifp == ifp)
162			continue;
163
164		/*
165		 * Rule 2.
166		 * Look if we have clones (different routes to the same
167		 * place with exactly the same cost).
168		 *
169		 * We should not publish on any of the clone interfaces.
170		 */
171		csap = sap->clone;
172		while (csap) {
173			if (csap->ifp == ifp)
174				goto next;
175			csap = csap->clone;
176		}
177
178		/*
179		 * Don't advertise services with more than 15 hops. It
180		 * will be confused with a service that has gone down.
181		 */
182		if (ntohs(sap->sap.hops) == (HOPCNT_INFINITY - 1))
183			continue;
184		metric = min(ntohs(sap->sap.hops) + 1, HOPCNT_INFINITY);
185
186		*n = sap->sap;
187		n->hops = htons(metric);
188		n++;
189next:
190		;
191	}
192	if (n != sap_msg->sap) {
193		size = (char *)n - (char *)sap_msg;
194		(*output)(sapsock, flags, dst, size);
195		TRACE_SAP_OUTPUT(ifp, dst, size);
196	}
197}
198
199