2 * EIGRP Network Related Functions.
3 * Copyright (C) 2013-2014
11 * This file is part of GNU Zebra.
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
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.
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
34 #include "sockunion.h"
41 extern struct zebra_privs_t eigrpd_privs
;
43 #include "eigrpd/eigrp_structs.h"
44 #include "eigrpd/eigrpd.h"
45 #include "eigrpd/eigrp_interface.h"
46 #include "eigrpd/eigrp_neighbor.h"
47 #include "eigrpd/eigrp_packet.h"
48 #include "eigrpd/eigrp_zebra.h"
49 #include "eigrpd/eigrp_vty.h"
50 #include "eigrpd/eigrp_network.h"
52 static int eigrp_network_match_iface(const struct connected
*,
53 const struct prefix
*);
54 static void eigrp_network_run_interface(struct eigrp
*, struct prefix
*,
57 int eigrp_sock_init(void)
62 if (eigrpd_privs
.change(ZPRIVS_RAISE
))
63 zlog_err("eigrp_sock_init: could not raise privs, %s",
64 safe_strerror(errno
));
66 eigrp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_EIGRPIGP
);
68 int save_errno
= errno
;
69 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
70 zlog_err("eigrp_sock_init: could not lower privs, %s",
71 safe_strerror(errno
));
72 zlog_err("eigrp_read_sock_init: socket: %s",
73 safe_strerror(save_errno
));
78 /* we will include IP header with packet */
79 ret
= setsockopt(eigrp_sock
, IPPROTO_IP
, IP_HDRINCL
, &hincl
,
82 int save_errno
= errno
;
83 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
84 zlog_err("eigrp_sock_init: could not lower privs, %s",
85 safe_strerror(errno
));
86 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
87 eigrp_sock
, safe_strerror(save_errno
));
89 #elif defined(IPTOS_PREC_INTERNETCONTROL)
90 #warning "IP_HDRINCL not available on this system"
91 #warning "using IPTOS_PREC_INTERNETCONTROL"
92 ret
= setsockopt_ipv4_tos(eigrp_sock
, IPTOS_PREC_INTERNETCONTROL
);
94 int save_errno
= errno
;
95 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
96 zlog_err("eigrpd_sock_init: could not lower privs, %s",
97 safe_strerror(errno
));
98 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", tos
,
99 eigrp_sock
, safe_strerror(save_errno
));
100 close(eigrp_sock
); /* Prevent sd leak. */
103 #else /* !IPTOS_PREC_INTERNETCONTROL */
104 #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
105 zlog_warn("IP_HDRINCL option not available");
106 #endif /* IP_HDRINCL */
108 ret
= setsockopt_ifindex(AF_INET
, eigrp_sock
, 1);
111 zlog_warn("Can't set pktinfo option for fd %d", eigrp_sock
);
113 if (eigrpd_privs
.change(ZPRIVS_LOWER
)) {
114 zlog_err("eigrp_sock_init: could not lower privs, %s",
115 safe_strerror(errno
));
121 void eigrp_adjust_sndbuflen(struct eigrp
*eigrp
, unsigned int buflen
)
124 /* Check if any work has to be done at all. */
125 if (eigrp
->maxsndbuflen
>= buflen
)
127 if (eigrpd_privs
.change(ZPRIVS_RAISE
))
128 zlog_err("%s: could not raise privs, %s", __func__
,
129 safe_strerror(errno
));
131 /* Now we try to set SO_SNDBUF to what our caller has requested
132 * (the MTU of a newly added interface). However, if the OS has
133 * truncated the actual buffer size to somewhat less size, try
134 * to detect it and update our records appropriately. The OS
135 * may allocate more buffer space, than requested, this isn't
138 setsockopt_so_sendbuf(eigrp
->fd
, buflen
);
139 newbuflen
= getsockopt_so_sendbuf(eigrp
->fd
);
140 if (newbuflen
< 0 || newbuflen
< (int)buflen
)
141 zlog_warn("%s: tried to set SO_SNDBUF to %u, but got %d",
142 __func__
, buflen
, newbuflen
);
144 eigrp
->maxsndbuflen
= (unsigned int)newbuflen
;
146 zlog_warn("%s: failed to get SO_SNDBUF", __func__
);
147 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
148 zlog_err("%s: could not lower privs, %s", __func__
,
149 safe_strerror(errno
));
152 int eigrp_if_ipmulticast(struct eigrp
*top
, struct prefix
*p
,
153 unsigned int ifindex
)
161 /* Prevent receiving self-origined multicast packets. */
162 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *)&val
,
166 "can't setsockopt IP_MULTICAST_LOOP (0) for fd %d: %s",
167 top
->fd
, safe_strerror(errno
));
169 /* Explicitly set multicast ttl to 1 -- endo. */
171 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&val
,
174 zlog_warn("can't setsockopt IP_MULTICAST_TTL (1) for fd %d: %s",
175 top
->fd
, safe_strerror(errno
));
177 ret
= setsockopt_ipv4_multicast_if(top
->fd
, p
->u
.prefix4
, ifindex
);
180 "can't setsockopt IP_MULTICAST_IF (fd %d, addr %s, "
182 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
183 safe_strerror(errno
));
188 /* Join to the EIGRP multicast group. */
189 int eigrp_if_add_allspfrouters(struct eigrp
*top
, struct prefix
*p
,
190 unsigned int ifindex
)
194 ret
= setsockopt_ipv4_multicast(
195 top
->fd
, IP_ADD_MEMBERSHIP
, p
->u
.prefix4
,
196 htonl(EIGRP_MULTICAST_ADDRESS
), ifindex
);
199 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
200 "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit "
201 "on # of multicast group memberships has been exceeded?",
202 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
203 safe_strerror(errno
));
205 zlog_debug("interface %s [%u] join EIGRP Multicast group.",
206 inet_ntoa(p
->u
.prefix4
), ifindex
);
211 int eigrp_if_drop_allspfrouters(struct eigrp
*top
, struct prefix
*p
,
212 unsigned int ifindex
)
216 ret
= setsockopt_ipv4_multicast(
217 top
->fd
, IP_DROP_MEMBERSHIP
, p
->u
.prefix4
,
218 htonl(EIGRP_MULTICAST_ADDRESS
), ifindex
);
221 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
222 "ifindex %u, AllSPFRouters): %s",
223 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
224 safe_strerror(errno
));
226 zlog_debug("interface %s [%u] leave EIGRP Multicast group.",
227 inet_ntoa(p
->u
.prefix4
), ifindex
);
232 int eigrp_network_set(struct eigrp
*eigrp
, struct prefix
*p
)
234 struct route_node
*rn
;
235 struct interface
*ifp
;
236 struct listnode
*node
;
238 rn
= route_node_get(eigrp
->networks
, (struct prefix
*)p
);
240 /* There is already same network statement. */
241 route_unlock_node(rn
);
245 struct prefix
*pref
= prefix_new();
246 PREFIX_COPY_IPV4(pref
, p
);
247 rn
->info
= (void *)pref
;
249 /* Schedule Router ID Update. */
250 if (eigrp
->router_id
== 0)
251 eigrp_router_id_update(eigrp
);
252 /* Run network config now. */
253 /* Get target interface. */
254 for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT
), node
, ifp
)) {
255 zlog_debug("Setting up %s", ifp
->name
);
256 eigrp_network_run_interface(eigrp
, p
, ifp
);
261 /* Check whether interface matches given network
262 * returns: 1, true. 0, false
264 static int eigrp_network_match_iface(const struct connected
*co
,
265 const struct prefix
*net
)
267 /* new approach: more elegant and conceptually clean */
268 return prefix_match_network_statement(net
, CONNECTED_PREFIX(co
));
271 static void eigrp_network_run_interface(struct eigrp
*eigrp
, struct prefix
*p
,
272 struct interface
*ifp
)
274 struct listnode
*cnode
;
275 struct connected
*co
;
277 /* if interface prefix is match specified prefix,
278 then create socket and join multicast group. */
279 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, co
)) {
281 if (CHECK_FLAG(co
->flags
, ZEBRA_IFA_SECONDARY
))
284 if (p
->family
== co
->address
->family
285 && !eigrp_if_table_lookup(ifp
, co
->address
)
286 && eigrp_network_match_iface(co
, p
)) {
287 struct eigrp_interface
*ei
;
289 ei
= eigrp_if_new(eigrp
, ifp
, co
->address
);
292 ei
->params
= eigrp_lookup_if_params(
293 ifp
, ei
->address
->u
.prefix4
);
295 /* Relate eigrp interface to eigrp instance. */
298 /* update network type as interface flag */
299 /* If network type is specified previously,
300 skip network type setting. */
301 ei
->type
= IF_DEF_PARAMS(ifp
)->type
;
303 /* if router_id is not configured, dont bring up
305 * eigrp_router_id_update() will call eigrp_if_update
306 * whenever r-id is configured instead.
308 if (if_is_operative(ifp
))
314 void eigrp_if_update(struct interface
*ifp
)
316 struct listnode
*node
, *nnode
;
317 struct route_node
*rn
;
321 * In the event there are multiple eigrp autonymnous systems running,
322 * we need to check eac one and add the interface as approperate
324 for (ALL_LIST_ELEMENTS(eigrp_om
->eigrp
, node
, nnode
, eigrp
)) {
325 /* EIGRP must be on and Router-ID must be configured. */
326 if (!eigrp
|| eigrp
->router_id
== 0)
329 /* Run each network for this interface. */
330 for (rn
= route_top(eigrp
->networks
); rn
; rn
= route_next(rn
))
331 if (rn
->info
!= NULL
) {
332 eigrp_network_run_interface(eigrp
, &rn
->p
, ifp
);
337 int eigrp_network_unset(struct eigrp
*eigrp
, struct prefix
*p
)
339 struct route_node
*rn
;
340 struct listnode
*node
, *nnode
;
341 struct eigrp_interface
*ei
;
344 rn
= route_node_lookup(eigrp
->networks
, p
);
349 route_unlock_node(rn
);
351 if (!IPV4_ADDR_SAME(&pref
->u
.prefix4
, &p
->u
.prefix4
))
354 prefix_ipv4_free(rn
->info
);
356 route_unlock_node(rn
); /* initial reference */
358 /* Find interfaces that not configured already. */
359 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei
)) {
361 struct connected
*co
= ei
->connected
;
363 for (rn
= route_top(eigrp
->networks
); rn
; rn
= route_next(rn
)) {
364 if (rn
->info
== NULL
)
367 if (eigrp_network_match_iface(co
, &rn
->p
)) {
369 route_unlock_node(rn
);
375 eigrp_if_free(ei
, INTERFACE_DOWN_BY_VTY
);
382 u_int32_t
eigrp_calculate_metrics(struct eigrp
*eigrp
,
383 struct eigrp_metrics metric
)
385 uint64_t temp_metric
;
388 if (metric
.delay
== EIGRP_MAX_METRIC
)
389 return EIGRP_MAX_METRIC
;
392 // {K1*BW+[(K2*BW)/(256-load)]+(K3*delay)}*{K5/(reliability+K4)}
394 if (eigrp
->k_values
[0])
395 temp_metric
+= (eigrp
->k_values
[0] * metric
.bandwidth
);
396 if (eigrp
->k_values
[1])
397 temp_metric
+= ((eigrp
->k_values
[1] * metric
.bandwidth
)
398 / (256 - metric
.load
));
399 if (eigrp
->k_values
[2])
400 temp_metric
+= (eigrp
->k_values
[2] * metric
.delay
);
401 if (eigrp
->k_values
[3] && !eigrp
->k_values
[4])
402 temp_metric
*= eigrp
->k_values
[3];
403 if (!eigrp
->k_values
[3] && eigrp
->k_values
[4])
404 temp_metric
*= (eigrp
->k_values
[4] / metric
.reliability
);
405 if (eigrp
->k_values
[3] && eigrp
->k_values
[4])
406 temp_metric
*= ((eigrp
->k_values
[4] / metric
.reliability
)
407 + eigrp
->k_values
[3]);
409 if (temp_metric
<= EIGRP_MAX_METRIC
)
410 return (u_int32_t
)temp_metric
;
412 return EIGRP_MAX_METRIC
;
415 u_int32_t
eigrp_calculate_total_metrics(struct eigrp
*eigrp
,
416 struct eigrp_neighbor_entry
*entry
)
418 entry
->total_metric
= entry
->reported_metric
;
419 uint64_t temp_delay
= (uint64_t)entry
->total_metric
.delay
420 + (uint64_t)eigrp_delay_to_scaled(
421 EIGRP_IF_PARAM(entry
->ei
, delay
));
422 entry
->total_metric
.delay
= temp_delay
> EIGRP_MAX_METRIC
424 : (u_int32_t
)temp_delay
;
427 eigrp_bandwidth_to_scaled(EIGRP_IF_PARAM(entry
->ei
, bandwidth
));
428 entry
->total_metric
.bandwidth
= entry
->total_metric
.bandwidth
> bw
430 : entry
->total_metric
.bandwidth
;
432 return eigrp_calculate_metrics(eigrp
, entry
->total_metric
);
435 u_char
eigrp_metrics_is_same(struct eigrp_metrics metric1
,
436 struct eigrp_metrics metric2
)
438 if ((metric1
.bandwidth
== metric2
.bandwidth
)
439 && (metric1
.delay
== metric2
.delay
)
440 && (metric1
.hop_count
== metric2
.hop_count
)
441 && (metric1
.load
== metric2
.load
)
442 && (metric1
.reliability
== metric2
.reliability
)
443 && (metric1
.mtu
[0] == metric2
.mtu
[0])
444 && (metric1
.mtu
[1] == metric2
.mtu
[1])
445 && (metric1
.mtu
[2] == metric2
.mtu
[2]))
448 return 0; // if different
451 void eigrp_external_routes_refresh(struct eigrp
*eigrp
, int type
)