2 * Asterisk -- A telephony toolkit for Linux.
4 * Various sorts of access control
6 * Copyright (C) 1999-2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/in_systm.h>
28 #include <netinet/ip.h>
29 #include <sys/ioctl.h>
31 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
33 #include <net/route.h>
37 #include <sys/sockio.h>
38 /* netinet/ip.h does not define the following (See RFCs 791 and 1349) */
39 #define IPTOS_LOWCOST 0x02
40 #define IPTOS_MINCOST IPTOS_LOWCOST
45 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47 #include "asterisk/acl.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/options.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/lock.h"
53 #include "asterisk/srv.h"
55 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
56 AST_MUTEX_DEFINE_STATIC(routeseq_lock);
60 /* Host access rule */
61 struct in_addr netaddr;
62 struct in_addr netmask;
67 /* Default IP - if not otherwise set, don't breathe garbage */
68 static struct in_addr __ourip = { 0x00000000 };
71 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
72 struct sockaddr_in ifru_addr;
75 /* Free HA structure */
76 void ast_free_ha(struct ast_ha *ha)
86 /* Copy HA structure */
87 static void ast_copy_ha(struct ast_ha *from, struct ast_ha *to)
89 memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr));
90 memcpy(&to->netmask, &from->netmask, sizeof(from->netmask));
91 to->sense = from->sense;
94 /* Create duplicate of ha structure */
95 static struct ast_ha *ast_duplicate_ha(struct ast_ha *original)
97 struct ast_ha *new_ha = malloc(sizeof(struct ast_ha));
98 /* Copy from original to new object */
99 ast_copy_ha(original, new_ha);
104 /* Create duplicate HA link list */
105 /* Used in chan_sip2 templates */
106 struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original)
108 struct ast_ha *start=original;
109 struct ast_ha *ret = NULL;
110 struct ast_ha *link,*prev=NULL;
113 link = ast_duplicate_ha(start); /* Create copy of this object */
115 prev->next = link; /* Link previous to this object */
118 ret = link; /* Save starting point */
120 start = start->next; /* Go to next object */
121 prev = link; /* Save pointer to this object */
123 return ret; /* Return start of list */
126 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
128 struct ast_ha *ha = malloc(sizeof(struct ast_ha));
129 char *nm = "255.255.255.255";
131 struct ast_ha *prev = NULL;
141 ast_copy_string(tmp, stuff, sizeof(tmp));
142 nm = strchr(tmp, '/');
144 nm = "255.255.255.255";
149 if (!strchr(nm, '.')) {
150 if ((sscanf(nm, "%d", &x) == 1) && (x >= 0) && (x <= 32)) {
156 ha->netmask.s_addr = htonl(y);
158 } else if (!inet_aton(nm, &ha->netmask)) {
159 ast_log(LOG_WARNING, "%s is not a valid netmask\n", nm);
163 if (!inet_aton(tmp, &ha->netaddr)) {
164 ast_log(LOG_WARNING, "%s is not a valid IP\n", tmp);
168 ha->netaddr.s_addr &= ha->netmask.s_addr;
169 if (!strncasecmp(sense, "p", 1)) {
170 ha->sense = AST_SENSE_ALLOW;
172 ha->sense = AST_SENSE_DENY;
181 ast_log(LOG_DEBUG, "%s/%s appended to acl for peer\n", stuff, nm);
185 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
187 /* Start optimistic */
188 int res = AST_SENSE_ALLOW;
190 char iabuf[INET_ADDRSTRLEN];
191 char iabuf2[INET_ADDRSTRLEN];
194 "##### Testing %s with %s\n",
195 ast_inet_ntoa(iabuf, sizeof(iabuf), sin->sin_addr),
196 ast_inet_ntoa(iabuf2, sizeof(iabuf2), ha->netaddr));
197 /* For each rule, if this address and the netmask = the net address
198 apply the current rule */
199 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr)
206 int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service)
209 struct ast_hostent ahp;
212 int tportno = ntohs(sin->sin_port);
213 if (inet_aton(value, &sin->sin_addr))
216 snprintf(srv, sizeof(srv), "%s.%s", service, value);
217 if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) {
218 sin->sin_port = htons(tportno);
222 hp = ast_gethostbyname(value, &ahp);
224 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
226 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
232 int ast_str2tos(const char *value, int *tos)
235 if (sscanf(value, "%i", &fval) == 1)
237 else if (!strcasecmp(value, "lowdelay"))
238 *tos = IPTOS_LOWDELAY;
239 else if (!strcasecmp(value, "throughput"))
240 *tos = IPTOS_THROUGHPUT;
241 else if (!strcasecmp(value, "reliability"))
242 *tos = IPTOS_RELIABILITY;
243 else if (!strcasecmp(value, "mincost"))
244 *tos = IPTOS_MINCOST;
245 else if (!strcasecmp(value, "none"))
252 int ast_get_ip(struct sockaddr_in *sin, const char *value)
254 return ast_get_ip_or_srv(sin, value, NULL);
257 /* iface is the interface (e.g. eth0); address is the return value */
258 int ast_lookup_iface(char *iface, struct in_addr *address)
261 struct my_ifreq ifreq;
263 memset(&ifreq, 0, sizeof(ifreq));
264 ast_copy_string(ifreq.ifrn_name, iface, sizeof(ifreq.ifrn_name));
266 mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
267 res = ioctl(mysock, SIOCGIFADDR, &ifreq);
271 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
272 memcpy((char *)address, (char *)&__ourip, sizeof(__ourip));
275 memcpy((char *)address, (char *)&ifreq.ifru_addr.sin_addr, sizeof(ifreq.ifru_addr.sin_addr));
280 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
283 struct sockaddr_in sin;
286 s = socket(PF_INET, SOCK_DGRAM, 0);
288 ast_log(LOG_WARNING, "Cannot create socket\n");
291 sin.sin_family = AF_INET;
293 sin.sin_addr = *them;
294 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) {
295 ast_log(LOG_WARNING, "Cannot connect\n");
300 if (getsockname(s, (struct sockaddr *)&sin, &slen)) {
301 ast_log(LOG_WARNING, "Cannot get socket name\n");
310 int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr)
312 char ourhost[MAXHOSTNAMELEN] = "";
313 struct ast_hostent ahp;
315 struct in_addr saddr;
317 /* just use the bind address if it is nonzero */
318 if (ntohl(bindaddr.sin_addr.s_addr)) {
319 memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip));
322 /* try to use our hostname */
323 if (gethostname(ourhost, sizeof(ourhost) - 1)) {
324 ast_log(LOG_WARNING, "Unable to get hostname\n");
326 hp = ast_gethostbyname(ourhost, &ahp);
328 memcpy(ourip, hp->h_addr, sizeof(*ourip));
332 /* A.ROOT-SERVERS.NET. */
333 if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip))