Phonet: use socket destination in pipe protocol
[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                 pn->pipe_state = PIPE_IDLE;
531                 sk->sk_state = TCP_CLOSE;
532                 break;
533 #endif
534
535         case PNS_PEP_ENABLE_REQ:
536                 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
537                 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
538                 break;
539
540 #ifdef CONFIG_PHONET_PIPECTRLR
541         case PNS_PEP_ENABLE_RESP:
542                 pn->pipe_state = PIPE_ENABLED;
543                 pipe_handler_send_ind(sk, PNS_PIPE_ENABLED_IND_UTID,
544                                 PNS_PIPE_ENABLED_IND);
545
546                 if (!pn_flow_safe(pn->tx_fc)) {
547                         atomic_set(&pn->tx_credits, 1);
548                         sk->sk_write_space(sk);
549                 }
550                 if (sk->sk_state == TCP_ESTABLISHED)
551                         break; /* Nothing to do */
552                 sk->sk_state = TCP_ESTABLISHED;
553                 pipe_grant_credits(sk);
554                 break;
555 #endif
556
557         case PNS_PEP_RESET_REQ:
558                 switch (hdr->state_after_reset) {
559                 case PN_PIPE_DISABLE:
560                         pn->init_enable = 0;
561                         break;
562                 case PN_PIPE_ENABLE:
563                         pn->init_enable = 1;
564                         break;
565                 default: /* not allowed to send an error here!? */
566                         err = -EINVAL;
567                         goto out;
568                 }
569                 /* fall through */
570         case PNS_PEP_DISABLE_REQ:
571                 atomic_set(&pn->tx_credits, 0);
572                 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
573                 break;
574
575 #ifdef CONFIG_PHONET_PIPECTRLR
576         case PNS_PEP_DISABLE_RESP:
577                 pn->pipe_state = PIPE_DISABLED;
578                 atomic_set(&pn->tx_credits, 0);
579                 pipe_handler_send_ind(sk, PNS_PIPE_DISABLED_IND_UTID,
580                                 PNS_PIPE_DISABLED_IND);
581                 sk->sk_state = TCP_SYN_RECV;
582                 pn->rx_credits = 0;
583                 break;
584 #endif
585
586         case PNS_PEP_CTRL_REQ:
587                 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
588                         atomic_inc(&sk->sk_drops);
589                         break;
590                 }
591                 __skb_pull(skb, 4);
592                 queue = &pn->ctrlreq_queue;
593                 goto queue;
594
595         case PNS_PIPE_ALIGNED_DATA:
596                 __skb_pull(skb, 1);
597                 /* fall through */
598         case PNS_PIPE_DATA:
599                 __skb_pull(skb, 3); /* Pipe data header */
600                 if (!pn_flow_safe(pn->rx_fc)) {
601                         err = sock_queue_rcv_skb(sk, skb);
602                         if (!err)
603                                 return 0;
604                         break;
605                 }
606
607                 if (pn->rx_credits == 0) {
608                         atomic_inc(&sk->sk_drops);
609                         err = -ENOBUFS;
610                         break;
611                 }
612                 pn->rx_credits--;
613                 queue = &sk->sk_receive_queue;
614                 goto queue;
615
616         case PNS_PEP_STATUS_IND:
617                 pipe_rcv_status(sk, skb);
618                 break;
619
620         case PNS_PIPE_REDIRECTED_IND:
621                 err = pipe_rcv_created(sk, skb);
622                 break;
623
624         case PNS_PIPE_CREATED_IND:
625                 err = pipe_rcv_created(sk, skb);
626                 if (err)
627                         break;
628                 /* fall through */
629         case PNS_PIPE_RESET_IND:
630                 if (!pn->init_enable)
631                         break;
632                 /* fall through */
633         case PNS_PIPE_ENABLED_IND:
634                 if (!pn_flow_safe(pn->tx_fc)) {
635                         atomic_set(&pn->tx_credits, 1);
636                         sk->sk_write_space(sk);
637                 }
638                 if (sk->sk_state == TCP_ESTABLISHED)
639                         break; /* Nothing to do */
640                 sk->sk_state = TCP_ESTABLISHED;
641                 pipe_grant_credits(sk);
642                 break;
643
644         case PNS_PIPE_DISABLED_IND:
645                 sk->sk_state = TCP_SYN_RECV;
646                 pn->rx_credits = 0;
647                 break;
648
649         default:
650                 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
651                                 hdr->message_id);
652                 err = -EINVAL;
653         }
654 out:
655         kfree_skb(skb);
656         return err;
657
658 queue:
659         skb->dev = NULL;
660         skb_set_owner_r(skb, sk);
661         err = skb->len;
662         skb_queue_tail(queue, skb);
663         if (!sock_flag(sk, SOCK_DEAD))
664                 sk->sk_data_ready(sk, err);
665         return 0;
666 }
667
668 /* Destroy connected sock. */
669 static void pipe_destruct(struct sock *sk)
670 {
671         struct pep_sock *pn = pep_sk(sk);
672
673         skb_queue_purge(&sk->sk_receive_queue);
674         skb_queue_purge(&pn->ctrlreq_queue);
675 }
676
677 #ifdef CONFIG_PHONET_PIPECTRLR
678 static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
679 {
680         struct pep_sock *pn = pep_sk(sk);
681         u8 host_pref_rx_fc[3] = {3, 2, 1}, host_req_tx_fc[3] = {3, 2, 1};
682         u8 remote_pref_rx_fc[3], remote_req_tx_fc[3];
683         u8 negotiated_rx_fc, negotiated_tx_fc;
684         int ret;
685
686         pipe_get_flow_info(sk, skb, remote_pref_rx_fc,
687                         remote_req_tx_fc);
688         negotiated_tx_fc = pipe_negotiate_fc(remote_req_tx_fc,
689                         host_pref_rx_fc,
690                         sizeof(host_pref_rx_fc));
691         negotiated_rx_fc = pipe_negotiate_fc(host_req_tx_fc,
692                         remote_pref_rx_fc,
693                         sizeof(host_pref_rx_fc));
694
695         pn->pipe_state = PIPE_DISABLED;
696         sk->sk_state = TCP_SYN_RECV;
697         sk->sk_backlog_rcv = pipe_do_rcv;
698         sk->sk_destruct = pipe_destruct;
699         pn->rx_credits = 0;
700         pn->rx_fc = negotiated_rx_fc;
701         pn->tx_fc = negotiated_tx_fc;
702         sk->sk_state_change(sk);
703
704         ret = pipe_handler_send_created_ind(sk,
705                         PNS_PIPE_CREATED_IND_UTID,
706                         PNS_PIPE_CREATED_IND
707                         );
708
709         return ret;
710 }
711 #endif
712
713 static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
714 {
715         struct sock *newsk;
716         struct pep_sock *newpn, *pn = pep_sk(sk);
717         struct pnpipehdr *hdr;
718         struct sockaddr_pn dst, src;
719         u16 peer_type;
720         u8 pipe_handle, enabled, n_sb;
721         u8 aligned = 0;
722
723         if (!pskb_pull(skb, sizeof(*hdr) + 4))
724                 return -EINVAL;
725
726         hdr = pnp_hdr(skb);
727         pipe_handle = hdr->pipe_handle;
728         switch (hdr->state_after_connect) {
729         case PN_PIPE_DISABLE:
730                 enabled = 0;
731                 break;
732         case PN_PIPE_ENABLE:
733                 enabled = 1;
734                 break;
735         default:
736                 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
737                 return -EINVAL;
738         }
739         peer_type = hdr->other_pep_type << 8;
740
741         if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) {
742                 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
743                 return -ENOBUFS;
744         }
745
746         /* Parse sub-blocks (options) */
747         n_sb = hdr->data[4];
748         while (n_sb > 0) {
749                 u8 type, buf[1], len = sizeof(buf);
750                 const u8 *data = pep_get_sb(skb, &type, &len, buf);
751
752                 if (data == NULL)
753                         return -EINVAL;
754                 switch (type) {
755                 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
756                         if (len < 1)
757                                 return -EINVAL;
758                         peer_type = (peer_type & 0xff00) | data[0];
759                         break;
760                 case PN_PIPE_SB_ALIGNED_DATA:
761                         aligned = data[0] != 0;
762                         break;
763                 }
764                 n_sb--;
765         }
766
767         skb = skb_clone(skb, GFP_ATOMIC);
768         if (!skb)
769                 return -ENOMEM;
770
771         /* Create a new to-be-accepted sock */
772         newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
773         if (!newsk) {
774                 kfree_skb(skb);
775                 return -ENOMEM;
776         }
777         sock_init_data(NULL, newsk);
778         newsk->sk_state = TCP_SYN_RECV;
779         newsk->sk_backlog_rcv = pipe_do_rcv;
780         newsk->sk_protocol = sk->sk_protocol;
781         newsk->sk_destruct = pipe_destruct;
782
783         newpn = pep_sk(newsk);
784         pn_skb_get_dst_sockaddr(skb, &dst);
785         pn_skb_get_src_sockaddr(skb, &src);
786         newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
787         newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
788         newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
789         skb_queue_head_init(&newpn->ctrlreq_queue);
790         newpn->pipe_handle = pipe_handle;
791         atomic_set(&newpn->tx_credits, 0);
792         newpn->peer_type = peer_type;
793         newpn->rx_credits = 0;
794         newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
795         newpn->init_enable = enabled;
796         newpn->aligned = aligned;
797
798         BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
799         skb_queue_head(&newsk->sk_receive_queue, skb);
800         if (!sock_flag(sk, SOCK_DEAD))
801                 sk->sk_data_ready(sk, 0);
802
803         sk_acceptq_added(sk);
804         sk_add_node(newsk, &pn->ackq);
805         return 0;
806 }
807
808 /* Listening sock must be locked */
809 static struct sock *pep_find_pipe(const struct hlist_head *hlist,
810                                         const struct sockaddr_pn *dst,
811                                         u8 pipe_handle)
812 {
813         struct hlist_node *node;
814         struct sock *sknode;
815         u16 dobj = pn_sockaddr_get_object(dst);
816
817         sk_for_each(sknode, node, hlist) {
818                 struct pep_sock *pnnode = pep_sk(sknode);
819
820                 /* Ports match, but addresses might not: */
821                 if (pnnode->pn_sk.sobject != dobj)
822                         continue;
823                 if (pnnode->pipe_handle != pipe_handle)
824                         continue;
825                 if (sknode->sk_state == TCP_CLOSE_WAIT)
826                         continue;
827
828                 sock_hold(sknode);
829                 return sknode;
830         }
831         return NULL;
832 }
833
834 /*
835  * Deliver an skb to a listening sock.
836  * Socket lock must be held.
837  * We then queue the skb to the right connected sock (if any).
838  */
839 static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
840 {
841         struct pep_sock *pn = pep_sk(sk);
842         struct sock *sknode;
843         struct pnpipehdr *hdr;
844         struct sockaddr_pn dst;
845         int err = NET_RX_SUCCESS;
846         u8 pipe_handle;
847
848         if (!pskb_may_pull(skb, sizeof(*hdr)))
849                 goto drop;
850
851         hdr = pnp_hdr(skb);
852         pipe_handle = hdr->pipe_handle;
853         if (pipe_handle == PN_PIPE_INVALID_HANDLE)
854                 goto drop;
855
856         pn_skb_get_dst_sockaddr(skb, &dst);
857
858         /* Look for an existing pipe handle */
859         sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
860         if (sknode)
861                 return sk_receive_skb(sknode, skb, 1);
862
863         /* Look for a pipe handle pending accept */
864         sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
865         if (sknode) {
866                 sock_put(sknode);
867                 if (net_ratelimit())
868                         printk(KERN_WARNING"Phonet unconnected PEP ignored");
869                 err = NET_RX_DROP;
870                 goto drop;
871         }
872
873         switch (hdr->message_id) {
874         case PNS_PEP_CONNECT_REQ:
875                 err = pep_connreq_rcv(sk, skb);
876                 break;
877
878 #ifdef CONFIG_PHONET_PIPECTRLR
879         case PNS_PEP_CONNECT_RESP:
880                 err = pep_connresp_rcv(sk, skb);
881                 break;
882 #endif
883
884         case PNS_PEP_DISCONNECT_REQ:
885                 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
886                 break;
887
888         case PNS_PEP_CTRL_REQ:
889                 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
890                 break;
891
892         case PNS_PEP_RESET_REQ:
893         case PNS_PEP_ENABLE_REQ:
894         case PNS_PEP_DISABLE_REQ:
895                 /* invalid handle is not even allowed here! */
896         default:
897                 err = NET_RX_DROP;
898         }
899 drop:
900         kfree_skb(skb);
901         return err;
902 }
903
904 static int pipe_do_remove(struct sock *sk)
905 {
906         struct pep_sock *pn = pep_sk(sk);
907         struct pnpipehdr *ph;
908         struct sk_buff *skb;
909
910         skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_KERNEL);
911         if (!skb)
912                 return -ENOMEM;
913
914         skb_reserve(skb, MAX_PNPIPE_HEADER);
915         __skb_push(skb, sizeof(*ph));
916         skb_reset_transport_header(skb);
917         ph = pnp_hdr(skb);
918         ph->utid = 0;
919         ph->message_id = PNS_PIPE_REMOVE_REQ;
920         ph->pipe_handle = pn->pipe_handle;
921         ph->data[0] = PAD;
922
923         return pn_skb_send(sk, skb, NULL);
924 }
925
926 /* associated socket ceases to exist */
927 static void pep_sock_close(struct sock *sk, long timeout)
928 {
929         struct pep_sock *pn = pep_sk(sk);
930         int ifindex = 0;
931
932         sock_hold(sk); /* keep a reference after sk_common_release() */
933         sk_common_release(sk);
934
935         lock_sock(sk);
936         if (sk->sk_state == TCP_LISTEN) {
937                 /* Destroy the listen queue */
938                 struct sock *sknode;
939                 struct hlist_node *p, *n;
940
941                 sk_for_each_safe(sknode, p, n, &pn->ackq)
942                         sk_del_node_init(sknode);
943                 sk->sk_state = TCP_CLOSE;
944         } else if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED))
945                 /* Forcefully remove dangling Phonet pipe */
946                 pipe_do_remove(sk);
947
948 #ifdef CONFIG_PHONET_PIPECTRLR
949         if (pn->pipe_state != PIPE_IDLE) {
950                 /* send pep disconnect request */
951                 pipe_handler_send_req(sk,
952                                 PNS_PEP_DISCONNECT_UTID, PNS_PEP_DISCONNECT_REQ,
953                                 GFP_KERNEL);
954
955                 pn->pipe_state = PIPE_IDLE;
956                 sk->sk_state = TCP_CLOSE;
957         }
958 #endif
959
960         ifindex = pn->ifindex;
961         pn->ifindex = 0;
962         release_sock(sk);
963
964         if (ifindex)
965                 gprs_detach(sk);
966         sock_put(sk);
967 }
968
969 static int pep_wait_connreq(struct sock *sk, int noblock)
970 {
971         struct task_struct *tsk = current;
972         struct pep_sock *pn = pep_sk(sk);
973         long timeo = sock_rcvtimeo(sk, noblock);
974
975         for (;;) {
976                 DEFINE_WAIT(wait);
977
978                 if (sk->sk_state != TCP_LISTEN)
979                         return -EINVAL;
980                 if (!hlist_empty(&pn->ackq))
981                         break;
982                 if (!timeo)
983                         return -EWOULDBLOCK;
984                 if (signal_pending(tsk))
985                         return sock_intr_errno(timeo);
986
987                 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
988                                                 TASK_INTERRUPTIBLE);
989                 release_sock(sk);
990                 timeo = schedule_timeout(timeo);
991                 lock_sock(sk);
992                 finish_wait(sk_sleep(sk), &wait);
993         }
994
995         return 0;
996 }
997
998 static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
999 {
1000         struct pep_sock *pn = pep_sk(sk);
1001         struct sock *newsk = NULL;
1002         struct sk_buff *oskb;
1003         int err;
1004
1005         lock_sock(sk);
1006         err = pep_wait_connreq(sk, flags & O_NONBLOCK);
1007         if (err)
1008                 goto out;
1009
1010         newsk = __sk_head(&pn->ackq);
1011
1012         oskb = skb_dequeue(&newsk->sk_receive_queue);
1013         err = pep_accept_conn(newsk, oskb);
1014         if (err) {
1015                 skb_queue_head(&newsk->sk_receive_queue, oskb);
1016                 newsk = NULL;
1017                 goto out;
1018         }
1019         kfree_skb(oskb);
1020
1021         sock_hold(sk);
1022         pep_sk(newsk)->listener = sk;
1023
1024         sock_hold(newsk);
1025         sk_del_node_init(newsk);
1026         sk_acceptq_removed(sk);
1027         sk_add_node(newsk, &pn->hlist);
1028         __sock_put(newsk);
1029
1030 out:
1031         release_sock(sk);
1032         *errp = err;
1033         return newsk;
1034 }
1035
1036 #ifdef CONFIG_PHONET_PIPECTRLR
1037 static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
1038 {
1039         struct pep_sock *pn = pep_sk(sk);
1040         const struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
1041
1042         pn->pn_sk.dobject = pn_sockaddr_get_object(spn);
1043         pn->pn_sk.resource = pn_sockaddr_get_resource(spn);
1044         return pipe_handler_send_req(sk,
1045                         PNS_PEP_CONNECT_UTID, PNS_PEP_CONNECT_REQ,
1046                         GFP_ATOMIC);
1047 }
1048 #endif
1049
1050 static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
1051 {
1052         struct pep_sock *pn = pep_sk(sk);
1053         int answ;
1054
1055         switch (cmd) {
1056         case SIOCINQ:
1057                 if (sk->sk_state == TCP_LISTEN)
1058                         return -EINVAL;
1059
1060                 lock_sock(sk);
1061                 if (sock_flag(sk, SOCK_URGINLINE) &&
1062                     !skb_queue_empty(&pn->ctrlreq_queue))
1063                         answ = skb_peek(&pn->ctrlreq_queue)->len;
1064                 else if (!skb_queue_empty(&sk->sk_receive_queue))
1065                         answ = skb_peek(&sk->sk_receive_queue)->len;
1066                 else
1067                         answ = 0;
1068                 release_sock(sk);
1069                 return put_user(answ, (int __user *)arg);
1070         }
1071
1072         return -ENOIOCTLCMD;
1073 }
1074
1075 static int pep_init(struct sock *sk)
1076 {
1077         struct pep_sock *pn = pep_sk(sk);
1078
1079         INIT_HLIST_HEAD(&pn->ackq);
1080         INIT_HLIST_HEAD(&pn->hlist);
1081         skb_queue_head_init(&pn->ctrlreq_queue);
1082         pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
1083         return 0;
1084 }
1085
1086 static int pep_setsockopt(struct sock *sk, int level, int optname,
1087                                 char __user *optval, unsigned int optlen)
1088 {
1089         struct pep_sock *pn = pep_sk(sk);
1090         int val = 0, err = 0;
1091
1092         if (level != SOL_PNPIPE)
1093                 return -ENOPROTOOPT;
1094         if (optlen >= sizeof(int)) {
1095                 if (get_user(val, (int __user *) optval))
1096                         return -EFAULT;
1097         }
1098
1099         lock_sock(sk);
1100         switch (optname) {
1101 #ifdef CONFIG_PHONET_PIPECTRLR
1102         case PNPIPE_PIPE_HANDLE:
1103                 if (val) {
1104                         if (pn->pipe_state > PIPE_IDLE) {
1105                                 err = -EFAULT;
1106                                 break;
1107                         }
1108                         pn->pipe_handle = val;
1109                         break;
1110                 }
1111 #endif
1112
1113         case PNPIPE_ENCAP:
1114                 if (val && val != PNPIPE_ENCAP_IP) {
1115                         err = -EINVAL;
1116                         break;
1117                 }
1118                 if (!pn->ifindex == !val)
1119                         break; /* Nothing to do! */
1120                 if (!capable(CAP_NET_ADMIN)) {
1121                         err = -EPERM;
1122                         break;
1123                 }
1124                 if (val) {
1125                         release_sock(sk);
1126                         err = gprs_attach(sk);
1127                         if (err > 0) {
1128                                 pn->ifindex = err;
1129                                 err = 0;
1130                         }
1131                 } else {
1132                         pn->ifindex = 0;
1133                         release_sock(sk);
1134                         gprs_detach(sk);
1135                         err = 0;
1136                 }
1137                 goto out_norel;
1138
1139 #ifdef CONFIG_PHONET_PIPECTRLR
1140         case PNPIPE_ENABLE:
1141                 if (pn->pipe_state <= PIPE_IDLE) {
1142                         err = -ENOTCONN;
1143                         break;
1144                 }
1145                 err = pipe_handler_enable_pipe(sk, val);
1146                 break;
1147 #endif
1148
1149         default:
1150                 err = -ENOPROTOOPT;
1151         }
1152         release_sock(sk);
1153
1154 out_norel:
1155         return err;
1156 }
1157
1158 static int pep_getsockopt(struct sock *sk, int level, int optname,
1159                                 char __user *optval, int __user *optlen)
1160 {
1161         struct pep_sock *pn = pep_sk(sk);
1162         int len, val;
1163
1164         if (level != SOL_PNPIPE)
1165                 return -ENOPROTOOPT;
1166         if (get_user(len, optlen))
1167                 return -EFAULT;
1168
1169         switch (optname) {
1170         case PNPIPE_ENCAP:
1171                 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
1172                 break;
1173
1174         case PNPIPE_IFINDEX:
1175                 val = pn->ifindex;
1176                 break;
1177
1178 #ifdef CONFIG_PHONET_PIPECTRLR
1179         case PNPIPE_ENABLE:
1180                 if (pn->pipe_state <= PIPE_IDLE)
1181                         return -ENOTCONN;
1182                 val = pn->pipe_state != PIPE_DISABLED;
1183                 break;
1184 #endif
1185
1186         default:
1187                 return -ENOPROTOOPT;
1188         }
1189
1190         len = min_t(unsigned int, sizeof(int), len);
1191         if (put_user(len, optlen))
1192                 return -EFAULT;
1193         if (put_user(val, (int __user *) optval))
1194                 return -EFAULT;
1195         return 0;
1196 }
1197
1198 static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1199 {
1200         struct pep_sock *pn = pep_sk(sk);
1201         struct pnpipehdr *ph;
1202         int err;
1203
1204         if (pn_flow_safe(pn->tx_fc) &&
1205             !atomic_add_unless(&pn->tx_credits, -1, 0)) {
1206                 kfree_skb(skb);
1207                 return -ENOBUFS;
1208         }
1209
1210         skb_push(skb, 3 + pn->aligned);
1211         skb_reset_transport_header(skb);
1212         ph = pnp_hdr(skb);
1213         ph->utid = 0;
1214         if (pn->aligned) {
1215                 ph->message_id = PNS_PIPE_ALIGNED_DATA;
1216                 ph->data[0] = 0; /* padding */
1217         } else
1218                 ph->message_id = PNS_PIPE_DATA;
1219         ph->pipe_handle = pn->pipe_handle;
1220         err = pn_skb_send(sk, skb, NULL);
1221
1222         if (err && pn_flow_safe(pn->tx_fc))
1223                 atomic_inc(&pn->tx_credits);
1224         return err;
1225
1226 }
1227
1228 static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
1229                         struct msghdr *msg, size_t len)
1230 {
1231         struct pep_sock *pn = pep_sk(sk);
1232         struct sk_buff *skb;
1233         long timeo;
1234         int flags = msg->msg_flags;
1235         int err, done;
1236
1237         if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
1238                                 MSG_CMSG_COMPAT)) ||
1239                         !(msg->msg_flags & MSG_EOR))
1240                 return -EOPNOTSUPP;
1241
1242         skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
1243                                         flags & MSG_DONTWAIT, &err);
1244         if (!skb)
1245                 return err;
1246
1247         skb_reserve(skb, MAX_PHONET_HEADER + 3);
1248         err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1249         if (err < 0)
1250                 goto outfree;
1251
1252         lock_sock(sk);
1253         timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1254         if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
1255                 err = -ENOTCONN;
1256                 goto out;
1257         }
1258         if (sk->sk_state != TCP_ESTABLISHED) {
1259                 /* Wait until the pipe gets to enabled state */
1260 disabled:
1261                 err = sk_stream_wait_connect(sk, &timeo);
1262                 if (err)
1263                         goto out;
1264
1265                 if (sk->sk_state == TCP_CLOSE_WAIT) {
1266                         err = -ECONNRESET;
1267                         goto out;
1268                 }
1269         }
1270         BUG_ON(sk->sk_state != TCP_ESTABLISHED);
1271
1272         /* Wait until flow control allows TX */
1273         done = atomic_read(&pn->tx_credits);
1274         while (!done) {
1275                 DEFINE_WAIT(wait);
1276
1277                 if (!timeo) {
1278                         err = -EAGAIN;
1279                         goto out;
1280                 }
1281                 if (signal_pending(current)) {
1282                         err = sock_intr_errno(timeo);
1283                         goto out;
1284                 }
1285
1286                 prepare_to_wait(sk_sleep(sk), &wait,
1287                                 TASK_INTERRUPTIBLE);
1288                 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
1289                 finish_wait(sk_sleep(sk), &wait);
1290
1291                 if (sk->sk_state != TCP_ESTABLISHED)
1292                         goto disabled;
1293         }
1294
1295         err = pipe_skb_send(sk, skb);
1296         if (err >= 0)
1297                 err = len; /* success! */
1298         skb = NULL;
1299 out:
1300         release_sock(sk);
1301 outfree:
1302         kfree_skb(skb);
1303         return err;
1304 }
1305
1306 int pep_writeable(struct sock *sk)
1307 {
1308         struct pep_sock *pn = pep_sk(sk);
1309
1310         return atomic_read(&pn->tx_credits);
1311 }
1312
1313 int pep_write(struct sock *sk, struct sk_buff *skb)
1314 {
1315         struct sk_buff *rskb, *fs;
1316         int flen = 0;
1317
1318         if (pep_sk(sk)->aligned)
1319                 return pipe_skb_send(sk, skb);
1320
1321         rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
1322         if (!rskb) {
1323                 kfree_skb(skb);
1324                 return -ENOMEM;
1325         }
1326         skb_shinfo(rskb)->frag_list = skb;
1327         rskb->len += skb->len;
1328         rskb->data_len += rskb->len;
1329         rskb->truesize += rskb->len;
1330
1331         /* Avoid nested fragments */
1332         skb_walk_frags(skb, fs)
1333                 flen += fs->len;
1334         skb->next = skb_shinfo(skb)->frag_list;
1335         skb_frag_list_init(skb);
1336         skb->len -= flen;
1337         skb->data_len -= flen;
1338         skb->truesize -= flen;
1339
1340         skb_reserve(rskb, MAX_PHONET_HEADER + 3);
1341         return pipe_skb_send(sk, rskb);
1342 }
1343
1344 struct sk_buff *pep_read(struct sock *sk)
1345 {
1346         struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1347
1348         if (sk->sk_state == TCP_ESTABLISHED)
1349                 pipe_grant_credits(sk);
1350         return skb;
1351 }
1352
1353 static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
1354                         struct msghdr *msg, size_t len, int noblock,
1355                         int flags, int *addr_len)
1356 {
1357         struct sk_buff *skb;
1358         int err;
1359
1360         if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
1361                         MSG_NOSIGNAL|MSG_CMSG_COMPAT))
1362                 return -EOPNOTSUPP;
1363
1364         if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
1365                 return -ENOTCONN;
1366
1367         if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
1368                 /* Dequeue and acknowledge control request */
1369                 struct pep_sock *pn = pep_sk(sk);
1370
1371                 if (flags & MSG_PEEK)
1372                         return -EOPNOTSUPP;
1373                 skb = skb_dequeue(&pn->ctrlreq_queue);
1374                 if (skb) {
1375                         pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1376                                                 GFP_KERNEL);
1377                         msg->msg_flags |= MSG_OOB;
1378                         goto copy;
1379                 }
1380                 if (flags & MSG_OOB)
1381                         return -EINVAL;
1382         }
1383
1384         skb = skb_recv_datagram(sk, flags, noblock, &err);
1385         lock_sock(sk);
1386         if (skb == NULL) {
1387                 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1388                         err = -ECONNRESET;
1389                 release_sock(sk);
1390                 return err;
1391         }
1392
1393         if (sk->sk_state == TCP_ESTABLISHED)
1394                 pipe_grant_credits(sk);
1395         release_sock(sk);
1396 copy:
1397         msg->msg_flags |= MSG_EOR;
1398         if (skb->len > len)
1399                 msg->msg_flags |= MSG_TRUNC;
1400         else
1401                 len = skb->len;
1402
1403         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1404         if (!err)
1405                 err = (flags & MSG_TRUNC) ? skb->len : len;
1406
1407         skb_free_datagram(sk, skb);
1408         return err;
1409 }
1410
1411 static void pep_sock_unhash(struct sock *sk)
1412 {
1413         struct pep_sock *pn = pep_sk(sk);
1414         struct sock *skparent = NULL;
1415
1416         lock_sock(sk);
1417
1418 #ifndef CONFIG_PHONET_PIPECTRLR
1419         if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1420                 skparent = pn->listener;
1421                 release_sock(sk);
1422
1423                 pn = pep_sk(skparent);
1424                 lock_sock(skparent);
1425                 sk_del_node_init(sk);
1426                 sk = skparent;
1427         }
1428 #endif
1429         /* Unhash a listening sock only when it is closed
1430          * and all of its active connected pipes are closed. */
1431         if (hlist_empty(&pn->hlist))
1432                 pn_sock_unhash(&pn->pn_sk.sk);
1433         release_sock(sk);
1434
1435         if (skparent)
1436                 sock_put(skparent);
1437 }
1438
1439 static struct proto pep_proto = {
1440         .close          = pep_sock_close,
1441         .accept         = pep_sock_accept,
1442 #ifdef CONFIG_PHONET_PIPECTRLR
1443         .connect        = pep_sock_connect,
1444 #endif
1445         .ioctl          = pep_ioctl,
1446         .init           = pep_init,
1447         .setsockopt     = pep_setsockopt,
1448         .getsockopt     = pep_getsockopt,
1449         .sendmsg        = pep_sendmsg,
1450         .recvmsg        = pep_recvmsg,
1451         .backlog_rcv    = pep_do_rcv,
1452         .hash           = pn_sock_hash,
1453         .unhash         = pep_sock_unhash,
1454         .get_port       = pn_sock_get_port,
1455         .obj_size       = sizeof(struct pep_sock),
1456         .owner          = THIS_MODULE,
1457         .name           = "PNPIPE",
1458 };
1459
1460 static struct phonet_protocol pep_pn_proto = {
1461         .ops            = &phonet_stream_ops,
1462         .prot           = &pep_proto,
1463         .sock_type      = SOCK_SEQPACKET,
1464 };
1465
1466 static int __init pep_register(void)
1467 {
1468         return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1469 }
1470
1471 static void __exit pep_unregister(void)
1472 {
1473         phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1474 }
1475
1476 module_init(pep_register);
1477 module_exit(pep_unregister);
1478 MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1479 MODULE_DESCRIPTION("Phonet pipe protocol");
1480 MODULE_LICENSE("GPL");
1481 MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);