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