Debian Codegraph @

ax25rtd/config.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1996 Joerg Reuter ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <sys/ioctl.h>
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <net/if.h>
33 #ifdef __GLIBC__
34 #include <net/ethernet.h>
35 #else
36 #include <linux/if_ether.h>
37 #endif
38 #include <net/if_arp.h>
39
40 #include <config.h>
41 #include <netax25/ax25.h>
42 #include <netrose/rose.h>
43 #include <netax25/axlib.h>
44 #include <netax25/axconfig.h>
45
46 #include "../pathnames.h"
47 #include "ax25rtd.h"
48
49 static ax25_address *asc2ax(char *call)
50 {
51 static ax25_address callsign;
52
53 ax25_aton_entry(call, (char *) &callsign);
54
55 return &callsign;
56 }
57
58 static long asc2ip(char *s)
59 {
60 struct in_addr addr;
61
62 if (!inet_aton(s, &addr))
63 return 0;
64
65 return addr.s_addr;
66 }
67
68 static char *prepare_cmdline(char *buf)
69 {
70 char *p;
71 for (p = buf; *p; p++) {
72 if (*p == '\t')
73 *p = ' ';
74 *p = tolower(*p);
75
76 if (*p == '\n') {
77 *p = '\0';
78 break;
79 }
80
81 if (*p == '#') {
82 *p = '\0';
83 break;
84 }
85 }
86
87 return buf;
88 }
89
90 static char *get_next_arg(char **p)
91 {
92 char *p2;
93
94 if (p == NULL || *p == NULL)
95 return NULL;
96
97 p2 = *p;
98 for (; *p2 && *p2 == ' '; p2++);
99 if (!*p2)
100 return NULL;
101
102 *p = strchr(p2, ' ');
103 if (*p != NULL) {
104 **p = '\0';
105 (*p)++;
106 }
107
108 return p2;
109 }
110
111 static inline void invalid_arg(char *c, char *p)
112 {
113 fprintf(stderr, "%s: invalid argument %s\n", c, p);
114 }
115
116 static inline void missing_arg(char *c)
117 {
118 fprintf(stderr, "%s: argument missing\n", c);
119 }
120
121 static int yesno(char *arg)
122 {
123 if (!arg)
124 return 0;
125 if (!strcmp(arg, "yes") || !strcmp(arg, "1"))
126 return 1;
127 if (!strcmp(arg, "no") || !strcmp(arg, "0"))
128 return 0;
129 return -1;
130 }
131
132 static ax25_address *get_mycall(char *port)
133 {
134 char *addr;
135
136 addr = ax25_config_get_addr(port);
137 if (addr == NULL)
138 return NULL;
139
140 return asc2ax(addr);
141 }
142
143 static void load_ports(void)
144 {
145 config *config, *cfg, *ncfg;
146 char buf[1024];
147 struct ifconf ifc;
148 struct ifreq ifr, *ifrp;
149 int k, fd;
150
151 fd = socket(AF_INET, SOCK_DGRAM, 0);
152 if (fd < 0) {
153 fprintf(stderr, "Unable to open socket\n");
154 exit(1);
155 }
156
157 ifc.ifc_len = sizeof(buf);
158 ifc.ifc_buf = buf;
159 if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
160 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
161 return;
162 }
163
164 ifrp = ifc.ifc_req;
165 for (k = ifc.ifc_len / sizeof(struct ifreq); k > 0; k--, ifrp++) {
166 strcpy(ifr.ifr_name, ifrp->ifr_name);
167 if (strcmp(ifr.ifr_name, "lo") == 0)
168 continue;
169
170 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
171 fprintf(stderr, "SIOCGIFFLAGS: %s\n",
172 strerror(errno));
173 exit(1);
174 }
175
176 if (!(ifr.ifr_flags & IFF_UP))
177 continue;
178
179 ioctl(fd, SIOCGIFHWADDR, &ifr);
180
181 if (ifr.ifr_hwaddr.sa_family != ARPHRD_AX25)
182 continue;
183
184 for (config = Config; config; config = config->next)
185 if (!memcmp
186 (&config->mycalls[0], ifr.ifr_hwaddr.sa_data,
187 AXLEN) && !*config->dev) {
188 strcpy(config->dev, ifr.ifr_name);
189 ioctl(fd, SIOCGIFADDR, &ifr);
190 config->ip =
191 ((struct sockaddr_in *) &ifr.
192 ifr_addr)->sin_addr.s_addr;
193 strcpy(ifr.ifr_name, config->dev);
194 ioctl(fd, SIOCGIFNETMASK, &ifr);
195 config->netmask =
196 ((struct sockaddr_in *) &ifr.
197 ifr_netmask)->sin_addr.s_addr;
198 break;
199 }
200 }
201 close(fd);
202
203 config = cfg = Config;
204
205 while (config) {
206 if (!*config->dev) {
207 if (config == Config) {
208 Config = config->next;
209 cfg = Config;
210 } else
211 cfg->next = config->next;
212 ncfg = config->next;
213 free(config);
214 config = ncfg;
215 } else {
216 cfg = config;
217 config = config->next;
218 }
219 }
220 }
221
222 static void load_listeners(void)
223 {
224 config *config;
225 char buf[1024], device[14], call[10], dcall[10];
226 char dummy[1024];
227 int k;
228 FILE *fp;
229 ax25_address *axcall;
230
231 fp = fopen(PROC_AX25_FILE, "r");
232
233 if (fp == NULL) {
234 fprintf(stderr, "No AX.25 in kernel. Tss, tss...\n");
235 exit(1);
236 }
237
238 while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
239 k = sscanf(buf, "%s %s %s %s", dummy, device, call, dcall);
240 if (k == 4 && !strcmp(dcall, "*")) {
241 axcall = asc2ax(call);
242 if (!strcmp("*", device)) {
243 for (config = Config; config;
244 config = config->next) {
245 if (call_is_mycall(config, axcall)
246 || config->nmycalls >
247 AX25_MAXCALLS)
248 continue;
249 memcpy(&config->
250 mycalls[config->nmycalls++],
251 axcall, AXLEN);
252 }
253 } else {
254 config = dev_get_config(device);
255 if (config == NULL
256 || call_is_mycall(config, axcall)
257 || config->nmycalls > AX25_MAXCALLS)
258 continue;
259
260 memcpy(&config->
261 mycalls[config->nmycalls++], axcall,
262 AXLEN);
263 }
264 }
265 }
266 fclose(fp);
267 }
268
269 void load_config(void)
270 {
271 FILE *fp;
272 char buf[1024], *p, *cmd, *arg;
273 config *config, *cfg;
274 ax25_address *axcall;
275
276 config = NULL;
277
278 fp = fopen(CONF_AX25ROUTED_FILE, "r");
279
280 if (fp == NULL) {
281 fprintf(stderr, "config file %s not found\n",
282 CONF_AX25ROUTED_FILE);
283 exit(1);
284 }
285
286 while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
287 p = prepare_cmdline(buf);
288 if (!*p)
289 continue;
290
291 cmd = get_next_arg(&p);
292 if (cmd == NULL)
293 continue;
294 arg = get_next_arg(&p);
295
296 if (*cmd == '[') {
297 cmd++;
298 p = strrchr(cmd, ']');
299 if (p == NULL) {
300 fprintf(stderr, "syntax error: [%s\n",
301 cmd);
302 continue;
303 }
304 *p = '\0';
305
306 axcall = get_mycall(cmd);
307 if (axcall == NULL)
308 continue;
309
310 cfg =
311 (struct config_ *)
312 malloc(sizeof(struct config_));
313 if (cfg == NULL) {
314 fprintf(stderr, "out of memory\n");
315 exit(1);
316 }
317 memset(cfg, 0, sizeof(struct config_));
318
319 if (config)
320 config->next = cfg;
321 else
322 Config = cfg;
323
324 cfg->next = NULL;
325 config = cfg;
326 strcpy(config->port, cmd);
327 memcpy(&config->mycalls[0], axcall, AXLEN);
328 config->nmycalls = 1;
329 } else if (config && !strcmp(cmd, "ax25-learn-routes")) {
330 /* ax25-learn-routes no|yes: learn digipeater path */
331 if (arg) {
332 int k = yesno(arg);
333
334 if (k == -1) {
335 invalid_arg(cmd, arg);
336 continue;
337 } else {
338 config->ax25_add_route = k;
339 }
340 } else
341 missing_arg(cmd);
342 } else if (config && !strcmp(cmd, "ax25-learn-only-mine")) {
343 /* ax25-learn-only-mine no|yes: learn only if addressed to me */
344 if (arg) {
345 int k = yesno(arg);
346
347 if (k == -1) {
348 invalid_arg(cmd, arg);
349 continue;
350 } else {
351 config->ax25_for_me = k;
352 }
353 } else
354 missing_arg(cmd);
355 } else if (config && !strcmp(cmd, "ax25-add-path")) {
356 /* ax25-add-path [no|<digis>]: add digis if digi-less frame rvd */
357 int k = 0;
358 if (!arg || (arg && yesno(arg) == 0))
359 continue;
360
361 config->ax25_add_default = 1;
362 while (arg && k < AX25_MAX_DIGIS) {
363 memcpy(&config->ax25_default_path.
364 fsa_digipeater[k], asc2ax(arg),
365 AXLEN);
366 arg = get_next_arg(&p);
367 k++;
368 }
369 config->ax25_default_path.fsa_ax25.sax25_ndigis =
370 k;
371 } else if (config && !strcmp(cmd, "ax25-more-mycalls")) {
372 /* ax25-more-mycalls call1 call2: frames to this calls are for "me", too. */
373 if (arg) {
374 while (arg
375 && config->nmycalls <
376 AX25_MAXCALLS) {
377 axcall = asc2ax(arg);
378 if (call_is_mycall(config, axcall)) {
379 arg = get_next_arg(&p);
380 continue;
381 }
382 memcpy(&config->
383 mycalls[config->nmycalls++],
384 axcall, AXLEN);
385 arg = get_next_arg(&p);
386 }
387 } else
388 missing_arg(cmd);
389 } else if (config && !strcmp(cmd, "ip-learn-routes")) {
390 /* ip-learn-routes no|yes: learn ip routes */
391 if (arg) {
392 int k = yesno(arg);
393
394 if (k == -1) {
395 invalid_arg(cmd, arg);
396 continue;
397 } else {
398 config->ip_add_route = k;
399 }
400 } else
401 missing_arg(cmd);
402 } else if (config && !strcmp(cmd, "irtt")) {
403 /* irtt <irtt>: new routes will get this IRTT in msec */
404 if (arg) {
405 int k = atoi(arg);
406
407 if (k == 0) {
408 invalid_arg(cmd, arg);
409 continue;
410 } else {
411 config->tcp_irtt = k;
412 }
413 } else
414 missing_arg(cmd);
415 } else if (config && !strcmp(cmd, "dg-mtu")) {
416 /* dg-mtu <mtu>: MTU for datagram mode routes (unused) */
417 if (arg) {
418 int k = atoi(arg);
419
420 if (k == 0) {
421 invalid_arg(cmd, arg);
422 continue;
423 } else {
424 config->dg_mtu = k;
425 }
426 } else
427 missing_arg(cmd);
428 } else if (config && !strcmp(cmd, "vc-mtu")) {
429 /* vc-mtu <mtu>: MTU for virtual connect mode routes (unused) */
430 if (arg) {
431 int k = atoi(arg);
432
433 if (k == 0) {
434 invalid_arg(cmd, arg);
435 continue;
436 } else {
437 config->vc_mtu = k;
438 }
439 } else
440 missing_arg(cmd);
441 } else if (config && !strcmp(cmd, "ip-adjust-mode")) {
442 /* ip-adjust-mode no|yes: adjust ip-mode? (dg or vc) */
443 if (arg) {
444 int k = yesno(arg);
445
446 if (k == -1) {
447 invalid_arg(cmd, arg);
448 continue;
449 } else {
450 config->ip_adjust_mode = k;
451 }
452 } else
453 missing_arg(cmd);
454 } else if (config && !strcmp(cmd, "arp-add")) {
455 /* arp-add no|yes: adjust arp table? */
456 if (arg) {
457 int k = yesno(arg);
458
459 if (k == -1) {
460 invalid_arg(cmd, arg);
461 continue;
462 } else {
463 config->ip_add_arp = k;
464 }
465 } else
466 missing_arg(cmd);
467 } else if (!strcmp(cmd, "ip-encaps-dev")) {
468 if (arg)
469 strcpy(ip_encaps_dev, arg);
470 else
471 missing_arg(cmd);
472 } else if (!strcmp(cmd, "ax25-maxroutes")) {
473 if (arg)
474 ax25_maxroutes = atoi(arg);
475 else
476 missing_arg(cmd);
477 } else if (!strcmp(cmd, "ip-maxroutes")) {
478 if (arg)
479 ip_maxroutes = atoi(arg);
480 else
481 missing_arg(cmd);
482 } else if (!strcmp(cmd, "iproute2-table")) {
483 if (arg)
484 strcpy(iproute2_table, arg);
485 else
486 missing_arg(cmd);
487 } else
488 fprintf(stderr, "invalid command %s\n", cmd);
489 }
490 fclose(fp);
491
492 load_ports();
493 load_listeners();
494
495 reload = 0;
496 }
497
498 void reload_config(void)
499 {
500 config *cfg, *config = Config;
501
502 while (config) {
503 cfg = config->next;
504 free(config);
505 config = cfg;
506 }
507 Config = NULL;
508
509 load_config();
510 }
511
512 /* commands:
513 ---------
514
515 add ax25 <callsign> <dev> <time> [<digipeater>] # Add an AX.25 route
516 add ip <ip> <dev> <time> <call> <mode> # Add an IP route & mode
517 del ax25 <callsign> <dev> # Remove an AX.25 route (from cache)
518 del ip <ip> # Remove an IP route (from cache)
519 list [ax25|ip] # List cache entries
520 reload # Reload config
521 save # Save cache
522 expire <minutes> # Expire cache entries
523 shutdown # Save cache and exit
524
525 There's a difference between 'list heard' and 'heard':
526
527 The 'list' commands will output the symbolic port names as defined in
528 /usr/local/etc/axports (i.e. 9k6 for scc3), while 'heard' shows the
529 'real' network device name (i.e. scc3). All commands accept either the
530 port or the network device name. The expample
531
532 add ax25 dl0tha scc3 0 db0pra
533
534 is equivalent to
535
536 add ax25 dl0tha 9k6 0 dbpra
537
538 Note that in conflicting cases the network device name has precedence
539 over the port name.
540 */
541
542 void interpret_command(int fd, char *buf)
543 {
544 char *p, *cmd, *arg, *arg2, *dev, *time;
545 ax25_address digipeater[AX25_MAX_DIGIS];
546 ax25_rt_entry *ax25rt;
547 int ndigi, ipmode, action;
548 time_t stamp;
549 config *config;
550 long ip;
551
552 p = prepare_cmdline(buf);
553
554 if (!*p)
555 return;
556
557 cmd = get_next_arg(&p);
558 arg = get_next_arg(&p);
559
560 if (!strcmp(cmd, "add")) {
561 if (arg == NULL)
562 return;
563 arg2 = get_next_arg(&p);
564 if (arg2 == NULL)
565 return;
566 dev = get_next_arg(&p);
567 if (dev == NULL)
568 return;
569 time = get_next_arg(&p);
570 if (time == NULL)
571 return;
572 config = dev_get_config(dev);
573 if (config == NULL)
574 return;
575
576 sscanf(time, "%lx", &stamp);
577
578 if (!strcmp(arg, "ax25")) {
579 ndigi = 0;
580 arg = get_next_arg(&p);
581
582 while (arg != NULL) {
583 memcpy(&digipeater[ndigi++], asc2ax(arg),
584 AXLEN);
585 if (ndigi == AX25_MAX_DIGIS)
586 break;
587 arg = get_next_arg(&p);
588 }
589
590 ax25rt =
591 update_ax25_route(config, asc2ax(arg2), ndigi,
592 digipeater, stamp);
593 if (ax25rt != NULL)
594 set_ax25_route(config, ax25rt);
595 } else if (!strcmp(arg, "ip")) {
596 ip = asc2ip(arg2);
597
598 arg2 = get_next_arg(&p);
599 if (arg2 == NULL)
600 return;
601
602 arg = get_next_arg(&p);
603 if (arg == NULL)
604 return;
605
606 if (*arg == 'x')
607 return;
608
609 ipmode = (*arg == 'v');
610
611 if (*ip_encaps_dev && (config =
612 dev_get_config(ip_encaps_dev)) == NULL) {
613 printf("no config for %s\n",
614 ip_encaps_dev);
615 return;
616 }
617
618 action =
619 update_ip_route(config, ip, ipmode,
620 asc2ax(arg2), stamp);
621
622 if (action & NEW_ROUTE)
623 if (set_route(config, ip))
624 return;
625
626 if (action & NEW_ARP)
627 if (set_arp(config, ip, asc2ax(arg2)))
628 return;
629
630 if (action & NEW_IPMODE)
631 if (set_ipmode
632 (config, asc2ax(arg2), ipmode))
633 return;
634 }
635 } else if (!strcmp(cmd, "del")) {
636 if (arg == NULL)
637 return;
638 arg2 = get_next_arg(&p);
639 if (arg2 == NULL)
640 return;
641
642 if (!strcmp(arg, "ax25")) {
643 arg = get_next_arg(&p);
644 if (arg == NULL
645 || (config = dev_get_config(arg)) == NULL)
646 return;
647 del_ax25_route(config, asc2ax(arg2));
648 } else if (!strcmp(arg, "ip")) {
649 del_ip_route(asc2ip(arg2));
650 }
651 } else if (!strcmp(cmd, "expire")) {
652 if (arg == NULL)
653 return;
654 sscanf(arg, "%ld", &stamp);
655 if (stamp != 0) {
656 stamp *= 60;
657 expire_ax25_route(stamp);
658 expire_ip_route(stamp);
659 }
660 } else if (!strcmp(cmd, "reload")) {
661 reload_config();
662 } else if (!strcmp(cmd, "list")) {
663 if (arg == NULL)
664 return;
665
666 if (!strcmp(arg, "ax25"))
667 dump_ax25_routes(fd, 0);
668 else if (!strcmp(arg, "ip"))
669 dump_ip_routes(fd, 0);
670 } else if (!strcmp(cmd, "shutdown")) {
671 save_cache();
672 daemon_shutdown(0);
673 } else if (!strcmp(cmd, "save")) {
674 save_cache();
675 } else if (!strcmp(cmd, "version")) {
676 char buf[] = "ax25rtd version " VERSION "\n";
677 write(fd, buf, strlen(buf));
678 } else if (!strcmp(cmd, "quit")) {
679 close(fd);
680 }
681 }
682
683 void load_cache(void)
684 {
685 FILE *fp;
686 char buf[512];
687
688 fp = fopen(DATA_AX25ROUTED_AXRT_FILE, "r");
689 if (fp != NULL) {
690 while (fgets(buf, sizeof(buf), fp) != NULL)
691 interpret_command(2, buf);
692 fclose(fp);
693 } else
694 perror("open AX.25 route cache file");
695
696 fp = fopen(DATA_AX25ROUTED_IPRT_FILE, "r");
697 if (fp != NULL) {
698 while (fgets(buf, sizeof(buf), fp) != NULL)
699 interpret_command(2, buf);
700 fclose(fp);
701 } else
702 perror("open IP route cache file");
703 }
704
705 void save_cache(void)
706 {
707 int fd;
708
709 fd = creat(DATA_AX25ROUTED_AXRT_FILE, 0664);
710 dump_ax25_routes(fd, 1);
711 close(fd);
712
713 fd = creat(DATA_AX25ROUTED_IPRT_FILE, 0664);
714 dump_ip_routes(fd, 1);
715 close(fd);
716
717 }