blob: 40952c7d43083c568c8231531286e8bc3e0b5733 [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
Rémi Denis-Courmont96414582008-10-05 11:15:13 -070053#define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */
54
55/* Get the next TLV sub-block. */
56static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen,
57 void *buf)
58{
59 void *data = NULL;
60 struct {
61 u8 sb_type;
62 u8 sb_len;
63 } *ph, h;
64 int buflen = *plen;
65
66 ph = skb_header_pointer(skb, 0, 2, &h);
67 if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len))
68 return NULL;
69 ph->sb_len -= 2;
70 *ptype = ph->sb_type;
71 *plen = ph->sb_len;
72
73 if (buflen > ph->sb_len)
74 buflen = ph->sb_len;
75 data = skb_header_pointer(skb, 2, buflen, buf);
76 __skb_pull(skb, 2 + ph->sb_len);
77 return data;
78}
79
80static int pep_reply(struct sock *sk, struct sk_buff *oskb,
81 u8 code, const void *data, int len, gfp_t priority)
82{
83 const struct pnpipehdr *oph = pnp_hdr(oskb);
84 struct pnpipehdr *ph;
85 struct sk_buff *skb;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +000086 struct sockaddr_pn peer;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -070087
88 skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
89 if (!skb)
90 return -ENOMEM;
91 skb_set_owner_w(skb, sk);
92
93 skb_reserve(skb, MAX_PNPIPE_HEADER);
94 __skb_put(skb, len);
95 skb_copy_to_linear_data(skb, data, len);
96 __skb_push(skb, sizeof(*ph));
97 skb_reset_transport_header(skb);
98 ph = pnp_hdr(skb);
99 ph->utid = oph->utid;
100 ph->message_id = oph->message_id + 1; /* REQ -> RESP */
101 ph->pipe_handle = oph->pipe_handle;
102 ph->error_code = code;
103
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000104 pn_skb_get_src_sockaddr(oskb, &peer);
105 return pn_skb_send(sk, skb, &peer);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700106}
107
108#define PAD 0x00
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000109
110#ifdef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000111static int pipe_handler_send_req(struct sock *sk, u8 msg_id, gfp_t priority)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000112{
113 int len;
114 struct pnpipehdr *ph;
115 struct sk_buff *skb;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000116 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000117
118 static const u8 data[4] = {
119 PAD, PAD, PAD, PAD,
120 };
121
122 switch (msg_id) {
123 case PNS_PEP_CONNECT_REQ:
124 len = sizeof(data);
125 break;
126
127 case PNS_PEP_DISCONNECT_REQ:
128 case PNS_PEP_ENABLE_REQ:
129 case PNS_PEP_DISABLE_REQ:
130 len = 0;
131 break;
132
133 default:
134 return -EINVAL;
135 }
136
137 skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
138 if (!skb)
139 return -ENOMEM;
140 skb_set_owner_w(skb, sk);
141
142 skb_reserve(skb, MAX_PNPIPE_HEADER);
143 if (len) {
144 __skb_put(skb, len);
145 skb_copy_to_linear_data(skb, data, len);
146 }
147 __skb_push(skb, sizeof(*ph));
148 skb_reset_transport_header(skb);
149 ph = pnp_hdr(skb);
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000150 ph->utid = msg_id; /* whatever */
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000151 ph->message_id = msg_id;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000152 ph->pipe_handle = pn->pipe_handle;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000153 ph->error_code = PN_PIPE_NO_ERROR;
154
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000155 return pn_skb_send(sk, skb, NULL);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000156}
157
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000158static int pipe_handler_send_created_ind(struct sock *sk, u8 msg_id)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000159{
160 int err_code;
161 struct pnpipehdr *ph;
162 struct sk_buff *skb;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000163
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000164 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000165 static u8 data[4] = {
166 0x03, 0x04,
167 };
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000168 data[2] = pn->tx_fc;
169 data[3] = pn->rx_fc;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000170
171 /*
172 * actually, below is number of sub-blocks and not error code.
173 * Pipe_created_ind message format does not have any
174 * error code field. However, the Phonet stack will always send
175 * an error code as part of pnpipehdr. So, use that err_code to
176 * specify the number of sub-blocks.
177 */
178 err_code = 0x01;
179
180 skb = alloc_skb(MAX_PNPIPE_HEADER + sizeof(data), GFP_ATOMIC);
181 if (!skb)
182 return -ENOMEM;
183 skb_set_owner_w(skb, sk);
184
185 skb_reserve(skb, MAX_PNPIPE_HEADER);
186 __skb_put(skb, sizeof(data));
187 skb_copy_to_linear_data(skb, data, sizeof(data));
188 __skb_push(skb, sizeof(*ph));
189 skb_reset_transport_header(skb);
190 ph = pnp_hdr(skb);
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000191 ph->utid = 0;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000192 ph->message_id = msg_id;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000193 ph->pipe_handle = pn->pipe_handle;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000194 ph->error_code = err_code;
195
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000196 return pn_skb_send(sk, skb, NULL);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000197}
198
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000199static int pipe_handler_send_ind(struct sock *sk, u8 msg_id)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000200{
201 int err_code;
202 struct pnpipehdr *ph;
203 struct sk_buff *skb;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000204 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000205
206 /*
207 * actually, below is a filler.
208 * Pipe_enabled/disabled_ind message format does not have any
209 * error code field. However, the Phonet stack will always send
210 * an error code as part of pnpipehdr. So, use that err_code to
211 * specify the filler value.
212 */
213 err_code = 0x0;
214
215 skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
216 if (!skb)
217 return -ENOMEM;
218 skb_set_owner_w(skb, sk);
219
220 skb_reserve(skb, MAX_PNPIPE_HEADER);
221 __skb_push(skb, sizeof(*ph));
222 skb_reset_transport_header(skb);
223 ph = pnp_hdr(skb);
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000224 ph->utid = 0;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000225 ph->message_id = msg_id;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000226 ph->pipe_handle = pn->pipe_handle;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000227 ph->error_code = err_code;
228
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000229 return pn_skb_send(sk, skb, NULL);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000230}
231
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000232static int pipe_handler_enable_pipe(struct sock *sk, int enable)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000233{
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000234 u8 id = enable ? PNS_PEP_ENABLE_REQ : PNS_PEP_DISABLE_REQ;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000235
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000236 return pipe_handler_send_req(sk, id, GFP_KERNEL);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000237}
238#endif
239
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700240static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
241{
242 static const u8 data[20] = {
243 PAD, PAD, PAD, 2 /* sub-blocks */,
244 PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
245 PN_MULTI_CREDIT_FLOW_CONTROL,
246 PN_ONE_CREDIT_FLOW_CONTROL,
247 PN_LEGACY_FLOW_CONTROL,
248 PAD,
249 PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
250 PN_MULTI_CREDIT_FLOW_CONTROL,
251 PN_ONE_CREDIT_FLOW_CONTROL,
252 PN_LEGACY_FLOW_CONTROL,
253 PAD,
254 };
255
256 might_sleep();
257 return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
258 GFP_KERNEL);
259}
260
261static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code)
262{
263 static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
264 WARN_ON(code == PN_PIPE_NO_ERROR);
265 return pep_reply(sk, skb, code, data, sizeof(data), GFP_ATOMIC);
266}
267
268/* Control requests are not sent by the pipe service and have a specific
269 * message format. */
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700270static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
271 gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700272{
273 const struct pnpipehdr *oph = pnp_hdr(oskb);
274 struct sk_buff *skb;
275 struct pnpipehdr *ph;
276 struct sockaddr_pn dst;
277
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700278 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700279 if (!skb)
280 return -ENOMEM;
281 skb_set_owner_w(skb, sk);
282
283 skb_reserve(skb, MAX_PHONET_HEADER);
284 ph = (struct pnpipehdr *)skb_put(skb, sizeof(*ph) + 4);
285
286 ph->utid = oph->utid;
287 ph->message_id = PNS_PEP_CTRL_RESP;
288 ph->pipe_handle = oph->pipe_handle;
289 ph->data[0] = oph->data[1]; /* CTRL id */
290 ph->data[1] = oph->data[0]; /* PEP type */
291 ph->data[2] = code; /* error code, at an usual offset */
292 ph->data[3] = PAD;
293 ph->data[4] = PAD;
294
295 pn_skb_get_src_sockaddr(oskb, &dst);
296 return pn_skb_send(sk, skb, &dst);
297}
298
299static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
300{
301 struct pep_sock *pn = pep_sk(sk);
302 struct pnpipehdr *ph;
303 struct sk_buff *skb;
304
305 skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority);
306 if (!skb)
307 return -ENOMEM;
308 skb_set_owner_w(skb, sk);
309
310 skb_reserve(skb, MAX_PNPIPE_HEADER + 4);
311 __skb_push(skb, sizeof(*ph) + 4);
312 skb_reset_transport_header(skb);
313 ph = pnp_hdr(skb);
314 ph->utid = 0;
315 ph->message_id = PNS_PEP_STATUS_IND;
316 ph->pipe_handle = pn->pipe_handle;
317 ph->pep_type = PN_PEP_TYPE_COMMON;
318 ph->data[1] = type;
319 ph->data[2] = PAD;
320 ph->data[3] = PAD;
321 ph->data[4] = status;
322
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000323 return pn_skb_send(sk, skb, NULL);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700324}
325
326/* Send our RX flow control information to the sender.
327 * Socket must be locked. */
328static void pipe_grant_credits(struct sock *sk)
329{
330 struct pep_sock *pn = pep_sk(sk);
331
332 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
333
334 switch (pn->rx_fc) {
335 case PN_LEGACY_FLOW_CONTROL: /* TODO */
336 break;
337 case PN_ONE_CREDIT_FLOW_CONTROL:
338 pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
339 PEP_IND_READY, GFP_ATOMIC);
340 pn->rx_credits = 1;
341 break;
342 case PN_MULTI_CREDIT_FLOW_CONTROL:
343 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
344 break;
345 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
346 CREDITS_MAX - pn->rx_credits,
347 GFP_ATOMIC) == 0)
348 pn->rx_credits = CREDITS_MAX;
349 break;
350 }
351}
352
353static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
354{
355 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000356 struct pnpipehdr *hdr;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800357 int wake = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700358
359 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
360 return -EINVAL;
361
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000362 hdr = pnp_hdr(skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700363 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
364 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
365 (unsigned)hdr->data[0]);
366 return -EOPNOTSUPP;
367 }
368
369 switch (hdr->data[1]) {
370 case PN_PEP_IND_FLOW_CONTROL:
371 switch (pn->tx_fc) {
372 case PN_LEGACY_FLOW_CONTROL:
373 switch (hdr->data[4]) {
374 case PEP_IND_BUSY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800375 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700376 break;
377 case PEP_IND_READY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800378 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700379 break;
380 }
381 break;
382 case PN_ONE_CREDIT_FLOW_CONTROL:
383 if (hdr->data[4] == PEP_IND_READY)
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800384 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700385 break;
386 }
387 break;
388
389 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
390 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
391 break;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800392 atomic_add(wake = hdr->data[4], &pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700393 break;
394
395 default:
396 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
397 (unsigned)hdr->data[1]);
398 return -EOPNOTSUPP;
399 }
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800400 if (wake)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700401 sk->sk_write_space(sk);
402 return 0;
403}
404
405static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
406{
407 struct pep_sock *pn = pep_sk(sk);
408 struct pnpipehdr *hdr = pnp_hdr(skb);
409 u8 n_sb = hdr->data[0];
410
411 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
412 __skb_pull(skb, sizeof(*hdr));
413 while (n_sb > 0) {
414 u8 type, buf[2], len = sizeof(buf);
415 u8 *data = pep_get_sb(skb, &type, &len, buf);
416
417 if (data == NULL)
418 return -EINVAL;
419 switch (type) {
420 case PN_PIPE_SB_NEGOTIATED_FC:
421 if (len < 2 || (data[0] | data[1]) > 3)
422 break;
423 pn->tx_fc = data[0] & 3;
424 pn->rx_fc = data[1] & 3;
425 break;
426 }
427 n_sb--;
428 }
429 return 0;
430}
431
432/* Queue an skb to a connected sock.
433 * Socket lock must be held. */
434static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
435{
436 struct pep_sock *pn = pep_sk(sk);
437 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700438 struct sk_buff_head *queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700439 int err = 0;
440
441 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
442
443 switch (hdr->message_id) {
444 case PNS_PEP_CONNECT_REQ:
445 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
446 break;
447
448 case PNS_PEP_DISCONNECT_REQ:
449 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
450 sk->sk_state = TCP_CLOSE_WAIT;
451 if (!sock_flag(sk, SOCK_DEAD))
452 sk->sk_state_change(sk);
453 break;
454
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000455#ifdef CONFIG_PHONET_PIPECTRLR
456 case PNS_PEP_DISCONNECT_RESP:
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000457 sk->sk_state = TCP_CLOSE;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000458 break;
459#endif
460
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700461 case PNS_PEP_ENABLE_REQ:
462 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
463 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
464 break;
465
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000466#ifdef CONFIG_PHONET_PIPECTRLR
467 case PNS_PEP_ENABLE_RESP:
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000468 pipe_handler_send_ind(sk, PNS_PIPE_ENABLED_IND);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000469
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000470 if (!pn_flow_safe(pn->tx_fc)) {
471 atomic_set(&pn->tx_credits, 1);
472 sk->sk_write_space(sk);
473 }
474 if (sk->sk_state == TCP_ESTABLISHED)
475 break; /* Nothing to do */
476 sk->sk_state = TCP_ESTABLISHED;
477 pipe_grant_credits(sk);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000478 break;
479#endif
480
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700481 case PNS_PEP_RESET_REQ:
482 switch (hdr->state_after_reset) {
483 case PN_PIPE_DISABLE:
484 pn->init_enable = 0;
485 break;
486 case PN_PIPE_ENABLE:
487 pn->init_enable = 1;
488 break;
489 default: /* not allowed to send an error here!? */
490 err = -EINVAL;
491 goto out;
492 }
493 /* fall through */
494 case PNS_PEP_DISABLE_REQ:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800495 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700496 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
497 break;
498
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000499#ifdef CONFIG_PHONET_PIPECTRLR
500 case PNS_PEP_DISABLE_RESP:
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000501 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000502 pipe_handler_send_ind(sk, PNS_PIPE_DISABLED_IND);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000503 sk->sk_state = TCP_SYN_RECV;
504 pn->rx_credits = 0;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000505 break;
506#endif
507
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700508 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000509 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
510 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700511 break;
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000512 }
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700513 __skb_pull(skb, 4);
514 queue = &pn->ctrlreq_queue;
515 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700516
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +0000517 case PNS_PIPE_ALIGNED_DATA:
518 __skb_pull(skb, 1);
519 /* fall through */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700520 case PNS_PIPE_DATA:
521 __skb_pull(skb, 3); /* Pipe data header */
522 if (!pn_flow_safe(pn->rx_fc)) {
523 err = sock_queue_rcv_skb(sk, skb);
524 if (!err)
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000525 return NET_RX_SUCCESS;
526 err = -ENOBUFS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700527 break;
528 }
529
530 if (pn->rx_credits == 0) {
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000531 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700532 err = -ENOBUFS;
533 break;
534 }
535 pn->rx_credits--;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700536 queue = &sk->sk_receive_queue;
537 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700538
539 case PNS_PEP_STATUS_IND:
540 pipe_rcv_status(sk, skb);
541 break;
542
543 case PNS_PIPE_REDIRECTED_IND:
544 err = pipe_rcv_created(sk, skb);
545 break;
546
547 case PNS_PIPE_CREATED_IND:
548 err = pipe_rcv_created(sk, skb);
549 if (err)
550 break;
551 /* fall through */
552 case PNS_PIPE_RESET_IND:
553 if (!pn->init_enable)
554 break;
555 /* fall through */
556 case PNS_PIPE_ENABLED_IND:
557 if (!pn_flow_safe(pn->tx_fc)) {
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800558 atomic_set(&pn->tx_credits, 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700559 sk->sk_write_space(sk);
560 }
561 if (sk->sk_state == TCP_ESTABLISHED)
562 break; /* Nothing to do */
563 sk->sk_state = TCP_ESTABLISHED;
564 pipe_grant_credits(sk);
565 break;
566
567 case PNS_PIPE_DISABLED_IND:
568 sk->sk_state = TCP_SYN_RECV;
569 pn->rx_credits = 0;
570 break;
571
572 default:
573 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
574 hdr->message_id);
575 err = -EINVAL;
576 }
577out:
578 kfree_skb(skb);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000579 return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700580
581queue:
582 skb->dev = NULL;
583 skb_set_owner_r(skb, sk);
584 err = skb->len;
585 skb_queue_tail(queue, skb);
586 if (!sock_flag(sk, SOCK_DEAD))
587 sk->sk_data_ready(sk, err);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000588 return NET_RX_SUCCESS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700589}
590
591/* Destroy connected sock. */
592static void pipe_destruct(struct sock *sk)
593{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700594 struct pep_sock *pn = pep_sk(sk);
595
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700596 skb_queue_purge(&sk->sk_receive_queue);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700597 skb_queue_purge(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700598}
599
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000600#ifdef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000601static u8 pipe_negotiate_fc(const u8 *fcs, unsigned n)
602{
603 unsigned i;
604 u8 final_fc = PN_NO_FLOW_CONTROL;
605
606 for (i = 0; i < n; i++) {
607 u8 fc = fcs[i];
608
609 if (fc > final_fc && fc < PN_MAX_FLOW_CONTROL)
610 final_fc = fc;
611 }
612 return final_fc;
613}
614
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000615static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
616{
617 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000618 struct pnpipehdr *hdr;
619 u8 n_sb;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000620
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000621 if (!pskb_pull(skb, sizeof(*hdr) + 4))
622 return -EINVAL;
623
624 hdr = pnp_hdr(skb);
625
626 /* Parse sub-blocks */
627 n_sb = hdr->data[4];
628 while (n_sb > 0) {
629 u8 type, buf[6], len = sizeof(buf);
630 const u8 *data = pep_get_sb(skb, &type, &len, buf);
631
632 if (data == NULL)
633 return -EINVAL;
634
635 switch (type) {
636 case PN_PIPE_SB_REQUIRED_FC_TX:
637 if (len < 2 || len < data[0])
638 break;
639 pn->tx_fc = pipe_negotiate_fc(data + 2, len - 2);
640 break;
641
642 case PN_PIPE_SB_PREFERRED_FC_RX:
643 if (len < 2 || len < data[0])
644 break;
645 pn->rx_fc = pipe_negotiate_fc(data + 2, len - 2);
646 break;
647
648 }
649 n_sb--;
650 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000651
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000652 sk->sk_state = TCP_SYN_RECV;
653 sk->sk_backlog_rcv = pipe_do_rcv;
654 sk->sk_destruct = pipe_destruct;
655 pn->rx_credits = 0;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000656 sk->sk_state_change(sk);
657
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000658 return pipe_handler_send_created_ind(sk, PNS_PIPE_CREATED_IND);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000659}
660#endif
661
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700662static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
663{
664 struct sock *newsk;
665 struct pep_sock *newpn, *pn = pep_sk(sk);
666 struct pnpipehdr *hdr;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000667 struct sockaddr_pn dst, src;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700668 u16 peer_type;
669 u8 pipe_handle, enabled, n_sb;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000670 u8 aligned = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700671
672 if (!pskb_pull(skb, sizeof(*hdr) + 4))
673 return -EINVAL;
674
675 hdr = pnp_hdr(skb);
676 pipe_handle = hdr->pipe_handle;
677 switch (hdr->state_after_connect) {
678 case PN_PIPE_DISABLE:
679 enabled = 0;
680 break;
681 case PN_PIPE_ENABLE:
682 enabled = 1;
683 break;
684 default:
685 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
686 return -EINVAL;
687 }
688 peer_type = hdr->other_pep_type << 8;
689
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700690 /* Parse sub-blocks (options) */
691 n_sb = hdr->data[4];
692 while (n_sb > 0) {
693 u8 type, buf[1], len = sizeof(buf);
694 const u8 *data = pep_get_sb(skb, &type, &len, buf);
695
696 if (data == NULL)
697 return -EINVAL;
698 switch (type) {
699 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
700 if (len < 1)
701 return -EINVAL;
702 peer_type = (peer_type & 0xff00) | data[0];
703 break;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000704 case PN_PIPE_SB_ALIGNED_DATA:
705 aligned = data[0] != 0;
706 break;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700707 }
708 n_sb--;
709 }
710
711 skb = skb_clone(skb, GFP_ATOMIC);
712 if (!skb)
713 return -ENOMEM;
714
715 /* Create a new to-be-accepted sock */
716 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
717 if (!newsk) {
718 kfree_skb(skb);
719 return -ENOMEM;
720 }
721 sock_init_data(NULL, newsk);
722 newsk->sk_state = TCP_SYN_RECV;
723 newsk->sk_backlog_rcv = pipe_do_rcv;
724 newsk->sk_protocol = sk->sk_protocol;
725 newsk->sk_destruct = pipe_destruct;
726
727 newpn = pep_sk(newsk);
728 pn_skb_get_dst_sockaddr(skb, &dst);
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000729 pn_skb_get_src_sockaddr(skb, &src);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700730 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000731 newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
732 newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700733 skb_queue_head_init(&newpn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700734 newpn->pipe_handle = pipe_handle;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800735 atomic_set(&newpn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700736 newpn->peer_type = peer_type;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800737 newpn->rx_credits = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700738 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
739 newpn->init_enable = enabled;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000740 newpn->aligned = aligned;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700741
742 BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
743 skb_queue_head(&newsk->sk_receive_queue, skb);
744 if (!sock_flag(sk, SOCK_DEAD))
745 sk->sk_data_ready(sk, 0);
746
747 sk_acceptq_added(sk);
748 sk_add_node(newsk, &pn->ackq);
749 return 0;
750}
751
752/* Listening sock must be locked */
753static struct sock *pep_find_pipe(const struct hlist_head *hlist,
754 const struct sockaddr_pn *dst,
755 u8 pipe_handle)
756{
757 struct hlist_node *node;
758 struct sock *sknode;
759 u16 dobj = pn_sockaddr_get_object(dst);
760
761 sk_for_each(sknode, node, hlist) {
762 struct pep_sock *pnnode = pep_sk(sknode);
763
764 /* Ports match, but addresses might not: */
765 if (pnnode->pn_sk.sobject != dobj)
766 continue;
767 if (pnnode->pipe_handle != pipe_handle)
768 continue;
769 if (sknode->sk_state == TCP_CLOSE_WAIT)
770 continue;
771
772 sock_hold(sknode);
773 return sknode;
774 }
775 return NULL;
776}
777
778/*
779 * Deliver an skb to a listening sock.
780 * Socket lock must be held.
781 * We then queue the skb to the right connected sock (if any).
782 */
783static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
784{
785 struct pep_sock *pn = pep_sk(sk);
786 struct sock *sknode;
Rémi Denis-Courmont2ddc1ac2009-02-10 17:14:50 -0800787 struct pnpipehdr *hdr;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700788 struct sockaddr_pn dst;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700789 u8 pipe_handle;
790
791 if (!pskb_may_pull(skb, sizeof(*hdr)))
792 goto drop;
793
794 hdr = pnp_hdr(skb);
795 pipe_handle = hdr->pipe_handle;
796 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
797 goto drop;
798
799 pn_skb_get_dst_sockaddr(skb, &dst);
800
801 /* Look for an existing pipe handle */
802 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
803 if (sknode)
804 return sk_receive_skb(sknode, skb, 1);
805
806 /* Look for a pipe handle pending accept */
807 sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
808 if (sknode) {
809 sock_put(sknode);
810 if (net_ratelimit())
811 printk(KERN_WARNING"Phonet unconnected PEP ignored");
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700812 goto drop;
813 }
814
815 switch (hdr->message_id) {
816 case PNS_PEP_CONNECT_REQ:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000817 if (sk->sk_state == TCP_LISTEN && !sk_acceptq_is_full(sk))
818 pep_connreq_rcv(sk, skb);
819 else
820 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700821 break;
822
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000823#ifdef CONFIG_PHONET_PIPECTRLR
824 case PNS_PEP_CONNECT_RESP:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000825 pep_connresp_rcv(sk, skb);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000826 break;
827#endif
828
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700829 case PNS_PEP_DISCONNECT_REQ:
830 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
831 break;
832
833 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700834 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700835 break;
836
837 case PNS_PEP_RESET_REQ:
838 case PNS_PEP_ENABLE_REQ:
839 case PNS_PEP_DISABLE_REQ:
840 /* invalid handle is not even allowed here! */
841 default:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000842 break;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700843 }
844drop:
845 kfree_skb(skb);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000846 return NET_RX_SUCCESS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700847}
848
David S. Miller00497132011-02-25 11:23:22 -0800849#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000850static int pipe_do_remove(struct sock *sk)
851{
852 struct pep_sock *pn = pep_sk(sk);
853 struct pnpipehdr *ph;
854 struct sk_buff *skb;
855
856 skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_KERNEL);
857 if (!skb)
858 return -ENOMEM;
859
860 skb_reserve(skb, MAX_PNPIPE_HEADER);
861 __skb_push(skb, sizeof(*ph));
862 skb_reset_transport_header(skb);
863 ph = pnp_hdr(skb);
864 ph->utid = 0;
865 ph->message_id = PNS_PIPE_REMOVE_REQ;
866 ph->pipe_handle = pn->pipe_handle;
867 ph->data[0] = PAD;
868
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000869 return pn_skb_send(sk, skb, NULL);
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000870}
David S. Miller00497132011-02-25 11:23:22 -0800871#endif
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000872
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700873/* associated socket ceases to exist */
874static void pep_sock_close(struct sock *sk, long timeout)
875{
876 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700877 int ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700878
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -0700879 sock_hold(sk); /* keep a reference after sk_common_release() */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700880 sk_common_release(sk);
881
882 lock_sock(sk);
883 if (sk->sk_state == TCP_LISTEN) {
884 /* Destroy the listen queue */
885 struct sock *sknode;
886 struct hlist_node *p, *n;
887
888 sk_for_each_safe(sknode, p, n, &pn->ackq)
889 sk_del_node_init(sknode);
890 sk->sk_state = TCP_CLOSE;
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000891 } else if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
892#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000893 /* Forcefully remove dangling Phonet pipe */
894 pipe_do_remove(sk);
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000895#else
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000896 /* send pep disconnect request */
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000897 pipe_handler_send_req(sk, PNS_PEP_DISCONNECT_REQ, GFP_KERNEL);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000898 sk->sk_state = TCP_CLOSE;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000899#endif
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000900 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000901
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700902 ifindex = pn->ifindex;
903 pn->ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700904 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700905
906 if (ifindex)
907 gprs_detach(sk);
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -0700908 sock_put(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700909}
910
911static int pep_wait_connreq(struct sock *sk, int noblock)
912{
913 struct task_struct *tsk = current;
914 struct pep_sock *pn = pep_sk(sk);
915 long timeo = sock_rcvtimeo(sk, noblock);
916
917 for (;;) {
918 DEFINE_WAIT(wait);
919
920 if (sk->sk_state != TCP_LISTEN)
921 return -EINVAL;
922 if (!hlist_empty(&pn->ackq))
923 break;
924 if (!timeo)
925 return -EWOULDBLOCK;
926 if (signal_pending(tsk))
927 return sock_intr_errno(timeo);
928
Eric Dumazet43815482010-04-29 11:01:49 +0000929 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700930 TASK_INTERRUPTIBLE);
931 release_sock(sk);
932 timeo = schedule_timeout(timeo);
933 lock_sock(sk);
Eric Dumazet43815482010-04-29 11:01:49 +0000934 finish_wait(sk_sleep(sk), &wait);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700935 }
936
937 return 0;
938}
939
940static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
941{
942 struct pep_sock *pn = pep_sk(sk);
943 struct sock *newsk = NULL;
944 struct sk_buff *oskb;
945 int err;
946
947 lock_sock(sk);
948 err = pep_wait_connreq(sk, flags & O_NONBLOCK);
949 if (err)
950 goto out;
951
952 newsk = __sk_head(&pn->ackq);
953
954 oskb = skb_dequeue(&newsk->sk_receive_queue);
955 err = pep_accept_conn(newsk, oskb);
956 if (err) {
957 skb_queue_head(&newsk->sk_receive_queue, oskb);
958 newsk = NULL;
959 goto out;
960 }
Rémi Denis-Courmont635f0812010-07-07 20:56:53 +0000961 kfree_skb(oskb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700962
963 sock_hold(sk);
964 pep_sk(newsk)->listener = sk;
965
966 sock_hold(newsk);
967 sk_del_node_init(newsk);
968 sk_acceptq_removed(sk);
969 sk_add_node(newsk, &pn->hlist);
970 __sock_put(newsk);
971
972out:
973 release_sock(sk);
974 *errp = err;
975 return newsk;
976}
977
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000978#ifdef CONFIG_PHONET_PIPECTRLR
979static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
980{
981 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000982 const struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000983
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000984 pn->pn_sk.dobject = pn_sockaddr_get_object(spn);
985 pn->pn_sk.resource = pn_sockaddr_get_resource(spn);
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000986 return pipe_handler_send_req(sk, PNS_PEP_CONNECT_REQ, GFP_KERNEL);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000987}
988#endif
989
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700990static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
991{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700992 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700993 int answ;
994
995 switch (cmd) {
996 case SIOCINQ:
997 if (sk->sk_state == TCP_LISTEN)
998 return -EINVAL;
999
1000 lock_sock(sk);
Joe Perchesf64f9e72009-11-29 16:55:45 -08001001 if (sock_flag(sk, SOCK_URGINLINE) &&
1002 !skb_queue_empty(&pn->ctrlreq_queue))
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001003 answ = skb_peek(&pn->ctrlreq_queue)->len;
1004 else if (!skb_queue_empty(&sk->sk_receive_queue))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001005 answ = skb_peek(&sk->sk_receive_queue)->len;
1006 else
1007 answ = 0;
1008 release_sock(sk);
1009 return put_user(answ, (int __user *)arg);
1010 }
1011
1012 return -ENOIOCTLCMD;
1013}
1014
1015static int pep_init(struct sock *sk)
1016{
1017 struct pep_sock *pn = pep_sk(sk);
1018
1019 INIT_HLIST_HEAD(&pn->ackq);
1020 INIT_HLIST_HEAD(&pn->hlist);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001021 skb_queue_head_init(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001022 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
1023 return 0;
1024}
1025
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001026static int pep_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001027 char __user *optval, unsigned int optlen)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001028{
1029 struct pep_sock *pn = pep_sk(sk);
1030 int val = 0, err = 0;
1031
1032 if (level != SOL_PNPIPE)
1033 return -ENOPROTOOPT;
1034 if (optlen >= sizeof(int)) {
1035 if (get_user(val, (int __user *) optval))
1036 return -EFAULT;
1037 }
1038
1039 lock_sock(sk);
1040 switch (optname) {
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001041#ifdef CONFIG_PHONET_PIPECTRLR
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001042 case PNPIPE_PIPE_HANDLE:
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001043 if (val) {
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001044 pn->pipe_handle = val;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001045 break;
1046 }
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001047#endif
1048
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001049 case PNPIPE_ENCAP:
1050 if (val && val != PNPIPE_ENCAP_IP) {
1051 err = -EINVAL;
1052 break;
1053 }
1054 if (!pn->ifindex == !val)
1055 break; /* Nothing to do! */
1056 if (!capable(CAP_NET_ADMIN)) {
1057 err = -EPERM;
1058 break;
1059 }
1060 if (val) {
1061 release_sock(sk);
1062 err = gprs_attach(sk);
1063 if (err > 0) {
1064 pn->ifindex = err;
1065 err = 0;
1066 }
1067 } else {
1068 pn->ifindex = 0;
1069 release_sock(sk);
1070 gprs_detach(sk);
1071 err = 0;
1072 }
1073 goto out_norel;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001074
1075#ifdef CONFIG_PHONET_PIPECTRLR
1076 case PNPIPE_ENABLE:
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +00001077 if ((1 << sk->sk_state) & ~(TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001078 err = -ENOTCONN;
1079 break;
1080 }
1081 err = pipe_handler_enable_pipe(sk, val);
1082 break;
1083#endif
1084
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001085 default:
1086 err = -ENOPROTOOPT;
1087 }
1088 release_sock(sk);
1089
1090out_norel:
1091 return err;
1092}
1093
1094static int pep_getsockopt(struct sock *sk, int level, int optname,
1095 char __user *optval, int __user *optlen)
1096{
1097 struct pep_sock *pn = pep_sk(sk);
1098 int len, val;
1099
1100 if (level != SOL_PNPIPE)
1101 return -ENOPROTOOPT;
1102 if (get_user(len, optlen))
1103 return -EFAULT;
1104
1105 switch (optname) {
1106 case PNPIPE_ENCAP:
1107 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
1108 break;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001109
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001110 case PNPIPE_IFINDEX:
1111 val = pn->ifindex;
1112 break;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001113
1114#ifdef CONFIG_PHONET_PIPECTRLR
1115 case PNPIPE_ENABLE:
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +00001116 val = sk->sk_state == TCP_ESTABLISHED;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001117 break;
1118#endif
1119
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001120 default:
1121 return -ENOPROTOOPT;
1122 }
1123
1124 len = min_t(unsigned int, sizeof(int), len);
1125 if (put_user(len, optlen))
1126 return -EFAULT;
1127 if (put_user(val, (int __user *) optval))
1128 return -EFAULT;
1129 return 0;
1130}
1131
1132static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1133{
1134 struct pep_sock *pn = pep_sk(sk);
1135 struct pnpipehdr *ph;
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001136 int err;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001137
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001138 if (pn_flow_safe(pn->tx_fc) &&
1139 !atomic_add_unless(&pn->tx_credits, -1, 0)) {
1140 kfree_skb(skb);
1141 return -ENOBUFS;
1142 }
1143
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001144 skb_push(skb, 3 + pn->aligned);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001145 skb_reset_transport_header(skb);
1146 ph = pnp_hdr(skb);
1147 ph->utid = 0;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001148 if (pn->aligned) {
1149 ph->message_id = PNS_PIPE_ALIGNED_DATA;
1150 ph->data[0] = 0; /* padding */
1151 } else
1152 ph->message_id = PNS_PIPE_DATA;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001153 ph->pipe_handle = pn->pipe_handle;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +00001154 err = pn_skb_send(sk, skb, NULL);
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001155
1156 if (err && pn_flow_safe(pn->tx_fc))
1157 atomic_inc(&pn->tx_credits);
1158 return err;
1159
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001160}
1161
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001162static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
1163 struct msghdr *msg, size_t len)
1164{
1165 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001166 struct sk_buff *skb;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001167 long timeo;
1168 int flags = msg->msg_flags;
1169 int err, done;
1170
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001171 if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
1172 MSG_CMSG_COMPAT)) ||
1173 !(msg->msg_flags & MSG_EOR))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001174 return -EOPNOTSUPP;
1175
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001176 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
1177 flags & MSG_DONTWAIT, &err);
1178 if (!skb)
Rémi Denis-Courmont02ac3262010-08-30 12:57:04 +00001179 return err;
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001180
1181 skb_reserve(skb, MAX_PHONET_HEADER + 3);
1182 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1183 if (err < 0)
1184 goto outfree;
1185
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001186 lock_sock(sk);
1187 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1188 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
1189 err = -ENOTCONN;
1190 goto out;
1191 }
1192 if (sk->sk_state != TCP_ESTABLISHED) {
1193 /* Wait until the pipe gets to enabled state */
1194disabled:
1195 err = sk_stream_wait_connect(sk, &timeo);
1196 if (err)
1197 goto out;
1198
1199 if (sk->sk_state == TCP_CLOSE_WAIT) {
1200 err = -ECONNRESET;
1201 goto out;
1202 }
1203 }
1204 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
1205
1206 /* Wait until flow control allows TX */
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001207 done = atomic_read(&pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001208 while (!done) {
1209 DEFINE_WAIT(wait);
1210
1211 if (!timeo) {
1212 err = -EAGAIN;
1213 goto out;
1214 }
1215 if (signal_pending(current)) {
1216 err = sock_intr_errno(timeo);
1217 goto out;
1218 }
1219
Eric Dumazet43815482010-04-29 11:01:49 +00001220 prepare_to_wait(sk_sleep(sk), &wait,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001221 TASK_INTERRUPTIBLE);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001222 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
Eric Dumazet43815482010-04-29 11:01:49 +00001223 finish_wait(sk_sleep(sk), &wait);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001224
1225 if (sk->sk_state != TCP_ESTABLISHED)
1226 goto disabled;
1227 }
1228
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001229 err = pipe_skb_send(sk, skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001230 if (err >= 0)
1231 err = len; /* success! */
1232 skb = NULL;
1233out:
1234 release_sock(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001235outfree:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001236 kfree_skb(skb);
1237 return err;
1238}
1239
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001240int pep_writeable(struct sock *sk)
1241{
1242 struct pep_sock *pn = pep_sk(sk);
1243
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001244 return atomic_read(&pn->tx_credits);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001245}
1246
1247int pep_write(struct sock *sk, struct sk_buff *skb)
1248{
1249 struct sk_buff *rskb, *fs;
1250 int flen = 0;
1251
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001252 if (pep_sk(sk)->aligned)
1253 return pipe_skb_send(sk, skb);
1254
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001255 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
1256 if (!rskb) {
1257 kfree_skb(skb);
1258 return -ENOMEM;
1259 }
1260 skb_shinfo(rskb)->frag_list = skb;
1261 rskb->len += skb->len;
1262 rskb->data_len += rskb->len;
1263 rskb->truesize += rskb->len;
1264
1265 /* Avoid nested fragments */
David S. Miller5c313e92009-06-09 00:21:58 -07001266 skb_walk_frags(skb, fs)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001267 flen += fs->len;
1268 skb->next = skb_shinfo(skb)->frag_list;
David S. Miller5c313e92009-06-09 00:21:58 -07001269 skb_frag_list_init(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001270 skb->len -= flen;
1271 skb->data_len -= flen;
1272 skb->truesize -= flen;
1273
1274 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
1275 return pipe_skb_send(sk, rskb);
1276}
1277
1278struct sk_buff *pep_read(struct sock *sk)
1279{
1280 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1281
1282 if (sk->sk_state == TCP_ESTABLISHED)
1283 pipe_grant_credits(sk);
1284 return skb;
1285}
1286
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001287static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
1288 struct msghdr *msg, size_t len, int noblock,
1289 int flags, int *addr_len)
1290{
1291 struct sk_buff *skb;
1292 int err;
1293
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001294 if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
1295 MSG_NOSIGNAL|MSG_CMSG_COMPAT))
1296 return -EOPNOTSUPP;
1297
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001298 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
1299 return -ENOTCONN;
1300
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001301 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
1302 /* Dequeue and acknowledge control request */
1303 struct pep_sock *pn = pep_sk(sk);
1304
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001305 if (flags & MSG_PEEK)
1306 return -EOPNOTSUPP;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001307 skb = skb_dequeue(&pn->ctrlreq_queue);
1308 if (skb) {
1309 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1310 GFP_KERNEL);
1311 msg->msg_flags |= MSG_OOB;
1312 goto copy;
1313 }
1314 if (flags & MSG_OOB)
1315 return -EINVAL;
1316 }
1317
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001318 skb = skb_recv_datagram(sk, flags, noblock, &err);
1319 lock_sock(sk);
1320 if (skb == NULL) {
1321 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1322 err = -ECONNRESET;
1323 release_sock(sk);
1324 return err;
1325 }
1326
1327 if (sk->sk_state == TCP_ESTABLISHED)
1328 pipe_grant_credits(sk);
1329 release_sock(sk);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001330copy:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001331 msg->msg_flags |= MSG_EOR;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001332 if (skb->len > len)
1333 msg->msg_flags |= MSG_TRUNC;
1334 else
1335 len = skb->len;
1336
1337 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1338 if (!err)
1339 err = (flags & MSG_TRUNC) ? skb->len : len;
1340
1341 skb_free_datagram(sk, skb);
1342 return err;
1343}
1344
1345static void pep_sock_unhash(struct sock *sk)
1346{
1347 struct pep_sock *pn = pep_sk(sk);
1348 struct sock *skparent = NULL;
1349
1350 lock_sock(sk);
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001351
1352#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001353 if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1354 skparent = pn->listener;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001355 release_sock(sk);
1356
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001357 pn = pep_sk(skparent);
Rémi Denis-Courmont7dfde172010-05-26 00:44:44 +00001358 lock_sock(skparent);
1359 sk_del_node_init(sk);
1360 sk = skparent;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001361 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001362#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001363 /* Unhash a listening sock only when it is closed
1364 * and all of its active connected pipes are closed. */
1365 if (hlist_empty(&pn->hlist))
1366 pn_sock_unhash(&pn->pn_sk.sk);
1367 release_sock(sk);
1368
1369 if (skparent)
1370 sock_put(skparent);
1371}
1372
1373static struct proto pep_proto = {
1374 .close = pep_sock_close,
1375 .accept = pep_sock_accept,
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001376#ifdef CONFIG_PHONET_PIPECTRLR
1377 .connect = pep_sock_connect,
1378#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001379 .ioctl = pep_ioctl,
1380 .init = pep_init,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001381 .setsockopt = pep_setsockopt,
1382 .getsockopt = pep_getsockopt,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001383 .sendmsg = pep_sendmsg,
1384 .recvmsg = pep_recvmsg,
1385 .backlog_rcv = pep_do_rcv,
1386 .hash = pn_sock_hash,
1387 .unhash = pep_sock_unhash,
1388 .get_port = pn_sock_get_port,
1389 .obj_size = sizeof(struct pep_sock),
1390 .owner = THIS_MODULE,
1391 .name = "PNPIPE",
1392};
1393
1394static struct phonet_protocol pep_pn_proto = {
1395 .ops = &phonet_stream_ops,
1396 .prot = &pep_proto,
1397 .sock_type = SOCK_SEQPACKET,
1398};
1399
1400static int __init pep_register(void)
1401{
1402 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1403}
1404
1405static void __exit pep_unregister(void)
1406{
1407 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1408}
1409
1410module_init(pep_register);
1411module_exit(pep_unregister);
1412MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1413MODULE_DESCRIPTION("Phonet pipe protocol");
1414MODULE_LICENSE("GPL");
1415MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);