On 2017 Jan 19 (Thu) at 06:26:25 +0100 (+0100), Peter Hessler wrote:
:On 2016 Dec 17 (Sat) at 14:05:40 +0100 (+0100), Peter Hessler wrote:
::On 2016 Sep 30 (Fri) at 10:16:19 +0200 (+0200), Peter Hessler wrote:
:::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.
:::
:
:In route(8), only say "up" or "down" for the state of BFD.  use -v or
:-bfd to get details that only matter to debug BFD.
:
:$ route -n get 203.0.113.9       
:   route to: 203.0.113.9
:destination: 203.0.113.9
:       mask: 255.255.255.255
:  interface: em1
: if address: 203.0.113.1
:   priority: 3 ()
:      flags: <UP,HOST,DONE,LLINFO,CLONED,BFD>
:        BFD: up
:     use       mtu    expire
:    1402         0       922 
:sockaddrs: <DST,GATEWAY,NETMASK,IFP,IFA>
:
:I also fixed a number of things that mpi@ noticed.
:

And now with sockaddr_bfd (and bfd_msghdr for that matter) adjusted for
4 byte boundaries and sized to a power of two.


Index: sbin/route/Makefile
===================================================================
RCS file: /cvs/openbsd/src/sbin/route/Makefile,v
retrieving revision 1.13
diff -u -p -u -p -r1.13 Makefile
--- sbin/route/Makefile 19 Jul 2013 14:41:46 -0000      1.13
+++ sbin/route/Makefile 17 Dec 2016 12:47:35 -0000
@@ -4,7 +4,7 @@ PROG=   route
 MAN=   route.8
 SRCS=  route.c show.c
 
-CFLAGS+=       -Wall
+CFLAGS+=       -Wall -DBFD
 
 route.o .depend lint tags: keywords.h
 
Index: sbin/route/route.c
===================================================================
RCS file: /cvs/openbsd/src/sbin/route/route.c,v
retrieving revision 1.194
diff -u -p -u -p -r1.194 route.c
--- sbin/route/route.c  17 Jan 2017 19:05:47 -0000      1.194
+++ sbin/route/route.c  19 Jan 2017 03:39:55 -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 *, int);
 #endif
 const char *get_linkstate(int, int);
 void    print_rtmsg(struct rt_msghdr *, int);
@@ -1444,6 +1445,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;
@@ -1492,6 +1496,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);
                        }
@@ -1524,6 +1533,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, rtm->rtm_fmask);
+#endif
 
 #define lock(f)        ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' 
')
        relative_expire = rtm->rtm_rmx.rmx_expire ?
@@ -1626,40 +1639,61 @@ void
 print_bfdmsg(struct rt_msghdr *rtm)
 {
        struct bfd_msghdr *bfdm = (struct bfd_msghdr *)rtm;
+
+       printf("\n");
+       print_sabfd(&bfdm->bm_sa, rtm->rtm_fmask);
+       pmsg_addrs(((char *)rtm + rtm->rtm_hdrlen), rtm->rtm_addrs);
+}
+
+void
+print_sabfd(struct sockaddr_bfd *sa_bfd, int fmask)
+{
        struct timeval tv;
 
        gettimeofday(&tv, NULL);
 
-       printf(" mode ");
-       switch (bfdm->bm_mode) {
+       printf("        BFD:");
+
+       /* only show the state, unless verbose or -bfd */
+       if (!verbose && ((fmask & RTF_BFD) != RTF_BFD)) {
+               printf(" %s\n", bfd_state(sa_bfd->bs_state));
+               return;
+       }
+
+       switch (sa_bfd->bs_mode) {
        case BFD_MODE_ASYNC:
-               printf("async");
+               printf(" async");
                break;
        case BFD_MODE_DEMAND:
-               printf("demand");
+               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(" remote %s", bfd_state(sa_bfd->bs_remotestate));
+       printf(" laststate %s", bfd_state(sa_bfd->bs_laststate));
+
+       printf(" error %d", sa_bfd->bs_error);
+       printf("\n            ");
+       printf(" diag %s", bfd_diag(sa_bfd->bs_localdiag));
+       printf(" remote %s", bfd_diag(sa_bfd->bs_remotediag));
+       printf("\n            ");
+       printf(" discr %u", sa_bfd->bs_localdiscr);
+       printf(" remote %u", sa_bfd->bs_remotediscr);
+       printf("\n            ");
+       printf(" uptime %s", bfd_calc_uptime(tv.tv_sec - sa_bfd->bs_uptime));
+       if (sa_bfd->bs_lastuptime)
+               printf(" last state time %s",
+                   bfd_calc_uptime(sa_bfd->bs_lastuptime));
+       printf("\n            ");
+       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.42
diff -u -p -u -p -r1.42 bfd.c
--- sys/net/bfd.c       12 Jan 2017 16:14:42 -0000      1.42
+++ sys/net/bfd.c       19 Jan 2017 08:22:27 -0000
@@ -309,6 +309,41 @@ bfd_lookup(struct rtentry *rt)
        return (NULL);
 }
 
+struct sockaddr *
+bfd2sa(struct rtentry *rt, struct sockaddr_bfd *sa_bfd)
+{
+       struct bfd_config *bfd;
+
+       bfd = bfd_lookup(rt);
+
+       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);
+}
+
 /*
  * End of public interfaces.
  *
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       19 Jan 2017 08:20:01 -0000
@@ -53,31 +53,55 @@
 #define BFD_FLAG_D                     0x02
 #define BFD_FLAG_M                     0x01
 
+struct sockaddr_bfd {
+       uint8_t         bs_len;         /* total length */
+       uint8_t         bs_family;      /* address family */
+       /* above matches sockaddr_storage */
+
+       /* Sorted for bit boundaries */
+       uint16_t        bs_mode;
+       uint32_t        bs_localdiscr;
+
+       int64_t         bs_uptime;
+
+       int64_t         bs_lastuptime;
+
+       uint32_t        bs_mintx;
+       uint32_t        bs_minrx;
+
+       uint32_t        bs_minecho;
+       uint32_t        bs_localdiag;
+
+       uint32_t        bs_remotediscr;
+       uint32_t        bs_remotediag;
+
+       uint16_t        bs_multiplier;
+       int             bs_state;
+       int             bs_remotestate;
+       int             bs_laststate;
+       int             bs_error;
+       /* add padding to reach a power of two */
+       uint16_t        bs_pad0;
+
+       uint64_t        bs_pad1;
+};
+
 struct bfd_msghdr {
-       unsigned short  bm_msglen;
-       unsigned char   bm_version;
-       unsigned char   bm_type;
-       unsigned short  bm_hdrlen;
+       uint16_t        bm_msglen;
+       uint_t          bm_version;
+       uint_t          bm_type;
+       uint16_t        bm_hdrlen;
+       uint16_t        bm_index;
+
+       uint16_t        bm_tableid;
+       uint8_t         bm_priority;
+       uint8_t         bm_mpls;
        int             bm_addrs;
-       /* above matches rtm_msghdr */
+       int             bm_flags;
+       /* above matches rt_msghdr */
+       uint16_t        bm_pad0;        /* for 4 byte boundary */
 
-       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 +148,8 @@ struct bfd_config {
        int                      bc_minecho;
        int                      bc_multiplier;
 };
+
+struct sockaddr *bfd2sa(struct rtentry *, struct sockaddr_bfd *);
 
 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.150
diff -u -p -u -p -r1.150 route.h
--- sys/net/route.h     19 Jan 2017 02:55:18 -0000      1.150
+++ sys/net/route.h     19 Jan 2017 03:14:07 -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.
Index: sys/net/rtsock.c
===================================================================
RCS file: /cvs/openbsd/src/sys/net/rtsock.c,v
retrieving revision 1.212
diff -u -p -u -p -r1.212 rtsock.c
--- sys/net/rtsock.c    20 Dec 2016 18:33:43 -0000      1.212
+++ sys/net/rtsock.c    17 Jan 2017 08:15:37 -0000
@@ -472,6 +472,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
@@ -705,6 +708,10 @@ 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)
+                       info.rti_info[RTAX_BFD] = bfd2sa(rt, &sa_bfd);
+#endif
 #ifdef MPLS
                if (rt->rt_flags & RTF_MPLS) {
                        bzero(&sa_mpls, sizeof(sa_mpls));
@@ -1355,6 +1362,7 @@ void
 rt_bfdmsg(struct bfd_config *bfd)
 {
        struct bfd_msghdr       *bfdm;
+       struct sockaddr_bfd      sa_bfd;
        struct mbuf             *m;
        struct rt_addrinfo       info;
 
@@ -1368,26 +1376,12 @@ 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->bc_rt, &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);
 }
 #endif /* BFD */
@@ -1402,6 +1396,9 @@ sysctl_dumpentry(struct rtentry *rt, voi
        int                      error = 0, size;
        struct rt_addrinfo       info;
        struct ifnet            *ifp;
+#ifdef BFD
+       struct sockaddr_bfd      sa_bfd;
+#endif
 #ifdef MPLS
        struct sockaddr_mpls     sa_mpls;
 #endif
@@ -1436,6 +1433,10 @@ 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)
+               info.rti_info[RTAX_BFD] = bfd2sa(rt, &sa_bfd);
+#endif
 #ifdef MPLS
        if (rt->rt_flags & RTF_MPLS) {
                bzero(&sa_mpls, sizeof(sa_mpls));

-- 
!07/11 PDP a ni deppart m'I  !pleH

Reply via email to