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