-
Notifications
You must be signed in to change notification settings - Fork 211
/
snmpUDPBaseDomain.c
564 lines (499 loc) · 18.3 KB
/
snmpUDPBaseDomain.c
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/* UDP base transport support functions
*
* Portions of this file are subject to the following copyright(s). See
* the Net-SNMP's COPYING file for more details and other copyrights
* that may apply:
*
* Portions of this file are copyrighted by:
* Copyright (c) 2016 VMware, Inc. All rights reserved.
* Use is subject to license terms specified in the COPYING file
* distributed with the Net-SNMP package.
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/types.h>
#include <net-snmp/library/snmpUDPBaseDomain.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef WIN32
#include <mswsock.h>
#endif
#include <errno.h>
#include <net-snmp/types.h>
#include <net-snmp/library/snmpSocketBaseDomain.h>
#include <net-snmp/library/snmpUDPDomain.h>
#include <net-snmp/library/snmp_debug.h>
#include <net-snmp/library/tools.h>
#include <net-snmp/library/default_store.h>
#include <net-snmp/library/system.h>
#include <net-snmp/library/snmp_assert.h>
#ifndef MSG_DONTWAIT
#define MSG_DONTWAIT 0
#endif
void
_netsnmp_udp_sockopt_set(int fd, int local)
{
#ifdef SO_BSDCOMPAT
/*
* Patch for Linux. Without this, UDP packets that fail get an ICMP
* response. Linux turns the failed ICMP response into an error message
* and return value, unlike all other OS's.
*/
if (0 == netsnmp_os_prematch("Linux","2.4"))
{
int one = 1;
DEBUGMSGTL(("socket:option", "setting socket option SO_BSDCOMPAT\n"));
setsockopt(fd, SOL_SOCKET, SO_BSDCOMPAT, (void *) &one,
sizeof(one));
}
#endif /*SO_BSDCOMPAT */
/*
* SO_REUSEADDR will allow multiple apps to open the same port at
* the same time. Only the last one to open the socket will get
* data. Obviously, for an agent, this is a bad thing. There should
* only be one listener.
*/
#ifdef ALLOW_PORT_HIJACKING
#ifdef SO_REUSEADDR
/*
* Allow the same port to be specified multiple times without failing.
* (useful for a listener)
*/
{
int one = 1;
DEBUGMSGTL(("socket:option", "setting socket option SO_REUSEADDR\n"));
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &one,
sizeof(one));
}
#endif /*SO_REUSEADDR */
#endif
/*
* Try to set the send and receive buffers to a reasonably large value, so
* that we can send and receive big PDUs (defaults to 8192 bytes (!) on
* Solaris, for instance). Don't worry too much about errors -- just
* plough on regardless.
*/
netsnmp_sock_buffer_set(fd, SO_SNDBUF, local, 0);
netsnmp_sock_buffer_set(fd, SO_RCVBUF, local, 0);
}
#if defined(HAVE_IP_PKTINFO) || (defined(HAVE_IP_RECVDSTADDR) && defined(HAVE_IP_SENDSRCADDR))
#define netsnmp_udpbase_recvfrom_sendto_defined
enum {
#if defined(HAVE_IP_PKTINFO)
cmsg_data_size = sizeof(struct in_pktinfo)
#elif defined(HAVE_IP_RECVDSTADDR)
cmsg_data_size = sizeof(struct in_addr)
#endif
};
#ifdef WIN32
#ifndef WSAID_WSASENDMSG
#define WSAID_WSASENDMSG \
{0xa441e712,0x754f,0x43ca,{0x84,0xa7,0x0d,0xee,0x44,0xcf,0x60,0x6d}}
typedef INT (WINAPI * LPFN_WSASENDMSG)(SOCKET, LPWSAMSG, DWORD, LPDWORD,
LPWSAOVERLAPPED,
LPWSAOVERLAPPED_COMPLETION_ROUTINE);
#endif
static LPFN_WSARECVMSG pfWSARecvMsg;
static LPFN_WSASENDMSG pfWSASendMsg;
#endif
int
netsnmp_udpbase_recvfrom(int s, void *buf, int len, struct sockaddr *from,
socklen_t *fromlen, struct sockaddr *dstip,
socklen_t *dstlen, int *if_index)
{
int r;
#if !defined(WIN32)
struct iovec iov;
char cmsg[CMSG_SPACE(cmsg_data_size)];
struct cmsghdr *cm;
struct msghdr msg;
iov.iov_base = buf;
iov.iov_len = len;
memset(&msg, 0, sizeof msg);
msg.msg_name = from;
msg.msg_namelen = *fromlen;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = &cmsg;
msg.msg_controllen = sizeof(cmsg);
r = recvmsg(s, &msg, MSG_DONTWAIT);
#else /* !defined(WIN32) */
WSABUF wsabuf;
char cmsg[WSA_CMSG_SPACE(sizeof(struct in_pktinfo))];
WSACMSGHDR *cm;
WSAMSG msg;
DWORD bytes_received;
wsabuf.buf = buf;
wsabuf.len = len;
msg.name = from;
msg.namelen = *fromlen;
msg.lpBuffers = &wsabuf;
msg.dwBufferCount = 1;
msg.Control.len = sizeof(cmsg);
msg.Control.buf = cmsg;
msg.dwFlags = 0;
if (pfWSARecvMsg) {
r = pfWSARecvMsg(s, &msg, &bytes_received, NULL, NULL) == 0 ?
bytes_received : -1;
*fromlen = msg.namelen;
} else {
r = recvfrom(s, buf, len, MSG_DONTWAIT, from, fromlen);
}
#endif /* !defined(WIN32) */
if (r == -1) {
return -1;
}
DEBUGMSGTL(("udpbase:recv", "got source addr: %s\n",
inet_ntoa(((struct sockaddr_in *)from)->sin_addr)));
{
/* Get the local port number for use in diagnostic messages */
int r2 = getsockname(s, dstip, dstlen);
netsnmp_assert(r2 == 0);
}
#if !defined(WIN32)
for (cm = CMSG_FIRSTHDR(&msg); cm != NULL; cm = CMSG_NXTHDR(&msg, cm)) {
#if defined(HAVE_IP_PKTINFO)
if (cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_PKTINFO) {
struct in_pktinfo* src = (struct in_pktinfo *)CMSG_DATA(cm);
netsnmp_assert(dstip->sa_family == AF_INET);
((struct sockaddr_in*)dstip)->sin_addr = src->ipi_addr;
*if_index = src->ipi_ifindex;
DEBUGMSGTL(("udpbase:recv",
"got destination (local) addr %s, iface %d\n",
inet_ntoa(src->ipi_addr), *if_index));
}
#elif defined(HAVE_IP_RECVDSTADDR)
if (cm->cmsg_level == IPPROTO_IP && cm->cmsg_type == IP_RECVDSTADDR) {
struct in_addr* src = (struct in_addr *)CMSG_DATA(cm);
((struct sockaddr_in*)dstip)->sin_addr = *src;
DEBUGMSGTL(("netsnmp_udp", "got destination (local) addr %s\n",
inet_ntoa(*src)));
}
#endif
}
#else /* !defined(WIN32) */
for (cm = WSA_CMSG_FIRSTHDR(&msg); cm; cm = WSA_CMSG_NXTHDR(&msg, cm)) {
if (cm->cmsg_level == IPPROTO_IP && cm->cmsg_type == IP_PKTINFO) {
struct in_pktinfo* src = (struct in_pktinfo *)WSA_CMSG_DATA(cm);
netsnmp_assert(dstip->sa_family == AF_INET);
((struct sockaddr_in*)dstip)->sin_addr = src->ipi_addr;
*if_index = src->ipi_ifindex;
DEBUGMSGTL(("udpbase:recv",
"got destination (local) addr %s, iface %d\n",
inet_ntoa(src->ipi_addr), *if_index));
}
}
#endif /* !defined(WIN32) */
return r;
}
#if !defined(WIN32)
int netsnmp_udpbase_sendto_unix(int fd, const struct in_addr *srcip,
int if_index, const struct sockaddr *remote,
const void *data, int len)
{
struct iovec iov;
struct msghdr m = { NULL };
char cmsg[CMSG_SPACE(cmsg_data_size)];
int rc;
char iface[IFNAMSIZ];
socklen_t ifacelen = IFNAMSIZ;
iov.iov_base = NETSNMP_REMOVE_CONST(void *, data);
iov.iov_len = len;
m.msg_name = NETSNMP_REMOVE_CONST(void *, remote);
m.msg_namelen = sizeof(struct sockaddr_in);
m.msg_iov = &iov;
m.msg_iovlen = 1;
m.msg_flags = 0;
if (srcip && srcip->s_addr != INADDR_ANY) {
struct cmsghdr *cm;
struct in_pktinfo ipi;
int use_sendto = FALSE;
memset(cmsg, 0, sizeof(cmsg));
m.msg_control = &cmsg;
m.msg_controllen = sizeof(cmsg);
cm = CMSG_FIRSTHDR(&m);
cm->cmsg_len = CMSG_LEN(cmsg_data_size);
#if defined(HAVE_IP_PKTINFO)
cm->cmsg_level = SOL_IP;
cm->cmsg_type = IP_PKTINFO;
memset(&ipi, 0, sizeof(ipi));
#ifdef HAVE_SO_BINDTODEVICE
/*
* For asymmetric multihomed users, we only set ifindex to 0 to
* let kernel handle return if there was no iface bound to the
* socket.
*/
if (getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, iface,
&ifacelen) != 0) {
DEBUGMSGTL(("udpbase:sendto",
"getsockopt SO_BINDTODEVICE failed: %s\n",
strerror(errno)));
} else if (ifacelen == 0) {
DEBUGMSGTL(("udpbase:sendto",
"sendto: SO_BINDTODEVICE not set\n"));
} else {
DEBUGMSGTL(("udpbase:sendto",
"sendto: SO_BINDTODEVICE dev=%s using ifindex=%d\n",
iface, if_index));
use_sendto = TRUE;
}
#endif /* HAVE_SO_BINDTODEVICE */
#ifdef HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST
DEBUGMSGTL(("udpbase:sendto", "sending from %s\n",
inet_ntoa(*srcip)));
ipi.ipi_spec_dst.s_addr = srcip->s_addr;
#else
DEBUGMSGTL(("udpbase:sendto", "ignoring from address %s\n",
inet_ntoa(*srcip)));
#endif
memcpy(CMSG_DATA(cm), &ipi, sizeof(ipi));
/*
* For Linux and VRF, use sendto() instead of sendmsg(). Do not pass a
* cmsg with IP_PKTINFO set because that would override the bind to
* VRF which is set by 'vrf exec' command. That would break VRF.
*/
if (use_sendto)
rc = sendto(fd, data, len, MSG_DONTWAIT, remote,
sizeof(struct sockaddr));
else
rc = sendmsg(fd, &m, MSG_DONTWAIT);
if (rc >= 0 || (errno != EINVAL && errno != ENETUNREACH))
return rc;
/*
* The error might be caused by broadcast srcip (i.e. we're responding
* to a broadcast request) - sendmsg does not like it. Try to resend it
* using the interface on which it was received
*/
DEBUGMSGTL(("udpbase:sendto", "re-sending on iface %d\n", if_index));
{
struct in_pktinfo ipi;
memset(&ipi, 0, sizeof(ipi));
ipi.ipi_ifindex = if_index;
#ifdef HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST
ipi.ipi_spec_dst.s_addr = INADDR_ANY;
#endif
memcpy(CMSG_DATA(cm), &ipi, sizeof(ipi));
}
#elif defined(HAVE_IP_SENDSRCADDR)
cm->cmsg_level = IPPROTO_IP;
cm->cmsg_type = IP_SENDSRCADDR;
memcpy((struct in_addr *)CMSG_DATA(cm), srcip, sizeof(struct in_addr));
#endif
rc = sendmsg(fd, &m, MSG_DONTWAIT);
if (rc >= 0 || errno != EINVAL)
return rc;
DEBUGMSGTL(("udpbase:sendto", "re-sending without source address\n"));
m.msg_control = NULL;
m.msg_controllen = 0;
}
return sendmsg(fd, &m, MSG_DONTWAIT);
}
#else /* !defined(WIN32) */
int netsnmp_udpbase_sendto_win32(int fd, const struct in_addr *srcip,
int if_index, const struct sockaddr *remote,
const void *data, int len)
{
WSABUF wsabuf;
WSAMSG m;
char cmsg[WSA_CMSG_SPACE(sizeof(struct in_pktinfo))];
DWORD bytes_sent;
int rc;
wsabuf.buf = NETSNMP_REMOVE_CONST(void *, data);
wsabuf.len = len;
memset(&m, 0, sizeof(m));
m.name = NETSNMP_REMOVE_CONST(struct sockaddr *, remote);
m.namelen = sizeof(struct sockaddr_in);
m.lpBuffers = &wsabuf;
m.dwBufferCount = 1;
if (pfWSASendMsg && srcip && srcip->s_addr != INADDR_ANY) {
WSACMSGHDR *cm;
DEBUGMSGTL(("udpbase:sendto", "sending from [%d] %s\n", if_index,
inet_ntoa(*srcip)));
memset(cmsg, 0, sizeof(cmsg));
m.Control.buf = cmsg;
m.Control.len = sizeof(cmsg);
cm = WSA_CMSG_FIRSTHDR(&m);
cm->cmsg_len = WSA_CMSG_LEN(cmsg_data_size);
cm->cmsg_level = IPPROTO_IP;
cm->cmsg_type = IP_PKTINFO;
{
struct in_pktinfo ipi = { 0 };
ipi.ipi_ifindex = if_index;
ipi.ipi_addr.s_addr = srcip->s_addr;
memcpy(WSA_CMSG_DATA(cm), &ipi, sizeof(ipi));
}
rc = pfWSASendMsg(fd, &m, 0, &bytes_sent, NULL, NULL);
if (rc == 0)
return bytes_sent;
DEBUGMSGTL(("udpbase:sendto", "sending from [%d] %s failed: %d\n",
if_index, inet_ntoa(*srcip), WSAGetLastError()));
}
rc = sendto(fd, data, len, 0, remote, sizeof(struct sockaddr));
return rc;
}
#endif /* !defined(WIN32) */
int netsnmp_udpbase_sendto(int fd, const struct in_addr *srcip, int if_index,
const struct sockaddr *remote, const void *data,
int len)
{
#if !defined(WIN32)
return netsnmp_udpbase_sendto_unix(fd, srcip, if_index, remote, data, len);
#else /* !defined(WIN32) */
return netsnmp_udpbase_sendto_win32(fd, srcip, if_index, remote, data, len);
#endif /* !defined(WIN32) */
}
#endif /* HAVE_IP_PKTINFO || HAVE_IP_RECVDSTADDR */
/*
* You can write something into opaque that will subsequently get passed back
* to your send function if you like. For instance, you might want to
* remember where a PDU came from, so that you can send a reply there...
*/
int
netsnmp_udpbase_recv(netsnmp_transport *t, void *buf, int size,
void **opaque, int *olength)
{
int rc = -1;
socklen_t fromlen = sizeof(netsnmp_sockaddr_storage);
netsnmp_indexed_addr_pair *addr_pair = NULL;
struct sockaddr *from;
if (t != NULL && t->sock >= 0) {
addr_pair = SNMP_MALLOC_TYPEDEF(netsnmp_indexed_addr_pair);
if (addr_pair == NULL) {
*opaque = NULL;
*olength = 0;
return -1;
} else
from = &addr_pair->remote_addr.sa;
while (rc < 0) {
#ifdef netsnmp_udpbase_recvfrom_sendto_defined
socklen_t local_addr_len = sizeof(addr_pair->local_addr);
rc = netsnmp_udp_recvfrom(t->sock, buf, size, from, &fromlen,
&addr_pair->local_addr.sa,
&local_addr_len, &(addr_pair->if_index));
#else
rc = recvfrom(t->sock, buf, size, MSG_DONTWAIT, from, &fromlen);
#endif /* netsnmp_udpbase_recvfrom_sendto_defined */
if (rc < 0 && errno != EINTR) {
break;
}
}
if (rc >= 0) {
DEBUGIF("netsnmp_udp") {
char *str = netsnmp_udp_fmtaddr(
NULL, addr_pair, sizeof(netsnmp_indexed_addr_pair));
DEBUGMSGTL(("netsnmp_udp",
"recvfrom fd %d got %d bytes (from %s)\n",
t->sock, rc, str));
free(str);
}
} else {
DEBUGMSGTL(("netsnmp_udp", "recvfrom fd %d err %d (\"%s\")\n",
t->sock, errno, strerror(errno)));
}
*opaque = (void *)addr_pair;
*olength = sizeof(netsnmp_indexed_addr_pair);
}
return rc;
}
int
netsnmp_udpbase_send(netsnmp_transport *t, const void *buf, int size,
void **opaque, int *olength)
{
int rc = -1;
const netsnmp_indexed_addr_pair *addr_pair = NULL;
const struct sockaddr *to = NULL;
if (opaque != NULL && *opaque != NULL && NULL != olength &&
((*olength == sizeof(netsnmp_indexed_addr_pair) ||
(*olength == sizeof(struct sockaddr_in))))) {
addr_pair = (const netsnmp_indexed_addr_pair *) (*opaque);
} else if (t != NULL && t->data != NULL &&
t->data_length == sizeof(netsnmp_indexed_addr_pair)) {
addr_pair = (netsnmp_indexed_addr_pair *) (t->data);
} else {
int len = -1;
if (opaque != NULL && *opaque != NULL && NULL != olength)
len = *olength;
else if (t != NULL && t->data != NULL)
len = t->data_length;
snmp_log(LOG_ERR, "unknown addr type of size %d\n", len);
return SNMPERR_GENERR;
}
to = &addr_pair->remote_addr.sa;
if (to != NULL && t != NULL && t->sock >= 0) {
DEBUGIF("netsnmp_udp") {
char *str = netsnmp_udp_fmtaddr(NULL, addr_pair,
sizeof(netsnmp_indexed_addr_pair));
DEBUGMSGTL(("netsnmp_udp", "send %d bytes from %p to %s on fd %d\n",
size, buf, str, t->sock));
free(str);
}
while (rc < 0) {
#ifdef netsnmp_udpbase_recvfrom_sendto_defined
rc = netsnmp_udp_sendto(t->sock,
addr_pair ? &(addr_pair->local_addr.sin.sin_addr) : NULL,
addr_pair ? addr_pair->if_index : 0, to, buf, size);
#else
rc = sendto(t->sock, buf, size, 0, to, sizeof(struct sockaddr));
#endif /* netsnmp_udpbase_recvfrom_sendto_defined */
if (rc < 0 && errno != EINTR) {
DEBUGMSGTL(("netsnmp_udp", "sendto error, rc %d (errno %d)\n",
rc, errno));
break;
}
}
}
return rc;
}
void
netsnmp_udp_base_ctor(void)
{
#if defined(WIN32) && defined(HAVE_IP_PKTINFO)
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
GUID WSARecvMsgGuid = WSAID_WSARECVMSG;
GUID WSASendMsgGuid = WSAID_WSASENDMSG;
DWORD nbytes;
int result;
netsnmp_static_assert(sizeof(in_addr_t) ==
sizeof((struct sockaddr_in *)NULL)->sin_addr);
netsnmp_assert(s != SOCKET_ERROR);
/* WSARecvMsg(): Windows XP / Windows Server 2003 and later */
result = WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
&WSARecvMsgGuid, sizeof(WSARecvMsgGuid),
&pfWSARecvMsg, sizeof(pfWSARecvMsg), &nbytes, NULL, NULL);
if (result == SOCKET_ERROR)
DEBUGMSGTL(("netsnmp_udp", "WSARecvMsg() not found (errno %d)\n",
WSAGetLastError()));
/* WSASendMsg(): Windows Vista / Windows Server 2008 and later */
result = WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
&WSASendMsgGuid, sizeof(WSASendMsgGuid),
&pfWSASendMsg, sizeof(pfWSASendMsg), &nbytes, NULL, NULL);
if (result == SOCKET_ERROR)
DEBUGMSGTL(("netsnmp_udp", "WSASendMsg() not found (errno %d)\n",
WSAGetLastError()));
closesocket(s);
#endif
}