/*-1* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.02*3* Copyright (c) 2010 Intel Corporation. All rights reserved.4*5* This software is available to you under a choice of one of two6* licenses. You may choose to be licensed under the terms of the GNU7* General Public License (GPL) Version 2, available from the file8* COPYING in the main directory of this source tree, or the9* OpenIB.org BSD license below:10*11* Redistribution and use in source and binary forms, with or12* without modification, are permitted provided that the following13* conditions are met:14*15* - Redistributions of source code must retain the above16* copyright notice, this list of conditions and the following17* disclaimer.18*19* - Redistributions in binary form must reproduce the above20* copyright notice, this list of conditions and the following21* disclaimer in the documentation and/or other materials22* provided with the distribution.23*24* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,25* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF26* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND27* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS28* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN29* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN30* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE31* SOFTWARE.32*/3334#if !defined(_RDMA_IB_H)35#define _RDMA_IB_H3637#include <linux/types.h>38#include <linux/sched.h>39#include <linux/file.h>4041/*42* Define a native infiniband address as in Linux upstream43* 8d36eb01da5d371feffa280e501377b5c450f5a544*/45#define AF_IB 414647struct ib_addr {48union {49__u8 uib_addr8[16];50__be16 uib_addr16[8];51__be32 uib_addr32[4];52__be64 uib_addr64[2];53} ib_u;54#define sib_addr8 ib_u.uib_addr855#define sib_addr16 ib_u.uib_addr1656#define sib_addr32 ib_u.uib_addr3257#define sib_addr64 ib_u.uib_addr6458#define sib_raw ib_u.uib_addr859#define sib_subnet_prefix ib_u.uib_addr64[0]60#define sib_interface_id ib_u.uib_addr64[1]61};6263static inline int ib_addr_any(const struct ib_addr *a)64{65return ((a->sib_addr64[0] | a->sib_addr64[1]) == 0);66}6768static inline int ib_addr_loopback(const struct ib_addr *a)69{70return ((a->sib_addr32[0] | a->sib_addr32[1] |71a->sib_addr32[2] | (a->sib_addr32[3] ^ htonl(1))) == 0);72}7374static inline void ib_addr_set(struct ib_addr *addr,75__be32 w1, __be32 w2, __be32 w3, __be32 w4)76{77addr->sib_addr32[0] = w1;78addr->sib_addr32[1] = w2;79addr->sib_addr32[2] = w3;80addr->sib_addr32[3] = w4;81}8283static inline int ib_addr_cmp(const struct ib_addr *a1, const struct ib_addr *a2)84{85return memcmp(a1, a2, sizeof(struct ib_addr));86}8788struct sockaddr_ib {89unsigned short int sib_family; /* AF_IB */90__be16 sib_pkey;91__be32 sib_flowinfo;92struct ib_addr sib_addr;93__be64 sib_sid;94__be64 sib_sid_mask;95__u64 sib_scope_id;96};9798/*99* The IB interfaces that use write() as bi-directional ioctl() are100* fundamentally unsafe, since there are lots of ways to trigger "write()"101* calls from various contexts with elevated privileges. That includes the102* traditional suid executable error message writes, but also various kernel103* interfaces that can write to file descriptors.104*105* This function provides protection for the legacy API by restricting the106* calling context.107*/108static inline bool ib_safe_file_access(struct file *filp)109{110struct thread *td = curthread;111112/*113* Check if called from userspace through a devfs related114* system call belonging to the given file:115*/116return (filp->_file != NULL &&117filp->_file == td->td_fpop &&118filp->_file->f_cred == td->td_ucred);119}120121#endif /* _RDMA_IB_H */122123124