]> git.proxmox.com Git - mirror_frr.git/blame_incremental - eigrpd/eigrp_network.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / eigrpd / eigrp_network.c
... / ...
CommitLineData
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * EIGRP Network Related Functions.
4 * Copyright (C) 2013-2014
5 * Authors:
6 * Donnie Savage
7 * Jan Janovic
8 * Matej Perina
9 * Peter Orsag
10 * Peter Paluch
11 */
12
13#include <zebra.h>
14
15#include "frrevent.h"
16#include "linklist.h"
17#include "prefix.h"
18#include "if.h"
19#include "sockunion.h"
20#include "log.h"
21#include "sockopt.h"
22#include "privs.h"
23#include "table.h"
24#include "vty.h"
25#include "lib_errors.h"
26
27#include "eigrpd/eigrp_structs.h"
28#include "eigrpd/eigrpd.h"
29#include "eigrpd/eigrp_interface.h"
30#include "eigrpd/eigrp_neighbor.h"
31#include "eigrpd/eigrp_packet.h"
32#include "eigrpd/eigrp_zebra.h"
33#include "eigrpd/eigrp_vty.h"
34#include "eigrpd/eigrp_network.h"
35
36static int eigrp_network_match_iface(const struct prefix *connected_prefix,
37 const struct prefix *prefix);
38static void eigrp_network_run_interface(struct eigrp *, struct prefix *,
39 struct interface *);
40
41int eigrp_sock_init(struct vrf *vrf)
42{
43 int eigrp_sock = -1;
44 int ret;
45#ifdef IP_HDRINCL
46 int hincl = 1;
47#endif
48
49 if (!vrf)
50 return eigrp_sock;
51
52 frr_with_privs(&eigrpd_privs) {
53 eigrp_sock = vrf_socket(
54 AF_INET, SOCK_RAW, IPPROTO_EIGRPIGP, vrf->vrf_id,
55 vrf->vrf_id != VRF_DEFAULT ? vrf->name : NULL);
56 if (eigrp_sock < 0) {
57 zlog_err("%s: socket: %s",
58 __func__, safe_strerror(errno));
59 exit(1);
60 }
61
62#ifdef IP_HDRINCL
63 /* we will include IP header with packet */
64 ret = setsockopt(eigrp_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
65 sizeof(hincl));
66 if (ret < 0) {
67 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
68 eigrp_sock, safe_strerror(errno));
69 }
70#elif defined(IPTOS_PREC_INTERNETCONTROL)
71#warning "IP_HDRINCL not available on this system"
72#warning "using IPTOS_PREC_INTERNETCONTROL"
73 ret = setsockopt_ipv4_tos(eigrp_sock,
74 IPTOS_PREC_INTERNETCONTROL);
75 if (ret < 0) {
76 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s",
77 tos, eigrp_sock, safe_strerror(errno));
78 close(eigrp_sock); /* Prevent sd leak. */
79 return ret;
80 }
81#else /* !IPTOS_PREC_INTERNETCONTROL */
82#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
83 zlog_warn("IP_HDRINCL option not available");
84#endif /* IP_HDRINCL */
85
86 ret = setsockopt_ifindex(AF_INET, eigrp_sock, 1);
87 if (ret < 0)
88 zlog_warn("Can't set pktinfo option for fd %d",
89 eigrp_sock);
90 }
91
92 return eigrp_sock;
93}
94
95void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen)
96{
97 int newbuflen;
98 /* Check if any work has to be done at all. */
99 if (eigrp->maxsndbuflen >= buflen)
100 return;
101
102 /* Now we try to set SO_SNDBUF to what our caller has requested
103 * (the MTU of a newly added interface). However, if the OS has
104 * truncated the actual buffer size to somewhat less size, try
105 * to detect it and update our records appropriately. The OS
106 * may allocate more buffer space, than requested, this isn't
107 * a error.
108 */
109 setsockopt_so_sendbuf(eigrp->fd, buflen);
110 newbuflen = getsockopt_so_sendbuf(eigrp->fd);
111 if (newbuflen < 0 || newbuflen < (int)buflen)
112 zlog_warn("%s: tried to set SO_SNDBUF to %u, but got %d",
113 __func__, buflen, newbuflen);
114 if (newbuflen >= 0)
115 eigrp->maxsndbuflen = (unsigned int)newbuflen;
116 else
117 zlog_warn("%s: failed to get SO_SNDBUF", __func__);
118}
119
120int eigrp_if_ipmulticast(struct eigrp *top, struct prefix *p,
121 unsigned int ifindex)
122{
123 uint8_t val;
124 int ret, len;
125
126 val = 0;
127 len = sizeof(val);
128
129 /* Prevent receiving self-origined multicast packets. */
130 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val,
131 len);
132 if (ret < 0)
133 zlog_warn(
134 "can't setsockopt IP_MULTICAST_LOOP (0) for fd %d: %s",
135 top->fd, safe_strerror(errno));
136
137 /* Explicitly set multicast ttl to 1 -- endo. */
138 val = 1;
139 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val,
140 len);
141 if (ret < 0)
142 zlog_warn("can't setsockopt IP_MULTICAST_TTL (1) for fd %d: %s",
143 top->fd, safe_strerror(errno));
144
145 ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex);
146 if (ret < 0)
147 zlog_warn(
148 "can't setsockopt IP_MULTICAST_IF (fd %d, addr %pI4, ifindex %u): %s",
149 top->fd, &p->u.prefix4, ifindex, safe_strerror(errno));
150
151 return ret;
152}
153
154/* Join to the EIGRP multicast group. */
155int eigrp_if_add_allspfrouters(struct eigrp *top, struct prefix *p,
156 unsigned int ifindex)
157{
158 int ret;
159
160 ret = setsockopt_ipv4_multicast(
161 top->fd, IP_ADD_MEMBERSHIP, p->u.prefix4,
162 htonl(EIGRP_MULTICAST_ADDRESS), ifindex);
163 if (ret < 0)
164 zlog_warn(
165 "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?",
166 top->fd, &p->u.prefix4, ifindex, safe_strerror(errno));
167 else
168 zlog_debug("interface %pI4 [%u] join EIGRP Multicast group.",
169 &p->u.prefix4, ifindex);
170
171 return ret;
172}
173
174int eigrp_if_drop_allspfrouters(struct eigrp *top, struct prefix *p,
175 unsigned int ifindex)
176{
177 int ret;
178
179 ret = setsockopt_ipv4_multicast(
180 top->fd, IP_DROP_MEMBERSHIP, p->u.prefix4,
181 htonl(EIGRP_MULTICAST_ADDRESS), ifindex);
182 if (ret < 0)
183 zlog_warn(
184 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %pI4, ifindex %u, AllSPFRouters): %s",
185 top->fd, &p->u.prefix4, ifindex, safe_strerror(errno));
186 else
187 zlog_debug("interface %pI4 [%u] leave EIGRP Multicast group.",
188 &p->u.prefix4, ifindex);
189
190 return ret;
191}
192
193int eigrp_network_set(struct eigrp *eigrp, struct prefix *p)
194{
195 struct vrf *vrf = vrf_lookup_by_id(eigrp->vrf_id);
196 struct route_node *rn;
197 struct interface *ifp;
198
199 rn = route_node_get(eigrp->networks, p);
200 if (rn->info) {
201 /* There is already same network statement. */
202 route_unlock_node(rn);
203 return 0;
204 }
205
206 struct prefix *pref = prefix_new();
207 prefix_copy(pref, p);
208 rn->info = (void *)pref;
209
210 /* Schedule Router ID Update. */
211 if (eigrp->router_id.s_addr == INADDR_ANY)
212 eigrp_router_id_update(eigrp);
213 /* Run network config now. */
214 /* Get target interface. */
215 FOR_ALL_INTERFACES (vrf, ifp) {
216 zlog_debug("Setting up %s", ifp->name);
217 eigrp_network_run_interface(eigrp, p, ifp);
218 }
219 return 1;
220}
221
222/* Check whether interface matches given network
223 * returns: 1, true. 0, false
224 */
225static int eigrp_network_match_iface(const struct prefix *co_prefix,
226 const struct prefix *net)
227{
228 /* new approach: more elegant and conceptually clean */
229 return prefix_match_network_statement(net, co_prefix);
230}
231
232static void eigrp_network_run_interface(struct eigrp *eigrp, struct prefix *p,
233 struct interface *ifp)
234{
235 struct eigrp_interface *ei;
236 struct listnode *cnode;
237 struct connected *co;
238
239 /* if interface prefix is match specified prefix,
240 then create socket and join multicast group. */
241 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, co)) {
242
243 if (CHECK_FLAG(co->flags, ZEBRA_IFA_SECONDARY))
244 continue;
245
246 if (p->family == co->address->family && !ifp->info
247 && eigrp_network_match_iface(co->address, p)) {
248
249 ei = eigrp_if_new(eigrp, ifp, co->address);
250
251 /* Relate eigrp interface to eigrp instance. */
252 ei->eigrp = eigrp;
253
254 /* if router_id is not configured, dont bring up
255 * interfaces.
256 * eigrp_router_id_update() will call eigrp_if_update
257 * whenever r-id is configured instead.
258 */
259 if (if_is_operative(ifp))
260 eigrp_if_up(ei);
261 }
262 }
263}
264
265void eigrp_if_update(struct interface *ifp)
266{
267 struct listnode *node, *nnode;
268 struct route_node *rn;
269 struct eigrp *eigrp;
270
271 /*
272 * In the event there are multiple eigrp autonymnous systems running,
273 * we need to check eac one and add the interface as approperate
274 */
275 for (ALL_LIST_ELEMENTS(eigrp_om->eigrp, node, nnode, eigrp)) {
276 if (ifp->vrf->vrf_id != eigrp->vrf_id)
277 continue;
278
279 /* EIGRP must be on and Router-ID must be configured. */
280 if (eigrp->router_id.s_addr == INADDR_ANY)
281 continue;
282
283 /* Run each network for this interface. */
284 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn))
285 if (rn->info != NULL) {
286 eigrp_network_run_interface(eigrp, &rn->p, ifp);
287 }
288 }
289}
290
291int eigrp_network_unset(struct eigrp *eigrp, struct prefix *p)
292{
293 struct route_node *rn;
294 struct listnode *node, *nnode;
295 struct eigrp_interface *ei;
296 struct prefix *pref;
297
298 rn = route_node_lookup(eigrp->networks, p);
299 if (rn == NULL)
300 return 0;
301
302 pref = rn->info;
303 route_unlock_node(rn);
304
305 if (!IPV4_ADDR_SAME(&pref->u.prefix4, &p->u.prefix4))
306 return 0;
307
308 prefix_ipv4_free((struct prefix_ipv4 **)&rn->info);
309 route_unlock_node(rn); /* initial reference */
310
311 /* Find interfaces that not configured already. */
312 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei)) {
313 bool found = false;
314
315 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn)) {
316 if (rn->info == NULL)
317 continue;
318
319 if (eigrp_network_match_iface(&ei->address, &rn->p)) {
320 found = true;
321 route_unlock_node(rn);
322 break;
323 }
324 }
325
326 if (!found) {
327 eigrp_if_free(ei, INTERFACE_DOWN_BY_VTY);
328 }
329 }
330
331 return 1;
332}
333
334void eigrp_external_routes_refresh(struct eigrp *eigrp, int type)
335{
336}