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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 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> |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 35 | #include <net/phonet/gprs.h> |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 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 | |
| 53 | static const struct sockaddr_pn pipe_srv = { |
| 54 | .spn_family = AF_PHONET, |
| 55 | .spn_resource = 0xD9, /* pipe service */ |
| 56 | }; |
| 57 | |
| 58 | #define pep_sb_size(s) (((s) + 5) & ~3) /* 2-bytes head, 32-bits aligned */ |
| 59 | |
| 60 | /* Get the next TLV sub-block. */ |
| 61 | static unsigned char *pep_get_sb(struct sk_buff *skb, u8 *ptype, u8 *plen, |
| 62 | void *buf) |
| 63 | { |
| 64 | void *data = NULL; |
| 65 | struct { |
| 66 | u8 sb_type; |
| 67 | u8 sb_len; |
| 68 | } *ph, h; |
| 69 | int buflen = *plen; |
| 70 | |
| 71 | ph = skb_header_pointer(skb, 0, 2, &h); |
| 72 | if (ph == NULL || ph->sb_len < 2 || !pskb_may_pull(skb, ph->sb_len)) |
| 73 | return NULL; |
| 74 | ph->sb_len -= 2; |
| 75 | *ptype = ph->sb_type; |
| 76 | *plen = ph->sb_len; |
| 77 | |
| 78 | if (buflen > ph->sb_len) |
| 79 | buflen = ph->sb_len; |
| 80 | data = skb_header_pointer(skb, 2, buflen, buf); |
| 81 | __skb_pull(skb, 2 + ph->sb_len); |
| 82 | return data; |
| 83 | } |
| 84 | |
| 85 | static int pep_reply(struct sock *sk, struct sk_buff *oskb, |
| 86 | u8 code, const void *data, int len, gfp_t priority) |
| 87 | { |
| 88 | const struct pnpipehdr *oph = pnp_hdr(oskb); |
| 89 | struct pnpipehdr *ph; |
| 90 | struct sk_buff *skb; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 91 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 92 | const struct phonethdr *hdr = pn_hdr(oskb); |
| 93 | struct sockaddr_pn spn = { |
| 94 | .spn_family = AF_PHONET, |
| 95 | .spn_resource = 0xD9, |
| 96 | .spn_dev = hdr->pn_sdev, |
| 97 | .spn_obj = hdr->pn_sobj, |
| 98 | }; |
| 99 | #endif |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 100 | |
| 101 | skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority); |
| 102 | if (!skb) |
| 103 | return -ENOMEM; |
| 104 | skb_set_owner_w(skb, sk); |
| 105 | |
| 106 | skb_reserve(skb, MAX_PNPIPE_HEADER); |
| 107 | __skb_put(skb, len); |
| 108 | skb_copy_to_linear_data(skb, data, len); |
| 109 | __skb_push(skb, sizeof(*ph)); |
| 110 | skb_reset_transport_header(skb); |
| 111 | ph = pnp_hdr(skb); |
| 112 | ph->utid = oph->utid; |
| 113 | ph->message_id = oph->message_id + 1; /* REQ -> RESP */ |
| 114 | ph->pipe_handle = oph->pipe_handle; |
| 115 | ph->error_code = code; |
| 116 | |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 117 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 118 | return pn_skb_send(sk, skb, &spn); |
| 119 | #else |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 120 | return pn_skb_send(sk, skb, &pipe_srv); |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 121 | #endif |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | #define PAD 0x00 |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 125 | |
| 126 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 127 | static u8 pipe_negotiate_fc(u8 *host_fc, u8 *remote_fc, int len) |
| 128 | { |
| 129 | int i, j; |
| 130 | u8 base_fc, final_fc; |
| 131 | |
| 132 | for (i = 0; i < len; i++) { |
| 133 | base_fc = host_fc[i]; |
| 134 | for (j = 0; j < len; j++) { |
| 135 | if (remote_fc[j] == base_fc) { |
| 136 | final_fc = base_fc; |
| 137 | goto done; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | return -EINVAL; |
| 142 | |
| 143 | done: |
| 144 | return final_fc; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | static int pipe_get_flow_info(struct sock *sk, struct sk_buff *skb, |
| 149 | u8 *pref_rx_fc, u8 *req_tx_fc) |
| 150 | { |
| 151 | struct pnpipehdr *hdr; |
| 152 | u8 n_sb; |
| 153 | |
| 154 | if (!pskb_may_pull(skb, sizeof(*hdr) + 4)) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | hdr = pnp_hdr(skb); |
| 158 | n_sb = hdr->data[4]; |
| 159 | |
| 160 | __skb_pull(skb, sizeof(*hdr) + 4); |
| 161 | while (n_sb > 0) { |
| 162 | u8 type, buf[3], len = sizeof(buf); |
| 163 | u8 *data = pep_get_sb(skb, &type, &len, buf); |
| 164 | |
| 165 | if (data == NULL) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | switch (type) { |
| 169 | case PN_PIPE_SB_REQUIRED_FC_TX: |
| 170 | if (len < 3 || (data[2] | data[3] | data[4]) > 3) |
| 171 | break; |
| 172 | req_tx_fc[0] = data[2]; |
| 173 | req_tx_fc[1] = data[3]; |
| 174 | req_tx_fc[2] = data[4]; |
| 175 | break; |
| 176 | |
| 177 | case PN_PIPE_SB_PREFERRED_FC_RX: |
| 178 | if (len < 3 || (data[2] | data[3] | data[4]) > 3) |
| 179 | break; |
| 180 | pref_rx_fc[0] = data[2]; |
| 181 | pref_rx_fc[1] = data[3]; |
| 182 | pref_rx_fc[2] = data[4]; |
| 183 | break; |
| 184 | |
| 185 | } |
| 186 | n_sb--; |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int pipe_handler_send_req(struct sock *sk, u16 dobj, u8 utid, |
| 192 | u8 msg_id, u8 p_handle, gfp_t priority) |
| 193 | { |
| 194 | int len; |
| 195 | struct pnpipehdr *ph; |
| 196 | struct sk_buff *skb; |
| 197 | struct sockaddr_pn spn = { |
| 198 | .spn_family = AF_PHONET, |
| 199 | .spn_resource = 0xD9, |
| 200 | .spn_dev = pn_dev(dobj), |
| 201 | .spn_obj = pn_obj(dobj), |
| 202 | }; |
| 203 | |
| 204 | static const u8 data[4] = { |
| 205 | PAD, PAD, PAD, PAD, |
| 206 | }; |
| 207 | |
| 208 | switch (msg_id) { |
| 209 | case PNS_PEP_CONNECT_REQ: |
| 210 | len = sizeof(data); |
| 211 | break; |
| 212 | |
| 213 | case PNS_PEP_DISCONNECT_REQ: |
| 214 | case PNS_PEP_ENABLE_REQ: |
| 215 | case PNS_PEP_DISABLE_REQ: |
| 216 | len = 0; |
| 217 | break; |
| 218 | |
| 219 | default: |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
| 223 | skb = alloc_skb(MAX_PNPIPE_HEADER + len, priority); |
| 224 | if (!skb) |
| 225 | return -ENOMEM; |
| 226 | skb_set_owner_w(skb, sk); |
| 227 | |
| 228 | skb_reserve(skb, MAX_PNPIPE_HEADER); |
| 229 | if (len) { |
| 230 | __skb_put(skb, len); |
| 231 | skb_copy_to_linear_data(skb, data, len); |
| 232 | } |
| 233 | __skb_push(skb, sizeof(*ph)); |
| 234 | skb_reset_transport_header(skb); |
| 235 | ph = pnp_hdr(skb); |
| 236 | ph->utid = utid; |
| 237 | ph->message_id = msg_id; |
| 238 | ph->pipe_handle = p_handle; |
| 239 | ph->error_code = PN_PIPE_NO_ERROR; |
| 240 | |
| 241 | return pn_skb_send(sk, skb, &spn); |
| 242 | } |
| 243 | |
| 244 | static int pipe_handler_send_created_ind(struct sock *sk, u16 dobj, |
| 245 | u8 utid, u8 p_handle, u8 msg_id, u8 tx_fc, u8 rx_fc) |
| 246 | { |
| 247 | int err_code; |
| 248 | struct pnpipehdr *ph; |
| 249 | struct sk_buff *skb; |
| 250 | struct sockaddr_pn spn = { |
| 251 | .spn_family = AF_PHONET, |
| 252 | .spn_resource = 0xD9, |
| 253 | .spn_dev = pn_dev(dobj), |
| 254 | .spn_obj = pn_obj(dobj), |
| 255 | }; |
| 256 | |
| 257 | static u8 data[4] = { |
| 258 | 0x03, 0x04, |
| 259 | }; |
| 260 | data[2] = tx_fc; |
| 261 | data[3] = rx_fc; |
| 262 | |
| 263 | /* |
| 264 | * actually, below is number of sub-blocks and not error code. |
| 265 | * Pipe_created_ind message format does not have any |
| 266 | * error code field. However, the Phonet stack will always send |
| 267 | * an error code as part of pnpipehdr. So, use that err_code to |
| 268 | * specify the number of sub-blocks. |
| 269 | */ |
| 270 | err_code = 0x01; |
| 271 | |
| 272 | skb = alloc_skb(MAX_PNPIPE_HEADER + sizeof(data), GFP_ATOMIC); |
| 273 | if (!skb) |
| 274 | return -ENOMEM; |
| 275 | skb_set_owner_w(skb, sk); |
| 276 | |
| 277 | skb_reserve(skb, MAX_PNPIPE_HEADER); |
| 278 | __skb_put(skb, sizeof(data)); |
| 279 | skb_copy_to_linear_data(skb, data, sizeof(data)); |
| 280 | __skb_push(skb, sizeof(*ph)); |
| 281 | skb_reset_transport_header(skb); |
| 282 | ph = pnp_hdr(skb); |
| 283 | ph->utid = utid; |
| 284 | ph->message_id = msg_id; |
| 285 | ph->pipe_handle = p_handle; |
| 286 | ph->error_code = err_code; |
| 287 | |
| 288 | return pn_skb_send(sk, skb, &spn); |
| 289 | } |
| 290 | |
| 291 | static int pipe_handler_send_ind(struct sock *sk, u16 dobj, u8 utid, |
| 292 | u8 p_handle, u8 msg_id) |
| 293 | { |
| 294 | int err_code; |
| 295 | struct pnpipehdr *ph; |
| 296 | struct sk_buff *skb; |
| 297 | struct sockaddr_pn spn = { |
| 298 | .spn_family = AF_PHONET, |
| 299 | .spn_resource = 0xD9, |
| 300 | .spn_dev = pn_dev(dobj), |
| 301 | .spn_obj = pn_obj(dobj), |
| 302 | }; |
| 303 | |
| 304 | /* |
| 305 | * actually, below is a filler. |
| 306 | * Pipe_enabled/disabled_ind message format does not have any |
| 307 | * error code field. However, the Phonet stack will always send |
| 308 | * an error code as part of pnpipehdr. So, use that err_code to |
| 309 | * specify the filler value. |
| 310 | */ |
| 311 | err_code = 0x0; |
| 312 | |
| 313 | skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC); |
| 314 | if (!skb) |
| 315 | return -ENOMEM; |
| 316 | skb_set_owner_w(skb, sk); |
| 317 | |
| 318 | skb_reserve(skb, MAX_PNPIPE_HEADER); |
| 319 | __skb_push(skb, sizeof(*ph)); |
| 320 | skb_reset_transport_header(skb); |
| 321 | ph = pnp_hdr(skb); |
| 322 | ph->utid = utid; |
| 323 | ph->message_id = msg_id; |
| 324 | ph->pipe_handle = p_handle; |
| 325 | ph->error_code = err_code; |
| 326 | |
| 327 | return pn_skb_send(sk, skb, &spn); |
| 328 | } |
| 329 | |
Rémi Denis-Courmont | 03789f2 | 2010-10-08 04:02:02 +0000 | [diff] [blame^] | 330 | static int pipe_handler_enable_pipe(struct sock *sk, int enable) |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 331 | { |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 332 | struct pep_sock *pn = pep_sk(sk); |
Rémi Denis-Courmont | 03789f2 | 2010-10-08 04:02:02 +0000 | [diff] [blame^] | 333 | int utid, req; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 334 | |
Rémi Denis-Courmont | 03789f2 | 2010-10-08 04:02:02 +0000 | [diff] [blame^] | 335 | if (enable) { |
| 336 | utid = PNS_PIPE_ENABLE_UTID; |
| 337 | req = PNS_PEP_ENABLE_REQ; |
| 338 | } else { |
| 339 | utid = PNS_PIPE_DISABLE_UTID; |
| 340 | req = PNS_PEP_DISABLE_REQ; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 341 | } |
Rémi Denis-Courmont | 03789f2 | 2010-10-08 04:02:02 +0000 | [diff] [blame^] | 342 | return pipe_handler_send_req(sk, pn->pn_sk.sobject, utid, req, |
| 343 | pn->pipe_handle, GFP_ATOMIC); |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static int pipe_handler_create_pipe(struct sock *sk, int pipe_handle, int cmd) |
| 347 | { |
| 348 | int ret; |
| 349 | struct pep_sock *pn = pep_sk(sk); |
| 350 | |
| 351 | switch (cmd) { |
| 352 | case PNPIPE_CREATE: |
| 353 | ret = pipe_handler_send_req(sk, pn->pn_sk.sobject, |
| 354 | PNS_PEP_CONNECT_UTID, PNS_PEP_CONNECT_REQ, |
| 355 | pipe_handle, GFP_ATOMIC); |
| 356 | break; |
| 357 | |
| 358 | case PNPIPE_DESTROY: |
| 359 | ret = pipe_handler_send_req(sk, pn->remote_pep, |
| 360 | PNS_PEP_DISCONNECT_UTID, |
| 361 | PNS_PEP_DISCONNECT_REQ, |
| 362 | pn->pipe_handle, GFP_ATOMIC); |
| 363 | break; |
| 364 | |
| 365 | default: |
| 366 | ret = -EINVAL; |
| 367 | } |
| 368 | |
| 369 | return ret; |
| 370 | } |
| 371 | #endif |
| 372 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 373 | static int pep_accept_conn(struct sock *sk, struct sk_buff *skb) |
| 374 | { |
| 375 | static const u8 data[20] = { |
| 376 | PAD, PAD, PAD, 2 /* sub-blocks */, |
| 377 | PN_PIPE_SB_REQUIRED_FC_TX, pep_sb_size(5), 3, PAD, |
| 378 | PN_MULTI_CREDIT_FLOW_CONTROL, |
| 379 | PN_ONE_CREDIT_FLOW_CONTROL, |
| 380 | PN_LEGACY_FLOW_CONTROL, |
| 381 | PAD, |
| 382 | PN_PIPE_SB_PREFERRED_FC_RX, pep_sb_size(5), 3, PAD, |
| 383 | PN_MULTI_CREDIT_FLOW_CONTROL, |
| 384 | PN_ONE_CREDIT_FLOW_CONTROL, |
| 385 | PN_LEGACY_FLOW_CONTROL, |
| 386 | PAD, |
| 387 | }; |
| 388 | |
| 389 | might_sleep(); |
| 390 | return pep_reply(sk, skb, PN_PIPE_NO_ERROR, data, sizeof(data), |
| 391 | GFP_KERNEL); |
| 392 | } |
| 393 | |
| 394 | static int pep_reject_conn(struct sock *sk, struct sk_buff *skb, u8 code) |
| 395 | { |
| 396 | static const u8 data[4] = { PAD, PAD, PAD, 0 /* sub-blocks */ }; |
| 397 | WARN_ON(code == PN_PIPE_NO_ERROR); |
| 398 | return pep_reply(sk, skb, code, data, sizeof(data), GFP_ATOMIC); |
| 399 | } |
| 400 | |
| 401 | /* Control requests are not sent by the pipe service and have a specific |
| 402 | * message format. */ |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 403 | static int pep_ctrlreq_error(struct sock *sk, struct sk_buff *oskb, u8 code, |
| 404 | gfp_t priority) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 405 | { |
| 406 | const struct pnpipehdr *oph = pnp_hdr(oskb); |
| 407 | struct sk_buff *skb; |
| 408 | struct pnpipehdr *ph; |
| 409 | struct sockaddr_pn dst; |
| 410 | |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 411 | skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 412 | if (!skb) |
| 413 | return -ENOMEM; |
| 414 | skb_set_owner_w(skb, sk); |
| 415 | |
| 416 | skb_reserve(skb, MAX_PHONET_HEADER); |
| 417 | ph = (struct pnpipehdr *)skb_put(skb, sizeof(*ph) + 4); |
| 418 | |
| 419 | ph->utid = oph->utid; |
| 420 | ph->message_id = PNS_PEP_CTRL_RESP; |
| 421 | ph->pipe_handle = oph->pipe_handle; |
| 422 | ph->data[0] = oph->data[1]; /* CTRL id */ |
| 423 | ph->data[1] = oph->data[0]; /* PEP type */ |
| 424 | ph->data[2] = code; /* error code, at an usual offset */ |
| 425 | ph->data[3] = PAD; |
| 426 | ph->data[4] = PAD; |
| 427 | |
| 428 | pn_skb_get_src_sockaddr(oskb, &dst); |
| 429 | return pn_skb_send(sk, skb, &dst); |
| 430 | } |
| 431 | |
| 432 | static int pipe_snd_status(struct sock *sk, u8 type, u8 status, gfp_t priority) |
| 433 | { |
| 434 | struct pep_sock *pn = pep_sk(sk); |
| 435 | struct pnpipehdr *ph; |
| 436 | struct sk_buff *skb; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 437 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 438 | struct sockaddr_pn spn = { |
| 439 | .spn_family = AF_PHONET, |
| 440 | .spn_resource = 0xD9, |
| 441 | .spn_dev = pn_dev(pn->remote_pep), |
| 442 | .spn_obj = pn_obj(pn->remote_pep), |
| 443 | }; |
| 444 | #endif |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 445 | |
| 446 | skb = alloc_skb(MAX_PNPIPE_HEADER + 4, priority); |
| 447 | if (!skb) |
| 448 | return -ENOMEM; |
| 449 | skb_set_owner_w(skb, sk); |
| 450 | |
| 451 | skb_reserve(skb, MAX_PNPIPE_HEADER + 4); |
| 452 | __skb_push(skb, sizeof(*ph) + 4); |
| 453 | skb_reset_transport_header(skb); |
| 454 | ph = pnp_hdr(skb); |
| 455 | ph->utid = 0; |
| 456 | ph->message_id = PNS_PEP_STATUS_IND; |
| 457 | ph->pipe_handle = pn->pipe_handle; |
| 458 | ph->pep_type = PN_PEP_TYPE_COMMON; |
| 459 | ph->data[1] = type; |
| 460 | ph->data[2] = PAD; |
| 461 | ph->data[3] = PAD; |
| 462 | ph->data[4] = status; |
| 463 | |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 464 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 465 | return pn_skb_send(sk, skb, &spn); |
| 466 | #else |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 467 | return pn_skb_send(sk, skb, &pipe_srv); |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 468 | #endif |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* Send our RX flow control information to the sender. |
| 472 | * Socket must be locked. */ |
| 473 | static void pipe_grant_credits(struct sock *sk) |
| 474 | { |
| 475 | struct pep_sock *pn = pep_sk(sk); |
| 476 | |
| 477 | BUG_ON(sk->sk_state != TCP_ESTABLISHED); |
| 478 | |
| 479 | switch (pn->rx_fc) { |
| 480 | case PN_LEGACY_FLOW_CONTROL: /* TODO */ |
| 481 | break; |
| 482 | case PN_ONE_CREDIT_FLOW_CONTROL: |
| 483 | pipe_snd_status(sk, PN_PEP_IND_FLOW_CONTROL, |
| 484 | PEP_IND_READY, GFP_ATOMIC); |
| 485 | pn->rx_credits = 1; |
| 486 | break; |
| 487 | case PN_MULTI_CREDIT_FLOW_CONTROL: |
| 488 | if ((pn->rx_credits + CREDITS_THR) > CREDITS_MAX) |
| 489 | break; |
| 490 | if (pipe_snd_status(sk, PN_PEP_IND_ID_MCFC_GRANT_CREDITS, |
| 491 | CREDITS_MAX - pn->rx_credits, |
| 492 | GFP_ATOMIC) == 0) |
| 493 | pn->rx_credits = CREDITS_MAX; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static int pipe_rcv_status(struct sock *sk, struct sk_buff *skb) |
| 499 | { |
| 500 | struct pep_sock *pn = pep_sk(sk); |
Kumar Sanghvi | a91e7d4 | 2010-09-27 23:10:42 +0000 | [diff] [blame] | 501 | struct pnpipehdr *hdr; |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 502 | int wake = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 503 | |
| 504 | if (!pskb_may_pull(skb, sizeof(*hdr) + 4)) |
| 505 | return -EINVAL; |
| 506 | |
Kumar Sanghvi | a91e7d4 | 2010-09-27 23:10:42 +0000 | [diff] [blame] | 507 | hdr = pnp_hdr(skb); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 508 | if (hdr->data[0] != PN_PEP_TYPE_COMMON) { |
| 509 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP type: %u\n", |
| 510 | (unsigned)hdr->data[0]); |
| 511 | return -EOPNOTSUPP; |
| 512 | } |
| 513 | |
| 514 | switch (hdr->data[1]) { |
| 515 | case PN_PEP_IND_FLOW_CONTROL: |
| 516 | switch (pn->tx_fc) { |
| 517 | case PN_LEGACY_FLOW_CONTROL: |
| 518 | switch (hdr->data[4]) { |
| 519 | case PEP_IND_BUSY: |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 520 | atomic_set(&pn->tx_credits, 0); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 521 | break; |
| 522 | case PEP_IND_READY: |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 523 | atomic_set(&pn->tx_credits, wake = 1); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | break; |
| 527 | case PN_ONE_CREDIT_FLOW_CONTROL: |
| 528 | if (hdr->data[4] == PEP_IND_READY) |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 529 | atomic_set(&pn->tx_credits, wake = 1); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | break; |
| 533 | |
| 534 | case PN_PEP_IND_ID_MCFC_GRANT_CREDITS: |
| 535 | if (pn->tx_fc != PN_MULTI_CREDIT_FLOW_CONTROL) |
| 536 | break; |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 537 | atomic_add(wake = hdr->data[4], &pn->tx_credits); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 538 | break; |
| 539 | |
| 540 | default: |
| 541 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP indication: %u\n", |
| 542 | (unsigned)hdr->data[1]); |
| 543 | return -EOPNOTSUPP; |
| 544 | } |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 545 | if (wake) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 546 | sk->sk_write_space(sk); |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int pipe_rcv_created(struct sock *sk, struct sk_buff *skb) |
| 551 | { |
| 552 | struct pep_sock *pn = pep_sk(sk); |
| 553 | struct pnpipehdr *hdr = pnp_hdr(skb); |
| 554 | u8 n_sb = hdr->data[0]; |
| 555 | |
| 556 | pn->rx_fc = pn->tx_fc = PN_LEGACY_FLOW_CONTROL; |
| 557 | __skb_pull(skb, sizeof(*hdr)); |
| 558 | while (n_sb > 0) { |
| 559 | u8 type, buf[2], len = sizeof(buf); |
| 560 | u8 *data = pep_get_sb(skb, &type, &len, buf); |
| 561 | |
| 562 | if (data == NULL) |
| 563 | return -EINVAL; |
| 564 | switch (type) { |
| 565 | case PN_PIPE_SB_NEGOTIATED_FC: |
| 566 | if (len < 2 || (data[0] | data[1]) > 3) |
| 567 | break; |
| 568 | pn->tx_fc = data[0] & 3; |
| 569 | pn->rx_fc = data[1] & 3; |
| 570 | break; |
| 571 | } |
| 572 | n_sb--; |
| 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /* Queue an skb to a connected sock. |
| 578 | * Socket lock must be held. */ |
| 579 | static int pipe_do_rcv(struct sock *sk, struct sk_buff *skb) |
| 580 | { |
| 581 | struct pep_sock *pn = pep_sk(sk); |
| 582 | struct pnpipehdr *hdr = pnp_hdr(skb); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 583 | struct sk_buff_head *queue; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 584 | int err = 0; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 585 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 586 | struct phonethdr *ph = pn_hdr(skb); |
| 587 | static u8 host_pref_rx_fc[3], host_req_tx_fc[3]; |
| 588 | u8 remote_pref_rx_fc[3], remote_req_tx_fc[3]; |
| 589 | u8 negotiated_rx_fc, negotiated_tx_fc; |
| 590 | #endif |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 591 | |
| 592 | BUG_ON(sk->sk_state == TCP_CLOSE_WAIT); |
| 593 | |
| 594 | switch (hdr->message_id) { |
| 595 | case PNS_PEP_CONNECT_REQ: |
| 596 | pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE); |
| 597 | break; |
| 598 | |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 599 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 600 | case PNS_PEP_CONNECT_RESP: |
| 601 | if ((ph->pn_sdev == pn_dev(pn->remote_pep)) && |
| 602 | (ph->pn_sobj == pn_obj(pn->remote_pep))) { |
| 603 | pipe_get_flow_info(sk, skb, remote_pref_rx_fc, |
| 604 | remote_req_tx_fc); |
| 605 | |
| 606 | negotiated_tx_fc = pipe_negotiate_fc(remote_req_tx_fc, |
| 607 | host_pref_rx_fc, |
| 608 | sizeof(host_pref_rx_fc)); |
| 609 | negotiated_rx_fc = pipe_negotiate_fc(host_req_tx_fc, |
| 610 | remote_pref_rx_fc, |
| 611 | sizeof(host_pref_rx_fc)); |
| 612 | |
| 613 | pn->pipe_state = PIPE_DISABLED; |
| 614 | pipe_handler_send_created_ind(sk, pn->remote_pep, |
| 615 | PNS_PIPE_CREATED_IND_UTID, |
| 616 | pn->pipe_handle, PNS_PIPE_CREATED_IND, |
| 617 | negotiated_tx_fc, negotiated_rx_fc); |
| 618 | pipe_handler_send_created_ind(sk, pn->pn_sk.sobject, |
| 619 | PNS_PIPE_CREATED_IND_UTID, |
| 620 | pn->pipe_handle, PNS_PIPE_CREATED_IND, |
| 621 | negotiated_tx_fc, negotiated_rx_fc); |
| 622 | } else { |
| 623 | pipe_handler_send_req(sk, pn->remote_pep, |
| 624 | PNS_PEP_CONNECT_UTID, |
| 625 | PNS_PEP_CONNECT_REQ, pn->pipe_handle, |
| 626 | GFP_ATOMIC); |
| 627 | pipe_get_flow_info(sk, skb, host_pref_rx_fc, |
| 628 | host_req_tx_fc); |
| 629 | } |
| 630 | break; |
| 631 | #endif |
| 632 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 633 | case PNS_PEP_DISCONNECT_REQ: |
| 634 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 635 | sk->sk_state = TCP_CLOSE_WAIT; |
| 636 | if (!sock_flag(sk, SOCK_DEAD)) |
| 637 | sk->sk_state_change(sk); |
| 638 | break; |
| 639 | |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 640 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 641 | case PNS_PEP_DISCONNECT_RESP: |
| 642 | pn->pipe_state = PIPE_IDLE; |
| 643 | pipe_handler_send_req(sk, pn->pn_sk.sobject, |
| 644 | PNS_PEP_DISCONNECT_UTID, |
| 645 | PNS_PEP_DISCONNECT_REQ, pn->pipe_handle, |
| 646 | GFP_KERNEL); |
| 647 | break; |
| 648 | #endif |
| 649 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 650 | case PNS_PEP_ENABLE_REQ: |
| 651 | /* Wait for PNS_PIPE_(ENABLED|REDIRECTED)_IND */ |
| 652 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 653 | break; |
| 654 | |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 655 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 656 | case PNS_PEP_ENABLE_RESP: |
| 657 | if ((ph->pn_sdev == pn_dev(pn->remote_pep)) && |
| 658 | (ph->pn_sobj == pn_obj(pn->remote_pep))) { |
| 659 | pn->pipe_state = PIPE_ENABLED; |
| 660 | pipe_handler_send_ind(sk, pn->remote_pep, |
| 661 | PNS_PIPE_ENABLED_IND_UTID, |
| 662 | pn->pipe_handle, PNS_PIPE_ENABLED_IND); |
| 663 | pipe_handler_send_ind(sk, pn->pn_sk.sobject, |
| 664 | PNS_PIPE_ENABLED_IND_UTID, |
| 665 | pn->pipe_handle, PNS_PIPE_ENABLED_IND); |
| 666 | } else |
| 667 | pipe_handler_send_req(sk, pn->remote_pep, |
| 668 | PNS_PIPE_ENABLE_UTID, |
| 669 | PNS_PEP_ENABLE_REQ, pn->pipe_handle, |
| 670 | GFP_KERNEL); |
| 671 | |
| 672 | break; |
| 673 | #endif |
| 674 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 675 | case PNS_PEP_RESET_REQ: |
| 676 | switch (hdr->state_after_reset) { |
| 677 | case PN_PIPE_DISABLE: |
| 678 | pn->init_enable = 0; |
| 679 | break; |
| 680 | case PN_PIPE_ENABLE: |
| 681 | pn->init_enable = 1; |
| 682 | break; |
| 683 | default: /* not allowed to send an error here!? */ |
| 684 | err = -EINVAL; |
| 685 | goto out; |
| 686 | } |
| 687 | /* fall through */ |
| 688 | case PNS_PEP_DISABLE_REQ: |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 689 | atomic_set(&pn->tx_credits, 0); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 690 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 691 | break; |
| 692 | |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 693 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 694 | case PNS_PEP_DISABLE_RESP: |
| 695 | if ((ph->pn_sdev == pn_dev(pn->remote_pep)) && |
| 696 | (ph->pn_sobj == pn_obj(pn->remote_pep))) { |
| 697 | pn->pipe_state = PIPE_DISABLED; |
| 698 | pipe_handler_send_ind(sk, pn->remote_pep, |
| 699 | PNS_PIPE_DISABLED_IND_UTID, |
| 700 | pn->pipe_handle, |
| 701 | PNS_PIPE_DISABLED_IND); |
| 702 | pipe_handler_send_ind(sk, pn->pn_sk.sobject, |
| 703 | PNS_PIPE_DISABLED_IND_UTID, |
| 704 | pn->pipe_handle, |
| 705 | PNS_PIPE_DISABLED_IND); |
| 706 | } else |
| 707 | pipe_handler_send_req(sk, pn->remote_pep, |
| 708 | PNS_PIPE_DISABLE_UTID, |
| 709 | PNS_PEP_DISABLE_REQ, pn->pipe_handle, |
| 710 | GFP_KERNEL); |
| 711 | break; |
| 712 | #endif |
| 713 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 714 | case PNS_PEP_CTRL_REQ: |
Rémi Denis-Courmont | 2e2fb4b | 2009-07-21 01:57:59 +0000 | [diff] [blame] | 715 | if (skb_queue_len(&pn->ctrlreq_queue) >= PNPIPE_CTRLREQ_MAX) { |
| 716 | atomic_inc(&sk->sk_drops); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 717 | break; |
Rémi Denis-Courmont | 2e2fb4b | 2009-07-21 01:57:59 +0000 | [diff] [blame] | 718 | } |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 719 | __skb_pull(skb, 4); |
| 720 | queue = &pn->ctrlreq_queue; |
| 721 | goto queue; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 722 | |
Rémi Denis-Courmont | fc6a110 | 2010-01-04 02:02:47 +0000 | [diff] [blame] | 723 | case PNS_PIPE_ALIGNED_DATA: |
| 724 | __skb_pull(skb, 1); |
| 725 | /* fall through */ |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 726 | case PNS_PIPE_DATA: |
| 727 | __skb_pull(skb, 3); /* Pipe data header */ |
| 728 | if (!pn_flow_safe(pn->rx_fc)) { |
| 729 | err = sock_queue_rcv_skb(sk, skb); |
| 730 | if (!err) |
| 731 | return 0; |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | if (pn->rx_credits == 0) { |
Rémi Denis-Courmont | 2e2fb4b | 2009-07-21 01:57:59 +0000 | [diff] [blame] | 736 | atomic_inc(&sk->sk_drops); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 737 | err = -ENOBUFS; |
| 738 | break; |
| 739 | } |
| 740 | pn->rx_credits--; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 741 | queue = &sk->sk_receive_queue; |
| 742 | goto queue; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 743 | |
| 744 | case PNS_PEP_STATUS_IND: |
| 745 | pipe_rcv_status(sk, skb); |
| 746 | break; |
| 747 | |
| 748 | case PNS_PIPE_REDIRECTED_IND: |
| 749 | err = pipe_rcv_created(sk, skb); |
| 750 | break; |
| 751 | |
| 752 | case PNS_PIPE_CREATED_IND: |
| 753 | err = pipe_rcv_created(sk, skb); |
| 754 | if (err) |
| 755 | break; |
| 756 | /* fall through */ |
| 757 | case PNS_PIPE_RESET_IND: |
| 758 | if (!pn->init_enable) |
| 759 | break; |
| 760 | /* fall through */ |
| 761 | case PNS_PIPE_ENABLED_IND: |
| 762 | if (!pn_flow_safe(pn->tx_fc)) { |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 763 | atomic_set(&pn->tx_credits, 1); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 764 | sk->sk_write_space(sk); |
| 765 | } |
| 766 | if (sk->sk_state == TCP_ESTABLISHED) |
| 767 | break; /* Nothing to do */ |
| 768 | sk->sk_state = TCP_ESTABLISHED; |
| 769 | pipe_grant_credits(sk); |
| 770 | break; |
| 771 | |
| 772 | case PNS_PIPE_DISABLED_IND: |
| 773 | sk->sk_state = TCP_SYN_RECV; |
| 774 | pn->rx_credits = 0; |
| 775 | break; |
| 776 | |
| 777 | default: |
| 778 | LIMIT_NETDEBUG(KERN_DEBUG"Phonet unknown PEP message: %u\n", |
| 779 | hdr->message_id); |
| 780 | err = -EINVAL; |
| 781 | } |
| 782 | out: |
| 783 | kfree_skb(skb); |
| 784 | return err; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 785 | |
| 786 | queue: |
| 787 | skb->dev = NULL; |
| 788 | skb_set_owner_r(skb, sk); |
| 789 | err = skb->len; |
| 790 | skb_queue_tail(queue, skb); |
| 791 | if (!sock_flag(sk, SOCK_DEAD)) |
| 792 | sk->sk_data_ready(sk, err); |
| 793 | return 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* Destroy connected sock. */ |
| 797 | static void pipe_destruct(struct sock *sk) |
| 798 | { |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 799 | struct pep_sock *pn = pep_sk(sk); |
| 800 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 801 | skb_queue_purge(&sk->sk_receive_queue); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 802 | skb_queue_purge(&pn->ctrlreq_queue); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static int pep_connreq_rcv(struct sock *sk, struct sk_buff *skb) |
| 806 | { |
| 807 | struct sock *newsk; |
| 808 | struct pep_sock *newpn, *pn = pep_sk(sk); |
| 809 | struct pnpipehdr *hdr; |
| 810 | struct sockaddr_pn dst; |
| 811 | u16 peer_type; |
| 812 | u8 pipe_handle, enabled, n_sb; |
Rémi Denis-Courmont | fea93ec | 2010-01-04 02:02:48 +0000 | [diff] [blame] | 813 | u8 aligned = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 814 | |
| 815 | if (!pskb_pull(skb, sizeof(*hdr) + 4)) |
| 816 | return -EINVAL; |
| 817 | |
| 818 | hdr = pnp_hdr(skb); |
| 819 | pipe_handle = hdr->pipe_handle; |
| 820 | switch (hdr->state_after_connect) { |
| 821 | case PN_PIPE_DISABLE: |
| 822 | enabled = 0; |
| 823 | break; |
| 824 | case PN_PIPE_ENABLE: |
| 825 | enabled = 1; |
| 826 | break; |
| 827 | default: |
| 828 | pep_reject_conn(sk, skb, PN_PIPE_ERR_INVALID_PARAM); |
| 829 | return -EINVAL; |
| 830 | } |
| 831 | peer_type = hdr->other_pep_type << 8; |
| 832 | |
| 833 | if (unlikely(sk->sk_state != TCP_LISTEN) || sk_acceptq_is_full(sk)) { |
| 834 | pep_reject_conn(sk, skb, PN_PIPE_ERR_PEP_IN_USE); |
| 835 | return -ENOBUFS; |
| 836 | } |
| 837 | |
| 838 | /* Parse sub-blocks (options) */ |
| 839 | n_sb = hdr->data[4]; |
| 840 | while (n_sb > 0) { |
| 841 | u8 type, buf[1], len = sizeof(buf); |
| 842 | const u8 *data = pep_get_sb(skb, &type, &len, buf); |
| 843 | |
| 844 | if (data == NULL) |
| 845 | return -EINVAL; |
| 846 | switch (type) { |
| 847 | case PN_PIPE_SB_CONNECT_REQ_PEP_SUB_TYPE: |
| 848 | if (len < 1) |
| 849 | return -EINVAL; |
| 850 | peer_type = (peer_type & 0xff00) | data[0]; |
| 851 | break; |
Rémi Denis-Courmont | fea93ec | 2010-01-04 02:02:48 +0000 | [diff] [blame] | 852 | case PN_PIPE_SB_ALIGNED_DATA: |
| 853 | aligned = data[0] != 0; |
| 854 | break; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 855 | } |
| 856 | n_sb--; |
| 857 | } |
| 858 | |
| 859 | skb = skb_clone(skb, GFP_ATOMIC); |
| 860 | if (!skb) |
| 861 | return -ENOMEM; |
| 862 | |
| 863 | /* Create a new to-be-accepted sock */ |
| 864 | newsk = sk_alloc(sock_net(sk), PF_PHONET, GFP_ATOMIC, sk->sk_prot); |
| 865 | if (!newsk) { |
| 866 | kfree_skb(skb); |
| 867 | return -ENOMEM; |
| 868 | } |
| 869 | sock_init_data(NULL, newsk); |
| 870 | newsk->sk_state = TCP_SYN_RECV; |
| 871 | newsk->sk_backlog_rcv = pipe_do_rcv; |
| 872 | newsk->sk_protocol = sk->sk_protocol; |
| 873 | newsk->sk_destruct = pipe_destruct; |
| 874 | |
| 875 | newpn = pep_sk(newsk); |
| 876 | pn_skb_get_dst_sockaddr(skb, &dst); |
| 877 | newpn->pn_sk.sobject = pn_sockaddr_get_object(&dst); |
| 878 | newpn->pn_sk.resource = pn->pn_sk.resource; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 879 | skb_queue_head_init(&newpn->ctrlreq_queue); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 880 | newpn->pipe_handle = pipe_handle; |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 881 | atomic_set(&newpn->tx_credits, 0); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 882 | newpn->peer_type = peer_type; |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 883 | newpn->rx_credits = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 884 | newpn->rx_fc = newpn->tx_fc = PN_LEGACY_FLOW_CONTROL; |
| 885 | newpn->init_enable = enabled; |
Rémi Denis-Courmont | fea93ec | 2010-01-04 02:02:48 +0000 | [diff] [blame] | 886 | newpn->aligned = aligned; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 887 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 888 | newpn->remote_pep = pn->remote_pep; |
| 889 | #endif |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 890 | |
| 891 | BUG_ON(!skb_queue_empty(&newsk->sk_receive_queue)); |
| 892 | skb_queue_head(&newsk->sk_receive_queue, skb); |
| 893 | if (!sock_flag(sk, SOCK_DEAD)) |
| 894 | sk->sk_data_ready(sk, 0); |
| 895 | |
| 896 | sk_acceptq_added(sk); |
| 897 | sk_add_node(newsk, &pn->ackq); |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /* Listening sock must be locked */ |
| 902 | static struct sock *pep_find_pipe(const struct hlist_head *hlist, |
| 903 | const struct sockaddr_pn *dst, |
| 904 | u8 pipe_handle) |
| 905 | { |
| 906 | struct hlist_node *node; |
| 907 | struct sock *sknode; |
| 908 | u16 dobj = pn_sockaddr_get_object(dst); |
| 909 | |
| 910 | sk_for_each(sknode, node, hlist) { |
| 911 | struct pep_sock *pnnode = pep_sk(sknode); |
| 912 | |
| 913 | /* Ports match, but addresses might not: */ |
| 914 | if (pnnode->pn_sk.sobject != dobj) |
| 915 | continue; |
| 916 | if (pnnode->pipe_handle != pipe_handle) |
| 917 | continue; |
| 918 | if (sknode->sk_state == TCP_CLOSE_WAIT) |
| 919 | continue; |
| 920 | |
| 921 | sock_hold(sknode); |
| 922 | return sknode; |
| 923 | } |
| 924 | return NULL; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * Deliver an skb to a listening sock. |
| 929 | * Socket lock must be held. |
| 930 | * We then queue the skb to the right connected sock (if any). |
| 931 | */ |
| 932 | static int pep_do_rcv(struct sock *sk, struct sk_buff *skb) |
| 933 | { |
| 934 | struct pep_sock *pn = pep_sk(sk); |
| 935 | struct sock *sknode; |
Rémi Denis-Courmont | 2ddc1ac | 2009-02-10 17:14:50 -0800 | [diff] [blame] | 936 | struct pnpipehdr *hdr; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 937 | struct sockaddr_pn dst; |
| 938 | int err = NET_RX_SUCCESS; |
| 939 | u8 pipe_handle; |
| 940 | |
| 941 | if (!pskb_may_pull(skb, sizeof(*hdr))) |
| 942 | goto drop; |
| 943 | |
| 944 | hdr = pnp_hdr(skb); |
| 945 | pipe_handle = hdr->pipe_handle; |
| 946 | if (pipe_handle == PN_PIPE_INVALID_HANDLE) |
| 947 | goto drop; |
| 948 | |
| 949 | pn_skb_get_dst_sockaddr(skb, &dst); |
| 950 | |
| 951 | /* Look for an existing pipe handle */ |
| 952 | sknode = pep_find_pipe(&pn->hlist, &dst, pipe_handle); |
| 953 | if (sknode) |
| 954 | return sk_receive_skb(sknode, skb, 1); |
| 955 | |
| 956 | /* Look for a pipe handle pending accept */ |
| 957 | sknode = pep_find_pipe(&pn->ackq, &dst, pipe_handle); |
| 958 | if (sknode) { |
| 959 | sock_put(sknode); |
| 960 | if (net_ratelimit()) |
| 961 | printk(KERN_WARNING"Phonet unconnected PEP ignored"); |
| 962 | err = NET_RX_DROP; |
| 963 | goto drop; |
| 964 | } |
| 965 | |
| 966 | switch (hdr->message_id) { |
| 967 | case PNS_PEP_CONNECT_REQ: |
| 968 | err = pep_connreq_rcv(sk, skb); |
| 969 | break; |
| 970 | |
| 971 | case PNS_PEP_DISCONNECT_REQ: |
| 972 | pep_reply(sk, skb, PN_PIPE_NO_ERROR, NULL, 0, GFP_ATOMIC); |
| 973 | break; |
| 974 | |
| 975 | case PNS_PEP_CTRL_REQ: |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 976 | 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] | 977 | break; |
| 978 | |
| 979 | case PNS_PEP_RESET_REQ: |
| 980 | case PNS_PEP_ENABLE_REQ: |
| 981 | case PNS_PEP_DISABLE_REQ: |
| 982 | /* invalid handle is not even allowed here! */ |
| 983 | default: |
| 984 | err = NET_RX_DROP; |
| 985 | } |
| 986 | drop: |
| 987 | kfree_skb(skb); |
| 988 | return err; |
| 989 | } |
| 990 | |
Rémi Denis-Courmont | 6482f55 | 2010-09-15 12:19:53 +0000 | [diff] [blame] | 991 | static int pipe_do_remove(struct sock *sk) |
| 992 | { |
| 993 | struct pep_sock *pn = pep_sk(sk); |
| 994 | struct pnpipehdr *ph; |
| 995 | struct sk_buff *skb; |
| 996 | |
| 997 | skb = alloc_skb(MAX_PNPIPE_HEADER, GFP_KERNEL); |
| 998 | if (!skb) |
| 999 | return -ENOMEM; |
| 1000 | |
| 1001 | skb_reserve(skb, MAX_PNPIPE_HEADER); |
| 1002 | __skb_push(skb, sizeof(*ph)); |
| 1003 | skb_reset_transport_header(skb); |
| 1004 | ph = pnp_hdr(skb); |
| 1005 | ph->utid = 0; |
| 1006 | ph->message_id = PNS_PIPE_REMOVE_REQ; |
| 1007 | ph->pipe_handle = pn->pipe_handle; |
| 1008 | ph->data[0] = PAD; |
| 1009 | |
| 1010 | return pn_skb_send(sk, skb, &pipe_srv); |
| 1011 | } |
| 1012 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1013 | /* associated socket ceases to exist */ |
| 1014 | static void pep_sock_close(struct sock *sk, long timeout) |
| 1015 | { |
| 1016 | struct pep_sock *pn = pep_sk(sk); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1017 | int ifindex = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1018 | |
Rémi Denis-Courmont | e513480 | 2010-05-25 16:08:39 -0700 | [diff] [blame] | 1019 | sock_hold(sk); /* keep a reference after sk_common_release() */ |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1020 | sk_common_release(sk); |
| 1021 | |
| 1022 | lock_sock(sk); |
| 1023 | if (sk->sk_state == TCP_LISTEN) { |
| 1024 | /* Destroy the listen queue */ |
| 1025 | struct sock *sknode; |
| 1026 | struct hlist_node *p, *n; |
| 1027 | |
| 1028 | sk_for_each_safe(sknode, p, n, &pn->ackq) |
| 1029 | sk_del_node_init(sknode); |
| 1030 | sk->sk_state = TCP_CLOSE; |
Rémi Denis-Courmont | 6482f55 | 2010-09-15 12:19:53 +0000 | [diff] [blame] | 1031 | } else if ((1 << sk->sk_state) & (TCPF_SYN_RECV|TCPF_ESTABLISHED)) |
| 1032 | /* Forcefully remove dangling Phonet pipe */ |
| 1033 | pipe_do_remove(sk); |
| 1034 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1035 | ifindex = pn->ifindex; |
| 1036 | pn->ifindex = 0; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1037 | release_sock(sk); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1038 | |
| 1039 | if (ifindex) |
| 1040 | gprs_detach(sk); |
Rémi Denis-Courmont | e513480 | 2010-05-25 16:08:39 -0700 | [diff] [blame] | 1041 | sock_put(sk); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static int pep_wait_connreq(struct sock *sk, int noblock) |
| 1045 | { |
| 1046 | struct task_struct *tsk = current; |
| 1047 | struct pep_sock *pn = pep_sk(sk); |
| 1048 | long timeo = sock_rcvtimeo(sk, noblock); |
| 1049 | |
| 1050 | for (;;) { |
| 1051 | DEFINE_WAIT(wait); |
| 1052 | |
| 1053 | if (sk->sk_state != TCP_LISTEN) |
| 1054 | return -EINVAL; |
| 1055 | if (!hlist_empty(&pn->ackq)) |
| 1056 | break; |
| 1057 | if (!timeo) |
| 1058 | return -EWOULDBLOCK; |
| 1059 | if (signal_pending(tsk)) |
| 1060 | return sock_intr_errno(timeo); |
| 1061 | |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1062 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1063 | TASK_INTERRUPTIBLE); |
| 1064 | release_sock(sk); |
| 1065 | timeo = schedule_timeout(timeo); |
| 1066 | lock_sock(sk); |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1067 | finish_wait(sk_sleep(sk), &wait); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | static struct sock *pep_sock_accept(struct sock *sk, int flags, int *errp) |
| 1074 | { |
| 1075 | struct pep_sock *pn = pep_sk(sk); |
| 1076 | struct sock *newsk = NULL; |
| 1077 | struct sk_buff *oskb; |
| 1078 | int err; |
| 1079 | |
| 1080 | lock_sock(sk); |
| 1081 | err = pep_wait_connreq(sk, flags & O_NONBLOCK); |
| 1082 | if (err) |
| 1083 | goto out; |
| 1084 | |
| 1085 | newsk = __sk_head(&pn->ackq); |
| 1086 | |
| 1087 | oskb = skb_dequeue(&newsk->sk_receive_queue); |
| 1088 | err = pep_accept_conn(newsk, oskb); |
| 1089 | if (err) { |
| 1090 | skb_queue_head(&newsk->sk_receive_queue, oskb); |
| 1091 | newsk = NULL; |
| 1092 | goto out; |
| 1093 | } |
Rémi Denis-Courmont | 635f081 | 2010-07-07 20:56:53 +0000 | [diff] [blame] | 1094 | kfree_skb(oskb); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1095 | |
| 1096 | sock_hold(sk); |
| 1097 | pep_sk(newsk)->listener = sk; |
| 1098 | |
| 1099 | sock_hold(newsk); |
| 1100 | sk_del_node_init(newsk); |
| 1101 | sk_acceptq_removed(sk); |
| 1102 | sk_add_node(newsk, &pn->hlist); |
| 1103 | __sock_put(newsk); |
| 1104 | |
| 1105 | out: |
| 1106 | release_sock(sk); |
| 1107 | *errp = err; |
| 1108 | return newsk; |
| 1109 | } |
| 1110 | |
| 1111 | static int pep_ioctl(struct sock *sk, int cmd, unsigned long arg) |
| 1112 | { |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 1113 | struct pep_sock *pn = pep_sk(sk); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1114 | int answ; |
| 1115 | |
| 1116 | switch (cmd) { |
| 1117 | case SIOCINQ: |
| 1118 | if (sk->sk_state == TCP_LISTEN) |
| 1119 | return -EINVAL; |
| 1120 | |
| 1121 | lock_sock(sk); |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1122 | if (sock_flag(sk, SOCK_URGINLINE) && |
| 1123 | !skb_queue_empty(&pn->ctrlreq_queue)) |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 1124 | answ = skb_peek(&pn->ctrlreq_queue)->len; |
| 1125 | else if (!skb_queue_empty(&sk->sk_receive_queue)) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1126 | answ = skb_peek(&sk->sk_receive_queue)->len; |
| 1127 | else |
| 1128 | answ = 0; |
| 1129 | release_sock(sk); |
| 1130 | return put_user(answ, (int __user *)arg); |
| 1131 | } |
| 1132 | |
| 1133 | return -ENOIOCTLCMD; |
| 1134 | } |
| 1135 | |
| 1136 | static int pep_init(struct sock *sk) |
| 1137 | { |
| 1138 | struct pep_sock *pn = pep_sk(sk); |
| 1139 | |
| 1140 | INIT_HLIST_HEAD(&pn->ackq); |
| 1141 | INIT_HLIST_HEAD(&pn->hlist); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 1142 | skb_queue_head_init(&pn->ctrlreq_queue); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1143 | pn->pipe_handle = PN_PIPE_INVALID_HANDLE; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1147 | static int pep_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1148 | char __user *optval, unsigned int optlen) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1149 | { |
| 1150 | struct pep_sock *pn = pep_sk(sk); |
| 1151 | int val = 0, err = 0; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1152 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 1153 | int remote_pep; |
| 1154 | int pipe_handle; |
| 1155 | #endif |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1156 | |
| 1157 | if (level != SOL_PNPIPE) |
| 1158 | return -ENOPROTOOPT; |
| 1159 | if (optlen >= sizeof(int)) { |
| 1160 | if (get_user(val, (int __user *) optval)) |
| 1161 | return -EFAULT; |
| 1162 | } |
| 1163 | |
| 1164 | lock_sock(sk); |
| 1165 | switch (optname) { |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1166 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 1167 | case PNPIPE_CREATE: |
| 1168 | if (val) { |
| 1169 | if (pn->pipe_state > PIPE_IDLE) { |
| 1170 | err = -EFAULT; |
| 1171 | break; |
| 1172 | } |
| 1173 | remote_pep = val & 0xFFFF; |
| 1174 | pipe_handle = (val >> 16) & 0xFF; |
| 1175 | pn->remote_pep = remote_pep; |
| 1176 | err = pipe_handler_create_pipe(sk, pipe_handle, |
| 1177 | PNPIPE_CREATE); |
| 1178 | break; |
| 1179 | } |
| 1180 | |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1181 | case PNPIPE_DESTROY: |
| 1182 | if (pn->pipe_state < PIPE_DISABLED) { |
| 1183 | err = -EFAULT; |
| 1184 | break; |
| 1185 | } |
| 1186 | |
| 1187 | err = pipe_handler_create_pipe(sk, 0x0, PNPIPE_DESTROY); |
| 1188 | break; |
| 1189 | #endif |
| 1190 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1191 | case PNPIPE_ENCAP: |
| 1192 | if (val && val != PNPIPE_ENCAP_IP) { |
| 1193 | err = -EINVAL; |
| 1194 | break; |
| 1195 | } |
| 1196 | if (!pn->ifindex == !val) |
| 1197 | break; /* Nothing to do! */ |
| 1198 | if (!capable(CAP_NET_ADMIN)) { |
| 1199 | err = -EPERM; |
| 1200 | break; |
| 1201 | } |
| 1202 | if (val) { |
| 1203 | release_sock(sk); |
| 1204 | err = gprs_attach(sk); |
| 1205 | if (err > 0) { |
| 1206 | pn->ifindex = err; |
| 1207 | err = 0; |
| 1208 | } |
| 1209 | } else { |
| 1210 | pn->ifindex = 0; |
| 1211 | release_sock(sk); |
| 1212 | gprs_detach(sk); |
| 1213 | err = 0; |
| 1214 | } |
| 1215 | goto out_norel; |
Rémi Denis-Courmont | 03789f2 | 2010-10-08 04:02:02 +0000 | [diff] [blame^] | 1216 | |
| 1217 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 1218 | case PNPIPE_ENABLE: |
| 1219 | if (pn->pipe_state <= PIPE_IDLE) { |
| 1220 | err = -ENOTCONN; |
| 1221 | break; |
| 1222 | } |
| 1223 | err = pipe_handler_enable_pipe(sk, val); |
| 1224 | break; |
| 1225 | #endif |
| 1226 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1227 | default: |
| 1228 | err = -ENOPROTOOPT; |
| 1229 | } |
| 1230 | release_sock(sk); |
| 1231 | |
| 1232 | out_norel: |
| 1233 | return err; |
| 1234 | } |
| 1235 | |
| 1236 | static int pep_getsockopt(struct sock *sk, int level, int optname, |
| 1237 | char __user *optval, int __user *optlen) |
| 1238 | { |
| 1239 | struct pep_sock *pn = pep_sk(sk); |
| 1240 | int len, val; |
| 1241 | |
| 1242 | if (level != SOL_PNPIPE) |
| 1243 | return -ENOPROTOOPT; |
| 1244 | if (get_user(len, optlen)) |
| 1245 | return -EFAULT; |
| 1246 | |
| 1247 | switch (optname) { |
| 1248 | case PNPIPE_ENCAP: |
| 1249 | val = pn->ifindex ? PNPIPE_ENCAP_IP : PNPIPE_ENCAP_NONE; |
| 1250 | break; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1251 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1252 | case PNPIPE_IFINDEX: |
| 1253 | val = pn->ifindex; |
| 1254 | break; |
Rémi Denis-Courmont | 03789f2 | 2010-10-08 04:02:02 +0000 | [diff] [blame^] | 1255 | |
| 1256 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 1257 | case PNPIPE_ENABLE: |
| 1258 | if (pn->pipe_state <= PIPE_IDLE) |
| 1259 | return -ENOTCONN; |
| 1260 | val = pn->pipe_state != PIPE_DISABLED; |
| 1261 | break; |
| 1262 | #endif |
| 1263 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1264 | default: |
| 1265 | return -ENOPROTOOPT; |
| 1266 | } |
| 1267 | |
| 1268 | len = min_t(unsigned int, sizeof(int), len); |
| 1269 | if (put_user(len, optlen)) |
| 1270 | return -EFAULT; |
| 1271 | if (put_user(val, (int __user *) optval)) |
| 1272 | return -EFAULT; |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | static int pipe_skb_send(struct sock *sk, struct sk_buff *skb) |
| 1277 | { |
| 1278 | struct pep_sock *pn = pep_sk(sk); |
| 1279 | struct pnpipehdr *ph; |
Rémi Denis-Courmont | e1a5964 | 2010-09-29 22:33:50 +0000 | [diff] [blame] | 1280 | int err; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1281 | #ifdef CONFIG_PHONET_PIPECTRLR |
| 1282 | struct sockaddr_pn spn = { |
| 1283 | .spn_family = AF_PHONET, |
| 1284 | .spn_resource = 0xD9, |
| 1285 | .spn_dev = pn_dev(pn->remote_pep), |
| 1286 | .spn_obj = pn_obj(pn->remote_pep), |
| 1287 | }; |
| 1288 | #endif |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1289 | |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 1290 | if (pn_flow_safe(pn->tx_fc) && |
| 1291 | !atomic_add_unless(&pn->tx_credits, -1, 0)) { |
| 1292 | kfree_skb(skb); |
| 1293 | return -ENOBUFS; |
| 1294 | } |
| 1295 | |
Rémi Denis-Courmont | fea93ec | 2010-01-04 02:02:48 +0000 | [diff] [blame] | 1296 | skb_push(skb, 3 + pn->aligned); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1297 | skb_reset_transport_header(skb); |
| 1298 | ph = pnp_hdr(skb); |
| 1299 | ph->utid = 0; |
Rémi Denis-Courmont | fea93ec | 2010-01-04 02:02:48 +0000 | [diff] [blame] | 1300 | if (pn->aligned) { |
| 1301 | ph->message_id = PNS_PIPE_ALIGNED_DATA; |
| 1302 | ph->data[0] = 0; /* padding */ |
| 1303 | } else |
| 1304 | ph->message_id = PNS_PIPE_DATA; |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1305 | ph->pipe_handle = pn->pipe_handle; |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1306 | #ifdef CONFIG_PHONET_PIPECTRLR |
Rémi Denis-Courmont | e1a5964 | 2010-09-29 22:33:50 +0000 | [diff] [blame] | 1307 | err = pn_skb_send(sk, skb, &spn); |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1308 | #else |
Rémi Denis-Courmont | e1a5964 | 2010-09-29 22:33:50 +0000 | [diff] [blame] | 1309 | err = pn_skb_send(sk, skb, &pipe_srv); |
Kumar Sanghvi | 8d98efa | 2010-09-26 19:07:59 +0000 | [diff] [blame] | 1310 | #endif |
Rémi Denis-Courmont | e1a5964 | 2010-09-29 22:33:50 +0000 | [diff] [blame] | 1311 | |
| 1312 | if (err && pn_flow_safe(pn->tx_fc)) |
| 1313 | atomic_inc(&pn->tx_credits); |
| 1314 | return err; |
| 1315 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1318 | static int pep_sendmsg(struct kiocb *iocb, struct sock *sk, |
| 1319 | struct msghdr *msg, size_t len) |
| 1320 | { |
| 1321 | struct pep_sock *pn = pep_sk(sk); |
Rémi Denis-Courmont | b170437 | 2009-11-09 04:06:40 +0000 | [diff] [blame] | 1322 | struct sk_buff *skb; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1323 | long timeo; |
| 1324 | int flags = msg->msg_flags; |
| 1325 | int err, done; |
| 1326 | |
Rémi Denis-Courmont | 82ecbcb | 2010-01-04 02:02:49 +0000 | [diff] [blame] | 1327 | if ((msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL| |
| 1328 | MSG_CMSG_COMPAT)) || |
| 1329 | !(msg->msg_flags & MSG_EOR)) |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1330 | return -EOPNOTSUPP; |
| 1331 | |
Rémi Denis-Courmont | b170437 | 2009-11-09 04:06:40 +0000 | [diff] [blame] | 1332 | skb = sock_alloc_send_skb(sk, MAX_PNPIPE_HEADER + len, |
| 1333 | flags & MSG_DONTWAIT, &err); |
| 1334 | if (!skb) |
Rémi Denis-Courmont | 02ac326 | 2010-08-30 12:57:04 +0000 | [diff] [blame] | 1335 | return err; |
Rémi Denis-Courmont | b170437 | 2009-11-09 04:06:40 +0000 | [diff] [blame] | 1336 | |
| 1337 | skb_reserve(skb, MAX_PHONET_HEADER + 3); |
| 1338 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); |
| 1339 | if (err < 0) |
| 1340 | goto outfree; |
| 1341 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1342 | lock_sock(sk); |
| 1343 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
| 1344 | if ((1 << sk->sk_state) & (TCPF_LISTEN|TCPF_CLOSE)) { |
| 1345 | err = -ENOTCONN; |
| 1346 | goto out; |
| 1347 | } |
| 1348 | if (sk->sk_state != TCP_ESTABLISHED) { |
| 1349 | /* Wait until the pipe gets to enabled state */ |
| 1350 | disabled: |
| 1351 | err = sk_stream_wait_connect(sk, &timeo); |
| 1352 | if (err) |
| 1353 | goto out; |
| 1354 | |
| 1355 | if (sk->sk_state == TCP_CLOSE_WAIT) { |
| 1356 | err = -ECONNRESET; |
| 1357 | goto out; |
| 1358 | } |
| 1359 | } |
| 1360 | BUG_ON(sk->sk_state != TCP_ESTABLISHED); |
| 1361 | |
| 1362 | /* Wait until flow control allows TX */ |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 1363 | done = atomic_read(&pn->tx_credits); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1364 | while (!done) { |
| 1365 | DEFINE_WAIT(wait); |
| 1366 | |
| 1367 | if (!timeo) { |
| 1368 | err = -EAGAIN; |
| 1369 | goto out; |
| 1370 | } |
| 1371 | if (signal_pending(current)) { |
| 1372 | err = sock_intr_errno(timeo); |
| 1373 | goto out; |
| 1374 | } |
| 1375 | |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1376 | prepare_to_wait(sk_sleep(sk), &wait, |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1377 | TASK_INTERRUPTIBLE); |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 1378 | done = sk_wait_event(sk, &timeo, atomic_read(&pn->tx_credits)); |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1379 | finish_wait(sk_sleep(sk), &wait); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1380 | |
| 1381 | if (sk->sk_state != TCP_ESTABLISHED) |
| 1382 | goto disabled; |
| 1383 | } |
| 1384 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1385 | err = pipe_skb_send(sk, skb); |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1386 | if (err >= 0) |
| 1387 | err = len; /* success! */ |
| 1388 | skb = NULL; |
| 1389 | out: |
| 1390 | release_sock(sk); |
Rémi Denis-Courmont | b170437 | 2009-11-09 04:06:40 +0000 | [diff] [blame] | 1391 | outfree: |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1392 | kfree_skb(skb); |
| 1393 | return err; |
| 1394 | } |
| 1395 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1396 | int pep_writeable(struct sock *sk) |
| 1397 | { |
| 1398 | struct pep_sock *pn = pep_sk(sk); |
| 1399 | |
Rémi Denis-Courmont | be67773 | 2008-12-17 15:48:31 -0800 | [diff] [blame] | 1400 | return atomic_read(&pn->tx_credits); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | int pep_write(struct sock *sk, struct sk_buff *skb) |
| 1404 | { |
| 1405 | struct sk_buff *rskb, *fs; |
| 1406 | int flen = 0; |
| 1407 | |
Rémi Denis-Courmont | fea93ec | 2010-01-04 02:02:48 +0000 | [diff] [blame] | 1408 | if (pep_sk(sk)->aligned) |
| 1409 | return pipe_skb_send(sk, skb); |
| 1410 | |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1411 | rskb = alloc_skb(MAX_PNPIPE_HEADER, GFP_ATOMIC); |
| 1412 | if (!rskb) { |
| 1413 | kfree_skb(skb); |
| 1414 | return -ENOMEM; |
| 1415 | } |
| 1416 | skb_shinfo(rskb)->frag_list = skb; |
| 1417 | rskb->len += skb->len; |
| 1418 | rskb->data_len += rskb->len; |
| 1419 | rskb->truesize += rskb->len; |
| 1420 | |
| 1421 | /* Avoid nested fragments */ |
David S. Miller | 5c313e9 | 2009-06-09 00:21:58 -0700 | [diff] [blame] | 1422 | skb_walk_frags(skb, fs) |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1423 | flen += fs->len; |
| 1424 | skb->next = skb_shinfo(skb)->frag_list; |
David S. Miller | 5c313e9 | 2009-06-09 00:21:58 -0700 | [diff] [blame] | 1425 | skb_frag_list_init(skb); |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1426 | skb->len -= flen; |
| 1427 | skb->data_len -= flen; |
| 1428 | skb->truesize -= flen; |
| 1429 | |
| 1430 | skb_reserve(rskb, MAX_PHONET_HEADER + 3); |
| 1431 | return pipe_skb_send(sk, rskb); |
| 1432 | } |
| 1433 | |
| 1434 | struct sk_buff *pep_read(struct sock *sk) |
| 1435 | { |
| 1436 | struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue); |
| 1437 | |
| 1438 | if (sk->sk_state == TCP_ESTABLISHED) |
| 1439 | pipe_grant_credits(sk); |
| 1440 | return skb; |
| 1441 | } |
| 1442 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1443 | static int pep_recvmsg(struct kiocb *iocb, struct sock *sk, |
| 1444 | struct msghdr *msg, size_t len, int noblock, |
| 1445 | int flags, int *addr_len) |
| 1446 | { |
| 1447 | struct sk_buff *skb; |
| 1448 | int err; |
| 1449 | |
Rémi Denis-Courmont | 82ecbcb | 2010-01-04 02:02:49 +0000 | [diff] [blame] | 1450 | if (flags & ~(MSG_OOB|MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_WAITALL| |
| 1451 | MSG_NOSIGNAL|MSG_CMSG_COMPAT)) |
| 1452 | return -EOPNOTSUPP; |
| 1453 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1454 | if (unlikely(1 << sk->sk_state & (TCPF_LISTEN | TCPF_CLOSE))) |
| 1455 | return -ENOTCONN; |
| 1456 | |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 1457 | if ((flags & MSG_OOB) || sock_flag(sk, SOCK_URGINLINE)) { |
| 1458 | /* Dequeue and acknowledge control request */ |
| 1459 | struct pep_sock *pn = pep_sk(sk); |
| 1460 | |
Rémi Denis-Courmont | 82ecbcb | 2010-01-04 02:02:49 +0000 | [diff] [blame] | 1461 | if (flags & MSG_PEEK) |
| 1462 | return -EOPNOTSUPP; |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 1463 | skb = skb_dequeue(&pn->ctrlreq_queue); |
| 1464 | if (skb) { |
| 1465 | pep_ctrlreq_error(sk, skb, PN_PIPE_NO_ERROR, |
| 1466 | GFP_KERNEL); |
| 1467 | msg->msg_flags |= MSG_OOB; |
| 1468 | goto copy; |
| 1469 | } |
| 1470 | if (flags & MSG_OOB) |
| 1471 | return -EINVAL; |
| 1472 | } |
| 1473 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1474 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 1475 | lock_sock(sk); |
| 1476 | if (skb == NULL) { |
| 1477 | if (err == -ENOTCONN && sk->sk_state == TCP_CLOSE_WAIT) |
| 1478 | err = -ECONNRESET; |
| 1479 | release_sock(sk); |
| 1480 | return err; |
| 1481 | } |
| 1482 | |
| 1483 | if (sk->sk_state == TCP_ESTABLISHED) |
| 1484 | pipe_grant_credits(sk); |
| 1485 | release_sock(sk); |
Rémi Denis-Courmont | c41bd97f8 | 2008-10-05 11:15:43 -0700 | [diff] [blame] | 1486 | copy: |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1487 | msg->msg_flags |= MSG_EOR; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1488 | if (skb->len > len) |
| 1489 | msg->msg_flags |= MSG_TRUNC; |
| 1490 | else |
| 1491 | len = skb->len; |
| 1492 | |
| 1493 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len); |
| 1494 | if (!err) |
| 1495 | err = (flags & MSG_TRUNC) ? skb->len : len; |
| 1496 | |
| 1497 | skb_free_datagram(sk, skb); |
| 1498 | return err; |
| 1499 | } |
| 1500 | |
| 1501 | static void pep_sock_unhash(struct sock *sk) |
| 1502 | { |
| 1503 | struct pep_sock *pn = pep_sk(sk); |
| 1504 | struct sock *skparent = NULL; |
| 1505 | |
| 1506 | lock_sock(sk); |
| 1507 | if ((1 << sk->sk_state) & ~(TCPF_CLOSE|TCPF_LISTEN)) { |
| 1508 | skparent = pn->listener; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1509 | release_sock(sk); |
| 1510 | |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1511 | pn = pep_sk(skparent); |
Rémi Denis-Courmont | 7dfde17 | 2010-05-26 00:44:44 +0000 | [diff] [blame] | 1512 | lock_sock(skparent); |
| 1513 | sk_del_node_init(sk); |
| 1514 | sk = skparent; |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1515 | } |
| 1516 | /* Unhash a listening sock only when it is closed |
| 1517 | * and all of its active connected pipes are closed. */ |
| 1518 | if (hlist_empty(&pn->hlist)) |
| 1519 | pn_sock_unhash(&pn->pn_sk.sk); |
| 1520 | release_sock(sk); |
| 1521 | |
| 1522 | if (skparent) |
| 1523 | sock_put(skparent); |
| 1524 | } |
| 1525 | |
| 1526 | static struct proto pep_proto = { |
| 1527 | .close = pep_sock_close, |
| 1528 | .accept = pep_sock_accept, |
| 1529 | .ioctl = pep_ioctl, |
| 1530 | .init = pep_init, |
Rémi Denis-Courmont | 02a4761 | 2008-10-05 11:16:16 -0700 | [diff] [blame] | 1531 | .setsockopt = pep_setsockopt, |
| 1532 | .getsockopt = pep_getsockopt, |
Rémi Denis-Courmont | 9641458 | 2008-10-05 11:15:13 -0700 | [diff] [blame] | 1533 | .sendmsg = pep_sendmsg, |
| 1534 | .recvmsg = pep_recvmsg, |
| 1535 | .backlog_rcv = pep_do_rcv, |
| 1536 | .hash = pn_sock_hash, |
| 1537 | .unhash = pep_sock_unhash, |
| 1538 | .get_port = pn_sock_get_port, |
| 1539 | .obj_size = sizeof(struct pep_sock), |
| 1540 | .owner = THIS_MODULE, |
| 1541 | .name = "PNPIPE", |
| 1542 | }; |
| 1543 | |
| 1544 | static struct phonet_protocol pep_pn_proto = { |
| 1545 | .ops = &phonet_stream_ops, |
| 1546 | .prot = &pep_proto, |
| 1547 | .sock_type = SOCK_SEQPACKET, |
| 1548 | }; |
| 1549 | |
| 1550 | static int __init pep_register(void) |
| 1551 | { |
| 1552 | return phonet_proto_register(PN_PROTO_PIPE, &pep_pn_proto); |
| 1553 | } |
| 1554 | |
| 1555 | static void __exit pep_unregister(void) |
| 1556 | { |
| 1557 | phonet_proto_unregister(PN_PROTO_PIPE, &pep_pn_proto); |
| 1558 | } |
| 1559 | |
| 1560 | module_init(pep_register); |
| 1561 | module_exit(pep_unregister); |
| 1562 | MODULE_AUTHOR("Remi Denis-Courmont, Nokia"); |
| 1563 | MODULE_DESCRIPTION("Phonet pipe protocol"); |
| 1564 | MODULE_LICENSE("GPL"); |
| 1565 | MODULE_ALIAS_NET_PF_PROTO(PF_PHONET, PN_PROTO_PIPE); |