s3: Add support for AF_NETLINK addr notifications
[metze/samba/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 "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         uint8_t buf[8192];
86         struct sockaddr_storage fromaddr;
87         socklen_t fromaddr_len;
88
89         enum addrchange_type type;
90         struct sockaddr_storage addr;
91 };
92
93 static void addrchange_done(struct tevent_req *subreq);
94
95 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
96                                    struct tevent_context *ev,
97                                    struct addrchange_context *ctx)
98 {
99         struct tevent_req *req, *subreq;
100         struct addrchange_state *state;
101
102         req = tevent_req_create(mem_ctx, &state, struct addrchange_state);
103         if (req == NULL) {
104                 return NULL;
105         }
106
107         state->fromaddr_len = sizeof(state->fromaddr);
108
109         subreq = recvfrom_send(state, ev, ctx->sock,
110                                state->buf, sizeof(state->buf), 0,
111                                &state->fromaddr, &state->fromaddr_len);
112         if (tevent_req_nomem(subreq, req)) {
113                 return tevent_req_post(req, ev);
114         }
115         tevent_req_set_callback(subreq, addrchange_done, req);
116         return req;
117 }
118
119 static void addrchange_done(struct tevent_req *subreq)
120 {
121         struct tevent_req *req = tevent_req_callback_data(
122                 subreq, struct tevent_req);
123         struct addrchange_state *state = tevent_req_data(
124                 req, struct addrchange_state);
125         struct nlmsghdr *h;
126         struct ifaddrmsg *ifa;
127         struct rtattr *rta;
128         ssize_t received;
129         int len;
130         int err;
131         bool found;
132
133         received = recvfrom_recv(subreq, &err);
134         TALLOC_FREE(subreq);
135         if (received == -1) {
136                 DEBUG(10, ("recvfrom returned %s\n", strerror(errno)));
137                 tevent_req_nterror(req, map_nt_error_from_unix(err));
138                 return;
139         }
140         if (received < sizeof(struct nlmsghdr)) {
141                 DEBUG(10, ("received %d, expected at least %d\n",
142                            (int)received, (int)sizeof(struct nlmsghdr)));
143                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
144                 return;
145         }
146
147         h = (struct nlmsghdr *)state->buf;
148         if (h->nlmsg_len < sizeof(struct nlmsghdr)) {
149                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
150                            (int)h->nlmsg_len, (int)sizeof(struct nlmsghdr)));
151                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
152                 return;
153         }
154         if (h->nlmsg_len > received) {
155                 DEBUG(10, ("nlmsg_len=%d, expected at most %d\n",
156                            (int)h->nlmsg_len, (int)received));
157                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
158                 return;
159         }
160         switch (h->nlmsg_type) {
161         case RTM_NEWADDR:
162                 state->type = ADDRCHANGE_ADD;
163                 break;
164         case RTM_DELADDR:
165                 state->type = ADDRCHANGE_DEL;
166                 break;
167         default:
168                 DEBUG(10, ("Got unexpected type %d\n", h->nlmsg_type));
169                 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
170                 return;
171         }
172
173         if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
174                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
175                            (int)h->nlmsg_len,
176                            (int)(sizeof(struct nlmsghdr)
177                                  +sizeof(struct ifaddrmsg))));
178                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
179                 return;
180         }
181
182         ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
183
184         state->addr.ss_family = ifa->ifa_family;
185
186         rta = IFA_RTA(ifa);
187         len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
188
189         found = false;
190
191         for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
192
193                 if ((rta->rta_type != IFA_LOCAL)
194                     && (rta->rta_type != IFA_ADDRESS)) {
195                         continue;
196                 }
197
198                 switch (ifa->ifa_family) {
199                 case AF_INET: {
200                         struct sockaddr_in *v4_addr;
201                         v4_addr = (struct sockaddr_in *)(void *)&state->addr;
202
203                         if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
204                                 continue;
205                         }
206                         v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
207                         found = true;
208                         break;
209                 }
210                 case AF_INET6: {
211                         struct sockaddr_in6 *v6_addr;
212                         v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
213
214                         if (RTA_PAYLOAD(rta) !=
215                             sizeof(v6_addr->sin6_addr.s6_addr)) {
216                                 continue;
217                         }
218                         memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
219                                sizeof(v6_addr->sin6_addr.s6_addr));
220                         found = true;
221                         break;
222                 }
223                 }
224         }
225
226         if (!found) {
227                 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
228                 return;
229         }
230
231         tevent_req_done(req);
232 }
233
234 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
235                          struct sockaddr_storage *addr)
236 {
237         struct addrchange_state *state = tevent_req_data(
238                 req, struct addrchange_state);
239         NTSTATUS status;
240
241         if (tevent_req_is_nterror(req, &status)) {
242                 return status;
243         }
244
245         *type = state->type;
246         *addr = state->addr;
247         return NT_STATUS_OK;
248 }
249
250 #else
251
252 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
253                                    struct addrchange_context **pctx)
254 {
255         return NT_STATUS_NOT_SUPPORTED;
256 }
257
258 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
259                                    struct tevent_context *ev,
260                                    struct addrchange_context *ctx)
261 {
262         return NULL;
263 }
264
265 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
266                          struct sockaddr_storage *addr)
267 {
268         return NT_STATUS_NOT_IMPLEMENTED;
269 }
270
271 #endif