blob: f818f76d297d0f8fbda96ac2db6d9358013a3801 [file] [log] [blame]
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Rémi Denis-Courmont96414582008-10-05 11:15:13 -070027#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>
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070035#include <net/phonet/gprs.h>
Rémi Denis-Courmont96414582008-10-05 11:15:13 -070036
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
53static 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. */
61static 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
85static 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;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +000091#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
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700100
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
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000117#ifdef CONFIG_PHONET_PIPECTRLR
118 return pn_skb_send(sk, skb, &spn);
119#else
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700120 return pn_skb_send(sk, skb, &pipe_srv);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000121#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700122}
123
124#define PAD 0x00
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000125
126#ifdef CONFIG_PHONET_PIPECTRLR
127static 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
143done:
144 return final_fc;
145
146}
147
148static 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
191static 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
244static 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
291static 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
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000330static int pipe_handler_enable_pipe(struct sock *sk, int enable)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000331{
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000332 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000333 int utid, req;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000334
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000335 if (enable) {
336 utid = PNS_PIPE_ENABLE_UTID;
337 req = PNS_PEP_ENABLE_REQ;
338 } else {
339 utid = PNS_PIPE_DISABLE_UTID;
340 req = PNS_PEP_DISABLE_REQ;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000341 }
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000342 return pipe_handler_send_req(sk, pn->pn_sk.sobject, utid, req,
343 pn->pipe_handle, GFP_ATOMIC);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000344}
345
346static int pipe_handler_create_pipe(struct sock *sk, int pipe_handle, int cmd)
347{
348 int ret;
349 struct pep_sock *pn = pep_sk(sk);
350
351 switch (cmd) {
352 case PNPIPE_CREATE:
353 ret = pipe_handler_send_req(sk, pn->pn_sk.sobject,
354 PNS_PEP_CONNECT_UTID, PNS_PEP_CONNECT_REQ,
355 pipe_handle, GFP_ATOMIC);
356 break;
357
358 case PNPIPE_DESTROY:
359 ret = pipe_handler_send_req(sk, pn->remote_pep,
360 PNS_PEP_DISCONNECT_UTID,
361 PNS_PEP_DISCONNECT_REQ,
362 pn->pipe_handle, GFP_ATOMIC);
363 break;
364
365 default:
366 ret = -EINVAL;
367 }
368
369 return ret;
370}
371#endif
372
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700373static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
374{
375 static const u8 data[20] = {
376 PAD, PAD, PAD, 2 /* sub-blocks */,
377 PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
378 PN_MULTI_CREDIT_FLOW_CONTROL,
379 PN_ONE_CREDIT_FLOW_CONTROL,
380 PN_LEGACY_FLOW_CONTROL,
381 PAD,
382 PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
383 PN_MULTI_CREDIT_FLOW_CONTROL,
384 PN_ONE_CREDIT_FLOW_CONTROL,
385 PN_LEGACY_FLOW_CONTROL,
386 PAD,
387 };
388
389 might_sleep();
390 return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
391 GFP_KERNEL);
392}
393
394static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code)
395{
396 static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
397 WARN_ON(code == PN_PIPE_NO_ERROR);
398 return pep_reply(sk, skb, code, data, sizeof(data), GFP_ATOMIC);
399}
400
401/* Control requests are not sent by the pipe service and have a specific
402 * message format. */
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700403static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
404 gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700405{
406 const struct pnpipehdr *oph = pnp_hdr(oskb);
407 struct sk_buff *skb;
408 struct pnpipehdr *ph;
409 struct sockaddr_pn dst;
410
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700411 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700412 if (!skb)
413 return -ENOMEM;
414 skb_set_owner_w(skb, sk);
415
416 skb_reserve(skb, MAX_PHONET_HEADER);
417 ph = (struct pnpipehdr *)skb_put(skb, sizeof(*ph) + 4);
418
419 ph->utid = oph->utid;
420 ph->message_id = PNS_PEP_CTRL_RESP;
421 ph->pipe_handle = oph->pipe_handle;
422 ph->data[0] = oph->data[1]; /* CTRL id */
423 ph->data[1] = oph->data[0]; /* PEP type */
424 ph->data[2] = code; /* error code, at an usual offset */
425 ph->data[3] = PAD;
426 ph->data[4] = PAD;
427
428 pn_skb_get_src_sockaddr(oskb, &dst);
429 return pn_skb_send(sk, skb, &dst);
430}
431
432static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
433{
434 struct pep_sock *pn = pep_sk(sk);
435 struct pnpipehdr *ph;
436 struct sk_buff *skb;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000437#ifdef CONFIG_PHONET_PIPECTRLR
438 struct sockaddr_pn spn = {
439 .spn_family = AF_PHONET,
440 .spn_resource = 0xD9,
441 .spn_dev = pn_dev(pn->remote_pep),
442 .spn_obj = pn_obj(pn->remote_pep),
443 };
444#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700445
446 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
447 if (!skb)
448 return -ENOMEM;
449 skb_set_owner_w(skb, sk);
450
451 skb_reserve(skb, MAX_PNPIPE_HEADER + 4);
452 __skb_push(skb, sizeof(*ph) + 4);
453 skb_reset_transport_header(skb);
454 ph = pnp_hdr(skb);
455 ph->utid = 0;
456 ph->message_id = PNS_PEP_STATUS_IND;
457 ph->pipe_handle = pn->pipe_handle;
458 ph->pep_type = PN_PEP_TYPE_COMMON;
459 ph->data[1] = type;
460 ph->data[2] = PAD;
461 ph->data[3] = PAD;
462 ph->data[4] = status;
463
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000464#ifdef CONFIG_PHONET_PIPECTRLR
465 return pn_skb_send(sk, skb, &spn);
466#else
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700467 return pn_skb_send(sk, skb, &pipe_srv);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000468#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700469}
470
471/* Send our RX flow control information to the sender.
472 * Socket must be locked. */
473static void pipe_grant_credits(struct sock *sk)
474{
475 struct pep_sock *pn = pep_sk(sk);
476
477 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
478
479 switch (pn->rx_fc) {
480 case PN_LEGACY_FLOW_CONTROL: /* TODO */
481 break;
482 case PN_ONE_CREDIT_FLOW_CONTROL:
483 pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
484 PEP_IND_READY, GFP_ATOMIC);
485 pn->rx_credits = 1;
486 break;
487 case PN_MULTI_CREDIT_FLOW_CONTROL:
488 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
489 break;
490 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
491 CREDITS_MAX - pn->rx_credits,
492 GFP_ATOMIC) == 0)
493 pn->rx_credits = CREDITS_MAX;
494 break;
495 }
496}
497
498static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
499{
500 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000501 struct pnpipehdr *hdr;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800502 int wake = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700503
504 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
505 return -EINVAL;
506
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000507 hdr = pnp_hdr(skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700508 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
509 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
510 (unsigned)hdr->data[0]);
511 return -EOPNOTSUPP;
512 }
513
514 switch (hdr->data[1]) {
515 case PN_PEP_IND_FLOW_CONTROL:
516 switch (pn->tx_fc) {
517 case PN_LEGACY_FLOW_CONTROL:
518 switch (hdr->data[4]) {
519 case PEP_IND_BUSY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800520 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700521 break;
522 case PEP_IND_READY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800523 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700524 break;
525 }
526 break;
527 case PN_ONE_CREDIT_FLOW_CONTROL:
528 if (hdr->data[4] == PEP_IND_READY)
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800529 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700530 break;
531 }
532 break;
533
534 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
535 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
536 break;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800537 atomic_add(wake = hdr->data[4], &pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700538 break;
539
540 default:
541 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
542 (unsigned)hdr->data[1]);
543 return -EOPNOTSUPP;
544 }
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800545 if (wake)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700546 sk->sk_write_space(sk);
547 return 0;
548}
549
550static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
551{
552 struct pep_sock *pn = pep_sk(sk);
553 struct pnpipehdr *hdr = pnp_hdr(skb);
554 u8 n_sb = hdr->data[0];
555
556 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
557 __skb_pull(skb, sizeof(*hdr));
558 while (n_sb > 0) {
559 u8 type, buf[2], len = sizeof(buf);
560 u8 *data = pep_get_sb(skb, &type, &len, buf);
561
562 if (data == NULL)
563 return -EINVAL;
564 switch (type) {
565 case PN_PIPE_SB_NEGOTIATED_FC:
566 if (len < 2 || (data[0] | data[1]) > 3)
567 break;
568 pn->tx_fc = data[0] & 3;
569 pn->rx_fc = data[1] & 3;
570 break;
571 }
572 n_sb--;
573 }
574 return 0;
575}
576
577/* Queue an skb to a connected sock.
578 * Socket lock must be held. */
579static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
580{
581 struct pep_sock *pn = pep_sk(sk);
582 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700583 struct sk_buff_head *queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700584 int err = 0;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000585#ifdef CONFIG_PHONET_PIPECTRLR
586 struct phonethdr *ph = pn_hdr(skb);
587 static u8 host_pref_rx_fc[3], host_req_tx_fc[3];
588 u8 remote_pref_rx_fc[3], remote_req_tx_fc[3];
589 u8 negotiated_rx_fc, negotiated_tx_fc;
590#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700591
592 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
593
594 switch (hdr->message_id) {
595 case PNS_PEP_CONNECT_REQ:
596 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
597 break;
598
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000599#ifdef CONFIG_PHONET_PIPECTRLR
600 case PNS_PEP_CONNECT_RESP:
601 if ((ph->pn_sdev == pn_dev(pn->remote_pep)) &&
602 (ph->pn_sobj == pn_obj(pn->remote_pep))) {
603 pipe_get_flow_info(sk, skb, remote_pref_rx_fc,
604 remote_req_tx_fc);
605
606 negotiated_tx_fc = pipe_negotiate_fc(remote_req_tx_fc,
607 host_pref_rx_fc,
608 sizeof(host_pref_rx_fc));
609 negotiated_rx_fc = pipe_negotiate_fc(host_req_tx_fc,
610 remote_pref_rx_fc,
611 sizeof(host_pref_rx_fc));
612
613 pn->pipe_state = PIPE_DISABLED;
614 pipe_handler_send_created_ind(sk, pn->remote_pep,
615 PNS_PIPE_CREATED_IND_UTID,
616 pn->pipe_handle, PNS_PIPE_CREATED_IND,
617 negotiated_tx_fc, negotiated_rx_fc);
618 pipe_handler_send_created_ind(sk, pn->pn_sk.sobject,
619 PNS_PIPE_CREATED_IND_UTID,
620 pn->pipe_handle, PNS_PIPE_CREATED_IND,
621 negotiated_tx_fc, negotiated_rx_fc);
622 } else {
623 pipe_handler_send_req(sk, pn->remote_pep,
624 PNS_PEP_CONNECT_UTID,
625 PNS_PEP_CONNECT_REQ, pn->pipe_handle,
626 GFP_ATOMIC);
627 pipe_get_flow_info(sk, skb, host_pref_rx_fc,
628 host_req_tx_fc);
629 }
630 break;
631#endif
632
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700633 case PNS_PEP_DISCONNECT_REQ:
634 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
635 sk->sk_state = TCP_CLOSE_WAIT;
636 if (!sock_flag(sk, SOCK_DEAD))
637 sk->sk_state_change(sk);
638 break;
639
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000640#ifdef CONFIG_PHONET_PIPECTRLR
641 case PNS_PEP_DISCONNECT_RESP:
642 pn->pipe_state = PIPE_IDLE;
643 pipe_handler_send_req(sk, pn->pn_sk.sobject,
644 PNS_PEP_DISCONNECT_UTID,
645 PNS_PEP_DISCONNECT_REQ, pn->pipe_handle,
646 GFP_KERNEL);
647 break;
648#endif
649
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700650 case PNS_PEP_ENABLE_REQ:
651 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
652 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
653 break;
654
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000655#ifdef CONFIG_PHONET_PIPECTRLR
656 case PNS_PEP_ENABLE_RESP:
657 if ((ph->pn_sdev == pn_dev(pn->remote_pep)) &&
658 (ph->pn_sobj == pn_obj(pn->remote_pep))) {
659 pn->pipe_state = PIPE_ENABLED;
660 pipe_handler_send_ind(sk, pn->remote_pep,
661 PNS_PIPE_ENABLED_IND_UTID,
662 pn->pipe_handle, PNS_PIPE_ENABLED_IND);
663 pipe_handler_send_ind(sk, pn->pn_sk.sobject,
664 PNS_PIPE_ENABLED_IND_UTID,
665 pn->pipe_handle, PNS_PIPE_ENABLED_IND);
666 } else
667 pipe_handler_send_req(sk, pn->remote_pep,
668 PNS_PIPE_ENABLE_UTID,
669 PNS_PEP_ENABLE_REQ, pn->pipe_handle,
670 GFP_KERNEL);
671
672 break;
673#endif
674
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700675 case PNS_PEP_RESET_REQ:
676 switch (hdr->state_after_reset) {
677 case PN_PIPE_DISABLE:
678 pn->init_enable = 0;
679 break;
680 case PN_PIPE_ENABLE:
681 pn->init_enable = 1;
682 break;
683 default: /* not allowed to send an error here!? */
684 err = -EINVAL;
685 goto out;
686 }
687 /* fall through */
688 case PNS_PEP_DISABLE_REQ:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800689 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700690 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
691 break;
692
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000693#ifdef CONFIG_PHONET_PIPECTRLR
694 case PNS_PEP_DISABLE_RESP:
695 if ((ph->pn_sdev == pn_dev(pn->remote_pep)) &&
696 (ph->pn_sobj == pn_obj(pn->remote_pep))) {
697 pn->pipe_state = PIPE_DISABLED;
698 pipe_handler_send_ind(sk, pn->remote_pep,
699 PNS_PIPE_DISABLED_IND_UTID,
700 pn->pipe_handle,
701 PNS_PIPE_DISABLED_IND);
702 pipe_handler_send_ind(sk, pn->pn_sk.sobject,
703 PNS_PIPE_DISABLED_IND_UTID,
704 pn->pipe_handle,
705 PNS_PIPE_DISABLED_IND);
706 } else
707 pipe_handler_send_req(sk, pn->remote_pep,
708 PNS_PIPE_DISABLE_UTID,
709 PNS_PEP_DISABLE_REQ, pn->pipe_handle,
710 GFP_KERNEL);
711 break;
712#endif
713
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700714 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000715 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
716 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700717 break;
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000718 }
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700719 __skb_pull(skb, 4);
720 queue = &pn->ctrlreq_queue;
721 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700722
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +0000723 case PNS_PIPE_ALIGNED_DATA:
724 __skb_pull(skb, 1);
725 /* fall through */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700726 case PNS_PIPE_DATA:
727 __skb_pull(skb, 3); /* Pipe data header */
728 if (!pn_flow_safe(pn->rx_fc)) {
729 err = sock_queue_rcv_skb(sk, skb);
730 if (!err)
731 return 0;
732 break;
733 }
734
735 if (pn->rx_credits == 0) {
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000736 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700737 err = -ENOBUFS;
738 break;
739 }
740 pn->rx_credits--;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700741 queue = &sk->sk_receive_queue;
742 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700743
744 case PNS_PEP_STATUS_IND:
745 pipe_rcv_status(sk, skb);
746 break;
747
748 case PNS_PIPE_REDIRECTED_IND:
749 err = pipe_rcv_created(sk, skb);
750 break;
751
752 case PNS_PIPE_CREATED_IND:
753 err = pipe_rcv_created(sk, skb);
754 if (err)
755 break;
756 /* fall through */
757 case PNS_PIPE_RESET_IND:
758 if (!pn->init_enable)
759 break;
760 /* fall through */
761 case PNS_PIPE_ENABLED_IND:
762 if (!pn_flow_safe(pn->tx_fc)) {
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800763 atomic_set(&pn->tx_credits, 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700764 sk->sk_write_space(sk);
765 }
766 if (sk->sk_state == TCP_ESTABLISHED)
767 break; /* Nothing to do */
768 sk->sk_state = TCP_ESTABLISHED;
769 pipe_grant_credits(sk);
770 break;
771
772 case PNS_PIPE_DISABLED_IND:
773 sk->sk_state = TCP_SYN_RECV;
774 pn->rx_credits = 0;
775 break;
776
777 default:
778 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
779 hdr->message_id);
780 err = -EINVAL;
781 }
782out:
783 kfree_skb(skb);
784 return err;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700785
786queue:
787 skb->dev = NULL;
788 skb_set_owner_r(skb, sk);
789 err = skb->len;
790 skb_queue_tail(queue, skb);
791 if (!sock_flag(sk, SOCK_DEAD))
792 sk->sk_data_ready(sk, err);
793 return 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700794}
795
796/* Destroy connected sock. */
797static void pipe_destruct(struct sock *sk)
798{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700799 struct pep_sock *pn = pep_sk(sk);
800
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700801 skb_queue_purge(&sk->sk_receive_queue);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700802 skb_queue_purge(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700803}
804
805static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
806{
807 struct sock *newsk;
808 struct pep_sock *newpn, *pn = pep_sk(sk);
809 struct pnpipehdr *hdr;
810 struct sockaddr_pn dst;
811 u16 peer_type;
812 u8 pipe_handle, enabled, n_sb;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000813 u8 aligned = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700814
815 if (!pskb_pull(skb, sizeof(*hdr) + 4))
816 return -EINVAL;
817
818 hdr = pnp_hdr(skb);
819 pipe_handle = hdr->pipe_handle;
820 switch (hdr->state_after_connect) {
821 case PN_PIPE_DISABLE:
822 enabled = 0;
823 break;
824 case PN_PIPE_ENABLE:
825 enabled = 1;
826 break;
827 default:
828 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
829 return -EINVAL;
830 }
831 peer_type = hdr->other_pep_type << 8;
832
833 if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) {
834 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
835 return -ENOBUFS;
836 }
837
838 /* Parse sub-blocks (options) */
839 n_sb = hdr->data[4];
840 while (n_sb > 0) {
841 u8 type, buf[1], len = sizeof(buf);
842 const u8 *data = pep_get_sb(skb, &type, &len, buf);
843
844 if (data == NULL)
845 return -EINVAL;
846 switch (type) {
847 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
848 if (len < 1)
849 return -EINVAL;
850 peer_type = (peer_type & 0xff00) | data[0];
851 break;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000852 case PN_PIPE_SB_ALIGNED_DATA:
853 aligned = data[0] != 0;
854 break;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700855 }
856 n_sb--;
857 }
858
859 skb = skb_clone(skb, GFP_ATOMIC);
860 if (!skb)
861 return -ENOMEM;
862
863 /* Create a new to-be-accepted sock */
864 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
865 if (!newsk) {
866 kfree_skb(skb);
867 return -ENOMEM;
868 }
869 sock_init_data(NULL, newsk);
870 newsk->sk_state = TCP_SYN_RECV;
871 newsk->sk_backlog_rcv = pipe_do_rcv;
872 newsk->sk_protocol = sk->sk_protocol;
873 newsk->sk_destruct = pipe_destruct;
874
875 newpn = pep_sk(newsk);
876 pn_skb_get_dst_sockaddr(skb, &dst);
877 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
878 newpn->pn_sk.resource = pn->pn_sk.resource;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700879 skb_queue_head_init(&newpn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700880 newpn->pipe_handle = pipe_handle;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800881 atomic_set(&newpn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700882 newpn->peer_type = peer_type;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800883 newpn->rx_credits = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700884 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
885 newpn->init_enable = enabled;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000886 newpn->aligned = aligned;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000887#ifdef CONFIG_PHONET_PIPECTRLR
888 newpn->remote_pep = pn->remote_pep;
889#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700890
891 BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
892 skb_queue_head(&newsk->sk_receive_queue, skb);
893 if (!sock_flag(sk, SOCK_DEAD))
894 sk->sk_data_ready(sk, 0);
895
896 sk_acceptq_added(sk);
897 sk_add_node(newsk, &pn->ackq);
898 return 0;
899}
900
901/* Listening sock must be locked */
902static struct sock *pep_find_pipe(const struct hlist_head *hlist,
903 const struct sockaddr_pn *dst,
904 u8 pipe_handle)
905{
906 struct hlist_node *node;
907 struct sock *sknode;
908 u16 dobj = pn_sockaddr_get_object(dst);
909
910 sk_for_each(sknode, node, hlist) {
911 struct pep_sock *pnnode = pep_sk(sknode);
912
913 /* Ports match, but addresses might not: */
914 if (pnnode->pn_sk.sobject != dobj)
915 continue;
916 if (pnnode->pipe_handle != pipe_handle)
917 continue;
918 if (sknode->sk_state == TCP_CLOSE_WAIT)
919 continue;
920
921 sock_hold(sknode);
922 return sknode;
923 }
924 return NULL;
925}
926
927/*
928 * Deliver an skb to a listening sock.
929 * Socket lock must be held.
930 * We then queue the skb to the right connected sock (if any).
931 */
932static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
933{
934 struct pep_sock *pn = pep_sk(sk);
935 struct sock *sknode;
Rémi Denis-Courmont2ddc1ac2009-02-10 17:14:50 -0800936 struct pnpipehdr *hdr;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700937 struct sockaddr_pn dst;
938 int err = NET_RX_SUCCESS;
939 u8 pipe_handle;
940
941 if (!pskb_may_pull(skb, sizeof(*hdr)))
942 goto drop;
943
944 hdr = pnp_hdr(skb);
945 pipe_handle = hdr->pipe_handle;
946 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
947 goto drop;
948
949 pn_skb_get_dst_sockaddr(skb, &dst);
950
951 /* Look for an existing pipe handle */
952 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
953 if (sknode)
954 return sk_receive_skb(sknode, skb, 1);
955
956 /* Look for a pipe handle pending accept */
957 sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
958 if (sknode) {
959 sock_put(sknode);
960 if (net_ratelimit())
961 printk(KERN_WARNING"Phonet unconnected PEP ignored");
962 err = NET_RX_DROP;
963 goto drop;
964 }
965
966 switch (hdr->message_id) {
967 case PNS_PEP_CONNECT_REQ:
968 err = pep_connreq_rcv(sk, skb);
969 break;
970
971 case PNS_PEP_DISCONNECT_REQ:
972 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
973 break;
974
975 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700976 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700977 break;
978
979 case PNS_PEP_RESET_REQ:
980 case PNS_PEP_ENABLE_REQ:
981 case PNS_PEP_DISABLE_REQ:
982 /* invalid handle is not even allowed here! */
983 default:
984 err = NET_RX_DROP;
985 }
986drop:
987 kfree_skb(skb);
988 return err;
989}
990
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000991static int pipe_do_remove(struct sock *sk)
992{
993 struct pep_sock *pn = pep_sk(sk);
994 struct pnpipehdr *ph;
995 struct sk_buff *skb;
996
997 skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_KERNEL);
998 if (!skb)
999 return -ENOMEM;
1000
1001 skb_reserve(skb, MAX_PNPIPE_HEADER);
1002 __skb_push(skb, sizeof(*ph));
1003 skb_reset_transport_header(skb);
1004 ph = pnp_hdr(skb);
1005 ph->utid = 0;
1006 ph->message_id = PNS_PIPE_REMOVE_REQ;
1007 ph->pipe_handle = pn->pipe_handle;
1008 ph->data[0] = PAD;
1009
1010 return pn_skb_send(sk, skb, &pipe_srv);
1011}
1012
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001013/* associated socket ceases to exist */
1014static void pep_sock_close(struct sock *sk, long timeout)
1015{
1016 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001017 int ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001018
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -07001019 sock_hold(sk); /* keep a reference after sk_common_release() */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001020 sk_common_release(sk);
1021
1022 lock_sock(sk);
1023 if (sk->sk_state == TCP_LISTEN) {
1024 /* Destroy the listen queue */
1025 struct sock *sknode;
1026 struct hlist_node *p, *n;
1027
1028 sk_for_each_safe(sknode, p, n, &pn->ackq)
1029 sk_del_node_init(sknode);
1030 sk->sk_state = TCP_CLOSE;
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +00001031 } else if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED))
1032 /* Forcefully remove dangling Phonet pipe */
1033 pipe_do_remove(sk);
1034
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001035 ifindex = pn->ifindex;
1036 pn->ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001037 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001038
1039 if (ifindex)
1040 gprs_detach(sk);
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -07001041 sock_put(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001042}
1043
1044static int pep_wait_connreq(struct sock *sk, int noblock)
1045{
1046 struct task_struct *tsk = current;
1047 struct pep_sock *pn = pep_sk(sk);
1048 long timeo = sock_rcvtimeo(sk, noblock);
1049
1050 for (;;) {
1051 DEFINE_WAIT(wait);
1052
1053 if (sk->sk_state != TCP_LISTEN)
1054 return -EINVAL;
1055 if (!hlist_empty(&pn->ackq))
1056 break;
1057 if (!timeo)
1058 return -EWOULDBLOCK;
1059 if (signal_pending(tsk))
1060 return sock_intr_errno(timeo);
1061
Eric Dumazet43815482010-04-29 11:01:49 +00001062 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001063 TASK_INTERRUPTIBLE);
1064 release_sock(sk);
1065 timeo = schedule_timeout(timeo);
1066 lock_sock(sk);
Eric Dumazet43815482010-04-29 11:01:49 +00001067 finish_wait(sk_sleep(sk), &wait);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001068 }
1069
1070 return 0;
1071}
1072
1073static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
1074{
1075 struct pep_sock *pn = pep_sk(sk);
1076 struct sock *newsk = NULL;
1077 struct sk_buff *oskb;
1078 int err;
1079
1080 lock_sock(sk);
1081 err = pep_wait_connreq(sk, flags & O_NONBLOCK);
1082 if (err)
1083 goto out;
1084
1085 newsk = __sk_head(&pn->ackq);
1086
1087 oskb = skb_dequeue(&newsk->sk_receive_queue);
1088 err = pep_accept_conn(newsk, oskb);
1089 if (err) {
1090 skb_queue_head(&newsk->sk_receive_queue, oskb);
1091 newsk = NULL;
1092 goto out;
1093 }
Rémi Denis-Courmont635f0812010-07-07 20:56:53 +00001094 kfree_skb(oskb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001095
1096 sock_hold(sk);
1097 pep_sk(newsk)->listener = sk;
1098
1099 sock_hold(newsk);
1100 sk_del_node_init(newsk);
1101 sk_acceptq_removed(sk);
1102 sk_add_node(newsk, &pn->hlist);
1103 __sock_put(newsk);
1104
1105out:
1106 release_sock(sk);
1107 *errp = err;
1108 return newsk;
1109}
1110
1111static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
1112{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001113 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001114 int answ;
1115
1116 switch (cmd) {
1117 case SIOCINQ:
1118 if (sk->sk_state == TCP_LISTEN)
1119 return -EINVAL;
1120
1121 lock_sock(sk);
Joe Perchesf64f9e72009-11-29 16:55:45 -08001122 if (sock_flag(sk, SOCK_URGINLINE) &&
1123 !skb_queue_empty(&pn->ctrlreq_queue))
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001124 answ = skb_peek(&pn->ctrlreq_queue)->len;
1125 else if (!skb_queue_empty(&sk->sk_receive_queue))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001126 answ = skb_peek(&sk->sk_receive_queue)->len;
1127 else
1128 answ = 0;
1129 release_sock(sk);
1130 return put_user(answ, (int __user *)arg);
1131 }
1132
1133 return -ENOIOCTLCMD;
1134}
1135
1136static int pep_init(struct sock *sk)
1137{
1138 struct pep_sock *pn = pep_sk(sk);
1139
1140 INIT_HLIST_HEAD(&pn->ackq);
1141 INIT_HLIST_HEAD(&pn->hlist);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001142 skb_queue_head_init(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001143 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
1144 return 0;
1145}
1146
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001147static int pep_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001148 char __user *optval, unsigned int optlen)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001149{
1150 struct pep_sock *pn = pep_sk(sk);
1151 int val = 0, err = 0;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001152#ifdef CONFIG_PHONET_PIPECTRLR
1153 int remote_pep;
1154 int pipe_handle;
1155#endif
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001156
1157 if (level != SOL_PNPIPE)
1158 return -ENOPROTOOPT;
1159 if (optlen >= sizeof(int)) {
1160 if (get_user(val, (int __user *) optval))
1161 return -EFAULT;
1162 }
1163
1164 lock_sock(sk);
1165 switch (optname) {
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001166#ifdef CONFIG_PHONET_PIPECTRLR
1167 case PNPIPE_CREATE:
1168 if (val) {
1169 if (pn->pipe_state > PIPE_IDLE) {
1170 err = -EFAULT;
1171 break;
1172 }
1173 remote_pep = val & 0xFFFF;
1174 pipe_handle = (val >> 16) & 0xFF;
1175 pn->remote_pep = remote_pep;
1176 err = pipe_handler_create_pipe(sk, pipe_handle,
1177 PNPIPE_CREATE);
1178 break;
1179 }
1180
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001181 case PNPIPE_DESTROY:
1182 if (pn->pipe_state < PIPE_DISABLED) {
1183 err = -EFAULT;
1184 break;
1185 }
1186
1187 err = pipe_handler_create_pipe(sk, 0x0, PNPIPE_DESTROY);
1188 break;
1189#endif
1190
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001191 case PNPIPE_ENCAP:
1192 if (val && val != PNPIPE_ENCAP_IP) {
1193 err = -EINVAL;
1194 break;
1195 }
1196 if (!pn->ifindex == !val)
1197 break; /* Nothing to do! */
1198 if (!capable(CAP_NET_ADMIN)) {
1199 err = -EPERM;
1200 break;
1201 }
1202 if (val) {
1203 release_sock(sk);
1204 err = gprs_attach(sk);
1205 if (err > 0) {
1206 pn->ifindex = err;
1207 err = 0;
1208 }
1209 } else {
1210 pn->ifindex = 0;
1211 release_sock(sk);
1212 gprs_detach(sk);
1213 err = 0;
1214 }
1215 goto out_norel;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001216
1217#ifdef CONFIG_PHONET_PIPECTRLR
1218 case PNPIPE_ENABLE:
1219 if (pn->pipe_state <= PIPE_IDLE) {
1220 err = -ENOTCONN;
1221 break;
1222 }
1223 err = pipe_handler_enable_pipe(sk, val);
1224 break;
1225#endif
1226
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001227 default:
1228 err = -ENOPROTOOPT;
1229 }
1230 release_sock(sk);
1231
1232out_norel:
1233 return err;
1234}
1235
1236static int pep_getsockopt(struct sock *sk, int level, int optname,
1237 char __user *optval, int __user *optlen)
1238{
1239 struct pep_sock *pn = pep_sk(sk);
1240 int len, val;
1241
1242 if (level != SOL_PNPIPE)
1243 return -ENOPROTOOPT;
1244 if (get_user(len, optlen))
1245 return -EFAULT;
1246
1247 switch (optname) {
1248 case PNPIPE_ENCAP:
1249 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
1250 break;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001251
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001252 case PNPIPE_IFINDEX:
1253 val = pn->ifindex;
1254 break;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001255
1256#ifdef CONFIG_PHONET_PIPECTRLR
1257 case PNPIPE_ENABLE:
1258 if (pn->pipe_state <= PIPE_IDLE)
1259 return -ENOTCONN;
1260 val = pn->pipe_state != PIPE_DISABLED;
1261 break;
1262#endif
1263
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001264 default:
1265 return -ENOPROTOOPT;
1266 }
1267
1268 len = min_t(unsigned int, sizeof(int), len);
1269 if (put_user(len, optlen))
1270 return -EFAULT;
1271 if (put_user(val, (int __user *) optval))
1272 return -EFAULT;
1273 return 0;
1274}
1275
1276static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1277{
1278 struct pep_sock *pn = pep_sk(sk);
1279 struct pnpipehdr *ph;
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001280 int err;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001281#ifdef CONFIG_PHONET_PIPECTRLR
1282 struct sockaddr_pn spn = {
1283 .spn_family = AF_PHONET,
1284 .spn_resource = 0xD9,
1285 .spn_dev = pn_dev(pn->remote_pep),
1286 .spn_obj = pn_obj(pn->remote_pep),
1287 };
1288#endif
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001289
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001290 if (pn_flow_safe(pn->tx_fc) &&
1291 !atomic_add_unless(&pn->tx_credits, -1, 0)) {
1292 kfree_skb(skb);
1293 return -ENOBUFS;
1294 }
1295
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001296 skb_push(skb, 3 + pn->aligned);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001297 skb_reset_transport_header(skb);
1298 ph = pnp_hdr(skb);
1299 ph->utid = 0;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001300 if (pn->aligned) {
1301 ph->message_id = PNS_PIPE_ALIGNED_DATA;
1302 ph->data[0] = 0; /* padding */
1303 } else
1304 ph->message_id = PNS_PIPE_DATA;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001305 ph->pipe_handle = pn->pipe_handle;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001306#ifdef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001307 err = pn_skb_send(sk, skb, &spn);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001308#else
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001309 err = pn_skb_send(sk, skb, &pipe_srv);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001310#endif
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001311
1312 if (err && pn_flow_safe(pn->tx_fc))
1313 atomic_inc(&pn->tx_credits);
1314 return err;
1315
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001316}
1317
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001318static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
1319 struct msghdr *msg, size_t len)
1320{
1321 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001322 struct sk_buff *skb;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001323 long timeo;
1324 int flags = msg->msg_flags;
1325 int err, done;
1326
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001327 if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
1328 MSG_CMSG_COMPAT)) ||
1329 !(msg->msg_flags & MSG_EOR))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001330 return -EOPNOTSUPP;
1331
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001332 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
1333 flags & MSG_DONTWAIT, &err);
1334 if (!skb)
Rémi Denis-Courmont02ac3262010-08-30 12:57:04 +00001335 return err;
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001336
1337 skb_reserve(skb, MAX_PHONET_HEADER + 3);
1338 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1339 if (err < 0)
1340 goto outfree;
1341
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001342 lock_sock(sk);
1343 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1344 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
1345 err = -ENOTCONN;
1346 goto out;
1347 }
1348 if (sk->sk_state != TCP_ESTABLISHED) {
1349 /* Wait until the pipe gets to enabled state */
1350disabled:
1351 err = sk_stream_wait_connect(sk, &timeo);
1352 if (err)
1353 goto out;
1354
1355 if (sk->sk_state == TCP_CLOSE_WAIT) {
1356 err = -ECONNRESET;
1357 goto out;
1358 }
1359 }
1360 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
1361
1362 /* Wait until flow control allows TX */
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001363 done = atomic_read(&pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001364 while (!done) {
1365 DEFINE_WAIT(wait);
1366
1367 if (!timeo) {
1368 err = -EAGAIN;
1369 goto out;
1370 }
1371 if (signal_pending(current)) {
1372 err = sock_intr_errno(timeo);
1373 goto out;
1374 }
1375
Eric Dumazet43815482010-04-29 11:01:49 +00001376 prepare_to_wait(sk_sleep(sk), &wait,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001377 TASK_INTERRUPTIBLE);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001378 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
Eric Dumazet43815482010-04-29 11:01:49 +00001379 finish_wait(sk_sleep(sk), &wait);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001380
1381 if (sk->sk_state != TCP_ESTABLISHED)
1382 goto disabled;
1383 }
1384
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001385 err = pipe_skb_send(sk, skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001386 if (err >= 0)
1387 err = len; /* success! */
1388 skb = NULL;
1389out:
1390 release_sock(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001391outfree:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001392 kfree_skb(skb);
1393 return err;
1394}
1395
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001396int pep_writeable(struct sock *sk)
1397{
1398 struct pep_sock *pn = pep_sk(sk);
1399
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001400 return atomic_read(&pn->tx_credits);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001401}
1402
1403int pep_write(struct sock *sk, struct sk_buff *skb)
1404{
1405 struct sk_buff *rskb, *fs;
1406 int flen = 0;
1407
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001408 if (pep_sk(sk)->aligned)
1409 return pipe_skb_send(sk, skb);
1410
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001411 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
1412 if (!rskb) {
1413 kfree_skb(skb);
1414 return -ENOMEM;
1415 }
1416 skb_shinfo(rskb)->frag_list = skb;
1417 rskb->len += skb->len;
1418 rskb->data_len += rskb->len;
1419 rskb->truesize += rskb->len;
1420
1421 /* Avoid nested fragments */
David S. Miller5c313e92009-06-09 00:21:58 -07001422 skb_walk_frags(skb, fs)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001423 flen += fs->len;
1424 skb->next = skb_shinfo(skb)->frag_list;
David S. Miller5c313e92009-06-09 00:21:58 -07001425 skb_frag_list_init(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001426 skb->len -= flen;
1427 skb->data_len -= flen;
1428 skb->truesize -= flen;
1429
1430 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
1431 return pipe_skb_send(sk, rskb);
1432}
1433
1434struct sk_buff *pep_read(struct sock *sk)
1435{
1436 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1437
1438 if (sk->sk_state == TCP_ESTABLISHED)
1439 pipe_grant_credits(sk);
1440 return skb;
1441}
1442
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001443static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
1444 struct msghdr *msg, size_t len, int noblock,
1445 int flags, int *addr_len)
1446{
1447 struct sk_buff *skb;
1448 int err;
1449
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001450 if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
1451 MSG_NOSIGNAL|MSG_CMSG_COMPAT))
1452 return -EOPNOTSUPP;
1453
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001454 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
1455 return -ENOTCONN;
1456
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001457 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
1458 /* Dequeue and acknowledge control request */
1459 struct pep_sock *pn = pep_sk(sk);
1460
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001461 if (flags & MSG_PEEK)
1462 return -EOPNOTSUPP;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001463 skb = skb_dequeue(&pn->ctrlreq_queue);
1464 if (skb) {
1465 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1466 GFP_KERNEL);
1467 msg->msg_flags |= MSG_OOB;
1468 goto copy;
1469 }
1470 if (flags & MSG_OOB)
1471 return -EINVAL;
1472 }
1473
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001474 skb = skb_recv_datagram(sk, flags, noblock, &err);
1475 lock_sock(sk);
1476 if (skb == NULL) {
1477 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1478 err = -ECONNRESET;
1479 release_sock(sk);
1480 return err;
1481 }
1482
1483 if (sk->sk_state == TCP_ESTABLISHED)
1484 pipe_grant_credits(sk);
1485 release_sock(sk);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001486copy:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001487 msg->msg_flags |= MSG_EOR;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001488 if (skb->len > len)
1489 msg->msg_flags |= MSG_TRUNC;
1490 else
1491 len = skb->len;
1492
1493 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1494 if (!err)
1495 err = (flags & MSG_TRUNC) ? skb->len : len;
1496
1497 skb_free_datagram(sk, skb);
1498 return err;
1499}
1500
1501static void pep_sock_unhash(struct sock *sk)
1502{
1503 struct pep_sock *pn = pep_sk(sk);
1504 struct sock *skparent = NULL;
1505
1506 lock_sock(sk);
1507 if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1508 skparent = pn->listener;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001509 release_sock(sk);
1510
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001511 pn = pep_sk(skparent);
Rémi Denis-Courmont7dfde172010-05-26 00:44:44 +00001512 lock_sock(skparent);
1513 sk_del_node_init(sk);
1514 sk = skparent;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001515 }
1516 /* Unhash a listening sock only when it is closed
1517 * and all of its active connected pipes are closed. */
1518 if (hlist_empty(&pn->hlist))
1519 pn_sock_unhash(&pn->pn_sk.sk);
1520 release_sock(sk);
1521
1522 if (skparent)
1523 sock_put(skparent);
1524}
1525
1526static struct proto pep_proto = {
1527 .close = pep_sock_close,
1528 .accept = pep_sock_accept,
1529 .ioctl = pep_ioctl,
1530 .init = pep_init,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001531 .setsockopt = pep_setsockopt,
1532 .getsockopt = pep_getsockopt,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001533 .sendmsg = pep_sendmsg,
1534 .recvmsg = pep_recvmsg,
1535 .backlog_rcv = pep_do_rcv,
1536 .hash = pn_sock_hash,
1537 .unhash = pep_sock_unhash,
1538 .get_port = pn_sock_get_port,
1539 .obj_size = sizeof(struct pep_sock),
1540 .owner = THIS_MODULE,
1541 .name = "PNPIPE",
1542};
1543
1544static struct phonet_protocol pep_pn_proto = {
1545 .ops = &phonet_stream_ops,
1546 .prot = &pep_proto,
1547 .sock_type = SOCK_SEQPACKET,
1548};
1549
1550static int __init pep_register(void)
1551{
1552 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1553}
1554
1555static void __exit pep_unregister(void)
1556{
1557 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1558}
1559
1560module_init(pep_register);
1561module_exit(pep_unregister);
1562MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1563MODULE_DESCRIPTION("Phonet pipe protocol");
1564MODULE_LICENSE("GPL");
1565MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);