^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * X.25 Packet Layer release 002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This is ALPHA test software. This code may break your machine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * randomly fail to work with new releases, misbehave and/or generally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * screw up. It might even work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * This code REQUIRES 2.1.15 or higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * History
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * X.25 001 Jonathan Naylor Started coding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/x25.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) LIST_HEAD(x25_route_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) DEFINE_RWLOCK(x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Add a new route.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct x25_route *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct list_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) write_lock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) list_for_each(entry, &x25_route_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) rt = list_entry(entry, struct x25_route, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!memcmp(&rt->address, address, sigdigits) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) rt->sigdigits == sigdigits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) strcpy(rt->address.x25_addr, "000000000000000");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) rt->sigdigits = sigdigits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) rt->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) refcount_set(&rt->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) list_add(&rt->node, &x25_route_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) write_unlock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * __x25_remove_route - remove route from x25_route_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @rt: route to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * Remove route from x25_route_list. If it was there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * Caller must hold x25_route_list_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void __x25_remove_route(struct x25_route *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (rt->node.next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) list_del(&rt->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) x25_route_put(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct x25_route *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct list_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) write_lock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) list_for_each(entry, &x25_route_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) rt = list_entry(entry, struct x25_route, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!memcmp(&rt->address, address, sigdigits) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) rt->sigdigits == sigdigits && rt->dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) __x25_remove_route(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) write_unlock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * A device has been removed, remove its routes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) void x25_route_device_down(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct x25_route *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct list_head *entry, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) write_lock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) list_for_each_safe(entry, tmp, &x25_route_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) rt = list_entry(entry, struct x25_route, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (rt->dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) __x25_remove_route(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) write_unlock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Remove any related forwarding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) x25_clear_forward_by_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * Check that the device given is a valid X.25 interface that is "up".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct net_device *x25_dev_get(char *devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct net_device *dev = dev_get_by_name(&init_net, devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (dev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #if IS_ENABLED(CONFIG_LLC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) && dev->type != ARPHRD_ETHER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ))){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * x25_get_route - Find a route given an X.25 address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * @addr: - address to find a route for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Find a route given an X.25 address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct x25_route *x25_get_route(struct x25_address *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct x25_route *rt, *use = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct list_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) read_lock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) list_for_each(entry, &x25_route_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) rt = list_entry(entry, struct x25_route, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!memcmp(&rt->address, addr, rt->sigdigits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) use = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) else if (rt->sigdigits > use->sigdigits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) use = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) x25_route_hold(use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) read_unlock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * Handle the ioctls that control the routing functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int x25_route_ioctl(unsigned int cmd, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct x25_route_struct rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (cmd != SIOCADDRT && cmd != SIOCDELRT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (copy_from_user(&rt, arg, sizeof(rt)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (rt.sigdigits > 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) dev = x25_dev_get(rt.device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (cmd == SIOCADDRT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) rc = x25_add_route(&rt.address, rt.sigdigits, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) rc = x25_del_route(&rt.address, rt.sigdigits, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * Release all memory associated with X.25 routing structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) void __exit x25_route_free(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct x25_route *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct list_head *entry, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) write_lock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) list_for_each_safe(entry, tmp, &x25_route_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) rt = list_entry(entry, struct x25_route, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) __x25_remove_route(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) write_unlock_bh(&x25_route_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }