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