dnsproxy: Fix constiness of variables
[framework/connectivity/connman.git] / gdhcp / common.h
1 /*
2  *
3  *  DHCP client library with GLib integration
4  *
5  *  Copyright (C) 2009-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <netinet/udp.h>
23 #include <netinet/ip.h>
24
25 #include <glib.h>
26
27 #include "gdhcp.h"
28
29 #define dhcp_get_unaligned(ptr)                 \
30 ({                                              \
31         struct __attribute__((packed)) {        \
32                 typeof(*(ptr)) __v;             \
33         } *__p = (void *) (ptr);                \
34         __p->__v;                               \
35 })
36
37 #define dhcp_put_unaligned(val, ptr)            \
38 do {                                            \
39         struct __attribute__((packed)) {        \
40                 typeof(*(ptr)) __v;             \
41         } *__p = (void *) (ptr);                \
42         __p->__v = (val);                       \
43 } while (0)
44
45 #define CLIENT_PORT 68
46 #define SERVER_PORT 67
47
48 #define DHCPV6_CLIENT_PORT 546
49 #define DHCPV6_SERVER_PORT 547
50 #define MAX_DHCPV6_PKT_SIZE 1500
51
52 #define EXTEND_FOR_BUGGY_SERVERS 80
53
54 static const uint8_t MAC_BCAST_ADDR[ETH_ALEN] __attribute__((aligned(2))) = {
55         0xff, 0xff, 0xff, 0xff, 0xff, 0xff
56 };
57
58 static const uint8_t MAC_ANY_ADDR[ETH_ALEN] __attribute__((aligned(2))) = {
59         0x00, 0x00, 0x00, 0x00, 0x00, 0x00
60 };
61
62 /* DHCP packet */
63 #define DHCP_MAGIC              0x63825363
64 #define DHCP_OPTIONS_BUFSIZE    308
65 #define BOOTREQUEST             1
66 #define BOOTREPLY               2
67
68 #define BROADCAST_FLAG          0x8000
69
70 /* See RFC 2131 */
71 struct dhcp_packet {
72         uint8_t op;
73         uint8_t htype;
74         uint8_t hlen;
75         uint8_t hops;
76         uint32_t xid;
77         uint16_t secs;
78         uint16_t flags;
79         uint32_t ciaddr;
80         uint32_t yiaddr;
81         uint32_t siaddr_nip;
82         uint32_t gateway_nip;
83         uint8_t chaddr[16];
84         uint8_t sname[64];
85         uint8_t file[128];
86         uint32_t cookie;
87         uint8_t options[DHCP_OPTIONS_BUFSIZE + EXTEND_FOR_BUGGY_SERVERS];
88 } __attribute__((packed));
89
90 struct ip_udp_dhcp_packet {
91         struct iphdr ip;
92         struct udphdr udp;
93         struct dhcp_packet data;
94 } __attribute__((packed));
95
96 /* See RFC 3315 */
97 struct dhcpv6_packet {
98         uint8_t message;
99         uint8_t transaction_id[3];
100         uint8_t options[];
101 } __attribute__((packed));
102
103
104 /* See RFC 2132 */
105 #define DHCP_PADDING            0x00
106 #define DHCP_SUBNET             0x01
107 #define DHCP_ROUTER             0x03
108 #define DHCP_TIME_SERVER        0x04
109 #define DHCP_NAME_SERVER        0x05
110 #define DHCP_DNS_SERVER         0x06
111 #define DHCP_HOST_NAME          0x0c
112 #define DHCP_DOMAIN_NAME        0x0f
113 #define DHCP_NTP_SERVER         0x2a
114 #define DHCP_REQUESTED_IP       0x32
115 #define DHCP_LEASE_TIME         0x33
116 #define DHCP_OPTION_OVERLOAD    0x34
117 #define DHCP_MESSAGE_TYPE       0x35
118 #define DHCP_SERVER_ID          0x36
119 #define DHCP_PARAM_REQ          0x37
120 #define DHCP_ERR_MESSAGE        0x38
121 #define DHCP_MAX_SIZE           0x39
122 #define DHCP_VENDOR             0x3c
123 #define DHCP_CLIENT_ID          0x3d
124 #define DHCP_END                0xff
125
126 #define OPT_CODE                0
127 #define OPT_LEN                 1
128 #define OPT_DATA                2
129 #define OPTION_FIELD            0
130 #define FILE_FIELD              1
131 #define SNAME_FIELD             2
132
133 /* DHCP_MESSAGE_TYPE values */
134 #define DHCPDISCOVER            1
135 #define DHCPOFFER               2
136 #define DHCPREQUEST             3
137 #define DHCPDECLINE             4
138 #define DHCPACK                 5
139 #define DHCPNAK                 6
140 #define DHCPRELEASE             7
141 #define DHCPINFORM              8
142 #define DHCP_MINTYPE DHCPDISCOVER
143 #define DHCP_MAXTYPE DHCPINFORM
144
145 /* Message types for DHCPv6, RFC 3315 sec 5.3 */
146 #define DHCPV6_SOLICIT          1
147 #define DHCPV6_ADVERTISE        2
148 #define DHCPV6_REQUEST          3
149 #define DHCPV6_CONFIRM          4
150 #define DHCPV6_RENEW            5
151 #define DHCPV6_REBIND           6
152 #define DHCPV6_REPLY            7
153 #define DHCPV6_RELEASE          8
154 #define DHCPV6_DECLINE          9
155 #define DHCPV6_RECONFIGURE      10
156 #define DHCPV6_INFORMATION_REQ  11
157
158 /*
159  * DUID time starts 2000-01-01.
160  */
161 #define DUID_TIME_EPOCH 946684800
162
163 typedef enum {
164         OPTION_UNKNOWN,
165         OPTION_IP,
166         OPTION_STRING,
167         OPTION_U8,
168         OPTION_U16,
169         OPTION_U32,
170         OPTION_TYPE_MASK = 0x0f,
171         OPTION_LIST = 0x10,
172 } GDHCPOptionType;
173
174 typedef struct dhcp_option {
175         GDHCPOptionType type;
176         uint8_t code;
177 } DHCPOption;
178
179 /* Length of the option types in binary form */
180 static const uint8_t dhcp_option_lengths[] = {
181         [OPTION_IP]     = 4,
182         [OPTION_STRING] = 1,
183         [OPTION_U8]     = 1,
184         [OPTION_U16]    = 2,
185         [OPTION_U32]    = 4,
186 };
187
188 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code);
189 uint8_t *dhcpv6_get_option(struct dhcpv6_packet *packet, uint16_t pkt_len,
190                         int code, uint16_t *option_len, int *option_count);
191 uint8_t *dhcpv6_get_sub_option(unsigned char *option, uint16_t max_len,
192                         uint16_t *code, uint16_t *option_len);
193 int dhcp_end_option(uint8_t *optionptr);
194 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt);
195 void dhcpv6_add_binary_option(struct dhcpv6_packet *packet, uint16_t max_len,
196                                 uint16_t *pkt_len, uint8_t *addopt);
197 void dhcp_add_simple_option(struct dhcp_packet *packet,
198                                 uint8_t code, uint32_t data);
199 GDHCPOptionType dhcp_get_code_type(uint8_t code);
200 GDHCPOptionType dhcpv6_get_code_type(uint16_t code);
201
202 uint16_t dhcp_checksum(void *addr, int count);
203
204 void dhcp_init_header(struct dhcp_packet *packet, char type);
205 void dhcpv6_init_header(struct dhcpv6_packet *packet, uint8_t type);
206
207 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
208                         uint32_t source_ip, int source_port,
209                         uint32_t dest_ip, int dest_port,
210                         const uint8_t *dest_arp, int ifindex);
211 int dhcpv6_send_packet(int index, struct dhcpv6_packet *dhcp_pkt, int len);
212 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
213                         uint32_t source_ip, int source_port,
214                         uint32_t dest_ip, int dest_port);
215 int dhcp_l3_socket(int port, const char *interface, int family);
216 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd);
217 int dhcpv6_recv_l3_packet(struct dhcpv6_packet **packet, unsigned char *buf,
218                         int buf_len, int fd);
219 int dhcp_l3_socket_send(int index, int port, int family);
220
221 char *get_interface_name(int index);
222 gboolean interface_is_up(int index);