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