]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_network.c
Merge pull request #1174 from opensourcerouting/show_route_defpy
[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
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"
49
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 *,
53 struct interface *);
54
55 int eigrp_sock_init(void)
56 {
57 int eigrp_sock;
58 int ret, hincl = 1;
59
60 if (eigrpd_privs.change(ZPRIVS_RAISE))
61 zlog_err("eigrp_sock_init: could not raise privs, %s",
62 safe_strerror(errno));
63
64 eigrp_sock = socket(AF_INET, SOCK_RAW, IPPROTO_EIGRPIGP);
65 if (eigrp_sock < 0) {
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));
72 exit(1);
73 }
74
75 #ifdef IP_HDRINCL
76 /* we will include IP header with packet */
77 ret = setsockopt(eigrp_sock, IPPROTO_IP, IP_HDRINCL, &hincl,
78 sizeof(hincl));
79 if (ret < 0) {
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));
86 }
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);
91 if (ret < 0) {
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. */
99 return ret;
100 }
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 */
105
106 ret = setsockopt_ifindex(AF_INET, eigrp_sock, 1);
107
108 if (ret < 0)
109 zlog_warn("Can't set pktinfo option for fd %d", eigrp_sock);
110
111 if (eigrpd_privs.change(ZPRIVS_LOWER)) {
112 zlog_err("eigrp_sock_init: could not lower privs, %s",
113 safe_strerror(errno));
114 }
115
116 return eigrp_sock;
117 }
118
119 void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen)
120 {
121 int newbuflen;
122 /* Check if any work has to be done at all. */
123 if (eigrp->maxsndbuflen >= buflen)
124 return;
125 if (eigrpd_privs.change(ZPRIVS_RAISE))
126 zlog_err("%s: could not raise privs, %s", __func__,
127 safe_strerror(errno));
128
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
134 * a error.
135 */
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);
141 if (newbuflen >= 0)
142 eigrp->maxsndbuflen = (unsigned int)newbuflen;
143 else
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));
148 }
149
150 int eigrp_if_ipmulticast(struct eigrp *top, struct prefix *p,
151 unsigned int ifindex)
152 {
153 u_char val;
154 int ret, len;
155
156 val = 0;
157 len = sizeof(val);
158
159 /* Prevent receiving self-origined multicast packets. */
160 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val,
161 len);
162 if (ret < 0)
163 zlog_warn(
164 "can't setsockopt IP_MULTICAST_LOOP (0) for fd %d: %s",
165 top->fd, safe_strerror(errno));
166
167 /* Explicitly set multicast ttl to 1 -- endo. */
168 val = 1;
169 ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val,
170 len);
171 if (ret < 0)
172 zlog_warn("can't setsockopt IP_MULTICAST_TTL (1) for fd %d: %s",
173 top->fd, safe_strerror(errno));
174
175 ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex);
176 if (ret < 0)
177 zlog_warn(
178 "can't setsockopt IP_MULTICAST_IF (fd %d, addr %s, "
179 "ifindex %u): %s",
180 top->fd, inet_ntoa(p->u.prefix4), ifindex,
181 safe_strerror(errno));
182
183 return ret;
184 }
185
186 /* Join to the EIGRP multicast group. */
187 int eigrp_if_add_allspfrouters(struct eigrp *top, struct prefix *p,
188 unsigned int ifindex)
189 {
190 int ret;
191
192 ret = setsockopt_ipv4_multicast(
193 top->fd, IP_ADD_MEMBERSHIP, p->u.prefix4,
194 htonl(EIGRP_MULTICAST_ADDRESS), ifindex);
195 if (ret < 0)
196 zlog_warn(
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));
202 else
203 zlog_debug("interface %s [%u] join EIGRP Multicast group.",
204 inet_ntoa(p->u.prefix4), ifindex);
205
206 return ret;
207 }
208
209 int eigrp_if_drop_allspfrouters(struct eigrp *top, struct prefix *p,
210 unsigned int ifindex)
211 {
212 int ret;
213
214 ret = setsockopt_ipv4_multicast(
215 top->fd, IP_DROP_MEMBERSHIP, p->u.prefix4,
216 htonl(EIGRP_MULTICAST_ADDRESS), ifindex);
217 if (ret < 0)
218 zlog_warn(
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));
223 else
224 zlog_debug("interface %s [%u] leave EIGRP Multicast group.",
225 inet_ntoa(p->u.prefix4), ifindex);
226
227 return ret;
228 }
229
230 int eigrp_network_set(struct eigrp *eigrp, struct prefix *p)
231 {
232 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
233 struct route_node *rn;
234 struct interface *ifp;
235
236 rn = route_node_get(eigrp->networks, (struct prefix *)p);
237 if (rn->info) {
238 /* There is already same network statement. */
239 route_unlock_node(rn);
240 return 0;
241 }
242
243 struct prefix *pref = prefix_new();
244 PREFIX_COPY_IPV4(pref, p);
245 rn->info = (void *)pref;
246
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);
255 }
256 return 1;
257 }
258
259 /* Check whether interface matches given network
260 * returns: 1, true. 0, false
261 */
262 static int eigrp_network_match_iface(const struct connected *co,
263 const struct prefix *net)
264 {
265 /* new approach: more elegant and conceptually clean */
266 return prefix_match_network_statement(net, CONNECTED_PREFIX(co));
267 }
268
269 static void eigrp_network_run_interface(struct eigrp *eigrp, struct prefix *p,
270 struct interface *ifp)
271 {
272 struct eigrp_interface *ei;
273 struct listnode *cnode;
274 struct connected *co;
275
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)) {
279
280 if (CHECK_FLAG(co->flags, ZEBRA_IFA_SECONDARY))
281 continue;
282
283 if (p->family == co->address->family
284 && !ifp->info
285 && eigrp_network_match_iface(co, p)) {
286
287 ei = eigrp_if_new(eigrp, ifp, co->address);
288 ei->connected = co;
289
290 /* Relate eigrp interface to eigrp instance. */
291 ei->eigrp = eigrp;
292
293 /* if router_id is not configured, dont bring up
294 * interfaces.
295 * eigrp_router_id_update() will call eigrp_if_update
296 * whenever r-id is configured instead.
297 */
298 if (if_is_operative(ifp))
299 eigrp_if_up(ei);
300 }
301 }
302 }
303
304 void eigrp_if_update(struct interface *ifp)
305 {
306 struct listnode *node, *nnode;
307 struct route_node *rn;
308 struct eigrp *eigrp;
309
310 /*
311 * In the event there are multiple eigrp autonymnous systems running,
312 * we need to check eac one and add the interface as approperate
313 */
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)
317 continue;
318
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);
323 }
324 }
325 }
326
327 int eigrp_network_unset(struct eigrp *eigrp, struct prefix *p)
328 {
329 struct route_node *rn;
330 struct listnode *node, *nnode;
331 struct eigrp_interface *ei;
332 struct prefix *pref;
333
334 rn = route_node_lookup(eigrp->networks, p);
335 if (rn == NULL)
336 return 0;
337
338 pref = rn->info;
339 route_unlock_node(rn);
340
341 if (!IPV4_ADDR_SAME(&pref->u.prefix4, &p->u.prefix4))
342 return 0;
343
344 prefix_ipv4_free(rn->info);
345 rn->info = NULL;
346 route_unlock_node(rn); /* initial reference */
347
348 /* Find interfaces that not configured already. */
349 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei)) {
350 int found = 0;
351 struct connected *co = ei->connected;
352
353 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn)) {
354 if (rn->info == NULL)
355 continue;
356
357 if (eigrp_network_match_iface(co, &rn->p)) {
358 found = 1;
359 route_unlock_node(rn);
360 break;
361 }
362 }
363
364 if (found == 0) {
365 eigrp_if_free(ei, INTERFACE_DOWN_BY_VTY);
366 }
367 }
368
369 return 1;
370 }
371
372 u_int32_t eigrp_calculate_metrics(struct eigrp *eigrp,
373 struct eigrp_metrics metric)
374 {
375 uint64_t temp_metric;
376 temp_metric = 0;
377
378 if (metric.delay == EIGRP_MAX_METRIC)
379 return EIGRP_MAX_METRIC;
380
381 // EIGRP Metric =
382 // {K1*BW+[(K2*BW)/(256-load)]+(K3*delay)}*{K5/(reliability+K4)}
383
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]);
398
399 if (temp_metric <= EIGRP_MAX_METRIC)
400 return (u_int32_t)temp_metric;
401 else
402 return EIGRP_MAX_METRIC;
403 }
404
405 u_int32_t eigrp_calculate_total_metrics(struct eigrp *eigrp,
406 struct eigrp_nexthop_entry *entry)
407 {
408 struct eigrp_interface *ei = entry->ei;
409
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
414 ? EIGRP_MAX_METRIC
415 : (u_int32_t)temp_delay;
416
417 u_int32_t bw =
418 eigrp_bandwidth_to_scaled(ei->params.bandwidth);
419 entry->total_metric.bandwidth = entry->total_metric.bandwidth > bw
420 ? bw
421 : entry->total_metric.bandwidth;
422
423 return eigrp_calculate_metrics(eigrp, entry->total_metric);
424 }
425
426 u_char eigrp_metrics_is_same(struct eigrp_metrics metric1,
427 struct eigrp_metrics metric2)
428 {
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]))
437 return 1;
438
439 return 0; // if different
440 }
441
442 void eigrp_external_routes_refresh(struct eigrp *eigrp, int type)
443 {
444 }