3 char const *cvs_id_radius
= "$Id: radius.c,v 1.55 2006/08/02 14:17:30 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])
638 if (*p
== 26 && p
[1] >= 7)
640 // Vendor-Specific Attribute
641 uint32_t vendor
= ntohl(*(int *)(p
+ 2));
642 uint8_t attrib
= *(p
+ 6);
643 int attrib_length
= *(p
+ 7) - 2;
645 LOG(4, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor
, attrib
, attrib_length
);
646 if (vendor
== 9 && attrib
== 1) // Cisco-AVPair
648 if (attrib_length
< 0) continue;
649 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
650 attrib_length
, p
+ 8);
652 handle_avpair(s
, p
+ 8, attrib_length
);
655 else if (vendor
== 529 && attrib
>= 135 && attrib
<= 136) // Ascend
657 // handle old-format ascend DNS attributes below
662 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
670 if (p
[1] < 6) continue;
671 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
672 session
[s
].ip_pool_index
= -1;
673 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
674 fmtaddr(htonl(session
[s
].ip
), 0));
676 if (session
[s
].ip
== 0xFFFFFFFE)
677 session
[s
].ip
= 0; // assign from pool
682 if (p
[1] < 6) continue;
683 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
684 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
685 fmtaddr(htonl(session
[s
].dns1
), 0));
690 if (p
[1] < 6) continue;
691 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
692 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
693 fmtaddr(htonl(session
[s
].dns2
), 0));
698 in_addr_t ip
= 0, mask
= 0;
702 uint8_t *e
= p
+ p
[1];
703 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
711 u
= u
* 10 + *n
- '0';
718 while (n
< e
&& isdigit(*n
))
719 bits
= bits
* 10 + *n
++ - '0';
720 mask
= (( -1) << (32 - bits
));
722 else if ((ip
>> 24) < 128)
724 else if ((ip
>> 24) < 192)
729 if (routes
== MAXROUTE
)
731 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
735 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n",
736 fmtaddr(htonl(ip
), 0), fmtaddr(htonl(mask
), 1));
738 session
[s
].route
[routes
].ip
= ip
;
739 session
[s
].route
[routes
].mask
= mask
;
746 char *filter
= (char *) p
+ 2;
752 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
753 if ((suffix
= memchr(filter
, '.', l
)))
755 int b
= suffix
- filter
;
756 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
757 fp
= &session
[s
].filter_in
;
758 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
759 fp
= &session
[s
].filter_out
;
766 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
770 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
772 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
777 ip_filters
[f
].used
++;
783 if (p
[1] < 6) continue;
784 session
[s
].session_timeout
= ntohl(*(uint32_t *)(p
+ 2));
785 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", session
[s
].session_timeout
);
790 if (p
[1] < 6) continue;
791 session
[s
].idle_timeout
= ntohl(*(uint32_t *)(p
+ 2));
792 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", session
[s
].idle_timeout
);
800 uint8_t *e
= p
+ p
[1];
801 uint8_t *m
= memchr(n
, '/', e
- p
);
804 inet_pton(AF_INET6
, (char *) n
, &r6
);
807 while (m
< e
&& isdigit(*m
)) {
808 prefixlen
= prefixlen
* 10 + *m
++ - '0';
813 LOG(3, s
, session
[s
].tunnel
,
814 " Radius reply contains route for %s/%d\n",
816 session
[s
].ipv6route
= r6
;
817 session
[s
].ipv6prefixlen
= prefixlen
;
822 else if (r_code
== AccessReject
)
824 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
825 sessionkill(s
, "Authentication rejected");
829 if (!session
[s
].dns1
&& config
->default_dns1
)
831 session
[s
].dns1
= ntohl(config
->default_dns1
);
832 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
834 if (!session
[s
].dns2
&& config
->default_dns2
)
836 session
[s
].dns2
= ntohl(config
->default_dns2
);
837 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
840 // Valid Session, set it up
841 session
[s
].unique_id
= 0;
846 // An ack for a stop or start record.
847 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
852 // finished with RADIUS
856 // Send a retry for RADIUS/CHAP message
857 void radiusretry(uint16_t r
)
859 sessionidt s
= radius
[r
].session
;
864 if (s
) t
= session
[s
].tunnel
;
866 switch (radius
[r
].state
)
868 case RADIUSCHAP
: // sending CHAP down PPP
871 case RADIUSAUTH
: // sending auth to RADIUS server
872 case RADIUSSTART
: // sending start accounting to RADIUS server
873 case RADIUSSTOP
: // sending stop accounting to RADIUS server
874 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
875 radiussend(r
, radius
[r
].state
);
878 case RADIUSNULL
: // Not in use
879 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
880 // free up RADIUS task
882 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
889 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
891 int i
, r_code
, r_id
, length
, attribute_length
;
892 uint8_t *packet
, attribute
;
894 char username
[MAXUSER
] = "";
904 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
909 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
910 LOG_HEX(5, "DAE Request", buf
, len
);
912 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
914 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
918 r_code
= buf
[0]; // request type
919 r_id
= buf
[1]; // radius indentifier.
921 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
923 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
927 if (!config
->cluster_iam_master
)
929 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
933 len
= ntohs(*(uint16_t *) (buf
+ 2));
935 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
937 // check authenticator
938 calc_auth(buf
, len
, zero
, hash
);
939 if (memcmp(hash
, buf
+ 4, 16) != 0)
941 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
951 attribute
= *packet
++;
952 attribute_length
= *packet
++;
953 if (attribute_length
< 2)
956 length
-= attribute_length
;
957 attribute_length
-= 2;
960 case 1: /* username */
961 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
962 memcpy(username
, packet
, len
);
964 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
967 case 4: /* nas ip address */
968 nas
= *(uint32_t *) packet
; // net order
969 if (nas
!= config
->bind_address
)
970 error
= 403; // NAS identification mismatch
972 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
975 case 5: /* nas port */
976 port
= ntohl(*(uint32_t *) packet
);
977 if (port
< 1 || port
> MAXSESSION
)
980 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
983 case 6: /* service type */
985 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
986 auth_only
= service_type
== 8; // Authenticate only
988 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
992 case 8: /* ip address */
993 ip
= *(uint32_t *) packet
; // net order
994 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
997 case 11: /* filter id */
998 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
999 if (!(p
= memchr(packet
, '.', attribute_length
)))
1001 error
= 404; // invalid request
1006 i
= find_filter((char *) packet
, len
);
1007 if (i
< 0 || !*ip_filters
[i
].name
)
1013 if (!memcmp(p
, ".in", attribute_length
- len
))
1015 else if (!memcmp(p
, ".out", attribute_length
- len
))
1022 case 26: /* vendor specific */
1023 if (attribute_length
>= 6
1024 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1025 && *(packet
+ 4) == 1 // Cisco-AVPair
1026 && *(packet
+ 5) >= 2) // length
1028 int len
= *(packet
+ 5) - 2;
1029 uint8_t *a
= packet
+ 6;
1031 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1032 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1035 avpair_len
[avp
++] = len
;
1041 packet
+= attribute_length
;
1044 if (!error
&& auth_only
)
1046 if (fin
!= -1 || fout
!= -1 || avp
)
1047 error
= 401; // unsupported attribute
1049 error
= 405; // unsupported service
1052 if (!error
&& !(port
|| ip
|| *username
))
1053 error
= 402; // missing attribute
1055 // exact match for SID if given
1059 if (!session
[s
].opened
)
1060 error
= 503; // not found
1065 // find/check session by IP
1066 i
= sessionbyip(ip
);
1067 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1073 if (!error
&& *username
)
1077 if (strcmp(session
[s
].user
, username
))
1080 else if (!(s
= sessionbyuser(username
)))
1084 t
= session
[s
].tunnel
;
1088 case DisconnectRequest
: // Packet of Disconnect/Death
1091 r_code
= DisconnectNAK
;
1095 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1096 r_code
= DisconnectACK
;
1098 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1101 case CoARequest
: // Change of Authorization
1108 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1113 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1114 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1121 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1126 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1128 filter_session(s
, fin
, fout
);
1130 // process cisco av-pair(s)
1131 for (i
= 0; i
< avp
; i
++)
1133 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1134 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1137 cluster_send_session(s
);
1155 *(uint32_t *) packet
= htonl(error
);
1159 *((uint16_t *)(buf
+ 2)) = htons(len
);
1162 calc_auth(buf
, len
, hash
, buf
+ 4);
1164 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1166 // send DAE response
1167 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1168 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));