-
Notifications
You must be signed in to change notification settings - Fork 21
/
Radius-DHCP-HTTPS.lua
182 lines (156 loc) · 4.83 KB
/
Radius-DHCP-HTTPS.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local parse_radius=require 'Default-Radius'
local parse_dhcp=require 'Default-DHCP'
local dictionary= {}
payload=avi.l4.read()
local prime={3,5,7,11,13,17}
local vip_port=avi.vs.port()
local vs_name=avi.vs.name()
---------------- hash function ---------
----- create hash for the key ----
local function hashfunction(key, prefix)
local key_len = string.len(key)
local nkey = 0
local tmp = 0
local hash = 0
while (key_len > 0) do
tmp = (key_len%6) + 1
nkey = nkey + (prime[tmp] * string.byte(key,key_len))
key_len = key_len -1
nnkey = nkey % 101
hash = tostring(nnkey)
return prefix.."_"..hash;
end
end
------------------------------------------
----------------- healthcheck -------------
--- check health of server, return 0 if server down, 1 if server up ---
local function fetch_healthy_server(hash)
local _value = avi.vs.table_lookup(hash)
local _server = ""
local _ret_code = 0
if(_value ~= NIL) then
local s = parse_radius.split(_value, "-")
_server1 = s[1]
if(avi.pool.get_server_status("l4-radius-pool", _server1, 1812) ~= 1) then
avi.vs.log("server".._server1.."is down, removing entry")
avi.vs.table_remove(hash)
_ret_code = 0
else
_ret_code = 1
end
end
return _ret_code, _server1;
end
--------------------------------------------
---------- key and timeout select--------------
---- keyselect for radius 1812 and 1813 ----
local function keysel()
if (vip_port ~= "1813" and vip_port ~= "1812" and vip_port ~= "1645" and vip_port ~="1646") then
return
end
avi.vs.log("vip_port ="..vip_port)
local dictionary = parse_radius.parsepacket(payload)
local nas_port_type=dictionary["61"]
local calling_station_id=dictionary["31"]
local framed_ip=dictionary["8"]
local nas_ip=dictionary["4"]
local key = ""
local timeout = 28800
if (calling_station_id == NIL) then
key = nas_ip
prefix = "nas_ip"
else
key = calling_station_id
prefix = "m"
end
if (nas_port_type == 19) then
timeout = 3600
end
return key, timeout, prefix, framed_ip
end
-----snmp parsing------------------------
if(vip_port=="161" or vip_port=="162") then
--- if snmp packet, load balance it to servers. No persistence required ----
avi.l4.ds_done()
return
-----dhcp parsing----------------------------
elseif (vip_port == "67") then
dhcp_params, dictionary = parse_dhcp.getDHCPParamsAndOptions(payload)
interface, mac = parse_dhcp.getOption61(dictionary)
key = string.lower(mac)
local health = 0
local server = ""
if (key ~= NIL) then
hash = hashfunction(key, "m")
health, server = fetch_healthy_server(hash)
if(health == 1) then
avi.pool.select("l4-radius-pool", server, 1812)
avi.vs.log("Using client id"..key)
avi.l4.ds_done()
return
end
end
------ https parsing -----
elseif (vip_port == "443") then
key = avi.vs.client_ip()
hash = hashfunction(key,"ip")
health, server = fetch_healthy_server(hash)
if (health == 1) then
avi.pool.select("l4-radius-pool", server, 1812)
avi.l4.ds_done()
return
end
----- radius 1813 parsing ---
elseif (vip_port == "1813" or vip_port=="1646") then
key, timeout, prefix, framed_ip = keysel()
hash = hashfunction(key,prefix)
health, server = fetch_healthy_server(hash)
if (health == 0) then
avi.vs.persist(hash, timeout)
avi.l4.ds_done()
return
end
if(framed_ip ~= NIL) then
avi.vs.log("persist using framed ip")
key = framed_ip
hash = hashfunction(key, "ip")
if(avi.vs.table_lookup(hash)==NIL) then
server2 = server.."-"..tostring(1812)
avi.vs.table_insert(hash,server2,timeout)
else
health,server=fetch_healthy_server(hash)
if(health==0) then
avi.vs.persist(hash,timeout)
end
end
end
avi.pool.select("l4-radius-pool", server, 1812)
avi.l4.ds_done()
return
-----radius authentication(1812) parsing-------
elseif (vip_port=="1812" or vip_port=="1645") then
key, timeout, prefix, framed_ip = keysel()
hash = hashfunction(key,prefix)
---- health check & create persistence-----
value = avi.vs.table_lookup(hash)
if(value == NIL) then
avi.vs.persist(hash, timeout)
avi.vs.log("new entry")
avi.l4.ds_done()
return
end
health,server = fetch_healthy_server(hash)
if (health~=1) then
avi.vs.persist(hash, timeout)
avi.vs.log("server down")
avi.l4.ds_done()
return
else
avi.vs.log("updating timers for existing entry")
avi.vs.table_refresh(key,timeout)
avi.pool.select("l4-radius-pool", server, 1812)
avi.l4.ds_done()
return
end
end
avi.l4.ds_done()