summaryrefslogtreecommitdiffstats
path: root/src/vat/ip_types.c
blob: 248205287a49079a590936099d56106d44bb414a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/ip/ip_types.h>
#include <vnet/ip/format.h>
#include <vnet/ip/ip.h>

u8 *
format_ip_address (u8 * s, va_list * args)
{
  ip_address_t *a = va_arg (*args, ip_address_t *);
  u8 ver = ip_addr_version (a);
  if (ver == AF_IP4)
    {
      return format (s, "%U", format_ip4_address, &ip_addr_v4 (a));
    }
  else if (ver == AF_IP6)
    {
      return format (s, "%U", format_ip6_address, &ip_addr_v6 (a));
    }
  else
    {
      clib_warning ("Can't format IP version %d!", ver);
      return 0;
    }
}

uword
unformat_ip_address (unformat_input_t * input, va_list * args)
{
  ip_address_t *a = va_arg (*args, ip_address_t *);
  ip_address_t tmp, *p_tmp = &tmp;

  clib_memset (p_tmp, 0, sizeof (*p_tmp));
  if (unformat (input, "%U", unformat_ip4_address, &ip_addr_v4 (p_tmp)))
    ip_addr_version (p_tmp) = AF_IP4;
  else if (unformat_user (input, unformat_ip6_address, &ip_addr_v6 (p_tmp)))
    ip_addr_version (p_tmp) = AF_IP6;
  else
    return 0;
  *a = *p_tmp;
  return 1;
}

u8 *
format_ip_prefix (u8 * s, va_list * args)
{
  ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
  return format (s, "%U/%d", format_ip_address, &ip_prefix_addr (a),
		 ip_prefix_len (a));
}

uword
unformat_ip_prefix (unformat_input_t * input, va_list * args)
{
  ip_prefix_t *a = va_arg (*args, ip_prefix_t *);
  /* %d writes more than a u8 */
  int plen;
  if (unformat (input, "%U/%d", unformat_ip_address, &ip_prefix_addr (a),
		&plen))
    {
      ip_prefix_len (a) = plen;
      if ((ip_prefix_version (a) == AF_IP4 && 32 < ip_prefix_len (a)) ||
	  (ip_prefix_version (a) == AF_IP6 && 128 < ip_prefix_len (a)))
	{
	  clib_warning ("Prefix length to big: %d!", ip_prefix_len (a));
	  return 0;
	}
      ip_prefix_normalize (a);
    }
  else
    return 0;
  return 1;
}

u16
ip_address_size (const ip_address_t * a)
{
  switch (ip_addr_version (a))
    {
    case AF_IP4:
      return sizeof (ip4_address_t);
      break;
    case AF_IP6:
      return sizeof (ip6_address_t);
      break;
    }
  return 0;
}

bool
ip_address_is_zero (const ip_address_t * ip)
{
  switch (ip_addr_version (ip))
    {
    case AF_IP4:
      return (ip_addr_v4 (ip).as_u32 == 0);
    case AF_IP6:
      return (ip_addr_v6 (ip).as_u64[0] == 0 &&
	      ip_addr_v6 (ip).as_u64[1] == 0);
      break;
    }
  return false;
}

int
ip_address_cmp (const ip_address_t * ip1, const ip_address_t * ip2)
{
  int res = 0;
  if (ip_addr_version (ip1) != ip_addr_version (ip2))
    return -1;
  res = ip46_address_cmp (&ip_addr_46 (ip1), &ip_addr_46 (ip2));

  if (res < 0)
    res = 2;
  else if (res > 0)
    res = 1;

  return res;
}

void
ip_address_copy (ip_address_t * dst, const ip_address_t * src)
{
  if (AF_IP4 == ip_addr_version (src))
    {
      /* don't copy any garbage from the union */
      clib_memset (dst, 0, sizeof (*dst));
      ip_addr_v4 (dst) = ip_addr_v4 (src);
      dst->version = AF_IP4;
    }
  else
    {
      clib_memcpy (dst, src, sizeof (ip_address_t));
    }
}

u8 *
ip_addr_bytes (ip_address_t * ip)
{
  switch (ip->version)
    {
    case AF_IP4:
      return (u8 *) & ip_addr_v4 (ip);
    case AF_IP6:
      return (u8 *) & ip_addr_v6 (ip);
      break;
    }
  ASSERT (0);
  return (NULL);
}

void
ip_address_copy_addr (void *dst, const ip_address_t * src)
{
  switch (src->version)
    {
    case AF_IP4:
      clib_memcpy (dst, &ip_addr_v4 (src), ip_address_size (src));
      break;
    case AF_IP6:
      clib_memcpy (dst, &ip_addr_v6 (src), ip_address_size (src));
      break;
    }
}

u16
ip_version_to_size (ip_address_family_t af)
{
  switch (af)
    {
    case AF_IP4:
      return sizeof (ip4_address_t);
      break;
    case AF_IP6:
      return sizeof (ip6_address_t);
      break;
    }
  return 0;
}

vnet_link_t
ip_address_family_to_link_type (ip_address_family_t af)
{
  switch (af)
    {
    case AF_IP4:
      return (VNET_LINK_IP4);
    case AF_IP6:
      return (VNET_LINK_IP6);
    }
  ASSERT (0);
  return (VNET_LINK_IP4);
}

void
ip_address_set (ip_address_t *dst, const void *src,
		ip_address_family_t version)
{
  ip_addr_version (dst) = version;

  switch (version)
    {
    case AF_IP4:
      ip_addr_v4 (dst) = *(ip4_address_t *) src;
      break;
    case AF_IP6:
      ip_addr_v6 (dst) = *(ip6_address_t *) src;
      break;
    }
}

fib_protocol_t
ip_address_family_to_fib_proto (ip_address_family_t af)
{
  switch (af)
    {
    case AF_IP4:
      return (FIB_PROTOCOL_IP4);
    case AF_IP6:
      return (FIB_PROTOCOL_IP6);
    }
  ASSERT (0);
  return (FIB_PROTOCOL_IP4);
}

ip_address_family_t
ip_address_family_from_fib_proto (fib_protocol_t fp)
{
  switch (fp)
    {
    case FIB_PROTOCOL_IP4:
      return (AF_IP4);
    case FIB_PROTOCOL_IP6:
      return (AF_IP6);
    case FIB_PROTOCOL_MPLS:
      ASSERT (0);
    }
  return (AF_IP4);
}

fib_protocol_t
ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
{
  *a = ip_addr_46 (addr);
  return (ip_address_family_to_fib_proto (ip_addr_version (addr)));
}

void
ip_address_from_46 (const ip46_address_t * nh,
		    fib_protocol_t fproto, ip_address_t * ip)
{
  ip_addr_46 (ip) = *nh;
  ip_addr_version (ip) = ip_address_family_from_fib_proto (fproto);
}

static void
ip_prefix_normalize_ip4 (ip4_address_t * ip4, u8 preflen)
{
  u32 mask = ~0;

  ASSERT (ip4);

  if (32 <= preflen)
    {
      return;
    }

  mask = pow2_mask (preflen) << (32 - preflen);
  mask = clib_host_to_net_u32 (mask);
  ip4->data_u32 &= mask;
}

static void
ip_prefix_normalize_ip6 (ip6_address_t * ip6, u8 preflen)
{
  u8 mask_6[16];
  u32 *m;
  u8 j, i0, i1;

  ASSERT (ip6);

  clib_memset (mask_6, 0, sizeof (mask_6));

  if (128 <= preflen)
    {
      return;
    }

  i1 = preflen % 32;
  i0 = preflen / 32;
  m = (u32 *) & mask_6[0];

  for (j = 0; j < i0; j++)
    {
      m[j] = ~0;
    }

  if (i1)
    {
      m[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
    }

  for (j = 0; j < sizeof (mask_6); j++)
    {
      ip6->as_u8[j] &= mask_6[j];
    }
}

void
ip_prefix_normalize (ip_prefix_t * a)
{
  u8 preflen = ip_prefix_len (a);

  switch (ip_prefix_version (a))
    {
    case AF_IP4:
      ip_prefix_normalize_ip4 (&ip_prefix_v4 (a), preflen);
      break;

    case AF_IP6:
      ip_prefix_normalize_ip6 (&ip_prefix_v6 (a), preflen);
      break;

    default:
      ASSERT (0);
    }
}

void
ip_prefix_copy (void *dst, void *src)
{
  clib_memcpy (dst, src, sizeof (ip_prefix_t));
}

int
ip_prefix_cmp (const ip_prefix_t *ipp1, const ip_prefix_t *ipp2)
{
  ip_prefix_t p1 = *ipp1, p2 = *ipp2;
  int cmp = 0;

  ip_prefix_normalize (&p1);
  ip_prefix_normalize (&p2);

  cmp = ip_address_cmp (&ip_prefix_addr (&p1), &ip_prefix_addr (&p2));
  if (cmp == 0)
    {
      if (ip_prefix_len (&p1) < ip_prefix_len (&p2))
	{
	  cmp = 1;
	}
      else
	{
	  if (ip_prefix_len (&p1) > ip_prefix_len (&p2))
	    cmp = 2;
	}
    }
  return cmp;
}

static bool
ip4_prefix_validate (const ip_prefix_t * ip)
{
  ip4_address_t ip4_addr, ip4_mask;

  if (ip_prefix_len (ip) > 32)
    return (false);

  ip4_addr = ip_prefix_v4 (ip);
  ip4_preflen_to_mask (ip_prefix_len (ip), &ip4_mask);

  return ((ip4_addr.as_u32 & ip4_mask.as_u32) == ip4_addr.as_u32);
}

static bool
ip6_prefix_validate (const ip_prefix_t * ip)
{
  ip6_address_t ip6_addr, ip6_mask;

  if (ip_prefix_len (ip) > 128)
    return (false);

  ip6_addr = ip_prefix_v6 (ip);
  ip6_preflen_to_mask (ip_prefix_len (ip), &ip6_mask);

  return (((ip6_addr.as_u64[0] & ip6_mask.as_u64[0]) == ip6_addr.as_u64[0]) &&
	  ((ip6_addr.as_u64[1] & ip6_mask.as_u64[1]) == ip6_addr.as_u64[1]));
}

bool
ip_prefix_validate (const ip_prefix_t * ip)
{
  switch (ip_prefix_version (ip))
    {
    case AF_IP4:
      return (ip4_prefix_validate (ip));
    case AF_IP6:
      return (ip6_prefix_validate (ip));
    }
  ASSERT (0);
  return (false);
}

void
ip4_address_normalize (ip4_address_t * ip4, u8 preflen)
{
  ASSERT (preflen <= 32);
  if (preflen == 0)
    ip4->data_u32 = 0;
  else
    ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
}

void
ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
{
  ASSERT (preflen <= 128);
  if (preflen == 0)
    {
      ip6->as_u64[0] = 0;
      ip6->as_u64[1] = 0;
    }
  else if (preflen <= 64)
    {
      ip6->as_u64[0] &=
	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
      ip6->as_u64[1] = 0;
    }
  else
    ip6->as_u64[1] &=
      clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
}

void
ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip)
{
  if (pref_len == 0)
    ip->as_u32 = 0;
  else
    ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
}

u32
ip4_mask_to_preflen (ip4_address_t * mask)
{
  if (mask->as_u32 == 0)
    return 0;
  return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
}

void
ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
				   ip4_address_t * res)
{
  u32 not_mask;
  not_mask = (1 << (32 - plen)) - 1;
  res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
}

void
ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
{
  if (pref_len == 0)
    {
      mask->as_u64[0] = 0;
      mask->as_u64[1] = 0;
    }
  else if (pref_len <= 64)
    {
      mask->as_u64[0] =
	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
      mask->as_u64[1] = 0;
    }
  else
    {
      mask->as_u64[0] = 0xffffffffffffffffL;
      mask->as_u64[1] =
	clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
    }
}

void
ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
				   ip6_address_t * res)
{
  u64 not_mask;
  if (plen == 0)
    {
      res->as_u64[0] = 0xffffffffffffffffL;
      res->as_u64[1] = 0xffffffffffffffffL;
    }
  else if (plen <= 64)
    {
      not_mask = ((u64) 1 << (64 - plen)) - 1;
      res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
      res->as_u64[1] = 0xffffffffffffffffL;
    }
  else
    {
      not_mask = ((u64) 1 << (128 - plen)) - 1;
      res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
    }
}

u32
ip6_mask_to_preflen (ip6_address_t * mask)
{
  if (mask->as_u64[1] != 0)
    return 128 - log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
  if (mask->as_u64[0] != 0)
    return 64 - log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
  return 0;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */