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 */
87 /* RFC 2544 Benchmark test range (198.18.0.0 - 198.19.255.255, but not 198.180.0.0 - 198.199.255.255) */
88 } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') {
90 /* RFC 1918 non-public address space */
91 } else if (strncmp(address, "192.168", 7) == 0) {
93 /* RFC 3330 Zeroconf network */
94 } else if (strncmp(address, "169.254", 7) == 0) {
95 /*!\note Better score than a test network, but not quite as good as RFC 1918
96 * address space. The reason is that some Linux distributions automatically
97 * configure a Zeroconf address before trying DHCP, so we want to prefer a
98 * DHCP lease to a Zeroconf address.
101 /* RFC 3330 Test network */
102 } else if (strncmp(address, "192.0.2.", 8) == 0) {
104 /* Every other address should be publically routable */
109 if (score > *best_score) {
111 memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
115 static int get_local_address(struct in_addr *ourip)
119 struct lifreq *ifr = NULL;
122 struct sockaddr_in *sa;
126 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
127 struct ifaddrs *ifap, *ifaphead;
129 const struct sockaddr_in *sin;
130 #endif /* BSD_OR_LINUX */
131 struct in_addr best_addr;
132 int best_score = -100;
133 memset(&best_addr, 0, sizeof(best_addr));
135 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
136 rtnerr = getifaddrs(&ifaphead);
141 #endif /* BSD_OR_LINUX */
143 s = socket(AF_INET, SOCK_STREAM, 0);
146 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
147 for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
149 if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) {
150 sin = (const struct sockaddr_in *) ifap->ifa_addr;
151 score_address(sin, &best_addr, &best_score);
154 if (best_score == 0) {
159 #endif /* BSD_OR_LINUX */
161 /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
163 /* Get a count of interfaces on the machine */
164 ifn.lifn_family = AF_INET;
167 if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
172 bufsz = ifn.lifn_count * sizeof(struct lifreq);
173 if (!(buf = malloc(bufsz))) {
177 memset(buf, 0, bufsz);
179 /* Get a list of interfaces on the machine */
180 ifc.lifc_len = bufsz;
182 ifc.lifc_family = AF_INET;
184 if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
190 for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) {
191 sa = (struct sockaddr_in *)&(ifr->lifr_addr);
192 score_address(sa, &best_addr, &best_score);
195 if (best_score == 0) {
205 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
206 freeifaddrs(ifaphead);
207 #endif /* BSD_OR_LINUX */
209 if (res == 0 && ourip) {
210 memcpy(ourip, &best_addr, sizeof(*ourip));
214 #endif /* HAVE_GETIFADDRS */
216 /* Free HA structure */
217 void ast_free_ha(struct ast_ha *ha)
227 /* Copy HA structure */
228 void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to)
230 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
231 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
232 to->sense = from->sense;
235 /* Create duplicate of ha structure */
236 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
238 struct ast_ha *new_ha;
240 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
241 /* Copy from original to new object */
242 ast_copy_ha(original, new_ha);
248 /* Create duplicate HA link list */
249 /* Used in chan_sip2 templates */
250 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
252 struct ast_ha *start = original;
253 struct ast_ha *ret = NULL;
254 struct ast_ha *current, *prev = NULL;
257 current = ast_duplicate_ha(start); /* Create copy of this object */
259 prev->next = current; /* Link previous to this object */
263 ret = current; /* Save starting point */
266 start = start->next; /* Go to next object */
267 prev = current; /* Save pointer to this object */
269 return ret; /* Return start of list */
272 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
276 struct ast_ha *prev = NULL;
279 char *tmp = ast_strdupa(stuff);
287 if (!(ha = ast_malloc(sizeof(*ha)))) {
291 if (!(nm = strchr(tmp, '/'))) {
292 /* assume /32. Yes, htonl does not do anything for this particular mask
293 but we better use it to show we remember about byte order */
294 ha->netmask.s_addr = htonl(0xFFFFFFFF);
299 if (!strchr(nm, '.')) {
300 if ((sscanf(nm, "%30d", &x) == 1) && (x >= 0) && (x <= 32)) {
301 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
303 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
310 } else if (!inet_aton(nm, &ha->netmask)) {
311 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
320 if (!inet_aton(tmp, &ha->netaddr)) {
321 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
329 ha->netaddr.s_addr &= ha->netmask.s_addr;
331 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
340 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);
345 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
347 /* Start optimistic */
348 int res = AST_SENSE_ALLOW;
350 #if 0 /* debugging code */
351 char iabuf[INET_ADDRSTRLEN];
352 char iabuf2[INET_ADDRSTRLEN];
354 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
355 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
356 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
358 /* For each rule, if this address and the netmask = the net address
359 apply the current rule */
360 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr) {
368 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
371 struct ast_hostent ahp;
374 int tportno = ntohs(sin->sin_port);
376 snprintf(srv, sizeof(srv), "%s.%s", service, value);
377 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
378 sin->sin_port = htons(tportno);
382 if ((hp = ast_gethostbyname(value, &ahp))) {
383 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
385 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
391 struct dscp_codepoint {
396 /* IANA registered DSCP codepoints */
398 static const struct dscp_codepoint dscp_pool1[] = {
422 int ast_str2cos(const char *value, unsigned int *cos)
426 if (sscanf(value, "%30d", &fval) == 1) {
436 int ast_str2tos(const char *value, unsigned int *tos)
441 if (sscanf(value, "%30i", &fval) == 1) {
446 for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
447 if (!strcasecmp(value, dscp_pool1[x].name)) {
448 *tos = dscp_pool1[x].space << 2;
456 const char *ast_tos2str(unsigned int tos)
460 for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) {
461 if (dscp_pool1[x].space == (tos >> 2)) {
462 return dscp_pool1[x].name;
469 int ast_get_ip(struct sockaddr_in *sin, const char *value)
471 return ast_get_ip_or_srv(sin, value, NULL);
474 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
477 struct sockaddr_in sin;
480 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
481 ast_log(LOG_ERROR, "Cannot create socket\n");
484 sin.sin_family = AF_INET;
485 sin.sin_port = htons(5060);
486 sin.sin_addr = *them;
487 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
488 ast_log(LOG_WARNING, "Cannot connect\n");
493 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
494 ast_log(LOG_WARNING, "Cannot get socket name\n");
499 ast_debug(3, "Found IP address for this socket\n");
504 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
506 char ourhost[MAXHOSTNAMELEN] = "";
507 struct ast_hostent ahp;
509 struct in_addr saddr;
511 /* just use the bind address if it is nonzero */
512 if (ntohl(bindaddr.sin_addr.s_addr)) {
513 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
514 ast_debug(3, "Attached to given IP address\n");
517 /* try to use our hostname */
518 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
519 ast_log(LOG_WARNING, "Unable to get hostname\n");
521 if ((hp = ast_gethostbyname(ourhost, &ahp))) {
522 memcpy(ourip, hp->h_addr, sizeof(*ourip));
523 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
527 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
528 /* A.ROOT-SERVERS.NET. */
529 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip)) {
532 return get_local_address(ourip);