summaryrefslogtreecommitdiffstats
path: root/net/ax25/ax25_route.c
blob: cf468c65f86de1add01b29cdae874e8f995e93c6 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * ax25_route.c: Routing table management for NEW-AX.25
 *
 * Authors:      Jens David (DG1KJD), Matthias Welwarsky (DG2FEF), Jonathan (G4K
 *               Alan Cox (GW4PTS), Joerg (DL1BKE), et al
 *
 * Comment:
 *
 * Changelog:
 *
 * License:      This module 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.
 */

#include <linux/config.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/spinlock.h>
#include <asm/uaccess.h>
#include <net/sock.h>
#include <net/ax25_uid.h>

#include "af_ax25.h"
#include "ax25_core.h"
#include "ax25_route.h"

static struct ax25_route *ax25_route = NULL;
rwlock_t ax25_rt_lock = RW_LOCK_UNLOCKED;

/*
 * delete all routes on a given device
 */
void ax25_rt_device_down(struct net_device *dev)
{
	struct ax25_route *this, *last;

	write_lock(&ax25_rt_lock);

	this = ax25_route;
	last = NULL;

	while (this) {
		if (this->dev != dev) {
			last = this;
			this = this->next;
			continue;
		}
		if (!last) {
			ax25_route = this->next;
			kfree(this);
			this = ax25_route;
			continue;
		}
		last->next = this->next;
		kfree(this);
		this = last->next;
	}

	write_unlock(&ax25_rt_lock);
}

int ax25_add_route(ax25_path_t *path, struct net_device *dev)
{
	struct ax25_route *this;

	write_lock(&ax25_rt_lock);

	for (this = ax25_route; this; this = this->next) {
		if (ax25cmp(&this->path.addr, &path->addr) == 0) {
			this->path = *path;
			this->dev  = dev;
			return 0;
		}
	}

	if ((this = (struct ax25_route *)kmalloc(sizeof(struct ax25_route), GFP_KERNEL)) == NULL)
		return -ENOMEM;
	this->path    = *path;
	this->dev     = dev;
	this->ip_mode = ' ';
        this->next    = ax25_route;
	ax25_route    = this;

	write_unlock(&ax25_rt_lock);

	return 0;
}

int ax25_del_route(ax25_address *addr)
{
	struct ax25_route *this, *last;

	write_lock(&ax25_rt_lock);

	this = ax25_route;
	last = NULL;
	while (this != NULL) {
		if (ax25cmp(&this->path.addr, addr) != 0) {
			last = this;
			this = this->next;
			continue;
		}
		if (!last) {
			ax25_route = this->next;
			kfree(this);
			return 0;
		}
		last->next = this->next;
		kfree(this);
		return 0;
	}

	write_unlock(&ax25_rt_lock);

	return -EINVAL;
}

int ax25_ipopt_route(ax25_address *addr, unsigned char opt)
{
	struct ax25_route *this;
	int err = -EINVAL;

	write_lock(&ax25_rt_lock);

	for (this = ax25_route; this; this = this->next) {
		if (ax25cmp(&this->path.addr, addr) == 0) {
			switch(opt) {
			case ' ':
			case 'D':
			case 'V':
			case 'C':
				this->ip_mode = opt;
				err = 0;
				break;
			}
		}
	}

	write_unlock(&ax25_rt_lock);

	return err;
}

int ax25_rt_ioctl(unsigned int cmd, void *arg)
{
	struct ax25_routes_struct route;
	struct ax25_route_opt_struct rt_option;
	struct net_device *dev;
	ax25_path_t ax25_path;
	int err = -EINVAL;
	int i;

	switch (cmd) {
	case SIOCADDRT:
		/* do some sanity checks */
		if (copy_from_user(&route, arg, sizeof(route))) {
			err = -EFAULT;
			break;
		}
		if ((dev = ax25rtr_get_dev(&route.port_addr)) == NULL)
			break;
		if (route.digi_count > AX25_MAX_DIGIS)
			break;

		ax25_path.addr = route.dest_addr;
		for (i = 0; i < route.digi_count; i++)
			ax25_path.digipeater[i] = route.digi_addr[i];
		ax25_path.dcount = route.digi_count;
		err = ax25_add_route(&ax25_path, dev);
		break;

	case SIOCDELRT:
		/* sanity checks */
		if (copy_from_user(&route, arg, sizeof(route))) {
			err = -EFAULT;
			break;
		}
		if ((dev = ax25rtr_get_dev(&route.port_addr)) == NULL)
			break;

		err = ax25_del_route(&route.dest_addr);
		break;

	case SIOCAX25OPTRT:
		/* sanity checks */
		if (copy_from_user(&rt_option, arg, sizeof(rt_option))) {
			err = -EFAULT;
			break;
		}
		if ((dev = ax25rtr_get_dev(&rt_option.port_addr)) == NULL)
			break;

		switch (rt_option.cmd) {
		case AX25_SET_RT_IPMODE:
			err = ax25_ipopt_route(&rt_option.dest_addr, rt_option.arg);
			break;
		}
	}

	return err;
}

int ax25_rt_get_info(char *buffer, char **start, off_t offset, int length)
{
	struct ax25_route *ax25_rt;
	int len     = 0;
	off_t pos   = 0;
	off_t begin = 0;
	char *callsign;
	int i;
  
	read_lock(&ax25_rt_lock);

	len += sprintf(buffer, "callsign  dev  mode digipeaters\n");

	for (ax25_rt = ax25_route; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
		if (ax25cmp(&ax25_rt->path.addr, &null_ax25_address) == 0)
			callsign = "default";
		else
			callsign = ax2asc(&ax25_rt->path.addr);
		len += sprintf(buffer + len, "%-9s %-4s",
			       callsign,
			       ax25_rt->dev ? ax25_rt->dev->name : "???");

		switch (ax25_rt->ip_mode) {
		case 'V':
			len += sprintf(buffer + len, "   vc");
			break;
		case 'D':
			len += sprintf(buffer + len, "   dg");
			break;
		case 'C':
			len += sprintf(buffer + len, "   vj");
			break;
		default:
			len += sprintf(buffer + len, "    *");
			break;
		}

		for (i = 0; i < ax25_rt->path.dcount; i++)
			len += sprintf(buffer + len, " %s", ax2asc(&ax25_rt->path.digipeater[i]));

		len += sprintf(buffer + len, "\n");

		pos = begin + len;

		if (pos < offset) {
			len   = 0;
			begin = pos;
		}

		if (pos > offset + length)
			break;
	}

	read_unlock(&ax25_rt_lock);

	*start = buffer + (offset - begin);
	len   -= (offset - begin);

	if (len > length) len = length;

	return len;
} 

/*
 *	Find AX.25 route
 */
struct ax25_route *ax25_find_route(ax25_address *addr)
{
	struct ax25_route *ax25_spe_rt = NULL;
	struct ax25_route *ax25_def_rt = NULL;
	struct ax25_route *ax25_rt;

	read_lock(&ax25_rt_lock);

	for (ax25_rt = ax25_route; ax25_rt != NULL; ax25_rt = ax25_rt->next) {
		if (ax25cmp(&ax25_rt->path.addr, addr) == 0)
			ax25_spe_rt = ax25_rt;
		if (ax25cmp(&ax25_rt->path.addr, &null_ax25_address) == 0)
			ax25_def_rt = ax25_rt;
	}

	if (ax25_spe_rt != NULL)
		return ax25_spe_rt;

	read_unlock(&ax25_rt_lock);

	return ax25_def_rt;
}

/*
 * 	MW: This is the core of the digipeating stuff. For a given
 * 	src/dest it finds the appropriate device and digipeaterpath
 *      to route to.
 */
struct net_device *ax25_rt_set_addr(ax25_addr_t *out, ax25_addr_t *in, struct net_device *dev, struct net_device *out_dev)
{
	struct ax25_route *ax25rt;
	ax25_address *next_dest;
	ax25_address *dptr;
	int more_digis;

	/*
	 * find out where to go next. we route the packet either
	 * to the next digi behind us or to the destination. We
	 * NEVER route to the destination if there are digipeaters
	 * left.
	 */
	more_digis = in->dcount - (in->lastrepeat+2);
	if (more_digis > 0)
		next_dest = &in->digipeater[in->lastrepeat+2];
	else
		next_dest = &in->dest;

	/*
	 * check for a route.
	 */
	if ((ax25rt = ax25_find_route(next_dest)) != NULL)
		out_dev = ax25rt->dev;

	/*
	 * set up the digipeater path.
	 * for now we just copy the path of the incoming SABM
	 * up to the digipeater before us, if any.
	 */
	out->dest = in->dest;
	out->src  = in->src;

	if (in->lastrepeat >= 0)
		memcpy(out->digipeater, in->digipeater, sizeof(ax25_address) * (in->lastrepeat+1));

	/*
	 * then we fill in the callsign of the device the frame
	 * came in.
	 */
	out->lastrepeat = in->lastrepeat+1;
	out->dcount     = out->lastrepeat+1;
	dptr    = &out->digipeater[(int) out->lastrepeat];
	*dptr++ = *((ax25_address *)dev->dev_addr);

	/*
	 * insert the route to next_dest, if any.
	 */
	if (ax25rt != NULL && ax25rt->path.dcount != 0) {
		memcpy(dptr, ax25rt->path.digipeater, sizeof(ax25_address) * ax25rt->path.dcount);
		out->dcount += ax25rt->path.dcount;
		dptr += ax25rt->path.dcount;
	}

	/*
	 * append next_dest if we route to a waypoint
	 */
	while (more_digis-- > 0 && out->dcount <= AX25_MAX_DIGIS) {
		*dptr++ = *next_dest++;
		out->dcount++;
	}
	return out_dev;
}

/*
 *      Find the device to use
 */
int ax25_rt_fillin_dev(ax25_cb *ax25, ax25_address *addr)
{
	struct ax25_route *ax25_rt;

	if ((ax25_rt = ax25_find_route(addr)) == NULL)
		return -EHOSTUNREACH;

/* 	ax25_remove_cb(ax25); */
	ax25_fillin_cb(ax25, ax25_rt->dev);
/* 	ax25_insert_cb(ax25); */

	return 0;
}

/*
 *	Return the IP mode of a given callsign/device pair.
 */
char ax25_rt_mode_get(ax25_address *callsign)
{
	struct ax25_route *ax25_rt;

	read_lock(&ax25_rt_lock);

	for (ax25_rt = ax25_route; ax25_rt != NULL; ax25_rt = ax25_rt->next)
		if (ax25cmp(&ax25_rt->path.addr, callsign) == 0)
			return ax25_rt->ip_mode;

	read_unlock(&ax25_rt_lock);

	return ' ';
}



/*
 *	Free all memory associated with routing and device structures.
 */
void ax25_rt_free(void)
{
	struct ax25_route *s, *ax25_rt = ax25_route;

	write_lock(&ax25_rt_lock);

	while (ax25_rt != NULL) {
		s       = ax25_rt;
		ax25_rt = ax25_rt->next;
		kfree(s);
	}

	write_unlock(&ax25_rt_lock);
}