2005-04-16 18:20:36 -04:00
|
|
|
/*
|
|
|
|
* An implementation of the Acorn Econet and AUN protocols.
|
|
|
|
* Philip Blundell <philb@gnu.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
#define pr_fmt(fmt) fmt
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/inetdevice.h>
|
|
|
|
#include <linux/route.h>
|
|
|
|
#include <linux/inet.h>
|
|
|
|
#include <linux/etherdevice.h>
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
#include <linux/wireless.h>
|
|
|
|
#include <linux/skbuff.h>
|
2005-12-26 23:43:12 -05:00
|
|
|
#include <linux/udp.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 04:04:11 -04:00
|
|
|
#include <linux/slab.h>
|
2010-11-24 14:51:47 -05:00
|
|
|
#include <linux/vmalloc.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/inet_common.h>
|
|
|
|
#include <linux/stat.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/if_ec.h>
|
|
|
|
#include <net/udp.h>
|
|
|
|
#include <net/ip.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/rcupdate.h>
|
|
|
|
#include <linux/bitops.h>
|
2006-03-28 03:01:55 -05:00
|
|
|
#include <linux/mutex.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <asm/system.h>
|
|
|
|
|
2005-12-22 15:49:22 -05:00
|
|
|
static const struct proto_ops econet_ops;
|
2005-04-16 18:20:36 -04:00
|
|
|
static struct hlist_head econet_sklist;
|
2010-06-09 12:33:05 -04:00
|
|
|
static DEFINE_SPINLOCK(econet_lock);
|
2006-03-28 03:01:55 -05:00
|
|
|
static DEFINE_MUTEX(econet_mutex);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/* Since there are only 256 possible network numbers (or fewer, depends
|
|
|
|
how you count) it makes sense to use a simple lookup table. */
|
|
|
|
static struct net_device *net2dev_map[256];
|
|
|
|
|
|
|
|
#define EC_PORT_IP 0xd2
|
|
|
|
|
|
|
|
#ifdef CONFIG_ECONET_AUNUDP
|
2006-01-04 16:56:08 -05:00
|
|
|
static DEFINE_SPINLOCK(aun_queue_lock);
|
2005-04-16 18:20:36 -04:00
|
|
|
static struct socket *udpsock;
|
|
|
|
#define AUN_PORT 0x8000
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
struct aunhdr {
|
2005-04-16 18:20:36 -04:00
|
|
|
unsigned char code; /* AUN magic protocol byte */
|
|
|
|
unsigned char port;
|
|
|
|
unsigned char cb;
|
|
|
|
unsigned char pad;
|
|
|
|
unsigned long handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long aun_seq;
|
|
|
|
|
|
|
|
/* Queue of packets waiting to be transmitted. */
|
|
|
|
static struct sk_buff_head aun_queue;
|
|
|
|
static struct timer_list ab_cleanup_timer;
|
|
|
|
|
|
|
|
#endif /* CONFIG_ECONET_AUNUDP */
|
|
|
|
|
|
|
|
/* Per-packet information */
|
2011-07-03 04:28:33 -04:00
|
|
|
struct ec_cb {
|
2005-04-16 18:20:36 -04:00
|
|
|
struct sockaddr_ec sec;
|
|
|
|
unsigned long cookie; /* Supplied by user. */
|
|
|
|
#ifdef CONFIG_ECONET_AUNUDP
|
|
|
|
int done;
|
|
|
|
unsigned long seq; /* Sequencing */
|
|
|
|
unsigned long timeout; /* Timeout */
|
|
|
|
unsigned long start; /* jiffies */
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ECONET_NATIVE
|
|
|
|
void (*sent)(struct sk_buff *, int result);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static void econet_remove_socket(struct hlist_head *list, struct sock *sk)
|
|
|
|
{
|
2010-06-09 12:33:05 -04:00
|
|
|
spin_lock_bh(&econet_lock);
|
2005-04-16 18:20:36 -04:00
|
|
|
sk_del_node_init(sk);
|
2010-06-09 12:33:05 -04:00
|
|
|
spin_unlock_bh(&econet_lock);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void econet_insert_socket(struct hlist_head *list, struct sock *sk)
|
|
|
|
{
|
2010-06-09 12:33:05 -04:00
|
|
|
spin_lock_bh(&econet_lock);
|
2005-04-16 18:20:36 -04:00
|
|
|
sk_add_node(sk, list);
|
2010-06-09 12:33:05 -04:00
|
|
|
spin_unlock_bh(&econet_lock);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pull a packet from our receive queue and hand it to the user.
|
|
|
|
* If necessary we block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int econet_recvmsg(struct kiocb *iocb, struct socket *sock,
|
|
|
|
struct msghdr *msg, size_t len, int flags)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
size_t copied;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
msg->msg_namelen = sizeof(struct sockaddr_ec);
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_lock(&econet_mutex);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/*
|
|
|
|
* Call the generic datagram receiver. This handles all sorts
|
|
|
|
* of horrible races and re-entrancy so we can forget about it
|
|
|
|
* in the protocol layers.
|
|
|
|
*
|
|
|
|
* Now it will return ENETDOWN, if device have just gone down,
|
|
|
|
* but then it will block.
|
|
|
|
*/
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/*
|
2007-02-09 09:24:42 -05:00
|
|
|
* An error occurred so return it. Because skb_recv_datagram()
|
2005-04-16 18:20:36 -04:00
|
|
|
* handles the blocking we don't see and worry about blocking
|
|
|
|
* retries.
|
|
|
|
*/
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
if (skb == NULL)
|
2005-04-16 18:20:36 -04:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* You lose any data beyond the buffer you gave. If it worries a
|
|
|
|
* user program they can ask the device for its MTU anyway.
|
|
|
|
*/
|
|
|
|
|
|
|
|
copied = skb->len;
|
2011-07-03 04:28:33 -04:00
|
|
|
if (copied > len) {
|
|
|
|
copied = len;
|
|
|
|
msg->msg_flags |= MSG_TRUNC;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We can't use skb_copy_datagram here */
|
|
|
|
err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
|
|
|
|
if (err)
|
|
|
|
goto out_free;
|
2007-04-19 19:16:32 -04:00
|
|
|
sk->sk_stamp = skb->tstamp;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
if (msg->msg_name)
|
|
|
|
memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free or return the buffer as appropriate. Again this
|
|
|
|
* hides all the races and re-entrancy issues from us.
|
|
|
|
*/
|
|
|
|
err = copied;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
skb_free_datagram(sk, skb);
|
|
|
|
out:
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_unlock(&econet_mutex);
|
2005-04-16 18:20:36 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind an Econet socket.
|
|
|
|
*/
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
static int econet_bind(struct socket *sock, struct sockaddr *uaddr,
|
|
|
|
int addr_len)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
|
2006-03-28 03:01:55 -05:00
|
|
|
struct sock *sk;
|
|
|
|
struct econet_sock *eo;
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/*
|
|
|
|
* Check legality
|
|
|
|
*/
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
if (addr_len < sizeof(struct sockaddr_ec) ||
|
|
|
|
sec->sec_family != AF_ECONET)
|
|
|
|
return -EINVAL;
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_lock(&econet_mutex);
|
|
|
|
|
|
|
|
sk = sock->sk;
|
|
|
|
eo = ec_sk(sk);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
eo->cb = sec->cb;
|
|
|
|
eo->port = sec->port;
|
|
|
|
eo->station = sec->addr.station;
|
|
|
|
eo->net = sec->addr.net;
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_unlock(&econet_mutex);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
|
|
|
|
/*
|
|
|
|
* Queue a transmit result for the user to be told about.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void tx_result(struct sock *sk, unsigned long cookie, int result)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb = alloc_skb(0, GFP_ATOMIC);
|
|
|
|
struct ec_cb *eb;
|
|
|
|
struct sockaddr_ec *sec;
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
if (skb == NULL) {
|
|
|
|
pr_debug("econet: memory squeeze, transmit result dropped\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eb = (struct ec_cb *)&skb->cb;
|
|
|
|
sec = (struct sockaddr_ec *)&eb->sec;
|
|
|
|
memset(sec, 0, sizeof(struct sockaddr_ec));
|
|
|
|
sec->cookie = cookie;
|
|
|
|
sec->type = ECTYPE_TRANSMIT_STATUS | result;
|
|
|
|
sec->sec_family = AF_ECONET;
|
|
|
|
|
|
|
|
if (sock_queue_rcv_skb(sk, skb) < 0)
|
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ECONET_NATIVE
|
|
|
|
/*
|
|
|
|
* Called by the Econet hardware driver when a packet transmit
|
|
|
|
* has completed. Tell the user.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void ec_tx_done(struct sk_buff *skb, int result)
|
|
|
|
{
|
|
|
|
struct ec_cb *eb = (struct ec_cb *)&skb->cb;
|
|
|
|
tx_result(skb->sk, eb->cookie, result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a packet. We have to work out which device it's going out on
|
|
|
|
* and hence whether to use real Econet or the UDP emulation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int econet_sendmsg(struct kiocb *iocb, struct socket *sock,
|
|
|
|
struct msghdr *msg, size_t len)
|
|
|
|
{
|
2011-07-03 04:28:33 -04:00
|
|
|
struct sockaddr_ec *saddr = (struct sockaddr_ec *)msg->msg_name;
|
2005-04-16 18:20:36 -04:00
|
|
|
struct net_device *dev;
|
|
|
|
struct ec_addr addr;
|
|
|
|
int err;
|
|
|
|
unsigned char port, cb;
|
|
|
|
#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
|
2011-01-25 19:04:18 -05:00
|
|
|
struct sock *sk = sock->sk;
|
2005-04-16 18:20:36 -04:00
|
|
|
struct sk_buff *skb;
|
|
|
|
struct ec_cb *eb;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ECONET_AUNUDP
|
|
|
|
struct msghdr udpmsg;
|
2010-11-24 14:51:47 -05:00
|
|
|
struct iovec iov[2];
|
2005-04-16 18:20:36 -04:00
|
|
|
struct aunhdr ah;
|
|
|
|
struct sockaddr_in udpdest;
|
|
|
|
__kernel_size_t size;
|
|
|
|
mm_segment_t oldfs;
|
2010-11-24 14:51:47 -05:00
|
|
|
char *userbuf;
|
2005-04-16 18:20:36 -04:00
|
|
|
#endif
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/*
|
2007-02-09 09:24:42 -05:00
|
|
|
* Check the flags.
|
2005-04-16 18:20:36 -04:00
|
|
|
*/
|
|
|
|
|
2007-02-09 09:24:42 -05:00
|
|
|
if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
|
2005-04-16 18:20:36 -04:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
2007-02-09 09:24:42 -05:00
|
|
|
* Get and verify the address.
|
2005-04-16 18:20:36 -04:00
|
|
|
*/
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_lock(&econet_mutex);
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
if (saddr == NULL || msg->msg_namelen < sizeof(struct sockaddr_ec)) {
|
|
|
|
mutex_unlock(&econet_mutex);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
addr.station = saddr->addr.station;
|
|
|
|
addr.net = saddr->addr.net;
|
|
|
|
port = saddr->port;
|
|
|
|
cb = saddr->cb;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/* Look for a device with the right network number. */
|
|
|
|
dev = net2dev_map[addr.net];
|
|
|
|
|
|
|
|
/* If not directly reachable, use some default */
|
2006-03-28 03:01:55 -05:00
|
|
|
if (dev == NULL) {
|
2005-04-16 18:20:36 -04:00
|
|
|
dev = net2dev_map[0];
|
|
|
|
/* No interfaces at all? */
|
2006-03-28 03:01:55 -05:00
|
|
|
if (dev == NULL) {
|
|
|
|
mutex_unlock(&econet_mutex);
|
2005-04-16 18:20:36 -04:00
|
|
|
return -ENETDOWN;
|
2006-03-28 03:01:55 -05:00
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
if (dev->type == ARPHRD_ECONET) {
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Real hardware Econet. We're not worthy etc. */
|
|
|
|
#ifdef CONFIG_ECONET_NATIVE
|
|
|
|
unsigned short proto = 0;
|
2011-11-17 21:20:04 -05:00
|
|
|
int hlen, tlen;
|
2007-10-09 04:36:32 -04:00
|
|
|
int res;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2010-11-24 14:51:47 -05:00
|
|
|
if (len + 15 > dev->mtu) {
|
|
|
|
mutex_unlock(&econet_mutex);
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
dev_hold(dev);
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2011-11-17 21:20:04 -05:00
|
|
|
hlen = LL_RESERVED_SPACE(dev);
|
|
|
|
tlen = dev->needed_tailroom;
|
|
|
|
skb = sock_alloc_send_skb(sk, len + hlen + tlen,
|
2005-04-16 18:20:36 -04:00
|
|
|
msg->msg_flags & MSG_DONTWAIT, &err);
|
2011-07-03 04:28:33 -04:00
|
|
|
if (skb == NULL)
|
2005-04-16 18:20:36 -04:00
|
|
|
goto out_unlock;
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2011-11-17 21:20:04 -05:00
|
|
|
skb_reserve(skb, hlen);
|
2007-04-10 23:45:18 -04:00
|
|
|
skb_reset_network_header(skb);
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
eb = (struct ec_cb *)&skb->cb;
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
eb->cookie = saddr->cookie;
|
|
|
|
eb->sec = *saddr;
|
|
|
|
eb->sent = ec_tx_done;
|
|
|
|
|
2007-10-09 04:36:32 -04:00
|
|
|
err = -EINVAL;
|
|
|
|
res = dev_hard_header(skb, dev, ntohs(proto), &addr, NULL, len);
|
|
|
|
if (res < 0)
|
|
|
|
goto out_free;
|
|
|
|
if (res > 0) {
|
2005-04-16 18:20:36 -04:00
|
|
|
struct ec_framehdr *fh;
|
|
|
|
/* Poke in our control byte and
|
|
|
|
port number. Hack, hack. */
|
2011-07-03 04:28:33 -04:00
|
|
|
fh = (struct ec_framehdr *)skb->data;
|
2005-04-16 18:20:36 -04:00
|
|
|
fh->cb = cb;
|
|
|
|
fh->port = port;
|
|
|
|
if (sock->type != SOCK_DGRAM) {
|
2007-04-19 23:29:13 -04:00
|
|
|
skb_reset_tail_pointer(skb);
|
2005-04-16 18:20:36 -04:00
|
|
|
skb->len = 0;
|
2007-10-09 04:36:32 -04:00
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Copy the data. Returns -EFAULT on error */
|
2011-07-03 04:28:33 -04:00
|
|
|
err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
|
2005-04-16 18:20:36 -04:00
|
|
|
skb->protocol = proto;
|
|
|
|
skb->dev = dev;
|
|
|
|
skb->priority = sk->sk_priority;
|
|
|
|
if (err)
|
|
|
|
goto out_free;
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
err = -ENETDOWN;
|
|
|
|
if (!(dev->flags & IFF_UP))
|
|
|
|
goto out_free;
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/*
|
|
|
|
* Now send it
|
|
|
|
*/
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
dev_queue_xmit(skb);
|
|
|
|
dev_put(dev);
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_unlock(&econet_mutex);
|
2010-09-22 16:43:57 -04:00
|
|
|
return len;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
out_free:
|
2005-04-16 18:20:36 -04:00
|
|
|
kfree_skb(skb);
|
2011-07-03 04:28:33 -04:00
|
|
|
out_unlock:
|
2005-04-16 18:20:36 -04:00
|
|
|
if (dev)
|
|
|
|
dev_put(dev);
|
|
|
|
#else
|
|
|
|
err = -EPROTOTYPE;
|
|
|
|
#endif
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_unlock(&econet_mutex);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_ECONET_AUNUDP
|
|
|
|
/* AUN virtual Econet. */
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
if (udpsock == NULL) {
|
|
|
|
mutex_unlock(&econet_mutex);
|
2005-04-16 18:20:36 -04:00
|
|
|
return -ENETDOWN; /* No socket - can't send */
|
2006-03-28 03:01:55 -05:00
|
|
|
}
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2010-11-24 14:51:47 -05:00
|
|
|
if (len > 32768) {
|
|
|
|
err = -E2BIG;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Make up a UDP datagram and hand it off to some higher intellect. */
|
|
|
|
|
|
|
|
memset(&udpdest, 0, sizeof(udpdest));
|
|
|
|
udpdest.sin_family = AF_INET;
|
|
|
|
udpdest.sin_port = htons(AUN_PORT);
|
|
|
|
|
|
|
|
/* At the moment we use the stupid Acorn scheme of Econet address
|
|
|
|
y.x maps to IP a.b.c.x. This should be replaced with something
|
|
|
|
more flexible and more aware of subnet masks. */
|
|
|
|
{
|
|
|
|
struct in_device *idev;
|
|
|
|
unsigned long network = 0;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2005-10-03 17:35:55 -04:00
|
|
|
idev = __in_dev_get_rcu(dev);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (idev) {
|
|
|
|
if (idev->ifa_list)
|
2007-02-09 09:24:42 -05:00
|
|
|
network = ntohl(idev->ifa_list->ifa_address) &
|
2005-04-16 18:20:36 -04:00
|
|
|
0xffffff00; /* !!! */
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
udpdest.sin_addr.s_addr = htonl(network | addr.station);
|
|
|
|
}
|
|
|
|
|
2011-03-16 21:40:10 -04:00
|
|
|
memset(&ah, 0, sizeof(ah));
|
2005-04-16 18:20:36 -04:00
|
|
|
ah.port = port;
|
|
|
|
ah.cb = cb & 0x7f;
|
|
|
|
ah.code = 2; /* magic */
|
|
|
|
|
|
|
|
/* tack our header on the front of the iovec */
|
|
|
|
size = sizeof(struct aunhdr);
|
|
|
|
iov[0].iov_base = (void *)&ah;
|
|
|
|
iov[0].iov_len = size;
|
2010-11-24 14:51:47 -05:00
|
|
|
|
|
|
|
userbuf = vmalloc(len);
|
|
|
|
if (userbuf == NULL) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto error;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2010-11-24 14:51:47 -05:00
|
|
|
iov[1].iov_base = userbuf;
|
|
|
|
iov[1].iov_len = len;
|
|
|
|
err = memcpy_fromiovec(userbuf, msg->msg_iov, len);
|
|
|
|
if (err)
|
|
|
|
goto error_free_buf;
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Get a skbuff (no data, just holds our cb information) */
|
2011-07-03 04:28:33 -04:00
|
|
|
skb = sock_alloc_send_skb(sk, 0, msg->msg_flags & MSG_DONTWAIT, &err);
|
|
|
|
if (skb == NULL)
|
2010-11-24 14:51:47 -05:00
|
|
|
goto error_free_buf;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
eb = (struct ec_cb *)&skb->cb;
|
|
|
|
|
|
|
|
eb->cookie = saddr->cookie;
|
2011-07-03 04:28:33 -04:00
|
|
|
eb->timeout = 5 * HZ;
|
2005-04-16 18:20:36 -04:00
|
|
|
eb->start = jiffies;
|
|
|
|
ah.handle = aun_seq;
|
|
|
|
eb->seq = (aun_seq++);
|
|
|
|
eb->sec = *saddr;
|
|
|
|
|
|
|
|
skb_queue_tail(&aun_queue, skb);
|
|
|
|
|
|
|
|
udpmsg.msg_name = (void *)&udpdest;
|
|
|
|
udpmsg.msg_namelen = sizeof(udpdest);
|
|
|
|
udpmsg.msg_iov = &iov[0];
|
2010-11-24 14:51:47 -05:00
|
|
|
udpmsg.msg_iovlen = 2;
|
2005-04-16 18:20:36 -04:00
|
|
|
udpmsg.msg_control = NULL;
|
|
|
|
udpmsg.msg_controllen = 0;
|
2011-07-03 04:28:33 -04:00
|
|
|
udpmsg.msg_flags = 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(KERNEL_DS); /* More privs :-) */
|
2005-04-16 18:20:36 -04:00
|
|
|
err = sock_sendmsg(udpsock, &udpmsg, size);
|
|
|
|
set_fs(oldfs);
|
2010-11-24 14:51:47 -05:00
|
|
|
|
|
|
|
error_free_buf:
|
|
|
|
vfree(userbuf);
|
2011-01-25 19:04:18 -05:00
|
|
|
error:
|
2005-04-16 18:20:36 -04:00
|
|
|
#else
|
|
|
|
err = -EPROTOTYPE;
|
|
|
|
#endif
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_unlock(&econet_mutex);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up the address of a socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int econet_getname(struct socket *sock, struct sockaddr *uaddr,
|
|
|
|
int *uaddr_len, int peer)
|
|
|
|
{
|
2006-03-28 03:01:55 -05:00
|
|
|
struct sock *sk;
|
|
|
|
struct econet_sock *eo;
|
2005-04-16 18:20:36 -04:00
|
|
|
struct sockaddr_ec *sec = (struct sockaddr_ec *)uaddr;
|
|
|
|
|
|
|
|
if (peer)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2009-08-05 23:48:36 -04:00
|
|
|
memset(sec, 0, sizeof(*sec));
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_lock(&econet_mutex);
|
|
|
|
|
|
|
|
sk = sock->sk;
|
|
|
|
eo = ec_sk(sk);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
sec->sec_family = AF_ECONET;
|
|
|
|
sec->port = eo->port;
|
|
|
|
sec->addr.station = eo->station;
|
|
|
|
sec->addr.net = eo->net;
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_unlock(&econet_mutex);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
*uaddr_len = sizeof(*sec);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void econet_destroy_timer(unsigned long data)
|
|
|
|
{
|
2011-07-03 04:28:33 -04:00
|
|
|
struct sock *sk = (struct sock *)data;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2009-06-16 06:12:03 -04:00
|
|
|
if (!sk_has_allocations(sk)) {
|
2005-04-16 18:20:36 -04:00
|
|
|
sk_free(sk);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk->sk_timer.expires = jiffies + 10 * HZ;
|
|
|
|
add_timer(&sk->sk_timer);
|
2011-07-03 04:28:33 -04:00
|
|
|
pr_debug("econet: socket destroy delayed\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close an econet socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int econet_release(struct socket *sock)
|
|
|
|
{
|
2006-03-28 03:01:55 -05:00
|
|
|
struct sock *sk;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_lock(&econet_mutex);
|
|
|
|
|
|
|
|
sk = sock->sk;
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!sk)
|
2006-03-28 03:01:55 -05:00
|
|
|
goto out_unlock;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
econet_remove_socket(&econet_sklist, sk);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now the socket is dead. No more input will appear.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sk->sk_state_change(sk); /* It is useless. Just for sanity. */
|
|
|
|
|
2008-06-17 06:01:47 -04:00
|
|
|
sock_orphan(sk);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/* Purge queues */
|
|
|
|
|
|
|
|
skb_queue_purge(&sk->sk_receive_queue);
|
|
|
|
|
2009-06-16 06:12:03 -04:00
|
|
|
if (sk_has_allocations(sk)) {
|
2005-04-16 18:20:36 -04:00
|
|
|
sk->sk_timer.data = (unsigned long)sk;
|
|
|
|
sk->sk_timer.expires = jiffies + HZ;
|
|
|
|
sk->sk_timer.function = econet_destroy_timer;
|
|
|
|
add_timer(&sk->sk_timer);
|
2006-03-28 03:01:55 -05:00
|
|
|
|
|
|
|
goto out_unlock;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_free(sk);
|
2006-03-28 03:01:55 -05:00
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&econet_mutex);
|
2005-04-16 18:20:36 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct proto econet_proto = {
|
|
|
|
.name = "ECONET",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.obj_size = sizeof(struct econet_sock),
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an Econet socket
|
|
|
|
*/
|
|
|
|
|
2009-11-06 01:18:14 -05:00
|
|
|
static int econet_create(struct net *net, struct socket *sock, int protocol,
|
|
|
|
int kern)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
struct econet_sock *eo;
|
|
|
|
int err;
|
|
|
|
|
2009-11-25 18:14:13 -05:00
|
|
|
if (!net_eq(net, &init_net))
|
2007-10-09 02:24:22 -04:00
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Econet only provides datagram services. */
|
|
|
|
if (sock->type != SOCK_DGRAM)
|
|
|
|
return -ESOCKTNOSUPPORT;
|
|
|
|
|
|
|
|
sock->state = SS_UNCONNECTED;
|
|
|
|
|
|
|
|
err = -ENOBUFS;
|
2007-11-01 03:39:31 -04:00
|
|
|
sk = sk_alloc(net, PF_ECONET, GFP_KERNEL, &econet_proto);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (sk == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
sk->sk_reuse = 1;
|
|
|
|
sock->ops = &econet_ops;
|
|
|
|
sock_init_data(sock, sk);
|
|
|
|
|
|
|
|
eo = ec_sk(sk);
|
|
|
|
sock_reset_flag(sk, SOCK_ZAPPED);
|
|
|
|
sk->sk_family = PF_ECONET;
|
|
|
|
eo->num = protocol;
|
|
|
|
|
|
|
|
econet_insert_socket(&econet_sklist, sk);
|
2010-09-22 16:43:57 -04:00
|
|
|
return 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle Econet specific ioctls
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int ec_dev_ioctl(struct socket *sock, unsigned int cmd, void __user *arg)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
struct ec_device *edev;
|
|
|
|
struct net_device *dev;
|
|
|
|
struct sockaddr_ec *sec;
|
2006-03-28 03:01:55 -05:00
|
|
|
int err;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch the caller's info block into kernel space
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
dev = dev_get_by_name(&init_net, ifr.ifr_name);
|
|
|
|
if (dev == NULL)
|
2005-04-16 18:20:36 -04:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
sec = (struct sockaddr_ec *)&ifr.ifr_addr;
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_lock(&econet_mutex);
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
switch (cmd) {
|
2005-04-16 18:20:36 -04:00
|
|
|
case SIOCSIFADDR:
|
2010-12-08 13:13:55 -05:00
|
|
|
if (!capable(CAP_NET_ADMIN)) {
|
|
|
|
err = -EPERM;
|
|
|
|
break;
|
|
|
|
}
|
2010-11-24 14:49:53 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
edev = dev->ec_ptr;
|
2006-03-28 03:01:55 -05:00
|
|
|
if (edev == NULL) {
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Magic up a new one. */
|
2006-07-21 17:51:30 -04:00
|
|
|
edev = kzalloc(sizeof(struct ec_device), GFP_KERNEL);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (edev == NULL) {
|
2006-03-28 03:01:55 -05:00
|
|
|
err = -ENOMEM;
|
|
|
|
break;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
dev->ec_ptr = edev;
|
2006-03-28 03:01:55 -05:00
|
|
|
} else
|
2005-04-16 18:20:36 -04:00
|
|
|
net2dev_map[edev->net] = NULL;
|
|
|
|
edev->station = sec->addr.station;
|
|
|
|
edev->net = sec->addr.net;
|
|
|
|
net2dev_map[sec->addr.net] = dev;
|
|
|
|
if (!net2dev_map[0])
|
|
|
|
net2dev_map[0] = dev;
|
2006-03-28 03:01:55 -05:00
|
|
|
break;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
edev = dev->ec_ptr;
|
2006-03-28 03:01:55 -05:00
|
|
|
if (edev == NULL) {
|
|
|
|
err = -ENODEV;
|
|
|
|
break;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
memset(sec, 0, sizeof(struct sockaddr_ec));
|
|
|
|
sec->addr.station = edev->station;
|
|
|
|
sec->addr.net = edev->net;
|
|
|
|
sec->sec_family = AF_ECONET;
|
|
|
|
dev_put(dev);
|
|
|
|
if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
|
2006-03-28 03:01:55 -05:00
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
mutex_unlock(&econet_mutex);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
dev_put(dev);
|
2006-03-28 03:01:55 -05:00
|
|
|
|
|
|
|
return err;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle generic ioctls
|
|
|
|
*/
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
static int econet_ioctl(struct socket *sock, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
void __user *argp = (void __user *)arg;
|
|
|
|
|
2011-07-01 05:43:04 -04:00
|
|
|
switch (cmd) {
|
|
|
|
case SIOCGSTAMP:
|
|
|
|
return sock_get_timestamp(sk, argp);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2011-07-01 05:43:04 -04:00
|
|
|
case SIOCGSTAMPNS:
|
|
|
|
return sock_get_timestampns(sk, argp);
|
2007-03-18 20:33:16 -04:00
|
|
|
|
2011-07-01 05:43:04 -04:00
|
|
|
case SIOCSIFADDR:
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
return ec_dev_ioctl(sock, cmd, argp);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
}
|
2011-07-01 05:43:04 -04:00
|
|
|
|
|
|
|
return -ENOIOCTLCMD;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2009-10-05 01:58:39 -04:00
|
|
|
static const struct net_proto_family econet_family_ops = {
|
2005-04-16 18:20:36 -04:00
|
|
|
.family = PF_ECONET,
|
|
|
|
.create = econet_create,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-03-28 03:01:55 -05:00
|
|
|
static const struct proto_ops econet_ops = {
|
2005-04-16 18:20:36 -04:00
|
|
|
.family = PF_ECONET,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.release = econet_release,
|
|
|
|
.bind = econet_bind,
|
|
|
|
.connect = sock_no_connect,
|
|
|
|
.socketpair = sock_no_socketpair,
|
|
|
|
.accept = sock_no_accept,
|
2007-02-09 09:24:42 -05:00
|
|
|
.getname = econet_getname,
|
2005-04-16 18:20:36 -04:00
|
|
|
.poll = datagram_poll,
|
|
|
|
.ioctl = econet_ioctl,
|
|
|
|
.listen = sock_no_listen,
|
|
|
|
.shutdown = sock_no_shutdown,
|
|
|
|
.setsockopt = sock_no_setsockopt,
|
|
|
|
.getsockopt = sock_no_getsockopt,
|
|
|
|
.sendmsg = econet_sendmsg,
|
|
|
|
.recvmsg = econet_recvmsg,
|
|
|
|
.mmap = sock_no_mmap,
|
|
|
|
.sendpage = sock_no_sendpage,
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(CONFIG_ECONET_AUNUDP) || defined(CONFIG_ECONET_NATIVE)
|
|
|
|
/*
|
|
|
|
* Find the listening socket, if any, for the given data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct sock *ec_listening_socket(unsigned char port, unsigned char
|
|
|
|
station, unsigned char net)
|
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
struct hlist_node *node;
|
|
|
|
|
2010-06-09 12:33:05 -04:00
|
|
|
spin_lock(&econet_lock);
|
2005-04-16 18:20:36 -04:00
|
|
|
sk_for_each(sk, node, &econet_sklist) {
|
|
|
|
struct econet_sock *opt = ec_sk(sk);
|
2007-02-09 09:24:42 -05:00
|
|
|
if ((opt->port == port || opt->port == 0) &&
|
2005-04-16 18:20:36 -04:00
|
|
|
(opt->station == station || opt->station == 0) &&
|
2010-06-09 12:33:05 -04:00
|
|
|
(opt->net == net || opt->net == 0)) {
|
|
|
|
sock_hold(sk);
|
2005-04-16 18:20:36 -04:00
|
|
|
goto found;
|
2010-06-09 12:33:05 -04:00
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
sk = NULL;
|
|
|
|
found:
|
2010-06-09 12:33:05 -04:00
|
|
|
spin_unlock(&econet_lock);
|
2005-04-16 18:20:36 -04:00
|
|
|
return sk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Queue a received packet for a socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int ec_queue_packet(struct sock *sk, struct sk_buff *skb,
|
|
|
|
unsigned char stn, unsigned char net,
|
|
|
|
unsigned char cb, unsigned char port)
|
|
|
|
{
|
|
|
|
struct ec_cb *eb = (struct ec_cb *)&skb->cb;
|
|
|
|
struct sockaddr_ec *sec = (struct sockaddr_ec *)&eb->sec;
|
|
|
|
|
|
|
|
memset(sec, 0, sizeof(struct sockaddr_ec));
|
|
|
|
sec->sec_family = AF_ECONET;
|
|
|
|
sec->type = ECTYPE_PACKET_RECEIVED;
|
|
|
|
sec->port = port;
|
|
|
|
sec->cb = cb;
|
|
|
|
sec->addr.net = net;
|
|
|
|
sec->addr.station = stn;
|
|
|
|
|
|
|
|
return sock_queue_rcv_skb(sk, skb);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ECONET_AUNUDP
|
|
|
|
/*
|
2007-02-09 09:24:42 -05:00
|
|
|
* Send an AUN protocol response.
|
2005-04-16 18:20:36 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void aun_send_response(__u32 addr, unsigned long seq, int code, int cb)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sin = {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_port = htons(AUN_PORT),
|
|
|
|
.sin_addr = {.s_addr = addr}
|
|
|
|
};
|
|
|
|
struct aunhdr ah = {.code = code, .cb = cb, .handle = seq};
|
|
|
|
struct kvec iov = {.iov_base = (void *)&ah, .iov_len = sizeof(ah)};
|
|
|
|
struct msghdr udpmsg;
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
udpmsg.msg_name = (void *)&sin;
|
|
|
|
udpmsg.msg_namelen = sizeof(sin);
|
|
|
|
udpmsg.msg_control = NULL;
|
|
|
|
udpmsg.msg_controllen = 0;
|
2011-07-03 04:28:33 -04:00
|
|
|
udpmsg.msg_flags = 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
kernel_sendmsg(udpsock, &udpmsg, &iov, 1, sizeof(ah));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle incoming AUN packets. Work out if anybody wants them,
|
|
|
|
* and send positive or negative acknowledgements as appropriate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void aun_incoming(struct sk_buff *skb, struct aunhdr *ah, size_t len)
|
|
|
|
{
|
2007-04-21 01:47:35 -04:00
|
|
|
struct iphdr *ip = ip_hdr(skb);
|
2005-04-16 18:20:36 -04:00
|
|
|
unsigned char stn = ntohl(ip->saddr) & 0xff;
|
2010-12-08 21:42:23 -05:00
|
|
|
struct dst_entry *dst = skb_dst(skb);
|
|
|
|
struct ec_device *edev = NULL;
|
2010-06-09 12:33:05 -04:00
|
|
|
struct sock *sk = NULL;
|
2005-04-16 18:20:36 -04:00
|
|
|
struct sk_buff *newskb;
|
2010-12-08 21:42:23 -05:00
|
|
|
|
|
|
|
if (dst)
|
|
|
|
edev = dst->dev->ec_ptr;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
if (!edev)
|
2005-04-16 18:20:36 -04:00
|
|
|
goto bad;
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
sk = ec_listening_socket(ah->port, stn, edev->net);
|
|
|
|
if (sk == NULL)
|
2005-04-16 18:20:36 -04:00
|
|
|
goto bad; /* Nobody wants it */
|
|
|
|
|
2007-02-09 09:24:42 -05:00
|
|
|
newskb = alloc_skb((len - sizeof(struct aunhdr) + 15) & ~15,
|
2005-04-16 18:20:36 -04:00
|
|
|
GFP_ATOMIC);
|
2011-07-03 04:28:33 -04:00
|
|
|
if (newskb == NULL) {
|
|
|
|
pr_debug("AUN: memory squeeze, dropping packet\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Send nack and hope sender tries again */
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
memcpy(skb_put(newskb, len - sizeof(struct aunhdr)), (void *)(ah + 1),
|
2005-04-16 18:20:36 -04:00
|
|
|
len - sizeof(struct aunhdr));
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
if (ec_queue_packet(sk, newskb, stn, edev->net, ah->cb, ah->port)) {
|
2005-04-16 18:20:36 -04:00
|
|
|
/* Socket is bankrupt. */
|
|
|
|
kfree_skb(newskb);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
aun_send_response(ip->saddr, ah->handle, 3, 0);
|
2010-06-09 12:33:05 -04:00
|
|
|
sock_put(sk);
|
2005-04-16 18:20:36 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
aun_send_response(ip->saddr, ah->handle, 4, 0);
|
2010-06-09 12:33:05 -04:00
|
|
|
if (sk)
|
|
|
|
sock_put(sk);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle incoming AUN transmit acknowledgements. If the sequence
|
|
|
|
* number matches something in our backlog then kill it and tell
|
|
|
|
* the user. If the remote took too long to reply then we may have
|
|
|
|
* dropped the packet already.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void aun_tx_ack(unsigned long seq, int result)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
unsigned long flags;
|
|
|
|
struct ec_cb *eb;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&aun_queue_lock, flags);
|
2009-05-28 19:46:29 -04:00
|
|
|
skb_queue_walk(&aun_queue, skb) {
|
2005-04-16 18:20:36 -04:00
|
|
|
eb = (struct ec_cb *)&skb->cb;
|
|
|
|
if (eb->seq == seq)
|
|
|
|
goto foundit;
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&aun_queue_lock, flags);
|
2011-07-03 04:28:33 -04:00
|
|
|
pr_debug("AUN: unknown sequence %ld\n", seq);
|
2005-04-16 18:20:36 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
foundit:
|
|
|
|
tx_result(skb->sk, eb->cookie, result);
|
2005-08-09 22:25:21 -04:00
|
|
|
skb_unlink(skb, &aun_queue);
|
2005-04-16 18:20:36 -04:00
|
|
|
spin_unlock_irqrestore(&aun_queue_lock, flags);
|
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with received AUN frames - sort out what type of thing it is
|
|
|
|
* and hand it to the right function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void aun_data_available(struct sock *sk, int slen)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
unsigned char *data;
|
|
|
|
struct aunhdr *ah;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
while ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) {
|
|
|
|
if (err == -EAGAIN) {
|
2011-07-03 04:28:33 -04:00
|
|
|
pr_err("AUN: no data available?!\n");
|
2005-04-16 18:20:36 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-07-03 04:28:33 -04:00
|
|
|
pr_debug("AUN: recvfrom() error %d\n", -err);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2007-04-25 21:04:18 -04:00
|
|
|
data = skb_transport_header(skb) + sizeof(struct udphdr);
|
2005-04-16 18:20:36 -04:00
|
|
|
ah = (struct aunhdr *)data;
|
|
|
|
len = skb->len - sizeof(struct udphdr);
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
switch (ah->code) {
|
2005-04-16 18:20:36 -04:00
|
|
|
case 2:
|
|
|
|
aun_incoming(skb, ah, len);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_OK);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
aun_tx_ack(ah->handle, ECTYPE_TRANSMIT_NOT_LISTENING);
|
|
|
|
break;
|
|
|
|
default:
|
2011-07-03 04:28:33 -04:00
|
|
|
pr_debug("AUN: unknown packet type: %d\n", data[0]);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
skb_free_datagram(sk, skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the timer to manage the AUN transmit queue. If a packet
|
|
|
|
* was sent to a dead or nonexistent host then we will never get an
|
|
|
|
* acknowledgement back. After a few seconds we need to spot this and
|
|
|
|
* drop the packet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void ab_cleanup(unsigned long h)
|
|
|
|
{
|
2009-05-28 19:46:29 -04:00
|
|
|
struct sk_buff *skb, *n;
|
2005-04-16 18:20:36 -04:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&aun_queue_lock, flags);
|
2009-05-28 19:46:29 -04:00
|
|
|
skb_queue_walk_safe(&aun_queue, skb, n) {
|
2005-04-16 18:20:36 -04:00
|
|
|
struct ec_cb *eb = (struct ec_cb *)&skb->cb;
|
2009-05-28 19:46:29 -04:00
|
|
|
if ((jiffies - eb->start) > eb->timeout) {
|
2007-02-09 09:24:42 -05:00
|
|
|
tx_result(skb->sk, eb->cookie,
|
2005-04-16 18:20:36 -04:00
|
|
|
ECTYPE_TRANSMIT_NOT_PRESENT);
|
2005-08-09 22:25:21 -04:00
|
|
|
skb_unlink(skb, &aun_queue);
|
2005-04-16 18:20:36 -04:00
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&aun_queue_lock, flags);
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
mod_timer(&ab_cleanup_timer, jiffies + (HZ * 2));
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __init aun_udp_initialise(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
skb_queue_head_init(&aun_queue);
|
2008-01-24 00:20:07 -05:00
|
|
|
setup_timer(&ab_cleanup_timer, ab_cleanup, 0);
|
2011-07-03 04:28:33 -04:00
|
|
|
ab_cleanup_timer.expires = jiffies + (HZ * 2);
|
2005-04-16 18:20:36 -04:00
|
|
|
add_timer(&ab_cleanup_timer);
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_port = htons(AUN_PORT);
|
|
|
|
|
|
|
|
/* We can count ourselves lucky Acorn machines are too dim to
|
|
|
|
speak IPv6. :-) */
|
2011-07-03 04:28:33 -04:00
|
|
|
error = sock_create_kern(PF_INET, SOCK_DGRAM, 0, &udpsock);
|
|
|
|
if (error < 0) {
|
|
|
|
pr_err("AUN: socket error %d\n", -error);
|
2005-04-16 18:20:36 -04:00
|
|
|
return error;
|
|
|
|
}
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
udpsock->sk->sk_reuse = 1;
|
|
|
|
udpsock->sk->sk_allocation = GFP_ATOMIC; /* we're going to call it
|
|
|
|
from interrupts */
|
2007-02-09 09:24:42 -05:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
error = udpsock->ops->bind(udpsock, (struct sockaddr *)&sin,
|
2011-07-03 04:28:33 -04:00
|
|
|
sizeof(sin));
|
|
|
|
if (error < 0) {
|
|
|
|
pr_err("AUN: bind error %d\n", -error);
|
2005-04-16 18:20:36 -04:00
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
|
|
|
|
udpsock->sk->sk_data_ready = aun_data_available;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
release:
|
|
|
|
sock_release(udpsock);
|
|
|
|
udpsock = NULL;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ECONET_NATIVE
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive an Econet frame from a device.
|
|
|
|
*/
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
static int econet_rcv(struct sk_buff *skb, struct net_device *dev,
|
|
|
|
struct packet_type *pt, struct net_device *orig_dev)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
|
|
|
struct ec_framehdr *hdr;
|
2010-06-09 12:33:05 -04:00
|
|
|
struct sock *sk = NULL;
|
2005-04-16 18:20:36 -04:00
|
|
|
struct ec_device *edev = dev->ec_ptr;
|
|
|
|
|
2008-07-20 01:34:43 -04:00
|
|
|
if (!net_eq(dev_net(dev), &init_net))
|
2007-09-17 14:53:39 -04:00
|
|
|
goto drop;
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
if (skb->pkt_type == PACKET_OTHERHOST)
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
if (!edev)
|
|
|
|
goto drop;
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
skb = skb_share_check(skb, GFP_ATOMIC);
|
|
|
|
if (skb == NULL)
|
2005-04-16 18:20:36 -04:00
|
|
|
return NET_RX_DROP;
|
|
|
|
|
|
|
|
if (!pskb_may_pull(skb, sizeof(struct ec_framehdr)))
|
|
|
|
goto drop;
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
hdr = (struct ec_framehdr *)skb->data;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/* First check for encapsulated IP */
|
|
|
|
if (hdr->port == EC_PORT_IP) {
|
|
|
|
skb->protocol = htons(ETH_P_IP);
|
|
|
|
skb_pull(skb, sizeof(struct ec_framehdr));
|
|
|
|
netif_rx(skb);
|
2009-07-06 07:05:58 -04:00
|
|
|
return NET_RX_SUCCESS;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sk = ec_listening_socket(hdr->port, hdr->src_stn, hdr->src_net);
|
|
|
|
if (!sk)
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
if (ec_queue_packet(sk, skb, edev->net, hdr->src_stn, hdr->cb,
|
|
|
|
hdr->port))
|
|
|
|
goto drop;
|
2010-06-09 12:33:05 -04:00
|
|
|
sock_put(sk);
|
2009-07-06 07:05:58 -04:00
|
|
|
return NET_RX_SUCCESS;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
drop:
|
2010-06-09 12:33:05 -04:00
|
|
|
if (sk)
|
|
|
|
sock_put(sk);
|
2005-04-16 18:20:36 -04:00
|
|
|
kfree_skb(skb);
|
|
|
|
return NET_RX_DROP;
|
|
|
|
}
|
|
|
|
|
2009-03-09 04:18:29 -04:00
|
|
|
static struct packet_type econet_packet_type __read_mostly = {
|
2011-07-03 04:28:33 -04:00
|
|
|
.type = cpu_to_be16(ETH_P_ECONET),
|
|
|
|
.func = econet_rcv,
|
2005-04-16 18:20:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void econet_hw_initialise(void)
|
|
|
|
{
|
|
|
|
dev_add_pack(&econet_packet_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-03 04:28:33 -04:00
|
|
|
static int econet_notifier(struct notifier_block *this, unsigned long msg,
|
|
|
|
void *data)
|
2005-04-16 18:20:36 -04:00
|
|
|
{
|
2011-07-03 04:28:33 -04:00
|
|
|
struct net_device *dev = data;
|
2005-04-16 18:20:36 -04:00
|
|
|
struct ec_device *edev;
|
|
|
|
|
2008-07-20 01:34:43 -04:00
|
|
|
if (!net_eq(dev_net(dev), &init_net))
|
2007-09-12 07:02:17 -04:00
|
|
|
return NOTIFY_DONE;
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
switch (msg) {
|
|
|
|
case NETDEV_UNREGISTER:
|
|
|
|
/* A device has gone down - kill any data we hold for it. */
|
|
|
|
edev = dev->ec_ptr;
|
2011-07-03 04:28:33 -04:00
|
|
|
if (edev) {
|
2005-04-16 18:20:36 -04:00
|
|
|
if (net2dev_map[0] == dev)
|
|
|
|
net2dev_map[0] = NULL;
|
|
|
|
net2dev_map[edev->net] = NULL;
|
|
|
|
kfree(edev);
|
|
|
|
dev->ec_ptr = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block econet_netdev_notifier = {
|
2011-07-03 04:28:33 -04:00
|
|
|
.notifier_call = econet_notifier,
|
2005-04-16 18:20:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void __exit econet_proto_exit(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ECONET_AUNUDP
|
|
|
|
del_timer(&ab_cleanup_timer);
|
|
|
|
if (udpsock)
|
|
|
|
sock_release(udpsock);
|
|
|
|
#endif
|
|
|
|
unregister_netdevice_notifier(&econet_netdev_notifier);
|
2007-08-14 20:25:20 -04:00
|
|
|
#ifdef CONFIG_ECONET_NATIVE
|
|
|
|
dev_remove_pack(&econet_packet_type);
|
|
|
|
#endif
|
2005-04-16 18:20:36 -04:00
|
|
|
sock_unregister(econet_family_ops.family);
|
|
|
|
proto_unregister(&econet_proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init econet_proto_init(void)
|
|
|
|
{
|
|
|
|
int err = proto_register(&econet_proto, 0);
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
goto out;
|
|
|
|
sock_register(&econet_family_ops);
|
|
|
|
#ifdef CONFIG_ECONET_AUNUDP
|
|
|
|
aun_udp_initialise();
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ECONET_NATIVE
|
|
|
|
econet_hw_initialise();
|
|
|
|
#endif
|
|
|
|
register_netdevice_notifier(&econet_netdev_notifier);
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(econet_proto_init);
|
|
|
|
module_exit(econet_proto_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_NETPROTO(PF_ECONET);
|