paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP-4, BGP-4+ daemon program |
| 2 | Copyright (C) 1996, 97, 98, 99, 2000 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 | #include <zebra.h> |
| 22 | |
| 23 | #include "prefix.h" |
| 24 | #include "thread.h" |
| 25 | #include "buffer.h" |
| 26 | #include "stream.h" |
| 27 | #include "command.h" |
| 28 | #include "sockunion.h" |
| 29 | #include "network.h" |
| 30 | #include "memory.h" |
| 31 | #include "filter.h" |
| 32 | #include "routemap.h" |
| 33 | #include "str.h" |
| 34 | #include "log.h" |
| 35 | #include "plist.h" |
| 36 | #include "linklist.h" |
| 37 | |
| 38 | #include "bgpd/bgpd.h" |
| 39 | #include "bgpd/bgp_table.h" |
| 40 | #include "bgpd/bgp_aspath.h" |
| 41 | #include "bgpd/bgp_route.h" |
| 42 | #include "bgpd/bgp_dump.h" |
| 43 | #include "bgpd/bgp_debug.h" |
| 44 | #include "bgpd/bgp_community.h" |
| 45 | #include "bgpd/bgp_attr.h" |
| 46 | #include "bgpd/bgp_regex.h" |
| 47 | #include "bgpd/bgp_clist.h" |
| 48 | #include "bgpd/bgp_fsm.h" |
| 49 | #include "bgpd/bgp_packet.h" |
| 50 | #include "bgpd/bgp_zebra.h" |
| 51 | #include "bgpd/bgp_open.h" |
| 52 | #include "bgpd/bgp_filter.h" |
| 53 | #include "bgpd/bgp_nexthop.h" |
| 54 | #include "bgpd/bgp_damp.h" |
| 55 | #include "bgpd/bgp_mplsvpn.h" |
| 56 | #include "bgpd/bgp_advertise.h" |
| 57 | #include "bgpd/bgp_network.h" |
| 58 | #include "bgpd/bgp_vty.h" |
| 59 | #ifdef HAVE_SNMP |
| 60 | #include "bgpd/bgp_snmp.h" |
| 61 | #endif /* HAVE_SNMP */ |
| 62 | |
| 63 | /* BGP process wide configuration. */ |
| 64 | static struct bgp_master bgp_master; |
| 65 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 66 | extern struct in_addr router_id_zebra; |
| 67 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | /* BGP process wide configuration pointer to export. */ |
| 69 | struct bgp_master *bm; |
| 70 | |
| 71 | /* BGP community-list. */ |
| 72 | struct community_list_handler *bgp_clist; |
| 73 | |
| 74 | /* BGP global flag manipulation. */ |
| 75 | int |
| 76 | bgp_option_set (int flag) |
| 77 | { |
| 78 | switch (flag) |
| 79 | { |
| 80 | case BGP_OPT_NO_FIB: |
| 81 | case BGP_OPT_MULTIPLE_INSTANCE: |
| 82 | case BGP_OPT_CONFIG_CISCO: |
| 83 | SET_FLAG (bm->options, flag); |
| 84 | break; |
| 85 | default: |
| 86 | return BGP_ERR_INVALID_FLAG; |
| 87 | break; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | bgp_option_unset (int flag) |
| 94 | { |
| 95 | switch (flag) |
| 96 | { |
| 97 | case BGP_OPT_MULTIPLE_INSTANCE: |
| 98 | if (listcount (bm->bgp) > 1) |
| 99 | return BGP_ERR_MULTIPLE_INSTANCE_USED; |
| 100 | /* Fall through. */ |
| 101 | case BGP_OPT_NO_FIB: |
| 102 | case BGP_OPT_CONFIG_CISCO: |
| 103 | UNSET_FLAG (bm->options, flag); |
| 104 | break; |
| 105 | default: |
| 106 | return BGP_ERR_INVALID_FLAG; |
| 107 | break; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | bgp_option_check (int flag) |
| 114 | { |
| 115 | return CHECK_FLAG (bm->options, flag); |
| 116 | } |
| 117 | |
| 118 | /* BGP flag manipulation. */ |
| 119 | int |
| 120 | bgp_flag_set (struct bgp *bgp, int flag) |
| 121 | { |
| 122 | SET_FLAG (bgp->flags, flag); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int |
| 127 | bgp_flag_unset (struct bgp *bgp, int flag) |
| 128 | { |
| 129 | UNSET_FLAG (bgp->flags, flag); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int |
| 134 | bgp_flag_check (struct bgp *bgp, int flag) |
| 135 | { |
| 136 | return CHECK_FLAG (bgp->flags, flag); |
| 137 | } |
| 138 | |
| 139 | /* Internal function to set BGP structure configureation flag. */ |
| 140 | static void |
| 141 | bgp_config_set (struct bgp *bgp, int config) |
| 142 | { |
| 143 | SET_FLAG (bgp->config, config); |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | bgp_config_unset (struct bgp *bgp, int config) |
| 148 | { |
| 149 | UNSET_FLAG (bgp->config, config); |
| 150 | } |
| 151 | |
| 152 | static int |
| 153 | bgp_config_check (struct bgp *bgp, int config) |
| 154 | { |
| 155 | return CHECK_FLAG (bgp->config, config); |
| 156 | } |
| 157 | |
| 158 | /* Set BGP router identifier. */ |
| 159 | int |
| 160 | bgp_router_id_set (struct bgp *bgp, struct in_addr *id) |
| 161 | { |
| 162 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 163 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | |
| 165 | if (bgp_config_check (bgp, BGP_CONFIG_ROUTER_ID) |
| 166 | && IPV4_ADDR_SAME (&bgp->router_id, id)) |
| 167 | return 0; |
| 168 | |
| 169 | IPV4_ADDR_COPY (&bgp->router_id, id); |
| 170 | bgp_config_set (bgp, BGP_CONFIG_ROUTER_ID); |
| 171 | |
| 172 | /* Set all peer's local identifier with this value. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 173 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 174 | { |
| 175 | IPV4_ADDR_COPY (&peer->local_id, id); |
| 176 | |
| 177 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 178 | { |
| 179 | peer->last_reset = PEER_DOWN_RID_CHANGE; |
| 180 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 181 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 182 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | } |
| 184 | return 0; |
| 185 | } |
| 186 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | /* BGP's cluster-id control. */ |
| 188 | int |
| 189 | bgp_cluster_id_set (struct bgp *bgp, struct in_addr *cluster_id) |
| 190 | { |
| 191 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 192 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | |
| 194 | if (bgp_config_check (bgp, BGP_CONFIG_CLUSTER_ID) |
| 195 | && IPV4_ADDR_SAME (&bgp->cluster_id, cluster_id)) |
| 196 | return 0; |
| 197 | |
| 198 | IPV4_ADDR_COPY (&bgp->cluster_id, cluster_id); |
| 199 | bgp_config_set (bgp, BGP_CONFIG_CLUSTER_ID); |
| 200 | |
| 201 | /* Clear all IBGP peer. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 202 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | { |
| 204 | if (peer_sort (peer) != BGP_PEER_IBGP) |
| 205 | continue; |
| 206 | |
| 207 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 208 | { |
| 209 | peer->last_reset = PEER_DOWN_CLID_CHANGE; |
| 210 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 211 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 212 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int |
| 218 | bgp_cluster_id_unset (struct bgp *bgp) |
| 219 | { |
| 220 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 221 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | |
| 223 | if (! bgp_config_check (bgp, BGP_CONFIG_CLUSTER_ID)) |
| 224 | return 0; |
| 225 | |
| 226 | bgp->cluster_id.s_addr = 0; |
| 227 | bgp_config_unset (bgp, BGP_CONFIG_CLUSTER_ID); |
| 228 | |
| 229 | /* Clear all IBGP peer. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 230 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | { |
| 232 | if (peer_sort (peer) != BGP_PEER_IBGP) |
| 233 | continue; |
| 234 | |
| 235 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 236 | { |
| 237 | peer->last_reset = PEER_DOWN_CLID_CHANGE; |
| 238 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 239 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 240 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* BGP timer configuration. */ |
| 246 | int |
| 247 | bgp_timers_set (struct bgp *bgp, u_int32_t keepalive, u_int32_t holdtime) |
| 248 | { |
| 249 | bgp->default_keepalive = (keepalive < holdtime / 3 |
| 250 | ? keepalive : holdtime / 3); |
| 251 | bgp->default_holdtime = holdtime; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | int |
| 257 | bgp_timers_unset (struct bgp *bgp) |
| 258 | { |
| 259 | bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE; |
| 260 | bgp->default_holdtime = BGP_DEFAULT_HOLDTIME; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* BGP confederation configuration. */ |
| 266 | int |
| 267 | bgp_confederation_id_set (struct bgp *bgp, as_t as) |
| 268 | { |
| 269 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 270 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | int already_confed; |
| 272 | |
| 273 | if (as == 0) |
| 274 | return BGP_ERR_INVALID_AS; |
| 275 | |
| 276 | /* Remember - were we doing confederation before? */ |
| 277 | already_confed = bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION); |
| 278 | bgp->confed_id = as; |
| 279 | bgp_config_set (bgp, BGP_CONFIG_CONFEDERATION); |
| 280 | |
| 281 | /* If we were doing confederation already, this is just an external |
| 282 | AS change. Just Reset EBGP sessions, not CONFED sessions. If we |
| 283 | were not doing confederation before, reset all EBGP sessions. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 284 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | { |
| 286 | /* We're looking for peers who's AS is not local or part of our |
| 287 | confederation. */ |
| 288 | if (already_confed) |
| 289 | { |
| 290 | if (peer_sort (peer) == BGP_PEER_EBGP) |
| 291 | { |
| 292 | peer->local_as = as; |
| 293 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 294 | { |
| 295 | peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE; |
| 296 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 297 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 298 | } |
| 299 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 300 | else |
| 301 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 302 | } |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | /* Not doign confederation before, so reset every non-local |
| 307 | session */ |
| 308 | if (peer_sort (peer) != BGP_PEER_IBGP) |
| 309 | { |
| 310 | /* Reset the local_as to be our EBGP one */ |
| 311 | if (peer_sort (peer) == BGP_PEER_EBGP) |
| 312 | peer->local_as = as; |
| 313 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 314 | { |
| 315 | peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE; |
| 316 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 317 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 318 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 319 | else |
| 320 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | int |
| 328 | bgp_confederation_id_unset (struct bgp *bgp) |
| 329 | { |
| 330 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 331 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 332 | |
| 333 | bgp->confed_id = 0; |
| 334 | bgp_config_unset (bgp, BGP_CONFIG_CONFEDERATION); |
| 335 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 336 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 337 | { |
| 338 | /* We're looking for peers who's AS is not local */ |
| 339 | if (peer_sort (peer) != BGP_PEER_IBGP) |
| 340 | { |
| 341 | peer->local_as = bgp->as; |
| 342 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 343 | { |
| 344 | peer->last_reset = PEER_DOWN_CONFED_ID_CHANGE; |
| 345 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 346 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 347 | } |
| 348 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 349 | else |
| 350 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 351 | } |
| 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /* Is an AS part of the confed or not? */ |
| 357 | int |
| 358 | bgp_confederation_peers_check (struct bgp *bgp, as_t as) |
| 359 | { |
| 360 | int i; |
| 361 | |
| 362 | if (! bgp) |
| 363 | return 0; |
| 364 | |
| 365 | for (i = 0; i < bgp->confed_peers_cnt; i++) |
| 366 | if (bgp->confed_peers[i] == as) |
| 367 | return 1; |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /* Add an AS to the confederation set. */ |
| 373 | int |
| 374 | bgp_confederation_peers_add (struct bgp *bgp, as_t as) |
| 375 | { |
| 376 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 377 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | |
| 379 | if (! bgp) |
| 380 | return BGP_ERR_INVALID_BGP; |
| 381 | |
| 382 | if (bgp->as == as) |
| 383 | return BGP_ERR_INVALID_AS; |
| 384 | |
| 385 | if (bgp_confederation_peers_check (bgp, as)) |
| 386 | return -1; |
| 387 | |
| 388 | if (bgp->confed_peers) |
| 389 | bgp->confed_peers = XREALLOC (MTYPE_BGP_CONFED_LIST, |
| 390 | bgp->confed_peers, |
| 391 | (bgp->confed_peers_cnt + 1) * sizeof (as_t)); |
| 392 | else |
| 393 | bgp->confed_peers = XMALLOC (MTYPE_BGP_CONFED_LIST, |
| 394 | (bgp->confed_peers_cnt + 1) * sizeof (as_t)); |
| 395 | |
| 396 | bgp->confed_peers[bgp->confed_peers_cnt] = as; |
| 397 | bgp->confed_peers_cnt++; |
| 398 | |
| 399 | if (bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION)) |
| 400 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 401 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 402 | { |
| 403 | if (peer->as == as) |
| 404 | { |
| 405 | peer->local_as = bgp->as; |
| 406 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 407 | { |
| 408 | peer->last_reset = PEER_DOWN_CONFED_PEER_CHANGE; |
| 409 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 410 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 411 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 412 | else |
| 413 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /* Delete an AS from the confederation set. */ |
| 421 | int |
| 422 | bgp_confederation_peers_remove (struct bgp *bgp, as_t as) |
| 423 | { |
| 424 | int i; |
| 425 | int j; |
| 426 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 427 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 428 | |
| 429 | if (! bgp) |
| 430 | return -1; |
| 431 | |
| 432 | if (! bgp_confederation_peers_check (bgp, as)) |
| 433 | return -1; |
| 434 | |
| 435 | for (i = 0; i < bgp->confed_peers_cnt; i++) |
| 436 | if (bgp->confed_peers[i] == as) |
| 437 | for(j = i + 1; j < bgp->confed_peers_cnt; j++) |
| 438 | bgp->confed_peers[j - 1] = bgp->confed_peers[j]; |
| 439 | |
| 440 | bgp->confed_peers_cnt--; |
| 441 | |
| 442 | if (bgp->confed_peers_cnt == 0) |
| 443 | { |
| 444 | if (bgp->confed_peers) |
| 445 | XFREE (MTYPE_BGP_CONFED_LIST, bgp->confed_peers); |
| 446 | bgp->confed_peers = NULL; |
| 447 | } |
| 448 | else |
| 449 | bgp->confed_peers = XREALLOC (MTYPE_BGP_CONFED_LIST, |
| 450 | bgp->confed_peers, |
| 451 | bgp->confed_peers_cnt * sizeof (as_t)); |
| 452 | |
| 453 | /* Now reset any peer who's remote AS has just been removed from the |
| 454 | CONFED */ |
| 455 | if (bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION)) |
| 456 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 457 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 458 | { |
| 459 | if (peer->as == as) |
| 460 | { |
| 461 | peer->local_as = bgp->confed_id; |
| 462 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 463 | { |
| 464 | peer->last_reset = PEER_DOWN_CONFED_PEER_CHANGE; |
| 465 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 466 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 467 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | else |
| 469 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /* Local preference configuration. */ |
| 478 | int |
| 479 | bgp_default_local_preference_set (struct bgp *bgp, u_int32_t local_pref) |
| 480 | { |
| 481 | if (! bgp) |
| 482 | return -1; |
| 483 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 484 | bgp->default_local_pref = local_pref; |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | int |
| 490 | bgp_default_local_preference_unset (struct bgp *bgp) |
| 491 | { |
| 492 | if (! bgp) |
| 493 | return -1; |
| 494 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF; |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 500 | /* If peer is RSERVER_CLIENT in at least one address family and is not member |
| 501 | of a peer_group for that family, return 1. |
| 502 | Used to check wether the peer is included in list bgp->rsclient. */ |
| 503 | int |
| 504 | peer_rsclient_active (struct peer *peer) |
| 505 | { |
| 506 | int i; |
| 507 | int j; |
| 508 | |
| 509 | for (i=AFI_IP; i < AFI_MAX; i++) |
| 510 | for (j=SAFI_UNICAST; j < SAFI_MAX; j++) |
| 511 | if (CHECK_FLAG(peer->af_flags[i][j], PEER_FLAG_RSERVER_CLIENT) |
| 512 | && ! peer->af_group[i][j]) |
| 513 | return 1; |
| 514 | return 0; |
| 515 | } |
| 516 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 517 | /* Peer comparison function for sorting. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | static int |
| 519 | peer_cmp (struct peer *p1, struct peer *p2) |
| 520 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 521 | return sockunion_cmp (&p1->su, &p2->su); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | int |
| 525 | peer_af_flag_check (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag) |
| 526 | { |
| 527 | return CHECK_FLAG (peer->af_flags[afi][safi], flag); |
| 528 | } |
| 529 | |
| 530 | /* Reset all address family specific configuration. */ |
| 531 | static void |
| 532 | peer_af_flag_reset (struct peer *peer, afi_t afi, safi_t safi) |
| 533 | { |
| 534 | int i; |
| 535 | struct bgp_filter *filter; |
| 536 | char orf_name[BUFSIZ]; |
| 537 | |
| 538 | filter = &peer->filter[afi][safi]; |
| 539 | |
| 540 | /* Clear neighbor filter and route-map */ |
| 541 | for (i = FILTER_IN; i < FILTER_MAX; i++) |
| 542 | { |
| 543 | if (filter->dlist[i].name) |
| 544 | { |
| 545 | free (filter->dlist[i].name); |
| 546 | filter->dlist[i].name = NULL; |
| 547 | } |
| 548 | if (filter->plist[i].name) |
| 549 | { |
| 550 | free (filter->plist[i].name); |
| 551 | filter->plist[i].name = NULL; |
| 552 | } |
| 553 | if (filter->aslist[i].name) |
| 554 | { |
| 555 | free (filter->aslist[i].name); |
| 556 | filter->aslist[i].name = NULL; |
| 557 | } |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 558 | } |
| 559 | for (i = RMAP_IN; i < RMAP_MAX; i++) |
| 560 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 561 | if (filter->map[i].name) |
| 562 | { |
| 563 | free (filter->map[i].name); |
| 564 | filter->map[i].name = NULL; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* Clear unsuppress map. */ |
| 569 | if (filter->usmap.name) |
| 570 | free (filter->usmap.name); |
| 571 | filter->usmap.name = NULL; |
| 572 | filter->usmap.map = NULL; |
| 573 | |
| 574 | /* Clear neighbor's all address family flags. */ |
| 575 | peer->af_flags[afi][safi] = 0; |
| 576 | |
| 577 | /* Clear neighbor's all address family sflags. */ |
| 578 | peer->af_sflags[afi][safi] = 0; |
| 579 | |
| 580 | /* Clear neighbor's all address family capabilities. */ |
| 581 | peer->af_cap[afi][safi] = 0; |
| 582 | |
| 583 | /* Clear ORF info */ |
| 584 | peer->orf_plist[afi][safi] = NULL; |
| 585 | sprintf (orf_name, "%s.%d.%d", peer->host, afi, safi); |
| 586 | prefix_bgp_orf_remove_all (orf_name); |
| 587 | |
| 588 | /* Set default neighbor send-community. */ |
| 589 | if (! bgp_option_check (BGP_OPT_CONFIG_CISCO)) |
| 590 | { |
| 591 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY); |
| 592 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY); |
| 593 | } |
| 594 | |
| 595 | /* Clear neighbor default_originate_rmap */ |
| 596 | if (peer->default_rmap[afi][safi].name) |
| 597 | free (peer->default_rmap[afi][safi].name); |
| 598 | peer->default_rmap[afi][safi].name = NULL; |
| 599 | peer->default_rmap[afi][safi].map = NULL; |
| 600 | |
| 601 | /* Clear neighbor maximum-prefix */ |
| 602 | peer->pmax[afi][safi] = 0; |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 603 | peer->pmax_threshold[afi][safi] = MAXIMUM_PREFIX_THRESHOLD_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* peer global config reset */ |
| 607 | void |
| 608 | peer_global_config_reset (struct peer *peer) |
| 609 | { |
| 610 | peer->weight = 0; |
| 611 | peer->change_local_as = 0; |
| 612 | peer->ttl = (peer_sort (peer) == BGP_PEER_IBGP ? 255 : 1); |
| 613 | if (peer->update_source) |
| 614 | { |
| 615 | sockunion_free (peer->update_source); |
| 616 | peer->update_source = NULL; |
| 617 | } |
| 618 | if (peer->update_if) |
| 619 | { |
| 620 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 621 | peer->update_if = NULL; |
| 622 | } |
| 623 | |
| 624 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 625 | peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV; |
| 626 | else |
| 627 | peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; |
| 628 | |
| 629 | peer->flags = 0; |
| 630 | peer->config = 0; |
| 631 | peer->holdtime = 0; |
| 632 | peer->keepalive = 0; |
| 633 | peer->connect = 0; |
| 634 | peer->v_connect = BGP_DEFAULT_CONNECT_RETRY; |
| 635 | } |
| 636 | |
| 637 | /* Check peer's AS number and determin is this peer IBGP or EBGP */ |
| 638 | int |
| 639 | peer_sort (struct peer *peer) |
| 640 | { |
| 641 | struct bgp *bgp; |
| 642 | |
| 643 | bgp = peer->bgp; |
| 644 | |
| 645 | /* Peer-group */ |
| 646 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 647 | { |
| 648 | if (peer->as) |
| 649 | return (bgp->as == peer->as ? BGP_PEER_IBGP : BGP_PEER_EBGP); |
| 650 | else |
| 651 | { |
| 652 | struct peer *peer1; |
| 653 | peer1 = listnode_head (peer->group->peer); |
| 654 | if (peer1) |
| 655 | return (peer1->local_as == peer1->as |
| 656 | ? BGP_PEER_IBGP : BGP_PEER_EBGP); |
| 657 | } |
| 658 | return BGP_PEER_INTERNAL; |
| 659 | } |
| 660 | |
| 661 | /* Normal peer */ |
| 662 | if (bgp && CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION)) |
| 663 | { |
| 664 | if (peer->local_as == 0) |
| 665 | return BGP_PEER_INTERNAL; |
| 666 | |
| 667 | if (peer->local_as == peer->as) |
| 668 | { |
| 669 | if (peer->local_as == bgp->confed_id) |
| 670 | return BGP_PEER_EBGP; |
| 671 | else |
| 672 | return BGP_PEER_IBGP; |
| 673 | } |
| 674 | |
| 675 | if (bgp_confederation_peers_check (bgp, peer->as)) |
| 676 | return BGP_PEER_CONFED; |
| 677 | |
| 678 | return BGP_PEER_EBGP; |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | return (peer->local_as == 0 |
| 683 | ? BGP_PEER_INTERNAL : peer->local_as == peer->as |
| 684 | ? BGP_PEER_IBGP : BGP_PEER_EBGP); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /* Allocate new peer object. */ |
| 689 | static struct peer * |
| 690 | peer_new () |
| 691 | { |
| 692 | afi_t afi; |
| 693 | safi_t safi; |
| 694 | struct peer *peer; |
| 695 | struct servent *sp; |
| 696 | |
| 697 | /* Allocate new peer. */ |
| 698 | peer = XMALLOC (MTYPE_BGP_PEER, sizeof (struct peer)); |
| 699 | memset (peer, 0, sizeof (struct peer)); |
| 700 | |
| 701 | /* Set default value. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 702 | peer->fd = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | peer->v_start = BGP_INIT_START_TIMER; |
| 704 | peer->v_connect = BGP_DEFAULT_CONNECT_RETRY; |
| 705 | peer->v_asorig = BGP_DEFAULT_ASORIGINATE; |
| 706 | peer->status = Idle; |
| 707 | peer->ostatus = Idle; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 708 | peer->weight = 0; |
| 709 | |
| 710 | /* Set default flags. */ |
| 711 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 712 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 713 | { |
| 714 | if (! bgp_option_check (BGP_OPT_CONFIG_CISCO)) |
| 715 | { |
| 716 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY); |
| 717 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY); |
| 718 | } |
| 719 | peer->orf_plist[afi][safi] = NULL; |
| 720 | } |
| 721 | SET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 722 | |
| 723 | /* Create buffers. */ |
| 724 | peer->ibuf = stream_new (BGP_MAX_PACKET_SIZE); |
| 725 | peer->obuf = stream_fifo_new (); |
| 726 | peer->work = stream_new (BGP_MAX_PACKET_SIZE); |
| 727 | |
| 728 | bgp_sync_init (peer); |
| 729 | |
| 730 | /* Get service port number. */ |
| 731 | sp = getservbyname ("bgp", "tcp"); |
| 732 | peer->port = (sp == NULL) ? BGP_PORT_DEFAULT : ntohs (sp->s_port); |
| 733 | |
| 734 | return peer; |
| 735 | } |
| 736 | |
| 737 | /* Create new BGP peer. */ |
| 738 | struct peer * |
| 739 | peer_create (union sockunion *su, struct bgp *bgp, as_t local_as, |
| 740 | as_t remote_as, afi_t afi, safi_t safi) |
| 741 | { |
| 742 | int active; |
| 743 | struct peer *peer; |
| 744 | char buf[SU_ADDRSTRLEN]; |
| 745 | |
| 746 | peer = peer_new (); |
| 747 | peer->bgp = bgp; |
| 748 | peer->su = *su; |
| 749 | peer->local_as = local_as; |
| 750 | peer->as = remote_as; |
| 751 | peer->local_id = bgp->router_id; |
| 752 | peer->v_holdtime = bgp->default_holdtime; |
| 753 | peer->v_keepalive = bgp->default_keepalive; |
| 754 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 755 | peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV; |
| 756 | else |
| 757 | peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; |
| 758 | listnode_add_sort (bgp->peer, peer); |
| 759 | |
| 760 | active = peer_active (peer); |
| 761 | |
| 762 | if (afi && safi) |
| 763 | peer->afc[afi][safi] = 1; |
| 764 | |
| 765 | /* Last read time set */ |
| 766 | peer->readtime = time (NULL); |
| 767 | |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 768 | /* Last reset time set */ |
| 769 | peer->resettime = time (NULL); |
| 770 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | /* Default TTL set. */ |
| 772 | peer->ttl = (peer_sort (peer) == BGP_PEER_IBGP ? 255 : 1); |
| 773 | |
| 774 | /* Make peer's address string. */ |
| 775 | sockunion2str (su, buf, SU_ADDRSTRLEN); |
paul | e83e208 | 2005-05-19 02:12:25 +0000 | [diff] [blame] | 776 | peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 777 | |
| 778 | /* Set up peer's events and timers. */ |
| 779 | if (! active && peer_active (peer)) |
| 780 | bgp_timer_set (peer); |
| 781 | |
| 782 | return peer; |
| 783 | } |
| 784 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 785 | /* Make accept BGP peer. Called from bgp_accept (). */ |
| 786 | struct peer * |
| 787 | peer_create_accept (struct bgp *bgp) |
| 788 | { |
| 789 | struct peer *peer; |
| 790 | |
| 791 | peer = peer_new (); |
| 792 | peer->bgp = bgp; |
| 793 | listnode_add_sort (bgp->peer, peer); |
| 794 | |
| 795 | return peer; |
| 796 | } |
| 797 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | /* Change peer's AS number. */ |
| 799 | void |
| 800 | peer_as_change (struct peer *peer, as_t as) |
| 801 | { |
| 802 | int type; |
| 803 | |
| 804 | /* Stop peer. */ |
| 805 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 806 | { |
| 807 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 808 | { |
| 809 | peer->last_reset = PEER_DOWN_REMOTE_AS_CHANGE; |
| 810 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 811 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 812 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 813 | else |
| 814 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 815 | } |
| 816 | type = peer_sort (peer); |
| 817 | peer->as = as; |
| 818 | |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 819 | if (bgp_config_check (peer->bgp, BGP_CONFIG_CONFEDERATION) |
| 820 | && ! bgp_confederation_peers_check (peer->bgp, as) |
| 821 | && peer->bgp->as != as) |
| 822 | peer->local_as = peer->bgp->confed_id; |
| 823 | else |
| 824 | peer->local_as = peer->bgp->as; |
| 825 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 826 | /* Advertisement-interval reset */ |
| 827 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 828 | peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV; |
| 829 | else |
| 830 | peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; |
| 831 | |
| 832 | /* TTL reset */ |
| 833 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 834 | peer->ttl = 255; |
| 835 | else if (type == BGP_PEER_IBGP) |
| 836 | peer->ttl = 1; |
| 837 | |
| 838 | /* reflector-client reset */ |
| 839 | if (peer_sort (peer) != BGP_PEER_IBGP) |
| 840 | { |
| 841 | UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_UNICAST], |
| 842 | PEER_FLAG_REFLECTOR_CLIENT); |
| 843 | UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_MULTICAST], |
| 844 | PEER_FLAG_REFLECTOR_CLIENT); |
| 845 | UNSET_FLAG (peer->af_flags[AFI_IP][SAFI_MPLS_VPN], |
| 846 | PEER_FLAG_REFLECTOR_CLIENT); |
| 847 | UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_UNICAST], |
| 848 | PEER_FLAG_REFLECTOR_CLIENT); |
| 849 | UNSET_FLAG (peer->af_flags[AFI_IP6][SAFI_MULTICAST], |
| 850 | PEER_FLAG_REFLECTOR_CLIENT); |
| 851 | } |
| 852 | |
| 853 | /* local-as reset */ |
| 854 | if (peer_sort (peer) != BGP_PEER_EBGP) |
| 855 | { |
| 856 | peer->change_local_as = 0; |
| 857 | UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | /* If peer does not exist, create new one. If peer already exists, |
| 862 | set AS number to the peer. */ |
| 863 | int |
| 864 | peer_remote_as (struct bgp *bgp, union sockunion *su, as_t *as, |
| 865 | afi_t afi, safi_t safi) |
| 866 | { |
| 867 | struct peer *peer; |
| 868 | as_t local_as; |
| 869 | |
| 870 | peer = peer_lookup (bgp, su); |
| 871 | |
| 872 | if (peer) |
| 873 | { |
| 874 | /* When this peer is a member of peer-group. */ |
| 875 | if (peer->group) |
| 876 | { |
| 877 | if (peer->group->conf->as) |
| 878 | { |
| 879 | /* Return peer group's AS number. */ |
| 880 | *as = peer->group->conf->as; |
| 881 | return BGP_ERR_PEER_GROUP_MEMBER; |
| 882 | } |
| 883 | if (peer_sort (peer->group->conf) == BGP_PEER_IBGP) |
| 884 | { |
| 885 | if (bgp->as != *as) |
| 886 | { |
| 887 | *as = peer->as; |
| 888 | return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT; |
| 889 | } |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | if (bgp->as == *as) |
| 894 | { |
| 895 | *as = peer->as; |
| 896 | return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT; |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /* Existing peer's AS number change. */ |
| 902 | if (peer->as != *as) |
| 903 | peer_as_change (peer, *as); |
| 904 | } |
| 905 | else |
| 906 | { |
| 907 | |
| 908 | /* If the peer is not part of our confederation, and its not an |
| 909 | iBGP peer then spoof the source AS */ |
| 910 | if (bgp_config_check (bgp, BGP_CONFIG_CONFEDERATION) |
| 911 | && ! bgp_confederation_peers_check (bgp, *as) |
| 912 | && bgp->as != *as) |
| 913 | local_as = bgp->confed_id; |
| 914 | else |
| 915 | local_as = bgp->as; |
| 916 | |
| 917 | /* If this is IPv4 unicast configuration and "no bgp default |
| 918 | ipv4-unicast" is specified. */ |
| 919 | |
| 920 | if (bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4) |
| 921 | && afi == AFI_IP && safi == SAFI_UNICAST) |
| 922 | peer = peer_create (su, bgp, local_as, *as, 0, 0); |
| 923 | else |
| 924 | peer = peer_create (su, bgp, local_as, *as, afi, safi); |
| 925 | } |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /* Activate the peer or peer group for specified AFI and SAFI. */ |
| 931 | int |
| 932 | peer_activate (struct peer *peer, afi_t afi, safi_t safi) |
| 933 | { |
| 934 | int active; |
| 935 | |
| 936 | if (peer->afc[afi][safi]) |
| 937 | return 0; |
| 938 | |
| 939 | /* Activate the address family configuration. */ |
| 940 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 941 | peer->afc[afi][safi] = 1; |
| 942 | else |
| 943 | { |
| 944 | active = peer_active (peer); |
| 945 | |
| 946 | peer->afc[afi][safi] = 1; |
| 947 | |
| 948 | if (! active && peer_active (peer)) |
| 949 | bgp_timer_set (peer); |
| 950 | else |
| 951 | { |
| 952 | if (peer->status == Established) |
| 953 | { |
| 954 | if (CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV)) |
| 955 | { |
| 956 | peer->afc_adv[afi][safi] = 1; |
| 957 | bgp_capability_send (peer, afi, safi, |
| 958 | CAPABILITY_CODE_MP, |
| 959 | CAPABILITY_ACTION_SET); |
| 960 | if (peer->afc_recv[afi][safi]) |
| 961 | { |
| 962 | peer->afc_nego[afi][safi] = 1; |
| 963 | bgp_announce_route (peer, afi, safi); |
| 964 | } |
| 965 | } |
| 966 | else |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 967 | { |
| 968 | peer->last_reset = PEER_DOWN_AF_ACTIVATE; |
| 969 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 970 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 971 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | } |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | int |
| 979 | peer_deactivate (struct peer *peer, afi_t afi, safi_t safi) |
| 980 | { |
| 981 | struct peer_group *group; |
| 982 | struct peer *peer1; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 983 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 984 | |
| 985 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 986 | { |
| 987 | group = peer->group; |
| 988 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 989 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 990 | { |
| 991 | if (peer1->af_group[afi][safi]) |
| 992 | return BGP_ERR_PEER_GROUP_MEMBER_EXISTS; |
| 993 | } |
| 994 | } |
| 995 | else |
| 996 | { |
| 997 | if (peer->af_group[afi][safi]) |
| 998 | return BGP_ERR_PEER_BELONGS_TO_GROUP; |
| 999 | } |
| 1000 | |
| 1001 | if (! peer->afc[afi][safi]) |
| 1002 | return 0; |
| 1003 | |
| 1004 | /* De-activate the address family configuration. */ |
| 1005 | peer->afc[afi][safi] = 0; |
| 1006 | peer_af_flag_reset (peer, afi, safi); |
| 1007 | |
| 1008 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 1009 | { |
| 1010 | if (peer->status == Established) |
| 1011 | { |
| 1012 | if (CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV)) |
| 1013 | { |
| 1014 | peer->afc_adv[afi][safi] = 0; |
| 1015 | peer->afc_nego[afi][safi] = 0; |
| 1016 | |
| 1017 | if (peer_active_nego (peer)) |
| 1018 | { |
| 1019 | bgp_capability_send (peer, afi, safi, |
| 1020 | CAPABILITY_CODE_MP, |
| 1021 | CAPABILITY_ACTION_UNSET); |
| 1022 | bgp_clear_route (peer, afi, safi); |
| 1023 | peer->pcount[afi][safi] = 0; |
| 1024 | } |
| 1025 | else |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1026 | { |
| 1027 | peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE; |
| 1028 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 1029 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 1030 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | } |
| 1032 | else |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1033 | { |
| 1034 | peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE; |
| 1035 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 1036 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 1037 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 1043 | void |
| 1044 | peer_nsf_stop (struct peer *peer) |
| 1045 | { |
| 1046 | afi_t afi; |
| 1047 | safi_t safi; |
| 1048 | |
| 1049 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT); |
| 1050 | UNSET_FLAG (peer->sflags, PEER_STATUS_NSF_MODE); |
| 1051 | |
| 1052 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 1053 | for (safi = SAFI_UNICAST ; safi < SAFI_UNICAST_MULTICAST ; safi++) |
| 1054 | peer->nsf[afi][safi] = 0; |
| 1055 | |
| 1056 | if (peer->t_gr_restart) |
| 1057 | { |
| 1058 | BGP_TIMER_OFF (peer->t_gr_restart); |
| 1059 | if (BGP_DEBUG (events, EVENTS)) |
| 1060 | zlog_debug ("%s graceful restart timer stopped", peer->host); |
| 1061 | } |
| 1062 | if (peer->t_gr_stale) |
| 1063 | { |
| 1064 | BGP_TIMER_OFF (peer->t_gr_stale); |
| 1065 | if (BGP_DEBUG (events, EVENTS)) |
| 1066 | zlog_debug ("%s graceful restart stalepath timer stopped", peer->host); |
| 1067 | } |
| 1068 | bgp_clear_route_all (peer); |
| 1069 | } |
| 1070 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1071 | /* Delete peer from confguration. */ |
| 1072 | int |
| 1073 | peer_delete (struct peer *peer) |
| 1074 | { |
| 1075 | int i; |
| 1076 | afi_t afi; |
| 1077 | safi_t safi; |
| 1078 | struct bgp *bgp; |
| 1079 | struct bgp_filter *filter; |
| 1080 | |
| 1081 | bgp = peer->bgp; |
| 1082 | |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 1083 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)) |
| 1084 | peer_nsf_stop (peer); |
| 1085 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1086 | /* If this peer belongs to peer group. Clearn up the |
| 1087 | relationship. */ |
| 1088 | if (peer->group) |
| 1089 | { |
| 1090 | listnode_delete (peer->group->peer, peer); |
| 1091 | peer->group = NULL; |
| 1092 | } |
| 1093 | |
| 1094 | /* Withdraw all information from routing table. We can not use |
| 1095 | BGP_EVENT_ADD (peer, BGP_Stop) at here. Because the event is |
| 1096 | executed after peer structure is deleted. */ |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1097 | peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1098 | bgp_stop (peer); |
| 1099 | bgp_fsm_change_status (peer, Idle); |
| 1100 | |
| 1101 | /* Stop all timers. */ |
| 1102 | BGP_TIMER_OFF (peer->t_start); |
| 1103 | BGP_TIMER_OFF (peer->t_connect); |
| 1104 | BGP_TIMER_OFF (peer->t_holdtime); |
| 1105 | BGP_TIMER_OFF (peer->t_keepalive); |
| 1106 | BGP_TIMER_OFF (peer->t_asorig); |
| 1107 | BGP_TIMER_OFF (peer->t_routeadv); |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 1108 | BGP_TIMER_OFF (peer->t_pmax_restart); |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 1109 | BGP_TIMER_OFF (peer->t_gr_restart); |
| 1110 | BGP_TIMER_OFF (peer->t_gr_stale); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1111 | |
| 1112 | /* Delete from all peer list. */ |
| 1113 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1114 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1115 | listnode_delete (bgp->peer, peer); |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1116 | if (peer_rsclient_active (peer)) |
| 1117 | listnode_delete (bgp->rsclient, peer); |
| 1118 | } |
| 1119 | |
| 1120 | /* Free RIB for any family in which peer is RSERVER_CLIENT, and is not |
| 1121 | member of a peer_group. */ |
| 1122 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1123 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 1124 | if (peer->rib[afi][safi] && ! peer->af_group[afi][safi]) |
| 1125 | bgp_table_finish (peer->rib[afi][safi]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1126 | |
| 1127 | /* Buffer. */ |
| 1128 | if (peer->ibuf) |
| 1129 | stream_free (peer->ibuf); |
| 1130 | |
| 1131 | if (peer->obuf) |
| 1132 | stream_fifo_free (peer->obuf); |
| 1133 | |
| 1134 | if (peer->work) |
| 1135 | stream_free (peer->work); |
| 1136 | |
| 1137 | /* Free allocated host character. */ |
| 1138 | if (peer->host) |
paul | e83e208 | 2005-05-19 02:12:25 +0000 | [diff] [blame] | 1139 | XFREE (MTYPE_BGP_PEER_HOST, peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1140 | |
| 1141 | /* Local and remote addresses. */ |
| 1142 | if (peer->su_local) |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 1143 | sockunion_free (peer->su_local); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1144 | if (peer->su_remote) |
paul | 22db9de | 2005-05-19 01:50:11 +0000 | [diff] [blame] | 1145 | sockunion_free (peer->su_remote); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1146 | |
| 1147 | /* Peer description string. */ |
| 1148 | if (peer->desc) |
paul | e83e208 | 2005-05-19 02:12:25 +0000 | [diff] [blame] | 1149 | XFREE (MTYPE_PEER_DESC, peer->desc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1150 | |
| 1151 | bgp_sync_delete (peer); |
| 1152 | |
| 1153 | /* Free filter related memory. */ |
| 1154 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1155 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 1156 | { |
| 1157 | filter = &peer->filter[afi][safi]; |
| 1158 | |
| 1159 | for (i = FILTER_IN; i < FILTER_MAX; i++) |
| 1160 | { |
| 1161 | if (filter->dlist[i].name) |
| 1162 | free (filter->dlist[i].name); |
| 1163 | if (filter->plist[i].name) |
| 1164 | free (filter->plist[i].name); |
| 1165 | if (filter->aslist[i].name) |
| 1166 | free (filter->aslist[i].name); |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1167 | } |
| 1168 | for (i = RMAP_IN; i < RMAP_MAX; i++) |
| 1169 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1170 | if (filter->map[i].name) |
| 1171 | free (filter->map[i].name); |
| 1172 | } |
| 1173 | |
| 1174 | if (filter->usmap.name) |
| 1175 | free (filter->usmap.name); |
| 1176 | |
| 1177 | if (peer->default_rmap[afi][safi].name) |
| 1178 | free (peer->default_rmap[afi][safi].name); |
| 1179 | } |
| 1180 | |
| 1181 | /* Update source configuration. */ |
| 1182 | if (peer->update_source) |
| 1183 | { |
| 1184 | sockunion_free (peer->update_source); |
| 1185 | peer->update_source = NULL; |
| 1186 | } |
| 1187 | if (peer->update_if) |
| 1188 | { |
| 1189 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 1190 | peer->update_if = NULL; |
| 1191 | } |
| 1192 | |
| 1193 | /* Free peer structure. */ |
| 1194 | XFREE (MTYPE_BGP_PEER, peer); |
| 1195 | |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | int |
| 1200 | peer_group_cmp (struct peer_group *g1, struct peer_group *g2) |
| 1201 | { |
| 1202 | return strcmp (g1->name, g2->name); |
| 1203 | } |
| 1204 | |
| 1205 | /* If peer is configured at least one address family return 1. */ |
| 1206 | int |
| 1207 | peer_group_active (struct peer *peer) |
| 1208 | { |
| 1209 | if (peer->af_group[AFI_IP][SAFI_UNICAST] |
| 1210 | || peer->af_group[AFI_IP][SAFI_MULTICAST] |
| 1211 | || peer->af_group[AFI_IP][SAFI_MPLS_VPN] |
| 1212 | || peer->af_group[AFI_IP6][SAFI_UNICAST] |
| 1213 | || peer->af_group[AFI_IP6][SAFI_MULTICAST]) |
| 1214 | return 1; |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | /* Peer group cofiguration. */ |
| 1219 | static struct peer_group * |
| 1220 | peer_group_new () |
| 1221 | { |
| 1222 | return (struct peer_group *) XCALLOC (MTYPE_PEER_GROUP, |
| 1223 | sizeof (struct peer_group)); |
| 1224 | } |
| 1225 | |
| 1226 | void |
| 1227 | peer_group_free (struct peer_group *group) |
| 1228 | { |
| 1229 | XFREE (MTYPE_PEER_GROUP, group); |
| 1230 | } |
| 1231 | |
| 1232 | struct peer_group * |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1233 | peer_group_lookup (struct bgp *bgp, const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1234 | { |
| 1235 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1236 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1237 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1238 | for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1239 | { |
| 1240 | if (strcmp (group->name, name) == 0) |
| 1241 | return group; |
| 1242 | } |
| 1243 | return NULL; |
| 1244 | } |
| 1245 | |
| 1246 | struct peer_group * |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1247 | peer_group_get (struct bgp *bgp, const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1248 | { |
| 1249 | struct peer_group *group; |
| 1250 | |
| 1251 | group = peer_group_lookup (bgp, name); |
| 1252 | if (group) |
| 1253 | return group; |
| 1254 | |
| 1255 | group = peer_group_new (); |
| 1256 | group->bgp = bgp; |
| 1257 | group->name = strdup (name); |
| 1258 | group->peer = list_new (); |
| 1259 | group->conf = peer_new (); |
| 1260 | if (! bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4)) |
| 1261 | group->conf->afc[AFI_IP][SAFI_UNICAST] = 1; |
| 1262 | group->conf->host = strdup (name); |
| 1263 | group->conf->bgp = bgp; |
| 1264 | group->conf->group = group; |
| 1265 | group->conf->as = 0; |
| 1266 | group->conf->ttl = 1; |
| 1267 | group->conf->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; |
| 1268 | UNSET_FLAG (group->conf->config, PEER_CONFIG_TIMER); |
| 1269 | UNSET_FLAG (group->conf->config, PEER_CONFIG_CONNECT); |
| 1270 | group->conf->keepalive = 0; |
| 1271 | group->conf->holdtime = 0; |
| 1272 | group->conf->connect = 0; |
| 1273 | SET_FLAG (group->conf->sflags, PEER_STATUS_GROUP); |
| 1274 | listnode_add_sort (bgp->group, group); |
| 1275 | |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | void |
| 1280 | peer_group2peer_config_copy (struct peer_group *group, struct peer *peer, |
| 1281 | afi_t afi, safi_t safi) |
| 1282 | { |
| 1283 | int in = FILTER_IN; |
| 1284 | int out = FILTER_OUT; |
| 1285 | struct peer *conf; |
| 1286 | struct bgp_filter *pfilter; |
| 1287 | struct bgp_filter *gfilter; |
| 1288 | |
| 1289 | conf = group->conf; |
| 1290 | pfilter = &peer->filter[afi][safi]; |
| 1291 | gfilter = &conf->filter[afi][safi]; |
| 1292 | |
| 1293 | /* remote-as */ |
| 1294 | if (conf->as) |
| 1295 | peer->as = conf->as; |
| 1296 | |
| 1297 | /* remote-as */ |
| 1298 | if (conf->change_local_as) |
| 1299 | peer->change_local_as = conf->change_local_as; |
| 1300 | |
| 1301 | /* TTL */ |
| 1302 | peer->ttl = conf->ttl; |
| 1303 | |
| 1304 | /* Weight */ |
| 1305 | peer->weight = conf->weight; |
| 1306 | |
| 1307 | /* peer flags apply */ |
| 1308 | peer->flags = conf->flags; |
| 1309 | /* peer af_flags apply */ |
| 1310 | peer->af_flags[afi][safi] = conf->af_flags[afi][safi]; |
| 1311 | /* peer config apply */ |
| 1312 | peer->config = conf->config; |
| 1313 | |
| 1314 | /* peer timers apply */ |
| 1315 | peer->holdtime = conf->holdtime; |
| 1316 | peer->keepalive = conf->keepalive; |
| 1317 | peer->connect = conf->connect; |
| 1318 | if (CHECK_FLAG (conf->config, PEER_CONFIG_CONNECT)) |
| 1319 | peer->v_connect = conf->connect; |
| 1320 | else |
| 1321 | peer->v_connect = BGP_DEFAULT_CONNECT_RETRY; |
| 1322 | |
| 1323 | /* advertisement-interval reset */ |
| 1324 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 1325 | peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV; |
| 1326 | else |
| 1327 | peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; |
| 1328 | |
| 1329 | /* maximum-prefix */ |
| 1330 | peer->pmax[afi][safi] = conf->pmax[afi][safi]; |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1331 | peer->pmax_threshold[afi][safi] = conf->pmax_threshold[afi][safi]; |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 1332 | peer->pmax_restart[afi][safi] = conf->pmax_restart[afi][safi]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1333 | |
| 1334 | /* allowas-in */ |
| 1335 | peer->allowas_in[afi][safi] = conf->allowas_in[afi][safi]; |
| 1336 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1337 | /* route-server-client */ |
| 1338 | if (CHECK_FLAG(conf->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)) |
| 1339 | { |
| 1340 | /* Make peer's RIB point to group's RIB. */ |
| 1341 | peer->rib[afi][safi] = group->conf->rib[afi][safi]; |
| 1342 | |
| 1343 | /* Import policy. */ |
| 1344 | if (pfilter->map[RMAP_IMPORT].name) |
| 1345 | free (pfilter->map[RMAP_IMPORT].name); |
| 1346 | if (gfilter->map[RMAP_IMPORT].name) |
| 1347 | { |
| 1348 | pfilter->map[RMAP_IMPORT].name = strdup (gfilter->map[RMAP_IMPORT].name); |
| 1349 | pfilter->map[RMAP_IMPORT].map = gfilter->map[RMAP_IMPORT].map; |
| 1350 | } |
| 1351 | else |
| 1352 | { |
| 1353 | pfilter->map[RMAP_IMPORT].name = NULL; |
| 1354 | pfilter->map[RMAP_IMPORT].map = NULL; |
| 1355 | } |
| 1356 | |
| 1357 | /* Export policy. */ |
| 1358 | if (gfilter->map[RMAP_EXPORT].name && ! pfilter->map[RMAP_EXPORT].name) |
| 1359 | { |
| 1360 | pfilter->map[RMAP_EXPORT].name = strdup (gfilter->map[RMAP_EXPORT].name); |
| 1361 | pfilter->map[RMAP_EXPORT].map = gfilter->map[RMAP_EXPORT].map; |
| 1362 | } |
| 1363 | } |
| 1364 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1365 | /* default-originate route-map */ |
| 1366 | if (conf->default_rmap[afi][safi].name) |
| 1367 | { |
| 1368 | if (peer->default_rmap[afi][safi].name) |
| 1369 | free (peer->default_rmap[afi][safi].name); |
| 1370 | peer->default_rmap[afi][safi].name = strdup (conf->default_rmap[afi][safi].name); |
| 1371 | peer->default_rmap[afi][safi].map = conf->default_rmap[afi][safi].map; |
| 1372 | } |
| 1373 | |
| 1374 | /* update-source apply */ |
| 1375 | if (conf->update_source) |
| 1376 | { |
| 1377 | if (peer->update_source) |
| 1378 | sockunion_free (peer->update_source); |
| 1379 | if (peer->update_if) |
| 1380 | { |
| 1381 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 1382 | peer->update_if = NULL; |
| 1383 | } |
| 1384 | peer->update_source = sockunion_dup (conf->update_source); |
| 1385 | } |
| 1386 | else if (conf->update_if) |
| 1387 | { |
| 1388 | if (peer->update_if) |
| 1389 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 1390 | if (peer->update_source) |
| 1391 | { |
| 1392 | sockunion_free (peer->update_source); |
| 1393 | peer->update_source = NULL; |
| 1394 | } |
| 1395 | peer->update_if = XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, conf->update_if); |
| 1396 | } |
| 1397 | |
| 1398 | /* inbound filter apply */ |
| 1399 | if (gfilter->dlist[in].name && ! pfilter->dlist[in].name) |
| 1400 | { |
| 1401 | if (pfilter->dlist[in].name) |
| 1402 | free (pfilter->dlist[in].name); |
| 1403 | pfilter->dlist[in].name = strdup (gfilter->dlist[in].name); |
| 1404 | pfilter->dlist[in].alist = gfilter->dlist[in].alist; |
| 1405 | } |
| 1406 | if (gfilter->plist[in].name && ! pfilter->plist[in].name) |
| 1407 | { |
| 1408 | if (pfilter->plist[in].name) |
| 1409 | free (pfilter->plist[in].name); |
| 1410 | pfilter->plist[in].name = strdup (gfilter->plist[in].name); |
| 1411 | pfilter->plist[in].plist = gfilter->plist[in].plist; |
| 1412 | } |
| 1413 | if (gfilter->aslist[in].name && ! pfilter->aslist[in].name) |
| 1414 | { |
| 1415 | if (pfilter->aslist[in].name) |
| 1416 | free (pfilter->aslist[in].name); |
| 1417 | pfilter->aslist[in].name = strdup (gfilter->aslist[in].name); |
| 1418 | pfilter->aslist[in].aslist = gfilter->aslist[in].aslist; |
| 1419 | } |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1420 | if (gfilter->map[RMAP_IN].name && ! pfilter->map[RMAP_IN].name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1421 | { |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1422 | if (pfilter->map[RMAP_IN].name) |
| 1423 | free (pfilter->map[RMAP_IN].name); |
| 1424 | pfilter->map[RMAP_IN].name = strdup (gfilter->map[RMAP_IN].name); |
| 1425 | pfilter->map[RMAP_IN].map = gfilter->map[RMAP_IN].map; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | /* outbound filter apply */ |
| 1429 | if (gfilter->dlist[out].name) |
| 1430 | { |
| 1431 | if (pfilter->dlist[out].name) |
| 1432 | free (pfilter->dlist[out].name); |
| 1433 | pfilter->dlist[out].name = strdup (gfilter->dlist[out].name); |
| 1434 | pfilter->dlist[out].alist = gfilter->dlist[out].alist; |
| 1435 | } |
| 1436 | else |
| 1437 | { |
| 1438 | if (pfilter->dlist[out].name) |
| 1439 | free (pfilter->dlist[out].name); |
| 1440 | pfilter->dlist[out].name = NULL; |
| 1441 | pfilter->dlist[out].alist = NULL; |
| 1442 | } |
| 1443 | if (gfilter->plist[out].name) |
| 1444 | { |
| 1445 | if (pfilter->plist[out].name) |
| 1446 | free (pfilter->plist[out].name); |
| 1447 | pfilter->plist[out].name = strdup (gfilter->plist[out].name); |
| 1448 | pfilter->plist[out].plist = gfilter->plist[out].plist; |
| 1449 | } |
| 1450 | else |
| 1451 | { |
| 1452 | if (pfilter->plist[out].name) |
| 1453 | free (pfilter->plist[out].name); |
| 1454 | pfilter->plist[out].name = NULL; |
| 1455 | pfilter->plist[out].plist = NULL; |
| 1456 | } |
| 1457 | if (gfilter->aslist[out].name) |
| 1458 | { |
| 1459 | if (pfilter->aslist[out].name) |
| 1460 | free (pfilter->aslist[out].name); |
| 1461 | pfilter->aslist[out].name = strdup (gfilter->aslist[out].name); |
| 1462 | pfilter->aslist[out].aslist = gfilter->aslist[out].aslist; |
| 1463 | } |
| 1464 | else |
| 1465 | { |
| 1466 | if (pfilter->aslist[out].name) |
| 1467 | free (pfilter->aslist[out].name); |
| 1468 | pfilter->aslist[out].name = NULL; |
| 1469 | pfilter->aslist[out].aslist = NULL; |
| 1470 | } |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1471 | if (gfilter->map[RMAP_OUT].name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1472 | { |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1473 | if (pfilter->map[RMAP_OUT].name) |
| 1474 | free (pfilter->map[RMAP_OUT].name); |
| 1475 | pfilter->map[RMAP_OUT].name = strdup (gfilter->map[RMAP_OUT].name); |
| 1476 | pfilter->map[RMAP_OUT].map = gfilter->map[RMAP_OUT].map; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1477 | } |
| 1478 | else |
| 1479 | { |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1480 | if (pfilter->map[RMAP_OUT].name) |
| 1481 | free (pfilter->map[RMAP_OUT].name); |
| 1482 | pfilter->map[RMAP_OUT].name = NULL; |
| 1483 | pfilter->map[RMAP_OUT].map = NULL; |
| 1484 | } |
| 1485 | |
| 1486 | /* RS-client's import/export route-maps. */ |
| 1487 | if (gfilter->map[RMAP_IMPORT].name) |
| 1488 | { |
| 1489 | if (pfilter->map[RMAP_IMPORT].name) |
| 1490 | free (pfilter->map[RMAP_IMPORT].name); |
| 1491 | pfilter->map[RMAP_IMPORT].name = strdup (gfilter->map[RMAP_IMPORT].name); |
| 1492 | pfilter->map[RMAP_IMPORT].map = gfilter->map[RMAP_IMPORT].map; |
| 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | if (pfilter->map[RMAP_IMPORT].name) |
| 1497 | free (pfilter->map[RMAP_IMPORT].name); |
| 1498 | pfilter->map[RMAP_IMPORT].name = NULL; |
| 1499 | pfilter->map[RMAP_IMPORT].map = NULL; |
| 1500 | } |
| 1501 | if (gfilter->map[RMAP_EXPORT].name && ! pfilter->map[RMAP_EXPORT].name) |
| 1502 | { |
| 1503 | if (pfilter->map[RMAP_EXPORT].name) |
| 1504 | free (pfilter->map[RMAP_EXPORT].name); |
| 1505 | pfilter->map[RMAP_EXPORT].name = strdup (gfilter->map[RMAP_EXPORT].name); |
| 1506 | pfilter->map[RMAP_EXPORT].map = gfilter->map[RMAP_EXPORT].map; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | if (gfilter->usmap.name) |
| 1510 | { |
| 1511 | if (pfilter->usmap.name) |
| 1512 | free (pfilter->usmap.name); |
| 1513 | pfilter->usmap.name = strdup (gfilter->usmap.name); |
| 1514 | pfilter->usmap.map = gfilter->usmap.map; |
| 1515 | } |
| 1516 | else |
| 1517 | { |
| 1518 | if (pfilter->usmap.name) |
| 1519 | free (pfilter->usmap.name); |
| 1520 | pfilter->usmap.name = NULL; |
| 1521 | pfilter->usmap.map = NULL; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | /* Peer group's remote AS configuration. */ |
| 1526 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1527 | peer_group_remote_as (struct bgp *bgp, const char *group_name, as_t *as) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1528 | { |
| 1529 | struct peer_group *group; |
| 1530 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1531 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1532 | |
| 1533 | group = peer_group_lookup (bgp, group_name); |
| 1534 | if (! group) |
| 1535 | return -1; |
| 1536 | |
| 1537 | if (group->conf->as == *as) |
| 1538 | return 0; |
| 1539 | |
| 1540 | /* When we setup peer-group AS number all peer group member's AS |
| 1541 | number must be updated to same number. */ |
| 1542 | peer_as_change (group->conf, *as); |
| 1543 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1544 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1545 | { |
| 1546 | if (peer->as != *as) |
| 1547 | peer_as_change (peer, *as); |
| 1548 | } |
| 1549 | |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | int |
| 1554 | peer_group_delete (struct peer_group *group) |
| 1555 | { |
| 1556 | struct bgp *bgp; |
| 1557 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1558 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1559 | |
| 1560 | bgp = group->bgp; |
| 1561 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1562 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1563 | { |
| 1564 | peer->group = NULL; |
| 1565 | peer_delete (peer); |
| 1566 | } |
| 1567 | list_delete (group->peer); |
| 1568 | |
| 1569 | free (group->name); |
| 1570 | group->name = NULL; |
| 1571 | |
| 1572 | group->conf->group = NULL; |
| 1573 | peer_delete (group->conf); |
| 1574 | |
| 1575 | /* Delete from all peer_group list. */ |
| 1576 | listnode_delete (bgp->group, group); |
| 1577 | |
| 1578 | peer_group_free (group); |
| 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | int |
| 1584 | peer_group_remote_as_delete (struct peer_group *group) |
| 1585 | { |
| 1586 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1587 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1588 | |
| 1589 | if (! group->conf->as) |
| 1590 | return 0; |
| 1591 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1592 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1593 | { |
| 1594 | peer->group = NULL; |
| 1595 | peer_delete (peer); |
| 1596 | } |
| 1597 | list_delete_all_node (group->peer); |
| 1598 | |
| 1599 | group->conf->as = 0; |
| 1600 | |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | /* Bind specified peer to peer group. */ |
| 1605 | int |
| 1606 | peer_group_bind (struct bgp *bgp, union sockunion *su, |
| 1607 | struct peer_group *group, afi_t afi, safi_t safi, as_t *as) |
| 1608 | { |
| 1609 | struct peer *peer; |
| 1610 | int first_member = 0; |
| 1611 | |
| 1612 | /* Check peer group's address family. */ |
| 1613 | if (! group->conf->afc[afi][safi]) |
| 1614 | return BGP_ERR_PEER_GROUP_AF_UNCONFIGURED; |
| 1615 | |
| 1616 | /* Lookup the peer. */ |
| 1617 | peer = peer_lookup (bgp, su); |
| 1618 | |
| 1619 | /* Create a new peer. */ |
| 1620 | if (! peer) |
| 1621 | { |
| 1622 | if (! group->conf->as) |
| 1623 | return BGP_ERR_PEER_GROUP_NO_REMOTE_AS; |
| 1624 | |
| 1625 | peer = peer_create (su, bgp, bgp->as, group->conf->as, afi, safi); |
| 1626 | peer->group = group; |
| 1627 | peer->af_group[afi][safi] = 1; |
| 1628 | listnode_add (group->peer, peer); |
| 1629 | peer_group2peer_config_copy (group, peer, afi, safi); |
| 1630 | |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | /* When the peer already belongs to peer group, check the consistency. */ |
| 1635 | if (peer->af_group[afi][safi]) |
| 1636 | { |
| 1637 | if (strcmp (peer->group->name, group->name) != 0) |
| 1638 | return BGP_ERR_PEER_GROUP_CANT_CHANGE; |
| 1639 | |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
| 1643 | /* Check current peer group configuration. */ |
| 1644 | if (peer_group_active (peer) |
| 1645 | && strcmp (peer->group->name, group->name) != 0) |
| 1646 | return BGP_ERR_PEER_GROUP_MISMATCH; |
| 1647 | |
| 1648 | if (! group->conf->as) |
| 1649 | { |
| 1650 | if (peer_sort (group->conf) != BGP_PEER_INTERNAL |
| 1651 | && peer_sort (group->conf) != peer_sort (peer)) |
| 1652 | { |
| 1653 | if (as) |
| 1654 | *as = peer->as; |
| 1655 | return BGP_ERR_PEER_GROUP_PEER_TYPE_DIFFERENT; |
| 1656 | } |
| 1657 | |
| 1658 | if (peer_sort (group->conf) == BGP_PEER_INTERNAL) |
| 1659 | first_member = 1; |
| 1660 | } |
| 1661 | |
| 1662 | peer->af_group[afi][safi] = 1; |
| 1663 | peer->afc[afi][safi] = 1; |
| 1664 | if (! peer->group) |
| 1665 | { |
| 1666 | peer->group = group; |
| 1667 | listnode_add (group->peer, peer); |
| 1668 | } |
| 1669 | |
| 1670 | if (first_member) |
| 1671 | { |
| 1672 | /* Advertisement-interval reset */ |
| 1673 | if (peer_sort (group->conf) == BGP_PEER_IBGP) |
| 1674 | group->conf->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV; |
| 1675 | else |
| 1676 | group->conf->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; |
| 1677 | |
| 1678 | /* ebgp-multihop reset */ |
| 1679 | if (peer_sort (group->conf) == BGP_PEER_IBGP) |
| 1680 | group->conf->ttl = 255; |
| 1681 | |
| 1682 | /* local-as reset */ |
| 1683 | if (peer_sort (group->conf) != BGP_PEER_EBGP) |
| 1684 | { |
| 1685 | group->conf->change_local_as = 0; |
| 1686 | UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 1687 | } |
| 1688 | } |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1689 | |
| 1690 | if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)) |
| 1691 | { |
| 1692 | /* If it's not configured as RSERVER_CLIENT in any other address |
| 1693 | family, without being member of a peer_group, remove it from |
| 1694 | list bgp->rsclient.*/ |
| 1695 | if (! peer_rsclient_active (peer)) |
| 1696 | listnode_delete (bgp->rsclient, peer); |
| 1697 | |
| 1698 | bgp_table_finish (peer->rib[afi][safi]); |
| 1699 | |
| 1700 | /* Import policy. */ |
| 1701 | if (peer->filter[afi][safi].map[RMAP_IMPORT].name) |
| 1702 | { |
| 1703 | free (peer->filter[afi][safi].map[RMAP_IMPORT].name); |
| 1704 | peer->filter[afi][safi].map[RMAP_IMPORT].name = NULL; |
| 1705 | peer->filter[afi][safi].map[RMAP_IMPORT].map = NULL; |
| 1706 | } |
| 1707 | |
| 1708 | /* Export policy. */ |
| 1709 | if (! CHECK_FLAG(group->conf->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT) |
| 1710 | && peer->filter[afi][safi].map[RMAP_EXPORT].name) |
| 1711 | { |
| 1712 | free (peer->filter[afi][safi].map[RMAP_EXPORT].name); |
| 1713 | peer->filter[afi][safi].map[RMAP_EXPORT].name = NULL; |
| 1714 | peer->filter[afi][safi].map[RMAP_EXPORT].map = NULL; |
| 1715 | } |
| 1716 | } |
| 1717 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1718 | peer_group2peer_config_copy (group, peer, afi, safi); |
| 1719 | |
| 1720 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1721 | { |
| 1722 | peer->last_reset = PEER_DOWN_RMAP_BIND; |
| 1723 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 1724 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 1725 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1726 | else |
| 1727 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 1728 | |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | int |
| 1733 | peer_group_unbind (struct bgp *bgp, struct peer *peer, |
| 1734 | struct peer_group *group, afi_t afi, safi_t safi) |
| 1735 | { |
| 1736 | if (! peer->af_group[afi][safi]) |
| 1737 | return 0; |
| 1738 | |
| 1739 | if (group != peer->group) |
| 1740 | return BGP_ERR_PEER_GROUP_MISMATCH; |
| 1741 | |
| 1742 | peer->af_group[afi][safi] = 0; |
| 1743 | peer->afc[afi][safi] = 0; |
| 1744 | peer_af_flag_reset (peer, afi, safi); |
| 1745 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1746 | if (peer->rib[afi][safi]) |
| 1747 | peer->rib[afi][safi] = NULL; |
| 1748 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1749 | if (! peer_group_active (peer)) |
| 1750 | { |
| 1751 | listnode_delete (group->peer, peer); |
| 1752 | peer->group = NULL; |
| 1753 | if (group->conf->as) |
| 1754 | { |
| 1755 | peer_delete (peer); |
| 1756 | return 0; |
| 1757 | } |
| 1758 | peer_global_config_reset (peer); |
| 1759 | } |
| 1760 | |
| 1761 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1762 | { |
| 1763 | peer->last_reset = PEER_DOWN_RMAP_UNBIND; |
| 1764 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 1765 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 1766 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1767 | else |
| 1768 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 1769 | |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | /* BGP instance creation by `router bgp' commands. */ |
| 1774 | struct bgp * |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1775 | bgp_create (as_t *as, const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1776 | { |
| 1777 | struct bgp *bgp; |
| 1778 | afi_t afi; |
| 1779 | safi_t safi; |
| 1780 | |
| 1781 | bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp)); |
| 1782 | |
| 1783 | bgp->peer_self = peer_new (); |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1784 | bgp->peer_self->host = strdup ("Static announcement"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1785 | |
| 1786 | bgp->peer = list_new (); |
| 1787 | bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp; |
| 1788 | |
| 1789 | bgp->group = list_new (); |
| 1790 | bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp; |
| 1791 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1792 | bgp->rsclient = list_new (); |
| 1793 | bgp->rsclient->cmp = (int (*)(void*, void*)) peer_cmp; |
| 1794 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1795 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1796 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 1797 | { |
| 1798 | bgp->route[afi][safi] = bgp_table_init (); |
| 1799 | bgp->aggregate[afi][safi] = bgp_table_init (); |
| 1800 | bgp->rib[afi][safi] = bgp_table_init (); |
| 1801 | } |
| 1802 | |
| 1803 | bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF; |
| 1804 | bgp->default_holdtime = BGP_DEFAULT_HOLDTIME; |
| 1805 | bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE; |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 1806 | bgp->restart_time = BGP_DEFAULT_RESTART_TIME; |
| 1807 | bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1808 | |
| 1809 | bgp->as = *as; |
| 1810 | |
| 1811 | if (name) |
| 1812 | bgp->name = strdup (name); |
| 1813 | |
| 1814 | return bgp; |
| 1815 | } |
| 1816 | |
| 1817 | /* Return first entry of BGP. */ |
| 1818 | struct bgp * |
| 1819 | bgp_get_default () |
| 1820 | { |
| 1821 | if (bm->bgp->head) |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1822 | return (listgetdata (listhead (bm->bgp))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1823 | return NULL; |
| 1824 | } |
| 1825 | |
| 1826 | /* Lookup BGP entry. */ |
| 1827 | struct bgp * |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1828 | bgp_lookup (as_t as, const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1829 | { |
| 1830 | struct bgp *bgp; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1831 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1832 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1833 | for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1834 | if (bgp->as == as |
| 1835 | && ((bgp->name == NULL && name == NULL) |
| 1836 | || (bgp->name && name && strcmp (bgp->name, name) == 0))) |
| 1837 | return bgp; |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | |
| 1841 | /* Lookup BGP structure by view name. */ |
| 1842 | struct bgp * |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1843 | bgp_lookup_by_name (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1844 | { |
| 1845 | struct bgp *bgp; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1846 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1847 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1848 | for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1849 | if ((bgp->name == NULL && name == NULL) |
| 1850 | || (bgp->name && name && strcmp (bgp->name, name) == 0)) |
| 1851 | return bgp; |
| 1852 | return NULL; |
| 1853 | } |
| 1854 | |
| 1855 | /* Called from VTY commands. */ |
| 1856 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1857 | bgp_get (struct bgp **bgp_val, as_t *as, const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1858 | { |
| 1859 | struct bgp *bgp; |
| 1860 | |
| 1861 | /* Multiple instance check. */ |
| 1862 | if (bgp_option_check (BGP_OPT_MULTIPLE_INSTANCE)) |
| 1863 | { |
| 1864 | if (name) |
| 1865 | bgp = bgp_lookup_by_name (name); |
| 1866 | else |
| 1867 | bgp = bgp_get_default (); |
| 1868 | |
| 1869 | /* Already exists. */ |
| 1870 | if (bgp) |
| 1871 | { |
| 1872 | if (bgp->as != *as) |
| 1873 | { |
| 1874 | *as = bgp->as; |
| 1875 | return BGP_ERR_INSTANCE_MISMATCH; |
| 1876 | } |
| 1877 | *bgp_val = bgp; |
| 1878 | return 0; |
| 1879 | } |
| 1880 | } |
| 1881 | else |
| 1882 | { |
| 1883 | /* BGP instance name can not be specified for single instance. */ |
| 1884 | if (name) |
| 1885 | return BGP_ERR_MULTIPLE_INSTANCE_NOT_SET; |
| 1886 | |
| 1887 | /* Get default BGP structure if exists. */ |
| 1888 | bgp = bgp_get_default (); |
| 1889 | |
| 1890 | if (bgp) |
| 1891 | { |
| 1892 | if (bgp->as != *as) |
| 1893 | { |
| 1894 | *as = bgp->as; |
| 1895 | return BGP_ERR_AS_MISMATCH; |
| 1896 | } |
| 1897 | *bgp_val = bgp; |
| 1898 | return 0; |
| 1899 | } |
| 1900 | } |
| 1901 | |
| 1902 | bgp = bgp_create (as, name); |
| 1903 | listnode_add (bm->bgp, bgp); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1904 | bgp_router_id_set(bgp, &router_id_zebra); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1905 | *bgp_val = bgp; |
| 1906 | |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
| 1910 | /* Delete BGP instance. */ |
| 1911 | int |
| 1912 | bgp_delete (struct bgp *bgp) |
| 1913 | { |
| 1914 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1915 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1916 | struct listnode *next; |
| 1917 | afi_t afi; |
| 1918 | safi_t safi; |
| 1919 | int i; |
| 1920 | |
| 1921 | /* Delete static route. */ |
| 1922 | bgp_static_delete (bgp); |
| 1923 | |
| 1924 | /* Unset redistribution. */ |
| 1925 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1926 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 1927 | if (i != ZEBRA_ROUTE_BGP) |
| 1928 | bgp_redistribute_unset (bgp, afi, i); |
| 1929 | |
| 1930 | bgp->group->del = (void (*)(void *)) peer_group_delete; |
| 1931 | list_delete (bgp->group); |
| 1932 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1933 | for (ALL_LIST_ELEMENTS (bgp->peer, node, next, peer)) |
| 1934 | peer_delete (peer); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1935 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 1936 | bgp->rsclient->del = (void (*)(void *)) peer_delete; |
| 1937 | list_delete (bgp->rsclient); |
| 1938 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1939 | listnode_delete (bm->bgp, bgp); |
| 1940 | |
| 1941 | if (bgp->name) |
| 1942 | free (bgp->name); |
| 1943 | |
| 1944 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1945 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 1946 | { |
| 1947 | if (bgp->route[afi][safi]) |
| 1948 | XFREE (MTYPE_ROUTE_TABLE, bgp->route[afi][safi]); |
| 1949 | if (bgp->aggregate[afi][safi]) |
| 1950 | XFREE (MTYPE_ROUTE_TABLE,bgp->aggregate[afi][safi]) ; |
| 1951 | if (bgp->rib[afi][safi]) |
| 1952 | XFREE (MTYPE_ROUTE_TABLE,bgp->rib[afi][safi]); |
| 1953 | } |
| 1954 | XFREE (MTYPE_BGP, bgp); |
| 1955 | |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
| 1959 | struct peer * |
| 1960 | peer_lookup (struct bgp *bgp, union sockunion *su) |
| 1961 | { |
| 1962 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1963 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (! bgp) |
| 1966 | bgp = bgp_get_default (); |
| 1967 | |
| 1968 | if (! bgp) |
| 1969 | return NULL; |
| 1970 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1971 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1972 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1973 | if (sockunion_same (&peer->su, su) |
| 1974 | && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 1975 | return peer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1976 | } |
| 1977 | return NULL; |
| 1978 | } |
| 1979 | |
| 1980 | struct peer * |
| 1981 | peer_lookup_with_open (union sockunion *su, as_t remote_as, |
| 1982 | struct in_addr *remote_id, int *as) |
| 1983 | { |
| 1984 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1985 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1986 | struct bgp *bgp; |
| 1987 | |
| 1988 | bgp = bgp_get_default (); |
| 1989 | if (! bgp) |
| 1990 | return NULL; |
| 1991 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1992 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1993 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1994 | if (sockunion_same (&peer->su, su) |
| 1995 | && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 1996 | { |
| 1997 | if (peer->as == remote_as |
| 1998 | && peer->remote_id.s_addr == remote_id->s_addr) |
| 1999 | return peer; |
| 2000 | if (peer->as == remote_as) |
| 2001 | *as = 1; |
| 2002 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2003 | } |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2004 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2005 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2006 | if (sockunion_same (&peer->su, su) |
| 2007 | && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 2008 | { |
| 2009 | if (peer->as == remote_as |
| 2010 | && peer->remote_id.s_addr == 0) |
| 2011 | return peer; |
| 2012 | if (peer->as == remote_as) |
| 2013 | *as = 1; |
| 2014 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2015 | } |
| 2016 | return NULL; |
| 2017 | } |
| 2018 | |
| 2019 | /* If peer is configured at least one address family return 1. */ |
| 2020 | int |
| 2021 | peer_active (struct peer *peer) |
| 2022 | { |
| 2023 | if (peer->afc[AFI_IP][SAFI_UNICAST] |
| 2024 | || peer->afc[AFI_IP][SAFI_MULTICAST] |
| 2025 | || peer->afc[AFI_IP][SAFI_MPLS_VPN] |
| 2026 | || peer->afc[AFI_IP6][SAFI_UNICAST] |
| 2027 | || peer->afc[AFI_IP6][SAFI_MULTICAST]) |
| 2028 | return 1; |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | /* If peer is negotiated at least one address family return 1. */ |
| 2033 | int |
| 2034 | peer_active_nego (struct peer *peer) |
| 2035 | { |
| 2036 | if (peer->afc_nego[AFI_IP][SAFI_UNICAST] |
| 2037 | || peer->afc_nego[AFI_IP][SAFI_MULTICAST] |
| 2038 | || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] |
| 2039 | || peer->afc_nego[AFI_IP6][SAFI_UNICAST] |
| 2040 | || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]) |
| 2041 | return 1; |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | /* peer_flag_change_type. */ |
| 2046 | enum peer_change_type |
| 2047 | { |
| 2048 | peer_change_none, |
| 2049 | peer_change_reset, |
| 2050 | peer_change_reset_in, |
| 2051 | peer_change_reset_out, |
| 2052 | }; |
| 2053 | |
| 2054 | void |
| 2055 | peer_change_action (struct peer *peer, afi_t afi, safi_t safi, |
| 2056 | enum peer_change_type type) |
| 2057 | { |
| 2058 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2059 | return; |
| 2060 | |
| 2061 | if (type == peer_change_reset) |
| 2062 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2063 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2064 | else if (type == peer_change_reset_in) |
| 2065 | { |
| 2066 | if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV) |
| 2067 | || CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV)) |
| 2068 | bgp_route_refresh_send (peer, afi, safi, 0, 0, 0); |
| 2069 | else |
| 2070 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2071 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2072 | } |
| 2073 | else if (type == peer_change_reset_out) |
| 2074 | bgp_announce_route (peer, afi, safi); |
| 2075 | } |
| 2076 | |
| 2077 | struct peer_flag_action |
| 2078 | { |
| 2079 | /* Peer's flag. */ |
| 2080 | u_int32_t flag; |
| 2081 | |
| 2082 | /* This flag can be set for peer-group member. */ |
| 2083 | u_char not_for_member; |
| 2084 | |
| 2085 | /* Action when the flag is changed. */ |
| 2086 | enum peer_change_type type; |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2087 | |
| 2088 | /* Peer down cause */ |
| 2089 | u_char peer_down; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2090 | }; |
| 2091 | |
| 2092 | struct peer_flag_action peer_flag_action_list[] = |
| 2093 | { |
| 2094 | { PEER_FLAG_PASSIVE, 0, peer_change_reset }, |
| 2095 | { PEER_FLAG_SHUTDOWN, 0, peer_change_reset }, |
| 2096 | { PEER_FLAG_DONT_CAPABILITY, 0, peer_change_none }, |
| 2097 | { PEER_FLAG_OVERRIDE_CAPABILITY, 0, peer_change_none }, |
| 2098 | { PEER_FLAG_STRICT_CAP_MATCH, 0, peer_change_none }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2099 | { PEER_FLAG_DYNAMIC_CAPABILITY, 0, peer_change_reset }, |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 2100 | { PEER_FLAG_DISABLE_CONNECTED_CHECK, 0, peer_change_reset }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2101 | { 0, 0, 0 } |
| 2102 | }; |
| 2103 | |
| 2104 | struct peer_flag_action peer_af_flag_action_list[] = |
| 2105 | { |
| 2106 | { PEER_FLAG_NEXTHOP_SELF, 1, peer_change_reset_out }, |
| 2107 | { PEER_FLAG_SEND_COMMUNITY, 1, peer_change_reset_out }, |
| 2108 | { PEER_FLAG_SEND_EXT_COMMUNITY, 1, peer_change_reset_out }, |
| 2109 | { PEER_FLAG_SOFT_RECONFIG, 0, peer_change_reset_in }, |
| 2110 | { PEER_FLAG_REFLECTOR_CLIENT, 1, peer_change_reset }, |
| 2111 | { PEER_FLAG_RSERVER_CLIENT, 1, peer_change_reset }, |
| 2112 | { PEER_FLAG_AS_PATH_UNCHANGED, 1, peer_change_reset_out }, |
| 2113 | { PEER_FLAG_NEXTHOP_UNCHANGED, 1, peer_change_reset_out }, |
| 2114 | { PEER_FLAG_MED_UNCHANGED, 1, peer_change_reset_out }, |
| 2115 | { PEER_FLAG_REMOVE_PRIVATE_AS, 1, peer_change_reset_out }, |
| 2116 | { PEER_FLAG_ALLOWAS_IN, 0, peer_change_reset_in }, |
| 2117 | { PEER_FLAG_ORF_PREFIX_SM, 1, peer_change_reset }, |
| 2118 | { PEER_FLAG_ORF_PREFIX_RM, 1, peer_change_reset }, |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 2119 | { PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED, 0, peer_change_reset_out }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2120 | { 0, 0, 0 } |
| 2121 | }; |
| 2122 | |
| 2123 | /* Proper action set. */ |
| 2124 | int |
| 2125 | peer_flag_action_set (struct peer_flag_action *action_list, int size, |
| 2126 | struct peer_flag_action *action, u_int32_t flag) |
| 2127 | { |
| 2128 | int i; |
| 2129 | int found = 0; |
| 2130 | int reset_in = 0; |
| 2131 | int reset_out = 0; |
| 2132 | struct peer_flag_action *match = NULL; |
| 2133 | |
| 2134 | /* Check peer's frag action. */ |
| 2135 | for (i = 0; i < size; i++) |
| 2136 | { |
| 2137 | match = &action_list[i]; |
| 2138 | |
| 2139 | if (match->flag == 0) |
| 2140 | break; |
| 2141 | |
| 2142 | if (match->flag & flag) |
| 2143 | { |
| 2144 | found = 1; |
| 2145 | |
| 2146 | if (match->type == peer_change_reset_in) |
| 2147 | reset_in = 1; |
| 2148 | if (match->type == peer_change_reset_out) |
| 2149 | reset_out = 1; |
| 2150 | if (match->type == peer_change_reset) |
| 2151 | { |
| 2152 | reset_in = 1; |
| 2153 | reset_out = 1; |
| 2154 | } |
| 2155 | if (match->not_for_member) |
| 2156 | action->not_for_member = 1; |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | /* Set peer clear type. */ |
| 2161 | if (reset_in && reset_out) |
| 2162 | action->type = peer_change_reset; |
| 2163 | else if (reset_in) |
| 2164 | action->type = peer_change_reset_in; |
| 2165 | else if (reset_out) |
| 2166 | action->type = peer_change_reset_out; |
| 2167 | else |
| 2168 | action->type = peer_change_none; |
| 2169 | |
| 2170 | return found; |
| 2171 | } |
| 2172 | |
| 2173 | void |
| 2174 | peer_flag_modify_action (struct peer *peer, u_int32_t flag) |
| 2175 | { |
| 2176 | if (flag == PEER_FLAG_SHUTDOWN) |
| 2177 | { |
| 2178 | if (CHECK_FLAG (peer->flags, flag)) |
| 2179 | { |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 2180 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)) |
| 2181 | peer_nsf_stop (peer); |
| 2182 | |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 2183 | UNSET_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW); |
| 2184 | if (peer->t_pmax_restart) |
| 2185 | { |
| 2186 | BGP_TIMER_OFF (peer->t_pmax_restart); |
| 2187 | if (BGP_DEBUG (events, EVENTS)) |
| 2188 | zlog_debug ("%s Maximum-prefix restart timer canceled", |
| 2189 | peer->host); |
| 2190 | } |
| 2191 | |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 2192 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT)) |
| 2193 | peer_nsf_stop (peer); |
| 2194 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2195 | if (peer->status == Established) |
| 2196 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2197 | BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN); |
| 2198 | else |
| 2199 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2200 | } |
| 2201 | else |
| 2202 | { |
| 2203 | peer->v_start = BGP_INIT_START_TIMER; |
| 2204 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2205 | } |
| 2206 | } |
| 2207 | else if (peer->status == Established) |
| 2208 | { |
hasso | c950243 | 2005-02-01 22:01:48 +0000 | [diff] [blame] | 2209 | if (flag == PEER_FLAG_DYNAMIC_CAPABILITY) |
| 2210 | peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE; |
| 2211 | else if (flag == PEER_FLAG_PASSIVE) |
| 2212 | peer->last_reset = PEER_DOWN_PASSIVE_CHANGE; |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 2213 | else if (flag == PEER_FLAG_DISABLE_CONNECTED_CHECK) |
hasso | c950243 | 2005-02-01 22:01:48 +0000 | [diff] [blame] | 2214 | peer->last_reset = PEER_DOWN_MULTIHOP_CHANGE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2215 | |
hasso | c950243 | 2005-02-01 22:01:48 +0000 | [diff] [blame] | 2216 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2217 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2218 | } |
| 2219 | else |
| 2220 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2221 | } |
| 2222 | |
| 2223 | /* Change specified peer flag. */ |
| 2224 | int |
| 2225 | peer_flag_modify (struct peer *peer, u_int32_t flag, int set) |
| 2226 | { |
| 2227 | int found; |
| 2228 | int size; |
| 2229 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2230 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2231 | struct peer_flag_action action; |
| 2232 | |
| 2233 | memset (&action, 0, sizeof (struct peer_flag_action)); |
| 2234 | size = sizeof peer_flag_action_list / sizeof (struct peer_flag_action); |
| 2235 | |
| 2236 | found = peer_flag_action_set (peer_flag_action_list, size, &action, flag); |
| 2237 | |
| 2238 | /* No flag action is found. */ |
| 2239 | if (! found) |
| 2240 | return BGP_ERR_INVALID_FLAG; |
| 2241 | |
| 2242 | /* Not for peer-group member. */ |
| 2243 | if (action.not_for_member && peer_group_active (peer)) |
| 2244 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 2245 | |
| 2246 | /* When unset the peer-group member's flag we have to check |
| 2247 | peer-group configuration. */ |
| 2248 | if (! set && peer_group_active (peer)) |
| 2249 | if (CHECK_FLAG (peer->group->conf->flags, flag)) |
| 2250 | { |
| 2251 | if (flag == PEER_FLAG_SHUTDOWN) |
| 2252 | return BGP_ERR_PEER_GROUP_SHUTDOWN; |
| 2253 | else |
| 2254 | return BGP_ERR_PEER_GROUP_HAS_THE_FLAG; |
| 2255 | } |
| 2256 | |
| 2257 | /* Flag conflict check. */ |
| 2258 | if (set |
| 2259 | && CHECK_FLAG (peer->flags | flag, PEER_FLAG_STRICT_CAP_MATCH) |
| 2260 | && CHECK_FLAG (peer->flags | flag, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 2261 | return BGP_ERR_PEER_FLAG_CONFLICT; |
| 2262 | |
| 2263 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2264 | { |
| 2265 | if (set && CHECK_FLAG (peer->flags, flag) == flag) |
| 2266 | return 0; |
| 2267 | if (! set && ! CHECK_FLAG (peer->flags, flag)) |
| 2268 | return 0; |
| 2269 | } |
| 2270 | |
| 2271 | if (set) |
| 2272 | SET_FLAG (peer->flags, flag); |
| 2273 | else |
| 2274 | UNSET_FLAG (peer->flags, flag); |
| 2275 | |
| 2276 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2277 | { |
| 2278 | if (action.type == peer_change_reset) |
| 2279 | peer_flag_modify_action (peer, flag); |
| 2280 | |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
| 2284 | /* peer-group member updates. */ |
| 2285 | group = peer->group; |
| 2286 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2287 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2288 | { |
| 2289 | if (set && CHECK_FLAG (peer->flags, flag) == flag) |
| 2290 | continue; |
| 2291 | |
| 2292 | if (! set && ! CHECK_FLAG (peer->flags, flag)) |
| 2293 | continue; |
| 2294 | |
| 2295 | if (set) |
| 2296 | SET_FLAG (peer->flags, flag); |
| 2297 | else |
| 2298 | UNSET_FLAG (peer->flags, flag); |
| 2299 | |
| 2300 | if (action.type == peer_change_reset) |
| 2301 | peer_flag_modify_action (peer, flag); |
| 2302 | } |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
| 2306 | int |
| 2307 | peer_flag_set (struct peer *peer, u_int32_t flag) |
| 2308 | { |
| 2309 | return peer_flag_modify (peer, flag, 1); |
| 2310 | } |
| 2311 | |
| 2312 | int |
| 2313 | peer_flag_unset (struct peer *peer, u_int32_t flag) |
| 2314 | { |
| 2315 | return peer_flag_modify (peer, flag, 0); |
| 2316 | } |
| 2317 | |
| 2318 | int |
| 2319 | peer_is_group_member (struct peer *peer, afi_t afi, safi_t safi) |
| 2320 | { |
| 2321 | if (peer->af_group[afi][safi]) |
| 2322 | return 1; |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | int |
| 2327 | peer_af_flag_modify (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag, |
| 2328 | int set) |
| 2329 | { |
| 2330 | int found; |
| 2331 | int size; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2332 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2333 | struct peer_group *group; |
| 2334 | struct peer_flag_action action; |
| 2335 | |
| 2336 | memset (&action, 0, sizeof (struct peer_flag_action)); |
| 2337 | size = sizeof peer_af_flag_action_list / sizeof (struct peer_flag_action); |
| 2338 | |
| 2339 | found = peer_flag_action_set (peer_af_flag_action_list, size, &action, flag); |
| 2340 | |
| 2341 | /* No flag action is found. */ |
| 2342 | if (! found) |
| 2343 | return BGP_ERR_INVALID_FLAG; |
| 2344 | |
| 2345 | /* Adress family must be activated. */ |
| 2346 | if (! peer->afc[afi][safi]) |
| 2347 | return BGP_ERR_PEER_INACTIVE; |
| 2348 | |
| 2349 | /* Not for peer-group member. */ |
| 2350 | if (action.not_for_member && peer_is_group_member (peer, afi, safi)) |
| 2351 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 2352 | |
| 2353 | /* Spcecial check for reflector client. */ |
| 2354 | if (flag & PEER_FLAG_REFLECTOR_CLIENT |
| 2355 | && peer_sort (peer) != BGP_PEER_IBGP) |
| 2356 | return BGP_ERR_NOT_INTERNAL_PEER; |
| 2357 | |
| 2358 | /* Spcecial check for remove-private-AS. */ |
| 2359 | if (flag & PEER_FLAG_REMOVE_PRIVATE_AS |
| 2360 | && peer_sort (peer) == BGP_PEER_IBGP) |
| 2361 | return BGP_ERR_REMOVE_PRIVATE_AS; |
| 2362 | |
| 2363 | /* When unset the peer-group member's flag we have to check |
| 2364 | peer-group configuration. */ |
| 2365 | if (! set && peer->af_group[afi][safi]) |
| 2366 | if (CHECK_FLAG (peer->group->conf->af_flags[afi][safi], flag)) |
| 2367 | return BGP_ERR_PEER_GROUP_HAS_THE_FLAG; |
| 2368 | |
| 2369 | /* When current flag configuration is same as requested one. */ |
| 2370 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2371 | { |
| 2372 | if (set && CHECK_FLAG (peer->af_flags[afi][safi], flag) == flag) |
| 2373 | return 0; |
| 2374 | if (! set && ! CHECK_FLAG (peer->af_flags[afi][safi], flag)) |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
| 2378 | if (set) |
| 2379 | SET_FLAG (peer->af_flags[afi][safi], flag); |
| 2380 | else |
| 2381 | UNSET_FLAG (peer->af_flags[afi][safi], flag); |
| 2382 | |
| 2383 | /* Execute action when peer is established. */ |
| 2384 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP) |
| 2385 | && peer->status == Established) |
| 2386 | { |
| 2387 | if (! set && flag == PEER_FLAG_SOFT_RECONFIG) |
| 2388 | bgp_clear_adj_in (peer, afi, safi); |
| 2389 | else |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2390 | { |
| 2391 | if (flag == PEER_FLAG_REFLECTOR_CLIENT) |
| 2392 | peer->last_reset = PEER_DOWN_RR_CLIENT_CHANGE; |
| 2393 | else if (flag == PEER_FLAG_RSERVER_CLIENT) |
| 2394 | peer->last_reset = PEER_DOWN_RS_CLIENT_CHANGE; |
| 2395 | else if (flag == PEER_FLAG_ORF_PREFIX_SM) |
| 2396 | peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE; |
| 2397 | else if (flag == PEER_FLAG_ORF_PREFIX_RM) |
| 2398 | peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE; |
| 2399 | |
| 2400 | peer_change_action (peer, afi, safi, action.type); |
| 2401 | } |
| 2402 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | /* Peer group member updates. */ |
| 2406 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2407 | { |
| 2408 | group = peer->group; |
| 2409 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2410 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2411 | { |
| 2412 | if (! peer->af_group[afi][safi]) |
| 2413 | continue; |
| 2414 | |
| 2415 | if (set && CHECK_FLAG (peer->af_flags[afi][safi], flag) == flag) |
| 2416 | continue; |
| 2417 | |
| 2418 | if (! set && ! CHECK_FLAG (peer->af_flags[afi][safi], flag)) |
| 2419 | continue; |
| 2420 | |
| 2421 | if (set) |
| 2422 | SET_FLAG (peer->af_flags[afi][safi], flag); |
| 2423 | else |
| 2424 | UNSET_FLAG (peer->af_flags[afi][safi], flag); |
| 2425 | |
| 2426 | if (peer->status == Established) |
| 2427 | { |
| 2428 | if (! set && flag == PEER_FLAG_SOFT_RECONFIG) |
| 2429 | bgp_clear_adj_in (peer, afi, safi); |
| 2430 | else |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2431 | { |
| 2432 | if (flag == PEER_FLAG_REFLECTOR_CLIENT) |
| 2433 | peer->last_reset = PEER_DOWN_RR_CLIENT_CHANGE; |
| 2434 | else if (flag == PEER_FLAG_RSERVER_CLIENT) |
| 2435 | peer->last_reset = PEER_DOWN_RS_CLIENT_CHANGE; |
| 2436 | else if (flag == PEER_FLAG_ORF_PREFIX_SM) |
| 2437 | peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE; |
| 2438 | else if (flag == PEER_FLAG_ORF_PREFIX_RM) |
| 2439 | peer->last_reset = PEER_DOWN_CAPABILITY_CHANGE; |
| 2440 | |
| 2441 | peer_change_action (peer, afi, safi, action.type); |
| 2442 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
| 2449 | int |
| 2450 | peer_af_flag_set (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag) |
| 2451 | { |
| 2452 | return peer_af_flag_modify (peer, afi, safi, flag, 1); |
| 2453 | } |
| 2454 | |
| 2455 | int |
| 2456 | peer_af_flag_unset (struct peer *peer, afi_t afi, safi_t safi, u_int32_t flag) |
| 2457 | { |
| 2458 | return peer_af_flag_modify (peer, afi, safi, flag, 0); |
| 2459 | } |
| 2460 | |
| 2461 | /* EBGP multihop configuration. */ |
| 2462 | int |
| 2463 | peer_ebgp_multihop_set (struct peer *peer, int ttl) |
| 2464 | { |
| 2465 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2466 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2467 | |
| 2468 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 2469 | return 0; |
| 2470 | |
| 2471 | peer->ttl = ttl; |
| 2472 | |
| 2473 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2474 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2475 | if (peer->fd >= 0 && peer_sort (peer) != BGP_PEER_IBGP) |
| 2476 | sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2477 | } |
| 2478 | else |
| 2479 | { |
| 2480 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2481 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2482 | { |
| 2483 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 2484 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2485 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2486 | peer->ttl = group->conf->ttl; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2487 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2488 | if (peer->fd >= 0) |
| 2489 | sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl); |
| 2490 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2491 | } |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | int |
| 2496 | peer_ebgp_multihop_unset (struct peer *peer) |
| 2497 | { |
| 2498 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2499 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2500 | |
| 2501 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 2502 | return 0; |
| 2503 | |
| 2504 | if (peer_group_active (peer)) |
| 2505 | peer->ttl = peer->group->conf->ttl; |
| 2506 | else |
| 2507 | peer->ttl = 1; |
| 2508 | |
| 2509 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2510 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2511 | if (peer->fd >= 0 && peer_sort (peer) != BGP_PEER_IBGP) |
| 2512 | sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2513 | } |
| 2514 | else |
| 2515 | { |
| 2516 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2517 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2518 | { |
| 2519 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 2520 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2521 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2522 | peer->ttl = 1; |
| 2523 | |
| 2524 | if (peer->fd >= 0) |
| 2525 | sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl); |
| 2526 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2527 | } |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
| 2531 | /* Neighbor description. */ |
| 2532 | int |
| 2533 | peer_description_set (struct peer *peer, char *desc) |
| 2534 | { |
| 2535 | if (peer->desc) |
| 2536 | XFREE (MTYPE_PEER_DESC, peer->desc); |
| 2537 | |
| 2538 | peer->desc = XSTRDUP (MTYPE_PEER_DESC, desc); |
| 2539 | |
| 2540 | return 0; |
| 2541 | } |
| 2542 | |
| 2543 | int |
| 2544 | peer_description_unset (struct peer *peer) |
| 2545 | { |
| 2546 | if (peer->desc) |
| 2547 | XFREE (MTYPE_PEER_DESC, peer->desc); |
| 2548 | |
| 2549 | peer->desc = NULL; |
| 2550 | |
| 2551 | return 0; |
| 2552 | } |
| 2553 | |
| 2554 | /* Neighbor update-source. */ |
| 2555 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 2556 | peer_update_source_if_set (struct peer *peer, const char *ifname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2557 | { |
| 2558 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2559 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2560 | |
| 2561 | if (peer->update_if) |
| 2562 | { |
| 2563 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP) |
| 2564 | && strcmp (peer->update_if, ifname) == 0) |
| 2565 | return 0; |
| 2566 | |
| 2567 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 2568 | peer->update_if = NULL; |
| 2569 | } |
| 2570 | |
| 2571 | if (peer->update_source) |
| 2572 | { |
| 2573 | sockunion_free (peer->update_source); |
| 2574 | peer->update_source = NULL; |
| 2575 | } |
| 2576 | |
| 2577 | peer->update_if = XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, ifname); |
| 2578 | |
| 2579 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2580 | { |
| 2581 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2582 | { |
| 2583 | peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE; |
| 2584 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2585 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2586 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2587 | else |
| 2588 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | /* peer-group member updates. */ |
| 2593 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2594 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2595 | { |
| 2596 | if (peer->update_if) |
| 2597 | { |
| 2598 | if (strcmp (peer->update_if, ifname) == 0) |
| 2599 | continue; |
| 2600 | |
| 2601 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 2602 | peer->update_if = NULL; |
| 2603 | } |
| 2604 | |
| 2605 | if (peer->update_source) |
| 2606 | { |
| 2607 | sockunion_free (peer->update_source); |
| 2608 | peer->update_source = NULL; |
| 2609 | } |
| 2610 | |
| 2611 | peer->update_if = XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, ifname); |
| 2612 | |
| 2613 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2614 | { |
| 2615 | peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE; |
| 2616 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2617 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2618 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2619 | else |
| 2620 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2621 | } |
| 2622 | return 0; |
| 2623 | } |
| 2624 | |
| 2625 | int |
| 2626 | peer_update_source_addr_set (struct peer *peer, union sockunion *su) |
| 2627 | { |
| 2628 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2629 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2630 | |
| 2631 | if (peer->update_source) |
| 2632 | { |
| 2633 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP) |
| 2634 | && sockunion_cmp (peer->update_source, su) == 0) |
| 2635 | return 0; |
| 2636 | sockunion_free (peer->update_source); |
| 2637 | peer->update_source = NULL; |
| 2638 | } |
| 2639 | |
| 2640 | if (peer->update_if) |
| 2641 | { |
| 2642 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 2643 | peer->update_if = NULL; |
| 2644 | } |
| 2645 | |
| 2646 | peer->update_source = sockunion_dup (su); |
| 2647 | |
| 2648 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2649 | { |
| 2650 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2651 | { |
| 2652 | peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE; |
| 2653 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2654 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2655 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2656 | else |
| 2657 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | /* peer-group member updates. */ |
| 2662 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2663 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2664 | { |
| 2665 | if (peer->update_source) |
| 2666 | { |
| 2667 | if (sockunion_cmp (peer->update_source, su) == 0) |
| 2668 | continue; |
| 2669 | sockunion_free (peer->update_source); |
| 2670 | peer->update_source = NULL; |
| 2671 | } |
| 2672 | |
| 2673 | if (peer->update_if) |
| 2674 | { |
| 2675 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 2676 | peer->update_if = NULL; |
| 2677 | } |
| 2678 | |
| 2679 | peer->update_source = sockunion_dup (su); |
| 2680 | |
| 2681 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2682 | { |
| 2683 | peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE; |
| 2684 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2685 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2686 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2687 | else |
| 2688 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2689 | } |
| 2690 | return 0; |
| 2691 | } |
| 2692 | |
| 2693 | int |
| 2694 | peer_update_source_unset (struct peer *peer) |
| 2695 | { |
| 2696 | union sockunion *su; |
| 2697 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2698 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2699 | |
| 2700 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP) |
| 2701 | && ! peer->update_source |
| 2702 | && ! peer->update_if) |
| 2703 | return 0; |
| 2704 | |
| 2705 | if (peer->update_source) |
| 2706 | { |
| 2707 | sockunion_free (peer->update_source); |
| 2708 | peer->update_source = NULL; |
| 2709 | } |
| 2710 | if (peer->update_if) |
| 2711 | { |
| 2712 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 2713 | peer->update_if = NULL; |
| 2714 | } |
| 2715 | |
| 2716 | if (peer_group_active (peer)) |
| 2717 | { |
| 2718 | group = peer->group; |
| 2719 | |
| 2720 | if (group->conf->update_source) |
| 2721 | { |
| 2722 | su = sockunion_dup (group->conf->update_source); |
| 2723 | peer->update_source = su; |
| 2724 | } |
| 2725 | else if (group->conf->update_if) |
| 2726 | peer->update_if = |
| 2727 | XSTRDUP (MTYPE_PEER_UPDATE_SOURCE, group->conf->update_if); |
| 2728 | } |
| 2729 | |
| 2730 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2731 | { |
| 2732 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2733 | { |
| 2734 | peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE; |
| 2735 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2736 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2737 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2738 | else |
| 2739 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2740 | return 0; |
| 2741 | } |
| 2742 | |
| 2743 | /* peer-group member updates. */ |
| 2744 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2745 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2746 | { |
| 2747 | if (! peer->update_source && ! peer->update_if) |
| 2748 | continue; |
| 2749 | |
| 2750 | if (peer->update_source) |
| 2751 | { |
| 2752 | sockunion_free (peer->update_source); |
| 2753 | peer->update_source = NULL; |
| 2754 | } |
| 2755 | |
| 2756 | if (peer->update_if) |
| 2757 | { |
| 2758 | XFREE (MTYPE_PEER_UPDATE_SOURCE, peer->update_if); |
| 2759 | peer->update_if = NULL; |
| 2760 | } |
| 2761 | |
| 2762 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2763 | { |
| 2764 | peer->last_reset = PEER_DOWN_UPDATE_SOURCE_CHANGE; |
| 2765 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 2766 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 2767 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2768 | else |
| 2769 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2770 | } |
| 2771 | return 0; |
| 2772 | } |
| 2773 | |
| 2774 | int |
| 2775 | peer_default_originate_set (struct peer *peer, afi_t afi, safi_t safi, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 2776 | const char *rmap) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2777 | { |
| 2778 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2779 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2780 | |
| 2781 | /* Adress family must be activated. */ |
| 2782 | if (! peer->afc[afi][safi]) |
| 2783 | return BGP_ERR_PEER_INACTIVE; |
| 2784 | |
| 2785 | /* Default originate can't be used for peer group memeber. */ |
| 2786 | if (peer_is_group_member (peer, afi, safi)) |
| 2787 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 2788 | |
| 2789 | if (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE) |
| 2790 | || (rmap && ! peer->default_rmap[afi][safi].name) |
| 2791 | || (rmap && strcmp (rmap, peer->default_rmap[afi][safi].name) != 0)) |
| 2792 | { |
| 2793 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE); |
| 2794 | |
| 2795 | if (rmap) |
| 2796 | { |
| 2797 | if (peer->default_rmap[afi][safi].name) |
| 2798 | free (peer->default_rmap[afi][safi].name); |
| 2799 | peer->default_rmap[afi][safi].name = strdup (rmap); |
| 2800 | peer->default_rmap[afi][safi].map = route_map_lookup_by_name (rmap); |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2805 | { |
| 2806 | if (peer->status == Established && peer->afc_nego[afi][safi]) |
| 2807 | bgp_default_originate (peer, afi, safi, 0); |
| 2808 | return 0; |
| 2809 | } |
| 2810 | |
| 2811 | /* peer-group member updates. */ |
| 2812 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2813 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2814 | { |
| 2815 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE); |
| 2816 | |
| 2817 | if (rmap) |
| 2818 | { |
| 2819 | if (peer->default_rmap[afi][safi].name) |
| 2820 | free (peer->default_rmap[afi][safi].name); |
| 2821 | peer->default_rmap[afi][safi].name = strdup (rmap); |
| 2822 | peer->default_rmap[afi][safi].map = route_map_lookup_by_name (rmap); |
| 2823 | } |
| 2824 | |
| 2825 | if (peer->status == Established && peer->afc_nego[afi][safi]) |
| 2826 | bgp_default_originate (peer, afi, safi, 0); |
| 2827 | } |
| 2828 | return 0; |
| 2829 | } |
| 2830 | |
| 2831 | int |
| 2832 | peer_default_originate_unset (struct peer *peer, afi_t afi, safi_t safi) |
| 2833 | { |
| 2834 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2835 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2836 | |
| 2837 | /* Adress family must be activated. */ |
| 2838 | if (! peer->afc[afi][safi]) |
| 2839 | return BGP_ERR_PEER_INACTIVE; |
| 2840 | |
| 2841 | /* Default originate can't be used for peer group memeber. */ |
| 2842 | if (peer_is_group_member (peer, afi, safi)) |
| 2843 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 2844 | |
| 2845 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)) |
| 2846 | { |
| 2847 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE); |
| 2848 | |
| 2849 | if (peer->default_rmap[afi][safi].name) |
| 2850 | free (peer->default_rmap[afi][safi].name); |
| 2851 | peer->default_rmap[afi][safi].name = NULL; |
| 2852 | peer->default_rmap[afi][safi].map = NULL; |
| 2853 | } |
| 2854 | |
| 2855 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2856 | { |
| 2857 | if (peer->status == Established && peer->afc_nego[afi][safi]) |
| 2858 | bgp_default_originate (peer, afi, safi, 1); |
| 2859 | return 0; |
| 2860 | } |
| 2861 | |
| 2862 | /* peer-group member updates. */ |
| 2863 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2864 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2865 | { |
| 2866 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE); |
| 2867 | |
| 2868 | if (peer->default_rmap[afi][safi].name) |
| 2869 | free (peer->default_rmap[afi][safi].name); |
| 2870 | peer->default_rmap[afi][safi].name = NULL; |
| 2871 | peer->default_rmap[afi][safi].map = NULL; |
| 2872 | |
| 2873 | if (peer->status == Established && peer->afc_nego[afi][safi]) |
| 2874 | bgp_default_originate (peer, afi, safi, 1); |
| 2875 | } |
| 2876 | return 0; |
| 2877 | } |
| 2878 | |
| 2879 | int |
| 2880 | peer_port_set (struct peer *peer, u_int16_t port) |
| 2881 | { |
| 2882 | peer->port = port; |
| 2883 | return 0; |
| 2884 | } |
| 2885 | |
| 2886 | int |
| 2887 | peer_port_unset (struct peer *peer) |
| 2888 | { |
| 2889 | peer->port = BGP_PORT_DEFAULT; |
| 2890 | return 0; |
| 2891 | } |
| 2892 | |
| 2893 | /* neighbor weight. */ |
| 2894 | int |
| 2895 | peer_weight_set (struct peer *peer, u_int16_t weight) |
| 2896 | { |
| 2897 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2898 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2899 | |
| 2900 | SET_FLAG (peer->config, PEER_CONFIG_WEIGHT); |
| 2901 | peer->weight = weight; |
| 2902 | |
| 2903 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2904 | return 0; |
| 2905 | |
| 2906 | /* peer-group member updates. */ |
| 2907 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2908 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2909 | { |
| 2910 | peer->weight = group->conf->weight; |
| 2911 | } |
| 2912 | return 0; |
| 2913 | } |
| 2914 | |
| 2915 | int |
| 2916 | peer_weight_unset (struct peer *peer) |
| 2917 | { |
| 2918 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2919 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2920 | |
| 2921 | /* Set default weight. */ |
| 2922 | if (peer_group_active (peer)) |
| 2923 | peer->weight = peer->group->conf->weight; |
| 2924 | else |
| 2925 | peer->weight = 0; |
| 2926 | |
| 2927 | UNSET_FLAG (peer->config, PEER_CONFIG_WEIGHT); |
| 2928 | |
| 2929 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2930 | return 0; |
| 2931 | |
| 2932 | /* peer-group member updates. */ |
| 2933 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2934 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2935 | { |
| 2936 | peer->weight = 0; |
| 2937 | } |
| 2938 | return 0; |
| 2939 | } |
| 2940 | |
| 2941 | int |
| 2942 | peer_timers_set (struct peer *peer, u_int32_t keepalive, u_int32_t holdtime) |
| 2943 | { |
| 2944 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2945 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2946 | |
| 2947 | /* Not for peer group memeber. */ |
| 2948 | if (peer_group_active (peer)) |
| 2949 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 2950 | |
| 2951 | /* keepalive value check. */ |
| 2952 | if (keepalive > 65535) |
| 2953 | return BGP_ERR_INVALID_VALUE; |
| 2954 | |
| 2955 | /* Holdtime value check. */ |
| 2956 | if (holdtime > 65535) |
| 2957 | return BGP_ERR_INVALID_VALUE; |
| 2958 | |
| 2959 | /* Holdtime value must be either 0 or greater than 3. */ |
| 2960 | if (holdtime < 3 && holdtime != 0) |
| 2961 | return BGP_ERR_INVALID_VALUE; |
| 2962 | |
| 2963 | /* Set value to the configuration. */ |
| 2964 | SET_FLAG (peer->config, PEER_CONFIG_TIMER); |
| 2965 | peer->holdtime = holdtime; |
| 2966 | peer->keepalive = (keepalive < holdtime / 3 ? keepalive : holdtime / 3); |
| 2967 | |
| 2968 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2969 | return 0; |
| 2970 | |
| 2971 | /* peer-group member updates. */ |
| 2972 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2973 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2974 | { |
| 2975 | SET_FLAG (peer->config, PEER_CONFIG_TIMER); |
| 2976 | peer->holdtime = group->conf->holdtime; |
| 2977 | peer->keepalive = group->conf->keepalive; |
| 2978 | } |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
| 2982 | int |
| 2983 | peer_timers_unset (struct peer *peer) |
| 2984 | { |
| 2985 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2986 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2987 | |
| 2988 | if (peer_group_active (peer)) |
| 2989 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 2990 | |
| 2991 | /* Clear configuration. */ |
| 2992 | UNSET_FLAG (peer->config, PEER_CONFIG_TIMER); |
| 2993 | peer->keepalive = 0; |
| 2994 | peer->holdtime = 0; |
| 2995 | |
| 2996 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 2997 | return 0; |
| 2998 | |
| 2999 | /* peer-group member updates. */ |
| 3000 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3001 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3002 | { |
| 3003 | UNSET_FLAG (peer->config, PEER_CONFIG_TIMER); |
| 3004 | peer->holdtime = 0; |
| 3005 | peer->keepalive = 0; |
| 3006 | } |
| 3007 | |
| 3008 | return 0; |
| 3009 | } |
| 3010 | |
| 3011 | int |
| 3012 | peer_timers_connect_set (struct peer *peer, u_int32_t connect) |
| 3013 | { |
| 3014 | if (peer_group_active (peer)) |
| 3015 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3016 | |
| 3017 | if (connect > 65535) |
| 3018 | return BGP_ERR_INVALID_VALUE; |
| 3019 | |
| 3020 | /* Set value to the configuration. */ |
| 3021 | SET_FLAG (peer->config, PEER_CONFIG_CONNECT); |
| 3022 | peer->connect = connect; |
| 3023 | |
| 3024 | /* Set value to timer setting. */ |
| 3025 | peer->v_connect = connect; |
| 3026 | |
| 3027 | return 0; |
| 3028 | } |
| 3029 | |
| 3030 | int |
| 3031 | peer_timers_connect_unset (struct peer *peer) |
| 3032 | { |
| 3033 | if (peer_group_active (peer)) |
| 3034 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3035 | |
| 3036 | /* Clear configuration. */ |
| 3037 | UNSET_FLAG (peer->config, PEER_CONFIG_CONNECT); |
| 3038 | peer->connect = 0; |
| 3039 | |
| 3040 | /* Set timer setting to default value. */ |
| 3041 | peer->v_connect = BGP_DEFAULT_CONNECT_RETRY; |
| 3042 | |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
| 3046 | int |
| 3047 | peer_advertise_interval_set (struct peer *peer, u_int32_t routeadv) |
| 3048 | { |
| 3049 | if (peer_group_active (peer)) |
| 3050 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3051 | |
| 3052 | if (routeadv > 600) |
| 3053 | return BGP_ERR_INVALID_VALUE; |
| 3054 | |
| 3055 | SET_FLAG (peer->config, PEER_CONFIG_ROUTEADV); |
| 3056 | peer->routeadv = routeadv; |
| 3057 | peer->v_routeadv = routeadv; |
| 3058 | |
| 3059 | return 0; |
| 3060 | } |
| 3061 | |
| 3062 | int |
| 3063 | peer_advertise_interval_unset (struct peer *peer) |
| 3064 | { |
| 3065 | if (peer_group_active (peer)) |
| 3066 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3067 | |
| 3068 | UNSET_FLAG (peer->config, PEER_CONFIG_ROUTEADV); |
| 3069 | peer->routeadv = 0; |
| 3070 | |
| 3071 | if (peer_sort (peer) == BGP_PEER_IBGP) |
| 3072 | peer->v_routeadv = BGP_DEFAULT_IBGP_ROUTEADV; |
| 3073 | else |
| 3074 | peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; |
| 3075 | |
| 3076 | return 0; |
| 3077 | } |
| 3078 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3079 | /* neighbor interface */ |
| 3080 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 3081 | peer_interface_set (struct peer *peer, const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3082 | { |
| 3083 | if (peer->ifname) |
| 3084 | free (peer->ifname); |
| 3085 | peer->ifname = strdup (str); |
| 3086 | |
| 3087 | return 0; |
| 3088 | } |
| 3089 | |
| 3090 | int |
| 3091 | peer_interface_unset (struct peer *peer) |
| 3092 | { |
| 3093 | if (peer->ifname) |
| 3094 | free (peer->ifname); |
| 3095 | peer->ifname = NULL; |
| 3096 | |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
| 3100 | /* Allow-as in. */ |
| 3101 | int |
| 3102 | peer_allowas_in_set (struct peer *peer, afi_t afi, safi_t safi, int allow_num) |
| 3103 | { |
| 3104 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3105 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3106 | |
| 3107 | if (allow_num < 1 || allow_num > 10) |
| 3108 | return BGP_ERR_INVALID_VALUE; |
| 3109 | |
| 3110 | if (peer->allowas_in[afi][safi] != allow_num) |
| 3111 | { |
| 3112 | peer->allowas_in[afi][safi] = allow_num; |
| 3113 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN); |
| 3114 | peer_change_action (peer, afi, safi, peer_change_reset_in); |
| 3115 | } |
| 3116 | |
| 3117 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3118 | return 0; |
| 3119 | |
| 3120 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3121 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3122 | { |
| 3123 | if (peer->allowas_in[afi][safi] != allow_num) |
| 3124 | { |
| 3125 | peer->allowas_in[afi][safi] = allow_num; |
| 3126 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN); |
| 3127 | peer_change_action (peer, afi, safi, peer_change_reset_in); |
| 3128 | } |
| 3129 | |
| 3130 | } |
| 3131 | return 0; |
| 3132 | } |
| 3133 | |
| 3134 | int |
| 3135 | peer_allowas_in_unset (struct peer *peer, afi_t afi, safi_t safi) |
| 3136 | { |
| 3137 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3138 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3139 | |
| 3140 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN)) |
| 3141 | { |
| 3142 | peer->allowas_in[afi][safi] = 0; |
| 3143 | peer_af_flag_unset (peer, afi, safi, PEER_FLAG_ALLOWAS_IN); |
| 3144 | } |
| 3145 | |
| 3146 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3147 | return 0; |
| 3148 | |
| 3149 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3150 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3151 | { |
| 3152 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ALLOWAS_IN)) |
| 3153 | { |
| 3154 | peer->allowas_in[afi][safi] = 0; |
| 3155 | peer_af_flag_unset (peer, afi, safi, PEER_FLAG_ALLOWAS_IN); |
| 3156 | } |
| 3157 | } |
| 3158 | return 0; |
| 3159 | } |
| 3160 | |
| 3161 | int |
| 3162 | peer_local_as_set (struct peer *peer, as_t as, int no_prepend) |
| 3163 | { |
| 3164 | struct bgp *bgp = peer->bgp; |
| 3165 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3166 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3167 | |
| 3168 | if (peer_sort (peer) != BGP_PEER_EBGP |
| 3169 | && peer_sort (peer) != BGP_PEER_INTERNAL) |
| 3170 | return BGP_ERR_LOCAL_AS_ALLOWED_ONLY_FOR_EBGP; |
| 3171 | |
| 3172 | if (bgp->as == as) |
| 3173 | return BGP_ERR_CANNOT_HAVE_LOCAL_AS_SAME_AS; |
| 3174 | |
| 3175 | if (peer_group_active (peer)) |
| 3176 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3177 | |
| 3178 | if (peer->change_local_as == as && |
| 3179 | ((CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND) && no_prepend) |
| 3180 | || (! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND) && ! no_prepend))) |
| 3181 | return 0; |
| 3182 | |
| 3183 | peer->change_local_as = as; |
| 3184 | if (no_prepend) |
| 3185 | SET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 3186 | else |
| 3187 | UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 3188 | |
| 3189 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3190 | { |
| 3191 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 3192 | { |
| 3193 | peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE; |
| 3194 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 3195 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 3196 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3197 | else |
| 3198 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 3199 | |
| 3200 | return 0; |
| 3201 | } |
| 3202 | |
| 3203 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3204 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3205 | { |
| 3206 | peer->change_local_as = as; |
| 3207 | if (no_prepend) |
| 3208 | SET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 3209 | else |
| 3210 | UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 3211 | |
| 3212 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 3213 | { |
| 3214 | peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE; |
| 3215 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 3216 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 3217 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3218 | else |
| 3219 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 3220 | } |
| 3221 | |
| 3222 | return 0; |
| 3223 | } |
| 3224 | |
| 3225 | int |
| 3226 | peer_local_as_unset (struct peer *peer) |
| 3227 | { |
| 3228 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3229 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3230 | |
| 3231 | if (peer_group_active (peer)) |
| 3232 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3233 | |
| 3234 | if (! peer->change_local_as) |
| 3235 | return 0; |
| 3236 | |
| 3237 | peer->change_local_as = 0; |
| 3238 | UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 3239 | |
| 3240 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3241 | { |
| 3242 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 3243 | { |
| 3244 | peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE; |
| 3245 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 3246 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 3247 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3248 | else |
| 3249 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 3250 | |
| 3251 | return 0; |
| 3252 | } |
| 3253 | |
| 3254 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3255 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3256 | { |
| 3257 | peer->change_local_as = 0; |
| 3258 | UNSET_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND); |
| 3259 | |
| 3260 | if (peer->status == Established) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 3261 | { |
| 3262 | peer->last_reset = PEER_DOWN_LOCAL_AS_CHANGE; |
| 3263 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 3264 | BGP_NOTIFY_CEASE_CONFIG_CHANGE); |
| 3265 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3266 | else |
| 3267 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 3268 | } |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | /* Set distribute list to the peer. */ |
| 3273 | int |
| 3274 | peer_distribute_set (struct peer *peer, afi_t afi, safi_t safi, int direct, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 3275 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3276 | { |
| 3277 | struct bgp_filter *filter; |
| 3278 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3279 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3280 | |
| 3281 | if (! peer->afc[afi][safi]) |
| 3282 | return BGP_ERR_PEER_INACTIVE; |
| 3283 | |
| 3284 | if (direct != FILTER_IN && direct != FILTER_OUT) |
| 3285 | return BGP_ERR_INVALID_VALUE; |
| 3286 | |
| 3287 | if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi)) |
| 3288 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3289 | |
| 3290 | filter = &peer->filter[afi][safi]; |
| 3291 | |
| 3292 | if (filter->plist[direct].name) |
| 3293 | return BGP_ERR_PEER_FILTER_CONFLICT; |
| 3294 | |
| 3295 | if (filter->dlist[direct].name) |
| 3296 | free (filter->dlist[direct].name); |
| 3297 | filter->dlist[direct].name = strdup (name); |
| 3298 | filter->dlist[direct].alist = access_list_lookup (afi, name); |
| 3299 | |
| 3300 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3301 | return 0; |
| 3302 | |
| 3303 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3304 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3305 | { |
| 3306 | filter = &peer->filter[afi][safi]; |
| 3307 | |
| 3308 | if (! peer->af_group[afi][safi]) |
| 3309 | continue; |
| 3310 | |
| 3311 | if (filter->dlist[direct].name) |
| 3312 | free (filter->dlist[direct].name); |
| 3313 | filter->dlist[direct].name = strdup (name); |
| 3314 | filter->dlist[direct].alist = access_list_lookup (afi, name); |
| 3315 | } |
| 3316 | |
| 3317 | return 0; |
| 3318 | } |
| 3319 | |
| 3320 | int |
| 3321 | peer_distribute_unset (struct peer *peer, afi_t afi, safi_t safi, int direct) |
| 3322 | { |
| 3323 | struct bgp_filter *filter; |
| 3324 | struct bgp_filter *gfilter; |
| 3325 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3326 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3327 | |
| 3328 | if (! peer->afc[afi][safi]) |
| 3329 | return BGP_ERR_PEER_INACTIVE; |
| 3330 | |
| 3331 | if (direct != FILTER_IN && direct != FILTER_OUT) |
| 3332 | return BGP_ERR_INVALID_VALUE; |
| 3333 | |
| 3334 | if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi)) |
| 3335 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3336 | |
| 3337 | filter = &peer->filter[afi][safi]; |
| 3338 | |
| 3339 | /* apply peer-group filter */ |
| 3340 | if (peer->af_group[afi][safi]) |
| 3341 | { |
| 3342 | gfilter = &peer->group->conf->filter[afi][safi]; |
| 3343 | |
| 3344 | if (gfilter->dlist[direct].name) |
| 3345 | { |
| 3346 | if (filter->dlist[direct].name) |
| 3347 | free (filter->dlist[direct].name); |
| 3348 | filter->dlist[direct].name = strdup (gfilter->dlist[direct].name); |
| 3349 | filter->dlist[direct].alist = gfilter->dlist[direct].alist; |
| 3350 | return 0; |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | if (filter->dlist[direct].name) |
| 3355 | free (filter->dlist[direct].name); |
| 3356 | filter->dlist[direct].name = NULL; |
| 3357 | filter->dlist[direct].alist = NULL; |
| 3358 | |
| 3359 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3360 | return 0; |
| 3361 | |
| 3362 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3363 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3364 | { |
| 3365 | filter = &peer->filter[afi][safi]; |
| 3366 | |
| 3367 | if (! peer->af_group[afi][safi]) |
| 3368 | continue; |
| 3369 | |
| 3370 | if (filter->dlist[direct].name) |
| 3371 | free (filter->dlist[direct].name); |
| 3372 | filter->dlist[direct].name = NULL; |
| 3373 | filter->dlist[direct].alist = NULL; |
| 3374 | } |
| 3375 | |
| 3376 | return 0; |
| 3377 | } |
| 3378 | |
| 3379 | /* Update distribute list. */ |
| 3380 | void |
| 3381 | peer_distribute_update (struct access_list *access) |
| 3382 | { |
| 3383 | afi_t afi; |
| 3384 | safi_t safi; |
| 3385 | int direct; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3386 | struct listnode *mnode, *mnnode; |
| 3387 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3388 | struct bgp *bgp; |
| 3389 | struct peer *peer; |
| 3390 | struct peer_group *group; |
| 3391 | struct bgp_filter *filter; |
| 3392 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3393 | for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3394 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3395 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3396 | { |
| 3397 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 3398 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 3399 | { |
| 3400 | filter = &peer->filter[afi][safi]; |
| 3401 | |
| 3402 | for (direct = FILTER_IN; direct < FILTER_MAX; direct++) |
| 3403 | { |
| 3404 | if (filter->dlist[direct].name) |
| 3405 | filter->dlist[direct].alist = |
| 3406 | access_list_lookup (afi, filter->dlist[direct].name); |
| 3407 | else |
| 3408 | filter->dlist[direct].alist = NULL; |
| 3409 | } |
| 3410 | } |
| 3411 | } |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3412 | for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3413 | { |
| 3414 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 3415 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 3416 | { |
| 3417 | filter = &group->conf->filter[afi][safi]; |
| 3418 | |
| 3419 | for (direct = FILTER_IN; direct < FILTER_MAX; direct++) |
| 3420 | { |
| 3421 | if (filter->dlist[direct].name) |
| 3422 | filter->dlist[direct].alist = |
| 3423 | access_list_lookup (afi, filter->dlist[direct].name); |
| 3424 | else |
| 3425 | filter->dlist[direct].alist = NULL; |
| 3426 | } |
| 3427 | } |
| 3428 | } |
| 3429 | } |
| 3430 | } |
| 3431 | |
| 3432 | /* Set prefix list to the peer. */ |
| 3433 | int |
| 3434 | peer_prefix_list_set (struct peer *peer, afi_t afi, safi_t safi, int direct, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 3435 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3436 | { |
| 3437 | struct bgp_filter *filter; |
| 3438 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3439 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3440 | |
| 3441 | if (! peer->afc[afi][safi]) |
| 3442 | return BGP_ERR_PEER_INACTIVE; |
| 3443 | |
| 3444 | if (direct != FILTER_IN && direct != FILTER_OUT) |
| 3445 | return BGP_ERR_INVALID_VALUE; |
| 3446 | |
| 3447 | if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi)) |
| 3448 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3449 | |
| 3450 | filter = &peer->filter[afi][safi]; |
| 3451 | |
| 3452 | if (filter->dlist[direct].name) |
| 3453 | return BGP_ERR_PEER_FILTER_CONFLICT; |
| 3454 | |
| 3455 | if (filter->plist[direct].name) |
| 3456 | free (filter->plist[direct].name); |
| 3457 | filter->plist[direct].name = strdup (name); |
| 3458 | filter->plist[direct].plist = prefix_list_lookup (afi, name); |
| 3459 | |
| 3460 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3461 | return 0; |
| 3462 | |
| 3463 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3464 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3465 | { |
| 3466 | filter = &peer->filter[afi][safi]; |
| 3467 | |
| 3468 | if (! peer->af_group[afi][safi]) |
| 3469 | continue; |
| 3470 | |
| 3471 | if (filter->plist[direct].name) |
| 3472 | free (filter->plist[direct].name); |
| 3473 | filter->plist[direct].name = strdup (name); |
| 3474 | filter->plist[direct].plist = prefix_list_lookup (afi, name); |
| 3475 | } |
| 3476 | return 0; |
| 3477 | } |
| 3478 | |
| 3479 | int |
| 3480 | peer_prefix_list_unset (struct peer *peer, afi_t afi, safi_t safi, int direct) |
| 3481 | { |
| 3482 | struct bgp_filter *filter; |
| 3483 | struct bgp_filter *gfilter; |
| 3484 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3485 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3486 | |
| 3487 | if (! peer->afc[afi][safi]) |
| 3488 | return BGP_ERR_PEER_INACTIVE; |
| 3489 | |
| 3490 | if (direct != FILTER_IN && direct != FILTER_OUT) |
| 3491 | return BGP_ERR_INVALID_VALUE; |
| 3492 | |
| 3493 | if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi)) |
| 3494 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3495 | |
| 3496 | filter = &peer->filter[afi][safi]; |
| 3497 | |
| 3498 | /* apply peer-group filter */ |
| 3499 | if (peer->af_group[afi][safi]) |
| 3500 | { |
| 3501 | gfilter = &peer->group->conf->filter[afi][safi]; |
| 3502 | |
| 3503 | if (gfilter->plist[direct].name) |
| 3504 | { |
| 3505 | if (filter->plist[direct].name) |
| 3506 | free (filter->plist[direct].name); |
| 3507 | filter->plist[direct].name = strdup (gfilter->plist[direct].name); |
| 3508 | filter->plist[direct].plist = gfilter->plist[direct].plist; |
| 3509 | return 0; |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | if (filter->plist[direct].name) |
| 3514 | free (filter->plist[direct].name); |
| 3515 | filter->plist[direct].name = NULL; |
| 3516 | filter->plist[direct].plist = NULL; |
| 3517 | |
| 3518 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3519 | return 0; |
| 3520 | |
| 3521 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3522 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3523 | { |
| 3524 | filter = &peer->filter[afi][safi]; |
| 3525 | |
| 3526 | if (! peer->af_group[afi][safi]) |
| 3527 | continue; |
| 3528 | |
| 3529 | if (filter->plist[direct].name) |
| 3530 | free (filter->plist[direct].name); |
| 3531 | filter->plist[direct].name = NULL; |
| 3532 | filter->plist[direct].plist = NULL; |
| 3533 | } |
| 3534 | |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
| 3538 | /* Update prefix-list list. */ |
| 3539 | void |
| 3540 | peer_prefix_list_update (struct prefix_list *plist) |
| 3541 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3542 | struct listnode *mnode, *mnnode; |
| 3543 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3544 | struct bgp *bgp; |
| 3545 | struct peer *peer; |
| 3546 | struct peer_group *group; |
| 3547 | struct bgp_filter *filter; |
| 3548 | afi_t afi; |
| 3549 | safi_t safi; |
| 3550 | int direct; |
| 3551 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3552 | for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3553 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3554 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3555 | { |
| 3556 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 3557 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 3558 | { |
| 3559 | filter = &peer->filter[afi][safi]; |
| 3560 | |
| 3561 | for (direct = FILTER_IN; direct < FILTER_MAX; direct++) |
| 3562 | { |
| 3563 | if (filter->plist[direct].name) |
| 3564 | filter->plist[direct].plist = |
| 3565 | prefix_list_lookup (afi, filter->plist[direct].name); |
| 3566 | else |
| 3567 | filter->plist[direct].plist = NULL; |
| 3568 | } |
| 3569 | } |
| 3570 | } |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3571 | for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3572 | { |
| 3573 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 3574 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 3575 | { |
| 3576 | filter = &group->conf->filter[afi][safi]; |
| 3577 | |
| 3578 | for (direct = FILTER_IN; direct < FILTER_MAX; direct++) |
| 3579 | { |
| 3580 | if (filter->plist[direct].name) |
| 3581 | filter->plist[direct].plist = |
| 3582 | prefix_list_lookup (afi, filter->plist[direct].name); |
| 3583 | else |
| 3584 | filter->plist[direct].plist = NULL; |
| 3585 | } |
| 3586 | } |
| 3587 | } |
| 3588 | } |
| 3589 | } |
| 3590 | |
| 3591 | int |
| 3592 | peer_aslist_set (struct peer *peer, afi_t afi, safi_t safi, int direct, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 3593 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3594 | { |
| 3595 | struct bgp_filter *filter; |
| 3596 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3597 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3598 | |
| 3599 | if (! peer->afc[afi][safi]) |
| 3600 | return BGP_ERR_PEER_INACTIVE; |
| 3601 | |
| 3602 | if (direct != FILTER_IN && direct != FILTER_OUT) |
| 3603 | return BGP_ERR_INVALID_VALUE; |
| 3604 | |
| 3605 | if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi)) |
| 3606 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3607 | |
| 3608 | filter = &peer->filter[afi][safi]; |
| 3609 | |
| 3610 | if (filter->aslist[direct].name) |
| 3611 | free (filter->aslist[direct].name); |
| 3612 | filter->aslist[direct].name = strdup (name); |
| 3613 | filter->aslist[direct].aslist = as_list_lookup (name); |
| 3614 | |
| 3615 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3616 | return 0; |
| 3617 | |
| 3618 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3619 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3620 | { |
| 3621 | filter = &peer->filter[afi][safi]; |
| 3622 | |
| 3623 | if (! peer->af_group[afi][safi]) |
| 3624 | continue; |
| 3625 | |
| 3626 | if (filter->aslist[direct].name) |
| 3627 | free (filter->aslist[direct].name); |
| 3628 | filter->aslist[direct].name = strdup (name); |
| 3629 | filter->aslist[direct].aslist = as_list_lookup (name); |
| 3630 | } |
| 3631 | return 0; |
| 3632 | } |
| 3633 | |
| 3634 | int |
| 3635 | peer_aslist_unset (struct peer *peer,afi_t afi, safi_t safi, int direct) |
| 3636 | { |
| 3637 | struct bgp_filter *filter; |
| 3638 | struct bgp_filter *gfilter; |
| 3639 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3640 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3641 | |
| 3642 | if (! peer->afc[afi][safi]) |
| 3643 | return BGP_ERR_PEER_INACTIVE; |
| 3644 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 3645 | if (direct != RMAP_IN && direct != RMAP_OUT && |
| 3646 | direct != RMAP_IMPORT && direct != RMAP_EXPORT) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3647 | return BGP_ERR_INVALID_VALUE; |
| 3648 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 3649 | if ( (direct == RMAP_OUT || direct == RMAP_IMPORT) |
| 3650 | && peer_is_group_member (peer, afi, safi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3651 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3652 | |
| 3653 | filter = &peer->filter[afi][safi]; |
| 3654 | |
| 3655 | /* apply peer-group filter */ |
| 3656 | if (peer->af_group[afi][safi]) |
| 3657 | { |
| 3658 | gfilter = &peer->group->conf->filter[afi][safi]; |
| 3659 | |
| 3660 | if (gfilter->aslist[direct].name) |
| 3661 | { |
| 3662 | if (filter->aslist[direct].name) |
| 3663 | free (filter->aslist[direct].name); |
| 3664 | filter->aslist[direct].name = strdup (gfilter->aslist[direct].name); |
| 3665 | filter->aslist[direct].aslist = gfilter->aslist[direct].aslist; |
| 3666 | return 0; |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | if (filter->aslist[direct].name) |
| 3671 | free (filter->aslist[direct].name); |
| 3672 | filter->aslist[direct].name = NULL; |
| 3673 | filter->aslist[direct].aslist = NULL; |
| 3674 | |
| 3675 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3676 | return 0; |
| 3677 | |
| 3678 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3679 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3680 | { |
| 3681 | filter = &peer->filter[afi][safi]; |
| 3682 | |
| 3683 | if (! peer->af_group[afi][safi]) |
| 3684 | continue; |
| 3685 | |
| 3686 | if (filter->aslist[direct].name) |
| 3687 | free (filter->aslist[direct].name); |
| 3688 | filter->aslist[direct].name = NULL; |
| 3689 | filter->aslist[direct].aslist = NULL; |
| 3690 | } |
| 3691 | |
| 3692 | return 0; |
| 3693 | } |
| 3694 | |
| 3695 | void |
| 3696 | peer_aslist_update () |
| 3697 | { |
| 3698 | afi_t afi; |
| 3699 | safi_t safi; |
| 3700 | int direct; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3701 | struct listnode *mnode, *mnnode; |
| 3702 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3703 | struct bgp *bgp; |
| 3704 | struct peer *peer; |
| 3705 | struct peer_group *group; |
| 3706 | struct bgp_filter *filter; |
| 3707 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3708 | for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3709 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3710 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3711 | { |
| 3712 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 3713 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 3714 | { |
| 3715 | filter = &peer->filter[afi][safi]; |
| 3716 | |
| 3717 | for (direct = FILTER_IN; direct < FILTER_MAX; direct++) |
| 3718 | { |
| 3719 | if (filter->aslist[direct].name) |
| 3720 | filter->aslist[direct].aslist = |
| 3721 | as_list_lookup (filter->aslist[direct].name); |
| 3722 | else |
| 3723 | filter->aslist[direct].aslist = NULL; |
| 3724 | } |
| 3725 | } |
| 3726 | } |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3727 | for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3728 | { |
| 3729 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 3730 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 3731 | { |
| 3732 | filter = &group->conf->filter[afi][safi]; |
| 3733 | |
| 3734 | for (direct = FILTER_IN; direct < FILTER_MAX; direct++) |
| 3735 | { |
| 3736 | if (filter->aslist[direct].name) |
| 3737 | filter->aslist[direct].aslist = |
| 3738 | as_list_lookup (filter->aslist[direct].name); |
| 3739 | else |
| 3740 | filter->aslist[direct].aslist = NULL; |
| 3741 | } |
| 3742 | } |
| 3743 | } |
| 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | /* Set route-map to the peer. */ |
| 3748 | int |
| 3749 | peer_route_map_set (struct peer *peer, afi_t afi, safi_t safi, int direct, |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 3750 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3751 | { |
| 3752 | struct bgp_filter *filter; |
| 3753 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3754 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3755 | |
| 3756 | if (! peer->afc[afi][safi]) |
| 3757 | return BGP_ERR_PEER_INACTIVE; |
| 3758 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 3759 | if (direct != RMAP_IN && direct != RMAP_OUT && |
| 3760 | direct != RMAP_IMPORT && direct != RMAP_EXPORT) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3761 | return BGP_ERR_INVALID_VALUE; |
| 3762 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 3763 | if ( (direct == RMAP_OUT || direct == RMAP_IMPORT) |
| 3764 | && peer_is_group_member (peer, afi, safi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3765 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3766 | |
| 3767 | filter = &peer->filter[afi][safi]; |
| 3768 | |
| 3769 | if (filter->map[direct].name) |
| 3770 | free (filter->map[direct].name); |
| 3771 | |
| 3772 | filter->map[direct].name = strdup (name); |
| 3773 | filter->map[direct].map = route_map_lookup_by_name (name); |
| 3774 | |
| 3775 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3776 | return 0; |
| 3777 | |
| 3778 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3779 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3780 | { |
| 3781 | filter = &peer->filter[afi][safi]; |
| 3782 | |
| 3783 | if (! peer->af_group[afi][safi]) |
| 3784 | continue; |
| 3785 | |
| 3786 | if (filter->map[direct].name) |
| 3787 | free (filter->map[direct].name); |
| 3788 | filter->map[direct].name = strdup (name); |
| 3789 | filter->map[direct].map = route_map_lookup_by_name (name); |
| 3790 | } |
| 3791 | return 0; |
| 3792 | } |
| 3793 | |
| 3794 | /* Unset route-map from the peer. */ |
| 3795 | int |
| 3796 | peer_route_map_unset (struct peer *peer, afi_t afi, safi_t safi, int direct) |
| 3797 | { |
| 3798 | struct bgp_filter *filter; |
| 3799 | struct bgp_filter *gfilter; |
| 3800 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3801 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3802 | |
| 3803 | if (! peer->afc[afi][safi]) |
| 3804 | return BGP_ERR_PEER_INACTIVE; |
| 3805 | |
| 3806 | if (direct != FILTER_IN && direct != FILTER_OUT) |
| 3807 | return BGP_ERR_INVALID_VALUE; |
| 3808 | |
| 3809 | if (direct == FILTER_OUT && peer_is_group_member (peer, afi, safi)) |
| 3810 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3811 | |
| 3812 | filter = &peer->filter[afi][safi]; |
| 3813 | |
| 3814 | /* apply peer-group filter */ |
| 3815 | if (peer->af_group[afi][safi]) |
| 3816 | { |
| 3817 | gfilter = &peer->group->conf->filter[afi][safi]; |
| 3818 | |
| 3819 | if (gfilter->map[direct].name) |
| 3820 | { |
| 3821 | if (filter->map[direct].name) |
| 3822 | free (filter->map[direct].name); |
| 3823 | filter->map[direct].name = strdup (gfilter->map[direct].name); |
| 3824 | filter->map[direct].map = gfilter->map[direct].map; |
| 3825 | return 0; |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | if (filter->map[direct].name) |
| 3830 | free (filter->map[direct].name); |
| 3831 | filter->map[direct].name = NULL; |
| 3832 | filter->map[direct].map = NULL; |
| 3833 | |
| 3834 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3835 | return 0; |
| 3836 | |
| 3837 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3838 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3839 | { |
| 3840 | filter = &peer->filter[afi][safi]; |
| 3841 | |
| 3842 | if (! peer->af_group[afi][safi]) |
| 3843 | continue; |
| 3844 | |
| 3845 | if (filter->map[direct].name) |
| 3846 | free (filter->map[direct].name); |
| 3847 | filter->map[direct].name = NULL; |
| 3848 | filter->map[direct].map = NULL; |
| 3849 | } |
| 3850 | return 0; |
| 3851 | } |
| 3852 | |
| 3853 | /* Set unsuppress-map to the peer. */ |
| 3854 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 3855 | peer_unsuppress_map_set (struct peer *peer, afi_t afi, safi_t safi, |
| 3856 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3857 | { |
| 3858 | struct bgp_filter *filter; |
| 3859 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3860 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3861 | |
| 3862 | if (! peer->afc[afi][safi]) |
| 3863 | return BGP_ERR_PEER_INACTIVE; |
| 3864 | |
| 3865 | if (peer_is_group_member (peer, afi, safi)) |
| 3866 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3867 | |
| 3868 | filter = &peer->filter[afi][safi]; |
| 3869 | |
| 3870 | if (filter->usmap.name) |
| 3871 | free (filter->usmap.name); |
| 3872 | |
| 3873 | filter->usmap.name = strdup (name); |
| 3874 | filter->usmap.map = route_map_lookup_by_name (name); |
| 3875 | |
| 3876 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3877 | return 0; |
| 3878 | |
| 3879 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3880 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3881 | { |
| 3882 | filter = &peer->filter[afi][safi]; |
| 3883 | |
| 3884 | if (! peer->af_group[afi][safi]) |
| 3885 | continue; |
| 3886 | |
| 3887 | if (filter->usmap.name) |
| 3888 | free (filter->usmap.name); |
| 3889 | filter->usmap.name = strdup (name); |
| 3890 | filter->usmap.map = route_map_lookup_by_name (name); |
| 3891 | } |
| 3892 | return 0; |
| 3893 | } |
| 3894 | |
| 3895 | /* Unset route-map from the peer. */ |
| 3896 | int |
| 3897 | peer_unsuppress_map_unset (struct peer *peer, afi_t afi, safi_t safi) |
| 3898 | { |
| 3899 | struct bgp_filter *filter; |
| 3900 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3901 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3902 | |
| 3903 | if (! peer->afc[afi][safi]) |
| 3904 | return BGP_ERR_PEER_INACTIVE; |
| 3905 | |
| 3906 | if (peer_is_group_member (peer, afi, safi)) |
| 3907 | return BGP_ERR_INVALID_FOR_PEER_GROUP_MEMBER; |
| 3908 | |
| 3909 | filter = &peer->filter[afi][safi]; |
| 3910 | |
| 3911 | if (filter->usmap.name) |
| 3912 | free (filter->usmap.name); |
| 3913 | filter->usmap.name = NULL; |
| 3914 | filter->usmap.map = NULL; |
| 3915 | |
| 3916 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3917 | return 0; |
| 3918 | |
| 3919 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3920 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3921 | { |
| 3922 | filter = &peer->filter[afi][safi]; |
| 3923 | |
| 3924 | if (! peer->af_group[afi][safi]) |
| 3925 | continue; |
| 3926 | |
| 3927 | if (filter->usmap.name) |
| 3928 | free (filter->usmap.name); |
| 3929 | filter->usmap.name = NULL; |
| 3930 | filter->usmap.map = NULL; |
| 3931 | } |
| 3932 | return 0; |
| 3933 | } |
| 3934 | |
| 3935 | int |
| 3936 | peer_maximum_prefix_set (struct peer *peer, afi_t afi, safi_t safi, |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 3937 | u_int32_t max, u_char threshold, |
| 3938 | int warning, u_int16_t restart) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3939 | { |
| 3940 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3941 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3942 | |
| 3943 | if (! peer->afc[afi][safi]) |
| 3944 | return BGP_ERR_PEER_INACTIVE; |
| 3945 | |
| 3946 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX); |
| 3947 | peer->pmax[afi][safi] = max; |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 3948 | peer->pmax_threshold[afi][safi] = threshold; |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 3949 | peer->pmax_restart[afi][safi] = restart; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3950 | if (warning) |
| 3951 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 3952 | else |
| 3953 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 3954 | |
| 3955 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 3956 | return 0; |
| 3957 | |
| 3958 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3959 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3960 | { |
| 3961 | if (! peer->af_group[afi][safi]) |
| 3962 | continue; |
| 3963 | |
| 3964 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX); |
| 3965 | peer->pmax[afi][safi] = max; |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 3966 | peer->pmax_threshold[afi][safi] = threshold; |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 3967 | peer->pmax_restart[afi][safi] = restart; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3968 | if (warning) |
| 3969 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 3970 | else |
| 3971 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 3972 | } |
| 3973 | return 0; |
| 3974 | } |
| 3975 | |
| 3976 | int |
| 3977 | peer_maximum_prefix_unset (struct peer *peer, afi_t afi, safi_t safi) |
| 3978 | { |
| 3979 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3980 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3981 | |
| 3982 | if (! peer->afc[afi][safi]) |
| 3983 | return BGP_ERR_PEER_INACTIVE; |
| 3984 | |
| 3985 | /* apply peer-group config */ |
| 3986 | if (peer->af_group[afi][safi]) |
| 3987 | { |
| 3988 | if (CHECK_FLAG (peer->group->conf->af_flags[afi][safi], |
| 3989 | PEER_FLAG_MAX_PREFIX)) |
| 3990 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX); |
| 3991 | else |
| 3992 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX); |
| 3993 | |
| 3994 | if (CHECK_FLAG (peer->group->conf->af_flags[afi][safi], |
| 3995 | PEER_FLAG_MAX_PREFIX_WARNING)) |
| 3996 | SET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 3997 | else |
| 3998 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 3999 | |
| 4000 | peer->pmax[afi][safi] = peer->group->conf->pmax[afi][safi]; |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4001 | peer->pmax_threshold[afi][safi] = peer->group->conf->pmax_threshold[afi][safi]; |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 4002 | peer->pmax_restart[afi][safi] = peer->group->conf->pmax_restart[afi][safi]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4003 | return 0; |
| 4004 | } |
| 4005 | |
| 4006 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX); |
| 4007 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 4008 | peer->pmax[afi][safi] = 0; |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 4009 | peer->pmax_threshold[afi][safi] = 0; |
| 4010 | peer->pmax_restart[afi][safi] = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4011 | |
| 4012 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 4013 | return 0; |
| 4014 | |
| 4015 | group = peer->group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4016 | for (ALL_LIST_ELEMENTS (group->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4017 | { |
| 4018 | if (! peer->af_group[afi][safi]) |
| 4019 | continue; |
| 4020 | |
| 4021 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX); |
| 4022 | UNSET_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING); |
| 4023 | peer->pmax[afi][safi] = 0; |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 4024 | peer->pmax_threshold[afi][safi] = 0; |
| 4025 | peer->pmax_restart[afi][safi] = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4026 | } |
| 4027 | return 0; |
| 4028 | } |
| 4029 | |
| 4030 | int |
| 4031 | peer_clear (struct peer *peer) |
| 4032 | { |
| 4033 | if (! CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN)) |
| 4034 | { |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 4035 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW)) |
| 4036 | { |
| 4037 | UNSET_FLAG (peer->sflags, PEER_STATUS_PREFIX_OVERFLOW); |
| 4038 | if (peer->t_pmax_restart) |
| 4039 | { |
| 4040 | BGP_TIMER_OFF (peer->t_pmax_restart); |
| 4041 | if (BGP_DEBUG (events, EVENTS)) |
| 4042 | zlog_debug ("%s Maximum-prefix restart timer canceled", |
| 4043 | peer->host); |
| 4044 | } |
| 4045 | BGP_EVENT_ADD (peer, BGP_Start); |
| 4046 | return 0; |
| 4047 | } |
| 4048 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4049 | peer->v_start = BGP_INIT_START_TIMER; |
| 4050 | if (peer->status == Established) |
| 4051 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 4052 | BGP_NOTIFY_CEASE_ADMIN_RESET); |
| 4053 | else |
| 4054 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 4055 | } |
| 4056 | return 0; |
| 4057 | } |
| 4058 | |
| 4059 | int |
| 4060 | peer_clear_soft (struct peer *peer, afi_t afi, safi_t safi, |
| 4061 | enum bgp_clear_type stype) |
| 4062 | { |
| 4063 | if (peer->status != Established) |
| 4064 | return 0; |
| 4065 | |
| 4066 | if (! peer->afc[afi][safi]) |
| 4067 | return BGP_ERR_AF_UNCONFIGURED; |
| 4068 | |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 4069 | if (stype == BGP_CLEAR_SOFT_RSCLIENT) |
| 4070 | { |
| 4071 | if (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)) |
| 4072 | return 0; |
| 4073 | bgp_check_local_routes_rsclient (peer, afi, safi); |
| 4074 | bgp_soft_reconfig_rsclient (peer, afi, safi); |
| 4075 | } |
| 4076 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4077 | if (stype == BGP_CLEAR_SOFT_OUT || stype == BGP_CLEAR_SOFT_BOTH) |
| 4078 | bgp_announce_route (peer, afi, safi); |
| 4079 | |
| 4080 | if (stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) |
| 4081 | { |
| 4082 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV) |
| 4083 | && (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV) |
| 4084 | || CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV))) |
| 4085 | { |
| 4086 | struct bgp_filter *filter = &peer->filter[afi][safi]; |
| 4087 | u_char prefix_type; |
| 4088 | |
| 4089 | if (CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV)) |
| 4090 | prefix_type = ORF_TYPE_PREFIX; |
| 4091 | else |
| 4092 | prefix_type = ORF_TYPE_PREFIX_OLD; |
| 4093 | |
| 4094 | if (filter->plist[FILTER_IN].plist) |
| 4095 | { |
| 4096 | if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND)) |
| 4097 | bgp_route_refresh_send (peer, afi, safi, |
| 4098 | prefix_type, REFRESH_DEFER, 1); |
| 4099 | bgp_route_refresh_send (peer, afi, safi, prefix_type, |
| 4100 | REFRESH_IMMEDIATE, 0); |
| 4101 | } |
| 4102 | else |
| 4103 | { |
| 4104 | if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND)) |
| 4105 | bgp_route_refresh_send (peer, afi, safi, |
| 4106 | prefix_type, REFRESH_IMMEDIATE, 1); |
| 4107 | else |
| 4108 | bgp_route_refresh_send (peer, afi, safi, 0, 0, 0); |
| 4109 | } |
| 4110 | return 0; |
| 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | if (stype == BGP_CLEAR_SOFT_IN || stype == BGP_CLEAR_SOFT_BOTH |
| 4115 | || stype == BGP_CLEAR_SOFT_IN_ORF_PREFIX) |
| 4116 | { |
| 4117 | /* If neighbor has soft reconfiguration inbound flag. |
| 4118 | Use Adj-RIB-In database. */ |
| 4119 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)) |
| 4120 | bgp_soft_reconfig_in (peer, afi, safi); |
| 4121 | else |
| 4122 | { |
| 4123 | /* If neighbor has route refresh capability, send route refresh |
| 4124 | message to the peer. */ |
| 4125 | if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV) |
| 4126 | || CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV)) |
| 4127 | bgp_route_refresh_send (peer, afi, safi, 0, 0, 0); |
| 4128 | else |
| 4129 | return BGP_ERR_SOFT_RECONFIG_UNCONFIGURED; |
| 4130 | } |
| 4131 | } |
| 4132 | return 0; |
| 4133 | } |
| 4134 | |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 4135 | /* Display peer uptime.*/ |
| 4136 | /* XXX: why does this function return char * when it takes buffer? */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4137 | char * |
| 4138 | peer_uptime (time_t uptime2, char *buf, size_t len) |
| 4139 | { |
| 4140 | time_t uptime1; |
| 4141 | struct tm *tm; |
| 4142 | |
| 4143 | /* Check buffer length. */ |
| 4144 | if (len < BGP_UPTIME_LEN) |
| 4145 | { |
gdt | c29fdba | 2004-12-09 14:46:46 +0000 | [diff] [blame] | 4146 | /* XXX: warning: long int format, size_t arg (arg 2) */ |
paul | a2b1ecd | 2004-10-31 18:58:09 +0000 | [diff] [blame] | 4147 | zlog_warn ("peer_uptime (): buffer shortage %ld", len); |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 4148 | /* XXX: should return status instead of buf... */ |
| 4149 | snprintf (buf, len, "<error> "); |
| 4150 | return buf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | /* If there is no connection has been done before print `never'. */ |
| 4154 | if (uptime2 == 0) |
| 4155 | { |
| 4156 | snprintf (buf, len, "never "); |
| 4157 | return buf; |
| 4158 | } |
| 4159 | |
| 4160 | /* Get current time. */ |
| 4161 | uptime1 = time (NULL); |
| 4162 | uptime1 -= uptime2; |
| 4163 | tm = gmtime (&uptime1); |
| 4164 | |
| 4165 | /* Making formatted timer strings. */ |
| 4166 | #define ONE_DAY_SECOND 60*60*24 |
| 4167 | #define ONE_WEEK_SECOND 60*60*24*7 |
| 4168 | |
| 4169 | if (uptime1 < ONE_DAY_SECOND) |
| 4170 | snprintf (buf, len, "%02d:%02d:%02d", |
| 4171 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 4172 | else if (uptime1 < ONE_WEEK_SECOND) |
| 4173 | snprintf (buf, len, "%dd%02dh%02dm", |
| 4174 | tm->tm_yday, tm->tm_hour, tm->tm_min); |
| 4175 | else |
| 4176 | snprintf (buf, len, "%02dw%dd%02dh", |
| 4177 | tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour); |
| 4178 | return buf; |
| 4179 | } |
| 4180 | |
| 4181 | void |
| 4182 | bgp_config_write_filter (struct vty *vty, struct peer *peer, |
| 4183 | afi_t afi, safi_t safi) |
| 4184 | { |
| 4185 | struct bgp_filter *filter; |
| 4186 | struct bgp_filter *gfilter = NULL; |
| 4187 | char *addr; |
| 4188 | int in = FILTER_IN; |
| 4189 | int out = FILTER_OUT; |
| 4190 | |
| 4191 | addr = peer->host; |
| 4192 | filter = &peer->filter[afi][safi]; |
| 4193 | if (peer->af_group[afi][safi]) |
| 4194 | gfilter = &peer->group->conf->filter[afi][safi]; |
| 4195 | |
| 4196 | /* distribute-list. */ |
| 4197 | if (filter->dlist[in].name) |
| 4198 | if (! gfilter || ! gfilter->dlist[in].name |
| 4199 | || strcmp (filter->dlist[in].name, gfilter->dlist[in].name) != 0) |
| 4200 | vty_out (vty, " neighbor %s distribute-list %s in%s", addr, |
| 4201 | filter->dlist[in].name, VTY_NEWLINE); |
| 4202 | if (filter->dlist[out].name && ! gfilter) |
| 4203 | vty_out (vty, " neighbor %s distribute-list %s out%s", addr, |
| 4204 | filter->dlist[out].name, VTY_NEWLINE); |
| 4205 | |
| 4206 | /* prefix-list. */ |
| 4207 | if (filter->plist[in].name) |
| 4208 | if (! gfilter || ! gfilter->plist[in].name |
| 4209 | || strcmp (filter->plist[in].name, gfilter->plist[in].name) != 0) |
| 4210 | vty_out (vty, " neighbor %s prefix-list %s in%s", addr, |
| 4211 | filter->plist[in].name, VTY_NEWLINE); |
| 4212 | if (filter->plist[out].name && ! gfilter) |
| 4213 | vty_out (vty, " neighbor %s prefix-list %s out%s", addr, |
| 4214 | filter->plist[out].name, VTY_NEWLINE); |
| 4215 | |
| 4216 | /* route-map. */ |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 4217 | if (filter->map[RMAP_IN].name) |
| 4218 | if (! gfilter || ! gfilter->map[RMAP_IN].name |
| 4219 | || strcmp (filter->map[RMAP_IN].name, gfilter->map[RMAP_IN].name) != 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4220 | vty_out (vty, " neighbor %s route-map %s in%s", addr, |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 4221 | filter->map[RMAP_IN].name, VTY_NEWLINE); |
| 4222 | if (filter->map[RMAP_OUT].name && ! gfilter) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4223 | vty_out (vty, " neighbor %s route-map %s out%s", addr, |
paul | fee0f4c | 2004-09-13 05:12:46 +0000 | [diff] [blame] | 4224 | filter->map[RMAP_OUT].name, VTY_NEWLINE); |
| 4225 | if (filter->map[RMAP_IMPORT].name && ! gfilter) |
| 4226 | vty_out (vty, " neighbor %s route-map %s import%s", addr, |
| 4227 | filter->map[RMAP_IMPORT].name, VTY_NEWLINE); |
| 4228 | if (filter->map[RMAP_EXPORT].name) |
| 4229 | if (! gfilter || ! gfilter->map[RMAP_EXPORT].name |
| 4230 | || strcmp (filter->map[RMAP_EXPORT].name, |
| 4231 | gfilter->map[RMAP_EXPORT].name) != 0) |
| 4232 | vty_out (vty, " neighbor %s route-map %s export%s", addr, |
| 4233 | filter->map[RMAP_EXPORT].name, VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4234 | |
| 4235 | /* unsuppress-map */ |
| 4236 | if (filter->usmap.name && ! gfilter) |
| 4237 | vty_out (vty, " neighbor %s unsuppress-map %s%s", addr, |
| 4238 | filter->usmap.name, VTY_NEWLINE); |
| 4239 | |
| 4240 | /* filter-list. */ |
| 4241 | if (filter->aslist[in].name) |
| 4242 | if (! gfilter || ! gfilter->aslist[in].name |
| 4243 | || strcmp (filter->aslist[in].name, gfilter->aslist[in].name) != 0) |
| 4244 | vty_out (vty, " neighbor %s filter-list %s in%s", addr, |
| 4245 | filter->aslist[in].name, VTY_NEWLINE); |
| 4246 | if (filter->aslist[out].name && ! gfilter) |
| 4247 | vty_out (vty, " neighbor %s filter-list %s out%s", addr, |
| 4248 | filter->aslist[out].name, VTY_NEWLINE); |
| 4249 | } |
| 4250 | |
| 4251 | /* BGP peer configuration display function. */ |
| 4252 | void |
| 4253 | bgp_config_write_peer (struct vty *vty, struct bgp *bgp, |
| 4254 | struct peer *peer, afi_t afi, safi_t safi) |
| 4255 | { |
| 4256 | struct bgp_filter *filter; |
| 4257 | struct peer *g_peer = NULL; |
| 4258 | char buf[SU_ADDRSTRLEN]; |
| 4259 | char *addr; |
| 4260 | |
| 4261 | filter = &peer->filter[afi][safi]; |
| 4262 | addr = peer->host; |
| 4263 | if (peer_group_active (peer)) |
| 4264 | g_peer = peer->group->conf; |
| 4265 | |
| 4266 | /************************************ |
| 4267 | ****** Global to the neighbor ****** |
| 4268 | ************************************/ |
| 4269 | if (afi == AFI_IP && safi == SAFI_UNICAST) |
| 4270 | { |
| 4271 | /* remote-as. */ |
| 4272 | if (! peer_group_active (peer)) |
| 4273 | { |
| 4274 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) |
| 4275 | vty_out (vty, " neighbor %s peer-group%s", addr, |
| 4276 | VTY_NEWLINE); |
| 4277 | if (peer->as) |
| 4278 | vty_out (vty, " neighbor %s remote-as %d%s", addr, peer->as, |
| 4279 | VTY_NEWLINE); |
| 4280 | } |
| 4281 | else |
| 4282 | { |
| 4283 | if (! g_peer->as) |
| 4284 | vty_out (vty, " neighbor %s remote-as %d%s", addr, peer->as, |
| 4285 | VTY_NEWLINE); |
| 4286 | if (peer->af_group[AFI_IP][SAFI_UNICAST]) |
| 4287 | vty_out (vty, " neighbor %s peer-group %s%s", addr, |
| 4288 | peer->group->name, VTY_NEWLINE); |
| 4289 | } |
| 4290 | |
| 4291 | /* local-as. */ |
| 4292 | if (peer->change_local_as) |
| 4293 | if (! peer_group_active (peer)) |
| 4294 | vty_out (vty, " neighbor %s local-as %d%s%s", addr, |
| 4295 | peer->change_local_as, |
| 4296 | CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND) ? |
| 4297 | " no-prepend" : "", VTY_NEWLINE); |
| 4298 | |
| 4299 | /* Description. */ |
| 4300 | if (peer->desc) |
| 4301 | vty_out (vty, " neighbor %s description %s%s", addr, peer->desc, |
| 4302 | VTY_NEWLINE); |
| 4303 | |
| 4304 | /* Shutdown. */ |
| 4305 | if (CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN)) |
| 4306 | if (! peer_group_active (peer) || |
| 4307 | ! CHECK_FLAG (g_peer->flags, PEER_FLAG_SHUTDOWN)) |
| 4308 | vty_out (vty, " neighbor %s shutdown%s", addr, VTY_NEWLINE); |
| 4309 | |
| 4310 | /* BGP port. */ |
| 4311 | if (peer->port != BGP_PORT_DEFAULT) |
| 4312 | vty_out (vty, " neighbor %s port %d%s", addr, peer->port, |
| 4313 | VTY_NEWLINE); |
| 4314 | |
| 4315 | /* Local interface name. */ |
| 4316 | if (peer->ifname) |
| 4317 | vty_out (vty, " neighbor %s interface %s%s", addr, peer->ifname, |
| 4318 | VTY_NEWLINE); |
| 4319 | |
| 4320 | /* Passive. */ |
| 4321 | if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSIVE)) |
| 4322 | if (! peer_group_active (peer) || |
| 4323 | ! CHECK_FLAG (g_peer->flags, PEER_FLAG_PASSIVE)) |
| 4324 | vty_out (vty, " neighbor %s passive%s", addr, VTY_NEWLINE); |
| 4325 | |
| 4326 | /* EBGP multihop. */ |
| 4327 | if (peer_sort (peer) != BGP_PEER_IBGP && peer->ttl != 1) |
| 4328 | if (! peer_group_active (peer) || |
| 4329 | g_peer->ttl != peer->ttl) |
| 4330 | vty_out (vty, " neighbor %s ebgp-multihop %d%s", addr, peer->ttl, |
| 4331 | VTY_NEWLINE); |
| 4332 | |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 4333 | /* disable-connected-check. */ |
| 4334 | if (CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4335 | if (! peer_group_active (peer) || |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 4336 | ! CHECK_FLAG (g_peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
| 4337 | vty_out (vty, " neighbor %s disable-connected-check%s", addr, VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4338 | |
| 4339 | /* Update-source. */ |
| 4340 | if (peer->update_if) |
| 4341 | if (! peer_group_active (peer) || ! g_peer->update_if |
| 4342 | || strcmp (g_peer->update_if, peer->update_if) != 0) |
| 4343 | vty_out (vty, " neighbor %s update-source %s%s", addr, |
| 4344 | peer->update_if, VTY_NEWLINE); |
| 4345 | if (peer->update_source) |
| 4346 | if (! peer_group_active (peer) || ! g_peer->update_source |
| 4347 | || sockunion_cmp (g_peer->update_source, |
| 4348 | peer->update_source) != 0) |
| 4349 | vty_out (vty, " neighbor %s update-source %s%s", addr, |
| 4350 | sockunion2str (peer->update_source, buf, SU_ADDRSTRLEN), |
| 4351 | VTY_NEWLINE); |
| 4352 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4353 | /* advertisement-interval */ |
| 4354 | if (CHECK_FLAG (peer->config, PEER_CONFIG_ROUTEADV)) |
| 4355 | vty_out (vty, " neighbor %s advertisement-interval %d%s", |
| 4356 | addr, peer->v_routeadv, VTY_NEWLINE); |
| 4357 | |
| 4358 | /* timers. */ |
| 4359 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER) |
| 4360 | && ! peer_group_active (peer)) |
| 4361 | vty_out (vty, " neighbor %s timers %d %d%s", addr, |
| 4362 | peer->keepalive, peer->holdtime, VTY_NEWLINE); |
| 4363 | |
| 4364 | if (CHECK_FLAG (peer->config, PEER_CONFIG_CONNECT)) |
| 4365 | vty_out (vty, " neighbor %s timers connect %d%s", addr, |
| 4366 | peer->connect, VTY_NEWLINE); |
| 4367 | |
| 4368 | /* Default weight. */ |
| 4369 | if (CHECK_FLAG (peer->config, PEER_CONFIG_WEIGHT)) |
| 4370 | if (! peer_group_active (peer) || |
| 4371 | g_peer->weight != peer->weight) |
| 4372 | vty_out (vty, " neighbor %s weight %d%s", addr, peer->weight, |
| 4373 | VTY_NEWLINE); |
| 4374 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4375 | /* Dynamic capability. */ |
| 4376 | if (CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY)) |
| 4377 | if (! peer_group_active (peer) || |
| 4378 | ! CHECK_FLAG (g_peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY)) |
| 4379 | vty_out (vty, " neighbor %s capability dynamic%s", addr, |
| 4380 | VTY_NEWLINE); |
| 4381 | |
| 4382 | /* dont capability negotiation. */ |
| 4383 | if (CHECK_FLAG (peer->flags, PEER_FLAG_DONT_CAPABILITY)) |
| 4384 | if (! peer_group_active (peer) || |
| 4385 | ! CHECK_FLAG (g_peer->flags, PEER_FLAG_DONT_CAPABILITY)) |
| 4386 | vty_out (vty, " neighbor %s dont-capability-negotiate%s", addr, |
| 4387 | VTY_NEWLINE); |
| 4388 | |
| 4389 | /* override capability negotiation. */ |
| 4390 | if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 4391 | if (! peer_group_active (peer) || |
| 4392 | ! CHECK_FLAG (g_peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 4393 | vty_out (vty, " neighbor %s override-capability%s", addr, |
| 4394 | VTY_NEWLINE); |
| 4395 | |
| 4396 | /* strict capability negotiation. */ |
| 4397 | if (CHECK_FLAG (peer->flags, PEER_FLAG_STRICT_CAP_MATCH)) |
| 4398 | if (! peer_group_active (peer) || |
| 4399 | ! CHECK_FLAG (g_peer->flags, PEER_FLAG_STRICT_CAP_MATCH)) |
| 4400 | vty_out (vty, " neighbor %s strict-capability-match%s", addr, |
| 4401 | VTY_NEWLINE); |
| 4402 | |
| 4403 | if (! peer_group_active (peer)) |
| 4404 | { |
| 4405 | if (bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4)) |
| 4406 | { |
| 4407 | if (peer->afc[AFI_IP][SAFI_UNICAST]) |
| 4408 | vty_out (vty, " neighbor %s activate%s", addr, VTY_NEWLINE); |
| 4409 | } |
| 4410 | else |
| 4411 | { |
| 4412 | if (! peer->afc[AFI_IP][SAFI_UNICAST]) |
| 4413 | vty_out (vty, " no neighbor %s activate%s", addr, VTY_NEWLINE); |
| 4414 | } |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | |
| 4419 | /************************************ |
| 4420 | ****** Per AF to the neighbor ****** |
| 4421 | ************************************/ |
| 4422 | |
| 4423 | if (! (afi == AFI_IP && safi == SAFI_UNICAST)) |
| 4424 | { |
| 4425 | if (peer->af_group[afi][safi]) |
| 4426 | vty_out (vty, " neighbor %s peer-group %s%s", addr, |
| 4427 | peer->group->name, VTY_NEWLINE); |
| 4428 | else |
| 4429 | vty_out (vty, " neighbor %s activate%s", addr, VTY_NEWLINE); |
| 4430 | } |
| 4431 | |
| 4432 | /* ORF capability. */ |
| 4433 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 4434 | || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 4435 | if (! peer->af_group[afi][safi]) |
| 4436 | { |
| 4437 | vty_out (vty, " neighbor %s capability orf prefix-list", addr); |
| 4438 | |
| 4439 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 4440 | && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 4441 | vty_out (vty, " both"); |
| 4442 | else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)) |
| 4443 | vty_out (vty, " send"); |
| 4444 | else |
| 4445 | vty_out (vty, " receive"); |
| 4446 | vty_out (vty, "%s", VTY_NEWLINE); |
| 4447 | } |
| 4448 | |
| 4449 | /* Route reflector client. */ |
| 4450 | if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_REFLECTOR_CLIENT) |
| 4451 | && ! peer->af_group[afi][safi]) |
| 4452 | vty_out (vty, " neighbor %s route-reflector-client%s", addr, |
| 4453 | VTY_NEWLINE); |
| 4454 | |
| 4455 | /* Nexthop self. */ |
| 4456 | if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_NEXTHOP_SELF) |
| 4457 | && ! peer->af_group[afi][safi]) |
| 4458 | vty_out (vty, " neighbor %s next-hop-self%s", addr, VTY_NEWLINE); |
| 4459 | |
| 4460 | /* Remove private AS. */ |
| 4461 | if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_REMOVE_PRIVATE_AS) |
| 4462 | && ! peer->af_group[afi][safi]) |
| 4463 | vty_out (vty, " neighbor %s remove-private-AS%s", |
| 4464 | addr, VTY_NEWLINE); |
| 4465 | |
| 4466 | /* send-community print. */ |
| 4467 | if (! peer->af_group[afi][safi]) |
| 4468 | { |
| 4469 | if (bgp_option_check (BGP_OPT_CONFIG_CISCO)) |
| 4470 | { |
| 4471 | if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY) |
| 4472 | && peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY)) |
| 4473 | vty_out (vty, " neighbor %s send-community both%s", addr, VTY_NEWLINE); |
| 4474 | else if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY)) |
| 4475 | vty_out (vty, " neighbor %s send-community extended%s", |
| 4476 | addr, VTY_NEWLINE); |
| 4477 | else if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY)) |
| 4478 | vty_out (vty, " neighbor %s send-community%s", addr, VTY_NEWLINE); |
| 4479 | } |
| 4480 | else |
| 4481 | { |
| 4482 | if (! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY) |
| 4483 | && ! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY)) |
| 4484 | vty_out (vty, " no neighbor %s send-community both%s", |
| 4485 | addr, VTY_NEWLINE); |
| 4486 | else if (! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_EXT_COMMUNITY)) |
| 4487 | vty_out (vty, " no neighbor %s send-community extended%s", |
| 4488 | addr, VTY_NEWLINE); |
| 4489 | else if (! peer_af_flag_check (peer, afi, safi, PEER_FLAG_SEND_COMMUNITY)) |
| 4490 | vty_out (vty, " no neighbor %s send-community%s", |
| 4491 | addr, VTY_NEWLINE); |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | /* Default information */ |
| 4496 | if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_DEFAULT_ORIGINATE) |
| 4497 | && ! peer->af_group[afi][safi]) |
| 4498 | { |
| 4499 | vty_out (vty, " neighbor %s default-originate", addr); |
| 4500 | if (peer->default_rmap[afi][safi].name) |
| 4501 | vty_out (vty, " route-map %s", peer->default_rmap[afi][safi].name); |
| 4502 | vty_out (vty, "%s", VTY_NEWLINE); |
| 4503 | } |
| 4504 | |
| 4505 | /* Soft reconfiguration inbound. */ |
| 4506 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)) |
| 4507 | if (! peer->af_group[afi][safi] || |
| 4508 | ! CHECK_FLAG (g_peer->af_flags[afi][safi], PEER_FLAG_SOFT_RECONFIG)) |
| 4509 | vty_out (vty, " neighbor %s soft-reconfiguration inbound%s", addr, |
| 4510 | VTY_NEWLINE); |
| 4511 | |
| 4512 | /* maximum-prefix. */ |
| 4513 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX)) |
| 4514 | if (! peer->af_group[afi][safi] |
| 4515 | || g_peer->pmax[afi][safi] != peer->pmax[afi][safi] |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4516 | || g_peer->pmax_threshold[afi][safi] != peer->pmax_threshold[afi][safi] |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4517 | || CHECK_FLAG (g_peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING) |
| 4518 | != CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING)) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4519 | { |
hasso | 0a486e5 | 2005-02-01 20:57:17 +0000 | [diff] [blame] | 4520 | vty_out (vty, " neighbor %s maximum-prefix %ld", addr, peer->pmax[afi][safi]); |
| 4521 | if (peer->pmax_threshold[afi][safi] != MAXIMUM_PREFIX_THRESHOLD_DEFAULT) |
| 4522 | vty_out (vty, " %d", peer->pmax_threshold[afi][safi]); |
| 4523 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MAX_PREFIX_WARNING)) |
| 4524 | vty_out (vty, " warning-only"); |
| 4525 | if (peer->pmax_restart[afi][safi]) |
| 4526 | vty_out (vty, " restart %d", peer->pmax_restart[afi][safi]); |
| 4527 | vty_out (vty, "%s", VTY_NEWLINE); |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4528 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4529 | |
| 4530 | /* Route server client. */ |
| 4531 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT) |
| 4532 | && ! peer->af_group[afi][safi]) |
| 4533 | vty_out (vty, " neighbor %s route-server-client%s", addr, VTY_NEWLINE); |
| 4534 | |
| 4535 | /* Allow AS in. */ |
| 4536 | if (peer_af_flag_check (peer, afi, safi, PEER_FLAG_ALLOWAS_IN)) |
| 4537 | if (! peer_group_active (peer) |
| 4538 | || ! peer_af_flag_check (g_peer, afi, safi, PEER_FLAG_ALLOWAS_IN) |
| 4539 | || peer->allowas_in[afi][safi] != g_peer->allowas_in[afi][safi]) |
| 4540 | { |
| 4541 | if (peer->allowas_in[afi][safi] == 3) |
| 4542 | vty_out (vty, " neighbor %s allowas-in%s", addr, VTY_NEWLINE); |
| 4543 | else |
| 4544 | vty_out (vty, " neighbor %s allowas-in %d%s", addr, |
| 4545 | peer->allowas_in[afi][safi], VTY_NEWLINE); |
| 4546 | } |
| 4547 | |
| 4548 | /* Filter. */ |
| 4549 | bgp_config_write_filter (vty, peer, afi, safi); |
| 4550 | |
| 4551 | /* atribute-unchanged. */ |
| 4552 | if ((CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED) |
| 4553 | || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED) |
| 4554 | || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED)) |
| 4555 | && ! peer->af_group[afi][safi]) |
| 4556 | { |
| 4557 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED) |
| 4558 | && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED) |
| 4559 | && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED)) |
| 4560 | vty_out (vty, " neighbor %s attribute-unchanged%s", addr, VTY_NEWLINE); |
| 4561 | else |
| 4562 | vty_out (vty, " neighbor %s attribute-unchanged%s%s%s%s", addr, |
| 4563 | (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)) ? |
| 4564 | " as-path" : "", |
| 4565 | (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_NEXTHOP_UNCHANGED)) ? |
| 4566 | " next-hop" : "", |
| 4567 | (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_MED_UNCHANGED)) ? |
| 4568 | " med" : "", VTY_NEWLINE); |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | /* Display "address-family" configuration header. */ |
| 4573 | void |
| 4574 | bgp_config_write_family_header (struct vty *vty, afi_t afi, safi_t safi, |
| 4575 | int *write) |
| 4576 | { |
| 4577 | if (*write) |
| 4578 | return; |
| 4579 | |
| 4580 | if (afi == AFI_IP && safi == SAFI_UNICAST) |
| 4581 | return; |
| 4582 | |
| 4583 | vty_out (vty, "!%s address-family ", VTY_NEWLINE); |
| 4584 | |
| 4585 | if (afi == AFI_IP) |
| 4586 | { |
| 4587 | if (safi == SAFI_MULTICAST) |
| 4588 | vty_out (vty, "ipv4 multicast"); |
| 4589 | else if (safi == SAFI_MPLS_VPN) |
| 4590 | vty_out (vty, "vpnv4 unicast"); |
| 4591 | } |
| 4592 | else if (afi == AFI_IP6) |
| 4593 | vty_out (vty, "ipv6"); |
| 4594 | |
| 4595 | vty_out (vty, "%s", VTY_NEWLINE); |
| 4596 | |
| 4597 | *write = 1; |
| 4598 | } |
| 4599 | |
| 4600 | /* Address family based peer configuration display. */ |
| 4601 | int |
| 4602 | bgp_config_write_family (struct vty *vty, struct bgp *bgp, afi_t afi, |
| 4603 | safi_t safi) |
| 4604 | { |
| 4605 | int write = 0; |
| 4606 | struct peer *peer; |
| 4607 | struct peer_group *group; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4608 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4609 | |
| 4610 | bgp_config_write_network (vty, bgp, afi, safi, &write); |
| 4611 | |
| 4612 | bgp_config_write_redistribute (vty, bgp, afi, safi, &write); |
| 4613 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4614 | for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4615 | { |
| 4616 | if (group->conf->afc[afi][safi]) |
| 4617 | { |
| 4618 | bgp_config_write_family_header (vty, afi, safi, &write); |
| 4619 | bgp_config_write_peer (vty, bgp, group->conf, afi, safi); |
| 4620 | } |
| 4621 | } |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4622 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4623 | { |
| 4624 | if (peer->afc[afi][safi]) |
| 4625 | { |
| 4626 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 4627 | { |
| 4628 | bgp_config_write_family_header (vty, afi, safi, &write); |
| 4629 | bgp_config_write_peer (vty, bgp, peer, afi, safi); |
| 4630 | } |
| 4631 | } |
| 4632 | } |
| 4633 | if (write) |
| 4634 | vty_out (vty, " exit-address-family%s", VTY_NEWLINE); |
| 4635 | |
| 4636 | return write; |
| 4637 | } |
| 4638 | |
| 4639 | int |
| 4640 | bgp_config_write (struct vty *vty) |
| 4641 | { |
| 4642 | int write = 0; |
| 4643 | struct bgp *bgp; |
| 4644 | struct peer_group *group; |
| 4645 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4646 | struct listnode *node, *nnode; |
| 4647 | struct listnode *mnode, *mnnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4648 | |
| 4649 | /* BGP Multiple instance. */ |
| 4650 | if (bgp_option_check (BGP_OPT_MULTIPLE_INSTANCE)) |
| 4651 | { |
| 4652 | vty_out (vty, "bgp multiple-instance%s", VTY_NEWLINE); |
| 4653 | write++; |
| 4654 | } |
| 4655 | |
| 4656 | /* BGP Config type. */ |
| 4657 | if (bgp_option_check (BGP_OPT_CONFIG_CISCO)) |
| 4658 | { |
| 4659 | vty_out (vty, "bgp config-type cisco%s", VTY_NEWLINE); |
| 4660 | write++; |
| 4661 | } |
| 4662 | |
| 4663 | /* BGP configuration. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4664 | for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4665 | { |
| 4666 | if (write) |
| 4667 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 4668 | |
| 4669 | /* Router bgp ASN */ |
| 4670 | vty_out (vty, "router bgp %d", bgp->as); |
| 4671 | |
| 4672 | if (bgp_option_check (BGP_OPT_MULTIPLE_INSTANCE)) |
| 4673 | { |
| 4674 | if (bgp->name) |
| 4675 | vty_out (vty, " view %s", bgp->name); |
| 4676 | } |
| 4677 | vty_out (vty, "%s", VTY_NEWLINE); |
| 4678 | |
| 4679 | /* No Synchronization */ |
| 4680 | if (bgp_option_check (BGP_OPT_CONFIG_CISCO)) |
| 4681 | vty_out (vty, " no synchronization%s", VTY_NEWLINE); |
| 4682 | |
| 4683 | /* BGP fast-external-failover. */ |
| 4684 | if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) |
| 4685 | vty_out (vty, " no bgp fast-external-failover%s", VTY_NEWLINE); |
| 4686 | |
| 4687 | /* BGP router ID. */ |
| 4688 | if (CHECK_FLAG (bgp->config, BGP_CONFIG_ROUTER_ID)) |
| 4689 | vty_out (vty, " bgp router-id %s%s", inet_ntoa (bgp->router_id), |
| 4690 | VTY_NEWLINE); |
| 4691 | |
paul | 848973c | 2003-08-13 00:32:49 +0000 | [diff] [blame] | 4692 | /* BGP log-neighbor-changes. */ |
| 4693 | if (bgp_flag_check (bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) |
| 4694 | vty_out (vty, " bgp log-neighbor-changes%s", VTY_NEWLINE); |
| 4695 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4696 | /* BGP configuration. */ |
| 4697 | if (bgp_flag_check (bgp, BGP_FLAG_ALWAYS_COMPARE_MED)) |
| 4698 | vty_out (vty, " bgp always-compare-med%s", VTY_NEWLINE); |
| 4699 | |
| 4700 | /* BGP default ipv4-unicast. */ |
| 4701 | if (bgp_flag_check (bgp, BGP_FLAG_NO_DEFAULT_IPV4)) |
| 4702 | vty_out (vty, " no bgp default ipv4-unicast%s", VTY_NEWLINE); |
| 4703 | |
| 4704 | /* BGP default local-preference. */ |
| 4705 | if (bgp->default_local_pref != BGP_DEFAULT_LOCAL_PREF) |
| 4706 | vty_out (vty, " bgp default local-preference %d%s", |
| 4707 | bgp->default_local_pref, VTY_NEWLINE); |
| 4708 | |
| 4709 | /* BGP client-to-client reflection. */ |
| 4710 | if (bgp_flag_check (bgp, BGP_FLAG_NO_CLIENT_TO_CLIENT)) |
| 4711 | vty_out (vty, " no bgp client-to-client reflection%s", VTY_NEWLINE); |
| 4712 | |
| 4713 | /* BGP cluster ID. */ |
| 4714 | if (CHECK_FLAG (bgp->config, BGP_CONFIG_CLUSTER_ID)) |
| 4715 | vty_out (vty, " bgp cluster-id %s%s", inet_ntoa (bgp->cluster_id), |
| 4716 | VTY_NEWLINE); |
| 4717 | |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4718 | /* Confederation identifier*/ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4719 | if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION)) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4720 | vty_out (vty, " bgp confederation identifier %i%s", bgp->confed_id, |
| 4721 | VTY_NEWLINE); |
| 4722 | |
| 4723 | /* Confederation peer */ |
| 4724 | if (bgp->confed_peers_cnt > 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4725 | { |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4726 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4727 | |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4728 | vty_out (vty, " bgp confederation peers"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4729 | |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4730 | for (i = 0; i < bgp->confed_peers_cnt; i++) |
| 4731 | vty_out(vty, " %d", bgp->confed_peers[i]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4732 | |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 4733 | vty_out (vty, "%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | /* BGP enforce-first-as. */ |
| 4737 | if (bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS)) |
| 4738 | vty_out (vty, " bgp enforce-first-as%s", VTY_NEWLINE); |
| 4739 | |
| 4740 | /* BGP deterministic-med. */ |
| 4741 | if (bgp_flag_check (bgp, BGP_FLAG_DETERMINISTIC_MED)) |
| 4742 | vty_out (vty, " bgp deterministic-med%s", VTY_NEWLINE); |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 4743 | |
| 4744 | /* BGP graceful-restart. */ |
hasso | 93406d8 | 2005-02-02 14:40:33 +0000 | [diff] [blame] | 4745 | if (bgp->stalepath_time != BGP_DEFAULT_STALEPATH_TIME) |
| 4746 | vty_out (vty, " bgp graceful-restart stalepath-time %d%s", |
| 4747 | bgp->stalepath_time, VTY_NEWLINE); |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 4748 | if (bgp_flag_check (bgp, BGP_FLAG_GRACEFUL_RESTART)) |
| 4749 | vty_out (vty, " bgp graceful-restart%s", VTY_NEWLINE); |
| 4750 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4751 | /* BGP bestpath method. */ |
| 4752 | if (bgp_flag_check (bgp, BGP_FLAG_ASPATH_IGNORE)) |
| 4753 | vty_out (vty, " bgp bestpath as-path ignore%s", VTY_NEWLINE); |
hasso | 6811845 | 2005-04-08 15:40:36 +0000 | [diff] [blame] | 4754 | if (bgp_flag_check (bgp, BGP_FLAG_ASPATH_CONFED)) |
| 4755 | vty_out (vty, " bgp bestpath as-path confed%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4756 | if (bgp_flag_check (bgp, BGP_FLAG_COMPARE_ROUTER_ID)) |
| 4757 | vty_out (vty, " bgp bestpath compare-routerid%s", VTY_NEWLINE); |
| 4758 | if (bgp_flag_check (bgp, BGP_FLAG_MED_CONFED) |
| 4759 | || bgp_flag_check (bgp, BGP_FLAG_MED_MISSING_AS_WORST)) |
| 4760 | { |
| 4761 | vty_out (vty, " bgp bestpath med"); |
| 4762 | if (bgp_flag_check (bgp, BGP_FLAG_MED_CONFED)) |
| 4763 | vty_out (vty, " confed"); |
| 4764 | if (bgp_flag_check (bgp, BGP_FLAG_MED_MISSING_AS_WORST)) |
| 4765 | vty_out (vty, " missing-as-worst"); |
| 4766 | vty_out (vty, "%s", VTY_NEWLINE); |
| 4767 | } |
| 4768 | |
| 4769 | /* BGP network import check. */ |
| 4770 | if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)) |
| 4771 | vty_out (vty, " bgp network import-check%s", VTY_NEWLINE); |
| 4772 | |
| 4773 | /* BGP scan interval. */ |
| 4774 | bgp_config_write_scan_time (vty); |
| 4775 | |
| 4776 | /* BGP flag dampening. */ |
| 4777 | if (CHECK_FLAG (bgp->af_flags[AFI_IP][SAFI_UNICAST], |
| 4778 | BGP_CONFIG_DAMPENING)) |
| 4779 | bgp_config_write_damp (vty); |
| 4780 | |
| 4781 | /* BGP static route configuration. */ |
| 4782 | bgp_config_write_network (vty, bgp, AFI_IP, SAFI_UNICAST, &write); |
| 4783 | |
| 4784 | /* BGP redistribute configuration. */ |
| 4785 | bgp_config_write_redistribute (vty, bgp, AFI_IP, SAFI_UNICAST, &write); |
| 4786 | |
| 4787 | /* BGP timers configuration. */ |
| 4788 | if (bgp->default_keepalive != BGP_DEFAULT_KEEPALIVE |
| 4789 | && bgp->default_holdtime != BGP_DEFAULT_HOLDTIME) |
| 4790 | vty_out (vty, " timers bgp %d %d%s", bgp->default_keepalive, |
| 4791 | bgp->default_holdtime, VTY_NEWLINE); |
| 4792 | |
| 4793 | /* peer-group */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4794 | for (ALL_LIST_ELEMENTS (bgp->group, node, nnode, group)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4795 | { |
| 4796 | bgp_config_write_peer (vty, bgp, group->conf, AFI_IP, SAFI_UNICAST); |
| 4797 | } |
| 4798 | |
| 4799 | /* Normal neighbor configuration. */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4800 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4801 | { |
| 4802 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 4803 | bgp_config_write_peer (vty, bgp, peer, AFI_IP, SAFI_UNICAST); |
| 4804 | } |
| 4805 | |
| 4806 | /* Distance configuration. */ |
| 4807 | bgp_config_write_distance (vty, bgp); |
| 4808 | |
| 4809 | /* No auto-summary */ |
| 4810 | if (bgp_option_check (BGP_OPT_CONFIG_CISCO)) |
| 4811 | vty_out (vty, " no auto-summary%s", VTY_NEWLINE); |
| 4812 | |
| 4813 | /* IPv4 multicast configuration. */ |
| 4814 | write += bgp_config_write_family (vty, bgp, AFI_IP, SAFI_MULTICAST); |
| 4815 | |
| 4816 | /* IPv4 VPN configuration. */ |
| 4817 | write += bgp_config_write_family (vty, bgp, AFI_IP, SAFI_MPLS_VPN); |
| 4818 | |
| 4819 | /* IPv6 unicast configuration. */ |
| 4820 | write += bgp_config_write_family (vty, bgp, AFI_IP6, SAFI_UNICAST); |
| 4821 | |
| 4822 | write++; |
| 4823 | } |
| 4824 | return write; |
| 4825 | } |
| 4826 | |
| 4827 | void |
| 4828 | bgp_master_init () |
| 4829 | { |
| 4830 | memset (&bgp_master, 0, sizeof (struct bgp_master)); |
| 4831 | |
| 4832 | bm = &bgp_master; |
| 4833 | bm->bgp = list_new (); |
| 4834 | bm->port = BGP_PORT_DEFAULT; |
| 4835 | bm->master = thread_master_create (); |
| 4836 | bm->start_time = time (NULL); |
| 4837 | } |
| 4838 | |
| 4839 | void |
| 4840 | bgp_init () |
| 4841 | { |
| 4842 | void bgp_zebra_init (); |
| 4843 | void bgp_route_map_init (); |
| 4844 | void bgp_filter_init (); |
| 4845 | |
| 4846 | /* BGP VTY commands installation. */ |
| 4847 | bgp_vty_init (); |
| 4848 | |
| 4849 | /* Create BGP server socket. */ |
| 4850 | bgp_socket (NULL, bm->port); |
| 4851 | |
| 4852 | /* Init zebra. */ |
| 4853 | bgp_zebra_init (); |
| 4854 | |
| 4855 | /* BGP inits. */ |
| 4856 | bgp_attr_init (); |
| 4857 | bgp_debug_init (); |
| 4858 | bgp_dump_init (); |
| 4859 | bgp_route_init (); |
| 4860 | bgp_route_map_init (); |
| 4861 | bgp_scan_init (); |
| 4862 | bgp_mplsvpn_init (); |
| 4863 | |
| 4864 | /* Access list initialize. */ |
| 4865 | access_list_init (); |
| 4866 | access_list_add_hook (peer_distribute_update); |
| 4867 | access_list_delete_hook (peer_distribute_update); |
| 4868 | |
| 4869 | /* Filter list initialize. */ |
| 4870 | bgp_filter_init (); |
| 4871 | as_list_add_hook (peer_aslist_update); |
| 4872 | as_list_delete_hook (peer_aslist_update); |
| 4873 | |
| 4874 | /* Prefix list initialize.*/ |
| 4875 | prefix_list_init (); |
| 4876 | prefix_list_add_hook (peer_prefix_list_update); |
| 4877 | prefix_list_delete_hook (peer_prefix_list_update); |
| 4878 | |
| 4879 | /* Community list initialize. */ |
| 4880 | bgp_clist = community_list_init (); |
| 4881 | |
| 4882 | #ifdef HAVE_SNMP |
| 4883 | bgp_snmp_init (); |
| 4884 | #endif /* HAVE_SNMP */ |
| 4885 | } |
paul | 545acaf | 2004-04-20 15:13:15 +0000 | [diff] [blame] | 4886 | |
| 4887 | void |
| 4888 | bgp_terminate () |
| 4889 | { |
paul | 545acaf | 2004-04-20 15:13:15 +0000 | [diff] [blame] | 4890 | struct bgp *bgp; |
| 4891 | struct peer *peer; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4892 | struct listnode *node, *nnode; |
| 4893 | struct listnode *mnode, *mnnode; |
paul | 545acaf | 2004-04-20 15:13:15 +0000 | [diff] [blame] | 4894 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4895 | for (ALL_LIST_ELEMENTS (bm->bgp, mnode, mnnode, bgp)) |
| 4896 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 545acaf | 2004-04-20 15:13:15 +0000 | [diff] [blame] | 4897 | if (peer->status == Established) |
| 4898 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, |
| 4899 | BGP_NOTIFY_CEASE_PEER_UNCONFIG); |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 4900 | |
paul | 545acaf | 2004-04-20 15:13:15 +0000 | [diff] [blame] | 4901 | bgp_cleanup_routes (); |
| 4902 | } |
| 4903 | |