18269fefcc518e1e76250ad687ec47d4a806d4df
[obnox/samba-ctdb.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 "linux/netlink.h"
25 #include "linux/rtnetlink.h"
26 #include "lib/async_req/async_sock.h"
27
28 struct addrchange_context {
29         int sock;
30         uint8_t *buf;
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 = 0;
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 nlmsghdr *h;
129         struct ifaddrmsg *ifa;
130         struct rtattr *rta;
131         ssize_t received;
132         int len;
133         int err;
134         bool found;
135
136         received = recvfrom_recv(subreq, &err);
137         TALLOC_FREE(subreq);
138         if (received == -1) {
139                 DEBUG(10, ("recvfrom returned %s\n", strerror(errno)));
140                 tevent_req_nterror(req, map_nt_error_from_unix(err));
141                 return;
142         }
143         if (received < sizeof(struct nlmsghdr)) {
144                 DEBUG(10, ("received %d, expected at least %d\n",
145                            (int)received, (int)sizeof(struct nlmsghdr)));
146                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
147                 return;
148         }
149
150         h = (struct nlmsghdr *)state->buf;
151         if (h->nlmsg_len < sizeof(struct nlmsghdr)) {
152                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
153                            (int)h->nlmsg_len, (int)sizeof(struct nlmsghdr)));
154                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
155                 return;
156         }
157         if (h->nlmsg_len > received) {
158                 DEBUG(10, ("nlmsg_len=%d, expected at most %d\n",
159                            (int)h->nlmsg_len, (int)received));
160                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
161                 return;
162         }
163         switch (h->nlmsg_type) {
164         case RTM_NEWADDR:
165                 state->type = ADDRCHANGE_ADD;
166                 break;
167         case RTM_DELADDR:
168                 state->type = ADDRCHANGE_DEL;
169                 break;
170         default:
171                 DEBUG(10, ("Got unexpected type %d - ignoring\n", h->nlmsg_type));
172
173                 state->fromaddr_len = sizeof(state->fromaddr);
174                 subreq = recvfrom_send(state, state->ev, state->ctx->sock,
175                                state->buf, sizeof(state->buf), 0,
176                                &state->fromaddr, &state->fromaddr_len);
177                 if (tevent_req_nomem(subreq, req)) {
178                         return;
179                 }
180                 tevent_req_set_callback(subreq, addrchange_done, req);
181                 return;
182         }
183
184         if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
185                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
186                            (int)h->nlmsg_len,
187                            (int)(sizeof(struct nlmsghdr)
188                                  +sizeof(struct ifaddrmsg))));
189                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
190                 return;
191         }
192
193         ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
194
195         state->addr.ss_family = ifa->ifa_family;
196
197         rta = IFA_RTA(ifa);
198         len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
199
200         found = false;
201
202         for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
203
204                 if ((rta->rta_type != IFA_LOCAL)
205                     && (rta->rta_type != IFA_ADDRESS)) {
206                         continue;
207                 }
208
209                 switch (ifa->ifa_family) {
210                 case AF_INET: {
211                         struct sockaddr_in *v4_addr;
212                         v4_addr = (struct sockaddr_in *)(void *)&state->addr;
213
214                         if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
215                                 continue;
216                         }
217                         v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
218                         found = true;
219                         break;
220                 }
221                 case AF_INET6: {
222                         struct sockaddr_in6 *v6_addr;
223                         v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
224
225                         if (RTA_PAYLOAD(rta) !=
226                             sizeof(v6_addr->sin6_addr.s6_addr)) {
227                                 continue;
228                         }
229                         memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
230                                sizeof(v6_addr->sin6_addr.s6_addr));
231                         found = true;
232                         break;
233                 }
234                 }
235         }
236
237         if (!found) {
238                 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
239                 return;
240         }
241
242         tevent_req_done(req);
243 }
244
245 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
246                          struct sockaddr_storage *addr)
247 {
248         struct addrchange_state *state = tevent_req_data(
249                 req, struct addrchange_state);
250         NTSTATUS status;
251
252         if (tevent_req_is_nterror(req, &status)) {
253                 return status;
254         }
255
256         *type = state->type;
257         *addr = state->addr;
258         return NT_STATUS_OK;
259 }
260
261 #else
262
263 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
264                                    struct addrchange_context **pctx)
265 {
266         return NT_STATUS_NOT_SUPPORTED;
267 }
268
269 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
270                                    struct tevent_context *ev,
271                                    struct addrchange_context *ctx)
272 {
273         return NULL;
274 }
275
276 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
277                          struct sockaddr_storage *addr)
278 {
279         return NT_STATUS_NOT_IMPLEMENTED;
280 }
281
282 #endif