📄 rip_zebra.c
字号:
/* RIPd and zebra interface. * Copyright (C) 1997, 1999 Kunihiro Ishiguro <kunihiro@zebra.org> * * This file is part of GNU Zebra. * * GNU Zebra is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * GNU Zebra is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Zebra; see the file COPYING. If not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */#include <zebra.h>#include "command.h"#include "prefix.h"#include "stream.h"#include "routemap.h"#include "zclient.h"#include "log.h"#include "ripd/ripd.h"#include "ripd/rip_debug.h"/* All information about zebra. */struct zclient *zclient = NULL;/* Callback prototypes for zebra client service. */int rip_interface_add (int, struct zclient *, zebra_size_t);int rip_interface_delete (int, struct zclient *, zebra_size_t);int rip_interface_address_add (int, struct zclient *, zebra_size_t);int rip_interface_address_delete (int, struct zclient *, zebra_size_t);int rip_interface_up (int, struct zclient *, zebra_size_t);int rip_interface_down (int, struct zclient *, zebra_size_t);/* RIPd to zebra command interface. */voidrip_zebra_ipv4_add (struct prefix_ipv4 *p, struct in_addr *nexthop, u_int32_t metric, u_char distance){ struct zapi_ipv4 api; if (zclient->redist[ZEBRA_ROUTE_RIP]) { api.type = ZEBRA_ROUTE_RIP; api.flags = 0; api.message = 0; SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); api.nexthop_num = 1; api.nexthop = &nexthop; api.ifindex_num = 0; SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); api.metric = metric; if (distance && distance != ZEBRA_RIP_DISTANCE_DEFAULT) { SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); api.distance = distance; } zapi_ipv4_add (zclient, p, &api); rip_global_route_changes++; }}voidrip_zebra_ipv4_delete (struct prefix_ipv4 *p, struct in_addr *nexthop, u_int32_t metric){ struct zapi_ipv4 api; if (zclient->redist[ZEBRA_ROUTE_RIP]) { api.type = ZEBRA_ROUTE_RIP; api.flags = 0; api.message = 0; SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); api.nexthop_num = 1; api.nexthop = &nexthop; api.ifindex_num = 0; SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); api.metric = metric; zapi_ipv4_delete (zclient, p, &api); rip_global_route_changes++; }}/* Zebra route add and delete treatment. */intrip_zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length){ struct stream *s; struct zapi_ipv4 api; unsigned long ifindex; struct in_addr nexthop; struct prefix_ipv4 p; s = zclient->ibuf; ifindex = 0; nexthop.s_addr = 0; /* Type, flags, message. */ api.type = stream_getc (s); api.flags = stream_getc (s); api.message = stream_getc (s); /* IPv4 prefix. */ memset (&p, 0, sizeof (struct prefix_ipv4)); p.family = AF_INET; p.prefixlen = stream_getc (s); stream_get (&p.prefix, s, PSIZE (p.prefixlen)); /* Nexthop, ifindex, distance, metric. */ if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) { api.nexthop_num = stream_getc (s); nexthop.s_addr = stream_get_ipv4 (s); } if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) { api.ifindex_num = stream_getc (s); ifindex = stream_getl (s); } if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) api.distance = stream_getc (s); if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) api.metric = stream_getl (s); /* Then fetch IPv4 prefixes. */ if (command == ZEBRA_IPV4_ROUTE_ADD) rip_redistribute_add (api.type, RIP_ROUTE_REDISTRIBUTE, &p, ifindex, &nexthop); else rip_redistribute_delete (api.type, RIP_ROUTE_REDISTRIBUTE, &p, ifindex); return 0;}voidrip_zclient_reset (){ zclient_reset (zclient);}/* RIP route-map set for redistribution */voidrip_routemap_set (int type, char *name){ if (rip->route_map[type].name) free(rip->route_map[type].name); rip->route_map[type].name = strdup (name); rip->route_map[type].map = route_map_lookup_by_name (name);}voidrip_redistribute_metric_set (int type, int metric){ rip->route_map[type].metric_config = 1; rip->route_map[type].metric = metric;}intrip_metric_unset (int type,int metric){#define DONT_CARE_METRIC_RIP 17 if (metric != DONT_CARE_METRIC_RIP && rip->route_map[type].metric != metric) return 1; rip->route_map[type].metric_config = 0; rip->route_map[type].metric = 0; return 0;}/* RIP route-map unset for redistribution */intrip_routemap_unset (int type,char *name){ if (! rip->route_map[type].name || (name != NULL && strcmp(rip->route_map[type].name,name))) return 1; free (rip->route_map[type].name); rip->route_map[type].name = NULL; rip->route_map[type].map = NULL; return 0;}/* Redistribution types */static struct { int type; int str_min_len; char *str;} redist_type[] = { {ZEBRA_ROUTE_KERNEL, 1, "kernel"}, {ZEBRA_ROUTE_CONNECT, 1, "connected"}, {ZEBRA_ROUTE_STATIC, 1, "static"}, {ZEBRA_ROUTE_OSPF, 1, "ospf"}, {ZEBRA_ROUTE_BGP, 1, "bgp"}, {0, 0, NULL}};DEFUN (router_zebra, router_zebra_cmd, "router zebra", "Enable a routing process\n" "Make connection to zebra daemon\n"){ vty->node = ZEBRA_NODE; zclient->enable = 1; zclient_start (zclient); return CMD_SUCCESS;}DEFUN (no_router_zebra, no_router_zebra_cmd, "no router zebra", NO_STR "Enable a routing process\n" "Make connection to zebra daemon\n"){ zclient->enable = 0; zclient_stop (zclient); return CMD_SUCCESS;}intrip_redistribute_set (int type){ if (zclient->redist[type]) return CMD_SUCCESS; zclient->redist[type] = 1; if (zclient->sock > 0) zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient->sock, type); return CMD_SUCCESS;}intrip_redistribute_unset (int type){ if (! zclient->redist[type]) return CMD_SUCCESS; zclient->redist[type] = 0; if (zclient->sock > 0) zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient->sock, type); /* Remove the routes from RIP table. */ rip_redistribute_withdraw (type); return CMD_SUCCESS;}intrip_redistribute_check (int type){ return (zclient->redist[type]);}voidrip_redistribute_clean (){ int i; for (i = 0; redist_type[i].str; i++) { if (zclient->redist[redist_type[i].type]) { if (zclient->sock > 0) zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient->sock, redist_type[i].type); zclient->redist[redist_type[i].type] = 0; /* Remove the routes from RIP table. */ rip_redistribute_withdraw (redist_type[i].type); } }}DEFUN (rip_redistribute_rip, rip_redistribute_rip_cmd, "redistribute rip", "Redistribute information from another routing protocol\n" "Routing Information Protocol (RIP)\n"){ zclient->redist[ZEBRA_ROUTE_RIP] = 1; return CMD_SUCCESS;}DEFUN (no_rip_redistribute_rip, no_rip_redistribute_rip_cmd, "no redistribute rip", NO_STR "Redistribute information from another routing protocol\n" "Routing Information Protocol (RIP)\n"){ zclient->redist[ZEBRA_ROUTE_RIP] = 0; return CMD_SUCCESS;}DEFUN (rip_redistribute_type, rip_redistribute_type_cmd, "redistribute (kernel|connected|static|ospf|bgp)", "Redistribute information from another routing protocol\n" "Kernel routes\n" "Connected\n" "Static routes\n" "Open Shortest Path First (OSPF)\n" "Border Gateway Protocol (BGP)\n"){ int i; for(i = 0; redist_type[i].str; i++) { if (strncmp (redist_type[i].str, argv[0], redist_type[i].str_min_len) == 0) { zclient_redistribute_set (zclient, redist_type[i].type); return CMD_SUCCESS; } } vty_out(vty, "Invalid type %s%s", argv[0], VTY_NEWLINE); return CMD_WARNING;}DEFUN (no_rip_redistribute_type, no_rip_redistribute_type_cmd,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -