blob: 7e717cb35ad15c7437dba106107cf4cebb653a52 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * An implementation of the Acorn Econet and AUN protocols.
3 * Philip Blundell <philb@gnu.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 */
11
Joe Perchesee9c88f2011-07-03 08:28:33 +000012#define pr_fmt(fmt) fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15
16#include <linux/types.h>
17#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/socket.h>
21#include <linux/sockios.h>
22#include <linux/in.h>
23#include <linux/errno.h>
24#include <linux/interrupt.h>
25#include <linux/if_ether.h>
26#include <linux/netdevice.h>
27#include <linux/inetdevice.h>
28#include <linux/route.h>
29#include <linux/inet.h>
30#include <linux/etherdevice.h>
31#include <linux/if_arp.h>
32#include <linux/wireless.h>
33#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020034#include <linux/udp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Phil Blundella27e13d2010-11-24 11:51:47 -080036#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/sock.h>
38#include <net/inet_common.h>
39#include <linux/stat.h>
40#include <linux/init.h>
41#include <linux/if_ec.h>
42#include <net/udp.h>
43#include <net/ip.h>
44#include <linux/spinlock.h>
45#include <linux/rcupdate.h>
46#include <linux/bitops.h>
David S. Miller1d181832006-03-28 00:01:55 -080047#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Joe Perchesee9c88f2011-07-03 08:28:33 +000049#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/system.h>
51
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080052static const struct proto_ops econet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct hlist_head econet_sklist;
Eric Dumazet0c78a922010-06-09 16:33:05 +000054static DEFINE_SPINLOCK(econet_lock);
David S. Miller1d181832006-03-28 00:01:55 -080055static DEFINE_MUTEX(econet_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* Since there are only 256 possible network numbers (or fewer, depends
58 how you count) it makes sense to use a simple lookup table. */
59static struct net_device *net2dev_map[256];
60
61#define EC_PORT_IP 0xd2
62
63#ifdef CONFIG_ECONET_AUNUDP
YOSHIFUJI Hideakica403302006-01-04 13:56:08 -080064static DEFINE_SPINLOCK(aun_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static struct socket *udpsock;
66#define AUN_PORT 0x8000
67
Joe Perchesee9c88f2011-07-03 08:28:33 +000068struct aunhdr {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned char code; /* AUN magic protocol byte */
70 unsigned char port;
71 unsigned char cb;
72 unsigned char pad;
73 unsigned long handle;
74};
75
76static unsigned long aun_seq;
77
78/* Queue of packets waiting to be transmitted. */
79static struct sk_buff_head aun_queue;
80static struct timer_list ab_cleanup_timer;
81
82#endif /* CONFIG_ECONET_AUNUDP */
83
84/* Per-packet information */
Joe Perchesee9c88f2011-07-03 08:28:33 +000085struct ec_cb {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct sockaddr_ec sec;
87 unsigned long cookie; /* Supplied by user. */
88#ifdef CONFIG_ECONET_AUNUDP
89 int done;
90 unsigned long seq; /* Sequencing */
91 unsigned long timeout; /* Timeout */
92 unsigned long start; /* jiffies */
93#endif
94#ifdef CONFIG_ECONET_NATIVE
95 void (*sent)(struct sk_buff *, int result);
96#endif
97};
98
99static void econet_remove_socket(struct hlist_head *list, struct sock *sk)
100{
Eric Dumazet0c78a922010-06-09 16:33:05 +0000101 spin_lock_bh(&econet_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 sk_del_node_init(sk);
Eric Dumazet0c78a922010-06-09 16:33:05 +0000103 spin_unlock_bh(&econet_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static void econet_insert_socket(struct hlist_head *list, struct sock *sk)
107{
Eric Dumazet0c78a922010-06-09 16:33:05 +0000108 spin_lock_bh(&econet_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 sk_add_node(sk, list);
Eric Dumazet0c78a922010-06-09 16:33:05 +0000110 spin_unlock_bh(&econet_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/*
114 * Pull a packet from our receive queue and hand it to the user.
115 * If necessary we block.
116 */
117
118static int econet_recvmsg(struct kiocb *iocb, struct socket *sock,
119 struct msghdr *msg, size_t len, int flags)
120{
121 struct sock *sk = sock->sk;
122 struct sk_buff *skb;
123 size_t copied;
124 int err;
125
126 msg->msg_namelen = sizeof(struct sockaddr_ec);
127
David S. Miller1d181832006-03-28 00:01:55 -0800128 mutex_lock(&econet_mutex);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /*
131 * Call the generic datagram receiver. This handles all sorts
132 * of horrible races and re-entrancy so we can forget about it
133 * in the protocol layers.
134 *
135 * Now it will return ENETDOWN, if device have just gone down,
136 * but then it will block.
137 */
138
Joe Perchesee9c88f2011-07-03 08:28:33 +0000139 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /*
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900142 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * handles the blocking we don't see and worry about blocking
144 * retries.
145 */
146
Joe Perchesee9c88f2011-07-03 08:28:33 +0000147 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto out;
149
150 /*
151 * You lose any data beyond the buffer you gave. If it worries a
152 * user program they can ask the device for its MTU anyway.
153 */
154
155 copied = skb->len;
Joe Perchesee9c88f2011-07-03 08:28:33 +0000156 if (copied > len) {
157 copied = len;
158 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 /* We can't use skb_copy_datagram here */
162 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
163 if (err)
164 goto out_free;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700165 sk->sk_stamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (msg->msg_name)
168 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
169
170 /*
171 * Free or return the buffer as appropriate. Again this
172 * hides all the races and re-entrancy issues from us.
173 */
174 err = copied;
175
176out_free:
177 skb_free_datagram(sk, skb);
178out:
David S. Miller1d181832006-03-28 00:01:55 -0800179 mutex_unlock(&econet_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return err;
181}
182
183/*
184 * Bind an Econet socket.
185 */
186
Joe Perchesee9c88f2011-07-03 08:28:33 +0000187static int econet_bind(struct socket *sock, struct sockaddr *uaddr,
188 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
David S. Miller1d181832006-03-28 00:01:55 -0800191 struct sock *sk;
192 struct econet_sock *eo;
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /*
195 * Check legality
196 */
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (addr_len < sizeof(struct sockaddr_ec) ||
199 sec->sec_family != AF_ECONET)
200 return -EINVAL;
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900201
David S. Miller1d181832006-03-28 00:01:55 -0800202 mutex_lock(&econet_mutex);
203
204 sk = sock->sk;
205 eo = ec_sk(sk);
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 eo->cb = sec->cb;
208 eo->port = sec->port;
209 eo->station = sec->addr.station;
210 eo->net = sec->addr.net;
211
David S. Miller1d181832006-03-28 00:01:55 -0800212 mutex_unlock(&econet_mutex);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215}
216
217#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
218/*
219 * Queue a transmit result for the user to be told about.
220 */
221
222static void tx_result(struct sock *sk, unsigned long cookie, int result)
223{
224 struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
225 struct ec_cb *eb;
226 struct sockaddr_ec *sec;
227
Joe Perchesee9c88f2011-07-03 08:28:33 +0000228 if (skb == NULL) {
229 pr_debug("econet: memory squeeze, transmit result dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return;
231 }
232
233 eb = (struct ec_cb *)&skb->cb;
234 sec = (struct sockaddr_ec *)&eb->sec;
235 memset(sec, 0, sizeof(struct sockaddr_ec));
236 sec->cookie = cookie;
237 sec->type = ECTYPE_TRANSMIT_STATUS | result;
238 sec->sec_family = AF_ECONET;
239
240 if (sock_queue_rcv_skb(sk, skb) < 0)
241 kfree_skb(skb);
242}
243#endif
244
245#ifdef CONFIG_ECONET_NATIVE
246/*
247 * Called by the Econet hardware driver when a packet transmit
248 * has completed. Tell the user.
249 */
250
251static void ec_tx_done(struct sk_buff *skb, int result)
252{
253 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
254 tx_result(skb->sk, eb->cookie, result);
255}
256#endif
257
258/*
259 * Send a packet. We have to work out which device it's going out on
260 * and hence whether to use real Econet or the UDP emulation.
261 */
262
263static int econet_sendmsg(struct kiocb *iocb, struct socket *sock,
264 struct msghdr *msg, size_t len)
265{
Joe Perchesee9c88f2011-07-03 08:28:33 +0000266 struct sockaddr_ec *saddr = (struct sockaddr_ec *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct net_device *dev;
268 struct ec_addr addr;
269 int err;
270 unsigned char port, cb;
271#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
Eric Dumazet389f2a12011-01-26 00:04:18 +0000272 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct sk_buff *skb;
274 struct ec_cb *eb;
275#endif
276#ifdef CONFIG_ECONET_AUNUDP
277 struct msghdr udpmsg;
Phil Blundella27e13d2010-11-24 11:51:47 -0800278 struct iovec iov[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct aunhdr ah;
280 struct sockaddr_in udpdest;
281 __kernel_size_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 mm_segment_t oldfs;
Phil Blundella27e13d2010-11-24 11:51:47 -0800283 char *userbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /*
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900287 * Check the flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900290 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -EINVAL;
292
293 /*
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900294 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900296
David S. Miller1d181832006-03-28 00:01:55 -0800297 mutex_lock(&econet_mutex);
298
Joe Perchesee9c88f2011-07-03 08:28:33 +0000299 if (saddr == NULL || msg->msg_namelen < sizeof(struct sockaddr_ec)) {
300 mutex_unlock(&econet_mutex);
301 return -EINVAL;
302 }
303 addr.station = saddr->addr.station;
304 addr.net = saddr->addr.net;
305 port = saddr->port;
306 cb = saddr->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* Look for a device with the right network number. */
309 dev = net2dev_map[addr.net];
310
311 /* If not directly reachable, use some default */
David S. Miller1d181832006-03-28 00:01:55 -0800312 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 dev = net2dev_map[0];
314 /* No interfaces at all? */
David S. Miller1d181832006-03-28 00:01:55 -0800315 if (dev == NULL) {
316 mutex_unlock(&econet_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -ENETDOWN;
David S. Miller1d181832006-03-28 00:01:55 -0800318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
David S. Miller1d181832006-03-28 00:01:55 -0800321 if (dev->type == ARPHRD_ECONET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Real hardware Econet. We're not worthy etc. */
323#ifdef CONFIG_ECONET_NATIVE
324 unsigned short proto = 0;
Herbert Xuae641942011-11-18 02:20:04 +0000325 int hlen, tlen;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700326 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Phil Blundella27e13d2010-11-24 11:51:47 -0800328 if (len + 15 > dev->mtu) {
329 mutex_unlock(&econet_mutex);
330 return -EMSGSIZE;
331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dev_hold(dev);
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900334
Herbert Xuae641942011-11-18 02:20:04 +0000335 hlen = LL_RESERVED_SPACE(dev);
336 tlen = dev->needed_tailroom;
337 skb = sock_alloc_send_skb(sk, len + hlen + tlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 msg->msg_flags & MSG_DONTWAIT, &err);
Joe Perchesee9c88f2011-07-03 08:28:33 +0000339 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto out_unlock;
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900341
Herbert Xuae641942011-11-18 02:20:04 +0000342 skb_reserve(skb, hlen);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700343 skb_reset_network_header(skb);
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 eb = (struct ec_cb *)&skb->cb;
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 eb->cookie = saddr->cookie;
348 eb->sec = *saddr;
349 eb->sent = ec_tx_done;
350
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700351 err = -EINVAL;
352 res = dev_hard_header(skb, dev, ntohs(proto), &addr, NULL, len);
353 if (res < 0)
354 goto out_free;
355 if (res > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct ec_framehdr *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* Poke in our control byte and
358 port number. Hack, hack. */
Joe Perchesee9c88f2011-07-03 08:28:33 +0000359 fh = (struct ec_framehdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 fh->cb = cb;
361 fh->port = port;
362 if (sock->type != SOCK_DGRAM) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700363 skb_reset_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 skb->len = 0;
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Copy the data. Returns -EFAULT on error */
Joe Perchesee9c88f2011-07-03 08:28:33 +0000369 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 skb->protocol = proto;
371 skb->dev = dev;
372 skb->priority = sk->sk_priority;
373 if (err)
374 goto out_free;
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 err = -ENETDOWN;
377 if (!(dev->flags & IFF_UP))
378 goto out_free;
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
381 * Now send it
382 */
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dev_queue_xmit(skb);
385 dev_put(dev);
David S. Miller1d181832006-03-28 00:01:55 -0800386 mutex_unlock(&econet_mutex);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000387 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Joe Perchesee9c88f2011-07-03 08:28:33 +0000389out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 kfree_skb(skb);
Joe Perchesee9c88f2011-07-03 08:28:33 +0000391out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (dev)
393 dev_put(dev);
394#else
395 err = -EPROTOTYPE;
396#endif
David S. Miller1d181832006-03-28 00:01:55 -0800397 mutex_unlock(&econet_mutex);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return err;
400 }
401
402#ifdef CONFIG_ECONET_AUNUDP
403 /* AUN virtual Econet. */
404
David S. Miller1d181832006-03-28 00:01:55 -0800405 if (udpsock == NULL) {
406 mutex_unlock(&econet_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -ENETDOWN; /* No socket - can't send */
David S. Miller1d181832006-03-28 00:01:55 -0800408 }
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900409
Phil Blundella27e13d2010-11-24 11:51:47 -0800410 if (len > 32768) {
411 err = -E2BIG;
412 goto error;
413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* Make up a UDP datagram and hand it off to some higher intellect. */
416
417 memset(&udpdest, 0, sizeof(udpdest));
418 udpdest.sin_family = AF_INET;
419 udpdest.sin_port = htons(AUN_PORT);
420
421 /* At the moment we use the stupid Acorn scheme of Econet address
422 y.x maps to IP a.b.c.x. This should be replaced with something
423 more flexible and more aware of subnet masks. */
424 {
425 struct in_device *idev;
426 unsigned long network = 0;
427
428 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700429 idev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (idev) {
431 if (idev->ifa_list)
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900432 network = ntohl(idev->ifa_list->ifa_address) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 0xffffff00; /* !!! */
434 }
435 rcu_read_unlock();
436 udpdest.sin_addr.s_addr = htonl(network | addr.station);
437 }
438
Vasiliy Kulikov67c5c6c2011-03-17 01:40:10 +0000439 memset(&ah, 0, sizeof(ah));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ah.port = port;
441 ah.cb = cb & 0x7f;
442 ah.code = 2; /* magic */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* tack our header on the front of the iovec */
445 size = sizeof(struct aunhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 iov[0].iov_base = (void *)&ah;
447 iov[0].iov_len = size;
Phil Blundella27e13d2010-11-24 11:51:47 -0800448
449 userbuf = vmalloc(len);
450 if (userbuf == NULL) {
451 err = -ENOMEM;
452 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
Phil Blundella27e13d2010-11-24 11:51:47 -0800455 iov[1].iov_base = userbuf;
456 iov[1].iov_len = len;
457 err = memcpy_fromiovec(userbuf, msg->msg_iov, len);
458 if (err)
459 goto error_free_buf;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Get a skbuff (no data, just holds our cb information) */
Joe Perchesee9c88f2011-07-03 08:28:33 +0000462 skb = sock_alloc_send_skb(sk, 0, msg->msg_flags & MSG_DONTWAIT, &err);
463 if (skb == NULL)
Phil Blundella27e13d2010-11-24 11:51:47 -0800464 goto error_free_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 eb = (struct ec_cb *)&skb->cb;
467
468 eb->cookie = saddr->cookie;
Joe Perchesee9c88f2011-07-03 08:28:33 +0000469 eb->timeout = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 eb->start = jiffies;
471 ah.handle = aun_seq;
472 eb->seq = (aun_seq++);
473 eb->sec = *saddr;
474
475 skb_queue_tail(&aun_queue, skb);
476
477 udpmsg.msg_name = (void *)&udpdest;
478 udpmsg.msg_namelen = sizeof(udpdest);
479 udpmsg.msg_iov = &iov[0];
Phil Blundella27e13d2010-11-24 11:51:47 -0800480 udpmsg.msg_iovlen = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 udpmsg.msg_control = NULL;
482 udpmsg.msg_controllen = 0;
Joe Perchesee9c88f2011-07-03 08:28:33 +0000483 udpmsg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Joe Perchesee9c88f2011-07-03 08:28:33 +0000485 oldfs = get_fs();
486 set_fs(KERNEL_DS); /* More privs :-) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 err = sock_sendmsg(udpsock, &udpmsg, size);
488 set_fs(oldfs);
Phil Blundella27e13d2010-11-24 11:51:47 -0800489
490error_free_buf:
491 vfree(userbuf);
Eric Dumazet389f2a12011-01-26 00:04:18 +0000492error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#else
494 err = -EPROTOTYPE;
495#endif
David S. Miller1d181832006-03-28 00:01:55 -0800496 mutex_unlock(&econet_mutex);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return err;
499}
500
501/*
502 * Look up the address of a socket.
503 */
504
505static int econet_getname(struct socket *sock, struct sockaddr *uaddr,
506 int *uaddr_len, int peer)
507{
David S. Miller1d181832006-03-28 00:01:55 -0800508 struct sock *sk;
509 struct econet_sock *eo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
511
512 if (peer)
513 return -EOPNOTSUPP;
514
Eric Dumazet80922bb2009-08-06 03:48:36 +0000515 memset(sec, 0, sizeof(*sec));
David S. Miller1d181832006-03-28 00:01:55 -0800516 mutex_lock(&econet_mutex);
517
518 sk = sock->sk;
519 eo = ec_sk(sk);
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sec->sec_family = AF_ECONET;
522 sec->port = eo->port;
523 sec->addr.station = eo->station;
524 sec->addr.net = eo->net;
525
David S. Miller1d181832006-03-28 00:01:55 -0800526 mutex_unlock(&econet_mutex);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *uaddr_len = sizeof(*sec);
529 return 0;
530}
531
532static void econet_destroy_timer(unsigned long data)
533{
Joe Perchesee9c88f2011-07-03 08:28:33 +0000534 struct sock *sk = (struct sock *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Eric Dumazetc5640392009-06-16 10:12:03 +0000536 if (!sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sk_free(sk);
538 return;
539 }
540
541 sk->sk_timer.expires = jiffies + 10 * HZ;
542 add_timer(&sk->sk_timer);
Joe Perchesee9c88f2011-07-03 08:28:33 +0000543 pr_debug("econet: socket destroy delayed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546/*
547 * Close an econet socket.
548 */
549
550static int econet_release(struct socket *sock)
551{
David S. Miller1d181832006-03-28 00:01:55 -0800552 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
David S. Miller1d181832006-03-28 00:01:55 -0800554 mutex_lock(&econet_mutex);
555
556 sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!sk)
David S. Miller1d181832006-03-28 00:01:55 -0800558 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 econet_remove_socket(&econet_sklist, sk);
561
562 /*
563 * Now the socket is dead. No more input will appear.
564 */
565
566 sk->sk_state_change(sk); /* It is useless. Just for sanity. */
567
David S. Miller0efffaf2008-06-17 03:01:47 -0700568 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* Purge queues */
571
572 skb_queue_purge(&sk->sk_receive_queue);
573
Eric Dumazetc5640392009-06-16 10:12:03 +0000574 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 sk->sk_timer.data = (unsigned long)sk;
576 sk->sk_timer.expires = jiffies + HZ;
577 sk->sk_timer.function = econet_destroy_timer;
578 add_timer(&sk->sk_timer);
David S. Miller1d181832006-03-28 00:01:55 -0800579
580 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 sk_free(sk);
David S. Miller1d181832006-03-28 00:01:55 -0800584
585out_unlock:
586 mutex_unlock(&econet_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
588}
589
590static struct proto econet_proto = {
591 .name = "ECONET",
592 .owner = THIS_MODULE,
593 .obj_size = sizeof(struct econet_sock),
594};
595
596/*
597 * Create an Econet socket
598 */
599
Eric Paris3f378b62009-11-05 22:18:14 -0800600static int econet_create(struct net *net, struct socket *sock, int protocol,
601 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct sock *sk;
604 struct econet_sock *eo;
605 int err;
606
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800607 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700608 return -EAFNOSUPPORT;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Econet only provides datagram services. */
611 if (sock->type != SOCK_DGRAM)
612 return -ESOCKTNOSUPPORT;
613
614 sock->state = SS_UNCONNECTED;
615
616 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700617 sk = sk_alloc(net, PF_ECONET, GFP_KERNEL, &econet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (sk == NULL)
619 goto out;
620
621 sk->sk_reuse = 1;
622 sock->ops = &econet_ops;
623 sock_init_data(sock, sk);
624
625 eo = ec_sk(sk);
626 sock_reset_flag(sk, SOCK_ZAPPED);
627 sk->sk_family = PF_ECONET;
628 eo->num = protocol;
629
630 econet_insert_socket(&econet_sklist, sk);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632out:
633 return err;
634}
635
636/*
637 * Handle Econet specific ioctls
638 */
639
640static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg)
641{
642 struct ifreq ifr;
643 struct ec_device *edev;
644 struct net_device *dev;
645 struct sockaddr_ec *sec;
David S. Miller1d181832006-03-28 00:01:55 -0800646 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /*
649 * Fetch the caller's info block into kernel space
650 */
651
652 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
653 return -EFAULT;
654
Joe Perchesee9c88f2011-07-03 08:28:33 +0000655 dev = dev_get_by_name(&init_net, ifr.ifr_name);
656 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -ENODEV;
658
659 sec = (struct sockaddr_ec *)&ifr.ifr_addr;
660
David S. Miller1d181832006-03-28 00:01:55 -0800661 mutex_lock(&econet_mutex);
662
663 err = 0;
664 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 case SIOCSIFADDR:
Nelson Elhage0c62fc62010-12-08 10:13:55 -0800666 if (!capable(CAP_NET_ADMIN)) {
667 err = -EPERM;
668 break;
669 }
Phil Blundell16c41742010-11-24 11:49:53 -0800670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 edev = dev->ec_ptr;
David S. Miller1d181832006-03-28 00:01:55 -0800672 if (edev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* Magic up a new one. */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700674 edev = kzalloc(sizeof(struct ec_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (edev == NULL) {
David S. Miller1d181832006-03-28 00:01:55 -0800676 err = -ENOMEM;
677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 dev->ec_ptr = edev;
David S. Miller1d181832006-03-28 00:01:55 -0800680 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 net2dev_map[edev->net] = NULL;
682 edev->station = sec->addr.station;
683 edev->net = sec->addr.net;
684 net2dev_map[sec->addr.net] = dev;
685 if (!net2dev_map[0])
686 net2dev_map[0] = dev;
David S. Miller1d181832006-03-28 00:01:55 -0800687 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 case SIOCGIFADDR:
690 edev = dev->ec_ptr;
David S. Miller1d181832006-03-28 00:01:55 -0800691 if (edev == NULL) {
692 err = -ENODEV;
693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 memset(sec, 0, sizeof(struct sockaddr_ec));
696 sec->addr.station = edev->station;
697 sec->addr.net = edev->net;
698 sec->sec_family = AF_ECONET;
699 dev_put(dev);
700 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
David S. Miller1d181832006-03-28 00:01:55 -0800701 err = -EFAULT;
702 break;
703
704 default:
705 err = -EINVAL;
706 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
David S. Miller1d181832006-03-28 00:01:55 -0800709 mutex_unlock(&econet_mutex);
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dev_put(dev);
David S. Miller1d181832006-03-28 00:01:55 -0800712
713 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/*
717 * Handle generic ioctls
718 */
719
Joe Perchesee9c88f2011-07-03 08:28:33 +0000720static int econet_ioctl(struct socket *sock, unsigned int cmd,
721 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct sock *sk = sock->sk;
724 void __user *argp = (void __user *)arg;
725
Joe Perches075e1912011-07-01 09:43:04 +0000726 switch (cmd) {
727 case SIOCGSTAMP:
728 return sock_get_timestamp(sk, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Joe Perches075e1912011-07-01 09:43:04 +0000730 case SIOCGSTAMPNS:
731 return sock_get_timestampns(sk, argp);
Eric Dumazetae40eb12007-03-18 17:33:16 -0700732
Joe Perches075e1912011-07-01 09:43:04 +0000733 case SIOCSIFADDR:
734 case SIOCGIFADDR:
735 return ec_dev_ioctl(sock, cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Joe Perches075e1912011-07-01 09:43:04 +0000738
739 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000742static const struct net_proto_family econet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 .family = PF_ECONET,
744 .create = econet_create,
745 .owner = THIS_MODULE,
746};
747
David S. Miller1d181832006-03-28 00:01:55 -0800748static const struct proto_ops econet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .family = PF_ECONET,
750 .owner = THIS_MODULE,
751 .release = econet_release,
752 .bind = econet_bind,
753 .connect = sock_no_connect,
754 .socketpair = sock_no_socketpair,
755 .accept = sock_no_accept,
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900756 .getname = econet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .poll = datagram_poll,
758 .ioctl = econet_ioctl,
759 .listen = sock_no_listen,
760 .shutdown = sock_no_shutdown,
761 .setsockopt = sock_no_setsockopt,
762 .getsockopt = sock_no_getsockopt,
763 .sendmsg = econet_sendmsg,
764 .recvmsg = econet_recvmsg,
765 .mmap = sock_no_mmap,
766 .sendpage = sock_no_sendpage,
767};
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
770/*
771 * Find the listening socket, if any, for the given data.
772 */
773
774static struct sock *ec_listening_socket(unsigned char port, unsigned char
775 station, unsigned char net)
776{
777 struct sock *sk;
778 struct hlist_node *node;
779
Eric Dumazet0c78a922010-06-09 16:33:05 +0000780 spin_lock(&econet_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 sk_for_each(sk, node, &econet_sklist) {
782 struct econet_sock *opt = ec_sk(sk);
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900783 if ((opt->port == port || opt->port == 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 (opt->station == station || opt->station == 0) &&
Eric Dumazet0c78a922010-06-09 16:33:05 +0000785 (opt->net == net || opt->net == 0)) {
786 sock_hold(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto found;
Eric Dumazet0c78a922010-06-09 16:33:05 +0000788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 sk = NULL;
791found:
Eric Dumazet0c78a922010-06-09 16:33:05 +0000792 spin_unlock(&econet_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return sk;
794}
795
796/*
797 * Queue a received packet for a socket.
798 */
799
800static int ec_queue_packet(struct sock *sk, struct sk_buff *skb,
801 unsigned char stn, unsigned char net,
802 unsigned char cb, unsigned char port)
803{
804 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
805 struct sockaddr_ec *sec = (struct sockaddr_ec *)&eb->sec;
806
807 memset(sec, 0, sizeof(struct sockaddr_ec));
808 sec->sec_family = AF_ECONET;
809 sec->type = ECTYPE_PACKET_RECEIVED;
810 sec->port = port;
811 sec->cb = cb;
812 sec->addr.net = net;
813 sec->addr.station = stn;
814
815 return sock_queue_rcv_skb(sk, skb);
816}
817#endif
818
819#ifdef CONFIG_ECONET_AUNUDP
820/*
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900821 * Send an AUN protocol response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823
824static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
825{
826 struct sockaddr_in sin = {
827 .sin_family = AF_INET,
828 .sin_port = htons(AUN_PORT),
829 .sin_addr = {.s_addr = addr}
830 };
831 struct aunhdr ah = {.code = code, .cb = cb, .handle = seq};
832 struct kvec iov = {.iov_base = (void *)&ah, .iov_len = sizeof(ah)};
833 struct msghdr udpmsg;
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 udpmsg.msg_name = (void *)&sin;
836 udpmsg.msg_namelen = sizeof(sin);
837 udpmsg.msg_control = NULL;
838 udpmsg.msg_controllen = 0;
Joe Perchesee9c88f2011-07-03 08:28:33 +0000839 udpmsg.msg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 kernel_sendmsg(udpsock, &udpmsg, &iov, 1, sizeof(ah));
842}
843
844
845/*
846 * Handle incoming AUN packets. Work out if anybody wants them,
847 * and send positive or negative acknowledgements as appropriate.
848 */
849
850static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
851{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700852 struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 unsigned char stn = ntohl(ip->saddr) & 0xff;
David S. Miller4e085e72010-12-08 18:42:23 -0800854 struct dst_entry *dst = skb_dst(skb);
855 struct ec_device *edev = NULL;
Eric Dumazet0c78a922010-06-09 16:33:05 +0000856 struct sock *sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 struct sk_buff *newskb;
David S. Miller4e085e72010-12-08 18:42:23 -0800858
859 if (dst)
860 edev = dst->dev->ec_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Joe Perchesee9c88f2011-07-03 08:28:33 +0000862 if (!edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto bad;
864
Joe Perchesee9c88f2011-07-03 08:28:33 +0000865 sk = ec_listening_socket(ah->port, stn, edev->net);
866 if (sk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto bad; /* Nobody wants it */
868
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900869 newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 GFP_ATOMIC);
Joe Perchesee9c88f2011-07-03 08:28:33 +0000871 if (newskb == NULL) {
872 pr_debug("AUN: memory squeeze, dropping packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /* Send nack and hope sender tries again */
874 goto bad;
875 }
876
Joe Perchesee9c88f2011-07-03 08:28:33 +0000877 memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah + 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 len - sizeof(struct aunhdr));
879
Joe Perchesee9c88f2011-07-03 08:28:33 +0000880 if (ec_queue_packet(sk, newskb, stn, edev->net, ah->cb, ah->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Socket is bankrupt. */
882 kfree_skb(newskb);
883 goto bad;
884 }
885
886 aun_send_response(ip->saddr, ah->handle, 3, 0);
Eric Dumazet0c78a922010-06-09 16:33:05 +0000887 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return;
889
890bad:
891 aun_send_response(ip->saddr, ah->handle, 4, 0);
Eric Dumazet0c78a922010-06-09 16:33:05 +0000892 if (sk)
893 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897 * Handle incoming AUN transmit acknowledgements. If the sequence
898 * number matches something in our backlog then kill it and tell
899 * the user. If the remote took too long to reply then we may have
900 * dropped the packet already.
901 */
902
903static void aun_tx_ack(unsigned long seq, int result)
904{
905 struct sk_buff *skb;
906 unsigned long flags;
907 struct ec_cb *eb;
908
909 spin_lock_irqsave(&aun_queue_lock, flags);
David S. Millerde103342009-05-28 16:46:29 -0700910 skb_queue_walk(&aun_queue, skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 eb = (struct ec_cb *)&skb->cb;
912 if (eb->seq == seq)
913 goto foundit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 spin_unlock_irqrestore(&aun_queue_lock, flags);
Joe Perchesee9c88f2011-07-03 08:28:33 +0000916 pr_debug("AUN: unknown sequence %ld\n", seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return;
918
919foundit:
920 tx_result(skb->sk, eb->cookie, result);
David S. Miller8728b832005-08-09 19:25:21 -0700921 skb_unlink(skb, &aun_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 spin_unlock_irqrestore(&aun_queue_lock, flags);
923 kfree_skb(skb);
924}
925
926/*
927 * Deal with received AUN frames - sort out what type of thing it is
928 * and hand it to the right function.
929 */
930
931static void aun_data_available(struct sock *sk, int slen)
932{
933 int err;
934 struct sk_buff *skb;
935 unsigned char *data;
936 struct aunhdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 size_t len;
938
939 while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
940 if (err == -EAGAIN) {
Joe Perchesee9c88f2011-07-03 08:28:33 +0000941 pr_err("AUN: no data available?!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return;
943 }
Joe Perchesee9c88f2011-07-03 08:28:33 +0000944 pr_debug("AUN: recvfrom() error %d\n", -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700947 data = skb_transport_header(skb) + sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ah = (struct aunhdr *)data;
949 len = skb->len - sizeof(struct udphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Joe Perchesee9c88f2011-07-03 08:28:33 +0000951 switch (ah->code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 case 2:
953 aun_incoming(skb, ah, len);
954 break;
955 case 3:
956 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
957 break;
958 case 4:
959 aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 default:
Joe Perchesee9c88f2011-07-03 08:28:33 +0000962 pr_debug("AUN: unknown packet type: %d\n", data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
965 skb_free_datagram(sk, skb);
966}
967
968/*
969 * Called by the timer to manage the AUN transmit queue. If a packet
970 * was sent to a dead or nonexistent host then we will never get an
971 * acknowledgement back. After a few seconds we need to spot this and
972 * drop the packet.
973 */
974
975static void ab_cleanup(unsigned long h)
976{
David S. Millerde103342009-05-28 16:46:29 -0700977 struct sk_buff *skb, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned long flags;
979
980 spin_lock_irqsave(&aun_queue_lock, flags);
David S. Millerde103342009-05-28 16:46:29 -0700981 skb_queue_walk_safe(&aun_queue, skb, n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 struct ec_cb *eb = (struct ec_cb *)&skb->cb;
David S. Millerde103342009-05-28 16:46:29 -0700983 if ((jiffies - eb->start) > eb->timeout) {
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +0900984 tx_result(skb->sk, eb->cookie,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 ECTYPE_TRANSMIT_NOT_PRESENT);
David S. Miller8728b832005-08-09 19:25:21 -0700986 skb_unlink(skb, &aun_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 kfree_skb(skb);
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 spin_unlock_irqrestore(&aun_queue_lock, flags);
991
Joe Perchesee9c88f2011-07-03 08:28:33 +0000992 mod_timer(&ab_cleanup_timer, jiffies + (HZ * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995static int __init aun_udp_initialise(void)
996{
997 int error;
998 struct sockaddr_in sin;
999
1000 skb_queue_head_init(&aun_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001001 setup_timer(&ab_cleanup_timer, ab_cleanup, 0);
Joe Perchesee9c88f2011-07-03 08:28:33 +00001002 ab_cleanup_timer.expires = jiffies + (HZ * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 add_timer(&ab_cleanup_timer);
1004
1005 memset(&sin, 0, sizeof(sin));
1006 sin.sin_port = htons(AUN_PORT);
1007
1008 /* We can count ourselves lucky Acorn machines are too dim to
1009 speak IPv6. :-) */
Joe Perchesee9c88f2011-07-03 08:28:33 +00001010 error = sock_create_kern(PF_INET, SOCK_DGRAM, 0, &udpsock);
1011 if (error < 0) {
1012 pr_err("AUN: socket error %d\n", -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return error;
1014 }
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +09001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 udpsock->sk->sk_reuse = 1;
1017 udpsock->sk->sk_allocation = GFP_ATOMIC; /* we're going to call it
1018 from interrupts */
YOSHIFUJI Hideakic9b6aab2007-02-09 23:24:42 +09001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
Joe Perchesee9c88f2011-07-03 08:28:33 +00001021 sizeof(sin));
1022 if (error < 0) {
1023 pr_err("AUN: bind error %d\n", -error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto release;
1025 }
1026
1027 udpsock->sk->sk_data_ready = aun_data_available;
1028
1029 return 0;
1030
1031release:
1032 sock_release(udpsock);
1033 udpsock = NULL;
1034 return error;
1035}
1036#endif
1037
1038#ifdef CONFIG_ECONET_NATIVE
1039
1040/*
1041 * Receive an Econet frame from a device.
1042 */
1043
Joe Perchesee9c88f2011-07-03 08:28:33 +00001044static int econet_rcv(struct sk_buff *skb, struct net_device *dev,
1045 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct ec_framehdr *hdr;
Eric Dumazet0c78a922010-06-09 16:33:05 +00001048 struct sock *sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct ec_device *edev = dev->ec_ptr;
1050
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001051 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -07001052 goto drop;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (skb->pkt_type == PACKET_OTHERHOST)
1055 goto drop;
1056
1057 if (!edev)
1058 goto drop;
1059
Joe Perchesee9c88f2011-07-03 08:28:33 +00001060 skb = skb_share_check(skb, GFP_ATOMIC);
1061 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return NET_RX_DROP;
1063
1064 if (!pskb_may_pull(skb, sizeof(struct ec_framehdr)))
1065 goto drop;
1066
Joe Perchesee9c88f2011-07-03 08:28:33 +00001067 hdr = (struct ec_framehdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* First check for encapsulated IP */
1070 if (hdr->port == EC_PORT_IP) {
1071 skb->protocol = htons(ETH_P_IP);
1072 skb_pull(skb, sizeof(struct ec_framehdr));
1073 netif_rx(skb);
Mark Smith482d8042009-07-06 11:05:58 +00001074 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 sk = ec_listening_socket(hdr->port, hdr->src_stn, hdr->src_net);
1078 if (!sk)
1079 goto drop;
1080
1081 if (ec_queue_packet(sk, skb, edev->net, hdr->src_stn, hdr->cb,
1082 hdr->port))
1083 goto drop;
Eric Dumazet0c78a922010-06-09 16:33:05 +00001084 sock_put(sk);
Mark Smith482d8042009-07-06 11:05:58 +00001085 return NET_RX_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087drop:
Eric Dumazet0c78a922010-06-09 16:33:05 +00001088 if (sk)
1089 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 kfree_skb(skb);
1091 return NET_RX_DROP;
1092}
1093
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001094static struct packet_type econet_packet_type __read_mostly = {
Joe Perchesee9c88f2011-07-03 08:28:33 +00001095 .type = cpu_to_be16(ETH_P_ECONET),
1096 .func = econet_rcv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097};
1098
1099static void econet_hw_initialise(void)
1100{
1101 dev_add_pack(&econet_packet_type);
1102}
1103
1104#endif
1105
Joe Perchesee9c88f2011-07-03 08:28:33 +00001106static int econet_notifier(struct notifier_block *this, unsigned long msg,
1107 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Joe Perchesee9c88f2011-07-03 08:28:33 +00001109 struct net_device *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct ec_device *edev;
1111
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001112 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001113 return NOTIFY_DONE;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 switch (msg) {
1116 case NETDEV_UNREGISTER:
1117 /* A device has gone down - kill any data we hold for it. */
1118 edev = dev->ec_ptr;
Joe Perchesee9c88f2011-07-03 08:28:33 +00001119 if (edev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (net2dev_map[0] == dev)
1121 net2dev_map[0] = NULL;
1122 net2dev_map[edev->net] = NULL;
1123 kfree(edev);
1124 dev->ec_ptr = NULL;
1125 }
1126 break;
1127 }
1128
1129 return NOTIFY_DONE;
1130}
1131
1132static struct notifier_block econet_netdev_notifier = {
Joe Perchesee9c88f2011-07-03 08:28:33 +00001133 .notifier_call = econet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134};
1135
1136static void __exit econet_proto_exit(void)
1137{
1138#ifdef CONFIG_ECONET_AUNUDP
1139 del_timer(&ab_cleanup_timer);
1140 if (udpsock)
1141 sock_release(udpsock);
1142#endif
1143 unregister_netdevice_notifier(&econet_netdev_notifier);
Alexey Dobriyan9c29a372007-08-14 17:25:20 -07001144#ifdef CONFIG_ECONET_NATIVE
1145 dev_remove_pack(&econet_packet_type);
1146#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 sock_unregister(econet_family_ops.family);
1148 proto_unregister(&econet_proto);
1149}
1150
1151static int __init econet_proto_init(void)
1152{
1153 int err = proto_register(&econet_proto, 0);
1154
1155 if (err != 0)
1156 goto out;
1157 sock_register(&econet_family_ops);
1158#ifdef CONFIG_ECONET_AUNUDP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 aun_udp_initialise();
1160#endif
1161#ifdef CONFIG_ECONET_NATIVE
1162 econet_hw_initialise();
1163#endif
1164 register_netdevice_notifier(&econet_netdev_notifier);
1165out:
1166 return err;
1167}
1168
1169module_init(econet_proto_init);
1170module_exit(econet_proto_exit);
1171
1172MODULE_LICENSE("GPL");
1173MODULE_ALIAS_NETPROTO(PF_ECONET);