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, hlist: sock lock needed
46 * - listener: read only
47 * - pipe_handle: read only
50 #define CREDITS_MAX 10
53 #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
55 /* Get the next TLV sub-block. */
56 static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen,
66 ph = skb_header_pointer(skb, 0, 2, &h);
67 if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len))
73 if (buflen > ph->sb_len)
75 data = skb_header_pointer(skb, 2, buflen, buf);
76 __skb_pull(skb, 2 + ph->sb_len);
80 static struct sk_buff *pep_alloc_skb(struct sock *sk, const void *payload,
81 int len, gfp_t priority)
83 struct sk_buff *skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
86 skb_set_owner_w(skb, sk);
88 skb_reserve(skb, MAX_PNPIPE_HEADER);
90 skb_copy_to_linear_data(skb, payload, len);
91 __skb_push(skb, sizeof(struct pnpipehdr));
92 skb_reset_transport_header(skb);
96 static int pep_reply(struct sock *sk, struct sk_buff *oskb, u8 code,
97 const void *data, int len, gfp_t priority)
99 const struct pnpipehdr *oph = pnp_hdr(oskb);
100 struct pnpipehdr *ph;
102 struct sockaddr_pn peer;
104 skb = pep_alloc_skb(sk, data, len, priority);
109 ph->utid = oph->utid;
110 ph->message_id = oph->message_id + 1; /* REQ -> RESP */
111 ph->pipe_handle = oph->pipe_handle;
112 ph->error_code = code;
114 pn_skb_get_src_sockaddr(oskb, &peer);
115 return pn_skb_send(sk, skb, &peer);
118 static int pep_indicate(struct sock *sk, u8 id, u8 code,
119 const void *data, int len, gfp_t priority)
121 struct pep_sock *pn = pep_sk(sk);
122 struct pnpipehdr *ph;
125 skb = pep_alloc_skb(sk, data, len, priority);
132 ph->pipe_handle = pn->pipe_handle;
134 return pn_skb_send(sk, skb, NULL);
139 static int pipe_handler_request(struct sock *sk, u8 id, u8 code,
140 const void *data, int len)
142 struct pep_sock *pn = pep_sk(sk);
143 struct pnpipehdr *ph;
146 skb = pep_alloc_skb(sk, data, len, GFP_KERNEL);
151 ph->utid = id; /* whatever */
153 ph->pipe_handle = pn->pipe_handle;
155 return pn_skb_send(sk, skb, NULL);
158 static int pipe_handler_send_created_ind(struct sock *sk)
160 struct pep_sock *pn = pep_sk(sk);
162 PN_PIPE_SB_NEGOTIATED_FC, pep_sb_size(2),
163 pn->tx_fc, pn->rx_fc,
166 return pep_indicate(sk, PNS_PIPE_CREATED_IND, 1 /* sub-blocks */,
167 data, 4, GFP_ATOMIC);
170 static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
172 static const u8 data[20] = {
173 PAD, PAD, PAD, 2 /* sub-blocks */,
174 PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
175 PN_MULTI_CREDIT_FLOW_CONTROL,
176 PN_ONE_CREDIT_FLOW_CONTROL,
177 PN_LEGACY_FLOW_CONTROL,
179 PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
180 PN_MULTI_CREDIT_FLOW_CONTROL,
181 PN_ONE_CREDIT_FLOW_CONTROL,
182 PN_LEGACY_FLOW_CONTROL,
187 return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
191 static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code,
194 static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
195 WARN_ON(code == PN_PIPE_NO_ERROR);
196 return pep_reply(sk, skb, code, data, sizeof(data), priority);
199 /* Control requests are not sent by the pipe service and have a specific
201 static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
204 const struct pnpipehdr *oph = pnp_hdr(oskb);
206 struct pnpipehdr *ph;
207 struct sockaddr_pn dst;
209 oph->data[0], /* PEP type */
210 code, /* error code, at an unusual offset */
214 skb = pep_alloc_skb(sk, data, 4, priority);
219 ph->utid = oph->utid;
220 ph->message_id = PNS_PEP_CTRL_RESP;
221 ph->pipe_handle = oph->pipe_handle;
222 ph->data[0] = oph->data[1]; /* CTRL id */
224 pn_skb_get_src_sockaddr(oskb, &dst);
225 return pn_skb_send(sk, skb, &dst);
228 static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
230 u8 data[4] = { type, PAD, PAD, status };
232 return pep_indicate(sk, PNS_PEP_STATUS_IND, PN_PEP_TYPE_COMMON,
236 /* Send our RX flow control information to the sender.
237 * Socket must be locked. */
238 static void pipe_grant_credits(struct sock *sk, gfp_t priority)
240 struct pep_sock *pn = pep_sk(sk);
242 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
245 case PN_LEGACY_FLOW_CONTROL: /* TODO */
247 case PN_ONE_CREDIT_FLOW_CONTROL:
248 if (pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
249 PEP_IND_READY, priority) == 0)
252 case PN_MULTI_CREDIT_FLOW_CONTROL:
253 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
255 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
256 CREDITS_MAX - pn->rx_credits,
258 pn->rx_credits = CREDITS_MAX;
263 static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
265 struct pep_sock *pn = pep_sk(sk);
266 struct pnpipehdr *hdr;
269 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
273 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
274 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
275 (unsigned)hdr->data[0]);
279 switch (hdr->data[1]) {
280 case PN_PEP_IND_FLOW_CONTROL:
282 case PN_LEGACY_FLOW_CONTROL:
283 switch (hdr->data[4]) {
285 atomic_set(&pn->tx_credits, 0);
288 atomic_set(&pn->tx_credits, wake = 1);
292 case PN_ONE_CREDIT_FLOW_CONTROL:
293 if (hdr->data[4] == PEP_IND_READY)
294 atomic_set(&pn->tx_credits, wake = 1);
299 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
300 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
302 atomic_add(wake = hdr->data[4], &pn->tx_credits);
306 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
307 (unsigned)hdr->data[1]);
311 sk->sk_write_space(sk);
315 static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
317 struct pep_sock *pn = pep_sk(sk);
318 struct pnpipehdr *hdr = pnp_hdr(skb);
319 u8 n_sb = hdr->data[0];
321 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
322 __skb_pull(skb, sizeof(*hdr));
324 u8 type, buf[2], len = sizeof(buf);
325 u8 *data = pep_get_sb(skb, &type, &len, buf);
330 case PN_PIPE_SB_NEGOTIATED_FC:
331 if (len < 2 || (data[0] | data[1]) > 3)
333 pn->tx_fc = data[0] & 3;
334 pn->rx_fc = data[1] & 3;
342 /* Queue an skb to a connected sock.
343 * Socket lock must be held. */
344 static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
346 struct pep_sock *pn = pep_sk(sk);
347 struct pnpipehdr *hdr = pnp_hdr(skb);
348 struct sk_buff_head *queue;
351 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
353 switch (hdr->message_id) {
354 case PNS_PEP_CONNECT_REQ:
355 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_ATOMIC);
358 case PNS_PEP_DISCONNECT_REQ:
359 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
360 sk->sk_state = TCP_CLOSE_WAIT;
361 if (!sock_flag(sk, SOCK_DEAD))
362 sk->sk_state_change(sk);
365 case PNS_PEP_ENABLE_REQ:
366 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
367 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
370 case PNS_PEP_RESET_REQ:
371 switch (hdr->state_after_reset) {
372 case PN_PIPE_DISABLE:
378 default: /* not allowed to send an error here!? */
383 case PNS_PEP_DISABLE_REQ:
384 atomic_set(&pn->tx_credits, 0);
385 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
388 case PNS_PEP_CTRL_REQ:
389 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
390 atomic_inc(&sk->sk_drops);
394 queue = &pn->ctrlreq_queue;
397 case PNS_PIPE_ALIGNED_DATA:
401 __skb_pull(skb, 3); /* Pipe data header */
402 if (!pn_flow_safe(pn->rx_fc)) {
403 err = sock_queue_rcv_skb(sk, skb);
405 return NET_RX_SUCCESS;
410 if (pn->rx_credits == 0) {
411 atomic_inc(&sk->sk_drops);
416 queue = &sk->sk_receive_queue;
419 case PNS_PEP_STATUS_IND:
420 pipe_rcv_status(sk, skb);
423 case PNS_PIPE_REDIRECTED_IND:
424 err = pipe_rcv_created(sk, skb);
427 case PNS_PIPE_CREATED_IND:
428 err = pipe_rcv_created(sk, skb);
432 case PNS_PIPE_RESET_IND:
433 if (!pn->init_enable)
436 case PNS_PIPE_ENABLED_IND:
437 if (!pn_flow_safe(pn->tx_fc)) {
438 atomic_set(&pn->tx_credits, 1);
439 sk->sk_write_space(sk);
441 if (sk->sk_state == TCP_ESTABLISHED)
442 break; /* Nothing to do */
443 sk->sk_state = TCP_ESTABLISHED;
444 pipe_grant_credits(sk, GFP_ATOMIC);
447 case PNS_PIPE_DISABLED_IND:
448 sk->sk_state = TCP_SYN_RECV;
453 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
459 return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS;
463 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, err);
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 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 /* Queue an skb to an actively connected sock.
537 * Socket lock must be held. */
538 static int pipe_handler_do_rcv(struct sock *sk, struct sk_buff *skb)
540 struct pep_sock *pn = pep_sk(sk);
541 struct pnpipehdr *hdr = pnp_hdr(skb);
542 int err = NET_RX_SUCCESS;
544 switch (hdr->message_id) {
545 case PNS_PIPE_ALIGNED_DATA:
549 __skb_pull(skb, 3); /* Pipe data header */
550 if (!pn_flow_safe(pn->rx_fc)) {
551 err = sock_queue_rcv_skb(sk, skb);
553 return NET_RX_SUCCESS;
558 if (pn->rx_credits == 0) {
559 atomic_inc(&sk->sk_drops);
565 skb_set_owner_r(skb, sk);
567 skb_queue_tail(&sk->sk_receive_queue, skb);
568 if (!sock_flag(sk, SOCK_DEAD))
569 sk->sk_data_ready(sk, err);
570 return NET_RX_SUCCESS;
572 case PNS_PEP_CONNECT_RESP:
573 if (sk->sk_state != TCP_SYN_SENT)
575 if (!sock_flag(sk, SOCK_DEAD))
576 sk->sk_state_change(sk);
577 if (pep_connresp_rcv(sk, skb)) {
578 sk->sk_state = TCP_CLOSE_WAIT;
582 sk->sk_state = TCP_ESTABLISHED;
583 if (!pn_flow_safe(pn->tx_fc)) {
584 atomic_set(&pn->tx_credits, 1);
585 sk->sk_write_space(sk);
587 pipe_grant_credits(sk, GFP_ATOMIC);
590 case PNS_PEP_DISCONNECT_RESP:
591 /* sock should already be dead, nothing to do */
594 case PNS_PEP_STATUS_IND:
595 pipe_rcv_status(sk, skb);
602 /* Listening sock must be locked */
603 static struct sock *pep_find_pipe(const struct hlist_head *hlist,
604 const struct sockaddr_pn *dst,
607 struct hlist_node *node;
609 u16 dobj = pn_sockaddr_get_object(dst);
611 sk_for_each(sknode, node, hlist) {
612 struct pep_sock *pnnode = pep_sk(sknode);
614 /* Ports match, but addresses might not: */
615 if (pnnode->pn_sk.sobject != dobj)
617 if (pnnode->pipe_handle != pipe_handle)
619 if (sknode->sk_state == TCP_CLOSE_WAIT)
629 * Deliver an skb to a listening sock.
630 * Socket lock must be held.
631 * We then queue the skb to the right connected sock (if any).
633 static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
635 struct pep_sock *pn = pep_sk(sk);
637 struct pnpipehdr *hdr;
638 struct sockaddr_pn dst;
641 if (!pskb_may_pull(skb, sizeof(*hdr)))
645 pipe_handle = hdr->pipe_handle;
646 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
649 pn_skb_get_dst_sockaddr(skb, &dst);
651 /* Look for an existing pipe handle */
652 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
654 return sk_receive_skb(sknode, skb, 1);
656 switch (hdr->message_id) {
657 case PNS_PEP_CONNECT_REQ:
658 if (sk->sk_state != TCP_LISTEN || sk_acceptq_is_full(sk)) {
659 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE,
663 skb_queue_head(&sk->sk_receive_queue, skb);
664 sk_acceptq_added(sk);
665 if (!sock_flag(sk, SOCK_DEAD))
666 sk->sk_data_ready(sk, 0);
667 return NET_RX_SUCCESS;
669 case PNS_PEP_DISCONNECT_REQ:
670 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
673 case PNS_PEP_CTRL_REQ:
674 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
677 case PNS_PEP_RESET_REQ:
678 case PNS_PEP_ENABLE_REQ:
679 case PNS_PEP_DISABLE_REQ:
680 /* invalid handle is not even allowed here! */
684 if ((1 << sk->sk_state)
685 & ~(TCPF_CLOSE|TCPF_LISTEN|TCPF_CLOSE_WAIT))
686 /* actively connected socket */
687 return pipe_handler_do_rcv(sk, skb);
691 return NET_RX_SUCCESS;
694 static int pipe_do_remove(struct sock *sk)
696 struct pep_sock *pn = pep_sk(sk);
697 struct pnpipehdr *ph;
700 skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL);
706 ph->message_id = PNS_PIPE_REMOVE_REQ;
707 ph->pipe_handle = pn->pipe_handle;
709 return pn_skb_send(sk, skb, NULL);
712 /* associated socket ceases to exist */
713 static void pep_sock_close(struct sock *sk, long timeout)
715 struct pep_sock *pn = pep_sk(sk);
718 sock_hold(sk); /* keep a reference after sk_common_release() */
719 sk_common_release(sk);
722 if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
723 if (sk->sk_backlog_rcv == pipe_do_rcv)
724 /* Forcefully remove dangling Phonet pipe */
727 pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD,
730 sk->sk_state = TCP_CLOSE;
732 ifindex = pn->ifindex;
741 static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
743 struct pep_sock *pn = pep_sk(sk), *newpn;
744 struct sock *newsk = NULL;
746 struct pnpipehdr *hdr;
747 struct sockaddr_pn dst, src;
750 u8 pipe_handle, enabled, n_sb;
753 skb = skb_recv_datagram(sk, 0, flags & O_NONBLOCK, errp);
758 if (sk->sk_state != TCP_LISTEN) {
762 sk_acceptq_removed(sk);
765 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
769 pipe_handle = hdr->pipe_handle;
770 switch (hdr->state_after_connect) {
771 case PN_PIPE_DISABLE:
778 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM,
782 peer_type = hdr->other_pep_type << 8;
784 /* Parse sub-blocks (options) */
787 u8 type, buf[1], len = sizeof(buf);
788 const u8 *data = pep_get_sb(skb, &type, &len, buf);
793 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
796 peer_type = (peer_type & 0xff00) | data[0];
798 case PN_PIPE_SB_ALIGNED_DATA:
799 aligned = data[0] != 0;
805 /* Check for duplicate pipe handle */
806 newsk = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
807 if (unlikely(newsk)) {
810 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_KERNEL);
814 /* Create a new to-be-accepted sock */
815 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_KERNEL, sk->sk_prot);
817 pep_reject_conn(sk, skb, PN_PIPE_ERR_OVERLOAD, GFP_KERNEL);
822 sock_init_data(NULL, newsk);
823 newsk->sk_state = TCP_SYN_RECV;
824 newsk->sk_backlog_rcv = pipe_do_rcv;
825 newsk->sk_protocol = sk->sk_protocol;
826 newsk->sk_destruct = pipe_destruct;
828 newpn = pep_sk(newsk);
829 pn_skb_get_dst_sockaddr(skb, &dst);
830 pn_skb_get_src_sockaddr(skb, &src);
831 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
832 newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
833 newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
835 newpn->listener = sk;
836 skb_queue_head_init(&newpn->ctrlreq_queue);
837 newpn->pipe_handle = pipe_handle;
838 atomic_set(&newpn->tx_credits, 0);
840 newpn->peer_type = peer_type;
841 newpn->rx_credits = 0;
842 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
843 newpn->init_enable = enabled;
844 newpn->aligned = aligned;
846 err = pep_accept_conn(newsk, skb);
852 sk_add_node(newsk, &pn->hlist);
860 static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
862 struct pep_sock *pn = pep_sk(sk);
864 u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD };
866 pn->pipe_handle = 1; /* anything but INVALID_HANDLE */
867 err = pipe_handler_request(sk, PNS_PEP_CONNECT_REQ,
868 PN_PIPE_ENABLE, data, 4);
870 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
873 sk->sk_state = TCP_SYN_SENT;
877 static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
879 struct pep_sock *pn = pep_sk(sk);
884 if (sk->sk_state == TCP_LISTEN)
888 if (sock_flag(sk, SOCK_URGINLINE) &&
889 !skb_queue_empty(&pn->ctrlreq_queue))
890 answ = skb_peek(&pn->ctrlreq_queue)->len;
891 else if (!skb_queue_empty(&sk->sk_receive_queue))
892 answ = skb_peek(&sk->sk_receive_queue)->len;
896 return put_user(answ, (int __user *)arg);
902 static int pep_init(struct sock *sk)
904 struct pep_sock *pn = pep_sk(sk);
906 sk->sk_destruct = pipe_destruct;
907 INIT_HLIST_HEAD(&pn->hlist);
909 skb_queue_head_init(&pn->ctrlreq_queue);
910 atomic_set(&pn->tx_credits, 0);
913 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
915 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
921 static int pep_setsockopt(struct sock *sk, int level, int optname,
922 char __user *optval, unsigned int optlen)
924 struct pep_sock *pn = pep_sk(sk);
925 int val = 0, err = 0;
927 if (level != SOL_PNPIPE)
929 if (optlen >= sizeof(int)) {
930 if (get_user(val, (int __user *) optval))
937 if (val && val != PNPIPE_ENCAP_IP) {
941 if (!pn->ifindex == !val)
942 break; /* Nothing to do! */
943 if (!capable(CAP_NET_ADMIN)) {
949 err = gprs_attach(sk);
971 static int pep_getsockopt(struct sock *sk, int level, int optname,
972 char __user *optval, int __user *optlen)
974 struct pep_sock *pn = pep_sk(sk);
977 if (level != SOL_PNPIPE)
979 if (get_user(len, optlen))
984 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
992 val = pn->pipe_handle;
993 if (val == PN_PIPE_INVALID_HANDLE)
1001 len = min_t(unsigned int, sizeof(int), len);
1002 if (put_user(len, optlen))
1004 if (put_user(val, (int __user *) optval))
1009 static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1011 struct pep_sock *pn = pep_sk(sk);
1012 struct pnpipehdr *ph;
1015 if (pn_flow_safe(pn->tx_fc) &&
1016 !atomic_add_unless(&pn->tx_credits, -1, 0)) {
1021 skb_push(skb, 3 + pn->aligned);
1022 skb_reset_transport_header(skb);
1026 ph->message_id = PNS_PIPE_ALIGNED_DATA;
1027 ph->data[0] = 0; /* padding */
1029 ph->message_id = PNS_PIPE_DATA;
1030 ph->pipe_handle = pn->pipe_handle;
1031 err = pn_skb_send(sk, skb, NULL);
1033 if (err && pn_flow_safe(pn->tx_fc))
1034 atomic_inc(&pn->tx_credits);
1039 static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
1040 struct msghdr *msg, size_t len)
1042 struct pep_sock *pn = pep_sk(sk);
1043 struct sk_buff *skb;
1045 int flags = msg->msg_flags;
1048 if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
1049 MSG_CMSG_COMPAT)) ||
1050 !(msg->msg_flags & MSG_EOR))
1053 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
1054 flags & MSG_DONTWAIT, &err);
1058 skb_reserve(skb, MAX_PHONET_HEADER + 3 + pn->aligned);
1059 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1064 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1065 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
1069 if (sk->sk_state != TCP_ESTABLISHED) {
1070 /* Wait until the pipe gets to enabled state */
1072 err = sk_stream_wait_connect(sk, &timeo);
1076 if (sk->sk_state == TCP_CLOSE_WAIT) {
1081 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
1083 /* Wait until flow control allows TX */
1084 done = atomic_read(&pn->tx_credits);
1092 if (signal_pending(current)) {
1093 err = sock_intr_errno(timeo);
1097 prepare_to_wait(sk_sleep(sk), &wait,
1098 TASK_INTERRUPTIBLE);
1099 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
1100 finish_wait(sk_sleep(sk), &wait);
1102 if (sk->sk_state != TCP_ESTABLISHED)
1106 err = pipe_skb_send(sk, skb);
1108 err = len; /* success! */
1117 int pep_writeable(struct sock *sk)
1119 struct pep_sock *pn = pep_sk(sk);
1121 return atomic_read(&pn->tx_credits);
1124 int pep_write(struct sock *sk, struct sk_buff *skb)
1126 struct sk_buff *rskb, *fs;
1129 if (pep_sk(sk)->aligned)
1130 return pipe_skb_send(sk, skb);
1132 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
1137 skb_shinfo(rskb)->frag_list = skb;
1138 rskb->len += skb->len;
1139 rskb->data_len += rskb->len;
1140 rskb->truesize += rskb->len;
1142 /* Avoid nested fragments */
1143 skb_walk_frags(skb, fs)
1145 skb->next = skb_shinfo(skb)->frag_list;
1146 skb_frag_list_init(skb);
1148 skb->data_len -= flen;
1149 skb->truesize -= flen;
1151 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
1152 return pipe_skb_send(sk, rskb);
1155 struct sk_buff *pep_read(struct sock *sk)
1157 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1159 if (sk->sk_state == TCP_ESTABLISHED)
1160 pipe_grant_credits(sk, GFP_ATOMIC);
1164 static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
1165 struct msghdr *msg, size_t len, int noblock,
1166 int flags, int *addr_len)
1168 struct sk_buff *skb;
1171 if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
1172 MSG_NOSIGNAL|MSG_CMSG_COMPAT))
1175 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
1178 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
1179 /* Dequeue and acknowledge control request */
1180 struct pep_sock *pn = pep_sk(sk);
1182 if (flags & MSG_PEEK)
1184 skb = skb_dequeue(&pn->ctrlreq_queue);
1186 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1188 msg->msg_flags |= MSG_OOB;
1191 if (flags & MSG_OOB)
1195 skb = skb_recv_datagram(sk, flags, noblock, &err);
1198 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1204 if (sk->sk_state == TCP_ESTABLISHED)
1205 pipe_grant_credits(sk, GFP_KERNEL);
1208 msg->msg_flags |= MSG_EOR;
1210 msg->msg_flags |= MSG_TRUNC;
1214 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1216 err = (flags & MSG_TRUNC) ? skb->len : len;
1218 skb_free_datagram(sk, skb);
1222 static void pep_sock_unhash(struct sock *sk)
1224 struct pep_sock *pn = pep_sk(sk);
1225 struct sock *skparent = NULL;
1229 if (pn->listener != NULL) {
1230 skparent = pn->listener;
1231 pn->listener = NULL;
1234 pn = pep_sk(skparent);
1235 lock_sock(skparent);
1236 sk_del_node_init(sk);
1240 /* Unhash a listening sock only when it is closed
1241 * and all of its active connected pipes are closed. */
1242 if (hlist_empty(&pn->hlist))
1243 pn_sock_unhash(&pn->pn_sk.sk);
1250 static struct proto pep_proto = {
1251 .close = pep_sock_close,
1252 .accept = pep_sock_accept,
1253 .connect = pep_sock_connect,
1256 .setsockopt = pep_setsockopt,
1257 .getsockopt = pep_getsockopt,
1258 .sendmsg = pep_sendmsg,
1259 .recvmsg = pep_recvmsg,
1260 .backlog_rcv = pep_do_rcv,
1261 .hash = pn_sock_hash,
1262 .unhash = pep_sock_unhash,
1263 .get_port = pn_sock_get_port,
1264 .obj_size = sizeof(struct pep_sock),
1265 .owner = THIS_MODULE,
1269 static struct phonet_protocol pep_pn_proto = {
1270 .ops = &phonet_stream_ops,
1272 .sock_type = SOCK_SEQPACKET,
1275 static int __init pep_register(void)
1277 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1280 static void __exit pep_unregister(void)
1282 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1285 module_init(pep_register);
1286 module_exit(pep_unregister);
1287 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1288 MODULE_DESCRIPTION("Phonet pipe protocol");
1289 MODULE_LICENSE("GPL");
1290 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);