From 1cd23a1f9fc1e80eed04163fed94159f67a8dd08 Mon Sep 17 00:00:00 2001
From: Maxime Bizon <mbizon@freebox.fr>
Date: Tue, 28 Jan 2020 18:23:14 +0100
Subject: [PATCH 05/12] pppd: add new network up notifier

only trigger when all opened network protocols are up

this allows waiting for ip and ipv6 to be done
---
 pppd/auth.c         | 14 ++++++++++++++
 pppd/main.c         |  1 +
 pppd/pppd-private.h |  1 +
 pppd/pppd.h         |  1 +
 4 files changed, 17 insertions(+)

diff --git a/pppd/auth.c b/pppd/auth.c
index c8d1428..0779c46 100644
--- a/pppd/auth.c
+++ b/pppd/auth.c
@@ -215,6 +215,10 @@ struct notifier *auth_with_peer_notifier = NULL;
 /* A notifier for when the link goes down. */
 struct notifier *link_down_notifier = NULL;
 
+/* A notifier when all possible network protocols are up. */
+struct notifier *network_up_notifier = NULL;
+static int network_up = 0;
+
 /*
  * This is used to ensure that we don't start an auth-up/down
  * script while one is already running.
@@ -811,6 +815,7 @@ void upper_layers_down(int unit)
     }
     num_np_open = 0;
     num_np_up = 0;
+    network_up = 0;
 }
 
 /*
@@ -1222,6 +1227,11 @@ np_up(int unit, int proto)
 	}
     }
     ++num_np_up;
+
+    if (num_np_up == num_np_open && !network_up) {
+	    network_up = 1;
+	    notify(network_up_notifier, 0);
+    }
 }
 
 /*
@@ -1248,6 +1258,10 @@ np_finished(int unit, int proto)
 	/* no further use for the link: shut up shop. */
 	lcp_close(0, "No network protocols running");
     }
+    if (num_np_up && num_np_up == num_np_open && !network_up) {
+	    network_up = 1;
+	    notify(network_up_notifier, 0);
+    }
 }
 
 /*
diff --git a/pppd/main.c b/pppd/main.c
index ea0b733..fe4d508 100644
--- a/pppd/main.c
+++ b/pppd/main.c
@@ -2100,6 +2100,7 @@ struct notifier **get_notifier_by_type(ppp_notify_t type)
         [NF_FIRST_LCP   ] = &first_lcp_notifier,
         [NF_AUTH_WITH_PEER] = &auth_with_peer_notifier,
         [NF_MPPE_STATE  ] = &mppe_state_notifier,
+        [NF_NETWORK_UP  ] = &network_up_notifier,
     };
     return list[type];
 }
diff --git a/pppd/pppd-private.h b/pppd/pppd-private.h
index b0aa798..57f534d 100644
--- a/pppd/pppd-private.h
+++ b/pppd/pppd-private.h
@@ -141,6 +141,7 @@ extern struct notifier *fork_notifier;	/* we are a new child process */
 extern struct notifier *first_lcp_notifier; /* first LCP packet received */
 extern struct notifier *auth_with_peer_notifier; /* we have authenticated  */
 extern struct notifier *mppe_state_notifier; /* mppe state changed */
+extern struct notifier *network_up_notifier; /* all network protocols are up */
 
 
 /* Values for do_callback and doing_callback */
diff --git a/pppd/pppd.h b/pppd/pppd.h
index 217cdc1..d9cb165 100644
--- a/pppd/pppd.h
+++ b/pppd/pppd.h
@@ -150,6 +150,7 @@ typedef enum
     NF_FIRST_LCP,
     NF_AUTH_WITH_PEER,
     NF_MPPE_STATE,
+    NF_NETWORK_UP,
     NF_MAX_NOTIFY
 } ppp_notify_t;
 
-- 
2.34.1

