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