Convert an integer to a MAC address in unlang / expr

Alan DeKok aland at deployingradius.com
Wed Aug 26 16:11:40 CEST 2020


On Aug 25, 2020, at 11:50 PM, James Wood <james.wood at purplewifi.com> wrote:
> 
> Quick one - I have some Aruba APs that are sending the BSSID in the
> Called-Station-Id attribute instead of the base radio MAC. I need to get
> back to the base radio MAC, so Aerohive has provided a mask we can apply to
> find out the base radio MAC from this BSSID.

  Could you also provide the debug output?  That will let us know if the regex is correct at least.

> I am doing:
> 
> if (&Called-Station-Id =~
> /^([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})([^0-9a-f](.+))?$/i)
> {
>   update control {
>       &Tmp-String-1 := "%{expr: 0x%{1}%{2}%{3}%{4}%{5}%{6} &
> 0xFFFFFFFFFFC0}"
>   }
> }
> 
> This works and the debug output shows &Tmp-String-1:
> 
> EXPAND %{expr: 0x%{1}%{2}%{3}%{4}%{5}%{6} & 0xFFFFFFFFFFC0}
> 
> --> 149928134221440
> 
> 149928134221440 is correct so I need to base convert this integer back to a
> real MAC in a format like  88-5B-DD-2E-9E-80 or  885BDD2E9E80 instead of
> 149928134221440.

  Without changing too much, you can do:

if (&Called-Station-Id =~
/^([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})[^0-9a-f]?([0-9a-f]{2})([^0-9a-f](.+))?$/i)
{
  update control {
      &Tmp-String-1 := "%{1}-%{2}-%{3}-%{4}-%{5}-%{expr:0x%{6} & 0xc0}"
  }
}

    Which should be a lot simpler.  The final value might still be printed as decimal, tho.  I don't think the %{hex:...} expansion will do what you want.

  Alan DeKok.




More information about the Freeradius-Users mailing list