blob: 795d15513108f5d18d5bbcdbcf763feadc0c7092 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * $Id: bgp_view.c,v 1.1 2002/12/13 20:15:29 paul Exp $
3 *
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"
32#include "zebra/zebra.h"
33#include "table.h"
34#include "log.h"
35
36#include "bgpd/bgpd.h"
37#include "bgpd/bgp_route.h"
38#include "bgpd/bgp_attr.h"
39#include "bgpd/bgp_dump.h"
40#include "bgpd/bgp_aspath.h"
41
42/* Static configuration of BGP annoucement. */
43struct route_table *bgp_static_ipv4;
44#ifdef HAVE_IPV6
45struct route_table *bgp_static_ipv6;
46#endif /* HAVE_IPV6 */
47
48/* Static annoucement peer. */
49struct peer *static_peer;
50
51/* Default value setting flag */
52#define VAL_LOCAL_PREF 0x01
53#define VAL_MED 0x02
54#define VAL_NEXT_HOP 0x04
55
56DEFUN (default_attr_localpref,
57 default_attr_localpref_cmd,
58 "default-attr local-pref NUMBER",
59 "Set default local preference value\n"
60 "Set default local preference value\n"
61 "Value\n")
62{
63 struct bgp *bgp;
64 long lpref;
65
66 bgp = (struct bgp *) vty->index;
67
68 lpref = strtol (argv[0], NULL, 10);
69
70 bgp->def |= VAL_LOCAL_PREF;
71 bgp->localpref = lpref;
72
73 return CMD_SUCCESS;
74}
75
76DEFUN (no_default_attr_localpref,
77 no_default_attr_localpref_cmd,
78 "no default-attr local-pref NUMBER",
79 NO_STR
80 "Unset default local preference value\n"
81 "Unset default local preference value\n"
82 "Value\n")
83{
84 struct bgp *bgp;
85
86 bgp = (struct bgp *) vty->index;
87
88 bgp->def &= ~DEFAULT_LOCAL_PREF;
89 bgp->localpref = 0;
90
91 return CMD_SUCCESS;
92}
93
94#ifdef HAVE_IPV6
95/* Network configuration for IPv6. */
96int
97bgp_network_config_ipv6 (struct vty *vty, char *address_str)
98{
99 int ret;
100 struct prefix p;
101 struct route_node *node;
102 struct bgp_info *bgp_info;
103
104 ret = str2prefix_ipv6 (address_str, (struct prefix_ipv6 *) &p);
105 if (!ret)
106 {
107 vty_out (vty, "Please specify valid address\r\n");
108 return CMD_WARNING;
109 }
110
111 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
112
113 node = route_node_get (bgp_static_ipv6, &p);
114 if (node->info)
115 {
116 vty_out (vty, "There is already same static announcement.\r\n");
117 route_unlock_node (node);
118 return CMD_WARNING;
119 }
120
121 bgp_info = bgp_info_new ();
122 bgp_info->type = ZEBRA_ROUTE_STATIC;
123 bgp_info->peer = static_peer;
124 bgp_info->attr = bgp_attr_make_default ();
125 node->info = bgp_info;
126
127 nlri_process (&p, bgp_info);
128
129 return CMD_SUCCESS;
130}
131#endif
132
133/* Configure static BGP network. */
134DEFUN (bgp_network,
135 bgp_network_cmd,
136 "network PREFIX",
137 "Announce network setup\n"
138 "Static network for bgp announcement\n")
139{
140 int ret;
141 struct bgp *bgp;
142 struct prefix p;
143 struct route_node *node;
144 struct bgp_info *bgp_info;
145
146 bgp = (struct bgp *) vty->index;
147
148 ret = str2prefix_ipv4 (argv[0], (struct prefix_ipv4 *) &p);
149 if (!ret)
150 {
151#ifdef HAVE_IPV6
152 return bgp_network_config_ipv6 (vty, argv[0]);
153#endif /* HAVE_IPV6 */
154
155 vty_out (vty, "Please specify address by a.b.c.d/mask\r\n");
156 return CMD_WARNING;
157 }
158
159 /* Make sure mask is applied. */
160 apply_mask ((struct prefix_ipv4 *) &p);
161
162 node = route_node_get (bgp_static_ipv4, &p);
163 if (node->info)
164 {
165 vty_out (vty, "There is already same static announcement.\r\n");
166 route_unlock_node (node);
167 return CMD_WARNING;
168 }
169
170 bgp_info = bgp_info_new ();
171 bgp_info->type = ZEBRA_ROUTE_STATIC;
172 bgp_info->peer = static_peer;
173 bgp_info->attr = bgp_attr_make_default ();
174 node->info = bgp_info;
175
176 nlri_process (&p, bgp_info);
177
178 return CMD_SUCCESS;
179}
180
181DEFUN (no_bgp_network,
182 no_bgp_network_cmd,
183 "no network PREFIX",
184 NO_STR
185 "Announce network setup\n"
186 "Delete static network for bgp announcement\n")
187{
188 int ret;
189 struct bgp *bgp;
190 struct route_node *np;
191 struct prefix_ipv4 p;
192
193 bgp = (struct bgp *) vty->index;
194
195 ret = str2prefix_ipv4 (argv[0], &p);
196 if (!ret)
197 {
198 vty_out (vty, "Please specify address by a.b.c.d/mask\r\n");
199 return CMD_WARNING;
200 }
201
202 apply_mask (&p);
203
204 np = route_node_get (bgp_static_ipv4, (struct prefix *) &p);
205 if (!np->info)
206 {
207 vty_out (vty, "Can't find specified static route configuration.\r\n");
208 route_unlock_node (np);
209 return CMD_WARNING;
210 }
211 nlri_delete (static_peer, (struct prefix *) &p);
212
213 /* bgp_attr_free (np->info); */
214 np->info = NULL;
215
216 route_unlock_node (np);
217
218 return CMD_SUCCESS;
219}
220
221int
222config_write_network (struct vty *vty, struct bgp *bgp)
223{
224 struct route_node *node;
225 struct bgp_route *route;
226 char buf[BUFSIZ];
227
228 for (node = route_top (bgp_static_ipv4); node; node = route_next (node))
229 for (route = node->info; route; route = route->next)
230 vty_out (vty, " network %s/%d%s",
231 inet_ntoa (node->p.u.prefix4), node->p.prefixlen, VTY_NEWLINE);
232#ifdef HAVE_IPV6
233 for (node = route_top (bgp_static_ipv6); node; node = route_next (node))
234 for (route = node->info; route; route = route->next)
235 vty_out (vty, " network %s/%d%s",
236 inet_ntop (AF_INET6, &node->p.u.prefix6, buf, BUFSIZ),
237 node->p.prefixlen, VTY_NEWLINE);
238#endif /* HAVE_IPV6 */
239
240 return 0;
241}
242
243void
244view_init ()
245{
246 bgp_static_ipv4 = route_table_init ();
247#ifdef HAVE_IPV6
248 bgp_static_ipv6 = route_table_init ();
249#endif /* HAVE_IPV6 */
250
251 static_peer = peer_new ();
252 static_peer->host = "Static annucement";
253
254 install_element (BGP_NODE, &bgp_network_cmd);
255 install_element (BGP_NODE, &no_bgp_network_cmd);
256 install_element (BGP_NODE, &default_attr_localpref_cmd);
257 install_element (BGP_NODE, &no_default_attr_localpref_cmd);
258}