-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpfsync.c
281 lines (243 loc) · 6.39 KB
/
pfsync.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (c) 2004 Chris Cappuccio <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/pfvar.h>
#include <netinet/ip_ipsp.h>
#include <net/if_pfsync.h>
#include "externs.h"
#define PFSYNC_MAXUPDATES 128
int
intsyncdev(int argc, char **argv, ...)
{
struct ifreq ifr;
struct pfsyncreq preq;
int set;
va_list ap;
char *ifname;
int ifs;
va_start(ap, argv);
ifname = va_arg(ap, char *);
ifs = va_arg(ap, int);
va_end(ap);
if (NO_ARG(argv[0])) {
set = 0;
argc--;
argv++;
} else
set = 1;
argc--;
argv++;
if ((!set && argc > 1) || (set && argc != 1)) {
printf("%% syncdev <if>\n");
printf("%% no syncdev [if]\n");
return (0);
}
if (!isprefix("pfsync", ifname)) {
printf("%% syncdev is only for pfsync devices\n");
return 0;
}
bzero((char *) &preq, sizeof(struct pfsyncreq));
ifr.ifr_data = (caddr_t) & preq;
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(ifs, SIOCGETPFSYNC, (caddr_t) & ifr) == -1) {
printf("%% intsyncdev: SIOCGETPFSYNC: %s\n", strerror(errno));
return (0);
}
if (argv[0])
if (!is_valid_ifname(argv[0])) {
printf("%% Interface not found: %s\n", argv[0]);
return (0);
}
if (set) {
strlcpy(preq.pfsyncr_syncdev, argv[0],
sizeof(preq.pfsyncr_syncdev));
set_ifflag(ifs, ifname, IFF_UP);
} else
bzero((char *) &preq.pfsyncr_syncdev,
sizeof(preq.pfsyncr_syncdev));
if (ioctl(ifs, SIOCSETPFSYNC, (caddr_t) & ifr) == -1) {
if (errno == ENOBUFS)
printf("%% Invalid synchronization interface: %s\n",
argv[0]);
else
printf("%% intsyncdev: SIOCSETPFSYNC: %s\n",
strerror(errno));
}
return (0);
}
int
intsyncpeer(int argc, char **argv, ...)
{
struct ifreq ifr;
struct pfsyncreq preq;
struct addrinfo hints, *peerres;
int set, ecode;
va_list ap;
char *ifname;
int ifs;
va_start(ap, argv);
ifname = va_arg(ap, char *);
ifs = va_arg(ap, int);
va_end(ap);
if (NO_ARG(argv[0])) {
set = 0;
argc--;
argv++;
} else
set = 1;
argc--;
argv++;
if (!isprefix("pfsync", ifname)) {
printf("%% syncpeer is only for pfsync devices\n");
return 0;
}
if ((!set && argc > 1) || (set && argc != 1)) {
printf("%% syncpeer <IPv4 peer address>\n");
printf("%% no syncpeer [IPv4 peer address]\n");
return (0);
}
bzero(&preq, sizeof(struct pfsyncreq));
ifr.ifr_data = (caddr_t) &preq;
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(ifs, SIOCGETPFSYNC, (caddr_t)&ifr) == -1) {
printf("%% intsyncpeer: SIOCGETPFSYNC: %s\n",
strerror(errno));
return(0);
}
if (set) {
bzero(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
if ((ecode = getaddrinfo(argv[0], NULL, &hints, &peerres)) != 0)
{
printf("%% error in parsing address string: %s\n",
gai_strerror(ecode));
return 0;
}
if (peerres->ai_addr->sa_family != AF_INET) {
printf("%% only IPv4 allowed for syncpeer\n");
freeaddrinfo(peerres);
return 0;
}
preq.pfsyncr_syncpeer.s_addr = ((struct sockaddr_in *)
peerres->ai_addr)->sin_addr.s_addr;
} else {
preq.pfsyncr_syncpeer.s_addr = 0;
}
if (ioctl(ifs, SIOCSETPFSYNC, (caddr_t)&ifr) == -1) {
printf("%% intsyncpeer: SIOCSETPFSYNC: %s\n",
strerror(errno));
}
if (set)
freeaddrinfo(peerres);
return 0;
}
int
intmaxupd(int argc, char **argv, ...)
{
struct ifreq ifr;
struct pfsyncreq preq;
u_int32_t val;
int set;
const char *errmsg = NULL;
va_list ap;
char *ifname;
int ifs;
va_start(ap, argv);
ifname = va_arg(ap, char *);
ifs = va_arg(ap, int);
va_end(ap);
if (NO_ARG(argv[0])) {
set = 0;
argc--;
argv++;
} else
set = 1;
argc--;
argv++;
if ((!set && argc > 2) || (set && (argc < 1 || argc > 2)) ||
(set && argc == 2 && !isprefix(argv[1], "defer"))) {
printf("%% maxupd <max pfsync updates> [defer]\n");
printf("%% no maxupd [max pfsync updates] [defer]\n");
return (0);
}
bzero((char *) &preq, sizeof(struct pfsyncreq));
ifr.ifr_data = (caddr_t) & preq;
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(ifs, SIOCGETPFSYNC, (caddr_t) & ifr) == -1) {
printf("%% intmaxupd: SIOCGETPFSYNC: %s\n", strerror(errno));
return (0);
}
if (set) {
errno = 0;
val = strtonum(argv[0], 0, INT_MAX, &errmsg);
if (errmsg) {
printf("%% maxupd value out of range %s: %s\n", argv[0],
errmsg);
return (0);
}
preq.pfsyncr_maxupdates = (int)val;
if (argc == 2)
preq.pfsyncr_defer = 1;
} else
preq.pfsyncr_maxupdates = PFSYNC_MAXUPDATES;
if (ioctl(ifs, SIOCSETPFSYNC, (caddr_t) & ifr) == -1) {
if (errno == EINVAL)
printf("%% maxupd value out of range\n");
else
printf("%% intmaxupd: SIOCSETPFSYNC: %s\n",
strerror(errno));
}
return (0);
}
int
conf_pfsync(FILE *output, int s, char *ifname)
{
struct ifreq ifr;
struct pfsyncreq preq;
bzero((char *) &preq, sizeof(struct pfsyncreq));
ifr.ifr_data = (caddr_t) & preq;
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGETPFSYNC, (caddr_t) & ifr) == -1)
return (0);
/* syncdev must be first */
if (preq.pfsyncr_syncdev[0] != '\0')
fprintf(output, " syncdev %s\n", preq.pfsyncr_syncdev);
/* syncpeer */
if (preq.pfsyncr_syncpeer.s_addr != htonl(INADDR_PFSYNC_GROUP))
fprintf(output, " syncpeer %s\n", inet_ntoa(
preq.pfsyncr_syncpeer));
if (preq.pfsyncr_maxupdates != PFSYNC_MAXUPDATES || preq.pfsyncr_defer) {
fprintf(output, " maxupd %i",
preq.pfsyncr_maxupdates);
if (preq.pfsyncr_defer)
fprintf(output, " defer");
fprintf(output, "\n");
}
return (0);
}