2008-09-23 11:02:10 +08:00
|
|
|
/*
|
|
|
|
* File: af_phonet.c
|
|
|
|
*
|
|
|
|
* Phonet protocols family
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Nokia Corporation.
|
|
|
|
*
|
|
|
|
* Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
|
|
|
|
* Original author: Sakari Ailus <sakari.ailus@nokia.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.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>
|
2008-09-23 11:02:10 +08:00
|
|
|
#include <asm/unaligned.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
|
|
|
|
#include <linux/if_phonet.h>
|
|
|
|
#include <linux/phonet.h>
|
|
|
|
#include <net/phonet/phonet.h>
|
2008-09-23 11:03:44 +08:00
|
|
|
#include <net/phonet/pn_dev.h>
|
2008-09-23 11:02:10 +08:00
|
|
|
|
2008-11-20 06:17:41 +08:00
|
|
|
/* Transport protocol registration */
|
|
|
|
static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
|
|
|
|
|
2011-01-10 12:06:58 +08:00
|
|
|
static struct phonet_protocol *phonet_proto_get(unsigned int protocol)
|
2008-11-20 06:17:41 +08:00
|
|
|
{
|
|
|
|
struct phonet_protocol *pp;
|
|
|
|
|
|
|
|
if (protocol >= PHONET_NPROTO)
|
|
|
|
return NULL;
|
|
|
|
|
2009-11-13 13:01:18 +08:00
|
|
|
rcu_read_lock();
|
2009-11-17 06:17:24 +08:00
|
|
|
pp = rcu_dereference(proto_tab[protocol]);
|
2008-11-20 06:17:41 +08:00
|
|
|
if (pp && !try_module_get(pp->prot->owner))
|
|
|
|
pp = NULL;
|
2009-11-13 13:01:18 +08:00
|
|
|
rcu_read_unlock();
|
2008-11-20 06:17:41 +08:00
|
|
|
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void phonet_proto_put(struct phonet_protocol *pp)
|
|
|
|
{
|
|
|
|
module_put(pp->prot->owner);
|
|
|
|
}
|
2008-09-23 11:02:10 +08:00
|
|
|
|
|
|
|
/* protocol family functions */
|
|
|
|
|
2009-11-06 14:18:14 +08:00
|
|
|
static int pn_socket_create(struct net *net, struct socket *sock, int protocol,
|
|
|
|
int kern)
|
2008-09-23 11:02:10 +08:00
|
|
|
{
|
2008-09-23 11:05:19 +08:00
|
|
|
struct sock *sk;
|
|
|
|
struct pn_sock *pn;
|
2008-09-23 11:02:10 +08:00
|
|
|
struct phonet_protocol *pnp;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (protocol == 0) {
|
|
|
|
/* Default protocol selection */
|
|
|
|
switch (sock->type) {
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
protocol = PN_PROTO_PHONET;
|
|
|
|
break;
|
2008-10-06 02:15:13 +08:00
|
|
|
case SOCK_SEQPACKET:
|
|
|
|
protocol = PN_PROTO_PIPE;
|
|
|
|
break;
|
2008-09-23 11:02:10 +08:00
|
|
|
default:
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pnp = phonet_proto_get(protocol);
|
2008-10-06 02:14:27 +08:00
|
|
|
if (pnp == NULL &&
|
|
|
|
request_module("net-pf-%d-proto-%d", PF_PHONET, protocol) == 0)
|
|
|
|
pnp = phonet_proto_get(protocol);
|
2008-10-17 06:24:51 +08:00
|
|
|
|
2008-09-23 11:02:10 +08:00
|
|
|
if (pnp == NULL)
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
if (sock->type != pnp->sock_type) {
|
|
|
|
err = -EPROTONOSUPPORT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-09-23 11:05:19 +08:00
|
|
|
sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
|
|
|
|
if (sk == NULL) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_init_data(sock, sk);
|
|
|
|
sock->state = SS_UNCONNECTED;
|
|
|
|
sock->ops = pnp->ops;
|
|
|
|
sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
|
|
|
|
sk->sk_protocol = protocol;
|
|
|
|
pn = pn_sk(sk);
|
|
|
|
pn->sobject = 0;
|
2011-02-25 07:14:57 +08:00
|
|
|
pn->dobject = 0;
|
2008-09-23 11:05:19 +08:00
|
|
|
pn->resource = 0;
|
|
|
|
sk->sk_prot->init(sk);
|
|
|
|
err = 0;
|
2008-09-23 11:02:10 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
phonet_proto_put(pnp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-10-05 13:58:39 +08:00
|
|
|
static const struct net_proto_family phonet_proto_family = {
|
2008-10-06 02:14:27 +08:00
|
|
|
.family = PF_PHONET,
|
2008-09-23 11:02:10 +08:00
|
|
|
.create = pn_socket_create,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2008-09-23 11:08:04 +08:00
|
|
|
/* Phonet device header operations */
|
|
|
|
static int pn_header_create(struct sk_buff *skb, struct net_device *dev,
|
|
|
|
unsigned short type, const void *daddr,
|
|
|
|
const void *saddr, unsigned len)
|
|
|
|
{
|
|
|
|
u8 *media = skb_push(skb, 1);
|
|
|
|
|
|
|
|
if (type != ETH_P_PHONET)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!saddr)
|
|
|
|
saddr = dev->dev_addr;
|
|
|
|
*media = *(const u8 *)saddr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pn_header_parse(const struct sk_buff *skb, unsigned char *haddr)
|
|
|
|
{
|
|
|
|
const u8 *media = skb_mac_header(skb);
|
|
|
|
*haddr = *media;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct header_ops phonet_header_ops = {
|
|
|
|
.create = pn_header_create,
|
|
|
|
.parse = pn_header_parse,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL(phonet_header_ops);
|
|
|
|
|
2008-09-23 11:05:57 +08:00
|
|
|
/*
|
|
|
|
* Prepends an ISI header and sends a datagram.
|
|
|
|
*/
|
|
|
|
static int pn_send(struct sk_buff *skb, struct net_device *dev,
|
2008-09-23 11:09:13 +08:00
|
|
|
u16 dst, u16 src, u8 res, u8 irq)
|
2008-09-23 11:05:57 +08:00
|
|
|
{
|
|
|
|
struct phonethdr *ph;
|
|
|
|
int err;
|
|
|
|
|
2008-11-17 11:48:49 +08:00
|
|
|
if (skb->len + 2 > 0xffff /* Phonet length field limit */ ||
|
|
|
|
skb->len + sizeof(struct phonethdr) > dev->mtu) {
|
2008-09-23 11:05:57 +08:00
|
|
|
err = -EMSGSIZE;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2009-09-23 11:17:11 +08:00
|
|
|
/* Broadcast sending is not implemented */
|
|
|
|
if (pn_addr(dst) == PNADDR_BROADCAST) {
|
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
2008-09-23 11:05:57 +08:00
|
|
|
skb_reset_transport_header(skb);
|
|
|
|
WARN_ON(skb_headroom(skb) & 1); /* HW assumes word alignment */
|
|
|
|
skb_push(skb, sizeof(struct phonethdr));
|
|
|
|
skb_reset_network_header(skb);
|
|
|
|
ph = pn_hdr(skb);
|
|
|
|
ph->pn_rdev = pn_dev(dst);
|
|
|
|
ph->pn_sdev = pn_dev(src);
|
|
|
|
ph->pn_res = res;
|
|
|
|
ph->pn_length = __cpu_to_be16(skb->len + 2 - sizeof(*ph));
|
|
|
|
ph->pn_robj = pn_obj(dst);
|
|
|
|
ph->pn_sobj = pn_obj(src);
|
|
|
|
|
|
|
|
skb->protocol = htons(ETH_P_PHONET);
|
|
|
|
skb->priority = 0;
|
|
|
|
skb->dev = dev;
|
|
|
|
|
2009-10-14 08:48:30 +08:00
|
|
|
if (skb->pkt_type == PACKET_LOOPBACK) {
|
2008-09-23 11:05:57 +08:00
|
|
|
skb_reset_mac_header(skb);
|
|
|
|
skb_orphan(skb);
|
2011-03-09 06:44:07 +08:00
|
|
|
err = (irq ? netif_rx(skb) : netif_rx_ni(skb)) ? -ENOBUFS : 0;
|
2008-09-23 11:05:57 +08:00
|
|
|
} else {
|
|
|
|
err = dev_hard_header(skb, dev, ntohs(skb->protocol),
|
|
|
|
NULL, NULL, skb->len);
|
|
|
|
if (err < 0) {
|
|
|
|
err = -EHOSTUNREACH;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
err = dev_queue_xmit(skb);
|
2011-03-09 06:44:07 +08:00
|
|
|
if (unlikely(err > 0))
|
|
|
|
err = net_xmit_errno(err);
|
2008-09-23 11:05:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
drop:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-09-23 11:09:13 +08:00
|
|
|
static int pn_raw_send(const void *data, int len, struct net_device *dev,
|
|
|
|
u16 dst, u16 src, u8 res)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb = alloc_skb(MAX_PHONET_HEADER + len, GFP_ATOMIC);
|
|
|
|
if (skb == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-10-14 08:48:30 +08:00
|
|
|
if (phonet_address_lookup(dev_net(dev), pn_addr(dst)) == 0)
|
|
|
|
skb->pkt_type = PACKET_LOOPBACK;
|
|
|
|
|
2008-09-23 11:09:13 +08:00
|
|
|
skb_reserve(skb, MAX_PHONET_HEADER);
|
|
|
|
__skb_put(skb, len);
|
|
|
|
skb_copy_to_linear_data(skb, data, len);
|
|
|
|
return pn_send(skb, dev, dst, src, res, 1);
|
|
|
|
}
|
|
|
|
|
2008-09-23 11:05:57 +08:00
|
|
|
/*
|
|
|
|
* Create a Phonet header for the skb and send it out. Returns
|
|
|
|
* non-zero error code if failed. The skb is freed then.
|
|
|
|
*/
|
|
|
|
int pn_skb_send(struct sock *sk, struct sk_buff *skb,
|
|
|
|
const struct sockaddr_pn *target)
|
|
|
|
{
|
2009-10-14 08:48:30 +08:00
|
|
|
struct net *net = sock_net(sk);
|
2008-09-23 11:05:57 +08:00
|
|
|
struct net_device *dev;
|
|
|
|
struct pn_sock *pn = pn_sk(sk);
|
|
|
|
int err;
|
2011-02-25 07:14:57 +08:00
|
|
|
u16 src, dst;
|
|
|
|
u8 daddr, saddr, res;
|
|
|
|
|
|
|
|
src = pn->sobject;
|
|
|
|
if (target != NULL) {
|
|
|
|
dst = pn_sockaddr_get_object(target);
|
|
|
|
res = pn_sockaddr_get_resource(target);
|
|
|
|
} else {
|
|
|
|
dst = pn->dobject;
|
|
|
|
res = pn->resource;
|
|
|
|
}
|
|
|
|
daddr = pn_addr(dst);
|
2008-09-23 11:05:57 +08:00
|
|
|
|
|
|
|
err = -EHOSTUNREACH;
|
|
|
|
if (sk->sk_bound_dev_if)
|
2009-10-14 08:48:30 +08:00
|
|
|
dev = dev_get_by_index(net, sk->sk_bound_dev_if);
|
|
|
|
else if (phonet_address_lookup(net, daddr) == 0) {
|
|
|
|
dev = phonet_device_get(net);
|
|
|
|
skb->pkt_type = PACKET_LOOPBACK;
|
2011-03-09 06:44:06 +08:00
|
|
|
} else if (dst == 0) {
|
2010-09-15 20:30:13 +08:00
|
|
|
/* Resource routing (small race until phonet_rcv()) */
|
2011-03-09 06:44:06 +08:00
|
|
|
struct sock *sk = pn_find_sock_by_res(net, res);
|
2010-09-15 20:30:13 +08:00
|
|
|
if (sk) {
|
|
|
|
sock_put(sk);
|
|
|
|
dev = phonet_device_get(net);
|
|
|
|
skb->pkt_type = PACKET_LOOPBACK;
|
|
|
|
} else
|
|
|
|
dev = phonet_route_output(net, daddr);
|
2009-10-14 08:48:30 +08:00
|
|
|
} else
|
|
|
|
dev = phonet_route_output(net, daddr);
|
|
|
|
|
2008-09-23 11:05:57 +08:00
|
|
|
if (!dev || !(dev->flags & IFF_UP))
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
saddr = phonet_address_get(dev, daddr);
|
|
|
|
if (saddr == PN_NO_ADDR)
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
if (!pn_addr(src))
|
|
|
|
src = pn_object(saddr, pn_obj(src));
|
|
|
|
|
2011-02-25 07:14:57 +08:00
|
|
|
err = pn_send(skb, dev, dst, src, res, 0);
|
2008-09-23 11:05:57 +08:00
|
|
|
dev_put(dev);
|
|
|
|
return err;
|
|
|
|
|
|
|
|
drop:
|
|
|
|
kfree_skb(skb);
|
|
|
|
if (dev)
|
|
|
|
dev_put(dev);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(pn_skb_send);
|
|
|
|
|
2008-09-23 11:09:13 +08:00
|
|
|
/* Do not send an error message in response to an error message */
|
|
|
|
static inline int can_respond(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
const struct phonethdr *ph;
|
|
|
|
const struct phonetmsg *pm;
|
|
|
|
u8 submsg_id;
|
|
|
|
|
|
|
|
if (!pskb_may_pull(skb, 3))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ph = pn_hdr(skb);
|
|
|
|
if (ph->pn_res == PN_PREFIX && !pskb_may_pull(skb, 5))
|
|
|
|
return 0;
|
2008-10-27 14:07:25 +08:00
|
|
|
if (ph->pn_res == PN_COMMGR) /* indications */
|
|
|
|
return 0;
|
2008-09-23 11:09:13 +08:00
|
|
|
|
|
|
|
ph = pn_hdr(skb); /* re-acquires the pointer */
|
|
|
|
pm = pn_msg(skb);
|
|
|
|
if (pm->pn_msg_id != PN_COMMON_MESSAGE)
|
|
|
|
return 1;
|
|
|
|
submsg_id = (ph->pn_res == PN_PREFIX)
|
|
|
|
? pm->pn_e_submsg_id : pm->pn_submsg_id;
|
|
|
|
if (submsg_id != PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP &&
|
|
|
|
pm->pn_e_submsg_id != PN_COMM_SERVICE_NOT_IDENTIFIED_RESP)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_obj_unreachable(struct sk_buff *rskb)
|
|
|
|
{
|
|
|
|
const struct phonethdr *oph = pn_hdr(rskb);
|
|
|
|
const struct phonetmsg *opm = pn_msg(rskb);
|
|
|
|
struct phonetmsg resp;
|
|
|
|
|
|
|
|
memset(&resp, 0, sizeof(resp));
|
|
|
|
resp.pn_trans_id = opm->pn_trans_id;
|
|
|
|
resp.pn_msg_id = PN_COMMON_MESSAGE;
|
|
|
|
if (oph->pn_res == PN_PREFIX) {
|
|
|
|
resp.pn_e_res_id = opm->pn_e_res_id;
|
|
|
|
resp.pn_e_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
|
|
|
|
resp.pn_e_orig_msg_id = opm->pn_msg_id;
|
|
|
|
resp.pn_e_status = 0;
|
|
|
|
} else {
|
|
|
|
resp.pn_submsg_id = PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP;
|
|
|
|
resp.pn_orig_msg_id = opm->pn_msg_id;
|
|
|
|
resp.pn_status = 0;
|
|
|
|
}
|
|
|
|
return pn_raw_send(&resp, sizeof(resp), rskb->dev,
|
|
|
|
pn_object(oph->pn_sdev, oph->pn_sobj),
|
|
|
|
pn_object(oph->pn_rdev, oph->pn_robj),
|
|
|
|
oph->pn_res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_reset_indications(struct sk_buff *rskb)
|
|
|
|
{
|
|
|
|
struct phonethdr *oph = pn_hdr(rskb);
|
|
|
|
static const u8 data[4] = {
|
|
|
|
0x00 /* trans ID */, 0x10 /* subscribe msg */,
|
|
|
|
0x00 /* subscription count */, 0x00 /* dummy */
|
|
|
|
};
|
|
|
|
|
|
|
|
return pn_raw_send(data, sizeof(data), rskb->dev,
|
|
|
|
pn_object(oph->pn_sdev, 0x00),
|
2008-10-27 14:07:25 +08:00
|
|
|
pn_object(oph->pn_rdev, oph->pn_robj),
|
|
|
|
PN_COMMGR);
|
2008-09-23 11:09:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-23 11:02:10 +08:00
|
|
|
/* packet type functions */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff received packets to associated sockets.
|
|
|
|
* On error, returns non-zero and releases the skb.
|
|
|
|
*/
|
|
|
|
static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
|
|
|
|
struct packet_type *pkttype,
|
|
|
|
struct net_device *orig_dev)
|
|
|
|
{
|
2009-01-23 11:00:26 +08:00
|
|
|
struct net *net = dev_net(dev);
|
2008-09-23 11:02:10 +08:00
|
|
|
struct phonethdr *ph;
|
|
|
|
struct sockaddr_pn sa;
|
|
|
|
u16 len;
|
|
|
|
|
|
|
|
/* check we have at least a full Phonet header */
|
|
|
|
if (!pskb_pull(skb, sizeof(struct phonethdr)))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* check that the advertised length is correct */
|
|
|
|
ph = pn_hdr(skb);
|
|
|
|
len = get_unaligned_be16(&ph->pn_length);
|
|
|
|
if (len < 2)
|
|
|
|
goto out;
|
|
|
|
len -= 2;
|
|
|
|
if ((len > skb->len) || pskb_trim(skb, len))
|
|
|
|
goto out;
|
|
|
|
skb_reset_transport_header(skb);
|
|
|
|
|
|
|
|
pn_skb_get_dst_sockaddr(skb, &sa);
|
|
|
|
|
2009-10-14 08:48:27 +08:00
|
|
|
/* check if this is broadcasted */
|
|
|
|
if (pn_sockaddr_get_addr(&sa) == PNADDR_BROADCAST) {
|
|
|
|
pn_deliver_sock_broadcast(net, skb);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-09-15 20:30:13 +08:00
|
|
|
/* resource routing */
|
|
|
|
if (pn_sockaddr_get_object(&sa) == 0) {
|
|
|
|
struct sock *sk = pn_find_sock_by_res(net, sa.spn_resource);
|
|
|
|
if (sk)
|
|
|
|
return sk_receive_skb(sk, skb, 0);
|
|
|
|
}
|
|
|
|
|
2009-01-23 11:00:26 +08:00
|
|
|
/* check if we are the destination */
|
|
|
|
if (phonet_address_lookup(net, pn_sockaddr_get_addr(&sa)) == 0) {
|
|
|
|
/* Phonet packet input */
|
|
|
|
struct sock *sk = pn_find_sock_by_sa(net, &sa);
|
|
|
|
|
|
|
|
if (sk)
|
|
|
|
return sk_receive_skb(sk, skb, 0);
|
|
|
|
|
2008-09-23 11:09:13 +08:00
|
|
|
if (can_respond(skb)) {
|
|
|
|
send_obj_unreachable(skb);
|
|
|
|
send_reset_indications(skb);
|
|
|
|
}
|
2009-10-14 08:48:31 +08:00
|
|
|
} else if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
|
|
|
|
goto out; /* Race between address deletion and loopback */
|
|
|
|
else {
|
|
|
|
/* Phonet packet routing */
|
|
|
|
struct net_device *out_dev;
|
|
|
|
|
|
|
|
out_dev = phonet_route_output(net, pn_sockaddr_get_addr(&sa));
|
|
|
|
if (!out_dev) {
|
|
|
|
LIMIT_NETDEBUG(KERN_WARNING"No Phonet route to %02X\n",
|
|
|
|
pn_sockaddr_get_addr(&sa));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
__skb_push(skb, sizeof(struct phonethdr));
|
|
|
|
skb->dev = out_dev;
|
|
|
|
if (out_dev == dev) {
|
|
|
|
LIMIT_NETDEBUG(KERN_ERR"Phonet loop to %02X on %s\n",
|
|
|
|
pn_sockaddr_get_addr(&sa), dev->name);
|
|
|
|
goto out_dev;
|
|
|
|
}
|
|
|
|
/* Some drivers (e.g. TUN) do not allocate HW header space */
|
|
|
|
if (skb_cow_head(skb, out_dev->hard_header_len))
|
|
|
|
goto out_dev;
|
|
|
|
|
|
|
|
if (dev_hard_header(skb, out_dev, ETH_P_PHONET, NULL, NULL,
|
|
|
|
skb->len) < 0)
|
|
|
|
goto out_dev;
|
|
|
|
dev_queue_xmit(skb);
|
|
|
|
dev_put(out_dev);
|
|
|
|
return NET_RX_SUCCESS;
|
|
|
|
out_dev:
|
|
|
|
dev_put(out_dev);
|
2008-09-23 11:09:13 +08:00
|
|
|
}
|
2008-09-23 11:05:19 +08:00
|
|
|
|
2008-09-23 11:02:10 +08:00
|
|
|
out:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return NET_RX_DROP;
|
|
|
|
}
|
|
|
|
|
2009-03-09 16:18:29 +08:00
|
|
|
static struct packet_type phonet_packet_type __read_mostly = {
|
2009-02-01 16:45:17 +08:00
|
|
|
.type = cpu_to_be16(ETH_P_PHONET),
|
2008-09-23 11:02:10 +08:00
|
|
|
.func = phonet_rcv,
|
|
|
|
};
|
|
|
|
|
2009-11-13 13:01:18 +08:00
|
|
|
static DEFINE_MUTEX(proto_tab_lock);
|
|
|
|
|
2011-01-10 12:06:58 +08:00
|
|
|
int __init_or_module phonet_proto_register(unsigned int protocol,
|
2008-09-23 11:02:10 +08:00
|
|
|
struct phonet_protocol *pp)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (protocol >= PHONET_NPROTO)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
err = proto_register(pp->prot, 1);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2009-11-13 13:01:18 +08:00
|
|
|
mutex_lock(&proto_tab_lock);
|
2008-09-23 11:02:10 +08:00
|
|
|
if (proto_tab[protocol])
|
|
|
|
err = -EBUSY;
|
|
|
|
else
|
2012-01-12 12:41:32 +08:00
|
|
|
rcu_assign_pointer(proto_tab[protocol], pp);
|
2009-11-13 13:01:18 +08:00
|
|
|
mutex_unlock(&proto_tab_lock);
|
2008-09-23 11:02:10 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(phonet_proto_register);
|
|
|
|
|
2011-01-10 12:06:58 +08:00
|
|
|
void phonet_proto_unregister(unsigned int protocol, struct phonet_protocol *pp)
|
2008-09-23 11:02:10 +08:00
|
|
|
{
|
2009-11-13 13:01:18 +08:00
|
|
|
mutex_lock(&proto_tab_lock);
|
2008-09-23 11:02:10 +08:00
|
|
|
BUG_ON(proto_tab[protocol] != pp);
|
2011-08-02 00:19:00 +08:00
|
|
|
RCU_INIT_POINTER(proto_tab[protocol], NULL);
|
2009-11-13 13:01:18 +08:00
|
|
|
mutex_unlock(&proto_tab_lock);
|
|
|
|
synchronize_rcu();
|
2008-09-23 11:02:10 +08:00
|
|
|
proto_unregister(pp->prot);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(phonet_proto_unregister);
|
|
|
|
|
|
|
|
/* Module registration */
|
|
|
|
static int __init phonet_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2009-01-23 11:00:27 +08:00
|
|
|
err = phonet_device_init();
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2009-11-09 10:17:01 +08:00
|
|
|
pn_sock_init();
|
2008-09-23 11:02:10 +08:00
|
|
|
err = sock_register(&phonet_proto_family);
|
|
|
|
if (err) {
|
|
|
|
printk(KERN_ALERT
|
|
|
|
"phonet protocol family initialization failed\n");
|
2009-01-23 11:00:27 +08:00
|
|
|
goto err_sock;
|
2008-09-23 11:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_add_pack(&phonet_packet_type);
|
2008-09-23 11:08:39 +08:00
|
|
|
phonet_sysctl_init();
|
2008-09-23 11:05:57 +08:00
|
|
|
|
|
|
|
err = isi_register();
|
|
|
|
if (err)
|
|
|
|
goto err;
|
2008-09-23 11:02:10 +08:00
|
|
|
return 0;
|
2008-09-23 11:05:57 +08:00
|
|
|
|
|
|
|
err:
|
2008-09-23 11:08:39 +08:00
|
|
|
phonet_sysctl_exit();
|
2008-10-06 02:14:27 +08:00
|
|
|
sock_unregister(PF_PHONET);
|
2008-09-23 11:05:57 +08:00
|
|
|
dev_remove_pack(&phonet_packet_type);
|
2009-01-23 11:00:27 +08:00
|
|
|
err_sock:
|
2008-09-23 11:05:57 +08:00
|
|
|
phonet_device_exit();
|
|
|
|
return err;
|
2008-09-23 11:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit phonet_exit(void)
|
|
|
|
{
|
2008-09-23 11:05:57 +08:00
|
|
|
isi_unregister();
|
2008-09-23 11:08:39 +08:00
|
|
|
phonet_sysctl_exit();
|
2008-10-06 02:14:27 +08:00
|
|
|
sock_unregister(PF_PHONET);
|
2008-09-23 11:02:10 +08:00
|
|
|
dev_remove_pack(&phonet_packet_type);
|
2008-09-23 11:03:44 +08:00
|
|
|
phonet_device_exit();
|
2008-09-23 11:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(phonet_init);
|
|
|
|
module_exit(phonet_exit);
|
|
|
|
MODULE_DESCRIPTION("Phonet protocol stack for Linux");
|
|
|
|
MODULE_LICENSE("GPL");
|
2008-10-06 02:14:27 +08:00
|
|
|
MODULE_ALIAS_NETPROTO(PF_PHONET);
|