1 /*
2  * Copyright (c) 2019 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(net_l2_ppp, CONFIG_NET_L2_PPP_LOG_LEVEL);
9 
10 #include <zephyr/net/net_core.h>
11 #include <zephyr/net/net_pkt.h>
12 #include <zephyr/sys/iterable_sections.h>
13 #include <zephyr/net/ppp.h>
14 
15 #include "net_private.h"
16 
17 #include "ppp_internal.h"
18 
ppp_network_up(struct ppp_context * ctx,int proto)19 void ppp_network_up(struct ppp_context *ctx, int proto)
20 {
21 	if (ctx->network_protos_up == 0) {
22 		ppp_change_phase(ctx, PPP_RUNNING);
23 	}
24 
25 	ctx->network_protos_up++;
26 
27 	NET_DBG("[%p] Proto %s (0x%04x) %s (%d)", ctx, ppp_proto2str(proto),
28 		proto, "up", ctx->network_protos_up);
29 }
30 
ppp_network_down(struct ppp_context * ctx,int proto)31 void ppp_network_down(struct ppp_context *ctx, int proto)
32 {
33 	ctx->network_protos_up--;
34 
35 	if (ctx->network_protos_up <= 0) {
36 		ctx->network_protos_up = 0;
37 		ppp_change_phase(ctx, PPP_TERMINATE);
38 	}
39 
40 	NET_DBG("[%p] Proto %s (0x%04x) %s (%d)", ctx, ppp_proto2str(proto),
41 		proto, "down", ctx->network_protos_up);
42 }
43 
ppp_network_done(struct ppp_context * ctx,int proto)44 void ppp_network_done(struct ppp_context *ctx, int proto)
45 {
46 	ctx->network_protos_up--;
47 	if (ctx->network_protos_up <= 0) {
48 		const struct ppp_protocol_handler *ppp_proto = ppp_lcp_get();
49 
50 		if (ppp_proto) {
51 			ppp_proto->close(ctx, "All networks down");
52 		}
53 	}
54 }
55 
ppp_network_all_down(struct ppp_context * ctx)56 void ppp_network_all_down(struct ppp_context *ctx)
57 {
58 	STRUCT_SECTION_FOREACH(ppp_protocol_handler, proto) {
59 		if (proto->protocol != PPP_LCP && proto->lower_down) {
60 			proto->lower_down(ctx);
61 		}
62 
63 		if (proto->protocol < 0xC000 && proto->close) {
64 			ctx->network_protos_open--;
65 			proto->close(ctx, "LCP down");
66 		}
67 	}
68 
69 	if (ctx->network_protos_open > 0) {
70 		NET_WARN("Not all network protocols were closed (%d)",
71 			 ctx->network_protos_open);
72 	}
73 
74 	ctx->network_protos_open = 0;
75 }
76