summaryrefslogtreecommitdiffstats
path: root/net/x25/x25_route.c
blob: 4cb51300bc193b437a1e97ad3a5fd4f7d28b27c0 (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
/*
 *	X.25 Packet Layer release 002
 *
 *	This is ALPHA test software. This code may break your machine, randomly fail to work with new 
 *	releases, misbehave and/or generally screw up. It might even work. 
 *
 *	This code REQUIRES 2.1.15 or higher
 *
 *	This module:
 *		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.
 *
 *	History
 *	X.25 001	Jonathan Naylor	Started coding.
 */

#include <linux/config.h>
#if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <net/arp.h>
#include <linux/if_arp.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/fcntl.h>
#include <linux/termios.h>	/* For TIOCINQ/OUTQ */
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <net/x25.h>

static struct x25_route *x25_route_list = NULL;

/*
 *	Add a new route.
 */
static int x25_add_route(x25_address *address, unsigned int sigdigits, struct net_device *dev)
{
	struct x25_route *x25_route;
	unsigned long flags;

	for (x25_route = x25_route_list; x25_route != NULL; x25_route = x25_route->next)
		if (memcmp(&x25_route->address, address, sigdigits) == 0 && x25_route->sigdigits == sigdigits)
			return -EINVAL;

	if ((x25_route = kmalloc(sizeof(*x25_route), GFP_ATOMIC)) == NULL)
		return -ENOMEM;

	strcpy(x25_route->address.x25_addr, "000000000000000");
	memcpy(x25_route->address.x25_addr, address->x25_addr, sigdigits);

	x25_route->sigdigits = sigdigits;
	x25_route->dev       = dev;

	save_flags(flags); cli();
	x25_route->next = x25_route_list;
	x25_route_list  = x25_route;
	restore_flags(flags);

	return 0;
}

static void x25_remove_route(struct x25_route *x25_route)
{
	struct x25_route *s;
	unsigned long flags;

	save_flags(flags);
	cli();

	if ((s = x25_route_list) == x25_route) {
		x25_route_list = x25_route->next;
		restore_flags(flags);
		kfree(x25_route);
		return;
	}

	while (s != NULL && s->next != NULL) {
		if (s->next == x25_route) {
			s->next = x25_route->next;
			restore_flags(flags);
			kfree(x25_route);
			return;
		}

		s = s->next;
	}

	restore_flags(flags);
}

static int x25_del_route(x25_address *address, unsigned int sigdigits, struct net_device *dev)
{
	struct x25_route *x25_route;

	for (x25_route = x25_route_list; x25_route != NULL; x25_route = x25_route->next) {
		if (memcmp(&x25_route->address, address, sigdigits) == 0 && x25_route->sigdigits == sigdigits && x25_route->dev == dev) {
			x25_remove_route(x25_route);
			return 0;
		}
	}

	return -EINVAL;
}

/*
 *	A device has been removed, remove its routes.
 */
void x25_route_device_down(struct net_device *dev)
{
	struct x25_route *route, *x25_route = x25_route_list;

	while (x25_route != NULL) {
		route     = x25_route;
		x25_route = x25_route->next;

		if (route->dev == dev)
			x25_remove_route(route);
	}
}

/*
 *	Check that the device given is a valid X.25 interface that is "up".
 */
struct net_device *x25_dev_get(char *devname)
{
	struct net_device *dev;

	if ((dev = dev_get_by_name(devname)) == NULL)
		return NULL;

	if ((dev->flags & IFF_UP) && (dev->type == ARPHRD_X25
#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
	   || dev->type == ARPHRD_ETHER
#endif
	   ))
		return dev;

	dev_put(dev);

	return NULL;
}

/*
 *	Find a device given an X.25 address.
 */
struct net_device *x25_get_route(x25_address *addr)
{
	struct x25_route *route, *use = NULL;

	for (route = x25_route_list; route != NULL; route = route->next) {
		if (memcmp(&route->address, addr, route->sigdigits) == 0) {
			if (use == NULL) {
				use = route;
			} else {
				if (route->sigdigits > use->sigdigits)
					use = route;
			}
		}
	}

	return (use != NULL) ? use->dev : NULL;
}

/*
 *	Handle the ioctls that control the routing functions.
 */
int x25_route_ioctl(unsigned int cmd, void *arg)
{
	struct x25_route_struct x25_route;
	struct net_device *dev;
	int err;

	switch (cmd) {

		case SIOCADDRT:
			if (copy_from_user(&x25_route, arg, sizeof(struct x25_route_struct)))
				return -EFAULT;
			if (x25_route.sigdigits < 0 || x25_route.sigdigits > 15)
				return -EINVAL;
			if ((dev = x25_dev_get(x25_route.device)) == NULL)
				return -EINVAL;
			err = x25_add_route(&x25_route.address, x25_route.sigdigits, dev);
			dev_put(dev);
			return err;

		case SIOCDELRT:
			if (copy_from_user(&x25_route, arg, sizeof(struct x25_route_struct)))
				return -EFAULT;
			if (x25_route.sigdigits < 0 || x25_route.sigdigits > 15)
				return -EINVAL;
			if ((dev = x25_dev_get(x25_route.device)) == NULL)
				return -EINVAL;
			err = x25_del_route(&x25_route.address, x25_route.sigdigits, dev);
			dev_put(dev);
			return err;

		default:
			return -EINVAL;
	}

	return 0;
}

int x25_routes_get_info(char *buffer, char **start, off_t offset, int length)
{
	struct x25_route *x25_route;
	int len     = 0;
	off_t pos   = 0;
	off_t begin = 0;

	cli();

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

	for (x25_route = x25_route_list; x25_route != NULL; x25_route = x25_route->next) {
		len += sprintf(buffer + len, "%-15s  %-6d  %-5s\n",
			x25_route->address.x25_addr,
			x25_route->sigdigits,
			(x25_route->dev != NULL) ? x25_route->dev->name : "???");

		pos = begin + len;

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

		if (pos > offset + length)
			break;
	}

	sti();

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

	if (len > length) len = length;

	return len;
} 

#ifdef MODULE

/*
 *	Release all memory associated with X.25 routing structures.
 */
void x25_route_free(void)
{
	struct x25_route *route, *x25_route = x25_route_list;

	while (x25_route != NULL) {
		route     = x25_route;
		x25_route = x25_route->next;

		x25_remove_route(route);
	}
}

#endif

#endif