xref: /dragonfly/sys/netinet/ip_carp.h (revision c37c9ab3)
1 /*
2  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3  * Copyright (c) 2003 Ryan McBride. 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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 /*
27  * $FreeBSD: src/sys/netinet/ip_carp.h,v 1.3 2006/12/01 18:37:41 imp Exp $
28  * $OpenBSD: ip_carp.h,v 1.8 2004/07/29 22:12:15 mcbride Exp $
29  * $DragonFly: src/sys/netinet/ip_carp.h,v 1.1 2007/08/16 20:03:57 dillon Exp $
30  */
31 
32 
33 #ifndef _IP_CARP_H
34 #define	_IP_CARP_H
35 
36 #include <sys/ioccom.h>
37 
38 /*
39  * The CARP header layout is as follows:
40  *
41  *     0                   1                   2                   3
42  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
43  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44  *    |Version| Type  | VirtualHostID |    AdvSkew    |    Auth Len   |
45  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46  *    |   Reserved    |     AdvBase   |          Checksum             |
47  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48  *    |                         Counter (1)                           |
49  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50  *    |                         Counter (2)                           |
51  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52  *    |                        SHA-1 HMAC (1)                         |
53  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54  *    |                        SHA-1 HMAC (2)                         |
55  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56  *    |                        SHA-1 HMAC (3)                         |
57  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58  *    |                        SHA-1 HMAC (4)                         |
59  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60  *    |                        SHA-1 HMAC (5)                         |
61  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62  *
63  */
64 
65 struct carp_header {
66 #if BYTE_ORDER == LITTLE_ENDIAN
67 	uint8_t		carp_type:4,
68 			carp_version:4;
69 #endif
70 #if BYTE_ORDER == BIG_ENDIAN
71 	uint8_t		carp_version:4,
72 			carp_type:4;
73 #endif
74 	uint8_t		carp_vhid;	/* virtual host id */
75 	uint8_t		carp_advskew;	/* advertisement skew */
76 	uint8_t		carp_authlen;   /* size of counter+md, 32bit chunks */
77 	uint8_t		carp_pad1;	/* reserved */
78 	uint8_t		carp_advbase;	/* advertisement interval */
79 	uint16_t	carp_cksum;
80 	uint32_t	carp_counter[2];
81 	unsigned char	carp_md[20];	/* SHA1 HMAC */
82 } __packed;
83 
84 #ifdef CTASSERT
85 CTASSERT(sizeof(struct carp_header) == 36);
86 #endif
87 
88 #define	CARP_DFLTTL		255
89 
90 /* carp_version */
91 #define	CARP_VERSION		2
92 
93 /* carp_type */
94 #define	CARP_ADVERTISEMENT	0x01
95 
96 #define	CARP_KEY_LEN		20	/* a sha1 hash of a passphrase */
97 
98 /* carp_advbase */
99 #define	CARP_DFLTINTV		1
100 
101 /*
102  * Statistics.
103  */
104 struct carpstats {
105 	uint64_t	carps_ipackets;		/* total input packets, IPv4 */
106 	uint64_t	carps_ipackets6;	/* total input packets, IPv6 */
107 	uint64_t	carps_badif;		/* wrong interface */
108 	uint64_t	carps_badttl;		/* TTL is not CARP_DFLTTL */
109 	uint64_t	carps_hdrops;		/* packets shorter than hdr */
110 	uint64_t	carps_badsum;		/* bad checksum */
111 	uint64_t	carps_badver;		/* bad (incl unsupp) version */
112 	uint64_t	carps_badlen;		/* data length does not match */
113 	uint64_t	carps_badauth;		/* bad authentication */
114 	uint64_t	carps_badvhid;		/* bad VHID */
115 	uint64_t	carps_badaddrs;		/* bad address list */
116 
117 	uint64_t	carps_opackets;		/* total output packets, IPv4 */
118 	uint64_t	carps_opackets6;	/* total output packets, IPv6 */
119 	uint64_t	carps_onomem;		/* no memory for an mbuf */
120 	uint64_t	carps_ostates;		/* total state updates sent */
121 
122 	uint64_t	carps_preempt;		/* if enabled, preemptions */
123 };
124 
125 /*
126  * Configuration structure for SIOCSVH SIOCGVH
127  */
128 struct carpreq {
129 	int		carpr_state;
130 #define	CARP_STATES	"INIT", "BACKUP", "MASTER"
131 #define	CARP_MAXSTATE	2
132 	int		carpr_vhid;
133 	int		carpr_advskew;
134 	int		carpr_advbase;
135 	unsigned char	carpr_key[CARP_KEY_LEN];
136 };
137 #define	SIOCSVH		_IOWR('i', 245, struct ifreq)
138 #define	SIOCGVH		_IOWR('i', 246, struct ifreq)
139 
140 
141 struct ifcarpvhaddr {
142 	uint32_t		carpa_flags;	/* CARP_VHAF_ */
143 	struct sockaddr_in	carpa_addr;	/* carp address */
144 	struct sockaddr_in	carpa_baddr;	/* backing address */
145 };
146 #define CARP_VHAF_OWNER		0x1
147 #define CARP_VHAF_ONLIST	0x2
148 
149 #define CARPGDEVNAME		0	/* SIOCGDRVSPEC char[IFNAMSIZ] */
150 #define CARPGVHADDR		1	/* SIOCGDRVSPEC ifcarpvhaddr array */
151 
152 /*
153  * Names for CARP sysctl objects
154  */
155 #define	CARPCTL_ALLOW		1	/* accept incoming CARP packets */
156 #define	CARPCTL_PREEMPT		2	/* high-pri backup preemption mode */
157 #define	CARPCTL_LOG		3	/* log bad packets */
158 #define	CARPCTL_STATS		4	/* statistics (read-only) */
159 #define	CARPCTL_ARPBALANCE	5	/* balance arp responses */
160 #define	CARPCTL_SETROUTE	6	/* change routes */
161 #define	CARPCTL_MAXID		7
162 
163 #define	CARPCTL_NAMES { \
164 	{ 0, 0 }, \
165 	{ "allow", CTLTYPE_INT }, \
166 	{ "preempt", CTLTYPE_INT }, \
167 	{ "log", CTLTYPE_INT }, \
168 	{ "stats", CTLTYPE_STRUCT }, \
169 	{ "arpbalance", CTLTYPE_INT }, \
170 	{ "setroute", CTLTYPE_INT }, \
171 }
172 
173 #ifdef _KERNEL
174 void		 carp_group_demote_adj(struct ifnet *, int);
175 int		 carp_proto_input(struct mbuf **, int *, int);
176 int		 carp6_proto_input(struct mbuf **, int *, int);
177 void		 carp_proto_ctlinput(netmsg_t);
178 int		 carp_iamatch(const struct in_ifaddr *);
179 struct ifaddr	*carp_iamatch6(void *, struct in6_addr *);
180 void		*carp_macmatch6(void *, struct mbuf *, const struct in6_addr *);
181 struct mbuf	*carp_input(void *, struct mbuf *);
182 struct ifnet	*carp_parent(struct ifnet *);
183 #endif /* _KERNEL */
184 
185 #endif /* _IP_CARP_H */
186