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