1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014 Intel Corporation
4  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5  * Copyright (C) 2015 Intel Deutschland GmbH
6  */
7 #include <net/ipv6.h>
8 #include <net/addrconf.h>
9 #include <linux/bitops.h>
10 #include "mvm.h"
11 
12 void iwl_mvm_set_wowlan_qos_seq(struct iwl_mvm_sta *mvm_ap_sta,
13 				struct iwl_wowlan_config_cmd *cmd)
14 {
15 	int i;
16 
17 	/*
18 	 * For QoS counters, we store the one to use next, so subtract 0x10
19 	 * since the uCode will add 0x10 *before* using the value while we
20 	 * increment after using the value (i.e. store the next value to use).
21 	 */
22 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
23 		u16 seq = mvm_ap_sta->tid_data[i].seq_number;
24 		seq -= 0x10;
25 		cmd->qos_seq[i] = cpu_to_le16(seq);
26 	}
27 }
28 
29 int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
30 			       struct ieee80211_vif *vif,
31 			       bool disable_offloading,
32 			       bool offload_ns,
33 			       u32 cmd_flags)
34 {
35 	union {
36 		struct iwl_proto_offload_cmd_v1 v1;
37 		struct iwl_proto_offload_cmd_v2 v2;
38 		struct iwl_proto_offload_cmd_v3_small v3s;
39 		struct iwl_proto_offload_cmd_v3_large v3l;
40 	} cmd = {};
41 	struct iwl_host_cmd hcmd = {
42 		.id = PROT_OFFLOAD_CONFIG_CMD,
43 		.flags = cmd_flags,
44 		.data[0] = &cmd,
45 		.dataflags[0] = IWL_HCMD_DFL_DUP,
46 	};
47 	struct iwl_proto_offload_cmd_common *common;
48 	u32 enabled = 0, size;
49 	u32 capa_flags = mvm->fw->ucode_capa.flags;
50 #if IS_ENABLED(CONFIG_IPV6)
51 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
52 	int i;
53 	/*
54 	 * Skip tentative address when ns offload is enabled to avoid
55 	 * violating RFC4862.
56 	 * Keep tentative address when ns offload is disabled so the NS packets
57 	 * will not be filtered out and will wake up the host.
58 	 */
59 	bool skip_tentative = offload_ns;
60 
61 	if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL ||
62 	    capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_LARGE) {
63 		struct iwl_ns_config *nsc;
64 		struct iwl_targ_addr *addrs;
65 		int n_nsc, n_addrs;
66 		int c;
67 		int num_skipped = 0;
68 
69 		if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL) {
70 			nsc = cmd.v3s.ns_config;
71 			n_nsc = IWL_PROTO_OFFLOAD_NUM_NS_CONFIG_V3S;
72 			addrs = cmd.v3s.targ_addrs;
73 			n_addrs = IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V3S;
74 		} else {
75 			nsc = cmd.v3l.ns_config;
76 			n_nsc = IWL_PROTO_OFFLOAD_NUM_NS_CONFIG_V3L;
77 			addrs = cmd.v3l.targ_addrs;
78 			n_addrs = IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V3L;
79 		}
80 
81 		/*
82 		 * For each address we have (and that will fit) fill a target
83 		 * address struct and combine for NS offload structs with the
84 		 * solicited node addresses.
85 		 */
86 		for (i = 0, c = 0;
87 		     i < mvmvif->num_target_ipv6_addrs &&
88 		     i < n_addrs && c < n_nsc; i++) {
89 			struct in6_addr solicited_addr;
90 			int j;
91 
92 			if (skip_tentative &&
93 			    test_bit(i, mvmvif->tentative_addrs)) {
94 				num_skipped++;
95 				continue;
96 			}
97 
98 			addrconf_addr_solict_mult(&mvmvif->target_ipv6_addrs[i],
99 						  &solicited_addr);
100 			for (j = 0; j < c; j++)
101 				if (ipv6_addr_cmp(&nsc[j].dest_ipv6_addr,
102 						  &solicited_addr) == 0)
103 					break;
104 			if (j == c)
105 				c++;
106 			addrs[i].addr = mvmvif->target_ipv6_addrs[i];
107 			addrs[i].config_num = cpu_to_le32(j);
108 			nsc[j].dest_ipv6_addr = solicited_addr;
109 			memcpy(nsc[j].target_mac_addr, vif->addr, ETH_ALEN);
110 		}
111 
112 		if (mvmvif->num_target_ipv6_addrs - num_skipped)
113 			enabled |= IWL_D3_PROTO_IPV6_VALID;
114 
115 		if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL)
116 			cmd.v3s.num_valid_ipv6_addrs =
117 				cpu_to_le32(i - num_skipped);
118 		else
119 			cmd.v3l.num_valid_ipv6_addrs =
120 				cpu_to_le32(i - num_skipped);
121 	} else if (capa_flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
122 		bool found = false;
123 
124 		BUILD_BUG_ON(sizeof(cmd.v2.target_ipv6_addr[0]) !=
125 			     sizeof(mvmvif->target_ipv6_addrs[0]));
126 
127 		for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
128 				    IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V2); i++) {
129 			if (skip_tentative &&
130 			    test_bit(i, mvmvif->tentative_addrs))
131 				continue;
132 
133 			memcpy(cmd.v2.target_ipv6_addr[i],
134 			       &mvmvif->target_ipv6_addrs[i],
135 			       sizeof(cmd.v2.target_ipv6_addr[i]));
136 
137 			found = true;
138 		}
139 		if (found) {
140 			enabled |= IWL_D3_PROTO_IPV6_VALID;
141 			memcpy(cmd.v2.ndp_mac_addr, vif->addr, ETH_ALEN);
142 		}
143 	} else {
144 		bool found = false;
145 		BUILD_BUG_ON(sizeof(cmd.v1.target_ipv6_addr[0]) !=
146 			     sizeof(mvmvif->target_ipv6_addrs[0]));
147 
148 		for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
149 				    IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V1); i++) {
150 			if (skip_tentative &&
151 			    test_bit(i, mvmvif->tentative_addrs))
152 				continue;
153 
154 			memcpy(cmd.v1.target_ipv6_addr[i],
155 			       &mvmvif->target_ipv6_addrs[i],
156 			       sizeof(cmd.v1.target_ipv6_addr[i]));
157 
158 			found = true;
159 		}
160 
161 		if (found) {
162 			enabled |= IWL_D3_PROTO_IPV6_VALID;
163 			memcpy(cmd.v1.ndp_mac_addr, vif->addr, ETH_ALEN);
164 		}
165 	}
166 
167 	if (offload_ns && (enabled & IWL_D3_PROTO_IPV6_VALID))
168 		enabled |= IWL_D3_PROTO_OFFLOAD_NS;
169 #endif
170 	if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL) {
171 		common = &cmd.v3s.common;
172 		size = sizeof(cmd.v3s);
173 	} else if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_LARGE) {
174 		common = &cmd.v3l.common;
175 		size = sizeof(cmd.v3l);
176 	} else if (capa_flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
177 		common = &cmd.v2.common;
178 		size = sizeof(cmd.v2);
179 	} else {
180 		common = &cmd.v1.common;
181 		size = sizeof(cmd.v1);
182 	}
183 
184 	if (vif->bss_conf.arp_addr_cnt) {
185 		enabled |= IWL_D3_PROTO_OFFLOAD_ARP | IWL_D3_PROTO_IPV4_VALID;
186 		common->host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
187 		memcpy(common->arp_mac_addr, vif->addr, ETH_ALEN);
188 	}
189 
190 	if (!disable_offloading)
191 		common->enabled = cpu_to_le32(enabled);
192 
193 	hcmd.len[0] = size;
194 	return iwl_mvm_send_cmd(mvm, &hcmd);
195 }
196