ip_carp.h revision 211193
1101099Srwatson/*	$FreeBSD: head/sys/netinet/ip_carp.h 211193 2010-08-11 20:18:19Z will $	*/
2166533Srwatson/*	$OpenBSD: ip_carp.h,v 1.8 2004/07/29 22:12:15 mcbride Exp $	*/
3140628Srwatson
4172930Srwatson/*
5101099Srwatson * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
6101099Srwatson * Copyright (c) 2003 Ryan McBride. All rights reserved.
7101099Srwatson *
8101099Srwatson * Redistribution and use in source and binary forms, with or without
9140628Srwatson * modification, are permitted provided that the following conditions
10140628Srwatson * are met:
11140628Srwatson * 1. Redistributions of source code must retain the above copyright
12140628Srwatson *    notice, this list of conditions and the following disclaimer.
13101099Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14172930Srwatson *    notice, this list of conditions and the following disclaimer in the
15172930Srwatson *    documentation and/or other materials provided with the distribution.
16172930Srwatson *
17101099Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18101099Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19101099Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20101099Srwatson * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21101099Srwatson * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22101099Srwatson * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23101099Srwatson * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24101099Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25101099Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26101099Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27101099Srwatson * THE POSSIBILITY OF SUCH DAMAGE.
28101099Srwatson */
29101099Srwatson
30101099Srwatson#ifndef _IP_CARP_H
31101099Srwatson#define	_IP_CARP_H
32101099Srwatson
33101099Srwatson/*
34101099Srwatson * The CARP header layout is as follows:
35101099Srwatson *
36101099Srwatson *     0                   1                   2                   3
37101099Srwatson *     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
38101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39101099Srwatson *    |Version| Type  | VirtualHostID |    AdvSkew    |    Auth Len   |
40101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41101099Srwatson *    |   Reserved    |     AdvBase   |          Checksum             |
42101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43168951Srwatson *    |                         Counter (1)                           |
44101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45101099Srwatson *    |                         Counter (2)                           |
46101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47101099Srwatson *    |                        SHA-1 HMAC (1)                         |
48101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49105988Srwatson *    |                        SHA-1 HMAC (2)                         |
50101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51164184Strhodes *    |                        SHA-1 HMAC (3)                         |
52103183Sbde *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53145076Scsjp *    |                        SHA-1 HMAC (4)                         |
54101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55168951Srwatson *    |                        SHA-1 HMAC (5)                         |
56101099Srwatson *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57115497Srwatson *
58101099Srwatson */
59101099Srwatson
60101099Srwatsonstruct carp_header {
61105696Srwatson#if BYTE_ORDER == LITTLE_ENDIAN
62101099Srwatson	u_int8_t	carp_type:4,
63101099Srwatson			carp_version:4;
64101099Srwatson#endif
65101099Srwatson#if BYTE_ORDER == BIG_ENDIAN
66101099Srwatson	u_int8_t	carp_version:4,
67150340Sphk			carp_type:4;
68101099Srwatson#endif
69140628Srwatson	u_int8_t	carp_vhid;	/* virtual host id */
70140628Srwatson	u_int8_t	carp_advskew;	/* advertisement skew */
71140628Srwatson	u_int8_t	carp_authlen;   /* size of counter+md, 32bit chunks */
72101099Srwatson	u_int8_t	carp_pad1;	/* reserved */
73101099Srwatson	u_int8_t	carp_advbase;	/* advertisement interval */
74101099Srwatson	u_int16_t	carp_cksum;
75101099Srwatson	u_int32_t	carp_counter[2];
76101099Srwatson	unsigned char	carp_md[20];	/* SHA1 HMAC */
77101099Srwatson} __packed;
78101099Srwatson
79101099Srwatson#ifdef CTASSERT
80101099SrwatsonCTASSERT(sizeof(struct carp_header) == 36);
81122875Srwatson#endif
82101099Srwatson
83101099Srwatson#define	CARP_DFLTTL		255
84122879Srwatson
85101099Srwatson/* carp_version */
86101099Srwatson#define	CARP_VERSION		2
87165469Srwatson
88101099Srwatson/* carp_type */
89101099Srwatson#define	CARP_ADVERTISEMENT	0x01
90101099Srwatson
91101099Srwatson#define	CARP_KEY_LEN		20	/* a sha1 hash of a passphrase */
92101099Srwatson
93101099Srwatson/* carp_advbase */
94101099Srwatson#define	CARP_DFLTINTV		1
95172955Srwatson
96105988Srwatson/*
97172955Srwatson * Statistics.
98105988Srwatson */
99172955Srwatsonstruct carpstats {
100172955Srwatson	uint64_t	carps_ipackets;		/* total input packets, IPv4 */
101172955Srwatson	uint64_t	carps_ipackets6;	/* total input packets, IPv6 */
102172955Srwatson	uint64_t	carps_badif;		/* wrong interface */
103101099Srwatson	uint64_t	carps_badttl;		/* TTL is not CARP_DFLTTL */
104101099Srwatson	uint64_t	carps_hdrops;		/* packets shorter than hdr */
105101099Srwatson	uint64_t	carps_badsum;		/* bad checksum */
106101099Srwatson	uint64_t	carps_badver;		/* bad (incl unsupp) version */
107101099Srwatson	uint64_t	carps_badlen;		/* data length does not match */
108101099Srwatson	uint64_t	carps_badauth;		/* bad authentication */
109101099Srwatson	uint64_t	carps_badvhid;		/* bad VHID */
110101099Srwatson	uint64_t	carps_badaddrs;		/* bad address list */
111101099Srwatson
112101099Srwatson	uint64_t	carps_opackets;		/* total output packets, IPv4 */
113101099Srwatson	uint64_t	carps_opackets6;	/* total output packets, IPv6 */
114101099Srwatson	uint64_t	carps_onomem;		/* no memory for an mbuf */
115101099Srwatson	uint64_t	carps_ostates;		/* total state updates sent */
116101099Srwatson
117101099Srwatson	uint64_t	carps_preempt;		/* if enabled, preemptions */
118101099Srwatson};
119105643Srwatson
120105643Srwatson#ifdef _KERNEL
121105643Srwatson#define	CARPSTATS_ADD(name, val)	carpstats.name += (val)
122105643Srwatson#define	CARPSTATS_INC(name)		CARPSTATS_ADD(name, 1)
123105606Srwatson#endif
124105606Srwatson
125105606Srwatson/*
126105606Srwatson * Configuration structure for SIOCSVH SIOCGVH
127105606Srwatson */
128153927Scsjpstruct carpreq {
129153927Scsjp	int		carpr_state;
130153927Scsjp#define	CARP_STATES	"INIT", "BACKUP", "MASTER"
131153927Scsjp#define	CARP_MAXSTATE	2
132153927Scsjp	int		carpr_vhid;
133105637Srwatson	int		carpr_advskew;
134101099Srwatson	int		carpr_advbase;
135105637Srwatson	unsigned char	carpr_key[CARP_KEY_LEN];
136105637Srwatson};
137101099Srwatson#define	SIOCSVH	_IOWR('i', 245, struct ifreq)
138172955Srwatson#define	SIOCGVH	_IOWR('i', 246, struct ifreq)
139172955Srwatson
140172955Srwatson/*
141101099Srwatson * Names for CARP sysctl objects
142122879Srwatson */
143101099Srwatson#define	CARPCTL_ALLOW		1	/* accept incoming CARP packets */
144105643Srwatson#define	CARPCTL_PREEMPT		2	/* high-pri backup preemption mode */
145105643Srwatson#define	CARPCTL_LOG		3	/* log bad packets */
146105643Srwatson#define	CARPCTL_STATS		4	/* statistics (read-only) */
147105643Srwatson#define	CARPCTL_ARPBALANCE	5	/* balance arp responses */
148105643Srwatson#define	CARPCTL_MAXID		6
149105643Srwatson
150105643Srwatson#define	CARPCTL_NAMES { \
151105643Srwatson	{ 0, 0 }, \
152105643Srwatson	{ "allow", CTLTYPE_INT }, \
153105643Srwatson	{ "preempt", CTLTYPE_INT }, \
154101099Srwatson	{ "log", CTLTYPE_INT }, \
155104514Srwatson	{ "stats", CTLTYPE_STRUCT }, \
156101099Srwatson	{ "arpbalance", CTLTYPE_INT }, \
157101099Srwatson}
158122879Srwatson
159101099Srwatson#ifdef _KERNEL
160101099Srwatsonvoid		 carp_carpdev_state(struct ifnet *);
161101099Srwatsonvoid		 carp_input (struct mbuf *, int);
162172955Srwatsonint		 carp6_input (struct mbuf **, int *, int);
163101099Srwatsonint		 carp_output (struct ifnet *, struct mbuf *, struct sockaddr *,
164101099Srwatson		     struct rtentry *);
165172955Srwatsonint		 carp_iamatch (struct ifnet *, struct in_ifaddr *, struct in_addr *,
166172955Srwatson		     u_int8_t **);
167101099Srwatsonstruct ifaddr	*carp_iamatch6(struct ifnet *, struct in6_addr *);
168101099Srwatsoncaddr_t		carp_macmatch6(struct ifnet *, struct mbuf *, const struct in6_addr *);
169101099Srwatsonstruct	ifnet	*carp_forus (struct ifnet *, u_char *);
170101099Srwatson
171101099Srwatson/* These are external networking stack hooks for CARP */
172172955Srwatson/* net/if.c */
173105634Srwatsonextern void (*carp_linkstate_p)(struct ifnet *);
174105634Srwatson/* net/if_bridge.c net/if_ethersubr.c */
175172955Srwatsonextern struct ifnet *(*carp_forus_p)(struct ifnet *, u_char *);
176105634Srwatson/* net/if_ethersubr.c */
177105634Srwatsonextern int (*carp_output_p)(struct ifnet *, struct mbuf *,
178105634Srwatson    struct sockaddr *, struct rtentry *);
179105634Srwatson#ifdef INET
180105634Srwatson/* netinet/if_ether.c */
181172955Srwatsonextern int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *,
182101099Srwatson    struct in_addr *, u_int8_t **);
183105643Srwatson#endif
184101099Srwatson#ifdef INET6
185105736Srwatson/* netinet6/nd6_nbr.c */
186101099Srwatsonextern struct ifaddr *(*carp_iamatch6_p)(struct ifnet *, struct in6_addr *);
187101099Srwatsonextern caddr_t (*carp_macmatch6_p)(struct ifnet *, struct mbuf *,
188101099Srwatson    const struct in6_addr *);
189101099Srwatson#endif
190101099Srwatson#endif
191101099Srwatson#endif /* _IP_CARP_H */
192101099Srwatson