1 /* Kernel routing table updates using netlink over GNU/Linux system.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 /* Hack for GNU libc version 2. */
26 #define MSG_TRUNC 0x20
27 #endif /* MSG_TRUNC */
33 #include "connected.h"
39 #include "zebra/zserv.h"
40 #include "zebra/redistribute.h"
41 #include "zebra/interface.h"
42 #include "zebra/debug.h"
44 /* Socket interface to kernel */
49 struct sockaddr_nl snl
;
51 } netlink
= { -1, 0, {0}, "netlink-listen"}, /* kernel messages */
52 netlink_cmd
= { -1, 0, {0}, "netlink-cmd"}, /* command channel */
53 netlink_addr
= { -1, 0, {0}, "netlink-addr"}; /* address channel */
55 struct message nlmsg_str
[] = {
56 {RTM_NEWROUTE
, "RTM_NEWROUTE"},
57 {RTM_DELROUTE
, "RTM_DELROUTE"},
58 {RTM_GETROUTE
, "RTM_GETROUTE"},
59 {RTM_NEWLINK
, "RTM_NEWLINK"},
60 {RTM_DELLINK
, "RTM_DELLINK"},
61 {RTM_GETLINK
, "RTM_GETLINK"},
62 {RTM_NEWADDR
, "RTM_NEWADDR"},
63 {RTM_DELADDR
, "RTM_DELADDR"},
64 {RTM_GETADDR
, "RTM_GETADDR"},
68 const char *nexthop_types_desc
[] =
74 "IPv4 nexthop with ifindex",
75 "IPv4 nexthop with ifname",
77 "IPv6 nexthop with ifindex",
78 "IPv6 nexthop with ifname",
83 extern struct zebra_t zebrad
;
85 extern struct zebra_privs_t zserv_privs
;
87 extern u_int32_t nl_rcvbufsize
;
89 /* Make socket for Linux netlink interface. */
91 netlink_socket (struct nlsock
*nl
, unsigned long groups
)
94 struct sockaddr_nl snl
;
99 sock
= socket (AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
102 zlog (NULL
, LOG_ERR
, "Can't open %s socket: %s", nl
->name
,
103 safe_strerror (errno
));
107 ret
= fcntl (sock
, F_SETFL
, O_NONBLOCK
);
110 zlog (NULL
, LOG_ERR
, "Can't set %s socket flags: %s", nl
->name
,
111 safe_strerror (errno
));
116 /* Set receive buffer size if it's set from command line */
119 u_int32_t oldsize
, oldlen
;
120 u_int32_t newsize
, newlen
;
122 oldlen
= sizeof(oldsize
);
123 newlen
= sizeof(newsize
);
125 ret
= getsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
128 zlog (NULL
, LOG_ERR
, "Can't get %s receive buffer size: %s", nl
->name
,
129 safe_strerror (errno
));
134 ret
= setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &nl_rcvbufsize
,
135 sizeof(nl_rcvbufsize
));
138 zlog (NULL
, LOG_ERR
, "Can't set %s receive buffer size: %s", nl
->name
,
139 safe_strerror (errno
));
144 ret
= getsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
147 zlog (NULL
, LOG_ERR
, "Can't get %s receive buffer size: %s", nl
->name
,
148 safe_strerror (errno
));
153 zlog (NULL
, LOG_INFO
,
154 "Setting netlink socket receive buffer size: %u -> %u",
158 memset (&snl
, 0, sizeof snl
);
159 snl
.nl_family
= AF_NETLINK
;
160 snl
.nl_groups
= groups
;
162 /* Bind the socket to the netlink structure for anything. */
163 if (zserv_privs
.change (ZPRIVS_RAISE
))
165 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
169 ret
= bind (sock
, (struct sockaddr
*) &snl
, sizeof snl
);
171 if (zserv_privs
.change (ZPRIVS_LOWER
))
172 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
176 zlog (NULL
, LOG_ERR
, "Can't bind %s socket to group 0x%x: %s",
177 nl
->name
, snl
.nl_groups
, safe_strerror (save_errno
));
182 /* multiple netlink sockets will have different nl_pid */
183 namelen
= sizeof snl
;
184 ret
= getsockname (sock
, (struct sockaddr
*) &snl
, (socklen_t
*) &namelen
);
185 if (ret
< 0 || namelen
!= sizeof snl
)
187 zlog (NULL
, LOG_ERR
, "Can't get %s socket name: %s", nl
->name
,
188 safe_strerror (errno
));
199 set_netlink_blocking (struct nlsock
*nl
, int *flags
)
202 /* Change socket flags for blocking I/O. */
203 if ((*flags
= fcntl (nl
->sock
, F_GETFL
, 0)) < 0)
205 zlog (NULL
, LOG_ERR
, "%s:%i F_GETFL error: %s",
206 __FUNCTION__
, __LINE__
, safe_strerror (errno
));
209 *flags
&= ~O_NONBLOCK
;
210 if (fcntl (nl
->sock
, F_SETFL
, *flags
) < 0)
212 zlog (NULL
, LOG_ERR
, "%s:%i F_SETFL error: %s",
213 __FUNCTION__
, __LINE__
, safe_strerror (errno
));
220 set_netlink_nonblocking (struct nlsock
*nl
, int *flags
)
222 /* Restore socket flags for nonblocking I/O */
223 *flags
|= O_NONBLOCK
;
224 if (fcntl (nl
->sock
, F_SETFL
, *flags
) < 0)
226 zlog (NULL
, LOG_ERR
, "%s:%i F_SETFL error: %s",
227 __FUNCTION__
, __LINE__
, safe_strerror (errno
));
233 /* Get type specified information from netlink. */
235 netlink_request (int family
, int type
, struct nlsock
*nl
)
238 struct sockaddr_nl snl
;
248 /* Check netlink socket. */
251 zlog (NULL
, LOG_ERR
, "%s socket isn't active.", nl
->name
);
255 memset (&snl
, 0, sizeof snl
);
256 snl
.nl_family
= AF_NETLINK
;
258 req
.nlh
.nlmsg_len
= sizeof req
;
259 req
.nlh
.nlmsg_type
= type
;
260 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
261 req
.nlh
.nlmsg_pid
= 0;
262 req
.nlh
.nlmsg_seq
= ++nl
->seq
;
263 req
.g
.rtgen_family
= family
;
265 /* linux appears to check capabilities on every message
266 * have to raise caps for every message sent
268 if (zserv_privs
.change (ZPRIVS_RAISE
))
270 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
274 ret
= sendto (nl
->sock
, (void *) &req
, sizeof req
, 0,
275 (struct sockaddr
*) &snl
, sizeof snl
);
278 if (zserv_privs
.change (ZPRIVS_LOWER
))
279 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
283 zlog (NULL
, LOG_ERR
, "%s sendto failed: %s", nl
->name
,
284 safe_strerror (save_errno
));
291 /* Receive message from netlink interface and pass those information
292 to the given function. */
294 netlink_parse_info (int (*filter
) (struct sockaddr_nl
*, struct nlmsghdr
*),
304 struct iovec iov
= { buf
, sizeof buf
};
305 struct sockaddr_nl snl
;
306 struct msghdr msg
= { (void *) &snl
, sizeof snl
, &iov
, 1, NULL
, 0, 0 };
310 if (zserv_privs
.change (ZPRIVS_RAISE
))
311 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
313 status
= recvmsg (nl
->sock
, &msg
, 0);
316 if (zserv_privs
.change (ZPRIVS_LOWER
))
317 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
321 if (save_errno
== EINTR
)
323 if (save_errno
== EWOULDBLOCK
|| save_errno
== EAGAIN
)
325 zlog (NULL
, LOG_ERR
, "%s recvmsg overrun: %s",
326 nl
->name
, safe_strerror(save_errno
));
332 zlog (NULL
, LOG_ERR
, "%s EOF", nl
->name
);
336 if (msg
.msg_namelen
!= sizeof snl
)
338 zlog (NULL
, LOG_ERR
, "%s sender address length error: length %d",
339 nl
->name
, msg
.msg_namelen
);
343 /* JF: Ignore messages that aren't from the kernel */
344 if ( snl
.nl_pid
!= 0 )
346 zlog ( NULL
, LOG_ERR
, "Ignoring message from pid %u", snl
.nl_pid
);
350 for (h
= (struct nlmsghdr
*) buf
; NLMSG_OK (h
, status
);
351 h
= NLMSG_NEXT (h
, status
))
353 /* Finish of reading. */
354 if (h
->nlmsg_type
== NLMSG_DONE
)
357 /* Error handling. */
358 if (h
->nlmsg_type
== NLMSG_ERROR
)
360 struct nlmsgerr
*err
= (struct nlmsgerr
*) NLMSG_DATA (h
);
362 /* If the error field is zero, then this is an ACK */
365 if (IS_ZEBRA_DEBUG_KERNEL
)
367 zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%d",
368 __FUNCTION__
, nl
->name
,
369 lookup (nlmsg_str
, err
->msg
.nlmsg_type
),
370 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
374 /* return if not a multipart message, otherwise continue */
375 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
382 if (h
->nlmsg_len
< NLMSG_LENGTH (sizeof (struct nlmsgerr
)))
384 zlog (NULL
, LOG_ERR
, "%s error: message truncated",
389 /* Deal with Error Noise - MAG */
391 int loglvl
= LOG_ERR
;
392 int errnum
= err
->error
;
393 int msg_type
= err
->msg
.nlmsg_type
;
395 if (nl
== &netlink_cmd
396 && (-errnum
== ENODEV
|| -errnum
== ESRCH
)
397 && (msg_type
== RTM_NEWROUTE
|| msg_type
== RTM_DELROUTE
))
400 zlog (NULL
, loglvl
, "%s error: %s, type=%s(%u), "
402 nl
->name
, safe_strerror (-errnum
),
403 lookup (nlmsg_str
, msg_type
),
404 msg_type
, err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
413 /* OK we got netlink message. */
414 if (IS_ZEBRA_DEBUG_KERNEL
)
415 zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%d",
417 lookup (nlmsg_str
, h
->nlmsg_type
), h
->nlmsg_type
,
418 h
->nlmsg_seq
, h
->nlmsg_pid
);
420 /* skip unsolicited messages originating from command socket */
421 if (nl
!= &netlink_cmd
&& h
->nlmsg_pid
== netlink_cmd
.snl
.nl_pid
)
423 if (IS_ZEBRA_DEBUG_KERNEL
)
424 zlog_debug ("netlink_parse_info: %s packet comes from %s",
425 nl
->name
, netlink_cmd
.name
);
429 error
= (*filter
) (&snl
, h
);
432 zlog (NULL
, LOG_ERR
, "%s filter function error", nl
->name
);
437 /* After error care. */
438 if (msg
.msg_flags
& MSG_TRUNC
)
440 zlog (NULL
, LOG_ERR
, "%s error: message truncated", nl
->name
);
445 zlog (NULL
, LOG_ERR
, "%s error: data remnant size %d", nl
->name
,
453 /* Utility function for parse rtattr. */
455 netlink_parse_rtattr (struct rtattr
**tb
, int max
, struct rtattr
*rta
,
458 while (RTA_OK (rta
, len
))
460 if (rta
->rta_type
<= max
)
461 tb
[rta
->rta_type
] = rta
;
462 rta
= RTA_NEXT (rta
, len
);
466 /* Called from interface_lookup_netlink(). This function is only used
469 netlink_interface (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
)
472 struct ifinfomsg
*ifi
;
473 struct rtattr
*tb
[IFLA_MAX
+ 1];
474 struct interface
*ifp
;
478 ifi
= NLMSG_DATA (h
);
480 if (h
->nlmsg_type
!= RTM_NEWLINK
)
483 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifinfomsg
));
487 /* Looking up interface name. */
488 memset (tb
, 0, sizeof tb
);
489 netlink_parse_rtattr (tb
, IFLA_MAX
, IFLA_RTA (ifi
), len
);
492 /* check for wireless messages to ignore */
493 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0))
495 if (IS_ZEBRA_DEBUG_KERNEL
)
496 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__
);
499 #endif /* IFLA_WIRELESS */
501 if (tb
[IFLA_IFNAME
] == NULL
)
503 name
= (char *) RTA_DATA (tb
[IFLA_IFNAME
]);
506 ifp
= if_get_by_name (name
);
508 ifp
->ifindex
= ifi
->ifi_index
;
509 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
510 ifp
->mtu6
= ifp
->mtu
= *(int *) RTA_DATA (tb
[IFLA_MTU
]);
513 /* Hardware type and address. */
514 ifp
->hw_type
= ifi
->ifi_type
;
516 if (tb
[IFLA_ADDRESS
])
520 hw_addr_len
= RTA_PAYLOAD (tb
[IFLA_ADDRESS
]);
522 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
523 zlog_warn ("Hardware address is too large: %d", hw_addr_len
);
526 ifp
->hw_addr_len
= hw_addr_len
;
527 memcpy (ifp
->hw_addr
, RTA_DATA (tb
[IFLA_ADDRESS
]), hw_addr_len
);
529 for (i
= 0; i
< hw_addr_len
; i
++)
530 if (ifp
->hw_addr
[i
] != 0)
533 if (i
== hw_addr_len
)
534 ifp
->hw_addr_len
= 0;
536 ifp
->hw_addr_len
= hw_addr_len
;
545 /* Lookup interface IPv4/IPv6 address. */
547 netlink_interface_addr (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
)
550 struct ifaddrmsg
*ifa
;
551 struct rtattr
*tb
[IFA_MAX
+ 1];
552 struct interface
*ifp
;
558 ifa
= NLMSG_DATA (h
);
560 if (ifa
->ifa_family
!= AF_INET
562 && ifa
->ifa_family
!= AF_INET6
563 #endif /* HAVE_IPV6 */
567 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
570 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifaddrmsg
));
574 memset (tb
, 0, sizeof tb
);
575 netlink_parse_rtattr (tb
, IFA_MAX
, IFA_RTA (ifa
), len
);
577 ifp
= if_lookup_by_index (ifa
->ifa_index
);
580 zlog_err ("netlink_interface_addr can't find interface by index %d",
585 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
588 zlog_debug ("netlink_interface_addr %s %s/%d:",
589 lookup (nlmsg_str
, h
->nlmsg_type
),
590 ifp
->name
, ifa
->ifa_prefixlen
);
592 zlog_debug (" IFA_LOCAL %s", inet_ntop (ifa
->ifa_family
,
593 RTA_DATA (tb
[IFA_LOCAL
]),
596 zlog_debug (" IFA_ADDRESS %s", inet_ntop (ifa
->ifa_family
,
600 if (tb
[IFA_BROADCAST
])
601 zlog_debug (" IFA_BROADCAST %s", inet_ntop (ifa
->ifa_family
,
605 if (tb
[IFA_LABEL
] && strcmp (ifp
->name
, RTA_DATA (tb
[IFA_LABEL
])))
606 zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb
[IFA_LABEL
]));
609 if (tb
[IFA_ADDRESS
] == NULL
)
610 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
612 if (ifp
->flags
& IFF_POINTOPOINT
)
616 addr
= RTA_DATA (tb
[IFA_LOCAL
]);
617 if (tb
[IFA_ADDRESS
] &&
618 memcmp(RTA_DATA(tb
[IFA_ADDRESS
]),RTA_DATA(tb
[IFA_LOCAL
]),4))
619 /* if IFA_ADDRESS != IFA_LOCAL, then it's the peer address */
620 broad
= RTA_DATA (tb
[IFA_ADDRESS
]);
627 addr
= RTA_DATA (tb
[IFA_ADDRESS
]);
635 addr
= RTA_DATA (tb
[IFA_ADDRESS
]);
639 if (tb
[IFA_BROADCAST
])
640 broad
= RTA_DATA(tb
[IFA_BROADCAST
]);
646 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
647 SET_FLAG (flags
, ZEBRA_IFA_SECONDARY
);
651 label
= (char *) RTA_DATA (tb
[IFA_LABEL
]);
653 if (ifp
&& label
&& strcmp (ifp
->name
, label
) == 0)
656 /* Register interface address to the interface. */
657 if (ifa
->ifa_family
== AF_INET
)
659 if (h
->nlmsg_type
== RTM_NEWADDR
)
660 connected_add_ipv4 (ifp
, flags
,
661 (struct in_addr
*) addr
, ifa
->ifa_prefixlen
,
662 (struct in_addr
*) broad
, label
);
664 connected_delete_ipv4 (ifp
, flags
,
665 (struct in_addr
*) addr
, ifa
->ifa_prefixlen
,
666 (struct in_addr
*) broad
, label
);
669 if (ifa
->ifa_family
== AF_INET6
)
671 if (h
->nlmsg_type
== RTM_NEWADDR
)
672 connected_add_ipv6 (ifp
,
673 (struct in6_addr
*) addr
, ifa
->ifa_prefixlen
,
674 (struct in6_addr
*) broad
);
676 connected_delete_ipv6 (ifp
,
677 (struct in6_addr
*) addr
, ifa
->ifa_prefixlen
,
678 (struct in6_addr
*) broad
);
680 #endif /* HAVE_IPV6 */
685 /* Looking up routing table by netlink interface. */
687 netlink_routing_table (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
)
691 struct rtattr
*tb
[RTA_MAX
+ 1];
694 char anyaddr
[16] = { 0 };
703 rtm
= NLMSG_DATA (h
);
705 if (h
->nlmsg_type
!= RTM_NEWROUTE
)
707 if (rtm
->rtm_type
!= RTN_UNICAST
)
710 table
= rtm
->rtm_table
;
711 #if 0 /* we weed them out later in rib_weed_tables () */
712 if (table
!= RT_TABLE_MAIN
&& table
!= zebrad
.rtm_table_default
)
716 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct rtmsg
));
720 memset (tb
, 0, sizeof tb
);
721 netlink_parse_rtattr (tb
, RTA_MAX
, RTM_RTA (rtm
), len
);
723 if (rtm
->rtm_flags
& RTM_F_CLONED
)
725 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
727 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
730 if (rtm
->rtm_src_len
!= 0)
733 /* Route which inserted by Zebra. */
734 if (rtm
->rtm_protocol
== RTPROT_ZEBRA
)
735 flags
|= ZEBRA_FLAG_SELFROUTE
;
743 index
= *(int *) RTA_DATA (tb
[RTA_OIF
]);
746 dest
= RTA_DATA (tb
[RTA_DST
]);
750 /* Multipath treatment is needed. */
752 gate
= RTA_DATA (tb
[RTA_GATEWAY
]);
754 if (tb
[RTA_PRIORITY
])
755 metric
= *(int *) RTA_DATA(tb
[RTA_PRIORITY
]);
757 if (rtm
->rtm_family
== AF_INET
)
759 struct prefix_ipv4 p
;
761 memcpy (&p
.prefix
, dest
, 4);
762 p
.prefixlen
= rtm
->rtm_dst_len
;
764 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, flags
, &p
, gate
, index
, table
, metric
, 0);
767 if (rtm
->rtm_family
== AF_INET6
)
769 struct prefix_ipv6 p
;
771 memcpy (&p
.prefix
, dest
, 16);
772 p
.prefixlen
= rtm
->rtm_dst_len
;
774 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL
, flags
, &p
, gate
, index
, table
);
776 #endif /* HAVE_IPV6 */
781 struct message rtproto_str
[] = {
782 {RTPROT_REDIRECT
, "redirect"},
783 {RTPROT_KERNEL
, "kernel"},
784 {RTPROT_BOOT
, "boot"},
785 {RTPROT_STATIC
, "static"},
786 {RTPROT_GATED
, "GateD"},
787 {RTPROT_RA
, "router advertisement"},
789 {RTPROT_ZEBRA
, "Zebra"},
791 {RTPROT_BIRD
, "BIRD"},
792 #endif /* RTPROT_BIRD */
796 /* Routing information change from the kernel. */
798 netlink_route_change (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
)
802 struct rtattr
*tb
[RTA_MAX
+ 1];
804 char anyaddr
[16] = { 0 };
811 rtm
= NLMSG_DATA (h
);
813 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
))
815 /* If this is not route add/delete message print warning. */
816 zlog_warn ("Kernel message: %d\n", h
->nlmsg_type
);
820 /* Connected route. */
821 if (IS_ZEBRA_DEBUG_KERNEL
)
822 zlog_debug ("%s %s %s proto %s",
824 RTM_NEWROUTE
? "RTM_NEWROUTE" : "RTM_DELROUTE",
825 rtm
->rtm_family
== AF_INET
? "ipv4" : "ipv6",
826 rtm
->rtm_type
== RTN_UNICAST
? "unicast" : "multicast",
827 lookup (rtproto_str
, rtm
->rtm_protocol
));
829 if (rtm
->rtm_type
!= RTN_UNICAST
)
834 table
= rtm
->rtm_table
;
835 if (table
!= RT_TABLE_MAIN
&& table
!= zebrad
.rtm_table_default
)
840 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct rtmsg
));
844 memset (tb
, 0, sizeof tb
);
845 netlink_parse_rtattr (tb
, RTA_MAX
, RTM_RTA (rtm
), len
);
847 if (rtm
->rtm_flags
& RTM_F_CLONED
)
849 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
851 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
854 if (rtm
->rtm_protocol
== RTPROT_ZEBRA
&& h
->nlmsg_type
== RTM_NEWROUTE
)
857 if (rtm
->rtm_src_len
!= 0)
859 zlog_warn ("netlink_route_change(): no src len");
868 index
= *(int *) RTA_DATA (tb
[RTA_OIF
]);
871 dest
= RTA_DATA (tb
[RTA_DST
]);
876 gate
= RTA_DATA (tb
[RTA_GATEWAY
]);
878 if (rtm
->rtm_family
== AF_INET
)
880 struct prefix_ipv4 p
;
882 memcpy (&p
.prefix
, dest
, 4);
883 p
.prefixlen
= rtm
->rtm_dst_len
;
885 if (IS_ZEBRA_DEBUG_KERNEL
)
887 if (h
->nlmsg_type
== RTM_NEWROUTE
)
888 zlog_debug ("RTM_NEWROUTE %s/%d",
889 inet_ntoa (p
.prefix
), p
.prefixlen
);
891 zlog_debug ("RTM_DELROUTE %s/%d",
892 inet_ntoa (p
.prefix
), p
.prefixlen
);
895 if (h
->nlmsg_type
== RTM_NEWROUTE
)
896 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, 0, &p
, gate
, index
, table
, 0, 0);
898 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL
, 0, &p
, gate
, index
, table
);
902 if (rtm
->rtm_family
== AF_INET6
)
904 struct prefix_ipv6 p
;
908 memcpy (&p
.prefix
, dest
, 16);
909 p
.prefixlen
= rtm
->rtm_dst_len
;
911 if (IS_ZEBRA_DEBUG_KERNEL
)
913 if (h
->nlmsg_type
== RTM_NEWROUTE
)
914 zlog_debug ("RTM_NEWROUTE %s/%d",
915 inet_ntop (AF_INET6
, &p
.prefix
, buf
, BUFSIZ
),
918 zlog_debug ("RTM_DELROUTE %s/%d",
919 inet_ntop (AF_INET6
, &p
.prefix
, buf
, BUFSIZ
),
923 if (h
->nlmsg_type
== RTM_NEWROUTE
)
924 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL
, 0, &p
, gate
, index
, 0);
926 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL
, 0, &p
, gate
, index
, 0);
928 #endif /* HAVE_IPV6 */
934 netlink_link_change (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
)
937 struct ifinfomsg
*ifi
;
938 struct rtattr
*tb
[IFLA_MAX
+ 1];
939 struct interface
*ifp
;
942 ifi
= NLMSG_DATA (h
);
944 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
))
946 /* If this is not link add/delete message so print warning. */
947 zlog_warn ("netlink_link_change: wrong kernel message %d\n",
952 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifinfomsg
));
956 /* Looking up interface name. */
957 memset (tb
, 0, sizeof tb
);
958 netlink_parse_rtattr (tb
, IFLA_MAX
, IFLA_RTA (ifi
), len
);
961 /* check for wireless messages to ignore */
962 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0))
964 if (IS_ZEBRA_DEBUG_KERNEL
)
965 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__
);
968 #endif /* IFLA_WIRELESS */
970 if (tb
[IFLA_IFNAME
] == NULL
)
972 name
= (char *) RTA_DATA (tb
[IFLA_IFNAME
]);
975 if (h
->nlmsg_type
== RTM_NEWLINK
)
977 ifp
= if_lookup_by_name (name
);
979 if (ifp
== NULL
|| !CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
982 ifp
= if_get_by_name (name
);
984 ifp
->ifindex
= ifi
->ifi_index
;
985 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
986 ifp
->mtu6
= ifp
->mtu
= *(int *) RTA_DATA (tb
[IFLA_MTU
]);
989 /* If new link is added. */
994 /* Interface status change. */
995 ifp
->ifindex
= ifi
->ifi_index
;
996 ifp
->mtu6
= ifp
->mtu
= *(int *) RTA_DATA (tb
[IFLA_MTU
]);
999 if (if_is_operative (ifp
))
1001 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1002 if (!if_is_operative (ifp
))
1005 /* Must notify client daemons of new interface status. */
1006 zebra_interface_up_update (ifp
);
1010 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1011 if (if_is_operative (ifp
))
1019 ifp
= if_lookup_by_name (name
);
1023 zlog (NULL
, LOG_WARNING
, "interface %s is deleted but can't find",
1028 if_delete_update (ifp
);
1035 netlink_information_fetch (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
)
1037 switch (h
->nlmsg_type
)
1040 return netlink_route_change (snl
, h
);
1043 return netlink_route_change (snl
, h
);
1046 return netlink_link_change (snl
, h
);
1049 return netlink_link_change (snl
, h
);
1052 return netlink_interface_addr (snl
, h
);
1055 return netlink_interface_addr (snl
, h
);
1058 zlog_warn ("Unknown netlink nlmsg_type %d\n", h
->nlmsg_type
);
1064 /* Interface lookup by netlink socket. */
1066 interface_lookup_netlink ()
1073 * Change netlink socket flags to blocking to ensure we get
1074 * a reply via nelink_parse_info
1076 snb_ret
= set_netlink_blocking (&netlink_cmd
, &flags
);
1078 zlog (NULL
, LOG_WARNING
,
1079 "%s:%i Warning: Could not set netlink socket to blocking.",
1080 __FUNCTION__
, __LINE__
);
1082 /* Get interface information. */
1083 ret
= netlink_request (AF_PACKET
, RTM_GETLINK
, &netlink_cmd
);
1086 ret
= netlink_parse_info (netlink_interface
, &netlink_cmd
);
1090 /* Get IPv4 address of the interfaces. */
1091 ret
= netlink_request (AF_INET
, RTM_GETADDR
, &netlink_cmd
);
1094 ret
= netlink_parse_info (netlink_interface_addr
, &netlink_cmd
);
1099 /* Get IPv6 address of the interfaces. */
1100 ret
= netlink_request (AF_INET6
, RTM_GETADDR
, &netlink_cmd
);
1103 ret
= netlink_parse_info (netlink_interface_addr
, &netlink_cmd
);
1106 #endif /* HAVE_IPV6 */
1108 /* restore socket flags */
1110 set_netlink_nonblocking (&netlink_cmd
, &flags
);
1114 /* Routing table read function using netlink interface. Only called
1117 netlink_route_read ()
1124 * Change netlink socket flags to blocking to ensure we get
1125 * a reply via nelink_parse_info
1127 snb_ret
= set_netlink_blocking (&netlink_cmd
, &flags
);
1129 zlog (NULL
, LOG_WARNING
,
1130 "%s:%i Warning: Could not set netlink socket to blocking.",
1131 __FUNCTION__
, __LINE__
);
1133 /* Get IPv4 routing table. */
1134 ret
= netlink_request (AF_INET
, RTM_GETROUTE
, &netlink_cmd
);
1137 ret
= netlink_parse_info (netlink_routing_table
, &netlink_cmd
);
1142 /* Get IPv6 routing table. */
1143 ret
= netlink_request (AF_INET6
, RTM_GETROUTE
, &netlink_cmd
);
1146 ret
= netlink_parse_info (netlink_routing_table
, &netlink_cmd
);
1149 #endif /* HAVE_IPV6 */
1153 set_netlink_nonblocking (&netlink_cmd
, &flags
);
1157 /* Utility function comes from iproute2.
1158 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1160 addattr_l (struct nlmsghdr
*n
, int maxlen
, int type
, void *data
, int alen
)
1165 len
= RTA_LENGTH (alen
);
1167 if (NLMSG_ALIGN (n
->nlmsg_len
) + len
> maxlen
)
1170 rta
= (struct rtattr
*) (((char *) n
) + NLMSG_ALIGN (n
->nlmsg_len
));
1171 rta
->rta_type
= type
;
1173 memcpy (RTA_DATA (rta
), data
, alen
);
1174 n
->nlmsg_len
= NLMSG_ALIGN (n
->nlmsg_len
) + len
;
1180 rta_addattr_l (struct rtattr
*rta
, int maxlen
, int type
, void *data
, int alen
)
1183 struct rtattr
*subrta
;
1185 len
= RTA_LENGTH (alen
);
1187 if (RTA_ALIGN (rta
->rta_len
) + len
> maxlen
)
1190 subrta
= (struct rtattr
*) (((char *) rta
) + RTA_ALIGN (rta
->rta_len
));
1191 subrta
->rta_type
= type
;
1192 subrta
->rta_len
= len
;
1193 memcpy (RTA_DATA (subrta
), data
, alen
);
1194 rta
->rta_len
= NLMSG_ALIGN (rta
->rta_len
) + len
;
1199 /* Utility function comes from iproute2.
1200 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1202 addattr32 (struct nlmsghdr
*n
, int maxlen
, int type
, int data
)
1207 len
= RTA_LENGTH (4);
1209 if (NLMSG_ALIGN (n
->nlmsg_len
) + len
> maxlen
)
1212 rta
= (struct rtattr
*) (((char *) n
) + NLMSG_ALIGN (n
->nlmsg_len
));
1213 rta
->rta_type
= type
;
1215 memcpy (RTA_DATA (rta
), &data
, 4);
1216 n
->nlmsg_len
= NLMSG_ALIGN (n
->nlmsg_len
) + len
;
1222 netlink_talk_filter (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
)
1224 zlog_warn ("netlink_talk: ignoring message type 0x%04x", h
->nlmsg_type
);
1228 /* sendmsg() to netlink socket then recvmsg(). */
1230 netlink_talk (struct nlmsghdr
*n
, struct nlsock
*nl
)
1233 struct sockaddr_nl snl
;
1234 struct iovec iov
= { (void *) n
, n
->nlmsg_len
};
1235 struct msghdr msg
= { (void *) &snl
, sizeof snl
, &iov
, 1, NULL
, 0, 0 };
1240 memset (&snl
, 0, sizeof snl
);
1241 snl
.nl_family
= AF_NETLINK
;
1243 n
->nlmsg_seq
= ++nl
->seq
;
1245 /* Request an acknowledgement by setting NLM_F_ACK */
1246 n
->nlmsg_flags
|= NLM_F_ACK
;
1248 if (IS_ZEBRA_DEBUG_KERNEL
)
1249 zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl
->name
,
1250 lookup (nlmsg_str
, n
->nlmsg_type
), n
->nlmsg_type
,
1253 /* Send message to netlink interface. */
1254 if (zserv_privs
.change (ZPRIVS_RAISE
))
1255 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
1256 status
= sendmsg (nl
->sock
, &msg
, 0);
1258 if (zserv_privs
.change (ZPRIVS_LOWER
))
1259 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
1263 zlog (NULL
, LOG_ERR
, "netlink_talk sendmsg() error: %s",
1264 safe_strerror (save_errno
));
1269 * Change socket flags for blocking I/O.
1270 * This ensures we wait for a reply in netlink_parse_info().
1272 snb_ret
= set_netlink_blocking (nl
, &flags
);
1274 zlog (NULL
, LOG_WARNING
,
1275 "%s:%i Warning: Could not set netlink socket to blocking.",
1276 __FUNCTION__
, __LINE__
);
1279 * Get reply from netlink socket.
1280 * The reply should either be an acknowlegement or an error.
1282 status
= netlink_parse_info (netlink_talk_filter
, nl
);
1284 /* Restore socket flags for nonblocking I/O */
1286 set_netlink_nonblocking (nl
, &flags
);
1291 /* Routing table change via netlink interface. */
1293 netlink_route (int cmd
, int family
, void *dest
, int length
, void *gate
,
1294 int index
, int zebra_flags
, int table
)
1298 struct sockaddr_nl snl
;
1308 memset (&req
, 0, sizeof req
);
1310 bytelen
= (family
== AF_INET
? 4 : 16);
1312 req
.n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct rtmsg
));
1313 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1314 req
.n
.nlmsg_type
= cmd
;
1315 req
.r
.rtm_family
= family
;
1316 req
.r
.rtm_table
= table
;
1317 req
.r
.rtm_dst_len
= length
;
1319 if ((zebra_flags
& ZEBRA_FLAG_BLACKHOLE
)
1320 || (zebra_flags
& ZEBRA_FLAG_REJECT
))
1325 if (cmd
== RTM_NEWROUTE
)
1327 req
.r
.rtm_protocol
= RTPROT_ZEBRA
;
1328 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1332 if (zebra_flags
& ZEBRA_FLAG_BLACKHOLE
)
1333 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1334 else if (zebra_flags
& ZEBRA_FLAG_REJECT
)
1335 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1337 assert (RTN_BLACKHOLE
!= RTN_UNREACHABLE
); /* false */
1340 req
.r
.rtm_type
= RTN_UNICAST
;
1344 addattr_l (&req
.n
, sizeof req
, RTA_DST
, dest
, bytelen
);
1349 addattr_l (&req
.n
, sizeof req
, RTA_GATEWAY
, gate
, bytelen
);
1351 addattr32 (&req
.n
, sizeof req
, RTA_OIF
, index
);
1354 /* Destination netlink address. */
1355 memset (&snl
, 0, sizeof snl
);
1356 snl
.nl_family
= AF_NETLINK
;
1358 /* Talk to netlink socket. */
1359 ret
= netlink_talk (&req
.n
, &netlink_cmd
);
1366 /* Routing table change via netlink interface. */
1368 netlink_route_multipath (int cmd
, struct prefix
*p
, struct rib
*rib
,
1372 struct sockaddr_nl snl
;
1373 struct nexthop
*nexthop
= NULL
;
1374 int nexthop_num
= 0;
1384 memset (&req
, 0, sizeof req
);
1386 bytelen
= (family
== AF_INET
? 4 : 16);
1388 req
.n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct rtmsg
));
1389 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1390 req
.n
.nlmsg_type
= cmd
;
1391 req
.r
.rtm_family
= family
;
1392 req
.r
.rtm_table
= rib
->table
;
1393 req
.r
.rtm_dst_len
= p
->prefixlen
;
1395 if ((rib
->flags
& ZEBRA_FLAG_BLACKHOLE
) || (rib
->flags
& ZEBRA_FLAG_REJECT
))
1400 if (cmd
== RTM_NEWROUTE
)
1402 req
.r
.rtm_protocol
= RTPROT_ZEBRA
;
1403 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1407 if (rib
->flags
& ZEBRA_FLAG_BLACKHOLE
)
1408 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1409 else if (rib
->flags
& ZEBRA_FLAG_REJECT
)
1410 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1412 assert (RTN_BLACKHOLE
!= RTN_UNREACHABLE
); /* false */
1415 req
.r
.rtm_type
= RTN_UNICAST
;
1418 addattr_l (&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
1421 addattr32 (&req
.n
, sizeof req
, RTA_PRIORITY
, rib
->metric
);
1425 if (cmd
== RTM_NEWROUTE
)
1426 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1427 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1431 /* Multipath case. */
1432 if (rib
->nexthop_active_num
== 1 || MULTIPATH_NUM
== 1)
1434 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1437 if ((cmd
== RTM_NEWROUTE
1438 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1439 || (cmd
== RTM_DELROUTE
1440 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)))
1443 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1445 if (IS_ZEBRA_DEBUG_KERNEL
)
1448 ("netlink_route_multipath() (recursive, 1 hop): "
1449 "%s %s/%d via %s if %u, type %s",
1450 lookup (nlmsg_str
, cmd
), inet_ntoa (p
->u
.prefix4
),
1451 p
->prefixlen
, inet_ntoa (nexthop
->rgate
.ipv4
),
1453 nexthop_types_desc
[nexthop
->rtype
]);
1456 if (nexthop
->rtype
== NEXTHOP_TYPE_IPV4
1457 || nexthop
->rtype
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1458 addattr_l (&req
.n
, sizeof req
, RTA_GATEWAY
,
1459 &nexthop
->rgate
.ipv4
, bytelen
);
1461 if (nexthop
->rtype
== NEXTHOP_TYPE_IPV6
1462 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFINDEX
1463 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFNAME
)
1464 addattr_l (&req
.n
, sizeof req
, RTA_GATEWAY
,
1465 &nexthop
->rgate
.ipv6
, bytelen
);
1466 #endif /* HAVE_IPV6 */
1467 if (nexthop
->rtype
== NEXTHOP_TYPE_IFINDEX
1468 || nexthop
->rtype
== NEXTHOP_TYPE_IFNAME
1469 || nexthop
->rtype
== NEXTHOP_TYPE_IPV4_IFINDEX
1470 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFINDEX
1471 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFNAME
)
1472 addattr32 (&req
.n
, sizeof req
, RTA_OIF
,
1477 if (IS_ZEBRA_DEBUG_KERNEL
)
1480 ("netlink_route_multipath(): (single hop)"
1481 "%s %s/%d via %s if %u, type %s",
1482 lookup (nlmsg_str
, cmd
), inet_ntoa (p
->u
.prefix4
),
1483 p
->prefixlen
, inet_ntoa (nexthop
->gate
.ipv4
),
1485 nexthop_types_desc
[nexthop
->type
]);
1488 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1489 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1490 addattr_l (&req
.n
, sizeof req
, RTA_GATEWAY
,
1491 &nexthop
->gate
.ipv4
, bytelen
);
1493 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1494 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
1495 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1496 addattr_l (&req
.n
, sizeof req
, RTA_GATEWAY
,
1497 &nexthop
->gate
.ipv6
, bytelen
);
1498 #endif /* HAVE_IPV6 */
1499 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1500 || nexthop
->type
== NEXTHOP_TYPE_IFNAME
1501 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1502 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
1503 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
1504 addattr32 (&req
.n
, sizeof req
, RTA_OIF
, nexthop
->ifindex
);
1507 if (cmd
== RTM_NEWROUTE
)
1508 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1518 struct rtattr
*rta
= (void *) buf
;
1519 struct rtnexthop
*rtnh
;
1521 rta
->rta_type
= RTA_MULTIPATH
;
1522 rta
->rta_len
= RTA_LENGTH (0);
1523 rtnh
= RTA_DATA (rta
);
1526 for (nexthop
= rib
->nexthop
;
1527 nexthop
&& (MULTIPATH_NUM
== 0 || nexthop_num
< MULTIPATH_NUM
);
1528 nexthop
= nexthop
->next
)
1530 if ((cmd
== RTM_NEWROUTE
1531 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1532 || (cmd
== RTM_DELROUTE
1533 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)))
1537 rtnh
->rtnh_len
= sizeof (*rtnh
);
1538 rtnh
->rtnh_flags
= 0;
1539 rtnh
->rtnh_hops
= 0;
1540 rta
->rta_len
+= rtnh
->rtnh_len
;
1542 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1544 if (IS_ZEBRA_DEBUG_KERNEL
)
1546 zlog_debug ("netlink_route_multipath() "
1547 "(recursive, multihop): "
1548 "%s %s/%d via %s if %u, type %s",
1549 lookup (nlmsg_str
, cmd
), inet_ntoa (p
->u
.prefix4
),
1550 p
->prefixlen
, inet_ntoa (nexthop
->rgate
.ipv4
),
1552 nexthop_types_desc
[nexthop
->type
]);
1554 if (nexthop
->rtype
== NEXTHOP_TYPE_IPV4
1555 || nexthop
->rtype
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1557 rta_addattr_l (rta
, 4096, RTA_GATEWAY
,
1558 &nexthop
->rgate
.ipv4
, bytelen
);
1559 rtnh
->rtnh_len
+= sizeof (struct rtattr
) + 4;
1562 if (nexthop
->rtype
== NEXTHOP_TYPE_IPV6
1563 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFNAME
1564 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1565 rta_addattr_l (rta
, 4096, RTA_GATEWAY
,
1566 &nexthop
->rgate
.ipv6
, bytelen
);
1567 #endif /* HAVE_IPV6 */
1569 if (nexthop
->rtype
== NEXTHOP_TYPE_IFINDEX
1570 || nexthop
->rtype
== NEXTHOP_TYPE_IFNAME
1571 || nexthop
->rtype
== NEXTHOP_TYPE_IPV4_IFINDEX
1572 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFINDEX
1573 || nexthop
->rtype
== NEXTHOP_TYPE_IPV6_IFNAME
)
1574 rtnh
->rtnh_ifindex
= nexthop
->rifindex
;
1576 rtnh
->rtnh_ifindex
= 0;
1580 if (IS_ZEBRA_DEBUG_KERNEL
)
1582 zlog_debug ("netlink_route_multipath() "
1584 "%s %s/%d via %s if %u, type %s",
1585 lookup (nlmsg_str
, cmd
), inet_ntoa (p
->u
.prefix4
),
1586 p
->prefixlen
, inet_ntoa (nexthop
->rgate
.ipv4
),
1588 nexthop_types_desc
[nexthop
->type
]);
1590 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1591 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1593 rta_addattr_l (rta
, 4096, RTA_GATEWAY
,
1594 &nexthop
->gate
.ipv4
, bytelen
);
1595 rtnh
->rtnh_len
+= sizeof (struct rtattr
) + 4;
1598 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1599 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
1600 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1601 rta_addattr_l (rta
, 4096, RTA_GATEWAY
,
1602 &nexthop
->gate
.ipv6
, bytelen
);
1603 #endif /* HAVE_IPV6 */
1605 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1606 || nexthop
->type
== NEXTHOP_TYPE_IFNAME
1607 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1608 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
1609 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1610 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1612 rtnh
->rtnh_ifindex
= 0;
1614 rtnh
= RTNH_NEXT (rtnh
);
1616 if (cmd
== RTM_NEWROUTE
)
1617 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1621 if (rta
->rta_len
> RTA_LENGTH (0))
1622 addattr_l (&req
.n
, 1024, RTA_MULTIPATH
, RTA_DATA (rta
),
1626 /* If there is no useful nexthop then return. */
1627 if (nexthop_num
== 0)
1629 if (IS_ZEBRA_DEBUG_KERNEL
)
1630 zlog_debug ("netlink_route_multipath(): No useful nexthop.");
1636 /* Destination netlink address. */
1637 memset (&snl
, 0, sizeof snl
);
1638 snl
.nl_family
= AF_NETLINK
;
1640 /* Talk to netlink socket. */
1641 return netlink_talk (&req
.n
, &netlink_cmd
);
1645 kernel_add_ipv4 (struct prefix
*p
, struct rib
*rib
)
1647 return netlink_route_multipath (RTM_NEWROUTE
, p
, rib
, AF_INET
);
1651 kernel_delete_ipv4 (struct prefix
*p
, struct rib
*rib
)
1653 return netlink_route_multipath (RTM_DELROUTE
, p
, rib
, AF_INET
);
1658 kernel_add_ipv6 (struct prefix
*p
, struct rib
*rib
)
1660 return netlink_route_multipath (RTM_NEWROUTE
, p
, rib
, AF_INET6
);
1664 kernel_delete_ipv6 (struct prefix
*p
, struct rib
*rib
)
1666 return netlink_route_multipath (RTM_DELROUTE
, p
, rib
, AF_INET6
);
1669 /* Delete IPv6 route from the kernel. */
1671 kernel_delete_ipv6_old (struct prefix_ipv6
*dest
, struct in6_addr
*gate
,
1672 int index
, int flags
, int table
)
1674 return netlink_route (RTM_DELROUTE
, AF_INET6
, &dest
->prefix
,
1675 dest
->prefixlen
, gate
, index
, flags
, table
);
1677 #endif /* HAVE_IPV6 */
1679 /* Interface address modification. */
1681 netlink_address (int cmd
, int family
, struct interface
*ifp
,
1682 struct connected
*ifc
)
1690 struct ifaddrmsg ifa
;
1695 memset (&req
, 0, sizeof req
);
1697 bytelen
= (family
== AF_INET
? 4 : 16);
1699 req
.n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct ifaddrmsg
));
1700 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
1701 req
.n
.nlmsg_type
= cmd
;
1702 req
.ifa
.ifa_family
= family
;
1704 req
.ifa
.ifa_index
= ifp
->ifindex
;
1705 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
1707 addattr_l (&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
1709 if (family
== AF_INET
&& cmd
== RTM_NEWADDR
)
1711 if (if_is_broadcast (ifp
) && ifc
->destination
)
1713 p
= ifc
->destination
;
1714 addattr_l (&req
.n
, sizeof req
, IFA_BROADCAST
, &p
->u
.prefix
,
1719 if (CHECK_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
))
1720 SET_FLAG (req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
1723 addattr_l (&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
1724 strlen (ifc
->label
) + 1);
1726 return netlink_talk (&req
.n
, &netlink_cmd
);
1730 kernel_address_add_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
1732 return netlink_address (RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
1736 kernel_address_delete_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
1738 return netlink_address (RTM_DELADDR
, AF_INET
, ifp
, ifc
);
1742 extern struct thread_master
*master
;
1744 /* Kernel route reflection. */
1746 kernel_read (struct thread
*thread
)
1751 sock
= THREAD_FD (thread
);
1752 ret
= netlink_parse_info (netlink_information_fetch
, &netlink
);
1753 thread_add_read (zebrad
.master
, kernel_read
, NULL
, netlink
.sock
);
1758 /* Exported interface function. This function simply calls
1759 netlink_socket (). */
1763 unsigned long groups
;
1765 groups
= RTMGRP_LINK
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_IFADDR
;
1767 groups
|= RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFADDR
;
1768 #endif /* HAVE_IPV6 */
1769 netlink_socket (&netlink
, groups
);
1770 netlink_socket (&netlink_cmd
, 0);
1772 /* Register kernel socket. */
1773 if (netlink
.sock
> 0)
1774 thread_add_read (zebrad
.master
, kernel_read
, NULL
, netlink
.sock
);