2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
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
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.
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
22 /* RIPng support by Vincent Jardin <vincent.jardin@6wind.com>
23 * Copyright (C) 2002 6WIND
35 #include "ripngd/ripngd.h"
37 #define RIPNG_OFFSET_LIST_IN 0
38 #define RIPNG_OFFSET_LIST_OUT 1
39 #define RIPNG_OFFSET_LIST_MAX 2
41 struct ripng_offset_list
{
46 /* struct access_list *alist; */
48 } direct
[RIPNG_OFFSET_LIST_MAX
];
51 static struct list
*ripng_offset_list_master
;
53 static int strcmp_safe(const char *s1
, const char *s2
)
55 if (s1
== NULL
&& s2
== NULL
)
61 return strcmp(s1
, s2
);
64 static struct ripng_offset_list
*ripng_offset_list_new(void)
66 struct ripng_offset_list
*new;
68 new = XCALLOC(MTYPE_RIPNG_OFFSET_LIST
,
69 sizeof(struct ripng_offset_list
));
73 static void ripng_offset_list_free(struct ripng_offset_list
*offset
)
75 XFREE(MTYPE_RIPNG_OFFSET_LIST
, offset
);
78 static struct ripng_offset_list
*ripng_offset_list_lookup(const char *ifname
)
80 struct ripng_offset_list
*offset
;
81 struct listnode
*node
, *nnode
;
83 for (ALL_LIST_ELEMENTS(ripng_offset_list_master
, node
, nnode
, offset
)) {
84 if (strcmp_safe(offset
->ifname
, ifname
) == 0)
90 static struct ripng_offset_list
*ripng_offset_list_get(const char *ifname
)
92 struct ripng_offset_list
*offset
;
94 offset
= ripng_offset_list_lookup(ifname
);
98 offset
= ripng_offset_list_new();
100 offset
->ifname
= strdup(ifname
);
101 listnode_add_sort(ripng_offset_list_master
, offset
);
106 static int ripng_offset_list_set(struct vty
*vty
, const char *alist
,
107 const char *direct_str
, const char *metric_str
,
112 struct ripng_offset_list
*offset
;
114 /* Check direction. */
115 if (strncmp(direct_str
, "i", 1) == 0)
116 direct
= RIPNG_OFFSET_LIST_IN
;
117 else if (strncmp(direct_str
, "o", 1) == 0)
118 direct
= RIPNG_OFFSET_LIST_OUT
;
120 vty_out(vty
, "Invalid direction: %s%s", direct_str
,
126 metric
= atoi(metric_str
);
127 if (metric
< 0 || metric
> 16) {
128 vty_out(vty
, "Invalid metric: %s%s", metric_str
, VTY_NEWLINE
);
132 /* Get offset-list structure with interface name. */
133 offset
= ripng_offset_list_get(ifname
);
135 if (offset
->direct
[direct
].alist_name
)
136 free(offset
->direct
[direct
].alist_name
);
137 offset
->direct
[direct
].alist_name
= strdup(alist
);
138 offset
->direct
[direct
].metric
= metric
;
143 static int ripng_offset_list_unset(struct vty
*vty
, const char *alist
,
144 const char *direct_str
,
145 const char *metric_str
, const char *ifname
)
149 struct ripng_offset_list
*offset
;
151 /* Check direction. */
152 if (strncmp(direct_str
, "i", 1) == 0)
153 direct
= RIPNG_OFFSET_LIST_IN
;
154 else if (strncmp(direct_str
, "o", 1) == 0)
155 direct
= RIPNG_OFFSET_LIST_OUT
;
157 vty_out(vty
, "Invalid direction: %s%s", direct_str
,
163 metric
= atoi(metric_str
);
164 if (metric
< 0 || metric
> 16) {
165 vty_out(vty
, "Invalid metric: %s%s", metric_str
, VTY_NEWLINE
);
169 /* Get offset-list structure with interface name. */
170 offset
= ripng_offset_list_lookup(ifname
);
173 if (offset
->direct
[direct
].alist_name
)
174 free(offset
->direct
[direct
].alist_name
);
175 offset
->direct
[direct
].alist_name
= NULL
;
177 if (offset
->direct
[RIPNG_OFFSET_LIST_IN
].alist_name
== NULL
178 && offset
->direct
[RIPNG_OFFSET_LIST_OUT
].alist_name
180 listnode_delete(ripng_offset_list_master
, offset
);
182 free(offset
->ifname
);
183 ripng_offset_list_free(offset
);
186 vty_out(vty
, "Can't find offset-list%s", VTY_NEWLINE
);
192 #define OFFSET_LIST_IN_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].alist_name)
193 #define OFFSET_LIST_IN_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_IN].metric)
195 #define OFFSET_LIST_OUT_NAME(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].alist_name)
196 #define OFFSET_LIST_OUT_METRIC(O) ((O)->direct[RIPNG_OFFSET_LIST_OUT].metric)
198 /* If metric is modifed return 1. */
199 int ripng_offset_list_apply_in(struct prefix_ipv6
*p
, struct interface
*ifp
,
202 struct ripng_offset_list
*offset
;
203 struct access_list
*alist
;
205 /* Look up offset-list with interface name. */
206 offset
= ripng_offset_list_lookup(ifp
->name
);
207 if (offset
&& OFFSET_LIST_IN_NAME(offset
)) {
208 alist
= access_list_lookup(AFI_IP6
,
209 OFFSET_LIST_IN_NAME(offset
));
212 && access_list_apply(alist
, (struct prefix
*)p
)
214 *metric
+= OFFSET_LIST_IN_METRIC(offset
);
219 /* Look up offset-list without interface name. */
220 offset
= ripng_offset_list_lookup(NULL
);
221 if (offset
&& OFFSET_LIST_IN_NAME(offset
)) {
222 alist
= access_list_lookup(AFI_IP6
,
223 OFFSET_LIST_IN_NAME(offset
));
226 && access_list_apply(alist
, (struct prefix
*)p
)
228 *metric
+= OFFSET_LIST_IN_METRIC(offset
);
236 /* If metric is modifed return 1. */
237 int ripng_offset_list_apply_out(struct prefix_ipv6
*p
, struct interface
*ifp
,
240 struct ripng_offset_list
*offset
;
241 struct access_list
*alist
;
243 /* Look up offset-list with interface name. */
244 offset
= ripng_offset_list_lookup(ifp
->name
);
245 if (offset
&& OFFSET_LIST_OUT_NAME(offset
)) {
246 alist
= access_list_lookup(AFI_IP6
,
247 OFFSET_LIST_OUT_NAME(offset
));
250 && access_list_apply(alist
, (struct prefix
*)p
)
252 *metric
+= OFFSET_LIST_OUT_METRIC(offset
);
258 /* Look up offset-list without interface name. */
259 offset
= ripng_offset_list_lookup(NULL
);
260 if (offset
&& OFFSET_LIST_OUT_NAME(offset
)) {
261 alist
= access_list_lookup(AFI_IP6
,
262 OFFSET_LIST_OUT_NAME(offset
));
265 && access_list_apply(alist
, (struct prefix
*)p
)
267 *metric
+= OFFSET_LIST_OUT_METRIC(offset
);
275 DEFUN (ripng_offset_list
,
276 ripng_offset_list_cmd
,
277 "offset-list WORD <in|out> (0-16)",
278 "Modify RIPng metric\n"
280 "For incoming updates\n"
281 "For outgoing updates\n"
287 return ripng_offset_list_set(vty
, argv
[idx_word
]->arg
,
288 argv
[idx_in_out
]->arg
,
289 argv
[idx_number
]->arg
, NULL
);
292 DEFUN (ripng_offset_list_ifname
,
293 ripng_offset_list_ifname_cmd
,
294 "offset-list WORD <in|out> (0-16) IFNAME",
295 "Modify RIPng metric\n"
297 "For incoming updates\n"
298 "For outgoing updates\n"
300 "Interface to match\n")
306 return ripng_offset_list_set(
307 vty
, argv
[idx_word
]->arg
, argv
[idx_in_out
]->arg
,
308 argv
[idx_number
]->arg
, argv
[idx_ifname
]->arg
);
311 DEFUN (no_ripng_offset_list
,
312 no_ripng_offset_list_cmd
,
313 "no offset-list WORD <in|out> (0-16)",
315 "Modify RIPng metric\n"
317 "For incoming updates\n"
318 "For outgoing updates\n"
324 return ripng_offset_list_unset(vty
, argv
[idx_word
]->arg
,
325 argv
[idx_in_out
]->arg
,
326 argv
[idx_number
]->arg
, NULL
);
329 DEFUN (no_ripng_offset_list_ifname
,
330 no_ripng_offset_list_ifname_cmd
,
331 "no offset-list WORD <in|out> (0-16) IFNAME",
333 "Modify RIPng metric\n"
335 "For incoming updates\n"
336 "For outgoing updates\n"
338 "Interface to match\n")
344 return ripng_offset_list_unset(
345 vty
, argv
[idx_word
]->arg
, argv
[idx_in_out
]->arg
,
346 argv
[idx_number
]->arg
, argv
[idx_ifname
]->arg
);
349 static int offset_list_cmp(struct ripng_offset_list
*o1
,
350 struct ripng_offset_list
*o2
)
352 return strcmp_safe(o1
->ifname
, o2
->ifname
);
355 static void offset_list_del(struct ripng_offset_list
*offset
)
357 if (OFFSET_LIST_IN_NAME(offset
))
358 free(OFFSET_LIST_IN_NAME(offset
));
359 if (OFFSET_LIST_OUT_NAME(offset
))
360 free(OFFSET_LIST_OUT_NAME(offset
));
362 free(offset
->ifname
);
363 ripng_offset_list_free(offset
);
366 void ripng_offset_init(void)
368 ripng_offset_list_master
= list_new();
369 ripng_offset_list_master
->cmp
=
370 (int (*)(void *, void *))offset_list_cmp
;
371 ripng_offset_list_master
->del
= (void (*)(void *))offset_list_del
;
373 install_element(RIPNG_NODE
, &ripng_offset_list_cmd
);
374 install_element(RIPNG_NODE
, &ripng_offset_list_ifname_cmd
);
375 install_element(RIPNG_NODE
, &no_ripng_offset_list_cmd
);
376 install_element(RIPNG_NODE
, &no_ripng_offset_list_ifname_cmd
);
379 void ripng_offset_clean(void)
381 list_delete(ripng_offset_list_master
);
383 ripng_offset_list_master
= list_new();
384 ripng_offset_list_master
->cmp
=
385 (int (*)(void *, void *))offset_list_cmp
;
386 ripng_offset_list_master
->del
= (void (*)(void *))offset_list_del
;
389 int config_write_ripng_offset_list(struct vty
*vty
)
391 struct listnode
*node
, *nnode
;
392 struct ripng_offset_list
*offset
;
394 for (ALL_LIST_ELEMENTS(ripng_offset_list_master
, node
, nnode
, offset
)) {
395 if (!offset
->ifname
) {
396 if (offset
->direct
[RIPNG_OFFSET_LIST_IN
].alist_name
)
397 vty_out(vty
, " offset-list %s in %d%s",
398 offset
->direct
[RIPNG_OFFSET_LIST_IN
]
400 offset
->direct
[RIPNG_OFFSET_LIST_IN
]
403 if (offset
->direct
[RIPNG_OFFSET_LIST_OUT
].alist_name
)
404 vty_out(vty
, " offset-list %s out %d%s",
405 offset
->direct
[RIPNG_OFFSET_LIST_OUT
]
407 offset
->direct
[RIPNG_OFFSET_LIST_OUT
]
411 if (offset
->direct
[RIPNG_OFFSET_LIST_IN
].alist_name
)
412 vty_out(vty
, " offset-list %s in %d %s%s",
413 offset
->direct
[RIPNG_OFFSET_LIST_IN
]
415 offset
->direct
[RIPNG_OFFSET_LIST_IN
]
417 offset
->ifname
, VTY_NEWLINE
);
418 if (offset
->direct
[RIPNG_OFFSET_LIST_OUT
].alist_name
)
419 vty_out(vty
, " offset-list %s out %d %s%s",
420 offset
->direct
[RIPNG_OFFSET_LIST_OUT
]
422 offset
->direct
[RIPNG_OFFSET_LIST_OUT
]
424 offset
->ifname
, VTY_NEWLINE
);