# Network config for laptop

## petterg

I'm bringing a laptop around to 3 different networks. Is there any way to make it detect which network setting to use?

The behavior I'd like would be something like this:

 - Try to get DHCP ip for a few seconds

 - - if sucsess, done

 - - else:

 - - - Check if 192.168.1.1 is in use (default gateway at one location)

 - - - - If 192.168.1.1 is used:

 - - - - - set 192.168.1.1 as gateway

 - - - - - check if 192.168.1.240 is available - if yes: use 192.168.1.240 as ip

 - - - - - else: try another random ip in the same subnet, keep trying until available ip is found and use that ip.

 - - - - else

 - - - - - Check if 10.0.0.254 ip is in use (default gateway at one location)

 - - - - - - (do the same stuff as for 192.168.1.* network)

Is this posible? There seems to be a lot of posibilities in /etc/conf.d/net, but I might need some kind of script for this?

----------

## UberLord

Sounds like we need an arping module which maps a gateway IP config to our interface config so something like this could work

```

# List gateways to arping

arping_eth0="192.168.1.1 10.1.1.1"

# Map found gateways to config's

config_192168001001=( "192.168.1.240/24" )

fallback_192168001001=( "apipa" )

routes_192168001001=( "default via 192.168.1.1" )

config_010000000001=( "10.0.0.254/24" )

fallback_010000000001=( "apipa" )

routes_010000000001=( "default via 10.0.0.1" )
```

Care to code one?

----------

## UberLord

baselayout-1.12.0_pre9 will replace the apipa module with a arping module which does the above (slightly different config, check net.example) and apipa if no gateway is found

so this works

```
config_eth0=( "dhcp" )

fallback_eth0=( "arping" )
```

----------

## UberLord

Those that want to try it out, can grab it from here

http://dev.gentoo.org/~uberlord/baselayout/arping

 Drop it into /lib/rcscripts/net.modules.d and delete the apipa module that 's currently there. Requires baselayout-1.12.0_pre8 (will ship with pre9).

----------

## petterg

I thought arping only used ip's in the range 169.254.0.0 - 169.254.255.255.

```

config_eth0=( "dhcp" ) 

fallback_eth0=( "arping" )

```

How can I set an ip-adress that it will try before it tries the random adresses?

Maybe I missunderstod. Am I supposed to use the code from both of your postings or just one of them (depending if baselayout-1.12.0_pre9 is installed)?

----------

## UberLord

 *petterg wrote:*   

> I thought arping only used ip's in the range 169.254.0.0 - 169.254.255.255.

 

That's the APIPA address range.

You use arping to see if an address is in use or not - APIPA uses this. However, you can use arping to find any ipv4 address, not just the APIPA range  :Smile: 

 *Quote:*   

> 
> 
> ```
> 
> config_eth0=( "dhcp" ) 
> ...

 

You use both. You have to instruct the interface config to use arping. That will then loop through the gateways in arping_eth0. If it finds one then it applies it's config (if any). If no gateways are found then we arping an APIPA address. This ensures that the interface should come up.

Maybe we need an option to arping but not apipa?

----------

## petterg

Thanx

I'll try this out...

----------

## jamapii

On my old laptop, I had a script that did this.

It probed various addresses until it was successful, and it looked at the MAC addresses in /proc/net arp (at least in old versions).

Now I'm relying on dhcp and on different runlevels for non-dhcp networks, but I will likely try the new arping module at some time  :Very Happy: 

About the config/fallback system, I suggest to make it more flexible, to replace it with something that allows unlimited fallbacks (and not hardcode fallbacks in modules). My preferred configuration would be: first try "dhcp", then try "arping", then assign a static ip address.

This requires at least an arping that does not fall back to apipa, and a way to set up a second fallback. (Sounds like 2-dimensional arrays, or a fallback2_eth0, fallback3_eth0...)

----------

## frozenJim

Whooo boy  :Shocked:  .  Uber, you are getting me all excited here.

So if I need to access two networks:

```
Home: 192.168.2.7

DNS: 192.168.2.1

Gateway: 192.168.2.1
```

and

```
Work: 172.16.10.170

DNS: 172.16.10.10

Gateway: 172.16.10.1
```

Then is it correct to use the following in my /etc/conf.d/net?:

```
# List gateways to arping

arping_eth0="192.168.2.1 172.16.10.1"

# Map found gateways to config's

config_192168002001=( "192.168.2.7" )

fallback_192168001001=( "dhcp" )

routes_192168001001=( "default via 192.168.2.1" )

config_172016010001=( "172.16.10.170" )

fallback_172016010001=( "dhcp" )

routes_172016010001=( "default via 172.16.10.10" )

config_eth0=( "dhcp" )

fallback_eth0=( "arping" ) 
```

If I'm hearing you correctly, it will arping both known networks and then use the config for the one that it found.

If IP assignment fails, it will fallback to dhcp (available on both networks in my case).

If neither network is avialable the it will attempt an standard dhcp connection.

If THAT fails, then it will try arping whatever network to grab any available address that is not in use.

Am I even CLOSE to understanding?

And then, under the last scenario, how do i discover and set my dns/gateway addresses?

----------

## UberLord

 *frozenJim wrote:*   

> Whooo boy  .  Uber, you are getting me all excited here.
> 
> So if I need to access two networks:
> 
> ```
> ...

 

gateways_eth0="192.168.2.1 172.16.10.1"

Aside from that it looks good. Although I'd set a timeout on the dhcp request - dhcpcd defaults to 60 seconds.

dhcpcd_eth0="-t 5"

5 should be good  :Smile: 

 *Quote:*   

> Am I even CLOSE to understanding?
> 
> And then, under the last scenario, how do i discover and set my dns/gateway addresses?

 

dnsservers_192168001001="12.3.4.5"

dnsdomain_192168001001="foo.bar.com"

You've already set the gateway by using the default route  :Smile: 

----------

## frozenJim

OK, I think I understand what we're doing here... but I'm not able to connect correctly.  All I ever get is a dhcp address.  Even if I comment out the fallback to dhcp line, I get a dhcp address.

WHY am I getting a dhcp address on the 172.17.10.1/19 network?  What can I do differently to get my address?  WHO is even requesting the dhcp address?

```

#note... the subnet is 255.255.224.0

gateways_eth0="172.16.10.1/19"

config_172016010001=( "172.16.10.170" )

#fallback_172016010001=( "dhcp" )

routes_172016010001=( "default via 172.16.10.1" )

dnsservers_172016010001="172.16.10.10"

dnsdomain_172016010001="ctsolutions.loc" 

#config_eth0=( "dhcp" )

#fallback_eth0=( "arping" )

#fallback_eth0=( "dhcp" ) 

#-----------------------

# OLD SETTINGS, ALL COMMENTED OUT #

#-----------------------

#config_eth0=( "172.16.10.170 netmask 255.255.224.0 brd 172.16.31.255" )

#routes_eth0=( "default gw 172.16.0.1" )

modules=( "wpa_supplicant" )

wpa_supplicant_eth1="-Dwext"
```

----------

## UberLord

 *frozenJim wrote:*   

> WHY am I getting a dhcp address on the 172.17.10.1/19 network?  What can I do differently to get my address?  WHO is even requesting the dhcp address?

 

You've commented out config_eth0 so it will default to dhcp. This is probably what you need

```
config_eth0=( "arping" )

fallback_eth0=( "dhcp" )
```

----------

## frozenJim

'tis solved.  As soon as I have  few free minutes, I will write this up in the Gentoo-Wiki.  Man you were so right about everyone turning green with envy when you just plug it in and get connected.

This truly is a development!  No matter where i go, the laptop decides to assign my IP, assigns my the next avail IP or uses dhcp depending on the network.  COOL! 

Is it my imagination, or is it really showing intelligence in determining whether to use the wireless or Cat5 connection based on speed?  It seems that whenever I have two connections concurrently, my laptop chooses to use the faster of the two.

----------

## UberLord

baselayout-1.12 assigned higher metrics to wireless, vpn and ppp devices so we do prefer the faster ones by default  :Smile: 

----------

