4 * Phonet pipe protocol end point socket
6 * Copyright (C) 2008 Nokia Corporation.
8 * Author: RĂ©mi Denis-Courmont
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/socket.h>
29 #include <net/tcp_states.h>
30 #include <asm/ioctls.h>
32 #include <linux/phonet.h>
33 #include <linux/module.h>
34 #include <net/phonet/phonet.h>
35 #include <net/phonet/pep.h>
36 #include <net/phonet/gprs.h>
39 * TCP_CLOSE sock not in use yet
40 * TCP_CLOSE_WAIT disconnected pipe
41 * TCP_LISTEN listening pipe endpoint
42 * TCP_SYN_RECV connected pipe in disabled state
43 * TCP_ESTABLISHED connected pipe in enabled state
46 * - sk_state, hlist: sock lock needed
47 * - listener: read only
48 * - pipe_handle: read only
51 #define CREDITS_MAX 10
54 #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
56 /* Get the next TLV sub-block. */
57 static unsigned char *pep_get_sb(struct sk_buff
*skb
, u8
*ptype
, u8
*plen
,
67 ph
= skb_header_pointer(skb
, 0, 2, &h
);
68 if (ph
== NULL
|| ph
->sb_len
< 2 || !pskb_may_pull(skb
, ph
->sb_len
))
74 if (buflen
> ph
->sb_len
)
76 data
= skb_header_pointer(skb
, 2, buflen
, buf
);
77 __skb_pull(skb
, 2 + ph
->sb_len
);
81 static struct sk_buff
*pep_alloc_skb(struct sock
*sk
, const void *payload
,
82 int len
, gfp_t priority
)
84 struct sk_buff
*skb
= alloc_skb(MAX_PNPIPE_HEADER
+ len
, priority
);
87 skb_set_owner_w(skb
, sk
);
89 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
91 skb_copy_to_linear_data(skb
, payload
, len
);
92 __skb_push(skb
, sizeof(struct pnpipehdr
));
93 skb_reset_transport_header(skb
);
97 static int pep_reply(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
98 const void *data
, int len
, gfp_t priority
)
100 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
101 struct pnpipehdr
*ph
;
103 struct sockaddr_pn peer
;
105 skb
= pep_alloc_skb(sk
, data
, len
, priority
);
110 ph
->utid
= oph
->utid
;
111 ph
->message_id
= oph
->message_id
+ 1; /* REQ -> RESP */
112 ph
->pipe_handle
= oph
->pipe_handle
;
113 ph
->error_code
= code
;
115 pn_skb_get_src_sockaddr(oskb
, &peer
);
116 return pn_skb_send(sk
, skb
, &peer
);
119 static int pep_indicate(struct sock
*sk
, u8 id
, u8 code
,
120 const void *data
, int len
, gfp_t priority
)
122 struct pep_sock
*pn
= pep_sk(sk
);
123 struct pnpipehdr
*ph
;
126 skb
= pep_alloc_skb(sk
, data
, len
, priority
);
133 ph
->pipe_handle
= pn
->pipe_handle
;
135 return pn_skb_send(sk
, skb
, NULL
);
140 static int pipe_handler_request(struct sock
*sk
, u8 id
, u8 code
,
141 const void *data
, int len
)
143 struct pep_sock
*pn
= pep_sk(sk
);
144 struct pnpipehdr
*ph
;
147 skb
= pep_alloc_skb(sk
, data
, len
, GFP_KERNEL
);
152 ph
->utid
= id
; /* whatever */
154 ph
->pipe_handle
= pn
->pipe_handle
;
156 return pn_skb_send(sk
, skb
, NULL
);
159 static int pipe_handler_send_created_ind(struct sock
*sk
)
161 struct pep_sock
*pn
= pep_sk(sk
);
163 PN_PIPE_SB_NEGOTIATED_FC
, pep_sb_size(2),
164 pn
->tx_fc
, pn
->rx_fc
,
167 return pep_indicate(sk
, PNS_PIPE_CREATED_IND
, 1 /* sub-blocks */,
168 data
, 4, GFP_ATOMIC
);
171 static int pep_accept_conn(struct sock
*sk
, struct sk_buff
*skb
)
173 static const u8 data
[20] = {
174 PAD
, PAD
, PAD
, 2 /* sub-blocks */,
175 PN_PIPE_SB_REQUIRED_FC_TX
, pep_sb_size(5), 3, PAD
,
176 PN_MULTI_CREDIT_FLOW_CONTROL
,
177 PN_ONE_CREDIT_FLOW_CONTROL
,
178 PN_LEGACY_FLOW_CONTROL
,
180 PN_PIPE_SB_PREFERRED_FC_RX
, pep_sb_size(5), 3, PAD
,
181 PN_MULTI_CREDIT_FLOW_CONTROL
,
182 PN_ONE_CREDIT_FLOW_CONTROL
,
183 PN_LEGACY_FLOW_CONTROL
,
188 return pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, data
, sizeof(data
),
192 static int pep_reject_conn(struct sock
*sk
, struct sk_buff
*skb
, u8 code
,
195 static const u8 data
[4] = { PAD
, PAD
, PAD
, 0 /* sub-blocks */ };
196 WARN_ON(code
== PN_PIPE_NO_ERROR
);
197 return pep_reply(sk
, skb
, code
, data
, sizeof(data
), priority
);
200 /* Control requests are not sent by the pipe service and have a specific
202 static int pep_ctrlreq_error(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
205 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
207 struct pnpipehdr
*ph
;
208 struct sockaddr_pn dst
;
210 oph
->data
[0], /* PEP type */
211 code
, /* error code, at an unusual offset */
215 skb
= pep_alloc_skb(sk
, data
, 4, priority
);
220 ph
->utid
= oph
->utid
;
221 ph
->message_id
= PNS_PEP_CTRL_RESP
;
222 ph
->pipe_handle
= oph
->pipe_handle
;
223 ph
->data
[0] = oph
->data
[1]; /* CTRL id */
225 pn_skb_get_src_sockaddr(oskb
, &dst
);
226 return pn_skb_send(sk
, skb
, &dst
);
229 static int pipe_snd_status(struct sock
*sk
, u8 type
, u8 status
, gfp_t priority
)
231 u8 data
[4] = { type
, PAD
, PAD
, status
};
233 return pep_indicate(sk
, PNS_PEP_STATUS_IND
, PN_PEP_TYPE_COMMON
,
237 /* Send our RX flow control information to the sender.
238 * Socket must be locked. */
239 static void pipe_grant_credits(struct sock
*sk
, gfp_t priority
)
241 struct pep_sock
*pn
= pep_sk(sk
);
243 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
246 case PN_LEGACY_FLOW_CONTROL
: /* TODO */
248 case PN_ONE_CREDIT_FLOW_CONTROL
:
249 if (pipe_snd_status(sk
, PN_PEP_IND_FLOW_CONTROL
,
250 PEP_IND_READY
, priority
) == 0)
253 case PN_MULTI_CREDIT_FLOW_CONTROL
:
254 if ((pn
->rx_credits
+ CREDITS_THR
) > CREDITS_MAX
)
256 if (pipe_snd_status(sk
, PN_PEP_IND_ID_MCFC_GRANT_CREDITS
,
257 CREDITS_MAX
- pn
->rx_credits
,
259 pn
->rx_credits
= CREDITS_MAX
;
264 static int pipe_rcv_status(struct sock
*sk
, struct sk_buff
*skb
)
266 struct pep_sock
*pn
= pep_sk(sk
);
267 struct pnpipehdr
*hdr
;
270 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
274 if (hdr
->data
[0] != PN_PEP_TYPE_COMMON
) {
275 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP type: %u\n",
276 (unsigned int)hdr
->data
[0]);
280 switch (hdr
->data
[1]) {
281 case PN_PEP_IND_FLOW_CONTROL
:
283 case PN_LEGACY_FLOW_CONTROL
:
284 switch (hdr
->data
[4]) {
286 atomic_set(&pn
->tx_credits
, 0);
289 atomic_set(&pn
->tx_credits
, wake
= 1);
293 case PN_ONE_CREDIT_FLOW_CONTROL
:
294 if (hdr
->data
[4] == PEP_IND_READY
)
295 atomic_set(&pn
->tx_credits
, wake
= 1);
300 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS
:
301 if (pn
->tx_fc
!= PN_MULTI_CREDIT_FLOW_CONTROL
)
303 atomic_add(wake
= hdr
->data
[4], &pn
->tx_credits
);
307 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP indication: %u\n",
308 (unsigned int)hdr
->data
[1]);
312 sk
->sk_write_space(sk
);
316 static int pipe_rcv_created(struct sock
*sk
, struct sk_buff
*skb
)
318 struct pep_sock
*pn
= pep_sk(sk
);
319 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
320 u8 n_sb
= hdr
->data
[0];
322 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
323 __skb_pull(skb
, sizeof(*hdr
));
325 u8 type
, buf
[2], len
= sizeof(buf
);
326 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
331 case PN_PIPE_SB_NEGOTIATED_FC
:
332 if (len
< 2 || (data
[0] | data
[1]) > 3)
334 pn
->tx_fc
= data
[0] & 3;
335 pn
->rx_fc
= data
[1] & 3;
343 /* Queue an skb to a connected sock.
344 * Socket lock must be held. */
345 static int pipe_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
347 struct pep_sock
*pn
= pep_sk(sk
);
348 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
349 struct sk_buff_head
*queue
;
352 BUG_ON(sk
->sk_state
== TCP_CLOSE_WAIT
);
354 switch (hdr
->message_id
) {
355 case PNS_PEP_CONNECT_REQ
:
356 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
, GFP_ATOMIC
);
359 case PNS_PEP_DISCONNECT_REQ
:
360 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
361 sk
->sk_state
= TCP_CLOSE_WAIT
;
362 if (!sock_flag(sk
, SOCK_DEAD
))
363 sk
->sk_state_change(sk
);
366 case PNS_PEP_ENABLE_REQ
:
367 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
368 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
371 case PNS_PEP_RESET_REQ
:
372 switch (hdr
->state_after_reset
) {
373 case PN_PIPE_DISABLE
:
379 default: /* not allowed to send an error here!? */
384 case PNS_PEP_DISABLE_REQ
:
385 atomic_set(&pn
->tx_credits
, 0);
386 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
389 case PNS_PEP_CTRL_REQ
:
390 if (skb_queue_len(&pn
->ctrlreq_queue
) >= PNPIPE_CTRLREQ_MAX
) {
391 atomic_inc(&sk
->sk_drops
);
395 queue
= &pn
->ctrlreq_queue
;
398 case PNS_PIPE_ALIGNED_DATA
:
402 __skb_pull(skb
, 3); /* Pipe data header */
403 if (!pn_flow_safe(pn
->rx_fc
)) {
404 err
= sock_queue_rcv_skb(sk
, skb
);
406 return NET_RX_SUCCESS
;
411 if (pn
->rx_credits
== 0) {
412 atomic_inc(&sk
->sk_drops
);
417 queue
= &sk
->sk_receive_queue
;
420 case PNS_PEP_STATUS_IND
:
421 pipe_rcv_status(sk
, skb
);
424 case PNS_PIPE_REDIRECTED_IND
:
425 err
= pipe_rcv_created(sk
, skb
);
428 case PNS_PIPE_CREATED_IND
:
429 err
= pipe_rcv_created(sk
, skb
);
433 case PNS_PIPE_RESET_IND
:
434 if (!pn
->init_enable
)
437 case PNS_PIPE_ENABLED_IND
:
438 if (!pn_flow_safe(pn
->tx_fc
)) {
439 atomic_set(&pn
->tx_credits
, 1);
440 sk
->sk_write_space(sk
);
442 if (sk
->sk_state
== TCP_ESTABLISHED
)
443 break; /* Nothing to do */
444 sk
->sk_state
= TCP_ESTABLISHED
;
445 pipe_grant_credits(sk
, GFP_ATOMIC
);
448 case PNS_PIPE_DISABLED_IND
:
449 sk
->sk_state
= TCP_SYN_RECV
;
454 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP message: %u\n",
460 return (err
== -ENOBUFS
) ? NET_RX_DROP
: NET_RX_SUCCESS
;
464 skb_set_owner_r(skb
, sk
);
465 skb_queue_tail(queue
, skb
);
466 if (!sock_flag(sk
, SOCK_DEAD
))
467 sk
->sk_data_ready(sk
);
468 return NET_RX_SUCCESS
;
471 /* Destroy connected sock. */
472 static void pipe_destruct(struct sock
*sk
)
474 struct pep_sock
*pn
= pep_sk(sk
);
476 skb_queue_purge(&sk
->sk_receive_queue
);
477 skb_queue_purge(&pn
->ctrlreq_queue
);
480 static u8
pipe_negotiate_fc(const u8
*fcs
, unsigned int n
)
483 u8 final_fc
= PN_NO_FLOW_CONTROL
;
485 for (i
= 0; i
< n
; i
++) {
488 if (fc
> final_fc
&& fc
< PN_MAX_FLOW_CONTROL
)
494 static int pep_connresp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
496 struct pep_sock
*pn
= pep_sk(sk
);
497 struct pnpipehdr
*hdr
;
500 if (!pskb_pull(skb
, sizeof(*hdr
) + 4))
504 if (hdr
->error_code
!= PN_PIPE_NO_ERROR
)
505 return -ECONNREFUSED
;
507 /* Parse sub-blocks */
510 u8 type
, buf
[6], len
= sizeof(buf
);
511 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
517 case PN_PIPE_SB_REQUIRED_FC_TX
:
518 if (len
< 2 || len
< data
[0])
520 pn
->tx_fc
= pipe_negotiate_fc(data
+ 2, len
- 2);
523 case PN_PIPE_SB_PREFERRED_FC_RX
:
524 if (len
< 2 || len
< data
[0])
526 pn
->rx_fc
= pipe_negotiate_fc(data
+ 2, len
- 2);
533 return pipe_handler_send_created_ind(sk
);
536 static int pep_enableresp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
538 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
540 if (hdr
->error_code
!= PN_PIPE_NO_ERROR
)
541 return -ECONNREFUSED
;
543 return pep_indicate(sk
, PNS_PIPE_ENABLED_IND
, 0 /* sub-blocks */,
544 NULL
, 0, GFP_ATOMIC
);
548 static void pipe_start_flow_control(struct sock
*sk
)
550 struct pep_sock
*pn
= pep_sk(sk
);
552 if (!pn_flow_safe(pn
->tx_fc
)) {
553 atomic_set(&pn
->tx_credits
, 1);
554 sk
->sk_write_space(sk
);
556 pipe_grant_credits(sk
, GFP_ATOMIC
);
559 /* Queue an skb to an actively connected sock.
560 * Socket lock must be held. */
561 static int pipe_handler_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
563 struct pep_sock
*pn
= pep_sk(sk
);
564 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
565 int err
= NET_RX_SUCCESS
;
567 switch (hdr
->message_id
) {
568 case PNS_PIPE_ALIGNED_DATA
:
572 __skb_pull(skb
, 3); /* Pipe data header */
573 if (!pn_flow_safe(pn
->rx_fc
)) {
574 err
= sock_queue_rcv_skb(sk
, skb
);
576 return NET_RX_SUCCESS
;
581 if (pn
->rx_credits
== 0) {
582 atomic_inc(&sk
->sk_drops
);
588 skb_set_owner_r(skb
, sk
);
589 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
590 if (!sock_flag(sk
, SOCK_DEAD
))
591 sk
->sk_data_ready(sk
);
592 return NET_RX_SUCCESS
;
594 case PNS_PEP_CONNECT_RESP
:
595 if (sk
->sk_state
!= TCP_SYN_SENT
)
597 if (!sock_flag(sk
, SOCK_DEAD
))
598 sk
->sk_state_change(sk
);
599 if (pep_connresp_rcv(sk
, skb
)) {
600 sk
->sk_state
= TCP_CLOSE_WAIT
;
603 if (pn
->init_enable
== PN_PIPE_DISABLE
)
604 sk
->sk_state
= TCP_SYN_RECV
;
606 sk
->sk_state
= TCP_ESTABLISHED
;
607 pipe_start_flow_control(sk
);
611 case PNS_PEP_ENABLE_RESP
:
612 if (sk
->sk_state
!= TCP_SYN_SENT
)
615 if (pep_enableresp_rcv(sk
, skb
)) {
616 sk
->sk_state
= TCP_CLOSE_WAIT
;
620 sk
->sk_state
= TCP_ESTABLISHED
;
621 pipe_start_flow_control(sk
);
624 case PNS_PEP_DISCONNECT_RESP
:
625 /* sock should already be dead, nothing to do */
628 case PNS_PEP_STATUS_IND
:
629 pipe_rcv_status(sk
, skb
);
636 /* Listening sock must be locked */
637 static struct sock
*pep_find_pipe(const struct hlist_head
*hlist
,
638 const struct sockaddr_pn
*dst
,
642 u16 dobj
= pn_sockaddr_get_object(dst
);
644 sk_for_each(sknode
, hlist
) {
645 struct pep_sock
*pnnode
= pep_sk(sknode
);
647 /* Ports match, but addresses might not: */
648 if (pnnode
->pn_sk
.sobject
!= dobj
)
650 if (pnnode
->pipe_handle
!= pipe_handle
)
652 if (sknode
->sk_state
== TCP_CLOSE_WAIT
)
662 * Deliver an skb to a listening sock.
663 * Socket lock must be held.
664 * We then queue the skb to the right connected sock (if any).
666 static int pep_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
668 struct pep_sock
*pn
= pep_sk(sk
);
670 struct pnpipehdr
*hdr
;
671 struct sockaddr_pn dst
;
674 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
678 pipe_handle
= hdr
->pipe_handle
;
679 if (pipe_handle
== PN_PIPE_INVALID_HANDLE
)
682 pn_skb_get_dst_sockaddr(skb
, &dst
);
684 /* Look for an existing pipe handle */
685 sknode
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
687 return sk_receive_skb(sknode
, skb
, 1);
689 switch (hdr
->message_id
) {
690 case PNS_PEP_CONNECT_REQ
:
691 if (sk
->sk_state
!= TCP_LISTEN
|| sk_acceptq_is_full(sk
)) {
692 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
,
696 skb_queue_head(&sk
->sk_receive_queue
, skb
);
697 sk_acceptq_added(sk
);
698 if (!sock_flag(sk
, SOCK_DEAD
))
699 sk
->sk_data_ready(sk
);
700 return NET_RX_SUCCESS
;
702 case PNS_PEP_DISCONNECT_REQ
:
703 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
706 case PNS_PEP_CTRL_REQ
:
707 pep_ctrlreq_error(sk
, skb
, PN_PIPE_INVALID_HANDLE
, GFP_ATOMIC
);
710 case PNS_PEP_RESET_REQ
:
711 case PNS_PEP_ENABLE_REQ
:
712 case PNS_PEP_DISABLE_REQ
:
713 /* invalid handle is not even allowed here! */
717 if ((1 << sk
->sk_state
)
718 & ~(TCPF_CLOSE
|TCPF_LISTEN
|TCPF_CLOSE_WAIT
))
719 /* actively connected socket */
720 return pipe_handler_do_rcv(sk
, skb
);
724 return NET_RX_SUCCESS
;
727 static int pipe_do_remove(struct sock
*sk
)
729 struct pep_sock
*pn
= pep_sk(sk
);
730 struct pnpipehdr
*ph
;
733 skb
= pep_alloc_skb(sk
, NULL
, 0, GFP_KERNEL
);
739 ph
->message_id
= PNS_PIPE_REMOVE_REQ
;
740 ph
->pipe_handle
= pn
->pipe_handle
;
742 return pn_skb_send(sk
, skb
, NULL
);
745 /* associated socket ceases to exist */
746 static void pep_sock_close(struct sock
*sk
, long timeout
)
748 struct pep_sock
*pn
= pep_sk(sk
);
751 sock_hold(sk
); /* keep a reference after sk_common_release() */
752 sk_common_release(sk
);
755 if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
)) {
756 if (sk
->sk_backlog_rcv
== pipe_do_rcv
)
757 /* Forcefully remove dangling Phonet pipe */
760 pipe_handler_request(sk
, PNS_PEP_DISCONNECT_REQ
, PAD
,
763 sk
->sk_state
= TCP_CLOSE
;
765 ifindex
= pn
->ifindex
;
774 static struct sock
*pep_sock_accept(struct sock
*sk
, int flags
, int *errp
)
776 struct pep_sock
*pn
= pep_sk(sk
), *newpn
;
777 struct sock
*newsk
= NULL
;
779 struct pnpipehdr
*hdr
;
780 struct sockaddr_pn dst
, src
;
783 u8 pipe_handle
, enabled
, n_sb
;
786 skb
= skb_recv_datagram(sk
, 0, flags
& O_NONBLOCK
, errp
);
791 if (sk
->sk_state
!= TCP_LISTEN
) {
795 sk_acceptq_removed(sk
);
798 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
802 pipe_handle
= hdr
->pipe_handle
;
803 switch (hdr
->state_after_connect
) {
804 case PN_PIPE_DISABLE
:
811 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_INVALID_PARAM
,
815 peer_type
= hdr
->other_pep_type
<< 8;
817 /* Parse sub-blocks (options) */
820 u8 type
, buf
[1], len
= sizeof(buf
);
821 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
826 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE
:
829 peer_type
= (peer_type
& 0xff00) | data
[0];
831 case PN_PIPE_SB_ALIGNED_DATA
:
832 aligned
= data
[0] != 0;
838 /* Check for duplicate pipe handle */
839 newsk
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
840 if (unlikely(newsk
)) {
843 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
, GFP_KERNEL
);
847 /* Create a new to-be-accepted sock */
848 newsk
= sk_alloc(sock_net(sk
), PF_PHONET
, GFP_KERNEL
, sk
->sk_prot
);
850 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_OVERLOAD
, GFP_KERNEL
);
855 sock_init_data(NULL
, newsk
);
856 newsk
->sk_state
= TCP_SYN_RECV
;
857 newsk
->sk_backlog_rcv
= pipe_do_rcv
;
858 newsk
->sk_protocol
= sk
->sk_protocol
;
859 newsk
->sk_destruct
= pipe_destruct
;
861 newpn
= pep_sk(newsk
);
862 pn_skb_get_dst_sockaddr(skb
, &dst
);
863 pn_skb_get_src_sockaddr(skb
, &src
);
864 newpn
->pn_sk
.sobject
= pn_sockaddr_get_object(&dst
);
865 newpn
->pn_sk
.dobject
= pn_sockaddr_get_object(&src
);
866 newpn
->pn_sk
.resource
= pn_sockaddr_get_resource(&dst
);
868 newpn
->listener
= sk
;
869 skb_queue_head_init(&newpn
->ctrlreq_queue
);
870 newpn
->pipe_handle
= pipe_handle
;
871 atomic_set(&newpn
->tx_credits
, 0);
873 newpn
->peer_type
= peer_type
;
874 newpn
->rx_credits
= 0;
875 newpn
->rx_fc
= newpn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
876 newpn
->init_enable
= enabled
;
877 newpn
->aligned
= aligned
;
879 err
= pep_accept_conn(newsk
, skb
);
885 sk_add_node(newsk
, &pn
->hlist
);
893 static int pep_sock_connect(struct sock
*sk
, struct sockaddr
*addr
, int len
)
895 struct pep_sock
*pn
= pep_sk(sk
);
897 u8 data
[4] = { 0 /* sub-blocks */, PAD
, PAD
, PAD
};
899 if (pn
->pipe_handle
== PN_PIPE_INVALID_HANDLE
)
900 pn
->pipe_handle
= 1; /* anything but INVALID_HANDLE */
902 err
= pipe_handler_request(sk
, PNS_PEP_CONNECT_REQ
,
903 pn
->init_enable
, data
, 4);
905 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
909 sk
->sk_state
= TCP_SYN_SENT
;
914 static int pep_sock_enable(struct sock
*sk
, struct sockaddr
*addr
, int len
)
918 err
= pipe_handler_request(sk
, PNS_PEP_ENABLE_REQ
, PAD
,
923 sk
->sk_state
= TCP_SYN_SENT
;
928 static int pep_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
930 struct pep_sock
*pn
= pep_sk(sk
);
932 int ret
= -ENOIOCTLCMD
;
936 if (sk
->sk_state
== TCP_LISTEN
) {
942 if (sock_flag(sk
, SOCK_URGINLINE
) &&
943 !skb_queue_empty(&pn
->ctrlreq_queue
))
944 answ
= skb_peek(&pn
->ctrlreq_queue
)->len
;
945 else if (!skb_queue_empty(&sk
->sk_receive_queue
))
946 answ
= skb_peek(&sk
->sk_receive_queue
)->len
;
950 ret
= put_user(answ
, (int __user
*)arg
);
953 case SIOCPNENABLEPIPE
:
955 if (sk
->sk_state
== TCP_SYN_SENT
)
957 else if (sk
->sk_state
== TCP_ESTABLISHED
)
960 ret
= pep_sock_enable(sk
, NULL
, 0);
968 static int pep_init(struct sock
*sk
)
970 struct pep_sock
*pn
= pep_sk(sk
);
972 sk
->sk_destruct
= pipe_destruct
;
973 INIT_HLIST_HEAD(&pn
->hlist
);
975 skb_queue_head_init(&pn
->ctrlreq_queue
);
976 atomic_set(&pn
->tx_credits
, 0);
979 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
981 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
987 static int pep_setsockopt(struct sock
*sk
, int level
, int optname
,
988 char __user
*optval
, unsigned int optlen
)
990 struct pep_sock
*pn
= pep_sk(sk
);
991 int val
= 0, err
= 0;
993 if (level
!= SOL_PNPIPE
)
995 if (optlen
>= sizeof(int)) {
996 if (get_user(val
, (int __user
*) optval
))
1003 if (val
&& val
!= PNPIPE_ENCAP_IP
) {
1007 if (!pn
->ifindex
== !val
)
1008 break; /* Nothing to do! */
1009 if (!capable(CAP_NET_ADMIN
)) {
1015 err
= gprs_attach(sk
);
1029 if ((sk
->sk_state
== TCP_CLOSE
) &&
1030 (val
>= 0) && (val
< PN_PIPE_INVALID_HANDLE
))
1031 pn
->pipe_handle
= val
;
1036 case PNPIPE_INITSTATE
:
1037 pn
->init_enable
= !!val
;
1049 static int pep_getsockopt(struct sock
*sk
, int level
, int optname
,
1050 char __user
*optval
, int __user
*optlen
)
1052 struct pep_sock
*pn
= pep_sk(sk
);
1055 if (level
!= SOL_PNPIPE
)
1056 return -ENOPROTOOPT
;
1057 if (get_user(len
, optlen
))
1062 val
= pn
->ifindex
? PNPIPE_ENCAP_IP
: PNPIPE_ENCAP_NONE
;
1065 case PNPIPE_IFINDEX
:
1070 val
= pn
->pipe_handle
;
1071 if (val
== PN_PIPE_INVALID_HANDLE
)
1075 case PNPIPE_INITSTATE
:
1076 val
= pn
->init_enable
;
1080 return -ENOPROTOOPT
;
1083 len
= min_t(unsigned int, sizeof(int), len
);
1084 if (put_user(len
, optlen
))
1086 if (put_user(val
, (int __user
*) optval
))
1091 static int pipe_skb_send(struct sock
*sk
, struct sk_buff
*skb
)
1093 struct pep_sock
*pn
= pep_sk(sk
);
1094 struct pnpipehdr
*ph
;
1097 if (pn_flow_safe(pn
->tx_fc
) &&
1098 !atomic_add_unless(&pn
->tx_credits
, -1, 0)) {
1103 skb_push(skb
, 3 + pn
->aligned
);
1104 skb_reset_transport_header(skb
);
1108 ph
->message_id
= PNS_PIPE_ALIGNED_DATA
;
1109 ph
->data
[0] = 0; /* padding */
1111 ph
->message_id
= PNS_PIPE_DATA
;
1112 ph
->pipe_handle
= pn
->pipe_handle
;
1113 err
= pn_skb_send(sk
, skb
, NULL
);
1115 if (err
&& pn_flow_safe(pn
->tx_fc
))
1116 atomic_inc(&pn
->tx_credits
);
1121 static int pep_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1122 struct msghdr
*msg
, size_t len
)
1124 struct pep_sock
*pn
= pep_sk(sk
);
1125 struct sk_buff
*skb
;
1127 int flags
= msg
->msg_flags
;
1130 if (len
> USHRT_MAX
)
1133 if ((msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|
1134 MSG_CMSG_COMPAT
)) ||
1135 !(msg
->msg_flags
& MSG_EOR
))
1138 skb
= sock_alloc_send_skb(sk
, MAX_PNPIPE_HEADER
+ len
,
1139 flags
& MSG_DONTWAIT
, &err
);
1143 skb_reserve(skb
, MAX_PHONET_HEADER
+ 3 + pn
->aligned
);
1144 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
1149 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
1150 if ((1 << sk
->sk_state
) & (TCPF_LISTEN
|TCPF_CLOSE
)) {
1154 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1155 /* Wait until the pipe gets to enabled state */
1157 err
= sk_stream_wait_connect(sk
, &timeo
);
1161 if (sk
->sk_state
== TCP_CLOSE_WAIT
) {
1166 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
1168 /* Wait until flow control allows TX */
1169 done
= atomic_read(&pn
->tx_credits
);
1177 if (signal_pending(current
)) {
1178 err
= sock_intr_errno(timeo
);
1182 prepare_to_wait(sk_sleep(sk
), &wait
,
1183 TASK_INTERRUPTIBLE
);
1184 done
= sk_wait_event(sk
, &timeo
, atomic_read(&pn
->tx_credits
));
1185 finish_wait(sk_sleep(sk
), &wait
);
1187 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1191 err
= pipe_skb_send(sk
, skb
);
1193 err
= len
; /* success! */
1202 int pep_writeable(struct sock
*sk
)
1204 struct pep_sock
*pn
= pep_sk(sk
);
1206 return atomic_read(&pn
->tx_credits
);
1209 int pep_write(struct sock
*sk
, struct sk_buff
*skb
)
1211 struct sk_buff
*rskb
, *fs
;
1214 if (pep_sk(sk
)->aligned
)
1215 return pipe_skb_send(sk
, skb
);
1217 rskb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
1222 skb_shinfo(rskb
)->frag_list
= skb
;
1223 rskb
->len
+= skb
->len
;
1224 rskb
->data_len
+= rskb
->len
;
1225 rskb
->truesize
+= rskb
->len
;
1227 /* Avoid nested fragments */
1228 skb_walk_frags(skb
, fs
)
1230 skb
->next
= skb_shinfo(skb
)->frag_list
;
1231 skb_frag_list_init(skb
);
1233 skb
->data_len
-= flen
;
1234 skb
->truesize
-= flen
;
1236 skb_reserve(rskb
, MAX_PHONET_HEADER
+ 3);
1237 return pipe_skb_send(sk
, rskb
);
1240 struct sk_buff
*pep_read(struct sock
*sk
)
1242 struct sk_buff
*skb
= skb_dequeue(&sk
->sk_receive_queue
);
1244 if (sk
->sk_state
== TCP_ESTABLISHED
)
1245 pipe_grant_credits(sk
, GFP_ATOMIC
);
1249 static int pep_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1250 struct msghdr
*msg
, size_t len
, int noblock
,
1251 int flags
, int *addr_len
)
1253 struct sk_buff
*skb
;
1256 if (flags
& ~(MSG_OOB
|MSG_PEEK
|MSG_TRUNC
|MSG_DONTWAIT
|MSG_WAITALL
|
1257 MSG_NOSIGNAL
|MSG_CMSG_COMPAT
))
1260 if (unlikely(1 << sk
->sk_state
& (TCPF_LISTEN
| TCPF_CLOSE
)))
1263 if ((flags
& MSG_OOB
) || sock_flag(sk
, SOCK_URGINLINE
)) {
1264 /* Dequeue and acknowledge control request */
1265 struct pep_sock
*pn
= pep_sk(sk
);
1267 if (flags
& MSG_PEEK
)
1269 skb
= skb_dequeue(&pn
->ctrlreq_queue
);
1271 pep_ctrlreq_error(sk
, skb
, PN_PIPE_NO_ERROR
,
1273 msg
->msg_flags
|= MSG_OOB
;
1276 if (flags
& MSG_OOB
)
1280 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1283 if (err
== -ENOTCONN
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
1289 if (sk
->sk_state
== TCP_ESTABLISHED
)
1290 pipe_grant_credits(sk
, GFP_KERNEL
);
1293 msg
->msg_flags
|= MSG_EOR
;
1295 msg
->msg_flags
|= MSG_TRUNC
;
1299 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
1301 err
= (flags
& MSG_TRUNC
) ? skb
->len
: len
;
1303 skb_free_datagram(sk
, skb
);
1307 static void pep_sock_unhash(struct sock
*sk
)
1309 struct pep_sock
*pn
= pep_sk(sk
);
1310 struct sock
*skparent
= NULL
;
1314 if (pn
->listener
!= NULL
) {
1315 skparent
= pn
->listener
;
1316 pn
->listener
= NULL
;
1319 pn
= pep_sk(skparent
);
1320 lock_sock(skparent
);
1321 sk_del_node_init(sk
);
1325 /* Unhash a listening sock only when it is closed
1326 * and all of its active connected pipes are closed. */
1327 if (hlist_empty(&pn
->hlist
))
1328 pn_sock_unhash(&pn
->pn_sk
.sk
);
1335 static struct proto pep_proto
= {
1336 .close
= pep_sock_close
,
1337 .accept
= pep_sock_accept
,
1338 .connect
= pep_sock_connect
,
1341 .setsockopt
= pep_setsockopt
,
1342 .getsockopt
= pep_getsockopt
,
1343 .sendmsg
= pep_sendmsg
,
1344 .recvmsg
= pep_recvmsg
,
1345 .backlog_rcv
= pep_do_rcv
,
1346 .hash
= pn_sock_hash
,
1347 .unhash
= pep_sock_unhash
,
1348 .get_port
= pn_sock_get_port
,
1349 .obj_size
= sizeof(struct pep_sock
),
1350 .owner
= THIS_MODULE
,
1354 static struct phonet_protocol pep_pn_proto
= {
1355 .ops
= &phonet_stream_ops
,
1357 .sock_type
= SOCK_SEQPACKET
,
1360 static int __init
pep_register(void)
1362 return phonet_proto_register(PN_PROTO_PIPE
, &pep_pn_proto
);
1365 static void __exit
pep_unregister(void)
1367 phonet_proto_unregister(PN_PROTO_PIPE
, &pep_pn_proto
);
1370 module_init(pep_register
);
1371 module_exit(pep_unregister
);
1372 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1373 MODULE_DESCRIPTION("Phonet pipe protocol");
1374 MODULE_LICENSE("GPL");
1375 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET
, PN_PROTO_PIPE
);