| 1 |
/* IPV4 base transport support functions |
| 2 |
* |
| 3 |
* Portions of this file are subject to the following copyright(s). See |
| 4 |
* the Net-SNMP's COPYING file for more details and other copyrights |
| 5 |
* that may apply: |
| 6 |
* |
| 7 |
* Portions of this file are copyrighted by: |
| 8 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 9 |
* Use is subject to license terms specified in the COPYING file |
| 10 |
* distributed with the Net-SNMP package. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <net-snmp/net-snmp-config.h> |
| 14 |
|
| 15 |
#include <net-snmp/types.h> |
| 16 |
#include "snmpIPBaseDomain.h" |
| 17 |
#include <net-snmp/library/snmpUDPIPv4BaseDomain.h> |
| 18 |
|
| 19 |
#include <stddef.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <ctype.h> |
| 23 |
#ifdef HAVE_STDLIB_H |
| 24 |
#include <stdlib.h> |
| 25 |
#endif |
| 26 |
#ifdef HAVE_STRING_H |
| 27 |
#include <string.h> |
| 28 |
#else |
| 29 |
#include <strings.h> |
| 30 |
#endif |
| 31 |
#ifdef HAVE_SYS_SOCKET_H |
| 32 |
#include <sys/socket.h> |
| 33 |
#endif |
| 34 |
#ifdef HAVE_NETINET_IN_H |
| 35 |
#include <netinet/in.h> |
| 36 |
#endif |
| 37 |
#ifdef HAVE_ARPA_INET_H |
| 38 |
#include <arpa/inet.h> |
| 39 |
#endif |
| 40 |
#ifdef HAVE_NETDB_H |
| 41 |
#include <netdb.h> |
| 42 |
#endif |
| 43 |
#include <errno.h> |
| 44 |
|
| 45 |
#include <net-snmp/types.h> |
| 46 |
#include <net-snmp/library/snmp_debug.h> |
| 47 |
#include <net-snmp/library/tools.h> |
| 48 |
#include <net-snmp/library/snmp_assert.h> |
| 49 |
#include <net-snmp/library/default_store.h> |
| 50 |
#include <net-snmp/library/snmp_transport.h> |
| 51 |
|
| 52 |
#include <net-snmp/library/snmpSocketBaseDomain.h> |
| 53 |
|
| 54 |
#ifndef NETSNMP_NO_SYSTEMD |
| 55 |
#include <net-snmp/library/sd-daemon.h> |
| 56 |
#endif |
| 57 |
|
| 58 |
#if defined(HAVE_IP_PKTINFO) || (defined(HAVE_IP_RECVDSTADDR) && defined(HAVE_IP_SENDSRCADDR)) |
| 59 |
int netsnmp_udpipv4_recvfrom(int s, void *buf, int len, struct sockaddr *from, |
| 60 |
socklen_t *fromlen, struct sockaddr *dstip, |
| 61 |
socklen_t *dstlen, int *if_index) |
| 62 |
{ |
| 63 |
return netsnmp_udpbase_recvfrom(s, buf, len, from, fromlen, dstip, dstlen, |
| 64 |
if_index); |
| 65 |
} |
| 66 |
|
| 67 |
int netsnmp_udpipv4_sendto(int fd, const struct in_addr *srcip, int if_index, |
| 68 |
const struct sockaddr *remote, const void *data, |
| 69 |
int len) |
| 70 |
{ |
| 71 |
return netsnmp_udpbase_sendto(fd, srcip, if_index, remote, data, len); |
| 72 |
} |
| 73 |
#endif /* HAVE_IP_PKTINFO || HAVE_IP_RECVDSTADDR */ |
| 74 |
|
| 75 |
netsnmp_transport * |
| 76 |
netsnmp_udpipv4base_transport_init(const struct netsnmp_ep *ep, int local) |
| 77 |
{ |
| 78 |
netsnmp_transport *t; |
| 79 |
const struct sockaddr_in *addr = &ep->a.sin; |
| 80 |
u_char *addr_ptr; |
| 81 |
|
| 82 |
if (addr == NULL || addr->sin_family != AF_INET) { |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
t = SNMP_MALLOC_TYPEDEF(netsnmp_transport); |
| 87 |
if (NULL == t) |
| 88 |
return NULL; |
| 89 |
|
| 90 |
t->sock = -1; |
| 91 |
|
| 92 |
addr_ptr = netsnmp_memdup(addr, sizeof(*addr)); |
| 93 |
if (NULL == addr_ptr) { |
| 94 |
free(t); |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
|
| 98 |
if (local) { |
| 99 |
/** This is a server session. */ |
| 100 |
t->local_length = sizeof(*addr); |
| 101 |
t->local = addr_ptr; |
| 102 |
} else { |
| 103 |
/** This is a client session. */ |
| 104 |
t->remote = addr_ptr; |
| 105 |
t->remote_length = sizeof(*addr); |
| 106 |
} |
| 107 |
|
| 108 |
DEBUGIF("netsnmp_udpbase") { |
| 109 |
netsnmp_indexed_addr_pair addr_pair; |
| 110 |
char *str; |
| 111 |
memset(&addr_pair, 0, sizeof(netsnmp_indexed_addr_pair)); |
| 112 |
memcpy(&(addr_pair.remote_addr), addr, sizeof(*addr)); |
| 113 |
str = netsnmp_udp_fmtaddr(NULL, (void *)&addr_pair, |
| 114 |
sizeof(netsnmp_indexed_addr_pair)); |
| 115 |
DEBUGMSGTL(("netsnmp_udpbase", "open %s %s\n", |
| 116 |
local ? "local" : "remote", str)); |
| 117 |
free(str); |
| 118 |
} |
| 119 |
|
| 120 |
if (!local) { |
| 121 |
netsnmp_indexed_addr_pair *addr_pair; |
| 122 |
|
| 123 |
/* |
| 124 |
* allocate space to save the (remote) address in the |
| 125 |
* transport-specific data pointer for later use by netsnmp_udp_send. |
| 126 |
*/ |
| 127 |
t->data = calloc(1, sizeof(netsnmp_indexed_addr_pair)); |
| 128 |
if (NULL == t->data) { |
| 129 |
netsnmp_transport_free(t); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
t->data_length = sizeof(netsnmp_indexed_addr_pair); |
| 133 |
|
| 134 |
addr_pair = (netsnmp_indexed_addr_pair *)t->data; |
| 135 |
memcpy(&addr_pair->remote_addr, addr, sizeof(*addr)); |
| 136 |
} |
| 137 |
return t; |
| 138 |
} |
| 139 |
|
| 140 |
int |
| 141 |
netsnmp_udpipv4base_transport_socket(int flags) |
| 142 |
{ |
| 143 |
int local = flags & NETSNMP_TSPEC_LOCAL; |
| 144 |
int sock = socket(PF_INET, SOCK_DGRAM, 0); |
| 145 |
|
| 146 |
DEBUGMSGTL(("UDPBase", "opened socket %d as local=%d\n", sock, local)); |
| 147 |
if (sock < 0) |
| 148 |
return -1; |
| 149 |
|
| 150 |
_netsnmp_udp_sockopt_set(sock, local); |
| 151 |
|
| 152 |
return sock; |
| 153 |
} |
| 154 |
|
| 155 |
int |
| 156 |
netsnmp_udpipv4base_transport_bind(netsnmp_transport *t, |
| 157 |
const struct netsnmp_ep *ep, |
| 158 |
int flags) |
| 159 |
{ |
| 160 |
const struct sockaddr_in *addr = &ep->a.sin; |
| 161 |
#if defined(HAVE_IP_PKTINFO) || defined(HAVE_IP_RECVDSTADDR) |
| 162 |
int sockopt = 1; |
| 163 |
#endif |
| 164 |
int rc; |
| 165 |
|
| 166 |
if (flags & NETSNMP_TSPEC_LOCAL) { |
| 167 |
#ifdef NETSNMP_NO_LISTEN_SUPPORT |
| 168 |
return NULL; |
| 169 |
#endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
| 170 |
#ifndef WIN32 |
| 171 |
#if defined(HAVE_IP_PKTINFO) |
| 172 |
if (setsockopt(t->sock, SOL_IP, IP_PKTINFO, &sockopt, sizeof sockopt) == -1) { |
| 173 |
DEBUGMSGTL(("netsnmp_udpbase", "couldn't set IP_PKTINFO: %s\n", |
| 174 |
strerror(errno))); |
| 175 |
return 1; |
| 176 |
} |
| 177 |
DEBUGMSGTL(("netsnmp_udpbase", "set IP_PKTINFO\n")); |
| 178 |
#elif defined(HAVE_IP_RECVDSTADDR) |
| 179 |
if (setsockopt(t->sock, IPPROTO_IP, IP_RECVDSTADDR, &sockopt, sizeof sockopt) == -1) { |
| 180 |
DEBUGMSGTL(("netsnmp_udp", "couldn't set IP_RECVDSTADDR: %s\n", |
| 181 |
strerror(errno))); |
| 182 |
return 1; |
| 183 |
} |
| 184 |
DEBUGMSGTL(("netsnmp_udp", "set IP_RECVDSTADDR\n")); |
| 185 |
#endif |
| 186 |
#else /* !defined(WIN32) */ |
| 187 |
{ |
| 188 |
int sockopt = 1; |
| 189 |
if (setsockopt(t->sock, IPPROTO_IP, IP_PKTINFO, (void *)&sockopt, |
| 190 |
sizeof(sockopt)) == -1) { |
| 191 |
DEBUGMSGTL(("netsnmp_udpbase", "couldn't set IP_PKTINFO: %d\n", |
| 192 |
WSAGetLastError())); |
| 193 |
} else { |
| 194 |
DEBUGMSGTL(("netsnmp_udpbase", "set IP_PKTINFO\n")); |
| 195 |
} |
| 196 |
} |
| 197 |
#endif /* !defined(WIN32) */ |
| 198 |
} |
| 199 |
|
| 200 |
DEBUGIF("netsnmp_udpbase") { |
| 201 |
netsnmp_indexed_addr_pair addr_pair; |
| 202 |
char *str; |
| 203 |
memset(&addr_pair, 0x0, sizeof(addr_pair)); |
| 204 |
memcpy(&(addr_pair.local_addr), addr, sizeof(*addr)); |
| 205 |
str = netsnmp_udp_fmtaddr(NULL, (void *)&addr_pair, |
| 206 |
sizeof(netsnmp_indexed_addr_pair)); |
| 207 |
DEBUGMSGTL(("netsnmp_udpbase", "binding socket: %d to %s\n", |
| 208 |
t->sock, str)); |
| 209 |
free(str); |
| 210 |
} |
| 211 |
if (flags & NETSNMP_TSPEC_PREBOUND) { |
| 212 |
DEBUGMSGTL(("netsnmp_udpbase", "socket %d is prebound, nothing to do\n", |
| 213 |
t->sock)); |
| 214 |
return 0; |
| 215 |
} |
| 216 |
rc = netsnmp_bindtodevice(t->sock, ep->iface); |
| 217 |
if (rc != 0) { |
| 218 |
DEBUGMSGTL(("netsnmp_udpbase", "failed to bind to iface %s: %s\n", |
| 219 |
ep->iface, strerror(errno))); |
| 220 |
goto err; |
| 221 |
} |
| 222 |
rc = bind(t->sock, (const struct sockaddr *)addr, sizeof(*addr)); |
| 223 |
if ( rc != 0 ) { |
| 224 |
DEBUGMSGTL(("netsnmp_udpbase", |
| 225 |
"failed to bind for clientaddr: %d %s\n", |
| 226 |
errno, strerror(errno))); |
| 227 |
goto err; |
| 228 |
} |
| 229 |
|
| 230 |
return 0; |
| 231 |
|
| 232 |
err: |
| 233 |
netsnmp_socketbase_close(t); |
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
void |
| 238 |
netsnmp_udpipv4base_transport_get_bound_addr(netsnmp_transport *t) |
| 239 |
{ |
| 240 |
netsnmp_indexed_addr_pair *addr_pair; |
| 241 |
socklen_t local_addr_len = sizeof(addr_pair->local_addr); |
| 242 |
int rc; |
| 243 |
|
| 244 |
/** only for client transports: must have data and not local */ |
| 245 |
if (NULL == t || NULL != t->local || NULL == t->data || |
| 246 |
t->data_length < local_addr_len) { |
| 247 |
snmp_log(LOG_ERR, "bad parameters for get bound addr\n"); |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
addr_pair = (netsnmp_indexed_addr_pair *)t->data; |
| 252 |
|
| 253 |
/** get local socket address for client session */ |
| 254 |
rc = getsockname(t->sock, (struct sockaddr*)&addr_pair->local_addr, |
| 255 |
&local_addr_len); |
| 256 |
netsnmp_assert(rc == 0); |
| 257 |
DEBUGIF("netsnmp_udpbase") { |
| 258 |
char *str; |
| 259 |
str = netsnmp_udp_fmtaddr(NULL, (void *)addr_pair, |
| 260 |
sizeof(netsnmp_indexed_addr_pair)); |
| 261 |
DEBUGMSGTL(("netsnmp_udpbase", "socket %d bound to %s\n", |
| 262 |
t->sock, str)); |
| 263 |
free(str); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
netsnmp_transport * |
| 268 |
netsnmp_udpipv4base_transport_with_source(const struct netsnmp_ep *ep, |
| 269 |
int local, |
| 270 |
const struct netsnmp_ep *src_addr) |
| 271 |
{ |
| 272 |
netsnmp_transport *t = NULL; |
| 273 |
const struct netsnmp_ep *bind_addr; |
| 274 |
int rc, flags = 0; |
| 275 |
|
| 276 |
t = netsnmp_udpipv4base_transport_init(ep, local); |
| 277 |
if (NULL == t) |
| 278 |
return NULL; |
| 279 |
|
| 280 |
if (local) { |
| 281 |
bind_addr = ep; |
| 282 |
flags |= NETSNMP_TSPEC_LOCAL; |
| 283 |
|
| 284 |
#ifndef NETSNMP_NO_SYSTEMD |
| 285 |
/* |
| 286 |
* Maybe the socket was already provided by systemd... |
| 287 |
*/ |
| 288 |
t->sock = netsnmp_sd_find_inet_socket(PF_INET, SOCK_DGRAM, -1, |
| 289 |
ntohs(ep->a.sin.sin_port)); |
| 290 |
if (t->sock >= 0) |
| 291 |
flags |= NETSNMP_TSPEC_PREBOUND; |
| 292 |
#endif |
| 293 |
} |
| 294 |
else |
| 295 |
bind_addr = src_addr; |
| 296 |
|
| 297 |
if (-1 == t->sock) |
| 298 |
t->sock = netsnmp_udpipv4base_transport_socket(flags); |
| 299 |
if (t->sock < 0) { |
| 300 |
netsnmp_transport_free(t); |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
* If we've been given an address to bind to, then bind to it. |
| 306 |
* Otherwise the OS will use "something sensible". |
| 307 |
*/ |
| 308 |
if (NULL == bind_addr) |
| 309 |
return t; |
| 310 |
|
| 311 |
/* for Linux VRF Traps we try to bind the iface if clientaddr is not set */ |
| 312 |
if (ep && ep->iface[0]) { |
| 313 |
rc = netsnmp_bindtodevice(t->sock, ep->iface); |
| 314 |
if (rc) |
| 315 |
DEBUGMSGTL(("netsnmp_udpbase", "VRF: Could not bind socket %d to %s\n", |
| 316 |
t->sock, ep->iface)); |
| 317 |
else |
| 318 |
DEBUGMSGTL(("netsnmp_udpbase", "VRF: Bound socket %d to %s\n", |
| 319 |
t->sock, ep->iface)); |
| 320 |
} |
| 321 |
|
| 322 |
rc = netsnmp_udpipv4base_transport_bind(t, bind_addr, flags); |
| 323 |
if (rc) { |
| 324 |
netsnmp_transport_free(t); |
| 325 |
t = NULL; |
| 326 |
} |
| 327 |
else if (!local) |
| 328 |
netsnmp_udpipv4base_transport_get_bound_addr(t); |
| 329 |
|
| 330 |
return t; |
| 331 |
} |
| 332 |
|
| 333 |
netsnmp_transport * |
| 334 |
netsnmp_udpipv4base_tspec_transport(netsnmp_tdomain_spec *tspec) |
| 335 |
{ |
| 336 |
struct netsnmp_ep addr; |
| 337 |
int local; |
| 338 |
|
| 339 |
if (NULL == tspec) |
| 340 |
return NULL; |
| 341 |
|
| 342 |
local = tspec->flags & NETSNMP_TSPEC_LOCAL; |
| 343 |
|
| 344 |
/** get address from target */ |
| 345 |
if (!netsnmp_sockaddr_in3(&addr, tspec->target, tspec->default_target)) |
| 346 |
return NULL; |
| 347 |
|
| 348 |
if (NULL != tspec->source) { |
| 349 |
struct netsnmp_ep src_addr; |
| 350 |
|
| 351 |
/** get sockaddr from source */ |
| 352 |
if (!netsnmp_sockaddr_in3(&src_addr, tspec->source, ":0")) |
| 353 |
return NULL; |
| 354 |
return netsnmp_udpipv4base_transport_with_source(&addr, local, |
| 355 |
&src_addr); |
| 356 |
} |
| 357 |
|
| 358 |
/** no source and default client address ok */ |
| 359 |
return netsnmp_udpipv4base_transport(&addr, local); |
| 360 |
} |
| 361 |
|
| 362 |
netsnmp_transport * |
| 363 |
netsnmp_udpipv4base_transport(const struct netsnmp_ep *ep, int local) |
| 364 |
{ |
| 365 |
struct netsnmp_ep client_ep; |
| 366 |
const char *client_addr; |
| 367 |
int uses_port; |
| 368 |
|
| 369 |
memset(&client_ep, 0, sizeof(client_ep)); |
| 370 |
client_ep.a.sin.sin_family = AF_INET; |
| 371 |
|
| 372 |
if (local) |
| 373 |
goto out; |
| 374 |
|
| 375 |
client_addr = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
| 376 |
NETSNMP_DS_LIB_CLIENT_ADDR); |
| 377 |
if (!client_addr) |
| 378 |
goto out; |
| 379 |
|
| 380 |
if (netsnmp_sockaddr_in3(&client_ep, client_addr, ":0") < 0) { |
| 381 |
snmp_log(LOG_ERR, "Parsing clientaddr %s failed\n", client_addr); |
| 382 |
goto out; |
| 383 |
} |
| 384 |
|
| 385 |
uses_port = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
| 386 |
NETSNMP_DS_LIB_CLIENT_ADDR_USES_PORT); |
| 387 |
if (!uses_port) |
| 388 |
client_ep.a.sin.sin_port = 0; |
| 389 |
|
| 390 |
out: |
| 391 |
return netsnmp_udpipv4base_transport_with_source(ep, local, &client_ep); |
| 392 |
} |