paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Address linked list routine. |
| 3 | * Copyright (C) 1997, 98 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "prefix.h" |
| 26 | #include "linklist.h" |
| 27 | #include "if.h" |
| 28 | #include "table.h" |
| 29 | #include "rib.h" |
| 30 | #include "table.h" |
| 31 | #include "log.h" |
| 32 | |
| 33 | #include "zebra/zserv.h" |
| 34 | #include "zebra/redistribute.h" |
hasso | eef1fe1 | 2004-10-03 18:46:08 +0000 | [diff] [blame] | 35 | #include "zebra/interface.h" |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 36 | #include "zebra/connected.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 38 | /* withdraw a connected address */ |
| 39 | static void |
| 40 | connected_withdraw (struct connected *ifc) |
| 41 | { |
| 42 | if (! ifc) |
| 43 | return; |
| 44 | |
| 45 | /* Update interface address information to protocol daemon. */ |
| 46 | if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)) |
| 47 | { |
| 48 | zebra_interface_address_delete_update (ifc->ifp, ifc); |
| 49 | |
| 50 | if_subnet_delete (ifc->ifp, ifc); |
| 51 | |
| 52 | if (ifc->address->family == AF_INET) |
| 53 | connected_down_ipv4 (ifc->ifp, ifc); |
vincent | aa2e32b | 2005-09-28 13:42:11 +0000 | [diff] [blame] | 54 | #ifdef HAVE_IPV6 |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 55 | else |
| 56 | connected_down_ipv6 (ifc->ifp, ifc); |
vincent | aa2e32b | 2005-09-28 13:42:11 +0000 | [diff] [blame] | 57 | #endif |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 58 | |
| 59 | UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL); |
| 60 | } |
| 61 | |
| 62 | listnode_delete (ifc->ifp->connected, ifc); |
| 63 | connected_free (ifc); |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | connected_announce (struct interface *ifp, struct connected *ifc) |
| 68 | { |
| 69 | if (!ifc) |
| 70 | return; |
| 71 | |
| 72 | listnode_add (ifp->connected, ifc); |
| 73 | |
| 74 | /* Update interface address information to protocol daemon. */ |
| 75 | if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)) |
| 76 | { |
| 77 | if (ifc->address->family == AF_INET) |
| 78 | if_subnet_add (ifp, ifc); |
| 79 | |
| 80 | SET_FLAG (ifc->conf, ZEBRA_IFC_REAL); |
| 81 | |
| 82 | zebra_interface_address_add_update (ifp, ifc); |
| 83 | |
| 84 | if (if_is_up(ifp)) |
| 85 | { |
| 86 | if (ifc->address->family == AF_INET) |
| 87 | connected_up_ipv4 (ifp, ifc); |
vincent | aa2e32b | 2005-09-28 13:42:11 +0000 | [diff] [blame] | 88 | #ifdef HAVE_IPV6 |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 89 | else |
| 90 | connected_up_ipv6 (ifp, ifc); |
vincent | aa2e32b | 2005-09-28 13:42:11 +0000 | [diff] [blame] | 91 | #endif |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | /* If same interface address is already exist... */ |
| 97 | struct connected * |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 98 | connected_check (struct interface *ifp, struct prefix *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | { |
| 100 | struct connected *ifc; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 101 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 103 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc)) |
| 104 | if (prefix_same (ifc->address, p)) |
| 105 | return ifc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | /* Called from if_up(). */ |
| 111 | void |
| 112 | connected_up_ipv4 (struct interface *ifp, struct connected *ifc) |
| 113 | { |
| 114 | struct prefix_ipv4 p; |
| 115 | struct prefix_ipv4 *addr; |
| 116 | struct prefix_ipv4 *dest; |
| 117 | |
| 118 | if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)) |
| 119 | return; |
| 120 | |
| 121 | addr = (struct prefix_ipv4 *) ifc->address; |
| 122 | dest = (struct prefix_ipv4 *) ifc->destination; |
| 123 | |
| 124 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 125 | p.family = AF_INET; |
| 126 | p.prefixlen = addr->prefixlen; |
| 127 | |
| 128 | /* Point-to-point check. */ |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 129 | if (CONNECTED_POINTOPOINT_HOST(ifc)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | p.prefix = dest->prefix; |
| 131 | else |
| 132 | p.prefix = addr->prefix; |
| 133 | |
| 134 | /* Apply mask to the network. */ |
| 135 | apply_mask_ipv4 (&p); |
| 136 | |
| 137 | /* In case of connected address is 0.0.0.0/0 we treat it tunnel |
| 138 | address. */ |
| 139 | if (prefix_ipv4_any (&p)) |
| 140 | return; |
| 141 | |
| 142 | rib_add_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, 0, 0); |
| 143 | |
| 144 | rib_update (); |
| 145 | } |
| 146 | |
| 147 | /* Add connected IPv4 route to the interface. */ |
| 148 | void |
| 149 | connected_add_ipv4 (struct interface *ifp, int flags, struct in_addr *addr, |
paul | d06b2a6 | 2005-10-11 03:53:54 +0000 | [diff] [blame^] | 150 | u_char prefixlen, struct in_addr *broad, |
| 151 | const char *label) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | { |
| 153 | struct prefix_ipv4 *p; |
| 154 | struct connected *ifc; |
| 155 | struct connected *current; |
| 156 | |
| 157 | /* Make connected structure. */ |
| 158 | ifc = connected_new (); |
| 159 | ifc->ifp = ifp; |
| 160 | ifc->flags = flags; |
| 161 | |
| 162 | /* Allocate new connected address. */ |
| 163 | p = prefix_ipv4_new (); |
| 164 | p->family = AF_INET; |
| 165 | p->prefix = *addr; |
| 166 | p->prefixlen = prefixlen; |
| 167 | ifc->address = (struct prefix *) p; |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 168 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 169 | /* If there is broadcast or pointopoint address. */ |
| 170 | if (broad) |
| 171 | { |
| 172 | p = prefix_ipv4_new (); |
| 173 | p->family = AF_INET; |
| 174 | p->prefix = *broad; |
| 175 | ifc->destination = (struct prefix *) p; |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 176 | |
| 177 | /* validate the destination address */ |
| 178 | if (ifp->flags & IFF_POINTOPOINT) |
| 179 | { |
| 180 | if (IPV4_ADDR_SAME(addr,broad)) |
| 181 | zlog_warn("warning: PtP interface %s has same local and peer " |
| 182 | "address %s, routing protocols may malfunction", |
| 183 | ifp->name,inet_ntoa(*addr)); |
| 184 | else if ((prefixlen != IPV4_MAX_PREFIXLEN) && |
| 185 | (ipv4_network_addr(addr->s_addr,prefixlen) != |
| 186 | ipv4_network_addr(broad->s_addr,prefixlen))) |
| 187 | { |
| 188 | char buf[2][INET_ADDRSTRLEN]; |
| 189 | zlog_warn("warning: PtP interface %s network mismatch: local " |
| 190 | "%s/%d vs. peer %s, routing protocols may malfunction", |
| 191 | ifp->name, |
| 192 | inet_ntop (AF_INET, addr, buf[0], sizeof(buf[0])), |
| 193 | prefixlen, |
| 194 | inet_ntop (AF_INET, broad, buf[1], sizeof(buf[1]))); |
| 195 | } |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | if (broad->s_addr != ipv4_broadcast_addr(addr->s_addr,prefixlen)) |
| 200 | { |
| 201 | char buf[2][INET_ADDRSTRLEN]; |
| 202 | struct in_addr bcalc; |
| 203 | bcalc.s_addr = ipv4_broadcast_addr(addr->s_addr,prefixlen); |
| 204 | zlog_warn("warning: interface %s broadcast addr %s/%d != " |
| 205 | "calculated %s, routing protocols may malfunction", |
| 206 | ifp->name, |
| 207 | inet_ntop (AF_INET, broad, buf[0], sizeof(buf[0])), |
| 208 | prefixlen, |
| 209 | inet_ntop (AF_INET, &bcalc, buf[1], sizeof(buf[1]))); |
| 210 | } |
| 211 | } |
| 212 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | } |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 214 | else |
| 215 | /* no broadcast or destination address was supplied */ |
ajs | 341a8f1 | 2004-12-22 16:32:16 +0000 | [diff] [blame] | 216 | if ((prefixlen == IPV4_MAX_PREFIXLEN) && if_is_pointopoint(ifp)) |
| 217 | zlog_warn("warning: PtP interface %s with addr %s/%d needs a " |
| 218 | "peer address",ifp->name,inet_ntoa(*addr),prefixlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | |
| 220 | /* Label of this address. */ |
| 221 | if (label) |
| 222 | ifc->label = strdup (label); |
| 223 | |
| 224 | /* Check same connected route. */ |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 225 | if ((current = connected_check (ifp, (struct prefix *) ifc->address))) |
| 226 | connected_withdraw (current); /* implicit withdraw - freebsd does this */ |
| 227 | |
| 228 | connected_announce (ifp, ifc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void |
| 232 | connected_down_ipv4 (struct interface *ifp, struct connected *ifc) |
| 233 | { |
| 234 | struct prefix_ipv4 p; |
| 235 | struct prefix_ipv4 *addr; |
| 236 | struct prefix_ipv4 *dest; |
| 237 | |
| 238 | if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)) |
| 239 | return; |
| 240 | |
| 241 | addr = (struct prefix_ipv4 *)ifc->address; |
| 242 | dest = (struct prefix_ipv4 *)ifc->destination; |
| 243 | |
| 244 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 245 | p.family = AF_INET; |
| 246 | p.prefixlen = addr->prefixlen; |
| 247 | |
paul | 960182a | 2003-04-09 07:16:04 +0000 | [diff] [blame] | 248 | /* Point-to-point check. */ |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 249 | if (CONNECTED_POINTOPOINT_HOST(ifc)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 250 | p.prefix = dest->prefix; |
| 251 | else |
| 252 | p.prefix = addr->prefix; |
| 253 | |
| 254 | /* Apply mask to the network. */ |
| 255 | apply_mask_ipv4 (&p); |
| 256 | |
| 257 | /* In case of connected address is 0.0.0.0/0 we treat it tunnel |
| 258 | address. */ |
| 259 | if (prefix_ipv4_any (&p)) |
| 260 | return; |
| 261 | |
| 262 | rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0); |
| 263 | |
| 264 | rib_update (); |
| 265 | } |
| 266 | |
| 267 | /* Delete connected IPv4 route to the interface. */ |
| 268 | void |
| 269 | connected_delete_ipv4 (struct interface *ifp, int flags, struct in_addr *addr, |
paul | d06b2a6 | 2005-10-11 03:53:54 +0000 | [diff] [blame^] | 270 | u_char prefixlen, struct in_addr *broad, |
| 271 | const char *label) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | { |
| 273 | struct prefix_ipv4 p; |
| 274 | struct connected *ifc; |
| 275 | |
| 276 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 277 | p.family = AF_INET; |
| 278 | p.prefix = *addr; |
| 279 | p.prefixlen = prefixlen; |
| 280 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 281 | ifc = connected_check (ifp, (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | if (! ifc) |
| 283 | return; |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 284 | |
| 285 | connected_withdraw (ifc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | #ifdef HAVE_IPV6 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | void |
| 290 | connected_up_ipv6 (struct interface *ifp, struct connected *ifc) |
| 291 | { |
| 292 | struct prefix_ipv6 p; |
| 293 | struct prefix_ipv6 *addr; |
| 294 | struct prefix_ipv6 *dest; |
| 295 | |
| 296 | if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)) |
| 297 | return; |
| 298 | |
| 299 | addr = (struct prefix_ipv6 *) ifc->address; |
| 300 | dest = (struct prefix_ipv6 *) ifc->destination; |
| 301 | |
| 302 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 303 | p.family = AF_INET6; |
| 304 | p.prefixlen = addr->prefixlen; |
| 305 | |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 306 | if (if_is_pointopoint (ifp) && dest) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 307 | { |
| 308 | if (IN6_IS_ADDR_UNSPECIFIED (&dest->prefix)) |
| 309 | p.prefix = addr->prefix; |
| 310 | else |
| 311 | p.prefix = dest->prefix; |
| 312 | } |
| 313 | else |
| 314 | p.prefix = addr->prefix; |
| 315 | |
| 316 | /* Apply mask to the network. */ |
| 317 | apply_mask_ipv6 (&p); |
| 318 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 319 | #if ! defined (MUSICA) && ! defined (LINUX) |
| 320 | /* XXX: It is already done by rib_bogus_ipv6 within rib_add_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 321 | if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix)) |
| 322 | return; |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 323 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 324 | |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 325 | rib_add_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, 0, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | |
| 327 | rib_update (); |
| 328 | } |
| 329 | |
| 330 | /* Add connected IPv6 route to the interface. */ |
| 331 | void |
| 332 | connected_add_ipv6 (struct interface *ifp, struct in6_addr *addr, |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 333 | u_char prefixlen, struct in6_addr *broad) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 334 | { |
| 335 | struct prefix_ipv6 *p; |
| 336 | struct connected *ifc; |
| 337 | struct connected *current; |
| 338 | |
| 339 | /* Make connected structure. */ |
| 340 | ifc = connected_new (); |
| 341 | ifc->ifp = ifp; |
| 342 | |
| 343 | /* Allocate new connected address. */ |
| 344 | p = prefix_ipv6_new (); |
| 345 | p->family = AF_INET6; |
| 346 | IPV6_ADDR_COPY (&p->prefix, addr); |
| 347 | p->prefixlen = prefixlen; |
| 348 | ifc->address = (struct prefix *) p; |
| 349 | |
| 350 | /* If there is broadcast or pointopoint address. */ |
| 351 | if (broad) |
| 352 | { |
| 353 | p = prefix_ipv6_new (); |
| 354 | p->family = AF_INET6; |
| 355 | IPV6_ADDR_COPY (&p->prefix, broad); |
| 356 | ifc->destination = (struct prefix *) p; |
| 357 | } |
| 358 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 359 | if ((current = connected_check (ifp, (struct prefix *) ifc->address))) |
| 360 | connected_withdraw (current); /* implicit update of existing address */ |
| 361 | |
| 362 | connected_announce (ifp, ifc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void |
| 366 | connected_down_ipv6 (struct interface *ifp, struct connected *ifc) |
| 367 | { |
| 368 | struct prefix_ipv6 p; |
| 369 | struct prefix_ipv6 *addr; |
| 370 | struct prefix_ipv6 *dest; |
| 371 | |
| 372 | if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)) |
| 373 | return; |
| 374 | |
| 375 | addr = (struct prefix_ipv6 *) ifc->address; |
| 376 | dest = (struct prefix_ipv6 *) ifc->destination; |
| 377 | |
| 378 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 379 | p.family = AF_INET6; |
| 380 | p.prefixlen = addr->prefixlen; |
| 381 | |
paul | 31a476c | 2003-09-29 19:54:53 +0000 | [diff] [blame] | 382 | if (if_is_pointopoint (ifp) && dest) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | { |
| 384 | if (IN6_IS_ADDR_UNSPECIFIED (&dest->prefix)) |
| 385 | p.prefix = addr->prefix; |
| 386 | else |
| 387 | p.prefix = dest->prefix; |
| 388 | } |
| 389 | else |
| 390 | p.prefix = addr->prefix; |
| 391 | |
| 392 | apply_mask_ipv6 (&p); |
| 393 | |
| 394 | if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix)) |
| 395 | return; |
| 396 | |
| 397 | rib_delete_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0); |
| 398 | |
| 399 | rib_update (); |
| 400 | } |
| 401 | |
| 402 | void |
| 403 | connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address, |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 404 | u_char prefixlen, struct in6_addr *broad) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 405 | { |
| 406 | struct prefix_ipv6 p; |
| 407 | struct connected *ifc; |
| 408 | |
| 409 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 410 | p.family = AF_INET6; |
| 411 | memcpy (&p.prefix, address, sizeof (struct in6_addr)); |
| 412 | p.prefixlen = prefixlen; |
| 413 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 414 | ifc = connected_check (ifp, (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 415 | if (! ifc) |
| 416 | return; |
| 417 | |
paul | ca16218 | 2005-09-12 16:58:52 +0000 | [diff] [blame] | 418 | connected_withdraw (ifc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 419 | } |
| 420 | #endif /* HAVE_IPV6 */ |