hi,
I found an interesting issue while toying routing.
route(1) accepts IPv4 destination and IPv6 gateway entry.
command is as below:
    route add 192.0.2.1 2001:db8::1

Curiously it has no error.
The entry is pointless, I think it should teach a mistake.
If destination and gateway address family are not the same, it should return 
error.

Comments?

Index: sbin/route/route.c
===================================================================
RCS file: /cvs/src/sbin/route/route.c,v
retrieving revision 1.254
diff -u -p -r1.254 route.c
--- sbin/route/route.c  12 Mar 2021 19:35:43 -0000      1.254
+++ sbin/route/route.c  26 Aug 2021 09:02:17 -0000
@@ -782,6 +782,9 @@ newroute(int argc, char **argv)
                        break;
        }
        oerrno = errno;
+       if ((rtm_addrs & RTA_GATEWAY) == 0 &&
+           so_dst.sa.sa_family != so_gate.sa.sa_family)
+               errx(1, "address family mismatch");
        if (!qflag && (*cmd != 'g' || ret != 0)) {
                printf("%s %s %s", cmd, ishost ? "host" : "net", dest);
                if (*gateway) {

Index: sys/net/rtsock.c
===================================================================
RCS file: /cvs/src/sys/net/rtsock.c,v
retrieving revision 1.319
diff -u -p -r1.319 rtsock.c
--- sys/net/rtsock.c    23 Jun 2021 16:10:45 -0000      1.319
+++ sys/net/rtsock.c    26 Aug 2021 09:02:01 -0000
@@ -822,6 +822,12 @@ route_output(struct mbuf *m, struct sock
                error = EINVAL;
                goto fail;
        }
+       if ((rtm->rtm_type == RTM_ADD || rtm->rtm_type == RTM_CHANGE) &&
+           info.rti_info[RTAX_DST]->sa_family !=
+           info.rti_info[RTAX_GATEWAY]->sa_family) {
+               error = EINVAL;
+               goto fail;
+       }
#ifdef MPLS
        info.rti_mpls = rtm->rtm_mpls;
#endif


Reply via email to