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
++)
405 *p
= 22; // Framed-Route
406 p
[1] = sprintf((char *) p
+ 2, "%s/%d %s 1",
407 fmtaddr(htonl(session
[s
].route
[r
].ip
), 0),
408 session
[s
].route
[r
].prefixlen
,
409 fmtaddr(htonl(session
[s
].ip
), 1)) + 2;
415 if (session
[s
].session_timeout
)
417 *p
= 27; // Session-Timeout
419 *(uint32_t *) (p
+ 2) = htonl(session
[s
].session_timeout
);
423 if (session
[s
].idle_timeout
)
425 *p
= 28; // Idle-Timeout
427 *(uint32_t *) (p
+ 2) = htonl(session
[s
].idle_timeout
);
431 if (*session
[s
].called
)
434 p
[1] = strlen(session
[s
].called
) + 2;
435 strcpy((char *) p
+ 2, session
[s
].called
);
439 if (*session
[s
].calling
)
442 p
[1] = strlen(session
[s
].calling
) + 2;
443 strcpy((char *) p
+ 2, session
[s
].calling
);
451 *(uint32_t *)(p
+ 2) = config
->bind_address
;
455 *(uint16_t *) (b
+ 2) = htons(p
- b
);
456 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
)
458 // Build auth for accounting packet
459 calc_auth(b
, p
- b
, zero
, b
+ 4);
460 memcpy(radius
[r
].auth
, b
+ 4, 16);
462 memset(&addr
, 0, sizeof(addr
));
463 addr
.sin_family
= AF_INET
;
464 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
467 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
468 // assume RADIUS accounting port is the authentication port +1
469 addr
.sin_port
= htons((state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
) ? port
: port
+1);
472 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
473 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
476 static void handle_avpair(sessionidt s
, uint8_t *avp
, int len
)
479 uint8_t *value
= memchr(avp
, '=', len
);
480 uint8_t tmp
[2048] = "";
494 if (len
> 2 && (*value
== '"' || *value
== '\'') && value
[len
- 1] == *value
)
500 // copy and null terminate
501 else if (len
< sizeof(tmp
) - 1)
503 memcpy(tmp
, value
, len
);
512 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], (char *) key
, (char *) value
};
513 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
517 // process RADIUS response
518 void processrad(uint8_t *buf
, int len
, char socket_index
)
531 LOG_HEX(5, "RADIUS Response", buf
, len
);
532 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
534 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
538 r_code
= buf
[0]; // response type
539 r_id
= buf
[1]; // radius reply indentifier.
541 len
= ntohs(*(uint16_t *) (buf
+ 2));
542 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
543 s
= radius
[r
].session
;
544 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
545 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
547 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
549 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
552 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSJUSTAUTH
&& radius
[r
].state
!= RADIUSSTART
553 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
555 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
558 t
= session
[s
].tunnel
;
559 calc_auth(buf
, len
, radius
[r
].auth
, hash
);
561 if (memcmp(hash
, buf
+ 4, 16))
563 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
564 return; // Do nothing. On timeout, it will try the next radius server.
567 if (((radius
[r
].state
== RADIUSAUTH
||radius
[r
].state
== RADIUSJUSTAUTH
) && r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
568 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
570 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
571 return; // We got something we didn't expect. Let the timeouts take
572 // care off finishing the radius session if that's really correct.
575 if (radius
[r
].state
== RADIUSAUTH
|| radius
[r
].state
== RADIUSJUSTAUTH
)
577 // run post-auth plugin
578 struct param_post_auth packet
= {
582 (r_code
== AccessAccept
),
583 radius
[r
].chap
? PPPCHAP
: PPPPAP
586 run_plugins(PLUGIN_POST_AUTH
, &packet
);
587 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
589 // process auth response
593 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
594 if (!p
) return; // Abort!
596 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
598 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
599 tunnelsend(b
, (p
- b
) + 4, t
); // send it
601 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
602 (r_code
== AccessAccept
) ? "allowed" : "denied");
607 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
608 if (!p
) return; // Abort!
613 *(uint16_t *) (p
+ 2) = ntohs(5);
614 p
[4] = 0; // no message
615 tunnelsend(b
, (p
- b
) + 5, t
); // send it
617 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
618 (r_code
== AccessAccept
) ? "allowed" : "denied");
621 if (r_code
== AccessAccept
)
624 // Extract IP, routes, etc
625 uint8_t *p
= buf
+ 20;
626 uint8_t *e
= buf
+ len
;
627 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
629 if (*p
== 26 && p
[1] >= 7)
631 // Vendor-Specific Attribute
632 uint32_t vendor
= ntohl(*(int *)(p
+ 2));
633 uint8_t attrib
= *(p
+ 6);
634 int attrib_length
= *(p
+ 7) - 2;
636 LOG(4, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor
, attrib
, attrib_length
);
637 if (vendor
== 9 && attrib
== 1) // Cisco-AVPair
639 if (attrib_length
< 0) continue;
640 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
641 attrib_length
, p
+ 8);
643 handle_avpair(s
, p
+ 8, attrib_length
);
646 else if (vendor
== 529 && attrib
>= 135 && attrib
<= 136) // Ascend
648 // handle old-format ascend DNS attributes below
653 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
661 if (p
[1] < 6) continue;
662 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
663 session
[s
].ip_pool_index
= -1;
664 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
665 fmtaddr(htonl(session
[s
].ip
), 0));
667 if (session
[s
].ip
== 0xFFFFFFFE)
668 session
[s
].ip
= 0; // assign from pool
673 if (p
[1] < 6) continue;
674 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
675 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
676 fmtaddr(htonl(session
[s
].dns1
), 0));
681 if (p
[1] < 6) continue;
682 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
683 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
684 fmtaddr(htonl(session
[s
].dns2
), 0));
693 uint8_t *e
= p
+ p
[1];
694 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
702 u
= u
* 10 + *n
- '0';
709 while (n
< e
&& isdigit(*n
))
710 bits
= bits
* 10 + *n
++ - '0';
712 else if ((ip
>> 24) < 128)
714 else if ((ip
>> 24) < 192)
719 if (routes
== MAXROUTE
)
721 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
725 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%d\n",
726 fmtaddr(htonl(ip
), 0), bits
);
728 session
[s
].route
[routes
].ip
= ip
;
729 session
[s
].route
[routes
].prefixlen
= bits
;
736 char *filter
= (char *) p
+ 2;
742 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
743 if ((suffix
= memchr(filter
, '.', l
)))
745 int b
= suffix
- filter
;
746 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
747 fp
= &session
[s
].filter_in
;
748 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
749 fp
= &session
[s
].filter_out
;
756 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
760 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
762 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
767 ip_filters
[f
].used
++;
773 if (p
[1] < 6) continue;
774 session
[s
].session_timeout
= ntohl(*(uint32_t *)(p
+ 2));
775 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", session
[s
].session_timeout
);
776 if(!session
[s
].session_timeout
&& config
->kill_timedout_sessions
)
777 sessionshutdown(s
, "Session timeout is zero", CDN_ADMIN_DISC
, 0);
782 if (p
[1] < 6) continue;
783 session
[s
].idle_timeout
= ntohl(*(uint32_t *)(p
+ 2));
784 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", session
[s
].idle_timeout
);
792 uint8_t *e
= p
+ p
[1];
793 uint8_t *m
= memchr(n
, '/', e
- p
);
796 inet_pton(AF_INET6
, (char *) n
, &r6
);
799 while (m
< e
&& isdigit(*m
)) {
800 prefixlen
= prefixlen
* 10 + *m
++ - '0';
805 LOG(3, s
, session
[s
].tunnel
,
806 " Radius reply contains route for %s/%d\n",
808 session
[s
].ipv6route
= r6
;
809 session
[s
].ipv6prefixlen
= prefixlen
;
814 else if (r_code
== AccessReject
)
816 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
817 sessionkill(s
, "Authentication rejected");
821 if (!session
[s
].dns1
&& config
->default_dns1
)
823 session
[s
].dns1
= ntohl(config
->default_dns1
);
824 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
826 if (!session
[s
].dns2
&& config
->default_dns2
)
828 session
[s
].dns2
= ntohl(config
->default_dns2
);
829 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
832 // Valid Session, set it up
833 session
[s
].unique_id
= 0;
838 // An ack for a stop or start record.
839 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
844 // finished with RADIUS
848 // Send a retry for RADIUS/CHAP message
849 void radiusretry(uint16_t r
)
851 sessionidt s
= radius
[r
].session
;
856 if (s
) t
= session
[s
].tunnel
;
858 switch (radius
[r
].state
)
860 case RADIUSCHAP
: // sending CHAP down PPP
863 case RADIUSAUTH
: // sending auth to RADIUS server
864 case RADIUSJUSTAUTH
: // sending auth to RADIUS server
865 case RADIUSSTART
: // sending start accounting to RADIUS server
866 case RADIUSSTOP
: // sending stop accounting to RADIUS server
867 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
868 radiussend(r
, radius
[r
].state
);
871 case RADIUSNULL
: // Not in use
872 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
873 // free up RADIUS task
875 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
882 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
884 int i
, r_code
, r_id
, length
, attribute_length
;
885 uint8_t *packet
, attribute
;
887 char username
[MAXUSER
] = "";
897 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
902 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
903 LOG_HEX(5, "DAE Request", buf
, len
);
905 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
907 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
911 r_code
= buf
[0]; // request type
912 r_id
= buf
[1]; // radius indentifier.
914 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
916 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
920 if (!config
->cluster_iam_master
)
922 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
926 len
= ntohs(*(uint16_t *) (buf
+ 2));
928 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
930 // check authenticator
931 calc_auth(buf
, len
, zero
, hash
);
932 if (memcmp(hash
, buf
+ 4, 16) != 0)
934 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
944 attribute
= *packet
++;
945 attribute_length
= *packet
++;
946 if (attribute_length
< 2)
949 length
-= attribute_length
;
950 attribute_length
-= 2;
953 case 1: /* username */
954 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
955 memcpy(username
, packet
, len
);
957 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
960 case 4: /* nas ip address */
961 nas
= *(uint32_t *) packet
; // net order
962 if (nas
!= config
->bind_address
)
963 error
= 403; // NAS identification mismatch
965 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
968 case 5: /* nas port */
969 port
= ntohl(*(uint32_t *) packet
);
970 if (port
< 1 || port
> MAXSESSION
)
973 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
976 case 6: /* service type */
978 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
979 auth_only
= service_type
== 8; // Authenticate only
981 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
985 case 8: /* ip address */
986 ip
= *(uint32_t *) packet
; // net order
987 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
990 case 11: /* filter id */
991 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
992 if (!(p
= memchr(packet
, '.', attribute_length
)))
994 error
= 404; // invalid request
999 i
= find_filter((char *) packet
, len
);
1000 if (i
< 0 || !*ip_filters
[i
].name
)
1006 if (!memcmp(p
, ".in", attribute_length
- len
))
1008 else if (!memcmp(p
, ".out", attribute_length
- len
))
1015 case 26: /* vendor specific */
1016 if (attribute_length
>= 6
1017 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1018 && *(packet
+ 4) == 1 // Cisco-AVPair
1019 && *(packet
+ 5) >= 2) // length
1021 int len
= *(packet
+ 5) - 2;
1022 uint8_t *a
= packet
+ 6;
1024 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1025 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1028 avpair_len
[avp
++] = len
;
1034 packet
+= attribute_length
;
1037 if (!error
&& auth_only
)
1039 if (fin
!= -1 || fout
!= -1 || avp
)
1040 error
= 401; // unsupported attribute
1042 error
= 405; // unsupported service
1045 if (!error
&& !(port
|| ip
|| *username
))
1046 error
= 402; // missing attribute
1048 // exact match for SID if given
1052 if (!session
[s
].opened
)
1053 error
= 503; // not found
1058 // find/check session by IP
1059 i
= sessionbyip(ip
);
1060 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1066 if (!error
&& *username
)
1070 if (strcmp(session
[s
].user
, username
))
1073 else if (!(s
= sessionbyuser(username
)))
1077 t
= session
[s
].tunnel
;
1081 case DisconnectRequest
: // Packet of Disconnect/Death
1084 r_code
= DisconnectNAK
;
1088 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1089 r_code
= DisconnectACK
;
1091 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1094 case CoARequest
: // Change of Authorization
1101 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1106 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1107 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1114 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1119 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1121 filter_session(s
, fin
, fout
);
1123 // process cisco av-pair(s)
1124 for (i
= 0; i
< avp
; i
++)
1126 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1127 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1130 cluster_send_session(s
);
1148 *(uint32_t *) packet
= htonl(error
);
1152 *((uint16_t *)(buf
+ 2)) = htons(len
);
1155 calc_auth(buf
, len
, hash
, buf
+ 4);
1157 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1159 // send DAE response
1160 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1161 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));