3 char const *cvs_id_radius
= "$Id: radius.c,v 1.21 2005/01/05 13:45:49 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 // Set up socket for radius requests
32 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", config
->num_radfds
);
33 radfds
= calloc(sizeof(int), config
->num_radfds
);
34 for (i
= 0; i
< config
->num_radfds
; i
++)
37 radfds
[i
] = socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
38 flags
= fcntl(radfds
[i
], F_GETFL
, 0);
39 fcntl(radfds
[i
], F_SETFL
, flags
| O_NONBLOCK
);
43 void radiusclear(uint16_t r
, sessionidt s
)
45 if (s
) session
[s
].radius
= 0;
46 memset(&radius
[r
], 0, sizeof(radius
[r
])); // radius[r].state = RADIUSNULL;
49 static uint16_t get_free_radius()
52 static uint32_t next_radius_id
= 0;
54 for (count
= MAXRADIUS
; count
> 0 ; --count
)
56 ++next_radius_id
; // Find the next ID to check.
57 if (next_radius_id
>= MAXRADIUS
)
60 if (radius
[next_radius_id
].state
== RADIUSNULL
)
62 return next_radius_id
;
66 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
70 uint16_t radiusnew(sessionidt s
)
72 uint16_t r
= session
[s
].radius
;
77 LOG(3, s
, session
[s
].tunnel
, "Re-used radius %d\n", r
);
81 if (!(r
= get_free_radius()))
83 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions\n");
84 STAT(radius_overflow
);
88 memset(&radius
[r
], 0, sizeof(radius
[r
]));
89 session
[s
].radius
= r
;
90 radius
[r
].session
= s
;
91 radius
[r
].state
= RADIUSWAIT
;
92 radius
[r
].retry
= TIME
+ 1200; // Wait at least 120 seconds to re-claim this.
94 LOG(3, s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
98 // Send a RADIUS request
99 void radiussend(uint16_t r
, uint8_t state
)
101 struct sockaddr_in addr
;
102 uint8_t b
[4096]; // RADIUS packet
110 s
= radius
[r
].session
;
111 if (!config
->numradiusservers
)
113 LOG(0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
116 if (!*config
->radiussecret
)
118 LOG(0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
122 if (state
!= RADIUSAUTH
&& !config
->radius_accounting
)
124 // Radius accounting is turned off
129 if (radius
[r
].state
!= state
)
132 radius
[r
].state
= state
;
133 radius
[r
].retry
= backoff(radius
[r
].try++);
134 LOG(4, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
135 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
136 radius_state(radius
[r
].state
), radius
[r
].try);
138 if (radius
[r
].try > config
->numradiusservers
* 2)
142 if (state
== RADIUSAUTH
)
143 sessionshutdown(s
, "RADIUS timeout");
146 LOG(1, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
147 radius_state(state
));
150 STAT(radius_timeout
);
154 STAT(radius_retries
);
155 radius
[r
].state
= RADIUSWAIT
;
156 radius
[r
].retry
= 100;
160 // contruct RADIUS access request
164 b
[0] = 1; // access request
168 b
[0] = 4; // accounting request
171 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
173 b
[1] = r
>> RADIUS_SHIFT
; // identifier
174 memcpy(b
+ 4, radius
[r
].auth
, 16);
179 p
[1] = strlen(session
[s
].user
) + 2;
180 strcpy(p
+ 2, session
[s
].user
);
183 if (state
== RADIUSAUTH
)
187 *p
= 3; // CHAP password
189 p
[2] = radius
[r
].id
; // ID
190 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
192 *p
= 60; // CHAP Challenge
194 memcpy(p
+ 2, radius
[r
].auth
, 16);
199 strcpy(pass
, radius
[r
].pass
);
202 pass
[pl
++] = 0; // pad
211 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
213 MD5Update(&ctx
, pass
+ p
- 16, 16);
215 MD5Update(&ctx
, radius
[r
].auth
, 16);
216 MD5Final(hash
, &ctx
);
219 pass
[p
] ^= hash
[p
& 15];
228 memcpy(p
+ 2, pass
, pl
);
232 else if (state
== RADIUSSTART
|| state
== RADIUSSTOP
)
234 *p
= 40; // accounting type
236 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSSTART
) ? 1 : 2);
240 *p
= 44; // session ID
242 sprintf(p
+ 2, "%08X%08X", session
[s
].id
, session
[s
].opened
);
244 if (state
== RADIUSSTOP
)
246 *p
= 42; // input octets
248 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
250 *p
= 43; // output octets
252 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
254 *p
= 46; // session time
256 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
258 *p
= 47; // input packets
260 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
262 *p
= 48; // output spackets
264 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
271 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
280 *(uint32_t *) (p
+ 2) = htonl(s
);
283 if (s
&& session
[s
].ip
)
285 *p
= 8; // Framed-IP-Address
287 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
290 if (*session
[s
].called
)
293 p
[1] = strlen(session
[s
].called
) + 2;
294 strcpy(p
+ 2, session
[s
].called
);
297 if (*radius
[r
].calling
)
300 p
[1] = strlen(radius
[r
].calling
) + 2;
301 strcpy(p
+ 2, radius
[r
].calling
);
304 else if (*session
[s
].calling
)
307 p
[1] = strlen(session
[s
].calling
) + 2;
308 strcpy(p
+ 2, session
[s
].calling
);
314 *(uint32_t *)(p
+ 2) = config
->bind_address
;
318 *(uint16_t *) (b
+ 2) = htons(p
- b
);
319 if (state
!= RADIUSAUTH
)
321 // Build auth for accounting packet
326 MD5Update(&ctx
, b
, 4);
327 MD5Update(&ctx
, z
, 16);
328 MD5Update(&ctx
, b
+ 20, (p
- b
) - 20);
329 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
330 MD5Final(hash
, &ctx
);
331 memcpy(b
+ 4, hash
, 16);
332 memcpy(radius
[r
].auth
, hash
, 16);
334 memset(&addr
, 0, sizeof(addr
));
335 addr
.sin_family
= AF_INET
;
336 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
339 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
340 // no need to define the accounting port for itself:
341 // the accounting port is as far as I know always one more
342 // than the auth port JK 20040713
343 addr
.sin_port
= htons((state
== RADIUSAUTH
) ? port
: port
+1);
346 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
347 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
350 // process RADIUS response
351 void processrad(uint8_t *buf
, int len
, char socket_index
)
353 uint8_t b
[MAXCONTROL
];
365 LOG_HEX(5, "RADIUS Response", buf
, len
);
366 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
368 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
372 r_code
= buf
[0]; // response type
373 r_id
= buf
[1]; // radius reply indentifier.
375 len
= ntohs(*(uint16_t *) (buf
+ 2));
376 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
377 s
= radius
[r
].session
;
378 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
379 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
381 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
383 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
386 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSSTART
&& radius
[r
].state
!= RADIUSSTOP
)
388 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
391 t
= session
[s
].tunnel
;
393 MD5Update(&ctx
, buf
, 4);
394 MD5Update(&ctx
, radius
[r
].auth
, 16);
395 MD5Update(&ctx
, buf
+ 20, len
- 20);
396 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
397 MD5Final(hash
, &ctx
);
399 if (memcmp(hash
, buf
+ 4, 16))
401 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
402 return; // Do nothing. On timeout, it will try the next radius server.
405 if ((radius
[r
].state
== RADIUSAUTH
&& r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
406 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
) && r_code
!= AccountingResponse
))
408 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
409 return; // We got something we didn't expect. Let the timeouts take
410 // care off finishing the radius session if that's really correct.
413 if (radius
[r
].state
== RADIUSAUTH
)
415 // run post-auth plugin
416 struct param_post_auth packet
= {
420 (r_code
== AccessAccept
),
421 radius
[r
].chap
? PPPCHAP
: PPPPAP
424 run_plugins(PLUGIN_POST_AUTH
, &packet
);
425 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
427 // process auth response
431 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
432 if (!p
) return; // Abort!
434 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
436 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
437 tunnelsend(b
, (p
- b
) + 4, t
); // send it
439 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
440 (r_code
== AccessAccept
) ? "allowed" : "denied");
445 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
446 if (!p
) return; // Abort!
451 *(uint16_t *) (p
+ 2) = ntohs(5);
452 p
[4] = 0; // no message
453 tunnelsend(b
, (p
- b
) + 5, t
); // send it
455 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
456 (r_code
== AccessAccept
) ? "allowed" : "denied");
459 if (r_code
== AccessAccept
)
462 // Extract IP, routes, etc
463 uint8_t *p
= buf
+ 20;
464 uint8_t *e
= buf
+ len
;
465 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
470 if (p
[1] < 6) continue;
471 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
472 session
[s
].ip_pool_index
= -1;
473 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
474 fmtaddr(htonl(session
[s
].ip
), 0));
479 if (p
[1] < 6) continue;
480 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
481 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
482 fmtaddr(htonl(session
[s
].dns1
), 0));
487 if (p
[1] < 6) continue;
488 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
489 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
490 fmtaddr(htonl(session
[s
].dns2
), 0));
495 in_addr_t ip
= 0, mask
= 0;
499 uint8_t *e
= p
+ p
[1];
500 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
508 u
= u
* 10 + *n
- '0';
515 while (n
< e
&& isdigit(*n
))
516 bits
= bits
* 10 + *n
++ - '0';
517 mask
= (( -1) << (32 - bits
));
519 else if ((ip
>> 24) < 128)
521 else if ((ip
>> 24) < 192)
526 if (routes
== MAXROUTE
)
528 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
532 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n",
533 fmtaddr(htonl(ip
), 0), fmtaddr(htonl(mask
), 1));
535 session
[s
].route
[routes
].ip
= ip
;
536 session
[s
].route
[routes
].mask
= mask
;
543 char *filter
= p
+ 2;
549 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
550 if ((suffix
= memchr(filter
, '.', l
)))
552 int b
= suffix
- filter
;
553 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
554 f
= &session
[s
].filter_in
;
555 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
556 f
= &session
[s
].filter_out
;
563 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
567 for (*f
= 0, i
= 0; !*f
&& i
< MAXFILTER
; i
++)
568 if (strlen(ip_filters
[i
].name
) == l
&&
569 !strncmp(ip_filters
[i
].name
, filter
, l
))
573 ip_filters
[*f
- 1].used
++;
575 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
578 else if (*p
== 26 && p
[1] >= 7)
580 // Vendor-Specific Attribute
581 int vendor
= ntohl(*(int *)(p
+ 2));
582 char attrib
= *(p
+ 6);
583 char attrib_length
= *(p
+ 7) - 2;
584 char *avpair
, *value
, *key
, *newp
;
586 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor
, attrib
, attrib_length
);
587 if (vendor
!= 9 || attrib
!= 1)
589 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
593 if (attrib_length
< 0) continue;
595 avpair
= key
= calloc(attrib_length
+ 1, 1);
596 memcpy(avpair
, p
+ 8, attrib_length
);
597 LOG(3, s
, session
[s
].tunnel
, " Cisco-Avpair value: %s\n", avpair
);
599 value
= strchr(key
, '=');
603 // Trim quotes off reply string
604 if (*value
== '\'' || *value
== '\"')
608 x
= value
+ strlen(value
) - 1;
609 if (*x
== '\'' || *x
== '\"')
614 newp
= strchr(value
, ',');
615 if (newp
) *newp
++ = 0;
617 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], key
, value
};
618 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
626 else if (r_code
== AccessReject
)
628 LOG(2, s
, session
[s
].tunnel
, " Authentication denied for %s\n", session
[s
].user
);
629 //FIXME: We should tear down the session here!
633 if (!session
[s
].dns1
&& config
->default_dns1
)
635 session
[s
].dns1
= htonl(config
->default_dns1
);
636 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
638 if (!session
[s
].dns2
&& config
->default_dns2
)
640 session
[s
].dns2
= htonl(config
->default_dns2
);
641 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
644 // Valid Session, set it up
645 session
[s
].unique_id
= 0;
650 // An ack for a stop or start record.
651 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
656 // finished with RADIUS
660 // Send a retry for RADIUS/CHAP message
661 void radiusretry(uint16_t r
)
663 sessionidt s
= radius
[r
].session
;
668 if (s
) t
= session
[s
].tunnel
;
670 radius
[r
].retry
= backoff(radius
[r
].try + 1);
671 switch (radius
[r
].state
)
673 case RADIUSCHAP
: // sending CHAP down PPP
677 sendipcp(t
, s
); // send IPCP
679 case RADIUSAUTH
: // sending auth to RADIUS server
680 radiussend(r
, RADIUSAUTH
);
682 case RADIUSSTART
: // sending start accounting to RADIUS server
683 radiussend(r
, RADIUSSTART
);
685 case RADIUSSTOP
: // sending stop accounting to RADIUS server
686 radiussend(r
, RADIUSSTOP
);
689 case RADIUSNULL
: // Not in use
690 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
691 // free up RADIUS task
693 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);