]> git.proxmox.com Git - mirror_frr.git/blame - eigrpd/eigrp_network.c
*: use frr_elevate_privs() (2/2: manual)
[mirror_frr.git] / eigrpd / eigrp_network.c
CommitLineData
7f57883e
DS
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 *
896014f4
DL
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
7f57883e
DS
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"
6ae7ed45 40#include "lib_errors.h"
7f57883e 41
7f57883e
DS
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
d62a17ae 51static int eigrp_network_match_iface(const struct connected *,
52 const struct prefix *);
53static void eigrp_network_run_interface(struct eigrp *, struct prefix *,
54 struct interface *);
7f57883e 55
d62a17ae 56int eigrp_sock_init(void)
7f57883e 57{
d62a17ae 58 int eigrp_sock;
029a775e 59 int ret;
60#ifdef IP_HDRINCL
61 int hincl = 1;
62#endif
d62a17ae 63
6bb30c2c
DL
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 }
7f57883e
DS
71
72#ifdef IP_HDRINCL
6bb30c2c
DL
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 }
d62a17ae 80#elif defined(IPTOS_PREC_INTERNETCONTROL)
7f57883e
DS
81#warning "IP_HDRINCL not available on this system"
82#warning "using IPTOS_PREC_INTERNETCONTROL"
6bb30c2c
DL
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 }
7f57883e
DS
91#else /* !IPTOS_PREC_INTERNETCONTROL */
92#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
6bb30c2c 93 zlog_warn("IP_HDRINCL option not available");
7f57883e
DS
94#endif /* IP_HDRINCL */
95
6bb30c2c
DL
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);
d62a17ae 100 }
7f57883e 101
d62a17ae 102 return eigrp_sock;
7f57883e
DS
103}
104
d62a17ae 105void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen)
7f57883e 106{
d62a17ae 107 int newbuflen;
108 /* Check if any work has to be done at all. */
109 if (eigrp->maxsndbuflen >= buflen)
110 return;
01b9e3fd 111 frr_elevate_privs(&eigrpd_privs) {
d62a17ae 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 */
01b9e3fd
DL
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 }
7f57883e
DS
130}
131
d62a17ae 132int eigrp_if_ipmulticast(struct eigrp *top, struct prefix *p,
133 unsigned int ifindex)
7f57883e 134{
d7c0a89a 135 uint8_t val;
d62a17ae 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;
7f57883e
DS
166}
167
168/* Join to the EIGRP multicast group. */
d62a17ae 169int eigrp_if_add_allspfrouters(struct eigrp *top, struct prefix *p,
170 unsigned int ifindex)
7f57883e 171{
d62a17ae 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;
7f57883e
DS
189}
190
d62a17ae 191int eigrp_if_drop_allspfrouters(struct eigrp *top, struct prefix *p,
192 unsigned int ifindex)
7f57883e 193{
d62a17ae 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;
7f57883e
DS
210}
211
cd6c066e 212int eigrp_network_set(struct eigrp *eigrp, struct prefix *p)
7f57883e 213{
f4e14fdb 214 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
d62a17ae 215 struct route_node *rn;
216 struct interface *ifp;
d62a17ae 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
a96029f8 225 struct prefix *pref = prefix_new();
d62a17ae 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. */
451fda4f 234 FOR_ALL_INTERFACES (vrf, ifp) {
d62a17ae 235 zlog_debug("Setting up %s", ifp->name);
a96029f8 236 eigrp_network_run_interface(eigrp, p, ifp);
d62a17ae 237 }
238 return 1;
7f57883e
DS
239}
240
241/* Check whether interface matches given network
242 * returns: 1, true. 0, false
243 */
d62a17ae 244static int eigrp_network_match_iface(const struct connected *co,
245 const struct prefix *net)
7f57883e 246{
d62a17ae 247 /* new approach: more elegant and conceptually clean */
248 return prefix_match_network_statement(net, CONNECTED_PREFIX(co));
7f57883e
DS
249}
250
d62a17ae 251static void eigrp_network_run_interface(struct eigrp *eigrp, struct prefix *p,
252 struct interface *ifp)
7f57883e 253{
b748db67 254 struct eigrp_interface *ei;
d62a17ae 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
996c9314 265 if (p->family == co->address->family && !ifp->info
d62a17ae 266 && eigrp_network_match_iface(co, p)) {
d62a17ae 267
268 ei = eigrp_if_new(eigrp, ifp, co->address);
269 ei->connected = co;
270
d62a17ae 271 /* Relate eigrp interface to eigrp instance. */
272 ei->eigrp = eigrp;
273
d62a17ae 274 /* if router_id is not configured, dont bring up
275 * interfaces.
276 * eigrp_router_id_update() will call eigrp_if_update
277 * whenever r-id is configured instead.
278 */
279 if (if_is_operative(ifp))
280 eigrp_if_up(ei);
281 }
282 }
7f57883e
DS
283}
284
d62a17ae 285void eigrp_if_update(struct interface *ifp)
7f57883e 286{
d62a17ae 287 struct listnode *node, *nnode;
288 struct route_node *rn;
289 struct eigrp *eigrp;
290
291 /*
292 * In the event there are multiple eigrp autonymnous systems running,
293 * we need to check eac one and add the interface as approperate
294 */
295 for (ALL_LIST_ELEMENTS(eigrp_om->eigrp, node, nnode, eigrp)) {
296 /* EIGRP must be on and Router-ID must be configured. */
297 if (!eigrp || eigrp->router_id == 0)
298 continue;
299
300 /* Run each network for this interface. */
301 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn))
302 if (rn->info != NULL) {
303 eigrp_network_run_interface(eigrp, &rn->p, ifp);
304 }
305 }
7f57883e
DS
306}
307
cd6c066e 308int eigrp_network_unset(struct eigrp *eigrp, struct prefix *p)
7f57883e 309{
d62a17ae 310 struct route_node *rn;
311 struct listnode *node, *nnode;
312 struct eigrp_interface *ei;
313 struct prefix *pref;
314
cd6c066e 315 rn = route_node_lookup(eigrp->networks, p);
d62a17ae 316 if (rn == NULL)
317 return 0;
318
319 pref = rn->info;
320 route_unlock_node(rn);
321
cd6c066e 322 if (!IPV4_ADDR_SAME(&pref->u.prefix4, &p->u.prefix4))
d62a17ae 323 return 0;
324
325 prefix_ipv4_free(rn->info);
326 rn->info = NULL;
327 route_unlock_node(rn); /* initial reference */
328
329 /* Find interfaces that not configured already. */
330 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei)) {
331 int found = 0;
332 struct connected *co = ei->connected;
333
334 for (rn = route_top(eigrp->networks); rn; rn = route_next(rn)) {
335 if (rn->info == NULL)
336 continue;
337
338 if (eigrp_network_match_iface(co, &rn->p)) {
339 found = 1;
340 route_unlock_node(rn);
341 break;
342 }
343 }
344
345 if (found == 0) {
346 eigrp_if_free(ei, INTERFACE_DOWN_BY_VTY);
347 }
348 }
349
350 return 1;
7f57883e
DS
351}
352
d7c0a89a
QY
353uint32_t eigrp_calculate_metrics(struct eigrp *eigrp,
354 struct eigrp_metrics metric)
7f57883e 355{
d62a17ae 356 uint64_t temp_metric;
357 temp_metric = 0;
358
359 if (metric.delay == EIGRP_MAX_METRIC)
360 return EIGRP_MAX_METRIC;
361
362 // EIGRP Metric =
363 // {K1*BW+[(K2*BW)/(256-load)]+(K3*delay)}*{K5/(reliability+K4)}
364
365 if (eigrp->k_values[0])
b1968f83 366 temp_metric += (eigrp->k_values[0] * metric.bandwidth);
d62a17ae 367 if (eigrp->k_values[1])
b1968f83 368 temp_metric += ((eigrp->k_values[1] * metric.bandwidth)
d62a17ae 369 / (256 - metric.load));
370 if (eigrp->k_values[2])
371 temp_metric += (eigrp->k_values[2] * metric.delay);
372 if (eigrp->k_values[3] && !eigrp->k_values[4])
373 temp_metric *= eigrp->k_values[3];
374 if (!eigrp->k_values[3] && eigrp->k_values[4])
375 temp_metric *= (eigrp->k_values[4] / metric.reliability);
376 if (eigrp->k_values[3] && eigrp->k_values[4])
377 temp_metric *= ((eigrp->k_values[4] / metric.reliability)
378 + eigrp->k_values[3]);
379
380 if (temp_metric <= EIGRP_MAX_METRIC)
d7c0a89a 381 return (uint32_t)temp_metric;
d62a17ae 382 else
383 return EIGRP_MAX_METRIC;
7f57883e
DS
384}
385
d7c0a89a
QY
386uint32_t eigrp_calculate_total_metrics(struct eigrp *eigrp,
387 struct eigrp_nexthop_entry *entry)
7f57883e 388{
b748db67
DS
389 struct eigrp_interface *ei = entry->ei;
390
d62a17ae 391 entry->total_metric = entry->reported_metric;
996c9314
LB
392 uint64_t temp_delay =
393 (uint64_t)entry->total_metric.delay
394 + (uint64_t)eigrp_delay_to_scaled(ei->params.delay);
d62a17ae 395 entry->total_metric.delay = temp_delay > EIGRP_MAX_METRIC
396 ? EIGRP_MAX_METRIC
d7c0a89a 397 : (uint32_t)temp_delay;
d62a17ae 398
d7c0a89a 399 uint32_t bw = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
b1968f83 400 entry->total_metric.bandwidth = entry->total_metric.bandwidth > bw
996c9314
LB
401 ? bw
402 : entry->total_metric.bandwidth;
d62a17ae 403
404 return eigrp_calculate_metrics(eigrp, entry->total_metric);
7f57883e
DS
405}
406
d7c0a89a
QY
407uint8_t eigrp_metrics_is_same(struct eigrp_metrics metric1,
408 struct eigrp_metrics metric2)
7f57883e 409{
b1968f83 410 if ((metric1.bandwidth == metric2.bandwidth)
d62a17ae 411 && (metric1.delay == metric2.delay)
412 && (metric1.hop_count == metric2.hop_count)
413 && (metric1.load == metric2.load)
414 && (metric1.reliability == metric2.reliability)
415 && (metric1.mtu[0] == metric2.mtu[0])
416 && (metric1.mtu[1] == metric2.mtu[1])
417 && (metric1.mtu[2] == metric2.mtu[2]))
418 return 1;
419
420 return 0; // if different
7f57883e 421}
f9e5c9ca 422
d62a17ae 423void eigrp_external_routes_refresh(struct eigrp *eigrp, int type)
7f57883e 424{
7f57883e 425}