This diff makes route get and route monitor work.  sockaddr_bfd is so we
can play like the other RTAX_* indexes in rti_info of route messages.

OK?


$ route -n monitor                                            
got message of size 128 on Wed Sep 28 21:35:32 2016
RTM_BFD: bidirectional forwarding detection: len 128
BFD mode async state down remotestate down laststate admindown error 0
localdiscr 4002401056 remotediscr 0 localdiag none remotediag none
uptime 04m46s lastuptime 00s mintx 1000000 minrx 1 minecho 0 multiplier 3

sockaddrs: <DST,IFA>
 192.168.50.1 192.168.50.61

$ route -n get 192.168.50.1
   route to: 192.168.50.1
destination: 192.168.50.1
       mask: 255.255.255.255
  interface: vio0
 if address: 192.168.50.61
   priority: 4 (connected)
      flags: <UP,HOST,DONE,LLINFO,CLONED,CACHED,BFD>
BFD mode async state down remotestate down laststate admindown error 0
localdiscr 4002401056 remotediscr 0 localdiag none remotediag none
uptime 05m13s lastuptime 00s mintx 1000000 minrx 1 minecho 0 multiplier 3
     use       mtu    expire
       8         0       879 
sockaddrs: <DST,GATEWAY,NETMASK,IFP,IFA>





Index: sbin/route/route.c
===================================================================
RCS file: /cvs/openbsd/src/sbin/route/route.c,v
retrieving revision 1.192
diff -u -p -u -p -r1.192 route.c
--- sbin/route/route.c  24 Sep 2016 19:36:49 -0000      1.192
+++ sbin/route/route.c  30 Sep 2016 08:09:48 -0000
@@ -100,6 +100,7 @@ const char *bfd_state(unsigned int);
 const char *bfd_diag(unsigned int);
 const char *bfd_calc_uptime(time_t);
 void    print_bfdmsg(struct rt_msghdr *);
+void    print_sabfd(struct sockaddr_bfd *);
 #endif
 const char *get_linkstate(int, int);
 void    print_rtmsg(struct rt_msghdr *, int);
@@ -1439,6 +1440,9 @@ print_getmsg(struct rt_msghdr *rtm, int 
        struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL, *ifa = NULL;
        struct sockaddr_dl *ifp = NULL;
        struct sockaddr_rtlabel *sa_rl = NULL;
+#ifdef BFD
+       struct sockaddr_bfd *sa_bfd = NULL;
+#endif
        struct sockaddr *mpls = NULL;
        struct sockaddr *sa;
        char *cp;
@@ -1487,6 +1491,11 @@ print_getmsg(struct rt_msghdr *rtm, int 
                                case RTA_LABEL:
                                        sa_rl = (struct sockaddr_rtlabel *)sa;
                                        break;
+#ifdef BFD
+                               case RTA_BFD:
+                                       sa_bfd = (struct sockaddr_bfd *)sa;
+                                       break;
+#endif
                                }
                                ADVANCE(cp, sa);
                        }
@@ -1519,6 +1528,10 @@ print_getmsg(struct rt_msghdr *rtm, int 
        printf("\n");
        if (sa_rl != NULL)
                printf("      label: %s\n", sa_rl->sr_label);
+#ifdef BFD
+       if (sa_bfd)
+               print_sabfd(sa_bfd);
+#endif
 
 #define lock(f)        ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' 
')
        relative_expire = rtm->rtm_rmx.rmx_expire ?
@@ -1621,12 +1634,21 @@ void
 print_bfdmsg(struct rt_msghdr *rtm)
 {
        struct bfd_msghdr *bfdm = (struct bfd_msghdr *)rtm;
+
+       printf("\n");
+       print_sabfd(&bfdm->bm_sa);
+       pmsg_addrs(((char *)rtm + rtm->rtm_hdrlen), rtm->rtm_addrs);
+}
+
+void
+print_sabfd(struct sockaddr_bfd *sa_bfd)
+{
        struct timeval tv;
 
        gettimeofday(&tv, NULL);
 
-       printf(" mode ");
-       switch (bfdm->bm_mode) {
+       printf("BFD mode ");
+       switch (sa_bfd->bs_mode) {
        case BFD_MODE_ASYNC:
                printf("async");
                break;
@@ -1634,27 +1656,26 @@ print_bfdmsg(struct rt_msghdr *rtm)
                printf("demand");
                break;
        default:
-               printf("unknown %u", bfdm->bm_mode);
+               printf("unknown %u", sa_bfd->bs_mode);
                break;
        }
-       printf(" state %s", bfd_state(bfdm->bm_state));
-       printf(" remotestate %s", bfd_state(bfdm->bm_remotestate));
-       printf(" laststate %s", bfd_state(bfdm->bm_laststate));
-
-       printf(" error %d", bfdm->bm_error);
-       printf(" localdiscr %u", bfdm->bm_localdiscr);
-       printf(" remotediscr %u", bfdm->bm_remotediscr);
-       printf(" localdiag %s", bfd_diag(bfdm->bm_localdiag));
-       printf(" remotediag %s", bfd_diag(bfdm->bm_remotediag));
-       printf(" uptime %s", bfd_calc_uptime(tv.tv_sec - bfdm->bm_uptime));
-       printf(" lastuptime %s", bfd_calc_uptime(bfdm->bm_lastuptime));
-
-       printf(" mintx %u", bfdm->bm_mintx);
-       printf(" minrx %u", bfdm->bm_minrx);
-       printf(" minecho %u", bfdm->bm_minecho);
-       printf(" multiplier %u", bfdm->bm_multiplier);
-
-       pmsg_addrs(((char *)rtm + rtm->rtm_hdrlen), rtm->rtm_addrs);
+       printf(" state %s", bfd_state(sa_bfd->bs_state));
+       printf(" remotestate %s", bfd_state(sa_bfd->bs_remotestate));
+       printf(" laststate %s", bfd_state(sa_bfd->bs_laststate));
+
+       printf(" error %d", sa_bfd->bs_error);
+       printf(" localdiscr %u", sa_bfd->bs_localdiscr);
+       printf(" remotediscr %u", sa_bfd->bs_remotediscr);
+       printf(" localdiag %s", bfd_diag(sa_bfd->bs_localdiag));
+       printf(" remotediag %s", bfd_diag(sa_bfd->bs_remotediag));
+       printf(" uptime %s", bfd_calc_uptime(tv.tv_sec - sa_bfd->bs_uptime));
+       printf(" lastuptime %s", bfd_calc_uptime(sa_bfd->bs_lastuptime));
+
+       printf(" mintx %u", sa_bfd->bs_mintx);
+       printf(" minrx %u", sa_bfd->bs_minrx);
+       printf(" minecho %u", sa_bfd->bs_minecho);
+       printf(" multiplier %u", sa_bfd->bs_multiplier);
+       printf("\n");
 }
 #endif /* BFD */
 
Index: sys/net/bfd.c
===================================================================
RCS file: /cvs/openbsd/src/sys/net/bfd.c,v
retrieving revision 1.41
diff -u -p -u -p -r1.41 bfd.c
--- sys/net/bfd.c       24 Sep 2016 19:29:55 -0000      1.41
+++ sys/net/bfd.c       30 Sep 2016 08:10:16 -0000
@@ -144,7 +144,6 @@ struct pool  bfd_pool, bfd_pool_neigh, b
 struct taskq   *bfdtq;
 
 
-struct bfd_config *bfd_lookup(struct rtentry *);
 void            bfddestroy(void);
 
 struct socket  *bfd_listener(struct bfd_config *, unsigned int);
Index: sys/net/bfd.h
===================================================================
RCS file: /cvs/openbsd/src/sys/net/bfd.h,v
retrieving revision 1.9
diff -u -p -u -p -r1.9 bfd.h
--- sys/net/bfd.h       20 Sep 2016 10:41:43 -0000      1.9
+++ sys/net/bfd.h       30 Sep 2016 07:49:02 -0000
@@ -53,31 +53,44 @@
 #define BFD_FLAG_D                     0x02
 #define BFD_FLAG_M                     0x01
 
+struct sockaddr_bfd {
+       __uint8_t       bs_len;         /* total length */
+       sa_family_t     bs_family;      /* address family */
+       /* above matches sockaddr_storage */
+
+       uint16_t        bs_mode;
+       uint32_t        bs_mintx;
+       uint32_t        bs_minrx;
+       uint32_t        bs_minecho;
+       uint16_t        bs_multiplier;
+
+       time_t          bs_uptime;
+       time_t          bs_lastuptime;
+       int             bs_state;
+       int             bs_remotestate;
+       int             bs_laststate;
+       int             bs_error;
+
+       uint32_t        bs_localdiscr;
+       uint32_t        bs_localdiag;
+       uint32_t        bs_remotediscr;
+       uint32_t        bs_remotediag;
+};
+
 struct bfd_msghdr {
        unsigned short  bm_msglen;
        unsigned char   bm_version;
        unsigned char   bm_type;
        unsigned short  bm_hdrlen;
+       u_short         bm_index;
+       u_short         bm_tableid;
+       u_char          bm_priority;
+       u_char          bm_mpls;
        int             bm_addrs;
+       int             bm_flags;
        /* above matches rtm_msghdr */
 
-       uint16_t        bm_mode;
-       uint32_t        bm_mintx;
-       uint32_t        bm_minrx;
-       uint32_t        bm_minecho;
-       uint16_t        bm_multiplier;
-
-       time_t          bm_uptime;
-       time_t          bm_lastuptime;
-       int             bm_state;
-       int             bm_remotestate;
-       int             bm_laststate;
-       int             bm_error;
-
-       uint32_t        bm_localdiscr;
-       uint32_t        bm_localdiag;
-       uint32_t        bm_remotediscr;
-       uint32_t        bm_remotediag;
+       struct sockaddr_bfd     bm_sa;  /* bfd msg for userland */
 };
 
 #ifdef _KERNEL
@@ -124,6 +137,8 @@ struct bfd_config {
        int                      bc_minecho;
        int                      bc_multiplier;
 };
+
+struct bfd_config *bfd_lookup(struct rtentry *);
 
 int             bfdset(struct rtentry *);
 void            bfdclear(struct rtentry *);
Index: sys/net/route.h
===================================================================
RCS file: /cvs/openbsd/src/sys/net/route.h,v
retrieving revision 1.148
diff -u -p -u -p -r1.148 route.h
--- sys/net/route.h     24 Sep 2016 19:27:10 -0000      1.148
+++ sys/net/route.h     29 Sep 2016 18:55:21 -0000
@@ -259,6 +259,7 @@ struct rt_msghdr {
 #define RTA_SRC                0x100   /* source sockaddr present */
 #define RTA_SRCMASK    0x200   /* source netmask present */
 #define        RTA_LABEL       0x400   /* route label present */
+#define RTA_BFD                0x800   /* bfd present */
 
 /*
  * Index offsets for sockaddr array for alternate internal encoding.
@@ -274,7 +275,8 @@ struct rt_msghdr {
 #define RTAX_SRC       8       /* source sockaddr present */
 #define RTAX_SRCMASK   9       /* source netmask present */
 #define RTAX_LABEL     10      /* route label present */
-#define RTAX_MAX       11      /* size of array to allocate */
+#define RTAX_BFD       11      /* bfd present */
+#define RTAX_MAX       12      /* size of array to allocate */
 
 /*
  * setsockopt defines used for the filtering.
@@ -357,6 +359,7 @@ struct socket;
 struct ifnet;
 struct sockaddr_in6;
 struct bfd_config;
+struct sockaddr_bfd;
 
 void    route_init(void);
 int     route_output(struct mbuf *, ...);
@@ -365,6 +368,7 @@ int  route_usrreq(struct socket *, int, 
 void    rt_ifmsg(struct ifnet *);
 void    rt_ifannouncemsg(struct ifnet *, int);
 void    rt_bfdmsg(struct bfd_config *);
+struct sockaddr *bfd2sa(struct bfd_config *, struct sockaddr_bfd *);
 void    rt_maskedcopy(struct sockaddr *,
            struct sockaddr *, struct sockaddr *);
 struct sockaddr *rt_plen2mask(struct rtentry *, struct sockaddr_in6 *);
Index: sys/net/rtsock.c
===================================================================
RCS file: /cvs/openbsd/src/sys/net/rtsock.c,v
retrieving revision 1.206
diff -u -p -u -p -r1.206 rtsock.c
--- sys/net/rtsock.c    24 Sep 2016 19:27:10 -0000      1.206
+++ sys/net/rtsock.c    30 Sep 2016 07:58:02 -0000
@@ -464,6 +464,9 @@ route_output(struct mbuf *m, ...)
        struct rawcb            *rp = NULL;
        struct sockaddr_rtlabel  sa_rl;
        struct sockaddr_in6      sa_mask;
+#ifdef BFD
+       struct sockaddr_bfd      sa_bfd;
+#endif
 #ifdef MPLS
        struct sockaddr_mpls     sa_mpls, *psa_mpls;
 #endif
@@ -697,6 +700,14 @@ report:
                    rt_plen2mask(rt, &sa_mask);
                info.rti_info[RTAX_LABEL] =
                    rtlabel_id2sa(rt->rt_labelid, &sa_rl);
+#ifdef BFD
+               if (rt->rt_flags & RTF_BFD) {
+                       struct bfd_config *bfd;
+                       bfd = bfd_lookup(rt);
+                       bfd2sa(bfd, &sa_bfd);
+                       info.rti_info[RTAX_BFD] = (struct sockaddr *)&sa_bfd;
+               }
+#endif
 #ifdef MPLS
                if (rt->rt_flags & RTF_MPLS) {
                        bzero(&sa_mpls, sizeof(sa_mpls));
@@ -1347,6 +1358,7 @@ void
 rt_bfdmsg(struct bfd_config *bfd)
 {
        struct bfd_msghdr       *bfdm;
+       struct sockaddr_bfd      sa_bfd;
        struct mbuf             *m;
        struct rt_addrinfo       info;
 
@@ -1360,28 +1372,45 @@ rt_bfdmsg(struct bfd_config *bfd)
        if (m == NULL)
                return;
        bfdm = mtod(m, struct bfd_msghdr *);
+       bfdm->bm_addrs = info.rti_addrs;
 
-       bfdm->bm_mode = bfd->bc_mode;
-       bfdm->bm_mintx = bfd->bc_mintx;
-       bfdm->bm_minrx = bfd->bc_minrx;
-       bfdm->bm_minecho = bfd->bc_minecho;
-       bfdm->bm_multiplier = bfd->bc_multiplier;
-
-       bfdm->bm_uptime = bfd->bc_time->tv_sec;
-       bfdm->bm_lastuptime = bfd->bc_lastuptime;
-       bfdm->bm_state = bfd->bc_state;
-       bfdm->bm_remotestate = bfd->bc_neighbor->bn_rstate;
-       bfdm->bm_laststate = bfd->bc_laststate;
-       bfdm->bm_error = bfd->bc_error;
-
-       bfdm->bm_localdiscr = bfd->bc_neighbor->bn_ldiscr;
-       bfdm->bm_localdiag = bfd->bc_neighbor->bn_ldiag;
-       bfdm->bm_remotediscr = bfd->bc_neighbor->bn_rdiscr;
-       bfdm->bm_remotediag = bfd->bc_neighbor->bn_rdiag;
+       bfd2sa(bfd, &sa_bfd);
+       memcpy(&bfdm->bm_sa, &sa_bfd, sizeof(sa_bfd));
 
-       route_proto.sp_protocol = 0;
+       route_proto.sp_protocol = info.rti_info[RTAX_DST]->sa_family;
        route_input(m, &route_proto, &route_src, &route_dst);
 }
+
+struct sockaddr *
+bfd2sa(struct bfd_config *bfd, struct sockaddr_bfd *sa_bfd)
+{
+       if (bfd == NULL)
+               return (NULL);
+
+       memset(sa_bfd, 0, sizeof(*sa_bfd));
+       sa_bfd->bs_len = sizeof(*sa_bfd);
+       sa_bfd->bs_family = bfd->bc_rt->rt_dest->sa_family;
+
+       sa_bfd->bs_mode = bfd->bc_mode;
+       sa_bfd->bs_mintx = bfd->bc_mintx;
+       sa_bfd->bs_minrx = bfd->bc_minrx;
+       sa_bfd->bs_minecho = bfd->bc_minecho;
+       sa_bfd->bs_multiplier = bfd->bc_multiplier;
+
+       sa_bfd->bs_uptime = bfd->bc_time->tv_sec;
+       sa_bfd->bs_lastuptime = bfd->bc_lastuptime;
+       sa_bfd->bs_state = bfd->bc_state;
+       sa_bfd->bs_remotestate = bfd->bc_neighbor->bn_rstate;
+       sa_bfd->bs_laststate = bfd->bc_laststate;
+       sa_bfd->bs_error = bfd->bc_error;
+
+       sa_bfd->bs_localdiscr = bfd->bc_neighbor->bn_ldiscr;
+       sa_bfd->bs_localdiag = bfd->bc_neighbor->bn_ldiag;
+       sa_bfd->bs_remotediscr = bfd->bc_neighbor->bn_rdiscr;
+       sa_bfd->bs_remotediag = bfd->bc_neighbor->bn_rdiag;
+
+       return ((struct sockaddr *)sa_bfd);
+}
 #endif /* BFD */
 
 /*
@@ -1394,6 +1423,10 @@ sysctl_dumpentry(struct rtentry *rt, voi
        int                      error = 0, size;
        struct rt_addrinfo       info;
        struct ifnet            *ifp;
+#ifdef BFD
+       struct bfd_config       *bfd;
+       struct sockaddr_bfd      sa_bfd;
+#endif
 #ifdef MPLS
        struct sockaddr_mpls     sa_mpls;
 #endif
@@ -1428,6 +1461,12 @@ sysctl_dumpentry(struct rtentry *rt, voi
        }
        if_put(ifp);
        info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl);
+#ifdef BFD
+       if (rt->rt_flags & RTF_BFD) {
+               bfd = bfd_lookup(rt);
+               info.rti_info[RTAX_BFD] = bfd2sa(bfd, &sa_bfd);
+       }
+#endif
 #ifdef MPLS
        if (rt->rt_flags & RTF_MPLS) {
                bzero(&sa_mpls, sizeof(sa_mpls));


-- 
Non-Reciprocal Laws of Expectations:
        Negative expectations yield negative results.
        Positive expectations yield negative results.

Reply via email to