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