| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <termios.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <signal.h> |
| 8 | #include <ctype.h> |
| 9 | #include <netdb.h> |
| 10 | #include <syslog.h> |
| 11 | #include <errno.h> |
| 12 | |
| 13 | #include <config.h> |
| 14 | #include <scm-version.h> |
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/ioctl.h> |
| 20 | |
| 21 | #include <net/if.h> |
| 22 | #include <netax25/ax25.h> |
| 23 | #include <netrose/rose.h> |
| 24 | |
| 25 | #include <netax25/daemon.h> |
| 26 | #include <netax25/axlib.h> |
| 27 | #include <netax25/ttyutils.h> |
| 28 | |
| 29 | #include "../pathnames.h" |
| 30 | |
| 31 | #ifndef N_6PACK |
| 32 | #define N_6PACK 7 /* This is valid for all architectures in 2.2.x */ |
| 33 | #endif |
| 34 | |
| 35 | static char *callsign; |
| 36 | static int speed; |
| 37 | static int mtu; |
| 38 | static int logging = FALSE; |
| 39 | static char *progname; |
| 40 | static char *kttyname; |
| 41 | static char *portname; |
| 42 | static char *inetaddr; |
| 43 | static int allow_broadcast; |
| 44 | static int i_am_unix98_pty_master; /* unix98 ptmx support */ |
| 45 | |
| 46 | static char *kiss_basename(char *s) |
| 47 | { |
| 48 | char *p = strrchr(s, '/'); |
| 49 | return p ? p + 1 : s; |
| 50 | } |
| 51 | |
| 52 | static void terminate(int sig) |
| 53 | { |
| 54 | if (logging) { |
| 55 | syslog(LOG_INFO, "terminating on SIGTERM\n"); |
| 56 | closelog(); |
| 57 | } |
| 58 | |
| 59 | if (!i_am_unix98_pty_master) |
| 60 | tty_unlock(kttyname); |
| 61 | |
| 62 | exit(0); |
| 63 | } |
| 64 | |
| 65 | static int readconfig(char *port) |
| 66 | { |
| 67 | FILE *fp; |
| 68 | char buffer[90], *s; |
| 69 | int n = 0; |
| 70 | |
| 71 | fp = fopen(CONF_AXPORTS_FILE, "r"); |
| 72 | if (fp == NULL) { |
| 73 | fprintf(stderr, "%s: cannot open axports file %s\n", |
| 74 | progname, CONF_AXPORTS_FILE); |
| 75 | return FALSE; |
| 76 | } |
| 77 | |
| 78 | while (fgets(buffer, 90, fp) != NULL) { |
| 79 | n++; |
| 80 | |
| 81 | s = strchr(buffer, '\n'); |
| 82 | if (s != NULL) |
| 83 | *s = '\0'; |
| 84 | |
| 85 | if (*buffer == 0 || *buffer == '#') |
| 86 | continue; |
| 87 | |
| 88 | s = strtok(buffer, " \t\r\n"); |
| 89 | if (s == NULL) { |
| 90 | fprintf(stderr, "%s: unable to parse line %d of the axports file\n", progname, n); |
| 91 | return FALSE; |
| 92 | } |
| 93 | |
| 94 | if (strcmp(s, port) != 0) |
| 95 | continue; |
| 96 | |
| 97 | s = strtok(NULL, " \t\r\n"); |
| 98 | if (s == NULL) { |
| 99 | fprintf(stderr, "%s: unable to parse line %d of the axports file\n", progname, n); |
| 100 | return FALSE; |
| 101 | } |
| 102 | |
| 103 | callsign = strdup(s); |
| 104 | |
| 105 | s = strtok(NULL, " \t\r\n"); |
| 106 | if (s == NULL) { |
| 107 | fprintf(stderr, "%s: unable to parse line %d of the axports file\n", progname, n); |
| 108 | return FALSE; |
| 109 | } |
| 110 | |
| 111 | speed = atoi(s); |
| 112 | |
| 113 | s = strtok(NULL, " \t\r\n"); |
| 114 | if (s == NULL) { |
| 115 | fprintf(stderr, "%s: unable to parse line %d of the axports file\n", progname, n); |
| 116 | return FALSE; |
| 117 | } |
| 118 | |
| 119 | if (mtu == 0) { |
| 120 | mtu = atoi(s); |
| 121 | if (mtu <= 0) { |
| 122 | fprintf(stderr, "%s: invalid paclen setting\n", progname); |
| 123 | return FALSE; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | fclose(fp); |
| 128 | |
| 129 | return TRUE; |
| 130 | } |
| 131 | |
| 132 | fclose(fp); |
| 133 | |
| 134 | fprintf(stderr, "%s: cannot find port %s in axports\n", progname, port); |
| 135 | |
| 136 | return FALSE; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | static int setifcall(int fd, char *name) |
| 141 | { |
| 142 | char call[7]; |
| 143 | |
| 144 | if (ax25_aton_entry(name, call) == -1) |
| 145 | return FALSE; |
| 146 | |
| 147 | if (ioctl(fd, SIOCSIFHWADDR, call) != 0) { |
| 148 | close(fd); |
| 149 | fprintf(stderr, "%s: ", progname); |
| 150 | perror("SIOCSIFHWADDR"); |
| 151 | return FALSE; |
| 152 | } |
| 153 | |
| 154 | return TRUE; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static int startiface(char *dev, struct hostent *hp) |
| 159 | { |
| 160 | struct ifreq ifr; |
| 161 | int fd; |
| 162 | |
| 163 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 164 | if (fd < 0) { |
| 165 | fprintf(stderr, "%s: ", progname); |
| 166 | perror("socket"); |
| 167 | return FALSE; |
| 168 | } |
| 169 | |
| 170 | strcpy(ifr.ifr_name, dev); |
| 171 | |
| 172 | if (hp != NULL) { |
| 173 | ifr.ifr_addr.sa_family = AF_INET; |
| 174 | |
| 175 | ifr.ifr_addr.sa_data[0] = 0; |
| 176 | ifr.ifr_addr.sa_data[1] = 0; |
| 177 | ifr.ifr_addr.sa_data[2] = hp->h_addr_list[0][0]; |
| 178 | ifr.ifr_addr.sa_data[3] = hp->h_addr_list[0][1]; |
| 179 | ifr.ifr_addr.sa_data[4] = hp->h_addr_list[0][2]; |
| 180 | ifr.ifr_addr.sa_data[5] = hp->h_addr_list[0][3]; |
| 181 | ifr.ifr_addr.sa_data[6] = 0; |
| 182 | |
| 183 | if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) { |
| 184 | fprintf(stderr, "%s: ", progname); |
| 185 | perror("SIOCSIFADDR"); |
| 186 | return FALSE; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | ifr.ifr_mtu = mtu; |
| 191 | |
| 192 | if (ioctl(fd, SIOCSIFMTU, &ifr) < 0) { |
| 193 | fprintf(stderr, "%s: ", progname); |
| 194 | perror("SIOCSIFMTU"); |
| 195 | return FALSE; |
| 196 | } |
| 197 | |
| 198 | if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) { |
| 199 | fprintf(stderr, "%s: ", progname); |
| 200 | perror("SIOCGIFFLAGS"); |
| 201 | return FALSE; |
| 202 | } |
| 203 | |
| 204 | ifr.ifr_flags &= IFF_NOARP; |
| 205 | ifr.ifr_flags |= IFF_UP; |
| 206 | ifr.ifr_flags |= IFF_RUNNING; |
| 207 | if (allow_broadcast) |
| 208 | ifr.ifr_flags |= IFF_BROADCAST; /* samba broadcasts are a pain.. */ |
| 209 | else |
| 210 | ifr.ifr_flags &= ~(IFF_BROADCAST); /* samba broadcasts are a pain.. */ |
| 211 | |
| 212 | if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) { |
| 213 | fprintf(stderr, "%s: ", progname); |
| 214 | perror("SIOCSIFFLAGS"); |
| 215 | return FALSE; |
| 216 | } |
| 217 | |
| 218 | close(fd); |
| 219 | |
| 220 | return TRUE; |
| 221 | } |
| 222 | |
| 223 | static void usage(void) |
| 224 | { |
| 225 | fprintf(stderr, "usage: %s [-b] [-l] [-m mtu] [-v] tty port [inetaddr]\n", progname); |
| 226 | } |
| 227 | |
| 228 | int main(int argc, char *argv[]) |
| 229 | { |
| 230 | int fd; |
| 231 | int disc = N_AX25; |
| 232 | char dev[64]; |
| 233 | int v = 4; |
| 234 | char *namepts = NULL; /* name of the unix98 pts slave, which |
| 235 | * the client has to use */ |
| 236 | struct hostent *hp = NULL; |
| 237 | |
| 238 | progname = kiss_basename(argv[0]); |
| 239 | |
| 240 | if (!strcmp(progname, "spattach")) |
| 241 | disc = N_6PACK; |
| 242 | |
| 243 | while ((fd = getopt(argc, argv, "b6i:lm:v")) != -1) { |
| 244 | switch (fd) { |
| 245 | case '6': |
| 246 | disc = N_6PACK; |
| 247 | break; |
| 248 | case 'b': |
| 249 | allow_broadcast = 1; |
| 250 | break; |
| 251 | case 'i': |
| 252 | fprintf(stderr, "%s: -i flag depreciated, use new command line format instead.\n", progname); |
| 253 | inetaddr = optarg; |
| 254 | break; |
| 255 | case 'l': |
| 256 | logging = TRUE; |
| 257 | break; |
| 258 | case 'm': |
| 259 | mtu = atoi(optarg); |
| 260 | if (mtu <= 0) { |
| 261 | fprintf(stderr, "%s: invalid mtu size - %s\n", progname, optarg); |
| 262 | return 1; |
| 263 | } |
| 264 | break; |
| 265 | case 'v': |
| 266 | printf("%s: %s\n", progname, FULL_VER); |
| 267 | return 0; |
| 268 | case ':': |
| 269 | case '?': |
| 270 | usage(); |
| 271 | return 1; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | #ifdef notdef |
| 276 | if ((argc - optind) != 3 && ((argc - optind) != 2 || !inetaddr)) { |
| 277 | #else |
| 278 | if ((argc - optind) < 2) { |
| 279 | #endif |
| 280 | usage(); |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | kttyname = argv[optind++]; |
| 285 | portname = argv[optind++]; |
| 286 | |
| 287 | if (argc-1 >= optind && !inetaddr) |
| 288 | inetaddr = argv[optind]; |
| 289 | |
| 290 | if (!strcmp("/dev/ptmx", kttyname)) |
| 291 | i_am_unix98_pty_master = 1; |
| 292 | |
| 293 | if (!i_am_unix98_pty_master) { |
| 294 | if (tty_is_locked(kttyname)) { |
| 295 | fprintf(stderr, "%s: device %s already in use\n", progname, kttyname); |
| 296 | return 1; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (!readconfig(portname)) |
| 301 | return 1; |
| 302 | |
| 303 | if (inetaddr && (hp = gethostbyname(inetaddr)) == NULL) { |
| 304 | fprintf(stderr, "%s: invalid internet name/address - %s\n", progname, inetaddr); |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | fd = open(kttyname, O_RDONLY | O_NONBLOCK); |
| 309 | if (fd == -1) { |
| 310 | if (errno == ENOENT) { |
| 311 | fprintf(stderr, "%s: Cannot find serial device %s, no such file or directory.\n", progname, kttyname); |
| 312 | } else { |
| 313 | fprintf(stderr, "%s: %s: ", progname, kttyname); |
| 314 | perror("open"); |
| 315 | } |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | if (i_am_unix98_pty_master) { |
| 320 | /* get name of pts-device */ |
| 321 | namepts = ptsname(fd); |
| 322 | if (namepts == NULL) { |
| 323 | fprintf(stderr, "%s: Cannot get name of pts-device.\n", progname); |
| 324 | return 1; |
| 325 | } |
| 326 | /* unlock pts-device */ |
| 327 | if (unlockpt(fd) == -1) { |
| 328 | fprintf(stderr, "%s: Cannot unlock pts-device %s\n", progname, namepts); |
| 329 | return 1; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (speed != 0 && !tty_speed(fd, speed)) |
| 334 | return 1; |
| 335 | |
| 336 | if (ioctl(fd, TIOCSETD, &disc) == -1) { |
| 337 | fprintf(stderr, "%s: Error setting line discipline: ", progname); |
| 338 | perror("TIOCSETD"); |
| 339 | fprintf(stderr, "Are you sure you have enabled %s support in the kernel\n", |
| 340 | disc == N_AX25 ? "MKISS" : "6PACK"); |
| 341 | fprintf(stderr, "or, if you made it a module, that the module is loaded?\n"); |
| 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | if (ioctl(fd, SIOCGIFNAME, dev) == -1) { |
| 346 | fprintf(stderr, "%s: ", progname); |
| 347 | perror("SIOCGIFNAME"); |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | if (!setifcall(fd, callsign)) |
| 352 | return 1; |
| 353 | |
| 354 | /* Now set the encapsulation */ |
| 355 | if (ioctl(fd, SIOCSIFENCAP, &v) == -1) { |
| 356 | fprintf(stderr, "%s: ", progname); |
| 357 | perror("SIOCSIFENCAP"); |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | /* ax25 ifaces should not really need to have an IP address assigned to */ |
| 362 | if (!startiface(dev, hp)) |
| 363 | return 1; |
| 364 | |
| 365 | printf("AX.25 port %s bound to device %s\n", portname, dev); |
| 366 | if (i_am_unix98_pty_master) { |
| 367 | /* Users await the slave pty to be referenced in the 3d line */ |
| 368 | printf("Awaiting client connects on\n%s\n", namepts); |
| 369 | } |
| 370 | if (logging) { |
| 371 | openlog(progname, LOG_PID, LOG_DAEMON); |
| 372 | syslog(LOG_INFO, "AX.25 port %s bound to device %s%s%s\n", portname, dev, (i_am_unix98_pty_master ? " with slave pty " : ""), (i_am_unix98_pty_master ? namepts : "")); |
| 373 | |
| 374 | } |
| 375 | |
| 376 | signal(SIGHUP, SIG_IGN); |
| 377 | signal(SIGTERM, terminate); |
| 378 | |
| 379 | /* |
| 380 | * Become a daemon if we can. |
| 381 | */ |
| 382 | if (!daemon_start(FALSE)) { |
| 383 | fprintf(stderr, "%s: cannot become a daemon\n", progname); |
| 384 | return 1; |
| 385 | } |
| 386 | if (!i_am_unix98_pty_master) { |
| 387 | if (!tty_lock(kttyname)) |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | fflush(stdout); |
| 392 | fflush(stderr); |
| 393 | close(0); |
| 394 | close(1); |
| 395 | close(2); |
| 396 | |
| 397 | while (1) |
| 398 | sleep(10000); |
| 399 | |
| 400 | /* NOT REACHED */ |
| 401 | return 0; |
| 402 | } |