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