]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* RIPng routemap. |
2 | * Copyright (C) 1999 Kunihiro Ishiguro | |
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 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "if.h" | |
24 | #include "memory.h" | |
25 | #include "prefix.h" | |
82f97584 | 26 | #include "vty.h" |
718e3744 | 27 | #include "routemap.h" |
28 | #include "command.h" | |
a94434b6 | 29 | #include "sockunion.h" |
718e3744 | 30 | |
31 | #include "ripngd/ripngd.h" | |
6b0655a2 | 32 | |
d62a17ae | 33 | struct rip_metric_modifier { |
34 | enum { metric_increment, metric_decrement, metric_absolute } type; | |
35 | ||
36 | u_char metric; | |
a94434b6 | 37 | }; |
38 | ||
a94434b6 | 39 | /* `match metric METRIC' */ |
40 | /* Match function return 1 if match is success else return zero. */ | |
d62a17ae | 41 | static route_map_result_t route_match_metric(void *rule, struct prefix *prefix, |
42 | route_map_object_t type, | |
43 | void *object) | |
a94434b6 | 44 | { |
d62a17ae | 45 | u_int32_t *metric; |
46 | struct ripng_info *rinfo; | |
47 | ||
48 | if (type == RMAP_RIPNG) { | |
49 | metric = rule; | |
50 | rinfo = object; | |
51 | ||
52 | if (rinfo->metric == *metric) | |
53 | return RMAP_MATCH; | |
54 | else | |
55 | return RMAP_NOMATCH; | |
56 | } | |
a94434b6 | 57 | return RMAP_NOMATCH; |
a94434b6 | 58 | } |
59 | ||
60 | /* Route map `match metric' match statement. `arg' is METRIC value */ | |
d62a17ae | 61 | static void *route_match_metric_compile(const char *arg) |
a94434b6 | 62 | { |
d62a17ae | 63 | u_int32_t *metric; |
a94434b6 | 64 | |
d62a17ae | 65 | metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(u_int32_t)); |
66 | *metric = atoi(arg); | |
a94434b6 | 67 | |
d62a17ae | 68 | if (*metric > 0) |
69 | return metric; | |
a94434b6 | 70 | |
d62a17ae | 71 | XFREE(MTYPE_ROUTE_MAP_COMPILED, metric); |
72 | return NULL; | |
a94434b6 | 73 | } |
74 | ||
75 | /* Free route map's compiled `match metric' value. */ | |
d62a17ae | 76 | static void route_match_metric_free(void *rule) |
a94434b6 | 77 | { |
d62a17ae | 78 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); |
a94434b6 | 79 | } |
80 | ||
81 | /* Route map commands for metric matching. */ | |
d62a17ae | 82 | static struct route_map_rule_cmd route_match_metric_cmd = { |
83 | "metric", route_match_metric, route_match_metric_compile, | |
84 | route_match_metric_free}; | |
6b0655a2 | 85 | |
718e3744 | 86 | /* `match interface IFNAME' */ |
a94434b6 | 87 | /* Match function return 1 if match is success else return zero. */ |
d62a17ae | 88 | static route_map_result_t route_match_interface(void *rule, |
89 | struct prefix *prefix, | |
90 | route_map_object_t type, | |
91 | void *object) | |
718e3744 | 92 | { |
d62a17ae | 93 | struct ripng_info *rinfo; |
94 | struct interface *ifp; | |
95 | char *ifname; | |
718e3744 | 96 | |
d62a17ae | 97 | if (type == RMAP_RIPNG) { |
98 | ifname = rule; | |
99 | ifp = if_lookup_by_name(ifname, VRF_DEFAULT); | |
718e3744 | 100 | |
d62a17ae | 101 | if (!ifp) |
102 | return RMAP_NOMATCH; | |
718e3744 | 103 | |
d62a17ae | 104 | rinfo = object; |
718e3744 | 105 | |
d62a17ae | 106 | if (rinfo->ifindex == ifp->ifindex) |
107 | return RMAP_MATCH; | |
108 | else | |
109 | return RMAP_NOMATCH; | |
110 | } | |
a94434b6 | 111 | return RMAP_NOMATCH; |
718e3744 | 112 | } |
113 | ||
a94434b6 | 114 | /* Route map `match interface' match statement. `arg' is IFNAME value */ |
d62a17ae | 115 | static void *route_match_interface_compile(const char *arg) |
718e3744 | 116 | { |
d62a17ae | 117 | return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); |
718e3744 | 118 | } |
119 | ||
d62a17ae | 120 | static void route_match_interface_free(void *rule) |
718e3744 | 121 | { |
d62a17ae | 122 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); |
718e3744 | 123 | } |
124 | ||
d62a17ae | 125 | static struct route_map_rule_cmd route_match_interface_cmd = { |
126 | "interface", route_match_interface, route_match_interface_compile, | |
127 | route_match_interface_free}; | |
a94434b6 | 128 | |
129 | /* `match tag TAG' */ | |
130 | /* Match function return 1 if match is success else return zero. */ | |
d62a17ae | 131 | static route_map_result_t route_match_tag(void *rule, struct prefix *prefix, |
132 | route_map_object_t type, void *object) | |
718e3744 | 133 | { |
d62a17ae | 134 | route_tag_t *tag; |
135 | struct ripng_info *rinfo; | |
136 | route_tag_t rinfo_tag; | |
137 | ||
138 | if (type == RMAP_RIPNG) { | |
139 | tag = rule; | |
140 | rinfo = object; | |
141 | ||
142 | /* The information stored by rinfo is host ordered. */ | |
143 | rinfo_tag = rinfo->tag; | |
144 | if (rinfo_tag == *tag) | |
145 | return RMAP_MATCH; | |
146 | else | |
147 | return RMAP_NOMATCH; | |
148 | } | |
a94434b6 | 149 | return RMAP_NOMATCH; |
a94434b6 | 150 | } |
151 | ||
d62a17ae | 152 | static struct route_map_rule_cmd route_match_tag_cmd = { |
9d303b37 | 153 | "tag", route_match_tag, route_map_rule_tag_compile, |
d62a17ae | 154 | route_map_rule_tag_free, |
718e3744 | 155 | }; |
6b0655a2 | 156 | |
a94434b6 | 157 | /* `set metric METRIC' */ |
718e3744 | 158 | |
a94434b6 | 159 | /* Set metric to attribute. */ |
d62a17ae | 160 | static route_map_result_t route_set_metric(void *rule, struct prefix *prefix, |
161 | route_map_object_t type, | |
162 | void *object) | |
718e3744 | 163 | { |
d62a17ae | 164 | if (type == RMAP_RIPNG) { |
165 | struct rip_metric_modifier *mod; | |
166 | struct ripng_info *rinfo; | |
167 | ||
168 | mod = rule; | |
169 | rinfo = object; | |
170 | ||
171 | if (mod->type == metric_increment) | |
172 | rinfo->metric_out += mod->metric; | |
173 | else if (mod->type == metric_decrement) | |
174 | rinfo->metric_out -= mod->metric; | |
175 | else if (mod->type == metric_absolute) | |
176 | rinfo->metric_out = mod->metric; | |
177 | ||
178 | if (rinfo->metric_out < 1) | |
179 | rinfo->metric_out = 1; | |
180 | if (rinfo->metric_out > RIPNG_METRIC_INFINITY) | |
181 | rinfo->metric_out = RIPNG_METRIC_INFINITY; | |
182 | ||
183 | rinfo->metric_set = 1; | |
184 | } | |
185 | return RMAP_OKAY; | |
718e3744 | 186 | } |
187 | ||
a94434b6 | 188 | /* set metric compilation. */ |
d62a17ae | 189 | static void *route_set_metric_compile(const char *arg) |
718e3744 | 190 | { |
d62a17ae | 191 | int len; |
192 | const char *pnt; | |
193 | int type; | |
194 | long metric; | |
195 | char *endptr = NULL; | |
196 | struct rip_metric_modifier *mod; | |
197 | ||
198 | len = strlen(arg); | |
199 | pnt = arg; | |
200 | ||
201 | if (len == 0) | |
202 | return NULL; | |
203 | ||
204 | /* Examine first character. */ | |
205 | if (arg[0] == '+') { | |
206 | type = metric_increment; | |
207 | pnt++; | |
208 | } else if (arg[0] == '-') { | |
209 | type = metric_decrement; | |
210 | pnt++; | |
211 | } else | |
212 | type = metric_absolute; | |
213 | ||
214 | /* Check beginning with digit string. */ | |
215 | if (*pnt < '0' || *pnt > '9') | |
216 | return NULL; | |
217 | ||
218 | /* Convert string to integer. */ | |
219 | metric = strtol(pnt, &endptr, 10); | |
220 | ||
221 | if (metric == LONG_MAX || *endptr != '\0') | |
222 | return NULL; | |
223 | /* Commented out by Hasso Tepper, to avoid problems in vtysh. */ | |
224 | /* if (metric < 0 || metric > RIPNG_METRIC_INFINITY) */ | |
225 | if (metric < 0) | |
226 | return NULL; | |
227 | ||
228 | mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, | |
229 | sizeof(struct rip_metric_modifier)); | |
230 | mod->type = type; | |
231 | mod->metric = metric; | |
232 | ||
233 | return mod; | |
718e3744 | 234 | } |
235 | ||
a94434b6 | 236 | /* Free route map's compiled `set metric' value. */ |
d62a17ae | 237 | static void route_set_metric_free(void *rule) |
718e3744 | 238 | { |
d62a17ae | 239 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); |
718e3744 | 240 | } |
241 | ||
d62a17ae | 242 | static struct route_map_rule_cmd route_set_metric_cmd = { |
9d303b37 | 243 | "metric", route_set_metric, route_set_metric_compile, |
d62a17ae | 244 | route_set_metric_free, |
718e3744 | 245 | }; |
a94434b6 | 246 | |
247 | /* `set ipv6 next-hop local IP_ADDRESS' */ | |
248 | ||
249 | /* Set nexthop to object. ojbect must be pointer to struct attr. */ | |
d62a17ae | 250 | static route_map_result_t route_set_ipv6_nexthop_local(void *rule, |
251 | struct prefix *prefix, | |
252 | route_map_object_t type, | |
253 | void *object) | |
718e3744 | 254 | { |
d62a17ae | 255 | struct in6_addr *address; |
256 | struct ripng_info *rinfo; | |
257 | ||
258 | if (type == RMAP_RIPNG) { | |
259 | /* Fetch routemap's rule information. */ | |
260 | address = rule; | |
261 | rinfo = object; | |
262 | ||
263 | /* Set next hop value. */ | |
264 | rinfo->nexthop_out = *address; | |
265 | } | |
266 | ||
267 | return RMAP_OKAY; | |
718e3744 | 268 | } |
269 | ||
a94434b6 | 270 | /* Route map `ipv6 nexthop local' compile function. Given string is converted |
271 | to struct in6_addr structure. */ | |
d62a17ae | 272 | static void *route_set_ipv6_nexthop_local_compile(const char *arg) |
718e3744 | 273 | { |
d62a17ae | 274 | int ret; |
275 | struct in6_addr *address; | |
718e3744 | 276 | |
d62a17ae | 277 | address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr)); |
a94434b6 | 278 | |
d62a17ae | 279 | ret = inet_pton(AF_INET6, arg, address); |
a94434b6 | 280 | |
d62a17ae | 281 | if (ret == 0) { |
282 | XFREE(MTYPE_ROUTE_MAP_COMPILED, address); | |
283 | return NULL; | |
284 | } | |
a94434b6 | 285 | |
d62a17ae | 286 | return address; |
718e3744 | 287 | } |
288 | ||
a94434b6 | 289 | /* Free route map's compiled `ipv6 nexthop local' value. */ |
d62a17ae | 290 | static void route_set_ipv6_nexthop_local_free(void *rule) |
718e3744 | 291 | { |
d62a17ae | 292 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); |
a94434b6 | 293 | } |
718e3744 | 294 | |
a94434b6 | 295 | /* Route map commands for ipv6 nexthop local set. */ |
d62a17ae | 296 | static struct route_map_rule_cmd route_set_ipv6_nexthop_local_cmd = { |
297 | "ipv6 next-hop local", route_set_ipv6_nexthop_local, | |
298 | route_set_ipv6_nexthop_local_compile, | |
299 | route_set_ipv6_nexthop_local_free}; | |
a94434b6 | 300 | |
301 | /* `set tag TAG' */ | |
302 | ||
303 | /* Set tag to object. ojbect must be pointer to struct attr. */ | |
d62a17ae | 304 | static route_map_result_t route_set_tag(void *rule, struct prefix *prefix, |
305 | route_map_object_t type, void *object) | |
a94434b6 | 306 | { |
d62a17ae | 307 | route_tag_t *tag; |
308 | struct ripng_info *rinfo; | |
309 | ||
310 | if (type == RMAP_RIPNG) { | |
311 | /* Fetch routemap's rule information. */ | |
312 | tag = rule; | |
313 | rinfo = object; | |
314 | ||
315 | /* Set next hop value. */ | |
316 | rinfo->tag_out = *tag; | |
317 | } | |
318 | ||
319 | return RMAP_OKAY; | |
718e3744 | 320 | } |
321 | ||
a94434b6 | 322 | /* Route map commands for tag set. */ |
d62a17ae | 323 | static struct route_map_rule_cmd route_set_tag_cmd = { |
324 | "tag", route_set_tag, route_map_rule_tag_compile, | |
325 | route_map_rule_tag_free}; | |
6b0655a2 | 326 | |
a94434b6 | 327 | #define MATCH_STR "Match values from routing table\n" |
328 | #define SET_STR "Set values in destination routing protocol\n" | |
329 | ||
d62a17ae | 330 | void ripng_route_map_reset() |
a94434b6 | 331 | { |
d62a17ae | 332 | /* XXX ??? */ |
333 | ; | |
a94434b6 | 334 | } |
335 | ||
d62a17ae | 336 | void ripng_route_map_init() |
718e3744 | 337 | { |
d62a17ae | 338 | route_map_init(); |
718e3744 | 339 | |
d62a17ae | 340 | route_map_match_interface_hook(generic_match_add); |
341 | route_map_no_match_interface_hook(generic_match_delete); | |
82f97584 | 342 | |
d62a17ae | 343 | route_map_match_metric_hook(generic_match_add); |
344 | route_map_no_match_metric_hook(generic_match_delete); | |
82f97584 | 345 | |
d62a17ae | 346 | route_map_match_tag_hook(generic_match_add); |
347 | route_map_no_match_tag_hook(generic_match_delete); | |
82f97584 | 348 | |
d62a17ae | 349 | route_map_set_ipv6_nexthop_local_hook(generic_set_add); |
350 | route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete); | |
82f97584 | 351 | |
d62a17ae | 352 | route_map_set_metric_hook(generic_set_add); |
353 | route_map_no_set_metric_hook(generic_set_delete); | |
82f97584 | 354 | |
d62a17ae | 355 | route_map_set_tag_hook(generic_set_add); |
356 | route_map_no_set_tag_hook(generic_set_delete); | |
82f97584 | 357 | |
d62a17ae | 358 | route_map_install_match(&route_match_metric_cmd); |
359 | route_map_install_match(&route_match_interface_cmd); | |
360 | route_map_install_match(&route_match_tag_cmd); | |
361 | route_map_install_set(&route_set_metric_cmd); | |
362 | route_map_install_set(&route_set_ipv6_nexthop_local_cmd); | |
363 | route_map_install_set(&route_set_tag_cmd); | |
718e3744 | 364 | } |