blob: ab3c46012a1d9394a173afc40d56291b6fac5a99 [file] [log] [blame]
hassoa94434b2003-05-25 17:10:12 +00001/* RIPng offset-list
2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
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 Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 /* RIPng support by Vincent Jardin <vincent.jardin@6wind.com>
23 * Copyright (C) 2002 6WIND
24 */
25
26#include <zebra.h>
27
28#include "if.h"
29#include "prefix.h"
30#include "filter.h"
31#include "command.h"
32#include "linklist.h"
33#include "memory.h"
34
35#define RIPNG_OFFSET_LIST_IN 0
36#define RIPNG_OFFSET_LIST_OUT 1
37#define RIPNG_OFFSET_LIST_MAX 2
38
39struct ripng_offset_list
40{
41 char *ifname;
42
43 struct
44 {
45 char *alist_name;
46 /* struct access_list *alist; */
47 int metric;
48 } direct[RIPNG_OFFSET_LIST_MAX];
49};
50
51static struct list *ripng_offset_list_master;
52
53int
54strcmp_safe (char *s1, char *s2)
55{
56 if (s1 == NULL && s2 == NULL)
57 return 0;
58 if (s1 == NULL)
59 return -1;
60 if (s2 == NULL)
61 return 1;
62 return strcmp (s1, s2);
63}
64
65struct ripng_offset_list *
66ripng_offset_list_new ()
67{
68 struct ripng_offset_list *new;
69
70 new = XCALLOC (MTYPE_RIPNG_OFFSET_LIST, sizeof (struct ripng_offset_list));
71 return new;
72}
73
74void
75ripng_offset_list_free (struct ripng_offset_list *offset)
76{
77 XFREE (MTYPE_RIPNG_OFFSET_LIST, offset);
78}
79
80struct ripng_offset_list *
81ripng_offset_list_lookup (char *ifname)
82{
83 struct ripng_offset_list *offset;
84 struct listnode *nn;
85
86 LIST_LOOP (ripng_offset_list_master, offset, nn)
87 {
88 if (strcmp_safe (offset->ifname, ifname) == 0)
89 return offset;
90 }
91 return NULL;
92}
93
94struct ripng_offset_list *
95ripng_offset_list_get (char *ifname)
96{
97 struct ripng_offset_list *offset;
98
99 offset = ripng_offset_list_lookup (ifname);
100 if (offset)
101 return offset;
102
103 offset = ripng_offset_list_new ();
104 if (ifname)
105 offset->ifname = strdup (ifname);
106 listnode_add_sort (ripng_offset_list_master, offset);
107
108 return offset;
109}
110
111int
112ripng_offset_list_set (struct vty *vty, char *alist, char *direct_str,
113 char *metric_str, char *ifname)
114{
115 int direct;
116 int metric;
117 struct ripng_offset_list *offset;
118
119 /* Check direction. */
120 if (strncmp (direct_str, "i", 1) == 0)
121 direct = RIPNG_OFFSET_LIST_IN;
122 else if (strncmp (direct_str, "o", 1) == 0)
123 direct = RIPNG_OFFSET_LIST_OUT;
124 else
125 {
126 vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
127 return CMD_WARNING;
128 }
129
130 /* Check metric. */
131 metric = atoi (metric_str);
132 if (metric < 0 || metric > 16)
133 {
134 vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
135 return CMD_WARNING;
136 }
137
138 /* Get offset-list structure with interface name. */
139 offset = ripng_offset_list_get (ifname);
140
141 if (offset->direct[direct].alist_name)
142 free (offset->direct[direct].alist_name);
143 offset->direct[direct].alist_name = strdup (alist);
144 offset->direct[direct].metric = metric;
145
146 return CMD_SUCCESS;
147}
148
149int
150ripng_offset_list_unset (struct vty *vty, char *alist, char *direct_str,
151 char *metric_str, char *ifname)
152{
153 int direct;
154 int metric;
155 struct ripng_offset_list *offset;
156
157 /* Check direction. */
158 if (strncmp (direct_str, "i", 1) == 0)
159 direct = RIPNG_OFFSET_LIST_IN;
160 else if (strncmp (direct_str, "o", 1) == 0)
161 direct = RIPNG_OFFSET_LIST_OUT;
162 else
163 {
164 vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
165 return CMD_WARNING;
166 }
167
168 /* Check metric. */
169 metric = atoi (metric_str);
170 if (metric < 0 || metric > 16)
171 {
172 vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
173 return CMD_WARNING;
174 }
175
176 /* Get offset-list structure with interface name. */
177 offset = ripng_offset_list_lookup (ifname);
178
179 if (offset)
180 {
181 if (offset->direct[direct].alist_name)
182 free (offset->direct[direct].alist_name);
183 offset->direct[direct].alist_name = NULL;
184
185 if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name == NULL &&
186 offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name == NULL)
187 {
188 listnode_delete (ripng_offset_list_master, offset);
189 if (offset->ifname)
190 free (offset->ifname);
191 ripng_offset_list_free (offset);
192 }
193 }
194 else
195 {
196 vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
197 return CMD_WARNING;
198 }
199 return CMD_SUCCESS;
200}
201
202#define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].alist_name)
203#define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].metric)
204
205#define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
206#define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].metric)
207
208/* If metric is modifed return 1. */
209int
210ripng_offset_list_apply_in (struct prefix_ipv6 *p, struct interface *ifp,
211 u_char *metric)
212{
213 struct ripng_offset_list *offset;
214 struct access_list *alist;
215
216 /* Look up offset-list with interface name. */
217 offset = ripng_offset_list_lookup (ifp->name);
218 if (offset && OFFSET_LIST_IN_NAME (offset))
219 {
220 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_IN_NAME (offset));
221
222 if (alist
223 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
224 {
225 *metric += OFFSET_LIST_IN_METRIC (offset);
226 return 1;
227 }
228 return 0;
229 }
230 /* Look up offset-list without interface name. */
231 offset = ripng_offset_list_lookup (NULL);
232 if (offset && OFFSET_LIST_IN_NAME (offset))
233 {
234 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_IN_NAME (offset));
235
236 if (alist
237 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
238 {
239 *metric += OFFSET_LIST_IN_METRIC (offset);
240 return 1;
241 }
242 return 0;
243 }
244 return 0;
245}
246
247/* If metric is modifed return 1. */
248int
249ripng_offset_list_apply_out (struct prefix_ipv6 *p, struct interface *ifp,
250 u_char *metric)
251{
252 struct ripng_offset_list *offset;
253 struct access_list *alist;
254
255 /* Look up offset-list with interface name. */
256 offset = ripng_offset_list_lookup (ifp->name);
257 if (offset && OFFSET_LIST_OUT_NAME (offset))
258 {
259 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_OUT_NAME (offset));
260
261 if (alist
262 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
263 {
264 *metric += OFFSET_LIST_OUT_METRIC (offset);
265 return 1;
266 }
267 return 0;
268 }
269
270 /* Look up offset-list without interface name. */
271 offset = ripng_offset_list_lookup (NULL);
272 if (offset && OFFSET_LIST_OUT_NAME (offset))
273 {
274 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_OUT_NAME (offset));
275
276 if (alist
277 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
278 {
279 *metric += OFFSET_LIST_OUT_METRIC (offset);
280 return 1;
281 }
282 return 0;
283 }
284 return 0;
285}
286
287DEFUN (ripng_offset_list,
288 ripng_offset_list_cmd,
289 "offset-list WORD (in|out) <0-16>",
290 "Modify RIPng metric\n"
291 "Access-list name\n"
292 "For incoming updates\n"
293 "For outgoing updates\n"
294 "Metric value\n")
295{
296 return ripng_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
297}
298
299DEFUN (ripng_offset_list_ifname,
300 ripng_offset_list_ifname_cmd,
301 "offset-list WORD (in|out) <0-16> IFNAME",
302 "Modify RIPng metric\n"
303 "Access-list name\n"
304 "For incoming updates\n"
305 "For outgoing updates\n"
306 "Metric value\n"
307 "Interface to match\n")
308{
309 return ripng_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
310}
311
312DEFUN (no_ripng_offset_list,
313 no_ripng_offset_list_cmd,
314 "no offset-list WORD (in|out) <0-16>",
315 NO_STR
316 "Modify RIPng metric\n"
317 "Access-list name\n"
318 "For incoming updates\n"
319 "For outgoing updates\n"
320 "Metric value\n")
321{
322 return ripng_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
323}
324
325DEFUN (no_ripng_offset_list_ifname,
326 no_ripng_offset_list_ifname_cmd,
327 "no offset-list WORD (in|out) <0-16> IFNAME",
328 NO_STR
329 "Modify RIPng metric\n"
330 "Access-list name\n"
331 "For incoming updates\n"
332 "For outgoing updates\n"
333 "Metric value\n"
334 "Interface to match\n")
335{
336 return ripng_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
337}
338
339int
340offset_list_cmp (struct ripng_offset_list *o1, struct ripng_offset_list *o2)
341{
342 return strcmp_safe (o1->ifname, o2->ifname);
343}
344
345void
346offset_list_del (struct ripng_offset_list *offset)
347{
348 if (OFFSET_LIST_IN_NAME (offset))
349 free (OFFSET_LIST_IN_NAME (offset));
350 if (OFFSET_LIST_OUT_NAME (offset))
351 free (OFFSET_LIST_OUT_NAME (offset));
352 if (offset->ifname)
353 free (offset->ifname);
354 ripng_offset_list_free (offset);
355}
356
357void
358ripng_offset_init ()
359{
360 ripng_offset_list_master = list_new ();
361 ripng_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
362 ripng_offset_list_master->del = (void (*)(void *)) offset_list_del;
363
364 install_element (RIPNG_NODE, &ripng_offset_list_cmd);
365 install_element (RIPNG_NODE, &ripng_offset_list_ifname_cmd);
366 install_element (RIPNG_NODE, &no_ripng_offset_list_cmd);
367 install_element (RIPNG_NODE, &no_ripng_offset_list_ifname_cmd);
368}
369
370void
371ripng_offset_clean ()
372{
373 list_delete (ripng_offset_list_master);
374
375 ripng_offset_list_master = list_new ();
376 ripng_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
377 ripng_offset_list_master->del = (void (*)(void *)) offset_list_del;
378}
379
380int
381config_write_ripng_offset_list (struct vty *vty)
382{
383 struct listnode *nn;
384 struct ripng_offset_list *offset;
385
386 LIST_LOOP (ripng_offset_list_master, offset, nn)
387 {
388 if (! offset->ifname)
389 {
390 if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name)
391 vty_out (vty, " offset-list %s in %d%s",
392 offset->direct[RIPNG_OFFSET_LIST_IN].alist_name,
393 offset->direct[RIPNG_OFFSET_LIST_IN].metric,
394 VTY_NEWLINE);
395 if (offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
396 vty_out (vty, " offset-list %s out %d%s",
397 offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name,
398 offset->direct[RIPNG_OFFSET_LIST_OUT].metric,
399 VTY_NEWLINE);
400 }
401 else
402 {
403 if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name)
404 vty_out (vty, " offset-list %s in %d %s%s",
405 offset->direct[RIPNG_OFFSET_LIST_IN].alist_name,
406 offset->direct[RIPNG_OFFSET_LIST_IN].metric,
407 offset->ifname, VTY_NEWLINE);
408 if (offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
409 vty_out (vty, " offset-list %s out %d %s%s",
410 offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name,
411 offset->direct[RIPNG_OFFSET_LIST_OUT].metric,
412 offset->ifname, VTY_NEWLINE);
413 }
414 }
415
416 return 0;
417}