Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 08 Apr 2024 09:23:21 +0100
From:      Roy Marples <roy@marples.name>
To:        "Anton Yudin" <contact@antonyudin.com>
Cc:        "freebsd-net" <freebsd-net@freebsd.org>
Subject:   Re: How to ignore a default route for one of the dhclient-ed interface?
Message-ID:  <18ebcce010c.c4bc5178125082.198975928787124826@marples.name>
In-Reply-To: <CAAokbZ0t%2BYdU1bjfd%2ByhnTjUw82puWBJHs3rBxwS-%2BkTkERz%2Bg@mail.gmail.com>
References:  <CAAokbZ0t%2BYdU1bjfd%2ByhnTjUw82puWBJHs3rBxwS-%2BkTkERz%2Bg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
 ---- On Mon, 08 Apr 2024 04:16:47 +0100  Anton Yudin  wrote ---=20
 > =C2=A0 I'm running a FreeBSD 14 with two interfaces that use DHCP.=C2=A0=
 I would like to make one of the interfaces to never set the default route.=
=C2=A0 Right now the first interface to be fully up sets the default route.
 >=20
 > =C2=A0 I tried to set the following in /etc/dhclient.conf
 > ---------------8=C2=A0 interface "wan1" {=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =
ignore routers;=C2=A0 }---------------8=C2=A0 but the default route still g=
ets set.
 > =C2=A0 I ended up creating a /etc/dhclient-enter-hooks with a very hacky=
 code that overrides the "route" command:---------------8<-----------------=
-------
 > route() {
 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 if [ "X$interface" =3D "Xwan1" -a "X$2" =3D =
"Xdefault" =C2=A0]; then
 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 echo "ignore rou=
te $1 $2 $3 $4" | logger -t "enter-hooks"
 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 else
 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 /sbin/route $1 $=
2 $3 $4
 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 fi
 > }---------------8
 > Is there a better way of doing this?

Yes there is.

You can use dhcpcd (available in ports) instead where you can prefer an int=
erface by metric.
When both interfaces have a DHCP lease, dhcpcd will prefer the lowest metri=
c and adjust routing and everything else accordingly.
Assuming you have wan0 and wan1 and are ignoring wan1 the chances are that =
dhcpcd will just work for you out of the box.

Otherwise you can tell dhcpcd your preference like so:

interface eth0
    metric 1

Here, eth0 will take preference other any other interface.

OR

You can also remove the option from the DHCP packet before processing
interface wan1
    nooption routers

Hope this helps

Roy



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?18ebcce010c.c4bc5178125082.198975928787124826>