| 1 |
#include <unistd.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <time.h> |
| 5 |
#include <string.h> |
| 6 |
#include <signal.h> |
| 7 |
#include <syslog.h> |
| 8 |
#include <errno.h> |
| 9 |
#include <fcntl.h> |
| 10 |
|
| 11 |
#include <sys/types.h> |
| 12 |
#include <sys/ioctl.h> |
| 13 |
#include <sys/param.h> |
| 14 |
#include <sys/stat.h> |
| 15 |
|
| 16 |
#include <sys/socket.h> |
| 17 |
|
| 18 |
#include <net/ethernet.h> |
| 19 |
|
| 20 |
#include <netinet/in.h> |
| 21 |
|
| 22 |
#include <netax25/ax25.h> |
| 23 |
#include <netrose/rose.h> |
| 24 |
|
| 25 |
|
| 26 |
#include <netax25/axlib.h> |
| 27 |
#include <netax25/axconfig.h> |
| 28 |
#include <netax25/daemon.h> |
| 29 |
#include <netax25/mheard.h> |
| 30 |
|
| 31 |
#include <config.h> |
| 32 |
#include <scm-version.h> |
| 33 |
|
| 34 |
#include "../pathnames.h" |
| 35 |
|
| 36 |
#define KISS_MASK 0x0F |
| 37 |
#define KISS_DATA 0x00 |
| 38 |
|
| 39 |
#define PID_SEGMENT 0x08 |
| 40 |
#define PID_ARP 0xCD |
| 41 |
#define PID_NETROM 0xCF |
| 42 |
#define PID_IP 0xCC |
| 43 |
#define PID_ROSE 0x01 |
| 44 |
#define PID_TEXNET 0xC3 |
| 45 |
#define PID_FLEXNET 0xCE |
| 46 |
#define PID_TEXT 0xF0 |
| 47 |
#define PID_PSATFT 0xBB |
| 48 |
#define PID_PSATPB 0xBD |
| 49 |
|
| 50 |
#define I 0x00 |
| 51 |
#define S 0x01 |
| 52 |
#define RR 0x01 |
| 53 |
#define RNR 0x05 |
| 54 |
#define REJ 0x09 |
| 55 |
#define U 0x03 |
| 56 |
#define SABM 0x2F |
| 57 |
#define SABME 0x6F |
| 58 |
#define DISC 0x43 |
| 59 |
#define DM 0x0F |
| 60 |
#define UA 0x63 |
| 61 |
#define FRMR 0x87 |
| 62 |
#define UI 0x03 |
| 63 |
|
| 64 |
#define PF 0x10 |
| 65 |
#define EPF 0x01 |
| 66 |
|
| 67 |
#define MMASK 7 |
| 68 |
|
| 69 |
#define HDLCAEB 0x01 |
| 70 |
#define SSID 0x1E |
| 71 |
#define SSSID_SPARE 0x40 |
| 72 |
#define ESSID_SPARE 0x20 |
| 73 |
|
| 74 |
#define ALEN 6 |
| 75 |
#define AXLEN 7 |
| 76 |
|
| 77 |
struct mheard_list_struct { |
| 78 |
int in_use; |
| 79 |
struct mheard_struct entry; |
| 80 |
long position; |
| 81 |
}; |
| 82 |
|
| 83 |
static struct mheard_list_struct *mheard_list; |
| 84 |
#define MHEARD_LIST_SIZE 1000 |
| 85 |
static int mheard_list_size = MHEARD_LIST_SIZE/10; |
| 86 |
static int logging = FALSE; |
| 87 |
|
| 88 |
static int ftype(char *, int *, int); |
| 89 |
static struct mheard_list_struct *findentry(ax25_address *, char *); |
| 90 |
|
| 91 |
static void terminate(int sig) |
| 92 |
{ |
| 93 |
if (logging) { |
| 94 |
syslog(LOG_INFO, "terminating on SIGTERM\n"); |
| 95 |
closelog(); |
| 96 |
} |
| 97 |
|
| 98 |
exit(0); |
| 99 |
} |
| 100 |
|
| 101 |
int main(int argc, char **argv) |
| 102 |
{ |
| 103 |
struct mheard_list_struct *mheard; |
| 104 |
char buffer[1500]; |
| 105 |
char *data; |
| 106 |
int size, s; |
| 107 |
char *port = NULL; |
| 108 |
struct sockaddr sa; |
| 109 |
socklen_t asize; |
| 110 |
long position; |
| 111 |
int ctlen, type, end, extseq, flush = FALSE; |
| 112 |
FILE *fp; |
| 113 |
char *p; |
| 114 |
char ports[1024]; |
| 115 |
int ports_excl = 0; |
| 116 |
|
| 117 |
*ports = 0; |
| 118 |
while ((s = getopt(argc, argv, "fln:p:v")) != -1) { |
| 119 |
switch (s) { |
| 120 |
case 'l': |
| 121 |
logging = TRUE; |
| 122 |
break; |
| 123 |
case 'f': |
| 124 |
flush = TRUE; |
| 125 |
break; |
| 126 |
case 'n': |
| 127 |
mheard_list_size = atoi(optarg); |
| 128 |
if (mheard_list_size < 10 || mheard_list_size > MHEARD_LIST_SIZE) { |
| 129 |
fprintf(stderr, "mheardd: list size must be between 10 and %d\n", MHEARD_LIST_SIZE); |
| 130 |
return 1; |
| 131 |
} |
| 132 |
break; |
| 133 |
case 'p': |
| 134 |
if (strlen(optarg) > sizeof(ports)-4) { |
| 135 |
fprintf(stderr, "mheardd: too many ports specified."); |
| 136 |
return 1; |
| 137 |
} |
| 138 |
if (*optarg == '!') { |
| 139 |
ports_excl = 1; |
| 140 |
optarg++; |
| 141 |
} |
| 142 |
sprintf(ports, "|%s|", optarg); |
| 143 |
for (p = ports; *p; p++) { |
| 144 |
if (*p == ' ' || *p == ',') |
| 145 |
*p = '|'; |
| 146 |
} |
| 147 |
break; |
| 148 |
case 'v': |
| 149 |
printf("mheardd: %s\n", FULL_VER); |
| 150 |
return 0; |
| 151 |
case ':': |
| 152 |
fprintf(stderr, "mheardd: option -n needs an argument\n"); |
| 153 |
return 1; |
| 154 |
case '?': |
| 155 |
fprintf(stderr, "Usage: mheardd [-f] [-l] [-n number] [-p [!]port1[,port2,..]] [-v]\n"); |
| 156 |
return 1; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
signal(SIGTERM, terminate); |
| 161 |
|
| 162 |
if (ax25_config_load_ports() == 0) { |
| 163 |
fprintf(stderr, "mheardd: no AX.25 port data configured\n"); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
mheard_list = calloc(mheard_list_size, |
| 168 |
sizeof(struct mheard_list_struct)); |
| 169 |
if (mheard_list == NULL) { |
| 170 |
fprintf(stderr, "mheardd: cannot allocate memory\n"); |
| 171 |
return 1; |
| 172 |
} |
| 173 |
|
| 174 |
if (flush) |
| 175 |
unlink(DATA_MHEARD_FILE); |
| 176 |
|
| 177 |
/* Load an existing heard list */ |
| 178 |
if ((fp = fopen(DATA_MHEARD_FILE, "r")) != NULL) { |
| 179 |
s = 0; |
| 180 |
position = ftell(fp); |
| 181 |
|
| 182 |
while (fread(buffer, sizeof(struct mheard_struct), 1, fp) == 1 && s < mheard_list_size) { |
| 183 |
memcpy(&mheard_list[s].entry, buffer, sizeof(struct mheard_struct)); |
| 184 |
mheard_list[s].in_use = TRUE; |
| 185 |
mheard_list[s].position = position; |
| 186 |
position = ftell(fp); |
| 187 |
s++; |
| 188 |
} |
| 189 |
|
| 190 |
fclose(fp); |
| 191 |
} else { |
| 192 |
fp = fopen(DATA_MHEARD_FILE, "w"); |
| 193 |
if (fp != NULL) |
| 194 |
fclose(fp); |
| 195 |
} |
| 196 |
|
| 197 |
s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_AX25)); |
| 198 |
if (s == -1) { |
| 199 |
perror("mheardd: socket"); |
| 200 |
return 1; |
| 201 |
} |
| 202 |
|
| 203 |
if (!daemon_start(FALSE)) { |
| 204 |
fprintf(stderr, "mheardd: cannot become a daemon\n"); |
| 205 |
return 1; |
| 206 |
} |
| 207 |
|
| 208 |
/* Use syslog for error messages rather than perror/fprintf */ |
| 209 |
if (logging) { |
| 210 |
openlog("mheardd", LOG_PID, LOG_DAEMON); |
| 211 |
syslog(LOG_INFO, "starting"); |
| 212 |
} |
| 213 |
|
| 214 |
for (;;) { |
| 215 |
asize = sizeof(sa); |
| 216 |
|
| 217 |
size = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &asize); |
| 218 |
if (size == -1) { |
| 219 |
if (logging) { |
| 220 |
syslog(LOG_ERR, "recv: %m"); |
| 221 |
closelog(); |
| 222 |
} |
| 223 |
return 1; |
| 224 |
} |
| 225 |
|
| 226 |
port = ax25_config_get_name(sa.sa_data); |
| 227 |
if (port == NULL) { |
| 228 |
if (logging) |
| 229 |
syslog(LOG_WARNING, "unknown port '%s'\n", sa.sa_data); |
| 230 |
continue; |
| 231 |
} |
| 232 |
if (*ports) { |
| 233 |
char testport[sizeof(sa.sa_data)+2]; |
| 234 |
sprintf(testport, "|%s|", sa.sa_data); |
| 235 |
if (ports_excl) { |
| 236 |
if (strstr(ports, testport)) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
} else { |
| 240 |
if (!strstr(ports, testport)) { |
| 241 |
continue; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
data = buffer; |
| 247 |
|
| 248 |
if ((*data & KISS_MASK) != KISS_DATA) |
| 249 |
continue; |
| 250 |
|
| 251 |
data++; |
| 252 |
size--; |
| 253 |
|
| 254 |
if (size < (AXLEN + AXLEN + 1)) { |
| 255 |
if (logging) |
| 256 |
syslog(LOG_WARNING, "packet too short\n"); |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
mheard = findentry((ax25_address *)(data + AXLEN), port); |
| 261 |
|
| 262 |
if (!ax25_validate(data + 0) || !ax25_validate(data + AXLEN)) { |
| 263 |
if (logging) |
| 264 |
syslog(LOG_WARNING, "invalid callsign on port %s\n", port); |
| 265 |
continue; |
| 266 |
} |
| 267 |
|
| 268 |
memcpy(&mheard->entry.from_call, data + AXLEN, sizeof(ax25_address)); |
| 269 |
memcpy(&mheard->entry.to_call, data + 0, sizeof(ax25_address)); |
| 270 |
strcpy(mheard->entry.portname, port); |
| 271 |
mheard->entry.ndigis = 0; |
| 272 |
|
| 273 |
extseq = ((data[AXLEN + ALEN] & SSSID_SPARE) != SSSID_SPARE); |
| 274 |
end = (data[AXLEN + ALEN] & HDLCAEB); |
| 275 |
|
| 276 |
data += (AXLEN + AXLEN); |
| 277 |
size -= (AXLEN + AXLEN); |
| 278 |
|
| 279 |
while (!end) { |
| 280 |
memcpy(&mheard->entry.digis[mheard->entry.ndigis], data, sizeof(ax25_address)); |
| 281 |
mheard->entry.ndigis++; |
| 282 |
|
| 283 |
end = (data[ALEN] & HDLCAEB); |
| 284 |
|
| 285 |
data += AXLEN; |
| 286 |
size -= AXLEN; |
| 287 |
} |
| 288 |
|
| 289 |
if (size == 0) { |
| 290 |
if (logging) |
| 291 |
syslog(LOG_WARNING, "packet too short\n"); |
| 292 |
continue; |
| 293 |
} |
| 294 |
|
| 295 |
ctlen = ftype(data, &type, extseq); |
| 296 |
|
| 297 |
mheard->entry.count++; |
| 298 |
|
| 299 |
switch (type) { |
| 300 |
case SABM: |
| 301 |
mheard->entry.type = MHEARD_TYPE_SABM; |
| 302 |
mheard->entry.uframes++; |
| 303 |
break; |
| 304 |
case SABME: |
| 305 |
mheard->entry.type = MHEARD_TYPE_SABME; |
| 306 |
mheard->entry.uframes++; |
| 307 |
break; |
| 308 |
case DISC: |
| 309 |
mheard->entry.type = MHEARD_TYPE_DISC; |
| 310 |
mheard->entry.uframes++; |
| 311 |
break; |
| 312 |
case UA: |
| 313 |
mheard->entry.type = MHEARD_TYPE_UA; |
| 314 |
mheard->entry.uframes++; |
| 315 |
break; |
| 316 |
case DM: |
| 317 |
mheard->entry.type = MHEARD_TYPE_DM; |
| 318 |
mheard->entry.uframes++; |
| 319 |
break; |
| 320 |
case RR: |
| 321 |
mheard->entry.type = MHEARD_TYPE_RR; |
| 322 |
mheard->entry.sframes++; |
| 323 |
break; |
| 324 |
case RNR: |
| 325 |
mheard->entry.type = MHEARD_TYPE_RNR; |
| 326 |
mheard->entry.sframes++; |
| 327 |
break; |
| 328 |
case REJ: |
| 329 |
mheard->entry.type = MHEARD_TYPE_REJ; |
| 330 |
mheard->entry.sframes++; |
| 331 |
break; |
| 332 |
case FRMR: |
| 333 |
mheard->entry.type = MHEARD_TYPE_FRMR; |
| 334 |
mheard->entry.uframes++; |
| 335 |
break; |
| 336 |
case I: |
| 337 |
mheard->entry.type = MHEARD_TYPE_I; |
| 338 |
mheard->entry.iframes++; |
| 339 |
break; |
| 340 |
case UI: |
| 341 |
mheard->entry.type = MHEARD_TYPE_UI; |
| 342 |
mheard->entry.uframes++; |
| 343 |
break; |
| 344 |
default: |
| 345 |
if (logging) |
| 346 |
syslog(LOG_WARNING, "unknown packet type %02X\n", *data); |
| 347 |
mheard->entry.type = MHEARD_TYPE_UNKNOWN; |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
data += ctlen; |
| 352 |
size -= ctlen; |
| 353 |
|
| 354 |
if (type == I || type == UI) { |
| 355 |
unsigned char pid = *data; |
| 356 |
|
| 357 |
switch (pid) { |
| 358 |
case PID_TEXT: |
| 359 |
mheard->entry.mode |= MHEARD_MODE_TEXT; |
| 360 |
break; |
| 361 |
case PID_SEGMENT: |
| 362 |
mheard->entry.mode |= MHEARD_MODE_SEGMENT; |
| 363 |
break; |
| 364 |
case PID_ARP: |
| 365 |
mheard->entry.mode |= MHEARD_MODE_ARP; |
| 366 |
break; |
| 367 |
case PID_NETROM: |
| 368 |
mheard->entry.mode |= MHEARD_MODE_NETROM; |
| 369 |
break; |
| 370 |
case PID_IP: |
| 371 |
mheard->entry.mode |= (type == I) ? MHEARD_MODE_IP_VC : MHEARD_MODE_IP_DG; |
| 372 |
break; |
| 373 |
case PID_ROSE: |
| 374 |
mheard->entry.mode |= MHEARD_MODE_ROSE; |
| 375 |
break; |
| 376 |
case PID_TEXNET: |
| 377 |
mheard->entry.mode |= MHEARD_MODE_TEXNET; |
| 378 |
break; |
| 379 |
case PID_FLEXNET: |
| 380 |
mheard->entry.mode |= MHEARD_MODE_FLEXNET; |
| 381 |
break; |
| 382 |
case PID_PSATPB: |
| 383 |
mheard->entry.mode |= MHEARD_MODE_PSATPB; |
| 384 |
break; |
| 385 |
case PID_PSATFT: |
| 386 |
mheard->entry.mode |= MHEARD_MODE_PSATFT; |
| 387 |
break; |
| 388 |
default: |
| 389 |
if (logging) |
| 390 |
syslog(LOG_WARNING, "unknown PID %02X\n", *data); |
| 391 |
mheard->entry.mode |= MHEARD_MODE_UNKNOWN; |
| 392 |
break; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
if (mheard->entry.first_heard == 0) |
| 397 |
time(&mheard->entry.first_heard); |
| 398 |
|
| 399 |
time(&mheard->entry.last_heard); |
| 400 |
|
| 401 |
fp = fopen(DATA_MHEARD_FILE, "r+"); |
| 402 |
if (fp == NULL) { |
| 403 |
if (logging) |
| 404 |
syslog(LOG_ERR, "cannot open mheard data file\n"); |
| 405 |
continue; |
| 406 |
} |
| 407 |
|
| 408 |
if (mheard->position == 0xFFFFFF) { |
| 409 |
fseek(fp, 0L, SEEK_END); |
| 410 |
mheard->position = ftell(fp); |
| 411 |
} |
| 412 |
|
| 413 |
fseek(fp, mheard->position, SEEK_SET); |
| 414 |
|
| 415 |
fwrite(&mheard->entry, sizeof(struct mheard_struct), 1, fp); |
| 416 |
|
| 417 |
fclose(fp); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
static int ftype(char *data, int *type, int extseq) |
| 422 |
{ |
| 423 |
if (extseq) { |
| 424 |
if ((*data & 0x01) == 0) { /* An I frame is an I-frame ... */ |
| 425 |
*type = I; |
| 426 |
return 2; |
| 427 |
} |
| 428 |
if (*data & 0x02) { |
| 429 |
*type = *data & ~PF; |
| 430 |
return 1; |
| 431 |
} else { |
| 432 |
*type = *data; |
| 433 |
return 2; |
| 434 |
} |
| 435 |
} else { |
| 436 |
if ((*data & 0x01) == 0) { /* An I frame is an I-frame ... */ |
| 437 |
*type = I; |
| 438 |
return 1; |
| 439 |
} |
| 440 |
if (*data & 0x02) { /* U-frames use all except P/F bit for type */ |
| 441 |
*type = *data & ~PF; |
| 442 |
return 1; |
| 443 |
} else { /* S-frames use low order 4 bits for type */ |
| 444 |
*type = *data & 0x0F; |
| 445 |
return 1; |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
static struct mheard_list_struct *findentry(ax25_address *callsign, char *port) |
| 451 |
{ |
| 452 |
struct mheard_list_struct *oldest = NULL; |
| 453 |
int i; |
| 454 |
|
| 455 |
for (i = 0; i < mheard_list_size; i++) |
| 456 |
if (mheard_list[i].in_use && |
| 457 |
ax25_cmp(&mheard_list[i].entry.from_call, callsign) == 0 && |
| 458 |
strcmp(mheard_list[i].entry.portname, port) == 0) |
| 459 |
return mheard_list + i; |
| 460 |
|
| 461 |
for (i = 0; i < mheard_list_size; i++) { |
| 462 |
if (!mheard_list[i].in_use) { |
| 463 |
mheard_list[i].in_use = TRUE; |
| 464 |
mheard_list[i].position = 0xFFFFFF; |
| 465 |
return mheard_list + i; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
for (i = 0; i < mheard_list_size; i++) { |
| 470 |
if (mheard_list[i].in_use) { |
| 471 |
if (oldest == NULL) { |
| 472 |
oldest = mheard_list + i; |
| 473 |
} else { |
| 474 |
if (mheard_list[i].entry.last_heard < oldest->entry.last_heard) |
| 475 |
oldest = mheard_list + i; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
memset(&oldest->entry, 0x00, sizeof(struct mheard_struct)); |
| 481 |
|
| 482 |
return oldest; |
| 483 |
} |