Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 3 Oct 2003 09:16:47 +0100
From:      Bruce M Simpson <bms@spc.org>
To:        Ruslan Ermilov <ru@freebsd.org>
Cc:        freebsd-net@freebsd.org
Subject:   Re: rtsock.c: eliminate masking of gotos, don't abuse M_RTABLE
Message-ID:  <20031003081647.GQ5194@saboteur.dek.spc.org>
In-Reply-To: <20031003080010.GF53479@sunbay.com>
References:  <20031003070031.GL5194@saboteur.dek.spc.org> <20031003023838.I16042@odysseus.silby.com> <20031003080010.GF53479@sunbay.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--MP5ln1Rcf9Bvi+ZW
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Fri, Oct 03, 2003 at 11:00:10AM +0300, Ruslan Ermilov wrote:
> I think these uncommitted patches will mostly affect route.c, while this
> patch is for rtsock.c, the route(4) interface with the kernel, which is
> unlikely to change a lot.

Much-improved patch to cleanup rtsock.c at bde's prodding attached.
I'm proposing doing another pass after this to clear up the bad
condition checking style, parentheses, and whitespace.

BMS

--MP5ln1Rcf9Bvi+ZW
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=1

Index: route.h
===================================================================
RCS file: /home/ncvs/src/sys/net/route.h,v
retrieving revision 1.47
diff -u -r1.47 route.h
--- route.h	5 Mar 2003 19:24:22 -0000	1.47
+++ route.h	3 Oct 2003 08:09:36 -0000
@@ -262,6 +262,11 @@
 };
 
 #ifdef _KERNEL
+MALLOC_DECLARE(M_RTMSG);
+
+#define	RTMSG_MALLOC(p, n)	(p) = malloc((n), M_RTMSG, M_NOWAIT)
+#define	RTMSG_FREE(p)		free((p), M_RTMSG)
+
 #define	RTFREE(rt) \
 	do { \
 		if ((rt)->rt_refcnt <= 1) \
@@ -296,6 +301,9 @@
 	    struct sockaddr *, struct sockaddr *, int, struct rtentry **);
 int	 rtrequest1(int, struct rt_addrinfo *, struct rtentry **);
 int	 rt_check(struct rtentry **, struct rtentry **, struct sockaddr *);
+#else
+#define RTMSG_MALLOC(p, n)	(p) = malloc((u_long)(n))
+#define RTMSG_FREE(p)		free((p))
 #endif
 
 #endif
Index: rtsock.c
===================================================================
RCS file: /home/ncvs/src/sys/net/rtsock.c,v
retrieving revision 1.89
diff -u -r1.89 rtsock.c
--- rtsock.c	5 Mar 2003 19:24:22 -0000	1.89
+++ rtsock.c	3 Oct 2003 08:11:06 -0000
@@ -280,7 +280,6 @@
 	struct ifnet *ifp = 0;
 	struct ifaddr *ifa = 0;
 
-#define senderr(e) { error = e; goto flush;}
 	if (m == 0 || ((m->m_len < sizeof(long)) &&
 		       (m = m_pullup(m, sizeof(long))) == 0))
 		return (ENOBUFS);
@@ -290,37 +289,45 @@
 	if (len < sizeof(*rtm) ||
 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
 		dst = 0;
-		senderr(EINVAL);
+		error = EINVAL;
+		goto flush;
 	}
-	R_Malloc(rtm, struct rt_msghdr *, len);
+	RTMSG_MALLOC(rtm, len);
 	if (rtm == 0) {
 		dst = 0;
-		senderr(ENOBUFS);
+		error = ENOBUFS;
+		goto flush;
 	}
 	m_copydata(m, 0, len, (caddr_t)rtm);
 	if (rtm->rtm_version != RTM_VERSION) {
 		dst = 0;
-		senderr(EPROTONOSUPPORT);
+		error = EPROTONOSUPPORT;
+		goto flush;
 	}
 	rtm->rtm_pid = curproc->p_pid;
 	bzero(&info, sizeof(info));
 	info.rti_addrs = rtm->rtm_addrs;
 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
 		dst = 0;
-		senderr(EINVAL);
+		error = EINVAL;
+		goto flush;
 	}
 	info.rti_flags = rtm->rtm_flags;
 	if (dst == 0 || (dst->sa_family >= AF_MAX)
-	    || (gate != 0 && (gate->sa_family >= AF_MAX)))
-		senderr(EINVAL);
+	    || (gate != 0 && (gate->sa_family >= AF_MAX))) {
+		error = EINVAL;
+		goto flush;
+	}
 	if (genmask) {
 		struct radix_node *t;
 		t = rn_addmask((caddr_t)genmask, 0, 1);
 		if (t && Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1,
 			      *(u_char *)t->rn_key - 1) == 0)
 			genmask = (struct sockaddr *)(t->rn_key);
-		else
-			senderr(ENOBUFS);
+		else {
+			error = ENOBUFS;
+			goto flush;
+		}
 	}
 
 	/*
@@ -328,13 +335,15 @@
 	 * is the only operation the non-superuser is allowed.
 	 */
 	if (rtm->rtm_type != RTM_GET && (error = suser(curthread)) != 0)
-		senderr(error);
+		goto flush;
 
 	switch (rtm->rtm_type) {
 
 	case RTM_ADD:
-		if (gate == 0)
-			senderr(EINVAL);
+		if (gate == 0) {
+			error = EINVAL;
+			goto flush;
+		}
 		error = rtrequest1(RTM_ADD, &info, &saved_nrt);
 		if (error == 0 && saved_nrt) {
 			rt_setmetrics(rtm->rtm_inits,
@@ -359,15 +368,18 @@
 	case RTM_CHANGE:
 	case RTM_LOCK:
 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
-			senderr(EAFNOSUPPORT);
+			error = EAFNOSUPPORT;
+			goto flush;
 		}
 		RADIX_NODE_HEAD_LOCK(rnh);
 		rt = (struct rtentry *) rnh->rnh_lookup(dst, netmask, rnh);
 		RADIX_NODE_HEAD_UNLOCK(rnh);
 		if (rt != NULL)
 			rt->rt_refcnt++;
-		else
-			senderr(ESRCH);
+		else {
+			error = ESRCH;
+			goto flush;
+		}
 
 		switch(rtm->rtm_type) {
 
@@ -394,11 +406,13 @@
 				(struct walkarg *)0);
 			if (len > rtm->rtm_msglen) {
 				struct rt_msghdr *new_rtm;
-				R_Malloc(new_rtm, struct rt_msghdr *, len);
-				if (new_rtm == 0)
-					senderr(ENOBUFS);
+				RTMSG_MALLOC(new_rtm, len);
+				if (new_rtm == 0) {
+					error = ENOBUFS;
+					goto flush;
+				}
 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
-				Free(rtm); rtm = new_rtm;
+				RTMSG_FREE(rtm); rtm = new_rtm;
 			}
 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
 				(struct walkarg *)0);
@@ -418,11 +432,11 @@
 			    (ifaaddr != NULL &&
 			    !sa_equal(ifaaddr, rt->rt_ifa->ifa_addr))) {
 				if ((error = rt_getifa(&info)) != 0)
-					senderr(error);
+					goto flush;
 			}
 			if (gate != NULL &&
 			    (error = rt_setgate(rt, rt_key(rt), gate)) != 0)
-				senderr(error);
+				goto flush;
 			if ((ifa = info.rti_ifa) != NULL) {
 				register struct ifaddr *oifa = rt->rt_ifa;
 				if (oifa != ifa) {
@@ -453,7 +467,7 @@
 		break;
 
 	default:
-		senderr(EOPNOTSUPP);
+		error = EOPNOTSUPP;
 	}
 
 flush:
@@ -473,7 +487,7 @@
 	if ((so->so_options & SO_USELOOPBACK) == 0) {
 		if (route_cb.any_count <= 1) {
 			if (rtm)
-				Free(rtm);
+				RTMSG_FREE(rtm);
 			m_freem(m);
 			return (error);
 		}
@@ -487,7 +501,7 @@
 			m = NULL;
 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
-		Free(rtm);
+		RTMSG_FREE(rtm);
 	}
 	if (rp)
 		rp->rcb_proto.sp_family = 0; /* Avoid us */

--MP5ln1Rcf9Bvi+ZW--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20031003081647.GQ5194>