]>
Commit | Line | Data |
---|---|---|
8557760c | 1 | /* BGP RD definitions for BGP-based VPNs (IP/EVPN) |
2 | * -- brought over from bgpd/bgp_mplsvpn.c | |
3 | * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org> | |
4 | * | |
5 | * This file is part of FRR. | |
6 | * | |
7 | * FRR is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * FRR is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with FRR; see the file COPYING. If not, write to the Free | |
19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | #include "command.h" | |
25 | #include "log.h" | |
26 | #include "prefix.h" | |
27 | #include "memory.h" | |
28 | #include "stream.h" | |
29 | #include "filter.h" | |
5d5ba018 | 30 | #include "frrstr.h" |
8557760c | 31 | |
23d0a753 DA |
32 | #include "lib/printfrr.h" |
33 | ||
8557760c | 34 | #include "bgpd/bgpd.h" |
35 | #include "bgpd/bgp_rd.h" | |
36 | #include "bgpd/bgp_attr.h" | |
37 | ||
49e5a4a0 | 38 | #ifdef ENABLE_BGP_VNC |
8557760c | 39 | #include "bgpd/rfapi/rfapi_backend.h" |
40 | #endif | |
41 | ||
1be1693e | 42 | uint16_t decode_rd_type(const uint8_t *pnt) |
8557760c | 43 | { |
d7c0a89a | 44 | uint16_t v; |
8557760c | 45 | |
d7c0a89a | 46 | v = ((uint16_t)*pnt++ << 8); |
49e5a4a0 | 47 | #ifdef ENABLE_BGP_VNC |
d62a17ae | 48 | /* |
49 | * VNC L2 stores LHI in lower byte, so omit it | |
50 | */ | |
51 | if (v != RD_TYPE_VNC_ETH) | |
d7c0a89a | 52 | v |= (uint16_t)*pnt; |
d62a17ae | 53 | #else /* duplicate code for clarity */ |
d7c0a89a | 54 | v |= (uint16_t)*pnt; |
8557760c | 55 | #endif |
d62a17ae | 56 | return v; |
8557760c | 57 | } |
58 | ||
d7c0a89a | 59 | void encode_rd_type(uint16_t v, uint8_t *pnt) |
8557760c | 60 | { |
d7c0a89a | 61 | *((uint16_t *)pnt) = htons(v); |
8557760c | 62 | } |
63 | ||
64 | /* type == RD_TYPE_AS */ | |
1be1693e | 65 | void decode_rd_as(const uint8_t *pnt, struct rd_as *rd_as) |
8557760c | 66 | { |
d7c0a89a QY |
67 | rd_as->as = (uint16_t)*pnt++ << 8; |
68 | rd_as->as |= (uint16_t)*pnt++; | |
937652c6 | 69 | ptr_get_be32(pnt, &rd_as->val); |
8557760c | 70 | } |
71 | ||
72 | /* type == RD_TYPE_AS4 */ | |
1be1693e | 73 | void decode_rd_as4(const uint8_t *pnt, struct rd_as *rd_as) |
8557760c | 74 | { |
937652c6 | 75 | pnt = ptr_get_be32(pnt, &rd_as->as); |
d7c0a89a QY |
76 | rd_as->val = ((uint16_t)*pnt++ << 8); |
77 | rd_as->val |= (uint16_t)*pnt; | |
8557760c | 78 | } |
79 | ||
80 | /* type == RD_TYPE_IP */ | |
5a1ae2c2 | 81 | void decode_rd_ip(const uint8_t *pnt, struct rd_ip *rd_ip) |
8557760c | 82 | { |
d62a17ae | 83 | memcpy(&rd_ip->ip, pnt, 4); |
84 | pnt += 4; | |
8557760c | 85 | |
d7c0a89a QY |
86 | rd_ip->val = ((uint16_t)*pnt++ << 8); |
87 | rd_ip->val |= (uint16_t)*pnt; | |
8557760c | 88 | } |
89 | ||
49e5a4a0 | 90 | #ifdef ENABLE_BGP_VNC |
8557760c | 91 | /* type == RD_TYPE_VNC_ETH */ |
5f040085 | 92 | void decode_rd_vnc_eth(const uint8_t *pnt, struct rd_vnc_eth *rd_vnc_eth) |
8557760c | 93 | { |
d62a17ae | 94 | rd_vnc_eth->type = RD_TYPE_VNC_ETH; |
95 | rd_vnc_eth->local_nve_id = pnt[1]; | |
28328ea9 | 96 | memcpy(rd_vnc_eth->macaddr.octet, pnt + 2, ETH_ALEN); |
8557760c | 97 | } |
98 | #endif | |
99 | ||
d62a17ae | 100 | int str2prefix_rd(const char *str, struct prefix_rd *prd) |
8557760c | 101 | { |
d62a17ae | 102 | int ret; /* ret of called functions */ |
103 | int lret; /* local ret, of this func */ | |
104 | char *p; | |
105 | char *p2; | |
106 | struct stream *s = NULL; | |
107 | char *half = NULL; | |
108 | struct in_addr addr; | |
109 | ||
110 | s = stream_new(8); | |
111 | ||
112 | prd->family = AF_UNSPEC; | |
113 | prd->prefixlen = 64; | |
114 | ||
115 | lret = 0; | |
116 | p = strchr(str, ':'); | |
117 | if (!p) | |
118 | goto out; | |
119 | ||
120 | if (!all_digit(p + 1)) | |
121 | goto out; | |
122 | ||
123 | half = XMALLOC(MTYPE_TMP, (p - str) + 1); | |
124 | memcpy(half, str, (p - str)); | |
125 | half[p - str] = '\0'; | |
126 | ||
127 | p2 = strchr(str, '.'); | |
128 | ||
129 | if (!p2) { | |
130 | unsigned long as_val; | |
131 | ||
132 | if (!all_digit(half)) | |
133 | goto out; | |
134 | ||
135 | as_val = atol(half); | |
136 | if (as_val > 0xffff) { | |
137 | stream_putw(s, RD_TYPE_AS4); | |
138 | stream_putl(s, as_val); | |
139 | stream_putw(s, atol(p + 1)); | |
140 | } else { | |
141 | stream_putw(s, RD_TYPE_AS); | |
142 | stream_putw(s, as_val); | |
143 | stream_putl(s, atol(p + 1)); | |
144 | } | |
145 | } else { | |
146 | ret = inet_aton(half, &addr); | |
147 | if (!ret) | |
148 | goto out; | |
149 | ||
150 | stream_putw(s, RD_TYPE_IP); | |
151 | stream_put_in_addr(s, &addr); | |
152 | stream_putw(s, atol(p + 1)); | |
153 | } | |
154 | memcpy(prd->val, s->data, 8); | |
155 | lret = 1; | |
8557760c | 156 | |
157 | out: | |
d62a17ae | 158 | if (s) |
159 | stream_free(s); | |
0a22ddfb | 160 | XFREE(MTYPE_TMP, half); |
d62a17ae | 161 | return lret; |
8557760c | 162 | } |
163 | ||
5f040085 | 164 | char *prefix_rd2str(const struct prefix_rd *prd, char *buf, size_t size) |
8557760c | 165 | { |
5f040085 | 166 | const uint8_t *pnt; |
d7c0a89a | 167 | uint16_t type; |
d62a17ae | 168 | struct rd_as rd_as; |
169 | struct rd_ip rd_ip; | |
170 | ||
02d32439 | 171 | assert(size >= RD_ADDRSTRLEN); |
d62a17ae | 172 | |
173 | pnt = prd->val; | |
174 | ||
175 | type = decode_rd_type(pnt); | |
176 | ||
177 | if (type == RD_TYPE_AS) { | |
178 | decode_rd_as(pnt + 2, &rd_as); | |
6cde4b45 | 179 | snprintf(buf, size, "%u:%u", rd_as.as, rd_as.val); |
d62a17ae | 180 | return buf; |
181 | } else if (type == RD_TYPE_AS4) { | |
182 | decode_rd_as4(pnt + 2, &rd_as); | |
6cde4b45 | 183 | snprintf(buf, size, "%u:%u", rd_as.as, rd_as.val); |
d62a17ae | 184 | return buf; |
185 | } else if (type == RD_TYPE_IP) { | |
186 | decode_rd_ip(pnt + 2, &rd_ip); | |
23d0a753 | 187 | snprintfrr(buf, size, "%pI4:%hu", &rd_ip.ip, rd_ip.val); |
d62a17ae | 188 | return buf; |
189 | } | |
49e5a4a0 | 190 | #ifdef ENABLE_BGP_VNC |
d62a17ae | 191 | else if (type == RD_TYPE_VNC_ETH) { |
192 | snprintf(buf, size, "LHI:%d, %02x:%02x:%02x:%02x:%02x:%02x", | |
193 | *(pnt + 1), /* LHI */ | |
194 | *(pnt + 2), /* MAC[0] */ | |
195 | *(pnt + 3), *(pnt + 4), *(pnt + 5), *(pnt + 6), | |
196 | *(pnt + 7)); | |
197 | ||
198 | return buf; | |
199 | } | |
8557760c | 200 | #endif |
02d32439 DS |
201 | |
202 | snprintf(buf, size, "Unknown Type: %d", type); | |
203 | return buf; | |
8557760c | 204 | } |
92708db6 | 205 | |
206 | void form_auto_rd(struct in_addr router_id, | |
207 | uint16_t rd_id, | |
208 | struct prefix_rd *prd) | |
209 | { | |
210 | char buf[100]; | |
211 | ||
212 | prd->family = AF_UNSPEC; | |
213 | prd->prefixlen = 64; | |
23d0a753 | 214 | snprintfrr(buf, sizeof(buf), "%pI4:%hu", &router_id, rd_id); |
24f8bd87 | 215 | (void)str2prefix_rd(buf, prd); |
92708db6 | 216 | } |