1 |
|
|
/* $OpenBSD: pftable.c,v 1.10 2017/01/24 04:22:42 benno Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2004 Damien Miller <djm@openbsd.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 <sys/types.h> |
20 |
|
|
#include <sys/ioctl.h> |
21 |
|
|
#include <sys/socket.h> |
22 |
|
|
|
23 |
|
|
#include <netinet/in.h> |
24 |
|
|
#include <net/if.h> |
25 |
|
|
#include <net/pfvar.h> |
26 |
|
|
|
27 |
|
|
#include <stdlib.h> |
28 |
|
|
#include <string.h> |
29 |
|
|
#include <errno.h> |
30 |
|
|
#include <fcntl.h> |
31 |
|
|
|
32 |
|
|
#include "log.h" |
33 |
|
|
|
34 |
|
|
/* Namespace collision: these are defined in both bgpd.h and pfvar.h */ |
35 |
|
|
#undef v4 |
36 |
|
|
#undef v6 |
37 |
|
|
#undef addr8 |
38 |
|
|
#undef addr16 |
39 |
|
|
#undef addr32 |
40 |
|
|
|
41 |
|
|
#include "bgpd.h" |
42 |
|
|
|
43 |
|
|
static int devpf = -1; |
44 |
|
|
|
45 |
|
|
struct pf_table { |
46 |
|
|
LIST_ENTRY(pf_table) entry; |
47 |
|
|
char name[PFTABLE_LEN]; |
48 |
|
|
unsigned long what; |
49 |
|
|
struct pfr_addr *worklist; |
50 |
|
|
int naddrs; |
51 |
|
|
int nalloc; |
52 |
|
|
}; |
53 |
|
|
|
54 |
|
|
/* List of tables under management */ |
55 |
|
|
LIST_HEAD(, pf_table) tables = LIST_HEAD_INITIALIZER(tables); |
56 |
|
|
|
57 |
|
|
static int |
58 |
|
|
pftable_change(struct pf_table *pft) |
59 |
|
|
{ |
60 |
|
|
struct pfioc_table tio; |
61 |
|
|
int ret; |
62 |
|
|
|
63 |
|
|
if (pft->naddrs == 0 || pft->what == 0) |
64 |
|
|
return (0); |
65 |
|
|
|
66 |
|
|
if (devpf == -1 && ((devpf = open("/dev/pf", O_RDWR)) == -1)) |
67 |
|
|
fatal("open(/dev/pf)"); |
68 |
|
|
|
69 |
|
|
bzero(&tio, sizeof(tio)); |
70 |
|
|
strlcpy(tio.pfrio_table.pfrt_name, pft->name, |
71 |
|
|
sizeof(tio.pfrio_table.pfrt_name)); |
72 |
|
|
tio.pfrio_buffer = pft->worklist; |
73 |
|
|
tio.pfrio_esize = sizeof(*pft->worklist); |
74 |
|
|
tio.pfrio_size = pft->naddrs; |
75 |
|
|
|
76 |
|
|
ret = ioctl(devpf, pft->what, &tio); |
77 |
|
|
|
78 |
|
|
/* bad prefixes shouldn't cause us to die */ |
79 |
|
|
if (ret == -1) { |
80 |
|
|
if (errno == EINVAL) |
81 |
|
|
return (0); |
82 |
|
|
log_warn("pftable_change ioctl"); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
return (ret); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
static int |
89 |
|
|
pftable_clear(const char *name) |
90 |
|
|
{ |
91 |
|
|
struct pfioc_table tio; |
92 |
|
|
|
93 |
|
|
if (devpf == -1 && ((devpf = open("/dev/pf", O_RDWR)) == -1)) |
94 |
|
|
fatal("open(/dev/pf)"); |
95 |
|
|
|
96 |
|
|
bzero(&tio, sizeof(tio)); |
97 |
|
|
strlcpy(tio.pfrio_table.pfrt_name, name, |
98 |
|
|
sizeof(tio.pfrio_table.pfrt_name)); |
99 |
|
|
|
100 |
|
|
if (ioctl(devpf, DIOCRCLRADDRS, &tio) != 0) { |
101 |
|
|
log_warn("pftable_clear ioctl"); |
102 |
|
|
return (-1); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
return (0); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
int |
109 |
|
|
pftable_exists(const char *name) |
110 |
|
|
{ |
111 |
|
|
struct pfioc_table tio; |
112 |
|
|
struct pfr_astats dummy; |
113 |
|
|
|
114 |
|
|
if (devpf == -1 && ((devpf = open("/dev/pf", O_RDWR)) == -1)) |
115 |
|
|
fatal("open(/dev/pf)"); |
116 |
|
|
|
117 |
|
|
bzero(&tio, sizeof(tio)); |
118 |
|
|
strlcpy(tio.pfrio_table.pfrt_name, name, |
119 |
|
|
sizeof(tio.pfrio_table.pfrt_name)); |
120 |
|
|
tio.pfrio_buffer = &dummy; |
121 |
|
|
tio.pfrio_esize = sizeof(dummy); |
122 |
|
|
tio.pfrio_size = 1; |
123 |
|
|
|
124 |
|
|
if (ioctl(devpf, DIOCRGETASTATS, &tio) != 0) |
125 |
|
|
return (-1); |
126 |
|
|
|
127 |
|
|
return (0); |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
int |
131 |
|
|
pftable_add(const char *name) |
132 |
|
|
{ |
133 |
|
|
struct pf_table *pft; |
134 |
|
|
|
135 |
|
|
/* Ignore duplicates */ |
136 |
|
|
LIST_FOREACH(pft, &tables, entry) |
137 |
|
|
if (strcmp(pft->name, name) == 0) |
138 |
|
|
return (0); |
139 |
|
|
|
140 |
|
|
if ((pft = malloc(sizeof(*pft))) == NULL) { |
141 |
|
|
log_warn("pftable malloc"); |
142 |
|
|
return (-1); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
bzero(pft, sizeof(*pft)); |
146 |
|
|
if (strlcpy(pft->name, name, sizeof(pft->name)) >= sizeof(pft->name)) { |
147 |
|
|
log_warn("pf_table name too long"); |
148 |
|
|
free(pft); |
149 |
|
|
return (-1); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
LIST_INSERT_HEAD(&tables, pft, entry); |
153 |
|
|
|
154 |
|
|
return (0); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
int |
158 |
|
|
pftable_clear_all(void) |
159 |
|
|
{ |
160 |
|
|
struct pf_table *pft; |
161 |
|
|
|
162 |
|
|
LIST_FOREACH(pft, &tables, entry) { |
163 |
|
|
if (pftable_clear(pft->name) != 0) |
164 |
|
|
return (-1); |
165 |
|
|
free(pft->worklist); |
166 |
|
|
pft->worklist = NULL; |
167 |
|
|
pft->nalloc = pft->naddrs = 0; |
168 |
|
|
pft->what = 0; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
return (0); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
static int |
175 |
|
|
pftable_add_work(const char *table, struct bgpd_addr *addr, |
176 |
|
|
u_int8_t len, int del) |
177 |
|
|
{ |
178 |
|
|
struct pf_table *pft; |
179 |
|
|
struct pfr_addr *pfa, *tmp; |
180 |
|
|
unsigned long what; |
181 |
|
|
|
182 |
|
|
if (*table == '\0' || len > 128) |
183 |
|
|
fatal("pftable_add_work: insane"); |
184 |
|
|
|
185 |
|
|
/* Find table */ |
186 |
|
|
LIST_FOREACH(pft, &tables, entry) |
187 |
|
|
if (strcmp(pft->name, table) == 0) |
188 |
|
|
break; |
189 |
|
|
|
190 |
|
|
if (pft == NULL) { |
191 |
|
|
log_warn("pf table %s not found", table); |
192 |
|
|
return (-1); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
/* Only one type of work on the list at a time */ |
196 |
|
|
what = del ? DIOCRDELADDRS : DIOCRADDADDRS; |
197 |
|
|
if (pft->naddrs != 0 && pft->what != what) |
198 |
|
|
fatal("attempt to mix pf table additions/deletions"); |
199 |
|
|
|
200 |
|
|
if (pft->nalloc <= pft->naddrs) |
201 |
|
|
pft->nalloc = pft->nalloc == 0 ? 1 : pft->nalloc * 2; |
202 |
|
|
tmp = reallocarray(pft->worklist, pft->nalloc, sizeof(*tmp)); |
203 |
|
|
if (tmp == NULL) { |
204 |
|
|
if (pft->worklist != NULL) { |
205 |
|
|
log_warn("pftable_add_work: malloc"); |
206 |
|
|
free(pft->worklist); |
207 |
|
|
pft->worklist = NULL; |
208 |
|
|
} |
209 |
|
|
pft->nalloc = pft->naddrs = 0; |
210 |
|
|
pft->what = 0; |
211 |
|
|
return (-1); |
212 |
|
|
} |
213 |
|
|
pft->worklist = tmp; |
214 |
|
|
pfa = &pft->worklist[pft->naddrs]; |
215 |
|
|
|
216 |
|
|
bzero(pfa, sizeof(*pfa)); |
217 |
|
|
memcpy(&pfa->pfra_u, &addr->ba, (len + 7U) / 8); |
218 |
|
|
pfa->pfra_af = aid2af(addr->aid); |
219 |
|
|
pfa->pfra_net = len; |
220 |
|
|
|
221 |
|
|
pft->naddrs++; |
222 |
|
|
pft->what = what; |
223 |
|
|
|
224 |
|
|
/* Don't let the list grow too large */ |
225 |
|
|
if (pft->naddrs >= 1024) |
226 |
|
|
pftable_commit(); |
227 |
|
|
|
228 |
|
|
return (0); |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
/* imsg handlers */ |
232 |
|
|
int |
233 |
|
|
pftable_addr_add(struct pftable_msg *m) |
234 |
|
|
{ |
235 |
|
|
return (pftable_add_work(m->pftable, &m->addr, m->len, 0)); |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
int |
239 |
|
|
pftable_addr_remove(struct pftable_msg *m) |
240 |
|
|
{ |
241 |
|
|
return (pftable_add_work(m->pftable, &m->addr, m->len, 1)); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
int |
245 |
|
|
pftable_commit(void) |
246 |
|
|
{ |
247 |
|
|
struct pf_table *pft; |
248 |
|
|
int ret = 0; |
249 |
|
|
|
250 |
|
|
LIST_FOREACH(pft, &tables, entry) { |
251 |
|
|
if (pft->what != 0 && pftable_change(pft) != 0) |
252 |
|
|
ret = -1; |
253 |
|
|
free(pft->worklist); |
254 |
|
|
pft->worklist = NULL; |
255 |
|
|
pft->nalloc = pft->naddrs = 0; |
256 |
|
|
pft->what = 0; |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
return (ret); |
260 |
|
|
} |
261 |
|
|
|