2008-10-06 02:15:13 +08:00
|
|
|
/*
|
|
|
|
* File: pep.c
|
|
|
|
*
|
|
|
|
* Phonet pipe protocol end point socket
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Nokia Corporation.
|
|
|
|
*
|
2012-06-14 06:29:03 +08:00
|
|
|
* Author: Rémi Denis-Courmont
|
2008-10-06 02:15:13 +08:00
|
|
|
*
|
|
|
|
* 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 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-10-06 02:15:13 +08:00
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/tcp_states.h>
|
|
|
|
#include <asm/ioctls.h>
|
|
|
|
|
|
|
|
#include <linux/phonet.h>
|
2011-05-27 21:12:25 +08:00
|
|
|
#include <linux/module.h>
|
2008-10-06 02:15:13 +08:00
|
|
|
#include <net/phonet/phonet.h>
|
|
|
|
#include <net/phonet/pep.h>
|
2008-10-06 02:16:16 +08:00
|
|
|
#include <net/phonet/gprs.h>
|
2008-10-06 02:15:13 +08:00
|
|
|
|
|
|
|
/* sk_state values:
|
|
|
|
* TCP_CLOSE sock not in use yet
|
|
|
|
* TCP_CLOSE_WAIT disconnected pipe
|
|
|
|
* TCP_LISTEN listening pipe endpoint
|
|
|
|
* TCP_SYN_RECV connected pipe in disabled state
|
|
|
|
* TCP_ESTABLISHED connected pipe in enabled state
|
|
|
|
*
|
|
|
|
* pep_sock locking:
|
2011-03-09 06:44:10 +08:00
|
|
|
* - sk_state, hlist: sock lock needed
|
2008-10-06 02:15:13 +08:00
|
|
|
* - listener: read only
|
|
|
|
* - pipe_handle: read only
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CREDITS_MAX 10
|
|
|
|
#define CREDITS_THR 7
|
|
|
|
|
|
|
|
#define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
|
|
|
|
|
|
|
|
/* Get the next TLV sub-block. */
|
|
|
|
static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen,
|
|
|
|
void *buf)
|
|
|
|
{
|
|
|
|
void *data = NULL;
|
|
|
|
struct {
|
|
|
|
u8 sb_type;
|
|
|
|
u8 sb_len;
|
|
|
|
} *ph, h;
|
|
|
|
int buflen = *plen;
|
|
|
|
|
|
|
|
ph = skb_header_pointer(skb, 0, 2, &h);
|
|
|
|
if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len))
|
|
|
|
return NULL;
|
|
|
|
ph->sb_len -= 2;
|
|
|
|
*ptype = ph->sb_type;
|
|
|
|
*plen = ph->sb_len;
|
|
|
|
|
|
|
|
if (buflen > ph->sb_len)
|
|
|
|
buflen = ph->sb_len;
|
|
|
|
data = skb_header_pointer(skb, 2, buflen, buf);
|
|
|
|
__skb_pull(skb, 2 + ph->sb_len);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
static struct sk_buff *pep_alloc_skb(struct sock *sk, const void *payload,
|
|
|
|
int len, gfp_t priority)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
|
|
|
|
if (!skb)
|
|
|
|
return NULL;
|
|
|
|
skb_set_owner_w(skb, sk);
|
|
|
|
|
|
|
|
skb_reserve(skb, MAX_PNPIPE_HEADER);
|
|
|
|
__skb_put(skb, len);
|
|
|
|
skb_copy_to_linear_data(skb, payload, len);
|
|
|
|
__skb_push(skb, sizeof(struct pnpipehdr));
|
|
|
|
skb_reset_transport_header(skb);
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pep_reply(struct sock *sk, struct sk_buff *oskb, u8 code,
|
|
|
|
const void *data, int len, gfp_t priority)
|
2008-10-06 02:15:13 +08:00
|
|
|
{
|
|
|
|
const struct pnpipehdr *oph = pnp_hdr(oskb);
|
|
|
|
struct pnpipehdr *ph;
|
|
|
|
struct sk_buff *skb;
|
2011-02-25 07:14:58 +08:00
|
|
|
struct sockaddr_pn peer;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
skb = pep_alloc_skb(sk, data, len, priority);
|
2008-10-06 02:15:13 +08:00
|
|
|
if (!skb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ph = pnp_hdr(skb);
|
|
|
|
ph->utid = oph->utid;
|
|
|
|
ph->message_id = oph->message_id + 1; /* REQ -> RESP */
|
|
|
|
ph->pipe_handle = oph->pipe_handle;
|
|
|
|
ph->error_code = code;
|
|
|
|
|
2011-02-25 07:14:58 +08:00
|
|
|
pn_skb_get_src_sockaddr(oskb, &peer);
|
|
|
|
return pn_skb_send(sk, skb, &peer);
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
static int pep_indicate(struct sock *sk, u8 id, u8 code,
|
|
|
|
const void *data, int len, gfp_t priority)
|
2010-09-27 03:07:59 +08:00
|
|
|
{
|
2011-03-09 06:44:09 +08:00
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2010-09-27 03:07:59 +08:00
|
|
|
struct pnpipehdr *ph;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
skb = pep_alloc_skb(sk, data, len, priority);
|
2010-09-27 03:07:59 +08:00
|
|
|
if (!skb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ph = pnp_hdr(skb);
|
2011-03-09 06:44:09 +08:00
|
|
|
ph->utid = 0;
|
|
|
|
ph->message_id = id;
|
2010-10-13 04:14:43 +08:00
|
|
|
ph->pipe_handle = pn->pipe_handle;
|
2011-03-09 06:44:09 +08:00
|
|
|
ph->data[0] = code;
|
2011-02-25 07:14:58 +08:00
|
|
|
return pn_skb_send(sk, skb, NULL);
|
2010-09-27 03:07:59 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
#define PAD 0x00
|
|
|
|
|
|
|
|
static int pipe_handler_request(struct sock *sk, u8 id, u8 code,
|
|
|
|
const void *data, int len)
|
2010-09-27 03:07:59 +08:00
|
|
|
{
|
2011-03-09 06:44:09 +08:00
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2010-09-27 03:07:59 +08:00
|
|
|
struct pnpipehdr *ph;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
skb = pep_alloc_skb(sk, data, len, GFP_KERNEL);
|
2010-09-27 03:07:59 +08:00
|
|
|
if (!skb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ph = pnp_hdr(skb);
|
2011-03-09 06:44:09 +08:00
|
|
|
ph->utid = id; /* whatever */
|
|
|
|
ph->message_id = id;
|
2010-10-13 04:14:43 +08:00
|
|
|
ph->pipe_handle = pn->pipe_handle;
|
2011-03-09 06:44:09 +08:00
|
|
|
ph->data[0] = code;
|
2011-02-25 07:14:58 +08:00
|
|
|
return pn_skb_send(sk, skb, NULL);
|
2010-09-27 03:07:59 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
static int pipe_handler_send_created_ind(struct sock *sk)
|
2010-09-27 03:07:59 +08:00
|
|
|
{
|
2010-10-13 04:14:43 +08:00
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2011-03-09 06:44:09 +08:00
|
|
|
u8 data[4] = {
|
|
|
|
PN_PIPE_SB_NEGOTIATED_FC, pep_sb_size(2),
|
|
|
|
pn->tx_fc, pn->rx_fc,
|
|
|
|
};
|
2010-09-27 03:07:59 +08:00
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
return pep_indicate(sk, PNS_PIPE_CREATED_IND, 1 /* sub-blocks */,
|
|
|
|
data, 4, GFP_ATOMIC);
|
|
|
|
}
|
2010-09-27 03:07:59 +08:00
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
static const u8 data[20] = {
|
|
|
|
PAD, PAD, PAD, 2 /* sub-blocks */,
|
|
|
|
PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
|
|
|
|
PN_MULTI_CREDIT_FLOW_CONTROL,
|
|
|
|
PN_ONE_CREDIT_FLOW_CONTROL,
|
|
|
|
PN_LEGACY_FLOW_CONTROL,
|
|
|
|
PAD,
|
|
|
|
PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
|
|
|
|
PN_MULTI_CREDIT_FLOW_CONTROL,
|
|
|
|
PN_ONE_CREDIT_FLOW_CONTROL,
|
|
|
|
PN_LEGACY_FLOW_CONTROL,
|
|
|
|
PAD,
|
|
|
|
};
|
|
|
|
|
|
|
|
might_sleep();
|
|
|
|
return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
|
|
|
|
GFP_KERNEL);
|
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code,
|
|
|
|
gfp_t priority)
|
2008-10-06 02:15:13 +08:00
|
|
|
{
|
|
|
|
static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
|
|
|
|
WARN_ON(code == PN_PIPE_NO_ERROR);
|
2011-03-09 06:44:10 +08:00
|
|
|
return pep_reply(sk, skb, code, data, sizeof(data), priority);
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Control requests are not sent by the pipe service and have a specific
|
|
|
|
* message format. */
|
2008-10-06 02:15:43 +08:00
|
|
|
static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
|
|
|
|
gfp_t priority)
|
2008-10-06 02:15:13 +08:00
|
|
|
{
|
|
|
|
const struct pnpipehdr *oph = pnp_hdr(oskb);
|
|
|
|
struct sk_buff *skb;
|
|
|
|
struct pnpipehdr *ph;
|
|
|
|
struct sockaddr_pn dst;
|
2011-03-09 06:44:09 +08:00
|
|
|
u8 data[4] = {
|
|
|
|
oph->data[0], /* PEP type */
|
|
|
|
code, /* error code, at an unusual offset */
|
|
|
|
PAD, PAD,
|
|
|
|
};
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
skb = pep_alloc_skb(sk, data, 4, priority);
|
2008-10-06 02:15:13 +08:00
|
|
|
if (!skb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
ph = pnp_hdr(skb);
|
2008-10-06 02:15:13 +08:00
|
|
|
ph->utid = oph->utid;
|
|
|
|
ph->message_id = PNS_PEP_CTRL_RESP;
|
|
|
|
ph->pipe_handle = oph->pipe_handle;
|
|
|
|
ph->data[0] = oph->data[1]; /* CTRL id */
|
|
|
|
|
|
|
|
pn_skb_get_src_sockaddr(oskb, &dst);
|
|
|
|
return pn_skb_send(sk, skb, &dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
|
|
|
|
{
|
2011-03-09 06:44:09 +08:00
|
|
|
u8 data[4] = { type, PAD, PAD, status };
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
return pep_indicate(sk, PNS_PEP_STATUS_IND, PN_PEP_TYPE_COMMON,
|
|
|
|
data, 4, priority);
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send our RX flow control information to the sender.
|
|
|
|
* Socket must be locked. */
|
2011-03-09 06:44:09 +08:00
|
|
|
static void pipe_grant_credits(struct sock *sk, gfp_t priority)
|
2008-10-06 02:15:13 +08:00
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
|
|
|
|
BUG_ON(sk->sk_state != TCP_ESTABLISHED);
|
|
|
|
|
|
|
|
switch (pn->rx_fc) {
|
|
|
|
case PN_LEGACY_FLOW_CONTROL: /* TODO */
|
|
|
|
break;
|
|
|
|
case PN_ONE_CREDIT_FLOW_CONTROL:
|
2011-03-09 06:44:09 +08:00
|
|
|
if (pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
|
|
|
|
PEP_IND_READY, priority) == 0)
|
|
|
|
pn->rx_credits = 1;
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
case PN_MULTI_CREDIT_FLOW_CONTROL:
|
|
|
|
if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
|
|
|
|
break;
|
|
|
|
if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
|
|
|
|
CREDITS_MAX - pn->rx_credits,
|
2011-03-09 06:44:09 +08:00
|
|
|
priority) == 0)
|
2008-10-06 02:15:13 +08:00
|
|
|
pn->rx_credits = CREDITS_MAX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2010-09-28 07:10:42 +08:00
|
|
|
struct pnpipehdr *hdr;
|
2008-12-18 07:48:31 +08:00
|
|
|
int wake = 0;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
|
|
|
if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-09-28 07:10:42 +08:00
|
|
|
hdr = pnp_hdr(skb);
|
2008-10-06 02:15:13 +08:00
|
|
|
if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
|
|
|
|
LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
|
2012-04-15 13:58:06 +08:00
|
|
|
(unsigned int)hdr->data[0]);
|
2008-10-06 02:15:13 +08:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (hdr->data[1]) {
|
|
|
|
case PN_PEP_IND_FLOW_CONTROL:
|
|
|
|
switch (pn->tx_fc) {
|
|
|
|
case PN_LEGACY_FLOW_CONTROL:
|
|
|
|
switch (hdr->data[4]) {
|
|
|
|
case PEP_IND_BUSY:
|
2008-12-18 07:48:31 +08:00
|
|
|
atomic_set(&pn->tx_credits, 0);
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
case PEP_IND_READY:
|
2008-12-18 07:48:31 +08:00
|
|
|
atomic_set(&pn->tx_credits, wake = 1);
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PN_ONE_CREDIT_FLOW_CONTROL:
|
|
|
|
if (hdr->data[4] == PEP_IND_READY)
|
2008-12-18 07:48:31 +08:00
|
|
|
atomic_set(&pn->tx_credits, wake = 1);
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
|
|
|
|
if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
|
|
|
|
break;
|
2008-12-18 07:48:31 +08:00
|
|
|
atomic_add(wake = hdr->data[4], &pn->tx_credits);
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
|
2012-04-15 13:58:06 +08:00
|
|
|
(unsigned int)hdr->data[1]);
|
2008-10-06 02:15:13 +08:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2008-12-18 07:48:31 +08:00
|
|
|
if (wake)
|
2008-10-06 02:15:13 +08:00
|
|
|
sk->sk_write_space(sk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
struct pnpipehdr *hdr = pnp_hdr(skb);
|
|
|
|
u8 n_sb = hdr->data[0];
|
|
|
|
|
|
|
|
pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
|
|
|
|
__skb_pull(skb, sizeof(*hdr));
|
|
|
|
while (n_sb > 0) {
|
|
|
|
u8 type, buf[2], len = sizeof(buf);
|
|
|
|
u8 *data = pep_get_sb(skb, &type, &len, buf);
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
switch (type) {
|
|
|
|
case PN_PIPE_SB_NEGOTIATED_FC:
|
|
|
|
if (len < 2 || (data[0] | data[1]) > 3)
|
|
|
|
break;
|
|
|
|
pn->tx_fc = data[0] & 3;
|
|
|
|
pn->rx_fc = data[1] & 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n_sb--;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Queue an skb to a connected sock.
|
|
|
|
* Socket lock must be held. */
|
|
|
|
static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
struct pnpipehdr *hdr = pnp_hdr(skb);
|
2008-10-06 02:15:43 +08:00
|
|
|
struct sk_buff_head *queue;
|
2008-10-06 02:15:13 +08:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
|
|
|
|
|
|
|
|
switch (hdr->message_id) {
|
|
|
|
case PNS_PEP_CONNECT_REQ:
|
2011-03-09 06:44:10 +08:00
|
|
|
pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_ATOMIC);
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_DISCONNECT_REQ:
|
|
|
|
pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
|
|
|
|
sk->sk_state = TCP_CLOSE_WAIT;
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD))
|
|
|
|
sk->sk_state_change(sk);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_ENABLE_REQ:
|
|
|
|
/* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
|
|
|
|
pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_RESET_REQ:
|
|
|
|
switch (hdr->state_after_reset) {
|
|
|
|
case PN_PIPE_DISABLE:
|
|
|
|
pn->init_enable = 0;
|
|
|
|
break;
|
|
|
|
case PN_PIPE_ENABLE:
|
|
|
|
pn->init_enable = 1;
|
|
|
|
break;
|
|
|
|
default: /* not allowed to send an error here!? */
|
|
|
|
err = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case PNS_PEP_DISABLE_REQ:
|
2008-12-18 07:48:31 +08:00
|
|
|
atomic_set(&pn->tx_credits, 0);
|
2008-10-06 02:15:13 +08:00
|
|
|
pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_CTRL_REQ:
|
2009-07-21 09:57:59 +08:00
|
|
|
if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
|
|
|
|
atomic_inc(&sk->sk_drops);
|
2008-10-06 02:15:43 +08:00
|
|
|
break;
|
2009-07-21 09:57:59 +08:00
|
|
|
}
|
2008-10-06 02:15:43 +08:00
|
|
|
__skb_pull(skb, 4);
|
|
|
|
queue = &pn->ctrlreq_queue;
|
|
|
|
goto queue;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2010-01-04 10:02:47 +08:00
|
|
|
case PNS_PIPE_ALIGNED_DATA:
|
|
|
|
__skb_pull(skb, 1);
|
|
|
|
/* fall through */
|
2008-10-06 02:15:13 +08:00
|
|
|
case PNS_PIPE_DATA:
|
|
|
|
__skb_pull(skb, 3); /* Pipe data header */
|
|
|
|
if (!pn_flow_safe(pn->rx_fc)) {
|
|
|
|
err = sock_queue_rcv_skb(sk, skb);
|
|
|
|
if (!err)
|
2011-03-09 06:44:08 +08:00
|
|
|
return NET_RX_SUCCESS;
|
|
|
|
err = -ENOBUFS;
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pn->rx_credits == 0) {
|
2009-07-21 09:57:59 +08:00
|
|
|
atomic_inc(&sk->sk_drops);
|
2008-10-06 02:15:13 +08:00
|
|
|
err = -ENOBUFS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pn->rx_credits--;
|
2008-10-06 02:15:43 +08:00
|
|
|
queue = &sk->sk_receive_queue;
|
|
|
|
goto queue;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
|
|
|
case PNS_PEP_STATUS_IND:
|
|
|
|
pipe_rcv_status(sk, skb);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PIPE_REDIRECTED_IND:
|
|
|
|
err = pipe_rcv_created(sk, skb);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PIPE_CREATED_IND:
|
|
|
|
err = pipe_rcv_created(sk, skb);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
/* fall through */
|
|
|
|
case PNS_PIPE_RESET_IND:
|
|
|
|
if (!pn->init_enable)
|
|
|
|
break;
|
|
|
|
/* fall through */
|
|
|
|
case PNS_PIPE_ENABLED_IND:
|
|
|
|
if (!pn_flow_safe(pn->tx_fc)) {
|
2008-12-18 07:48:31 +08:00
|
|
|
atomic_set(&pn->tx_credits, 1);
|
2008-10-06 02:15:13 +08:00
|
|
|
sk->sk_write_space(sk);
|
|
|
|
}
|
|
|
|
if (sk->sk_state == TCP_ESTABLISHED)
|
|
|
|
break; /* Nothing to do */
|
|
|
|
sk->sk_state = TCP_ESTABLISHED;
|
2011-03-09 06:44:09 +08:00
|
|
|
pipe_grant_credits(sk, GFP_ATOMIC);
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PIPE_DISABLED_IND:
|
|
|
|
sk->sk_state = TCP_SYN_RECV;
|
|
|
|
pn->rx_credits = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
|
|
|
|
hdr->message_id);
|
|
|
|
err = -EINVAL;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
kfree_skb(skb);
|
2011-03-09 06:44:08 +08:00
|
|
|
return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS;
|
2008-10-06 02:15:43 +08:00
|
|
|
|
|
|
|
queue:
|
|
|
|
skb->dev = NULL;
|
|
|
|
skb_set_owner_r(skb, sk);
|
|
|
|
skb_queue_tail(queue, skb);
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD))
|
2014-04-12 04:15:36 +08:00
|
|
|
sk->sk_data_ready(sk);
|
2011-03-09 06:44:08 +08:00
|
|
|
return NET_RX_SUCCESS;
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy connected sock. */
|
|
|
|
static void pipe_destruct(struct sock *sk)
|
|
|
|
{
|
2008-10-06 02:15:43 +08:00
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
skb_queue_purge(&sk->sk_receive_queue);
|
2008-10-06 02:15:43 +08:00
|
|
|
skb_queue_purge(&pn->ctrlreq_queue);
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
2012-04-15 13:58:06 +08:00
|
|
|
static u8 pipe_negotiate_fc(const u8 *fcs, unsigned int n)
|
2011-02-25 07:15:01 +08:00
|
|
|
{
|
2012-04-15 13:58:06 +08:00
|
|
|
unsigned int i;
|
2011-02-25 07:15:01 +08:00
|
|
|
u8 final_fc = PN_NO_FLOW_CONTROL;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
u8 fc = fcs[i];
|
|
|
|
|
|
|
|
if (fc > final_fc && fc < PN_MAX_FLOW_CONTROL)
|
|
|
|
final_fc = fc;
|
|
|
|
}
|
|
|
|
return final_fc;
|
|
|
|
}
|
|
|
|
|
2010-10-13 04:14:43 +08:00
|
|
|
static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2011-02-25 07:15:01 +08:00
|
|
|
struct pnpipehdr *hdr;
|
|
|
|
u8 n_sb;
|
|
|
|
|
|
|
|
if (!pskb_pull(skb, sizeof(*hdr) + 4))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
hdr = pnp_hdr(skb);
|
2011-03-09 06:44:12 +08:00
|
|
|
if (hdr->error_code != PN_PIPE_NO_ERROR)
|
|
|
|
return -ECONNREFUSED;
|
2011-02-25 07:15:01 +08:00
|
|
|
|
|
|
|
/* Parse sub-blocks */
|
|
|
|
n_sb = hdr->data[4];
|
|
|
|
while (n_sb > 0) {
|
|
|
|
u8 type, buf[6], len = sizeof(buf);
|
|
|
|
const u8 *data = pep_get_sb(skb, &type, &len, buf);
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case PN_PIPE_SB_REQUIRED_FC_TX:
|
|
|
|
if (len < 2 || len < data[0])
|
|
|
|
break;
|
|
|
|
pn->tx_fc = pipe_negotiate_fc(data + 2, len - 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PN_PIPE_SB_PREFERRED_FC_RX:
|
|
|
|
if (len < 2 || len < data[0])
|
|
|
|
break;
|
|
|
|
pn->rx_fc = pipe_negotiate_fc(data + 2, len - 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
n_sb--;
|
|
|
|
}
|
2010-10-13 04:14:43 +08:00
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
return pipe_handler_send_created_ind(sk);
|
2010-10-13 04:14:43 +08:00
|
|
|
}
|
2011-03-09 06:44:12 +08:00
|
|
|
|
2011-11-18 09:22:05 +08:00
|
|
|
static int pep_enableresp_rcv(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pnpipehdr *hdr = pnp_hdr(skb);
|
|
|
|
|
|
|
|
if (hdr->error_code != PN_PIPE_NO_ERROR)
|
|
|
|
return -ECONNREFUSED;
|
|
|
|
|
|
|
|
return pep_indicate(sk, PNS_PIPE_ENABLED_IND, 0 /* sub-blocks */,
|
|
|
|
NULL, 0, GFP_ATOMIC);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pipe_start_flow_control(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
|
|
|
|
if (!pn_flow_safe(pn->tx_fc)) {
|
|
|
|
atomic_set(&pn->tx_credits, 1);
|
|
|
|
sk->sk_write_space(sk);
|
|
|
|
}
|
|
|
|
pipe_grant_credits(sk, GFP_ATOMIC);
|
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:12 +08:00
|
|
|
/* Queue an skb to an actively connected sock.
|
|
|
|
* Socket lock must be held. */
|
|
|
|
static int pipe_handler_do_rcv(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
struct pnpipehdr *hdr = pnp_hdr(skb);
|
|
|
|
int err = NET_RX_SUCCESS;
|
|
|
|
|
|
|
|
switch (hdr->message_id) {
|
|
|
|
case PNS_PIPE_ALIGNED_DATA:
|
|
|
|
__skb_pull(skb, 1);
|
|
|
|
/* fall through */
|
|
|
|
case PNS_PIPE_DATA:
|
|
|
|
__skb_pull(skb, 3); /* Pipe data header */
|
|
|
|
if (!pn_flow_safe(pn->rx_fc)) {
|
|
|
|
err = sock_queue_rcv_skb(sk, skb);
|
|
|
|
if (!err)
|
|
|
|
return NET_RX_SUCCESS;
|
|
|
|
err = NET_RX_DROP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pn->rx_credits == 0) {
|
|
|
|
atomic_inc(&sk->sk_drops);
|
|
|
|
err = NET_RX_DROP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pn->rx_credits--;
|
|
|
|
skb->dev = NULL;
|
|
|
|
skb_set_owner_r(skb, sk);
|
|
|
|
skb_queue_tail(&sk->sk_receive_queue, skb);
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD))
|
2014-04-12 04:15:36 +08:00
|
|
|
sk->sk_data_ready(sk);
|
2011-03-09 06:44:12 +08:00
|
|
|
return NET_RX_SUCCESS;
|
|
|
|
|
|
|
|
case PNS_PEP_CONNECT_RESP:
|
|
|
|
if (sk->sk_state != TCP_SYN_SENT)
|
|
|
|
break;
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD))
|
|
|
|
sk->sk_state_change(sk);
|
|
|
|
if (pep_connresp_rcv(sk, skb)) {
|
|
|
|
sk->sk_state = TCP_CLOSE_WAIT;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 09:22:05 +08:00
|
|
|
if (pn->init_enable == PN_PIPE_DISABLE)
|
|
|
|
sk->sk_state = TCP_SYN_RECV;
|
|
|
|
else {
|
|
|
|
sk->sk_state = TCP_ESTABLISHED;
|
|
|
|
pipe_start_flow_control(sk);
|
|
|
|
}
|
|
|
|
break;
|
2011-03-09 06:44:12 +08:00
|
|
|
|
2011-11-18 09:22:05 +08:00
|
|
|
case PNS_PEP_ENABLE_RESP:
|
|
|
|
if (sk->sk_state != TCP_SYN_SENT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (pep_enableresp_rcv(sk, skb)) {
|
|
|
|
sk->sk_state = TCP_CLOSE_WAIT;
|
|
|
|
break;
|
2011-03-09 06:44:12 +08:00
|
|
|
}
|
2011-11-18 09:22:05 +08:00
|
|
|
|
|
|
|
sk->sk_state = TCP_ESTABLISHED;
|
|
|
|
pipe_start_flow_control(sk);
|
2011-03-09 06:44:12 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_DISCONNECT_RESP:
|
|
|
|
/* sock should already be dead, nothing to do */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_STATUS_IND:
|
|
|
|
pipe_rcv_status(sk, skb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
kfree_skb(skb);
|
|
|
|
return err;
|
|
|
|
}
|
2010-10-13 04:14:43 +08:00
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
/* Listening sock must be locked */
|
|
|
|
static struct sock *pep_find_pipe(const struct hlist_head *hlist,
|
|
|
|
const struct sockaddr_pn *dst,
|
|
|
|
u8 pipe_handle)
|
|
|
|
{
|
|
|
|
struct sock *sknode;
|
|
|
|
u16 dobj = pn_sockaddr_get_object(dst);
|
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 09:06:00 +08:00
|
|
|
sk_for_each(sknode, hlist) {
|
2008-10-06 02:15:13 +08:00
|
|
|
struct pep_sock *pnnode = pep_sk(sknode);
|
|
|
|
|
|
|
|
/* Ports match, but addresses might not: */
|
|
|
|
if (pnnode->pn_sk.sobject != dobj)
|
|
|
|
continue;
|
|
|
|
if (pnnode->pipe_handle != pipe_handle)
|
|
|
|
continue;
|
|
|
|
if (sknode->sk_state == TCP_CLOSE_WAIT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sock_hold(sknode);
|
|
|
|
return sknode;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deliver an skb to a listening sock.
|
|
|
|
* Socket lock must be held.
|
|
|
|
* We then queue the skb to the right connected sock (if any).
|
|
|
|
*/
|
|
|
|
static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
struct sock *sknode;
|
2009-02-11 09:14:50 +08:00
|
|
|
struct pnpipehdr *hdr;
|
2008-10-06 02:15:13 +08:00
|
|
|
struct sockaddr_pn dst;
|
|
|
|
u8 pipe_handle;
|
|
|
|
|
|
|
|
if (!pskb_may_pull(skb, sizeof(*hdr)))
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
hdr = pnp_hdr(skb);
|
|
|
|
pipe_handle = hdr->pipe_handle;
|
|
|
|
if (pipe_handle == PN_PIPE_INVALID_HANDLE)
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
pn_skb_get_dst_sockaddr(skb, &dst);
|
|
|
|
|
|
|
|
/* Look for an existing pipe handle */
|
|
|
|
sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
|
|
|
|
if (sknode)
|
|
|
|
return sk_receive_skb(sknode, skb, 1);
|
|
|
|
|
|
|
|
switch (hdr->message_id) {
|
|
|
|
case PNS_PEP_CONNECT_REQ:
|
2011-03-09 06:44:10 +08:00
|
|
|
if (sk->sk_state != TCP_LISTEN || sk_acceptq_is_full(sk)) {
|
|
|
|
pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE,
|
|
|
|
GFP_ATOMIC);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
skb_queue_head(&sk->sk_receive_queue, skb);
|
|
|
|
sk_acceptq_added(sk);
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD))
|
2014-04-12 04:15:36 +08:00
|
|
|
sk->sk_data_ready(sk);
|
2011-03-09 06:44:10 +08:00
|
|
|
return NET_RX_SUCCESS;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
|
|
|
case PNS_PEP_DISCONNECT_REQ:
|
|
|
|
pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_CTRL_REQ:
|
2008-10-06 02:15:43 +08:00
|
|
|
pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
|
2008-10-06 02:15:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PNS_PEP_RESET_REQ:
|
|
|
|
case PNS_PEP_ENABLE_REQ:
|
|
|
|
case PNS_PEP_DISABLE_REQ:
|
|
|
|
/* invalid handle is not even allowed here! */
|
2011-03-09 06:44:08 +08:00
|
|
|
break;
|
2011-03-09 06:44:12 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
if ((1 << sk->sk_state)
|
|
|
|
& ~(TCPF_CLOSE|TCPF_LISTEN|TCPF_CLOSE_WAIT))
|
|
|
|
/* actively connected socket */
|
|
|
|
return pipe_handler_do_rcv(sk, skb);
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
drop:
|
|
|
|
kfree_skb(skb);
|
2011-03-09 06:44:08 +08:00
|
|
|
return NET_RX_SUCCESS;
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
2010-09-15 20:19:53 +08:00
|
|
|
static int pipe_do_remove(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
struct pnpipehdr *ph;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
2011-03-09 06:44:09 +08:00
|
|
|
skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL);
|
2010-09-15 20:19:53 +08:00
|
|
|
if (!skb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ph = pnp_hdr(skb);
|
|
|
|
ph->utid = 0;
|
|
|
|
ph->message_id = PNS_PIPE_REMOVE_REQ;
|
|
|
|
ph->pipe_handle = pn->pipe_handle;
|
|
|
|
ph->data[0] = PAD;
|
2011-02-25 07:14:58 +08:00
|
|
|
return pn_skb_send(sk, skb, NULL);
|
2010-09-15 20:19:53 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
/* associated socket ceases to exist */
|
|
|
|
static void pep_sock_close(struct sock *sk, long timeout)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2008-10-06 02:16:16 +08:00
|
|
|
int ifindex = 0;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2010-05-26 07:08:39 +08:00
|
|
|
sock_hold(sk); /* keep a reference after sk_common_release() */
|
2008-10-06 02:15:13 +08:00
|
|
|
sk_common_release(sk);
|
|
|
|
|
|
|
|
lock_sock(sk);
|
2011-03-09 06:44:10 +08:00
|
|
|
if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
|
2011-03-09 06:44:12 +08:00
|
|
|
if (sk->sk_backlog_rcv == pipe_do_rcv)
|
|
|
|
/* Forcefully remove dangling Phonet pipe */
|
|
|
|
pipe_do_remove(sk);
|
|
|
|
else
|
|
|
|
pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD,
|
|
|
|
NULL, 0);
|
2011-02-25 07:14:59 +08:00
|
|
|
}
|
2011-03-09 06:44:10 +08:00
|
|
|
sk->sk_state = TCP_CLOSE;
|
2010-10-13 04:14:43 +08:00
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
ifindex = pn->ifindex;
|
|
|
|
pn->ifindex = 0;
|
2008-10-06 02:15:13 +08:00
|
|
|
release_sock(sk);
|
2008-10-06 02:16:16 +08:00
|
|
|
|
|
|
|
if (ifindex)
|
|
|
|
gprs_detach(sk);
|
2010-05-26 07:08:39 +08:00
|
|
|
sock_put(sk);
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
|
2008-10-06 02:15:13 +08:00
|
|
|
{
|
2011-03-09 06:44:10 +08:00
|
|
|
struct pep_sock *pn = pep_sk(sk), *newpn;
|
|
|
|
struct sock *newsk = NULL;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
struct pnpipehdr *hdr;
|
|
|
|
struct sockaddr_pn dst, src;
|
|
|
|
int err;
|
|
|
|
u16 peer_type;
|
|
|
|
u8 pipe_handle, enabled, n_sb;
|
|
|
|
u8 aligned = 0;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
skb = skb_recv_datagram(sk, 0, flags & O_NONBLOCK, errp);
|
|
|
|
if (!skb)
|
|
|
|
return NULL;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
lock_sock(sk);
|
|
|
|
if (sk->sk_state != TCP_LISTEN) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto drop;
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
2011-03-09 06:44:10 +08:00
|
|
|
sk_acceptq_removed(sk);
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
err = -EPROTO;
|
|
|
|
if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
|
|
|
|
goto drop;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
hdr = pnp_hdr(skb);
|
|
|
|
pipe_handle = hdr->pipe_handle;
|
|
|
|
switch (hdr->state_after_connect) {
|
|
|
|
case PN_PIPE_DISABLE:
|
|
|
|
enabled = 0;
|
|
|
|
break;
|
|
|
|
case PN_PIPE_ENABLE:
|
|
|
|
enabled = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM,
|
|
|
|
GFP_KERNEL);
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
peer_type = hdr->other_pep_type << 8;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
/* Parse sub-blocks (options) */
|
|
|
|
n_sb = hdr->data[4];
|
|
|
|
while (n_sb > 0) {
|
|
|
|
u8 type, buf[1], len = sizeof(buf);
|
|
|
|
const u8 *data = pep_get_sb(skb, &type, &len, buf);
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
if (data == NULL)
|
|
|
|
goto drop;
|
|
|
|
switch (type) {
|
|
|
|
case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
|
|
|
|
if (len < 1)
|
|
|
|
goto drop;
|
|
|
|
peer_type = (peer_type & 0xff00) | data[0];
|
|
|
|
break;
|
|
|
|
case PN_PIPE_SB_ALIGNED_DATA:
|
|
|
|
aligned = data[0] != 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n_sb--;
|
|
|
|
}
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
/* Check for duplicate pipe handle */
|
|
|
|
newsk = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
|
|
|
|
if (unlikely(newsk)) {
|
|
|
|
__sock_put(newsk);
|
2008-10-06 02:15:13 +08:00
|
|
|
newsk = NULL;
|
2011-03-09 06:44:10 +08:00
|
|
|
pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_KERNEL);
|
|
|
|
goto drop;
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
/* Create a new to-be-accepted sock */
|
|
|
|
newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_KERNEL, sk->sk_prot);
|
|
|
|
if (!newsk) {
|
|
|
|
pep_reject_conn(sk, skb, PN_PIPE_ERR_OVERLOAD, GFP_KERNEL);
|
|
|
|
err = -ENOBUFS;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock_init_data(NULL, newsk);
|
|
|
|
newsk->sk_state = TCP_SYN_RECV;
|
|
|
|
newsk->sk_backlog_rcv = pipe_do_rcv;
|
|
|
|
newsk->sk_protocol = sk->sk_protocol;
|
|
|
|
newsk->sk_destruct = pipe_destruct;
|
|
|
|
|
|
|
|
newpn = pep_sk(newsk);
|
|
|
|
pn_skb_get_dst_sockaddr(skb, &dst);
|
|
|
|
pn_skb_get_src_sockaddr(skb, &src);
|
|
|
|
newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
|
|
|
|
newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
|
|
|
|
newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
|
2008-10-06 02:15:13 +08:00
|
|
|
sock_hold(sk);
|
2011-03-09 06:44:10 +08:00
|
|
|
newpn->listener = sk;
|
|
|
|
skb_queue_head_init(&newpn->ctrlreq_queue);
|
|
|
|
newpn->pipe_handle = pipe_handle;
|
|
|
|
atomic_set(&newpn->tx_credits, 0);
|
|
|
|
newpn->ifindex = 0;
|
|
|
|
newpn->peer_type = peer_type;
|
|
|
|
newpn->rx_credits = 0;
|
|
|
|
newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
|
|
|
|
newpn->init_enable = enabled;
|
|
|
|
newpn->aligned = aligned;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
err = pep_accept_conn(newsk, skb);
|
|
|
|
if (err) {
|
|
|
|
sock_put(newsk);
|
|
|
|
newsk = NULL;
|
|
|
|
goto drop;
|
|
|
|
}
|
2008-10-06 02:15:13 +08:00
|
|
|
sk_add_node(newsk, &pn->hlist);
|
2011-03-09 06:44:10 +08:00
|
|
|
drop:
|
2008-10-06 02:15:13 +08:00
|
|
|
release_sock(sk);
|
2011-03-09 06:44:10 +08:00
|
|
|
kfree_skb(skb);
|
2008-10-06 02:15:13 +08:00
|
|
|
*errp = err;
|
|
|
|
return newsk;
|
|
|
|
}
|
|
|
|
|
2010-10-13 04:14:43 +08:00
|
|
|
static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2011-03-09 06:44:12 +08:00
|
|
|
int err;
|
2011-03-09 06:44:09 +08:00
|
|
|
u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD };
|
2010-10-13 04:14:43 +08:00
|
|
|
|
2011-11-18 09:22:05 +08:00
|
|
|
if (pn->pipe_handle == PN_PIPE_INVALID_HANDLE)
|
|
|
|
pn->pipe_handle = 1; /* anything but INVALID_HANDLE */
|
|
|
|
|
2011-03-09 06:44:12 +08:00
|
|
|
err = pipe_handler_request(sk, PNS_PEP_CONNECT_REQ,
|
2011-11-18 09:22:05 +08:00
|
|
|
pn->init_enable, data, 4);
|
2011-03-09 06:44:12 +08:00
|
|
|
if (err) {
|
|
|
|
pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
|
|
|
|
return err;
|
|
|
|
}
|
2011-11-18 09:22:05 +08:00
|
|
|
|
2011-03-09 06:44:12 +08:00
|
|
|
sk->sk_state = TCP_SYN_SENT;
|
2011-11-18 09:22:05 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pep_sock_enable(struct sock *sk, struct sockaddr *addr, int len)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = pipe_handler_request(sk, PNS_PEP_ENABLE_REQ, PAD,
|
|
|
|
NULL, 0);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
sk->sk_state = TCP_SYN_SENT;
|
|
|
|
|
2011-03-09 06:44:12 +08:00
|
|
|
return 0;
|
2010-10-13 04:14:43 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
|
|
|
|
{
|
2008-10-06 02:15:43 +08:00
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2008-10-06 02:15:13 +08:00
|
|
|
int answ;
|
2011-11-18 09:22:05 +08:00
|
|
|
int ret = -ENOIOCTLCMD;
|
2008-10-06 02:15:13 +08:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SIOCINQ:
|
2011-11-18 09:22:05 +08:00
|
|
|
if (sk->sk_state == TCP_LISTEN) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2008-10-06 02:15:13 +08:00
|
|
|
|
|
|
|
lock_sock(sk);
|
2009-11-30 08:55:45 +08:00
|
|
|
if (sock_flag(sk, SOCK_URGINLINE) &&
|
|
|
|
!skb_queue_empty(&pn->ctrlreq_queue))
|
2008-10-06 02:15:43 +08:00
|
|
|
answ = skb_peek(&pn->ctrlreq_queue)->len;
|
|
|
|
else if (!skb_queue_empty(&sk->sk_receive_queue))
|
2008-10-06 02:15:13 +08:00
|
|
|
answ = skb_peek(&sk->sk_receive_queue)->len;
|
|
|
|
else
|
|
|
|
answ = 0;
|
|
|
|
release_sock(sk);
|
2011-11-18 09:22:05 +08:00
|
|
|
ret = put_user(answ, (int __user *)arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCPNENABLEPIPE:
|
|
|
|
lock_sock(sk);
|
|
|
|
if (sk->sk_state == TCP_SYN_SENT)
|
|
|
|
ret = -EBUSY;
|
|
|
|
else if (sk->sk_state == TCP_ESTABLISHED)
|
|
|
|
ret = -EISCONN;
|
|
|
|
else
|
|
|
|
ret = pep_sock_enable(sk, NULL, 0);
|
|
|
|
release_sock(sk);
|
|
|
|
break;
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 09:22:05 +08:00
|
|
|
return ret;
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pep_init(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
|
2011-03-09 06:44:10 +08:00
|
|
|
sk->sk_destruct = pipe_destruct;
|
2008-10-06 02:15:13 +08:00
|
|
|
INIT_HLIST_HEAD(&pn->hlist);
|
2011-03-09 06:44:12 +08:00
|
|
|
pn->listener = NULL;
|
2008-10-06 02:15:43 +08:00
|
|
|
skb_queue_head_init(&pn->ctrlreq_queue);
|
2011-03-09 06:44:12 +08:00
|
|
|
atomic_set(&pn->tx_credits, 0);
|
|
|
|
pn->ifindex = 0;
|
|
|
|
pn->peer_type = 0;
|
2008-10-06 02:15:13 +08:00
|
|
|
pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
|
2011-03-09 06:44:12 +08:00
|
|
|
pn->rx_credits = 0;
|
|
|
|
pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
|
|
|
|
pn->init_enable = 1;
|
|
|
|
pn->aligned = 0;
|
2008-10-06 02:15:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
static int pep_setsockopt(struct sock *sk, int level, int optname,
|
2009-10-01 07:12:20 +08:00
|
|
|
char __user *optval, unsigned int optlen)
|
2008-10-06 02:16:16 +08:00
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
int val = 0, err = 0;
|
|
|
|
|
|
|
|
if (level != SOL_PNPIPE)
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
if (optlen >= sizeof(int)) {
|
|
|
|
if (get_user(val, (int __user *) optval))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock_sock(sk);
|
|
|
|
switch (optname) {
|
|
|
|
case PNPIPE_ENCAP:
|
|
|
|
if (val && val != PNPIPE_ENCAP_IP) {
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!pn->ifindex == !val)
|
|
|
|
break; /* Nothing to do! */
|
|
|
|
if (!capable(CAP_NET_ADMIN)) {
|
|
|
|
err = -EPERM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (val) {
|
|
|
|
release_sock(sk);
|
|
|
|
err = gprs_attach(sk);
|
|
|
|
if (err > 0) {
|
|
|
|
pn->ifindex = err;
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pn->ifindex = 0;
|
|
|
|
release_sock(sk);
|
|
|
|
gprs_detach(sk);
|
|
|
|
err = 0;
|
|
|
|
}
|
|
|
|
goto out_norel;
|
Phonet: cleanup pipe enable socket option
The current code works like this:
int garbage, status;
socklen_t len = sizeof(status);
/* enable pipe */
setsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &garbage, sizeof(garbage));
/* disable pipe */
setsockopt(fd, SOL_PNPIPE, PNPIPE_DISABLE, &garbage, sizeof(garbage));
/* get status */
getsockopt(fd, SOL_PNPIPE, PNPIPE_INQ, &status, &len);
...which does not follow the usual socket option pattern. This patch
merges all three "options" into a single gettable&settable option,
before Linux 2.6.37 gets out:
int status;
socklen_t len = sizeof(status);
/* enable pipe */
status = 1;
setsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &status, sizeof(status));
/* disable pipe */
status = 0;
setsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &status, sizeof(status));
/* get status */
getsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &status, &len);
This also fixes the error code from EFAULT to ENOTCONN.
Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
Cc: Kumar Sanghvi <kumar.sanghvi@stericsson.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-10-08 12:02:02 +08:00
|
|
|
|
2011-11-18 09:22:05 +08:00
|
|
|
case PNPIPE_HANDLE:
|
|
|
|
if ((sk->sk_state == TCP_CLOSE) &&
|
|
|
|
(val >= 0) && (val < PN_PIPE_INVALID_HANDLE))
|
|
|
|
pn->pipe_handle = val;
|
|
|
|
else
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PNPIPE_INITSTATE:
|
|
|
|
pn->init_enable = !!val;
|
|
|
|
break;
|
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
default:
|
|
|
|
err = -ENOPROTOOPT;
|
|
|
|
}
|
|
|
|
release_sock(sk);
|
|
|
|
|
|
|
|
out_norel:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pep_getsockopt(struct sock *sk, int level, int optname,
|
|
|
|
char __user *optval, int __user *optlen)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
int len, val;
|
|
|
|
|
|
|
|
if (level != SOL_PNPIPE)
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
if (get_user(len, optlen))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
switch (optname) {
|
|
|
|
case PNPIPE_ENCAP:
|
|
|
|
val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
|
|
|
|
break;
|
2010-09-27 03:07:59 +08:00
|
|
|
|
Phonet: cleanup pipe enable socket option
The current code works like this:
int garbage, status;
socklen_t len = sizeof(status);
/* enable pipe */
setsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &garbage, sizeof(garbage));
/* disable pipe */
setsockopt(fd, SOL_PNPIPE, PNPIPE_DISABLE, &garbage, sizeof(garbage));
/* get status */
getsockopt(fd, SOL_PNPIPE, PNPIPE_INQ, &status, &len);
...which does not follow the usual socket option pattern. This patch
merges all three "options" into a single gettable&settable option,
before Linux 2.6.37 gets out:
int status;
socklen_t len = sizeof(status);
/* enable pipe */
status = 1;
setsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &status, sizeof(status));
/* disable pipe */
status = 0;
setsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &status, sizeof(status));
/* get status */
getsockopt(fd, SOL_PNPIPE, PNPIPE_ENABLE, &status, &len);
This also fixes the error code from EFAULT to ENOTCONN.
Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
Cc: Kumar Sanghvi <kumar.sanghvi@stericsson.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-10-08 12:02:02 +08:00
|
|
|
case PNPIPE_IFINDEX:
|
|
|
|
val = pn->ifindex;
|
|
|
|
break;
|
|
|
|
|
2011-03-09 06:44:11 +08:00
|
|
|
case PNPIPE_HANDLE:
|
|
|
|
val = pn->pipe_handle;
|
|
|
|
if (val == PN_PIPE_INVALID_HANDLE)
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
|
2011-11-18 09:22:05 +08:00
|
|
|
case PNPIPE_INITSTATE:
|
|
|
|
val = pn->init_enable;
|
|
|
|
break;
|
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
default:
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = min_t(unsigned int, sizeof(int), len);
|
|
|
|
if (put_user(len, optlen))
|
|
|
|
return -EFAULT;
|
|
|
|
if (put_user(val, (int __user *) optval))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
struct pnpipehdr *ph;
|
2010-09-30 06:33:50 +08:00
|
|
|
int err;
|
2008-10-06 02:16:16 +08:00
|
|
|
|
2008-12-18 07:48:31 +08:00
|
|
|
if (pn_flow_safe(pn->tx_fc) &&
|
|
|
|
!atomic_add_unless(&pn->tx_credits, -1, 0)) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2010-01-04 10:02:48 +08:00
|
|
|
skb_push(skb, 3 + pn->aligned);
|
2008-10-06 02:16:16 +08:00
|
|
|
skb_reset_transport_header(skb);
|
|
|
|
ph = pnp_hdr(skb);
|
|
|
|
ph->utid = 0;
|
2010-01-04 10:02:48 +08:00
|
|
|
if (pn->aligned) {
|
|
|
|
ph->message_id = PNS_PIPE_ALIGNED_DATA;
|
|
|
|
ph->data[0] = 0; /* padding */
|
|
|
|
} else
|
|
|
|
ph->message_id = PNS_PIPE_DATA;
|
2008-10-06 02:16:16 +08:00
|
|
|
ph->pipe_handle = pn->pipe_handle;
|
2011-02-25 07:14:58 +08:00
|
|
|
err = pn_skb_send(sk, skb, NULL);
|
2010-09-30 06:33:50 +08:00
|
|
|
|
|
|
|
if (err && pn_flow_safe(pn->tx_fc))
|
|
|
|
atomic_inc(&pn->tx_credits);
|
|
|
|
return err;
|
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
|
|
|
|
struct msghdr *msg, size_t len)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
2009-11-09 12:06:40 +08:00
|
|
|
struct sk_buff *skb;
|
2008-10-06 02:15:13 +08:00
|
|
|
long timeo;
|
|
|
|
int flags = msg->msg_flags;
|
|
|
|
int err, done;
|
|
|
|
|
2012-04-05 20:07:45 +08:00
|
|
|
if (len > USHRT_MAX)
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
2010-01-04 10:02:49 +08:00
|
|
|
if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
|
|
|
|
MSG_CMSG_COMPAT)) ||
|
|
|
|
!(msg->msg_flags & MSG_EOR))
|
2008-10-06 02:15:13 +08:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2009-11-09 12:06:40 +08:00
|
|
|
skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
|
|
|
|
flags & MSG_DONTWAIT, &err);
|
|
|
|
if (!skb)
|
2010-08-30 20:57:04 +08:00
|
|
|
return err;
|
2009-11-09 12:06:40 +08:00
|
|
|
|
2011-03-16 05:55:49 +08:00
|
|
|
skb_reserve(skb, MAX_PHONET_HEADER + 3 + pn->aligned);
|
2009-11-09 12:06:40 +08:00
|
|
|
err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
|
|
|
|
if (err < 0)
|
|
|
|
goto outfree;
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
lock_sock(sk);
|
|
|
|
timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
|
|
|
|
if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
|
|
|
|
err = -ENOTCONN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sk->sk_state != TCP_ESTABLISHED) {
|
|
|
|
/* Wait until the pipe gets to enabled state */
|
|
|
|
disabled:
|
|
|
|
err = sk_stream_wait_connect(sk, &timeo);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (sk->sk_state == TCP_CLOSE_WAIT) {
|
|
|
|
err = -ECONNRESET;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BUG_ON(sk->sk_state != TCP_ESTABLISHED);
|
|
|
|
|
|
|
|
/* Wait until flow control allows TX */
|
2008-12-18 07:48:31 +08:00
|
|
|
done = atomic_read(&pn->tx_credits);
|
2008-10-06 02:15:13 +08:00
|
|
|
while (!done) {
|
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
|
|
|
|
if (!timeo) {
|
|
|
|
err = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (signal_pending(current)) {
|
|
|
|
err = sock_intr_errno(timeo);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-04-29 19:01:49 +08:00
|
|
|
prepare_to_wait(sk_sleep(sk), &wait,
|
2008-10-06 02:15:13 +08:00
|
|
|
TASK_INTERRUPTIBLE);
|
2008-12-18 07:48:31 +08:00
|
|
|
done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
|
2010-04-29 19:01:49 +08:00
|
|
|
finish_wait(sk_sleep(sk), &wait);
|
2008-10-06 02:15:13 +08:00
|
|
|
|
|
|
|
if (sk->sk_state != TCP_ESTABLISHED)
|
|
|
|
goto disabled;
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
err = pipe_skb_send(sk, skb);
|
2008-10-06 02:15:13 +08:00
|
|
|
if (err >= 0)
|
|
|
|
err = len; /* success! */
|
|
|
|
skb = NULL;
|
|
|
|
out:
|
|
|
|
release_sock(sk);
|
2009-11-09 12:06:40 +08:00
|
|
|
outfree:
|
2008-10-06 02:15:13 +08:00
|
|
|
kfree_skb(skb);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
int pep_writeable(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
|
2008-12-18 07:48:31 +08:00
|
|
|
return atomic_read(&pn->tx_credits);
|
2008-10-06 02:16:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int pep_write(struct sock *sk, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct sk_buff *rskb, *fs;
|
|
|
|
int flen = 0;
|
|
|
|
|
2010-01-04 10:02:48 +08:00
|
|
|
if (pep_sk(sk)->aligned)
|
|
|
|
return pipe_skb_send(sk, skb);
|
|
|
|
|
2008-10-06 02:16:16 +08:00
|
|
|
rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
|
|
|
|
if (!rskb) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
skb_shinfo(rskb)->frag_list = skb;
|
|
|
|
rskb->len += skb->len;
|
|
|
|
rskb->data_len += rskb->len;
|
|
|
|
rskb->truesize += rskb->len;
|
|
|
|
|
|
|
|
/* Avoid nested fragments */
|
2009-06-09 15:21:58 +08:00
|
|
|
skb_walk_frags(skb, fs)
|
2008-10-06 02:16:16 +08:00
|
|
|
flen += fs->len;
|
|
|
|
skb->next = skb_shinfo(skb)->frag_list;
|
2009-06-09 15:21:58 +08:00
|
|
|
skb_frag_list_init(skb);
|
2008-10-06 02:16:16 +08:00
|
|
|
skb->len -= flen;
|
|
|
|
skb->data_len -= flen;
|
|
|
|
skb->truesize -= flen;
|
|
|
|
|
|
|
|
skb_reserve(rskb, MAX_PHONET_HEADER + 3);
|
|
|
|
return pipe_skb_send(sk, rskb);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sk_buff *pep_read(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
|
|
|
|
|
|
|
|
if (sk->sk_state == TCP_ESTABLISHED)
|
2011-03-09 06:44:09 +08:00
|
|
|
pipe_grant_credits(sk, GFP_ATOMIC);
|
2008-10-06 02:16:16 +08:00
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
|
|
|
|
struct msghdr *msg, size_t len, int noblock,
|
|
|
|
int flags, int *addr_len)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
int err;
|
|
|
|
|
2010-01-04 10:02:49 +08:00
|
|
|
if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
|
|
|
|
MSG_NOSIGNAL|MSG_CMSG_COMPAT))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
|
|
|
|
return -ENOTCONN;
|
|
|
|
|
2008-10-06 02:15:43 +08:00
|
|
|
if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
|
|
|
|
/* Dequeue and acknowledge control request */
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
|
2010-01-04 10:02:49 +08:00
|
|
|
if (flags & MSG_PEEK)
|
|
|
|
return -EOPNOTSUPP;
|
2008-10-06 02:15:43 +08:00
|
|
|
skb = skb_dequeue(&pn->ctrlreq_queue);
|
|
|
|
if (skb) {
|
|
|
|
pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
|
|
|
|
GFP_KERNEL);
|
|
|
|
msg->msg_flags |= MSG_OOB;
|
|
|
|
goto copy;
|
|
|
|
}
|
|
|
|
if (flags & MSG_OOB)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
skb = skb_recv_datagram(sk, flags, noblock, &err);
|
|
|
|
lock_sock(sk);
|
|
|
|
if (skb == NULL) {
|
|
|
|
if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
|
|
|
|
err = -ECONNRESET;
|
|
|
|
release_sock(sk);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sk->sk_state == TCP_ESTABLISHED)
|
2011-03-09 06:44:09 +08:00
|
|
|
pipe_grant_credits(sk, GFP_KERNEL);
|
2008-10-06 02:15:13 +08:00
|
|
|
release_sock(sk);
|
2008-10-06 02:15:43 +08:00
|
|
|
copy:
|
2008-10-06 02:15:13 +08:00
|
|
|
msg->msg_flags |= MSG_EOR;
|
|
|
|
if (skb->len > len)
|
|
|
|
msg->msg_flags |= MSG_TRUNC;
|
|
|
|
else
|
|
|
|
len = skb->len;
|
|
|
|
|
|
|
|
err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
|
|
|
|
if (!err)
|
|
|
|
err = (flags & MSG_TRUNC) ? skb->len : len;
|
|
|
|
|
|
|
|
skb_free_datagram(sk, skb);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pep_sock_unhash(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct pep_sock *pn = pep_sk(sk);
|
|
|
|
struct sock *skparent = NULL;
|
|
|
|
|
|
|
|
lock_sock(sk);
|
2010-10-13 04:14:43 +08:00
|
|
|
|
2011-03-09 06:44:12 +08:00
|
|
|
if (pn->listener != NULL) {
|
2008-10-06 02:15:13 +08:00
|
|
|
skparent = pn->listener;
|
2011-03-09 06:44:12 +08:00
|
|
|
pn->listener = NULL;
|
2008-10-06 02:15:13 +08:00
|
|
|
release_sock(sk);
|
|
|
|
|
|
|
|
pn = pep_sk(skparent);
|
2010-05-26 08:44:44 +08:00
|
|
|
lock_sock(skparent);
|
|
|
|
sk_del_node_init(sk);
|
|
|
|
sk = skparent;
|
2008-10-06 02:15:13 +08:00
|
|
|
}
|
2011-03-09 06:44:12 +08:00
|
|
|
|
2008-10-06 02:15:13 +08:00
|
|
|
/* Unhash a listening sock only when it is closed
|
|
|
|
* and all of its active connected pipes are closed. */
|
|
|
|
if (hlist_empty(&pn->hlist))
|
|
|
|
pn_sock_unhash(&pn->pn_sk.sk);
|
|
|
|
release_sock(sk);
|
|
|
|
|
|
|
|
if (skparent)
|
|
|
|
sock_put(skparent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct proto pep_proto = {
|
|
|
|
.close = pep_sock_close,
|
|
|
|
.accept = pep_sock_accept,
|
2010-10-13 04:14:43 +08:00
|
|
|
.connect = pep_sock_connect,
|
2008-10-06 02:15:13 +08:00
|
|
|
.ioctl = pep_ioctl,
|
|
|
|
.init = pep_init,
|
2008-10-06 02:16:16 +08:00
|
|
|
.setsockopt = pep_setsockopt,
|
|
|
|
.getsockopt = pep_getsockopt,
|
2008-10-06 02:15:13 +08:00
|
|
|
.sendmsg = pep_sendmsg,
|
|
|
|
.recvmsg = pep_recvmsg,
|
|
|
|
.backlog_rcv = pep_do_rcv,
|
|
|
|
.hash = pn_sock_hash,
|
|
|
|
.unhash = pep_sock_unhash,
|
|
|
|
.get_port = pn_sock_get_port,
|
|
|
|
.obj_size = sizeof(struct pep_sock),
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.name = "PNPIPE",
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct phonet_protocol pep_pn_proto = {
|
|
|
|
.ops = &phonet_stream_ops,
|
|
|
|
.prot = &pep_proto,
|
|
|
|
.sock_type = SOCK_SEQPACKET,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init pep_register(void)
|
|
|
|
{
|
|
|
|
return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit pep_unregister(void)
|
|
|
|
{
|
|
|
|
phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(pep_register);
|
|
|
|
module_exit(pep_unregister);
|
|
|
|
MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
|
|
|
|
MODULE_DESCRIPTION("Phonet pipe protocol");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);
|