blob: 7a1778a411873316134393b09142f39702297162 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/x25.h>
Sachin Sharma20d658e2014-02-18 00:16:23 -080024#ifdef KW_TAINT_ANALYSIS
25 extern void * get_tainted_stuff();
26#endif
Denis Cheng0e3cf7e2007-12-07 00:50:43 -080027LIST_HEAD(x25_route_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028DEFINE_RWLOCK(x25_route_list_lock);
29
30/*
31 * Add a new route.
32 */
33static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
34 struct net_device *dev)
35{
36 struct x25_route *rt;
37 struct list_head *entry;
38 int rc = -EINVAL;
39
40 write_lock_bh(&x25_route_list_lock);
41
42 list_for_each(entry, &x25_route_list) {
43 rt = list_entry(entry, struct x25_route, node);
44
45 if (!memcmp(&rt->address, address, sigdigits) &&
46 rt->sigdigits == sigdigits)
47 goto out;
48 }
49
50 rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
51 rc = -ENOMEM;
52 if (!rt)
53 goto out;
54
55 strcpy(rt->address.x25_addr, "000000000000000");
56 memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
57
58 rt->sigdigits = sigdigits;
59 rt->dev = dev;
60 atomic_set(&rt->refcnt, 1);
61
62 list_add(&rt->node, &x25_route_list);
63 rc = 0;
64out:
65 write_unlock_bh(&x25_route_list_lock);
66 return rc;
67}
68
69/**
70 * __x25_remove_route - remove route from x25_route_list
71 * @rt - route to remove
72 *
73 * Remove route from x25_route_list. If it was there.
74 * Caller must hold x25_route_list_lock.
75 */
76static void __x25_remove_route(struct x25_route *rt)
77{
78 if (rt->node.next) {
79 list_del(&rt->node);
80 x25_route_put(rt);
81 }
82}
83
84static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
85 struct net_device *dev)
86{
87 struct x25_route *rt;
88 struct list_head *entry;
89 int rc = -EINVAL;
90
91 write_lock_bh(&x25_route_list_lock);
92
93 list_for_each(entry, &x25_route_list) {
94 rt = list_entry(entry, struct x25_route, node);
95
96 if (!memcmp(&rt->address, address, sigdigits) &&
97 rt->sigdigits == sigdigits && rt->dev == dev) {
98 __x25_remove_route(rt);
99 rc = 0;
100 break;
101 }
102 }
103
104 write_unlock_bh(&x25_route_list_lock);
105 return rc;
106}
107
108/*
109 * A device has been removed, remove its routes.
110 */
111void x25_route_device_down(struct net_device *dev)
112{
113 struct x25_route *rt;
114 struct list_head *entry, *tmp;
115
116 write_lock_bh(&x25_route_list_lock);
117
118 list_for_each_safe(entry, tmp, &x25_route_list) {
119 rt = list_entry(entry, struct x25_route, node);
120
121 if (rt->dev == dev)
122 __x25_remove_route(rt);
123 }
124 write_unlock_bh(&x25_route_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800125
126 /* Remove any related forwarding */
127 x25_clear_forward_by_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/*
131 * Check that the device given is a valid X.25 interface that is "up".
132 */
133struct net_device *x25_dev_get(char *devname)
134{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700135 struct net_device *dev = dev_get_by_name(&init_net, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (dev &&
138 (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
Igor Maravić29c36262011-12-12 02:58:23 +0000139#if IS_ENABLED(CONFIG_LLC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 && dev->type != ARPHRD_ETHER
141#endif
andrew hendry429d33a2009-11-24 15:16:05 +0000142 ))){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dev_put(dev);
andrew hendry429d33a2009-11-24 15:16:05 +0000144 dev = NULL;
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 return dev;
148}
149
150/**
151 * x25_get_route - Find a route given an X.25 address.
152 * @addr - address to find a route for
153 *
154 * Find a route given an X.25 address.
155 */
156struct x25_route *x25_get_route(struct x25_address *addr)
157{
158 struct x25_route *rt, *use = NULL;
159 struct list_head *entry;
160
161 read_lock_bh(&x25_route_list_lock);
162
163 list_for_each(entry, &x25_route_list) {
164 rt = list_entry(entry, struct x25_route, node);
165
166 if (!memcmp(&rt->address, addr, rt->sigdigits)) {
167 if (!use)
168 use = rt;
169 else if (rt->sigdigits > use->sigdigits)
170 use = rt;
171 }
172 }
173
174 if (use)
175 x25_route_hold(use);
176
177 read_unlock_bh(&x25_route_list_lock);
178 return use;
179}
180
181/*
182 * Handle the ioctls that control the routing functions.
183 */
Sachin Sharma20d658e2014-02-18 00:16:23 -0800184int x25_route_ioctl(unsigned int cmd, void __user *arg_actual)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct x25_route_struct rt;
187 struct net_device *dev;
188 int rc = -EINVAL;
Sachin Sharma20d658e2014-02-18 00:16:23 -0800189 #ifdef KW_TAINT_ANALYSIS
190 void __user *arg = (void __user *)get_tainted_stuff();
191 #else
192 void __user *arg = arg_actual;
193 #endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
195 goto out;
196
197 rc = -EFAULT;
198 if (copy_from_user(&rt, arg, sizeof(rt)))
199 goto out;
200
201 rc = -EINVAL;
roel kluin091bb8a2009-10-23 05:21:56 +0000202 if (rt.sigdigits > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto out;
204
205 dev = x25_dev_get(rt.device);
206 if (!dev)
207 goto out;
208
209 if (cmd == SIOCADDRT)
210 rc = x25_add_route(&rt.address, rt.sigdigits, dev);
211 else
212 rc = x25_del_route(&rt.address, rt.sigdigits, dev);
213 dev_put(dev);
214out:
215 return rc;
216}
217
218/*
219 * Release all memory associated with X.25 routing structures.
220 */
221void __exit x25_route_free(void)
222{
223 struct x25_route *rt;
224 struct list_head *entry, *tmp;
225
226 write_lock_bh(&x25_route_list_lock);
227 list_for_each_safe(entry, tmp, &x25_route_list) {
228 rt = list_entry(entry, struct x25_route, node);
229 __x25_remove_route(rt);
230 }
231 write_unlock_bh(&x25_route_list_lock);
232}