]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_offset.c
2005-10-26 Paul Jakma <paul.jakma@sun.com>
[mirror_frr.git] / ripd / rip_offset.c
1 /* RIP 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 #include <zebra.h>
23
24 #include "if.h"
25 #include "prefix.h"
26 #include "filter.h"
27 #include "command.h"
28 #include "linklist.h"
29 #include "memory.h"
30
31 #include "ripd/ripd.h"
32
33 #define RIP_OFFSET_LIST_IN 0
34 #define RIP_OFFSET_LIST_OUT 1
35 #define RIP_OFFSET_LIST_MAX 2
36
37 struct rip_offset_list
38 {
39 char *ifname;
40
41 struct
42 {
43 char *alist_name;
44 /* struct access_list *alist; */
45 int metric;
46 } direct[RIP_OFFSET_LIST_MAX];
47 };
48
49 static struct list *rip_offset_list_master;
50
51 static int
52 strcmp_safe (const char *s1, const char *s2)
53 {
54 if (s1 == NULL && s2 == NULL)
55 return 0;
56 if (s1 == NULL)
57 return -1;
58 if (s2 == NULL)
59 return 1;
60 return strcmp (s1, s2);
61 }
62
63 static struct rip_offset_list *
64 rip_offset_list_new (void)
65 {
66 struct rip_offset_list *new;
67
68 new = XMALLOC (MTYPE_RIP_OFFSET_LIST, sizeof (struct rip_offset_list));
69 memset (new, 0, sizeof (struct rip_offset_list));
70 return new;
71 }
72
73 static void
74 rip_offset_list_free (struct rip_offset_list *offset)
75 {
76 XFREE (MTYPE_RIP_OFFSET_LIST, offset);
77 }
78
79 static struct rip_offset_list *
80 rip_offset_list_lookup (const char *ifname)
81 {
82 struct rip_offset_list *offset;
83 struct listnode *node, *nnode;
84
85 for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
86 {
87 if (strcmp_safe (offset->ifname, ifname) == 0)
88 return offset;
89 }
90 return NULL;
91 }
92
93 static struct rip_offset_list *
94 rip_offset_list_get (const char *ifname)
95 {
96 struct rip_offset_list *offset;
97
98 offset = rip_offset_list_lookup (ifname);
99 if (offset)
100 return offset;
101
102 offset = rip_offset_list_new ();
103 if (ifname)
104 offset->ifname = strdup (ifname);
105 listnode_add_sort (rip_offset_list_master, offset);
106
107 return offset;
108 }
109
110 static int
111 rip_offset_list_set (struct vty *vty, const char *alist, const char *direct_str,
112 const char *metric_str, const char *ifname)
113 {
114 int direct;
115 int metric;
116 struct rip_offset_list *offset;
117
118 /* Check direction. */
119 if (strncmp (direct_str, "i", 1) == 0)
120 direct = RIP_OFFSET_LIST_IN;
121 else if (strncmp (direct_str, "o", 1) == 0)
122 direct = RIP_OFFSET_LIST_OUT;
123 else
124 {
125 vty_out (vty, "Invalid direction: %s%s", direct_str, VTY_NEWLINE);
126 return CMD_WARNING;
127 }
128
129 /* Check metric. */
130 metric = atoi (metric_str);
131 if (metric < 0 || metric > 16)
132 {
133 vty_out (vty, "Invalid metric: %s%s", metric_str, VTY_NEWLINE);
134 return CMD_WARNING;
135 }
136
137 /* Get offset-list structure with interface name. */
138 offset = rip_offset_list_get (ifname);
139
140 if (offset->direct[direct].alist_name)
141 free (offset->direct[direct].alist_name);
142 offset->direct[direct].alist_name = strdup (alist);
143 offset->direct[direct].metric = metric;
144
145 return CMD_SUCCESS;
146 }
147
148 static int
149 rip_offset_list_unset (struct vty *vty, const char *alist,
150 const char *direct_str, const char *metric_str,
151 const char *ifname)
152 {
153 int direct;
154 int metric;
155 struct rip_offset_list *offset;
156
157 /* Check direction. */
158 if (strncmp (direct_str, "i", 1) == 0)
159 direct = RIP_OFFSET_LIST_IN;
160 else if (strncmp (direct_str, "o", 1) == 0)
161 direct = RIP_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 = rip_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[RIP_OFFSET_LIST_IN].alist_name == NULL &&
186 offset->direct[RIP_OFFSET_LIST_OUT].alist_name == NULL)
187 {
188 listnode_delete (rip_offset_list_master, offset);
189 if (offset->ifname)
190 free (offset->ifname);
191 rip_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[RIP_OFFSET_LIST_IN].alist_name)
203 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_IN].metric)
204
205 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIP_OFFSET_LIST_OUT].alist_name)
206 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIP_OFFSET_LIST_OUT].metric)
207
208 /* If metric is modifed return 1. */
209 int
210 rip_offset_list_apply_in (struct prefix_ipv4 *p, struct interface *ifp,
211 u_int32_t *metric)
212 {
213 struct rip_offset_list *offset;
214 struct access_list *alist;
215
216 /* Look up offset-list with interface name. */
217 offset = rip_offset_list_lookup (ifp->name);
218 if (offset && OFFSET_LIST_IN_NAME (offset))
219 {
220 alist = access_list_lookup (AFI_IP, 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 = rip_offset_list_lookup (NULL);
232 if (offset && OFFSET_LIST_IN_NAME (offset))
233 {
234 alist = access_list_lookup (AFI_IP, 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. */
248 int
249 rip_offset_list_apply_out (struct prefix_ipv4 *p, struct interface *ifp,
250 u_int32_t *metric)
251 {
252 struct rip_offset_list *offset;
253 struct access_list *alist;
254
255 /* Look up offset-list with interface name. */
256 offset = rip_offset_list_lookup (ifp->name);
257 if (offset && OFFSET_LIST_OUT_NAME (offset))
258 {
259 alist = access_list_lookup (AFI_IP, 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 = rip_offset_list_lookup (NULL);
272 if (offset && OFFSET_LIST_OUT_NAME (offset))
273 {
274 alist = access_list_lookup (AFI_IP, 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
287 DEFUN (rip_offset_list,
288 rip_offset_list_cmd,
289 "offset-list WORD (in|out) <0-16>",
290 "Modify RIP metric\n"
291 "Access-list name\n"
292 "For incoming updates\n"
293 "For outgoing updates\n"
294 "Metric value\n")
295 {
296 return rip_offset_list_set (vty, argv[0], argv[1], argv[2], NULL);
297 }
298
299 DEFUN (rip_offset_list_ifname,
300 rip_offset_list_ifname_cmd,
301 "offset-list WORD (in|out) <0-16> IFNAME",
302 "Modify RIP 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 rip_offset_list_set (vty, argv[0], argv[1], argv[2], argv[3]);
310 }
311
312 DEFUN (no_rip_offset_list,
313 no_rip_offset_list_cmd,
314 "no offset-list WORD (in|out) <0-16>",
315 NO_STR
316 "Modify RIP metric\n"
317 "Access-list name\n"
318 "For incoming updates\n"
319 "For outgoing updates\n"
320 "Metric value\n")
321 {
322 return rip_offset_list_unset (vty, argv[0], argv[1], argv[2], NULL);
323 }
324
325 DEFUN (no_rip_offset_list_ifname,
326 no_rip_offset_list_ifname_cmd,
327 "no offset-list WORD (in|out) <0-16> IFNAME",
328 NO_STR
329 "Modify RIP 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 rip_offset_list_unset (vty, argv[0], argv[1], argv[2], argv[3]);
337 }
338
339 static int
340 offset_list_cmp (struct rip_offset_list *o1, struct rip_offset_list *o2)
341 {
342 return strcmp_safe (o1->ifname, o2->ifname);
343 }
344
345 static void
346 offset_list_del (struct rip_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 rip_offset_list_free (offset);
355 }
356
357 void
358 rip_offset_init ()
359 {
360 rip_offset_list_master = list_new ();
361 rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
362 rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
363
364 install_element (RIP_NODE, &rip_offset_list_cmd);
365 install_element (RIP_NODE, &rip_offset_list_ifname_cmd);
366 install_element (RIP_NODE, &no_rip_offset_list_cmd);
367 install_element (RIP_NODE, &no_rip_offset_list_ifname_cmd);
368 }
369
370 void
371 rip_offset_clean ()
372 {
373 list_delete (rip_offset_list_master);
374
375 rip_offset_list_master = list_new ();
376 rip_offset_list_master->cmp = (int (*)(void *, void *)) offset_list_cmp;
377 rip_offset_list_master->del = (void (*)(void *)) offset_list_del;
378 }
379
380 int
381 config_write_rip_offset_list (struct vty *vty)
382 {
383 struct listnode *node, *nnode;
384 struct rip_offset_list *offset;
385
386 for (ALL_LIST_ELEMENTS (rip_offset_list_master, node, nnode, offset))
387 {
388 if (! offset->ifname)
389 {
390 if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
391 vty_out (vty, " offset-list %s in %d%s",
392 offset->direct[RIP_OFFSET_LIST_IN].alist_name,
393 offset->direct[RIP_OFFSET_LIST_IN].metric,
394 VTY_NEWLINE);
395 if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
396 vty_out (vty, " offset-list %s out %d%s",
397 offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
398 offset->direct[RIP_OFFSET_LIST_OUT].metric,
399 VTY_NEWLINE);
400 }
401 else
402 {
403 if (offset->direct[RIP_OFFSET_LIST_IN].alist_name)
404 vty_out (vty, " offset-list %s in %d %s%s",
405 offset->direct[RIP_OFFSET_LIST_IN].alist_name,
406 offset->direct[RIP_OFFSET_LIST_IN].metric,
407 offset->ifname, VTY_NEWLINE);
408 if (offset->direct[RIP_OFFSET_LIST_OUT].alist_name)
409 vty_out (vty, " offset-list %s out %d %s%s",
410 offset->direct[RIP_OFFSET_LIST_OUT].alist_name,
411 offset->direct[RIP_OFFSET_LIST_OUT].metric,
412 offset->ifname, VTY_NEWLINE);
413 }
414 }
415
416 return 0;
417 }