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