1 /*	$OpenBSD: output-bird.c,v 1.19 2024/02/22 12:49:42 job Exp $ */
2 /*
3  * Copyright (c) 2019 Claudio Jeker <claudio@openbsd.org>
4  * Copyright (c) 2020 Robert Scheck <robert@fedoraproject.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <stdlib.h>
20 
21 #include "extern.h"
22 
23 int
output_bird1v4(FILE * out,struct vrp_tree * vrps,struct brk_tree * brks,struct vap_tree * vaps,struct vsp_tree * vsps,struct stats * st)24 output_bird1v4(FILE *out, struct vrp_tree *vrps, struct brk_tree *brks,
25     struct vap_tree *vaps, struct vsp_tree *vsps, struct stats *st)
26 {
27 	extern		const char *bird_tablename;
28 	struct vrp	*v;
29 
30 	if (outputheader(out, st) < 0)
31 		return -1;
32 
33 	if (fprintf(out, "\nroa table %s {\n", bird_tablename) < 0)
34 		return -1;
35 
36 	RB_FOREACH(v, vrp_tree, vrps) {
37 		char buf[64];
38 
39 		if (v->afi == AFI_IPV4) {
40 			ip_addr_print(&v->addr, v->afi, buf, sizeof(buf));
41 			if (fprintf(out, "\troa %s max %u as %u;\n", buf,
42 			    v->maxlength, v->asid) < 0)
43 				return -1;
44 		}
45 	}
46 
47 	if (fprintf(out, "}\n") < 0)
48 		return -1;
49 	return 0;
50 }
51 
52 int
output_bird1v6(FILE * out,struct vrp_tree * vrps,struct brk_tree * brks,struct vap_tree * vaps,struct vsp_tree * vsps,struct stats * st)53 output_bird1v6(FILE *out, struct vrp_tree *vrps, struct brk_tree *brks,
54     struct vap_tree *vaps, struct vsp_tree *vsps, struct stats *st)
55 {
56 	extern		const char *bird_tablename;
57 	struct vrp	*v;
58 
59 	if (outputheader(out, st) < 0)
60 		return -1;
61 
62 	if (fprintf(out, "\nroa table %s {\n", bird_tablename) < 0)
63 		return -1;
64 
65 	RB_FOREACH(v, vrp_tree, vrps) {
66 		char buf[64];
67 
68 		if (v->afi == AFI_IPV6) {
69 			ip_addr_print(&v->addr, v->afi, buf, sizeof(buf));
70 			if (fprintf(out, "\troa %s max %u as %u;\n", buf,
71 			    v->maxlength, v->asid) < 0)
72 				return -1;
73 		}
74 	}
75 
76 	if (fprintf(out, "}\n") < 0)
77 		return -1;
78 	return 0;
79 }
80 
81 int
output_bird2(FILE * out,struct vrp_tree * vrps,struct brk_tree * brks,struct vap_tree * vaps,struct vsp_tree * vsps,struct stats * st)82 output_bird2(FILE *out, struct vrp_tree *vrps, struct brk_tree *brks,
83     struct vap_tree *vaps, struct vsp_tree *vsps, struct stats *st)
84 {
85 	extern		const char *bird_tablename;
86 	struct vrp	*v;
87 	time_t		 now = get_current_time();
88 
89 	if (outputheader(out, st) < 0)
90 		return -1;
91 
92 	if (fprintf(out, "\ndefine force_roa_table_update = %lld;\n\n"
93 	    "roa4 table %s4;\nroa6 table %s6;\n\n"
94 	    "protocol static {\n\troa4 { table %s4; };\n\n",
95 	    (long long)now, bird_tablename, bird_tablename,
96 	    bird_tablename) < 0)
97 		return -1;
98 
99 	RB_FOREACH(v, vrp_tree, vrps) {
100 		char buf[64];
101 
102 		if (v->afi == AFI_IPV4) {
103 			ip_addr_print(&v->addr, v->afi, buf, sizeof(buf));
104 			if (fprintf(out, "\troute %s max %u as %u;\n", buf,
105 			    v->maxlength, v->asid) < 0)
106 				return -1;
107 		}
108 	}
109 
110 	if (fprintf(out, "}\n\nprotocol static {\n\troa6 { table %s6; };\n\n",
111 	    bird_tablename) < 0)
112 		return -1;
113 
114 	RB_FOREACH(v, vrp_tree, vrps) {
115 		char buf[64];
116 
117 		if (v->afi == AFI_IPV6) {
118 			ip_addr_print(&v->addr, v->afi, buf, sizeof(buf));
119 			if (fprintf(out, "\troute %s max %u as %u;\n", buf,
120 			    v->maxlength, v->asid) < 0)
121 				return -1;
122 		}
123 	}
124 
125 	if (fprintf(out, "}\n") < 0)
126 		return -1;
127 	return 0;
128 }
129