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