]>
Commit | Line | Data |
---|---|---|
7f57883e DS |
1 | /* |
2 | * EIGRP Network Related Functions. | |
3 | * Copyright (C) 2013-2014 | |
4 | * Authors: | |
5 | * Donnie Savage | |
6 | * Jan Janovic | |
7 | * Matej Perina | |
8 | * Peter Orsag | |
9 | * Peter Paluch | |
10 | * | |
11 | * This file is part of GNU Zebra. | |
12 | * | |
13 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
14 | * under the terms of the GNU General Public License as published by the | |
15 | * Free Software Foundation; either version 2, or (at your option) any | |
16 | * later version. | |
17 | * | |
18 | * GNU Zebra is distributed in the hope that it will be useful, but | |
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 | * General Public License for more details. | |
22 | * | |
896014f4 DL |
23 | * You should have received a copy of the GNU General Public License along |
24 | * with this program; see the file COPYING; if not, write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
7f57883e DS |
26 | */ |
27 | ||
28 | #include <zebra.h> | |
29 | ||
30 | #include "thread.h" | |
31 | #include "linklist.h" | |
32 | #include "prefix.h" | |
33 | #include "if.h" | |
34 | #include "sockunion.h" | |
35 | #include "log.h" | |
36 | #include "sockopt.h" | |
37 | #include "privs.h" | |
38 | #include "table.h" | |
39 | #include "vty.h" | |
6ae7ed45 | 40 | #include "lib_errors.h" |
7f57883e | 41 | |
7f57883e DS |
42 | #include "eigrpd/eigrp_structs.h" |
43 | #include "eigrpd/eigrpd.h" | |
44 | #include "eigrpd/eigrp_interface.h" | |
45 | #include "eigrpd/eigrp_neighbor.h" | |
46 | #include "eigrpd/eigrp_packet.h" | |
47 | #include "eigrpd/eigrp_zebra.h" | |
48 | #include "eigrpd/eigrp_vty.h" | |
49 | #include "eigrpd/eigrp_network.h" | |
50 | ||
952248db DS |
51 | static int eigrp_network_match_iface(const struct prefix *connected_prefix, |
52 | const struct prefix *prefix); | |
d62a17ae | 53 | static void eigrp_network_run_interface(struct eigrp *, struct prefix *, |
54 | struct interface *); | |
7f57883e | 55 | |
d62a17ae | 56 | int eigrp_sock_init(void) |
7f57883e | 57 | { |
d62a17ae | 58 | int eigrp_sock; |
029a775e | 59 | int ret; |
60 | #ifdef IP_HDRINCL | |
61 | int hincl = 1; | |
62 | #endif | |
d62a17ae | 63 | |
6bb30c2c DL |
64 | frr_elevate_privs(&eigrpd_privs) { |
65 | eigrp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_EIGRPIGP); | |
66 | if (eigrp_sock < 0) { | |
67 | zlog_err("eigrp_read_sock_init: socket: %s", | |
68 | safe_strerror(errno)); | |
69 | exit(1); | |
70 | } | |
7f57883e DS |
71 | |
72 | #ifdef IP_HDRINCL | |
6bb30c2c DL |
73 | /* we will include IP header with packet */ |
74 | ret = setsockopt(eigrp_sock, IPPROTO_IP, IP_HDRINCL, &hincl, | |
75 | sizeof(hincl)); | |
76 | if (ret < 0) { | |
77 | zlog_warn("Can't set IP_HDRINCL option for fd %d: %s", | |
78 | eigrp_sock, safe_strerror(errno)); | |
79 | } | |
d62a17ae | 80 | #elif defined(IPTOS_PREC_INTERNETCONTROL) |
7f57883e DS |
81 | #warning "IP_HDRINCL not available on this system" |
82 | #warning "using IPTOS_PREC_INTERNETCONTROL" | |
6bb30c2c DL |
83 | ret = setsockopt_ipv4_tos(eigrp_sock, |
84 | IPTOS_PREC_INTERNETCONTROL); | |
85 | if (ret < 0) { | |
86 | zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", | |
87 | tos, eigrp_sock, safe_strerror(errno)); | |
88 | close(eigrp_sock); /* Prevent sd leak. */ | |
89 | return ret; | |
90 | } | |
7f57883e DS |
91 | #else /* !IPTOS_PREC_INTERNETCONTROL */ |
92 | #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL" | |
6bb30c2c | 93 | zlog_warn("IP_HDRINCL option not available"); |
7f57883e DS |
94 | #endif /* IP_HDRINCL */ |
95 | ||
6bb30c2c DL |
96 | ret = setsockopt_ifindex(AF_INET, eigrp_sock, 1); |
97 | if (ret < 0) | |
98 | zlog_warn("Can't set pktinfo option for fd %d", | |
99 | eigrp_sock); | |
d62a17ae | 100 | } |
7f57883e | 101 | |
d62a17ae | 102 | return eigrp_sock; |
7f57883e DS |
103 | } |
104 | ||
d62a17ae | 105 | void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen) |
7f57883e | 106 | { |
d62a17ae | 107 | int newbuflen; |
108 | /* Check if any work has to be done at all. */ | |
109 | if (eigrp->maxsndbuflen >= buflen) | |
110 | return; | |
01b9e3fd | 111 | frr_elevate_privs(&eigrpd_privs) { |
d62a17ae | 112 | |
113 | /* Now we try to set SO_SNDBUF to what our caller has requested | |
114 | * (the MTU of a newly added interface). However, if the OS has | |
115 | * truncated the actual buffer size to somewhat less size, try | |
116 | * to detect it and update our records appropriately. The OS | |
117 | * may allocate more buffer space, than requested, this isn't | |
118 | * a error. | |
119 | */ | |
01b9e3fd DL |
120 | setsockopt_so_sendbuf(eigrp->fd, buflen); |
121 | newbuflen = getsockopt_so_sendbuf(eigrp->fd); | |
122 | if (newbuflen < 0 || newbuflen < (int)buflen) | |
123 | zlog_warn("%s: tried to set SO_SNDBUF to %u, but got %d", | |
124 | __func__, buflen, newbuflen); | |
125 | if (newbuflen >= 0) | |
126 | eigrp->maxsndbuflen = (unsigned int)newbuflen; | |
127 | else | |
128 | zlog_warn("%s: failed to get SO_SNDBUF", __func__); | |
129 | } | |
7f57883e DS |
130 | } |
131 | ||
d62a17ae | 132 | int eigrp_if_ipmulticast(struct eigrp *top, struct prefix *p, |
133 | unsigned int ifindex) | |
7f57883e | 134 | { |
d7c0a89a | 135 | uint8_t val; |
d62a17ae | 136 | int ret, len; |
137 | ||
138 | val = 0; | |
139 | len = sizeof(val); | |
140 | ||
141 | /* Prevent receiving self-origined multicast packets. */ | |
142 | ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, | |
143 | len); | |
144 | if (ret < 0) | |
145 | zlog_warn( | |
146 | "can't setsockopt IP_MULTICAST_LOOP (0) for fd %d: %s", | |
147 | top->fd, safe_strerror(errno)); | |
148 | ||
149 | /* Explicitly set multicast ttl to 1 -- endo. */ | |
150 | val = 1; | |
151 | ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, | |
152 | len); | |
153 | if (ret < 0) | |
154 | zlog_warn("can't setsockopt IP_MULTICAST_TTL (1) for fd %d: %s", | |
155 | top->fd, safe_strerror(errno)); | |
156 | ||
157 | ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex); | |
158 | if (ret < 0) | |
159 | zlog_warn( | |
160 | "can't setsockopt IP_MULTICAST_IF (fd %d, addr %s, " | |
161 | "ifindex %u): %s", | |
162 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
163 | safe_strerror(errno)); | |
164 | ||
165 | return ret; | |
7f57883e DS |
166 | } |
167 | ||
168 | /* Join to the EIGRP multicast group. */ | |
d62a17ae | 169 | int eigrp_if_add_allspfrouters(struct eigrp *top, struct prefix *p, |
170 | unsigned int ifindex) | |
7f57883e | 171 | { |
d62a17ae | 172 | int ret; |
173 | ||
174 | ret = setsockopt_ipv4_multicast( | |
175 | top->fd, IP_ADD_MEMBERSHIP, p->u.prefix4, | |
176 | htonl(EIGRP_MULTICAST_ADDRESS), ifindex); | |
177 | if (ret < 0) | |
178 | zlog_warn( | |
179 | "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " | |
180 | "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit " | |
181 | "on # of multicast group memberships has been exceeded?", | |
182 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
183 | safe_strerror(errno)); | |
184 | else | |
185 | zlog_debug("interface %s [%u] join EIGRP Multicast group.", | |
186 | inet_ntoa(p->u.prefix4), ifindex); | |
187 | ||
188 | return ret; | |
7f57883e DS |
189 | } |
190 | ||
d62a17ae | 191 | int eigrp_if_drop_allspfrouters(struct eigrp *top, struct prefix *p, |
192 | unsigned int ifindex) | |
7f57883e | 193 | { |
d62a17ae | 194 | int ret; |
195 | ||
196 | ret = setsockopt_ipv4_multicast( | |
197 | top->fd, IP_DROP_MEMBERSHIP, p->u.prefix4, | |
198 | htonl(EIGRP_MULTICAST_ADDRESS), ifindex); | |
199 | if (ret < 0) | |
200 | zlog_warn( | |
201 | "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " | |
202 | "ifindex %u, AllSPFRouters): %s", | |
203 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
204 | safe_strerror(errno)); | |
205 | else | |
206 | zlog_debug("interface %s [%u] leave EIGRP Multicast group.", | |
207 | inet_ntoa(p->u.prefix4), ifindex); | |
208 | ||
209 | return ret; | |
7f57883e DS |
210 | } |
211 | ||
cd6c066e | 212 | int eigrp_network_set(struct eigrp *eigrp, struct prefix *p) |
7f57883e | 213 | { |
f4e14fdb | 214 | struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); |
d62a17ae | 215 | struct route_node *rn; |
216 | struct interface *ifp; | |
d62a17ae | 217 | |
218 | rn = route_node_get(eigrp->networks, (struct prefix *)p); | |
219 | if (rn->info) { | |
220 | /* There is already same network statement. */ | |
221 | route_unlock_node(rn); | |
222 | return 0; | |
223 | } | |
224 | ||
a96029f8 | 225 | struct prefix *pref = prefix_new(); |
d62a17ae | 226 | PREFIX_COPY_IPV4(pref, p); |
227 | rn->info = (void *)pref; | |
228 | ||
229 | /* Schedule Router ID Update. */ | |
230 | if (eigrp->router_id == 0) | |
231 | eigrp_router_id_update(eigrp); | |
232 | /* Run network config now. */ | |
233 | /* Get target interface. */ | |
451fda4f | 234 | FOR_ALL_INTERFACES (vrf, ifp) { |
d62a17ae | 235 | zlog_debug("Setting up %s", ifp->name); |
a96029f8 | 236 | eigrp_network_run_interface(eigrp, p, ifp); |
d62a17ae | 237 | } |
238 | return 1; | |
7f57883e DS |
239 | } |
240 | ||
241 | /* Check whether interface matches given network | |
242 | * returns: 1, true. 0, false | |
243 | */ | |
952248db | 244 | static int eigrp_network_match_iface(const struct prefix *co_prefix, |
d62a17ae | 245 | const struct prefix *net) |
7f57883e | 246 | { |
d62a17ae | 247 | /* new approach: more elegant and conceptually clean */ |
952248db | 248 | return prefix_match_network_statement(net, co_prefix); |
7f57883e DS |
249 | } |
250 | ||
d62a17ae | 251 | static void eigrp_network_run_interface(struct eigrp *eigrp, struct prefix *p, |
252 | struct interface *ifp) | |
7f57883e | 253 | { |
b748db67 | 254 | struct eigrp_interface *ei; |
d62a17ae | 255 | struct listnode *cnode; |
256 | struct connected *co; | |
257 | ||
258 | /* if interface prefix is match specified prefix, | |
259 | then create socket and join multicast group. */ | |
260 | for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, co)) { | |
261 | ||
262 | if (CHECK_FLAG(co->flags, ZEBRA_IFA_SECONDARY)) | |
263 | continue; | |
264 | ||
996c9314 | 265 | if (p->family == co->address->family && !ifp->info |
952248db | 266 | && eigrp_network_match_iface(co->address, p)) { |
d62a17ae | 267 | |
268 | ei = eigrp_if_new(eigrp, ifp, co->address); | |
d62a17ae | 269 | |
d62a17ae | 270 | /* Relate eigrp interface to eigrp instance. */ |
271 | ei->eigrp = eigrp; | |
272 | ||
d62a17ae | 273 | /* if router_id is not configured, dont bring up |
274 | * interfaces. | |
275 | * eigrp_router_id_update() will call eigrp_if_update | |
276 | * whenever r-id is configured instead. | |
277 | */ | |
278 | if (if_is_operative(ifp)) | |
279 | eigrp_if_up(ei); | |
280 | } | |
281 | } | |
7f57883e DS |
282 | } |
283 | ||
d62a17ae | 284 | void eigrp_if_update(struct interface *ifp) |
7f57883e | 285 | { |
d62a17ae | 286 | struct listnode *node, *nnode; |
287 | struct route_node *rn; | |
288 | struct eigrp *eigrp; | |
289 | ||
290 | /* | |
291 | * In the event there are multiple eigrp autonymnous systems running, | |
292 | * we need to check eac one and add the interface as approperate | |
293 | */ | |
294 | for (ALL_LIST_ELEMENTS(eigrp_om->eigrp, node, nnode, eigrp)) { | |
295 | /* EIGRP must be on and Router-ID must be configured. */ | |
296 | if (!eigrp || eigrp->router_id == 0) | |
297 | continue; | |
298 | ||
299 | /* Run each network for this interface. */ | |
300 | for (rn = route_top(eigrp->networks); rn; rn = route_next(rn)) | |
301 | if (rn->info != NULL) { | |
302 | eigrp_network_run_interface(eigrp, &rn->p, ifp); | |
303 | } | |
304 | } | |
7f57883e DS |
305 | } |
306 | ||
cd6c066e | 307 | int eigrp_network_unset(struct eigrp *eigrp, struct prefix *p) |
7f57883e | 308 | { |
d62a17ae | 309 | struct route_node *rn; |
310 | struct listnode *node, *nnode; | |
311 | struct eigrp_interface *ei; | |
312 | struct prefix *pref; | |
313 | ||
cd6c066e | 314 | rn = route_node_lookup(eigrp->networks, p); |
d62a17ae | 315 | if (rn == NULL) |
316 | return 0; | |
317 | ||
318 | pref = rn->info; | |
319 | route_unlock_node(rn); | |
320 | ||
cd6c066e | 321 | if (!IPV4_ADDR_SAME(&pref->u.prefix4, &p->u.prefix4)) |
d62a17ae | 322 | return 0; |
323 | ||
324 | prefix_ipv4_free(rn->info); | |
325 | rn->info = NULL; | |
326 | route_unlock_node(rn); /* initial reference */ | |
327 | ||
328 | /* Find interfaces that not configured already. */ | |
329 | for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei)) { | |
952248db | 330 | bool found = false; |
d62a17ae | 331 | |
332 | for (rn = route_top(eigrp->networks); rn; rn = route_next(rn)) { | |
333 | if (rn->info == NULL) | |
334 | continue; | |
335 | ||
952248db DS |
336 | if (eigrp_network_match_iface(ei->address, &rn->p)) { |
337 | found = true; | |
d62a17ae | 338 | route_unlock_node(rn); |
339 | break; | |
340 | } | |
341 | } | |
342 | ||
952248db | 343 | if (!found) { |
d62a17ae | 344 | eigrp_if_free(ei, INTERFACE_DOWN_BY_VTY); |
345 | } | |
346 | } | |
347 | ||
348 | return 1; | |
7f57883e DS |
349 | } |
350 | ||
d7c0a89a QY |
351 | uint32_t eigrp_calculate_metrics(struct eigrp *eigrp, |
352 | struct eigrp_metrics metric) | |
7f57883e | 353 | { |
d62a17ae | 354 | uint64_t temp_metric; |
355 | temp_metric = 0; | |
356 | ||
357 | if (metric.delay == EIGRP_MAX_METRIC) | |
358 | return EIGRP_MAX_METRIC; | |
359 | ||
360 | // EIGRP Metric = | |
361 | // {K1*BW+[(K2*BW)/(256-load)]+(K3*delay)}*{K5/(reliability+K4)} | |
362 | ||
363 | if (eigrp->k_values[0]) | |
b1968f83 | 364 | temp_metric += (eigrp->k_values[0] * metric.bandwidth); |
d62a17ae | 365 | if (eigrp->k_values[1]) |
b1968f83 | 366 | temp_metric += ((eigrp->k_values[1] * metric.bandwidth) |
d62a17ae | 367 | / (256 - metric.load)); |
368 | if (eigrp->k_values[2]) | |
369 | temp_metric += (eigrp->k_values[2] * metric.delay); | |
370 | if (eigrp->k_values[3] && !eigrp->k_values[4]) | |
371 | temp_metric *= eigrp->k_values[3]; | |
372 | if (!eigrp->k_values[3] && eigrp->k_values[4]) | |
373 | temp_metric *= (eigrp->k_values[4] / metric.reliability); | |
374 | if (eigrp->k_values[3] && eigrp->k_values[4]) | |
375 | temp_metric *= ((eigrp->k_values[4] / metric.reliability) | |
376 | + eigrp->k_values[3]); | |
377 | ||
378 | if (temp_metric <= EIGRP_MAX_METRIC) | |
d7c0a89a | 379 | return (uint32_t)temp_metric; |
d62a17ae | 380 | else |
381 | return EIGRP_MAX_METRIC; | |
7f57883e DS |
382 | } |
383 | ||
d7c0a89a QY |
384 | uint32_t eigrp_calculate_total_metrics(struct eigrp *eigrp, |
385 | struct eigrp_nexthop_entry *entry) | |
7f57883e | 386 | { |
b748db67 DS |
387 | struct eigrp_interface *ei = entry->ei; |
388 | ||
d62a17ae | 389 | entry->total_metric = entry->reported_metric; |
996c9314 LB |
390 | uint64_t temp_delay = |
391 | (uint64_t)entry->total_metric.delay | |
392 | + (uint64_t)eigrp_delay_to_scaled(ei->params.delay); | |
d62a17ae | 393 | entry->total_metric.delay = temp_delay > EIGRP_MAX_METRIC |
394 | ? EIGRP_MAX_METRIC | |
d7c0a89a | 395 | : (uint32_t)temp_delay; |
d62a17ae | 396 | |
d7c0a89a | 397 | uint32_t bw = eigrp_bandwidth_to_scaled(ei->params.bandwidth); |
b1968f83 | 398 | entry->total_metric.bandwidth = entry->total_metric.bandwidth > bw |
996c9314 LB |
399 | ? bw |
400 | : entry->total_metric.bandwidth; | |
d62a17ae | 401 | |
402 | return eigrp_calculate_metrics(eigrp, entry->total_metric); | |
7f57883e DS |
403 | } |
404 | ||
d7c0a89a QY |
405 | uint8_t eigrp_metrics_is_same(struct eigrp_metrics metric1, |
406 | struct eigrp_metrics metric2) | |
7f57883e | 407 | { |
b1968f83 | 408 | if ((metric1.bandwidth == metric2.bandwidth) |
d62a17ae | 409 | && (metric1.delay == metric2.delay) |
410 | && (metric1.hop_count == metric2.hop_count) | |
411 | && (metric1.load == metric2.load) | |
412 | && (metric1.reliability == metric2.reliability) | |
413 | && (metric1.mtu[0] == metric2.mtu[0]) | |
414 | && (metric1.mtu[1] == metric2.mtu[1]) | |
415 | && (metric1.mtu[2] == metric2.mtu[2])) | |
416 | return 1; | |
417 | ||
418 | return 0; // if different | |
7f57883e | 419 | } |
f9e5c9ca | 420 | |
d62a17ae | 421 | void eigrp_external_routes_refresh(struct eigrp *eigrp, int type) |
7f57883e | 422 | { |
7f57883e | 423 | } |