treewide: rework handling of netlink events
[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 #include <libubox/ustream.h>
17 #include "odhcpd.h"
18
19 #define ALL_DHCPV6_RELAYS {{{0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
20                 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}}}
21
22 #define ALL_DHCPV6_SERVERS {{{0xff, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
23                 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03}}}
24
25 #define DHCPV6_CLIENT_PORT 546
26 #define DHCPV6_SERVER_PORT 547
27
28 #define DHCPV6_MSG_SOLICIT 1
29 #define DHCPV6_MSG_ADVERTISE 2
30 #define DHCPV6_MSG_REQUEST 3
31 #define DHCPV6_MSG_CONFIRM 4
32 #define DHCPV6_MSG_RENEW 5
33 #define DHCPV6_MSG_REBIND 6
34 #define DHCPV6_MSG_REPLY 7
35 #define DHCPV6_MSG_RELEASE 8
36 #define DHCPV6_MSG_DECLINE 9
37 #define DHCPV6_MSG_RECONFIGURE 10
38 #define DHCPV6_MSG_INFORMATION_REQUEST 11
39 #define DHCPV6_MSG_RELAY_FORW 12
40 #define DHCPV6_MSG_RELAY_REPL 13
41
42 #define DHCPV6_OPT_CLIENTID 1
43 #define DHCPV6_OPT_SERVERID 2
44 #define DHCPV6_OPT_IA_NA 3
45 #define DHCPV6_OPT_IA_ADDR 5
46 #define DHCPV6_OPT_ORO 6
47 #define DHCPV6_OPT_STATUS 13
48 #define DHCPV6_OPT_RELAY_MSG 9
49 #define DHCPV6_OPT_AUTH 11
50 #define DHCPV6_OPT_USER_CLASS 15
51 #define DHCPV6_OPT_INTERFACE_ID 18
52 #define DHCPV6_OPT_RECONF_MSG 19
53 #define DHCPV6_OPT_RECONF_ACCEPT 20
54 #define DHCPV6_OPT_DNS_SERVERS 23
55 #define DHCPV6_OPT_DNS_DOMAIN 24
56 #define DHCPV6_OPT_IA_PD 25
57 #define DHCPV6_OPT_IA_PREFIX 26
58 #define DHCPV6_OPT_INFO_REFRESH 32
59 #define DHCPV6_OPT_FQDN 39
60 #define DHCPV6_OPT_SOL_MAX_RT 82
61 #define DHCPV6_OPT_INF_MAX_RT 83
62
63 #define DHCPV6_DUID_VENDOR 2
64
65 #define DHCPV6_STATUS_OK 0
66 #define DHCPV6_STATUS_NOADDRSAVAIL 2
67 #define DHCPV6_STATUS_NOBINDING 3
68 #define DHCPV6_STATUS_NOTONLINK 4
69 #define DHCPV6_STATUS_USEMULTICAST 5
70 #define DHCPV6_STATUS_NOPREFIXAVAIL 6
71
72 // I just remembered I have an old one lying around...
73 #define DHCPV6_ENT_NO  30462
74 #define DHCPV6_ENT_TYPE 1
75
76
77 #define DHCPV6_HOP_COUNT_LIMIT 32
78
79 #define DHCPV6_REC_TIMEOUT      2000 /* msec */
80 #define DHCPV6_REC_MAX_RC       8
81
82 struct dhcpv6_client_header {
83         uint8_t msg_type;
84         uint8_t transaction_id[3];
85 } __attribute__((packed));
86
87 struct dhcpv6_relay_header {
88         uint8_t msg_type;
89         uint8_t hop_count;
90         struct in6_addr link_address;
91         struct in6_addr peer_address;
92         uint8_t options[];
93 } __attribute__((packed));
94
95 struct dhcpv6_relay_forward_envelope {
96         uint8_t msg_type;
97         uint8_t hop_count;
98         struct in6_addr link_address;
99         struct in6_addr peer_address;
100         uint16_t interface_id_type;
101         uint16_t interface_id_len;
102         uint32_t interface_id_data;
103         uint16_t relay_message_type;
104         uint16_t relay_message_len;
105 } __attribute__((packed));
106
107 struct dhcpv6_auth_reconfigure {
108         uint16_t type;
109         uint16_t len;
110         uint8_t protocol;
111         uint8_t algorithm;
112         uint8_t rdm;
113         uint32_t replay[2];
114         uint8_t reconf_type;
115         uint8_t key[16];
116 } _packed;
117
118 struct dhcpv6_ia_hdr {
119         uint16_t type;
120         uint16_t len;
121         uint32_t iaid;
122         uint32_t t1;
123         uint32_t t2;
124 } _packed;
125
126 struct dhcpv6_ia_prefix {
127         uint16_t type;
128         uint16_t len;
129         uint32_t preferred;
130         uint32_t valid;
131         uint8_t prefix;
132         struct in6_addr addr;
133 } _packed;
134
135 struct dhcpv6_ia_addr {
136         uint16_t type;
137         uint16_t len;
138         struct in6_addr addr;
139         uint32_t preferred;
140         uint32_t valid;
141 } _packed;
142
143 struct dhcpv6_assignment {
144         struct list_head head;
145         struct interface *iface;
146
147         struct sockaddr_in6 peer;
148         time_t valid_until;
149
150         struct uloop_timeout reconf_timer;
151         bool accept_reconf;
152         int reconf_cnt;
153         uint8_t key[16];
154
155         char *hostname;
156         uint32_t assigned;
157         uint32_t iaid;
158         uint8_t mac[6];
159         uint8_t length; // length == 128 -> IA_NA, length <= 64 -> IA_PD
160
161         struct odhcpd_ipaddr *managed;
162         ssize_t managed_size;
163         struct ustream_fd managed_sock;
164
165         uint32_t leasetime;
166         unsigned int flags;
167
168         uint8_t clid_len;
169         uint8_t clid_data[];
170 };
171
172 struct dhcpv6_cer_id {
173         uint16_t type;
174         uint16_t len;
175         uint16_t reserved;
176         uint16_t auth_type;
177         uint8_t auth[16];
178         struct in6_addr addr;
179 };
180
181 typedef void (*dhcpv6_binding_cb_handler_t)(struct in6_addr *addr, int prefix,
182                                                 uint32_t pref, uint32_t valid,
183                                                 void *arg);
184
185 #define dhcpv6_for_each_option(start, end, otype, olen, odata)\
186         for (uint8_t *_o = (uint8_t*)(start); _o + 4 <= (end) &&\
187                 ((otype) = _o[0] << 8 | _o[1]) && ((odata) = (void*)&_o[4]) &&\
188                 ((olen) = _o[2] << 8 | _o[3]) + (odata) <= (end); \
189                 _o += 4 + (_o[2] << 8 | _o[3]))
190
191 int dhcpv6_init_ia(struct interface *iface, int socket);
192 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
193                 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end);
194 int dhcpv6_ia_init(void);
195 int dhcpv6_setup_ia_interface(struct interface *iface, bool enable);
196 void dhcpv6_enum_ia_addrs(struct interface *iface, struct dhcpv6_assignment *c, time_t now,
197                                 dhcpv6_binding_cb_handler_t func, void *arg);
198 void dhcpv6_write_statefile(void);