]>
git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_rd.c
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>
5 * This file is part of FRR.
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
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.
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
31 #include "bgpd/bgpd.h"
32 #include "bgpd/bgp_rd.h"
33 #include "bgpd/bgp_attr.h"
36 #include "bgpd/rfapi/rfapi_backend.h"
40 decode_rd_type (u_char
*pnt
)
44 v
= ((u_int16_t
) *pnt
++ << 8);
47 * VNC L2 stores LHI in lower byte, so omit it
49 if (v
!= RD_TYPE_VNC_ETH
)
50 v
|= (u_int16_t
) *pnt
;
51 #else /* duplicate code for clarity */
52 v
|= (u_int16_t
) *pnt
;
58 encode_rd_type (u_int16_t v
, u_char
*pnt
)
60 *((u_int16_t
*)pnt
) = htons(v
);
63 /* type == RD_TYPE_AS */
65 decode_rd_as (u_char
*pnt
, struct rd_as
*rd_as
)
67 rd_as
->as
= (u_int16_t
) *pnt
++ << 8;
68 rd_as
->as
|= (u_int16_t
) *pnt
++;
70 rd_as
->val
= ((u_int32_t
) *pnt
++ << 24);
71 rd_as
->val
|= ((u_int32_t
) *pnt
++ << 16);
72 rd_as
->val
|= ((u_int32_t
) *pnt
++ << 8);
73 rd_as
->val
|= (u_int32_t
) *pnt
;
76 /* type == RD_TYPE_AS4 */
78 decode_rd_as4 (u_char
*pnt
, struct rd_as
*rd_as
)
80 rd_as
->as
= (u_int32_t
) *pnt
++ << 24;
81 rd_as
->as
|= (u_int32_t
) *pnt
++ << 16;
82 rd_as
->as
|= (u_int32_t
) *pnt
++ << 8;
83 rd_as
->as
|= (u_int32_t
) *pnt
++;
85 rd_as
->val
= ((u_int16_t
) *pnt
++ << 8);
86 rd_as
->val
|= (u_int16_t
) *pnt
;
89 /* type == RD_TYPE_IP */
91 decode_rd_ip (u_char
*pnt
, struct rd_ip
*rd_ip
)
93 memcpy (&rd_ip
->ip
, pnt
, 4);
96 rd_ip
->val
= ((u_int16_t
) *pnt
++ << 8);
97 rd_ip
->val
|= (u_int16_t
) *pnt
;
101 /* type == RD_TYPE_VNC_ETH */
103 decode_rd_vnc_eth (u_char
*pnt
, struct rd_vnc_eth
*rd_vnc_eth
)
105 rd_vnc_eth
->type
= RD_TYPE_VNC_ETH
;
106 rd_vnc_eth
->local_nve_id
= pnt
[1];
107 memcpy (rd_vnc_eth
->macaddr
.octet
, pnt
+ 2, ETHER_ADDR_LEN
);
112 str2prefix_rd (const char *str
, struct prefix_rd
*prd
)
114 int ret
; /* ret of called functions */
115 int lret
; /* local ret, of this func */
118 struct stream
*s
= NULL
;
124 prd
->family
= AF_UNSPEC
;
128 p
= strchr (str
, ':');
132 if (! all_digit (p
+ 1))
135 half
= XMALLOC (MTYPE_TMP
, (p
- str
) + 1);
136 memcpy (half
, str
, (p
- str
));
137 half
[p
- str
] = '\0';
139 p2
= strchr (str
, '.');
143 unsigned long as_val
;
145 if (! all_digit (half
))
151 stream_putw (s
, RD_TYPE_AS4
);
152 stream_putl (s
, as_val
);
153 stream_putw (s
, atol (p
+ 1));
157 stream_putw (s
, RD_TYPE_AS
);
158 stream_putw (s
, as_val
);
159 stream_putl (s
, atol (p
+ 1));
164 ret
= inet_aton (half
, &addr
);
168 stream_putw (s
, RD_TYPE_IP
);
169 stream_put_in_addr (s
, &addr
);
170 stream_putw (s
, atol (p
+ 1));
172 memcpy (prd
->val
, s
->data
, 8);
179 XFREE(MTYPE_TMP
, half
);
184 prefix_rd2str (struct prefix_rd
*prd
, char *buf
, size_t size
)
191 if (size
< RD_ADDRSTRLEN
)
196 type
= decode_rd_type (pnt
);
198 if (type
== RD_TYPE_AS
)
200 decode_rd_as (pnt
+ 2, &rd_as
);
201 snprintf (buf
, size
, "%u:%d", rd_as
.as
, rd_as
.val
);
204 else if (type
== RD_TYPE_AS4
)
206 decode_rd_as4 (pnt
+ 2, &rd_as
);
207 snprintf (buf
, size
, "%u:%d", rd_as
.as
, rd_as
.val
);
210 else if (type
== RD_TYPE_IP
)
212 decode_rd_ip (pnt
+ 2, &rd_ip
);
213 snprintf (buf
, size
, "%s:%d", inet_ntoa (rd_ip
.ip
), rd_ip
.val
);
217 else if (type
== RD_TYPE_VNC_ETH
)
219 snprintf(buf
, size
, "LHI:%d, %02x:%02x:%02x:%02x:%02x:%02x",
221 *(pnt
+2), /* MAC[0] */