session: zero out ips in local endpoint lookup only if local
[vpp.git] / src / vnet / ip / ip.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/ip/ip.h>
17 #include <vnet/fib/fib_table.h>
18
19 u8
20 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4)
21 {
22   if (is_ip4)
23     return (ip46_address->ip4.as_u32 == 0);
24   else
25     return (ip46_address->as_u64[0] == 0 && ip46_address->as_u64[1] == 0);
26 }
27
28 u8
29 ip_is_local_host (ip46_address_t * ip46_address, u8 is_ip4)
30 {
31   if (is_ip4)
32     return (ip46_address->ip4.as_u8[0] == 127);
33   else
34     return (ip46_address->as_u64[0] == 0 && ip46_address->as_u64[1] == 1);
35 }
36
37 u8
38 ip4_is_local_host (ip4_address_t * ip4_address)
39 {
40   return (ip4_address->as_u8[0] == 127);
41 }
42
43 u8
44 ip6_is_local_host (ip6_address_t * ip6_address)
45 {
46   return (ip6_address->as_u64[0] == 0 && ip6_address->as_u64[1] == 1);
47 }
48
49 /**
50  * Checks that an ip is local to the requested fib
51  */
52 u8
53 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4)
54 {
55   fib_node_index_t fei;
56   fib_entry_flag_t flags;
57   fib_prefix_t prefix;
58
59   /* Check if requester is local */
60   if (is_ip4)
61     {
62       prefix.fp_len = 32;
63       prefix.fp_proto = FIB_PROTOCOL_IP4;
64     }
65   else
66     {
67       prefix.fp_len = 128;
68       prefix.fp_proto = FIB_PROTOCOL_IP6;
69     }
70
71   clib_memcpy (&prefix.fp_addr, ip46_address, sizeof (ip46_address_t));
72   fei = fib_table_lookup (0, &prefix);
73   flags = fib_entry_get_flags (fei);
74
75   return (flags & FIB_ENTRY_FLAG_LOCAL);
76 }
77
78 void
79 ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4)
80 {
81   if (is_ip4)
82     dst->ip4.as_u32 = src->ip4.as_u32;
83   else
84     clib_memcpy (&dst->ip6, &src->ip6, sizeof (ip6_address_t));
85 }
86
87 void
88 ip_set (ip46_address_t * dst, void *src, u8 is_ip4)
89 {
90   if (is_ip4)
91     dst->ip4.as_u32 = ((ip4_address_t *) src)->as_u32;
92   else
93     clib_memcpy (&dst->ip6, (ip6_address_t *) src, sizeof (ip6_address_t));
94 }
95
96 u8
97 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4)
98 {
99   ip_interface_address_t *ia = 0;
100
101   if (is_ip4)
102     {
103       ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
104       ip4_address_t *ip4;
105       /* *INDENT-OFF* */
106       foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
107       ({
108         ip4 = ip_interface_address_get_address (lm4, ia);
109         if (ip4_address_compare (ip4, &ip->ip4) == 0)
110           return 1;
111       }));
112       /* *INDENT-ON* */
113     }
114   else
115     {
116       ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
117       ip6_address_t *ip6;
118       /* *INDENT-OFF* */
119       foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
120       ({
121         ip6 = ip_interface_address_get_address (lm6, ia);
122         if (ip6_address_compare (ip6, &ip->ip6) == 0)
123           return 1;
124       }));
125       /* *INDENT-ON* */
126     }
127   return 0;
128 }
129
130 void *
131 ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
132 {
133   ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
134   ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
135   ip_interface_address_t *ia = 0;
136
137   if (is_ip4)
138     {
139       /* *INDENT-OFF* */
140       foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
141       ({
142         return ip_interface_address_get_address (lm4, ia);
143       }));
144       /* *INDENT-ON* */
145     }
146   else
147     {
148       /* *INDENT-OFF* */
149       foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
150       ({
151         ip6_address_t *rv;
152         rv = ip_interface_address_get_address (lm6, ia);
153         /* Trying to use a link-local ip6 src address is a fool's errand */
154         if (!ip6_address_is_link_local_unicast (rv))
155           return rv;
156       }));
157       /* *INDENT-ON* */
158     }
159
160   return 0;
161 }
162
163 void
164 ip4_address_normalize (ip4_address_t * ip4, u8 preflen)
165 {
166   ASSERT (preflen <= 32);
167   if (preflen == 0)
168     ip4->data_u32 = 0;
169   else
170     ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
171 }
172
173 void
174 ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
175 {
176   ASSERT (preflen <= 128);
177   if (preflen == 0)
178     {
179       ip6->as_u64[0] = 0;
180       ip6->as_u64[1] = 0;
181     }
182   else if (preflen <= 64)
183     {
184       ip6->as_u64[0] &=
185         clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
186       ip6->as_u64[1] = 0;
187     }
188   else
189     ip6->as_u64[1] &=
190       clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
191 }
192
193 void
194 ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip)
195 {
196   if (pref_len == 0)
197     ip->as_u32 = 0;
198   else
199     ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
200 }
201
202 u32
203 ip4_mask_to_preflen (ip4_address_t * mask)
204 {
205   if (mask->as_u32 == 0)
206     return 0;
207   return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
208 }
209
210 void
211 ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
212                                    ip4_address_t * res)
213 {
214   u32 not_mask;
215   not_mask = (1 << (32 - plen)) - 1;
216   res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
217 }
218
219 void
220 ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
221 {
222   if (pref_len == 0)
223     {
224       mask->as_u64[0] = 0;
225       mask->as_u64[1] = 0;
226     }
227   else if (pref_len <= 64)
228     {
229       mask->as_u64[0] =
230         clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
231       mask->as_u64[1] = 0;
232     }
233   else
234     {
235       mask->as_u64[1] =
236         clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
237     }
238 }
239
240 void
241 ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
242                                    ip6_address_t * res)
243 {
244   u64 not_mask;
245   if (plen == 0)
246     {
247       res->as_u64[0] = 0xffffffffffffffffL;
248       res->as_u64[1] = 0xffffffffffffffffL;
249     }
250   else if (plen <= 64)
251     {
252       not_mask = ((u64) 1 << (64 - plen)) - 1;
253       res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
254       res->as_u64[1] = 0xffffffffffffffffL;
255     }
256   else
257     {
258       not_mask = ((u64) 1 << (128 - plen)) - 1;
259       res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
260     }
261 }
262
263 u32
264 ip6_mask_to_preflen (ip6_address_t * mask)
265 {
266   u8 first1, first0;
267   if (mask->as_u64[0] == 0 && mask->as_u64[1] == 0)
268     return 0;
269   first1 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
270   first0 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
271
272   if (first1 != 0)
273     return 128 - first1;
274   else
275     return 64 - first0;
276 }
277
278 /*
279  * fd.io coding-style-patch-verification: ON
280  *
281  * Local Variables:
282  * eval: (c-set-style "gnu")
283  * End:
284  */