]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / lib / sockunion.c
1 /* Socket union related function.
2 * Copyright (c) 1997, 98 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "vty.h"
25 #include "sockunion.h"
26 #include "memory.h"
27 #include "log.h"
28 #include "jhash.h"
29 #include "lib_errors.h"
30
31 DEFINE_MTYPE_STATIC(LIB, SOCKUNION, "Socket union")
32
33 const char *inet_sutop(const union sockunion *su, char *str)
34 {
35 switch (su->sa.sa_family) {
36 case AF_INET:
37 inet_ntop(AF_INET, &su->sin.sin_addr, str, INET_ADDRSTRLEN);
38 break;
39 case AF_INET6:
40 inet_ntop(AF_INET6, &su->sin6.sin6_addr, str, INET6_ADDRSTRLEN);
41 break;
42 }
43 return str;
44 }
45
46 int str2sockunion(const char *str, union sockunion *su)
47 {
48 int ret;
49
50 if (str == NULL)
51 return -1;
52
53 memset(su, 0, sizeof(union sockunion));
54
55 ret = inet_pton(AF_INET, str, &su->sin.sin_addr);
56 if (ret > 0) /* Valid IPv4 address format. */
57 {
58 su->sin.sin_family = AF_INET;
59 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
60 su->sin.sin_len = sizeof(struct sockaddr_in);
61 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
62 return 0;
63 }
64 ret = inet_pton(AF_INET6, str, &su->sin6.sin6_addr);
65 if (ret > 0) /* Valid IPv6 address format. */
66 {
67 su->sin6.sin6_family = AF_INET6;
68 #ifdef SIN6_LEN
69 su->sin6.sin6_len = sizeof(struct sockaddr_in6);
70 #endif /* SIN6_LEN */
71 return 0;
72 }
73 return -1;
74 }
75
76 const char *sockunion2str(const union sockunion *su, char *buf, size_t len)
77 {
78 switch (sockunion_family(su)) {
79 case AF_UNSPEC:
80 snprintf(buf, len, "(unspec)");
81 return buf;
82 case AF_INET:
83 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
84 case AF_INET6:
85 return inet_ntop(AF_INET6, &su->sin6.sin6_addr, buf, len);
86 }
87 snprintf(buf, len, "(af %d)", sockunion_family(su));
88 return buf;
89 }
90
91 union sockunion *sockunion_str2su(const char *str)
92 {
93 union sockunion *su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
94
95 if (!str2sockunion(str, su))
96 return su;
97
98 XFREE(MTYPE_SOCKUNION, su);
99 return NULL;
100 }
101
102 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
103 static void sockunion_normalise_mapped(union sockunion *su)
104 {
105 struct sockaddr_in sin;
106
107 if (su->sa.sa_family == AF_INET6
108 && IN6_IS_ADDR_V4MAPPED(&su->sin6.sin6_addr)) {
109 memset(&sin, 0, sizeof(struct sockaddr_in));
110 sin.sin_family = AF_INET;
111 sin.sin_port = su->sin6.sin6_port;
112 memcpy(&sin.sin_addr, ((char *)&su->sin6.sin6_addr) + 12, 4);
113 memcpy(su, &sin, sizeof(struct sockaddr_in));
114 }
115 }
116
117 /* return sockunion structure : this function should be revised. */
118 static const char *sockunion_log(const union sockunion *su, char *buf,
119 size_t len)
120 {
121 switch (su->sa.sa_family) {
122 case AF_INET:
123 return inet_ntop(AF_INET, &su->sin.sin_addr, buf, len);
124
125 case AF_INET6:
126 return inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf, len);
127 break;
128
129 default:
130 snprintf(buf, len, "af_unknown %d ", su->sa.sa_family);
131 return buf;
132 }
133 }
134
135 /* Return socket of sockunion. */
136 int sockunion_socket(const union sockunion *su)
137 {
138 int sock;
139
140 sock = socket(su->sa.sa_family, SOCK_STREAM, 0);
141 if (sock < 0) {
142 char buf[SU_ADDRSTRLEN];
143 flog_err(EC_LIB_SOCKET, "Can't make socket for %s : %s",
144 sockunion_log(su, buf, SU_ADDRSTRLEN),
145 safe_strerror(errno));
146 return -1;
147 }
148
149 return sock;
150 }
151
152 /* Return accepted new socket file descriptor. */
153 int sockunion_accept(int sock, union sockunion *su)
154 {
155 socklen_t len;
156 int client_sock;
157
158 len = sizeof(union sockunion);
159 client_sock = accept(sock, (struct sockaddr *)su, &len);
160
161 sockunion_normalise_mapped(su);
162 return client_sock;
163 }
164
165 /* Return sizeof union sockunion. */
166 int sockunion_sizeof(const union sockunion *su)
167 {
168 int ret;
169
170 ret = 0;
171 switch (su->sa.sa_family) {
172 case AF_INET:
173 ret = sizeof(struct sockaddr_in);
174 break;
175 case AF_INET6:
176 ret = sizeof(struct sockaddr_in6);
177 break;
178 }
179 return ret;
180 }
181
182 /* Performs a non-blocking connect(). */
183 enum connect_result sockunion_connect(int fd, const union sockunion *peersu,
184 unsigned short port, ifindex_t ifindex)
185 {
186 int ret;
187 union sockunion su;
188
189 memcpy(&su, peersu, sizeof(union sockunion));
190
191 switch (su.sa.sa_family) {
192 case AF_INET:
193 su.sin.sin_port = port;
194 break;
195 case AF_INET6:
196 su.sin6.sin6_port = port;
197 #ifdef KAME
198 if (IN6_IS_ADDR_LINKLOCAL(&su.sin6.sin6_addr) && ifindex) {
199 su.sin6.sin6_scope_id = ifindex;
200 SET_IN6_LINKLOCAL_IFINDEX(su.sin6.sin6_addr, ifindex);
201 }
202 #endif /* KAME */
203 break;
204 }
205
206 /* Call connect function. */
207 ret = connect(fd, (struct sockaddr *)&su, sockunion_sizeof(&su));
208
209 /* Immediate success */
210 if (ret == 0)
211 return connect_success;
212
213 /* If connect is in progress then return 1 else it's real error. */
214 if (ret < 0) {
215 if (errno != EINPROGRESS) {
216 char str[SU_ADDRSTRLEN];
217 zlog_info("can't connect to %s fd %d : %s",
218 sockunion_log(&su, str, sizeof str), fd,
219 safe_strerror(errno));
220 return connect_error;
221 }
222 }
223
224 return connect_in_progress;
225 }
226
227 /* Make socket from sockunion union. */
228 int sockunion_stream_socket(union sockunion *su)
229 {
230 int sock;
231
232 if (su->sa.sa_family == 0)
233 su->sa.sa_family = AF_INET_UNION;
234
235 sock = socket(su->sa.sa_family, SOCK_STREAM, 0);
236
237 if (sock < 0)
238 flog_err(EC_LIB_SOCKET,
239 "can't make socket sockunion_stream_socket");
240
241 return sock;
242 }
243
244 /* Bind socket to specified address. */
245 int sockunion_bind(int sock, union sockunion *su, unsigned short port,
246 union sockunion *su_addr)
247 {
248 int size = 0;
249 int ret;
250
251 if (su->sa.sa_family == AF_INET) {
252 size = sizeof(struct sockaddr_in);
253 su->sin.sin_port = htons(port);
254 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
255 su->sin.sin_len = size;
256 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
257 if (su_addr == NULL)
258 sockunion2ip(su) = htonl(INADDR_ANY);
259 } else if (su->sa.sa_family == AF_INET6) {
260 size = sizeof(struct sockaddr_in6);
261 su->sin6.sin6_port = htons(port);
262 #ifdef SIN6_LEN
263 su->sin6.sin6_len = size;
264 #endif /* SIN6_LEN */
265 if (su_addr == NULL) {
266 #ifdef LINUX_IPV6
267 memset(&su->sin6.sin6_addr, 0, sizeof(struct in6_addr));
268 #else
269 su->sin6.sin6_addr = in6addr_any;
270 #endif /* LINUX_IPV6 */
271 }
272 }
273
274 ret = bind(sock, (struct sockaddr *)su, size);
275 if (ret < 0) {
276 char buf[SU_ADDRSTRLEN];
277 flog_err(EC_LIB_SOCKET, "can't bind socket for %s : %s",
278 sockunion_log(su, buf, SU_ADDRSTRLEN),
279 safe_strerror(errno));
280 }
281
282 return ret;
283 }
284
285 int sockopt_reuseaddr(int sock)
286 {
287 int ret;
288 int on = 1;
289
290 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
291 sizeof(on));
292 if (ret < 0) {
293 flog_err(EC_LIB_SOCKET,
294 "can't set sockopt SO_REUSEADDR to socket %d", sock);
295 return -1;
296 }
297 return 0;
298 }
299
300 #ifdef SO_REUSEPORT
301 int sockopt_reuseport(int sock)
302 {
303 int ret;
304 int on = 1;
305
306 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *)&on,
307 sizeof(on));
308 if (ret < 0) {
309 flog_err(EC_LIB_SOCKET,
310 "can't set sockopt SO_REUSEPORT to socket %d", sock);
311 return -1;
312 }
313 return 0;
314 }
315 #else
316 int sockopt_reuseport(int sock)
317 {
318 return 0;
319 }
320 #endif /* 0 */
321
322 int sockopt_ttl(int family, int sock, int ttl)
323 {
324 int ret;
325
326 #ifdef IP_TTL
327 if (family == AF_INET) {
328 ret = setsockopt(sock, IPPROTO_IP, IP_TTL, (void *)&ttl,
329 sizeof(int));
330 if (ret < 0) {
331 flog_err(EC_LIB_SOCKET,
332 "can't set sockopt IP_TTL %d to socket %d",
333 ttl, sock);
334 return -1;
335 }
336 return 0;
337 }
338 #endif /* IP_TTL */
339 if (family == AF_INET6) {
340 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
341 (void *)&ttl, sizeof(int));
342 if (ret < 0) {
343 flog_err(
344 EC_LIB_SOCKET,
345 "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
346 ttl, sock);
347 return -1;
348 }
349 return 0;
350 }
351 return 0;
352 }
353
354 /*
355 * This function called setsockopt(.., TCP_CORK,...)
356 * Which on linux is a no-op since it is enabled by
357 * default and on BSD it uses TCP_NOPUSH to do
358 * the same thing( which it was not configured to
359 * use). This cleanup of the api occurred on 8/1/17
360 * I imagine if after more than 1 year of no-one
361 * complaining, and a major upgrade release we
362 * can deprecate and remove this function call
363 */
364 int sockopt_cork(int sock, int onoff)
365 {
366 return 0;
367 }
368
369 int sockopt_minttl(int family, int sock, int minttl)
370 {
371 #ifdef IP_MINTTL
372 if (family == AF_INET) {
373 int ret = setsockopt(sock, IPPROTO_IP, IP_MINTTL, &minttl,
374 sizeof(minttl));
375 if (ret < 0)
376 flog_err(
377 EC_LIB_SOCKET,
378 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
379 minttl, sock, safe_strerror(errno));
380 return ret;
381 }
382 #endif /* IP_MINTTL */
383 #ifdef IPV6_MINHOPCOUNT
384 if (family == AF_INET6) {
385 int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT,
386 &minttl, sizeof(minttl));
387 if (ret < 0)
388 flog_err(
389 EC_LIB_SOCKET,
390 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
391 minttl, sock, safe_strerror(errno));
392 return ret;
393 }
394 #endif
395
396 errno = EOPNOTSUPP;
397 return -1;
398 }
399
400 int sockopt_v6only(int family, int sock)
401 {
402 int ret, on = 1;
403
404 #ifdef IPV6_V6ONLY
405 if (family == AF_INET6) {
406 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on,
407 sizeof(int));
408 if (ret < 0) {
409 flog_err(EC_LIB_SOCKET,
410 "can't set sockopt IPV6_V6ONLY "
411 "to socket %d",
412 sock);
413 return -1;
414 }
415 return 0;
416 }
417 #endif /* IPV6_V6ONLY */
418 return 0;
419 }
420
421 /* If same family and same prefix return 1. */
422 int sockunion_same(const union sockunion *su1, const union sockunion *su2)
423 {
424 int ret = 0;
425
426 if (su1->sa.sa_family != su2->sa.sa_family)
427 return 0;
428
429 switch (su1->sa.sa_family) {
430 case AF_INET:
431 ret = memcmp(&su1->sin.sin_addr, &su2->sin.sin_addr,
432 sizeof(struct in_addr));
433 break;
434 case AF_INET6:
435 ret = memcmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
436 sizeof(struct in6_addr));
437 if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) {
438 /* compare interface indices */
439 if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id)
440 ret = (su1->sin6.sin6_scope_id
441 == su2->sin6.sin6_scope_id)
442 ? 0
443 : 1;
444 }
445 break;
446 }
447 if (ret == 0)
448 return 1;
449 else
450 return 0;
451 }
452
453 unsigned int sockunion_hash(const union sockunion *su)
454 {
455 switch (sockunion_family(su)) {
456 case AF_INET:
457 return jhash_1word(su->sin.sin_addr.s_addr, 0);
458 case AF_INET6:
459 return jhash2(su->sin6.sin6_addr.s6_addr32,
460 array_size(su->sin6.sin6_addr.s6_addr32), 0);
461 }
462 return 0;
463 }
464
465 size_t family2addrsize(int family)
466 {
467 switch (family) {
468 case AF_INET:
469 return sizeof(struct in_addr);
470 case AF_INET6:
471 return sizeof(struct in6_addr);
472 }
473 return 0;
474 }
475
476 size_t sockunion_get_addrlen(const union sockunion *su)
477 {
478 return family2addrsize(sockunion_family(su));
479 }
480
481 const uint8_t *sockunion_get_addr(const union sockunion *su)
482 {
483 switch (sockunion_family(su)) {
484 case AF_INET:
485 return (const uint8_t *)&su->sin.sin_addr.s_addr;
486 case AF_INET6:
487 return (const uint8_t *)&su->sin6.sin6_addr;
488 }
489 return NULL;
490 }
491
492 void sockunion_set(union sockunion *su, int family, const uint8_t *addr,
493 size_t bytes)
494 {
495 if (family2addrsize(family) != bytes)
496 return;
497
498 sockunion_family(su) = family;
499 switch (family) {
500 case AF_INET:
501 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
502 break;
503 case AF_INET6:
504 memcpy(&su->sin6.sin6_addr, addr, bytes);
505 break;
506 }
507 }
508
509 /* After TCP connection is established. Get local address and port. */
510 union sockunion *sockunion_getsockname(int fd)
511 {
512 int ret;
513 socklen_t len;
514 union {
515 struct sockaddr sa;
516 struct sockaddr_in sin;
517 struct sockaddr_in6 sin6;
518 char tmp_buffer[128];
519 } name;
520 union sockunion *su;
521
522 memset(&name, 0, sizeof name);
523 len = sizeof name;
524
525 ret = getsockname(fd, (struct sockaddr *)&name, &len);
526 if (ret < 0) {
527 flog_err(EC_LIB_SOCKET,
528 "Can't get local address and port by getsockname: %s",
529 safe_strerror(errno));
530 return NULL;
531 }
532
533 if (name.sa.sa_family == AF_INET) {
534 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
535 memcpy(su, &name, sizeof(struct sockaddr_in));
536 return su;
537 }
538 if (name.sa.sa_family == AF_INET6) {
539 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
540 memcpy(su, &name, sizeof(struct sockaddr_in6));
541 sockunion_normalise_mapped(su);
542 return su;
543 }
544 return NULL;
545 }
546
547 /* After TCP connection is established. Get remote address and port. */
548 union sockunion *sockunion_getpeername(int fd)
549 {
550 int ret;
551 socklen_t len;
552 union {
553 struct sockaddr sa;
554 struct sockaddr_in sin;
555 struct sockaddr_in6 sin6;
556 char tmp_buffer[128];
557 } name;
558 union sockunion *su;
559
560 memset(&name, 0, sizeof name);
561 len = sizeof name;
562 ret = getpeername(fd, (struct sockaddr *)&name, &len);
563 if (ret < 0) {
564 flog_err(EC_LIB_SOCKET, "Can't get remote address and port: %s",
565 safe_strerror(errno));
566 return NULL;
567 }
568
569 if (name.sa.sa_family == AF_INET) {
570 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
571 memcpy(su, &name, sizeof(struct sockaddr_in));
572 return su;
573 }
574 if (name.sa.sa_family == AF_INET6) {
575 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
576 memcpy(su, &name, sizeof(struct sockaddr_in6));
577 sockunion_normalise_mapped(su);
578 return su;
579 }
580 return NULL;
581 }
582
583 /* Print sockunion structure */
584 static void __attribute__((unused)) sockunion_print(const union sockunion *su)
585 {
586 if (su == NULL)
587 return;
588
589 switch (su->sa.sa_family) {
590 case AF_INET:
591 printf("%s\n", inet_ntoa(su->sin.sin_addr));
592 break;
593 case AF_INET6: {
594 char buf[SU_ADDRSTRLEN];
595
596 printf("%s\n", inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf,
597 sizeof(buf)));
598 } break;
599
600 #ifdef AF_LINK
601 case AF_LINK: {
602 struct sockaddr_dl *sdl;
603
604 sdl = (struct sockaddr_dl *)&(su->sa);
605 printf("link#%d\n", sdl->sdl_index);
606 } break;
607 #endif /* AF_LINK */
608 default:
609 printf("af_unknown %d\n", su->sa.sa_family);
610 break;
611 }
612 }
613
614 static int in6addr_cmp(const struct in6_addr *addr1,
615 const struct in6_addr *addr2)
616 {
617 unsigned int i;
618 const uint8_t *p1, *p2;
619
620 p1 = (const uint8_t *)addr1;
621 p2 = (const uint8_t *)addr2;
622
623 for (i = 0; i < sizeof(struct in6_addr); i++) {
624 if (p1[i] > p2[i])
625 return 1;
626 else if (p1[i] < p2[i])
627 return -1;
628 }
629 return 0;
630 }
631
632 int sockunion_cmp(const union sockunion *su1, const union sockunion *su2)
633 {
634 if (su1->sa.sa_family > su2->sa.sa_family)
635 return 1;
636 if (su1->sa.sa_family < su2->sa.sa_family)
637 return -1;
638
639 if (su1->sa.sa_family == AF_INET) {
640 if (ntohl(sockunion2ip(su1)) == ntohl(sockunion2ip(su2)))
641 return 0;
642 if (ntohl(sockunion2ip(su1)) > ntohl(sockunion2ip(su2)))
643 return 1;
644 else
645 return -1;
646 }
647 if (su1->sa.sa_family == AF_INET6)
648 return in6addr_cmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
649 return 0;
650 }
651
652 /* Duplicate sockunion. */
653 union sockunion *sockunion_dup(const union sockunion *su)
654 {
655 union sockunion *dup =
656 XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
657 memcpy(dup, su, sizeof(union sockunion));
658 return dup;
659 }
660
661 void sockunion_free(union sockunion *su)
662 {
663 XFREE(MTYPE_SOCKUNION, su);
664 }
665
666 void sockunion_init(union sockunion *su)
667 {
668 memset(su, 0, sizeof(union sockunion));
669 }