From 3384986befe84c244b2fc9c3c9c4c36e48f9dfec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaut=20VAR=C3=88NE?= Date: Mon, 22 May 2023 16:48:20 +0200 Subject: [PATCH] uspot: support ChilliSpot radius ratelimits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces a helper function "ratelimit_client()", invoked from allow_client(), which parses the radius reply for known ratelimiting attributes: - WISPr-Bandwidth-Max-{Up,Down} - ChilliSpot-Bandwidth-Max-{Up,Down} WISPr attributes are expressed in bits/s, ChilliSpot in kbits/s. If none of the attributes are present, the function is a NOP. If any of the -Up or -Down is missing, the corresponding limit is not set. NB: ratelimit currently does not support setting only up OR down ratelimiting if defaults are not set. Signed-off-by: Thibaut VARĂˆNE --- .../uspot/files/usr/share/uspot/common.uc | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/feeds/ucentral/uspot/files/usr/share/uspot/common.uc b/feeds/ucentral/uspot/files/usr/share/uspot/common.uc index 8dc22b516..7df2ecd1c 100644 --- a/feeds/ucentral/uspot/files/usr/share/uspot/common.uc +++ b/feeds/ucentral/uspot/files/usr/share/uspot/common.uc @@ -167,15 +167,34 @@ return { include('redir.uc', { redir_location: ctx.query_string.userurl }); else include('allow.uc', ctx); - //data.radius.reply['WISPr-Bandwidth-Max-Up'] = "20000000"; - //data.radius.reply['WISPr-Bandwidth-Max-Down'] = "10000000"; - if (data?.radius?.reply && (+data.radius.reply['WISPr-Bandwidth-Max-Up'] && +data.radius.reply['WISPr-Bandwidth-Max-Down'])) - ctx.ubus.call('ratelimit', 'client_set', { - device: ctx.device, - address: ctx.mac, - rate_egress: sprintf('%s', data.radius.reply['WISPr-Bandwidth-Max-Down']), - rate_ingress: sprintf('%s', data.radius.reply['WISPr-Bandwidth-Max-Up']), - }); + + this.ratelimit_client(ctx, data); + }, + + // ratelimit a client from radius reply attributes + ratelimit_client: function(ctx, data) { + if (!(data?.radius?.reply)) + return; + + let reply = data.radius.reply; + + // check known attributes - WISPr: bps, ChiliSpot: kbps + let maxup = reply['WISPr-Bandwidth-Max-Up'] || reply['ChilliSpot-Bandwidth-Max-Up']*1000; + let maxdown = reply['WISPr-Bandwidth-Max-Down'] || reply['ChilliSpot-Bandwidth-Max-Down']*1000; + + if (!(+maxdown || +maxup)) + return; + + let args = { + device: ctx.device, + address: ctx.mac, + }; + if (+maxdown) + args.rate_egress = sprintf('%s', maxdown); + if (+maxup) + args.rate_ingress = sprintf('%s', maxup); + + ctx.ubus.call('ratelimit', 'client_set', args); }, // put a client back into pre-auth state