2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Various sorts of access control
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
30 #include "asterisk/network.h"
32 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__)
34 #include <net/route.h>
38 #include <sys/sockio.h>
40 #elif defined(HAVE_GETIFADDRS)
44 #include "asterisk/acl.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/srv.h"
50 #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS))
51 static int get_local_address(struct in_addr
*ourip
)
56 static void score_address(const struct sockaddr_in
*sin
, struct in_addr
*best_addr
, int *best_score
)
61 address
= ast_inet_ntoa(sin
->sin_addr
);
63 /* RFC 1700 alias for the local network */
64 if (address
[0] == '0')
66 /* RFC 1700 localnet */
67 else if (strncmp(address
, "127", 3) == 0)
69 /* RFC 1918 non-public address space */
70 else if (strncmp(address
, "10.", 3) == 0)
72 /* RFC 1918 non-public address space */
73 else if (strncmp(address
, "172", 3) == 0) {
74 /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
75 if (address
[4] == '1' && address
[5] >= '6' && address
[6] == '.')
77 /* 172.20.0.0 - 172.29.255.255, but not 172.200.0.0 - 172.255.255.255 nor 172.2.0.0 - 172.2.255.255 */
78 else if (address
[4] == '2' && address
[6] == '.')
80 /* 172.30.0.0 - 172.31.255.255 */
81 else if (address
[4] == '3' && address
[5] <= '1')
83 /* All other 172 addresses are public */
86 /* RFC 2544 Benchmark test range */
87 } else if (strncmp(address
, "198.1", 5) == 0 && address
[5] >= '8' && address
[6] == '.')
89 /* RFC 1918 non-public address space */
90 else if (strncmp(address
, "192.168", 7) == 0)
92 /* RFC 3330 Zeroconf network */
93 else if (strncmp(address
, "169.254", 7) == 0)
94 /*!\note Better score than a test network, but not quite as good as RFC 1918
95 * address space. The reason is that some Linux distributions automatically
96 * configure a Zeroconf address before trying DHCP, so we want to prefer a
97 * DHCP lease to a Zeroconf address.
100 /* RFC 3330 Test network */
101 else if (strncmp(address
, "192.0.2.", 8) == 0)
103 /* Every other address should be publically routable */
107 if (score
> *best_score
) {
109 memcpy(best_addr
, &sin
->sin_addr
, sizeof(*best_addr
));
113 static int get_local_address(struct in_addr
*ourip
)
117 struct lifreq
*ifr
= NULL
;
120 struct sockaddr_in
*sa
;
124 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
125 struct ifaddrs
*ifap
, *ifaphead
;
127 const struct sockaddr_in
*sin
;
128 #endif /* BSD_OR_LINUX */
129 struct in_addr best_addr
;
130 int best_score
= -100;
131 memset(&best_addr
, 0, sizeof(best_addr
));
133 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
134 rtnerr
= getifaddrs(&ifaphead
);
139 #endif /* BSD_OR_LINUX */
141 s
= socket(AF_INET
, SOCK_STREAM
, 0);
144 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
145 for (ifap
= ifaphead
; ifap
; ifap
= ifap
->ifa_next
) {
147 if (ifap
->ifa_addr
&& ifap
->ifa_addr
->sa_family
== AF_INET
) {
148 sin
= (const struct sockaddr_in
*) ifap
->ifa_addr
;
149 score_address(sin
, &best_addr
, &best_score
);
156 #endif /* BSD_OR_LINUX */
158 /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
160 /* Get a count of interfaces on the machine */
161 ifn
.lifn_family
= AF_INET
;
164 if (ioctl(s
, SIOCGLIFNUM
, &ifn
) < 0) {
169 bufsz
= ifn
.lifn_count
* sizeof(struct lifreq
);
170 if (!(buf
= malloc(bufsz
))) {
174 memset(buf
, 0, bufsz
);
176 /* Get a list of interfaces on the machine */
177 ifc
.lifc_len
= bufsz
;
179 ifc
.lifc_family
= AF_INET
;
181 if (ioctl(s
, SIOCGLIFCONF
, &ifc
) < 0) {
187 for (ifr
= (struct lifreq
*)buf
, x
= 0; x
< ifn
.lifn_count
; ifr
++, x
++) {
188 sa
= (struct sockaddr_in
*)&(ifr
->lifr_addr
);
189 score_address(sa
, &best_addr
, &best_score
);
201 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
202 freeifaddrs(ifaphead
);
203 #endif /* BSD_OR_LINUX */
205 if (res
== 0 && ourip
)
206 memcpy(ourip
, &best_addr
, sizeof(*ourip
));
209 #endif /* HAVE_GETIFADDRS */
211 /* Free HA structure */
212 void ast_free_ha(struct ast_ha
*ha
)
222 /* Copy HA structure */
223 static void ast_copy_ha(struct ast_ha
*from
, struct ast_ha
*to
)
225 memcpy(&to
->netaddr
, &from
->netaddr
, sizeof(from
->netaddr
));
226 memcpy(&to
->netmask
, &from
->netmask
, sizeof(from
->netmask
));
227 to
->sense
= from
->sense
;
230 /* Create duplicate of ha structure */
231 static struct ast_ha
*ast_duplicate_ha(struct ast_ha
*original
)
233 struct ast_ha
*new_ha
;
235 if ((new_ha
= ast_malloc(sizeof(*new_ha
)))) {
236 /* Copy from original to new object */
237 ast_copy_ha(original
, new_ha
);
243 /* Create duplicate HA link list */
244 /* Used in chan_sip2 templates */
245 struct ast_ha
*ast_duplicate_ha_list(struct ast_ha
*original
)
247 struct ast_ha
*start
= original
;
248 struct ast_ha
*ret
= NULL
;
249 struct ast_ha
*link
, *prev
= NULL
;
252 link
= ast_duplicate_ha(start
); /* Create copy of this object */
254 prev
->next
= link
; /* Link previous to this object */
257 ret
= link
; /* Save starting point */
259 start
= start
->next
; /* Go to next object */
260 prev
= link
; /* Save pointer to this object */
262 return ret
; /* Return start of list */
265 struct ast_ha
*ast_append_ha(const char *sense
, const char *stuff
, struct ast_ha
*path
, int *error
)
269 struct ast_ha
*prev
= NULL
;
272 char *tmp
= ast_strdupa(stuff
);
280 ha
= ast_malloc(sizeof(*ha
));
284 nm
= strchr(tmp
, '/');
286 /* assume /32. Yes, htonl does not do anything for this particular mask
287 but we better use it to show we remember about byte order */
288 ha
->netmask
.s_addr
= htonl(0xFFFFFFFF);
293 if (!strchr(nm
, '.')) {
294 if ((sscanf(nm
, "%d", &x
) == 1) && (x
>= 0) && (x
<= 32))
295 ha
->netmask
.s_addr
= htonl(0xFFFFFFFF << (32 - x
));
297 ast_log(LOG_WARNING
, "Invalid CIDR in %s\n", stuff
);
303 } else if (!inet_aton(nm
, &ha
->netmask
)) {
304 ast_log(LOG_WARNING
, "Invalid mask in %s\n", stuff
);
312 if (!inet_aton(tmp
, &ha
->netaddr
)) {
313 ast_log(LOG_WARNING
, "Invalid IP address in %s\n", stuff
);
320 ha
->netaddr
.s_addr
&= ha
->netmask
.s_addr
;
322 ha
->sense
= strncasecmp(sense
, "p", 1) ? AST_SENSE_DENY
: AST_SENSE_ALLOW
;
331 ast_debug(1, "%s/%s sense %d appended to acl for peer\n", ast_strdupa(ast_inet_ntoa(ha
->netaddr
)), ast_strdupa(ast_inet_ntoa(ha
->netmask
)), ha
->sense
);
336 int ast_apply_ha(struct ast_ha
*ha
, struct sockaddr_in
*sin
)
338 /* Start optimistic */
339 int res
= AST_SENSE_ALLOW
;
341 #if 0 /* debugging code */
342 char iabuf
[INET_ADDRSTRLEN
];
343 char iabuf2
[INET_ADDRSTRLEN
];
345 ast_copy_string(iabuf
, ast_inet_ntoa(sin
->sin_addr
), sizeof(iabuf
));
346 ast_copy_string(iabuf2
, ast_inet_ntoa(ha
->netaddr
), sizeof(iabuf2
));
347 ast_debug(1, "##### Testing %s with %s\n", iabuf
, iabuf2
);
349 /* For each rule, if this address and the netmask = the net address
350 apply the current rule */
351 if ((sin
->sin_addr
.s_addr
& ha
->netmask
.s_addr
) == ha
->netaddr
.s_addr
)
358 int ast_get_ip_or_srv(struct sockaddr_in
*sin
, const char *value
, const char *service
)
361 struct ast_hostent ahp
;
364 int tportno
= ntohs(sin
->sin_port
);
366 snprintf(srv
, sizeof(srv
), "%s.%s", service
, value
);
367 if (ast_get_srv(NULL
, host
, sizeof(host
), &tportno
, srv
) > 0) {
368 sin
->sin_port
= htons(tportno
);
372 hp
= ast_gethostbyname(value
, &ahp
);
374 memcpy(&sin
->sin_addr
, hp
->h_addr
, sizeof(sin
->sin_addr
));
376 ast_log(LOG_WARNING
, "Unable to lookup '%s'\n", value
);
382 struct dscp_codepoint
{
387 /* IANA registered DSCP codepoints */
389 static const struct dscp_codepoint dscp_pool1
[] = {
413 int ast_str2cos(const char *value
, unsigned int *cos
)
417 if (sscanf(value
, "%d", &fval
) == 1) {
427 int ast_str2tos(const char *value
, unsigned int *tos
)
432 if (sscanf(value
, "%i", &fval
) == 1) {
437 for (x
= 0; x
< sizeof(dscp_pool1
) / sizeof(dscp_pool1
[0]); x
++) {
438 if (!strcasecmp(value
, dscp_pool1
[x
].name
)) {
439 *tos
= dscp_pool1
[x
].space
<< 2;
447 const char *ast_tos2str(unsigned int tos
)
451 for (x
= 0; x
< sizeof(dscp_pool1
) / sizeof(dscp_pool1
[0]); x
++) {
452 if (dscp_pool1
[x
].space
== (tos
>> 2))
453 return dscp_pool1
[x
].name
;
459 int ast_get_ip(struct sockaddr_in
*sin
, const char *value
)
461 return ast_get_ip_or_srv(sin
, value
, NULL
);
464 int ast_ouraddrfor(struct in_addr
*them
, struct in_addr
*us
)
467 struct sockaddr_in sin
;
470 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
472 ast_log(LOG_ERROR
, "Cannot create socket\n");
475 sin
.sin_family
= AF_INET
;
477 sin
.sin_addr
= *them
;
478 if (connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
))) {
479 ast_log(LOG_WARNING
, "Cannot connect\n");
484 if (getsockname(s
, (struct sockaddr
*)&sin
, &slen
)) {
485 ast_log(LOG_WARNING
, "Cannot get socket name\n");
490 ast_debug(3, "Found IP address for this socket\n");
495 int ast_find_ourip(struct in_addr
*ourip
, struct sockaddr_in bindaddr
)
497 char ourhost
[MAXHOSTNAMELEN
] = "";
498 struct ast_hostent ahp
;
500 struct in_addr saddr
;
502 /* just use the bind address if it is nonzero */
503 if (ntohl(bindaddr
.sin_addr
.s_addr
)) {
504 memcpy(ourip
, &bindaddr
.sin_addr
, sizeof(*ourip
));
505 ast_debug(3, "Attached to given IP address\n");
508 /* try to use our hostname */
509 if (gethostname(ourhost
, sizeof(ourhost
) - 1)) {
510 ast_log(LOG_WARNING
, "Unable to get hostname\n");
512 hp
= ast_gethostbyname(ourhost
, &ahp
);
514 memcpy(ourip
, hp
->h_addr
, sizeof(*ourip
));
515 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost
);
519 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
520 /* A.ROOT-SERVERS.NET. */
521 if (inet_aton("198.41.0.4", &saddr
) && !ast_ouraddrfor(&saddr
, ourip
))
523 return get_local_address(ourip
);