blob: 8ad2b53338817897c9de166e71bdc4a224b7e62a [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>
26#include <linux/socket.h>
27#include <net/sock.h>
28#include <net/tcp_states.h>
29#include <asm/ioctls.h>
30
31#include <linux/phonet.h>
32#include <net/phonet/phonet.h>
33#include <net/phonet/pep.h>
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070034#include <net/phonet/gprs.h>
Rémi Denis-Courmont96414582008-10-05 11:15:13 -070035
36/* sk_state values:
37 * TCP_CLOSE sock not in use yet
38 * TCP_CLOSE_WAIT disconnected pipe
39 * TCP_LISTEN listening pipe endpoint
40 * TCP_SYN_RECV connected pipe in disabled state
41 * TCP_ESTABLISHED connected pipe in enabled state
42 *
43 * pep_sock locking:
44 * - sk_state, ackq, hlist: sock lock needed
45 * - listener: read only
46 * - pipe_handle: read only
47 */
48
49#define CREDITS_MAX 10
50#define CREDITS_THR 7
51
52static const struct sockaddr_pn pipe_srv = {
53 .spn_family = AF_PHONET,
54 .spn_resource = 0xD9, /* pipe service */
55};
56
57#define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
58
59/* Get the next TLV sub-block. */
60static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen,
61 void *buf)
62{
63 void *data = NULL;
64 struct {
65 u8 sb_type;
66 u8 sb_len;
67 } *ph, h;
68 int buflen = *plen;
69
70 ph = skb_header_pointer(skb, 0, 2, &h);
71 if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len))
72 return NULL;
73 ph->sb_len -= 2;
74 *ptype = ph->sb_type;
75 *plen = ph->sb_len;
76
77 if (buflen > ph->sb_len)
78 buflen = ph->sb_len;
79 data = skb_header_pointer(skb, 2, buflen, buf);
80 __skb_pull(skb, 2 + ph->sb_len);
81 return data;
82}
83
84static int pep_reply(struct sock *sk, struct sk_buff *oskb,
85 u8 code, const void *data, int len, gfp_t priority)
86{
87 const struct pnpipehdr *oph = pnp_hdr(oskb);
88 struct pnpipehdr *ph;
89 struct sk_buff *skb;
90
91 skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
92 if (!skb)
93 return -ENOMEM;
94 skb_set_owner_w(skb, sk);
95
96 skb_reserve(skb, MAX_PNPIPE_HEADER);
97 __skb_put(skb, len);
98 skb_copy_to_linear_data(skb, data, len);
99 __skb_push(skb, sizeof(*ph));
100 skb_reset_transport_header(skb);
101 ph = pnp_hdr(skb);
102 ph->utid = oph->utid;
103 ph->message_id = oph->message_id + 1; /* REQ -> RESP */
104 ph->pipe_handle = oph->pipe_handle;
105 ph->error_code = code;
106
107 return pn_skb_send(sk, skb, &pipe_srv);
108}
109
110#define PAD 0x00
111static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
112{
113 static const u8 data[20] = {
114 PAD, PAD, PAD, 2 /* sub-blocks */,
115 PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
116 PN_MULTI_CREDIT_FLOW_CONTROL,
117 PN_ONE_CREDIT_FLOW_CONTROL,
118 PN_LEGACY_FLOW_CONTROL,
119 PAD,
120 PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
121 PN_MULTI_CREDIT_FLOW_CONTROL,
122 PN_ONE_CREDIT_FLOW_CONTROL,
123 PN_LEGACY_FLOW_CONTROL,
124 PAD,
125 };
126
127 might_sleep();
128 return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
129 GFP_KERNEL);
130}
131
132static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code)
133{
134 static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
135 WARN_ON(code == PN_PIPE_NO_ERROR);
136 return pep_reply(sk, skb, code, data, sizeof(data), GFP_ATOMIC);
137}
138
139/* Control requests are not sent by the pipe service and have a specific
140 * message format. */
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700141static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
142 gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700143{
144 const struct pnpipehdr *oph = pnp_hdr(oskb);
145 struct sk_buff *skb;
146 struct pnpipehdr *ph;
147 struct sockaddr_pn dst;
148
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700149 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700150 if (!skb)
151 return -ENOMEM;
152 skb_set_owner_w(skb, sk);
153
154 skb_reserve(skb, MAX_PHONET_HEADER);
155 ph = (struct pnpipehdr *)skb_put(skb, sizeof(*ph) + 4);
156
157 ph->utid = oph->utid;
158 ph->message_id = PNS_PEP_CTRL_RESP;
159 ph->pipe_handle = oph->pipe_handle;
160 ph->data[0] = oph->data[1]; /* CTRL id */
161 ph->data[1] = oph->data[0]; /* PEP type */
162 ph->data[2] = code; /* error code, at an usual offset */
163 ph->data[3] = PAD;
164 ph->data[4] = PAD;
165
166 pn_skb_get_src_sockaddr(oskb, &dst);
167 return pn_skb_send(sk, skb, &dst);
168}
169
170static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
171{
172 struct pep_sock *pn = pep_sk(sk);
173 struct pnpipehdr *ph;
174 struct sk_buff *skb;
175
176 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
177 if (!skb)
178 return -ENOMEM;
179 skb_set_owner_w(skb, sk);
180
181 skb_reserve(skb, MAX_PNPIPE_HEADER + 4);
182 __skb_push(skb, sizeof(*ph) + 4);
183 skb_reset_transport_header(skb);
184 ph = pnp_hdr(skb);
185 ph->utid = 0;
186 ph->message_id = PNS_PEP_STATUS_IND;
187 ph->pipe_handle = pn->pipe_handle;
188 ph->pep_type = PN_PEP_TYPE_COMMON;
189 ph->data[1] = type;
190 ph->data[2] = PAD;
191 ph->data[3] = PAD;
192 ph->data[4] = status;
193
194 return pn_skb_send(sk, skb, &pipe_srv);
195}
196
197/* Send our RX flow control information to the sender.
198 * Socket must be locked. */
199static void pipe_grant_credits(struct sock *sk)
200{
201 struct pep_sock *pn = pep_sk(sk);
202
203 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
204
205 switch (pn->rx_fc) {
206 case PN_LEGACY_FLOW_CONTROL: /* TODO */
207 break;
208 case PN_ONE_CREDIT_FLOW_CONTROL:
209 pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
210 PEP_IND_READY, GFP_ATOMIC);
211 pn->rx_credits = 1;
212 break;
213 case PN_MULTI_CREDIT_FLOW_CONTROL:
214 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
215 break;
216 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
217 CREDITS_MAX - pn->rx_credits,
218 GFP_ATOMIC) == 0)
219 pn->rx_credits = CREDITS_MAX;
220 break;
221 }
222}
223
224static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
225{
226 struct pep_sock *pn = pep_sk(sk);
227 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800228 int wake = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700229
230 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
231 return -EINVAL;
232
233 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
234 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
235 (unsigned)hdr->data[0]);
236 return -EOPNOTSUPP;
237 }
238
239 switch (hdr->data[1]) {
240 case PN_PEP_IND_FLOW_CONTROL:
241 switch (pn->tx_fc) {
242 case PN_LEGACY_FLOW_CONTROL:
243 switch (hdr->data[4]) {
244 case PEP_IND_BUSY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800245 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700246 break;
247 case PEP_IND_READY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800248 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700249 break;
250 }
251 break;
252 case PN_ONE_CREDIT_FLOW_CONTROL:
253 if (hdr->data[4] == PEP_IND_READY)
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800254 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700255 break;
256 }
257 break;
258
259 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
260 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
261 break;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800262 atomic_add(wake = hdr->data[4], &pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700263 break;
264
265 default:
266 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
267 (unsigned)hdr->data[1]);
268 return -EOPNOTSUPP;
269 }
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800270 if (wake)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700271 sk->sk_write_space(sk);
272 return 0;
273}
274
275static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
276{
277 struct pep_sock *pn = pep_sk(sk);
278 struct pnpipehdr *hdr = pnp_hdr(skb);
279 u8 n_sb = hdr->data[0];
280
281 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
282 __skb_pull(skb, sizeof(*hdr));
283 while (n_sb > 0) {
284 u8 type, buf[2], len = sizeof(buf);
285 u8 *data = pep_get_sb(skb, &type, &len, buf);
286
287 if (data == NULL)
288 return -EINVAL;
289 switch (type) {
290 case PN_PIPE_SB_NEGOTIATED_FC:
291 if (len < 2 || (data[0] | data[1]) > 3)
292 break;
293 pn->tx_fc = data[0] & 3;
294 pn->rx_fc = data[1] & 3;
295 break;
296 }
297 n_sb--;
298 }
299 return 0;
300}
301
302/* Queue an skb to a connected sock.
303 * Socket lock must be held. */
304static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
305{
306 struct pep_sock *pn = pep_sk(sk);
307 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700308 struct sk_buff_head *queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700309 int err = 0;
310
311 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
312
313 switch (hdr->message_id) {
314 case PNS_PEP_CONNECT_REQ:
315 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
316 break;
317
318 case PNS_PEP_DISCONNECT_REQ:
319 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
320 sk->sk_state = TCP_CLOSE_WAIT;
321 if (!sock_flag(sk, SOCK_DEAD))
322 sk->sk_state_change(sk);
323 break;
324
325 case PNS_PEP_ENABLE_REQ:
326 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
327 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
328 break;
329
330 case PNS_PEP_RESET_REQ:
331 switch (hdr->state_after_reset) {
332 case PN_PIPE_DISABLE:
333 pn->init_enable = 0;
334 break;
335 case PN_PIPE_ENABLE:
336 pn->init_enable = 1;
337 break;
338 default: /* not allowed to send an error here!? */
339 err = -EINVAL;
340 goto out;
341 }
342 /* fall through */
343 case PNS_PEP_DISABLE_REQ:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800344 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700345 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
346 break;
347
348 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700349 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX)
350 break;
351 __skb_pull(skb, 4);
352 queue = &pn->ctrlreq_queue;
353 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700354
355 case PNS_PIPE_DATA:
356 __skb_pull(skb, 3); /* Pipe data header */
357 if (!pn_flow_safe(pn->rx_fc)) {
358 err = sock_queue_rcv_skb(sk, skb);
359 if (!err)
360 return 0;
361 break;
362 }
363
364 if (pn->rx_credits == 0) {
365 err = -ENOBUFS;
366 break;
367 }
368 pn->rx_credits--;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700369 queue = &sk->sk_receive_queue;
370 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700371
372 case PNS_PEP_STATUS_IND:
373 pipe_rcv_status(sk, skb);
374 break;
375
376 case PNS_PIPE_REDIRECTED_IND:
377 err = pipe_rcv_created(sk, skb);
378 break;
379
380 case PNS_PIPE_CREATED_IND:
381 err = pipe_rcv_created(sk, skb);
382 if (err)
383 break;
384 /* fall through */
385 case PNS_PIPE_RESET_IND:
386 if (!pn->init_enable)
387 break;
388 /* fall through */
389 case PNS_PIPE_ENABLED_IND:
390 if (!pn_flow_safe(pn->tx_fc)) {
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800391 atomic_set(&pn->tx_credits, 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700392 sk->sk_write_space(sk);
393 }
394 if (sk->sk_state == TCP_ESTABLISHED)
395 break; /* Nothing to do */
396 sk->sk_state = TCP_ESTABLISHED;
397 pipe_grant_credits(sk);
398 break;
399
400 case PNS_PIPE_DISABLED_IND:
401 sk->sk_state = TCP_SYN_RECV;
402 pn->rx_credits = 0;
403 break;
404
405 default:
406 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
407 hdr->message_id);
408 err = -EINVAL;
409 }
410out:
411 kfree_skb(skb);
412 return err;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700413
414queue:
415 skb->dev = NULL;
416 skb_set_owner_r(skb, sk);
417 err = skb->len;
418 skb_queue_tail(queue, skb);
419 if (!sock_flag(sk, SOCK_DEAD))
420 sk->sk_data_ready(sk, err);
421 return 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700422}
423
424/* Destroy connected sock. */
425static void pipe_destruct(struct sock *sk)
426{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700427 struct pep_sock *pn = pep_sk(sk);
428
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700429 skb_queue_purge(&sk->sk_receive_queue);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700430 skb_queue_purge(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700431}
432
433static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
434{
435 struct sock *newsk;
436 struct pep_sock *newpn, *pn = pep_sk(sk);
437 struct pnpipehdr *hdr;
438 struct sockaddr_pn dst;
439 u16 peer_type;
440 u8 pipe_handle, enabled, n_sb;
441
442 if (!pskb_pull(skb, sizeof(*hdr) + 4))
443 return -EINVAL;
444
445 hdr = pnp_hdr(skb);
446 pipe_handle = hdr->pipe_handle;
447 switch (hdr->state_after_connect) {
448 case PN_PIPE_DISABLE:
449 enabled = 0;
450 break;
451 case PN_PIPE_ENABLE:
452 enabled = 1;
453 break;
454 default:
455 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
456 return -EINVAL;
457 }
458 peer_type = hdr->other_pep_type << 8;
459
460 if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) {
461 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
462 return -ENOBUFS;
463 }
464
465 /* Parse sub-blocks (options) */
466 n_sb = hdr->data[4];
467 while (n_sb > 0) {
468 u8 type, buf[1], len = sizeof(buf);
469 const u8 *data = pep_get_sb(skb, &type, &len, buf);
470
471 if (data == NULL)
472 return -EINVAL;
473 switch (type) {
474 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
475 if (len < 1)
476 return -EINVAL;
477 peer_type = (peer_type & 0xff00) | data[0];
478 break;
479 }
480 n_sb--;
481 }
482
483 skb = skb_clone(skb, GFP_ATOMIC);
484 if (!skb)
485 return -ENOMEM;
486
487 /* Create a new to-be-accepted sock */
488 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
489 if (!newsk) {
490 kfree_skb(skb);
491 return -ENOMEM;
492 }
493 sock_init_data(NULL, newsk);
494 newsk->sk_state = TCP_SYN_RECV;
495 newsk->sk_backlog_rcv = pipe_do_rcv;
496 newsk->sk_protocol = sk->sk_protocol;
497 newsk->sk_destruct = pipe_destruct;
498
499 newpn = pep_sk(newsk);
500 pn_skb_get_dst_sockaddr(skb, &dst);
501 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
502 newpn->pn_sk.resource = pn->pn_sk.resource;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700503 skb_queue_head_init(&newpn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700504 newpn->pipe_handle = pipe_handle;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800505 atomic_set(&newpn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700506 newpn->peer_type = peer_type;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800507 newpn->rx_credits = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700508 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
509 newpn->init_enable = enabled;
510
511 BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
512 skb_queue_head(&newsk->sk_receive_queue, skb);
513 if (!sock_flag(sk, SOCK_DEAD))
514 sk->sk_data_ready(sk, 0);
515
516 sk_acceptq_added(sk);
517 sk_add_node(newsk, &pn->ackq);
518 return 0;
519}
520
521/* Listening sock must be locked */
522static struct sock *pep_find_pipe(const struct hlist_head *hlist,
523 const struct sockaddr_pn *dst,
524 u8 pipe_handle)
525{
526 struct hlist_node *node;
527 struct sock *sknode;
528 u16 dobj = pn_sockaddr_get_object(dst);
529
530 sk_for_each(sknode, node, hlist) {
531 struct pep_sock *pnnode = pep_sk(sknode);
532
533 /* Ports match, but addresses might not: */
534 if (pnnode->pn_sk.sobject != dobj)
535 continue;
536 if (pnnode->pipe_handle != pipe_handle)
537 continue;
538 if (sknode->sk_state == TCP_CLOSE_WAIT)
539 continue;
540
541 sock_hold(sknode);
542 return sknode;
543 }
544 return NULL;
545}
546
547/*
548 * Deliver an skb to a listening sock.
549 * Socket lock must be held.
550 * We then queue the skb to the right connected sock (if any).
551 */
552static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
553{
554 struct pep_sock *pn = pep_sk(sk);
555 struct sock *sknode;
Rémi Denis-Courmont2ddc1ac2009-02-10 17:14:50 -0800556 struct pnpipehdr *hdr;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700557 struct sockaddr_pn dst;
558 int err = NET_RX_SUCCESS;
559 u8 pipe_handle;
560
561 if (!pskb_may_pull(skb, sizeof(*hdr)))
562 goto drop;
563
564 hdr = pnp_hdr(skb);
565 pipe_handle = hdr->pipe_handle;
566 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
567 goto drop;
568
569 pn_skb_get_dst_sockaddr(skb, &dst);
570
571 /* Look for an existing pipe handle */
572 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
573 if (sknode)
574 return sk_receive_skb(sknode, skb, 1);
575
576 /* Look for a pipe handle pending accept */
577 sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
578 if (sknode) {
579 sock_put(sknode);
580 if (net_ratelimit())
581 printk(KERN_WARNING"Phonet unconnected PEP ignored");
582 err = NET_RX_DROP;
583 goto drop;
584 }
585
586 switch (hdr->message_id) {
587 case PNS_PEP_CONNECT_REQ:
588 err = pep_connreq_rcv(sk, skb);
589 break;
590
591 case PNS_PEP_DISCONNECT_REQ:
592 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
593 break;
594
595 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700596 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700597 break;
598
599 case PNS_PEP_RESET_REQ:
600 case PNS_PEP_ENABLE_REQ:
601 case PNS_PEP_DISABLE_REQ:
602 /* invalid handle is not even allowed here! */
603 default:
604 err = NET_RX_DROP;
605 }
606drop:
607 kfree_skb(skb);
608 return err;
609}
610
611/* associated socket ceases to exist */
612static void pep_sock_close(struct sock *sk, long timeout)
613{
614 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700615 int ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700616
617 sk_common_release(sk);
618
619 lock_sock(sk);
620 if (sk->sk_state == TCP_LISTEN) {
621 /* Destroy the listen queue */
622 struct sock *sknode;
623 struct hlist_node *p, *n;
624
625 sk_for_each_safe(sknode, p, n, &pn->ackq)
626 sk_del_node_init(sknode);
627 sk->sk_state = TCP_CLOSE;
628 }
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700629 ifindex = pn->ifindex;
630 pn->ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700631 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700632
633 if (ifindex)
634 gprs_detach(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700635}
636
637static int pep_wait_connreq(struct sock *sk, int noblock)
638{
639 struct task_struct *tsk = current;
640 struct pep_sock *pn = pep_sk(sk);
641 long timeo = sock_rcvtimeo(sk, noblock);
642
643 for (;;) {
644 DEFINE_WAIT(wait);
645
646 if (sk->sk_state != TCP_LISTEN)
647 return -EINVAL;
648 if (!hlist_empty(&pn->ackq))
649 break;
650 if (!timeo)
651 return -EWOULDBLOCK;
652 if (signal_pending(tsk))
653 return sock_intr_errno(timeo);
654
655 prepare_to_wait_exclusive(&sk->sk_socket->wait, &wait,
656 TASK_INTERRUPTIBLE);
657 release_sock(sk);
658 timeo = schedule_timeout(timeo);
659 lock_sock(sk);
660 finish_wait(&sk->sk_socket->wait, &wait);
661 }
662
663 return 0;
664}
665
666static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
667{
668 struct pep_sock *pn = pep_sk(sk);
669 struct sock *newsk = NULL;
670 struct sk_buff *oskb;
671 int err;
672
673 lock_sock(sk);
674 err = pep_wait_connreq(sk, flags & O_NONBLOCK);
675 if (err)
676 goto out;
677
678 newsk = __sk_head(&pn->ackq);
679
680 oskb = skb_dequeue(&newsk->sk_receive_queue);
681 err = pep_accept_conn(newsk, oskb);
682 if (err) {
683 skb_queue_head(&newsk->sk_receive_queue, oskb);
684 newsk = NULL;
685 goto out;
686 }
687
688 sock_hold(sk);
689 pep_sk(newsk)->listener = sk;
690
691 sock_hold(newsk);
692 sk_del_node_init(newsk);
693 sk_acceptq_removed(sk);
694 sk_add_node(newsk, &pn->hlist);
695 __sock_put(newsk);
696
697out:
698 release_sock(sk);
699 *errp = err;
700 return newsk;
701}
702
703static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
704{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700705 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700706 int answ;
707
708 switch (cmd) {
709 case SIOCINQ:
710 if (sk->sk_state == TCP_LISTEN)
711 return -EINVAL;
712
713 lock_sock(sk);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700714 if (sock_flag(sk, SOCK_URGINLINE)
715 && !skb_queue_empty(&pn->ctrlreq_queue))
716 answ = skb_peek(&pn->ctrlreq_queue)->len;
717 else if (!skb_queue_empty(&sk->sk_receive_queue))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700718 answ = skb_peek(&sk->sk_receive_queue)->len;
719 else
720 answ = 0;
721 release_sock(sk);
722 return put_user(answ, (int __user *)arg);
723 }
724
725 return -ENOIOCTLCMD;
726}
727
728static int pep_init(struct sock *sk)
729{
730 struct pep_sock *pn = pep_sk(sk);
731
732 INIT_HLIST_HEAD(&pn->ackq);
733 INIT_HLIST_HEAD(&pn->hlist);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700734 skb_queue_head_init(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700735 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
736 return 0;
737}
738
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700739static int pep_setsockopt(struct sock *sk, int level, int optname,
740 char __user *optval, int optlen)
741{
742 struct pep_sock *pn = pep_sk(sk);
743 int val = 0, err = 0;
744
745 if (level != SOL_PNPIPE)
746 return -ENOPROTOOPT;
747 if (optlen >= sizeof(int)) {
748 if (get_user(val, (int __user *) optval))
749 return -EFAULT;
750 }
751
752 lock_sock(sk);
753 switch (optname) {
754 case PNPIPE_ENCAP:
755 if (val && val != PNPIPE_ENCAP_IP) {
756 err = -EINVAL;
757 break;
758 }
759 if (!pn->ifindex == !val)
760 break; /* Nothing to do! */
761 if (!capable(CAP_NET_ADMIN)) {
762 err = -EPERM;
763 break;
764 }
765 if (val) {
766 release_sock(sk);
767 err = gprs_attach(sk);
768 if (err > 0) {
769 pn->ifindex = err;
770 err = 0;
771 }
772 } else {
773 pn->ifindex = 0;
774 release_sock(sk);
775 gprs_detach(sk);
776 err = 0;
777 }
778 goto out_norel;
779 default:
780 err = -ENOPROTOOPT;
781 }
782 release_sock(sk);
783
784out_norel:
785 return err;
786}
787
788static int pep_getsockopt(struct sock *sk, int level, int optname,
789 char __user *optval, int __user *optlen)
790{
791 struct pep_sock *pn = pep_sk(sk);
792 int len, val;
793
794 if (level != SOL_PNPIPE)
795 return -ENOPROTOOPT;
796 if (get_user(len, optlen))
797 return -EFAULT;
798
799 switch (optname) {
800 case PNPIPE_ENCAP:
801 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
802 break;
803 case PNPIPE_IFINDEX:
804 val = pn->ifindex;
805 break;
806 default:
807 return -ENOPROTOOPT;
808 }
809
810 len = min_t(unsigned int, sizeof(int), len);
811 if (put_user(len, optlen))
812 return -EFAULT;
813 if (put_user(val, (int __user *) optval))
814 return -EFAULT;
815 return 0;
816}
817
818static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
819{
820 struct pep_sock *pn = pep_sk(sk);
821 struct pnpipehdr *ph;
822
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800823 if (pn_flow_safe(pn->tx_fc) &&
824 !atomic_add_unless(&pn->tx_credits, -1, 0)) {
825 kfree_skb(skb);
826 return -ENOBUFS;
827 }
828
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700829 skb_push(skb, 3);
830 skb_reset_transport_header(skb);
831 ph = pnp_hdr(skb);
832 ph->utid = 0;
833 ph->message_id = PNS_PIPE_DATA;
834 ph->pipe_handle = pn->pipe_handle;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700835
836 return pn_skb_send(sk, skb, &pipe_srv);
837}
838
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700839static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
840 struct msghdr *msg, size_t len)
841{
842 struct pep_sock *pn = pep_sk(sk);
843 struct sk_buff *skb = NULL;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700844 long timeo;
845 int flags = msg->msg_flags;
846 int err, done;
847
848 if (msg->msg_flags & MSG_OOB || !(msg->msg_flags & MSG_EOR))
849 return -EOPNOTSUPP;
850
851 lock_sock(sk);
852 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
853 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
854 err = -ENOTCONN;
855 goto out;
856 }
857 if (sk->sk_state != TCP_ESTABLISHED) {
858 /* Wait until the pipe gets to enabled state */
859disabled:
860 err = sk_stream_wait_connect(sk, &timeo);
861 if (err)
862 goto out;
863
864 if (sk->sk_state == TCP_CLOSE_WAIT) {
865 err = -ECONNRESET;
866 goto out;
867 }
868 }
869 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
870
871 /* Wait until flow control allows TX */
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800872 done = atomic_read(&pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700873 while (!done) {
874 DEFINE_WAIT(wait);
875
876 if (!timeo) {
877 err = -EAGAIN;
878 goto out;
879 }
880 if (signal_pending(current)) {
881 err = sock_intr_errno(timeo);
882 goto out;
883 }
884
885 prepare_to_wait(&sk->sk_socket->wait, &wait,
886 TASK_INTERRUPTIBLE);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800887 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700888 finish_wait(&sk->sk_socket->wait, &wait);
889
890 if (sk->sk_state != TCP_ESTABLISHED)
891 goto disabled;
892 }
893
894 if (!skb) {
895 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
896 flags & MSG_DONTWAIT, &err);
897 if (skb == NULL)
898 goto out;
899 skb_reserve(skb, MAX_PHONET_HEADER + 3);
900
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800901 if (sk->sk_state != TCP_ESTABLISHED ||
902 !atomic_read(&pn->tx_credits))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700903 goto disabled; /* sock_alloc_send_skb might sleep */
904 }
905
906 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
907 if (err < 0)
908 goto out;
909
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700910 err = pipe_skb_send(sk, skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700911 if (err >= 0)
912 err = len; /* success! */
913 skb = NULL;
914out:
915 release_sock(sk);
916 kfree_skb(skb);
917 return err;
918}
919
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700920int pep_writeable(struct sock *sk)
921{
922 struct pep_sock *pn = pep_sk(sk);
923
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800924 return atomic_read(&pn->tx_credits);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700925}
926
927int pep_write(struct sock *sk, struct sk_buff *skb)
928{
929 struct sk_buff *rskb, *fs;
930 int flen = 0;
931
932 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
933 if (!rskb) {
934 kfree_skb(skb);
935 return -ENOMEM;
936 }
937 skb_shinfo(rskb)->frag_list = skb;
938 rskb->len += skb->len;
939 rskb->data_len += rskb->len;
940 rskb->truesize += rskb->len;
941
942 /* Avoid nested fragments */
943 for (fs = skb_shinfo(skb)->frag_list; fs; fs = fs->next)
944 flen += fs->len;
945 skb->next = skb_shinfo(skb)->frag_list;
946 skb_shinfo(skb)->frag_list = NULL;
947 skb->len -= flen;
948 skb->data_len -= flen;
949 skb->truesize -= flen;
950
951 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
952 return pipe_skb_send(sk, rskb);
953}
954
955struct sk_buff *pep_read(struct sock *sk)
956{
957 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
958
959 if (sk->sk_state == TCP_ESTABLISHED)
960 pipe_grant_credits(sk);
961 return skb;
962}
963
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700964static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
965 struct msghdr *msg, size_t len, int noblock,
966 int flags, int *addr_len)
967{
968 struct sk_buff *skb;
969 int err;
970
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700971 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
972 return -ENOTCONN;
973
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700974 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
975 /* Dequeue and acknowledge control request */
976 struct pep_sock *pn = pep_sk(sk);
977
978 skb = skb_dequeue(&pn->ctrlreq_queue);
979 if (skb) {
980 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
981 GFP_KERNEL);
982 msg->msg_flags |= MSG_OOB;
983 goto copy;
984 }
985 if (flags & MSG_OOB)
986 return -EINVAL;
987 }
988
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700989 skb = skb_recv_datagram(sk, flags, noblock, &err);
990 lock_sock(sk);
991 if (skb == NULL) {
992 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
993 err = -ECONNRESET;
994 release_sock(sk);
995 return err;
996 }
997
998 if (sk->sk_state == TCP_ESTABLISHED)
999 pipe_grant_credits(sk);
1000 release_sock(sk);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001001copy:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001002 msg->msg_flags |= MSG_EOR;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001003 if (skb->len > len)
1004 msg->msg_flags |= MSG_TRUNC;
1005 else
1006 len = skb->len;
1007
1008 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1009 if (!err)
1010 err = (flags & MSG_TRUNC) ? skb->len : len;
1011
1012 skb_free_datagram(sk, skb);
1013 return err;
1014}
1015
1016static void pep_sock_unhash(struct sock *sk)
1017{
1018 struct pep_sock *pn = pep_sk(sk);
1019 struct sock *skparent = NULL;
1020
1021 lock_sock(sk);
1022 if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1023 skparent = pn->listener;
1024 sk_del_node_init(sk);
1025 release_sock(sk);
1026
1027 sk = skparent;
1028 pn = pep_sk(skparent);
1029 lock_sock(sk);
1030 }
1031 /* Unhash a listening sock only when it is closed
1032 * and all of its active connected pipes are closed. */
1033 if (hlist_empty(&pn->hlist))
1034 pn_sock_unhash(&pn->pn_sk.sk);
1035 release_sock(sk);
1036
1037 if (skparent)
1038 sock_put(skparent);
1039}
1040
1041static struct proto pep_proto = {
1042 .close = pep_sock_close,
1043 .accept = pep_sock_accept,
1044 .ioctl = pep_ioctl,
1045 .init = pep_init,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001046 .setsockopt = pep_setsockopt,
1047 .getsockopt = pep_getsockopt,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001048 .sendmsg = pep_sendmsg,
1049 .recvmsg = pep_recvmsg,
1050 .backlog_rcv = pep_do_rcv,
1051 .hash = pn_sock_hash,
1052 .unhash = pep_sock_unhash,
1053 .get_port = pn_sock_get_port,
1054 .obj_size = sizeof(struct pep_sock),
1055 .owner = THIS_MODULE,
1056 .name = "PNPIPE",
1057};
1058
1059static struct phonet_protocol pep_pn_proto = {
1060 .ops = &phonet_stream_ops,
1061 .prot = &pep_proto,
1062 .sock_type = SOCK_SEQPACKET,
1063};
1064
1065static int __init pep_register(void)
1066{
1067 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1068}
1069
1070static void __exit pep_unregister(void)
1071{
1072 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1073}
1074
1075module_init(pep_register);
1076module_exit(pep_unregister);
1077MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1078MODULE_DESCRIPTION("Phonet pipe protocol");
1079MODULE_LICENSE("GPL");
1080MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);