1a0fcf16hselasky/*-
2a0fcf16hselasky * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3a0fcf16hselasky *
479ee7efhselasky * Copyright (c) 2014 Chelsio, Inc. All rights reserved.
579ee7efhselasky * Copyright (c) 2014 Intel Corporation. All rights reserved.
679ee7efhselasky *
779ee7efhselasky * This software is available to you under a choice of one of two
879ee7efhselasky * licenses.  You may choose to be licensed under the terms of the GNU
979ee7efhselasky * General Public License (GPL) Version 2, available from the file
1079ee7efhselasky * COPYING in the main directory of this source tree, or the
1179ee7efhselasky * OpenIB.org BSD license below:
1279ee7efhselasky *
1379ee7efhselasky *     Redistribution and use in source and binary forms, with or
1479ee7efhselasky *     without modification, are permitted provided that the following
1579ee7efhselasky *     conditions are met:
1679ee7efhselasky *
1779ee7efhselasky *      - Redistributions of source code must retain the above
1879ee7efhselasky *	  copyright notice, this list of conditions and the following
1979ee7efhselasky *	  disclaimer.
2079ee7efhselasky *
2179ee7efhselasky *      - Redistributions in binary form must reproduce the above
2279ee7efhselasky *	  copyright notice, this list of conditions and the following
2379ee7efhselasky *	  disclaimer in the documentation and/or other materials
2479ee7efhselasky *	  provided with the distribution.
2579ee7efhselasky *
2679ee7efhselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2779ee7efhselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2879ee7efhselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2979ee7efhselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
3079ee7efhselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3179ee7efhselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3279ee7efhselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3379ee7efhselasky * SOFTWARE.
3479ee7efhselasky */
3579ee7efhselasky
362692d50hselasky#include <sys/cdefs.h>
372692d50hselasky__FBSDID("$FreeBSD$");
382692d50hselasky
3979ee7efhselasky#include "iwpm_util.h"
4079ee7efhselasky
4179ee7efhselasky#define IWPM_MAPINFO_HASH_SIZE	512
4279ee7efhselasky#define IWPM_MAPINFO_HASH_MASK	(IWPM_MAPINFO_HASH_SIZE - 1)
4379ee7efhselasky#define IWPM_REMINFO_HASH_SIZE	64
4479ee7efhselasky#define IWPM_REMINFO_HASH_MASK	(IWPM_REMINFO_HASH_SIZE - 1)
4579ee7efhselasky#define IWPM_MSG_SIZE		512
4679ee7efhselasky
4779ee7efhselaskyint iwpm_compare_sockaddr(struct sockaddr_storage *a_sockaddr,
4879ee7efhselasky				struct sockaddr_storage *b_sockaddr)
4979ee7efhselasky{
5079ee7efhselasky	if (a_sockaddr->ss_family != b_sockaddr->ss_family)
5179ee7efhselasky		return 1;
5279ee7efhselasky	if (a_sockaddr->ss_family == AF_INET) {
5379ee7efhselasky		struct sockaddr_in *a4_sockaddr =
5479ee7efhselasky			(struct sockaddr_in *)a_sockaddr;
5579ee7efhselasky		struct sockaddr_in *b4_sockaddr =
5679ee7efhselasky			(struct sockaddr_in *)b_sockaddr;
5779ee7efhselasky		if (!memcmp(&a4_sockaddr->sin_addr,
5879ee7efhselasky			&b4_sockaddr->sin_addr, sizeof(struct in_addr))
5979ee7efhselasky			&& a4_sockaddr->sin_port == b4_sockaddr->sin_port)
6079ee7efhselasky				return 0;
6179ee7efhselasky
6279ee7efhselasky	} else if (a_sockaddr->ss_family == AF_INET6) {
6379ee7efhselasky		struct sockaddr_in6 *a6_sockaddr =
6479ee7efhselasky			(struct sockaddr_in6 *)a_sockaddr;
6579ee7efhselasky		struct sockaddr_in6 *b6_sockaddr =
6679ee7efhselasky			(struct sockaddr_in6 *)b_sockaddr;
6779ee7efhselasky		if (!memcmp(&a6_sockaddr->sin6_addr,
6879ee7efhselasky			&b6_sockaddr->sin6_addr, sizeof(struct in6_addr))
6979ee7efhselasky			&& a6_sockaddr->sin6_port == b6_sockaddr->sin6_port)
7079ee7efhselasky				return 0;
7179ee7efhselasky
7279ee7efhselasky	} else {
7379ee7efhselasky		pr_err("%s: Invalid sockaddr family\n", __func__);
7479ee7efhselasky	}
7579ee7efhselasky	return 1;
7679ee7efhselasky}
7779ee7efhselasky
7879ee7efhselaskyvoid iwpm_print_sockaddr(struct sockaddr_storage *sockaddr, char *msg)
7979ee7efhselasky{
8079ee7efhselasky	struct sockaddr_in6 *sockaddr_v6;
8179ee7efhselasky	struct sockaddr_in *sockaddr_v4;
8279ee7efhselasky
8379ee7efhselasky	switch (sockaddr->ss_family) {
8479ee7efhselasky	case AF_INET:
8579ee7efhselasky		sockaddr_v4 = (struct sockaddr_in *)sockaddr;
8679ee7efhselasky		pr_debug("%s IPV4 %pI4: %u(0x%04X)\n",
8779ee7efhselasky			msg, &sockaddr_v4->sin_addr,
8879ee7efhselasky			ntohs(sockaddr_v4->sin_port),
8979ee7efhselasky			ntohs(sockaddr_v4->sin_port));
9079ee7efhselasky		break;
9179ee7efhselasky	case AF_INET6:
9279ee7efhselasky		sockaddr_v6 = (struct sockaddr_in6 *)sockaddr;
9379ee7efhselasky		pr_debug("%s IPV6 %pI6: %u(0x%04X)\n",
9479ee7efhselasky			msg, &sockaddr_v6->sin6_addr,
9579ee7efhselasky			ntohs(sockaddr_v6->sin6_port),
9679ee7efhselasky			ntohs(sockaddr_v6->sin6_port));
9779ee7efhselasky		break;
9879ee7efhselasky	default:
9979ee7efhselasky		break;
10079ee7efhselasky	}
10179ee7efhselasky}
10279ee7efhselasky
103