1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5270710Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28290045Shselasky *
29290045Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/net/ipv6.h 331756 2018-03-30 02:04:46Z emaste $
30219820Sjeff */
31219820Sjeff#ifndef _LINUX_NET_IPV6_H_
32219820Sjeff#define	_LINUX_NET_IPV6_H_
33219820Sjeff
34290335Shselasky#include <sys/types.h>
35290335Shselasky#include <netinet/in.h>
36290335Shselasky#include <linux/types.h>
37219820Sjeff
38328653Shselasky#define	IPV6_DEFAULT_HOPLIMIT 64
39328653Shselasky
40328653Shselasky#define	ipv6_addr_loopback(addr) IN6_IS_ADDR_LOOPBACK(addr)
41328653Shselasky#define	ipv6_addr_any(addr) IN6_IS_ADDR_UNSPECIFIED(addr)
42328653Shselasky
43328653Shselasky#define	ipv6_addr_copy(dst, src)			\
44219820Sjeff	memcpy((dst), (src), sizeof(struct in6_addr))
45219820Sjeff
46219820Sjeffstatic inline void
47219820Sjeffipv6_ib_mc_map(const struct in6_addr *addr, const unsigned char *broadcast,
48219820Sjeff    char *buf)
49219820Sjeff{
50219820Sjeff	unsigned char scope;
51219820Sjeff
52219820Sjeff	scope = broadcast[5] & 0xF;
53219820Sjeff	buf[0]  = 0;
54219820Sjeff	buf[1]  = 0xff;
55219820Sjeff	buf[2]  = 0xff;
56219820Sjeff	buf[3]  = 0xff;
57219820Sjeff	buf[4]  = 0xff;
58219820Sjeff	buf[5]  = 0x10 | scope;
59219820Sjeff	buf[6]  = 0x60;
60219820Sjeff	buf[7]  = 0x1b;
61219820Sjeff	buf[8]  = broadcast[8];
62219820Sjeff	buf[9]  = broadcast[9];
63219820Sjeff	memcpy(&buf[10], &addr->s6_addr[6], 10);
64219820Sjeff}
65219820Sjeff
66331756Semastestatic inline void __ipv6_addr_set_half(__be32 *addr, __be32 wh, __be32 wl)
67270710Shselasky{
68270710Shselasky#if BITS_PER_LONG == 64
69270710Shselasky#if defined(__BIG_ENDIAN)
70331756Semaste	if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) {
71331756Semaste		*(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl));
72331756Semaste		return;
73331756Semaste	}
74270710Shselasky#elif defined(__LITTLE_ENDIAN)
75331756Semaste	if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) {
76331756Semaste		*(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh));
77331756Semaste		return;
78331756Semaste	}
79270710Shselasky#endif
80270710Shselasky#endif
81331756Semaste	addr[0] = wh;
82331756Semaste	addr[1] = wl;
83270710Shselasky}
84270710Shselasky
85270710Shselaskystatic inline void ipv6_addr_set(struct in6_addr *addr,
86331756Semaste				     __be32 w1, __be32 w2,
87331756Semaste				     __be32 w3, __be32 w4)
88270710Shselasky{
89331756Semaste	__ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2);
90331756Semaste	__ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4);
91270710Shselasky}
92270710Shselasky
93270710Shselaskystatic inline void ipv6_addr_set_v4mapped(const __be32 addr,
94270710Shselasky					  struct in6_addr *v4mapped)
95270710Shselasky{
96270710Shselasky	ipv6_addr_set(v4mapped,
97270710Shselasky			0, 0,
98270710Shselasky			htonl(0x0000FFFF),
99270710Shselasky			addr);
100270710Shselasky}
101270710Shselasky
102270710Shselaskystatic inline int ipv6_addr_v4mapped(const struct in6_addr *a)
103270710Shselasky{
104270710Shselasky	return ((a->s6_addr32[0] | a->s6_addr32[1] |
105270710Shselasky		(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0);
106270710Shselasky}
107270710Shselasky
108270710Shselaskystatic inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2)
109270710Shselasky{
110331756Semaste	return memcmp(a1, a2, sizeof(struct in6_addr));
111270710Shselasky}
112270710Shselasky
113270710Shselasky
114219820Sjeff#endif	/* _LINUX_NET_IPV6_H_ */
115