aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ip.c
blob: 4c5fa47fc8ff6cab9f643f5ee5320e28d39e062a (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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 *	BIRD Library -- IP address functions
 *
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

/**
 * DOC: IP addresses
 *
 * BIRD uses its own abstraction of IP address in order to share the same
 * code for both IPv4 and IPv6. IP addresses are represented as entities
 * of type &ip_addr which are never to be treated as numbers and instead
 * they must be manipulated using the following functions and macros.
 */

#include <stdlib.h>

#include "nest/bird.h"
#include "lib/ip.h"


int
ip6_compare(ip6_addr a, ip6_addr b)
{
  int i;
  for (i=0; i<4; i++)
    if (a.addr[i] > b.addr[i])
      return 1;
    else if (a.addr[i] < b.addr[i])
      return -1;
  return 0;
}

ip6_addr
ip6_mkmask(uint n)
{
  ip6_addr a;
  int i;

  for (i=0; i<4; i++)
  {
    if (!n)
      a.addr[i] = 0;
    else if (n >= 32)
    {
      a.addr[i] = ~0;
      n -= 32;
    }
    else
    {
      a.addr[i] = u32_mkmask(n);
      n = 0;
    }
  }

  return a;
}

uint
ip6_masklen(ip6_addr *a)
{
  int i, j, n;

  for (i=0, n=0; i<4; i++, n+=32)
    if (a->addr[i] != ~0U)
    {
      j = u32_masklen(a->addr[i]);
      if (j == 255)
	return j;
      n += j;
      while (++i < 4)
	if (a->addr[i])
	  return 255;
      break;
    }

  return n;
}

int
ip4_classify(ip4_addr ad)
{
  u32 a = _I(ad);
  u32 b = a >> 24U;

  if (b < 0xe0)
  {
    if (b == 0x00)				/* 0.0.0.0/8        This network */
      return IADDR_INVALID;

    if (b == 0x7f)				/* 127.0.0.0/8      Loopback address */
      return IADDR_HOST | SCOPE_HOST;

    if ((b == 0x0a) ||				/* 10.0.0.0/8       Private range */
	((a & 0xffff0000) == 0xc0a80000) ||	/* 192.168.0.0/16   Private range */
	((a & 0xfff00000) == 0xac100000))	/* 172.16.0.0/12    Private range */
      return IADDR_HOST | SCOPE_SITE;

    return IADDR_HOST | SCOPE_UNIVERSE;
  }

  if (b < 0xf0)					/* 224.0.0.0/4      Multicast address */
    return IADDR_MULTICAST | SCOPE_UNIVERSE;

  if (a == 0xffffffff)				/* 255.255.255.255  Broadcast address */
    return IADDR_BROADCAST | SCOPE_LINK;

  return IADDR_HOST | SCOPE_SITE;		/* 240.0.0.0/4      Reserved / private */
}

int
ip6_classify(ip6_addr *a)
{
  u32 x = a->addr[0];

  if ((x & 0xe0000000) == 0x20000000)		/* 2000::/3  Aggregatable Global Unicast Address */
    return IADDR_HOST | SCOPE_UNIVERSE;
  if ((x & 0xffc00000) == 0xfe800000)		/* fe80::/10 Link-Local Address */
    return IADDR_HOST | SCOPE_LINK;
  if ((x & 0xffc00000) == 0xfec00000)		/* fec0::/10 Site-Local Address */
    return IADDR_HOST | SCOPE_SITE;
  if ((x & 0xfe000000) == 0xfc000000)		/* fc00::/7  Unique Local Unicast Address (RFC 4193) */
    return IADDR_HOST | SCOPE_SITE;
  if ((x & 0xff000000) == 0xff000000)		/* ff00::/8  Multicast Address */
  {
    uint scope = (x >> 16) & 0x0f;
    switch (scope)
    {
    case 1:  return IADDR_MULTICAST | SCOPE_HOST;
    case 2:  return IADDR_MULTICAST | SCOPE_LINK;
    case 5:  return IADDR_MULTICAST | SCOPE_SITE;
    case 8:  return IADDR_MULTICAST | SCOPE_ORGANIZATION;
    case 14: return IADDR_MULTICAST | SCOPE_UNIVERSE;
    default: return IADDR_MULTICAST | SCOPE_UNDEFINED;
    }
  }

  if (!x && !a->addr[1])
  {
    u32 a2 = a->addr[2];
    u32 a3 = a->addr[3];

    if (a2 == 0 && a3 == 1)
      return IADDR_HOST | SCOPE_HOST;		/* Loopback address */
    if (a2 == 0)
      return ip4_classify(_MI4(a3));		/* IPv4 compatible addresses */
    if (a2 == 0xffff)
      return ip4_classify(_MI4(a3));		/* IPv4 mapped addresses */

    return IADDR_INVALID;
  }

  return IADDR_HOST | SCOPE_UNDEFINED;
}



/*
 *  Conversion of IPv6 address to presentation format and vice versa.
 *  Heavily inspired by routines written by Paul Vixie for the BIND project
 *  and of course by RFC 2373.
 */


char *
ip4_ntop(ip4_addr a, char *b)
{
  u32 x = _I(a);
  return b + bsprintf(b, "%d.%d.%d.%d", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
}


char *
ip6_ntop(ip6_addr a, char *b)
{
  u16 words[8];
  int bestpos, bestlen, curpos, curlen, i;

  /* First of all, preprocess the address and find the longest run of zeros */
  bestlen = bestpos = curpos = curlen = 0;
  for (i=0; i<8; i++)
  {
    u32 x = a.addr[i/2];
    words[i] = ((i%2) ? x : (x >> 16)) & 0xffff;
    if (words[i])
      curlen = 0;
    else
    {
      if (!curlen)
	curpos = i;
      curlen++;
      if (curlen > bestlen)
      {
	bestpos = curpos;
	bestlen = curlen;
      }
    }
  }

  if (bestlen < 2)
    bestpos = -1;

  /* Is it an encapsulated IPv4 address? */
  if (!bestpos && ((bestlen == 5 && a.addr[2] == 0xffff) || (bestlen == 6)))
  {
    u32 x = a.addr[3];
    b += bsprintf(b, "::%s%d.%d.%d.%d",
		  a.addr[2] ? "ffff:" : "",
		  (x >> 24) & 0xff,
		  (x >> 16) & 0xff,
		  (x >> 8) & 0xff,
		  x & 0xff);
    return b;
  }

  /* Normal IPv6 formatting, compress the largest sequence of zeros */
  for (i=0; i<8; i++)
  {
    if (i == bestpos)
    {
      i += bestlen - 1;
      *b++ = ':';
      if (i == 7)
	*b++ = ':';
    }
    else
    {
      if (i)
	*b++ = ':';
      b += bsprintf(b, "%x", words[i]);
    }
  }
  *b = 0;
  return b;
}

int
ip4_pton(const char *a, ip4_addr *o)
{
  int i;
  unsigned long int l;
  u32 ia = 0;

  i=4;
  while (i--)
  {
    char *d, *c = strchr(a, '.');
    if (!c != !i)
      return 0;
    l = bstrtoul10(a, &d);
    if (((d != c) && *d) || (l > 255))
      return 0;
    ia = (ia << 8) | l;
    if (c)
      c++;
    a = c;
  }
  *o = ip4_from_u32(ia);
  return 1;
}

int
ip6_pton(const char *a, ip6_addr *o)
{
  u16 words[8];
  int i, j, k, l, hfil;
  const char *start;

  if (!a[0])				/* Empty string check */
    return 0;

  if (a[0] == ':')			/* Leading :: */
  {
    if (a[1] != ':')
      return 0;
    a++;
  }

  hfil = -1;
  i = 0;
  while (*a)
  {
    if (*a == ':')			/* :: */
    {
      if (hfil >= 0)
	return 0;

      hfil = i;
      a++;
      continue;
    }

    j = 0;
    l = 0;
    start = a;
    for (;;)
    {
      if (*a >= '0' && *a <= '9')
	k = *a++ - '0';
      else if (*a >= 'A' && *a <= 'F')
	k = *a++ - 'A' + 10;
      else if (*a >= 'a' && *a <= 'f')
	k = *a++ - 'a' + 10;
      else
	break;

      j = (j << 4) + k;
      if (j >= 0x10000 || ++l > 4)
	return 0;
    }

    if (*a == ':' && a[1])
      a++;
    else if (*a == '.' && (i == 6 || (i < 6 && hfil >= 0)))
    {				/* Embedded IPv4 address */
      ip4_addr x;
      if (!ip4_pton(start, &x))
	return 0;
      words[i++] = _I(x) >> 16;
      words[i++] = _I(x);
      break;
    }
    else if (*a)
      return 0;

    if (i >= 8)
      return 0;

    words[i++] = j;
  }

  /* Replace :: with an appropriate number of zeros */
  if (hfil >= 0)
  {
    j = 8 - i;
    for (i=7; i-j >= hfil; i--)
      words[i] = words[i-j];
    for (; i>=hfil; i--)
      words[i] = 0;
  }
  else if (i != 8)	/* Incomplete address */
    return 0;

  /* Convert the address to ip6_addr format */
  for (i=0; i<4; i++)
    o->addr[i] = (words[2*i] << 16) | words[2*i+1];

  return 1;
}


/**
 * ip_scope_text - get textual representation of address scope
 * @scope: scope (%SCOPE_xxx)
 *
 * Returns a pointer to a textual name of the scope given.
 */
char *
ip_scope_text(uint scope)
{
  static char *scope_table[] = { "host", "link", "site", "org", "univ", "undef" };

  if (scope > SCOPE_UNDEFINED)
    return "?";
  else
    return scope_table[scope];
}

ip4_addr
ip4_class_mask(ip4_addr ad)
{
  u32 m, a = _I(ad);

  if (a == 0x00000000)
    m = 0x00000000;
  else if (a < 0x80000000)
    m = 0xff000000;
  else if (a < 0xc0000000)
    m = 0xffff0000;
  else
    m = 0xffffff00;
  if (a & ~m)
    m = 0xffffffff;

  return _MI4(m);
}

#if 0
/**
 * ipa_equal - compare two IP addresses for equality
 * @x: IP address
 * @y: IP address
 *
 * ipa_equal() returns 1 if @x and @y represent the same IP address, else 0.
 */
int ipa_equal(ip_addr x, ip_addr y) { DUMMY }

/**
 * ipa_nonzero - test if an IP address is defined
 * @x: IP address
 *
 * ipa_nonzero returns 1 if @x is a defined IP address (not all bits are zero),
 * else 0.
 *
 * The undefined all-zero address is reachable as a |IPA_NONE| macro.
 */
int ipa_nonzero(ip_addr x) { DUMMY }

/**
 * ipa_and - compute bitwise and of two IP addresses
 * @x: IP address
 * @y: IP address
 *
 * This function returns a bitwise and of @x and @y. It's primarily
 * used for network masking.
 */
ip_addr ipa_and(ip_addr x, ip_addr y) { DUMMY }

/**
 * ipa_or - compute bitwise or of two IP addresses
 * @x: IP address
 * @y: IP address
 *
 * This function returns a bitwise or of @x and @y.
 */
ip_addr ipa_or(ip_addr x, ip_addr y) { DUMMY }

/**
 * ipa_xor - compute bitwise xor of two IP addresses
 * @x: IP address
 * @y: IP address
 *
 * This function returns a bitwise xor of @x and @y.
 */
ip_addr ipa_xor(ip_addr x, ip_addr y) { DUMMY }

/**
 * ipa_not - compute bitwise negation of two IP addresses
 * @x: IP address
 *
 * This function returns a bitwise negation of @x.
 */
ip_addr ipa_not(ip_addr x) { DUMMY }

/**
 * ipa_mkmask - create a netmask
 * @x: prefix length
 *
 * This function returns an &ip_addr corresponding of a netmask
 * of an address prefix of size @x.
 */
ip_addr ipa_mkmask(int x) { DUMMY }

/**
 * ipa_masklen - calculate netmask length
 * @x: IP address
 *
 * This function checks whether @x represents a valid netmask and
 * returns the size of the associate network prefix or -1 for invalid
 * mask.
 */
int ipa_masklen(ip_addr x) { DUMMY }

/**
 * ipa_hash - hash IP addresses
 * @x: IP address
 *
 * ipa_hash() returns a 16-bit hash value of the IP address @x.
 */
int ipa_hash(ip_addr x) { DUMMY }

/**
 * ipa_hton - convert IP address to network order
 * @x: IP address
 *
 * Converts the IP address @x to the network byte order.
 *
 * Beware, this is a macro and it alters the argument!
 */
void ipa_hton(ip_addr x) { DUMMY }

/**
 * ipa_ntoh - convert IP address to host order
 * @x: IP address
 *
 * Converts the IP address @x from the network byte order.
 *
 * Beware, this is a macro and it alters the argument!
 */
void ipa_ntoh(ip_addr x) { DUMMY }

/**
 * ipa_classify - classify an IP address
 * @x: IP address
 *
 * ipa_classify() returns an address class of @x, that is a bitwise or
 * of address type (%IADDR_INVALID, %IADDR_HOST, %IADDR_BROADCAST, %IADDR_MULTICAST)
 * with address scope (%SCOPE_HOST to %SCOPE_UNIVERSE) or -1 (%IADDR_INVALID)
 * for an invalid address.
 */
int ipa_classify(ip_addr x) { DUMMY }

/**
 * ip4_class_mask - guess netmask according to address class
 * @x: IPv4 address
 *
 * This function (available in IPv4 version only) returns a
 * network mask according to the address class of @x. Although
 * classful addressing is nowadays obsolete, there still live
 * routing protocols transferring no prefix lengths nor netmasks
 * and this function could be useful to them.
 */
ip4_addr ip4_class_mask(ip4_addr x) { DUMMY }

/**
 * ipa_from_u32 - convert IPv4 address to an integer
 * @x: IP address
 *
 * This function takes an IPv4 address and returns its numeric
 * representation.
 */
u32 ipa_from_u32(ip_addr x) { DUMMY }

/**
 * ipa_to_u32 - convert integer to IPv4 address
 * @x: a 32-bit integer
 *
 * ipa_to_u32() takes a numeric representation of an IPv4 address
 * and converts it to the corresponding &ip_addr.
 */
ip_addr ipa_to_u32(u32 x) { DUMMY }

/**
 * ipa_compare - compare two IP addresses for order
 * @x: IP address
 * @y: IP address
 *
 * The ipa_compare() function takes two IP addresses and returns
 * -1 if @x is less than @y in canonical ordering (lexicographical
 * order of the bit strings), 1 if @x is greater than @y and 0
 * if they are the same.
 */
int ipa_compare(ip_addr x, ip_addr y) { DUMMY }

/**
 * ipa_build6 - build an IPv6 address from parts
 * @a1: part #1
 * @a2: part #2
 * @a3: part #3
 * @a4: part #4
 *
 * ipa_build() takes @a1 to @a4 and assembles them to a single IPv6
 * address. It's used for example when a protocol wants to bind its
 * socket to a hard-wired multicast address.
 */
ip_addr ipa_build6(u32 a1, u32 a2, u32 a3, u32 a4) { DUMMY }

/**
 * ip_ntop - convert IP address to textual representation
 * @a: IP address
 * @buf: buffer of size at least %STD_ADDRESS_P_LENGTH
 *
 * This function takes an IP address and creates its textual
 * representation for presenting to the user.
 */
char *ip_ntop(ip_addr a, char *buf) { DUMMY }

/**
 * ip_ntox - convert IP address to hexadecimal representation
 * @a: IP address
 * @buf: buffer of size at least %STD_ADDRESS_P_LENGTH
 *
 * This function takes an IP address and creates its hexadecimal
 * textual representation. Primary use: debugging dumps.
 */
char *ip_ntox(ip_addr a, char *buf) { DUMMY }

/**
 * ip_pton - parse textual representation of IP address
 * @a: textual representation
 * @o: where to put the resulting address
 *
 * This function parses a textual IP address representation and
 * stores the decoded address to a variable pointed to by @o.
 * Returns 0 if a parse error has occurred, else 0.
 */
int ip_pton(char *a, ip_addr *o) { DUMMY }

#endif