blob: abfb795af142aadb3252e7a788cfd8ea04eabefe [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:
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +000045 * - sk_state, hlist: sock lock needed
Rémi Denis-Courmont96414582008-10-05 11:15:13 -070046 * - 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
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000205static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code,
206 gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700207{
208 static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ };
209 WARN_ON(code == PN_PIPE_NO_ERROR);
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000210 return pep_reply(sk, skb, code, data, sizeof(data), priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700211}
212
213/* Control requests are not sent by the pipe service and have a specific
214 * message format. */
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700215static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code,
216 gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700217{
218 const struct pnpipehdr *oph = pnp_hdr(oskb);
219 struct sk_buff *skb;
220 struct pnpipehdr *ph;
221 struct sockaddr_pn dst;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000222 u8 data[4] = {
223 oph->data[0], /* PEP type */
224 code, /* error code, at an unusual offset */
225 PAD, PAD,
226 };
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700227
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000228 skb = pep_alloc_skb(sk, data, 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700229 if (!skb)
230 return -ENOMEM;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700231
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000232 ph = pnp_hdr(skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700233 ph->utid = oph->utid;
234 ph->message_id = PNS_PEP_CTRL_RESP;
235 ph->pipe_handle = oph->pipe_handle;
236 ph->data[0] = oph->data[1]; /* CTRL id */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700237
238 pn_skb_get_src_sockaddr(oskb, &dst);
239 return pn_skb_send(sk, skb, &dst);
240}
241
242static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority)
243{
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000244 u8 data[4] = { type, PAD, PAD, status };
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700245
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000246 return pep_indicate(sk, PNS_PEP_STATUS_IND, PN_PEP_TYPE_COMMON,
247 data, 4, priority);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700248}
249
250/* Send our RX flow control information to the sender.
251 * Socket must be locked. */
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000252static void pipe_grant_credits(struct sock *sk, gfp_t priority)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700253{
254 struct pep_sock *pn = pep_sk(sk);
255
256 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
257
258 switch (pn->rx_fc) {
259 case PN_LEGACY_FLOW_CONTROL: /* TODO */
260 break;
261 case PN_ONE_CREDIT_FLOW_CONTROL:
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000262 if (pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL,
263 PEP_IND_READY, priority) == 0)
264 pn->rx_credits = 1;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700265 break;
266 case PN_MULTI_CREDIT_FLOW_CONTROL:
267 if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX)
268 break;
269 if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS,
270 CREDITS_MAX - pn->rx_credits,
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000271 priority) == 0)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700272 pn->rx_credits = CREDITS_MAX;
273 break;
274 }
275}
276
277static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb)
278{
279 struct pep_sock *pn = pep_sk(sk);
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000280 struct pnpipehdr *hdr;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800281 int wake = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700282
283 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
284 return -EINVAL;
285
Kumar Sanghvia91e7d42010-09-27 23:10:42 +0000286 hdr = pnp_hdr(skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700287 if (hdr->data[0] != PN_PEP_TYPE_COMMON) {
288 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n",
289 (unsigned)hdr->data[0]);
290 return -EOPNOTSUPP;
291 }
292
293 switch (hdr->data[1]) {
294 case PN_PEP_IND_FLOW_CONTROL:
295 switch (pn->tx_fc) {
296 case PN_LEGACY_FLOW_CONTROL:
297 switch (hdr->data[4]) {
298 case PEP_IND_BUSY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800299 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700300 break;
301 case PEP_IND_READY:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800302 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700303 break;
304 }
305 break;
306 case PN_ONE_CREDIT_FLOW_CONTROL:
307 if (hdr->data[4] == PEP_IND_READY)
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800308 atomic_set(&pn->tx_credits, wake = 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700309 break;
310 }
311 break;
312
313 case PN_PEP_IND_ID_MCFC_GRANT_CREDITS:
314 if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL)
315 break;
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800316 atomic_add(wake = hdr->data[4], &pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700317 break;
318
319 default:
320 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n",
321 (unsigned)hdr->data[1]);
322 return -EOPNOTSUPP;
323 }
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800324 if (wake)
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700325 sk->sk_write_space(sk);
326 return 0;
327}
328
329static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb)
330{
331 struct pep_sock *pn = pep_sk(sk);
332 struct pnpipehdr *hdr = pnp_hdr(skb);
333 u8 n_sb = hdr->data[0];
334
335 pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL;
336 __skb_pull(skb, sizeof(*hdr));
337 while (n_sb > 0) {
338 u8 type, buf[2], len = sizeof(buf);
339 u8 *data = pep_get_sb(skb, &type, &len, buf);
340
341 if (data == NULL)
342 return -EINVAL;
343 switch (type) {
344 case PN_PIPE_SB_NEGOTIATED_FC:
345 if (len < 2 || (data[0] | data[1]) > 3)
346 break;
347 pn->tx_fc = data[0] & 3;
348 pn->rx_fc = data[1] & 3;
349 break;
350 }
351 n_sb--;
352 }
353 return 0;
354}
355
356/* Queue an skb to a connected sock.
357 * Socket lock must be held. */
358static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb)
359{
360 struct pep_sock *pn = pep_sk(sk);
361 struct pnpipehdr *hdr = pnp_hdr(skb);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700362 struct sk_buff_head *queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700363 int err = 0;
364
365 BUG_ON(sk->sk_state == TCP_CLOSE_WAIT);
366
367 switch (hdr->message_id) {
368 case PNS_PEP_CONNECT_REQ:
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000369 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700370 break;
371
372 case PNS_PEP_DISCONNECT_REQ:
373 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
374 sk->sk_state = TCP_CLOSE_WAIT;
375 if (!sock_flag(sk, SOCK_DEAD))
376 sk->sk_state_change(sk);
377 break;
378
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000379#ifdef CONFIG_PHONET_PIPECTRLR
380 case PNS_PEP_DISCONNECT_RESP:
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000381 sk->sk_state = TCP_CLOSE;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000382 break;
383#endif
384
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700385 case PNS_PEP_ENABLE_REQ:
386 /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */
387 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
388 break;
389
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000390#ifdef CONFIG_PHONET_PIPECTRLR
391 case PNS_PEP_ENABLE_RESP:
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000392 pipe_handler_send_ind(sk, PNS_PIPE_ENABLED_IND);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000393
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000394 if (!pn_flow_safe(pn->tx_fc)) {
395 atomic_set(&pn->tx_credits, 1);
396 sk->sk_write_space(sk);
397 }
398 if (sk->sk_state == TCP_ESTABLISHED)
399 break; /* Nothing to do */
400 sk->sk_state = TCP_ESTABLISHED;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000401 pipe_grant_credits(sk, GFP_ATOMIC);
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000402 break;
403#endif
404
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700405 case PNS_PEP_RESET_REQ:
406 switch (hdr->state_after_reset) {
407 case PN_PIPE_DISABLE:
408 pn->init_enable = 0;
409 break;
410 case PN_PIPE_ENABLE:
411 pn->init_enable = 1;
412 break;
413 default: /* not allowed to send an error here!? */
414 err = -EINVAL;
415 goto out;
416 }
417 /* fall through */
418 case PNS_PEP_DISABLE_REQ:
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800419 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700420 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
421 break;
422
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000423#ifdef CONFIG_PHONET_PIPECTRLR
424 case PNS_PEP_DISABLE_RESP:
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000425 atomic_set(&pn->tx_credits, 0);
Rémi Denis-Courmont0165d692011-02-24 23:15:00 +0000426 pipe_handler_send_ind(sk, PNS_PIPE_DISABLED_IND);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000427 sk->sk_state = TCP_SYN_RECV;
428 pn->rx_credits = 0;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000429 break;
430#endif
431
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700432 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000433 if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) {
434 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700435 break;
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000436 }
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700437 __skb_pull(skb, 4);
438 queue = &pn->ctrlreq_queue;
439 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700440
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +0000441 case PNS_PIPE_ALIGNED_DATA:
442 __skb_pull(skb, 1);
443 /* fall through */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700444 case PNS_PIPE_DATA:
445 __skb_pull(skb, 3); /* Pipe data header */
446 if (!pn_flow_safe(pn->rx_fc)) {
447 err = sock_queue_rcv_skb(sk, skb);
448 if (!err)
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000449 return NET_RX_SUCCESS;
450 err = -ENOBUFS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700451 break;
452 }
453
454 if (pn->rx_credits == 0) {
Rémi Denis-Courmont2e2fb4b2009-07-21 01:57:59 +0000455 atomic_inc(&sk->sk_drops);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700456 err = -ENOBUFS;
457 break;
458 }
459 pn->rx_credits--;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700460 queue = &sk->sk_receive_queue;
461 goto queue;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700462
463 case PNS_PEP_STATUS_IND:
464 pipe_rcv_status(sk, skb);
465 break;
466
467 case PNS_PIPE_REDIRECTED_IND:
468 err = pipe_rcv_created(sk, skb);
469 break;
470
471 case PNS_PIPE_CREATED_IND:
472 err = pipe_rcv_created(sk, skb);
473 if (err)
474 break;
475 /* fall through */
476 case PNS_PIPE_RESET_IND:
477 if (!pn->init_enable)
478 break;
479 /* fall through */
480 case PNS_PIPE_ENABLED_IND:
481 if (!pn_flow_safe(pn->tx_fc)) {
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -0800482 atomic_set(&pn->tx_credits, 1);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700483 sk->sk_write_space(sk);
484 }
485 if (sk->sk_state == TCP_ESTABLISHED)
486 break; /* Nothing to do */
487 sk->sk_state = TCP_ESTABLISHED;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000488 pipe_grant_credits(sk, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700489 break;
490
491 case PNS_PIPE_DISABLED_IND:
492 sk->sk_state = TCP_SYN_RECV;
493 pn->rx_credits = 0;
494 break;
495
496 default:
497 LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n",
498 hdr->message_id);
499 err = -EINVAL;
500 }
501out:
502 kfree_skb(skb);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000503 return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700504
505queue:
506 skb->dev = NULL;
507 skb_set_owner_r(skb, sk);
508 err = skb->len;
509 skb_queue_tail(queue, skb);
510 if (!sock_flag(sk, SOCK_DEAD))
511 sk->sk_data_ready(sk, err);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000512 return NET_RX_SUCCESS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700513}
514
515/* Destroy connected sock. */
516static void pipe_destruct(struct sock *sk)
517{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700518 struct pep_sock *pn = pep_sk(sk);
519
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700520 skb_queue_purge(&sk->sk_receive_queue);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700521 skb_queue_purge(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700522}
523
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000524#ifdef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000525static u8 pipe_negotiate_fc(const u8 *fcs, unsigned n)
526{
527 unsigned i;
528 u8 final_fc = PN_NO_FLOW_CONTROL;
529
530 for (i = 0; i < n; i++) {
531 u8 fc = fcs[i];
532
533 if (fc > final_fc && fc < PN_MAX_FLOW_CONTROL)
534 final_fc = fc;
535 }
536 return final_fc;
537}
538
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000539static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb)
540{
541 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000542 struct pnpipehdr *hdr;
543 u8 n_sb;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000544
Rémi Denis-Courmont8f44fcc2011-02-24 23:15:01 +0000545 if (!pskb_pull(skb, sizeof(*hdr) + 4))
546 return -EINVAL;
547
548 hdr = pnp_hdr(skb);
549
550 /* Parse sub-blocks */
551 n_sb = hdr->data[4];
552 while (n_sb > 0) {
553 u8 type, buf[6], len = sizeof(buf);
554 const u8 *data = pep_get_sb(skb, &type, &len, buf);
555
556 if (data == NULL)
557 return -EINVAL;
558
559 switch (type) {
560 case PN_PIPE_SB_REQUIRED_FC_TX:
561 if (len < 2 || len < data[0])
562 break;
563 pn->tx_fc = pipe_negotiate_fc(data + 2, len - 2);
564 break;
565
566 case PN_PIPE_SB_PREFERRED_FC_RX:
567 if (len < 2 || len < data[0])
568 break;
569 pn->rx_fc = pipe_negotiate_fc(data + 2, len - 2);
570 break;
571
572 }
573 n_sb--;
574 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000575
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000576 sk->sk_state = TCP_SYN_RECV;
577 sk->sk_backlog_rcv = pipe_do_rcv;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000578 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 -0700585/* Listening sock must be locked */
586static struct sock *pep_find_pipe(const struct hlist_head *hlist,
587 const struct sockaddr_pn *dst,
588 u8 pipe_handle)
589{
590 struct hlist_node *node;
591 struct sock *sknode;
592 u16 dobj = pn_sockaddr_get_object(dst);
593
594 sk_for_each(sknode, node, hlist) {
595 struct pep_sock *pnnode = pep_sk(sknode);
596
597 /* Ports match, but addresses might not: */
598 if (pnnode->pn_sk.sobject != dobj)
599 continue;
600 if (pnnode->pipe_handle != pipe_handle)
601 continue;
602 if (sknode->sk_state == TCP_CLOSE_WAIT)
603 continue;
604
605 sock_hold(sknode);
606 return sknode;
607 }
608 return NULL;
609}
610
611/*
612 * Deliver an skb to a listening sock.
613 * Socket lock must be held.
614 * We then queue the skb to the right connected sock (if any).
615 */
616static int pep_do_rcv(struct sock *sk, struct sk_buff *skb)
617{
618 struct pep_sock *pn = pep_sk(sk);
619 struct sock *sknode;
Rémi Denis-Courmont2ddc1ac2009-02-10 17:14:50 -0800620 struct pnpipehdr *hdr;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700621 struct sockaddr_pn dst;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700622 u8 pipe_handle;
623
624 if (!pskb_may_pull(skb, sizeof(*hdr)))
625 goto drop;
626
627 hdr = pnp_hdr(skb);
628 pipe_handle = hdr->pipe_handle;
629 if (pipe_handle == PN_PIPE_INVALID_HANDLE)
630 goto drop;
631
632 pn_skb_get_dst_sockaddr(skb, &dst);
633
634 /* Look for an existing pipe handle */
635 sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
636 if (sknode)
637 return sk_receive_skb(sknode, skb, 1);
638
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700639 switch (hdr->message_id) {
640 case PNS_PEP_CONNECT_REQ:
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000641 if (sk->sk_state != TCP_LISTEN || sk_acceptq_is_full(sk)) {
642 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE,
643 GFP_ATOMIC);
644 break;
645 }
646 skb_queue_head(&sk->sk_receive_queue, skb);
647 sk_acceptq_added(sk);
648 if (!sock_flag(sk, SOCK_DEAD))
649 sk->sk_data_ready(sk, 0);
650 return NET_RX_SUCCESS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700651
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000652#ifdef CONFIG_PHONET_PIPECTRLR
653 case PNS_PEP_CONNECT_RESP:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000654 pep_connresp_rcv(sk, skb);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000655 break;
656#endif
657
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700658 case PNS_PEP_DISCONNECT_REQ:
659 pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC);
660 break;
661
662 case PNS_PEP_CTRL_REQ:
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700663 pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700664 break;
665
666 case PNS_PEP_RESET_REQ:
667 case PNS_PEP_ENABLE_REQ:
668 case PNS_PEP_DISABLE_REQ:
669 /* invalid handle is not even allowed here! */
670 default:
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000671 break;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700672 }
673drop:
674 kfree_skb(skb);
Rémi Denis-Courmont0ebbf312011-03-08 22:44:08 +0000675 return NET_RX_SUCCESS;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700676}
677
David S. Miller00497132011-02-25 11:23:22 -0800678#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000679static int pipe_do_remove(struct sock *sk)
680{
681 struct pep_sock *pn = pep_sk(sk);
682 struct pnpipehdr *ph;
683 struct sk_buff *skb;
684
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000685 skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL);
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000686 if (!skb)
687 return -ENOMEM;
688
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000689 ph = pnp_hdr(skb);
690 ph->utid = 0;
691 ph->message_id = PNS_PIPE_REMOVE_REQ;
692 ph->pipe_handle = pn->pipe_handle;
693 ph->data[0] = PAD;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000694 return pn_skb_send(sk, skb, NULL);
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000695}
David S. Miller00497132011-02-25 11:23:22 -0800696#endif
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000697
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700698/* associated socket ceases to exist */
699static void pep_sock_close(struct sock *sk, long timeout)
700{
701 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700702 int ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700703
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -0700704 sock_hold(sk); /* keep a reference after sk_common_release() */
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700705 sk_common_release(sk);
706
707 lock_sock(sk);
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000708 if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000709#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont6482f552010-09-15 12:19:53 +0000710 /* Forcefully remove dangling Phonet pipe */
711 pipe_do_remove(sk);
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000712#else
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000713 /* send pep disconnect request */
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000714 pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD, NULL, 0);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000715#endif
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000716 }
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000717 sk->sk_state = TCP_CLOSE;
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000718
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700719 ifindex = pn->ifindex;
720 pn->ifindex = 0;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700721 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700722
723 if (ifindex)
724 gprs_detach(sk);
Rémi Denis-Courmonte5134802010-05-25 16:08:39 -0700725 sock_put(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700726}
727
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700728static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp)
729{
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000730 struct pep_sock *pn = pep_sk(sk), *newpn;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700731 struct sock *newsk = NULL;
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000732 struct sk_buff *skb;
733 struct pnpipehdr *hdr;
734 struct sockaddr_pn dst, src;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700735 int err;
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000736 u16 peer_type;
737 u8 pipe_handle, enabled, n_sb;
738 u8 aligned = 0;
739
740 skb = skb_recv_datagram(sk, 0, flags & O_NONBLOCK, errp);
741 if (!skb)
742 return NULL;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700743
744 lock_sock(sk);
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000745 if (sk->sk_state != TCP_LISTEN) {
746 err = -EINVAL;
747 goto drop;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700748 }
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700749 sk_acceptq_removed(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700750
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000751 err = -EPROTO;
752 if (!pskb_may_pull(skb, sizeof(*hdr) + 4))
753 goto drop;
754
755 hdr = pnp_hdr(skb);
756 pipe_handle = hdr->pipe_handle;
757 switch (hdr->state_after_connect) {
758 case PN_PIPE_DISABLE:
759 enabled = 0;
760 break;
761 case PN_PIPE_ENABLE:
762 enabled = 1;
763 break;
764 default:
765 pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM,
766 GFP_KERNEL);
767 goto drop;
768 }
769 peer_type = hdr->other_pep_type << 8;
770
771 /* Parse sub-blocks (options) */
772 n_sb = hdr->data[4];
773 while (n_sb > 0) {
774 u8 type, buf[1], len = sizeof(buf);
775 const u8 *data = pep_get_sb(skb, &type, &len, buf);
776
777 if (data == NULL)
778 goto drop;
779 switch (type) {
780 case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE:
781 if (len < 1)
782 goto drop;
783 peer_type = (peer_type & 0xff00) | data[0];
784 break;
785 case PN_PIPE_SB_ALIGNED_DATA:
786 aligned = data[0] != 0;
787 break;
788 }
789 n_sb--;
790 }
791
792 /* Check for duplicate pipe handle */
793 newsk = pep_find_pipe(&pn->hlist, &dst, pipe_handle);
794 if (unlikely(newsk)) {
795 __sock_put(newsk);
796 newsk = NULL;
797 pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_KERNEL);
798 goto drop;
799 }
800
801 /* Create a new to-be-accepted sock */
802 newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_KERNEL, sk->sk_prot);
803 if (!newsk) {
804 pep_reject_conn(sk, skb, PN_PIPE_ERR_OVERLOAD, GFP_KERNEL);
805 err = -ENOBUFS;
806 goto drop;
807 }
808
809 sock_init_data(NULL, newsk);
810 newsk->sk_state = TCP_SYN_RECV;
811 newsk->sk_backlog_rcv = pipe_do_rcv;
812 newsk->sk_protocol = sk->sk_protocol;
813 newsk->sk_destruct = pipe_destruct;
814
815 newpn = pep_sk(newsk);
816 pn_skb_get_dst_sockaddr(skb, &dst);
817 pn_skb_get_src_sockaddr(skb, &src);
818 newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst);
819 newpn->pn_sk.dobject = pn_sockaddr_get_object(&src);
820 newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst);
821 sock_hold(sk);
822 newpn->listener = sk;
823 skb_queue_head_init(&newpn->ctrlreq_queue);
824 newpn->pipe_handle = pipe_handle;
825 atomic_set(&newpn->tx_credits, 0);
826 newpn->ifindex = 0;
827 newpn->peer_type = peer_type;
828 newpn->rx_credits = 0;
829 newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL;
830 newpn->init_enable = enabled;
831 newpn->aligned = aligned;
832
833 err = pep_accept_conn(newsk, skb);
834 if (err) {
835 sock_put(newsk);
836 newsk = NULL;
837 goto drop;
838 }
839 sk_add_node(newsk, &pn->hlist);
840drop:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700841 release_sock(sk);
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000842 kfree_skb(skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700843 *errp = err;
844 return newsk;
845}
846
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000847#ifdef CONFIG_PHONET_PIPECTRLR
848static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len)
849{
850 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000851 const struct sockaddr_pn *spn = (struct sockaddr_pn *)addr;
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000852 u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD };
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000853
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +0000854 pn->pn_sk.dobject = pn_sockaddr_get_object(spn);
855 pn->pn_sk.resource = pn_sockaddr_get_resource(spn);
Rémi Denis-Courmontacaf7df2011-03-08 22:44:11 +0000856 pn->pipe_handle = 1; /* anything but INVALID_HANDLE */
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +0000857 return pipe_handler_request(sk, PNS_PEP_CONNECT_REQ,
858 PN_PIPE_DISABLE, data, 4);
Kumar Sanghvib3d62552010-10-12 20:14:43 +0000859}
860#endif
861
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700862static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg)
863{
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700864 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700865 int answ;
866
867 switch (cmd) {
868 case SIOCINQ:
869 if (sk->sk_state == TCP_LISTEN)
870 return -EINVAL;
871
872 lock_sock(sk);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800873 if (sock_flag(sk, SOCK_URGINLINE) &&
874 !skb_queue_empty(&pn->ctrlreq_queue))
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700875 answ = skb_peek(&pn->ctrlreq_queue)->len;
876 else if (!skb_queue_empty(&sk->sk_receive_queue))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700877 answ = skb_peek(&sk->sk_receive_queue)->len;
878 else
879 answ = 0;
880 release_sock(sk);
881 return put_user(answ, (int __user *)arg);
882 }
883
884 return -ENOIOCTLCMD;
885}
886
887static int pep_init(struct sock *sk)
888{
889 struct pep_sock *pn = pep_sk(sk);
890
Rémi Denis-Courmontf7ae8d52011-03-08 22:44:10 +0000891 sk->sk_destruct = pipe_destruct;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700892 INIT_HLIST_HEAD(&pn->hlist);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -0700893 skb_queue_head_init(&pn->ctrlreq_queue);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -0700894 pn->pipe_handle = PN_PIPE_INVALID_HANDLE;
895 return 0;
896}
897
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700898static int pep_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700899 char __user *optval, unsigned int optlen)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700900{
901 struct pep_sock *pn = pep_sk(sk);
902 int val = 0, err = 0;
903
904 if (level != SOL_PNPIPE)
905 return -ENOPROTOOPT;
906 if (optlen >= sizeof(int)) {
907 if (get_user(val, (int __user *) optval))
908 return -EFAULT;
909 }
910
911 lock_sock(sk);
912 switch (optname) {
913 case PNPIPE_ENCAP:
914 if (val && val != PNPIPE_ENCAP_IP) {
915 err = -EINVAL;
916 break;
917 }
918 if (!pn->ifindex == !val)
919 break; /* Nothing to do! */
920 if (!capable(CAP_NET_ADMIN)) {
921 err = -EPERM;
922 break;
923 }
924 if (val) {
925 release_sock(sk);
926 err = gprs_attach(sk);
927 if (err > 0) {
928 pn->ifindex = err;
929 err = 0;
930 }
931 } else {
932 pn->ifindex = 0;
933 release_sock(sk);
934 gprs_detach(sk);
935 err = 0;
936 }
937 goto out_norel;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000938
939#ifdef CONFIG_PHONET_PIPECTRLR
940 case PNPIPE_ENABLE:
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000941 if ((1 << sk->sk_state) & ~(TCPF_SYN_RECV|TCPF_ESTABLISHED)) {
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000942 err = -ENOTCONN;
943 break;
944 }
945 err = pipe_handler_enable_pipe(sk, val);
946 break;
947#endif
948
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700949 default:
950 err = -ENOPROTOOPT;
951 }
952 release_sock(sk);
953
954out_norel:
955 return err;
956}
957
958static int pep_getsockopt(struct sock *sk, int level, int optname,
959 char __user *optval, int __user *optlen)
960{
961 struct pep_sock *pn = pep_sk(sk);
962 int len, val;
963
964 if (level != SOL_PNPIPE)
965 return -ENOPROTOOPT;
966 if (get_user(len, optlen))
967 return -EFAULT;
968
969 switch (optname) {
970 case PNPIPE_ENCAP:
971 val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE;
972 break;
Kumar Sanghvi8d98efa2010-09-26 19:07:59 +0000973
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700974 case PNPIPE_IFINDEX:
975 val = pn->ifindex;
976 break;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000977
Rémi Denis-Courmontacaf7df2011-03-08 22:44:11 +0000978 case PNPIPE_HANDLE:
979 val = pn->pipe_handle;
980 if (val == PN_PIPE_INVALID_HANDLE)
981 return -EINVAL;
982 break;
983
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000984#ifdef CONFIG_PHONET_PIPECTRLR
985 case PNPIPE_ENABLE:
Rémi Denis-Courmont2feb6182011-02-24 23:14:59 +0000986 val = sk->sk_state == TCP_ESTABLISHED;
Rémi Denis-Courmont03789f22010-10-08 04:02:02 +0000987 break;
988#endif
989
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700990 default:
991 return -ENOPROTOOPT;
992 }
993
994 len = min_t(unsigned int, sizeof(int), len);
995 if (put_user(len, optlen))
996 return -EFAULT;
997 if (put_user(val, (int __user *) optval))
998 return -EFAULT;
999 return 0;
1000}
1001
1002static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1003{
1004 struct pep_sock *pn = pep_sk(sk);
1005 struct pnpipehdr *ph;
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001006 int err;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001007
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001008 if (pn_flow_safe(pn->tx_fc) &&
1009 !atomic_add_unless(&pn->tx_credits, -1, 0)) {
1010 kfree_skb(skb);
1011 return -ENOBUFS;
1012 }
1013
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001014 skb_push(skb, 3 + pn->aligned);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001015 skb_reset_transport_header(skb);
1016 ph = pnp_hdr(skb);
1017 ph->utid = 0;
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001018 if (pn->aligned) {
1019 ph->message_id = PNS_PIPE_ALIGNED_DATA;
1020 ph->data[0] = 0; /* padding */
1021 } else
1022 ph->message_id = PNS_PIPE_DATA;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001023 ph->pipe_handle = pn->pipe_handle;
Rémi Denis-Courmont14ba8fa2011-02-24 23:14:58 +00001024 err = pn_skb_send(sk, skb, NULL);
Rémi Denis-Courmonte1a59642010-09-29 22:33:50 +00001025
1026 if (err && pn_flow_safe(pn->tx_fc))
1027 atomic_inc(&pn->tx_credits);
1028 return err;
1029
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001030}
1031
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001032static int pep_sendmsg(struct kiocb *iocb, struct sock *sk,
1033 struct msghdr *msg, size_t len)
1034{
1035 struct pep_sock *pn = pep_sk(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001036 struct sk_buff *skb;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001037 long timeo;
1038 int flags = msg->msg_flags;
1039 int err, done;
1040
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001041 if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
1042 MSG_CMSG_COMPAT)) ||
1043 !(msg->msg_flags & MSG_EOR))
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001044 return -EOPNOTSUPP;
1045
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001046 skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len,
1047 flags & MSG_DONTWAIT, &err);
1048 if (!skb)
Rémi Denis-Courmont02ac3262010-08-30 12:57:04 +00001049 return err;
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001050
1051 skb_reserve(skb, MAX_PHONET_HEADER + 3);
1052 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1053 if (err < 0)
1054 goto outfree;
1055
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001056 lock_sock(sk);
1057 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1058 if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) {
1059 err = -ENOTCONN;
1060 goto out;
1061 }
1062 if (sk->sk_state != TCP_ESTABLISHED) {
1063 /* Wait until the pipe gets to enabled state */
1064disabled:
1065 err = sk_stream_wait_connect(sk, &timeo);
1066 if (err)
1067 goto out;
1068
1069 if (sk->sk_state == TCP_CLOSE_WAIT) {
1070 err = -ECONNRESET;
1071 goto out;
1072 }
1073 }
1074 BUG_ON(sk->sk_state != TCP_ESTABLISHED);
1075
1076 /* Wait until flow control allows TX */
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001077 done = atomic_read(&pn->tx_credits);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001078 while (!done) {
1079 DEFINE_WAIT(wait);
1080
1081 if (!timeo) {
1082 err = -EAGAIN;
1083 goto out;
1084 }
1085 if (signal_pending(current)) {
1086 err = sock_intr_errno(timeo);
1087 goto out;
1088 }
1089
Eric Dumazet43815482010-04-29 11:01:49 +00001090 prepare_to_wait(sk_sleep(sk), &wait,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001091 TASK_INTERRUPTIBLE);
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001092 done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits));
Eric Dumazet43815482010-04-29 11:01:49 +00001093 finish_wait(sk_sleep(sk), &wait);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001094
1095 if (sk->sk_state != TCP_ESTABLISHED)
1096 goto disabled;
1097 }
1098
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001099 err = pipe_skb_send(sk, skb);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001100 if (err >= 0)
1101 err = len; /* success! */
1102 skb = NULL;
1103out:
1104 release_sock(sk);
Rémi Denis-Courmontb1704372009-11-09 04:06:40 +00001105outfree:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001106 kfree_skb(skb);
1107 return err;
1108}
1109
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001110int pep_writeable(struct sock *sk)
1111{
1112 struct pep_sock *pn = pep_sk(sk);
1113
Rémi Denis-Courmontbe677732008-12-17 15:48:31 -08001114 return atomic_read(&pn->tx_credits);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001115}
1116
1117int pep_write(struct sock *sk, struct sk_buff *skb)
1118{
1119 struct sk_buff *rskb, *fs;
1120 int flen = 0;
1121
Rémi Denis-Courmontfea93ec2010-01-04 02:02:48 +00001122 if (pep_sk(sk)->aligned)
1123 return pipe_skb_send(sk, skb);
1124
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001125 rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC);
1126 if (!rskb) {
1127 kfree_skb(skb);
1128 return -ENOMEM;
1129 }
1130 skb_shinfo(rskb)->frag_list = skb;
1131 rskb->len += skb->len;
1132 rskb->data_len += rskb->len;
1133 rskb->truesize += rskb->len;
1134
1135 /* Avoid nested fragments */
David S. Miller5c313e92009-06-09 00:21:58 -07001136 skb_walk_frags(skb, fs)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001137 flen += fs->len;
1138 skb->next = skb_shinfo(skb)->frag_list;
David S. Miller5c313e92009-06-09 00:21:58 -07001139 skb_frag_list_init(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001140 skb->len -= flen;
1141 skb->data_len -= flen;
1142 skb->truesize -= flen;
1143
1144 skb_reserve(rskb, MAX_PHONET_HEADER + 3);
1145 return pipe_skb_send(sk, rskb);
1146}
1147
1148struct sk_buff *pep_read(struct sock *sk)
1149{
1150 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1151
1152 if (sk->sk_state == TCP_ESTABLISHED)
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +00001153 pipe_grant_credits(sk, GFP_ATOMIC);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001154 return skb;
1155}
1156
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001157static int pep_recvmsg(struct kiocb *iocb, struct sock *sk,
1158 struct msghdr *msg, size_t len, int noblock,
1159 int flags, int *addr_len)
1160{
1161 struct sk_buff *skb;
1162 int err;
1163
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001164 if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL|
1165 MSG_NOSIGNAL|MSG_CMSG_COMPAT))
1166 return -EOPNOTSUPP;
1167
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001168 if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE)))
1169 return -ENOTCONN;
1170
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001171 if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) {
1172 /* Dequeue and acknowledge control request */
1173 struct pep_sock *pn = pep_sk(sk);
1174
Rémi Denis-Courmont82ecbcb2010-01-04 02:02:49 +00001175 if (flags & MSG_PEEK)
1176 return -EOPNOTSUPP;
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001177 skb = skb_dequeue(&pn->ctrlreq_queue);
1178 if (skb) {
1179 pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR,
1180 GFP_KERNEL);
1181 msg->msg_flags |= MSG_OOB;
1182 goto copy;
1183 }
1184 if (flags & MSG_OOB)
1185 return -EINVAL;
1186 }
1187
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001188 skb = skb_recv_datagram(sk, flags, noblock, &err);
1189 lock_sock(sk);
1190 if (skb == NULL) {
1191 if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT)
1192 err = -ECONNRESET;
1193 release_sock(sk);
1194 return err;
1195 }
1196
1197 if (sk->sk_state == TCP_ESTABLISHED)
Rémi Denis-Courmont44c9ab12011-03-08 22:44:09 +00001198 pipe_grant_credits(sk, GFP_KERNEL);
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001199 release_sock(sk);
Rémi Denis-Courmontc41bd97f82008-10-05 11:15:43 -07001200copy:
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001201 msg->msg_flags |= MSG_EOR;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001202 if (skb->len > len)
1203 msg->msg_flags |= MSG_TRUNC;
1204 else
1205 len = skb->len;
1206
1207 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len);
1208 if (!err)
1209 err = (flags & MSG_TRUNC) ? skb->len : len;
1210
1211 skb_free_datagram(sk, skb);
1212 return err;
1213}
1214
1215static void pep_sock_unhash(struct sock *sk)
1216{
1217 struct pep_sock *pn = pep_sk(sk);
1218 struct sock *skparent = NULL;
1219
1220 lock_sock(sk);
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001221
1222#ifndef CONFIG_PHONET_PIPECTRLR
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001223 if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) {
1224 skparent = pn->listener;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001225 release_sock(sk);
1226
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001227 pn = pep_sk(skparent);
Rémi Denis-Courmont7dfde172010-05-26 00:44:44 +00001228 lock_sock(skparent);
1229 sk_del_node_init(sk);
1230 sk = skparent;
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001231 }
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001232#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001233 /* Unhash a listening sock only when it is closed
1234 * and all of its active connected pipes are closed. */
1235 if (hlist_empty(&pn->hlist))
1236 pn_sock_unhash(&pn->pn_sk.sk);
1237 release_sock(sk);
1238
1239 if (skparent)
1240 sock_put(skparent);
1241}
1242
1243static struct proto pep_proto = {
1244 .close = pep_sock_close,
1245 .accept = pep_sock_accept,
Kumar Sanghvib3d62552010-10-12 20:14:43 +00001246#ifdef CONFIG_PHONET_PIPECTRLR
1247 .connect = pep_sock_connect,
1248#endif
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001249 .ioctl = pep_ioctl,
1250 .init = pep_init,
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001251 .setsockopt = pep_setsockopt,
1252 .getsockopt = pep_getsockopt,
Rémi Denis-Courmont96414582008-10-05 11:15:13 -07001253 .sendmsg = pep_sendmsg,
1254 .recvmsg = pep_recvmsg,
1255 .backlog_rcv = pep_do_rcv,
1256 .hash = pn_sock_hash,
1257 .unhash = pep_sock_unhash,
1258 .get_port = pn_sock_get_port,
1259 .obj_size = sizeof(struct pep_sock),
1260 .owner = THIS_MODULE,
1261 .name = "PNPIPE",
1262};
1263
1264static struct phonet_protocol pep_pn_proto = {
1265 .ops = &phonet_stream_ops,
1266 .prot = &pep_proto,
1267 .sock_type = SOCK_SEQPACKET,
1268};
1269
1270static int __init pep_register(void)
1271{
1272 return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto);
1273}
1274
1275static void __exit pep_unregister(void)
1276{
1277 phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto);
1278}
1279
1280module_init(pep_register);
1281module_exit(pep_unregister);
1282MODULE_AUTHOR("Remi Denis-Courmont, Nokia");
1283MODULE_DESCRIPTION("Phonet pipe protocol");
1284MODULE_LICENSE("GPL");
1285MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE);