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"
33 #include <sys/sockio.h>
36 #if defined(__linux__)
40 #include "asterisk/acl.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/srv.h"
46 static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score)
51 address = ast_inet_ntoa(sin->sin_addr);
53 /* RFC 1700 alias for the local network */
54 if (address[0] == '0')
56 /* RFC 1700 localnet */
57 else if (strncmp(address, "127", 3) == 0)
59 /* RFC 1918 non-public address space */
60 else if (strncmp(address, "10.", 3) == 0)
62 /* RFC 1918 non-public address space */
63 else if (strncmp(address, "172", 3) == 0) {
64 /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */
65 if (address[4] == '1' && address[5] >= '6' && address[6] == '.')
67 /* 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 */
68 else if (address[4] == '2' && address[6] == '.')
70 /* 172.30.0.0 - 172.31.255.255 */
71 else if (address[4] == '3' && address[5] <= '1')
73 /* All other 172 addresses are public */
76 /* RFC 2544 Benchmark test range */
77 } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.')
79 /* RFC 1918 non-public address space */
80 else if (strncmp(address, "192.168", 7) == 0)
82 /* RFC 3330 Zeroconf network */
83 else if (strncmp(address, "169.254", 7) == 0)
84 /*!\note Better score than a test network, but not quite as good as RFC 1918
85 * address space. The reason is that some Linux distributions automatically
86 * configure a Zeroconf address before trying DHCP, so we want to prefer a
87 * DHCP lease to a Zeroconf address.
90 /* RFC 3330 Test network */
91 else if (strncmp(address, "192.0.2.", 8) == 0)
93 /* Every other address should be publically routable */
97 if (score > *best_score) {
99 memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr));
103 static int get_local_address(struct in_addr *ourip)
107 struct lifreq *ifr = NULL;
110 struct sockaddr_in *sa;
113 #endif /* _SOLARIS */
114 #if defined(_BSD) || defined(__linux__)
115 struct ifaddrs *ifap, *ifaphead;
117 const struct sockaddr_in *sin;
118 #endif /* defined(_BSD) || defined(_LINUX) */
119 struct in_addr best_addr = { 0, };
120 int best_score = -100;
122 #if defined(_BSD) || defined(__linux__)
123 rtnerr = getifaddrs(&ifaphead);
128 #endif /* BSD_OR_LINUX */
130 s = socket(AF_INET, SOCK_STREAM, 0);
133 #if defined(_BSD) || defined(__linux__)
134 for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {
136 if (ifap->ifa_addr->sa_family == AF_INET) {
137 sin = (const struct sockaddr_in *) ifap->ifa_addr;
138 score_address(sin, &best_addr, &best_score);
147 /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */
149 /* Get a count of interfaces on the machine */
150 ifn.lifn_family = AF_INET;
153 if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) {
158 bufsz = ifn.lifn_count * sizeof(struct lifreq);
159 if (!(buf = malloc(bufsz))) {
163 memset(buf, 0, bufsz);
165 /* Get a list of interfaces on the machine */
166 ifc.lifc_len = bufsz;
168 ifc.lifc_family = AF_INET;
170 if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) {
176 for (ifr = (struct lifreq *)buf, x = 0; x < ifn.lifn_count; ifr++, x++) {
177 sa = (struct sockaddr_in *)&(ifr->lifr_addr);
178 score_address(sin, &best_addr, &best_score);
186 #endif /* _SOLARIS */
190 #if defined(_BSD) || defined(__linux__)
191 freeifaddrs(ifaphead);
194 if (res == 0 && ourip)
195 memcpy(ourip, &best_addr, sizeof(*ourip));
198 /* Free HA structure */
199 void ast_free_ha(struct ast_ha *ha)
209 /* Copy HA structure */
210 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
212 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
213 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
214 to->sense = from->sense;
217 /* Create duplicate of ha structure */
218 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
220 struct ast_ha *new_ha;
222 if ((new_ha = ast_malloc(sizeof(*new_ha)))) {
223 /* Copy from original to new object */
224 ast_copy_ha(original, new_ha);
230 /* Create duplicate HA link list */
231 /* Used in chan_sip2 templates */
232 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
234 struct ast_ha *start = original;
235 struct ast_ha *ret = NULL;
236 struct ast_ha *link, *prev = NULL;
239 link = ast_duplicate_ha(start); /* Create copy of this object */
241 prev->next = link; /* Link previous to this object */
244 ret = link; /* Save starting point */
246 start = start->next; /* Go to next object */
247 prev = link; /* Save pointer to this object */
249 return ret; /* Return start of list */
252 struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
256 struct ast_ha *prev = NULL;
259 char *tmp = ast_strdupa(stuff);
267 ha = ast_malloc(sizeof(*ha));
271 nm = strchr(tmp, '/');
273 /* assume /32. Yes, htonl does not do anything for this particular mask
274 but we better use it to show we remember about byte order */
275 ha->netmask.s_addr = htonl(0xFFFFFFFF);
280 if (!strchr(nm, '.')) {
281 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32))
282 ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x));
284 ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff);
290 } else if (!inet_aton(nm, &ha->netmask)) {
291 ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff);
299 if (!inet_aton(tmp, &ha->netaddr)) {
300 ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff);
307 ha->netaddr.s_addr &= ha->netmask.s_addr;
309 ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW;
318 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);
323 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
325 /* Start optimistic */
326 int res = AST_SENSE_ALLOW;
328 #if 0 /* debugging code */
329 char iabuf[INET_ADDRSTRLEN];
330 char iabuf2[INET_ADDRSTRLEN];
332 ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf));
333 ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2));
334 ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2);
336 /* For each rule, if this address and the netmask = the net address
337 apply the current rule */
338 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
345 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
348 struct ast_hostent ahp;
351 int tportno = ntohs(sin->sin_port);
353 snprintf(srv, sizeof(srv), "%s.%s", service, value);
354 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
355 sin->sin_port = htons(tportno);
359 hp = ast_gethostbyname(value, &ahp);
361 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
363 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
369 struct dscp_codepoint {
374 /* IANA registered DSCP codepoints */
376 static const struct dscp_codepoint dscp_pool1[] = {
400 int ast_str2cos(const char *value, unsigned int *cos)
404 if (sscanf(value, "%d", &fval) == 1) {
414 int ast_str2tos(const char *value, unsigned int *tos)
419 if (sscanf(value, "%i", &fval) == 1) {
424 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
425 if (!strcasecmp(value, dscp_pool1[x].name)) {
426 *tos = dscp_pool1[x].space << 2;
434 const char *ast_tos2str(unsigned int tos)
438 for (x = 0; x < sizeof(dscp_pool1) / sizeof(dscp_pool1[0]); x++) {
439 if (dscp_pool1[x].space == (tos >> 2))
440 return dscp_pool1[x].name;
446 int ast_get_ip(struct sockaddr_in *sin, const char *value)
448 return ast_get_ip_or_srv(sin, value, NULL);
451 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
454 struct sockaddr_in sin;
457 s = socket(PF_INET, SOCK_DGRAM, 0);
459 ast_log(LOG_ERROR, "Cannot create socket\n");
462 sin.sin_family = AF_INET;
464 sin.sin_addr = *them;
465 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
466 ast_log(LOG_WARNING, "Cannot connect\n");
471 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
472 ast_log(LOG_WARNING, "Cannot get socket name\n");
477 ast_debug(3, "Found IP address for this socket\n");
482 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
484 char ourhost[MAXHOSTNAMELEN] = "";
485 struct ast_hostent ahp;
487 struct in_addr saddr;
489 /* just use the bind address if it is nonzero */
490 if (ntohl(bindaddr.sin_addr.s_addr)) {
491 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
492 ast_debug(3, "Attached to given IP address\n");
495 /* try to use our hostname */
496 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
497 ast_log(LOG_WARNING, "Unable to get hostname\n");
499 hp = ast_gethostbyname(ourhost, &ahp);
501 memcpy(ourip, hp->h_addr, sizeof(*ourip));
502 ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost);
506 ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n");
507 /* A.ROOT-SERVERS.NET. */
508 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))
510 return get_local_address(ourip);