Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * DDP: An implementation of the AppleTalk DDP protocol for | |
3 | * Ethernet 'ELAP'. | |
4 | * | |
113aa838 | 5 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
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) | |
ed4477b9 | 31 | * Jay Schulist : Made work as module with |
1da177e4 LT |
32 | * AppleTalk drivers, cleaned it. |
33 | * Rob Newberry : Added proxy AARP and AARP | |
34 | * procfs, moved probing to AARP | |
35 | * module. | |
ed4477b9 YH |
36 | * Adrian Sun/ |
37 | * Michael Zuelsdorff : fix for net.0 packets. don't | |
1da177e4 | 38 | * allow illegal ether/tokentalk |
ed4477b9 YH |
39 | * port assignment. we lose a |
40 | * valid localtalk port as a | |
1da177e4 LT |
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. | |
ed4477b9 | 51 | * |
1da177e4 LT |
52 | */ |
53 | ||
4fc268d2 | 54 | #include <linux/capability.h> |
1da177e4 | 55 | #include <linux/module.h> |
1da177e4 LT |
56 | #include <linux/if_arp.h> |
57 | #include <linux/termios.h> /* For TIOCOUTQ/INQ */ | |
58 | #include <net/datalink.h> | |
59 | #include <net/psnap.h> | |
60 | #include <net/sock.h> | |
c752f073 | 61 | #include <net/tcp_states.h> |
1da177e4 LT |
62 | #include <net/route.h> |
63 | #include <linux/atalk.h> | |
a1f8e7f7 | 64 | #include "../core/kmap_skb.h" |
1da177e4 LT |
65 | |
66 | struct datalink_proto *ddp_dl, *aarp_dl; | |
90ddc4f0 | 67 | static const struct proto_ops atalk_dgram_ops; |
1da177e4 LT |
68 | |
69 | /**************************************************************************\ | |
70 | * * | |
71 | * Handlers for the socket list. * | |
72 | * * | |
73 | \**************************************************************************/ | |
74 | ||
75 | HLIST_HEAD(atalk_sockets); | |
76 | DEFINE_RWLOCK(atalk_sockets_lock); | |
77 | ||
78 | static inline void __atalk_insert_socket(struct sock *sk) | |
79 | { | |
80 | sk_add_node(sk, &atalk_sockets); | |
81 | } | |
82 | ||
83 | static inline void atalk_remove_socket(struct sock *sk) | |
84 | { | |
85 | write_lock_bh(&atalk_sockets_lock); | |
86 | sk_del_node_init(sk); | |
87 | write_unlock_bh(&atalk_sockets_lock); | |
88 | } | |
89 | ||
90 | static struct sock *atalk_search_socket(struct sockaddr_at *to, | |
91 | struct atalk_iface *atif) | |
92 | { | |
93 | struct sock *s; | |
94 | struct hlist_node *node; | |
95 | ||
96 | read_lock_bh(&atalk_sockets_lock); | |
97 | sk_for_each(s, node, &atalk_sockets) { | |
98 | struct atalk_sock *at = at_sk(s); | |
99 | ||
100 | if (to->sat_port != at->src_port) | |
101 | continue; | |
102 | ||
ed4477b9 | 103 | if (to->sat_addr.s_net == ATADDR_ANYNET && |
64233bff | 104 | to->sat_addr.s_node == ATADDR_BCAST) |
1da177e4 LT |
105 | goto found; |
106 | ||
ed4477b9 | 107 | if (to->sat_addr.s_net == at->src_net && |
1da177e4 LT |
108 | (to->sat_addr.s_node == at->src_node || |
109 | to->sat_addr.s_node == ATADDR_BCAST || | |
110 | to->sat_addr.s_node == ATADDR_ANYNODE)) | |
111 | goto found; | |
112 | ||
ed4477b9 | 113 | /* XXXX.0 -- we got a request for this router. make sure |
1da177e4 LT |
114 | * that the node is appropriately set. */ |
115 | if (to->sat_addr.s_node == ATADDR_ANYNODE && | |
116 | to->sat_addr.s_net != ATADDR_ANYNET && | |
117 | atif->address.s_node == at->src_node) { | |
118 | to->sat_addr.s_node = atif->address.s_node; | |
119 | goto found; | |
120 | } | |
121 | } | |
122 | s = NULL; | |
123 | found: | |
124 | read_unlock_bh(&atalk_sockets_lock); | |
125 | return s; | |
126 | } | |
127 | ||
128 | /** | |
129 | * atalk_find_or_insert_socket - Try to find a socket matching ADDR | |
130 | * @sk - socket to insert in the list if it is not there already | |
131 | * @sat - address to search for | |
132 | * | |
133 | * Try to find a socket matching ADDR in the socket list, if found then return | |
134 | * it. If not, insert SK into the socket list. | |
135 | * | |
136 | * This entire operation must execute atomically. | |
137 | */ | |
138 | static struct sock *atalk_find_or_insert_socket(struct sock *sk, | |
139 | struct sockaddr_at *sat) | |
140 | { | |
141 | struct sock *s; | |
142 | struct hlist_node *node; | |
143 | struct atalk_sock *at; | |
144 | ||
145 | write_lock_bh(&atalk_sockets_lock); | |
146 | sk_for_each(s, node, &atalk_sockets) { | |
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 | ||
c564039f | 165 | if (sk_has_allocations(sk)) { |
1da177e4 LT |
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 | ||
c564039f | 177 | if (sk_has_allocations(sk)) { |
b24b8a24 PE |
178 | setup_timer(&sk->sk_timer, atalk_destroy_timer, |
179 | (unsigned long)sk); | |
1da177e4 | 180 | sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME; |
1da177e4 LT |
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 | { | |
0da974f4 | 228 | struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL); |
1da177e4 LT |
229 | |
230 | if (!iface) | |
231 | goto out; | |
232 | ||
1da177e4 LT |
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, | |
296 | struct atalk_addr* proxy_addr) | |
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; | |
ed4477b9 | 314 | |
1da177e4 LT |
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 */ | |
f6e276ee | 400 | static struct atalk_iface *atalk_find_interface(__be16 net, int node) |
1da177e4 LT |
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)) | |
ed4477b9 | 417 | break; |
1da177e4 LT |
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 | /* | |
ed4477b9 | 432 | * we must search through all routes unless we find a |
1da177e4 LT |
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; | |
ed4477b9 | 438 | |
1da177e4 LT |
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 | } | |
ed4477b9 YH |
460 | |
461 | /* | |
1da177e4 LT |
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) >= | |
ed4477b9 | 540 | ntohs(iface->nets.nr_firstnet) && |
1da177e4 | 541 | ntohs(ga->sat_addr.s_net) <= |
ed4477b9 | 542 | ntohs(iface->nets.nr_lastnet)) |
1da177e4 LT |
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; | |
ed4477b9 | 548 | } |
1da177e4 LT |
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) { | |
0da974f4 | 559 | rt = kzalloc(sizeof(*rt), GFP_ATOMIC); |
1da177e4 LT |
560 | |
561 | retval = -ENOBUFS; | |
562 | if (!rt) | |
563 | goto out_unlock; | |
1da177e4 LT |
564 | |
565 | rt->next = atalk_routes; | |
566 | atalk_routes = rt; | |
567 | } | |
568 | ||
569 | /* Fill in the routing entry */ | |
570 | rt->target = ta->sat_addr; | |
c7f905f0 | 571 | dev_hold(devhint); |
1da177e4 LT |
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 */ | |
584 | static int atrtr_delete(struct atalk_addr * addr) | |
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 | { | |
890d52d3 EB |
647 | struct net_device *dev = ptr; |
648 | ||
721499e8 | 649 | if (!net_eq(dev_net(dev), &init_net)) |
e9dc8653 EB |
650 | return NOTIFY_DONE; |
651 | ||
1da177e4 LT |
652 | if (event == NETDEV_DOWN) |
653 | /* Discard any use of this */ | |
890d52d3 | 654 | atalk_dev_down(dev); |
1da177e4 LT |
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 | ||
881d966b | 677 | dev = __dev_get_by_name(&init_net, atreq.ifr_name); |
1da177e4 LT |
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) { | |
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; | |
695 | ||
696 | nr = (struct atalk_netrange *)&sa->sat_zero[0]; | |
697 | add_route = 1; | |
698 | ||
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, | |
ed4477b9 | 706 | sa->sat_addr.s_node)) { |
1da177e4 LT |
707 | printk(KERN_DEBUG "AppleTalk: point-to-point " |
708 | "interface added with " | |
709 | "existing address\n"); | |
710 | add_route = 0; | |
711 | } | |
ed4477b9 | 712 | |
1da177e4 LT |
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; | |
726 | ||
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); | |
732 | if (!atif) | |
733 | return -ENOMEM; | |
734 | } | |
735 | atif->nets = *nr; | |
736 | ||
737 | /* | |
738 | * Check if the chosen address is used. If so we | |
739 | * error and atalkd will try another. | |
740 | */ | |
741 | ||
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 | } | |
748 | ||
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)) { | |
765 | sa->sat_addr.s_net = atif->address.s_net; | |
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"); | |
773 | return -EINVAL; | |
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(dev, aarp_mcast, 6, 1); | |
783 | return 0; | |
784 | ||
785 | case SIOCGIFADDR: | |
786 | if (!atif) | |
787 | return -EADDRNOTAVAIL; | |
788 | ||
789 | sa->sat_family = AF_APPLETALK; | |
790 | sa->sat_addr = atif->address; | |
791 | break; | |
792 | ||
793 | case SIOCGIFBRDADDR: | |
794 | if (!atif) | |
795 | return -EADDRNOTAVAIL; | |
796 | ||
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; | |
801 | ||
ed4477b9 YH |
802 | case SIOCATALKDIFADDR: |
803 | case SIOCDIFADDR: | |
1da177e4 LT |
804 | if (!capable(CAP_NET_ADMIN)) |
805 | return -EPERM; | |
806 | if (sa->sat_family != AF_APPLETALK) | |
807 | return -EINVAL; | |
808 | atalk_dev_down(dev); | |
ed4477b9 | 809 | break; |
1da177e4 LT |
810 | |
811 | case SIOCSARP: | |
812 | if (!capable(CAP_NET_ADMIN)) | |
ed4477b9 YH |
813 | return -EPERM; |
814 | if (sa->sat_family != AF_APPLETALK) | |
815 | return -EINVAL; | |
ed4477b9 YH |
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; | |
822 | ||
823 | /* | |
824 | * atif points to the current interface on this network; | |
825 | * we aren't concerned about its current status (at | |
1da177e4 LT |
826 | * least for now), but it has all the settings about |
827 | * the network we're going to probe. Consequently, it | |
828 | * must exist. | |
ed4477b9 YH |
829 | */ |
830 | if (!atif) | |
831 | return -EADDRNOTAVAIL; | |
832 | ||
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 || | |
1da177e4 | 842 | sa->sat_addr.s_node == 254) |
ed4477b9 YH |
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 | ||
1da177e4 | 852 | /* |
ed4477b9 | 853 | * We now have an address on the local network, and |
1da177e4 LT |
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. | |
ed4477b9 YH |
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; | |
1da177e4 LT |
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) { | |
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); | |
890 | ||
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'; | |
881d966b | 898 | dev = __dev_get_by_name(&init_net, name); |
1da177e4 LT |
899 | if (!dev) |
900 | return -ENODEV; | |
ed4477b9 | 901 | } |
1da177e4 LT |
902 | return atrtr_create(&rt, dev); |
903 | } | |
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 | */ | |
ed4477b9 | 919 | static unsigned long atalk_sum_partial(const unsigned char *data, |
1da177e4 LT |
920 | int len, unsigned long sum) |
921 | { | |
922 | /* This ought to be unwrapped neatly. I'll trust gcc for now */ | |
923 | while (len--) { | |
924 | sum += *data; | |
925 | sum <<= 1; | |
926 | if (sum & 0x10000) { | |
927 | sum++; | |
928 | sum &= 0xffff; | |
929 | } | |
930 | data++; | |
931 | } | |
932 | return sum; | |
933 | } | |
934 | ||
935 | /* Checksum skb data -- similar to skb_checksum */ | |
936 | static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset, | |
937 | int len, unsigned long sum) | |
938 | { | |
1a028e50 | 939 | int start = skb_headlen(skb); |
c32ba3f9 | 940 | struct sk_buff *frag_iter; |
1da177e4 LT |
941 | int i, copy; |
942 | ||
943 | /* checksum stuff in header space */ | |
1a028e50 | 944 | if ( (copy = start - offset) > 0) { |
1da177e4 LT |
945 | if (copy > len) |
946 | copy = len; | |
947 | sum = atalk_sum_partial(skb->data + offset, copy, sum); | |
ed4477b9 | 948 | if ( (len -= copy) == 0) |
1da177e4 LT |
949 | return sum; |
950 | ||
951 | offset += copy; | |
952 | } | |
953 | ||
954 | /* checksum stuff in frags */ | |
955 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1a028e50 | 956 | int end; |
1da177e4 | 957 | |
547b792c | 958 | WARN_ON(start > offset + len); |
1a028e50 DM |
959 | |
960 | end = start + skb_shinfo(skb)->frags[i].size; | |
1da177e4 LT |
961 | if ((copy = end - offset) > 0) { |
962 | u8 *vaddr; | |
963 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
964 | ||
965 | if (copy > len) | |
966 | copy = len; | |
967 | vaddr = kmap_skb_frag(frag); | |
1a028e50 DM |
968 | sum = atalk_sum_partial(vaddr + frag->page_offset + |
969 | offset - start, copy, sum); | |
1da177e4 LT |
970 | kunmap_skb_frag(vaddr); |
971 | ||
972 | if (!(len -= copy)) | |
973 | return sum; | |
974 | offset += copy; | |
975 | } | |
1a028e50 | 976 | start = end; |
1da177e4 LT |
977 | } |
978 | ||
c32ba3f9 DM |
979 | skb_walk_frags(skb, frag_iter) { |
980 | int end; | |
1a028e50 | 981 | |
c32ba3f9 | 982 | WARN_ON(start > offset + len); |
1da177e4 | 983 | |
c32ba3f9 DM |
984 | end = start + frag_iter->len; |
985 | if ((copy = end - offset) > 0) { | |
986 | if (copy > len) | |
987 | copy = len; | |
988 | sum = atalk_sum_skb(frag_iter, offset - start, | |
989 | copy, sum); | |
990 | if ((len -= copy) == 0) | |
991 | return sum; | |
992 | offset += copy; | |
1da177e4 | 993 | } |
c32ba3f9 | 994 | start = end; |
1da177e4 LT |
995 | } |
996 | ||
997 | BUG_ON(len > 0); | |
998 | ||
999 | return sum; | |
1000 | } | |
1001 | ||
2a50f28c | 1002 | static __be16 atalk_checksum(const struct sk_buff *skb, int len) |
1da177e4 LT |
1003 | { |
1004 | unsigned long sum; | |
1005 | ||
1006 | /* skip header 4 bytes */ | |
1007 | sum = atalk_sum_skb(skb, 4, len-4, 0); | |
1008 | ||
1009 | /* Use 0xFFFF for 0. 0 itself means none */ | |
2a50f28c | 1010 | return sum ? htons((unsigned short)sum) : htons(0xFFFF); |
1da177e4 LT |
1011 | } |
1012 | ||
1013 | static struct proto ddp_proto = { | |
1014 | .name = "DDP", | |
1015 | .owner = THIS_MODULE, | |
1016 | .obj_size = sizeof(struct atalk_sock), | |
1017 | }; | |
1018 | ||
1019 | /* | |
1020 | * Create a socket. Initialise the socket, blank the addresses | |
1021 | * set the state. | |
1022 | */ | |
1b8d7ae4 | 1023 | static int atalk_create(struct net *net, struct socket *sock, int protocol) |
1da177e4 LT |
1024 | { |
1025 | struct sock *sk; | |
1026 | int rc = -ESOCKTNOSUPPORT; | |
1027 | ||
1b8d7ae4 EB |
1028 | if (net != &init_net) |
1029 | return -EAFNOSUPPORT; | |
1030 | ||
1da177e4 LT |
1031 | /* |
1032 | * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do | |
ed4477b9 | 1033 | * and gives you the full ELAP frame. Should be handy for CAP 8) |
1da177e4 LT |
1034 | */ |
1035 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) | |
1036 | goto out; | |
1037 | rc = -ENOMEM; | |
6257ff21 | 1038 | sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto); |
1da177e4 LT |
1039 | if (!sk) |
1040 | goto out; | |
1041 | rc = 0; | |
1042 | sock->ops = &atalk_dgram_ops; | |
1043 | sock_init_data(sock, sk); | |
1044 | ||
1045 | /* Checksums on by default */ | |
1046 | sock_set_flag(sk, SOCK_ZAPPED); | |
1047 | out: | |
1048 | return rc; | |
1049 | } | |
1050 | ||
1051 | /* Free a socket. No work needed */ | |
1052 | static int atalk_release(struct socket *sock) | |
1053 | { | |
1054 | struct sock *sk = sock->sk; | |
1055 | ||
1056 | if (sk) { | |
1057 | sock_orphan(sk); | |
1058 | sock->sk = NULL; | |
1059 | atalk_destroy_socket(sk); | |
1060 | } | |
1061 | return 0; | |
1062 | } | |
1063 | ||
1064 | /** | |
1065 | * atalk_pick_and_bind_port - Pick a source port when one is not given | |
1066 | * @sk - socket to insert into the tables | |
1067 | * @sat - address to search for | |
1068 | * | |
1069 | * Pick a source port when one is not given. If we can find a suitable free | |
1070 | * one, we insert the socket into the tables using it. | |
1071 | * | |
1072 | * This whole operation must be atomic. | |
1073 | */ | |
1074 | static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat) | |
1075 | { | |
1076 | int retval; | |
1077 | ||
1078 | write_lock_bh(&atalk_sockets_lock); | |
1079 | ||
1080 | for (sat->sat_port = ATPORT_RESERVED; | |
1081 | sat->sat_port < ATPORT_LAST; | |
1082 | sat->sat_port++) { | |
1083 | struct sock *s; | |
1084 | struct hlist_node *node; | |
1085 | ||
1086 | sk_for_each(s, node, &atalk_sockets) { | |
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); | |
1136 | ||
1137 | if (!sock_flag(sk, SOCK_ZAPPED) || | |
1138 | addr_len != sizeof(struct sockaddr_at)) | |
1139 | return -EINVAL; | |
1140 | ||
1141 | if (addr->sat_family != AF_APPLETALK) | |
1142 | return -EAFNOSUPPORT; | |
1143 | ||
1144 | if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) { | |
1145 | struct atalk_addr *ap = atalk_find_primary(); | |
1146 | ||
1147 | if (!ap) | |
1148 | return -EADDRNOTAVAIL; | |
1149 | ||
1150 | at->src_net = addr->sat_addr.s_net = ap->s_net; | |
1151 | at->src_node = addr->sat_addr.s_node= ap->s_node; | |
1152 | } else { | |
1153 | if (!atalk_find_interface(addr->sat_addr.s_net, | |
1154 | addr->sat_addr.s_node)) | |
1155 | return -EADDRNOTAVAIL; | |
1156 | ||
1157 | at->src_net = addr->sat_addr.s_net; | |
1158 | at->src_node = addr->sat_addr.s_node; | |
1159 | } | |
1160 | ||
1161 | if (addr->sat_port == ATADDR_ANYPORT) { | |
1162 | int n = atalk_pick_and_bind_port(sk, addr); | |
1163 | ||
1164 | if (n < 0) | |
1165 | return n; | |
1166 | } else { | |
1167 | at->src_port = addr->sat_port; | |
1168 | ||
1169 | if (atalk_find_or_insert_socket(sk, addr)) | |
1170 | return -EADDRINUSE; | |
1171 | } | |
1172 | ||
1173 | sock_reset_flag(sk, SOCK_ZAPPED); | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | /* Set the address we talk to */ | |
1178 | static int atalk_connect(struct socket *sock, struct sockaddr *uaddr, | |
1179 | int addr_len, int flags) | |
1180 | { | |
1181 | struct sock *sk = sock->sk; | |
1182 | struct atalk_sock *at = at_sk(sk); | |
1183 | struct sockaddr_at *addr; | |
1184 | ||
1185 | sk->sk_state = TCP_CLOSE; | |
1186 | sock->state = SS_UNCONNECTED; | |
1187 | ||
1188 | if (addr_len != sizeof(*addr)) | |
1189 | return -EINVAL; | |
1190 | ||
1191 | addr = (struct sockaddr_at *)uaddr; | |
1192 | ||
1193 | if (addr->sat_family != AF_APPLETALK) | |
1194 | return -EAFNOSUPPORT; | |
1195 | ||
1196 | if (addr->sat_addr.s_node == ATADDR_BCAST && | |
1197 | !sock_flag(sk, SOCK_BROADCAST)) { | |
ed4477b9 | 1198 | #if 1 |
1da177e4 LT |
1199 | printk(KERN_WARNING "%s is broken and did not set " |
1200 | "SO_BROADCAST. It will break when 2.2 is " | |
1201 | "released.\n", | |
1202 | current->comm); | |
1203 | #else | |
1204 | return -EACCES; | |
ed4477b9 | 1205 | #endif |
1da177e4 LT |
1206 | } |
1207 | ||
1208 | if (sock_flag(sk, SOCK_ZAPPED)) | |
1209 | if (atalk_autobind(sk) < 0) | |
1210 | return -EBUSY; | |
1211 | ||
1212 | if (!atrtr_get_dev(&addr->sat_addr)) | |
1213 | return -ENETUNREACH; | |
1214 | ||
1215 | at->dest_port = addr->sat_port; | |
1216 | at->dest_net = addr->sat_addr.s_net; | |
1217 | at->dest_node = addr->sat_addr.s_node; | |
1218 | ||
1219 | sock->state = SS_CONNECTED; | |
1220 | sk->sk_state = TCP_ESTABLISHED; | |
1221 | return 0; | |
1222 | } | |
1223 | ||
1224 | /* | |
1225 | * Find the name of an AppleTalk socket. Just copy the right | |
1226 | * fields into the sockaddr. | |
1227 | */ | |
1228 | static int atalk_getname(struct socket *sock, struct sockaddr *uaddr, | |
1229 | int *uaddr_len, int peer) | |
1230 | { | |
1231 | struct sockaddr_at sat; | |
1232 | struct sock *sk = sock->sk; | |
1233 | struct atalk_sock *at = at_sk(sk); | |
1234 | ||
1235 | if (sock_flag(sk, SOCK_ZAPPED)) | |
1236 | if (atalk_autobind(sk) < 0) | |
1237 | return -ENOBUFS; | |
1238 | ||
1239 | *uaddr_len = sizeof(struct sockaddr_at); | |
1240 | ||
1241 | if (peer) { | |
1242 | if (sk->sk_state != TCP_ESTABLISHED) | |
1243 | return -ENOTCONN; | |
1244 | ||
1245 | sat.sat_addr.s_net = at->dest_net; | |
1246 | sat.sat_addr.s_node = at->dest_node; | |
1247 | sat.sat_port = at->dest_port; | |
1248 | } else { | |
1249 | sat.sat_addr.s_net = at->src_net; | |
1250 | sat.sat_addr.s_node = at->src_node; | |
1251 | sat.sat_port = at->src_port; | |
1252 | } | |
1253 | ||
1254 | sat.sat_family = AF_APPLETALK; | |
1255 | memcpy(uaddr, &sat, sizeof(sat)); | |
1256 | return 0; | |
1257 | } | |
1258 | ||
1259 | #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE) | |
1260 | static __inline__ int is_ip_over_ddp(struct sk_buff *skb) | |
1261 | { | |
ed4477b9 | 1262 | return skb->data[12] == 22; |
1da177e4 LT |
1263 | } |
1264 | ||
1265 | static int handle_ip_over_ddp(struct sk_buff *skb) | |
1266 | { | |
881d966b | 1267 | struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0"); |
1da177e4 LT |
1268 | struct net_device_stats *stats; |
1269 | ||
1270 | /* This needs to be able to handle ipddp"N" devices */ | |
ed4477b9 YH |
1271 | if (!dev) |
1272 | return -ENODEV; | |
1da177e4 | 1273 | |
ed4477b9 YH |
1274 | skb->protocol = htons(ETH_P_IP); |
1275 | skb_pull(skb, 13); | |
1276 | skb->dev = dev; | |
badff6d0 | 1277 | skb_reset_transport_header(skb); |
1da177e4 | 1278 | |
524ad0a7 | 1279 | stats = netdev_priv(dev); |
ed4477b9 YH |
1280 | stats->rx_packets++; |
1281 | stats->rx_bytes += skb->len + 13; | |
1282 | netif_rx(skb); /* Send the SKB up to a higher place. */ | |
1da177e4 LT |
1283 | return 0; |
1284 | } | |
1285 | #else | |
1286 | /* make it easy for gcc to optimize this test out, i.e. kill the code */ | |
1287 | #define is_ip_over_ddp(skb) 0 | |
1288 | #define handle_ip_over_ddp(skb) 0 | |
1289 | #endif | |
1290 | ||
1291 | static void atalk_route_packet(struct sk_buff *skb, struct net_device *dev, | |
2a50f28c | 1292 | struct ddpehdr *ddp, __u16 len_hops, |
1da177e4 LT |
1293 | int origlen) |
1294 | { | |
1295 | struct atalk_route *rt; | |
1296 | struct atalk_addr ta; | |
1297 | ||
1298 | /* | |
1299 | * Don't route multicast, etc., packets, or packets sent to "this | |
ed4477b9 | 1300 | * network" |
1da177e4 LT |
1301 | */ |
1302 | if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) { | |
1303 | /* | |
1304 | * FIXME: | |
1305 | * | |
1306 | * Can it ever happen that a packet is from a PPP iface and | |
1307 | * needs to be broadcast onto the default network? | |
1308 | */ | |
1309 | if (dev->type == ARPHRD_PPP) | |
1310 | printk(KERN_DEBUG "AppleTalk: didn't forward broadcast " | |
1311 | "packet received from PPP iface\n"); | |
1312 | goto free_it; | |
1313 | } | |
1314 | ||
1315 | ta.s_net = ddp->deh_dnet; | |
1316 | ta.s_node = ddp->deh_dnode; | |
1317 | ||
1318 | /* Route the packet */ | |
1319 | rt = atrtr_find(&ta); | |
2a50f28c AV |
1320 | /* increment hops count */ |
1321 | len_hops += 1 << 10; | |
1322 | if (!rt || !(len_hops & (15 << 10))) | |
1da177e4 | 1323 | goto free_it; |
2a50f28c | 1324 | |
1da177e4 | 1325 | /* FIXME: use skb->cb to be able to use shared skbs */ |
1da177e4 LT |
1326 | |
1327 | /* | |
1328 | * Route goes through another gateway, so set the target to the | |
1329 | * gateway instead. | |
1330 | */ | |
1331 | ||
1332 | if (rt->flags & RTF_GATEWAY) { | |
1333 | ta.s_net = rt->gateway.s_net; | |
1334 | ta.s_node = rt->gateway.s_node; | |
1335 | } | |
1336 | ||
ed4477b9 YH |
1337 | /* Fix up skb->len field */ |
1338 | skb_trim(skb, min_t(unsigned int, origlen, | |
1da177e4 | 1339 | (rt->dev->hard_header_len + |
2a50f28c | 1340 | ddp_dl->header_length + (len_hops & 1023)))); |
1da177e4 | 1341 | |
1da177e4 | 1342 | /* FIXME: use skb->cb to be able to use shared skbs */ |
2a50f28c | 1343 | ddp->deh_len_hops = htons(len_hops); |
1da177e4 LT |
1344 | |
1345 | /* | |
1346 | * Send the buffer onwards | |
1347 | * | |
1348 | * Now we must always be careful. If it's come from LocalTalk to | |
1349 | * EtherTalk it might not fit | |
1350 | * | |
1351 | * Order matters here: If a packet has to be copied to make a new | |
1352 | * headroom (rare hopefully) then it won't need unsharing. | |
1353 | * | |
1354 | * Note. ddp-> becomes invalid at the realloc. | |
1355 | */ | |
1356 | if (skb_headroom(skb) < 22) { | |
1357 | /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */ | |
1358 | struct sk_buff *nskb = skb_realloc_headroom(skb, 32); | |
1359 | kfree_skb(skb); | |
ed4477b9 | 1360 | if (!nskb) |
1da177e4 LT |
1361 | goto out; |
1362 | skb = nskb; | |
1363 | } else | |
1364 | skb = skb_unshare(skb, GFP_ATOMIC); | |
ed4477b9 | 1365 | |
1da177e4 LT |
1366 | /* |
1367 | * If the buffer didn't vanish into the lack of space bitbucket we can | |
1368 | * send it. | |
1369 | */ | |
1370 | if (skb && aarp_send_ddp(rt->dev, skb, &ta, NULL) == -1) | |
1371 | goto free_it; | |
1372 | out: | |
1373 | return; | |
1374 | free_it: | |
1375 | kfree_skb(skb); | |
1376 | } | |
1377 | ||
1378 | /** | |
1379 | * atalk_rcv - Receive a packet (in skb) from device dev | |
1380 | * @skb - packet received | |
1381 | * @dev - network device where the packet comes from | |
1382 | * @pt - packet type | |
1383 | * | |
1384 | * Receive a packet (in skb) from device dev. This has come from the SNAP | |
b0e380b1 ACM |
1385 | * decoder, and on entry skb->transport_header is the DDP header, skb->len |
1386 | * is the DDP header, skb->len is the DDP length. The physical headers | |
1387 | * have been extracted. PPP should probably pass frames marked as for this | |
1388 | * layer. [ie ARPHRD_ETHERTALK] | |
1da177e4 LT |
1389 | */ |
1390 | static int atalk_rcv(struct sk_buff *skb, struct net_device *dev, | |
f2ccd8fa | 1391 | struct packet_type *pt, struct net_device *orig_dev) |
1da177e4 LT |
1392 | { |
1393 | struct ddpehdr *ddp; | |
1394 | struct sock *sock; | |
1395 | struct atalk_iface *atif; | |
1396 | struct sockaddr_at tosat; | |
ed4477b9 | 1397 | int origlen; |
2a50f28c | 1398 | __u16 len_hops; |
1da177e4 | 1399 | |
721499e8 | 1400 | if (!net_eq(dev_net(dev), &init_net)) |
e730c155 EB |
1401 | goto freeit; |
1402 | ||
1da177e4 | 1403 | /* Don't mangle buffer if shared */ |
ed4477b9 | 1404 | if (!(skb = skb_share_check(skb, GFP_ATOMIC))) |
1da177e4 | 1405 | goto out; |
ed4477b9 | 1406 | |
1da177e4 LT |
1407 | /* Size check and make sure header is contiguous */ |
1408 | if (!pskb_may_pull(skb, sizeof(*ddp))) | |
1409 | goto freeit; | |
1410 | ||
1411 | ddp = ddp_hdr(skb); | |
1412 | ||
2a50f28c | 1413 | len_hops = ntohs(ddp->deh_len_hops); |
1da177e4 LT |
1414 | |
1415 | /* Trim buffer in case of stray trailing data */ | |
1416 | origlen = skb->len; | |
2a50f28c | 1417 | skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023)); |
1da177e4 LT |
1418 | |
1419 | /* | |
1420 | * Size check to see if ddp->deh_len was crap | |
1421 | * (Otherwise we'll detonate most spectacularly | |
75559c16 | 1422 | * in the middle of atalk_checksum() or recvmsg()). |
1da177e4 | 1423 | */ |
75559c16 JD |
1424 | if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) { |
1425 | pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, " | |
1426 | "skb->len=%u)\n", len_hops & 1023, skb->len); | |
1da177e4 | 1427 | goto freeit; |
75559c16 | 1428 | } |
1da177e4 LT |
1429 | |
1430 | /* | |
1431 | * Any checksums. Note we don't do htons() on this == is assumed to be | |
1432 | * valid for net byte orders all over the networking code... | |
1433 | */ | |
1434 | if (ddp->deh_sum && | |
2a50f28c | 1435 | atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum) |
1da177e4 LT |
1436 | /* Not a valid AppleTalk frame - dustbin time */ |
1437 | goto freeit; | |
1438 | ||
1439 | /* Check the packet is aimed at us */ | |
1440 | if (!ddp->deh_dnet) /* Net 0 is 'this network' */ | |
1441 | atif = atalk_find_anynet(ddp->deh_dnode, dev); | |
1442 | else | |
1443 | atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode); | |
1444 | ||
1da177e4 | 1445 | if (!atif) { |
64233bff OD |
1446 | /* Not ours, so we route the packet via the correct |
1447 | * AppleTalk iface | |
1448 | */ | |
2a50f28c | 1449 | atalk_route_packet(skb, dev, ddp, len_hops, origlen); |
1da177e4 LT |
1450 | goto out; |
1451 | } | |
1452 | ||
1453 | /* if IP over DDP is not selected this code will be optimized out */ | |
1454 | if (is_ip_over_ddp(skb)) | |
1455 | return handle_ip_over_ddp(skb); | |
1456 | /* | |
1457 | * Which socket - atalk_search_socket() looks for a *full match* | |
1458 | * of the <net, node, port> tuple. | |
1459 | */ | |
1460 | tosat.sat_addr.s_net = ddp->deh_dnet; | |
1461 | tosat.sat_addr.s_node = ddp->deh_dnode; | |
1462 | tosat.sat_port = ddp->deh_dport; | |
1463 | ||
1464 | sock = atalk_search_socket(&tosat, atif); | |
1465 | if (!sock) /* But not one of our sockets */ | |
1466 | goto freeit; | |
1467 | ||
1468 | /* Queue packet (standard) */ | |
1469 | skb->sk = sock; | |
1470 | ||
1471 | if (sock_queue_rcv_skb(sock, skb) < 0) | |
1472 | goto freeit; | |
1473 | out: | |
1474 | return 0; | |
1475 | freeit: | |
1476 | kfree_skb(skb); | |
1477 | goto out; | |
1478 | } | |
1479 | ||
1480 | /* | |
1481 | * Receive a LocalTalk frame. We make some demands on the caller here. | |
1482 | * Caller must provide enough headroom on the packet to pull the short | |
1483 | * header and append a long one. | |
1484 | */ | |
1485 | static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev, | |
f2ccd8fa | 1486 | struct packet_type *pt, struct net_device *orig_dev) |
1da177e4 | 1487 | { |
721499e8 | 1488 | if (!net_eq(dev_net(dev), &init_net)) |
e730c155 EB |
1489 | goto freeit; |
1490 | ||
1da177e4 | 1491 | /* Expand any short form frames */ |
98e399f8 | 1492 | if (skb_mac_header(skb)[2] == 1) { |
1da177e4 LT |
1493 | struct ddpehdr *ddp; |
1494 | /* Find our address */ | |
1495 | struct atalk_addr *ap = atalk_find_dev_addr(dev); | |
1496 | ||
2a50f28c | 1497 | if (!ap || skb->len < sizeof(__be16) || skb->len > 1023) |
1da177e4 LT |
1498 | goto freeit; |
1499 | ||
1500 | /* Don't mangle buffer if shared */ | |
ed4477b9 | 1501 | if (!(skb = skb_share_check(skb, GFP_ATOMIC))) |
1da177e4 LT |
1502 | return 0; |
1503 | ||
1504 | /* | |
1505 | * The push leaves us with a ddephdr not an shdr, and | |
1506 | * handily the port bytes in the right place preset. | |
1507 | */ | |
1508 | ddp = (struct ddpehdr *) skb_push(skb, sizeof(*ddp) - 4); | |
1509 | ||
1510 | /* Now fill in the long header */ | |
1511 | ||
ed4477b9 YH |
1512 | /* |
1513 | * These two first. The mac overlays the new source/dest | |
1514 | * network information so we MUST copy these before | |
1515 | * we write the network numbers ! | |
1516 | */ | |
1da177e4 | 1517 | |
98e399f8 ACM |
1518 | ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */ |
1519 | ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */ | |
1da177e4 LT |
1520 | |
1521 | ddp->deh_dnet = ap->s_net; /* Network number */ | |
1522 | ddp->deh_snet = ap->s_net; | |
1523 | ddp->deh_sum = 0; /* No checksum */ | |
1524 | /* | |
1525 | * Not sure about this bit... | |
1526 | */ | |
2a50f28c AV |
1527 | /* Non routable, so force a drop if we slip up later */ |
1528 | ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10)); | |
1da177e4 | 1529 | } |
badff6d0 | 1530 | skb_reset_transport_header(skb); |
1da177e4 | 1531 | |
f2ccd8fa | 1532 | return atalk_rcv(skb, dev, pt, orig_dev); |
1da177e4 LT |
1533 | freeit: |
1534 | kfree_skb(skb); | |
1535 | return 0; | |
1536 | } | |
1537 | ||
1538 | static int atalk_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, | |
1539 | size_t len) | |
1540 | { | |
1541 | struct sock *sk = sock->sk; | |
1542 | struct atalk_sock *at = at_sk(sk); | |
1543 | struct sockaddr_at *usat = (struct sockaddr_at *)msg->msg_name; | |
1544 | int flags = msg->msg_flags; | |
1545 | int loopback = 0; | |
1546 | struct sockaddr_at local_satalk, gsat; | |
1547 | struct sk_buff *skb; | |
1548 | struct net_device *dev; | |
1549 | struct ddpehdr *ddp; | |
1550 | int size; | |
1551 | struct atalk_route *rt; | |
1552 | int err; | |
1553 | ||
1554 | if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) | |
1555 | return -EINVAL; | |
1556 | ||
1557 | if (len > DDP_MAXSZ) | |
1558 | return -EMSGSIZE; | |
1559 | ||
1560 | if (usat) { | |
1561 | if (sock_flag(sk, SOCK_ZAPPED)) | |
1562 | if (atalk_autobind(sk) < 0) | |
1563 | return -EBUSY; | |
1564 | ||
1565 | if (msg->msg_namelen < sizeof(*usat) || | |
1566 | usat->sat_family != AF_APPLETALK) | |
1567 | return -EINVAL; | |
1568 | ||
03ba9991 | 1569 | /* netatalk didn't implement this check */ |
1da177e4 LT |
1570 | if (usat->sat_addr.s_node == ATADDR_BCAST && |
1571 | !sock_flag(sk, SOCK_BROADCAST)) { | |
1da177e4 | 1572 | return -EPERM; |
1da177e4 LT |
1573 | } |
1574 | } else { | |
1575 | if (sk->sk_state != TCP_ESTABLISHED) | |
1576 | return -ENOTCONN; | |
1577 | usat = &local_satalk; | |
1578 | usat->sat_family = AF_APPLETALK; | |
1579 | usat->sat_port = at->dest_port; | |
1580 | usat->sat_addr.s_node = at->dest_node; | |
1581 | usat->sat_addr.s_net = at->dest_net; | |
1582 | } | |
1583 | ||
1584 | /* Build a packet */ | |
1585 | SOCK_DEBUG(sk, "SK %p: Got address.\n", sk); | |
1586 | ||
1587 | /* For headers */ | |
1588 | size = sizeof(struct ddpehdr) + len + ddp_dl->header_length; | |
1589 | ||
1590 | if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) { | |
1591 | rt = atrtr_find(&usat->sat_addr); | |
1da177e4 LT |
1592 | } else { |
1593 | struct atalk_addr at_hint; | |
1594 | ||
1595 | at_hint.s_node = 0; | |
1596 | at_hint.s_net = at->src_net; | |
1597 | ||
1598 | rt = atrtr_find(&at_hint); | |
1da177e4 | 1599 | } |
64233bff OD |
1600 | if (!rt) |
1601 | return -ENETUNREACH; | |
1602 | ||
1603 | dev = rt->dev; | |
1da177e4 LT |
1604 | |
1605 | SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n", | |
1606 | sk, size, dev->name); | |
1607 | ||
1608 | size += dev->hard_header_len; | |
1609 | skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err); | |
1610 | if (!skb) | |
1611 | return err; | |
ed4477b9 | 1612 | |
1da177e4 LT |
1613 | skb->sk = sk; |
1614 | skb_reserve(skb, ddp_dl->header_length); | |
1615 | skb_reserve(skb, dev->hard_header_len); | |
1616 | skb->dev = dev; | |
1617 | ||
1618 | SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk); | |
1619 | ||
1620 | ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr)); | |
2a50f28c | 1621 | ddp->deh_len_hops = htons(len + sizeof(*ddp)); |
1da177e4 LT |
1622 | ddp->deh_dnet = usat->sat_addr.s_net; |
1623 | ddp->deh_snet = at->src_net; | |
1624 | ddp->deh_dnode = usat->sat_addr.s_node; | |
1625 | ddp->deh_snode = at->src_node; | |
1626 | ddp->deh_dport = usat->sat_port; | |
1627 | ddp->deh_sport = at->src_port; | |
1628 | ||
1629 | SOCK_DEBUG(sk, "SK %p: Copy user data (%Zd bytes).\n", sk, len); | |
1630 | ||
1631 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); | |
1632 | if (err) { | |
1633 | kfree_skb(skb); | |
1634 | return -EFAULT; | |
1635 | } | |
1636 | ||
1637 | if (sk->sk_no_check == 1) | |
1638 | ddp->deh_sum = 0; | |
1639 | else | |
1640 | ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp)); | |
1641 | ||
1642 | /* | |
1643 | * Loopback broadcast packets to non gateway targets (ie routes | |
1644 | * to group we are in) | |
1645 | */ | |
1646 | if (ddp->deh_dnode == ATADDR_BCAST && | |
1647 | !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) { | |
1648 | struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL); | |
1649 | ||
1650 | if (skb2) { | |
1651 | loopback = 1; | |
1652 | SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk); | |
1653 | if (aarp_send_ddp(dev, skb2, | |
1654 | &usat->sat_addr, NULL) == -1) | |
1655 | kfree_skb(skb2); | |
1656 | /* else queued/sent above in the aarp queue */ | |
1657 | } | |
1658 | } | |
1659 | ||
1660 | if (dev->flags & IFF_LOOPBACK || loopback) { | |
1661 | SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk); | |
1662 | /* loop back */ | |
1663 | skb_orphan(skb); | |
64233bff OD |
1664 | if (ddp->deh_dnode == ATADDR_BCAST) { |
1665 | struct atalk_addr at_lo; | |
1666 | ||
1667 | at_lo.s_node = 0; | |
1668 | at_lo.s_net = 0; | |
1669 | ||
1670 | rt = atrtr_find(&at_lo); | |
1671 | if (!rt) { | |
1672 | kfree_skb(skb); | |
1673 | return -ENETUNREACH; | |
1674 | } | |
1675 | dev = rt->dev; | |
1676 | skb->dev = dev; | |
1677 | } | |
1da177e4 LT |
1678 | ddp_dl->request(ddp_dl, skb, dev->dev_addr); |
1679 | } else { | |
1680 | SOCK_DEBUG(sk, "SK %p: send out.\n", sk); | |
1681 | if (rt->flags & RTF_GATEWAY) { | |
1682 | gsat.sat_addr = rt->gateway; | |
1683 | usat = &gsat; | |
1684 | } | |
1685 | ||
1686 | if (aarp_send_ddp(dev, skb, &usat->sat_addr, NULL) == -1) | |
1687 | kfree_skb(skb); | |
1688 | /* else queued/sent above in the aarp queue */ | |
1689 | } | |
1690 | SOCK_DEBUG(sk, "SK %p: Done write (%Zd).\n", sk, len); | |
1691 | ||
1692 | return len; | |
1693 | } | |
1694 | ||
1695 | static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, | |
1696 | size_t size, int flags) | |
1697 | { | |
1698 | struct sock *sk = sock->sk; | |
1699 | struct sockaddr_at *sat = (struct sockaddr_at *)msg->msg_name; | |
1700 | struct ddpehdr *ddp; | |
1701 | int copied = 0; | |
2a50f28c | 1702 | int offset = 0; |
1da177e4 | 1703 | int err = 0; |
1da177e4 LT |
1704 | struct sk_buff *skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, |
1705 | flags & MSG_DONTWAIT, &err); | |
1706 | if (!skb) | |
1707 | return err; | |
1708 | ||
1709 | /* FIXME: use skb->cb to be able to use shared skbs */ | |
1710 | ddp = ddp_hdr(skb); | |
2a50f28c | 1711 | copied = ntohs(ddp->deh_len_hops) & 1023; |
1da177e4 | 1712 | |
2a50f28c AV |
1713 | if (sk->sk_type != SOCK_RAW) { |
1714 | offset = sizeof(*ddp); | |
1715 | copied -= offset; | |
1716 | } | |
1da177e4 | 1717 | |
2a50f28c AV |
1718 | if (copied > size) { |
1719 | copied = size; | |
1720 | msg->msg_flags |= MSG_TRUNC; | |
1da177e4 | 1721 | } |
2a50f28c | 1722 | err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied); |
1da177e4 LT |
1723 | |
1724 | if (!err) { | |
1725 | if (sat) { | |
1726 | sat->sat_family = AF_APPLETALK; | |
1727 | sat->sat_port = ddp->deh_sport; | |
1728 | sat->sat_addr.s_node = ddp->deh_snode; | |
1729 | sat->sat_addr.s_net = ddp->deh_snet; | |
1730 | } | |
1731 | msg->msg_namelen = sizeof(*sat); | |
1732 | } | |
1733 | ||
1734 | skb_free_datagram(sk, skb); /* Free the datagram. */ | |
1735 | return err ? : copied; | |
1736 | } | |
1737 | ||
1738 | ||
1739 | /* | |
1740 | * AppleTalk ioctl calls. | |
1741 | */ | |
1742 | static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) | |
1743 | { | |
b5e5fa5e | 1744 | int rc = -ENOIOCTLCMD; |
1da177e4 LT |
1745 | struct sock *sk = sock->sk; |
1746 | void __user *argp = (void __user *)arg; | |
1747 | ||
1748 | switch (cmd) { | |
1749 | /* Protocol layer */ | |
1750 | case TIOCOUTQ: { | |
31e6d363 | 1751 | long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
1da177e4 LT |
1752 | |
1753 | if (amount < 0) | |
1754 | amount = 0; | |
1755 | rc = put_user(amount, (int __user *)argp); | |
1756 | break; | |
1757 | } | |
1758 | case TIOCINQ: { | |
1759 | /* | |
1760 | * These two are safe on a single CPU system as only | |
1761 | * user tasks fiddle here | |
1762 | */ | |
1763 | struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); | |
1764 | long amount = 0; | |
1765 | ||
1766 | if (skb) | |
1767 | amount = skb->len - sizeof(struct ddpehdr); | |
1768 | rc = put_user(amount, (int __user *)argp); | |
1769 | break; | |
1770 | } | |
1771 | case SIOCGSTAMP: | |
1772 | rc = sock_get_timestamp(sk, argp); | |
1773 | break; | |
ae40eb1e ED |
1774 | case SIOCGSTAMPNS: |
1775 | rc = sock_get_timestampns(sk, argp); | |
1776 | break; | |
1da177e4 LT |
1777 | /* Routing */ |
1778 | case SIOCADDRT: | |
1779 | case SIOCDELRT: | |
1780 | rc = -EPERM; | |
1781 | if (capable(CAP_NET_ADMIN)) | |
1782 | rc = atrtr_ioctl(cmd, argp); | |
1783 | break; | |
1784 | /* Interface */ | |
1785 | case SIOCGIFADDR: | |
1786 | case SIOCSIFADDR: | |
1787 | case SIOCGIFBRDADDR: | |
1788 | case SIOCATALKDIFADDR: | |
1789 | case SIOCDIFADDR: | |
1790 | case SIOCSARP: /* proxy AARP */ | |
1791 | case SIOCDARP: /* proxy AARP */ | |
1792 | rtnl_lock(); | |
1793 | rc = atif_ioctl(cmd, argp); | |
1794 | rtnl_unlock(); | |
1795 | break; | |
1da177e4 LT |
1796 | } |
1797 | ||
1798 | return rc; | |
1799 | } | |
1800 | ||
f6c90b71 PV |
1801 | |
1802 | #ifdef CONFIG_COMPAT | |
1803 | static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) | |
1804 | { | |
1805 | /* | |
1806 | * All Appletalk ioctls except SIOCATALKDIFADDR are standard. And | |
1807 | * SIOCATALKDIFADDR is handled by upper layer as well, so there is | |
1808 | * nothing to do. Eventually SIOCATALKDIFADDR should be moved | |
1809 | * here so there is no generic SIOCPROTOPRIVATE translation in the | |
1810 | * system. | |
1811 | */ | |
1812 | return -ENOIOCTLCMD; | |
1813 | } | |
1814 | #endif | |
1815 | ||
1816 | ||
1da177e4 LT |
1817 | static struct net_proto_family atalk_family_ops = { |
1818 | .family = PF_APPLETALK, | |
1819 | .create = atalk_create, | |
1820 | .owner = THIS_MODULE, | |
1821 | }; | |
1822 | ||
90ddc4f0 | 1823 | static const struct proto_ops SOCKOPS_WRAPPED(atalk_dgram_ops) = { |
1da177e4 LT |
1824 | .family = PF_APPLETALK, |
1825 | .owner = THIS_MODULE, | |
1826 | .release = atalk_release, | |
1827 | .bind = atalk_bind, | |
1828 | .connect = atalk_connect, | |
1829 | .socketpair = sock_no_socketpair, | |
1830 | .accept = sock_no_accept, | |
1831 | .getname = atalk_getname, | |
1832 | .poll = datagram_poll, | |
1833 | .ioctl = atalk_ioctl, | |
f6c90b71 PV |
1834 | #ifdef CONFIG_COMPAT |
1835 | .compat_ioctl = atalk_compat_ioctl, | |
1836 | #endif | |
1da177e4 LT |
1837 | .listen = sock_no_listen, |
1838 | .shutdown = sock_no_shutdown, | |
1839 | .setsockopt = sock_no_setsockopt, | |
1840 | .getsockopt = sock_no_getsockopt, | |
1841 | .sendmsg = atalk_sendmsg, | |
1842 | .recvmsg = atalk_recvmsg, | |
1843 | .mmap = sock_no_mmap, | |
1844 | .sendpage = sock_no_sendpage, | |
1845 | }; | |
1846 | ||
1da177e4 LT |
1847 | SOCKOPS_WRAP(atalk_dgram, PF_APPLETALK); |
1848 | ||
1849 | static struct notifier_block ddp_notifier = { | |
1850 | .notifier_call = ddp_device_event, | |
1851 | }; | |
1852 | ||
7546dd97 | 1853 | static struct packet_type ltalk_packet_type __read_mostly = { |
09640e63 | 1854 | .type = cpu_to_be16(ETH_P_LOCALTALK), |
1da177e4 LT |
1855 | .func = ltalk_rcv, |
1856 | }; | |
1857 | ||
7546dd97 | 1858 | static struct packet_type ppptalk_packet_type __read_mostly = { |
09640e63 | 1859 | .type = cpu_to_be16(ETH_P_PPPTALK), |
1da177e4 LT |
1860 | .func = atalk_rcv, |
1861 | }; | |
1862 | ||
1863 | static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B }; | |
1864 | ||
1865 | /* Export symbols for use by drivers when AppleTalk is a module */ | |
1866 | EXPORT_SYMBOL(aarp_send_ddp); | |
1867 | EXPORT_SYMBOL(atrtr_get_dev); | |
1868 | EXPORT_SYMBOL(atalk_find_dev_addr); | |
1869 | ||
2db09608 | 1870 | static const char atalk_err_snap[] __initconst = |
1da177e4 LT |
1871 | KERN_CRIT "Unable to register DDP with SNAP.\n"; |
1872 | ||
1873 | /* Called by proto.c on kernel start up */ | |
1874 | static int __init atalk_init(void) | |
1875 | { | |
1876 | int rc = proto_register(&ddp_proto, 0); | |
1877 | ||
1878 | if (rc != 0) | |
1879 | goto out; | |
1880 | ||
1881 | (void)sock_register(&atalk_family_ops); | |
1882 | ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv); | |
1883 | if (!ddp_dl) | |
1884 | printk(atalk_err_snap); | |
1885 | ||
1886 | dev_add_pack(<alk_packet_type); | |
1887 | dev_add_pack(&ppptalk_packet_type); | |
1888 | ||
1889 | register_netdevice_notifier(&ddp_notifier); | |
1890 | aarp_proto_init(); | |
1891 | atalk_proc_init(); | |
1892 | atalk_register_sysctl(); | |
1893 | out: | |
1894 | return rc; | |
1895 | } | |
1896 | module_init(atalk_init); | |
1897 | ||
1898 | /* | |
1899 | * No explicit module reference count manipulation is needed in the | |
1900 | * protocol. Socket layer sets module reference count for us | |
1901 | * and interfaces reference counting is done | |
1902 | * by the network device layer. | |
1903 | * | |
1904 | * Ergo, before the AppleTalk module can be removed, all AppleTalk | |
1905 | * sockets be closed from user space. | |
1906 | */ | |
1907 | static void __exit atalk_exit(void) | |
1908 | { | |
1909 | #ifdef CONFIG_SYSCTL | |
1910 | atalk_unregister_sysctl(); | |
1911 | #endif /* CONFIG_SYSCTL */ | |
1912 | atalk_proc_exit(); | |
1913 | aarp_cleanup_module(); /* General aarp clean-up. */ | |
1914 | unregister_netdevice_notifier(&ddp_notifier); | |
1915 | dev_remove_pack(<alk_packet_type); | |
1916 | dev_remove_pack(&ppptalk_packet_type); | |
1917 | unregister_snap_client(ddp_dl); | |
1918 | sock_unregister(PF_APPLETALK); | |
1919 | proto_unregister(&ddp_proto); | |
1920 | } | |
1921 | module_exit(atalk_exit); | |
1922 | ||
1923 | MODULE_LICENSE("GPL"); | |
113aa838 | 1924 | MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>"); |
1da177e4 LT |
1925 | MODULE_DESCRIPTION("AppleTalk 0.20\n"); |
1926 | MODULE_ALIAS_NETPROTO(PF_APPLETALK); |