xref: /freebsd/sbin/ifconfig/ifpfsync.c (revision a97719482dd97b6690bed7276136b54af7f22a21)
1a9771948SGleb Smirnoff /*
2a9771948SGleb Smirnoff  * Copyright (c) 2003 Ryan McBride. All rights reserved.
3a9771948SGleb Smirnoff  * Copyright (c) 2004 Max Laier. All rights reserved.
4a9771948SGleb Smirnoff  *
5a9771948SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
6a9771948SGleb Smirnoff  * modification, are permitted provided that the following conditions
7a9771948SGleb Smirnoff  * are met:
8a9771948SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
9a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
10a9771948SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
11a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
12a9771948SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
13a9771948SGleb Smirnoff  *
14a9771948SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a9771948SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a9771948SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a9771948SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a9771948SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a9771948SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a9771948SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a9771948SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a9771948SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a9771948SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a9771948SGleb Smirnoff  * SUCH DAMAGE.
25a9771948SGleb Smirnoff  *
26a9771948SGleb Smirnoff  * $FreeBSD$
27a9771948SGleb Smirnoff  */
28a9771948SGleb Smirnoff 
29a9771948SGleb Smirnoff #include <sys/types.h>
30a9771948SGleb Smirnoff #include <sys/ioctl.h>
31a9771948SGleb Smirnoff #include <sys/socket.h>
32a9771948SGleb Smirnoff 
33a9771948SGleb Smirnoff #include <net/if.h>
34a9771948SGleb Smirnoff #include <netinet/in.h>
35a9771948SGleb Smirnoff #include <net/pfvar.h>
36a9771948SGleb Smirnoff #include <net/if_pfsync.h>
37a9771948SGleb Smirnoff #include <net/route.h>
38a9771948SGleb Smirnoff 
39a9771948SGleb Smirnoff #include <err.h>
40a9771948SGleb Smirnoff #include <stdio.h>
41a9771948SGleb Smirnoff #include <stdlib.h>
42a9771948SGleb Smirnoff #include <string.h>
43a9771948SGleb Smirnoff #include <unistd.h>
44a9771948SGleb Smirnoff 
45a9771948SGleb Smirnoff #include "ifconfig.h"
46a9771948SGleb Smirnoff 
47a9771948SGleb Smirnoff void setpfsync_syncif(const char *, int, int, const struct afswtch *rafp);
48a9771948SGleb Smirnoff void unsetpfsync_syncif(const char *, int, int, const struct afswtch *rafp);
49a9771948SGleb Smirnoff void setpfsync_maxupd(const char *, int, int, const struct afswtch *rafp);
50a9771948SGleb Smirnoff void pfsync_status(int, const struct rt_addrinfo *);
51a9771948SGleb Smirnoff 
52a9771948SGleb Smirnoff void
53a9771948SGleb Smirnoff setpfsync_syncif(const char *val, int d, int s, const struct afswtch *rafp)
54a9771948SGleb Smirnoff {
55a9771948SGleb Smirnoff 	struct pfsyncreq preq;
56a9771948SGleb Smirnoff 
57a9771948SGleb Smirnoff 	bzero((char *)&preq, sizeof(struct pfsyncreq));
58a9771948SGleb Smirnoff 	ifr.ifr_data = (caddr_t)&preq;
59a9771948SGleb Smirnoff 
60a9771948SGleb Smirnoff 	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
61a9771948SGleb Smirnoff 		err(1, "SIOCGETPFSYNC");
62a9771948SGleb Smirnoff 
63a9771948SGleb Smirnoff 	strlcpy(preq.pfsyncr_syncif, val, sizeof(preq.pfsyncr_syncif));
64a9771948SGleb Smirnoff 
65a9771948SGleb Smirnoff 	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
66a9771948SGleb Smirnoff 		err(1, "SIOCSETPFSYNC");
67a9771948SGleb Smirnoff }
68a9771948SGleb Smirnoff 
69a9771948SGleb Smirnoff void
70a9771948SGleb Smirnoff unsetpfsync_syncif(const char *val, int d, int s, const struct afswtch *rafp)
71a9771948SGleb Smirnoff {
72a9771948SGleb Smirnoff 	struct pfsyncreq preq;
73a9771948SGleb Smirnoff 
74a9771948SGleb Smirnoff 	bzero((char *)&preq, sizeof(struct pfsyncreq));
75a9771948SGleb Smirnoff 	ifr.ifr_data = (caddr_t)&preq;
76a9771948SGleb Smirnoff 
77a9771948SGleb Smirnoff 	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
78a9771948SGleb Smirnoff 		err(1, "SIOCGETPFSYNC");
79a9771948SGleb Smirnoff 
80a9771948SGleb Smirnoff 	bzero((char *)&preq.pfsyncr_syncif, sizeof(preq.pfsyncr_syncif));
81a9771948SGleb Smirnoff 
82a9771948SGleb Smirnoff 	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
83a9771948SGleb Smirnoff 		err(1, "SIOCSETPFSYNC");
84a9771948SGleb Smirnoff }
85a9771948SGleb Smirnoff 
86a9771948SGleb Smirnoff void
87a9771948SGleb Smirnoff setpfsync_maxupd(const char *val, int d, int s, const struct afswtch *rafp)
88a9771948SGleb Smirnoff {
89a9771948SGleb Smirnoff 	int maxupdates;
90a9771948SGleb Smirnoff 	struct pfsyncreq preq;
91a9771948SGleb Smirnoff 
92a9771948SGleb Smirnoff 	maxupdates = atoi(val);
93a9771948SGleb Smirnoff 
94a9771948SGleb Smirnoff 	memset((char *)&preq, 0, sizeof(struct pfsyncreq));
95a9771948SGleb Smirnoff 	ifr.ifr_data = (caddr_t)&preq;
96a9771948SGleb Smirnoff 
97a9771948SGleb Smirnoff 	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
98a9771948SGleb Smirnoff 		err(1, "SIOCGETPFSYNC");
99a9771948SGleb Smirnoff 
100a9771948SGleb Smirnoff 	preq.pfsyncr_maxupdates = maxupdates;
101a9771948SGleb Smirnoff 
102a9771948SGleb Smirnoff 	if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1)
103a9771948SGleb Smirnoff 		err(1, "SIOCSETPFSYNC");
104a9771948SGleb Smirnoff }
105a9771948SGleb Smirnoff 
106a9771948SGleb Smirnoff void
107a9771948SGleb Smirnoff pfsync_status(int s, const struct rt_addrinfo *info __unused)
108a9771948SGleb Smirnoff {
109a9771948SGleb Smirnoff 	struct pfsyncreq preq;
110a9771948SGleb Smirnoff 
111a9771948SGleb Smirnoff 	bzero((char *)&preq, sizeof(struct pfsyncreq));
112a9771948SGleb Smirnoff 	ifr.ifr_data = (caddr_t)&preq;
113a9771948SGleb Smirnoff 
114a9771948SGleb Smirnoff 	if (ioctl(s, SIOCGETPFSYNC, (caddr_t)&ifr) == -1)
115a9771948SGleb Smirnoff 		return;
116a9771948SGleb Smirnoff 
117a9771948SGleb Smirnoff 	if (preq.pfsyncr_syncif[0] != '\0') {
118a9771948SGleb Smirnoff 		printf("\tpfsync: syncif: %s maxupd: %d\n",
119a9771948SGleb Smirnoff 		    preq.pfsyncr_syncif, preq.pfsyncr_maxupdates);
120a9771948SGleb Smirnoff 	}
121a9771948SGleb Smirnoff }
122a9771948SGleb Smirnoff 
123a9771948SGleb Smirnoff static struct cmd pfsync_cmds[] = {
124a9771948SGleb Smirnoff 	DEF_CMD_ARG("syncif",		setpfsync_syncif),
125a9771948SGleb Smirnoff 	DEF_CMD_ARG("maxupd",		setpfsync_maxupd),
126a9771948SGleb Smirnoff 	DEF_CMD("-syncif",	1,	unsetpfsync_syncif),
127a9771948SGleb Smirnoff };
128a9771948SGleb Smirnoff static struct afswtch af_pfsync = {
129a9771948SGleb Smirnoff 	.af_name	= "af_pfsync",
130a9771948SGleb Smirnoff 	.af_af		= AF_UNSPEC,
131a9771948SGleb Smirnoff 	.af_status	= pfsync_status,
132a9771948SGleb Smirnoff };
133a9771948SGleb Smirnoff 
134a9771948SGleb Smirnoff static __constructor void
135a9771948SGleb Smirnoff pfsync_ctor(void)
136a9771948SGleb Smirnoff {
137a9771948SGleb Smirnoff #define	N(a)	(sizeof(a) / sizeof(a[0]))
138a9771948SGleb Smirnoff 	int i;
139a9771948SGleb Smirnoff 
140a9771948SGleb Smirnoff 	for (i = 0; i < N(pfsync_cmds);  i++)
141a9771948SGleb Smirnoff 		cmd_register(&pfsync_cmds[i]);
142a9771948SGleb Smirnoff 	af_register(&af_pfsync);
143a9771948SGleb Smirnoff #undef N
144a9771948SGleb Smirnoff }
145