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