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