blob: 610794a416e8d2fe2312b791591de809ae24f274 [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
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +000080static struct sk_buff *pep_alloc_skb(struct sock *sk, const void *payload,
81 int len, gfp_t priority)
82{
83 struct sk_buff *skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority);
84 if (!skb)
85 return NULL;
86 skb_set_owner_w(skb, sk);
87
88 skb_reserve(skb, MAX_PNPIPE_HEADER);
89 __skb_put(skb, len);
90 skb_copy_to_linear_data(skb, payload, len);
91 __skb_push(skb, sizeof(struct pnpipehdr));
92 skb_reset_transport_header(skb);
93 return skb;
94}
95
96static int pep_reply(struct sock *sk, struct sk_buff *oskb, u8 code,
97 const void *data, int len, gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -070098{
99 const struct pnpipehdr *oph = pnp_hdr(oskb);
100 struct pnpipehdr *ph;
101 struct sk_buff *skb;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000102 struct sockaddr_pn peer;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700103
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000104 skb = pep_alloc_skb(sk, data, len, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700105 if (!skb)
106 return -ENOMEM;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700107
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700108 ph = pnp_hdr(skb);
109 ph->utid = oph->utid;
110 ph->message_id = oph->message_id + 1; /* REQ -> RESP */
111 ph->pipe_handle = oph->pipe_handle;
112 ph->error_code = code;
113
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000114 pn_skb_get_src_sockaddr(oskb, &peer);
115 return pn_skb_send(sk, skb, &peer);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700116}
117
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000118static int pep_indicate(struct sock *sk, u8 id, u8 code,
119 const void *data, int len, gfp_t priority)
120{
121 struct pep_sock *pn = pep_sk(sk);
122 struct pnpipehdr *ph;
123 struct sk_buff *skb;
124
125 skb = pep_alloc_skb(sk, data, len, priority);
126 if (!skb)
127 return -ENOMEM;
128
129 ph = pnp_hdr(skb);
130 ph->utid = 0;
131 ph->message_id = id;
132 ph->pipe_handle = pn->pipe_handle;
133 ph->data[0] = code;
134 return pn_skb_send(sk, skb, NULL);
135}
136
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700137#define PAD 0x00
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000138
139#ifdef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000140static int pipe_handler_request(struct sock *sk, u8 id, u8 code,
141 const void *data, int len)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000142{
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000143 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000144 struct pnpipehdr *ph;
145 struct sk_buff *skb;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000146
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000147 skb = pep_alloc_skb(sk, data, len, GFP_KERNEL);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000148 if (!skb)
149 return -ENOMEM;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000150
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000151 ph = pnp_hdr(skb);
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000152 ph->utid = id; /* whatever */
153 ph->message_id = id;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000154 ph->pipe_handle = pn->pipe_handle;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000155 ph->data[0] = code;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000156 return pn_skb_send(sk, skb, NULL);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000157}
158
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000159static int pipe_handler_send_created_ind(struct sock *sk)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000160{
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000161 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000162 u8 data[4] = {
163 PN_PIPE_SB_NEGOTIATED_FC, pep_sb_size(2),
164 pn->tx_fc, pn->rx_fc,
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000165 };
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000166
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000167 return pep_indicate(sk, PNS_PIPE_CREATED_IND, 1 /* sub-blocks */,
168 data, 4, GFP_ATOMIC);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000169}
170
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000171static int pipe_handler_send_ind(struct sock *sk, u8 id)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000172{
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000173 return pep_indicate(sk, id, PAD, NULL, 0, GFP_ATOMIC);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000174}
175
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000176static int pipe_handler_enable_pipe(struct sock *sk, int enable)
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000177{
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000178 u8 id = enable ? PNS_PEP_ENABLE_REQ : PNS_PEP_DISABLE_REQ;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000179
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000180 return pipe_handler_request(sk, id, PAD, NULL, 0);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000181}
182#endif
183
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700184static int pep_accept_conn(struct sock *sk, struct sk_buff *skb)
185{
186 static const u8 data[20] = {
187 PAD, PAD, PAD, 2 /* sub-blocks */,
188 PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD,
189 PN_MULTI_CREDIT_FLOW_CONTROL,
190 PN_ONE_CREDIT_FLOW_CONTROL,
191 PN_LEGACY_FLOW_CONTROL,
192 PAD,
193 PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD,
194 PN_MULTI_CREDIT_FLOW_CONTROL,
195 PN_ONE_CREDIT_FLOW_CONTROL,
196 PN_LEGACY_FLOW_CONTROL,
197 PAD,
198 };
199
200 might_sleep();
201 return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data),
202 GFP_KERNEL);
203}
204
205static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code)
206{
207 static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
208 WARN_ON(code == PN_PIPE_NO_ERROR);
209 return pep_reply(sk, skb, code, data, sizeof(data), GFP_ATOMIC);
210}
211
212/* Control requests are not sent by the pipe service and have a specific
213 * message format. */
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700214static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
215 gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700216{
217 const struct pnpipehdr *oph = pnp_hdr(oskb);
218 struct sk_buff *skb;
219 struct pnpipehdr *ph;
220 struct sockaddr_pn dst;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000221 u8 data[4] = {
222 oph->data[0], /* PEP type */
223 code, /* error code, at an unusual offset */
224 PAD, PAD,
225 };
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700226
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000227 skb = pep_alloc_skb(sk, data, 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700228 if (!skb)
229 return -ENOMEM;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700230
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000231 ph = pnp_hdr(skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700232 ph->utid = oph->utid;
233 ph->message_id = PNS_PEP_CTRL_RESP;
234 ph->pipe_handle = oph->pipe_handle;
235 ph->data[0] = oph->data[1]; /* CTRL id */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700236
237 pn_skb_get_src_sockaddr(oskb, &dst);
238 return pn_skb_send(sk, skb, &dst);
239}
240
241static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
242{
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000243 u8 data[4] = { type, PAD, PAD, status };
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700244
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000245 return pep_indicate(sk, PNS_PEP_STATUS_IND, PN_PEP_TYPE_COMMON,
246 data, 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700247}
248
249/* Send our RX flow control information to the sender.
250 * Socket must be locked. */
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000251static void pipe_grant_credits(struct sock *sk, gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700252{
253 struct pep_sock *pn = pep_sk(sk);
254
255 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
256
257 switch (pn->rx_fc) {
258 case PN_LEGACY_FLOW_CONTROL: /* TODO */
259 break;
260 case PN_ONE_CREDIT_FLOW_CONTROL:
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000261 if (pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
262 PEP_IND_READY, priority) == 0)
263 pn->rx_credits = 1;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700264 break;
265 case PN_MULTI_CREDIT_FLOW_CONTROL:
266 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
267 break;
268 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
269 CREDITS_MAX - pn->rx_credits,
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000270 priority) == 0)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700271 pn->rx_credits = CREDITS_MAX;
272 break;
273 }
274}
275
276static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
277{
278 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000279 struct pnpipehdr *hdr;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800280 int wake = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700281
282 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
283 return -EINVAL;
284
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000285 hdr = pnp_hdr(skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700286 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
287 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
288 (unsigned)hdr->data[0]);
289 return -EOPNOTSUPP;
290 }
291
292 switch (hdr->data[1]) {
293 case PN_PEP_IND_FLOW_CONTROL:
294 switch (pn->tx_fc) {
295 case PN_LEGACY_FLOW_CONTROL:
296 switch (hdr->data[4]) {
297 case PEP_IND_BUSY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800298 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700299 break;
300 case PEP_IND_READY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800301 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700302 break;
303 }
304 break;
305 case PN_ONE_CREDIT_FLOW_CONTROL:
306 if (hdr->data[4] == PEP_IND_READY)
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800307 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700308 break;
309 }
310 break;
311
312 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
313 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
314 break;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800315 atomic_add(wake = hdr->data[4], &pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700316 break;
317
318 default:
319 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
320 (unsigned)hdr->data[1]);
321 return -EOPNOTSUPP;
322 }
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800323 if (wake)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700324 sk->sk_write_space(sk);
325 return 0;
326}
327
328static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
329{
330 struct pep_sock *pn = pep_sk(sk);
331 struct pnpipehdr *hdr = pnp_hdr(skb);
332 u8 n_sb = hdr->data[0];
333
334 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
335 __skb_pull(skb, sizeof(*hdr));
336 while (n_sb > 0) {
337 u8 type, buf[2], len = sizeof(buf);
338 u8 *data = pep_get_sb(skb, &type, &len, buf);
339
340 if (data == NULL)
341 return -EINVAL;
342 switch (type) {
343 case PN_PIPE_SB_NEGOTIATED_FC:
344 if (len < 2 || (data[0] | data[1]) > 3)
345 break;
346 pn->tx_fc = data[0] & 3;
347 pn->rx_fc = data[1] & 3;
348 break;
349 }
350 n_sb--;
351 }
352 return 0;
353}
354
355/* Queue an skb to a connected sock.
356 * Socket lock must be held. */
357static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
358{
359 struct pep_sock *pn = pep_sk(sk);
360 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700361 struct sk_buff_head *queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700362 int err = 0;
363
364 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
365
366 switch (hdr->message_id) {
367 case PNS_PEP_CONNECT_REQ:
368 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
369 break;
370
371 case PNS_PEP_DISCONNECT_REQ:
372 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
373 sk->sk_state = TCP_CLOSE_WAIT;
374 if (!sock_flag(sk, SOCK_DEAD))
375 sk->sk_state_change(sk);
376 break;
377
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000378#ifdef CONFIG_PHONET_PIPECTRLR
379 case PNS_PEP_DISCONNECT_RESP:
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000380 sk->sk_state = TCP_CLOSE;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000381 break;
382#endif
383
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700384 case PNS_PEP_ENABLE_REQ:
385 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
386 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
387 break;
388
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000389#ifdef CONFIG_PHONET_PIPECTRLR
390 case PNS_PEP_ENABLE_RESP:
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000391 pipe_handler_send_ind(sk, PNS_PIPE_ENABLED_IND);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000392
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000393 if (!pn_flow_safe(pn->tx_fc)) {
394 atomic_set(&pn->tx_credits, 1);
395 sk->sk_write_space(sk);
396 }
397 if (sk->sk_state == TCP_ESTABLISHED)
398 break; /* Nothing to do */
399 sk->sk_state = TCP_ESTABLISHED;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000400 pipe_grant_credits(sk, GFP_ATOMIC);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000401 break;
402#endif
403
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700404 case PNS_PEP_RESET_REQ:
405 switch (hdr->state_after_reset) {
406 case PN_PIPE_DISABLE:
407 pn->init_enable = 0;
408 break;
409 case PN_PIPE_ENABLE:
410 pn->init_enable = 1;
411 break;
412 default: /* not allowed to send an error here!? */
413 err = -EINVAL;
414 goto out;
415 }
416 /* fall through */
417 case PNS_PEP_DISABLE_REQ:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800418 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700419 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
420 break;
421
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000422#ifdef CONFIG_PHONET_PIPECTRLR
423 case PNS_PEP_DISABLE_RESP:
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000424 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000425 pipe_handler_send_ind(sk, PNS_PIPE_DISABLED_IND);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000426 sk->sk_state = TCP_SYN_RECV;
427 pn->rx_credits = 0;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000428 break;
429#endif
430
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700431 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000432 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
433 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700434 break;
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000435 }
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700436 __skb_pull(skb, 4);
437 queue = &pn->ctrlreq_queue;
438 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700439
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +0000440 case PNS_PIPE_ALIGNED_DATA:
441 __skb_pull(skb, 1);
442 /* fall through */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700443 case PNS_PIPE_DATA:
444 __skb_pull(skb, 3); /* Pipe data header */
445 if (!pn_flow_safe(pn->rx_fc)) {
446 err = sock_queue_rcv_skb(sk, skb);
447 if (!err)
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000448 return NET_RX_SUCCESS;
449 err = -ENOBUFS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700450 break;
451 }
452
453 if (pn->rx_credits == 0) {
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000454 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700455 err = -ENOBUFS;
456 break;
457 }
458 pn->rx_credits--;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700459 queue = &sk->sk_receive_queue;
460 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700461
462 case PNS_PEP_STATUS_IND:
463 pipe_rcv_status(sk, skb);
464 break;
465
466 case PNS_PIPE_REDIRECTED_IND:
467 err = pipe_rcv_created(sk, skb);
468 break;
469
470 case PNS_PIPE_CREATED_IND:
471 err = pipe_rcv_created(sk, skb);
472 if (err)
473 break;
474 /* fall through */
475 case PNS_PIPE_RESET_IND:
476 if (!pn->init_enable)
477 break;
478 /* fall through */
479 case PNS_PIPE_ENABLED_IND:
480 if (!pn_flow_safe(pn->tx_fc)) {
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800481 atomic_set(&pn->tx_credits, 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700482 sk->sk_write_space(sk);
483 }
484 if (sk->sk_state == TCP_ESTABLISHED)
485 break; /* Nothing to do */
486 sk->sk_state = TCP_ESTABLISHED;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000487 pipe_grant_credits(sk, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700488 break;
489
490 case PNS_PIPE_DISABLED_IND:
491 sk->sk_state = TCP_SYN_RECV;
492 pn->rx_credits = 0;
493 break;
494
495 default:
496 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
497 hdr->message_id);
498 err = -EINVAL;
499 }
500out:
501 kfree_skb(skb);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000502 return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700503
504queue:
505 skb->dev = NULL;
506 skb_set_owner_r(skb, sk);
507 err = skb->len;
508 skb_queue_tail(queue, skb);
509 if (!sock_flag(sk, SOCK_DEAD))
510 sk->sk_data_ready(sk, err);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000511 return NET_RX_SUCCESS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700512}
513
514/* Destroy connected sock. */
515static void pipe_destruct(struct sock *sk)
516{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700517 struct pep_sock *pn = pep_sk(sk);
518
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700519 skb_queue_purge(&sk->sk_receive_queue);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700520 skb_queue_purge(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700521}
522
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000523#ifdef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000524static u8 pipe_negotiate_fc(const u8 *fcs, unsigned n)
525{
526 unsigned i;
527 u8 final_fc = PN_NO_FLOW_CONTROL;
528
529 for (i = 0; i < n; i++) {
530 u8 fc = fcs[i];
531
532 if (fc > final_fc && fc < PN_MAX_FLOW_CONTROL)
533 final_fc = fc;
534 }
535 return final_fc;
536}
537
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000538static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
539{
540 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000541 struct pnpipehdr *hdr;
542 u8 n_sb;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000543
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000544 if (!pskb_pull(skb, sizeof(*hdr) + 4))
545 return -EINVAL;
546
547 hdr = pnp_hdr(skb);
548
549 /* Parse sub-blocks */
550 n_sb = hdr->data[4];
551 while (n_sb > 0) {
552 u8 type, buf[6], len = sizeof(buf);
553 const u8 *data = pep_get_sb(skb, &type, &len, buf);
554
555 if (data == NULL)
556 return -EINVAL;
557
558 switch (type) {
559 case PN_PIPE_SB_REQUIRED_FC_TX:
560 if (len < 2 || len < data[0])
561 break;
562 pn->tx_fc = pipe_negotiate_fc(data + 2, len - 2);
563 break;
564
565 case PN_PIPE_SB_PREFERRED_FC_RX:
566 if (len < 2 || len < data[0])
567 break;
568 pn->rx_fc = pipe_negotiate_fc(data + 2, len - 2);
569 break;
570
571 }
572 n_sb--;
573 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000574
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000575 sk->sk_state = TCP_SYN_RECV;
576 sk->sk_backlog_rcv = pipe_do_rcv;
577 sk->sk_destruct = pipe_destruct;
578 pn->rx_credits = 0;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000579 sk->sk_state_change(sk);
580
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000581 return pipe_handler_send_created_ind(sk);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000582}
583#endif
584
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700585static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb)
586{
587 struct sock *newsk;
588 struct pep_sock *newpn, *pn = pep_sk(sk);
589 struct pnpipehdr *hdr;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000590 struct sockaddr_pn dst, src;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700591 u16 peer_type;
592 u8 pipe_handle, enabled, n_sb;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000593 u8 aligned = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700594
595 if (!pskb_pull(skb, sizeof(*hdr) + 4))
596 return -EINVAL;
597
598 hdr = pnp_hdr(skb);
599 pipe_handle = hdr->pipe_handle;
600 switch (hdr->state_after_connect) {
601 case PN_PIPE_DISABLE:
602 enabled = 0;
603 break;
604 case PN_PIPE_ENABLE:
605 enabled = 1;
606 break;
607 default:
608 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM);
609 return -EINVAL;
610 }
611 peer_type = hdr->other_pep_type << 8;
612
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700613 /* Parse sub-blocks (options) */
614 n_sb = hdr->data[4];
615 while (n_sb > 0) {
616 u8 type, buf[1], len = sizeof(buf);
617 const u8 *data = pep_get_sb(skb, &type, &len, buf);
618
619 if (data == NULL)
620 return -EINVAL;
621 switch (type) {
622 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
623 if (len < 1)
624 return -EINVAL;
625 peer_type = (peer_type & 0xff00) | data[0];
626 break;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000627 case PN_PIPE_SB_ALIGNED_DATA:
628 aligned = data[0] != 0;
629 break;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700630 }
631 n_sb--;
632 }
633
634 skb = skb_clone(skb, GFP_ATOMIC);
635 if (!skb)
636 return -ENOMEM;
637
638 /* Create a new to-be-accepted sock */
639 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot);
640 if (!newsk) {
641 kfree_skb(skb);
642 return -ENOMEM;
643 }
644 sock_init_data(NULL, newsk);
645 newsk->sk_state = TCP_SYN_RECV;
646 newsk->sk_backlog_rcv = pipe_do_rcv;
647 newsk->sk_protocol = sk->sk_protocol;
648 newsk->sk_destruct = pipe_destruct;
649
650 newpn = pep_sk(newsk);
651 pn_skb_get_dst_sockaddr(skb, &dst);
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000652 pn_skb_get_src_sockaddr(skb, &src);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700653 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000654 newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
655 newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700656 skb_queue_head_init(&newpn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700657 newpn->pipe_handle = pipe_handle;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800658 atomic_set(&newpn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700659 newpn->peer_type = peer_type;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800660 newpn->rx_credits = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700661 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
662 newpn->init_enable = enabled;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +0000663 newpn->aligned = aligned;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700664
665 BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue));
666 skb_queue_head(&newsk->sk_receive_queue, skb);
667 if (!sock_flag(sk, SOCK_DEAD))
668 sk->sk_data_ready(sk, 0);
669
670 sk_acceptq_added(sk);
671 sk_add_node(newsk, &pn->ackq);
672 return 0;
673}
674
675/* Listening sock must be locked */
676static struct sock *pep_find_pipe(const struct hlist_head *hlist,
677 const struct sockaddr_pn *dst,
678 u8 pipe_handle)
679{
680 struct hlist_node *node;
681 struct sock *sknode;
682 u16 dobj = pn_sockaddr_get_object(dst);
683
684 sk_for_each(sknode, node, hlist) {
685 struct pep_sock *pnnode = pep_sk(sknode);
686
687 /* Ports match, but addresses might not: */
688 if (pnnode->pn_sk.sobject != dobj)
689 continue;
690 if (pnnode->pipe_handle != pipe_handle)
691 continue;
692 if (sknode->sk_state == TCP_CLOSE_WAIT)
693 continue;
694
695 sock_hold(sknode);
696 return sknode;
697 }
698 return NULL;
699}
700
701/*
702 * Deliver an skb to a listening sock.
703 * Socket lock must be held.
704 * We then queue the skb to the right connected sock (if any).
705 */
706static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
707{
708 struct pep_sock *pn = pep_sk(sk);
709 struct sock *sknode;
Rémi Denis-Courmont2ddc1ac2009-02-10 17:14:50 -0800710 struct pnpipehdr *hdr;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700711 struct sockaddr_pn dst;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700712 u8 pipe_handle;
713
714 if (!pskb_may_pull(skb, sizeof(*hdr)))
715 goto drop;
716
717 hdr = pnp_hdr(skb);
718 pipe_handle = hdr->pipe_handle;
719 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
720 goto drop;
721
722 pn_skb_get_dst_sockaddr(skb, &dst);
723
724 /* Look for an existing pipe handle */
725 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
726 if (sknode)
727 return sk_receive_skb(sknode, skb, 1);
728
729 /* Look for a pipe handle pending accept */
730 sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle);
731 if (sknode) {
732 sock_put(sknode);
733 if (net_ratelimit())
734 printk(KERN_WARNING"Phonet unconnected PEP ignored");
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700735 goto drop;
736 }
737
738 switch (hdr->message_id) {
739 case PNS_PEP_CONNECT_REQ:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000740 if (sk->sk_state == TCP_LISTEN && !sk_acceptq_is_full(sk))
741 pep_connreq_rcv(sk, skb);
742 else
743 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700744 break;
745
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000746#ifdef CONFIG_PHONET_PIPECTRLR
747 case PNS_PEP_CONNECT_RESP:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000748 pep_connresp_rcv(sk, skb);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000749 break;
750#endif
751
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700752 case PNS_PEP_DISCONNECT_REQ:
753 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
754 break;
755
756 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700757 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700758 break;
759
760 case PNS_PEP_RESET_REQ:
761 case PNS_PEP_ENABLE_REQ:
762 case PNS_PEP_DISABLE_REQ:
763 /* invalid handle is not even allowed here! */
764 default:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000765 break;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700766 }
767drop:
768 kfree_skb(skb);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000769 return NET_RX_SUCCESS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700770}
771
David S. Miller00497132011-02-25 11:23:22 -0800772#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000773static int pipe_do_remove(struct sock *sk)
774{
775 struct pep_sock *pn = pep_sk(sk);
776 struct pnpipehdr *ph;
777 struct sk_buff *skb;
778
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000779 skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL);
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000780 if (!skb)
781 return -ENOMEM;
782
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000783 ph = pnp_hdr(skb);
784 ph->utid = 0;
785 ph->message_id = PNS_PIPE_REMOVE_REQ;
786 ph->pipe_handle = pn->pipe_handle;
787 ph->data[0] = PAD;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000788 return pn_skb_send(sk, skb, NULL);
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000789}
David S. Miller00497132011-02-25 11:23:22 -0800790#endif
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000791
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700792/* associated socket ceases to exist */
793static void pep_sock_close(struct sock *sk, long timeout)
794{
795 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700796 int ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700797
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -0700798 sock_hold(sk); /* keep a reference after sk_common_release() */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700799 sk_common_release(sk);
800
801 lock_sock(sk);
802 if (sk->sk_state == TCP_LISTEN) {
803 /* Destroy the listen queue */
804 struct sock *sknode;
805 struct hlist_node *p, *n;
806
807 sk_for_each_safe(sknode, p, n, &pn->ackq)
808 sk_del_node_init(sknode);
809 sk->sk_state = TCP_CLOSE;
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000810 } else if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
811#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000812 /* Forcefully remove dangling Phonet pipe */
813 pipe_do_remove(sk);
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000814#else
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000815 /* send pep disconnect request */
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000816 pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD, NULL, 0);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000817 sk->sk_state = TCP_CLOSE;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000818#endif
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000819 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000820
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700821 ifindex = pn->ifindex;
822 pn->ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700823 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700824
825 if (ifindex)
826 gprs_detach(sk);
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -0700827 sock_put(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700828}
829
830static int pep_wait_connreq(struct sock *sk, int noblock)
831{
832 struct task_struct *tsk = current;
833 struct pep_sock *pn = pep_sk(sk);
834 long timeo = sock_rcvtimeo(sk, noblock);
835
836 for (;;) {
837 DEFINE_WAIT(wait);
838
839 if (sk->sk_state != TCP_LISTEN)
840 return -EINVAL;
841 if (!hlist_empty(&pn->ackq))
842 break;
843 if (!timeo)
844 return -EWOULDBLOCK;
845 if (signal_pending(tsk))
846 return sock_intr_errno(timeo);
847
Eric Dumazet43815482010-04-29 11:01:49 +0000848 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700849 TASK_INTERRUPTIBLE);
850 release_sock(sk);
851 timeo = schedule_timeout(timeo);
852 lock_sock(sk);
Eric Dumazet43815482010-04-29 11:01:49 +0000853 finish_wait(sk_sleep(sk), &wait);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700854 }
855
856 return 0;
857}
858
859static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
860{
861 struct pep_sock *pn = pep_sk(sk);
862 struct sock *newsk = NULL;
863 struct sk_buff *oskb;
864 int err;
865
866 lock_sock(sk);
867 err = pep_wait_connreq(sk, flags & O_NONBLOCK);
868 if (err)
869 goto out;
870
871 newsk = __sk_head(&pn->ackq);
872
873 oskb = skb_dequeue(&newsk->sk_receive_queue);
874 err = pep_accept_conn(newsk, oskb);
875 if (err) {
876 skb_queue_head(&newsk->sk_receive_queue, oskb);
877 newsk = NULL;
878 goto out;
879 }
Rémi Denis-Courmont635f0812010-07-07 20:56:53 +0000880 kfree_skb(oskb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700881
882 sock_hold(sk);
883 pep_sk(newsk)->listener = sk;
884
885 sock_hold(newsk);
886 sk_del_node_init(newsk);
887 sk_acceptq_removed(sk);
888 sk_add_node(newsk, &pn->hlist);
889 __sock_put(newsk);
890
891out:
892 release_sock(sk);
893 *errp = err;
894 return newsk;
895}
896
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000897#ifdef CONFIG_PHONET_PIPECTRLR
898static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
899{
900 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000901 const struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000902 u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD };
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000903
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000904 pn->pn_sk.dobject = pn_sockaddr_get_object(spn);
905 pn->pn_sk.resource = pn_sockaddr_get_resource(spn);
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000906 return pipe_handler_request(sk, PNS_PEP_CONNECT_REQ,
907 PN_PIPE_DISABLE, data, 4);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000908}
909#endif
910
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700911static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
912{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700913 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700914 int answ;
915
916 switch (cmd) {
917 case SIOCINQ:
918 if (sk->sk_state == TCP_LISTEN)
919 return -EINVAL;
920
921 lock_sock(sk);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800922 if (sock_flag(sk, SOCK_URGINLINE) &&
923 !skb_queue_empty(&pn->ctrlreq_queue))
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700924 answ = skb_peek(&pn->ctrlreq_queue)->len;
925 else if (!skb_queue_empty(&sk->sk_receive_queue))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700926 answ = skb_peek(&sk->sk_receive_queue)->len;
927 else
928 answ = 0;
929 release_sock(sk);
930 return put_user(answ, (int __user *)arg);
931 }
932
933 return -ENOIOCTLCMD;
934}
935
936static int pep_init(struct sock *sk)
937{
938 struct pep_sock *pn = pep_sk(sk);
939
940 INIT_HLIST_HEAD(&pn->ackq);
941 INIT_HLIST_HEAD(&pn->hlist);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700942 skb_queue_head_init(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700943 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
944 return 0;
945}
946
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700947static int pep_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700948 char __user *optval, unsigned int optlen)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700949{
950 struct pep_sock *pn = pep_sk(sk);
951 int val = 0, err = 0;
952
953 if (level != SOL_PNPIPE)
954 return -ENOPROTOOPT;
955 if (optlen >= sizeof(int)) {
956 if (get_user(val, (int __user *) optval))
957 return -EFAULT;
958 }
959
960 lock_sock(sk);
961 switch (optname) {
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000962#ifdef CONFIG_PHONET_PIPECTRLR
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000963 case PNPIPE_PIPE_HANDLE:
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000964 if (val) {
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000965 pn->pipe_handle = val;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000966 break;
967 }
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000968#endif
969
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700970 case PNPIPE_ENCAP:
971 if (val && val != PNPIPE_ENCAP_IP) {
972 err = -EINVAL;
973 break;
974 }
975 if (!pn->ifindex == !val)
976 break; /* Nothing to do! */
977 if (!capable(CAP_NET_ADMIN)) {
978 err = -EPERM;
979 break;
980 }
981 if (val) {
982 release_sock(sk);
983 err = gprs_attach(sk);
984 if (err > 0) {
985 pn->ifindex = err;
986 err = 0;
987 }
988 } else {
989 pn->ifindex = 0;
990 release_sock(sk);
991 gprs_detach(sk);
992 err = 0;
993 }
994 goto out_norel;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000995
996#ifdef CONFIG_PHONET_PIPECTRLR
997 case PNPIPE_ENABLE:
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000998 if ((1 << sk->sk_state) & ~(TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000999 err = -ENOTCONN;
1000 break;
1001 }
1002 err = pipe_handler_enable_pipe(sk, val);
1003 break;
1004#endif
1005
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001006 default:
1007 err = -ENOPROTOOPT;
1008 }
1009 release_sock(sk);
1010
1011out_norel:
1012 return err;
1013}
1014
1015static int pep_getsockopt(struct sock *sk, int level, int optname,
1016 char __user *optval, int __user *optlen)
1017{
1018 struct pep_sock *pn = pep_sk(sk);
1019 int len, val;
1020
1021 if (level != SOL_PNPIPE)
1022 return -ENOPROTOOPT;
1023 if (get_user(len, optlen))
1024 return -EFAULT;
1025
1026 switch (optname) {
1027 case PNPIPE_ENCAP:
1028 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
1029 break;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +00001030
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001031 case PNPIPE_IFINDEX:
1032 val = pn->ifindex;
1033 break;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001034
1035#ifdef CONFIG_PHONET_PIPECTRLR
1036 case PNPIPE_ENABLE:
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +00001037 val = sk->sk_state == TCP_ESTABLISHED;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +00001038 break;
1039#endif
1040
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001041 default:
1042 return -ENOPROTOOPT;
1043 }
1044
1045 len = min_t(unsigned int, sizeof(int), len);
1046 if (put_user(len, optlen))
1047 return -EFAULT;
1048 if (put_user(val, (int __user *) optval))
1049 return -EFAULT;
1050 return 0;
1051}
1052
1053static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1054{
1055 struct pep_sock *pn = pep_sk(sk);
1056 struct pnpipehdr *ph;
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001057 int err;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001058
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001059 if (pn_flow_safe(pn->tx_fc) &&
1060 !atomic_add_unless(&pn->tx_credits, -1, 0)) {
1061 kfree_skb(skb);
1062 return -ENOBUFS;
1063 }
1064
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001065 skb_push(skb, 3 + pn->aligned);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001066 skb_reset_transport_header(skb);
1067 ph = pnp_hdr(skb);
1068 ph->utid = 0;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001069 if (pn->aligned) {
1070 ph->message_id = PNS_PIPE_ALIGNED_DATA;
1071 ph->data[0] = 0; /* padding */
1072 } else
1073 ph->message_id = PNS_PIPE_DATA;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001074 ph->pipe_handle = pn->pipe_handle;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +00001075 err = pn_skb_send(sk, skb, NULL);
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001076
1077 if (err && pn_flow_safe(pn->tx_fc))
1078 atomic_inc(&pn->tx_credits);
1079 return err;
1080
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001081}
1082
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001083static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
1084 struct msghdr *msg, size_t len)
1085{
1086 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001087 struct sk_buff *skb;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001088 long timeo;
1089 int flags = msg->msg_flags;
1090 int err, done;
1091
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001092 if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
1093 MSG_CMSG_COMPAT)) ||
1094 !(msg->msg_flags & MSG_EOR))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001095 return -EOPNOTSUPP;
1096
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001097 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
1098 flags & MSG_DONTWAIT, &err);
1099 if (!skb)
Rémi Denis-Courmont02ac3262010-08-30 12:57:04 +00001100 return err;
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001101
1102 skb_reserve(skb, MAX_PHONET_HEADER + 3);
1103 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1104 if (err < 0)
1105 goto outfree;
1106
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001107 lock_sock(sk);
1108 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1109 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
1110 err = -ENOTCONN;
1111 goto out;
1112 }
1113 if (sk->sk_state != TCP_ESTABLISHED) {
1114 /* Wait until the pipe gets to enabled state */
1115disabled:
1116 err = sk_stream_wait_connect(sk, &timeo);
1117 if (err)
1118 goto out;
1119
1120 if (sk->sk_state == TCP_CLOSE_WAIT) {
1121 err = -ECONNRESET;
1122 goto out;
1123 }
1124 }
1125 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
1126
1127 /* Wait until flow control allows TX */
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001128 done = atomic_read(&pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001129 while (!done) {
1130 DEFINE_WAIT(wait);
1131
1132 if (!timeo) {
1133 err = -EAGAIN;
1134 goto out;
1135 }
1136 if (signal_pending(current)) {
1137 err = sock_intr_errno(timeo);
1138 goto out;
1139 }
1140
Eric Dumazet43815482010-04-29 11:01:49 +00001141 prepare_to_wait(sk_sleep(sk), &wait,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001142 TASK_INTERRUPTIBLE);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001143 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
Eric Dumazet43815482010-04-29 11:01:49 +00001144 finish_wait(sk_sleep(sk), &wait);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001145
1146 if (sk->sk_state != TCP_ESTABLISHED)
1147 goto disabled;
1148 }
1149
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001150 err = pipe_skb_send(sk, skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001151 if (err >= 0)
1152 err = len; /* success! */
1153 skb = NULL;
1154out:
1155 release_sock(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001156outfree:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001157 kfree_skb(skb);
1158 return err;
1159}
1160
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001161int pep_writeable(struct sock *sk)
1162{
1163 struct pep_sock *pn = pep_sk(sk);
1164
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001165 return atomic_read(&pn->tx_credits);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001166}
1167
1168int pep_write(struct sock *sk, struct sk_buff *skb)
1169{
1170 struct sk_buff *rskb, *fs;
1171 int flen = 0;
1172
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001173 if (pep_sk(sk)->aligned)
1174 return pipe_skb_send(sk, skb);
1175
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001176 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
1177 if (!rskb) {
1178 kfree_skb(skb);
1179 return -ENOMEM;
1180 }
1181 skb_shinfo(rskb)->frag_list = skb;
1182 rskb->len += skb->len;
1183 rskb->data_len += rskb->len;
1184 rskb->truesize += rskb->len;
1185
1186 /* Avoid nested fragments */
David S. Miller5c313e92009-06-09 00:21:58 -07001187 skb_walk_frags(skb, fs)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001188 flen += fs->len;
1189 skb->next = skb_shinfo(skb)->frag_list;
David S. Miller5c313e92009-06-09 00:21:58 -07001190 skb_frag_list_init(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001191 skb->len -= flen;
1192 skb->data_len -= flen;
1193 skb->truesize -= flen;
1194
1195 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
1196 return pipe_skb_send(sk, rskb);
1197}
1198
1199struct sk_buff *pep_read(struct sock *sk)
1200{
1201 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1202
1203 if (sk->sk_state == TCP_ESTABLISHED)
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +00001204 pipe_grant_credits(sk, GFP_ATOMIC);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001205 return skb;
1206}
1207
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001208static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
1209 struct msghdr *msg, size_t len, int noblock,
1210 int flags, int *addr_len)
1211{
1212 struct sk_buff *skb;
1213 int err;
1214
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001215 if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
1216 MSG_NOSIGNAL|MSG_CMSG_COMPAT))
1217 return -EOPNOTSUPP;
1218
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001219 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
1220 return -ENOTCONN;
1221
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001222 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
1223 /* Dequeue and acknowledge control request */
1224 struct pep_sock *pn = pep_sk(sk);
1225
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001226 if (flags & MSG_PEEK)
1227 return -EOPNOTSUPP;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001228 skb = skb_dequeue(&pn->ctrlreq_queue);
1229 if (skb) {
1230 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1231 GFP_KERNEL);
1232 msg->msg_flags |= MSG_OOB;
1233 goto copy;
1234 }
1235 if (flags & MSG_OOB)
1236 return -EINVAL;
1237 }
1238
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001239 skb = skb_recv_datagram(sk, flags, noblock, &err);
1240 lock_sock(sk);
1241 if (skb == NULL) {
1242 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1243 err = -ECONNRESET;
1244 release_sock(sk);
1245 return err;
1246 }
1247
1248 if (sk->sk_state == TCP_ESTABLISHED)
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +00001249 pipe_grant_credits(sk, GFP_KERNEL);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001250 release_sock(sk);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001251copy:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001252 msg->msg_flags |= MSG_EOR;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001253 if (skb->len > len)
1254 msg->msg_flags |= MSG_TRUNC;
1255 else
1256 len = skb->len;
1257
1258 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1259 if (!err)
1260 err = (flags & MSG_TRUNC) ? skb->len : len;
1261
1262 skb_free_datagram(sk, skb);
1263 return err;
1264}
1265
1266static void pep_sock_unhash(struct sock *sk)
1267{
1268 struct pep_sock *pn = pep_sk(sk);
1269 struct sock *skparent = NULL;
1270
1271 lock_sock(sk);
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001272
1273#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001274 if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1275 skparent = pn->listener;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001276 release_sock(sk);
1277
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001278 pn = pep_sk(skparent);
Rémi Denis-Courmont7dfde172010-05-26 00:44:44 +00001279 lock_sock(skparent);
1280 sk_del_node_init(sk);
1281 sk = skparent;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001282 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001283#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001284 /* Unhash a listening sock only when it is closed
1285 * and all of its active connected pipes are closed. */
1286 if (hlist_empty(&pn->hlist))
1287 pn_sock_unhash(&pn->pn_sk.sk);
1288 release_sock(sk);
1289
1290 if (skparent)
1291 sock_put(skparent);
1292}
1293
1294static struct proto pep_proto = {
1295 .close = pep_sock_close,
1296 .accept = pep_sock_accept,
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001297#ifdef CONFIG_PHONET_PIPECTRLR
1298 .connect = pep_sock_connect,
1299#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001300 .ioctl = pep_ioctl,
1301 .init = pep_init,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001302 .setsockopt = pep_setsockopt,
1303 .getsockopt = pep_getsockopt,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001304 .sendmsg = pep_sendmsg,
1305 .recvmsg = pep_recvmsg,
1306 .backlog_rcv = pep_do_rcv,
1307 .hash = pn_sock_hash,
1308 .unhash = pep_sock_unhash,
1309 .get_port = pn_sock_get_port,
1310 .obj_size = sizeof(struct pep_sock),
1311 .owner = THIS_MODULE,
1312 .name = "PNPIPE",
1313};
1314
1315static struct phonet_protocol pep_pn_proto = {
1316 .ops = &phonet_stream_ops,
1317 .prot = &pep_proto,
1318 .sock_type = SOCK_SEQPACKET,
1319};
1320
1321static int __init pep_register(void)
1322{
1323 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1324}
1325
1326static void __exit pep_unregister(void)
1327{
1328 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1329}
1330
1331module_init(pep_register);
1332module_exit(pep_unregister);
1333MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1334MODULE_DESCRIPTION("Phonet pipe protocol");
1335MODULE_LICENSE("GPL");
1336MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);