blob: 51873335de5718c1330bc12dcf5446e079f5dbfb [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
hasso60bb6a42004-08-26 11:22:19 +00002 * $Id: bgp_view.c,v 1.2 2004/08/26 11:22:19 hasso Exp $
paul718e3742002-12-13 20:15:29 +00003 *
4 * Multiple view function for route server.
5 * Copyright (C) 1997 Kunihiro Ishiguro
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
24
25#include <zebra.h>
26
27#include "linklist.h"
28#include "vector.h"
29#include "vty.h"
30#include "command.h"
31#include "prefix.h"
paul718e3742002-12-13 20:15:29 +000032#include "table.h"
33#include "log.h"
34
35#include "bgpd/bgpd.h"
36#include "bgpd/bgp_route.h"
37#include "bgpd/bgp_attr.h"
38#include "bgpd/bgp_dump.h"
39#include "bgpd/bgp_aspath.h"
40
41/* Static configuration of BGP annoucement. */
42struct route_table *bgp_static_ipv4;
43#ifdef HAVE_IPV6
44struct route_table *bgp_static_ipv6;
45#endif /* HAVE_IPV6 */
46
47/* Static annoucement peer. */
48struct peer *static_peer;
49
50/* Default value setting flag */
51#define VAL_LOCAL_PREF 0x01
52#define VAL_MED 0x02
53#define VAL_NEXT_HOP 0x04
54
55DEFUN (default_attr_localpref,
56 default_attr_localpref_cmd,
57 "default-attr local-pref NUMBER",
58 "Set default local preference value\n"
59 "Set default local preference value\n"
60 "Value\n")
61{
62 struct bgp *bgp;
63 long lpref;
64
65 bgp = (struct bgp *) vty->index;
66
67 lpref = strtol (argv[0], NULL, 10);
68
69 bgp->def |= VAL_LOCAL_PREF;
70 bgp->localpref = lpref;
71
72 return CMD_SUCCESS;
73}
74
75DEFUN (no_default_attr_localpref,
76 no_default_attr_localpref_cmd,
77 "no default-attr local-pref NUMBER",
78 NO_STR
79 "Unset default local preference value\n"
80 "Unset default local preference value\n"
81 "Value\n")
82{
83 struct bgp *bgp;
84
85 bgp = (struct bgp *) vty->index;
86
87 bgp->def &= ~DEFAULT_LOCAL_PREF;
88 bgp->localpref = 0;
89
90 return CMD_SUCCESS;
91}
92
93#ifdef HAVE_IPV6
94/* Network configuration for IPv6. */
95int
96bgp_network_config_ipv6 (struct vty *vty, char *address_str)
97{
98 int ret;
99 struct prefix p;
100 struct route_node *node;
101 struct bgp_info *bgp_info;
102
103 ret = str2prefix_ipv6 (address_str, (struct prefix_ipv6 *) &p);
104 if (!ret)
105 {
106 vty_out (vty, "Please specify valid address\r\n");
107 return CMD_WARNING;
108 }
109
110 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
111
112 node = route_node_get (bgp_static_ipv6, &p);
113 if (node->info)
114 {
115 vty_out (vty, "There is already same static announcement.\r\n");
116 route_unlock_node (node);
117 return CMD_WARNING;
118 }
119
120 bgp_info = bgp_info_new ();
121 bgp_info->type = ZEBRA_ROUTE_STATIC;
122 bgp_info->peer = static_peer;
123 bgp_info->attr = bgp_attr_make_default ();
124 node->info = bgp_info;
125
126 nlri_process (&p, bgp_info);
127
128 return CMD_SUCCESS;
129}
130#endif
131
132/* Configure static BGP network. */
133DEFUN (bgp_network,
134 bgp_network_cmd,
135 "network PREFIX",
136 "Announce network setup\n"
137 "Static network for bgp announcement\n")
138{
139 int ret;
140 struct bgp *bgp;
141 struct prefix p;
142 struct route_node *node;
143 struct bgp_info *bgp_info;
144
145 bgp = (struct bgp *) vty->index;
146
147 ret = str2prefix_ipv4 (argv[0], (struct prefix_ipv4 *) &p);
148 if (!ret)
149 {
150#ifdef HAVE_IPV6
151 return bgp_network_config_ipv6 (vty, argv[0]);
152#endif /* HAVE_IPV6 */
153
154 vty_out (vty, "Please specify address by a.b.c.d/mask\r\n");
155 return CMD_WARNING;
156 }
157
158 /* Make sure mask is applied. */
159 apply_mask ((struct prefix_ipv4 *) &p);
160
161 node = route_node_get (bgp_static_ipv4, &p);
162 if (node->info)
163 {
164 vty_out (vty, "There is already same static announcement.\r\n");
165 route_unlock_node (node);
166 return CMD_WARNING;
167 }
168
169 bgp_info = bgp_info_new ();
170 bgp_info->type = ZEBRA_ROUTE_STATIC;
171 bgp_info->peer = static_peer;
172 bgp_info->attr = bgp_attr_make_default ();
173 node->info = bgp_info;
174
175 nlri_process (&p, bgp_info);
176
177 return CMD_SUCCESS;
178}
179
180DEFUN (no_bgp_network,
181 no_bgp_network_cmd,
182 "no network PREFIX",
183 NO_STR
184 "Announce network setup\n"
185 "Delete static network for bgp announcement\n")
186{
187 int ret;
188 struct bgp *bgp;
189 struct route_node *np;
190 struct prefix_ipv4 p;
191
192 bgp = (struct bgp *) vty->index;
193
194 ret = str2prefix_ipv4 (argv[0], &p);
195 if (!ret)
196 {
197 vty_out (vty, "Please specify address by a.b.c.d/mask\r\n");
198 return CMD_WARNING;
199 }
200
201 apply_mask (&p);
202
203 np = route_node_get (bgp_static_ipv4, (struct prefix *) &p);
204 if (!np->info)
205 {
206 vty_out (vty, "Can't find specified static route configuration.\r\n");
207 route_unlock_node (np);
208 return CMD_WARNING;
209 }
210 nlri_delete (static_peer, (struct prefix *) &p);
211
212 /* bgp_attr_free (np->info); */
213 np->info = NULL;
214
215 route_unlock_node (np);
216
217 return CMD_SUCCESS;
218}
219
220int
221config_write_network (struct vty *vty, struct bgp *bgp)
222{
223 struct route_node *node;
224 struct bgp_route *route;
225 char buf[BUFSIZ];
226
227 for (node = route_top (bgp_static_ipv4); node; node = route_next (node))
228 for (route = node->info; route; route = route->next)
229 vty_out (vty, " network %s/%d%s",
230 inet_ntoa (node->p.u.prefix4), node->p.prefixlen, VTY_NEWLINE);
231#ifdef HAVE_IPV6
232 for (node = route_top (bgp_static_ipv6); node; node = route_next (node))
233 for (route = node->info; route; route = route->next)
234 vty_out (vty, " network %s/%d%s",
235 inet_ntop (AF_INET6, &node->p.u.prefix6, buf, BUFSIZ),
236 node->p.prefixlen, VTY_NEWLINE);
237#endif /* HAVE_IPV6 */
238
239 return 0;
240}
241
242void
243view_init ()
244{
245 bgp_static_ipv4 = route_table_init ();
246#ifdef HAVE_IPV6
247 bgp_static_ipv6 = route_table_init ();
248#endif /* HAVE_IPV6 */
249
250 static_peer = peer_new ();
251 static_peer->host = "Static annucement";
252
253 install_element (BGP_NODE, &bgp_network_cmd);
254 install_element (BGP_NODE, &no_bgp_network_cmd);
255 install_element (BGP_NODE, &default_attr_localpref_cmd);
256 install_element (BGP_NODE, &no_default_attr_localpref_cmd);
257}