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 #include "eigrpd/eigrp_structs.h"
42 #include "eigrpd/eigrpd.h"
43 #include "eigrpd/eigrp_interface.h"
44 #include "eigrpd/eigrp_neighbor.h"
45 #include "eigrpd/eigrp_packet.h"
46 #include "eigrpd/eigrp_zebra.h"
47 #include "eigrpd/eigrp_vty.h"
48 #include "eigrpd/eigrp_network.h"
50 static int eigrp_network_match_iface(const struct connected
*,
51 const struct prefix
*);
52 static void eigrp_network_run_interface(struct eigrp
*, struct prefix
*,
55 int eigrp_sock_init(void)
60 if (eigrpd_privs
.change(ZPRIVS_RAISE
))
61 zlog_err("eigrp_sock_init: could not raise privs, %s",
62 safe_strerror(errno
));
64 eigrp_sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_EIGRPIGP
);
66 int save_errno
= errno
;
67 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
68 zlog_err("eigrp_sock_init: could not lower privs, %s",
69 safe_strerror(errno
));
70 zlog_err("eigrp_read_sock_init: socket: %s",
71 safe_strerror(save_errno
));
76 /* we will include IP header with packet */
77 ret
= setsockopt(eigrp_sock
, IPPROTO_IP
, IP_HDRINCL
, &hincl
,
80 int save_errno
= errno
;
81 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
82 zlog_err("eigrp_sock_init: could not lower privs, %s",
83 safe_strerror(errno
));
84 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
85 eigrp_sock
, safe_strerror(save_errno
));
87 #elif defined(IPTOS_PREC_INTERNETCONTROL)
88 #warning "IP_HDRINCL not available on this system"
89 #warning "using IPTOS_PREC_INTERNETCONTROL"
90 ret
= setsockopt_ipv4_tos(eigrp_sock
, IPTOS_PREC_INTERNETCONTROL
);
92 int save_errno
= errno
;
93 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
94 zlog_err("eigrpd_sock_init: could not lower privs, %s",
95 safe_strerror(errno
));
96 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", tos
,
97 eigrp_sock
, safe_strerror(save_errno
));
98 close(eigrp_sock
); /* Prevent sd leak. */
101 #else /* !IPTOS_PREC_INTERNETCONTROL */
102 #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
103 zlog_warn("IP_HDRINCL option not available");
104 #endif /* IP_HDRINCL */
106 ret
= setsockopt_ifindex(AF_INET
, eigrp_sock
, 1);
109 zlog_warn("Can't set pktinfo option for fd %d", eigrp_sock
);
111 if (eigrpd_privs
.change(ZPRIVS_LOWER
)) {
112 zlog_err("eigrp_sock_init: could not lower privs, %s",
113 safe_strerror(errno
));
119 void eigrp_adjust_sndbuflen(struct eigrp
*eigrp
, unsigned int buflen
)
122 /* Check if any work has to be done at all. */
123 if (eigrp
->maxsndbuflen
>= buflen
)
125 if (eigrpd_privs
.change(ZPRIVS_RAISE
))
126 zlog_err("%s: could not raise privs, %s", __func__
,
127 safe_strerror(errno
));
129 /* Now we try to set SO_SNDBUF to what our caller has requested
130 * (the MTU of a newly added interface). However, if the OS has
131 * truncated the actual buffer size to somewhat less size, try
132 * to detect it and update our records appropriately. The OS
133 * may allocate more buffer space, than requested, this isn't
136 setsockopt_so_sendbuf(eigrp
->fd
, buflen
);
137 newbuflen
= getsockopt_so_sendbuf(eigrp
->fd
);
138 if (newbuflen
< 0 || newbuflen
< (int)buflen
)
139 zlog_warn("%s: tried to set SO_SNDBUF to %u, but got %d",
140 __func__
, buflen
, newbuflen
);
142 eigrp
->maxsndbuflen
= (unsigned int)newbuflen
;
144 zlog_warn("%s: failed to get SO_SNDBUF", __func__
);
145 if (eigrpd_privs
.change(ZPRIVS_LOWER
))
146 zlog_err("%s: could not lower privs, %s", __func__
,
147 safe_strerror(errno
));
150 int eigrp_if_ipmulticast(struct eigrp
*top
, struct prefix
*p
,
151 unsigned int ifindex
)
159 /* Prevent receiving self-origined multicast packets. */
160 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *)&val
,
164 "can't setsockopt IP_MULTICAST_LOOP (0) for fd %d: %s",
165 top
->fd
, safe_strerror(errno
));
167 /* Explicitly set multicast ttl to 1 -- endo. */
169 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&val
,
172 zlog_warn("can't setsockopt IP_MULTICAST_TTL (1) for fd %d: %s",
173 top
->fd
, safe_strerror(errno
));
175 ret
= setsockopt_ipv4_multicast_if(top
->fd
, p
->u
.prefix4
, ifindex
);
178 "can't setsockopt IP_MULTICAST_IF (fd %d, addr %s, "
180 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
181 safe_strerror(errno
));
186 /* Join to the EIGRP multicast group. */
187 int eigrp_if_add_allspfrouters(struct eigrp
*top
, struct prefix
*p
,
188 unsigned int ifindex
)
192 ret
= setsockopt_ipv4_multicast(
193 top
->fd
, IP_ADD_MEMBERSHIP
, p
->u
.prefix4
,
194 htonl(EIGRP_MULTICAST_ADDRESS
), ifindex
);
197 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
198 "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit "
199 "on # of multicast group memberships has been exceeded?",
200 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
201 safe_strerror(errno
));
203 zlog_debug("interface %s [%u] join EIGRP Multicast group.",
204 inet_ntoa(p
->u
.prefix4
), ifindex
);
209 int eigrp_if_drop_allspfrouters(struct eigrp
*top
, struct prefix
*p
,
210 unsigned int ifindex
)
214 ret
= setsockopt_ipv4_multicast(
215 top
->fd
, IP_DROP_MEMBERSHIP
, p
->u
.prefix4
,
216 htonl(EIGRP_MULTICAST_ADDRESS
), ifindex
);
219 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
220 "ifindex %u, AllSPFRouters): %s",
221 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
222 safe_strerror(errno
));
224 zlog_debug("interface %s [%u] leave EIGRP Multicast group.",
225 inet_ntoa(p
->u
.prefix4
), ifindex
);
230 int eigrp_network_set(struct eigrp
*eigrp
, struct prefix
*p
)
232 struct vrf
*vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
233 struct route_node
*rn
;
234 struct interface
*ifp
;
236 rn
= route_node_get(eigrp
->networks
, (struct prefix
*)p
);
238 /* There is already same network statement. */
239 route_unlock_node(rn
);
243 struct prefix
*pref
= prefix_new();
244 PREFIX_COPY_IPV4(pref
, p
);
245 rn
->info
= (void *)pref
;
247 /* Schedule Router ID Update. */
248 if (eigrp
->router_id
== 0)
249 eigrp_router_id_update(eigrp
);
250 /* Run network config now. */
251 /* Get target interface. */
252 FOR_ALL_INTERFACES (vrf
, ifp
) {
253 zlog_debug("Setting up %s", ifp
->name
);
254 eigrp_network_run_interface(eigrp
, p
, ifp
);
259 /* Check whether interface matches given network
260 * returns: 1, true. 0, false
262 static int eigrp_network_match_iface(const struct connected
*co
,
263 const struct prefix
*net
)
265 /* new approach: more elegant and conceptually clean */
266 return prefix_match_network_statement(net
, CONNECTED_PREFIX(co
));
269 static void eigrp_network_run_interface(struct eigrp
*eigrp
, struct prefix
*p
,
270 struct interface
*ifp
)
272 struct eigrp_interface
*ei
;
273 struct listnode
*cnode
;
274 struct connected
*co
;
276 /* if interface prefix is match specified prefix,
277 then create socket and join multicast group. */
278 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, co
)) {
280 if (CHECK_FLAG(co
->flags
, ZEBRA_IFA_SECONDARY
))
283 if (p
->family
== co
->address
->family
285 && eigrp_network_match_iface(co
, p
)) {
287 ei
= eigrp_if_new(eigrp
, ifp
, co
->address
);
290 /* Relate eigrp interface to eigrp instance. */
293 /* if router_id is not configured, dont bring up
295 * eigrp_router_id_update() will call eigrp_if_update
296 * whenever r-id is configured instead.
298 if (if_is_operative(ifp
))
304 void eigrp_if_update(struct interface
*ifp
)
306 struct listnode
*node
, *nnode
;
307 struct route_node
*rn
;
311 * In the event there are multiple eigrp autonymnous systems running,
312 * we need to check eac one and add the interface as approperate
314 for (ALL_LIST_ELEMENTS(eigrp_om
->eigrp
, node
, nnode
, eigrp
)) {
315 /* EIGRP must be on and Router-ID must be configured. */
316 if (!eigrp
|| eigrp
->router_id
== 0)
319 /* Run each network for this interface. */
320 for (rn
= route_top(eigrp
->networks
); rn
; rn
= route_next(rn
))
321 if (rn
->info
!= NULL
) {
322 eigrp_network_run_interface(eigrp
, &rn
->p
, ifp
);
327 int eigrp_network_unset(struct eigrp
*eigrp
, struct prefix
*p
)
329 struct route_node
*rn
;
330 struct listnode
*node
, *nnode
;
331 struct eigrp_interface
*ei
;
334 rn
= route_node_lookup(eigrp
->networks
, p
);
339 route_unlock_node(rn
);
341 if (!IPV4_ADDR_SAME(&pref
->u
.prefix4
, &p
->u
.prefix4
))
344 prefix_ipv4_free(rn
->info
);
346 route_unlock_node(rn
); /* initial reference */
348 /* Find interfaces that not configured already. */
349 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei
)) {
351 struct connected
*co
= ei
->connected
;
353 for (rn
= route_top(eigrp
->networks
); rn
; rn
= route_next(rn
)) {
354 if (rn
->info
== NULL
)
357 if (eigrp_network_match_iface(co
, &rn
->p
)) {
359 route_unlock_node(rn
);
365 eigrp_if_free(ei
, INTERFACE_DOWN_BY_VTY
);
372 u_int32_t
eigrp_calculate_metrics(struct eigrp
*eigrp
,
373 struct eigrp_metrics metric
)
375 uint64_t temp_metric
;
378 if (metric
.delay
== EIGRP_MAX_METRIC
)
379 return EIGRP_MAX_METRIC
;
382 // {K1*BW+[(K2*BW)/(256-load)]+(K3*delay)}*{K5/(reliability+K4)}
384 if (eigrp
->k_values
[0])
385 temp_metric
+= (eigrp
->k_values
[0] * metric
.bandwidth
);
386 if (eigrp
->k_values
[1])
387 temp_metric
+= ((eigrp
->k_values
[1] * metric
.bandwidth
)
388 / (256 - metric
.load
));
389 if (eigrp
->k_values
[2])
390 temp_metric
+= (eigrp
->k_values
[2] * metric
.delay
);
391 if (eigrp
->k_values
[3] && !eigrp
->k_values
[4])
392 temp_metric
*= eigrp
->k_values
[3];
393 if (!eigrp
->k_values
[3] && eigrp
->k_values
[4])
394 temp_metric
*= (eigrp
->k_values
[4] / metric
.reliability
);
395 if (eigrp
->k_values
[3] && eigrp
->k_values
[4])
396 temp_metric
*= ((eigrp
->k_values
[4] / metric
.reliability
)
397 + eigrp
->k_values
[3]);
399 if (temp_metric
<= EIGRP_MAX_METRIC
)
400 return (u_int32_t
)temp_metric
;
402 return EIGRP_MAX_METRIC
;
405 u_int32_t
eigrp_calculate_total_metrics(struct eigrp
*eigrp
,
406 struct eigrp_nexthop_entry
*entry
)
408 struct eigrp_interface
*ei
= entry
->ei
;
410 entry
->total_metric
= entry
->reported_metric
;
411 uint64_t temp_delay
= (uint64_t)entry
->total_metric
.delay
412 + (uint64_t)eigrp_delay_to_scaled(ei
->params
.delay
);
413 entry
->total_metric
.delay
= temp_delay
> EIGRP_MAX_METRIC
415 : (u_int32_t
)temp_delay
;
418 eigrp_bandwidth_to_scaled(ei
->params
.bandwidth
);
419 entry
->total_metric
.bandwidth
= entry
->total_metric
.bandwidth
> bw
421 : entry
->total_metric
.bandwidth
;
423 return eigrp_calculate_metrics(eigrp
, entry
->total_metric
);
426 u_char
eigrp_metrics_is_same(struct eigrp_metrics metric1
,
427 struct eigrp_metrics metric2
)
429 if ((metric1
.bandwidth
== metric2
.bandwidth
)
430 && (metric1
.delay
== metric2
.delay
)
431 && (metric1
.hop_count
== metric2
.hop_count
)
432 && (metric1
.load
== metric2
.load
)
433 && (metric1
.reliability
== metric2
.reliability
)
434 && (metric1
.mtu
[0] == metric2
.mtu
[0])
435 && (metric1
.mtu
[1] == metric2
.mtu
[1])
436 && (metric1
.mtu
[2] == metric2
.mtu
[2]))
439 return 0; // if different
442 void eigrp_external_routes_refresh(struct eigrp
*eigrp
, int type
)