2 * Samba Unix/Linux SMB client library
3 * Copyright (C) Volker Lendecke 2011
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.
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.
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/>.
20 #include "lib/addrchange.h"
22 #if HAVE_LINUX_RTNETLINK_H
24 #include "linux/netlink.h"
25 #include "linux/rtnetlink.h"
26 #include "lib/async_req/async_sock.h"
28 struct addrchange_context {
32 static int addrchange_context_destructor(struct addrchange_context *c);
34 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
35 struct addrchange_context **pctx)
37 struct addrchange_context *ctx;
38 struct sockaddr_nl addr;
42 ctx = talloc(mem_ctx, struct addrchange_context);
44 return NT_STATUS_NO_MEMORY;
47 ctx->sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
48 if (ctx->sock == -1) {
49 status = map_nt_error_from_unix(errno);
52 talloc_set_destructor(ctx, addrchange_context_destructor);
55 * We're interested in address changes
58 addr.nl_family = AF_NETLINK;
59 addr.nl_groups = RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_IFADDR;
61 res = bind(ctx->sock, (struct sockaddr *)(void *)&addr, sizeof(addr));
63 status = map_nt_error_from_unix(errno);
74 static int addrchange_context_destructor(struct addrchange_context *c)
83 struct addrchange_state {
84 struct tevent_context *ev;
85 struct addrchange_context *ctx;
87 struct sockaddr_storage fromaddr;
88 socklen_t fromaddr_len;
90 enum addrchange_type type;
91 struct sockaddr_storage addr;
94 static void addrchange_done(struct tevent_req *subreq);
96 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
97 struct tevent_context *ev,
98 struct addrchange_context *ctx)
100 struct tevent_req *req, *subreq;
101 struct addrchange_state *state;
103 req = tevent_req_create(mem_ctx, &state, struct addrchange_state);
110 state->fromaddr_len = sizeof(state->fromaddr);
111 subreq = recvfrom_send(state, state->ev, state->ctx->sock,
112 state->buf, sizeof(state->buf), 0,
113 &state->fromaddr, &state->fromaddr_len);
114 if (tevent_req_nomem(subreq, req)) {
115 return tevent_req_post(req, state->ev);
117 tevent_req_set_callback(subreq, addrchange_done, req);
121 static void addrchange_done(struct tevent_req *subreq)
123 struct tevent_req *req = tevent_req_callback_data(
124 subreq, struct tevent_req);
125 struct addrchange_state *state = tevent_req_data(
126 req, struct addrchange_state);
127 struct sockaddr_nl *addr;
129 struct ifaddrmsg *ifa;
136 received = recvfrom_recv(subreq, &err);
138 if (received == -1) {
139 DEBUG(10, ("recvfrom returned %s\n", strerror(errno)));
140 tevent_req_nterror(req, map_nt_error_from_unix(err));
143 if ((state->fromaddr_len != sizeof(struct sockaddr_nl))
144 || (state->fromaddr.ss_family != AF_NETLINK)) {
145 DEBUG(10, ("Got message from wrong addr\n"));
149 addr = (struct sockaddr_nl *)(void *)&state->addr;
150 if (addr->nl_pid != 0) {
151 DEBUG(10, ("Got msg from pid %d, not from the kernel\n",
156 if (received < sizeof(struct nlmsghdr)) {
157 DEBUG(10, ("received %d, expected at least %d\n",
158 (int)received, (int)sizeof(struct nlmsghdr)));
162 h = (struct nlmsghdr *)state->buf;
163 if (h->nlmsg_len < sizeof(struct nlmsghdr)) {
164 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
165 (int)h->nlmsg_len, (int)sizeof(struct nlmsghdr)));
168 if (h->nlmsg_len > received) {
169 DEBUG(10, ("nlmsg_len=%d, expected at most %d\n",
170 (int)h->nlmsg_len, (int)received));
173 switch (h->nlmsg_type) {
175 state->type = ADDRCHANGE_ADD;
178 state->type = ADDRCHANGE_DEL;
181 DEBUG(10, ("Got unexpected type %d - ignoring\n", h->nlmsg_type));
185 if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
186 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
188 (int)(sizeof(struct nlmsghdr)
189 +sizeof(struct ifaddrmsg))));
190 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
194 ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
196 state->addr.ss_family = ifa->ifa_family;
199 len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
203 for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
205 if ((rta->rta_type != IFA_LOCAL)
206 && (rta->rta_type != IFA_ADDRESS)) {
210 switch (ifa->ifa_family) {
212 struct sockaddr_in *v4_addr;
213 v4_addr = (struct sockaddr_in *)(void *)&state->addr;
215 if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
218 v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
223 struct sockaddr_in6 *v6_addr;
224 v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
226 if (RTA_PAYLOAD(rta) !=
227 sizeof(v6_addr->sin6_addr.s6_addr)) {
230 memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
231 sizeof(v6_addr->sin6_addr.s6_addr));
239 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
243 tevent_req_done(req);
247 state->fromaddr_len = sizeof(state->fromaddr);
248 subreq = recvfrom_send(state, state->ev, state->ctx->sock,
249 state->buf, sizeof(state->buf), 0,
250 &state->fromaddr, &state->fromaddr_len);
251 if (tevent_req_nomem(subreq, req)) {
254 tevent_req_set_callback(subreq, addrchange_done, req);
257 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
258 struct sockaddr_storage *addr)
260 struct addrchange_state *state = tevent_req_data(
261 req, struct addrchange_state);
264 if (tevent_req_is_nterror(req, &status)) {
275 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
276 struct addrchange_context **pctx)
278 return NT_STATUS_NOT_SUPPORTED;
281 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
282 struct tevent_context *ev,
283 struct addrchange_context *ctx)
288 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
289 struct sockaddr_storage *addr)
291 return NT_STATUS_NOT_IMPLEMENTED;