]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_network.c
zebra: Allow ns delete to happen after under/over flow checks
[mirror_frr.git] / eigrpd / eigrp_network.c
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 *
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
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"
40 #include "lib_errors.h"
41
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
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 *,
54 struct interface *);
55
56 int eigrp_sock_init(void)
57 {
58 int eigrp_sock;
59 int ret;
60 #ifdef IP_HDRINCL
61 int hincl = 1;
62 #endif
63
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 }
71
72 #ifdef IP_HDRINCL
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 }
80 #elif defined(IPTOS_PREC_INTERNETCONTROL)
81 #warning "IP_HDRINCL not available on this system"
82 #warning "using IPTOS_PREC_INTERNETCONTROL"
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 }
91 #else /* !IPTOS_PREC_INTERNETCONTROL */
92 #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
93 zlog_warn("IP_HDRINCL option not available");
94 #endif /* IP_HDRINCL */
95
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);
100 }
101
102 return eigrp_sock;
103 }
104
105 void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen)
106 {
107 int newbuflen;
108 /* Check if any work has to be done at all. */
109 if (eigrp->maxsndbuflen >= buflen)
110 return;
111 frr_elevate_privs(&eigrpd_privs) {
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 */
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 }
130 }
131
132 int eigrp_if_ipmulticast(struct eigrp *top, struct prefix *p,
133 unsigned int ifindex)
134 {
135 uint8_t val;
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;
166 }
167
168 /* Join to the EIGRP multicast group. */
169 int eigrp_if_add_allspfrouters(struct eigrp *top, struct prefix *p,
170 unsigned int ifindex)
171 {
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;
189 }
190
191 int eigrp_if_drop_allspfrouters(struct eigrp *top, struct prefix *p,
192 unsigned int ifindex)
193 {
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;
210 }
211
212 int eigrp_network_set(struct eigrp *eigrp, struct prefix *p)
213 {
214 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
215 struct route_node *rn;
216 struct interface *ifp;
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
225 struct prefix *pref = prefix_new();
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. */
234 FOR_ALL_INTERFACES (vrf, ifp) {
235 zlog_debug("Setting up %s", ifp->name);
236 eigrp_network_run_interface(eigrp, p, ifp);
237 }
238 return 1;
239 }
240
241 /* Check whether interface matches given network
242 * returns: 1, true. 0, false
243 */
244 static int eigrp_network_match_iface(const struct prefix *co_prefix,
245 const struct prefix *net)
246 {
247 /* new approach: more elegant and conceptually clean */
248 return prefix_match_network_statement(net, co_prefix);
249 }
250
251 static void eigrp_network_run_interface(struct eigrp *eigrp, struct prefix *p,
252 struct interface *ifp)
253 {
254 struct eigrp_interface *ei;
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
265 if (p->family == co->address->family && !ifp->info
266 && eigrp_network_match_iface(co->address, p)) {
267
268 ei = eigrp_if_new(eigrp, ifp, co->address);
269
270 /* Relate eigrp interface to eigrp instance. */
271 ei->eigrp = eigrp;
272
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 }
282 }
283
284 void eigrp_if_update(struct interface *ifp)
285 {
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 }
305 }
306
307 int eigrp_network_unset(struct eigrp *eigrp, struct prefix *p)
308 {
309 struct route_node *rn;
310 struct listnode *node, *nnode;
311 struct eigrp_interface *ei;
312 struct prefix *pref;
313
314 rn = route_node_lookup(eigrp->networks, p);
315 if (rn == NULL)
316 return 0;
317
318 pref = rn->info;
319 route_unlock_node(rn);
320
321 if (!IPV4_ADDR_SAME(&pref->u.prefix4, &p->u.prefix4))
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)) {
330 bool found = false;
331
332 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn)) {
333 if (rn->info == NULL)
334 continue;
335
336 if (eigrp_network_match_iface(ei->address, &rn->p)) {
337 found = true;
338 route_unlock_node(rn);
339 break;
340 }
341 }
342
343 if (!found) {
344 eigrp_if_free(ei, INTERFACE_DOWN_BY_VTY);
345 }
346 }
347
348 return 1;
349 }
350
351 uint32_t eigrp_calculate_metrics(struct eigrp *eigrp,
352 struct eigrp_metrics metric)
353 {
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])
364 temp_metric += (eigrp->k_values[0] * metric.bandwidth);
365 if (eigrp->k_values[1])
366 temp_metric += ((eigrp->k_values[1] * metric.bandwidth)
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)
379 return (uint32_t)temp_metric;
380 else
381 return EIGRP_MAX_METRIC;
382 }
383
384 uint32_t eigrp_calculate_total_metrics(struct eigrp *eigrp,
385 struct eigrp_nexthop_entry *entry)
386 {
387 struct eigrp_interface *ei = entry->ei;
388
389 entry->total_metric = entry->reported_metric;
390 uint64_t temp_delay =
391 (uint64_t)entry->total_metric.delay
392 + (uint64_t)eigrp_delay_to_scaled(ei->params.delay);
393 entry->total_metric.delay = temp_delay > EIGRP_MAX_METRIC
394 ? EIGRP_MAX_METRIC
395 : (uint32_t)temp_delay;
396
397 uint32_t bw = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
398 entry->total_metric.bandwidth = entry->total_metric.bandwidth > bw
399 ? bw
400 : entry->total_metric.bandwidth;
401
402 return eigrp_calculate_metrics(eigrp, entry->total_metric);
403 }
404
405 uint8_t eigrp_metrics_is_same(struct eigrp_metrics metric1,
406 struct eigrp_metrics metric2)
407 {
408 if ((metric1.bandwidth == metric2.bandwidth)
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
419 }
420
421 void eigrp_external_routes_refresh(struct eigrp *eigrp, int type)
422 {
423 }