xref: /openbmc/linux/include/rdma/ib.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
16bf9d8f6SLeon Romanovsky /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
28d36eb01SSean Hefty /*
38d36eb01SSean Hefty  * Copyright (c) 2010 Intel Corporation.  All rights reserved.
48d36eb01SSean Hefty  */
58d36eb01SSean Hefty 
66bf9d8f6SLeon Romanovsky #ifndef _RDMA_IB_H
78d36eb01SSean Hefty #define _RDMA_IB_H
88d36eb01SSean Hefty 
98d36eb01SSean Hefty #include <linux/types.h>
10e6bd18f5SJason Gunthorpe #include <linux/sched.h>
115b825c3aSIngo Molnar #include <linux/cred.h>
1239289bfcSJason Gunthorpe #include <linux/uaccess.h>
1339289bfcSJason Gunthorpe #include <linux/fs.h>
148d36eb01SSean Hefty 
158d36eb01SSean Hefty struct ib_addr {
168d36eb01SSean Hefty 	union {
178d36eb01SSean Hefty 		__u8		uib_addr8[16];
188d36eb01SSean Hefty 		__be16		uib_addr16[8];
198d36eb01SSean Hefty 		__be32		uib_addr32[4];
208d36eb01SSean Hefty 		__be64		uib_addr64[2];
218d36eb01SSean Hefty 	} ib_u;
228d36eb01SSean Hefty #define sib_addr8		ib_u.uib_addr8
238d36eb01SSean Hefty #define sib_addr16		ib_u.uib_addr16
248d36eb01SSean Hefty #define sib_addr32		ib_u.uib_addr32
258d36eb01SSean Hefty #define sib_addr64		ib_u.uib_addr64
268d36eb01SSean Hefty #define sib_raw			ib_u.uib_addr8
278d36eb01SSean Hefty #define sib_subnet_prefix	ib_u.uib_addr64[0]
288d36eb01SSean Hefty #define sib_interface_id	ib_u.uib_addr64[1]
298d36eb01SSean Hefty };
308d36eb01SSean Hefty 
ib_addr_any(const struct ib_addr * a)31ca3a8aceSParav Pandit static inline bool ib_addr_any(const struct ib_addr *a)
328d36eb01SSean Hefty {
338d36eb01SSean Hefty 	return ((a->sib_addr64[0] | a->sib_addr64[1]) == 0);
348d36eb01SSean Hefty }
358d36eb01SSean Hefty 
ib_addr_loopback(const struct ib_addr * a)36ca3a8aceSParav Pandit static inline bool ib_addr_loopback(const struct ib_addr *a)
378d36eb01SSean Hefty {
388d36eb01SSean Hefty 	return ((a->sib_addr32[0] | a->sib_addr32[1] |
398d36eb01SSean Hefty 		 a->sib_addr32[2] | (a->sib_addr32[3] ^ htonl(1))) == 0);
408d36eb01SSean Hefty }
418d36eb01SSean Hefty 
ib_addr_set(struct ib_addr * addr,__be32 w1,__be32 w2,__be32 w3,__be32 w4)428d36eb01SSean Hefty static inline void ib_addr_set(struct ib_addr *addr,
438d36eb01SSean Hefty 			       __be32 w1, __be32 w2, __be32 w3, __be32 w4)
448d36eb01SSean Hefty {
458d36eb01SSean Hefty 	addr->sib_addr32[0] = w1;
468d36eb01SSean Hefty 	addr->sib_addr32[1] = w2;
478d36eb01SSean Hefty 	addr->sib_addr32[2] = w3;
488d36eb01SSean Hefty 	addr->sib_addr32[3] = w4;
498d36eb01SSean Hefty }
508d36eb01SSean Hefty 
ib_addr_cmp(const struct ib_addr * a1,const struct ib_addr * a2)518d36eb01SSean Hefty static inline int ib_addr_cmp(const struct ib_addr *a1, const struct ib_addr *a2)
528d36eb01SSean Hefty {
538d36eb01SSean Hefty 	return memcmp(a1, a2, sizeof(struct ib_addr));
548d36eb01SSean Hefty }
558d36eb01SSean Hefty 
568d36eb01SSean Hefty struct sockaddr_ib {
578d36eb01SSean Hefty 	unsigned short int	sib_family;	/* AF_IB */
588d36eb01SSean Hefty 	__be16			sib_pkey;
598d36eb01SSean Hefty 	__be32			sib_flowinfo;
608d36eb01SSean Hefty 	struct ib_addr		sib_addr;
618d36eb01SSean Hefty 	__be64			sib_sid;
628d36eb01SSean Hefty 	__be64			sib_sid_mask;
638d36eb01SSean Hefty 	__u64			sib_scope_id;
648d36eb01SSean Hefty };
658d36eb01SSean Hefty 
66e6bd18f5SJason Gunthorpe /*
67e6bd18f5SJason Gunthorpe  * The IB interfaces that use write() as bi-directional ioctl() are
68e6bd18f5SJason Gunthorpe  * fundamentally unsafe, since there are lots of ways to trigger "write()"
69e6bd18f5SJason Gunthorpe  * calls from various contexts with elevated privileges. That includes the
70e6bd18f5SJason Gunthorpe  * traditional suid executable error message writes, but also various kernel
71e6bd18f5SJason Gunthorpe  * interfaces that can write to file descriptors.
72e6bd18f5SJason Gunthorpe  *
73e6bd18f5SJason Gunthorpe  * This function provides protection for the legacy API by restricting the
74e6bd18f5SJason Gunthorpe  * calling context.
75e6bd18f5SJason Gunthorpe  */
ib_safe_file_access(struct file * filp)76e6bd18f5SJason Gunthorpe static inline bool ib_safe_file_access(struct file *filp)
77e6bd18f5SJason Gunthorpe {
78*967747bbSArnd Bergmann 	return filp->f_cred == current_cred();
79e6bd18f5SJason Gunthorpe }
80e6bd18f5SJason Gunthorpe 
818d36eb01SSean Hefty #endif /* _RDMA_IB_H */
82