3 char const *cvs_id_radius
= "$Id: radius.c,v 1.56 2009-12-08 14:49:28 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
&& state
!= RADIUSJUSTAUTH
&& !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
|| state
== RADIUSJUSTAUTH
)
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
223 b
[0] = AccessRequest
; // access request
228 b
[0] = AccountingRequest
; // accounting request
231 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
233 b
[1] = r
>> RADIUS_SHIFT
; // identifier
234 memcpy(b
+ 4, radius
[r
].auth
, 16);
239 p
[1] = strlen(session
[s
].user
) + 2;
240 strcpy((char *) p
+ 2, session
[s
].user
);
243 if (state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
)
247 *p
= 3; // CHAP password
249 p
[2] = radius
[r
].id
; // ID
250 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
252 *p
= 60; // CHAP Challenge
254 memcpy(p
+ 2, radius
[r
].auth
, 16);
259 strcpy(pass
, radius
[r
].pass
);
262 pass
[pl
++] = 0; // pad
271 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
273 MD5_Update(&ctx
, pass
+ p
- 16, 16);
275 MD5_Update(&ctx
, radius
[r
].auth
, 16);
276 MD5_Final(hash
, &ctx
);
279 pass
[p
] ^= hash
[p
& 15];
288 memcpy(p
+ 2, pass
, pl
);
294 *p
= 40; // accounting type
296 *(uint32_t *) (p
+ 2) = htonl(state
- RADIUSSTART
+ 1); // start=1, stop=2, interim=3
300 *p
= 44; // session ID
302 sprintf((char *) p
+ 2, "%08X%08X", session
[s
].unique_id
, session
[s
].opened
);
304 if (state
== RADIUSSTART
)
308 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
310 sess_local
[s
].last_interim
= time_now
; // Setup "first" Interim
314 *p
= 42; // input octets
316 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
319 *p
= 43; // output octets
321 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
324 *p
= 46; // session time
326 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
329 *p
= 47; // input packets
331 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
334 *p
= 48; // output packets
336 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
339 *p
= 52; // input gigawords
341 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin_wrap
);
344 *p
= 53; // output gigawords
346 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout_wrap
);
349 if (state
== RADIUSSTOP
&& radius
[r
].term_cause
)
351 *p
= 49; // acct-terminate-cause
353 *(uint32_t *) (p
+ 2) = htonl(radius
[r
].term_cause
);
356 if (radius
[r
].term_msg
)
358 *p
= 26; // vendor-specific
359 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
360 p
[6] = 1; // Cisco-AVPair
361 p
[7] = 2 + sprintf((char *) p
+ 8, "disc-cause-ext=%s", radius
[r
].term_msg
);
369 struct param_radius_account acct
= { &tunnel
[session
[s
].tunnel
], &session
[s
], &p
};
370 run_plugins(PLUGIN_RADIUS_ACCOUNT
, &acct
);
379 *(uint32_t *) (p
+ 2) = htonl(s
);
382 *p
= 6; // Service-Type
384 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 8 : 2)); // Authenticate only or Framed-User respectevily
387 *p
= 7; // Framed-Protocol
388 p
[1] = htonl((state
== RADIUSJUSTAUTH
? 0 : 6));
389 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 0 : 1)); // PPP
394 *p
= 8; // Framed-IP-Address
396 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
400 if (session
[s
].route
[0].ip
)
403 for (r
= 0; s
&& r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
406 if (session
[s
].route
[r
].mask
)
408 int mask
= session
[s
].route
[r
].mask
;
416 *p
= 22; // Framed-Route
417 p
[1] = sprintf((char *) p
+ 2, "%s/%d %s 1",
418 fmtaddr(htonl(session
[s
].route
[r
].ip
), 0),
419 width
, fmtaddr(htonl(session
[s
].ip
), 1)) + 2;
425 if (session
[s
].session_timeout
)
427 *p
= 27; // Session-Timeout
429 *(uint32_t *) (p
+ 2) = htonl(session
[s
].session_timeout
);
433 if (session
[s
].idle_timeout
)
435 *p
= 28; // Idle-Timeout
437 *(uint32_t *) (p
+ 2) = htonl(session
[s
].idle_timeout
);
441 if (*session
[s
].called
)
444 p
[1] = strlen(session
[s
].called
) + 2;
445 strcpy((char *) p
+ 2, session
[s
].called
);
449 if (*session
[s
].calling
)
452 p
[1] = strlen(session
[s
].calling
) + 2;
453 strcpy((char *) p
+ 2, session
[s
].calling
);
461 *(uint32_t *)(p
+ 2) = config
->bind_address
;
465 *(uint16_t *) (b
+ 2) = htons(p
- b
);
466 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
)
468 // Build auth for accounting packet
469 calc_auth(b
, p
- b
, zero
, b
+ 4);
470 memcpy(radius
[r
].auth
, b
+ 4, 16);
472 memset(&addr
, 0, sizeof(addr
));
473 addr
.sin_family
= AF_INET
;
474 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
477 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
478 // assume RADIUS accounting port is the authentication port +1
479 addr
.sin_port
= htons((state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
) ? port
: port
+1);
482 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
483 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
486 static void handle_avpair(sessionidt s
, uint8_t *avp
, int len
)
489 uint8_t *value
= memchr(avp
, '=', len
);
490 uint8_t tmp
[2048] = "";
504 if (len
> 2 && (*value
== '"' || *value
== '\'') && value
[len
- 1] == *value
)
510 // copy and null terminate
511 else if (len
< sizeof(tmp
) - 1)
513 memcpy(tmp
, value
, len
);
522 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], (char *) key
, (char *) value
};
523 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
527 // process RADIUS response
528 void processrad(uint8_t *buf
, int len
, char socket_index
)
541 LOG_HEX(5, "RADIUS Response", buf
, len
);
542 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
544 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
548 r_code
= buf
[0]; // response type
549 r_id
= buf
[1]; // radius reply indentifier.
551 len
= ntohs(*(uint16_t *) (buf
+ 2));
552 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
553 s
= radius
[r
].session
;
554 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
555 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
557 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
559 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
562 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSJUSTAUTH
&& radius
[r
].state
!= RADIUSSTART
563 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
565 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
568 t
= session
[s
].tunnel
;
569 calc_auth(buf
, len
, radius
[r
].auth
, hash
);
571 if (memcmp(hash
, buf
+ 4, 16))
573 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
574 return; // Do nothing. On timeout, it will try the next radius server.
577 if (((radius
[r
].state
== RADIUSAUTH
||radius
[r
].state
== RADIUSJUSTAUTH
) && r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
578 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
580 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
581 return; // We got something we didn't expect. Let the timeouts take
582 // care off finishing the radius session if that's really correct.
585 if (radius
[r
].state
== RADIUSAUTH
|| radius
[r
].state
== RADIUSJUSTAUTH
)
587 // run post-auth plugin
588 struct param_post_auth packet
= {
592 (r_code
== AccessAccept
),
593 radius
[r
].chap
? PPPCHAP
: PPPPAP
596 run_plugins(PLUGIN_POST_AUTH
, &packet
);
597 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
599 // process auth response
603 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
604 if (!p
) return; // Abort!
606 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
608 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
609 tunnelsend(b
, (p
- b
) + 4, t
); // send it
611 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
612 (r_code
== AccessAccept
) ? "allowed" : "denied");
617 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
618 if (!p
) return; // Abort!
623 *(uint16_t *) (p
+ 2) = ntohs(5);
624 p
[4] = 0; // no message
625 tunnelsend(b
, (p
- b
) + 5, t
); // send it
627 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
628 (r_code
== AccessAccept
) ? "allowed" : "denied");
631 if (r_code
== AccessAccept
)
634 // Extract IP, routes, etc
635 uint8_t *p
= buf
+ 20;
636 uint8_t *e
= buf
+ len
;
637 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
639 if (*p
== 26 && p
[1] >= 7)
641 // Vendor-Specific Attribute
642 uint32_t vendor
= ntohl(*(int *)(p
+ 2));
643 uint8_t attrib
= *(p
+ 6);
644 int attrib_length
= *(p
+ 7) - 2;
646 LOG(4, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor
, attrib
, attrib_length
);
647 if (vendor
== 9 && attrib
== 1) // Cisco-AVPair
649 if (attrib_length
< 0) continue;
650 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
651 attrib_length
, p
+ 8);
653 handle_avpair(s
, p
+ 8, attrib_length
);
656 else if (vendor
== 529 && attrib
>= 135 && attrib
<= 136) // Ascend
658 // handle old-format ascend DNS attributes below
663 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
671 if (p
[1] < 6) continue;
672 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
673 session
[s
].ip_pool_index
= -1;
674 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
675 fmtaddr(htonl(session
[s
].ip
), 0));
677 if (session
[s
].ip
== 0xFFFFFFFE)
678 session
[s
].ip
= 0; // assign from pool
683 if (p
[1] < 6) continue;
684 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
685 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
686 fmtaddr(htonl(session
[s
].dns1
), 0));
691 if (p
[1] < 6) continue;
692 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
693 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
694 fmtaddr(htonl(session
[s
].dns2
), 0));
699 in_addr_t ip
= 0, mask
= 0;
703 uint8_t *e
= p
+ p
[1];
704 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
712 u
= u
* 10 + *n
- '0';
719 while (n
< e
&& isdigit(*n
))
720 bits
= bits
* 10 + *n
++ - '0';
721 mask
= (( -1) << (32 - bits
));
723 else if ((ip
>> 24) < 128)
725 else if ((ip
>> 24) < 192)
730 if (routes
== MAXROUTE
)
732 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
736 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n",
737 fmtaddr(htonl(ip
), 0), fmtaddr(htonl(mask
), 1));
739 session
[s
].route
[routes
].ip
= ip
;
740 session
[s
].route
[routes
].mask
= mask
;
747 char *filter
= (char *) p
+ 2;
753 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
754 if ((suffix
= memchr(filter
, '.', l
)))
756 int b
= suffix
- filter
;
757 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
758 fp
= &session
[s
].filter_in
;
759 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
760 fp
= &session
[s
].filter_out
;
767 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
771 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
773 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
778 ip_filters
[f
].used
++;
784 if (p
[1] < 6) continue;
785 session
[s
].session_timeout
= ntohl(*(uint32_t *)(p
+ 2));
786 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", session
[s
].session_timeout
);
787 if(!session
[s
].session_timeout
&& config
->kill_timedout_sessions
)
788 sessionshutdown(s
, "Session timeout is zero", CDN_ADMIN_DISC
, 0);
793 if (p
[1] < 6) continue;
794 session
[s
].idle_timeout
= ntohl(*(uint32_t *)(p
+ 2));
795 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", session
[s
].idle_timeout
);
803 uint8_t *e
= p
+ p
[1];
804 uint8_t *m
= memchr(n
, '/', e
- p
);
807 inet_pton(AF_INET6
, (char *) n
, &r6
);
810 while (m
< e
&& isdigit(*m
)) {
811 prefixlen
= prefixlen
* 10 + *m
++ - '0';
816 LOG(3, s
, session
[s
].tunnel
,
817 " Radius reply contains route for %s/%d\n",
819 session
[s
].ipv6route
= r6
;
820 session
[s
].ipv6prefixlen
= prefixlen
;
825 else if (r_code
== AccessReject
)
827 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
828 sessionkill(s
, "Authentication rejected");
832 if (!session
[s
].dns1
&& config
->default_dns1
)
834 session
[s
].dns1
= ntohl(config
->default_dns1
);
835 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
837 if (!session
[s
].dns2
&& config
->default_dns2
)
839 session
[s
].dns2
= ntohl(config
->default_dns2
);
840 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
843 // Valid Session, set it up
844 session
[s
].unique_id
= 0;
849 // An ack for a stop or start record.
850 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
855 // finished with RADIUS
859 // Send a retry for RADIUS/CHAP message
860 void radiusretry(uint16_t r
)
862 sessionidt s
= radius
[r
].session
;
867 if (s
) t
= session
[s
].tunnel
;
869 switch (radius
[r
].state
)
871 case RADIUSCHAP
: // sending CHAP down PPP
874 case RADIUSAUTH
: // sending auth to RADIUS server
875 case RADIUSJUSTAUTH
: // sending auth to RADIUS server
876 case RADIUSSTART
: // sending start accounting to RADIUS server
877 case RADIUSSTOP
: // sending stop accounting to RADIUS server
878 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
879 radiussend(r
, radius
[r
].state
);
882 case RADIUSNULL
: // Not in use
883 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
884 // free up RADIUS task
886 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
893 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
895 int i
, r_code
, r_id
, length
, attribute_length
;
896 uint8_t *packet
, attribute
;
898 char username
[MAXUSER
] = "";
908 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
913 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
914 LOG_HEX(5, "DAE Request", buf
, len
);
916 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
918 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
922 r_code
= buf
[0]; // request type
923 r_id
= buf
[1]; // radius indentifier.
925 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
927 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
931 if (!config
->cluster_iam_master
)
933 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
937 len
= ntohs(*(uint16_t *) (buf
+ 2));
939 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
941 // check authenticator
942 calc_auth(buf
, len
, zero
, hash
);
943 if (memcmp(hash
, buf
+ 4, 16) != 0)
945 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
955 attribute
= *packet
++;
956 attribute_length
= *packet
++;
957 if (attribute_length
< 2)
960 length
-= attribute_length
;
961 attribute_length
-= 2;
964 case 1: /* username */
965 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
966 memcpy(username
, packet
, len
);
968 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
971 case 4: /* nas ip address */
972 nas
= *(uint32_t *) packet
; // net order
973 if (nas
!= config
->bind_address
)
974 error
= 403; // NAS identification mismatch
976 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
979 case 5: /* nas port */
980 port
= ntohl(*(uint32_t *) packet
);
981 if (port
< 1 || port
> MAXSESSION
)
984 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
987 case 6: /* service type */
989 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
990 auth_only
= service_type
== 8; // Authenticate only
992 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
996 case 8: /* ip address */
997 ip
= *(uint32_t *) packet
; // net order
998 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
1001 case 11: /* filter id */
1002 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
1003 if (!(p
= memchr(packet
, '.', attribute_length
)))
1005 error
= 404; // invalid request
1010 i
= find_filter((char *) packet
, len
);
1011 if (i
< 0 || !*ip_filters
[i
].name
)
1017 if (!memcmp(p
, ".in", attribute_length
- len
))
1019 else if (!memcmp(p
, ".out", attribute_length
- len
))
1026 case 26: /* vendor specific */
1027 if (attribute_length
>= 6
1028 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1029 && *(packet
+ 4) == 1 // Cisco-AVPair
1030 && *(packet
+ 5) >= 2) // length
1032 int len
= *(packet
+ 5) - 2;
1033 uint8_t *a
= packet
+ 6;
1035 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1036 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1039 avpair_len
[avp
++] = len
;
1045 packet
+= attribute_length
;
1048 if (!error
&& auth_only
)
1050 if (fin
!= -1 || fout
!= -1 || avp
)
1051 error
= 401; // unsupported attribute
1053 error
= 405; // unsupported service
1056 if (!error
&& !(port
|| ip
|| *username
))
1057 error
= 402; // missing attribute
1059 // exact match for SID if given
1063 if (!session
[s
].opened
)
1064 error
= 503; // not found
1069 // find/check session by IP
1070 i
= sessionbyip(ip
);
1071 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1077 if (!error
&& *username
)
1081 if (strcmp(session
[s
].user
, username
))
1084 else if (!(s
= sessionbyuser(username
)))
1088 t
= session
[s
].tunnel
;
1092 case DisconnectRequest
: // Packet of Disconnect/Death
1095 r_code
= DisconnectNAK
;
1099 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1100 r_code
= DisconnectACK
;
1102 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1105 case CoARequest
: // Change of Authorization
1112 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1117 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1118 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1125 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1130 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1132 filter_session(s
, fin
, fout
);
1134 // process cisco av-pair(s)
1135 for (i
= 0; i
< avp
; i
++)
1137 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1138 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1141 cluster_send_session(s
);
1159 *(uint32_t *) packet
= htonl(error
);
1163 *((uint16_t *)(buf
+ 2)) = htons(len
);
1166 calc_auth(buf
, len
, hash
, buf
+ 4);
1168 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1170 // send DAE response
1171 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1172 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));