route.8: Remove NS remains.
[dragonfly.git] / usr.sbin / IPXrouted / sap_input.c
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: src/usr.sbin/IPXrouted/sap_input.c,v 1.7 1999/08/28 01:15:04 peter Exp $
32  */
33
34 /*
35  * IPX Routing Table Management Daemon
36  */
37 #include "defs.h"
38
39 int dognreply = 1;
40
41 /*
42  * Process a newly received packet.
43  */
44 void
45 sap_input(struct sockaddr *from, int size)
46 {
47         int newsize;
48         int sapchanged = 0;
49         struct sap_entry *sap;
50         struct sap_info *n;
51         struct interface *ifp = NULL;
52         struct afswitch *afp;
53         struct sockaddr_ipx *ipxp;
54
55         ifp = if_ifwithnet(from);
56         ipxp = (struct sockaddr_ipx *)from;
57         if (ifp == NULL) {
58                 if(ftrace) {
59                         fprintf(ftrace, "Received bogus packet from %s\n",
60                                 ipxdp_ntoa(&ipxp->sipx_addr));
61                 }
62                 return;
63         }
64
65         if (ftrace) 
66                 dumpsappacket(ftrace, "received", from, (char *)sap_msg , size);
67
68         if (from->sa_family >= AF_MAX)
69                 return;
70         afp = &afswitch[from->sa_family];
71         
72         size -= sizeof (u_short)        /* command */;
73         n = sap_msg->sap;
74
75         switch (ntohs(sap_msg->sap_cmd)) {
76
77         case SAP_REQ_NEAR:
78                 if (ftrace)
79                         fprintf(ftrace, "Received a sap REQ_NEAR packet.\n");
80                 if (!dognreply)
81                         return;
82                 sap = sap_nearestserver(n->ServType, ifp);
83                 if (sap == NULL)
84                         return;
85                 sap_msg->sap_cmd = htons(SAP_RESP_NEAR);
86                 *n = sap->sap;
87                 n->hops = htons(ntohs(n->hops) + 1);
88                 if (ntohs(n->hops) >= HOPCNT_INFINITY)
89                         return;
90
91                 newsize = sizeof(struct sap_info) + sizeof(struct sap_packet);
92                 (*afp->af_output)(sapsock, 0, from, newsize);
93                 if (ftrace) {
94                         fprintf(ftrace, "sap_nearestserver %X %s returned:\n",
95                                 ntohs(n->ServType),
96                                 ifp->int_name);
97                         fprintf(ftrace, "  service %04X %-20.20s "
98                                         "addr %s.%04X metric %d\n",
99                                         ntohs(sap->sap.ServType),
100                                         sap->sap.ServName,
101                                         ipxdp_ntoa(&sap->sap.ipx),
102                                         ntohs(sap->sap.ipx.x_port),
103                                         ntohs(sap->sap.hops));
104                 }
105                 return;
106
107         case SAP_REQ:
108                 if (ftrace)
109                         fprintf(ftrace, "Received a sap REQ packet.\n");
110
111                 sap_supply(from, 0, ifp, n->ServType, 0);
112                 return;
113
114         case SAP_RESP_NEAR:
115                 /* XXX We do nothing here, for the moment.
116                  * Maybe we should check if the service is in our table?
117                  *
118                  */
119                 if (ftrace)
120                         fprintf(ftrace, "Received a sap RESP_NEAR packet.\n");
121
122                 return;
123
124         case SAP_RESP:
125                 if (ftrace)
126                         fprintf(ftrace, "Received a sap RESP packet.\n");
127
128                 (*afp->af_canon)(from);
129
130                 for (; size > 0; size -= sizeof (struct sap_info), n++) {
131                         if (size < sizeof (struct netinfo))
132                                 break;
133                         /*
134                          * The idea here is that if the hop count is more
135                          * than INFINITY it is bogus and should be discarded.
136                          * If it is equal to INFINITY it is a message to say
137                          * that a service went down. If we don't allready
138                          * have it in our tables discard it. Otherwise
139                          * update our table and set the timer to EXPIRE_TIME
140                          * so that it is removed next time we go through the
141                          * tables.
142                          */
143                         if (ntohs(n->hops) > HOPCNT_INFINITY)
144                                 continue;
145                         sap = sap_lookup(n->ServType, n->ServName);
146                         if (sap == NULL) {
147                                 if (ntohs(n->hops) == HOPCNT_INFINITY)
148                                         continue;
149                                 sap_add(n, from);
150                                 sapchanged = 1;
151                                 continue;
152                         }
153
154                         /*
155                          * A clone is a different route to the same service
156                          * with exactly the same cost (metric).
157                          * They must all be recorded because those interfaces
158                          * must be handled in the same way as the first route
159                          * to that service. ie When using the split horizon
160                          * algorithm we must look at these interfaces also.
161                          *
162                          * Update if from gateway and different,
163                          * from anywhere and less hops or
164                          * getting stale and equivalent.
165                          */
166                         if (((ifp != sap->ifp) ||
167                              !equal(&sap->source, from)) &&
168                             (n->hops == sap->sap.hops) &&
169                             (ntohs(n->hops) != HOPCNT_INFINITY)) {
170                                 struct sap_entry *tsap = sap->clone;
171
172                                 while (tsap) {
173                                         if ((ifp == tsap->ifp) &&
174                                             equal(&tsap->source, from)) {
175                                                 tsap->timer = 0;
176                                                 break;
177                                         }
178                                         tsap = tsap->clone;
179                                 }
180                                 if (tsap == NULL) {
181                                         sap_add_clone(sap, n, from);
182                                 }
183                                 continue;
184                         }
185                         if ((ifp == sap->ifp) &&
186                             equal(&sap->source, from) &&
187                             (ntohs(n->hops) == ntohs(sap->sap.hops)))
188                                 sap->timer = 0;
189                         else if (((ifp == sap->ifp) &&
190                                   equal(&sap->source, from) &&
191                                   (n->hops != sap->sap.hops)) ||
192                                  (ntohs(n->hops) < ntohs(sap->sap.hops)) ||
193                                  (sap->timer > (EXPIRE_TIME*2/3) &&
194                                   ntohs(sap->sap.hops) == ntohs(n->hops) &&
195                                   ntohs(n->hops) != HOPCNT_INFINITY)) {
196                                 sap_change(sap, n, from);
197                                 sapchanged = 1;
198                         }
199                 }
200                 if (sapchanged) {
201                         struct sap_entry *sap;
202                         struct sap_hash *sh;
203                         sap_supply_toall(1);
204
205                         for (sh = sap_head; sh < &sap_head[SAPHASHSIZ]; sh++)
206                                 for (sap = sh->forw;
207                                     sap != (struct sap_entry *)sh;
208                                     sap = sap->forw)
209                                         sap->state &= ~RTS_CHANGED;
210                 }
211                 return;
212         }
213 }