Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | /* * Copyright (c) 2024 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(net_dns_dispatcher, CONFIG_DNS_SOCKET_DISPATCHER_LOG_LEVEL); #include <zephyr/kernel.h> #include <zephyr/sys/check.h> #include <zephyr/sys/slist.h> #include <zephyr/net_buf.h> #include <zephyr/net/net_if.h> #include <zephyr/net/dns_resolve.h> #include <zephyr/net/socket_service.h> #include "../../ip/net_stats.h" #include "dns_pack.h" static K_MUTEX_DEFINE(lock); static sys_slist_t sockets; #define DNS_RESOLVER_MIN_BUF 1 #define DNS_RESOLVER_BUF_CTR (DNS_RESOLVER_MIN_BUF + \ CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR) NET_BUF_POOL_DEFINE(dns_msg_pool, DNS_RESOLVER_BUF_CTR, DNS_RESOLVER_MAX_BUF_SIZE, 0, NULL); static struct socket_dispatch_table { struct dns_socket_dispatcher *ctx; } dispatch_table[CONFIG_ZVFS_OPEN_MAX]; static int dns_dispatch(struct dns_socket_dispatcher *dispatcher, int sock, struct sockaddr *addr, size_t addrlen, struct net_buf *dns_data, size_t buf_len) { /* Helper struct to track the dns msg received from the server */ struct dns_msg_t dns_msg; bool is_query; int data_len; int ret; data_len = MIN(buf_len, DNS_RESOLVER_MAX_BUF_SIZE); dns_msg.msg = dns_data->data; dns_msg.msg_size = data_len; /* Make sure that we can read DNS id, flags and rcode */ if (dns_msg.msg_size < (sizeof(uint16_t) + sizeof(uint16_t))) { ret = -EINVAL; goto done; } if (dns_header_rcode(dns_msg.msg) == DNS_HEADER_REFUSED) { ret = -EINVAL; goto done; } is_query = (dns_header_qr(dns_msg.msg) == DNS_QUERY); if (is_query) { if (dispatcher->type == DNS_SOCKET_RESPONDER) { /* Call the responder callback */ ret = dispatcher->cb(dispatcher->ctx, sock, addr, addrlen, dns_data, data_len); } else if (dispatcher->pair) { ret = dispatcher->pair->cb(dispatcher->pair->ctx, sock, addr, addrlen, dns_data, data_len); } else { /* Discard the message as it was a query and there are none * expecting a query. */ ret = -ENOENT; } } else { /* So this was an answer to a query that was made by resolver. */ if (dispatcher->type == DNS_SOCKET_RESOLVER) { /* Call the resolver callback */ ret = dispatcher->cb(dispatcher->ctx, sock, addr, addrlen, dns_data, data_len); } else if (dispatcher->pair) { ret = dispatcher->pair->cb(dispatcher->pair->ctx, sock, addr, addrlen, dns_data, data_len); } else { /* Discard the message as it was not a query reply and * we were a reply. */ ret = -ENOENT; } } done: if (IS_ENABLED(CONFIG_NET_STATISTICS_DNS)) { struct net_if *iface = NULL; if (IS_ENABLED(CONFIG_NET_IPV6) && addr->sa_family == AF_INET6) { iface = net_if_ipv6_select_src_iface(&net_sin6(addr)->sin6_addr); } else if (IS_ENABLED(CONFIG_NET_IPV4) && addr->sa_family == AF_INET) { iface = net_if_ipv4_select_src_iface(&net_sin(addr)->sin_addr); } if (iface != NULL) { if (ret < 0) { net_stats_update_dns_drop(iface); } else { net_stats_update_dns_recv(iface); } } } return ret; } static int recv_data(struct net_socket_service_event *pev) { struct socket_dispatch_table *table = pev->user_data; struct dns_socket_dispatcher *dispatcher; socklen_t optlen = sizeof(int); struct net_buf *dns_data = NULL; struct sockaddr addr; size_t addrlen; int family, sock_error; int ret = 0, len; dispatcher = table[pev->event.fd].ctx; k_mutex_lock(&dispatcher->lock, K_FOREVER); (void)zsock_getsockopt(pev->event.fd, SOL_SOCKET, SO_DOMAIN, &family, &optlen); if ((pev->event.revents & ZSOCK_POLLERR) || (pev->event.revents & ZSOCK_POLLNVAL)) { (void)zsock_getsockopt(pev->event.fd, SOL_SOCKET, SO_ERROR, &sock_error, &optlen); if (sock_error > 0) { NET_ERR("Receiver IPv%d socket error (%d)", family == AF_INET ? 4 : 6, sock_error); ret = DNS_EAI_SYSTEM; } goto unlock; } addrlen = (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); dns_data = net_buf_alloc(&dns_msg_pool, dispatcher->buf_timeout); if (!dns_data) { ret = DNS_EAI_MEMORY; goto unlock; } ret = zsock_recvfrom(pev->event.fd, dns_data->data, net_buf_max_len(dns_data), 0, (struct sockaddr *)&addr, &addrlen); if (ret < 0) { ret = -errno; NET_ERR("recv failed on IPv%d socket (%d)", family == AF_INET ? 4 : 6, -ret); goto free_buf; } len = ret; ret = dns_dispatch(dispatcher, pev->event.fd, (struct sockaddr *)&addr, addrlen, dns_data, len); free_buf: if (dns_data) { net_buf_unref(dns_data); } unlock: k_mutex_unlock(&dispatcher->lock); return ret; } void dns_dispatcher_svc_handler(struct net_socket_service_event *pev) { int ret; ret = recv_data(pev); if (ret < 0 && ret != DNS_EAI_ALLDONE && ret != -ENOENT) { NET_ERR("DNS recv error (%d)", ret); } } int dns_dispatcher_register(struct dns_socket_dispatcher *ctx) { struct dns_socket_dispatcher *entry, *next, *found = NULL; sys_snode_t *prev_node = NULL; bool dup = false; size_t addrlen; int ret = 0; k_mutex_lock(&lock, K_FOREVER); if (sys_slist_find(&sockets, &ctx->node, &prev_node)) { ret = -EALREADY; goto out; } SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&sockets, entry, next, node) { /* Refuse to register context if we have identical context * already registered. */ if (ctx->type == entry->type && ctx->local_addr.sa_family == entry->local_addr.sa_family && ctx->ifindex == entry->ifindex) { if (net_sin(&entry->local_addr)->sin_port == net_sin(&ctx->local_addr)->sin_port) { dup = true; continue; } } /* Then check if there is an entry with same family and * port already in the list. If there is then we can act * as a dispatcher for the given socket. Do not break * from the loop even if we found an entry so that we * can catch possible duplicates. */ if (found == NULL && ctx->type != entry->type && ctx->local_addr.sa_family == entry->local_addr.sa_family) { if (net_sin(&entry->local_addr)->sin_port == net_sin(&ctx->local_addr)->sin_port) { found = entry; continue; } } } if (dup) { /* Found a duplicate */ ret = -EALREADY; goto out; } if (found != NULL) { entry = found; if (entry->pair != NULL) { NET_DBG("Already paired connection found."); ret = -EALREADY; goto out; } entry->pair = ctx; for (int i = 0; i < ctx->fds_len; i++) { CHECKIF((int)ctx->fds[i].fd >= (int)ARRAY_SIZE(dispatch_table)) { ret = -ERANGE; goto out; } if (ctx->fds[i].fd < 0) { continue; } if (dispatch_table[ctx->fds[i].fd].ctx == NULL) { dispatch_table[ctx->fds[i].fd].ctx = ctx; } } /* Basically we are now done. If there is incoming data to * the socket, the dispatcher will then pass it to the correct * recipient. */ ret = 0; goto out; } ctx->buf_timeout = DNS_BUF_TIMEOUT; if (ctx->local_addr.sa_family == AF_INET) { addrlen = sizeof(struct sockaddr_in); } else { addrlen = sizeof(struct sockaddr_in6); } /* Bind and then register a socket service with this combo */ ret = zsock_bind(ctx->sock, &ctx->local_addr, addrlen); if (ret < 0) { ret = -errno; NET_DBG("Cannot bind DNS socket %d (%d)", ctx->sock, ret); goto out; } ctx->pair = NULL; for (int i = 0; i < ctx->fds_len; i++) { if ((int)ctx->fds[i].fd >= (int)ARRAY_SIZE(dispatch_table)) { ret = -ERANGE; goto out; } if (ctx->fds[i].fd < 0) { continue; } if (dispatch_table[ctx->fds[i].fd].ctx == NULL) { dispatch_table[ctx->fds[i].fd].ctx = ctx; } } ret = net_socket_service_register(ctx->svc, ctx->fds, ctx->fds_len, &dispatch_table); if (ret < 0) { NET_DBG("Cannot register socket service (%d)", ret); goto out; } sys_slist_prepend(&sockets, &ctx->node); out: k_mutex_unlock(&lock); return ret; } int dns_dispatcher_unregister(struct dns_socket_dispatcher *ctx) { int ret = 0; k_mutex_lock(&lock, K_FOREVER); (void)sys_slist_find_and_remove(&sockets, &ctx->node); for (int i = 0; i < ctx->fds_len; i++) { CHECKIF((int)ctx->fds[i].fd >= (int)ARRAY_SIZE(dispatch_table)) { ret = -ERANGE; goto out; } dispatch_table[ctx->fds[i].fd].ctx = NULL; } out: k_mutex_unlock(&lock); return ret; } void dns_dispatcher_init(void) { sys_slist_init(&sockets); } |