Module Name:    src
Committed By:   kefren
Date:           Wed Jul 24 09:05:53 UTC 2013

Modified Files:
        src/usr.sbin/ldpd: label.c label.h mpls_interface.c mpls_routes.c

Log Message:
* use a flag in label struct in order to determine if we should treat the
  associated routes using RTF_HOST
* clean the mess in flush_mpls_routes that could result in accessing
  uninitialized memory


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/usr.sbin/ldpd/label.c
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/ldpd/label.h
cvs rdiff -u -r1.11 -r1.12 src/usr.sbin/ldpd/mpls_interface.c
cvs rdiff -u -r1.19 -r1.20 src/usr.sbin/ldpd/mpls_routes.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ldpd/label.c
diff -u src/usr.sbin/ldpd/label.c:1.9 src/usr.sbin/ldpd/label.c:1.10
--- src/usr.sbin/ldpd/label.c:1.9	Thu Jul 18 11:45:36 2013
+++ src/usr.sbin/ldpd/label.c	Wed Jul 24 09:05:53 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: label.c,v 1.9 2013/07/18 11:45:36 kefren Exp $ */
+/* $NetBSD: label.c,v 1.10 2013/07/24 09:05:53 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -55,7 +55,7 @@ label_init()
 struct label   *
 label_add(const union sockunion * so_dest, const union sockunion * so_pref,
 	  const union sockunion * so_gate, uint32_t binding,
-	  const struct ldp_peer * p, uint32_t label)
+	  const struct ldp_peer * p, uint32_t label, bool host)
 {
 	struct label   *l;
 	char	spreftmp[INET_ADDRSTRLEN];
@@ -83,6 +83,7 @@ label_add(const union sockunion * so_des
 		l->binding = get_free_local_label();
 	l->p = p;
 	l->label = label;
+	l->host = host;
 
 	SLIST_INSERT_HEAD(&label_head, l, labels);
 
@@ -125,12 +126,13 @@ label_reattach_route(struct label *l, in
 	if (readd == REATT_INET_CHANGE) {
 		/* Delete the tagged route and re-add IPv4 route */
 		delete_route(&l->so_dest,
-		    l->so_pref.sa.sa_len != 0 ? &l->so_pref : NULL, NO_FREESO);
+		    l->host ? NULL : &l->so_pref, NO_FREESO);
 		add_route(&l->so_dest,
-		    l->so_pref.sa.sa_len != 0 ? &l->so_pref : NULL, &l->so_gate,
+		    l->host ? NULL : &l->so_pref, &l->so_gate,
 		    NULL, NULL, NO_FREESO, RTM_READD);
 	} else if (readd == REATT_INET_DEL)
-		delete_route(&l->so_dest, &l->so_pref, NO_FREESO);
+		delete_route(&l->so_dest, l->host ? NULL : &l->so_pref,
+		    NO_FREESO);
 
 	/* Deletes the MPLS route */
 	if (l->binding >= min_label)

Index: src/usr.sbin/ldpd/label.h
diff -u src/usr.sbin/ldpd/label.h:1.6 src/usr.sbin/ldpd/label.h:1.7
--- src/usr.sbin/ldpd/label.h:1.6	Thu Jul 18 11:45:36 2013
+++ src/usr.sbin/ldpd/label.h	Wed Jul 24 09:05:53 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: label.h,v 1.6 2013/07/18 11:45:36 kefren Exp $ */
+/* $NetBSD: label.h,v 1.7 2013/07/24 09:05:53 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -51,6 +51,7 @@
 struct label {
 	union sockunion so_dest, so_pref, so_gate;
 	int binding, label;
+	bool host;	/* change routes using RTF_HOST */
 	const struct ldp_peer *p;
 	SLIST_ENTRY(label) labels;
 };
@@ -58,7 +59,8 @@ SLIST_HEAD(,label) label_head;
 
 void            label_init(void);
 struct label *	label_add(const union sockunion *, const union sockunion *,
-	  const union sockunion *, uint32_t, const struct ldp_peer *, uint32_t);
+	  const union sockunion *, uint32_t, const struct ldp_peer *, uint32_t,
+	  bool);
 void            label_del(struct label *);
 void            del_all_peer_labels(const struct ldp_peer*, int);
 void		label_reattach_all_peer_labels(const struct ldp_peer*, int);

Index: src/usr.sbin/ldpd/mpls_interface.c
diff -u src/usr.sbin/ldpd/mpls_interface.c:1.11 src/usr.sbin/ldpd/mpls_interface.c:1.12
--- src/usr.sbin/ldpd/mpls_interface.c:1.11	Thu Jul 18 11:45:36 2013
+++ src/usr.sbin/ldpd/mpls_interface.c	Wed Jul 24 09:05:53 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: mpls_interface.c,v 1.11 2013/07/18 11:45:36 kefren Exp $ */
+/* $NetBSD: mpls_interface.c,v 1.12 2013/07/24 09:05:53 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -138,24 +138,9 @@ mpls_add_label(struct label *lab)
 		fatalp("Out of memory\n");
 		return LDP_E_MEMORY;
 	}
-/*
-	if (so_oldifa != NULL) {
-		so_ifa = malloc(sizeof(*so_ifa));
-		if (so_ifa == NULL) {
-			free(so_dest);
-			if (so_pref != NULL)
-				free(so_pref);
-			free(so_tag);
-			free(so_nexthop);
-			fatalp("Out of memory\n");
-			return LDP_E_MEMORY;
-		}
-		memcpy(so_ifa, so_oldifa, so_oldifa->sa.sa_len);
-	} else
-		so_ifa = NULL;
-*/
-	if (add_route(&lab->so_dest, &lab->so_pref, &lab->so_gate, &so_ifa,
-	    so_tag, NO_FREESO, RTM_CHANGE) != LDP_E_OK) {
+
+	if (add_route(&lab->so_dest, lab->host ? NULL : &lab->so_pref,
+	    &lab->so_gate, &so_ifa, so_tag, NO_FREESO, RTM_CHANGE) != LDP_E_OK){
 		free(so_tag);
 		fatalp("[mpls_add_label]: INET route failure\n");
 		return LDP_E_ROUTE_ERROR;

Index: src/usr.sbin/ldpd/mpls_routes.c
diff -u src/usr.sbin/ldpd/mpls_routes.c:1.19 src/usr.sbin/ldpd/mpls_routes.c:1.20
--- src/usr.sbin/ldpd/mpls_routes.c:1.19	Sat Jul 20 05:16:08 2013
+++ src/usr.sbin/ldpd/mpls_routes.c	Wed Jul 24 09:05:53 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: mpls_routes.c,v 1.19 2013/07/20 05:16:08 kefren Exp $ */
+/* $NetBSD: mpls_routes.c,v 1.20 2013/07/24 09:05:53 kefren Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -414,8 +414,10 @@ delete_route(union sockunion * so_dest, 
 	rm.m_rtm.rtm_seq = ++rt_seq;
 	if (so_pref)
 		rm.m_rtm.rtm_addrs = RTA_DST | RTA_NETMASK;
-	else
+	else {
 		rm.m_rtm.rtm_addrs = RTA_DST;
+		rm.m_rtm.rtm_flags |= RTF_HOST;
+	}
 
 	/* destination, gateway, netmask, genmask, ifp, ifa */
 
@@ -656,7 +658,8 @@ check_route(struct rt_msg * rg, uint rle
 			/* Just add an IMPLNULL label */
 			if (so_gate == NULL)
 				label_add(so_dest, so_pref, NULL,
-					MPLS_LABEL_IMPLNULL, NULL, 0);
+					MPLS_LABEL_IMPLNULL, NULL, 0,
+					rg->m_rtm.rtm_flags & RTF_HOST);
 			else {
 				pm = ldp_test_mapping(&so_dest->sa,
 					 prefixlen, &so_gate->sa);
@@ -665,13 +668,15 @@ check_route(struct rt_msg * rg, uint rle
 					 * gets rewritten in mpls_add_label */
 					lab = label_add(so_dest, so_pref,
 					    so_gate, MPLS_LABEL_IMPLNULL,
-					    pm->peer, pm->lm->label);
+					    pm->peer, pm->lm->label,
+					    rg->m_rtm.rtm_flags & RTF_HOST);
 					if (lab != NULL)
 						mpls_add_label(lab);
 					free(pm);
 				} else
 					label_add(so_dest, so_pref, so_gate,
-					    MPLS_LABEL_IMPLNULL, NULL, 0);
+					    MPLS_LABEL_IMPLNULL, NULL, 0,
+					    rg->m_rtm.rtm_flags & RTF_HOST);
 			}
 		} else	/* We already know about this prefix */
 			fatalp("Binding already there for prefix %s/%d !\n",
@@ -885,7 +890,9 @@ bind_current_routes()
 		    (so_gate->sa.sa_family == AF_MPLS)) {
 			debugp("MPLS route to %s deleted.\n",
 			    inet_ntoa(so_dst->sin.sin_addr));
-			delete_route(so_dst, so_pref, NO_FREESO);
+			delete_route(so_dst,
+			    rtmes->rtm_flags & RTF_HOST ? NULL : so_pref,
+			    NO_FREESO);
 			if (rtmes->rtm_flags & RTF_HOST)
 				free(so_pref);
 			continue;
@@ -896,7 +903,8 @@ bind_current_routes()
 
 		if (so_gate == NULL || so_gate->sa.sa_family == AF_INET)
 			label_add(so_dst, so_pref, so_gate,
-			    MPLS_LABEL_IMPLNULL, NULL, 0);
+			    MPLS_LABEL_IMPLNULL, NULL, 0,
+			    rtmes->rtm_flags & RTF_HOST);
 
 		if (rtmes->rtm_flags & RTF_HOST)
 			free(so_pref);
@@ -955,11 +963,12 @@ flush_mpls_routes()
 			continue;
 		}
 
-		if (rtm->rtm_addrs & RTA_GATEWAY) {
-			GETNEXT(so_gate, so_dst);
+		if ((rtm->rtm_addrs & RTA_GATEWAY) == 0)
+			continue;
+		GETNEXT(so_gate, so_dst);
+
+		if ((rtm->rtm_flags & RTF_HOST) == 0)
 			GETNEXT(so_pref, so_gate);
-		} else
-			GETNEXT(so_pref, so_dst);
 
 		if (so_gate->sa.sa_family == AF_MPLS) {
 			if (so_dst->sa.sa_family == AF_INET)

Reply via email to