blob: db7d0b3980d3af45797164de8e929d5a6cf7c4f5 [file] [log] [blame]
Paul Jakma57345092011-12-25 17:52:09 +01001/*
2 * This file is free software: you may copy, redistribute and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 2 of the License, or (at your
5 * option) any later version.
6 *
7 * This file is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 * This file incorporates work covered by the following copyright and
16 * permission notice:
17 *
18Copyright 2011 by Matthieu Boutier and Juliusz Chroboczek
19
20Permission is hereby granted, free of charge, to any person obtaining a copy
21of this software and associated documentation files (the "Software"), to deal
22in the Software without restriction, including without limitation the rights
23to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24copies of the Software, and to permit persons to whom the Software is
25furnished to do so, subject to the following conditions:
26
27The above copyright notice and this permission notice shall be included in
28all copies or substantial portions of the Software.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36THE SOFTWARE.
37*/
38
39#include <sys/types.h>
40#include <sys/socket.h>
41#include <netinet/in.h>
42#include <netdb.h>
43#include <arpa/inet.h>
44
45#include <zebra.h>
46#include "prefix.h"
47#include "zclient.h"
48#include "kernel.h"
49#include "privs.h"
50#include "command.h"
51#include "vty.h"
52#include "memory.h"
53#include "thread.h"
54
55#include "util.h"
56#include "babel_interface.h"
57#include "babel_zebra.h"
58
59
60static int
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +010061kernel_route_v4(int add, const unsigned char *pref, unsigned short plen,
62 const unsigned char *gate, int ifindex,
63 unsigned int metric);
Paul Jakma57345092011-12-25 17:52:09 +010064static int
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +010065kernel_route_v6(int add, const unsigned char *pref, unsigned short plen,
66 const unsigned char *gate, int ifindex,
67 unsigned int metric);
Paul Jakma57345092011-12-25 17:52:09 +010068
Paul Jakma57345092011-12-25 17:52:09 +010069int
70kernel_interface_operational(struct interface *interface)
71{
72 return if_is_operative(interface);
73}
74
75int
Paul Jakma57345092011-12-25 17:52:09 +010076kernel_interface_mtu(struct interface *interface)
77{
78 return MIN(interface->mtu, interface->mtu6);
79}
80
81int
82kernel_interface_wireless(struct interface *interface)
83{
84 return 0;
85}
86
Paul Jakma57345092011-12-25 17:52:09 +010087int
88kernel_route(int operation, const unsigned char *pref, unsigned short plen,
89 const unsigned char *gate, int ifindex, unsigned int metric,
90 const unsigned char *newgate, int newifindex,
91 unsigned int newmetric)
92{
93 int rc;
Paul Jakma57345092011-12-25 17:52:09 +010094 int ipv4;
95
96 /* Check that the protocol family is consistent. */
97 if(plen >= 96 && v4mapped(pref)) {
98 if(!v4mapped(gate)) {
99 errno = EINVAL;
100 return -1;
101 }
102 ipv4 = 1;
103 } else {
104 if(v4mapped(gate)) {
105 errno = EINVAL;
106 return -1;
107 }
108 ipv4 = 0;
109 }
110
111 switch (operation) {
112 case ROUTE_ADD:
113 return ipv4 ?
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100114 kernel_route_v4(1, pref, plen, gate, ifindex, metric):
115 kernel_route_v6(1, pref, plen, gate, ifindex, metric);
Paul Jakma57345092011-12-25 17:52:09 +0100116 break;
117 case ROUTE_FLUSH:
118 return ipv4 ?
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100119 kernel_route_v4(0, pref, plen, gate, ifindex, metric):
120 kernel_route_v6(0, pref, plen, gate, ifindex, metric);
Paul Jakma57345092011-12-25 17:52:09 +0100121 break;
122 case ROUTE_MODIFY:
123 if(newmetric == metric && memcmp(newgate, gate, 16) == 0 &&
124 newifindex == ifindex)
125 return 0;
Matthieu Boutierec8d8d52012-01-20 15:32:16 +0100126 debugf(BABEL_DEBUG_ROUTE, "Modify route: delete old; add new.");
Matthieu Boutier53b21952012-01-31 17:09:55 +0100127 rc = ipv4 ?
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100128 kernel_route_v4(0, pref, plen, gate, ifindex, metric):
129 kernel_route_v6(0, pref, plen, gate, ifindex, metric);
Matthieu Boutier53b21952012-01-31 17:09:55 +0100130
131 if (rc < 0)
132 return -1;
Paul Jakma57345092011-12-25 17:52:09 +0100133
Matthieu Boutierec8d8d52012-01-20 15:32:16 +0100134 rc = ipv4 ?
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100135 kernel_route_v4(1, pref, plen, newgate, newifindex, newmetric):
136 kernel_route_v6(1, pref, plen, newgate, newifindex, newmetric);
Paul Jakma57345092011-12-25 17:52:09 +0100137
138 return rc;
139 break;
140 default:
141 zlog_err("this should never appens (false value - kernel_route)");
142 assert(0);
143 exit(1);
144 break;
145 }
146}
147
148static int
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100149kernel_route_v4(int add,
150 const unsigned char *pref, unsigned short plen,
151 const unsigned char *gate, int ifindex, unsigned int metric)
Paul Jakma57345092011-12-25 17:52:09 +0100152{
Paul Jakma57345092011-12-25 17:52:09 +0100153 struct zapi_ipv4 api; /* quagga's communication system */
154 struct prefix_ipv4 quagga_prefix; /* quagga's prefix */
155 struct in_addr babel_prefix_addr; /* babeld's prefix addr */
156 struct in_addr nexthop; /* next router to go */
157 struct in_addr *nexthop_pointer = &nexthop; /* it's an array! */
158
Matthieu Boutier53b21952012-01-31 17:09:55 +0100159 /* convert to be understandable by quagga */
Paul Jakma57345092011-12-25 17:52:09 +0100160 /* convert given addresses */
161 uchar_to_inaddr(&babel_prefix_addr, pref);
162 uchar_to_inaddr(&nexthop, gate);
163
164 /* make prefix structure */
165 memset (&quagga_prefix, 0, sizeof(quagga_prefix));
166 quagga_prefix.family = AF_INET;
167 IPV4_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
168 quagga_prefix.prefixlen = plen - 96; /* our plen is for v4mapped's addr */
169 apply_mask_ipv4(&quagga_prefix);
170
171 api.type = ZEBRA_ROUTE_BABEL;
172 api.flags = 0;
173 api.message = 0;
Denis Ovsienkoa19a3bf2012-01-21 23:16:00 +0400174 api.safi = SAFI_UNICAST;
Juliusz Chroboczekb6475ec2012-02-09 12:29:10 +0100175
176 /* Unlike the native Linux and BSD interfaces, Quagga doesn't like
177 there to be both and IPv4 nexthop and an ifindex. Omit the
178 ifindex, and assume that the connected prefixes be set up
179 correctly. */
180
Paul Jakma57345092011-12-25 17:52:09 +0100181 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
Juliusz Chroboczekb6475ec2012-02-09 12:29:10 +0100182 api.ifindex_num = 0;
Juliusz Chroboczekd70ab9d2012-02-09 17:23:09 +0100183 if(metric >= KERNEL_INFINITY) {
184 api.flags = ZEBRA_FLAG_BLACKHOLE;
185 api.nexthop_num = 0;
186 } else {
187 api.nexthop_num = 1;
188 api.nexthop = &nexthop_pointer;
189 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
190 api.metric = metric;
191 }
Paul Jakma57345092011-12-25 17:52:09 +0100192
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100193 debugf(BABEL_DEBUG_ROUTE, "%s route (ipv4) to zebra",
194 add ? "adding" : "removing" );
195 return zapi_ipv4_route (add ? ZEBRA_IPV4_ROUTE_ADD :
196 ZEBRA_IPV4_ROUTE_DELETE,
197 zclient, &quagga_prefix, &api);
Paul Jakma57345092011-12-25 17:52:09 +0100198}
199
200static int
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100201kernel_route_v6(int add, const unsigned char *pref, unsigned short plen,
202 const unsigned char *gate, int ifindex, unsigned int metric)
Paul Jakma57345092011-12-25 17:52:09 +0100203{
204 unsigned int tmp_ifindex = ifindex; /* (for typing) */
205 struct zapi_ipv6 api; /* quagga's communication system */
206 struct prefix_ipv6 quagga_prefix; /* quagga's prefix */
207 struct in6_addr babel_prefix_addr; /* babeld's prefix addr */
208 struct in6_addr nexthop; /* next router to go */
209 struct in6_addr *nexthop_pointer = &nexthop;
210
Matthieu Boutier53b21952012-01-31 17:09:55 +0100211 /* convert to be understandable by quagga */
Paul Jakma57345092011-12-25 17:52:09 +0100212 /* convert given addresses */
213 uchar_to_in6addr(&babel_prefix_addr, pref);
214 uchar_to_in6addr(&nexthop, gate);
215
216 /* make prefix structure */
217 memset (&quagga_prefix, 0, sizeof(quagga_prefix));
218 quagga_prefix.family = AF_INET6;
219 IPV6_ADDR_COPY (&quagga_prefix.prefix, &babel_prefix_addr);
220 quagga_prefix.prefixlen = plen;
221 apply_mask_ipv6(&quagga_prefix);
222
223 api.type = ZEBRA_ROUTE_BABEL;
224 api.flags = 0;
225 api.message = 0;
Denis Ovsienkoa19a3bf2012-01-21 23:16:00 +0400226 api.safi = SAFI_UNICAST;
Paul Jakma57345092011-12-25 17:52:09 +0100227 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
Juliusz Chroboczekd70ab9d2012-02-09 17:23:09 +0100228 if(metric >= KERNEL_INFINITY) {
Juliusz Chroboczekb63b4482012-02-14 11:17:32 +0100229 api.flags = ZEBRA_FLAG_BLACKHOLE;
Juliusz Chroboczekd70ab9d2012-02-09 17:23:09 +0100230 api.nexthop_num = 0;
231 api.ifindex_num = 0;
232 } else {
233 api.nexthop_num = 1;
234 api.nexthop = &nexthop_pointer;
235 SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
236 api.ifindex_num = 1;
237 api.ifindex = &tmp_ifindex;
238 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
239 api.metric = metric;
240 }
Paul Jakma57345092011-12-25 17:52:09 +0100241
Juliusz Chroboczek5ca79862012-02-14 11:22:29 +0100242 debugf(BABEL_DEBUG_ROUTE, "%s route (ipv6) to zebra",
243 add ? "adding" : "removing" );
244 return zapi_ipv6_route (add ? ZEBRA_IPV6_ROUTE_ADD :
245 ZEBRA_IPV6_ROUTE_DELETE,
246 zclient, &quagga_prefix, &api);
Paul Jakma57345092011-12-25 17:52:09 +0100247}
248
249int
Paul Jakma57345092011-12-25 17:52:09 +0100250if_eui64(char *ifname, int ifindex, unsigned char *eui)
251{
252 struct interface *ifp = if_lookup_by_index(ifindex);
253 if (ifp == NULL) {
254 return -1;
255 }
256#ifdef HAVE_STRUCT_SOCKADDR_DL
257 u_char len = ifp->sdl.sdl_alen;
258 char *tmp = ifp->sdl.sdl_data + ifp->sdl.sdl_nlen;
259#else
260 u_char len = (u_char) ifp->hw_addr_len;
261 char *tmp = (void*) ifp->hw_addr;
262#endif
263 if (len == 8) {
264 memcpy(eui, tmp, 8);
265 eui[0] ^= 2;
266 } else if (len == 6) {
267 memcpy(eui, tmp, 3);
268 eui[3] = 0xFF;
269 eui[4] = 0xFE;
270 memcpy(eui+5, tmp+3, 3);
Paul Jakma57345092011-12-25 17:52:09 +0100271 } else {
272 return -1;
273 }
274 return 0;
275}