| 1 |
/* process.c Handle processing and routing of AX25 frames |
| 2 |
* |
| 3 |
* Copyright 1991, Michael Westerhof, Sun Microsystems, Inc. |
| 4 |
* This software may be freely used, distributed, or modified, providing |
| 5 |
* this header is not removed. |
| 6 |
* |
| 7 |
* This is the only module that knows about the internal structure of |
| 8 |
* AX25 frames. |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* Dual port additions by M.Durrant VE3PNX and D.J.Dionne Feb 4, 1995 |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
#include <syslog.h> |
| 18 |
|
| 19 |
#include "ax25ipd.h" |
| 20 |
|
| 21 |
/* if dual port the upper nibble will have a value of 1 (not 0) */ |
| 22 |
#define FROM_PORT2(p) (((*(p+1))&0x10)!=0) |
| 23 |
#define FOR_PORT2(p) (addrmatch(p,mycallsign2) || addrmatch(p,myalias2)) |
| 24 |
/* ve3djf and ve3pnx addition above */ |
| 25 |
#define IS_LAST(p) (((*(p+6))&0x01)!=0) |
| 26 |
#define NOT_LAST(p) (((*(p+6))&0x01)==0) |
| 27 |
#define REPEATED(p) (((*(p+6))&0x80)!=0) |
| 28 |
#define NOTREPEATED(p) (((*(p+6))&0x80)==0) |
| 29 |
#define IS_ME(p) (addrmatch(p,mycallsign) || addrmatch(p,myalias) || addrmatch(p,mycallsign2) || addrmatch(p,myalias2) ) |
| 30 |
#define NOT_ME(p) (!(addrmatch(p,mycallsign) || addrmatch(p,myalias) || addrmatch(p,mycallsign2) || addrmatch(p,myalias2) ) ) |
| 31 |
#define ARE_DIGIS(f) (((*(f+13))&0x01)==0) |
| 32 |
#define NO_DIGIS(f) (((*(f+13))&0x01)!=0) |
| 33 |
#define SETREPEATED(p) (*(p+6))|=0x80 |
| 34 |
#define SETLAST(p) (*(p+6))|=0x01 |
| 35 |
|
| 36 |
static unsigned char bcbuf[256]; /* Must be larger than bc_text!!! */ |
| 37 |
static int bclen; /* The size of bcbuf */ |
| 38 |
|
| 39 |
/* |
| 40 |
* Initialize the process variables |
| 41 |
*/ |
| 42 |
|
| 43 |
void process_init(void) |
| 44 |
{ |
| 45 |
bclen = -1; /* flag that we need to rebuild the bctext */ |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* handle a frame given us by the kiss routines. The buf variable is |
| 50 |
* a pointer to an AX25 frame. Note that the AX25 frame from kiss does |
| 51 |
* not include the CRC bytes. These are computed by this routine, and |
| 52 |
* it is expected that the buffer we have has room for the CRC bytes. |
| 53 |
* We will either dump this frame, or send it via the IP interface. |
| 54 |
* |
| 55 |
* If we are in digi mode, we validate in several ways: |
| 56 |
* a) we must be the next digi in line to pick up the packet |
| 57 |
* b) the next site to get the packet (the next listed digi, or |
| 58 |
* the destination if we are the last digi) must be known to |
| 59 |
* us via the route table. |
| 60 |
* If we pass validation, we then set the digipeated bit for our entry |
| 61 |
* in the packet, compute the CRC, and send the packet to the IP |
| 62 |
* interface. |
| 63 |
* |
| 64 |
* If we are in tnc mode, we have less work to do. |
| 65 |
* a) the next site to get the packet (the next listed digi, or |
| 66 |
* the destination) must be known to us via the route table. |
| 67 |
* If we pass validation, we compute the CRC, and send the packet to |
| 68 |
* the IP interface. |
| 69 |
*/ |
| 70 |
|
| 71 |
void from_kiss(unsigned char *buf, int l) |
| 72 |
{ |
| 73 |
unsigned char *a, *ipaddr; |
| 74 |
|
| 75 |
if (l < 15) { |
| 76 |
LOGL2("from_kiss: dumped - length wrong!\n"); |
| 77 |
stats.kiss_tooshort++; |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if (loglevel > 2) |
| 82 |
dump_ax25frame("from_kiss: ", buf, l); |
| 83 |
|
| 84 |
if (digi) { /* if we are in digi mode */ |
| 85 |
a = next_addr(buf); |
| 86 |
if (NOT_ME(a)) { |
| 87 |
stats.kiss_not_for_me++; |
| 88 |
LOGL4("from_kiss: (digi) dumped - not for me\n"); |
| 89 |
return; |
| 90 |
} |
| 91 |
if (a == buf) { /* must be a digi */ |
| 92 |
stats.kiss_i_am_dest++; |
| 93 |
LOGL2 |
| 94 |
("from_kiss: (digi) dumped - I am destination!\n"); |
| 95 |
return; |
| 96 |
} |
| 97 |
SETREPEATED(a); |
| 98 |
a = next_addr(buf); /* find who gets it after us */ |
| 99 |
} else { /* must be tnc mode */ |
| 100 |
a = next_addr(buf); |
| 101 |
#ifdef TNC_FILTER |
| 102 |
if (IS_ME(a)) { |
| 103 |
LOGL2 |
| 104 |
("from_kiss: (tnc) dumped - addressed to self!\n"); |
| 105 |
return; |
| 106 |
} |
| 107 |
#endif |
| 108 |
} /* end of tnc mode */ |
| 109 |
|
| 110 |
/* Lookup the IP address for this route */ |
| 111 |
ipaddr = call_to_ip(a); |
| 112 |
|
| 113 |
if (ipaddr == NULL) { |
| 114 |
if (is_call_bcast(a)) { |
| 115 |
/* Warning - assuming buffer has room for 2 bytes */ |
| 116 |
add_crc(buf, l); |
| 117 |
l += 2; |
| 118 |
send_broadcast(buf, l); |
| 119 |
} else { |
| 120 |
stats.kiss_no_ip_addr++; |
| 121 |
LOGL2 |
| 122 |
("from_kiss: dumped - cannot figure out where to send this!\n"); |
| 123 |
} |
| 124 |
return; |
| 125 |
} else { |
| 126 |
/* Warning - assuming buffer has room for 2 bytes */ |
| 127 |
add_crc(buf, l); |
| 128 |
l += 2; |
| 129 |
send_ip(buf, l, ipaddr); |
| 130 |
if (is_call_bcast(a)) { |
| 131 |
send_broadcast(buf, l); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* handle a frame given us by the IP routines. The buf variable is |
| 138 |
* a pointer to an AX25 frame. |
| 139 |
* Note that the frame includes the CRC bytes, which we dump ASAP. |
| 140 |
* We will either dump this frame, or send it via the KISS interface. |
| 141 |
* |
| 142 |
* If we are in digi mode, we only validate that: |
| 143 |
* a) we must be the next digi in line to pick up the packet |
| 144 |
* If we pass validation, we then set the digipeated bit for our entry |
| 145 |
* in the packet, and send the packet to the KISS send routine. |
| 146 |
* |
| 147 |
* If we are in tnc mode, we validate pretty well nothing, just like a |
| 148 |
* real TNC... #define FILTER_TNC will change this. |
| 149 |
* We simply send the packet to the KISS send routine. |
| 150 |
*/ |
| 151 |
|
| 152 |
void from_ip(unsigned char *buf, int l) |
| 153 |
{ |
| 154 |
int port = 0; |
| 155 |
unsigned char *a; |
| 156 |
|
| 157 |
if (!ok_crc(buf, l)) { |
| 158 |
stats.ip_failed_crc++; |
| 159 |
LOGL2("from_ip: dumped - CRC incorrect!\n"); |
| 160 |
return; |
| 161 |
} |
| 162 |
l = l - 2; /* dump the blasted CRC */ |
| 163 |
|
| 164 |
if (l < 15) { |
| 165 |
stats.ip_tooshort++; |
| 166 |
LOGL2("from_ip: dumped - length wrong!\n"); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
if (loglevel > 2) |
| 171 |
dump_ax25frame("from_ip: ", buf, l); |
| 172 |
|
| 173 |
if (digi) { /* if we are in digi mode */ |
| 174 |
a = next_addr(buf); |
| 175 |
if (NOT_ME(a)) { |
| 176 |
stats.ip_not_for_me++; |
| 177 |
LOGL2("from_ip: (digi) dumped - not for me!\n"); |
| 178 |
return; |
| 179 |
} |
| 180 |
if (a == buf) { /* must be a digi */ |
| 181 |
stats.ip_i_am_dest++; |
| 182 |
LOGL2 |
| 183 |
("from_ip: (digi) dumped - I am destination!\n"); |
| 184 |
return; |
| 185 |
} |
| 186 |
if (dual_port == 1 && FOR_PORT2(a)) { |
| 187 |
port = 0x10; |
| 188 |
} |
| 189 |
SETREPEATED(a); |
| 190 |
} else { /* must be tnc mode */ |
| 191 |
a = next_addr(buf); |
| 192 |
#ifdef TNC_FILTER |
| 193 |
if (NOT_ME(a)) { |
| 194 |
LOGL2 |
| 195 |
("from_ip: (tnc) dumped - I am not destination!\n"); |
| 196 |
return; |
| 197 |
} |
| 198 |
#endif |
| 199 |
} /* end of tnc mode */ |
| 200 |
if (!ttyfd_bpq) |
| 201 |
send_kiss(port, buf, l); |
| 202 |
else { |
| 203 |
send_bpq(buf, l); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/* |
| 208 |
* Send an ID frame out the KISS port. |
| 209 |
*/ |
| 210 |
|
| 211 |
void do_beacon(void) |
| 212 |
{ |
| 213 |
int i; |
| 214 |
unsigned char *p; |
| 215 |
|
| 216 |
if (bclen == 0) |
| 217 |
return; /* nothing to do! */ |
| 218 |
|
| 219 |
if (bclen < 0) { /* build the id string */ |
| 220 |
p = bcbuf; |
| 221 |
*p++ = ('I' << 1); |
| 222 |
*p++ = ('D' << 1); |
| 223 |
*p++ = (' ' << 1); |
| 224 |
*p++ = (' ' << 1); |
| 225 |
*p++ = (' ' << 1); |
| 226 |
*p++ = (' ' << 1); |
| 227 |
*p++ = '\0' | 0x60; /* SSID, set reserved bits */ |
| 228 |
|
| 229 |
for (i = 0; i < 6; i++) |
| 230 |
*p++ = mycallsign[i]; |
| 231 |
*p++ = mycallsign[6] | 0x60; /* ensure reserved bits are set */ |
| 232 |
SETLAST(bcbuf + 7); /* Set the E bit -- last address */ |
| 233 |
|
| 234 |
*p++ = 0x03; /* Control field -- UI frame */ |
| 235 |
|
| 236 |
*p++ = 0xf0; /* Protocol ID -- 0xf0 is no protocol */ |
| 237 |
|
| 238 |
strcpy((char *)p, bc_text); /* add the text field */ |
| 239 |
|
| 240 |
bclen = 16 + strlen(bc_text); /* adjust the length nicely */ |
| 241 |
} |
| 242 |
|
| 243 |
if (loglevel > 2) |
| 244 |
dump_ax25frame("do_beacon: ", bcbuf, bclen); |
| 245 |
stats.kiss_beacon_outs++; |
| 246 |
if (!ttyfd_bpq) |
| 247 |
send_kiss(0, bcbuf, bclen); |
| 248 |
else { |
| 249 |
send_bpq(bcbuf, bclen); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* return true if the addresses supplied match |
| 255 |
* modified for wildcarding by vk5xxx |
| 256 |
*/ |
| 257 |
int addrmatch(unsigned char *a, unsigned char *b) |
| 258 |
{ |
| 259 |
if ((*a == '\0') || (*b == '\0')) |
| 260 |
return 0; |
| 261 |
|
| 262 |
if ((*a++ ^ *b++) & 0xfe) |
| 263 |
return 0; /* "K" */ |
| 264 |
if ((*a++ ^ *b++) & 0xfe) |
| 265 |
return 0; /* "A" */ |
| 266 |
if ((*a++ ^ *b++) & 0xfe) |
| 267 |
return 0; /* "9" */ |
| 268 |
if ((*a++ ^ *b++) & 0xfe) |
| 269 |
return 0; /* "W" */ |
| 270 |
if ((*a++ ^ *b++) & 0xfe) |
| 271 |
return 0; /* "S" */ |
| 272 |
if ((*a++ ^ *b++) & 0xfe) |
| 273 |
return 0; /* "B" */ |
| 274 |
if (((*b) & 0x1e) == 0) |
| 275 |
return 1; /* ssid 0 matches all ssid's */ |
| 276 |
if ((*a++ ^ *b) & 0x1e) |
| 277 |
return 0; /* ssid */ |
| 278 |
/* if ((*a++^*b++)&0x1e)return 0; ssid (how it was ...) */ |
| 279 |
return 1; |
| 280 |
} |
| 281 |
|
| 282 |
/* |
| 283 |
* return pointer to the next station to get this packet |
| 284 |
*/ |
| 285 |
unsigned char *next_addr(unsigned char *f) |
| 286 |
{ |
| 287 |
unsigned char *a; |
| 288 |
|
| 289 |
/* If no digis, return the destination address */ |
| 290 |
if (NO_DIGIS(f)) |
| 291 |
return f; |
| 292 |
|
| 293 |
/* check each digi field. The first one that hasn't seen it is the one */ |
| 294 |
a = f + 7; |
| 295 |
do { |
| 296 |
a += 7; |
| 297 |
if (NOTREPEATED(a)) |
| 298 |
return a; |
| 299 |
} |
| 300 |
while (NOT_LAST(a)); |
| 301 |
|
| 302 |
/* all the digis have seen it. return the destination address */ |
| 303 |
return f; |
| 304 |
} |
| 305 |
|
| 306 |
/* |
| 307 |
* tack on the CRC for the frame. Note we assume the buffer is long |
| 308 |
* enough to have the two bytes tacked on. |
| 309 |
*/ |
| 310 |
void add_crc(unsigned char *buf, int l) |
| 311 |
{ |
| 312 |
unsigned short int u; |
| 313 |
|
| 314 |
u = compute_crc(buf, l); |
| 315 |
buf[l] = u & 0xff; /* lsb first */ |
| 316 |
buf[l + 1] = (u >> 8) & 0xff; /* msb next */ |
| 317 |
} |
| 318 |
|
| 319 |
/* |
| 320 |
* Dump AX25 frame. |
| 321 |
*/ |
| 322 |
void dump_ax25frame(char *t, unsigned char *buf, int l) |
| 323 |
{ |
| 324 |
#ifdef DEBUG |
| 325 |
int i; |
| 326 |
#endif |
| 327 |
unsigned char *a; |
| 328 |
|
| 329 |
printf("%s AX25: (l=%3d) ", t, l); |
| 330 |
|
| 331 |
if (l < 15) { |
| 332 |
printf("Bogus size...\n"); |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
printf("%s -> ", call_to_a(buf + 7)); |
| 337 |
printf("%s", call_to_a(buf)); |
| 338 |
|
| 339 |
if (ARE_DIGIS(buf)) { |
| 340 |
printf(" v"); |
| 341 |
a = buf + 7; |
| 342 |
do { |
| 343 |
a += 7; |
| 344 |
printf(" %s", call_to_a(a)); |
| 345 |
if (REPEATED(a)) |
| 346 |
printf("*"); |
| 347 |
} |
| 348 |
while (NOT_LAST(a)); |
| 349 |
} |
| 350 |
|
| 351 |
printf("\n"); |
| 352 |
|
| 353 |
#ifdef DEBUG |
| 354 |
for (i = 0; i < l; i++) |
| 355 |
printf("%02x ", buf[i]); |
| 356 |
printf("\n"); |
| 357 |
#endif |
| 358 |
|
| 359 |
fflush(stdout); |
| 360 |
} |