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