3 char const *cvs_id_radius
= "$Id: radius.c,v 1.20.2.1 2005/02/14 05:56:38 bodea Exp $";
8 #include <sys/socket.h>
12 #include <arpa/inet.h>
14 #include <netinet/in.h>
16 #include "constants.h"
21 extern radiust
*radius
;
22 extern sessiont
*session
;
23 extern tunnelt
*tunnel
;
24 extern configt
*config
;
26 extern ip_filtert
*ip_filters
;
28 static const char *radius_state(int state
)
30 static char *tmp
= NULL
;
32 for (i
= 0; radius_states
[i
]; i
++)
33 if (i
== state
) return radius_states
[i
];
35 if (tmp
== NULL
) tmp
= (char *)calloc(64, 1);
36 sprintf(tmp
, "%d", state
);
40 // Set up socket for radius requests
44 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", config
->num_radfds
);
45 radfds
= calloc(sizeof(int), config
->num_radfds
);
46 for (i
= 0; i
< config
->num_radfds
; i
++)
49 radfds
[i
] = socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
50 flags
= fcntl(radfds
[i
], F_GETFL
, 0);
51 fcntl(radfds
[i
], F_SETFL
, flags
| O_NONBLOCK
);
55 void radiusclear(uint16_t r
, sessionidt s
)
57 if (s
) session
[s
].radius
= 0;
58 memset(&radius
[r
], 0, sizeof(radius
[r
])); // radius[r].state = RADIUSNULL;
62 static uint16_t get_free_radius()
65 static uint32_t next_radius_id
= 0;
67 for (count
= MAXRADIUS
; count
> 0 ; --count
)
69 ++next_radius_id
; // Find the next ID to check.
70 if (next_radius_id
>= MAXRADIUS
)
73 if (radius
[next_radius_id
].state
== RADIUSNULL
)
75 return next_radius_id
;
79 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
83 uint16_t radiusnew(sessionidt s
)
85 uint16_t r
= session
[s
].radius
;
90 LOG(3, s
, session
[s
].tunnel
, "Re-used radius %d\n", r
);
94 if (!(r
= get_free_radius()))
96 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions\n");
97 STAT(radius_overflow
);
101 memset(&radius
[r
], 0, sizeof(radius
[r
]));
102 session
[s
].radius
= r
;
103 radius
[r
].session
= s
;
104 radius
[r
].state
= RADIUSWAIT
;
105 radius
[r
].retry
= TIME
+ 1200; // Wait at least 120 seconds to re-claim this.
107 LOG(3, s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
111 // Send a RADIUS request
112 void radiussend(uint16_t r
, uint8_t state
)
114 struct sockaddr_in addr
;
115 uint8_t b
[4096]; // RADIUS packet
121 CSTAT(call_radiussend
);
123 s
= radius
[r
].session
;
124 if (!config
->numradiusservers
)
126 LOG(0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
129 if (!*config
->radiussecret
)
131 LOG(0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
135 if (state
!= RADIUSAUTH
&& !config
->radius_accounting
)
137 // Radius accounting is turned off
142 if (radius
[r
].state
!= state
)
144 radius
[r
].state
= state
;
145 radius
[r
].retry
= backoff(radius
[r
].try++);
146 LOG(4, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
147 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
148 radius_state(radius
[r
].state
), radius
[r
].try);
150 if (radius
[r
].try > config
->numradiusservers
* 2)
154 if (state
== RADIUSAUTH
)
155 sessionshutdown(s
, "RADIUS timeout");
158 LOG(1, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
159 radius_states
[state
]);
162 STAT(radius_timeout
);
166 STAT(radius_retries
);
167 radius
[r
].state
= RADIUSWAIT
;
168 radius
[r
].retry
= 100;
172 // contruct RADIUS access request
176 b
[0] = 1; // access request
180 b
[0] = 4; // accounting request
183 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
185 b
[1] = r
>> RADIUS_SHIFT
; // identifier
186 memcpy(b
+ 4, radius
[r
].auth
, 16);
191 p
[1] = strlen(session
[s
].user
) + 2;
192 strcpy(p
+ 2, session
[s
].user
);
195 if (state
== RADIUSAUTH
)
199 *p
= 3; // CHAP password
201 p
[2] = radius
[r
].id
; // ID
202 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
204 *p
= 60; // CHAP Challenge
206 memcpy(p
+ 2, radius
[r
].auth
, 16);
211 strcpy(pass
, radius
[r
].pass
);
214 pass
[pl
++] = 0; // pad
223 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
225 MD5Update(&ctx
, pass
+ p
- 16, 16);
227 MD5Update(&ctx
, radius
[r
].auth
, 16);
228 MD5Final(hash
, &ctx
);
231 pass
[p
] ^= hash
[p
& 15];
240 memcpy(p
+ 2, pass
, pl
);
244 else if (state
== RADIUSSTART
|| state
== RADIUSSTOP
)
246 *p
= 40; // accounting type
248 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSSTART
) ? 1 : 2);
252 *p
= 44; // session ID
254 sprintf(p
+ 2, "%08X%08X", session
[s
].id
, session
[s
].opened
);
256 if (state
== RADIUSSTOP
)
258 *p
= 42; // input octets
260 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
262 *p
= 43; // output octets
264 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
266 *p
= 46; // session time
268 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
270 *p
= 47; // input packets
272 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
274 *p
= 48; // output spackets
276 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
283 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
292 *(uint32_t *) (p
+ 2) = htonl(s
);
295 if (s
&& session
[s
].ip
)
297 *p
= 8; // Framed-IP-Address
299 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
302 if (*session
[s
].called
)
305 p
[1] = strlen(session
[s
].called
) + 2;
306 strcpy(p
+ 2, session
[s
].called
);
309 if (*radius
[r
].calling
)
312 p
[1] = strlen(radius
[r
].calling
) + 2;
313 strcpy(p
+ 2, radius
[r
].calling
);
316 else if (*session
[s
].calling
)
319 p
[1] = strlen(session
[s
].calling
) + 2;
320 strcpy(p
+ 2, session
[s
].calling
);
326 *(uint32_t *)(p
+ 2) = config
->bind_address
;
330 *(uint16_t *) (b
+ 2) = htons(p
- b
);
331 if (state
!= RADIUSAUTH
)
333 // Build auth for accounting packet
338 MD5Update(&ctx
, b
, 4);
339 MD5Update(&ctx
, z
, 16);
340 MD5Update(&ctx
, b
+ 20, (p
- b
) - 20);
341 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
342 MD5Final(hash
, &ctx
);
343 memcpy(b
+ 4, hash
, 16);
344 memcpy(radius
[r
].auth
, hash
, 16);
346 memset(&addr
, 0, sizeof(addr
));
347 addr
.sin_family
= AF_INET
;
348 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
351 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
352 // no need to define the accounting port for itself:
353 // the accounting port is as far as I know always one more
354 // than the auth port JK 20040713
355 addr
.sin_port
= htons((state
== RADIUSAUTH
) ? port
: port
+1);
358 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
359 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
362 // process RADIUS response
363 void processrad(uint8_t *buf
, int len
, char socket_index
)
365 uint8_t b
[MAXCONTROL
];
373 int r_code
, r_id
; // Radius code.
375 r_code
= buf
[0]; // First byte in radius packet.
376 r_id
= buf
[1]; // radius reply indentifier.
379 CSTAT(call_processrad
);
381 LOG_HEX(5, "RADIUS Response", buf
, len
);
382 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
384 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
387 len
= ntohs(*(uint16_t *) (buf
+ 2));
388 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
389 s
= radius
[r
].session
;
390 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (code %d, id %d)\n",
391 radius_states
[radius
[r
].state
], r
, s
, r_code
, r_id
);
392 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
394 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
397 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSSTART
&& radius
[r
].state
!= RADIUSSTOP
)
399 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
402 t
= session
[s
].tunnel
;
404 MD5Update(&ctx
, buf
, 4);
405 MD5Update(&ctx
, radius
[r
].auth
, 16);
406 MD5Update(&ctx
, buf
+ 20, len
- 20);
407 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
408 MD5Final(hash
, &ctx
);
410 if (memcmp(hash
, buf
+ 4, 16))
412 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
413 return; // Do nothing. On timeout, it will try the next radius server.
415 if ((radius
[r
].state
== RADIUSAUTH
&& *buf
!= 2 && *buf
!= 3) ||
416 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
) && *buf
!= 5))
418 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %d\n", *buf
);
419 return; // We got something we didn't expect. Let the timeouts take
420 // care off finishing the radius session if that's really correct.
422 if (radius
[r
].state
== RADIUSAUTH
)
424 LOG(4, s
, session
[s
].tunnel
, " Original response is \"%s\"\n", (*buf
== 2) ? "accept" : "reject");
425 // process auth response
429 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
430 if (!p
) return; // Abort!
433 struct param_post_auth packet
= { &tunnel
[t
], &session
[s
], session
[s
].user
, (*buf
== 2), PPPCHAP
};
434 run_plugins(PLUGIN_POST_AUTH
, &packet
);
435 *buf
= packet
.auth_allowed
? 2 : 3;
438 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
439 (*buf
== 2) ? "allowed" : "denied");
440 *p
= (*buf
== 2) ? 3 : 4; // ack/nak
442 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
443 tunnelsend(b
, (p
- b
) + 4, t
); // send it
448 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
449 if (!p
) return; // Abort!
452 struct param_post_auth packet
= { &tunnel
[t
], &session
[s
], session
[s
].user
, (*buf
== 2), PPPPAP
};
453 run_plugins(PLUGIN_POST_AUTH
, &packet
);
454 *buf
= packet
.auth_allowed
? 2 : 3;
457 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
458 (*buf
== 2) ? "allowed" : "denied");
462 *(uint16_t *) (p
+ 2) = ntohs(5);
463 p
[4] = 0; // no message
464 tunnelsend(b
, (p
- b
) + 5, t
); // send it
470 // Extract IP, routes, etc
471 uint8_t *p
= buf
+ 20;
472 uint8_t *e
= buf
+ len
;
473 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
478 if (p
[1] < 6) continue;
479 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
480 session
[s
].ip_pool_index
= -1;
481 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
482 fmtaddr(htonl(session
[s
].ip
), 0));
484 if (session
[s
].ip
== 0xFFFFFFFE)
485 session
[s
].ip
= 0; // assign from pool
490 if (p
[1] < 6) continue;
491 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
492 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
493 fmtaddr(htonl(session
[s
].dns1
), 0));
498 if (p
[1] < 6) continue;
499 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
500 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
501 fmtaddr(htonl(session
[s
].dns2
), 0));
506 in_addr_t ip
= 0, mask
= 0;
510 uint8_t *e
= p
+ p
[1];
511 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
519 u
= u
* 10 + *n
- '0';
526 while (n
< e
&& isdigit(*n
))
527 bits
= bits
* 10 + *n
++ - '0';
528 mask
= (( -1) << (32 - bits
));
530 else if ((ip
>> 24) < 128)
532 else if ((ip
>> 24) < 192)
537 if (routes
== MAXROUTE
)
539 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
543 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n",
544 fmtaddr(htonl(ip
), 0), fmtaddr(htonl(mask
), 1));
546 session
[s
].route
[routes
].ip
= ip
;
547 session
[s
].route
[routes
].mask
= mask
;
554 char *filter
= p
+ 2;
560 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
561 if ((suffix
= memchr(filter
, '.', l
)))
563 int b
= suffix
- filter
;
564 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
565 f
= &session
[s
].filter_in
;
566 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
567 f
= &session
[s
].filter_out
;
574 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
578 for (*f
= 0, i
= 0; !*f
&& i
< MAXFILTER
; i
++)
579 if (strlen(ip_filters
[i
].name
) == l
&&
580 !strncmp(ip_filters
[i
].name
, filter
, l
))
584 ip_filters
[*f
- 1].used
++;
586 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
589 else if (*p
== 26 && p
[1] >= 7)
591 // Vendor-Specific Attribute
592 int vendor
= ntohl(*(int *)(p
+ 2));
593 char attrib
= *(p
+ 6);
594 char attrib_length
= *(p
+ 7) - 2;
595 char *avpair
, *value
, *key
, *newp
;
597 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor
, attrib
, attrib_length
);
598 if (vendor
!= 9 || attrib
!= 1)
600 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
604 if (attrib_length
< 0) continue;
606 avpair
= key
= calloc(attrib_length
+ 1, 1);
607 memcpy(avpair
, p
+ 8, attrib_length
);
608 LOG(3, s
, session
[s
].tunnel
, " Cisco-Avpair value: %s\n", avpair
);
610 value
= strchr(key
, '=');
614 // Trim quotes off reply string
615 if (*value
== '\'' || *value
== '\"')
619 x
= value
+ strlen(value
) - 1;
620 if (*x
== '\'' || *x
== '\"')
625 newp
= strchr(value
, ',');
626 if (newp
) *newp
++ = 0;
628 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], key
, value
};
629 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
639 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
640 sessionkill(s
, "Authentication rejected");
644 if (!session
[s
].dns1
&& config
->default_dns1
)
646 session
[s
].dns1
= htonl(config
->default_dns1
);
647 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
649 if (!session
[s
].dns2
&& config
->default_dns2
)
651 session
[s
].dns2
= htonl(config
->default_dns2
);
652 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
655 // Valid Session, set it up
656 session
[s
].unique_id
= 0;
661 // An ack for a stop or start record.
662 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_states
[radius
[r
].state
]);
667 // finished with RADIUS
671 // Send a retry for RADIUS/CHAP message
672 void radiusretry(uint16_t r
)
674 sessionidt s
= radius
[r
].session
;
677 CSTAT(call_radiusretry
);
680 t
= session
[s
].tunnel
;
682 radius
[r
].retry
= backoff(radius
[r
].try + 1);
683 switch (radius
[r
].state
)
685 case RADIUSCHAP
: // sending CHAP down PPP
689 sendipcp(t
, s
); // send IPCP
691 case RADIUSAUTH
: // sending auth to RADIUS server
692 radiussend(r
, RADIUSAUTH
);
694 case RADIUSSTART
: // sending start accounting to RADIUS server
695 radiussend(r
, RADIUSSTART
);
697 case RADIUSSTOP
: // sending stop accounting to RADIUS server
698 radiussend(r
, RADIUSSTOP
);
701 case RADIUSNULL
: // Not in use
702 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
703 // free up RADIUS task
705 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);