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