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