1 /* RxRPC security handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/udp.h>
16 #include <linux/crypto.h>
18 #include <net/af_rxrpc.h>
19 #include <keys/rxrpc-type.h>
20 #include "ar-internal.h"
22 static const struct rxrpc_security
*rxrpc_security_types
[] = {
23 [RXRPC_SECURITY_NONE
] = &rxrpc_no_security
,
25 [RXRPC_SECURITY_RXKAD
] = &rxkad
,
29 int __init
rxrpc_init_security(void)
33 for (i
= 0; i
< ARRAY_SIZE(rxrpc_security_types
); i
++) {
34 if (rxrpc_security_types
[i
]) {
35 ret
= rxrpc_security_types
[i
]->init();
44 for (i
--; i
>= 0; i
--)
45 if (rxrpc_security_types
[i
])
46 rxrpc_security_types
[i
]->exit();
50 void rxrpc_exit_security(void)
54 for (i
= 0; i
< ARRAY_SIZE(rxrpc_security_types
); i
++)
55 if (rxrpc_security_types
[i
])
56 rxrpc_security_types
[i
]->exit();
60 * look up an rxrpc security module
62 static const struct rxrpc_security
*rxrpc_security_lookup(u8 security_index
)
64 if (security_index
>= ARRAY_SIZE(rxrpc_security_types
))
66 return rxrpc_security_types
[security_index
];
70 * initialise the security on a client connection
72 int rxrpc_init_client_conn_security(struct rxrpc_connection
*conn
)
74 const struct rxrpc_security
*sec
;
75 struct rxrpc_key_token
*token
;
76 struct key
*key
= conn
->params
.key
;
79 _enter("{%d},{%x}", conn
->debug_id
, key_serial(key
));
84 ret
= key_validate(key
);
88 token
= key
->payload
.data
[0];
92 sec
= rxrpc_security_lookup(token
->security_index
);
97 ret
= conn
->security
->init_connection_security(conn
);
99 conn
->security
= &rxrpc_no_security
;
108 * initialise the security on a server connection
110 int rxrpc_init_server_conn_security(struct rxrpc_connection
*conn
)
112 const struct rxrpc_security
*sec
;
113 struct rxrpc_local
*local
= conn
->params
.local
;
114 struct rxrpc_sock
*rx
;
117 char kdesc
[5 + 1 + 3 + 1];
121 sprintf(kdesc
, "%u:%u", conn
->service_id
, conn
->security_ix
);
123 sec
= rxrpc_security_lookup(conn
->security_ix
);
125 _leave(" = -ENOKEY [lookup]");
129 /* find the service */
130 read_lock(&local
->services_lock
);
131 rx
= rcu_dereference_protected(local
->service
,
132 lockdep_is_held(&local
->services_lock
));
133 if (rx
&& (rx
->srx
.srx_service
== conn
->service_id
||
134 rx
->second_service
== conn
->service_id
))
137 /* the service appears to have died */
138 read_unlock(&local
->services_lock
);
139 _leave(" = -ENOENT");
143 if (!rx
->securities
) {
144 read_unlock(&local
->services_lock
);
145 _leave(" = -ENOKEY");
149 /* look through the service's keyring */
150 kref
= keyring_search(make_key_ref(rx
->securities
, 1UL),
151 &key_type_rxrpc_s
, kdesc
);
153 read_unlock(&local
->services_lock
);
154 _leave(" = %ld [search]", PTR_ERR(kref
));
155 return PTR_ERR(kref
);
158 key
= key_ref_to_ptr(kref
);
159 read_unlock(&local
->services_lock
);
161 conn
->server_key
= key
;
162 conn
->security
= sec
;