32 #if defined (HAVE_TR_SUPPORT) && \ 33 (defined (PACKET_ASSEMBLY) || defined (PACKET_DECODING)) 37 #include "netinet/if_tr.h" 46 static int insert_source_routing (
struct trh_hdr *trh,
struct interface_info* interface);
47 static void save_source_routing (
struct trh_hdr *trh,
struct interface_info* interface);
48 static void expire_routes (
void);
54 struct routing_entry {
55 struct routing_entry *next;
56 unsigned char addr[TR_ALEN];
57 unsigned char iface[5];
60 unsigned long access_time;
63 static struct routing_entry *routing_info = NULL;
65 static int routing_timeout = 10;
66 static struct timeval routing_timer;
80 trh = (
struct trh_hdr *) &buf[*bufix];
81 if (interface -> hw_address.hlen - 1 == sizeof (trh->saddr))
82 memcpy (trh->saddr, &interface -> hw_address.hbuf [1],
85 memset (trh->saddr, 0x00, sizeof (trh->saddr));
87 if (to && to -> hlen == 7)
88 memcpy (trh->daddr, &to -> hbuf [1],
sizeof trh->daddr);
90 memset (trh->daddr, 0xff, sizeof (trh->daddr));
92 hdr_len = insert_source_routing (trh, interface);
98 llc = (
struct trllc *)(buf + *bufix + hdr_len);
99 llc->dsap = EXTENDED_SAP;
100 llc->ssap = EXTENDED_SAP;
107 hdr_len +=
sizeof(
struct trllc);
113 static unsigned char tr_broadcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
127 struct trh_hdr *trh = (
struct trh_hdr *) buf + bufix;
131 unsigned int route_len = 0;
136 gettimeofday(&now, NULL);
138 if (routing_timer.tv_sec == 0)
139 routing_timer.tv_sec = now.tv_sec + routing_timeout;
140 else if ((now.tv_sec - routing_timer.tv_sec) > 0)
147 route_len = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
148 llc = (
struct trllc *)(buf + bufix +
sizeof(
struct trh_hdr)-TR_MAXRIFLEN+route_len);
149 if (llc->dsap == EXTENDED_SAP
150 && llc->ssap == EXTENDED_SAP
151 && llc->llc == UI_CMD
152 && llc->protid[0] == 0
153 && llc->protid[1] == 0
154 && llc->protid[2] == 0) {
156 trh->saddr[0] |= TR_RII;
159 if (trh->saddr[0] & TR_RII)
160 route_len = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
164 hdr_len =
sizeof (
struct trh_hdr) - TR_MAXRIFLEN + route_len;
168 llc = (
struct trllc *)(buf + bufix + hdr_len);
169 ip = (
struct ip *) (llc + 1);
170 udp = (
struct udphdr *) ((
unsigned char*) ip +
IP_HL (ip));
174 if (llc->dsap != EXTENDED_SAP
176 || ip->
ip_p != IPPROTO_UDP
182 save_source_routing(trh, interface);
184 return hdr_len +
sizeof (
struct trllc);
190 static int insert_source_routing (trh, interface)
194 struct routing_entry *rover;
196 unsigned int route_len = 0;
198 gettimeofday(&now, NULL);
201 if (memcmp(trh->daddr, tr_broadcast,TR_ALEN) == 0) {
202 trh->saddr[0] |= TR_RII;
203 trh->rcf = ((
sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK;
204 trh->rcf |= (TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
205 trh->rcf = htons(trh->rcf);
208 for (rover = routing_info; rover != NULL; rover = rover->next) {
209 if (memcmp(rover->addr, trh->daddr, TR_ALEN) == 0)
215 if ((rover->rcf & TR_RCF_LEN_MASK) >> 8) {
216 u_int16_t rcf = rover->rcf;
217 memcpy(trh->rseg,rover->rseg,
sizeof(trh->rseg));
218 rcf ^= TR_RCF_DIR_BIT;
219 rcf &= ~TR_RCF_BROADCAST_MASK;
220 trh->rcf = htons(rcf);
221 trh->saddr[0] |= TR_RII;
223 rover->access_time = now.tv_sec;
227 trh->saddr[0] |= TR_RII;
228 trh->rcf = ((
sizeof(trh->rcf)) << 8) & TR_RCF_LEN_MASK;
229 trh->rcf |= (TR_RCF_FRAME2K | TR_RCF_LIMITED_BROADCAST);
230 trh->rcf = htons(trh->rcf);
235 if (trh->saddr[0] & TR_RII)
236 route_len = (ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8;
240 return sizeof (
struct trh_hdr) - TR_MAXRIFLEN + route_len;
246 static void save_source_routing(trh, interface)
250 struct routing_entry *rover;
252 unsigned char saddr[TR_ALEN];
255 gettimeofday(&now, NULL);
257 memcpy(saddr, trh->saddr,
sizeof(saddr));
261 for (rover = routing_info; rover != NULL; rover = rover->next) {
262 if (memcmp(&rover->addr[0], &saddr[0], TR_ALEN) == 0)
268 if ((trh->saddr[0] & TR_RII) &&
269 ((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2) {
270 rcf = ntohs(trh->rcf);
271 rcf &= ~TR_RCF_BROADCAST_MASK;
272 memcpy(rover->rseg, trh->rseg,
sizeof(rover->rseg));
275 rover->access_time = now.tv_sec;
280 rover =
dmalloc (
sizeof (
struct routing_entry),
MDL);
283 "%s: unable to save source routing information\n",
288 memcpy(rover->addr, saddr,
sizeof(rover->addr));
289 memcpy(rover->iface, interface->
name, 5);
290 rover->access_time = now.tv_sec;
291 if (trh->saddr[0] & TR_RII) {
292 if (((ntohs(trh->rcf) & TR_RCF_LEN_MASK) >> 8) > 2) {
293 rcf = ntohs(trh->rcf);
294 rcf &= ~TR_RCF_BROADCAST_MASK;
295 memcpy(rover->rseg, trh->rseg,
sizeof(rover->rseg));
301 rover->next = routing_info;
302 routing_info = rover;
310 static void expire_routes()
312 struct routing_entry *rover;
313 struct routing_entry **prover = &routing_info;
316 gettimeofday(&now, NULL);
318 while((rover = *prover) != NULL) {
319 if ((now.tv_sec - rover->access_time) > routing_timeout) {
320 *prover = rover->next;
323 prover = &rover->next;
327 routing_timer.tv_sec = now.tv_sec + routing_timeout;
328 routing_timer.tv_usec = now.tv_usec;
void * dmalloc(unsigned, const char *, int)
ssize_t decode_tr_header(struct interface_info *, unsigned char *, unsigned, struct hardware *)
void assemble_tr_header(struct interface_info *, unsigned char *, unsigned *, struct hardware *)
void dfree(void *, const char *, int)