blob: 9fbe4bb38d942ee2e2c6a22feb45a62500581796 [file] [log] [blame]
Thomas Gleixneree5d8f42019-05-20 19:08:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * X.25 Packet Layer release 002
4 *
5 * This is ALPHA test software. This code may break your machine,
6 * randomly fail to work with new releases, misbehave and/or generally
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09007 * screw up. It might even work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This code REQUIRES 2.1.15 or higher
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * History
12 * X.25 001 Jonathan Naylor Started coding.
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/if_arp.h>
16#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <net/x25.h>
19
Denis Cheng0e3cf7e2007-12-07 00:50:43 -080020LIST_HEAD(x25_route_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021DEFINE_RWLOCK(x25_route_list_lock);
22
23/*
24 * Add a new route.
25 */
26static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
27 struct net_device *dev)
28{
29 struct x25_route *rt;
30 struct list_head *entry;
31 int rc = -EINVAL;
32
33 write_lock_bh(&x25_route_list_lock);
34
35 list_for_each(entry, &x25_route_list) {
36 rt = list_entry(entry, struct x25_route, node);
37
38 if (!memcmp(&rt->address, address, sigdigits) &&
39 rt->sigdigits == sigdigits)
40 goto out;
41 }
42
43 rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
44 rc = -ENOMEM;
45 if (!rt)
46 goto out;
47
48 strcpy(rt->address.x25_addr, "000000000000000");
49 memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
50
51 rt->sigdigits = sigdigits;
52 rt->dev = dev;
Reshetova, Elena5f9ccf62017-07-04 15:53:19 +030053 refcount_set(&rt->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 list_add(&rt->node, &x25_route_list);
56 rc = 0;
57out:
58 write_unlock_bh(&x25_route_list_lock);
59 return rc;
60}
61
62/**
63 * __x25_remove_route - remove route from x25_route_list
Ben Hutchings2c530402012-07-10 10:55:09 +000064 * @rt: route to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
66 * Remove route from x25_route_list. If it was there.
67 * Caller must hold x25_route_list_lock.
68 */
69static void __x25_remove_route(struct x25_route *rt)
70{
71 if (rt->node.next) {
72 list_del(&rt->node);
73 x25_route_put(rt);
74 }
75}
76
77static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
78 struct net_device *dev)
79{
80 struct x25_route *rt;
81 struct list_head *entry;
82 int rc = -EINVAL;
83
84 write_lock_bh(&x25_route_list_lock);
85
86 list_for_each(entry, &x25_route_list) {
87 rt = list_entry(entry, struct x25_route, node);
88
89 if (!memcmp(&rt->address, address, sigdigits) &&
90 rt->sigdigits == sigdigits && rt->dev == dev) {
91 __x25_remove_route(rt);
92 rc = 0;
93 break;
94 }
95 }
96
97 write_unlock_bh(&x25_route_list_lock);
98 return rc;
99}
100
101/*
102 * A device has been removed, remove its routes.
103 */
104void x25_route_device_down(struct net_device *dev)
105{
106 struct x25_route *rt;
107 struct list_head *entry, *tmp;
108
109 write_lock_bh(&x25_route_list_lock);
110
111 list_for_each_safe(entry, tmp, &x25_route_list) {
112 rt = list_entry(entry, struct x25_route, node);
113
114 if (rt->dev == dev)
115 __x25_remove_route(rt);
116 }
117 write_unlock_bh(&x25_route_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/*
121 * Check that the device given is a valid X.25 interface that is "up".
122 */
123struct net_device *x25_dev_get(char *devname)
124{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700125 struct net_device *dev = dev_get_by_name(&init_net, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Xie He13458ff2020-12-08 19:33:46 -0800127 if (dev && (!(dev->flags & IFF_UP) || dev->type != ARPHRD_X25)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 dev_put(dev);
andrew hendry429d33a2009-11-24 15:16:05 +0000129 dev = NULL;
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return dev;
133}
134
135/**
136 * x25_get_route - Find a route given an X.25 address.
Andrew Lunn62c89232020-07-13 01:15:16 +0200137 * @addr: - address to find a route for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
139 * Find a route given an X.25 address.
140 */
141struct x25_route *x25_get_route(struct x25_address *addr)
142{
143 struct x25_route *rt, *use = NULL;
144 struct list_head *entry;
145
146 read_lock_bh(&x25_route_list_lock);
147
148 list_for_each(entry, &x25_route_list) {
149 rt = list_entry(entry, struct x25_route, node);
150
151 if (!memcmp(&rt->address, addr, rt->sigdigits)) {
152 if (!use)
153 use = rt;
154 else if (rt->sigdigits > use->sigdigits)
155 use = rt;
156 }
157 }
158
159 if (use)
160 x25_route_hold(use);
161
162 read_unlock_bh(&x25_route_list_lock);
163 return use;
164}
165
166/*
167 * Handle the ioctls that control the routing functions.
168 */
169int x25_route_ioctl(unsigned int cmd, void __user *arg)
170{
171 struct x25_route_struct rt;
172 struct net_device *dev;
173 int rc = -EINVAL;
174
175 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
176 goto out;
177
178 rc = -EFAULT;
179 if (copy_from_user(&rt, arg, sizeof(rt)))
180 goto out;
181
182 rc = -EINVAL;
roel kluin091bb8a2009-10-23 05:21:56 +0000183 if (rt.sigdigits > 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
185
186 dev = x25_dev_get(rt.device);
187 if (!dev)
188 goto out;
189
190 if (cmd == SIOCADDRT)
191 rc = x25_add_route(&rt.address, rt.sigdigits, dev);
192 else
193 rc = x25_del_route(&rt.address, rt.sigdigits, dev);
194 dev_put(dev);
195out:
196 return rc;
197}
198
199/*
200 * Release all memory associated with X.25 routing structures.
201 */
202void __exit x25_route_free(void)
203{
204 struct x25_route *rt;
205 struct list_head *entry, *tmp;
206
207 write_lock_bh(&x25_route_list_lock);
208 list_for_each_safe(entry, tmp, &x25_route_list) {
209 rt = list_entry(entry, struct x25_route, node);
210 __x25_remove_route(rt);
211 }
212 write_unlock_bh(&x25_route_list_lock);
213}