summaryrefslogtreecommitdiffstats
path: root/ax25rtd/ax25rtd.c
blob: 8fcf87a3f2d7aabc28757234b7ef3babb11f7346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (c) 1996 Joerg Reuter (jreuter@poboxes.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
 *   02110-1301, USA.
 *
 */

/*
 * This daemon tries to learn AX.25, ARP, IP route entries by listening
 * to the AX.25 traffic. It caches up to 256 entries (in "FIFO" mode)
 * and saves the cache on demand or at shutdown in /var/ax25/ax25rtd/ip_route
 * and /var/ax25/ax25rtd/ax25_route. The configuration file is
 * /etc/ax25/ax25rtd.conf, you can almost everything configure
 * there. See ax25rtcl.c for runtime maintenance.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#ifdef __GLIBC__
#include <net/ethernet.h>
#else
#include <linux/if_ether.h>
#endif

#include <netax25/ax25.h>
#include <netrose/rose.h>
#include <netax25/axconfig.h>
#include <netax25/axlib.h>

#include "../pathnames.h"
#include "ax25rtd.h"

config *Config = NULL;

int reload = 0;

ip_rt_entry *ip_routes;
int ip_routes_cnt;
int ip_maxroutes = IP_MAXROUTES;

ax25_rt_entry *ax25_routes;
int ax25_routes_cnt;
int ax25_maxroutes = AX25_MAXROUTES;

char ip_encaps_dev[32] = "";
char iproute2_table[32] = "";

config *dev_get_config(char *dev)
{
	config *config;

	for (config = Config; config; config = config->next)
		if (!strcmp(config->dev, dev))
			return config;

	return port_get_config(dev);
}

config *port_get_config(char *port)
{
	config *config;

	for (config = Config; config; config = config->next)
		if (!strcmp(config->port, port))
			return config;
	return NULL;
}

static void sig_reload(int d)
{
	reload = 1;
	signal(SIGHUP, sig_reload);
}

static void sig_debug(int d)
{
	fprintf(stderr, "config:\n");
	dump_config(2);
	fprintf(stderr, "ip-routes:\n");
	dump_ip_routes(2, 0);
	fprintf(stderr, "ax25-routes:\n");
	dump_ax25_routes(2, 0);
	signal(SIGUSR1, sig_debug);
}

static void sig_term(int d)
{
	save_cache();
	daemon_shutdown(0);
}

void daemon_shutdown(int reason)
{
	unlink(DATA_AX25ROUTED_CTL_SOCK);
	exit(reason);
}

#define FD_MAX(fd) {fd_max = (fd > fd_max? fd : fd_max); FD_SET(fd, &read_fds);}

int main(int argc, char **argv)
{
	char buf[256];
	int size, s, cntrl_s, cntrl_fd;
	socklen_t cntrl_len;
	struct sockaddr_un cntrl_addr;
	fd_set read_fds, write_fds;
	int fd_max;

	if (ax25_config_load_ports() == 0) {
		fprintf(stderr, "ax25rtd: no AX.25 port configured\n");
		return 1;
	}

	load_config();
	load_cache();

	if (fork())
		return 0;

	s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_AX25));
	if (s == -1) {
		perror("AX.25 socket");
		return 1;
	}

	cntrl_s = socket(AF_UNIX, SOCK_STREAM, 0);
	if (cntrl_s < 0) {
		perror("Control socket");
		return 1;
	}

	unlink(DATA_AX25ROUTED_CTL_SOCK);

	cntrl_addr.sun_family = AF_UNIX;
	strcpy(cntrl_addr.sun_path, DATA_AX25ROUTED_CTL_SOCK);
	cntrl_len =
	    sizeof(cntrl_addr.sun_family) +
	    strlen(DATA_AX25ROUTED_CTL_SOCK);

	if (bind(cntrl_s, (struct sockaddr *) &cntrl_addr, cntrl_len) < 0) {
		perror("bind Control socket");
		daemon_shutdown(1);
	}

	chmod(DATA_AX25ROUTED_CTL_SOCK, 0600);
	listen(cntrl_s, 1);

	signal(SIGUSR1, sig_debug);
	signal(SIGHUP, sig_reload);
	signal(SIGTERM, sig_term);

	cntrl_fd = -1;

	for (;;) {
		fd_max = 0;
		FD_ZERO(&read_fds);
		FD_ZERO(&write_fds);
		FD_MAX(s);
		if (cntrl_fd > 0) {
			FD_MAX(cntrl_fd);
			FD_SET(cntrl_fd, &write_fds);
		} else {
			FD_MAX(cntrl_s);
		}

		if (select(fd_max + 1, &read_fds, NULL, &write_fds, NULL) < 0) {
			if (errno == EINTR)	/* woops! */
				continue;

			if (!FD_ISSET(cntrl_fd, &write_fds)) {
				perror("select");
				save_cache();
				daemon_shutdown(1);
			} else {
				close(cntrl_fd);
				cntrl_fd = -1;
				continue;
			}
		}

		if (cntrl_fd > 0) {
			if (FD_ISSET(cntrl_fd, &read_fds)) {
				size = read(cntrl_fd, buf, sizeof(buf));
				if (size > 0) {
					buf[size] = '\0';
					interpret_command(cntrl_fd, buf);
				} else {
					close(cntrl_fd);
					cntrl_fd = -1;
				}
			}
		} else if (FD_ISSET(cntrl_s, &read_fds)) {
			cntrl_fd = accept(cntrl_s,
					  (struct sockaddr *)&cntrl_addr,
					  &cntrl_len);
			if (cntrl_fd < 0) {
				perror("accept Control");
				save_cache();
				daemon_shutdown(1);
			}
		}

		if (reload)
			reload_config();

		if (FD_ISSET(s, &read_fds))
			ax25_receive(s);
	}

	return 0;		/* what ?! */
}