]>
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; | |
6a74c5f9 | 35 | bool used; |
d7c0a89a | 36 | uint8_t metric; |
a94434b6 | 37 | }; |
38 | ||
a94434b6 | 39 | /* `match metric METRIC' */ |
40 | /* Match function return 1 if match is success else return zero. */ | |
b68885f9 LK |
41 | static enum route_map_cmd_result_t |
42 | route_match_metric(void *rule, const struct prefix *prefix, | |
43 | route_map_object_t type, void *object) | |
a94434b6 | 44 | { |
d7c0a89a | 45 | uint32_t *metric; |
d62a17ae | 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 | { |
d7c0a89a | 63 | uint32_t *metric; |
a94434b6 | 64 | |
d7c0a89a | 65 | metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t)); |
d62a17ae | 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. */ |
b68885f9 LK |
88 | static enum route_map_cmd_result_t |
89 | route_match_interface(void *rule, const struct prefix *prefix, | |
90 | route_map_object_t type, void *object) | |
718e3744 | 91 | { |
d62a17ae | 92 | struct ripng_info *rinfo; |
93 | struct interface *ifp; | |
94 | char *ifname; | |
718e3744 | 95 | |
d62a17ae | 96 | if (type == RMAP_RIPNG) { |
97 | ifname = rule; | |
a36898e7 | 98 | ifp = if_lookup_by_name(ifname, VRF_DEFAULT); |
718e3744 | 99 | |
d62a17ae | 100 | if (!ifp) |
101 | return RMAP_NOMATCH; | |
718e3744 | 102 | |
d62a17ae | 103 | rinfo = object; |
718e3744 | 104 | |
d62a17ae | 105 | if (rinfo->ifindex == ifp->ifindex) |
106 | return RMAP_MATCH; | |
107 | else | |
108 | return RMAP_NOMATCH; | |
109 | } | |
a94434b6 | 110 | return RMAP_NOMATCH; |
718e3744 | 111 | } |
112 | ||
a94434b6 | 113 | /* Route map `match interface' match statement. `arg' is IFNAME value */ |
d62a17ae | 114 | static void *route_match_interface_compile(const char *arg) |
718e3744 | 115 | { |
d62a17ae | 116 | return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); |
718e3744 | 117 | } |
118 | ||
d62a17ae | 119 | static void route_match_interface_free(void *rule) |
718e3744 | 120 | { |
d62a17ae | 121 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); |
718e3744 | 122 | } |
123 | ||
d62a17ae | 124 | static struct route_map_rule_cmd route_match_interface_cmd = { |
125 | "interface", route_match_interface, route_match_interface_compile, | |
126 | route_match_interface_free}; | |
a94434b6 | 127 | |
128 | /* `match tag TAG' */ | |
129 | /* Match function return 1 if match is success else return zero. */ | |
b68885f9 LK |
130 | static enum route_map_cmd_result_t route_match_tag(void *rule, |
131 | const struct prefix *prefix, | |
132 | route_map_object_t type, | |
133 | void *object) | |
718e3744 | 134 | { |
d62a17ae | 135 | route_tag_t *tag; |
136 | struct ripng_info *rinfo; | |
137 | route_tag_t rinfo_tag; | |
138 | ||
139 | if (type == RMAP_RIPNG) { | |
140 | tag = rule; | |
141 | rinfo = object; | |
142 | ||
143 | /* The information stored by rinfo is host ordered. */ | |
144 | rinfo_tag = rinfo->tag; | |
145 | if (rinfo_tag == *tag) | |
146 | return RMAP_MATCH; | |
147 | else | |
148 | return RMAP_NOMATCH; | |
149 | } | |
a94434b6 | 150 | return RMAP_NOMATCH; |
a94434b6 | 151 | } |
152 | ||
d62a17ae | 153 | static struct route_map_rule_cmd route_match_tag_cmd = { |
9d303b37 | 154 | "tag", route_match_tag, route_map_rule_tag_compile, |
d62a17ae | 155 | route_map_rule_tag_free, |
718e3744 | 156 | }; |
6b0655a2 | 157 | |
a94434b6 | 158 | /* `set metric METRIC' */ |
718e3744 | 159 | |
a94434b6 | 160 | /* Set metric to attribute. */ |
b68885f9 LK |
161 | static enum route_map_cmd_result_t |
162 | route_set_metric(void *rule, const struct prefix *prefix, | |
163 | route_map_object_t type, void *object) | |
718e3744 | 164 | { |
d62a17ae | 165 | if (type == RMAP_RIPNG) { |
166 | struct rip_metric_modifier *mod; | |
167 | struct ripng_info *rinfo; | |
168 | ||
169 | mod = rule; | |
170 | rinfo = object; | |
171 | ||
6a74c5f9 DS |
172 | if (!mod->used) |
173 | return RMAP_OKAY; | |
174 | ||
d62a17ae | 175 | if (mod->type == metric_increment) |
176 | rinfo->metric_out += mod->metric; | |
177 | else if (mod->type == metric_decrement) | |
178 | rinfo->metric_out -= mod->metric; | |
179 | else if (mod->type == metric_absolute) | |
180 | rinfo->metric_out = mod->metric; | |
181 | ||
182 | if (rinfo->metric_out < 1) | |
183 | rinfo->metric_out = 1; | |
184 | if (rinfo->metric_out > RIPNG_METRIC_INFINITY) | |
185 | rinfo->metric_out = RIPNG_METRIC_INFINITY; | |
186 | ||
187 | rinfo->metric_set = 1; | |
188 | } | |
189 | return RMAP_OKAY; | |
718e3744 | 190 | } |
191 | ||
a94434b6 | 192 | /* set metric compilation. */ |
d62a17ae | 193 | static void *route_set_metric_compile(const char *arg) |
718e3744 | 194 | { |
d62a17ae | 195 | int len; |
196 | const char *pnt; | |
d62a17ae | 197 | long metric; |
198 | char *endptr = NULL; | |
199 | struct rip_metric_modifier *mod; | |
200 | ||
201 | len = strlen(arg); | |
202 | pnt = arg; | |
203 | ||
6a74c5f9 DS |
204 | mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, |
205 | sizeof(struct rip_metric_modifier)); | |
206 | mod->used = false; | |
207 | ||
d62a17ae | 208 | if (len == 0) |
6a74c5f9 | 209 | return mod; |
d62a17ae | 210 | |
211 | /* Examine first character. */ | |
212 | if (arg[0] == '+') { | |
6a74c5f9 | 213 | mod->type = metric_increment; |
d62a17ae | 214 | pnt++; |
215 | } else if (arg[0] == '-') { | |
6a74c5f9 | 216 | mod->type = metric_decrement; |
d62a17ae | 217 | pnt++; |
218 | } else | |
6a74c5f9 | 219 | mod->type = metric_absolute; |
d62a17ae | 220 | |
221 | /* Check beginning with digit string. */ | |
222 | if (*pnt < '0' || *pnt > '9') | |
6a74c5f9 | 223 | return mod; |
d62a17ae | 224 | |
225 | /* Convert string to integer. */ | |
226 | metric = strtol(pnt, &endptr, 10); | |
227 | ||
6a74c5f9 DS |
228 | if (*endptr != '\0' || metric < 0) |
229 | return mod; | |
d62a17ae | 230 | |
6a74c5f9 DS |
231 | if (metric > RIPNG_METRIC_INFINITY) { |
232 | zlog_info("%s: Metric specified: %ld is being converted into METRIC_INFINITY", | |
233 | __PRETTY_FUNCTION__, | |
234 | metric); | |
235 | mod->metric = RIPNG_METRIC_INFINITY; | |
236 | } else | |
237 | mod->metric = metric; | |
d62a17ae | 238 | |
6a74c5f9 | 239 | mod->used = true; |
d62a17ae | 240 | return mod; |
718e3744 | 241 | } |
242 | ||
a94434b6 | 243 | /* Free route map's compiled `set metric' value. */ |
d62a17ae | 244 | static void route_set_metric_free(void *rule) |
718e3744 | 245 | { |
d62a17ae | 246 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); |
718e3744 | 247 | } |
248 | ||
d62a17ae | 249 | static struct route_map_rule_cmd route_set_metric_cmd = { |
9d303b37 | 250 | "metric", route_set_metric, route_set_metric_compile, |
d62a17ae | 251 | route_set_metric_free, |
718e3744 | 252 | }; |
a94434b6 | 253 | |
254 | /* `set ipv6 next-hop local IP_ADDRESS' */ | |
255 | ||
256 | /* Set nexthop to object. ojbect must be pointer to struct attr. */ | |
b68885f9 LK |
257 | static enum route_map_cmd_result_t |
258 | route_set_ipv6_nexthop_local(void *rule, const struct prefix *p, | |
259 | route_map_object_t type, void *object) | |
718e3744 | 260 | { |
d62a17ae | 261 | struct in6_addr *address; |
262 | struct ripng_info *rinfo; | |
263 | ||
264 | if (type == RMAP_RIPNG) { | |
265 | /* Fetch routemap's rule information. */ | |
266 | address = rule; | |
267 | rinfo = object; | |
268 | ||
269 | /* Set next hop value. */ | |
270 | rinfo->nexthop_out = *address; | |
271 | } | |
272 | ||
273 | return RMAP_OKAY; | |
718e3744 | 274 | } |
275 | ||
a94434b6 | 276 | /* Route map `ipv6 nexthop local' compile function. Given string is converted |
277 | to struct in6_addr structure. */ | |
d62a17ae | 278 | static void *route_set_ipv6_nexthop_local_compile(const char *arg) |
718e3744 | 279 | { |
d62a17ae | 280 | int ret; |
281 | struct in6_addr *address; | |
718e3744 | 282 | |
d62a17ae | 283 | address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr)); |
a94434b6 | 284 | |
d62a17ae | 285 | ret = inet_pton(AF_INET6, arg, address); |
a94434b6 | 286 | |
d62a17ae | 287 | if (ret == 0) { |
288 | XFREE(MTYPE_ROUTE_MAP_COMPILED, address); | |
289 | return NULL; | |
290 | } | |
a94434b6 | 291 | |
d62a17ae | 292 | return address; |
718e3744 | 293 | } |
294 | ||
a94434b6 | 295 | /* Free route map's compiled `ipv6 nexthop local' value. */ |
d62a17ae | 296 | static void route_set_ipv6_nexthop_local_free(void *rule) |
718e3744 | 297 | { |
d62a17ae | 298 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); |
a94434b6 | 299 | } |
718e3744 | 300 | |
a94434b6 | 301 | /* Route map commands for ipv6 nexthop local set. */ |
d62a17ae | 302 | static struct route_map_rule_cmd route_set_ipv6_nexthop_local_cmd = { |
303 | "ipv6 next-hop local", route_set_ipv6_nexthop_local, | |
304 | route_set_ipv6_nexthop_local_compile, | |
305 | route_set_ipv6_nexthop_local_free}; | |
a94434b6 | 306 | |
307 | /* `set tag TAG' */ | |
308 | ||
309 | /* Set tag to object. ojbect must be pointer to struct attr. */ | |
b68885f9 LK |
310 | static enum route_map_cmd_result_t |
311 | route_set_tag(void *rule, const struct prefix *prefix, route_map_object_t type, | |
312 | void *object) | |
a94434b6 | 313 | { |
d62a17ae | 314 | route_tag_t *tag; |
315 | struct ripng_info *rinfo; | |
316 | ||
317 | if (type == RMAP_RIPNG) { | |
318 | /* Fetch routemap's rule information. */ | |
319 | tag = rule; | |
320 | rinfo = object; | |
321 | ||
322 | /* Set next hop value. */ | |
323 | rinfo->tag_out = *tag; | |
324 | } | |
325 | ||
326 | return RMAP_OKAY; | |
718e3744 | 327 | } |
328 | ||
a94434b6 | 329 | /* Route map commands for tag set. */ |
d62a17ae | 330 | static struct route_map_rule_cmd route_set_tag_cmd = { |
331 | "tag", route_set_tag, route_map_rule_tag_compile, | |
332 | route_map_rule_tag_free}; | |
6b0655a2 | 333 | |
a94434b6 | 334 | #define MATCH_STR "Match values from routing table\n" |
335 | #define SET_STR "Set values in destination routing protocol\n" | |
336 | ||
4d762f26 | 337 | void ripng_route_map_init(void) |
718e3744 | 338 | { |
d62a17ae | 339 | route_map_init(); |
718e3744 | 340 | |
d62a17ae | 341 | route_map_match_interface_hook(generic_match_add); |
342 | route_map_no_match_interface_hook(generic_match_delete); | |
82f97584 | 343 | |
d62a17ae | 344 | route_map_match_metric_hook(generic_match_add); |
345 | route_map_no_match_metric_hook(generic_match_delete); | |
82f97584 | 346 | |
d62a17ae | 347 | route_map_match_tag_hook(generic_match_add); |
348 | route_map_no_match_tag_hook(generic_match_delete); | |
82f97584 | 349 | |
d62a17ae | 350 | route_map_set_ipv6_nexthop_local_hook(generic_set_add); |
351 | route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete); | |
82f97584 | 352 | |
d62a17ae | 353 | route_map_set_metric_hook(generic_set_add); |
354 | route_map_no_set_metric_hook(generic_set_delete); | |
82f97584 | 355 | |
d62a17ae | 356 | route_map_set_tag_hook(generic_set_add); |
357 | route_map_no_set_tag_hook(generic_set_delete); | |
82f97584 | 358 | |
d62a17ae | 359 | route_map_install_match(&route_match_metric_cmd); |
360 | route_map_install_match(&route_match_interface_cmd); | |
361 | route_map_install_match(&route_match_tag_cmd); | |
362 | route_map_install_set(&route_set_metric_cmd); | |
363 | route_map_install_set(&route_set_ipv6_nexthop_local_cmd); | |
364 | route_map_install_set(&route_set_tag_cmd); | |
718e3744 | 365 | } |