6 #include <sys/socket.h>
10 #include <arpa/inet.h>
12 #include <netinet/in.h>
16 #include "constants.h"
26 extern radiust
*radius
;
27 extern sessiont
*session
;
28 extern tunnelt
*tunnel
;
29 extern configt
*config
;
31 extern ip_filtert
*ip_filters
;
33 static const hasht zero
;
35 static void calc_auth(const void *buf
, size_t len
, const uint8_t *in
, uint8_t *out
)
40 MD5_Update(&ctx
, (void *)buf
, 4); // code, id, length
41 MD5_Update(&ctx
, (void *)in
, 16); // auth
42 MD5_Update(&ctx
, (void *)(buf
+ 20), len
- 20);
43 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
47 // Set up socket for radius requests
52 uint16_t min
= config
->radius_bind_min
;
53 uint16_t max
= config
->radius_bind_max
;
55 struct sockaddr_in addr
;
63 else if (max
) /* no minimum specified, bind from max down */
70 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", RADIUS_FDS
);
71 radfds
= calloc(sizeof(int), RADIUS_FDS
);
72 for (i
= 0; i
< RADIUS_FDS
; i
++)
75 radfds
[i
] = socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
76 flags
= fcntl(radfds
[i
], F_GETFL
, 0);
77 fcntl(radfds
[i
], F_SETFL
, flags
| O_NONBLOCK
);
83 memset(&addr
, 0, sizeof(addr
));
84 addr
.sin_family
= AF_INET
;
85 addr
.sin_addr
.s_addr
= INADDR_ANY
;
88 addr
.sin_port
= htons(port
);
89 if ((b
= bind(radfds
[i
], (struct sockaddr
*) &addr
, sizeof(addr
))) < 0)
91 if ((port
+= inc
) < min
|| port
> max
)
93 LOG(0, 0, 0, "Can't bind RADIUS socket in range %u-%u\n", min
, max
);
102 void radiusclear(uint16_t r
, sessionidt s
)
104 if (s
) sess_local
[s
].radius
= 0;
105 memset(&radius
[r
], 0, sizeof(radius
[r
])); // radius[r].state = RADIUSNULL;
108 static uint16_t get_free_radius()
111 static uint32_t next_radius_id
= 0;
113 for (count
= MAXRADIUS
; count
> 0; --count
)
115 ++next_radius_id
; // Find the next ID to check.
116 if (next_radius_id
>= MAXRADIUS
)
119 if (radius
[next_radius_id
].state
== RADIUSNULL
)
121 return next_radius_id
;
125 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
129 uint16_t radiusnew(sessionidt s
)
131 uint16_t r
= sess_local
[s
].radius
;
136 LOG(3, s
, session
[s
].tunnel
, "Re-used radius %d\n", r
);
140 if (!(r
= get_free_radius()))
142 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions\n");
143 STAT(radius_overflow
);
147 memset(&radius
[r
], 0, sizeof(radius
[r
]));
148 sess_local
[s
].radius
= r
;
149 radius
[r
].session
= s
;
150 radius
[r
].state
= RADIUSWAIT
;
151 radius
[r
].retry
= TIME
+ 1200; // Wait at least 120 seconds to re-claim this.
153 random_data(radius
[r
].auth
, sizeof(radius
[r
].auth
));
155 LOG(3, s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
159 // Send a RADIUS request
160 void radiussend(uint16_t r
, uint8_t state
)
162 struct sockaddr_in addr
;
163 uint8_t b
[4096]; // RADIUS packet
171 s
= radius
[r
].session
;
172 if (!config
->numradiusservers
)
174 LOG(0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
177 if (!*config
->radiussecret
)
179 LOG(0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
183 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
&& !config
->radius_accounting
)
185 // Radius accounting is turned off
190 if (radius
[r
].state
!= state
)
193 radius
[r
].state
= state
;
194 radius
[r
].retry
= backoff(radius
[r
].try++) + 20; // 3s, 4s, 6s, 10s...
195 LOG(4, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
196 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
197 radius_state(radius
[r
].state
), radius
[r
].try);
199 if (radius
[r
].try > config
->numradiusservers
* 2)
203 if (state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
)
204 sessionshutdown(s
, "RADIUS timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
207 LOG(1, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
208 radius_state(state
));
211 STAT(radius_timeout
);
215 STAT(radius_retries
);
216 radius
[r
].state
= RADIUSWAIT
;
217 radius
[r
].retry
= 100;
221 // contruct RADIUS access request
226 b
[0] = AccessRequest
; // access request
231 b
[0] = AccountingRequest
; // accounting request
234 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
236 b
[1] = r
>> RADIUS_SHIFT
; // identifier
237 memcpy(b
+ 4, radius
[r
].auth
, 16);
243 p
[1] = strlen(session
[s
].user
) + 2;
244 strcpy((char *) p
+ 2, session
[s
].user
);
247 if (state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
)
251 *p
= 3; // CHAP password
253 p
[2] = radius
[r
].id
; // ID
254 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
256 *p
= 60; // CHAP Challenge
258 memcpy(p
+ 2, radius
[r
].auth
, 16);
263 strcpy(pass
, radius
[r
].pass
);
266 pass
[pl
++] = 0; // pad
275 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
277 MD5_Update(&ctx
, pass
+ p
- 16, 16);
279 MD5_Update(&ctx
, radius
[r
].auth
, 16);
280 MD5_Final(hash
, &ctx
);
283 pass
[p
] ^= hash
[p
& 15];
292 memcpy(p
+ 2, pass
, pl
);
298 *p
= 40; // accounting type
300 *(uint32_t *) (p
+ 2) = htonl(state
- RADIUSSTART
+ 1); // start=1, stop=2, interim=3
304 *p
= 44; // session ID
306 sprintf((char *) p
+ 2, "%08X%08X", session
[s
].unique_id
, session
[s
].opened
);
308 if (state
== RADIUSSTART
)
312 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
314 sess_local
[s
].last_interim
= time_now
; // Setup "first" Interim
318 *p
= 42; // input octets
320 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
323 *p
= 43; // output octets
325 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
328 *p
= 46; // session time
330 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
333 *p
= 47; // input packets
335 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
338 *p
= 48; // output packets
340 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
343 *p
= 52; // input gigawords
345 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin_wrap
);
348 *p
= 53; // output gigawords
350 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout_wrap
);
353 if (state
== RADIUSSTOP
&& radius
[r
].term_cause
)
355 *p
= 49; // acct-terminate-cause
357 *(uint32_t *) (p
+ 2) = htonl(radius
[r
].term_cause
);
360 if (radius
[r
].term_msg
)
362 *p
= 26; // vendor-specific
363 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
364 p
[6] = 1; // Cisco-AVPair
365 p
[7] = 2 + sprintf((char *) p
+ 8, "disc-cause-ext=%s", radius
[r
].term_msg
);
372 if (session
[s
].classlen
) {
374 p
[1] = session
[s
].classlen
+ 2;
375 memcpy(p
+ 2, session
[s
].class, session
[s
].classlen
);
380 struct param_radius_account acct
= { &tunnel
[session
[s
].tunnel
], &session
[s
], &p
};
381 run_plugins(PLUGIN_RADIUS_ACCOUNT
, &acct
);
390 *(uint32_t *) (p
+ 2) = htonl(s
);
393 *p
= 6; // Service-Type
395 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 8 : 2)); // Authenticate only or Framed-User respectevily
398 *p
= 7; // Framed-Protocol
399 p
[1] = htonl((state
== RADIUSJUSTAUTH
? 0 : 6));
400 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 0 : 1)); // PPP
405 *p
= 8; // Framed-IP-Address
407 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
411 if (session
[s
].route
[0].ip
)
414 for (r
= 0; s
&& r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
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 session
[s
].route
[r
].prefixlen
,
420 fmtaddr(htonl(session
[s
].ip
), 1)) + 2;
426 if (session
[s
].session_timeout
)
428 *p
= 27; // Session-Timeout
430 *(uint32_t *) (p
+ 2) = htonl(session
[s
].session_timeout
);
434 if (session
[s
].idle_timeout
)
436 *p
= 28; // Idle-Timeout
438 *(uint32_t *) (p
+ 2) = htonl(session
[s
].idle_timeout
);
442 if (*session
[s
].called
)
445 p
[1] = strlen(session
[s
].called
) + 2;
446 strcpy((char *) p
+ 2, session
[s
].called
);
450 if (*session
[s
].calling
)
453 p
[1] = strlen(session
[s
].calling
) + 2;
454 strcpy((char *) p
+ 2, session
[s
].calling
);
462 *(uint32_t *)(p
+ 2) = config
->bind_address
;
466 *(uint16_t *) (b
+ 2) = htons(p
- b
);
467 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
)
469 // Build auth for accounting packet
470 calc_auth(b
, p
- b
, zero
, b
+ 4);
471 memcpy(radius
[r
].auth
, b
+ 4, 16);
473 memset(&addr
, 0, sizeof(addr
));
474 addr
.sin_family
= AF_INET
;
475 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
478 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
479 // assume RADIUS accounting port is the authentication port +1
480 addr
.sin_port
= htons((state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
) ? port
: port
+1);
483 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
484 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
487 static void handle_avpair(sessionidt s
, uint8_t *avp
, int len
)
490 uint8_t *value
= memchr(avp
, '=', len
);
491 uint8_t tmp
[2048] = "";
505 if (len
> 2 && (*value
== '"' || *value
== '\'') && value
[len
- 1] == *value
)
511 // copy and null terminate
512 else if (len
< sizeof(tmp
) - 1)
514 memcpy(tmp
, value
, len
);
523 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], (char *) key
, (char *) value
};
524 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
528 // process RADIUS response
529 void processrad(uint8_t *buf
, int len
, char socket_index
)
540 int OpentunnelReq
= 0;
545 LOG_HEX(5, "RADIUS Response", buf
, len
);
546 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
548 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
552 r_code
= buf
[0]; // response type
553 r_id
= buf
[1]; // radius reply indentifier.
555 len
= ntohs(*(uint16_t *) (buf
+ 2));
556 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
557 s
= radius
[r
].session
;
558 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
559 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
561 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
563 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
566 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSJUSTAUTH
&& radius
[r
].state
!= RADIUSSTART
567 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
569 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
572 t
= session
[s
].tunnel
;
573 calc_auth(buf
, len
, radius
[r
].auth
, hash
);
575 if (memcmp(hash
, buf
+ 4, 16))
577 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
578 return; // Do nothing. On timeout, it will try the next radius server.
581 if (((radius
[r
].state
== RADIUSAUTH
||radius
[r
].state
== RADIUSJUSTAUTH
) && r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
582 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
584 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
585 return; // We got something we didn't expect. Let the timeouts take
586 // care off finishing the radius session if that's really correct.
589 if (radius
[r
].state
== RADIUSAUTH
|| radius
[r
].state
== RADIUSJUSTAUTH
)
591 // run post-auth plugin
592 struct param_post_auth packet
= {
596 (r_code
== AccessAccept
),
597 radius
[r
].chap
? PPPCHAP
: PPPPAP
600 run_plugins(PLUGIN_POST_AUTH
, &packet
);
601 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
603 // process auth response
607 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
608 if (!p
) return; // Abort!
610 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
612 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
613 tunnelsend(b
, (p
- b
) + 4, t
); // send it
615 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
616 (r_code
== AccessAccept
) ? "allowed" : "denied");
621 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
622 if (!p
) return; // Abort!
627 *(uint16_t *) (p
+ 2) = ntohs(5);
628 p
[4] = 0; // no message
629 tunnelsend(b
, (p
- b
) + 5, t
); // send it
631 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
632 (r_code
== AccessAccept
) ? "allowed" : "denied");
635 if (r_code
== AccessAccept
)
638 // Extract IP, routes, etc
639 uint8_t *p
= buf
+ 20;
640 uint8_t *e
= buf
+ len
;
643 uint8_t strtemp
[256];
644 lac_reset_rad_tag_tunnel_ctxt();
646 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
648 if (*p
== 26 && p
[1] >= 7)
650 // Vendor-Specific Attribute
651 uint32_t vendor
= ntohl(*(int *)(p
+ 2));
652 uint8_t attrib
= *(p
+ 6);
653 int attrib_length
= *(p
+ 7) - 2;
655 LOG(4, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor
, attrib
, attrib_length
);
656 if (vendor
== 9 && attrib
== 1) // Cisco-AVPair
658 if (attrib_length
< 0) continue;
659 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
660 attrib_length
, p
+ 8);
662 handle_avpair(s
, p
+ 8, attrib_length
);
665 else if (vendor
== 529 && attrib
>= 135 && attrib
<= 136) // Ascend
667 // handle old-format ascend DNS attributes below
672 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
680 if (p
[1] < 6) continue;
681 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
682 session
[s
].ip_pool_index
= -1;
683 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
684 fmtaddr(htonl(session
[s
].ip
), 0));
686 if (session
[s
].ip
== 0xFFFFFFFE)
687 session
[s
].ip
= 0; // assign from pool
692 if (p
[1] < 6) continue;
693 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
694 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
695 fmtaddr(htonl(session
[s
].dns1
), 0));
700 if (p
[1] < 6) continue;
701 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
702 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
703 fmtaddr(htonl(session
[s
].dns2
), 0));
712 uint8_t *e
= p
+ p
[1];
713 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
721 u
= u
* 10 + *n
- '0';
728 while (n
< e
&& isdigit(*n
))
729 bits
= bits
* 10 + *n
++ - '0';
731 else if ((ip
>> 24) < 128)
733 else if ((ip
>> 24) < 192)
738 if (routes
== MAXROUTE
)
740 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
744 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%d\n",
745 fmtaddr(htonl(ip
), 0), bits
);
747 session
[s
].route
[routes
].ip
= ip
;
748 session
[s
].route
[routes
].prefixlen
= bits
;
755 char *filter
= (char *) p
+ 2;
761 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
762 if ((suffix
= memchr(filter
, '.', l
)))
764 int b
= suffix
- filter
;
765 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
766 fp
= &session
[s
].filter_in
;
767 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
768 fp
= &session
[s
].filter_out
;
775 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
779 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
781 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
786 ip_filters
[f
].used
++;
792 if (p
[1] < 6) continue;
793 session
[s
].session_timeout
= ntohl(*(uint32_t *)(p
+ 2));
794 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", session
[s
].session_timeout
);
795 if(!session
[s
].session_timeout
&& config
->kill_timedout_sessions
)
796 sessionshutdown(s
, "Session timeout is zero", CDN_ADMIN_DISC
, 0);
801 if (p
[1] < 6) continue;
802 session
[s
].idle_timeout
= ntohl(*(uint32_t *)(p
+ 2));
803 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", session
[s
].idle_timeout
);
811 uint8_t *e
= p
+ p
[1];
812 uint8_t *m
= memchr(n
, '/', e
- p
);
815 inet_pton(AF_INET6
, (char *) n
, &r6
);
818 while (m
< e
&& isdigit(*m
)) {
819 prefixlen
= prefixlen
* 10 + *m
++ - '0';
824 LOG(3, s
, session
[s
].tunnel
,
825 " Radius reply contains route for %s/%d\n",
827 session
[s
].ipv6route
= r6
;
828 session
[s
].ipv6prefixlen
= prefixlen
;
834 if (p
[1] < 3) continue;
835 session
[s
].classlen
= p
[1] - 2;
836 if (session
[s
].classlen
> MAXCLASS
)
837 session
[s
].classlen
= MAXCLASS
;
838 memcpy(session
[s
].class, p
+ 2, session
[s
].classlen
);
844 if (p
[1] != 6) continue;
846 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Type:%d %d\n",
847 tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
849 lac_set_rad_tag_tunnel_type(tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
850 /* Request open tunnel to remote LNS*/
855 // Tunnel-Medium-Type
856 if (p
[1] < 6) continue;
858 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Medium-Type:%d %d\n",
859 tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
861 lac_set_rad_tag_tunnel_medium_type(tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
865 // Tunnel-Server-Endpoint
866 if (p
[1] < 3) continue;
868 //If the Tag field is greater than 0x1F,
869 // it SHOULD be interpreted as the first byte of the following String field.
870 memset(strtemp
, 0, 256);
874 memcpy(strtemp
, (p
+ 2), p
[1]-2);
877 memcpy(strtemp
, (p
+ 3), p
[1]-3);
879 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Server-Endpoint:%d %s\n", tag
, strtemp
);
881 lac_set_rad_tag_tunnel_serv_endpt(tag
, (char *) strtemp
);
888 if (p
[1] < 5) continue;
891 memset(strtemp
, 0, 256);
893 memcpy(strtemp
, (p
+ 3), lentemp
);
894 if (!rad_tunnel_pwdecode(strtemp
, &lentemp
, config
->radiussecret
, radius
[r
].auth
))
896 LOG_HEX(3, "Error Decode Tunnel-Password, Dump Radius reponse:", p
, p
[1]);
900 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Password:%d %s\n", tag
, strtemp
);
901 if (strlen((char *) strtemp
) > 63)
903 LOG(1, s
, session
[s
].tunnel
, "tunnel password is too long (>63)\n");
907 lac_set_rad_tag_tunnel_password(tag
, (char *) strtemp
);
911 // Tunnel-Assignment-Id
912 if (p
[1] < 3) continue;
914 //If the Tag field is greater than 0x1F,
915 // it SHOULD be interpreted as the first byte of the following String field.
916 memset(strtemp
, 0, 256);
920 memcpy(strtemp
, (p
+ 2), p
[1]-2);
923 memcpy(strtemp
, (p
+ 3), p
[1]-3);
925 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Assignment-Id:%d %s\n", tag
, strtemp
);
927 lac_set_rad_tag_tunnel_assignment_id(tag
, (char *) strtemp
);
932 else if (r_code
== AccessReject
)
934 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
935 sessionkill(s
, "Authentication rejected");
940 if ((!config
->disable_lac_func
) && OpentunnelReq
)
942 char assignment_id
[256];
943 // Save radius tag context to conf
944 lac_save_rad_tag_tunnels(s
);
946 memset(assignment_id
, 0, 256);
947 if (!lac_rad_select_assignment_id(s
, assignment_id
))
948 break; // Error no assignment_id
950 LOG(3, s
, session
[s
].tunnel
, "Select Tunnel Remote LNS for assignment_id == %s\n", assignment_id
);
952 if (lac_rad_forwardtoremotelns(s
, assignment_id
, session
[s
].user
))
955 // Sanity check, no local IP to session forwarded
957 for (ro
= 0; r
< MAXROUTE
&& session
[s
].route
[ro
].ip
; r
++)
959 session
[s
].route
[ro
].ip
= 0;
962 // Restart LCP auth...
969 if (!session
[s
].dns1
&& config
->default_dns1
)
971 session
[s
].dns1
= ntohl(config
->default_dns1
);
972 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
974 if (!session
[s
].dns2
&& config
->default_dns2
)
976 session
[s
].dns2
= ntohl(config
->default_dns2
);
977 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
980 // Valid Session, set it up
981 session
[s
].unique_id
= 0;
986 // An ack for a stop or start record.
987 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
992 // finished with RADIUS
996 // Send a retry for RADIUS/CHAP message
997 void radiusretry(uint16_t r
)
999 sessionidt s
= radius
[r
].session
;
1004 if (s
) t
= session
[s
].tunnel
;
1006 switch (radius
[r
].state
)
1008 case RADIUSCHAP
: // sending CHAP down PPP
1011 case RADIUSAUTH
: // sending auth to RADIUS server
1012 case RADIUSJUSTAUTH
: // sending auth to RADIUS server
1013 case RADIUSSTART
: // sending start accounting to RADIUS server
1014 case RADIUSSTOP
: // sending stop accounting to RADIUS server
1015 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
1016 radiussend(r
, radius
[r
].state
);
1019 case RADIUSNULL
: // Not in use
1020 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
1021 // free up RADIUS task
1023 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
1030 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
1032 int i
, r_code
, r_id
, length
, attribute_length
;
1033 uint8_t *packet
, attribute
;
1035 char username
[MAXUSER
] = "";
1044 uint8_t *avpair
[64];
1045 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
1050 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
1051 LOG_HEX(5, "DAE Request", buf
, len
);
1053 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
1055 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
1059 r_code
= buf
[0]; // request type
1060 r_id
= buf
[1]; // radius indentifier.
1062 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
1064 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
1068 if (!config
->cluster_iam_master
)
1070 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1074 len
= ntohs(*(uint16_t *) (buf
+ 2));
1076 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
1078 // check authenticator
1079 calc_auth(buf
, len
, zero
, hash
);
1080 if (memcmp(hash
, buf
+ 4, 16) != 0)
1082 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
1086 // unpack attributes
1092 attribute
= *packet
++;
1093 attribute_length
= *packet
++;
1094 if (attribute_length
< 2)
1097 length
-= attribute_length
;
1098 attribute_length
-= 2;
1101 case 1: /* username */
1102 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
1103 memcpy(username
, packet
, len
);
1105 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
1108 case 4: /* nas ip address */
1109 nas
= *(uint32_t *) packet
; // net order
1110 if (nas
!= config
->bind_address
)
1111 error
= 403; // NAS identification mismatch
1113 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
1116 case 5: /* nas port */
1117 port
= ntohl(*(uint32_t *) packet
);
1118 if (port
< 1 || port
> MAXSESSION
)
1121 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
1124 case 6: /* service type */
1126 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
1127 auth_only
= service_type
== 8; // Authenticate only
1129 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
1133 case 8: /* ip address */
1134 ip
= *(uint32_t *) packet
; // net order
1135 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
1138 case 11: /* filter id */
1139 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
1140 if (!(p
= memchr(packet
, '.', attribute_length
)))
1142 error
= 404; // invalid request
1147 i
= find_filter((char *) packet
, len
);
1148 if (i
< 0 || !*ip_filters
[i
].name
)
1154 if (!memcmp(p
, ".in", attribute_length
- len
))
1156 else if (!memcmp(p
, ".out", attribute_length
- len
))
1163 case 26: /* vendor specific */
1164 if (attribute_length
>= 6
1165 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1166 && *(packet
+ 4) == 1 // Cisco-AVPair
1167 && *(packet
+ 5) >= 2) // length
1169 int len
= *(packet
+ 5) - 2;
1170 uint8_t *a
= packet
+ 6;
1172 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1173 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1176 avpair_len
[avp
++] = len
;
1182 packet
+= attribute_length
;
1185 if (!error
&& auth_only
)
1187 if (fin
!= -1 || fout
!= -1 || avp
)
1188 error
= 401; // unsupported attribute
1190 error
= 405; // unsupported service
1193 if (!error
&& !(port
|| ip
|| *username
))
1194 error
= 402; // missing attribute
1196 // exact match for SID if given
1200 if (!session
[s
].opened
)
1201 error
= 503; // not found
1206 // find/check session by IP
1207 i
= sessionbyip(ip
);
1208 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1214 if (!error
&& *username
)
1218 if (strcmp(session
[s
].user
, username
))
1221 else if (!(s
= sessionbyuser(username
)))
1225 t
= session
[s
].tunnel
;
1229 case DisconnectRequest
: // Packet of Disconnect/Death
1232 r_code
= DisconnectNAK
;
1236 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1237 r_code
= DisconnectACK
;
1239 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1242 case CoARequest
: // Change of Authorization
1249 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1254 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1255 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1262 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1267 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1269 filter_session(s
, fin
, fout
);
1271 // process cisco av-pair(s)
1272 for (i
= 0; i
< avp
; i
++)
1274 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1275 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1278 cluster_send_session(s
);
1296 *(uint32_t *) packet
= htonl(error
);
1300 *((uint16_t *)(buf
+ 2)) = htons(len
);
1303 calc_auth(buf
, len
, hash
, buf
+ 4);
1305 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1307 // send DAE response
1308 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1309 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));
1313 // Decrypte the encrypted Tunnel Password.
1314 // Defined in RFC-2868.
1315 // the pl2tpsecret buffer must set to 256 characters.
1316 // return 0 on decoding error else length of decoded l2tpsecret
1317 int rad_tunnel_pwdecode(uint8_t *pl2tpsecret
, size_t *pl2tpsecretlen
,
1318 const char *radiussecret
, const uint8_t * auth
)
1320 MD5_CTX ctx
, oldctx
;
1323 unsigned i
, n
, len
, decodedlen
;
1325 /* 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 6 7
1326 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1327 | Salt | Salt | String ..........
1328 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*/
1330 len
= *pl2tpsecretlen
;
1334 LOG(1, 0, 0, "tunnel password is too short, We need at least a salt\n");
1341 *pl2tpsecretlen
= 0;
1342 LOG(1, 0, 0, "tunnel passwd is empty !!!\n");
1346 len
-= 2; /* discount the salt */
1348 //Use the secret to setup the decryption
1349 secretlen
= strlen(radiussecret
);
1352 MD5_Update(&ctx
, (void *) radiussecret
, secretlen
);
1353 oldctx
= ctx
; /* save intermediate work */
1355 // Set up the initial key:
1356 // b(1) = MD5(radiussecret + auth + salt)
1357 MD5_Update(&ctx
, (void *) auth
, 16);
1358 MD5_Update(&ctx
, pl2tpsecret
, 2);
1361 for (n
= 0; n
< len
; n
+= 16)
1367 MD5_Final(hash
, &ctx
);
1371 // the first octet, it's the 'data_len'
1373 decodedlen
= pl2tpsecret
[2] ^ hash
[0];
1374 if (decodedlen
>= len
)
1376 LOG(1, 0, 0, "tunnel password is too long !!!\n");
1380 MD5_Update(&ctx
, pl2tpsecret
+ 2, 16);
1384 MD5_Final(hash
, &ctx
);
1387 MD5_Update(&ctx
, pl2tpsecret
+ n
+ 2, 16);
1390 for (i
= base
; i
< 16; i
++)
1392 pl2tpsecret
[n
+ i
- 1] = pl2tpsecret
[n
+ i
+ 2] ^ hash
[i
];
1396 if (decodedlen
> 239) decodedlen
= 239;
1398 *pl2tpsecretlen
= decodedlen
;
1399 pl2tpsecret
[decodedlen
] = 0;