Coverage Report

Created: 2026-07-16 06:28

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