blob: b95fae9ab393976e682b245b537e8b9b6251e2d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * X.25 Packet Layer release 002
3 *
4 * This is ALPHA test software. This code may break your machine,
5 * randomly fail to work with new releases, misbehave and/or generally
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09006 * screw up. It might even work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This code REQUIRES 2.1.15 or higher
9 *
10 * This module:
11 * This module is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * History
17 * X.25 001 Jonathan Naylor Started coding.
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/if_arp.h>
21#include <linux/init.h>
22#include <net/x25.h>
23
Denis Cheng0e3cf7e2007-12-07 00:50:43 -080024LIST_HEAD(x25_route_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025DEFINE_RWLOCK(x25_route_list_lock);
26
27/*
28 * Add a new route.
29 */
30static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
31 struct net_device *dev)
32{
33 struct x25_route *rt;
34 struct list_head *entry;
35 int rc = -EINVAL;
36
37 write_lock_bh(&x25_route_list_lock);
38
39 list_for_each(entry, &x25_route_list) {
40 rt = list_entry(entry, struct x25_route, node);
41
42 if (!memcmp(&rt->address, address, sigdigits) &&
43 rt->sigdigits == sigdigits)
44 goto out;
45 }
46
47 rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
48 rc = -ENOMEM;
49 if (!rt)
50 goto out;
51
52 strcpy(rt->address.x25_addr, "000000000000000");
53 memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
54
55 rt->sigdigits = sigdigits;
56 rt->dev = dev;
57 atomic_set(&rt->refcnt, 1);
58
59 list_add(&rt->node, &x25_route_list);
60 rc = 0;
61out:
62 write_unlock_bh(&x25_route_list_lock);
63 return rc;
64}
65
66/**
67 * __x25_remove_route - remove route from x25_route_list
68 * @rt - route to remove
69 *
70 * Remove route from x25_route_list. If it was there.
71 * Caller must hold x25_route_list_lock.
72 */
73static void __x25_remove_route(struct x25_route *rt)
74{
75 if (rt->node.next) {
76 list_del(&rt->node);
77 x25_route_put(rt);
78 }
79}
80
81static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
82 struct net_device *dev)
83{
84 struct x25_route *rt;
85 struct list_head *entry;
86 int rc = -EINVAL;
87
88 write_lock_bh(&x25_route_list_lock);
89
90 list_for_each(entry, &x25_route_list) {
91 rt = list_entry(entry, struct x25_route, node);
92
93 if (!memcmp(&rt->address, address, sigdigits) &&
94 rt->sigdigits == sigdigits && rt->dev == dev) {
95 __x25_remove_route(rt);
96 rc = 0;
97 break;
98 }
99 }
100
101 write_unlock_bh(&x25_route_list_lock);
102 return rc;
103}
104
105/*
106 * A device has been removed, remove its routes.
107 */
108void x25_route_device_down(struct net_device *dev)
109{
110 struct x25_route *rt;
111 struct list_head *entry, *tmp;
112
113 write_lock_bh(&x25_route_list_lock);
114
115 list_for_each_safe(entry, tmp, &x25_route_list) {
116 rt = list_entry(entry, struct x25_route, node);
117
118 if (rt->dev == dev)
119 __x25_remove_route(rt);
120 }
121 write_unlock_bh(&x25_route_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800122
123 /* Remove any related forwarding */
124 x25_clear_forward_by_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/*
128 * Check that the device given is a valid X.25 interface that is "up".
129 */
130struct net_device *x25_dev_get(char *devname)
131{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700132 struct net_device *dev = dev_get_by_name(&init_net, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (dev &&
135 (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
136#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
137 && dev->type != ARPHRD_ETHER
138#endif
andrew hendry429d33a2009-11-24 15:16:05 +0000139 ))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dev_put(dev);
andrew hendry429d33a2009-11-24 15:16:05 +0000141 dev = NULL;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return dev;
145}
146
147/**
148 * x25_get_route - Find a route given an X.25 address.
149 * @addr - address to find a route for
150 *
151 * Find a route given an X.25 address.
152 */
153struct x25_route *x25_get_route(struct x25_address *addr)
154{
155 struct x25_route *rt, *use = NULL;
156 struct list_head *entry;
157
158 read_lock_bh(&x25_route_list_lock);
159
160 list_for_each(entry, &x25_route_list) {
161 rt = list_entry(entry, struct x25_route, node);
162
163 if (!memcmp(&rt->address, addr, rt->sigdigits)) {
164 if (!use)
165 use = rt;
166 else if (rt->sigdigits > use->sigdigits)
167 use = rt;
168 }
169 }
170
171 if (use)
172 x25_route_hold(use);
173
174 read_unlock_bh(&x25_route_list_lock);
175 return use;
176}
177
178/*
179 * Handle the ioctls that control the routing functions.
180 */
181int x25_route_ioctl(unsigned int cmd, void __user *arg)
182{
183 struct x25_route_struct rt;
184 struct net_device *dev;
185 int rc = -EINVAL;
186
187 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
188 goto out;
189
190 rc = -EFAULT;
191 if (copy_from_user(&rt, arg, sizeof(rt)))
192 goto out;
193
194 rc = -EINVAL;
roel kluin091bb8a2009-10-23 05:21:56 +0000195 if (rt.sigdigits > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto out;
197
198 dev = x25_dev_get(rt.device);
199 if (!dev)
200 goto out;
201
202 if (cmd == SIOCADDRT)
203 rc = x25_add_route(&rt.address, rt.sigdigits, dev);
204 else
205 rc = x25_del_route(&rt.address, rt.sigdigits, dev);
206 dev_put(dev);
207out:
208 return rc;
209}
210
211/*
212 * Release all memory associated with X.25 routing structures.
213 */
214void __exit x25_route_free(void)
215{
216 struct x25_route *rt;
217 struct list_head *entry, *tmp;
218
219 write_lock_bh(&x25_route_list_lock);
220 list_for_each_safe(entry, tmp, &x25_route_list) {
221 rt = list_entry(entry, struct x25_route, node);
222 __x25_remove_route(rt);
223 }
224 write_unlock_bh(&x25_route_list_lock);
225}