Use 1 socket per interface for DHCPv6
[project/odhcpd.git] / src / dhcpv6.h
1 /**
2  *   Copyright (C) 2012 Steven Barth <steven@midlink.org>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License version 2
6  *   as published by the Free Software Foundation.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License version 2 for more details.
12  *
13  */
14 #pragma once
15
16 #define ALL_DHCPV6_RELAYS {{{0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
17                 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}}}
18
19 #define ALL_DHCPV6_SERVERS {{{0xff, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
20                 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03}}}
21
22 #define DHCPV6_CLIENT_PORT 546
23 #define DHCPV6_SERVER_PORT 547
24
25 #define DHCPV6_MSG_SOLICIT 1
26 #define DHCPV6_MSG_ADVERTISE 2
27 #define DHCPV6_MSG_REQUEST 3
28 #define DHCPV6_MSG_CONFIRM 4
29 #define DHCPV6_MSG_RENEW 5
30 #define DHCPV6_MSG_REBIND 6
31 #define DHCPV6_MSG_REPLY 7
32 #define DHCPV6_MSG_RELEASE 8
33 #define DHCPV6_MSG_DECLINE 9
34 #define DHCPV6_MSG_RECONFIGURE 10
35 #define DHCPV6_MSG_INFORMATION_REQUEST 11
36 #define DHCPV6_MSG_RELAY_FORW 12
37 #define DHCPV6_MSG_RELAY_REPL 13
38
39 #define DHCPV6_OPT_CLIENTID 1
40 #define DHCPV6_OPT_SERVERID 2
41 #define DHCPV6_OPT_IA_NA 3
42 #define DHCPV6_OPT_IA_ADDR 5
43 #define DHCPV6_OPT_ORO 6
44 #define DHCPV6_OPT_STATUS 13
45 #define DHCPV6_OPT_RELAY_MSG 9
46 #define DHCPV6_OPT_AUTH 11
47 #define DHCPV6_OPT_INTERFACE_ID 18
48 #define DHCPV6_OPT_RECONF_MSG 19
49 #define DHCPV6_OPT_RECONF_ACCEPT 20
50 #define DHCPV6_OPT_DNS_SERVERS 23
51 #define DHCPV6_OPT_DNS_DOMAIN 24
52 #define DHCPV6_OPT_IA_PD 25
53 #define DHCPV6_OPT_IA_PREFIX 26
54 #define DHCPV6_OPT_INFO_REFRESH 32
55 #define DHCPV6_OPT_FQDN 39
56
57 #ifdef EXT_PREFIX_CLASS
58 /* draft-bhandari-dhc-class-based-prefix, not yet standardized */
59 #define DHCPV6_OPT_PREFIX_CLASS EXT_PREFIX_CLASS
60 #endif
61
62 #define DHCPV6_DUID_VENDOR 2
63
64 #define DHCPV6_STATUS_OK 0
65 #define DHCPV6_STATUS_NOADDRSAVAIL 2
66 #define DHCPV6_STATUS_NOBINDING 3
67 #define DHCPV6_STATUS_NOTONLINK 4
68 #define DHCPV6_STATUS_NOPREFIXAVAIL 6
69
70 // I just remembered I have an old one lying around...
71 #define DHCPV6_ENT_NO  30462
72 #define DHCPV6_ENT_TYPE 1
73
74
75 #define DHCPV6_HOP_COUNT_LIMIT 32
76
77 struct dhcpv6_client_header {
78         uint8_t msg_type;
79         uint8_t transaction_id[3];
80 } __attribute__((packed));
81
82 struct dhcpv6_relay_header {
83         uint8_t msg_type;
84         uint8_t hop_count;
85         struct in6_addr link_address;
86         struct in6_addr peer_address;
87         uint8_t options[];
88 } __attribute__((packed));
89
90 struct dhcpv6_relay_forward_envelope {
91         uint8_t msg_type;
92         uint8_t hop_count;
93         struct in6_addr link_address;
94         struct in6_addr peer_address;
95         uint16_t interface_id_type;
96         uint16_t interface_id_len;
97         uint32_t interface_id_data;
98         uint16_t relay_message_type;
99         uint16_t relay_message_len;
100 } __attribute__((packed));
101
102 struct dhcpv6_auth_reconfigure {
103         uint16_t type;
104         uint16_t len;
105         uint8_t protocol;
106         uint8_t algorithm;
107         uint8_t rdm;
108         uint32_t replay[2];
109         uint8_t reconf_type;
110         uint8_t key[16];
111 } _packed;
112
113 struct dhcpv6_ia_hdr {
114         uint16_t type;
115         uint16_t len;
116         uint32_t iaid;
117         uint32_t t1;
118         uint32_t t2;
119 } _packed;
120
121 struct dhcpv6_ia_prefix {
122         uint16_t type;
123         uint16_t len;
124         uint32_t preferred;
125         uint32_t valid;
126         uint8_t prefix;
127         struct in6_addr addr;
128 } _packed;
129
130 struct dhcpv6_ia_addr {
131         uint16_t type;
132         uint16_t len;
133         struct in6_addr addr;
134         uint32_t preferred;
135         uint32_t valid;
136 } _packed;
137
138 struct dhcpv6_assignment {
139         struct list_head head;
140         struct sockaddr_in6 peer;
141         time_t valid_until;
142         time_t reconf_sent;
143         bool all_class;
144         uint8_t classes_cnt;
145         uint16_t *classes;
146         int reconf_cnt;
147         char *hostname;
148         uint8_t key[16];
149         uint32_t assigned;
150         uint32_t iaid;
151         uint8_t mac[6];
152         uint8_t length; // length == 128 -> IA_NA, length <= 64 -> IA_PD
153         bool accept_reconf;
154         uint8_t clid_len;
155         uint8_t clid_data[];
156 };
157
158
159
160 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
161         for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (end) &&\
162                 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
163                 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (end); \
164                 _o += 4 + (_o[2] << 8 | _o[3]))
165
166 int dhcpv6_init_ia(struct interface *iface, int socket);
167 size_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
168                 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);
169 int dhcpv6_ia_init(void);
170 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable);
171 void dhcpv6_write_statefile(void);