4 * Phonet pipe protocol end point socket
6 * Copyright (C) 2008 Nokia Corporation.
8 * Author: RĂ©mi Denis-Courmont <remi.denis-courmont@nokia.com>
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/socket.h>
28 #include <net/tcp_states.h>
29 #include <asm/ioctls.h>
31 #include <linux/phonet.h>
32 #include <net/phonet/phonet.h>
33 #include <net/phonet/pep.h>
34 #include <net/phonet/gprs.h>
37 * TCP_CLOSE sock not in use yet
38 * TCP_CLOSE_WAIT disconnected pipe
39 * TCP_LISTEN listening pipe endpoint
40 * TCP_SYN_RECV connected pipe in disabled state
41 * TCP_ESTABLISHED connected pipe in enabled state
44 * - sk_state, ackq, hlist: sock lock needed
45 * - listener: read only
46 * - pipe_handle: read only
49 #define CREDITS_MAX 10
52 static const struct sockaddr_pn pipe_srv
= {
53 .spn_family
= AF_PHONET
,
54 .spn_resource
= 0xD9, /* pipe service */
57 #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
59 /* Get the next TLV sub-block. */
60 static unsigned char *pep_get_sb(struct sk_buff
*skb
, u8
*ptype
, u8
*plen
,
70 ph
= skb_header_pointer(skb
, 0, 2, &h
);
71 if (ph
== NULL
|| ph
->sb_len
< 2 || !pskb_may_pull(skb
, ph
->sb_len
))
77 if (buflen
> ph
->sb_len
)
79 data
= skb_header_pointer(skb
, 2, buflen
, buf
);
80 __skb_pull(skb
, 2 + ph
->sb_len
);
84 static int pep_reply(struct sock
*sk
, struct sk_buff
*oskb
,
85 u8 code
, const void *data
, int len
, gfp_t priority
)
87 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
91 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ len
, priority
);
94 skb_set_owner_w(skb
, sk
);
96 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
98 skb_copy_to_linear_data(skb
, data
, len
);
99 __skb_push(skb
, sizeof(*ph
));
100 skb_reset_transport_header(skb
);
102 ph
->utid
= oph
->utid
;
103 ph
->message_id
= oph
->message_id
+ 1; /* REQ -> RESP */
104 ph
->pipe_handle
= oph
->pipe_handle
;
105 ph
->error_code
= code
;
107 return pn_skb_send(sk
, skb
, &pipe_srv
);
111 static int pep_accept_conn(struct sock
*sk
, struct sk_buff
*skb
)
113 static const u8 data
[20] = {
114 PAD
, PAD
, PAD
, 2 /* sub-blocks */,
115 PN_PIPE_SB_REQUIRED_FC_TX
, pep_sb_size(5), 3, PAD
,
116 PN_MULTI_CREDIT_FLOW_CONTROL
,
117 PN_ONE_CREDIT_FLOW_CONTROL
,
118 PN_LEGACY_FLOW_CONTROL
,
120 PN_PIPE_SB_PREFERRED_FC_RX
, pep_sb_size(5), 3, PAD
,
121 PN_MULTI_CREDIT_FLOW_CONTROL
,
122 PN_ONE_CREDIT_FLOW_CONTROL
,
123 PN_LEGACY_FLOW_CONTROL
,
128 return pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, data
, sizeof(data
),
132 static int pep_reject_conn(struct sock
*sk
, struct sk_buff
*skb
, u8 code
)
134 static const u8 data
[4] = { PAD
, PAD
, PAD
, 0 /* sub-blocks */ };
135 WARN_ON(code
== PN_PIPE_NO_ERROR
);
136 return pep_reply(sk
, skb
, code
, data
, sizeof(data
), GFP_ATOMIC
);
139 /* Control requests are not sent by the pipe service and have a specific
141 static int pep_ctrlreq_error(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
144 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
146 struct pnpipehdr
*ph
;
147 struct sockaddr_pn dst
;
149 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
152 skb_set_owner_w(skb
, sk
);
154 skb_reserve(skb
, MAX_PHONET_HEADER
);
155 ph
= (struct pnpipehdr
*)skb_put(skb
, sizeof(*ph
) + 4);
157 ph
->utid
= oph
->utid
;
158 ph
->message_id
= PNS_PEP_CTRL_RESP
;
159 ph
->pipe_handle
= oph
->pipe_handle
;
160 ph
->data
[0] = oph
->data
[1]; /* CTRL id */
161 ph
->data
[1] = oph
->data
[0]; /* PEP type */
162 ph
->data
[2] = code
; /* error code, at an usual offset */
166 pn_skb_get_src_sockaddr(oskb
, &dst
);
167 return pn_skb_send(sk
, skb
, &dst
);
170 static int pipe_snd_status(struct sock
*sk
, u8 type
, u8 status
, gfp_t priority
)
172 struct pep_sock
*pn
= pep_sk(sk
);
173 struct pnpipehdr
*ph
;
176 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
179 skb_set_owner_w(skb
, sk
);
181 skb_reserve(skb
, MAX_PNPIPE_HEADER
+ 4);
182 __skb_push(skb
, sizeof(*ph
) + 4);
183 skb_reset_transport_header(skb
);
186 ph
->message_id
= PNS_PEP_STATUS_IND
;
187 ph
->pipe_handle
= pn
->pipe_handle
;
188 ph
->pep_type
= PN_PEP_TYPE_COMMON
;
192 ph
->data
[4] = status
;
194 return pn_skb_send(sk
, skb
, &pipe_srv
);
197 /* Send our RX flow control information to the sender.
198 * Socket must be locked. */
199 static void pipe_grant_credits(struct sock
*sk
)
201 struct pep_sock
*pn
= pep_sk(sk
);
203 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
206 case PN_LEGACY_FLOW_CONTROL
: /* TODO */
208 case PN_ONE_CREDIT_FLOW_CONTROL
:
209 pipe_snd_status(sk
, PN_PEP_IND_FLOW_CONTROL
,
210 PEP_IND_READY
, GFP_ATOMIC
);
213 case PN_MULTI_CREDIT_FLOW_CONTROL
:
214 if ((pn
->rx_credits
+ CREDITS_THR
) > CREDITS_MAX
)
216 if (pipe_snd_status(sk
, PN_PEP_IND_ID_MCFC_GRANT_CREDITS
,
217 CREDITS_MAX
- pn
->rx_credits
,
219 pn
->rx_credits
= CREDITS_MAX
;
224 static int pipe_rcv_status(struct sock
*sk
, struct sk_buff
*skb
)
226 struct pep_sock
*pn
= pep_sk(sk
);
227 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
230 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
233 if (hdr
->data
[0] != PN_PEP_TYPE_COMMON
) {
234 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP type: %u\n",
235 (unsigned)hdr
->data
[0]);
239 switch (hdr
->data
[1]) {
240 case PN_PEP_IND_FLOW_CONTROL
:
242 case PN_LEGACY_FLOW_CONTROL
:
243 switch (hdr
->data
[4]) {
245 atomic_set(&pn
->tx_credits
, 0);
248 atomic_set(&pn
->tx_credits
, wake
= 1);
252 case PN_ONE_CREDIT_FLOW_CONTROL
:
253 if (hdr
->data
[4] == PEP_IND_READY
)
254 atomic_set(&pn
->tx_credits
, wake
= 1);
259 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS
:
260 if (pn
->tx_fc
!= PN_MULTI_CREDIT_FLOW_CONTROL
)
262 atomic_add(wake
= hdr
->data
[4], &pn
->tx_credits
);
266 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP indication: %u\n",
267 (unsigned)hdr
->data
[1]);
271 sk
->sk_write_space(sk
);
275 static int pipe_rcv_created(struct sock
*sk
, struct sk_buff
*skb
)
277 struct pep_sock
*pn
= pep_sk(sk
);
278 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
279 u8 n_sb
= hdr
->data
[0];
281 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
282 __skb_pull(skb
, sizeof(*hdr
));
284 u8 type
, buf
[2], len
= sizeof(buf
);
285 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
290 case PN_PIPE_SB_NEGOTIATED_FC
:
291 if (len
< 2 || (data
[0] | data
[1]) > 3)
293 pn
->tx_fc
= data
[0] & 3;
294 pn
->rx_fc
= data
[1] & 3;
302 /* Queue an skb to a connected sock.
303 * Socket lock must be held. */
304 static int pipe_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
306 struct pep_sock
*pn
= pep_sk(sk
);
307 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
308 struct sk_buff_head
*queue
;
311 BUG_ON(sk
->sk_state
== TCP_CLOSE_WAIT
);
313 switch (hdr
->message_id
) {
314 case PNS_PEP_CONNECT_REQ
:
315 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
318 case PNS_PEP_DISCONNECT_REQ
:
319 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
320 sk
->sk_state
= TCP_CLOSE_WAIT
;
321 if (!sock_flag(sk
, SOCK_DEAD
))
322 sk
->sk_state_change(sk
);
325 case PNS_PEP_ENABLE_REQ
:
326 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
327 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
330 case PNS_PEP_RESET_REQ
:
331 switch (hdr
->state_after_reset
) {
332 case PN_PIPE_DISABLE
:
338 default: /* not allowed to send an error here!? */
343 case PNS_PEP_DISABLE_REQ
:
344 atomic_set(&pn
->tx_credits
, 0);
345 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
348 case PNS_PEP_CTRL_REQ
:
349 if (skb_queue_len(&pn
->ctrlreq_queue
) >= PNPIPE_CTRLREQ_MAX
) {
350 atomic_inc(&sk
->sk_drops
);
354 queue
= &pn
->ctrlreq_queue
;
357 case PNS_PIPE_ALIGNED_DATA
:
361 __skb_pull(skb
, 3); /* Pipe data header */
362 if (!pn_flow_safe(pn
->rx_fc
)) {
363 err
= sock_queue_rcv_skb(sk
, skb
);
369 if (pn
->rx_credits
== 0) {
370 atomic_inc(&sk
->sk_drops
);
375 queue
= &sk
->sk_receive_queue
;
378 case PNS_PEP_STATUS_IND
:
379 pipe_rcv_status(sk
, skb
);
382 case PNS_PIPE_REDIRECTED_IND
:
383 err
= pipe_rcv_created(sk
, skb
);
386 case PNS_PIPE_CREATED_IND
:
387 err
= pipe_rcv_created(sk
, skb
);
391 case PNS_PIPE_RESET_IND
:
392 if (!pn
->init_enable
)
395 case PNS_PIPE_ENABLED_IND
:
396 if (!pn_flow_safe(pn
->tx_fc
)) {
397 atomic_set(&pn
->tx_credits
, 1);
398 sk
->sk_write_space(sk
);
400 if (sk
->sk_state
== TCP_ESTABLISHED
)
401 break; /* Nothing to do */
402 sk
->sk_state
= TCP_ESTABLISHED
;
403 pipe_grant_credits(sk
);
406 case PNS_PIPE_DISABLED_IND
:
407 sk
->sk_state
= TCP_SYN_RECV
;
412 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP message: %u\n",
422 skb_set_owner_r(skb
, sk
);
424 skb_queue_tail(queue
, skb
);
425 if (!sock_flag(sk
, SOCK_DEAD
))
426 sk
->sk_data_ready(sk
, err
);
430 /* Destroy connected sock. */
431 static void pipe_destruct(struct sock
*sk
)
433 struct pep_sock
*pn
= pep_sk(sk
);
435 skb_queue_purge(&sk
->sk_receive_queue
);
436 skb_queue_purge(&pn
->ctrlreq_queue
);
439 static int pep_connreq_rcv(struct sock
*sk
, struct sk_buff
*skb
)
442 struct pep_sock
*newpn
, *pn
= pep_sk(sk
);
443 struct pnpipehdr
*hdr
;
444 struct sockaddr_pn dst
;
446 u8 pipe_handle
, enabled
, n_sb
;
449 if (!pskb_pull(skb
, sizeof(*hdr
) + 4))
453 pipe_handle
= hdr
->pipe_handle
;
454 switch (hdr
->state_after_connect
) {
455 case PN_PIPE_DISABLE
:
462 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_INVALID_PARAM
);
465 peer_type
= hdr
->other_pep_type
<< 8;
467 if (unlikely(sk
->sk_state
!= TCP_LISTEN
) || sk_acceptq_is_full(sk
)) {
468 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
472 /* Parse sub-blocks (options) */
475 u8 type
, buf
[1], len
= sizeof(buf
);
476 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
481 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE
:
484 peer_type
= (peer_type
& 0xff00) | data
[0];
486 case PN_PIPE_SB_ALIGNED_DATA
:
487 aligned
= data
[0] != 0;
493 skb
= skb_clone(skb
, GFP_ATOMIC
);
497 /* Create a new to-be-accepted sock */
498 newsk
= sk_alloc(sock_net(sk
), PF_PHONET
, GFP_ATOMIC
, sk
->sk_prot
);
503 sock_init_data(NULL
, newsk
);
504 newsk
->sk_state
= TCP_SYN_RECV
;
505 newsk
->sk_backlog_rcv
= pipe_do_rcv
;
506 newsk
->sk_protocol
= sk
->sk_protocol
;
507 newsk
->sk_destruct
= pipe_destruct
;
509 newpn
= pep_sk(newsk
);
510 pn_skb_get_dst_sockaddr(skb
, &dst
);
511 newpn
->pn_sk
.sobject
= pn_sockaddr_get_object(&dst
);
512 newpn
->pn_sk
.resource
= pn
->pn_sk
.resource
;
513 skb_queue_head_init(&newpn
->ctrlreq_queue
);
514 newpn
->pipe_handle
= pipe_handle
;
515 atomic_set(&newpn
->tx_credits
, 0);
516 newpn
->peer_type
= peer_type
;
517 newpn
->rx_credits
= 0;
518 newpn
->rx_fc
= newpn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
519 newpn
->init_enable
= enabled
;
520 newpn
->aligned
= aligned
;
522 BUG_ON(!skb_queue_empty(&newsk
->sk_receive_queue
));
523 skb_queue_head(&newsk
->sk_receive_queue
, skb
);
524 if (!sock_flag(sk
, SOCK_DEAD
))
525 sk
->sk_data_ready(sk
, 0);
527 sk_acceptq_added(sk
);
528 sk_add_node(newsk
, &pn
->ackq
);
532 /* Listening sock must be locked */
533 static struct sock
*pep_find_pipe(const struct hlist_head
*hlist
,
534 const struct sockaddr_pn
*dst
,
537 struct hlist_node
*node
;
539 u16 dobj
= pn_sockaddr_get_object(dst
);
541 sk_for_each(sknode
, node
, hlist
) {
542 struct pep_sock
*pnnode
= pep_sk(sknode
);
544 /* Ports match, but addresses might not: */
545 if (pnnode
->pn_sk
.sobject
!= dobj
)
547 if (pnnode
->pipe_handle
!= pipe_handle
)
549 if (sknode
->sk_state
== TCP_CLOSE_WAIT
)
559 * Deliver an skb to a listening sock.
560 * Socket lock must be held.
561 * We then queue the skb to the right connected sock (if any).
563 static int pep_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
565 struct pep_sock
*pn
= pep_sk(sk
);
567 struct pnpipehdr
*hdr
;
568 struct sockaddr_pn dst
;
569 int err
= NET_RX_SUCCESS
;
572 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
576 pipe_handle
= hdr
->pipe_handle
;
577 if (pipe_handle
== PN_PIPE_INVALID_HANDLE
)
580 pn_skb_get_dst_sockaddr(skb
, &dst
);
582 /* Look for an existing pipe handle */
583 sknode
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
585 return sk_receive_skb(sknode
, skb
, 1);
587 /* Look for a pipe handle pending accept */
588 sknode
= pep_find_pipe(&pn
->ackq
, &dst
, pipe_handle
);
592 printk(KERN_WARNING
"Phonet unconnected PEP ignored");
597 switch (hdr
->message_id
) {
598 case PNS_PEP_CONNECT_REQ
:
599 err
= pep_connreq_rcv(sk
, skb
);
602 case PNS_PEP_DISCONNECT_REQ
:
603 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
606 case PNS_PEP_CTRL_REQ
:
607 pep_ctrlreq_error(sk
, skb
, PN_PIPE_INVALID_HANDLE
, GFP_ATOMIC
);
610 case PNS_PEP_RESET_REQ
:
611 case PNS_PEP_ENABLE_REQ
:
612 case PNS_PEP_DISABLE_REQ
:
613 /* invalid handle is not even allowed here! */
622 /* associated socket ceases to exist */
623 static void pep_sock_close(struct sock
*sk
, long timeout
)
625 struct pep_sock
*pn
= pep_sk(sk
);
628 sk_common_release(sk
);
631 if (sk
->sk_state
== TCP_LISTEN
) {
632 /* Destroy the listen queue */
634 struct hlist_node
*p
, *n
;
636 sk_for_each_safe(sknode
, p
, n
, &pn
->ackq
)
637 sk_del_node_init(sknode
);
638 sk
->sk_state
= TCP_CLOSE
;
640 ifindex
= pn
->ifindex
;
648 static int pep_wait_connreq(struct sock
*sk
, int noblock
)
650 struct task_struct
*tsk
= current
;
651 struct pep_sock
*pn
= pep_sk(sk
);
652 long timeo
= sock_rcvtimeo(sk
, noblock
);
657 if (sk
->sk_state
!= TCP_LISTEN
)
659 if (!hlist_empty(&pn
->ackq
))
663 if (signal_pending(tsk
))
664 return sock_intr_errno(timeo
);
666 prepare_to_wait_exclusive(&sk
->sk_socket
->wait
, &wait
,
669 timeo
= schedule_timeout(timeo
);
671 finish_wait(&sk
->sk_socket
->wait
, &wait
);
677 static struct sock
*pep_sock_accept(struct sock
*sk
, int flags
, int *errp
)
679 struct pep_sock
*pn
= pep_sk(sk
);
680 struct sock
*newsk
= NULL
;
681 struct sk_buff
*oskb
;
685 err
= pep_wait_connreq(sk
, flags
& O_NONBLOCK
);
689 newsk
= __sk_head(&pn
->ackq
);
691 oskb
= skb_dequeue(&newsk
->sk_receive_queue
);
692 err
= pep_accept_conn(newsk
, oskb
);
694 skb_queue_head(&newsk
->sk_receive_queue
, oskb
);
700 pep_sk(newsk
)->listener
= sk
;
703 sk_del_node_init(newsk
);
704 sk_acceptq_removed(sk
);
705 sk_add_node(newsk
, &pn
->hlist
);
714 static int pep_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
716 struct pep_sock
*pn
= pep_sk(sk
);
721 if (sk
->sk_state
== TCP_LISTEN
)
725 if (sock_flag(sk
, SOCK_URGINLINE
) &&
726 !skb_queue_empty(&pn
->ctrlreq_queue
))
727 answ
= skb_peek(&pn
->ctrlreq_queue
)->len
;
728 else if (!skb_queue_empty(&sk
->sk_receive_queue
))
729 answ
= skb_peek(&sk
->sk_receive_queue
)->len
;
733 return put_user(answ
, (int __user
*)arg
);
739 static int pep_init(struct sock
*sk
)
741 struct pep_sock
*pn
= pep_sk(sk
);
743 INIT_HLIST_HEAD(&pn
->ackq
);
744 INIT_HLIST_HEAD(&pn
->hlist
);
745 skb_queue_head_init(&pn
->ctrlreq_queue
);
746 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
750 static int pep_setsockopt(struct sock
*sk
, int level
, int optname
,
751 char __user
*optval
, unsigned int optlen
)
753 struct pep_sock
*pn
= pep_sk(sk
);
754 int val
= 0, err
= 0;
756 if (level
!= SOL_PNPIPE
)
758 if (optlen
>= sizeof(int)) {
759 if (get_user(val
, (int __user
*) optval
))
766 if (val
&& val
!= PNPIPE_ENCAP_IP
) {
770 if (!pn
->ifindex
== !val
)
771 break; /* Nothing to do! */
772 if (!capable(CAP_NET_ADMIN
)) {
778 err
= gprs_attach(sk
);
799 static int pep_getsockopt(struct sock
*sk
, int level
, int optname
,
800 char __user
*optval
, int __user
*optlen
)
802 struct pep_sock
*pn
= pep_sk(sk
);
805 if (level
!= SOL_PNPIPE
)
807 if (get_user(len
, optlen
))
812 val
= pn
->ifindex
? PNPIPE_ENCAP_IP
: PNPIPE_ENCAP_NONE
;
821 len
= min_t(unsigned int, sizeof(int), len
);
822 if (put_user(len
, optlen
))
824 if (put_user(val
, (int __user
*) optval
))
829 static int pipe_skb_send(struct sock
*sk
, struct sk_buff
*skb
)
831 struct pep_sock
*pn
= pep_sk(sk
);
832 struct pnpipehdr
*ph
;
834 if (pn_flow_safe(pn
->tx_fc
) &&
835 !atomic_add_unless(&pn
->tx_credits
, -1, 0)) {
840 skb_push(skb
, 3 + pn
->aligned
);
841 skb_reset_transport_header(skb
);
845 ph
->message_id
= PNS_PIPE_ALIGNED_DATA
;
846 ph
->data
[0] = 0; /* padding */
848 ph
->message_id
= PNS_PIPE_DATA
;
849 ph
->pipe_handle
= pn
->pipe_handle
;
851 return pn_skb_send(sk
, skb
, &pipe_srv
);
854 static int pep_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
855 struct msghdr
*msg
, size_t len
)
857 struct pep_sock
*pn
= pep_sk(sk
);
860 int flags
= msg
->msg_flags
;
863 if ((msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|
865 !(msg
->msg_flags
& MSG_EOR
))
868 skb
= sock_alloc_send_skb(sk
, MAX_PNPIPE_HEADER
+ len
,
869 flags
& MSG_DONTWAIT
, &err
);
873 skb_reserve(skb
, MAX_PHONET_HEADER
+ 3);
874 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
879 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
880 if ((1 << sk
->sk_state
) & (TCPF_LISTEN
|TCPF_CLOSE
)) {
884 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
885 /* Wait until the pipe gets to enabled state */
887 err
= sk_stream_wait_connect(sk
, &timeo
);
891 if (sk
->sk_state
== TCP_CLOSE_WAIT
) {
896 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
898 /* Wait until flow control allows TX */
899 done
= atomic_read(&pn
->tx_credits
);
907 if (signal_pending(current
)) {
908 err
= sock_intr_errno(timeo
);
912 prepare_to_wait(&sk
->sk_socket
->wait
, &wait
,
914 done
= sk_wait_event(sk
, &timeo
, atomic_read(&pn
->tx_credits
));
915 finish_wait(&sk
->sk_socket
->wait
, &wait
);
917 if (sk
->sk_state
!= TCP_ESTABLISHED
)
921 err
= pipe_skb_send(sk
, skb
);
923 err
= len
; /* success! */
932 int pep_writeable(struct sock
*sk
)
934 struct pep_sock
*pn
= pep_sk(sk
);
936 return atomic_read(&pn
->tx_credits
);
939 int pep_write(struct sock
*sk
, struct sk_buff
*skb
)
941 struct sk_buff
*rskb
, *fs
;
944 if (pep_sk(sk
)->aligned
)
945 return pipe_skb_send(sk
, skb
);
947 rskb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
952 skb_shinfo(rskb
)->frag_list
= skb
;
953 rskb
->len
+= skb
->len
;
954 rskb
->data_len
+= rskb
->len
;
955 rskb
->truesize
+= rskb
->len
;
957 /* Avoid nested fragments */
958 skb_walk_frags(skb
, fs
)
960 skb
->next
= skb_shinfo(skb
)->frag_list
;
961 skb_frag_list_init(skb
);
963 skb
->data_len
-= flen
;
964 skb
->truesize
-= flen
;
966 skb_reserve(rskb
, MAX_PHONET_HEADER
+ 3);
967 return pipe_skb_send(sk
, rskb
);
970 struct sk_buff
*pep_read(struct sock
*sk
)
972 struct sk_buff
*skb
= skb_dequeue(&sk
->sk_receive_queue
);
974 if (sk
->sk_state
== TCP_ESTABLISHED
)
975 pipe_grant_credits(sk
);
979 static int pep_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
980 struct msghdr
*msg
, size_t len
, int noblock
,
981 int flags
, int *addr_len
)
986 if (flags
& ~(MSG_OOB
|MSG_PEEK
|MSG_TRUNC
|MSG_DONTWAIT
|MSG_WAITALL
|
987 MSG_NOSIGNAL
|MSG_CMSG_COMPAT
))
990 if (unlikely(1 << sk
->sk_state
& (TCPF_LISTEN
| TCPF_CLOSE
)))
993 if ((flags
& MSG_OOB
) || sock_flag(sk
, SOCK_URGINLINE
)) {
994 /* Dequeue and acknowledge control request */
995 struct pep_sock
*pn
= pep_sk(sk
);
997 if (flags
& MSG_PEEK
)
999 skb
= skb_dequeue(&pn
->ctrlreq_queue
);
1001 pep_ctrlreq_error(sk
, skb
, PN_PIPE_NO_ERROR
,
1003 msg
->msg_flags
|= MSG_OOB
;
1006 if (flags
& MSG_OOB
)
1010 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1013 if (err
== -ENOTCONN
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
1019 if (sk
->sk_state
== TCP_ESTABLISHED
)
1020 pipe_grant_credits(sk
);
1023 msg
->msg_flags
|= MSG_EOR
;
1025 msg
->msg_flags
|= MSG_TRUNC
;
1029 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
1031 err
= (flags
& MSG_TRUNC
) ? skb
->len
: len
;
1033 skb_free_datagram(sk
, skb
);
1037 static void pep_sock_unhash(struct sock
*sk
)
1039 struct pep_sock
*pn
= pep_sk(sk
);
1040 struct sock
*skparent
= NULL
;
1043 if ((1 << sk
->sk_state
) & ~(TCPF_CLOSE
|TCPF_LISTEN
)) {
1044 skparent
= pn
->listener
;
1045 sk_del_node_init(sk
);
1049 pn
= pep_sk(skparent
);
1052 /* Unhash a listening sock only when it is closed
1053 * and all of its active connected pipes are closed. */
1054 if (hlist_empty(&pn
->hlist
))
1055 pn_sock_unhash(&pn
->pn_sk
.sk
);
1062 static struct proto pep_proto
= {
1063 .close
= pep_sock_close
,
1064 .accept
= pep_sock_accept
,
1067 .setsockopt
= pep_setsockopt
,
1068 .getsockopt
= pep_getsockopt
,
1069 .sendmsg
= pep_sendmsg
,
1070 .recvmsg
= pep_recvmsg
,
1071 .backlog_rcv
= pep_do_rcv
,
1072 .hash
= pn_sock_hash
,
1073 .unhash
= pep_sock_unhash
,
1074 .get_port
= pn_sock_get_port
,
1075 .obj_size
= sizeof(struct pep_sock
),
1076 .owner
= THIS_MODULE
,
1080 static struct phonet_protocol pep_pn_proto
= {
1081 .ops
= &phonet_stream_ops
,
1083 .sock_type
= SOCK_SEQPACKET
,
1086 static int __init
pep_register(void)
1088 return phonet_proto_register(PN_PROTO_PIPE
, &pep_pn_proto
);
1091 static void __exit
pep_unregister(void)
1093 phonet_proto_unregister(PN_PROTO_PIPE
, &pep_pn_proto
);
1096 module_init(pep_register
);
1097 module_exit(pep_unregister
);
1098 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1099 MODULE_DESCRIPTION("Phonet pipe protocol");
1100 MODULE_LICENSE("GPL");
1101 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET
, PN_PROTO_PIPE
);