# macchanger + WLAN + dhcpcd + openrc timing problems

## toralf

The config see below won't work always during boot. Sometimes I do get 

```
[ERROR] Could not change MAC: interface up or insufficient permissions: Device or resource busy

 * ERROR: cannot start cupsd as net.wlp3s0 would not start

 * ERROR: cannot start dnsmasq as net.wlp3s0 would not start

 * ERROR: cannot start ntp-client as net.wlp3s0 would not start

```

 during boot. In such a case sometimes even a "sudo /etc/init.d/net.wlp3s0 restart" won't help, so I have to reboot the (hardened) stable Gentoo. Now I do wonder, if I could run "macchanger" much more earlier in the udev - init phase ? Which udev init script I should use there to have the MAC address change already done before openrc starts the network init scripts ?

```
# /etc/conf.d/net

#

#  KVM

#

bridge_br0="tap0"

config_br0="192.168.1.254/16"

brctl_br0="setfd 0

sethello 10

stp off"

rc_net_br0_need="net.tap0"

config_tap0="null"

tuntap_tap0="tap"

tunctl_tap0="-u tfoerste"

#  LAN

#

config_enp0s25="null"

#   don't play well with macchanger

#

#config_wlp3s0="dhcp"

preup(){

  if [[ "$IFACE" = "wlp3s0" ]]; then

    macchanger -r $IFACE

    :

  fi

}

postup(){

  if [[ "$IFACE" = "br0" ]]; then

    sysctl -w net.ipv4.conf.br0.forwarding=1

  fi

}

predown(){

  if [[ "$IFACE" = "br0" ]]; then

    sysctl -w net.ipv4.conf.br0.forwarding=0

  fi

}

postdown(){

  if [[ "$IFACE" = "wlp3s0" ]]; then

    macchanger -p $IFACE

    :

  fi

}
```

filed bug #547020

----------

## UberLord

This isn't a bug in the software, the error is in your preup() function which assumes the interface will be down.

Once the interface is "up", the scripts won't actually take it "down" when stopping.

```

preup()

{

  if [ "$IFACE" = wlp3s0 ]; then

    ifconfig "$IFACE" down

    macchanger -r "$IFACE"

    :

  fi

}

```

----------

## toralf

 *UberLord wrote:*   

> This isn't a bug in the software, the error is in your preup() function which assumes the interface will be down.
> 
> Once the interface is "up", the scripts won't actually take it "down" when stopping.
> 
> ```
> ...

 Ah, what I already tried , was 

```
   ifconfig "$IFACE" down

    macchanger -r "$IFACE"

 ifconfig "$IFACE" up
```

which won't worked right, but will try your suggestion too. That's why I filed https://bugs.gentoo.org/show_bug.cgi?id=547020

----------

## UberLord

Well, if there is an error you might need a sleep between bringing the interface down and running macchanger.

----------

## lost+found

Does it make a difference, if the syntax mentioned in /usr/share/doc/netifrc-0.2.2/net.example.bz2 is used (instead of running macchanger in a `preup')?

```
# For changing MAC addresses using the below, emerge net-analyzer/macchanger

# - to randomize the last 3 bytes only

#mac_eth0="random-ending"

# - to randomize between the same physical type of connection (e.g. fibre,

# copper, wireless) , all vendors

#mac_eth0="random-samekind"

# - to randomize between any physical type of connection (e.g. fibre, copper,

# wireless) , all vendors

#mac_eth0="random-anykind"

# - full randomization - WARNING: some MAC addresses generated by this may NOT

# act as expected

#mac_eth0="random-full"

# custom - passes all parameters directly to net-analyzer/macchanger

#mac_eth0="some custom set of parameters"
```

Also it might help to separate any services related to networking, and the rest, in different runlevels:

In runlevel `default': any services that need networking + the stacked runlevel `nonetwork'

In runlevel `nonetwork': any services not networking related (including the display manager)

Then, instead of just restarting the network interface, use:

```
# rc nonetwork && rc default
```

This might solve problems with depending services.

(And if the network interfaces have their real MAC addresses put back in `nonetwork', you're able to restart Udev after an upgrade safely.)

----------

## toralf

I tried :

```
mac_wlp3s0="random-samekind"
```

but got either

```
 * Bringing up interface wlp3s0

 *   Changing MAC address of wlp3s0 ...

RTNETLINK answers: Operation not possible due to RF-kill

 *   Failed to set MAC address                                                                                              [ !! ]
```

or

```
 * Bringing up interface wlp3s0

Current MAC:   00:1d:d7:29:0f:cd (Algolith)

Permanent MAC: e8:b1:fc:9d:87:85 (Intel Corporate)

[ERROR] Could not change MAC: interface up or insufficient permissions: Device or resource busy

 *   Starting wpa_supplicant on wlp3s0 ...

```

Tweaking /etc/init.d/net.lo is the only reliable way.

(BTW ofcourse I forgot to put xdm into "nonetwork" when I switched the fist time to 

```
rc nonetwork
```

 :Very Happy: )

----------

## lost+found

 *toralf wrote:*   

> RTNETLINK answers: Operation not possible due to RF-kill

 

Don't know if this is good or bad news, but according to Internet this is a driver issue. The good news is, there's a kernel option: `CONFIG_RFKILL', and a userspace tool: net-wireless/rfkill. I think the driver uses rfkill on its own (to unblock the controller), but if that fails it can be scripted maybe.

In the Bluetooth wiki there are a few lines about using rfkill manually.

----------

## toralf

Good hint.

But if I use "rfkill unblock 0" within preup(), then I do always just run into the subsequent issue 

```
[ERROR] Could not change MAC: interface up or insufficient permissions: Device or resource busy

```

----------

## toralf

 *lost+found wrote:*   

> Does it make a difference, if the syntax mentioned in /usr/share/doc/netifrc-0.2.2/net.example.bz2 is used (instead of running macchanger in a `preup')?

 BTW, is there any chance to get the SSID coded into the config option, eg. to exclude the MAC changing at home (at least with WLAN router I eventually run into permanent connection problems, till I removed the outdated entries from the past)

----------

## lost+found

 *toralf wrote:*   

>  *lost+found wrote:*   Does it make a difference, if the syntax mentioned in /usr/share/doc/netifrc-0.2.2/net.example.bz2 is used (instead of running macchanger in a `preup')? BTW, is there any chance to get the SSID coded into the config option, eg. to exclude the MAC changing at home (at least with WLAN router I eventually run into permanent connection problems, till I removed the outdated entries from the past)

 

The easiest way, I think, is creating a parallel runlevel `home' (again with `nonetwork' stacked into it). When you switch runlevel from `default' to `nonetwork' to `home', completely different settings can be read from the file `/etc/conf.d/net.home' (/etc/conf.d/net is only used when there's no file with the runlevel name extension).

So `rc nonetwork && rc home' will then just restart everything related to the home network. To be able to switch or start a default at boot time, softlevel=home, softlevel=nonetwork etc. will work. It's possible to have separate settings for other services too (sshd?), in this wiki example. The downside is that there are much more files to maintain, and it might not work when not everything is closed down in `nonetwork' as seems to be the case... (can dhcpcd be killed?)

Maybe something like this is usefull in preup etc.:

```
RL=`/bin/rc-status -r`

if [ "$RL" != "home" ]; then

...
```

----------

## UberLord

 *toralf wrote:*   

> BTW, is there any chance to get the SSID coded into the config option, eg. to exclude the MAC changing at home (at least with WLAN router I eventually run into permanent connection problems, till I removed the outdated entries from the past)

 

No, because the interface needs to be UP to learn the SSID but down to change it.

When it's up, it's already too late - the MAC address is part of the 802.11 frames.

----------

## vhuto

How are you, guys.   :Very Happy: 

 *toralf wrote:*   

> But if I use "rfkill unblock 0" within preup(),
> 
> 

 

Rfkill blocks radio emission. So, toralf, you should use "rfkill block 0" or "rfkill block wifi", etc.

Then a wireless interface will be down. At this timing, its MAC address can be changed. Maybe...

===== below is all about my Gentoo =====

I also had the same trouble with mac_wlan0="random-ending" in conf.d/net.

So, I wrote udev rule like:

```
SUBSYSTEM=="net", ACTION=="add",    RUN+="/usr/bin/macchanger -e %k"

SUBSYSTEM=="net", ACTION=="remove", RUN+="/usr/bin/macchanger -p %k"
```

This had worked for a while, but comes worse. i dont know why.

For now, using my init script:

```
#!/sbin/runscript

depend() {

    after udev

    before dhcpcd

}

start() {

    ebegin "Changing MAC Addresses"

    macchanger -e eno1

    macchanger -e wlo1

    eend $?

}
```

(Apparently it's should be rewritten with elegance  :Wink:  )

On my Gentoo system,

- WiFi is blocked since booting system by Rfkill module setting.

- Dhcpcd daemon starts by its init script at default runrevel and provides net service. 

- And I don't have complex setting about networking. (Using dhcpcd for ethernet, moreover, wpa_supplicant for wlan)

So, I have removed net.eno1 and net.wlo1 from runlevels and those settings from conf.d/net. Dhcpcd daemon takes care of those things. 

Also, Dhcpcd starts wpa_supplicant daemon through its hook-script: /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant

```
----- /etc/dhcpcd.conf -----

#nohook wpa_supplicant

env wpa_supplicant_driver=wext

-----
```

Last edited by vhuto on Wed Apr 29, 2015 9:33 am; edited 1 time in total

----------

## Doron

I may be able to help, since I had similar issues and after some debugging managed to

find the issue;

Currently I have installed:

[I] net-analyzer/macchanger

     Available versions:  1.7.0

It seems that since 1.6.0 (yes..) the output of macchanger changed.

so net-misc/netifrc-0.2.2 was looking for the wrong string....

You can see it all in: https://bugs.gentoo.org/show_bug.cgi?id=504458 (which I found after digging the code for too long).

Bottom line, you should use net-misc/netifrc-0.2.3 by adding a keyword to it

and use the standard way in the conf file:

#mac_eth0="random-anykind"

----------

## toralf

Ah - thx Doron !

FWIW I field a now bug :https://bugs.gentoo.org/show_bug.cgi?id=547906 for it

----------

## vhuto

Oh, 0.2.3, 0.2.3...let me try it.

I restored the netifrc standard way. It successfully changes MAC address.

(The code config_interface="dhcp" dosen't work, so I added a dhcpcd service in boot runlevel.)

WiFi connection is eventually not established. I need restart of wpa_supplicant for it.

```
wpa_cli terminate

wpa_supplicant -iwlo1 -Dwext -c/etc/wpa_supplicant/wpa_supplicant.conf -B

```

Also on my environment above, I need this restart every time after changing MAC address. Perhaps my customizing is odd a bit  :Question: 

Anyway sorry for noise. Congrats  :Razz: 

----------

