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 {
33 static int addrchange_context_destructor(struct addrchange_context *c);
35 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
36 struct addrchange_context **pctx)
38 struct addrchange_context *ctx;
39 struct sockaddr_nl addr;
43 ctx = talloc(mem_ctx, struct addrchange_context);
45 return NT_STATUS_NO_MEMORY;
48 ctx->sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
49 if (ctx->sock == -1) {
50 status = map_nt_error_from_unix(errno);
53 talloc_set_destructor(ctx, addrchange_context_destructor);
56 * We're interested in address changes
59 addr.nl_family = AF_NETLINK;
60 addr.nl_groups = RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_IFADDR;
62 res = bind(ctx->sock, (struct sockaddr *)(void *)&addr, sizeof(addr));
64 status = map_nt_error_from_unix(errno);
75 static int addrchange_context_destructor(struct addrchange_context *c)
84 struct addrchange_state {
86 struct sockaddr_storage fromaddr;
87 socklen_t fromaddr_len;
89 enum addrchange_type type;
90 struct sockaddr_storage addr;
93 static void addrchange_done(struct tevent_req *subreq);
95 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
96 struct tevent_context *ev,
97 struct addrchange_context *ctx)
99 struct tevent_req *req, *subreq;
100 struct addrchange_state *state;
102 req = tevent_req_create(mem_ctx, &state, struct addrchange_state);
107 state->fromaddr_len = sizeof(state->fromaddr);
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);
115 tevent_req_set_callback(subreq, addrchange_done, req);
119 static void addrchange_done(struct tevent_req *subreq)
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);
126 struct ifaddrmsg *ifa;
133 received = recvfrom_recv(subreq, &err);
135 if (received == -1) {
136 DEBUG(10, ("recvfrom returned %s\n", strerror(errno)));
137 tevent_req_nterror(req, map_nt_error_from_unix(err));
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);
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);
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);
160 switch (h->nlmsg_type) {
162 state->type = ADDRCHANGE_ADD;
165 state->type = ADDRCHANGE_DEL;
168 DEBUG(10, ("Got unexpected type %d\n", h->nlmsg_type));
169 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
173 if (h->nlmsg_len < sizeof(struct nlmsghdr)+sizeof(struct ifaddrmsg)) {
174 DEBUG(10, ("nlmsg_len=%d, expected at least %d\n",
176 (int)(sizeof(struct nlmsghdr)
177 +sizeof(struct ifaddrmsg))));
178 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
182 ifa = (struct ifaddrmsg *)NLMSG_DATA(h);
184 state->addr.ss_family = ifa->ifa_family;
187 len = h->nlmsg_len - sizeof(struct nlmsghdr) + sizeof(struct ifaddrmsg);
191 for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
193 if ((rta->rta_type != IFA_LOCAL)
194 && (rta->rta_type != IFA_ADDRESS)) {
198 switch (ifa->ifa_family) {
200 struct sockaddr_in *v4_addr;
201 v4_addr = (struct sockaddr_in *)(void *)&state->addr;
203 if (RTA_PAYLOAD(rta) != sizeof(uint32_t)) {
206 v4_addr->sin_addr.s_addr = *(uint32_t *)RTA_DATA(rta);
211 struct sockaddr_in6 *v6_addr;
212 v6_addr = (struct sockaddr_in6 *)(void *)&state->addr;
214 if (RTA_PAYLOAD(rta) !=
215 sizeof(v6_addr->sin6_addr.s6_addr)) {
218 memcpy(v6_addr->sin6_addr.s6_addr, RTA_DATA(rta),
219 sizeof(v6_addr->sin6_addr.s6_addr));
227 tevent_req_nterror(req, NT_STATUS_INVALID_ADDRESS);
231 tevent_req_done(req);
234 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
235 struct sockaddr_storage *addr)
237 struct addrchange_state *state = tevent_req_data(
238 req, struct addrchange_state);
241 if (tevent_req_is_nterror(req, &status)) {
252 NTSTATUS addrchange_context_create(TALLOC_CTX *mem_ctx,
253 struct addrchange_context **pctx)
255 return NT_STATUS_NOT_SUPPORTED;
258 struct tevent_req *addrchange_send(TALLOC_CTX *mem_ctx,
259 struct tevent_context *ev,
260 struct addrchange_context *ctx)
265 NTSTATUS addrchange_recv(struct tevent_req *req, enum addrchange_type *type,
266 struct sockaddr_storage *addr)
268 return NT_STATUS_NOT_IMPLEMENTED;