]>
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 __inline
int iface_compare(struct iface
*, struct iface
*);
30 static struct if_addr
*if_addr_new(struct kaddr
*);
31 static struct if_addr
*if_addr_lookup(struct if_addr_head
*, struct kaddr
*);
32 static int if_start(struct iface
*, int);
33 static int if_reset(struct iface
*, int);
34 static void if_update_af(struct iface_af
*);
35 static int if_hello_timer(struct thread
*);
36 static void if_start_hello_timer(struct iface_af
*);
37 static void if_stop_hello_timer(struct iface_af
*);
38 static int if_join_ipv4_group(struct iface
*, struct in_addr
*);
39 static int if_leave_ipv4_group(struct iface
*, struct in_addr
*);
40 static int if_join_ipv6_group(struct iface
*, struct in6_addr
*);
41 static int if_leave_ipv6_group(struct iface
*, struct in6_addr
*);
43 RB_GENERATE(iface_head
, iface
, entry
, iface_compare
)
46 iface_compare(struct iface
*a
, struct iface
*b
)
48 return (strcmp(a
->name
, b
->name
));
52 if_new(const char *name
)
56 if ((iface
= calloc(1, sizeof(*iface
))) == NULL
)
57 fatal("if_new: calloc");
59 strlcpy(iface
->name
, name
, sizeof(iface
->name
));
62 iface
->ipv4
.af
= AF_INET
;
63 iface
->ipv4
.iface
= iface
;
64 iface
->ipv4
.enabled
= 0;
67 iface
->ipv6
.af
= AF_INET6
;
68 iface
->ipv6
.iface
= iface
;
69 iface
->ipv6
.enabled
= 0;
75 ldpe_if_init(struct iface
*iface
)
77 log_debug("%s: interface %s", __func__
, iface
->name
);
79 LIST_INIT(&iface
->addr_list
);
82 iface
->ipv4
.iface
= iface
;
83 iface
->ipv4
.state
= IF_STA_DOWN
;
84 RB_INIT(&iface
->ipv4
.adj_tree
);
87 iface
->ipv6
.iface
= iface
;
88 iface
->ipv6
.state
= IF_STA_DOWN
;
89 RB_INIT(&iface
->ipv6
.adj_tree
);
93 ldpe_if_exit(struct iface
*iface
)
95 struct if_addr
*if_addr
;
97 log_debug("%s: interface %s", __func__
, iface
->name
);
99 if (iface
->ipv4
.state
== IF_STA_ACTIVE
)
100 if_reset(iface
, AF_INET
);
101 if (iface
->ipv6
.state
== IF_STA_ACTIVE
)
102 if_reset(iface
, AF_INET6
);
104 while ((if_addr
= LIST_FIRST(&iface
->addr_list
)) != NULL
) {
105 LIST_REMOVE(if_addr
, entry
);
111 if_lookup(struct ldpd_conf
*xconf
, unsigned short ifindex
)
115 RB_FOREACH(iface
, iface_head
, &xconf
->iface_tree
)
116 if (iface
->ifindex
== ifindex
)
123 if_lookup_name(struct ldpd_conf
*xconf
, const char *ifname
)
126 strlcpy(iface
.name
, ifname
, sizeof(iface
.name
));
127 return (RB_FIND(iface_head
, &xconf
->iface_tree
, &iface
));
131 if_update_info(struct iface
*iface
, struct kif
*kif
)
134 if (kif
->flags
& IFF_POINTOPOINT
)
135 iface
->type
= IF_TYPE_POINTOPOINT
;
136 if (kif
->flags
& IFF_BROADCAST
&&
137 kif
->flags
& IFF_MULTICAST
)
138 iface
->type
= IF_TYPE_BROADCAST
;
140 /* get index and flags */
141 iface
->ifindex
= kif
->ifindex
;
142 iface
->operative
= kif
->operative
;
146 iface_af_get(struct iface
*iface
, int af
)
150 return (&iface
->ipv4
);
152 return (&iface
->ipv6
);
154 fatalx("iface_af_get: unknown af");
158 static struct if_addr
*
159 if_addr_new(struct kaddr
*ka
)
161 struct if_addr
*if_addr
;
163 if ((if_addr
= calloc(1, sizeof(*if_addr
))) == NULL
)
166 if_addr
->af
= ka
->af
;
167 if_addr
->addr
= ka
->addr
;
168 if_addr
->prefixlen
= ka
->prefixlen
;
169 if_addr
->dstbrd
= ka
->dstbrd
;
174 static struct if_addr
*
175 if_addr_lookup(struct if_addr_head
*addr_list
, struct kaddr
*ka
)
177 struct if_addr
*if_addr
;
180 LIST_FOREACH(if_addr
, addr_list
, entry
)
181 if (!ldp_addrcmp(af
, &if_addr
->addr
, &ka
->addr
) &&
182 if_addr
->prefixlen
== ka
->prefixlen
&&
183 !ldp_addrcmp(af
, &if_addr
->dstbrd
, &ka
->dstbrd
))
190 if_addr_add(struct kaddr
*ka
)
193 struct if_addr
*if_addr
;
196 if (if_addr_lookup(&global
.addr_list
, ka
) == NULL
) {
197 if_addr
= if_addr_new(ka
);
199 LIST_INSERT_HEAD(&global
.addr_list
, if_addr
, entry
);
200 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
201 if (nbr
->state
!= NBR_STA_OPER
)
203 if (if_addr
->af
== AF_INET
&& !nbr
->v4_enabled
)
205 if (if_addr
->af
== AF_INET6
&& !nbr
->v6_enabled
)
208 send_address_single(nbr
, if_addr
, 0);
212 iface
= if_lookup_name(leconf
, ka
->ifname
);
214 if (ka
->af
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&ka
->addr
.v6
))
215 iface
->linklocal
= ka
->addr
.v6
;
217 if (if_addr_lookup(&iface
->addr_list
, ka
) == NULL
) {
218 if_addr
= if_addr_new(ka
);
219 LIST_INSERT_HEAD(&iface
->addr_list
, if_addr
, entry
);
220 ldp_if_update(iface
, if_addr
->af
);
226 if_addr_del(struct kaddr
*ka
)
229 struct if_addr
*if_addr
;
232 iface
= if_lookup_name(leconf
, ka
->ifname
);
234 if (ka
->af
== AF_INET6
&&
235 IN6_ARE_ADDR_EQUAL(&iface
->linklocal
, &ka
->addr
.v6
))
236 memset(&iface
->linklocal
, 0, sizeof(iface
->linklocal
));
238 if_addr
= if_addr_lookup(&iface
->addr_list
, ka
);
240 LIST_REMOVE(if_addr
, entry
);
241 ldp_if_update(iface
, if_addr
->af
);
246 if_addr
= if_addr_lookup(&global
.addr_list
, ka
);
248 RB_FOREACH(nbr
, nbr_id_head
, &nbrs_by_id
) {
249 if (nbr
->state
!= NBR_STA_OPER
)
251 if (if_addr
->af
== AF_INET
&& !nbr
->v4_enabled
)
253 if (if_addr
->af
== AF_INET6
&& !nbr
->v6_enabled
)
255 send_address_single(nbr
, if_addr
, 1);
257 LIST_REMOVE(if_addr
, entry
);
263 if_start(struct iface
*iface
, int af
)
268 log_debug("%s: %s address-family %s", __func__
, iface
->name
,
271 ia
= iface_af_get(iface
, af
);
273 gettimeofday(&now
, NULL
);
274 ia
->uptime
= now
.tv_sec
;
278 if (if_join_ipv4_group(iface
, &global
.mcast_addr_v4
))
282 if (if_join_ipv6_group(iface
, &global
.mcast_addr_v6
))
286 fatalx("if_start: unknown af");
289 send_hello(HELLO_LINK
, ia
, NULL
);
290 if_start_hello_timer(ia
);
291 ia
->state
= IF_STA_ACTIVE
;
297 if_reset(struct iface
*iface
, int af
)
302 log_debug("%s: %s address-family %s", __func__
, iface
->name
,
305 ia
= iface_af_get(iface
, af
);
306 if_stop_hello_timer(ia
);
308 while ((adj
= RB_ROOT(&ia
->adj_tree
)) != NULL
)
309 adj_del(adj
, S_SHUTDOWN
);
314 if (global
.ipv4
.ldp_disc_socket
!= -1)
315 if_leave_ipv4_group(iface
, &global
.mcast_addr_v4
);
318 if (global
.ipv6
.ldp_disc_socket
!= -1)
319 if_leave_ipv6_group(iface
, &global
.mcast_addr_v6
);
322 fatalx("if_reset: unknown af");
325 ia
->state
= IF_STA_DOWN
;
331 if_update_af(struct iface_af
*ia
)
333 int addr_ok
= 0, socket_ok
, rtr_id_ok
;
334 struct if_addr
*if_addr
;
339 * NOTE: for LDPv4, each interface should have at least one
340 * valid IP address otherwise they can not be enabled.
342 LIST_FOREACH(if_addr
, &ia
->iface
->addr_list
, entry
) {
343 if (if_addr
->af
== AF_INET
) {
350 /* for IPv6 the link-local address is enough. */
351 if (IN6_IS_ADDR_LINKLOCAL(&ia
->iface
->linklocal
))
355 fatalx("if_update_af: unknown af");
358 if ((ldp_af_global_get(&global
, ia
->af
))->ldp_disc_socket
!= -1)
363 if (ldp_rtr_id_get(leconf
) != INADDR_ANY
)
368 if (ia
->state
== IF_STA_DOWN
) {
369 if (!ia
->enabled
|| !ia
->iface
->operative
|| !addr_ok
||
370 !socket_ok
|| !rtr_id_ok
)
373 if_start(ia
->iface
, ia
->af
);
374 } else if (ia
->state
== IF_STA_ACTIVE
) {
375 if (ia
->enabled
&& ia
->iface
->operative
&& addr_ok
&&
376 socket_ok
&& rtr_id_ok
)
379 if_reset(ia
->iface
, ia
->af
);
384 ldp_if_update(struct iface
*iface
, int af
)
386 if (af
== AF_INET
|| af
== AF_UNSPEC
)
387 if_update_af(&iface
->ipv4
);
388 if (af
== AF_INET6
|| af
== AF_UNSPEC
)
389 if_update_af(&iface
->ipv6
);
393 if_update_all(int af
)
397 RB_FOREACH(iface
, iface_head
, &leconf
->iface_tree
)
398 ldp_if_update(iface
, af
);
402 if_get_hello_holdtime(struct iface_af
*ia
)
404 if (ia
->hello_holdtime
!= 0)
405 return (ia
->hello_holdtime
);
407 if ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_holdtime
!= 0)
408 return ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_holdtime
);
410 return (leconf
->lhello_holdtime
);
414 if_get_hello_interval(struct iface_af
*ia
)
416 if (ia
->hello_interval
!= 0)
417 return (ia
->hello_interval
);
419 if ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_interval
!= 0)
420 return ((ldp_af_conf_get(leconf
, ia
->af
))->lhello_interval
);
422 return (leconf
->lhello_interval
);
428 if_hello_timer(struct thread
*thread
)
430 struct iface_af
*ia
= THREAD_ARG(thread
);
432 ia
->hello_timer
= NULL
;
433 send_hello(HELLO_LINK
, ia
, NULL
);
434 if_start_hello_timer(ia
);
440 if_start_hello_timer(struct iface_af
*ia
)
442 THREAD_TIMER_OFF(ia
->hello_timer
);
443 ia
->hello_timer
= thread_add_timer(master
, if_hello_timer
, ia
,
444 if_get_hello_interval(ia
));
448 if_stop_hello_timer(struct iface_af
*ia
)
450 THREAD_TIMER_OFF(ia
->hello_timer
);
454 if_to_ctl(struct iface_af
*ia
)
456 static struct ctl_iface ictl
;
461 memcpy(ictl
.name
, ia
->iface
->name
, sizeof(ictl
.name
));
462 ictl
.ifindex
= ia
->iface
->ifindex
;
463 ictl
.state
= ia
->state
;
464 ictl
.type
= ia
->iface
->type
;
465 ictl
.hello_holdtime
= if_get_hello_holdtime(ia
);
466 ictl
.hello_interval
= if_get_hello_interval(ia
);
468 gettimeofday(&now
, NULL
);
469 if (ia
->state
!= IF_STA_DOWN
&&
471 ictl
.uptime
= now
.tv_sec
- ia
->uptime
;
476 RB_FOREACH(adj
, ia_adj_head
, &ia
->adj_tree
)
482 /* multicast membership sockopts */
484 if_get_ipv4_addr(struct iface
*iface
)
486 struct if_addr
*if_addr
;
488 LIST_FOREACH(if_addr
, &iface
->addr_list
, entry
)
489 if (if_addr
->af
== AF_INET
)
490 return (if_addr
->addr
.v4
.s_addr
);
496 if_join_ipv4_group(struct iface
*iface
, struct in_addr
*addr
)
498 struct in_addr if_addr
;
500 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
503 if_addr
.s_addr
= if_get_ipv4_addr(iface
);
505 if (setsockopt_ipv4_multicast(global
.ipv4
.ldp_disc_socket
,
506 IP_ADD_MEMBERSHIP
, if_addr
, addr
->s_addr
, iface
->ifindex
) < 0) {
507 log_warn("%s: error IP_ADD_MEMBERSHIP, interface %s address %s",
508 __func__
, iface
->name
, inet_ntoa(*addr
));
515 if_leave_ipv4_group(struct iface
*iface
, struct in_addr
*addr
)
517 struct in_addr if_addr
;
519 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
522 if_addr
.s_addr
= if_get_ipv4_addr(iface
);
524 if (setsockopt_ipv4_multicast(global
.ipv4
.ldp_disc_socket
,
525 IP_DROP_MEMBERSHIP
, if_addr
, addr
->s_addr
, iface
->ifindex
) < 0) {
526 log_warn("%s: error IP_DROP_MEMBERSHIP, interface %s "
527 "address %s", __func__
, iface
->name
, inet_ntoa(*addr
));
535 if_join_ipv6_group(struct iface
*iface
, struct in6_addr
*addr
)
537 struct ipv6_mreq mreq
;
539 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
542 mreq
.ipv6mr_multiaddr
= *addr
;
543 mreq
.ipv6mr_interface
= iface
->ifindex
;
545 if (setsockopt(global
.ipv6
.ldp_disc_socket
, IPPROTO_IPV6
,
546 IPV6_JOIN_GROUP
, &mreq
, sizeof(mreq
)) < 0) {
547 log_warn("%s: error IPV6_JOIN_GROUP, interface %s address %s",
548 __func__
, iface
->name
, log_in6addr(addr
));
556 if_leave_ipv6_group(struct iface
*iface
, struct in6_addr
*addr
)
558 struct ipv6_mreq mreq
;
560 log_debug("%s: interface %s addr %s", __func__
, iface
->name
,
563 mreq
.ipv6mr_multiaddr
= *addr
;
564 mreq
.ipv6mr_interface
= iface
->ifindex
;
566 if (setsockopt(global
.ipv6
.ldp_disc_socket
, IPPROTO_IPV6
,
567 IPV6_LEAVE_GROUP
, (void *)&mreq
, sizeof(mreq
)) < 0) {
568 log_warn("%s: error IPV6_LEAVE_GROUP, interface %s address %s",
569 __func__
, iface
->name
, log_in6addr(addr
));