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" |
| 35 | #include "rib.h" |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 36 | #include "thread.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 37 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | |
| 39 | #include "zebra/zserv.h" |
| 40 | #include "zebra/redistribute.h" |
| 41 | #include "zebra/interface.h" |
| 42 | #include "zebra/debug.h" |
| 43 | |
| 44 | /* Socket interface to kernel */ |
| 45 | struct nlsock |
| 46 | { |
| 47 | int sock; |
| 48 | int seq; |
| 49 | struct sockaddr_nl snl; |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 50 | const char *name; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 51 | } netlink = { -1, 0, {0}, "netlink-listen"}, /* kernel messages */ |
| 52 | netlink_cmd = { -1, 0, {0}, "netlink-cmd"}, /* command channel */ |
| 53 | netlink_addr = { -1, 0, {0}, "netlink-addr"}; /* address channel */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 55 | struct message nlmsg_str[] = { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | {RTM_NEWROUTE, "RTM_NEWROUTE"}, |
| 57 | {RTM_DELROUTE, "RTM_DELROUTE"}, |
| 58 | {RTM_GETROUTE, "RTM_GETROUTE"}, |
| 59 | {RTM_NEWLINK, "RTM_NEWLINK"}, |
| 60 | {RTM_DELLINK, "RTM_DELLINK"}, |
| 61 | {RTM_GETLINK, "RTM_GETLINK"}, |
| 62 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 63 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 64 | {RTM_GETADDR, "RTM_GETADDR"}, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 65 | {0, NULL} |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 68 | const char *nexthop_types_desc[] = |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 69 | { |
| 70 | "none", |
| 71 | "Directly connected", |
| 72 | "Interface route", |
| 73 | "IPv4 nexthop", |
| 74 | "IPv4 nexthop with ifindex", |
| 75 | "IPv4 nexthop with ifname", |
| 76 | "IPv6 nexthop" |
| 77 | "IPv6 nexthop with ifindex", |
| 78 | "IPv6 nexthop with ifname", |
| 79 | "Null0 nexthop", |
| 80 | }; |
| 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | /* Make socket for Linux netlink interface. */ |
| 90 | static int |
| 91 | netlink_socket (struct nlsock *nl, unsigned long groups) |
| 92 | { |
| 93 | int ret; |
| 94 | struct sockaddr_nl snl; |
| 95 | int sock; |
| 96 | int namelen; |
| 97 | |
| 98 | sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 99 | if (sock < 0) |
| 100 | { |
| 101 | zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 102 | strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | ret = fcntl (sock, F_SETFL, O_NONBLOCK); |
| 107 | if (ret < 0) |
| 108 | { |
| 109 | zlog (NULL, LOG_ERR, "Can't set %s socket flags: %s", nl->name, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 110 | strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 111 | close (sock); |
| 112 | return -1; |
| 113 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 114 | |
hasso | c34b6b5 | 2004-08-31 13:41:49 +0000 | [diff] [blame] | 115 | /* Set receive buffer size if it's set from command line */ |
| 116 | if (nl_rcvbufsize) |
| 117 | { |
| 118 | u_int32_t oldsize, oldlen; |
| 119 | u_int32_t newsize, newlen; |
| 120 | |
| 121 | oldlen = sizeof(oldsize); |
| 122 | newlen = sizeof(newsize); |
| 123 | |
| 124 | ret = getsockopt(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 | strerror (errno)); |
| 129 | close (sock); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &nl_rcvbufsize, |
| 134 | sizeof(nl_rcvbufsize)); |
| 135 | if (ret < 0) |
| 136 | { |
| 137 | zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name, |
| 138 | strerror (errno)); |
| 139 | close (sock); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | ret = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen); |
| 144 | if (ret < 0) |
| 145 | { |
| 146 | zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name, |
| 147 | strerror (errno)); |
| 148 | close (sock); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | zlog (NULL, LOG_INFO, |
| 153 | "Setting netlink socket receive buffer size: %u -> %u", |
| 154 | oldsize, newsize); |
| 155 | } |
| 156 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | memset (&snl, 0, sizeof snl); |
| 158 | snl.nl_family = AF_NETLINK; |
| 159 | snl.nl_groups = groups; |
| 160 | |
| 161 | /* Bind the socket to the netlink structure for anything. */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 162 | if (zserv_privs.change (ZPRIVS_RAISE)) |
| 163 | { |
| 164 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 165 | return -1; |
| 166 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 167 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 168 | ret = bind (sock, (struct sockaddr *) &snl, sizeof snl); |
hasso | 55e7ecd | 2004-08-06 08:41:56 +0000 | [diff] [blame] | 169 | if (zserv_privs.change (ZPRIVS_LOWER)) |
| 170 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 171 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | if (ret < 0) |
| 173 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 174 | zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s", |
| 175 | nl->name, snl.nl_groups, strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | close (sock); |
| 177 | return -1; |
| 178 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 179 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 180 | /* multiple netlink sockets will have different nl_pid */ |
| 181 | namelen = sizeof snl; |
hasso | c9e52be | 2004-09-26 16:09:34 +0000 | [diff] [blame] | 182 | ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | if (ret < 0 || namelen != sizeof snl) |
| 184 | { |
| 185 | zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 186 | strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | close (sock); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | nl->snl = snl; |
| 192 | nl->sock = sock; |
| 193 | return ret; |
| 194 | } |
| 195 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 196 | int |
| 197 | set_netlink_blocking (struct nlsock *nl, int *flags) |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 198 | { |
| 199 | |
| 200 | /* Change socket flags for blocking I/O. */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 201 | if ((*flags = fcntl (nl->sock, F_GETFL, 0)) < 0) |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 202 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 203 | zlog (NULL, LOG_ERR, "%s:%i F_GETFL error: %s", |
| 204 | __FUNCTION__, __LINE__, strerror (errno)); |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 205 | return -1; |
| 206 | } |
| 207 | *flags &= ~O_NONBLOCK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 208 | if (fcntl (nl->sock, F_SETFL, *flags) < 0) |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 209 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 210 | zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s", |
| 211 | __FUNCTION__, __LINE__, strerror (errno)); |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 217 | int |
| 218 | set_netlink_nonblocking (struct nlsock *nl, int *flags) |
| 219 | { |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 220 | /* Restore socket flags for nonblocking I/O */ |
| 221 | *flags |= O_NONBLOCK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 222 | if (fcntl (nl->sock, F_SETFL, *flags) < 0) |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 223 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 224 | zlog (NULL, LOG_ERR, "%s:%i F_SETFL error: %s", |
| 225 | __FUNCTION__, __LINE__, strerror (errno)); |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 226 | return -1; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | /* Get type specified information from netlink. */ |
| 232 | static int |
| 233 | netlink_request (int family, int type, struct nlsock *nl) |
| 234 | { |
| 235 | int ret; |
| 236 | struct sockaddr_nl snl; |
| 237 | |
| 238 | struct |
| 239 | { |
| 240 | struct nlmsghdr nlh; |
| 241 | struct rtgenmsg g; |
| 242 | } req; |
| 243 | |
| 244 | |
| 245 | /* Check netlink socket. */ |
| 246 | if (nl->sock < 0) |
| 247 | { |
| 248 | zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | memset (&snl, 0, sizeof snl); |
| 253 | snl.nl_family = AF_NETLINK; |
| 254 | |
| 255 | req.nlh.nlmsg_len = sizeof req; |
| 256 | req.nlh.nlmsg_type = type; |
| 257 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
| 258 | req.nlh.nlmsg_pid = 0; |
| 259 | req.nlh.nlmsg_seq = ++nl->seq; |
| 260 | req.g.rtgen_family = family; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 261 | |
| 262 | /* linux appears to check capabilities on every message |
| 263 | * have to raise caps for every message sent |
| 264 | */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 265 | if (zserv_privs.change (ZPRIVS_RAISE)) |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 266 | { |
| 267 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 268 | return -1; |
| 269 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 270 | |
| 271 | ret = sendto (nl->sock, (void *) &req, sizeof req, 0, |
| 272 | (struct sockaddr *) &snl, sizeof snl); |
| 273 | |
| 274 | if (zserv_privs.change (ZPRIVS_LOWER)) |
| 275 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 276 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 277 | if (ret < 0) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 278 | { |
| 279 | zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name, |
| 280 | strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | return -1; |
| 282 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 283 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /* Receive message from netlink interface and pass those information |
| 288 | to the given function. */ |
| 289 | static int |
| 290 | netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *), |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 291 | struct nlsock *nl) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 292 | { |
| 293 | int status; |
| 294 | int ret = 0; |
| 295 | int error; |
| 296 | |
| 297 | while (1) |
| 298 | { |
| 299 | char buf[4096]; |
| 300 | struct iovec iov = { buf, sizeof buf }; |
| 301 | struct sockaddr_nl snl; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 302 | struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 303 | struct nlmsghdr *h; |
| 304 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 305 | if (zserv_privs.change (ZPRIVS_RAISE)) |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 306 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 307 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 308 | status = recvmsg (nl->sock, &msg, 0); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 309 | |
| 310 | if (zserv_privs.change (ZPRIVS_LOWER)) |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 311 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | |
| 313 | if (status < 0) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 314 | { |
| 315 | if (errno == EINTR) |
| 316 | continue; |
| 317 | if (errno == EWOULDBLOCK || errno == EAGAIN) |
| 318 | break; |
| 319 | zlog (NULL, LOG_ERR, "%s recvmsg overrun", nl->name); |
| 320 | continue; |
| 321 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | |
| 323 | if (status == 0) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 324 | { |
| 325 | zlog (NULL, LOG_ERR, "%s EOF", nl->name); |
| 326 | return -1; |
| 327 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | |
| 329 | if (msg.msg_namelen != sizeof snl) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 330 | { |
| 331 | zlog (NULL, LOG_ERR, "%s sender address length error: length %d", |
| 332 | nl->name, msg.msg_namelen); |
| 333 | return -1; |
| 334 | } |
paul | b84d3a1 | 2003-11-17 10:31:01 +0000 | [diff] [blame] | 335 | |
| 336 | /* JF: Ignore messages that aren't from the kernel */ |
| 337 | if ( snl.nl_pid != 0 ) |
| 338 | { |
| 339 | zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl.nl_pid ); |
| 340 | continue; |
| 341 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 342 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 343 | for (h = (struct nlmsghdr *) buf; NLMSG_OK (h, status); |
| 344 | h = NLMSG_NEXT (h, status)) |
| 345 | { |
| 346 | /* Finish of reading. */ |
| 347 | if (h->nlmsg_type == NLMSG_DONE) |
| 348 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 349 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 350 | /* Error handling. */ |
| 351 | if (h->nlmsg_type == NLMSG_ERROR) |
| 352 | { |
| 353 | struct nlmsgerr *err = (struct nlmsgerr *) NLMSG_DATA (h); |
| 354 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | /* If the error field is zero, then this is an ACK */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 356 | if (err->error == 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 357 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 358 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 359 | { |
| 360 | zlog_info ("%s: %s ACK: type=%s(%u), seq=%u, pid=%d", |
| 361 | __FUNCTION__, nl->name, |
| 362 | lookup (nlmsg_str, err->msg.nlmsg_type), |
| 363 | err->msg.nlmsg_type, err->msg.nlmsg_seq, |
| 364 | err->msg.nlmsg_pid); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 365 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 366 | |
| 367 | /* return if not a multipart message, otherwise continue */ |
| 368 | if (!(h->nlmsg_flags & NLM_F_MULTI)) |
| 369 | { |
| 370 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 371 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 372 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 373 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 374 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 375 | if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr))) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 376 | { |
| 377 | zlog (NULL, LOG_ERR, "%s error: message truncated", |
| 378 | nl->name); |
| 379 | return -1; |
| 380 | } |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 381 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 382 | /* Deal with Error Noise - MAG */ |
| 383 | { |
| 384 | int loglvl = LOG_ERR; |
| 385 | int errnum = err->error; |
| 386 | int msg_type = err->msg.nlmsg_type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 387 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 388 | if (nl == &netlink_cmd |
| 389 | && (-errnum == ENODEV || -errnum == ESRCH) |
| 390 | && (msg_type == RTM_NEWROUTE || msg_type == RTM_DELROUTE)) |
| 391 | loglvl = LOG_DEBUG; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 392 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 393 | zlog (NULL, loglvl, "%s error: %s, type=%s(%u), " |
| 394 | "seq=%u, pid=%d", |
| 395 | nl->name, strerror (-errnum), |
| 396 | lookup (nlmsg_str, msg_type), |
| 397 | msg_type, err->msg.nlmsg_seq, err->msg.nlmsg_pid); |
| 398 | } |
| 399 | /* |
| 400 | ret = -1; |
| 401 | continue; |
| 402 | */ |
| 403 | return -1; |
| 404 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 405 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 406 | /* OK we got netlink message. */ |
| 407 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 408 | zlog_info ("netlink_parse_info: %s type %s(%u), seq=%u, pid=%d", |
| 409 | nl->name, |
| 410 | lookup (nlmsg_str, h->nlmsg_type), h->nlmsg_type, |
| 411 | h->nlmsg_seq, h->nlmsg_pid); |
| 412 | |
| 413 | /* skip unsolicited messages originating from command socket */ |
| 414 | if (nl != &netlink_cmd && h->nlmsg_pid == netlink_cmd.snl.nl_pid) |
| 415 | { |
| 416 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 417 | zlog_info ("netlink_parse_info: %s packet comes from %s", |
| 418 | nl->name, netlink_cmd.name); |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | error = (*filter) (&snl, h); |
| 423 | if (error < 0) |
| 424 | { |
| 425 | zlog (NULL, LOG_ERR, "%s filter function error", nl->name); |
| 426 | ret = error; |
| 427 | } |
| 428 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 429 | |
| 430 | /* After error care. */ |
| 431 | if (msg.msg_flags & MSG_TRUNC) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 432 | { |
| 433 | zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name); |
| 434 | continue; |
| 435 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 436 | if (status) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 437 | { |
| 438 | zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name, |
| 439 | status); |
| 440 | return -1; |
| 441 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 442 | } |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | /* Utility function for parse rtattr. */ |
| 447 | static void |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 448 | netlink_parse_rtattr (struct rtattr **tb, int max, struct rtattr *rta, |
| 449 | int len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 450 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 451 | while (RTA_OK (rta, len)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 452 | { |
| 453 | if (rta->rta_type <= max) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 454 | tb[rta->rta_type] = rta; |
| 455 | rta = RTA_NEXT (rta, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | /* Called from interface_lookup_netlink(). This function is only used |
| 460 | during bootstrap. */ |
| 461 | int |
| 462 | netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h) |
| 463 | { |
| 464 | int len; |
| 465 | struct ifinfomsg *ifi; |
| 466 | struct rtattr *tb[IFLA_MAX + 1]; |
| 467 | struct interface *ifp; |
| 468 | char *name; |
| 469 | int i; |
| 470 | |
| 471 | ifi = NLMSG_DATA (h); |
| 472 | |
| 473 | if (h->nlmsg_type != RTM_NEWLINK) |
| 474 | return 0; |
| 475 | |
| 476 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); |
| 477 | if (len < 0) |
| 478 | return -1; |
| 479 | |
| 480 | /* Looking up interface name. */ |
| 481 | memset (tb, 0, sizeof tb); |
| 482 | netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len); |
| 483 | if (tb[IFLA_IFNAME] == NULL) |
| 484 | return -1; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 485 | name = (char *) RTA_DATA (tb[IFLA_IFNAME]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 486 | |
| 487 | /* Add interface. */ |
| 488 | ifp = if_get_by_name (name); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 489 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 490 | ifp->ifindex = ifi->ifi_index; |
| 491 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 492 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 493 | ifp->metric = 1; |
| 494 | |
| 495 | /* Hardware type and address. */ |
| 496 | ifp->hw_type = ifi->ifi_type; |
| 497 | |
| 498 | if (tb[IFLA_ADDRESS]) |
| 499 | { |
| 500 | int hw_addr_len; |
| 501 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 502 | hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 503 | |
| 504 | if (hw_addr_len > INTERFACE_HWADDR_MAX) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 505 | zlog_warn ("Hardware address is too large: %d", hw_addr_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 506 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 507 | { |
| 508 | ifp->hw_addr_len = hw_addr_len; |
| 509 | memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 510 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 511 | for (i = 0; i < hw_addr_len; i++) |
| 512 | if (ifp->hw_addr[i] != 0) |
| 513 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 514 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 515 | if (i == hw_addr_len) |
| 516 | ifp->hw_addr_len = 0; |
| 517 | else |
| 518 | ifp->hw_addr_len = hw_addr_len; |
| 519 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if_add_update (ifp); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* Lookup interface IPv4/IPv6 address. */ |
| 528 | int |
| 529 | netlink_interface_addr (struct sockaddr_nl *snl, struct nlmsghdr *h) |
| 530 | { |
| 531 | int len; |
| 532 | struct ifaddrmsg *ifa; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 533 | struct rtattr *tb[IFA_MAX + 1]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | struct interface *ifp; |
| 535 | void *addr = NULL; |
| 536 | void *broad = NULL; |
| 537 | u_char flags = 0; |
| 538 | char *label = NULL; |
| 539 | |
| 540 | ifa = NLMSG_DATA (h); |
| 541 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 542 | if (ifa->ifa_family != AF_INET |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | #ifdef HAVE_IPV6 |
| 544 | && ifa->ifa_family != AF_INET6 |
| 545 | #endif /* HAVE_IPV6 */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 546 | ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | return 0; |
| 548 | |
| 549 | if (h->nlmsg_type != RTM_NEWADDR && h->nlmsg_type != RTM_DELADDR) |
| 550 | return 0; |
| 551 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 552 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifaddrmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | if (len < 0) |
| 554 | return -1; |
| 555 | |
| 556 | memset (tb, 0, sizeof tb); |
| 557 | netlink_parse_rtattr (tb, IFA_MAX, IFA_RTA (ifa), len); |
| 558 | |
| 559 | ifp = if_lookup_by_index (ifa->ifa_index); |
| 560 | if (ifp == NULL) |
| 561 | { |
| 562 | zlog_err ("netlink_interface_addr can't find interface by index %d", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 563 | ifa->ifa_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 564 | return -1; |
| 565 | } |
| 566 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 567 | if (IS_ZEBRA_DEBUG_KERNEL) /* remove this line to see initial ifcfg */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | { |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 569 | char buf[BUFSIZ]; |
| 570 | zlog_info ("netlink_interface_addr %s %s/%d:", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 571 | lookup (nlmsg_str, h->nlmsg_type), |
| 572 | ifp->name, ifa->ifa_prefixlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 573 | if (tb[IFA_LOCAL]) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 574 | zlog_info (" IFA_LOCAL %s", inet_ntop (ifa->ifa_family, |
| 575 | RTA_DATA (tb[IFA_LOCAL]), |
| 576 | buf, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | if (tb[IFA_ADDRESS]) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 578 | zlog_info (" IFA_ADDRESS %s", inet_ntop (ifa->ifa_family, |
| 579 | RTA_DATA (tb |
| 580 | [IFA_ADDRESS]), |
| 581 | buf, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 582 | if (tb[IFA_BROADCAST]) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 583 | zlog_info (" IFA_BROADCAST %s", inet_ntop (ifa->ifa_family, |
| 584 | RTA_DATA (tb |
| 585 | [IFA_BROADCAST]), |
| 586 | buf, BUFSIZ)); |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 587 | if (tb[IFA_LABEL] && strcmp (ifp->name, RTA_DATA (tb[IFA_LABEL]))) |
hasso | 5707cce | 2004-03-04 19:20:44 +0000 | [diff] [blame] | 588 | zlog_info (" IFA_LABEL %s", (char *)RTA_DATA (tb[IFA_LABEL])); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 589 | } |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 590 | |
| 591 | if (tb[IFA_ADDRESS] == NULL) |
| 592 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; |
| 593 | |
| 594 | if (ifp->flags & IFF_POINTOPOINT) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 595 | { |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 596 | if (tb[IFA_LOCAL]) |
| 597 | { |
| 598 | addr = RTA_DATA (tb[IFA_LOCAL]); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 599 | if (tb[IFA_ADDRESS] && |
| 600 | memcmp(RTA_DATA(tb[IFA_ADDRESS]),RTA_DATA(tb[IFA_LOCAL]),4)) |
| 601 | /* if IFA_ADDRESS != IFA_LOCAL, then it's the peer address */ |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 602 | broad = RTA_DATA (tb[IFA_ADDRESS]); |
| 603 | else |
| 604 | broad = NULL; |
| 605 | } |
| 606 | else |
| 607 | { |
| 608 | if (tb[IFA_ADDRESS]) |
| 609 | addr = RTA_DATA (tb[IFA_ADDRESS]); |
| 610 | else |
| 611 | addr = NULL; |
| 612 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 613 | } |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 614 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 615 | { |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 616 | if (tb[IFA_ADDRESS]) |
| 617 | addr = RTA_DATA (tb[IFA_ADDRESS]); |
| 618 | else |
| 619 | addr = NULL; |
| 620 | |
| 621 | if (tb[IFA_BROADCAST]) |
| 622 | broad = RTA_DATA(tb[IFA_BROADCAST]); |
| 623 | else |
| 624 | broad = NULL; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 625 | } |
paul | 00df0c1 | 2002-12-13 21:07:36 +0000 | [diff] [blame] | 626 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | /* Flags. */ |
| 628 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
| 629 | SET_FLAG (flags, ZEBRA_IFA_SECONDARY); |
| 630 | |
| 631 | /* Label */ |
| 632 | if (tb[IFA_LABEL]) |
| 633 | label = (char *) RTA_DATA (tb[IFA_LABEL]); |
| 634 | |
| 635 | if (ifp && label && strcmp (ifp->name, label) == 0) |
| 636 | label = NULL; |
| 637 | |
| 638 | /* Register interface address to the interface. */ |
| 639 | if (ifa->ifa_family == AF_INET) |
| 640 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 641 | if (h->nlmsg_type == RTM_NEWADDR) |
| 642 | connected_add_ipv4 (ifp, flags, |
| 643 | (struct in_addr *) addr, ifa->ifa_prefixlen, |
| 644 | (struct in_addr *) broad, label); |
| 645 | else |
| 646 | connected_delete_ipv4 (ifp, flags, |
| 647 | (struct in_addr *) addr, ifa->ifa_prefixlen, |
| 648 | (struct in_addr *) broad, label); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 649 | } |
| 650 | #ifdef HAVE_IPV6 |
| 651 | if (ifa->ifa_family == AF_INET6) |
| 652 | { |
| 653 | if (h->nlmsg_type == RTM_NEWADDR) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 654 | connected_add_ipv6 (ifp, |
| 655 | (struct in6_addr *) addr, ifa->ifa_prefixlen, |
| 656 | (struct in6_addr *) broad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 657 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 658 | connected_delete_ipv6 (ifp, |
| 659 | (struct in6_addr *) addr, ifa->ifa_prefixlen, |
| 660 | (struct in6_addr *) broad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 661 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 662 | #endif /* HAVE_IPV6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | /* Looking up routing table by netlink interface. */ |
| 668 | int |
| 669 | netlink_routing_table (struct sockaddr_nl *snl, struct nlmsghdr *h) |
| 670 | { |
| 671 | int len; |
| 672 | struct rtmsg *rtm; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 673 | struct rtattr *tb[RTA_MAX + 1]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 674 | u_char flags = 0; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 675 | |
| 676 | char anyaddr[16] = { 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 677 | |
| 678 | int index; |
| 679 | int table; |
hasso | 34195bf | 2004-04-06 12:07:06 +0000 | [diff] [blame] | 680 | int metric; |
| 681 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 682 | void *dest; |
| 683 | void *gate; |
| 684 | |
| 685 | rtm = NLMSG_DATA (h); |
| 686 | |
| 687 | if (h->nlmsg_type != RTM_NEWROUTE) |
| 688 | return 0; |
| 689 | if (rtm->rtm_type != RTN_UNICAST) |
| 690 | return 0; |
| 691 | |
| 692 | table = rtm->rtm_table; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 693 | #if 0 /* we weed them out later in rib_weed_tables () */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 694 | if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 695 | return 0; |
| 696 | #endif |
| 697 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 698 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | if (len < 0) |
| 700 | return -1; |
| 701 | |
| 702 | memset (tb, 0, sizeof tb); |
| 703 | netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len); |
| 704 | |
| 705 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 706 | return 0; |
| 707 | if (rtm->rtm_protocol == RTPROT_REDIRECT) |
| 708 | return 0; |
| 709 | if (rtm->rtm_protocol == RTPROT_KERNEL) |
| 710 | return 0; |
| 711 | |
| 712 | if (rtm->rtm_src_len != 0) |
| 713 | return 0; |
| 714 | |
| 715 | /* Route which inserted by Zebra. */ |
| 716 | if (rtm->rtm_protocol == RTPROT_ZEBRA) |
| 717 | flags |= ZEBRA_FLAG_SELFROUTE; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 718 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 719 | index = 0; |
hasso | 34195bf | 2004-04-06 12:07:06 +0000 | [diff] [blame] | 720 | metric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 721 | dest = NULL; |
| 722 | gate = NULL; |
| 723 | |
| 724 | if (tb[RTA_OIF]) |
| 725 | index = *(int *) RTA_DATA (tb[RTA_OIF]); |
| 726 | |
| 727 | if (tb[RTA_DST]) |
| 728 | dest = RTA_DATA (tb[RTA_DST]); |
| 729 | else |
| 730 | dest = anyaddr; |
| 731 | |
| 732 | /* Multipath treatment is needed. */ |
| 733 | if (tb[RTA_GATEWAY]) |
| 734 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 735 | |
hasso | 34195bf | 2004-04-06 12:07:06 +0000 | [diff] [blame] | 736 | if (tb[RTA_PRIORITY]) |
| 737 | metric = *(int *) RTA_DATA(tb[RTA_PRIORITY]); |
| 738 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 739 | if (rtm->rtm_family == AF_INET) |
| 740 | { |
| 741 | struct prefix_ipv4 p; |
| 742 | p.family = AF_INET; |
| 743 | memcpy (&p.prefix, dest, 4); |
| 744 | p.prefixlen = rtm->rtm_dst_len; |
| 745 | |
hasso | 34195bf | 2004-04-06 12:07:06 +0000 | [diff] [blame] | 746 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table, metric, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 747 | } |
| 748 | #ifdef HAVE_IPV6 |
| 749 | if (rtm->rtm_family == AF_INET6) |
| 750 | { |
| 751 | struct prefix_ipv6 p; |
| 752 | p.family = AF_INET6; |
| 753 | memcpy (&p.prefix, dest, 16); |
| 754 | p.prefixlen = rtm->rtm_dst_len; |
| 755 | |
| 756 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, index, table); |
| 757 | } |
| 758 | #endif /* HAVE_IPV6 */ |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 763 | struct message rtproto_str[] = { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 764 | {RTPROT_REDIRECT, "redirect"}, |
| 765 | {RTPROT_KERNEL, "kernel"}, |
| 766 | {RTPROT_BOOT, "boot"}, |
| 767 | {RTPROT_STATIC, "static"}, |
| 768 | {RTPROT_GATED, "GateD"}, |
| 769 | {RTPROT_RA, "router advertisement"}, |
| 770 | {RTPROT_MRT, "MRT"}, |
| 771 | {RTPROT_ZEBRA, "Zebra"}, |
| 772 | #ifdef RTPROT_BIRD |
| 773 | {RTPROT_BIRD, "BIRD"}, |
| 774 | #endif /* RTPROT_BIRD */ |
| 775 | {0, NULL} |
| 776 | }; |
| 777 | |
| 778 | /* Routing information change from the kernel. */ |
| 779 | int |
| 780 | netlink_route_change (struct sockaddr_nl *snl, struct nlmsghdr *h) |
| 781 | { |
| 782 | int len; |
| 783 | struct rtmsg *rtm; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 784 | struct rtattr *tb[RTA_MAX + 1]; |
| 785 | |
| 786 | char anyaddr[16] = { 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 787 | |
| 788 | int index; |
| 789 | int table; |
| 790 | void *dest; |
| 791 | void *gate; |
| 792 | |
| 793 | rtm = NLMSG_DATA (h); |
| 794 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 795 | if (!(h->nlmsg_type == RTM_NEWROUTE || h->nlmsg_type == RTM_DELROUTE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 796 | { |
| 797 | /* If this is not route add/delete message print warning. */ |
| 798 | zlog_warn ("Kernel message: %d\n", h->nlmsg_type); |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | /* Connected route. */ |
| 803 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 804 | zlog_info ("%s %s %s proto %s", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 805 | h->nlmsg_type == |
| 806 | RTM_NEWROUTE ? "RTM_NEWROUTE" : "RTM_DELROUTE", |
| 807 | rtm->rtm_family == AF_INET ? "ipv4" : "ipv6", |
| 808 | rtm->rtm_type == RTN_UNICAST ? "unicast" : "multicast", |
| 809 | lookup (rtproto_str, rtm->rtm_protocol)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 810 | |
| 811 | if (rtm->rtm_type != RTN_UNICAST) |
| 812 | { |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | table = rtm->rtm_table; |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 817 | if (table != RT_TABLE_MAIN && table != zebrad.rtm_table_default) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 818 | { |
| 819 | return 0; |
| 820 | } |
| 821 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 822 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct rtmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | if (len < 0) |
| 824 | return -1; |
| 825 | |
| 826 | memset (tb, 0, sizeof tb); |
| 827 | netlink_parse_rtattr (tb, RTA_MAX, RTM_RTA (rtm), len); |
| 828 | |
| 829 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 830 | return 0; |
| 831 | if (rtm->rtm_protocol == RTPROT_REDIRECT) |
| 832 | return 0; |
| 833 | if (rtm->rtm_protocol == RTPROT_KERNEL) |
| 834 | return 0; |
| 835 | |
| 836 | if (rtm->rtm_protocol == RTPROT_ZEBRA && h->nlmsg_type == RTM_NEWROUTE) |
| 837 | return 0; |
| 838 | |
| 839 | if (rtm->rtm_src_len != 0) |
| 840 | { |
| 841 | zlog_warn ("netlink_route_change(): no src len"); |
| 842 | return 0; |
| 843 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 844 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 845 | index = 0; |
| 846 | dest = NULL; |
| 847 | gate = NULL; |
| 848 | |
| 849 | if (tb[RTA_OIF]) |
| 850 | index = *(int *) RTA_DATA (tb[RTA_OIF]); |
| 851 | |
| 852 | if (tb[RTA_DST]) |
| 853 | dest = RTA_DATA (tb[RTA_DST]); |
| 854 | else |
| 855 | dest = anyaddr; |
| 856 | |
| 857 | if (tb[RTA_GATEWAY]) |
| 858 | gate = RTA_DATA (tb[RTA_GATEWAY]); |
| 859 | |
| 860 | if (rtm->rtm_family == AF_INET) |
| 861 | { |
| 862 | struct prefix_ipv4 p; |
| 863 | p.family = AF_INET; |
| 864 | memcpy (&p.prefix, dest, 4); |
| 865 | p.prefixlen = rtm->rtm_dst_len; |
| 866 | |
| 867 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 868 | { |
| 869 | if (h->nlmsg_type == RTM_NEWROUTE) |
| 870 | zlog_info ("RTM_NEWROUTE %s/%d", |
| 871 | inet_ntoa (p.prefix), p.prefixlen); |
| 872 | else |
| 873 | zlog_info ("RTM_DELROUTE %s/%d", |
| 874 | inet_ntoa (p.prefix), p.prefixlen); |
| 875 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 876 | |
| 877 | if (h->nlmsg_type == RTM_NEWROUTE) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 878 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, 0, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 879 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 880 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | #ifdef HAVE_IPV6 |
| 884 | if (rtm->rtm_family == AF_INET6) |
| 885 | { |
| 886 | struct prefix_ipv6 p; |
| 887 | char buf[BUFSIZ]; |
| 888 | |
| 889 | p.family = AF_INET6; |
| 890 | memcpy (&p.prefix, dest, 16); |
| 891 | p.prefixlen = rtm->rtm_dst_len; |
| 892 | |
| 893 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 894 | { |
| 895 | if (h->nlmsg_type == RTM_NEWROUTE) |
| 896 | zlog_info ("RTM_NEWROUTE %s/%d", |
| 897 | inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ), |
| 898 | p.prefixlen); |
| 899 | else |
| 900 | zlog_info ("RTM_DELROUTE %s/%d", |
| 901 | inet_ntop (AF_INET6, &p.prefix, buf, BUFSIZ), |
| 902 | p.prefixlen); |
| 903 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 904 | |
| 905 | if (h->nlmsg_type == RTM_NEWROUTE) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 906 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 907 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 908 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 909 | } |
| 910 | #endif /* HAVE_IPV6 */ |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | int |
| 916 | netlink_link_change (struct sockaddr_nl *snl, struct nlmsghdr *h) |
| 917 | { |
| 918 | int len; |
| 919 | struct ifinfomsg *ifi; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 920 | struct rtattr *tb[IFLA_MAX + 1]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 921 | struct interface *ifp; |
| 922 | char *name; |
| 923 | |
| 924 | ifi = NLMSG_DATA (h); |
| 925 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 926 | if (!(h->nlmsg_type == RTM_NEWLINK || h->nlmsg_type == RTM_DELLINK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 927 | { |
| 928 | /* If this is not link add/delete message so print warning. */ |
| 929 | zlog_warn ("netlink_link_change: wrong kernel message %d\n", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 930 | h->nlmsg_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg)); |
| 935 | if (len < 0) |
| 936 | return -1; |
| 937 | |
| 938 | /* Looking up interface name. */ |
| 939 | memset (tb, 0, sizeof tb); |
| 940 | netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len); |
| 941 | if (tb[IFLA_IFNAME] == NULL) |
| 942 | return -1; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 943 | name = (char *) RTA_DATA (tb[IFLA_IFNAME]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 944 | |
| 945 | /* Add interface. */ |
| 946 | if (h->nlmsg_type == RTM_NEWLINK) |
| 947 | { |
| 948 | ifp = if_lookup_by_name (name); |
| 949 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 950 | if (ifp == NULL || !CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 951 | { |
| 952 | if (ifp == NULL) |
| 953 | ifp = if_get_by_name (name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 954 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 955 | ifp->ifindex = ifi->ifi_index; |
| 956 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 957 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 958 | ifp->metric = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 959 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 960 | /* If new link is added. */ |
| 961 | if_add_update (ifp); |
| 962 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 963 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 964 | { |
| 965 | /* Interface status change. */ |
| 966 | ifp->ifindex = ifi->ifi_index; |
paul | 44145db | 2004-05-09 11:00:23 +0000 | [diff] [blame] | 967 | ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 968 | ifp->metric = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 969 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 970 | if (if_is_operative (ifp)) |
| 971 | { |
| 972 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
| 973 | if (!if_is_operative (ifp)) |
| 974 | if_down (ifp); |
| 975 | } |
| 976 | else |
| 977 | { |
| 978 | ifp->flags = ifi->ifi_flags & 0x0000fffff; |
| 979 | if (if_is_operative (ifp)) |
| 980 | if_up (ifp); |
| 981 | } |
| 982 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | } |
| 984 | else |
| 985 | { |
| 986 | /* RTM_DELLINK. */ |
| 987 | ifp = if_lookup_by_name (name); |
| 988 | |
| 989 | if (ifp == NULL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 990 | { |
| 991 | zlog (NULL, LOG_WARNING, "interface %s is deleted but can't find", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 992 | name); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 993 | return 0; |
| 994 | } |
| 995 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 996 | if_delete_update (ifp); |
| 997 | } |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | int |
| 1003 | netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h) |
| 1004 | { |
| 1005 | switch (h->nlmsg_type) |
| 1006 | { |
| 1007 | case RTM_NEWROUTE: |
| 1008 | return netlink_route_change (snl, h); |
| 1009 | break; |
| 1010 | case RTM_DELROUTE: |
| 1011 | return netlink_route_change (snl, h); |
| 1012 | break; |
| 1013 | case RTM_NEWLINK: |
| 1014 | return netlink_link_change (snl, h); |
| 1015 | break; |
| 1016 | case RTM_DELLINK: |
| 1017 | return netlink_link_change (snl, h); |
| 1018 | break; |
| 1019 | case RTM_NEWADDR: |
| 1020 | return netlink_interface_addr (snl, h); |
| 1021 | break; |
| 1022 | case RTM_DELADDR: |
| 1023 | return netlink_interface_addr (snl, h); |
| 1024 | break; |
| 1025 | default: |
| 1026 | zlog_warn ("Unknown netlink nlmsg_type %d\n", h->nlmsg_type); |
| 1027 | break; |
| 1028 | } |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | /* Interface lookup by netlink socket. */ |
| 1033 | int |
| 1034 | interface_lookup_netlink () |
| 1035 | { |
| 1036 | int ret; |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 1037 | int flags; |
| 1038 | int snb_ret; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1039 | |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 1040 | /* |
| 1041 | * Change netlink socket flags to blocking to ensure we get |
| 1042 | * a reply via nelink_parse_info |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1043 | */ |
| 1044 | snb_ret = set_netlink_blocking (&netlink_cmd, &flags); |
| 1045 | if (snb_ret < 0) |
| 1046 | zlog (NULL, LOG_WARNING, |
| 1047 | "%s:%i Warning: Could not set netlink socket to blocking.", |
| 1048 | __FUNCTION__, __LINE__); |
| 1049 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1050 | /* Get interface information. */ |
| 1051 | ret = netlink_request (AF_PACKET, RTM_GETLINK, &netlink_cmd); |
| 1052 | if (ret < 0) |
| 1053 | return ret; |
| 1054 | ret = netlink_parse_info (netlink_interface, &netlink_cmd); |
| 1055 | if (ret < 0) |
| 1056 | return ret; |
| 1057 | |
| 1058 | /* Get IPv4 address of the interfaces. */ |
| 1059 | ret = netlink_request (AF_INET, RTM_GETADDR, &netlink_cmd); |
| 1060 | if (ret < 0) |
| 1061 | return ret; |
| 1062 | ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd); |
| 1063 | if (ret < 0) |
| 1064 | return ret; |
| 1065 | |
| 1066 | #ifdef HAVE_IPV6 |
| 1067 | /* Get IPv6 address of the interfaces. */ |
| 1068 | ret = netlink_request (AF_INET6, RTM_GETADDR, &netlink_cmd); |
| 1069 | if (ret < 0) |
| 1070 | return ret; |
| 1071 | ret = netlink_parse_info (netlink_interface_addr, &netlink_cmd); |
| 1072 | if (ret < 0) |
| 1073 | return ret; |
| 1074 | #endif /* HAVE_IPV6 */ |
| 1075 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1076 | /* restore socket flags */ |
| 1077 | if (snb_ret == 0) |
| 1078 | set_netlink_nonblocking (&netlink_cmd, &flags); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | /* Routing table read function using netlink interface. Only called |
| 1083 | bootstrap time. */ |
| 1084 | int |
| 1085 | netlink_route_read () |
| 1086 | { |
| 1087 | int ret; |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 1088 | int flags; |
| 1089 | int snb_ret; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1090 | |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 1091 | /* |
| 1092 | * Change netlink socket flags to blocking to ensure we get |
| 1093 | * a reply via nelink_parse_info |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1094 | */ |
| 1095 | snb_ret = set_netlink_blocking (&netlink_cmd, &flags); |
| 1096 | if (snb_ret < 0) |
| 1097 | zlog (NULL, LOG_WARNING, |
| 1098 | "%s:%i Warning: Could not set netlink socket to blocking.", |
| 1099 | __FUNCTION__, __LINE__); |
| 1100 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1101 | /* Get IPv4 routing table. */ |
| 1102 | ret = netlink_request (AF_INET, RTM_GETROUTE, &netlink_cmd); |
| 1103 | if (ret < 0) |
| 1104 | return ret; |
| 1105 | ret = netlink_parse_info (netlink_routing_table, &netlink_cmd); |
| 1106 | if (ret < 0) |
| 1107 | return ret; |
| 1108 | |
| 1109 | #ifdef HAVE_IPV6 |
| 1110 | /* Get IPv6 routing table. */ |
| 1111 | ret = netlink_request (AF_INET6, RTM_GETROUTE, &netlink_cmd); |
| 1112 | if (ret < 0) |
| 1113 | return ret; |
| 1114 | ret = netlink_parse_info (netlink_routing_table, &netlink_cmd); |
| 1115 | if (ret < 0) |
| 1116 | return ret; |
| 1117 | #endif /* HAVE_IPV6 */ |
| 1118 | |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 1119 | /* restore flags */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1120 | if (snb_ret == 0) |
| 1121 | set_netlink_nonblocking (&netlink_cmd, &flags); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | /* Utility function comes from iproute2. |
| 1126 | Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ |
| 1127 | int |
| 1128 | addattr_l (struct nlmsghdr *n, int maxlen, int type, void *data, int alen) |
| 1129 | { |
| 1130 | int len; |
| 1131 | struct rtattr *rta; |
| 1132 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1133 | len = RTA_LENGTH (alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1134 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1135 | if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1136 | return -1; |
| 1137 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1138 | rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1139 | rta->rta_type = type; |
| 1140 | rta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1141 | memcpy (RTA_DATA (rta), data, alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1142 | n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | int |
| 1148 | rta_addattr_l (struct rtattr *rta, int maxlen, int type, void *data, int alen) |
| 1149 | { |
| 1150 | int len; |
| 1151 | struct rtattr *subrta; |
| 1152 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1153 | len = RTA_LENGTH (alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1154 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1155 | if (RTA_ALIGN (rta->rta_len) + len > maxlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1156 | return -1; |
| 1157 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1158 | subrta = (struct rtattr *) (((char *) rta) + RTA_ALIGN (rta->rta_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1159 | subrta->rta_type = type; |
| 1160 | subrta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1161 | memcpy (RTA_DATA (subrta), data, alen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1162 | rta->rta_len = NLMSG_ALIGN (rta->rta_len) + len; |
| 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | /* Utility function comes from iproute2. |
| 1168 | Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ |
| 1169 | int |
| 1170 | addattr32 (struct nlmsghdr *n, int maxlen, int type, int data) |
| 1171 | { |
| 1172 | int len; |
| 1173 | struct rtattr *rta; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1174 | |
| 1175 | len = RTA_LENGTH (4); |
| 1176 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1177 | if (NLMSG_ALIGN (n->nlmsg_len) + len > maxlen) |
| 1178 | return -1; |
| 1179 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1180 | rta = (struct rtattr *) (((char *) n) + NLMSG_ALIGN (n->nlmsg_len)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1181 | rta->rta_type = type; |
| 1182 | rta->rta_len = len; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1183 | memcpy (RTA_DATA (rta), &data, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1184 | n->nlmsg_len = NLMSG_ALIGN (n->nlmsg_len) + len; |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | static int |
| 1190 | netlink_talk_filter (struct sockaddr_nl *snl, struct nlmsghdr *h) |
| 1191 | { |
| 1192 | zlog_warn ("netlink_talk: ignoring message type 0x%04x", h->nlmsg_type); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | /* sendmsg() to netlink socket then recvmsg(). */ |
| 1197 | int |
| 1198 | netlink_talk (struct nlmsghdr *n, struct nlsock *nl) |
| 1199 | { |
| 1200 | int status; |
| 1201 | struct sockaddr_nl snl; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1202 | struct iovec iov = { (void *) n, n->nlmsg_len }; |
| 1203 | struct msghdr msg = { (void *) &snl, sizeof snl, &iov, 1, NULL, 0, 0 }; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1204 | int flags = 0; |
paul | 5f37d86 | 2003-04-19 00:11:28 +0000 | [diff] [blame] | 1205 | int snb_ret; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1206 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1207 | memset (&snl, 0, sizeof snl); |
| 1208 | snl.nl_family = AF_NETLINK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1209 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1210 | n->nlmsg_seq = ++netlink_cmd.seq; |
| 1211 | |
| 1212 | /* Request an acknowledgement by setting NLM_F_ACK */ |
| 1213 | n->nlmsg_flags |= NLM_F_ACK; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1214 | |
| 1215 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1216 | zlog_info ("netlink_talk: %s type %s(%u), seq=%u", netlink_cmd.name, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1217 | lookup (nlmsg_str, n->nlmsg_type), n->nlmsg_type, |
| 1218 | n->nlmsg_seq); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1219 | |
| 1220 | /* Send message to netlink interface. */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1221 | if (zserv_privs.change (ZPRIVS_RAISE)) |
| 1222 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1223 | status = sendmsg (nl->sock, &msg, 0); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1224 | if (zserv_privs.change (ZPRIVS_LOWER)) |
| 1225 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
| 1226 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1227 | if (status < 0) |
| 1228 | { |
| 1229 | zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s", |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1230 | strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1231 | return -1; |
| 1232 | } |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1233 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1234 | /* |
| 1235 | * Change socket flags for blocking I/O. |
| 1236 | * This ensures we wait for a reply in netlink_parse_info(). |
| 1237 | */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1238 | snb_ret = set_netlink_blocking (nl, &flags); |
| 1239 | if (snb_ret < 0) |
| 1240 | zlog (NULL, LOG_WARNING, |
| 1241 | "%s:%i Warning: Could not set netlink socket to blocking.", |
| 1242 | __FUNCTION__, __LINE__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* |
| 1245 | * Get reply from netlink socket. |
| 1246 | * The reply should either be an acknowlegement or an error. |
| 1247 | */ |
| 1248 | status = netlink_parse_info (netlink_talk_filter, nl); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1249 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1250 | /* Restore socket flags for nonblocking I/O */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1251 | if (snb_ret == 0) |
| 1252 | set_netlink_nonblocking (nl, &flags); |
| 1253 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1254 | return status; |
| 1255 | } |
| 1256 | |
| 1257 | /* Routing table change via netlink interface. */ |
| 1258 | int |
| 1259 | netlink_route (int cmd, int family, void *dest, int length, void *gate, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1260 | int index, int zebra_flags, int table) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1261 | { |
| 1262 | int ret; |
| 1263 | int bytelen; |
| 1264 | struct sockaddr_nl snl; |
| 1265 | int discard; |
| 1266 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1267 | struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1268 | { |
| 1269 | struct nlmsghdr n; |
| 1270 | struct rtmsg r; |
| 1271 | char buf[1024]; |
| 1272 | } req; |
| 1273 | |
| 1274 | memset (&req, 0, sizeof req); |
| 1275 | |
| 1276 | bytelen = (family == AF_INET ? 4 : 16); |
| 1277 | |
| 1278 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); |
| 1279 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; |
| 1280 | req.n.nlmsg_type = cmd; |
| 1281 | req.r.rtm_family = family; |
| 1282 | req.r.rtm_table = table; |
| 1283 | req.r.rtm_dst_len = length; |
| 1284 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 1285 | if ((zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 1286 | || (zebra_flags & ZEBRA_FLAG_REJECT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1287 | discard = 1; |
| 1288 | else |
| 1289 | discard = 0; |
| 1290 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1291 | if (cmd == RTM_NEWROUTE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1292 | { |
| 1293 | req.r.rtm_protocol = RTPROT_ZEBRA; |
| 1294 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 1295 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1296 | if (discard) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1297 | { |
| 1298 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 1299 | req.r.rtm_type = RTN_BLACKHOLE; |
| 1300 | else if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 1301 | req.r.rtm_type = RTN_UNREACHABLE; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1302 | else |
| 1303 | assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */ |
| 1304 | } |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1305 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1306 | req.r.rtm_type = RTN_UNICAST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | if (dest) |
| 1310 | addattr_l (&req.n, sizeof req, RTA_DST, dest, bytelen); |
| 1311 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1312 | if (!discard) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1313 | { |
| 1314 | if (gate) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1315 | addattr_l (&req.n, sizeof req, RTA_GATEWAY, gate, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1316 | if (index > 0) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1317 | addattr32 (&req.n, sizeof req, RTA_OIF, index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* Destination netlink address. */ |
| 1321 | memset (&snl, 0, sizeof snl); |
| 1322 | snl.nl_family = AF_NETLINK; |
| 1323 | |
| 1324 | /* Talk to netlink socket. */ |
| 1325 | ret = netlink_talk (&req.n, &netlink); |
| 1326 | if (ret < 0) |
| 1327 | return -1; |
| 1328 | |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* Routing table change via netlink interface. */ |
| 1333 | int |
| 1334 | netlink_route_multipath (int cmd, struct prefix *p, struct rib *rib, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1335 | int family) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1336 | { |
| 1337 | int bytelen; |
| 1338 | struct sockaddr_nl snl; |
| 1339 | struct nexthop *nexthop = NULL; |
| 1340 | int nexthop_num = 0; |
| 1341 | struct nlsock *nl; |
| 1342 | int discard; |
| 1343 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1344 | struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1345 | { |
| 1346 | struct nlmsghdr n; |
| 1347 | struct rtmsg r; |
| 1348 | char buf[1024]; |
| 1349 | } req; |
| 1350 | |
| 1351 | memset (&req, 0, sizeof req); |
| 1352 | |
| 1353 | bytelen = (family == AF_INET ? 4 : 16); |
| 1354 | |
| 1355 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg)); |
| 1356 | req.n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST; |
| 1357 | req.n.nlmsg_type = cmd; |
| 1358 | req.r.rtm_family = family; |
| 1359 | req.r.rtm_table = rib->table; |
| 1360 | req.r.rtm_dst_len = p->prefixlen; |
| 1361 | |
paul | 13766da | 2003-02-07 14:46:23 +0000 | [diff] [blame] | 1362 | #ifdef RTM_F_EQUALIZE |
| 1363 | req.r.rtm_flags |= RTM_F_EQUALIZE; |
| 1364 | #endif /* RTM_F_EQUALIZE */ |
| 1365 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1366 | if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1367 | discard = 1; |
| 1368 | else |
| 1369 | discard = 0; |
| 1370 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1371 | if (cmd == RTM_NEWROUTE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1372 | { |
| 1373 | req.r.rtm_protocol = RTPROT_ZEBRA; |
| 1374 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; |
| 1375 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1376 | if (discard) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1377 | { |
| 1378 | if (rib->flags & ZEBRA_FLAG_BLACKHOLE) |
| 1379 | req.r.rtm_type = RTN_BLACKHOLE; |
| 1380 | else if (rib->flags & ZEBRA_FLAG_REJECT) |
| 1381 | req.r.rtm_type = RTN_UNREACHABLE; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1382 | else |
| 1383 | assert (RTN_BLACKHOLE != RTN_UNREACHABLE); /* false */ |
| 1384 | } |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1385 | else |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1386 | req.r.rtm_type = RTN_UNICAST; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | addattr_l (&req.n, sizeof req, RTA_DST, &p->u.prefix, bytelen); |
| 1390 | |
| 1391 | /* Metric. */ |
| 1392 | addattr32 (&req.n, sizeof req, RTA_PRIORITY, rib->metric); |
| 1393 | |
| 1394 | if (discard) |
| 1395 | { |
| 1396 | if (cmd == RTM_NEWROUTE) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1397 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1398 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1399 | goto skip; |
| 1400 | } |
| 1401 | |
| 1402 | /* Multipath case. */ |
| 1403 | if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1) |
| 1404 | { |
| 1405 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1406 | { |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1407 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1408 | if ((cmd == RTM_NEWROUTE |
| 1409 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1410 | || (cmd == RTM_DELROUTE |
| 1411 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))) |
| 1412 | { |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1413 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1414 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1415 | { |
| 1416 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1417 | { |
| 1418 | zlog_info |
| 1419 | ("netlink_route_multipath() (recursive, 1 hop): " |
| 1420 | "%s %s/%d via %s if %u, type %s", |
| 1421 | lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4), |
| 1422 | p->prefixlen, inet_ntoa (nexthop->rgate.ipv4), |
| 1423 | nexthop->rifindex, |
| 1424 | nexthop_types_desc[nexthop->rtype]); |
| 1425 | } |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1426 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1427 | if (nexthop->rtype == NEXTHOP_TYPE_IPV4 |
| 1428 | || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1429 | addattr_l (&req.n, sizeof req, RTA_GATEWAY, |
| 1430 | &nexthop->rgate.ipv4, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1431 | #ifdef HAVE_IPV6 |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1432 | if (nexthop->rtype == NEXTHOP_TYPE_IPV6 |
| 1433 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX |
| 1434 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME) |
| 1435 | addattr_l (&req.n, sizeof req, RTA_GATEWAY, |
| 1436 | &nexthop->rgate.ipv6, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1437 | #endif /* HAVE_IPV6 */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1438 | if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX |
| 1439 | || nexthop->rtype == NEXTHOP_TYPE_IFNAME |
| 1440 | || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX |
| 1441 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX |
| 1442 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME) |
| 1443 | addattr32 (&req.n, sizeof req, RTA_OIF, |
| 1444 | nexthop->rifindex); |
| 1445 | } |
| 1446 | else |
| 1447 | { |
| 1448 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1449 | { |
| 1450 | zlog_info |
| 1451 | ("netlink_route_multipath(): (single hop)" |
| 1452 | "%s %s/%d via %s if %u, type %s", |
| 1453 | lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4), |
| 1454 | p->prefixlen, inet_ntoa (nexthop->gate.ipv4), |
| 1455 | nexthop->ifindex, |
| 1456 | nexthop_types_desc[nexthop->type]); |
| 1457 | } |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 1458 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1459 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 1460 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1461 | addattr_l (&req.n, sizeof req, RTA_GATEWAY, |
| 1462 | &nexthop->gate.ipv4, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1463 | #ifdef HAVE_IPV6 |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1464 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 1465 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1466 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1467 | addattr_l (&req.n, sizeof req, RTA_GATEWAY, |
| 1468 | &nexthop->gate.ipv6, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1469 | #endif /* HAVE_IPV6 */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1470 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1471 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 1472 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX |
| 1473 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 1474 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
| 1475 | addattr32 (&req.n, sizeof req, RTA_OIF, nexthop->ifindex); |
| 1476 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1477 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1478 | if (cmd == RTM_NEWROUTE) |
| 1479 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1480 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1481 | nexthop_num++; |
| 1482 | break; |
| 1483 | } |
| 1484 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1485 | } |
| 1486 | else |
| 1487 | { |
| 1488 | char buf[1024]; |
| 1489 | struct rtattr *rta = (void *) buf; |
| 1490 | struct rtnexthop *rtnh; |
| 1491 | |
| 1492 | rta->rta_type = RTA_MULTIPATH; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1493 | rta->rta_len = RTA_LENGTH (0); |
| 1494 | rtnh = RTA_DATA (rta); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1495 | |
| 1496 | nexthop_num = 0; |
| 1497 | for (nexthop = rib->nexthop; |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1498 | nexthop && (MULTIPATH_NUM == 0 || nexthop_num < MULTIPATH_NUM); |
| 1499 | nexthop = nexthop->next) |
| 1500 | { |
| 1501 | if ((cmd == RTM_NEWROUTE |
| 1502 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 1503 | || (cmd == RTM_DELROUTE |
| 1504 | && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))) |
| 1505 | { |
| 1506 | nexthop_num++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1507 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1508 | rtnh->rtnh_len = sizeof (*rtnh); |
| 1509 | rtnh->rtnh_flags = 0; |
| 1510 | rtnh->rtnh_hops = 0; |
| 1511 | rta->rta_len += rtnh->rtnh_len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1512 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1513 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 1514 | { |
| 1515 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1516 | { |
| 1517 | zlog_info ("netlink_route_multipath() " |
| 1518 | "(recursive, multihop): " |
| 1519 | "%s %s/%d via %s if %u, type %s", |
| 1520 | lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4), |
| 1521 | p->prefixlen, inet_ntoa (nexthop->rgate.ipv4), |
| 1522 | nexthop->rifindex, |
| 1523 | nexthop_types_desc[nexthop->type]); |
| 1524 | } |
| 1525 | if (nexthop->rtype == NEXTHOP_TYPE_IPV4 |
| 1526 | || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1527 | { |
| 1528 | rta_addattr_l (rta, 4096, RTA_GATEWAY, |
| 1529 | &nexthop->rgate.ipv4, bytelen); |
| 1530 | rtnh->rtnh_len += sizeof (struct rtattr) + 4; |
| 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_IFNAME |
| 1535 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1536 | rta_addattr_l (rta, 4096, RTA_GATEWAY, |
| 1537 | &nexthop->rgate.ipv6, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1538 | #endif /* HAVE_IPV6 */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1539 | /* ifindex */ |
| 1540 | if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX |
| 1541 | || nexthop->rtype == NEXTHOP_TYPE_IFNAME |
| 1542 | || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX |
| 1543 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX |
| 1544 | || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME) |
| 1545 | rtnh->rtnh_ifindex = nexthop->rifindex; |
| 1546 | else |
| 1547 | rtnh->rtnh_ifindex = 0; |
| 1548 | } |
| 1549 | else |
| 1550 | { |
| 1551 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 1552 | { |
| 1553 | zlog_info ("netlink_route_multipath() " |
| 1554 | "(multihop): " |
| 1555 | "%s %s/%d via %s if %u, type %s", |
| 1556 | lookup (nlmsg_str, cmd), inet_ntoa (p->u.prefix4), |
| 1557 | p->prefixlen, inet_ntoa (nexthop->rgate.ipv4), |
| 1558 | nexthop->rifindex, |
| 1559 | nexthop_types_desc[nexthop->type]); |
| 1560 | } |
| 1561 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 1562 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 1563 | { |
| 1564 | rta_addattr_l (rta, 4096, RTA_GATEWAY, |
| 1565 | &nexthop->gate.ipv4, bytelen); |
| 1566 | rtnh->rtnh_len += sizeof (struct rtattr) + 4; |
| 1567 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1568 | #ifdef HAVE_IPV6 |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1569 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 1570 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1571 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1572 | rta_addattr_l (rta, 4096, RTA_GATEWAY, |
| 1573 | &nexthop->gate.ipv6, bytelen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1574 | #endif /* HAVE_IPV6 */ |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1575 | /* ifindex */ |
| 1576 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
| 1577 | || nexthop->type == NEXTHOP_TYPE_IFNAME |
| 1578 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX |
| 1579 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 1580 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
| 1581 | rtnh->rtnh_ifindex = nexthop->ifindex; |
| 1582 | else |
| 1583 | rtnh->rtnh_ifindex = 0; |
| 1584 | } |
| 1585 | rtnh = RTNH_NEXT (rtnh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1586 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1587 | if (cmd == RTM_NEWROUTE) |
| 1588 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1589 | } |
| 1590 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1591 | |
| 1592 | if (rta->rta_len > RTA_LENGTH (0)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1593 | addattr_l (&req.n, 1024, RTA_MULTIPATH, RTA_DATA (rta), |
| 1594 | RTA_PAYLOAD (rta)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /* If there is no useful nexthop then return. */ |
| 1598 | if (nexthop_num == 0) |
| 1599 | { |
| 1600 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1601 | zlog_info ("netlink_route_multipath(): No useful nexthop."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1605 | skip: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1606 | |
| 1607 | /* Destination netlink address. */ |
| 1608 | memset (&snl, 0, sizeof snl); |
| 1609 | snl.nl_family = AF_NETLINK; |
| 1610 | |
| 1611 | if (family == AF_INET) |
| 1612 | nl = &netlink_cmd; |
| 1613 | else |
| 1614 | nl = &netlink; |
| 1615 | |
| 1616 | /* Talk to netlink socket. */ |
| 1617 | return netlink_talk (&req.n, nl); |
| 1618 | } |
| 1619 | |
| 1620 | int |
| 1621 | kernel_add_ipv4 (struct prefix *p, struct rib *rib) |
| 1622 | { |
| 1623 | return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET); |
| 1624 | } |
| 1625 | |
| 1626 | int |
| 1627 | kernel_delete_ipv4 (struct prefix *p, struct rib *rib) |
| 1628 | { |
| 1629 | return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET); |
| 1630 | } |
| 1631 | |
| 1632 | #ifdef HAVE_IPV6 |
| 1633 | int |
| 1634 | kernel_add_ipv6 (struct prefix *p, struct rib *rib) |
| 1635 | { |
| 1636 | return netlink_route_multipath (RTM_NEWROUTE, p, rib, AF_INET6); |
| 1637 | } |
| 1638 | |
| 1639 | int |
| 1640 | kernel_delete_ipv6 (struct prefix *p, struct rib *rib) |
| 1641 | { |
| 1642 | return netlink_route_multipath (RTM_DELROUTE, p, rib, AF_INET6); |
| 1643 | } |
| 1644 | |
| 1645 | /* Delete IPv6 route from the kernel. */ |
| 1646 | int |
| 1647 | kernel_delete_ipv6_old (struct prefix_ipv6 *dest, struct in6_addr *gate, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1648 | int index, int flags, int table) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1649 | { |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1650 | return netlink_route (RTM_DELROUTE, AF_INET6, &dest->prefix, |
| 1651 | dest->prefixlen, gate, index, flags, table); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1652 | } |
| 1653 | #endif /* HAVE_IPV6 */ |
| 1654 | |
| 1655 | /* Interface address modification. */ |
| 1656 | int |
| 1657 | netlink_address (int cmd, int family, struct interface *ifp, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1658 | struct connected *ifc) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1659 | { |
| 1660 | int bytelen; |
| 1661 | struct prefix *p; |
| 1662 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1663 | struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1664 | { |
| 1665 | struct nlmsghdr n; |
| 1666 | struct ifaddrmsg ifa; |
| 1667 | char buf[1024]; |
| 1668 | } req; |
| 1669 | |
| 1670 | p = ifc->address; |
| 1671 | memset (&req, 0, sizeof req); |
| 1672 | |
| 1673 | bytelen = (family == AF_INET ? 4 : 16); |
| 1674 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1675 | req.n.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1676 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 1677 | req.n.nlmsg_type = cmd; |
| 1678 | req.ifa.ifa_family = family; |
| 1679 | |
| 1680 | req.ifa.ifa_index = ifp->ifindex; |
| 1681 | req.ifa.ifa_prefixlen = p->prefixlen; |
| 1682 | |
| 1683 | addattr_l (&req.n, sizeof req, IFA_LOCAL, &p->u.prefix, bytelen); |
| 1684 | |
| 1685 | if (family == AF_INET && cmd == RTM_NEWADDR) |
| 1686 | { |
| 1687 | if (if_is_broadcast (ifp) && ifc->destination) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1688 | { |
| 1689 | p = ifc->destination; |
| 1690 | addattr_l (&req.n, sizeof req, IFA_BROADCAST, &p->u.prefix, |
| 1691 | bytelen); |
| 1692 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY)) |
| 1696 | SET_FLAG (req.ifa.ifa_flags, IFA_F_SECONDARY); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1697 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1698 | if (ifc->label) |
| 1699 | addattr_l (&req.n, sizeof req, IFA_LABEL, ifc->label, |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1700 | strlen (ifc->label) + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1701 | |
| 1702 | return netlink_talk (&req.n, &netlink_cmd); |
| 1703 | } |
| 1704 | |
| 1705 | int |
| 1706 | kernel_address_add_ipv4 (struct interface *ifp, struct connected *ifc) |
| 1707 | { |
| 1708 | return netlink_address (RTM_NEWADDR, AF_INET, ifp, ifc); |
| 1709 | } |
| 1710 | |
| 1711 | int |
| 1712 | kernel_address_delete_ipv4 (struct interface *ifp, struct connected *ifc) |
| 1713 | { |
| 1714 | return netlink_address (RTM_DELADDR, AF_INET, ifp, ifc); |
| 1715 | } |
| 1716 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1717 | |
| 1718 | extern struct thread_master *master; |
| 1719 | |
| 1720 | /* Kernel route reflection. */ |
| 1721 | int |
| 1722 | kernel_read (struct thread *thread) |
| 1723 | { |
| 1724 | int ret; |
| 1725 | int sock; |
| 1726 | |
| 1727 | sock = THREAD_FD (thread); |
| 1728 | ret = netlink_parse_info (netlink_information_fetch, &netlink); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1729 | thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1730 | |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | /* Exported interface function. This function simply calls |
| 1735 | netlink_socket (). */ |
| 1736 | void |
| 1737 | kernel_init () |
| 1738 | { |
| 1739 | unsigned long groups; |
| 1740 | |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1741 | groups = RTMGRP_LINK | RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_IFADDR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1742 | #ifdef HAVE_IPV6 |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1743 | groups |= RTMGRP_IPV6_ROUTE | RTMGRP_IPV6_IFADDR; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1744 | #endif /* HAVE_IPV6 */ |
| 1745 | netlink_socket (&netlink, groups); |
| 1746 | netlink_socket (&netlink_cmd, 0); |
| 1747 | |
| 1748 | /* Register kernel socket. */ |
| 1749 | if (netlink.sock > 0) |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1750 | thread_add_read (zebrad.master, kernel_read, NULL, netlink.sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1751 | } |