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" |
| 39 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 40 | extern struct zebra_privs_t zserv_privs; |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 41 | extern struct zebra_t zebrad; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 42 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | /* Socket length roundup function. */ |
| 44 | #define ROUNDUP(a) \ |
| 45 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
| 46 | |
| 47 | /* And this macro is wrapper for handling sa_len. */ |
| 48 | #ifdef HAVE_SA_LEN |
| 49 | #define WRAPUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len) |
| 50 | #else |
| 51 | #define WRAPUP(X) ROUNDUP(sizeof (struct sockaddr)) |
| 52 | #endif /* HAVE_SA_LEN */ |
| 53 | |
| 54 | /* Routing socket message types. */ |
| 55 | struct message rtm_type_str[] = |
| 56 | { |
| 57 | {RTM_ADD, "RTM_ADD"}, |
| 58 | {RTM_DELETE, "RTM_DELETE"}, |
| 59 | {RTM_CHANGE, "RTM_CHANGE"}, |
| 60 | {RTM_GET, "RTM_GET"}, |
| 61 | {RTM_LOSING, "RTM_LOSING"}, |
| 62 | {RTM_REDIRECT, "RTM_REDIRECT"}, |
| 63 | {RTM_MISS, "RTM_MISS"}, |
| 64 | {RTM_LOCK, "RTM_LOCK"}, |
| 65 | {RTM_OLDADD, "RTM_OLDADD"}, |
| 66 | {RTM_OLDDEL, "RTM_OLDDEL"}, |
| 67 | {RTM_RESOLVE, "RTM_RESOLVE"}, |
| 68 | {RTM_NEWADDR, "RTM_NEWADDR"}, |
| 69 | {RTM_DELADDR, "RTM_DELADDR"}, |
| 70 | {RTM_IFINFO, "RTM_IFINFO"}, |
| 71 | #ifdef RTM_OIFINFO |
| 72 | {RTM_OIFINFO, "RTM_OIFINFO"}, |
| 73 | #endif /* RTM_OIFINFO */ |
| 74 | #ifdef RTM_NEWMADDR |
| 75 | {RTM_NEWMADDR, "RTM_NEWMADDR"}, |
| 76 | #endif /* RTM_NEWMADDR */ |
| 77 | #ifdef RTM_DELMADDR |
| 78 | {RTM_DELMADDR, "RTM_DELMADDR"}, |
| 79 | #endif /* RTM_DELMADDR */ |
| 80 | #ifdef RTM_IFANNOUNCE |
| 81 | {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"}, |
| 82 | #endif /* RTM_IFANNOUNCE */ |
| 83 | {0, NULL} |
| 84 | }; |
| 85 | |
| 86 | struct message rtm_flag_str[] = |
| 87 | { |
| 88 | {RTF_UP, "UP"}, |
| 89 | {RTF_GATEWAY, "GATEWAY"}, |
| 90 | {RTF_HOST, "HOST"}, |
| 91 | {RTF_REJECT, "REJECT"}, |
| 92 | {RTF_DYNAMIC, "DYNAMIC"}, |
| 93 | {RTF_MODIFIED, "MODIFIED"}, |
| 94 | {RTF_DONE, "DONE"}, |
| 95 | #ifdef RTF_MASK |
| 96 | {RTF_MASK, "MASK"}, |
| 97 | #endif /* RTF_MASK */ |
| 98 | {RTF_CLONING, "CLONING"}, |
| 99 | {RTF_XRESOLVE, "XRESOLVE"}, |
| 100 | {RTF_LLINFO, "LLINFO"}, |
| 101 | {RTF_STATIC, "STATIC"}, |
| 102 | {RTF_BLACKHOLE, "BLACKHOLE"}, |
| 103 | {RTF_PROTO1, "PROTO1"}, |
| 104 | {RTF_PROTO2, "PROTO2"}, |
| 105 | #ifdef RTF_PRCLONING |
| 106 | {RTF_PRCLONING, "PRCLONING"}, |
| 107 | #endif /* RTF_PRCLONING */ |
| 108 | #ifdef RTF_WASCLONED |
| 109 | {RTF_WASCLONED, "WASCLONED"}, |
| 110 | #endif /* RTF_WASCLONED */ |
| 111 | #ifdef RTF_PROTO3 |
| 112 | {RTF_PROTO3, "PROTO3"}, |
| 113 | #endif /* RTF_PROTO3 */ |
| 114 | #ifdef RTF_PINNED |
| 115 | {RTF_PINNED, "PINNED"}, |
| 116 | #endif /* RTF_PINNED */ |
| 117 | #ifdef RTF_LOCAL |
| 118 | {RTF_LOCAL, "LOCAL"}, |
| 119 | #endif /* RTF_LOCAL */ |
| 120 | #ifdef RTF_BROADCAST |
| 121 | {RTF_BROADCAST, "BROADCAST"}, |
| 122 | #endif /* RTF_BROADCAST */ |
| 123 | #ifdef RTF_MULTICAST |
| 124 | {RTF_MULTICAST, "MULTICAST"}, |
| 125 | #endif /* RTF_MULTICAST */ |
| 126 | {0, NULL} |
| 127 | }; |
| 128 | |
| 129 | /* Kernel routing update socket. */ |
| 130 | int routing_sock = -1; |
| 131 | |
| 132 | /* Yes I'm checking ugly routing socket behavior. */ |
| 133 | /* #define DEBUG */ |
| 134 | |
| 135 | /* Supported address family check. */ |
| 136 | static int |
| 137 | af_check (int family) |
| 138 | { |
| 139 | if (family == AF_INET) |
| 140 | return 1; |
| 141 | #ifdef HAVE_IPV6 |
| 142 | if (family == AF_INET6) |
| 143 | return 1; |
| 144 | #endif /* HAVE_IPV6 */ |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* Dump routing table flag for debug purpose. */ |
| 149 | void |
| 150 | rtm_flag_dump (int flag) |
| 151 | { |
| 152 | struct message *mes; |
| 153 | static char buf[BUFSIZ]; |
| 154 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 155 | buf[0] = '0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | for (mes = rtm_flag_str; mes->key != 0; mes++) |
| 157 | { |
| 158 | if (mes->key & flag) |
| 159 | { |
| 160 | strlcat (buf, mes->str, BUFSIZ); |
| 161 | strlcat (buf, " ", BUFSIZ); |
| 162 | } |
| 163 | } |
| 164 | zlog_info ("Kernel: %s", buf); |
| 165 | } |
| 166 | |
| 167 | #ifdef RTM_IFANNOUNCE |
| 168 | /* Interface adding function */ |
| 169 | int |
| 170 | ifan_read (struct if_announcemsghdr *ifan) |
| 171 | { |
| 172 | struct interface *ifp; |
| 173 | |
| 174 | ifp = if_lookup_by_index (ifan->ifan_index); |
| 175 | if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL) |
| 176 | { |
| 177 | /* Create Interface */ |
| 178 | ifp = if_get_by_name (ifan->ifan_name); |
| 179 | ifp->ifindex = ifan->ifan_index; |
| 180 | |
| 181 | if_add_update (ifp); |
| 182 | } |
| 183 | else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE) |
| 184 | { |
| 185 | if_delete_update (ifp); |
| 186 | if_delete (ifp); |
| 187 | } |
| 188 | |
| 189 | if_get_flags (ifp); |
| 190 | if_get_mtu (ifp); |
| 191 | if_get_metric (ifp); |
| 192 | |
| 193 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 194 | zlog_info ("interface %s index %d", ifp->name, ifp->ifindex); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | #endif /* RTM_IFANNOUNCE */ |
| 199 | |
| 200 | /* Interface adding function called from interface_list. */ |
| 201 | int |
| 202 | ifm_read (struct if_msghdr *ifm) |
| 203 | { |
| 204 | struct interface *ifp; |
| 205 | struct sockaddr_dl *sdl = NULL; |
| 206 | |
| 207 | sdl = (struct sockaddr_dl *)(ifm + 1); |
| 208 | |
| 209 | /* Use sdl index. */ |
| 210 | ifp = if_lookup_by_index (ifm->ifm_index); |
| 211 | |
| 212 | if (ifp == NULL) |
| 213 | { |
| 214 | /* Check interface's address.*/ |
| 215 | if (! (ifm->ifm_addrs & RTA_IFP)) |
| 216 | { |
| 217 | zlog_warn ("There must be RTA_IFP address for ifindex %d\n", |
| 218 | ifm->ifm_index); |
| 219 | return -1; |
| 220 | } |
| 221 | |
paul | 592c814 | 2003-06-06 23:24:55 +0000 | [diff] [blame] | 222 | ifp = if_create (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | |
paul | 592c814 | 2003-06-06 23:24:55 +0000 | [diff] [blame] | 224 | strncpy (ifp->name, sdl->sdl_data, sdl->sdl_nlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | ifp->ifindex = ifm->ifm_index; |
| 226 | ifp->flags = ifm->ifm_flags; |
| 227 | #if defined(__bsdi__) |
| 228 | if_kvm_get_mtu (ifp); |
| 229 | #else |
| 230 | if_get_mtu (ifp); |
| 231 | #endif /* __bsdi__ */ |
| 232 | if_get_metric (ifp); |
| 233 | |
| 234 | /* Fetch hardware address. */ |
| 235 | if (sdl->sdl_family != AF_LINK) |
| 236 | { |
| 237 | zlog_warn ("sockaddr_dl->sdl_family is not AF_LINK"); |
| 238 | return -1; |
| 239 | } |
| 240 | memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl)); |
| 241 | |
| 242 | if_add_update (ifp); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | /* There is a case of promisc, allmulti flag modification. */ |
| 247 | if (if_is_up (ifp)) |
| 248 | { |
| 249 | ifp->flags = ifm->ifm_flags; |
| 250 | if (! if_is_up (ifp)) |
| 251 | if_down (ifp); |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | ifp->flags = ifm->ifm_flags; |
| 256 | if (if_is_up (ifp)) |
| 257 | if_up (ifp); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | #ifdef HAVE_NET_RT_IFLIST |
| 262 | ifp->stats = ifm->ifm_data; |
| 263 | #endif /* HAVE_NET_RT_IFLIST */ |
| 264 | |
| 265 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 266 | zlog_info ("interface %s index %d", ifp->name, ifp->ifindex); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /* Address read from struct ifa_msghdr. */ |
| 272 | void |
| 273 | ifam_read_mesg (struct ifa_msghdr *ifm, |
| 274 | union sockunion *addr, |
| 275 | union sockunion *mask, |
| 276 | union sockunion *dest) |
| 277 | { |
| 278 | caddr_t pnt, end; |
| 279 | |
| 280 | pnt = (caddr_t)(ifm + 1); |
| 281 | end = ((caddr_t)ifm) + ifm->ifam_msglen; |
| 282 | |
| 283 | #define IFAMADDRGET(X,R) \ |
| 284 | if (ifm->ifam_addrs & (R)) \ |
| 285 | { \ |
| 286 | int len = WRAPUP(pnt); \ |
| 287 | if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \ |
| 288 | memcpy ((caddr_t)(X), pnt, len); \ |
| 289 | pnt += len; \ |
| 290 | } |
| 291 | #define IFAMMASKGET(X,R) \ |
| 292 | if (ifm->ifam_addrs & (R)) \ |
| 293 | { \ |
| 294 | int len = WRAPUP(pnt); \ |
| 295 | if ((X) != NULL) \ |
| 296 | memcpy ((caddr_t)(X), pnt, len); \ |
| 297 | pnt += len; \ |
| 298 | } |
| 299 | |
| 300 | /* Be sure structure is cleared */ |
| 301 | memset (mask, 0, sizeof (union sockunion)); |
| 302 | memset (addr, 0, sizeof (union sockunion)); |
| 303 | memset (dest, 0, sizeof (union sockunion)); |
| 304 | |
| 305 | /* We fetch each socket variable into sockunion. */ |
| 306 | IFAMADDRGET (NULL, RTA_DST); |
| 307 | IFAMADDRGET (NULL, RTA_GATEWAY); |
| 308 | IFAMMASKGET (mask, RTA_NETMASK); |
| 309 | IFAMADDRGET (NULL, RTA_GENMASK); |
| 310 | IFAMADDRGET (NULL, RTA_IFP); |
| 311 | IFAMADDRGET (addr, RTA_IFA); |
| 312 | IFAMADDRGET (NULL, RTA_AUTHOR); |
| 313 | IFAMADDRGET (dest, RTA_BRD); |
| 314 | |
| 315 | /* Assert read up end point matches to end point */ |
| 316 | if (pnt != end) |
| 317 | zlog_warn ("ifam_read() does't read all socket data"); |
| 318 | } |
| 319 | |
| 320 | /* Interface's address information get. */ |
| 321 | int |
| 322 | ifam_read (struct ifa_msghdr *ifam) |
| 323 | { |
| 324 | struct interface *ifp; |
| 325 | union sockunion addr, mask, gate; |
| 326 | |
| 327 | /* Check does this interface exist or not. */ |
| 328 | ifp = if_lookup_by_index (ifam->ifam_index); |
| 329 | if (ifp == NULL) |
| 330 | { |
| 331 | zlog_warn ("no interface for index %d", ifam->ifam_index); |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | /* Allocate and read address information. */ |
| 336 | ifam_read_mesg (ifam, &addr, &mask, &gate); |
| 337 | |
| 338 | /* Check interface flag for implicit up of the interface. */ |
| 339 | if_refresh (ifp); |
| 340 | |
| 341 | /* Add connected address. */ |
| 342 | switch (sockunion_family (&addr)) |
| 343 | { |
| 344 | case AF_INET: |
| 345 | if (ifam->ifam_type == RTM_NEWADDR) |
| 346 | connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 347 | ip_masklen (mask.sin.sin_addr), |
| 348 | &gate.sin.sin_addr, NULL); |
| 349 | else |
| 350 | connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr, |
| 351 | ip_masklen (mask.sin.sin_addr), |
| 352 | &gate.sin.sin_addr, NULL); |
| 353 | break; |
| 354 | #ifdef HAVE_IPV6 |
| 355 | case AF_INET6: |
| 356 | /* Unset interface index from link-local address when IPv6 stack |
| 357 | is KAME. */ |
| 358 | if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr)) |
| 359 | SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0); |
| 360 | |
| 361 | if (ifam->ifam_type == RTM_NEWADDR) |
| 362 | connected_add_ipv6 (ifp, |
| 363 | &addr.sin6.sin6_addr, |
| 364 | ip6_masklen (mask.sin6.sin6_addr), |
| 365 | &gate.sin6.sin6_addr); |
| 366 | else |
| 367 | connected_delete_ipv6 (ifp, |
| 368 | &addr.sin6.sin6_addr, |
| 369 | ip6_masklen (mask.sin6.sin6_addr), |
| 370 | &gate.sin6.sin6_addr); |
| 371 | break; |
| 372 | #endif /* HAVE_IPV6 */ |
| 373 | default: |
| 374 | /* Unsupported family silently ignore... */ |
| 375 | break; |
| 376 | } |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* Interface function for reading kernel routing table information. */ |
| 381 | int |
| 382 | rtm_read_mesg (struct rt_msghdr *rtm, |
| 383 | union sockunion *dest, |
| 384 | union sockunion *mask, |
| 385 | union sockunion *gate) |
| 386 | { |
| 387 | caddr_t pnt, end; |
| 388 | |
| 389 | /* Pnt points out socket data start point. */ |
| 390 | pnt = (caddr_t)(rtm + 1); |
| 391 | end = ((caddr_t)rtm) + rtm->rtm_msglen; |
| 392 | |
| 393 | /* rt_msghdr version check. */ |
| 394 | if (rtm->rtm_version != RTM_VERSION) |
| 395 | zlog (NULL, LOG_WARNING, |
| 396 | "Routing message version different %d should be %d." |
| 397 | "This may cause problem\n", rtm->rtm_version, RTM_VERSION); |
| 398 | |
| 399 | #define RTMADDRGET(X,R) \ |
| 400 | if (rtm->rtm_addrs & (R)) \ |
| 401 | { \ |
| 402 | int len = WRAPUP (pnt); \ |
| 403 | if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \ |
| 404 | memcpy ((caddr_t)(X), pnt, len); \ |
| 405 | pnt += len; \ |
| 406 | } |
| 407 | #define RTMMASKGET(X,R) \ |
| 408 | if (rtm->rtm_addrs & (R)) \ |
| 409 | { \ |
| 410 | int len = WRAPUP (pnt); \ |
| 411 | if ((X) != NULL) \ |
| 412 | memcpy ((caddr_t)(X), pnt, len); \ |
| 413 | pnt += len; \ |
| 414 | } |
| 415 | |
| 416 | /* Be sure structure is cleared */ |
| 417 | memset (dest, 0, sizeof (union sockunion)); |
| 418 | memset (gate, 0, sizeof (union sockunion)); |
| 419 | memset (mask, 0, sizeof (union sockunion)); |
| 420 | |
| 421 | /* We fetch each socket variable into sockunion. */ |
| 422 | RTMADDRGET (dest, RTA_DST); |
| 423 | RTMADDRGET (gate, RTA_GATEWAY); |
| 424 | RTMMASKGET (mask, RTA_NETMASK); |
| 425 | RTMADDRGET (NULL, RTA_GENMASK); |
| 426 | RTMADDRGET (NULL, RTA_IFP); |
| 427 | RTMADDRGET (NULL, RTA_IFA); |
| 428 | RTMADDRGET (NULL, RTA_AUTHOR); |
| 429 | RTMADDRGET (NULL, RTA_BRD); |
| 430 | |
| 431 | /* If there is netmask information set it's family same as |
| 432 | destination family*/ |
| 433 | if (rtm->rtm_addrs & RTA_NETMASK) |
| 434 | mask->sa.sa_family = dest->sa.sa_family; |
| 435 | |
| 436 | /* Assert read up to the end of pointer. */ |
| 437 | if (pnt != end) |
| 438 | zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data."); |
| 439 | |
| 440 | return rtm->rtm_flags; |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | rtm_read (struct rt_msghdr *rtm) |
| 445 | { |
| 446 | int flags; |
| 447 | u_char zebra_flags; |
| 448 | union sockunion dest, mask, gate; |
| 449 | |
| 450 | zebra_flags = 0; |
| 451 | |
| 452 | /* Discard self send message. */ |
| 453 | if (rtm->rtm_type != RTM_GET |
| 454 | && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid)) |
| 455 | return; |
| 456 | |
| 457 | /* Read destination and netmask and gateway from rtm message |
| 458 | structure. */ |
| 459 | flags = rtm_read_mesg (rtm, &dest, &mask, &gate); |
| 460 | |
| 461 | #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/ |
| 462 | if (flags & RTF_CLONED) |
| 463 | return; |
| 464 | #endif |
| 465 | #ifdef RTF_WASCLONED /*freebsd*/ |
| 466 | if (flags & RTF_WASCLONED) |
| 467 | return; |
| 468 | #endif |
| 469 | |
| 470 | if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP)) |
| 471 | return; |
| 472 | |
| 473 | /* This is connected route. */ |
| 474 | if (! (flags & RTF_GATEWAY)) |
| 475 | return; |
| 476 | |
| 477 | if (flags & RTF_PROTO1) |
| 478 | SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE); |
| 479 | |
| 480 | /* This is persistent route. */ |
| 481 | if (flags & RTF_STATIC) |
| 482 | SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC); |
| 483 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 484 | /* This is a reject or blackhole route */ |
| 485 | if (flags & RTF_REJECT) |
| 486 | SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT); |
| 487 | if (flags & RTF_BLACKHOLE) |
| 488 | SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE); |
| 489 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 490 | if (dest.sa.sa_family == AF_INET) |
| 491 | { |
| 492 | struct prefix_ipv4 p; |
| 493 | |
| 494 | p.family = AF_INET; |
| 495 | p.prefix = dest.sin.sin_addr; |
| 496 | if (flags & RTF_HOST) |
| 497 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 498 | else |
| 499 | p.prefixlen = ip_masklen (mask.sin.sin_addr); |
| 500 | |
| 501 | if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD) |
| 502 | rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 503 | &p, &gate.sin.sin_addr, 0, 0, 0, 0); |
| 504 | else |
| 505 | rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 506 | &p, &gate.sin.sin_addr, 0, 0); |
| 507 | } |
| 508 | #ifdef HAVE_IPV6 |
| 509 | if (dest.sa.sa_family == AF_INET6) |
| 510 | { |
| 511 | struct prefix_ipv6 p; |
| 512 | unsigned int ifindex = 0; |
| 513 | |
| 514 | p.family = AF_INET6; |
| 515 | p.prefix = dest.sin6.sin6_addr; |
| 516 | if (flags & RTF_HOST) |
| 517 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 518 | else |
| 519 | p.prefixlen = ip6_masklen (mask.sin6.sin6_addr); |
| 520 | |
| 521 | #ifdef KAME |
| 522 | if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr)) |
| 523 | { |
| 524 | ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr); |
| 525 | SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0); |
| 526 | } |
| 527 | #endif /* KAME */ |
| 528 | |
| 529 | if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD) |
| 530 | rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 531 | &p, &gate.sin6.sin6_addr, ifindex, 0); |
| 532 | else |
| 533 | rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, |
| 534 | &p, &gate.sin6.sin6_addr, ifindex, 0); |
| 535 | } |
| 536 | #endif /* HAVE_IPV6 */ |
| 537 | } |
| 538 | |
| 539 | /* Interface function for the kernel routing table updates. Support |
| 540 | for RTM_CHANGE will be needed. */ |
| 541 | int |
| 542 | rtm_write (int message, |
| 543 | union sockunion *dest, |
| 544 | union sockunion *mask, |
| 545 | union sockunion *gate, |
| 546 | unsigned int index, |
| 547 | int zebra_flags, |
| 548 | int metric) |
| 549 | { |
| 550 | int ret; |
| 551 | caddr_t pnt; |
| 552 | struct interface *ifp; |
| 553 | struct sockaddr_in tmp_gate; |
| 554 | #ifdef HAVE_IPV6 |
| 555 | struct sockaddr_in6 tmp_gate6; |
| 556 | #endif /* HAVE_IPV6 */ |
| 557 | |
| 558 | /* Sequencial number of routing message. */ |
| 559 | static int msg_seq = 0; |
| 560 | |
| 561 | /* Struct of rt_msghdr and buffer for storing socket's data. */ |
| 562 | struct |
| 563 | { |
| 564 | struct rt_msghdr rtm; |
| 565 | char buf[512]; |
| 566 | } msg; |
| 567 | |
| 568 | memset (&tmp_gate, 0, sizeof (struct sockaddr_in)); |
| 569 | tmp_gate.sin_family = AF_INET; |
| 570 | #ifdef HAVE_SIN_LEN |
| 571 | tmp_gate.sin_len = sizeof (struct sockaddr_in); |
| 572 | #endif /* HAVE_SIN_LEN */ |
| 573 | |
| 574 | #ifdef HAVE_IPV6 |
| 575 | memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6)); |
| 576 | tmp_gate6.sin6_family = AF_INET6; |
| 577 | #ifdef SIN6_LEN |
| 578 | tmp_gate6.sin6_len = sizeof (struct sockaddr_in6); |
| 579 | #endif /* SIN6_LEN */ |
| 580 | #endif /* HAVE_IPV6 */ |
| 581 | |
| 582 | if (routing_sock < 0) |
| 583 | return ZEBRA_ERR_EPERM; |
| 584 | |
| 585 | /* Clear and set rt_msghdr values */ |
| 586 | memset (&msg, 0, sizeof (struct rt_msghdr)); |
| 587 | msg.rtm.rtm_version = RTM_VERSION; |
| 588 | msg.rtm.rtm_type = message; |
| 589 | msg.rtm.rtm_seq = msg_seq++; |
| 590 | msg.rtm.rtm_addrs = RTA_DST; |
| 591 | msg.rtm.rtm_addrs |= RTA_GATEWAY; |
| 592 | msg.rtm.rtm_flags = RTF_UP; |
| 593 | msg.rtm.rtm_index = index; |
| 594 | |
| 595 | if (metric != 0) |
| 596 | { |
| 597 | msg.rtm.rtm_rmx.rmx_hopcount = metric; |
| 598 | msg.rtm.rtm_inits |= RTV_HOPCOUNT; |
| 599 | } |
| 600 | |
| 601 | ifp = if_lookup_by_index (index); |
| 602 | |
| 603 | if (gate && message == RTM_ADD) |
| 604 | msg.rtm.rtm_flags |= RTF_GATEWAY; |
| 605 | |
| 606 | if (! gate && message == RTM_ADD && ifp && |
| 607 | (ifp->flags & IFF_POINTOPOINT) == 0) |
| 608 | msg.rtm.rtm_flags |= RTF_CLONING; |
| 609 | |
| 610 | /* If no protocol specific gateway is specified, use link |
| 611 | address for gateway. */ |
| 612 | if (! gate) |
| 613 | { |
| 614 | if (!ifp) |
| 615 | { |
| 616 | zlog_warn ("no gateway found for interface index %d", index); |
| 617 | return -1; |
| 618 | } |
| 619 | gate = (union sockunion *) & ifp->sdl; |
| 620 | } |
| 621 | |
| 622 | if (mask) |
| 623 | msg.rtm.rtm_addrs |= RTA_NETMASK; |
| 624 | else if (message == RTM_ADD) |
| 625 | msg.rtm.rtm_flags |= RTF_HOST; |
| 626 | |
| 627 | /* Tagging route with flags */ |
| 628 | msg.rtm.rtm_flags |= (RTF_PROTO1); |
| 629 | |
| 630 | /* Additional flags. */ |
| 631 | if (zebra_flags & ZEBRA_FLAG_BLACKHOLE) |
| 632 | msg.rtm.rtm_flags |= RTF_BLACKHOLE; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 633 | if (zebra_flags & ZEBRA_FLAG_REJECT) |
| 634 | msg.rtm.rtm_flags |= RTF_REJECT; |
| 635 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 636 | |
| 637 | #ifdef HAVE_SIN_LEN |
| 638 | #define SOCKADDRSET(X,R) \ |
| 639 | if (msg.rtm.rtm_addrs & (R)) \ |
| 640 | { \ |
| 641 | int len = ROUNDUP ((X)->sa.sa_len); \ |
| 642 | memcpy (pnt, (caddr_t)(X), len); \ |
| 643 | pnt += len; \ |
| 644 | } |
| 645 | #else |
| 646 | #define SOCKADDRSET(X,R) \ |
| 647 | if (msg.rtm.rtm_addrs & (R)) \ |
| 648 | { \ |
| 649 | int len = ROUNDUP (sizeof((X)->sa)); \ |
| 650 | memcpy (pnt, (caddr_t)(X), len); \ |
| 651 | pnt += len; \ |
| 652 | } |
| 653 | #endif /* HAVE_SIN_LEN */ |
| 654 | |
| 655 | pnt = (caddr_t) msg.buf; |
| 656 | |
| 657 | /* Write each socket data into rtm message buffer */ |
| 658 | SOCKADDRSET (dest, RTA_DST); |
| 659 | SOCKADDRSET (gate, RTA_GATEWAY); |
| 660 | SOCKADDRSET (mask, RTA_NETMASK); |
| 661 | |
| 662 | msg.rtm.rtm_msglen = pnt - (caddr_t) &msg; |
| 663 | |
| 664 | ret = write (routing_sock, &msg, msg.rtm.rtm_msglen); |
| 665 | |
| 666 | if (ret != msg.rtm.rtm_msglen) |
| 667 | { |
| 668 | if (errno == EEXIST) |
| 669 | return ZEBRA_ERR_RTEXIST; |
| 670 | if (errno == ENETUNREACH) |
| 671 | return ZEBRA_ERR_RTUNREACH; |
| 672 | |
| 673 | zlog_warn ("write : %s (%d)", strerror (errno), errno); |
| 674 | return -1; |
| 675 | } |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | |
| 680 | #include "thread.h" |
| 681 | #include "zebra/zserv.h" |
| 682 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | /* For debug purpose. */ |
| 684 | void |
| 685 | rtmsg_debug (struct rt_msghdr *rtm) |
| 686 | { |
| 687 | char *type = "Unknown"; |
| 688 | struct message *mes; |
| 689 | |
| 690 | for (mes = rtm_type_str; mes->str; mes++) |
| 691 | if (mes->key == rtm->rtm_type) |
| 692 | { |
| 693 | type = mes->str; |
| 694 | break; |
| 695 | } |
| 696 | |
| 697 | zlog_info ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type); |
| 698 | rtm_flag_dump (rtm->rtm_flags); |
| 699 | zlog_info ("Kernel: message seq %d", rtm->rtm_seq); |
| 700 | zlog_info ("Kernel: pid %d", rtm->rtm_pid); |
| 701 | } |
| 702 | |
| 703 | /* This is pretty gross, better suggestions welcome -- mhandler */ |
| 704 | #ifndef RTAX_MAX |
| 705 | #ifdef RTA_NUMBITS |
| 706 | #define RTAX_MAX RTA_NUMBITS |
| 707 | #else |
| 708 | #define RTAX_MAX 8 |
| 709 | #endif /* RTA_NUMBITS */ |
| 710 | #endif /* RTAX_MAX */ |
| 711 | |
| 712 | /* Kernel routing table and interface updates via routing socket. */ |
| 713 | int |
| 714 | kernel_read (struct thread *thread) |
| 715 | { |
| 716 | int sock; |
| 717 | int nbytes; |
| 718 | struct rt_msghdr *rtm; |
| 719 | |
| 720 | union |
| 721 | { |
| 722 | /* Routing information. */ |
| 723 | struct |
| 724 | { |
| 725 | struct rt_msghdr rtm; |
| 726 | struct sockaddr addr[RTAX_MAX]; |
| 727 | } r; |
| 728 | |
| 729 | /* Interface information. */ |
| 730 | struct |
| 731 | { |
| 732 | struct if_msghdr ifm; |
| 733 | struct sockaddr addr[RTAX_MAX]; |
| 734 | } im; |
| 735 | |
| 736 | /* Interface address information. */ |
| 737 | struct |
| 738 | { |
| 739 | struct ifa_msghdr ifa; |
| 740 | struct sockaddr addr[RTAX_MAX]; |
| 741 | } ia; |
| 742 | |
| 743 | #ifdef RTM_IFANNOUNCE |
| 744 | /* Interface arrival/departure */ |
| 745 | struct |
| 746 | { |
| 747 | struct if_announcemsghdr ifan; |
| 748 | struct sockaddr addr[RTAX_MAX]; |
| 749 | } ian; |
| 750 | #endif /* RTM_IFANNOUNCE */ |
| 751 | |
| 752 | } buf; |
| 753 | |
| 754 | /* Fetch routing socket. */ |
| 755 | sock = THREAD_FD (thread); |
| 756 | |
| 757 | nbytes= read (sock, &buf, sizeof buf); |
| 758 | |
| 759 | if (nbytes <= 0) |
| 760 | { |
| 761 | if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN) |
| 762 | zlog_warn ("routing socket error: %s", strerror (errno)); |
| 763 | return 0; |
| 764 | } |
| 765 | |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 766 | thread_add_read (zebrad.master, kernel_read, NULL, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 767 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 768 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 769 | rtmsg_debug (&buf.r.rtm); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 770 | |
| 771 | rtm = &buf.r.rtm; |
| 772 | |
| 773 | switch (rtm->rtm_type) |
| 774 | { |
| 775 | case RTM_ADD: |
| 776 | case RTM_DELETE: |
| 777 | rtm_read (rtm); |
| 778 | break; |
| 779 | case RTM_IFINFO: |
| 780 | ifm_read (&buf.im.ifm); |
| 781 | break; |
| 782 | case RTM_NEWADDR: |
| 783 | case RTM_DELADDR: |
| 784 | ifam_read (&buf.ia.ifa); |
| 785 | break; |
| 786 | #ifdef RTM_IFANNOUNCE |
| 787 | case RTM_IFANNOUNCE: |
| 788 | ifan_read (&buf.ian.ifan); |
| 789 | break; |
| 790 | #endif /* RTM_IFANNOUNCE */ |
| 791 | default: |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 792 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 793 | zlog_info("Unprocessed RTM_type: %d", rtm->rtm_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 794 | break; |
| 795 | } |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | /* Make routing socket. */ |
| 800 | void |
| 801 | routing_socket () |
| 802 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 803 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 804 | zlog_err ("routing_socket: Can't raise privileges"); |
| 805 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 806 | routing_sock = socket (AF_ROUTE, SOCK_RAW, 0); |
| 807 | |
| 808 | if (routing_sock < 0) |
| 809 | { |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 810 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 811 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 812 | zlog_warn ("Can't init kernel routing socket"); |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0) |
| 817 | zlog_warn ("Can't set O_NONBLOCK to routing socket"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 818 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 819 | zlog_err ("routing_socket: Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | |
| 821 | /* kernel_read needs rewrite. */ |
paul | 9bcdb63 | 2003-07-08 08:09:45 +0000 | [diff] [blame] | 822 | thread_add_read (zebrad.master, kernel_read, NULL, routing_sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | /* Exported interface function. This function simply calls |
| 826 | routing_socket (). */ |
| 827 | void |
| 828 | kernel_init () |
| 829 | { |
| 830 | routing_socket (); |
| 831 | } |