Hello,

Here is a patch to route(8) to automatically choose the right address family
when adding a default route using the "default" keyword.

Index: route.8
===================================================================
RCS file: /cvs/src/sbin/route/route.8,v
retrieving revision 1.79
diff -u -p -r1.79 route.8
--- route.8     1 Jan 2017 01:08:11 -0000       1.79
+++ route.8     28 Jul 2017 13:41:24 -0000
@@ -317,7 +317,8 @@ data, in hexadecimal format
 In the absence of modifiers, an address is assumed to be IPv4,
 unless containing a
 .Sq :\&
-character, when it is treated as IPv6.
+character, when it is treated as IPv6. When using the "default" keyword,
+the address family used is that of the gateway.
 .Pp
 The optional modifier
 .Fl link
Index: route.c
===================================================================
RCS file: /cvs/src/sbin/route/route.c,v
retrieving revision 1.200
diff -u -p -r1.200 route.c
--- route.c     23 Mar 2017 13:28:25 -0000      1.200
+++ route.c     28 Jul 2017 13:41:24 -0000
@@ -72,7 +72,7 @@ union sockunion so_dst, so_gate, so_mask
 
 typedef union sockunion *sup;
 pid_t  pid;
-int    rtm_addrs, s;
+int    rtm_addrs, s, defroute = 0;
 int    forcehost, forcenet, Fflag, nflag, af, qflag, tflag, Tflag;
 int    iflag, verbose, aflen = sizeof(struct sockaddr_in);
 int    locking, lockrest, debugonly;
@@ -875,6 +875,10 @@ getaddr(int which, char *s, struct hoste
                break;
        case RTA_GATEWAY:
                su = &so_gate;
+               if (defroute) {
+                       so_dst.sa.sa_len = aflen;
+                       so_dst.sa.sa_family = af;
+               }
                break;
        case RTA_NETMASK:
                su = &so_mask;
@@ -901,6 +905,8 @@ getaddr(int which, char *s, struct hoste
                        break;
                case RTA_NETMASK:
                        su->sa.sa_len = 0;
+                       af = 0;
+                       defroute = 1;
                }
                return (0);
        }

Reply via email to