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
);
113 #ifdef CONFIG_PHONET_PIPECTRLR
114 static u8
pipe_negotiate_fc(u8
*host_fc
, u8
*remote_fc
, int len
)
117 u8 base_fc
, final_fc
;
119 for (i
= 0; i
< len
; i
++) {
120 base_fc
= host_fc
[i
];
121 for (j
= 0; j
< len
; j
++) {
122 if (remote_fc
[j
] == base_fc
) {
135 static int pipe_get_flow_info(struct sock
*sk
, struct sk_buff
*skb
,
136 u8
*pref_rx_fc
, u8
*req_tx_fc
)
138 struct pnpipehdr
*hdr
;
141 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
147 __skb_pull(skb
, sizeof(*hdr
) + 4);
149 u8 type
, buf
[3], len
= sizeof(buf
);
150 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
156 case PN_PIPE_SB_REQUIRED_FC_TX
:
157 if (len
< 3 || (data
[2] | data
[3] | data
[4]) > 3)
159 req_tx_fc
[0] = data
[2];
160 req_tx_fc
[1] = data
[3];
161 req_tx_fc
[2] = data
[4];
164 case PN_PIPE_SB_PREFERRED_FC_RX
:
165 if (len
< 3 || (data
[2] | data
[3] | data
[4]) > 3)
167 pref_rx_fc
[0] = data
[2];
168 pref_rx_fc
[1] = data
[3];
169 pref_rx_fc
[2] = data
[4];
178 static int pipe_handler_send_req(struct sock
*sk
, u8 utid
,
179 u8 msg_id
, gfp_t priority
)
182 struct pnpipehdr
*ph
;
184 struct pep_sock
*pn
= pep_sk(sk
);
186 static const u8 data
[4] = {
191 case PNS_PEP_CONNECT_REQ
:
195 case PNS_PEP_DISCONNECT_REQ
:
196 case PNS_PEP_ENABLE_REQ
:
197 case PNS_PEP_DISABLE_REQ
:
205 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ len
, priority
);
208 skb_set_owner_w(skb
, sk
);
210 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
213 skb_copy_to_linear_data(skb
, data
, len
);
215 __skb_push(skb
, sizeof(*ph
));
216 skb_reset_transport_header(skb
);
219 ph
->message_id
= msg_id
;
220 ph
->pipe_handle
= pn
->pipe_handle
;
221 ph
->error_code
= PN_PIPE_NO_ERROR
;
223 return pn_skb_send(sk
, skb
, &pn
->remote_pep
);
226 static int pipe_handler_send_created_ind(struct sock
*sk
,
230 struct pnpipehdr
*ph
;
233 struct pep_sock
*pn
= pep_sk(sk
);
234 static u8 data
[4] = {
241 * actually, below is number of sub-blocks and not error code.
242 * Pipe_created_ind message format does not have any
243 * error code field. However, the Phonet stack will always send
244 * an error code as part of pnpipehdr. So, use that err_code to
245 * specify the number of sub-blocks.
249 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ sizeof(data
), GFP_ATOMIC
);
252 skb_set_owner_w(skb
, sk
);
254 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
255 __skb_put(skb
, sizeof(data
));
256 skb_copy_to_linear_data(skb
, data
, sizeof(data
));
257 __skb_push(skb
, sizeof(*ph
));
258 skb_reset_transport_header(skb
);
261 ph
->message_id
= msg_id
;
262 ph
->pipe_handle
= pn
->pipe_handle
;
263 ph
->error_code
= err_code
;
265 return pn_skb_send(sk
, skb
, &pn
->remote_pep
);
268 static int pipe_handler_send_ind(struct sock
*sk
, u8 utid
, u8 msg_id
)
271 struct pnpipehdr
*ph
;
273 struct pep_sock
*pn
= pep_sk(sk
);
276 * actually, below is a filler.
277 * Pipe_enabled/disabled_ind message format does not have any
278 * error code field. However, the Phonet stack will always send
279 * an error code as part of pnpipehdr. So, use that err_code to
280 * specify the filler value.
284 skb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
287 skb_set_owner_w(skb
, sk
);
289 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
290 __skb_push(skb
, sizeof(*ph
));
291 skb_reset_transport_header(skb
);
294 ph
->message_id
= msg_id
;
295 ph
->pipe_handle
= pn
->pipe_handle
;
296 ph
->error_code
= err_code
;
298 return pn_skb_send(sk
, skb
, &pn
->remote_pep
);
301 static int pipe_handler_enable_pipe(struct sock
*sk
, int enable
)
306 utid
= PNS_PIPE_ENABLE_UTID
;
307 req
= PNS_PEP_ENABLE_REQ
;
309 utid
= PNS_PIPE_DISABLE_UTID
;
310 req
= PNS_PEP_DISABLE_REQ
;
312 return pipe_handler_send_req(sk
, utid
, req
, GFP_ATOMIC
);
316 static int pep_accept_conn(struct sock
*sk
, struct sk_buff
*skb
)
318 static const u8 data
[20] = {
319 PAD
, PAD
, PAD
, 2 /* sub-blocks */,
320 PN_PIPE_SB_REQUIRED_FC_TX
, pep_sb_size(5), 3, PAD
,
321 PN_MULTI_CREDIT_FLOW_CONTROL
,
322 PN_ONE_CREDIT_FLOW_CONTROL
,
323 PN_LEGACY_FLOW_CONTROL
,
325 PN_PIPE_SB_PREFERRED_FC_RX
, pep_sb_size(5), 3, PAD
,
326 PN_MULTI_CREDIT_FLOW_CONTROL
,
327 PN_ONE_CREDIT_FLOW_CONTROL
,
328 PN_LEGACY_FLOW_CONTROL
,
333 return pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, data
, sizeof(data
),
337 static int pep_reject_conn(struct sock
*sk
, struct sk_buff
*skb
, u8 code
)
339 static const u8 data
[4] = { PAD
, PAD
, PAD
, 0 /* sub-blocks */ };
340 WARN_ON(code
== PN_PIPE_NO_ERROR
);
341 return pep_reply(sk
, skb
, code
, data
, sizeof(data
), GFP_ATOMIC
);
344 /* Control requests are not sent by the pipe service and have a specific
346 static int pep_ctrlreq_error(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
349 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
351 struct pnpipehdr
*ph
;
352 struct sockaddr_pn dst
;
354 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
357 skb_set_owner_w(skb
, sk
);
359 skb_reserve(skb
, MAX_PHONET_HEADER
);
360 ph
= (struct pnpipehdr
*)skb_put(skb
, sizeof(*ph
) + 4);
362 ph
->utid
= oph
->utid
;
363 ph
->message_id
= PNS_PEP_CTRL_RESP
;
364 ph
->pipe_handle
= oph
->pipe_handle
;
365 ph
->data
[0] = oph
->data
[1]; /* CTRL id */
366 ph
->data
[1] = oph
->data
[0]; /* PEP type */
367 ph
->data
[2] = code
; /* error code, at an usual offset */
371 pn_skb_get_src_sockaddr(oskb
, &dst
);
372 return pn_skb_send(sk
, skb
, &dst
);
375 static int pipe_snd_status(struct sock
*sk
, u8 type
, u8 status
, gfp_t priority
)
377 struct pep_sock
*pn
= pep_sk(sk
);
378 struct pnpipehdr
*ph
;
381 skb
= alloc_skb(MAX_PNPIPE_HEADER
+ 4, priority
);
384 skb_set_owner_w(skb
, sk
);
386 skb_reserve(skb
, MAX_PNPIPE_HEADER
+ 4);
387 __skb_push(skb
, sizeof(*ph
) + 4);
388 skb_reset_transport_header(skb
);
391 ph
->message_id
= PNS_PEP_STATUS_IND
;
392 ph
->pipe_handle
= pn
->pipe_handle
;
393 ph
->pep_type
= PN_PEP_TYPE_COMMON
;
397 ph
->data
[4] = status
;
399 #ifdef CONFIG_PHONET_PIPECTRLR
400 return pn_skb_send(sk
, skb
, &pn
->remote_pep
);
402 return pn_skb_send(sk
, skb
, &pipe_srv
);
406 /* Send our RX flow control information to the sender.
407 * Socket must be locked. */
408 static void pipe_grant_credits(struct sock
*sk
)
410 struct pep_sock
*pn
= pep_sk(sk
);
412 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
415 case PN_LEGACY_FLOW_CONTROL
: /* TODO */
417 case PN_ONE_CREDIT_FLOW_CONTROL
:
418 pipe_snd_status(sk
, PN_PEP_IND_FLOW_CONTROL
,
419 PEP_IND_READY
, GFP_ATOMIC
);
422 case PN_MULTI_CREDIT_FLOW_CONTROL
:
423 if ((pn
->rx_credits
+ CREDITS_THR
) > CREDITS_MAX
)
425 if (pipe_snd_status(sk
, PN_PEP_IND_ID_MCFC_GRANT_CREDITS
,
426 CREDITS_MAX
- pn
->rx_credits
,
428 pn
->rx_credits
= CREDITS_MAX
;
433 static int pipe_rcv_status(struct sock
*sk
, struct sk_buff
*skb
)
435 struct pep_sock
*pn
= pep_sk(sk
);
436 struct pnpipehdr
*hdr
;
439 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
443 if (hdr
->data
[0] != PN_PEP_TYPE_COMMON
) {
444 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP type: %u\n",
445 (unsigned)hdr
->data
[0]);
449 switch (hdr
->data
[1]) {
450 case PN_PEP_IND_FLOW_CONTROL
:
452 case PN_LEGACY_FLOW_CONTROL
:
453 switch (hdr
->data
[4]) {
455 atomic_set(&pn
->tx_credits
, 0);
458 atomic_set(&pn
->tx_credits
, wake
= 1);
462 case PN_ONE_CREDIT_FLOW_CONTROL
:
463 if (hdr
->data
[4] == PEP_IND_READY
)
464 atomic_set(&pn
->tx_credits
, wake
= 1);
469 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS
:
470 if (pn
->tx_fc
!= PN_MULTI_CREDIT_FLOW_CONTROL
)
472 atomic_add(wake
= hdr
->data
[4], &pn
->tx_credits
);
476 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP indication: %u\n",
477 (unsigned)hdr
->data
[1]);
481 sk
->sk_write_space(sk
);
485 static int pipe_rcv_created(struct sock
*sk
, struct sk_buff
*skb
)
487 struct pep_sock
*pn
= pep_sk(sk
);
488 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
489 u8 n_sb
= hdr
->data
[0];
491 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
492 __skb_pull(skb
, sizeof(*hdr
));
494 u8 type
, buf
[2], len
= sizeof(buf
);
495 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
500 case PN_PIPE_SB_NEGOTIATED_FC
:
501 if (len
< 2 || (data
[0] | data
[1]) > 3)
503 pn
->tx_fc
= data
[0] & 3;
504 pn
->rx_fc
= data
[1] & 3;
512 /* Queue an skb to a connected sock.
513 * Socket lock must be held. */
514 static int pipe_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
516 struct pep_sock
*pn
= pep_sk(sk
);
517 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
518 struct sk_buff_head
*queue
;
521 BUG_ON(sk
->sk_state
== TCP_CLOSE_WAIT
);
523 switch (hdr
->message_id
) {
524 case PNS_PEP_CONNECT_REQ
:
525 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
528 case PNS_PEP_DISCONNECT_REQ
:
529 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
530 sk
->sk_state
= TCP_CLOSE_WAIT
;
531 if (!sock_flag(sk
, SOCK_DEAD
))
532 sk
->sk_state_change(sk
);
535 #ifdef CONFIG_PHONET_PIPECTRLR
536 case PNS_PEP_DISCONNECT_RESP
:
537 pn
->pipe_state
= PIPE_IDLE
;
538 sk
->sk_state
= TCP_CLOSE
;
542 case PNS_PEP_ENABLE_REQ
:
543 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
544 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
547 #ifdef CONFIG_PHONET_PIPECTRLR
548 case PNS_PEP_ENABLE_RESP
:
549 pn
->pipe_state
= PIPE_ENABLED
;
550 pipe_handler_send_ind(sk
, PNS_PIPE_ENABLED_IND_UTID
,
551 PNS_PIPE_ENABLED_IND
);
553 if (!pn_flow_safe(pn
->tx_fc
)) {
554 atomic_set(&pn
->tx_credits
, 1);
555 sk
->sk_write_space(sk
);
557 if (sk
->sk_state
== TCP_ESTABLISHED
)
558 break; /* Nothing to do */
559 sk
->sk_state
= TCP_ESTABLISHED
;
560 pipe_grant_credits(sk
);
564 case PNS_PEP_RESET_REQ
:
565 switch (hdr
->state_after_reset
) {
566 case PN_PIPE_DISABLE
:
572 default: /* not allowed to send an error here!? */
577 case PNS_PEP_DISABLE_REQ
:
578 atomic_set(&pn
->tx_credits
, 0);
579 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
582 #ifdef CONFIG_PHONET_PIPECTRLR
583 case PNS_PEP_DISABLE_RESP
:
584 pn
->pipe_state
= PIPE_DISABLED
;
585 atomic_set(&pn
->tx_credits
, 0);
586 pipe_handler_send_ind(sk
, PNS_PIPE_DISABLED_IND_UTID
,
587 PNS_PIPE_DISABLED_IND
);
588 sk
->sk_state
= TCP_SYN_RECV
;
593 case PNS_PEP_CTRL_REQ
:
594 if (skb_queue_len(&pn
->ctrlreq_queue
) >= PNPIPE_CTRLREQ_MAX
) {
595 atomic_inc(&sk
->sk_drops
);
599 queue
= &pn
->ctrlreq_queue
;
602 case PNS_PIPE_ALIGNED_DATA
:
606 __skb_pull(skb
, 3); /* Pipe data header */
607 if (!pn_flow_safe(pn
->rx_fc
)) {
608 err
= sock_queue_rcv_skb(sk
, skb
);
614 if (pn
->rx_credits
== 0) {
615 atomic_inc(&sk
->sk_drops
);
620 queue
= &sk
->sk_receive_queue
;
623 case PNS_PEP_STATUS_IND
:
624 pipe_rcv_status(sk
, skb
);
627 case PNS_PIPE_REDIRECTED_IND
:
628 err
= pipe_rcv_created(sk
, skb
);
631 case PNS_PIPE_CREATED_IND
:
632 err
= pipe_rcv_created(sk
, skb
);
636 case PNS_PIPE_RESET_IND
:
637 if (!pn
->init_enable
)
640 case PNS_PIPE_ENABLED_IND
:
641 if (!pn_flow_safe(pn
->tx_fc
)) {
642 atomic_set(&pn
->tx_credits
, 1);
643 sk
->sk_write_space(sk
);
645 if (sk
->sk_state
== TCP_ESTABLISHED
)
646 break; /* Nothing to do */
647 sk
->sk_state
= TCP_ESTABLISHED
;
648 pipe_grant_credits(sk
);
651 case PNS_PIPE_DISABLED_IND
:
652 sk
->sk_state
= TCP_SYN_RECV
;
657 LIMIT_NETDEBUG(KERN_DEBUG
"Phonet unknown PEP message: %u\n",
667 skb_set_owner_r(skb
, sk
);
669 skb_queue_tail(queue
, skb
);
670 if (!sock_flag(sk
, SOCK_DEAD
))
671 sk
->sk_data_ready(sk
, err
);
675 /* Destroy connected sock. */
676 static void pipe_destruct(struct sock
*sk
)
678 struct pep_sock
*pn
= pep_sk(sk
);
680 skb_queue_purge(&sk
->sk_receive_queue
);
681 skb_queue_purge(&pn
->ctrlreq_queue
);
684 #ifdef CONFIG_PHONET_PIPECTRLR
685 static int pep_connresp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
687 struct pep_sock
*pn
= pep_sk(sk
);
688 u8 host_pref_rx_fc
[3] = {3, 2, 1}, host_req_tx_fc
[3] = {3, 2, 1};
689 u8 remote_pref_rx_fc
[3], remote_req_tx_fc
[3];
690 u8 negotiated_rx_fc
, negotiated_tx_fc
;
693 pipe_get_flow_info(sk
, skb
, remote_pref_rx_fc
,
695 negotiated_tx_fc
= pipe_negotiate_fc(remote_req_tx_fc
,
697 sizeof(host_pref_rx_fc
));
698 negotiated_rx_fc
= pipe_negotiate_fc(host_req_tx_fc
,
700 sizeof(host_pref_rx_fc
));
702 pn
->pipe_state
= PIPE_DISABLED
;
703 sk
->sk_state
= TCP_SYN_RECV
;
704 sk
->sk_backlog_rcv
= pipe_do_rcv
;
705 sk
->sk_destruct
= pipe_destruct
;
707 pn
->rx_fc
= negotiated_rx_fc
;
708 pn
->tx_fc
= negotiated_tx_fc
;
709 sk
->sk_state_change(sk
);
711 ret
= pipe_handler_send_created_ind(sk
,
712 PNS_PIPE_CREATED_IND_UTID
,
720 static int pep_connreq_rcv(struct sock
*sk
, struct sk_buff
*skb
)
723 struct pep_sock
*newpn
, *pn
= pep_sk(sk
);
724 struct pnpipehdr
*hdr
;
725 struct sockaddr_pn dst
;
727 u8 pipe_handle
, enabled
, n_sb
;
730 if (!pskb_pull(skb
, sizeof(*hdr
) + 4))
734 pipe_handle
= hdr
->pipe_handle
;
735 switch (hdr
->state_after_connect
) {
736 case PN_PIPE_DISABLE
:
743 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_INVALID_PARAM
);
746 peer_type
= hdr
->other_pep_type
<< 8;
748 if (unlikely(sk
->sk_state
!= TCP_LISTEN
) || sk_acceptq_is_full(sk
)) {
749 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
);
753 /* Parse sub-blocks (options) */
756 u8 type
, buf
[1], len
= sizeof(buf
);
757 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
762 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE
:
765 peer_type
= (peer_type
& 0xff00) | data
[0];
767 case PN_PIPE_SB_ALIGNED_DATA
:
768 aligned
= data
[0] != 0;
774 skb
= skb_clone(skb
, GFP_ATOMIC
);
778 /* Create a new to-be-accepted sock */
779 newsk
= sk_alloc(sock_net(sk
), PF_PHONET
, GFP_ATOMIC
, sk
->sk_prot
);
784 sock_init_data(NULL
, newsk
);
785 newsk
->sk_state
= TCP_SYN_RECV
;
786 newsk
->sk_backlog_rcv
= pipe_do_rcv
;
787 newsk
->sk_protocol
= sk
->sk_protocol
;
788 newsk
->sk_destruct
= pipe_destruct
;
790 newpn
= pep_sk(newsk
);
791 pn_skb_get_dst_sockaddr(skb
, &dst
);
792 newpn
->pn_sk
.sobject
= pn_sockaddr_get_object(&dst
);
793 newpn
->pn_sk
.resource
= pn
->pn_sk
.resource
;
794 skb_queue_head_init(&newpn
->ctrlreq_queue
);
795 newpn
->pipe_handle
= pipe_handle
;
796 atomic_set(&newpn
->tx_credits
, 0);
797 newpn
->peer_type
= peer_type
;
798 newpn
->rx_credits
= 0;
799 newpn
->rx_fc
= newpn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
800 newpn
->init_enable
= enabled
;
801 newpn
->aligned
= aligned
;
803 BUG_ON(!skb_queue_empty(&newsk
->sk_receive_queue
));
804 skb_queue_head(&newsk
->sk_receive_queue
, skb
);
805 if (!sock_flag(sk
, SOCK_DEAD
))
806 sk
->sk_data_ready(sk
, 0);
808 sk_acceptq_added(sk
);
809 sk_add_node(newsk
, &pn
->ackq
);
813 /* Listening sock must be locked */
814 static struct sock
*pep_find_pipe(const struct hlist_head
*hlist
,
815 const struct sockaddr_pn
*dst
,
818 struct hlist_node
*node
;
820 u16 dobj
= pn_sockaddr_get_object(dst
);
822 sk_for_each(sknode
, node
, hlist
) {
823 struct pep_sock
*pnnode
= pep_sk(sknode
);
825 /* Ports match, but addresses might not: */
826 if (pnnode
->pn_sk
.sobject
!= dobj
)
828 if (pnnode
->pipe_handle
!= pipe_handle
)
830 if (sknode
->sk_state
== TCP_CLOSE_WAIT
)
840 * Deliver an skb to a listening sock.
841 * Socket lock must be held.
842 * We then queue the skb to the right connected sock (if any).
844 static int pep_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
846 struct pep_sock
*pn
= pep_sk(sk
);
848 struct pnpipehdr
*hdr
;
849 struct sockaddr_pn dst
;
850 int err
= NET_RX_SUCCESS
;
853 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
857 pipe_handle
= hdr
->pipe_handle
;
858 if (pipe_handle
== PN_PIPE_INVALID_HANDLE
)
861 pn_skb_get_dst_sockaddr(skb
, &dst
);
863 /* Look for an existing pipe handle */
864 sknode
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
866 return sk_receive_skb(sknode
, skb
, 1);
868 /* Look for a pipe handle pending accept */
869 sknode
= pep_find_pipe(&pn
->ackq
, &dst
, pipe_handle
);
873 printk(KERN_WARNING
"Phonet unconnected PEP ignored");
878 switch (hdr
->message_id
) {
879 case PNS_PEP_CONNECT_REQ
:
880 err
= pep_connreq_rcv(sk
, skb
);
883 #ifdef CONFIG_PHONET_PIPECTRLR
884 case PNS_PEP_CONNECT_RESP
:
885 err
= pep_connresp_rcv(sk
, skb
);
889 case PNS_PEP_DISCONNECT_REQ
:
890 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
893 case PNS_PEP_CTRL_REQ
:
894 pep_ctrlreq_error(sk
, skb
, PN_PIPE_INVALID_HANDLE
, GFP_ATOMIC
);
897 case PNS_PEP_RESET_REQ
:
898 case PNS_PEP_ENABLE_REQ
:
899 case PNS_PEP_DISABLE_REQ
:
900 /* invalid handle is not even allowed here! */
909 static int pipe_do_remove(struct sock
*sk
)
911 struct pep_sock
*pn
= pep_sk(sk
);
912 struct pnpipehdr
*ph
;
915 skb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_KERNEL
);
919 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
920 __skb_push(skb
, sizeof(*ph
));
921 skb_reset_transport_header(skb
);
924 ph
->message_id
= PNS_PIPE_REMOVE_REQ
;
925 ph
->pipe_handle
= pn
->pipe_handle
;
928 return pn_skb_send(sk
, skb
, &pipe_srv
);
931 /* associated socket ceases to exist */
932 static void pep_sock_close(struct sock
*sk
, long timeout
)
934 struct pep_sock
*pn
= pep_sk(sk
);
937 sock_hold(sk
); /* keep a reference after sk_common_release() */
938 sk_common_release(sk
);
941 if (sk
->sk_state
== TCP_LISTEN
) {
942 /* Destroy the listen queue */
944 struct hlist_node
*p
, *n
;
946 sk_for_each_safe(sknode
, p
, n
, &pn
->ackq
)
947 sk_del_node_init(sknode
);
948 sk
->sk_state
= TCP_CLOSE
;
949 } else if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
))
950 /* Forcefully remove dangling Phonet pipe */
953 #ifdef CONFIG_PHONET_PIPECTRLR
954 if (pn
->pipe_state
!= PIPE_IDLE
) {
955 /* send pep disconnect request */
956 pipe_handler_send_req(sk
,
957 PNS_PEP_DISCONNECT_UTID
, PNS_PEP_DISCONNECT_REQ
,
960 pn
->pipe_state
= PIPE_IDLE
;
961 sk
->sk_state
= TCP_CLOSE
;
965 ifindex
= pn
->ifindex
;
974 static int pep_wait_connreq(struct sock
*sk
, int noblock
)
976 struct task_struct
*tsk
= current
;
977 struct pep_sock
*pn
= pep_sk(sk
);
978 long timeo
= sock_rcvtimeo(sk
, noblock
);
983 if (sk
->sk_state
!= TCP_LISTEN
)
985 if (!hlist_empty(&pn
->ackq
))
989 if (signal_pending(tsk
))
990 return sock_intr_errno(timeo
);
992 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
995 timeo
= schedule_timeout(timeo
);
997 finish_wait(sk_sleep(sk
), &wait
);
1003 static struct sock
*pep_sock_accept(struct sock
*sk
, int flags
, int *errp
)
1005 struct pep_sock
*pn
= pep_sk(sk
);
1006 struct sock
*newsk
= NULL
;
1007 struct sk_buff
*oskb
;
1011 err
= pep_wait_connreq(sk
, flags
& O_NONBLOCK
);
1015 newsk
= __sk_head(&pn
->ackq
);
1017 oskb
= skb_dequeue(&newsk
->sk_receive_queue
);
1018 err
= pep_accept_conn(newsk
, oskb
);
1020 skb_queue_head(&newsk
->sk_receive_queue
, oskb
);
1027 pep_sk(newsk
)->listener
= sk
;
1030 sk_del_node_init(newsk
);
1031 sk_acceptq_removed(sk
);
1032 sk_add_node(newsk
, &pn
->hlist
);
1041 #ifdef CONFIG_PHONET_PIPECTRLR
1042 static int pep_sock_connect(struct sock
*sk
, struct sockaddr
*addr
, int len
)
1044 struct pep_sock
*pn
= pep_sk(sk
);
1045 struct sockaddr_pn
*spn
= (struct sockaddr_pn
*)addr
;
1047 memcpy(&pn
->remote_pep
, spn
, sizeof(struct sockaddr_pn
));
1049 return pipe_handler_send_req(sk
,
1050 PNS_PEP_CONNECT_UTID
, PNS_PEP_CONNECT_REQ
,
1055 static int pep_ioctl(struct sock
*sk
, int cmd
, unsigned long arg
)
1057 struct pep_sock
*pn
= pep_sk(sk
);
1062 if (sk
->sk_state
== TCP_LISTEN
)
1066 if (sock_flag(sk
, SOCK_URGINLINE
) &&
1067 !skb_queue_empty(&pn
->ctrlreq_queue
))
1068 answ
= skb_peek(&pn
->ctrlreq_queue
)->len
;
1069 else if (!skb_queue_empty(&sk
->sk_receive_queue
))
1070 answ
= skb_peek(&sk
->sk_receive_queue
)->len
;
1074 return put_user(answ
, (int __user
*)arg
);
1077 return -ENOIOCTLCMD
;
1080 static int pep_init(struct sock
*sk
)
1082 struct pep_sock
*pn
= pep_sk(sk
);
1084 INIT_HLIST_HEAD(&pn
->ackq
);
1085 INIT_HLIST_HEAD(&pn
->hlist
);
1086 skb_queue_head_init(&pn
->ctrlreq_queue
);
1087 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
1091 static int pep_setsockopt(struct sock
*sk
, int level
, int optname
,
1092 char __user
*optval
, unsigned int optlen
)
1094 struct pep_sock
*pn
= pep_sk(sk
);
1095 int val
= 0, err
= 0;
1097 if (level
!= SOL_PNPIPE
)
1098 return -ENOPROTOOPT
;
1099 if (optlen
>= sizeof(int)) {
1100 if (get_user(val
, (int __user
*) optval
))
1106 #ifdef CONFIG_PHONET_PIPECTRLR
1107 case PNPIPE_PIPE_HANDLE
:
1109 if (pn
->pipe_state
> PIPE_IDLE
) {
1113 pn
->pipe_handle
= val
;
1119 if (val
&& val
!= PNPIPE_ENCAP_IP
) {
1123 if (!pn
->ifindex
== !val
)
1124 break; /* Nothing to do! */
1125 if (!capable(CAP_NET_ADMIN
)) {
1131 err
= gprs_attach(sk
);
1144 #ifdef CONFIG_PHONET_PIPECTRLR
1146 if (pn
->pipe_state
<= PIPE_IDLE
) {
1150 err
= pipe_handler_enable_pipe(sk
, val
);
1163 static int pep_getsockopt(struct sock
*sk
, int level
, int optname
,
1164 char __user
*optval
, int __user
*optlen
)
1166 struct pep_sock
*pn
= pep_sk(sk
);
1169 if (level
!= SOL_PNPIPE
)
1170 return -ENOPROTOOPT
;
1171 if (get_user(len
, optlen
))
1176 val
= pn
->ifindex
? PNPIPE_ENCAP_IP
: PNPIPE_ENCAP_NONE
;
1179 case PNPIPE_IFINDEX
:
1183 #ifdef CONFIG_PHONET_PIPECTRLR
1185 if (pn
->pipe_state
<= PIPE_IDLE
)
1187 val
= pn
->pipe_state
!= PIPE_DISABLED
;
1192 return -ENOPROTOOPT
;
1195 len
= min_t(unsigned int, sizeof(int), len
);
1196 if (put_user(len
, optlen
))
1198 if (put_user(val
, (int __user
*) optval
))
1203 static int pipe_skb_send(struct sock
*sk
, struct sk_buff
*skb
)
1205 struct pep_sock
*pn
= pep_sk(sk
);
1206 struct pnpipehdr
*ph
;
1209 if (pn_flow_safe(pn
->tx_fc
) &&
1210 !atomic_add_unless(&pn
->tx_credits
, -1, 0)) {
1215 skb_push(skb
, 3 + pn
->aligned
);
1216 skb_reset_transport_header(skb
);
1220 ph
->message_id
= PNS_PIPE_ALIGNED_DATA
;
1221 ph
->data
[0] = 0; /* padding */
1223 ph
->message_id
= PNS_PIPE_DATA
;
1224 ph
->pipe_handle
= pn
->pipe_handle
;
1225 #ifdef CONFIG_PHONET_PIPECTRLR
1226 err
= pn_skb_send(sk
, skb
, &pn
->remote_pep
);
1228 err
= pn_skb_send(sk
, skb
, &pipe_srv
);
1231 if (err
&& pn_flow_safe(pn
->tx_fc
))
1232 atomic_inc(&pn
->tx_credits
);
1237 static int pep_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
1238 struct msghdr
*msg
, size_t len
)
1240 struct pep_sock
*pn
= pep_sk(sk
);
1241 struct sk_buff
*skb
;
1243 int flags
= msg
->msg_flags
;
1246 if ((msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|
1247 MSG_CMSG_COMPAT
)) ||
1248 !(msg
->msg_flags
& MSG_EOR
))
1251 skb
= sock_alloc_send_skb(sk
, MAX_PNPIPE_HEADER
+ len
,
1252 flags
& MSG_DONTWAIT
, &err
);
1256 skb_reserve(skb
, MAX_PHONET_HEADER
+ 3);
1257 err
= memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
);
1262 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
1263 if ((1 << sk
->sk_state
) & (TCPF_LISTEN
|TCPF_CLOSE
)) {
1267 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1268 /* Wait until the pipe gets to enabled state */
1270 err
= sk_stream_wait_connect(sk
, &timeo
);
1274 if (sk
->sk_state
== TCP_CLOSE_WAIT
) {
1279 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
1281 /* Wait until flow control allows TX */
1282 done
= atomic_read(&pn
->tx_credits
);
1290 if (signal_pending(current
)) {
1291 err
= sock_intr_errno(timeo
);
1295 prepare_to_wait(sk_sleep(sk
), &wait
,
1296 TASK_INTERRUPTIBLE
);
1297 done
= sk_wait_event(sk
, &timeo
, atomic_read(&pn
->tx_credits
));
1298 finish_wait(sk_sleep(sk
), &wait
);
1300 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1304 err
= pipe_skb_send(sk
, skb
);
1306 err
= len
; /* success! */
1315 int pep_writeable(struct sock
*sk
)
1317 struct pep_sock
*pn
= pep_sk(sk
);
1319 return atomic_read(&pn
->tx_credits
);
1322 int pep_write(struct sock
*sk
, struct sk_buff
*skb
)
1324 struct sk_buff
*rskb
, *fs
;
1327 if (pep_sk(sk
)->aligned
)
1328 return pipe_skb_send(sk
, skb
);
1330 rskb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
1335 skb_shinfo(rskb
)->frag_list
= skb
;
1336 rskb
->len
+= skb
->len
;
1337 rskb
->data_len
+= rskb
->len
;
1338 rskb
->truesize
+= rskb
->len
;
1340 /* Avoid nested fragments */
1341 skb_walk_frags(skb
, fs
)
1343 skb
->next
= skb_shinfo(skb
)->frag_list
;
1344 skb_frag_list_init(skb
);
1346 skb
->data_len
-= flen
;
1347 skb
->truesize
-= flen
;
1349 skb_reserve(rskb
, MAX_PHONET_HEADER
+ 3);
1350 return pipe_skb_send(sk
, rskb
);
1353 struct sk_buff
*pep_read(struct sock
*sk
)
1355 struct sk_buff
*skb
= skb_dequeue(&sk
->sk_receive_queue
);
1357 if (sk
->sk_state
== TCP_ESTABLISHED
)
1358 pipe_grant_credits(sk
);
1362 static int pep_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
1363 struct msghdr
*msg
, size_t len
, int noblock
,
1364 int flags
, int *addr_len
)
1366 struct sk_buff
*skb
;
1369 if (flags
& ~(MSG_OOB
|MSG_PEEK
|MSG_TRUNC
|MSG_DONTWAIT
|MSG_WAITALL
|
1370 MSG_NOSIGNAL
|MSG_CMSG_COMPAT
))
1373 if (unlikely(1 << sk
->sk_state
& (TCPF_LISTEN
| TCPF_CLOSE
)))
1376 if ((flags
& MSG_OOB
) || sock_flag(sk
, SOCK_URGINLINE
)) {
1377 /* Dequeue and acknowledge control request */
1378 struct pep_sock
*pn
= pep_sk(sk
);
1380 if (flags
& MSG_PEEK
)
1382 skb
= skb_dequeue(&pn
->ctrlreq_queue
);
1384 pep_ctrlreq_error(sk
, skb
, PN_PIPE_NO_ERROR
,
1386 msg
->msg_flags
|= MSG_OOB
;
1389 if (flags
& MSG_OOB
)
1393 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
1396 if (err
== -ENOTCONN
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
1402 if (sk
->sk_state
== TCP_ESTABLISHED
)
1403 pipe_grant_credits(sk
);
1406 msg
->msg_flags
|= MSG_EOR
;
1408 msg
->msg_flags
|= MSG_TRUNC
;
1412 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, len
);
1414 err
= (flags
& MSG_TRUNC
) ? skb
->len
: len
;
1416 skb_free_datagram(sk
, skb
);
1420 static void pep_sock_unhash(struct sock
*sk
)
1422 struct pep_sock
*pn
= pep_sk(sk
);
1423 struct sock
*skparent
= NULL
;
1427 #ifndef CONFIG_PHONET_PIPECTRLR
1428 if ((1 << sk
->sk_state
) & ~(TCPF_CLOSE
|TCPF_LISTEN
)) {
1429 skparent
= pn
->listener
;
1432 pn
= pep_sk(skparent
);
1433 lock_sock(skparent
);
1434 sk_del_node_init(sk
);
1438 /* Unhash a listening sock only when it is closed
1439 * and all of its active connected pipes are closed. */
1440 if (hlist_empty(&pn
->hlist
))
1441 pn_sock_unhash(&pn
->pn_sk
.sk
);
1448 static struct proto pep_proto
= {
1449 .close
= pep_sock_close
,
1450 .accept
= pep_sock_accept
,
1451 #ifdef CONFIG_PHONET_PIPECTRLR
1452 .connect
= pep_sock_connect
,
1456 .setsockopt
= pep_setsockopt
,
1457 .getsockopt
= pep_getsockopt
,
1458 .sendmsg
= pep_sendmsg
,
1459 .recvmsg
= pep_recvmsg
,
1460 .backlog_rcv
= pep_do_rcv
,
1461 .hash
= pn_sock_hash
,
1462 .unhash
= pep_sock_unhash
,
1463 .get_port
= pn_sock_get_port
,
1464 .obj_size
= sizeof(struct pep_sock
),
1465 .owner
= THIS_MODULE
,
1469 static struct phonet_protocol pep_pn_proto
= {
1470 .ops
= &phonet_stream_ops
,
1472 .sock_type
= SOCK_SEQPACKET
,
1475 static int __init
pep_register(void)
1477 return phonet_proto_register(PN_PROTO_PIPE
, &pep_pn_proto
);
1480 static void __exit
pep_unregister(void)
1482 phonet_proto_unregister(PN_PROTO_PIPE
, &pep_pn_proto
);
1485 module_init(pep_register
);
1486 module_exit(pep_unregister
);
1487 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1488 MODULE_DESCRIPTION("Phonet pipe protocol");
1489 MODULE_LICENSE("GPL");
1490 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET
, PN_PROTO_PIPE
);