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
240
241
242
243
244
245
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <syslog.h>
#include <config.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netax25/ax25.h>
#include <netrom/netrom.h>
#include <netrose/rose.h>
#include <netax25/axlib.h>
#include "../pathnames.h"
#include "netromd.h"
static int validcallsign(ax25_address *a)
{
char c;
int n, end = 0;
for (n = 0; n < 6; n++) {
c = (a->ax25_call[n] >> 1) & 0x7F;
if (!end && (isupper(c) || isdigit(c)))
continue;
if (c == ' ') {
end = 1;
continue;
}
return FALSE;
}
return TRUE;
}
static int validmnemonic(char *mnemonic)
{
if (compliant) {
if (strspn(mnemonic, "#_&-/ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == strlen(mnemonic))
return TRUE;
} else {
if (strspn(mnemonic, "#_&-/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == strlen(mnemonic))
return TRUE;
}
return FALSE;
}
static int add_node(int s, unsigned char *buffer, struct nr_route_struct *nr_node, int quality, int index)
{
ax25_address best_neighbour;
int best_quality;
char *p;
memcpy(&nr_node->callsign, buffer + 0, CALLSIGN_LEN);
memcpy(nr_node->mnemonic, buffer + 7, MNEMONIC_LEN);
memcpy(&best_neighbour, buffer + 13, CALLSIGN_LEN);
best_quality = buffer[20];
nr_node->mnemonic[MNEMONIC_LEN] = '\0';
p = strchr(nr_node->mnemonic, ' ');
if (p != NULL)
*p = '\0';
if (!validcallsign(&nr_node->callsign)) {
if (debug && logging)
syslog(LOG_DEBUG, "netromr: add_node: invalid node callsign - %s", ax25_ntoa(&nr_node->callsign));
return -1;
}
if (!validmnemonic(nr_node->mnemonic)) {
if (debug && logging)
syslog(LOG_DEBUG, "netromr: add_node: invalid mnemonic - %s", nr_node->mnemonic);
return -1;
}
if (!validcallsign(&best_neighbour)) {
if (debug && logging)
syslog(LOG_DEBUG, "netromr: add_node: invalid best neighbour callsign - %s", ax25_ntoa(&best_neighbour));
return -1;
}
if (ax25_cmp(&my_call, &best_neighbour) == 0) {
if (debug && logging)
syslog(LOG_DEBUG, "netromr: add_node: route to me");
return FALSE;
}
nr_node->quality = ((quality * best_quality) + 128) / 256;
if (nr_node->quality < port_list[index].worst_qual) {
if (debug && logging)
syslog(LOG_DEBUG, "netromr: add_node: quality less than worst_qual");
return FALSE;
}
/* log this only when logging verbosely */
if (debug > 1 && logging) {
syslog(LOG_DEBUG, "Node update: %s:%s",
ax25_ntoa(&nr_node->callsign), nr_node->mnemonic);
syslog(LOG_DEBUG, "Neighbour: %s device: %s",
ax25_ntoa(&nr_node->neighbour), nr_node->device);
syslog(LOG_DEBUG, "Quality: %d obs: %d ndigis: %d",
nr_node->quality, nr_node->obs_count, nr_node->ndigis);
}
if (ioctl(s, SIOCADDRT, nr_node) == -1) {
if (logging)
syslog(LOG_ERR, "netromr: SIOCADDRT: %m");
return -1;
}
return TRUE;
}
void receive_nodes(unsigned char *buffer, int length, ax25_address *neighbour, int index)
{
struct nr_route_struct nr_node;
char neigh_buffer[90], *portcall, *p;
FILE *fp;
int s;
int quality, obs_count, qual, lock;
char *callsign, *device;
if (!validcallsign(neighbour)) {
if (debug && logging)
syslog(LOG_DEBUG, "rejecting frame, invalid neighbour callsign - %s\n", ax25_ntoa(neighbour));
return;
}
nr_node.type = NETROM_NODE;
nr_node.ndigis = 0;
sprintf(neigh_buffer, "%s/obsolescence_count_initialiser", PROC_NR_SYSCTL_DIR);
fp = fopen(neigh_buffer, "r");
if (fp == NULL) {
if (logging)
syslog(LOG_ERR, "netromr: cannot open %s\n", neigh_buffer);
return;
}
fgets(neigh_buffer, 90, fp);
obs_count = atoi(neigh_buffer);
fclose(fp);
s = socket(AF_NETROM, SOCK_SEQPACKET, 0);
if (s < 0) {
if (logging)
syslog(LOG_ERR, "netromr: socket: %m");
return;
}
fp = fopen(PROC_NR_NEIGH_FILE, "r");
if (fp == NULL) {
if (logging)
syslog(LOG_ERR, "netromr: cannot open %s\n", PROC_NR_NEIGH_FILE);
close(s);
return;
}
fgets(neigh_buffer, 90, fp);
portcall = ax25_ntoa(neighbour);
quality = port_list[index].default_qual;
while (fgets(neigh_buffer, 90, fp) != NULL) {
strtok(neigh_buffer, " "); /* skip addr field */
callsign = strtok(NULL, " ");
device = strtok(NULL, " ");
qual = atoi(strtok(NULL, " "));
lock = atoi(strtok(NULL, " "));
if (strcmp(callsign, portcall) == 0 &&
strcmp(device, port_list[index].device) == 0 &&
lock == 1) {
quality = qual;
break;
}
}
fclose(fp);
nr_node.callsign = *neighbour;
memcpy(nr_node.mnemonic, buffer, MNEMONIC_LEN);
nr_node.mnemonic[MNEMONIC_LEN] = '\0';
p = strchr(nr_node.mnemonic, ' ');
if (p != NULL)
*p = '\0';
if (!validmnemonic(nr_node.mnemonic)) {
if (debug && logging)
syslog(LOG_DEBUG, "rejecting route, invalid mnemonic - %s\n", nr_node.mnemonic);
} else {
nr_node.neighbour = *neighbour;
strcpy(nr_node.device, port_list[index].device);
nr_node.quality = quality;
nr_node.obs_count = obs_count;
if (ioctl(s, SIOCADDRT, &nr_node) == -1) {
if (logging)
syslog(LOG_ERR, "netromr: SIOCADDRT: %m");
close(s);
return;
}
}
buffer += MNEMONIC_LEN;
length -= MNEMONIC_LEN;
while (length >= ROUTE_LEN) {
nr_node.neighbour = *neighbour;
strcpy(nr_node.device, port_list[index].device);
nr_node.obs_count = obs_count;
if (add_node(s, buffer, &nr_node, quality, index) == -1) {
close(s);
return;
}
buffer += ROUTE_LEN;
length -= ROUTE_LEN;
}
close(s);
}
|