]> git.proxmox.com Git - mirror_frr.git/blame - ripngd/ripng_offset.c
2005-04-07 Paul Jakma <paul.jakma@sun.com>
[mirror_frr.git] / ripngd / ripng_offset.c
CommitLineData
a94434b6 1/* 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
98b718a9 54strcmp_safe (const char *s1, const char *s2)
a94434b6 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 *
98b718a9 81ripng_offset_list_lookup (const char *ifname)
a94434b6 82{
83 struct ripng_offset_list *offset;
1eb8ef25 84 struct listnode *node, *nnode;
a94434b6 85
1eb8ef25 86 for (ALL_LIST_ELEMENTS (ripng_offset_list_master, node, nnode, offset))
a94434b6 87 {
88 if (strcmp_safe (offset->ifname, ifname) == 0)
89 return offset;
90 }
91 return NULL;
92}
93
94struct ripng_offset_list *
98b718a9 95ripng_offset_list_get (const char *ifname)
a94434b6 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
98b718a9 112ripng_offset_list_set (struct vty *vty, const char *alist,
113 const char *direct_str, const char *metric_str,
114 const char *ifname)
a94434b6 115{
116 int direct;
117 int metric;
118 struct ripng_offset_list *offset;
119
120 /* Check direction. */
121 if (strncmp (direct_str, "i", 1) == 0)
122 direct = RIPNG_OFFSET_LIST_IN;
123 else if (strncmp (direct_str, "o", 1) == 0)
124 direct = RIPNG_OFFSET_LIST_OUT;
125 else
126 {
127 vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
128 return CMD_WARNING;
129 }
130
131 /* Check metric. */
132 metric = atoi (metric_str);
133 if (metric < 0 || metric > 16)
134 {
135 vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
136 return CMD_WARNING;
137 }
138
139 /* Get offset-list structure with interface name. */
140 offset = ripng_offset_list_get (ifname);
141
142 if (offset->direct[direct].alist_name)
143 free (offset->direct[direct].alist_name);
144 offset->direct[direct].alist_name = strdup (alist);
145 offset->direct[direct].metric = metric;
146
147 return CMD_SUCCESS;
148}
149
150int
98b718a9 151ripng_offset_list_unset (struct vty *vty, const char *alist,
152 const char *direct_str, const char *metric_str,
153 const char *ifname)
a94434b6 154{
155 int direct;
156 int metric;
157 struct ripng_offset_list *offset;
158
159 /* Check direction. */
160 if (strncmp (direct_str, "i", 1) == 0)
161 direct = RIPNG_OFFSET_LIST_IN;
162 else if (strncmp (direct_str, "o", 1) == 0)
163 direct = RIPNG_OFFSET_LIST_OUT;
164 else
165 {
166 vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
167 return CMD_WARNING;
168 }
169
170 /* Check metric. */
171 metric = atoi (metric_str);
172 if (metric < 0 || metric > 16)
173 {
174 vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
175 return CMD_WARNING;
176 }
177
178 /* Get offset-list structure with interface name. */
179 offset = ripng_offset_list_lookup (ifname);
180
181 if (offset)
182 {
183 if (offset->direct[direct].alist_name)
184 free (offset->direct[direct].alist_name);
185 offset->direct[direct].alist_name = NULL;
186
187 if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name == NULL &&
188 offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name == NULL)
189 {
190 listnode_delete (ripng_offset_list_master, offset);
191 if (offset->ifname)
192 free (offset->ifname);
193 ripng_offset_list_free (offset);
194 }
195 }
196 else
197 {
198 vty_out (vty, "Can't find offset-list%s", VTY_NEWLINE);
199 return CMD_WARNING;
200 }
201 return CMD_SUCCESS;
202}
203
204#define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].alist_name)
205#define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].metric)
206
207#define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
208#define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].metric)
209
210/* If metric is modifed return 1. */
211int
212ripng_offset_list_apply_in (struct prefix_ipv6 *p, struct interface *ifp,
213 u_char *metric)
214{
215 struct ripng_offset_list *offset;
216 struct access_list *alist;
217
218 /* Look up offset-list with interface name. */
219 offset = ripng_offset_list_lookup (ifp->name);
220 if (offset && OFFSET_LIST_IN_NAME (offset))
221 {
222 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_IN_NAME (offset));
223
224 if (alist
225 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
226 {
227 *metric += OFFSET_LIST_IN_METRIC (offset);
228 return 1;
229 }
230 return 0;
231 }
232 /* Look up offset-list without interface name. */
233 offset = ripng_offset_list_lookup (NULL);
234 if (offset && OFFSET_LIST_IN_NAME (offset))
235 {
236 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_IN_NAME (offset));
237
238 if (alist
239 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
240 {
241 *metric += OFFSET_LIST_IN_METRIC (offset);
242 return 1;
243 }
244 return 0;
245 }
246 return 0;
247}
248
249/* If metric is modifed return 1. */
250int
251ripng_offset_list_apply_out (struct prefix_ipv6 *p, struct interface *ifp,
252 u_char *metric)
253{
254 struct ripng_offset_list *offset;
255 struct access_list *alist;
256
257 /* Look up offset-list with interface name. */
258 offset = ripng_offset_list_lookup (ifp->name);
259 if (offset && OFFSET_LIST_OUT_NAME (offset))
260 {
261 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_OUT_NAME (offset));
262
263 if (alist
264 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
265 {
266 *metric += OFFSET_LIST_OUT_METRIC (offset);
267 return 1;
268 }
269 return 0;
270 }
271
272 /* Look up offset-list without interface name. */
273 offset = ripng_offset_list_lookup (NULL);
274 if (offset && OFFSET_LIST_OUT_NAME (offset))
275 {
276 alist = access_list_lookup (AFI_IP6, OFFSET_LIST_OUT_NAME (offset));
277
278 if (alist
279 && access_list_apply (alist, (struct prefix *)p) == FILTER_PERMIT)
280 {
281 *metric += OFFSET_LIST_OUT_METRIC (offset);
282 return 1;
283 }
284 return 0;
285 }
286 return 0;
287}
288
289DEFUN (ripng_offset_list,
290 ripng_offset_list_cmd,
291 "offset-list WORD (in|out) <0-16>",
292 "Modify RIPng metric\n"
293 "Access-list name\n"
294 "For incoming updates\n"
295 "For outgoing updates\n"
296 "Metric value\n")
297{
298 return ripng_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
299}
300
301DEFUN (ripng_offset_list_ifname,
302 ripng_offset_list_ifname_cmd,
303 "offset-list WORD (in|out) <0-16> IFNAME",
304 "Modify RIPng metric\n"
305 "Access-list name\n"
306 "For incoming updates\n"
307 "For outgoing updates\n"
308 "Metric value\n"
309 "Interface to match\n")
310{
311 return ripng_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
312}
313
314DEFUN (no_ripng_offset_list,
315 no_ripng_offset_list_cmd,
316 "no offset-list WORD (in|out) <0-16>",
317 NO_STR
318 "Modify RIPng metric\n"
319 "Access-list name\n"
320 "For incoming updates\n"
321 "For outgoing updates\n"
322 "Metric value\n")
323{
324 return ripng_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
325}
326
327DEFUN (no_ripng_offset_list_ifname,
328 no_ripng_offset_list_ifname_cmd,
329 "no offset-list WORD (in|out) <0-16> IFNAME",
330 NO_STR
331 "Modify RIPng metric\n"
332 "Access-list name\n"
333 "For incoming updates\n"
334 "For outgoing updates\n"
335 "Metric value\n"
336 "Interface to match\n")
337{
338 return ripng_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
339}
340
341int
342offset_list_cmp (struct ripng_offset_list *o1, struct ripng_offset_list *o2)
343{
344 return strcmp_safe (o1->ifname, o2->ifname);
345}
346
347void
348offset_list_del (struct ripng_offset_list *offset)
349{
350 if (OFFSET_LIST_IN_NAME (offset))
351 free (OFFSET_LIST_IN_NAME (offset));
352 if (OFFSET_LIST_OUT_NAME (offset))
353 free (OFFSET_LIST_OUT_NAME (offset));
354 if (offset->ifname)
355 free (offset->ifname);
356 ripng_offset_list_free (offset);
357}
358
359void
360ripng_offset_init ()
361{
362 ripng_offset_list_master = list_new ();
363 ripng_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
364 ripng_offset_list_master->del = (void (*)(void *)) offset_list_del;
365
366 install_element (RIPNG_NODE, &ripng_offset_list_cmd);
367 install_element (RIPNG_NODE, &ripng_offset_list_ifname_cmd);
368 install_element (RIPNG_NODE, &no_ripng_offset_list_cmd);
369 install_element (RIPNG_NODE, &no_ripng_offset_list_ifname_cmd);
370}
371
372void
373ripng_offset_clean ()
374{
375 list_delete (ripng_offset_list_master);
376
377 ripng_offset_list_master = list_new ();
378 ripng_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
379 ripng_offset_list_master->del = (void (*)(void *)) offset_list_del;
380}
381
382int
383config_write_ripng_offset_list (struct vty *vty)
384{
1eb8ef25 385 struct listnode *node, *nnode;
a94434b6 386 struct ripng_offset_list *offset;
387
1eb8ef25 388 for (ALL_LIST_ELEMENTS (ripng_offset_list_master, node, nnode, offset))
a94434b6 389 {
390 if (! offset->ifname)
391 {
392 if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name)
393 vty_out (vty, " offset-list %s in %d%s",
394 offset->direct[RIPNG_OFFSET_LIST_IN].alist_name,
395 offset->direct[RIPNG_OFFSET_LIST_IN].metric,
396 VTY_NEWLINE);
397 if (offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
398 vty_out (vty, " offset-list %s out %d%s",
399 offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name,
400 offset->direct[RIPNG_OFFSET_LIST_OUT].metric,
401 VTY_NEWLINE);
402 }
403 else
404 {
405 if (offset->direct[RIPNG_OFFSET_LIST_IN].alist_name)
406 vty_out (vty, " offset-list %s in %d %s%s",
407 offset->direct[RIPNG_OFFSET_LIST_IN].alist_name,
408 offset->direct[RIPNG_OFFSET_LIST_IN].metric,
409 offset->ifname, VTY_NEWLINE);
410 if (offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
411 vty_out (vty, " offset-list %s out %d %s%s",
412 offset->direct[RIPNG_OFFSET_LIST_OUT].alist_name,
413 offset->direct[RIPNG_OFFSET_LIST_OUT].metric,
414 offset->ifname, VTY_NEWLINE);
415 }
416 }
417
418 return 0;
419}