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, |
| 19 | struct rxrpc_peer *peer, |
| 20 | struct sk_buff *skb) |
| 21 | { |
| 22 | struct rxrpc_connection *conn, *candidate = NULL; |
| 23 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 24 | struct rb_node *p, **pp; |
| 25 | const char *new = "old"; |
| 26 | u32 epoch, cid; |
| 27 | |
| 28 | _enter(""); |
| 29 | |
| 30 | ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED); |
| 31 | |
| 32 | epoch = sp->hdr.epoch; |
| 33 | cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 34 | |
| 35 | /* search the connection list first */ |
| 36 | read_lock_bh(&peer->conn_lock); |
| 37 | |
| 38 | p = peer->service_conns.rb_node; |
| 39 | while (p) { |
| 40 | conn = rb_entry(p, struct rxrpc_connection, service_node); |
| 41 | |
| 42 | _debug("maybe %x", conn->proto.cid); |
| 43 | |
| 44 | if (epoch < conn->proto.epoch) |
| 45 | p = p->rb_left; |
| 46 | else if (epoch > conn->proto.epoch) |
| 47 | p = p->rb_right; |
| 48 | else if (cid < conn->proto.cid) |
| 49 | p = p->rb_left; |
| 50 | else if (cid > conn->proto.cid) |
| 51 | p = p->rb_right; |
| 52 | else |
| 53 | goto found_extant_connection; |
| 54 | } |
| 55 | read_unlock_bh(&peer->conn_lock); |
| 56 | |
| 57 | /* not yet present - create a candidate for a new record and then |
| 58 | * redo the search */ |
| 59 | candidate = rxrpc_alloc_connection(GFP_NOIO); |
| 60 | if (!candidate) { |
| 61 | _leave(" = -ENOMEM"); |
| 62 | return ERR_PTR(-ENOMEM); |
| 63 | } |
| 64 | |
| 65 | candidate->proto.local = local; |
| 66 | candidate->proto.epoch = sp->hdr.epoch; |
| 67 | candidate->proto.cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 68 | candidate->proto.in_clientflag = RXRPC_CLIENT_INITIATED; |
| 69 | candidate->params.local = local; |
| 70 | candidate->params.peer = peer; |
| 71 | candidate->params.service_id = sp->hdr.serviceId; |
| 72 | candidate->security_ix = sp->hdr.securityIndex; |
| 73 | candidate->out_clientflag = 0; |
| 74 | candidate->state = RXRPC_CONN_SERVICE; |
| 75 | if (candidate->params.service_id) |
| 76 | candidate->state = RXRPC_CONN_SERVICE_UNSECURED; |
| 77 | |
| 78 | write_lock_bh(&peer->conn_lock); |
| 79 | |
| 80 | pp = &peer->service_conns.rb_node; |
| 81 | p = NULL; |
| 82 | while (*pp) { |
| 83 | p = *pp; |
| 84 | conn = rb_entry(p, struct rxrpc_connection, service_node); |
| 85 | |
| 86 | if (epoch < conn->proto.epoch) |
| 87 | pp = &(*pp)->rb_left; |
| 88 | else if (epoch > conn->proto.epoch) |
| 89 | pp = &(*pp)->rb_right; |
| 90 | else if (cid < conn->proto.cid) |
| 91 | pp = &(*pp)->rb_left; |
| 92 | else if (cid > conn->proto.cid) |
| 93 | pp = &(*pp)->rb_right; |
| 94 | else |
| 95 | goto found_extant_second; |
| 96 | } |
| 97 | |
| 98 | /* we can now add the new candidate to the list */ |
| 99 | conn = candidate; |
| 100 | candidate = NULL; |
| 101 | rb_link_node(&conn->service_node, p, pp); |
| 102 | rb_insert_color(&conn->service_node, &peer->service_conns); |
| 103 | rxrpc_get_peer(peer); |
| 104 | rxrpc_get_local(local); |
| 105 | |
| 106 | write_unlock_bh(&peer->conn_lock); |
| 107 | |
| 108 | write_lock(&rxrpc_connection_lock); |
| 109 | list_add_tail(&conn->link, &rxrpc_connections); |
| 110 | write_unlock(&rxrpc_connection_lock); |
| 111 | |
| 112 | new = "new"; |
| 113 | |
| 114 | success: |
| 115 | _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid); |
| 116 | |
| 117 | _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage)); |
| 118 | return conn; |
| 119 | |
| 120 | /* we found the connection in the list immediately */ |
| 121 | found_extant_connection: |
| 122 | if (sp->hdr.securityIndex != conn->security_ix) { |
| 123 | read_unlock_bh(&peer->conn_lock); |
| 124 | goto security_mismatch; |
| 125 | } |
| 126 | rxrpc_get_connection(conn); |
| 127 | read_unlock_bh(&peer->conn_lock); |
| 128 | goto success; |
| 129 | |
| 130 | /* we found the connection on the second time through the list */ |
| 131 | found_extant_second: |
| 132 | if (sp->hdr.securityIndex != conn->security_ix) { |
| 133 | write_unlock_bh(&peer->conn_lock); |
| 134 | goto security_mismatch; |
| 135 | } |
| 136 | rxrpc_get_connection(conn); |
| 137 | write_unlock_bh(&peer->conn_lock); |
| 138 | kfree(candidate); |
| 139 | goto success; |
| 140 | |
| 141 | security_mismatch: |
| 142 | kfree(candidate); |
| 143 | _leave(" = -EKEYREJECTED"); |
| 144 | return ERR_PTR(-EKEYREJECTED); |
| 145 | } |