1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * X.25 Packet Layer release 002
5 * This is ALPHA test software. This code may break your machine,
6 * randomly fail to work with new releases, misbehave and/or generally
7 * screw up. It might even work.
9 * This code REQUIRES 2.1.15 or higher
12 * X.25 001 Jonathan Naylor Started coding.
15 #include <linux/if_arp.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
20 LIST_HEAD(x25_route_list
);
21 DEFINE_RWLOCK(x25_route_list_lock
);
26 static int x25_add_route(struct x25_address
*address
, unsigned int sigdigits
,
27 struct net_device
*dev
)
32 write_lock_bh(&x25_route_list_lock
);
34 list_for_each_entry(rt
, &x25_route_list
, node
) {
35 if (!memcmp(&rt
->address
, address
, sigdigits
) &&
36 rt
->sigdigits
== sigdigits
)
40 rt
= kmalloc(sizeof(*rt
), GFP_ATOMIC
);
45 strcpy(rt
->address
.x25_addr
, "000000000000000");
46 memcpy(rt
->address
.x25_addr
, address
->x25_addr
, sigdigits
);
48 rt
->sigdigits
= sigdigits
;
50 refcount_set(&rt
->refcnt
, 1);
52 list_add(&rt
->node
, &x25_route_list
);
55 write_unlock_bh(&x25_route_list_lock
);
60 * __x25_remove_route - remove route from x25_route_list
61 * @rt: route to remove
63 * Remove route from x25_route_list. If it was there.
64 * Caller must hold x25_route_list_lock.
66 static void __x25_remove_route(struct x25_route
*rt
)
74 static int x25_del_route(struct x25_address
*address
, unsigned int sigdigits
,
75 struct net_device
*dev
)
80 write_lock_bh(&x25_route_list_lock
);
82 list_for_each_entry(rt
, &x25_route_list
, node
) {
83 if (!memcmp(&rt
->address
, address
, sigdigits
) &&
84 rt
->sigdigits
== sigdigits
&& rt
->dev
== dev
) {
85 __x25_remove_route(rt
);
91 write_unlock_bh(&x25_route_list_lock
);
96 * A device has been removed, remove its routes.
98 void x25_route_device_down(struct net_device
*dev
)
100 struct x25_route
*rt
;
101 struct list_head
*entry
, *tmp
;
103 write_lock_bh(&x25_route_list_lock
);
105 list_for_each_safe(entry
, tmp
, &x25_route_list
) {
106 rt
= list_entry(entry
, struct x25_route
, node
);
109 __x25_remove_route(rt
);
111 write_unlock_bh(&x25_route_list_lock
);
115 * Check that the device given is a valid X.25 interface that is "up".
117 struct net_device
*x25_dev_get(char *devname
)
119 struct net_device
*dev
= dev_get_by_name(&init_net
, devname
);
121 if (dev
&& (!(dev
->flags
& IFF_UP
) || dev
->type
!= ARPHRD_X25
)) {
130 * x25_get_route - Find a route given an X.25 address.
131 * @addr: - address to find a route for
133 * Find a route given an X.25 address.
135 struct x25_route
*x25_get_route(struct x25_address
*addr
)
137 struct x25_route
*rt
, *use
= NULL
;
139 read_lock_bh(&x25_route_list_lock
);
141 list_for_each_entry(rt
, &x25_route_list
, node
) {
142 if (!memcmp(&rt
->address
, addr
, rt
->sigdigits
)) {
145 else if (rt
->sigdigits
> use
->sigdigits
)
153 read_unlock_bh(&x25_route_list_lock
);
158 * Handle the ioctls that control the routing functions.
160 int x25_route_ioctl(unsigned int cmd
, void __user
*arg
)
162 struct x25_route_struct rt
;
163 struct net_device
*dev
;
166 if (cmd
!= SIOCADDRT
&& cmd
!= SIOCDELRT
)
170 if (copy_from_user(&rt
, arg
, sizeof(rt
)))
174 if (rt
.sigdigits
> 15)
177 dev
= x25_dev_get(rt
.device
);
181 if (cmd
== SIOCADDRT
)
182 rc
= x25_add_route(&rt
.address
, rt
.sigdigits
, dev
);
184 rc
= x25_del_route(&rt
.address
, rt
.sigdigits
, dev
);
191 * Release all memory associated with X.25 routing structures.
193 void __exit
x25_route_free(void)
195 struct x25_route
*rt
;
196 struct list_head
*entry
, *tmp
;
198 write_lock_bh(&x25_route_list_lock
);
199 list_for_each_safe(entry
, tmp
, &x25_route_list
) {
200 rt
= list_entry(entry
, struct x25_route
, node
);
201 __x25_remove_route(rt
);
203 write_unlock_bh(&x25_route_list_lock
);