Revert "MUSB: Remove usb_musb_pm_init() call"
[linux-ginger.git] / net / x25 / x25_route.c
blob2c999ccf504a82741961e6a2420ccb07fef13b03
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/if_arp.h>
21 #include <linux/init.h>
22 #include <net/x25.h>
24 LIST_HEAD(x25_route_list);
25 DEFINE_RWLOCK(x25_route_list_lock);
28 * Add a new route.
30 static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
31 struct net_device *dev)
33 struct x25_route *rt;
34 struct list_head *entry;
35 int rc = -EINVAL;
37 write_lock_bh(&x25_route_list_lock);
39 list_for_each(entry, &x25_route_list) {
40 rt = list_entry(entry, struct x25_route, node);
42 if (!memcmp(&rt->address, address, sigdigits) &&
43 rt->sigdigits == sigdigits)
44 goto out;
47 rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
48 rc = -ENOMEM;
49 if (!rt)
50 goto out;
52 strcpy(rt->address.x25_addr, "000000000000000");
53 memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
55 rt->sigdigits = sigdigits;
56 rt->dev = dev;
57 atomic_set(&rt->refcnt, 1);
59 list_add(&rt->node, &x25_route_list);
60 rc = 0;
61 out:
62 write_unlock_bh(&x25_route_list_lock);
63 return rc;
66 /**
67 * __x25_remove_route - remove route from x25_route_list
68 * @rt - route to remove
70 * Remove route from x25_route_list. If it was there.
71 * Caller must hold x25_route_list_lock.
73 static void __x25_remove_route(struct x25_route *rt)
75 if (rt->node.next) {
76 list_del(&rt->node);
77 x25_route_put(rt);
81 static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
82 struct net_device *dev)
84 struct x25_route *rt;
85 struct list_head *entry;
86 int rc = -EINVAL;
88 write_lock_bh(&x25_route_list_lock);
90 list_for_each(entry, &x25_route_list) {
91 rt = list_entry(entry, struct x25_route, node);
93 if (!memcmp(&rt->address, address, sigdigits) &&
94 rt->sigdigits == sigdigits && rt->dev == dev) {
95 __x25_remove_route(rt);
96 rc = 0;
97 break;
101 write_unlock_bh(&x25_route_list_lock);
102 return rc;
106 * A device has been removed, remove its routes.
108 void x25_route_device_down(struct net_device *dev)
110 struct x25_route *rt;
111 struct list_head *entry, *tmp;
113 write_lock_bh(&x25_route_list_lock);
115 list_for_each_safe(entry, tmp, &x25_route_list) {
116 rt = list_entry(entry, struct x25_route, node);
118 if (rt->dev == dev)
119 __x25_remove_route(rt);
121 write_unlock_bh(&x25_route_list_lock);
123 /* Remove any related forwarding */
124 x25_clear_forward_by_dev(dev);
128 * Check that the device given is a valid X.25 interface that is "up".
130 struct net_device *x25_dev_get(char *devname)
132 struct net_device *dev = dev_get_by_name(&init_net, devname);
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
140 dev_put(dev);
142 return dev;
146 * x25_get_route - Find a route given an X.25 address.
147 * @addr - address to find a route for
149 * Find a route given an X.25 address.
151 struct x25_route *x25_get_route(struct x25_address *addr)
153 struct x25_route *rt, *use = NULL;
154 struct list_head *entry;
156 read_lock_bh(&x25_route_list_lock);
158 list_for_each(entry, &x25_route_list) {
159 rt = list_entry(entry, struct x25_route, node);
161 if (!memcmp(&rt->address, addr, rt->sigdigits)) {
162 if (!use)
163 use = rt;
164 else if (rt->sigdigits > use->sigdigits)
165 use = rt;
169 if (use)
170 x25_route_hold(use);
172 read_unlock_bh(&x25_route_list_lock);
173 return use;
177 * Handle the ioctls that control the routing functions.
179 int x25_route_ioctl(unsigned int cmd, void __user *arg)
181 struct x25_route_struct rt;
182 struct net_device *dev;
183 int rc = -EINVAL;
185 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
186 goto out;
188 rc = -EFAULT;
189 if (copy_from_user(&rt, arg, sizeof(rt)))
190 goto out;
192 rc = -EINVAL;
193 if (rt.sigdigits < 0 || rt.sigdigits > 15)
194 goto out;
196 dev = x25_dev_get(rt.device);
197 if (!dev)
198 goto out;
200 if (cmd == SIOCADDRT)
201 rc = x25_add_route(&rt.address, rt.sigdigits, dev);
202 else
203 rc = x25_del_route(&rt.address, rt.sigdigits, dev);
204 dev_put(dev);
205 out:
206 return rc;
210 * Release all memory associated with X.25 routing structures.
212 void __exit x25_route_free(void)
214 struct x25_route *rt;
215 struct list_head *entry, *tmp;
217 write_lock_bh(&x25_route_list_lock);
218 list_for_each_safe(entry, tmp, &x25_route_list) {
219 rt = list_entry(entry, struct x25_route, node);
220 __x25_remove_route(rt);
222 write_unlock_bh(&x25_route_list_lock);