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 #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 int pep_reply(struct sock
*sk
, struct sk_buff
*oskb
,
81 u8 code
, const void *data
, int len
, gfp_t priority
)
83 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
86 struct sockaddr_pn peer
;
88 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ len
, priority
);
91 skb_set_owner_w(skb
, sk
);
93 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
95 skb_copy_to_linear_data(skb
, data
, len
);
96 __skb_push(skb
, sizeof(*ph
));
97 skb_reset_transport_header(skb
);
100 ph
->message_id
= oph
->message_id
+ 1; /* REQ -> RESP */
101 ph
->pipe_handle
= oph
->pipe_handle
;
102 ph
->error_code
= code
;
104 pn_skb_get_src_sockaddr(oskb
, &peer
);
105 return pn_skb_send(sk
, skb
, &peer
);
110 #ifdef CONFIG_PHONET_PIPECTRLR
111 static u8
pipe_negotiate_fc(u8
*host_fc
, u8
*remote_fc
, int len
)
114 u8 base_fc
, final_fc
;
116 for (i
= 0; i
< len
; i
++) {
117 base_fc
= host_fc
[i
];
118 for (j
= 0; j
< len
; j
++) {
119 if (remote_fc
[j
] == base_fc
) {
132 static int pipe_get_flow_info(struct sock
*sk
, struct sk_buff
*skb
,
133 u8
*pref_rx_fc
, u8
*req_tx_fc
)
135 struct pnpipehdr
*hdr
;
138 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
144 __skb_pull(skb
, sizeof(*hdr
) + 4);
146 u8 type
, buf
[3], len
= sizeof(buf
);
147 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
153 case PN_PIPE_SB_REQUIRED_FC_TX
:
154 if (len
< 3 || (data
[2] | data
[3] | data
[4]) > 3)
156 req_tx_fc
[0] = data
[2];
157 req_tx_fc
[1] = data
[3];
158 req_tx_fc
[2] = data
[4];
161 case PN_PIPE_SB_PREFERRED_FC_RX
:
162 if (len
< 3 || (data
[2] | data
[3] | data
[4]) > 3)
164 pref_rx_fc
[0] = data
[2];
165 pref_rx_fc
[1] = data
[3];
166 pref_rx_fc
[2] = data
[4];
175 static int pipe_handler_send_req(struct sock
*sk
, u8 utid
,
176 u8 msg_id
, gfp_t priority
)
179 struct pnpipehdr
*ph
;
181 struct pep_sock
*pn
= pep_sk(sk
);
183 static const u8 data
[4] = {
188 case PNS_PEP_CONNECT_REQ
:
192 case PNS_PEP_DISCONNECT_REQ
:
193 case PNS_PEP_ENABLE_REQ
:
194 case PNS_PEP_DISABLE_REQ
:
202 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ len
, priority
);
205 skb_set_owner_w(skb
, sk
);
207 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
210 skb_copy_to_linear_data(skb
, data
, len
);
212 __skb_push(skb
, sizeof(*ph
));
213 skb_reset_transport_header(skb
);
216 ph
->message_id
= msg_id
;
217 ph
->pipe_handle
= pn
->pipe_handle
;
218 ph
->error_code
= PN_PIPE_NO_ERROR
;
220 return pn_skb_send(sk
, skb
, NULL
);
223 static int pipe_handler_send_created_ind(struct sock
*sk
,
227 struct pnpipehdr
*ph
;
230 struct pep_sock
*pn
= pep_sk(sk
);
231 static u8 data
[4] = {
238 * actually, below is number of sub-blocks and not error code.
239 * Pipe_created_ind message format does not have any
240 * error code field. However, the Phonet stack will always send
241 * an error code as part of pnpipehdr. So, use that err_code to
242 * specify the number of sub-blocks.
246 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ sizeof(data
), GFP_ATOMIC
);
249 skb_set_owner_w(skb
, sk
);
251 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
252 __skb_put(skb
, sizeof(data
));
253 skb_copy_to_linear_data(skb
, data
, sizeof(data
));
254 __skb_push(skb
, sizeof(*ph
));
255 skb_reset_transport_header(skb
);
258 ph
->message_id
= msg_id
;
259 ph
->pipe_handle
= pn
->pipe_handle
;
260 ph
->error_code
= err_code
;
262 return pn_skb_send(sk
, skb
, NULL
);
265 static int pipe_handler_send_ind(struct sock
*sk
, u8 utid
, u8 msg_id
)
268 struct pnpipehdr
*ph
;
270 struct pep_sock
*pn
= pep_sk(sk
);
273 * actually, below is a filler.
274 * Pipe_enabled/disabled_ind message format does not have any
275 * error code field. However, the Phonet stack will always send
276 * an error code as part of pnpipehdr. So, use that err_code to
277 * specify the filler value.
281 skb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
284 skb_set_owner_w(skb
, sk
);
286 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
287 __skb_push(skb
, sizeof(*ph
));
288 skb_reset_transport_header(skb
);
291 ph
->message_id
= msg_id
;
292 ph
->pipe_handle
= pn
->pipe_handle
;
293 ph
->error_code
= err_code
;
295 return pn_skb_send(sk
, skb
, NULL
);
298 static int pipe_handler_enable_pipe(struct sock
*sk
, int enable
)
303 utid
= PNS_PIPE_ENABLE_UTID
;
304 req
= PNS_PEP_ENABLE_REQ
;
306 utid
= PNS_PIPE_DISABLE_UTID
;
307 req
= PNS_PEP_DISABLE_REQ
;
309 return pipe_handler_send_req(sk
, utid
, req
, GFP_ATOMIC
);
313 static int pep_accept_conn(struct sock
*sk
, struct sk_buff
*skb
)
315 static const u8 data
[20] = {
316 PAD
, PAD
, PAD
, 2 /* sub-blocks */,
317 PN_PIPE_SB_REQUIRED_FC_TX
, pep_sb_size(5), 3, PAD
,
318 PN_MULTI_CREDIT_FLOW_CONTROL
,
319 PN_ONE_CREDIT_FLOW_CONTROL
,
320 PN_LEGACY_FLOW_CONTROL
,
322 PN_PIPE_SB_PREFERRED_FC_RX
, pep_sb_size(5), 3, PAD
,
323 PN_MULTI_CREDIT_FLOW_CONTROL
,
324 PN_ONE_CREDIT_FLOW_CONTROL
,
325 PN_LEGACY_FLOW_CONTROL
,
330 return pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, data
, sizeof(data
),
334 static int pep_reject_conn(struct sock
*sk
, struct sk_buff
*skb
, u8 code
)
336 static const u8 data
[4] = { PAD
, PAD
, PAD
, 0 /* sub-blocks */ };
337 WARN_ON(code
== PN_PIPE_NO_ERROR
);
338 return pep_reply(sk
, skb
, code
, data
, sizeof(data
), GFP_ATOMIC
);
341 /* Control requests are not sent by the pipe service and have a specific
343 static int pep_ctrlreq_error(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
346 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
348 struct pnpipehdr
*ph
;
349 struct sockaddr_pn dst
;
351 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
354 skb_set_owner_w(skb
, sk
);
356 skb_reserve(skb
, MAX_PHONET_HEADER
);
357 ph
= (struct pnpipehdr
*)skb_put(skb
, sizeof(*ph
) + 4);
359 ph
->utid
= oph
->utid
;
360 ph
->message_id
= PNS_PEP_CTRL_RESP
;
361 ph
->pipe_handle
= oph
->pipe_handle
;
362 ph
->data
[0] = oph
->data
[1]; /* CTRL id */
363 ph
->data
[1] = oph
->data
[0]; /* PEP type */
364 ph
->data
[2] = code
; /* error code, at an usual offset */
368 pn_skb_get_src_sockaddr(oskb
, &dst
);
369 return pn_skb_send(sk
, skb
, &dst
);
372 static int pipe_snd_status(struct sock
*sk
, u8 type
, u8 status
, gfp_t priority
)
374 struct pep_sock
*pn
= pep_sk(sk
);
375 struct pnpipehdr
*ph
;
378 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
381 skb_set_owner_w(skb
, sk
);
383 skb_reserve(skb
, MAX_PNPIPE_HEADER
+ 4);
384 __skb_push(skb
, sizeof(*ph
) + 4);
385 skb_reset_transport_header(skb
);
388 ph
->message_id
= PNS_PEP_STATUS_IND
;
389 ph
->pipe_handle
= pn
->pipe_handle
;
390 ph
->pep_type
= PN_PEP_TYPE_COMMON
;
394 ph
->data
[4] = status
;
396 return pn_skb_send(sk
, skb
, NULL
);
399 /* Send our RX flow control information to the sender.
400 * Socket must be locked. */
401 static void pipe_grant_credits(struct sock
*sk
)
403 struct pep_sock
*pn
= pep_sk(sk
);
405 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
408 case PN_LEGACY_FLOW_CONTROL
: /* TODO */
410 case PN_ONE_CREDIT_FLOW_CONTROL
:
411 pipe_snd_status(sk
, PN_PEP_IND_FLOW_CONTROL
,
412 PEP_IND_READY
, GFP_ATOMIC
);
415 case PN_MULTI_CREDIT_FLOW_CONTROL
:
416 if ((pn
->rx_credits
+ CREDITS_THR
) > CREDITS_MAX
)
418 if (pipe_snd_status(sk
, PN_PEP_IND_ID_MCFC_GRANT_CREDITS
,
419 CREDITS_MAX
- pn
->rx_credits
,
421 pn
->rx_credits
= CREDITS_MAX
;
426 static int pipe_rcv_status(struct sock
*sk
, struct sk_buff
*skb
)
428 struct pep_sock
*pn
= pep_sk(sk
);
429 struct pnpipehdr
*hdr
;
432 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
436 if (hdr
->data
[0] != PN_PEP_TYPE_COMMON
) {
437 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP type: %u\n",
438 (unsigned)hdr
->data
[0]);
442 switch (hdr
->data
[1]) {
443 case PN_PEP_IND_FLOW_CONTROL
:
445 case PN_LEGACY_FLOW_CONTROL
:
446 switch (hdr
->data
[4]) {
448 atomic_set(&pn
->tx_credits
, 0);
451 atomic_set(&pn
->tx_credits
, wake
= 1);
455 case PN_ONE_CREDIT_FLOW_CONTROL
:
456 if (hdr
->data
[4] == PEP_IND_READY
)
457 atomic_set(&pn
->tx_credits
, wake
= 1);
462 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS
:
463 if (pn
->tx_fc
!= PN_MULTI_CREDIT_FLOW_CONTROL
)
465 atomic_add(wake
= hdr
->data
[4], &pn
->tx_credits
);
469 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP indication: %u\n",
470 (unsigned)hdr
->data
[1]);
474 sk
->sk_write_space(sk
);
478 static int pipe_rcv_created(struct sock
*sk
, struct sk_buff
*skb
)
480 struct pep_sock
*pn
= pep_sk(sk
);
481 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
482 u8 n_sb
= hdr
->data
[0];
484 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
485 __skb_pull(skb
, sizeof(*hdr
));
487 u8 type
, buf
[2], len
= sizeof(buf
);
488 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
493 case PN_PIPE_SB_NEGOTIATED_FC
:
494 if (len
< 2 || (data
[0] | data
[1]) > 3)
496 pn
->tx_fc
= data
[0] & 3;
497 pn
->rx_fc
= data
[1] & 3;
505 /* Queue an skb to a connected sock.
506 * Socket lock must be held. */
507 static int pipe_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
509 struct pep_sock
*pn
= pep_sk(sk
);
510 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
511 struct sk_buff_head
*queue
;
514 BUG_ON(sk
->sk_state
== TCP_CLOSE_WAIT
);
516 switch (hdr
->message_id
) {
517 case PNS_PEP_CONNECT_REQ
:
518 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
521 case PNS_PEP_DISCONNECT_REQ
:
522 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
523 sk
->sk_state
= TCP_CLOSE_WAIT
;
524 if (!sock_flag(sk
, SOCK_DEAD
))
525 sk
->sk_state_change(sk
);
528 #ifdef CONFIG_PHONET_PIPECTRLR
529 case PNS_PEP_DISCONNECT_RESP
:
530 pn
->pipe_state
= PIPE_IDLE
;
531 sk
->sk_state
= TCP_CLOSE
;
535 case PNS_PEP_ENABLE_REQ
:
536 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
537 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
540 #ifdef CONFIG_PHONET_PIPECTRLR
541 case PNS_PEP_ENABLE_RESP
:
542 pn
->pipe_state
= PIPE_ENABLED
;
543 pipe_handler_send_ind(sk
, PNS_PIPE_ENABLED_IND_UTID
,
544 PNS_PIPE_ENABLED_IND
);
546 if (!pn_flow_safe(pn
->tx_fc
)) {
547 atomic_set(&pn
->tx_credits
, 1);
548 sk
->sk_write_space(sk
);
550 if (sk
->sk_state
== TCP_ESTABLISHED
)
551 break; /* Nothing to do */
552 sk
->sk_state
= TCP_ESTABLISHED
;
553 pipe_grant_credits(sk
);
557 case PNS_PEP_RESET_REQ
:
558 switch (hdr
->state_after_reset
) {
559 case PN_PIPE_DISABLE
:
565 default: /* not allowed to send an error here!? */
570 case PNS_PEP_DISABLE_REQ
:
571 atomic_set(&pn
->tx_credits
, 0);
572 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
575 #ifdef CONFIG_PHONET_PIPECTRLR
576 case PNS_PEP_DISABLE_RESP
:
577 pn
->pipe_state
= PIPE_DISABLED
;
578 atomic_set(&pn
->tx_credits
, 0);
579 pipe_handler_send_ind(sk
, PNS_PIPE_DISABLED_IND_UTID
,
580 PNS_PIPE_DISABLED_IND
);
581 sk
->sk_state
= TCP_SYN_RECV
;
586 case PNS_PEP_CTRL_REQ
:
587 if (skb_queue_len(&pn
->ctrlreq_queue
) >= PNPIPE_CTRLREQ_MAX
) {
588 atomic_inc(&sk
->sk_drops
);
592 queue
= &pn
->ctrlreq_queue
;
595 case PNS_PIPE_ALIGNED_DATA
:
599 __skb_pull(skb
, 3); /* Pipe data header */
600 if (!pn_flow_safe(pn
->rx_fc
)) {
601 err
= sock_queue_rcv_skb(sk
, skb
);
607 if (pn
->rx_credits
== 0) {
608 atomic_inc(&sk
->sk_drops
);
613 queue
= &sk
->sk_receive_queue
;
616 case PNS_PEP_STATUS_IND
:
617 pipe_rcv_status(sk
, skb
);
620 case PNS_PIPE_REDIRECTED_IND
:
621 err
= pipe_rcv_created(sk
, skb
);
624 case PNS_PIPE_CREATED_IND
:
625 err
= pipe_rcv_created(sk
, skb
);
629 case PNS_PIPE_RESET_IND
:
630 if (!pn
->init_enable
)
633 case PNS_PIPE_ENABLED_IND
:
634 if (!pn_flow_safe(pn
->tx_fc
)) {
635 atomic_set(&pn
->tx_credits
, 1);
636 sk
->sk_write_space(sk
);
638 if (sk
->sk_state
== TCP_ESTABLISHED
)
639 break; /* Nothing to do */
640 sk
->sk_state
= TCP_ESTABLISHED
;
641 pipe_grant_credits(sk
);
644 case PNS_PIPE_DISABLED_IND
:
645 sk
->sk_state
= TCP_SYN_RECV
;
650 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP message: %u\n",
660 skb_set_owner_r(skb
, sk
);
662 skb_queue_tail(queue
, skb
);
663 if (!sock_flag(sk
, SOCK_DEAD
))
664 sk
->sk_data_ready(sk
, err
);
668 /* Destroy connected sock. */
669 static void pipe_destruct(struct sock
*sk
)
671 struct pep_sock
*pn
= pep_sk(sk
);
673 skb_queue_purge(&sk
->sk_receive_queue
);
674 skb_queue_purge(&pn
->ctrlreq_queue
);
677 #ifdef CONFIG_PHONET_PIPECTRLR
678 static int pep_connresp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
680 struct pep_sock
*pn
= pep_sk(sk
);
681 u8 host_pref_rx_fc
[3] = {3, 2, 1}, host_req_tx_fc
[3] = {3, 2, 1};
682 u8 remote_pref_rx_fc
[3], remote_req_tx_fc
[3];
683 u8 negotiated_rx_fc
, negotiated_tx_fc
;
686 pipe_get_flow_info(sk
, skb
, remote_pref_rx_fc
,
688 negotiated_tx_fc
= pipe_negotiate_fc(remote_req_tx_fc
,
690 sizeof(host_pref_rx_fc
));
691 negotiated_rx_fc
= pipe_negotiate_fc(host_req_tx_fc
,
693 sizeof(host_pref_rx_fc
));
695 pn
->pipe_state
= PIPE_DISABLED
;
696 sk
->sk_state
= TCP_SYN_RECV
;
697 sk
->sk_backlog_rcv
= pipe_do_rcv
;
698 sk
->sk_destruct
= pipe_destruct
;
700 pn
->rx_fc
= negotiated_rx_fc
;
701 pn
->tx_fc
= negotiated_tx_fc
;
702 sk
->sk_state_change(sk
);
704 ret
= pipe_handler_send_created_ind(sk
,
705 PNS_PIPE_CREATED_IND_UTID
,
713 static int pep_connreq_rcv(struct sock
*sk
, struct sk_buff
*skb
)
716 struct pep_sock
*newpn
, *pn
= pep_sk(sk
);
717 struct pnpipehdr
*hdr
;
718 struct sockaddr_pn dst
, src
;
720 u8 pipe_handle
, enabled
, n_sb
;
723 if (!pskb_pull(skb
, sizeof(*hdr
) + 4))
727 pipe_handle
= hdr
->pipe_handle
;
728 switch (hdr
->state_after_connect
) {
729 case PN_PIPE_DISABLE
:
736 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_INVALID_PARAM
);
739 peer_type
= hdr
->other_pep_type
<< 8;
741 if (unlikely(sk
->sk_state
!= TCP_LISTEN
) || sk_acceptq_is_full(sk
)) {
742 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
746 /* Parse sub-blocks (options) */
749 u8 type
, buf
[1], len
= sizeof(buf
);
750 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
755 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE
:
758 peer_type
= (peer_type
& 0xff00) | data
[0];
760 case PN_PIPE_SB_ALIGNED_DATA
:
761 aligned
= data
[0] != 0;
767 skb
= skb_clone(skb
, GFP_ATOMIC
);
771 /* Create a new to-be-accepted sock */
772 newsk
= sk_alloc(sock_net(sk
), PF_PHONET
, GFP_ATOMIC
, sk
->sk_prot
);
777 sock_init_data(NULL
, newsk
);
778 newsk
->sk_state
= TCP_SYN_RECV
;
779 newsk
->sk_backlog_rcv
= pipe_do_rcv
;
780 newsk
->sk_protocol
= sk
->sk_protocol
;
781 newsk
->sk_destruct
= pipe_destruct
;
783 newpn
= pep_sk(newsk
);
784 pn_skb_get_dst_sockaddr(skb
, &dst
);
785 pn_skb_get_src_sockaddr(skb
, &src
);
786 newpn
->pn_sk
.sobject
= pn_sockaddr_get_object(&dst
);
787 newpn
->pn_sk
.dobject
= pn_sockaddr_get_object(&src
);
788 newpn
->pn_sk
.resource
= pn_sockaddr_get_resource(&dst
);
789 skb_queue_head_init(&newpn
->ctrlreq_queue
);
790 newpn
->pipe_handle
= pipe_handle
;
791 atomic_set(&newpn
->tx_credits
, 0);
792 newpn
->peer_type
= peer_type
;
793 newpn
->rx_credits
= 0;
794 newpn
->rx_fc
= newpn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
795 newpn
->init_enable
= enabled
;
796 newpn
->aligned
= aligned
;
798 BUG_ON(!skb_queue_empty(&newsk
->sk_receive_queue
));
799 skb_queue_head(&newsk
->sk_receive_queue
, skb
);
800 if (!sock_flag(sk
, SOCK_DEAD
))
801 sk
->sk_data_ready(sk
, 0);
803 sk_acceptq_added(sk
);
804 sk_add_node(newsk
, &pn
->ackq
);
808 /* Listening sock must be locked */
809 static struct sock
*pep_find_pipe(const struct hlist_head
*hlist
,
810 const struct sockaddr_pn
*dst
,
813 struct hlist_node
*node
;
815 u16 dobj
= pn_sockaddr_get_object(dst
);
817 sk_for_each(sknode
, node
, hlist
) {
818 struct pep_sock
*pnnode
= pep_sk(sknode
);
820 /* Ports match, but addresses might not: */
821 if (pnnode
->pn_sk
.sobject
!= dobj
)
823 if (pnnode
->pipe_handle
!= pipe_handle
)
825 if (sknode
->sk_state
== TCP_CLOSE_WAIT
)
835 * Deliver an skb to a listening sock.
836 * Socket lock must be held.
837 * We then queue the skb to the right connected sock (if any).
839 static int pep_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
841 struct pep_sock
*pn
= pep_sk(sk
);
843 struct pnpipehdr
*hdr
;
844 struct sockaddr_pn dst
;
845 int err
= NET_RX_SUCCESS
;
848 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
852 pipe_handle
= hdr
->pipe_handle
;
853 if (pipe_handle
== PN_PIPE_INVALID_HANDLE
)
856 pn_skb_get_dst_sockaddr(skb
, &dst
);
858 /* Look for an existing pipe handle */
859 sknode
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
861 return sk_receive_skb(sknode
, skb
, 1);
863 /* Look for a pipe handle pending accept */
864 sknode
= pep_find_pipe(&pn
->ackq
, &dst
, pipe_handle
);
868 printk(KERN_WARNING
"Phonet unconnected PEP ignored");
873 switch (hdr
->message_id
) {
874 case PNS_PEP_CONNECT_REQ
:
875 err
= pep_connreq_rcv(sk
, skb
);
878 #ifdef CONFIG_PHONET_PIPECTRLR
879 case PNS_PEP_CONNECT_RESP
:
880 err
= pep_connresp_rcv(sk
, skb
);
884 case PNS_PEP_DISCONNECT_REQ
:
885 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
888 case PNS_PEP_CTRL_REQ
:
889 pep_ctrlreq_error(sk
, skb
, PN_PIPE_INVALID_HANDLE
, GFP_ATOMIC
);
892 case PNS_PEP_RESET_REQ
:
893 case PNS_PEP_ENABLE_REQ
:
894 case PNS_PEP_DISABLE_REQ
:
895 /* invalid handle is not even allowed here! */
904 static int pipe_do_remove(struct sock
*sk
)
906 struct pep_sock
*pn
= pep_sk(sk
);
907 struct pnpipehdr
*ph
;
910 skb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_KERNEL
);
914 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
915 __skb_push(skb
, sizeof(*ph
));
916 skb_reset_transport_header(skb
);
919 ph
->message_id
= PNS_PIPE_REMOVE_REQ
;
920 ph
->pipe_handle
= pn
->pipe_handle
;
923 return pn_skb_send(sk
, skb
, NULL
);
926 /* associated socket ceases to exist */
927 static void pep_sock_close(struct sock
*sk
, long timeout
)
929 struct pep_sock
*pn
= pep_sk(sk
);
932 sock_hold(sk
); /* keep a reference after sk_common_release() */
933 sk_common_release(sk
);
936 if (sk
->sk_state
== TCP_LISTEN
) {
937 /* Destroy the listen queue */
939 struct hlist_node
*p
, *n
;
941 sk_for_each_safe(sknode
, p
, n
, &pn
->ackq
)
942 sk_del_node_init(sknode
);
943 sk
->sk_state
= TCP_CLOSE
;
944 } else if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
))
945 /* Forcefully remove dangling Phonet pipe */
948 #ifdef CONFIG_PHONET_PIPECTRLR
949 if (pn
->pipe_state
!= PIPE_IDLE
) {
950 /* send pep disconnect request */
951 pipe_handler_send_req(sk
,
952 PNS_PEP_DISCONNECT_UTID
, PNS_PEP_DISCONNECT_REQ
,
955 pn
->pipe_state
= PIPE_IDLE
;
956 sk
->sk_state
= TCP_CLOSE
;
960 ifindex
= pn
->ifindex
;
969 static int pep_wait_connreq(struct sock
*sk
, int noblock
)
971 struct task_struct
*tsk
= current
;
972 struct pep_sock
*pn
= pep_sk(sk
);
973 long timeo
= sock_rcvtimeo(sk
, noblock
);
978 if (sk
->sk_state
!= TCP_LISTEN
)
980 if (!hlist_empty(&pn
->ackq
))
984 if (signal_pending(tsk
))
985 return sock_intr_errno(timeo
);
987 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
990 timeo
= schedule_timeout(timeo
);
992 finish_wait(sk_sleep(sk
), &wait
);
998 static struct sock
*pep_sock_accept(struct sock
*sk
, int flags
, int *errp
)
1000 struct pep_sock
*pn
= pep_sk(sk
);
1001 struct sock
*newsk
= NULL
;
1002 struct sk_buff
*oskb
;
1006 err
= pep_wait_connreq(sk
, flags
& O_NONBLOCK
);
1010 newsk
= __sk_head(&pn
->ackq
);
1012 oskb
= skb_dequeue(&newsk
->sk_receive_queue
);
1013 err
= pep_accept_conn(newsk
, oskb
);
1015 skb_queue_head(&newsk
->sk_receive_queue
, oskb
);
1022 pep_sk(newsk
)->listener
= sk
;
1025 sk_del_node_init(newsk
);
1026 sk_acceptq_removed(sk
);
1027 sk_add_node(newsk
, &pn
->hlist
);
1036 #ifdef CONFIG_PHONET_PIPECTRLR
1037 static int pep_sock_connect(struct sock
*sk
, struct sockaddr
*addr
, int len
)
1039 struct pep_sock
*pn
= pep_sk(sk
);
1040 const struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
1042 pn
->pn_sk
.dobject
= pn_sockaddr_get_object(spn
);
1043 pn
->pn_sk
.resource
= pn_sockaddr_get_resource(spn
);
1044 return pipe_handler_send_req(sk
,
1045 PNS_PEP_CONNECT_UTID
, PNS_PEP_CONNECT_REQ
,
1050 static int pep_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
1052 struct pep_sock
*pn
= pep_sk(sk
);
1057 if (sk
->sk_state
== TCP_LISTEN
)
1061 if (sock_flag(sk
, SOCK_URGINLINE
) &&
1062 !skb_queue_empty(&pn
->ctrlreq_queue
))
1063 answ
= skb_peek(&pn
->ctrlreq_queue
)->len
;
1064 else if (!skb_queue_empty(&sk
->sk_receive_queue
))
1065 answ
= skb_peek(&sk
->sk_receive_queue
)->len
;
1069 return put_user(answ
, (int __user
*)arg
);
1072 return -ENOIOCTLCMD
;
1075 static int pep_init(struct sock
*sk
)
1077 struct pep_sock
*pn
= pep_sk(sk
);
1079 INIT_HLIST_HEAD(&pn
->ackq
);
1080 INIT_HLIST_HEAD(&pn
->hlist
);
1081 skb_queue_head_init(&pn
->ctrlreq_queue
);
1082 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
1086 static int pep_setsockopt(struct sock
*sk
, int level
, int optname
,
1087 char __user
*optval
, unsigned int optlen
)
1089 struct pep_sock
*pn
= pep_sk(sk
);
1090 int val
= 0, err
= 0;
1092 if (level
!= SOL_PNPIPE
)
1093 return -ENOPROTOOPT
;
1094 if (optlen
>= sizeof(int)) {
1095 if (get_user(val
, (int __user
*) optval
))
1101 #ifdef CONFIG_PHONET_PIPECTRLR
1102 case PNPIPE_PIPE_HANDLE
:
1104 if (pn
->pipe_state
> PIPE_IDLE
) {
1108 pn
->pipe_handle
= val
;
1114 if (val
&& val
!= PNPIPE_ENCAP_IP
) {
1118 if (!pn
->ifindex
== !val
)
1119 break; /* Nothing to do! */
1120 if (!capable(CAP_NET_ADMIN
)) {
1126 err
= gprs_attach(sk
);
1139 #ifdef CONFIG_PHONET_PIPECTRLR
1141 if (pn
->pipe_state
<= PIPE_IDLE
) {
1145 err
= pipe_handler_enable_pipe(sk
, val
);
1158 static int pep_getsockopt(struct sock
*sk
, int level
, int optname
,
1159 char __user
*optval
, int __user
*optlen
)
1161 struct pep_sock
*pn
= pep_sk(sk
);
1164 if (level
!= SOL_PNPIPE
)
1165 return -ENOPROTOOPT
;
1166 if (get_user(len
, optlen
))
1171 val
= pn
->ifindex
? PNPIPE_ENCAP_IP
: PNPIPE_ENCAP_NONE
;
1174 case PNPIPE_IFINDEX
:
1178 #ifdef CONFIG_PHONET_PIPECTRLR
1180 if (pn
->pipe_state
<= PIPE_IDLE
)
1182 val
= pn
->pipe_state
!= PIPE_DISABLED
;
1187 return -ENOPROTOOPT
;
1190 len
= min_t(unsigned int, sizeof(int), len
);
1191 if (put_user(len
, optlen
))
1193 if (put_user(val
, (int __user
*) optval
))
1198 static int pipe_skb_send(struct sock
*sk
, struct sk_buff
*skb
)
1200 struct pep_sock
*pn
= pep_sk(sk
);
1201 struct pnpipehdr
*ph
;
1204 if (pn_flow_safe(pn
->tx_fc
) &&
1205 !atomic_add_unless(&pn
->tx_credits
, -1, 0)) {
1210 skb_push(skb
, 3 + pn
->aligned
);
1211 skb_reset_transport_header(skb
);
1215 ph
->message_id
= PNS_PIPE_ALIGNED_DATA
;
1216 ph
->data
[0] = 0; /* padding */
1218 ph
->message_id
= PNS_PIPE_DATA
;
1219 ph
->pipe_handle
= pn
->pipe_handle
;
1220 err
= pn_skb_send(sk
, skb
, NULL
);
1222 if (err
&& pn_flow_safe(pn
->tx_fc
))
1223 atomic_inc(&pn
->tx_credits
);
1228 static int pep_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1229 struct msghdr
*msg
, size_t len
)
1231 struct pep_sock
*pn
= pep_sk(sk
);
1232 struct sk_buff
*skb
;
1234 int flags
= msg
->msg_flags
;
1237 if ((msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|
1238 MSG_CMSG_COMPAT
)) ||
1239 !(msg
->msg_flags
& MSG_EOR
))
1242 skb
= sock_alloc_send_skb(sk
, MAX_PNPIPE_HEADER
+ len
,
1243 flags
& MSG_DONTWAIT
, &err
);
1247 skb_reserve(skb
, MAX_PHONET_HEADER
+ 3);
1248 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
1253 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
1254 if ((1 << sk
->sk_state
) & (TCPF_LISTEN
|TCPF_CLOSE
)) {
1258 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1259 /* Wait until the pipe gets to enabled state */
1261 err
= sk_stream_wait_connect(sk
, &timeo
);
1265 if (sk
->sk_state
== TCP_CLOSE_WAIT
) {
1270 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
1272 /* Wait until flow control allows TX */
1273 done
= atomic_read(&pn
->tx_credits
);
1281 if (signal_pending(current
)) {
1282 err
= sock_intr_errno(timeo
);
1286 prepare_to_wait(sk_sleep(sk
), &wait
,
1287 TASK_INTERRUPTIBLE
);
1288 done
= sk_wait_event(sk
, &timeo
, atomic_read(&pn
->tx_credits
));
1289 finish_wait(sk_sleep(sk
), &wait
);
1291 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1295 err
= pipe_skb_send(sk
, skb
);
1297 err
= len
; /* success! */
1306 int pep_writeable(struct sock
*sk
)
1308 struct pep_sock
*pn
= pep_sk(sk
);
1310 return atomic_read(&pn
->tx_credits
);
1313 int pep_write(struct sock
*sk
, struct sk_buff
*skb
)
1315 struct sk_buff
*rskb
, *fs
;
1318 if (pep_sk(sk
)->aligned
)
1319 return pipe_skb_send(sk
, skb
);
1321 rskb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
1326 skb_shinfo(rskb
)->frag_list
= skb
;
1327 rskb
->len
+= skb
->len
;
1328 rskb
->data_len
+= rskb
->len
;
1329 rskb
->truesize
+= rskb
->len
;
1331 /* Avoid nested fragments */
1332 skb_walk_frags(skb
, fs
)
1334 skb
->next
= skb_shinfo(skb
)->frag_list
;
1335 skb_frag_list_init(skb
);
1337 skb
->data_len
-= flen
;
1338 skb
->truesize
-= flen
;
1340 skb_reserve(rskb
, MAX_PHONET_HEADER
+ 3);
1341 return pipe_skb_send(sk
, rskb
);
1344 struct sk_buff
*pep_read(struct sock
*sk
)
1346 struct sk_buff
*skb
= skb_dequeue(&sk
->sk_receive_queue
);
1348 if (sk
->sk_state
== TCP_ESTABLISHED
)
1349 pipe_grant_credits(sk
);
1353 static int pep_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1354 struct msghdr
*msg
, size_t len
, int noblock
,
1355 int flags
, int *addr_len
)
1357 struct sk_buff
*skb
;
1360 if (flags
& ~(MSG_OOB
|MSG_PEEK
|MSG_TRUNC
|MSG_DONTWAIT
|MSG_WAITALL
|
1361 MSG_NOSIGNAL
|MSG_CMSG_COMPAT
))
1364 if (unlikely(1 << sk
->sk_state
& (TCPF_LISTEN
| TCPF_CLOSE
)))
1367 if ((flags
& MSG_OOB
) || sock_flag(sk
, SOCK_URGINLINE
)) {
1368 /* Dequeue and acknowledge control request */
1369 struct pep_sock
*pn
= pep_sk(sk
);
1371 if (flags
& MSG_PEEK
)
1373 skb
= skb_dequeue(&pn
->ctrlreq_queue
);
1375 pep_ctrlreq_error(sk
, skb
, PN_PIPE_NO_ERROR
,
1377 msg
->msg_flags
|= MSG_OOB
;
1380 if (flags
& MSG_OOB
)
1384 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1387 if (err
== -ENOTCONN
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
1393 if (sk
->sk_state
== TCP_ESTABLISHED
)
1394 pipe_grant_credits(sk
);
1397 msg
->msg_flags
|= MSG_EOR
;
1399 msg
->msg_flags
|= MSG_TRUNC
;
1403 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
1405 err
= (flags
& MSG_TRUNC
) ? skb
->len
: len
;
1407 skb_free_datagram(sk
, skb
);
1411 static void pep_sock_unhash(struct sock
*sk
)
1413 struct pep_sock
*pn
= pep_sk(sk
);
1414 struct sock
*skparent
= NULL
;
1418 #ifndef CONFIG_PHONET_PIPECTRLR
1419 if ((1 << sk
->sk_state
) & ~(TCPF_CLOSE
|TCPF_LISTEN
)) {
1420 skparent
= pn
->listener
;
1423 pn
= pep_sk(skparent
);
1424 lock_sock(skparent
);
1425 sk_del_node_init(sk
);
1429 /* Unhash a listening sock only when it is closed
1430 * and all of its active connected pipes are closed. */
1431 if (hlist_empty(&pn
->hlist
))
1432 pn_sock_unhash(&pn
->pn_sk
.sk
);
1439 static struct proto pep_proto
= {
1440 .close
= pep_sock_close
,
1441 .accept
= pep_sock_accept
,
1442 #ifdef CONFIG_PHONET_PIPECTRLR
1443 .connect
= pep_sock_connect
,
1447 .setsockopt
= pep_setsockopt
,
1448 .getsockopt
= pep_getsockopt
,
1449 .sendmsg
= pep_sendmsg
,
1450 .recvmsg
= pep_recvmsg
,
1451 .backlog_rcv
= pep_do_rcv
,
1452 .hash
= pn_sock_hash
,
1453 .unhash
= pep_sock_unhash
,
1454 .get_port
= pn_sock_get_port
,
1455 .obj_size
= sizeof(struct pep_sock
),
1456 .owner
= THIS_MODULE
,
1460 static struct phonet_protocol pep_pn_proto
= {
1461 .ops
= &phonet_stream_ops
,
1463 .sock_type
= SOCK_SEQPACKET
,
1466 static int __init
pep_register(void)
1468 return phonet_proto_register(PN_PROTO_PIPE
, &pep_pn_proto
);
1471 static void __exit
pep_unregister(void)
1473 phonet_proto_unregister(PN_PROTO_PIPE
, &pep_pn_proto
);
1476 module_init(pep_register
);
1477 module_exit(pep_unregister
);
1478 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1479 MODULE_DESCRIPTION("Phonet pipe protocol");
1480 MODULE_LICENSE("GPL");
1481 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET
, PN_PROTO_PIPE
);