2009-06-08 20:18:48 +08:00
|
|
|
/*
|
|
|
|
* IEEE802154.4 socket interface
|
|
|
|
*
|
|
|
|
* Copyright 2007, 2008 Siemens AG
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* Written by:
|
|
|
|
* Sergey Lapin <slapin@ossfans.org>
|
|
|
|
* Maxim Gorbachyov <maxim.gorbachev@siemens.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/capability.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/termios.h> /* For TIOCOUTQ/INQ */
|
|
|
|
#include <linux/list.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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2009-06-08 20:18:48 +08:00
|
|
|
#include <net/datalink.h>
|
|
|
|
#include <net/psnap.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/tcp_states.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
2009-07-23 20:56:29 +08:00
|
|
|
#include <net/af_ieee802154.h>
|
|
|
|
#include <net/ieee802154_netdev.h>
|
2009-06-08 20:18:48 +08:00
|
|
|
|
|
|
|
#include "af802154.h"
|
|
|
|
|
2014-07-02 11:31:09 +08:00
|
|
|
/* Utility function for families */
|
2014-03-15 04:24:01 +08:00
|
|
|
struct net_device*
|
|
|
|
ieee802154_get_dev(struct net *net, const struct ieee802154_addr *addr)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
struct net_device *dev = NULL;
|
|
|
|
struct net_device *tmp;
|
2014-03-15 04:23:59 +08:00
|
|
|
__le16 pan_id, short_addr;
|
2014-03-15 04:24:01 +08:00
|
|
|
u8 hwaddr[IEEE802154_ADDR_LEN];
|
2009-06-08 20:18:48 +08:00
|
|
|
|
2014-03-15 04:24:01 +08:00
|
|
|
switch (addr->mode) {
|
2009-06-08 20:18:48 +08:00
|
|
|
case IEEE802154_ADDR_LONG:
|
2014-03-15 04:24:01 +08:00
|
|
|
ieee802154_devaddr_to_raw(hwaddr, addr->extended_addr);
|
2010-12-05 09:23:53 +08:00
|
|
|
rcu_read_lock();
|
2014-03-15 04:24:01 +08:00
|
|
|
dev = dev_getbyhwaddr_rcu(net, ARPHRD_IEEE802154, hwaddr);
|
2009-06-08 20:18:48 +08:00
|
|
|
if (dev)
|
|
|
|
dev_hold(dev);
|
2010-12-05 09:23:53 +08:00
|
|
|
rcu_read_unlock();
|
2009-06-08 20:18:48 +08:00
|
|
|
break;
|
|
|
|
case IEEE802154_ADDR_SHORT:
|
2014-03-15 04:24:01 +08:00
|
|
|
if (addr->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST) ||
|
|
|
|
addr->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
|
2014-03-20 21:57:02 +08:00
|
|
|
addr->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST))
|
2009-06-08 20:18:48 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
rtnl_lock();
|
|
|
|
|
|
|
|
for_each_netdev(net, tmp) {
|
|
|
|
if (tmp->type != ARPHRD_IEEE802154)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pan_id = ieee802154_mlme_ops(tmp)->get_pan_id(tmp);
|
|
|
|
short_addr =
|
|
|
|
ieee802154_mlme_ops(tmp)->get_short_addr(tmp);
|
|
|
|
|
2014-03-15 04:24:01 +08:00
|
|
|
if (pan_id == addr->pan_id &&
|
|
|
|
short_addr == addr->short_addr) {
|
2009-06-08 20:18:48 +08:00
|
|
|
dev = tmp;
|
|
|
|
dev_hold(dev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rtnl_unlock();
|
|
|
|
break;
|
|
|
|
default:
|
2014-07-02 11:31:09 +08:00
|
|
|
pr_warn("Unsupported ieee802154 address type: %d\n",
|
|
|
|
addr->mode);
|
2009-06-08 20:18:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ieee802154_sock_release(struct socket *sock)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
|
|
if (sk) {
|
|
|
|
sock->sk = NULL;
|
|
|
|
sk->sk_prot->close(sk, 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-04 18:51:59 +08:00
|
|
|
|
2009-06-08 20:18:48 +08:00
|
|
|
static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
|
2014-07-02 11:31:09 +08:00
|
|
|
struct msghdr *msg, size_t len)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
|
|
return sk->sk_prot->sendmsg(iocb, sk, msg, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
|
2014-07-02 11:31:09 +08:00
|
|
|
int addr_len)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
|
|
if (sk->sk_prot->bind)
|
|
|
|
return sk->sk_prot->bind(sk, uaddr, addr_len);
|
|
|
|
|
|
|
|
return sock_no_bind(sock, uaddr, addr_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ieee802154_sock_connect(struct socket *sock, struct sockaddr *uaddr,
|
2014-07-02 11:31:09 +08:00
|
|
|
int addr_len, int flags)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
2010-04-01 06:58:26 +08:00
|
|
|
if (addr_len < sizeof(uaddr->sa_family))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-06-08 20:18:48 +08:00
|
|
|
if (uaddr->sa_family == AF_UNSPEC)
|
|
|
|
return sk->sk_prot->disconnect(sk, flags);
|
|
|
|
|
|
|
|
return sk->sk_prot->connect(sk, uaddr, addr_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ieee802154_dev_ioctl(struct sock *sk, struct ifreq __user *arg,
|
2014-07-02 11:31:09 +08:00
|
|
|
unsigned int cmd)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
2009-08-02 18:12:01 +08:00
|
|
|
int ret = -ENOIOCTLCMD;
|
2009-06-08 20:18:48 +08:00
|
|
|
struct net_device *dev;
|
|
|
|
|
|
|
|
if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
ifr.ifr_name[IFNAMSIZ-1] = 0;
|
|
|
|
|
|
|
|
dev_load(sock_net(sk), ifr.ifr_name);
|
|
|
|
dev = dev_get_by_name(sock_net(sk), ifr.ifr_name);
|
2009-08-02 18:12:01 +08:00
|
|
|
|
2010-04-27 02:20:32 +08:00
|
|
|
if (!dev)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2009-08-15 00:00:20 +08:00
|
|
|
if (dev->type == ARPHRD_IEEE802154 && dev->netdev_ops->ndo_do_ioctl)
|
2009-06-08 20:18:48 +08:00
|
|
|
ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, cmd);
|
|
|
|
|
|
|
|
if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
dev_put(dev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ieee802154_sock_ioctl(struct socket *sock, unsigned int cmd,
|
2014-07-02 11:31:09 +08:00
|
|
|
unsigned long arg)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SIOCGSTAMP:
|
|
|
|
return sock_get_timestamp(sk, (struct timeval __user *)arg);
|
|
|
|
case SIOCGSTAMPNS:
|
|
|
|
return sock_get_timestampns(sk, (struct timespec __user *)arg);
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
case SIOCSIFADDR:
|
|
|
|
return ieee802154_dev_ioctl(sk, (struct ifreq __user *)arg,
|
|
|
|
cmd);
|
|
|
|
default:
|
|
|
|
if (!sk->sk_prot->ioctl)
|
|
|
|
return -ENOIOCTLCMD;
|
|
|
|
return sk->sk_prot->ioctl(sk, cmd, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct proto_ops ieee802154_raw_ops = {
|
|
|
|
.family = PF_IEEE802154,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.release = ieee802154_sock_release,
|
|
|
|
.bind = ieee802154_sock_bind,
|
|
|
|
.connect = ieee802154_sock_connect,
|
|
|
|
.socketpair = sock_no_socketpair,
|
|
|
|
.accept = sock_no_accept,
|
|
|
|
.getname = sock_no_getname,
|
|
|
|
.poll = datagram_poll,
|
|
|
|
.ioctl = ieee802154_sock_ioctl,
|
|
|
|
.listen = sock_no_listen,
|
|
|
|
.shutdown = sock_no_shutdown,
|
|
|
|
.setsockopt = sock_common_setsockopt,
|
|
|
|
.getsockopt = sock_common_getsockopt,
|
|
|
|
.sendmsg = ieee802154_sock_sendmsg,
|
|
|
|
.recvmsg = sock_common_recvmsg,
|
|
|
|
.mmap = sock_no_mmap,
|
|
|
|
.sendpage = sock_no_sendpage,
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_setsockopt = compat_sock_common_setsockopt,
|
|
|
|
.compat_getsockopt = compat_sock_common_getsockopt,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct proto_ops ieee802154_dgram_ops = {
|
|
|
|
.family = PF_IEEE802154,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.release = ieee802154_sock_release,
|
|
|
|
.bind = ieee802154_sock_bind,
|
|
|
|
.connect = ieee802154_sock_connect,
|
|
|
|
.socketpair = sock_no_socketpair,
|
|
|
|
.accept = sock_no_accept,
|
|
|
|
.getname = sock_no_getname,
|
|
|
|
.poll = datagram_poll,
|
|
|
|
.ioctl = ieee802154_sock_ioctl,
|
|
|
|
.listen = sock_no_listen,
|
|
|
|
.shutdown = sock_no_shutdown,
|
|
|
|
.setsockopt = sock_common_setsockopt,
|
|
|
|
.getsockopt = sock_common_getsockopt,
|
|
|
|
.sendmsg = ieee802154_sock_sendmsg,
|
|
|
|
.recvmsg = sock_common_recvmsg,
|
|
|
|
.mmap = sock_no_mmap,
|
|
|
|
.sendpage = sock_no_sendpage,
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_setsockopt = compat_sock_common_setsockopt,
|
|
|
|
.compat_getsockopt = compat_sock_common_getsockopt,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-07-02 11:31:09 +08:00
|
|
|
/* Create a socket. Initialise the socket, blank the addresses
|
2009-06-08 20:18:48 +08:00
|
|
|
* set the state.
|
|
|
|
*/
|
|
|
|
static int ieee802154_create(struct net *net, struct socket *sock,
|
2009-11-06 14:18:14 +08:00
|
|
|
int protocol, int kern)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
struct sock *sk;
|
|
|
|
int rc;
|
|
|
|
struct proto *proto;
|
|
|
|
const struct proto_ops *ops;
|
|
|
|
|
2009-11-26 07:14:13 +08:00
|
|
|
if (!net_eq(net, &init_net))
|
2009-06-08 20:18:48 +08:00
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
|
|
|
|
switch (sock->type) {
|
|
|
|
case SOCK_RAW:
|
|
|
|
proto = &ieee802154_raw_prot;
|
|
|
|
ops = &ieee802154_raw_ops;
|
|
|
|
break;
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
proto = &ieee802154_dgram_prot;
|
|
|
|
ops = &ieee802154_dgram_ops;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = -ESOCKTNOSUPPORT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = -ENOMEM;
|
|
|
|
sk = sk_alloc(net, PF_IEEE802154, GFP_KERNEL, proto);
|
|
|
|
if (!sk)
|
|
|
|
goto out;
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
sock->ops = ops;
|
|
|
|
|
|
|
|
sock_init_data(sock, sk);
|
|
|
|
/* FIXME: sk->sk_destruct */
|
|
|
|
sk->sk_family = PF_IEEE802154;
|
|
|
|
|
|
|
|
/* Checksums on by default */
|
|
|
|
sock_set_flag(sk, SOCK_ZAPPED);
|
|
|
|
|
|
|
|
if (sk->sk_prot->hash)
|
|
|
|
sk->sk_prot->hash(sk);
|
|
|
|
|
|
|
|
if (sk->sk_prot->init) {
|
|
|
|
rc = sk->sk_prot->init(sk);
|
|
|
|
if (rc)
|
|
|
|
sk_common_release(sk);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-10-05 13:58:39 +08:00
|
|
|
static const struct net_proto_family ieee802154_family_ops = {
|
2009-06-08 20:18:48 +08:00
|
|
|
.family = PF_IEEE802154,
|
|
|
|
.create = ieee802154_create,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ieee802154_rcv(struct sk_buff *skb, struct net_device *dev,
|
2014-07-02 11:31:09 +08:00
|
|
|
struct packet_type *pt, struct net_device *orig_dev)
|
2009-06-08 20:18:48 +08:00
|
|
|
{
|
|
|
|
if (!netif_running(dev))
|
2011-06-29 20:51:37 +08:00
|
|
|
goto drop;
|
2009-06-08 20:18:48 +08:00
|
|
|
pr_debug("got frame, type %d, dev %p\n", dev->type, dev);
|
2009-06-18 12:16:46 +08:00
|
|
|
#ifdef DEBUG
|
2014-07-02 11:31:09 +08:00
|
|
|
print_hex_dump_bytes("ieee802154_rcv ",
|
|
|
|
DUMP_PREFIX_NONE, skb->data, skb->len);
|
2009-06-18 12:16:46 +08:00
|
|
|
#endif
|
2009-06-08 20:18:48 +08:00
|
|
|
|
|
|
|
if (!net_eq(dev_net(dev), &init_net))
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
ieee802154_raw_deliver(dev, skb);
|
|
|
|
|
|
|
|
if (dev->type != ARPHRD_IEEE802154)
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
if (skb->pkt_type != PACKET_OTHERHOST)
|
|
|
|
return ieee802154_dgram_deliver(dev, skb);
|
|
|
|
|
|
|
|
drop:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return NET_RX_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct packet_type ieee802154_packet_type = {
|
2014-03-13 01:04:18 +08:00
|
|
|
.type = htons(ETH_P_IEEE802154),
|
2009-06-08 20:18:48 +08:00
|
|
|
.func = ieee802154_rcv,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init af_ieee802154_init(void)
|
|
|
|
{
|
|
|
|
int rc = -EINVAL;
|
|
|
|
|
|
|
|
rc = proto_register(&ieee802154_raw_prot, 1);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rc = proto_register(&ieee802154_dgram_prot, 1);
|
|
|
|
if (rc)
|
|
|
|
goto err_dgram;
|
|
|
|
|
|
|
|
/* Tell SOCKET that we are alive */
|
|
|
|
rc = sock_register(&ieee802154_family_ops);
|
|
|
|
if (rc)
|
|
|
|
goto err_sock;
|
|
|
|
dev_add_pack(&ieee802154_packet_type);
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err_sock:
|
|
|
|
proto_unregister(&ieee802154_dgram_prot);
|
|
|
|
err_dgram:
|
|
|
|
proto_unregister(&ieee802154_raw_prot);
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
2014-12-04 18:51:59 +08:00
|
|
|
|
2009-06-08 20:18:48 +08:00
|
|
|
static void __exit af_ieee802154_remove(void)
|
|
|
|
{
|
|
|
|
dev_remove_pack(&ieee802154_packet_type);
|
|
|
|
sock_unregister(PF_IEEE802154);
|
|
|
|
proto_unregister(&ieee802154_dgram_prot);
|
|
|
|
proto_unregister(&ieee802154_raw_prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(af_ieee802154_init);
|
|
|
|
module_exit(af_ieee802154_remove);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_NETPROTO(PF_IEEE802154);
|