1 /*
2  * The olsr.org Optimized Link-State Routing daemon (olsrd)
3  *
4  * (c) by the OLSR project
5  *
6  * See our Git repository to find out who worked on this file
7  * and thus is a copyright holder on it.
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  *   notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above copyright
18  *   notice, this list of conditions and the following disclaimer in
19  *   the documentation and/or other materials provided with the
20  *   distribution.
21  * * Neither the name of olsr.org, olsrd nor the names of its
22  *   contributors may be used to endorse or promote products derived
23  *   from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * Visit http://www.olsr.org for more information.
39  *
40  * If you find this software useful feel free to make a donation
41  * to the project. For more information see the website or contact
42  * the copyright holders.
43  *
44  */
45 
46 #ifndef _IPCALC
47 #define _IPCALC
48 
49 #include "olsr_types.h"
50 #include "defs.h"
51 
52 #include <limits.h>
53 #include <arpa/inet.h>
54 #include <netinet/in.h>
55 
56 extern const struct olsr_ip_prefix ipv4_internet_route;
57 extern const struct olsr_ip_prefix ipv6_mappedv4_route;
58 extern const struct olsr_ip_prefix ipv6_internet_route;
59 
60 extern const union olsr_ip_addr olsr_ip_zero;
61 
62 extern const union olsr_ip_addr ipv6_def_multicast;
63 
64 struct ipaddr_str {
65   char buf[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
66 } __attribute__ ((unused));
67 
68 /*
69  * Macros for comparing and copying IP addresses
70  */
71 static INLINE int
ip4cmp(const struct in_addr * a,const struct in_addr * b)72 ip4cmp(const struct in_addr *a, const struct in_addr *b)
73 {
74   return a->s_addr > b->s_addr ? +1 : a->s_addr < b->s_addr ? -1 : 0;
75 }
76 static INLINE int
ip4equal(const struct in_addr * a,const struct in_addr * b)77 ip4equal(const struct in_addr *a, const struct in_addr *b)
78 {
79   return a->s_addr == b->s_addr;
80 }
81 
82 static INLINE int
ip6cmp(const struct in6_addr * a,const struct in6_addr * b)83 ip6cmp(const struct in6_addr *a, const struct in6_addr *b)
84 {
85   return memcmp(a, b, sizeof(*a));
86 }
87 static INLINE int
ip6equal(const struct in6_addr * a,const struct in6_addr * b)88 ip6equal(const struct in6_addr *a, const struct in6_addr *b)
89 {
90   return ip6cmp(a, b) == 0;
91 }
92 
93 static INLINE int
ipequal(const union olsr_ip_addr * a,const union olsr_ip_addr * b)94 ipequal(const union olsr_ip_addr *a, const union olsr_ip_addr *b)
95 {
96   return olsr_cnf->ip_version == AF_INET ? ip4equal(&a->v4, &b->v4) : ip6equal(&a->v6, &b->v6);
97 }
98 
99 /* Do not use this - this is as evil as the COPY_IP() macro was and only used in
100  * source which also needs cleanups.
101  */
102 static INLINE void
genipcopy(void * dst,const void * src)103 genipcopy(void *dst, const void *src)
104 {
105   memcpy(dst, src, olsr_cnf->ipsize);
106 }
107 
108 int ip_in_net(const union olsr_ip_addr *ipaddr, const struct olsr_ip_prefix *net);
109 
110 int prefix_to_netmask(uint8_t *, int, uint8_t);
111 
112 static INLINE int
olsr_prefix_to_netmask(union olsr_ip_addr * adr,uint8_t prefixlen)113 olsr_prefix_to_netmask(union olsr_ip_addr *adr, uint8_t prefixlen)
114 {
115   return prefix_to_netmask(adr->v6.s6_addr, olsr_cnf->ipsize, prefixlen);
116 }
117 
118 uint8_t netmask_to_prefix(const uint8_t *, int);
119 
120 static INLINE uint8_t
olsr_netmask_to_prefix(const union olsr_ip_addr * adr)121 olsr_netmask_to_prefix(const union olsr_ip_addr *adr)
122 {
123   return netmask_to_prefix(adr->v6.s6_addr, olsr_cnf->ipsize);
124 }
125 
126 static INLINE uint8_t
olsr_netmask4_to_prefix(const uint32_t * a)127 olsr_netmask4_to_prefix(const uint32_t * a)
128 {
129   return netmask_to_prefix((const uint8_t *)a, sizeof(*a));
130 }
131 static INLINE uint8_t
olsr_netmask6_to_prefix(const struct in6_addr * a)132 olsr_netmask6_to_prefix(const struct in6_addr *a)
133 {
134   return netmask_to_prefix((const uint8_t *)a, sizeof(*a));
135 }
136 
137 static INLINE const char *
ip4_to_string(struct ipaddr_str * const buf,const struct in_addr addr4)138 ip4_to_string(struct ipaddr_str *const buf, const struct in_addr addr4)
139 {
140   return inet_ntop(AF_INET, &addr4, buf->buf, sizeof(buf->buf));
141 }
142 
143 static INLINE const char *
ip6_to_string(struct ipaddr_str * const buf,const struct in6_addr * const addr6)144 ip6_to_string(struct ipaddr_str *const buf, const struct in6_addr *const addr6)
145 {
146   return inet_ntop(AF_INET6, addr6, buf->buf, sizeof(buf->buf));
147 }
148 
149 static INLINE const char *
olsr_ip_to_string(struct ipaddr_str * const buf,const union olsr_ip_addr * addr)150 olsr_ip_to_string(struct ipaddr_str *const buf, const union olsr_ip_addr *addr)
151 {
152   return inet_ntop(olsr_cnf->ip_version, addr, buf->buf, sizeof(buf->buf));
153 }
154 
155 const char *
156 olsr_ip_prefix_to_string(const struct olsr_ip_prefix *prefix);
157 
158 int
159 olsr_string_to_prefix(int ipversion, struct olsr_ip_prefix *dst, const char *buf);
160 
161 static INLINE const char *
sockaddr4_to_string(struct ipaddr_str * const buf,const struct sockaddr * const addr)162 sockaddr4_to_string(struct ipaddr_str *const buf, const struct sockaddr *const addr)
163 {
164   const struct sockaddr_in *addr4 = (const struct sockaddr_in *)CONST_ARM_NOWARN_ALIGN(addr);
165   return ip4_to_string(buf, addr4->sin_addr);
166 }
167 
168 static INLINE bool
is_prefix_niit_ipv6(const struct olsr_ip_prefix * p)169 is_prefix_niit_ipv6(const struct olsr_ip_prefix *p) {
170 #ifdef __ANDROID__
171   #define IN6_IS_ADDR_V4MAPPED_ANDROID_WORKAROUND(a) \
172 	((((__const uint32_t *) (a))[0] == 0)				      \
173 	 && (((__const uint32_t *) (a))[1] == 0)			      \
174 	 && (((__const uint32_t *) (a))[2] == htonl (0xffff)))
175 	bool v4mapped = IN6_IS_ADDR_V4MAPPED_ANDROID_WORKAROUND(&p->prefix.v6);
176 #else
177 	bool v4mapped = IN6_IS_ADDR_V4MAPPED(&p->prefix.v6);
178 #endif
179   return olsr_cnf->ip_version == AF_INET6 && v4mapped
180       && p->prefix_len >= ipv6_mappedv4_route.prefix_len;
181 }
182 
183 static INLINE struct olsr_ip_prefix *
prefix_mappedv4_to_v4(struct olsr_ip_prefix * v4,const struct olsr_ip_prefix * v6)184 prefix_mappedv4_to_v4(struct olsr_ip_prefix *v4, const struct olsr_ip_prefix *v6) {
185   memcpy(&v4->prefix.v4, &v6->prefix.v6.s6_addr[12], sizeof(struct in_addr));
186       v4->prefix_len = v6->prefix_len - 96;
187   return v4;
188 }
189 
190 
191 static INLINE bool
ip_is_linklocal(const union olsr_ip_addr * ip)192 ip_is_linklocal(const union olsr_ip_addr *ip) {
193   return olsr_cnf->ip_version == AF_INET6
194       && ip->v6.s6_addr[0] == 0xfe && (ip->v6.s6_addr[1] & 0xc0) == 0x80;
195 }
196 
197 static INLINE bool
ip_prefix_is_mappedv4(const struct olsr_ip_prefix * prefix)198 ip_prefix_is_mappedv4(const struct olsr_ip_prefix *prefix) {
199   return prefix->prefix_len >= ipv6_mappedv4_route.prefix_len
200       && memcmp(prefix, &ipv6_mappedv4_route, ipv6_mappedv4_route.prefix_len / 8) == 0;
201 }
202 
203 static INLINE bool
ip_prefix_is_mappedv4_inetgw(const struct olsr_ip_prefix * prefix)204 ip_prefix_is_mappedv4_inetgw(const struct olsr_ip_prefix *prefix) {
205   return prefix->prefix_len == ipv6_mappedv4_route.prefix_len
206       && memcmp(prefix, &ipv6_mappedv4_route, ipv6_mappedv4_route.prefix_len / 8) == 0;
207 }
208 
209 static INLINE bool
ip_prefix_is_v4_inetgw(const struct olsr_ip_prefix * prefix)210 ip_prefix_is_v4_inetgw(const struct olsr_ip_prefix *prefix) {
211   return prefix->prefix_len == ipv4_internet_route.prefix_len
212       && prefix->prefix.v4.s_addr == ipv4_internet_route.prefix.v4.s_addr;
213 }
214 
215 static INLINE bool
ip_prefix_is_v6_inetgw(const struct olsr_ip_prefix * prefix)216 ip_prefix_is_v6_inetgw(const struct olsr_ip_prefix *prefix) {
217   return prefix->prefix_len == ipv6_internet_route.prefix_len
218       && memcmp(prefix, &ipv6_internet_route, ipv6_internet_route.prefix_len/8) == 0;
219 }
220 
221 extern bool is_prefix_inetgw(const struct olsr_ip_prefix *prefix);
222 
223 #endif /* _IPCALC */
224 
225 /*
226  * Local Variables:
227  * c-basic-offset: 2
228  * indent-tabs-mode: nil
229  * End:
230  */
231