395e7a004a73ab009f63af84de2b0493343552a8
3 char const *cvs_id_radius
= "$Id: radius.c,v 1.30 2005/05/07 08:17:26 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
) sess_local
[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
= sess_local
[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 sess_local
[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 random_data(radius
[r
].auth
, sizeof(radius
[r
].auth
));
96 LOG(3, s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
100 // Send a RADIUS request
101 void radiussend(uint16_t r
, uint8_t state
)
103 struct sockaddr_in addr
;
104 uint8_t b
[4096]; // RADIUS packet
112 s
= radius
[r
].session
;
113 if (!config
->numradiusservers
)
115 LOG(0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
118 if (!*config
->radiussecret
)
120 LOG(0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
124 if (state
!= RADIUSAUTH
&& !config
->radius_accounting
)
126 // Radius accounting is turned off
131 if (radius
[r
].state
!= state
)
134 radius
[r
].state
= state
;
135 radius
[r
].retry
= backoff(radius
[r
].try++);
136 LOG(4, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
137 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
138 radius_state(radius
[r
].state
), radius
[r
].try);
140 if (radius
[r
].try > config
->numradiusservers
* 2)
144 if (state
== RADIUSAUTH
)
145 sessionshutdown(s
, "RADIUS timeout.", 3, 0);
148 LOG(1, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
149 radius_state(state
));
152 STAT(radius_timeout
);
156 STAT(radius_retries
);
157 radius
[r
].state
= RADIUSWAIT
;
158 radius
[r
].retry
= 100;
162 // contruct RADIUS access request
166 b
[0] = 1; // access request
171 b
[0] = 4; // accounting request
174 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
176 b
[1] = r
>> RADIUS_SHIFT
; // identifier
177 memcpy(b
+ 4, radius
[r
].auth
, 16);
182 p
[1] = strlen(session
[s
].user
) + 2;
183 strcpy(p
+ 2, session
[s
].user
);
186 if (state
== RADIUSAUTH
)
190 *p
= 3; // CHAP password
192 p
[2] = radius
[r
].id
; // ID
193 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
195 *p
= 60; // CHAP Challenge
197 memcpy(p
+ 2, radius
[r
].auth
, 16);
202 strcpy(pass
, radius
[r
].pass
);
205 pass
[pl
++] = 0; // pad
214 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
216 MD5Update(&ctx
, pass
+ p
- 16, 16);
218 MD5Update(&ctx
, radius
[r
].auth
, 16);
219 MD5Final(hash
, &ctx
);
222 pass
[p
] ^= hash
[p
& 15];
231 memcpy(p
+ 2, pass
, pl
);
235 else if (state
== RADIUSSTART
|| state
== RADIUSSTOP
|| state
== RADIUSINTERIM
)
237 *p
= 40; // accounting type
239 *(uint32_t *) (p
+ 2) = htonl(state
- RADIUSSTART
+ 1); // start=1, stop=2, interim=3
243 *p
= 44; // session ID
245 sprintf(p
+ 2, "%08X%08X", session
[s
].unique_id
, session
[s
].opened
);
247 if (state
== RADIUSSTART
)
251 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
253 sess_local
[s
].last_interim
= time_now
; // Setup "first" Interim
257 *p
= 42; // input octets
259 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
261 *p
= 43; // output octets
263 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
265 *p
= 46; // session time
267 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
269 *p
= 47; // input packets
271 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
273 *p
= 48; // output spackets
275 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
279 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
281 *p
= 26; // vendor-specific
282 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
283 p
[6] = 1; // Cisco-Avpair
284 p
[7] = 2 + sprintf(p
+ 8, "intercept=%s:%d",
285 fmtaddr(session
[s
].snoop_ip
, 0), session
[s
].snoop_port
);
296 *(uint32_t *) (p
+ 2) = htonl(s
);
299 if (s
&& session
[s
].ip
)
301 *p
= 8; // Framed-IP-Address
303 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
306 if (*session
[s
].called
)
309 p
[1] = strlen(session
[s
].called
) + 2;
310 strcpy(p
+ 2, session
[s
].called
);
313 if (*radius
[r
].calling
)
316 p
[1] = strlen(radius
[r
].calling
) + 2;
317 strcpy(p
+ 2, radius
[r
].calling
);
320 else if (*session
[s
].calling
)
323 p
[1] = strlen(session
[s
].calling
) + 2;
324 strcpy(p
+ 2, session
[s
].calling
);
330 *(uint32_t *)(p
+ 2) = config
->bind_address
;
334 *(uint16_t *) (b
+ 2) = htons(p
- b
);
335 if (state
!= RADIUSAUTH
)
337 // Build auth for accounting packet
342 MD5Update(&ctx
, b
, 4);
343 MD5Update(&ctx
, z
, 16);
344 MD5Update(&ctx
, b
+ 20, (p
- b
) - 20);
345 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
346 MD5Final(hash
, &ctx
);
347 memcpy(b
+ 4, hash
, 16);
348 memcpy(radius
[r
].auth
, hash
, 16);
350 memset(&addr
, 0, sizeof(addr
));
351 addr
.sin_family
= AF_INET
;
352 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
355 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
356 // assume RADIUS accounting port is the authentication port +1
357 addr
.sin_port
= htons((state
== RADIUSAUTH
) ? port
: port
+1);
360 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
361 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
364 // process RADIUS response
365 void processrad(uint8_t *buf
, int len
, char socket_index
)
367 uint8_t b
[MAXCONTROL
];
379 LOG_HEX(5, "RADIUS Response", buf
, len
);
380 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
382 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
386 r_code
= buf
[0]; // response type
387 r_id
= buf
[1]; // radius reply indentifier.
389 len
= ntohs(*(uint16_t *) (buf
+ 2));
390 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
391 s
= radius
[r
].session
;
392 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
393 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
395 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
397 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
400 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSSTART
401 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
403 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
406 t
= session
[s
].tunnel
;
408 MD5Update(&ctx
, buf
, 4);
409 MD5Update(&ctx
, radius
[r
].auth
, 16);
410 MD5Update(&ctx
, buf
+ 20, len
- 20);
411 MD5Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
412 MD5Final(hash
, &ctx
);
414 if (memcmp(hash
, buf
+ 4, 16))
416 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
417 return; // Do nothing. On timeout, it will try the next radius server.
420 if ((radius
[r
].state
== RADIUSAUTH
&& r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
421 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
423 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
424 return; // We got something we didn't expect. Let the timeouts take
425 // care off finishing the radius session if that's really correct.
428 if (radius
[r
].state
== RADIUSAUTH
)
430 // run post-auth plugin
431 struct param_post_auth packet
= {
435 (r_code
== AccessAccept
),
436 radius
[r
].chap
? PPPCHAP
: PPPPAP
439 run_plugins(PLUGIN_POST_AUTH
, &packet
);
440 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
442 // process auth response
446 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPCHAP
);
447 if (!p
) return; // Abort!
449 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
451 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
452 tunnelsend(b
, (p
- b
) + 4, t
); // send it
454 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
455 (r_code
== AccessAccept
) ? "allowed" : "denied");
460 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, t
, s
, PPPPAP
);
461 if (!p
) return; // Abort!
466 *(uint16_t *) (p
+ 2) = ntohs(5);
467 p
[4] = 0; // no message
468 tunnelsend(b
, (p
- b
) + 5, t
); // send it
470 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
471 (r_code
== AccessAccept
) ? "allowed" : "denied");
474 if (r_code
== AccessAccept
)
477 // Extract IP, routes, etc
478 uint8_t *p
= buf
+ 20;
479 uint8_t *e
= buf
+ len
;
480 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
485 if (p
[1] < 6) continue;
486 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
487 session
[s
].ip_pool_index
= -1;
488 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
489 fmtaddr(htonl(session
[s
].ip
), 0));
491 if (session
[s
].ip
== 0xFFFFFFFE)
492 session
[s
].ip
= 0; // assign from pool
497 if (p
[1] < 6) continue;
498 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
499 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
500 fmtaddr(htonl(session
[s
].dns1
), 0));
505 if (p
[1] < 6) continue;
506 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
507 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
508 fmtaddr(htonl(session
[s
].dns2
), 0));
513 in_addr_t ip
= 0, mask
= 0;
517 uint8_t *e
= p
+ p
[1];
518 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
526 u
= u
* 10 + *n
- '0';
533 while (n
< e
&& isdigit(*n
))
534 bits
= bits
* 10 + *n
++ - '0';
535 mask
= (( -1) << (32 - bits
));
537 else if ((ip
>> 24) < 128)
539 else if ((ip
>> 24) < 192)
544 if (routes
== MAXROUTE
)
546 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
550 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n",
551 fmtaddr(htonl(ip
), 0), fmtaddr(htonl(mask
), 1));
553 session
[s
].route
[routes
].ip
= ip
;
554 session
[s
].route
[routes
].mask
= mask
;
561 char *filter
= p
+ 2;
567 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
568 if ((suffix
= memchr(filter
, '.', l
)))
570 int b
= suffix
- filter
;
571 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
572 f
= &session
[s
].filter_in
;
573 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
574 f
= &session
[s
].filter_out
;
581 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
585 for (*f
= 0, i
= 0; !*f
&& i
< MAXFILTER
; i
++)
586 if (strlen(ip_filters
[i
].name
) == l
&&
587 !strncmp(ip_filters
[i
].name
, filter
, l
))
591 ip_filters
[*f
- 1].used
++;
593 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
596 else if (*p
== 26 && p
[1] >= 7)
598 // Vendor-Specific Attribute
599 int vendor
= ntohl(*(int *)(p
+ 2));
600 char attrib
= *(p
+ 6);
601 int attrib_length
= *(p
+ 7) - 2;
602 char *avpair
, *value
, *key
, *newp
;
604 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor
, attrib
, attrib_length
);
605 if (vendor
!= 9 || attrib
!= 1)
607 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
611 if (attrib_length
< 0) continue;
613 avpair
= key
= calloc(attrib_length
+ 1, 1);
614 memcpy(avpair
, p
+ 8, attrib_length
);
615 LOG(3, s
, session
[s
].tunnel
, " Cisco-Avpair value: %s\n", avpair
);
617 value
= strchr(key
, '=');
621 // Trim quotes off reply string
622 if (*value
== '\'' || *value
== '\"')
626 x
= value
+ strlen(value
) - 1;
627 if (*x
== '\'' || *x
== '\"')
632 newp
= strchr(value
, ',');
633 if (newp
) *newp
++ = 0;
635 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], key
, value
};
636 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
648 uint8_t *e
= p
+ p
[1];
649 uint8_t *m
= strchr(n
, '/');
652 inet_pton(AF_INET6
, n
, &r6
);
655 while (m
< e
&& isdigit(*m
)) {
656 prefixlen
= prefixlen
* 10 + *m
++ - '0';
661 LOG(3, s
, session
[s
].tunnel
,
662 " Radius reply contains route for %s/%d\n",
664 session
[s
].ipv6route
= r6
;
665 session
[s
].ipv6prefixlen
= prefixlen
;
670 else if (r_code
== AccessReject
)
672 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
673 sessionkill(s
, "Authentication rejected");
677 if (!session
[s
].dns1
&& config
->default_dns1
)
679 session
[s
].dns1
= htonl(config
->default_dns1
);
680 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
682 if (!session
[s
].dns2
&& config
->default_dns2
)
684 session
[s
].dns2
= htonl(config
->default_dns2
);
685 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
688 // Valid Session, set it up
689 session
[s
].unique_id
= 0;
694 // An ack for a stop or start record.
695 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
700 // finished with RADIUS
704 // Send a retry for RADIUS/CHAP message
705 void radiusretry(uint16_t r
)
707 sessionidt s
= radius
[r
].session
;
712 if (s
) t
= session
[s
].tunnel
;
714 radius
[r
].retry
= backoff(radius
[r
].try + 1);
715 switch (radius
[r
].state
)
717 case RADIUSCHAP
: // sending CHAP down PPP
721 sendipcp(t
, s
); // send IPCP
723 case RADIUSAUTH
: // sending auth to RADIUS server
724 radiussend(r
, RADIUSAUTH
);
726 case RADIUSSTART
: // sending start accounting to RADIUS server
727 radiussend(r
, RADIUSSTART
);
729 case RADIUSSTOP
: // sending stop accounting to RADIUS server
730 radiussend(r
, RADIUSSTOP
);
732 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
733 radiussend(r
, RADIUSINTERIM
);
736 case RADIUSNULL
: // Not in use
737 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
738 // free up RADIUS task
740 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);