# init script for hostapd[solved]

## Adel Ahmed

I'm trying to start hostap automatically on boot, So I wrote a simple init script:

localhost ~ # cat /etc/init.d/router 

#!/sbin/runscript

# Copyright 1999-2013 Gentoo Foundation

# Distributed under the terms of the GNU General Public License v2

# $Header: $

depend() {

	after xdm

}

start() {

	hostapd /etc/hostapd/hostapd.conf > /var/log/router.log 2>&1 &

	#/etc/init.d/iptables start

	ifconfig wlan0 192.168.0.1/24

}

stop() {

	echo bye

}

If I run this script manually, everything is fine, If I add the script to the default run level, hostapd is running, iwconfig show wlan0 is running in master mode, ifconfig show wlan0 has no IP and I cannot connect to this Access point

when I looked at /var/log/router.log (hostapd's output), i found this error:

handle probe req: send: resource temporarily unavailable

I want hostap to start automatically on boot, and then the IP should be assigned to that interface

I don't mind other approaches as long as the job gets done, I'm using gnome 2.32.1-r2, openrc 0.11.8

thanksLast edited by Adel Ahmed on Fri Dec 06, 2013 7:30 pm; edited 1 time in total

----------

## Christian99

why don't u use the hostapd script, that is already there?

----------

## Adel Ahmed

same result, a manual start works while the autostart yields the same result

EDIT: actually the wireless interface comes up briefly and is lost afterwards

I'll install rsyslog and start debugging

----------

## szatox

i bet it's the same issue

https://forums.gentoo.org/viewtopic-p-7451478.html#7451478

----------

## Adel Ahmed

nope 

I can't find any logs under /var/log, the only one that had any helpful info was /var/log/rc.log:

sometimes I get:

* Starting hostapd ...

Configuration file: /etc/hostapd/hostapd.conf

nl80211: Failed to set interface wlan0 into AP mode

nl80211 driver initialization failed.

 * start-stop-daemon: failed to start `/usr/sbin/hostapd'

In usually get:

 * Starting hostapd ...

Configuration file: /etc/hostapd/hostapd.conf

Using interface wlan0 with hwaddr 00:60:b3:xx:xx:xx and ssid "test"

and I always end up with no wireless AP

----------

## szatox

have you triggered that rc-update --update command? This is what I linked, not entire topic

Do you have logger installed?

What does this command say?

grep -v '^$\|^#' /etc/hostapd/hostapd.conf /etc/conf.d/hostapd /etc/conf.d/net

----------

## Adel Ahmed

yup I have  :Very Happy: 

and here's the output of those very interesting options, definitely gonna look them up and learn how to use them, thanks:

localhost ~ # grep -v '^$\|^#' /etc/hostapd/hostapd.conf /etc/conf.d/hostapd /etc/conf.d/net

/etc/hostapd/hostapd.conf:interface=wlan0

/etc/hostapd/hostapd.conf:driver=nl80211

/etc/hostapd/hostapd.conf:ssid=test

/etc/hostapd/hostapd.conf:hw_mode=g

/etc/hostapd/hostapd.conf:channel=7

/etc/hostapd/hostapd.conf:wpa=1

/etc/hostapd/hostapd.conf:wpa_passphrase=password

/etc/hostapd/hostapd.conf:wpa_key_mgmt=WPA-PSK

/etc/hostapd/hostapd.conf:wpa_pairwise=TKIP

/etc/hostapd/hostapd.conf:rsn_pairwise=CCMP

/etc/hostapd/hostapd.conf:logger_syslog=-1

/etc/hostapd/hostapd.conf:logger_syslog_level=4

/etc/conf.d/hostapd:INTERFACES="wlan0"

/etc/conf.d/hostapd:CONFIGS="/etc/hostapd/hostapd.conf"

/etc/conf.d/hostapd:OPTIONS=""

/etc/conf.d/net:config_eth0=( "192.168.1.6 netmask 255.255.255.0" ) 

/etc/conf.d/net:routes_eth0=( "default gw 192.168.1.1" ) 

/etc/conf.d/net:dns_servers_eth0=( "8.8.8.8" )

/etc/conf.d/net:modules_wlan0=( "wpa_supplicant" ) 

/etc/conf.d/net: wpa_supplicant_wlan0=( "-Dwext" )

/etc/conf.d/net:config_wlan0=( "192.168.0.1 brd 192.168.0.255 netmask 255.255.255.0" )

/etc/conf.d/net:routes_wlan0=( "default via 192.168.0.1" )

so this command looks for something that does not begin with a # or a $, am I getting this right? also is this a regular expression?

----------

## Adel Ahmed

yet restarting the init script seems to be working, I have removed the 'need net.wlan0' don't know if this makes any difference( I have done this earlier to get the script working in the first place)

----------

## Adel Ahmed

I have temporarily worked around the problem by addin the at command to another init script

still gonna try working on it though

----------

## szatox

ok, i hav never set hostapd with wpa, so I might get things wrong at this point, but isn't hostapd for master mode the same thing wpa_supplican't is for client mode? 

I mean, for master mode you set passphrase in hostapd.conf. Why you want to use 2 different tools for doing the same thing at the same time?

I've picked up first "get started fast" howto on hostapd just to make sure http://nims11.wordpress.com/2012/04/27/hostapd-the-linux-way-to-create-virtual-wifi-access-point/ I know it's not for gentoo, but hey, hostapd is hostapd, and it will read hsotapd's config regardless of distro  :Smile: 

so, probably the right thing to do will be (probably is the keyword here... I'm using old, weak wep, yet I'd give this a try)

remove those lines as they probably make mess

/etc/conf.d/net:modules_wlan0=( "wpa_supplicant" ) 

/etc/conf.d/net: wpa_supplicant_wlan0=( "-Dwext" ) 

those 2 will make services related to network start hostapd instead of wlan0 directly

add line: rc_net_wlan0_provide="!net" to /etc/conf.d/net

add add line :rc_provide="net" to /etc/conf.d/hostapd (or make your dhcpd rc_need="hostapd" - assuming you  have dhcpd for your local network)

Also, I don't get the purpose of line below, considering you already have an interface with IP address in the same subnet. I don't know how it's handled, might do nothing or prevent you from sending anything to this subnet (or anything at all using this interface)

/etc/conf.d/net:routes_wlan0=( "default via 192.168.0.1" ) 

hostapd does know what interface it needs, it's init script has some magic that takes care of it. You don't have to explicitly state it

and yes, this command contains regular expression. It says "ignore every line that is empty or begins with #".  In other words ->skip comments and the rest of crap. (well, in fact it's buggy, but with default configs works well enough for me to give up on handling whitespace too)

----------

## Logicien

Use the wlan0 wireless interface as a Wpa server and client at the same time and give to this interface the same Ip address and route cannot be a sane configuration.

Why use Wpa_supplicant client on wlan0 if it is a Wpa server with Hostapd? I never seen that at the same time.

Why use Wpa_supplicant client on wlan0 if no dhcp request is made? 

Why use Wpa_supplicant if static IP address and route is set on wlan0?

Why give the same IP adress and route 192.168.0.1 to wlan0? I never seen an interface be routed to itself. This mean that the traffic that cannot be routed through 192.168.0.0 and arrive to 192.168.0.1 will be routed through 192.168.0.0 by the default route 192.168.0.1. The traffic will go back on the interface and communication with other networks will be impossible.

Hostapd is usefull if the clients it authenticate can receive an Ip configuration from a Dhcp server on the Ap interface. The Ap interface IP address is on the same network than the one receive by the Dhcp clients.

----------

## Adel Ahmed

about that route thing, I removed it, that does make sense  :Very Happy: , it's just a habit to set the default gateway even If I'm at the gw  :Smile: 

I removed both lines and I'm getting the same result

----------

## Logicien

Before, you say wlan0 have run in master mode. Have you ever check if it really support that mode?

```
iw phy
```

Look for

```
Supported interface modes:

...

* AP

...
```

----------

## Adel Ahmed

thanks for your reply

here's the result:

Supported interface modes:

		 * IBSS

		 * managed

		 * AP

		 * AP/VLAN

		 * monitor

I'm not having troubles running the interface in master mode, I have a problem running the if in master mode automatically, I can't pinpoint the problem though

----------

## szatox

What do you do to run it manually?

Also, result of `rc-update show` might be usefull

----------

## Adel Ahmed

well it's already started so I issue /etc/init.d/hostapd restart, and if it's not, I use start instead. It works in both cases

localhost pri # rc-update show

                  atd |      default                 

             bootmisc | boot                         

           consolekit |      default                 

                 dbus |      default                 

                devfs |                       sysinit

                dhcpd |      default                 

                dmesg |                       sysinit

              hostapd |      default                 

             hostname | boot                         

              hwclock | boot                         

              keymaps | boot                         

            killprocs |              shutdown        

           lm_sensors |      default                 

                local |      default                 

           localmount | boot                         

              modules | boot                         

             mount-ro |              shutdown        

                 mtab | boot                         

                named |      default                 

               net.lo | boot                         

               procfs | boot                         

                 root | boot                         

              rsyslog |      default                 

            savecache |              shutdown        

                 sshd |      default                 

                 swap | boot                         

            swapfiles | boot                         

               sysctl | boot                         

                sysfs |                       sysinit

         termencoding | boot                         

             tigervnc |      default                 

       tmpfiles.setup | boot                         

                 udev |                       sysinit

           udev-mount |                       sysinit

       udev-postmount |      default                 

              urandom | boot                         

                  vnc |      default                 

                 wicd |      default                 

                  xdm |      default

----------

## szatox

*Staring at a glass ball, playing psychic, writing answers to not-yet-asked questions and stating things you haven't mentioned.

You've got a PC you want to turn into a wireless router. Your setup is (the internet)  -//- (eth0) <your wtf-trouble-somePC>(wlan0) ))) local network

For some reason, you have decided to use WICD as network manager, even though you only have 1 interface in client mode to manage, even though it's handled automagicaly by provided-by-default netifrc.

At the begining of this topic, you were trying to manage your wlan0 with 3 (sic!) services at the same time: netifrc+hostapd, wicd, wpa_supplicant. (Still missing NM though XD )

So, with wpa_Supplicant exterminated, you stil lhave conflicting wicd and netifrc+hostapd. Hostapd and wicd surely both provide net, so hostapd is launched first, as it's before wicd in alphabetical order. Once it's set, wicd is launched and overwrites whatever hostapd has done turning your wlan0 into client again.

I think that's 3rd WTF pointed out by now.

So, make your mind on the way you go for cofiguring network interfaces and clean up the mess after the other.

Also, sniffing another problem: have you limited dhcpd to your wlan0? Default config provides some protection from this, but you might me sending DHCP offers to computers in your ISP network as well as to computers in your local network. And I bet neither you nor your ISP want this

----------

## Adel Ahmed

yes that is correct  :Very Happy: 

a simple removal of wicd fixed things and hostapd is starting as an init script  :Very Happy: 

and my DHCP is only listening on the local netowrk  :Very Happy: 

Problem solved. 

and I configured a static ip for wlan0 by adding the following to /etc/hostapd/hostapd.conf

own_ip_addr=192.168.0.1

thanks for your help, you have been most helpful

----------

