2 * Asterisk -- A telephony toolkit for Linux.
4 * Various sorts of access control
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
22 #include <asterisk/acl.h>
23 #include <asterisk/logger.h>
24 #include <asterisk/channel.h>
25 #include <arpa/inet.h>
26 #include <sys/socket.h>
29 #include <netinet/in_systm.h>
30 #include <netinet/ip.h>
31 #include <sys/ioctl.h>
34 #include <net/route.h>
36 static ast_mutex_t routeseq_lock = AST_MUTEX_INITIALIZER;
39 #define AST_SENSE_DENY 0
40 #define AST_SENSE_ALLOW 1
43 /* Host access rule */
44 struct in_addr netaddr;
45 struct in_addr netmask;
50 /* Default IP - if not otherwise set, don't breathe garbage */
51 static struct in_addr __ourip = { 0x00000000 };
54 char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "eth0", "ppp0", etc. */
55 struct sockaddr_in ifru_addr;
58 void ast_free_ha(struct ast_ha *ha)
68 struct ast_ha *ast_append_ha(char *sense, char *stuff, struct ast_ha *path)
70 struct ast_ha *ha = malloc(sizeof(struct ast_ha));
73 struct ast_ha *prev = NULL;
83 strncpy(tmp, stuff, sizeof(tmp) - 1);
84 nm = strchr(tmp, '/');
86 nm = "255.255.255.255";
91 if (!strchr(nm, '.')) {
92 if ((sscanf(nm, "%i", &x) == 1) && (x >= 0) && (x <= 32)) {
98 ha->netmask.s_addr = htonl(y);
100 } else if (!inet_aton(nm, &ha->netmask)) {
101 ast_log(LOG_WARNING, "%s not a valid netmask\n", nm);
105 if (!inet_aton(tmp, &ha->netaddr)) {
106 ast_log(LOG_WARNING, "%s not a valid IP\n", tmp);
110 ha->netaddr.s_addr &= ha->netmask.s_addr;
111 if (!strncasecmp(sense, "p", 1)) {
112 ha->sense = AST_SENSE_ALLOW;
114 ha->sense = AST_SENSE_DENY;
125 int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin)
127 /* Start optimistic */
128 int res = AST_SENSE_ALLOW;
130 /* For each rule, if this address and the netmask = the net address
131 apply the current rule */
132 if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == (ha->netaddr.s_addr))
139 int ast_get_ip(struct sockaddr_in *sin, char *value)
142 hp = gethostbyname(value);
144 memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr));
146 ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value);
152 int inaddrcmp(struct sockaddr_in *sin1, struct sockaddr_in *sin2)
154 return ((sin1->sin_addr.s_addr != sin2->sin_addr.s_addr )
155 || (sin1->sin_port != sin2->sin_port));
158 /* iface is the interface (e.g. eth0); address is the return value */
159 int ast_lookup_iface(char *iface, struct in_addr *address) {
161 struct my_ifreq ifreq;
163 memset(&ifreq, 0, sizeof(ifreq));
164 strncpy(ifreq.ifrn_name,iface,sizeof(ifreq.ifrn_name) - 1);
166 mysock = socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);
167 res = ioctl(mysock,SIOCGIFADDR,&ifreq);
171 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
172 memcpy((char *)address,(char *)&__ourip,sizeof(__ourip));
175 memcpy((char *)address,(char *)&ifreq.ifru_addr.sin_addr,sizeof(ifreq.ifru_addr.sin_addr));
180 int ast_ouraddrfor(struct in_addr *them, struct in_addr *us)
183 struct sockaddr_in *sin;
186 struct rt_msghdr m_rtm;
189 char *cp, *p = ast_strdupa(inet_ntoa(*them));
190 int i, l, s, seq, flags;
191 pid_t pid = getpid();
192 static int routeseq; /* Protected by "routeseq_lock" mutex */
194 memset(us, 0, sizeof(struct in_addr));
196 memset(&m_rtmsg, 0, sizeof(m_rtmsg));
197 m_rtmsg.m_rtm.rtm_type = RTM_GET;
198 m_rtmsg.m_rtm.rtm_flags = RTF_UP | RTF_HOST;
199 m_rtmsg.m_rtm.rtm_version = RTM_VERSION;
200 ast_mutex_lock(&routeseq_lock);
202 ast_mutex_unlock(&routeseq_lock);
203 m_rtmsg.m_rtm.rtm_seq = seq;
204 m_rtmsg.m_rtm.rtm_addrs = RTA_IFA | RTA_DST;
205 m_rtmsg.m_rtm.rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
206 sin = (struct sockaddr_in *)m_rtmsg.m_space;
207 sin->sin_family = AF_INET;
208 sin->sin_len = sizeof(struct sockaddr_in);
209 sin->sin_addr = *them;
211 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
212 ast_log(LOG_ERROR, "Error opening routing socket\n");
215 flags = fcntl(s, F_GETFL);
216 fcntl(s, F_SETFL, flags | O_NONBLOCK);
217 if (write(s, (char *)&m_rtmsg, m_rtmsg.m_rtm.rtm_msglen) < 0) {
218 ast_log(LOG_ERROR, "Error writing to routing socket: %s\n", strerror(errno));
223 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
224 } while (l > 0 && (m_rtmsg.m_rtm.rtm_seq != 1 || m_rtmsg.m_rtm.rtm_pid != pid));
227 ast_log(LOG_ERROR, "Error reading from routing socket\n");
233 if (m_rtmsg.m_rtm.rtm_version != RTM_VERSION) {
234 ast_log(LOG_ERROR, "Unsupported route socket protocol version\n");
238 if (m_rtmsg.m_rtm.rtm_msglen != l)
239 ast_log(LOG_WARNING, "Message length mismatch, in packet %d, returned %d\n",
240 m_rtmsg.m_rtm.rtm_msglen, l);
242 if (m_rtmsg.m_rtm.rtm_errno) {
243 ast_log(LOG_ERROR, "RTM_GET got %s (%d)\n",
244 strerror(m_rtmsg.m_rtm.rtm_errno), m_rtmsg.m_rtm.rtm_errno);
248 cp = (char *)m_rtmsg.m_space;
249 if (m_rtmsg.m_rtm.rtm_addrs)
250 for (i = 1; i; i <<= 1)
251 if (m_rtmsg.m_rtm.rtm_addrs & i) {
252 sa = (struct sockaddr *)cp;
253 if (i == RTA_IFA && sa->sa_family == AF_INET) {
254 sin = (struct sockaddr_in *)sa;
256 ast_log(LOG_DEBUG, "Found route to %s, output from our address %s.\n", p, inet_ntoa(*us));
259 cp += sa->sa_len > 0 ?
260 (1 + ((sa->sa_len - 1) | (sizeof(long) - 1))) :
264 ast_log(LOG_DEBUG, "No route found for address %s!\n", p);
268 unsigned int remote_ip;
271 remote_ip = them->s_addr;
273 PROC = fopen("/proc/net/route","r");
275 bzero(us,sizeof(struct in_addr));
278 /* First line contains headers */
279 fgets(line,sizeof(line),PROC);
281 while (!feof(PROC)) {
283 unsigned int dest, gateway, mask;
287 fgets(line,sizeof(line),PROC);
290 for (i=0;i<sizeof(line);i++) {
293 fields[fieldnum++] = line + i;
294 offset = strchr(line + i,'\t');
295 if (offset == NULL) {
298 } else if (fieldnum >= 9) {
299 /* Short-circuit: can't break at 8, since the end of field 7 is figured when fieldnum=8 */
307 sscanf(fields[0],"%s",iface);
308 sscanf(fields[1],"%x",&dest);
309 sscanf(fields[2],"%x",&gateway);
310 sscanf(fields[7],"%x",&mask);
312 printf("Addr: %s %08x Dest: %08x Mask: %08x\n", inet_ntoa(*them), remote_ip, dest, mask);
314 /* Looks simple, but here is the magic */
315 if (((remote_ip & mask) ^ dest) == 0) {
316 res = ast_lookup_iface(iface,us);
322 ast_log(LOG_WARNING, "Yikes! No default route?!!\n");
323 bzero(us,sizeof(struct in_addr));
326 /* We've already warned in subroutine */