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: | |
f7ae8d59 | 45 | * - sk_state, hlist: sock lock needed |
9641458d RDC |
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 | ||
f7ae8d59 RDC |
205 | static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code, |
206 | gfp_t priority) | |
9641458d RDC |
207 | { |
208 | static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ }; | |
209 | WARN_ON(code == PN_PIPE_NO_ERROR); | |
f7ae8d59 | 210 | return pep_reply(sk, skb, code, data, sizeof(data), priority); |
9641458d RDC |
211 | } |
212 | ||
213 | /* Control requests are not sent by the pipe service and have a specific | |
214 | * message format. */ | |
c41bd97f RDC |
215 | static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code, |
216 | gfp_t priority) | |
9641458d RDC |
217 | { |
218 | const struct pnpipehdr *oph = pnp_hdr(oskb); | |
219 | struct sk_buff *skb; | |
220 | struct pnpipehdr *ph; | |
221 | struct sockaddr_pn dst; | |
44c9ab16 RDC |
222 | u8 data[4] = { |
223 | oph->data[0], /* PEP type */ | |
224 | code, /* error code, at an unusual offset */ | |
225 | PAD, PAD, | |
226 | }; | |
9641458d | 227 | |
44c9ab16 | 228 | skb = pep_alloc_skb(sk, data, 4, priority); |
9641458d RDC |
229 | if (!skb) |
230 | return -ENOMEM; | |
9641458d | 231 | |
44c9ab16 | 232 | ph = pnp_hdr(skb); |
9641458d RDC |
233 | 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 */ | |
9641458d RDC |
237 | |
238 | pn_skb_get_src_sockaddr(oskb, &dst); | |
239 | return pn_skb_send(sk, skb, &dst); | |
240 | } | |
241 | ||
242 | static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority) | |
243 | { | |
44c9ab16 | 244 | u8 data[4] = { type, PAD, PAD, status }; |
9641458d | 245 | |
44c9ab16 RDC |
246 | return pep_indicate(sk, PNS_PEP_STATUS_IND, PN_PEP_TYPE_COMMON, |
247 | data, 4, priority); | |
9641458d RDC |
248 | } |
249 | ||
250 | /* Send our RX flow control information to the sender. | |
251 | * Socket must be locked. */ | |
44c9ab16 | 252 | static void pipe_grant_credits(struct sock *sk, gfp_t priority) |
9641458d RDC |
253 | { |
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: | |
44c9ab16 RDC |
262 | if (pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL, |
263 | PEP_IND_READY, priority) == 0) | |
264 | pn->rx_credits = 1; | |
9641458d RDC |
265 | 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, | |
44c9ab16 | 271 | priority) == 0) |
9641458d RDC |
272 | pn->rx_credits = CREDITS_MAX; |
273 | break; | |
274 | } | |
275 | } | |
276 | ||
277 | static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb) | |
278 | { | |
279 | struct pep_sock *pn = pep_sk(sk); | |
a91e7d47 | 280 | struct pnpipehdr *hdr; |
be677730 | 281 | int wake = 0; |
9641458d RDC |
282 | |
283 | if (!pskb_may_pull(skb, sizeof(*hdr) + 4)) | |
284 | return -EINVAL; | |
285 | ||
a91e7d47 | 286 | hdr = pnp_hdr(skb); |
9641458d RDC |
287 | 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: | |
be677730 | 299 | atomic_set(&pn->tx_credits, 0); |
9641458d RDC |
300 | break; |
301 | case PEP_IND_READY: | |
be677730 | 302 | atomic_set(&pn->tx_credits, wake = 1); |
9641458d RDC |
303 | break; |
304 | } | |
305 | break; | |
306 | case PN_ONE_CREDIT_FLOW_CONTROL: | |
307 | if (hdr->data[4] == PEP_IND_READY) | |
be677730 | 308 | atomic_set(&pn->tx_credits, wake = 1); |
9641458d RDC |
309 | 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; | |
be677730 | 316 | atomic_add(wake = hdr->data[4], &pn->tx_credits); |
9641458d RDC |
317 | break; |
318 | ||
319 | default: | |
320 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n", | |
321 | (unsigned)hdr->data[1]); | |
322 | return -EOPNOTSUPP; | |
323 | } | |
be677730 | 324 | if (wake) |
9641458d RDC |
325 | sk->sk_write_space(sk); |
326 | return 0; | |
327 | } | |
328 | ||
329 | static 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. */ | |
358 | static 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); | |
c41bd97f | 362 | struct sk_buff_head *queue; |
9641458d RDC |
363 | 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: | |
f7ae8d59 | 369 | pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_ATOMIC); |
9641458d RDC |
370 | 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 | ||
8d98efa8 KS |
379 | #ifdef CONFIG_PHONET_PIPECTRLR |
380 | case PNS_PEP_DISCONNECT_RESP: | |
b3d62553 | 381 | sk->sk_state = TCP_CLOSE; |
8d98efa8 KS |
382 | break; |
383 | #endif | |
384 | ||
9641458d RDC |
385 | 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 | ||
8d98efa8 KS |
390 | #ifdef CONFIG_PHONET_PIPECTRLR |
391 | case PNS_PEP_ENABLE_RESP: | |
0165d69b | 392 | pipe_handler_send_ind(sk, PNS_PIPE_ENABLED_IND); |
8d98efa8 | 393 | |
b3d62553 KS |
394 | 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; | |
44c9ab16 | 401 | pipe_grant_credits(sk, GFP_ATOMIC); |
8d98efa8 KS |
402 | break; |
403 | #endif | |
404 | ||
9641458d RDC |
405 | 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: | |
be677730 | 419 | atomic_set(&pn->tx_credits, 0); |
9641458d RDC |
420 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
421 | break; | |
422 | ||
8d98efa8 KS |
423 | #ifdef CONFIG_PHONET_PIPECTRLR |
424 | case PNS_PEP_DISABLE_RESP: | |
b3d62553 | 425 | atomic_set(&pn->tx_credits, 0); |
0165d69b | 426 | pipe_handler_send_ind(sk, PNS_PIPE_DISABLED_IND); |
b3d62553 KS |
427 | sk->sk_state = TCP_SYN_RECV; |
428 | pn->rx_credits = 0; | |
8d98efa8 KS |
429 | break; |
430 | #endif | |
431 | ||
9641458d | 432 | case PNS_PEP_CTRL_REQ: |
2e2fb4b3 RDC |
433 | if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) { |
434 | atomic_inc(&sk->sk_drops); | |
c41bd97f | 435 | break; |
2e2fb4b3 | 436 | } |
c41bd97f RDC |
437 | __skb_pull(skb, 4); |
438 | queue = &pn->ctrlreq_queue; | |
439 | goto queue; | |
9641458d | 440 | |
fc6a1107 RDC |
441 | case PNS_PIPE_ALIGNED_DATA: |
442 | __skb_pull(skb, 1); | |
443 | /* fall through */ | |
9641458d RDC |
444 | 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) | |
0ebbf318 RDC |
449 | return NET_RX_SUCCESS; |
450 | err = -ENOBUFS; | |
9641458d RDC |
451 | break; |
452 | } | |
453 | ||
454 | if (pn->rx_credits == 0) { | |
2e2fb4b3 | 455 | atomic_inc(&sk->sk_drops); |
9641458d RDC |
456 | err = -ENOBUFS; |
457 | break; | |
458 | } | |
459 | pn->rx_credits--; | |
c41bd97f RDC |
460 | queue = &sk->sk_receive_queue; |
461 | goto queue; | |
9641458d RDC |
462 | |
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)) { | |
be677730 | 482 | atomic_set(&pn->tx_credits, 1); |
9641458d RDC |
483 | sk->sk_write_space(sk); |
484 | } | |
485 | if (sk->sk_state == TCP_ESTABLISHED) | |
486 | break; /* Nothing to do */ | |
487 | sk->sk_state = TCP_ESTABLISHED; | |
44c9ab16 | 488 | pipe_grant_credits(sk, GFP_ATOMIC); |
9641458d RDC |
489 | 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 | } | |
501 | out: | |
502 | kfree_skb(skb); | |
0ebbf318 | 503 | return (err == -ENOBUFS) ? NET_RX_DROP : NET_RX_SUCCESS; |
c41bd97f RDC |
504 | |
505 | queue: | |
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); | |
0ebbf318 | 512 | return NET_RX_SUCCESS; |
9641458d RDC |
513 | } |
514 | ||
515 | /* Destroy connected sock. */ | |
516 | static void pipe_destruct(struct sock *sk) | |
517 | { | |
c41bd97f RDC |
518 | struct pep_sock *pn = pep_sk(sk); |
519 | ||
9641458d | 520 | skb_queue_purge(&sk->sk_receive_queue); |
c41bd97f | 521 | skb_queue_purge(&pn->ctrlreq_queue); |
9641458d RDC |
522 | } |
523 | ||
b3d62553 | 524 | #ifdef CONFIG_PHONET_PIPECTRLR |
8f44fcc7 RDC |
525 | static 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 | ||
b3d62553 KS |
539 | static int pep_connresp_rcv(struct sock *sk, struct sk_buff *skb) |
540 | { | |
541 | struct pep_sock *pn = pep_sk(sk); | |
8f44fcc7 RDC |
542 | struct pnpipehdr *hdr; |
543 | u8 n_sb; | |
544 | ||
545 | 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 | } | |
b3d62553 | 575 | |
b3d62553 KS |
576 | sk->sk_state = TCP_SYN_RECV; |
577 | sk->sk_backlog_rcv = pipe_do_rcv; | |
b3d62553 | 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 | /* Listening sock must be locked */ |
586 | static 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 | */ | |
616 | static int pep_do_rcv(struct sock *sk, struct sk_buff *skb) | |
617 | { | |
618 | struct pep_sock *pn = pep_sk(sk); | |
619 | struct sock *sknode; | |
2ddc1ac1 | 620 | struct pnpipehdr *hdr; |
9641458d | 621 | struct sockaddr_pn dst; |
9641458d RDC |
622 | 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 | ||
9641458d RDC |
639 | switch (hdr->message_id) { |
640 | case PNS_PEP_CONNECT_REQ: | |
f7ae8d59 RDC |
641 | 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; | |
9641458d | 651 | |
b3d62553 KS |
652 | #ifdef CONFIG_PHONET_PIPECTRLR |
653 | case PNS_PEP_CONNECT_RESP: | |
0ebbf318 | 654 | pep_connresp_rcv(sk, skb); |
b3d62553 KS |
655 | break; |
656 | #endif | |
657 | ||
9641458d RDC |
658 | 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: | |
c41bd97f | 663 | pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC); |
9641458d RDC |
664 | 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: | |
0ebbf318 | 671 | break; |
9641458d RDC |
672 | } |
673 | drop: | |
674 | kfree_skb(skb); | |
0ebbf318 | 675 | return NET_RX_SUCCESS; |
9641458d RDC |
676 | } |
677 | ||
00497135 | 678 | #ifndef CONFIG_PHONET_PIPECTRLR |
6482f554 RDC |
679 | static 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 | ||
44c9ab16 | 685 | skb = pep_alloc_skb(sk, NULL, 0, GFP_KERNEL); |
6482f554 RDC |
686 | if (!skb) |
687 | return -ENOMEM; | |
688 | ||
6482f554 RDC |
689 | 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; | |
14ba8fae | 694 | return pn_skb_send(sk, skb, NULL); |
6482f554 | 695 | } |
00497135 | 696 | #endif |
6482f554 | 697 | |
9641458d RDC |
698 | /* associated socket ceases to exist */ |
699 | static void pep_sock_close(struct sock *sk, long timeout) | |
700 | { | |
701 | struct pep_sock *pn = pep_sk(sk); | |
02a47617 | 702 | int ifindex = 0; |
9641458d | 703 | |
e513480e | 704 | sock_hold(sk); /* keep a reference after sk_common_release() */ |
9641458d RDC |
705 | sk_common_release(sk); |
706 | ||
707 | lock_sock(sk); | |
f7ae8d59 | 708 | if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) { |
2feb6181 | 709 | #ifndef CONFIG_PHONET_PIPECTRLR |
6482f554 RDC |
710 | /* Forcefully remove dangling Phonet pipe */ |
711 | pipe_do_remove(sk); | |
2feb6181 | 712 | #else |
b3d62553 | 713 | /* send pep disconnect request */ |
44c9ab16 | 714 | pipe_handler_request(sk, PNS_PEP_DISCONNECT_REQ, PAD, NULL, 0); |
b3d62553 | 715 | #endif |
2feb6181 | 716 | } |
f7ae8d59 | 717 | sk->sk_state = TCP_CLOSE; |
b3d62553 | 718 | |
02a47617 RDC |
719 | ifindex = pn->ifindex; |
720 | pn->ifindex = 0; | |
9641458d | 721 | release_sock(sk); |
02a47617 RDC |
722 | |
723 | if (ifindex) | |
724 | gprs_detach(sk); | |
e513480e | 725 | sock_put(sk); |
9641458d RDC |
726 | } |
727 | ||
f7ae8d59 | 728 | static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp) |
9641458d | 729 | { |
f7ae8d59 RDC |
730 | struct pep_sock *pn = pep_sk(sk), *newpn; |
731 | struct sock *newsk = NULL; | |
732 | struct sk_buff *skb; | |
733 | struct pnpipehdr *hdr; | |
734 | struct sockaddr_pn dst, src; | |
735 | int err; | |
736 | u16 peer_type; | |
737 | u8 pipe_handle, enabled, n_sb; | |
738 | u8 aligned = 0; | |
9641458d | 739 | |
f7ae8d59 RDC |
740 | skb = skb_recv_datagram(sk, 0, flags & O_NONBLOCK, errp); |
741 | if (!skb) | |
742 | return NULL; | |
9641458d | 743 | |
f7ae8d59 RDC |
744 | lock_sock(sk); |
745 | if (sk->sk_state != TCP_LISTEN) { | |
746 | err = -EINVAL; | |
747 | goto drop; | |
9641458d | 748 | } |
f7ae8d59 | 749 | sk_acceptq_removed(sk); |
9641458d | 750 | |
f7ae8d59 RDC |
751 | err = -EPROTO; |
752 | if (!pskb_may_pull(skb, sizeof(*hdr) + 4)) | |
753 | goto drop; | |
9641458d | 754 | |
f7ae8d59 RDC |
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; | |
9641458d | 770 | |
f7ae8d59 RDC |
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); | |
9641458d | 776 | |
f7ae8d59 RDC |
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 | } | |
9641458d | 791 | |
f7ae8d59 RDC |
792 | /* Check for duplicate pipe handle */ |
793 | newsk = pep_find_pipe(&pn->hlist, &dst, pipe_handle); | |
794 | if (unlikely(newsk)) { | |
795 | __sock_put(newsk); | |
9641458d | 796 | newsk = NULL; |
f7ae8d59 RDC |
797 | pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE, GFP_KERNEL); |
798 | goto drop; | |
9641458d RDC |
799 | } |
800 | ||
f7ae8d59 RDC |
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); | |
9641458d | 821 | sock_hold(sk); |
f7ae8d59 RDC |
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; | |
9641458d | 832 | |
f7ae8d59 RDC |
833 | err = pep_accept_conn(newsk, skb); |
834 | if (err) { | |
835 | sock_put(newsk); | |
836 | newsk = NULL; | |
837 | goto drop; | |
838 | } | |
9641458d | 839 | sk_add_node(newsk, &pn->hlist); |
f7ae8d59 | 840 | drop: |
9641458d | 841 | release_sock(sk); |
f7ae8d59 | 842 | kfree_skb(skb); |
9641458d RDC |
843 | *errp = err; |
844 | return newsk; | |
845 | } | |
846 | ||
b3d62553 KS |
847 | #ifdef CONFIG_PHONET_PIPECTRLR |
848 | static int pep_sock_connect(struct sock *sk, struct sockaddr *addr, int len) | |
849 | { | |
850 | struct pep_sock *pn = pep_sk(sk); | |
14ba8fae | 851 | const struct sockaddr_pn *spn = (struct sockaddr_pn *)addr; |
44c9ab16 | 852 | u8 data[4] = { 0 /* sub-blocks */, PAD, PAD, PAD }; |
b3d62553 | 853 | |
14ba8fae RDC |
854 | pn->pn_sk.dobject = pn_sockaddr_get_object(spn); |
855 | pn->pn_sk.resource = pn_sockaddr_get_resource(spn); | |
44c9ab16 RDC |
856 | return pipe_handler_request(sk, PNS_PEP_CONNECT_REQ, |
857 | PN_PIPE_DISABLE, data, 4); | |
b3d62553 KS |
858 | } |
859 | #endif | |
860 | ||
9641458d RDC |
861 | static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg) |
862 | { | |
c41bd97f | 863 | struct pep_sock *pn = pep_sk(sk); |
9641458d RDC |
864 | int answ; |
865 | ||
866 | switch (cmd) { | |
867 | case SIOCINQ: | |
868 | if (sk->sk_state == TCP_LISTEN) | |
869 | return -EINVAL; | |
870 | ||
871 | lock_sock(sk); | |
f64f9e71 JP |
872 | if (sock_flag(sk, SOCK_URGINLINE) && |
873 | !skb_queue_empty(&pn->ctrlreq_queue)) | |
c41bd97f RDC |
874 | answ = skb_peek(&pn->ctrlreq_queue)->len; |
875 | else if (!skb_queue_empty(&sk->sk_receive_queue)) | |
9641458d RDC |
876 | answ = skb_peek(&sk->sk_receive_queue)->len; |
877 | else | |
878 | answ = 0; | |
879 | release_sock(sk); | |
880 | return put_user(answ, (int __user *)arg); | |
881 | } | |
882 | ||
883 | return -ENOIOCTLCMD; | |
884 | } | |
885 | ||
886 | static int pep_init(struct sock *sk) | |
887 | { | |
888 | struct pep_sock *pn = pep_sk(sk); | |
889 | ||
f7ae8d59 | 890 | sk->sk_destruct = pipe_destruct; |
9641458d | 891 | INIT_HLIST_HEAD(&pn->hlist); |
c41bd97f | 892 | skb_queue_head_init(&pn->ctrlreq_queue); |
9641458d RDC |
893 | pn->pipe_handle = PN_PIPE_INVALID_HANDLE; |
894 | return 0; | |
895 | } | |
896 | ||
02a47617 | 897 | static int pep_setsockopt(struct sock *sk, int level, int optname, |
b7058842 | 898 | char __user *optval, unsigned int optlen) |
02a47617 RDC |
899 | { |
900 | struct pep_sock *pn = pep_sk(sk); | |
901 | int val = 0, err = 0; | |
902 | ||
903 | if (level != SOL_PNPIPE) | |
904 | return -ENOPROTOOPT; | |
905 | if (optlen >= sizeof(int)) { | |
906 | if (get_user(val, (int __user *) optval)) | |
907 | return -EFAULT; | |
908 | } | |
909 | ||
910 | lock_sock(sk); | |
911 | switch (optname) { | |
8d98efa8 | 912 | #ifdef CONFIG_PHONET_PIPECTRLR |
b3d62553 | 913 | case PNPIPE_PIPE_HANDLE: |
8d98efa8 | 914 | if (val) { |
b3d62553 | 915 | pn->pipe_handle = val; |
8d98efa8 KS |
916 | break; |
917 | } | |
8d98efa8 KS |
918 | #endif |
919 | ||
02a47617 RDC |
920 | case PNPIPE_ENCAP: |
921 | if (val && val != PNPIPE_ENCAP_IP) { | |
922 | err = -EINVAL; | |
923 | break; | |
924 | } | |
925 | if (!pn->ifindex == !val) | |
926 | break; /* Nothing to do! */ | |
927 | if (!capable(CAP_NET_ADMIN)) { | |
928 | err = -EPERM; | |
929 | break; | |
930 | } | |
931 | if (val) { | |
932 | release_sock(sk); | |
933 | err = gprs_attach(sk); | |
934 | if (err > 0) { | |
935 | pn->ifindex = err; | |
936 | err = 0; | |
937 | } | |
938 | } else { | |
939 | pn->ifindex = 0; | |
940 | release_sock(sk); | |
941 | gprs_detach(sk); | |
942 | err = 0; | |
943 | } | |
944 | goto out_norel; | |
03789f26 RDC |
945 | |
946 | #ifdef CONFIG_PHONET_PIPECTRLR | |
947 | case PNPIPE_ENABLE: | |
2feb6181 | 948 | if ((1 << sk->sk_state) & ~(TCPF_SYN_RECV|TCPF_ESTABLISHED)) { |
03789f26 RDC |
949 | err = -ENOTCONN; |
950 | break; | |
951 | } | |
952 | err = pipe_handler_enable_pipe(sk, val); | |
953 | break; | |
954 | #endif | |
955 | ||
02a47617 RDC |
956 | default: |
957 | err = -ENOPROTOOPT; | |
958 | } | |
959 | release_sock(sk); | |
960 | ||
961 | out_norel: | |
962 | return err; | |
963 | } | |
964 | ||
965 | static int pep_getsockopt(struct sock *sk, int level, int optname, | |
966 | char __user *optval, int __user *optlen) | |
967 | { | |
968 | struct pep_sock *pn = pep_sk(sk); | |
969 | int len, val; | |
970 | ||
971 | if (level != SOL_PNPIPE) | |
972 | return -ENOPROTOOPT; | |
973 | if (get_user(len, optlen)) | |
974 | return -EFAULT; | |
975 | ||
976 | switch (optname) { | |
977 | case PNPIPE_ENCAP: | |
978 | val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE; | |
979 | break; | |
8d98efa8 | 980 | |
03789f26 RDC |
981 | case PNPIPE_IFINDEX: |
982 | val = pn->ifindex; | |
983 | break; | |
984 | ||
8d98efa8 | 985 | #ifdef CONFIG_PHONET_PIPECTRLR |
03789f26 | 986 | case PNPIPE_ENABLE: |
2feb6181 | 987 | val = sk->sk_state == TCP_ESTABLISHED; |
8d98efa8 KS |
988 | break; |
989 | #endif | |
990 | ||
02a47617 RDC |
991 | default: |
992 | return -ENOPROTOOPT; | |
993 | } | |
994 | ||
995 | len = min_t(unsigned int, sizeof(int), len); | |
996 | if (put_user(len, optlen)) | |
997 | return -EFAULT; | |
998 | if (put_user(val, (int __user *) optval)) | |
999 | return -EFAULT; | |
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | static int pipe_skb_send(struct sock *sk, struct sk_buff *skb) | |
1004 | { | |
1005 | struct pep_sock *pn = pep_sk(sk); | |
1006 | struct pnpipehdr *ph; | |
e1a5964f | 1007 | int err; |
02a47617 | 1008 | |
be677730 RDC |
1009 | if (pn_flow_safe(pn->tx_fc) && |
1010 | !atomic_add_unless(&pn->tx_credits, -1, 0)) { | |
1011 | kfree_skb(skb); | |
1012 | return -ENOBUFS; | |
1013 | } | |
1014 | ||
fea93ece | 1015 | skb_push(skb, 3 + pn->aligned); |
02a47617 RDC |
1016 | skb_reset_transport_header(skb); |
1017 | ph = pnp_hdr(skb); | |
1018 | ph->utid = 0; | |
fea93ece RDC |
1019 | if (pn->aligned) { |
1020 | ph->message_id = PNS_PIPE_ALIGNED_DATA; | |
1021 | ph->data[0] = 0; /* padding */ | |
1022 | } else | |
1023 | ph->message_id = PNS_PIPE_DATA; | |
02a47617 | 1024 | ph->pipe_handle = pn->pipe_handle; |
14ba8fae | 1025 | err = pn_skb_send(sk, skb, NULL); |
e1a5964f RDC |
1026 | |
1027 | if (err && pn_flow_safe(pn->tx_fc)) | |
1028 | atomic_inc(&pn->tx_credits); | |
1029 | return err; | |
1030 | ||
02a47617 RDC |
1031 | } |
1032 | ||
9641458d RDC |
1033 | static int pep_sendmsg(struct kiocb *iocb, struct sock *sk, |
1034 | struct msghdr *msg, size_t len) | |
1035 | { | |
1036 | struct pep_sock *pn = pep_sk(sk); | |
b1704374 | 1037 | struct sk_buff *skb; |
9641458d RDC |
1038 | long timeo; |
1039 | int flags = msg->msg_flags; | |
1040 | int err, done; | |
1041 | ||
82ecbcb9 RDC |
1042 | if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL| |
1043 | MSG_CMSG_COMPAT)) || | |
1044 | !(msg->msg_flags & MSG_EOR)) | |
9641458d RDC |
1045 | return -EOPNOTSUPP; |
1046 | ||
b1704374 RDC |
1047 | skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len, |
1048 | flags & MSG_DONTWAIT, &err); | |
1049 | if (!skb) | |
02ac3268 | 1050 | return err; |
b1704374 RDC |
1051 | |
1052 | skb_reserve(skb, MAX_PHONET_HEADER + 3); | |
1053 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); | |
1054 | if (err < 0) | |
1055 | goto outfree; | |
1056 | ||
9641458d RDC |
1057 | lock_sock(sk); |
1058 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); | |
1059 | if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) { | |
1060 | err = -ENOTCONN; | |
1061 | goto out; | |
1062 | } | |
1063 | if (sk->sk_state != TCP_ESTABLISHED) { | |
1064 | /* Wait until the pipe gets to enabled state */ | |
1065 | disabled: | |
1066 | err = sk_stream_wait_connect(sk, &timeo); | |
1067 | if (err) | |
1068 | goto out; | |
1069 | ||
1070 | if (sk->sk_state == TCP_CLOSE_WAIT) { | |
1071 | err = -ECONNRESET; | |
1072 | goto out; | |
1073 | } | |
1074 | } | |
1075 | BUG_ON(sk->sk_state != TCP_ESTABLISHED); | |
1076 | ||
1077 | /* Wait until flow control allows TX */ | |
be677730 | 1078 | done = atomic_read(&pn->tx_credits); |
9641458d RDC |
1079 | while (!done) { |
1080 | DEFINE_WAIT(wait); | |
1081 | ||
1082 | if (!timeo) { | |
1083 | err = -EAGAIN; | |
1084 | goto out; | |
1085 | } | |
1086 | if (signal_pending(current)) { | |
1087 | err = sock_intr_errno(timeo); | |
1088 | goto out; | |
1089 | } | |
1090 | ||
43815482 | 1091 | prepare_to_wait(sk_sleep(sk), &wait, |
9641458d | 1092 | TASK_INTERRUPTIBLE); |
be677730 | 1093 | done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits)); |
43815482 | 1094 | finish_wait(sk_sleep(sk), &wait); |
9641458d RDC |
1095 | |
1096 | if (sk->sk_state != TCP_ESTABLISHED) | |
1097 | goto disabled; | |
1098 | } | |
1099 | ||
02a47617 | 1100 | err = pipe_skb_send(sk, skb); |
9641458d RDC |
1101 | if (err >= 0) |
1102 | err = len; /* success! */ | |
1103 | skb = NULL; | |
1104 | out: | |
1105 | release_sock(sk); | |
b1704374 | 1106 | outfree: |
9641458d RDC |
1107 | kfree_skb(skb); |
1108 | return err; | |
1109 | } | |
1110 | ||
02a47617 RDC |
1111 | int pep_writeable(struct sock *sk) |
1112 | { | |
1113 | struct pep_sock *pn = pep_sk(sk); | |
1114 | ||
be677730 | 1115 | return atomic_read(&pn->tx_credits); |
02a47617 RDC |
1116 | } |
1117 | ||
1118 | int pep_write(struct sock *sk, struct sk_buff *skb) | |
1119 | { | |
1120 | struct sk_buff *rskb, *fs; | |
1121 | int flen = 0; | |
1122 | ||
fea93ece RDC |
1123 | if (pep_sk(sk)->aligned) |
1124 | return pipe_skb_send(sk, skb); | |
1125 | ||
02a47617 RDC |
1126 | rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC); |
1127 | if (!rskb) { | |
1128 | kfree_skb(skb); | |
1129 | return -ENOMEM; | |
1130 | } | |
1131 | skb_shinfo(rskb)->frag_list = skb; | |
1132 | rskb->len += skb->len; | |
1133 | rskb->data_len += rskb->len; | |
1134 | rskb->truesize += rskb->len; | |
1135 | ||
1136 | /* Avoid nested fragments */ | |
5c313e9a | 1137 | skb_walk_frags(skb, fs) |
02a47617 RDC |
1138 | flen += fs->len; |
1139 | skb->next = skb_shinfo(skb)->frag_list; | |
5c313e9a | 1140 | skb_frag_list_init(skb); |
02a47617 RDC |
1141 | skb->len -= flen; |
1142 | skb->data_len -= flen; | |
1143 | skb->truesize -= flen; | |
1144 | ||
1145 | skb_reserve(rskb, MAX_PHONET_HEADER + 3); | |
1146 | return pipe_skb_send(sk, rskb); | |
1147 | } | |
1148 | ||
1149 | struct sk_buff *pep_read(struct sock *sk) | |
1150 | { | |
1151 | struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue); | |
1152 | ||
1153 | if (sk->sk_state == TCP_ESTABLISHED) | |
44c9ab16 | 1154 | pipe_grant_credits(sk, GFP_ATOMIC); |
02a47617 RDC |
1155 | return skb; |
1156 | } | |
1157 | ||
9641458d RDC |
1158 | static int pep_recvmsg(struct kiocb *iocb, struct sock *sk, |
1159 | struct msghdr *msg, size_t len, int noblock, | |
1160 | int flags, int *addr_len) | |
1161 | { | |
1162 | struct sk_buff *skb; | |
1163 | int err; | |
1164 | ||
82ecbcb9 RDC |
1165 | if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL| |
1166 | MSG_NOSIGNAL|MSG_CMSG_COMPAT)) | |
1167 | return -EOPNOTSUPP; | |
1168 | ||
9641458d RDC |
1169 | if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE))) |
1170 | return -ENOTCONN; | |
1171 | ||
c41bd97f RDC |
1172 | if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) { |
1173 | /* Dequeue and acknowledge control request */ | |
1174 | struct pep_sock *pn = pep_sk(sk); | |
1175 | ||
82ecbcb9 RDC |
1176 | if (flags & MSG_PEEK) |
1177 | return -EOPNOTSUPP; | |
c41bd97f RDC |
1178 | skb = skb_dequeue(&pn->ctrlreq_queue); |
1179 | if (skb) { | |
1180 | pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR, | |
1181 | GFP_KERNEL); | |
1182 | msg->msg_flags |= MSG_OOB; | |
1183 | goto copy; | |
1184 | } | |
1185 | if (flags & MSG_OOB) | |
1186 | return -EINVAL; | |
1187 | } | |
1188 | ||
9641458d RDC |
1189 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
1190 | lock_sock(sk); | |
1191 | if (skb == NULL) { | |
1192 | if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT) | |
1193 | err = -ECONNRESET; | |
1194 | release_sock(sk); | |
1195 | return err; | |
1196 | } | |
1197 | ||
1198 | if (sk->sk_state == TCP_ESTABLISHED) | |
44c9ab16 | 1199 | pipe_grant_credits(sk, GFP_KERNEL); |
9641458d | 1200 | release_sock(sk); |
c41bd97f | 1201 | copy: |
9641458d | 1202 | msg->msg_flags |= MSG_EOR; |
9641458d RDC |
1203 | if (skb->len > len) |
1204 | msg->msg_flags |= MSG_TRUNC; | |
1205 | else | |
1206 | len = skb->len; | |
1207 | ||
1208 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len); | |
1209 | if (!err) | |
1210 | err = (flags & MSG_TRUNC) ? skb->len : len; | |
1211 | ||
1212 | skb_free_datagram(sk, skb); | |
1213 | return err; | |
1214 | } | |
1215 | ||
1216 | static void pep_sock_unhash(struct sock *sk) | |
1217 | { | |
1218 | struct pep_sock *pn = pep_sk(sk); | |
1219 | struct sock *skparent = NULL; | |
1220 | ||
1221 | lock_sock(sk); | |
b3d62553 KS |
1222 | |
1223 | #ifndef CONFIG_PHONET_PIPECTRLR | |
9641458d RDC |
1224 | if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) { |
1225 | skparent = pn->listener; | |
9641458d RDC |
1226 | release_sock(sk); |
1227 | ||
9641458d | 1228 | pn = pep_sk(skparent); |
7dfde179 RDC |
1229 | lock_sock(skparent); |
1230 | sk_del_node_init(sk); | |
1231 | sk = skparent; | |
9641458d | 1232 | } |
b3d62553 | 1233 | #endif |
9641458d RDC |
1234 | /* Unhash a listening sock only when it is closed |
1235 | * and all of its active connected pipes are closed. */ | |
1236 | if (hlist_empty(&pn->hlist)) | |
1237 | pn_sock_unhash(&pn->pn_sk.sk); | |
1238 | release_sock(sk); | |
1239 | ||
1240 | if (skparent) | |
1241 | sock_put(skparent); | |
1242 | } | |
1243 | ||
1244 | static struct proto pep_proto = { | |
1245 | .close = pep_sock_close, | |
1246 | .accept = pep_sock_accept, | |
b3d62553 KS |
1247 | #ifdef CONFIG_PHONET_PIPECTRLR |
1248 | .connect = pep_sock_connect, | |
1249 | #endif | |
9641458d RDC |
1250 | .ioctl = pep_ioctl, |
1251 | .init = pep_init, | |
02a47617 RDC |
1252 | .setsockopt = pep_setsockopt, |
1253 | .getsockopt = pep_getsockopt, | |
9641458d RDC |
1254 | .sendmsg = pep_sendmsg, |
1255 | .recvmsg = pep_recvmsg, | |
1256 | .backlog_rcv = pep_do_rcv, | |
1257 | .hash = pn_sock_hash, | |
1258 | .unhash = pep_sock_unhash, | |
1259 | .get_port = pn_sock_get_port, | |
1260 | .obj_size = sizeof(struct pep_sock), | |
1261 | .owner = THIS_MODULE, | |
1262 | .name = "PNPIPE", | |
1263 | }; | |
1264 | ||
1265 | static struct phonet_protocol pep_pn_proto = { | |
1266 | .ops = &phonet_stream_ops, | |
1267 | .prot = &pep_proto, | |
1268 | .sock_type = SOCK_SEQPACKET, | |
1269 | }; | |
1270 | ||
1271 | static int __init pep_register(void) | |
1272 | { | |
1273 | return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto); | |
1274 | } | |
1275 | ||
1276 | static void __exit pep_unregister(void) | |
1277 | { | |
1278 | phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto); | |
1279 | } | |
1280 | ||
1281 | module_init(pep_register); | |
1282 | module_exit(pep_unregister); | |
1283 | MODULE_AUTHOR("Remi Denis-Courmont, Nokia"); | |
1284 | MODULE_DESCRIPTION("Phonet pipe protocol"); | |
1285 | MODULE_LICENSE("GPL"); | |
1286 | MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE); |