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