Debian Codegraph @

snmplib/transports/snmpUDPIPv6Domain.c

raw
1 /*
2 * Portions of this file are copyrighted by:
3 * Copyright (c) 2016 VMware, Inc. All rights reserved.
4 * Use is subject to license terms specified in the COPYING file
5 * distributed with the Net-SNMP package.
6 */
7 #include <net-snmp/net-snmp-config.h>
8
9 #include "snmpIPBaseDomain.h"
10 #include <net-snmp/library/snmpUDPIPv6Domain.h>
11 #include <net-snmp/library/system.h>
12
13 #include <net-snmp/types.h>
14
15 #ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
16
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <ctype.h>
20 #include <errno.h>
21
22 #ifdef HAVE_STRING_H
23 #include <string.h>
24 #else
25 #include <strings.h>
26 #endif
27 #include <stddef.h>
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
42 #endif
43 #ifdef HAVE_NETDB_H
44 #include <netdb.h>
45 #endif
46 #ifdef HAVE_NET_IF_H
47 #include <net/if.h>
48 #endif
49
50 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY
51 #define SS_FAMILY ss_family
52 #elif defined(HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY)
53 #define SS_FAMILY __ss_family
54 #endif
55
56 #include <net-snmp/types.h>
57 #include <net-snmp/output_api.h>
58 #include <net-snmp/config_api.h>
59
60 #include <net-snmp/library/snmp_transport.h>
61 #include <net-snmp/library/snmpSocketBaseDomain.h>
62 #include <net-snmp/library/tools.h>
63 #include <net-snmp/library/snmp_assert.h>
64
65 #ifndef NETSNMP_NO_SYSTEMD
66 #include <net-snmp/library/sd-daemon.h>
67 #endif
68
69 #include "inet_ntop.h"
70 #include "inet_pton.h"
71
72 oid netsnmp_UDPIPv6Domain[] = { TRANSPORT_DOMAIN_UDP_IPV6 };
73 static netsnmp_tdomain udp6Domain;
74
75 /*
76 * needs to be in sync with the definitions in snmplib/snmpTCPDomain.c
77 * and perl/agent/agent.xs
78 */
79 typedef netsnmp_indexed_addr_pair netsnmp_udp_addr_pair;
80
81 /*
82 * Return a string representing the address in data, or else the "far end"
83 * address if data is NULL.
84 */
85
86 static char *
87 netsnmp_udp6_fmtaddr(netsnmp_transport *t, const void *data, int len)
88 {
89 return netsnmp_ipv6_fmtaddr("UDP/IPv6", t, data, len);
90 }
91
92 #if defined(HAVE_IPV6_RECVPKTINFO) && !defined(WIN32)
93
94 #define netsnmp_udp6_recvfrom_sendto_defined
95
96 enum {
97 cmsg_data_size = sizeof(struct in6_pktinfo)
98 };
99
100 int
101 netsnmp_udp6_recvfrom(int s, void *buf, int len, struct sockaddr *from,
102 socklen_t *fromlen, struct sockaddr *dstip,
103 socklen_t *dstlen, int *if_index)
104 {
105 int r;
106 struct iovec iov;
107 char cmsg[CMSG_SPACE(cmsg_data_size)];
108 struct cmsghdr *cm;
109 struct msghdr msg;
110
111 iov.iov_base = buf;
112 iov.iov_len = len;
113
114 memset(&msg, 0, sizeof msg);
115 msg.msg_name = from;
116 msg.msg_namelen = *fromlen;
117 msg.msg_iov = &iov;
118 msg.msg_iovlen = 1;
119 msg.msg_control = &cmsg;
120 msg.msg_controllen = sizeof(cmsg);
121
122 r = recvmsg(s, &msg, MSG_DONTWAIT);
123
124 if (r == -1) {
125 return -1;
126 }
127
128 {
129 char buf[INET6_ADDRSTRLEN];
130 DEBUGMSGTL(("udp6:recv", "got source addr: %s\n", inet_ntop(AF_INET6,
131 &(((struct sockaddr_in6 *)from)->sin6_addr), buf,
132 sizeof(struct sockaddr_in6))));
133 }
134
135 {
136 /* Get the local port number for use in diagnostic messages */
137 int r2 = getsockname(s, dstip, dstlen);
138 netsnmp_assert(r2 == 0);
139 }
140
141 for (cm = CMSG_FIRSTHDR(&msg); cm != NULL; cm = CMSG_NXTHDR(&msg, cm)) {
142 if (cm->cmsg_level == IPPROTO_IPV6 && cm->cmsg_type == IPV6_PKTINFO) {
143 struct in6_pktinfo* src = (struct in6_pktinfo *)CMSG_DATA(cm);
144
145 netsnmp_assert(dstip->sa_family == AF_INET6);
146 ((struct sockaddr_in6*)dstip)->sin6_addr = src->ipi6_addr;
147 *if_index = src->ipi6_ifindex;
148
149 {
150 char buf[INET6_ADDRSTRLEN];
151 DEBUGMSGTL(("udp6:recv",
152 "got destination (local) addr %s, iface %d\n",
153 inet_ntop(AF_INET6,
154 &(((struct sockaddr_in6 *)dstip)->sin6_addr), buf,
155 sizeof(struct sockaddr_in6)), *if_index));
156 }
157 }
158 }
159
160 return r;
161 }
162
163 int netsnmp_udp6_sendto(int fd, const struct in6_addr *srcip, int if_index,
164 const struct sockaddr *remote, const void *data,
165 int len)
166 {
167 struct iovec iov;
168 struct msghdr m = { NULL };
169 char cmsg[CMSG_SPACE(cmsg_data_size)];
170 int rc;
171 #ifdef HAVE_SO_BINDTODEVICE
172 char iface[IFNAMSIZ];
173 socklen_t ifacelen = IFNAMSIZ;
174 #endif
175
176 iov.iov_base = NETSNMP_REMOVE_CONST(void *, data);
177 iov.iov_len = len;
178
179 m.msg_name = NETSNMP_REMOVE_CONST(void *, remote);
180 m.msg_namelen = sizeof(struct sockaddr_in6);
181 m.msg_iov = &iov;
182 m.msg_iovlen = 1;
183 m.msg_flags = 0;
184
185 if (srcip && memcmp(&srcip->s6_addr, &in6addr_any, sizeof(struct in6_addr)) != 0) {
186 struct cmsghdr *cm;
187 struct in6_pktinfo ipi;
188 int use_sendto = FALSE;
189
190 memset(cmsg, 0, sizeof(cmsg));
191
192 m.msg_control = &cmsg;
193 m.msg_controllen = sizeof(cmsg);
194
195 cm = CMSG_FIRSTHDR(&m);
196 cm->cmsg_len = CMSG_LEN(cmsg_data_size);
197
198 cm->cmsg_level = IPPROTO_IPV6;
199 cm->cmsg_type = IPV6_PKTINFO;
200
201 memset(&ipi, 0, sizeof(ipi));
202
203 #ifdef HAVE_SO_BINDTODEVICE
204 /*
205 * For asymmetric multihomed users, we only set ifindex to 0 to
206 * let kernel handle return if there was no iface bound to the
207 * socket.
208 */
209 if (getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, iface,
210 &ifacelen) != 0) {
211 DEBUGMSGTL(("udp6:sendto",
212 "getsockopt SO_BINDTODEVICE failed: %s\n",
213 strerror(errno)));
214 } else if (ifacelen == 0) {
215 DEBUGMSGTL(("udp6:sendto",
216 "sendto: SO_BINDTODEVICE not set\n"));
217 } else {
218 DEBUGMSGTL(("udp6:sendto",
219 "sendto: SO_BINDTODEVICE dev=%s using ifindex=%d\n",
220 iface, if_index));
221 use_sendto = TRUE;
222 }
223 #endif /* HAVE_SO_BINDTODEVICE */
224
225 ipi.ipi6_addr = *srcip;
226 memcpy(CMSG_DATA(cm), &ipi, sizeof(ipi));
227
228 {
229 char buf[INET6_ADDRSTRLEN];
230 DEBUGMSGTL(("udp6:sendto", "sending from %s\n",
231 inet_ntop(AF_INET6, srcip, buf, INET6_ADDRSTRLEN)));
232 }
233
234 /*
235 * For Linux and VRF, use sendto() instead of sendmsg(). Do not pass a
236 * cmsg with IP_PKTINFO set because that would override the bind to
237 * VRF which is set by 'vrf exec' command. That would break VRF.
238 */
239 if (use_sendto) {
240 rc = sendto(fd, data, len, MSG_DONTWAIT, remote,
241 sizeof(struct sockaddr));
242 } else {
243 rc = sendmsg(fd, &m, MSG_DONTWAIT);
244 }
245 if (rc >= 0 || errno != EINVAL)
246 return rc;
247
248 /*
249 * The error might be caused by broadcast srcip (i.e. we're responding
250 * to a broadcast request) - sendmsg does not like it. Try to resend it
251 * using the interface on which it was received
252 */
253
254 DEBUGMSGTL(("udp6:sendto", "re-sending on iface %d\n", if_index));
255
256 {
257 struct in6_pktinfo ipi;
258 memset(&ipi, 0, sizeof(ipi));
259 ipi.ipi6_addr = in6addr_any;
260 ipi.ipi6_ifindex = if_index;
261 memcpy(CMSG_DATA(cm), &ipi, sizeof(ipi));
262 }
263
264 rc = sendmsg(fd, &m, MSG_DONTWAIT);
265 if (rc >= 0 || errno != EINVAL)
266 return rc;
267
268 DEBUGMSGTL(("udp6:sendto", "re-sending without source address\n"));
269 m.msg_control = NULL;
270 m.msg_controllen = 0;
271 }
272
273 return sendmsg(fd, &m, MSG_DONTWAIT);
274 }
275
276 #endif /* HAVE_IPV6_RECVPKTINFO || !WIN32 */
277
278 /*
279 * You can write something into opaque that will subsequently get passed back
280 * to your send function if you like. For instance, you might want to
281 * remember where a PDU came from, so that you can send a reply there...
282 */
283
284 static int
285 netsnmp_udp6_recv(netsnmp_transport *t, void *buf, int size,
286 void **opaque, int *olength)
287 {
288 int rc = -1;
289 socklen_t fromlen = sizeof(struct sockaddr_in6);
290 netsnmp_indexed_addr_pair *addr_pair = NULL;
291 struct sockaddr *from;
292
293 if (t != NULL && t->sock >= 0) {
294 addr_pair = SNMP_MALLOC_TYPEDEF(netsnmp_indexed_addr_pair);
295 if (addr_pair == NULL) {
296 *opaque = NULL;
297 *olength = 0;
298 return -1;
299 } else {
300 from = &addr_pair->remote_addr.sa;
301 }
302
303 while (rc < 0) {
304 #ifdef netsnmp_udp6_recvfrom_sendto_defined
305 socklen_t local_addr_len = sizeof(addr_pair->local_addr);
306 rc = netsnmp_udp6_recvfrom(t->sock, buf, size, from, &fromlen,
307 &addr_pair->local_addr.sa,
308 &local_addr_len, &(addr_pair->if_index));
309 #else
310 rc = recvfrom(t->sock, buf, size, 0, from, &fromlen);
311 #endif /* netsnmp_udp6_recvfrom_sendto_defined */
312 if (rc < 0 && errno != EINTR) {
313 break;
314 }
315 }
316
317 if (rc >= 0) {
318 DEBUGIF("netsnmp_udp6") {
319 char *str = netsnmp_udp6_fmtaddr(NULL, from, fromlen);
320 DEBUGMSGTL(("netsnmp_udp6",
321 "recvfrom fd %d got %d bytes (from %s)\n", t->sock,
322 rc, str));
323 free(str);
324 }
325 } else {
326 DEBUGMSGTL(("netsnmp_udp6", "recvfrom fd %d err %d (\"%s\")\n",
327 t->sock, errno, strerror(errno)));
328 }
329 *opaque = (void *) addr_pair;
330 *olength = sizeof(netsnmp_indexed_addr_pair);
331 }
332 return rc;
333 }
334
335
336
337 static int
338 netsnmp_udp6_send(netsnmp_transport *t, const void *buf, int size,
339 void **opaque, int *olength)
340 {
341 int rc = -1;
342 const netsnmp_indexed_addr_pair *addr_pair = NULL;
343 const struct sockaddr *to = NULL;
344
345 if (opaque != NULL && *opaque != NULL && NULL != olength &&
346 (*olength == sizeof(netsnmp_indexed_addr_pair) ||
347 *olength == sizeof(struct sockaddr_in6))) {
348 addr_pair = (const netsnmp_indexed_addr_pair *) (*opaque);
349 } else if (t != NULL && t->data != NULL &&
350 t->data_length == sizeof(netsnmp_indexed_addr_pair)) {
351 addr_pair = (netsnmp_indexed_addr_pair *) (t->data);
352 } else {
353 int len = -1;
354 if (opaque != NULL && *opaque != NULL && NULL != olength)
355 len = *olength;
356 else if (t != NULL && t->data != NULL)
357 len = t->data_length;
358 snmp_log(LOG_ERR, "unknown addr type of size %d\n", len);
359 return SNMPERR_GENERR;
360 }
361
362 to = &addr_pair->remote_addr.sa;
363
364 if (to != NULL && t != NULL && t->sock >= 0) {
365 DEBUGIF("netsnmp_udp6") {
366 char *str = netsnmp_udp6_fmtaddr(NULL, addr_pair,
367 sizeof(netsnmp_indexed_addr_pair));
368 DEBUGMSGTL(("netsnmp_udp6",
369 "send %d bytes from %p to %s on fd %d\n",
370 size, buf, str, t->sock));
371 free(str);
372 }
373
374 while (rc < 0) {
375 #ifdef netsnmp_udp6_recvfrom_sendto_defined
376 rc = netsnmp_udp6_sendto(t->sock,
377 addr_pair ? &(addr_pair->local_addr.sin6.sin6_addr) : NULL,
378 addr_pair ? addr_pair->if_index : 0, to, buf, size);
379 #else
380 rc = sendto(t->sock, buf, size, 0, to, sizeof(struct sockaddr_in6));
381 #endif /* netsnmp_udp6_recvfrom_sendto_defined */
382 if (rc < 0 && errno != EINTR) {
383 DEBUGMSGTL(("netsnmp_udp6", "sendto error, rc %d (errno %d)\n",
384 rc, errno));
385 break;
386 }
387 }
388 }
389 return rc;
390 }
391
392
393 /*
394 * Initialize a UDP/IPv6-based transport for SNMP. Local is TRUE if addr is the
395 * local address to bind to (i.e. this is a server-type session); otherwise
396 * addr is the remote address to send things to.
397 */
398
399 netsnmp_transport *
400 netsnmp_udp6_transport_init(const struct netsnmp_ep *ep, int flags)
401 {
402 const struct sockaddr_in6 *addr = &ep->a.sin6;
403 netsnmp_transport *t = NULL;
404 int local = flags & NETSNMP_TSPEC_LOCAL;
405 u_char *addr_ptr;
406
407 #ifdef NETSNMP_NO_LISTEN_SUPPORT
408 if (local)
409 return NULL;
410 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
411
412 if (addr == NULL || addr->sin6_family != AF_INET6) {
413 return NULL;
414 }
415
416 t = SNMP_MALLOC_TYPEDEF(netsnmp_transport);
417 if (t == NULL) {
418 return NULL;
419 }
420
421 t->sock = -1;
422
423 addr_ptr = netsnmp_memdup(addr, sizeof(*addr));
424 if (addr_ptr == NULL) {
425 free(t);
426 return NULL;
427 }
428 if (local) {
429 /** This is a server session. */
430 t->local_length = sizeof(*addr);
431 t->local = addr_ptr;
432 } else {
433 /** This is a client session. */
434 t->remote = addr_ptr;
435 t->remote_length = sizeof(*addr);
436 }
437
438 DEBUGIF("netsnmp_udp6") {
439 char *str = netsnmp_udp6_fmtaddr(NULL, addr, sizeof(*addr));
440 DEBUGMSGTL(("netsnmp_udp6", "open %s %s\n", local ? "local" : "remote",
441 str));
442 free(str);
443 }
444
445 if (!local) {
446 netsnmp_indexed_addr_pair *addr_pair;
447
448 /*
449 * allocate space to save the (remote) address in the
450 * transport-specific data pointer for later use by netsnmp_udp_send.
451 */
452 t->data = calloc(1, sizeof(netsnmp_indexed_addr_pair));
453 if (NULL == t->data) {
454 netsnmp_transport_free(t);
455 return NULL;
456 }
457 t->data_length = sizeof(netsnmp_indexed_addr_pair);
458
459 addr_pair = (netsnmp_indexed_addr_pair *)t->data;
460 memcpy(&addr_pair->remote_addr, addr, sizeof(*addr));
461 }
462
463 /*
464 * 16-bit length field, 8 byte UDP header, 40 byte IPv6 header.
465 */
466
467 t->msgMaxSize = 0xffff - 8 - 40;
468 t->f_recv = netsnmp_udp6_recv;
469 t->f_send = netsnmp_udp6_send;
470 t->f_close = netsnmp_socketbase_close;
471 t->f_accept = NULL;
472 t->f_setup_session = netsnmp_ipbase_session_init;
473 t->f_fmtaddr = netsnmp_udp6_fmtaddr;
474 t->f_get_taddr = netsnmp_ipv6_get_taddr;
475
476 t->domain = netsnmp_UDPIPv6Domain;
477 t->domain_length =
478 sizeof(netsnmp_UDPIPv6Domain) / sizeof(netsnmp_UDPIPv6Domain[0]);
479
480 return t;
481 }
482
483 static void set_ipv6_recvpktinfo_sockopt(int sd)
484 {
485 #if defined(HAVE_IPV6_RECVPKTINFO) && !defined(WIN32)
486 int sockopt = 1;
487
488 if (setsockopt(sd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &sockopt,
489 sizeof(sockopt)) == -1) {
490 DEBUGMSGTL(("netsnmp_udp6", "couldn't set IPV6_RECVPKTINFO: %s\n",
491 strerror(errno)));
492 } else {
493 DEBUGMSGTL(("netsnmp_udp6", "set IPV6_RECVPKTINFO\n"));
494 }
495 #endif
496 }
497
498 int
499 netsnmp_udp6_transport_bind(netsnmp_transport *t,
500 const struct netsnmp_ep *ep,
501 int flags)
502 {
503 const struct sockaddr_in6 *addr = &ep->a.sin6;
504 int local = flags & NETSNMP_TSPEC_LOCAL;
505 int rc = 0;
506
507 if (local) {
508 #ifndef NETSNMP_NO_LISTEN_SUPPORT
509 /*
510 * This session is intended as a server, so we must bind on to the
511 * given IP address, which may include an interface address, or could
512 * be INADDR_ANY, but certainly includes a port number.
513 */
514
515 #ifdef IPV6_V6ONLY
516 /* Try to restrict PF_INET6 socket to IPv6 communications only. */
517 {
518 int one=1;
519 if (setsockopt(t->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&one, sizeof(one)) != 0) {
520 DEBUGMSGTL(("netsnmp_udp6", "couldn't set IPV6_V6ONLY to %d bytes: %s\n", one, strerror(errno)));
521 }
522 }
523 #endif
524 set_ipv6_recvpktinfo_sockopt(t->sock);
525 #else /* NETSNMP_NO_LISTEN_SUPPORT */
526 return -1;
527 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
528 }
529
530 DEBUGIF("netsnmp_udp6") {
531 char *str;
532 str = netsnmp_udp6_fmtaddr(NULL, addr, sizeof(*addr));
533 DEBUGMSGTL(("netsnmp_udp6", "binding socket: %d to %s\n",
534 t->sock, str));
535 free(str);
536 }
537 if (flags & NETSNMP_TSPEC_PREBOUND) {
538 DEBUGMSGTL(("netsnmp_udp6", "socket %d is prebound, nothing to do\n",
539 t->sock));
540 return 0;
541 }
542 rc = netsnmp_bindtodevice(t->sock, ep->iface);
543 if (rc != 0) {
544 DEBUGMSGTL(("netsnmp_udp6", "failed to bind to iface %s: %s\n",
545 ep->iface, strerror(errno)));
546 goto err;
547 }
548 rc = bind(t->sock, (const struct sockaddr *)addr, sizeof(*addr));
549 if (rc != 0) {
550 DEBUGMSGTL(("netsnmp_udp6", "failed to bind for clientaddr: %d %s\n",
551 errno, strerror(errno)));
552 goto err;
553 }
554
555 return 0;
556
557 err:
558 netsnmp_socketbase_close(t);
559 return -1;
560 }
561
562 int
563 netsnmp_udp6_transport_socket(int flags)
564 {
565 int local = flags & NETSNMP_TSPEC_LOCAL;
566 int sock = socket(PF_INET6, SOCK_DGRAM, 0);
567
568 DEBUGMSGTL(("UDPBase", "opened socket %d as local=%d\n", sock, local));
569 if (sock < 0)
570 return -1;
571
572 _netsnmp_udp_sockopt_set(sock, local);
573
574 return sock;
575 }
576
577 void
578 netsnmp_udp6_transport_get_bound_addr(netsnmp_transport *t)
579 {
580 netsnmp_indexed_addr_pair *addr_pair;
581 socklen_t local_addr_len = sizeof(addr_pair->local_addr);
582 int rc;
583
584 /** only for client transports: must have data and not local */
585 if (NULL == t || NULL != t->local || NULL == t->data ||
586 t->data_length < local_addr_len) {
587 snmp_log(LOG_ERR, "bad parameters for get bound addr\n");
588 return;
589 }
590
591 addr_pair = (netsnmp_indexed_addr_pair *)t->data;
592
593 /** get local socket address for client session */
594 local_addr_len = sizeof(addr_pair->local_addr);
595 rc = getsockname(t->sock, (struct sockaddr*)&addr_pair->local_addr,
596 &local_addr_len);
597 netsnmp_assert(rc == 0);
598 DEBUGIF("netsnmp_udpbase") {
599 char *str = netsnmp_udp6_fmtaddr(NULL, (void *)&addr_pair->local_addr,
600 sizeof(addr_pair->local_addr));
601 DEBUGMSGTL(("netsnmp_udpbase", "socket %d bound to %s\n",
602 t->sock, str));
603 free(str);
604 }
605 }
606
607 netsnmp_transport *
608 netsnmp_udpipv6base_tspec_transport(netsnmp_tdomain_spec *tspec)
609 {
610 struct netsnmp_ep ep;
611 int local;
612
613 if (NULL == tspec)
614 return NULL;
615
616 local = tspec->flags & NETSNMP_TSPEC_LOCAL;
617
618 /** get address from target */
619 if (!netsnmp_sockaddr_in6_3(&ep, tspec->target, tspec->default_target))
620 return NULL;
621
622 if (NULL != tspec->source) {
623 struct netsnmp_ep src_addr;
624
625 /** get sockaddr from source */
626 if (!netsnmp_sockaddr_in6_3(&src_addr, tspec->source, ":0"))
627 return NULL;
628 return netsnmp_udp6_transport_with_source(&ep, local, &src_addr);
629 }
630
631 /** no source and default client address ok */
632 return netsnmp_udp6_transport(&ep, local);
633 }
634
635 netsnmp_transport *
636 netsnmp_udp6_transport_with_source(const struct netsnmp_ep *ep,
637 int local, const struct netsnmp_ep *src_addr)
638 {
639 netsnmp_transport *t = NULL;
640 const struct netsnmp_ep *bind_addr;
641 int rc, flags = 0;
642
643 t = netsnmp_udp6_transport_init(ep, local);
644 if (NULL == t)
645 return NULL;
646
647 if (local) {
648 bind_addr = ep;
649 flags |= NETSNMP_TSPEC_LOCAL;
650
651 #ifndef NETSNMP_NO_SYSTEMD
652 /*
653 * Maybe the socket was already provided by systemd...
654 */
655 t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_DGRAM, -1,
656 ntohs(ep->a.sin6.sin6_port));
657 if (t->sock >= 0)
658 flags |= NETSNMP_TSPEC_PREBOUND;
659 #endif
660 }
661 else
662 bind_addr = src_addr;
663
664 if (-1 == t->sock)
665 t->sock = netsnmp_udp6_transport_socket(flags);
666 if (t->sock < 0) {
667 netsnmp_transport_free(t);
668 return NULL;
669 }
670
671 /*
672 * If we've been given an address to bind to, then bind to it.
673 * Otherwise the OS will use "something sensible".
674 */
675 if (NULL == bind_addr)
676 return t;
677
678 rc = netsnmp_udp6_transport_bind(t, bind_addr, flags);
679 if (rc) {
680 netsnmp_transport_free(t);
681 t = NULL;
682 }
683 else if (!local)
684 netsnmp_udp6_transport_get_bound_addr(t);
685
686 return t;
687 }
688
689 /*
690 * Open a UDP/IPv6-based transport for SNMP. Local is TRUE if addr is the
691 * local address to bind to (i.e. this is a server-type session); otherwise
692 * addr is the remote address to send things to.
693 */
694
695 netsnmp_transport *
696 netsnmp_udp6_transport(const struct netsnmp_ep *ep, int local)
697 {
698 struct netsnmp_ep client_ep;
699 const char *client_addr;
700
701 memset(&client_ep, 0, sizeof(client_ep));
702 client_ep.a.sin6.sin6_family = AF_INET6;
703
704 if (local)
705 goto out;
706
707 client_addr = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
708 NETSNMP_DS_LIB_CLIENT_ADDR);
709 if (!client_addr)
710 goto out;
711
712 if (netsnmp_sockaddr_in6_3(&client_ep, client_addr, ":0") < 0)
713 snmp_log(LOG_ERR, "Parsing clientaddr %s failed\n", client_addr);
714
715 out:
716 return netsnmp_udp6_transport_with_source(ep, local, &client_ep);
717 }
718
719
720 #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
721 /*
722 * The following functions provide the "com2sec6" configuration token
723 * functionality for compatibility.
724 */
725
726 #define EXAMPLE_NETWORK "NETWORK"
727 #define EXAMPLE_COMMUNITY "COMMUNITY"
728
729 typedef struct com2Sec6Entry_s {
730 const char *secName;
731 const char *contextName;
732 struct com2Sec6Entry_s *next;
733 struct in6_addr network;
734 struct in6_addr mask;
735 int negate;
736 const char community[1];
737 } com2Sec6Entry;
738
739 static com2Sec6Entry *com2Sec6List = NULL, *com2Sec6ListLast = NULL;
740
741
742 NETSNMP_STATIC_INLINE int
743 create_com2Sec6Entry(const struct addrinfo* const run,
744 const struct in6_addr* const mask,
745 const char* const secName,
746 const size_t secNameLen,
747 const char* const contextName,
748 const size_t contextNameLen,
749 const char* const community,
750 const size_t communityLen,
751 int negate,
752 com2Sec6Entry** const begin,
753 com2Sec6Entry** const end)
754 {
755 const struct sockaddr_in6 * const run_addr =
756 (const struct sockaddr_in6*)run->ai_addr;
757 int i;
758
759 /* Check that the network and mask are consistent. */
760 for (i = 0; i < 16; ++i) {
761 if (run_addr->sin6_addr.s6_addr[i] & ~mask->s6_addr[i]) {
762 config_perror("source/mask mismatch");
763 return 1;
764 }
765 }
766
767 {
768 char buf1[INET6_ADDRSTRLEN];
769 char buf2[INET6_ADDRSTRLEN];
770 DEBUGMSGTL(("netsnmp_udp6_parse_security",
771 "<\"%s\", %s/%s> => \"%s\"\n",
772 community,
773 inet_ntop(AF_INET6, &run_addr->sin6_addr,
774 buf1, sizeof(buf1)),
775 inet_ntop(AF_INET6, mask, buf2, sizeof(buf2)),
776 secName));
777 }
778
779 {
780 /* Allocate all the needed chunks */
781 void * const v =
782 malloc(offsetof(com2Sec6Entry, community) + communityLen +
783 secNameLen + contextNameLen);
784
785 com2Sec6Entry* const e = (com2Sec6Entry*)v;
786 char *last = ((char*)v) + offsetof(com2Sec6Entry, community);
787
788 if (v == NULL) {
789 config_perror("memory error");
790 return 1;
791 }
792
793 memcpy(last, community, communityLen);
794 last += communityLen;
795
796 memcpy(last, secName, secNameLen);
797 e->secName = last;
798 last += secNameLen;
799
800 if (contextNameLen) {
801 memcpy(last, contextName, contextNameLen);
802 e->contextName = last;
803 } else
804 e->contextName = last - 1;
805
806 memcpy(&e->network, &run_addr->sin6_addr, sizeof(struct in6_addr));
807 memcpy(&e->mask, mask, sizeof(struct in6_addr));
808
809 e->negate = negate;
810 e->next = NULL;
811 if (*end != NULL) {
812 (*end)->next = e;
813 *end = e;
814 } else {
815 *end = *begin = e;
816 }
817 }
818 return 0;
819 }
820
821 void
822 netsnmp_udp6_parse_security(const char *token, char *param)
823 {
824 /** copy_nword does null term, so we need vars of max size + 2. */
825 /** (one for null, one to detect param too long */
826 char secName[VACMSTRINGLEN]; /* == VACM_MAX_STRING + 2 */
827 size_t secNameLen;
828 char contextName[VACMSTRINGLEN];
829 size_t contextNameLen;
830 char community[COMMUNITY_MAX_LEN + 2];/* overflow + null char */
831 size_t communityLen;
832 char source[301]; /* !(1)+dns-name(253)+/(1)+mask(45)+\0(1) */
833 char *sourcep;
834 struct in6_addr mask;
835 int negate;
836
837 /*
838 * Get security, source address/netmask and community strings.
839 */
840
841 param = copy_nword( param, secName, sizeof(secName));
842 if (strcmp(secName, "-Cn") == 0) {
843 if (!param) {
844 config_perror("missing CONTEXT_NAME parameter");
845 return;
846 }
847 param = copy_nword( param, contextName, sizeof(contextName));
848 contextNameLen = strlen(contextName);
849 if (contextNameLen > VACM_MAX_STRING) {
850 config_perror("context name too long");
851 return;
852 }
853 if (!param) {
854 config_perror("missing NAME parameter");
855 return;
856 }
857 ++contextNameLen; /* null termination */
858 param = copy_nword( param, secName, sizeof(secName));
859 } else {
860 contextNameLen = 0;
861 }
862
863 secNameLen = strlen(secName);
864 if (secNameLen == 0) {
865 config_perror("empty NAME parameter");
866 return;
867 } else if (secNameLen > VACM_MAX_STRING) {
868 config_perror("security name too long");
869 return;
870 }
871 ++secNameLen; /* null termination */
872
873 if (!param) {
874 config_perror("missing SOURCE parameter");
875 return;
876 }
877 param = copy_nword( param, source, sizeof(source));
878 if (source[0] == '\0') {
879 config_perror("empty SOURCE parameter");
880 return;
881 }
882 if (strncmp(source, EXAMPLE_NETWORK, strlen(EXAMPLE_NETWORK)) == 0) {
883 config_perror("example config NETWORK not properly configured");
884 return;
885 }
886
887 if (!param) {
888 config_perror("missing COMMUNITY parameter");
889 return;
890 }
891 param = copy_nword( param, community, sizeof(community));
892 if (community[0] == '\0') {
893 config_perror("empty COMMUNITY parameter");
894 return;
895 }
896 communityLen = strlen(community);
897 if (communityLen > COMMUNITY_MAX_LEN) {
898 config_perror("community name too long");
899 return;
900 }
901 ++communityLen; /* null termination */
902 if (communityLen == sizeof(EXAMPLE_COMMUNITY) &&
903 memcmp(community, EXAMPLE_COMMUNITY, sizeof(EXAMPLE_COMMUNITY)) == 0) {
904 config_perror("example config COMMUNITY not properly configured");
905 return;
906 }
907
908 /* Possible mask cases
909 * "default" <=> 0::0/0
910 * <hostname>[/] <=> <hostname>/128
911 * <hostname>/number <=> <hostname>/number
912 * <hostname>/<mask> <=> <hostname>/<mask>
913 */
914 {
915 /* Deal with the "default" case first. */
916 const int isdefault = strcmp(source, "default") == 0;
917
918 if (isdefault) {
919 memset(mask.s6_addr, '\0', sizeof(mask.s6_addr));
920 negate = 0;
921 sourcep = NULL; /* gcc gets confused about sourcep being used */
922 } else {
923 char *strmask;
924 if (*source == '!') {
925 negate = 1;
926 sourcep = source + 1;
927 } else {
928 negate = 0;
929 sourcep = source;
930 }
931
932 /* Split the source/netmask parts */
933 strmask = strchr(sourcep, '/');
934 if (strmask != NULL)
935 /* Mask given. */
936 *strmask++ = '\0';
937
938 /* Try to interpret the mask */
939 if (strmask == NULL || *strmask == '\0') {
940 /* No mask was given. Assume /128 */
941 memset(mask.s6_addr, 0xff, sizeof(mask.s6_addr));
942 } else {
943 /* Try to interpret mask as a "number of 1 bits". */
944 char* cp;
945 long masklength = strtol(strmask, &cp, 10);
946 if (*cp == '\0') {
947 if (0 <= masklength && masklength <= 128) {
948 const int j = masklength / 8;
949 const int jj = masklength % 8;
950
951 memset(mask.s6_addr, 0xff, j);
952 if (j < 16) {
953 mask.s6_addr[j] = (0xffu << (8 - jj));
954 memset(mask.s6_addr + j + 1, '\0', 15 - j);
955 }
956 } else {
957 config_perror("bad mask length");
958 return;
959 }
960 }
961 /* Try to interpret mask numerically. */
962 else if (inet_pton(AF_INET6, strmask, &mask) != 1) {
963 config_perror("bad mask");
964 return;
965 }
966 }
967 }
968
969 {
970 struct sockaddr_in6 pton_addr;
971 struct addrinfo hints, *res = NULL;
972 memset(&hints, '\0', sizeof(hints));
973
974 /* First check if default, otherwise try to parse as a numeric
975 * address, if that also fails try to lookup the address */
976 if (isdefault) {
977 memset(&pton_addr.sin6_addr.s6_addr, '\0',
978 sizeof(struct in6_addr));
979 } else if (inet_pton(AF_INET6, sourcep, &pton_addr.sin6_addr) != 1) {
980 /* Nope, wasn't a numeric IPv6 address. Must be IPv4 or a hostname. */
981
982 /* Try interpreting as dotted quad - IPv4 */
983 struct in_addr network;
984 if (inet_pton(AF_INET, sourcep, &network) > 0){
985 /* Yes, it's IPv4 - so it's already parsed and we can return. */
986 DEBUGMSGTL(("com2sec6", "IPv4 detected for IPv6 parser. Skipping.\n"));
987 return;
988 }
989 #ifdef HAVE_GETADDRINFO
990 {
991 int gai_error;
992
993 hints.ai_family = AF_INET6;
994 hints.ai_socktype = SOCK_DGRAM;
995 gai_error = netsnmp_getaddrinfo(sourcep, NULL, &hints,
996 &res);
997 if (gai_error != 0) {
998 config_perror(gai_strerror(gai_error));
999 return;
1000 }
1001 }
1002 #else
1003 config_perror("getaddrinfo() not available");
1004 return;
1005 #endif
1006 }
1007 if (res == NULL) {
1008 hints.ai_addrlen = sizeof(pton_addr);
1009 hints.ai_addr = (struct sockaddr*)&pton_addr;
1010 hints.ai_next = NULL;
1011 res = &hints;
1012 }
1013
1014 {
1015 struct addrinfo *run;
1016 int failed = 0;
1017 com2Sec6Entry *begin = NULL, *end = NULL;
1018
1019 for (run = res; run && !failed; run = run->ai_next)
1020 failed =
1021 create_com2Sec6Entry(run, &mask,
1022 secName, secNameLen,
1023 contextName, contextNameLen,
1024 community, communityLen, negate,
1025 &begin, &end);
1026
1027 if (failed) {
1028 /* Free eventually allocated chunks */
1029 while (begin) {
1030 end = begin;
1031 begin = begin->next;
1032 free(end);
1033 }
1034 } else if (com2Sec6ListLast != NULL) {
1035 com2Sec6ListLast->next = begin;
1036 com2Sec6ListLast = end;
1037 } else {
1038 com2Sec6List = begin;
1039 com2Sec6ListLast = end;
1040 }
1041 }
1042 #ifdef HAVE_GETADDRINFO
1043 if (res != &hints)
1044 freeaddrinfo(res);
1045 #endif
1046 }
1047 }
1048 }
1049
1050 void
1051 netsnmp_udp6_com2Sec6List_free(void)
1052 {
1053 com2Sec6Entry *e = com2Sec6List;
1054 while (e != NULL) {
1055 com2Sec6Entry *tmp = e;
1056 e = e->next;
1057 free(tmp);
1058 }
1059 com2Sec6List = com2Sec6ListLast = NULL;
1060 }
1061
1062 #endif /* support for community based SNMP */
1063
1064 void
1065 netsnmp_udp6_agent_config_tokens_register(void)
1066 {
1067 #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
1068 register_app_config_handler("com2sec6", netsnmp_udp6_parse_security,
1069 netsnmp_udp6_com2Sec6List_free,
1070 "[-Cn CONTEXT] secName IPv6-network-address[/netmask] community");
1071 #endif /* support for community based SNMP */
1072 }
1073
1074
1075
1076 #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
1077
1078 /*
1079 * Return 0 if there are no com2sec entries, or return 1 if there ARE com2sec
1080 * entries. On return, if a com2sec entry matched the passed parameters,
1081 * then *secName points at the appropriate security name, or is NULL if the
1082 * parameters did not match any com2sec entry.
1083 */
1084
1085 int
1086 netsnmp_udp6_getSecName(void *opaque, int olength,
1087 const char *community,
1088 int community_len,
1089 const char **secName, const char **contextName)
1090 {
1091 const com2Sec6Entry *c;
1092 netsnmp_udp_addr_pair *addr_pair = (netsnmp_udp_addr_pair *) opaque;
1093 struct sockaddr_in6 *from = (struct sockaddr_in6 *) &(addr_pair->remote_addr);
1094 char *ztcommunity = NULL;
1095 char str6[INET6_ADDRSTRLEN];
1096
1097 if (secName != NULL) {
1098 *secName = NULL; /* Haven't found anything yet */
1099 }
1100
1101 /*
1102 * Special case if there are NO entries (as opposed to no MATCHING
1103 * entries).
1104 */
1105
1106 if (com2Sec6List == NULL) {
1107 DEBUGMSGTL(("netsnmp_udp6_getSecName", "no com2sec entries\n"));
1108 return 0;
1109 }
1110
1111 /*
1112 * If there is no IPv6 source address, then there can be no valid security
1113 * name.
1114 */
1115
1116 DEBUGMSGTL(("netsnmp_udp_getSecName", "opaque = %p (len = %d), sizeof = %d, family = %d (%d)\n",
1117 opaque, olength, (int)sizeof(netsnmp_udp_addr_pair),
1118 from->sin6_family, AF_INET6));
1119 if (opaque == NULL || olength != sizeof(netsnmp_udp_addr_pair) ||
1120 from->sin6_family != PF_INET6) {
1121 DEBUGMSGTL(("netsnmp_udp6_getSecName",
1122 "no IPv6 source address in PDU?\n"));
1123 return 1;
1124 }
1125
1126 ztcommunity = (char *) malloc(community_len + 1);
1127 if (ztcommunity != NULL) {
1128 memcpy(ztcommunity, community, community_len);
1129 ztcommunity[community_len] = '\0';
1130 }
1131
1132 inet_ntop(AF_INET6, &from->sin6_addr, str6, sizeof(str6));
1133 DEBUGMSGTL(("netsnmp_udp6_getSecName", "resolve <\"%s\", %s>\n",
1134 ztcommunity ? ztcommunity : "<malloc error>", str6));
1135
1136 for (c = com2Sec6List; c != NULL; c = c->next) {
1137 {
1138 char buf1[INET6_ADDRSTRLEN];
1139 char buf2[INET6_ADDRSTRLEN];
1140 DEBUGMSGTL(("netsnmp_udp6_getSecName",
1141 "compare <\"%s\", %s/%s>", c->community,
1142 inet_ntop(AF_INET6, &c->network, buf1, sizeof(buf1)),
1143 inet_ntop(AF_INET6, &c->mask, buf2, sizeof(buf2))));
1144 }
1145 if ((community_len == (int)strlen(c->community)) &&
1146 (memcmp(community, c->community, community_len) == 0)) {
1147 int i, ok = 1;
1148 for (i = 0; ok && i < 16; ++i)
1149 if ((from->sin6_addr.s6_addr[i] & c->mask.s6_addr[i]) !=
1150 c->network.s6_addr[i])
1151 ok = 0;
1152 if (ok) {
1153 DEBUGMSG(("netsnmp_udp6_getSecName", "... SUCCESS\n"));
1154 if (c->negate) {
1155 /*
1156 * If we matched a negative entry, then we are done - claim that we
1157 * matched nothing.
1158 */
1159 DEBUGMSG(("netsnmp_udp6_getSecName", "... <negative entry>\n"));
1160 break;
1161 }
1162 if (secName != NULL) {
1163 *secName = c->secName;
1164 *contextName = c->contextName;
1165 }
1166 break;
1167 }
1168 }
1169 else {
1170 DEBUGMSG(("netsnmp_udp6_getSecName", "... nope\n"));
1171 }
1172 }
1173
1174 if (ztcommunity != NULL) {
1175 free(ztcommunity);
1176 }
1177 return 1;
1178 }
1179 #endif /* support for community based SNMP */
1180
1181 netsnmp_transport *
1182 netsnmp_udp6_create_tstring(const char *str, int local,
1183 const char *default_target)
1184 {
1185 struct netsnmp_ep ep;
1186
1187 if (netsnmp_sockaddr_in6_3(&ep, str, default_target)) {
1188 return netsnmp_udp6_transport(&ep, local);
1189 } else {
1190 return NULL;
1191 }
1192 }
1193
1194 netsnmp_transport *
1195 netsnmp_udp6_create_tspec(netsnmp_tdomain_spec *tspec)
1196 {
1197 netsnmp_transport *t = netsnmp_udpipv6base_tspec_transport(tspec);
1198 return t;
1199
1200 }
1201
1202
1203 /*
1204 * See:
1205 *
1206 * http://www.ietf.org/internet-drafts/draft-ietf-ops-taddress-mib-01.txt
1207 *
1208 * (or newer equivalent) for details of the TC which we are using for
1209 * the mapping here.
1210 */
1211
1212 netsnmp_transport *
1213 netsnmp_udp6_create_ostring(const void *o, size_t o_len, int local)
1214 {
1215 struct netsnmp_ep ep;
1216
1217 memset(&ep, 0, sizeof(ep));
1218 if (netsnmp_ipv6_ostring_to_sockaddr(&ep.a.sin6, o, o_len))
1219 return netsnmp_udp6_transport(&ep, local);
1220 return NULL;
1221 }
1222
1223
1224 void
1225 netsnmp_udpipv6_ctor(void)
1226 {
1227 udp6Domain.name = netsnmp_UDPIPv6Domain;
1228 udp6Domain.name_length = sizeof(netsnmp_UDPIPv6Domain) / sizeof(oid);
1229 udp6Domain.f_create_from_tstring_new = netsnmp_udp6_create_tstring;
1230 udp6Domain.f_create_from_tspec = netsnmp_udp6_create_tspec;
1231 udp6Domain.f_create_from_ostring = netsnmp_udp6_create_ostring;
1232 udp6Domain.prefix = (const char**)calloc(5, sizeof(char *));
1233 udp6Domain.prefix[0] = "udp6";
1234 udp6Domain.prefix[1] = "ipv6";
1235 udp6Domain.prefix[2] = "udpv6";
1236 udp6Domain.prefix[3] = "udpipv6";
1237
1238 netsnmp_tdomain_register(&udp6Domain);
1239 }
1240
1241 #else
1242
1243 #ifdef NETSNMP_DLL
1244 /* need this hook for win32 MSVC++ DLL build */
1245 void
1246 netsnmp_udp6_agent_config_tokens_register(void)
1247 { }
1248 #endif
1249
1250 #endif /* NETSNMP_TRANSPORT_UDPIPV6_DOMAIN */
1251