1142215Sglebius/*	$FreeBSD: stable/11/sys/netinet/ip_carp.h 338081 2018-08-20 01:01:33Z loos $	*/
2142215Sglebius/*	$OpenBSD: ip_carp.h,v 1.8 2004/07/29 22:12:15 mcbride Exp $	*/
3142215Sglebius
4142215Sglebius/*
5142215Sglebius * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
6142215Sglebius * Copyright (c) 2003 Ryan McBride. All rights reserved.
7142215Sglebius *
8142215Sglebius * Redistribution and use in source and binary forms, with or without
9142215Sglebius * modification, are permitted provided that the following conditions
10142215Sglebius * are met:
11142215Sglebius * 1. Redistributions of source code must retain the above copyright
12142215Sglebius *    notice, this list of conditions and the following disclaimer.
13142215Sglebius * 2. Redistributions in binary form must reproduce the above copyright
14142215Sglebius *    notice, this list of conditions and the following disclaimer in the
15142215Sglebius *    documentation and/or other materials provided with the distribution.
16142215Sglebius *
17142215Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18142215Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19142215Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20142215Sglebius * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21142215Sglebius * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22142215Sglebius * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23142215Sglebius * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24142215Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25142215Sglebius * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26142215Sglebius * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27142215Sglebius * THE POSSIBILITY OF SUCH DAMAGE.
28142215Sglebius */
29142215Sglebius
30142215Sglebius#ifndef _IP_CARP_H
31142215Sglebius#define	_IP_CARP_H
32142215Sglebius
33142215Sglebius/*
34142215Sglebius * The CARP header layout is as follows:
35142215Sglebius *
36142215Sglebius *     0                   1                   2                   3
37142215Sglebius *     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
38142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39142215Sglebius *    |Version| Type  | VirtualHostID |    AdvSkew    |    Auth Len   |
40142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41142215Sglebius *    |   Reserved    |     AdvBase   |          Checksum             |
42142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43142215Sglebius *    |                         Counter (1)                           |
44142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45142215Sglebius *    |                         Counter (2)                           |
46142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47142215Sglebius *    |                        SHA-1 HMAC (1)                         |
48142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49142215Sglebius *    |                        SHA-1 HMAC (2)                         |
50142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51142215Sglebius *    |                        SHA-1 HMAC (3)                         |
52142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53142215Sglebius *    |                        SHA-1 HMAC (4)                         |
54142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55142215Sglebius *    |                        SHA-1 HMAC (5)                         |
56142215Sglebius *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57142215Sglebius *
58142215Sglebius */
59142215Sglebius
60142215Sglebiusstruct carp_header {
61142215Sglebius#if BYTE_ORDER == LITTLE_ENDIAN
62142215Sglebius	u_int8_t	carp_type:4,
63142215Sglebius			carp_version:4;
64142215Sglebius#endif
65142215Sglebius#if BYTE_ORDER == BIG_ENDIAN
66142215Sglebius	u_int8_t	carp_version:4,
67142215Sglebius			carp_type:4;
68142215Sglebius#endif
69142215Sglebius	u_int8_t	carp_vhid;	/* virtual host id */
70142215Sglebius	u_int8_t	carp_advskew;	/* advertisement skew */
71142215Sglebius	u_int8_t	carp_authlen;   /* size of counter+md, 32bit chunks */
72142215Sglebius	u_int8_t	carp_pad1;	/* reserved */
73142215Sglebius	u_int8_t	carp_advbase;	/* advertisement interval */
74142215Sglebius	u_int16_t	carp_cksum;
75142215Sglebius	u_int32_t	carp_counter[2];
76142215Sglebius	unsigned char	carp_md[20];	/* SHA1 HMAC */
77142215Sglebius} __packed;
78142215Sglebius
79164808Simp#ifdef CTASSERT
80164808SimpCTASSERT(sizeof(struct carp_header) == 36);
81164808Simp#endif
82164808Simp
83142215Sglebius#define	CARP_DFLTTL		255
84142215Sglebius
85142215Sglebius/* carp_version */
86142215Sglebius#define	CARP_VERSION		2
87142215Sglebius
88142215Sglebius/* carp_type */
89142215Sglebius#define	CARP_ADVERTISEMENT	0x01
90142215Sglebius
91142215Sglebius#define	CARP_KEY_LEN		20	/* a sha1 hash of a passphrase */
92142215Sglebius
93142215Sglebius/* carp_advbase */
94142215Sglebius#define	CARP_DFLTINTV		1
95142215Sglebius
96142215Sglebius/*
97142215Sglebius * Statistics.
98142215Sglebius */
99142215Sglebiusstruct carpstats {
100142215Sglebius	uint64_t	carps_ipackets;		/* total input packets, IPv4 */
101142215Sglebius	uint64_t	carps_ipackets6;	/* total input packets, IPv6 */
102142215Sglebius	uint64_t	carps_badif;		/* wrong interface */
103142215Sglebius	uint64_t	carps_badttl;		/* TTL is not CARP_DFLTTL */
104142215Sglebius	uint64_t	carps_hdrops;		/* packets shorter than hdr */
105142215Sglebius	uint64_t	carps_badsum;		/* bad checksum */
106142215Sglebius	uint64_t	carps_badver;		/* bad (incl unsupp) version */
107142215Sglebius	uint64_t	carps_badlen;		/* data length does not match */
108142215Sglebius	uint64_t	carps_badauth;		/* bad authentication */
109142215Sglebius	uint64_t	carps_badvhid;		/* bad VHID */
110142215Sglebius	uint64_t	carps_badaddrs;		/* bad address list */
111142215Sglebius
112142215Sglebius	uint64_t	carps_opackets;		/* total output packets, IPv4 */
113142215Sglebius	uint64_t	carps_opackets6;	/* total output packets, IPv6 */
114142215Sglebius	uint64_t	carps_onomem;		/* no memory for an mbuf */
115142215Sglebius	uint64_t	carps_ostates;		/* total state updates sent */
116142215Sglebius
117142215Sglebius	uint64_t	carps_preempt;		/* if enabled, preemptions */
118142215Sglebius};
119142215Sglebius
120142215Sglebius/*
121142215Sglebius * Configuration structure for SIOCSVH SIOCGVH
122142215Sglebius */
123142215Sglebiusstruct carpreq {
124228571Sglebius	int		carpr_count;
125228571Sglebius	int		carpr_vhid;
126228571Sglebius#define	CARP_MAXVHID	255
127142215Sglebius	int		carpr_state;
128142215Sglebius#define	CARP_STATES	"INIT", "BACKUP", "MASTER"
129142215Sglebius#define	CARP_MAXSTATE	2
130142215Sglebius	int		carpr_advskew;
131228736Sglebius#define	CARP_MAXSKEW	240
132142215Sglebius	int		carpr_advbase;
133142215Sglebius	unsigned char	carpr_key[CARP_KEY_LEN];
134142215Sglebius};
135142215Sglebius#define	SIOCSVH	_IOWR('i', 245, struct ifreq)
136142215Sglebius#define	SIOCGVH	_IOWR('i', 246, struct ifreq)
137142215Sglebius
138142215Sglebius#ifdef _KERNEL
139228571Sglebiusint		carp_ioctl(struct ifreq *, u_long, struct thread *);
140228571Sglebiusint		carp_attach(struct ifaddr *, int);
141338081Sloosvoid		carp_detach(struct ifaddr *, bool);
142228571Sglebiusvoid		carp_carpdev_state(struct ifnet *);
143269699Skevloint		carp_input(struct mbuf **, int *, int);
144228571Sglebiusint		carp6_input (struct mbuf **, int *, int);
145249925Sglebiusint		carp_output (struct ifnet *, struct mbuf *,
146249925Sglebius		    const struct sockaddr *);
147246659Sglebiusint		carp_master(struct ifaddr *);
148228571Sglebiusint		carp_iamatch(struct ifaddr *, uint8_t **);
149211157Swillstruct ifaddr	*carp_iamatch6(struct ifnet *, struct in6_addr *);
150211157Swillcaddr_t		carp_macmatch6(struct ifnet *, struct mbuf *, const struct in6_addr *);
151228571Sglebiusint		carp_forus(struct ifnet *, u_char *);
152211193Swill
153211193Swill/* These are external networking stack hooks for CARP */
154211193Swill/* net/if.c */
155228571Sglebiusextern int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
156228571Sglebiusextern int (*carp_attach_p)(struct ifaddr *, int);
157338081Sloosextern void (*carp_detach_p)(struct ifaddr *, bool);
158211193Swillextern void (*carp_linkstate_p)(struct ifnet *);
159228736Sglebiusextern void (*carp_demote_adj_p)(int, char *);
160246659Sglebiusextern int (*carp_master_p)(struct ifaddr *);
161211193Swill/* net/if_bridge.c net/if_ethersubr.c */
162228571Sglebiusextern int (*carp_forus_p)(struct ifnet *, u_char *);
163211193Swill/* net/if_ethersubr.c */
164211193Swillextern int (*carp_output_p)(struct ifnet *, struct mbuf *,
165249925Sglebius    const struct sockaddr *);
166228571Sglebius/* net/rtsock.c */
167228571Sglebiusextern int (*carp_get_vhid_p)(struct ifaddr *);
168211193Swill#ifdef INET
169211193Swill/* netinet/if_ether.c */
170228571Sglebiusextern int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
171142215Sglebius#endif
172211193Swill#ifdef INET6
173211193Swill/* netinet6/nd6_nbr.c */
174211193Swillextern struct ifaddr *(*carp_iamatch6_p)(struct ifnet *, struct in6_addr *);
175211193Swillextern caddr_t (*carp_macmatch6_p)(struct ifnet *, struct mbuf *,
176211193Swill    const struct in6_addr *);
177211193Swill#endif
178211193Swill#endif
179142215Sglebius#endif /* _IP_CARP_H */
180