[PATCH] USB: Fix irda-usb use after use
[linux-2.6/mini2440.git] / net / x25 / x25_route.c
blob6c5d37517035866e874cef3f82e1cda304fbafd4
1 /*
2 * X.25 Packet Layer release 002
4 * This is ALPHA test software. This code may break your machine,
5 * randomly fail to work with new releases, misbehave and/or generally
6 * screw up. It might even work.
8 * This code REQUIRES 2.1.15 or higher
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.
16 * History
17 * X.25 001 Jonathan Naylor Started coding.
20 #include <linux/config.h>
21 #include <linux/if_arp.h>
22 #include <linux/init.h>
23 #include <net/x25.h>
25 struct list_head x25_route_list = LIST_HEAD_INIT(x25_route_list);
26 DEFINE_RWLOCK(x25_route_list_lock);
29 * Add a new route.
31 static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
32 struct net_device *dev)
34 struct x25_route *rt;
35 struct list_head *entry;
36 int rc = -EINVAL;
38 write_lock_bh(&x25_route_list_lock);
40 list_for_each(entry, &x25_route_list) {
41 rt = list_entry(entry, struct x25_route, node);
43 if (!memcmp(&rt->address, address, sigdigits) &&
44 rt->sigdigits == sigdigits)
45 goto out;
48 rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
49 rc = -ENOMEM;
50 if (!rt)
51 goto out;
53 strcpy(rt->address.x25_addr, "000000000000000");
54 memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
56 rt->sigdigits = sigdigits;
57 rt->dev = dev;
58 atomic_set(&rt->refcnt, 1);
60 list_add(&rt->node, &x25_route_list);
61 rc = 0;
62 out:
63 write_unlock_bh(&x25_route_list_lock);
64 return rc;
67 /**
68 * __x25_remove_route - remove route from x25_route_list
69 * @rt - route to remove
71 * Remove route from x25_route_list. If it was there.
72 * Caller must hold x25_route_list_lock.
74 static void __x25_remove_route(struct x25_route *rt)
76 if (rt->node.next) {
77 list_del(&rt->node);
78 x25_route_put(rt);
82 static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
83 struct net_device *dev)
85 struct x25_route *rt;
86 struct list_head *entry;
87 int rc = -EINVAL;
89 write_lock_bh(&x25_route_list_lock);
91 list_for_each(entry, &x25_route_list) {
92 rt = list_entry(entry, struct x25_route, node);
94 if (!memcmp(&rt->address, address, sigdigits) &&
95 rt->sigdigits == sigdigits && rt->dev == dev) {
96 __x25_remove_route(rt);
97 rc = 0;
98 break;
102 write_unlock_bh(&x25_route_list_lock);
103 return rc;
107 * A device has been removed, remove its routes.
109 void x25_route_device_down(struct net_device *dev)
111 struct x25_route *rt;
112 struct list_head *entry, *tmp;
114 write_lock_bh(&x25_route_list_lock);
116 list_for_each_safe(entry, tmp, &x25_route_list) {
117 rt = list_entry(entry, struct x25_route, node);
119 if (rt->dev == dev)
120 __x25_remove_route(rt);
122 write_unlock_bh(&x25_route_list_lock);
126 * Check that the device given is a valid X.25 interface that is "up".
128 struct net_device *x25_dev_get(char *devname)
130 struct net_device *dev = dev_get_by_name(devname);
132 if (dev &&
133 (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
134 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
135 && dev->type != ARPHRD_ETHER
136 #endif
138 dev_put(dev);
140 return dev;
144 * x25_get_route - Find a route given an X.25 address.
145 * @addr - address to find a route for
147 * Find a route given an X.25 address.
149 struct x25_route *x25_get_route(struct x25_address *addr)
151 struct x25_route *rt, *use = NULL;
152 struct list_head *entry;
154 read_lock_bh(&x25_route_list_lock);
156 list_for_each(entry, &x25_route_list) {
157 rt = list_entry(entry, struct x25_route, node);
159 if (!memcmp(&rt->address, addr, rt->sigdigits)) {
160 if (!use)
161 use = rt;
162 else if (rt->sigdigits > use->sigdigits)
163 use = rt;
167 if (use)
168 x25_route_hold(use);
170 read_unlock_bh(&x25_route_list_lock);
171 return use;
175 * Handle the ioctls that control the routing functions.
177 int x25_route_ioctl(unsigned int cmd, void __user *arg)
179 struct x25_route_struct rt;
180 struct net_device *dev;
181 int rc = -EINVAL;
183 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
184 goto out;
186 rc = -EFAULT;
187 if (copy_from_user(&rt, arg, sizeof(rt)))
188 goto out;
190 rc = -EINVAL;
191 if (rt.sigdigits < 0 || rt.sigdigits > 15)
192 goto out;
194 dev = x25_dev_get(rt.device);
195 if (!dev)
196 goto out;
198 if (cmd == SIOCADDRT)
199 rc = x25_add_route(&rt.address, rt.sigdigits, dev);
200 else
201 rc = x25_del_route(&rt.address, rt.sigdigits, dev);
202 dev_put(dev);
203 out:
204 return rc;
208 * Release all memory associated with X.25 routing structures.
210 void __exit x25_route_free(void)
212 struct x25_route *rt;
213 struct list_head *entry, *tmp;
215 write_lock_bh(&x25_route_list_lock);
216 list_for_each_safe(entry, tmp, &x25_route_list) {
217 rt = list_entry(entry, struct x25_route, node);
218 __x25_remove_route(rt);
220 write_unlock_bh(&x25_route_list_lock);