1/*-
2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3 *
4 * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
5 * Copyright (c) 2005 Intel Corporation.  All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses.  You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 *     Redistribution and use in source and binary forms, with or
14 *     without modification, are permitted provided that the following
15 *     conditions are met:
16 *
17 *      - Redistributions of source code must retain the above
18 *        copyright notice, this list of conditions and the following
19 *        disclaimer.
20 *
21 *      - Redistributions in binary form must reproduce the above
22 *        copyright notice, this list of conditions and the following
23 *        disclaimer in the documentation and/or other materials
24 *        provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36#if !defined(IB_ADDR_H)
37#define IB_ADDR_H
38
39#include <linux/in.h>
40#include <linux/in6.h>
41#include <linux/if_arp.h>
42#include <linux/netdevice.h>
43#include <linux/socket.h>
44#include <linux/if_vlan.h>
45#include <net/ipv6.h>
46#include <net/if_inet6.h>
47#include <net/ip.h>
48#include <rdma/ib_verbs.h>
49#include <rdma/ib_pack.h>
50#include <rdma/ib_addr_freebsd.h>
51
52/* Linux netdevice.h but for working on an ifnet rather than a net_device. */
53#define	dev_hold(d)	if_ref(d)
54#define	dev_put(d)	if_rele(d)
55#define	dev_net(d)	if_getvnet(d)
56#define	net_eq(a,b)	((a) == (b))
57
58
59struct rdma_addr_client {
60	atomic_t refcount;
61	struct completion comp;
62};
63
64union rdma_sockaddr {
65	struct sockaddr         _sockaddr;
66	struct sockaddr_in      _sockaddr_in;
67	struct sockaddr_in6     _sockaddr_in6;
68	struct sockaddr_storage _sockaddr_ss;
69};
70
71/**
72 * rdma_addr_register_client - Register an address client.
73 */
74void rdma_addr_register_client(struct rdma_addr_client *client);
75
76/**
77 * rdma_addr_unregister_client - Deregister an address client.
78 * @client: Client object to deregister.
79 */
80void rdma_addr_unregister_client(struct rdma_addr_client *client);
81
82/**
83 * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
84 * @src_dev_addr:	Source MAC address.
85 * @dst_dev_addr:	Destination MAC address.
86 * @broadcast:		Broadcast address of the device.
87 * @dev_type:		The interface hardware type of the device.
88 * @bound_dev_if:	An optional device interface index.
89 * @transport:		The transport type used.
90 * @net:		Network namespace containing the bound_dev_if net_dev.
91 */
92struct vnet;
93struct rdma_dev_addr {
94	unsigned char src_dev_addr[MAX_ADDR_LEN];
95	unsigned char dst_dev_addr[MAX_ADDR_LEN];
96	unsigned char broadcast[MAX_ADDR_LEN];
97	unsigned short dev_type;
98	int bound_dev_if;
99	enum rdma_transport_type transport;
100	struct vnet *net;
101	enum rdma_network_type network;
102	int hoplimit;
103};
104
105/**
106 * rdma_translate_ip - Translate a local IP address to an RDMA hardware
107 *   address.
108 *
109 * The dev_addr->net and dev_addr->bound_dev_if fields must be initialized.
110 */
111int rdma_translate_ip(const struct sockaddr *addr,
112		      struct rdma_dev_addr *dev_addr);
113
114/**
115 * rdma_resolve_ip - Resolve source and destination IP addresses to
116 *   RDMA hardware addresses.
117 * @client: Address client associated with request.
118 * @src_addr: An optional source address to use in the resolution.  If a
119 *   source address is not provided, a usable address will be returned via
120 *   the callback.
121 * @dst_addr: The destination address to resolve.
122 * @addr: A reference to a data location that will receive the resolved
123 *   addresses.  The data location must remain valid until the callback has
124 *   been invoked. The net field of the addr struct must be valid.
125 * @timeout_ms: Amount of time to wait for the address resolution to complete.
126 * @callback: Call invoked once address resolution has completed, timed out,
127 *   or been canceled.  A status of 0 indicates success.
128 * @context: User-specified context associated with the call.
129 */
130int rdma_resolve_ip(struct rdma_addr_client *client,
131		    struct sockaddr *src_addr, struct sockaddr *dst_addr,
132		    struct rdma_dev_addr *addr, int timeout_ms,
133		    void (*callback)(int status, struct sockaddr *src_addr,
134				     struct rdma_dev_addr *addr, void *context),
135		    void *context);
136
137int rdma_resolve_ip_route(struct sockaddr *src_addr,
138			  const struct sockaddr *dst_addr,
139			  struct rdma_dev_addr *addr);
140
141void rdma_addr_cancel(struct rdma_dev_addr *addr);
142
143int rdma_copy_addr(struct rdma_dev_addr *dev_addr, if_t dev,
144	      const unsigned char *dst_dev_addr);
145
146int rdma_addr_size(struct sockaddr *addr);
147int rdma_addr_size_in6(struct sockaddr_in6 *addr);
148int rdma_addr_size_kss(struct sockaddr_storage *addr);
149
150int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
151				 const union ib_gid *dgid,
152				 u8 *smac, if_t dev,
153				 int *hoplimit);
154
155static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
156{
157	return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
158}
159
160static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
161{
162	dev_addr->broadcast[8] = pkey >> 8;
163	dev_addr->broadcast[9] = (unsigned char) pkey;
164}
165
166static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
167				    union ib_gid *gid)
168{
169	memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
170}
171
172static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
173{
174	return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
175}
176
177static inline u16 rdma_vlan_dev_vlan_id(if_t dev)
178{
179	uint16_t tag;
180
181	if (if_gettype(dev) == IFT_ETHER && if_getpcp(dev) != IFNET_PCP_NONE)
182		return 0x0000;	/* prio-tagged traffic */
183	if (VLAN_TAG(__DECONST(if_t, dev), &tag) != 0)
184		return 0xffff;
185	return tag;
186}
187
188static inline int rdma_ip2gid(const struct sockaddr *addr, union ib_gid *gid)
189{
190	switch (addr->sa_family) {
191	case AF_INET:
192		ipv6_addr_set_v4mapped(((const struct sockaddr_in *)
193					addr)->sin_addr.s_addr,
194				       (struct in6_addr *)gid);
195		break;
196	case AF_INET6:
197		memcpy(gid->raw, &((const struct sockaddr_in6 *)addr)->sin6_addr, 16);
198		/* make sure scope ID gets zeroed inside GID */
199		if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)gid->raw) ||
200		    IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)gid->raw)) {
201			gid->raw[2] = 0;
202			gid->raw[3] = 0;
203		}
204		break;
205	default:
206		return -EINVAL;
207	}
208	return 0;
209}
210
211/* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
212static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
213{
214	if (ipv6_addr_v4mapped((const struct in6_addr *)gid)) {
215		struct sockaddr_in *out_in = (struct sockaddr_in *)out;
216		memset(out_in, 0, sizeof(*out_in));
217		out_in->sin_len = sizeof(*out_in);
218		out_in->sin_family = AF_INET;
219		memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
220	} else {
221		struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
222		memset(out_in, 0, sizeof(*out_in));
223		out_in->sin6_len = sizeof(*out_in);
224		out_in->sin6_family = AF_INET6;
225		memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
226	}
227}
228
229static u_int
230_iboe_addr_get_sgid_ia_cb(void *arg, struct ifaddr *ifa, u_int count __unused)
231{
232	ipv6_addr_set_v4mapped(((struct sockaddr_in *)
233			       ifa->ifa_addr)->sin_addr.s_addr,
234			       (struct in6_addr *)arg);
235	return (0);
236}
237
238static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr,
239				      union ib_gid *gid)
240{
241	if_t dev;
242
243#ifdef VIMAGE
244	if (dev_addr->net == NULL)
245		return;
246#endif
247	dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
248	if (dev) {
249		if_foreach_addr_type(dev, AF_INET,
250				     _iboe_addr_get_sgid_ia_cb, gid);
251		dev_put(dev);
252	}
253}
254
255static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
256{
257	if (dev_addr->transport == RDMA_TRANSPORT_IB &&
258	    dev_addr->dev_type != ARPHRD_INFINIBAND)
259		iboe_addr_get_sgid(dev_addr, gid);
260	else
261		memcpy(gid, dev_addr->src_dev_addr +
262		       rdma_addr_gid_offset(dev_addr), sizeof *gid);
263}
264
265static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
266{
267	memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
268}
269
270static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
271{
272	memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
273}
274
275static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
276{
277	memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
278}
279
280static inline enum ib_mtu iboe_get_mtu(int mtu)
281{
282	/*
283	 * reduce IB headers from effective IBoE MTU. 28 stands for
284	 * atomic header which is the biggest possible header after BTH
285	 */
286	mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28;
287
288	if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
289		return IB_MTU_4096;
290	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
291		return IB_MTU_2048;
292	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
293		return IB_MTU_1024;
294	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
295		return IB_MTU_512;
296	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
297		return IB_MTU_256;
298	else
299		return 0;
300}
301
302static inline int iboe_get_rate(if_t dev)
303{
304	uint64_t baudrate = if_getbaudrate(dev);
305#ifdef if_baudrate_pf
306	int exp;
307	for (exp = dev->if_baudrate_pf; exp > 0; exp--)
308		baudrate *= 10;
309#endif
310	if (baudrate >= IF_Gbps(40))
311		return IB_RATE_40_GBPS;
312	else if (baudrate >= IF_Gbps(30))
313		return IB_RATE_30_GBPS;
314	else if (baudrate >= IF_Gbps(20))
315		return IB_RATE_20_GBPS;
316	else if (baudrate >= IF_Gbps(10))
317		return IB_RATE_10_GBPS;
318	else
319		return IB_RATE_PORT_CURRENT;
320}
321
322static inline int rdma_link_local_addr(struct in6_addr *addr)
323{
324	if (addr->s6_addr32[0] == htonl(0xfe800000) &&
325	    addr->s6_addr32[1] == 0)
326		return 1;
327
328	return 0;
329}
330
331static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
332{
333	memcpy(mac, &addr->s6_addr[8], 3);
334	memcpy(mac + 3, &addr->s6_addr[13], 3);
335	mac[0] ^= 2;
336}
337
338static inline int rdma_is_multicast_addr(struct in6_addr *addr)
339{
340	__be32 ipv4_addr;
341
342	if (addr->s6_addr[0] == 0xff)
343		return 1;
344
345	ipv4_addr = addr->s6_addr32[3];
346	return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
347}
348
349static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
350{
351	int i;
352
353	mac[0] = 0x33;
354	mac[1] = 0x33;
355	for (i = 2; i < 6; ++i)
356		mac[i] = addr->s6_addr[i + 10];
357}
358
359static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
360{
361	u16 vid;
362
363	vid = dgid->raw[11] << 8 | dgid->raw[12];
364	return vid < 0x1000 ? vid : 0xffff;
365}
366
367static inline if_t rdma_vlan_dev_real_dev(if_t dev)
368{
369	struct epoch_tracker et;
370
371	NET_EPOCH_ENTER(et);
372	if (if_gettype(dev) != IFT_ETHER || if_getpcp(dev) == IFNET_PCP_NONE)
373		dev = VLAN_TRUNKDEV(dev);	/* non prio-tagged traffic */
374	NET_EPOCH_EXIT(et);
375	return (dev);
376}
377
378#endif /* IB_ADDR_H */
379