paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Kernel routing table updates using netlink over GNU/Linux system. |
| 2 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 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 |
| 9 | * later version. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
Timo Teräs | 954c7d6 | 2016-01-15 17:36:33 +0200 | [diff] [blame] | 23 | #include <net/if_arp.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 24 | |
| 25 | /* Hack for GNU libc version 2. */ |
| 26 | #ifndef MSG_TRUNC |
| 27 | #define MSG_TRUNC 0x20 |
| 28 | #endif /* MSG_TRUNC */ |
| 29 | |
| 30 | #include "linklist.h" |
| 31 | #include "if.h" |
| 32 | #include "log.h" |
| 33 | #include "prefix.h" |
| 34 | #include "connected.h" |
| 35 | #include "table.h" |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 36 | #include "memory.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | #include "rib.h" |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 38 | #include "thread.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 39 | #include "privs.h" |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 40 | #include "vrf.h" |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 41 | #include "nexthop.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | |
| 43 | #include "zebra/zserv.h" |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 44 | #include "zebra/rt.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | #include "zebra/redistribute.h" |
| 46 | #include "zebra/interface.h" |
| 47 | #include "zebra/debug.h" |
| 48 | |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 49 | #include "rt_netlink.h" |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 50 | |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 51 | static const struct message nlmsg_str[] = { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | {RTM_NEWROUTE, "RTM_NEWROUTE"}, |
| 53 | {RTM_DELROUTE, "RTM_DELROUTE"}, |
| 54 | {RTM_GETROUTE, "RTM_GETROUTE"}, |
| 55 | {RTM_NEWLINK, "RTM_NEWLINK"}, |
| 56 | {RTM_DELLINK, "RTM_DELLINK"}, |
| 57 | {RTM_GETLINK, "RTM_GETLINK"}, |
| 58 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 59 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 60 | {RTM_GETADDR, "RTM_GETADDR"}, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 61 | {0, NULL} |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 64 | extern struct zebra_t zebrad; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 65 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 66 | extern struct zebra_privs_t zserv_privs; |
| 67 | |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 68 | extern u_int32_t nl_rcvbufsize; |
| 69 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 70 | /* Note: on netlink systems, there should be a 1-to-1 mapping between interface |
| 71 | names and ifindex values. */ |
| 72 | static void |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 73 | set_ifindex(struct interface *ifp, ifindex_t ifi_index) |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 74 | { |
| 75 | struct interface *oifp; |
| 76 | |
| 77 | if (((oifp = if_lookup_by_index(ifi_index)) != NULL) && (oifp != ifp)) |
| 78 | { |
| 79 | if (ifi_index == IFINDEX_INTERNAL) |
| 80 | zlog_err("Netlink is setting interface %s ifindex to reserved " |
| 81 | "internal value %u", ifp->name, ifi_index); |
| 82 | else |
| 83 | { |
| 84 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 85 | zlog_debug("interface index %d was renamed from %s to %s", |
| 86 | ifi_index, oifp->name, ifp->name); |
| 87 | if (if_is_up(oifp)) |
| 88 | zlog_err("interface rename detected on up interface: index %d " |
| 89 | "was renamed from %s to %s, results are uncertain!", |
| 90 | ifi_index, oifp->name, ifp->name); |
| 91 | if_delete_update(oifp); |
| 92 | } |
| 93 | } |
| 94 | ifp->ifindex = ifi_index; |
| 95 | } |
| 96 | |
Ulrich Weber | f1ef81b | 2013-01-22 10:39:18 +0000 | [diff] [blame] | 97 | #ifndef SO_RCVBUFFORCE |
| 98 | #define SO_RCVBUFFORCE (33) |
| 99 | #endif |
| 100 | |
Stephen Hemminger | 30afea3 | 2008-08-16 18:25:47 +0100 | [diff] [blame] | 101 | static int |
| 102 | netlink_recvbuf (struct nlsock *nl, uint32_t newsize) |
| 103 | { |
| 104 | u_int32_t oldsize; |
| 105 | socklen_t newlen = sizeof(newsize); |
| 106 | socklen_t oldlen = sizeof(oldsize); |
| 107 | int ret; |
| 108 | |
| 109 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen); |
| 110 | if (ret < 0) |
| 111 | { |
| 112 | zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name, |
| 113 | safe_strerror (errno)); |
| 114 | return -1; |
| 115 | } |
| 116 | |
Ulrich Weber | f1ef81b | 2013-01-22 10:39:18 +0000 | [diff] [blame] | 117 | /* Try force option (linux >= 2.6.14) and fall back to normal set */ |
| 118 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 119 | zlog_err ("routing_socket: Can't raise privileges"); |
| 120 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, &nl_rcvbufsize, |
Stephen Hemminger | 30afea3 | 2008-08-16 18:25:47 +0100 | [diff] [blame] | 121 | sizeof(nl_rcvbufsize)); |
Ulrich Weber | f1ef81b | 2013-01-22 10:39:18 +0000 | [diff] [blame] | 122 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 123 | zlog_err ("routing_socket: Can't lower privileges"); |
| 124 | if (ret < 0) |
| 125 | ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &nl_rcvbufsize, |
| 126 | sizeof(nl_rcvbufsize)); |
Stephen Hemminger | 30afea3 | 2008-08-16 18:25:47 +0100 | [diff] [blame] | 127 | if (ret < 0) |
| 128 | { |
| 129 | zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name, |
| 130 | safe_strerror (errno)); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen); |
| 135 | if (ret < 0) |
| 136 | { |
| 137 | zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name, |
| 138 | safe_strerror (errno)); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | zlog (NULL, LOG_INFO, |
| 143 | "Setting netlink socket receive buffer size: %u -> %u", |
| 144 | oldsize, newsize); |
| 145 | return 0; |
| 146 | } |
| 147 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | /* Make socket for Linux netlink interface. */ |
| 149 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 150 | netlink_socket (struct nlsock *nl, unsigned long groups, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | { |
| 152 | int ret; |
| 153 | struct sockaddr_nl snl; |
| 154 | int sock; |
| 155 | int namelen; |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 156 | int save_errno; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | |
Michal Sekletar | 8e998b1 | 2014-05-16 14:13:43 +0000 | [diff] [blame] | 158 | if (zserv_privs.change (ZPRIVS_RAISE)) |
| 159 | { |
| 160 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 161 | return -1; |
| 162 | } |
| 163 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 164 | sock = vrf_socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | if (sock < 0) |
| 166 | { |
| 167 | zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name, |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 168 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 169 | return -1; |
| 170 | } |
| 171 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | memset (&snl, 0, sizeof snl); |
| 173 | snl.nl_family = AF_NETLINK; |
| 174 | snl.nl_groups = groups; |
| 175 | |
| 176 | /* Bind the socket to the netlink structure for anything. */ |
| 177 | ret = bind (sock, (struct sockaddr *) &snl, sizeof snl); |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 178 | save_errno = errno; |
hasso | 55e7ecd | 2004-08-06 08:41:56 +0000 | [diff] [blame] | 179 | if (zserv_privs.change (ZPRIVS_LOWER)) |
| 180 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 181 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | if (ret < 0) |
| 183 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 184 | zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s", |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 185 | nl->name, snl.nl_groups, safe_strerror (save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | close (sock); |
| 187 | return -1; |
| 188 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 189 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | /* multiple netlink sockets will have different nl_pid */ |
| 191 | namelen = sizeof snl; |
hasso | c9e52be | 2004-09-26 16:09:34 +0000 | [diff] [blame] | 192 | ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | if (ret < 0 || namelen != sizeof snl) |
| 194 | { |
| 195 | zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name, |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 196 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | close (sock); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | nl->snl = snl; |
| 202 | nl->sock = sock; |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | /* Get type specified information from netlink. */ |
| 207 | static int |
| 208 | netlink_request (int family, int type, struct nlsock *nl) |
| 209 | { |
| 210 | int ret; |
| 211 | struct sockaddr_nl snl; |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 212 | int save_errno; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | |
| 214 | struct |
| 215 | { |
| 216 | struct nlmsghdr nlh; |
| 217 | struct rtgenmsg g; |
| 218 | } req; |
| 219 | |
| 220 | |
| 221 | /* Check netlink socket. */ |
| 222 | if (nl->sock < 0) |
| 223 | { |
| 224 | zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | memset (&snl, 0, sizeof snl); |
| 229 | snl.nl_family = AF_NETLINK; |
| 230 | |
ajs | c05612b | 2005-10-01 16:36:54 +0000 | [diff] [blame] | 231 | memset (&req, 0, sizeof req); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 232 | req.nlh.nlmsg_len = sizeof req; |
| 233 | req.nlh.nlmsg_type = type; |
| 234 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
Stephen Hemminger | 3d265b4 | 2008-08-16 17:30:39 +0100 | [diff] [blame] | 235 | req.nlh.nlmsg_pid = nl->snl.nl_pid; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | req.nlh.nlmsg_seq = ++nl->seq; |
| 237 | req.g.rtgen_family = family; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 238 | |
| 239 | /* linux appears to check capabilities on every message |
| 240 | * have to raise caps for every message sent |
| 241 | */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 242 | if (zserv_privs.change (ZPRIVS_RAISE)) |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 243 | { |
| 244 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 245 | return -1; |
| 246 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 247 | |
| 248 | ret = sendto (nl->sock, (void *) &req, sizeof req, 0, |
| 249 | (struct sockaddr *) &snl, sizeof snl); |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 250 | save_errno = errno; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 251 | |
| 252 | if (zserv_privs.change (ZPRIVS_LOWER)) |
| 253 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 254 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 255 | if (ret < 0) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 256 | { |
| 257 | zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name, |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 258 | safe_strerror (save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | return -1; |
| 260 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 261 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* Receive message from netlink interface and pass those information |
| 266 | to the given function. */ |
| 267 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 268 | netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *, |
| 269 | vrf_id_t), |
| 270 | struct nlsock *nl, struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | { |
| 272 | int status; |
| 273 | int ret = 0; |
| 274 | int error; |
| 275 | |
| 276 | while (1) |
| 277 | { |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 278 | char buf[NL_PKT_BUF_SIZE]; |
Timo Teräs | c299ed7 | 2014-07-29 09:41:55 +0000 | [diff] [blame] | 279 | struct iovec iov = { |
| 280 | .iov_base = buf, |
| 281 | .iov_len = sizeof buf |
| 282 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | struct sockaddr_nl snl; |
Timo Teräs | c299ed7 | 2014-07-29 09:41:55 +0000 | [diff] [blame] | 284 | struct msghdr msg = { |
| 285 | .msg_name = (void *) &snl, |
| 286 | .msg_namelen = sizeof snl, |
| 287 | .msg_iov = &iov, |
| 288 | .msg_iovlen = 1 |
| 289 | }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 290 | struct nlmsghdr *h; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 291 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 292 | status = recvmsg (nl->sock, &msg, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 293 | if (status < 0) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 294 | { |
Stephen Hemminger | 4c69947 | 2008-08-17 17:01:44 +0100 | [diff] [blame] | 295 | if (errno == EINTR) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 296 | continue; |
Stephen Hemminger | 4c69947 | 2008-08-17 17:01:44 +0100 | [diff] [blame] | 297 | if (errno == EWOULDBLOCK || errno == EAGAIN) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 298 | break; |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 299 | zlog (NULL, LOG_ERR, "%s recvmsg overrun: %s", |
Stephen Hemminger | 4c69947 | 2008-08-17 17:01:44 +0100 | [diff] [blame] | 300 | nl->name, safe_strerror(errno)); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 301 | continue; |
| 302 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 303 | |
| 304 | if (status == 0) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 305 | { |
| 306 | zlog (NULL, LOG_ERR, "%s EOF", nl->name); |
| 307 | return -1; |
| 308 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 309 | |
| 310 | if (msg.msg_namelen != sizeof snl) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 311 | { |
| 312 | zlog (NULL, LOG_ERR, "%s sender address length error: length %d", |
| 313 | nl->name, msg.msg_namelen); |
| 314 | return -1; |
| 315 | } |
paul | b84d3a1 | 2003-11-17 10:31:01 +0000 | [diff] [blame] | 316 | |
hasso | 206d805 | 2005-04-09 16:38:51 +0000 | [diff] [blame] | 317 | for (h = (struct nlmsghdr *) buf; NLMSG_OK (h, (unsigned int) status); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 318 | h = NLMSG_NEXT (h, status)) |
| 319 | { |
| 320 | /* Finish of reading. */ |
| 321 | if (h->nlmsg_type == NLMSG_DONE) |
| 322 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 324 | /* Error handling. */ |
| 325 | if (h->nlmsg_type == NLMSG_ERROR) |
| 326 | { |
| 327 | struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h); |
Stephen Hemminger | 898987e | 2008-08-17 16:56:15 +0100 | [diff] [blame] | 328 | int errnum = err->error; |
| 329 | int msg_type = err->msg.nlmsg_type; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 330 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 331 | /* If the error field is zero, then this is an ACK */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 332 | if (err->error == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 333 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 334 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 335 | { |
hasso | 1ada819 | 2005-06-12 11:28:18 +0000 | [diff] [blame] | 336 | zlog_debug ("%s: %s ACK: type=%s(%u), seq=%u, pid=%u", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 337 | __FUNCTION__, nl->name, |
| 338 | lookup (nlmsg_str, err->msg.nlmsg_type), |
| 339 | err->msg.nlmsg_type, err->msg.nlmsg_seq, |
| 340 | err->msg.nlmsg_pid); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 341 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 342 | |
| 343 | /* return if not a multipart message, otherwise continue */ |
| 344 | if (!(h->nlmsg_flags & NLM_F_MULTI)) |
| 345 | { |
| 346 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 347 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 348 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 349 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 350 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr))) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 352 | { |
| 353 | zlog (NULL, LOG_ERR, "%s error: message truncated", |
| 354 | nl->name); |
| 355 | return -1; |
| 356 | } |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 357 | |
Stephen Hemminger | 898987e | 2008-08-17 16:56:15 +0100 | [diff] [blame] | 358 | /* Deal with errors that occur because of races in link handling */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 359 | if (nl == &zvrf->netlink_cmd |
Stephen Hemminger | 898987e | 2008-08-17 16:56:15 +0100 | [diff] [blame] | 360 | && ((msg_type == RTM_DELROUTE && |
| 361 | (-errnum == ENODEV || -errnum == ESRCH)) |
| 362 | || (msg_type == RTM_NEWROUTE && -errnum == EEXIST))) |
| 363 | { |
| 364 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 365 | zlog_debug ("%s: error: %s type=%s(%u), seq=%u, pid=%u", |
| 366 | nl->name, safe_strerror (-errnum), |
| 367 | lookup (nlmsg_str, msg_type), |
| 368 | msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid); |
| 369 | return 0; |
| 370 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 371 | |
Stephen Hemminger | 898987e | 2008-08-17 16:56:15 +0100 | [diff] [blame] | 372 | zlog_err ("%s error: %s, type=%s(%u), seq=%u, pid=%u", |
| 373 | nl->name, safe_strerror (-errnum), |
| 374 | lookup (nlmsg_str, msg_type), |
| 375 | msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 376 | return -1; |
| 377 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 379 | /* OK we got netlink message. */ |
| 380 | if (IS_ZEBRA_DEBUG_KERNEL) |
hasso | 1ada819 | 2005-06-12 11:28:18 +0000 | [diff] [blame] | 381 | zlog_debug ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%u", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 382 | nl->name, |
| 383 | lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type, |
| 384 | h->nlmsg_seq, h->nlmsg_pid); |
| 385 | |
Christian Franke | 599da95 | 2013-01-24 14:04:43 +0000 | [diff] [blame] | 386 | /* skip unsolicited messages originating from command socket |
| 387 | * linux sets the originators port-id for {NEW|DEL}ADDR messages, |
| 388 | * so this has to be checked here. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 389 | if (nl != &zvrf->netlink_cmd |
| 390 | && h->nlmsg_pid == zvrf->netlink_cmd.snl.nl_pid |
Christian Franke | 599da95 | 2013-01-24 14:04:43 +0000 | [diff] [blame] | 391 | && (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 392 | { |
| 393 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 394 | zlog_debug ("netlink_parse_info: %s packet comes from %s", |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 395 | zvrf->netlink_cmd.name, nl->name); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 396 | continue; |
| 397 | } |
| 398 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 399 | error = (*filter) (&snl, h, zvrf->vrf_id); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 400 | if (error < 0) |
| 401 | { |
| 402 | zlog (NULL, LOG_ERR, "%s filter function error", nl->name); |
| 403 | ret = error; |
| 404 | } |
| 405 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | |
| 407 | /* After error care. */ |
| 408 | if (msg.msg_flags & MSG_TRUNC) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 409 | { |
| 410 | zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name); |
| 411 | continue; |
| 412 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 413 | if (status) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 414 | { |
| 415 | zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name, |
| 416 | status); |
| 417 | return -1; |
| 418 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 419 | } |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | /* Utility function for parse rtattr. */ |
| 424 | static void |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 425 | netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta, |
| 426 | int len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 428 | while (RTA_OK (rta, len)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 429 | { |
| 430 | if (rta->rta_type <= max) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 431 | tb[rta->rta_type] = rta; |
| 432 | rta = RTA_NEXT (rta, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 436 | /* Utility function to parse hardware link-layer address and update ifp */ |
| 437 | static void |
| 438 | netlink_interface_update_hw_addr (struct rtattr **tb, struct interface *ifp) |
| 439 | { |
| 440 | int i; |
| 441 | |
| 442 | if (tb[IFLA_ADDRESS]) |
| 443 | { |
| 444 | int hw_addr_len; |
| 445 | |
| 446 | hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]); |
| 447 | |
| 448 | if (hw_addr_len > INTERFACE_HWADDR_MAX) |
| 449 | zlog_warn ("Hardware address is too large: %d", hw_addr_len); |
| 450 | else |
| 451 | { |
| 452 | ifp->hw_addr_len = hw_addr_len; |
| 453 | memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len); |
| 454 | |
| 455 | for (i = 0; i < hw_addr_len; i++) |
| 456 | if (ifp->hw_addr[i] != 0) |
| 457 | break; |
| 458 | |
| 459 | if (i == hw_addr_len) |
| 460 | ifp->hw_addr_len = 0; |
| 461 | else |
| 462 | ifp->hw_addr_len = hw_addr_len; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
Timo Teräs | 954c7d6 | 2016-01-15 17:36:33 +0200 | [diff] [blame] | 467 | static enum zebra_link_type |
| 468 | netlink_to_zebra_link_type (unsigned int hwt) |
| 469 | { |
| 470 | switch (hwt) |
| 471 | { |
| 472 | case ARPHRD_ETHER: return ZEBRA_LLT_ETHER; |
| 473 | case ARPHRD_EETHER: return ZEBRA_LLT_EETHER; |
| 474 | case ARPHRD_AX25: return ZEBRA_LLT_AX25; |
| 475 | case ARPHRD_PRONET: return ZEBRA_LLT_PRONET; |
| 476 | case ARPHRD_IEEE802: return ZEBRA_LLT_IEEE802; |
| 477 | case ARPHRD_ARCNET: return ZEBRA_LLT_ARCNET; |
| 478 | case ARPHRD_APPLETLK: return ZEBRA_LLT_APPLETLK; |
| 479 | case ARPHRD_DLCI: return ZEBRA_LLT_DLCI; |
| 480 | case ARPHRD_ATM: return ZEBRA_LLT_ATM; |
| 481 | case ARPHRD_METRICOM: return ZEBRA_LLT_METRICOM; |
| 482 | case ARPHRD_IEEE1394: return ZEBRA_LLT_IEEE1394; |
| 483 | case ARPHRD_EUI64: return ZEBRA_LLT_EUI64; |
| 484 | case ARPHRD_INFINIBAND: return ZEBRA_LLT_INFINIBAND; |
| 485 | case ARPHRD_SLIP: return ZEBRA_LLT_SLIP; |
| 486 | case ARPHRD_CSLIP: return ZEBRA_LLT_CSLIP; |
| 487 | case ARPHRD_SLIP6: return ZEBRA_LLT_SLIP6; |
| 488 | case ARPHRD_CSLIP6: return ZEBRA_LLT_CSLIP6; |
| 489 | case ARPHRD_RSRVD: return ZEBRA_LLT_RSRVD; |
| 490 | case ARPHRD_ADAPT: return ZEBRA_LLT_ADAPT; |
| 491 | case ARPHRD_ROSE: return ZEBRA_LLT_ROSE; |
| 492 | case ARPHRD_X25: return ZEBRA_LLT_X25; |
| 493 | case ARPHRD_PPP: return ZEBRA_LLT_PPP; |
| 494 | case ARPHRD_CISCO: return ZEBRA_LLT_CHDLC; |
| 495 | case ARPHRD_LAPB: return ZEBRA_LLT_LAPB; |
| 496 | case ARPHRD_RAWHDLC: return ZEBRA_LLT_RAWHDLC; |
| 497 | case ARPHRD_TUNNEL: return ZEBRA_LLT_IPIP; |
| 498 | case ARPHRD_TUNNEL6: return ZEBRA_LLT_IPIP6; |
| 499 | case ARPHRD_FRAD: return ZEBRA_LLT_FRAD; |
| 500 | case ARPHRD_SKIP: return ZEBRA_LLT_SKIP; |
| 501 | case ARPHRD_LOOPBACK: return ZEBRA_LLT_LOOPBACK; |
| 502 | case ARPHRD_LOCALTLK: return ZEBRA_LLT_LOCALTLK; |
| 503 | case ARPHRD_FDDI: return ZEBRA_LLT_FDDI; |
| 504 | case ARPHRD_SIT: return ZEBRA_LLT_SIT; |
| 505 | case ARPHRD_IPDDP: return ZEBRA_LLT_IPDDP; |
| 506 | case ARPHRD_IPGRE: return ZEBRA_LLT_IPGRE; |
| 507 | case ARPHRD_PIMREG: return ZEBRA_LLT_PIMREG; |
| 508 | case ARPHRD_HIPPI: return ZEBRA_LLT_HIPPI; |
| 509 | case ARPHRD_ECONET: return ZEBRA_LLT_ECONET; |
| 510 | case ARPHRD_IRDA: return ZEBRA_LLT_IRDA; |
| 511 | case ARPHRD_FCPP: return ZEBRA_LLT_FCPP; |
| 512 | case ARPHRD_FCAL: return ZEBRA_LLT_FCAL; |
| 513 | case ARPHRD_FCPL: return ZEBRA_LLT_FCPL; |
| 514 | case ARPHRD_FCFABRIC: return ZEBRA_LLT_FCFABRIC; |
| 515 | case ARPHRD_IEEE802_TR: return ZEBRA_LLT_IEEE802_TR; |
| 516 | case ARPHRD_IEEE80211: return ZEBRA_LLT_IEEE80211; |
| 517 | case ARPHRD_IEEE802154: return ZEBRA_LLT_IEEE802154; |
| 518 | #ifdef ARPHRD_IP6GRE |
| 519 | case ARPHRD_IP6GRE: return ZEBRA_LLT_IP6GRE; |
| 520 | #endif |
| 521 | #ifdef ARPHRD_IEEE802154_PHY |
| 522 | case ARPHRD_IEEE802154_PHY: return ZEBRA_LLT_IEEE802154_PHY; |
| 523 | #endif |
| 524 | |
| 525 | default: return ZEBRA_LLT_UNKNOWN; |
| 526 | } |
| 527 | } |
| 528 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 529 | /* Called from interface_lookup_netlink(). This function is only used |
| 530 | during bootstrap. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 531 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 532 | netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 533 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | { |
| 535 | int len; |
| 536 | struct ifinfomsg *ifi; |
| 537 | struct rtattr *tb[IFLA_MAX + 1]; |
| 538 | struct interface *ifp; |
| 539 | char *name; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 540 | |
| 541 | ifi = NLMSG_DATA (h); |
| 542 | |
| 543 | if (h->nlmsg_type != RTM_NEWLINK) |
| 544 | return 0; |
| 545 | |
| 546 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); |
| 547 | if (len < 0) |
| 548 | return -1; |
| 549 | |
| 550 | /* Looking up interface name. */ |
| 551 | memset (tb, 0, sizeof tb); |
| 552 | netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len); |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 553 | |
paul | 1e19315 | 2005-02-14 23:53:05 +0000 | [diff] [blame] | 554 | #ifdef IFLA_WIRELESS |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 555 | /* check for wireless messages to ignore */ |
| 556 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) |
| 557 | { |
| 558 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 559 | zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__); |
| 560 | return 0; |
| 561 | } |
paul | 1e19315 | 2005-02-14 23:53:05 +0000 | [diff] [blame] | 562 | #endif /* IFLA_WIRELESS */ |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 563 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 564 | if (tb[IFLA_IFNAME] == NULL) |
| 565 | return -1; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 566 | name = (char *) RTA_DATA (tb[IFLA_IFNAME]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 567 | |
| 568 | /* Add interface. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 569 | ifp = if_get_by_name_vrf (name, vrf_id); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 570 | set_ifindex(ifp, ifi->ifi_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 571 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
Stephen Hemminger | 4308abb | 2008-12-01 14:19:38 -0800 | [diff] [blame] | 572 | ifp->mtu6 = ifp->mtu = *(uint32_t *) RTA_DATA (tb[IFLA_MTU]); |
Brett Ciphery | db19c85 | 2013-10-03 13:48:54 +0000 | [diff] [blame] | 573 | ifp->metric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 574 | |
| 575 | /* Hardware type and address. */ |
Timo Teräs | 954c7d6 | 2016-01-15 17:36:33 +0200 | [diff] [blame] | 576 | ifp->ll_type = netlink_to_zebra_link_type (ifi->ifi_type); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 577 | netlink_interface_update_hw_addr (tb, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 578 | |
| 579 | if_add_update (ifp); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /* Lookup interface IPv4/IPv6 address. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 585 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 586 | netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 587 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 588 | { |
| 589 | int len; |
| 590 | struct ifaddrmsg *ifa; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 591 | struct rtattr *tb[IFA_MAX + 1]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 592 | struct interface *ifp; |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 593 | void *addr; |
| 594 | void *broad; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 595 | u_char flags = 0; |
| 596 | char *label = NULL; |
| 597 | |
| 598 | ifa = NLMSG_DATA (h); |
| 599 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 600 | if (ifa->ifa_family != AF_INET |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 601 | #ifdef HAVE_IPV6 |
| 602 | && ifa->ifa_family != AF_INET6 |
| 603 | #endif /* HAVE_IPV6 */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 604 | ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 605 | return 0; |
| 606 | |
| 607 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) |
| 608 | return 0; |
| 609 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 610 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | if (len < 0) |
| 612 | return -1; |
| 613 | |
| 614 | memset (tb, 0, sizeof tb); |
| 615 | netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len); |
| 616 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 617 | ifp = if_lookup_by_index_vrf (ifa->ifa_index, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 618 | if (ifp == NULL) |
| 619 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 620 | zlog_err ("netlink_interface_addr can't find interface by index %d vrf %u", |
| 621 | ifa->ifa_index, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | return -1; |
| 623 | } |
| 624 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 625 | if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 626 | { |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 627 | char buf[BUFSIZ]; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 628 | zlog_debug ("netlink_interface_addr %s %s vrf %u:", |
| 629 | lookup (nlmsg_str, h->nlmsg_type), ifp->name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | if (tb[IFA_LOCAL]) |
hasso | 206d805 | 2005-04-09 16:38:51 +0000 | [diff] [blame] | 631 | zlog_debug (" IFA_LOCAL %s/%d", |
| 632 | inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_LOCAL]), |
| 633 | buf, BUFSIZ), ifa->ifa_prefixlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 634 | if (tb[IFA_ADDRESS]) |
hasso | 206d805 | 2005-04-09 16:38:51 +0000 | [diff] [blame] | 635 | zlog_debug (" IFA_ADDRESS %s/%d", |
| 636 | inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_ADDRESS]), |
| 637 | buf, BUFSIZ), ifa->ifa_prefixlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 638 | if (tb[IFA_BROADCAST]) |
hasso | 206d805 | 2005-04-09 16:38:51 +0000 | [diff] [blame] | 639 | zlog_debug (" IFA_BROADCAST %s/%d", |
| 640 | inet_ntop (ifa->ifa_family, RTA_DATA (tb[IFA_BROADCAST]), |
| 641 | buf, BUFSIZ), ifa->ifa_prefixlen); |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 642 | if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL]))) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 643 | zlog_debug (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL])); |
paul | d34b899 | 2006-01-17 18:03:04 +0000 | [diff] [blame] | 644 | |
| 645 | if (tb[IFA_CACHEINFO]) |
| 646 | { |
| 647 | struct ifa_cacheinfo *ci = RTA_DATA (tb[IFA_CACHEINFO]); |
| 648 | zlog_debug (" IFA_CACHEINFO pref %d, valid %d", |
| 649 | ci->ifa_prefered, ci->ifa_valid); |
| 650 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | } |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 652 | |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 653 | /* logic copied from iproute2/ip/ipaddress.c:print_addrinfo() */ |
| 654 | if (tb[IFA_LOCAL] == NULL) |
| 655 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 656 | if (tb[IFA_ADDRESS] == NULL) |
| 657 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; |
| 658 | |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 659 | /* local interface address */ |
| 660 | addr = (tb[IFA_LOCAL] ? RTA_DATA(tb[IFA_LOCAL]) : NULL); |
| 661 | |
| 662 | /* is there a peer address? */ |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 663 | if (tb[IFA_ADDRESS] && |
vize | 068fd77 | 2007-08-10 06:25:20 +0000 | [diff] [blame] | 664 | memcmp(RTA_DATA(tb[IFA_ADDRESS]), RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_ADDRESS]))) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 665 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 666 | broad = RTA_DATA(tb[IFA_ADDRESS]); |
| 667 | SET_FLAG (flags, ZEBRA_IFA_PEER); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 668 | } |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 669 | else |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 670 | /* seeking a broadcast address */ |
| 671 | broad = (tb[IFA_BROADCAST] ? RTA_DATA(tb[IFA_BROADCAST]) : NULL); |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 672 | |
Paul Jakma | 27b4725 | 2006-07-02 16:38:54 +0000 | [diff] [blame] | 673 | /* addr is primary key, SOL if we don't have one */ |
| 674 | if (addr == NULL) |
| 675 | { |
| 676 | zlog_debug ("%s: NULL address", __func__); |
| 677 | return -1; |
| 678 | } |
| 679 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 680 | /* Flags. */ |
| 681 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 682 | SET_FLAG (flags, ZEBRA_IFA_SECONDARY); |
| 683 | |
| 684 | /* Label */ |
| 685 | if (tb[IFA_LABEL]) |
| 686 | label = (char *) RTA_DATA (tb[IFA_LABEL]); |
| 687 | |
| 688 | if (ifp && label && strcmp (ifp->name, label) == 0) |
| 689 | label = NULL; |
| 690 | |
| 691 | /* Register interface address to the interface. */ |
| 692 | if (ifa->ifa_family == AF_INET) |
| 693 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 694 | if (h->nlmsg_type == RTM_NEWADDR) |
| 695 | connected_add_ipv4 (ifp, flags, |
| 696 | (struct in_addr *) addr, ifa->ifa_prefixlen, |
| 697 | (struct in_addr *) broad, label); |
| 698 | else |
| 699 | connected_delete_ipv4 (ifp, flags, |
| 700 | (struct in_addr *) addr, ifa->ifa_prefixlen, |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 701 | (struct in_addr *) broad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 702 | } |
| 703 | #ifdef HAVE_IPV6 |
| 704 | if (ifa->ifa_family == AF_INET6) |
| 705 | { |
| 706 | if (h->nlmsg_type == RTM_NEWADDR) |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 707 | connected_add_ipv6 (ifp, flags, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 708 | (struct in6_addr *) addr, ifa->ifa_prefixlen, |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 709 | (struct in6_addr *) broad, label); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 710 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 711 | connected_delete_ipv6 (ifp, |
| 712 | (struct in6_addr *) addr, ifa->ifa_prefixlen, |
| 713 | (struct in6_addr *) broad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 714 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 715 | #endif /* HAVE_IPV6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /* Looking up routing table by netlink interface. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 721 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 722 | netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 723 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | { |
| 725 | int len; |
| 726 | struct rtmsg *rtm; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 727 | struct rtattr *tb[RTA_MAX + 1]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 728 | u_char flags = 0; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 729 | |
| 730 | char anyaddr[16] = { 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 731 | |
| 732 | int index; |
| 733 | int table; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 734 | u_int32_t mtu = 0; |
hasso | 34195bf | 2004-04-06 12:07:06 +0000 | [diff] [blame] | 735 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 736 | void *dest; |
| 737 | void *gate; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 738 | void *src; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 739 | |
| 740 | rtm = NLMSG_DATA (h); |
| 741 | |
| 742 | if (h->nlmsg_type != RTM_NEWROUTE) |
| 743 | return 0; |
| 744 | if (rtm->rtm_type != RTN_UNICAST) |
| 745 | return 0; |
| 746 | |
| 747 | table = rtm->rtm_table; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 748 | #if 0 /* we weed them out later in rib_weed_tables () */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 749 | if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 750 | return 0; |
| 751 | #endif |
| 752 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 753 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 754 | if (len < 0) |
| 755 | return -1; |
| 756 | |
| 757 | memset (tb, 0, sizeof tb); |
| 758 | netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len); |
| 759 | |
| 760 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 761 | return 0; |
| 762 | if (rtm->rtm_protocol == RTPROT_REDIRECT) |
| 763 | return 0; |
| 764 | if (rtm->rtm_protocol == RTPROT_KERNEL) |
| 765 | return 0; |
| 766 | |
| 767 | if (rtm->rtm_src_len != 0) |
| 768 | return 0; |
| 769 | |
| 770 | /* Route which inserted by Zebra. */ |
| 771 | if (rtm->rtm_protocol == RTPROT_ZEBRA) |
| 772 | flags |= ZEBRA_FLAG_SELFROUTE; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 773 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 774 | index = 0; |
| 775 | dest = NULL; |
| 776 | gate = NULL; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 777 | src = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 778 | |
| 779 | if (tb[RTA_OIF]) |
| 780 | index = *(int *) RTA_DATA (tb[RTA_OIF]); |
| 781 | |
| 782 | if (tb[RTA_DST]) |
| 783 | dest = RTA_DATA (tb[RTA_DST]); |
| 784 | else |
| 785 | dest = anyaddr; |
| 786 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 787 | if (tb[RTA_PREFSRC]) |
| 788 | src = RTA_DATA (tb[RTA_PREFSRC]); |
| 789 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 790 | if (tb[RTA_GATEWAY]) |
| 791 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 792 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 793 | if (tb[RTA_METRICS]) |
| 794 | { |
| 795 | struct rtattr *mxrta[RTAX_MAX+1]; |
| 796 | |
| 797 | memset (mxrta, 0, sizeof mxrta); |
| 798 | netlink_parse_rtattr (mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), |
| 799 | RTA_PAYLOAD(tb[RTA_METRICS])); |
| 800 | |
| 801 | if (mxrta[RTAX_MTU]) |
| 802 | mtu = *(u_int32_t *) RTA_DATA(mxrta[RTAX_MTU]); |
| 803 | } |
| 804 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 805 | if (rtm->rtm_family == AF_INET) |
| 806 | { |
| 807 | struct prefix_ipv4 p; |
| 808 | p.family = AF_INET; |
| 809 | memcpy (&p.prefix, dest, 4); |
| 810 | p.prefixlen = rtm->rtm_dst_len; |
| 811 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 812 | if (!tb[RTA_MULTIPATH]) |
| 813 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index, |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 814 | vrf_id, table, 0, mtu, 0, SAFI_UNICAST); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 815 | else |
| 816 | { |
| 817 | /* This is a multipath route */ |
| 818 | |
| 819 | struct rib *rib; |
| 820 | struct rtnexthop *rtnh = |
| 821 | (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]); |
| 822 | |
| 823 | len = RTA_PAYLOAD (tb[RTA_MULTIPATH]); |
| 824 | |
| 825 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 826 | rib->type = ZEBRA_ROUTE_KERNEL; |
| 827 | rib->distance = 0; |
| 828 | rib->flags = flags; |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 829 | rib->metric = 0; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 830 | rib->mtu = mtu; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 831 | rib->vrf_id = vrf_id; |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 832 | rib->table = table; |
| 833 | rib->nexthop_num = 0; |
| 834 | rib->uptime = time (NULL); |
| 835 | |
| 836 | for (;;) |
| 837 | { |
| 838 | if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len) |
| 839 | break; |
| 840 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 841 | index = rtnh->rtnh_ifindex; |
| 842 | gate = 0; |
| 843 | if (rtnh->rtnh_len > sizeof (*rtnh)) |
| 844 | { |
| 845 | memset (tb, 0, sizeof (tb)); |
| 846 | netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh), |
| 847 | rtnh->rtnh_len - sizeof (*rtnh)); |
| 848 | if (tb[RTA_GATEWAY]) |
| 849 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 850 | } |
| 851 | |
| 852 | if (gate) |
| 853 | { |
| 854 | if (index) |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 855 | rib_nexthop_ipv4_ifindex_add (rib, gate, src, index); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 856 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 857 | rib_nexthop_ipv4_add (rib, gate, src); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 858 | } |
| 859 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 860 | rib_nexthop_ifindex_add (rib, index); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 861 | |
| 862 | len -= NLMSG_ALIGN(rtnh->rtnh_len); |
| 863 | rtnh = RTNH_NEXT(rtnh); |
| 864 | } |
| 865 | |
| 866 | if (rib->nexthop_num == 0) |
| 867 | XFREE (MTYPE_RIB, rib); |
| 868 | else |
Avneesh Sachdev | 14d2bba | 2012-04-11 23:51:08 -0700 | [diff] [blame] | 869 | rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 870 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 871 | } |
| 872 | #ifdef HAVE_IPV6 |
| 873 | if (rtm->rtm_family == AF_INET6) |
| 874 | { |
| 875 | struct prefix_ipv6 p; |
| 876 | p.family = AF_INET6; |
| 877 | memcpy (&p.prefix, dest, 16); |
| 878 | p.prefixlen = rtm->rtm_dst_len; |
| 879 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 880 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, vrf_id, |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 881 | table, 0, mtu, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 882 | } |
| 883 | #endif /* HAVE_IPV6 */ |
| 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 888 | static const struct message rtproto_str[] = { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 889 | {RTPROT_REDIRECT, "redirect"}, |
| 890 | {RTPROT_KERNEL, "kernel"}, |
| 891 | {RTPROT_BOOT, "boot"}, |
| 892 | {RTPROT_STATIC, "static"}, |
| 893 | {RTPROT_GATED, "GateD"}, |
| 894 | {RTPROT_RA, "router advertisement"}, |
| 895 | {RTPROT_MRT, "MRT"}, |
| 896 | {RTPROT_ZEBRA, "Zebra"}, |
| 897 | #ifdef RTPROT_BIRD |
| 898 | {RTPROT_BIRD, "BIRD"}, |
| 899 | #endif /* RTPROT_BIRD */ |
| 900 | {0, NULL} |
| 901 | }; |
| 902 | |
| 903 | /* Routing information change from the kernel. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 904 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 905 | netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 906 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 907 | { |
| 908 | int len; |
| 909 | struct rtmsg *rtm; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 910 | struct rtattr *tb[RTA_MAX + 1]; |
| 911 | |
| 912 | char anyaddr[16] = { 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 913 | |
| 914 | int index; |
| 915 | int table; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 916 | u_int32_t mtu = 0; |
Dmitry Popov | 83d1614 | 2011-09-11 13:48:25 +0400 | [diff] [blame] | 917 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 918 | void *dest; |
| 919 | void *gate; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 920 | void *src; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 921 | |
| 922 | rtm = NLMSG_DATA (h); |
| 923 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 924 | if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 925 | { |
| 926 | /* If this is not route add/delete message print warning. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 927 | zlog_warn ("Kernel message: %d vrf %u\n", h->nlmsg_type, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | /* Connected route. */ |
| 932 | if (IS_ZEBRA_DEBUG_KERNEL) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 933 | zlog_debug ("%s %s %s proto %s vrf %u", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 934 | h->nlmsg_type == |
| 935 | RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE", |
| 936 | rtm->rtm_family == AF_INET ? "ipv4" : "ipv6", |
| 937 | rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast", |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 938 | lookup (rtproto_str, rtm->rtm_protocol), |
| 939 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 940 | |
| 941 | if (rtm->rtm_type != RTN_UNICAST) |
| 942 | { |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | table = rtm->rtm_table; |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 947 | if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 948 | { |
| 949 | return 0; |
| 950 | } |
| 951 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 952 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 953 | if (len < 0) |
| 954 | return -1; |
| 955 | |
| 956 | memset (tb, 0, sizeof tb); |
| 957 | netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len); |
| 958 | |
| 959 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 960 | return 0; |
| 961 | if (rtm->rtm_protocol == RTPROT_REDIRECT) |
| 962 | return 0; |
| 963 | if (rtm->rtm_protocol == RTPROT_KERNEL) |
| 964 | return 0; |
| 965 | |
| 966 | if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE) |
| 967 | return 0; |
| 968 | |
| 969 | if (rtm->rtm_src_len != 0) |
| 970 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 971 | zlog_warn ("netlink_route_change(): no src len, vrf %u", vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 972 | return 0; |
| 973 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 974 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 975 | index = 0; |
| 976 | dest = NULL; |
| 977 | gate = NULL; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 978 | src = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 979 | |
| 980 | if (tb[RTA_OIF]) |
| 981 | index = *(int *) RTA_DATA (tb[RTA_OIF]); |
| 982 | |
| 983 | if (tb[RTA_DST]) |
| 984 | dest = RTA_DATA (tb[RTA_DST]); |
| 985 | else |
| 986 | dest = anyaddr; |
| 987 | |
| 988 | if (tb[RTA_GATEWAY]) |
| 989 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 990 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 991 | if (tb[RTA_PREFSRC]) |
| 992 | src = RTA_DATA (tb[RTA_PREFSRC]); |
| 993 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 994 | if (h->nlmsg_type == RTM_NEWROUTE) |
| 995 | { |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 996 | if (tb[RTA_METRICS]) |
| 997 | { |
| 998 | struct rtattr *mxrta[RTAX_MAX+1]; |
| 999 | |
| 1000 | memset (mxrta, 0, sizeof mxrta); |
| 1001 | netlink_parse_rtattr (mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), |
| 1002 | RTA_PAYLOAD(tb[RTA_METRICS])); |
| 1003 | |
| 1004 | if (mxrta[RTAX_MTU]) |
| 1005 | mtu = *(u_int32_t *) RTA_DATA(mxrta[RTAX_MTU]); |
| 1006 | } |
| 1007 | } |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1008 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1009 | if (rtm->rtm_family == AF_INET) |
| 1010 | { |
| 1011 | struct prefix_ipv4 p; |
| 1012 | p.family = AF_INET; |
| 1013 | memcpy (&p.prefix, dest, 4); |
| 1014 | p.prefixlen = rtm->rtm_dst_len; |
| 1015 | |
| 1016 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1017 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1018 | char buf[PREFIX_STRLEN]; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1019 | zlog_debug ("%s %s vrf %u", |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1020 | h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE", |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1021 | prefix2str (&p, buf, sizeof(buf)), vrf_id); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1022 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1023 | |
| 1024 | if (h->nlmsg_type == RTM_NEWROUTE) |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1025 | { |
| 1026 | if (!tb[RTA_MULTIPATH]) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1027 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, vrf_id, |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1028 | table, 0, mtu, 0, SAFI_UNICAST); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1029 | else |
| 1030 | { |
| 1031 | /* This is a multipath route */ |
| 1032 | |
| 1033 | struct rib *rib; |
| 1034 | struct rtnexthop *rtnh = |
| 1035 | (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]); |
| 1036 | |
| 1037 | len = RTA_PAYLOAD (tb[RTA_MULTIPATH]); |
| 1038 | |
| 1039 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 1040 | rib->type = ZEBRA_ROUTE_KERNEL; |
| 1041 | rib->distance = 0; |
| 1042 | rib->flags = 0; |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1043 | rib->metric = 0; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1044 | rib->mtu = mtu; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1045 | rib->vrf_id = vrf_id; |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1046 | rib->table = table; |
| 1047 | rib->nexthop_num = 0; |
| 1048 | rib->uptime = time (NULL); |
| 1049 | |
| 1050 | for (;;) |
| 1051 | { |
| 1052 | if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len) |
| 1053 | break; |
| 1054 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1055 | index = rtnh->rtnh_ifindex; |
| 1056 | gate = 0; |
| 1057 | if (rtnh->rtnh_len > sizeof (*rtnh)) |
| 1058 | { |
| 1059 | memset (tb, 0, sizeof (tb)); |
| 1060 | netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh), |
| 1061 | rtnh->rtnh_len - sizeof (*rtnh)); |
| 1062 | if (tb[RTA_GATEWAY]) |
| 1063 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 1064 | } |
| 1065 | |
| 1066 | if (gate) |
| 1067 | { |
| 1068 | if (index) |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1069 | rib_nexthop_ipv4_ifindex_add (rib, gate, src, index); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1070 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1071 | rib_nexthop_ipv4_add (rib, gate, src); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1072 | } |
| 1073 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1074 | rib_nexthop_ifindex_add (rib, index); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1075 | |
| 1076 | len -= NLMSG_ALIGN(rtnh->rtnh_len); |
| 1077 | rtnh = RTNH_NEXT(rtnh); |
| 1078 | } |
| 1079 | |
| 1080 | if (rib->nexthop_num == 0) |
| 1081 | XFREE (MTYPE_RIB, rib); |
| 1082 | else |
Avneesh Sachdev | 14d2bba | 2012-04-11 23:51:08 -0700 | [diff] [blame] | 1083 | rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1084 | } |
| 1085 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1086 | else |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1087 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1088 | SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | #ifdef HAVE_IPV6 |
| 1092 | if (rtm->rtm_family == AF_INET6) |
| 1093 | { |
| 1094 | struct prefix_ipv6 p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1095 | |
| 1096 | p.family = AF_INET6; |
| 1097 | memcpy (&p.prefix, dest, 16); |
| 1098 | p.prefixlen = rtm->rtm_dst_len; |
| 1099 | |
| 1100 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1101 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1102 | char buf[PREFIX_STRLEN]; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1103 | zlog_debug ("%s %s vrf %u", |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1104 | h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE", |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1105 | prefix2str (&p, buf, sizeof(buf)), vrf_id); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1106 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (h->nlmsg_type == RTM_NEWROUTE) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1109 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, vrf_id, table, |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1110 | 0, mtu, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1111 | else |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1112 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1113 | SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1114 | } |
| 1115 | #endif /* HAVE_IPV6 */ |
| 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1120 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1121 | netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 1122 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1123 | { |
| 1124 | int len; |
| 1125 | struct ifinfomsg *ifi; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1126 | struct rtattr *tb[IFLA_MAX + 1]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1127 | struct interface *ifp; |
| 1128 | char *name; |
| 1129 | |
| 1130 | ifi = NLMSG_DATA (h); |
| 1131 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1132 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | { |
| 1134 | /* If this is not link add/delete message so print warning. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1135 | zlog_warn ("netlink_link_change: wrong kernel message %d vrf %u\n", |
| 1136 | h->nlmsg_type, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); |
| 1141 | if (len < 0) |
| 1142 | return -1; |
| 1143 | |
| 1144 | /* Looking up interface name. */ |
| 1145 | memset (tb, 0, sizeof tb); |
| 1146 | netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len); |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1147 | |
paul | 1e19315 | 2005-02-14 23:53:05 +0000 | [diff] [blame] | 1148 | #ifdef IFLA_WIRELESS |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1149 | /* check for wireless messages to ignore */ |
| 1150 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) |
| 1151 | { |
| 1152 | if (IS_ZEBRA_DEBUG_KERNEL) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1153 | zlog_debug ("%s: ignoring IFLA_WIRELESS message, vrf %u", __func__, |
| 1154 | vrf_id); |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
paul | 1e19315 | 2005-02-14 23:53:05 +0000 | [diff] [blame] | 1157 | #endif /* IFLA_WIRELESS */ |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1158 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1159 | if (tb[IFLA_IFNAME] == NULL) |
| 1160 | return -1; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1161 | name = (char *) RTA_DATA (tb[IFLA_IFNAME]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* Add interface. */ |
| 1164 | if (h->nlmsg_type == RTM_NEWLINK) |
| 1165 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1166 | ifp = if_lookup_by_name_vrf (name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1168 | if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 1169 | { |
| 1170 | if (ifp == NULL) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1171 | ifp = if_get_by_name_vrf (name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1172 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 1173 | set_ifindex(ifp, ifi->ifi_index); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1174 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 1175 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); |
Brett Ciphery | db19c85 | 2013-10-03 13:48:54 +0000 | [diff] [blame] | 1176 | ifp->metric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1177 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1178 | netlink_interface_update_hw_addr (tb, ifp); |
| 1179 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1180 | /* If new link is added. */ |
| 1181 | if_add_update (ifp); |
| 1182 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1183 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1184 | { |
| 1185 | /* Interface status change. */ |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 1186 | set_ifindex(ifp, ifi->ifi_index); |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 1187 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); |
Brett Ciphery | db19c85 | 2013-10-03 13:48:54 +0000 | [diff] [blame] | 1188 | ifp->metric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1189 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1190 | netlink_interface_update_hw_addr (tb, ifp); |
| 1191 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1192 | if (if_is_operative (ifp)) |
| 1193 | { |
| 1194 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
| 1195 | if (!if_is_operative (ifp)) |
| 1196 | if_down (ifp); |
ajs | a608bbf | 2005-03-29 17:03:49 +0000 | [diff] [blame] | 1197 | else |
| 1198 | /* Must notify client daemons of new interface status. */ |
| 1199 | zebra_interface_up_update (ifp); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1200 | } |
| 1201 | else |
| 1202 | { |
| 1203 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
| 1204 | if (if_is_operative (ifp)) |
| 1205 | if_up (ifp); |
| 1206 | } |
| 1207 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1208 | } |
| 1209 | else |
| 1210 | { |
| 1211 | /* RTM_DELLINK. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1212 | ifp = if_lookup_by_name_vrf (name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1213 | |
| 1214 | if (ifp == NULL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1215 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1216 | zlog_warn ("interface %s vrf %u is deleted but can't find", |
| 1217 | name, vrf_id); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1221 | if_delete_update (ifp); |
| 1222 | } |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1227 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1228 | netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 1229 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1230 | { |
Stephen Hemminger | 3d265b4 | 2008-08-16 17:30:39 +0100 | [diff] [blame] | 1231 | /* JF: Ignore messages that aren't from the kernel */ |
| 1232 | if ( snl->nl_pid != 0 ) |
| 1233 | { |
| 1234 | zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl->nl_pid ); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1238 | switch (h->nlmsg_type) |
| 1239 | { |
| 1240 | case RTM_NEWROUTE: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1241 | return netlink_route_change (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | case RTM_DELROUTE: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1244 | return netlink_route_change (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1245 | break; |
| 1246 | case RTM_NEWLINK: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1247 | return netlink_link_change (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1248 | break; |
| 1249 | case RTM_DELLINK: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1250 | return netlink_link_change (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | case RTM_NEWADDR: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1253 | return netlink_interface_addr (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | case RTM_DELADDR: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1256 | return netlink_interface_addr (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1257 | break; |
| 1258 | default: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1259 | zlog_warn ("Unknown netlink nlmsg_type %d vrf %u\n", h->nlmsg_type, |
| 1260 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1261 | break; |
| 1262 | } |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | /* Interface lookup by netlink socket. */ |
| 1267 | int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1268 | interface_lookup_netlink (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1269 | { |
| 1270 | int ret; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1271 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1272 | /* Get interface information. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1273 | ret = netlink_request (AF_PACKET, RTM_GETLINK, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1274 | if (ret < 0) |
| 1275 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1276 | ret = netlink_parse_info (netlink_interface, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1277 | if (ret < 0) |
| 1278 | return ret; |
| 1279 | |
| 1280 | /* Get IPv4 address of the interfaces. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1281 | ret = netlink_request (AF_INET, RTM_GETADDR, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1282 | if (ret < 0) |
| 1283 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1284 | ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1285 | if (ret < 0) |
| 1286 | return ret; |
| 1287 | |
| 1288 | #ifdef HAVE_IPV6 |
| 1289 | /* Get IPv6 address of the interfaces. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1290 | ret = netlink_request (AF_INET6, RTM_GETADDR, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1291 | if (ret < 0) |
| 1292 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1293 | ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1294 | if (ret < 0) |
| 1295 | return ret; |
| 1296 | #endif /* HAVE_IPV6 */ |
| 1297 | |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* Routing table read function using netlink interface. Only called |
| 1302 | bootstrap time. */ |
| 1303 | int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1304 | netlink_route_read (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1305 | { |
| 1306 | int ret; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1307 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1308 | /* Get IPv4 routing table. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1309 | ret = netlink_request (AF_INET, RTM_GETROUTE, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1310 | if (ret < 0) |
| 1311 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1312 | ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1313 | if (ret < 0) |
| 1314 | return ret; |
| 1315 | |
| 1316 | #ifdef HAVE_IPV6 |
| 1317 | /* Get IPv6 routing table. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1318 | ret = netlink_request (AF_INET6, RTM_GETROUTE, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1319 | if (ret < 0) |
| 1320 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1321 | ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1322 | if (ret < 0) |
| 1323 | return ret; |
| 1324 | #endif /* HAVE_IPV6 */ |
| 1325 | |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | /* Utility function comes from iproute2. |
| 1330 | Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 1331 | int |
Paul Jakma | 6f23541 | 2016-04-05 13:21:20 +0100 | [diff] [blame] | 1332 | addattr_l (struct nlmsghdr *n, size_t maxlen, int type, void *data, size_t alen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1333 | { |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1334 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1335 | struct rtattr *rta; |
| 1336 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1337 | len = RTA_LENGTH (alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1338 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1339 | if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1340 | return -1; |
| 1341 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1342 | rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1343 | rta->rta_type = type; |
| 1344 | rta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1345 | memcpy (RTA_DATA (rta), data, alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1346 | n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 1351 | int |
Paul Jakma | 6f23541 | 2016-04-05 13:21:20 +0100 | [diff] [blame] | 1352 | rta_addattr_l (struct rtattr *rta, size_t maxlen, int type, void *data, |
| 1353 | size_t alen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1354 | { |
Paul Jakma | 6f23541 | 2016-04-05 13:21:20 +0100 | [diff] [blame] | 1355 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1356 | struct rtattr *subrta; |
| 1357 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1358 | len = RTA_LENGTH (alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1359 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1360 | if (RTA_ALIGN (rta->rta_len) + len > maxlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1361 | return -1; |
| 1362 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1363 | subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1364 | subrta->rta_type = type; |
| 1365 | subrta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1366 | memcpy (RTA_DATA (subrta), data, alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1367 | rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len; |
| 1368 | |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | /* Utility function comes from iproute2. |
| 1373 | Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 1374 | int |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1375 | addattr32 (struct nlmsghdr *n, size_t maxlen, int type, int data) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1376 | { |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1377 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1378 | struct rtattr *rta; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1379 | |
| 1380 | len = RTA_LENGTH (4); |
| 1381 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1382 | if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) |
| 1383 | return -1; |
| 1384 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1385 | rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1386 | rta->rta_type = type; |
| 1387 | rta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1388 | memcpy (RTA_DATA (rta), &data, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1389 | n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; |
| 1390 | |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1395 | netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 1396 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1397 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1398 | zlog_warn ("netlink_talk: ignoring message type 0x%04x vrf %u", h->nlmsg_type, |
| 1399 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | /* sendmsg() to netlink socket then recvmsg(). */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1404 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1405 | netlink_talk (struct nlmsghdr *n, struct nlsock *nl, struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1406 | { |
| 1407 | int status; |
| 1408 | struct sockaddr_nl snl; |
Timo Teräs | c299ed7 | 2014-07-29 09:41:55 +0000 | [diff] [blame] | 1409 | struct iovec iov = { |
| 1410 | .iov_base = (void *) n, |
| 1411 | .iov_len = n->nlmsg_len |
| 1412 | }; |
| 1413 | struct msghdr msg = { |
| 1414 | .msg_name = (void *) &snl, |
| 1415 | .msg_namelen = sizeof snl, |
| 1416 | .msg_iov = &iov, |
| 1417 | .msg_iovlen = 1, |
| 1418 | }; |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 1419 | int save_errno; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1420 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1421 | memset (&snl, 0, sizeof snl); |
| 1422 | snl.nl_family = AF_NETLINK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1423 | |
hasso | b7ed1ec | 2005-03-31 20:13:49 +0000 | [diff] [blame] | 1424 | n->nlmsg_seq = ++nl->seq; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1425 | |
| 1426 | /* Request an acknowledgement by setting NLM_F_ACK */ |
| 1427 | n->nlmsg_flags |= NLM_F_ACK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1428 | |
| 1429 | if (IS_ZEBRA_DEBUG_KERNEL) |
hasso | b7ed1ec | 2005-03-31 20:13:49 +0000 | [diff] [blame] | 1430 | zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl->name, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1431 | lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type, |
| 1432 | n->nlmsg_seq); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1433 | |
| 1434 | /* Send message to netlink interface. */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1435 | if (zserv_privs.change (ZPRIVS_RAISE)) |
| 1436 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1437 | status = sendmsg (nl->sock, &msg, 0); |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 1438 | save_errno = errno; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1439 | if (zserv_privs.change (ZPRIVS_LOWER)) |
| 1440 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 1441 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1442 | if (status < 0) |
| 1443 | { |
| 1444 | zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s", |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 1445 | safe_strerror (save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1446 | return -1; |
| 1447 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1448 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1449 | |
| 1450 | /* |
| 1451 | * Get reply from netlink socket. |
| 1452 | * The reply should either be an acknowlegement or an error. |
| 1453 | */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1454 | return netlink_parse_info (netlink_talk_filter, nl, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1457 | /* This function takes a nexthop as argument and adds |
| 1458 | * the appropriate netlink attributes to an existing |
| 1459 | * netlink message. |
| 1460 | * |
| 1461 | * @param routedesc: Human readable description of route type |
| 1462 | * (direct/recursive, single-/multipath) |
| 1463 | * @param bytelen: Length of addresses in bytes. |
| 1464 | * @param nexthop: Nexthop information |
| 1465 | * @param nlmsg: nlmsghdr structure to fill in. |
| 1466 | * @param req_size: The size allocated for the message. |
| 1467 | */ |
| 1468 | static void |
| 1469 | _netlink_route_build_singlepath( |
| 1470 | const char *routedesc, |
| 1471 | int bytelen, |
| 1472 | struct nexthop *nexthop, |
| 1473 | struct nlmsghdr *nlmsg, |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1474 | struct rtmsg *rtmsg, |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1475 | size_t req_size) |
| 1476 | { |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1477 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK)) |
| 1478 | rtmsg->rtm_flags |= RTNH_F_ONLINK; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1479 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 1480 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1481 | { |
| 1482 | addattr_l (nlmsg, req_size, RTA_GATEWAY, |
| 1483 | &nexthop->gate.ipv4, bytelen); |
| 1484 | if (nexthop->src.ipv4.s_addr) |
| 1485 | addattr_l (nlmsg, req_size, RTA_PREFSRC, |
| 1486 | &nexthop->src.ipv4, bytelen); |
| 1487 | |
| 1488 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1489 | zlog_debug("netlink_route_multipath() (%s): " |
| 1490 | "nexthop via %s if %u", |
| 1491 | routedesc, |
| 1492 | inet_ntoa (nexthop->gate.ipv4), |
| 1493 | nexthop->ifindex); |
| 1494 | } |
| 1495 | #ifdef HAVE_IPV6 |
| 1496 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 1497 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1498 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1499 | { |
| 1500 | addattr_l (nlmsg, req_size, RTA_GATEWAY, |
| 1501 | &nexthop->gate.ipv6, bytelen); |
| 1502 | |
| 1503 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1504 | zlog_debug("netlink_route_multipath() (%s): " |
| 1505 | "nexthop via %s if %u", |
| 1506 | routedesc, |
| 1507 | inet6_ntoa (nexthop->gate.ipv6), |
| 1508 | nexthop->ifindex); |
| 1509 | } |
| 1510 | #endif /* HAVE_IPV6 */ |
| 1511 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1512 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 1513 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1514 | { |
| 1515 | addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex); |
| 1516 | |
| 1517 | if (nexthop->src.ipv4.s_addr) |
| 1518 | addattr_l (nlmsg, req_size, RTA_PREFSRC, |
| 1519 | &nexthop->src.ipv4, bytelen); |
| 1520 | |
| 1521 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1522 | zlog_debug("netlink_route_multipath() (%s): " |
| 1523 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1524 | } |
| 1525 | |
| 1526 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 1527 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
| 1528 | { |
| 1529 | addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex); |
| 1530 | |
| 1531 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1532 | zlog_debug("netlink_route_multipath() (%s): " |
| 1533 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | /* This function takes a nexthop as argument and |
| 1538 | * appends to the given rtattr/rtnexthop pair the |
| 1539 | * representation of the nexthop. If the nexthop |
| 1540 | * defines a preferred source, the src parameter |
| 1541 | * will be modified to point to that src, otherwise |
| 1542 | * it will be kept unmodified. |
| 1543 | * |
| 1544 | * @param routedesc: Human readable description of route type |
| 1545 | * (direct/recursive, single-/multipath) |
| 1546 | * @param bytelen: Length of addresses in bytes. |
| 1547 | * @param nexthop: Nexthop information |
| 1548 | * @param rta: rtnetlink attribute structure |
| 1549 | * @param rtnh: pointer to an rtnetlink nexthop structure |
| 1550 | * @param src: pointer pointing to a location where |
| 1551 | * the prefsrc should be stored. |
| 1552 | */ |
| 1553 | static void |
| 1554 | _netlink_route_build_multipath( |
| 1555 | const char *routedesc, |
| 1556 | int bytelen, |
| 1557 | struct nexthop *nexthop, |
| 1558 | struct rtattr *rta, |
| 1559 | struct rtnexthop *rtnh, |
| 1560 | union g_addr **src |
| 1561 | ) |
| 1562 | { |
| 1563 | rtnh->rtnh_len = sizeof (*rtnh); |
| 1564 | rtnh->rtnh_flags = 0; |
| 1565 | rtnh->rtnh_hops = 0; |
| 1566 | rta->rta_len += rtnh->rtnh_len; |
| 1567 | |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1568 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK)) |
| 1569 | rtnh->rtnh_flags |= RTNH_F_ONLINK; |
| 1570 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1571 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 1572 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1573 | { |
| 1574 | rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY, |
| 1575 | &nexthop->gate.ipv4, bytelen); |
Lu Feng | 621e2aa | 2014-07-11 07:52:15 +0000 | [diff] [blame] | 1576 | rtnh->rtnh_len += sizeof (struct rtattr) + bytelen; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1577 | |
| 1578 | if (nexthop->src.ipv4.s_addr) |
| 1579 | *src = &nexthop->src; |
| 1580 | |
| 1581 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1582 | zlog_debug("netlink_route_multipath() (%s): " |
| 1583 | "nexthop via %s if %u", |
| 1584 | routedesc, |
| 1585 | inet_ntoa (nexthop->gate.ipv4), |
| 1586 | nexthop->ifindex); |
| 1587 | } |
| 1588 | #ifdef HAVE_IPV6 |
| 1589 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 1590 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1591 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1592 | { |
| 1593 | rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY, |
| 1594 | &nexthop->gate.ipv6, bytelen); |
Lu Feng | 621e2aa | 2014-07-11 07:52:15 +0000 | [diff] [blame] | 1595 | rtnh->rtnh_len += sizeof (struct rtattr) + bytelen; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1596 | |
| 1597 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1598 | zlog_debug("netlink_route_multipath() (%s): " |
| 1599 | "nexthop via %s if %u", |
| 1600 | routedesc, |
| 1601 | inet6_ntoa (nexthop->gate.ipv6), |
| 1602 | nexthop->ifindex); |
| 1603 | } |
| 1604 | #endif /* HAVE_IPV6 */ |
| 1605 | /* ifindex */ |
| 1606 | if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX |
| 1607 | || nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1608 | || nexthop->type == NEXTHOP_TYPE_IFNAME) |
| 1609 | { |
| 1610 | rtnh->rtnh_ifindex = nexthop->ifindex; |
| 1611 | if (nexthop->src.ipv4.s_addr) |
| 1612 | *src = &nexthop->src; |
| 1613 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1614 | zlog_debug("netlink_route_multipath() (%s): " |
| 1615 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1616 | } |
| 1617 | else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1618 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1619 | { |
| 1620 | rtnh->rtnh_ifindex = nexthop->ifindex; |
| 1621 | |
| 1622 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1623 | zlog_debug("netlink_route_multipath() (%s): " |
| 1624 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1625 | } |
| 1626 | else |
| 1627 | { |
| 1628 | rtnh->rtnh_ifindex = 0; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | /* Log debug information for netlink_route_multipath |
| 1633 | * if debug logging is enabled. |
| 1634 | * |
| 1635 | * @param cmd: Netlink command which is to be processed |
| 1636 | * @param p: Prefix for which the change is due |
| 1637 | * @param nexthop: Nexthop which is currently processed |
| 1638 | * @param routedesc: Semantic annotation for nexthop |
| 1639 | * (recursive, multipath, etc.) |
| 1640 | * @param family: Address family which the change concerns |
| 1641 | */ |
| 1642 | static void |
| 1643 | _netlink_route_debug( |
| 1644 | int cmd, |
| 1645 | struct prefix *p, |
| 1646 | struct nexthop *nexthop, |
| 1647 | const char *routedesc, |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1648 | int family, |
| 1649 | struct zebra_vrf *zvrf) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1650 | { |
| 1651 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1652 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1653 | char buf[PREFIX_STRLEN]; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1654 | zlog_debug ("netlink_route_multipath() (%s): %s %s vrf %u type %s", |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1655 | routedesc, |
| 1656 | lookup (nlmsg_str, cmd), |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1657 | prefix2str (p, buf, sizeof(buf)), |
Kaloyan Kovachev | 8c56b44 | 2015-06-15 17:08:48 +0300 | [diff] [blame] | 1658 | zvrf->vrf_id, |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1659 | nexthop_type_to_str (nexthop->type)); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1663 | /* Routing table change via netlink interface. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1664 | static int |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1665 | netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1666 | { |
| 1667 | int bytelen; |
| 1668 | struct sockaddr_nl snl; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1669 | struct nexthop *nexthop = NULL, *tnexthop; |
| 1670 | int recursing; |
| 1671 | int nexthop_num; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1672 | int discard; |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1673 | int family = PREFIX_FAMILY(p); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1674 | const char *routedesc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1675 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1676 | struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1677 | { |
| 1678 | struct nlmsghdr n; |
| 1679 | struct rtmsg r; |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1680 | char buf[NL_PKT_BUF_SIZE]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1681 | } req; |
| 1682 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1683 | struct zebra_vrf *zvrf = vrf_info_lookup (rib->vrf_id); |
| 1684 | |
Jorge Boncompte [DTI2] | a4c06de | 2012-04-20 14:28:40 +0200 | [diff] [blame] | 1685 | memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1686 | |
| 1687 | bytelen = (family == AF_INET ? 4 : 16); |
| 1688 | |
| 1689 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1690 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REPLACE | NLM_F_REQUEST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1691 | req.n.nlmsg_type = cmd; |
| 1692 | req.r.rtm_family = family; |
| 1693 | req.r.rtm_table = rib->table; |
| 1694 | req.r.rtm_dst_len = p->prefixlen; |
Timo Teräs | 40da221 | 2008-08-13 17:37:14 +0100 | [diff] [blame] | 1695 | req.r.rtm_protocol = RTPROT_ZEBRA; |
Timo Teräs | 82a6635 | 2016-01-15 17:36:30 +0200 | [diff] [blame] | 1696 | req.r.rtm_scope = RT_SCOPE_LINK; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1697 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1698 | if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1699 | discard = 1; |
| 1700 | else |
| 1701 | discard = 0; |
| 1702 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1703 | if (cmd == RTM_NEWROUTE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1704 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1705 | if (discard) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1706 | { |
| 1707 | if (rib->flags & ZEBRA_FLAG_BLACKHOLE) |
| 1708 | req.r.rtm_type = RTN_BLACKHOLE; |
| 1709 | else if (rib->flags & ZEBRA_FLAG_REJECT) |
| 1710 | req.r.rtm_type = RTN_UNREACHABLE; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1711 | else |
| 1712 | assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */ |
| 1713 | } |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1714 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1715 | req.r.rtm_type = RTN_UNICAST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen); |
| 1719 | |
| 1720 | /* Metric. */ |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1721 | addattr32 (&req.n, sizeof req, RTA_PRIORITY, NL_DEFAULT_ROUTE_METRIC); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1722 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1723 | if (rib->mtu || rib->nexthop_mtu) |
| 1724 | { |
| 1725 | char buf[NL_PKT_BUF_SIZE]; |
| 1726 | struct rtattr *rta = (void *) buf; |
| 1727 | u_int32_t mtu = rib->mtu; |
| 1728 | if (!mtu || (rib->nexthop_mtu && rib->nexthop_mtu < mtu)) |
| 1729 | mtu = rib->nexthop_mtu; |
| 1730 | rta->rta_type = RTA_METRICS; |
| 1731 | rta->rta_len = RTA_LENGTH(0); |
| 1732 | rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTAX_MTU, &mtu, sizeof mtu); |
| 1733 | addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_METRICS, RTA_DATA (rta), |
| 1734 | RTA_PAYLOAD (rta)); |
| 1735 | } |
| 1736 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1737 | if (discard) |
| 1738 | { |
| 1739 | if (cmd == RTM_NEWROUTE) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1740 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 1741 | { |
| 1742 | /* We shouldn't encounter recursive nexthops on discard routes, |
| 1743 | * but it is probably better to handle that case correctly anyway. |
| 1744 | */ |
| 1745 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1746 | continue; |
| 1747 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1748 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1749 | goto skip; |
| 1750 | } |
| 1751 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1752 | /* Count overall nexthops so we can decide whether to use singlepath |
| 1753 | * or multipath case. */ |
| 1754 | nexthop_num = 0; |
| 1755 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1756 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1757 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1758 | continue; |
| 1759 | if (cmd == RTM_NEWROUTE && !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1760 | continue; |
| 1761 | if (cmd == RTM_DELROUTE && !CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 1762 | continue; |
| 1763 | |
Timo Teräs | 82a6635 | 2016-01-15 17:36:30 +0200 | [diff] [blame] | 1764 | if (nexthop->type != NEXTHOP_TYPE_IFINDEX && |
| 1765 | nexthop->type != NEXTHOP_TYPE_IFNAME) |
| 1766 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 1767 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1768 | nexthop_num++; |
| 1769 | } |
| 1770 | |
| 1771 | /* Singlepath case. */ |
| 1772 | if (nexthop_num == 1 || MULTIPATH_NUM == 1) |
| 1773 | { |
| 1774 | nexthop_num = 0; |
| 1775 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1776 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1777 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1778 | continue; |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1779 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1780 | if ((cmd == RTM_NEWROUTE |
| 1781 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1782 | || (cmd == RTM_DELROUTE |
| 1783 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))) |
| 1784 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1785 | routedesc = recursing ? "recursive, 1 hop" : "single hop"; |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1786 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1787 | _netlink_route_debug(cmd, p, nexthop, routedesc, family, zvrf); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1788 | _netlink_route_build_singlepath(routedesc, bytelen, |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1789 | nexthop, &req.n, &req.r, |
| 1790 | sizeof req); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1791 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1792 | if (cmd == RTM_NEWROUTE) |
| 1793 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1794 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1795 | nexthop_num++; |
| 1796 | break; |
| 1797 | } |
| 1798 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1799 | } |
| 1800 | else |
| 1801 | { |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1802 | char buf[NL_PKT_BUF_SIZE]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1803 | struct rtattr *rta = (void *) buf; |
| 1804 | struct rtnexthop *rtnh; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1805 | union g_addr *src = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1806 | |
| 1807 | rta->rta_type = RTA_MULTIPATH; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1808 | rta->rta_len = RTA_LENGTH (0); |
| 1809 | rtnh = RTA_DATA (rta); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1810 | |
| 1811 | nexthop_num = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1812 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1813 | { |
Donald Sharp | 91ce87a | 2015-12-09 08:24:45 -0500 | [diff] [blame] | 1814 | if (nexthop_num >= MULTIPATH_NUM) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1815 | break; |
| 1816 | |
| 1817 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1818 | continue; |
| 1819 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1820 | if ((cmd == RTM_NEWROUTE |
| 1821 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1822 | || (cmd == RTM_DELROUTE |
| 1823 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))) |
| 1824 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1825 | routedesc = recursing ? "recursive, multihop" : "multihop"; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1826 | nexthop_num++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1827 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1828 | _netlink_route_debug(cmd, p, nexthop, |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1829 | routedesc, family, zvrf); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1830 | _netlink_route_build_multipath(routedesc, bytelen, |
| 1831 | nexthop, rta, rtnh, &src); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1832 | rtnh = RTNH_NEXT (rtnh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1833 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1834 | if (cmd == RTM_NEWROUTE) |
| 1835 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1836 | } |
| 1837 | } |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1838 | if (src) |
| 1839 | addattr_l (&req.n, sizeof req, RTA_PREFSRC, &src->ipv4, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1840 | |
| 1841 | if (rta->rta_len > RTA_LENGTH (0)) |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1842 | addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH, RTA_DATA (rta), |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1843 | RTA_PAYLOAD (rta)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | /* If there is no useful nexthop then return. */ |
| 1847 | if (nexthop_num == 0) |
| 1848 | { |
| 1849 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1850 | zlog_debug ("netlink_route_multipath(): No useful nexthop."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1851 | return 0; |
| 1852 | } |
| 1853 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1854 | skip: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* Destination netlink address. */ |
| 1857 | memset (&snl, 0, sizeof snl); |
| 1858 | snl.nl_family = AF_NETLINK; |
| 1859 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1860 | /* Talk to netlink socket. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1861 | return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | int |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1865 | kernel_route_rib (struct prefix *p, struct rib *old, struct rib *new) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1866 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1867 | if (!old && new) |
| 1868 | return netlink_route_multipath (RTM_NEWROUTE, p, new); |
| 1869 | if (old && !new) |
| 1870 | return netlink_route_multipath (RTM_DELROUTE, p, old); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1871 | |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1872 | /* Replace, can be done atomically if metric does not change; |
| 1873 | * netlink uses [prefix, tos, priority] to identify prefix. |
| 1874 | * Now metric is not sent to kernel, so we can just do atomic replace. */ |
| 1875 | return netlink_route_multipath (RTM_NEWROUTE, p, new); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1876 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1877 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1878 | /* Interface address modification. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1879 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1880 | netlink_address (int cmd, int family, struct interface *ifp, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1881 | struct connected *ifc) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1882 | { |
| 1883 | int bytelen; |
| 1884 | struct prefix *p; |
| 1885 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1886 | struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1887 | { |
| 1888 | struct nlmsghdr n; |
| 1889 | struct ifaddrmsg ifa; |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1890 | char buf[NL_PKT_BUF_SIZE]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1891 | } req; |
| 1892 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1893 | struct zebra_vrf *zvrf = vrf_info_lookup (ifp->vrf_id); |
| 1894 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1895 | p = ifc->address; |
Jorge Boncompte [DTI2] | a4c06de | 2012-04-20 14:28:40 +0200 | [diff] [blame] | 1896 | memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1897 | |
| 1898 | bytelen = (family == AF_INET ? 4 : 16); |
| 1899 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1900 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1901 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 1902 | req.n.nlmsg_type = cmd; |
| 1903 | req.ifa.ifa_family = family; |
| 1904 | |
| 1905 | req.ifa.ifa_index = ifp->ifindex; |
| 1906 | req.ifa.ifa_prefixlen = p->prefixlen; |
| 1907 | |
| 1908 | addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen); |
| 1909 | |
| 1910 | if (family == AF_INET && cmd == RTM_NEWADDR) |
| 1911 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 1912 | if (!CONNECTED_PEER(ifc) && ifc->destination) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1913 | { |
| 1914 | p = ifc->destination; |
| 1915 | addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix, |
| 1916 | bytelen); |
| 1917 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY)) |
| 1921 | SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1922 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1923 | if (ifc->label) |
| 1924 | addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1925 | strlen (ifc->label) + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1926 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1927 | return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | int |
| 1931 | kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc) |
| 1932 | { |
| 1933 | return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc); |
| 1934 | } |
| 1935 | |
| 1936 | int |
| 1937 | kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc) |
| 1938 | { |
| 1939 | return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc); |
| 1940 | } |
| 1941 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1942 | |
| 1943 | extern struct thread_master *master; |
| 1944 | |
| 1945 | /* Kernel route reflection. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1946 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1947 | kernel_read (struct thread *thread) |
| 1948 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1949 | struct zebra_vrf *zvrf = (struct zebra_vrf *)THREAD_ARG (thread); |
| 1950 | netlink_parse_info (netlink_information_fetch, &zvrf->netlink, zvrf); |
| 1951 | zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf, |
| 1952 | zvrf->netlink.sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1953 | |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
Stephen Hemminger | 3d265b4 | 2008-08-16 17:30:39 +0100 | [diff] [blame] | 1957 | /* Filter out messages from self that occur on listener socket, |
| 1958 | caused by our actions on the command socket |
| 1959 | */ |
| 1960 | static void netlink_install_filter (int sock, __u32 pid) |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1961 | { |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1962 | struct sock_filter filter[] = { |
Stephen Hemminger | 3d265b4 | 2008-08-16 17:30:39 +0100 | [diff] [blame] | 1963 | /* 0: ldh [4] */ |
| 1964 | BPF_STMT(BPF_LD|BPF_ABS|BPF_H, offsetof(struct nlmsghdr, nlmsg_type)), |
| 1965 | /* 1: jeq 0x18 jt 3 jf 6 */ |
| 1966 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_NEWROUTE), 1, 0), |
| 1967 | /* 2: jeq 0x19 jt 3 jf 6 */ |
| 1968 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_DELROUTE), 0, 3), |
| 1969 | /* 3: ldw [12] */ |
| 1970 | BPF_STMT(BPF_LD|BPF_ABS|BPF_W, offsetof(struct nlmsghdr, nlmsg_pid)), |
| 1971 | /* 4: jeq XX jt 5 jf 6 */ |
| 1972 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htonl(pid), 0, 1), |
| 1973 | /* 5: ret 0 (skip) */ |
| 1974 | BPF_STMT(BPF_RET|BPF_K, 0), |
| 1975 | /* 6: ret 0xffff (keep) */ |
| 1976 | BPF_STMT(BPF_RET|BPF_K, 0xffff), |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1977 | }; |
| 1978 | |
| 1979 | struct sock_fprog prog = { |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 1980 | .len = array_size(filter), |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1981 | .filter = filter, |
| 1982 | }; |
| 1983 | |
| 1984 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) < 0) |
| 1985 | zlog_warn ("Can't install socket filter: %s\n", safe_strerror(errno)); |
| 1986 | } |
| 1987 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1988 | /* Exported interface function. This function simply calls |
| 1989 | netlink_socket (). */ |
| 1990 | void |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1991 | kernel_init (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1992 | { |
| 1993 | unsigned long groups; |
| 1994 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1995 | groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1996 | #ifdef HAVE_IPV6 |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1997 | groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1998 | #endif /* HAVE_IPV6 */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1999 | netlink_socket (&zvrf->netlink, groups, zvrf->vrf_id); |
| 2000 | netlink_socket (&zvrf->netlink_cmd, 0, zvrf->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2001 | |
| 2002 | /* Register kernel socket. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2003 | if (zvrf->netlink.sock > 0) |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 2004 | { |
Stephen Hemminger | 4cde931 | 2008-08-16 17:51:27 +0100 | [diff] [blame] | 2005 | /* Only want non-blocking on the netlink event socket */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2006 | if (fcntl (zvrf->netlink.sock, F_SETFL, O_NONBLOCK) < 0) |
| 2007 | zlog_err ("Can't set %s socket flags: %s", zvrf->netlink.name, |
| 2008 | safe_strerror (errno)); |
Stephen Hemminger | 4cde931 | 2008-08-16 17:51:27 +0100 | [diff] [blame] | 2009 | |
Stephen Hemminger | 30afea3 | 2008-08-16 18:25:47 +0100 | [diff] [blame] | 2010 | /* Set receive buffer size if it's set from command line */ |
| 2011 | if (nl_rcvbufsize) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2012 | netlink_recvbuf (&zvrf->netlink, nl_rcvbufsize); |
Stephen Hemminger | 30afea3 | 2008-08-16 18:25:47 +0100 | [diff] [blame] | 2013 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2014 | netlink_install_filter (zvrf->netlink.sock, zvrf->netlink_cmd.snl.nl_pid); |
| 2015 | zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf, |
| 2016 | zvrf->netlink.sock); |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | void |
| 2021 | kernel_terminate (struct zebra_vrf *zvrf) |
| 2022 | { |
| 2023 | THREAD_READ_OFF (zvrf->t_netlink); |
| 2024 | |
| 2025 | if (zvrf->netlink.sock >= 0) |
| 2026 | { |
| 2027 | close (zvrf->netlink.sock); |
| 2028 | zvrf->netlink.sock = -1; |
| 2029 | } |
| 2030 | |
| 2031 | if (zvrf->netlink_cmd.sock >= 0) |
| 2032 | { |
| 2033 | close (zvrf->netlink_cmd.sock); |
| 2034 | zvrf->netlink_cmd.sock = -1; |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 2035 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2036 | } |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 2037 | |
| 2038 | /* |
| 2039 | * nl_msg_type_to_str |
| 2040 | */ |
| 2041 | const char * |
| 2042 | nl_msg_type_to_str (uint16_t msg_type) |
| 2043 | { |
| 2044 | return lookup (nlmsg_str, msg_type); |
| 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | * nl_rtproto_to_str |
| 2049 | */ |
| 2050 | const char * |
| 2051 | nl_rtproto_to_str (u_char rtproto) |
| 2052 | { |
| 2053 | return lookup (rtproto_str, rtproto); |
| 2054 | } |