Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 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> |
| 26 | #include <linux/socket.h> |
| 27 | #include <net/sock.h> |
| 28 | #include <net/tcp_states.h> |
| 29 | #include <asm/ioctls.h> |
| 30 | |
| 31 | #include <linux/phonet.h> |
| 32 | #include <net/phonet/phonet.h> |
| 33 | #include <net/phonet/pep.h> |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 34 | #include <net/phonet/gprs.h> |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 35 | |
| 36 | /* sk_state values: |
| 37 | * TCP_CLOSE sock not in use yet |
| 38 | * TCP_CLOSE_WAIT disconnected pipe |
| 39 | * TCP_LISTEN listening pipe endpoint |
| 40 | * TCP_SYN_RECV connected pipe in disabled state |
| 41 | * TCP_ESTABLISHED connected pipe in enabled state |
| 42 | * |
| 43 | * pep_sock locking: |
| 44 | * - sk_state, ackq, hlist: sock lock needed |
| 45 | * - listener: read only |
| 46 | * - pipe_handle: read only |
| 47 | */ |
| 48 | |
| 49 | #define CREDITS_MAX 10 |
| 50 | #define CREDITS_THR 7 |
| 51 | |
| 52 | static const struct sockaddr_pn pipe_srv = { |
| 53 | .spn_family = AF_PHONET, |
| 54 | .spn_resource = 0xD9, /* pipe service */ |
| 55 | }; |
| 56 | |
| 57 | #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */ |
| 58 | |
| 59 | /* Get the next TLV sub-block. */ |
| 60 | static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen, |
| 61 | void *buf) |
| 62 | { |
| 63 | void *data = NULL; |
| 64 | struct { |
| 65 | u8 sb_type; |
| 66 | u8 sb_len; |
| 67 | } *ph, h; |
| 68 | int buflen = *plen; |
| 69 | |
| 70 | ph = skb_header_pointer(skb, 0, 2, &h); |
| 71 | if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len)) |
| 72 | return NULL; |
| 73 | ph->sb_len -= 2; |
| 74 | *ptype = ph->sb_type; |
| 75 | *plen = ph->sb_len; |
| 76 | |
| 77 | if (buflen > ph->sb_len) |
| 78 | buflen = ph->sb_len; |
| 79 | data = skb_header_pointer(skb, 2, buflen, buf); |
| 80 | __skb_pull(skb, 2 + ph->sb_len); |
| 81 | return data; |
| 82 | } |
| 83 | |
| 84 | static int pep_reply(struct sock *sk, struct sk_buff *oskb, |
| 85 | u8 code, const void *data, int len, gfp_t priority) |
| 86 | { |
| 87 | const struct pnpipehdr *oph = pnp_hdr(oskb); |
| 88 | struct pnpipehdr *ph; |
| 89 | struct sk_buff *skb; |
| 90 | |
| 91 | skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority); |
| 92 | if (!skb) |
| 93 | return -ENOMEM; |
| 94 | skb_set_owner_w(skb, sk); |
| 95 | |
| 96 | skb_reserve(skb, MAX_PNPIPE_HEADER); |
| 97 | __skb_put(skb, len); |
| 98 | skb_copy_to_linear_data(skb, data, len); |
| 99 | __skb_push(skb, sizeof(*ph)); |
| 100 | skb_reset_transport_header(skb); |
| 101 | ph = pnp_hdr(skb); |
| 102 | ph->utid = oph->utid; |
| 103 | ph->message_id = oph->message_id + 1; /* REQ -> RESP */ |
| 104 | ph->pipe_handle = oph->pipe_handle; |
| 105 | ph->error_code = code; |
| 106 | |
| 107 | return pn_skb_send(sk, skb, &pipe_srv); |
| 108 | } |
| 109 | |
| 110 | #define PAD 0x00 |
| 111 | static int pep_accept_conn(struct sock *sk, struct sk_buff *skb) |
| 112 | { |
| 113 | static const u8 data[20] = { |
| 114 | PAD, PAD, PAD, 2 /* sub-blocks */, |
| 115 | PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD, |
| 116 | PN_MULTI_CREDIT_FLOW_CONTROL, |
| 117 | PN_ONE_CREDIT_FLOW_CONTROL, |
| 118 | PN_LEGACY_FLOW_CONTROL, |
| 119 | PAD, |
| 120 | PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD, |
| 121 | PN_MULTI_CREDIT_FLOW_CONTROL, |
| 122 | PN_ONE_CREDIT_FLOW_CONTROL, |
| 123 | PN_LEGACY_FLOW_CONTROL, |
| 124 | PAD, |
| 125 | }; |
| 126 | |
| 127 | might_sleep(); |
| 128 | return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data), |
| 129 | GFP_KERNEL); |
| 130 | } |
| 131 | |
| 132 | static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code) |
| 133 | { |
| 134 | static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ }; |
| 135 | WARN_ON(code == PN_PIPE_NO_ERROR); |
| 136 | return pep_reply(sk, skb, code, data, sizeof(data), GFP_ATOMIC); |
| 137 | } |
| 138 | |
| 139 | /* Control requests are not sent by the pipe service and have a specific |
| 140 | * message format. */ |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 141 | static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code, |
| 142 | gfp_t priority) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 143 | { |
| 144 | const struct pnpipehdr *oph = pnp_hdr(oskb); |
| 145 | struct sk_buff *skb; |
| 146 | struct pnpipehdr *ph; |
| 147 | struct sockaddr_pn dst; |
| 148 | |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 149 | skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 150 | if (!skb) |
| 151 | return -ENOMEM; |
| 152 | skb_set_owner_w(skb, sk); |
| 153 | |
| 154 | skb_reserve(skb, MAX_PHONET_HEADER); |
| 155 | ph = (struct pnpipehdr *)skb_put(skb, sizeof(*ph) + 4); |
| 156 | |
| 157 | ph->utid = oph->utid; |
| 158 | ph->message_id = PNS_PEP_CTRL_RESP; |
| 159 | ph->pipe_handle = oph->pipe_handle; |
| 160 | ph->data[0] = oph->data[1]; /* CTRL id */ |
| 161 | ph->data[1] = oph->data[0]; /* PEP type */ |
| 162 | ph->data[2] = code; /* error code, at an usual offset */ |
| 163 | ph->data[3] = PAD; |
| 164 | ph->data[4] = PAD; |
| 165 | |
| 166 | pn_skb_get_src_sockaddr(oskb, &dst); |
| 167 | return pn_skb_send(sk, skb, &dst); |
| 168 | } |
| 169 | |
| 170 | static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority) |
| 171 | { |
| 172 | struct pep_sock *pn = pep_sk(sk); |
| 173 | struct pnpipehdr *ph; |
| 174 | struct sk_buff *skb; |
| 175 | |
| 176 | skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority); |
| 177 | if (!skb) |
| 178 | return -ENOMEM; |
| 179 | skb_set_owner_w(skb, sk); |
| 180 | |
| 181 | skb_reserve(skb, MAX_PNPIPE_HEADER + 4); |
| 182 | __skb_push(skb, sizeof(*ph) + 4); |
| 183 | skb_reset_transport_header(skb); |
| 184 | ph = pnp_hdr(skb); |
| 185 | ph->utid = 0; |
| 186 | ph->message_id = PNS_PEP_STATUS_IND; |
| 187 | ph->pipe_handle = pn->pipe_handle; |
| 188 | ph->pep_type = PN_PEP_TYPE_COMMON; |
| 189 | ph->data[1] = type; |
| 190 | ph->data[2] = PAD; |
| 191 | ph->data[3] = PAD; |
| 192 | ph->data[4] = status; |
| 193 | |
| 194 | return pn_skb_send(sk, skb, &pipe_srv); |
| 195 | } |
| 196 | |
| 197 | /* Send our RX flow control information to the sender. |
| 198 | * Socket must be locked. */ |
| 199 | static void pipe_grant_credits(struct sock *sk) |
| 200 | { |
| 201 | struct pep_sock *pn = pep_sk(sk); |
| 202 | |
| 203 | BUG_ON(sk->sk_state != TCP_ESTABLISHED); |
| 204 | |
| 205 | switch (pn->rx_fc) { |
| 206 | case PN_LEGACY_FLOW_CONTROL: /* TODO */ |
| 207 | break; |
| 208 | case PN_ONE_CREDIT_FLOW_CONTROL: |
| 209 | pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL, |
| 210 | PEP_IND_READY, GFP_ATOMIC); |
| 211 | pn->rx_credits = 1; |
| 212 | break; |
| 213 | case PN_MULTI_CREDIT_FLOW_CONTROL: |
| 214 | if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX) |
| 215 | break; |
| 216 | if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS, |
| 217 | CREDITS_MAX - pn->rx_credits, |
| 218 | GFP_ATOMIC) == 0) |
| 219 | pn->rx_credits = CREDITS_MAX; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb) |
| 225 | { |
| 226 | struct pep_sock *pn = pep_sk(sk); |
| 227 | struct pnpipehdr *hdr = pnp_hdr(skb); |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 228 | int wake = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 229 | |
| 230 | if (!pskb_may_pull(skb, sizeof(*hdr) + 4)) |
| 231 | return -EINVAL; |
| 232 | |
| 233 | if (hdr->data[0] != PN_PEP_TYPE_COMMON) { |
| 234 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n", |
| 235 | (unsigned)hdr->data[0]); |
| 236 | return -EOPNOTSUPP; |
| 237 | } |
| 238 | |
| 239 | switch (hdr->data[1]) { |
| 240 | case PN_PEP_IND_FLOW_CONTROL: |
| 241 | switch (pn->tx_fc) { |
| 242 | case PN_LEGACY_FLOW_CONTROL: |
| 243 | switch (hdr->data[4]) { |
| 244 | case PEP_IND_BUSY: |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 245 | atomic_set(&pn->tx_credits, 0); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 246 | break; |
| 247 | case PEP_IND_READY: |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 248 | atomic_set(&pn->tx_credits, wake = 1); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | break; |
| 252 | case PN_ONE_CREDIT_FLOW_CONTROL: |
| 253 | if (hdr->data[4] == PEP_IND_READY) |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 254 | atomic_set(&pn->tx_credits, wake = 1); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | break; |
| 258 | |
| 259 | case PN_PEP_IND_ID_MCFC_GRANT_CREDITS: |
| 260 | if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL) |
| 261 | break; |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 262 | atomic_add(wake = hdr->data[4], &pn->tx_credits); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 263 | break; |
| 264 | |
| 265 | default: |
| 266 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n", |
| 267 | (unsigned)hdr->data[1]); |
| 268 | return -EOPNOTSUPP; |
| 269 | } |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 270 | if (wake) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 271 | sk->sk_write_space(sk); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb) |
| 276 | { |
| 277 | struct pep_sock *pn = pep_sk(sk); |
| 278 | struct pnpipehdr *hdr = pnp_hdr(skb); |
| 279 | u8 n_sb = hdr->data[0]; |
| 280 | |
| 281 | pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL; |
| 282 | __skb_pull(skb, sizeof(*hdr)); |
| 283 | while (n_sb > 0) { |
| 284 | u8 type, buf[2], len = sizeof(buf); |
| 285 | u8 *data = pep_get_sb(skb, &type, &len, buf); |
| 286 | |
| 287 | if (data == NULL) |
| 288 | return -EINVAL; |
| 289 | switch (type) { |
| 290 | case PN_PIPE_SB_NEGOTIATED_FC: |
| 291 | if (len < 2 || (data[0] | data[1]) > 3) |
| 292 | break; |
| 293 | pn->tx_fc = data[0] & 3; |
| 294 | pn->rx_fc = data[1] & 3; |
| 295 | break; |
| 296 | } |
| 297 | n_sb--; |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* Queue an skb to a connected sock. |
| 303 | * Socket lock must be held. */ |
| 304 | static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb) |
| 305 | { |
| 306 | struct pep_sock *pn = pep_sk(sk); |
| 307 | struct pnpipehdr *hdr = pnp_hdr(skb); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 308 | struct sk_buff_head *queue; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 309 | int err = 0; |
| 310 | |
| 311 | BUG_ON(sk->sk_state == TCP_CLOSE_WAIT); |
| 312 | |
| 313 | switch (hdr->message_id) { |
| 314 | case PNS_PEP_CONNECT_REQ: |
| 315 | pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE); |
| 316 | break; |
| 317 | |
| 318 | case PNS_PEP_DISCONNECT_REQ: |
| 319 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 320 | sk->sk_state = TCP_CLOSE_WAIT; |
| 321 | if (!sock_flag(sk, SOCK_DEAD)) |
| 322 | sk->sk_state_change(sk); |
| 323 | break; |
| 324 | |
| 325 | case PNS_PEP_ENABLE_REQ: |
| 326 | /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */ |
| 327 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 328 | break; |
| 329 | |
| 330 | case PNS_PEP_RESET_REQ: |
| 331 | switch (hdr->state_after_reset) { |
| 332 | case PN_PIPE_DISABLE: |
| 333 | pn->init_enable = 0; |
| 334 | break; |
| 335 | case PN_PIPE_ENABLE: |
| 336 | pn->init_enable = 1; |
| 337 | break; |
| 338 | default: /* not allowed to send an error here!? */ |
| 339 | err = -EINVAL; |
| 340 | goto out; |
| 341 | } |
| 342 | /* fall through */ |
| 343 | case PNS_PEP_DISABLE_REQ: |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 344 | atomic_set(&pn->tx_credits, 0); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 345 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 346 | break; |
| 347 | |
| 348 | case PNS_PEP_CTRL_REQ: |
Rémi Denis-Courmont | 2e2fb4b | 2009-07-21 01:57:59 +0000 | [diff] [blame] | 349 | if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) { |
| 350 | atomic_inc(&sk->sk_drops); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 351 | break; |
Rémi Denis-Courmont | 2e2fb4b | 2009-07-21 01:57:59 +0000 | [diff] [blame] | 352 | } |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 353 | __skb_pull(skb, 4); |
| 354 | queue = &pn->ctrlreq_queue; |
| 355 | goto queue; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 356 | |
| 357 | case PNS_PIPE_DATA: |
| 358 | __skb_pull(skb, 3); /* Pipe data header */ |
| 359 | if (!pn_flow_safe(pn->rx_fc)) { |
| 360 | err = sock_queue_rcv_skb(sk, skb); |
| 361 | if (!err) |
| 362 | return 0; |
Rémi Denis-Courmont | 2e2fb4b | 2009-07-21 01:57:59 +0000 | [diff] [blame] | 363 | if (err == -ENOMEM) |
| 364 | atomic_inc(&sk->sk_drops); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | |
| 368 | if (pn->rx_credits == 0) { |
Rémi Denis-Courmont | 2e2fb4b | 2009-07-21 01:57:59 +0000 | [diff] [blame] | 369 | atomic_inc(&sk->sk_drops); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 370 | err = -ENOBUFS; |
| 371 | break; |
| 372 | } |
| 373 | pn->rx_credits--; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 374 | queue = &sk->sk_receive_queue; |
| 375 | goto queue; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 376 | |
| 377 | case PNS_PEP_STATUS_IND: |
| 378 | pipe_rcv_status(sk, skb); |
| 379 | break; |
| 380 | |
| 381 | case PNS_PIPE_REDIRECTED_IND: |
| 382 | err = pipe_rcv_created(sk, skb); |
| 383 | break; |
| 384 | |
| 385 | case PNS_PIPE_CREATED_IND: |
| 386 | err = pipe_rcv_created(sk, skb); |
| 387 | if (err) |
| 388 | break; |
| 389 | /* fall through */ |
| 390 | case PNS_PIPE_RESET_IND: |
| 391 | if (!pn->init_enable) |
| 392 | break; |
| 393 | /* fall through */ |
| 394 | case PNS_PIPE_ENABLED_IND: |
| 395 | if (!pn_flow_safe(pn->tx_fc)) { |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 396 | atomic_set(&pn->tx_credits, 1); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 397 | sk->sk_write_space(sk); |
| 398 | } |
| 399 | if (sk->sk_state == TCP_ESTABLISHED) |
| 400 | break; /* Nothing to do */ |
| 401 | sk->sk_state = TCP_ESTABLISHED; |
| 402 | pipe_grant_credits(sk); |
| 403 | break; |
| 404 | |
| 405 | case PNS_PIPE_DISABLED_IND: |
| 406 | sk->sk_state = TCP_SYN_RECV; |
| 407 | pn->rx_credits = 0; |
| 408 | break; |
| 409 | |
| 410 | default: |
| 411 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n", |
| 412 | hdr->message_id); |
| 413 | err = -EINVAL; |
| 414 | } |
| 415 | out: |
| 416 | kfree_skb(skb); |
| 417 | return err; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 418 | |
| 419 | queue: |
| 420 | skb->dev = NULL; |
| 421 | skb_set_owner_r(skb, sk); |
| 422 | err = skb->len; |
| 423 | skb_queue_tail(queue, skb); |
| 424 | if (!sock_flag(sk, SOCK_DEAD)) |
| 425 | sk->sk_data_ready(sk, err); |
| 426 | return 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* Destroy connected sock. */ |
| 430 | static void pipe_destruct(struct sock *sk) |
| 431 | { |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 432 | struct pep_sock *pn = pep_sk(sk); |
| 433 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 434 | skb_queue_purge(&sk->sk_receive_queue); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 435 | skb_queue_purge(&pn->ctrlreq_queue); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb) |
| 439 | { |
| 440 | struct sock *newsk; |
| 441 | struct pep_sock *newpn, *pn = pep_sk(sk); |
| 442 | struct pnpipehdr *hdr; |
| 443 | struct sockaddr_pn dst; |
| 444 | u16 peer_type; |
| 445 | u8 pipe_handle, enabled, n_sb; |
| 446 | |
| 447 | if (!pskb_pull(skb, sizeof(*hdr) + 4)) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | hdr = pnp_hdr(skb); |
| 451 | pipe_handle = hdr->pipe_handle; |
| 452 | switch (hdr->state_after_connect) { |
| 453 | case PN_PIPE_DISABLE: |
| 454 | enabled = 0; |
| 455 | break; |
| 456 | case PN_PIPE_ENABLE: |
| 457 | enabled = 1; |
| 458 | break; |
| 459 | default: |
| 460 | pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM); |
| 461 | return -EINVAL; |
| 462 | } |
| 463 | peer_type = hdr->other_pep_type << 8; |
| 464 | |
| 465 | if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) { |
| 466 | pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE); |
| 467 | return -ENOBUFS; |
| 468 | } |
| 469 | |
| 470 | /* Parse sub-blocks (options) */ |
| 471 | n_sb = hdr->data[4]; |
| 472 | while (n_sb > 0) { |
| 473 | u8 type, buf[1], len = sizeof(buf); |
| 474 | const u8 *data = pep_get_sb(skb, &type, &len, buf); |
| 475 | |
| 476 | if (data == NULL) |
| 477 | return -EINVAL; |
| 478 | switch (type) { |
| 479 | case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE: |
| 480 | if (len < 1) |
| 481 | return -EINVAL; |
| 482 | peer_type = (peer_type & 0xff00) | data[0]; |
| 483 | break; |
| 484 | } |
| 485 | n_sb--; |
| 486 | } |
| 487 | |
| 488 | skb = skb_clone(skb, GFP_ATOMIC); |
| 489 | if (!skb) |
| 490 | return -ENOMEM; |
| 491 | |
| 492 | /* Create a new to-be-accepted sock */ |
| 493 | newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot); |
| 494 | if (!newsk) { |
| 495 | kfree_skb(skb); |
| 496 | return -ENOMEM; |
| 497 | } |
| 498 | sock_init_data(NULL, newsk); |
| 499 | newsk->sk_state = TCP_SYN_RECV; |
| 500 | newsk->sk_backlog_rcv = pipe_do_rcv; |
| 501 | newsk->sk_protocol = sk->sk_protocol; |
| 502 | newsk->sk_destruct = pipe_destruct; |
| 503 | |
| 504 | newpn = pep_sk(newsk); |
| 505 | pn_skb_get_dst_sockaddr(skb, &dst); |
| 506 | newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst); |
| 507 | newpn->pn_sk.resource = pn->pn_sk.resource; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 508 | skb_queue_head_init(&newpn->ctrlreq_queue); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 509 | newpn->pipe_handle = pipe_handle; |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 510 | atomic_set(&newpn->tx_credits, 0); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 511 | newpn->peer_type = peer_type; |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 512 | newpn->rx_credits = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 513 | newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL; |
| 514 | newpn->init_enable = enabled; |
| 515 | |
| 516 | BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue)); |
| 517 | skb_queue_head(&newsk->sk_receive_queue, skb); |
| 518 | if (!sock_flag(sk, SOCK_DEAD)) |
| 519 | sk->sk_data_ready(sk, 0); |
| 520 | |
| 521 | sk_acceptq_added(sk); |
| 522 | sk_add_node(newsk, &pn->ackq); |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | /* Listening sock must be locked */ |
| 527 | static struct sock *pep_find_pipe(const struct hlist_head *hlist, |
| 528 | const struct sockaddr_pn *dst, |
| 529 | u8 pipe_handle) |
| 530 | { |
| 531 | struct hlist_node *node; |
| 532 | struct sock *sknode; |
| 533 | u16 dobj = pn_sockaddr_get_object(dst); |
| 534 | |
| 535 | sk_for_each(sknode, node, hlist) { |
| 536 | struct pep_sock *pnnode = pep_sk(sknode); |
| 537 | |
| 538 | /* Ports match, but addresses might not: */ |
| 539 | if (pnnode->pn_sk.sobject != dobj) |
| 540 | continue; |
| 541 | if (pnnode->pipe_handle != pipe_handle) |
| 542 | continue; |
| 543 | if (sknode->sk_state == TCP_CLOSE_WAIT) |
| 544 | continue; |
| 545 | |
| 546 | sock_hold(sknode); |
| 547 | return sknode; |
| 548 | } |
| 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Deliver an skb to a listening sock. |
| 554 | * Socket lock must be held. |
| 555 | * We then queue the skb to the right connected sock (if any). |
| 556 | */ |
| 557 | static int pep_do_rcv(struct sock *sk, struct sk_buff *skb) |
| 558 | { |
| 559 | struct pep_sock *pn = pep_sk(sk); |
| 560 | struct sock *sknode; |
Rémi Denis-Courmont | 2ddc1ac | 2009-02-10 17:14:50 -0800 | [diff] [blame] | 561 | struct pnpipehdr *hdr; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 562 | struct sockaddr_pn dst; |
| 563 | int err = NET_RX_SUCCESS; |
| 564 | u8 pipe_handle; |
| 565 | |
| 566 | if (!pskb_may_pull(skb, sizeof(*hdr))) |
| 567 | goto drop; |
| 568 | |
| 569 | hdr = pnp_hdr(skb); |
| 570 | pipe_handle = hdr->pipe_handle; |
| 571 | if (pipe_handle == PN_PIPE_INVALID_HANDLE) |
| 572 | goto drop; |
| 573 | |
| 574 | pn_skb_get_dst_sockaddr(skb, &dst); |
| 575 | |
| 576 | /* Look for an existing pipe handle */ |
| 577 | sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle); |
| 578 | if (sknode) |
| 579 | return sk_receive_skb(sknode, skb, 1); |
| 580 | |
| 581 | /* Look for a pipe handle pending accept */ |
| 582 | sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle); |
| 583 | if (sknode) { |
| 584 | sock_put(sknode); |
| 585 | if (net_ratelimit()) |
| 586 | printk(KERN_WARNING"Phonet unconnected PEP ignored"); |
| 587 | err = NET_RX_DROP; |
| 588 | goto drop; |
| 589 | } |
| 590 | |
| 591 | switch (hdr->message_id) { |
| 592 | case PNS_PEP_CONNECT_REQ: |
| 593 | err = pep_connreq_rcv(sk, skb); |
| 594 | break; |
| 595 | |
| 596 | case PNS_PEP_DISCONNECT_REQ: |
| 597 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 598 | break; |
| 599 | |
| 600 | case PNS_PEP_CTRL_REQ: |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 601 | pep_ctrlreq_error(sk, skb, PN_PIPE_INVALID_HANDLE, GFP_ATOMIC); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | case PNS_PEP_RESET_REQ: |
| 605 | case PNS_PEP_ENABLE_REQ: |
| 606 | case PNS_PEP_DISABLE_REQ: |
| 607 | /* invalid handle is not even allowed here! */ |
| 608 | default: |
| 609 | err = NET_RX_DROP; |
| 610 | } |
| 611 | drop: |
| 612 | kfree_skb(skb); |
| 613 | return err; |
| 614 | } |
| 615 | |
| 616 | /* associated socket ceases to exist */ |
| 617 | static void pep_sock_close(struct sock *sk, long timeout) |
| 618 | { |
| 619 | struct pep_sock *pn = pep_sk(sk); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 620 | int ifindex = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 621 | |
| 622 | sk_common_release(sk); |
| 623 | |
| 624 | lock_sock(sk); |
| 625 | if (sk->sk_state == TCP_LISTEN) { |
| 626 | /* Destroy the listen queue */ |
| 627 | struct sock *sknode; |
| 628 | struct hlist_node *p, *n; |
| 629 | |
| 630 | sk_for_each_safe(sknode, p, n, &pn->ackq) |
| 631 | sk_del_node_init(sknode); |
| 632 | sk->sk_state = TCP_CLOSE; |
| 633 | } |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 634 | ifindex = pn->ifindex; |
| 635 | pn->ifindex = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 636 | release_sock(sk); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 637 | |
| 638 | if (ifindex) |
| 639 | gprs_detach(sk); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static int pep_wait_connreq(struct sock *sk, int noblock) |
| 643 | { |
| 644 | struct task_struct *tsk = current; |
| 645 | struct pep_sock *pn = pep_sk(sk); |
| 646 | long timeo = sock_rcvtimeo(sk, noblock); |
| 647 | |
| 648 | for (;;) { |
| 649 | DEFINE_WAIT(wait); |
| 650 | |
| 651 | if (sk->sk_state != TCP_LISTEN) |
| 652 | return -EINVAL; |
| 653 | if (!hlist_empty(&pn->ackq)) |
| 654 | break; |
| 655 | if (!timeo) |
| 656 | return -EWOULDBLOCK; |
| 657 | if (signal_pending(tsk)) |
| 658 | return sock_intr_errno(timeo); |
| 659 | |
| 660 | prepare_to_wait_exclusive(&sk->sk_socket->wait, &wait, |
| 661 | TASK_INTERRUPTIBLE); |
| 662 | release_sock(sk); |
| 663 | timeo = schedule_timeout(timeo); |
| 664 | lock_sock(sk); |
| 665 | finish_wait(&sk->sk_socket->wait, &wait); |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp) |
| 672 | { |
| 673 | struct pep_sock *pn = pep_sk(sk); |
| 674 | struct sock *newsk = NULL; |
| 675 | struct sk_buff *oskb; |
| 676 | int err; |
| 677 | |
| 678 | lock_sock(sk); |
| 679 | err = pep_wait_connreq(sk, flags & O_NONBLOCK); |
| 680 | if (err) |
| 681 | goto out; |
| 682 | |
| 683 | newsk = __sk_head(&pn->ackq); |
| 684 | |
| 685 | oskb = skb_dequeue(&newsk->sk_receive_queue); |
| 686 | err = pep_accept_conn(newsk, oskb); |
| 687 | if (err) { |
| 688 | skb_queue_head(&newsk->sk_receive_queue, oskb); |
| 689 | newsk = NULL; |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | sock_hold(sk); |
| 694 | pep_sk(newsk)->listener = sk; |
| 695 | |
| 696 | sock_hold(newsk); |
| 697 | sk_del_node_init(newsk); |
| 698 | sk_acceptq_removed(sk); |
| 699 | sk_add_node(newsk, &pn->hlist); |
| 700 | __sock_put(newsk); |
| 701 | |
| 702 | out: |
| 703 | release_sock(sk); |
| 704 | *errp = err; |
| 705 | return newsk; |
| 706 | } |
| 707 | |
| 708 | static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 709 | { |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 710 | struct pep_sock *pn = pep_sk(sk); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 711 | int answ; |
| 712 | |
| 713 | switch (cmd) { |
| 714 | case SIOCINQ: |
| 715 | if (sk->sk_state == TCP_LISTEN) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | lock_sock(sk); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 719 | if (sock_flag(sk, SOCK_URGINLINE) |
| 720 | && !skb_queue_empty(&pn->ctrlreq_queue)) |
| 721 | answ = skb_peek(&pn->ctrlreq_queue)->len; |
| 722 | else if (!skb_queue_empty(&sk->sk_receive_queue)) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 723 | answ = skb_peek(&sk->sk_receive_queue)->len; |
| 724 | else |
| 725 | answ = 0; |
| 726 | release_sock(sk); |
| 727 | return put_user(answ, (int __user *)arg); |
| 728 | } |
| 729 | |
| 730 | return -ENOIOCTLCMD; |
| 731 | } |
| 732 | |
| 733 | static int pep_init(struct sock *sk) |
| 734 | { |
| 735 | struct pep_sock *pn = pep_sk(sk); |
| 736 | |
| 737 | INIT_HLIST_HEAD(&pn->ackq); |
| 738 | INIT_HLIST_HEAD(&pn->hlist); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 739 | skb_queue_head_init(&pn->ctrlreq_queue); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 740 | pn->pipe_handle = PN_PIPE_INVALID_HANDLE; |
| 741 | return 0; |
| 742 | } |
| 743 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 744 | static int pep_setsockopt(struct sock *sk, int level, int optname, |
| 745 | char __user *optval, int optlen) |
| 746 | { |
| 747 | struct pep_sock *pn = pep_sk(sk); |
| 748 | int val = 0, err = 0; |
| 749 | |
| 750 | if (level != SOL_PNPIPE) |
| 751 | return -ENOPROTOOPT; |
| 752 | if (optlen >= sizeof(int)) { |
| 753 | if (get_user(val, (int __user *) optval)) |
| 754 | return -EFAULT; |
| 755 | } |
| 756 | |
| 757 | lock_sock(sk); |
| 758 | switch (optname) { |
| 759 | case PNPIPE_ENCAP: |
| 760 | if (val && val != PNPIPE_ENCAP_IP) { |
| 761 | err = -EINVAL; |
| 762 | break; |
| 763 | } |
| 764 | if (!pn->ifindex == !val) |
| 765 | break; /* Nothing to do! */ |
| 766 | if (!capable(CAP_NET_ADMIN)) { |
| 767 | err = -EPERM; |
| 768 | break; |
| 769 | } |
| 770 | if (val) { |
| 771 | release_sock(sk); |
| 772 | err = gprs_attach(sk); |
| 773 | if (err > 0) { |
| 774 | pn->ifindex = err; |
| 775 | err = 0; |
| 776 | } |
| 777 | } else { |
| 778 | pn->ifindex = 0; |
| 779 | release_sock(sk); |
| 780 | gprs_detach(sk); |
| 781 | err = 0; |
| 782 | } |
| 783 | goto out_norel; |
| 784 | default: |
| 785 | err = -ENOPROTOOPT; |
| 786 | } |
| 787 | release_sock(sk); |
| 788 | |
| 789 | out_norel: |
| 790 | return err; |
| 791 | } |
| 792 | |
| 793 | static int pep_getsockopt(struct sock *sk, int level, int optname, |
| 794 | char __user *optval, int __user *optlen) |
| 795 | { |
| 796 | struct pep_sock *pn = pep_sk(sk); |
| 797 | int len, val; |
| 798 | |
| 799 | if (level != SOL_PNPIPE) |
| 800 | return -ENOPROTOOPT; |
| 801 | if (get_user(len, optlen)) |
| 802 | return -EFAULT; |
| 803 | |
| 804 | switch (optname) { |
| 805 | case PNPIPE_ENCAP: |
| 806 | val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE; |
| 807 | break; |
| 808 | case PNPIPE_IFINDEX: |
| 809 | val = pn->ifindex; |
| 810 | break; |
| 811 | default: |
| 812 | return -ENOPROTOOPT; |
| 813 | } |
| 814 | |
| 815 | len = min_t(unsigned int, sizeof(int), len); |
| 816 | if (put_user(len, optlen)) |
| 817 | return -EFAULT; |
| 818 | if (put_user(val, (int __user *) optval)) |
| 819 | return -EFAULT; |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int pipe_skb_send(struct sock *sk, struct sk_buff *skb) |
| 824 | { |
| 825 | struct pep_sock *pn = pep_sk(sk); |
| 826 | struct pnpipehdr *ph; |
| 827 | |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 828 | if (pn_flow_safe(pn->tx_fc) && |
| 829 | !atomic_add_unless(&pn->tx_credits, -1, 0)) { |
| 830 | kfree_skb(skb); |
| 831 | return -ENOBUFS; |
| 832 | } |
| 833 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 834 | skb_push(skb, 3); |
| 835 | skb_reset_transport_header(skb); |
| 836 | ph = pnp_hdr(skb); |
| 837 | ph->utid = 0; |
| 838 | ph->message_id = PNS_PIPE_DATA; |
| 839 | ph->pipe_handle = pn->pipe_handle; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 840 | |
| 841 | return pn_skb_send(sk, skb, &pipe_srv); |
| 842 | } |
| 843 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 844 | static int pep_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 845 | struct msghdr *msg, size_t len) |
| 846 | { |
| 847 | struct pep_sock *pn = pep_sk(sk); |
| 848 | struct sk_buff *skb = NULL; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 849 | long timeo; |
| 850 | int flags = msg->msg_flags; |
| 851 | int err, done; |
| 852 | |
| 853 | if (msg->msg_flags & MSG_OOB || !(msg->msg_flags & MSG_EOR)) |
| 854 | return -EOPNOTSUPP; |
| 855 | |
| 856 | lock_sock(sk); |
| 857 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
| 858 | if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) { |
| 859 | err = -ENOTCONN; |
| 860 | goto out; |
| 861 | } |
| 862 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 863 | /* Wait until the pipe gets to enabled state */ |
| 864 | disabled: |
| 865 | err = sk_stream_wait_connect(sk, &timeo); |
| 866 | if (err) |
| 867 | goto out; |
| 868 | |
| 869 | if (sk->sk_state == TCP_CLOSE_WAIT) { |
| 870 | err = -ECONNRESET; |
| 871 | goto out; |
| 872 | } |
| 873 | } |
| 874 | BUG_ON(sk->sk_state != TCP_ESTABLISHED); |
| 875 | |
| 876 | /* Wait until flow control allows TX */ |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 877 | done = atomic_read(&pn->tx_credits); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 878 | while (!done) { |
| 879 | DEFINE_WAIT(wait); |
| 880 | |
| 881 | if (!timeo) { |
| 882 | err = -EAGAIN; |
| 883 | goto out; |
| 884 | } |
| 885 | if (signal_pending(current)) { |
| 886 | err = sock_intr_errno(timeo); |
| 887 | goto out; |
| 888 | } |
| 889 | |
| 890 | prepare_to_wait(&sk->sk_socket->wait, &wait, |
| 891 | TASK_INTERRUPTIBLE); |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 892 | done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits)); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 893 | finish_wait(&sk->sk_socket->wait, &wait); |
| 894 | |
| 895 | if (sk->sk_state != TCP_ESTABLISHED) |
| 896 | goto disabled; |
| 897 | } |
| 898 | |
| 899 | if (!skb) { |
| 900 | skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len, |
| 901 | flags & MSG_DONTWAIT, &err); |
| 902 | if (skb == NULL) |
| 903 | goto out; |
| 904 | skb_reserve(skb, MAX_PHONET_HEADER + 3); |
| 905 | |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 906 | if (sk->sk_state != TCP_ESTABLISHED || |
| 907 | !atomic_read(&pn->tx_credits)) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 908 | goto disabled; /* sock_alloc_send_skb might sleep */ |
| 909 | } |
| 910 | |
| 911 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); |
| 912 | if (err < 0) |
| 913 | goto out; |
| 914 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 915 | err = pipe_skb_send(sk, skb); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 916 | if (err >= 0) |
| 917 | err = len; /* success! */ |
| 918 | skb = NULL; |
| 919 | out: |
| 920 | release_sock(sk); |
| 921 | kfree_skb(skb); |
| 922 | return err; |
| 923 | } |
| 924 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 925 | int pep_writeable(struct sock *sk) |
| 926 | { |
| 927 | struct pep_sock *pn = pep_sk(sk); |
| 928 | |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 929 | return atomic_read(&pn->tx_credits); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | int pep_write(struct sock *sk, struct sk_buff *skb) |
| 933 | { |
| 934 | struct sk_buff *rskb, *fs; |
| 935 | int flen = 0; |
| 936 | |
| 937 | rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC); |
| 938 | if (!rskb) { |
| 939 | kfree_skb(skb); |
| 940 | return -ENOMEM; |
| 941 | } |
| 942 | skb_shinfo(rskb)->frag_list = skb; |
| 943 | rskb->len += skb->len; |
| 944 | rskb->data_len += rskb->len; |
| 945 | rskb->truesize += rskb->len; |
| 946 | |
| 947 | /* Avoid nested fragments */ |
David S. Miller | 5c313e9 | 2009-06-09 00:21:58 -0700 | [diff] [blame] | 948 | skb_walk_frags(skb, fs) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 949 | flen += fs->len; |
| 950 | skb->next = skb_shinfo(skb)->frag_list; |
David S. Miller | 5c313e9 | 2009-06-09 00:21:58 -0700 | [diff] [blame] | 951 | skb_frag_list_init(skb); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 952 | skb->len -= flen; |
| 953 | skb->data_len -= flen; |
| 954 | skb->truesize -= flen; |
| 955 | |
| 956 | skb_reserve(rskb, MAX_PHONET_HEADER + 3); |
| 957 | return pipe_skb_send(sk, rskb); |
| 958 | } |
| 959 | |
| 960 | struct sk_buff *pep_read(struct sock *sk) |
| 961 | { |
| 962 | struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue); |
| 963 | |
| 964 | if (sk->sk_state == TCP_ESTABLISHED) |
| 965 | pipe_grant_credits(sk); |
| 966 | return skb; |
| 967 | } |
| 968 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 969 | static int pep_recvmsg(struct kiocb *iocb, struct sock *sk, |
| 970 | struct msghdr *msg, size_t len, int noblock, |
| 971 | int flags, int *addr_len) |
| 972 | { |
| 973 | struct sk_buff *skb; |
| 974 | int err; |
| 975 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 976 | if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE))) |
| 977 | return -ENOTCONN; |
| 978 | |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 979 | if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) { |
| 980 | /* Dequeue and acknowledge control request */ |
| 981 | struct pep_sock *pn = pep_sk(sk); |
| 982 | |
| 983 | skb = skb_dequeue(&pn->ctrlreq_queue); |
| 984 | if (skb) { |
| 985 | pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR, |
| 986 | GFP_KERNEL); |
| 987 | msg->msg_flags |= MSG_OOB; |
| 988 | goto copy; |
| 989 | } |
| 990 | if (flags & MSG_OOB) |
| 991 | return -EINVAL; |
| 992 | } |
| 993 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 994 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 995 | lock_sock(sk); |
| 996 | if (skb == NULL) { |
| 997 | if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT) |
| 998 | err = -ECONNRESET; |
| 999 | release_sock(sk); |
| 1000 | return err; |
| 1001 | } |
| 1002 | |
| 1003 | if (sk->sk_state == TCP_ESTABLISHED) |
| 1004 | pipe_grant_credits(sk); |
| 1005 | release_sock(sk); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 1006 | copy: |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1007 | msg->msg_flags |= MSG_EOR; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1008 | if (skb->len > len) |
| 1009 | msg->msg_flags |= MSG_TRUNC; |
| 1010 | else |
| 1011 | len = skb->len; |
| 1012 | |
| 1013 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len); |
| 1014 | if (!err) |
| 1015 | err = (flags & MSG_TRUNC) ? skb->len : len; |
| 1016 | |
| 1017 | skb_free_datagram(sk, skb); |
| 1018 | return err; |
| 1019 | } |
| 1020 | |
| 1021 | static void pep_sock_unhash(struct sock *sk) |
| 1022 | { |
| 1023 | struct pep_sock *pn = pep_sk(sk); |
| 1024 | struct sock *skparent = NULL; |
| 1025 | |
| 1026 | lock_sock(sk); |
| 1027 | if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) { |
| 1028 | skparent = pn->listener; |
| 1029 | sk_del_node_init(sk); |
| 1030 | release_sock(sk); |
| 1031 | |
| 1032 | sk = skparent; |
| 1033 | pn = pep_sk(skparent); |
| 1034 | lock_sock(sk); |
| 1035 | } |
| 1036 | /* Unhash a listening sock only when it is closed |
| 1037 | * and all of its active connected pipes are closed. */ |
| 1038 | if (hlist_empty(&pn->hlist)) |
| 1039 | pn_sock_unhash(&pn->pn_sk.sk); |
| 1040 | release_sock(sk); |
| 1041 | |
| 1042 | if (skparent) |
| 1043 | sock_put(skparent); |
| 1044 | } |
| 1045 | |
| 1046 | static struct proto pep_proto = { |
| 1047 | .close = pep_sock_close, |
| 1048 | .accept = pep_sock_accept, |
| 1049 | .ioctl = pep_ioctl, |
| 1050 | .init = pep_init, |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1051 | .setsockopt = pep_setsockopt, |
| 1052 | .getsockopt = pep_getsockopt, |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1053 | .sendmsg = pep_sendmsg, |
| 1054 | .recvmsg = pep_recvmsg, |
| 1055 | .backlog_rcv = pep_do_rcv, |
| 1056 | .hash = pn_sock_hash, |
| 1057 | .unhash = pep_sock_unhash, |
| 1058 | .get_port = pn_sock_get_port, |
| 1059 | .obj_size = sizeof(struct pep_sock), |
| 1060 | .owner = THIS_MODULE, |
| 1061 | .name = "PNPIPE", |
| 1062 | }; |
| 1063 | |
| 1064 | static struct phonet_protocol pep_pn_proto = { |
| 1065 | .ops = &phonet_stream_ops, |
| 1066 | .prot = &pep_proto, |
| 1067 | .sock_type = SOCK_SEQPACKET, |
| 1068 | }; |
| 1069 | |
| 1070 | static int __init pep_register(void) |
| 1071 | { |
| 1072 | return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto); |
| 1073 | } |
| 1074 | |
| 1075 | static void __exit pep_unregister(void) |
| 1076 | { |
| 1077 | phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto); |
| 1078 | } |
| 1079 | |
| 1080 | module_init(pep_register); |
| 1081 | module_exit(pep_unregister); |
| 1082 | MODULE_AUTHOR("Remi Denis-Courmont, Nokia"); |
| 1083 | MODULE_DESCRIPTION("Phonet pipe protocol"); |
| 1084 | MODULE_LICENSE("GPL"); |
| 1085 | MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE); |