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