]> git.proxmox.com Git - mirror_frr.git/blob - lib/prefix.c
frr: Remove HAVE_IPV6 from code base
[mirror_frr.git] / lib / prefix.c
1 /*
2 * Prefix related functions.
3 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "prefix.h"
26 #include "vty.h"
27 #include "sockunion.h"
28 #include "memory.h"
29 #include "log.h"
30
31 DEFINE_MTYPE_STATIC(LIB, PREFIX, "Prefix")
32
33 /* Maskbit. */
34 static const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
35 0xf8, 0xfc, 0xfe, 0xff};
36
37 static const struct in6_addr maskbytes6[] =
38 {
39 /* /0 */ { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
40 /* /1 */ { { { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
41 /* /2 */ { { { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
42 /* /3 */ { { { 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
43 /* /4 */ { { { 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
44 /* /5 */ { { { 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
45 /* /6 */ { { { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
46 /* /7 */ { { { 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
47 /* /8 */ { { { 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
48 /* /9 */ { { { 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
49 /* /10 */ { { { 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
50 /* /11 */ { { { 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
51 /* /12 */ { { { 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
52 /* /13 */ { { { 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
53 /* /14 */ { { { 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
54 /* /15 */ { { { 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
55 /* /16 */ { { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
56 /* /17 */ { { { 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
57 /* /18 */ { { { 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
58 /* /19 */ { { { 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
59 /* /20 */ { { { 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
60 /* /21 */ { { { 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
61 /* /22 */ { { { 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
62 /* /23 */ { { { 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
63 /* /24 */ { { { 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
64 /* /25 */ { { { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
65 /* /26 */ { { { 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
66 /* /27 */ { { { 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
67 /* /28 */ { { { 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
68 /* /29 */ { { { 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
69 /* /30 */ { { { 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
70 /* /31 */ { { { 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
71 /* /32 */ { { { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
72 /* /33 */ { { { 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
73 /* /34 */ { { { 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
74 /* /35 */ { { { 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
75 /* /36 */ { { { 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
76 /* /37 */ { { { 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
77 /* /38 */ { { { 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
78 /* /39 */ { { { 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
79 /* /40 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
80 /* /41 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
81 /* /42 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
82 /* /43 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
83 /* /44 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
84 /* /45 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
85 /* /46 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
86 /* /47 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
87 /* /48 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
88 /* /49 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
89 /* /50 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
90 /* /51 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
91 /* /52 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
92 /* /53 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
93 /* /54 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
94 /* /55 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
95 /* /56 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
96 /* /57 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
97 /* /58 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
98 /* /59 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
99 /* /60 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
100 /* /61 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
101 /* /62 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
102 /* /63 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
103 /* /64 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
104 /* /65 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
105 /* /66 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
106 /* /67 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
107 /* /68 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
108 /* /69 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
109 /* /70 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
110 /* /71 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
111 /* /72 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
112 /* /73 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
113 /* /74 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
114 /* /75 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
115 /* /76 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
116 /* /77 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
117 /* /78 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
118 /* /79 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
119 /* /80 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
120 /* /81 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
121 /* /82 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
122 /* /83 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
123 /* /84 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
124 /* /85 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
125 /* /86 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
126 /* /87 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
127 /* /88 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 } } },
128 /* /89 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00 } } },
129 /* /90 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00 } } },
130 /* /91 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00 } } },
131 /* /92 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00 } } },
132 /* /93 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00 } } },
133 /* /94 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00 } } },
134 /* /95 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00 } } },
135 /* /96 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 } } },
136 /* /97 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00 } } },
137 /* /98 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00 } } },
138 /* /99 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00 } } },
139 /* /100 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00 } } },
140 /* /101 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00 } } },
141 /* /102 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00 } } },
142 /* /103 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00 } } },
143 /* /104 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00 } } },
144 /* /105 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 } } },
145 /* /106 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00 } } },
146 /* /107 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00 } } },
147 /* /108 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00 } } },
148 /* /109 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00 } } },
149 /* /110 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00 } } },
150 /* /111 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00 } } },
151 /* /112 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00 } } },
152 /* /113 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00 } } },
153 /* /114 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00 } } },
154 /* /115 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00 } } },
155 /* /116 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00 } } },
156 /* /117 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00 } } },
157 /* /118 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00 } } },
158 /* /119 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00 } } },
159 /* /120 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 } } },
160 /* /121 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 } } },
161 /* /122 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0 } } },
162 /* /123 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0 } } },
163 /* /124 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 } } },
164 /* /125 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8 } } },
165 /* /126 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc } } },
166 /* /127 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe } } },
167 /* /128 */ { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }
168 };
169
170 /* Number of bits in prefix type. */
171 #ifndef PNBBY
172 #define PNBBY 8
173 #endif /* PNBBY */
174
175 #define MASKBIT(offset) ((0xff << (PNBBY - (offset))) & 0xff)
176
177 unsigned int
178 prefix_bit (const u_char *prefix, const u_char prefixlen)
179 {
180 unsigned int offset = prefixlen / 8;
181 unsigned int shift = 7 - (prefixlen % 8);
182
183 return (prefix[offset] >> shift) & 1;
184 }
185
186 unsigned int
187 prefix6_bit (const struct in6_addr *prefix, const u_char prefixlen)
188 {
189 return prefix_bit((const u_char *) &prefix->s6_addr, prefixlen);
190 }
191
192 int
193 str2family(const char *string)
194 {
195 if (!strcmp("ipv4", string))
196 return AF_INET;
197 else if (!strcmp("ipv6", string))
198 return AF_INET6;
199 else if (!strcmp("ethernet", string))
200 return AF_ETHERNET;
201 return -1;
202 }
203
204 /* Address Famiy Identifier to Address Family converter. */
205 int
206 afi2family (afi_t afi)
207 {
208 if (afi == AFI_IP)
209 return AF_INET;
210 else if (afi == AFI_IP6)
211 return AF_INET6;
212 else if (afi == AFI_ETHER)
213 return AF_ETHERNET;
214 return 0;
215 }
216
217 afi_t
218 family2afi (int family)
219 {
220 if (family == AF_INET)
221 return AFI_IP;
222 else if (family == AF_INET6)
223 return AFI_IP6;
224 else if (family == AF_ETHERNET)
225 return AFI_ETHER;
226 return 0;
227 }
228
229 const char *
230 afi2str(afi_t afi)
231 {
232 switch (afi) {
233 case AFI_IP:
234 return "IPv4";
235 case AFI_IP6:
236 return "IPv6";
237 case AFI_ETHER:
238 return "ethernet";
239 case AFI_MAX:
240 return "bad-value";
241 default:
242 break;
243 }
244 return NULL;
245 }
246
247 const char *
248 safi2str(safi_t safi)
249 {
250 switch (safi) {
251 case SAFI_UNICAST:
252 return "unicast";
253 case SAFI_MULTICAST:
254 return "multicast";
255 case SAFI_ENCAP:
256 return "encap";
257 case SAFI_MPLS_VPN:
258 return "vpn";
259 }
260 return NULL;
261 }
262
263 /* If n includes p prefix then return 1 else return 0. */
264 int
265 prefix_match (const struct prefix *n, const struct prefix *p)
266 {
267 int offset;
268 int shift;
269 const u_char *np, *pp;
270
271 /* If n's prefix is longer than p's one return 0. */
272 if (n->prefixlen > p->prefixlen)
273 return 0;
274
275 /* Set both prefix's head pointer. */
276 np = (const u_char *)&n->u.prefix;
277 pp = (const u_char *)&p->u.prefix;
278
279 offset = n->prefixlen / PNBBY;
280 shift = n->prefixlen % PNBBY;
281
282 if (shift)
283 if (maskbit[shift] & (np[offset] ^ pp[offset]))
284 return 0;
285
286 while (offset--)
287 if (np[offset] != pp[offset])
288 return 0;
289 return 1;
290 }
291
292 /* Copy prefix from src to dest. */
293 void
294 prefix_copy (struct prefix *dest, const struct prefix *src)
295 {
296 dest->family = src->family;
297 dest->prefixlen = src->prefixlen;
298
299 if (src->family == AF_INET)
300 dest->u.prefix4 = src->u.prefix4;
301 else if (src->family == AF_INET6)
302 dest->u.prefix6 = src->u.prefix6;
303 else if (src->family == AF_UNSPEC)
304 {
305 dest->u.lp.id = src->u.lp.id;
306 dest->u.lp.adv_router = src->u.lp.adv_router;
307 }
308 else if (src->family == AF_ETHERNET)
309 {
310 dest->u.prefix_eth = src->u.prefix_eth;
311 }
312 else
313 {
314 zlog (NULL, LOG_ERR, "prefix_copy(): Unknown address family %d",
315 src->family);
316 assert (0);
317 }
318 }
319
320 /*
321 * Return 1 if the address/netmask contained in the prefix structure
322 * is the same, and else return 0. For this routine, 'same' requires
323 * that not only the prefix length and the network part be the same,
324 * but also the host part. Thus, 10.0.0.1/8 and 10.0.0.2/8 are not
325 * the same. Note that this routine has the same return value sense
326 * as '==' (which is different from prefix_cmp).
327 */
328 int
329 prefix_same (const struct prefix *p1, const struct prefix *p2)
330 {
331 if ((p1 && !p2) || (!p1 && p2))
332 return 0;
333
334 if (!p1 && !p2)
335 return 1;
336
337 if (p1->family == p2->family && p1->prefixlen == p2->prefixlen)
338 {
339 if (p1->family == AF_INET)
340 if (IPV4_ADDR_SAME (&p1->u.prefix4.s_addr, &p2->u.prefix4.s_addr))
341 return 1;
342 if (p1->family == AF_INET6 )
343 if (IPV6_ADDR_SAME (&p1->u.prefix6.s6_addr, &p2->u.prefix6.s6_addr))
344 return 1;
345 if (p1->family == AF_ETHERNET) {
346 if (!memcmp(p1->u.prefix_eth.octet, p2->u.prefix_eth.octet, ETHER_ADDR_LEN))
347 return 1;
348 }
349 }
350 return 0;
351 }
352
353 /*
354 * Return 0 if the network prefixes represented by the struct prefix
355 * arguments are the same prefix, and 1 otherwise. Network prefixes
356 * are considered the same if the prefix lengths are equal and the
357 * network parts are the same. Host bits (which are considered masked
358 * by the prefix length) are not significant. Thus, 10.0.0.1/8 and
359 * 10.0.0.2/8 are considered equivalent by this routine. Note that
360 * this routine has the same return sense as strcmp (which is different
361 * from prefix_same).
362 */
363 int
364 prefix_cmp (const struct prefix *p1, const struct prefix *p2)
365 {
366 int offset;
367 int shift;
368
369 /* Set both prefix's head pointer. */
370 const u_char *pp1 = (const u_char *)&p1->u.prefix;
371 const u_char *pp2 = (const u_char *)&p2->u.prefix;
372
373 if (p1->family != p2->family || p1->prefixlen != p2->prefixlen)
374 return 1;
375
376 offset = p1->prefixlen / PNBBY;
377 shift = p1->prefixlen % PNBBY;
378
379 if (shift)
380 if (maskbit[shift] & (pp1[offset] ^ pp2[offset]))
381 return 1;
382
383 while (offset--)
384 if (pp1[offset] != pp2[offset])
385 return 1;
386
387 return 0;
388 }
389
390 /*
391 * Count the number of common bits in 2 prefixes. The prefix length is
392 * ignored for this function; the whole prefix is compared. If the prefix
393 * address families don't match, return -1; otherwise the return value is
394 * in range 0 ... maximum prefix length for the address family.
395 */
396 int
397 prefix_common_bits (const struct prefix *p1, const struct prefix *p2)
398 {
399 int pos, bit;
400 int length = 0;
401 u_char xor;
402
403 /* Set both prefix's head pointer. */
404 const u_char *pp1 = (const u_char *)&p1->u.prefix;
405 const u_char *pp2 = (const u_char *)&p2->u.prefix;
406
407 if (p1->family == AF_INET)
408 length = IPV4_MAX_BYTELEN;
409 if (p1->family == AF_INET6)
410 length = IPV6_MAX_BYTELEN;
411 if (p1->family != p2->family || !length)
412 return -1;
413
414 for (pos = 0; pos < length; pos++)
415 if (pp1[pos] != pp2[pos])
416 break;
417 if (pos == length)
418 return pos * 8;
419
420 xor = pp1[pos] ^ pp2[pos];
421 for (bit = 0; bit < 8; bit++)
422 if (xor & (1 << (7 - bit)))
423 break;
424
425 return pos * 8 + bit;
426 }
427
428 /* Return prefix family type string. */
429 const char *
430 prefix_family_str (const struct prefix *p)
431 {
432 if (p->family == AF_INET)
433 return "inet";
434 if (p->family == AF_INET6)
435 return "inet6";
436 if (p->family == AF_ETHERNET)
437 return "ether";
438 return "unspec";
439 }
440
441 /* Allocate new prefix_ipv4 structure. */
442 struct prefix_ipv4 *
443 prefix_ipv4_new ()
444 {
445 struct prefix_ipv4 *p;
446
447 /* Call prefix_new to allocate a full-size struct prefix to avoid problems
448 where the struct prefix_ipv4 is cast to struct prefix and unallocated
449 bytes were being referenced (e.g. in structure assignments). */
450 p = (struct prefix_ipv4 *)prefix_new();
451 p->family = AF_INET;
452 return p;
453 }
454
455 /* Free prefix_ipv4 structure. */
456 void
457 prefix_ipv4_free (struct prefix_ipv4 *p)
458 {
459 prefix_free((struct prefix *)p);
460 }
461
462 /* When string format is invalid return 0. */
463 int
464 str2prefix_ipv4 (const char *str, struct prefix_ipv4 *p)
465 {
466 int ret;
467 int plen;
468 char *pnt;
469 char *cp;
470
471 /* Find slash inside string. */
472 pnt = strchr (str, '/');
473
474 /* String doesn't contail slash. */
475 if (pnt == NULL)
476 {
477 /* Convert string to prefix. */
478 ret = inet_aton (str, &p->prefix);
479 if (ret == 0)
480 return 0;
481
482 /* If address doesn't contain slash we assume it host address. */
483 p->family = AF_INET;
484 p->prefixlen = IPV4_MAX_BITLEN;
485
486 return ret;
487 }
488 else
489 {
490 cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
491 strncpy (cp, str, pnt - str);
492 *(cp + (pnt - str)) = '\0';
493 ret = inet_aton (cp, &p->prefix);
494 XFREE (MTYPE_TMP, cp);
495
496 /* Get prefix length. */
497 plen = (u_char) atoi (++pnt);
498 if (plen > IPV4_MAX_PREFIXLEN)
499 return 0;
500
501 p->family = AF_INET;
502 p->prefixlen = plen;
503 }
504
505 return ret;
506 }
507
508 /* When string format is invalid return 0. */
509 int
510 str2prefix_eth (const char *str, struct prefix_eth *p)
511 {
512 int ret = 0;
513 int plen = 48;
514 char *pnt;
515 char *cp = NULL;
516 const char *str_addr = str;
517 unsigned int a[6];
518 int i;
519
520 /* Find slash inside string. */
521 pnt = strchr (str, '/');
522
523 if (pnt)
524 {
525 /* Get prefix length. */
526 plen = (u_char) atoi (++pnt);
527 if (plen > 48)
528 {
529 ret = 0;
530 goto done;
531 }
532
533 cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
534 strncpy (cp, str, pnt - str);
535 *(cp + (pnt - str)) = '\0';
536
537 str_addr = cp;
538 }
539
540 /* Convert string to prefix. */
541 if (sscanf(str_addr, "%2x:%2x:%2x:%2x:%2x:%2x",
542 a+0, a+1, a+2, a+3, a+4, a+5) != 6)
543 {
544 ret = 0;
545 goto done;
546 }
547 for (i = 0; i < 6; ++i)
548 {
549 p->eth_addr.octet[i] = a[i] & 0xff;
550 }
551 p->prefixlen = plen;
552 p->family = AF_ETHERNET;
553 ret = 1;
554
555 done:
556 if (cp)
557 XFREE (MTYPE_TMP, cp);
558
559 return ret;
560 }
561
562 /* Convert masklen into IP address's netmask (network byte order). */
563 void
564 masklen2ip (const int masklen, struct in_addr *netmask)
565 {
566 assert (masklen >= 0 && masklen <= IPV4_MAX_BITLEN);
567
568 /* left shift is only defined for less than the size of the type.
569 * we unconditionally use long long in case the target platform
570 * has defined behaviour for << 32 (or has a 64-bit left shift) */
571
572 if (sizeof(unsigned long long) > 4)
573 netmask->s_addr = htonl(0xffffffffULL << (32 - masklen));
574 else
575 netmask->s_addr = htonl(masklen ? 0xffffffffU << (32 - masklen) : 0);
576 }
577
578 /* Convert IP address's netmask into integer. We assume netmask is
579 sequential one. Argument netmask should be network byte order. */
580 u_char
581 ip_masklen (struct in_addr netmask)
582 {
583 uint32_t tmp = ~ntohl(netmask.s_addr);
584 if (tmp)
585 /* clz: count leading zeroes. sadly, the behaviour of this builtin
586 * is undefined for a 0 argument, even though most CPUs give 32 */
587 return __builtin_clz(tmp);
588 else
589 return 32;
590 }
591
592 /* Apply mask to IPv4 prefix (network byte order). */
593 void
594 apply_mask_ipv4 (struct prefix_ipv4 *p)
595 {
596 struct in_addr mask;
597 masklen2ip(p->prefixlen, &mask);
598 p->prefix.s_addr &= mask.s_addr;
599 }
600
601 /* If prefix is 0.0.0.0/0 then return 1 else return 0. */
602 int
603 prefix_ipv4_any (const struct prefix_ipv4 *p)
604 {
605 return (p->prefix.s_addr == 0 && p->prefixlen == 0);
606 }
607
608 /* Allocate a new ip version 6 route */
609 struct prefix_ipv6 *
610 prefix_ipv6_new (void)
611 {
612 struct prefix_ipv6 *p;
613
614 /* Allocate a full-size struct prefix to avoid problems with structure
615 size mismatches. */
616 p = (struct prefix_ipv6 *)prefix_new();
617 p->family = AF_INET6;
618 return p;
619 }
620
621 /* Free prefix for IPv6. */
622 void
623 prefix_ipv6_free (struct prefix_ipv6 *p)
624 {
625 prefix_free((struct prefix *)p);
626 }
627
628 /* If given string is valid return pin6 else return NULL */
629 int
630 str2prefix_ipv6 (const char *str, struct prefix_ipv6 *p)
631 {
632 char *pnt;
633 char *cp;
634 int ret;
635
636 pnt = strchr (str, '/');
637
638 /* If string doesn't contain `/' treat it as host route. */
639 if (pnt == NULL)
640 {
641 ret = inet_pton (AF_INET6, str, &p->prefix);
642 if (ret == 0)
643 return 0;
644 p->prefixlen = IPV6_MAX_BITLEN;
645 }
646 else
647 {
648 int plen;
649
650 cp = XMALLOC (MTYPE_TMP, (pnt - str) + 1);
651 strncpy (cp, str, pnt - str);
652 *(cp + (pnt - str)) = '\0';
653 ret = inet_pton (AF_INET6, cp, &p->prefix);
654 XFREE (MTYPE_TMP, cp);
655 if (ret == 0)
656 return 0;
657 plen = (u_char) atoi (++pnt);
658 if (plen > IPV6_MAX_BITLEN)
659 return 0;
660 p->prefixlen = plen;
661 }
662 p->family = AF_INET6;
663
664 return ret;
665 }
666
667 /* Convert struct in6_addr netmask into integer.
668 * FIXME return u_char as ip_maskleni() does. */
669 int
670 ip6_masklen (struct in6_addr netmask)
671 {
672 int len = 0;
673 unsigned char val;
674 unsigned char *pnt;
675
676 pnt = (unsigned char *) & netmask;
677
678 while ((*pnt == 0xff) && len < IPV6_MAX_BITLEN)
679 {
680 len += 8;
681 pnt++;
682 }
683
684 if (len < IPV6_MAX_BITLEN)
685 {
686 val = *pnt;
687 while (val)
688 {
689 len++;
690 val <<= 1;
691 }
692 }
693 return len;
694 }
695
696 void
697 masklen2ip6 (const int masklen, struct in6_addr *netmask)
698 {
699 assert (masklen >= 0 && masklen <= IPV6_MAX_BITLEN);
700 memcpy (netmask, maskbytes6 + masklen, sizeof (struct in6_addr));
701 }
702
703 void
704 apply_mask_ipv6 (struct prefix_ipv6 *p)
705 {
706 u_char *pnt;
707 int index;
708 int offset;
709
710 index = p->prefixlen / 8;
711
712 if (index < 16)
713 {
714 pnt = (u_char *) &p->prefix;
715 offset = p->prefixlen % 8;
716
717 pnt[index] &= maskbit[offset];
718 index++;
719
720 while (index < 16)
721 pnt[index++] = 0;
722 }
723 }
724
725 void
726 str2in6_addr (const char *str, struct in6_addr *addr)
727 {
728 int i;
729 unsigned int x;
730
731 /* %x must point to unsinged int */
732 for (i = 0; i < 16; i++)
733 {
734 sscanf (str + (i * 2), "%02x", &x);
735 addr->s6_addr[i] = x & 0xff;
736 }
737 }
738
739 void
740 apply_mask (struct prefix *p)
741 {
742 switch (p->family)
743 {
744 case AF_INET:
745 apply_mask_ipv4 ((struct prefix_ipv4 *)p);
746 break;
747 case AF_INET6:
748 apply_mask_ipv6 ((struct prefix_ipv6 *)p);
749 break;
750 default:
751 break;
752 }
753 return;
754 }
755
756 /* Utility function of convert between struct prefix <=> union sockunion.
757 * FIXME This function isn't used anywhere. */
758 struct prefix *
759 sockunion2prefix (const union sockunion *dest,
760 const union sockunion *mask)
761 {
762 if (dest->sa.sa_family == AF_INET)
763 {
764 struct prefix_ipv4 *p;
765
766 p = prefix_ipv4_new ();
767 p->family = AF_INET;
768 p->prefix = dest->sin.sin_addr;
769 p->prefixlen = ip_masklen (mask->sin.sin_addr);
770 return (struct prefix *) p;
771 }
772 if (dest->sa.sa_family == AF_INET6)
773 {
774 struct prefix_ipv6 *p;
775
776 p = prefix_ipv6_new ();
777 p->family = AF_INET6;
778 p->prefixlen = ip6_masklen (mask->sin6.sin6_addr);
779 memcpy (&p->prefix, &dest->sin6.sin6_addr, sizeof (struct in6_addr));
780 return (struct prefix *) p;
781 }
782 return NULL;
783 }
784
785 /* Utility function of convert between struct prefix <=> union sockunion. */
786 struct prefix *
787 sockunion2hostprefix (const union sockunion *su, struct prefix *prefix)
788 {
789 if (su->sa.sa_family == AF_INET)
790 {
791 struct prefix_ipv4 *p;
792
793 p = prefix ? (struct prefix_ipv4 *) prefix : prefix_ipv4_new ();
794 p->family = AF_INET;
795 p->prefix = su->sin.sin_addr;
796 p->prefixlen = IPV4_MAX_BITLEN;
797 return (struct prefix *) p;
798 }
799 if (su->sa.sa_family == AF_INET6)
800 {
801 struct prefix_ipv6 *p;
802
803 p = prefix ? (struct prefix_ipv6 *) prefix : prefix_ipv6_new ();
804 p->family = AF_INET6;
805 p->prefixlen = IPV6_MAX_BITLEN;
806 memcpy (&p->prefix, &su->sin6.sin6_addr, sizeof (struct in6_addr));
807 return (struct prefix *) p;
808 }
809 return NULL;
810 }
811
812 void
813 prefix2sockunion (const struct prefix *p, union sockunion *su)
814 {
815 memset (su, 0, sizeof (*su));
816
817 su->sa.sa_family = p->family;
818 if (p->family == AF_INET)
819 su->sin.sin_addr = p->u.prefix4;
820 if (p->family == AF_INET6)
821 memcpy (&su->sin6.sin6_addr, &p->u.prefix6, sizeof (struct in6_addr));
822 }
823
824 int
825 prefix_blen (const struct prefix *p)
826 {
827 switch (p->family)
828 {
829 case AF_INET:
830 return IPV4_MAX_BYTELEN;
831 break;
832 case AF_INET6:
833 return IPV6_MAX_BYTELEN;
834 break;
835 case AF_ETHERNET:
836 return ETHER_ADDR_LEN;
837 }
838 return 0;
839 }
840
841 /* Generic function for conversion string to struct prefix. */
842 int
843 str2prefix (const char *str, struct prefix *p)
844 {
845 int ret;
846
847 /* First we try to convert string to struct prefix_ipv4. */
848 ret = str2prefix_ipv4 (str, (struct prefix_ipv4 *) p);
849 if (ret)
850 return ret;
851
852 /* Next we try to convert string to struct prefix_ipv6. */
853 ret = str2prefix_ipv6 (str, (struct prefix_ipv6 *) p);
854 if (ret)
855 return ret;
856
857 /* Next we try to convert string to struct prefix_eth. */
858 ret = str2prefix_eth (str, (struct prefix_eth *) p);
859 if (ret)
860 return ret;
861
862 return 0;
863 }
864
865 const char *
866 prefix2str (union prefix46constptr pu, char *str, int size)
867 {
868 const struct prefix *p = pu.p;
869
870 if (p->family == AF_ETHERNET)
871 {
872 snprintf(str, size, "%02x:%02x:%02x:%02x:%02x:%02x/%d",
873 p->u.prefix_eth.octet[0], p->u.prefix_eth.octet[1],
874 p->u.prefix_eth.octet[2], p->u.prefix_eth.octet[3],
875 p->u.prefix_eth.octet[4], p->u.prefix_eth.octet[5],
876 p->prefixlen);
877 }
878 else
879 {
880 char buf[PREFIX2STR_BUFFER];
881 inet_ntop(p->family, &p->u.prefix, buf, sizeof(buf));
882 snprintf(str, size, "%s/%d", buf, p->prefixlen);
883 }
884
885 return str;
886 }
887
888 struct prefix *
889 prefix_new ()
890 {
891 struct prefix *p;
892
893 p = XCALLOC (MTYPE_PREFIX, sizeof *p);
894 return p;
895 }
896
897 /* Free prefix structure. */
898 void
899 prefix_free (struct prefix *p)
900 {
901 XFREE (MTYPE_PREFIX, p);
902 }
903
904 /* Utility function. Check the string only contains digit
905 * character.
906 * FIXME str.[c|h] would be better place for this function. */
907 int
908 all_digit (const char *str)
909 {
910 for (; *str != '\0'; str++)
911 if (!isdigit ((int) *str))
912 return 0;
913 return 1;
914 }
915
916 /* Utility function to convert ipv4 prefixes to Classful prefixes */
917 void apply_classful_mask_ipv4 (struct prefix_ipv4 *p)
918 {
919
920 u_int32_t destination;
921
922 destination = ntohl (p->prefix.s_addr);
923
924 if (p->prefixlen == IPV4_MAX_PREFIXLEN);
925 /* do nothing for host routes */
926 else if (IN_CLASSC (destination))
927 {
928 p->prefixlen=24;
929 apply_mask_ipv4(p);
930 }
931 else if (IN_CLASSB(destination))
932 {
933 p->prefixlen=16;
934 apply_mask_ipv4(p);
935 }
936 else
937 {
938 p->prefixlen=8;
939 apply_mask_ipv4(p);
940 }
941 }
942
943 in_addr_t
944 ipv4_network_addr (in_addr_t hostaddr, int masklen)
945 {
946 struct in_addr mask;
947
948 masklen2ip (masklen, &mask);
949 return hostaddr & mask.s_addr;
950 }
951
952 in_addr_t
953 ipv4_broadcast_addr (in_addr_t hostaddr, int masklen)
954 {
955 struct in_addr mask;
956
957 masklen2ip (masklen, &mask);
958 return (masklen != IPV4_MAX_PREFIXLEN-1) ?
959 /* normal case */
960 (hostaddr | ~mask.s_addr) :
961 /* special case for /31 */
962 (hostaddr ^ ~mask.s_addr);
963 }
964
965 /* Utility function to convert ipv4 netmask to prefixes
966 ex.) "1.1.0.0" "255.255.0.0" => "1.1.0.0/16"
967 ex.) "1.0.0.0" NULL => "1.0.0.0/8" */
968 int
969 netmask_str2prefix_str (const char *net_str, const char *mask_str,
970 char *prefix_str)
971 {
972 struct in_addr network;
973 struct in_addr mask;
974 u_char prefixlen;
975 u_int32_t destination;
976 int ret;
977
978 ret = inet_aton (net_str, &network);
979 if (! ret)
980 return 0;
981
982 if (mask_str)
983 {
984 ret = inet_aton (mask_str, &mask);
985 if (! ret)
986 return 0;
987
988 prefixlen = ip_masklen (mask);
989 }
990 else
991 {
992 destination = ntohl (network.s_addr);
993
994 if (network.s_addr == 0)
995 prefixlen = 0;
996 else if (IN_CLASSC (destination))
997 prefixlen = 24;
998 else if (IN_CLASSB (destination))
999 prefixlen = 16;
1000 else if (IN_CLASSA (destination))
1001 prefixlen = 8;
1002 else
1003 return 0;
1004 }
1005
1006 sprintf (prefix_str, "%s/%d", net_str, prefixlen);
1007
1008 return 1;
1009 }
1010
1011 /* Utility function for making IPv6 address string. */
1012 const char *
1013 inet6_ntoa (struct in6_addr addr)
1014 {
1015 static char buf[INET6_ADDRSTRLEN];
1016
1017 inet_ntop (AF_INET6, &addr, buf, INET6_ADDRSTRLEN);
1018 return buf;
1019 }