blob: d9179f914424faf42d7e795ba73e7bec05a393b8 [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
2 * IS-IS Rout(e)ing protocol - isis_zebra.c
3 *
4 * Copyright (C) 2001,2002 Sampo Saaristo
5 * Tampere University of Technology
6 * Institute of Communications Engineering
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public Licenseas published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24#include <net/ethernet.h>
25
26#include "thread.h"
27#include "command.h"
28#include "memory.h"
29#include "log.h"
30#include "if.h"
31#include "network.h"
32#include "prefix.h"
33#include "zclient.h"
34#include "stream.h"
35#include "linklist.h"
36
37#include "isisd/isis_constants.h"
38#include "isisd/isis_common.h"
39#include "isisd/isis_circuit.h"
40#include "isisd/isis_csm.h"
41#include "isisd/isis_route.h"
42#include "isisd/isis_zebra.h"
43
44struct zclient *zclient = NULL;
45
46extern struct thread_master *master;
47
48int
49isis_zebra_if_add (int command, struct zclient *zclient, zebra_size_t length)
50{
51 struct interface *ifp;
52
53 ifp = zebra_interface_add_read (zclient->ibuf);
54
55
56 zlog_info ("Zebra I/F add: %s index %d flags %ld metric %d mtu %d",
57 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
58
59 if (if_is_up (ifp))
60 isis_csm_state_change (IF_UP_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
61
62 return 0;
63}
64
65int
66isis_zebra_if_del (int command, struct zclient *zclient, zebra_size_t length)
67{
68 struct interface *ifp;
69 struct stream *s;
70
71 s = zclient->ibuf;
72 ifp = zebra_interface_state_read (s);
73
74 if (!ifp)
75 return 0;
76
77 if (if_is_up (ifp))
78 zlog_warn ("Zebra: got delete of %s, but interface is still up",
79 ifp->name);
80
81 zlog_info ("Zebra I/F delete: %s index %d flags %ld metric %d mtu %d",
82 ifp->name, ifp->ifindex, ifp->flags, ifp->metric, ifp->mtu);
83
84 if_delete (ifp);
85
86 isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
87
88 return 0;
89}
90
91struct interface *
92zebra_interface_if_lookup (struct stream *s)
93{
94 struct interface *ifp;
95 u_char ifname_tmp[INTERFACE_NAMSIZ];
96
97 /* Read interface name. */
98 stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
99
100 /* Lookup this by interface index. */
101 ifp = if_lookup_by_name (ifname_tmp);
102
103 /* If such interface does not exist, indicate an error */
104 if (!ifp)
105 return NULL;
106
107 return ifp;
108}
109
110void
111zebra_interface_if_set_value (struct stream *s, struct interface *ifp)
112{
113 /* Read interface's index. */
114 ifp->ifindex = stream_getl (s);
115
116 /* Read interface's value. */
117 ifp->flags = stream_getl (s);
118 ifp->metric = stream_getl (s);
119 ifp->mtu = stream_getl (s);
120 ifp->bandwidth = stream_getl (s);
121}
122
123int
124isis_zebra_if_state_up (int command, struct zclient *zclient,
125 zebra_size_t length)
126{
127 struct interface *ifp;
128
129 ifp = zebra_interface_if_lookup (zclient->ibuf);
130
131 if (!ifp)
132 return 0;
133
134 if (if_is_up (ifp)) {
135 zebra_interface_if_set_value (zclient->ibuf, ifp);
136 isis_circuit_update_params (circuit_scan_by_ifp (ifp), ifp);
137 return 0;
138 }
139
140 zebra_interface_if_set_value (zclient->ibuf, ifp);
141 isis_csm_state_change (IF_UP_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
142
143 return 0;
144}
145
146
147int
148isis_zebra_if_state_down (int command, struct zclient *zclient,
149 zebra_size_t length)
150{
151 struct interface *ifp;
152
153 ifp = zebra_interface_if_lookup (zclient->ibuf);
154
155 if (ifp == NULL)
156 return 0;
157
158 if (if_is_up (ifp)) {
159 zebra_interface_if_set_value (zclient->ibuf, ifp);
160 isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
161 }
162
163 return 0;
164}
165
166int
167isis_zebra_if_address_add (int command, struct zclient *zclient,
168 zebra_size_t length)
169{
170 struct connected *c;
171 struct prefix *p;
172 u_char buf[BUFSIZ];
173
174 c = zebra_interface_address_add_read (zclient->ibuf);
175
176 if (c == NULL)
177 return 0;
178
179 p = c->address;
180
181 prefix2str (p, buf, BUFSIZ);
182#ifdef EXTREME_DEBUG
183 if (p->family == AF_INET)
184 zlog_info ("connected IP address %s", buf);
185#ifdef HAVE_IPV6
186 if (p->family == AF_INET6)
187 zlog_info ("connected IPv6 address %s", buf);
188#endif /* HAVE_IPV6 */
189#endif /* EXTREME_DEBUG */
190 isis_circuit_add_addr (circuit_scan_by_ifp (c->ifp), c);
191
192 return 0;
193}
194
195int
196isis_zebra_if_address_del (int command, struct zclient *client,
197 zebra_size_t length)
198{
199 struct connected *c;
200 struct interface *ifp;
201
202 c = zebra_interface_address_delete_read (zclient->ibuf);
203
204 if (c == NULL)
205 return 0;
206
207 ifp = c->ifp;
208
209 connected_free (c);
210
211 isis_circuit_del_addr (circuit_scan_by_ifp (ifp), c);
212
213 return 0;
214}
215
216void
217isis_zebra_route_add_ipv4 (struct prefix *prefix,
218 struct isis_route_info *route_info)
219{
220 u_char message, flags;
221 int psize;
222 struct stream *stream;
223 struct isis_nexthop *nexthop;
224 struct listnode *node;
225
226 if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
227 return;
228
229 if (zclient->redist[ZEBRA_ROUTE_ISIS]) {
230 message = 0;
231 flags = 0;
232
233 SET_FLAG (message, ZAPI_MESSAGE_NEXTHOP);
234 SET_FLAG (message, ZAPI_MESSAGE_METRIC);
235 SET_FLAG (message, ZAPI_MESSAGE_DISTANCE);
236
237 stream = zclient->obuf;
238 stream_reset (stream);
239 /* Length place holder. */
240 stream_putw (stream, 0);
241 /* command */
242 stream_putc (stream, ZEBRA_IPV4_ROUTE_ADD);
243 /* type */
244 stream_putc (stream, ZEBRA_ROUTE_ISIS);
245 /* flags */
246 stream_putc (stream, flags);
247 /* message */
248 stream_putc (stream, message);
249 /* prefix information */
250 psize = PSIZE (prefix->prefixlen);
251 stream_putc (stream, prefix->prefixlen);
252 stream_write (stream, (u_char *)&prefix->u.prefix4, psize);
253
254 stream_putc (stream, listcount (route_info->nexthops));
255
256 /* Nexthop, ifindex, distance and metric information */
257 for (node = listhead (route_info->nexthops); node; nextnode (node)) {
258 nexthop = getdata (node);
259 /* FIXME: can it be ? */
260 if (nexthop->ip.s_addr != INADDR_ANY) {
261 stream_putc (stream, ZEBRA_NEXTHOP_IPV4);
262 stream_put_in_addr (stream, &nexthop->ip);
263 } else {
264 stream_putc (stream, ZEBRA_NEXTHOP_IFINDEX);
265 stream_putl (stream, nexthop->ifindex);
266 }
267 }
268 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
269 stream_putc (stream, route_info->depth);
270 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
271 stream_putl (stream, route_info->cost);
272
273 stream_putw_at (stream, 0, stream_get_endp (stream));
274 writen (zclient->sock, stream->data, stream_get_endp (stream));
275 }
276}
277
278void
279isis_zebra_route_del_ipv4 (struct prefix *prefix,
280 struct isis_route_info *route_info)
281{
282 struct zapi_ipv4 api;
283 struct prefix_ipv4 prefix4;
284
285 if (zclient->redist[ZEBRA_ROUTE_ISIS]) {
286 api.type = ZEBRA_ROUTE_ISIS;
287 api.flags = 0;
288 api.message = 0;
289 prefix4.family = AF_INET;
290 prefix4.prefixlen = prefix->prefixlen;
291 prefix4.prefix = prefix->u.prefix4;
292 zapi_ipv4_delete (zclient, &prefix4, &api);
293 }
294
295 return;
296}
297
298#ifdef HAVE_IPV6
299void
300isis_zebra_route_add_ipv6 (struct prefix *prefix,
301 struct isis_route_info *route_info)
302{
303 struct zapi_ipv6 api;
304 struct in6_addr **nexthop_list;
305 unsigned int *ifindex_list;
306 struct isis_nexthop6 *nexthop6;
307 int i, size;
308 struct listnode *node;
309 struct prefix_ipv6 prefix6;
310
311 if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
312 return;
313
314 api.type = ZEBRA_ROUTE_ISIS;
315 api.flags = 0;
316 api.message = 0;
317 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
318 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
319 SET_FLAG (api.message, ZAPI_MESSAGE_METRIC);
320 api.metric = route_info->cost;
321#if 0
322 SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE);
323 api.distance = route_info->depth;
324#endif
325 api.nexthop_num = listcount (route_info->nexthops6);
326 api.ifindex_num = listcount (route_info->nexthops6);
327
328 /* allocate memory for nexthop_list */
329 size = sizeof (struct isis_nexthop6 *) * listcount (route_info->nexthops6);
330 nexthop_list = (struct in6_addr **) XMALLOC (MTYPE_ISIS_TMP, size);
331 if (!nexthop_list) {
332 zlog_err ("isis_zebra_add_route_ipv6: out of memory!");
333 return;
334 }
335
336 /* allocate memory for ifindex_list */
337 size = sizeof (unsigned int) * listcount (route_info->nexthops6);
338 ifindex_list = (unsigned int *) XMALLOC (MTYPE_ISIS_TMP, size);
339 if (!ifindex_list) {
340 zlog_err ("isis_zebra_add_route_ipv6: out of memory!");
341 XFREE (MTYPE_ISIS_TMP, nexthop_list);
342 return;
343 }
344
345 /* for each nexthop */
346 i = 0;
347 for (node = listhead (route_info->nexthops6); node; nextnode (node)) {
348 nexthop6 = getdata (node);
349
350 if (!IN6_IS_ADDR_LINKLOCAL (&nexthop6->ip6) &&
351 !IN6_IS_ADDR_UNSPECIFIED (&nexthop6->ip6)) {
352 api.nexthop_num--;
353 api.ifindex_num--;
354 continue;
355 }
356
357 nexthop_list[i] = &nexthop6->ip6;
358 ifindex_list[i] = nexthop6->ifindex;
359 i++;
360 }
361
362 api.nexthop = nexthop_list;
363 api.ifindex = ifindex_list;
364
365 if (api.nexthop_num && api.ifindex_num) {
366 prefix6.family = AF_INET6;
367 prefix6.prefixlen = prefix->prefixlen;
368 memcpy (&prefix6.prefix, &prefix->u.prefix6, sizeof (struct in6_addr));
369 zapi_ipv6_add (zclient, &prefix6, &api);
370 SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC);
371 }
372
373 XFREE (MTYPE_ISIS_TMP, nexthop_list);
374 XFREE (MTYPE_ISIS_TMP, ifindex_list);
375
376 return;
377}
378
379void
380isis_zebra_route_del_ipv6 (struct prefix *prefix,
381 struct isis_route_info *route_info)
382{
383 struct zapi_ipv6 api;
384 struct in6_addr **nexthop_list;
385 unsigned int *ifindex_list;
386 struct isis_nexthop6 *nexthop6;
387 int i, size;
388 struct listnode *node;
389 struct prefix_ipv6 prefix6;
390
391 if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
392 return;
393
394 api.type = ZEBRA_ROUTE_ISIS;
395 api.flags = 0;
396 api.message = 0;
397 SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP);
398 SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX);
399 api.nexthop_num = listcount (route_info->nexthops6);
400 api.ifindex_num = listcount (route_info->nexthops6);
401
402 /* allocate memory for nexthop_list */
403 size = sizeof (struct isis_nexthop6 *) * listcount (route_info->nexthops6);
404 nexthop_list = (struct in6_addr **) XMALLOC (MTYPE_ISIS_TMP, size);
405 if (!nexthop_list) {
406 zlog_err ("isis_zebra_route_del_ipv6: out of memory!");
407 return;
408 }
409
410 /* allocate memory for ifindex_list */
411 size = sizeof (unsigned int) * listcount (route_info->nexthops6);
412 ifindex_list = (unsigned int *) XMALLOC (MTYPE_ISIS_TMP, size);
413 if (!ifindex_list) {
414 zlog_err ("isis_zebra_route_del_ipv6: out of memory!");
415 XFREE (MTYPE_ISIS_TMP, nexthop_list);
416 return;
417 }
418
419 /* for each nexthop */
420 i = 0;
421 for (node = listhead (route_info->nexthops6); node; nextnode (node)) {
422 nexthop6 = getdata (node);
423
424 if (!IN6_IS_ADDR_LINKLOCAL (&nexthop6->ip6) &&
425 !IN6_IS_ADDR_UNSPECIFIED (&nexthop6->ip6)) {
426 api.nexthop_num--;
427 api.ifindex_num--;
428 continue;
429 }
430
431 nexthop_list[i] = &nexthop6->ip6;
432 ifindex_list[i] = nexthop6->ifindex;
433 i++;
434 }
435
436 api.nexthop = nexthop_list;
437 api.ifindex = ifindex_list;
438
439 if (api.nexthop_num && api.ifindex_num) {
440 prefix6.family = AF_INET6;
441 prefix6.prefixlen = prefix->prefixlen;
442 memcpy (&prefix6.prefix, &prefix->u.prefix6, sizeof (struct in6_addr));
443 zapi_ipv6_delete (zclient, &prefix6, &api);
444 UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC);
445 }
446
447 XFREE (MTYPE_ISIS_TMP, nexthop_list);
448 XFREE (MTYPE_ISIS_TMP, ifindex_list);
449}
450
451
452#endif /* HAVE_IPV6 */
453
454
455
456void
457isis_zebra_route_update (struct prefix *prefix,
458 struct isis_route_info *route_info)
459{
460 if (zclient->sock < 0)
461 return;
462
463 if (!zclient->redist[ZEBRA_ROUTE_ISIS])
464 return;
465
466 if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ACTIVE)) {
467 if (prefix->family == AF_INET)
468 isis_zebra_route_add_ipv4 (prefix, route_info);
469#ifdef HAVE_IPV6
470 else if (prefix->family == AF_INET6)
471 isis_zebra_route_add_ipv6 (prefix, route_info);
472#endif /* HAVE_IPV6 */
473 } else {
474 if (prefix->family == AF_INET)
475 isis_zebra_route_del_ipv4 (prefix, route_info);
476#ifdef HAVE_IPV6
477 else if (prefix->family == AF_INET6)
478 isis_zebra_route_del_ipv6 (prefix, route_info);
479#endif /* HAVE_IPV6 */
480 }
481 return;
482}
483
484
485int
486isis_zebra_read_ipv4 (int command, struct zclient *zclient,
487 zebra_size_t length)
488{
489 struct stream *stream;
490 struct zapi_ipv4 api;
491 struct prefix_ipv4 p;
492 unsigned long ifindex;
493 struct in_addr nexthop;
494
495 stream = zclient->ibuf;
496 memset (&p, 0, sizeof (struct prefix_ipv4));
497 ifindex = 0;
498
499 api.type = stream_getc (stream);
500 api.flags = stream_getc (stream);
501 api.message = stream_getc (stream);
502
503 p.family = AF_INET;
504 p.prefixlen = stream_getc (stream);
505 stream_get (&p.prefix, stream, PSIZE (p.prefixlen));
506
507 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) {
508 api.nexthop_num = stream_getc (stream);
509 nexthop.s_addr = stream_get_ipv4 (stream);
510 }
511 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) {
512 api.ifindex_num = stream_getc (stream);
513 ifindex = stream_getl (stream);
514 }
515 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
516 api.distance = stream_getc (stream);
517 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
518 api.metric = stream_getl (stream);
519 else
520 api.metric = 0;
521
522 if (command == ZEBRA_IPV4_ROUTE_ADD) {
523 zlog_info ("IPv4 Route add from Z");
524 }
525
526 return 0;
527}
528
529
530int
531isis_zebra_read_ipv6 (int command, struct zclient *zclient,
532 zebra_size_t length)
533{
534
535 return 0;
536}
537
538#define ISIS_TYPE_IS_REDISTRIBUTED(T) \
539T == ZEBRA_ROUTE_MAX ? zclient->default_information : zclient->redist[type]
540
541int
542isis_distribute_list_update (int routetype)
543{
544 return 0;
545}
546
547int
548isis_redistribute_default_set(int routetype, int metric_type, int metric_value)
549{
550 return 0;
551}
552
553
554void
555isis_zebra_init ()
556{
557
558 zclient = zclient_new ();
559 zclient_init (zclient, ZEBRA_ROUTE_ISIS);
560 zclient->interface_add = isis_zebra_if_add;
561 zclient->interface_delete = isis_zebra_if_del;
562 zclient->interface_up = isis_zebra_if_state_up;
563 zclient->interface_down = isis_zebra_if_state_down;
564 zclient->interface_address_add = isis_zebra_if_address_add;
565 zclient->interface_address_delete = isis_zebra_if_address_del;
566 zclient->ipv4_route_add = isis_zebra_read_ipv4;
567 zclient->ipv4_route_delete = isis_zebra_read_ipv4;
568#ifdef HAVE_IPV6
569 zclient->ipv6_route_add = isis_zebra_read_ipv6;
570 zclient->ipv6_route_delete = isis_zebra_read_ipv6;
571#endif /* HAVE_IPV6 */
572
573 return;
574}
575
576void
577isis_zebra_finish ()
578{
579
580 zclient_stop (zclient);
581 zclient_free (zclient);
582 zclient = (struct zclient *) NULL;
583
584 return;
585}
586
587
588
589
590
591
592