1331772Shselasky/*-
2331772Shselasky * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3331772Shselasky *
4319974Shselasky * Copyright (c) 2014 Chelsio, Inc. All rights reserved.
5319974Shselasky * Copyright (c) 2014 Intel Corporation. All rights reserved.
6319974Shselasky *
7319974Shselasky * This software is available to you under a choice of one of two
8319974Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
9319974Shselasky * General Public License (GPL) Version 2, available from the file
10319974Shselasky * COPYING in the main directory of this source tree, or the
11319974Shselasky * OpenIB.org BSD license below:
12319974Shselasky *
13319974Shselasky *     Redistribution and use in source and binary forms, with or
14319974Shselasky *     without modification, are permitted provided that the following
15319974Shselasky *     conditions are met:
16319974Shselasky *
17319974Shselasky *      - Redistributions of source code must retain the above
18319974Shselasky *	  copyright notice, this list of conditions and the following
19319974Shselasky *	  disclaimer.
20319974Shselasky *
21319974Shselasky *      - Redistributions in binary form must reproduce the above
22319974Shselasky *	  copyright notice, this list of conditions and the following
23319974Shselasky *	  disclaimer in the documentation and/or other materials
24319974Shselasky *	  provided with the distribution.
25319974Shselasky *
26319974Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27319974Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28319974Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29319974Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30319974Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31319974Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32319974Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33319974Shselasky * SOFTWARE.
34319974Shselasky */
35319974Shselasky
36337096Shselasky#include <sys/cdefs.h>
37337096Shselasky__FBSDID("$FreeBSD: stable/11/sys/ofed/drivers/infiniband/core/ib_iwpm_util.c 337096 2018-08-02 08:33:51Z hselasky $");
38337096Shselasky
39319974Shselasky#include "iwpm_util.h"
40319974Shselasky
41319974Shselasky#define IWPM_MAPINFO_HASH_SIZE	512
42319974Shselasky#define IWPM_MAPINFO_HASH_MASK	(IWPM_MAPINFO_HASH_SIZE - 1)
43319974Shselasky#define IWPM_REMINFO_HASH_SIZE	64
44319974Shselasky#define IWPM_REMINFO_HASH_MASK	(IWPM_REMINFO_HASH_SIZE - 1)
45319974Shselasky#define IWPM_MSG_SIZE		512
46319974Shselasky
47319974Shselaskyint iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
48319974Shselasky				struct sockaddr_storage *b_sockaddr)
49319974Shselasky{
50319974Shselasky	if (a_sockaddr->ss_family != b_sockaddr->ss_family)
51319974Shselasky		return 1;
52319974Shselasky	if (a_sockaddr->ss_family == AF_INET) {
53319974Shselasky		struct sockaddr_in *a4_sockaddr =
54319974Shselasky			(struct sockaddr_in *)a_sockaddr;
55319974Shselasky		struct sockaddr_in *b4_sockaddr =
56319974Shselasky			(struct sockaddr_in *)b_sockaddr;
57319974Shselasky		if (!memcmp(&a4_sockaddr->sin_addr,
58319974Shselasky			&b4_sockaddr->sin_addr, sizeof(struct in_addr))
59319974Shselasky			&& a4_sockaddr->sin_port == b4_sockaddr->sin_port)
60319974Shselasky				return 0;
61319974Shselasky
62319974Shselasky	} else if (a_sockaddr->ss_family == AF_INET6) {
63319974Shselasky		struct sockaddr_in6 *a6_sockaddr =
64319974Shselasky			(struct sockaddr_in6 *)a_sockaddr;
65319974Shselasky		struct sockaddr_in6 *b6_sockaddr =
66319974Shselasky			(struct sockaddr_in6 *)b_sockaddr;
67319974Shselasky		if (!memcmp(&a6_sockaddr->sin6_addr,
68319974Shselasky			&b6_sockaddr->sin6_addr, sizeof(struct in6_addr))
69319974Shselasky			&& a6_sockaddr->sin6_port == b6_sockaddr->sin6_port)
70319974Shselasky				return 0;
71319974Shselasky
72319974Shselasky	} else {
73319974Shselasky		pr_err("%s: Invalid sockaddr family\n", __func__);
74319974Shselasky	}
75319974Shselasky	return 1;
76319974Shselasky}
77319974Shselasky
78319974Shselaskyvoid iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg)
79319974Shselasky{
80319974Shselasky	struct sockaddr_in6 *sockaddr_v6;
81319974Shselasky	struct sockaddr_in *sockaddr_v4;
82319974Shselasky
83319974Shselasky	switch (sockaddr->ss_family) {
84319974Shselasky	case AF_INET:
85319974Shselasky		sockaddr_v4 = (struct sockaddr_in *)sockaddr;
86319974Shselasky		pr_debug("%s IPV4 %pI4: %u(0x%04X)\n",
87319974Shselasky			msg, &sockaddr_v4->sin_addr,
88319974Shselasky			ntohs(sockaddr_v4->sin_port),
89319974Shselasky			ntohs(sockaddr_v4->sin_port));
90319974Shselasky		break;
91319974Shselasky	case AF_INET6:
92319974Shselasky		sockaddr_v6 = (struct sockaddr_in6 *)sockaddr;
93319974Shselasky		pr_debug("%s IPV6 %pI6: %u(0x%04X)\n",
94319974Shselasky			msg, &sockaddr_v6->sin6_addr,
95319974Shselasky			ntohs(sockaddr_v6->sin6_port),
96319974Shselasky			ntohs(sockaddr_v6->sin6_port));
97319974Shselasky		break;
98319974Shselasky	default:
99319974Shselasky		break;
100319974Shselasky	}
101319974Shselasky}
102319974Shselasky
103