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);
229 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
232 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
233 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
234 (unsigned)hdr->data[0]);
238 switch (hdr->data[1]) {
239 case PN_PEP_IND_FLOW_CONTROL:
241 case PN_LEGACY_FLOW_CONTROL:
242 switch (hdr->data[4]) {
251 case PN_ONE_CREDIT_FLOW_CONTROL:
252 if (hdr->data[4] == PEP_IND_READY)
258 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
259 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
261 if (pn->tx_credits + hdr->data[4] > 0xff)
262 pn->tx_credits = 0xff;
264 pn->tx_credits += hdr->data[4];
268 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
269 (unsigned)hdr->data[1]);
273 sk->sk_write_space(sk);
277 static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
279 struct pep_sock *pn = pep_sk(sk);
280 struct pnpipehdr *hdr = pnp_hdr(skb);
281 u8 n_sb = hdr->data[0];
283 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
284 __skb_pull(skb, sizeof(*hdr));
286 u8 type, buf[2], len = sizeof(buf);
287 u8 *data = pep_get_sb(skb, &type, &len, buf);
292 case PN_PIPE_SB_NEGOTIATED_FC:
293 if (len < 2 || (data[0] | data[1]) > 3)
295 pn->tx_fc = data[0] & 3;
296 pn->rx_fc = data[1] & 3;
304 /* Queue an skb to a connected sock.
305 * Socket lock must be held. */
306 static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
308 struct pep_sock *pn = pep_sk(sk);
309 struct pnpipehdr *hdr = pnp_hdr(skb);
310 struct sk_buff_head *queue;
313 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
315 switch (hdr->message_id) {
316 case PNS_PEP_CONNECT_REQ:
317 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
320 case PNS_PEP_DISCONNECT_REQ:
321 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
322 sk->sk_state = TCP_CLOSE_WAIT;
323 if (!sock_flag(sk, SOCK_DEAD))
324 sk->sk_state_change(sk);
327 case PNS_PEP_ENABLE_REQ:
328 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
329 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
332 case PNS_PEP_RESET_REQ:
333 switch (hdr->state_after_reset) {
334 case PN_PIPE_DISABLE:
340 default: /* not allowed to send an error here!? */
345 case PNS_PEP_DISABLE_REQ:
347 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
350 case PNS_PEP_CTRL_REQ:
351 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX)
354 queue = &pn->ctrlreq_queue;
358 __skb_pull(skb, 3); /* Pipe data header */
359 if (!pn_flow_safe(pn->rx_fc)) {
360 err = sock_queue_rcv_skb(sk, skb);
366 if (pn->rx_credits == 0) {
371 queue = &sk->sk_receive_queue;
374 case PNS_PEP_STATUS_IND:
375 pipe_rcv_status(sk, skb);
378 case PNS_PIPE_REDIRECTED_IND:
379 err = pipe_rcv_created(sk, skb);
382 case PNS_PIPE_CREATED_IND:
383 err = pipe_rcv_created(sk, skb);
387 case PNS_PIPE_RESET_IND:
388 if (!pn->init_enable)
391 case PNS_PIPE_ENABLED_IND:
392 if (!pn_flow_safe(pn->tx_fc)) {
394 sk->sk_write_space(sk);
396 if (sk->sk_state == TCP_ESTABLISHED)
397 break; /* Nothing to do */
398 sk->sk_state = TCP_ESTABLISHED;
399 pipe_grant_credits(sk);
402 case PNS_PIPE_DISABLED_IND:
403 sk->sk_state = TCP_SYN_RECV;
408 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
418 skb_set_owner_r(skb, sk);
420 skb_queue_tail(queue, skb);
421 if (!sock_flag(sk, SOCK_DEAD))
422 sk->sk_data_ready(sk, err);
426 /* Destroy connected sock. */
427 static void pipe_destruct(struct sock *sk)
429 struct pep_sock *pn = pep_sk(sk);
431 skb_queue_purge(&sk->sk_receive_queue);
432 skb_queue_purge(&pn->ctrlreq_queue);
435 static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
438 struct pep_sock *newpn, *pn = pep_sk(sk);
439 struct pnpipehdr *hdr;
440 struct sockaddr_pn dst;
442 u8 pipe_handle, enabled, n_sb;
444 if (!pskb_pull(skb, sizeof(*hdr) + 4))
448 pipe_handle = hdr->pipe_handle;
449 switch (hdr->state_after_connect) {
450 case PN_PIPE_DISABLE:
457 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
460 peer_type = hdr->other_pep_type << 8;
462 if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) {
463 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
467 /* Parse sub-blocks (options) */
470 u8 type, buf[1], len = sizeof(buf);
471 const u8 *data = pep_get_sb(skb, &type, &len, buf);
476 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
479 peer_type = (peer_type & 0xff00) | data[0];
485 skb = skb_clone(skb, GFP_ATOMIC);
489 /* Create a new to-be-accepted sock */
490 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
495 sock_init_data(NULL, newsk);
496 newsk->sk_state = TCP_SYN_RECV;
497 newsk->sk_backlog_rcv = pipe_do_rcv;
498 newsk->sk_protocol = sk->sk_protocol;
499 newsk->sk_destruct = pipe_destruct;
501 newpn = pep_sk(newsk);
502 pn_skb_get_dst_sockaddr(skb, &dst);
503 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
504 newpn->pn_sk.resource = pn->pn_sk.resource;
505 skb_queue_head_init(&newpn->ctrlreq_queue);
506 newpn->pipe_handle = pipe_handle;
507 newpn->peer_type = peer_type;
508 newpn->rx_credits = newpn->tx_credits = 0;
509 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
510 newpn->init_enable = enabled;
512 BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
513 skb_queue_head(&newsk->sk_receive_queue, skb);
514 if (!sock_flag(sk, SOCK_DEAD))
515 sk->sk_data_ready(sk, 0);
517 sk_acceptq_added(sk);
518 sk_add_node(newsk, &pn->ackq);
522 /* Listening sock must be locked */
523 static struct sock *pep_find_pipe(const struct hlist_head *hlist,
524 const struct sockaddr_pn *dst,
527 struct hlist_node *node;
529 u16 dobj = pn_sockaddr_get_object(dst);
531 sk_for_each(sknode, node, hlist) {
532 struct pep_sock *pnnode = pep_sk(sknode);
534 /* Ports match, but addresses might not: */
535 if (pnnode->pn_sk.sobject != dobj)
537 if (pnnode->pipe_handle != pipe_handle)
539 if (sknode->sk_state == TCP_CLOSE_WAIT)
549 * Deliver an skb to a listening sock.
550 * Socket lock must be held.
551 * We then queue the skb to the right connected sock (if any).
553 static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
555 struct pep_sock *pn = pep_sk(sk);
557 struct pnpipehdr *hdr = pnp_hdr(skb);
558 struct sockaddr_pn dst;
559 int err = NET_RX_SUCCESS;
562 if (!pskb_may_pull(skb, sizeof(*hdr)))
566 pipe_handle = hdr->pipe_handle;
567 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
570 pn_skb_get_dst_sockaddr(skb, &dst);
572 /* Look for an existing pipe handle */
573 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
575 return sk_receive_skb(sknode, skb, 1);
577 /* Look for a pipe handle pending accept */
578 sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
582 printk(KERN_WARNING"Phonet unconnected PEP ignored");
587 switch (hdr->message_id) {
588 case PNS_PEP_CONNECT_REQ:
589 err = pep_connreq_rcv(sk, skb);
592 case PNS_PEP_DISCONNECT_REQ:
593 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
596 case PNS_PEP_CTRL_REQ:
597 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
600 case PNS_PEP_RESET_REQ:
601 case PNS_PEP_ENABLE_REQ:
602 case PNS_PEP_DISABLE_REQ:
603 /* invalid handle is not even allowed here! */
612 /* associated socket ceases to exist */
613 static void pep_sock_close(struct sock *sk, long timeout)
615 struct pep_sock *pn = pep_sk(sk);
618 sk_common_release(sk);
621 if (sk->sk_state == TCP_LISTEN) {
622 /* Destroy the listen queue */
624 struct hlist_node *p, *n;
626 sk_for_each_safe(sknode, p, n, &pn->ackq)
627 sk_del_node_init(sknode);
628 sk->sk_state = TCP_CLOSE;
630 ifindex = pn->ifindex;
638 static int pep_wait_connreq(struct sock *sk, int noblock)
640 struct task_struct *tsk = current;
641 struct pep_sock *pn = pep_sk(sk);
642 long timeo = sock_rcvtimeo(sk, noblock);
647 if (sk->sk_state != TCP_LISTEN)
649 if (!hlist_empty(&pn->ackq))
653 if (signal_pending(tsk))
654 return sock_intr_errno(timeo);
656 prepare_to_wait_exclusive(&sk->sk_socket->wait, &wait,
659 timeo = schedule_timeout(timeo);
661 finish_wait(&sk->sk_socket->wait, &wait);
667 static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
669 struct pep_sock *pn = pep_sk(sk);
670 struct sock *newsk = NULL;
671 struct sk_buff *oskb;
675 err = pep_wait_connreq(sk, flags & O_NONBLOCK);
679 newsk = __sk_head(&pn->ackq);
681 oskb = skb_dequeue(&newsk->sk_receive_queue);
682 err = pep_accept_conn(newsk, oskb);
684 skb_queue_head(&newsk->sk_receive_queue, oskb);
690 pep_sk(newsk)->listener = sk;
693 sk_del_node_init(newsk);
694 sk_acceptq_removed(sk);
695 sk_add_node(newsk, &pn->hlist);
704 static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
706 struct pep_sock *pn = pep_sk(sk);
711 if (sk->sk_state == TCP_LISTEN)
715 if (sock_flag(sk, SOCK_URGINLINE)
716 && !skb_queue_empty(&pn->ctrlreq_queue))
717 answ = skb_peek(&pn->ctrlreq_queue)->len;
718 else if (!skb_queue_empty(&sk->sk_receive_queue))
719 answ = skb_peek(&sk->sk_receive_queue)->len;
723 return put_user(answ, (int __user *)arg);
729 static int pep_init(struct sock *sk)
731 struct pep_sock *pn = pep_sk(sk);
733 INIT_HLIST_HEAD(&pn->ackq);
734 INIT_HLIST_HEAD(&pn->hlist);
735 skb_queue_head_init(&pn->ctrlreq_queue);
736 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
740 static int pep_setsockopt(struct sock *sk, int level, int optname,
741 char __user *optval, int optlen)
743 struct pep_sock *pn = pep_sk(sk);
744 int val = 0, err = 0;
746 if (level != SOL_PNPIPE)
748 if (optlen >= sizeof(int)) {
749 if (get_user(val, (int __user *) optval))
756 if (val && val != PNPIPE_ENCAP_IP) {
760 if (!pn->ifindex == !val)
761 break; /* Nothing to do! */
762 if (!capable(CAP_NET_ADMIN)) {
768 err = gprs_attach(sk);
789 static int pep_getsockopt(struct sock *sk, int level, int optname,
790 char __user *optval, int __user *optlen)
792 struct pep_sock *pn = pep_sk(sk);
795 if (level != SOL_PNPIPE)
797 if (get_user(len, optlen))
802 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
811 len = min_t(unsigned int, sizeof(int), len);
812 if (put_user(len, optlen))
814 if (put_user(val, (int __user *) optval))
819 static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
821 struct pep_sock *pn = pep_sk(sk);
822 struct pnpipehdr *ph;
825 skb_reset_transport_header(skb);
828 ph->message_id = PNS_PIPE_DATA;
829 ph->pipe_handle = pn->pipe_handle;
830 if (pn_flow_safe(pn->tx_fc) && pn->tx_credits)
833 return pn_skb_send(sk, skb, &pipe_srv);
836 static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
837 struct msghdr *msg, size_t len)
839 struct pep_sock *pn = pep_sk(sk);
840 struct sk_buff *skb = NULL;
842 int flags = msg->msg_flags;
845 if (msg->msg_flags & MSG_OOB || !(msg->msg_flags & MSG_EOR))
849 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
850 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
854 if (sk->sk_state != TCP_ESTABLISHED) {
855 /* Wait until the pipe gets to enabled state */
857 err = sk_stream_wait_connect(sk, &timeo);
861 if (sk->sk_state == TCP_CLOSE_WAIT) {
866 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
868 /* Wait until flow control allows TX */
869 done = pn->tx_credits > 0;
877 if (signal_pending(current)) {
878 err = sock_intr_errno(timeo);
882 prepare_to_wait(&sk->sk_socket->wait, &wait,
884 done = sk_wait_event(sk, &timeo, pn->tx_credits > 0);
885 finish_wait(&sk->sk_socket->wait, &wait);
887 if (sk->sk_state != TCP_ESTABLISHED)
892 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
893 flags & MSG_DONTWAIT, &err);
896 skb_reserve(skb, MAX_PHONET_HEADER + 3);
898 if (sk->sk_state != TCP_ESTABLISHED || !pn->tx_credits)
899 goto disabled; /* sock_alloc_send_skb might sleep */
902 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
906 err = pipe_skb_send(sk, skb);
908 err = len; /* success! */
916 int pep_writeable(struct sock *sk)
918 struct pep_sock *pn = pep_sk(sk);
920 return (sk->sk_state == TCP_ESTABLISHED) ? pn->tx_credits : 0;
923 int pep_write(struct sock *sk, struct sk_buff *skb)
925 struct sk_buff *rskb, *fs;
928 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
933 skb_shinfo(rskb)->frag_list = skb;
934 rskb->len += skb->len;
935 rskb->data_len += rskb->len;
936 rskb->truesize += rskb->len;
938 /* Avoid nested fragments */
939 for (fs = skb_shinfo(skb)->frag_list; fs; fs = fs->next)
941 skb->next = skb_shinfo(skb)->frag_list;
942 skb_shinfo(skb)->frag_list = NULL;
944 skb->data_len -= flen;
945 skb->truesize -= flen;
947 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
948 return pipe_skb_send(sk, rskb);
951 struct sk_buff *pep_read(struct sock *sk)
953 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
955 if (sk->sk_state == TCP_ESTABLISHED)
956 pipe_grant_credits(sk);
960 static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
961 struct msghdr *msg, size_t len, int noblock,
962 int flags, int *addr_len)
967 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
970 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
971 /* Dequeue and acknowledge control request */
972 struct pep_sock *pn = pep_sk(sk);
974 skb = skb_dequeue(&pn->ctrlreq_queue);
976 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
978 msg->msg_flags |= MSG_OOB;
985 skb = skb_recv_datagram(sk, flags, noblock, &err);
988 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
994 if (sk->sk_state == TCP_ESTABLISHED)
995 pipe_grant_credits(sk);
998 msg->msg_flags |= MSG_EOR;
1000 msg->msg_flags |= MSG_TRUNC;
1004 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1006 err = (flags & MSG_TRUNC) ? skb->len : len;
1008 skb_free_datagram(sk, skb);
1012 static void pep_sock_unhash(struct sock *sk)
1014 struct pep_sock *pn = pep_sk(sk);
1015 struct sock *skparent = NULL;
1018 if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1019 skparent = pn->listener;
1020 sk_del_node_init(sk);
1024 pn = pep_sk(skparent);
1027 /* Unhash a listening sock only when it is closed
1028 * and all of its active connected pipes are closed. */
1029 if (hlist_empty(&pn->hlist))
1030 pn_sock_unhash(&pn->pn_sk.sk);
1037 static struct proto pep_proto = {
1038 .close = pep_sock_close,
1039 .accept = pep_sock_accept,
1042 .setsockopt = pep_setsockopt,
1043 .getsockopt = pep_getsockopt,
1044 .sendmsg = pep_sendmsg,
1045 .recvmsg = pep_recvmsg,
1046 .backlog_rcv = pep_do_rcv,
1047 .hash = pn_sock_hash,
1048 .unhash = pep_sock_unhash,
1049 .get_port = pn_sock_get_port,
1050 .obj_size = sizeof(struct pep_sock),
1051 .owner = THIS_MODULE,
1055 static struct phonet_protocol pep_pn_proto = {
1056 .ops = &phonet_stream_ops,
1058 .sock_type = SOCK_SEQPACKET,
1061 static int __init pep_register(void)
1063 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1066 static void __exit pep_unregister(void)
1068 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1071 module_init(pep_register);
1072 module_exit(pep_unregister);
1073 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1074 MODULE_DESCRIPTION("Phonet pipe protocol");
1075 MODULE_LICENSE("GPL");
1076 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);