1 /* $OpenBSD: dhcpd.h,v 1.22 2017/04/04 15:50:29 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> 5 * Copyright (c) 1995, 1996, 1997, 1998, 1999 6 * The Internet Software Consortium. 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 * 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 * 3. Neither the name of The Internet Software Consortium nor the names 18 * of its contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 22 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * This software has been written for the Internet Software Consortium 36 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 37 * Enterprises. To learn more about the Internet Software Consortium, 38 * see ``http://www.vix.com/isc''. To learn more about Vixie 39 * Enterprises, see ``http://www.vix.com''. 40 */ 41 42 #include <sys/queue.h> 43 44 #define SERVER_PORT 67 45 #define CLIENT_PORT 68 46 47 /* Maximum size of client hardware address. */ 48 #define CHADDR_SIZE 16 49 50 struct packet_ctx { 51 uint8_t pc_htype; 52 uint8_t pc_hlen; 53 uint8_t pc_smac[CHADDR_SIZE]; 54 uint8_t pc_dmac[CHADDR_SIZE]; 55 56 struct sockaddr_storage pc_src; 57 struct sockaddr_storage pc_dst; 58 59 const char *pc_circuit; 60 int pc_circuitlen; 61 const char *pc_remote; 62 int pc_remotelen; 63 }; 64 65 struct iaddr { 66 int len; 67 unsigned char iabuf[CHADDR_SIZE]; 68 }; 69 70 struct hardware { 71 u_int8_t htype; 72 u_int8_t hlen; 73 u_int8_t haddr[CHADDR_SIZE]; 74 }; 75 76 /* Possible states in which the client can be. */ 77 enum dhcp_state { 78 S_REBOOTING, 79 S_INIT, 80 S_SELECTING, 81 S_REQUESTING, 82 S_BOUND, 83 S_RENEWING, 84 S_REBINDING 85 }; 86 87 /* DHCP relaying modes. */ 88 enum dhcp_relay_mode { 89 DRM_UNKNOWN, 90 DRM_LAYER2, 91 DRM_LAYER3, 92 }; 93 94 struct interface_info { 95 struct hardware hw_address; 96 struct in_addr primary_address; 97 char name[IFNAMSIZ]; 98 int rfdesc; 99 int wfdesc; 100 unsigned char *rbuf; 101 size_t rbuf_max; 102 size_t rbuf_offset; 103 size_t rbuf_len; 104 struct ifreq ifr; 105 int noifmedia; 106 int errors; 107 int dead; 108 u_int16_t index; 109 110 int ipv6; /* Has any IPv6 address. */ 111 int gipv6; /* Has global IPv6 address. */ 112 struct in6_addr linklocal; /* IPv6 link-local address. */ 113 114 TAILQ_ENTRY(interface_info) entry; 115 }; 116 TAILQ_HEAD(iflist, interface_info); 117 118 struct timeout { 119 struct timeout *next; 120 time_t when; 121 void (*func)(void *); 122 void *what; 123 }; 124 125 struct protocol { 126 struct protocol *next; 127 int fd; 128 void (*handler)(struct protocol *); 129 void *local; 130 }; 131 132 struct server_list { 133 struct interface_info *intf; 134 struct server_list *next; 135 struct sockaddr_storage to; 136 int fd; 137 }; 138 139 #define DHCPD_LOG_FACILITY LOG_DAEMON 140 141 /* External definitions... */ 142 143 /* errwarn.c */ 144 void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 145 int warning(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 146 int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 147 int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2))); 148 149 /* bpf.c */ 150 int if_register_bpf(struct interface_info *); 151 void if_register_send(struct interface_info *); 152 void if_register_receive(struct interface_info *, int); 153 ssize_t send_packet(struct interface_info *, 154 struct dhcp_packet *, size_t, struct packet_ctx *); 155 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t, 156 struct packet_ctx *); 157 158 /* dispatch.c */ 159 extern void (*bootp_packet_handler)(struct interface_info *, 160 struct dhcp_packet *, int, struct packet_ctx *); 161 struct interface_info *iflist_getbyname(const char *); 162 void setup_iflist(void); 163 struct interface_info *register_interface(const char *, 164 void (*)(struct protocol *), int isserver); 165 void dispatch(void); 166 void got_one(struct protocol *); 167 void add_protocol(char *, int, void (*)(struct protocol *), void *); 168 void remove_protocol(struct protocol *); 169 170 /* packet.c */ 171 void assemble_hw_header(struct interface_info *, unsigned char *, 172 int *, struct packet_ctx *); 173 void assemble_udp_ip_header(struct interface_info *, unsigned char *, 174 int *, struct packet_ctx *pc, unsigned char *, int); 175 ssize_t decode_hw_header(struct interface_info *, unsigned char *, 176 int, struct packet_ctx *); 177 ssize_t decode_udp_ip_header(struct interface_info *, unsigned char *, 178 int, struct packet_ctx *, int); 179 180 /* dhcrelay.c */ 181 extern int server_fd; 182 extern struct iflist intflist; 183 184 /* crap */ 185 extern time_t cur_time; 186 187 static inline struct sockaddr_in * 188 ss2sin(struct sockaddr_storage *ss) 189 { 190 return ((struct sockaddr_in *)ss); 191 } 192 193 static inline struct sockaddr_in6 * 194 ss2sin6(struct sockaddr_storage *ss) 195 { 196 return ((struct sockaddr_in6 *)ss); 197 } 198