6 #include <sys/socket.h>
10 #include <arpa/inet.h>
12 #include <netinet/in.h>
16 #include "constants.h"
25 extern radiust
*radius
;
26 extern sessiont
*session
;
27 extern tunnelt
*tunnel
;
28 extern configt
*config
;
30 extern ip_filtert
*ip_filters
;
32 static const hasht zero
;
34 static void calc_auth(const void *buf
, size_t len
, const uint8_t *in
, uint8_t *out
)
39 MD5_Update(&ctx
, (void *)buf
, 4); // code, id, length
40 MD5_Update(&ctx
, (void *)in
, 16); // auth
41 MD5_Update(&ctx
, (void *)(buf
+ 20), len
- 20);
42 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
46 // Set up socket for radius requests
51 uint16_t min
= config
->radius_bind_min
;
52 uint16_t max
= config
->radius_bind_max
;
54 struct sockaddr_in addr
;
62 else if (max
) /* no minimum specified, bind from max down */
69 LOG(3, 0, 0, "Creating %d sockets for RADIUS queries\n", RADIUS_FDS
);
70 radfds
= calloc(sizeof(int), RADIUS_FDS
);
71 for (i
= 0; i
< RADIUS_FDS
; i
++)
74 radfds
[i
] = socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
75 flags
= fcntl(radfds
[i
], F_GETFL
, 0);
76 fcntl(radfds
[i
], F_SETFL
, flags
| O_NONBLOCK
);
82 memset(&addr
, 0, sizeof(addr
));
83 addr
.sin_family
= AF_INET
;
84 addr
.sin_addr
.s_addr
= INADDR_ANY
;
87 addr
.sin_port
= htons(port
);
88 if ((b
= bind(radfds
[i
], (struct sockaddr
*) &addr
, sizeof(addr
))) < 0)
90 if ((port
+= inc
) < min
|| port
> max
)
92 LOG(0, 0, 0, "Can't bind RADIUS socket in range %u-%u\n", min
, max
);
101 void radiusclear(uint16_t r
, sessionidt s
)
103 if (s
) sess_local
[s
].radius
= 0;
104 memset(&radius
[r
], 0, sizeof(radius
[r
])); // radius[r].state = RADIUSNULL;
107 static uint16_t get_free_radius()
110 static uint32_t next_radius_id
= 0;
112 for (count
= MAXRADIUS
; count
> 0; --count
)
114 ++next_radius_id
; // Find the next ID to check.
115 if (next_radius_id
>= MAXRADIUS
)
118 if (radius
[next_radius_id
].state
== RADIUSNULL
)
120 return next_radius_id
;
124 LOG(0, 0, 0, "Can't find a free radius session! This is very bad!\n");
128 uint16_t radiusnew(sessionidt s
)
130 uint16_t r
= sess_local
[s
].radius
;
135 LOG(3, s
, session
[s
].tunnel
, "Re-used radius %d\n", r
);
139 if (!(r
= get_free_radius()))
141 LOG(1, s
, session
[s
].tunnel
, "No free RADIUS sessions\n");
142 STAT(radius_overflow
);
146 memset(&radius
[r
], 0, sizeof(radius
[r
]));
147 sess_local
[s
].radius
= r
;
148 radius
[r
].session
= s
;
149 radius
[r
].state
= RADIUSWAIT
;
150 radius
[r
].retry
= TIME
+ 1200; // Wait at least 120 seconds to re-claim this.
152 random_data(radius
[r
].auth
, sizeof(radius
[r
].auth
));
154 LOG(3, s
, session
[s
].tunnel
, "Allocated radius %d\n", r
);
158 // Send a RADIUS request
159 void radiussend(uint16_t r
, uint8_t state
)
161 struct sockaddr_in addr
;
162 uint8_t b
[4096]; // RADIUS packet
170 s
= radius
[r
].session
;
171 if (!config
->numradiusservers
)
173 LOG(0, s
, session
[s
].tunnel
, "No RADIUS servers\n");
176 if (!*config
->radiussecret
)
178 LOG(0, s
, session
[s
].tunnel
, "No RADIUS secret\n");
182 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
&& !config
->radius_accounting
)
184 // Radius accounting is turned off
189 if (radius
[r
].state
!= state
)
192 radius
[r
].state
= state
;
193 radius
[r
].retry
= backoff(radius
[r
].try++) + 20; // 3s, 4s, 6s, 10s...
194 LOG(4, s
, session
[s
].tunnel
, "Send RADIUS id %d sock %d state %s try %d\n",
195 r
>> RADIUS_SHIFT
, r
& RADIUS_MASK
,
196 radius_state(radius
[r
].state
), radius
[r
].try);
198 if (radius
[r
].try > config
->numradiusservers
* 2)
202 if (state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
)
203 sessionshutdown(s
, "RADIUS timeout.", CDN_ADMIN_DISC
, TERM_REAUTHENTICATION_FAILURE
);
206 LOG(1, s
, session
[s
].tunnel
, "RADIUS timeout, but in state %s so don't timeout session\n",
207 radius_state(state
));
210 STAT(radius_timeout
);
214 STAT(radius_retries
);
215 radius
[r
].state
= RADIUSWAIT
;
216 radius
[r
].retry
= 100;
220 // contruct RADIUS access request
225 b
[0] = AccessRequest
; // access request
230 b
[0] = AccountingRequest
; // accounting request
233 LOG(0, 0, 0, "Unknown radius state %d\n", state
);
235 b
[1] = r
>> RADIUS_SHIFT
; // identifier
236 memcpy(b
+ 4, radius
[r
].auth
, 16);
242 p
[1] = strlen(session
[s
].user
) + 2;
243 strcpy((char *) p
+ 2, session
[s
].user
);
246 if (state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
)
250 *p
= 3; // CHAP password
252 p
[2] = radius
[r
].id
; // ID
253 memcpy(p
+ 3, radius
[r
].pass
, 16); // response from CHAP request
255 *p
= 60; // CHAP Challenge
257 memcpy(p
+ 2, radius
[r
].auth
, 16);
262 strcpy(pass
, radius
[r
].pass
);
265 pass
[pl
++] = 0; // pad
274 MD5_Update(&ctx
, config
->radiussecret
, strlen(config
->radiussecret
));
276 MD5_Update(&ctx
, pass
+ p
- 16, 16);
278 MD5_Update(&ctx
, radius
[r
].auth
, 16);
279 MD5_Final(hash
, &ctx
);
282 pass
[p
] ^= hash
[p
& 15];
291 memcpy(p
+ 2, pass
, pl
);
297 *p
= 40; // accounting type
299 *(uint32_t *) (p
+ 2) = htonl(state
- RADIUSSTART
+ 1); // start=1, stop=2, interim=3
303 *p
= 44; // session ID
305 sprintf((char *) p
+ 2, "%08X%08X", session
[s
].unique_id
, session
[s
].opened
);
307 if (state
== RADIUSSTART
)
311 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
313 sess_local
[s
].last_interim
= time_now
; // Setup "first" Interim
317 *p
= 42; // input octets
319 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin
);
322 *p
= 43; // output octets
324 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout
);
327 *p
= 46; // session time
329 *(uint32_t *) (p
+ 2) = htonl(time(NULL
) - session
[s
].opened
);
332 *p
= 47; // input packets
334 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pin
);
337 *p
= 48; // output packets
339 *(uint32_t *) (p
+ 2) = htonl(session
[s
].pout
);
342 *p
= 52; // input gigawords
344 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cin_wrap
);
347 *p
= 53; // output gigawords
349 *(uint32_t *) (p
+ 2) = htonl(session
[s
].cout_wrap
);
352 if (state
== RADIUSSTOP
&& radius
[r
].term_cause
)
354 *p
= 49; // acct-terminate-cause
356 *(uint32_t *) (p
+ 2) = htonl(radius
[r
].term_cause
);
359 if (radius
[r
].term_msg
)
361 *p
= 26; // vendor-specific
362 *(uint32_t *) (p
+ 2) = htonl(9); // Cisco
363 p
[6] = 1; // Cisco-AVPair
364 p
[7] = 2 + sprintf((char *) p
+ 8, "disc-cause-ext=%s", radius
[r
].term_msg
);
371 if (session
[s
].classlen
) {
373 p
[1] = session
[s
].classlen
+ 2;
374 memcpy(p
+ 2, session
[s
].class, session
[s
].classlen
);
379 struct param_radius_account acct
= { &tunnel
[session
[s
].tunnel
], &session
[s
], &p
};
380 run_plugins(PLUGIN_RADIUS_ACCOUNT
, &acct
);
389 *(uint32_t *) (p
+ 2) = htonl(s
);
392 *p
= 6; // Service-Type
394 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 8 : 2)); // Authenticate only or Framed-User respectevily
397 *p
= 7; // Framed-Protocol
398 p
[1] = htonl((state
== RADIUSJUSTAUTH
? 0 : 6));
399 *(uint32_t *) (p
+ 2) = htonl((state
== RADIUSJUSTAUTH
? 0 : 1)); // PPP
404 *p
= 8; // Framed-IP-Address
406 *(uint32_t *) (p
+ 2) = htonl(session
[s
].ip
);
410 if (session
[s
].route
[0].ip
)
413 for (r
= 0; s
&& r
< MAXROUTE
&& session
[s
].route
[r
].ip
; r
++)
415 *p
= 22; // Framed-Route
416 p
[1] = sprintf((char *) p
+ 2, "%s/%d %s 1",
417 fmtaddr(htonl(session
[s
].route
[r
].ip
), 0),
418 session
[s
].route
[r
].prefixlen
,
419 fmtaddr(htonl(session
[s
].ip
), 1)) + 2;
425 if (session
[s
].session_timeout
)
427 *p
= 27; // Session-Timeout
429 *(uint32_t *) (p
+ 2) = htonl(session
[s
].session_timeout
);
433 if (session
[s
].idle_timeout
)
435 *p
= 28; // Idle-Timeout
437 *(uint32_t *) (p
+ 2) = htonl(session
[s
].idle_timeout
);
441 if (*session
[s
].called
)
444 p
[1] = strlen(session
[s
].called
) + 2;
445 strcpy((char *) p
+ 2, session
[s
].called
);
449 if (*session
[s
].calling
)
452 p
[1] = strlen(session
[s
].calling
) + 2;
453 strcpy((char *) p
+ 2, session
[s
].calling
);
461 *(uint32_t *)(p
+ 2) = config
->bind_address
;
465 *(uint16_t *) (b
+ 2) = htons(p
- b
);
466 if (state
!= RADIUSAUTH
&& state
!= RADIUSJUSTAUTH
)
468 // Build auth for accounting packet
469 calc_auth(b
, p
- b
, zero
, b
+ 4);
470 memcpy(radius
[r
].auth
, b
+ 4, 16);
472 memset(&addr
, 0, sizeof(addr
));
473 addr
.sin_family
= AF_INET
;
474 *(uint32_t *) & addr
.sin_addr
= config
->radiusserver
[(radius
[r
].try - 1) % config
->numradiusservers
];
477 uint16_t port
= config
->radiusport
[(radius
[r
].try - 1) % config
->numradiusservers
];
478 // assume RADIUS accounting port is the authentication port +1
479 addr
.sin_port
= htons((state
== RADIUSAUTH
|| state
== RADIUSJUSTAUTH
) ? port
: port
+1);
482 LOG_HEX(5, "RADIUS Send", b
, (p
- b
));
483 sendto(radfds
[r
& RADIUS_MASK
], b
, p
- b
, 0, (void *) &addr
, sizeof(addr
));
486 static void handle_avpair(sessionidt s
, uint8_t *avp
, int len
)
489 uint8_t *value
= memchr(avp
, '=', len
);
490 uint8_t tmp
[2048] = "";
504 if (len
> 2 && (*value
== '"' || *value
== '\'') && value
[len
- 1] == *value
)
510 // copy and null terminate
511 else if (len
< sizeof(tmp
) - 1)
513 memcpy(tmp
, value
, len
);
522 struct param_radius_response p
= { &tunnel
[session
[s
].tunnel
], &session
[s
], (char *) key
, (char *) value
};
523 run_plugins(PLUGIN_RADIUS_RESPONSE
, &p
);
527 // process RADIUS response
528 void processrad(uint8_t *buf
, int len
, char socket_index
)
538 int OpentunnelReq
= 0;
542 LOG_HEX(5, "RADIUS Response", buf
, len
);
543 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
545 LOG(1, 0, 0, "Duff RADIUS response length %d\n", len
);
549 r_code
= buf
[0]; // response type
550 r_id
= buf
[1]; // radius reply indentifier.
552 len
= ntohs(*(uint16_t *) (buf
+ 2));
553 r
= socket_index
| (r_id
<< RADIUS_SHIFT
);
554 s
= radius
[r
].session
;
555 LOG(3, s
, session
[s
].tunnel
, "Received %s, radius %d response for session %u (%s, id %d)\n",
556 radius_state(radius
[r
].state
), r
, s
, radius_code(r_code
), r_id
);
558 if (!s
&& radius
[r
].state
!= RADIUSSTOP
)
560 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
563 if (radius
[r
].state
!= RADIUSAUTH
&& radius
[r
].state
!= RADIUSJUSTAUTH
&& radius
[r
].state
!= RADIUSSTART
564 && radius
[r
].state
!= RADIUSSTOP
&& radius
[r
].state
!= RADIUSINTERIM
)
566 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response\n");
569 t
= session
[s
].tunnel
;
570 calc_auth(buf
, len
, radius
[r
].auth
, hash
);
572 if (memcmp(hash
, buf
+ 4, 16))
574 LOG(0, s
, session
[s
].tunnel
, " Incorrect auth on RADIUS response!! (wrong secret in radius config?)\n");
575 return; // Do nothing. On timeout, it will try the next radius server.
578 if (((radius
[r
].state
== RADIUSAUTH
||radius
[r
].state
== RADIUSJUSTAUTH
) && r_code
!= AccessAccept
&& r_code
!= AccessReject
) ||
579 ((radius
[r
].state
== RADIUSSTART
|| radius
[r
].state
== RADIUSSTOP
|| radius
[r
].state
== RADIUSINTERIM
) && r_code
!= AccountingResponse
))
581 LOG(1, s
, session
[s
].tunnel
, " Unexpected RADIUS response %s\n", radius_code(r_code
));
582 return; // We got something we didn't expect. Let the timeouts take
583 // care off finishing the radius session if that's really correct.
586 if (radius
[r
].state
== RADIUSAUTH
|| radius
[r
].state
== RADIUSJUSTAUTH
)
588 // run post-auth plugin
589 struct param_post_auth packet
= {
593 (r_code
== AccessAccept
),
594 radius
[r
].chap
? PPPCHAP
: PPPPAP
597 run_plugins(PLUGIN_POST_AUTH
, &packet
);
598 r_code
= packet
.auth_allowed
? AccessAccept
: AccessReject
;
600 if (r_code
== AccessAccept
)
603 // Extract IP, routes, etc
604 uint8_t *p
= buf
+ 20;
605 uint8_t *e
= buf
+ len
;
607 uint8_t strtemp
[256];
608 lac_reset_rad_tag_tunnel_ctxt();
610 for (; p
+ 2 <= e
&& p
[1] && p
+ p
[1] <= e
; p
+= p
[1])
612 if (*p
== 26 && p
[1] >= 7)
614 // Vendor-Specific Attribute
615 uint32_t vendor
= ntohl(*(int *)(p
+ 2));
616 uint8_t attrib
= *(p
+ 6);
617 int attrib_length
= *(p
+ 7) - 2;
619 LOG(4, s
, session
[s
].tunnel
, " Radius reply contains Vendor-Specific. Vendor=%u Attrib=%u Length=%d\n", vendor
, attrib
, attrib_length
);
620 if (vendor
== 9 && attrib
== 1) // Cisco-AVPair
622 if (attrib_length
< 0) continue;
623 LOG(3, s
, session
[s
].tunnel
, " Cisco-AVPair value: %.*s\n",
624 attrib_length
, p
+ 8);
626 handle_avpair(s
, p
+ 8, attrib_length
);
629 else if (vendor
== 529 && attrib
>= 135 && attrib
<= 136) // Ascend
631 // handle old-format ascend DNS attributes below
636 LOG(3, s
, session
[s
].tunnel
, " Unknown vendor-specific\n");
644 if (p
[1] < 6) continue;
645 session
[s
].ip
= ntohl(*(uint32_t *) (p
+ 2));
646 session
[s
].ip_pool_index
= -1;
647 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains IP address %s\n",
648 fmtaddr(htonl(session
[s
].ip
), 0));
650 if (session
[s
].ip
== 0xFFFFFFFE)
651 session
[s
].ip
= 0; // assign from pool
656 if (p
[1] < 6) continue;
657 session
[s
].dns1
= ntohl(*(uint32_t *) (p
+ 2));
658 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains primary DNS address %s\n",
659 fmtaddr(htonl(session
[s
].dns1
), 0));
664 if (p
[1] < 6) continue;
665 session
[s
].dns2
= ntohl(*(uint32_t *) (p
+ 2));
666 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains secondary DNS address %s\n",
667 fmtaddr(htonl(session
[s
].dns2
), 0));
676 uint8_t *e
= p
+ p
[1];
677 while (n
< e
&& (isdigit(*n
) || *n
== '.'))
685 u
= u
* 10 + *n
- '0';
692 while (n
< e
&& isdigit(*n
))
693 bits
= bits
* 10 + *n
++ - '0';
695 else if ((ip
>> 24) < 128)
697 else if ((ip
>> 24) < 192)
702 if (routes
== MAXROUTE
)
704 LOG(1, s
, session
[s
].tunnel
, " Too many routes\n");
708 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains route for %s/%d\n",
709 fmtaddr(htonl(ip
), 0), bits
);
711 session
[s
].route
[routes
].ip
= ip
;
712 session
[s
].route
[routes
].prefixlen
= bits
;
719 char *filter
= (char *) p
+ 2;
725 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Filter-Id \"%.*s\"\n", l
, filter
);
726 if ((suffix
= memchr(filter
, '.', l
)))
728 int b
= suffix
- filter
;
729 if (l
- b
== 3 && !memcmp("in", suffix
+1, 2))
730 fp
= &session
[s
].filter_in
;
731 else if (l
- b
== 4 && !memcmp("out", suffix
+1, 3))
732 fp
= &session
[s
].filter_out
;
739 LOG(3, s
, session
[s
].tunnel
, " Invalid filter\n");
743 if ((f
= find_filter(filter
, l
)) < 0 || !*ip_filters
[f
].name
)
745 LOG(3, s
, session
[s
].tunnel
, " Unknown filter\n");
750 ip_filters
[f
].used
++;
756 if (p
[1] < 6) continue;
757 session
[s
].session_timeout
= ntohl(*(uint32_t *)(p
+ 2));
758 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Session-Timeout = %u\n", session
[s
].session_timeout
);
759 if(!session
[s
].session_timeout
&& config
->kill_timedout_sessions
)
760 sessionshutdown(s
, "Session timeout is zero", CDN_ADMIN_DISC
, 0);
765 if (p
[1] < 6) continue;
766 session
[s
].idle_timeout
= ntohl(*(uint32_t *)(p
+ 2));
767 LOG(3, s
, session
[s
].tunnel
, " Radius reply contains Idle-Timeout = %u\n", session
[s
].idle_timeout
);
775 uint8_t *e
= p
+ p
[1];
776 uint8_t *m
= memchr(n
, '/', e
- p
);
779 inet_pton(AF_INET6
, (char *) n
, &r6
);
782 while (m
< e
&& isdigit(*m
)) {
783 prefixlen
= prefixlen
* 10 + *m
++ - '0';
788 LOG(3, s
, session
[s
].tunnel
,
789 " Radius reply contains route for %s/%d\n",
791 session
[s
].ipv6route
= r6
;
792 session
[s
].ipv6prefixlen
= prefixlen
;
798 if (p
[1] < 3) continue;
799 session
[s
].classlen
= p
[1] - 2;
800 if (session
[s
].classlen
> MAXCLASS
)
801 session
[s
].classlen
= MAXCLASS
;
802 memcpy(session
[s
].class, p
+ 2, session
[s
].classlen
);
807 if (p
[1] != 6) continue;
809 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Type:%d %d\n",
810 tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
812 lac_set_rad_tag_tunnel_type(tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
813 /* Request open tunnel to remote LNS*/
818 // Tunnel-Medium-Type
819 if (p
[1] < 6) continue;
821 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Medium-Type:%d %d\n",
822 tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
824 lac_set_rad_tag_tunnel_medium_type(tag
, ntohl(*(uint32_t *)(p
+ 2)) & 0xFFFFFF);
828 // Tunnel-Server-Endpoint
829 if (p
[1] < 3) continue;
831 //If the Tag field is greater than 0x1F,
832 // it SHOULD be interpreted as the first byte of the following String field.
833 memset(strtemp
, 0, 256);
837 memcpy(strtemp
, (p
+ 2), p
[1]-2);
840 memcpy(strtemp
, (p
+ 3), p
[1]-3);
842 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Server-Endpoint:%d %s\n", tag
, strtemp
);
844 lac_set_rad_tag_tunnel_serv_endpt(tag
, (char *) strtemp
);
851 if (p
[1] < 5) continue;
854 memset(strtemp
, 0, 256);
856 memcpy(strtemp
, (p
+ 3), lentemp
);
857 if (!rad_tunnel_pwdecode(strtemp
, &lentemp
, config
->radiussecret
, radius
[r
].auth
))
859 LOG_HEX(3, "Error Decode Tunnel-Password, Dump Radius reponse:", p
, p
[1]);
863 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Password:%d %s\n", tag
, strtemp
);
864 if (strlen((char *) strtemp
) > 63)
866 LOG(1, s
, session
[s
].tunnel
, "tunnel password is too long (>63)\n");
870 lac_set_rad_tag_tunnel_password(tag
, (char *) strtemp
);
874 // Tunnel-Assignment-Id
875 if (p
[1] < 3) continue;
877 //If the Tag field is greater than 0x1F,
878 // it SHOULD be interpreted as the first byte of the following String field.
879 memset(strtemp
, 0, 256);
883 memcpy(strtemp
, (p
+ 2), p
[1]-2);
886 memcpy(strtemp
, (p
+ 3), p
[1]-3);
888 LOG(3, s
, session
[s
].tunnel
, " Radius reply Tunnel-Assignment-Id:%d %s\n", tag
, strtemp
);
890 lac_set_rad_tag_tunnel_assignment_id(tag
, (char *) strtemp
);
894 else if (r_code
== AccessReject
)
896 LOG(2, s
, session
[s
].tunnel
, " Authentication rejected for %s\n", session
[s
].user
);
897 sessionkill(s
, "Authentication rejected");
901 if ((!config
->disable_lac_func
) && OpentunnelReq
)
903 char assignment_id
[256];
904 // Save radius tag context to conf
905 lac_save_rad_tag_tunnels(s
);
907 memset(assignment_id
, 0, 256);
908 if (!lac_rad_select_assignment_id(s
, assignment_id
))
909 break; // Error no assignment_id
911 LOG(3, s
, session
[s
].tunnel
, "Select Tunnel Remote LNS for assignment_id == %s\n", assignment_id
);
913 if (lac_rad_forwardtoremotelns(s
, assignment_id
, session
[s
].user
))
916 // Sanity check, no local IP to session forwarded
918 for (ro
= 0; r
< MAXROUTE
&& session
[s
].route
[ro
].ip
; r
++)
920 session
[s
].route
[ro
].ip
= 0;
926 // process auth response
930 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPCHAP
, 0, 0, 0);
931 if (!p
) return; // Abort!
933 *p
= (r_code
== AccessAccept
) ? 3 : 4; // ack/nak
935 *(uint16_t *) (p
+ 2) = ntohs(4); // no message
936 tunnelsend(b
, (p
- b
) + 4, t
); // send it
938 LOG(3, s
, session
[s
].tunnel
, " CHAP User %s authentication %s.\n", session
[s
].user
,
939 (r_code
== AccessAccept
) ? "allowed" : "denied");
944 uint8_t *p
= makeppp(b
, sizeof(b
), 0, 0, s
, t
, PPPPAP
, 0, 0, 0);
945 if (!p
) return; // Abort!
950 *(uint16_t *) (p
+ 2) = ntohs(5);
951 p
[4] = 0; // no message
952 tunnelsend(b
, (p
- b
) + 5, t
); // send it
954 LOG(3, s
, session
[s
].tunnel
, " PAP User %s authentication %s.\n", session
[s
].user
,
955 (r_code
== AccessAccept
) ? "allowed" : "denied");
958 if (!session
[s
].dns1
&& config
->default_dns1
)
960 session
[s
].dns1
= ntohl(config
->default_dns1
);
961 LOG(3, s
, t
, " Sending dns1 = %s\n", fmtaddr(config
->default_dns1
, 0));
963 if (!session
[s
].dns2
&& config
->default_dns2
)
965 session
[s
].dns2
= ntohl(config
->default_dns2
);
966 LOG(3, s
, t
, " Sending dns2 = %s\n", fmtaddr(config
->default_dns2
, 0));
969 // Valid Session, set it up
970 session
[s
].unique_id
= 0;
975 // An ack for a stop or start record.
976 LOG(3, s
, t
, " RADIUS accounting ack recv in state %s\n", radius_state(radius
[r
].state
));
981 // finished with RADIUS
985 // Send a retry for RADIUS/CHAP message
986 void radiusretry(uint16_t r
)
988 sessionidt s
= radius
[r
].session
;
993 if (s
) t
= session
[s
].tunnel
;
995 switch (radius
[r
].state
)
997 case RADIUSCHAP
: // sending CHAP down PPP
1000 case RADIUSAUTH
: // sending auth to RADIUS server
1001 case RADIUSJUSTAUTH
: // sending auth to RADIUS server
1002 case RADIUSSTART
: // sending start accounting to RADIUS server
1003 case RADIUSSTOP
: // sending stop accounting to RADIUS server
1004 case RADIUSINTERIM
: // sending interim accounting to RADIUS server
1005 radiussend(r
, radius
[r
].state
);
1008 case RADIUSNULL
: // Not in use
1009 case RADIUSWAIT
: // waiting timeout before available, in case delayed reply from RADIUS server
1010 // free up RADIUS task
1012 LOG(3, s
, session
[s
].tunnel
, "Freeing up radius session %d\n", r
);
1019 void processdae(uint8_t *buf
, int len
, struct sockaddr_in
*addr
, int alen
, struct in_addr
*local
)
1021 int i
, r_code
, r_id
, length
, attribute_length
;
1022 uint8_t *packet
, attribute
;
1024 char username
[MAXUSER
] = "";
1033 uint8_t *avpair
[64];
1034 int avpair_len
[sizeof(avpair
)/sizeof(*avpair
)];
1039 LOG(3, 0, 0, "DAE request from %s\n", fmtaddr(addr
->sin_addr
.s_addr
, 0));
1040 LOG_HEX(5, "DAE Request", buf
, len
);
1042 if (len
< 20 || len
< ntohs(*(uint16_t *) (buf
+ 2)))
1044 LOG(1, 0, 0, "Duff DAE request length %d\n", len
);
1048 r_code
= buf
[0]; // request type
1049 r_id
= buf
[1]; // radius indentifier.
1051 if (r_code
!= DisconnectRequest
&& r_code
!= CoARequest
)
1053 LOG(1, 0, 0, "Unrecognised DAE request %s\n", radius_code(r_code
));
1057 if (!config
->cluster_iam_master
)
1059 master_forward_dae_packet(buf
, len
, addr
->sin_addr
.s_addr
, addr
->sin_port
);
1063 len
= ntohs(*(uint16_t *) (buf
+ 2));
1065 LOG(3, 0, 0, "Received DAE %s, id %d\n", radius_code(r_code
), r_id
);
1067 // check authenticator
1068 calc_auth(buf
, len
, zero
, hash
);
1069 if (memcmp(hash
, buf
+ 4, 16) != 0)
1071 LOG(1, 0, 0, "Incorrect vector in DAE request (wrong secret in radius config?)\n");
1075 // unpack attributes
1081 attribute
= *packet
++;
1082 attribute_length
= *packet
++;
1083 if (attribute_length
< 2)
1086 length
-= attribute_length
;
1087 attribute_length
-= 2;
1090 case 1: /* username */
1091 len
= attribute_length
< MAXUSER
? attribute_length
: MAXUSER
- 1;
1092 memcpy(username
, packet
, len
);
1094 LOG(4, 0, 0, " Received DAE User-Name: %s\n", username
);
1097 case 4: /* nas ip address */
1098 nas
= *(uint32_t *) packet
; // net order
1099 if (nas
!= config
->bind_address
)
1100 error
= 403; // NAS identification mismatch
1102 LOG(4, 0, 0, " Received DAE NAS-IP-Address: %s\n", fmtaddr(nas
, 0));
1105 case 5: /* nas port */
1106 port
= ntohl(*(uint32_t *) packet
);
1107 if (port
< 1 || port
> MAXSESSION
)
1110 LOG(4, 0, 0, " Received DAE NAS-Port: %u\n", port
);
1113 case 6: /* service type */
1115 uint32_t service_type
= ntohl(*(uint32_t *) packet
);
1116 auth_only
= service_type
== 8; // Authenticate only
1118 LOG(4, 0, 0, " Received DAE Service-Type: %u\n", service_type
);
1122 case 8: /* ip address */
1123 ip
= *(uint32_t *) packet
; // net order
1124 LOG(4, 0, 0, " Received DAE Framed-IP-Address: %s\n", fmtaddr(ip
, 0));
1127 case 11: /* filter id */
1128 LOG(4, 0, 0, " Received DAE Filter-Id: %.*s\n", attribute_length
, packet
);
1129 if (!(p
= memchr(packet
, '.', attribute_length
)))
1131 error
= 404; // invalid request
1136 i
= find_filter((char *) packet
, len
);
1137 if (i
< 0 || !*ip_filters
[i
].name
)
1143 if (!memcmp(p
, ".in", attribute_length
- len
))
1145 else if (!memcmp(p
, ".out", attribute_length
- len
))
1152 case 26: /* vendor specific */
1153 if (attribute_length
>= 6
1154 && ntohl(*(uint32_t *) packet
) == 9 // Cisco
1155 && *(packet
+ 4) == 1 // Cisco-AVPair
1156 && *(packet
+ 5) >= 2) // length
1158 int len
= *(packet
+ 5) - 2;
1159 uint8_t *a
= packet
+ 6;
1161 LOG(4, 0, 0, " Received DAE Cisco-AVPair: %.*s\n", len
, a
);
1162 if (avp
< sizeof(avpair
)/sizeof(*avpair
) - 1)
1165 avpair_len
[avp
++] = len
;
1171 packet
+= attribute_length
;
1174 if (!error
&& auth_only
)
1176 if (fin
!= -1 || fout
!= -1 || avp
)
1177 error
= 401; // unsupported attribute
1179 error
= 405; // unsupported service
1182 if (!error
&& !(port
|| ip
|| *username
))
1183 error
= 402; // missing attribute
1185 // exact match for SID if given
1189 if (!session
[s
].opened
)
1190 error
= 503; // not found
1195 // find/check session by IP
1196 i
= sessionbyip(ip
);
1197 if (!i
|| (s
&& s
!= i
)) // not found or mismatching port
1203 if (!error
&& *username
)
1207 if (strcmp(session
[s
].user
, username
))
1210 else if (!(s
= sessionbyuser(username
)))
1214 t
= session
[s
].tunnel
;
1218 case DisconnectRequest
: // Packet of Disconnect/Death
1221 r_code
= DisconnectNAK
;
1225 LOG(3, s
, t
, " DAE Disconnect %d (%s)\n", s
, session
[s
].user
);
1226 r_code
= DisconnectACK
;
1228 sessionshutdown(s
, "Requested by PoD", CDN_ADMIN_DISC
, TERM_ADMIN_RESET
); // disconnect session
1231 case CoARequest
: // Change of Authorization
1238 LOG(3, s
, t
, " DAE Change %d (%s)\n", s
, session
[s
].user
);
1243 struct param_radius_reset p
= { &tunnel
[session
[s
].tunnel
], &session
[s
] };
1244 run_plugins(PLUGIN_RADIUS_RESET
, &p
);
1251 LOG(3, s
, t
, " Filter in %d (%s)\n", fin
, ip_filters
[fin
- 1].name
);
1256 LOG(3, s
, t
, " Filter out %d (%s)\n", fout
, ip_filters
[fout
- 1].name
);
1258 filter_session(s
, fin
, fout
);
1260 // process cisco av-pair(s)
1261 for (i
= 0; i
< avp
; i
++)
1263 LOG(3, s
, t
, " Cisco-AVPair: %.*s\n", avpair_len
[i
], avpair
[i
]);
1264 handle_avpair(s
, avpair
[i
], avpair_len
[i
]);
1267 cluster_send_session(s
);
1285 *(uint32_t *) packet
= htonl(error
);
1289 *((uint16_t *)(buf
+ 2)) = htons(len
);
1292 calc_auth(buf
, len
, hash
, buf
+ 4);
1294 LOG(3, 0, 0, "Sending DAE %s, id=%d\n", radius_code(r_code
), r_id
);
1296 // send DAE response
1297 if (sendtofrom(daefd
, buf
, len
, MSG_DONTWAIT
| MSG_NOSIGNAL
, (struct sockaddr
*) addr
, alen
, local
) < 0)
1298 LOG(0, 0, 0, "Error sending DAE response packet: %s\n", strerror(errno
));
1301 // Decrypte the encrypted Tunnel Password.
1302 // Defined in RFC-2868.
1303 // the pl2tpsecret buffer must set to 256 characters.
1304 // return 0 on decoding error else length of decoded l2tpsecret
1305 int rad_tunnel_pwdecode(uint8_t *pl2tpsecret
, size_t *pl2tpsecretlen
,
1306 const char *radiussecret
, const uint8_t * auth
)
1308 MD5_CTX ctx
, oldctx
;
1311 unsigned i
, n
, len
, decodedlen
;
1313 /* 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
1314 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1315 | Salt | Salt | String ..........
1316 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+*/
1318 len
= *pl2tpsecretlen
;
1322 LOG(1, 0, 0, "tunnel password is too short, We need at least a salt\n");
1329 *pl2tpsecretlen
= 0;
1330 LOG(1, 0, 0, "tunnel passwd is empty !!!\n");
1334 len
-= 2; /* discount the salt */
1336 //Use the secret to setup the decryption
1337 secretlen
= strlen(radiussecret
);
1340 MD5_Update(&ctx
, (void *) radiussecret
, secretlen
);
1341 oldctx
= ctx
; /* save intermediate work */
1343 // Set up the initial key:
1344 // b(1) = MD5(radiussecret + auth + salt)
1345 MD5_Update(&ctx
, (void *) auth
, 16);
1346 MD5_Update(&ctx
, pl2tpsecret
, 2);
1349 for (n
= 0; n
< len
; n
+= 16)
1355 MD5_Final(hash
, &ctx
);
1359 // the first octet, it's the 'data_len'
1361 decodedlen
= pl2tpsecret
[2] ^ hash
[0];
1362 if (decodedlen
>= len
)
1364 LOG(1, 0, 0, "tunnel password is too long !!!\n");
1368 MD5_Update(&ctx
, pl2tpsecret
+ 2, 16);
1372 MD5_Final(hash
, &ctx
);
1375 MD5_Update(&ctx
, pl2tpsecret
+ n
+ 2, 16);
1378 for (i
= base
; i
< 16; i
++)
1380 pl2tpsecret
[n
+ i
- 1] = pl2tpsecret
[n
+ i
+ 2] ^ hash
[i
];
1384 if (decodedlen
> 239) decodedlen
= 239;
1386 *pl2tpsecretlen
= decodedlen
;
1387 pl2tpsecret
[decodedlen
] = 0;