paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* zebra client |
| 2 | Copyright (C) 1997, 98, 99 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 |
| 18 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "command.h" |
| 24 | #include "stream.h" |
| 25 | #include "network.h" |
| 26 | #include "prefix.h" |
| 27 | #include "log.h" |
| 28 | #include "sockunion.h" |
| 29 | #include "zclient.h" |
| 30 | #include "routemap.h" |
| 31 | #include "thread.h" |
Donald Sharp | 0490729 | 2016-01-07 10:03:01 -0500 | [diff] [blame] | 32 | #include "filter.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
| 34 | #include "bgpd/bgpd.h" |
| 35 | #include "bgpd/bgp_route.h" |
| 36 | #include "bgpd/bgp_attr.h" |
| 37 | #include "bgpd/bgp_nexthop.h" |
| 38 | #include "bgpd/bgp_zebra.h" |
| 39 | #include "bgpd/bgp_fsm.h" |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 40 | #include "bgpd/bgp_debug.h" |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 41 | #include "bgpd/bgp_mpath.h" |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 42 | #include "bgpd/bgp_nexthop.h" |
| 43 | #include "bgpd/bgp_nht.h" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 44 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | /* All information about zebra. */ |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 46 | struct zclient *zclient = NULL; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 47 | struct in_addr router_id_zebra; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 49 | /* Growable buffer for nexthops sent to zebra */ |
| 50 | struct stream *bgp_nexthop_buf = NULL; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 51 | struct stream *bgp_ifindices_buf = NULL; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 52 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 53 | /* Router-id update message from zebra. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 54 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 55 | bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length, |
| 56 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 58 | struct prefix router_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 59 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 60 | zebra_router_id_update_read(zclient->ibuf,&router_id); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 61 | |
| 62 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 63 | { |
| 64 | char buf[128]; |
| 65 | prefix2str(&router_id, buf, sizeof(buf)); |
| 66 | zlog_debug("Zebra rcvd: router id update %s", buf); |
| 67 | } |
| 68 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 69 | router_id_zebra = router_id.u.prefix4; |
| 70 | |
David Lamparter | 584083d | 2016-05-24 18:58:08 +0200 | [diff] [blame] | 71 | bgp_router_id_zebra_bump (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 75 | /* Nexthop update message from zebra. */ |
| 76 | static int |
| 77 | bgp_read_nexthop_update (int command, struct zclient *zclient, |
| 78 | zebra_size_t length, vrf_id_t vrf_id) |
| 79 | { |
| 80 | bgp_parse_nexthop_update(); |
| 81 | return 0; |
| 82 | } |
| 83 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | /* Inteface addition message from zebra. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 85 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 86 | bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length, |
| 87 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | { |
| 89 | struct interface *ifp; |
| 90 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 91 | ifp = zebra_interface_add_read (zclient->ibuf, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 93 | if (BGP_DEBUG(zebra, ZEBRA) && ifp) |
| 94 | zlog_debug("Zebra rcvd: interface add %s", ifp->name); |
| 95 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 99 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 100 | bgp_interface_delete (int command, struct zclient *zclient, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 101 | zebra_size_t length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | { |
| 103 | struct stream *s; |
| 104 | struct interface *ifp; |
| 105 | |
| 106 | s = zclient->ibuf; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 107 | ifp = zebra_interface_state_read (s, vrf_id); |
ajs | d2fc889 | 2005-04-02 18:38:43 +0000 | [diff] [blame] | 108 | ifp->ifindex = IFINDEX_INTERNAL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 109 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 110 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 111 | zlog_debug("Zebra rcvd: interface delete %s", ifp->name); |
| 112 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 116 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 117 | bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length, |
| 118 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | { |
| 120 | struct stream *s; |
| 121 | struct interface *ifp; |
| 122 | struct connected *c; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 123 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | |
| 125 | s = zclient->ibuf; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 126 | ifp = zebra_interface_state_read (s, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | |
| 128 | if (! ifp) |
| 129 | return 0; |
| 130 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 131 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 132 | zlog_debug("Zebra rcvd: interface %s up", ifp->name); |
| 133 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 134 | for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c)) |
| 135 | bgp_connected_add (c); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 140 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 141 | bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length, |
| 142 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 143 | { |
| 144 | struct stream *s; |
| 145 | struct interface *ifp; |
| 146 | struct connected *c; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 147 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | |
| 149 | s = zclient->ibuf; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 150 | ifp = zebra_interface_state_read (s, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | if (! ifp) |
| 152 | return 0; |
| 153 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 154 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 155 | zlog_debug("Zebra rcvd: interface %s down", ifp->name); |
| 156 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 157 | for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c)) |
| 158 | bgp_connected_delete (c); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | |
Pradosh Mohapatra | 8da8689 | 2013-09-11 03:33:55 +0000 | [diff] [blame] | 160 | /* Fast external-failover */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 162 | struct listnode *mnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | struct bgp *bgp; |
| 164 | struct peer *peer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 166 | for (ALL_LIST_ELEMENTS_RO (bm->bgp, mnode, bgp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | { |
| 168 | if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER)) |
| 169 | continue; |
| 170 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 171 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | { |
Pradosh Mohapatra | 8da8689 | 2013-09-11 03:33:55 +0000 | [diff] [blame] | 173 | if ((peer->ttl != 1) && (peer->gtsm_hops != 1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 174 | continue; |
| 175 | |
Pradosh Mohapatra | 8da8689 | 2013-09-11 03:33:55 +0000 | [diff] [blame] | 176 | if (ifp == peer->nexthop.ifp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 185 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | bgp_interface_address_add (int command, struct zclient *zclient, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 187 | zebra_size_t length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 188 | { |
| 189 | struct connected *ifc; |
| 190 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 191 | ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 192 | |
| 193 | if (ifc == NULL) |
| 194 | return 0; |
| 195 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 196 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 197 | { |
| 198 | char buf[128]; |
| 199 | prefix2str(ifc->address, buf, sizeof(buf)); |
| 200 | zlog_debug("Zebra rcvd: interface %s address add %s", |
| 201 | ifc->ifp->name, buf); |
| 202 | } |
| 203 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 204 | if (if_is_operative (ifc->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | bgp_connected_add (ifc); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 210 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | bgp_interface_address_delete (int command, struct zclient *zclient, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 212 | zebra_size_t length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | { |
| 214 | struct connected *ifc; |
| 215 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 216 | ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | |
| 218 | if (ifc == NULL) |
| 219 | return 0; |
| 220 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 221 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 222 | { |
| 223 | char buf[128]; |
| 224 | prefix2str(ifc->address, buf, sizeof(buf)); |
| 225 | zlog_debug("Zebra rcvd: interface %s address delete %s", |
| 226 | ifc->ifp->name, buf); |
| 227 | } |
| 228 | |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 229 | if (if_is_operative (ifc->ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | bgp_connected_delete (ifc); |
| 231 | |
| 232 | connected_free (ifc); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /* Zebra route add and delete treatment. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 238 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 239 | zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length, |
| 240 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 241 | { |
| 242 | struct stream *s; |
| 243 | struct zapi_ipv4 api; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 244 | struct in_addr nexthop; |
| 245 | struct prefix_ipv4 p; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 246 | unsigned char plength = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 247 | |
| 248 | s = zclient->ibuf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | nexthop.s_addr = 0; |
| 250 | |
| 251 | /* Type, flags, message. */ |
| 252 | api.type = stream_getc (s); |
| 253 | api.flags = stream_getc (s); |
| 254 | api.message = stream_getc (s); |
| 255 | |
| 256 | /* IPv4 prefix. */ |
| 257 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 258 | p.family = AF_INET; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 259 | plength = stream_getc (s); |
| 260 | p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, plength); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 261 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 262 | |
| 263 | /* Nexthop, ifindex, distance, metric. */ |
| 264 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 265 | { |
| 266 | api.nexthop_num = stream_getc (s); |
| 267 | nexthop.s_addr = stream_get_ipv4 (s); |
| 268 | } |
| 269 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) |
| 270 | { |
| 271 | api.ifindex_num = stream_getc (s); |
Stephen Hemminger | 9206f9e | 2011-12-18 19:43:40 +0400 | [diff] [blame] | 272 | stream_getl (s); /* ifindex, unused */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | } |
| 274 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 275 | api.distance = stream_getc (s); |
| 276 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 277 | api.metric = stream_getl (s); |
| 278 | else |
| 279 | api.metric = 0; |
| 280 | |
| 281 | if (command == ZEBRA_IPV4_ROUTE_ADD) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 282 | { |
| 283 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 284 | { |
| 285 | char buf[2][INET_ADDRSTRLEN]; |
| 286 | zlog_debug("Zebra rcvd: IPv4 route add %s %s/%d nexthop %s metric %u", |
| 287 | zebra_route_string(api.type), |
| 288 | inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])), |
| 289 | p.prefixlen, |
| 290 | inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])), |
| 291 | api.metric); |
| 292 | } |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 293 | bgp_redistribute_add((struct prefix *)&p, &nexthop, NULL, |
| 294 | api.metric, api.type); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 295 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 296 | else |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 297 | { |
| 298 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 299 | { |
| 300 | char buf[2][INET_ADDRSTRLEN]; |
| 301 | zlog_debug("Zebra rcvd: IPv4 route delete %s %s/%d " |
| 302 | "nexthop %s metric %u", |
| 303 | zebra_route_string(api.type), |
| 304 | inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])), |
| 305 | p.prefixlen, |
| 306 | inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])), |
| 307 | api.metric); |
| 308 | } |
| 309 | bgp_redistribute_delete((struct prefix *)&p, api.type); |
| 310 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 315 | /* Zebra route add and delete treatment. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 316 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 317 | zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length, |
| 318 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 319 | { |
| 320 | struct stream *s; |
| 321 | struct zapi_ipv6 api; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | struct in6_addr nexthop; |
| 323 | struct prefix_ipv6 p; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 324 | unsigned char plength = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 325 | |
| 326 | s = zclient->ibuf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 328 | |
| 329 | /* Type, flags, message. */ |
| 330 | api.type = stream_getc (s); |
| 331 | api.flags = stream_getc (s); |
| 332 | api.message = stream_getc (s); |
| 333 | |
| 334 | /* IPv6 prefix. */ |
| 335 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 336 | p.family = AF_INET6; |
Donald Sharp | 5e57b5f | 2016-03-11 16:28:34 -0500 | [diff] [blame] | 337 | plength = stream_getc (s); |
| 338 | p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, plength); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 340 | |
| 341 | /* Nexthop, ifindex, distance, metric. */ |
| 342 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 343 | { |
| 344 | api.nexthop_num = stream_getc (s); |
| 345 | stream_get (&nexthop, s, 16); |
| 346 | } |
| 347 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX)) |
| 348 | { |
| 349 | api.ifindex_num = stream_getc (s); |
Stephen Hemminger | 9206f9e | 2011-12-18 19:43:40 +0400 | [diff] [blame] | 350 | stream_getl (s); /* ifindex, unused */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | } |
| 352 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 353 | api.distance = stream_getc (s); |
| 354 | else |
| 355 | api.distance = 0; |
| 356 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 357 | api.metric = stream_getl (s); |
| 358 | else |
| 359 | api.metric = 0; |
| 360 | |
| 361 | /* Simply ignore link-local address. */ |
| 362 | if (IN6_IS_ADDR_LINKLOCAL (&p.prefix)) |
| 363 | return 0; |
| 364 | |
| 365 | if (command == ZEBRA_IPV6_ROUTE_ADD) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 366 | { |
| 367 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 368 | { |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 369 | char buf[2][INET6_ADDRSTRLEN]; |
| 370 | zlog_debug("Zebra rcvd: IPv6 route add %s %s/%d nexthop %s metric %u", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 371 | zebra_route_string(api.type), |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 372 | inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])), |
| 373 | p.prefixlen, |
| 374 | inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])), |
| 375 | api.metric); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 376 | } |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 377 | bgp_redistribute_add ((struct prefix *)&p, NULL, &nexthop, |
| 378 | api.metric, api.type); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 379 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 380 | else |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 381 | { |
| 382 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 383 | { |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 384 | char buf[2][INET6_ADDRSTRLEN]; |
| 385 | zlog_debug("Zebra rcvd: IPv6 route delete %s %s/%d " |
| 386 | "nexthop %s metric %u", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 387 | zebra_route_string(api.type), |
Stephen Hemminger | f04a80a | 2011-12-06 14:51:10 +0400 | [diff] [blame] | 388 | inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])), |
| 389 | p.prefixlen, |
| 390 | inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])), |
| 391 | api.metric); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 392 | } |
| 393 | bgp_redistribute_delete ((struct prefix *) &p, api.type); |
| 394 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 398 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 399 | struct interface * |
| 400 | if_lookup_by_ipv4 (struct in_addr *addr) |
| 401 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 402 | struct listnode *ifnode; |
| 403 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 404 | struct interface *ifp; |
| 405 | struct connected *connected; |
| 406 | struct prefix_ipv4 p; |
| 407 | struct prefix *cp; |
| 408 | |
| 409 | p.family = AF_INET; |
| 410 | p.prefix = *addr; |
| 411 | p.prefixlen = IPV4_MAX_BITLEN; |
| 412 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 413 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 414 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 415 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 416 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 417 | cp = connected->address; |
| 418 | |
| 419 | if (cp->family == AF_INET) |
| 420 | if (prefix_match (cp, (struct prefix *)&p)) |
| 421 | return ifp; |
| 422 | } |
| 423 | } |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | struct interface * |
| 428 | if_lookup_by_ipv4_exact (struct in_addr *addr) |
| 429 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 430 | struct listnode *ifnode; |
| 431 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 432 | struct interface *ifp; |
| 433 | struct connected *connected; |
| 434 | struct prefix *cp; |
| 435 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 436 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 437 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 438 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 439 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 440 | cp = connected->address; |
| 441 | |
| 442 | if (cp->family == AF_INET) |
| 443 | if (IPV4_ADDR_SAME (&cp->u.prefix4, addr)) |
| 444 | return ifp; |
| 445 | } |
| 446 | } |
| 447 | return NULL; |
| 448 | } |
| 449 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 450 | struct interface * |
| 451 | if_lookup_by_ipv6 (struct in6_addr *addr) |
| 452 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 453 | struct listnode *ifnode; |
| 454 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 455 | struct interface *ifp; |
| 456 | struct connected *connected; |
| 457 | struct prefix_ipv6 p; |
| 458 | struct prefix *cp; |
| 459 | |
| 460 | p.family = AF_INET6; |
| 461 | p.prefix = *addr; |
| 462 | p.prefixlen = IPV6_MAX_BITLEN; |
| 463 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 464 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 465 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 466 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 467 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | cp = connected->address; |
| 469 | |
| 470 | if (cp->family == AF_INET6) |
| 471 | if (prefix_match (cp, (struct prefix *)&p)) |
| 472 | return ifp; |
| 473 | } |
| 474 | } |
| 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | struct interface * |
| 479 | if_lookup_by_ipv6_exact (struct in6_addr *addr) |
| 480 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 481 | struct listnode *ifnode; |
| 482 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | struct interface *ifp; |
| 484 | struct connected *connected; |
| 485 | struct prefix *cp; |
| 486 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 487 | for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 488 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 489 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 490 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 491 | cp = connected->address; |
| 492 | |
| 493 | if (cp->family == AF_INET6) |
| 494 | if (IPV6_ADDR_SAME (&cp->u.prefix6, addr)) |
| 495 | return ifp; |
| 496 | } |
| 497 | } |
| 498 | return NULL; |
| 499 | } |
| 500 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 501 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 502 | if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr) |
| 503 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 504 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 505 | struct connected *connected; |
| 506 | struct prefix *cp; |
| 507 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 508 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 509 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 510 | cp = connected->address; |
| 511 | |
| 512 | if (cp->family == AF_INET6) |
| 513 | if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6)) |
| 514 | { |
| 515 | memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN); |
| 516 | return 1; |
| 517 | } |
| 518 | } |
| 519 | return 0; |
| 520 | } |
| 521 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 522 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 523 | if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr) |
| 524 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 525 | struct listnode *cnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 526 | struct connected *connected; |
| 527 | struct prefix *cp; |
| 528 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 529 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 530 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | cp = connected->address; |
| 532 | |
| 533 | if (cp->family == AF_INET6) |
| 534 | if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6)) |
| 535 | { |
| 536 | memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN); |
| 537 | return 1; |
| 538 | } |
| 539 | } |
| 540 | return 0; |
| 541 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 542 | |
Pradosh Mohapatra | 6ee06fa | 2014-01-12 18:30:13 +0000 | [diff] [blame] | 543 | static int |
| 544 | if_get_ipv4_address (struct interface *ifp, struct in_addr *addr) |
| 545 | { |
| 546 | struct listnode *cnode; |
| 547 | struct connected *connected; |
| 548 | struct prefix *cp; |
| 549 | |
| 550 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected)) |
| 551 | { |
| 552 | cp = connected->address; |
| 553 | if ((cp->family == AF_INET) && !ipv4_martian(&(cp->u.prefix4))) |
| 554 | { |
| 555 | *addr = cp->u.prefix4; |
| 556 | return 1; |
| 557 | } |
| 558 | } |
| 559 | return 0; |
| 560 | } |
| 561 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | int |
| 563 | bgp_nexthop_set (union sockunion *local, union sockunion *remote, |
| 564 | struct bgp_nexthop *nexthop, struct peer *peer) |
| 565 | { |
| 566 | int ret = 0; |
| 567 | struct interface *ifp = NULL; |
| 568 | |
| 569 | memset (nexthop, 0, sizeof (struct bgp_nexthop)); |
| 570 | |
| 571 | if (!local) |
| 572 | return -1; |
| 573 | if (!remote) |
| 574 | return -1; |
| 575 | |
| 576 | if (local->sa.sa_family == AF_INET) |
| 577 | { |
| 578 | nexthop->v4 = local->sin.sin_addr; |
Vivek Venkatraman | fa2e786 | 2015-05-19 18:03:54 -0700 | [diff] [blame] | 579 | if (peer->update_if) |
| 580 | ifp = if_lookup_by_name (peer->update_if); |
| 581 | else |
| 582 | ifp = if_lookup_by_ipv4_exact (&local->sin.sin_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 584 | if (local->sa.sa_family == AF_INET6) |
| 585 | { |
| 586 | if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr)) |
| 587 | { |
| 588 | if (peer->ifname) |
Feng Lu | 395828e | 2015-05-22 11:39:55 +0200 | [diff] [blame] | 589 | ifp = if_lookup_by_name (peer->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 590 | } |
Vivek Venkatraman | fa2e786 | 2015-05-19 18:03:54 -0700 | [diff] [blame] | 591 | else if (peer->update_if) |
| 592 | ifp = if_lookup_by_name (peer->update_if); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 593 | else |
Vivek Venkatraman | fa2e786 | 2015-05-19 18:03:54 -0700 | [diff] [blame] | 594 | ifp = if_lookup_by_ipv6_exact (&local->sin6.sin6_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 595 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 596 | |
| 597 | if (!ifp) |
| 598 | return -1; |
| 599 | |
| 600 | nexthop->ifp = ifp; |
| 601 | |
| 602 | /* IPv4 connection. */ |
| 603 | if (local->sa.sa_family == AF_INET) |
| 604 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 605 | /* IPv6 nexthop*/ |
| 606 | ret = if_get_ipv6_global (ifp, &nexthop->v6_global); |
| 607 | |
| 608 | /* There is no global nexthop. */ |
| 609 | if (!ret) |
| 610 | if_get_ipv6_local (ifp, &nexthop->v6_global); |
| 611 | else |
| 612 | if_get_ipv6_local (ifp, &nexthop->v6_local); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | } |
| 614 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 615 | /* IPv6 connection. */ |
| 616 | if (local->sa.sa_family == AF_INET6) |
| 617 | { |
| 618 | struct interface *direct = NULL; |
| 619 | |
Pradosh Mohapatra | 6ee06fa | 2014-01-12 18:30:13 +0000 | [diff] [blame] | 620 | /* IPv4 nexthop. */ |
| 621 | ret = if_get_ipv4_address(ifp, &nexthop->v4); |
| 622 | if (!ret && peer->local_id.s_addr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 623 | nexthop->v4 = peer->local_id; |
| 624 | |
| 625 | /* Global address*/ |
| 626 | if (! IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr)) |
| 627 | { |
| 628 | memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, |
| 629 | IPV6_MAX_BYTELEN); |
| 630 | |
| 631 | /* If directory connected set link-local address. */ |
| 632 | direct = if_lookup_by_ipv6 (&remote->sin6.sin6_addr); |
| 633 | if (direct) |
| 634 | if_get_ipv6_local (ifp, &nexthop->v6_local); |
| 635 | } |
| 636 | else |
| 637 | /* Link-local address. */ |
| 638 | { |
| 639 | ret = if_get_ipv6_global (ifp, &nexthop->v6_global); |
| 640 | |
| 641 | /* If there is no global address. Set link-local address as |
| 642 | global. I know this break RFC specification... */ |
| 643 | if (!ret) |
| 644 | memcpy (&nexthop->v6_global, &local->sin6.sin6_addr, |
| 645 | IPV6_MAX_BYTELEN); |
| 646 | else |
| 647 | memcpy (&nexthop->v6_local, &local->sin6.sin6_addr, |
| 648 | IPV6_MAX_BYTELEN); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | if (IN6_IS_ADDR_LINKLOCAL (&local->sin6.sin6_addr) || |
| 653 | if_lookup_by_ipv6 (&remote->sin6.sin6_addr)) |
| 654 | peer->shared_network = 1; |
| 655 | else |
| 656 | peer->shared_network = 0; |
| 657 | |
| 658 | /* KAME stack specific treatment. */ |
| 659 | #ifdef KAME |
| 660 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_global) |
| 661 | && IN6_LINKLOCAL_IFINDEX (nexthop->v6_global)) |
| 662 | { |
| 663 | SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_global, 0); |
| 664 | } |
| 665 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->v6_local) |
| 666 | && IN6_LINKLOCAL_IFINDEX (nexthop->v6_local)) |
| 667 | { |
| 668 | SET_IN6_LINKLOCAL_IFINDEX (nexthop->v6_local, 0); |
| 669 | } |
| 670 | #endif /* KAME */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 671 | return ret; |
| 672 | } |
| 673 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 674 | void |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 675 | bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 676 | { |
| 677 | int flags; |
| 678 | u_char distance; |
| 679 | struct peer *peer; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 680 | struct bgp_info *mpinfo; |
| 681 | size_t oldsize, newsize; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 682 | u_int32_t nhcount; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | |
| 684 | if (zclient->sock < 0) |
| 685 | return; |
| 686 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 687 | if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 688 | return; |
| 689 | |
| 690 | flags = 0; |
| 691 | peer = info->peer; |
| 692 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 693 | if (peer->sort == BGP_PEER_IBGP || peer->sort == BGP_PEER_CONFED) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 694 | { |
| 695 | SET_FLAG (flags, ZEBRA_FLAG_IBGP); |
| 696 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 697 | } |
| 698 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 699 | if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1) |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 700 | || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 701 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 702 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 703 | nhcount = 1 + bgp_info_mpath_count (info); |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 704 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 705 | if (p->family == AF_INET) |
| 706 | { |
| 707 | struct zapi_ipv4 api; |
| 708 | struct in_addr *nexthop; |
| 709 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 710 | /* resize nexthop buffer size if necessary */ |
| 711 | if ((oldsize = stream_get_size (bgp_nexthop_buf)) < |
| 712 | (sizeof (struct in_addr *) * nhcount)) |
| 713 | { |
| 714 | newsize = (sizeof (struct in_addr *) * nhcount); |
| 715 | newsize = stream_resize (bgp_nexthop_buf, newsize); |
| 716 | if (newsize == oldsize) |
| 717 | { |
| 718 | zlog_err ("can't resize nexthop buffer"); |
| 719 | return; |
| 720 | } |
| 721 | } |
| 722 | stream_reset (bgp_nexthop_buf); |
| 723 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 724 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 725 | api.flags = flags; |
| 726 | nexthop = &info->attr->nexthop; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 727 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *)); |
| 728 | for (mpinfo = bgp_info_mpath_first (info); mpinfo; |
| 729 | mpinfo = bgp_info_mpath_next (mpinfo)) |
| 730 | { |
| 731 | nexthop = &mpinfo->attr->nexthop; |
| 732 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in_addr *)); |
| 733 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 734 | |
| 735 | api.type = ZEBRA_ROUTE_BGP; |
| 736 | api.message = 0; |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 737 | api.safi = safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 738 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 739 | api.nexthop_num = nhcount; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 740 | api.nexthop = (struct in_addr **)STREAM_DATA (bgp_nexthop_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 741 | api.ifindex_num = 0; |
| 742 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 743 | api.metric = info->attr->med; |
| 744 | |
| 745 | distance = bgp_distance_apply (p, info, bgp); |
| 746 | |
| 747 | if (distance) |
| 748 | { |
| 749 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); |
| 750 | api.distance = distance; |
| 751 | } |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 752 | |
| 753 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 754 | { |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 755 | int i; |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 756 | char buf[2][INET_ADDRSTRLEN]; |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 757 | zlog_debug("Zebra send: IPv4 route add %s/%d nexthop %s metric %u" |
| 758 | " count %d", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 759 | inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])), |
| 760 | p->prefixlen, |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 761 | inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])), |
| 762 | api.metric, api.nexthop_num); |
| 763 | for (i = 1; i < api.nexthop_num; i++) |
| 764 | zlog_debug("Zebra send: IPv4 route add [nexthop %d] %s", |
| 765 | i, inet_ntop(AF_INET, api.nexthop[i], buf[1], |
| 766 | sizeof(buf[1]))); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 767 | } |
| 768 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 769 | zapi_ipv4_route (ZEBRA_IPV4_ROUTE_ADD, zclient, |
| 770 | (struct prefix_ipv4 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | } |
Lou Berger | 205e674 | 2016-01-12 13:42:11 -0500 | [diff] [blame] | 772 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 773 | /* We have to think about a IPv6 link-local address curse. */ |
| 774 | if (p->family == AF_INET6) |
| 775 | { |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 776 | ifindex_t ifindex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 777 | struct in6_addr *nexthop; |
| 778 | struct zapi_ipv6 api; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 779 | int valid_nh_count = 0; |
| 780 | |
| 781 | /* resize nexthop buffer size if necessary */ |
| 782 | if ((oldsize = stream_get_size (bgp_nexthop_buf)) < |
| 783 | (sizeof (struct in6_addr *) * nhcount)) |
| 784 | { |
| 785 | newsize = (sizeof (struct in6_addr *) * nhcount); |
| 786 | newsize = stream_resize (bgp_nexthop_buf, newsize); |
| 787 | if (newsize == oldsize) |
| 788 | { |
| 789 | zlog_err ("can't resize nexthop buffer"); |
| 790 | return; |
| 791 | } |
| 792 | } |
| 793 | stream_reset (bgp_nexthop_buf); |
| 794 | |
| 795 | /* resize ifindices buffer size if necessary */ |
| 796 | if ((oldsize = stream_get_size (bgp_ifindices_buf)) < |
| 797 | (sizeof (unsigned int) * nhcount)) |
| 798 | { |
| 799 | newsize = (sizeof (unsigned int) * nhcount); |
| 800 | newsize = stream_resize (bgp_ifindices_buf, newsize); |
| 801 | if (newsize == oldsize) |
| 802 | { |
| 803 | zlog_err ("can't resize nexthop buffer"); |
| 804 | return; |
| 805 | } |
| 806 | } |
| 807 | stream_reset (bgp_ifindices_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 808 | |
| 809 | ifindex = 0; |
| 810 | nexthop = NULL; |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 811 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 812 | assert (info->attr->extra); |
| 813 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 814 | /* Only global address nexthop exists. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 815 | if (info->attr->extra->mp_nexthop_len == 16) |
| 816 | nexthop = &info->attr->extra->mp_nexthop_global; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 817 | |
| 818 | /* If both global and link-local address present. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 819 | if (info->attr->extra->mp_nexthop_len == 32) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | { |
| 821 | /* Workaround for Cisco's nexthop bug. */ |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 822 | if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | && peer->su_remote->sa.sa_family == AF_INET6) |
| 824 | nexthop = &peer->su_remote->sin6.sin6_addr; |
| 825 | else |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 826 | nexthop = &info->attr->extra->mp_nexthop_local; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 827 | |
| 828 | if (info->peer->nexthop.ifp) |
| 829 | ifindex = info->peer->nexthop.ifp->ifindex; |
| 830 | } |
| 831 | |
| 832 | if (nexthop == NULL) |
| 833 | return; |
| 834 | |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame] | 835 | if (!ifindex) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 836 | { |
| 837 | if (info->peer->ifname) |
Feng Lu | 395828e | 2015-05-22 11:39:55 +0200 | [diff] [blame] | 838 | ifindex = ifname2ifindex (info->peer->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 839 | else if (info->peer->nexthop.ifp) |
| 840 | ifindex = info->peer->nexthop.ifp->ifindex; |
| 841 | } |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 842 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *)); |
| 843 | stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int)); |
| 844 | valid_nh_count++; |
| 845 | |
| 846 | for (mpinfo = bgp_info_mpath_first (info); mpinfo; |
| 847 | mpinfo = bgp_info_mpath_next (mpinfo)) |
| 848 | { |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame] | 849 | ifindex = 0; |
| 850 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 851 | /* Only global address nexthop exists. */ |
| 852 | if (mpinfo->attr->extra->mp_nexthop_len == 16) |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 853 | nexthop = &mpinfo->attr->extra->mp_nexthop_global; |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame] | 854 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 855 | /* If both global and link-local address present. */ |
| 856 | if (mpinfo->attr->extra->mp_nexthop_len == 32) |
| 857 | { |
| 858 | /* Workaround for Cisco's nexthop bug. */ |
| 859 | if (IN6_IS_ADDR_UNSPECIFIED (&mpinfo->attr->extra->mp_nexthop_global) |
| 860 | && mpinfo->peer->su_remote->sa.sa_family == AF_INET6) |
| 861 | { |
| 862 | nexthop = &mpinfo->peer->su_remote->sin6.sin6_addr; |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | nexthop = &mpinfo->attr->extra->mp_nexthop_local; |
| 867 | } |
| 868 | |
| 869 | if (mpinfo->peer->nexthop.ifp) |
| 870 | { |
| 871 | ifindex = mpinfo->peer->nexthop.ifp->ifindex; |
| 872 | } |
| 873 | } |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame] | 874 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 875 | if (nexthop == NULL) |
| 876 | { |
| 877 | continue; |
| 878 | } |
| 879 | |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame] | 880 | if (!ifindex) |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 881 | { |
| 882 | if (mpinfo->peer->ifname) |
| 883 | { |
| 884 | ifindex = if_nametoindex (mpinfo->peer->ifname); |
| 885 | } |
| 886 | else if (mpinfo->peer->nexthop.ifp) |
| 887 | { |
| 888 | ifindex = mpinfo->peer->nexthop.ifp->ifindex; |
| 889 | } |
| 890 | } |
Dinesh Dutt | 4feb0d0 | 2015-11-09 20:14:55 -0500 | [diff] [blame] | 891 | |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 892 | if (ifindex == 0) |
| 893 | { |
| 894 | continue; |
| 895 | } |
| 896 | |
| 897 | stream_put (bgp_nexthop_buf, &nexthop, sizeof (struct in6_addr *)); |
| 898 | stream_put (bgp_ifindices_buf, &ifindex, sizeof (unsigned int)); |
| 899 | valid_nh_count++; |
| 900 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 901 | |
| 902 | /* Make Zebra API structure. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 903 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 904 | api.flags = flags; |
| 905 | api.type = ZEBRA_ROUTE_BGP; |
| 906 | api.message = 0; |
G.Balaji | c7ec179 | 2011-11-26 22:04:05 +0400 | [diff] [blame] | 907 | api.safi = safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 908 | SET_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 909 | api.nexthop_num = valid_nh_count; |
| 910 | api.nexthop = (struct in6_addr **)STREAM_DATA (bgp_nexthop_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 911 | SET_FLAG (api.message, ZAPI_MESSAGE_IFINDEX); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 912 | api.ifindex_num = valid_nh_count; |
| 913 | api.ifindex = (ifindex_t *)STREAM_DATA (bgp_ifindices_buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 914 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 915 | api.metric = info->attr->med; |
| 916 | |
Roman Hoog Antink | 6184c39 | 2014-03-17 14:01:42 +0100 | [diff] [blame] | 917 | distance = ipv6_bgp_distance_apply (p, info, bgp); |
| 918 | |
| 919 | if (distance) |
| 920 | { |
| 921 | SET_FLAG (api.message, ZAPI_MESSAGE_DISTANCE); |
| 922 | api.distance = distance; |
| 923 | } |
| 924 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 925 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 926 | { |
| 927 | char buf[2][INET6_ADDRSTRLEN]; |
| 928 | zlog_debug("Zebra send: IPv6 route add %s/%d nexthop %s metric %u", |
| 929 | inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])), |
| 930 | p->prefixlen, |
| 931 | inet_ntop(AF_INET6, nexthop, buf[1], sizeof(buf[1])), |
| 932 | api.metric); |
| 933 | } |
| 934 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 935 | zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, |
| 936 | (struct prefix_ipv6 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 937 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | void |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 941 | bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 942 | { |
| 943 | int flags; |
| 944 | struct peer *peer; |
| 945 | |
| 946 | if (zclient->sock < 0) |
| 947 | return; |
| 948 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 949 | if (! vrf_bitmap_check (zclient->redist[ZEBRA_ROUTE_BGP], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 950 | return; |
| 951 | |
| 952 | peer = info->peer; |
| 953 | flags = 0; |
| 954 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 955 | if (peer->sort == BGP_PEER_IBGP) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 956 | { |
| 957 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 958 | SET_FLAG (flags, ZEBRA_FLAG_IBGP); |
| 959 | } |
| 960 | |
Jorge Boncompte [DTI2] | 6d85b15 | 2012-05-07 16:52:54 +0000 | [diff] [blame] | 961 | if ((peer->sort == BGP_PEER_EBGP && peer->ttl != 1) |
hasso | 6ffd207 | 2005-02-02 14:50:11 +0000 | [diff] [blame] | 962 | || CHECK_FLAG (peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 963 | SET_FLAG (flags, ZEBRA_FLAG_INTERNAL); |
| 964 | |
| 965 | if (p->family == AF_INET) |
| 966 | { |
| 967 | struct zapi_ipv4 api; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 968 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 969 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 970 | api.flags = flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 971 | |
| 972 | api.type = ZEBRA_ROUTE_BGP; |
| 973 | api.message = 0; |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 974 | api.safi = safi; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 975 | api.nexthop_num = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 976 | api.ifindex_num = 0; |
| 977 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 978 | api.metric = info->attr->med; |
| 979 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 980 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 981 | { |
| 982 | char buf[2][INET_ADDRSTRLEN]; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 983 | zlog_debug("Zebra send: IPv4 route delete %s/%d metric %u", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 984 | inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])), |
| 985 | p->prefixlen, |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 986 | api.metric); |
| 987 | } |
| 988 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 989 | zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, |
| 990 | (struct prefix_ipv4 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 991 | } |
Lou Berger | 205e674 | 2016-01-12 13:42:11 -0500 | [diff] [blame] | 992 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 993 | /* We have to think about a IPv6 link-local address curse. */ |
| 994 | if (p->family == AF_INET6) |
| 995 | { |
| 996 | struct zapi_ipv6 api; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 997 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 998 | api.vrf_id = VRF_DEFAULT; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 999 | api.flags = flags; |
| 1000 | api.type = ZEBRA_ROUTE_BGP; |
| 1001 | api.message = 0; |
G.Balaji | c7ec179 | 2011-11-26 22:04:05 +0400 | [diff] [blame] | 1002 | api.safi = safi; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 1003 | api.nexthop_num = 0; |
| 1004 | api.ifindex_num = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1005 | SET_FLAG (api.message, ZAPI_MESSAGE_METRIC); |
| 1006 | api.metric = info->attr->med; |
| 1007 | |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1008 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 1009 | { |
| 1010 | char buf[2][INET6_ADDRSTRLEN]; |
Paul Jakma | 64e0ac2 | 2015-11-18 16:00:54 +0000 | [diff] [blame] | 1011 | zlog_debug("Zebra send: IPv6 route delete %s/%d metric %u", |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1012 | inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])), |
| 1013 | p->prefixlen, |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1014 | api.metric); |
| 1015 | } |
| 1016 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1017 | zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, |
| 1018 | (struct prefix_ipv6 *) p, &api); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1019 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1020 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1021 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1022 | /* Other routes redistribution into BGP. */ |
| 1023 | int |
| 1024 | bgp_redistribute_set (struct bgp *bgp, afi_t afi, int type) |
| 1025 | { |
| 1026 | /* Set flag to BGP instance. */ |
| 1027 | bgp->redist[afi][type] = 1; |
| 1028 | |
| 1029 | /* Return if already redistribute flag is set. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1030 | if (vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | return CMD_WARNING; |
| 1032 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1033 | vrf_bitmap_set (zclient->redist[type], VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* Return if zebra connection is not established. */ |
| 1036 | if (zclient->sock < 0) |
| 1037 | return CMD_WARNING; |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 1040 | zlog_debug("Zebra send: redistribute add %s", zebra_route_string(type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1041 | |
| 1042 | /* Send distribute add message to zebra. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1043 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, type, VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1044 | |
| 1045 | return CMD_SUCCESS; |
| 1046 | } |
| 1047 | |
| 1048 | /* Redistribute with route-map specification. */ |
| 1049 | int |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1050 | bgp_redistribute_rmap_set (struct bgp *bgp, afi_t afi, int type, |
| 1051 | const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1052 | { |
| 1053 | if (bgp->rmap[afi][type].name |
| 1054 | && (strcmp (bgp->rmap[afi][type].name, name) == 0)) |
| 1055 | return 0; |
| 1056 | |
| 1057 | if (bgp->rmap[afi][type].name) |
| 1058 | free (bgp->rmap[afi][type].name); |
| 1059 | bgp->rmap[afi][type].name = strdup (name); |
| 1060 | bgp->rmap[afi][type].map = route_map_lookup_by_name (name); |
| 1061 | |
| 1062 | return 1; |
| 1063 | } |
| 1064 | |
| 1065 | /* Redistribute with metric specification. */ |
| 1066 | int |
| 1067 | bgp_redistribute_metric_set (struct bgp *bgp, afi_t afi, int type, |
| 1068 | u_int32_t metric) |
| 1069 | { |
| 1070 | if (bgp->redist_metric_flag[afi][type] |
| 1071 | && bgp->redist_metric[afi][type] == metric) |
| 1072 | return 0; |
| 1073 | |
| 1074 | bgp->redist_metric_flag[afi][type] = 1; |
| 1075 | bgp->redist_metric[afi][type] = metric; |
| 1076 | |
| 1077 | return 1; |
| 1078 | } |
| 1079 | |
| 1080 | /* Unset redistribution. */ |
| 1081 | int |
| 1082 | bgp_redistribute_unset (struct bgp *bgp, afi_t afi, int type) |
| 1083 | { |
| 1084 | /* Unset flag from BGP instance. */ |
| 1085 | bgp->redist[afi][type] = 0; |
| 1086 | |
| 1087 | /* Unset route-map. */ |
| 1088 | if (bgp->rmap[afi][type].name) |
| 1089 | free (bgp->rmap[afi][type].name); |
| 1090 | bgp->rmap[afi][type].name = NULL; |
| 1091 | bgp->rmap[afi][type].map = NULL; |
| 1092 | |
| 1093 | /* Unset metric. */ |
| 1094 | bgp->redist_metric_flag[afi][type] = 0; |
| 1095 | bgp->redist_metric[afi][type] = 0; |
| 1096 | |
| 1097 | /* Return if zebra connection is disabled. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1098 | if (! vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1099 | return CMD_WARNING; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1100 | vrf_bitmap_unset (zclient->redist[type], VRF_DEFAULT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (bgp->redist[AFI_IP][type] == 0 |
| 1103 | && bgp->redist[AFI_IP6][type] == 0 |
| 1104 | && zclient->sock >= 0) |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1105 | { |
| 1106 | /* Send distribute delete message to zebra. */ |
| 1107 | if (BGP_DEBUG(zebra, ZEBRA)) |
| 1108 | zlog_debug("Zebra send: redistribute delete %s", |
| 1109 | zebra_route_string(type)); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1110 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_DELETE, zclient, type, |
| 1111 | VRF_DEFAULT); |
Andrew J. Schorr | a39275d | 2006-11-30 16:36:57 +0000 | [diff] [blame] | 1112 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1113 | |
| 1114 | /* Withdraw redistributed routes from current BGP's routing table. */ |
| 1115 | bgp_redistribute_withdraw (bgp, afi, type); |
| 1116 | |
| 1117 | return CMD_SUCCESS; |
| 1118 | } |
| 1119 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1120 | void |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1121 | bgp_zclient_reset (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1122 | { |
| 1123 | zclient_reset (zclient); |
| 1124 | } |
| 1125 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1126 | static void |
| 1127 | bgp_zebra_connected (struct zclient *zclient) |
| 1128 | { |
| 1129 | zclient_send_requests (zclient, VRF_DEFAULT); |
| 1130 | } |
| 1131 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1132 | void |
Donald Sharp | 7125293 | 2015-09-24 09:25:19 -0400 | [diff] [blame] | 1133 | bgp_zebra_init (struct thread_master *master) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1134 | { |
| 1135 | /* Set default values. */ |
Donald Sharp | 7125293 | 2015-09-24 09:25:19 -0400 | [diff] [blame] | 1136 | zclient = zclient_new (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1137 | zclient_init (zclient, ZEBRA_ROUTE_BGP); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1138 | zclient->zebra_connected = bgp_zebra_connected; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1139 | zclient->router_id_update = bgp_router_id_update; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1140 | zclient->interface_add = bgp_interface_add; |
| 1141 | zclient->interface_delete = bgp_interface_delete; |
| 1142 | zclient->interface_address_add = bgp_interface_address_add; |
| 1143 | zclient->interface_address_delete = bgp_interface_address_delete; |
| 1144 | zclient->ipv4_route_add = zebra_read_ipv4; |
| 1145 | zclient->ipv4_route_delete = zebra_read_ipv4; |
| 1146 | zclient->interface_up = bgp_interface_up; |
| 1147 | zclient->interface_down = bgp_interface_down; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1148 | zclient->ipv6_route_add = zebra_read_ipv6; |
| 1149 | zclient->ipv6_route_delete = zebra_read_ipv6; |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1150 | zclient->nexthop_update = bgp_read_nexthop_update; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1151 | |
Josh Bailey | 8196f13 | 2011-07-20 20:47:07 -0700 | [diff] [blame] | 1152 | bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE); |
Ayan Banerjee | b8d1f71 | 2015-11-09 20:14:54 -0500 | [diff] [blame] | 1153 | bgp_ifindices_buf = stream_new(BGP_IFINDICES_BUF_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1154 | } |
Lou Berger | 82dd707 | 2016-01-12 13:41:57 -0500 | [diff] [blame] | 1155 | |
| 1156 | void |
| 1157 | bgp_zebra_destroy(void) |
| 1158 | { |
| 1159 | if (zclient == NULL) |
| 1160 | return; |
| 1161 | zclient_stop(zclient); |
| 1162 | zclient_free(zclient); |
| 1163 | zclient = NULL; |
| 1164 | } |