]> git.proxmox.com Git - mirror_frr.git/blob - lib/sockunion.c
*: rename zlog_fer -> flog_err
[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 zlog_warn("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 static 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 zlog_warn("can't make socket sockunion_stream_socket");
239
240 return sock;
241 }
242
243 /* Bind socket to specified address. */
244 int sockunion_bind(int sock, union sockunion *su, unsigned short port,
245 union sockunion *su_addr)
246 {
247 int size = 0;
248 int ret;
249
250 if (su->sa.sa_family == AF_INET) {
251 size = sizeof(struct sockaddr_in);
252 su->sin.sin_port = htons(port);
253 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
254 su->sin.sin_len = size;
255 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
256 if (su_addr == NULL)
257 sockunion2ip(su) = htonl(INADDR_ANY);
258 } else if (su->sa.sa_family == AF_INET6) {
259 size = sizeof(struct sockaddr_in6);
260 su->sin6.sin6_port = htons(port);
261 #ifdef SIN6_LEN
262 su->sin6.sin6_len = size;
263 #endif /* SIN6_LEN */
264 if (su_addr == NULL) {
265 #ifdef LINUX_IPV6
266 memset(&su->sin6.sin6_addr, 0, sizeof(struct in6_addr));
267 #else
268 su->sin6.sin6_addr = in6addr_any;
269 #endif /* LINUX_IPV6 */
270 }
271 }
272
273 ret = bind(sock, (struct sockaddr *)su, size);
274 if (ret < 0) {
275 char buf[SU_ADDRSTRLEN];
276 zlog_warn("can't bind socket for %s : %s",
277 sockunion_log(su, buf, SU_ADDRSTRLEN),
278 safe_strerror(errno));
279 }
280
281 return ret;
282 }
283
284 int sockopt_reuseaddr(int sock)
285 {
286 int ret;
287 int on = 1;
288
289 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
290 sizeof(on));
291 if (ret < 0) {
292 zlog_warn("can't set sockopt SO_REUSEADDR to socket %d", sock);
293 return -1;
294 }
295 return 0;
296 }
297
298 #ifdef SO_REUSEPORT
299 int sockopt_reuseport(int sock)
300 {
301 int ret;
302 int on = 1;
303
304 ret = setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void *)&on,
305 sizeof(on));
306 if (ret < 0) {
307 zlog_warn("can't set sockopt SO_REUSEPORT to socket %d", sock);
308 return -1;
309 }
310 return 0;
311 }
312 #else
313 int sockopt_reuseport(int sock)
314 {
315 return 0;
316 }
317 #endif /* 0 */
318
319 int sockopt_ttl(int family, int sock, int ttl)
320 {
321 int ret;
322
323 #ifdef IP_TTL
324 if (family == AF_INET) {
325 ret = setsockopt(sock, IPPROTO_IP, IP_TTL, (void *)&ttl,
326 sizeof(int));
327 if (ret < 0) {
328 zlog_warn("can't set sockopt IP_TTL %d to socket %d",
329 ttl, sock);
330 return -1;
331 }
332 return 0;
333 }
334 #endif /* IP_TTL */
335 if (family == AF_INET6) {
336 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
337 (void *)&ttl, sizeof(int));
338 if (ret < 0) {
339 zlog_warn(
340 "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d",
341 ttl, sock);
342 return -1;
343 }
344 return 0;
345 }
346 return 0;
347 }
348
349 /*
350 * This function called setsockopt(.., TCP_CORK,...)
351 * Which on linux is a no-op since it is enabled by
352 * default and on BSD it uses TCP_NOPUSH to do
353 * the same thing( which it was not configured to
354 * use). This cleanup of the api occured on 8/1/17
355 * I imagine if after more than 1 year of no-one
356 * complaining, and a major upgrade release we
357 * can deprecate and remove this function call
358 */
359 int sockopt_cork(int sock, int onoff)
360 {
361 return 0;
362 }
363
364 int sockopt_mark_default(int sock, int mark, struct zebra_privs_t *cap)
365 {
366 #ifdef SO_MARK
367 int ret;
368
369 if (cap->change(ZPRIVS_RAISE))
370 flog_err(LIB_ERR_PRIVILEGES,
371 "routing_socket: Can't raise privileges");
372
373 ret = setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
374
375 if (cap->change(ZPRIVS_LOWER))
376 flog_err(LIB_ERR_PRIVILEGES,
377 "routing_socket: Can't lower privileges");
378
379 return ret;
380 #else
381 return 0;
382 #endif
383 }
384
385 int sockopt_minttl(int family, int sock, int minttl)
386 {
387 #ifdef IP_MINTTL
388 if (family == AF_INET) {
389 int ret = setsockopt(sock, IPPROTO_IP, IP_MINTTL, &minttl,
390 sizeof(minttl));
391 if (ret < 0)
392 zlog_warn(
393 "can't set sockopt IP_MINTTL to %d on socket %d: %s",
394 minttl, sock, safe_strerror(errno));
395 return ret;
396 }
397 #endif /* IP_MINTTL */
398 #ifdef IPV6_MINHOPCOUNT
399 if (family == AF_INET6) {
400 int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT,
401 &minttl, sizeof(minttl));
402 if (ret < 0)
403 zlog_warn(
404 "can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s",
405 minttl, sock, safe_strerror(errno));
406 return ret;
407 }
408 #endif
409
410 errno = EOPNOTSUPP;
411 return -1;
412 }
413
414 int sockopt_v6only(int family, int sock)
415 {
416 int ret, on = 1;
417
418 #ifdef IPV6_V6ONLY
419 if (family == AF_INET6) {
420 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on,
421 sizeof(int));
422 if (ret < 0) {
423 zlog_warn(
424 "can't set sockopt IPV6_V6ONLY "
425 "to socket %d",
426 sock);
427 return -1;
428 }
429 return 0;
430 }
431 #endif /* IPV6_V6ONLY */
432 return 0;
433 }
434
435 /* If same family and same prefix return 1. */
436 int sockunion_same(const union sockunion *su1, const union sockunion *su2)
437 {
438 int ret = 0;
439
440 if (su1->sa.sa_family != su2->sa.sa_family)
441 return 0;
442
443 switch (su1->sa.sa_family) {
444 case AF_INET:
445 ret = memcmp(&su1->sin.sin_addr, &su2->sin.sin_addr,
446 sizeof(struct in_addr));
447 break;
448 case AF_INET6:
449 ret = memcmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr,
450 sizeof(struct in6_addr));
451 if ((ret == 0) && IN6_IS_ADDR_LINKLOCAL(&su1->sin6.sin6_addr)) {
452 /* compare interface indices */
453 if (su1->sin6.sin6_scope_id && su2->sin6.sin6_scope_id)
454 ret = (su1->sin6.sin6_scope_id
455 == su2->sin6.sin6_scope_id)
456 ? 0
457 : 1;
458 }
459 break;
460 }
461 if (ret == 0)
462 return 1;
463 else
464 return 0;
465 }
466
467 unsigned int sockunion_hash(const union sockunion *su)
468 {
469 switch (sockunion_family(su)) {
470 case AF_INET:
471 return jhash_1word(su->sin.sin_addr.s_addr, 0);
472 case AF_INET6:
473 return jhash2(su->sin6.sin6_addr.s6_addr32,
474 ZEBRA_NUM_OF(su->sin6.sin6_addr.s6_addr32), 0);
475 }
476 return 0;
477 }
478
479 size_t family2addrsize(int family)
480 {
481 switch (family) {
482 case AF_INET:
483 return sizeof(struct in_addr);
484 case AF_INET6:
485 return sizeof(struct in6_addr);
486 }
487 return 0;
488 }
489
490 size_t sockunion_get_addrlen(const union sockunion *su)
491 {
492 return family2addrsize(sockunion_family(su));
493 }
494
495 const uint8_t *sockunion_get_addr(const union sockunion *su)
496 {
497 switch (sockunion_family(su)) {
498 case AF_INET:
499 return (const uint8_t *)&su->sin.sin_addr.s_addr;
500 case AF_INET6:
501 return (const uint8_t *)&su->sin6.sin6_addr;
502 }
503 return NULL;
504 }
505
506 void sockunion_set(union sockunion *su, int family, const uint8_t *addr,
507 size_t bytes)
508 {
509 if (family2addrsize(family) != bytes)
510 return;
511
512 sockunion_family(su) = family;
513 switch (family) {
514 case AF_INET:
515 memcpy(&su->sin.sin_addr.s_addr, addr, bytes);
516 break;
517 case AF_INET6:
518 memcpy(&su->sin6.sin6_addr, addr, bytes);
519 break;
520 }
521 }
522
523 /* After TCP connection is established. Get local address and port. */
524 union sockunion *sockunion_getsockname(int fd)
525 {
526 int ret;
527 socklen_t len;
528 union {
529 struct sockaddr sa;
530 struct sockaddr_in sin;
531 struct sockaddr_in6 sin6;
532 char tmp_buffer[128];
533 } name;
534 union sockunion *su;
535
536 memset(&name, 0, sizeof name);
537 len = sizeof name;
538
539 ret = getsockname(fd, (struct sockaddr *)&name, &len);
540 if (ret < 0) {
541 zlog_warn("Can't get local address and port by getsockname: %s",
542 safe_strerror(errno));
543 return NULL;
544 }
545
546 if (name.sa.sa_family == AF_INET) {
547 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
548 memcpy(su, &name, sizeof(struct sockaddr_in));
549 return su;
550 }
551 if (name.sa.sa_family == AF_INET6) {
552 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
553 memcpy(su, &name, sizeof(struct sockaddr_in6));
554 sockunion_normalise_mapped(su);
555 return su;
556 }
557 return NULL;
558 }
559
560 /* After TCP connection is established. Get remote address and port. */
561 union sockunion *sockunion_getpeername(int fd)
562 {
563 int ret;
564 socklen_t len;
565 union {
566 struct sockaddr sa;
567 struct sockaddr_in sin;
568 struct sockaddr_in6 sin6;
569 char tmp_buffer[128];
570 } name;
571 union sockunion *su;
572
573 memset(&name, 0, sizeof name);
574 len = sizeof name;
575 ret = getpeername(fd, (struct sockaddr *)&name, &len);
576 if (ret < 0) {
577 zlog_warn("Can't get remote address and port: %s",
578 safe_strerror(errno));
579 return NULL;
580 }
581
582 if (name.sa.sa_family == AF_INET) {
583 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
584 memcpy(su, &name, sizeof(struct sockaddr_in));
585 return su;
586 }
587 if (name.sa.sa_family == AF_INET6) {
588 su = XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
589 memcpy(su, &name, sizeof(struct sockaddr_in6));
590 sockunion_normalise_mapped(su);
591 return su;
592 }
593 return NULL;
594 }
595
596 /* Print sockunion structure */
597 static void __attribute__((unused)) sockunion_print(const union sockunion *su)
598 {
599 if (su == NULL)
600 return;
601
602 switch (su->sa.sa_family) {
603 case AF_INET:
604 printf("%s\n", inet_ntoa(su->sin.sin_addr));
605 break;
606 case AF_INET6: {
607 char buf[SU_ADDRSTRLEN];
608
609 printf("%s\n", inet_ntop(AF_INET6, &(su->sin6.sin6_addr), buf,
610 sizeof(buf)));
611 } break;
612
613 #ifdef AF_LINK
614 case AF_LINK: {
615 struct sockaddr_dl *sdl;
616
617 sdl = (struct sockaddr_dl *)&(su->sa);
618 printf("link#%d\n", sdl->sdl_index);
619 } break;
620 #endif /* AF_LINK */
621 default:
622 printf("af_unknown %d\n", su->sa.sa_family);
623 break;
624 }
625 }
626
627 static int in6addr_cmp(const struct in6_addr *addr1,
628 const struct in6_addr *addr2)
629 {
630 unsigned int i;
631 const uint8_t *p1, *p2;
632
633 p1 = (const uint8_t *)addr1;
634 p2 = (const uint8_t *)addr2;
635
636 for (i = 0; i < sizeof(struct in6_addr); i++) {
637 if (p1[i] > p2[i])
638 return 1;
639 else if (p1[i] < p2[i])
640 return -1;
641 }
642 return 0;
643 }
644
645 int sockunion_cmp(const union sockunion *su1, const union sockunion *su2)
646 {
647 if (su1->sa.sa_family > su2->sa.sa_family)
648 return 1;
649 if (su1->sa.sa_family < su2->sa.sa_family)
650 return -1;
651
652 if (su1->sa.sa_family == AF_INET) {
653 if (ntohl(sockunion2ip(su1)) == ntohl(sockunion2ip(su2)))
654 return 0;
655 if (ntohl(sockunion2ip(su1)) > ntohl(sockunion2ip(su2)))
656 return 1;
657 else
658 return -1;
659 }
660 if (su1->sa.sa_family == AF_INET6)
661 return in6addr_cmp(&su1->sin6.sin6_addr, &su2->sin6.sin6_addr);
662 return 0;
663 }
664
665 /* Duplicate sockunion. */
666 union sockunion *sockunion_dup(const union sockunion *su)
667 {
668 union sockunion *dup =
669 XCALLOC(MTYPE_SOCKUNION, sizeof(union sockunion));
670 memcpy(dup, su, sizeof(union sockunion));
671 return dup;
672 }
673
674 void sockunion_free(union sockunion *su)
675 {
676 XFREE(MTYPE_SOCKUNION, su);
677 }
678
679 void sockunion_init(union sockunion *su)
680 {
681 memset(su, 0, sizeof(union sockunion));
682 }