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"
40 #include "lib_errors.h"
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"
51 static int eigrp_network_match_iface(const struct prefix
*connected_prefix
,
52 const struct prefix
*prefix
);
53 static void eigrp_network_run_interface(struct eigrp
*, struct prefix
*,
56 int eigrp_sock_init(struct vrf
*vrf
)
67 frr_with_privs(&eigrpd_privs
) {
68 eigrp_sock
= vrf_socket(
69 AF_INET
, SOCK_RAW
, IPPROTO_EIGRPIGP
, vrf
->vrf_id
,
70 vrf
->vrf_id
!= VRF_DEFAULT
? vrf
->name
: NULL
);
72 zlog_err("eigrp_read_sock_init: socket: %s",
73 safe_strerror(errno
));
78 /* we will include IP header with packet */
79 ret
= setsockopt(eigrp_sock
, IPPROTO_IP
, IP_HDRINCL
, &hincl
,
82 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
83 eigrp_sock
, safe_strerror(errno
));
85 #elif defined(IPTOS_PREC_INTERNETCONTROL)
86 #warning "IP_HDRINCL not available on this system"
87 #warning "using IPTOS_PREC_INTERNETCONTROL"
88 ret
= setsockopt_ipv4_tos(eigrp_sock
,
89 IPTOS_PREC_INTERNETCONTROL
);
91 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s",
92 tos
, eigrp_sock
, safe_strerror(errno
));
93 close(eigrp_sock
); /* Prevent sd leak. */
96 #else /* !IPTOS_PREC_INTERNETCONTROL */
97 #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
98 zlog_warn("IP_HDRINCL option not available");
99 #endif /* IP_HDRINCL */
101 ret
= setsockopt_ifindex(AF_INET
, eigrp_sock
, 1);
103 zlog_warn("Can't set pktinfo option for fd %d",
110 void eigrp_adjust_sndbuflen(struct eigrp
*eigrp
, unsigned int buflen
)
113 /* Check if any work has to be done at all. */
114 if (eigrp
->maxsndbuflen
>= buflen
)
117 /* Now we try to set SO_SNDBUF to what our caller has requested
118 * (the MTU of a newly added interface). However, if the OS has
119 * truncated the actual buffer size to somewhat less size, try
120 * to detect it and update our records appropriately. The OS
121 * may allocate more buffer space, than requested, this isn't
124 setsockopt_so_sendbuf(eigrp
->fd
, buflen
);
125 newbuflen
= getsockopt_so_sendbuf(eigrp
->fd
);
126 if (newbuflen
< 0 || newbuflen
< (int)buflen
)
127 zlog_warn("%s: tried to set SO_SNDBUF to %u, but got %d",
128 __func__
, buflen
, newbuflen
);
130 eigrp
->maxsndbuflen
= (unsigned int)newbuflen
;
132 zlog_warn("%s: failed to get SO_SNDBUF", __func__
);
135 int eigrp_if_ipmulticast(struct eigrp
*top
, struct prefix
*p
,
136 unsigned int ifindex
)
144 /* Prevent receiving self-origined multicast packets. */
145 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *)&val
,
149 "can't setsockopt IP_MULTICAST_LOOP (0) for fd %d: %s",
150 top
->fd
, safe_strerror(errno
));
152 /* Explicitly set multicast ttl to 1 -- endo. */
154 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&val
,
157 zlog_warn("can't setsockopt IP_MULTICAST_TTL (1) for fd %d: %s",
158 top
->fd
, safe_strerror(errno
));
160 ret
= setsockopt_ipv4_multicast_if(top
->fd
, p
->u
.prefix4
, ifindex
);
163 "can't setsockopt IP_MULTICAST_IF (fd %d, addr %pI4, ifindex %u): %s",
164 top
->fd
, &p
->u
.prefix4
, ifindex
, safe_strerror(errno
));
169 /* Join to the EIGRP multicast group. */
170 int eigrp_if_add_allspfrouters(struct eigrp
*top
, struct prefix
*p
,
171 unsigned int ifindex
)
175 ret
= setsockopt_ipv4_multicast(
176 top
->fd
, IP_ADD_MEMBERSHIP
, p
->u
.prefix4
,
177 htonl(EIGRP_MULTICAST_ADDRESS
), ifindex
);
180 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllSPFRouters): %s; perhaps a kernel limit on # of multicast group memberships has been exceeded?",
181 top
->fd
, &p
->u
.prefix4
, ifindex
, safe_strerror(errno
));
183 zlog_debug("interface %pI4 [%u] join EIGRP Multicast group.",
184 &p
->u
.prefix4
, ifindex
);
189 int eigrp_if_drop_allspfrouters(struct eigrp
*top
, struct prefix
*p
,
190 unsigned int ifindex
)
194 ret
= setsockopt_ipv4_multicast(
195 top
->fd
, IP_DROP_MEMBERSHIP
, p
->u
.prefix4
,
196 htonl(EIGRP_MULTICAST_ADDRESS
), ifindex
);
199 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllSPFRouters): %s",
200 top
->fd
, &p
->u
.prefix4
, ifindex
, safe_strerror(errno
));
202 zlog_debug("interface %pI4 [%u] leave EIGRP Multicast group.",
203 &p
->u
.prefix4
, ifindex
);
208 int eigrp_network_set(struct eigrp
*eigrp
, struct prefix
*p
)
210 struct vrf
*vrf
= vrf_lookup_by_id(eigrp
->vrf_id
);
211 struct route_node
*rn
;
212 struct interface
*ifp
;
214 rn
= route_node_get(eigrp
->networks
, p
);
216 /* There is already same network statement. */
217 route_unlock_node(rn
);
221 struct prefix
*pref
= prefix_new();
222 prefix_copy(pref
, p
);
223 rn
->info
= (void *)pref
;
225 /* Schedule Router ID Update. */
226 if (eigrp
->router_id
.s_addr
== INADDR_ANY
)
227 eigrp_router_id_update(eigrp
);
228 /* Run network config now. */
229 /* Get target interface. */
230 FOR_ALL_INTERFACES (vrf
, ifp
) {
231 zlog_debug("Setting up %s", ifp
->name
);
232 eigrp_network_run_interface(eigrp
, p
, ifp
);
237 /* Check whether interface matches given network
238 * returns: 1, true. 0, false
240 static int eigrp_network_match_iface(const struct prefix
*co_prefix
,
241 const struct prefix
*net
)
243 /* new approach: more elegant and conceptually clean */
244 return prefix_match_network_statement(net
, co_prefix
);
247 static void eigrp_network_run_interface(struct eigrp
*eigrp
, struct prefix
*p
,
248 struct interface
*ifp
)
250 struct eigrp_interface
*ei
;
251 struct listnode
*cnode
;
252 struct connected
*co
;
254 /* if interface prefix is match specified prefix,
255 then create socket and join multicast group. */
256 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, co
)) {
258 if (CHECK_FLAG(co
->flags
, ZEBRA_IFA_SECONDARY
))
261 if (p
->family
== co
->address
->family
&& !ifp
->info
262 && eigrp_network_match_iface(co
->address
, p
)) {
264 ei
= eigrp_if_new(eigrp
, ifp
, co
->address
);
266 /* Relate eigrp interface to eigrp instance. */
269 /* if router_id is not configured, dont bring up
271 * eigrp_router_id_update() will call eigrp_if_update
272 * whenever r-id is configured instead.
274 if (if_is_operative(ifp
))
280 void eigrp_if_update(struct interface
*ifp
)
282 struct listnode
*node
, *nnode
;
283 struct route_node
*rn
;
287 * In the event there are multiple eigrp autonymnous systems running,
288 * we need to check eac one and add the interface as approperate
290 for (ALL_LIST_ELEMENTS(eigrp_om
->eigrp
, node
, nnode
, eigrp
)) {
291 if (ifp
->vrf
->vrf_id
!= eigrp
->vrf_id
)
294 /* EIGRP must be on and Router-ID must be configured. */
295 if (eigrp
->router_id
.s_addr
== INADDR_ANY
)
298 /* Run each network for this interface. */
299 for (rn
= route_top(eigrp
->networks
); rn
; rn
= route_next(rn
))
300 if (rn
->info
!= NULL
) {
301 eigrp_network_run_interface(eigrp
, &rn
->p
, ifp
);
306 int eigrp_network_unset(struct eigrp
*eigrp
, struct prefix
*p
)
308 struct route_node
*rn
;
309 struct listnode
*node
, *nnode
;
310 struct eigrp_interface
*ei
;
313 rn
= route_node_lookup(eigrp
->networks
, p
);
318 route_unlock_node(rn
);
320 if (!IPV4_ADDR_SAME(&pref
->u
.prefix4
, &p
->u
.prefix4
))
323 prefix_ipv4_free((struct prefix_ipv4
**)&rn
->info
);
324 route_unlock_node(rn
); /* initial reference */
326 /* Find interfaces that not configured already. */
327 for (ALL_LIST_ELEMENTS(eigrp
->eiflist
, node
, nnode
, ei
)) {
330 for (rn
= route_top(eigrp
->networks
); rn
; rn
= route_next(rn
)) {
331 if (rn
->info
== NULL
)
334 if (eigrp_network_match_iface(&ei
->address
, &rn
->p
)) {
336 route_unlock_node(rn
);
342 eigrp_if_free(ei
, INTERFACE_DOWN_BY_VTY
);
349 void eigrp_external_routes_refresh(struct eigrp
*eigrp
, int type
)