xref: /freebsd/sys/netinet/ip_carp.h (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*	$FreeBSD$	*/
2 /*	$OpenBSD: ip_carp.h,v 1.8 2004/07/29 22:12:15 mcbride Exp $	*/
3 
4 /*
5  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
6  * Copyright (c) 2003 Ryan McBride. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _IP_CARP_H
31 #define	_IP_CARP_H
32 
33 /*
34  * The CARP header layout is as follows:
35  *
36  *     0                   1                   2                   3
37  *     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
38  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  *    |Version| Type  | VirtualHostID |    AdvSkew    |    Auth Len   |
40  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41  *    |   Reserved    |     AdvBase   |          Checksum             |
42  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43  *    |                         Counter (1)                           |
44  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45  *    |                         Counter (2)                           |
46  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47  *    |                        SHA-1 HMAC (1)                         |
48  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49  *    |                        SHA-1 HMAC (2)                         |
50  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51  *    |                        SHA-1 HMAC (3)                         |
52  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53  *    |                        SHA-1 HMAC (4)                         |
54  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55  *    |                        SHA-1 HMAC (5)                         |
56  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57  *
58  */
59 
60 struct carp_header {
61 #if BYTE_ORDER == LITTLE_ENDIAN
62 	u_int8_t	carp_type:4,
63 			carp_version:4;
64 #endif
65 #if BYTE_ORDER == BIG_ENDIAN
66 	u_int8_t	carp_version:4,
67 			carp_type:4;
68 #endif
69 	u_int8_t	carp_vhid;	/* virtual host id */
70 	u_int8_t	carp_advskew;	/* advertisement skew */
71 	u_int8_t	carp_authlen;   /* size of counter+md, 32bit chunks */
72 	u_int8_t	carp_pad1;	/* reserved */
73 	u_int8_t	carp_advbase;	/* advertisement interval */
74 	u_int16_t	carp_cksum;
75 	u_int32_t	carp_counter[2];
76 	unsigned char	carp_md[20];	/* SHA1 HMAC */
77 } __packed;
78 
79 #ifdef CTASSERT
80 CTASSERT(sizeof(struct carp_header) == 36);
81 #endif
82 
83 #define	CARP_DFLTTL		255
84 
85 /* carp_version */
86 #define	CARP_VERSION		2
87 
88 /* carp_type */
89 #define	CARP_ADVERTISEMENT	0x01
90 
91 #define	CARP_KEY_LEN		20	/* a sha1 hash of a passphrase */
92 
93 /* carp_advbase */
94 #define	CARP_DFLTINTV		1
95 
96 /*
97  * Statistics.
98  */
99 struct carpstats {
100 	uint64_t	carps_ipackets;		/* total input packets, IPv4 */
101 	uint64_t	carps_ipackets6;	/* total input packets, IPv6 */
102 	uint64_t	carps_badif;		/* wrong interface */
103 	uint64_t	carps_badttl;		/* TTL is not CARP_DFLTTL */
104 	uint64_t	carps_hdrops;		/* packets shorter than hdr */
105 	uint64_t	carps_badsum;		/* bad checksum */
106 	uint64_t	carps_badver;		/* bad (incl unsupp) version */
107 	uint64_t	carps_badlen;		/* data length does not match */
108 	uint64_t	carps_badauth;		/* bad authentication */
109 	uint64_t	carps_badvhid;		/* bad VHID */
110 	uint64_t	carps_badaddrs;		/* bad address list */
111 
112 	uint64_t	carps_opackets;		/* total output packets, IPv4 */
113 	uint64_t	carps_opackets6;	/* total output packets, IPv6 */
114 	uint64_t	carps_onomem;		/* no memory for an mbuf */
115 	uint64_t	carps_ostates;		/* total state updates sent */
116 
117 	uint64_t	carps_preempt;		/* if enabled, preemptions */
118 };
119 
120 #ifdef _KERNEL
121 #define	CARPSTATS_ADD(name, val)	carpstats.name += (val)
122 #define	CARPSTATS_INC(name)		CARPSTATS_ADD(name, 1)
123 #endif
124 
125 /*
126  * Configuration structure for SIOCSVH SIOCGVH
127  */
128 struct carpreq {
129 	int		carpr_count;
130 	int		carpr_vhid;
131 #define	CARP_MAXVHID	255
132 	int		carpr_state;
133 #define	CARP_STATES	"INIT", "BACKUP", "MASTER"
134 #define	CARP_MAXSTATE	2
135 	int		carpr_advskew;
136 #define	CARP_MAXSKEW	240
137 	int		carpr_advbase;
138 	unsigned char	carpr_key[CARP_KEY_LEN];
139 };
140 #define	SIOCSVH	_IOWR('i', 245, struct ifreq)
141 #define	SIOCGVH	_IOWR('i', 246, struct ifreq)
142 
143 #ifdef _KERNEL
144 int		carp_ioctl(struct ifreq *, u_long, struct thread *);
145 int		carp_attach(struct ifaddr *, int);
146 void		carp_detach(struct ifaddr *);
147 void		carp_carpdev_state(struct ifnet *);
148 void		carp_input (struct mbuf *, int);
149 int		carp6_input (struct mbuf **, int *, int);
150 int		carp_output (struct ifnet *, struct mbuf *, struct sockaddr *);
151 int		carp_iamatch(struct ifaddr *, uint8_t **);
152 struct ifaddr	*carp_iamatch6(struct ifnet *, struct in6_addr *);
153 caddr_t		carp_macmatch6(struct ifnet *, struct mbuf *, const struct in6_addr *);
154 int		carp_forus(struct ifnet *, u_char *);
155 
156 /* These are external networking stack hooks for CARP */
157 /* net/if.c */
158 extern int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
159 extern int (*carp_attach_p)(struct ifaddr *, int);
160 extern void (*carp_detach_p)(struct ifaddr *);
161 extern void (*carp_linkstate_p)(struct ifnet *);
162 extern void (*carp_demote_adj_p)(int, char *);
163 /* net/if_bridge.c net/if_ethersubr.c */
164 extern int (*carp_forus_p)(struct ifnet *, u_char *);
165 /* net/if_ethersubr.c */
166 extern int (*carp_output_p)(struct ifnet *, struct mbuf *,
167     struct sockaddr *);
168 /* net/rtsock.c */
169 extern int (*carp_get_vhid_p)(struct ifaddr *);
170 #ifdef INET
171 /* netinet/if_ether.c */
172 extern int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
173 #endif
174 #ifdef INET6
175 /* netinet6/nd6_nbr.c */
176 extern struct ifaddr *(*carp_iamatch6_p)(struct ifnet *, struct in6_addr *);
177 extern caddr_t (*carp_macmatch6_p)(struct ifnet *, struct mbuf *,
178     const struct in6_addr *);
179 #endif
180 #endif
181 #endif /* _IP_CARP_H */
182