+#include "kinfo.h"
+struct walkarg {
+ int w_op, w_arg;
+ int w_given, w_needed;
+ caddr_t w_where;
+ struct {
+ struct rt_msghdr m_rtm;
+ char m_sabuf[128];
+ } w_m;
+#define w_rtm w_m.m_rtm
+};
+/*
+ * This is used in dumping the kernel table via getkinfo().
+ */
+rt_dumpentry(rn, w)
+ struct radix_node *rn;
+ register struct walkarg *w;
+{
+ register struct sockaddr *sa;
+ int n, error;
+
+ for (; rn; rn = rn->rn_dupedkey) {
+ int count = 0, size = sizeof(w->w_rtm);
+ register struct rtentry *rt = (struct rtentry *)rn;
+
+ if (rn->rn_flags & RNF_ROOT)
+ continue;
+ if (w->w_op == KINFO_RT_FLAGS && !(rt->rt_flags & w->w_arg))
+ continue;
+#define next(a, l) {size += (l); w->w_rtm.rtm_addrs |= (a); }
+ w->w_rtm.rtm_addrs = 0;
+ if (sa = rt_key(rt))
+ next(RTA_DST, ROUNDUP(sa->sa_len));
+ if (sa = rt->rt_gateway)
+ next(RTA_GATEWAY, ROUNDUP(sa->sa_len));
+ if (sa = rt_mask(rt))
+ next(RTA_NETMASK, ROUNDUP(sa->sa_len));
+ if (sa = rt->rt_genmask)
+ next(RTA_GENMASK, ROUNDUP(sa->sa_len));
+ w->w_needed += size;
+ if (w->w_where == NULL || w->w_needed > 0)
+ continue;
+ w->w_rtm.rtm_msglen = size;
+ w->w_rtm.rtm_flags = rt->rt_flags;
+ w->w_rtm.rtm_use = rt->rt_use;
+ w->w_rtm.rtm_rmx = rt->rt_rmx;
+ w->w_rtm.rtm_index = rt->rt_ifp->if_index;
+#undef next
+#define next(l) {n = (l); Bcopy(sa, cp, n); cp += n;}
+ if (size <= sizeof(w->w_m)) {
+ register caddr_t cp = (caddr_t)(w->w_m.m_sabuf);
+ if (sa = rt_key(rt))
+ next(ROUNDUP(sa->sa_len));
+ if (sa = rt->rt_gateway)
+ next(ROUNDUP(sa->sa_len));
+ if (sa = rt_mask(rt))
+ next(ROUNDUP(sa->sa_len));
+ if (sa = rt->rt_genmask)
+ next(ROUNDUP(sa->sa_len));
+#undef next
+#define next(s, l) {n = (l); \
+ if (error = copyout((caddr_t)(s), w->w_where, n)) return (error); \
+ w->w_where += n;}
+
+ next(&w->w_m, size); /* Copy rtmsg and sockaddrs back */
+ continue;
+ }
+ next(&w->w_rtm, sizeof(w->w_rtm));
+ if (sa = rt_key(rt))
+ next(sa, ROUNDUP(sa->sa_len));
+ if (sa = rt->rt_gateway)
+ next(sa, ROUNDUP(sa->sa_len));
+ if (sa = rt_mask(rt))
+ next(sa, ROUNDUP(sa->sa_len));
+ if (sa = rt->rt_genmask)
+ next(sa, ROUNDUP(sa->sa_len));
+ }
+ return (0);
+#undef next
+}
+
+kinfo_rtable(op, where, given, arg, needed)
+ int op, arg;
+ caddr_t where;
+ int *given, *needed;
+{
+ register struct radix_node_head *rnh;
+ int s, error = 0;
+ u_char af = ki_af(op);
+ struct walkarg w;
+
+ op &= 0xffff;
+ if (op != KINFO_RT_DUMP && op != KINFO_RT_FLAGS)
+ return (EINVAL);
+
+ Bzero(&w, sizeof(w));
+ if ((w.w_where = where) && given)
+ w.w_given = *given;
+ w.w_needed = 0 - w.w_given;
+ w.w_arg = arg;
+ w.w_op = op;
+ w.w_rtm.rtm_version = RTM_VERSION;
+ w.w_rtm.rtm_type = RTM_GET;
+
+ s = splnet();
+ for (rnh = radix_node_head; rnh; rnh = rnh->rnh_next) {
+ if (rnh->rnh_af == 0)
+ continue;
+ if (af && af != rnh->rnh_af)
+ continue;
+ error = rt_walk(rnh->rnh_treetop, rt_dumpentry, &w);
+ if (error)
+ break;
+ }
+ w.w_needed += w.w_given;
+ if (where && given)
+ *given = w.w_where - where;
+ else
+ w.w_needed = (11 * w.w_needed) / 10;
+ *needed = w.w_needed;
+ splx(s);
+ return (error);
+}
+
+rt_walk(rn, f, w)
+ register struct radix_node *rn;
+ register int (*f)();
+ struct walkarg *w;
+{
+ int error;
+ for (;;) {
+ while (rn->rn_b >= 0)
+ rn = rn->rn_l; /* First time through node, go left */
+ if (error = (*f)(rn, w))
+ return (error); /* Process Leaf */
+ while (rn->rn_p->rn_r == rn) { /* if coming back from right */
+ rn = rn->rn_p; /* go back up */
+ if (rn->rn_flags & RNF_ROOT)
+ return 0;
+ }
+ rn = rn->rn_p->rn_r; /* otherwise, go right*/
+ }
+}
+