2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/security.h>
34 #include <linux/completion.h>
35 #include <linux/list.h>
37 #include <rdma/ib_verbs.h>
38 #include <rdma/ib_cache.h>
39 #include "core_priv.h"
42 static LIST_HEAD(mad_agent_list
);
43 /* Lock to protect mad_agent_list */
44 static DEFINE_SPINLOCK(mad_agent_list_lock
);
46 static struct pkey_index_qp_list
*get_pkey_idx_qp_list(struct ib_port_pkey
*pp
)
48 struct pkey_index_qp_list
*pkey
= NULL
;
49 struct pkey_index_qp_list
*tmp_pkey
;
50 struct ib_device
*dev
= pp
->sec
->dev
;
52 spin_lock(&dev
->port_data
[pp
->port_num
].pkey_list_lock
);
53 list_for_each_entry (tmp_pkey
, &dev
->port_data
[pp
->port_num
].pkey_list
,
55 if (tmp_pkey
->pkey_index
== pp
->pkey_index
) {
60 spin_unlock(&dev
->port_data
[pp
->port_num
].pkey_list_lock
);
64 static int get_pkey_and_subnet_prefix(struct ib_port_pkey
*pp
,
68 struct ib_device
*dev
= pp
->sec
->dev
;
71 ret
= ib_get_cached_pkey(dev
, pp
->port_num
, pp
->pkey_index
, pkey
);
75 ret
= ib_get_cached_subnet_prefix(dev
, pp
->port_num
, subnet_prefix
);
80 static int enforce_qp_pkey_security(u16 pkey
,
82 struct ib_qp_security
*qp_sec
)
84 struct ib_qp_security
*shared_qp_sec
;
87 ret
= security_ib_pkey_access(qp_sec
->security
, subnet_prefix
, pkey
);
91 list_for_each_entry(shared_qp_sec
,
92 &qp_sec
->shared_qp_list
,
94 ret
= security_ib_pkey_access(shared_qp_sec
->security
,
103 /* The caller of this function must hold the QP security
104 * mutex of the QP of the security structure in *pps.
106 * It takes separate ports_pkeys and security structure
107 * because in some cases the pps will be for a new settings
108 * or the pps will be for the real QP and security structure
109 * will be for a shared QP.
111 static int check_qp_port_pkey_settings(struct ib_ports_pkeys
*pps
,
112 struct ib_qp_security
*sec
)
121 if (pps
->main
.state
!= IB_PORT_PKEY_NOT_VALID
) {
122 ret
= get_pkey_and_subnet_prefix(&pps
->main
,
128 ret
= enforce_qp_pkey_security(pkey
,
135 if (pps
->alt
.state
!= IB_PORT_PKEY_NOT_VALID
) {
136 ret
= get_pkey_and_subnet_prefix(&pps
->alt
,
142 ret
= enforce_qp_pkey_security(pkey
,
150 /* The caller of this function must hold the QP security
153 static void qp_to_error(struct ib_qp_security
*sec
)
155 struct ib_qp_security
*shared_qp_sec
;
156 struct ib_qp_attr attr
= {
157 .qp_state
= IB_QPS_ERR
159 struct ib_event event
= {
160 .event
= IB_EVENT_QP_FATAL
163 /* If the QP is in the process of being destroyed
164 * the qp pointer in the security structure is
165 * undefined. It cannot be modified now.
170 ib_modify_qp(sec
->qp
,
174 if (sec
->qp
->event_handler
&& sec
->qp
->qp_context
) {
175 event
.element
.qp
= sec
->qp
;
176 sec
->qp
->event_handler(&event
,
177 sec
->qp
->qp_context
);
180 list_for_each_entry(shared_qp_sec
,
181 &sec
->shared_qp_list
,
183 struct ib_qp
*qp
= shared_qp_sec
->qp
;
185 if (qp
->event_handler
&& qp
->qp_context
) {
186 event
.element
.qp
= qp
;
187 event
.device
= qp
->device
;
188 qp
->event_handler(&event
,
194 static inline void check_pkey_qps(struct pkey_index_qp_list
*pkey
,
195 struct ib_device
*device
,
199 struct ib_port_pkey
*pp
, *tmp_pp
;
201 LIST_HEAD(to_error_list
);
204 if (!ib_get_cached_pkey(device
,
208 spin_lock(&pkey
->qp_list_lock
);
209 list_for_each_entry(pp
, &pkey
->qp_list
, qp_list
) {
210 if (atomic_read(&pp
->sec
->error_list_count
))
213 if (enforce_qp_pkey_security(pkey_val
,
216 atomic_inc(&pp
->sec
->error_list_count
);
217 list_add(&pp
->to_error_list
,
221 spin_unlock(&pkey
->qp_list_lock
);
224 list_for_each_entry_safe(pp
,
228 mutex_lock(&pp
->sec
->mutex
);
229 qp_to_error(pp
->sec
);
230 list_del(&pp
->to_error_list
);
231 atomic_dec(&pp
->sec
->error_list_count
);
232 comp
= pp
->sec
->destroying
;
233 mutex_unlock(&pp
->sec
->mutex
);
236 complete(&pp
->sec
->error_complete
);
240 /* The caller of this function must hold the QP security
243 static int port_pkey_list_insert(struct ib_port_pkey
*pp
)
245 struct pkey_index_qp_list
*tmp_pkey
;
246 struct pkey_index_qp_list
*pkey
;
247 struct ib_device
*dev
;
248 u8 port_num
= pp
->port_num
;
251 if (pp
->state
!= IB_PORT_PKEY_VALID
)
256 pkey
= get_pkey_idx_qp_list(pp
);
261 pkey
= kzalloc(sizeof(*pkey
), GFP_KERNEL
);
265 spin_lock(&dev
->port_data
[port_num
].pkey_list_lock
);
266 /* Check for the PKey again. A racing process may
269 list_for_each_entry(tmp_pkey
,
270 &dev
->port_data
[port_num
].pkey_list
,
272 if (tmp_pkey
->pkey_index
== pp
->pkey_index
) {
281 pkey
->pkey_index
= pp
->pkey_index
;
282 spin_lock_init(&pkey
->qp_list_lock
);
283 INIT_LIST_HEAD(&pkey
->qp_list
);
284 list_add(&pkey
->pkey_index_list
,
285 &dev
->port_data
[port_num
].pkey_list
);
287 spin_unlock(&dev
->port_data
[port_num
].pkey_list_lock
);
290 spin_lock(&pkey
->qp_list_lock
);
291 list_add(&pp
->qp_list
, &pkey
->qp_list
);
292 spin_unlock(&pkey
->qp_list_lock
);
294 pp
->state
= IB_PORT_PKEY_LISTED
;
299 /* The caller of this function must hold the QP security
302 static void port_pkey_list_remove(struct ib_port_pkey
*pp
)
304 struct pkey_index_qp_list
*pkey
;
306 if (pp
->state
!= IB_PORT_PKEY_LISTED
)
309 pkey
= get_pkey_idx_qp_list(pp
);
311 spin_lock(&pkey
->qp_list_lock
);
312 list_del(&pp
->qp_list
);
313 spin_unlock(&pkey
->qp_list_lock
);
315 /* The setting may still be valid, i.e. after
316 * a destroy has failed for example.
318 pp
->state
= IB_PORT_PKEY_VALID
;
321 static void destroy_qp_security(struct ib_qp_security
*sec
)
323 security_ib_free_security(sec
->security
);
324 kfree(sec
->ports_pkeys
);
328 /* The caller of this function must hold the QP security
331 static struct ib_ports_pkeys
*get_new_pps(const struct ib_qp
*qp
,
332 const struct ib_qp_attr
*qp_attr
,
335 struct ib_ports_pkeys
*new_pps
;
336 struct ib_ports_pkeys
*qp_pps
= qp
->qp_sec
->ports_pkeys
;
338 new_pps
= kzalloc(sizeof(*new_pps
), GFP_KERNEL
);
342 if (qp_attr_mask
& (IB_QP_PKEY_INDEX
| IB_QP_PORT
)) {
344 new_pps
->main
.port_num
= qp_attr
->port_num
;
345 new_pps
->main
.pkey_index
= qp_attr
->pkey_index
;
347 new_pps
->main
.port_num
= (qp_attr_mask
& IB_QP_PORT
) ?
349 qp_pps
->main
.port_num
;
351 new_pps
->main
.pkey_index
=
352 (qp_attr_mask
& IB_QP_PKEY_INDEX
) ?
353 qp_attr
->pkey_index
:
354 qp_pps
->main
.pkey_index
;
356 new_pps
->main
.state
= IB_PORT_PKEY_VALID
;
358 new_pps
->main
.port_num
= qp_pps
->main
.port_num
;
359 new_pps
->main
.pkey_index
= qp_pps
->main
.pkey_index
;
360 if (qp_pps
->main
.state
!= IB_PORT_PKEY_NOT_VALID
)
361 new_pps
->main
.state
= IB_PORT_PKEY_VALID
;
364 if (qp_attr_mask
& IB_QP_ALT_PATH
) {
365 new_pps
->alt
.port_num
= qp_attr
->alt_port_num
;
366 new_pps
->alt
.pkey_index
= qp_attr
->alt_pkey_index
;
367 new_pps
->alt
.state
= IB_PORT_PKEY_VALID
;
369 new_pps
->alt
.port_num
= qp_pps
->alt
.port_num
;
370 new_pps
->alt
.pkey_index
= qp_pps
->alt
.pkey_index
;
371 if (qp_pps
->alt
.state
!= IB_PORT_PKEY_NOT_VALID
)
372 new_pps
->alt
.state
= IB_PORT_PKEY_VALID
;
375 new_pps
->main
.sec
= qp
->qp_sec
;
376 new_pps
->alt
.sec
= qp
->qp_sec
;
380 int ib_open_shared_qp_security(struct ib_qp
*qp
, struct ib_device
*dev
)
382 struct ib_qp
*real_qp
= qp
->real_qp
;
385 ret
= ib_create_qp_security(qp
, dev
);
393 mutex_lock(&real_qp
->qp_sec
->mutex
);
394 ret
= check_qp_port_pkey_settings(real_qp
->qp_sec
->ports_pkeys
,
401 list_add(&qp
->qp_sec
->shared_qp_list
,
402 &real_qp
->qp_sec
->shared_qp_list
);
404 mutex_unlock(&real_qp
->qp_sec
->mutex
);
406 destroy_qp_security(qp
->qp_sec
);
411 void ib_close_shared_qp_security(struct ib_qp_security
*sec
)
413 struct ib_qp
*real_qp
= sec
->qp
->real_qp
;
415 mutex_lock(&real_qp
->qp_sec
->mutex
);
416 list_del(&sec
->shared_qp_list
);
417 mutex_unlock(&real_qp
->qp_sec
->mutex
);
419 destroy_qp_security(sec
);
422 int ib_create_qp_security(struct ib_qp
*qp
, struct ib_device
*dev
)
428 rdma_for_each_port (dev
, i
) {
429 is_ib
= rdma_protocol_ib(dev
, i
);
434 /* If this isn't an IB device don't create the security context */
438 qp
->qp_sec
= kzalloc(sizeof(*qp
->qp_sec
), GFP_KERNEL
);
443 qp
->qp_sec
->dev
= dev
;
444 mutex_init(&qp
->qp_sec
->mutex
);
445 INIT_LIST_HEAD(&qp
->qp_sec
->shared_qp_list
);
446 atomic_set(&qp
->qp_sec
->error_list_count
, 0);
447 init_completion(&qp
->qp_sec
->error_complete
);
448 ret
= security_ib_alloc_security(&qp
->qp_sec
->security
);
456 EXPORT_SYMBOL(ib_create_qp_security
);
458 void ib_destroy_qp_security_begin(struct ib_qp_security
*sec
)
460 /* Return if not IB */
464 mutex_lock(&sec
->mutex
);
466 /* Remove the QP from the lists so it won't get added to
467 * a to_error_list during the destroy process.
469 if (sec
->ports_pkeys
) {
470 port_pkey_list_remove(&sec
->ports_pkeys
->main
);
471 port_pkey_list_remove(&sec
->ports_pkeys
->alt
);
474 /* If the QP is already in one or more of those lists
475 * the destroying flag will ensure the to error flow
476 * doesn't operate on an undefined QP.
478 sec
->destroying
= true;
480 /* Record the error list count to know how many completions
483 sec
->error_comps_pending
= atomic_read(&sec
->error_list_count
);
485 mutex_unlock(&sec
->mutex
);
488 void ib_destroy_qp_security_abort(struct ib_qp_security
*sec
)
493 /* Return if not IB */
497 /* If a concurrent cache update is in progress this
498 * QP security could be marked for an error state
499 * transition. Wait for this to complete.
501 for (i
= 0; i
< sec
->error_comps_pending
; i
++)
502 wait_for_completion(&sec
->error_complete
);
504 mutex_lock(&sec
->mutex
);
505 sec
->destroying
= false;
507 /* Restore the position in the lists and verify
508 * access is still allowed in case a cache update
509 * occurred while attempting to destroy.
511 * Because these setting were listed already
512 * and removed during ib_destroy_qp_security_begin
513 * we know the pkey_index_qp_list for the PKey
514 * already exists so port_pkey_list_insert won't fail.
516 if (sec
->ports_pkeys
) {
517 port_pkey_list_insert(&sec
->ports_pkeys
->main
);
518 port_pkey_list_insert(&sec
->ports_pkeys
->alt
);
521 ret
= check_qp_port_pkey_settings(sec
->ports_pkeys
, sec
);
525 mutex_unlock(&sec
->mutex
);
528 void ib_destroy_qp_security_end(struct ib_qp_security
*sec
)
532 /* Return if not IB */
536 /* If a concurrent cache update is occurring we must
537 * wait until this QP security structure is processed
538 * in the QP to error flow before destroying it because
539 * the to_error_list is in use.
541 for (i
= 0; i
< sec
->error_comps_pending
; i
++)
542 wait_for_completion(&sec
->error_complete
);
544 destroy_qp_security(sec
);
547 void ib_security_cache_change(struct ib_device
*device
,
551 struct pkey_index_qp_list
*pkey
;
553 list_for_each_entry (pkey
, &device
->port_data
[port_num
].pkey_list
,
562 void ib_security_release_port_pkey_list(struct ib_device
*device
)
564 struct pkey_index_qp_list
*pkey
, *tmp_pkey
;
567 rdma_for_each_port (device
, i
) {
568 list_for_each_entry_safe(pkey
,
570 &device
->port_data
[i
].pkey_list
,
572 list_del(&pkey
->pkey_index_list
);
578 int ib_security_modify_qp(struct ib_qp
*qp
,
579 struct ib_qp_attr
*qp_attr
,
581 struct ib_udata
*udata
)
584 struct ib_ports_pkeys
*tmp_pps
;
585 struct ib_ports_pkeys
*new_pps
= NULL
;
586 struct ib_qp
*real_qp
= qp
->real_qp
;
587 bool special_qp
= (real_qp
->qp_type
== IB_QPT_SMI
||
588 real_qp
->qp_type
== IB_QPT_GSI
||
589 real_qp
->qp_type
>= IB_QPT_RESERVED1
);
590 bool pps_change
= ((qp_attr_mask
& (IB_QP_PKEY_INDEX
| IB_QP_PORT
)) ||
591 (qp_attr_mask
& IB_QP_ALT_PATH
));
593 WARN_ONCE((qp_attr_mask
& IB_QP_PORT
&&
594 rdma_protocol_ib(real_qp
->device
, qp_attr
->port_num
) &&
596 "%s: QP security is not initialized for IB QP: %d\n",
597 __func__
, real_qp
->qp_num
);
599 /* The port/pkey settings are maintained only for the real QP. Open
600 * handles on the real QP will be in the shared_qp_list. When
601 * enforcing security on the real QP all the shared QPs will be
605 if (pps_change
&& !special_qp
&& real_qp
->qp_sec
) {
606 mutex_lock(&real_qp
->qp_sec
->mutex
);
607 new_pps
= get_new_pps(real_qp
,
611 mutex_unlock(&real_qp
->qp_sec
->mutex
);
614 /* Add this QP to the lists for the new port
615 * and pkey settings before checking for permission
616 * in case there is a concurrent cache update
617 * occurring. Walking the list for a cache change
618 * doesn't acquire the security mutex unless it's
619 * sending the QP to error.
621 ret
= port_pkey_list_insert(&new_pps
->main
);
624 ret
= port_pkey_list_insert(&new_pps
->alt
);
627 ret
= check_qp_port_pkey_settings(new_pps
,
632 ret
= real_qp
->device
->ops
.modify_qp(real_qp
,
638 /* Clean up the lists and free the appropriate
639 * ports_pkeys structure.
644 tmp_pps
= real_qp
->qp_sec
->ports_pkeys
;
645 real_qp
->qp_sec
->ports_pkeys
= new_pps
;
649 port_pkey_list_remove(&tmp_pps
->main
);
650 port_pkey_list_remove(&tmp_pps
->alt
);
653 mutex_unlock(&real_qp
->qp_sec
->mutex
);
658 static int ib_security_pkey_access(struct ib_device
*dev
,
667 if (!rdma_protocol_ib(dev
, port_num
))
670 ret
= ib_get_cached_pkey(dev
, port_num
, pkey_index
, &pkey
);
674 ret
= ib_get_cached_subnet_prefix(dev
, port_num
, &subnet_prefix
);
679 return security_ib_pkey_access(sec
, subnet_prefix
, pkey
);
682 void ib_mad_agent_security_change(void)
684 struct ib_mad_agent
*ag
;
686 spin_lock(&mad_agent_list_lock
);
687 list_for_each_entry(ag
,
690 WRITE_ONCE(ag
->smp_allowed
,
691 !security_ib_endport_manage_subnet(ag
->security
,
692 dev_name(&ag
->device
->dev
), ag
->port_num
));
693 spin_unlock(&mad_agent_list_lock
);
696 int ib_mad_agent_security_setup(struct ib_mad_agent
*agent
,
697 enum ib_qp_type qp_type
)
701 if (!rdma_protocol_ib(agent
->device
, agent
->port_num
))
704 INIT_LIST_HEAD(&agent
->mad_agent_sec_list
);
706 ret
= security_ib_alloc_security(&agent
->security
);
710 if (qp_type
!= IB_QPT_SMI
)
713 spin_lock(&mad_agent_list_lock
);
714 ret
= security_ib_endport_manage_subnet(agent
->security
,
715 dev_name(&agent
->device
->dev
),
720 WRITE_ONCE(agent
->smp_allowed
, true);
721 list_add(&agent
->mad_agent_sec_list
, &mad_agent_list
);
722 spin_unlock(&mad_agent_list_lock
);
726 spin_unlock(&mad_agent_list_lock
);
727 security_ib_free_security(agent
->security
);
731 void ib_mad_agent_security_cleanup(struct ib_mad_agent
*agent
)
733 if (!rdma_protocol_ib(agent
->device
, agent
->port_num
))
736 if (agent
->qp
->qp_type
== IB_QPT_SMI
) {
737 spin_lock(&mad_agent_list_lock
);
738 list_del(&agent
->mad_agent_sec_list
);
739 spin_unlock(&mad_agent_list_lock
);
742 security_ib_free_security(agent
->security
);
745 int ib_mad_enforce_security(struct ib_mad_agent_private
*map
, u16 pkey_index
)
747 if (!rdma_protocol_ib(map
->agent
.device
, map
->agent
.port_num
))
750 if (map
->agent
.qp
->qp_type
== IB_QPT_SMI
) {
751 if (!READ_ONCE(map
->agent
.smp_allowed
))
756 return ib_security_pkey_access(map
->agent
.device
,
759 map
->agent
.security
);