Phonet: remove redumdant pep->pipe_state
[linux-3.10.git] / net / phonet / pep.c
1 /*
2  * File: pep.c
3  *
4  * Phonet pipe protocol end point socket
5  *
6  * Copyright (C) 2008 Nokia Corporation.
7  *
8  * Author: RĂ©mi Denis-Courmont <remi.denis-courmont@nokia.com>
9  *
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.
13  *
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.
18  *
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
22  * 02110-1301 USA
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/socket.h>
28 #include <net/sock.h>
29 #include <net/tcp_states.h>
30 #include <asm/ioctls.h>
31
32 #include <linux/phonet.h>
33 #include <net/phonet/phonet.h>
34 #include <net/phonet/pep.h>
35 #include <net/phonet/gprs.h>
36
37 /* sk_state values:
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
43  *
44  * pep_sock locking:
45  *  - sk_state, ackq, hlist: sock lock needed
46  *  - listener: read only
47  *  - pipe_handle: read only
48  */
49
50 #define CREDITS_MAX     10
51 #define CREDITS_THR     7
52
53 #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
54
55 /* Get the next TLV sub-block. */
56 static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen,
57                                         void *buf)
58 {
59         void *data = NULL;
60         struct {
61                 u8 sb_type;
62                 u8 sb_len;
63         } *ph, h;
64         int buflen = *plen;
65
66         ph = skb_header_pointer(skb, 0, 2, &h);
67         if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len))
68                 return NULL;
69         ph->sb_len -= 2;
70         *ptype = ph->sb_type;
71         *plen = ph->sb_len;
72
73         if (buflen > ph->sb_len)
74                 buflen = ph->sb_len;
75         data = skb_header_pointer(skb, 2, buflen, buf);
76         __skb_pull(skb, 2 + ph->sb_len);
77         return data;
78 }
79
80 static int pep_reply(struct sock *sk, struct sk_buff *oskb,
81                         u8 code, const void *data, int len, gfp_t priority)
82 {
83         const struct pnpipehdr *oph = pnp_hdr(oskb);
84         struct pnpipehdr *ph;
85         struct sk_buff *skb;
86         struct sockaddr_pn peer;
87
88         skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
89         if (!skb)
90                 return -ENOMEM;
91         skb_set_owner_w(skb, sk);
92
93         skb_reserve(skb, MAX_PNPIPE_HEADER);
94         __skb_put(skb, len);
95         skb_copy_to_linear_data(skb, data, len);
96         __skb_push(skb, sizeof(*ph));
97         skb_reset_transport_header(skb);
98         ph = pnp_hdr(skb);
99         ph->utid = oph->utid;
100         ph->message_id = oph->message_id + 1; /* REQ -> RESP */
101         ph->pipe_handle = oph->pipe_handle;
102         ph->error_code = code;
103
104         pn_skb_get_src_sockaddr(oskb, &peer);
105         return pn_skb_send(sk, skb, &peer);
106 }
107
108 #define PAD 0x00
109
110 #ifdef CONFIG_PHONET_PIPECTRLR
111 static u8 pipe_negotiate_fc(u8 *host_fc, u8 *remote_fc, int len)
112 {
113         int i, j;
114         u8 base_fc, final_fc;
115
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) {
120                                 final_fc = base_fc;
121                                 goto done;
122                         }
123                 }
124         }
125         return -EINVAL;
126
127 done:
128         return final_fc;
129
130 }
131
132 static int pipe_get_flow_info(struct sock *sk, struct sk_buff *skb,
133                 u8 *pref_rx_fc, u8 *req_tx_fc)
134 {
135         struct pnpipehdr *hdr;
136         u8 n_sb;
137
138         if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
139                 return -EINVAL;
140
141         hdr = pnp_hdr(skb);
142         n_sb = hdr->data[4];
143
144         __skb_pull(skb, sizeof(*hdr) + 4);
145         while (n_sb > 0) {
146                 u8 type, buf[3], len = sizeof(buf);
147                 u8 *data = pep_get_sb(skb, &type, &len, buf);
148
149                 if (data == NULL)
150                         return -EINVAL;
151
152                 switch (type) {
153                 case PN_PIPE_SB_REQUIRED_FC_TX:
154                         if (len < 3 || (data[2] | data[3] | data[4]) > 3)
155                                 break;
156                         req_tx_fc[0] = data[2];
157                         req_tx_fc[1] = data[3];
158                         req_tx_fc[2] = data[4];
159                         break;
160
161                 case PN_PIPE_SB_PREFERRED_FC_RX:
162                         if (len < 3 || (data[2] | data[3] | data[4]) > 3)
163                                 break;
164                         pref_rx_fc[0] = data[2];
165                         pref_rx_fc[1] = data[3];
166                         pref_rx_fc[2] = data[4];
167                         break;
168
169                 }
170                 n_sb--;
171         }
172         return 0;
173 }
174
175 static int pipe_handler_send_req(struct sock *sk, u8 utid,
176                 u8 msg_id, gfp_t priority)
177 {
178         int len;
179         struct pnpipehdr *ph;
180         struct sk_buff *skb;
181         struct pep_sock *pn = pep_sk(sk);
182
183         static const u8 data[4] = {
184                 PAD, PAD, PAD, PAD,
185         };
186
187         switch (msg_id) {
188         case PNS_PEP_CONNECT_REQ:
189                 len = sizeof(data);
190                 break;
191
192         case PNS_PEP_DISCONNECT_REQ:
193         case PNS_PEP_ENABLE_REQ:
194         case PNS_PEP_DISABLE_REQ:
195                 len = 0;
196                 break;
197
198         default:
199                 return -EINVAL;
200         }
201
202         skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
203         if (!skb)
204                 return -ENOMEM;
205         skb_set_owner_w(skb, sk);
206
207         skb_reserve(skb, MAX_PNPIPE_HEADER);
208         if (len) {
209                 __skb_put(skb, len);
210                 skb_copy_to_linear_data(skb, data, len);
211         }
212         __skb_push(skb, sizeof(*ph));
213         skb_reset_transport_header(skb);
214         ph = pnp_hdr(skb);
215         ph->utid = utid;
216         ph->message_id = msg_id;
217         ph->pipe_handle = pn->pipe_handle;
218         ph->error_code = PN_PIPE_NO_ERROR;
219
220         return pn_skb_send(sk, skb, NULL);
221 }
222
223 static int pipe_handler_send_created_ind(struct sock *sk,
224                 u8 utid, u8 msg_id)
225 {
226         int err_code;
227         struct pnpipehdr *ph;
228         struct sk_buff *skb;
229
230         struct pep_sock *pn = pep_sk(sk);
231         static u8 data[4] = {
232                 0x03, 0x04,
233         };
234         data[2] = pn->tx_fc;
235         data[3] = pn->rx_fc;
236
237         /*
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.
243          */
244         err_code = 0x01;
245
246         skb = alloc_skb(MAX_PNPIPE_HEADER + sizeof(data), GFP_ATOMIC);
247         if (!skb)
248                 return -ENOMEM;
249         skb_set_owner_w(skb, sk);
250
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);
256         ph = pnp_hdr(skb);
257         ph->utid = utid;
258         ph->message_id = msg_id;
259         ph->pipe_handle = pn->pipe_handle;
260         ph->error_code = err_code;
261
262         return pn_skb_send(sk, skb, NULL);
263 }
264
265 static int pipe_handler_send_ind(struct sock *sk, u8 utid, u8 msg_id)
266 {
267         int err_code;
268         struct pnpipehdr *ph;
269         struct sk_buff *skb;
270         struct pep_sock *pn = pep_sk(sk);
271
272         /*
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.
278          */
279         err_code = 0x0;
280
281         skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
282         if (!skb)
283                 return -ENOMEM;
284         skb_set_owner_w(skb, sk);
285
286         skb_reserve(skb, MAX_PNPIPE_HEADER);
287         __skb_push(skb, sizeof(*ph));
288         skb_reset_transport_header(skb);
289         ph = pnp_hdr(skb);
290         ph->utid = utid;
291         ph->message_id = msg_id;
292         ph->pipe_handle = pn->pipe_handle;
293         ph->error_code = err_code;
294
295         return pn_skb_send(sk, skb, NULL);
296 }
297
298 static int pipe_handler_enable_pipe(struct sock *sk, int enable)
299 {
300         int utid, req;
301
302         if (enable) {
303                 utid = PNS_PIPE_ENABLE_UTID;
304                 req = PNS_PEP_ENABLE_REQ;
305         } else {
306                 utid = PNS_PIPE_DISABLE_UTID;
307                 req = PNS_PEP_DISABLE_REQ;
308         }
309         return pipe_handler_send_req(sk, utid, req, GFP_ATOMIC);
310 }
311 #endif
312
313 static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
314 {
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,
321                         PAD,
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,
326                         PAD,
327         };
328
329         might_sleep();
330         return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
331                                 GFP_KERNEL);
332 }
333
334 static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code)
335 {
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);
339 }
340
341 /* Control requests are not sent by the pipe service and have a specific
342  * message format. */
343 static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
344                                 gfp_t priority)
345 {
346         const struct pnpipehdr *oph = pnp_hdr(oskb);
347         struct sk_buff *skb;
348         struct pnpipehdr *ph;
349         struct sockaddr_pn dst;
350
351         skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
352         if (!skb)
353                 return -ENOMEM;
354         skb_set_owner_w(skb, sk);
355
356         skb_reserve(skb, MAX_PHONET_HEADER);
357         ph = (struct pnpipehdr *)skb_put(skb, sizeof(*ph) + 4);
358
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 */
365         ph->data[3] = PAD;
366         ph->data[4] = PAD;
367
368         pn_skb_get_src_sockaddr(oskb, &dst);
369         return pn_skb_send(sk, skb, &dst);
370 }
371
372 static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
373 {
374         struct pep_sock *pn = pep_sk(sk);
375         struct pnpipehdr *ph;
376         struct sk_buff *skb;
377
378         skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
379         if (!skb)
380                 return -ENOMEM;
381         skb_set_owner_w(skb, sk);
382
383         skb_reserve(skb, MAX_PNPIPE_HEADER + 4);
384         __skb_push(skb, sizeof(*ph) + 4);
385         skb_reset_transport_header(skb);
386         ph = pnp_hdr(skb);
387         ph->utid = 0;
388         ph->message_id = PNS_PEP_STATUS_IND;
389         ph->pipe_handle = pn->pipe_handle;
390         ph->pep_type = PN_PEP_TYPE_COMMON;
391         ph->data[1] = type;
392         ph->data[2] = PAD;
393         ph->data[3] = PAD;
394         ph->data[4] = status;
395
396         return pn_skb_send(sk, skb, NULL);
397 }
398
399 /* Send our RX flow control information to the sender.
400  * Socket must be locked. */
401 static void pipe_grant_credits(struct sock *sk)
402 {
403         struct pep_sock *pn = pep_sk(sk);
404
405         BUG_ON(sk->sk_state != TCP_ESTABLISHED);
406
407         switch (pn->rx_fc) {
408         case PN_LEGACY_FLOW_CONTROL: /* TODO */
409                 break;
410         case PN_ONE_CREDIT_FLOW_CONTROL:
411                 pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
412                                 PEP_IND_READY, GFP_ATOMIC);
413                 pn->rx_credits = 1;
414                 break;
415         case PN_MULTI_CREDIT_FLOW_CONTROL:
416                 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
417                         break;
418                 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
419                                         CREDITS_MAX - pn->rx_credits,
420                                         GFP_ATOMIC) == 0)
421                         pn->rx_credits = CREDITS_MAX;
422                 break;
423         }
424 }
425
426 static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
427 {
428         struct pep_sock *pn = pep_sk(sk);
429         struct pnpipehdr *hdr;
430         int wake = 0;
431
432         if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
433                 return -EINVAL;
434
435         hdr = pnp_hdr(skb);
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]);
439                 return -EOPNOTSUPP;
440         }
441
442         switch (hdr->data[1]) {
443         case PN_PEP_IND_FLOW_CONTROL:
444                 switch (pn->tx_fc) {
445                 case PN_LEGACY_FLOW_CONTROL:
446                         switch (hdr->data[4]) {
447                         case PEP_IND_BUSY:
448                                 atomic_set(&pn->tx_credits, 0);
449                                 break;
450                         case PEP_IND_READY:
451                                 atomic_set(&pn->tx_credits, wake = 1);
452                                 break;
453                         }
454                         break;
455                 case PN_ONE_CREDIT_FLOW_CONTROL:
456                         if (hdr->data[4] == PEP_IND_READY)
457                                 atomic_set(&pn->tx_credits, wake = 1);
458                         break;
459                 }
460                 break;
461
462         case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
463                 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
464                         break;
465                 atomic_add(wake = hdr->data[4], &pn->tx_credits);
466                 break;
467
468         default:
469                 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
470                                 (unsigned)hdr->data[1]);
471                 return -EOPNOTSUPP;
472         }
473         if (wake)
474                 sk->sk_write_space(sk);
475         return 0;
476 }
477
478 static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
479 {
480         struct pep_sock *pn = pep_sk(sk);
481         struct pnpipehdr *hdr = pnp_hdr(skb);
482         u8 n_sb = hdr->data[0];
483
484         pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
485         __skb_pull(skb, sizeof(*hdr));
486         while (n_sb > 0) {
487                 u8 type, buf[2], len = sizeof(buf);
488                 u8 *data = pep_get_sb(skb, &type, &len, buf);
489
490                 if (data == NULL)
491                         return -EINVAL;
492                 switch (type) {
493                 case PN_PIPE_SB_NEGOTIATED_FC:
494                         if (len < 2 || (data[0] | data[1]) > 3)
495                                 break;
496                         pn->tx_fc = data[0] & 3;
497                         pn->rx_fc = data[1] & 3;
498                         break;
499                 }
500                 n_sb--;
501         }
502         return 0;
503 }
504
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)
508 {
509         struct pep_sock *pn = pep_sk(sk);
510         struct pnpipehdr *hdr = pnp_hdr(skb);
511         struct sk_buff_head *queue;
512         int err = 0;
513
514         BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
515
516         switch (hdr->message_id) {
517         case PNS_PEP_CONNECT_REQ:
518                 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
519                 break;
520
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);
526                 break;
527
528 #ifdef CONFIG_PHONET_PIPECTRLR
529         case PNS_PEP_DISCONNECT_RESP:
530                 sk->sk_state = TCP_CLOSE;
531                 break;
532 #endif
533
534         case PNS_PEP_ENABLE_REQ:
535                 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
536                 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
537                 break;
538
539 #ifdef CONFIG_PHONET_PIPECTRLR
540         case PNS_PEP_ENABLE_RESP:
541                 pipe_handler_send_ind(sk, PNS_PIPE_ENABLED_IND_UTID,
542                                 PNS_PIPE_ENABLED_IND);
543
544                 if (!pn_flow_safe(pn->tx_fc)) {
545                         atomic_set(&pn->tx_credits, 1);
546                         sk->sk_write_space(sk);
547                 }
548                 if (sk->sk_state == TCP_ESTABLISHED)
549                         break; /* Nothing to do */
550                 sk->sk_state = TCP_ESTABLISHED;
551                 pipe_grant_credits(sk);
552                 break;
553 #endif
554
555         case PNS_PEP_RESET_REQ:
556                 switch (hdr->state_after_reset) {
557                 case PN_PIPE_DISABLE:
558                         pn->init_enable = 0;
559                         break;
560                 case PN_PIPE_ENABLE:
561                         pn->init_enable = 1;
562                         break;
563                 default: /* not allowed to send an error here!? */
564                         err = -EINVAL;
565                         goto out;
566                 }
567                 /* fall through */
568         case PNS_PEP_DISABLE_REQ:
569                 atomic_set(&pn->tx_credits, 0);
570                 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
571                 break;
572
573 #ifdef CONFIG_PHONET_PIPECTRLR
574         case PNS_PEP_DISABLE_RESP:
575                 atomic_set(&pn->tx_credits, 0);
576                 pipe_handler_send_ind(sk, PNS_PIPE_DISABLED_IND_UTID,
577                                 PNS_PIPE_DISABLED_IND);
578                 sk->sk_state = TCP_SYN_RECV;
579                 pn->rx_credits = 0;
580                 break;
581 #endif
582
583         case PNS_PEP_CTRL_REQ:
584                 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
585                         atomic_inc(&sk->sk_drops);
586                         break;
587                 }
588                 __skb_pull(skb, 4);
589                 queue = &pn->ctrlreq_queue;
590                 goto queue;
591
592         case PNS_PIPE_ALIGNED_DATA:
593                 __skb_pull(skb, 1);
594                 /* fall through */
595         case PNS_PIPE_DATA:
596                 __skb_pull(skb, 3); /* Pipe data header */
597                 if (!pn_flow_safe(pn->rx_fc)) {
598                         err = sock_queue_rcv_skb(sk, skb);
599                         if (!err)
600                                 return 0;
601                         break;
602                 }
603
604                 if (pn->rx_credits == 0) {
605                         atomic_inc(&sk->sk_drops);
606                         err = -ENOBUFS;
607                         break;
608                 }
609                 pn->rx_credits--;
610                 queue = &sk->sk_receive_queue;
611                 goto queue;
612
613         case PNS_PEP_STATUS_IND:
614                 pipe_rcv_status(sk, skb);
615                 break;
616
617         case PNS_PIPE_REDIRECTED_IND:
618                 err = pipe_rcv_created(sk, skb);
619                 break;
620
621         case PNS_PIPE_CREATED_IND:
622                 err = pipe_rcv_created(sk, skb);
623                 if (err)
624                         break;
625                 /* fall through */
626         case PNS_PIPE_RESET_IND:
627                 if (!pn->init_enable)
628                         break;
629                 /* fall through */
630         case PNS_PIPE_ENABLED_IND:
631                 if (!pn_flow_safe(pn->tx_fc)) {
632                         atomic_set(&pn->tx_credits, 1);
633                         sk->sk_write_space(sk);
634                 }
635                 if (sk->sk_state == TCP_ESTABLISHED)
636                         break; /* Nothing to do */
637                 sk->sk_state = TCP_ESTABLISHED;
638                 pipe_grant_credits(sk);
639                 break;
640
641         case PNS_PIPE_DISABLED_IND:
642                 sk->sk_state = TCP_SYN_RECV;
643                 pn->rx_credits = 0;
644                 break;
645
646         default:
647                 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
648                                 hdr->message_id);
649                 err = -EINVAL;
650         }
651 out:
652         kfree_skb(skb);
653         return err;
654
655 queue:
656         skb->dev = NULL;
657         skb_set_owner_r(skb, sk);
658         err = skb->len;
659         skb_queue_tail(queue, skb);
660         if (!sock_flag(sk, SOCK_DEAD))
661                 sk->sk_data_ready(sk, err);
662         return 0;
663 }
664
665 /* Destroy connected sock. */
666 static void pipe_destruct(struct sock *sk)
667 {
668         struct pep_sock *pn = pep_sk(sk);
669
670         skb_queue_purge(&sk->sk_receive_queue);
671         skb_queue_purge(&pn->ctrlreq_queue);
672 }
673
674 #ifdef CONFIG_PHONET_PIPECTRLR
675 static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
676 {
677         struct pep_sock *pn = pep_sk(sk);
678         u8 host_pref_rx_fc[3] = {3, 2, 1}, host_req_tx_fc[3] = {3, 2, 1};
679         u8 remote_pref_rx_fc[3], remote_req_tx_fc[3];
680         u8 negotiated_rx_fc, negotiated_tx_fc;
681         int ret;
682
683         pipe_get_flow_info(sk, skb, remote_pref_rx_fc,
684                         remote_req_tx_fc);
685         negotiated_tx_fc = pipe_negotiate_fc(remote_req_tx_fc,
686                         host_pref_rx_fc,
687                         sizeof(host_pref_rx_fc));
688         negotiated_rx_fc = pipe_negotiate_fc(host_req_tx_fc,
689                         remote_pref_rx_fc,
690                         sizeof(host_pref_rx_fc));
691
692         sk->sk_state = TCP_SYN_RECV;
693         sk->sk_backlog_rcv = pipe_do_rcv;
694         sk->sk_destruct = pipe_destruct;
695         pn->rx_credits = 0;
696         pn->rx_fc = negotiated_rx_fc;
697         pn->tx_fc = negotiated_tx_fc;
698         sk->sk_state_change(sk);
699
700         ret = pipe_handler_send_created_ind(sk,
701                         PNS_PIPE_CREATED_IND_UTID,
702                         PNS_PIPE_CREATED_IND
703                         );
704
705         return ret;
706 }
707 #endif
708
709 static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
710 {
711         struct sock *newsk;
712         struct pep_sock *newpn, *pn = pep_sk(sk);
713         struct pnpipehdr *hdr;
714         struct sockaddr_pn dst, src;
715         u16 peer_type;
716         u8 pipe_handle, enabled, n_sb;
717         u8 aligned = 0;
718
719         if (!pskb_pull(skb, sizeof(*hdr) + 4))
720                 return -EINVAL;
721
722         hdr = pnp_hdr(skb);
723         pipe_handle = hdr->pipe_handle;
724         switch (hdr->state_after_connect) {
725         case PN_PIPE_DISABLE:
726                 enabled = 0;
727                 break;
728         case PN_PIPE_ENABLE:
729                 enabled = 1;
730                 break;
731         default:
732                 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
733                 return -EINVAL;
734         }
735         peer_type = hdr->other_pep_type << 8;
736
737         if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) {
738                 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
739                 return -ENOBUFS;
740         }
741
742         /* Parse sub-blocks (options) */
743         n_sb = hdr->data[4];
744         while (n_sb > 0) {
745                 u8 type, buf[1], len = sizeof(buf);
746                 const u8 *data = pep_get_sb(skb, &type, &len, buf);
747
748                 if (data == NULL)
749                         return -EINVAL;
750                 switch (type) {
751                 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
752                         if (len < 1)
753                                 return -EINVAL;
754                         peer_type = (peer_type & 0xff00) | data[0];
755                         break;
756                 case PN_PIPE_SB_ALIGNED_DATA:
757                         aligned = data[0] != 0;
758                         break;
759                 }
760                 n_sb--;
761         }
762
763         skb = skb_clone(skb, GFP_ATOMIC);
764         if (!skb)
765                 return -ENOMEM;
766
767         /* Create a new to-be-accepted sock */
768         newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
769         if (!newsk) {
770                 kfree_skb(skb);
771                 return -ENOMEM;
772         }
773         sock_init_data(NULL, newsk);
774         newsk->sk_state = TCP_SYN_RECV;
775         newsk->sk_backlog_rcv = pipe_do_rcv;
776         newsk->sk_protocol = sk->sk_protocol;
777         newsk->sk_destruct = pipe_destruct;
778
779         newpn = pep_sk(newsk);
780         pn_skb_get_dst_sockaddr(skb, &dst);
781         pn_skb_get_src_sockaddr(skb, &src);
782         newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
783         newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
784         newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
785         skb_queue_head_init(&newpn->ctrlreq_queue);
786         newpn->pipe_handle = pipe_handle;
787         atomic_set(&newpn->tx_credits, 0);
788         newpn->peer_type = peer_type;
789         newpn->rx_credits = 0;
790         newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
791         newpn->init_enable = enabled;
792         newpn->aligned = aligned;
793
794         BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
795         skb_queue_head(&newsk->sk_receive_queue, skb);
796         if (!sock_flag(sk, SOCK_DEAD))
797                 sk->sk_data_ready(sk, 0);
798
799         sk_acceptq_added(sk);
800         sk_add_node(newsk, &pn->ackq);
801         return 0;
802 }
803
804 /* Listening sock must be locked */
805 static struct sock *pep_find_pipe(const struct hlist_head *hlist,
806                                         const struct sockaddr_pn *dst,
807                                         u8 pipe_handle)
808 {
809         struct hlist_node *node;
810         struct sock *sknode;
811         u16 dobj = pn_sockaddr_get_object(dst);
812
813         sk_for_each(sknode, node, hlist) {
814                 struct pep_sock *pnnode = pep_sk(sknode);
815
816                 /* Ports match, but addresses might not: */
817                 if (pnnode->pn_sk.sobject != dobj)
818                         continue;
819                 if (pnnode->pipe_handle != pipe_handle)
820                         continue;
821                 if (sknode->sk_state == TCP_CLOSE_WAIT)
822                         continue;
823
824                 sock_hold(sknode);
825                 return sknode;
826         }
827         return NULL;
828 }
829
830 /*
831  * Deliver an skb to a listening sock.
832  * Socket lock must be held.
833  * We then queue the skb to the right connected sock (if any).
834  */
835 static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
836 {
837         struct pep_sock *pn = pep_sk(sk);
838         struct sock *sknode;
839         struct pnpipehdr *hdr;
840         struct sockaddr_pn dst;
841         int err = NET_RX_SUCCESS;
842         u8 pipe_handle;
843
844         if (!pskb_may_pull(skb, sizeof(*hdr)))
845                 goto drop;
846
847         hdr = pnp_hdr(skb);
848         pipe_handle = hdr->pipe_handle;
849         if (pipe_handle == PN_PIPE_INVALID_HANDLE)
850                 goto drop;
851
852         pn_skb_get_dst_sockaddr(skb, &dst);
853
854         /* Look for an existing pipe handle */
855         sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
856         if (sknode)
857                 return sk_receive_skb(sknode, skb, 1);
858
859         /* Look for a pipe handle pending accept */
860         sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
861         if (sknode) {
862                 sock_put(sknode);
863                 if (net_ratelimit())
864                         printk(KERN_WARNING"Phonet unconnected PEP ignored");
865                 err = NET_RX_DROP;
866                 goto drop;
867         }
868
869         switch (hdr->message_id) {
870         case PNS_PEP_CONNECT_REQ:
871                 err = pep_connreq_rcv(sk, skb);
872                 break;
873
874 #ifdef CONFIG_PHONET_PIPECTRLR
875         case PNS_PEP_CONNECT_RESP:
876                 err = pep_connresp_rcv(sk, skb);
877                 break;
878 #endif
879
880         case PNS_PEP_DISCONNECT_REQ:
881                 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
882                 break;
883
884         case PNS_PEP_CTRL_REQ:
885                 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
886                 break;
887
888         case PNS_PEP_RESET_REQ:
889         case PNS_PEP_ENABLE_REQ:
890         case PNS_PEP_DISABLE_REQ:
891                 /* invalid handle is not even allowed here! */
892         default:
893                 err = NET_RX_DROP;
894         }
895 drop:
896         kfree_skb(skb);
897         return err;
898 }
899
900 static int pipe_do_remove(struct sock *sk)
901 {
902         struct pep_sock *pn = pep_sk(sk);
903         struct pnpipehdr *ph;
904         struct sk_buff *skb;
905
906         skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_KERNEL);
907         if (!skb)
908                 return -ENOMEM;
909
910         skb_reserve(skb, MAX_PNPIPE_HEADER);
911         __skb_push(skb, sizeof(*ph));
912         skb_reset_transport_header(skb);
913         ph = pnp_hdr(skb);
914         ph->utid = 0;
915         ph->message_id = PNS_PIPE_REMOVE_REQ;
916         ph->pipe_handle = pn->pipe_handle;
917         ph->data[0] = PAD;
918
919         return pn_skb_send(sk, skb, NULL);
920 }
921
922 /* associated socket ceases to exist */
923 static void pep_sock_close(struct sock *sk, long timeout)
924 {
925         struct pep_sock *pn = pep_sk(sk);
926         int ifindex = 0;
927
928         sock_hold(sk); /* keep a reference after sk_common_release() */
929         sk_common_release(sk);
930
931         lock_sock(sk);
932         if (sk->sk_state == TCP_LISTEN) {
933                 /* Destroy the listen queue */
934                 struct sock *sknode;
935                 struct hlist_node *p, *n;
936
937                 sk_for_each_safe(sknode, p, n, &pn->ackq)
938                         sk_del_node_init(sknode);
939                 sk->sk_state = TCP_CLOSE;
940         } else if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
941 #ifndef CONFIG_PHONET_PIPECTRLR
942                 /* Forcefully remove dangling Phonet pipe */
943                 pipe_do_remove(sk);
944 #else
945                 /* send pep disconnect request */
946                 pipe_handler_send_req(sk,
947                                 PNS_PEP_DISCONNECT_UTID, PNS_PEP_DISCONNECT_REQ,
948                                 GFP_KERNEL);
949                 sk->sk_state = TCP_CLOSE;
950 #endif
951         }
952
953         ifindex = pn->ifindex;
954         pn->ifindex = 0;
955         release_sock(sk);
956
957         if (ifindex)
958                 gprs_detach(sk);
959         sock_put(sk);
960 }
961
962 static int pep_wait_connreq(struct sock *sk, int noblock)
963 {
964         struct task_struct *tsk = current;
965         struct pep_sock *pn = pep_sk(sk);
966         long timeo = sock_rcvtimeo(sk, noblock);
967
968         for (;;) {
969                 DEFINE_WAIT(wait);
970
971                 if (sk->sk_state != TCP_LISTEN)
972                         return -EINVAL;
973                 if (!hlist_empty(&pn->ackq))
974                         break;
975                 if (!timeo)
976                         return -EWOULDBLOCK;
977                 if (signal_pending(tsk))
978                         return sock_intr_errno(timeo);
979
980                 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
981                                                 TASK_INTERRUPTIBLE);
982                 release_sock(sk);
983                 timeo = schedule_timeout(timeo);
984                 lock_sock(sk);
985                 finish_wait(sk_sleep(sk), &wait);
986         }
987
988         return 0;
989 }
990
991 static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
992 {
993         struct pep_sock *pn = pep_sk(sk);
994         struct sock *newsk = NULL;
995         struct sk_buff *oskb;
996         int err;
997
998         lock_sock(sk);
999         err = pep_wait_connreq(sk, flags & O_NONBLOCK);
1000         if (err)
1001                 goto out;
1002
1003         newsk = __sk_head(&pn->ackq);
1004
1005         oskb = skb_dequeue(&newsk->sk_receive_queue);
1006         err = pep_accept_conn(newsk, oskb);
1007         if (err) {
1008                 skb_queue_head(&newsk->sk_receive_queue, oskb);
1009                 newsk = NULL;
1010                 goto out;
1011         }
1012         kfree_skb(oskb);
1013
1014         sock_hold(sk);
1015         pep_sk(newsk)->listener = sk;
1016
1017         sock_hold(newsk);
1018         sk_del_node_init(newsk);
1019         sk_acceptq_removed(sk);
1020         sk_add_node(newsk, &pn->hlist);
1021         __sock_put(newsk);
1022
1023 out:
1024         release_sock(sk);
1025         *errp = err;
1026         return newsk;
1027 }
1028
1029 #ifdef CONFIG_PHONET_PIPECTRLR
1030 static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
1031 {
1032         struct pep_sock *pn = pep_sk(sk);
1033         const struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
1034
1035         pn->pn_sk.dobject = pn_sockaddr_get_object(spn);
1036         pn->pn_sk.resource = pn_sockaddr_get_resource(spn);
1037         return pipe_handler_send_req(sk,
1038                         PNS_PEP_CONNECT_UTID, PNS_PEP_CONNECT_REQ,
1039                         GFP_ATOMIC);
1040 }
1041 #endif
1042
1043 static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
1044 {
1045         struct pep_sock *pn = pep_sk(sk);
1046         int answ;
1047
1048         switch (cmd) {
1049         case SIOCINQ:
1050                 if (sk->sk_state == TCP_LISTEN)
1051                         return -EINVAL;
1052
1053                 lock_sock(sk);
1054                 if (sock_flag(sk, SOCK_URGINLINE) &&
1055                     !skb_queue_empty(&pn->ctrlreq_queue))
1056                         answ = skb_peek(&pn->ctrlreq_queue)->len;
1057                 else if (!skb_queue_empty(&sk->sk_receive_queue))
1058                         answ = skb_peek(&sk->sk_receive_queue)->len;
1059                 else
1060                         answ = 0;
1061                 release_sock(sk);
1062                 return put_user(answ, (int __user *)arg);
1063         }
1064
1065         return -ENOIOCTLCMD;
1066 }
1067
1068 static int pep_init(struct sock *sk)
1069 {
1070         struct pep_sock *pn = pep_sk(sk);
1071
1072         INIT_HLIST_HEAD(&pn->ackq);
1073         INIT_HLIST_HEAD(&pn->hlist);
1074         skb_queue_head_init(&pn->ctrlreq_queue);
1075         pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
1076         return 0;
1077 }
1078
1079 static int pep_setsockopt(struct sock *sk, int level, int optname,
1080                                 char __user *optval, unsigned int optlen)
1081 {
1082         struct pep_sock *pn = pep_sk(sk);
1083         int val = 0, err = 0;
1084
1085         if (level != SOL_PNPIPE)
1086                 return -ENOPROTOOPT;
1087         if (optlen >= sizeof(int)) {
1088                 if (get_user(val, (int __user *) optval))
1089                         return -EFAULT;
1090         }
1091
1092         lock_sock(sk);
1093         switch (optname) {
1094 #ifdef CONFIG_PHONET_PIPECTRLR
1095         case PNPIPE_PIPE_HANDLE:
1096                 if (val) {
1097                         pn->pipe_handle = val;
1098                         break;
1099                 }
1100 #endif
1101
1102         case PNPIPE_ENCAP:
1103                 if (val && val != PNPIPE_ENCAP_IP) {
1104                         err = -EINVAL;
1105                         break;
1106                 }
1107                 if (!pn->ifindex == !val)
1108                         break; /* Nothing to do! */
1109                 if (!capable(CAP_NET_ADMIN)) {
1110                         err = -EPERM;
1111                         break;
1112                 }
1113                 if (val) {
1114                         release_sock(sk);
1115                         err = gprs_attach(sk);
1116                         if (err > 0) {
1117                                 pn->ifindex = err;
1118                                 err = 0;
1119                         }
1120                 } else {
1121                         pn->ifindex = 0;
1122                         release_sock(sk);
1123                         gprs_detach(sk);
1124                         err = 0;
1125                 }
1126                 goto out_norel;
1127
1128 #ifdef CONFIG_PHONET_PIPECTRLR
1129         case PNPIPE_ENABLE:
1130                 if ((1 << sk->sk_state) & ~(TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
1131                         err = -ENOTCONN;
1132                         break;
1133                 }
1134                 err = pipe_handler_enable_pipe(sk, val);
1135                 break;
1136 #endif
1137
1138         default:
1139                 err = -ENOPROTOOPT;
1140         }
1141         release_sock(sk);
1142
1143 out_norel:
1144         return err;
1145 }
1146
1147 static int pep_getsockopt(struct sock *sk, int level, int optname,
1148                                 char __user *optval, int __user *optlen)
1149 {
1150         struct pep_sock *pn = pep_sk(sk);
1151         int len, val;
1152
1153         if (level != SOL_PNPIPE)
1154                 return -ENOPROTOOPT;
1155         if (get_user(len, optlen))
1156                 return -EFAULT;
1157
1158         switch (optname) {
1159         case PNPIPE_ENCAP:
1160                 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
1161                 break;
1162
1163         case PNPIPE_IFINDEX:
1164                 val = pn->ifindex;
1165                 break;
1166
1167 #ifdef CONFIG_PHONET_PIPECTRLR
1168         case PNPIPE_ENABLE:
1169                 val = sk->sk_state == TCP_ESTABLISHED;
1170                 break;
1171 #endif
1172
1173         default:
1174                 return -ENOPROTOOPT;
1175         }
1176
1177         len = min_t(unsigned int, sizeof(int), len);
1178         if (put_user(len, optlen))
1179                 return -EFAULT;
1180         if (put_user(val, (int __user *) optval))
1181                 return -EFAULT;
1182         return 0;
1183 }
1184
1185 static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1186 {
1187         struct pep_sock *pn = pep_sk(sk);
1188         struct pnpipehdr *ph;
1189         int err;
1190
1191         if (pn_flow_safe(pn->tx_fc) &&
1192             !atomic_add_unless(&pn->tx_credits, -1, 0)) {
1193                 kfree_skb(skb);
1194                 return -ENOBUFS;
1195         }
1196
1197         skb_push(skb, 3 + pn->aligned);
1198         skb_reset_transport_header(skb);
1199         ph = pnp_hdr(skb);
1200         ph->utid = 0;
1201         if (pn->aligned) {
1202                 ph->message_id = PNS_PIPE_ALIGNED_DATA;
1203                 ph->data[0] = 0; /* padding */
1204         } else
1205                 ph->message_id = PNS_PIPE_DATA;
1206         ph->pipe_handle = pn->pipe_handle;
1207         err = pn_skb_send(sk, skb, NULL);
1208
1209         if (err && pn_flow_safe(pn->tx_fc))
1210                 atomic_inc(&pn->tx_credits);
1211         return err;
1212
1213 }
1214
1215 static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
1216                         struct msghdr *msg, size_t len)
1217 {
1218         struct pep_sock *pn = pep_sk(sk);
1219         struct sk_buff *skb;
1220         long timeo;
1221         int flags = msg->msg_flags;
1222         int err, done;
1223
1224         if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
1225                                 MSG_CMSG_COMPAT)) ||
1226                         !(msg->msg_flags & MSG_EOR))
1227                 return -EOPNOTSUPP;
1228
1229         skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
1230                                         flags & MSG_DONTWAIT, &err);
1231         if (!skb)
1232                 return err;
1233
1234         skb_reserve(skb, MAX_PHONET_HEADER + 3);
1235         err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1236         if (err < 0)
1237                 goto outfree;
1238
1239         lock_sock(sk);
1240         timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1241         if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
1242                 err = -ENOTCONN;
1243                 goto out;
1244         }
1245         if (sk->sk_state != TCP_ESTABLISHED) {
1246                 /* Wait until the pipe gets to enabled state */
1247 disabled:
1248                 err = sk_stream_wait_connect(sk, &timeo);
1249                 if (err)
1250                         goto out;
1251
1252                 if (sk->sk_state == TCP_CLOSE_WAIT) {
1253                         err = -ECONNRESET;
1254                         goto out;
1255                 }
1256         }
1257         BUG_ON(sk->sk_state != TCP_ESTABLISHED);
1258
1259         /* Wait until flow control allows TX */
1260         done = atomic_read(&pn->tx_credits);
1261         while (!done) {
1262                 DEFINE_WAIT(wait);
1263
1264                 if (!timeo) {
1265                         err = -EAGAIN;
1266                         goto out;
1267                 }
1268                 if (signal_pending(current)) {
1269                         err = sock_intr_errno(timeo);
1270                         goto out;
1271                 }
1272
1273                 prepare_to_wait(sk_sleep(sk), &wait,
1274                                 TASK_INTERRUPTIBLE);
1275                 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
1276                 finish_wait(sk_sleep(sk), &wait);
1277
1278                 if (sk->sk_state != TCP_ESTABLISHED)
1279                         goto disabled;
1280         }
1281
1282         err = pipe_skb_send(sk, skb);
1283         if (err >= 0)
1284                 err = len; /* success! */
1285         skb = NULL;
1286 out:
1287         release_sock(sk);
1288 outfree:
1289         kfree_skb(skb);
1290         return err;
1291 }
1292
1293 int pep_writeable(struct sock *sk)
1294 {
1295         struct pep_sock *pn = pep_sk(sk);
1296
1297         return atomic_read(&pn->tx_credits);
1298 }
1299
1300 int pep_write(struct sock *sk, struct sk_buff *skb)
1301 {
1302         struct sk_buff *rskb, *fs;
1303         int flen = 0;
1304
1305         if (pep_sk(sk)->aligned)
1306                 return pipe_skb_send(sk, skb);
1307
1308         rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
1309         if (!rskb) {
1310                 kfree_skb(skb);
1311                 return -ENOMEM;
1312         }
1313         skb_shinfo(rskb)->frag_list = skb;
1314         rskb->len += skb->len;
1315         rskb->data_len += rskb->len;
1316         rskb->truesize += rskb->len;
1317
1318         /* Avoid nested fragments */
1319         skb_walk_frags(skb, fs)
1320                 flen += fs->len;
1321         skb->next = skb_shinfo(skb)->frag_list;
1322         skb_frag_list_init(skb);
1323         skb->len -= flen;
1324         skb->data_len -= flen;
1325         skb->truesize -= flen;
1326
1327         skb_reserve(rskb, MAX_PHONET_HEADER + 3);
1328         return pipe_skb_send(sk, rskb);
1329 }
1330
1331 struct sk_buff *pep_read(struct sock *sk)
1332 {
1333         struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1334
1335         if (sk->sk_state == TCP_ESTABLISHED)
1336                 pipe_grant_credits(sk);
1337         return skb;
1338 }
1339
1340 static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
1341                         struct msghdr *msg, size_t len, int noblock,
1342                         int flags, int *addr_len)
1343 {
1344         struct sk_buff *skb;
1345         int err;
1346
1347         if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
1348                         MSG_NOSIGNAL|MSG_CMSG_COMPAT))
1349                 return -EOPNOTSUPP;
1350
1351         if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
1352                 return -ENOTCONN;
1353
1354         if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
1355                 /* Dequeue and acknowledge control request */
1356                 struct pep_sock *pn = pep_sk(sk);
1357
1358                 if (flags & MSG_PEEK)
1359                         return -EOPNOTSUPP;
1360                 skb = skb_dequeue(&pn->ctrlreq_queue);
1361                 if (skb) {
1362                         pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1363                                                 GFP_KERNEL);
1364                         msg->msg_flags |= MSG_OOB;
1365                         goto copy;
1366                 }
1367                 if (flags & MSG_OOB)
1368                         return -EINVAL;
1369         }
1370
1371         skb = skb_recv_datagram(sk, flags, noblock, &err);
1372         lock_sock(sk);
1373         if (skb == NULL) {
1374                 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1375                         err = -ECONNRESET;
1376                 release_sock(sk);
1377                 return err;
1378         }
1379
1380         if (sk->sk_state == TCP_ESTABLISHED)
1381                 pipe_grant_credits(sk);
1382         release_sock(sk);
1383 copy:
1384         msg->msg_flags |= MSG_EOR;
1385         if (skb->len > len)
1386                 msg->msg_flags |= MSG_TRUNC;
1387         else
1388                 len = skb->len;
1389
1390         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1391         if (!err)
1392                 err = (flags & MSG_TRUNC) ? skb->len : len;
1393
1394         skb_free_datagram(sk, skb);
1395         return err;
1396 }
1397
1398 static void pep_sock_unhash(struct sock *sk)
1399 {
1400         struct pep_sock *pn = pep_sk(sk);
1401         struct sock *skparent = NULL;
1402
1403         lock_sock(sk);
1404
1405 #ifndef CONFIG_PHONET_PIPECTRLR
1406         if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1407                 skparent = pn->listener;
1408                 release_sock(sk);
1409
1410                 pn = pep_sk(skparent);
1411                 lock_sock(skparent);
1412                 sk_del_node_init(sk);
1413                 sk = skparent;
1414         }
1415 #endif
1416         /* Unhash a listening sock only when it is closed
1417          * and all of its active connected pipes are closed. */
1418         if (hlist_empty(&pn->hlist))
1419                 pn_sock_unhash(&pn->pn_sk.sk);
1420         release_sock(sk);
1421
1422         if (skparent)
1423                 sock_put(skparent);
1424 }
1425
1426 static struct proto pep_proto = {
1427         .close          = pep_sock_close,
1428         .accept         = pep_sock_accept,
1429 #ifdef CONFIG_PHONET_PIPECTRLR
1430         .connect        = pep_sock_connect,
1431 #endif
1432         .ioctl          = pep_ioctl,
1433         .init           = pep_init,
1434         .setsockopt     = pep_setsockopt,
1435         .getsockopt     = pep_getsockopt,
1436         .sendmsg        = pep_sendmsg,
1437         .recvmsg        = pep_recvmsg,
1438         .backlog_rcv    = pep_do_rcv,
1439         .hash           = pn_sock_hash,
1440         .unhash         = pep_sock_unhash,
1441         .get_port       = pn_sock_get_port,
1442         .obj_size       = sizeof(struct pep_sock),
1443         .owner          = THIS_MODULE,
1444         .name           = "PNPIPE",
1445 };
1446
1447 static struct phonet_protocol pep_pn_proto = {
1448         .ops            = &phonet_stream_ops,
1449         .prot           = &pep_proto,
1450         .sock_type      = SOCK_SEQPACKET,
1451 };
1452
1453 static int __init pep_register(void)
1454 {
1455         return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1456 }
1457
1458 static void __exit pep_unregister(void)
1459 {
1460         phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1461 }
1462
1463 module_init(pep_register);
1464 module_exit(pep_unregister);
1465 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1466 MODULE_DESCRIPTION("Phonet pipe protocol");
1467 MODULE_LICENSE("GPL");
1468 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);