1 /*
2 * Copyright (c) 2018-2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/sysctl.h>
30 #include <sys/systm.h>
31 #include <sys/eventhandler.h>
32
33 #include <net/dlil.h>
34 #include <net/if.h>
35 #include <net/if_var.h>
36 #include <net/nwk_wq.h>
37
38 #include <os/log.h>
39
40 typedef enum {
41 IF_LOW_POWER_EVENT_OFF = 0,
42 IF_LOW_POWER_EVENT_ON = 1
43 } if_low_power_ev_code_t;
44
45 struct if_low_power_ev_args {
46 struct ifnet *ifp;
47 if_low_power_ev_code_t event_code;
48 };
49
50 struct if_low_power_ev_nwk_wq_entry {
51 struct nwk_wq_entry nwk_wqe;
52 struct if_low_power_ev_args ev_args;
53 };
54
55
56 typedef void (*if_low_power_event_fn) (struct eventhandler_entry_arg,
57 struct ifnet *, if_low_power_ev_code_t);
58 EVENTHANDLER_DECLARE(if_low_power_event, if_low_power_event_fn);
59
60 struct eventhandler_lists_ctxt if_low_power_evhdlr_ctx;
61
62 static void if_low_power_evhdlr_callback(__unused struct eventhandler_entry_arg arg,
63 struct ifnet *ifp, if_low_power_ev_code_t event_code);
64
65 extern void shutdown_sockets_on_interface(struct ifnet *ifp);
66
67 SYSCTL_DECL(_net_link_generic_system);
68 SYSCTL_NODE(_net_link_generic_system, OID_AUTO, low_power,
69 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "low power mode");
70
71 int if_low_power_verbose = 0;
72 int if_low_power_restricted = 1;
73
74 #if (DEVELOPMENT || DEBUG)
75 SYSCTL_INT(_net_link_generic_system_low_power, OID_AUTO, verbose,
76 CTLFLAG_RW | CTLFLAG_LOCKED,
77 &if_low_power_verbose, 0, "");
78 SYSCTL_INT(_net_link_generic_system_low_power, OID_AUTO, restricted,
79 CTLFLAG_RW | CTLFLAG_LOCKED,
80 &if_low_power_restricted, 0, "");
81 #endif /* (DEVELOPMENT || DEBUG) */
82
83
84 static void
if_low_power_evhdlr_callback(__unused struct eventhandler_entry_arg arg,struct ifnet * ifp,if_low_power_ev_code_t event_code)85 if_low_power_evhdlr_callback(__unused struct eventhandler_entry_arg arg,
86 struct ifnet *ifp, if_low_power_ev_code_t event_code)
87 {
88 struct kev_dl_low_power_mode kev;
89
90 if (!IF_FULLY_ATTACHED(ifp)) {
91 return;
92 }
93
94 if (if_low_power_verbose > 0) {
95 os_log_info(OS_LOG_DEFAULT,
96 "%s: ifp %s event_code %d", __func__,
97 if_name(ifp), event_code);
98 }
99
100 if (event_code == IF_LOW_POWER_EVENT_OFF) {
101 if_clear_xflags(ifp, IFXF_LOW_POWER);
102 } else {
103 if_set_xflags(ifp, IFXF_LOW_POWER);
104 }
105
106 if (event_code == IF_LOW_POWER_EVENT_ON) {
107 os_atomic_inc(&ifp->if_low_power_gencnt, relaxed);
108
109 if (if_low_power_restricted != 0) {
110 shutdown_sockets_on_interface(ifp);
111 intf_event_enqueue_nwk_wq_entry(ifp, NULL,
112 INTF_EVENT_CODE_LOW_POWER_UPDATE);
113 }
114 }
115
116 bzero(&kev, sizeof(struct kev_dl_low_power_mode));
117 kev.low_power_event = event_code;
118 dlil_post_msg(ifp,
119 KEV_DL_SUBCLASS,
120 KEV_DL_LOW_POWER_MODE_CHANGED,
121 (struct net_event_data *)&kev,
122 sizeof(struct kev_dl_low_power_mode), FALSE);
123 }
124
125 void
if_low_power_evhdlr_init(void)126 if_low_power_evhdlr_init(void)
127 {
128 eventhandler_lists_ctxt_init(&if_low_power_evhdlr_ctx);
129
130 (void)EVENTHANDLER_REGISTER(&if_low_power_evhdlr_ctx,
131 if_low_power_event,
132 &if_low_power_evhdlr_callback,
133 eventhandler_entry_dummy_arg,
134 EVENTHANDLER_PRI_ANY);
135 }
136
137 int
if_set_low_power(ifnet_t ifp,bool on)138 if_set_low_power(ifnet_t ifp, bool on)
139 {
140 int error = 0;
141
142 if (ifp == NULL) {
143 return EINVAL;
144 }
145
146 os_log(OS_LOG_DEFAULT,
147 "%s: ifp %s low_power mode %d", __func__, if_name(ifp), on);
148
149 if (on) {
150 if_set_xflags(ifp, IFXF_LOW_POWER);
151 } else {
152 if_clear_xflags(ifp, IFXF_LOW_POWER);
153 }
154 return error;
155 }
156