paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Kernel communication using routing socket. |
| 2 | * Copyright (C) 1999 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 | #include "if.h" |
| 25 | #include "prefix.h" |
| 26 | #include "sockunion.h" |
| 27 | #include "connected.h" |
| 28 | #include "memory.h" |
| 29 | #include "ioctl.h" |
| 30 | #include "log.h" |
| 31 | #include "str.h" |
| 32 | #include "table.h" |
| 33 | #include "rib.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 34 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include "zebra/interface.h" |
| 37 | #include "zebra/zserv.h" |
| 38 | #include "zebra/debug.h" |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 39 | #include "zebra/kernel_socket.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 41 | extern struct zebra_privs_t zserv_privs; |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 42 | extern struct zebra_t zebrad; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 43 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 44 | /* |
| 45 | * Given a sockaddr length, round it up to include pad bytes following |
| 46 | * it. Assumes the kernel pads to sizeof(long). |
| 47 | * |
| 48 | * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr |
| 49 | * length anyway (< sizeof (struct sockaddr)), so this shouldn't |
| 50 | * matter. |
| 51 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | #define ROUNDUP(a) \ |
| 53 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 54 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 55 | /* |
| 56 | * Given a pointer (sockaddr or void *), return the number of bytes |
| 57 | * taken up by the sockaddr and any padding needed for alignment. |
| 58 | */ |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 59 | #if defined(HAVE_SA_LEN) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 60 | #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 61 | #elif defined(HAVE_IPV6) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 62 | /* |
| 63 | * One would hope all fixed-size structure definitions are aligned, |
| 64 | * but round them up nonetheless. |
| 65 | */ |
| 66 | #define SAROUNDUP(X) \ |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 67 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 68 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 69 | (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \ |
| 70 | ROUNDUP(sizeof(struct sockaddr_in6)) : \ |
| 71 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
paul | c50ae8b | 2004-05-11 11:31:07 +0000 | [diff] [blame] | 72 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))) |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 73 | #else /* HAVE_IPV6 */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 74 | #define SAROUNDUP(X) \ |
paul | 30be802 | 2003-10-22 02:51:38 +0000 | [diff] [blame] | 75 | (((struct sockaddr *)(X))->sa_family == AF_INET ? \ |
| 76 | ROUNDUP(sizeof(struct sockaddr_in)):\ |
| 77 | (((struct sockaddr *)(X))->sa_family == AF_LINK ? \ |
| 78 | ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | #endif /* HAVE_SA_LEN */ |
| 80 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 81 | /* We use an additional pointer in following, pdest, rather than (DEST) |
| 82 | * directly, because gcc will warn if the macro is expanded and DEST is NULL, |
| 83 | * complaining that memcpy is being passed a NULL value, despite the fact |
| 84 | * the if (NULL) makes it impossible. |
| 85 | */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 86 | #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 87 | if ((RTMADDRS) & (RTA)) \ |
| 88 | { \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 89 | void *pdest = (DEST); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 90 | int len = SAROUNDUP ((PNT)); \ |
paul | ea6f82b | 2005-06-28 17:20:26 +0000 | [diff] [blame] | 91 | if ( ((DEST) != NULL) && \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 92 | af_check (((struct sockaddr *)(PNT))->sa_family)) \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 93 | memcpy (pdest, (PNT), len); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 94 | (PNT) += len; \ |
| 95 | } |
| 96 | #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \ |
| 97 | if ((RTMADDRS) & (RTA)) \ |
| 98 | { \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 99 | void *pdest = (DEST); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 100 | int len = SAROUNDUP ((PNT)); \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 101 | if ((DEST) != NULL) \ |
| 102 | memcpy (pdest, (PNT), len); \ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 103 | (PNT) += len; \ |
| 104 | } |
| 105 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 106 | #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \ |
| 107 | if ((RTMADDRS) & (RTA)) \ |
| 108 | { \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 109 | u_char *pdest = (u_char *) (DEST); \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 110 | int len = SAROUNDUP ((PNT)); \ |
| 111 | struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \ |
| 112 | if (IS_ZEBRA_DEBUG_KERNEL) \ |
| 113 | zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \ |
| 114 | __func__, sdl->sdl_nlen, sdl->sdl_alen); \ |
| 115 | if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \ |
| 116 | && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \ |
| 117 | { \ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 118 | memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \ |
| 119 | pdest[sdl->sdl_nlen] = '\0'; \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 120 | (LEN) = sdl->sdl_nlen; \ |
| 121 | } \ |
| 122 | (PNT) += len; \ |
| 123 | } \ |
| 124 | else \ |
| 125 | { \ |
| 126 | (LEN) = 0; \ |
| 127 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | /* Routing socket message types. */ |
| 129 | struct message rtm_type_str[] = |
| 130 | { |
| 131 | {RTM_ADD, "RTM_ADD"}, |
| 132 | {RTM_DELETE, "RTM_DELETE"}, |
| 133 | {RTM_CHANGE, "RTM_CHANGE"}, |
| 134 | {RTM_GET, "RTM_GET"}, |
| 135 | {RTM_LOSING, "RTM_LOSING"}, |
| 136 | {RTM_REDIRECT, "RTM_REDIRECT"}, |
| 137 | {RTM_MISS, "RTM_MISS"}, |
| 138 | {RTM_LOCK, "RTM_LOCK"}, |
| 139 | {RTM_OLDADD, "RTM_OLDADD"}, |
| 140 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
| 141 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
| 142 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 143 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 144 | {RTM_IFINFO, "RTM_IFINFO"}, |
| 145 | #ifdef RTM_OIFINFO |
| 146 | {RTM_OIFINFO, "RTM_OIFINFO"}, |
| 147 | #endif /* RTM_OIFINFO */ |
| 148 | #ifdef RTM_NEWMADDR |
| 149 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, |
| 150 | #endif /* RTM_NEWMADDR */ |
| 151 | #ifdef RTM_DELMADDR |
| 152 | {RTM_DELMADDR, "RTM_DELMADDR"}, |
| 153 | #endif /* RTM_DELMADDR */ |
| 154 | #ifdef RTM_IFANNOUNCE |
| 155 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, |
| 156 | #endif /* RTM_IFANNOUNCE */ |
| 157 | {0, NULL} |
| 158 | }; |
| 159 | |
| 160 | struct message rtm_flag_str[] = |
| 161 | { |
| 162 | {RTF_UP, "UP"}, |
| 163 | {RTF_GATEWAY, "GATEWAY"}, |
| 164 | {RTF_HOST, "HOST"}, |
| 165 | {RTF_REJECT, "REJECT"}, |
| 166 | {RTF_DYNAMIC, "DYNAMIC"}, |
| 167 | {RTF_MODIFIED, "MODIFIED"}, |
| 168 | {RTF_DONE, "DONE"}, |
| 169 | #ifdef RTF_MASK |
| 170 | {RTF_MASK, "MASK"}, |
| 171 | #endif /* RTF_MASK */ |
| 172 | {RTF_CLONING, "CLONING"}, |
| 173 | {RTF_XRESOLVE, "XRESOLVE"}, |
| 174 | {RTF_LLINFO, "LLINFO"}, |
| 175 | {RTF_STATIC, "STATIC"}, |
| 176 | {RTF_BLACKHOLE, "BLACKHOLE"}, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 177 | #ifdef RTF_PRIVATE |
| 178 | {RTF_PRIVATE, "PRIVATE"}, |
| 179 | #endif /* RTF_PRIVATE */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 180 | {RTF_PROTO1, "PROTO1"}, |
| 181 | {RTF_PROTO2, "PROTO2"}, |
| 182 | #ifdef RTF_PRCLONING |
| 183 | {RTF_PRCLONING, "PRCLONING"}, |
| 184 | #endif /* RTF_PRCLONING */ |
| 185 | #ifdef RTF_WASCLONED |
| 186 | {RTF_WASCLONED, "WASCLONED"}, |
| 187 | #endif /* RTF_WASCLONED */ |
| 188 | #ifdef RTF_PROTO3 |
| 189 | {RTF_PROTO3, "PROTO3"}, |
| 190 | #endif /* RTF_PROTO3 */ |
| 191 | #ifdef RTF_PINNED |
| 192 | {RTF_PINNED, "PINNED"}, |
| 193 | #endif /* RTF_PINNED */ |
| 194 | #ifdef RTF_LOCAL |
| 195 | {RTF_LOCAL, "LOCAL"}, |
| 196 | #endif /* RTF_LOCAL */ |
| 197 | #ifdef RTF_BROADCAST |
| 198 | {RTF_BROADCAST, "BROADCAST"}, |
| 199 | #endif /* RTF_BROADCAST */ |
| 200 | #ifdef RTF_MULTICAST |
| 201 | {RTF_MULTICAST, "MULTICAST"}, |
| 202 | #endif /* RTF_MULTICAST */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 203 | #ifdef RTF_MULTIRT |
| 204 | {RTF_MULTIRT, "MULTIRT"}, |
| 205 | #endif /* RTF_MULTIRT */ |
| 206 | #ifdef RTF_SETSRC |
| 207 | {RTF_SETSRC, "SETSRC"}, |
| 208 | #endif /* RTF_SETSRC */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | {0, NULL} |
| 210 | }; |
| 211 | |
| 212 | /* Kernel routing update socket. */ |
| 213 | int routing_sock = -1; |
| 214 | |
| 215 | /* Yes I'm checking ugly routing socket behavior. */ |
| 216 | /* #define DEBUG */ |
| 217 | |
| 218 | /* Supported address family check. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 219 | static int inline |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | af_check (int family) |
| 221 | { |
| 222 | if (family == AF_INET) |
| 223 | return 1; |
| 224 | #ifdef HAVE_IPV6 |
| 225 | if (family == AF_INET6) |
| 226 | return 1; |
| 227 | #endif /* HAVE_IPV6 */ |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* Dump routing table flag for debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 232 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 233 | rtm_flag_dump (int flag) |
| 234 | { |
| 235 | struct message *mes; |
| 236 | static char buf[BUFSIZ]; |
| 237 | |
gdt | cced60d | 2004-07-13 16:45:54 +0000 | [diff] [blame] | 238 | buf[0] = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | for (mes = rtm_flag_str; mes->key != 0; mes++) |
| 240 | { |
| 241 | if (mes->key & flag) |
| 242 | { |
| 243 | strlcat (buf, mes->str, BUFSIZ); |
| 244 | strlcat (buf, " ", BUFSIZ); |
| 245 | } |
| 246 | } |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 247 | zlog_debug ("Kernel: %s", buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | #ifdef RTM_IFANNOUNCE |
| 251 | /* Interface adding function */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 252 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 253 | ifan_read (struct if_announcemsghdr *ifan) |
| 254 | { |
| 255 | struct interface *ifp; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 256 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 257 | ifp = if_lookup_by_index (ifan->ifan_index); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 258 | |
| 259 | if (ifp) |
| 260 | assert ( (ifp->ifindex == ifan->ifan_index) |
| 261 | || (ifp->ifindex == IFINDEX_INTERNAL) ); |
| 262 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 263 | if ( (ifp == NULL) |
| 264 | || ((ifp->ifindex == IFINDEX_INTERNAL) |
| 265 | && (ifan->ifan_what == IFAN_ARRIVAL)) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 267 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 268 | zlog_debug ("%s: creating interface for ifindex %d, name %s", |
| 269 | __func__, ifan->ifan_index, ifan->ifan_name); |
| 270 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | /* Create Interface */ |
ajs | 08dbfb6 | 2005-04-03 03:40:52 +0000 | [diff] [blame] | 272 | ifp = if_get_by_name_len(ifan->ifan_name, |
| 273 | strnlen(ifan->ifan_name, |
| 274 | sizeof(ifan->ifan_name))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 275 | ifp->ifindex = ifan->ifan_index; |
| 276 | |
| 277 | if_add_update (ifp); |
| 278 | } |
| 279 | else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 280 | if_delete_update (ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | |
| 282 | if_get_flags (ifp); |
| 283 | if_get_mtu (ifp); |
| 284 | if_get_metric (ifp); |
| 285 | |
| 286 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 287 | zlog_debug ("%s: interface %s index %d", |
| 288 | __func__, ifan->ifan_name, ifan->ifan_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | #endif /* RTM_IFANNOUNCE */ |
| 293 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 294 | /* |
| 295 | * Handle struct if_msghdr obtained from reading routing socket or |
| 296 | * sysctl (from interface_list). There may or may not be sockaddrs |
| 297 | * present after the header. |
| 298 | */ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 299 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 300 | ifm_read (struct if_msghdr *ifm) |
| 301 | { |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 302 | struct interface *ifp = NULL; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 303 | char ifname[IFNAMSIZ]; |
| 304 | short ifnlen = 0; |
paul | 0994c3a | 2005-11-11 09:52:40 +0000 | [diff] [blame] | 305 | caddr_t *cp; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 306 | |
| 307 | /* terminate ifname at head (for strnlen) and tail (for safety) */ |
| 308 | ifname[IFNAMSIZ - 1] = '\0'; |
| 309 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 310 | /* paranoia: sanity check structure */ |
| 311 | if (ifm->ifm_msglen < sizeof(struct if_msghdr)) |
| 312 | { |
| 313 | zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n", |
| 314 | ifm->ifm_msglen); |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 319 | * Check for a sockaddr_dl following the message. First, point to |
| 320 | * where a socakddr might be if one follows the message. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 321 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 322 | cp = (void *)(ifm + 1); |
| 323 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 324 | #ifdef SUNOS_5 |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 325 | /* |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 326 | * XXX This behavior should be narrowed to only the kernel versions |
| 327 | * for which the structures returned do not match the headers. |
| 328 | * |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 329 | * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 330 | * is 12 bytes larger than the 32 bit version. |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 331 | */ |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 332 | if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC) |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 333 | cp = cp + 12; |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 334 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 335 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 336 | RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp); |
| 337 | RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp); |
| 338 | RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp); |
| 339 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp); |
| 340 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen); |
| 341 | RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp); |
| 342 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp); |
| 343 | RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp); |
| 344 | |
| 345 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 346 | zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)")); |
| 347 | |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 348 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 349 | * Look up on ifindex first, because ifindices are the primary handle for |
| 350 | * interfaces across the user/kernel boundary, for most systems. (Some |
| 351 | * messages, such as up/down status changes on NetBSD, do not include a |
| 352 | * sockaddr_dl). |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 353 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 354 | if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL ) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 355 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 356 | /* we have an ifp, verify that the name matches as some systems, |
| 357 | * eg Solaris, have a 1:many association of ifindex:ifname |
| 358 | * if they dont match, we dont have the correct ifp and should |
| 359 | * set it back to NULL to let next check do lookup by name |
| 360 | */ |
| 361 | if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) ) |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 362 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 363 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 364 | zlog_debug ("%s: ifp name %s doesnt match sdl name %s", |
| 365 | __func__, ifp->name, ifname); |
| 366 | ifp = NULL; |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 369 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 370 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 371 | * If we dont have an ifp, try looking up by name. Particularly as some |
| 372 | * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname |
| 373 | * is therefore our unique handle to that interface. |
| 374 | * |
| 375 | * Interfaces specified in the configuration file for which the ifindex |
| 376 | * has not been determined will have ifindex == IFINDEX_INTERNAL, and such |
| 377 | * interfaces are found by this search, and then their ifindex values can |
| 378 | * be filled in. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 379 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 380 | if ( (ifp == NULL) && ifnlen) |
| 381 | ifp = if_lookup_by_name (ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 383 | /* |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 384 | * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL), |
| 385 | * create or fill in an interface. |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 386 | */ |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 387 | if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 388 | { |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 389 | /* |
| 390 | * To create or fill in an interface, a sockaddr_dl (via |
| 391 | * RTA_IFP) is required. |
| 392 | */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 393 | if (!ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 394 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 395 | zlog_warn ("Interface index %d (new) missing ifname\n", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 396 | ifm->ifm_index); |
| 397 | return -1; |
| 398 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 399 | |
paul | 3e95a07 | 2003-09-24 00:05:45 +0000 | [diff] [blame] | 400 | if (ifp == NULL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 401 | { |
| 402 | /* Interface that zebra was not previously aware of, so create. */ |
| 403 | ifp = if_create (ifname, ifnlen); |
| 404 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 405 | zlog_debug ("%s: creating ifp for ifindex %d", |
| 406 | __func__, ifm->ifm_index); |
| 407 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 408 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 409 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 410 | zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d", |
| 411 | __func__, ifp->name, ifp->ifindex); |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 412 | /* |
| 413 | * Fill in newly created interface structure, or larval |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 414 | * structure with ifindex IFINDEX_INTERNAL. |
gdt | 4bfbea8 | 2004-01-06 01:13:05 +0000 | [diff] [blame] | 415 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 416 | ifp->ifindex = ifm->ifm_index; |
| 417 | ifp->flags = ifm->ifm_flags; |
| 418 | #if defined(__bsdi__) |
| 419 | if_kvm_get_mtu (ifp); |
| 420 | #else |
| 421 | if_get_mtu (ifp); |
| 422 | #endif /* __bsdi__ */ |
| 423 | if_get_metric (ifp); |
| 424 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 425 | if_add_update (ifp); |
| 426 | } |
| 427 | else |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 428 | /* |
| 429 | * Interface structure exists. Adjust stored flags from |
| 430 | * notification. If interface has up->down or down->up |
| 431 | * transition, call state change routines (to adjust routes, |
| 432 | * notify routing daemons, etc.). (Other flag changes are stored |
| 433 | * but apparently do not trigger action.) |
| 434 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 435 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 436 | if (ifp->ifindex != ifm->ifm_index) |
| 437 | { |
| 438 | zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, " |
| 439 | "ifm index %d", |
| 440 | __func__, ifp->name, ifp->ifindex, ifm->ifm_index); |
| 441 | return -1; |
| 442 | } |
| 443 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | if (if_is_up (ifp)) |
| 445 | { |
| 446 | ifp->flags = ifm->ifm_flags; |
| 447 | if (! if_is_up (ifp)) |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 448 | { |
| 449 | if_down (ifp); |
| 450 | #ifndef RTM_IFANNOUNCE |
| 451 | /* No RTM_IFANNOUNCE on this platform, so we can never |
| 452 | * distinguish between down and delete. We must presume |
| 453 | * it has been deleted. |
| 454 | * Eg, Solaris will not notify us of unplumb. |
| 455 | * |
| 456 | * XXX: Fixme - this should be runtime detected |
| 457 | * So that a binary compiled on a system with IFANNOUNCE |
| 458 | * will still behave correctly if run on a platform without |
| 459 | */ |
| 460 | if_delete_update (ifp); |
| 461 | #endif /* RTM_IFANNOUNCE */ |
| 462 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 463 | } |
| 464 | else |
| 465 | { |
| 466 | ifp->flags = ifm->ifm_flags; |
| 467 | if (if_is_up (ifp)) |
| 468 | if_up (ifp); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | #ifdef HAVE_NET_RT_IFLIST |
| 473 | ifp->stats = ifm->ifm_data; |
| 474 | #endif /* HAVE_NET_RT_IFLIST */ |
| 475 | |
| 476 | if (IS_ZEBRA_DEBUG_KERNEL) |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 477 | zlog_debug ("%s: interface %s index %d", |
| 478 | __func__, ifp->name, ifp->ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* Address read from struct ifa_msghdr. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 484 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | ifam_read_mesg (struct ifa_msghdr *ifm, |
| 486 | union sockunion *addr, |
| 487 | union sockunion *mask, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 488 | union sockunion *brd, |
| 489 | char *ifname, |
| 490 | short *ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 491 | { |
| 492 | caddr_t pnt, end; |
| 493 | |
| 494 | pnt = (caddr_t)(ifm + 1); |
| 495 | end = ((caddr_t)ifm) + ifm->ifam_msglen; |
| 496 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 497 | /* Be sure structure is cleared */ |
| 498 | memset (mask, 0, sizeof (union sockunion)); |
| 499 | memset (addr, 0, sizeof (union sockunion)); |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 500 | memset (brd, 0, sizeof (union sockunion)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | |
| 502 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 503 | RTA_ADDR_GET (NULL, RTA_DST, ifm->ifam_addrs, pnt); |
| 504 | RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifam_addrs, pnt); |
| 505 | RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt); |
| 506 | RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 507 | RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 508 | RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt); |
| 509 | RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 510 | RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 511 | |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 512 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 513 | zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x", |
| 514 | __func__, ifm->ifam_index, |
| 515 | (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs); |
| 516 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 517 | /* Assert read up end point matches to end point */ |
| 518 | if (pnt != end) |
| 519 | zlog_warn ("ifam_read() does't read all socket data"); |
| 520 | } |
| 521 | |
| 522 | /* Interface's address information get. */ |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 523 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | ifam_read (struct ifa_msghdr *ifam) |
| 525 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 526 | struct interface *ifp = NULL; |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 527 | union sockunion addr, mask, brd; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 528 | char ifname[INTERFACE_NAMSIZ]; |
| 529 | short ifnlen = 0; |
| 530 | char isalias = 0; |
| 531 | |
| 532 | ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0'; |
| 533 | |
| 534 | /* Allocate and read address information. */ |
| 535 | ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen); |
| 536 | |
| 537 | if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 538 | { |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 539 | zlog_warn ("%s: no interface for ifname %s, index %d", |
| 540 | __func__, ifname, ifam->ifam_index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | return -1; |
| 542 | } |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 543 | |
| 544 | if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ)) |
| 545 | isalias = 1; |
| 546 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | /* Check interface flag for implicit up of the interface. */ |
| 548 | if_refresh (ifp); |
| 549 | |
| 550 | /* Add connected address. */ |
| 551 | switch (sockunion_family (&addr)) |
| 552 | { |
| 553 | case AF_INET: |
| 554 | if (ifam->ifam_type == RTM_NEWADDR) |
| 555 | connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 556 | ip_masklen (mask.sin.sin_addr), |
paul | 0994c3a | 2005-11-11 09:52:40 +0000 | [diff] [blame] | 557 | &brd.sin.sin_addr, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 558 | else |
| 559 | connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 560 | ip_masklen (mask.sin.sin_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 561 | &brd.sin.sin_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | break; |
| 563 | #ifdef HAVE_IPV6 |
| 564 | case AF_INET6: |
| 565 | /* Unset interface index from link-local address when IPv6 stack |
| 566 | is KAME. */ |
| 567 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) |
| 568 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); |
| 569 | |
| 570 | if (ifam->ifam_type == RTM_NEWADDR) |
| 571 | connected_add_ipv6 (ifp, |
| 572 | &addr.sin6.sin6_addr, |
| 573 | ip6_masklen (mask.sin6.sin6_addr), |
paul | 0994c3a | 2005-11-11 09:52:40 +0000 | [diff] [blame] | 574 | &brd.sin6.sin6_addr, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 575 | else |
| 576 | connected_delete_ipv6 (ifp, |
| 577 | &addr.sin6.sin6_addr, |
| 578 | ip6_masklen (mask.sin6.sin6_addr), |
paul | 0752ef0 | 2005-11-03 12:35:21 +0000 | [diff] [blame] | 579 | &brd.sin6.sin6_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 580 | break; |
| 581 | #endif /* HAVE_IPV6 */ |
| 582 | default: |
| 583 | /* Unsupported family silently ignore... */ |
| 584 | break; |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | /* Interface function for reading kernel routing table information. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 590 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 591 | rtm_read_mesg (struct rt_msghdr *rtm, |
| 592 | union sockunion *dest, |
| 593 | union sockunion *mask, |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 594 | union sockunion *gate, |
| 595 | char *ifname, |
| 596 | short *ifnlen) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 597 | { |
| 598 | caddr_t pnt, end; |
| 599 | |
| 600 | /* Pnt points out socket data start point. */ |
| 601 | pnt = (caddr_t)(rtm + 1); |
| 602 | end = ((caddr_t)rtm) + rtm->rtm_msglen; |
| 603 | |
| 604 | /* rt_msghdr version check. */ |
| 605 | if (rtm->rtm_version != RTM_VERSION) |
| 606 | zlog (NULL, LOG_WARNING, |
| 607 | "Routing message version different %d should be %d." |
| 608 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 609 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 610 | /* Be sure structure is cleared */ |
| 611 | memset (dest, 0, sizeof (union sockunion)); |
| 612 | memset (gate, 0, sizeof (union sockunion)); |
| 613 | memset (mask, 0, sizeof (union sockunion)); |
| 614 | |
| 615 | /* We fetch each socket variable into sockunion. */ |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 616 | RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt); |
| 617 | RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt); |
| 618 | RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt); |
| 619 | RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 620 | RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen); |
paul | 62debbb | 2005-06-14 14:07:07 +0000 | [diff] [blame] | 621 | RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt); |
| 622 | RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt); |
| 623 | RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | |
| 625 | /* If there is netmask information set it's family same as |
| 626 | destination family*/ |
| 627 | if (rtm->rtm_addrs & RTA_NETMASK) |
| 628 | mask->sa.sa_family = dest->sa.sa_family; |
| 629 | |
| 630 | /* Assert read up to the end of pointer. */ |
| 631 | if (pnt != end) |
| 632 | zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data."); |
| 633 | |
| 634 | return rtm->rtm_flags; |
| 635 | } |
| 636 | |
paul | ec1a428 | 2005-11-24 15:15:17 +0000 | [diff] [blame^] | 637 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 638 | rtm_read (struct rt_msghdr *rtm) |
| 639 | { |
| 640 | int flags; |
| 641 | u_char zebra_flags; |
| 642 | union sockunion dest, mask, gate; |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 643 | char ifname[INTERFACE_NAMSIZ + 1]; |
| 644 | short ifnlen = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 645 | |
| 646 | zebra_flags = 0; |
| 647 | |
| 648 | /* Discard self send message. */ |
| 649 | if (rtm->rtm_type != RTM_GET |
| 650 | && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid)) |
| 651 | return; |
| 652 | |
| 653 | /* Read destination and netmask and gateway from rtm message |
| 654 | structure. */ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 655 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 656 | |
| 657 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ |
| 658 | if (flags & RTF_CLONED) |
| 659 | return; |
| 660 | #endif |
| 661 | #ifdef RTF_WASCLONED /*freebsd*/ |
| 662 | if (flags & RTF_WASCLONED) |
| 663 | return; |
| 664 | #endif |
| 665 | |
| 666 | if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP)) |
| 667 | return; |
| 668 | |
| 669 | /* This is connected route. */ |
| 670 | if (! (flags & RTF_GATEWAY)) |
| 671 | return; |
| 672 | |
| 673 | if (flags & RTF_PROTO1) |
| 674 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); |
| 675 | |
| 676 | /* This is persistent route. */ |
| 677 | if (flags & RTF_STATIC) |
| 678 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); |
| 679 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 680 | /* This is a reject or blackhole route */ |
| 681 | if (flags & RTF_REJECT) |
| 682 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); |
| 683 | if (flags & RTF_BLACKHOLE) |
| 684 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); |
| 685 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 686 | if (dest.sa.sa_family == AF_INET) |
| 687 | { |
| 688 | struct prefix_ipv4 p; |
| 689 | |
| 690 | p.family = AF_INET; |
| 691 | p.prefix = dest.sin.sin_addr; |
| 692 | if (flags & RTF_HOST) |
| 693 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 694 | else |
| 695 | p.prefixlen = ip_masklen (mask.sin.sin_addr); |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 696 | |
| 697 | /* Change, delete the old prefix, we have no further information |
| 698 | * to specify the route really |
| 699 | */ |
| 700 | if (rtm->rtm_type == RTM_CHANGE) |
| 701 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
| 702 | NULL, 0, 0); |
| 703 | |
| 704 | if (rtm->rtm_type == RTM_GET |
| 705 | || rtm->rtm_type == RTM_ADD |
| 706 | || rtm->rtm_type == RTM_CHANGE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 707 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 708 | &p, &gate.sin.sin_addr, 0, 0, 0, 0); |
| 709 | else |
| 710 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 711 | &p, &gate.sin.sin_addr, 0, 0); |
| 712 | } |
| 713 | #ifdef HAVE_IPV6 |
| 714 | if (dest.sa.sa_family == AF_INET6) |
| 715 | { |
| 716 | struct prefix_ipv6 p; |
| 717 | unsigned int ifindex = 0; |
| 718 | |
| 719 | p.family = AF_INET6; |
| 720 | p.prefix = dest.sin6.sin6_addr; |
| 721 | if (flags & RTF_HOST) |
| 722 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 723 | else |
| 724 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); |
| 725 | |
| 726 | #ifdef KAME |
| 727 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) |
| 728 | { |
| 729 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); |
| 730 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); |
| 731 | } |
| 732 | #endif /* KAME */ |
| 733 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 734 | /* CHANGE: delete the old prefix, we have no further information |
| 735 | * to specify the route really |
| 736 | */ |
| 737 | if (rtm->rtm_type == RTM_CHANGE) |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 738 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p, |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 739 | NULL, 0, 0); |
| 740 | |
| 741 | if (rtm->rtm_type == RTM_GET |
| 742 | || rtm->rtm_type == RTM_ADD |
| 743 | || rtm->rtm_type == RTM_CHANGE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 744 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 745 | &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 746 | else |
| 747 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 748 | &p, &gate.sin6.sin6_addr, ifindex, 0); |
| 749 | } |
| 750 | #endif /* HAVE_IPV6 */ |
| 751 | } |
| 752 | |
| 753 | /* Interface function for the kernel routing table updates. Support |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 754 | * for RTM_CHANGE will be needed. |
| 755 | * Exported only for rt_socket.c |
| 756 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 757 | int |
| 758 | rtm_write (int message, |
| 759 | union sockunion *dest, |
| 760 | union sockunion *mask, |
| 761 | union sockunion *gate, |
| 762 | unsigned int index, |
| 763 | int zebra_flags, |
| 764 | int metric) |
| 765 | { |
| 766 | int ret; |
| 767 | caddr_t pnt; |
| 768 | struct interface *ifp; |
| 769 | struct sockaddr_in tmp_gate; |
| 770 | #ifdef HAVE_IPV6 |
| 771 | struct sockaddr_in6 tmp_gate6; |
| 772 | #endif /* HAVE_IPV6 */ |
| 773 | |
| 774 | /* Sequencial number of routing message. */ |
| 775 | static int msg_seq = 0; |
| 776 | |
| 777 | /* Struct of rt_msghdr and buffer for storing socket's data. */ |
| 778 | struct |
| 779 | { |
| 780 | struct rt_msghdr rtm; |
| 781 | char buf[512]; |
| 782 | } msg; |
| 783 | |
| 784 | memset (&tmp_gate, 0, sizeof (struct sockaddr_in)); |
| 785 | tmp_gate.sin_family = AF_INET; |
| 786 | #ifdef HAVE_SIN_LEN |
| 787 | tmp_gate.sin_len = sizeof (struct sockaddr_in); |
| 788 | #endif /* HAVE_SIN_LEN */ |
| 789 | |
| 790 | #ifdef HAVE_IPV6 |
| 791 | memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6)); |
| 792 | tmp_gate6.sin6_family = AF_INET6; |
| 793 | #ifdef SIN6_LEN |
| 794 | tmp_gate6.sin6_len = sizeof (struct sockaddr_in6); |
| 795 | #endif /* SIN6_LEN */ |
| 796 | #endif /* HAVE_IPV6 */ |
| 797 | |
| 798 | if (routing_sock < 0) |
| 799 | return ZEBRA_ERR_EPERM; |
| 800 | |
| 801 | /* Clear and set rt_msghdr values */ |
| 802 | memset (&msg, 0, sizeof (struct rt_msghdr)); |
| 803 | msg.rtm.rtm_version = RTM_VERSION; |
| 804 | msg.rtm.rtm_type = message; |
| 805 | msg.rtm.rtm_seq = msg_seq++; |
| 806 | msg.rtm.rtm_addrs = RTA_DST; |
| 807 | msg.rtm.rtm_addrs |= RTA_GATEWAY; |
| 808 | msg.rtm.rtm_flags = RTF_UP; |
| 809 | msg.rtm.rtm_index = index; |
| 810 | |
| 811 | if (metric != 0) |
| 812 | { |
| 813 | msg.rtm.rtm_rmx.rmx_hopcount = metric; |
| 814 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; |
| 815 | } |
| 816 | |
| 817 | ifp = if_lookup_by_index (index); |
| 818 | |
| 819 | if (gate && message == RTM_ADD) |
| 820 | msg.rtm.rtm_flags |= RTF_GATEWAY; |
| 821 | |
| 822 | if (! gate && message == RTM_ADD && ifp && |
| 823 | (ifp->flags & IFF_POINTOPOINT) == 0) |
| 824 | msg.rtm.rtm_flags |= RTF_CLONING; |
| 825 | |
| 826 | /* If no protocol specific gateway is specified, use link |
| 827 | address for gateway. */ |
| 828 | if (! gate) |
| 829 | { |
| 830 | if (!ifp) |
| 831 | { |
| 832 | zlog_warn ("no gateway found for interface index %d", index); |
| 833 | return -1; |
| 834 | } |
| 835 | gate = (union sockunion *) & ifp->sdl; |
| 836 | } |
| 837 | |
| 838 | if (mask) |
| 839 | msg.rtm.rtm_addrs |= RTA_NETMASK; |
| 840 | else if (message == RTM_ADD) |
| 841 | msg.rtm.rtm_flags |= RTF_HOST; |
| 842 | |
| 843 | /* Tagging route with flags */ |
| 844 | msg.rtm.rtm_flags |= (RTF_PROTO1); |
| 845 | |
| 846 | /* Additional flags. */ |
| 847 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 848 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 849 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 850 | msg.rtm.rtm_flags |= RTF_REJECT; |
| 851 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 852 | |
| 853 | #ifdef HAVE_SIN_LEN |
| 854 | #define SOCKADDRSET(X,R) \ |
| 855 | if (msg.rtm.rtm_addrs & (R)) \ |
| 856 | { \ |
| 857 | int len = ROUNDUP ((X)->sa.sa_len); \ |
| 858 | memcpy (pnt, (caddr_t)(X), len); \ |
| 859 | pnt += len; \ |
| 860 | } |
| 861 | #else |
| 862 | #define SOCKADDRSET(X,R) \ |
| 863 | if (msg.rtm.rtm_addrs & (R)) \ |
| 864 | { \ |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 865 | int len = SAROUNDUP (X); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 866 | memcpy (pnt, (caddr_t)(X), len); \ |
| 867 | pnt += len; \ |
| 868 | } |
| 869 | #endif /* HAVE_SIN_LEN */ |
| 870 | |
| 871 | pnt = (caddr_t) msg.buf; |
| 872 | |
| 873 | /* Write each socket data into rtm message buffer */ |
| 874 | SOCKADDRSET (dest, RTA_DST); |
| 875 | SOCKADDRSET (gate, RTA_GATEWAY); |
| 876 | SOCKADDRSET (mask, RTA_NETMASK); |
| 877 | |
| 878 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; |
| 879 | |
| 880 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); |
| 881 | |
| 882 | if (ret != msg.rtm.rtm_msglen) |
| 883 | { |
| 884 | if (errno == EEXIST) |
| 885 | return ZEBRA_ERR_RTEXIST; |
| 886 | if (errno == ENETUNREACH) |
| 887 | return ZEBRA_ERR_RTUNREACH; |
| 888 | |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 889 | zlog_warn ("write : %s (%d)", safe_strerror (errno), errno); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 890 | return -1; |
| 891 | } |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | |
| 896 | #include "thread.h" |
| 897 | #include "zebra/zserv.h" |
| 898 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 899 | /* For debug purpose. */ |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 900 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 901 | rtmsg_debug (struct rt_msghdr *rtm) |
| 902 | { |
gdt | 6a250b0 | 2004-12-09 14:48:12 +0000 | [diff] [blame] | 903 | const char *type = "Unknown"; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 904 | struct message *mes; |
| 905 | |
| 906 | for (mes = rtm_type_str; mes->str; mes++) |
| 907 | if (mes->key == rtm->rtm_type) |
| 908 | { |
| 909 | type = mes->str; |
| 910 | break; |
| 911 | } |
| 912 | |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 913 | zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 914 | rtm_flag_dump (rtm->rtm_flags); |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 915 | zlog_debug ("Kernel: message seq %d", rtm->rtm_seq); |
paul | 6fe70d1 | 2005-11-12 22:55:10 +0000 | [diff] [blame] | 916 | zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* This is pretty gross, better suggestions welcome -- mhandler */ |
| 920 | #ifndef RTAX_MAX |
| 921 | #ifdef RTA_NUMBITS |
| 922 | #define RTAX_MAX RTA_NUMBITS |
| 923 | #else |
| 924 | #define RTAX_MAX 8 |
| 925 | #endif /* RTA_NUMBITS */ |
| 926 | #endif /* RTAX_MAX */ |
| 927 | |
| 928 | /* Kernel routing table and interface updates via routing socket. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 929 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 930 | kernel_read (struct thread *thread) |
| 931 | { |
| 932 | int sock; |
| 933 | int nbytes; |
| 934 | struct rt_msghdr *rtm; |
| 935 | |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 936 | /* |
| 937 | * This must be big enough for any message the kernel might send. |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 938 | * Rather than determining how many sockaddrs of what size might be |
| 939 | * in each particular message, just use RTAX_MAX of sockaddr_storage |
| 940 | * for each. Note that the sockaddrs must be after each message |
| 941 | * definition, or rather after whichever happens to be the largest, |
| 942 | * since the buffer needs to be big enough for a message and the |
| 943 | * sockaddrs together. |
gdt | dbee01f | 2004-01-06 00:36:51 +0000 | [diff] [blame] | 944 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 945 | union |
| 946 | { |
| 947 | /* Routing information. */ |
| 948 | struct |
| 949 | { |
| 950 | struct rt_msghdr rtm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 951 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 952 | } r; |
| 953 | |
| 954 | /* Interface information. */ |
| 955 | struct |
| 956 | { |
| 957 | struct if_msghdr ifm; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 958 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 959 | } im; |
| 960 | |
| 961 | /* Interface address information. */ |
| 962 | struct |
| 963 | { |
| 964 | struct ifa_msghdr ifa; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 965 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 966 | } ia; |
| 967 | |
| 968 | #ifdef RTM_IFANNOUNCE |
| 969 | /* Interface arrival/departure */ |
| 970 | struct |
| 971 | { |
| 972 | struct if_announcemsghdr ifan; |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 973 | struct sockaddr_storage addr[RTAX_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 974 | } ian; |
| 975 | #endif /* RTM_IFANNOUNCE */ |
| 976 | |
| 977 | } buf; |
| 978 | |
| 979 | /* Fetch routing socket. */ |
| 980 | sock = THREAD_FD (thread); |
| 981 | |
| 982 | nbytes= read (sock, &buf, sizeof buf); |
| 983 | |
| 984 | if (nbytes <= 0) |
| 985 | { |
| 986 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 987 | zlog_warn ("routing socket error: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 988 | return 0; |
| 989 | } |
| 990 | |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 991 | thread_add_read (zebrad.master, kernel_read, NULL, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 992 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 993 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 994 | rtmsg_debug (&buf.r.rtm); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 995 | |
| 996 | rtm = &buf.r.rtm; |
| 997 | |
gdt | b27900b | 2004-01-08 15:44:29 +0000 | [diff] [blame] | 998 | /* |
| 999 | * Ensure that we didn't drop any data, so that processing routines |
| 1000 | * can assume they have the whole message. |
| 1001 | */ |
gdt | da26e3b | 2004-01-05 17:20:59 +0000 | [diff] [blame] | 1002 | if (rtm->rtm_msglen != nbytes) |
| 1003 | { |
| 1004 | zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n", |
| 1005 | rtm->rtm_msglen, nbytes, rtm->rtm_type); |
| 1006 | return -1; |
| 1007 | } |
| 1008 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1009 | switch (rtm->rtm_type) |
| 1010 | { |
| 1011 | case RTM_ADD: |
| 1012 | case RTM_DELETE: |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 1013 | case RTM_CHANGE: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1014 | rtm_read (rtm); |
| 1015 | break; |
| 1016 | case RTM_IFINFO: |
| 1017 | ifm_read (&buf.im.ifm); |
| 1018 | break; |
| 1019 | case RTM_NEWADDR: |
| 1020 | case RTM_DELADDR: |
| 1021 | ifam_read (&buf.ia.ifa); |
| 1022 | break; |
| 1023 | #ifdef RTM_IFANNOUNCE |
| 1024 | case RTM_IFANNOUNCE: |
| 1025 | ifan_read (&buf.ian.ifan); |
| 1026 | break; |
| 1027 | #endif /* RTM_IFANNOUNCE */ |
| 1028 | default: |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 1029 | if (IS_ZEBRA_DEBUG_KERNEL) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1030 | zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | break; |
| 1032 | } |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | /* Make routing socket. */ |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1037 | static void |
| 1038 | routing_socket (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1039 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1040 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 1041 | zlog_err ("routing_socket: Can't raise privileges"); |
| 1042 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1043 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
| 1044 | |
| 1045 | if (routing_sock < 0) |
| 1046 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1047 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1048 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1049 | zlog_warn ("Can't init kernel routing socket"); |
| 1050 | return; |
| 1051 | } |
| 1052 | |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1053 | /* XXX: Socket should be NONBLOCK, however as we currently |
| 1054 | * discard failed writes, this will lead to inconsistencies. |
| 1055 | * For now, socket must be blocking. |
| 1056 | */ |
| 1057 | /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) |
| 1058 | zlog_warn ("Can't set O_NONBLOCK to routing socket");*/ |
| 1059 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1060 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 1061 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1062 | |
| 1063 | /* kernel_read needs rewrite. */ |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 1064 | thread_add_read (zebrad.master, kernel_read, NULL, routing_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* Exported interface function. This function simply calls |
| 1068 | routing_socket (). */ |
| 1069 | void |
paul | 6621ca8 | 2005-11-23 13:02:08 +0000 | [diff] [blame] | 1070 | kernel_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1071 | { |
| 1072 | routing_socket (); |
| 1073 | } |