6 #include <sys/socket.h>
10 #include <arpa/inet.h>
12 #include <netinet/in.h>
15 #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 static const hasht zero
;
30 static void calc_auth(const void *buf
, size_t len
, const uint8_t *in
, uint8_t *out
)
35 MD5_Update(&ctx
, (void *)buf
, 4); // code, id, length
36 MD5_Update(&ctx
, (void *)in
, 16); // auth
37 MD5_Update(&ctx
, (void *)(buf
+ 20), len
- 20);
38 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
42 // Set up socket for radius requests
47 uint16_t min
= config
->radius_bind_min
;
48 uint16_t max
= config
->radius_bind_max
;
50 struct sockaddr_in addr
;
58 else if (max
) /* no minimum specified, bind from max down */
65 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", RADIUS_FDS
);
66 radfds
= calloc(sizeof(int), RADIUS_FDS
);
67 for (i
= 0; i
< RADIUS_FDS
; i
++)
70 radfds
[i
] = socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
71 flags
= fcntl(radfds
[i
], F_GETFL
, 0);
72 fcntl(radfds
[i
], F_SETFL
, flags
| O_NONBLOCK
);
78 memset(&addr
, 0, sizeof(addr
));
79 addr
.sin_family
= AF_INET
;
80 addr
.sin_addr
.s_addr
= INADDR_ANY
;
83 addr
.sin_port
= htons(port
);
84 if ((b
= bind(radfds
[i
], (struct sockaddr
*) &addr
, sizeof(addr
))) < 0)
86 if ((port
+= inc
) < min
|| port
> max
)
88 LOG(0, 0, 0, "Can't bind RADIUS socket in range %u-%u\n", min
, max
);
97 void radiusclear(uint16_t r
, sessionidt s
)
99 if (s
) sess_local
[s
].radius
= 0;
100 memset(&radius
[r
], 0, sizeof(radius
[r
])); // radius[r].state = RADIUSNULL;
103 static uint16_t get_free_radius()
106 static uint32_t next_radius_id
= 0;
108 for (count
= MAXRADIUS
; count
> 0; --count
)
110 ++next_radius_id
; // Find the next ID to check.
111 if (next_radius_id
>= MAXRADIUS
)
114 if (radius
[next_radius_id
].state
== RADIUSNULL
)
116 return next_radius_id
;
120 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
124 uint16_t radiusnew(sessionidt s
)
126 uint16_t r
= sess_local
[s
].radius
;
131 LOG(3, s
, session
[s
].tunnel
, "Re-used radius %d\n", r
);
135 if (!(r
= get_free_radius()))
137 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions\n");
138 STAT(radius_overflow
);
142 memset(&radius
[r
], 0, sizeof(radius
[r
]));
143 sess_local
[s
].radius
= r
;
144 radius
[r
].session
= s
;
145 radius
[r
].state
= RADIUSWAIT
;
146 radius
[r
].retry
= TIME
+ 1200; // Wait at least 120 seconds to re-claim this.
148 random_data(radius
[r
].auth
, sizeof(radius
[r
].auth
));
150 LOG(3, s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
154 // Send a RADIUS request
155 void radiussend(uint16_t r
, uint8_t state
)
157 struct sockaddr_in addr
;
158 uint8_t b
[4096]; // RADIUS packet
166 s
= radius
[r
].session
;
167 if (!config
->numradiusservers
)
169 LOG(0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
172 if (!*config
->radiussecret
)
174 LOG(0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
178 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
&& !config
->radius_accounting
)
180 // Radius accounting is turned off
185 if (radius
[r
].state
!= state
)
188 radius
[r
].state
= state
;
189 radius
[r
].retry
= backoff(radius
[r
].try++) + 20; // 3s, 4s, 6s, 10s...
190 LOG(4, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
191 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
192 radius_state(radius
[r
].state
), radius
[r
].try);
194 if (radius
[r
].try > config
->numradiusservers
* 2)
198 if (state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
)
199 sessionshutdown(s
, "RADIUS timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
202 LOG(1, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
203 radius_state(state
));
206 STAT(radius_timeout
);
210 STAT(radius_retries
);
211 radius
[r
].state
= RADIUSWAIT
;
212 radius
[r
].retry
= 100;
216 // contruct RADIUS access request
221 b
[0] = AccessRequest
; // access request
226 b
[0] = AccountingRequest
; // accounting request
229 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
231 b
[1] = r
>> RADIUS_SHIFT
; // identifier
232 memcpy(b
+ 4, radius
[r
].auth
, 16);
237 p
[1] = strlen(session
[s
].user
) + 2;
238 strcpy((char *) p
+ 2, session
[s
].user
);
241 if (state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
)
245 *p
= 3; // CHAP password
247 p
[2] = radius
[r
].id
; // ID
248 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
250 *p
= 60; // CHAP Challenge
252 memcpy(p
+ 2, radius
[r
].auth
, 16);
257 strcpy(pass
, radius
[r
].pass
);
260 pass
[pl
++] = 0; // pad
269 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
271 MD5_Update(&ctx
, pass
+ p
- 16, 16);
273 MD5_Update(&ctx
, radius
[r
].auth
, 16);
274 MD5_Final(hash
, &ctx
);
277 pass
[p
] ^= hash
[p
& 15];
286 memcpy(p
+ 2, pass
, pl
);
292 *p
= 40; // accounting type
294 *(uint32_t *) (p
+ 2) = htonl(state
- RADIUSSTART
+ 1); // start=1, stop=2, interim=3
298 *p
= 44; // session ID
300 sprintf((char *) p
+ 2, "%08X%08X", session
[s
].unique_id
, session
[s
].opened
);
302 if (state
== RADIUSSTART
)
306 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
308 sess_local
[s
].last_interim
= time_now
; // Setup "first" Interim
312 *p
= 42; // input octets
314 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
317 *p
= 43; // output octets
319 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
322 *p
= 46; // session time
324 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
327 *p
= 47; // input packets
329 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
332 *p
= 48; // output packets
334 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
337 *p
= 52; // input gigawords
339 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin_wrap
);
342 *p
= 53; // output gigawords
344 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout_wrap
);
347 if (state
== RADIUSSTOP
&& radius
[r
].term_cause
)
349 *p
= 49; // acct-terminate-cause
351 *(uint32_t *) (p
+ 2) = htonl(radius
[r
].term_cause
);
354 if (radius
[r
].term_msg
)
356 *p
= 26; // vendor-specific
357 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
358 p
[6] = 1; // Cisco-AVPair
359 p
[7] = 2 + sprintf((char *) p
+ 8, "disc-cause-ext=%s", radius
[r
].term_msg
);
366 if (session
[s
].classlen
) {
368 p
[1] = session
[s
].classlen
+ 2;
369 memcpy(p
+ 2, session
[s
].class, session
[s
].classlen
);
374 struct param_radius_account acct
= { &tunnel
[session
[s
].tunnel
], &session
[s
], &p
};
375 run_plugins(PLUGIN_RADIUS_ACCOUNT
, &acct
);
384 *(uint32_t *) (p
+ 2) = htonl(s
);
387 *p
= 6; // Service-Type
389 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 8 : 2)); // Authenticate only or Framed-User respectevily
392 *p
= 7; // Framed-Protocol
393 p
[1] = htonl((state
== RADIUSJUSTAUTH
? 0 : 6));
394 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 0 : 1)); // PPP
399 *p
= 8; // Framed-IP-Address
401 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
405 if (session
[s
].route
[0].ip
)
408 for (r
= 0; s
&& r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
410 *p
= 22; // Framed-Route
411 p
[1] = sprintf((char *) p
+ 2, "%s/%d %s 1",
412 fmtaddr(htonl(session
[s
].route
[r
].ip
), 0),
413 session
[s
].route
[r
].prefixlen
,
414 fmtaddr(htonl(session
[s
].ip
), 1)) + 2;
420 if (session
[s
].session_timeout
)
422 *p
= 27; // Session-Timeout
424 *(uint32_t *) (p
+ 2) = htonl(session
[s
].session_timeout
);
428 if (session
[s
].idle_timeout
)
430 *p
= 28; // Idle-Timeout
432 *(uint32_t *) (p
+ 2) = htonl(session
[s
].idle_timeout
);
436 if (*session
[s
].called
)
439 p
[1] = strlen(session
[s
].called
) + 2;
440 strcpy((char *) p
+ 2, session
[s
].called
);
444 if (*session
[s
].calling
)
447 p
[1] = strlen(session
[s
].calling
) + 2;
448 strcpy((char *) p
+ 2, session
[s
].calling
);
456 *(uint32_t *)(p
+ 2) = config
->bind_address
;
460 *(uint16_t *) (b
+ 2) = htons(p
- b
);
461 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
)
463 // Build auth for accounting packet
464 calc_auth(b
, p
- b
, zero
, b
+ 4);
465 memcpy(radius
[r
].auth
, b
+ 4, 16);
467 memset(&addr
, 0, sizeof(addr
));
468 addr
.sin_family
= AF_INET
;
469 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
472 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
473 // assume RADIUS accounting port is the authentication port +1
474 addr
.sin_port
= htons((state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
) ? port
: port
+1);
477 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
478 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
481 static void handle_avpair(sessionidt s
, uint8_t *avp
, int len
)
484 uint8_t *value
= memchr(avp
, '=', len
);
485 uint8_t tmp
[2048] = "";
499 if (len
> 2 && (*value
== '"' || *value
== '\'') && value
[len
- 1] == *value
)
505 // copy and null terminate
506 else if (len
< sizeof(tmp
) - 1)
508 memcpy(tmp
, value
, len
);
517 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], (char *) key
, (char *) value
};
518 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
522 // process RADIUS response
523 void processrad(uint8_t *buf
, int len
, char socket_index
)
536 LOG_HEX(5, "RADIUS Response", buf
, len
);
537 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
539 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
543 r_code
= buf
[0]; // response type
544 r_id
= buf
[1]; // radius reply indentifier.
546 len
= ntohs(*(uint16_t *) (buf
+ 2));
547 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
548 s
= radius
[r
].session
;
549 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
550 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
552 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
554 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
557 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSJUSTAUTH
&& radius
[r
].state
!= RADIUSSTART
558 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
560 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
563 t
= session
[s
].tunnel
;
564 calc_auth(buf
, len
, radius
[r
].auth
, hash
);
566 if (memcmp(hash
, buf
+ 4, 16))
568 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
569 return; // Do nothing. On timeout, it will try the next radius server.
572 if (((radius
[r
].state
== RADIUSAUTH
||radius
[r
].state
== RADIUSJUSTAUTH
) && r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
573 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
575 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
576 return; // We got something we didn't expect. Let the timeouts take
577 // care off finishing the radius session if that's really correct.
580 if (radius
[r
].state
== RADIUSAUTH
|| radius
[r
].state
== RADIUSJUSTAUTH
)
582 // run post-auth plugin
583 struct param_post_auth packet
= {
587 (r_code
== AccessAccept
),
588 radius
[r
].chap
? PPPCHAP
: PPPPAP
591 run_plugins(PLUGIN_POST_AUTH
, &packet
);
592 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
594 // process auth response
598 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
599 if (!p
) return; // Abort!
601 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
603 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
604 tunnelsend(b
, (p
- b
) + 4, t
); // send it
606 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
607 (r_code
== AccessAccept
) ? "allowed" : "denied");
612 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
613 if (!p
) return; // Abort!
618 *(uint16_t *) (p
+ 2) = ntohs(5);
619 p
[4] = 0; // no message
620 tunnelsend(b
, (p
- b
) + 5, t
); // send it
622 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
623 (r_code
== AccessAccept
) ? "allowed" : "denied");
626 if (r_code
== AccessAccept
)
629 // Extract IP, routes, etc
630 uint8_t *p
= buf
+ 20;
631 uint8_t *e
= buf
+ len
;
632 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
634 if (*p
== 26 && p
[1] >= 7)
636 // Vendor-Specific Attribute
637 uint32_t vendor
= ntohl(*(int *)(p
+ 2));
638 uint8_t attrib
= *(p
+ 6);
639 int attrib_length
= *(p
+ 7) - 2;
641 LOG(4, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor
, attrib
, attrib_length
);
642 if (vendor
== 9 && attrib
== 1) // Cisco-AVPair
644 if (attrib_length
< 0) continue;
645 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
646 attrib_length
, p
+ 8);
648 handle_avpair(s
, p
+ 8, attrib_length
);
651 else if (vendor
== 529 && attrib
>= 135 && attrib
<= 136) // Ascend
653 // handle old-format ascend DNS attributes below
658 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
666 if (p
[1] < 6) continue;
667 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
668 session
[s
].ip_pool_index
= -1;
669 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
670 fmtaddr(htonl(session
[s
].ip
), 0));
672 if (session
[s
].ip
== 0xFFFFFFFE)
673 session
[s
].ip
= 0; // assign from pool
678 if (p
[1] < 6) continue;
679 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
680 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
681 fmtaddr(htonl(session
[s
].dns1
), 0));
686 if (p
[1] < 6) continue;
687 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
688 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
689 fmtaddr(htonl(session
[s
].dns2
), 0));
698 uint8_t *e
= p
+ p
[1];
699 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
707 u
= u
* 10 + *n
- '0';
714 while (n
< e
&& isdigit(*n
))
715 bits
= bits
* 10 + *n
++ - '0';
717 else if ((ip
>> 24) < 128)
719 else if ((ip
>> 24) < 192)
724 if (routes
== MAXROUTE
)
726 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
730 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%d\n",
731 fmtaddr(htonl(ip
), 0), bits
);
733 session
[s
].route
[routes
].ip
= ip
;
734 session
[s
].route
[routes
].prefixlen
= bits
;
741 char *filter
= (char *) p
+ 2;
747 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
748 if ((suffix
= memchr(filter
, '.', l
)))
750 int b
= suffix
- filter
;
751 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
752 fp
= &session
[s
].filter_in
;
753 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
754 fp
= &session
[s
].filter_out
;
761 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
765 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
767 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
772 ip_filters
[f
].used
++;
778 if (p
[1] < 6) continue;
779 session
[s
].session_timeout
= ntohl(*(uint32_t *)(p
+ 2));
780 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", session
[s
].session_timeout
);
781 if(!session
[s
].session_timeout
&& config
->kill_timedout_sessions
)
782 sessionshutdown(s
, "Session timeout is zero", CDN_ADMIN_DISC
, 0);
787 if (p
[1] < 6) continue;
788 session
[s
].idle_timeout
= ntohl(*(uint32_t *)(p
+ 2));
789 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", session
[s
].idle_timeout
);
797 uint8_t *e
= p
+ p
[1];
798 uint8_t *m
= memchr(n
, '/', e
- p
);
801 inet_pton(AF_INET6
, (char *) n
, &r6
);
804 while (m
< e
&& isdigit(*m
)) {
805 prefixlen
= prefixlen
* 10 + *m
++ - '0';
810 LOG(3, s
, session
[s
].tunnel
,
811 " Radius reply contains route for %s/%d\n",
813 session
[s
].ipv6route
= r6
;
814 session
[s
].ipv6prefixlen
= prefixlen
;
820 if (p
[1] < 3) continue;
821 session
[s
].classlen
= p
[1] - 2;
822 if (session
[s
].classlen
> MAXCLASS
)
823 session
[s
].classlen
= MAXCLASS
;
824 memcpy(session
[s
].class, p
+ 2, session
[s
].classlen
);
828 else if (r_code
== AccessReject
)
830 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
831 sessionkill(s
, "Authentication rejected");
835 if (!session
[s
].dns1
&& config
->default_dns1
)
837 session
[s
].dns1
= ntohl(config
->default_dns1
);
838 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
840 if (!session
[s
].dns2
&& config
->default_dns2
)
842 session
[s
].dns2
= ntohl(config
->default_dns2
);
843 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
846 // Valid Session, set it up
847 session
[s
].unique_id
= 0;
852 // An ack for a stop or start record.
853 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
858 // finished with RADIUS
862 // Send a retry for RADIUS/CHAP message
863 void radiusretry(uint16_t r
)
865 sessionidt s
= radius
[r
].session
;
870 if (s
) t
= session
[s
].tunnel
;
872 switch (radius
[r
].state
)
874 case RADIUSCHAP
: // sending CHAP down PPP
877 case RADIUSAUTH
: // sending auth to RADIUS server
878 case RADIUSJUSTAUTH
: // sending auth to RADIUS server
879 case RADIUSSTART
: // sending start accounting to RADIUS server
880 case RADIUSSTOP
: // sending stop accounting to RADIUS server
881 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
882 radiussend(r
, radius
[r
].state
);
885 case RADIUSNULL
: // Not in use
886 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
887 // free up RADIUS task
889 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
896 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
898 int i
, r_code
, r_id
, length
, attribute_length
;
899 uint8_t *packet
, attribute
;
901 char username
[MAXUSER
] = "";
911 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
916 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
917 LOG_HEX(5, "DAE Request", buf
, len
);
919 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
921 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
925 r_code
= buf
[0]; // request type
926 r_id
= buf
[1]; // radius indentifier.
928 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
930 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
934 if (!config
->cluster_iam_master
)
936 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
940 len
= ntohs(*(uint16_t *) (buf
+ 2));
942 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
944 // check authenticator
945 calc_auth(buf
, len
, zero
, hash
);
946 if (memcmp(hash
, buf
+ 4, 16) != 0)
948 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
958 attribute
= *packet
++;
959 attribute_length
= *packet
++;
960 if (attribute_length
< 2)
963 length
-= attribute_length
;
964 attribute_length
-= 2;
967 case 1: /* username */
968 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
969 memcpy(username
, packet
, len
);
971 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
974 case 4: /* nas ip address */
975 nas
= *(uint32_t *) packet
; // net order
976 if (nas
!= config
->bind_address
)
977 error
= 403; // NAS identification mismatch
979 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
982 case 5: /* nas port */
983 port
= ntohl(*(uint32_t *) packet
);
984 if (port
< 1 || port
> MAXSESSION
)
987 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
990 case 6: /* service type */
992 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
993 auth_only
= service_type
== 8; // Authenticate only
995 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
999 case 8: /* ip address */
1000 ip
= *(uint32_t *) packet
; // net order
1001 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
1004 case 11: /* filter id */
1005 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
1006 if (!(p
= memchr(packet
, '.', attribute_length
)))
1008 error
= 404; // invalid request
1013 i
= find_filter((char *) packet
, len
);
1014 if (i
< 0 || !*ip_filters
[i
].name
)
1020 if (!memcmp(p
, ".in", attribute_length
- len
))
1022 else if (!memcmp(p
, ".out", attribute_length
- len
))
1029 case 26: /* vendor specific */
1030 if (attribute_length
>= 6
1031 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1032 && *(packet
+ 4) == 1 // Cisco-AVPair
1033 && *(packet
+ 5) >= 2) // length
1035 int len
= *(packet
+ 5) - 2;
1036 uint8_t *a
= packet
+ 6;
1038 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1039 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1042 avpair_len
[avp
++] = len
;
1048 packet
+= attribute_length
;
1051 if (!error
&& auth_only
)
1053 if (fin
!= -1 || fout
!= -1 || avp
)
1054 error
= 401; // unsupported attribute
1056 error
= 405; // unsupported service
1059 if (!error
&& !(port
|| ip
|| *username
))
1060 error
= 402; // missing attribute
1062 // exact match for SID if given
1066 if (!session
[s
].opened
)
1067 error
= 503; // not found
1072 // find/check session by IP
1073 i
= sessionbyip(ip
);
1074 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1080 if (!error
&& *username
)
1084 if (strcmp(session
[s
].user
, username
))
1087 else if (!(s
= sessionbyuser(username
)))
1091 t
= session
[s
].tunnel
;
1095 case DisconnectRequest
: // Packet of Disconnect/Death
1098 r_code
= DisconnectNAK
;
1102 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1103 r_code
= DisconnectACK
;
1105 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1108 case CoARequest
: // Change of Authorization
1115 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1120 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1121 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1128 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1133 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1135 filter_session(s
, fin
, fout
);
1137 // process cisco av-pair(s)
1138 for (i
= 0; i
< avp
; i
++)
1140 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1141 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1144 cluster_send_session(s
);
1162 *(uint32_t *) packet
= htonl(error
);
1166 *((uint16_t *)(buf
+ 2)) = htons(len
);
1169 calc_auth(buf
, len
, hash
, buf
+ 4);
1171 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1173 // send DAE response
1174 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1175 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));