1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/* $OpenBSD: pftable.c,v 1.5 2005/07/01 09:19:24 claudio Exp $ */
/*
* Copyright (c) 2004 Damien Miller <djm@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/pfvar.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
/* Namespace collision: these are defined in both bgpd.h and pfvar.h */
#undef v4
#undef v6
#undef addr8
#undef addr16
#undef addr32
#include "bgpd.h"
static int devpf = -1;
struct pf_table {
LIST_ENTRY(pf_table) entry;
char name[PFTABLE_LEN];
unsigned long what;
struct pfr_addr *worklist;
int naddrs;
int nalloc;
};
/* List of tables under management */
LIST_HEAD(, pf_table) tables = LIST_HEAD_INITIALIZER(tables);
static int
pftable_change(struct pf_table *pft)
{
struct pfioc_table tio;
int ret;
if (pft->naddrs == 0 || pft->what == 0)
return (0);
if (devpf == -1 && ((devpf = open("/dev/pf", O_RDWR)) == -1))
fatal("open(/dev/pf)");
bzero(&tio, sizeof(tio));
strlcpy(tio.pfrio_table.pfrt_name, pft->name,
sizeof(tio.pfrio_table.pfrt_name));
tio.pfrio_buffer = pft->worklist;
tio.pfrio_esize = sizeof(*pft->worklist);
tio.pfrio_size = pft->naddrs;
ret = ioctl(devpf, pft->what, &tio);
/* bad prefixes shouldn't cause us to die */
if (ret == -1) {
if (errno == EINVAL)
return (0);
log_warn("pftable_change ioctl");
}
return (ret);
}
static int
pftable_clear(const char *name)
{
struct pfioc_table tio;
if (devpf == -1 && ((devpf = open("/dev/pf", O_RDWR)) == -1))
fatal("open(/dev/pf)");
bzero(&tio, sizeof(tio));
strlcpy(tio.pfrio_table.pfrt_name, name,
sizeof(tio.pfrio_table.pfrt_name));
if (ioctl(devpf, DIOCRCLRADDRS, &tio) != 0) {
log_warn("pftable_clear ioctl");
return (-1);
}
return (0);
}
int
pftable_exists(const char *name)
{
struct pfioc_table tio;
struct pfr_astats dummy;
if (devpf == -1 && ((devpf = open("/dev/pf", O_RDWR)) == -1))
fatal("open(/dev/pf)");
bzero(&tio, sizeof(tio));
strlcpy(tio.pfrio_table.pfrt_name, name,
sizeof(tio.pfrio_table.pfrt_name));
tio.pfrio_buffer = &dummy;
tio.pfrio_esize = sizeof(dummy);
tio.pfrio_size = 1;
if (ioctl(devpf, DIOCRGETASTATS, &tio) != 0)
return (-1);
return (0);
}
int
pftable_add(const char *name)
{
struct pf_table *pft;
/* Ignore duplicates */
LIST_FOREACH(pft, &tables, entry)
if (strcmp(pft->name, name) == 0)
return (0);
if ((pft = malloc(sizeof(*pft))) == NULL) {
log_warn("pftable malloc");
return (-1);
}
bzero(pft, sizeof(*pft));
if (strlcpy(pft->name, name, sizeof(pft->name)) >= sizeof(pft->name)) {
log_warn("pf_table name too long");
free(pft);
return (-1);
}
LIST_INSERT_HEAD(&tables, pft, entry);
return (0);
}
int
pftable_clear_all(void)
{
struct pf_table *pft;
LIST_FOREACH(pft, &tables, entry) {
if (pftable_clear(pft->name) != 0)
return (-1);
if (pft->worklist != NULL) {
free(pft->worklist);
pft->worklist = NULL;
}
pft->nalloc = pft->naddrs = 0;
pft->what = 0;
}
return (0);
}
static int
pftable_add_work(const char *table, struct bgpd_addr *addr,
u_int8_t len, int del)
{
struct pf_table *pft;
struct pfr_addr *pfa, *tmp;
unsigned long what;
if (*table == '\0' || len > 128)
fatal("pftable_add_work: insane");
/* Find table */
LIST_FOREACH(pft, &tables, entry)
if (strcmp(pft->name, table) == 0)
break;
if (pft == NULL) {
log_warn("pf table %s not found", table);
return (-1);
}
/* Only one type of work on the list at a time */
what = del ? DIOCRDELADDRS : DIOCRADDADDRS;
if (pft->naddrs != 0 && pft->what != what)
fatal("attempt to mix pf table additions/deletions");
if (pft->nalloc <= pft->naddrs)
pft->nalloc = pft->nalloc == 0 ? 1 : pft->nalloc * 2;
tmp = realloc(pft->worklist, sizeof(*tmp) * pft->nalloc);
if (tmp == NULL) {
if (pft->worklist != NULL) {
log_warn("pftable_add_work: malloc");
free(pft->worklist);
pft->worklist = NULL;
}
pft->nalloc = pft->naddrs = 0;
pft->what = 0;
return (-1);
}
pft->worklist = tmp;
pfa = &pft->worklist[pft->naddrs];
bzero(pfa, sizeof(*pfa));
memcpy(&pfa->pfra_u, &addr->ba, (len + 7U) / 8);
pfa->pfra_af = addr->af;
pfa->pfra_net = len;
pft->naddrs++;
pft->what = what;
/* Don't let the list grow too large */
if (pft->naddrs >= 1024)
pftable_commit();
return (0);
}
/* imsg handlers */
int
pftable_addr_add(struct pftable_msg *m)
{
return (pftable_add_work(m->pftable, &m->addr, m->len, 0));
}
int
pftable_addr_remove(struct pftable_msg *m)
{
return (pftable_add_work(m->pftable, &m->addr, m->len, 1));
}
int
pftable_commit(void)
{
struct pf_table *pft;
int ret = 0;
LIST_FOREACH(pft, &tables, entry) {
if (pft->what != 0 && pftable_change(pft) != 0)
ret = -1;
if (pft->worklist != NULL)
free(pft->worklist);
pft->worklist = NULL;
pft->nalloc = pft->naddrs = 0;
pft->what = 0;
}
return (ret);
}
|