From 4ef1eb92edcd6f93a743e5bb801834250ce1a8ca Mon Sep 17 00:00:00 2001 From: Vinit Agnihotri Date: Wed, 28 Feb 2024 03:56:23 -0800 Subject: [PATCH] smbd-server: Handle ip drop event and close listening socket Signed-off-by: Vinit Agnihotri Reviewed-by: Martin Schwenke Reviewed-by: Andrew Bartlett --- source3/smbd/server.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/source3/smbd/server.c b/source3/smbd/server.c index ed0f7746966..aba980e9375 100644 --- a/source3/smbd/server.c +++ b/source3/smbd/server.c @@ -1639,6 +1639,33 @@ static void smbd_init_addrchange(TALLOC_CTX *mem_ctx, tevent_req_set_callback(req, smbd_addr_changed, state); } +static void smbd_close_socket_for_ip(struct smbd_parent_context *parent, + struct sockaddr_storage *addr) +{ + struct smbd_open_socket *s = NULL; + + for (s = parent->sockets; s != NULL; s = s->next) { + struct sockaddr_storage a; + socklen_t addr_len = sizeof(a); + + if (getsockname(s->fd, (struct sockaddr *)&a, &addr_len) < 0) { + DBG_NOTICE("smbd: Unable to get address - skip\n"); + continue; + } + if (sockaddr_equal((struct sockaddr *)&a, + (struct sockaddr *)addr)) { + char addrstr[INET6_ADDRSTRLEN]; + + DLIST_REMOVE(parent->sockets, s); + TALLOC_FREE(s); + print_sockaddr(addrstr, sizeof(addrstr), addr); + DBG_NOTICE("smbd: Closed listening socket for %s\n", + addrstr); + return; + } + } +} + static void smbd_addr_changed(struct tevent_req *req) { struct smbd_addrchanged_state *state = tevent_req_callback_data( @@ -1666,6 +1693,8 @@ static void smbd_addr_changed(struct tevent_req *req) "on if_index %u\n", addrstr, if_index); + smbd_close_socket_for_ip(state->parent, &addr); + goto rearm; }