17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d04ccbb3Scarlsonj  * Common Development and Distribution License (the "License").
6d04ccbb3Scarlsonj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22d04ccbb3Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23d04ccbb3Scarlsonj  * Use is subject to license terms.
24*b31320a7SChris Fraire  * Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_PACKET_H
287c478bd9Sstevel@tonic-gate #define	_PACKET_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <netinet/in.h>
327c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
33d04ccbb3Scarlsonj #include <netinet/dhcp6.h>
347c478bd9Sstevel@tonic-gate #include <dhcp_impl.h>
357c478bd9Sstevel@tonic-gate 
36d04ccbb3Scarlsonj #include "common.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * packet.[ch] contain routines for manipulating, setting, and
407c478bd9Sstevel@tonic-gate  * transmitting DHCP/BOOTP packets.  see packet.c for descriptions on
417c478bd9Sstevel@tonic-gate  * how to use the exported functions.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
457c478bd9Sstevel@tonic-gate extern "C" {
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * data type for recv_pkt().  needed because we may want to wait for
507c478bd9Sstevel@tonic-gate  * several kinds of packets at once, and the existing enumeration of
517c478bd9Sstevel@tonic-gate  * DHCP packet types does not provide a way to do that easily.  here,
527c478bd9Sstevel@tonic-gate  * we light a different bit in the enumeration for each type of packet
537c478bd9Sstevel@tonic-gate  * we want to receive.
54d04ccbb3Scarlsonj  *
55d04ccbb3Scarlsonj  * Note that for DHCPv6, types 4 (CONFIRM), 5 (RENEW), 6 (REBIND), 12
56d04ccbb3Scarlsonj  * (RELAY-FORW, and 13 (RELAY-REPL) are not in the table.  They're never
57d04ccbb3Scarlsonj  * received by a client, so there's no reason to process them.  (SOLICIT,
58d04ccbb3Scarlsonj  * REQUEST, DECLINE, RELEASE, and INFORMATION-REQUEST are also never seen by
59d04ccbb3Scarlsonj  * clients, but are included for consistency.)
60d04ccbb3Scarlsonj  *
61d04ccbb3Scarlsonj  * Note also that the symbols are named for the DHCPv4 message types, and that
62d04ccbb3Scarlsonj  * DHCPv6 has analogous message types.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate typedef enum {
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	DHCP_PUNTYPED	= 0x001,	/* untyped (BOOTP) message */
68d04ccbb3Scarlsonj 	DHCP_PDISCOVER	= 0x002,	/* in v6: SOLICIT (1) */
69d04ccbb3Scarlsonj 	DHCP_POFFER 	= 0x004,	/* in v6: ADVERTISE (2) */
70d04ccbb3Scarlsonj 	DHCP_PREQUEST	= 0x008,	/* in v6: REQUEST (3) */
71d04ccbb3Scarlsonj 	DHCP_PDECLINE	= 0x010,	/* in v6: DECLINE (9) */
72d04ccbb3Scarlsonj 	DHCP_PACK	= 0x020,	/* in v6: REPLY (7), status == 0 */
73d04ccbb3Scarlsonj 	DHCP_PNAK	= 0x040,	/* in v6: REPLY (7), status != 0 */
74d04ccbb3Scarlsonj 	DHCP_PRELEASE	= 0x080,	/* in v6: RELEASE (8) */
75d04ccbb3Scarlsonj 	DHCP_PINFORM	= 0x100,	/* in v6: INFORMATION-REQUEST (11) */
76d04ccbb3Scarlsonj 	DHCP_PRECONFIG	= 0x200		/* v6 only: RECONFIGURE (10) */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate } dhcp_message_type_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
81d04ccbb3Scarlsonj  * A dhcp_pkt_t is used by the output-side packet manipulation functions.
82d04ccbb3Scarlsonj  * While the structure is not strictly necessary, it allows a better separation
83d04ccbb3Scarlsonj  * of functionality since metadata about the packet (such as its current
84d04ccbb3Scarlsonj  * length) is stored along with the packet.
85d04ccbb3Scarlsonj  *
86d04ccbb3Scarlsonj  * Note that 'pkt' points to a dhcpv6_message_t if the packet is IPv6.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate 
89d04ccbb3Scarlsonj typedef struct dhcp_pkt_s {
907c478bd9Sstevel@tonic-gate 	PKT		*pkt;		/* the real underlying packet */
917c478bd9Sstevel@tonic-gate 	unsigned int	pkt_max_len; 	/* its maximum length */
927c478bd9Sstevel@tonic-gate 	unsigned int	pkt_cur_len;	/* its current length */
93d04ccbb3Scarlsonj 	boolean_t	pkt_isv6;
947c478bd9Sstevel@tonic-gate } dhcp_pkt_t;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * a `stop_func_t' is used by parts of dhcpagent that use the
987c478bd9Sstevel@tonic-gate  * retransmission capability of send_pkt().  this makes it so the
997c478bd9Sstevel@tonic-gate  * callers of send_pkt() decide when to stop retransmitting, which
1007c478bd9Sstevel@tonic-gate  * makes more sense than hardcoding their instance-specific cases into
1017c478bd9Sstevel@tonic-gate  * packet.c
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
104d04ccbb3Scarlsonj typedef boolean_t stop_func_t(dhcp_smach_t *, unsigned int);
105d04ccbb3Scarlsonj 
106d04ccbb3Scarlsonj /*
107d04ccbb3Scarlsonj  * Default I/O and interface control sockets.
108d04ccbb3Scarlsonj  */
109d04ccbb3Scarlsonj extern int v6_sock_fd;
110d04ccbb3Scarlsonj extern int v4_sock_fd;
111d04ccbb3Scarlsonj 
112d04ccbb3Scarlsonj extern const in6_addr_t ipv6_all_dhcp_relay_and_servers;
113d04ccbb3Scarlsonj extern const in6_addr_t my_in6addr_any;
1147c478bd9Sstevel@tonic-gate 
115d04ccbb3Scarlsonj PKT_LIST	*alloc_pkt_entry(size_t, boolean_t);
116d04ccbb3Scarlsonj void		free_pkt_entry(PKT_LIST *);
1177c478bd9Sstevel@tonic-gate void		free_pkt_list(PKT_LIST **);
118d04ccbb3Scarlsonj uchar_t		pkt_recv_type(const PKT_LIST *);
119d04ccbb3Scarlsonj uint_t		pkt_get_xid(const PKT *, boolean_t);
120d04ccbb3Scarlsonj dhcp_pkt_t	*init_pkt(dhcp_smach_t *, uchar_t);
121d04ccbb3Scarlsonj boolean_t	remove_pkt_opt(dhcp_pkt_t *, uint_t);
122d04ccbb3Scarlsonj boolean_t	update_v6opt_len(dhcpv6_option_t *, int);
123d04ccbb3Scarlsonj void		*add_pkt_opt(dhcp_pkt_t *, uint_t, const void *, uint_t);
124*b31320a7SChris Fraire size_t		encode_dhcp_opt(void *, boolean_t, uint_t, const void *,
125*b31320a7SChris Fraire 			uint_t);
126d04ccbb3Scarlsonj void		*add_pkt_subopt(dhcp_pkt_t *, dhcpv6_option_t *, uint_t,
127d04ccbb3Scarlsonj 		    const void *, uint_t);
128d04ccbb3Scarlsonj void		*add_pkt_opt16(dhcp_pkt_t *, uint_t, uint16_t);
129d04ccbb3Scarlsonj void		*add_pkt_opt32(dhcp_pkt_t *, uint_t, uint32_t);
130d04ccbb3Scarlsonj void		*add_pkt_prl(dhcp_pkt_t *, dhcp_smach_t *);
131d04ccbb3Scarlsonj boolean_t	add_pkt_lif(dhcp_pkt_t *, dhcp_lif_t *, int, const char *);
132d04ccbb3Scarlsonj void		stop_pkt_retransmission(dhcp_smach_t *);
133d04ccbb3Scarlsonj void		retransmit_now(dhcp_smach_t *);
134e704a8f2Smeem PKT_LIST	*recv_pkt(int, int, boolean_t);
135d04ccbb3Scarlsonj boolean_t	pkt_v4_match(uchar_t, dhcp_message_type_t);
136d04ccbb3Scarlsonj void		pkt_smach_enqueue(dhcp_smach_t *, PKT_LIST *);
137d04ccbb3Scarlsonj boolean_t	send_pkt(dhcp_smach_t *, dhcp_pkt_t *, in_addr_t,
1387c478bd9Sstevel@tonic-gate 		    stop_func_t *);
139d04ccbb3Scarlsonj boolean_t	send_pkt_v6(dhcp_smach_t *, dhcp_pkt_t *, in6_addr_t,
140d04ccbb3Scarlsonj 		    stop_func_t *, uint_t, uint_t);
141d04ccbb3Scarlsonj boolean_t	dhcp_ip_default(void);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #endif	/* _PACKET_H */
148