Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * DDP: An implementation of the AppleTalk DDP protocol for |
| 4 | * Ethernet 'ELAP'. |
| 5 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 6 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * With more than a little assistance from |
| 9 | * |
| 10 | * Wesley Craig <netatalk@umich.edu> |
| 11 | * |
| 12 | * Fixes: |
| 13 | * Neil Horman : Added missing device ioctls |
| 14 | * Michael Callahan : Made routing work |
| 15 | * Wesley Craig : Fix probing to listen to a |
| 16 | * passed node id. |
| 17 | * Alan Cox : Added send/recvmsg support |
| 18 | * Alan Cox : Moved at. to protinfo in |
| 19 | * socket. |
| 20 | * Alan Cox : Added firewall hooks. |
| 21 | * Alan Cox : Supports new ARPHRD_LOOPBACK |
| 22 | * Christer Weinigel : Routing and /proc fixes. |
| 23 | * Bradford Johnson : LocalTalk. |
| 24 | * Tom Dyas : Module support. |
| 25 | * Alan Cox : Hooks for PPP (based on the |
| 26 | * LocalTalk hook). |
| 27 | * Alan Cox : Posix bits |
| 28 | * Alan Cox/Mike Freeman : Possible fix to NBP problems |
| 29 | * Bradford Johnson : IP-over-DDP (experimental) |
| 30 | * Jay Schulist : Moved IP-over-DDP to its own |
| 31 | * driver file. (ipddp.c & ipddp.h) |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 32 | * Jay Schulist : Made work as module with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * AppleTalk drivers, cleaned it. |
| 34 | * Rob Newberry : Added proxy AARP and AARP |
| 35 | * procfs, moved probing to AARP |
| 36 | * module. |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 37 | * Adrian Sun/ |
| 38 | * Michael Zuelsdorff : fix for net.0 packets. don't |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | * allow illegal ether/tokentalk |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 40 | * port assignment. we lose a |
| 41 | * valid localtalk port as a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * result. |
| 43 | * Arnaldo C. de Melo : Cleanup, in preparation for |
| 44 | * shared skb support 8) |
| 45 | * Arnaldo C. de Melo : Move proc stuff to atalk_proc.c, |
| 46 | * use seq_file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | */ |
| 48 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 49 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/if_arp.h> |
| 52 | #include <linux/termios.h> /* For TIOCOUTQ/INQ */ |
Stephen Rothwell | 415ce61 | 2009-11-08 20:41:03 -0800 | [diff] [blame] | 53 | #include <linux/compat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <net/datalink.h> |
| 56 | #include <net/psnap.h> |
| 57 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 58 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <net/route.h> |
Christoph Hellwig | dc13c876 | 2020-05-18 08:28:08 +0200 | [diff] [blame] | 60 | #include <net/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/atalk.h> |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 62 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | struct datalink_proto *ddp_dl, *aarp_dl; |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 65 | static const struct proto_ops atalk_dgram_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | /**************************************************************************\ |
| 68 | * * |
| 69 | * Handlers for the socket list. * |
| 70 | * * |
| 71 | \**************************************************************************/ |
| 72 | |
| 73 | HLIST_HEAD(atalk_sockets); |
| 74 | DEFINE_RWLOCK(atalk_sockets_lock); |
| 75 | |
| 76 | static inline void __atalk_insert_socket(struct sock *sk) |
| 77 | { |
| 78 | sk_add_node(sk, &atalk_sockets); |
| 79 | } |
| 80 | |
| 81 | static inline void atalk_remove_socket(struct sock *sk) |
| 82 | { |
| 83 | write_lock_bh(&atalk_sockets_lock); |
| 84 | sk_del_node_init(sk); |
| 85 | write_unlock_bh(&atalk_sockets_lock); |
| 86 | } |
| 87 | |
| 88 | static struct sock *atalk_search_socket(struct sockaddr_at *to, |
| 89 | struct atalk_iface *atif) |
| 90 | { |
| 91 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | read_lock_bh(&atalk_sockets_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 94 | sk_for_each(s, &atalk_sockets) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct atalk_sock *at = at_sk(s); |
| 96 | |
| 97 | if (to->sat_port != at->src_port) |
| 98 | continue; |
| 99 | |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 100 | if (to->sat_addr.s_net == ATADDR_ANYNET && |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 101 | to->sat_addr.s_node == ATADDR_BCAST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | goto found; |
| 103 | |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 104 | if (to->sat_addr.s_net == at->src_net && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | (to->sat_addr.s_node == at->src_node || |
| 106 | to->sat_addr.s_node == ATADDR_BCAST || |
| 107 | to->sat_addr.s_node == ATADDR_ANYNODE)) |
| 108 | goto found; |
| 109 | |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 110 | /* XXXX.0 -- we got a request for this router. make sure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * that the node is appropriately set. */ |
| 112 | if (to->sat_addr.s_node == ATADDR_ANYNODE && |
| 113 | to->sat_addr.s_net != ATADDR_ANYNET && |
| 114 | atif->address.s_node == at->src_node) { |
| 115 | to->sat_addr.s_node = atif->address.s_node; |
| 116 | goto found; |
| 117 | } |
| 118 | } |
| 119 | s = NULL; |
| 120 | found: |
| 121 | read_unlock_bh(&atalk_sockets_lock); |
| 122 | return s; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * atalk_find_or_insert_socket - Try to find a socket matching ADDR |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 127 | * @sk: socket to insert in the list if it is not there already |
| 128 | * @sat: address to search for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * |
| 130 | * Try to find a socket matching ADDR in the socket list, if found then return |
| 131 | * it. If not, insert SK into the socket list. |
| 132 | * |
| 133 | * This entire operation must execute atomically. |
| 134 | */ |
| 135 | static struct sock *atalk_find_or_insert_socket(struct sock *sk, |
| 136 | struct sockaddr_at *sat) |
| 137 | { |
| 138 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | struct atalk_sock *at; |
| 140 | |
| 141 | write_lock_bh(&atalk_sockets_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 142 | sk_for_each(s, &atalk_sockets) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | at = at_sk(s); |
| 144 | |
| 145 | if (at->src_net == sat->sat_addr.s_net && |
| 146 | at->src_node == sat->sat_addr.s_node && |
| 147 | at->src_port == sat->sat_port) |
| 148 | goto found; |
| 149 | } |
| 150 | s = NULL; |
| 151 | __atalk_insert_socket(sk); /* Wheee, it's free, assign and insert. */ |
| 152 | found: |
| 153 | write_unlock_bh(&atalk_sockets_lock); |
| 154 | return s; |
| 155 | } |
| 156 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 157 | static void atalk_destroy_timer(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 159 | struct sock *sk = from_timer(sk, t, sk_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Eric Dumazet | c564039 | 2009-06-16 10:12:03 +0000 | [diff] [blame] | 161 | if (sk_has_allocations(sk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME; |
| 163 | add_timer(&sk->sk_timer); |
| 164 | } else |
| 165 | sock_put(sk); |
| 166 | } |
| 167 | |
| 168 | static inline void atalk_destroy_socket(struct sock *sk) |
| 169 | { |
| 170 | atalk_remove_socket(sk); |
| 171 | skb_queue_purge(&sk->sk_receive_queue); |
| 172 | |
Eric Dumazet | c564039 | 2009-06-16 10:12:03 +0000 | [diff] [blame] | 173 | if (sk_has_allocations(sk)) { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 174 | timer_setup(&sk->sk_timer, atalk_destroy_timer, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | add_timer(&sk->sk_timer); |
| 177 | } else |
| 178 | sock_put(sk); |
| 179 | } |
| 180 | |
| 181 | /**************************************************************************\ |
| 182 | * * |
| 183 | * Routing tables for the AppleTalk socket layer. * |
| 184 | * * |
| 185 | \**************************************************************************/ |
| 186 | |
| 187 | /* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */ |
| 188 | struct atalk_route *atalk_routes; |
| 189 | DEFINE_RWLOCK(atalk_routes_lock); |
| 190 | |
| 191 | struct atalk_iface *atalk_interfaces; |
| 192 | DEFINE_RWLOCK(atalk_interfaces_lock); |
| 193 | |
| 194 | /* For probing devices or in a routerless network */ |
| 195 | struct atalk_route atrtr_default; |
| 196 | |
| 197 | /* AppleTalk interface control */ |
| 198 | /* |
| 199 | * Drop a device. Doesn't drop any of its routes - that is the caller's |
| 200 | * problem. Called when we down the interface or delete the address. |
| 201 | */ |
| 202 | static void atif_drop_device(struct net_device *dev) |
| 203 | { |
| 204 | struct atalk_iface **iface = &atalk_interfaces; |
| 205 | struct atalk_iface *tmp; |
| 206 | |
| 207 | write_lock_bh(&atalk_interfaces_lock); |
| 208 | while ((tmp = *iface) != NULL) { |
| 209 | if (tmp->dev == dev) { |
| 210 | *iface = tmp->next; |
| 211 | dev_put(dev); |
| 212 | kfree(tmp); |
| 213 | dev->atalk_ptr = NULL; |
| 214 | } else |
| 215 | iface = &tmp->next; |
| 216 | } |
| 217 | write_unlock_bh(&atalk_interfaces_lock); |
| 218 | } |
| 219 | |
| 220 | static struct atalk_iface *atif_add_device(struct net_device *dev, |
| 221 | struct atalk_addr *sa) |
| 222 | { |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 223 | struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | if (!iface) |
| 226 | goto out; |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | dev_hold(dev); |
| 229 | iface->dev = dev; |
| 230 | dev->atalk_ptr = iface; |
| 231 | iface->address = *sa; |
| 232 | iface->status = 0; |
| 233 | |
| 234 | write_lock_bh(&atalk_interfaces_lock); |
| 235 | iface->next = atalk_interfaces; |
| 236 | atalk_interfaces = iface; |
| 237 | write_unlock_bh(&atalk_interfaces_lock); |
| 238 | out: |
| 239 | return iface; |
| 240 | } |
| 241 | |
| 242 | /* Perform phase 2 AARP probing on our tentative address */ |
| 243 | static int atif_probe_device(struct atalk_iface *atif) |
| 244 | { |
| 245 | int netrange = ntohs(atif->nets.nr_lastnet) - |
| 246 | ntohs(atif->nets.nr_firstnet) + 1; |
| 247 | int probe_net = ntohs(atif->address.s_net); |
| 248 | int probe_node = atif->address.s_node; |
| 249 | int netct, nodect; |
| 250 | |
| 251 | /* Offset the network we start probing with */ |
| 252 | if (probe_net == ATADDR_ANYNET) { |
| 253 | probe_net = ntohs(atif->nets.nr_firstnet); |
| 254 | if (netrange) |
| 255 | probe_net += jiffies % netrange; |
| 256 | } |
| 257 | if (probe_node == ATADDR_ANYNODE) |
| 258 | probe_node = jiffies & 0xFF; |
| 259 | |
| 260 | /* Scan the networks */ |
| 261 | atif->status |= ATIF_PROBE; |
| 262 | for (netct = 0; netct <= netrange; netct++) { |
| 263 | /* Sweep the available nodes from a given start */ |
| 264 | atif->address.s_net = htons(probe_net); |
| 265 | for (nodect = 0; nodect < 256; nodect++) { |
| 266 | atif->address.s_node = (nodect + probe_node) & 0xFF; |
| 267 | if (atif->address.s_node > 0 && |
| 268 | atif->address.s_node < 254) { |
| 269 | /* Probe a proposed address */ |
| 270 | aarp_probe_network(atif); |
| 271 | |
| 272 | if (!(atif->status & ATIF_PROBE_FAIL)) { |
| 273 | atif->status &= ~ATIF_PROBE; |
| 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | atif->status &= ~ATIF_PROBE_FAIL; |
| 278 | } |
| 279 | probe_net++; |
| 280 | if (probe_net > ntohs(atif->nets.nr_lastnet)) |
| 281 | probe_net = ntohs(atif->nets.nr_firstnet); |
| 282 | } |
| 283 | atif->status &= ~ATIF_PROBE; |
| 284 | |
| 285 | return -EADDRINUSE; /* Network is full... */ |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /* Perform AARP probing for a proxy address */ |
| 290 | static int atif_proxy_probe_device(struct atalk_iface *atif, |
wangweidong | 6f0984a | 2014-02-14 15:43:44 +0800 | [diff] [blame] | 291 | struct atalk_addr *proxy_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
| 293 | int netrange = ntohs(atif->nets.nr_lastnet) - |
| 294 | ntohs(atif->nets.nr_firstnet) + 1; |
| 295 | /* we probe the interface's network */ |
| 296 | int probe_net = ntohs(atif->address.s_net); |
| 297 | int probe_node = ATADDR_ANYNODE; /* we'll take anything */ |
| 298 | int netct, nodect; |
| 299 | |
| 300 | /* Offset the network we start probing with */ |
| 301 | if (probe_net == ATADDR_ANYNET) { |
| 302 | probe_net = ntohs(atif->nets.nr_firstnet); |
| 303 | if (netrange) |
| 304 | probe_net += jiffies % netrange; |
| 305 | } |
| 306 | |
| 307 | if (probe_node == ATADDR_ANYNODE) |
| 308 | probe_node = jiffies & 0xFF; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /* Scan the networks */ |
| 311 | for (netct = 0; netct <= netrange; netct++) { |
| 312 | /* Sweep the available nodes from a given start */ |
| 313 | proxy_addr->s_net = htons(probe_net); |
| 314 | for (nodect = 0; nodect < 256; nodect++) { |
| 315 | proxy_addr->s_node = (nodect + probe_node) & 0xFF; |
| 316 | if (proxy_addr->s_node > 0 && |
| 317 | proxy_addr->s_node < 254) { |
| 318 | /* Tell AARP to probe a proposed address */ |
| 319 | int ret = aarp_proxy_probe_network(atif, |
| 320 | proxy_addr); |
| 321 | |
| 322 | if (ret != -EADDRINUSE) |
| 323 | return ret; |
| 324 | } |
| 325 | } |
| 326 | probe_net++; |
| 327 | if (probe_net > ntohs(atif->nets.nr_lastnet)) |
| 328 | probe_net = ntohs(atif->nets.nr_firstnet); |
| 329 | } |
| 330 | |
| 331 | return -EADDRINUSE; /* Network is full... */ |
| 332 | } |
| 333 | |
| 334 | |
| 335 | struct atalk_addr *atalk_find_dev_addr(struct net_device *dev) |
| 336 | { |
| 337 | struct atalk_iface *iface = dev->atalk_ptr; |
| 338 | return iface ? &iface->address : NULL; |
| 339 | } |
| 340 | |
| 341 | static struct atalk_addr *atalk_find_primary(void) |
| 342 | { |
| 343 | struct atalk_iface *fiface = NULL; |
| 344 | struct atalk_addr *retval; |
| 345 | struct atalk_iface *iface; |
| 346 | |
| 347 | /* |
| 348 | * Return a point-to-point interface only if |
| 349 | * there is no non-ptp interface available. |
| 350 | */ |
| 351 | read_lock_bh(&atalk_interfaces_lock); |
| 352 | for (iface = atalk_interfaces; iface; iface = iface->next) { |
| 353 | if (!fiface && !(iface->dev->flags & IFF_LOOPBACK)) |
| 354 | fiface = iface; |
| 355 | if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { |
| 356 | retval = &iface->address; |
| 357 | goto out; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (fiface) |
| 362 | retval = &fiface->address; |
| 363 | else if (atalk_interfaces) |
| 364 | retval = &atalk_interfaces->address; |
| 365 | else |
| 366 | retval = NULL; |
| 367 | out: |
| 368 | read_unlock_bh(&atalk_interfaces_lock); |
| 369 | return retval; |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Find a match for 'any network' - ie any of our interfaces with that |
| 374 | * node number will do just nicely. |
| 375 | */ |
| 376 | static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev) |
| 377 | { |
| 378 | struct atalk_iface *iface = dev->atalk_ptr; |
| 379 | |
| 380 | if (!iface || iface->status & ATIF_PROBE) |
| 381 | goto out_err; |
| 382 | |
| 383 | if (node != ATADDR_BCAST && |
| 384 | iface->address.s_node != node && |
| 385 | node != ATADDR_ANYNODE) |
| 386 | goto out_err; |
| 387 | out: |
| 388 | return iface; |
| 389 | out_err: |
| 390 | iface = NULL; |
| 391 | goto out; |
| 392 | } |
| 393 | |
| 394 | /* Find a match for a specific network:node pair */ |
Alexey Dobriyan | f6e276ee | 2005-06-20 13:32:05 -0700 | [diff] [blame] | 395 | static struct atalk_iface *atalk_find_interface(__be16 net, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
| 397 | struct atalk_iface *iface; |
| 398 | |
| 399 | read_lock_bh(&atalk_interfaces_lock); |
| 400 | for (iface = atalk_interfaces; iface; iface = iface->next) { |
| 401 | if ((node == ATADDR_BCAST || |
| 402 | node == ATADDR_ANYNODE || |
| 403 | iface->address.s_node == node) && |
| 404 | iface->address.s_net == net && |
| 405 | !(iface->status & ATIF_PROBE)) |
| 406 | break; |
| 407 | |
| 408 | /* XXXX.0 -- net.0 returns the iface associated with net */ |
| 409 | if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET && |
| 410 | ntohs(iface->nets.nr_firstnet) <= ntohs(net) && |
| 411 | ntohs(net) <= ntohs(iface->nets.nr_lastnet)) |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 412 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | read_unlock_bh(&atalk_interfaces_lock); |
| 415 | return iface; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /* |
| 420 | * Find a route for an AppleTalk packet. This ought to get cached in |
| 421 | * the socket (later on...). We know about host routes and the fact |
| 422 | * that a route must be direct to broadcast. |
| 423 | */ |
| 424 | static struct atalk_route *atrtr_find(struct atalk_addr *target) |
| 425 | { |
| 426 | /* |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 427 | * we must search through all routes unless we find a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * host route, because some host routes might overlap |
| 429 | * network routes |
| 430 | */ |
| 431 | struct atalk_route *net_route = NULL; |
| 432 | struct atalk_route *r; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | read_lock_bh(&atalk_routes_lock); |
| 435 | for (r = atalk_routes; r; r = r->next) { |
| 436 | if (!(r->flags & RTF_UP)) |
| 437 | continue; |
| 438 | |
| 439 | if (r->target.s_net == target->s_net) { |
| 440 | if (r->flags & RTF_HOST) { |
| 441 | /* |
| 442 | * if this host route is for the target, |
| 443 | * the we're done |
| 444 | */ |
| 445 | if (r->target.s_node == target->s_node) |
| 446 | goto out; |
| 447 | } else |
| 448 | /* |
| 449 | * this route will work if there isn't a |
| 450 | * direct host route, so cache it |
| 451 | */ |
| 452 | net_route = r; |
| 453 | } |
| 454 | } |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 455 | |
| 456 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | * if we found a network route but not a direct host |
| 458 | * route, then return it |
| 459 | */ |
| 460 | if (net_route) |
| 461 | r = net_route; |
| 462 | else if (atrtr_default.dev) |
| 463 | r = &atrtr_default; |
| 464 | else /* No route can be found */ |
| 465 | r = NULL; |
| 466 | out: |
| 467 | read_unlock_bh(&atalk_routes_lock); |
| 468 | return r; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /* |
| 473 | * Given an AppleTalk network, find the device to use. This can be |
| 474 | * a simple lookup. |
| 475 | */ |
| 476 | struct net_device *atrtr_get_dev(struct atalk_addr *sa) |
| 477 | { |
| 478 | struct atalk_route *atr = atrtr_find(sa); |
| 479 | return atr ? atr->dev : NULL; |
| 480 | } |
| 481 | |
| 482 | /* Set up a default router */ |
| 483 | static void atrtr_set_default(struct net_device *dev) |
| 484 | { |
| 485 | atrtr_default.dev = dev; |
| 486 | atrtr_default.flags = RTF_UP; |
| 487 | atrtr_default.gateway.s_net = htons(0); |
| 488 | atrtr_default.gateway.s_node = 0; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Add a router. Basically make sure it looks valid and stuff the |
| 493 | * entry in the list. While it uses netranges we always set them to one |
| 494 | * entry to work like netatalk. |
| 495 | */ |
| 496 | static int atrtr_create(struct rtentry *r, struct net_device *devhint) |
| 497 | { |
| 498 | struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst; |
| 499 | struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway; |
| 500 | struct atalk_route *rt; |
| 501 | struct atalk_iface *iface, *riface; |
| 502 | int retval = -EINVAL; |
| 503 | |
| 504 | /* |
| 505 | * Fixme: Raise/Lower a routing change semaphore for these |
| 506 | * operations. |
| 507 | */ |
| 508 | |
| 509 | /* Validate the request */ |
| 510 | if (ta->sat_family != AF_APPLETALK || |
| 511 | (!devhint && ga->sat_family != AF_APPLETALK)) |
| 512 | goto out; |
| 513 | |
| 514 | /* Now walk the routing table and make our decisions */ |
| 515 | write_lock_bh(&atalk_routes_lock); |
| 516 | for (rt = atalk_routes; rt; rt = rt->next) { |
| 517 | if (r->rt_flags != rt->flags) |
| 518 | continue; |
| 519 | |
| 520 | if (ta->sat_addr.s_net == rt->target.s_net) { |
| 521 | if (!(rt->flags & RTF_HOST)) |
| 522 | break; |
| 523 | if (ta->sat_addr.s_node == rt->target.s_node) |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (!devhint) { |
| 529 | riface = NULL; |
| 530 | |
| 531 | read_lock_bh(&atalk_interfaces_lock); |
| 532 | for (iface = atalk_interfaces; iface; iface = iface->next) { |
| 533 | if (!riface && |
| 534 | ntohs(ga->sat_addr.s_net) >= |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 535 | ntohs(iface->nets.nr_firstnet) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | ntohs(ga->sat_addr.s_net) <= |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 537 | ntohs(iface->nets.nr_lastnet)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | riface = iface; |
| 539 | |
| 540 | if (ga->sat_addr.s_net == iface->address.s_net && |
| 541 | ga->sat_addr.s_node == iface->address.s_node) |
| 542 | riface = iface; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | read_unlock_bh(&atalk_interfaces_lock); |
| 545 | |
| 546 | retval = -ENETUNREACH; |
| 547 | if (!riface) |
| 548 | goto out_unlock; |
| 549 | |
| 550 | devhint = riface->dev; |
| 551 | } |
| 552 | |
| 553 | if (!rt) { |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 554 | rt = kzalloc(sizeof(*rt), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | retval = -ENOBUFS; |
| 557 | if (!rt) |
| 558 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | rt->next = atalk_routes; |
| 561 | atalk_routes = rt; |
| 562 | } |
| 563 | |
| 564 | /* Fill in the routing entry */ |
| 565 | rt->target = ta->sat_addr; |
Herbert Xu | c7f905f0 | 2005-04-19 22:44:17 -0700 | [diff] [blame] | 566 | dev_hold(devhint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | rt->dev = devhint; |
| 568 | rt->flags = r->rt_flags; |
| 569 | rt->gateway = ga->sat_addr; |
| 570 | |
| 571 | retval = 0; |
| 572 | out_unlock: |
| 573 | write_unlock_bh(&atalk_routes_lock); |
| 574 | out: |
| 575 | return retval; |
| 576 | } |
| 577 | |
| 578 | /* Delete a route. Find it and discard it */ |
wangweidong | 6f0984a | 2014-02-14 15:43:44 +0800 | [diff] [blame] | 579 | static int atrtr_delete(struct atalk_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
| 581 | struct atalk_route **r = &atalk_routes; |
| 582 | int retval = 0; |
| 583 | struct atalk_route *tmp; |
| 584 | |
| 585 | write_lock_bh(&atalk_routes_lock); |
| 586 | while ((tmp = *r) != NULL) { |
| 587 | if (tmp->target.s_net == addr->s_net && |
| 588 | (!(tmp->flags&RTF_GATEWAY) || |
| 589 | tmp->target.s_node == addr->s_node)) { |
| 590 | *r = tmp->next; |
| 591 | dev_put(tmp->dev); |
| 592 | kfree(tmp); |
| 593 | goto out; |
| 594 | } |
| 595 | r = &tmp->next; |
| 596 | } |
| 597 | retval = -ENOENT; |
| 598 | out: |
| 599 | write_unlock_bh(&atalk_routes_lock); |
| 600 | return retval; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Called when a device is downed. Just throw away any routes |
| 605 | * via it. |
| 606 | */ |
| 607 | static void atrtr_device_down(struct net_device *dev) |
| 608 | { |
| 609 | struct atalk_route **r = &atalk_routes; |
| 610 | struct atalk_route *tmp; |
| 611 | |
| 612 | write_lock_bh(&atalk_routes_lock); |
| 613 | while ((tmp = *r) != NULL) { |
| 614 | if (tmp->dev == dev) { |
| 615 | *r = tmp->next; |
| 616 | dev_put(dev); |
| 617 | kfree(tmp); |
| 618 | } else |
| 619 | r = &tmp->next; |
| 620 | } |
| 621 | write_unlock_bh(&atalk_routes_lock); |
| 622 | |
| 623 | if (atrtr_default.dev == dev) |
| 624 | atrtr_set_default(NULL); |
| 625 | } |
| 626 | |
| 627 | /* Actually down the interface */ |
| 628 | static inline void atalk_dev_down(struct net_device *dev) |
| 629 | { |
| 630 | atrtr_device_down(dev); /* Remove all routes for the device */ |
| 631 | aarp_device_down(dev); /* Remove AARP entries for the device */ |
| 632 | atif_drop_device(dev); /* Remove the device */ |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * A device event has occurred. Watch for devices going down and |
| 637 | * delete our use of them (iface and route). |
| 638 | */ |
| 639 | static int ddp_device_event(struct notifier_block *this, unsigned long event, |
| 640 | void *ptr) |
| 641 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 642 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | 890d52d | 2007-09-12 11:26:59 +0200 | [diff] [blame] | 643 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 644 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 645 | return NOTIFY_DONE; |
| 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | if (event == NETDEV_DOWN) |
| 648 | /* Discard any use of this */ |
Eric W. Biederman | 890d52d | 2007-09-12 11:26:59 +0200 | [diff] [blame] | 649 | atalk_dev_down(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
| 651 | return NOTIFY_DONE; |
| 652 | } |
| 653 | |
| 654 | /* ioctl calls. Shouldn't even need touching */ |
| 655 | /* Device configuration ioctl calls */ |
| 656 | static int atif_ioctl(int cmd, void __user *arg) |
| 657 | { |
| 658 | static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF }; |
| 659 | struct ifreq atreq; |
| 660 | struct atalk_netrange *nr; |
| 661 | struct sockaddr_at *sa; |
| 662 | struct net_device *dev; |
| 663 | struct atalk_iface *atif; |
| 664 | int ct; |
| 665 | int limit; |
| 666 | struct rtentry rtdef; |
| 667 | int add_route; |
| 668 | |
Arnd Bergmann | 29c4964 | 2021-07-22 16:29:03 +0200 | [diff] [blame] | 669 | if (get_user_ifreq(&atreq, NULL, arg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | return -EFAULT; |
| 671 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 672 | dev = __dev_get_by_name(&init_net, atreq.ifr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (!dev) |
| 674 | return -ENODEV; |
| 675 | |
| 676 | sa = (struct sockaddr_at *)&atreq.ifr_addr; |
| 677 | atif = atalk_find_dev(dev); |
| 678 | |
| 679 | switch (cmd) { |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 680 | case SIOCSIFADDR: |
| 681 | if (!capable(CAP_NET_ADMIN)) |
| 682 | return -EPERM; |
| 683 | if (sa->sat_family != AF_APPLETALK) |
| 684 | return -EINVAL; |
| 685 | if (dev->type != ARPHRD_ETHER && |
| 686 | dev->type != ARPHRD_LOOPBACK && |
| 687 | dev->type != ARPHRD_LOCALTLK && |
| 688 | dev->type != ARPHRD_PPP) |
| 689 | return -EPROTONOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 691 | nr = (struct atalk_netrange *)&sa->sat_zero[0]; |
| 692 | add_route = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 694 | /* |
| 695 | * if this is a point-to-point iface, and we already |
| 696 | * have an iface for this AppleTalk address, then we |
| 697 | * should not add a route |
| 698 | */ |
| 699 | if ((dev->flags & IFF_POINTOPOINT) && |
| 700 | atalk_find_interface(sa->sat_addr.s_net, |
| 701 | sa->sat_addr.s_node)) { |
| 702 | printk(KERN_DEBUG "AppleTalk: point-to-point " |
| 703 | "interface added with " |
| 704 | "existing address\n"); |
| 705 | add_route = 0; |
| 706 | } |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 707 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 708 | /* |
| 709 | * Phase 1 is fine on LocalTalk but we don't do |
gushengxian | 2aa8eca | 2021-06-08 18:52:57 -0700 | [diff] [blame] | 710 | * EtherTalk phase 1. Anyone wanting to add it, go ahead. |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 711 | */ |
| 712 | if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2) |
| 713 | return -EPROTONOSUPPORT; |
| 714 | if (sa->sat_addr.s_node == ATADDR_BCAST || |
| 715 | sa->sat_addr.s_node == 254) |
| 716 | return -EINVAL; |
| 717 | if (atif) { |
| 718 | /* Already setting address */ |
| 719 | if (atif->status & ATIF_PROBE) |
| 720 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 722 | atif->address.s_net = sa->sat_addr.s_net; |
| 723 | atif->address.s_node = sa->sat_addr.s_node; |
| 724 | atrtr_device_down(dev); /* Flush old routes */ |
| 725 | } else { |
| 726 | atif = atif_add_device(dev, &sa->sat_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (!atif) |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 728 | return -ENOMEM; |
| 729 | } |
| 730 | atif->nets = *nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 732 | /* |
| 733 | * Check if the chosen address is used. If so we |
| 734 | * error and atalkd will try another. |
| 735 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 737 | if (!(dev->flags & IFF_LOOPBACK) && |
| 738 | !(dev->flags & IFF_POINTOPOINT) && |
| 739 | atif_probe_device(atif) < 0) { |
| 740 | atif_drop_device(dev); |
| 741 | return -EADDRINUSE; |
| 742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 744 | /* Hey it worked - add the direct routes */ |
| 745 | sa = (struct sockaddr_at *)&rtdef.rt_gateway; |
| 746 | sa->sat_family = AF_APPLETALK; |
| 747 | sa->sat_addr.s_net = atif->address.s_net; |
| 748 | sa->sat_addr.s_node = atif->address.s_node; |
| 749 | sa = (struct sockaddr_at *)&rtdef.rt_dst; |
| 750 | rtdef.rt_flags = RTF_UP; |
| 751 | sa->sat_family = AF_APPLETALK; |
| 752 | sa->sat_addr.s_node = ATADDR_ANYNODE; |
| 753 | if (dev->flags & IFF_LOOPBACK || |
| 754 | dev->flags & IFF_POINTOPOINT) |
| 755 | rtdef.rt_flags |= RTF_HOST; |
| 756 | |
| 757 | /* Routerless initial state */ |
| 758 | if (nr->nr_firstnet == htons(0) && |
| 759 | nr->nr_lastnet == htons(0xFFFE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | sa->sat_addr.s_net = atif->address.s_net; |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 761 | atrtr_create(&rtdef, dev); |
| 762 | atrtr_set_default(dev); |
| 763 | } else { |
| 764 | limit = ntohs(nr->nr_lastnet); |
| 765 | if (limit - ntohs(nr->nr_firstnet) > 4096) { |
| 766 | printk(KERN_WARNING "Too many routes/" |
| 767 | "iface.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | return -EINVAL; |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 769 | } |
| 770 | if (add_route) |
| 771 | for (ct = ntohs(nr->nr_firstnet); |
| 772 | ct <= limit; ct++) { |
| 773 | sa->sat_addr.s_net = htons(ct); |
| 774 | atrtr_create(&rtdef, dev); |
| 775 | } |
| 776 | } |
| 777 | dev_mc_add_global(dev, aarp_mcast); |
| 778 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 780 | case SIOCGIFADDR: |
| 781 | if (!atif) |
| 782 | return -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 784 | sa->sat_family = AF_APPLETALK; |
| 785 | sa->sat_addr = atif->address; |
| 786 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 788 | case SIOCGIFBRDADDR: |
| 789 | if (!atif) |
| 790 | return -EADDRNOTAVAIL; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 791 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 792 | sa->sat_family = AF_APPLETALK; |
| 793 | sa->sat_addr.s_net = atif->address.s_net; |
| 794 | sa->sat_addr.s_node = ATADDR_BCAST; |
| 795 | break; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 796 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 797 | case SIOCATALKDIFADDR: |
| 798 | case SIOCDIFADDR: |
| 799 | if (!capable(CAP_NET_ADMIN)) |
| 800 | return -EPERM; |
| 801 | if (sa->sat_family != AF_APPLETALK) |
| 802 | return -EINVAL; |
| 803 | atalk_dev_down(dev); |
| 804 | break; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 805 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 806 | case SIOCSARP: |
| 807 | if (!capable(CAP_NET_ADMIN)) |
| 808 | return -EPERM; |
| 809 | if (sa->sat_family != AF_APPLETALK) |
| 810 | return -EINVAL; |
| 811 | /* |
| 812 | * for now, we only support proxy AARP on ELAP; |
| 813 | * we should be able to do it for LocalTalk, too. |
| 814 | */ |
| 815 | if (dev->type != ARPHRD_ETHER) |
| 816 | return -EPROTONOSUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 818 | /* |
| 819 | * atif points to the current interface on this network; |
| 820 | * we aren't concerned about its current status (at |
| 821 | * least for now), but it has all the settings about |
| 822 | * the network we're going to probe. Consequently, it |
| 823 | * must exist. |
| 824 | */ |
| 825 | if (!atif) |
| 826 | return -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 828 | nr = (struct atalk_netrange *)&(atif->nets); |
| 829 | /* |
| 830 | * Phase 1 is fine on Localtalk but we don't do |
gushengxian | 2aa8eca | 2021-06-08 18:52:57 -0700 | [diff] [blame] | 831 | * Ethertalk phase 1. Anyone wanting to add it, go ahead. |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 832 | */ |
| 833 | if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2) |
| 834 | return -EPROTONOSUPPORT; |
| 835 | |
| 836 | if (sa->sat_addr.s_node == ATADDR_BCAST || |
| 837 | sa->sat_addr.s_node == 254) |
| 838 | return -EINVAL; |
| 839 | |
| 840 | /* |
| 841 | * Check if the chosen address is used. If so we |
| 842 | * error and ATCP will try another. |
| 843 | */ |
| 844 | if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0) |
| 845 | return -EADDRINUSE; |
| 846 | |
| 847 | /* |
| 848 | * We now have an address on the local network, and |
| 849 | * the AARP code will defend it for us until we take it |
| 850 | * down. We don't set up any routes right now, because |
| 851 | * ATCP will install them manually via SIOCADDRT. |
| 852 | */ |
| 853 | break; |
| 854 | |
| 855 | case SIOCDARP: |
| 856 | if (!capable(CAP_NET_ADMIN)) |
| 857 | return -EPERM; |
| 858 | if (sa->sat_family != AF_APPLETALK) |
| 859 | return -EINVAL; |
| 860 | if (!atif) |
| 861 | return -EADDRNOTAVAIL; |
| 862 | |
| 863 | /* give to aarp module to remove proxy entry */ |
| 864 | aarp_proxy_remove(atif->dev, &(sa->sat_addr)); |
| 865 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Arnd Bergmann | 29c4964 | 2021-07-22 16:29:03 +0200 | [diff] [blame] | 868 | return put_user_ifreq(&atreq, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Christoph Hellwig | a500492 | 2020-05-18 08:28:07 +0200 | [diff] [blame] | 871 | static int atrtr_ioctl_addrt(struct rtentry *rt) |
| 872 | { |
| 873 | struct net_device *dev = NULL; |
| 874 | |
| 875 | if (rt->rt_dev) { |
| 876 | char name[IFNAMSIZ]; |
| 877 | |
| 878 | if (copy_from_user(name, rt->rt_dev, IFNAMSIZ-1)) |
| 879 | return -EFAULT; |
| 880 | name[IFNAMSIZ-1] = '\0'; |
| 881 | |
| 882 | dev = __dev_get_by_name(&init_net, name); |
| 883 | if (!dev) |
| 884 | return -ENODEV; |
| 885 | } |
| 886 | return atrtr_create(rt, dev); |
| 887 | } |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | /* Routing ioctl() calls */ |
| 890 | static int atrtr_ioctl(unsigned int cmd, void __user *arg) |
| 891 | { |
| 892 | struct rtentry rt; |
| 893 | |
| 894 | if (copy_from_user(&rt, arg, sizeof(rt))) |
| 895 | return -EFAULT; |
| 896 | |
| 897 | switch (cmd) { |
Joe Perches | 4a9e4b0 | 2011-07-01 09:43:02 +0000 | [diff] [blame] | 898 | case SIOCDELRT: |
| 899 | if (rt.rt_dst.sa_family != AF_APPLETALK) |
| 900 | return -EINVAL; |
| 901 | return atrtr_delete(&((struct sockaddr_at *) |
| 902 | &rt.rt_dst)->sat_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Christoph Hellwig | a500492 | 2020-05-18 08:28:07 +0200 | [diff] [blame] | 904 | case SIOCADDRT: |
| 905 | return atrtr_ioctl_addrt(&rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | return -EINVAL; |
| 908 | } |
| 909 | |
| 910 | /**************************************************************************\ |
| 911 | * * |
| 912 | * Handling for system calls applied via the various interfaces to an * |
| 913 | * AppleTalk socket object. * |
| 914 | * * |
| 915 | \**************************************************************************/ |
| 916 | |
| 917 | /* |
| 918 | * Checksum: This is 'optional'. It's quite likely also a good |
| 919 | * candidate for assembler hackery 8) |
| 920 | */ |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 921 | static unsigned long atalk_sum_partial(const unsigned char *data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | int len, unsigned long sum) |
| 923 | { |
| 924 | /* This ought to be unwrapped neatly. I'll trust gcc for now */ |
| 925 | while (len--) { |
Joe Perches | f7a3a1d | 2009-11-04 10:26:13 +0000 | [diff] [blame] | 926 | sum += *data++; |
| 927 | sum = rol16(sum, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
| 929 | return sum; |
| 930 | } |
| 931 | |
| 932 | /* Checksum skb data -- similar to skb_checksum */ |
| 933 | static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset, |
| 934 | int len, unsigned long sum) |
| 935 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 936 | int start = skb_headlen(skb); |
David S. Miller | c32ba3f | 2009-06-09 00:17:44 -0700 | [diff] [blame] | 937 | struct sk_buff *frag_iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | int i, copy; |
| 939 | |
| 940 | /* checksum stuff in header space */ |
wangweidong | fd1dc26 | 2014-02-14 15:43:43 +0800 | [diff] [blame] | 941 | if ((copy = start - offset) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | if (copy > len) |
| 943 | copy = len; |
| 944 | sum = atalk_sum_partial(skb->data + offset, copy, sum); |
wangweidong | fd1dc26 | 2014-02-14 15:43:43 +0800 | [diff] [blame] | 945 | if ((len -= copy) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return sum; |
| 947 | |
| 948 | offset += copy; |
| 949 | } |
| 950 | |
| 951 | /* checksum stuff in frags */ |
| 952 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 953 | int end; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 954 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 955 | WARN_ON(start > offset + len); |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 956 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 957 | end = start + skb_frag_size(frag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if ((copy = end - offset) > 0) { |
| 959 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
| 961 | if (copy > len) |
| 962 | copy = len; |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 963 | vaddr = kmap_atomic(skb_frag_page(frag)); |
Jonathan Lemon | b54c9d5 | 2019-07-30 07:40:33 -0700 | [diff] [blame] | 964 | sum = atalk_sum_partial(vaddr + skb_frag_off(frag) + |
| 965 | offset - start, copy, sum); |
Eric Dumazet | 51c56b0 | 2012-04-05 11:35:15 +0200 | [diff] [blame] | 966 | kunmap_atomic(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | if (!(len -= copy)) |
| 969 | return sum; |
| 970 | offset += copy; |
| 971 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 972 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
| 974 | |
David S. Miller | c32ba3f | 2009-06-09 00:17:44 -0700 | [diff] [blame] | 975 | skb_walk_frags(skb, frag_iter) { |
| 976 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
David S. Miller | c32ba3f | 2009-06-09 00:17:44 -0700 | [diff] [blame] | 978 | WARN_ON(start > offset + len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
David S. Miller | c32ba3f | 2009-06-09 00:17:44 -0700 | [diff] [blame] | 980 | end = start + frag_iter->len; |
| 981 | if ((copy = end - offset) > 0) { |
| 982 | if (copy > len) |
| 983 | copy = len; |
| 984 | sum = atalk_sum_skb(frag_iter, offset - start, |
| 985 | copy, sum); |
| 986 | if ((len -= copy) == 0) |
| 987 | return sum; |
| 988 | offset += copy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
David S. Miller | c32ba3f | 2009-06-09 00:17:44 -0700 | [diff] [blame] | 990 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | BUG_ON(len > 0); |
| 994 | |
| 995 | return sum; |
| 996 | } |
| 997 | |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 998 | static __be16 atalk_checksum(const struct sk_buff *skb, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { |
| 1000 | unsigned long sum; |
| 1001 | |
| 1002 | /* skip header 4 bytes */ |
| 1003 | sum = atalk_sum_skb(skb, 4, len-4, 0); |
| 1004 | |
| 1005 | /* Use 0xFFFF for 0. 0 itself means none */ |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1006 | return sum ? htons((unsigned short)sum) : htons(0xFFFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | static struct proto ddp_proto = { |
| 1010 | .name = "DDP", |
| 1011 | .owner = THIS_MODULE, |
| 1012 | .obj_size = sizeof(struct atalk_sock), |
| 1013 | }; |
| 1014 | |
| 1015 | /* |
| 1016 | * Create a socket. Initialise the socket, blank the addresses |
| 1017 | * set the state. |
| 1018 | */ |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1019 | static int atalk_create(struct net *net, struct socket *sock, int protocol, |
| 1020 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | { |
| 1022 | struct sock *sk; |
| 1023 | int rc = -ESOCKTNOSUPPORT; |
| 1024 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 1025 | if (!net_eq(net, &init_net)) |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 1026 | return -EAFNOSUPPORT; |
| 1027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | /* |
| 1029 | * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1030 | * and gives you the full ELAP frame. Should be handy for CAP 8) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | */ |
| 1032 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) |
| 1033 | goto out; |
Ori Nimron | 6cc03e8 | 2019-09-20 09:35:46 +0200 | [diff] [blame] | 1034 | |
| 1035 | rc = -EPERM; |
| 1036 | if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) |
| 1037 | goto out; |
| 1038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | rc = -ENOMEM; |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 1040 | sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | if (!sk) |
| 1042 | goto out; |
| 1043 | rc = 0; |
| 1044 | sock->ops = &atalk_dgram_ops; |
| 1045 | sock_init_data(sock, sk); |
| 1046 | |
| 1047 | /* Checksums on by default */ |
| 1048 | sock_set_flag(sk, SOCK_ZAPPED); |
| 1049 | out: |
| 1050 | return rc; |
| 1051 | } |
| 1052 | |
| 1053 | /* Free a socket. No work needed */ |
| 1054 | static int atalk_release(struct socket *sock) |
| 1055 | { |
| 1056 | struct sock *sk = sock->sk; |
| 1057 | |
| 1058 | if (sk) { |
David S. Miller | c100c8f | 2011-03-31 18:59:10 -0700 | [diff] [blame] | 1059 | sock_hold(sk); |
| 1060 | lock_sock(sk); |
| 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | sock_orphan(sk); |
| 1063 | sock->sk = NULL; |
| 1064 | atalk_destroy_socket(sk); |
Arnd Bergmann | b20e7bb | 2011-03-21 18:18:00 -0700 | [diff] [blame] | 1065 | |
David S. Miller | c100c8f | 2011-03-31 18:59:10 -0700 | [diff] [blame] | 1066 | release_sock(sk); |
| 1067 | sock_put(sk); |
| 1068 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * atalk_pick_and_bind_port - Pick a source port when one is not given |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1074 | * @sk: socket to insert into the tables |
| 1075 | * @sat: address to search for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | * |
| 1077 | * Pick a source port when one is not given. If we can find a suitable free |
| 1078 | * one, we insert the socket into the tables using it. |
| 1079 | * |
| 1080 | * This whole operation must be atomic. |
| 1081 | */ |
| 1082 | static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat) |
| 1083 | { |
| 1084 | int retval; |
| 1085 | |
| 1086 | write_lock_bh(&atalk_sockets_lock); |
| 1087 | |
| 1088 | for (sat->sat_port = ATPORT_RESERVED; |
| 1089 | sat->sat_port < ATPORT_LAST; |
| 1090 | sat->sat_port++) { |
| 1091 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1093 | sk_for_each(s, &atalk_sockets) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | struct atalk_sock *at = at_sk(s); |
| 1095 | |
| 1096 | if (at->src_net == sat->sat_addr.s_net && |
| 1097 | at->src_node == sat->sat_addr.s_node && |
| 1098 | at->src_port == sat->sat_port) |
| 1099 | goto try_next_port; |
| 1100 | } |
| 1101 | |
| 1102 | /* Wheee, it's free, assign and insert. */ |
| 1103 | __atalk_insert_socket(sk); |
| 1104 | at_sk(sk)->src_port = sat->sat_port; |
| 1105 | retval = 0; |
| 1106 | goto out; |
| 1107 | |
| 1108 | try_next_port:; |
| 1109 | } |
| 1110 | |
| 1111 | retval = -EBUSY; |
| 1112 | out: |
| 1113 | write_unlock_bh(&atalk_sockets_lock); |
| 1114 | return retval; |
| 1115 | } |
| 1116 | |
| 1117 | static int atalk_autobind(struct sock *sk) |
| 1118 | { |
| 1119 | struct atalk_sock *at = at_sk(sk); |
| 1120 | struct sockaddr_at sat; |
| 1121 | struct atalk_addr *ap = atalk_find_primary(); |
| 1122 | int n = -EADDRNOTAVAIL; |
| 1123 | |
| 1124 | if (!ap || ap->s_net == htons(ATADDR_ANYNET)) |
| 1125 | goto out; |
| 1126 | |
| 1127 | at->src_net = sat.sat_addr.s_net = ap->s_net; |
| 1128 | at->src_node = sat.sat_addr.s_node = ap->s_node; |
| 1129 | |
| 1130 | n = atalk_pick_and_bind_port(sk, &sat); |
| 1131 | if (!n) |
| 1132 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 1133 | out: |
| 1134 | return n; |
| 1135 | } |
| 1136 | |
| 1137 | /* Set the address 'our end' of the connection */ |
| 1138 | static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 1139 | { |
| 1140 | struct sockaddr_at *addr = (struct sockaddr_at *)uaddr; |
| 1141 | struct sock *sk = sock->sk; |
| 1142 | struct atalk_sock *at = at_sk(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1143 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | if (!sock_flag(sk, SOCK_ZAPPED) || |
| 1146 | addr_len != sizeof(struct sockaddr_at)) |
| 1147 | return -EINVAL; |
| 1148 | |
| 1149 | if (addr->sat_family != AF_APPLETALK) |
| 1150 | return -EAFNOSUPPORT; |
| 1151 | |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1152 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) { |
| 1154 | struct atalk_addr *ap = atalk_find_primary(); |
| 1155 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1156 | err = -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | if (!ap) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1158 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
| 1160 | at->src_net = addr->sat_addr.s_net = ap->s_net; |
wangweidong | fd1dc26 | 2014-02-14 15:43:43 +0800 | [diff] [blame] | 1161 | at->src_node = addr->sat_addr.s_node = ap->s_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } else { |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1163 | err = -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | if (!atalk_find_interface(addr->sat_addr.s_net, |
| 1165 | addr->sat_addr.s_node)) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1166 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
| 1168 | at->src_net = addr->sat_addr.s_net; |
| 1169 | at->src_node = addr->sat_addr.s_node; |
| 1170 | } |
| 1171 | |
| 1172 | if (addr->sat_port == ATADDR_ANYPORT) { |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1173 | err = atalk_pick_and_bind_port(sk, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1175 | if (err < 0) |
| 1176 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } else { |
| 1178 | at->src_port = addr->sat_port; |
| 1179 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1180 | err = -EADDRINUSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | if (atalk_find_or_insert_socket(sk, addr)) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1182 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | sock_reset_flag(sk, SOCK_ZAPPED); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1186 | err = 0; |
| 1187 | out: |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1188 | release_sock(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1189 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /* Set the address we talk to */ |
| 1193 | static int atalk_connect(struct socket *sock, struct sockaddr *uaddr, |
| 1194 | int addr_len, int flags) |
| 1195 | { |
| 1196 | struct sock *sk = sock->sk; |
| 1197 | struct atalk_sock *at = at_sk(sk); |
| 1198 | struct sockaddr_at *addr; |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1199 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
| 1201 | sk->sk_state = TCP_CLOSE; |
| 1202 | sock->state = SS_UNCONNECTED; |
| 1203 | |
| 1204 | if (addr_len != sizeof(*addr)) |
| 1205 | return -EINVAL; |
| 1206 | |
| 1207 | addr = (struct sockaddr_at *)uaddr; |
| 1208 | |
| 1209 | if (addr->sat_family != AF_APPLETALK) |
| 1210 | return -EAFNOSUPPORT; |
| 1211 | |
| 1212 | if (addr->sat_addr.s_node == ATADDR_BCAST && |
| 1213 | !sock_flag(sk, SOCK_BROADCAST)) { |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1214 | #if 1 |
Dave Jones | 278f015 | 2012-06-06 08:45:59 +0000 | [diff] [blame] | 1215 | pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | current->comm); |
| 1217 | #else |
| 1218 | return -EACCES; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1219 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1222 | lock_sock(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1223 | err = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | if (sock_flag(sk, SOCK_ZAPPED)) |
| 1225 | if (atalk_autobind(sk) < 0) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1226 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1228 | err = -ENETUNREACH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | if (!atrtr_get_dev(&addr->sat_addr)) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1230 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
| 1232 | at->dest_port = addr->sat_port; |
| 1233 | at->dest_net = addr->sat_addr.s_net; |
| 1234 | at->dest_node = addr->sat_addr.s_node; |
| 1235 | |
| 1236 | sock->state = SS_CONNECTED; |
| 1237 | sk->sk_state = TCP_ESTABLISHED; |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1238 | err = 0; |
| 1239 | out: |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1240 | release_sock(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1241 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * Find the name of an AppleTalk socket. Just copy the right |
| 1246 | * fields into the sockaddr. |
| 1247 | */ |
| 1248 | static int atalk_getname(struct socket *sock, struct sockaddr *uaddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1249 | int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | { |
| 1251 | struct sockaddr_at sat; |
| 1252 | struct sock *sk = sock->sk; |
| 1253 | struct atalk_sock *at = at_sk(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1254 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1256 | lock_sock(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1257 | err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | if (sock_flag(sk, SOCK_ZAPPED)) |
| 1259 | if (atalk_autobind(sk) < 0) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1260 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Dan Carpenter | fccc9f1 | 2013-04-22 20:22:15 +0000 | [diff] [blame] | 1262 | memset(&sat, 0, sizeof(sat)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
| 1264 | if (peer) { |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1265 | err = -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | if (sk->sk_state != TCP_ESTABLISHED) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1267 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
| 1269 | sat.sat_addr.s_net = at->dest_net; |
| 1270 | sat.sat_addr.s_node = at->dest_node; |
| 1271 | sat.sat_port = at->dest_port; |
| 1272 | } else { |
| 1273 | sat.sat_addr.s_net = at->src_net; |
| 1274 | sat.sat_addr.s_node = at->src_node; |
| 1275 | sat.sat_port = at->src_port; |
| 1276 | } |
| 1277 | |
| 1278 | sat.sat_family = AF_APPLETALK; |
| 1279 | memcpy(uaddr, &sat, sizeof(sat)); |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1280 | err = sizeof(struct sockaddr_at); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1281 | |
| 1282 | out: |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1283 | release_sock(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1284 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Javier Martinez Canillas | a73ec31 | 2016-09-09 08:43:13 -0400 | [diff] [blame] | 1287 | #if IS_ENABLED(CONFIG_IPDDP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | static __inline__ int is_ip_over_ddp(struct sk_buff *skb) |
| 1289 | { |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1290 | return skb->data[12] == 22; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | static int handle_ip_over_ddp(struct sk_buff *skb) |
| 1294 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1295 | struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | struct net_device_stats *stats; |
| 1297 | |
| 1298 | /* This needs to be able to handle ipddp"N" devices */ |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1299 | if (!dev) { |
| 1300 | kfree_skb(skb); |
| 1301 | return NET_RX_DROP; |
| 1302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1304 | skb->protocol = htons(ETH_P_IP); |
| 1305 | skb_pull(skb, 13); |
| 1306 | skb->dev = dev; |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 1307 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
Wang Chen | 524ad0a | 2008-11-12 23:39:10 -0800 | [diff] [blame] | 1309 | stats = netdev_priv(dev); |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1310 | stats->rx_packets++; |
| 1311 | stats->rx_bytes += skb->len + 13; |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1312 | return netif_rx(skb); /* Send the SKB up to a higher place. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | } |
| 1314 | #else |
| 1315 | /* make it easy for gcc to optimize this test out, i.e. kill the code */ |
| 1316 | #define is_ip_over_ddp(skb) 0 |
| 1317 | #define handle_ip_over_ddp(skb) 0 |
| 1318 | #endif |
| 1319 | |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1320 | static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev, |
| 1321 | struct ddpehdr *ddp, __u16 len_hops, int origlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | { |
| 1323 | struct atalk_route *rt; |
| 1324 | struct atalk_addr ta; |
| 1325 | |
| 1326 | /* |
| 1327 | * Don't route multicast, etc., packets, or packets sent to "this |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1328 | * network" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | */ |
| 1330 | if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) { |
| 1331 | /* |
| 1332 | * FIXME: |
| 1333 | * |
| 1334 | * Can it ever happen that a packet is from a PPP iface and |
| 1335 | * needs to be broadcast onto the default network? |
| 1336 | */ |
| 1337 | if (dev->type == ARPHRD_PPP) |
| 1338 | printk(KERN_DEBUG "AppleTalk: didn't forward broadcast " |
| 1339 | "packet received from PPP iface\n"); |
| 1340 | goto free_it; |
| 1341 | } |
| 1342 | |
| 1343 | ta.s_net = ddp->deh_dnet; |
| 1344 | ta.s_node = ddp->deh_dnode; |
| 1345 | |
| 1346 | /* Route the packet */ |
| 1347 | rt = atrtr_find(&ta); |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1348 | /* increment hops count */ |
| 1349 | len_hops += 1 << 10; |
| 1350 | if (!rt || !(len_hops & (15 << 10))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | goto free_it; |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | /* FIXME: use skb->cb to be able to use shared skbs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
| 1355 | /* |
| 1356 | * Route goes through another gateway, so set the target to the |
| 1357 | * gateway instead. |
| 1358 | */ |
| 1359 | |
| 1360 | if (rt->flags & RTF_GATEWAY) { |
| 1361 | ta.s_net = rt->gateway.s_net; |
| 1362 | ta.s_node = rt->gateway.s_node; |
| 1363 | } |
| 1364 | |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1365 | /* Fix up skb->len field */ |
| 1366 | skb_trim(skb, min_t(unsigned int, origlen, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | (rt->dev->hard_header_len + |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1368 | ddp_dl->header_length + (len_hops & 1023)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | /* FIXME: use skb->cb to be able to use shared skbs */ |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1371 | ddp->deh_len_hops = htons(len_hops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
| 1373 | /* |
| 1374 | * Send the buffer onwards |
| 1375 | * |
| 1376 | * Now we must always be careful. If it's come from LocalTalk to |
| 1377 | * EtherTalk it might not fit |
| 1378 | * |
| 1379 | * Order matters here: If a packet has to be copied to make a new |
| 1380 | * headroom (rare hopefully) then it won't need unsharing. |
| 1381 | * |
| 1382 | * Note. ddp-> becomes invalid at the realloc. |
| 1383 | */ |
| 1384 | if (skb_headroom(skb) < 22) { |
| 1385 | /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */ |
| 1386 | struct sk_buff *nskb = skb_realloc_headroom(skb, 32); |
| 1387 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | skb = nskb; |
| 1389 | } else |
| 1390 | skb = skb_unshare(skb, GFP_ATOMIC); |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * If the buffer didn't vanish into the lack of space bitbucket we can |
| 1394 | * send it. |
| 1395 | */ |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1396 | if (skb == NULL) |
| 1397 | goto drop; |
| 1398 | |
| 1399 | if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP) |
| 1400 | return NET_RX_DROP; |
Mark Smith | 8be8057 | 2009-09-12 20:48:43 +0000 | [diff] [blame] | 1401 | return NET_RX_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | free_it: |
| 1403 | kfree_skb(skb); |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1404 | drop: |
| 1405 | return NET_RX_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * atalk_rcv - Receive a packet (in skb) from device dev |
Andrew Lunn | 709565a | 2020-10-28 01:55:27 +0100 | [diff] [blame] | 1410 | * @skb: packet received |
| 1411 | * @dev: network device where the packet comes from |
| 1412 | * @pt: packet type |
| 1413 | * @orig_dev: the original receive net device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | * |
| 1415 | * Receive a packet (in skb) from device dev. This has come from the SNAP |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1416 | * decoder, and on entry skb->transport_header is the DDP header, skb->len |
| 1417 | * is the DDP header, skb->len is the DDP length. The physical headers |
| 1418 | * have been extracted. PPP should probably pass frames marked as for this |
| 1419 | * layer. [ie ARPHRD_ETHERTALK] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | */ |
| 1421 | static int atalk_rcv(struct sk_buff *skb, struct net_device *dev, |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 1422 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | { |
| 1424 | struct ddpehdr *ddp; |
| 1425 | struct sock *sock; |
| 1426 | struct atalk_iface *atif; |
| 1427 | struct sockaddr_at tosat; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1428 | int origlen; |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1429 | __u16 len_hops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 1431 | if (!net_eq(dev_net(dev), &init_net)) |
Mark Smith | 6885ffb | 2009-08-06 23:21:22 +0000 | [diff] [blame] | 1432 | goto drop; |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 1433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | /* Don't mangle buffer if shared */ |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1435 | if (!(skb = skb_share_check(skb, GFP_ATOMIC))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | goto out; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | /* Size check and make sure header is contiguous */ |
| 1439 | if (!pskb_may_pull(skb, sizeof(*ddp))) |
Mark Smith | 6885ffb | 2009-08-06 23:21:22 +0000 | [diff] [blame] | 1440 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | |
| 1442 | ddp = ddp_hdr(skb); |
| 1443 | |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1444 | len_hops = ntohs(ddp->deh_len_hops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | /* Trim buffer in case of stray trailing data */ |
| 1447 | origlen = skb->len; |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1448 | skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
| 1450 | /* |
| 1451 | * Size check to see if ddp->deh_len was crap |
| 1452 | * (Otherwise we'll detonate most spectacularly |
Jean Delvare | 75559c1 | 2007-04-04 23:52:46 -0700 | [diff] [blame] | 1453 | * in the middle of atalk_checksum() or recvmsg()). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | */ |
Jean Delvare | 75559c1 | 2007-04-04 23:52:46 -0700 | [diff] [blame] | 1455 | if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) { |
| 1456 | pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, " |
| 1457 | "skb->len=%u)\n", len_hops & 1023, skb->len); |
Mark Smith | 6885ffb | 2009-08-06 23:21:22 +0000 | [diff] [blame] | 1458 | goto drop; |
Jean Delvare | 75559c1 | 2007-04-04 23:52:46 -0700 | [diff] [blame] | 1459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | |
| 1461 | /* |
| 1462 | * Any checksums. Note we don't do htons() on this == is assumed to be |
| 1463 | * valid for net byte orders all over the networking code... |
| 1464 | */ |
| 1465 | if (ddp->deh_sum && |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1466 | atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* Not a valid AppleTalk frame - dustbin time */ |
Mark Smith | 6885ffb | 2009-08-06 23:21:22 +0000 | [diff] [blame] | 1468 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
| 1470 | /* Check the packet is aimed at us */ |
| 1471 | if (!ddp->deh_dnet) /* Net 0 is 'this network' */ |
| 1472 | atif = atalk_find_anynet(ddp->deh_dnode, dev); |
| 1473 | else |
| 1474 | atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode); |
| 1475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | if (!atif) { |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 1477 | /* Not ours, so we route the packet via the correct |
| 1478 | * AppleTalk iface |
| 1479 | */ |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1480 | return atalk_route_packet(skb, dev, ddp, len_hops, origlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | /* if IP over DDP is not selected this code will be optimized out */ |
| 1484 | if (is_ip_over_ddp(skb)) |
| 1485 | return handle_ip_over_ddp(skb); |
| 1486 | /* |
| 1487 | * Which socket - atalk_search_socket() looks for a *full match* |
| 1488 | * of the <net, node, port> tuple. |
| 1489 | */ |
| 1490 | tosat.sat_addr.s_net = ddp->deh_dnet; |
| 1491 | tosat.sat_addr.s_node = ddp->deh_dnode; |
| 1492 | tosat.sat_port = ddp->deh_dport; |
| 1493 | |
| 1494 | sock = atalk_search_socket(&tosat, atif); |
| 1495 | if (!sock) /* But not one of our sockets */ |
Mark Smith | 6885ffb | 2009-08-06 23:21:22 +0000 | [diff] [blame] | 1496 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
| 1498 | /* Queue packet (standard) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (sock_queue_rcv_skb(sock, skb) < 0) |
Mark Smith | 6885ffb | 2009-08-06 23:21:22 +0000 | [diff] [blame] | 1500 | goto drop; |
| 1501 | |
| 1502 | return NET_RX_SUCCESS; |
| 1503 | |
| 1504 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | kfree_skb(skb); |
Mark Smith | 6885ffb | 2009-08-06 23:21:22 +0000 | [diff] [blame] | 1506 | out: |
| 1507 | return NET_RX_DROP; |
| 1508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Receive a LocalTalk frame. We make some demands on the caller here. |
| 1513 | * Caller must provide enough headroom on the packet to pull the short |
| 1514 | * header and append a long one. |
| 1515 | */ |
| 1516 | static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev, |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 1517 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | { |
YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 1519 | if (!net_eq(dev_net(dev), &init_net)) |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 1520 | goto freeit; |
| 1521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | /* Expand any short form frames */ |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 1523 | if (skb_mac_header(skb)[2] == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | struct ddpehdr *ddp; |
| 1525 | /* Find our address */ |
| 1526 | struct atalk_addr *ap = atalk_find_dev_addr(dev); |
| 1527 | |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1528 | if (!ap || skb->len < sizeof(__be16) || skb->len > 1023) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | goto freeit; |
| 1530 | |
| 1531 | /* Don't mangle buffer if shared */ |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1532 | if (!(skb = skb_share_check(skb, GFP_ATOMIC))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | return 0; |
| 1534 | |
| 1535 | /* |
| 1536 | * The push leaves us with a ddephdr not an shdr, and |
| 1537 | * handily the port bytes in the right place preset. |
| 1538 | */ |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 1539 | ddp = skb_push(skb, sizeof(*ddp) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | /* Now fill in the long header */ |
| 1542 | |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1543 | /* |
| 1544 | * These two first. The mac overlays the new source/dest |
| 1545 | * network information so we MUST copy these before |
| 1546 | * we write the network numbers ! |
| 1547 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 1549 | ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */ |
| 1550 | ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
| 1552 | ddp->deh_dnet = ap->s_net; /* Network number */ |
| 1553 | ddp->deh_snet = ap->s_net; |
| 1554 | ddp->deh_sum = 0; /* No checksum */ |
| 1555 | /* |
| 1556 | * Not sure about this bit... |
| 1557 | */ |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1558 | /* Non routable, so force a drop if we slip up later */ |
| 1559 | ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 1561 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 1563 | return atalk_rcv(skb, dev, pt, orig_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | freeit: |
| 1565 | kfree_skb(skb); |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1569 | static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | { |
| 1571 | struct sock *sk = sock->sk; |
| 1572 | struct atalk_sock *at = at_sk(sk); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1573 | DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | int flags = msg->msg_flags; |
| 1575 | int loopback = 0; |
| 1576 | struct sockaddr_at local_satalk, gsat; |
| 1577 | struct sk_buff *skb; |
| 1578 | struct net_device *dev; |
| 1579 | struct ddpehdr *ddp; |
Doug Brown | 39935dc | 2021-02-11 21:27:54 -0800 | [diff] [blame] | 1580 | int size, hard_header_len; |
| 1581 | struct atalk_route *rt, *rt_lo = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | int err; |
| 1583 | |
| 1584 | if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) |
| 1585 | return -EINVAL; |
| 1586 | |
| 1587 | if (len > DDP_MAXSZ) |
| 1588 | return -EMSGSIZE; |
| 1589 | |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1590 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | if (usat) { |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1592 | err = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | if (sock_flag(sk, SOCK_ZAPPED)) |
| 1594 | if (atalk_autobind(sk) < 0) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1595 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1597 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | if (msg->msg_namelen < sizeof(*usat) || |
| 1599 | usat->sat_family != AF_APPLETALK) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1600 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1602 | err = -EPERM; |
Alan Cox | 03ba999 | 2009-03-27 00:27:18 -0700 | [diff] [blame] | 1603 | /* netatalk didn't implement this check */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | if (usat->sat_addr.s_node == ATADDR_BCAST && |
| 1605 | !sock_flag(sk, SOCK_BROADCAST)) { |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1606 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | } |
| 1608 | } else { |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1609 | err = -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | if (sk->sk_state != TCP_ESTABLISHED) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1611 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | usat = &local_satalk; |
| 1613 | usat->sat_family = AF_APPLETALK; |
| 1614 | usat->sat_port = at->dest_port; |
| 1615 | usat->sat_addr.s_node = at->dest_node; |
| 1616 | usat->sat_addr.s_net = at->dest_net; |
| 1617 | } |
| 1618 | |
| 1619 | /* Build a packet */ |
| 1620 | SOCK_DEBUG(sk, "SK %p: Got address.\n", sk); |
| 1621 | |
| 1622 | /* For headers */ |
| 1623 | size = sizeof(struct ddpehdr) + len + ddp_dl->header_length; |
| 1624 | |
| 1625 | if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) { |
| 1626 | rt = atrtr_find(&usat->sat_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | } else { |
| 1628 | struct atalk_addr at_hint; |
| 1629 | |
| 1630 | at_hint.s_node = 0; |
| 1631 | at_hint.s_net = at->src_net; |
| 1632 | |
| 1633 | rt = atrtr_find(&at_hint); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | } |
Anton Protopopov | 48bb230 | 2016-02-17 10:53:59 -0500 | [diff] [blame] | 1635 | err = -ENETUNREACH; |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 1636 | if (!rt) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1637 | goto out; |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 1638 | |
| 1639 | dev = rt->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
| 1641 | SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n", |
| 1642 | sk, size, dev->name); |
| 1643 | |
Doug Brown | 39935dc | 2021-02-11 21:27:54 -0800 | [diff] [blame] | 1644 | hard_header_len = dev->hard_header_len; |
| 1645 | /* Leave room for loopback hardware header if necessary */ |
| 1646 | if (usat->sat_addr.s_node == ATADDR_BCAST && |
| 1647 | (dev->flags & IFF_LOOPBACK || !(rt->flags & RTF_GATEWAY))) { |
| 1648 | struct atalk_addr at_lo; |
| 1649 | |
| 1650 | at_lo.s_node = 0; |
| 1651 | at_lo.s_net = 0; |
| 1652 | |
| 1653 | rt_lo = atrtr_find(&at_lo); |
| 1654 | |
| 1655 | if (rt_lo && rt_lo->dev->hard_header_len > hard_header_len) |
| 1656 | hard_header_len = rt_lo->dev->hard_header_len; |
| 1657 | } |
| 1658 | |
| 1659 | size += hard_header_len; |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1660 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err); |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1662 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | if (!skb) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1664 | goto out; |
YOSHIFUJI Hideaki | ed4477b | 2007-02-09 23:24:27 +0900 | [diff] [blame] | 1665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | skb_reserve(skb, ddp_dl->header_length); |
Doug Brown | 39935dc | 2021-02-11 21:27:54 -0800 | [diff] [blame] | 1667 | skb_reserve(skb, hard_header_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | skb->dev = dev; |
| 1669 | |
| 1670 | SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk); |
| 1671 | |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 1672 | ddp = skb_put(skb, sizeof(struct ddpehdr)); |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1673 | ddp->deh_len_hops = htons(len + sizeof(*ddp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | ddp->deh_dnet = usat->sat_addr.s_net; |
| 1675 | ddp->deh_snet = at->src_net; |
| 1676 | ddp->deh_dnode = usat->sat_addr.s_node; |
| 1677 | ddp->deh_snode = at->src_node; |
| 1678 | ddp->deh_dport = usat->sat_port; |
| 1679 | ddp->deh_sport = at->src_port; |
| 1680 | |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1681 | SOCK_DEBUG(sk, "SK %p: Copy user data (%zd bytes).\n", sk, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 1683 | err = memcpy_from_msg(skb_put(skb, len), msg, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | if (err) { |
| 1685 | kfree_skb(skb); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1686 | err = -EFAULT; |
| 1687 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Tom Herbert | 28448b8 | 2014-05-23 08:47:19 -0700 | [diff] [blame] | 1690 | if (sk->sk_no_check_tx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | ddp->deh_sum = 0; |
| 1692 | else |
| 1693 | ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp)); |
| 1694 | |
| 1695 | /* |
| 1696 | * Loopback broadcast packets to non gateway targets (ie routes |
| 1697 | * to group we are in) |
| 1698 | */ |
| 1699 | if (ddp->deh_dnode == ATADDR_BCAST && |
| 1700 | !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) { |
| 1701 | struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL); |
| 1702 | |
| 1703 | if (skb2) { |
| 1704 | loopback = 1; |
| 1705 | SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk); |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1706 | /* |
| 1707 | * If it fails it is queued/sent above in the aarp queue |
| 1708 | */ |
| 1709 | aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | if (dev->flags & IFF_LOOPBACK || loopback) { |
| 1714 | SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk); |
| 1715 | /* loop back */ |
| 1716 | skb_orphan(skb); |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 1717 | if (ddp->deh_dnode == ATADDR_BCAST) { |
Doug Brown | 39935dc | 2021-02-11 21:27:54 -0800 | [diff] [blame] | 1718 | if (!rt_lo) { |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 1719 | kfree_skb(skb); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1720 | err = -ENETUNREACH; |
| 1721 | goto out; |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 1722 | } |
Doug Brown | 39935dc | 2021-02-11 21:27:54 -0800 | [diff] [blame] | 1723 | dev = rt_lo->dev; |
Oliver Dawid | 64233bf | 2005-09-27 16:11:29 -0700 | [diff] [blame] | 1724 | skb->dev = dev; |
| 1725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | ddp_dl->request(ddp_dl, skb, dev->dev_addr); |
| 1727 | } else { |
| 1728 | SOCK_DEBUG(sk, "SK %p: send out.\n", sk); |
| 1729 | if (rt->flags & RTF_GATEWAY) { |
| 1730 | gsat.sat_addr = rt->gateway; |
| 1731 | usat = &gsat; |
| 1732 | } |
| 1733 | |
Arnaldo Carvalho de Melo | ffcfb8d | 2009-09-11 11:35:22 -0700 | [diff] [blame] | 1734 | /* |
| 1735 | * If it fails it is queued/sent above in the aarp queue |
| 1736 | */ |
| 1737 | aarp_send_ddp(dev, skb, &usat->sat_addr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | } |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1739 | SOCK_DEBUG(sk, "SK %p: Done write (%zd).\n", sk, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1741 | out: |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1742 | release_sock(sk); |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1743 | return err ? : len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1746 | static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 1747 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | { |
| 1749 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | struct ddpehdr *ddp; |
| 1751 | int copied = 0; |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1752 | int offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | int err = 0; |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1754 | struct sk_buff *skb; |
| 1755 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1756 | skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | flags & MSG_DONTWAIT, &err); |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1758 | lock_sock(sk); |
| 1759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | if (!skb) |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1761 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
| 1763 | /* FIXME: use skb->cb to be able to use shared skbs */ |
| 1764 | ddp = ddp_hdr(skb); |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1765 | copied = ntohs(ddp->deh_len_hops) & 1023; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1767 | if (sk->sk_type != SOCK_RAW) { |
| 1768 | offset = sizeof(*ddp); |
| 1769 | copied -= offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1772 | if (copied > size) { |
| 1773 | copied = size; |
| 1774 | msg->msg_flags |= MSG_TRUNC; |
| 1775 | } |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 1776 | err = skb_copy_datagram_msg(skb, offset, msg, copied); |
Al Viro | 2a50f28 | 2006-09-26 21:22:08 -0700 | [diff] [blame] | 1777 | |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 1778 | if (!err && msg->msg_name) { |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1779 | DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name); |
Hannes Frederic Sowa | f3d3342 | 2013-11-21 03:14:22 +0100 | [diff] [blame] | 1780 | sat->sat_family = AF_APPLETALK; |
| 1781 | sat->sat_port = ddp->deh_sport; |
| 1782 | sat->sat_addr.s_node = ddp->deh_snode; |
| 1783 | sat->sat_addr.s_net = ddp->deh_snet; |
| 1784 | msg->msg_namelen = sizeof(*sat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | skb_free_datagram(sk, skb); /* Free the datagram. */ |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1788 | |
| 1789 | out: |
Arnd Bergmann | 60d9f46 | 2011-01-23 00:21:11 +0100 | [diff] [blame] | 1790 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | return err ? : copied; |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | /* |
| 1796 | * AppleTalk ioctl calls. |
| 1797 | */ |
| 1798 | static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1799 | { |
Christoph Hellwig | b5e5fa5 | 2006-01-03 14:18:33 -0800 | [diff] [blame] | 1800 | int rc = -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | struct sock *sk = sock->sk; |
| 1802 | void __user *argp = (void __user *)arg; |
| 1803 | |
| 1804 | switch (cmd) { |
wangweidong | 7b30600 | 2014-02-14 15:43:46 +0800 | [diff] [blame] | 1805 | /* Protocol layer */ |
| 1806 | case TIOCOUTQ: { |
| 1807 | long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | |
wangweidong | 7b30600 | 2014-02-14 15:43:46 +0800 | [diff] [blame] | 1809 | if (amount < 0) |
| 1810 | amount = 0; |
| 1811 | rc = put_user(amount, (int __user *)argp); |
| 1812 | break; |
| 1813 | } |
| 1814 | case TIOCINQ: { |
| 1815 | /* |
| 1816 | * These two are safe on a single CPU system as only |
| 1817 | * user tasks fiddle here |
| 1818 | */ |
| 1819 | struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); |
| 1820 | long amount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | |
wangweidong | 7b30600 | 2014-02-14 15:43:46 +0800 | [diff] [blame] | 1822 | if (skb) |
wangweidong | 63ae889 | 2014-07-04 15:29:48 +0800 | [diff] [blame] | 1823 | amount = skb->len - sizeof(struct ddpehdr); |
wangweidong | 7b30600 | 2014-02-14 15:43:46 +0800 | [diff] [blame] | 1824 | rc = put_user(amount, (int __user *)argp); |
| 1825 | break; |
| 1826 | } |
wangweidong | 7b30600 | 2014-02-14 15:43:46 +0800 | [diff] [blame] | 1827 | /* Routing */ |
| 1828 | case SIOCADDRT: |
| 1829 | case SIOCDELRT: |
| 1830 | rc = -EPERM; |
| 1831 | if (capable(CAP_NET_ADMIN)) |
| 1832 | rc = atrtr_ioctl(cmd, argp); |
| 1833 | break; |
| 1834 | /* Interface */ |
| 1835 | case SIOCGIFADDR: |
| 1836 | case SIOCSIFADDR: |
| 1837 | case SIOCGIFBRDADDR: |
| 1838 | case SIOCATALKDIFADDR: |
| 1839 | case SIOCDIFADDR: |
| 1840 | case SIOCSARP: /* proxy AARP */ |
| 1841 | case SIOCDARP: /* proxy AARP */ |
| 1842 | rtnl_lock(); |
| 1843 | rc = atif_ioctl(cmd, argp); |
| 1844 | rtnl_unlock(); |
| 1845 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | return rc; |
| 1849 | } |
| 1850 | |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1851 | |
| 1852 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | dc13c876 | 2020-05-18 08:28:08 +0200 | [diff] [blame] | 1853 | static int atalk_compat_routing_ioctl(struct sock *sk, unsigned int cmd, |
| 1854 | struct compat_rtentry __user *ur) |
| 1855 | { |
| 1856 | compat_uptr_t rtdev; |
| 1857 | struct rtentry rt; |
| 1858 | |
| 1859 | if (copy_from_user(&rt.rt_dst, &ur->rt_dst, |
| 1860 | 3 * sizeof(struct sockaddr)) || |
| 1861 | get_user(rt.rt_flags, &ur->rt_flags) || |
| 1862 | get_user(rt.rt_metric, &ur->rt_metric) || |
| 1863 | get_user(rt.rt_mtu, &ur->rt_mtu) || |
| 1864 | get_user(rt.rt_window, &ur->rt_window) || |
| 1865 | get_user(rt.rt_irtt, &ur->rt_irtt) || |
| 1866 | get_user(rtdev, &ur->rt_dev)) |
| 1867 | return -EFAULT; |
| 1868 | |
| 1869 | switch (cmd) { |
| 1870 | case SIOCDELRT: |
| 1871 | if (rt.rt_dst.sa_family != AF_APPLETALK) |
| 1872 | return -EINVAL; |
| 1873 | return atrtr_delete(&((struct sockaddr_at *) |
| 1874 | &rt.rt_dst)->sat_addr); |
| 1875 | |
| 1876 | case SIOCADDRT: |
| 1877 | rt.rt_dev = compat_ptr(rtdev); |
| 1878 | return atrtr_ioctl_addrt(&rt); |
| 1879 | default: |
| 1880 | return -EINVAL; |
| 1881 | } |
| 1882 | } |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1883 | static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 1884 | { |
Christoph Hellwig | dc13c876 | 2020-05-18 08:28:08 +0200 | [diff] [blame] | 1885 | void __user *argp = compat_ptr(arg); |
| 1886 | struct sock *sk = sock->sk; |
| 1887 | |
| 1888 | switch (cmd) { |
| 1889 | case SIOCADDRT: |
| 1890 | case SIOCDELRT: |
| 1891 | return atalk_compat_routing_ioctl(sk, cmd, argp); |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1892 | /* |
Arnd Bergmann | 2066022 | 2009-11-06 08:09:06 +0000 | [diff] [blame] | 1893 | * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we |
| 1894 | * cannot handle it in common code. The data we access if ifreq |
| 1895 | * here is compatible, so we can simply call the native |
| 1896 | * handler. |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1897 | */ |
Christoph Hellwig | dc13c876 | 2020-05-18 08:28:08 +0200 | [diff] [blame] | 1898 | case SIOCATALKDIFADDR: |
| 1899 | return atalk_ioctl(sock, cmd, (unsigned long)argp); |
| 1900 | default: |
| 1901 | return -ENOIOCTLCMD; |
| 1902 | } |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1903 | } |
Christoph Hellwig | dc13c876 | 2020-05-18 08:28:08 +0200 | [diff] [blame] | 1904 | #endif /* CONFIG_COMPAT */ |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1905 | |
| 1906 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1907 | static const struct net_proto_family atalk_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | .family = PF_APPLETALK, |
| 1909 | .create = atalk_create, |
| 1910 | .owner = THIS_MODULE, |
| 1911 | }; |
| 1912 | |
Arnd Bergmann | ecced8b | 2009-11-05 04:37:26 +0000 | [diff] [blame] | 1913 | static const struct proto_ops atalk_dgram_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | .family = PF_APPLETALK, |
| 1915 | .owner = THIS_MODULE, |
| 1916 | .release = atalk_release, |
| 1917 | .bind = atalk_bind, |
| 1918 | .connect = atalk_connect, |
| 1919 | .socketpair = sock_no_socketpair, |
| 1920 | .accept = sock_no_accept, |
| 1921 | .getname = atalk_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1922 | .poll = datagram_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | .ioctl = atalk_ioctl, |
Arnd Bergmann | c7cbdbf | 2019-04-17 22:51:48 +0200 | [diff] [blame] | 1924 | .gettstamp = sock_gettstamp, |
Petr Vandrovec | f6c90b7 | 2006-03-27 23:39:31 -0800 | [diff] [blame] | 1925 | #ifdef CONFIG_COMPAT |
| 1926 | .compat_ioctl = atalk_compat_ioctl, |
| 1927 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | .listen = sock_no_listen, |
| 1929 | .shutdown = sock_no_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | .sendmsg = atalk_sendmsg, |
| 1931 | .recvmsg = atalk_recvmsg, |
| 1932 | .mmap = sock_no_mmap, |
| 1933 | .sendpage = sock_no_sendpage, |
| 1934 | }; |
| 1935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | static struct notifier_block ddp_notifier = { |
| 1937 | .notifier_call = ddp_device_event, |
| 1938 | }; |
| 1939 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 1940 | static struct packet_type ltalk_packet_type __read_mostly = { |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 1941 | .type = cpu_to_be16(ETH_P_LOCALTALK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | .func = ltalk_rcv, |
| 1943 | }; |
| 1944 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 1945 | static struct packet_type ppptalk_packet_type __read_mostly = { |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 1946 | .type = cpu_to_be16(ETH_P_PPPTALK), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | .func = atalk_rcv, |
| 1948 | }; |
| 1949 | |
| 1950 | static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B }; |
| 1951 | |
| 1952 | /* Export symbols for use by drivers when AppleTalk is a module */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | EXPORT_SYMBOL(atrtr_get_dev); |
| 1954 | EXPORT_SYMBOL(atalk_find_dev_addr); |
| 1955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | /* Called by proto.c on kernel start up */ |
| 1957 | static int __init atalk_init(void) |
| 1958 | { |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 1959 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 1961 | rc = proto_register(&ddp_proto, 0); |
| 1962 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | goto out; |
| 1964 | |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 1965 | rc = sock_register(&atalk_family_ops); |
| 1966 | if (rc) |
| 1967 | goto out_proto; |
| 1968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv); |
YueHaibing | 9804501 | 2019-03-14 13:47:59 +0800 | [diff] [blame] | 1970 | if (!ddp_dl) { |
| 1971 | pr_crit("Unable to register DDP with SNAP.\n"); |
YueHaibing | c93ad13 | 2019-04-30 19:34:08 +0800 | [diff] [blame] | 1972 | rc = -ENOMEM; |
YueHaibing | 9804501 | 2019-03-14 13:47:59 +0800 | [diff] [blame] | 1973 | goto out_sock; |
| 1974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | |
| 1976 | dev_add_pack(<alk_packet_type); |
| 1977 | dev_add_pack(&ppptalk_packet_type); |
| 1978 | |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 1979 | rc = register_netdevice_notifier(&ddp_notifier); |
| 1980 | if (rc) |
YueHaibing | 9804501 | 2019-03-14 13:47:59 +0800 | [diff] [blame] | 1981 | goto out_snap; |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 1982 | |
YueHaibing | 9804501 | 2019-03-14 13:47:59 +0800 | [diff] [blame] | 1983 | rc = aarp_proto_init(); |
| 1984 | if (rc) |
| 1985 | goto out_dev; |
| 1986 | |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 1987 | rc = atalk_proc_init(); |
| 1988 | if (rc) |
| 1989 | goto out_aarp; |
| 1990 | |
| 1991 | rc = atalk_register_sysctl(); |
| 1992 | if (rc) |
| 1993 | goto out_proc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | out: |
| 1995 | return rc; |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 1996 | out_proc: |
| 1997 | atalk_proc_exit(); |
| 1998 | out_aarp: |
| 1999 | aarp_cleanup_module(); |
YueHaibing | 9804501 | 2019-03-14 13:47:59 +0800 | [diff] [blame] | 2000 | out_dev: |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 2001 | unregister_netdevice_notifier(&ddp_notifier); |
YueHaibing | 9804501 | 2019-03-14 13:47:59 +0800 | [diff] [blame] | 2002 | out_snap: |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 2003 | dev_remove_pack(&ppptalk_packet_type); |
| 2004 | dev_remove_pack(<alk_packet_type); |
| 2005 | unregister_snap_client(ddp_dl); |
YueHaibing | 9804501 | 2019-03-14 13:47:59 +0800 | [diff] [blame] | 2006 | out_sock: |
YueHaibing | 6377f78 | 2019-03-01 10:57:57 +0800 | [diff] [blame] | 2007 | sock_unregister(PF_APPLETALK); |
| 2008 | out_proto: |
| 2009 | proto_unregister(&ddp_proto); |
| 2010 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | } |
| 2012 | module_init(atalk_init); |
| 2013 | |
| 2014 | /* |
| 2015 | * No explicit module reference count manipulation is needed in the |
| 2016 | * protocol. Socket layer sets module reference count for us |
| 2017 | * and interfaces reference counting is done |
| 2018 | * by the network device layer. |
| 2019 | * |
| 2020 | * Ergo, before the AppleTalk module can be removed, all AppleTalk |
gushengxian | 2aa8eca | 2021-06-08 18:52:57 -0700 | [diff] [blame] | 2021 | * sockets should be closed from user space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | */ |
| 2023 | static void __exit atalk_exit(void) |
| 2024 | { |
| 2025 | #ifdef CONFIG_SYSCTL |
| 2026 | atalk_unregister_sysctl(); |
| 2027 | #endif /* CONFIG_SYSCTL */ |
| 2028 | atalk_proc_exit(); |
| 2029 | aarp_cleanup_module(); /* General aarp clean-up. */ |
| 2030 | unregister_netdevice_notifier(&ddp_notifier); |
| 2031 | dev_remove_pack(<alk_packet_type); |
| 2032 | dev_remove_pack(&ppptalk_packet_type); |
| 2033 | unregister_snap_client(ddp_dl); |
| 2034 | sock_unregister(PF_APPLETALK); |
| 2035 | proto_unregister(&ddp_proto); |
| 2036 | } |
| 2037 | module_exit(atalk_exit); |
| 2038 | |
| 2039 | MODULE_LICENSE("GPL"); |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 2040 | MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | MODULE_DESCRIPTION("AppleTalk 0.20\n"); |
| 2042 | MODULE_ALIAS_NETPROTO(PF_APPLETALK); |