s3: Fix the build on sles8
[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 "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         uint8_t *buf;
32 };
33
34 static int addrchange_context_destructor(struct addrchange_context *c);
35
36 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
37                                    struct addrchange_context **pctx)
38 {
39         struct addrchange_context *ctx;
40         struct sockaddr_nl addr;
41         NTSTATUS status;
42         int res;
43
44         ctx = talloc(mem_ctx, struct addrchange_context);
45         if (ctx == NULL) {
46                 return NT_STATUS_NO_MEMORY;
47         }
48
49         ctx->sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
50         if (ctx->sock == -1) {
51                 status = map_nt_error_from_unix(errno);
52                 goto fail;
53         }
54         talloc_set_destructor(ctx, addrchange_context_destructor);
55
56         /*
57          * We're interested in address changes
58          */
59         ZERO_STRUCT(addr);
60         addr.nl_family = AF_NETLINK;
61         addr.nl_groups = RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_IFADDR;
62
63         res = bind(ctx->sock, (struct sockaddr *)(void *)&addr, sizeof(addr));
64         if (res == -1) {
65                 status = map_nt_error_from_unix(errno);
66                 goto fail;
67         }
68
69         *pctx = ctx;
70         return NT_STATUS_OK;
71 fail:
72         TALLOC_FREE(ctx);
73         return status;
74 }
75
76 static int addrchange_context_destructor(struct addrchange_context *c)
77 {
78         if (c->sock != -1) {
79                 close(c->sock);
80                 c->sock = 0;
81         }
82         return 0;
83 }
84
85 struct addrchange_state {
86         uint8_t buf[8192];
87         struct sockaddr_storage fromaddr;
88         socklen_t fromaddr_len;
89
90         enum addrchange_type type;
91         struct sockaddr_storage addr;
92 };
93
94 static void addrchange_done(struct tevent_req *subreq);
95
96 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
97                                    struct tevent_context *ev,
98                                    struct addrchange_context *ctx)
99 {
100         struct tevent_req *req, *subreq;
101         struct addrchange_state *state;
102
103         req = tevent_req_create(mem_ctx, &state, struct addrchange_state);
104         if (req == NULL) {
105                 return NULL;
106         }
107
108         state->fromaddr_len = sizeof(state->fromaddr);
109
110         subreq = recvfrom_send(state, ev, ctx->sock,
111                                state->buf, sizeof(state->buf), 0,
112                                &state->fromaddr, &state->fromaddr_len);
113         if (tevent_req_nomem(subreq, req)) {
114                 return tevent_req_post(req, ev);
115         }
116         tevent_req_set_callback(subreq, addrchange_done, req);
117         return req;
118 }
119
120 static void addrchange_done(struct tevent_req *subreq)
121 {
122         struct tevent_req *req = tevent_req_callback_data(
123                 subreq, struct tevent_req);
124         struct addrchange_state *state = tevent_req_data(
125                 req, struct addrchange_state);
126         struct nlmsghdr *h;
127         struct ifaddrmsg *ifa;
128         struct rtattr *rta;
129         ssize_t received;
130         int len;
131         int err;
132         bool found;
133
134         received = recvfrom_recv(subreq, &err);
135         TALLOC_FREE(subreq);
136         if (received == -1) {
137                 DEBUG(10, ("recvfrom returned %s\n", strerror(errno)));
138                 tevent_req_nterror(req, map_nt_error_from_unix(err));
139                 return;
140         }
141         if (received < sizeof(struct nlmsghdr)) {
142                 DEBUG(10, ("received %d, expected at least %d\n",
143                            (int)received, (int)sizeof(struct nlmsghdr)));
144                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
145                 return;
146         }
147
148         h = (struct nlmsghdr *)state->buf;
149         if (h->nlmsg_len < sizeof(struct nlmsghdr)) {
150                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
151                            (int)h->nlmsg_len, (int)sizeof(struct nlmsghdr)));
152                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
153                 return;
154         }
155         if (h->nlmsg_len > received) {
156                 DEBUG(10, ("nlmsg_len=%d, expected at most %d\n",
157                            (int)h->nlmsg_len, (int)received));
158                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
159                 return;
160         }
161         switch (h->nlmsg_type) {
162         case RTM_NEWADDR:
163                 state->type = ADDRCHANGE_ADD;
164                 break;
165         case RTM_DELADDR:
166                 state->type = ADDRCHANGE_DEL;
167                 break;
168         default:
169                 DEBUG(10, ("Got unexpected type %d\n", h->nlmsg_type));
170                 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
171                 return;
172         }
173
174         if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
175                 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
176                            (int)h->nlmsg_len,
177                            (int)(sizeof(struct nlmsghdr)
178                                  +sizeof(struct ifaddrmsg))));
179                 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
180                 return;
181         }
182
183         ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
184
185         state->addr.ss_family = ifa->ifa_family;
186
187         rta = IFA_RTA(ifa);
188         len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
189
190         found = false;
191
192         for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
193
194                 if ((rta->rta_type != IFA_LOCAL)
195                     && (rta->rta_type != IFA_ADDRESS)) {
196                         continue;
197                 }
198
199                 switch (ifa->ifa_family) {
200                 case AF_INET: {
201                         struct sockaddr_in *v4_addr;
202                         v4_addr = (struct sockaddr_in *)(void *)&state->addr;
203
204                         if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
205                                 continue;
206                         }
207                         v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
208                         found = true;
209                         break;
210                 }
211                 case AF_INET6: {
212                         struct sockaddr_in6 *v6_addr;
213                         v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
214
215                         if (RTA_PAYLOAD(rta) !=
216                             sizeof(v6_addr->sin6_addr.s6_addr)) {
217                                 continue;
218                         }
219                         memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
220                                sizeof(v6_addr->sin6_addr.s6_addr));
221                         found = true;
222                         break;
223                 }
224                 }
225         }
226
227         if (!found) {
228                 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
229                 return;
230         }
231
232         tevent_req_done(req);
233 }
234
235 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
236                          struct sockaddr_storage *addr)
237 {
238         struct addrchange_state *state = tevent_req_data(
239                 req, struct addrchange_state);
240         NTSTATUS status;
241
242         if (tevent_req_is_nterror(req, &status)) {
243                 return status;
244         }
245
246         *type = state->type;
247         *addr = state->addr;
248         return NT_STATUS_OK;
249 }
250
251 #else
252
253 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
254                                    struct addrchange_context **pctx)
255 {
256         return NT_STATUS_NOT_SUPPORTED;
257 }
258
259 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
260                                    struct tevent_context *ev,
261                                    struct addrchange_context *ctx)
262 {
263         return NULL;
264 }
265
266 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
267                          struct sockaddr_storage *addr)
268 {
269         return NT_STATUS_NOT_IMPLEMENTED;
270 }
271
272 #endif