Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Hideaki | f8e1d201 | 2007-02-09 23:25:27 +0900 | [diff] [blame] | 6 | * screw up. It might even work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/if_arp.h> |
| 21 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <net/x25.h> |
Sachin Sharma | 20d658e | 2014-02-18 00:16:23 -0800 | [diff] [blame] | 24 | #ifdef KW_TAINT_ANALYSIS |
| 25 | extern void * get_tainted_stuff(); |
| 26 | #endif |
Denis Cheng | 0e3cf7e | 2007-12-07 00:50:43 -0800 | [diff] [blame] | 27 | LIST_HEAD(x25_route_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | DEFINE_RWLOCK(x25_route_list_lock); |
| 29 | |
| 30 | /* |
| 31 | * Add a new route. |
| 32 | */ |
| 33 | static 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; |
| 64 | out: |
| 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 | */ |
| 76 | static 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 | |
| 84 | static 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 | */ |
| 111 | void 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 Hendry | 95a9dc4 | 2007-02-08 13:34:02 -0800 | [diff] [blame] | 125 | |
| 126 | /* Remove any related forwarding */ |
| 127 | x25_clear_forward_by_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Check that the device given is a valid X.25 interface that is "up". |
| 132 | */ |
| 133 | struct net_device *x25_dev_get(char *devname) |
| 134 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 135 | struct net_device *dev = dev_get_by_name(&init_net, devname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | if (dev && |
| 138 | (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25 |
Igor Maravić | 29c3626 | 2011-12-12 02:58:23 +0000 | [diff] [blame] | 139 | #if IS_ENABLED(CONFIG_LLC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | && dev->type != ARPHRD_ETHER |
| 141 | #endif |
andrew hendry | 429d33a | 2009-11-24 15:16:05 +0000 | [diff] [blame] | 142 | ))){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | dev_put(dev); |
andrew hendry | 429d33a | 2009-11-24 15:16:05 +0000 | [diff] [blame] | 144 | dev = NULL; |
| 145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 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 | */ |
| 156 | struct 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 Sharma | 20d658e | 2014-02-18 00:16:23 -0800 | [diff] [blame] | 184 | int x25_route_ioctl(unsigned int cmd, void __user *arg_actual) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | struct x25_route_struct rt; |
| 187 | struct net_device *dev; |
| 188 | int rc = -EINVAL; |
Sachin Sharma | 20d658e | 2014-02-18 00:16:23 -0800 | [diff] [blame] | 189 | #ifdef KW_TAINT_ANALYSIS |
| 190 | void __user *arg = (void __user *)get_tainted_stuff(); |
| 191 | #else |
| 192 | void __user *arg = arg_actual; |
| 193 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | 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 kluin | 091bb8a | 2009-10-23 05:21:56 +0000 | [diff] [blame] | 202 | if (rt.sigdigits > 15) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | 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); |
| 214 | out: |
| 215 | return rc; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Release all memory associated with X.25 routing structures. |
| 220 | */ |
| 221 | void __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 | } |