1 // SPDX-License-Identifier: GPL-2.0-only
5 * Phonet pipe protocol end point socket
7 * Copyright (C) 2008 Nokia Corporation.
9 * Author: RĂ©mi Denis-Courmont
12 #include <linux/kernel.h>
13 #include <linux/sched/signal.h>
14 #include <linux/slab.h>
15 #include <linux/socket.h>
17 #include <net/tcp_states.h>
18 #include <asm/ioctls.h>
20 #include <linux/phonet.h>
21 #include <linux/module.h>
22 #include <net/phonet/phonet.h>
23 #include <net/phonet/pep.h>
24 #include <net/phonet/gprs.h>
27 * TCP_CLOSE sock not in use yet
28 * TCP_CLOSE_WAIT disconnected pipe
29 * TCP_LISTEN listening pipe endpoint
30 * TCP_SYN_RECV connected pipe in disabled state
31 * TCP_ESTABLISHED connected pipe in enabled state
34 * - sk_state, hlist: sock lock needed
35 * - listener: read only
36 * - pipe_handle: read only
39 #define CREDITS_MAX 10
42 #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
44 /* Get the next TLV sub-block. */
45 static unsigned char *pep_get_sb(struct sk_buff
*skb
, u8
*ptype
, u8
*plen
,
55 ph
= skb_header_pointer(skb
, 0, 2, &h
);
56 if (ph
== NULL
|| ph
->sb_len
< 2 || !pskb_may_pull(skb
, ph
->sb_len
))
62 if (buflen
> ph
->sb_len
)
64 data
= skb_header_pointer(skb
, 2, buflen
, buf
);
65 __skb_pull(skb
, 2 + ph
->sb_len
);
69 static struct sk_buff
*pep_alloc_skb(struct sock
*sk
, const void *payload
,
70 int len
, gfp_t priority
)
72 struct sk_buff
*skb
= alloc_skb(MAX_PNPIPE_HEADER
+ len
, priority
);
75 skb_set_owner_w(skb
, sk
);
77 skb_reserve(skb
, MAX_PNPIPE_HEADER
);
79 skb_copy_to_linear_data(skb
, payload
, len
);
80 __skb_push(skb
, sizeof(struct pnpipehdr
));
81 skb_reset_transport_header(skb
);
85 static int pep_reply(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
86 const void *data
, int len
, gfp_t priority
)
88 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
91 struct sockaddr_pn peer
;
93 skb
= pep_alloc_skb(sk
, data
, len
, priority
);
99 ph
->message_id
= oph
->message_id
+ 1; /* REQ -> RESP */
100 ph
->pipe_handle
= oph
->pipe_handle
;
101 ph
->error_code
= code
;
103 pn_skb_get_src_sockaddr(oskb
, &peer
);
104 return pn_skb_send(sk
, skb
, &peer
);
107 static int pep_indicate(struct sock
*sk
, u8 id
, u8 code
,
108 const void *data
, int len
, gfp_t priority
)
110 struct pep_sock
*pn
= pep_sk(sk
);
111 struct pnpipehdr
*ph
;
114 skb
= pep_alloc_skb(sk
, data
, len
, priority
);
121 ph
->pipe_handle
= pn
->pipe_handle
;
122 ph
->error_code
= code
;
123 return pn_skb_send(sk
, skb
, NULL
);
128 static int pipe_handler_request(struct sock
*sk
, u8 id
, u8 code
,
129 const void *data
, int len
)
131 struct pep_sock
*pn
= pep_sk(sk
);
132 struct pnpipehdr
*ph
;
135 skb
= pep_alloc_skb(sk
, data
, len
, GFP_KERNEL
);
140 ph
->utid
= id
; /* whatever */
142 ph
->pipe_handle
= pn
->pipe_handle
;
143 ph
->error_code
= code
;
144 return pn_skb_send(sk
, skb
, NULL
);
147 static int pipe_handler_send_created_ind(struct sock
*sk
)
149 struct pep_sock
*pn
= pep_sk(sk
);
151 PN_PIPE_SB_NEGOTIATED_FC
, pep_sb_size(2),
152 pn
->tx_fc
, pn
->rx_fc
,
155 return pep_indicate(sk
, PNS_PIPE_CREATED_IND
, 1 /* sub-blocks */,
156 data
, 4, GFP_ATOMIC
);
159 static int pep_accept_conn(struct sock
*sk
, struct sk_buff
*skb
)
161 static const u8 data
[20] = {
162 PAD
, PAD
, PAD
, 2 /* sub-blocks */,
163 PN_PIPE_SB_REQUIRED_FC_TX
, pep_sb_size(5), 3, PAD
,
164 PN_MULTI_CREDIT_FLOW_CONTROL
,
165 PN_ONE_CREDIT_FLOW_CONTROL
,
166 PN_LEGACY_FLOW_CONTROL
,
168 PN_PIPE_SB_PREFERRED_FC_RX
, pep_sb_size(5), 3, PAD
,
169 PN_MULTI_CREDIT_FLOW_CONTROL
,
170 PN_ONE_CREDIT_FLOW_CONTROL
,
171 PN_LEGACY_FLOW_CONTROL
,
176 return pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, data
, sizeof(data
),
180 static int pep_reject_conn(struct sock
*sk
, struct sk_buff
*skb
, u8 code
,
183 static const u8 data
[4] = { PAD
, PAD
, PAD
, 0 /* sub-blocks */ };
184 WARN_ON(code
== PN_PIPE_NO_ERROR
);
185 return pep_reply(sk
, skb
, code
, data
, sizeof(data
), priority
);
188 /* Control requests are not sent by the pipe service and have a specific
190 static int pep_ctrlreq_error(struct sock
*sk
, struct sk_buff
*oskb
, u8 code
,
193 const struct pnpipehdr
*oph
= pnp_hdr(oskb
);
195 struct pnpipehdr
*ph
;
196 struct sockaddr_pn dst
;
198 oph
->pep_type
, /* PEP type */
199 code
, /* error code, at an unusual offset */
203 skb
= pep_alloc_skb(sk
, data
, 4, priority
);
208 ph
->utid
= oph
->utid
;
209 ph
->message_id
= PNS_PEP_CTRL_RESP
;
210 ph
->pipe_handle
= oph
->pipe_handle
;
211 ph
->data0
= oph
->data
[0]; /* CTRL id */
213 pn_skb_get_src_sockaddr(oskb
, &dst
);
214 return pn_skb_send(sk
, skb
, &dst
);
217 static int pipe_snd_status(struct sock
*sk
, u8 type
, u8 status
, gfp_t priority
)
219 u8 data
[4] = { type
, PAD
, PAD
, status
};
221 return pep_indicate(sk
, PNS_PEP_STATUS_IND
, PN_PEP_TYPE_COMMON
,
225 /* Send our RX flow control information to the sender.
226 * Socket must be locked. */
227 static void pipe_grant_credits(struct sock
*sk
, gfp_t priority
)
229 struct pep_sock
*pn
= pep_sk(sk
);
231 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
234 case PN_LEGACY_FLOW_CONTROL
: /* TODO */
236 case PN_ONE_CREDIT_FLOW_CONTROL
:
237 if (pipe_snd_status(sk
, PN_PEP_IND_FLOW_CONTROL
,
238 PEP_IND_READY
, priority
) == 0)
241 case PN_MULTI_CREDIT_FLOW_CONTROL
:
242 if ((pn
->rx_credits
+ CREDITS_THR
) > CREDITS_MAX
)
244 if (pipe_snd_status(sk
, PN_PEP_IND_ID_MCFC_GRANT_CREDITS
,
245 CREDITS_MAX
- pn
->rx_credits
,
247 pn
->rx_credits
= CREDITS_MAX
;
252 static int pipe_rcv_status(struct sock
*sk
, struct sk_buff
*skb
)
254 struct pep_sock
*pn
= pep_sk(sk
);
255 struct pnpipehdr
*hdr
;
258 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
262 if (hdr
->pep_type
!= PN_PEP_TYPE_COMMON
) {
263 net_dbg_ratelimited("Phonet unknown PEP type: %u\n",
264 (unsigned int)hdr
->pep_type
);
268 switch (hdr
->data
[0]) {
269 case PN_PEP_IND_FLOW_CONTROL
:
271 case PN_LEGACY_FLOW_CONTROL
:
272 switch (hdr
->data
[3]) {
274 atomic_set(&pn
->tx_credits
, 0);
277 atomic_set(&pn
->tx_credits
, wake
= 1);
281 case PN_ONE_CREDIT_FLOW_CONTROL
:
282 if (hdr
->data
[3] == PEP_IND_READY
)
283 atomic_set(&pn
->tx_credits
, wake
= 1);
288 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS
:
289 if (pn
->tx_fc
!= PN_MULTI_CREDIT_FLOW_CONTROL
)
291 atomic_add(wake
= hdr
->data
[3], &pn
->tx_credits
);
295 net_dbg_ratelimited("Phonet unknown PEP indication: %u\n",
296 (unsigned int)hdr
->data
[0]);
300 sk
->sk_write_space(sk
);
304 static int pipe_rcv_created(struct sock
*sk
, struct sk_buff
*skb
)
306 struct pep_sock
*pn
= pep_sk(sk
);
307 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
308 u8 n_sb
= hdr
->data0
;
310 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
311 __skb_pull(skb
, sizeof(*hdr
));
313 u8 type
, buf
[2], len
= sizeof(buf
);
314 u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
319 case PN_PIPE_SB_NEGOTIATED_FC
:
320 if (len
< 2 || (data
[0] | data
[1]) > 3)
322 pn
->tx_fc
= data
[0] & 3;
323 pn
->rx_fc
= data
[1] & 3;
331 /* Queue an skb to a connected sock.
332 * Socket lock must be held. */
333 static int pipe_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
335 struct pep_sock
*pn
= pep_sk(sk
);
336 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
337 struct sk_buff_head
*queue
;
340 BUG_ON(sk
->sk_state
== TCP_CLOSE_WAIT
);
342 switch (hdr
->message_id
) {
343 case PNS_PEP_CONNECT_REQ
:
344 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
, GFP_ATOMIC
);
347 case PNS_PEP_DISCONNECT_REQ
:
348 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
349 sk
->sk_state
= TCP_CLOSE_WAIT
;
350 if (!sock_flag(sk
, SOCK_DEAD
))
351 sk
->sk_state_change(sk
);
354 case PNS_PEP_ENABLE_REQ
:
355 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
356 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
359 case PNS_PEP_RESET_REQ
:
360 switch (hdr
->state_after_reset
) {
361 case PN_PIPE_DISABLE
:
367 default: /* not allowed to send an error here!? */
372 case PNS_PEP_DISABLE_REQ
:
373 atomic_set(&pn
->tx_credits
, 0);
374 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
377 case PNS_PEP_CTRL_REQ
:
378 if (skb_queue_len(&pn
->ctrlreq_queue
) >= PNPIPE_CTRLREQ_MAX
) {
379 atomic_inc(&sk
->sk_drops
);
383 queue
= &pn
->ctrlreq_queue
;
386 case PNS_PIPE_ALIGNED_DATA
:
390 __skb_pull(skb
, 3); /* Pipe data header */
391 if (!pn_flow_safe(pn
->rx_fc
)) {
392 err
= sock_queue_rcv_skb(sk
, skb
);
394 return NET_RX_SUCCESS
;
399 if (pn
->rx_credits
== 0) {
400 atomic_inc(&sk
->sk_drops
);
405 queue
= &sk
->sk_receive_queue
;
408 case PNS_PEP_STATUS_IND
:
409 pipe_rcv_status(sk
, skb
);
412 case PNS_PIPE_REDIRECTED_IND
:
413 err
= pipe_rcv_created(sk
, skb
);
416 case PNS_PIPE_CREATED_IND
:
417 err
= pipe_rcv_created(sk
, skb
);
421 case PNS_PIPE_RESET_IND
:
422 if (!pn
->init_enable
)
425 case PNS_PIPE_ENABLED_IND
:
426 if (!pn_flow_safe(pn
->tx_fc
)) {
427 atomic_set(&pn
->tx_credits
, 1);
428 sk
->sk_write_space(sk
);
430 if (sk
->sk_state
== TCP_ESTABLISHED
)
431 break; /* Nothing to do */
432 sk
->sk_state
= TCP_ESTABLISHED
;
433 pipe_grant_credits(sk
, GFP_ATOMIC
);
436 case PNS_PIPE_DISABLED_IND
:
437 sk
->sk_state
= TCP_SYN_RECV
;
442 net_dbg_ratelimited("Phonet unknown PEP message: %u\n",
448 return (err
== -ENOBUFS
) ? NET_RX_DROP
: NET_RX_SUCCESS
;
452 skb_set_owner_r(skb
, sk
);
453 skb_queue_tail(queue
, skb
);
454 if (!sock_flag(sk
, SOCK_DEAD
))
455 sk
->sk_data_ready(sk
);
456 return NET_RX_SUCCESS
;
459 /* Destroy connected sock. */
460 static void pipe_destruct(struct sock
*sk
)
462 struct pep_sock
*pn
= pep_sk(sk
);
464 skb_queue_purge(&sk
->sk_receive_queue
);
465 skb_queue_purge(&pn
->ctrlreq_queue
);
468 static u8
pipe_negotiate_fc(const u8
*fcs
, unsigned int n
)
471 u8 final_fc
= PN_NO_FLOW_CONTROL
;
473 for (i
= 0; i
< n
; i
++) {
476 if (fc
> final_fc
&& fc
< PN_MAX_FLOW_CONTROL
)
482 static int pep_connresp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
484 struct pep_sock
*pn
= pep_sk(sk
);
485 struct pnpipehdr
*hdr
;
488 if (!pskb_pull(skb
, sizeof(*hdr
) + 4))
492 if (hdr
->error_code
!= PN_PIPE_NO_ERROR
)
493 return -ECONNREFUSED
;
495 /* Parse sub-blocks */
498 u8 type
, buf
[6], len
= sizeof(buf
);
499 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
505 case PN_PIPE_SB_REQUIRED_FC_TX
:
506 if (len
< 2 || len
< data
[0])
508 pn
->tx_fc
= pipe_negotiate_fc(data
+ 2, len
- 2);
511 case PN_PIPE_SB_PREFERRED_FC_RX
:
512 if (len
< 2 || len
< data
[0])
514 pn
->rx_fc
= pipe_negotiate_fc(data
+ 2, len
- 2);
521 return pipe_handler_send_created_ind(sk
);
524 static int pep_enableresp_rcv(struct sock
*sk
, struct sk_buff
*skb
)
526 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
528 if (hdr
->error_code
!= PN_PIPE_NO_ERROR
)
529 return -ECONNREFUSED
;
531 return pep_indicate(sk
, PNS_PIPE_ENABLED_IND
, 0 /* sub-blocks */,
532 NULL
, 0, GFP_ATOMIC
);
536 static void pipe_start_flow_control(struct sock
*sk
)
538 struct pep_sock
*pn
= pep_sk(sk
);
540 if (!pn_flow_safe(pn
->tx_fc
)) {
541 atomic_set(&pn
->tx_credits
, 1);
542 sk
->sk_write_space(sk
);
544 pipe_grant_credits(sk
, GFP_ATOMIC
);
547 /* Queue an skb to an actively connected sock.
548 * Socket lock must be held. */
549 static int pipe_handler_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
551 struct pep_sock
*pn
= pep_sk(sk
);
552 struct pnpipehdr
*hdr
= pnp_hdr(skb
);
553 int err
= NET_RX_SUCCESS
;
555 switch (hdr
->message_id
) {
556 case PNS_PIPE_ALIGNED_DATA
:
560 __skb_pull(skb
, 3); /* Pipe data header */
561 if (!pn_flow_safe(pn
->rx_fc
)) {
562 err
= sock_queue_rcv_skb(sk
, skb
);
564 return NET_RX_SUCCESS
;
569 if (pn
->rx_credits
== 0) {
570 atomic_inc(&sk
->sk_drops
);
576 skb_set_owner_r(skb
, sk
);
577 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
578 if (!sock_flag(sk
, SOCK_DEAD
))
579 sk
->sk_data_ready(sk
);
580 return NET_RX_SUCCESS
;
582 case PNS_PEP_CONNECT_RESP
:
583 if (sk
->sk_state
!= TCP_SYN_SENT
)
585 if (!sock_flag(sk
, SOCK_DEAD
))
586 sk
->sk_state_change(sk
);
587 if (pep_connresp_rcv(sk
, skb
)) {
588 sk
->sk_state
= TCP_CLOSE_WAIT
;
591 if (pn
->init_enable
== PN_PIPE_DISABLE
)
592 sk
->sk_state
= TCP_SYN_RECV
;
594 sk
->sk_state
= TCP_ESTABLISHED
;
595 pipe_start_flow_control(sk
);
599 case PNS_PEP_ENABLE_RESP
:
600 if (sk
->sk_state
!= TCP_SYN_SENT
)
603 if (pep_enableresp_rcv(sk
, skb
)) {
604 sk
->sk_state
= TCP_CLOSE_WAIT
;
608 sk
->sk_state
= TCP_ESTABLISHED
;
609 pipe_start_flow_control(sk
);
612 case PNS_PEP_DISCONNECT_RESP
:
613 /* sock should already be dead, nothing to do */
616 case PNS_PEP_STATUS_IND
:
617 pipe_rcv_status(sk
, skb
);
624 /* Listening sock must be locked */
625 static struct sock
*pep_find_pipe(const struct hlist_head
*hlist
,
626 const struct sockaddr_pn
*dst
,
630 u16 dobj
= pn_sockaddr_get_object(dst
);
632 sk_for_each(sknode
, hlist
) {
633 struct pep_sock
*pnnode
= pep_sk(sknode
);
635 /* Ports match, but addresses might not: */
636 if (pnnode
->pn_sk
.sobject
!= dobj
)
638 if (pnnode
->pipe_handle
!= pipe_handle
)
640 if (sknode
->sk_state
== TCP_CLOSE_WAIT
)
650 * Deliver an skb to a listening sock.
651 * Socket lock must be held.
652 * We then queue the skb to the right connected sock (if any).
654 static int pep_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
656 struct pep_sock
*pn
= pep_sk(sk
);
658 struct pnpipehdr
*hdr
;
659 struct sockaddr_pn dst
;
662 if (!pskb_may_pull(skb
, sizeof(*hdr
)))
666 pipe_handle
= hdr
->pipe_handle
;
667 if (pipe_handle
== PN_PIPE_INVALID_HANDLE
)
670 pn_skb_get_dst_sockaddr(skb
, &dst
);
672 /* Look for an existing pipe handle */
673 sknode
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
675 return sk_receive_skb(sknode
, skb
, 1);
677 switch (hdr
->message_id
) {
678 case PNS_PEP_CONNECT_REQ
:
679 if (sk
->sk_state
!= TCP_LISTEN
|| sk_acceptq_is_full(sk
)) {
680 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
,
684 skb_queue_head(&sk
->sk_receive_queue
, skb
);
685 sk_acceptq_added(sk
);
686 if (!sock_flag(sk
, SOCK_DEAD
))
687 sk
->sk_data_ready(sk
);
688 return NET_RX_SUCCESS
;
690 case PNS_PEP_DISCONNECT_REQ
:
691 pep_reply(sk
, skb
, PN_PIPE_NO_ERROR
, NULL
, 0, GFP_ATOMIC
);
694 case PNS_PEP_CTRL_REQ
:
695 pep_ctrlreq_error(sk
, skb
, PN_PIPE_INVALID_HANDLE
, GFP_ATOMIC
);
698 case PNS_PEP_RESET_REQ
:
699 case PNS_PEP_ENABLE_REQ
:
700 case PNS_PEP_DISABLE_REQ
:
701 /* invalid handle is not even allowed here! */
705 if ((1 << sk
->sk_state
)
706 & ~(TCPF_CLOSE
|TCPF_LISTEN
|TCPF_CLOSE_WAIT
))
707 /* actively connected socket */
708 return pipe_handler_do_rcv(sk
, skb
);
712 return NET_RX_SUCCESS
;
715 static int pipe_do_remove(struct sock
*sk
)
717 struct pep_sock
*pn
= pep_sk(sk
);
718 struct pnpipehdr
*ph
;
721 skb
= pep_alloc_skb(sk
, NULL
, 0, GFP_KERNEL
);
727 ph
->message_id
= PNS_PIPE_REMOVE_REQ
;
728 ph
->pipe_handle
= pn
->pipe_handle
;
730 return pn_skb_send(sk
, skb
, NULL
);
733 /* associated socket ceases to exist */
734 static void pep_sock_close(struct sock
*sk
, long timeout
)
736 struct pep_sock
*pn
= pep_sk(sk
);
739 sock_hold(sk
); /* keep a reference after sk_common_release() */
740 sk_common_release(sk
);
743 if ((1 << sk
->sk_state
) & (TCPF_SYN_RECV
|TCPF_ESTABLISHED
)) {
744 if (sk
->sk_backlog_rcv
== pipe_do_rcv
)
745 /* Forcefully remove dangling Phonet pipe */
748 pipe_handler_request(sk
, PNS_PEP_DISCONNECT_REQ
, PAD
,
751 sk
->sk_state
= TCP_CLOSE
;
753 ifindex
= pn
->ifindex
;
762 static struct sock
*pep_sock_accept(struct sock
*sk
,
763 struct proto_accept_arg
*arg
)
765 struct pep_sock
*pn
= pep_sk(sk
), *newpn
;
766 struct sock
*newsk
= NULL
;
768 struct pnpipehdr
*hdr
;
769 struct sockaddr_pn dst
, src
;
772 u8 pipe_handle
, enabled
, n_sb
;
775 skb
= skb_recv_datagram(sk
, (arg
->flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0,
781 if (sk
->sk_state
!= TCP_LISTEN
) {
785 sk_acceptq_removed(sk
);
788 if (!pskb_may_pull(skb
, sizeof(*hdr
) + 4))
792 pipe_handle
= hdr
->pipe_handle
;
793 switch (hdr
->state_after_connect
) {
794 case PN_PIPE_DISABLE
:
801 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_INVALID_PARAM
,
805 peer_type
= hdr
->other_pep_type
<< 8;
807 /* Parse sub-blocks (options) */
810 u8 type
, buf
[1], len
= sizeof(buf
);
811 const u8
*data
= pep_get_sb(skb
, &type
, &len
, buf
);
816 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE
:
819 peer_type
= (peer_type
& 0xff00) | data
[0];
821 case PN_PIPE_SB_ALIGNED_DATA
:
822 aligned
= data
[0] != 0;
828 /* Check for duplicate pipe handle */
829 newsk
= pep_find_pipe(&pn
->hlist
, &dst
, pipe_handle
);
830 if (unlikely(newsk
)) {
833 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_PEP_IN_USE
, GFP_KERNEL
);
837 /* Create a new to-be-accepted sock */
838 newsk
= sk_alloc(sock_net(sk
), PF_PHONET
, GFP_KERNEL
, sk
->sk_prot
,
841 pep_reject_conn(sk
, skb
, PN_PIPE_ERR_OVERLOAD
, GFP_KERNEL
);
846 sock_init_data(NULL
, newsk
);
847 newsk
->sk_state
= TCP_SYN_RECV
;
848 newsk
->sk_backlog_rcv
= pipe_do_rcv
;
849 newsk
->sk_protocol
= sk
->sk_protocol
;
850 newsk
->sk_destruct
= pipe_destruct
;
852 newpn
= pep_sk(newsk
);
853 pn_skb_get_dst_sockaddr(skb
, &dst
);
854 pn_skb_get_src_sockaddr(skb
, &src
);
855 newpn
->pn_sk
.sobject
= pn_sockaddr_get_object(&dst
);
856 newpn
->pn_sk
.dobject
= pn_sockaddr_get_object(&src
);
857 newpn
->pn_sk
.resource
= pn_sockaddr_get_resource(&dst
);
859 newpn
->listener
= sk
;
860 skb_queue_head_init(&newpn
->ctrlreq_queue
);
861 newpn
->pipe_handle
= pipe_handle
;
862 atomic_set(&newpn
->tx_credits
, 0);
864 newpn
->peer_type
= peer_type
;
865 newpn
->rx_credits
= 0;
866 newpn
->rx_fc
= newpn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
867 newpn
->init_enable
= enabled
;
868 newpn
->aligned
= aligned
;
870 err
= pep_accept_conn(newsk
, skb
);
877 sk_add_node(newsk
, &pn
->hlist
);
885 static int pep_sock_connect(struct sock
*sk
, struct sockaddr
*addr
, int len
)
887 struct pep_sock
*pn
= pep_sk(sk
);
889 u8 data
[4] = { 0 /* sub-blocks */, PAD
, PAD
, PAD
};
891 if (pn
->pipe_handle
== PN_PIPE_INVALID_HANDLE
)
892 pn
->pipe_handle
= 1; /* anything but INVALID_HANDLE */
894 err
= pipe_handler_request(sk
, PNS_PEP_CONNECT_REQ
,
895 pn
->init_enable
, data
, 4);
897 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
901 sk
->sk_state
= TCP_SYN_SENT
;
906 static int pep_sock_enable(struct sock
*sk
, struct sockaddr
*addr
, int len
)
910 err
= pipe_handler_request(sk
, PNS_PEP_ENABLE_REQ
, PAD
,
915 sk
->sk_state
= TCP_SYN_SENT
;
920 static unsigned int pep_first_packet_length(struct sock
*sk
)
922 struct pep_sock
*pn
= pep_sk(sk
);
923 struct sk_buff_head
*q
;
925 unsigned int len
= 0;
928 if (sock_flag(sk
, SOCK_URGINLINE
)) {
929 q
= &pn
->ctrlreq_queue
;
930 spin_lock_bh(&q
->lock
);
936 spin_unlock_bh(&q
->lock
);
939 if (likely(!found
)) {
940 q
= &sk
->sk_receive_queue
;
941 spin_lock_bh(&q
->lock
);
945 spin_unlock_bh(&q
->lock
);
951 static int pep_ioctl(struct sock
*sk
, int cmd
, int *karg
)
953 struct pep_sock
*pn
= pep_sk(sk
);
954 int ret
= -ENOIOCTLCMD
;
958 if (sk
->sk_state
== TCP_LISTEN
) {
963 *karg
= pep_first_packet_length(sk
);
967 case SIOCPNENABLEPIPE
:
969 if (sk
->sk_state
== TCP_SYN_SENT
)
971 else if (sk
->sk_state
== TCP_ESTABLISHED
)
973 else if (!pn
->pn_sk
.sobject
)
974 ret
= -EADDRNOTAVAIL
;
976 ret
= pep_sock_enable(sk
, NULL
, 0);
984 static int pep_init(struct sock
*sk
)
986 struct pep_sock
*pn
= pep_sk(sk
);
988 sk
->sk_destruct
= pipe_destruct
;
989 INIT_HLIST_HEAD(&pn
->hlist
);
991 skb_queue_head_init(&pn
->ctrlreq_queue
);
992 atomic_set(&pn
->tx_credits
, 0);
995 pn
->pipe_handle
= PN_PIPE_INVALID_HANDLE
;
997 pn
->rx_fc
= pn
->tx_fc
= PN_LEGACY_FLOW_CONTROL
;
1003 static int pep_setsockopt(struct sock
*sk
, int level
, int optname
,
1004 sockptr_t optval
, unsigned int optlen
)
1006 struct pep_sock
*pn
= pep_sk(sk
);
1007 int val
= 0, err
= 0;
1009 if (level
!= SOL_PNPIPE
)
1010 return -ENOPROTOOPT
;
1011 if (optlen
>= sizeof(int)) {
1012 if (copy_from_sockptr(&val
, optval
, sizeof(int)))
1019 if (val
&& val
!= PNPIPE_ENCAP_IP
) {
1023 if (!pn
->ifindex
== !val
)
1024 break; /* Nothing to do! */
1025 if (!capable(CAP_NET_ADMIN
)) {
1031 err
= gprs_attach(sk
);
1045 if ((sk
->sk_state
== TCP_CLOSE
) &&
1046 (val
>= 0) && (val
< PN_PIPE_INVALID_HANDLE
))
1047 pn
->pipe_handle
= val
;
1052 case PNPIPE_INITSTATE
:
1053 pn
->init_enable
= !!val
;
1065 static int pep_getsockopt(struct sock
*sk
, int level
, int optname
,
1066 char __user
*optval
, int __user
*optlen
)
1068 struct pep_sock
*pn
= pep_sk(sk
);
1071 if (level
!= SOL_PNPIPE
)
1072 return -ENOPROTOOPT
;
1073 if (get_user(len
, optlen
))
1078 val
= pn
->ifindex
? PNPIPE_ENCAP_IP
: PNPIPE_ENCAP_NONE
;
1081 case PNPIPE_IFINDEX
:
1086 val
= pn
->pipe_handle
;
1087 if (val
== PN_PIPE_INVALID_HANDLE
)
1091 case PNPIPE_INITSTATE
:
1092 val
= pn
->init_enable
;
1096 return -ENOPROTOOPT
;
1099 len
= min_t(unsigned int, sizeof(int), len
);
1100 if (put_user(len
, optlen
))
1102 if (put_user(val
, (int __user
*) optval
))
1107 static int pipe_skb_send(struct sock
*sk
, struct sk_buff
*skb
)
1109 struct pep_sock
*pn
= pep_sk(sk
);
1110 struct pnpipehdr
*ph
;
1113 if (pn_flow_safe(pn
->tx_fc
) &&
1114 !atomic_add_unless(&pn
->tx_credits
, -1, 0)) {
1119 skb_push(skb
, 3 + pn
->aligned
);
1120 skb_reset_transport_header(skb
);
1124 ph
->message_id
= PNS_PIPE_ALIGNED_DATA
;
1125 ph
->data0
= 0; /* padding */
1127 ph
->message_id
= PNS_PIPE_DATA
;
1128 ph
->pipe_handle
= pn
->pipe_handle
;
1129 err
= pn_skb_send(sk
, skb
, NULL
);
1131 if (err
&& pn_flow_safe(pn
->tx_fc
))
1132 atomic_inc(&pn
->tx_credits
);
1137 static int pep_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
)
1139 struct pep_sock
*pn
= pep_sk(sk
);
1140 struct sk_buff
*skb
;
1142 int flags
= msg
->msg_flags
;
1145 if (len
> USHRT_MAX
)
1148 if ((msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_NOSIGNAL
|
1149 MSG_CMSG_COMPAT
)) ||
1150 !(msg
->msg_flags
& MSG_EOR
))
1153 skb
= sock_alloc_send_skb(sk
, MAX_PNPIPE_HEADER
+ len
,
1154 flags
& MSG_DONTWAIT
, &err
);
1158 skb_reserve(skb
, MAX_PHONET_HEADER
+ 3 + pn
->aligned
);
1159 err
= memcpy_from_msg(skb_put(skb
, len
), msg
, len
);
1164 timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
1165 if ((1 << sk
->sk_state
) & (TCPF_LISTEN
|TCPF_CLOSE
)) {
1169 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1170 /* Wait until the pipe gets to enabled state */
1172 err
= sk_stream_wait_connect(sk
, &timeo
);
1176 if (sk
->sk_state
== TCP_CLOSE_WAIT
) {
1181 BUG_ON(sk
->sk_state
!= TCP_ESTABLISHED
);
1183 /* Wait until flow control allows TX */
1184 done
= atomic_read(&pn
->tx_credits
);
1186 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
1192 if (signal_pending(current
)) {
1193 err
= sock_intr_errno(timeo
);
1197 add_wait_queue(sk_sleep(sk
), &wait
);
1198 done
= sk_wait_event(sk
, &timeo
, atomic_read(&pn
->tx_credits
), &wait
);
1199 remove_wait_queue(sk_sleep(sk
), &wait
);
1201 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1205 err
= pipe_skb_send(sk
, skb
);
1207 err
= len
; /* success! */
1216 int pep_writeable(struct sock
*sk
)
1218 struct pep_sock
*pn
= pep_sk(sk
);
1220 return atomic_read(&pn
->tx_credits
);
1223 int pep_write(struct sock
*sk
, struct sk_buff
*skb
)
1225 struct sk_buff
*rskb
, *fs
;
1228 if (pep_sk(sk
)->aligned
)
1229 return pipe_skb_send(sk
, skb
);
1231 rskb
= alloc_skb(MAX_PNPIPE_HEADER
, GFP_ATOMIC
);
1236 skb_shinfo(rskb
)->frag_list
= skb
;
1237 rskb
->len
+= skb
->len
;
1238 rskb
->data_len
+= rskb
->len
;
1239 rskb
->truesize
+= rskb
->len
;
1241 /* Avoid nested fragments */
1242 skb_walk_frags(skb
, fs
)
1244 skb
->next
= skb_shinfo(skb
)->frag_list
;
1245 skb_frag_list_init(skb
);
1247 skb
->data_len
-= flen
;
1248 skb
->truesize
-= flen
;
1250 skb_reserve(rskb
, MAX_PHONET_HEADER
+ 3);
1251 return pipe_skb_send(sk
, rskb
);
1254 struct sk_buff
*pep_read(struct sock
*sk
)
1256 struct sk_buff
*skb
= skb_dequeue(&sk
->sk_receive_queue
);
1258 if (sk
->sk_state
== TCP_ESTABLISHED
)
1259 pipe_grant_credits(sk
, GFP_ATOMIC
);
1263 static int pep_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
1264 int flags
, int *addr_len
)
1266 struct sk_buff
*skb
;
1269 if (flags
& ~(MSG_OOB
|MSG_PEEK
|MSG_TRUNC
|MSG_DONTWAIT
|MSG_WAITALL
|
1270 MSG_NOSIGNAL
|MSG_CMSG_COMPAT
))
1273 if (unlikely(1 << sk
->sk_state
& (TCPF_LISTEN
| TCPF_CLOSE
)))
1276 if ((flags
& MSG_OOB
) || sock_flag(sk
, SOCK_URGINLINE
)) {
1277 /* Dequeue and acknowledge control request */
1278 struct pep_sock
*pn
= pep_sk(sk
);
1280 if (flags
& MSG_PEEK
)
1282 skb
= skb_dequeue(&pn
->ctrlreq_queue
);
1284 pep_ctrlreq_error(sk
, skb
, PN_PIPE_NO_ERROR
,
1286 msg
->msg_flags
|= MSG_OOB
;
1289 if (flags
& MSG_OOB
)
1293 skb
= skb_recv_datagram(sk
, flags
, &err
);
1296 if (err
== -ENOTCONN
&& sk
->sk_state
== TCP_CLOSE_WAIT
)
1302 if (sk
->sk_state
== TCP_ESTABLISHED
)
1303 pipe_grant_credits(sk
, GFP_KERNEL
);
1306 msg
->msg_flags
|= MSG_EOR
;
1308 msg
->msg_flags
|= MSG_TRUNC
;
1312 err
= skb_copy_datagram_msg(skb
, 0, msg
, len
);
1314 err
= (flags
& MSG_TRUNC
) ? skb
->len
: len
;
1316 skb_free_datagram(sk
, skb
);
1320 static void pep_sock_unhash(struct sock
*sk
)
1322 struct pep_sock
*pn
= pep_sk(sk
);
1323 struct sock
*skparent
= NULL
;
1327 if (pn
->listener
!= NULL
) {
1328 skparent
= pn
->listener
;
1329 pn
->listener
= NULL
;
1332 pn
= pep_sk(skparent
);
1333 lock_sock(skparent
);
1334 sk_del_node_init(sk
);
1338 /* Unhash a listening sock only when it is closed
1339 * and all of its active connected pipes are closed. */
1340 if (hlist_empty(&pn
->hlist
))
1341 pn_sock_unhash(&pn
->pn_sk
.sk
);
1348 static struct proto pep_proto
= {
1349 .close
= pep_sock_close
,
1350 .accept
= pep_sock_accept
,
1351 .connect
= pep_sock_connect
,
1354 .setsockopt
= pep_setsockopt
,
1355 .getsockopt
= pep_getsockopt
,
1356 .sendmsg
= pep_sendmsg
,
1357 .recvmsg
= pep_recvmsg
,
1358 .backlog_rcv
= pep_do_rcv
,
1359 .hash
= pn_sock_hash
,
1360 .unhash
= pep_sock_unhash
,
1361 .get_port
= pn_sock_get_port
,
1362 .obj_size
= sizeof(struct pep_sock
),
1363 .owner
= THIS_MODULE
,
1367 static const struct phonet_protocol pep_pn_proto
= {
1368 .ops
= &phonet_stream_ops
,
1370 .sock_type
= SOCK_SEQPACKET
,
1373 static int __init
pep_register(void)
1375 return phonet_proto_register(PN_PROTO_PIPE
, &pep_pn_proto
);
1378 static void __exit
pep_unregister(void)
1380 phonet_proto_unregister(PN_PROTO_PIPE
, &pep_pn_proto
);
1383 module_init(pep_register
);
1384 module_exit(pep_unregister
);
1385 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1386 MODULE_DESCRIPTION("Phonet pipe protocol");
1387 MODULE_LICENSE("GPL");
1388 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET
, PN_PROTO_PIPE
);