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"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zebra_vrf.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/interface.h"
48 #include "zebra/debug.h"
49 #include "zebra/rtadv.h"
51 #include "rt_netlink.h"
53 static const struct message nlmsg_str
[] = {
54 {RTM_NEWROUTE
, "RTM_NEWROUTE"},
55 {RTM_DELROUTE
, "RTM_DELROUTE"},
56 {RTM_GETROUTE
, "RTM_GETROUTE"},
57 {RTM_NEWLINK
, "RTM_NEWLINK"},
58 {RTM_DELLINK
, "RTM_DELLINK"},
59 {RTM_GETLINK
, "RTM_GETLINK"},
60 {RTM_NEWADDR
, "RTM_NEWADDR"},
61 {RTM_DELADDR
, "RTM_DELADDR"},
62 {RTM_GETADDR
, "RTM_GETADDR"},
63 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
64 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
65 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
69 extern struct zebra_privs_t zserv_privs
;
71 extern u_int32_t nl_rcvbufsize
;
73 /* Note: on netlink systems, there should be a 1-to-1 mapping between interface
74 names and ifindex values. */
76 set_ifindex(struct interface
*ifp
, unsigned int ifi_index
)
78 struct interface
*oifp
;
79 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
81 if (((oifp
= if_lookup_by_index_per_ns (zns
, ifi_index
)) != NULL
) && (oifp
!= ifp
))
83 if (ifi_index
== IFINDEX_INTERNAL
)
84 zlog_err("Netlink is setting interface %s ifindex to reserved "
85 "internal value %u", ifp
->name
, ifi_index
);
88 if (IS_ZEBRA_DEBUG_KERNEL
)
89 zlog_debug("interface index %d was renamed from %s to %s",
90 ifi_index
, oifp
->name
, ifp
->name
);
92 zlog_err("interface rename detected on up interface: index %d "
93 "was renamed from %s to %s, results are uncertain!",
94 ifi_index
, oifp
->name
, ifp
->name
);
95 if_delete_update(oifp
);
98 ifp
->ifindex
= ifi_index
;
101 #ifndef SO_RCVBUFFORCE
102 #define SO_RCVBUFFORCE (33)
106 netlink_recvbuf (struct nlsock
*nl
, uint32_t newsize
)
109 socklen_t newlen
= sizeof(newsize
);
110 socklen_t oldlen
= sizeof(oldsize
);
113 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
116 zlog (NULL
, LOG_ERR
, "Can't get %s receive buffer size: %s", nl
->name
,
117 safe_strerror (errno
));
121 /* Try force option (linux >= 2.6.14) and fall back to normal set */
122 if ( zserv_privs
.change (ZPRIVS_RAISE
) )
123 zlog_err ("routing_socket: Can't raise privileges");
124 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
, &nl_rcvbufsize
,
125 sizeof(nl_rcvbufsize
));
126 if ( zserv_privs
.change (ZPRIVS_LOWER
) )
127 zlog_err ("routing_socket: Can't lower privileges");
129 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &nl_rcvbufsize
,
130 sizeof(nl_rcvbufsize
));
133 zlog (NULL
, LOG_ERR
, "Can't set %s receive buffer size: %s", nl
->name
,
134 safe_strerror (errno
));
138 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
141 zlog (NULL
, LOG_ERR
, "Can't get %s receive buffer size: %s", nl
->name
,
142 safe_strerror (errno
));
146 zlog (NULL
, LOG_INFO
,
147 "Setting netlink socket receive buffer size: %u -> %u",
152 /* Make socket for Linux netlink interface. */
154 netlink_socket (struct nlsock
*nl
, unsigned long groups
, ns_id_t ns_id
)
157 struct sockaddr_nl snl
;
162 if (zserv_privs
.change (ZPRIVS_RAISE
))
164 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
168 sock
= socket (AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
171 zlog (NULL
, LOG_ERR
, "Can't open %s socket: %s", nl
->name
,
172 safe_strerror (errno
));
176 memset (&snl
, 0, sizeof snl
);
177 snl
.nl_family
= AF_NETLINK
;
178 snl
.nl_groups
= groups
;
180 /* Bind the socket to the netlink structure for anything. */
181 ret
= bind (sock
, (struct sockaddr
*) &snl
, sizeof snl
);
183 if (zserv_privs
.change (ZPRIVS_LOWER
))
184 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
188 zlog (NULL
, LOG_ERR
, "Can't bind %s socket to group 0x%x: %s",
189 nl
->name
, snl
.nl_groups
, safe_strerror (save_errno
));
194 /* multiple netlink sockets will have different nl_pid */
195 namelen
= sizeof snl
;
196 ret
= getsockname (sock
, (struct sockaddr
*) &snl
, (socklen_t
*) &namelen
);
197 if (ret
< 0 || namelen
!= sizeof snl
)
199 zlog (NULL
, LOG_ERR
, "Can't get %s socket name: %s", nl
->name
,
200 safe_strerror (errno
));
210 /* Get type specified information from netlink. */
212 netlink_request (int family
, int type
, struct nlsock
*nl
)
215 struct sockaddr_nl snl
;
224 /* Check netlink socket. */
227 zlog (NULL
, LOG_ERR
, "%s socket isn't active.", nl
->name
);
231 memset (&snl
, 0, sizeof snl
);
232 snl
.nl_family
= AF_NETLINK
;
234 memset (&req
, 0, sizeof req
);
235 req
.nlh
.nlmsg_len
= sizeof req
;
236 req
.nlh
.nlmsg_type
= type
;
237 req
.nlh
.nlmsg_flags
= NLM_F_ROOT
| NLM_F_MATCH
| NLM_F_REQUEST
;
238 req
.nlh
.nlmsg_pid
= nl
->snl
.nl_pid
;
239 req
.nlh
.nlmsg_seq
= ++nl
->seq
;
240 req
.g
.rtgen_family
= family
;
242 /* linux appears to check capabilities on every message
243 * have to raise caps for every message sent
245 if (zserv_privs
.change (ZPRIVS_RAISE
))
247 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
251 ret
= sendto (nl
->sock
, (void *) &req
, sizeof req
, 0,
252 (struct sockaddr
*) &snl
, sizeof snl
);
255 if (zserv_privs
.change (ZPRIVS_LOWER
))
256 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
260 zlog (NULL
, LOG_ERR
, "%s sendto failed: %s", nl
->name
,
261 safe_strerror (save_errno
));
269 Pending: create an efficient table_id (in a tree/hash) based lookup)
272 vrf_lookup_by_table (u_int32_t table_id
)
274 struct zebra_vrf
*zvrf
;
277 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
279 if ((zvrf
= vrf_iter2info (iter
)) == NULL
||
280 (zvrf
->table_id
!= table_id
))
289 /* Receive message from netlink interface and pass those information
290 to the given function. */
292 netlink_parse_info (int (*filter
) (struct sockaddr_nl
*, struct nlmsghdr
*,
294 struct nlsock
*nl
, struct zebra_ns
*zns
, int count
)
303 char buf
[NL_PKT_BUF_SIZE
];
306 .iov_len
= sizeof buf
308 struct sockaddr_nl snl
;
309 struct msghdr msg
= {
310 .msg_name
= (void *) &snl
,
311 .msg_namelen
= sizeof snl
,
317 if (count
&& read_in
>= count
)
320 status
= recvmsg (nl
->sock
, &msg
, 0);
325 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
327 zlog (NULL
, LOG_ERR
, "%s recvmsg overrun: %s",
328 nl
->name
, safe_strerror(errno
));
334 zlog (NULL
, LOG_ERR
, "%s EOF", nl
->name
);
338 if (msg
.msg_namelen
!= sizeof snl
)
340 zlog (NULL
, LOG_ERR
, "%s sender address length error: length %d",
341 nl
->name
, msg
.msg_namelen
);
346 for (h
= (struct nlmsghdr
*) buf
; NLMSG_OK (h
, (unsigned int) status
);
347 h
= NLMSG_NEXT (h
, status
))
349 /* Finish of reading. */
350 if (h
->nlmsg_type
== NLMSG_DONE
)
353 /* Error handling. */
354 if (h
->nlmsg_type
== NLMSG_ERROR
)
356 struct nlmsgerr
*err
= (struct nlmsgerr
*) NLMSG_DATA (h
);
357 int errnum
= err
->error
;
358 int msg_type
= err
->msg
.nlmsg_type
;
360 /* If the error field is zero, then this is an ACK */
363 if (IS_ZEBRA_DEBUG_KERNEL
)
365 zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
366 __FUNCTION__
, nl
->name
,
367 lookup (nlmsg_str
, err
->msg
.nlmsg_type
),
368 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
372 /* return if not a multipart message, otherwise continue */
373 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
380 if (h
->nlmsg_len
< NLMSG_LENGTH (sizeof (struct nlmsgerr
)))
382 zlog (NULL
, LOG_ERR
, "%s error: message truncated",
387 /* Deal with errors that occur because of races in link handling */
388 if (nl
== &zns
->netlink_cmd
389 && ((msg_type
== RTM_DELROUTE
&&
390 (-errnum
== ENODEV
|| -errnum
== ESRCH
))
391 || (msg_type
== RTM_NEWROUTE
&& -errnum
== EEXIST
)))
393 if (IS_ZEBRA_DEBUG_KERNEL
)
394 zlog_debug ("%s: error: %s type=%s(%u), seq=%u, pid=%u",
395 nl
->name
, safe_strerror (-errnum
),
396 lookup (nlmsg_str
, msg_type
),
397 msg_type
, err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
401 /* We see RTM_DELNEIGH when shutting down an interface with an IPv4
402 * link-local. The kernel should have already deleted the neighbor
403 * so do not log these as an error.
405 if (msg_type
== RTM_DELNEIGH
||
406 (nl
== &zns
->netlink_cmd
&& msg_type
== RTM_NEWROUTE
&&
407 (-errnum
== ESRCH
|| -errnum
== ENETUNREACH
)))
409 /* This is known to happen in some situations, don't log
412 if (IS_ZEBRA_DEBUG_KERNEL
)
413 zlog_debug ("%s error: %s, type=%s(%u), seq=%u, pid=%u",
414 nl
->name
, safe_strerror (-errnum
),
415 lookup (nlmsg_str
, msg_type
),
416 msg_type
, err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
419 zlog_err ("%s error: %s, type=%s(%u), seq=%u, pid=%u",
420 nl
->name
, safe_strerror (-errnum
),
421 lookup (nlmsg_str
, msg_type
),
422 msg_type
, err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
427 /* OK we got netlink message. */
428 if (IS_ZEBRA_DEBUG_KERNEL
)
429 zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%u",
431 lookup (nlmsg_str
, h
->nlmsg_type
), h
->nlmsg_type
,
432 h
->nlmsg_seq
, h
->nlmsg_pid
);
434 /* skip unsolicited messages originating from command socket
435 * linux sets the originators port-id for {NEW|DEL}ADDR messages,
436 * so this has to be checked here. */
437 if (nl
!= &zns
->netlink_cmd
438 && h
->nlmsg_pid
== zns
->netlink_cmd
.snl
.nl_pid
439 && (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
))
441 if (IS_ZEBRA_DEBUG_KERNEL
)
442 zlog_debug ("netlink_parse_info: %s packet comes from %s",
443 zns
->netlink_cmd
.name
, nl
->name
);
447 error
= (*filter
) (&snl
, h
, zns
->ns_id
);
450 zlog (NULL
, LOG_ERR
, "%s filter function error", nl
->name
);
455 /* After error care. */
456 if (msg
.msg_flags
& MSG_TRUNC
)
458 zlog (NULL
, LOG_ERR
, "%s error: message truncated", nl
->name
);
463 zlog (NULL
, LOG_ERR
, "%s error: data remnant size %d", nl
->name
,
471 /* Utility function for parse rtattr. */
473 netlink_parse_rtattr (struct rtattr
**tb
, int max
, struct rtattr
*rta
,
476 while (RTA_OK (rta
, len
))
478 if (rta
->rta_type
<= max
)
479 tb
[rta
->rta_type
] = rta
;
480 rta
= RTA_NEXT (rta
, len
);
484 /* Utility function to parse hardware link-layer address and update ifp */
486 netlink_interface_update_hw_addr (struct rtattr
**tb
, struct interface
*ifp
)
490 if (tb
[IFLA_ADDRESS
])
494 hw_addr_len
= RTA_PAYLOAD (tb
[IFLA_ADDRESS
]);
496 if (hw_addr_len
> INTERFACE_HWADDR_MAX
)
497 zlog_warn ("Hardware address is too large: %d", hw_addr_len
);
500 ifp
->hw_addr_len
= hw_addr_len
;
501 memcpy (ifp
->hw_addr
, RTA_DATA (tb
[IFLA_ADDRESS
]), hw_addr_len
);
503 for (i
= 0; i
< hw_addr_len
; i
++)
504 if (ifp
->hw_addr
[i
] != 0)
507 if (i
== hw_addr_len
)
508 ifp
->hw_addr_len
= 0;
510 ifp
->hw_addr_len
= hw_addr_len
;
515 #define parse_rtattr_nested(tb, max, rta) \
516 netlink_parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))
519 netlink_vrf_change (struct nlmsghdr
*h
, struct rtattr
*tb
, const char *name
)
521 struct ifinfomsg
*ifi
;
522 struct rtattr
*linkinfo
[IFLA_INFO_MAX
+1];
523 struct rtattr
*attr
[IFLA_VRF_MAX
+1];
525 struct zebra_vrf
*zvrf
;
526 u_int32_t nl_table_id
;
528 ifi
= NLMSG_DATA (h
);
530 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
);
532 if (!linkinfo
[IFLA_INFO_DATA
]) {
533 if (IS_ZEBRA_DEBUG_KERNEL
)
534 zlog_debug ("%s: IFLA_INFO_DATA missing from VRF message: %s", __func__
, name
);
538 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, linkinfo
[IFLA_INFO_DATA
]);
539 if (!attr
[IFLA_VRF_TABLE
]) {
540 if (IS_ZEBRA_DEBUG_KERNEL
)
541 zlog_debug ("%s: IFLA_VRF_TABLE missing from VRF message: %s", __func__
, name
);
545 nl_table_id
= *(u_int32_t
*)RTA_DATA(attr
[IFLA_VRF_TABLE
]);
547 if (h
->nlmsg_type
== RTM_NEWLINK
)
549 /* If VRF already exists, we just return; status changes are handled
550 * against the VRF "interface".
552 vrf
= vrf_lookup ((vrf_id_t
)ifi
->ifi_index
);
553 if (vrf
&& vrf
->info
)
556 if (IS_ZEBRA_DEBUG_KERNEL
)
557 zlog_debug ("RTM_NEWLINK for VRF %s(%u) table %u",
558 name
, ifi
->ifi_index
, nl_table_id
);
561 * vrf_get is implied creation if it does not exist
563 vrf
= vrf_get((vrf_id_t
)ifi
->ifi_index
, name
); // It would create vrf
566 zlog_err ("VRF %s id %u not created", name
, ifi
->ifi_index
);
570 /* Enable the created VRF. */
571 if (!vrf_enable (vrf
))
573 zlog_err ("Failed to enable VRF %s id %u", name
, ifi
->ifi_index
);
578 * This is the only place that we get the actual kernel table_id
579 * being used. We need it to set the table_id of the routes
580 * we are passing to the kernel.... And to throw some totally
581 * awesome parties. that too.
583 zvrf
= (struct zebra_vrf
*)vrf
->info
;
584 zvrf
->table_id
= nl_table_id
;
586 else //h->nlmsg_type == RTM_DELLINK
588 if (IS_ZEBRA_DEBUG_KERNEL
)
589 zlog_debug ("RTM_DELLINK for VRF %s(%u)", name
, ifi
->ifi_index
);
591 vrf
= vrf_lookup ((vrf_id_t
)ifi
->ifi_index
);
595 zlog_warn ("%s: vrf not found", __func__
);
603 /* Called from interface_lookup_netlink(). This function is only used
606 netlink_interface (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
610 struct ifinfomsg
*ifi
;
611 struct rtattr
*tb
[IFLA_MAX
+ 1];
612 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
613 struct interface
*ifp
;
616 char *slave_kind
= NULL
;
619 ifi
= NLMSG_DATA (h
);
621 if (h
->nlmsg_type
!= RTM_NEWLINK
)
624 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifinfomsg
));
628 if (ifi
->ifi_family
== AF_BRIDGE
)
631 /* Looking up interface name. */
632 memset (tb
, 0, sizeof tb
);
633 netlink_parse_rtattr (tb
, IFLA_MAX
, IFLA_RTA (ifi
), len
);
636 /* check for wireless messages to ignore */
637 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0))
639 if (IS_ZEBRA_DEBUG_KERNEL
)
640 zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__
);
643 #endif /* IFLA_WIRELESS */
645 if (tb
[IFLA_IFNAME
] == NULL
)
647 name
= (char *) RTA_DATA (tb
[IFLA_IFNAME
]);
649 if (tb
[IFLA_LINKINFO
])
651 memset (linkinfo
, 0, sizeof linkinfo
);
652 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
654 if (linkinfo
[IFLA_INFO_KIND
])
655 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
657 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
658 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
659 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
662 if (kind
&& strcmp(kind
, "vrf") == 0)
665 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
666 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
672 if ((kind
&& strcmp(kind
, "vrf") == 0) ||
673 (slave_kind
&& strcmp(slave_kind
, "vrf") == 0))
674 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
676 vrf_id
= VRF_DEFAULT
;
680 ifp
= if_get_by_name_vrf (name
, vrf_id
);
681 set_ifindex(ifp
, ifi
->ifi_index
);
682 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
684 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
685 ifp
->mtu6
= ifp
->mtu
= *(uint32_t *) RTA_DATA (tb
[IFLA_MTU
]);
688 /* Hardware type and address. */
689 ifp
->hw_type
= ifi
->ifi_type
;
690 netlink_interface_update_hw_addr (tb
, ifp
);
697 /* Lookup interface IPv4/IPv6 address. */
699 netlink_interface_addr (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
703 struct ifaddrmsg
*ifa
;
704 struct rtattr
*tb
[IFA_MAX
+ 1];
705 struct interface
*ifp
;
711 vrf_id_t vrf_id
= ns_id
;
713 ifa
= NLMSG_DATA (h
);
715 if (ifa
->ifa_family
!= AF_INET
717 && ifa
->ifa_family
!= AF_INET6
718 #endif /* HAVE_IPV6 */
722 if (h
->nlmsg_type
!= RTM_NEWADDR
&& h
->nlmsg_type
!= RTM_DELADDR
)
725 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifaddrmsg
));
729 memset (tb
, 0, sizeof tb
);
730 netlink_parse_rtattr (tb
, IFA_MAX
, IFA_RTA (ifa
), len
);
732 ifp
= if_lookup_by_index_per_ns (zebra_ns_lookup (ns_id
), ifa
->ifa_index
);
735 zlog_err ("netlink_interface_addr can't find interface by index %d vrf %u",
736 ifa
->ifa_index
, vrf_id
);
740 if (IS_ZEBRA_DEBUG_KERNEL
) /* remove this line to see initial ifcfg */
743 zlog_debug ("netlink_interface_addr %s %s vrf %u flags 0x%x:",
744 lookup (nlmsg_str
, h
->nlmsg_type
), ifp
->name
,
745 vrf_id
, ifa
->ifa_flags
);
747 zlog_debug (" IFA_LOCAL %s/%d",
748 inet_ntop (ifa
->ifa_family
, RTA_DATA (tb
[IFA_LOCAL
]),
749 buf
, BUFSIZ
), ifa
->ifa_prefixlen
);
751 zlog_debug (" IFA_ADDRESS %s/%d",
752 inet_ntop (ifa
->ifa_family
, RTA_DATA (tb
[IFA_ADDRESS
]),
753 buf
, BUFSIZ
), ifa
->ifa_prefixlen
);
754 if (tb
[IFA_BROADCAST
])
755 zlog_debug (" IFA_BROADCAST %s/%d",
756 inet_ntop (ifa
->ifa_family
, RTA_DATA (tb
[IFA_BROADCAST
]),
757 buf
, BUFSIZ
), ifa
->ifa_prefixlen
);
758 if (tb
[IFA_LABEL
] && strcmp (ifp
->name
, RTA_DATA (tb
[IFA_LABEL
])))
759 zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb
[IFA_LABEL
]));
761 if (tb
[IFA_CACHEINFO
])
763 struct ifa_cacheinfo
*ci
= RTA_DATA (tb
[IFA_CACHEINFO
]);
764 zlog_debug (" IFA_CACHEINFO pref %d, valid %d",
765 ci
->ifa_prefered
, ci
->ifa_valid
);
769 /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */
770 if (tb
[IFA_LOCAL
] == NULL
)
771 tb
[IFA_LOCAL
] = tb
[IFA_ADDRESS
];
772 if (tb
[IFA_ADDRESS
] == NULL
)
773 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
775 /* local interface address */
776 addr
= (tb
[IFA_LOCAL
] ? RTA_DATA(tb
[IFA_LOCAL
]) : NULL
);
778 /* is there a peer address? */
779 if (tb
[IFA_ADDRESS
] &&
780 memcmp(RTA_DATA(tb
[IFA_ADDRESS
]), RTA_DATA(tb
[IFA_LOCAL
]), RTA_PAYLOAD(tb
[IFA_ADDRESS
])))
782 broad
= RTA_DATA(tb
[IFA_ADDRESS
]);
783 SET_FLAG (flags
, ZEBRA_IFA_PEER
);
786 /* seeking a broadcast address */
787 broad
= (tb
[IFA_BROADCAST
] ? RTA_DATA(tb
[IFA_BROADCAST
]) : NULL
);
789 /* addr is primary key, SOL if we don't have one */
792 zlog_debug ("%s: NULL address", __func__
);
797 if (ifa
->ifa_flags
& IFA_F_SECONDARY
)
798 SET_FLAG (flags
, ZEBRA_IFA_SECONDARY
);
802 label
= (char *) RTA_DATA (tb
[IFA_LABEL
]);
804 if (ifp
&& label
&& strcmp (ifp
->name
, label
) == 0)
807 /* Register interface address to the interface. */
808 if (ifa
->ifa_family
== AF_INET
)
810 if (h
->nlmsg_type
== RTM_NEWADDR
)
811 connected_add_ipv4 (ifp
, flags
,
812 (struct in_addr
*) addr
, ifa
->ifa_prefixlen
,
813 (struct in_addr
*) broad
, label
);
815 connected_delete_ipv4 (ifp
, flags
,
816 (struct in_addr
*) addr
, ifa
->ifa_prefixlen
,
817 (struct in_addr
*) broad
);
820 if (ifa
->ifa_family
== AF_INET6
)
822 if (h
->nlmsg_type
== RTM_NEWADDR
)
824 /* Only consider valid addresses; we'll not get a notification from
825 * the kernel till IPv6 DAD has completed, but at init time, Quagga
826 * does query for and will receive all addresses.
828 if (!(ifa
->ifa_flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
)))
829 connected_add_ipv6 (ifp
, flags
, (struct in6_addr
*) addr
,
830 ifa
->ifa_prefixlen
, (struct in6_addr
*) broad
, label
);
833 connected_delete_ipv6 (ifp
,
834 (struct in6_addr
*) addr
, ifa
->ifa_prefixlen
,
835 (struct in6_addr
*) broad
);
837 #endif /* HAVE_IPV6 */
842 /* Looking up routing table by netlink interface. */
844 netlink_routing_table (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
849 struct rtattr
*tb
[RTA_MAX
+ 1];
852 char anyaddr
[16] = { 0 };
862 rtm
= NLMSG_DATA (h
);
864 if (h
->nlmsg_type
!= RTM_NEWROUTE
)
866 if (rtm
->rtm_type
!= RTN_UNICAST
)
869 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct rtmsg
));
873 memset (tb
, 0, sizeof tb
);
874 netlink_parse_rtattr (tb
, RTA_MAX
, RTM_RTA (rtm
), len
);
876 if (rtm
->rtm_flags
& RTM_F_CLONED
)
878 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
880 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
883 if (rtm
->rtm_src_len
!= 0)
886 /* Table corresponding to route. */
888 table
= *(int *) RTA_DATA (tb
[RTA_TABLE
]);
890 table
= rtm
->rtm_table
;
893 vrf_id
= vrf_lookup_by_table(table
);
894 if (vrf_id
== VRF_DEFAULT
)
896 if (!is_zebra_valid_kernel_table(table
) &&
897 !is_zebra_main_routing_table(table
))
901 /* Route which inserted by Zebra. */
902 if (rtm
->rtm_protocol
== RTPROT_ZEBRA
)
903 flags
|= ZEBRA_FLAG_SELFROUTE
;
912 index
= *(int *) RTA_DATA (tb
[RTA_OIF
]);
915 dest
= RTA_DATA (tb
[RTA_DST
]);
920 src
= RTA_DATA (tb
[RTA_PREFSRC
]);
923 gate
= RTA_DATA (tb
[RTA_GATEWAY
]);
925 if (tb
[RTA_PRIORITY
])
926 metric
= *(int *) RTA_DATA(tb
[RTA_PRIORITY
]);
928 if (rtm
->rtm_family
== AF_INET
)
930 struct prefix_ipv4 p
;
932 memcpy (&p
.prefix
, dest
, 4);
933 p
.prefixlen
= rtm
->rtm_dst_len
;
935 if (!tb
[RTA_MULTIPATH
])
936 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, 0, flags
, &p
, gate
, src
, index
,
937 vrf_id
, table
, metric
, 0, SAFI_UNICAST
);
940 /* This is a multipath route */
943 struct rtnexthop
*rtnh
=
944 (struct rtnexthop
*) RTA_DATA (tb
[RTA_MULTIPATH
]);
946 len
= RTA_PAYLOAD (tb
[RTA_MULTIPATH
]);
948 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
949 rib
->type
= ZEBRA_ROUTE_KERNEL
;
952 rib
->metric
= metric
;
953 rib
->vrf_id
= vrf_id
;
955 rib
->nexthop_num
= 0;
956 rib
->uptime
= time (NULL
);
960 if (len
< (int) sizeof (*rtnh
) || rtnh
->rtnh_len
> len
)
963 index
= rtnh
->rtnh_ifindex
;
965 if (rtnh
->rtnh_len
> sizeof (*rtnh
))
967 memset (tb
, 0, sizeof (tb
));
968 netlink_parse_rtattr (tb
, RTA_MAX
, RTNH_DATA (rtnh
),
969 rtnh
->rtnh_len
- sizeof (*rtnh
));
971 gate
= RTA_DATA (tb
[RTA_GATEWAY
]);
977 rib_nexthop_ipv4_ifindex_add (rib
, gate
, src
, index
);
979 rib_nexthop_ipv4_add (rib
, gate
, src
);
982 rib_nexthop_ifindex_add (rib
, index
);
984 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
985 rtnh
= RTNH_NEXT(rtnh
);
988 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
990 if (rib
->nexthop_num
== 0)
991 XFREE (MTYPE_RIB
, rib
);
993 rib_add_ipv4_multipath (&p
, rib
, SAFI_UNICAST
);
997 if (rtm
->rtm_family
== AF_INET6
)
999 struct prefix_ipv6 p
;
1000 p
.family
= AF_INET6
;
1001 memcpy (&p
.prefix
, dest
, 16);
1002 p
.prefixlen
= rtm
->rtm_dst_len
;
1004 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL
, 0, flags
, &p
, gate
, index
, vrf_id
,
1005 table
, metric
, 0, SAFI_UNICAST
);
1007 #endif /* HAVE_IPV6 */
1012 static const struct message rtproto_str
[] = {
1013 {RTPROT_REDIRECT
, "redirect"},
1014 {RTPROT_KERNEL
, "kernel"},
1015 {RTPROT_BOOT
, "boot"},
1016 {RTPROT_STATIC
, "static"},
1017 {RTPROT_GATED
, "GateD"},
1018 {RTPROT_RA
, "router advertisement"},
1019 {RTPROT_MRT
, "MRT"},
1020 {RTPROT_ZEBRA
, "Zebra"},
1022 {RTPROT_BIRD
, "BIRD"},
1023 #endif /* RTPROT_BIRD */
1027 /* Routing information change from the kernel. */
1029 netlink_route_change (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1034 struct rtattr
*tb
[RTA_MAX
+ 1];
1035 u_char zebra_flags
= 0;
1037 char anyaddr
[16] = { 0 };
1047 vrf_id_t vrf_id
= ns_id
;
1049 rtm
= NLMSG_DATA (h
);
1051 if (!(h
->nlmsg_type
== RTM_NEWROUTE
|| h
->nlmsg_type
== RTM_DELROUTE
))
1053 /* If this is not route add/delete message print warning. */
1054 zlog_warn ("Kernel message: %d vrf %u\n", h
->nlmsg_type
, vrf_id
);
1058 /* Connected route. */
1059 if (IS_ZEBRA_DEBUG_KERNEL
)
1060 zlog_debug ("%s %s %s proto %s vrf %u",
1062 RTM_NEWROUTE
? "RTM_NEWROUTE" : "RTM_DELROUTE",
1063 rtm
->rtm_family
== AF_INET
? "ipv4" : "ipv6",
1064 rtm
->rtm_type
== RTN_UNICAST
? "unicast" : "multicast",
1065 lookup (rtproto_str
, rtm
->rtm_protocol
),
1068 if (rtm
->rtm_type
!= RTN_UNICAST
)
1073 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct rtmsg
));
1077 memset (tb
, 0, sizeof tb
);
1078 netlink_parse_rtattr (tb
, RTA_MAX
, RTM_RTA (rtm
), len
);
1080 if (rtm
->rtm_flags
& RTM_F_CLONED
)
1082 if (rtm
->rtm_protocol
== RTPROT_REDIRECT
)
1084 if (rtm
->rtm_protocol
== RTPROT_KERNEL
)
1087 if (rtm
->rtm_protocol
== RTPROT_ZEBRA
&& h
->nlmsg_type
== RTM_NEWROUTE
)
1089 if (rtm
->rtm_protocol
== RTPROT_ZEBRA
)
1090 SET_FLAG(zebra_flags
, ZEBRA_FLAG_SELFROUTE
);
1092 if (rtm
->rtm_src_len
!= 0)
1094 zlog_warn ("netlink_route_change(): no src len, vrf %u", vrf_id
);
1098 /* Table corresponding to route. */
1100 table
= *(int *) RTA_DATA (tb
[RTA_TABLE
]);
1102 table
= rtm
->rtm_table
;
1105 vrf_id
= vrf_lookup_by_table(table
);
1106 if (vrf_id
== VRF_DEFAULT
)
1108 if (!is_zebra_valid_kernel_table(table
) &&
1109 !is_zebra_main_routing_table(table
))
1120 index
= *(int *) RTA_DATA (tb
[RTA_OIF
]);
1123 dest
= RTA_DATA (tb
[RTA_DST
]);
1127 if (tb
[RTA_GATEWAY
])
1128 gate
= RTA_DATA (tb
[RTA_GATEWAY
]);
1130 if (tb
[RTA_PREFSRC
])
1131 src
= RTA_DATA (tb
[RTA_PREFSRC
]);
1133 if (h
->nlmsg_type
== RTM_NEWROUTE
&& tb
[RTA_PRIORITY
])
1134 metric
= *(int *) RTA_DATA(tb
[RTA_PRIORITY
]);
1136 if (rtm
->rtm_family
== AF_INET
)
1138 struct prefix_ipv4 p
;
1140 memcpy (&p
.prefix
, dest
, 4);
1141 p
.prefixlen
= rtm
->rtm_dst_len
;
1143 if (IS_ZEBRA_DEBUG_KERNEL
)
1145 char buf
[PREFIX2STR_BUFFER
];
1146 zlog_debug ("%s %s vrf %u",
1147 h
->nlmsg_type
== RTM_NEWROUTE
? "RTM_NEWROUTE" : "RTM_DELROUTE",
1148 prefix2str (&p
, buf
, sizeof(buf
)), vrf_id
);
1151 if (h
->nlmsg_type
== RTM_NEWROUTE
)
1153 if (!tb
[RTA_MULTIPATH
])
1154 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, 0, 0, &p
, gate
, src
, index
, vrf_id
,
1155 table
, metric
, 0, SAFI_UNICAST
);
1158 /* This is a multipath route */
1161 struct rtnexthop
*rtnh
=
1162 (struct rtnexthop
*) RTA_DATA (tb
[RTA_MULTIPATH
]);
1164 len
= RTA_PAYLOAD (tb
[RTA_MULTIPATH
]);
1166 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1167 rib
->type
= ZEBRA_ROUTE_KERNEL
;
1170 rib
->metric
= metric
;
1171 rib
->vrf_id
= vrf_id
;
1173 rib
->nexthop_num
= 0;
1174 rib
->uptime
= time (NULL
);
1178 if (len
< (int) sizeof (*rtnh
) || rtnh
->rtnh_len
> len
)
1181 index
= rtnh
->rtnh_ifindex
;
1183 if (rtnh
->rtnh_len
> sizeof (*rtnh
))
1185 memset (tb
, 0, sizeof (tb
));
1186 netlink_parse_rtattr (tb
, RTA_MAX
, RTNH_DATA (rtnh
),
1187 rtnh
->rtnh_len
- sizeof (*rtnh
));
1188 if (tb
[RTA_GATEWAY
])
1189 gate
= RTA_DATA (tb
[RTA_GATEWAY
]);
1195 rib_nexthop_ipv4_ifindex_add (rib
, gate
, src
, index
);
1197 rib_nexthop_ipv4_add (rib
, gate
, src
);
1200 rib_nexthop_ifindex_add (rib
, index
);
1202 len
-= NLMSG_ALIGN(rtnh
->rtnh_len
);
1203 rtnh
= RTNH_NEXT(rtnh
);
1206 zserv_nexthop_num_warn(__func__
, (const struct prefix
*)&p
,
1209 if (rib
->nexthop_num
== 0)
1210 XFREE (MTYPE_RIB
, rib
);
1212 rib_add_ipv4_multipath (&p
, rib
, SAFI_UNICAST
);
1216 rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL
, 0, zebra_flags
, &p
, gate
, index
,
1217 vrf_id
, table
, SAFI_UNICAST
);
1221 if (rtm
->rtm_family
== AF_INET6
)
1223 struct prefix_ipv6 p
;
1224 char buf
[PREFIX2STR_BUFFER
];
1226 p
.family
= AF_INET6
;
1227 memcpy (&p
.prefix
, dest
, 16);
1228 p
.prefixlen
= rtm
->rtm_dst_len
;
1230 if (IS_ZEBRA_DEBUG_KERNEL
)
1232 zlog_debug ("%s %s vrf %u",
1233 h
->nlmsg_type
== RTM_NEWROUTE
? "RTM_NEWROUTE" : "RTM_DELROUTE",
1234 prefix2str (&p
, buf
, sizeof(buf
)), vrf_id
);
1237 if (h
->nlmsg_type
== RTM_NEWROUTE
)
1238 rib_add_ipv6 (ZEBRA_ROUTE_KERNEL
, 0, 0, &p
, gate
, index
, vrf_id
,
1239 table
, metric
, 0, SAFI_UNICAST
);
1241 rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL
, 0, zebra_flags
, &p
, gate
, index
,
1242 vrf_id
, table
, SAFI_UNICAST
);
1244 #endif /* HAVE_IPV6 */
1250 netlink_link_change (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1254 struct ifinfomsg
*ifi
;
1255 struct rtattr
*tb
[IFLA_MAX
+ 1];
1256 struct rtattr
*linkinfo
[IFLA_MAX
+ 1];
1257 struct interface
*ifp
;
1260 char *slave_kind
= NULL
;
1263 vrf_id_t vrf_id
= ns_id
;
1265 ifi
= NLMSG_DATA (h
);
1267 if (!(h
->nlmsg_type
== RTM_NEWLINK
|| h
->nlmsg_type
== RTM_DELLINK
))
1269 /* If this is not link add/delete message so print warning. */
1270 zlog_warn ("netlink_link_change: wrong kernel message %d vrf %u\n",
1271 h
->nlmsg_type
, vrf_id
);
1275 len
= h
->nlmsg_len
- NLMSG_LENGTH (sizeof (struct ifinfomsg
));
1279 if (ifi
->ifi_family
== AF_BRIDGE
)
1282 /* Looking up interface name. */
1283 memset (tb
, 0, sizeof tb
);
1284 netlink_parse_rtattr (tb
, IFLA_MAX
, IFLA_RTA (ifi
), len
);
1286 #ifdef IFLA_WIRELESS
1287 /* check for wireless messages to ignore */
1288 if ((tb
[IFLA_WIRELESS
] != NULL
) && (ifi
->ifi_change
== 0))
1290 if (IS_ZEBRA_DEBUG_KERNEL
)
1291 zlog_debug ("%s: ignoring IFLA_WIRELESS message, vrf %u", __func__
,
1295 #endif /* IFLA_WIRELESS */
1297 if (tb
[IFLA_IFNAME
] == NULL
)
1299 name
= (char *) RTA_DATA (tb
[IFLA_IFNAME
]);
1301 if (tb
[IFLA_LINKINFO
])
1303 memset (linkinfo
, 0, sizeof linkinfo
);
1304 parse_rtattr_nested(linkinfo
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
1306 if (linkinfo
[IFLA_INFO_KIND
])
1307 kind
= RTA_DATA(linkinfo
[IFLA_INFO_KIND
]);
1309 #if HAVE_DECL_IFLA_INFO_SLAVE_KIND
1310 if (linkinfo
[IFLA_INFO_SLAVE_KIND
])
1311 slave_kind
= RTA_DATA(linkinfo
[IFLA_INFO_SLAVE_KIND
]);
1314 if (kind
&& strcmp(kind
, "vrf") == 0)
1317 netlink_vrf_change(h
, tb
[IFLA_LINKINFO
], name
);
1318 vrf_id
= (vrf_id_t
)ifi
->ifi_index
;
1322 /* See if interface is present. */
1323 ifp
= if_lookup_by_index_per_ns (zebra_ns_lookup (NS_DEFAULT
), ifi
->ifi_index
);
1325 if (h
->nlmsg_type
== RTM_NEWLINK
)
1327 if (tb
[IFLA_MASTER
])
1329 if ((kind
&& strcmp(kind
, "vrf") == 0) ||
1330 (slave_kind
&& strcmp(slave_kind
, "vrf") == 0))
1331 vrf_id
= *(u_int32_t
*)RTA_DATA(tb
[IFLA_MASTER
]);
1333 vrf_id
= VRF_DEFAULT
;
1336 if (ifp
== NULL
|| !CHECK_FLAG (ifp
->status
, ZEBRA_INTERFACE_ACTIVE
))
1338 /* Add interface notification from kernel */
1339 if (IS_ZEBRA_DEBUG_KERNEL
)
1340 zlog_debug ("RTM_NEWLINK for %s(%u) (ifp %p) vrf_id %u flags 0x%x",
1341 name
, ifi
->ifi_index
, ifp
, vrf_id
, ifi
->ifi_flags
);
1345 /* unknown interface */
1346 ifp
= if_get_by_name_vrf (name
, vrf_id
);
1350 /* pre-configured interface, learnt now */
1351 if (ifp
->vrf_id
!= vrf_id
)
1352 if_update_vrf (ifp
, name
, strlen(name
), vrf_id
);
1354 /* Start IPv6 RA, if any IPv6 addresses on interface. */
1355 if (interface_ipv6_auto_ra_allowed (ifp
))
1357 if (ipv6_address_configured (ifp
))
1358 ipv6_nd_suppress_ra_set (ifp
, RA_ENABLE
);
1362 /* Update interface information. */
1363 set_ifindex(ifp
, ifi
->ifi_index
);
1364 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1366 SET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1367 ifp
->mtu6
= ifp
->mtu
= *(int *) RTA_DATA (tb
[IFLA_MTU
]);
1370 netlink_interface_update_hw_addr (tb
, ifp
);
1372 /* Inform clients, install any configured addresses. */
1373 if_add_update (ifp
);
1375 else if (ifp
->vrf_id
!= vrf_id
)
1377 /* VRF change for an interface. */
1378 if (IS_ZEBRA_DEBUG_KERNEL
)
1379 zlog_debug ("RTM_NEWLINK vrf-change for %s(%u) "
1380 "vrf_id %u -> %u flags 0x%x",
1381 name
, ifp
->ifindex
, ifp
->vrf_id
,
1382 vrf_id
, ifi
->ifi_flags
);
1384 if_handle_vrf_change (ifp
, vrf_id
);
1388 /* Interface status change. */
1389 if (IS_ZEBRA_DEBUG_KERNEL
)
1390 zlog_debug ("RTM_NEWLINK status for %s(%u) flags 0x%x",
1391 name
, ifp
->ifindex
, ifi
->ifi_flags
);
1393 set_ifindex(ifp
, ifi
->ifi_index
);
1394 ifp
->mtu6
= ifp
->mtu
= *(int *) RTA_DATA (tb
[IFLA_MTU
]);
1397 netlink_interface_update_hw_addr (tb
, ifp
);
1399 if (if_is_no_ptm_operative (ifp
))
1401 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1402 if (!if_is_no_ptm_operative (ifp
))
1404 else if (if_is_operative (ifp
))
1405 /* Must notify client daemons of new interface status. */
1406 zebra_interface_up_update (ifp
);
1410 ifp
->flags
= ifi
->ifi_flags
& 0x0000fffff;
1411 if (if_is_operative (ifp
))
1418 /* Delete interface notification from kernel */
1421 zlog_warn ("RTM_DELLINK for unknown interface %s(%u)",
1422 name
, ifi
->ifi_index
);
1426 if (IS_ZEBRA_DEBUG_KERNEL
)
1427 zlog_debug ("RTM_DELLINK for %s(%u)", name
, ifp
->ifindex
);
1429 UNSET_FLAG(ifp
->status
, ZEBRA_INTERFACE_VRF_LOOPBACK
);
1430 if_delete_update (ifp
);
1437 netlink_information_fetch (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1440 /* JF: Ignore messages that aren't from the kernel */
1441 if ( snl
->nl_pid
!= 0 )
1443 zlog ( NULL
, LOG_ERR
, "Ignoring message from pid %u", snl
->nl_pid
);
1447 switch (h
->nlmsg_type
)
1450 return netlink_route_change (snl
, h
, ns_id
);
1453 return netlink_route_change (snl
, h
, ns_id
);
1456 return netlink_link_change (snl
, h
, ns_id
);
1459 return netlink_link_change (snl
, h
, ns_id
);
1462 return netlink_interface_addr (snl
, h
, ns_id
);
1465 return netlink_interface_addr (snl
, h
, ns_id
);
1468 zlog_warn ("Unknown netlink nlmsg_type %d vrf %u\n", h
->nlmsg_type
,
1475 /* Interface lookup by netlink socket. */
1477 interface_lookup_netlink (struct zebra_ns
*zns
)
1481 /* Get interface information. */
1482 ret
= netlink_request (AF_PACKET
, RTM_GETLINK
, &zns
->netlink_cmd
);
1485 ret
= netlink_parse_info (netlink_interface
, &zns
->netlink_cmd
, zns
, 0);
1489 /* Get IPv4 address of the interfaces. */
1490 ret
= netlink_request (AF_INET
, RTM_GETADDR
, &zns
->netlink_cmd
);
1493 ret
= netlink_parse_info (netlink_interface_addr
, &zns
->netlink_cmd
, zns
, 0);
1498 /* Get IPv6 address of the interfaces. */
1499 ret
= netlink_request (AF_INET6
, RTM_GETADDR
, &zns
->netlink_cmd
);
1502 ret
= netlink_parse_info (netlink_interface_addr
, &zns
->netlink_cmd
, zns
, 0);
1505 #endif /* HAVE_IPV6 */
1510 /* Routing table read function using netlink interface. Only called
1513 netlink_route_read (struct zebra_ns
*zns
)
1517 /* Get IPv4 routing table. */
1518 ret
= netlink_request (AF_INET
, RTM_GETROUTE
, &zns
->netlink_cmd
);
1521 ret
= netlink_parse_info (netlink_routing_table
, &zns
->netlink_cmd
, zns
, 0);
1526 /* Get IPv6 routing table. */
1527 ret
= netlink_request (AF_INET6
, RTM_GETROUTE
, &zns
->netlink_cmd
);
1530 ret
= netlink_parse_info (netlink_routing_table
, &zns
->netlink_cmd
, zns
, 0);
1533 #endif /* HAVE_IPV6 */
1538 /* Utility function comes from iproute2.
1539 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1541 addattr_l (struct nlmsghdr
*n
, unsigned int maxlen
, int type
, void *data
, int alen
)
1546 len
= RTA_LENGTH (alen
);
1548 if (NLMSG_ALIGN (n
->nlmsg_len
) + len
> maxlen
)
1551 rta
= (struct rtattr
*) (((char *) n
) + NLMSG_ALIGN (n
->nlmsg_len
));
1552 rta
->rta_type
= type
;
1554 memcpy (RTA_DATA (rta
), data
, alen
);
1555 n
->nlmsg_len
= NLMSG_ALIGN (n
->nlmsg_len
) + len
;
1561 rta_addattr_l (struct rtattr
*rta
, int maxlen
, int type
, void *data
, int alen
)
1564 struct rtattr
*subrta
;
1566 len
= RTA_LENGTH (alen
);
1568 if ((int)RTA_ALIGN (rta
->rta_len
) + len
> maxlen
)
1571 subrta
= (struct rtattr
*) (((char *) rta
) + RTA_ALIGN (rta
->rta_len
));
1572 subrta
->rta_type
= type
;
1573 subrta
->rta_len
= len
;
1574 memcpy (RTA_DATA (subrta
), data
, alen
);
1575 rta
->rta_len
= NLMSG_ALIGN (rta
->rta_len
) + len
;
1580 /* Utility function comes from iproute2.
1581 Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */
1583 addattr32 (struct nlmsghdr
*n
, unsigned int maxlen
, int type
, int data
)
1588 len
= RTA_LENGTH (4);
1590 if (NLMSG_ALIGN (n
->nlmsg_len
) + len
> maxlen
)
1593 rta
= (struct rtattr
*) (((char *) n
) + NLMSG_ALIGN (n
->nlmsg_len
));
1594 rta
->rta_type
= type
;
1596 memcpy (RTA_DATA (rta
), &data
, 4);
1597 n
->nlmsg_len
= NLMSG_ALIGN (n
->nlmsg_len
) + len
;
1603 netlink_talk_filter (struct sockaddr_nl
*snl
, struct nlmsghdr
*h
,
1606 zlog_warn ("netlink_talk: ignoring message type 0x%04x NS %u", h
->nlmsg_type
,
1611 /* sendmsg() to netlink socket then recvmsg(). */
1613 netlink_talk (struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
)
1616 struct sockaddr_nl snl
;
1617 struct iovec iov
= {
1618 .iov_base
= (void *) n
,
1619 .iov_len
= n
->nlmsg_len
1621 struct msghdr msg
= {
1622 .msg_name
= (void *) &snl
,
1623 .msg_namelen
= sizeof snl
,
1629 memset (&snl
, 0, sizeof snl
);
1630 snl
.nl_family
= AF_NETLINK
;
1632 n
->nlmsg_seq
= ++nl
->seq
;
1634 /* Request an acknowledgement by setting NLM_F_ACK */
1635 n
->nlmsg_flags
|= NLM_F_ACK
;
1637 if (IS_ZEBRA_DEBUG_KERNEL
)
1638 zlog_debug ("netlink_talk: %s type %s(%u), seq=%u flags 0x%x",
1640 lookup (nlmsg_str
, n
->nlmsg_type
), n
->nlmsg_type
,
1641 n
->nlmsg_seq
, n
->nlmsg_flags
);
1643 /* Send message to netlink interface. */
1644 if (zserv_privs
.change (ZPRIVS_RAISE
))
1645 zlog (NULL
, LOG_ERR
, "Can't raise privileges");
1646 status
= sendmsg (nl
->sock
, &msg
, 0);
1648 if (zserv_privs
.change (ZPRIVS_LOWER
))
1649 zlog (NULL
, LOG_ERR
, "Can't lower privileges");
1653 zlog (NULL
, LOG_ERR
, "netlink_talk sendmsg() error: %s",
1654 safe_strerror (save_errno
));
1660 * Get reply from netlink socket.
1661 * The reply should either be an acknowlegement or an error.
1663 return netlink_parse_info (netlink_talk_filter
, nl
, zns
, 0);
1666 /* Routing table change via netlink interface. */
1668 netlink_route (int cmd
, int family
, void *dest
, int length
, void *gate
,
1669 int index
, int zebra_flags
, int table
)
1673 struct sockaddr_nl snl
;
1676 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
1682 char buf
[NL_PKT_BUF_SIZE
];
1685 memset (&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
1687 bytelen
= (family
== AF_INET
? 4 : 16);
1689 req
.n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct rtmsg
));
1690 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
1691 req
.n
.nlmsg_type
= cmd
;
1692 req
.r
.rtm_family
= family
;
1693 req
.r
.rtm_dst_len
= length
;
1694 req
.r
.rtm_protocol
= RTPROT_ZEBRA
;
1695 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
1697 if ((zebra_flags
& ZEBRA_FLAG_BLACKHOLE
)
1698 || (zebra_flags
& ZEBRA_FLAG_REJECT
))
1703 if (cmd
== RTM_NEWROUTE
)
1707 if (zebra_flags
& ZEBRA_FLAG_BLACKHOLE
)
1708 req
.r
.rtm_type
= RTN_BLACKHOLE
;
1709 else if (zebra_flags
& ZEBRA_FLAG_REJECT
)
1710 req
.r
.rtm_type
= RTN_UNREACHABLE
;
1712 assert (RTN_BLACKHOLE
!= RTN_UNREACHABLE
); /* false */
1714 if (IS_ZEBRA_DEBUG_KERNEL
)
1715 zlog_debug ("%s: Adding discard route for family %s\n",
1716 __FUNCTION__
, family
== AF_INET
? "IPv4" : "IPv6");
1719 req
.r
.rtm_type
= RTN_UNICAST
;
1723 addattr_l (&req
.n
, sizeof req
, RTA_DST
, dest
, bytelen
);
1725 /* Table corresponding to this route. */
1727 req
.r
.rtm_table
= table
;
1730 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
1731 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, table
);
1737 addattr_l (&req
.n
, sizeof req
, RTA_GATEWAY
, gate
, bytelen
);
1739 addattr32 (&req
.n
, sizeof req
, RTA_OIF
, index
);
1742 /* Destination netlink address. */
1743 memset (&snl
, 0, sizeof snl
);
1744 snl
.nl_family
= AF_NETLINK
;
1746 /* Talk to netlink socket. */
1747 ret
= netlink_talk (&req
.n
, &zns
->netlink_cmd
, NS_DEFAULT
);
1754 /* This function takes a nexthop as argument and adds
1755 * the appropriate netlink attributes to an existing
1758 * @param routedesc: Human readable description of route type
1759 * (direct/recursive, single-/multipath)
1760 * @param bytelen: Length of addresses in bytes.
1761 * @param nexthop: Nexthop information
1762 * @param nlmsg: nlmsghdr structure to fill in.
1763 * @param req_size: The size allocated for the message.
1766 _netlink_route_build_singlepath(
1767 const char *routedesc
,
1769 struct nexthop
*nexthop
,
1770 struct nlmsghdr
*nlmsg
,
1771 struct rtmsg
*rtmsg
,
1776 if (rtmsg
->rtm_family
== AF_INET
&&
1777 (nexthop
->type
== NEXTHOP_TYPE_IPV6
1778 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
1780 char buf
[16] = "169.254.0.1";
1781 struct in_addr ipv4_ll
;
1783 inet_pton (AF_INET
, buf
, &ipv4_ll
);
1784 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1785 addattr_l (nlmsg
, req_size
, RTA_GATEWAY
, &ipv4_ll
, 4);
1786 addattr32 (nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1788 if (nexthop
->rmap_src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1789 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1790 &nexthop
->rmap_src
.ipv4
, bytelen
);
1791 else if (nexthop
->src
.ipv4
.s_addr
&& (cmd
== RTM_NEWROUTE
))
1792 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1793 &nexthop
->src
.ipv4
, bytelen
);
1795 if (IS_ZEBRA_DEBUG_KERNEL
)
1796 zlog_debug(" 5549: _netlink_route_build_singlepath() (%s): "
1797 "nexthop via %s if %u",
1798 routedesc
, buf
, nexthop
->ifindex
);
1802 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1803 rtmsg
->rtm_flags
|= RTNH_F_ONLINK
;
1805 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1806 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1808 addattr_l (nlmsg
, req_size
, RTA_GATEWAY
,
1809 &nexthop
->gate
.ipv4
, bytelen
);
1811 if (cmd
== RTM_NEWROUTE
)
1813 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1814 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1815 &nexthop
->rmap_src
.ipv4
, bytelen
);
1816 else if (nexthop
->src
.ipv4
.s_addr
)
1817 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1818 &nexthop
->src
.ipv4
, bytelen
);
1821 if (IS_ZEBRA_DEBUG_KERNEL
)
1822 zlog_debug("netlink_route_multipath() (%s): "
1823 "nexthop via %s if %u",
1825 inet_ntoa (nexthop
->gate
.ipv4
),
1828 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1829 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1831 addattr_l (nlmsg
, req_size
, RTA_GATEWAY
,
1832 &nexthop
->gate
.ipv6
, bytelen
);
1834 if (cmd
== RTM_NEWROUTE
)
1836 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1837 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1838 &nexthop
->rmap_src
.ipv6
, bytelen
);
1839 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1840 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1841 &nexthop
->src
.ipv6
, bytelen
);
1844 if (IS_ZEBRA_DEBUG_KERNEL
)
1845 zlog_debug("netlink_route_multipath() (%s): "
1846 "nexthop via %s if %u",
1848 inet6_ntoa (nexthop
->gate
.ipv6
),
1851 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1852 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1854 addattr32 (nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1856 if (cmd
== RTM_NEWROUTE
)
1858 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1859 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1860 &nexthop
->rmap_src
.ipv4
, bytelen
);
1861 else if (nexthop
->src
.ipv4
.s_addr
)
1862 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1863 &nexthop
->src
.ipv4
, bytelen
);
1866 if (IS_ZEBRA_DEBUG_KERNEL
)
1867 zlog_debug("netlink_route_multipath() (%s): "
1868 "nexthop via if %u", routedesc
, nexthop
->ifindex
);
1871 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1873 addattr32 (nlmsg
, req_size
, RTA_OIF
, nexthop
->ifindex
);
1875 if (cmd
== RTM_NEWROUTE
)
1877 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1878 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1879 &nexthop
->rmap_src
.ipv6
, bytelen
);
1880 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1881 addattr_l (nlmsg
, req_size
, RTA_PREFSRC
,
1882 &nexthop
->src
.ipv6
, bytelen
);
1885 if (IS_ZEBRA_DEBUG_KERNEL
)
1886 zlog_debug("netlink_route_multipath() (%s): "
1887 "nexthop via if %u", routedesc
, nexthop
->ifindex
);
1891 /* This function takes a nexthop as argument and
1892 * appends to the given rtattr/rtnexthop pair the
1893 * representation of the nexthop. If the nexthop
1894 * defines a preferred source, the src parameter
1895 * will be modified to point to that src, otherwise
1896 * it will be kept unmodified.
1898 * @param routedesc: Human readable description of route type
1899 * (direct/recursive, single-/multipath)
1900 * @param bytelen: Length of addresses in bytes.
1901 * @param nexthop: Nexthop information
1902 * @param rta: rtnetlink attribute structure
1903 * @param rtnh: pointer to an rtnetlink nexthop structure
1904 * @param src: pointer pointing to a location where
1905 * the prefsrc should be stored.
1908 _netlink_route_build_multipath(
1909 const char *routedesc
,
1911 struct nexthop
*nexthop
,
1913 struct rtnexthop
*rtnh
,
1914 struct rtmsg
*rtmsg
,
1917 rtnh
->rtnh_len
= sizeof (*rtnh
);
1918 rtnh
->rtnh_flags
= 0;
1919 rtnh
->rtnh_hops
= 0;
1920 rta
->rta_len
+= rtnh
->rtnh_len
;
1922 if (rtmsg
->rtm_family
== AF_INET
&&
1923 (nexthop
->type
== NEXTHOP_TYPE_IPV6
1924 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
))
1926 char buf
[16] = "169.254.0.1";
1927 struct in_addr ipv4_ll
;
1929 inet_pton (AF_INET
, buf
, &ipv4_ll
);
1931 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1932 rta_addattr_l (rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
,
1934 rtnh
->rtnh_len
+= sizeof (struct rtattr
) + bytelen
;
1935 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1937 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1938 *src
= &nexthop
->rmap_src
;
1939 else if (nexthop
->src
.ipv4
.s_addr
)
1940 *src
= &nexthop
->src
;
1942 if (IS_ZEBRA_DEBUG_KERNEL
)
1943 zlog_debug(" 5549: netlink_route_build_multipath() (%s): "
1944 "nexthop via %s if %u",
1945 routedesc
, buf
, nexthop
->ifindex
);
1950 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
1951 rtnh
->rtnh_flags
|= RTNH_F_ONLINK
;
1953 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1954 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
1956 rta_addattr_l (rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
,
1957 &nexthop
->gate
.ipv4
, bytelen
);
1958 rtnh
->rtnh_len
+= sizeof (struct rtattr
) + 4;
1960 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1961 *src
= &nexthop
->rmap_src
;
1962 else if (nexthop
->src
.ipv4
.s_addr
)
1963 *src
= &nexthop
->src
;
1965 if (IS_ZEBRA_DEBUG_KERNEL
)
1966 zlog_debug("netlink_route_multipath() (%s): "
1967 "nexthop via %s if %u",
1969 inet_ntoa (nexthop
->gate
.ipv4
),
1972 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1973 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
1975 rta_addattr_l (rta
, NL_PKT_BUF_SIZE
, RTA_GATEWAY
,
1976 &nexthop
->gate
.ipv6
, bytelen
);
1977 rtnh
->rtnh_len
+= sizeof (struct rtattr
) + bytelen
;
1979 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
1980 *src
= &nexthop
->rmap_src
;
1981 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
1982 *src
= &nexthop
->src
;
1984 if (IS_ZEBRA_DEBUG_KERNEL
)
1985 zlog_debug("netlink_route_multipath() (%s): "
1986 "nexthop via %s if %u",
1988 inet6_ntoa (nexthop
->gate
.ipv6
),
1992 if (nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
1993 || nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
1995 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
1997 if (nexthop
->rmap_src
.ipv4
.s_addr
)
1998 *src
= &nexthop
->rmap_src
;
1999 else if (nexthop
->src
.ipv4
.s_addr
)
2000 *src
= &nexthop
->src
;
2002 if (IS_ZEBRA_DEBUG_KERNEL
)
2003 zlog_debug("netlink_route_multipath() (%s): "
2004 "nexthop via if %u", routedesc
, nexthop
->ifindex
);
2006 else if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
2008 rtnh
->rtnh_ifindex
= nexthop
->ifindex
;
2010 if (IS_ZEBRA_DEBUG_KERNEL
)
2011 zlog_debug("netlink_route_multipath() (%s): "
2012 "nexthop via if %u", routedesc
, nexthop
->ifindex
);
2016 rtnh
->rtnh_ifindex
= 0;
2020 /* Log debug information for netlink_route_multipath
2021 * if debug logging is enabled.
2023 * @param cmd: Netlink command which is to be processed
2024 * @param p: Prefix for which the change is due
2025 * @param nexthop: Nexthop which is currently processed
2026 * @param routedesc: Semantic annotation for nexthop
2027 * (recursive, multipath, etc.)
2028 * @param family: Address family which the change concerns
2031 _netlink_route_debug(
2034 struct nexthop
*nexthop
,
2035 const char *routedesc
,
2037 struct zebra_vrf
*zvrf
)
2039 if (IS_ZEBRA_DEBUG_KERNEL
)
2041 zlog_debug ("netlink_route_multipath() (%s): %s %s/%d vrf %u type %s",
2043 lookup (nlmsg_str
, cmd
),
2045 (family
== AF_INET
) ? inet_ntoa (p
->u
.prefix4
) :
2046 inet6_ntoa (p
->u
.prefix6
),
2048 inet_ntoa (p
->u
.prefix4
),
2049 #endif /* HAVE_IPV6 */
2050 p
->prefixlen
, zvrf
->vrf_id
, nexthop_type_to_str (nexthop
->type
));
2055 netlink_neigh_update (int cmd
, int ifindex
, __u32 addr
, char *lla
, int llalen
)
2063 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
2065 memset(&req
.n
, 0, sizeof(req
.n
));
2066 memset(&req
.ndm
, 0, sizeof(req
.ndm
));
2068 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
2069 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2070 req
.n
.nlmsg_type
= cmd
; //RTM_NEWNEIGH or RTM_DELNEIGH
2071 req
.ndm
.ndm_family
= AF_INET
;
2072 req
.ndm
.ndm_state
= NUD_PERMANENT
;
2073 req
.ndm
.ndm_ifindex
= ifindex
;
2074 req
.ndm
.ndm_type
= RTN_UNICAST
;
2076 addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &addr
, 4);
2077 addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, lla
, llalen
);
2079 return netlink_talk (&req
.n
, &zns
->netlink_cmd
, NS_DEFAULT
);
2082 /* Routing table change via netlink interface. */
2083 /* Update flag indicates whether this is a "replace" or not. */
2085 netlink_route_multipath (int cmd
, struct prefix
*p
, struct rib
*rib
,
2086 int family
, int update
)
2089 struct sockaddr_nl snl
;
2090 struct nexthop
*nexthop
= NULL
, *tnexthop
;
2094 const char *routedesc
;
2102 char buf
[NL_PKT_BUF_SIZE
];
2105 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
2106 struct zebra_vrf
*zvrf
= vrf_info_lookup (rib
->vrf_id
);
2108 memset (&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2110 bytelen
= (family
== AF_INET
? 4 : 16);
2112 req
.n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct rtmsg
));
2113 req
.n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
2114 if ((cmd
== RTM_NEWROUTE
) && update
)
2115 req
.n
.nlmsg_flags
|= NLM_F_REPLACE
;
2116 req
.n
.nlmsg_type
= cmd
;
2117 req
.r
.rtm_family
= family
;
2118 req
.r
.rtm_dst_len
= p
->prefixlen
;
2119 req
.r
.rtm_protocol
= RTPROT_ZEBRA
;
2120 req
.r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
2122 if ((rib
->flags
& ZEBRA_FLAG_BLACKHOLE
) || (rib
->flags
& ZEBRA_FLAG_REJECT
))
2127 if (cmd
== RTM_NEWROUTE
)
2131 if (rib
->flags
& ZEBRA_FLAG_BLACKHOLE
)
2132 req
.r
.rtm_type
= RTN_BLACKHOLE
;
2133 else if (rib
->flags
& ZEBRA_FLAG_REJECT
)
2134 req
.r
.rtm_type
= RTN_UNREACHABLE
;
2136 assert (RTN_BLACKHOLE
!= RTN_UNREACHABLE
); /* false */
2139 req
.r
.rtm_type
= RTN_UNICAST
;
2142 addattr_l (&req
.n
, sizeof req
, RTA_DST
, &p
->u
.prefix
, bytelen
);
2145 /* Hardcode the metric for all routes coming from zebra. Metric isn't used
2146 * either by the kernel or by zebra. Its purely for calculating best path(s)
2147 * by the routing protocol and for communicating with protocol peers.
2149 addattr32 (&req
.n
, sizeof req
, RTA_PRIORITY
, NL_DEFAULT_ROUTE_METRIC
);
2151 /* Table corresponding to this route. */
2152 if (rib
->table
< 256)
2153 req
.r
.rtm_table
= rib
->table
;
2156 req
.r
.rtm_table
= RT_TABLE_UNSPEC
;
2157 addattr32(&req
.n
, sizeof req
, RTA_TABLE
, rib
->table
);
2162 if (cmd
== RTM_NEWROUTE
)
2163 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2165 /* We shouldn't encounter recursive nexthops on discard routes,
2166 * but it is probably better to handle that case correctly anyway.
2168 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2174 /* Count overall nexthops so we can decide whether to use singlepath
2175 * or multipath case. */
2177 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2179 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2181 if (cmd
== RTM_NEWROUTE
&& !CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2183 if (cmd
== RTM_DELROUTE
&& !CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2189 /* Singlepath case. */
2190 if (nexthop_num
== 1 || MULTIPATH_NUM
== 1)
2193 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2195 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2199 if (family
== AF_INET
)
2201 if (nexthop
->rmap_src
.ipv4
.s_addr
!= 0)
2203 src
.ipv4
= nexthop
->rmap_src
.ipv4
;
2206 else if (nexthop
->src
.ipv4
.s_addr
!= 0)
2208 src
.ipv4
= nexthop
->src
.ipv4
;
2212 else if (family
== AF_INET6
)
2214 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
2216 src
.ipv6
= nexthop
->rmap_src
.ipv6
;
2219 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
2221 src
.ipv6
= nexthop
->src
.ipv6
;
2229 if ((cmd
== RTM_NEWROUTE
2230 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2231 || (cmd
== RTM_DELROUTE
2232 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)))
2234 routedesc
= recursing
? "recursive, 1 hop" : "single hop";
2236 _netlink_route_debug(cmd
, p
, nexthop
, routedesc
, family
, zvrf
);
2237 _netlink_route_build_singlepath(routedesc
, bytelen
,
2238 nexthop
, &req
.n
, &req
.r
,
2244 if (setsrc
&& (cmd
== RTM_NEWROUTE
))
2246 if (family
== AF_INET
)
2247 addattr_l (&req
.n
, sizeof req
, RTA_PREFSRC
, &src
.ipv4
, bytelen
);
2248 else if (family
== AF_INET6
)
2249 addattr_l (&req
.n
, sizeof req
, RTA_PREFSRC
, &src
.ipv6
, bytelen
);
2254 char buf
[NL_PKT_BUF_SIZE
];
2255 struct rtattr
*rta
= (void *) buf
;
2256 struct rtnexthop
*rtnh
;
2257 union g_addr
*src1
= NULL
;
2259 rta
->rta_type
= RTA_MULTIPATH
;
2260 rta
->rta_len
= RTA_LENGTH (0);
2261 rtnh
= RTA_DATA (rta
);
2264 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2266 if (nexthop_num
>= MULTIPATH_NUM
)
2269 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
2271 /* This only works for IPv4 now */
2274 if (family
== AF_INET
)
2276 if (nexthop
->rmap_src
.ipv4
.s_addr
!= 0)
2278 src
.ipv4
= nexthop
->rmap_src
.ipv4
;
2281 else if (nexthop
->src
.ipv4
.s_addr
!= 0)
2283 src
.ipv4
= nexthop
->src
.ipv4
;
2287 else if (family
== AF_INET6
)
2289 if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->rmap_src
.ipv6
))
2291 src
.ipv6
= nexthop
->rmap_src
.ipv6
;
2294 else if (!IN6_IS_ADDR_UNSPECIFIED(&nexthop
->src
.ipv6
))
2296 src
.ipv6
= nexthop
->src
.ipv6
;
2304 if ((cmd
== RTM_NEWROUTE
2305 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2306 || (cmd
== RTM_DELROUTE
2307 && CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
)))
2309 routedesc
= recursing
? "recursive, multihop" : "multihop";
2312 _netlink_route_debug(cmd
, p
, nexthop
,
2313 routedesc
, family
, zvrf
);
2314 _netlink_route_build_multipath(routedesc
, bytelen
,
2315 nexthop
, rta
, rtnh
, &req
.r
, &src1
);
2316 rtnh
= RTNH_NEXT (rtnh
);
2318 if (!setsrc
&& src1
)
2320 if (family
== AF_INET
)
2321 src
.ipv4
= src1
->ipv4
;
2322 else if (family
== AF_INET6
)
2323 src
.ipv6
= src1
->ipv6
;
2329 if (setsrc
&& (cmd
== RTM_NEWROUTE
))
2331 if (family
== AF_INET
)
2332 addattr_l (&req
.n
, sizeof req
, RTA_PREFSRC
, &src
.ipv4
, bytelen
);
2333 else if (family
== AF_INET6
)
2334 addattr_l (&req
.n
, sizeof req
, RTA_PREFSRC
, &src
.ipv6
, bytelen
);
2335 zlog_debug("Setting source");
2338 if (rta
->rta_len
> RTA_LENGTH (0))
2339 addattr_l (&req
.n
, NL_PKT_BUF_SIZE
, RTA_MULTIPATH
, RTA_DATA (rta
),
2343 /* If there is no useful nexthop then return. */
2344 if (nexthop_num
== 0)
2346 if (IS_ZEBRA_DEBUG_KERNEL
)
2347 zlog_debug ("netlink_route_multipath(): No useful nexthop.");
2353 /* Destination netlink address. */
2354 memset (&snl
, 0, sizeof snl
);
2355 snl
.nl_family
= AF_NETLINK
;
2357 /* Talk to netlink socket. */
2358 return netlink_talk (&req
.n
, &zns
->netlink_cmd
, zns
);
2362 kernel_add_ipv4 (struct prefix
*p
, struct rib
*rib
)
2364 return netlink_route_multipath (RTM_NEWROUTE
, p
, rib
, AF_INET
, 0);
2368 kernel_update_ipv4 (struct prefix
*p
, struct rib
*rib
)
2370 return netlink_route_multipath (RTM_NEWROUTE
, p
, rib
, AF_INET
, 1);
2374 kernel_delete_ipv4 (struct prefix
*p
, struct rib
*rib
)
2376 return netlink_route_multipath (RTM_DELROUTE
, p
, rib
, AF_INET
, 0);
2381 kernel_add_ipv6 (struct prefix
*p
, struct rib
*rib
)
2384 return netlink_route_multipath (RTM_NEWROUTE
, p
, rib
, AF_INET6
, 0);
2389 kernel_update_ipv6 (struct prefix
*p
, struct rib
*rib
)
2391 return netlink_route_multipath (RTM_NEWROUTE
, p
, rib
, AF_INET6
, 1);
2395 kernel_delete_ipv6 (struct prefix
*p
, struct rib
*rib
)
2398 return netlink_route_multipath (RTM_DELROUTE
, p
, rib
, AF_INET6
, 0);
2402 /* Delete IPv6 route from the kernel. */
2404 kernel_delete_ipv6_old (struct prefix_ipv6
*dest
, struct in6_addr
*gate
,
2405 unsigned int index
, int flags
, int table
)
2407 return netlink_route (RTM_DELROUTE
, AF_INET6
, &dest
->prefix
,
2408 dest
->prefixlen
, gate
, index
, flags
, table
);
2410 #endif /* HAVE_IPV6 */
2412 /* Interface address modification. */
2414 netlink_address (int cmd
, int family
, struct interface
*ifp
,
2415 struct connected
*ifc
)
2423 struct ifaddrmsg ifa
;
2424 char buf
[NL_PKT_BUF_SIZE
];
2427 struct zebra_ns
*zns
= zebra_ns_lookup (NS_DEFAULT
);
2430 memset (&req
, 0, sizeof req
- NL_PKT_BUF_SIZE
);
2432 bytelen
= (family
== AF_INET
? 4 : 16);
2434 req
.n
.nlmsg_len
= NLMSG_LENGTH (sizeof (struct ifaddrmsg
));
2435 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
2436 req
.n
.nlmsg_type
= cmd
;
2437 req
.ifa
.ifa_family
= family
;
2439 req
.ifa
.ifa_index
= ifp
->ifindex
;
2440 req
.ifa
.ifa_prefixlen
= p
->prefixlen
;
2442 addattr_l (&req
.n
, sizeof req
, IFA_LOCAL
, &p
->u
.prefix
, bytelen
);
2444 if (family
== AF_INET
&& cmd
== RTM_NEWADDR
)
2446 if (!CONNECTED_PEER(ifc
) && ifc
->destination
)
2448 p
= ifc
->destination
;
2449 addattr_l (&req
.n
, sizeof req
, IFA_BROADCAST
, &p
->u
.prefix
,
2454 if (CHECK_FLAG (ifc
->flags
, ZEBRA_IFA_SECONDARY
))
2455 SET_FLAG (req
.ifa
.ifa_flags
, IFA_F_SECONDARY
);
2458 addattr_l (&req
.n
, sizeof req
, IFA_LABEL
, ifc
->label
,
2459 strlen (ifc
->label
) + 1);
2461 return netlink_talk (&req
.n
, &zns
->netlink_cmd
, zns
);
2465 kernel_address_add_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
2467 return netlink_address (RTM_NEWADDR
, AF_INET
, ifp
, ifc
);
2471 kernel_address_delete_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
2473 return netlink_address (RTM_DELADDR
, AF_INET
, ifp
, ifc
);
2477 extern struct thread_master
*master
;
2479 /* Kernel route reflection. */
2481 kernel_read (struct thread
*thread
)
2483 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG (thread
);
2484 netlink_parse_info (netlink_information_fetch
, &zns
->netlink
, zns
, 5);
2485 zns
->t_netlink
= thread_add_read (zebrad
.master
, kernel_read
, zns
,
2491 /* Filter out messages from self that occur on listener socket,
2492 caused by our actions on the command socket
2494 static void netlink_install_filter (int sock
, __u32 pid
)
2496 struct sock_filter filter
[] = {
2498 BPF_STMT(BPF_LD
|BPF_ABS
|BPF_H
, offsetof(struct nlmsghdr
, nlmsg_type
)),
2499 /* 1: jeq 0x18 jt 3 jf 6 */
2500 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, htons(RTM_NEWROUTE
), 1, 0),
2501 /* 2: jeq 0x19 jt 3 jf 6 */
2502 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, htons(RTM_DELROUTE
), 0, 3),
2504 BPF_STMT(BPF_LD
|BPF_ABS
|BPF_W
, offsetof(struct nlmsghdr
, nlmsg_pid
)),
2505 /* 4: jeq XX jt 5 jf 6 */
2506 BPF_JUMP(BPF_JMP
|BPF_JEQ
|BPF_K
, htonl(pid
), 0, 1),
2507 /* 5: ret 0 (skip) */
2508 BPF_STMT(BPF_RET
|BPF_K
, 0),
2509 /* 6: ret 0xffff (keep) */
2510 BPF_STMT(BPF_RET
|BPF_K
, 0xffff),
2513 struct sock_fprog prog
= {
2514 .len
= array_size(filter
),
2518 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
)) < 0)
2519 zlog_warn ("Can't install socket filter: %s\n", safe_strerror(errno
));
2522 /* Exported interface function. This function simply calls
2523 netlink_socket (). */
2525 kernel_init (struct zebra_ns
*zns
)
2527 unsigned long groups
;
2529 groups
= RTMGRP_LINK
| RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_IFADDR
;
2531 groups
|= RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFADDR
;
2532 #endif /* HAVE_IPV6 */
2533 netlink_socket (&zns
->netlink
, groups
, zns
->ns_id
);
2534 netlink_socket (&zns
->netlink_cmd
, 0, zns
->ns_id
);
2536 /* Register kernel socket. */
2537 if (zns
->netlink
.sock
> 0)
2539 /* Only want non-blocking on the netlink event socket */
2540 if (fcntl (zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
2541 zlog_err ("Can't set %s socket flags: %s", zns
->netlink
.name
,
2542 safe_strerror (errno
));
2544 /* Set receive buffer size if it's set from command line */
2546 netlink_recvbuf (&zns
->netlink
, nl_rcvbufsize
);
2548 netlink_install_filter (zns
->netlink
.sock
, zns
->netlink_cmd
.snl
.nl_pid
);
2549 zns
->t_netlink
= thread_add_read (zebrad
.master
, kernel_read
, zns
,
2555 kernel_terminate (struct zebra_ns
*zns
)
2557 THREAD_READ_OFF (zns
->t_netlink
);
2559 if (zns
->netlink
.sock
>= 0)
2561 close (zns
->netlink
.sock
);
2562 zns
->netlink
.sock
= -1;
2565 if (zns
->netlink_cmd
.sock
>= 0)
2567 close (zns
->netlink_cmd
.sock
);
2568 zns
->netlink_cmd
.sock
= -1;
2573 * nl_msg_type_to_str
2576 nl_msg_type_to_str (uint16_t msg_type
)
2578 return lookup (nlmsg_str
, msg_type
);
2585 nl_rtproto_to_str (u_char rtproto
)
2587 return lookup (rtproto_str
, rtproto
);