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$")
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <sys/ioctl.h>
47 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
49 #include <net/route.h>
53 #include <sys/sockio.h>
56 /* netinet/ip.h may not define the following (See RFCs 791 and 1349) */
57 #if !defined(IPTOS_LOWCOST)
58 #define IPTOS_LOWCOST 0x02
61 #if !defined(IPTOS_MINCOST)
62 #define IPTOS_MINCOST IPTOS_LOWCOST
65 #include "asterisk/acl.h"
66 #include "asterisk/logger.h"
67 #include "asterisk/channel.h"
68 #include "asterisk/options.h"
69 #include "asterisk/utils.h"
70 #include "asterisk/lock.h"
71 #include "asterisk/srv.h"
74 /* Host access rule */
75 struct in_addr netaddr
;
76 struct in_addr netmask
;
81 /* Default IP - if not otherwise set, don't breathe garbage */
82 static struct in_addr __ourip
= { 0x00000000 };
85 char ifrn_name
[IFNAMSIZ
]; /* Interface name, e.g. "eth0", "ppp0", etc. */
86 struct sockaddr_in ifru_addr
;
89 /* Free HA structure */
90 void ast_free_ha(struct ast_ha
*ha
)
100 /* Copy HA structure */
101 static void ast_copy_ha(struct ast_ha
*from
, struct ast_ha
*to
)
103 memcpy(&to
->netaddr
, &from
->netaddr
, sizeof(from
->netaddr
));
104 memcpy(&to
->netmask
, &from
->netmask
, sizeof(from
->netmask
));
105 to
->sense
= from
->sense
;
108 /* Create duplicate of ha structure */
109 static struct ast_ha
*ast_duplicate_ha(struct ast_ha
*original
)
111 struct ast_ha
*new_ha
;
113 if ((new_ha
= ast_malloc(sizeof(*new_ha
)))) {
114 /* Copy from original to new object */
115 ast_copy_ha(original
, new_ha
);
121 /* Create duplicate HA link list */
122 /* Used in chan_sip2 templates */
123 struct ast_ha
*ast_duplicate_ha_list(struct ast_ha
*original
)
125 struct ast_ha
*start
= original
;
126 struct ast_ha
*ret
= NULL
;
127 struct ast_ha
*link
, *prev
= NULL
;
130 link
= ast_duplicate_ha(start
); /* Create copy of this object */
132 prev
->next
= link
; /* Link previous to this object */
135 ret
= link
; /* Save starting point */
137 start
= start
->next
; /* Go to next object */
138 prev
= link
; /* Save pointer to this object */
140 return ret
; /* Return start of list */
143 struct ast_ha
*ast_append_ha(char *sense
, char *stuff
, struct ast_ha
*path
)
146 char *nm
= "255.255.255.255";
148 struct ast_ha
*prev
= NULL
;
158 if ((ha
= ast_malloc(sizeof(*ha
)))) {
159 ast_copy_string(tmp
, stuff
, sizeof(tmp
));
160 nm
= strchr(tmp
, '/');
162 nm
= "255.255.255.255";
167 if (!strchr(nm
, '.')) {
168 if ((sscanf(nm
, "%d", &x
) == 1) && (x
>= 0) && (x
<= 32)) {
170 for (z
= 0; z
< x
; z
++) {
174 ha
->netmask
.s_addr
= htonl(y
);
176 } else if (!inet_aton(nm
, &ha
->netmask
)) {
177 ast_log(LOG_WARNING
, "%s is not a valid netmask\n", nm
);
181 if (!inet_aton(tmp
, &ha
->netaddr
)) {
182 ast_log(LOG_WARNING
, "%s is not a valid IP\n", tmp
);
186 ha
->netaddr
.s_addr
&= ha
->netmask
.s_addr
;
187 if (!strncasecmp(sense
, "p", 1)) {
188 ha
->sense
= AST_SENSE_ALLOW
;
190 ha
->sense
= AST_SENSE_DENY
;
199 ast_log(LOG_DEBUG
, "%s/%s appended to acl for peer\n", stuff
, nm
);
203 int ast_apply_ha(struct ast_ha
*ha
, struct sockaddr_in
*sin
)
205 /* Start optimistic */
206 int res
= AST_SENSE_ALLOW
;
208 char iabuf
[INET_ADDRSTRLEN
];
209 char iabuf2
[INET_ADDRSTRLEN
];
211 ast_copy_string(iabuf
, ast_inet_ntoa(sin
->sin_addr
), sizeof(iabuf
));
212 ast_copy_string(iabuf2
, ast_inet_ntoa(ha
->netaddr
), sizeof(iabuf2
));
213 ast_log(LOG_DEBUG
, "##### Testing %s with %s\n", iabuf
, iabuf2
);
214 /* For each rule, if this address and the netmask = the net address
215 apply the current rule */
216 if ((sin
->sin_addr
.s_addr
& ha
->netmask
.s_addr
) == ha
->netaddr
.s_addr
)
223 int ast_get_ip_or_srv(struct sockaddr_in
*sin
, const char *value
, const char *service
)
226 struct ast_hostent ahp
;
229 int tportno
= ntohs(sin
->sin_port
);
230 if (inet_aton(value
, &sin
->sin_addr
))
233 snprintf(srv
, sizeof(srv
), "%s.%s", service
, value
);
234 if (ast_get_srv(NULL
, host
, sizeof(host
), &tportno
, srv
) > 0) {
235 sin
->sin_port
= htons(tportno
);
239 hp
= ast_gethostbyname(value
, &ahp
);
241 memcpy(&sin
->sin_addr
, hp
->h_addr
, sizeof(sin
->sin_addr
));
243 ast_log(LOG_WARNING
, "Unable to lookup '%s'\n", value
);
249 struct dscp_codepoint
{
254 /* IANA registered DSCP codepoints */
256 static const struct dscp_codepoint dscp_pool1
[] = {
280 int ast_str2tos(const char *value
, unsigned int *tos
)
285 if (sscanf(value
, "%i", &fval
) == 1) {
290 for (x
= 0; x
< sizeof(dscp_pool1
) / sizeof(dscp_pool1
[0]); x
++) {
291 if (!strcasecmp(value
, dscp_pool1
[x
].name
)) {
292 *tos
= dscp_pool1
[x
].space
<< 2;
297 if (!strcasecmp(value
, "lowdelay"))
298 *tos
= IPTOS_LOWDELAY
;
299 else if (!strcasecmp(value
, "throughput"))
300 *tos
= IPTOS_THROUGHPUT
;
301 else if (!strcasecmp(value
, "reliability"))
302 *tos
= IPTOS_RELIABILITY
;
303 else if (!strcasecmp(value
, "mincost"))
304 *tos
= IPTOS_MINCOST
;
305 else if (!strcasecmp(value
, "none"))
310 ast_log(LOG_WARNING
, "TOS value %s is deprecated. Please see doc/ip-tos.txt for more information.\n", value
);
315 const char *ast_tos2str(unsigned int tos
)
324 case IPTOS_THROUGHPUT
:
326 case IPTOS_RELIABILITY
:
327 return "reliability";
331 for (x
= 0; x
< sizeof(dscp_pool1
) / sizeof(dscp_pool1
[0]); x
++) {
332 if (dscp_pool1
[x
].space
== (tos
>> 2))
333 return dscp_pool1
[x
].name
;
340 int ast_get_ip(struct sockaddr_in
*sin
, const char *value
)
342 return ast_get_ip_or_srv(sin
, value
, NULL
);
345 /* iface is the interface (e.g. eth0); address is the return value */
346 int ast_lookup_iface(char *iface
, struct in_addr
*address
)
349 struct my_ifreq ifreq
;
351 memset(&ifreq
, 0, sizeof(ifreq
));
352 ast_copy_string(ifreq
.ifrn_name
, iface
, sizeof(ifreq
.ifrn_name
));
354 mysock
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_IP
);
355 res
= ioctl(mysock
, SIOCGIFADDR
, &ifreq
);
359 ast_log(LOG_WARNING
, "Unable to get IP of %s: %s\n", iface
, strerror(errno
));
360 memcpy((char *)address
, (char *)&__ourip
, sizeof(__ourip
));
363 memcpy((char *)address
, (char *)&ifreq
.ifru_addr
.sin_addr
, sizeof(ifreq
.ifru_addr
.sin_addr
));
368 int ast_ouraddrfor(struct in_addr
*them
, struct in_addr
*us
)
371 struct sockaddr_in sin
;
374 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
376 ast_log(LOG_WARNING
, "Cannot create socket\n");
379 sin
.sin_family
= AF_INET
;
381 sin
.sin_addr
= *them
;
382 if (connect(s
, (struct sockaddr
*)&sin
, sizeof(sin
))) {
383 ast_log(LOG_WARNING
, "Cannot connect\n");
388 if (getsockname(s
, (struct sockaddr
*)&sin
, &slen
)) {
389 ast_log(LOG_WARNING
, "Cannot get socket name\n");
398 int ast_find_ourip(struct in_addr
*ourip
, struct sockaddr_in bindaddr
)
400 char ourhost
[MAXHOSTNAMELEN
] = "";
401 struct ast_hostent ahp
;
403 struct in_addr saddr
;
405 /* just use the bind address if it is nonzero */
406 if (ntohl(bindaddr
.sin_addr
.s_addr
)) {
407 memcpy(ourip
, &bindaddr
.sin_addr
, sizeof(*ourip
));
410 /* try to use our hostname */
411 if (gethostname(ourhost
, sizeof(ourhost
) - 1)) {
412 ast_log(LOG_WARNING
, "Unable to get hostname\n");
414 hp
= ast_gethostbyname(ourhost
, &ahp
);
416 memcpy(ourip
, hp
->h_addr
, sizeof(*ourip
));
420 /* A.ROOT-SERVERS.NET. */
421 if (inet_aton("198.41.0.4", &saddr
) && !ast_ouraddrfor(&saddr
, ourip
))