| 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_output.c,v 1.9 1999/08/28 01:15:04 peter Exp $ |
| 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 | */ |
| 46 | void |
| 47 | sap_supply_toall(int changesonly) |
| 48 | { |
| 49 | struct interface *ifp; |
| 50 | struct sockaddr dst; |
| 51 | struct sockaddr_ipx *ipx_dst; |
| 52 | int flags; |
| 53 | extern struct interface *ifnet; |
| 54 | |
| 55 | ipx_dst = (struct sockaddr_ipx *)&dst; |
| 56 | |
| 57 | for (ifp = ifnet; ifp; ifp = ifp->int_next) { |
| 58 | if (ifp->int_flags & IFF_PASSIVE) |
| 59 | continue; |
| 60 | |
| 61 | dst = ifp->int_flags & IFF_BROADCAST ? ifp->int_broadaddr : |
| 62 | ifp->int_flags & IFF_POINTOPOINT ? ifp->int_dstaddr : |
| 63 | ifp->int_addr; |
| 64 | |
| 65 | ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP); |
| 66 | |
| 67 | flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0; |
| 68 | sap_supply(&dst, flags, ifp, SAP_WILDCARD, changesonly); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | sapsndmsg(struct sockaddr *dst, int flags, struct interface *ifp, |
| 74 | int changesonly) |
| 75 | { |
| 76 | struct sockaddr t_dst; |
| 77 | struct sockaddr_ipx *ipx_dst; |
| 78 | |
| 79 | t_dst = *dst; |
| 80 | ipx_dst = (struct sockaddr_ipx *)&t_dst; |
| 81 | |
| 82 | if (ipx_dst->sipx_addr.x_port == 0) |
| 83 | ipx_dst->sipx_addr.x_port = htons(IPXPORT_SAP); |
| 84 | |
| 85 | (*afswitch[dst->sa_family].af_output) |
| 86 | (sapsock, flags, &t_dst, |
| 87 | sizeof (struct sap_packet) + sizeof(u_short)); |
| 88 | TRACE_SAP_OUTPUT(ifp, &t_dst, |
| 89 | sizeof (struct sap_packet) + sizeof(u_short)); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Supply dst with the contents of the SAP tables. If the ServType == |
| 94 | * SAP_WILDCARD (0xFFFF) supply the whole table, otherwise only the |
| 95 | * services that are of ServType. If this won't fit in one packet, chop |
| 96 | * it up into several. |
| 97 | * |
| 98 | * This must be done using the split horizon algorithm. |
| 99 | * 1. Don't send SAP info to the interface from where it was received. |
| 100 | * 2. If a service is received from more than one interface and the cost is |
| 101 | * the same, don't publish it on either interface. I am calling this |
| 102 | * clones. |
| 103 | */ |
| 104 | void |
| 105 | sap_supply(struct sockaddr *dst, int flags, struct interface *ifp, |
| 106 | int ServType, int changesonly) |
| 107 | { |
| 108 | struct sap_entry *sap; |
| 109 | struct sap_entry *csap; /* Clone route */ |
| 110 | struct sap_hash *sh; |
| 111 | struct sap_info *n = sap_msg->sap; |
| 112 | struct sap_hash *base = sap_head; |
| 113 | struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) dst; |
| 114 | af_output_t *output = afswitch[dst->sa_family].af_output; |
| 115 | int size, metric; |
| 116 | int delay = 0; |
| 117 | |
| 118 | if (sipx->sipx_port == 0) |
| 119 | sipx->sipx_port = htons(IPXPORT_SAP); |
| 120 | |
| 121 | sap_msg->sap_cmd = ntohs(SAP_RESP); |
| 122 | |
| 123 | for (sh = base; sh < &base[SAPHASHSIZ]; sh++) { |
| 124 | for (sap = sh->forw; sap != (struct sap_entry *)sh; |
| 125 | sap = sap->forw) { |
| 126 | size = (char *)n - (char *)sap_msg; |
| 127 | if (size >= |
| 128 | ((MAXSAPENTRIES * sizeof (struct sap_info)) + |
| 129 | sizeof (sap_msg->sap_cmd))) { |
| 130 | (*output)(sapsock, flags, dst, size); |
| 131 | TRACE_SAP_OUTPUT(ifp, dst, size); |
| 132 | n = sap_msg->sap; |
| 133 | delay++; |
| 134 | if(delay == 2) { |
| 135 | usleep(50000); |
| 136 | delay = 0; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (changesonly && !(sap->state & RTS_CHANGED)) |
| 141 | continue; |
| 142 | |
| 143 | /* |
| 144 | * Check for the servicetype except if the ServType is |
| 145 | * a wildcard (0xFFFF). |
| 146 | */ |
| 147 | if ((ServType != SAP_WILDCARD) && |
| 148 | (ServType != sap->sap.ServType)) |
| 149 | continue; |
| 150 | |
| 151 | /* |
| 152 | * This should do rule one and two of the split horizon |
| 153 | * algorithm. |
| 154 | */ |
| 155 | if (sap->ifp == ifp) |
| 156 | continue; |
| 157 | |
| 158 | /* |
| 159 | * Rule 2. |
| 160 | * Look if we have clones (different routes to the same |
| 161 | * place with exactly the same cost). |
| 162 | * |
| 163 | * We should not publish on any of the clone |
| 164 | * interfaces. |
| 165 | */ |
| 166 | csap = sap->clone; |
| 167 | while (csap) { |
| 168 | if (csap->ifp == ifp) |
| 169 | goto next; |
| 170 | csap = csap->clone; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Don't advertise services with more than 15 hops. It |
| 175 | * will be confused with a service that has gone down. |
| 176 | */ |
| 177 | if (ntohs(sap->sap.hops) == (HOPCNT_INFINITY - 1)) |
| 178 | continue; |
| 179 | metric = min(ntohs(sap->sap.hops) + 1, |
| 180 | HOPCNT_INFINITY); |
| 181 | |
| 182 | *n = sap->sap; |
| 183 | n->hops = htons(metric); |
| 184 | n++; |
| 185 | next: |
| 186 | ; |
| 187 | } |
| 188 | if (n != sap_msg->sap) { |
| 189 | size = (char *)n - (char *)sap_msg; |
| 190 | (*output)(sapsock, flags, dst, size); |
| 191 | TRACE_SAP_OUTPUT(ifp, dst, size); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |