/src/net-snmp/snmplib/transports/snmpTCPIPv6Domain.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include <net-snmp/net-snmp-config.h> |
2 | | |
3 | | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
4 | | |
5 | | #include <net-snmp/types.h> |
6 | | #include "snmpIPBaseDomain.h" |
7 | | #include <net-snmp/library/snmpTCPIPv6Domain.h> |
8 | | |
9 | | #include <stdio.h> |
10 | | #include <sys/types.h> |
11 | | #include <errno.h> |
12 | | |
13 | | #ifdef HAVE_STRING_H |
14 | | #include <string.h> |
15 | | #else |
16 | | #include <strings.h> |
17 | | #endif |
18 | | #ifdef HAVE_STDLIB_H |
19 | | #include <stdlib.h> |
20 | | #endif |
21 | | #ifdef HAVE_UNISTD_H |
22 | | #include <unistd.h> |
23 | | #endif |
24 | | #ifdef HAVE_SYS_SOCKET_H |
25 | | #include <sys/socket.h> |
26 | | #endif |
27 | | #ifdef HAVE_NETINET_IN_H |
28 | | #include <netinet/in.h> |
29 | | #endif |
30 | | #ifdef HAVE_ARPA_INET_H |
31 | | #include <arpa/inet.h> |
32 | | #endif |
33 | | #ifdef HAVE_NETDB_H |
34 | | #include <netdb.h> |
35 | | #endif |
36 | | #ifdef HAVE_FCNTL_H |
37 | | #include <fcntl.h> |
38 | | #endif |
39 | | |
40 | | #include <net-snmp/types.h> |
41 | | #include <net-snmp/output_api.h> |
42 | | #include <net-snmp/config_api.h> |
43 | | |
44 | | #include <net-snmp/library/snmp.h> |
45 | | #include <net-snmp/library/snmp_transport.h> |
46 | | #include <net-snmp/library/snmpSocketBaseDomain.h> |
47 | | #include <net-snmp/library/snmpTCPBaseDomain.h> |
48 | | #include <net-snmp/library/tools.h> |
49 | | |
50 | | #ifndef NETSNMP_NO_SYSTEMD |
51 | | #include <net-snmp/library/sd-daemon.h> |
52 | | #endif |
53 | | |
54 | | #include "inet_ntop.h" |
55 | | |
56 | | const oid netsnmp_TCPIPv6Domain[] = { TRANSPORT_DOMAIN_TCP_IPV6 }; |
57 | | static netsnmp_tdomain tcp6Domain; |
58 | | |
59 | | /* |
60 | | * Return a string representing the address in data, or else the "far end" |
61 | | * address if data is NULL. |
62 | | */ |
63 | | |
64 | | static char * |
65 | | netsnmp_tcp6_fmtaddr(netsnmp_transport *t, const void *data, int len) |
66 | 0 | { |
67 | 0 | return netsnmp_ipv6_fmtaddr("TCP/IPv6", t, data, len); |
68 | 0 | } |
69 | | |
70 | | static int |
71 | | netsnmp_tcp6_accept(netsnmp_transport *t) |
72 | 0 | { |
73 | 0 | struct sockaddr_in6 *farend = NULL; |
74 | 0 | int newsock = -1; |
75 | 0 | socklen_t farendlen = sizeof(struct sockaddr_in6); |
76 | |
|
77 | 0 | farend = (struct sockaddr_in6 *) malloc(sizeof(struct sockaddr_in6)); |
78 | |
|
79 | 0 | if (farend == NULL) { |
80 | | /* |
81 | | * Indicate that the acceptance of this socket failed. |
82 | | */ |
83 | 0 | DEBUGMSGTL(("netsnmp_tcp6", "accept: malloc failed\n")); |
84 | 0 | return -1; |
85 | 0 | } |
86 | | |
87 | 0 | if (t != NULL && t->sock >= 0) { |
88 | 0 | newsock = (int) accept(t->sock, (struct sockaddr *) farend, &farendlen); |
89 | |
|
90 | 0 | if (newsock < 0) { |
91 | 0 | DEBUGMSGTL(("netsnmp_tcp6","accept failed rc %d errno %d \"%s\"\n", |
92 | 0 | newsock, errno, strerror(errno))); |
93 | 0 | free(farend); |
94 | 0 | return newsock; |
95 | 0 | } |
96 | | |
97 | 0 | if (t->data != NULL) { |
98 | 0 | free(t->data); |
99 | 0 | } |
100 | |
|
101 | 0 | t->data = farend; |
102 | 0 | t->data_length = farendlen; |
103 | 0 | DEBUGIF("netsnmp_tcp6") { |
104 | 0 | char *str = netsnmp_tcp6_fmtaddr(NULL, farend, farendlen); |
105 | 0 | DEBUGMSGTL(("netsnmp_tcp6", "accept succeeded (from %s)\n", str)); |
106 | 0 | free(str); |
107 | 0 | } |
108 | | |
109 | | /* |
110 | | * Try to make the new socket blocking. |
111 | | */ |
112 | |
|
113 | 0 | if (netsnmp_set_non_blocking_mode(newsock, FALSE) < 0) |
114 | 0 | DEBUGMSGTL(("netsnmp_tcp6", |
115 | 0 | "accept: couldn't f_getfl of fd %d\n", newsock)); |
116 | | |
117 | | /* |
118 | | * Allow user to override the send and receive buffers. Default is |
119 | | * to use os default. Don't worry too much about errors -- |
120 | | * just plough on regardless. |
121 | | */ |
122 | 0 | netsnmp_sock_buffer_set(newsock, SO_SNDBUF, 1, 0); |
123 | 0 | netsnmp_sock_buffer_set(newsock, SO_RCVBUF, 1, 0); |
124 | |
|
125 | 0 | return newsock; |
126 | 0 | } else { |
127 | 0 | free(farend); |
128 | 0 | return -1; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | |
133 | | |
134 | | /* |
135 | | * Open a TCP/IPv6-based transport for SNMP. Local is TRUE if addr is the |
136 | | * local address to bind to (i.e. this is a server-type session); otherwise |
137 | | * addr is the remote address to send things to. |
138 | | */ |
139 | | |
140 | | netsnmp_transport * |
141 | | netsnmp_tcp6_transport(const struct netsnmp_ep *ep, int local) |
142 | 0 | { |
143 | 0 | const struct sockaddr_in6 *addr = &ep->a.sin6; |
144 | 0 | netsnmp_transport *t = NULL; |
145 | 0 | int rc = 0; |
146 | 0 | int socket_initialized = 0; |
147 | |
|
148 | | #ifdef NETSNMP_NO_LISTEN_SUPPORT |
149 | | if (local) |
150 | | return NULL; |
151 | | #endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
152 | |
|
153 | 0 | if (addr == NULL || addr->sin6_family != AF_INET6) { |
154 | 0 | return NULL; |
155 | 0 | } |
156 | | |
157 | 0 | t = SNMP_MALLOC_TYPEDEF(netsnmp_transport); |
158 | 0 | if (t == NULL) { |
159 | 0 | return NULL; |
160 | 0 | } |
161 | | |
162 | 0 | DEBUGIF("netsnmp_tcp6") { |
163 | 0 | char *str = netsnmp_tcp6_fmtaddr(NULL, addr, |
164 | 0 | sizeof(struct sockaddr_in6)); |
165 | 0 | DEBUGMSGTL(("netsnmp_tcp6", "open %s %s\n", local ? "local" : "remote", |
166 | 0 | str)); |
167 | 0 | free(str); |
168 | 0 | } |
169 | |
|
170 | 0 | t->sock = -1; |
171 | 0 | t->data = malloc(sizeof(netsnmp_indexed_addr_pair)); |
172 | 0 | if (t->data == NULL) |
173 | 0 | goto err; |
174 | 0 | t->data_length = sizeof(netsnmp_indexed_addr_pair); |
175 | 0 | memcpy(t->data, addr, sizeof(struct sockaddr_in6)); |
176 | |
|
177 | 0 | t->domain = netsnmp_TCPIPv6Domain; |
178 | 0 | t->domain_length = OID_LENGTH(netsnmp_TCPIPv6Domain); |
179 | |
|
180 | 0 | #ifndef NETSNMP_NO_SYSTEMD |
181 | | /* |
182 | | * Maybe the socket was already provided by systemd... |
183 | | */ |
184 | 0 | if (local) { |
185 | 0 | t->sock = netsnmp_sd_find_inet_socket(PF_INET6, SOCK_STREAM, 1, |
186 | 0 | ntohs(addr->sin6_port)); |
187 | 0 | if (t->sock >= 0) |
188 | 0 | socket_initialized = 1; |
189 | 0 | } |
190 | 0 | #endif |
191 | 0 | if (!socket_initialized) |
192 | 0 | t->sock = (int) socket(PF_INET6, SOCK_STREAM, 0); |
193 | 0 | if (t->sock < 0) |
194 | 0 | goto err; |
195 | | |
196 | 0 | t->flags = NETSNMP_TRANSPORT_FLAG_STREAM; |
197 | | |
198 | | /* for Linux VRF Traps we try to bind the iface if clientaddr is not set */ |
199 | 0 | if (local == 0 && ep) { |
200 | 0 | rc = netsnmp_bindtodevice(t->sock, ep->iface); |
201 | 0 | if (rc) |
202 | 0 | DEBUGMSGTL(("netsnmp_tcp", "VRF: Could not bind socket %d to %s\n", |
203 | 0 | t->sock, ep->iface)); |
204 | 0 | else |
205 | 0 | DEBUGMSGTL(("netsnmp_tcp", "VRF: Bound socket %d to %s\n", |
206 | 0 | t->sock, ep->iface)); |
207 | 0 | } |
208 | |
|
209 | 0 | if (local) { |
210 | 0 | #ifndef NETSNMP_NO_LISTEN_SUPPORT |
211 | 0 | int opt = 1; |
212 | | |
213 | | /* |
214 | | * This session is intended as a server, so we must bind on to the |
215 | | * given IP address, which may include an interface address, or could |
216 | | * be INADDR_ANY, but certainly includes a port number. |
217 | | */ |
218 | |
|
219 | 0 | #ifdef IPV6_V6ONLY |
220 | | /* Try to restrict PF_INET6 socket to IPv6 communications only. */ |
221 | 0 | { |
222 | 0 | int one=1; |
223 | 0 | if (setsockopt(t->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&one, sizeof(one)) != 0) { |
224 | 0 | DEBUGMSGTL(("netsnmp_tcp6", "couldn't set IPV6_V6ONLY to %d bytes: %s\n", one, strerror(errno))); |
225 | 0 | } |
226 | 0 | } |
227 | 0 | #endif |
228 | |
|
229 | 0 | t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN; |
230 | 0 | t->local_length = sizeof(*addr); |
231 | 0 | t->local = netsnmp_memdup(addr, sizeof(*addr)); |
232 | 0 | if (!t->local) |
233 | 0 | goto err; |
234 | | |
235 | | /* |
236 | | * We should set SO_REUSEADDR too. |
237 | | */ |
238 | | |
239 | 0 | setsockopt(t->sock, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)); |
240 | |
|
241 | 0 | if (!socket_initialized) { |
242 | 0 | rc = netsnmp_bindtodevice(t->sock, ep->iface); |
243 | 0 | if (rc != 0) { |
244 | 0 | DEBUGMSGTL(("netsnmp_tcp6", "failed to bind to iface %s: %s\n", |
245 | 0 | ep->iface, strerror(errno))); |
246 | 0 | goto err; |
247 | 0 | } |
248 | 0 | rc = bind(t->sock, (const struct sockaddr *)addr, sizeof(*addr)); |
249 | 0 | if (rc != 0) |
250 | 0 | goto err; |
251 | 0 | } |
252 | | |
253 | | /* |
254 | | * Since we are going to be letting select() tell us when connections |
255 | | * are ready to be accept()ed, we need to make the socket n0n-blocking |
256 | | * to avoid the race condition described in W. R. Stevens, ``Unix |
257 | | * Network Programming Volume I Second Edition'', pp. 422--4, which |
258 | | * could otherwise wedge the agent. |
259 | | */ |
260 | | |
261 | 0 | netsnmp_set_non_blocking_mode(t->sock, TRUE); |
262 | | |
263 | | /* |
264 | | * Now sit here and wait for connections to arrive. |
265 | | */ |
266 | |
|
267 | 0 | if (!socket_initialized) { |
268 | 0 | rc = listen(t->sock, NETSNMP_STREAM_QUEUE_LEN); |
269 | 0 | if (rc != 0) |
270 | 0 | goto err; |
271 | 0 | } |
272 | | |
273 | | /* |
274 | | * no buffer size on listen socket - doesn't make sense |
275 | | */ |
276 | | #else /* NETSNMP_NO_LISTEN_SUPPORT */ |
277 | | return NULL; |
278 | | #endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
279 | 0 | } else { |
280 | 0 | t->remote_length = sizeof(*addr); |
281 | 0 | t->remote = netsnmp_memdup(addr, sizeof(*addr)); |
282 | 0 | if (!t->remote) |
283 | 0 | goto err; |
284 | | |
285 | | /* |
286 | | * This is a client-type session, so attempt to connect to the far |
287 | | * end. We don't go non-blocking here because it's not obvious what |
288 | | * you'd then do if you tried to do snmp_sends before the connection |
289 | | * had completed. So this can block. |
290 | | */ |
291 | | |
292 | 0 | rc = connect(t->sock, (const struct sockaddr *)addr, sizeof(*addr)); |
293 | 0 | DEBUGMSGTL(("netsnmp_tcp6", "connect returns %d\n", rc)); |
294 | 0 | if (rc < 0) |
295 | 0 | goto err; |
296 | | |
297 | | /* |
298 | | * Allow user to override the send and receive buffers. Default is |
299 | | * to use os default. Don't worry too much about errors -- |
300 | | * just plough on regardless. |
301 | | */ |
302 | 0 | netsnmp_sock_buffer_set(t->sock, SO_SNDBUF, local, 0); |
303 | 0 | netsnmp_sock_buffer_set(t->sock, SO_RCVBUF, local, 0); |
304 | 0 | } |
305 | | |
306 | | /* |
307 | | * Message size is not limited by this transport (hence msgMaxSize |
308 | | * is equal to the maximum legal size of an SNMP message). |
309 | | */ |
310 | | |
311 | 0 | t->msgMaxSize = SNMP_MAX_PACKET_LEN; |
312 | 0 | t->f_recv = netsnmp_tcpbase_recv; |
313 | 0 | t->f_send = netsnmp_tcpbase_send; |
314 | 0 | t->f_close = netsnmp_socketbase_close; |
315 | 0 | t->f_accept = netsnmp_tcp6_accept; |
316 | 0 | t->f_fmtaddr = netsnmp_tcp6_fmtaddr; |
317 | 0 | t->f_get_taddr = netsnmp_ipv6_get_taddr; |
318 | |
|
319 | 0 | return t; |
320 | | |
321 | 0 | err: |
322 | 0 | netsnmp_socketbase_close(t); |
323 | 0 | netsnmp_transport_free(t); |
324 | 0 | return NULL; |
325 | 0 | } |
326 | | |
327 | | netsnmp_transport * |
328 | | netsnmp_tcp6_create_tstring(const char *str, int local, |
329 | | const char *default_target) |
330 | 0 | { |
331 | 0 | struct netsnmp_ep ep; |
332 | |
|
333 | 0 | if (netsnmp_sockaddr_in6_3(&ep, str, default_target)) { |
334 | 0 | return netsnmp_tcp6_transport(&ep, local); |
335 | 0 | } else { |
336 | 0 | return NULL; |
337 | 0 | } |
338 | 0 | } |
339 | | |
340 | | |
341 | | /* |
342 | | * See: |
343 | | * |
344 | | * http://www.ietf.org/internet-drafts/draft-ietf-ops-taddress-mib-01.txt |
345 | | * |
346 | | * (or newer equivalent) for details of the TC which we are using for |
347 | | * the mapping here. |
348 | | */ |
349 | | |
350 | | netsnmp_transport * |
351 | | netsnmp_tcp6_create_ostring(const void *o, size_t o_len, int local) |
352 | 0 | { |
353 | 0 | struct netsnmp_ep ep; |
354 | |
|
355 | 0 | memset(&ep, 0, sizeof(ep)); |
356 | 0 | if (netsnmp_ipv6_ostring_to_sockaddr(&ep.a.sin6, o, o_len)) |
357 | 0 | return netsnmp_tcp6_transport(&ep, local); |
358 | 0 | return NULL; |
359 | 0 | } |
360 | | |
361 | | |
362 | | void |
363 | | netsnmp_tcpipv6_ctor(void) |
364 | 3.82k | { |
365 | 3.82k | tcp6Domain.name = netsnmp_TCPIPv6Domain; |
366 | 3.82k | tcp6Domain.name_length = OID_LENGTH(netsnmp_TCPIPv6Domain); |
367 | 3.82k | tcp6Domain.f_create_from_tstring_new = netsnmp_tcp6_create_tstring; |
368 | 3.82k | tcp6Domain.f_create_from_ostring = netsnmp_tcp6_create_ostring; |
369 | 3.82k | tcp6Domain.prefix = calloc(4, sizeof(char *)); |
370 | 3.82k | if (!tcp6Domain.prefix) { |
371 | 0 | snmp_log(LOG_ERR, "calloc() failed - out of memory\n"); |
372 | 0 | return; |
373 | 0 | } |
374 | 3.82k | tcp6Domain.prefix[0] = "tcp6"; |
375 | 3.82k | tcp6Domain.prefix[1] = "tcpv6"; |
376 | 3.82k | tcp6Domain.prefix[2] = "tcpipv6"; |
377 | | |
378 | 3.82k | netsnmp_tdomain_register(&tcp6Domain); |
379 | 3.82k | } |
380 | | |
381 | | #endif /* NETSNMP_TRANSPORT_TCPIPV6_DOMAIN */ |
382 | | |