summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorClaudio Jeker <claudio@cvs.openbsd.org>2019-02-21 11:17:23 +0000
committerClaudio Jeker <claudio@cvs.openbsd.org>2019-02-21 11:17:23 +0000
commit749dca8d033835696e29b9b8a22b44a13eed7525 (patch)
treeb55919a3a91530f49fbba53a594ecd519762e383 /usr.sbin
parent4b1048e02917d21e3a041c1c26de72b48d013567 (diff)
Media and link states are highly OS dependent, to make porting easier
export the interface info in a way that does not need OS specific functions to print it. Link state and media are now strings that are set by bgpd. bgpctl can just print them. Move get_linkstate and get_media_descr to kroute.c where all other system specific stuff is. OK sthen@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/bgpd/bgpd.h23
-rw-r--r--usr.sbin/bgpd/kroute.c86
-rw-r--r--usr.sbin/bgpd/util.c68
3 files changed, 107 insertions, 70 deletions
diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h
index e2c0795b346..07a57e44f81 100644
--- a/usr.sbin/bgpd/bgpd.h
+++ b/usr.sbin/bgpd/bgpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: bgpd.h,v 1.373 2019/02/19 09:13:23 claudio Exp $ */
+/* $OpenBSD: bgpd.h,v 1.374 2019/02/21 11:17:22 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -656,15 +656,25 @@ struct pftable_msg {
u_int8_t len;
};
+struct ctl_show_interface {
+ char ifname[IFNAMSIZ];
+ char linkstate[32];
+ char media[32];
+ u_int64_t baudrate;
+ u_int rdomain;
+ u_int8_t nh_reachable;
+ u_int8_t is_up;
+};
+
struct ctl_show_nexthop {
- struct bgpd_addr addr;
- struct kif kif;
+ struct bgpd_addr addr;
+ struct ctl_show_interface iface;
union {
struct kroute kr4;
struct kroute6 kr6;
} kr;
- u_int8_t valid;
- u_int8_t krvalid;
+ u_int8_t valid;
+ u_int8_t krvalid;
};
struct ctl_neighbor {
@@ -1292,9 +1302,6 @@ sa_family_t aid2af(u_int8_t);
int af2aid(sa_family_t, u_int8_t, u_int8_t *);
struct sockaddr *addr2sa(struct bgpd_addr *, u_int16_t, socklen_t *);
void sa2addr(struct sockaddr *, struct bgpd_addr *);
-uint64_t ift2ifm(uint8_t);
-const char * get_media_descr(uint64_t);
-const char * get_linkstate(uint8_t, int);
const char * get_baudrate(unsigned long long, char *);
static const char * const log_procnames[] = {
diff --git a/usr.sbin/bgpd/kroute.c b/usr.sbin/bgpd/kroute.c
index a2b2bdcecc3..1705803459b 100644
--- a/usr.sbin/bgpd/kroute.c
+++ b/usr.sbin/bgpd/kroute.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kroute.c,v 1.232 2019/02/18 09:58:19 claudio Exp $ */
+/* $OpenBSD: kroute.c,v 1.233 2019/02/21 11:17:22 claudio Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -26,6 +26,7 @@
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_dl.h>
+#include <net/if_media.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netmpls/mpls.h>
@@ -171,6 +172,9 @@ int protect_lo(struct ktable *);
u_int8_t prefixlen_classful(in_addr_t);
u_int8_t mask2prefixlen(in_addr_t);
u_int8_t mask2prefixlen6(struct sockaddr_in6 *);
+uint64_t ift2ifm(uint8_t);
+const char *get_media_descr(uint64_t);
+const char *get_linkstate(uint8_t, int);
void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
void if_change(u_short, int, struct if_data *, u_int);
void if_announce(void *, u_int);
@@ -1011,6 +1015,30 @@ kr_nexthop_delete(u_int rtableid, struct bgpd_addr *addr,
knexthop_remove(kt, kn);
}
+static struct ctl_show_interface *
+kr_show_interface(struct kif *kif)
+{
+ static struct ctl_show_interface iface;
+ uint64_t ifms_type;
+
+ bzero(&iface, sizeof(iface));
+ strlcpy(iface.ifname, kif->ifname, sizeof(iface.ifname));
+
+ snprintf(iface.linkstate, sizeof(iface.linkstate),
+ "%s", get_linkstate(kif->if_type, kif->link_state));
+
+ if ((ifms_type = ift2ifm(kif->if_type)) != 0)
+ snprintf(iface.media, sizeof(iface.media),
+ "%s", get_media_descr(ifms_type));
+
+ iface.baudrate = kif->baudrate;
+ iface.rdomain = kif->rdomain;
+ iface.nh_reachable = kif->nh_reachable;
+ iface.is_up = (kif->flags & IFF_UP) == IFF_UP;
+
+ return &iface;
+}
+
void
kr_show_route(struct imsg *imsg)
{
@@ -1125,8 +1153,9 @@ kr_show_route(struct imsg *imsg)
break;
}
if ((kif = kif_find(ifindex)) != NULL)
- memcpy(&snh.kif, &kif->k,
- sizeof(snh.kif));
+ memcpy(&snh.iface,
+ kr_show_interface(&kif->k),
+ sizeof(snh.iface));
}
send_imsg_session(IMSG_CTL_SHOW_NEXTHOP, imsg->hdr.pid,
&snh, sizeof(snh));
@@ -1135,7 +1164,8 @@ kr_show_route(struct imsg *imsg)
case IMSG_CTL_SHOW_INTERFACE:
RB_FOREACH(kif, kif_tree, &kit)
send_imsg_session(IMSG_CTL_SHOW_INTERFACE,
- imsg->hdr.pid, &kif->k, sizeof(kif->k));
+ imsg->hdr.pid, kr_show_interface(&kif->k),
+ sizeof(struct ctl_show_interface));
break;
case IMSG_CTL_SHOW_FIB_TABLES:
for (i = 0; i < krt_size; i++) {
@@ -2629,6 +2659,54 @@ prefixlen2mask6(u_int8_t prefixlen)
return (&mask);
}
+const struct if_status_description
+ if_status_descriptions[] = LINK_STATE_DESCRIPTIONS;
+const struct ifmedia_description
+ ifm_type_descriptions[] = IFM_TYPE_DESCRIPTIONS;
+
+uint64_t
+ift2ifm(uint8_t if_type)
+{
+ switch (if_type) {
+ case IFT_ETHER:
+ return (IFM_ETHER);
+ case IFT_FDDI:
+ return (IFM_FDDI);
+ case IFT_CARP:
+ return (IFM_CARP);
+ case IFT_IEEE80211:
+ return (IFM_IEEE80211);
+ default:
+ return (0);
+ }
+}
+
+const char *
+get_media_descr(uint64_t media_type)
+{
+ const struct ifmedia_description *p;
+
+ for (p = ifm_type_descriptions; p->ifmt_string != NULL; p++)
+ if (media_type == p->ifmt_word)
+ return (p->ifmt_string);
+
+ return ("unknown media");
+}
+
+const char *
+get_linkstate(uint8_t if_type, int link_state)
+{
+ const struct if_status_description *p;
+ static char buf[8];
+
+ for (p = if_status_descriptions; p->ifs_string != NULL; p++) {
+ if (LINK_STATE_DESC_MATCH(p, if_type, link_state))
+ return (p->ifs_string);
+ }
+ snprintf(buf, sizeof(buf), "[#%d]", link_state);
+ return (buf);
+}
+
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
diff --git a/usr.sbin/bgpd/util.c b/usr.sbin/bgpd/util.c
index 07ef03acff1..ed1e406662d 100644
--- a/usr.sbin/bgpd/util.c
+++ b/usr.sbin/bgpd/util.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: util.c,v 1.45 2019/02/18 12:35:08 claudio Exp $ */
+/* $OpenBSD: util.c,v 1.46 2019/02/21 11:17:22 claudio Exp $ */
/*
* Copyright (c) 2006 Claudio Jeker <claudio@openbsd.org>
@@ -18,9 +18,6 @@
*/
#include <sys/types.h>
#include <sys/socket.h>
-#include <net/if.h>
-#include <net/if_media.h>
-#include <net/if_types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
@@ -888,68 +885,23 @@ sa2addr(struct sockaddr *sa, struct bgpd_addr *addr)
}
}
-const struct if_status_description
- if_status_descriptions[] = LINK_STATE_DESCRIPTIONS;
-const struct ifmedia_description
- ifm_type_descriptions[] = IFM_TYPE_DESCRIPTIONS;
-
-uint64_t
-ift2ifm(uint8_t if_type)
-{
- switch (if_type) {
- case IFT_ETHER:
- return (IFM_ETHER);
- case IFT_FDDI:
- return (IFM_FDDI);
- case IFT_CARP:
- return (IFM_CARP);
- case IFT_IEEE80211:
- return (IFM_IEEE80211);
- default:
- return (0);
- }
-}
-
-const char *
-get_media_descr(uint64_t media_type)
-{
- const struct ifmedia_description *p;
-
- for (p = ifm_type_descriptions; p->ifmt_string != NULL; p++)
- if (media_type == p->ifmt_word)
- return (p->ifmt_string);
-
- return ("unknown media");
-}
-
-const char *
-get_linkstate(uint8_t if_type, int link_state)
-{
- const struct if_status_description *p;
- static char buf[8];
-
- for (p = if_status_descriptions; p->ifs_string != NULL; p++) {
- if (LINK_STATE_DESC_MATCH(p, if_type, link_state))
- return (p->ifs_string);
- }
- snprintf(buf, sizeof(buf), "[#%d]", link_state);
- return (buf);
-}
-
const char *
get_baudrate(unsigned long long baudrate, char *unit)
{
static char bbuf[16];
+ const unsigned long long kilo = 1000;
+ const unsigned long long mega = 1000ULL * kilo;
+ const unsigned long long giga = 1000ULL * mega;
- if (baudrate > IF_Gbps(1))
+ if (baudrate > giga)
snprintf(bbuf, sizeof(bbuf), "%llu G%s",
- baudrate / IF_Gbps(1), unit);
- else if (baudrate > IF_Mbps(1))
+ baudrate / giga, unit);
+ else if (baudrate > mega)
snprintf(bbuf, sizeof(bbuf), "%llu M%s",
- baudrate / IF_Mbps(1), unit);
- else if (baudrate > IF_Kbps(1))
+ baudrate / mega, unit);
+ else if (baudrate > kilo)
snprintf(bbuf, sizeof(bbuf), "%llu K%s",
- baudrate / IF_Kbps(1), unit);
+ baudrate / kilo, unit);
else
snprintf(bbuf, sizeof(bbuf), "%llu %s",
baudrate, unit);