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>
41 #if defined(__Darwin__) || defined(__linux__)
45 #include "asterisk/acl.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/utils.h"
48 #include "asterisk/lock.h"
49 #include "asterisk/srv.h"
51 static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
56 address = ast_inet_ntoa(sin->sin_addr);
58 /* RFC 1700 alias for the local network */
59 if (address[0] == '0')
61 /* RFC 1700 localnet */
62 else if (strncmp(address, "127", 3) == 0)
64 /* RFC 1918 non-public address space */
65 else if (strncmp(address, "10.", 3) == 0)
67 /* RFC 1918 non-public address space */
68 else if (strncmp(address, "172", 3) == 0) {
69 /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
70 if (address[4] == '1' && address[5] >= '6' && address[6] == '.')
72 /* 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 */
73 else if (address[4] == '2' && address[6] == '.')
75 /* 172.30.0.0 - 172.31.255.255 */
76 else if (address[4] == '3' && address[5] <= '1')
78 /* All other 172 addresses are public */
81 /* RFC 2544 Benchmark test range */
82 } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.')
84 /* RFC 1918 non-public address space */
85 else if (strncmp(address, "192.168", 7) == 0)
87 /* RFC 3330 Zeroconf network */
88 else if (strncmp(address, "169.254", 7) == 0)
89 /*!\note Better score than a test network, but not quite as good as RFC 1918
90 * address space. The reason is that some Linux distributions automatically
91 * configure a Zeroconf address before trying DHCP, so we want to prefer a
92 * DHCP lease to a Zeroconf address.
95 /* RFC 3330 Test network */
96 else if (strncmp(address, "192.0.2.", 8) == 0)
98 /* Every other address should be publically routable */
102 if (score > *best_score) {
104 memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
108 static int get_local_address(struct in_addr *ourip)
112 struct lifreq *ifr = NULL;
115 struct sockaddr_in *sa;
119 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
120 struct ifaddrs *ifap, *ifaphead;
122 const struct sockaddr_in *sin;
123 #endif /* BSD_OR_LINUX */
124 struct in_addr best_addr = { 0, };
125 int best_score = -100;
127 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
128 rtnerr = getifaddrs(&ifaphead);
133 #endif /* BSD_OR_LINUX */
135 s = socket(AF_INET, SOCK_STREAM, 0);
138 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
139 for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
141 if (ifap->ifa_addr->sa_family == AF_INET) {
142 sin = (const struct sockaddr_in *) ifap->ifa_addr;
143 score_address(sin, &best_addr, &best_score);
150 #endif /* BSD_OR_LINUX */
152 /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
154 /* Get a count of interfaces on the machine */
155 ifn.lifn_family = AF_INET;
158 if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
163 bufsz = ifn.lifn_count * sizeof(struct lifreq);
164 if (!(buf = malloc(bufsz))) {
168 memset(buf, 0, bufsz);
170 /* Get a list of interfaces on the machine */
171 ifc.lifc_len = bufsz;
173 ifc.lifc_family = AF_INET;
175 if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
181 for (ifr = (struct lifreq *)buf, x = 0; x < ifn.lifn_count; ifr++, x++) {
182 sa = (struct sockaddr_in *)&(ifr->lifr_addr);
183 score_address(sin, &best_addr, &best_score);
195 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__)
196 freeifaddrs(ifaphead);
197 #endif /* BSD_OR_LINUX */
199 if (res == 0 && ourip)
200 memcpy(ourip, &best_addr, sizeof(*ourip));
203 /* Free HA structure */
204 void ast_free_ha(struct ast_ha *ha)
214 /* Copy HA structure */
215 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
217 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
218 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
219 to->sense = from->sense;
222 /* Create duplicate of ha structure */
223 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
225 struct ast_ha *new_ha;
227 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
228 /* Copy from original to new object */
229 ast_copy_ha(original, new_ha);
235 /* Create duplicate HA link list */
236 /* Used in chan_sip2 templates */
237 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
239 struct ast_ha *start = original;
240 struct ast_ha *ret = NULL;
241 struct ast_ha *link, *prev = NULL;
244 link = ast_duplicate_ha(start); /* Create copy of this object */
246 prev->next = link; /* Link previous to this object */
249 ret = link; /* Save starting point */
251 start = start->next; /* Go to next object */
252 prev = link; /* Save pointer to this object */
254 return ret; /* Return start of list */
257 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
261 struct ast_ha *prev = NULL;
264 char *tmp = ast_strdupa(stuff);
272 ha = ast_malloc(sizeof(*ha));
276 nm = strchr(tmp, '/');
278 /* assume /32. Yes, htonl does not do anything for this particular mask
279 but we better use it to show we remember about byte order */
280 ha->netmask.s_addr = htonl(0xFFFFFFFF);
285 if (!strchr(nm, '.')) {
286 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
287 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
289 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
295 } else if (!inet_aton(nm, &ha->netmask)) {
296 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
304 if (!inet_aton(tmp, &ha->netaddr)) {
305 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
312 ha->netaddr.s_addr &= ha->netmask.s_addr;
314 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
323 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);
328 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
330 /* Start optimistic */
331 int res = AST_SENSE_ALLOW;
333 #if 0 /* debugging code */
334 char iabuf[INET_ADDRSTRLEN];
335 char iabuf2[INET_ADDRSTRLEN];
337 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
338 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
339 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
341 /* For each rule, if this address and the netmask = the net address
342 apply the current rule */
343 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
350 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
353 struct ast_hostent ahp;
356 int tportno = ntohs(sin->sin_port);
358 snprintf(srv, sizeof(srv), "%s.%s", service, value);
359 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
360 sin->sin_port = htons(tportno);
364 hp = ast_gethostbyname(value, &ahp);
366 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
368 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
374 struct dscp_codepoint {
379 /* IANA registered DSCP codepoints */
381 static const struct dscp_codepoint dscp_pool1[] = {
405 int ast_str2cos(const char *value, unsigned int *cos)
409 if (sscanf(value, "%d", &fval) == 1) {
419 int ast_str2tos(const char *value, unsigned int *tos)
424 if (sscanf(value, "%i", &fval) == 1) {
429 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
430 if (!strcasecmp(value, dscp_pool1[x].name)) {
431 *tos = dscp_pool1[x].space << 2;
439 const char *ast_tos2str(unsigned int tos)
443 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
444 if (dscp_pool1[x].space == (tos >> 2))
445 return dscp_pool1[x].name;
451 int ast_get_ip(struct sockaddr_in *sin, const char *value)
453 return ast_get_ip_or_srv(sin, value, NULL);
456 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
459 struct sockaddr_in sin;
462 s = socket(PF_INET, SOCK_DGRAM, 0);
464 ast_log(LOG_ERROR, "Cannot create socket\n");
467 sin.sin_family = AF_INET;
469 sin.sin_addr = *them;
470 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
471 ast_log(LOG_WARNING, "Cannot connect\n");
476 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
477 ast_log(LOG_WARNING, "Cannot get socket name\n");
482 ast_debug(3, "Found IP address for this socket\n");
487 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
489 char ourhost[MAXHOSTNAMELEN] = "";
490 struct ast_hostent ahp;
492 struct in_addr saddr;
494 /* just use the bind address if it is nonzero */
495 if (ntohl(bindaddr.sin_addr.s_addr)) {
496 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
497 ast_debug(3, "Attached to given IP address\n");
500 /* try to use our hostname */
501 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
502 ast_log(LOG_WARNING, "Unable to get hostname\n");
504 hp = ast_gethostbyname(ourhost, &ahp);
506 memcpy(ourip, hp->h_addr, sizeof(*ourip));
507 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
511 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
512 /* A.ROOT-SERVERS.NET. */
513 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
515 return get_local_address(ourip);