3 char const *cvs_id_radius
= "$Id: radius.c,v 1.52 2006-07-01 12:40:17 bodea Exp $";
8 #include <sys/socket.h>
12 #include <arpa/inet.h>
14 #include <netinet/in.h>
17 #include "constants.h"
23 extern radiust
*radius
;
24 extern sessiont
*session
;
25 extern tunnelt
*tunnel
;
26 extern configt
*config
;
28 extern ip_filtert
*ip_filters
;
30 static const hasht zero
;
32 static void calc_auth(const void *buf
, size_t len
, const uint8_t *in
, uint8_t *out
)
37 MD5_Update(&ctx
, (void *)buf
, 4); // code, id, length
38 MD5_Update(&ctx
, (void *)in
, 16); // auth
39 MD5_Update(&ctx
, (void *)(buf
+ 20), len
- 20);
40 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
44 // Set up socket for radius requests
49 uint16_t min
= config
->radius_bind_min
;
50 uint16_t max
= config
->radius_bind_max
;
52 struct sockaddr_in addr
;
60 else if (max
) /* no minimum specified, bind from max down */
67 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", RADIUS_FDS
);
68 radfds
= calloc(sizeof(int), RADIUS_FDS
);
69 for (i
= 0; i
< RADIUS_FDS
; i
++)
72 radfds
[i
] = socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
73 flags
= fcntl(radfds
[i
], F_GETFL
, 0);
74 fcntl(radfds
[i
], F_SETFL
, flags
| O_NONBLOCK
);
80 memset(&addr
, 0, sizeof(addr
));
81 addr
.sin_family
= AF_INET
;
82 addr
.sin_addr
.s_addr
= INADDR_ANY
;
85 addr
.sin_port
= htons(port
);
86 if ((b
= bind(radfds
[i
], (struct sockaddr
*) &addr
, sizeof(addr
))) < 0)
88 if ((port
+= inc
) < min
|| port
> max
)
90 LOG(0, 0, 0, "Can't bind RADIUS socket in range %u-%u\n", min
, max
);
99 void radiusclear(uint16_t r
, sessionidt s
)
101 if (s
) sess_local
[s
].radius
= 0;
102 memset(&radius
[r
], 0, sizeof(radius
[r
])); // radius[r].state = RADIUSNULL;
105 static uint16_t get_free_radius()
108 static uint32_t next_radius_id
= 0;
110 for (count
= MAXRADIUS
; count
> 0; --count
)
112 ++next_radius_id
; // Find the next ID to check.
113 if (next_radius_id
>= MAXRADIUS
)
116 if (radius
[next_radius_id
].state
== RADIUSNULL
)
118 return next_radius_id
;
122 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
126 uint16_t radiusnew(sessionidt s
)
128 uint16_t r
= sess_local
[s
].radius
;
133 LOG(3, s
, session
[s
].tunnel
, "Re-used radius %d\n", r
);
137 if (!(r
= get_free_radius()))
139 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions\n");
140 STAT(radius_overflow
);
144 memset(&radius
[r
], 0, sizeof(radius
[r
]));
145 sess_local
[s
].radius
= r
;
146 radius
[r
].session
= s
;
147 radius
[r
].state
= RADIUSWAIT
;
148 radius
[r
].retry
= TIME
+ 1200; // Wait at least 120 seconds to re-claim this.
150 random_data(radius
[r
].auth
, sizeof(radius
[r
].auth
));
152 LOG(3, s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
156 // Send a RADIUS request
157 void radiussend(uint16_t r
, uint8_t state
)
159 struct sockaddr_in addr
;
160 uint8_t b
[4096]; // RADIUS packet
168 s
= radius
[r
].session
;
169 if (!config
->numradiusservers
)
171 LOG(0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
174 if (!*config
->radiussecret
)
176 LOG(0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
180 if (state
!= RADIUSAUTH
&& !config
->radius_accounting
)
182 // Radius accounting is turned off
187 if (radius
[r
].state
!= state
)
190 radius
[r
].state
= state
;
191 radius
[r
].retry
= backoff(radius
[r
].try++) + 20; // 3s, 4s, 6s, 10s...
192 LOG(4, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
193 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
194 radius_state(radius
[r
].state
), radius
[r
].try);
196 if (radius
[r
].try > config
->numradiusservers
* 2)
200 if (state
== RADIUSAUTH
)
201 sessionshutdown(s
, "RADIUS timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
204 LOG(1, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
205 radius_state(state
));
208 STAT(radius_timeout
);
212 STAT(radius_retries
);
213 radius
[r
].state
= RADIUSWAIT
;
214 radius
[r
].retry
= 100;
218 // contruct RADIUS access request
222 b
[0] = AccessRequest
; // access request
227 b
[0] = AccountingRequest
; // accounting request
230 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
232 b
[1] = r
>> RADIUS_SHIFT
; // identifier
233 memcpy(b
+ 4, radius
[r
].auth
, 16);
238 p
[1] = strlen(session
[s
].user
) + 2;
239 strcpy((char *) p
+ 2, session
[s
].user
);
242 if (state
== RADIUSAUTH
)
246 *p
= 3; // CHAP password
248 p
[2] = radius
[r
].id
; // ID
249 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
251 *p
= 60; // CHAP Challenge
253 memcpy(p
+ 2, radius
[r
].auth
, 16);
258 strcpy(pass
, radius
[r
].pass
);
261 pass
[pl
++] = 0; // pad
270 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
272 MD5_Update(&ctx
, pass
+ p
- 16, 16);
274 MD5_Update(&ctx
, radius
[r
].auth
, 16);
275 MD5_Final(hash
, &ctx
);
278 pass
[p
] ^= hash
[p
& 15];
287 memcpy(p
+ 2, pass
, pl
);
293 *p
= 40; // accounting type
295 *(uint32_t *) (p
+ 2) = htonl(state
- RADIUSSTART
+ 1); // start=1, stop=2, interim=3
299 *p
= 44; // session ID
301 sprintf((char *) p
+ 2, "%08X%08X", session
[s
].unique_id
, session
[s
].opened
);
303 if (state
== RADIUSSTART
)
307 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
309 sess_local
[s
].last_interim
= time_now
; // Setup "first" Interim
313 *p
= 42; // input octets
315 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
318 *p
= 43; // output octets
320 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
323 *p
= 46; // session time
325 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
328 *p
= 47; // input packets
330 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
333 *p
= 48; // output packets
335 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
338 *p
= 52; // input gigawords
340 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin_wrap
);
343 *p
= 53; // output gigawords
345 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout_wrap
);
348 if (state
== RADIUSSTOP
&& radius
[r
].term_cause
)
350 *p
= 49; // acct-terminate-cause
352 *(uint32_t *) (p
+ 2) = htonl(radius
[r
].term_cause
);
355 if (radius
[r
].term_msg
)
357 *p
= 26; // vendor-specific
358 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
359 p
[6] = 1; // Cisco-AVPair
360 p
[7] = 2 + sprintf((char *) p
+ 8, "disc-cause-ext=%s", radius
[r
].term_msg
);
368 struct param_radius_account acct
= { &tunnel
[session
[s
].tunnel
], &session
[s
], &p
};
369 run_plugins(PLUGIN_RADIUS_ACCOUNT
, &acct
);
378 *(uint32_t *) (p
+ 2) = htonl(s
);
381 *p
= 6; // Service-Type
383 *(uint32_t *) (p
+ 2) = htonl(2); // Framed-User
386 *p
= 7; // Framed-Protocol
388 *(uint32_t *) (p
+ 2) = htonl(1); // PPP
393 *p
= 8; // Framed-IP-Address
395 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
399 if (session
[s
].route
[0].ip
)
402 for (r
= 0; s
&& r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
405 if (session
[s
].route
[r
].mask
)
407 int mask
= session
[s
].route
[r
].mask
;
415 *p
= 22; // Framed-Route
416 p
[1] = sprintf((char *) p
+ 2, "%s/%d %s 1",
417 fmtaddr(htonl(session
[s
].route
[r
].ip
), 0),
418 width
, fmtaddr(htonl(session
[s
].ip
), 1)) + 2;
424 if (session
[s
].session_timeout
)
426 *p
= 27; // Session-Timeout
428 *(uint32_t *) (p
+ 2) = htonl(session
[s
].session_timeout
);
432 if (session
[s
].idle_timeout
)
434 *p
= 28; // Idle-Timeout
436 *(uint32_t *) (p
+ 2) = htonl(session
[s
].idle_timeout
);
440 if (*session
[s
].called
)
443 p
[1] = strlen(session
[s
].called
) + 2;
444 strcpy((char *) p
+ 2, session
[s
].called
);
448 if (*session
[s
].calling
)
451 p
[1] = strlen(session
[s
].calling
) + 2;
452 strcpy((char *) p
+ 2, session
[s
].calling
);
460 *(uint32_t *)(p
+ 2) = config
->bind_address
;
464 *(uint16_t *) (b
+ 2) = htons(p
- b
);
465 if (state
!= RADIUSAUTH
)
467 // Build auth for accounting packet
468 calc_auth(b
, p
- b
, zero
, b
+ 4);
469 memcpy(radius
[r
].auth
, b
+ 4, 16);
471 memset(&addr
, 0, sizeof(addr
));
472 addr
.sin_family
= AF_INET
;
473 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
476 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
477 // assume RADIUS accounting port is the authentication port +1
478 addr
.sin_port
= htons((state
== RADIUSAUTH
) ? port
: port
+1);
481 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
482 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
485 static void handle_avpair(sessionidt s
, uint8_t *avp
, int len
)
488 uint8_t *value
= memchr(avp
, '=', len
);
489 uint8_t tmp
[2048] = "";
503 if (len
> 2 && (*value
== '"' || *value
== '\'') && value
[len
- 1] == *value
)
509 // copy and null terminate
510 else if (len
< sizeof(tmp
) - 1)
512 memcpy(tmp
, value
, len
);
521 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], (char *) key
, (char *) value
};
522 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
526 // process RADIUS response
527 void processrad(uint8_t *buf
, int len
, char socket_index
)
540 LOG_HEX(5, "RADIUS Response", buf
, len
);
541 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
543 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
547 r_code
= buf
[0]; // response type
548 r_id
= buf
[1]; // radius reply indentifier.
550 len
= ntohs(*(uint16_t *) (buf
+ 2));
551 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
552 s
= radius
[r
].session
;
553 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
554 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
556 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
558 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
561 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSSTART
562 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
564 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
567 t
= session
[s
].tunnel
;
568 calc_auth(buf
, len
, radius
[r
].auth
, hash
);
570 if (memcmp(hash
, buf
+ 4, 16))
572 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
573 return; // Do nothing. On timeout, it will try the next radius server.
576 if ((radius
[r
].state
== RADIUSAUTH
&& r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
577 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
579 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
580 return; // We got something we didn't expect. Let the timeouts take
581 // care off finishing the radius session if that's really correct.
584 if (radius
[r
].state
== RADIUSAUTH
)
586 // run post-auth plugin
587 struct param_post_auth packet
= {
591 (r_code
== AccessAccept
),
592 radius
[r
].chap
? PPPCHAP
: PPPPAP
595 run_plugins(PLUGIN_POST_AUTH
, &packet
);
596 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
598 // process auth response
602 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
603 if (!p
) return; // Abort!
605 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
607 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
608 tunnelsend(b
, (p
- b
) + 4, t
); // send it
610 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
611 (r_code
== AccessAccept
) ? "allowed" : "denied");
616 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
617 if (!p
) return; // Abort!
622 *(uint16_t *) (p
+ 2) = ntohs(5);
623 p
[4] = 0; // no message
624 tunnelsend(b
, (p
- b
) + 5, t
); // send it
626 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
627 (r_code
== AccessAccept
) ? "allowed" : "denied");
630 if (r_code
== AccessAccept
)
633 // Extract IP, routes, etc
634 uint8_t *p
= buf
+ 20;
635 uint8_t *e
= buf
+ len
;
636 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
641 if (p
[1] < 6) continue;
642 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
643 session
[s
].ip_pool_index
= -1;
644 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
645 fmtaddr(htonl(session
[s
].ip
), 0));
647 if (session
[s
].ip
== 0xFFFFFFFE)
648 session
[s
].ip
= 0; // assign from pool
653 if (p
[1] < 6) continue;
654 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
655 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
656 fmtaddr(htonl(session
[s
].dns1
), 0));
662 LOG(2, s
, session
[s
].tunnel
, "Error: Received Session timeout with length %d < 6\n", p
[1]);
666 session
[s
].timeout
= ntohl(*(uint32_t *) (p
+ 2));
667 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session timeout %d\n", session
[s
].timeout
);
668 if (!session
[s
].timeout
)
669 sessionshutdown(s
, "Session timeout is zero", CDN_ADMIN_DISC
, TERM_SESSION_TIMEOUT
);
674 if (p
[1] < 6) continue;
675 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
676 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
677 fmtaddr(htonl(session
[s
].dns2
), 0));
682 in_addr_t ip
= 0, mask
= 0;
686 uint8_t *e
= p
+ p
[1];
687 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
695 u
= u
* 10 + *n
- '0';
702 while (n
< e
&& isdigit(*n
))
703 bits
= bits
* 10 + *n
++ - '0';
704 mask
= (( -1) << (32 - bits
));
706 else if ((ip
>> 24) < 128)
708 else if ((ip
>> 24) < 192)
713 if (routes
== MAXROUTE
)
715 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
719 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n",
720 fmtaddr(htonl(ip
), 0), fmtaddr(htonl(mask
), 1));
722 session
[s
].route
[routes
].ip
= ip
;
723 session
[s
].route
[routes
].mask
= mask
;
730 char *filter
= (char *) p
+ 2;
736 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
737 if ((suffix
= memchr(filter
, '.', l
)))
739 int b
= suffix
- filter
;
740 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
741 fp
= &session
[s
].filter_in
;
742 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
743 fp
= &session
[s
].filter_out
;
750 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
754 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
756 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
761 ip_filters
[f
].used
++;
767 uint32_t to
= ntohl(*(uint32_t *)(p
+ 2));
769 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", to
);
772 session
[s
].session_timeout
= to
;
778 uint32_t to
= ntohl(*(uint32_t *)(p
+ 2));
780 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", to
);
783 session
[s
].idle_timeout
= to
;
786 else if (*p
== 26 && p
[1] >= 7)
788 // Vendor-Specific Attribute
789 int vendor
= ntohl(*(int *)(p
+ 2));
790 char attrib
= *(p
+ 6);
791 int attrib_length
= *(p
+ 7) - 2;
793 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%d Attrib=%d Length=%d\n", vendor
, attrib
, attrib_length
);
794 if (vendor
!= 9 || attrib
!= 1)
796 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
800 if (attrib_length
> 0)
802 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
803 attrib_length
, p
+ 8);
805 handle_avpair(s
, p
+ 8, attrib_length
);
814 uint8_t *e
= p
+ p
[1];
815 uint8_t *m
= memchr(n
, '/', e
- p
);
818 inet_pton(AF_INET6
, (char *) n
, &r6
);
821 while (m
< e
&& isdigit(*m
)) {
822 prefixlen
= prefixlen
* 10 + *m
++ - '0';
827 LOG(3, s
, session
[s
].tunnel
,
828 " Radius reply contains route for %s/%d\n",
830 session
[s
].ipv6route
= r6
;
831 session
[s
].ipv6prefixlen
= prefixlen
;
836 else if (r_code
== AccessReject
)
838 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
839 sessionkill(s
, "Authentication rejected");
843 if (!session
[s
].dns1
&& config
->default_dns1
)
845 session
[s
].dns1
= ntohl(config
->default_dns1
);
846 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
848 if (!session
[s
].dns2
&& config
->default_dns2
)
850 session
[s
].dns2
= ntohl(config
->default_dns2
);
851 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
854 // Valid Session, set it up
855 session
[s
].unique_id
= 0;
860 // An ack for a stop or start record.
861 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
866 // finished with RADIUS
870 // Send a retry for RADIUS/CHAP message
871 void radiusretry(uint16_t r
)
873 sessionidt s
= radius
[r
].session
;
878 if (s
) t
= session
[s
].tunnel
;
880 switch (radius
[r
].state
)
882 case RADIUSCHAP
: // sending CHAP down PPP
885 case RADIUSAUTH
: // sending auth to RADIUS server
886 case RADIUSSTART
: // sending start accounting to RADIUS server
887 case RADIUSSTOP
: // sending stop accounting to RADIUS server
888 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
889 radiussend(r
, radius
[r
].state
);
892 case RADIUSNULL
: // Not in use
893 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
894 // free up RADIUS task
896 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
903 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
905 int i
, r_code
, r_id
, length
, attribute_length
;
906 uint8_t *packet
, attribute
;
908 char username
[MAXUSER
] = "";
918 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
923 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
924 LOG_HEX(5, "DAE Request", buf
, len
);
926 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
928 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
932 r_code
= buf
[0]; // request type
933 r_id
= buf
[1]; // radius indentifier.
935 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
937 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
941 if (!config
->cluster_iam_master
)
943 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
947 len
= ntohs(*(uint16_t *) (buf
+ 2));
949 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
951 // check authenticator
952 calc_auth(buf
, len
, zero
, hash
);
953 if (memcmp(hash
, buf
+ 4, 16) != 0)
955 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
965 attribute
= *packet
++;
966 attribute_length
= *packet
++;
967 if (attribute_length
< 2)
970 length
-= attribute_length
;
971 attribute_length
-= 2;
974 case 1: /* username */
975 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
976 memcpy(username
, packet
, len
);
978 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
981 case 4: /* nas ip address */
982 nas
= *(uint32_t *) packet
; // net order
983 if (nas
!= config
->bind_address
)
984 error
= 403; // NAS identification mismatch
986 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
989 case 5: /* nas port */
990 port
= ntohl(*(uint32_t *) packet
);
991 if (port
< 1 || port
> MAXSESSION
)
994 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
997 case 6: /* service type */
999 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
1000 auth_only
= service_type
== 8; // Authenticate only
1002 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
1006 case 8: /* ip address */
1007 ip
= *(uint32_t *) packet
; // net order
1008 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
1011 case 11: /* filter id */
1012 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
1013 if (!(p
= memchr(packet
, '.', attribute_length
)))
1015 error
= 404; // invalid request
1020 i
= find_filter((char *) packet
, len
);
1021 if (i
< 0 || !*ip_filters
[i
].name
)
1027 if (!memcmp(p
, ".in", attribute_length
- len
))
1029 else if (!memcmp(p
, ".out", attribute_length
- len
))
1036 case 26: /* vendor specific */
1037 if (attribute_length
>= 6
1038 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1039 && *(packet
+ 4) == 1 // Cisco-AVPair
1040 && *(packet
+ 5) >= 2) // length
1042 int len
= *(packet
+ 5) - 2;
1043 uint8_t *a
= packet
+ 6;
1045 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1046 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1049 avpair_len
[avp
++] = len
;
1055 packet
+= attribute_length
;
1058 if (!error
&& auth_only
)
1060 if (fin
!= -1 || fout
!= -1 || avp
)
1061 error
= 401; // unsupported attribute
1063 error
= 405; // unsupported service
1066 if (!error
&& !(port
|| ip
|| *username
))
1067 error
= 402; // missing attribute
1069 // exact match for SID if given
1073 if (!session
[s
].opened
)
1074 error
= 503; // not found
1079 // find/check session by IP
1080 i
= sessionbyip(ip
);
1081 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1087 if (!error
&& *username
)
1091 if (strcmp(session
[s
].user
, username
))
1094 else if (!(s
= sessionbyuser(username
)))
1098 t
= session
[s
].tunnel
;
1102 case DisconnectRequest
: // Packet of Disconnect/Death
1105 r_code
= DisconnectNAK
;
1109 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1110 r_code
= DisconnectACK
;
1112 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1115 case CoARequest
: // Change of Authorization
1122 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1127 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1128 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1135 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1140 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1142 filter_session(s
, fin
, fout
);
1144 // process cisco av-pair(s)
1145 for (i
= 0; i
< avp
; i
++)
1147 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1148 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1151 cluster_send_session(s
);
1169 *(uint32_t *) packet
= htonl(error
);
1173 *((uint16_t *)(buf
+ 2)) = htons(len
);
1176 calc_auth(buf
, len
, hash
, buf
+ 4);
1178 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1180 // send DAE response
1181 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1182 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));