]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_network.c
Merge pull request #4722 from ak503/static
[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(struct vrf *vrf)
57 {
58 int eigrp_sock;
59 int ret;
60 #ifdef IP_HDRINCL
61 int hincl = 1;
62 #endif
63
64 frr_with_privs(&eigrpd_privs) {
65 eigrp_sock = vrf_socket(
66 AF_INET, SOCK_RAW, IPPROTO_EIGRPIGP, vrf->vrf_id,
67 vrf->vrf_id != VRF_DEFAULT ? vrf->name : NULL);
68 if (eigrp_sock < 0) {
69 zlog_err("eigrp_read_sock_init: socket: %s",
70 safe_strerror(errno));
71 exit(1);
72 }
73
74 #ifdef IP_HDRINCL
75 /* we will include IP header with packet */
76 ret = setsockopt(eigrp_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
77 sizeof(hincl));
78 if (ret < 0) {
79 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
80 eigrp_sock, safe_strerror(errno));
81 }
82 #elif defined(IPTOS_PREC_INTERNETCONTROL)
83 #warning "IP_HDRINCL not available on this system"
84 #warning "using IPTOS_PREC_INTERNETCONTROL"
85 ret = setsockopt_ipv4_tos(eigrp_sock,
86 IPTOS_PREC_INTERNETCONTROL);
87 if (ret < 0) {
88 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s",
89 tos, eigrp_sock, safe_strerror(errno));
90 close(eigrp_sock); /* Prevent sd leak. */
91 return ret;
92 }
93 #else /* !IPTOS_PREC_INTERNETCONTROL */
94 #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
95 zlog_warn("IP_HDRINCL option not available");
96 #endif /* IP_HDRINCL */
97
98 ret = setsockopt_ifindex(AF_INET, eigrp_sock, 1);
99 if (ret < 0)
100 zlog_warn("Can't set pktinfo option for fd %d",
101 eigrp_sock);
102 }
103
104 return eigrp_sock;
105 }
106
107 void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen)
108 {
109 int newbuflen;
110 /* Check if any work has to be done at all. */
111 if (eigrp->maxsndbuflen >= buflen)
112 return;
113
114 /* Now we try to set SO_SNDBUF to what our caller has requested
115 * (the MTU of a newly added interface). However, if the OS has
116 * truncated the actual buffer size to somewhat less size, try
117 * to detect it and update our records appropriately. The OS
118 * may allocate more buffer space, than requested, this isn't
119 * a error.
120 */
121 setsockopt_so_sendbuf(eigrp->fd, buflen);
122 newbuflen = getsockopt_so_sendbuf(eigrp->fd);
123 if (newbuflen < 0 || newbuflen < (int)buflen)
124 zlog_warn("%s: tried to set SO_SNDBUF to %u, but got %d",
125 __func__, buflen, newbuflen);
126 if (newbuflen >= 0)
127 eigrp->maxsndbuflen = (unsigned int)newbuflen;
128 else
129 zlog_warn("%s: failed to get SO_SNDBUF", __func__);
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(eigrp->vrf_id);
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.s_addr == 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 if (ifp->vrf_id != eigrp->vrf_id)
296 continue;
297
298 /* EIGRP must be on and Router-ID must be configured. */
299 if (eigrp->router_id.s_addr == 0)
300 continue;
301
302 /* Run each network for this interface. */
303 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn))
304 if (rn->info != NULL) {
305 eigrp_network_run_interface(eigrp, &rn->p, ifp);
306 }
307 }
308 }
309
310 int eigrp_network_unset(struct eigrp *eigrp, struct prefix *p)
311 {
312 struct route_node *rn;
313 struct listnode *node, *nnode;
314 struct eigrp_interface *ei;
315 struct prefix *pref;
316
317 rn = route_node_lookup(eigrp->networks, p);
318 if (rn == NULL)
319 return 0;
320
321 pref = rn->info;
322 route_unlock_node(rn);
323
324 if (!IPV4_ADDR_SAME(&pref->u.prefix4, &p->u.prefix4))
325 return 0;
326
327 prefix_ipv4_free(rn->info);
328 rn->info = NULL;
329 route_unlock_node(rn); /* initial reference */
330
331 /* Find interfaces that not configured already. */
332 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei)) {
333 bool found = false;
334
335 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn)) {
336 if (rn->info == NULL)
337 continue;
338
339 if (eigrp_network_match_iface(&ei->address, &rn->p)) {
340 found = true;
341 route_unlock_node(rn);
342 break;
343 }
344 }
345
346 if (!found) {
347 eigrp_if_free(ei, INTERFACE_DOWN_BY_VTY);
348 }
349 }
350
351 return 1;
352 }
353
354 uint32_t eigrp_calculate_metrics(struct eigrp *eigrp,
355 struct eigrp_metrics metric)
356 {
357 uint64_t temp_metric;
358 temp_metric = 0;
359
360 if (metric.delay == EIGRP_MAX_METRIC)
361 return EIGRP_MAX_METRIC;
362
363 // EIGRP Metric =
364 // {K1*BW+[(K2*BW)/(256-load)]+(K3*delay)}*{K5/(reliability+K4)}
365
366 if (eigrp->k_values[0])
367 temp_metric += (eigrp->k_values[0] * metric.bandwidth);
368 if (eigrp->k_values[1])
369 temp_metric += ((eigrp->k_values[1] * metric.bandwidth)
370 / (256 - metric.load));
371 if (eigrp->k_values[2])
372 temp_metric += (eigrp->k_values[2] * metric.delay);
373 if (eigrp->k_values[3] && !eigrp->k_values[4])
374 temp_metric *= eigrp->k_values[3];
375 if (!eigrp->k_values[3] && eigrp->k_values[4])
376 temp_metric *= (eigrp->k_values[4] / metric.reliability);
377 if (eigrp->k_values[3] && eigrp->k_values[4])
378 temp_metric *= ((eigrp->k_values[4] / metric.reliability)
379 + eigrp->k_values[3]);
380
381 if (temp_metric <= EIGRP_MAX_METRIC)
382 return (uint32_t)temp_metric;
383 else
384 return EIGRP_MAX_METRIC;
385 }
386
387 uint32_t eigrp_calculate_total_metrics(struct eigrp *eigrp,
388 struct eigrp_nexthop_entry *entry)
389 {
390 struct eigrp_interface *ei = entry->ei;
391
392 entry->total_metric = entry->reported_metric;
393 uint64_t temp_delay =
394 (uint64_t)entry->total_metric.delay
395 + (uint64_t)eigrp_delay_to_scaled(ei->params.delay);
396 entry->total_metric.delay = temp_delay > EIGRP_MAX_METRIC
397 ? EIGRP_MAX_METRIC
398 : (uint32_t)temp_delay;
399
400 uint32_t bw = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
401 entry->total_metric.bandwidth = entry->total_metric.bandwidth > bw
402 ? bw
403 : entry->total_metric.bandwidth;
404
405 return eigrp_calculate_metrics(eigrp, entry->total_metric);
406 }
407
408 uint8_t eigrp_metrics_is_same(struct eigrp_metrics metric1,
409 struct eigrp_metrics metric2)
410 {
411 if ((metric1.bandwidth == metric2.bandwidth)
412 && (metric1.delay == metric2.delay)
413 && (metric1.hop_count == metric2.hop_count)
414 && (metric1.load == metric2.load)
415 && (metric1.reliability == metric2.reliability)
416 && (metric1.mtu[0] == metric2.mtu[0])
417 && (metric1.mtu[1] == metric2.mtu[1])
418 && (metric1.mtu[2] == metric2.mtu[2]))
419 return 1;
420
421 return 0; // if different
422 }
423
424 void eigrp_external_routes_refresh(struct eigrp *eigrp, int type)
425 {
426 }