Debian Codegraph @

ax25/rxecho.c

raw
1 /*
2 * rxecho.c - Copies AX.25 packets from an interface to another interface.
3 * Reads CONFIGFILE (see below) and uses that information to
4 * decide what packets should be copied and where.
5 *
6 * CONFIGFILE format is:
7 *
8 * # this is a comment
9 * 144 kiss0 oh2bns-1,oh2bns-2
10 * kiss0 144 *
11 *
12 * This means that packets received on port 144 are copied to port
13 * kiss0 if they are destined to oh2bns-1 or oh2bns-2. Packets
14 * from port kiss0 are all copied to port 144.
15 *
16 * There may be empty lines and an arbirary amount of white
17 * space around the tokens but the callsign field must not
18 * have any spaces in it. There can be up to MAXCALLS call-
19 * signs in the callsign field (see below).
20 *
21 * Copyright (C) 1996 by Tomi Manninen, OH2BNS, <[email protected]>.
22 *
23 * *** Modified 9/9/96 by Heikki Hannikainen, OH7LZB, <[email protected]>:
24 *
25 * One port can actually be echoed to multiple ports (with a
26 * different recipient callsign, of course). The old behaviour was
27 * to give up on the first matching port, even if the recipient
28 * callsign didn't match (and the frame wasn't echoed anywhere).
29 *
30 * *** 20021206 dl9sau:
31 * - fixed a bug preventing echo to multible ports; it may also
32 * lead to retransmission on the interface where it came from
33 * - fixed problem that frames via sendto(...,alen) had a wrong
34 * protocol (because alen became larger than the size of
35 * struct sockaddr).
36 * - added support for new PF_PACKET family with sockaddr_ll
37 *
38 * ***
39 *
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
44 *
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
49 *
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
53 * 02110-1301, USA.
54 *
55 */
56
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <syslog.h>
62 #include <signal.h>
63 #include <sys/types.h>
64
65 #include <config.h>
66 #include <scm-version.h>
67
68 #include <sys/socket.h>
69
70 #include <features.h> /* for the glibc version number */
71 #include <sys/ioctl.h>
72 #include <net/if.h>
73 #include <netpacket/packet.h>
74 #include <net/ethernet.h>
75
76 #include <netinet/in.h>
77
78 #include <netax25/ax25.h>
79 #include <netrose/rose.h>
80
81 #include <netax25/axlib.h>
82 #include <netax25/axconfig.h>
83 #include <netax25/daemon.h>
84
85 #include "../pathnames.h"
86
87 #define MAXCALLS 8
88
89 struct config {
90 char from[IFNAMSIZ];
91 int from_idx;
92 char to[IFNAMSIZ];
93 int to_idx;
94 ax25_address calls[MAXCALLS];/* list of calls to echo */
95 int ncalls; /* number of calls to echo */
96
97 struct config *next;
98 };
99
100 static int logging = FALSE;
101
102 static void terminate(int sig)
103 {
104 if (logging) {
105 syslog(LOG_INFO, "terminating on SIGTERM\n");
106 closelog();
107 }
108
109 exit(0);
110 }
111
112 /*
113 * Read string "call1,call2,call3,..." into p.
114 */
115 static int read_calls(struct config *p, char *s)
116 {
117 char *cp, *cp1;
118
119 if (p == NULL || s == NULL)
120 return -1;
121
122 p->ncalls = 0;
123
124 if (strcmp(s, "*") == 0)
125 return 0;
126
127 cp = s;
128
129 while ((cp1 = strchr(cp, ',')) != NULL && p->ncalls < MAXCALLS) {
130 *cp1 = 0;
131
132 if (ax25_aton_entry(cp, p->calls[p->ncalls].ax25_call) == -1)
133 return -1;
134
135 p->ncalls++;
136 cp = ++cp1;
137 }
138
139 if (p->ncalls < MAXCALLS) {
140 if (ax25_aton_entry(cp, p->calls[p->ncalls].ax25_call) == -1)
141 return -1;
142
143 p->ncalls++;
144 }
145
146 return p->ncalls;
147 }
148
149 static struct config *readconfig(void)
150 {
151 FILE *fp;
152 char line[80], *cp, *dev;
153 struct config *p, *list = NULL;
154
155 fp = fopen(CONF_RXECHO_FILE, "r");
156 if (fp == NULL) {
157 fprintf(stderr, "rxecho: cannot open config file\n");
158 return NULL;
159 }
160
161 while (fgets(line, 80, fp) != NULL) {
162 cp = strtok(line, " \t\r\n");
163
164 if (cp == NULL || cp[0] == '#')
165 continue;
166
167 p = calloc(1, sizeof(struct config));
168 if (p == NULL) {
169 perror("rxecho: malloc");
170 return NULL;
171 }
172
173 dev = ax25_config_get_dev(cp);
174 if (dev == NULL) {
175 fprintf(stderr, "rxecho: invalid port name - %s\n", cp);
176 return NULL;
177 }
178
179 strcpy(p->from, dev);
180 p->from_idx = -1;
181
182 cp = strtok(NULL, " \t\r\n");
183 if (cp == NULL) {
184 fprintf(stderr, "rxecho: config file error.\n");
185 return NULL;
186 }
187
188 dev = ax25_config_get_dev(cp);
189 if (dev == NULL) {
190 fprintf(stderr, "rxecho: invalid port name - %s\n", cp);
191 return NULL;
192 }
193
194 strcpy(p->to, dev);
195 p->to_idx = -1;
196
197 if (read_calls(p, strtok(NULL, " \t\r\n")) == -1) {
198 fprintf(stderr, "rxecho: config file error.\n");
199 return NULL;
200 }
201
202 p->next = list;
203 list = p;
204 }
205
206 fclose(fp);
207
208 if (list == NULL)
209 fprintf(stderr, "rxecho: Empty config file!\n");
210
211 return list;
212 }
213
214 /*
215 * Slightly modified from linux/include/net/ax25.h and
216 * linux/net/ax25/ax25_subr.c:
217 */
218
219 #if 0
220 #define C_COMMAND 1
221 #define C_RESPONSE 2
222 #define LAPB_C 0x80
223 #endif
224 #define LAPB_E 0x01
225 #define AX25_ADDR_LEN 7
226 #define AX25_REPEATED 0x80
227
228 typedef struct {
229 ax25_address calls[AX25_MAX_DIGIS];
230 unsigned char repeated[AX25_MAX_DIGIS];
231 char ndigi;
232 char lastrepeat;
233 } ax25_digi;
234
235 /*
236 * Given an AX.25 address pull of to, from, digi list, and the start of data.
237 */
238 static unsigned char *ax25_parse_addr(unsigned char *buf, int len, ax25_address *src, ax25_address *dest, ax25_digi *digi)
239 {
240 int d = 0;
241
242 if (len < 14) return NULL;
243
244 #if 0
245 if (flags != NULL) {
246 *flags = 0;
247
248 if (buf[6] & LAPB_C) {
249 *flags = C_COMMAND;
250 }
251
252 if (buf[13] & LAPB_C) {
253 *flags = C_RESPONSE;
254 }
255 }
256
257 if (dama != NULL)
258 *dama = ~buf[13] & DAMA_FLAG;
259 #endif
260
261 /* Copy to, from */
262 if (dest != NULL)
263 memcpy(dest, buf + 0, AX25_ADDR_LEN);
264
265 if (src != NULL)
266 memcpy(src, buf + 7, AX25_ADDR_LEN);
267
268 buf += 2 * AX25_ADDR_LEN;
269 len -= 2 * AX25_ADDR_LEN;
270
271 digi->lastrepeat = -1;
272 digi->ndigi = 0;
273
274 while (!(buf[-1] & LAPB_E)) {
275 if (d >= AX25_MAX_DIGIS) return NULL; /* Max of 6 digis */
276 if (len < 7) return NULL; /* Short packet */
277
278 if (digi != NULL) {
279 memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
280 digi->ndigi = d + 1;
281
282 if (buf[6] & AX25_REPEATED) {
283 digi->repeated[d] = 1;
284 digi->lastrepeat = d;
285 } else {
286 digi->repeated[d] = 0;
287 }
288 }
289
290 buf += AX25_ADDR_LEN;
291 len -= AX25_ADDR_LEN;
292 d++;
293 }
294
295 return buf;
296 }
297
298 /*
299 * Check if frame should be echoed. Return 0 if it should and -1 if not.
300 */
301 static int check_calls(struct config *cfg, unsigned char *buf, int len)
302 {
303 ax25_address dest;
304 ax25_digi digi;
305 ax25_address *axp;
306 int i;
307
308 if ((buf[0] & 0x0F) != 0)
309 return -1; /* don't echo non-data */
310
311 if (cfg->ncalls == 0)
312 return 0; /* copy everything */
313
314 if (ax25_parse_addr(++buf, --len, NULL, &dest, &digi) == NULL)
315 return -1; /* invalid ax.25 header */
316
317 /*
318 * If there are no digis or all digis are already repeated
319 * use destination address. Else use first non-repeated digi.
320 */
321 if (digi.ndigi == 0 || digi.ndigi == digi.lastrepeat + 1)
322 axp = &dest;
323 else
324 axp = &digi.calls[digi.lastrepeat + 1];
325
326 for (i = 0; i < cfg->ncalls; i++)
327 if (ax25_cmp(&cfg->calls[i], axp) == 0)
328 return 0;
329
330 return -1;
331 }
332
333 int main(int argc, char **argv)
334 {
335 struct sockaddr_ll sll;
336 struct sockaddr *psa = (struct sockaddr *)&sll;
337 const socklen_t sa_len = sizeof(struct sockaddr_ll);
338 int from_idx;
339 int s, size;
340 socklen_t alen;
341 unsigned char buf[1500];
342 struct config *p, *list;
343
344 while ((s = getopt(argc, argv, "lv")) != -1) {
345 switch (s) {
346 case 'l':
347 logging = TRUE;
348 break;
349 case 'v':
350 printf("rxecho: %s\n", FULL_VER);
351 return 0;
352 default:
353 fprintf(stderr, "usage: rxecho [-l] [-v]\n");
354 return 1;
355 }
356 }
357
358 signal(SIGTERM, terminate);
359
360 if (ax25_config_load_ports() == 0) {
361 fprintf(stderr, "rxecho: no AX.25 port data configured\n");
362 return 1;
363 }
364
365 list = readconfig();
366 if (list == NULL)
367 return 1;
368
369 s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_AX25));
370 if (s == -1) {
371 perror("rxecho: socket:");
372 return 1;
373 }
374
375 for (p = list; p != NULL; p = p->next) {
376 int i;
377
378 for (i = 0; i < 2; i++) {
379 struct config *q;
380 struct ifreq ifr;
381 char *p_name = (i ? p->to : p->from);
382 int *p_idx = (i ? &p->to_idx : &p->from_idx);
383
384 /* already set? */
385 if (*p_idx >= 0)
386 continue;
387 strncpy(ifr.ifr_name, p_name, sizeof(ifr.ifr_name)-1);
388 ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0;
389 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
390 perror("SIOCGIFINDEX");
391 return 1;
392 }
393 *p_idx = ifr.ifr_ifindex;
394 for (q = p->next; q != NULL; q = q->next) {
395 if (q->from_idx < 0 && !strcmp(q->from, p_name))
396 q->from_idx = *p_idx;
397 if (q->to_idx < 0 && !strcmp(q->to, p_name))
398 q->to_idx = *p_idx;
399 }
400 }
401 }
402
403 if (!daemon_start(FALSE)) {
404 fprintf(stderr, "rxecho: cannot become a daemon\n");
405 close(s);
406 return 1;
407 }
408
409 if (logging) {
410 openlog("rxecho", LOG_PID, LOG_DAEMON);
411 syslog(LOG_INFO, "starting");
412 }
413
414 for (;;) {
415 alen = sa_len;
416
417 size = recvfrom(s, buf, 1500, 0, psa, &alen);
418 if (size == -1) {
419 if (logging) {
420 syslog(LOG_ERR, "recvfrom: %m");
421 closelog();
422 }
423 return 1;
424 }
425
426 from_idx = sll.sll_ifindex;
427
428 for (p = list; p != NULL; p = p->next)
429 if (p->from_idx == from_idx && (check_calls(p, buf, size) == 0)) {
430 sll.sll_ifindex = p->to_idx;
431 /*
432 * cave: alen (set by recvfrom()) may > salen
433 * which means it may point beyound of the
434 * end of the struct. thats why we use the
435 * correct len (sa_len). this fixes a bug
436 * where skpt->protocol on sockaddr structs
437 * pointed behind the struct, leading to
438 * funny protocol IDs.
439 * btw, the linux kernel internaly always
440 * maps to sockaddr to sockaddr_pkt
441 * on sockets of type SOCK_PACKET on family
442 * AF_INET. sockaddr_pkt is len 18, sockaddr
443 * is 16.
444 */
445 if (sendto(s, buf, size, 0, psa, sa_len) == -1) {
446 if (logging) {
447 syslog(LOG_ERR, "sendto: %m");
448 closelog();
449 }
450 }
451 }
452 }
453 }