David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 1 | /* Service connection management |
| 2 | * |
| 3 | * Copyright (C) 2016 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 Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/slab.h> |
| 13 | #include "ar-internal.h" |
| 14 | |
| 15 | /* |
| 16 | * get a record of an incoming connection |
| 17 | */ |
| 18 | struct rxrpc_connection *rxrpc_incoming_connection(struct rxrpc_local *local, |
David Howells | d991b4a | 2016-06-29 14:40:39 +0100 | [diff] [blame^] | 19 | struct sockaddr_rxrpc *srx, |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 20 | struct sk_buff *skb) |
| 21 | { |
| 22 | struct rxrpc_connection *conn, *candidate = NULL; |
| 23 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | d991b4a | 2016-06-29 14:40:39 +0100 | [diff] [blame^] | 24 | struct rxrpc_peer *peer; |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 25 | struct rb_node *p, **pp; |
| 26 | const char *new = "old"; |
| 27 | u32 epoch, cid; |
| 28 | |
| 29 | _enter(""); |
| 30 | |
David Howells | d991b4a | 2016-06-29 14:40:39 +0100 | [diff] [blame^] | 31 | peer = rxrpc_lookup_peer(local, srx, GFP_NOIO); |
| 32 | if (!peer) { |
| 33 | _debug("no peer"); |
| 34 | return ERR_PTR(-EBUSY); |
| 35 | } |
| 36 | |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 37 | ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED); |
| 38 | |
| 39 | epoch = sp->hdr.epoch; |
| 40 | cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 41 | |
| 42 | /* search the connection list first */ |
| 43 | read_lock_bh(&peer->conn_lock); |
| 44 | |
| 45 | p = peer->service_conns.rb_node; |
| 46 | while (p) { |
| 47 | conn = rb_entry(p, struct rxrpc_connection, service_node); |
| 48 | |
| 49 | _debug("maybe %x", conn->proto.cid); |
| 50 | |
| 51 | if (epoch < conn->proto.epoch) |
| 52 | p = p->rb_left; |
| 53 | else if (epoch > conn->proto.epoch) |
| 54 | p = p->rb_right; |
| 55 | else if (cid < conn->proto.cid) |
| 56 | p = p->rb_left; |
| 57 | else if (cid > conn->proto.cid) |
| 58 | p = p->rb_right; |
| 59 | else |
| 60 | goto found_extant_connection; |
| 61 | } |
| 62 | read_unlock_bh(&peer->conn_lock); |
| 63 | |
| 64 | /* not yet present - create a candidate for a new record and then |
| 65 | * redo the search */ |
| 66 | candidate = rxrpc_alloc_connection(GFP_NOIO); |
| 67 | if (!candidate) { |
David Howells | d991b4a | 2016-06-29 14:40:39 +0100 | [diff] [blame^] | 68 | rxrpc_put_peer(peer); |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 69 | _leave(" = -ENOMEM"); |
| 70 | return ERR_PTR(-ENOMEM); |
| 71 | } |
| 72 | |
| 73 | candidate->proto.local = local; |
| 74 | candidate->proto.epoch = sp->hdr.epoch; |
| 75 | candidate->proto.cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 76 | candidate->proto.in_clientflag = RXRPC_CLIENT_INITIATED; |
| 77 | candidate->params.local = local; |
| 78 | candidate->params.peer = peer; |
| 79 | candidate->params.service_id = sp->hdr.serviceId; |
| 80 | candidate->security_ix = sp->hdr.securityIndex; |
| 81 | candidate->out_clientflag = 0; |
| 82 | candidate->state = RXRPC_CONN_SERVICE; |
| 83 | if (candidate->params.service_id) |
| 84 | candidate->state = RXRPC_CONN_SERVICE_UNSECURED; |
| 85 | |
| 86 | write_lock_bh(&peer->conn_lock); |
| 87 | |
| 88 | pp = &peer->service_conns.rb_node; |
| 89 | p = NULL; |
| 90 | while (*pp) { |
| 91 | p = *pp; |
| 92 | conn = rb_entry(p, struct rxrpc_connection, service_node); |
| 93 | |
| 94 | if (epoch < conn->proto.epoch) |
| 95 | pp = &(*pp)->rb_left; |
| 96 | else if (epoch > conn->proto.epoch) |
| 97 | pp = &(*pp)->rb_right; |
| 98 | else if (cid < conn->proto.cid) |
| 99 | pp = &(*pp)->rb_left; |
| 100 | else if (cid > conn->proto.cid) |
| 101 | pp = &(*pp)->rb_right; |
| 102 | else |
| 103 | goto found_extant_second; |
| 104 | } |
| 105 | |
| 106 | /* we can now add the new candidate to the list */ |
| 107 | conn = candidate; |
| 108 | candidate = NULL; |
| 109 | rb_link_node(&conn->service_node, p, pp); |
| 110 | rb_insert_color(&conn->service_node, &peer->service_conns); |
| 111 | rxrpc_get_peer(peer); |
| 112 | rxrpc_get_local(local); |
| 113 | |
| 114 | write_unlock_bh(&peer->conn_lock); |
| 115 | |
| 116 | write_lock(&rxrpc_connection_lock); |
| 117 | list_add_tail(&conn->link, &rxrpc_connections); |
| 118 | write_unlock(&rxrpc_connection_lock); |
| 119 | |
| 120 | new = "new"; |
| 121 | |
| 122 | success: |
| 123 | _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid); |
| 124 | |
David Howells | d991b4a | 2016-06-29 14:40:39 +0100 | [diff] [blame^] | 125 | rxrpc_put_peer(peer); |
David Howells | 7877a4a | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 126 | _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage)); |
| 127 | return conn; |
| 128 | |
| 129 | /* we found the connection in the list immediately */ |
| 130 | found_extant_connection: |
| 131 | if (sp->hdr.securityIndex != conn->security_ix) { |
| 132 | read_unlock_bh(&peer->conn_lock); |
| 133 | goto security_mismatch; |
| 134 | } |
| 135 | rxrpc_get_connection(conn); |
| 136 | read_unlock_bh(&peer->conn_lock); |
| 137 | goto success; |
| 138 | |
| 139 | /* we found the connection on the second time through the list */ |
| 140 | found_extant_second: |
| 141 | if (sp->hdr.securityIndex != conn->security_ix) { |
| 142 | write_unlock_bh(&peer->conn_lock); |
| 143 | goto security_mismatch; |
| 144 | } |
| 145 | rxrpc_get_connection(conn); |
| 146 | write_unlock_bh(&peer->conn_lock); |
| 147 | kfree(candidate); |
| 148 | goto success; |
| 149 | |
| 150 | security_mismatch: |
| 151 | kfree(candidate); |
| 152 | _leave(" = -EKEYREJECTED"); |
| 153 | return ERR_PTR(-EKEYREJECTED); |
| 154 | } |