David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* AF_RXRPC local endpoint management |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame^] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/net.h> |
| 16 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 18 | #include <linux/udp.h> |
| 19 | #include <linux/ip.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 20 | #include <net/sock.h> |
| 21 | #include <net/af_rxrpc.h> |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 22 | #include <generated/utsrelease.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 23 | #include "ar-internal.h" |
| 24 | |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 25 | static const char rxrpc_version_string[65] = "linux-" UTS_RELEASE " AF_RXRPC"; |
| 26 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 27 | static LIST_HEAD(rxrpc_locals); |
| 28 | DEFINE_RWLOCK(rxrpc_local_lock); |
| 29 | static DECLARE_RWSEM(rxrpc_local_sem); |
| 30 | static DECLARE_WAIT_QUEUE_HEAD(rxrpc_local_wq); |
| 31 | |
| 32 | static void rxrpc_destroy_local(struct work_struct *work); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 33 | static void rxrpc_process_local_events(struct work_struct *work); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * allocate a new local |
| 37 | */ |
| 38 | static |
| 39 | struct rxrpc_local *rxrpc_alloc_local(struct sockaddr_rxrpc *srx) |
| 40 | { |
| 41 | struct rxrpc_local *local; |
| 42 | |
| 43 | local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL); |
| 44 | if (local) { |
| 45 | INIT_WORK(&local->destroyer, &rxrpc_destroy_local); |
| 46 | INIT_WORK(&local->acceptor, &rxrpc_accept_incoming_calls); |
| 47 | INIT_WORK(&local->rejecter, &rxrpc_reject_packets); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 48 | INIT_WORK(&local->event_processor, &rxrpc_process_local_events); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 49 | INIT_LIST_HEAD(&local->services); |
| 50 | INIT_LIST_HEAD(&local->link); |
| 51 | init_rwsem(&local->defrag_sem); |
| 52 | skb_queue_head_init(&local->accept_queue); |
| 53 | skb_queue_head_init(&local->reject_queue); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 54 | skb_queue_head_init(&local->event_queue); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 55 | spin_lock_init(&local->lock); |
| 56 | rwlock_init(&local->services_lock); |
| 57 | atomic_set(&local->usage, 1); |
| 58 | local->debug_id = atomic_inc_return(&rxrpc_debug_id); |
| 59 | memcpy(&local->srx, srx, sizeof(*srx)); |
| 60 | } |
| 61 | |
| 62 | _leave(" = %p", local); |
| 63 | return local; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * create the local socket |
| 68 | * - must be called with rxrpc_local_sem writelocked |
| 69 | */ |
| 70 | static int rxrpc_create_local(struct rxrpc_local *local) |
| 71 | { |
| 72 | struct sock *sock; |
| 73 | int ret, opt; |
| 74 | |
| 75 | _enter("%p{%d}", local, local->srx.transport_type); |
| 76 | |
| 77 | /* create a socket to represent the local endpoint */ |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 78 | ret = sock_create_kern(&init_net, PF_INET, local->srx.transport_type, |
| 79 | IPPROTO_UDP, &local->socket); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 80 | if (ret < 0) { |
| 81 | _leave(" = %d [socket]", ret); |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | /* if a local address was supplied then bind it */ |
| 86 | if (local->srx.transport_len > sizeof(sa_family_t)) { |
| 87 | _debug("bind"); |
| 88 | ret = kernel_bind(local->socket, |
| 89 | (struct sockaddr *) &local->srx.transport, |
| 90 | local->srx.transport_len); |
| 91 | if (ret < 0) { |
| 92 | _debug("bind failed"); |
| 93 | goto error; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* we want to receive ICMP errors */ |
| 98 | opt = 1; |
| 99 | ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR, |
| 100 | (char *) &opt, sizeof(opt)); |
| 101 | if (ret < 0) { |
| 102 | _debug("setsockopt failed"); |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | /* we want to set the don't fragment bit */ |
| 107 | opt = IP_PMTUDISC_DO; |
| 108 | ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER, |
| 109 | (char *) &opt, sizeof(opt)); |
| 110 | if (ret < 0) { |
| 111 | _debug("setsockopt failed"); |
| 112 | goto error; |
| 113 | } |
| 114 | |
| 115 | write_lock_bh(&rxrpc_local_lock); |
| 116 | list_add(&local->link, &rxrpc_locals); |
| 117 | write_unlock_bh(&rxrpc_local_lock); |
| 118 | |
| 119 | /* set the socket up */ |
| 120 | sock = local->socket->sk; |
| 121 | sock->sk_user_data = local; |
| 122 | sock->sk_data_ready = rxrpc_data_ready; |
| 123 | sock->sk_error_report = rxrpc_UDP_error_report; |
| 124 | _leave(" = 0"); |
| 125 | return 0; |
| 126 | |
| 127 | error: |
Trond Myklebust | 91cf45f | 2007-11-12 18:10:39 -0800 | [diff] [blame] | 128 | kernel_sock_shutdown(local->socket, SHUT_RDWR); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 129 | local->socket->sk->sk_user_data = NULL; |
| 130 | sock_release(local->socket); |
| 131 | local->socket = NULL; |
| 132 | |
| 133 | _leave(" = %d", ret); |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * create a new local endpoint using the specified UDP address |
| 139 | */ |
| 140 | struct rxrpc_local *rxrpc_lookup_local(struct sockaddr_rxrpc *srx) |
| 141 | { |
| 142 | struct rxrpc_local *local; |
| 143 | int ret; |
| 144 | |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 145 | _enter("{%d,%u,%pI4+%hu}", |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 146 | srx->transport_type, |
| 147 | srx->transport.family, |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 148 | &srx->transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 149 | ntohs(srx->transport.sin.sin_port)); |
| 150 | |
| 151 | down_write(&rxrpc_local_sem); |
| 152 | |
| 153 | /* see if we have a suitable local local endpoint already */ |
| 154 | read_lock_bh(&rxrpc_local_lock); |
| 155 | |
| 156 | list_for_each_entry(local, &rxrpc_locals, link) { |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 157 | _debug("CMP {%d,%u,%pI4+%hu}", |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 158 | local->srx.transport_type, |
| 159 | local->srx.transport.family, |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 160 | &local->srx.transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 161 | ntohs(local->srx.transport.sin.sin_port)); |
| 162 | |
| 163 | if (local->srx.transport_type != srx->transport_type || |
| 164 | local->srx.transport.family != srx->transport.family) |
| 165 | continue; |
| 166 | |
| 167 | switch (srx->transport.family) { |
| 168 | case AF_INET: |
| 169 | if (local->srx.transport.sin.sin_port != |
| 170 | srx->transport.sin.sin_port) |
| 171 | continue; |
| 172 | if (memcmp(&local->srx.transport.sin.sin_addr, |
| 173 | &srx->transport.sin.sin_addr, |
| 174 | sizeof(struct in_addr)) != 0) |
| 175 | continue; |
| 176 | goto found_local; |
| 177 | |
| 178 | default: |
| 179 | BUG(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | read_unlock_bh(&rxrpc_local_lock); |
| 184 | |
| 185 | /* we didn't find one, so we need to create one */ |
| 186 | local = rxrpc_alloc_local(srx); |
| 187 | if (!local) { |
| 188 | up_write(&rxrpc_local_sem); |
| 189 | return ERR_PTR(-ENOMEM); |
| 190 | } |
| 191 | |
| 192 | ret = rxrpc_create_local(local); |
| 193 | if (ret < 0) { |
| 194 | up_write(&rxrpc_local_sem); |
| 195 | kfree(local); |
| 196 | _leave(" = %d", ret); |
| 197 | return ERR_PTR(ret); |
| 198 | } |
| 199 | |
| 200 | up_write(&rxrpc_local_sem); |
| 201 | |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 202 | _net("LOCAL new %d {%d,%u,%pI4+%hu}", |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | local->debug_id, |
| 204 | local->srx.transport_type, |
| 205 | local->srx.transport.family, |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 206 | &local->srx.transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 207 | ntohs(local->srx.transport.sin.sin_port)); |
| 208 | |
| 209 | _leave(" = %p [new]", local); |
| 210 | return local; |
| 211 | |
| 212 | found_local: |
| 213 | rxrpc_get_local(local); |
| 214 | read_unlock_bh(&rxrpc_local_lock); |
| 215 | up_write(&rxrpc_local_sem); |
| 216 | |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 217 | _net("LOCAL old %d {%d,%u,%pI4+%hu}", |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 218 | local->debug_id, |
| 219 | local->srx.transport_type, |
| 220 | local->srx.transport.family, |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 221 | &local->srx.transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 222 | ntohs(local->srx.transport.sin.sin_port)); |
| 223 | |
| 224 | _leave(" = %p [reuse]", local); |
| 225 | return local; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * release a local endpoint |
| 230 | */ |
| 231 | void rxrpc_put_local(struct rxrpc_local *local) |
| 232 | { |
| 233 | _enter("%p{u=%d}", local, atomic_read(&local->usage)); |
| 234 | |
| 235 | ASSERTCMP(atomic_read(&local->usage), >, 0); |
| 236 | |
| 237 | /* to prevent a race, the decrement and the dequeue must be effectively |
| 238 | * atomic */ |
| 239 | write_lock_bh(&rxrpc_local_lock); |
| 240 | if (unlikely(atomic_dec_and_test(&local->usage))) { |
| 241 | _debug("destroy local"); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 242 | rxrpc_queue_work(&local->destroyer); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 243 | } |
| 244 | write_unlock_bh(&rxrpc_local_lock); |
| 245 | _leave(""); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * destroy a local endpoint |
| 250 | */ |
| 251 | static void rxrpc_destroy_local(struct work_struct *work) |
| 252 | { |
| 253 | struct rxrpc_local *local = |
| 254 | container_of(work, struct rxrpc_local, destroyer); |
| 255 | |
| 256 | _enter("%p{%d}", local, atomic_read(&local->usage)); |
| 257 | |
| 258 | down_write(&rxrpc_local_sem); |
| 259 | |
| 260 | write_lock_bh(&rxrpc_local_lock); |
| 261 | if (atomic_read(&local->usage) > 0) { |
| 262 | write_unlock_bh(&rxrpc_local_lock); |
| 263 | up_read(&rxrpc_local_sem); |
| 264 | _leave(" [resurrected]"); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | list_del(&local->link); |
| 269 | local->socket->sk->sk_user_data = NULL; |
| 270 | write_unlock_bh(&rxrpc_local_lock); |
| 271 | |
| 272 | downgrade_write(&rxrpc_local_sem); |
| 273 | |
| 274 | ASSERT(list_empty(&local->services)); |
| 275 | ASSERT(!work_pending(&local->acceptor)); |
| 276 | ASSERT(!work_pending(&local->rejecter)); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 277 | ASSERT(!work_pending(&local->event_processor)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 278 | |
| 279 | /* finish cleaning up the local descriptor */ |
| 280 | rxrpc_purge_queue(&local->accept_queue); |
| 281 | rxrpc_purge_queue(&local->reject_queue); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 282 | rxrpc_purge_queue(&local->event_queue); |
Trond Myklebust | 91cf45f | 2007-11-12 18:10:39 -0800 | [diff] [blame] | 283 | kernel_sock_shutdown(local->socket, SHUT_RDWR); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 284 | sock_release(local->socket); |
| 285 | |
| 286 | up_read(&rxrpc_local_sem); |
| 287 | |
| 288 | _net("DESTROY LOCAL %d", local->debug_id); |
| 289 | kfree(local); |
| 290 | |
| 291 | if (list_empty(&rxrpc_locals)) |
| 292 | wake_up_all(&rxrpc_local_wq); |
| 293 | |
| 294 | _leave(""); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * preemptively destroy all local local endpoint rather than waiting for |
| 299 | * them to be destroyed |
| 300 | */ |
| 301 | void __exit rxrpc_destroy_all_locals(void) |
| 302 | { |
| 303 | DECLARE_WAITQUEUE(myself,current); |
| 304 | |
| 305 | _enter(""); |
| 306 | |
| 307 | /* we simply have to wait for them to go away */ |
| 308 | if (!list_empty(&rxrpc_locals)) { |
| 309 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 310 | add_wait_queue(&rxrpc_local_wq, &myself); |
| 311 | |
| 312 | while (!list_empty(&rxrpc_locals)) { |
| 313 | schedule(); |
| 314 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 315 | } |
| 316 | |
| 317 | remove_wait_queue(&rxrpc_local_wq, &myself); |
| 318 | set_current_state(TASK_RUNNING); |
| 319 | } |
| 320 | |
| 321 | _leave(""); |
| 322 | } |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * Reply to a version request |
| 326 | */ |
| 327 | static void rxrpc_send_version_request(struct rxrpc_local *local, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 328 | struct rxrpc_host_header *hdr, |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 329 | struct sk_buff *skb) |
| 330 | { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 331 | struct rxrpc_wire_header whdr; |
| 332 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 333 | struct sockaddr_in sin; |
| 334 | struct msghdr msg; |
| 335 | struct kvec iov[2]; |
| 336 | size_t len; |
| 337 | int ret; |
| 338 | |
| 339 | _enter(""); |
| 340 | |
| 341 | sin.sin_family = AF_INET; |
| 342 | sin.sin_port = udp_hdr(skb)->source; |
| 343 | sin.sin_addr.s_addr = ip_hdr(skb)->saddr; |
| 344 | |
| 345 | msg.msg_name = &sin; |
| 346 | msg.msg_namelen = sizeof(sin); |
| 347 | msg.msg_control = NULL; |
| 348 | msg.msg_controllen = 0; |
| 349 | msg.msg_flags = 0; |
| 350 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 351 | whdr.epoch = htonl(sp->hdr.epoch); |
| 352 | whdr.cid = htonl(sp->hdr.cid); |
| 353 | whdr.callNumber = htonl(sp->hdr.callNumber); |
| 354 | whdr.seq = 0; |
| 355 | whdr.serial = 0; |
| 356 | whdr.type = RXRPC_PACKET_TYPE_VERSION; |
| 357 | whdr.flags = RXRPC_LAST_PACKET | (~hdr->flags & RXRPC_CLIENT_INITIATED); |
| 358 | whdr.userStatus = 0; |
| 359 | whdr.securityIndex = 0; |
| 360 | whdr._rsvd = 0; |
| 361 | whdr.serviceId = htons(sp->hdr.serviceId); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 362 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 363 | iov[0].iov_base = &whdr; |
| 364 | iov[0].iov_len = sizeof(whdr); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 365 | iov[1].iov_base = (char *)rxrpc_version_string; |
| 366 | iov[1].iov_len = sizeof(rxrpc_version_string); |
| 367 | |
| 368 | len = iov[0].iov_len + iov[1].iov_len; |
| 369 | |
| 370 | _proto("Tx VERSION (reply)"); |
| 371 | |
| 372 | ret = kernel_sendmsg(local->socket, &msg, iov, 2, len); |
| 373 | if (ret < 0) |
| 374 | _debug("sendmsg failed: %d", ret); |
| 375 | |
| 376 | _leave(""); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Process event packets targetted at a local endpoint. |
| 381 | */ |
| 382 | static void rxrpc_process_local_events(struct work_struct *work) |
| 383 | { |
| 384 | struct rxrpc_local *local = container_of(work, struct rxrpc_local, event_processor); |
| 385 | struct sk_buff *skb; |
| 386 | char v; |
| 387 | |
| 388 | _enter(""); |
| 389 | |
| 390 | atomic_inc(&local->usage); |
| 391 | |
| 392 | while ((skb = skb_dequeue(&local->event_queue))) { |
| 393 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 394 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 395 | _debug("{%d},{%u}", local->debug_id, sp->hdr.type); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 396 | |
| 397 | switch (sp->hdr.type) { |
| 398 | case RXRPC_PACKET_TYPE_VERSION: |
| 399 | if (skb_copy_bits(skb, 0, &v, 1) < 0) |
| 400 | return; |
| 401 | _proto("Rx VERSION { %02x }", v); |
| 402 | if (v == 0) |
| 403 | rxrpc_send_version_request(local, &sp->hdr, skb); |
| 404 | break; |
| 405 | |
| 406 | default: |
| 407 | /* Just ignore anything we don't understand */ |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | rxrpc_put_local(local); |
| 412 | rxrpc_free_skb(skb); |
| 413 | } |
| 414 | |
| 415 | rxrpc_put_local(local); |
| 416 | _leave(""); |
| 417 | } |