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