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/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 <net/phonet/phonet.h>
34 #include <net/phonet/pep.h>
35 #include <net/phonet/gprs.h>
38 * TCP_CLOSE sock not in use yet
39 * TCP_CLOSE_WAIT disconnected pipe
40 * TCP_LISTEN listening pipe endpoint
41 * TCP_SYN_RECV connected pipe in disabled state
42 * TCP_ESTABLISHED connected pipe in enabled state
45 * - sk_state, ackq, hlist: sock lock needed
46 * - listener: read only
47 * - pipe_handle: read only
50 #define CREDITS_MAX 10
53 static const struct sockaddr_pn pipe_srv
= {
54 .spn_family
= AF_PHONET
,
55 .spn_resource
= 0xD9, /* pipe service */
58 #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
60 /* Get the next TLV sub-block. */
61 static unsigned char *pep_get_sb(struct sk_buff
*skb
, u8
*ptype
, u8
*plen
,
71 ph
= skb_header_pointer(skb
, 0, 2, &h
);
72 if (ph
== NULL
|| ph
->sb_len
< 2 || !pskb_may_pull(skb
, ph
->sb_len
))
78 if (buflen
> ph
->sb_len
)
80 data
= skb_header_pointer(skb
, 2, buflen
, buf
);
81 __skb_pull(skb
, 2 + ph
->sb_len
);
85 static int pep_reply(struct sock
*sk
, struct sk_buff
*oskb
,
86 u8 code
, const void *data
, int len
, gfp_t priority
)
88 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
92 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ len
, priority
);
95 skb_set_owner_w(skb
, sk
);
97 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
99 skb_copy_to_linear_data(skb
, data
, len
);
100 __skb_push(skb
, sizeof(*ph
));
101 skb_reset_transport_header(skb
);
103 ph
->utid
= oph
->utid
;
104 ph
->message_id
= oph
->message_id
+ 1; /* REQ -> RESP */
105 ph
->pipe_handle
= oph
->pipe_handle
;
106 ph
->error_code
= code
;
108 return pn_skb_send(sk
, skb
, &pipe_srv
);
112 static int pep_accept_conn(struct sock
*sk
, struct sk_buff
*skb
)
114 static const u8 data
[20] = {
115 PAD
, PAD
, PAD
, 2 /* sub-blocks */,
116 PN_PIPE_SB_REQUIRED_FC_TX
, pep_sb_size(5), 3, PAD
,
117 PN_MULTI_CREDIT_FLOW_CONTROL
,
118 PN_ONE_CREDIT_FLOW_CONTROL
,
119 PN_LEGACY_FLOW_CONTROL
,
121 PN_PIPE_SB_PREFERRED_FC_RX
, pep_sb_size(5), 3, PAD
,
122 PN_MULTI_CREDIT_FLOW_CONTROL
,
123 PN_ONE_CREDIT_FLOW_CONTROL
,
124 PN_LEGACY_FLOW_CONTROL
,
129 return pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, data
, sizeof(data
),
133 static int pep_reject_conn(struct sock
*sk
, struct sk_buff
*skb
, u8 code
)
135 static const u8 data
[4] = { PAD
, PAD
, PAD
, 0 /* sub-blocks */ };
136 WARN_ON(code
== PN_PIPE_NO_ERROR
);
137 return pep_reply(sk
, skb
, code
, data
, sizeof(data
), GFP_ATOMIC
);
140 /* Control requests are not sent by the pipe service and have a specific
142 static int pep_ctrlreq_error(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
145 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
147 struct pnpipehdr
*ph
;
148 struct sockaddr_pn dst
;
150 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
153 skb_set_owner_w(skb
, sk
);
155 skb_reserve(skb
, MAX_PHONET_HEADER
);
156 ph
= (struct pnpipehdr
*)skb_put(skb
, sizeof(*ph
) + 4);
158 ph
->utid
= oph
->utid
;
159 ph
->message_id
= PNS_PEP_CTRL_RESP
;
160 ph
->pipe_handle
= oph
->pipe_handle
;
161 ph
->data
[0] = oph
->data
[1]; /* CTRL id */
162 ph
->data
[1] = oph
->data
[0]; /* PEP type */
163 ph
->data
[2] = code
; /* error code, at an usual offset */
167 pn_skb_get_src_sockaddr(oskb
, &dst
);
168 return pn_skb_send(sk
, skb
, &dst
);
171 static int pipe_snd_status(struct sock
*sk
, u8 type
, u8 status
, gfp_t priority
)
173 struct pep_sock
*pn
= pep_sk(sk
);
174 struct pnpipehdr
*ph
;
177 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
180 skb_set_owner_w(skb
, sk
);
182 skb_reserve(skb
, MAX_PNPIPE_HEADER
+ 4);
183 __skb_push(skb
, sizeof(*ph
) + 4);
184 skb_reset_transport_header(skb
);
187 ph
->message_id
= PNS_PEP_STATUS_IND
;
188 ph
->pipe_handle
= pn
->pipe_handle
;
189 ph
->pep_type
= PN_PEP_TYPE_COMMON
;
193 ph
->data
[4] = status
;
195 return pn_skb_send(sk
, skb
, &pipe_srv
);
198 /* Send our RX flow control information to the sender.
199 * Socket must be locked. */
200 static void pipe_grant_credits(struct sock
*sk
)
202 struct pep_sock
*pn
= pep_sk(sk
);
204 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
207 case PN_LEGACY_FLOW_CONTROL
: /* TODO */
209 case PN_ONE_CREDIT_FLOW_CONTROL
:
210 pipe_snd_status(sk
, PN_PEP_IND_FLOW_CONTROL
,
211 PEP_IND_READY
, GFP_ATOMIC
);
214 case PN_MULTI_CREDIT_FLOW_CONTROL
:
215 if ((pn
->rx_credits
+ CREDITS_THR
) > CREDITS_MAX
)
217 if (pipe_snd_status(sk
, PN_PEP_IND_ID_MCFC_GRANT_CREDITS
,
218 CREDITS_MAX
- pn
->rx_credits
,
220 pn
->rx_credits
= CREDITS_MAX
;
225 static int pipe_rcv_status(struct sock
*sk
, struct sk_buff
*skb
)
227 struct pep_sock
*pn
= pep_sk(sk
);
228 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
231 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
234 if (hdr
->data
[0] != PN_PEP_TYPE_COMMON
) {
235 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP type: %u\n",
236 (unsigned)hdr
->data
[0]);
240 switch (hdr
->data
[1]) {
241 case PN_PEP_IND_FLOW_CONTROL
:
243 case PN_LEGACY_FLOW_CONTROL
:
244 switch (hdr
->data
[4]) {
246 atomic_set(&pn
->tx_credits
, 0);
249 atomic_set(&pn
->tx_credits
, wake
= 1);
253 case PN_ONE_CREDIT_FLOW_CONTROL
:
254 if (hdr
->data
[4] == PEP_IND_READY
)
255 atomic_set(&pn
->tx_credits
, wake
= 1);
260 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS
:
261 if (pn
->tx_fc
!= PN_MULTI_CREDIT_FLOW_CONTROL
)
263 atomic_add(wake
= hdr
->data
[4], &pn
->tx_credits
);
267 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP indication: %u\n",
268 (unsigned)hdr
->data
[1]);
272 sk
->sk_write_space(sk
);
276 static int pipe_rcv_created(struct sock
*sk
, struct sk_buff
*skb
)
278 struct pep_sock
*pn
= pep_sk(sk
);
279 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
280 u8 n_sb
= hdr
->data
[0];
282 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
283 __skb_pull(skb
, sizeof(*hdr
));
285 u8 type
, buf
[2], len
= sizeof(buf
);
286 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
291 case PN_PIPE_SB_NEGOTIATED_FC
:
292 if (len
< 2 || (data
[0] | data
[1]) > 3)
294 pn
->tx_fc
= data
[0] & 3;
295 pn
->rx_fc
= data
[1] & 3;
303 /* Queue an skb to a connected sock.
304 * Socket lock must be held. */
305 static int pipe_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
307 struct pep_sock
*pn
= pep_sk(sk
);
308 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
309 struct sk_buff_head
*queue
;
312 BUG_ON(sk
->sk_state
== TCP_CLOSE_WAIT
);
314 switch (hdr
->message_id
) {
315 case PNS_PEP_CONNECT_REQ
:
316 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
319 case PNS_PEP_DISCONNECT_REQ
:
320 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
321 sk
->sk_state
= TCP_CLOSE_WAIT
;
322 if (!sock_flag(sk
, SOCK_DEAD
))
323 sk
->sk_state_change(sk
);
326 case PNS_PEP_ENABLE_REQ
:
327 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
328 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
331 case PNS_PEP_RESET_REQ
:
332 switch (hdr
->state_after_reset
) {
333 case PN_PIPE_DISABLE
:
339 default: /* not allowed to send an error here!? */
344 case PNS_PEP_DISABLE_REQ
:
345 atomic_set(&pn
->tx_credits
, 0);
346 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
349 case PNS_PEP_CTRL_REQ
:
350 if (skb_queue_len(&pn
->ctrlreq_queue
) >= PNPIPE_CTRLREQ_MAX
) {
351 atomic_inc(&sk
->sk_drops
);
355 queue
= &pn
->ctrlreq_queue
;
358 case PNS_PIPE_ALIGNED_DATA
:
362 __skb_pull(skb
, 3); /* Pipe data header */
363 if (!pn_flow_safe(pn
->rx_fc
)) {
364 err
= sock_queue_rcv_skb(sk
, skb
);
370 if (pn
->rx_credits
== 0) {
371 atomic_inc(&sk
->sk_drops
);
376 queue
= &sk
->sk_receive_queue
;
379 case PNS_PEP_STATUS_IND
:
380 pipe_rcv_status(sk
, skb
);
383 case PNS_PIPE_REDIRECTED_IND
:
384 err
= pipe_rcv_created(sk
, skb
);
387 case PNS_PIPE_CREATED_IND
:
388 err
= pipe_rcv_created(sk
, skb
);
392 case PNS_PIPE_RESET_IND
:
393 if (!pn
->init_enable
)
396 case PNS_PIPE_ENABLED_IND
:
397 if (!pn_flow_safe(pn
->tx_fc
)) {
398 atomic_set(&pn
->tx_credits
, 1);
399 sk
->sk_write_space(sk
);
401 if (sk
->sk_state
== TCP_ESTABLISHED
)
402 break; /* Nothing to do */
403 sk
->sk_state
= TCP_ESTABLISHED
;
404 pipe_grant_credits(sk
);
407 case PNS_PIPE_DISABLED_IND
:
408 sk
->sk_state
= TCP_SYN_RECV
;
413 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP message: %u\n",
423 skb_set_owner_r(skb
, sk
);
425 skb_queue_tail(queue
, skb
);
426 if (!sock_flag(sk
, SOCK_DEAD
))
427 sk
->sk_data_ready(sk
, err
);
431 /* Destroy connected sock. */
432 static void pipe_destruct(struct sock
*sk
)
434 struct pep_sock
*pn
= pep_sk(sk
);
436 skb_queue_purge(&sk
->sk_receive_queue
);
437 skb_queue_purge(&pn
->ctrlreq_queue
);
440 static int pep_connreq_rcv(struct sock
*sk
, struct sk_buff
*skb
)
443 struct pep_sock
*newpn
, *pn
= pep_sk(sk
);
444 struct pnpipehdr
*hdr
;
445 struct sockaddr_pn dst
;
447 u8 pipe_handle
, enabled
, n_sb
;
450 if (!pskb_pull(skb
, sizeof(*hdr
) + 4))
454 pipe_handle
= hdr
->pipe_handle
;
455 switch (hdr
->state_after_connect
) {
456 case PN_PIPE_DISABLE
:
463 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_INVALID_PARAM
);
466 peer_type
= hdr
->other_pep_type
<< 8;
468 if (unlikely(sk
->sk_state
!= TCP_LISTEN
) || sk_acceptq_is_full(sk
)) {
469 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
473 /* Parse sub-blocks (options) */
476 u8 type
, buf
[1], len
= sizeof(buf
);
477 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
482 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE
:
485 peer_type
= (peer_type
& 0xff00) | data
[0];
487 case PN_PIPE_SB_ALIGNED_DATA
:
488 aligned
= data
[0] != 0;
494 skb
= skb_clone(skb
, GFP_ATOMIC
);
498 /* Create a new to-be-accepted sock */
499 newsk
= sk_alloc(sock_net(sk
), PF_PHONET
, GFP_ATOMIC
, sk
->sk_prot
);
504 sock_init_data(NULL
, newsk
);
505 newsk
->sk_state
= TCP_SYN_RECV
;
506 newsk
->sk_backlog_rcv
= pipe_do_rcv
;
507 newsk
->sk_protocol
= sk
->sk_protocol
;
508 newsk
->sk_destruct
= pipe_destruct
;
510 newpn
= pep_sk(newsk
);
511 pn_skb_get_dst_sockaddr(skb
, &dst
);
512 newpn
->pn_sk
.sobject
= pn_sockaddr_get_object(&dst
);
513 newpn
->pn_sk
.resource
= pn
->pn_sk
.resource
;
514 skb_queue_head_init(&newpn
->ctrlreq_queue
);
515 newpn
->pipe_handle
= pipe_handle
;
516 atomic_set(&newpn
->tx_credits
, 0);
517 newpn
->peer_type
= peer_type
;
518 newpn
->rx_credits
= 0;
519 newpn
->rx_fc
= newpn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
520 newpn
->init_enable
= enabled
;
521 newpn
->aligned
= aligned
;
523 BUG_ON(!skb_queue_empty(&newsk
->sk_receive_queue
));
524 skb_queue_head(&newsk
->sk_receive_queue
, skb
);
525 if (!sock_flag(sk
, SOCK_DEAD
))
526 sk
->sk_data_ready(sk
, 0);
528 sk_acceptq_added(sk
);
529 sk_add_node(newsk
, &pn
->ackq
);
533 /* Listening sock must be locked */
534 static struct sock
*pep_find_pipe(const struct hlist_head
*hlist
,
535 const struct sockaddr_pn
*dst
,
538 struct hlist_node
*node
;
540 u16 dobj
= pn_sockaddr_get_object(dst
);
542 sk_for_each(sknode
, node
, hlist
) {
543 struct pep_sock
*pnnode
= pep_sk(sknode
);
545 /* Ports match, but addresses might not: */
546 if (pnnode
->pn_sk
.sobject
!= dobj
)
548 if (pnnode
->pipe_handle
!= pipe_handle
)
550 if (sknode
->sk_state
== TCP_CLOSE_WAIT
)
560 * Deliver an skb to a listening sock.
561 * Socket lock must be held.
562 * We then queue the skb to the right connected sock (if any).
564 static int pep_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
566 struct pep_sock
*pn
= pep_sk(sk
);
568 struct pnpipehdr
*hdr
;
569 struct sockaddr_pn dst
;
570 int err
= NET_RX_SUCCESS
;
573 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
577 pipe_handle
= hdr
->pipe_handle
;
578 if (pipe_handle
== PN_PIPE_INVALID_HANDLE
)
581 pn_skb_get_dst_sockaddr(skb
, &dst
);
583 /* Look for an existing pipe handle */
584 sknode
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
586 return sk_receive_skb(sknode
, skb
, 1);
588 /* Look for a pipe handle pending accept */
589 sknode
= pep_find_pipe(&pn
->ackq
, &dst
, pipe_handle
);
593 printk(KERN_WARNING
"Phonet unconnected PEP ignored");
598 switch (hdr
->message_id
) {
599 case PNS_PEP_CONNECT_REQ
:
600 err
= pep_connreq_rcv(sk
, skb
);
603 case PNS_PEP_DISCONNECT_REQ
:
604 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
607 case PNS_PEP_CTRL_REQ
:
608 pep_ctrlreq_error(sk
, skb
, PN_PIPE_INVALID_HANDLE
, GFP_ATOMIC
);
611 case PNS_PEP_RESET_REQ
:
612 case PNS_PEP_ENABLE_REQ
:
613 case PNS_PEP_DISABLE_REQ
:
614 /* invalid handle is not even allowed here! */
623 /* associated socket ceases to exist */
624 static void pep_sock_close(struct sock
*sk
, long timeout
)
626 struct pep_sock
*pn
= pep_sk(sk
);
629 sock_hold(sk
); /* keep a reference after sk_common_release() */
630 sk_common_release(sk
);
633 if (sk
->sk_state
== TCP_LISTEN
) {
634 /* Destroy the listen queue */
636 struct hlist_node
*p
, *n
;
638 sk_for_each_safe(sknode
, p
, n
, &pn
->ackq
)
639 sk_del_node_init(sknode
);
640 sk
->sk_state
= TCP_CLOSE
;
642 ifindex
= pn
->ifindex
;
651 static int pep_wait_connreq(struct sock
*sk
, int noblock
)
653 struct task_struct
*tsk
= current
;
654 struct pep_sock
*pn
= pep_sk(sk
);
655 long timeo
= sock_rcvtimeo(sk
, noblock
);
660 if (sk
->sk_state
!= TCP_LISTEN
)
662 if (!hlist_empty(&pn
->ackq
))
666 if (signal_pending(tsk
))
667 return sock_intr_errno(timeo
);
669 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
672 timeo
= schedule_timeout(timeo
);
674 finish_wait(sk_sleep(sk
), &wait
);
680 static struct sock
*pep_sock_accept(struct sock
*sk
, int flags
, int *errp
)
682 struct pep_sock
*pn
= pep_sk(sk
);
683 struct sock
*newsk
= NULL
;
684 struct sk_buff
*oskb
;
688 err
= pep_wait_connreq(sk
, flags
& O_NONBLOCK
);
692 newsk
= __sk_head(&pn
->ackq
);
694 oskb
= skb_dequeue(&newsk
->sk_receive_queue
);
695 err
= pep_accept_conn(newsk
, oskb
);
697 skb_queue_head(&newsk
->sk_receive_queue
, oskb
);
703 pep_sk(newsk
)->listener
= sk
;
706 sk_del_node_init(newsk
);
707 sk_acceptq_removed(sk
);
708 sk_add_node(newsk
, &pn
->hlist
);
717 static int pep_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
719 struct pep_sock
*pn
= pep_sk(sk
);
724 if (sk
->sk_state
== TCP_LISTEN
)
728 if (sock_flag(sk
, SOCK_URGINLINE
) &&
729 !skb_queue_empty(&pn
->ctrlreq_queue
))
730 answ
= skb_peek(&pn
->ctrlreq_queue
)->len
;
731 else if (!skb_queue_empty(&sk
->sk_receive_queue
))
732 answ
= skb_peek(&sk
->sk_receive_queue
)->len
;
736 return put_user(answ
, (int __user
*)arg
);
742 static int pep_init(struct sock
*sk
)
744 struct pep_sock
*pn
= pep_sk(sk
);
746 INIT_HLIST_HEAD(&pn
->ackq
);
747 INIT_HLIST_HEAD(&pn
->hlist
);
748 skb_queue_head_init(&pn
->ctrlreq_queue
);
749 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
753 static int pep_setsockopt(struct sock
*sk
, int level
, int optname
,
754 char __user
*optval
, unsigned int optlen
)
756 struct pep_sock
*pn
= pep_sk(sk
);
757 int val
= 0, err
= 0;
759 if (level
!= SOL_PNPIPE
)
761 if (optlen
>= sizeof(int)) {
762 if (get_user(val
, (int __user
*) optval
))
769 if (val
&& val
!= PNPIPE_ENCAP_IP
) {
773 if (!pn
->ifindex
== !val
)
774 break; /* Nothing to do! */
775 if (!capable(CAP_NET_ADMIN
)) {
781 err
= gprs_attach(sk
);
802 static int pep_getsockopt(struct sock
*sk
, int level
, int optname
,
803 char __user
*optval
, int __user
*optlen
)
805 struct pep_sock
*pn
= pep_sk(sk
);
808 if (level
!= SOL_PNPIPE
)
810 if (get_user(len
, optlen
))
815 val
= pn
->ifindex
? PNPIPE_ENCAP_IP
: PNPIPE_ENCAP_NONE
;
824 len
= min_t(unsigned int, sizeof(int), len
);
825 if (put_user(len
, optlen
))
827 if (put_user(val
, (int __user
*) optval
))
832 static int pipe_skb_send(struct sock
*sk
, struct sk_buff
*skb
)
834 struct pep_sock
*pn
= pep_sk(sk
);
835 struct pnpipehdr
*ph
;
837 if (pn_flow_safe(pn
->tx_fc
) &&
838 !atomic_add_unless(&pn
->tx_credits
, -1, 0)) {
843 skb_push(skb
, 3 + pn
->aligned
);
844 skb_reset_transport_header(skb
);
848 ph
->message_id
= PNS_PIPE_ALIGNED_DATA
;
849 ph
->data
[0] = 0; /* padding */
851 ph
->message_id
= PNS_PIPE_DATA
;
852 ph
->pipe_handle
= pn
->pipe_handle
;
854 return pn_skb_send(sk
, skb
, &pipe_srv
);
857 static int pep_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
858 struct msghdr
*msg
, size_t len
)
860 struct pep_sock
*pn
= pep_sk(sk
);
863 int flags
= msg
->msg_flags
;
866 if ((msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|
868 !(msg
->msg_flags
& MSG_EOR
))
871 skb
= sock_alloc_send_skb(sk
, MAX_PNPIPE_HEADER
+ len
,
872 flags
& MSG_DONTWAIT
, &err
);
876 skb_reserve(skb
, MAX_PHONET_HEADER
+ 3);
877 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
882 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
883 if ((1 << sk
->sk_state
) & (TCPF_LISTEN
|TCPF_CLOSE
)) {
887 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
888 /* Wait until the pipe gets to enabled state */
890 err
= sk_stream_wait_connect(sk
, &timeo
);
894 if (sk
->sk_state
== TCP_CLOSE_WAIT
) {
899 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
901 /* Wait until flow control allows TX */
902 done
= atomic_read(&pn
->tx_credits
);
910 if (signal_pending(current
)) {
911 err
= sock_intr_errno(timeo
);
915 prepare_to_wait(sk_sleep(sk
), &wait
,
917 done
= sk_wait_event(sk
, &timeo
, atomic_read(&pn
->tx_credits
));
918 finish_wait(sk_sleep(sk
), &wait
);
920 if (sk
->sk_state
!= TCP_ESTABLISHED
)
924 err
= pipe_skb_send(sk
, skb
);
926 err
= len
; /* success! */
935 int pep_writeable(struct sock
*sk
)
937 struct pep_sock
*pn
= pep_sk(sk
);
939 return atomic_read(&pn
->tx_credits
);
942 int pep_write(struct sock
*sk
, struct sk_buff
*skb
)
944 struct sk_buff
*rskb
, *fs
;
947 if (pep_sk(sk
)->aligned
)
948 return pipe_skb_send(sk
, skb
);
950 rskb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
955 skb_shinfo(rskb
)->frag_list
= skb
;
956 rskb
->len
+= skb
->len
;
957 rskb
->data_len
+= rskb
->len
;
958 rskb
->truesize
+= rskb
->len
;
960 /* Avoid nested fragments */
961 skb_walk_frags(skb
, fs
)
963 skb
->next
= skb_shinfo(skb
)->frag_list
;
964 skb_frag_list_init(skb
);
966 skb
->data_len
-= flen
;
967 skb
->truesize
-= flen
;
969 skb_reserve(rskb
, MAX_PHONET_HEADER
+ 3);
970 return pipe_skb_send(sk
, rskb
);
973 struct sk_buff
*pep_read(struct sock
*sk
)
975 struct sk_buff
*skb
= skb_dequeue(&sk
->sk_receive_queue
);
977 if (sk
->sk_state
== TCP_ESTABLISHED
)
978 pipe_grant_credits(sk
);
982 static int pep_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
983 struct msghdr
*msg
, size_t len
, int noblock
,
984 int flags
, int *addr_len
)
989 if (flags
& ~(MSG_OOB
|MSG_PEEK
|MSG_TRUNC
|MSG_DONTWAIT
|MSG_WAITALL
|
990 MSG_NOSIGNAL
|MSG_CMSG_COMPAT
))
993 if (unlikely(1 << sk
->sk_state
& (TCPF_LISTEN
| TCPF_CLOSE
)))
996 if ((flags
& MSG_OOB
) || sock_flag(sk
, SOCK_URGINLINE
)) {
997 /* Dequeue and acknowledge control request */
998 struct pep_sock
*pn
= pep_sk(sk
);
1000 if (flags
& MSG_PEEK
)
1002 skb
= skb_dequeue(&pn
->ctrlreq_queue
);
1004 pep_ctrlreq_error(sk
, skb
, PN_PIPE_NO_ERROR
,
1006 msg
->msg_flags
|= MSG_OOB
;
1009 if (flags
& MSG_OOB
)
1013 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1016 if (err
== -ENOTCONN
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
1022 if (sk
->sk_state
== TCP_ESTABLISHED
)
1023 pipe_grant_credits(sk
);
1026 msg
->msg_flags
|= MSG_EOR
;
1028 msg
->msg_flags
|= MSG_TRUNC
;
1032 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
1034 err
= (flags
& MSG_TRUNC
) ? skb
->len
: len
;
1036 skb_free_datagram(sk
, skb
);
1040 static void pep_sock_unhash(struct sock
*sk
)
1042 struct pep_sock
*pn
= pep_sk(sk
);
1043 struct sock
*skparent
= NULL
;
1046 if ((1 << sk
->sk_state
) & ~(TCPF_CLOSE
|TCPF_LISTEN
)) {
1047 skparent
= pn
->listener
;
1050 pn
= pep_sk(skparent
);
1051 lock_sock(skparent
);
1052 sk_del_node_init(sk
);
1055 /* Unhash a listening sock only when it is closed
1056 * and all of its active connected pipes are closed. */
1057 if (hlist_empty(&pn
->hlist
))
1058 pn_sock_unhash(&pn
->pn_sk
.sk
);
1065 static struct proto pep_proto
= {
1066 .close
= pep_sock_close
,
1067 .accept
= pep_sock_accept
,
1070 .setsockopt
= pep_setsockopt
,
1071 .getsockopt
= pep_getsockopt
,
1072 .sendmsg
= pep_sendmsg
,
1073 .recvmsg
= pep_recvmsg
,
1074 .backlog_rcv
= pep_do_rcv
,
1075 .hash
= pn_sock_hash
,
1076 .unhash
= pep_sock_unhash
,
1077 .get_port
= pn_sock_get_port
,
1078 .obj_size
= sizeof(struct pep_sock
),
1079 .owner
= THIS_MODULE
,
1083 static struct phonet_protocol pep_pn_proto
= {
1084 .ops
= &phonet_stream_ops
,
1086 .sock_type
= SOCK_SEQPACKET
,
1089 static int __init
pep_register(void)
1091 return phonet_proto_register(PN_PROTO_PIPE
, &pep_pn_proto
);
1094 static void __exit
pep_unregister(void)
1096 phonet_proto_unregister(PN_PROTO_PIPE
, &pep_pn_proto
);
1099 module_init(pep_register
);
1100 module_exit(pep_unregister
);
1101 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1102 MODULE_DESCRIPTION("Phonet pipe protocol");
1103 MODULE_LICENSE("GPL");
1104 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET
, PN_PROTO_PIPE
);