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