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