summaryrefslogtreecommitdiffstats
path: root/usr.sbin/IPXrouted/sap_output.c
blob: 1de02598512be474c3f81c6345d604ab4ec25363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (c) 1995 John Hay.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by John Hay.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY John Hay AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL John Hay OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id: sap_output.c,v 1.4 1996/04/13 15:13:24 jhay Exp $
 */

/*
 * Routing Table Management Daemon
 */
#include <unistd.h>
#include "defs.h"

/*
 * Apply the function "f" to all non-passive
 * interfaces.  If the interface supports the
 * use of broadcasting use it, otherwise address
 * the output to the known router.
 */
void
sap_supply_toall(void)
{
	register struct interface *ifp;
	struct sockaddr dst;
	register struct sockaddr_ipx *ipx_dst;
	register int flags;
	extern struct interface *ifnet;

	ipx_dst = (struct sockaddr_ipx *)&dst;

	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
		if (ifp->int_flags & IFF_PASSIVE)
			continue;

		dst = ifp->int_flags & IFF_BROADCAST ? ifp->int_broadaddr :
		      ifp->int_flags & IFF_POINTOPOINT ? ifp->int_dstaddr :
		      ifp->int_addr;

		ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);

		flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
		sap_supply(&dst, flags, ifp, SAP_WILDCARD);
	}
}

void 
sapsndmsg(dst, flags, ifp)
	struct sockaddr *dst;
	int flags;
	struct interface *ifp;
{
	struct sockaddr t_dst;
	struct sockaddr_ipx *ipx_dst;

	t_dst = *dst;
	ipx_dst = (struct sockaddr_ipx *)&t_dst;

	if (ipx_dst->sipx_addr.x_port == 0)
		ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP);

        (*afswitch[dst->sa_family].af_output)
		(sapsock, flags, &t_dst, 
		sizeof (struct sap_packet) + sizeof(u_short));
	TRACE_SAP_OUTPUT(ifp, &t_dst, 
			 sizeof (struct sap_packet) + sizeof(u_short));
}

/*
 * Supply dst with the contents of the SAP tables. If the ServType ==
 * SAP_WILDCARD (0xFFFF) supply the whole table, otherwise only the
 * services that are of ServType. If this won't fit in one packet, chop 
 * it up into several.
 *
 * This must be done using the split horizon algorithm.
 * 1. Don't send SAP info to the interface from where it was received.
 * 2. If a service is received from more than one interface and the cost is
 *    the same, don't publish it on either interface. I am calling this
 *    clones.
 */
void
sap_supply(dst, flags, ifp, ServType)
	struct sockaddr *dst;
	int flags;
	struct interface *ifp;
	int ServType;
{
	register struct sap_entry *sap;
	register struct sap_entry *csap; /* Clone route */
	register struct sap_hash *sh;
	register struct sap_info *n = sap_msg->sap;
	struct sap_hash *base = sap_head;
	struct sockaddr_ipx *sipx =  (struct sockaddr_ipx *) dst;
	af_output_t *output = afswitch[dst->sa_family].af_output;
	int size, metric;
	int delay = 0;

	if (sipx->sipx_port == 0)
		sipx->sipx_port = htons(IPXPORT_SAP);

	sap_msg->sap_cmd = ntohs(SAP_RESP);

	for (sh = base; sh < &base[SAPHASHSIZ]; sh++)
	for (sap = sh->forw; sap != (struct sap_entry *)sh; sap = sap->forw) {
		size = (char *)n - (char *)sap_msg;
		if (size >= ((MAXSAPENTRIES * sizeof (struct sap_info)) +
				sizeof (sap_msg->sap_cmd))) {
			(*output)(sapsock, flags, dst, size);
			TRACE_SAP_OUTPUT(ifp, dst, size);
			n = sap_msg->sap;
			delay++;
			if(delay == 2) {
				usleep(20000);
				delay = 0;
			}
		}

		/*
		 * Check for the servicetype except if the ServType is
		 * a wildcard (0xFFFF).
		 */
		if ((ServType != SAP_WILDCARD) &&
		    (ServType != sap->sap.ServType))
			continue;

		/*
		 * This should do rule one and two of the split horizon
		 * algorithm.
		 */
		if (sap->ifp == ifp)
			continue;

		/*
		 * Rule 2.
		 * Look if we have clones (different routes to the same
		 * place with exactly the same cost).
		 *
		 * We should not publish on any of the clone interfaces.
		 */
		csap = sap->clone;
		while (csap) {
			if (csap->ifp == ifp)
				goto next;
			csap = csap->clone;
		}

		/*
		 * Don't advertise services with more than 15 hops. It
		 * will be confused with a service that has gone down.
		 */
		if (ntohs(sap->sap.hops) == (HOPCNT_INFINITY - 1))
			continue;
		metric = min(ntohs(sap->sap.hops) + 1, HOPCNT_INFINITY);

		*n = sap->sap;
		n->hops = htons(metric);
		n++;
next:
	}
	if (n != sap_msg->sap) {
		size = (char *)n - (char *)sap_msg;
		(*output)(sapsock, flags, dst, size);
		TRACE_SAP_OUTPUT(ifp, dst, size);
	}
}

OpenPOWER on IntegriCloud