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]; |
James Li | 92992c6 | 2015-11-09 20:21:57 -0500 | [diff] [blame] | 911 | u_char zebra_flags = 0; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 912 | |
| 913 | char anyaddr[16] = { 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 914 | |
| 915 | int index; |
| 916 | int table; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 917 | u_int32_t mtu = 0; |
Dmitry Popov | 83d1614 | 2011-09-11 13:48:25 +0400 | [diff] [blame] | 918 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 919 | void *dest; |
| 920 | void *gate; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 921 | void *src; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 922 | |
| 923 | rtm = NLMSG_DATA (h); |
| 924 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 925 | if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 926 | { |
| 927 | /* If this is not route add/delete message print warning. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 928 | zlog_warn ("Kernel message: %d vrf %u\n", h->nlmsg_type, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | /* Connected route. */ |
| 933 | if (IS_ZEBRA_DEBUG_KERNEL) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 934 | zlog_debug ("%s %s %s proto %s vrf %u", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 935 | h->nlmsg_type == |
| 936 | RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE", |
| 937 | rtm->rtm_family == AF_INET ? "ipv4" : "ipv6", |
| 938 | rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast", |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 939 | lookup (rtproto_str, rtm->rtm_protocol), |
| 940 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 941 | |
| 942 | if (rtm->rtm_type != RTN_UNICAST) |
| 943 | { |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | table = rtm->rtm_table; |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 948 | if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 949 | { |
| 950 | return 0; |
| 951 | } |
| 952 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 953 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 954 | if (len < 0) |
| 955 | return -1; |
| 956 | |
| 957 | memset (tb, 0, sizeof tb); |
| 958 | netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len); |
| 959 | |
| 960 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 961 | return 0; |
| 962 | if (rtm->rtm_protocol == RTPROT_REDIRECT) |
| 963 | return 0; |
| 964 | if (rtm->rtm_protocol == RTPROT_KERNEL) |
| 965 | return 0; |
| 966 | |
| 967 | if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE) |
| 968 | return 0; |
James Li | 92992c6 | 2015-11-09 20:21:57 -0500 | [diff] [blame] | 969 | if (rtm->rtm_protocol == RTPROT_ZEBRA) |
| 970 | SET_FLAG(zebra_flags, ZEBRA_FLAG_SELFROUTE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 971 | |
| 972 | if (rtm->rtm_src_len != 0) |
| 973 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 974 | zlog_warn ("netlink_route_change(): no src len, vrf %u", vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 975 | return 0; |
| 976 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 977 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 978 | index = 0; |
| 979 | dest = NULL; |
| 980 | gate = NULL; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 981 | src = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 982 | |
| 983 | if (tb[RTA_OIF]) |
| 984 | index = *(int *) RTA_DATA (tb[RTA_OIF]); |
| 985 | |
| 986 | if (tb[RTA_DST]) |
| 987 | dest = RTA_DATA (tb[RTA_DST]); |
| 988 | else |
| 989 | dest = anyaddr; |
| 990 | |
| 991 | if (tb[RTA_GATEWAY]) |
| 992 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 993 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 994 | if (tb[RTA_PREFSRC]) |
| 995 | src = RTA_DATA (tb[RTA_PREFSRC]); |
| 996 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 997 | if (h->nlmsg_type == RTM_NEWROUTE) |
| 998 | { |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 999 | if (tb[RTA_METRICS]) |
| 1000 | { |
| 1001 | struct rtattr *mxrta[RTAX_MAX+1]; |
| 1002 | |
| 1003 | memset (mxrta, 0, sizeof mxrta); |
| 1004 | netlink_parse_rtattr (mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]), |
| 1005 | RTA_PAYLOAD(tb[RTA_METRICS])); |
| 1006 | |
| 1007 | if (mxrta[RTAX_MTU]) |
| 1008 | mtu = *(u_int32_t *) RTA_DATA(mxrta[RTAX_MTU]); |
| 1009 | } |
| 1010 | } |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1011 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1012 | if (rtm->rtm_family == AF_INET) |
| 1013 | { |
| 1014 | struct prefix_ipv4 p; |
| 1015 | p.family = AF_INET; |
| 1016 | memcpy (&p.prefix, dest, 4); |
| 1017 | p.prefixlen = rtm->rtm_dst_len; |
| 1018 | |
| 1019 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1020 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1021 | char buf[PREFIX_STRLEN]; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1022 | zlog_debug ("%s %s vrf %u", |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1023 | h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE", |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1024 | prefix2str (&p, buf, sizeof(buf)), vrf_id); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1025 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (h->nlmsg_type == RTM_NEWROUTE) |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1028 | { |
| 1029 | if (!tb[RTA_MULTIPATH]) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1030 | 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] | 1031 | table, 0, mtu, 0, SAFI_UNICAST); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1032 | else |
| 1033 | { |
| 1034 | /* This is a multipath route */ |
| 1035 | |
| 1036 | struct rib *rib; |
| 1037 | struct rtnexthop *rtnh = |
| 1038 | (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]); |
| 1039 | |
| 1040 | len = RTA_PAYLOAD (tb[RTA_MULTIPATH]); |
| 1041 | |
| 1042 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 1043 | rib->type = ZEBRA_ROUTE_KERNEL; |
| 1044 | rib->distance = 0; |
| 1045 | rib->flags = 0; |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1046 | rib->metric = 0; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1047 | rib->mtu = mtu; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1048 | rib->vrf_id = vrf_id; |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1049 | rib->table = table; |
| 1050 | rib->nexthop_num = 0; |
| 1051 | rib->uptime = time (NULL); |
| 1052 | |
| 1053 | for (;;) |
| 1054 | { |
| 1055 | if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len) |
| 1056 | break; |
| 1057 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1058 | index = rtnh->rtnh_ifindex; |
| 1059 | gate = 0; |
| 1060 | if (rtnh->rtnh_len > sizeof (*rtnh)) |
| 1061 | { |
| 1062 | memset (tb, 0, sizeof (tb)); |
| 1063 | netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh), |
| 1064 | rtnh->rtnh_len - sizeof (*rtnh)); |
| 1065 | if (tb[RTA_GATEWAY]) |
| 1066 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 1067 | } |
| 1068 | |
| 1069 | if (gate) |
| 1070 | { |
| 1071 | if (index) |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1072 | rib_nexthop_ipv4_ifindex_add (rib, gate, src, index); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1073 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1074 | rib_nexthop_ipv4_add (rib, gate, src); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1075 | } |
| 1076 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1077 | rib_nexthop_ifindex_add (rib, index); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1078 | |
| 1079 | len -= NLMSG_ALIGN(rtnh->rtnh_len); |
| 1080 | rtnh = RTNH_NEXT(rtnh); |
| 1081 | } |
| 1082 | |
| 1083 | if (rib->nexthop_num == 0) |
| 1084 | XFREE (MTYPE_RIB, rib); |
| 1085 | else |
Avneesh Sachdev | 14d2bba | 2012-04-11 23:51:08 -0700 | [diff] [blame] | 1086 | rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST); |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1089 | else |
James Li | 92992c6 | 2015-11-09 20:21:57 -0500 | [diff] [blame] | 1090 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, gate, |
| 1091 | index, vrf_id, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | #ifdef HAVE_IPV6 |
| 1095 | if (rtm->rtm_family == AF_INET6) |
| 1096 | { |
| 1097 | struct prefix_ipv6 p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1098 | |
| 1099 | p.family = AF_INET6; |
| 1100 | memcpy (&p.prefix, dest, 16); |
| 1101 | p.prefixlen = rtm->rtm_dst_len; |
| 1102 | |
| 1103 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1104 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1105 | char buf[PREFIX_STRLEN]; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1106 | zlog_debug ("%s %s vrf %u", |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1107 | h->nlmsg_type == RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE", |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1108 | prefix2str (&p, buf, sizeof(buf)), vrf_id); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1109 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (h->nlmsg_type == RTM_NEWROUTE) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1112 | 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] | 1113 | 0, mtu, 0, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1114 | else |
James Li | 92992c6 | 2015-11-09 20:21:57 -0500 | [diff] [blame] | 1115 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, gate, index, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1116 | SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1117 | } |
| 1118 | #endif /* HAVE_IPV6 */ |
| 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1123 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1124 | netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 1125 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1126 | { |
| 1127 | int len; |
| 1128 | struct ifinfomsg *ifi; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1129 | struct rtattr *tb[IFLA_MAX + 1]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1130 | struct interface *ifp; |
| 1131 | char *name; |
| 1132 | |
| 1133 | ifi = NLMSG_DATA (h); |
| 1134 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1135 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1136 | { |
| 1137 | /* If this is not link add/delete message so print warning. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1138 | zlog_warn ("netlink_link_change: wrong kernel message %d vrf %u\n", |
| 1139 | h->nlmsg_type, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); |
| 1144 | if (len < 0) |
| 1145 | return -1; |
| 1146 | |
| 1147 | /* Looking up interface name. */ |
| 1148 | memset (tb, 0, sizeof tb); |
| 1149 | netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len); |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1150 | |
paul | 1e19315 | 2005-02-14 23:53:05 +0000 | [diff] [blame] | 1151 | #ifdef IFLA_WIRELESS |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1152 | /* check for wireless messages to ignore */ |
| 1153 | if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0)) |
| 1154 | { |
| 1155 | if (IS_ZEBRA_DEBUG_KERNEL) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1156 | zlog_debug ("%s: ignoring IFLA_WIRELESS message, vrf %u", __func__, |
| 1157 | vrf_id); |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
paul | 1e19315 | 2005-02-14 23:53:05 +0000 | [diff] [blame] | 1160 | #endif /* IFLA_WIRELESS */ |
paul | c15cb24 | 2005-01-24 09:05:27 +0000 | [diff] [blame] | 1161 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1162 | if (tb[IFLA_IFNAME] == NULL) |
| 1163 | return -1; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1164 | name = (char *) RTA_DATA (tb[IFLA_IFNAME]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1165 | |
| 1166 | /* Add interface. */ |
| 1167 | if (h->nlmsg_type == RTM_NEWLINK) |
| 1168 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1169 | ifp = if_lookup_by_name_vrf (name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1170 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1171 | if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 1172 | { |
| 1173 | if (ifp == NULL) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1174 | ifp = if_get_by_name_vrf (name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1175 | |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 1176 | set_ifindex(ifp, ifi->ifi_index); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1177 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 1178 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); |
Brett Ciphery | db19c85 | 2013-10-03 13:48:54 +0000 | [diff] [blame] | 1179 | ifp->metric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1180 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1181 | netlink_interface_update_hw_addr (tb, ifp); |
| 1182 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1183 | /* If new link is added. */ |
| 1184 | if_add_update (ifp); |
| 1185 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1186 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1187 | { |
| 1188 | /* Interface status change. */ |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 1189 | set_ifindex(ifp, ifi->ifi_index); |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 1190 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); |
Brett Ciphery | db19c85 | 2013-10-03 13:48:54 +0000 | [diff] [blame] | 1191 | ifp->metric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1192 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 1193 | netlink_interface_update_hw_addr (tb, ifp); |
| 1194 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1195 | if (if_is_operative (ifp)) |
| 1196 | { |
| 1197 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
| 1198 | if (!if_is_operative (ifp)) |
| 1199 | if_down (ifp); |
ajs | a608bbf | 2005-03-29 17:03:49 +0000 | [diff] [blame] | 1200 | else |
| 1201 | /* Must notify client daemons of new interface status. */ |
| 1202 | zebra_interface_up_update (ifp); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
| 1207 | if (if_is_operative (ifp)) |
| 1208 | if_up (ifp); |
| 1209 | } |
| 1210 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1211 | } |
| 1212 | else |
| 1213 | { |
| 1214 | /* RTM_DELLINK. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1215 | ifp = if_lookup_by_name_vrf (name, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1216 | |
| 1217 | if (ifp == NULL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1218 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1219 | zlog_warn ("interface %s vrf %u is deleted but can't find", |
| 1220 | name, vrf_id); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1224 | if_delete_update (ifp); |
| 1225 | } |
| 1226 | |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1230 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1231 | netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 1232 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1233 | { |
Stephen Hemminger | 3d265b4 | 2008-08-16 17:30:39 +0100 | [diff] [blame] | 1234 | /* JF: Ignore messages that aren't from the kernel */ |
| 1235 | if ( snl->nl_pid != 0 ) |
| 1236 | { |
| 1237 | zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl->nl_pid ); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1241 | switch (h->nlmsg_type) |
| 1242 | { |
| 1243 | case RTM_NEWROUTE: |
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_DELROUTE: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1247 | return netlink_route_change (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1248 | break; |
| 1249 | case RTM_NEWLINK: |
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_DELLINK: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1253 | return netlink_link_change (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | case RTM_NEWADDR: |
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 | case RTM_DELADDR: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1259 | return netlink_interface_addr (snl, h, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1260 | break; |
| 1261 | default: |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1262 | zlog_warn ("Unknown netlink nlmsg_type %d vrf %u\n", h->nlmsg_type, |
| 1263 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1264 | break; |
| 1265 | } |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | /* Interface lookup by netlink socket. */ |
| 1270 | int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1271 | interface_lookup_netlink (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1272 | { |
| 1273 | int ret; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1274 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1275 | /* Get interface information. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1276 | ret = netlink_request (AF_PACKET, RTM_GETLINK, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1277 | if (ret < 0) |
| 1278 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1279 | ret = netlink_parse_info (netlink_interface, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1280 | if (ret < 0) |
| 1281 | return ret; |
| 1282 | |
| 1283 | /* Get IPv4 address of the interfaces. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1284 | ret = netlink_request (AF_INET, RTM_GETADDR, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1285 | if (ret < 0) |
| 1286 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1287 | ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1288 | if (ret < 0) |
| 1289 | return ret; |
| 1290 | |
| 1291 | #ifdef HAVE_IPV6 |
| 1292 | /* Get IPv6 address of the interfaces. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1293 | ret = netlink_request (AF_INET6, RTM_GETADDR, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1294 | if (ret < 0) |
| 1295 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1296 | ret = netlink_parse_info (netlink_interface_addr, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1297 | if (ret < 0) |
| 1298 | return ret; |
| 1299 | #endif /* HAVE_IPV6 */ |
| 1300 | |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | /* Routing table read function using netlink interface. Only called |
| 1305 | bootstrap time. */ |
| 1306 | int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1307 | netlink_route_read (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1308 | { |
| 1309 | int ret; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1310 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1311 | /* Get IPv4 routing table. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1312 | ret = netlink_request (AF_INET, RTM_GETROUTE, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1313 | if (ret < 0) |
| 1314 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1315 | ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1316 | if (ret < 0) |
| 1317 | return ret; |
| 1318 | |
| 1319 | #ifdef HAVE_IPV6 |
| 1320 | /* Get IPv6 routing table. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1321 | ret = netlink_request (AF_INET6, RTM_GETROUTE, &zvrf->netlink_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1322 | if (ret < 0) |
| 1323 | return ret; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1324 | ret = netlink_parse_info (netlink_routing_table, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1325 | if (ret < 0) |
| 1326 | return ret; |
| 1327 | #endif /* HAVE_IPV6 */ |
| 1328 | |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* Utility function comes from iproute2. |
| 1333 | Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 1334 | int |
Paul Jakma | 6f23541 | 2016-04-05 13:21:20 +0100 | [diff] [blame] | 1335 | 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] | 1336 | { |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1337 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1338 | struct rtattr *rta; |
| 1339 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1340 | len = RTA_LENGTH (alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1341 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1342 | if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1343 | return -1; |
| 1344 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1345 | rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1346 | rta->rta_type = type; |
| 1347 | rta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1348 | memcpy (RTA_DATA (rta), data, alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1349 | n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; |
| 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 1354 | int |
Paul Jakma | 6f23541 | 2016-04-05 13:21:20 +0100 | [diff] [blame] | 1355 | rta_addattr_l (struct rtattr *rta, size_t maxlen, int type, void *data, |
| 1356 | size_t alen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1357 | { |
Paul Jakma | 6f23541 | 2016-04-05 13:21:20 +0100 | [diff] [blame] | 1358 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1359 | struct rtattr *subrta; |
| 1360 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1361 | len = RTA_LENGTH (alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1362 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1363 | if (RTA_ALIGN (rta->rta_len) + len > maxlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1364 | return -1; |
| 1365 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1366 | subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1367 | subrta->rta_type = type; |
| 1368 | subrta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1369 | memcpy (RTA_DATA (subrta), data, alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1370 | rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len; |
| 1371 | |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | /* Utility function comes from iproute2. |
| 1376 | Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 1377 | int |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1378 | addattr32 (struct nlmsghdr *n, size_t maxlen, int type, int data) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1379 | { |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 1380 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1381 | struct rtattr *rta; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1382 | |
| 1383 | len = RTA_LENGTH (4); |
| 1384 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1385 | if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) |
| 1386 | return -1; |
| 1387 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1388 | rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1389 | rta->rta_type = type; |
| 1390 | rta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1391 | memcpy (RTA_DATA (rta), &data, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1392 | n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1398 | netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h, |
| 1399 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1400 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1401 | zlog_warn ("netlink_talk: ignoring message type 0x%04x vrf %u", h->nlmsg_type, |
| 1402 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | /* sendmsg() to netlink socket then recvmsg(). */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1407 | static int |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1408 | netlink_talk (struct nlmsghdr *n, struct nlsock *nl, struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1409 | { |
| 1410 | int status; |
| 1411 | struct sockaddr_nl snl; |
Timo Teräs | c299ed7 | 2014-07-29 09:41:55 +0000 | [diff] [blame] | 1412 | struct iovec iov = { |
| 1413 | .iov_base = (void *) n, |
| 1414 | .iov_len = n->nlmsg_len |
| 1415 | }; |
| 1416 | struct msghdr msg = { |
| 1417 | .msg_name = (void *) &snl, |
| 1418 | .msg_namelen = sizeof snl, |
| 1419 | .msg_iov = &iov, |
| 1420 | .msg_iovlen = 1, |
| 1421 | }; |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 1422 | int save_errno; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1423 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1424 | memset (&snl, 0, sizeof snl); |
| 1425 | snl.nl_family = AF_NETLINK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1426 | |
hasso | b7ed1ec | 2005-03-31 20:13:49 +0000 | [diff] [blame] | 1427 | n->nlmsg_seq = ++nl->seq; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1428 | |
| 1429 | /* Request an acknowledgement by setting NLM_F_ACK */ |
| 1430 | n->nlmsg_flags |= NLM_F_ACK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1431 | |
| 1432 | if (IS_ZEBRA_DEBUG_KERNEL) |
hasso | b7ed1ec | 2005-03-31 20:13:49 +0000 | [diff] [blame] | 1433 | zlog_debug ("netlink_talk: %s type %s(%u), seq=%u", nl->name, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1434 | lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type, |
| 1435 | n->nlmsg_seq); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* Send message to netlink interface. */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1438 | if (zserv_privs.change (ZPRIVS_RAISE)) |
| 1439 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1440 | status = sendmsg (nl->sock, &msg, 0); |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 1441 | save_errno = errno; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1442 | if (zserv_privs.change (ZPRIVS_LOWER)) |
| 1443 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 1444 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1445 | if (status < 0) |
| 1446 | { |
| 1447 | zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s", |
ajs | 4be019d | 2005-01-29 16:12:41 +0000 | [diff] [blame] | 1448 | safe_strerror (save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1449 | return -1; |
| 1450 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1451 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1452 | |
| 1453 | /* |
| 1454 | * Get reply from netlink socket. |
| 1455 | * The reply should either be an acknowlegement or an error. |
| 1456 | */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1457 | return netlink_parse_info (netlink_talk_filter, nl, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1460 | /* This function takes a nexthop as argument and adds |
| 1461 | * the appropriate netlink attributes to an existing |
| 1462 | * netlink message. |
| 1463 | * |
| 1464 | * @param routedesc: Human readable description of route type |
| 1465 | * (direct/recursive, single-/multipath) |
| 1466 | * @param bytelen: Length of addresses in bytes. |
| 1467 | * @param nexthop: Nexthop information |
| 1468 | * @param nlmsg: nlmsghdr structure to fill in. |
| 1469 | * @param req_size: The size allocated for the message. |
| 1470 | */ |
| 1471 | static void |
| 1472 | _netlink_route_build_singlepath( |
| 1473 | const char *routedesc, |
| 1474 | int bytelen, |
| 1475 | struct nexthop *nexthop, |
| 1476 | struct nlmsghdr *nlmsg, |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1477 | struct rtmsg *rtmsg, |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1478 | size_t req_size) |
| 1479 | { |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1480 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK)) |
| 1481 | rtmsg->rtm_flags |= RTNH_F_ONLINK; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1482 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 1483 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1484 | { |
| 1485 | addattr_l (nlmsg, req_size, RTA_GATEWAY, |
| 1486 | &nexthop->gate.ipv4, bytelen); |
| 1487 | if (nexthop->src.ipv4.s_addr) |
| 1488 | addattr_l (nlmsg, req_size, RTA_PREFSRC, |
| 1489 | &nexthop->src.ipv4, bytelen); |
| 1490 | |
| 1491 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1492 | zlog_debug("netlink_route_multipath() (%s): " |
| 1493 | "nexthop via %s if %u", |
| 1494 | routedesc, |
| 1495 | inet_ntoa (nexthop->gate.ipv4), |
| 1496 | nexthop->ifindex); |
| 1497 | } |
| 1498 | #ifdef HAVE_IPV6 |
| 1499 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 1500 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1501 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1502 | { |
| 1503 | addattr_l (nlmsg, req_size, RTA_GATEWAY, |
| 1504 | &nexthop->gate.ipv6, bytelen); |
| 1505 | |
| 1506 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1507 | zlog_debug("netlink_route_multipath() (%s): " |
| 1508 | "nexthop via %s if %u", |
| 1509 | routedesc, |
| 1510 | inet6_ntoa (nexthop->gate.ipv6), |
| 1511 | nexthop->ifindex); |
| 1512 | } |
| 1513 | #endif /* HAVE_IPV6 */ |
| 1514 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1515 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 1516 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1517 | { |
| 1518 | addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex); |
| 1519 | |
| 1520 | if (nexthop->src.ipv4.s_addr) |
| 1521 | addattr_l (nlmsg, req_size, RTA_PREFSRC, |
| 1522 | &nexthop->src.ipv4, bytelen); |
| 1523 | |
| 1524 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1525 | zlog_debug("netlink_route_multipath() (%s): " |
| 1526 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1527 | } |
| 1528 | |
| 1529 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 1530 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
| 1531 | { |
| 1532 | addattr32 (nlmsg, req_size, RTA_OIF, nexthop->ifindex); |
| 1533 | |
| 1534 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1535 | zlog_debug("netlink_route_multipath() (%s): " |
| 1536 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | /* This function takes a nexthop as argument and |
| 1541 | * appends to the given rtattr/rtnexthop pair the |
| 1542 | * representation of the nexthop. If the nexthop |
| 1543 | * defines a preferred source, the src parameter |
| 1544 | * will be modified to point to that src, otherwise |
| 1545 | * it will be kept unmodified. |
| 1546 | * |
| 1547 | * @param routedesc: Human readable description of route type |
| 1548 | * (direct/recursive, single-/multipath) |
| 1549 | * @param bytelen: Length of addresses in bytes. |
| 1550 | * @param nexthop: Nexthop information |
| 1551 | * @param rta: rtnetlink attribute structure |
| 1552 | * @param rtnh: pointer to an rtnetlink nexthop structure |
| 1553 | * @param src: pointer pointing to a location where |
| 1554 | * the prefsrc should be stored. |
| 1555 | */ |
| 1556 | static void |
| 1557 | _netlink_route_build_multipath( |
| 1558 | const char *routedesc, |
| 1559 | int bytelen, |
| 1560 | struct nexthop *nexthop, |
| 1561 | struct rtattr *rta, |
| 1562 | struct rtnexthop *rtnh, |
| 1563 | union g_addr **src |
| 1564 | ) |
| 1565 | { |
| 1566 | rtnh->rtnh_len = sizeof (*rtnh); |
| 1567 | rtnh->rtnh_flags = 0; |
| 1568 | rtnh->rtnh_hops = 0; |
| 1569 | rta->rta_len += rtnh->rtnh_len; |
| 1570 | |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1571 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ONLINK)) |
| 1572 | rtnh->rtnh_flags |= RTNH_F_ONLINK; |
| 1573 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1574 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 1575 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1576 | { |
| 1577 | rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY, |
| 1578 | &nexthop->gate.ipv4, bytelen); |
Lu Feng | 621e2aa | 2014-07-11 07:52:15 +0000 | [diff] [blame] | 1579 | rtnh->rtnh_len += sizeof (struct rtattr) + bytelen; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1580 | |
| 1581 | if (nexthop->src.ipv4.s_addr) |
| 1582 | *src = &nexthop->src; |
| 1583 | |
| 1584 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1585 | zlog_debug("netlink_route_multipath() (%s): " |
| 1586 | "nexthop via %s if %u", |
| 1587 | routedesc, |
| 1588 | inet_ntoa (nexthop->gate.ipv4), |
| 1589 | nexthop->ifindex); |
| 1590 | } |
| 1591 | #ifdef HAVE_IPV6 |
| 1592 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 1593 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1594 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1595 | { |
| 1596 | rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTA_GATEWAY, |
| 1597 | &nexthop->gate.ipv6, bytelen); |
Lu Feng | 621e2aa | 2014-07-11 07:52:15 +0000 | [diff] [blame] | 1598 | rtnh->rtnh_len += sizeof (struct rtattr) + bytelen; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1599 | |
| 1600 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1601 | zlog_debug("netlink_route_multipath() (%s): " |
| 1602 | "nexthop via %s if %u", |
| 1603 | routedesc, |
| 1604 | inet6_ntoa (nexthop->gate.ipv6), |
| 1605 | nexthop->ifindex); |
| 1606 | } |
| 1607 | #endif /* HAVE_IPV6 */ |
| 1608 | /* ifindex */ |
| 1609 | if (nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX |
| 1610 | || nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1611 | || nexthop->type == NEXTHOP_TYPE_IFNAME) |
| 1612 | { |
| 1613 | rtnh->rtnh_ifindex = nexthop->ifindex; |
| 1614 | if (nexthop->src.ipv4.s_addr) |
| 1615 | *src = &nexthop->src; |
| 1616 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1617 | zlog_debug("netlink_route_multipath() (%s): " |
| 1618 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1619 | } |
| 1620 | else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1621 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1622 | { |
| 1623 | rtnh->rtnh_ifindex = nexthop->ifindex; |
| 1624 | |
| 1625 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1626 | zlog_debug("netlink_route_multipath() (%s): " |
| 1627 | "nexthop via if %u", routedesc, nexthop->ifindex); |
| 1628 | } |
| 1629 | else |
| 1630 | { |
| 1631 | rtnh->rtnh_ifindex = 0; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | /* Log debug information for netlink_route_multipath |
| 1636 | * if debug logging is enabled. |
| 1637 | * |
| 1638 | * @param cmd: Netlink command which is to be processed |
| 1639 | * @param p: Prefix for which the change is due |
| 1640 | * @param nexthop: Nexthop which is currently processed |
| 1641 | * @param routedesc: Semantic annotation for nexthop |
| 1642 | * (recursive, multipath, etc.) |
| 1643 | * @param family: Address family which the change concerns |
| 1644 | */ |
| 1645 | static void |
| 1646 | _netlink_route_debug( |
| 1647 | int cmd, |
| 1648 | struct prefix *p, |
| 1649 | struct nexthop *nexthop, |
| 1650 | const char *routedesc, |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1651 | int family, |
| 1652 | struct zebra_vrf *zvrf) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1653 | { |
| 1654 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1655 | { |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1656 | char buf[PREFIX_STRLEN]; |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1657 | zlog_debug ("netlink_route_multipath() (%s): %s %s vrf %u type %s", |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1658 | routedesc, |
| 1659 | lookup (nlmsg_str, cmd), |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1660 | prefix2str (p, buf, sizeof(buf)), |
Kaloyan Kovachev | 8c56b44 | 2015-06-15 17:08:48 +0300 | [diff] [blame] | 1661 | zvrf->vrf_id, |
Timo Teräs | be6335d | 2015-05-23 11:08:41 +0300 | [diff] [blame] | 1662 | nexthop_type_to_str (nexthop->type)); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1666 | /* Routing table change via netlink interface. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1667 | static int |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1668 | netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1669 | { |
| 1670 | int bytelen; |
| 1671 | struct sockaddr_nl snl; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1672 | struct nexthop *nexthop = NULL, *tnexthop; |
| 1673 | int recursing; |
| 1674 | int nexthop_num; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1675 | int discard; |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1676 | int family = PREFIX_FAMILY(p); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1677 | const char *routedesc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1678 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1679 | struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1680 | { |
| 1681 | struct nlmsghdr n; |
| 1682 | struct rtmsg r; |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1683 | char buf[NL_PKT_BUF_SIZE]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1684 | } req; |
| 1685 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1686 | struct zebra_vrf *zvrf = vrf_info_lookup (rib->vrf_id); |
| 1687 | |
Jorge Boncompte [DTI2] | a4c06de | 2012-04-20 14:28:40 +0200 | [diff] [blame] | 1688 | memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1689 | |
| 1690 | bytelen = (family == AF_INET ? 4 : 16); |
| 1691 | |
| 1692 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1693 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REPLACE | NLM_F_REQUEST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1694 | req.n.nlmsg_type = cmd; |
| 1695 | req.r.rtm_family = family; |
| 1696 | req.r.rtm_table = rib->table; |
| 1697 | req.r.rtm_dst_len = p->prefixlen; |
Timo Teräs | 40da221 | 2008-08-13 17:37:14 +0100 | [diff] [blame] | 1698 | req.r.rtm_protocol = RTPROT_ZEBRA; |
Timo Teräs | 82a6635 | 2016-01-15 17:36:30 +0200 | [diff] [blame] | 1699 | req.r.rtm_scope = RT_SCOPE_LINK; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1700 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1701 | if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1702 | discard = 1; |
| 1703 | else |
| 1704 | discard = 0; |
| 1705 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1706 | if (cmd == RTM_NEWROUTE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1707 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1708 | if (discard) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1709 | { |
| 1710 | if (rib->flags & ZEBRA_FLAG_BLACKHOLE) |
| 1711 | req.r.rtm_type = RTN_BLACKHOLE; |
| 1712 | else if (rib->flags & ZEBRA_FLAG_REJECT) |
| 1713 | req.r.rtm_type = RTN_UNREACHABLE; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1714 | else |
| 1715 | assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */ |
| 1716 | } |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1717 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1718 | req.r.rtm_type = RTN_UNICAST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen); |
| 1722 | |
| 1723 | /* Metric. */ |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1724 | addattr32 (&req.n, sizeof req, RTA_PRIORITY, NL_DEFAULT_ROUTE_METRIC); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1725 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1726 | if (rib->mtu || rib->nexthop_mtu) |
| 1727 | { |
| 1728 | char buf[NL_PKT_BUF_SIZE]; |
| 1729 | struct rtattr *rta = (void *) buf; |
| 1730 | u_int32_t mtu = rib->mtu; |
| 1731 | if (!mtu || (rib->nexthop_mtu && rib->nexthop_mtu < mtu)) |
| 1732 | mtu = rib->nexthop_mtu; |
| 1733 | rta->rta_type = RTA_METRICS; |
| 1734 | rta->rta_len = RTA_LENGTH(0); |
| 1735 | rta_addattr_l (rta, NL_PKT_BUF_SIZE, RTAX_MTU, &mtu, sizeof mtu); |
| 1736 | addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_METRICS, RTA_DATA (rta), |
| 1737 | RTA_PAYLOAD (rta)); |
| 1738 | } |
| 1739 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1740 | if (discard) |
| 1741 | { |
| 1742 | if (cmd == RTM_NEWROUTE) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1743 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 1744 | { |
| 1745 | /* We shouldn't encounter recursive nexthops on discard routes, |
| 1746 | * but it is probably better to handle that case correctly anyway. |
| 1747 | */ |
| 1748 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1749 | continue; |
| 1750 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1751 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1752 | goto skip; |
| 1753 | } |
| 1754 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1755 | /* Count overall nexthops so we can decide whether to use singlepath |
| 1756 | * or multipath case. */ |
| 1757 | nexthop_num = 0; |
| 1758 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1759 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1760 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1761 | continue; |
| 1762 | if (cmd == RTM_NEWROUTE && !CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1763 | continue; |
| 1764 | if (cmd == RTM_DELROUTE && !CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 1765 | continue; |
| 1766 | |
Timo Teräs | 82a6635 | 2016-01-15 17:36:30 +0200 | [diff] [blame] | 1767 | if (nexthop->type != NEXTHOP_TYPE_IFINDEX && |
| 1768 | nexthop->type != NEXTHOP_TYPE_IFNAME) |
| 1769 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 1770 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1771 | nexthop_num++; |
| 1772 | } |
| 1773 | |
| 1774 | /* Singlepath case. */ |
| 1775 | if (nexthop_num == 1 || MULTIPATH_NUM == 1) |
| 1776 | { |
| 1777 | nexthop_num = 0; |
| 1778 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1779 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1780 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1781 | continue; |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1782 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1783 | if ((cmd == RTM_NEWROUTE |
| 1784 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1785 | || (cmd == RTM_DELROUTE |
| 1786 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))) |
| 1787 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1788 | routedesc = recursing ? "recursive, 1 hop" : "single hop"; |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1789 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1790 | _netlink_route_debug(cmd, p, nexthop, routedesc, family, zvrf); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1791 | _netlink_route_build_singlepath(routedesc, bytelen, |
Christian Franke | e8d3d29 | 2013-07-05 15:35:39 +0000 | [diff] [blame] | 1792 | nexthop, &req.n, &req.r, |
| 1793 | sizeof req); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1794 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1795 | if (cmd == RTM_NEWROUTE) |
| 1796 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1797 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1798 | nexthop_num++; |
| 1799 | break; |
| 1800 | } |
| 1801 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1802 | } |
| 1803 | else |
| 1804 | { |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1805 | char buf[NL_PKT_BUF_SIZE]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1806 | struct rtattr *rta = (void *) buf; |
| 1807 | struct rtnexthop *rtnh; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1808 | union g_addr *src = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1809 | |
| 1810 | rta->rta_type = RTA_MULTIPATH; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1811 | rta->rta_len = RTA_LENGTH (0); |
| 1812 | rtnh = RTA_DATA (rta); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1813 | |
| 1814 | nexthop_num = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1815 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1816 | { |
Donald Sharp | 91ce87a | 2015-12-09 08:24:45 -0500 | [diff] [blame] | 1817 | if (nexthop_num >= MULTIPATH_NUM) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1818 | break; |
| 1819 | |
| 1820 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1821 | continue; |
| 1822 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1823 | if ((cmd == RTM_NEWROUTE |
| 1824 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1825 | || (cmd == RTM_DELROUTE |
| 1826 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))) |
| 1827 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1828 | routedesc = recursing ? "recursive, multihop" : "multihop"; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1829 | nexthop_num++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1830 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1831 | _netlink_route_debug(cmd, p, nexthop, |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1832 | routedesc, family, zvrf); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1833 | _netlink_route_build_multipath(routedesc, bytelen, |
| 1834 | nexthop, rta, rtnh, &src); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1835 | rtnh = RTNH_NEXT (rtnh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1836 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1837 | if (cmd == RTM_NEWROUTE) |
| 1838 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1839 | } |
| 1840 | } |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1841 | if (src) |
| 1842 | addattr_l (&req.n, sizeof req, RTA_PREFSRC, &src->ipv4, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1843 | |
| 1844 | if (rta->rta_len > RTA_LENGTH (0)) |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1845 | addattr_l (&req.n, NL_PKT_BUF_SIZE, RTA_MULTIPATH, RTA_DATA (rta), |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1846 | RTA_PAYLOAD (rta)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | /* If there is no useful nexthop then return. */ |
| 1850 | if (nexthop_num == 0) |
| 1851 | { |
| 1852 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1853 | zlog_debug ("netlink_route_multipath(): No useful nexthop."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1854 | return 0; |
| 1855 | } |
| 1856 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1857 | skip: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1858 | |
| 1859 | /* Destination netlink address. */ |
| 1860 | memset (&snl, 0, sizeof snl); |
| 1861 | snl.nl_family = AF_NETLINK; |
| 1862 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1863 | /* Talk to netlink socket. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1864 | return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | int |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1868 | kernel_route_rib (struct prefix *p, struct rib *old, struct rib *new) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1869 | { |
Timo Teräs | 0abf679 | 2016-01-15 17:36:29 +0200 | [diff] [blame] | 1870 | if (!old && new) |
| 1871 | return netlink_route_multipath (RTM_NEWROUTE, p, new); |
| 1872 | if (old && !new) |
| 1873 | return netlink_route_multipath (RTM_DELROUTE, p, old); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1874 | |
Timo Teräs | 4d3ae71 | 2016-01-15 17:36:32 +0200 | [diff] [blame] | 1875 | /* Replace, can be done atomically if metric does not change; |
| 1876 | * netlink uses [prefix, tos, priority] to identify prefix. |
| 1877 | * Now metric is not sent to kernel, so we can just do atomic replace. */ |
| 1878 | return netlink_route_multipath (RTM_NEWROUTE, p, new); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1879 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1880 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1881 | /* Interface address modification. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1882 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1883 | netlink_address (int cmd, int family, struct interface *ifp, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1884 | struct connected *ifc) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1885 | { |
| 1886 | int bytelen; |
| 1887 | struct prefix *p; |
| 1888 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1889 | struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1890 | { |
| 1891 | struct nlmsghdr n; |
| 1892 | struct ifaddrmsg ifa; |
JR Rivers | 3cadc0c | 2012-04-01 12:16:31 -0700 | [diff] [blame] | 1893 | char buf[NL_PKT_BUF_SIZE]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1894 | } req; |
| 1895 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1896 | struct zebra_vrf *zvrf = vrf_info_lookup (ifp->vrf_id); |
| 1897 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1898 | p = ifc->address; |
Jorge Boncompte [DTI2] | a4c06de | 2012-04-20 14:28:40 +0200 | [diff] [blame] | 1899 | memset (&req, 0, sizeof req - NL_PKT_BUF_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1900 | |
| 1901 | bytelen = (family == AF_INET ? 4 : 16); |
| 1902 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1903 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1904 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 1905 | req.n.nlmsg_type = cmd; |
| 1906 | req.ifa.ifa_family = family; |
| 1907 | |
| 1908 | req.ifa.ifa_index = ifp->ifindex; |
| 1909 | req.ifa.ifa_prefixlen = p->prefixlen; |
| 1910 | |
| 1911 | addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen); |
| 1912 | |
| 1913 | if (family == AF_INET && cmd == RTM_NEWADDR) |
| 1914 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 1915 | if (!CONNECTED_PEER(ifc) && ifc->destination) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1916 | { |
| 1917 | p = ifc->destination; |
| 1918 | addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix, |
| 1919 | bytelen); |
| 1920 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY)) |
| 1924 | SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1925 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1926 | if (ifc->label) |
| 1927 | addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1928 | strlen (ifc->label) + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1929 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1930 | return netlink_talk (&req.n, &zvrf->netlink_cmd, zvrf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | int |
| 1934 | kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc) |
| 1935 | { |
| 1936 | return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc); |
| 1937 | } |
| 1938 | |
| 1939 | int |
| 1940 | kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc) |
| 1941 | { |
| 1942 | return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc); |
| 1943 | } |
| 1944 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1945 | |
| 1946 | extern struct thread_master *master; |
| 1947 | |
| 1948 | /* Kernel route reflection. */ |
Stephen Hemminger | 6072b24 | 2008-08-14 16:52:26 +0100 | [diff] [blame] | 1949 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1950 | kernel_read (struct thread *thread) |
| 1951 | { |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1952 | struct zebra_vrf *zvrf = (struct zebra_vrf *)THREAD_ARG (thread); |
| 1953 | netlink_parse_info (netlink_information_fetch, &zvrf->netlink, zvrf); |
| 1954 | zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf, |
| 1955 | zvrf->netlink.sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1956 | |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
Stephen Hemminger | 3d265b4 | 2008-08-16 17:30:39 +0100 | [diff] [blame] | 1960 | /* Filter out messages from self that occur on listener socket, |
| 1961 | caused by our actions on the command socket |
| 1962 | */ |
| 1963 | static void netlink_install_filter (int sock, __u32 pid) |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1964 | { |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1965 | struct sock_filter filter[] = { |
Stephen Hemminger | 3d265b4 | 2008-08-16 17:30:39 +0100 | [diff] [blame] | 1966 | /* 0: ldh [4] */ |
| 1967 | BPF_STMT(BPF_LD|BPF_ABS|BPF_H, offsetof(struct nlmsghdr, nlmsg_type)), |
| 1968 | /* 1: jeq 0x18 jt 3 jf 6 */ |
| 1969 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_NEWROUTE), 1, 0), |
| 1970 | /* 2: jeq 0x19 jt 3 jf 6 */ |
| 1971 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(RTM_DELROUTE), 0, 3), |
| 1972 | /* 3: ldw [12] */ |
| 1973 | BPF_STMT(BPF_LD|BPF_ABS|BPF_W, offsetof(struct nlmsghdr, nlmsg_pid)), |
| 1974 | /* 4: jeq XX jt 5 jf 6 */ |
| 1975 | BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htonl(pid), 0, 1), |
| 1976 | /* 5: ret 0 (skip) */ |
| 1977 | BPF_STMT(BPF_RET|BPF_K, 0), |
| 1978 | /* 6: ret 0xffff (keep) */ |
| 1979 | BPF_STMT(BPF_RET|BPF_K, 0xffff), |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1980 | }; |
| 1981 | |
| 1982 | struct sock_fprog prog = { |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 1983 | .len = array_size(filter), |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 1984 | .filter = filter, |
| 1985 | }; |
| 1986 | |
| 1987 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) < 0) |
| 1988 | zlog_warn ("Can't install socket filter: %s\n", safe_strerror(errno)); |
| 1989 | } |
| 1990 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1991 | /* Exported interface function. This function simply calls |
| 1992 | netlink_socket (). */ |
| 1993 | void |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 1994 | kernel_init (struct zebra_vrf *zvrf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1995 | { |
| 1996 | unsigned long groups; |
| 1997 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1998 | groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1999 | #ifdef HAVE_IPV6 |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2000 | groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2001 | #endif /* HAVE_IPV6 */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2002 | netlink_socket (&zvrf->netlink, groups, zvrf->vrf_id); |
| 2003 | netlink_socket (&zvrf->netlink_cmd, 0, zvrf->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2004 | |
| 2005 | /* Register kernel socket. */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2006 | if (zvrf->netlink.sock > 0) |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 2007 | { |
Stephen Hemminger | 4cde931 | 2008-08-16 17:51:27 +0100 | [diff] [blame] | 2008 | /* Only want non-blocking on the netlink event socket */ |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2009 | if (fcntl (zvrf->netlink.sock, F_SETFL, O_NONBLOCK) < 0) |
| 2010 | zlog_err ("Can't set %s socket flags: %s", zvrf->netlink.name, |
| 2011 | safe_strerror (errno)); |
Stephen Hemminger | 4cde931 | 2008-08-16 17:51:27 +0100 | [diff] [blame] | 2012 | |
Stephen Hemminger | 30afea3 | 2008-08-16 18:25:47 +0100 | [diff] [blame] | 2013 | /* Set receive buffer size if it's set from command line */ |
| 2014 | if (nl_rcvbufsize) |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2015 | netlink_recvbuf (&zvrf->netlink, nl_rcvbufsize); |
Stephen Hemminger | 30afea3 | 2008-08-16 18:25:47 +0100 | [diff] [blame] | 2016 | |
Feng Lu | 758fb8f | 2014-07-03 18:23:09 +0800 | [diff] [blame] | 2017 | netlink_install_filter (zvrf->netlink.sock, zvrf->netlink_cmd.snl.nl_pid); |
| 2018 | zvrf->t_netlink = thread_add_read (zebrad.master, kernel_read, zvrf, |
| 2019 | zvrf->netlink.sock); |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | void |
| 2024 | kernel_terminate (struct zebra_vrf *zvrf) |
| 2025 | { |
| 2026 | THREAD_READ_OFF (zvrf->t_netlink); |
| 2027 | |
| 2028 | if (zvrf->netlink.sock >= 0) |
| 2029 | { |
| 2030 | close (zvrf->netlink.sock); |
| 2031 | zvrf->netlink.sock = -1; |
| 2032 | } |
| 2033 | |
| 2034 | if (zvrf->netlink_cmd.sock >= 0) |
| 2035 | { |
| 2036 | close (zvrf->netlink_cmd.sock); |
| 2037 | zvrf->netlink_cmd.sock = -1; |
Paul Jakma | 768a27e | 2008-05-29 18:23:08 +0000 | [diff] [blame] | 2038 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2039 | } |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 2040 | |
| 2041 | /* |
| 2042 | * nl_msg_type_to_str |
| 2043 | */ |
| 2044 | const char * |
| 2045 | nl_msg_type_to_str (uint16_t msg_type) |
| 2046 | { |
| 2047 | return lookup (nlmsg_str, msg_type); |
| 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * nl_rtproto_to_str |
| 2052 | */ |
| 2053 | const char * |
| 2054 | nl_rtproto_to_str (u_char rtproto) |
| 2055 | { |
| 2056 | return lookup (rtproto_str, rtproto); |
| 2057 | } |