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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/* tr.c
token ring interface support
Contributed in May of 1999 by Andrew Chittenden */
/*
* Copyright (c) 2004-2005 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1996-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Internet Systems Consortium, Inc.
* 950 Charter Street
* Redwood City, CA 94063
* <info@isc.org>
* http://www.isc.org/
*/
#ifndef lint
static char copyright[] =
"$Id: tr.c,v 1.7.2.3 2005/09/08 21:19:21 dhankins Exp $ Copyright (c) 2004-2005 Internet Systems Consortium. All rights reserved.\n";
#endif /* not lint */
#include "dhcpd.h"
#if defined (HAVE_TR_SUPPORT) && \
(defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING))
#include "includes/netinet/ip.h"
#include "includes/netinet/udp.h"
#include "includes/netinet/if_ether.h"
#include "netinet/if_tr.h"
#include <sys/time.h>
/*
* token ring device handling subroutines. These are required as token-ring
* does not have a simple on-the-wire header but requires the use of
* source routing
*/
static int insert_source_routing PROTO ((struct trh_hdr *trh, struct interface_info* interface));
static void save_source_routing PROTO ((struct trh_hdr *trh, struct interface_info* interface));
static void expire_routes PROTO ((void));
/*
* As we keep a list of interesting routing information only, a singly
* linked list is all we need
*/
struct routing_entry {
struct routing_entry *next;
unsigned char addr[TR_ALEN];
unsigned char iface[5];
u_int16_t rcf; /* route control field */
u_int16_t rseg[8]; /* routing registers */
unsigned long access_time; /* time we last used this entry */
};
static struct routing_entry *routing_info = NULL;
static int routing_timeout = 10;
static struct timeval routing_timer;
void assemble_tr_header (interface, buf, bufix, to)
struct interface_info *interface;
unsigned char *buf;
unsigned *bufix;
struct hardware *to;
{
struct trh_hdr *trh;
int hdr_len;
struct trllc *llc;
/* set up the token header */
trh = (struct trh_hdr *) &buf[*bufix];
if (interface -> hw_address.hlen - 1 == sizeof (trh->saddr))
memcpy (trh->saddr, &interface -> hw_address.hbuf [1],
sizeof (trh->saddr));
else
memset (trh->saddr, 0x00, sizeof (trh->saddr));
if (to && to -> hlen == 7) /* XXX */
memcpy (trh->daddr, &to -> hbuf [1], sizeof trh->daddr);
else
memset (trh->daddr, 0xff, sizeof (trh->daddr));
hdr_len = insert_source_routing (trh, interface);
trh->ac = AC;
trh->fc = LLC_FRAME;
/* set up the llc header for snap encoding after the tr header */
llc = (struct trllc *)(buf + *bufix + hdr_len);
llc->dsap = EXTENDED_SAP;
llc->ssap = EXTENDED_SAP;
llc->llc = UI_CMD;
llc->protid[0] = 0;
llc->protid[1] = 0;
llc->protid[2] = 0;
llc->ethertype = htons(ETHERTYPE_IP);
hdr_len += sizeof(struct trllc);
*bufix += hdr_len;
}
static unsigned char tr_broadcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
/*
* decoding the token header is a bit complex as you can see here. It is
* further complicated by the linux kernel stripping off some valuable
* information (see comment below) even though we've asked for the raw
* packets.
*/
ssize_t decode_tr_header (interface, buf, bufix, from)
struct interface_info *interface;
unsigned char *buf;
unsigned bufix;
struct hardware *from;
{
struct trh_hdr *trh = (struct trh_hdr *) buf + bufix;
struct trllc *llc;
struct ip *ip;
struct udphdr *udp;
unsigned int route_len = 0;
ssize_t hdr_len;
struct timeval now;
/* see whether any source routing information has expired */
gettimeofday(&now, NULL);
if (routing_timer.tv_sec == 0)
routing_timer.tv_sec = now.tv_sec + routing_timeout;
else if ((now.tv_sec - routing_timer.tv_sec) > 0)
expire_routes();
/* the kernel might have stripped off the source
* routing bit. We try a heuristic to determine whether
* this is the case and put it back on if so
*/
route_len = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
llc = (struct trllc *)(buf + bufix + sizeof(struct trh_hdr)-TR_MAXRIFLEN+route_len);
if (llc->dsap == EXTENDED_SAP
&& llc->ssap == EXTENDED_SAP
&& llc->llc == UI_CMD
&& llc->protid[0] == 0
&& llc->protid[1] == 0
&& llc->protid[2] == 0) {
/* say there is source routing information present */
trh->saddr[0] |= TR_RII;
}
if (trh->saddr[0] & TR_RII)
route_len = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
else
route_len = 0;
hdr_len = sizeof (struct trh_hdr) - TR_MAXRIFLEN + route_len;
/* now filter out unwanted packets: this is based on the packet
* filter code in bpf.c */
llc = (struct trllc *)(buf + bufix + hdr_len);
ip = (struct ip *) (llc + 1);
udp = (struct udphdr *) ((unsigned char*) ip + IP_HL (ip));
/* make sure it is a snap encoded, IP, UDP, unfragmented packet sent
* to our port */
if (llc->dsap != EXTENDED_SAP
|| ntohs(llc->ethertype) != ETHERTYPE_IP
|| ip->ip_p != IPPROTO_UDP
|| (ntohs (ip->ip_off) & IP_OFFMASK) != 0
|| udp->uh_dport != local_port)
return -1;
/* only save source routing information for packets from valued hosts */
save_source_routing(trh, interface);
return hdr_len + sizeof (struct trllc);
}
/* insert_source_routing inserts source route information into the token ring
* header
*/
static int insert_source_routing (trh, interface)
struct trh_hdr *trh;
struct interface_info* interface;
{
struct routing_entry *rover;
struct timeval now;
unsigned int route_len = 0;
gettimeofday(&now, NULL);
/* single route broadcasts as per rfc 1042 */
if (memcmp(trh->daddr, tr_broadcast,TR_ALEN) == 0) {
trh->saddr[0] |= TR_RII;
trh->rcf = ((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK;
trh->rcf |= (TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
trh->rcf = htons(trh->rcf);
} else {
/* look for a routing entry */
for (rover = routing_info; rover != NULL; rover = rover->next) {
if (memcmp(rover->addr, trh->daddr, TR_ALEN) == 0)
break;
}
if (rover != NULL) {
/* success: route that frame */
if ((rover->rcf & TR_RCF_LEN_MASK) >> 8) {
u_int16_t rcf = rover->rcf;
memcpy(trh->rseg,rover->rseg,sizeof(trh->rseg));
rcf ^= TR_RCF_DIR_BIT;
rcf &= ~TR_RCF_BROADCAST_MASK;
trh->rcf = htons(rcf);
trh->saddr[0] |= TR_RII;
}
rover->access_time = now.tv_sec;
} else {
/* we don't have any routing information so send a
* limited broadcast */
trh->saddr[0] |= TR_RII;
trh->rcf = ((sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK;
trh->rcf |= (TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
trh->rcf = htons(trh->rcf);
}
}
/* return how much of the header we've actually used */
if (trh->saddr[0] & TR_RII)
route_len = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
else
route_len = 0;
return sizeof (struct trh_hdr) - TR_MAXRIFLEN + route_len;
}
/*
* save any source routing information
*/
static void save_source_routing(trh, interface)
struct trh_hdr *trh;
struct interface_info *interface;
{
struct routing_entry *rover;
struct timeval now;
unsigned char saddr[TR_ALEN];
u_int16_t rcf = 0;
gettimeofday(&now, NULL);
memcpy(saddr, trh->saddr, sizeof(saddr));
saddr[0] &= 0x7f; /* strip off source routing present flag */
/* scan our table to see if we've got it */
for (rover = routing_info; rover != NULL; rover = rover->next) {
if (memcmp(&rover->addr[0], &saddr[0], TR_ALEN) == 0)
break;
}
/* found an entry so update it with fresh information */
if (rover != NULL) {
if ((trh->saddr[0] & TR_RII) &&
((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2) {
rcf = ntohs(trh->rcf);
rcf &= ~TR_RCF_BROADCAST_MASK;
memcpy(rover->rseg, trh->rseg, sizeof(rover->rseg));
}
rover->rcf = rcf;
rover->access_time = now.tv_sec;
return; /* that's all folks */
}
/* no entry found, so create one */
rover = dmalloc (sizeof (struct routing_entry), MDL);
if (rover == NULL) {
fprintf(stderr,
"%s: unable to save source routing information\n",
__FILE__);
return;
}
memcpy(rover->addr, saddr, sizeof(rover->addr));
memcpy(rover->iface, interface->name, 5);
rover->access_time = now.tv_sec;
if (trh->saddr[0] & TR_RII) {
if (((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2) {
rcf = ntohs(trh->rcf);
rcf &= ~TR_RCF_BROADCAST_MASK;
memcpy(rover->rseg, trh->rseg, sizeof(rover->rseg));
}
rover->rcf = rcf;
}
/* insert into list */
rover->next = routing_info;
routing_info = rover;
return;
}
/*
* get rid of old routes
*/
static void expire_routes()
{
struct routing_entry *rover;
struct routing_entry **prover = &routing_info;
struct timeval now;
gettimeofday(&now, NULL);
while((rover = *prover) != NULL) {
if ((now.tv_sec - rover->access_time) > routing_timeout) {
*prover = rover->next;
dfree (rover, MDL);
} else
prover = &rover->next;
}
/* Reset the timer */
routing_timer.tv_sec = now.tv_sec + routing_timeout;
routing_timer.tv_usec = now.tv_usec;
}
#endif
|