FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
ip.c
Go to the documentation of this file.
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 &&
35  clib_net_to_host_u64 (ip46_address->as_u64[1]) == 1);
36 }
37 
38 u8
40 {
41  return (ip4_address->as_u8[0] == 127);
42 }
43 
44 u8
46 {
47  return (ip6_address->as_u64[0] == 0 &&
48  clib_net_to_host_u64 (ip6_address->as_u64[1]) == 1);
49 }
50 
51 /**
52  * Checks that an ip is local to the requested fib
53  */
54 u8
55 ip_is_local (u32 fib_index, ip46_address_t * ip46_address, u8 is_ip4)
56 {
57  fib_node_index_t fei;
60 
61  /* Check if requester is local */
62  if (is_ip4)
63  {
64  prefix.fp_len = 32;
65  prefix.fp_proto = FIB_PROTOCOL_IP4;
66  }
67  else
68  {
69  prefix.fp_len = 128;
70  prefix.fp_proto = FIB_PROTOCOL_IP6;
71  }
72 
73  clib_memcpy_fast (&prefix.fp_addr, ip46_address, sizeof (ip46_address_t));
74  fei = fib_table_lookup (fib_index, &prefix);
75  flags = fib_entry_get_flags (fei);
76 
77  return (flags & FIB_ENTRY_FLAG_LOCAL);
78 }
79 
80 void
81 ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4)
82 {
83  if (is_ip4)
84  {
86  dst->ip4.as_u32 = src->ip4.as_u32;
87  }
88  else
89  clib_memcpy_fast (&dst->ip6, &src->ip6, sizeof (ip6_address_t));
90 }
91 
92 void
93 ip_set (ip46_address_t * dst, void *src, u8 is_ip4)
94 {
95  if (is_ip4)
96  {
98  dst->ip4.as_u32 = ((ip4_address_t *) src)->as_u32;
99  }
100  else
101  clib_memcpy_fast (&dst->ip6, (ip6_address_t *) src,
102  sizeof (ip6_address_t));
103 }
104 
105 u8
106 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4)
107 {
108  ip_interface_address_t *ia = 0;
109 
110  if (is_ip4)
111  {
113  ip4_address_t *ip4;
114  /* *INDENT-OFF* */
115  foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
116  ({
117  ip4 = ip_interface_address_get_address (lm4, ia);
118  if (ip4_address_compare (ip4, &ip->ip4) == 0)
119  return 1;
120  }));
121  /* *INDENT-ON* */
122  }
123  else
124  {
126  ip6_address_t *ip6;
127  /* *INDENT-OFF* */
128  foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
129  ({
130  ip6 = ip_interface_address_get_address (lm6, ia);
131  if (ip6_address_compare (ip6, &ip->ip6) == 0)
132  return 1;
133  }));
134  /* *INDENT-ON* */
135  }
136  return 0;
137 }
138 
139 void *
141 {
144  ip_interface_address_t *ia = 0;
145 
146  if (is_ip4)
147  {
148  /* *INDENT-OFF* */
149  foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
150  ({
151  return ip_interface_address_get_address (lm4, ia);
152  }));
153  /* *INDENT-ON* */
154  }
155  else
156  {
157  /* *INDENT-OFF* */
158  foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
159  ({
160  ip6_address_t *rv;
161  rv = ip_interface_address_get_address (lm6, ia);
162  /* Trying to use a link-local ip6 src address is a fool's errand */
164  return rv;
165  }));
166  /* *INDENT-ON* */
167  }
168 
169  return 0;
170 }
171 
172 void
174 {
175  ASSERT (preflen <= 32);
176  if (preflen == 0)
177  ip4->data_u32 = 0;
178  else
179  ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
180 }
181 
182 void
184 {
185  ASSERT (preflen <= 128);
186  if (preflen == 0)
187  {
188  ip6->as_u64[0] = 0;
189  ip6->as_u64[1] = 0;
190  }
191  else if (preflen <= 64)
192  {
193  ip6->as_u64[0] &=
194  clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
195  ip6->as_u64[1] = 0;
196  }
197  else
198  ip6->as_u64[1] &=
199  clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
200 }
201 
202 void
204 {
205  if (pref_len == 0)
206  ip->as_u32 = 0;
207  else
208  ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
209 }
210 
211 u32
213 {
214  if (mask->as_u32 == 0)
215  return 0;
216  return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
217 }
218 
219 void
221  ip4_address_t * res)
222 {
223  u32 not_mask;
224  not_mask = (1 << (32 - plen)) - 1;
225  res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
226 }
227 
228 void
230 {
231  if (pref_len == 0)
232  {
233  mask->as_u64[0] = 0;
234  mask->as_u64[1] = 0;
235  }
236  else if (pref_len <= 64)
237  {
238  mask->as_u64[0] =
239  clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
240  mask->as_u64[1] = 0;
241  }
242  else
243  {
244  mask->as_u64[0] = 0xffffffffffffffffL;
245  mask->as_u64[1] =
246  clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
247  }
248 }
249 
250 void
252  ip6_address_t * res)
253 {
254  u64 not_mask;
255  if (plen == 0)
256  {
257  res->as_u64[0] = 0xffffffffffffffffL;
258  res->as_u64[1] = 0xffffffffffffffffL;
259  }
260  else if (plen <= 64)
261  {
262  not_mask = ((u64) 1 << (64 - plen)) - 1;
263  res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
264  res->as_u64[1] = 0xffffffffffffffffL;
265  }
266  else
267  {
268  not_mask = ((u64) 1 << (128 - plen)) - 1;
269  res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
270  }
271 }
272 
273 u32
275 {
276  u8 first1, first0;
277  if (mask->as_u64[0] == 0 && mask->as_u64[1] == 0)
278  return 0;
279  first1 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
280  first0 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
281 
282  if (first1 != 0)
283  return 128 - first1;
284  else
285  return 64 - first0;
286 }
287 
288 u8 *
289 format_ip_address_family (u8 * s, va_list * args)
290 {
291  ip_address_family_t af = va_arg (*args, ip_address_family_t);
292 
293  switch (af)
294  {
295  case AF_IP4:
296  return (format (s, "ip4"));
297  case AF_IP6:
298  return (format (s, "ip6"));
299  }
300 
301  return (format (s, "unknown"));
302 }
303 
304 u8 *
305 format_ip_dscp (u8 * s, va_list * va)
306 {
307  ip_dscp_t dscp = va_arg (*va, u32); // int promotion of u8
308 
309  switch (dscp)
310  {
311 #define _(n,v) \
312  case IP_DSCP_##v: \
313  return (format (s, "%s", #v));
315 #undef _
316  }
317 
318  return (format (s, "unknown"));
319 }
320 
321 /*
322  * fd.io coding-style-patch-verification: ON
323  *
324  * Local Variables:
325  * eval: (c-set-style "gnu")
326  * End:
327  */
#define foreach_ip_interface_address(lm, a, sw_if_index, loop, body)
Definition: lookup.h:213
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
void ip6_preflen_to_mask(u8 pref_len, ip6_address_t *mask)
Definition: ip.c:229
void ip4_preflen_to_mask(u8 pref_len, ip4_address_t *ip)
Definition: ip.c:203
u32 flags
Definition: vhost_user.h:141
void ip_copy(ip46_address_t *dst, ip46_address_t *src, u8 is_ip4)
Definition: ip.c:81
static uword log2_first_set(uword x)
Definition: clib.h:266
u8 ip_is_local(u32 fib_index, ip46_address_t *ip46_address, u8 is_ip4)
Checks that an ip is local to the requested fib.
Definition: ip.c:55
u64 as_u64[2]
Definition: ip6_packet.h:51
unsigned long u64
Definition: types.h:89
void ip_set(ip46_address_t *dst, void *src, u8 is_ip4)
Definition: ip.c:93
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
u8 ip_interface_has_address(u32 sw_if_index, ip46_address_t *ip, u8 is_ip4)
Definition: ip.c:106
vl_api_address_t src
Definition: gre.api:51
ip_lookup_main_t lookup_main
Definition: ip4.h:107
void * ip_interface_get_first_ip(u32 sw_if_index, u8 is_ip4)
Definition: ip.c:140
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vl_api_mprefix_t prefix
Definition: ip.api:456
enum ip_address_family_t_ ip_address_family_t
unsigned char u8
Definition: types.h:56
void ip6_prefix_max_address_host_order(ip6_address_t *ip, u8 plen, ip6_address_t *res)
Definition: ip.c:251
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
void ip4_address_normalize(ip4_address_t *ip4, u8 preflen)
Definition: ip.c:173
#define ip46_address_mask_ip4(ip46)
Definition: ip6_packet.h:89
Aggregate type for a prefix.
Definition: fib_types.h:203
unsigned int u32
Definition: types.h:88
u16 fp_len
The mask length.
Definition: fib_types.h:207
fib_node_index_t fib_table_lookup(u32 fib_index, const fib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
Definition: fib_table.c:66
u8 * format_ip_dscp(u8 *s, va_list *va)
Definition: ip.c:305
Definition: fib_entry.h:286
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:226
void ip6_address_normalize(ip6_address_t *ip6, u8 preflen)
Definition: ip.c:183
int ip6_address_compare(ip6_address_t *a1, ip6_address_t *a2)
Definition: ip46_cli.c:60
u8 * format_ip_address_family(u8 *s, va_list *args)
Definition: ip.c:289
vl_api_address_t dst
Definition: gre.api:52
u32 ip6_mask_to_preflen(ip6_address_t *mask)
Definition: ip.c:274
int ip4_address_compare(ip4_address_t *a1, ip4_address_t *a2)
Definition: ip46_cli.c:53
u8 ip6_address[16]
Definition: ip_types.api:18
u8 ip4_is_local_host(ip4_address_t *ip4_address)
Definition: ip.c:39
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:30
u32 ip4_mask_to_preflen(ip4_address_t *mask)
Definition: ip.c:212
u8 ip6_is_local_host(ip6_address_t *ip6_address)
Definition: ip.c:45
enum fib_entry_flag_t_ fib_entry_flag_t
#define ASSERT(truth)
ip6_main_t ip6_main
Definition: ip6_forward.c:2805
ip_lookup_main_t lookup_main
Definition: ip6.h:179
enum ip_dscp_t_ ip_dscp_t
static uword ip6_address_is_link_local_unicast(const ip6_address_t *a)
Definition: ip6_packet.h:326
u8 ip_is_zero(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:20
u8 ip4_address[4]
Definition: ip_types.api:17
vl_api_address_t ip
Definition: l2.api:489
void ip4_prefix_max_address_host_order(ip4_address_t *ip, u8 plen, ip4_address_t *res)
Definition: ip.c:220
u8 ip_is_local_host(ip46_address_t *ip46_address, u8 is_ip4)
Definition: ip.c:29
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1076
static void * ip_interface_address_get_address(ip_lookup_main_t *lm, ip_interface_address_t *a)
Definition: lookup.h:199
fib_entry_flag_t fib_entry_get_flags(fib_node_index_t fib_entry_index)
Definition: fib_entry.c:302