]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Zebra daemon server routine. |
2 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with GNU Zebra; see the file COPYING. If not, write to the | |
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | * Boston, MA 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "prefix.h" | |
25 | #include "command.h" | |
26 | #include "if.h" | |
27 | #include "thread.h" | |
28 | #include "stream.h" | |
29 | #include "memory.h" | |
30 | #include "table.h" | |
31 | #include "rib.h" | |
32 | #include "network.h" | |
33 | #include "sockunion.h" | |
34 | #include "log.h" | |
35 | #include "zclient.h" | |
edd7c245 | 36 | #include "privs.h" |
719e9741 | 37 | #include "network.h" |
38 | #include "buffer.h" | |
718e3744 | 39 | |
40 | #include "zebra/zserv.h" | |
18a6dce6 | 41 | #include "zebra/router-id.h" |
718e3744 | 42 | #include "zebra/redistribute.h" |
43 | #include "zebra/debug.h" | |
44 | #include "zebra/ipforward.h" | |
45 | \f | |
46 | /* Event list of zebra. */ | |
47 | enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE }; | |
48 | ||
b21b19c5 | 49 | extern struct zebra_t zebrad; |
718e3744 | 50 | |
b9df2d25 | 51 | static void zebra_event (enum event event, int sock, struct zserv *client); |
ccf3557b | 52 | |
edd7c245 | 53 | extern struct zebra_privs_t zserv_privs; |
718e3744 | 54 | \f |
55 | /* For logging of zebra meesages. */ | |
fce954f8 | 56 | static const char *zebra_command_str [] = |
718e3744 | 57 | { |
58 | "NULL", | |
59 | "ZEBRA_INTERFACE_ADD", | |
60 | "ZEBRA_INTERFACE_DELETE", | |
61 | "ZEBRA_INTERFACE_ADDRESS_ADD", | |
62 | "ZEBRA_INTERFACE_ADDRESS_DELETE", | |
63 | "ZEBRA_INTERFACE_UP", | |
64 | "ZEBRA_INTERFACE_DOWN", | |
65 | "ZEBRA_IPV4_ROUTE_ADD", | |
66 | "ZEBRA_IPV4_ROUTE_DELETE", | |
67 | "ZEBRA_IPV6_ROUTE_ADD", | |
68 | "ZEBRA_IPV6_ROUTE_DELETE", | |
69 | "ZEBRA_REDISTRIBUTE_ADD", | |
70 | "ZEBRA_REDISTRIBUTE_DELETE", | |
71 | "ZEBRA_REDISTRIBUTE_DEFAULT_ADD", | |
72 | "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE", | |
73 | "ZEBRA_IPV4_NEXTHOP_LOOKUP", | |
74 | "ZEBRA_IPV6_NEXTHOP_LOOKUP", | |
75 | "ZEBRA_IPV4_IMPORT_LOOKUP", | |
18a6dce6 | 76 | "ZEBRA_IPV6_IMPORT_LOOKUP", |
77 | "ZEBRA_ROUTER_ID_ADD", | |
78 | "ZEBRA_ROUTER_ID_DELETE", | |
79 | "ZEBRA_ROUTER_ID_UPDATE" | |
718e3744 | 80 | }; |
81 | \f | |
ccf3557b | 82 | |
719e9741 | 83 | static void zebra_client_close (struct zserv *client); |
ccf3557b | 84 | |
719e9741 | 85 | static int |
86 | zserv_delayed_close(struct thread *thread) | |
ccf3557b | 87 | { |
719e9741 | 88 | struct zserv *client = THREAD_ARG(thread); |
ccf3557b | 89 | |
719e9741 | 90 | client->t_suicide = NULL; |
91 | zebra_client_close(client); | |
ccf3557b | 92 | return 0; |
93 | } | |
94 | ||
719e9741 | 95 | static int |
96 | zserv_flush_data(struct thread *thread) | |
ccf3557b | 97 | { |
719e9741 | 98 | struct zserv *client = THREAD_ARG(thread); |
ccf3557b | 99 | |
719e9741 | 100 | client->t_write = NULL; |
101 | if (client->t_suicide) | |
ccf3557b | 102 | { |
719e9741 | 103 | zebra_client_close(client); |
104 | return -1; | |
ccf3557b | 105 | } |
719e9741 | 106 | switch (buffer_flush_available(client->wb, client->sock)) |
ccf3557b | 107 | { |
719e9741 | 108 | case BUFFER_ERROR: |
109 | zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, " | |
110 | "closing", __func__, client->sock); | |
111 | zebra_client_close(client); | |
112 | break; | |
113 | case BUFFER_PENDING: | |
114 | client->t_write = thread_add_write(zebrad.master, zserv_flush_data, | |
115 | client, client->sock); | |
116 | break; | |
117 | case BUFFER_EMPTY: | |
118 | break; | |
ccf3557b | 119 | } |
719e9741 | 120 | return 0; |
121 | } | |
ccf3557b | 122 | |
719e9741 | 123 | static int |
124 | zebra_server_send_message(struct zserv *client) | |
125 | { | |
126 | if (client->t_suicide) | |
127 | return -1; | |
128 | switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf), | |
129 | stream_get_endp(client->obuf))) | |
130 | { | |
131 | case BUFFER_ERROR: | |
132 | zlog_warn("%s: buffer_write failed to zserv client fd %d, closing", | |
133 | __func__, client->sock); | |
134 | /* Schedule a delayed close since many of the functions that call this | |
135 | one do not check the return code. They do not allow for the | |
136 | possibility that an I/O error may have caused the client to be | |
137 | deleted. */ | |
138 | client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close, | |
139 | client, 0); | |
140 | return -1; | |
141 | break; | |
142 | case BUFFER_EMPTY: | |
143 | THREAD_OFF(client->t_write); | |
144 | break; | |
145 | case BUFFER_PENDING: | |
146 | THREAD_WRITE_ON(zebrad.master, client->t_write, | |
147 | zserv_flush_data, client, client->sock); | |
148 | break; | |
149 | } | |
ccf3557b | 150 | return 0; |
151 | } | |
152 | ||
718e3744 | 153 | /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */ |
b9df2d25 | 154 | /* |
155 | * This function is called in the following situations: | |
156 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request | |
157 | * from the client. | |
158 | * - at startup, when zebra figures out the available interfaces | |
159 | * - when an interface is added (where support for | |
160 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when | |
161 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is | |
162 | * received) | |
163 | */ | |
718e3744 | 164 | int |
165 | zsend_interface_add (struct zserv *client, struct interface *ifp) | |
166 | { | |
167 | struct stream *s; | |
168 | ||
169 | /* Check this client need interface information. */ | |
170 | if (! client->ifinfo) | |
719e9741 | 171 | return 0; |
718e3744 | 172 | |
173 | s = client->obuf; | |
174 | stream_reset (s); | |
175 | ||
176 | /* Place holder for size. */ | |
177 | stream_putw (s, 0); | |
178 | ||
179 | /* Message type. */ | |
180 | stream_putc (s, ZEBRA_INTERFACE_ADD); | |
181 | ||
182 | /* Interface information. */ | |
183 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); | |
184 | stream_putl (s, ifp->ifindex); | |
2e3b2e47 | 185 | stream_putc (s, ifp->status); |
718e3744 | 186 | stream_putl (s, ifp->flags); |
187 | stream_putl (s, ifp->metric); | |
188 | stream_putl (s, ifp->mtu); | |
b9df2d25 | 189 | stream_putl (s, ifp->mtu6); |
718e3744 | 190 | stream_putl (s, ifp->bandwidth); |
191 | #ifdef HAVE_SOCKADDR_DL | |
192 | stream_put (s, &ifp->sdl, sizeof (ifp->sdl)); | |
193 | #else | |
194 | stream_putl (s, ifp->hw_addr_len); | |
195 | if (ifp->hw_addr_len) | |
196 | stream_put (s, ifp->hw_addr, ifp->hw_addr_len); | |
197 | #endif /* HAVE_SOCKADDR_DL */ | |
198 | ||
199 | /* Write packet size. */ | |
200 | stream_putw_at (s, 0, stream_get_endp (s)); | |
201 | ||
719e9741 | 202 | return zebra_server_send_message(client); |
718e3744 | 203 | } |
204 | ||
205 | /* Interface deletion from zebra daemon. */ | |
b9df2d25 | 206 | /* |
207 | * This function is only called when support for | |
208 | * RTM_IFANNOUNCE or AF_NETLINK sockets (RTM_DELLINK message) | |
209 | * is available. It is not called on Solaris. | |
210 | */ | |
211 | #if (defined(RTM_IFANNOUNCE) || defined(HAVE_NETLINK)) | |
718e3744 | 212 | int |
213 | zsend_interface_delete (struct zserv *client, struct interface *ifp) | |
214 | { | |
215 | struct stream *s; | |
216 | ||
217 | /* Check this client need interface information. */ | |
218 | if (! client->ifinfo) | |
719e9741 | 219 | return 0; |
718e3744 | 220 | |
221 | s = client->obuf; | |
222 | stream_reset (s); | |
223 | ||
224 | /* Packet length placeholder. */ | |
225 | stream_putw (s, 0); | |
226 | ||
227 | /* Interface information. */ | |
228 | stream_putc (s, ZEBRA_INTERFACE_DELETE); | |
229 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); | |
230 | stream_putl (s, ifp->ifindex); | |
2e3b2e47 | 231 | stream_putc (s, ifp->status); |
718e3744 | 232 | stream_putl (s, ifp->flags); |
233 | stream_putl (s, ifp->metric); | |
234 | stream_putl (s, ifp->mtu); | |
b9df2d25 | 235 | stream_putl (s, ifp->mtu6); |
718e3744 | 236 | stream_putl (s, ifp->bandwidth); |
237 | ||
238 | /* Write packet length. */ | |
239 | stream_putw_at (s, 0, stream_get_endp (s)); | |
240 | ||
719e9741 | 241 | return zebra_server_send_message (client); |
718e3744 | 242 | } |
b9df2d25 | 243 | #endif /* (defined(RTM_IFANNOUNCE) || defined(HAVE_LINUX_RTNETLINK_H)) */ |
718e3744 | 244 | |
b9df2d25 | 245 | /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or |
246 | * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. | |
247 | * | |
248 | * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: | |
249 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request | |
250 | * from the client, after the ZEBRA_INTERFACE_ADD has been | |
251 | * sent from zebra to the client | |
252 | * - redistribute new address info to all clients in the following situations | |
253 | * - at startup, when zebra figures out the available interfaces | |
254 | * - when an interface is added (where support for | |
255 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when | |
256 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is | |
257 | * received) | |
258 | * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]" | |
259 | * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" | |
260 | * - when an RTM_NEWADDR message is received from the kernel, | |
261 | * | |
262 | * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: | |
263 | * | |
264 | * zsend_interface_address(DELETE) | |
265 | * ^ | |
266 | * | | |
267 | * zebra_interface_address_delete_update | |
268 | * ^ ^ ^ | |
269 | * | | if_delete_update (not called on | |
270 | * | | Solaris) | |
271 | * ip_address_uninstall connected_delete_ipv4 | |
272 | * [ipv6_addresss_uninstall] [connected_delete_ipv6] | |
273 | * ^ ^ | |
274 | * | | | |
275 | * | RTM_NEWADDR on routing/netlink socket | |
276 | * | | |
277 | * vty commands: | |
278 | * "no ip address A.B.C.D/M [label LINE]" | |
279 | * "no ip address A.B.C.D/M secondary" | |
280 | * ["no ipv6 address X:X::X:X/M"] | |
281 | * | |
282 | */ | |
718e3744 | 283 | int |
b9df2d25 | 284 | zsend_interface_address (int cmd, struct zserv *client, |
285 | struct interface *ifp, struct connected *ifc) | |
718e3744 | 286 | { |
287 | int blen; | |
288 | struct stream *s; | |
289 | struct prefix *p; | |
290 | ||
291 | /* Check this client need interface information. */ | |
292 | if (! client->ifinfo) | |
719e9741 | 293 | return 0; |
718e3744 | 294 | |
295 | s = client->obuf; | |
296 | stream_reset (s); | |
297 | ||
298 | /* Place holder for size. */ | |
299 | stream_putw (s, 0); | |
300 | ||
b9df2d25 | 301 | stream_putc (s, cmd); |
718e3744 | 302 | stream_putl (s, ifp->ifindex); |
303 | ||
304 | /* Interface address flag. */ | |
305 | stream_putc (s, ifc->flags); | |
306 | ||
307 | /* Prefix information. */ | |
308 | p = ifc->address; | |
309 | stream_putc (s, p->family); | |
310 | blen = prefix_blen (p); | |
311 | stream_put (s, &p->u.prefix, blen); | |
b9df2d25 | 312 | |
313 | /* | |
314 | * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE | |
315 | * but zebra_interface_address_delete_read() in the gnu version | |
316 | * expects to find it | |
317 | */ | |
718e3744 | 318 | stream_putc (s, p->prefixlen); |
319 | ||
320 | /* Destination. */ | |
321 | p = ifc->destination; | |
322 | if (p) | |
323 | stream_put (s, &p->u.prefix, blen); | |
324 | else | |
325 | stream_put (s, NULL, blen); | |
326 | ||
327 | /* Write packet size. */ | |
328 | stream_putw_at (s, 0, stream_get_endp (s)); | |
329 | ||
719e9741 | 330 | return zebra_server_send_message(client); |
718e3744 | 331 | } |
332 | ||
b9df2d25 | 333 | /* |
334 | * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or | |
335 | * ZEBRA_INTERFACE_DOWN. | |
336 | * | |
337 | * The ZEBRA_INTERFACE_UP message is sent from the zebra server to | |
338 | * the clients in one of 2 situations: | |
339 | * - an if_up is detected e.g., as a result of an RTM_IFINFO message | |
340 | * - a vty command modifying the bandwidth of an interface is received. | |
341 | * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. | |
342 | */ | |
718e3744 | 343 | int |
b9df2d25 | 344 | zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp) |
718e3744 | 345 | { |
346 | struct stream *s; | |
347 | ||
348 | /* Check this client need interface information. */ | |
349 | if (! client->ifinfo) | |
719e9741 | 350 | return 0; |
718e3744 | 351 | |
352 | s = client->obuf; | |
353 | stream_reset (s); | |
354 | ||
355 | /* Place holder for size. */ | |
356 | stream_putw (s, 0); | |
357 | ||
358 | /* Zebra command. */ | |
b9df2d25 | 359 | stream_putc (s, cmd); |
718e3744 | 360 | |
361 | /* Interface information. */ | |
362 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); | |
363 | stream_putl (s, ifp->ifindex); | |
2e3b2e47 | 364 | stream_putc (s, ifp->status); |
718e3744 | 365 | stream_putl (s, ifp->flags); |
366 | stream_putl (s, ifp->metric); | |
367 | stream_putl (s, ifp->mtu); | |
b9df2d25 | 368 | stream_putl (s, ifp->mtu6); |
718e3744 | 369 | stream_putl (s, ifp->bandwidth); |
370 | ||
371 | /* Write packet size. */ | |
372 | stream_putw_at (s, 0, stream_get_endp (s)); | |
373 | ||
719e9741 | 374 | return zebra_server_send_message(client); |
718e3744 | 375 | } |
376 | ||
b9df2d25 | 377 | /* |
378 | * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a | |
379 | * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following | |
380 | * situations: | |
381 | * - when the client starts up, and requests default information | |
382 | * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the | |
383 | * - case of rip, ripngd, ospfd and ospf6d, when the client sends a | |
384 | * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd, | |
385 | * - when the zebra server redistributes routes after it updates its rib | |
386 | * | |
387 | * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a | |
388 | * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when: | |
389 | * - a "ip route" or "ipv6 route" vty command is issued, a prefix is | |
390 | * - deleted from zebra's rib, and this info | |
391 | * has to be redistributed to the clients | |
392 | * | |
393 | * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the | |
394 | * zebra server when the client wants to tell the zebra server to add a | |
395 | * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the | |
396 | * same message being sent back and forth, this function and | |
397 | * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code | |
398 | * duplication. | |
399 | */ | |
718e3744 | 400 | int |
b9df2d25 | 401 | zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p, |
402 | struct rib *rib) | |
718e3744 | 403 | { |
404 | int psize; | |
405 | struct stream *s; | |
406 | struct nexthop *nexthop; | |
b9df2d25 | 407 | unsigned long nhnummark = 0; |
408 | int nhnum = 0; | |
409 | u_char zapi_flags = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX; | |
410 | ||
718e3744 | 411 | s = client->obuf; |
412 | stream_reset (s); | |
413 | ||
414 | /* Place holder for size. */ | |
415 | stream_putw (s, 0); | |
416 | ||
417 | /* Put command, type and nexthop. */ | |
b9df2d25 | 418 | stream_putc (s, cmd); |
718e3744 | 419 | stream_putc (s, rib->type); |
420 | stream_putc (s, rib->flags); | |
718e3744 | 421 | |
b9df2d25 | 422 | /* |
423 | * XXX no need to set ZAPI_MESSAGE_NEXTHOP if we are going to | |
424 | * send empty nexthop? | |
425 | */ | |
426 | if (cmd == ZEBRA_IPV4_ROUTE_ADD || ZEBRA_IPV6_ROUTE_ADD) | |
427 | zapi_flags |= ZAPI_MESSAGE_METRIC; | |
718e3744 | 428 | |
b9df2d25 | 429 | stream_putc (s, zapi_flags); |
718e3744 | 430 | |
431 | /* Prefix. */ | |
432 | psize = PSIZE (p->prefixlen); | |
433 | stream_putc (s, p->prefixlen); | |
b9df2d25 | 434 | stream_write (s, (u_char *) & p->u.prefix, psize); |
435 | ||
436 | /* | |
437 | * XXX The message format sent by zebra below does not match the format | |
438 | * of the corresponding message expected by the zebra server | |
439 | * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly, | |
440 | * (is there a bug on the client side if more than one segment is sent?) | |
441 | * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX | |
442 | * is hard-coded. | |
443 | */ | |
718e3744 | 444 | /* Nexthop */ |
445 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
446 | { | |
447 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) | |
b9df2d25 | 448 | { |
9985f83c | 449 | nhnummark = stream_get_endp (s); |
b9df2d25 | 450 | stream_putc (s, 1); /* placeholder */ |
451 | nhnum++; | |
452 | ||
453 | switch(nexthop->type) | |
454 | { | |
455 | case NEXTHOP_TYPE_IPV4: | |
456 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
457 | stream_put_in_addr (s, &nexthop->gate.ipv4); | |
458 | break; | |
718e3744 | 459 | #ifdef HAVE_IPV6 |
b9df2d25 | 460 | case NEXTHOP_TYPE_IPV6: |
461 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
462 | case NEXTHOP_TYPE_IPV6_IFNAME: | |
463 | stream_write (s, (u_char *) &nexthop->gate.ipv6, 16); | |
464 | break; | |
465 | #endif | |
466 | default: | |
467 | if (cmd == ZEBRA_IPV4_ROUTE_ADD | |
468 | || cmd == ZEBRA_IPV4_ROUTE_DELETE) | |
469 | { | |
470 | struct in_addr empty; | |
44983cf8 | 471 | memset (&empty, 0, sizeof (struct in_addr)); |
b9df2d25 | 472 | stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN); |
473 | } | |
474 | else | |
475 | { | |
476 | struct in6_addr empty; | |
477 | memset (&empty, 0, sizeof (struct in6_addr)); | |
478 | stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN); | |
479 | } | |
480 | } | |
481 | ||
482 | /* Interface index. */ | |
483 | stream_putc (s, 1); | |
484 | stream_putl (s, nexthop->ifindex); | |
485 | ||
486 | break; | |
487 | } | |
718e3744 | 488 | } |
489 | ||
490 | /* Metric */ | |
491 | stream_putl (s, rib->metric); | |
492 | ||
b9df2d25 | 493 | /* Write next-hop number */ |
494 | if (nhnummark) | |
c1eaa442 | 495 | stream_putc_at (s, nhnummark, nhnum); |
b9df2d25 | 496 | |
718e3744 | 497 | /* Write packet size. */ |
498 | stream_putw_at (s, 0, stream_get_endp (s)); | |
499 | ||
719e9741 | 500 | return zebra_server_send_message(client); |
718e3744 | 501 | } |
502 | ||
b9df2d25 | 503 | #ifdef HAVE_IPV6 |
719e9741 | 504 | static int |
718e3744 | 505 | zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr) |
506 | { | |
507 | struct stream *s; | |
508 | struct rib *rib; | |
509 | unsigned long nump; | |
510 | u_char num; | |
511 | struct nexthop *nexthop; | |
512 | ||
513 | /* Lookup nexthop. */ | |
514 | rib = rib_match_ipv6 (addr); | |
515 | ||
516 | /* Get output stream. */ | |
517 | s = client->obuf; | |
518 | stream_reset (s); | |
519 | ||
520 | /* Fill in result. */ | |
521 | stream_putw (s, 0); | |
522 | stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); | |
523 | stream_put (s, &addr, 16); | |
524 | ||
525 | if (rib) | |
526 | { | |
527 | stream_putl (s, rib->metric); | |
528 | num = 0; | |
9985f83c | 529 | nump = stream_get_endp(s); |
718e3744 | 530 | stream_putc (s, 0); |
531 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
532 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) | |
533 | { | |
534 | stream_putc (s, nexthop->type); | |
535 | switch (nexthop->type) | |
536 | { | |
537 | case ZEBRA_NEXTHOP_IPV6: | |
538 | stream_put (s, &nexthop->gate.ipv6, 16); | |
539 | break; | |
540 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: | |
541 | case ZEBRA_NEXTHOP_IPV6_IFNAME: | |
542 | stream_put (s, &nexthop->gate.ipv6, 16); | |
543 | stream_putl (s, nexthop->ifindex); | |
544 | break; | |
545 | case ZEBRA_NEXTHOP_IFINDEX: | |
546 | case ZEBRA_NEXTHOP_IFNAME: | |
547 | stream_putl (s, nexthop->ifindex); | |
548 | break; | |
fa2b17e3 | 549 | default: |
550 | /* do nothing */ | |
551 | break; | |
718e3744 | 552 | } |
553 | num++; | |
554 | } | |
555 | stream_putc_at (s, nump, num); | |
556 | } | |
557 | else | |
558 | { | |
559 | stream_putl (s, 0); | |
560 | stream_putc (s, 0); | |
561 | } | |
562 | ||
563 | stream_putw_at (s, 0, stream_get_endp (s)); | |
564 | ||
719e9741 | 565 | return zebra_server_send_message(client); |
718e3744 | 566 | } |
567 | #endif /* HAVE_IPV6 */ | |
568 | ||
b9df2d25 | 569 | static int |
718e3744 | 570 | zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr) |
571 | { | |
572 | struct stream *s; | |
573 | struct rib *rib; | |
574 | unsigned long nump; | |
575 | u_char num; | |
576 | struct nexthop *nexthop; | |
577 | ||
578 | /* Lookup nexthop. */ | |
579 | rib = rib_match_ipv4 (addr); | |
580 | ||
581 | /* Get output stream. */ | |
582 | s = client->obuf; | |
583 | stream_reset (s); | |
584 | ||
585 | /* Fill in result. */ | |
586 | stream_putw (s, 0); | |
587 | stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); | |
588 | stream_put_in_addr (s, &addr); | |
589 | ||
590 | if (rib) | |
591 | { | |
592 | stream_putl (s, rib->metric); | |
593 | num = 0; | |
9985f83c | 594 | nump = stream_get_endp(s); |
718e3744 | 595 | stream_putc (s, 0); |
596 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
597 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) | |
598 | { | |
599 | stream_putc (s, nexthop->type); | |
600 | switch (nexthop->type) | |
601 | { | |
602 | case ZEBRA_NEXTHOP_IPV4: | |
603 | stream_put_in_addr (s, &nexthop->gate.ipv4); | |
604 | break; | |
605 | case ZEBRA_NEXTHOP_IFINDEX: | |
606 | case ZEBRA_NEXTHOP_IFNAME: | |
607 | stream_putl (s, nexthop->ifindex); | |
608 | break; | |
fa2b17e3 | 609 | default: |
610 | /* do nothing */ | |
611 | break; | |
718e3744 | 612 | } |
613 | num++; | |
614 | } | |
615 | stream_putc_at (s, nump, num); | |
616 | } | |
617 | else | |
618 | { | |
619 | stream_putl (s, 0); | |
620 | stream_putc (s, 0); | |
621 | } | |
622 | ||
623 | stream_putw_at (s, 0, stream_get_endp (s)); | |
624 | ||
719e9741 | 625 | return zebra_server_send_message(client); |
718e3744 | 626 | } |
627 | ||
b9df2d25 | 628 | static int |
718e3744 | 629 | zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p) |
630 | { | |
631 | struct stream *s; | |
632 | struct rib *rib; | |
633 | unsigned long nump; | |
634 | u_char num; | |
635 | struct nexthop *nexthop; | |
636 | ||
637 | /* Lookup nexthop. */ | |
638 | rib = rib_lookup_ipv4 (p); | |
639 | ||
640 | /* Get output stream. */ | |
641 | s = client->obuf; | |
642 | stream_reset (s); | |
643 | ||
644 | /* Fill in result. */ | |
645 | stream_putw (s, 0); | |
646 | stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP); | |
647 | stream_put_in_addr (s, &p->prefix); | |
648 | ||
649 | if (rib) | |
650 | { | |
651 | stream_putl (s, rib->metric); | |
652 | num = 0; | |
9985f83c | 653 | nump = stream_get_endp(s); |
718e3744 | 654 | stream_putc (s, 0); |
655 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
656 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) | |
657 | { | |
658 | stream_putc (s, nexthop->type); | |
659 | switch (nexthop->type) | |
660 | { | |
661 | case ZEBRA_NEXTHOP_IPV4: | |
662 | stream_put_in_addr (s, &nexthop->gate.ipv4); | |
663 | break; | |
664 | case ZEBRA_NEXTHOP_IFINDEX: | |
665 | case ZEBRA_NEXTHOP_IFNAME: | |
666 | stream_putl (s, nexthop->ifindex); | |
667 | break; | |
fa2b17e3 | 668 | default: |
669 | /* do nothing */ | |
670 | break; | |
718e3744 | 671 | } |
672 | num++; | |
673 | } | |
674 | stream_putc_at (s, nump, num); | |
675 | } | |
676 | else | |
677 | { | |
678 | stream_putl (s, 0); | |
679 | stream_putc (s, 0); | |
680 | } | |
681 | ||
682 | stream_putw_at (s, 0, stream_get_endp (s)); | |
683 | ||
719e9741 | 684 | return zebra_server_send_message(client); |
718e3744 | 685 | } |
686 | \f | |
18a6dce6 | 687 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ |
688 | int | |
689 | zsend_router_id_update (struct zserv *client, struct prefix *p) | |
690 | { | |
691 | struct stream *s; | |
692 | int blen; | |
693 | ||
694 | /* Check this client need interface information. */ | |
695 | if (!client->ridinfo) | |
719e9741 | 696 | return 0; |
18a6dce6 | 697 | |
698 | s = client->obuf; | |
699 | stream_reset (s); | |
700 | ||
701 | /* Place holder for size. */ | |
702 | stream_putw (s, 0); | |
703 | ||
704 | /* Message type. */ | |
705 | stream_putc (s, ZEBRA_ROUTER_ID_UPDATE); | |
706 | ||
707 | /* Prefix information. */ | |
708 | stream_putc (s, p->family); | |
709 | blen = prefix_blen (p); | |
710 | stream_put (s, &p->u.prefix, blen); | |
711 | stream_putc (s, p->prefixlen); | |
712 | ||
713 | /* Write packet size. */ | |
714 | stream_putw_at (s, 0, stream_get_endp (s)); | |
715 | ||
719e9741 | 716 | return zebra_server_send_message(client); |
18a6dce6 | 717 | } |
718 | \f | |
718e3744 | 719 | /* Register zebra server interface information. Send current all |
720 | interface and address information. */ | |
719e9741 | 721 | static int |
718e3744 | 722 | zread_interface_add (struct zserv *client, u_short length) |
723 | { | |
52dc7ee6 | 724 | struct listnode *ifnode; |
725 | struct listnode *cnode; | |
718e3744 | 726 | struct interface *ifp; |
727 | struct connected *c; | |
728 | ||
729 | /* Interface information is needed. */ | |
730 | client->ifinfo = 1; | |
731 | ||
732 | for (ifnode = listhead (iflist); ifnode; ifnode = nextnode (ifnode)) | |
733 | { | |
734 | ifp = getdata (ifnode); | |
735 | ||
736 | /* Skip pseudo interface. */ | |
737 | if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) | |
738 | continue; | |
739 | ||
719e9741 | 740 | if (zsend_interface_add (client, ifp) < 0) |
741 | return -1; | |
718e3744 | 742 | |
743 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) | |
744 | { | |
745 | c = getdata (cnode); | |
719e9741 | 746 | if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) && |
747 | (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, | |
748 | ifp, c) < 0)) | |
749 | return -1; | |
718e3744 | 750 | } |
751 | } | |
719e9741 | 752 | return 0; |
718e3744 | 753 | } |
754 | ||
755 | /* Unregister zebra server interface information. */ | |
719e9741 | 756 | static int |
718e3744 | 757 | zread_interface_delete (struct zserv *client, u_short length) |
758 | { | |
759 | client->ifinfo = 0; | |
719e9741 | 760 | return 0; |
718e3744 | 761 | } |
762 | ||
763 | /* This function support multiple nexthop. */ | |
b9df2d25 | 764 | /* |
765 | * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and | |
766 | * add kernel route. | |
767 | */ | |
719e9741 | 768 | static int |
718e3744 | 769 | zread_ipv4_add (struct zserv *client, u_short length) |
770 | { | |
771 | int i; | |
772 | struct rib *rib; | |
773 | struct prefix_ipv4 p; | |
774 | u_char message; | |
775 | struct in_addr nexthop; | |
776 | u_char nexthop_num; | |
777 | u_char nexthop_type; | |
778 | struct stream *s; | |
779 | unsigned int ifindex; | |
780 | u_char ifname_len; | |
781 | ||
782 | /* Get input stream. */ | |
783 | s = client->ibuf; | |
784 | ||
785 | /* Allocate new rib. */ | |
786 | rib = XMALLOC (MTYPE_RIB, sizeof (struct rib)); | |
787 | memset (rib, 0, sizeof (struct rib)); | |
788 | ||
789 | /* Type, flags, message. */ | |
790 | rib->type = stream_getc (s); | |
791 | rib->flags = stream_getc (s); | |
b9df2d25 | 792 | message = stream_getc (s); |
718e3744 | 793 | rib->uptime = time (NULL); |
794 | ||
795 | /* IPv4 prefix. */ | |
796 | memset (&p, 0, sizeof (struct prefix_ipv4)); | |
797 | p.family = AF_INET; | |
798 | p.prefixlen = stream_getc (s); | |
799 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); | |
800 | ||
801 | /* Nexthop parse. */ | |
802 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) | |
803 | { | |
804 | nexthop_num = stream_getc (s); | |
805 | ||
806 | for (i = 0; i < nexthop_num; i++) | |
807 | { | |
808 | nexthop_type = stream_getc (s); | |
809 | ||
810 | switch (nexthop_type) | |
811 | { | |
812 | case ZEBRA_NEXTHOP_IFINDEX: | |
813 | ifindex = stream_getl (s); | |
814 | nexthop_ifindex_add (rib, ifindex); | |
815 | break; | |
816 | case ZEBRA_NEXTHOP_IFNAME: | |
817 | ifname_len = stream_getc (s); | |
9985f83c | 818 | stream_forward_getp (s, ifname_len); |
718e3744 | 819 | break; |
820 | case ZEBRA_NEXTHOP_IPV4: | |
821 | nexthop.s_addr = stream_get_ipv4 (s); | |
822 | nexthop_ipv4_add (rib, &nexthop); | |
823 | break; | |
824 | case ZEBRA_NEXTHOP_IPV6: | |
9985f83c | 825 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
718e3744 | 826 | break; |
595db7f1 | 827 | case ZEBRA_NEXTHOP_BLACKHOLE: |
828 | nexthop_blackhole_add (rib); | |
829 | break; | |
718e3744 | 830 | } |
831 | } | |
832 | } | |
833 | ||
834 | /* Distance. */ | |
835 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) | |
836 | rib->distance = stream_getc (s); | |
837 | ||
838 | /* Metric. */ | |
839 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) | |
840 | rib->metric = stream_getl (s); | |
841 | ||
842 | rib_add_ipv4_multipath (&p, rib); | |
719e9741 | 843 | return 0; |
718e3744 | 844 | } |
845 | ||
846 | /* Zebra server IPv4 prefix delete function. */ | |
719e9741 | 847 | static int |
718e3744 | 848 | zread_ipv4_delete (struct zserv *client, u_short length) |
849 | { | |
850 | int i; | |
851 | struct stream *s; | |
852 | struct zapi_ipv4 api; | |
853 | struct in_addr nexthop; | |
854 | unsigned long ifindex; | |
855 | struct prefix_ipv4 p; | |
856 | u_char nexthop_num; | |
857 | u_char nexthop_type; | |
858 | u_char ifname_len; | |
859 | ||
860 | s = client->ibuf; | |
861 | ifindex = 0; | |
862 | nexthop.s_addr = 0; | |
863 | ||
864 | /* Type, flags, message. */ | |
865 | api.type = stream_getc (s); | |
866 | api.flags = stream_getc (s); | |
867 | api.message = stream_getc (s); | |
868 | ||
869 | /* IPv4 prefix. */ | |
870 | memset (&p, 0, sizeof (struct prefix_ipv4)); | |
871 | p.family = AF_INET; | |
872 | p.prefixlen = stream_getc (s); | |
873 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); | |
874 | ||
875 | /* Nexthop, ifindex, distance, metric. */ | |
876 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) | |
877 | { | |
878 | nexthop_num = stream_getc (s); | |
879 | ||
880 | for (i = 0; i < nexthop_num; i++) | |
881 | { | |
882 | nexthop_type = stream_getc (s); | |
883 | ||
884 | switch (nexthop_type) | |
885 | { | |
886 | case ZEBRA_NEXTHOP_IFINDEX: | |
887 | ifindex = stream_getl (s); | |
888 | break; | |
889 | case ZEBRA_NEXTHOP_IFNAME: | |
890 | ifname_len = stream_getc (s); | |
9985f83c | 891 | stream_forward_getp (s, ifname_len); |
718e3744 | 892 | break; |
893 | case ZEBRA_NEXTHOP_IPV4: | |
894 | nexthop.s_addr = stream_get_ipv4 (s); | |
895 | break; | |
896 | case ZEBRA_NEXTHOP_IPV6: | |
9985f83c | 897 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
718e3744 | 898 | break; |
899 | } | |
900 | } | |
901 | } | |
902 | ||
903 | /* Distance. */ | |
904 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) | |
905 | api.distance = stream_getc (s); | |
906 | else | |
907 | api.distance = 0; | |
908 | ||
909 | /* Metric. */ | |
910 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) | |
911 | api.metric = stream_getl (s); | |
912 | else | |
913 | api.metric = 0; | |
914 | ||
915 | rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex, | |
916 | client->rtm_table); | |
719e9741 | 917 | return 0; |
718e3744 | 918 | } |
919 | ||
920 | /* Nexthop lookup for IPv4. */ | |
719e9741 | 921 | static int |
718e3744 | 922 | zread_ipv4_nexthop_lookup (struct zserv *client, u_short length) |
923 | { | |
924 | struct in_addr addr; | |
925 | ||
926 | addr.s_addr = stream_get_ipv4 (client->ibuf); | |
719e9741 | 927 | return zsend_ipv4_nexthop_lookup (client, addr); |
718e3744 | 928 | } |
929 | ||
930 | /* Nexthop lookup for IPv4. */ | |
719e9741 | 931 | static int |
718e3744 | 932 | zread_ipv4_import_lookup (struct zserv *client, u_short length) |
933 | { | |
934 | struct prefix_ipv4 p; | |
935 | ||
936 | p.family = AF_INET; | |
937 | p.prefixlen = stream_getc (client->ibuf); | |
938 | p.prefix.s_addr = stream_get_ipv4 (client->ibuf); | |
939 | ||
719e9741 | 940 | return zsend_ipv4_import_lookup (client, &p); |
718e3744 | 941 | } |
942 | ||
943 | #ifdef HAVE_IPV6 | |
944 | /* Zebra server IPv6 prefix add function. */ | |
719e9741 | 945 | static int |
718e3744 | 946 | zread_ipv6_add (struct zserv *client, u_short length) |
947 | { | |
948 | int i; | |
949 | struct stream *s; | |
950 | struct zapi_ipv6 api; | |
951 | struct in6_addr nexthop; | |
952 | unsigned long ifindex; | |
953 | struct prefix_ipv6 p; | |
954 | ||
955 | s = client->ibuf; | |
956 | ifindex = 0; | |
957 | memset (&nexthop, 0, sizeof (struct in6_addr)); | |
958 | ||
959 | /* Type, flags, message. */ | |
960 | api.type = stream_getc (s); | |
961 | api.flags = stream_getc (s); | |
962 | api.message = stream_getc (s); | |
963 | ||
964 | /* IPv4 prefix. */ | |
965 | memset (&p, 0, sizeof (struct prefix_ipv6)); | |
966 | p.family = AF_INET6; | |
967 | p.prefixlen = stream_getc (s); | |
968 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); | |
969 | ||
970 | /* Nexthop, ifindex, distance, metric. */ | |
971 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) | |
972 | { | |
973 | u_char nexthop_type; | |
974 | ||
975 | api.nexthop_num = stream_getc (s); | |
976 | for (i = 0; i < api.nexthop_num; i++) | |
977 | { | |
978 | nexthop_type = stream_getc (s); | |
979 | ||
980 | switch (nexthop_type) | |
981 | { | |
982 | case ZEBRA_NEXTHOP_IPV6: | |
983 | stream_get (&nexthop, s, 16); | |
984 | break; | |
985 | case ZEBRA_NEXTHOP_IFINDEX: | |
986 | ifindex = stream_getl (s); | |
987 | break; | |
988 | } | |
989 | } | |
990 | } | |
991 | ||
992 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) | |
993 | api.distance = stream_getc (s); | |
994 | else | |
995 | api.distance = 0; | |
996 | ||
997 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) | |
998 | api.metric = stream_getl (s); | |
999 | else | |
1000 | api.metric = 0; | |
1001 | ||
1002 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) | |
1003 | rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0); | |
1004 | else | |
1005 | rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0); | |
719e9741 | 1006 | return 0; |
718e3744 | 1007 | } |
1008 | ||
1009 | /* Zebra server IPv6 prefix delete function. */ | |
719e9741 | 1010 | static int |
718e3744 | 1011 | zread_ipv6_delete (struct zserv *client, u_short length) |
1012 | { | |
1013 | int i; | |
1014 | struct stream *s; | |
1015 | struct zapi_ipv6 api; | |
1016 | struct in6_addr nexthop; | |
1017 | unsigned long ifindex; | |
1018 | struct prefix_ipv6 p; | |
1019 | ||
1020 | s = client->ibuf; | |
1021 | ifindex = 0; | |
1022 | memset (&nexthop, 0, sizeof (struct in6_addr)); | |
1023 | ||
1024 | /* Type, flags, message. */ | |
1025 | api.type = stream_getc (s); | |
1026 | api.flags = stream_getc (s); | |
1027 | api.message = stream_getc (s); | |
1028 | ||
1029 | /* IPv4 prefix. */ | |
1030 | memset (&p, 0, sizeof (struct prefix_ipv6)); | |
1031 | p.family = AF_INET6; | |
1032 | p.prefixlen = stream_getc (s); | |
1033 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); | |
1034 | ||
1035 | /* Nexthop, ifindex, distance, metric. */ | |
1036 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) | |
1037 | { | |
1038 | u_char nexthop_type; | |
1039 | ||
1040 | api.nexthop_num = stream_getc (s); | |
1041 | for (i = 0; i < api.nexthop_num; i++) | |
1042 | { | |
1043 | nexthop_type = stream_getc (s); | |
1044 | ||
1045 | switch (nexthop_type) | |
1046 | { | |
1047 | case ZEBRA_NEXTHOP_IPV6: | |
1048 | stream_get (&nexthop, s, 16); | |
1049 | break; | |
1050 | case ZEBRA_NEXTHOP_IFINDEX: | |
1051 | ifindex = stream_getl (s); | |
1052 | break; | |
1053 | } | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) | |
1058 | api.distance = stream_getc (s); | |
1059 | else | |
1060 | api.distance = 0; | |
1061 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) | |
1062 | api.metric = stream_getl (s); | |
1063 | else | |
1064 | api.metric = 0; | |
1065 | ||
1066 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) | |
1067 | rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0); | |
1068 | else | |
1069 | rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0); | |
719e9741 | 1070 | return 0; |
718e3744 | 1071 | } |
1072 | ||
719e9741 | 1073 | static int |
718e3744 | 1074 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length) |
1075 | { | |
1076 | struct in6_addr addr; | |
1077 | char buf[BUFSIZ]; | |
1078 | ||
1079 | stream_get (&addr, client->ibuf, 16); | |
1080 | printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); | |
1081 | ||
719e9741 | 1082 | return zsend_ipv6_nexthop_lookup (client, &addr); |
718e3744 | 1083 | } |
1084 | #endif /* HAVE_IPV6 */ | |
1085 | ||
18a6dce6 | 1086 | /* Register zebra server router-id information. Send current router-id */ |
719e9741 | 1087 | static int |
18a6dce6 | 1088 | zread_router_id_add (struct zserv *client, u_short length) |
1089 | { | |
1090 | struct prefix p; | |
1091 | ||
1092 | /* Router-id information is needed. */ | |
1093 | client->ridinfo = 1; | |
1094 | ||
1095 | router_id_get (&p); | |
1096 | ||
719e9741 | 1097 | return zsend_router_id_update (client,&p); |
18a6dce6 | 1098 | } |
1099 | ||
1100 | /* Unregister zebra server router-id information. */ | |
719e9741 | 1101 | static int |
18a6dce6 | 1102 | zread_router_id_delete (struct zserv *client, u_short length) |
1103 | { | |
1104 | client->ridinfo = 0; | |
719e9741 | 1105 | return 0; |
18a6dce6 | 1106 | } |
1107 | ||
718e3744 | 1108 | /* Close zebra client. */ |
b9df2d25 | 1109 | static void |
718e3744 | 1110 | zebra_client_close (struct zserv *client) |
1111 | { | |
1112 | /* Close file descriptor. */ | |
1113 | if (client->sock) | |
1114 | { | |
1115 | close (client->sock); | |
1116 | client->sock = -1; | |
1117 | } | |
1118 | ||
1119 | /* Free stream buffers. */ | |
1120 | if (client->ibuf) | |
1121 | stream_free (client->ibuf); | |
1122 | if (client->obuf) | |
1123 | stream_free (client->obuf); | |
719e9741 | 1124 | if (client->wb) |
1125 | buffer_free(client->wb); | |
718e3744 | 1126 | |
1127 | /* Release threads. */ | |
1128 | if (client->t_read) | |
1129 | thread_cancel (client->t_read); | |
1130 | if (client->t_write) | |
1131 | thread_cancel (client->t_write); | |
719e9741 | 1132 | if (client->t_suicide) |
1133 | thread_cancel (client->t_suicide); | |
718e3744 | 1134 | |
1135 | /* Free client structure. */ | |
b21b19c5 | 1136 | listnode_delete (zebrad.client_list, client); |
718e3744 | 1137 | XFREE (0, client); |
1138 | } | |
1139 | ||
1140 | /* Make new client. */ | |
b9df2d25 | 1141 | static void |
718e3744 | 1142 | zebra_client_create (int sock) |
1143 | { | |
1144 | struct zserv *client; | |
1145 | ||
1146 | client = XCALLOC (0, sizeof (struct zserv)); | |
1147 | ||
1148 | /* Make client input/output buffer. */ | |
1149 | client->sock = sock; | |
1150 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
1151 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
719e9741 | 1152 | client->wb = buffer_new(0); |
718e3744 | 1153 | |
1154 | /* Set table number. */ | |
b21b19c5 | 1155 | client->rtm_table = zebrad.rtm_table_default; |
718e3744 | 1156 | |
1157 | /* Add this client to linked list. */ | |
b21b19c5 | 1158 | listnode_add (zebrad.client_list, client); |
718e3744 | 1159 | |
1160 | /* Make new read thread. */ | |
1161 | zebra_event (ZEBRA_READ, sock, client); | |
1162 | } | |
1163 | ||
1164 | /* Handler of zebra service request. */ | |
b9df2d25 | 1165 | static int |
718e3744 | 1166 | zebra_client_read (struct thread *thread) |
1167 | { | |
1168 | int sock; | |
1169 | struct zserv *client; | |
1170 | int nbyte; | |
1171 | u_short length; | |
1172 | u_char command; | |
1173 | ||
1174 | /* Get thread data. Reset reading thread because I'm running. */ | |
1175 | sock = THREAD_FD (thread); | |
1176 | client = THREAD_ARG (thread); | |
1177 | client->t_read = NULL; | |
1178 | ||
719e9741 | 1179 | if (client->t_suicide) |
718e3744 | 1180 | { |
719e9741 | 1181 | zebra_client_close(client); |
718e3744 | 1182 | return -1; |
1183 | } | |
719e9741 | 1184 | |
1185 | /* Read length and command (if we don't have it already). */ | |
1186 | if (stream_get_endp(client->ibuf) < ZEBRA_HEADER_SIZE) | |
1187 | { | |
1188 | if (((nbyte = stream_read_try (client->ibuf, sock, | |
1189 | ZEBRA_HEADER_SIZE)) == 0) || | |
1190 | (nbyte == -1)) | |
1191 | { | |
1192 | if (IS_ZEBRA_DEBUG_EVENT) | |
1193 | zlog_debug ("connection closed socket [%d]", sock); | |
1194 | zebra_client_close (client); | |
1195 | return -1; | |
1196 | } | |
1197 | if (stream_get_endp(client->ibuf) < ZEBRA_HEADER_SIZE) | |
1198 | { | |
1199 | /* Try again later. */ | |
1200 | zebra_event (ZEBRA_READ, sock, client); | |
1201 | return 0; | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | /* Reset to read from the beginning of the incoming packet. */ | |
1206 | stream_set_getp(client->ibuf, 0); | |
1207 | ||
718e3744 | 1208 | length = stream_getw (client->ibuf); |
1209 | command = stream_getc (client->ibuf); | |
1210 | ||
719e9741 | 1211 | if (length < ZEBRA_HEADER_SIZE) |
718e3744 | 1212 | { |
1213 | if (IS_ZEBRA_DEBUG_EVENT) | |
719e9741 | 1214 | zlog_debug ("length %d is less than %d ", length, ZEBRA_HEADER_SIZE); |
718e3744 | 1215 | zebra_client_close (client); |
1216 | return -1; | |
1217 | } | |
1218 | ||
718e3744 | 1219 | /* Read rest of data. */ |
719e9741 | 1220 | if (stream_get_endp(client->ibuf) < length) |
718e3744 | 1221 | { |
719e9741 | 1222 | nbyte = stream_read_try (client->ibuf, sock, |
1223 | length-stream_get_endp(client->ibuf)); | |
1224 | if ((nbyte == 0) || (nbyte == -1)) | |
718e3744 | 1225 | { |
1226 | if (IS_ZEBRA_DEBUG_EVENT) | |
b6178002 | 1227 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
718e3744 | 1228 | zebra_client_close (client); |
1229 | return -1; | |
1230 | } | |
719e9741 | 1231 | if (stream_get_endp(client->ibuf) < length) |
1232 | { | |
1233 | /* Try again later. */ | |
1234 | zebra_event (ZEBRA_READ, sock, client); | |
1235 | return 0; | |
1236 | } | |
718e3744 | 1237 | } |
1238 | ||
719e9741 | 1239 | length -= ZEBRA_HEADER_SIZE; |
1240 | ||
718e3744 | 1241 | /* Debug packet information. */ |
1242 | if (IS_ZEBRA_DEBUG_EVENT) | |
b6178002 | 1243 | zlog_debug ("zebra message comes from socket [%d]", sock); |
718e3744 | 1244 | |
1245 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) | |
b6178002 | 1246 | zlog_debug ("zebra message received [%s] %d", |
718e3744 | 1247 | zebra_command_str[command], length); |
1248 | ||
1249 | switch (command) | |
1250 | { | |
18a6dce6 | 1251 | case ZEBRA_ROUTER_ID_ADD: |
1252 | zread_router_id_add (client, length); | |
1253 | break; | |
1254 | case ZEBRA_ROUTER_ID_DELETE: | |
1255 | zread_router_id_delete (client, length); | |
1256 | break; | |
718e3744 | 1257 | case ZEBRA_INTERFACE_ADD: |
1258 | zread_interface_add (client, length); | |
1259 | break; | |
1260 | case ZEBRA_INTERFACE_DELETE: | |
1261 | zread_interface_delete (client, length); | |
1262 | break; | |
1263 | case ZEBRA_IPV4_ROUTE_ADD: | |
1264 | zread_ipv4_add (client, length); | |
1265 | break; | |
1266 | case ZEBRA_IPV4_ROUTE_DELETE: | |
1267 | zread_ipv4_delete (client, length); | |
1268 | break; | |
1269 | #ifdef HAVE_IPV6 | |
1270 | case ZEBRA_IPV6_ROUTE_ADD: | |
1271 | zread_ipv6_add (client, length); | |
1272 | break; | |
1273 | case ZEBRA_IPV6_ROUTE_DELETE: | |
1274 | zread_ipv6_delete (client, length); | |
1275 | break; | |
1276 | #endif /* HAVE_IPV6 */ | |
1277 | case ZEBRA_REDISTRIBUTE_ADD: | |
1278 | zebra_redistribute_add (command, client, length); | |
1279 | break; | |
1280 | case ZEBRA_REDISTRIBUTE_DELETE: | |
1281 | zebra_redistribute_delete (command, client, length); | |
1282 | break; | |
1283 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: | |
1284 | zebra_redistribute_default_add (command, client, length); | |
1285 | break; | |
1286 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: | |
1287 | zebra_redistribute_default_delete (command, client, length); | |
1288 | break; | |
1289 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: | |
1290 | zread_ipv4_nexthop_lookup (client, length); | |
1291 | break; | |
1292 | #ifdef HAVE_IPV6 | |
1293 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: | |
1294 | zread_ipv6_nexthop_lookup (client, length); | |
1295 | break; | |
1296 | #endif /* HAVE_IPV6 */ | |
1297 | case ZEBRA_IPV4_IMPORT_LOOKUP: | |
1298 | zread_ipv4_import_lookup (client, length); | |
1299 | break; | |
1300 | default: | |
1301 | zlog_info ("Zebra received unknown command %d", command); | |
1302 | break; | |
1303 | } | |
1304 | ||
719e9741 | 1305 | if (client->t_suicide) |
1306 | { | |
1307 | /* No need to wait for thread callback, just kill immediately. */ | |
1308 | zebra_client_close(client); | |
1309 | return -1; | |
1310 | } | |
1311 | ||
718e3744 | 1312 | stream_reset (client->ibuf); |
1313 | zebra_event (ZEBRA_READ, sock, client); | |
718e3744 | 1314 | return 0; |
1315 | } | |
1316 | ||
718e3744 | 1317 | |
1318 | /* Accept code of zebra server socket. */ | |
b9df2d25 | 1319 | static int |
718e3744 | 1320 | zebra_accept (struct thread *thread) |
1321 | { | |
1322 | int accept_sock; | |
1323 | int client_sock; | |
1324 | struct sockaddr_in client; | |
1325 | socklen_t len; | |
1326 | ||
1327 | accept_sock = THREAD_FD (thread); | |
1328 | ||
719e9741 | 1329 | /* Reregister myself. */ |
1330 | zebra_event (ZEBRA_SERV, accept_sock, NULL); | |
1331 | ||
718e3744 | 1332 | len = sizeof (struct sockaddr_in); |
1333 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); | |
1334 | ||
1335 | if (client_sock < 0) | |
1336 | { | |
6099b3b5 | 1337 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
718e3744 | 1338 | return -1; |
1339 | } | |
1340 | ||
ccf3557b | 1341 | /* Make client socket non-blocking. */ |
719e9741 | 1342 | set_nonblocking(client_sock); |
865b852c | 1343 | |
718e3744 | 1344 | /* Create new zebra client. */ |
1345 | zebra_client_create (client_sock); | |
1346 | ||
718e3744 | 1347 | return 0; |
1348 | } | |
1349 | ||
b9df2d25 | 1350 | #ifdef HAVE_TCP_ZEBRA |
718e3744 | 1351 | /* Make zebra's server socket. */ |
b9df2d25 | 1352 | static void |
718e3744 | 1353 | zebra_serv () |
1354 | { | |
1355 | int ret; | |
1356 | int accept_sock; | |
1357 | struct sockaddr_in addr; | |
1358 | ||
1359 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); | |
1360 | ||
1361 | if (accept_sock < 0) | |
1362 | { | |
6099b3b5 | 1363 | zlog_warn ("Can't bind to socket: %s", safe_strerror (errno)); |
718e3744 | 1364 | zlog_warn ("zebra can't provice full functionality due to above error"); |
1365 | return; | |
1366 | } | |
1367 | ||
1368 | memset (&addr, 0, sizeof (struct sockaddr_in)); | |
1369 | addr.sin_family = AF_INET; | |
1370 | addr.sin_port = htons (ZEBRA_PORT); | |
1371 | #ifdef HAVE_SIN_LEN | |
1372 | addr.sin_len = sizeof (struct sockaddr_in); | |
1373 | #endif /* HAVE_SIN_LEN */ | |
1374 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | |
1375 | ||
1376 | sockopt_reuseaddr (accept_sock); | |
1377 | sockopt_reuseport (accept_sock); | |
1378 | ||
edd7c245 | 1379 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
1380 | zlog (NULL, LOG_ERR, "Can't raise privileges"); | |
1381 | ||
718e3744 | 1382 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
1383 | sizeof (struct sockaddr_in)); | |
1384 | if (ret < 0) | |
1385 | { | |
6099b3b5 | 1386 | zlog_warn ("Can't bind to socket: %s", safe_strerror (errno)); |
718e3744 | 1387 | zlog_warn ("zebra can't provice full functionality due to above error"); |
1388 | close (accept_sock); /* Avoid sd leak. */ | |
1389 | return; | |
1390 | } | |
edd7c245 | 1391 | |
1392 | if ( zserv_privs.change(ZPRIVS_LOWER) ) | |
1393 | zlog (NULL, LOG_ERR, "Can't lower privileges"); | |
718e3744 | 1394 | |
1395 | ret = listen (accept_sock, 1); | |
1396 | if (ret < 0) | |
1397 | { | |
6099b3b5 | 1398 | zlog_warn ("Can't listen to socket: %s", safe_strerror (errno)); |
718e3744 | 1399 | zlog_warn ("zebra can't provice full functionality due to above error"); |
1400 | close (accept_sock); /* Avoid sd leak. */ | |
1401 | return; | |
1402 | } | |
1403 | ||
1404 | zebra_event (ZEBRA_SERV, accept_sock, NULL); | |
1405 | } | |
b9df2d25 | 1406 | #endif /* HAVE_TCP_ZEBRA */ |
718e3744 | 1407 | |
1408 | /* For sockaddr_un. */ | |
1409 | #include <sys/un.h> | |
1410 | ||
1411 | /* zebra server UNIX domain socket. */ | |
b9df2d25 | 1412 | static void |
fce954f8 | 1413 | zebra_serv_un (const char *path) |
718e3744 | 1414 | { |
1415 | int ret; | |
1416 | int sock, len; | |
1417 | struct sockaddr_un serv; | |
1418 | mode_t old_mask; | |
1419 | ||
1420 | /* First of all, unlink existing socket */ | |
1421 | unlink (path); | |
1422 | ||
1423 | /* Set umask */ | |
1424 | old_mask = umask (0077); | |
1425 | ||
1426 | /* Make UNIX domain socket. */ | |
1427 | sock = socket (AF_UNIX, SOCK_STREAM, 0); | |
1428 | if (sock < 0) | |
1429 | { | |
1430 | perror ("sock"); | |
1431 | return; | |
1432 | } | |
1433 | ||
1434 | /* Make server socket. */ | |
1435 | memset (&serv, 0, sizeof (struct sockaddr_un)); | |
1436 | serv.sun_family = AF_UNIX; | |
1437 | strncpy (serv.sun_path, path, strlen (path)); | |
1438 | #ifdef HAVE_SUN_LEN | |
1439 | len = serv.sun_len = SUN_LEN(&serv); | |
1440 | #else | |
1441 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); | |
1442 | #endif /* HAVE_SUN_LEN */ | |
1443 | ||
1444 | ret = bind (sock, (struct sockaddr *) &serv, len); | |
1445 | if (ret < 0) | |
1446 | { | |
1447 | perror ("bind"); | |
1448 | close (sock); | |
1449 | return; | |
1450 | } | |
1451 | ||
1452 | ret = listen (sock, 5); | |
1453 | if (ret < 0) | |
1454 | { | |
1455 | perror ("listen"); | |
1456 | close (sock); | |
1457 | return; | |
1458 | } | |
1459 | ||
1460 | umask (old_mask); | |
1461 | ||
1462 | zebra_event (ZEBRA_SERV, sock, NULL); | |
1463 | } | |
1464 | \f | |
718e3744 | 1465 | |
b9df2d25 | 1466 | static void |
718e3744 | 1467 | zebra_event (enum event event, int sock, struct zserv *client) |
1468 | { | |
1469 | switch (event) | |
1470 | { | |
1471 | case ZEBRA_SERV: | |
b21b19c5 | 1472 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
718e3744 | 1473 | break; |
1474 | case ZEBRA_READ: | |
1475 | client->t_read = | |
b21b19c5 | 1476 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
718e3744 | 1477 | break; |
1478 | case ZEBRA_WRITE: | |
1479 | /**/ | |
1480 | break; | |
1481 | } | |
1482 | } | |
1483 | \f | |
1484 | /* Display default rtm_table for all clients. */ | |
1485 | DEFUN (show_table, | |
1486 | show_table_cmd, | |
1487 | "show table", | |
1488 | SHOW_STR | |
1489 | "default routing table to use for all clients\n") | |
1490 | { | |
b21b19c5 | 1491 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
718e3744 | 1492 | VTY_NEWLINE); |
1493 | return CMD_SUCCESS; | |
1494 | } | |
1495 | ||
1496 | DEFUN (config_table, | |
1497 | config_table_cmd, | |
1498 | "table TABLENO", | |
1499 | "Configure target kernel routing table\n" | |
1500 | "TABLE integer\n") | |
1501 | { | |
b21b19c5 | 1502 | zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); |
718e3744 | 1503 | return CMD_SUCCESS; |
1504 | } | |
1505 | ||
647e4f1f | 1506 | DEFUN (ip_forwarding, |
1507 | ip_forwarding_cmd, | |
1508 | "ip forwarding", | |
1509 | IP_STR | |
1510 | "Turn on IP forwarding") | |
1511 | { | |
1512 | int ret; | |
1513 | ||
1514 | ret = ipforward (); | |
b71f00f2 | 1515 | if (ret == 0) |
1516 | ret = ipforward_on (); | |
647e4f1f | 1517 | |
647e4f1f | 1518 | if (ret == 0) |
1519 | { | |
1520 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); | |
1521 | return CMD_WARNING; | |
1522 | } | |
1523 | ||
1524 | return CMD_SUCCESS; | |
1525 | } | |
1526 | ||
718e3744 | 1527 | DEFUN (no_ip_forwarding, |
1528 | no_ip_forwarding_cmd, | |
1529 | "no ip forwarding", | |
1530 | NO_STR | |
1531 | IP_STR | |
1532 | "Turn off IP forwarding") | |
1533 | { | |
1534 | int ret; | |
1535 | ||
1536 | ret = ipforward (); | |
b71f00f2 | 1537 | if (ret != 0) |
1538 | ret = ipforward_off (); | |
718e3744 | 1539 | |
718e3744 | 1540 | if (ret != 0) |
1541 | { | |
1542 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); | |
1543 | return CMD_WARNING; | |
1544 | } | |
1545 | ||
1546 | return CMD_SUCCESS; | |
1547 | } | |
1548 | ||
1549 | /* This command is for debugging purpose. */ | |
1550 | DEFUN (show_zebra_client, | |
1551 | show_zebra_client_cmd, | |
1552 | "show zebra client", | |
1553 | SHOW_STR | |
1554 | "Zebra information" | |
1555 | "Client information") | |
1556 | { | |
52dc7ee6 | 1557 | struct listnode *node; |
718e3744 | 1558 | struct zserv *client; |
1559 | ||
b21b19c5 | 1560 | for (node = listhead (zebrad.client_list); node; nextnode (node)) |
718e3744 | 1561 | { |
1562 | client = getdata (node); | |
1563 | vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); | |
1564 | } | |
1565 | return CMD_SUCCESS; | |
1566 | } | |
1567 | ||
1568 | /* Table configuration write function. */ | |
b9df2d25 | 1569 | static int |
718e3744 | 1570 | config_write_table (struct vty *vty) |
1571 | { | |
b21b19c5 | 1572 | if (zebrad.rtm_table_default) |
1573 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, | |
718e3744 | 1574 | VTY_NEWLINE); |
1575 | return 0; | |
1576 | } | |
1577 | ||
1578 | /* table node for routing tables. */ | |
1579 | struct cmd_node table_node = | |
1580 | { | |
1581 | TABLE_NODE, | |
1582 | "", /* This node has no interface. */ | |
1583 | 1 | |
1584 | }; | |
1585 | \f | |
1586 | /* Only display ip forwarding is enabled or not. */ | |
1587 | DEFUN (show_ip_forwarding, | |
1588 | show_ip_forwarding_cmd, | |
1589 | "show ip forwarding", | |
1590 | SHOW_STR | |
1591 | IP_STR | |
1592 | "IP forwarding status\n") | |
1593 | { | |
1594 | int ret; | |
1595 | ||
1596 | ret = ipforward (); | |
1597 | ||
1598 | if (ret == 0) | |
1599 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); | |
1600 | else | |
1601 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); | |
1602 | return CMD_SUCCESS; | |
1603 | } | |
1604 | ||
1605 | #ifdef HAVE_IPV6 | |
1606 | /* Only display ipv6 forwarding is enabled or not. */ | |
1607 | DEFUN (show_ipv6_forwarding, | |
1608 | show_ipv6_forwarding_cmd, | |
1609 | "show ipv6 forwarding", | |
1610 | SHOW_STR | |
1611 | "IPv6 information\n" | |
1612 | "Forwarding status\n") | |
1613 | { | |
1614 | int ret; | |
1615 | ||
1616 | ret = ipforward_ipv6 (); | |
1617 | ||
1618 | switch (ret) | |
1619 | { | |
1620 | case -1: | |
1621 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); | |
1622 | break; | |
1623 | case 0: | |
1624 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); | |
1625 | break; | |
1626 | case 1: | |
1627 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); | |
1628 | break; | |
1629 | default: | |
1630 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); | |
1631 | break; | |
1632 | } | |
1633 | return CMD_SUCCESS; | |
1634 | } | |
1635 | ||
55906724 | 1636 | DEFUN (ipv6_forwarding, |
1637 | ipv6_forwarding_cmd, | |
1638 | "ipv6 forwarding", | |
1639 | IPV6_STR | |
1640 | "Turn on IPv6 forwarding") | |
1641 | { | |
1642 | int ret; | |
1643 | ||
41d3fc96 | 1644 | ret = ipforward_ipv6 (); |
b71f00f2 | 1645 | if (ret == 0) |
1646 | ret = ipforward_ipv6_on (); | |
41d3fc96 | 1647 | |
41d3fc96 | 1648 | if (ret == 0) |
55906724 | 1649 | { |
1650 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); | |
1651 | return CMD_WARNING; | |
1652 | } | |
1653 | ||
1654 | return CMD_SUCCESS; | |
1655 | } | |
1656 | ||
718e3744 | 1657 | DEFUN (no_ipv6_forwarding, |
1658 | no_ipv6_forwarding_cmd, | |
1659 | "no ipv6 forwarding", | |
1660 | NO_STR | |
55906724 | 1661 | IPV6_STR |
1662 | "Turn off IPv6 forwarding") | |
718e3744 | 1663 | { |
1664 | int ret; | |
1665 | ||
41d3fc96 | 1666 | ret = ipforward_ipv6 (); |
b71f00f2 | 1667 | if (ret != 0) |
1668 | ret = ipforward_ipv6_off (); | |
41d3fc96 | 1669 | |
718e3744 | 1670 | if (ret != 0) |
1671 | { | |
1672 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); | |
1673 | return CMD_WARNING; | |
1674 | } | |
1675 | ||
1676 | return CMD_SUCCESS; | |
1677 | } | |
1678 | ||
1679 | #endif /* HAVE_IPV6 */ | |
1680 | ||
1681 | /* IPForwarding configuration write function. */ | |
719e9741 | 1682 | static int |
718e3744 | 1683 | config_write_forwarding (struct vty *vty) |
1684 | { | |
18a6dce6 | 1685 | /* FIXME: Find better place for that. */ |
1686 | router_id_write (vty); | |
1687 | ||
3e0b3a56 | 1688 | if (ipforward ()) |
1689 | vty_out (vty, "ip forwarding%s", VTY_NEWLINE); | |
718e3744 | 1690 | #ifdef HAVE_IPV6 |
3e0b3a56 | 1691 | if (ipforward_ipv6 ()) |
1692 | vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); | |
718e3744 | 1693 | #endif /* HAVE_IPV6 */ |
1694 | vty_out (vty, "!%s", VTY_NEWLINE); | |
1695 | return 0; | |
1696 | } | |
1697 | ||
1698 | /* table node for routing tables. */ | |
1699 | struct cmd_node forwarding_node = | |
1700 | { | |
1701 | FORWARDING_NODE, | |
1702 | "", /* This node has no interface. */ | |
1703 | 1 | |
1704 | }; | |
1705 | ||
1706 | \f | |
1707 | /* Initialisation of zebra and installation of commands. */ | |
1708 | void | |
1709 | zebra_init () | |
1710 | { | |
1711 | /* Client list init. */ | |
b21b19c5 | 1712 | zebrad.client_list = list_new (); |
718e3744 | 1713 | |
718e3744 | 1714 | /* Make zebra server socket. */ |
1715 | #ifdef HAVE_TCP_ZEBRA | |
1716 | zebra_serv (); | |
1717 | #else | |
1718 | zebra_serv_un (ZEBRA_SERV_PATH); | |
1719 | #endif /* HAVE_TCP_ZEBRA */ | |
1720 | ||
1721 | /* Install configuration write function. */ | |
1722 | install_node (&table_node, config_write_table); | |
1723 | install_node (&forwarding_node, config_write_forwarding); | |
1724 | ||
1725 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); | |
1726 | install_element (ENABLE_NODE, &show_ip_forwarding_cmd); | |
647e4f1f | 1727 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
718e3744 | 1728 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
1729 | install_element (ENABLE_NODE, &show_zebra_client_cmd); | |
1730 | ||
1731 | #ifdef HAVE_NETLINK | |
1732 | install_element (VIEW_NODE, &show_table_cmd); | |
1733 | install_element (ENABLE_NODE, &show_table_cmd); | |
1734 | install_element (CONFIG_NODE, &config_table_cmd); | |
1735 | #endif /* HAVE_NETLINK */ | |
1736 | ||
1737 | #ifdef HAVE_IPV6 | |
1738 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); | |
1739 | install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd); | |
55906724 | 1740 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
718e3744 | 1741 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
1742 | #endif /* HAVE_IPV6 */ | |
1743 | } |