lib/util/charset rename iconv_convenience to iconv_handle
[ira/wip.git] / source3 / lib / addrchange.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Copyright (C) Volker Lendecke 2011
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "includes.h"
20 #include "lib/addrchange.h"
21
22 #if HAVE_LINUX_RTNETLINK_H
23
24 #include "asm/types.h"
25 #include "linux/netlink.h"
26 #include "linux/rtnetlink.h"
27 #include "lib/async_req/async_sock.h"
28
29 struct addrchange_context {
30         int sock;
31 };
32
33 static int addrchange_context_destructor(struct addrchange_context *c);
34
35 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
36                                    struct addrchange_context **pctx)
37 {
38         struct addrchange_context *ctx;
39         struct sockaddr_nl addr;
40         NTSTATUS status;
41         int res;
42
43         ctx = talloc(mem_ctx, struct addrchange_context);
44         if (ctx == NULL) {
45                 return NT_STATUS_NO_MEMORY;
46         }
47
48         ctx->sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
49         if (ctx->sock == -1) {
50                 status = map_nt_error_from_unix(errno);
51                 goto fail;
52         }
53         talloc_set_destructor(ctx, addrchange_context_destructor);
54
55         /*
56          * We're interested in address changes
57          */
58         ZERO_STRUCT(addr);
59         addr.nl_family = AF_NETLINK;
60         addr.nl_groups = RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_IFADDR;
61
62         res = bind(ctx->sock, (struct sockaddr *)(void *)&addr, sizeof(addr));
63         if (res == -1) {
64                 status = map_nt_error_from_unix(errno);
65                 goto fail;
66         }
67
68         *pctx = ctx;
69         return NT_STATUS_OK;
70 fail:
71         TALLOC_FREE(ctx);
72         return status;
73 }
74
75 static int addrchange_context_destructor(struct addrchange_context *c)
76 {
77         if (c->sock != -1) {
78                 close(c->sock);
79                 c->sock = -1;
80         }
81         return 0;
82 }
83
84 struct addrchange_state {
85         struct tevent_context *ev;
86         struct addrchange_context *ctx;
87         uint8_t buf[8192];
88         struct sockaddr_storage fromaddr;
89         socklen_t fromaddr_len;
90
91         enum addrchange_type type;
92         struct sockaddr_storage addr;
93 };
94
95 static void addrchange_done(struct tevent_req *subreq);
96
97 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
98                                    struct tevent_context *ev,
99                                    struct addrchange_context *ctx)
100 {
101         struct tevent_req *req, *subreq;
102         struct addrchange_state *state;
103
104         req = tevent_req_create(mem_ctx, &state, struct addrchange_state);
105         if (req == NULL) {
106                 return NULL;
107         }
108         state->ev = ev;
109         state->ctx = ctx;
110
111         state->fromaddr_len = sizeof(state->fromaddr);
112         subreq = recvfrom_send(state, state->ev, state->ctx->sock,
113                                state->buf, sizeof(state->buf), 0,
114                                &state->fromaddr, &state->fromaddr_len);
115         if (tevent_req_nomem(subreq, req)) {
116                 return tevent_req_post(req, state->ev);
117         }
118         tevent_req_set_callback(subreq, addrchange_done, req);
119         return req;
120 }
121
122 static void addrchange_done(struct tevent_req *subreq)
123 {
124         struct tevent_req *req = tevent_req_callback_data(
125                 subreq, struct tevent_req);
126         struct addrchange_state *state = tevent_req_data(
127                 req, struct addrchange_state);
128         struct sockaddr_nl *addr;
129         struct nlmsghdr *h;
130         struct ifaddrmsg *ifa;
131         struct rtattr *rta;
132         ssize_t received;
133         int len;
134         int err;
135         bool found;
136
137         received = recvfrom_recv(subreq, &err);
138         TALLOC_FREE(subreq);
139         if (received == -1) {
140                 DEBUG(10, ("recvfrom returned %s\n", strerror(errno)));
141                 tevent_req_nterror(req, map_nt_error_from_unix(err));
142                 return;
143         }
144         if ((state->fromaddr_len != sizeof(struct sockaddr_nl))
145             || (state->fromaddr.ss_family != AF_NETLINK)) {
146                 DEBUG(10, ("Got message from wrong addr\n"));
147                 goto retry;
148         }
149
150         addr = (struct sockaddr_nl *)(void *)&state->addr;
151         if (addr->nl_pid != 0) {
152                 DEBUG(10, ("Got msg from pid %d, not from the kernel\n",
153                            (int)addr->nl_pid));
154                 goto retry;
155         }
156
157         if (received < sizeof(struct nlmsghdr)) {
158                 DEBUG(10, ("received %d, expected at least %d\n",
159                            (int)received, (int)sizeof(struct nlmsghdr)));
160                 goto retry;
161         }
162
163         h = (struct nlmsghdr *)state->buf;
164         if (h->nlmsg_len < sizeof(struct nlmsghdr)) {
165                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
166                            (int)h->nlmsg_len, (int)sizeof(struct nlmsghdr)));
167                 goto retry;
168         }
169         if (h->nlmsg_len > received) {
170                 DEBUG(10, ("nlmsg_len=%d, expected at most %d\n",
171                            (int)h->nlmsg_len, (int)received));
172                 goto retry;
173         }
174         switch (h->nlmsg_type) {
175         case RTM_NEWADDR:
176                 state->type = ADDRCHANGE_ADD;
177                 break;
178         case RTM_DELADDR:
179                 state->type = ADDRCHANGE_DEL;
180                 break;
181         default:
182                 DEBUG(10, ("Got unexpected type %d - ignoring\n", h->nlmsg_type));
183                 goto retry;
184         }
185
186         if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
187                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
188                            (int)h->nlmsg_len,
189                            (int)(sizeof(struct nlmsghdr)
190                                  +sizeof(struct ifaddrmsg))));
191                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
192                 return;
193         }
194
195         ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
196
197         state->addr.ss_family = ifa->ifa_family;
198
199         rta = IFA_RTA(ifa);
200         len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
201
202         found = false;
203
204         for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
205
206                 if ((rta->rta_type != IFA_LOCAL)
207                     && (rta->rta_type != IFA_ADDRESS)) {
208                         continue;
209                 }
210
211                 switch (ifa->ifa_family) {
212                 case AF_INET: {
213                         struct sockaddr_in *v4_addr;
214                         v4_addr = (struct sockaddr_in *)(void *)&state->addr;
215
216                         if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
217                                 continue;
218                         }
219                         v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
220                         found = true;
221                         break;
222                 }
223                 case AF_INET6: {
224                         struct sockaddr_in6 *v6_addr;
225                         v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
226
227                         if (RTA_PAYLOAD(rta) !=
228                             sizeof(v6_addr->sin6_addr.s6_addr)) {
229                                 continue;
230                         }
231                         memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
232                                sizeof(v6_addr->sin6_addr.s6_addr));
233                         found = true;
234                         break;
235                 }
236                 }
237         }
238
239         if (!found) {
240                 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
241                 return;
242         }
243
244         tevent_req_done(req);
245         return;
246
247 retry:
248         state->fromaddr_len = sizeof(state->fromaddr);
249         subreq = recvfrom_send(state, state->ev, state->ctx->sock,
250                                state->buf, sizeof(state->buf), 0,
251                                &state->fromaddr, &state->fromaddr_len);
252         if (tevent_req_nomem(subreq, req)) {
253                 return;
254         }
255         tevent_req_set_callback(subreq, addrchange_done, req);
256 }
257
258 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
259                          struct sockaddr_storage *addr)
260 {
261         struct addrchange_state *state = tevent_req_data(
262                 req, struct addrchange_state);
263         NTSTATUS status;
264
265         if (tevent_req_is_nterror(req, &status)) {
266                 return status;
267         }
268
269         *type = state->type;
270         *addr = state->addr;
271         return NT_STATUS_OK;
272 }
273
274 #else
275
276 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
277                                    struct addrchange_context **pctx)
278 {
279         return NT_STATUS_NOT_SUPPORTED;
280 }
281
282 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
283                                    struct tevent_context *ev,
284                                    struct addrchange_context *ctx)
285 {
286         return NULL;
287 }
288
289 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
290                          struct sockaddr_storage *addr)
291 {
292         return NT_STATUS_NOT_IMPLEMENTED;
293 }
294
295 #endif