blob: e2a95762abd37685c6b10a63795df539ffe0b295 [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;
91
92 skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
93 if (!skb)
94 return -ENOMEM;
95 skb_set_owner_w(skb, sk);
96
97 skb_reserve(skb, MAX_PNPIPE_HEADER);
98 __skb_put(skb, len);
99 skb_copy_to_linear_data(skb, data, len);
100 __skb_push(skb, sizeof(*ph));
101 skb_reset_transport_header(skb);
102 ph = pnp_hdr(skb);
103 ph->utid = oph->utid;
104 ph->message_id = oph->message_id + 1; /* REQ -> RESP */
105 ph->pipe_handle = oph->pipe_handle;
106 ph->error_code = code;
107
108 return pn_skb_send(sk, skb, &pipe_srv);
109}
110
111#define PAD 0x00
112static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
113{
114 static const u8 data[20] = {
115 PAD, PAD, PAD, 2 /* sub-blocks */,
116 PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
117 PN_MULTI_CREDIT_FLOW_CONTROL,
118 PN_ONE_CREDIT_FLOW_CONTROL,
119 PN_LEGACY_FLOW_CONTROL,
120 PAD,
121 PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
122 PN_MULTI_CREDIT_FLOW_CONTROL,
123 PN_ONE_CREDIT_FLOW_CONTROL,
124 PN_LEGACY_FLOW_CONTROL,
125 PAD,
126 };
127
128 might_sleep();
129 return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
130 GFP_KERNEL);
131}
132
133static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code)
134{
135 static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
136 WARN_ON(code == PN_PIPE_NO_ERROR);
137 return pep_reply(sk, skb, code, data, sizeof(data), GFP_ATOMIC);
138}
139
140/* Control requests are not sent by the pipe service and have a specific
141 * message format. */
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700142static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
143 gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700144{
145 const struct pnpipehdr *oph = pnp_hdr(oskb);
146 struct sk_buff *skb;
147 struct pnpipehdr *ph;
148 struct sockaddr_pn dst;
149
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700150 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700151 if (!skb)
152 return -ENOMEM;
153 skb_set_owner_w(skb, sk);
154
155 skb_reserve(skb, MAX_PHONET_HEADER);
156 ph = (struct pnpipehdr *)skb_put(skb, sizeof(*ph) + 4);
157
158 ph->utid = oph->utid;
159 ph->message_id = PNS_PEP_CTRL_RESP;
160 ph->pipe_handle = oph->pipe_handle;
161 ph->data[0] = oph->data[1]; /* CTRL id */
162 ph->data[1] = oph->data[0]; /* PEP type */
163 ph->data[2] = code; /* error code, at an usual offset */
164 ph->data[3] = PAD;
165 ph->data[4] = PAD;
166
167 pn_skb_get_src_sockaddr(oskb, &dst);
168 return pn_skb_send(sk, skb, &dst);
169}
170
171static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
172{
173 struct pep_sock *pn = pep_sk(sk);
174 struct pnpipehdr *ph;
175 struct sk_buff *skb;
176
177 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
178 if (!skb)
179 return -ENOMEM;
180 skb_set_owner_w(skb, sk);
181
182 skb_reserve(skb, MAX_PNPIPE_HEADER + 4);
183 __skb_push(skb, sizeof(*ph) + 4);
184 skb_reset_transport_header(skb);
185 ph = pnp_hdr(skb);
186 ph->utid = 0;
187 ph->message_id = PNS_PEP_STATUS_IND;
188 ph->pipe_handle = pn->pipe_handle;
189 ph->pep_type = PN_PEP_TYPE_COMMON;
190 ph->data[1] = type;
191 ph->data[2] = PAD;
192 ph->data[3] = PAD;
193 ph->data[4] = status;
194
195 return pn_skb_send(sk, skb, &pipe_srv);
196}
197
198/* Send our RX flow control information to the sender.
199 * Socket must be locked. */
200static void pipe_grant_credits(struct sock *sk)
201{
202 struct pep_sock *pn = pep_sk(sk);
203
204 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
205
206 switch (pn->rx_fc) {
207 case PN_LEGACY_FLOW_CONTROL: /* TODO */
208 break;
209 case PN_ONE_CREDIT_FLOW_CONTROL:
210 pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
211 PEP_IND_READY, GFP_ATOMIC);
212 pn->rx_credits = 1;
213 break;
214 case PN_MULTI_CREDIT_FLOW_CONTROL:
215 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
216 break;
217 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
218 CREDITS_MAX - pn->rx_credits,
219 GFP_ATOMIC) == 0)
220 pn->rx_credits = CREDITS_MAX;
221 break;
222 }
223}
224
225static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
226{
227 struct pep_sock *pn = pep_sk(sk);
228 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800229 int wake = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700230
231 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
232 return -EINVAL;
233
234 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
235 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
236 (unsigned)hdr->data[0]);
237 return -EOPNOTSUPP;
238 }
239
240 switch (hdr->data[1]) {
241 case PN_PEP_IND_FLOW_CONTROL:
242 switch (pn->tx_fc) {
243 case PN_LEGACY_FLOW_CONTROL:
244 switch (hdr->data[4]) {
245 case PEP_IND_BUSY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800246 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700247 break;
248 case PEP_IND_READY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800249 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700250 break;
251 }
252 break;
253 case PN_ONE_CREDIT_FLOW_CONTROL:
254 if (hdr->data[4] == PEP_IND_READY)
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800255 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700256 break;
257 }
258 break;
259
260 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
261 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
262 break;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800263 atomic_add(wake = hdr->data[4], &pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700264 break;
265
266 default:
267 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
268 (unsigned)hdr->data[1]);
269 return -EOPNOTSUPP;
270 }
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800271 if (wake)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700272 sk->sk_write_space(sk);
273 return 0;
274}
275
276static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
277{
278 struct pep_sock *pn = pep_sk(sk);
279 struct pnpipehdr *hdr = pnp_hdr(skb);
280 u8 n_sb = hdr->data[0];
281
282 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
283 __skb_pull(skb, sizeof(*hdr));
284 while (n_sb > 0) {
285 u8 type, buf[2], len = sizeof(buf);
286 u8 *data = pep_get_sb(skb, &type, &len, buf);
287
288 if (data == NULL)
289 return -EINVAL;
290 switch (type) {
291 case PN_PIPE_SB_NEGOTIATED_FC:
292 if (len < 2 || (data[0] | data[1]) > 3)
293 break;
294 pn->tx_fc = data[0] & 3;
295 pn->rx_fc = data[1] & 3;
296 break;
297 }
298 n_sb--;
299 }
300 return 0;
301}
302
303/* Queue an skb to a connected sock.
304 * Socket lock must be held. */
305static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
306{
307 struct pep_sock *pn = pep_sk(sk);
308 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700309 struct sk_buff_head *queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700310 int err = 0;
311
312 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
313
314 switch (hdr->message_id) {
315 case PNS_PEP_CONNECT_REQ:
316 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
317 break;
318
319 case PNS_PEP_DISCONNECT_REQ:
320 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
321 sk->sk_state = TCP_CLOSE_WAIT;
322 if (!sock_flag(sk, SOCK_DEAD))
323 sk->sk_state_change(sk);
324 break;
325
326 case PNS_PEP_ENABLE_REQ:
327 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
328 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
329 break;
330
331 case PNS_PEP_RESET_REQ:
332 switch (hdr->state_after_reset) {
333 case PN_PIPE_DISABLE:
334 pn->init_enable = 0;
335 break;
336 case PN_PIPE_ENABLE:
337 pn->init_enable = 1;
338 break;
339 default: /* not allowed to send an error here!? */
340 err = -EINVAL;
341 goto out;
342 }
343 /* fall through */
344 case PNS_PEP_DISABLE_REQ:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800345 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700346 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
347 break;
348
349 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000350 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
351 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700352 break;
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000353 }
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700354 __skb_pull(skb, 4);
355 queue = &pn->ctrlreq_queue;
356 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700357
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +0000358 case PNS_PIPE_ALIGNED_DATA:
359 __skb_pull(skb, 1);
360 /* fall through */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700361 case PNS_PIPE_DATA:
362 __skb_pull(skb, 3); /* Pipe data header */
363 if (!pn_flow_safe(pn->rx_fc)) {
364 err = sock_queue_rcv_skb(sk, skb);
365 if (!err)
366 return 0;
367 break;
368 }
369
370 if (pn->rx_credits == 0) {
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000371 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700372 err = -ENOBUFS;
373 break;
374 }
375 pn->rx_credits--;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700376 queue = &sk->sk_receive_queue;
377 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700378
379 case PNS_PEP_STATUS_IND:
380 pipe_rcv_status(sk, skb);
381 break;
382
383 case PNS_PIPE_REDIRECTED_IND:
384 err = pipe_rcv_created(sk, skb);
385 break;
386
387 case PNS_PIPE_CREATED_IND:
388 err = pipe_rcv_created(sk, skb);
389 if (err)
390 break;
391 /* fall through */
392 case PNS_PIPE_RESET_IND:
393 if (!pn->init_enable)
394 break;
395 /* fall through */
396 case PNS_PIPE_ENABLED_IND:
397 if (!pn_flow_safe(pn->tx_fc)) {
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800398 atomic_set(&pn->tx_credits, 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700399 sk->sk_write_space(sk);
400 }
401 if (sk->sk_state == TCP_ESTABLISHED)
402 break; /* Nothing to do */
403 sk->sk_state = TCP_ESTABLISHED;
404 pipe_grant_credits(sk);
405 break;
406
407 case PNS_PIPE_DISABLED_IND:
408 sk->sk_state = TCP_SYN_RECV;
409 pn->rx_credits = 0;
410 break;
411
412 default:
413 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
414 hdr->message_id);
415 err = -EINVAL;
416 }
417out:
418 kfree_skb(skb);
419 return err;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700420
421queue:
422 skb->dev = NULL;
423 skb_set_owner_r(skb, sk);
424 err = skb->len;
425 skb_queue_tail(queue, skb);
426 if (!sock_flag(sk, SOCK_DEAD))
427 sk->sk_data_ready(sk, err);
428 return 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700429}
430
431/* Destroy connected sock. */
432static void pipe_destruct(struct sock *sk)
433{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700434 struct pep_sock *pn = pep_sk(sk);
435
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700436 skb_queue_purge(&sk->sk_receive_queue);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700437 skb_queue_purge(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700438}
439
440static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
441{
442 struct sock *newsk;
443 struct pep_sock *newpn, *pn = pep_sk(sk);
444 struct pnpipehdr *hdr;
445 struct sockaddr_pn dst;
446 u16 peer_type;
447 u8 pipe_handle, enabled, n_sb;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000448 u8 aligned = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700449
450 if (!pskb_pull(skb, sizeof(*hdr) + 4))
451 return -EINVAL;
452
453 hdr = pnp_hdr(skb);
454 pipe_handle = hdr->pipe_handle;
455 switch (hdr->state_after_connect) {
456 case PN_PIPE_DISABLE:
457 enabled = 0;
458 break;
459 case PN_PIPE_ENABLE:
460 enabled = 1;
461 break;
462 default:
463 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
464 return -EINVAL;
465 }
466 peer_type = hdr->other_pep_type << 8;
467
468 if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) {
469 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
470 return -ENOBUFS;
471 }
472
473 /* Parse sub-blocks (options) */
474 n_sb = hdr->data[4];
475 while (n_sb > 0) {
476 u8 type, buf[1], len = sizeof(buf);
477 const u8 *data = pep_get_sb(skb, &type, &len, buf);
478
479 if (data == NULL)
480 return -EINVAL;
481 switch (type) {
482 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
483 if (len < 1)
484 return -EINVAL;
485 peer_type = (peer_type & 0xff00) | data[0];
486 break;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000487 case PN_PIPE_SB_ALIGNED_DATA:
488 aligned = data[0] != 0;
489 break;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700490 }
491 n_sb--;
492 }
493
494 skb = skb_clone(skb, GFP_ATOMIC);
495 if (!skb)
496 return -ENOMEM;
497
498 /* Create a new to-be-accepted sock */
499 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
500 if (!newsk) {
501 kfree_skb(skb);
502 return -ENOMEM;
503 }
504 sock_init_data(NULL, newsk);
505 newsk->sk_state = TCP_SYN_RECV;
506 newsk->sk_backlog_rcv = pipe_do_rcv;
507 newsk->sk_protocol = sk->sk_protocol;
508 newsk->sk_destruct = pipe_destruct;
509
510 newpn = pep_sk(newsk);
511 pn_skb_get_dst_sockaddr(skb, &dst);
512 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
513 newpn->pn_sk.resource = pn->pn_sk.resource;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700514 skb_queue_head_init(&newpn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700515 newpn->pipe_handle = pipe_handle;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800516 atomic_set(&newpn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700517 newpn->peer_type = peer_type;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800518 newpn->rx_credits = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700519 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
520 newpn->init_enable = enabled;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000521 newpn->aligned = aligned;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700522
523 BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
524 skb_queue_head(&newsk->sk_receive_queue, skb);
525 if (!sock_flag(sk, SOCK_DEAD))
526 sk->sk_data_ready(sk, 0);
527
528 sk_acceptq_added(sk);
529 sk_add_node(newsk, &pn->ackq);
530 return 0;
531}
532
533/* Listening sock must be locked */
534static struct sock *pep_find_pipe(const struct hlist_head *hlist,
535 const struct sockaddr_pn *dst,
536 u8 pipe_handle)
537{
538 struct hlist_node *node;
539 struct sock *sknode;
540 u16 dobj = pn_sockaddr_get_object(dst);
541
542 sk_for_each(sknode, node, hlist) {
543 struct pep_sock *pnnode = pep_sk(sknode);
544
545 /* Ports match, but addresses might not: */
546 if (pnnode->pn_sk.sobject != dobj)
547 continue;
548 if (pnnode->pipe_handle != pipe_handle)
549 continue;
550 if (sknode->sk_state == TCP_CLOSE_WAIT)
551 continue;
552
553 sock_hold(sknode);
554 return sknode;
555 }
556 return NULL;
557}
558
559/*
560 * Deliver an skb to a listening sock.
561 * Socket lock must be held.
562 * We then queue the skb to the right connected sock (if any).
563 */
564static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
565{
566 struct pep_sock *pn = pep_sk(sk);
567 struct sock *sknode;
Rémi Denis-Courmont2ddc1ac2009-02-10 17:14:50 -0800568 struct pnpipehdr *hdr;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700569 struct sockaddr_pn dst;
570 int err = NET_RX_SUCCESS;
571 u8 pipe_handle;
572
573 if (!pskb_may_pull(skb, sizeof(*hdr)))
574 goto drop;
575
576 hdr = pnp_hdr(skb);
577 pipe_handle = hdr->pipe_handle;
578 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
579 goto drop;
580
581 pn_skb_get_dst_sockaddr(skb, &dst);
582
583 /* Look for an existing pipe handle */
584 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
585 if (sknode)
586 return sk_receive_skb(sknode, skb, 1);
587
588 /* Look for a pipe handle pending accept */
589 sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
590 if (sknode) {
591 sock_put(sknode);
592 if (net_ratelimit())
593 printk(KERN_WARNING"Phonet unconnected PEP ignored");
594 err = NET_RX_DROP;
595 goto drop;
596 }
597
598 switch (hdr->message_id) {
599 case PNS_PEP_CONNECT_REQ:
600 err = pep_connreq_rcv(sk, skb);
601 break;
602
603 case PNS_PEP_DISCONNECT_REQ:
604 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
605 break;
606
607 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700608 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700609 break;
610
611 case PNS_PEP_RESET_REQ:
612 case PNS_PEP_ENABLE_REQ:
613 case PNS_PEP_DISABLE_REQ:
614 /* invalid handle is not even allowed here! */
615 default:
616 err = NET_RX_DROP;
617 }
618drop:
619 kfree_skb(skb);
620 return err;
621}
622
623/* associated socket ceases to exist */
624static void pep_sock_close(struct sock *sk, long timeout)
625{
626 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700627 int ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700628
629 sk_common_release(sk);
630
631 lock_sock(sk);
632 if (sk->sk_state == TCP_LISTEN) {
633 /* Destroy the listen queue */
634 struct sock *sknode;
635 struct hlist_node *p, *n;
636
637 sk_for_each_safe(sknode, p, n, &pn->ackq)
638 sk_del_node_init(sknode);
639 sk->sk_state = TCP_CLOSE;
640 }
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700641 ifindex = pn->ifindex;
642 pn->ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700643 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700644
645 if (ifindex)
646 gprs_detach(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700647}
648
649static int pep_wait_connreq(struct sock *sk, int noblock)
650{
651 struct task_struct *tsk = current;
652 struct pep_sock *pn = pep_sk(sk);
653 long timeo = sock_rcvtimeo(sk, noblock);
654
655 for (;;) {
656 DEFINE_WAIT(wait);
657
658 if (sk->sk_state != TCP_LISTEN)
659 return -EINVAL;
660 if (!hlist_empty(&pn->ackq))
661 break;
662 if (!timeo)
663 return -EWOULDBLOCK;
664 if (signal_pending(tsk))
665 return sock_intr_errno(timeo);
666
667 prepare_to_wait_exclusive(&sk->sk_socket->wait, &wait,
668 TASK_INTERRUPTIBLE);
669 release_sock(sk);
670 timeo = schedule_timeout(timeo);
671 lock_sock(sk);
672 finish_wait(&sk->sk_socket->wait, &wait);
673 }
674
675 return 0;
676}
677
678static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
679{
680 struct pep_sock *pn = pep_sk(sk);
681 struct sock *newsk = NULL;
682 struct sk_buff *oskb;
683 int err;
684
685 lock_sock(sk);
686 err = pep_wait_connreq(sk, flags & O_NONBLOCK);
687 if (err)
688 goto out;
689
690 newsk = __sk_head(&pn->ackq);
691
692 oskb = skb_dequeue(&newsk->sk_receive_queue);
693 err = pep_accept_conn(newsk, oskb);
694 if (err) {
695 skb_queue_head(&newsk->sk_receive_queue, oskb);
696 newsk = NULL;
697 goto out;
698 }
699
700 sock_hold(sk);
701 pep_sk(newsk)->listener = sk;
702
703 sock_hold(newsk);
704 sk_del_node_init(newsk);
705 sk_acceptq_removed(sk);
706 sk_add_node(newsk, &pn->hlist);
707 __sock_put(newsk);
708
709out:
710 release_sock(sk);
711 *errp = err;
712 return newsk;
713}
714
715static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
716{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700717 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700718 int answ;
719
720 switch (cmd) {
721 case SIOCINQ:
722 if (sk->sk_state == TCP_LISTEN)
723 return -EINVAL;
724
725 lock_sock(sk);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800726 if (sock_flag(sk, SOCK_URGINLINE) &&
727 !skb_queue_empty(&pn->ctrlreq_queue))
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700728 answ = skb_peek(&pn->ctrlreq_queue)->len;
729 else if (!skb_queue_empty(&sk->sk_receive_queue))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700730 answ = skb_peek(&sk->sk_receive_queue)->len;
731 else
732 answ = 0;
733 release_sock(sk);
734 return put_user(answ, (int __user *)arg);
735 }
736
737 return -ENOIOCTLCMD;
738}
739
740static int pep_init(struct sock *sk)
741{
742 struct pep_sock *pn = pep_sk(sk);
743
744 INIT_HLIST_HEAD(&pn->ackq);
745 INIT_HLIST_HEAD(&pn->hlist);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700746 skb_queue_head_init(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700747 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
748 return 0;
749}
750
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700751static int pep_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700752 char __user *optval, unsigned int optlen)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700753{
754 struct pep_sock *pn = pep_sk(sk);
755 int val = 0, err = 0;
756
757 if (level != SOL_PNPIPE)
758 return -ENOPROTOOPT;
759 if (optlen >= sizeof(int)) {
760 if (get_user(val, (int __user *) optval))
761 return -EFAULT;
762 }
763
764 lock_sock(sk);
765 switch (optname) {
766 case PNPIPE_ENCAP:
767 if (val && val != PNPIPE_ENCAP_IP) {
768 err = -EINVAL;
769 break;
770 }
771 if (!pn->ifindex == !val)
772 break; /* Nothing to do! */
773 if (!capable(CAP_NET_ADMIN)) {
774 err = -EPERM;
775 break;
776 }
777 if (val) {
778 release_sock(sk);
779 err = gprs_attach(sk);
780 if (err > 0) {
781 pn->ifindex = err;
782 err = 0;
783 }
784 } else {
785 pn->ifindex = 0;
786 release_sock(sk);
787 gprs_detach(sk);
788 err = 0;
789 }
790 goto out_norel;
791 default:
792 err = -ENOPROTOOPT;
793 }
794 release_sock(sk);
795
796out_norel:
797 return err;
798}
799
800static int pep_getsockopt(struct sock *sk, int level, int optname,
801 char __user *optval, int __user *optlen)
802{
803 struct pep_sock *pn = pep_sk(sk);
804 int len, val;
805
806 if (level != SOL_PNPIPE)
807 return -ENOPROTOOPT;
808 if (get_user(len, optlen))
809 return -EFAULT;
810
811 switch (optname) {
812 case PNPIPE_ENCAP:
813 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
814 break;
815 case PNPIPE_IFINDEX:
816 val = pn->ifindex;
817 break;
818 default:
819 return -ENOPROTOOPT;
820 }
821
822 len = min_t(unsigned int, sizeof(int), len);
823 if (put_user(len, optlen))
824 return -EFAULT;
825 if (put_user(val, (int __user *) optval))
826 return -EFAULT;
827 return 0;
828}
829
830static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
831{
832 struct pep_sock *pn = pep_sk(sk);
833 struct pnpipehdr *ph;
834
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800835 if (pn_flow_safe(pn->tx_fc) &&
836 !atomic_add_unless(&pn->tx_credits, -1, 0)) {
837 kfree_skb(skb);
838 return -ENOBUFS;
839 }
840
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000841 skb_push(skb, 3 + pn->aligned);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700842 skb_reset_transport_header(skb);
843 ph = pnp_hdr(skb);
844 ph->utid = 0;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000845 if (pn->aligned) {
846 ph->message_id = PNS_PIPE_ALIGNED_DATA;
847 ph->data[0] = 0; /* padding */
848 } else
849 ph->message_id = PNS_PIPE_DATA;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700850 ph->pipe_handle = pn->pipe_handle;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700851
852 return pn_skb_send(sk, skb, &pipe_srv);
853}
854
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700855static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
856 struct msghdr *msg, size_t len)
857{
858 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +0000859 struct sk_buff *skb;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700860 long timeo;
861 int flags = msg->msg_flags;
862 int err, done;
863
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +0000864 if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
865 MSG_CMSG_COMPAT)) ||
866 !(msg->msg_flags & MSG_EOR))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700867 return -EOPNOTSUPP;
868
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +0000869 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
870 flags & MSG_DONTWAIT, &err);
871 if (!skb)
872 return -ENOBUFS;
873
874 skb_reserve(skb, MAX_PHONET_HEADER + 3);
875 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
876 if (err < 0)
877 goto outfree;
878
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700879 lock_sock(sk);
880 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
881 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
882 err = -ENOTCONN;
883 goto out;
884 }
885 if (sk->sk_state != TCP_ESTABLISHED) {
886 /* Wait until the pipe gets to enabled state */
887disabled:
888 err = sk_stream_wait_connect(sk, &timeo);
889 if (err)
890 goto out;
891
892 if (sk->sk_state == TCP_CLOSE_WAIT) {
893 err = -ECONNRESET;
894 goto out;
895 }
896 }
897 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
898
899 /* Wait until flow control allows TX */
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800900 done = atomic_read(&pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700901 while (!done) {
902 DEFINE_WAIT(wait);
903
904 if (!timeo) {
905 err = -EAGAIN;
906 goto out;
907 }
908 if (signal_pending(current)) {
909 err = sock_intr_errno(timeo);
910 goto out;
911 }
912
913 prepare_to_wait(&sk->sk_socket->wait, &wait,
914 TASK_INTERRUPTIBLE);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800915 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700916 finish_wait(&sk->sk_socket->wait, &wait);
917
918 if (sk->sk_state != TCP_ESTABLISHED)
919 goto disabled;
920 }
921
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700922 err = pipe_skb_send(sk, skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700923 if (err >= 0)
924 err = len; /* success! */
925 skb = NULL;
926out:
927 release_sock(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +0000928outfree:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700929 kfree_skb(skb);
930 return err;
931}
932
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700933int pep_writeable(struct sock *sk)
934{
935 struct pep_sock *pn = pep_sk(sk);
936
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800937 return atomic_read(&pn->tx_credits);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700938}
939
940int pep_write(struct sock *sk, struct sk_buff *skb)
941{
942 struct sk_buff *rskb, *fs;
943 int flen = 0;
944
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000945 if (pep_sk(sk)->aligned)
946 return pipe_skb_send(sk, skb);
947
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700948 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
949 if (!rskb) {
950 kfree_skb(skb);
951 return -ENOMEM;
952 }
953 skb_shinfo(rskb)->frag_list = skb;
954 rskb->len += skb->len;
955 rskb->data_len += rskb->len;
956 rskb->truesize += rskb->len;
957
958 /* Avoid nested fragments */
David S. Miller5c313e92009-06-09 00:21:58 -0700959 skb_walk_frags(skb, fs)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700960 flen += fs->len;
961 skb->next = skb_shinfo(skb)->frag_list;
David S. Miller5c313e92009-06-09 00:21:58 -0700962 skb_frag_list_init(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700963 skb->len -= flen;
964 skb->data_len -= flen;
965 skb->truesize -= flen;
966
967 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
968 return pipe_skb_send(sk, rskb);
969}
970
971struct sk_buff *pep_read(struct sock *sk)
972{
973 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
974
975 if (sk->sk_state == TCP_ESTABLISHED)
976 pipe_grant_credits(sk);
977 return skb;
978}
979
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700980static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
981 struct msghdr *msg, size_t len, int noblock,
982 int flags, int *addr_len)
983{
984 struct sk_buff *skb;
985 int err;
986
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +0000987 if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
988 MSG_NOSIGNAL|MSG_CMSG_COMPAT))
989 return -EOPNOTSUPP;
990
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700991 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
992 return -ENOTCONN;
993
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700994 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
995 /* Dequeue and acknowledge control request */
996 struct pep_sock *pn = pep_sk(sk);
997
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +0000998 if (flags & MSG_PEEK)
999 return -EOPNOTSUPP;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001000 skb = skb_dequeue(&pn->ctrlreq_queue);
1001 if (skb) {
1002 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1003 GFP_KERNEL);
1004 msg->msg_flags |= MSG_OOB;
1005 goto copy;
1006 }
1007 if (flags & MSG_OOB)
1008 return -EINVAL;
1009 }
1010
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001011 skb = skb_recv_datagram(sk, flags, noblock, &err);
1012 lock_sock(sk);
1013 if (skb == NULL) {
1014 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1015 err = -ECONNRESET;
1016 release_sock(sk);
1017 return err;
1018 }
1019
1020 if (sk->sk_state == TCP_ESTABLISHED)
1021 pipe_grant_credits(sk);
1022 release_sock(sk);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001023copy:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001024 msg->msg_flags |= MSG_EOR;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001025 if (skb->len > len)
1026 msg->msg_flags |= MSG_TRUNC;
1027 else
1028 len = skb->len;
1029
1030 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1031 if (!err)
1032 err = (flags & MSG_TRUNC) ? skb->len : len;
1033
1034 skb_free_datagram(sk, skb);
1035 return err;
1036}
1037
1038static void pep_sock_unhash(struct sock *sk)
1039{
1040 struct pep_sock *pn = pep_sk(sk);
1041 struct sock *skparent = NULL;
1042
1043 lock_sock(sk);
1044 if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1045 skparent = pn->listener;
1046 sk_del_node_init(sk);
1047 release_sock(sk);
1048
1049 sk = skparent;
1050 pn = pep_sk(skparent);
1051 lock_sock(sk);
1052 }
1053 /* Unhash a listening sock only when it is closed
1054 * and all of its active connected pipes are closed. */
1055 if (hlist_empty(&pn->hlist))
1056 pn_sock_unhash(&pn->pn_sk.sk);
1057 release_sock(sk);
1058
1059 if (skparent)
1060 sock_put(skparent);
1061}
1062
1063static struct proto pep_proto = {
1064 .close = pep_sock_close,
1065 .accept = pep_sock_accept,
1066 .ioctl = pep_ioctl,
1067 .init = pep_init,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001068 .setsockopt = pep_setsockopt,
1069 .getsockopt = pep_getsockopt,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001070 .sendmsg = pep_sendmsg,
1071 .recvmsg = pep_recvmsg,
1072 .backlog_rcv = pep_do_rcv,
1073 .hash = pn_sock_hash,
1074 .unhash = pep_sock_unhash,
1075 .get_port = pn_sock_get_port,
1076 .obj_size = sizeof(struct pep_sock),
1077 .owner = THIS_MODULE,
1078 .name = "PNPIPE",
1079};
1080
1081static struct phonet_protocol pep_pn_proto = {
1082 .ops = &phonet_stream_ops,
1083 .prot = &pep_proto,
1084 .sock_type = SOCK_SEQPACKET,
1085};
1086
1087static int __init pep_register(void)
1088{
1089 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1090}
1091
1092static void __exit pep_unregister(void)
1093{
1094 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1095}
1096
1097module_init(pep_register);
1098module_exit(pep_unregister);
1099MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1100MODULE_DESCRIPTION("Phonet pipe protocol");
1101MODULE_LICENSE("GPL");
1102MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);