Commit | Line | Data |
---|---|---|
9641458d RDC |
1 | /* |
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> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
9641458d RDC |
27 | #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> | |
02a47617 | 35 | #include <net/phonet/gprs.h> |
9641458d RDC |
36 | |
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 | ||
9641458d RDC |
53 | #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */ |
54 | ||
55 | /* Get the next TLV sub-block. */ | |
56 | static 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 | ||
44c9ab16 RDC |
80 | static 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 | ||
96 | static int pep_reply(struct sock *sk, struct sk_buff *oskb, u8 code, | |
97 | const void *data, int len, gfp_t priority) | |
9641458d RDC |
98 | { |
99 | const struct pnpipehdr *oph = pnp_hdr(oskb); | |
100 | struct pnpipehdr *ph; | |
101 | struct sk_buff *skb; | |
14ba8fae | 102 | struct sockaddr_pn peer; |
9641458d | 103 | |
44c9ab16 | 104 | skb = pep_alloc_skb(sk, data, len, priority); |
9641458d RDC |
105 | if (!skb) |
106 | return -ENOMEM; | |
9641458d | 107 | |
9641458d RDC |
108 | 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 | ||
14ba8fae RDC |
114 | pn_skb_get_src_sockaddr(oskb, &peer); |
115 | return pn_skb_send(sk, skb, &peer); | |
9641458d RDC |
116 | } |
117 | ||
44c9ab16 RDC |
118 | static int pep_indicate(struct sock *sk, u8 id, u8 code, |
119 | const void *data, int len, gfp_t priority) | |
8d98efa8 | 120 | { |
44c9ab16 | 121 | struct pep_sock *pn = pep_sk(sk); |
8d98efa8 KS |
122 | struct pnpipehdr *ph; |
123 | struct sk_buff *skb; | |
8d98efa8 | 124 | |
44c9ab16 | 125 | skb = pep_alloc_skb(sk, data, len, priority); |
8d98efa8 KS |
126 | if (!skb) |
127 | return -ENOMEM; | |
8d98efa8 | 128 | |
8d98efa8 | 129 | ph = pnp_hdr(skb); |
44c9ab16 RDC |
130 | ph->utid = 0; |
131 | ph->message_id = id; | |
b3d62553 | 132 | ph->pipe_handle = pn->pipe_handle; |
44c9ab16 | 133 | ph->data[0] = code; |
14ba8fae | 134 | return pn_skb_send(sk, skb, NULL); |
8d98efa8 KS |
135 | } |
136 | ||
44c9ab16 RDC |
137 | #define PAD 0x00 |
138 | ||
139 | #ifdef CONFIG_PHONET_PIPECTRLR | |
140 | static int pipe_handler_request(struct sock *sk, u8 id, u8 code, | |
141 | const void *data, int len) | |
8d98efa8 | 142 | { |
44c9ab16 | 143 | struct pep_sock *pn = pep_sk(sk); |
8d98efa8 KS |
144 | struct pnpipehdr *ph; |
145 | struct sk_buff *skb; | |
8d98efa8 | 146 | |
44c9ab16 | 147 | skb = pep_alloc_skb(sk, data, len, GFP_KERNEL); |
8d98efa8 KS |
148 | if (!skb) |
149 | return -ENOMEM; | |
8d98efa8 | 150 | |
8d98efa8 | 151 | ph = pnp_hdr(skb); |
44c9ab16 RDC |
152 | ph->utid = id; /* whatever */ |
153 | ph->message_id = id; | |
b3d62553 | 154 | ph->pipe_handle = pn->pipe_handle; |
44c9ab16 | 155 | ph->data[0] = code; |
14ba8fae | 156 | return pn_skb_send(sk, skb, NULL); |
8d98efa8 KS |
157 | } |
158 | ||
44c9ab16 | 159 | static int pipe_handler_send_created_ind(struct sock *sk) |
8d98efa8 | 160 | { |
b3d62553 | 161 | struct pep_sock *pn = pep_sk(sk); |
44c9ab16 RDC |
162 | u8 data[4] = { |
163 | PN_PIPE_SB_NEGOTIATED_FC, pep_sb_size(2), | |
164 | pn->tx_fc, pn->rx_fc, | |
165 | }; | |
8d98efa8 | 166 | |
44c9ab16 RDC |
167 | return pep_indicate(sk, PNS_PIPE_CREATED_IND, 1 /* sub-blocks */, |
168 | data, 4, GFP_ATOMIC); | |
169 | } | |
8d98efa8 | 170 | |
44c9ab16 RDC |
171 | static int pipe_handler_send_ind(struct sock *sk, u8 id) |
172 | { | |
173 | return pep_indicate(sk, id, PAD, NULL, 0, GFP_ATOMIC); | |
8d98efa8 KS |
174 | } |
175 | ||
03789f26 | 176 | static int pipe_handler_enable_pipe(struct sock *sk, int enable) |
8d98efa8 | 177 | { |
0165d69b RDC |
178 | u8 id = enable ? PNS_PEP_ENABLE_REQ : PNS_PEP_DISABLE_REQ; |
179 | ||
44c9ab16 | 180 | return pipe_handler_request(sk, id, PAD, NULL, 0); |
8d98efa8 KS |
181 | } |
182 | #endif | |
183 | ||
9641458d RDC |
184 | static 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 | ||
205 | static 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. */ | |
c41bd97f RDC |
214 | static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code, |
215 | gfp_t priority) | |
9641458d RDC |
216 | { |
217 | const struct pnpipehdr *oph = pnp_hdr(oskb); | |
218 | struct sk_buff *skb; | |
219 | struct pnpipehdr *ph; | |
220 | struct sockaddr_pn dst; | |
44c9ab16 RDC |
221 | u8 data[4] = { |
222 | oph->data[0], /* PEP type */ | |
223 | code, /* error code, at an unusual offset */ | |
224 | PAD, PAD, | |
225 | }; | |
9641458d | 226 | |
44c9ab16 | 227 | skb = pep_alloc_skb(sk, data, 4, priority); |
9641458d RDC |
228 | if (!skb) |
229 | return -ENOMEM; | |
9641458d | 230 | |
44c9ab16 | 231 | ph = pnp_hdr(skb); |
9641458d RDC |
232 | 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 */ | |
9641458d RDC |
236 | |
237 | pn_skb_get_src_sockaddr(oskb, &dst); | |
238 | return pn_skb_send(sk, skb, &dst); | |
239 | } | |
240 | ||
241 | static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority) | |
242 | { | |
44c9ab16 | 243 | u8 data[4] = { type, PAD, PAD, status }; |
9641458d | 244 | |
44c9ab16 RDC |
245 | return pep_indicate(sk, PNS_PEP_STATUS_IND, PN_PEP_TYPE_COMMON, |
246 | data, 4, priority); | |
9641458d RDC |
247 | } |
248 | ||
249 | /* Send our RX flow control information to the sender. | |
250 | * Socket must be locked. */ | |
44c9ab16 | 251 | static void pipe_grant_credits(struct sock *sk, gfp_t priority) |
9641458d RDC |
252 | { |
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: | |
44c9ab16 RDC |
261 | if (pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL, |
262 | PEP_IND_READY, priority) == 0) | |
263 | pn->rx_credits = 1; | |
9641458d RDC |
264 | 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, | |
44c9ab16 | 270 | priority) == 0) |
9641458d RDC |
271 | pn->rx_credits = CREDITS_MAX; |
272 | break; | |
273 | } | |
274 | } | |
275 | ||
276 | static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb) | |
277 | { | |
278 | struct pep_sock *pn = pep_sk(sk); | |
a91e7d47 | 279 | struct pnpipehdr *hdr; |
be677730 | 280 | int wake = 0; |
9641458d RDC |
281 | |
282 | if (!pskb_may_pull(skb, sizeof(*hdr) + 4)) | |
283 | return -EINVAL; | |
284 | ||
a91e7d47 | 285 | hdr = pnp_hdr(skb); |
9641458d RDC |
286 | 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: | |
be677730 | 298 | atomic_set(&pn->tx_credits, 0); |
9641458d RDC |
299 | break; |
300 | case PEP_IND_READY: | |
be677730 | 301 | atomic_set(&pn->tx_credits, wake = 1); |
9641458d RDC |
302 | break; |
303 | } | |
304 | break; | |
305 | case PN_ONE_CREDIT_FLOW_CONTROL: | |
306 | if (hdr->data[4] == PEP_IND_READY) | |
be677730 | 307 | atomic_set(&pn->tx_credits, wake = 1); |
9641458d RDC |
308 | 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; | |
be677730 | 315 | atomic_add(wake = hdr->data[4], &pn->tx_credits); |
9641458d RDC |
316 | break; |
317 | ||
318 | default: | |
319 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n", | |
320 | (unsigned)hdr->data[1]); | |
321 | return -EOPNOTSUPP; | |
322 | } | |
be677730 | 323 | if (wake) |
9641458d RDC |
324 | sk->sk_write_space(sk); |
325 | return 0; | |
326 | } | |
327 | ||
328 | static 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. */ | |
357 | static 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); | |
c41bd97f | 361 | struct sk_buff_head *queue; |
9641458d RDC |
362 | 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 | ||
8d98efa8 KS |
378 | #ifdef CONFIG_PHONET_PIPECTRLR |
379 | case PNS_PEP_DISCONNECT_RESP: | |
b3d62553 | 380 | sk->sk_state = TCP_CLOSE; |
8d98efa8 KS |
381 | break; |
382 | #endif | |
383 | ||
9641458d RDC |
384 | 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 | ||
8d98efa8 KS |
389 | #ifdef CONFIG_PHONET_PIPECTRLR |
390 | case PNS_PEP_ENABLE_RESP: | |
0165d69b | 391 | pipe_handler_send_ind(sk, PNS_PIPE_ENABLED_IND); |
8d98efa8 | 392 | |
b3d62553 KS |
393 | 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; | |
44c9ab16 | 400 | pipe_grant_credits(sk, GFP_ATOMIC); |
8d98efa8 KS |
401 | break; |
402 | #endif | |
403 | ||
9641458d RDC |
404 | 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: | |
be677730 | 418 | atomic_set(&pn->tx_credits, 0); |
9641458d RDC |
419 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
420 | break; | |
421 | ||
8d98efa8 KS |
422 | #ifdef CONFIG_PHONET_PIPECTRLR |
423 | case PNS_PEP_DISABLE_RESP: | |
b3d62553 | 424 | atomic_set(&pn->tx_credits, 0); |
0165d69b | 425 | pipe_handler_send_ind(sk, PNS_PIPE_DISABLED_IND); |
b3d62553 KS |
426 | sk->sk_state = TCP_SYN_RECV; |
427 | pn->rx_credits = 0; | |
8d98efa8 KS |
428 | break; |
429 | #endif | |
430 | ||
9641458d | 431 | case PNS_PEP_CTRL_REQ: |
2e2fb4b3 RDC |
432 | if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) { |
433 | atomic_inc(&sk->sk_drops); | |
c41bd97f | 434 | break; |
2e2fb4b3 | 435 | } |
c41bd97f RDC |
436 | __skb_pull(skb, 4); |
437 | queue = &pn->ctrlreq_queue; | |
438 | goto queue; | |
9641458d | 439 | |
fc6a1107 RDC |
440 | case PNS_PIPE_ALIGNED_DATA: |
441 | __skb_pull(skb, 1); | |
442 | /* fall through */ | |
9641458d RDC |
443 | 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) | |
0ebbf318 RDC |
448 | return NET_RX_SUCCESS; |
449 | err = -ENOBUFS; | |
9641458d RDC |
450 | break; |
451 | } | |
452 | ||
453 | if (pn->rx_credits == 0) { | |
2e2fb4b3 | 454 | atomic_inc(&sk->sk_drops); |
9641458d RDC |
455 | err = -ENOBUFS; |
456 | break; | |
457 | } | |
458 | pn->rx_credits--; | |
c41bd97f RDC |
459 | queue = &sk->sk_receive_queue; |
460 | goto queue; | |
9641458d RDC |
461 | |
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)) { | |
be677730 | 481 | atomic_set(&pn->tx_credits, 1); |
9641458d RDC |
482 | sk->sk_write_space(sk); |
483 | } | |
484 | if (sk->sk_state == TCP_ESTABLISHED) | |
485 | break; /* Nothing to do */ | |
486 | sk->sk_state = TCP_ESTABLISHED; | |
44c9ab16 | 487 | pipe_grant_credits(sk, GFP_ATOMIC); |
9641458d RDC |
488 | 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 | } | |
500 | out: | |
501 | kfree_skb(skb); | |
0ebbf318 | 502 | return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS; |
c41bd97f RDC |
503 | |
504 | queue: | |
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); | |
0ebbf318 | 511 | return NET_RX_SUCCESS; |
9641458d RDC |
512 | } |
513 | ||
514 | /* Destroy connected sock. */ | |
515 | static void pipe_destruct(struct sock *sk) | |
516 | { | |
c41bd97f RDC |
517 | struct pep_sock *pn = pep_sk(sk); |
518 | ||
9641458d | 519 | skb_queue_purge(&sk->sk_receive_queue); |
c41bd97f | 520 | skb_queue_purge(&pn->ctrlreq_queue); |
9641458d RDC |
521 | } |
522 | ||
b3d62553 | 523 | #ifdef CONFIG_PHONET_PIPECTRLR |
8f44fcc7 RDC |
524 | static 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 | ||
b3d62553 KS |
538 | static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb) |
539 | { | |
540 | struct pep_sock *pn = pep_sk(sk); | |
8f44fcc7 RDC |
541 | struct pnpipehdr *hdr; |
542 | u8 n_sb; | |
543 | ||
544 | 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 | } | |
b3d62553 | 574 | |
b3d62553 KS |
575 | 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; | |
b3d62553 KS |
579 | sk->sk_state_change(sk); |
580 | ||
44c9ab16 | 581 | return pipe_handler_send_created_ind(sk); |
b3d62553 KS |
582 | } |
583 | #endif | |
584 | ||
9641458d RDC |
585 | static 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; | |
14ba8fae | 590 | struct sockaddr_pn dst, src; |
9641458d RDC |
591 | u16 peer_type; |
592 | u8 pipe_handle, enabled, n_sb; | |
fea93ece | 593 | u8 aligned = 0; |
9641458d RDC |
594 | |
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 | ||
9641458d RDC |
613 | /* 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; | |
fea93ece RDC |
627 | case PN_PIPE_SB_ALIGNED_DATA: |
628 | aligned = data[0] != 0; | |
629 | break; | |
9641458d RDC |
630 | } |
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); | |
14ba8fae | 652 | pn_skb_get_src_sockaddr(skb, &src); |
9641458d | 653 | newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst); |
14ba8fae RDC |
654 | newpn->pn_sk.dobject = pn_sockaddr_get_object(&src); |
655 | newpn->pn_sk.resource = pn_sockaddr_get_resource(&dst); | |
c41bd97f | 656 | skb_queue_head_init(&newpn->ctrlreq_queue); |
9641458d | 657 | newpn->pipe_handle = pipe_handle; |
be677730 | 658 | atomic_set(&newpn->tx_credits, 0); |
9641458d | 659 | newpn->peer_type = peer_type; |
be677730 | 660 | newpn->rx_credits = 0; |
9641458d RDC |
661 | newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL; |
662 | newpn->init_enable = enabled; | |
fea93ece | 663 | newpn->aligned = aligned; |
9641458d RDC |
664 | |
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 */ | |
676 | static 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 | */ | |
706 | static int pep_do_rcv(struct sock *sk, struct sk_buff *skb) | |
707 | { | |
708 | struct pep_sock *pn = pep_sk(sk); | |
709 | struct sock *sknode; | |
2ddc1ac1 | 710 | struct pnpipehdr *hdr; |
9641458d | 711 | struct sockaddr_pn dst; |
9641458d RDC |
712 | 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"); | |
9641458d RDC |
735 | goto drop; |
736 | } | |
737 | ||
738 | switch (hdr->message_id) { | |
739 | case PNS_PEP_CONNECT_REQ: | |
0ebbf318 RDC |
740 | 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); | |
9641458d RDC |
744 | break; |
745 | ||
b3d62553 KS |
746 | #ifdef CONFIG_PHONET_PIPECTRLR |
747 | case PNS_PEP_CONNECT_RESP: | |
0ebbf318 | 748 | pep_connresp_rcv(sk, skb); |
b3d62553 KS |
749 | break; |
750 | #endif | |
751 | ||
9641458d RDC |
752 | 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: | |
c41bd97f | 757 | pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC); |
9641458d RDC |
758 | 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: | |
0ebbf318 | 765 | break; |
9641458d RDC |
766 | } |
767 | drop: | |
768 | kfree_skb(skb); | |
0ebbf318 | 769 | return NET_RX_SUCCESS; |
9641458d RDC |
770 | } |
771 | ||
00497135 | 772 | #ifndef CONFIG_PHONET_PIPECTRLR |
6482f554 RDC |
773 | static 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 | ||
44c9ab16 | 779 | skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL); |
6482f554 RDC |
780 | if (!skb) |
781 | return -ENOMEM; | |
782 | ||
6482f554 RDC |
783 | 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; | |
14ba8fae | 788 | return pn_skb_send(sk, skb, NULL); |
6482f554 | 789 | } |
00497135 | 790 | #endif |
6482f554 | 791 | |
9641458d RDC |
792 | /* associated socket ceases to exist */ |
793 | static void pep_sock_close(struct sock *sk, long timeout) | |
794 | { | |
795 | struct pep_sock *pn = pep_sk(sk); | |
02a47617 | 796 | int ifindex = 0; |
9641458d | 797 | |
e513480e | 798 | sock_hold(sk); /* keep a reference after sk_common_release() */ |
9641458d RDC |
799 | 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; | |
2feb6181 RDC |
810 | } else if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) { |
811 | #ifndef CONFIG_PHONET_PIPECTRLR | |
6482f554 RDC |
812 | /* Forcefully remove dangling Phonet pipe */ |
813 | pipe_do_remove(sk); | |
2feb6181 | 814 | #else |
b3d62553 | 815 | /* send pep disconnect request */ |
44c9ab16 | 816 | pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD, NULL, 0); |
b3d62553 | 817 | sk->sk_state = TCP_CLOSE; |
b3d62553 | 818 | #endif |
2feb6181 | 819 | } |
b3d62553 | 820 | |
02a47617 RDC |
821 | ifindex = pn->ifindex; |
822 | pn->ifindex = 0; | |
9641458d | 823 | release_sock(sk); |
02a47617 RDC |
824 | |
825 | if (ifindex) | |
826 | gprs_detach(sk); | |
e513480e | 827 | sock_put(sk); |
9641458d RDC |
828 | } |
829 | ||
830 | static 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 | ||
43815482 | 848 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
9641458d RDC |
849 | TASK_INTERRUPTIBLE); |
850 | release_sock(sk); | |
851 | timeo = schedule_timeout(timeo); | |
852 | lock_sock(sk); | |
43815482 | 853 | finish_wait(sk_sleep(sk), &wait); |
9641458d RDC |
854 | } |
855 | ||
856 | return 0; | |
857 | } | |
858 | ||
859 | static 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 | } | |
635f0815 | 880 | kfree_skb(oskb); |
9641458d RDC |
881 | |
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 | ||
891 | out: | |
892 | release_sock(sk); | |
893 | *errp = err; | |
894 | return newsk; | |
895 | } | |
896 | ||
b3d62553 KS |
897 | #ifdef CONFIG_PHONET_PIPECTRLR |
898 | static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len) | |
899 | { | |
900 | struct pep_sock *pn = pep_sk(sk); | |
14ba8fae | 901 | const struct sockaddr_pn *spn = (struct sockaddr_pn *)addr; |
44c9ab16 | 902 | u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD }; |
b3d62553 | 903 | |
14ba8fae RDC |
904 | pn->pn_sk.dobject = pn_sockaddr_get_object(spn); |
905 | pn->pn_sk.resource = pn_sockaddr_get_resource(spn); | |
44c9ab16 RDC |
906 | return pipe_handler_request(sk, PNS_PEP_CONNECT_REQ, |
907 | PN_PIPE_DISABLE, data, 4); | |
b3d62553 KS |
908 | } |
909 | #endif | |
910 | ||
9641458d RDC |
911 | static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg) |
912 | { | |
c41bd97f | 913 | struct pep_sock *pn = pep_sk(sk); |
9641458d RDC |
914 | int answ; |
915 | ||
916 | switch (cmd) { | |
917 | case SIOCINQ: | |
918 | if (sk->sk_state == TCP_LISTEN) | |
919 | return -EINVAL; | |
920 | ||
921 | lock_sock(sk); | |
f64f9e71 JP |
922 | if (sock_flag(sk, SOCK_URGINLINE) && |
923 | !skb_queue_empty(&pn->ctrlreq_queue)) | |
c41bd97f RDC |
924 | answ = skb_peek(&pn->ctrlreq_queue)->len; |
925 | else if (!skb_queue_empty(&sk->sk_receive_queue)) | |
9641458d RDC |
926 | 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 | ||
936 | static 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); | |
c41bd97f | 942 | skb_queue_head_init(&pn->ctrlreq_queue); |
9641458d RDC |
943 | pn->pipe_handle = PN_PIPE_INVALID_HANDLE; |
944 | return 0; | |
945 | } | |
946 | ||
02a47617 | 947 | static int pep_setsockopt(struct sock *sk, int level, int optname, |
b7058842 | 948 | char __user *optval, unsigned int optlen) |
02a47617 RDC |
949 | { |
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) { | |
8d98efa8 | 962 | #ifdef CONFIG_PHONET_PIPECTRLR |
b3d62553 | 963 | case PNPIPE_PIPE_HANDLE: |
8d98efa8 | 964 | if (val) { |
b3d62553 | 965 | pn->pipe_handle = val; |
8d98efa8 KS |
966 | break; |
967 | } | |
8d98efa8 KS |
968 | #endif |
969 | ||
02a47617 RDC |
970 | 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; | |
03789f26 RDC |
995 | |
996 | #ifdef CONFIG_PHONET_PIPECTRLR | |
997 | case PNPIPE_ENABLE: | |
2feb6181 | 998 | if ((1 << sk->sk_state) & ~(TCPF_SYN_RECV|TCPF_ESTABLISHED)) { |
03789f26 RDC |
999 | err = -ENOTCONN; |
1000 | break; | |
1001 | } | |
1002 | err = pipe_handler_enable_pipe(sk, val); | |
1003 | break; | |
1004 | #endif | |
1005 | ||
02a47617 RDC |
1006 | default: |
1007 | err = -ENOPROTOOPT; | |
1008 | } | |
1009 | release_sock(sk); | |
1010 | ||
1011 | out_norel: | |
1012 | return err; | |
1013 | } | |
1014 | ||
1015 | static 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; | |
8d98efa8 | 1030 | |
03789f26 RDC |
1031 | case PNPIPE_IFINDEX: |
1032 | val = pn->ifindex; | |
1033 | break; | |
1034 | ||
8d98efa8 | 1035 | #ifdef CONFIG_PHONET_PIPECTRLR |
03789f26 | 1036 | case PNPIPE_ENABLE: |
2feb6181 | 1037 | val = sk->sk_state == TCP_ESTABLISHED; |
8d98efa8 KS |
1038 | break; |
1039 | #endif | |
1040 | ||
02a47617 RDC |
1041 | 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 | ||
1053 | static int pipe_skb_send(struct sock *sk, struct sk_buff *skb) | |
1054 | { | |
1055 | struct pep_sock *pn = pep_sk(sk); | |
1056 | struct pnpipehdr *ph; | |
e1a5964f | 1057 | int err; |
02a47617 | 1058 | |
be677730 RDC |
1059 | 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 | ||
fea93ece | 1065 | skb_push(skb, 3 + pn->aligned); |
02a47617 RDC |
1066 | skb_reset_transport_header(skb); |
1067 | ph = pnp_hdr(skb); | |
1068 | ph->utid = 0; | |
fea93ece RDC |
1069 | 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; | |
02a47617 | 1074 | ph->pipe_handle = pn->pipe_handle; |
14ba8fae | 1075 | err = pn_skb_send(sk, skb, NULL); |
e1a5964f RDC |
1076 | |
1077 | if (err && pn_flow_safe(pn->tx_fc)) | |
1078 | atomic_inc(&pn->tx_credits); | |
1079 | return err; | |
1080 | ||
02a47617 RDC |
1081 | } |
1082 | ||
9641458d RDC |
1083 | static 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); | |
b1704374 | 1087 | struct sk_buff *skb; |
9641458d RDC |
1088 | long timeo; |
1089 | int flags = msg->msg_flags; | |
1090 | int err, done; | |
1091 | ||
82ecbcb9 RDC |
1092 | if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL| |
1093 | MSG_CMSG_COMPAT)) || | |
1094 | !(msg->msg_flags & MSG_EOR)) | |
9641458d RDC |
1095 | return -EOPNOTSUPP; |
1096 | ||
b1704374 RDC |
1097 | skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len, |
1098 | flags & MSG_DONTWAIT, &err); | |
1099 | if (!skb) | |
02ac3268 | 1100 | return err; |
b1704374 RDC |
1101 | |
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 | ||
9641458d RDC |
1107 | 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 */ | |
1115 | disabled: | |
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 */ | |
be677730 | 1128 | done = atomic_read(&pn->tx_credits); |
9641458d RDC |
1129 | 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 | ||
43815482 | 1141 | prepare_to_wait(sk_sleep(sk), &wait, |
9641458d | 1142 | TASK_INTERRUPTIBLE); |
be677730 | 1143 | done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits)); |
43815482 | 1144 | finish_wait(sk_sleep(sk), &wait); |
9641458d RDC |
1145 | |
1146 | if (sk->sk_state != TCP_ESTABLISHED) | |
1147 | goto disabled; | |
1148 | } | |
1149 | ||
02a47617 | 1150 | err = pipe_skb_send(sk, skb); |
9641458d RDC |
1151 | if (err >= 0) |
1152 | err = len; /* success! */ | |
1153 | skb = NULL; | |
1154 | out: | |
1155 | release_sock(sk); | |
b1704374 | 1156 | outfree: |
9641458d RDC |
1157 | kfree_skb(skb); |
1158 | return err; | |
1159 | } | |
1160 | ||
02a47617 RDC |
1161 | int pep_writeable(struct sock *sk) |
1162 | { | |
1163 | struct pep_sock *pn = pep_sk(sk); | |
1164 | ||
be677730 | 1165 | return atomic_read(&pn->tx_credits); |
02a47617 RDC |
1166 | } |
1167 | ||
1168 | int pep_write(struct sock *sk, struct sk_buff *skb) | |
1169 | { | |
1170 | struct sk_buff *rskb, *fs; | |
1171 | int flen = 0; | |
1172 | ||
fea93ece RDC |
1173 | if (pep_sk(sk)->aligned) |
1174 | return pipe_skb_send(sk, skb); | |
1175 | ||
02a47617 RDC |
1176 | 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 */ | |
5c313e9a | 1187 | skb_walk_frags(skb, fs) |
02a47617 RDC |
1188 | flen += fs->len; |
1189 | skb->next = skb_shinfo(skb)->frag_list; | |
5c313e9a | 1190 | skb_frag_list_init(skb); |
02a47617 RDC |
1191 | 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 | ||
1199 | struct 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) | |
44c9ab16 | 1204 | pipe_grant_credits(sk, GFP_ATOMIC); |
02a47617 RDC |
1205 | return skb; |
1206 | } | |
1207 | ||
9641458d RDC |
1208 | static 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 | ||
82ecbcb9 RDC |
1215 | if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL| |
1216 | MSG_NOSIGNAL|MSG_CMSG_COMPAT)) | |
1217 | return -EOPNOTSUPP; | |
1218 | ||
9641458d RDC |
1219 | if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE))) |
1220 | return -ENOTCONN; | |
1221 | ||
c41bd97f RDC |
1222 | if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) { |
1223 | /* Dequeue and acknowledge control request */ | |
1224 | struct pep_sock *pn = pep_sk(sk); | |
1225 | ||
82ecbcb9 RDC |
1226 | if (flags & MSG_PEEK) |
1227 | return -EOPNOTSUPP; | |
c41bd97f RDC |
1228 | 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 | ||
9641458d RDC |
1239 | 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) | |
44c9ab16 | 1249 | pipe_grant_credits(sk, GFP_KERNEL); |
9641458d | 1250 | release_sock(sk); |
c41bd97f | 1251 | copy: |
9641458d | 1252 | msg->msg_flags |= MSG_EOR; |
9641458d RDC |
1253 | 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 | ||
1266 | static 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); | |
b3d62553 KS |
1272 | |
1273 | #ifndef CONFIG_PHONET_PIPECTRLR | |
9641458d RDC |
1274 | if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) { |
1275 | skparent = pn->listener; | |
9641458d RDC |
1276 | release_sock(sk); |
1277 | ||
9641458d | 1278 | pn = pep_sk(skparent); |
7dfde179 RDC |
1279 | lock_sock(skparent); |
1280 | sk_del_node_init(sk); | |
1281 | sk = skparent; | |
9641458d | 1282 | } |
b3d62553 | 1283 | #endif |
9641458d RDC |
1284 | /* 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 | ||
1294 | static struct proto pep_proto = { | |
1295 | .close = pep_sock_close, | |
1296 | .accept = pep_sock_accept, | |
b3d62553 KS |
1297 | #ifdef CONFIG_PHONET_PIPECTRLR |
1298 | .connect = pep_sock_connect, | |
1299 | #endif | |
9641458d RDC |
1300 | .ioctl = pep_ioctl, |
1301 | .init = pep_init, | |
02a47617 RDC |
1302 | .setsockopt = pep_setsockopt, |
1303 | .getsockopt = pep_getsockopt, | |
9641458d RDC |
1304 | .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 | ||
1315 | static struct phonet_protocol pep_pn_proto = { | |
1316 | .ops = &phonet_stream_ops, | |
1317 | .prot = &pep_proto, | |
1318 | .sock_type = SOCK_SEQPACKET, | |
1319 | }; | |
1320 | ||
1321 | static int __init pep_register(void) | |
1322 | { | |
1323 | return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto); | |
1324 | } | |
1325 | ||
1326 | static void __exit pep_unregister(void) | |
1327 | { | |
1328 | phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto); | |
1329 | } | |
1330 | ||
1331 | module_init(pep_register); | |
1332 | module_exit(pep_unregister); | |
1333 | MODULE_AUTHOR("Remi Denis-Courmont, Nokia"); | |
1334 | MODULE_DESCRIPTION("Phonet pipe protocol"); | |
1335 | MODULE_LICENSE("GPL"); | |
1336 | MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE); |