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