]>
git.proxmox.com Git - mirror_frr.git/blob - ldpd/interface.c
4 * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 static struct if_addr
*if_addr_new(struct kaddr
*);
30 static struct if_addr
*if_addr_lookup(struct if_addr_head
*, struct kaddr
*);
31 static int if_start(struct iface
*, int);
32 static int if_reset(struct iface
*, int);
33 static void if_update_af(struct iface_af
*, int);
34 static int if_hello_timer(struct thread
*);
35 static void if_start_hello_timer(struct iface_af
*);
36 static void if_stop_hello_timer(struct iface_af
*);
37 static int if_join_ipv4_group(struct iface
*, struct in_addr
*);
38 static int if_leave_ipv4_group(struct iface
*, struct in_addr
*);
39 static int if_join_ipv6_group(struct iface
*, struct in6_addr
*);
40 static int if_leave_ipv6_group(struct iface
*, struct in6_addr
*);
43 if_new(struct kif
*kif
)
47 if ((iface
= calloc(1, sizeof(*iface
))) == NULL
)
48 fatal("if_new: calloc");
50 strlcpy(iface
->name
, kif
->ifname
, sizeof(iface
->name
));
51 LIST_INIT(&iface
->addr_list
);
53 if_update_info(iface
, kif
);
56 iface
->ipv4
.af
= AF_INET
;
57 iface
->ipv4
.iface
= iface
;
58 iface
->ipv4
.enabled
= 0;
59 iface
->ipv4
.state
= IF_STA_DOWN
;
60 LIST_INIT(&iface
->ipv4
.adj_list
);
63 iface
->ipv6
.af
= AF_INET6
;
64 iface
->ipv6
.iface
= iface
;
65 iface
->ipv6
.enabled
= 0;
66 iface
->ipv6
.state
= IF_STA_DOWN
;
67 LIST_INIT(&iface
->ipv6
.adj_list
);
73 if_lookup(struct ldpd_conf
*xconf
, unsigned short ifindex
)
77 LIST_FOREACH(iface
, &xconf
->iface_list
, entry
)
78 if (iface
->ifindex
== ifindex
)
85 if_exit(struct iface
*iface
)
87 struct if_addr
*if_addr
;
89 log_debug("%s: interface %s", __func__
, iface
->name
);
91 if (iface
->ipv4
.state
== IF_STA_ACTIVE
)
92 if_reset(iface
, AF_INET
);
93 if (iface
->ipv6
.state
== IF_STA_ACTIVE
)
94 if_reset(iface
, AF_INET6
);
96 while ((if_addr
= LIST_FIRST(&iface
->addr_list
)) != NULL
) {
97 LIST_REMOVE(if_addr
, entry
);
103 if_lookup_name(struct ldpd_conf
*xconf
, const char *ifname
)
107 LIST_FOREACH(iface
, &xconf
->iface_list
, entry
)
108 if (strcmp(iface
->name
, ifname
) == 0)
115 if_update_info(struct iface
*iface
, struct kif
*kif
)
118 if (kif
->flags
& IFF_POINTOPOINT
)
119 iface
->type
= IF_TYPE_POINTOPOINT
;
120 if (kif
->flags
& IFF_BROADCAST
&&
121 kif
->flags
& IFF_MULTICAST
)
122 iface
->type
= IF_TYPE_BROADCAST
;
124 /* get index and flags */
125 iface
->ifindex
= kif
->ifindex
;
126 iface
->flags
= kif
->flags
;
130 iface_af_get(struct iface
*iface
, int af
)
134 return (&iface
->ipv4
);
136 return (&iface
->ipv6
);
138 fatalx("iface_af_get: unknown af");
142 static struct if_addr
*
143 if_addr_new(struct kaddr
*ka
)
145 struct if_addr
*if_addr
;
147 if ((if_addr
= calloc(1, sizeof(*if_addr
))) == NULL
)
150 if_addr
->af
= ka
->af
;
151 if_addr
->addr
= ka
->addr
;
152 if_addr
->prefixlen
= ka
->prefixlen
;
153 if_addr
->dstbrd
= ka
->dstbrd
;
158 static struct if_addr
*
159 if_addr_lookup(struct if_addr_head
*addr_list
, struct kaddr
*ka
)
161 struct if_addr
*if_addr
;
164 LIST_FOREACH(if_addr
, addr_list
, entry
)
165 if (!ldp_addrcmp(af
, &if_addr
->addr
, &ka
->addr
) &&
166 if_addr
->prefixlen
== ka
->prefixlen
&&
167 !ldp_addrcmp(af
, &if_addr
->dstbrd
, &ka
->dstbrd
))
174 if_addr_add(struct kaddr
*ka
)
177 struct if_addr
*if_addr
;
180 if (if_addr_lookup(&global
.addr_list
, ka
) == NULL
) {
181 if_addr
= if_addr_new(ka
);
183 LIST_INSERT_HEAD(&global
.addr_list
, if_addr
, entry
);
184 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
185 if (nbr
->state
!= NBR_STA_OPER
)
187 if (if_addr
->af
== AF_INET
&& !nbr
->v4_enabled
)
189 if (if_addr
->af
== AF_INET6
&& !nbr
->v6_enabled
)
192 send_address_single(nbr
, if_addr
, 0);
196 iface
= if_lookup(leconf
, ka
->ifindex
);
198 if (ka
->af
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&ka
->addr
.v6
))
199 iface
->linklocal
= ka
->addr
.v6
;
201 if (if_addr_lookup(&iface
->addr_list
, ka
) == NULL
) {
202 if_addr
= if_addr_new(ka
);
203 LIST_INSERT_HEAD(&iface
->addr_list
, if_addr
, entry
);
204 if_update(iface
, if_addr
->af
);
210 if_addr_del(struct kaddr
*ka
)
213 struct if_addr
*if_addr
;
216 iface
= if_lookup(leconf
, ka
->ifindex
);
218 if (ka
->af
== AF_INET6
&&
219 IN6_ARE_ADDR_EQUAL(&iface
->linklocal
, &ka
->addr
.v6
))
220 memset(&iface
->linklocal
, 0, sizeof(iface
->linklocal
));
222 if_addr
= if_addr_lookup(&iface
->addr_list
, ka
);
224 LIST_REMOVE(if_addr
, entry
);
225 if_update(iface
, if_addr
->af
);
230 if_addr
= if_addr_lookup(&global
.addr_list
, ka
);
232 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
233 if (nbr
->state
!= NBR_STA_OPER
)
235 if (if_addr
->af
== AF_INET
&& !nbr
->v4_enabled
)
237 if (if_addr
->af
== AF_INET6
&& !nbr
->v6_enabled
)
239 send_address_single(nbr
, if_addr
, 1);
241 LIST_REMOVE(if_addr
, entry
);
247 if_start(struct iface
*iface
, int af
)
252 log_debug("%s: %s address-family %s", __func__
, iface
->name
,
255 ia
= iface_af_get(iface
, af
);
257 gettimeofday(&now
, NULL
);
258 ia
->uptime
= now
.tv_sec
;
262 if (if_join_ipv4_group(iface
, &global
.mcast_addr_v4
))
266 if (if_join_ipv6_group(iface
, &global
.mcast_addr_v6
))
270 fatalx("if_start: unknown af");
273 send_hello(HELLO_LINK
, ia
, NULL
);
275 if_start_hello_timer(ia
);
280 if_reset(struct iface
*iface
, int af
)
285 log_debug("%s: %s address-family %s", __func__
, iface
->name
,
288 ia
= iface_af_get(iface
, af
);
289 if_stop_hello_timer(ia
);
291 while ((adj
= LIST_FIRST(&ia
->adj_list
)) != NULL
)
292 adj_del(adj
, S_SHUTDOWN
);
297 if (global
.ipv4
.ldp_disc_socket
!= -1)
298 if_leave_ipv4_group(iface
, &global
.mcast_addr_v4
);
301 if (global
.ipv6
.ldp_disc_socket
!= -1)
302 if_leave_ipv6_group(iface
, &global
.mcast_addr_v6
);
305 fatalx("if_start: unknown af");
312 if_update_af(struct iface_af
*ia
, int link_ok
)
314 int addr_ok
= 0, socket_ok
, rtr_id_ok
;
315 struct if_addr
*if_addr
;
320 * NOTE: for LDPv4, each interface should have at least one
321 * valid IP address otherwise they can not be enabled.
323 LIST_FOREACH(if_addr
, &ia
->iface
->addr_list
, entry
) {
324 if (if_addr
->af
== AF_INET
) {
331 /* for IPv6 the link-local address is enough. */
332 if (IN6_IS_ADDR_LINKLOCAL(&ia
->iface
->linklocal
))
336 fatalx("if_update_af: unknown af");
339 if ((ldp_af_global_get(&global
, ia
->af
))->ldp_disc_socket
!= -1)
344 if (ldp_rtr_id_get(leconf
) != INADDR_ANY
)
349 if (ia
->state
== IF_STA_DOWN
) {
350 if (!ia
->enabled
|| !link_ok
|| !addr_ok
|| !socket_ok
||
354 ia
->state
= IF_STA_ACTIVE
;
355 if_start(ia
->iface
, ia
->af
);
356 } else if (ia
->state
== IF_STA_ACTIVE
) {
357 if (ia
->enabled
&& link_ok
&& addr_ok
&& socket_ok
&& rtr_id_ok
)
360 ia
->state
= IF_STA_DOWN
;
361 if_reset(ia
->iface
, ia
->af
);
366 if_update(struct iface
*iface
, int af
)
370 link_ok
= (iface
->flags
& IFF_UP
) && (iface
->flags
& IFF_RUNNING
);
372 if (af
== AF_INET
|| af
== AF_UNSPEC
)
373 if_update_af(&iface
->ipv4
, link_ok
);
374 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
375 if_update_af(&iface
->ipv6
, link_ok
);
379 if_update_all(int af
)
383 LIST_FOREACH(iface
, &leconf
->iface_list
, entry
)
384 if_update(iface
, af
);
388 if_get_hello_holdtime(struct iface_af
*ia
)
390 if (ia
->hello_holdtime
!= 0)
391 return (ia
->hello_holdtime
);
393 if ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_holdtime
!= 0)
394 return ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_holdtime
);
396 return (leconf
->lhello_holdtime
);
400 if_get_hello_interval(struct iface_af
*ia
)
402 if (ia
->hello_interval
!= 0)
403 return (ia
->hello_interval
);
405 if ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_interval
!= 0)
406 return ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_interval
);
408 return (leconf
->lhello_interval
);
414 if_hello_timer(struct thread
*thread
)
416 struct iface_af
*ia
= THREAD_ARG(thread
);
418 ia
->hello_timer
= NULL
;
419 send_hello(HELLO_LINK
, ia
, NULL
);
420 if_start_hello_timer(ia
);
426 if_start_hello_timer(struct iface_af
*ia
)
428 THREAD_TIMER_OFF(ia
->hello_timer
);
429 ia
->hello_timer
= thread_add_timer(master
, if_hello_timer
, ia
,
430 if_get_hello_interval(ia
));
434 if_stop_hello_timer(struct iface_af
*ia
)
436 THREAD_TIMER_OFF(ia
->hello_timer
);
440 if_to_ctl(struct iface_af
*ia
)
442 static struct ctl_iface ictl
;
447 memcpy(ictl
.name
, ia
->iface
->name
, sizeof(ictl
.name
));
448 ictl
.ifindex
= ia
->iface
->ifindex
;
449 ictl
.state
= ia
->state
;
450 ictl
.flags
= ia
->iface
->flags
;
451 ictl
.type
= ia
->iface
->type
;
452 ictl
.hello_holdtime
= if_get_hello_holdtime(ia
);
453 ictl
.hello_interval
= if_get_hello_interval(ia
);
455 gettimeofday(&now
, NULL
);
456 if (ia
->state
!= IF_STA_DOWN
&&
458 ictl
.uptime
= now
.tv_sec
- ia
->uptime
;
463 LIST_FOREACH(adj
, &ia
->adj_list
, ia_entry
)
469 /* multicast membership sockopts */
471 if_get_ipv4_addr(struct iface
*iface
)
473 struct if_addr
*if_addr
;
475 LIST_FOREACH(if_addr
, &iface
->addr_list
, entry
)
476 if (if_addr
->af
== AF_INET
)
477 return (if_addr
->addr
.v4
.s_addr
);
483 if_join_ipv4_group(struct iface
*iface
, struct in_addr
*addr
)
485 struct in_addr if_addr
;
487 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
490 if_addr
.s_addr
= if_get_ipv4_addr(iface
);
492 if (setsockopt_ipv4_multicast(global
.ipv4
.ldp_disc_socket
,
493 IP_ADD_MEMBERSHIP
, if_addr
, addr
->s_addr
, iface
->ifindex
) < 0) {
494 log_warn("%s: error IP_ADD_MEMBERSHIP, interface %s address %s",
495 __func__
, iface
->name
, inet_ntoa(*addr
));
502 if_leave_ipv4_group(struct iface
*iface
, struct in_addr
*addr
)
504 struct in_addr if_addr
;
506 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
509 if_addr
.s_addr
= if_get_ipv4_addr(iface
);
511 if (setsockopt_ipv4_multicast(global
.ipv4
.ldp_disc_socket
,
512 IP_DROP_MEMBERSHIP
, if_addr
, addr
->s_addr
, iface
->ifindex
) < 0) {
513 log_warn("%s: error IP_DROP_MEMBERSHIP, interface %s "
514 "address %s", __func__
, iface
->name
, inet_ntoa(*addr
));
522 if_join_ipv6_group(struct iface
*iface
, struct in6_addr
*addr
)
524 struct ipv6_mreq mreq
;
526 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
529 mreq
.ipv6mr_multiaddr
= *addr
;
530 mreq
.ipv6mr_interface
= iface
->ifindex
;
532 if (setsockopt(global
.ipv6
.ldp_disc_socket
, IPPROTO_IPV6
,
533 IPV6_JOIN_GROUP
, &mreq
, sizeof(mreq
)) < 0) {
534 log_warn("%s: error IPV6_JOIN_GROUP, interface %s address %s",
535 __func__
, iface
->name
, log_in6addr(addr
));
543 if_leave_ipv6_group(struct iface
*iface
, struct in6_addr
*addr
)
545 struct ipv6_mreq mreq
;
547 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
550 mreq
.ipv6mr_multiaddr
= *addr
;
551 mreq
.ipv6mr_interface
= iface
->ifindex
;
553 if (setsockopt(global
.ipv6
.ldp_disc_socket
, IPPROTO_IPV6
,
554 IPV6_LEAVE_GROUP
, (void *)&mreq
, sizeof(mreq
)) < 0) {
555 log_warn("%s: error IPV6_LEAVE_GROUP, interface %s address %s",
556 __func__
, iface
->name
, log_in6addr(addr
));