ifpfsync.c revision 319265
1/*
2 * Copyright (c) 2003 Ryan McBride. All rights reserved.
3 * Copyright (c) 2004 Max Laier. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/10/sbin/ifconfig/ifpfsync.c 319265 2017-05-30 22:45:01Z asomers $
27 */
28
29#include <sys/param.h>
30#include <sys/ioctl.h>
31#include <sys/socket.h>
32
33#include <net/if.h>
34#include <netinet/in.h>
35#include <net/pfvar.h>
36#include <net/if_pfsync.h>
37#include <net/route.h>
38#include <arpa/inet.h>
39
40#include <err.h>
41#include <netdb.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47#include "ifconfig.h"
48
49void setpfsync_syncdev(const char *, int, int, const struct afswtch *);
50void unsetpfsync_syncdev(const char *, int, int, const struct afswtch *);
51void setpfsync_syncpeer(const char *, int, int, const struct afswtch *);
52void unsetpfsync_syncpeer(const char *, int, int, const struct afswtch *);
53void setpfsync_syncpeer(const char *, int, int, const struct afswtch *);
54void setpfsync_maxupd(const char *, int, int, const struct afswtch *);
55void setpfsync_defer(const char *, int, int, const struct afswtch *);
56void pfsync_status(int);
57
58void
59setpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
60{
61	struct pfsyncreq preq;
62
63	bzero((char *)&preq, sizeof(struct pfsyncreq));
64	ifr.ifr_data = (caddr_t)&preq;
65
66	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
67		err(1, "SIOCGETPFSYNC");
68
69	strlcpy(preq.pfsyncr_syncdev, val, sizeof(preq.pfsyncr_syncdev));
70
71	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
72		err(1, "SIOCSETPFSYNC");
73}
74
75/* ARGSUSED */
76void
77unsetpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
78{
79	struct pfsyncreq preq;
80
81	bzero((char *)&preq, sizeof(struct pfsyncreq));
82	ifr.ifr_data = (caddr_t)&preq;
83
84	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
85		err(1, "SIOCGETPFSYNC");
86
87	bzero((char *)&preq.pfsyncr_syncdev, sizeof(preq.pfsyncr_syncdev));
88
89	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
90		err(1, "SIOCSETPFSYNC");
91}
92
93/* ARGSUSED */
94void
95setpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
96{
97	struct pfsyncreq preq;
98	struct addrinfo hints, *peerres;
99	int ecode;
100
101	bzero((char *)&preq, sizeof(struct pfsyncreq));
102	ifr.ifr_data = (caddr_t)&preq;
103
104	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
105		err(1, "SIOCGETPFSYNC");
106
107	memset(&hints, 0, sizeof(hints));
108	hints.ai_family = AF_INET;
109	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
110
111	if ((ecode = getaddrinfo(val, NULL, &hints, &peerres)) != 0)
112		errx(1, "error in parsing address string: %s",
113		    gai_strerror(ecode));
114
115	if (peerres->ai_addr->sa_family != AF_INET)
116		errx(1, "only IPv4 addresses supported for the syncpeer");
117
118	preq.pfsyncr_syncpeer.s_addr = ((struct sockaddr_in *)
119	    peerres->ai_addr)->sin_addr.s_addr;
120
121	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
122		err(1, "SIOCSETPFSYNC");
123	freeaddrinfo(peerres);
124}
125
126/* ARGSUSED */
127void
128unsetpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
129{
130	struct pfsyncreq preq;
131
132	bzero((char *)&preq, sizeof(struct pfsyncreq));
133	ifr.ifr_data = (caddr_t)&preq;
134
135	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
136		err(1, "SIOCGETPFSYNC");
137
138	preq.pfsyncr_syncpeer.s_addr = 0;
139
140	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
141		err(1, "SIOCSETPFSYNC");
142}
143
144/* ARGSUSED */
145void
146setpfsync_maxupd(const char *val, int d, int s, const struct afswtch *rafp)
147{
148	struct pfsyncreq preq;
149	int maxupdates;
150
151	maxupdates = atoi(val);
152	if ((maxupdates < 0) || (maxupdates > 255))
153		errx(1, "maxupd %s: out of range", val);
154
155	memset((char *)&preq, 0, sizeof(struct pfsyncreq));
156	ifr.ifr_data = (caddr_t)&preq;
157
158	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
159		err(1, "SIOCGETPFSYNC");
160
161	preq.pfsyncr_maxupdates = maxupdates;
162
163	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
164		err(1, "SIOCSETPFSYNC");
165}
166
167/* ARGSUSED */
168void
169setpfsync_defer(const char *val, int d, int s, const struct afswtch *rafp)
170{
171	struct pfsyncreq preq;
172
173	memset((char *)&preq, 0, sizeof(struct pfsyncreq));
174	ifr.ifr_data = (caddr_t)&preq;
175
176	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
177		err(1, "SIOCGETPFSYNC");
178
179	preq.pfsyncr_defer = d;
180	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
181		err(1, "SIOCSETPFSYNC");
182}
183
184void
185pfsync_status(int s)
186{
187	struct pfsyncreq preq;
188
189	bzero((char *)&preq, sizeof(struct pfsyncreq));
190	ifr.ifr_data = (caddr_t)&preq;
191
192	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
193		return;
194
195	if (preq.pfsyncr_syncdev[0] != '\0' ||
196	    preq.pfsyncr_syncpeer.s_addr != INADDR_PFSYNC_GROUP)
197			printf("\t");
198
199	if (preq.pfsyncr_syncdev[0] != '\0')
200		printf("pfsync: syncdev: %s ", preq.pfsyncr_syncdev);
201	if (preq.pfsyncr_syncpeer.s_addr != INADDR_PFSYNC_GROUP)
202		printf("syncpeer: %s ", inet_ntoa(preq.pfsyncr_syncpeer));
203
204	if (preq.pfsyncr_syncdev[0] != '\0' ||
205	    preq.pfsyncr_syncpeer.s_addr != INADDR_PFSYNC_GROUP) {
206		printf("maxupd: %d ", preq.pfsyncr_maxupdates);
207		printf("defer: %s\n", preq.pfsyncr_defer ? "on" : "off");
208	}
209}
210
211static struct cmd pfsync_cmds[] = {
212	DEF_CMD_ARG("syncdev",		setpfsync_syncdev),
213	DEF_CMD("-syncdev",	1,	unsetpfsync_syncdev),
214	DEF_CMD_ARG("syncif",		setpfsync_syncdev),
215	DEF_CMD("-syncif",	1,	unsetpfsync_syncdev),
216	DEF_CMD_ARG("syncpeer",		setpfsync_syncpeer),
217	DEF_CMD("-syncpeer",	1,	unsetpfsync_syncpeer),
218	DEF_CMD_ARG("maxupd",		setpfsync_maxupd),
219	DEF_CMD("defer",	1,	setpfsync_defer),
220	DEF_CMD("-defer",	0,	setpfsync_defer),
221};
222static struct afswtch af_pfsync = {
223	.af_name	= "af_pfsync",
224	.af_af		= AF_UNSPEC,
225	.af_other_status = pfsync_status,
226};
227
228static __constructor void
229pfsync_ctor(void)
230{
231	int i;
232
233	for (i = 0; i < nitems(pfsync_cmds);  i++)
234		cmd_register(&pfsync_cmds[i]);
235	af_register(&af_pfsync);
236}
237