From 0b1a3dc967ea6ed7b2756a5cbed95b4e082c7313 Mon Sep 17 00:00:00 2001 From: benno Date: Mon, 6 Aug 2018 11:42:18 +0000 Subject: [PATCH] make ifconfig join display the list of networks configured for auto-join with feedback from florian and stsp ok florian@ phessler@ (on previous versions of the diff) stsp@ --- sbin/ifconfig/ifconfig.c | 58 ++++++++++++++++++++++++++++++++-- sys/net80211/ieee80211_ioctl.c | 23 +++++++++++++- sys/net80211/ieee80211_ioctl.h | 11 ++++++- 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 9bfb1751aab..19b2d1a3b84 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ifconfig.c,v 1.369 2018/07/13 08:41:32 krw Exp $ */ +/* $OpenBSD: ifconfig.c,v 1.370 2018/08/06 11:42:18 benno Exp $ */ /* $NetBSD: ifconfig.c,v 1.40 1997/10/01 02:19:43 enami Exp $ */ /* @@ -163,6 +163,7 @@ int newaddr = 0; int af = AF_INET; int explicit_prefix = 0; int Lflag = 1; +int show_join = 0; int showmediaflag; int showcapsflag; @@ -633,6 +634,7 @@ void in6_status(int); void in6_getaddr(const char *, int); void in6_getprefix(const char *, int); void ieee80211_status(void); +void join_status(void); void ieee80211_listchans(void); void ieee80211_listnodes(void); void ieee80211_printnode(struct ieee80211_nodereq *); @@ -1656,7 +1658,7 @@ setifjoin(const char *val, int d) int len; if (val == NULL) { - /* TODO: display the list of join'd networks */ + show_join = 1; return; } @@ -2292,14 +2294,64 @@ ieee80211_status(void) putchar(' '); printb_status(ifr.ifr_flags, IEEE80211_F_USERBITS); } - putchar('\n'); + if (show_join) + join_status(); if (shownet80211chans) ieee80211_listchans(); else if (shownet80211nodes) ieee80211_listnodes(); } +void +join_status(void) +{ + struct ieee80211_joinreq_all ja; + struct ieee80211_join *jn = NULL; + int jsz = 100; + int ojsz; + int i; + int r; + + bzero(&ja, sizeof(ja)); + jn = recallocarray(NULL, 0, jsz, sizeof(*jn)); + if (jn == NULL) + err(1, "recallocarray"); + ojsz = jsz; + while (1) { + ja.ja_node = jn; + ja.ja_size = jsz * sizeof(*jn); + strlcpy(ja.ja_ifname, name, sizeof(ja.ja_ifname)); + + if ((r = ioctl(s, SIOCG80211JOINALL, &ja)) != 0) { + if (errno == E2BIG) { + jsz += 100; + jn = recallocarray(jn, ojsz, jsz, sizeof(*jn)); + if (jn == NULL) + err(1, "recallocarray"); + ojsz = jsz; + continue; + } else if (errno != ENOENT) + warn("SIOCG80211JOINALL"); + return; + } + break; + } + + if (!ja.ja_nodes) + return; + + fputs("\tjoin:\t", stdout); + for (i = 0; i < ja.ja_nodes; i++) { + if (i > 0) + printf("\t\t"); + if (jn[i].i_len > IEEE80211_NWID_LEN) + jn[i].i_len = IEEE80211_NWID_LEN; + print_string(jn[i].i_nwid, jn[i].i_len); + putchar('\n'); + } +} + void ieee80211_listchans(void) { diff --git a/sys/net80211/ieee80211_ioctl.c b/sys/net80211/ieee80211_ioctl.c index 40791460678..2ff4c10d267 100644 --- a/sys/net80211/ieee80211_ioctl.c +++ b/sys/net80211/ieee80211_ioctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ieee80211_ioctl.c,v 1.62 2018/08/06 11:28:01 stsp Exp $ */ +/* $OpenBSD: ieee80211_ioctl.c,v 1.63 2018/08/06 11:42:18 benno Exp $ */ /* $NetBSD: ieee80211_ioctl.c,v 1.15 2004/05/06 02:58:16 dyoung Exp $ */ /*- @@ -391,8 +391,10 @@ ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) struct ieee80211com *ic = (void *)ifp; struct ifreq *ifr = (struct ifreq *)data; int i, error = 0; + size_t len; struct ieee80211_nwid nwid; struct ieee80211_join join; + struct ieee80211_joinreq_all *ja; struct ieee80211_ess *ess; struct ieee80211_wpapsk *psk; struct ieee80211_keyavail *ka; @@ -488,6 +490,25 @@ ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) } } break; + case SIOCG80211JOINALL: + ja = (struct ieee80211_joinreq_all *)data; + ja->ja_nodes = len = 0; + TAILQ_FOREACH(ess, &ic->ic_ess, ess_next) { + if (len + sizeof(ja->ja_node[0]) >= ja->ja_size) { + error = E2BIG; + break; + } + memset(&join, 0, sizeof(join)); + join.i_len = ess->esslen; + memcpy(&join.i_nwid, ess->essid, join.i_len); + error = copyout(&join, &ja->ja_node[ja->ja_nodes], + sizeof(ja->ja_node[0])); + if (error) + break; + len += sizeof(join); + ja->ja_nodes++; + } + break; case SIOCS80211NWKEY: if ((error = suser(curproc)) != 0) break; diff --git a/sys/net80211/ieee80211_ioctl.h b/sys/net80211/ieee80211_ioctl.h index 9ea74127b22..561ac0f038b 100644 --- a/sys/net80211/ieee80211_ioctl.h +++ b/sys/net80211/ieee80211_ioctl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ieee80211_ioctl.h,v 1.32 2018/07/11 20:18:09 phessler Exp $ */ +/* $OpenBSD: ieee80211_ioctl.h,v 1.33 2018/08/06 11:42:18 benno Exp $ */ /* $NetBSD: ieee80211_ioctl.h,v 1.7 2004/04/30 22:51:04 dyoung Exp $ */ /*- @@ -275,6 +275,7 @@ struct ieee80211_keyrun { #define SIOCS80211SCAN _IOW('i', 210, struct ifreq) +#define SIOCG80211JOINALL _IOWR('i', 218, struct ieee80211_joinreq_all) #define SIOCS80211JOIN _IOWR('i', 255, struct ifreq) #define SIOCG80211JOIN _IOWR('i', 256, struct ifreq) @@ -288,6 +289,14 @@ struct ieee80211_join { struct ieee80211_nwkey i_nwkey; }; +struct ieee80211_joinreq_all { + char ja_ifname[IFNAMSIZ]; + int ja_nodes; /* returned count */ + size_t ja_size; /* size of node buffer */ + struct ieee80211_join *ja_node; /* allocated node buffer */ +}; + + #define IEEE80211_JOIN_SHOW 0x01 #define IEEE80211_JOIN_FOUND 0x02 #define IEEE80211_JOIN_DEL 0x04 -- 2.20.1