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
);
367 struct param_radius_account acct
= { &tunnel
[session
[s
].tunnel
], &session
[s
], &p
};
368 run_plugins(PLUGIN_RADIUS_ACCOUNT
, &acct
);
377 *(uint32_t *) (p
+ 2) = htonl(s
);
380 *p
= 6; // Service-Type
382 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 8 : 2)); // Authenticate only or Framed-User respectevily
385 *p
= 7; // Framed-Protocol
386 p
[1] = htonl((state
== RADIUSJUSTAUTH
? 0 : 6));
387 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 0 : 1)); // PPP
392 *p
= 8; // Framed-IP-Address
394 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
398 if (session
[s
].route
[0].ip
)
401 for (r
= 0; s
&& r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
404 if (session
[s
].route
[r
].mask
)
406 int mask
= session
[s
].route
[r
].mask
;
414 *p
= 22; // Framed-Route
415 p
[1] = sprintf((char *) p
+ 2, "%s/%d %s 1",
416 fmtaddr(htonl(session
[s
].route
[r
].ip
), 0),
417 width
, fmtaddr(htonl(session
[s
].ip
), 1)) + 2;
423 if (session
[s
].session_timeout
)
425 *p
= 27; // Session-Timeout
427 *(uint32_t *) (p
+ 2) = htonl(session
[s
].session_timeout
);
431 if (session
[s
].idle_timeout
)
433 *p
= 28; // Idle-Timeout
435 *(uint32_t *) (p
+ 2) = htonl(session
[s
].idle_timeout
);
439 if (*session
[s
].called
)
442 p
[1] = strlen(session
[s
].called
) + 2;
443 strcpy((char *) p
+ 2, session
[s
].called
);
447 if (*session
[s
].calling
)
450 p
[1] = strlen(session
[s
].calling
) + 2;
451 strcpy((char *) p
+ 2, session
[s
].calling
);
459 *(uint32_t *)(p
+ 2) = config
->bind_address
;
463 *(uint16_t *) (b
+ 2) = htons(p
- b
);
464 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
)
466 // Build auth for accounting packet
467 calc_auth(b
, p
- b
, zero
, b
+ 4);
468 memcpy(radius
[r
].auth
, b
+ 4, 16);
470 memset(&addr
, 0, sizeof(addr
));
471 addr
.sin_family
= AF_INET
;
472 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
475 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
476 // assume RADIUS accounting port is the authentication port +1
477 addr
.sin_port
= htons((state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
) ? port
: port
+1);
480 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
481 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
484 static void handle_avpair(sessionidt s
, uint8_t *avp
, int len
)
487 uint8_t *value
= memchr(avp
, '=', len
);
488 uint8_t tmp
[2048] = "";
502 if (len
> 2 && (*value
== '"' || *value
== '\'') && value
[len
- 1] == *value
)
508 // copy and null terminate
509 else if (len
< sizeof(tmp
) - 1)
511 memcpy(tmp
, value
, len
);
520 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], (char *) key
, (char *) value
};
521 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
525 // process RADIUS response
526 void processrad(uint8_t *buf
, int len
, char socket_index
)
539 LOG_HEX(5, "RADIUS Response", buf
, len
);
540 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
542 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
546 r_code
= buf
[0]; // response type
547 r_id
= buf
[1]; // radius reply indentifier.
549 len
= ntohs(*(uint16_t *) (buf
+ 2));
550 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
551 s
= radius
[r
].session
;
552 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
553 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
555 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
557 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
560 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSJUSTAUTH
&& radius
[r
].state
!= RADIUSSTART
561 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
563 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
566 t
= session
[s
].tunnel
;
567 calc_auth(buf
, len
, radius
[r
].auth
, hash
);
569 if (memcmp(hash
, buf
+ 4, 16))
571 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
572 return; // Do nothing. On timeout, it will try the next radius server.
575 if (((radius
[r
].state
== RADIUSAUTH
||radius
[r
].state
== RADIUSJUSTAUTH
) && r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
576 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
578 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
579 return; // We got something we didn't expect. Let the timeouts take
580 // care off finishing the radius session if that's really correct.
583 if (radius
[r
].state
== RADIUSAUTH
|| radius
[r
].state
== RADIUSJUSTAUTH
)
585 // run post-auth plugin
586 struct param_post_auth packet
= {
590 (r_code
== AccessAccept
),
591 radius
[r
].chap
? PPPCHAP
: PPPPAP
594 run_plugins(PLUGIN_POST_AUTH
, &packet
);
595 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
597 // process auth response
601 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
602 if (!p
) return; // Abort!
604 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
606 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
607 tunnelsend(b
, (p
- b
) + 4, t
); // send it
609 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
610 (r_code
== AccessAccept
) ? "allowed" : "denied");
615 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
616 if (!p
) return; // Abort!
621 *(uint16_t *) (p
+ 2) = ntohs(5);
622 p
[4] = 0; // no message
623 tunnelsend(b
, (p
- b
) + 5, t
); // send it
625 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
626 (r_code
== AccessAccept
) ? "allowed" : "denied");
629 if (r_code
== AccessAccept
)
632 // Extract IP, routes, etc
633 uint8_t *p
= buf
+ 20;
634 uint8_t *e
= buf
+ len
;
635 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
637 if (*p
== 26 && p
[1] >= 7)
639 // Vendor-Specific Attribute
640 uint32_t vendor
= ntohl(*(int *)(p
+ 2));
641 uint8_t attrib
= *(p
+ 6);
642 int attrib_length
= *(p
+ 7) - 2;
644 LOG(4, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor
, attrib
, attrib_length
);
645 if (vendor
== 9 && attrib
== 1) // Cisco-AVPair
647 if (attrib_length
< 0) continue;
648 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
649 attrib_length
, p
+ 8);
651 handle_avpair(s
, p
+ 8, attrib_length
);
654 else if (vendor
== 529 && attrib
>= 135 && attrib
<= 136) // Ascend
656 // handle old-format ascend DNS attributes below
661 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
669 if (p
[1] < 6) continue;
670 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
671 session
[s
].ip_pool_index
= -1;
672 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
673 fmtaddr(htonl(session
[s
].ip
), 0));
675 if (session
[s
].ip
== 0xFFFFFFFE)
676 session
[s
].ip
= 0; // assign from pool
681 if (p
[1] < 6) continue;
682 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
683 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
684 fmtaddr(htonl(session
[s
].dns1
), 0));
689 if (p
[1] < 6) continue;
690 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
691 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
692 fmtaddr(htonl(session
[s
].dns2
), 0));
697 in_addr_t ip
= 0, mask
= 0;
701 uint8_t *e
= p
+ p
[1];
702 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
710 u
= u
* 10 + *n
- '0';
717 while (n
< e
&& isdigit(*n
))
718 bits
= bits
* 10 + *n
++ - '0';
719 mask
= (( -1) << (32 - bits
));
721 else if ((ip
>> 24) < 128)
723 else if ((ip
>> 24) < 192)
728 if (routes
== MAXROUTE
)
730 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
734 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%s\n",
735 fmtaddr(htonl(ip
), 0), fmtaddr(htonl(mask
), 1));
737 session
[s
].route
[routes
].ip
= ip
;
738 session
[s
].route
[routes
].mask
= mask
;
745 char *filter
= (char *) p
+ 2;
751 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
752 if ((suffix
= memchr(filter
, '.', l
)))
754 int b
= suffix
- filter
;
755 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
756 fp
= &session
[s
].filter_in
;
757 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
758 fp
= &session
[s
].filter_out
;
765 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
769 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
771 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
776 ip_filters
[f
].used
++;
782 if (p
[1] < 6) continue;
783 session
[s
].session_timeout
= ntohl(*(uint32_t *)(p
+ 2));
784 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", session
[s
].session_timeout
);
785 if(!session
[s
].session_timeout
&& config
->kill_timedout_sessions
)
786 sessionshutdown(s
, "Session timeout is zero", CDN_ADMIN_DISC
, 0);
791 if (p
[1] < 6) continue;
792 session
[s
].idle_timeout
= ntohl(*(uint32_t *)(p
+ 2));
793 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", session
[s
].idle_timeout
);
801 uint8_t *e
= p
+ p
[1];
802 uint8_t *m
= memchr(n
, '/', e
- p
);
805 inet_pton(AF_INET6
, (char *) n
, &r6
);
808 while (m
< e
&& isdigit(*m
)) {
809 prefixlen
= prefixlen
* 10 + *m
++ - '0';
814 LOG(3, s
, session
[s
].tunnel
,
815 " Radius reply contains route for %s/%d\n",
817 session
[s
].ipv6route
= r6
;
818 session
[s
].ipv6prefixlen
= prefixlen
;
823 else if (r_code
== AccessReject
)
825 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
826 sessionkill(s
, "Authentication rejected");
830 if (!session
[s
].dns1
&& config
->default_dns1
)
832 session
[s
].dns1
= ntohl(config
->default_dns1
);
833 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
835 if (!session
[s
].dns2
&& config
->default_dns2
)
837 session
[s
].dns2
= ntohl(config
->default_dns2
);
838 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
841 // Valid Session, set it up
842 session
[s
].unique_id
= 0;
847 // An ack for a stop or start record.
848 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
853 // finished with RADIUS
857 // Send a retry for RADIUS/CHAP message
858 void radiusretry(uint16_t r
)
860 sessionidt s
= radius
[r
].session
;
865 if (s
) t
= session
[s
].tunnel
;
867 switch (radius
[r
].state
)
869 case RADIUSCHAP
: // sending CHAP down PPP
872 case RADIUSAUTH
: // sending auth to RADIUS server
873 case RADIUSJUSTAUTH
: // sending auth to RADIUS server
874 case RADIUSSTART
: // sending start accounting to RADIUS server
875 case RADIUSSTOP
: // sending stop accounting to RADIUS server
876 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
877 radiussend(r
, radius
[r
].state
);
880 case RADIUSNULL
: // Not in use
881 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
882 // free up RADIUS task
884 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
891 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
893 int i
, r_code
, r_id
, length
, attribute_length
;
894 uint8_t *packet
, attribute
;
896 char username
[MAXUSER
] = "";
906 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
911 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
912 LOG_HEX(5, "DAE Request", buf
, len
);
914 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
916 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
920 r_code
= buf
[0]; // request type
921 r_id
= buf
[1]; // radius indentifier.
923 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
925 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
929 if (!config
->cluster_iam_master
)
931 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
935 len
= ntohs(*(uint16_t *) (buf
+ 2));
937 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
939 // check authenticator
940 calc_auth(buf
, len
, zero
, hash
);
941 if (memcmp(hash
, buf
+ 4, 16) != 0)
943 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
953 attribute
= *packet
++;
954 attribute_length
= *packet
++;
955 if (attribute_length
< 2)
958 length
-= attribute_length
;
959 attribute_length
-= 2;
962 case 1: /* username */
963 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
964 memcpy(username
, packet
, len
);
966 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
969 case 4: /* nas ip address */
970 nas
= *(uint32_t *) packet
; // net order
971 if (nas
!= config
->bind_address
)
972 error
= 403; // NAS identification mismatch
974 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
977 case 5: /* nas port */
978 port
= ntohl(*(uint32_t *) packet
);
979 if (port
< 1 || port
> MAXSESSION
)
982 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
985 case 6: /* service type */
987 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
988 auth_only
= service_type
== 8; // Authenticate only
990 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
994 case 8: /* ip address */
995 ip
= *(uint32_t *) packet
; // net order
996 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
999 case 11: /* filter id */
1000 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
1001 if (!(p
= memchr(packet
, '.', attribute_length
)))
1003 error
= 404; // invalid request
1008 i
= find_filter((char *) packet
, len
);
1009 if (i
< 0 || !*ip_filters
[i
].name
)
1015 if (!memcmp(p
, ".in", attribute_length
- len
))
1017 else if (!memcmp(p
, ".out", attribute_length
- len
))
1024 case 26: /* vendor specific */
1025 if (attribute_length
>= 6
1026 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1027 && *(packet
+ 4) == 1 // Cisco-AVPair
1028 && *(packet
+ 5) >= 2) // length
1030 int len
= *(packet
+ 5) - 2;
1031 uint8_t *a
= packet
+ 6;
1033 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1034 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1037 avpair_len
[avp
++] = len
;
1043 packet
+= attribute_length
;
1046 if (!error
&& auth_only
)
1048 if (fin
!= -1 || fout
!= -1 || avp
)
1049 error
= 401; // unsupported attribute
1051 error
= 405; // unsupported service
1054 if (!error
&& !(port
|| ip
|| *username
))
1055 error
= 402; // missing attribute
1057 // exact match for SID if given
1061 if (!session
[s
].opened
)
1062 error
= 503; // not found
1067 // find/check session by IP
1068 i
= sessionbyip(ip
);
1069 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1075 if (!error
&& *username
)
1079 if (strcmp(session
[s
].user
, username
))
1082 else if (!(s
= sessionbyuser(username
)))
1086 t
= session
[s
].tunnel
;
1090 case DisconnectRequest
: // Packet of Disconnect/Death
1093 r_code
= DisconnectNAK
;
1097 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1098 r_code
= DisconnectACK
;
1100 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1103 case CoARequest
: // Change of Authorization
1110 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1115 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1116 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1123 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1128 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1130 filter_session(s
, fin
, fout
);
1132 // process cisco av-pair(s)
1133 for (i
= 0; i
< avp
; i
++)
1135 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1136 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1139 cluster_send_session(s
);
1157 *(uint32_t *) packet
= htonl(error
);
1161 *((uint16_t *)(buf
+ 2)) = htons(len
);
1164 calc_auth(buf
, len
, hash
, buf
+ 4);
1166 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1168 // send DAE response
1169 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1170 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));