xref: /openbmc/linux/drivers/net/hyperv/rndis_filter.c (revision 6f6249a599e52e1a5f0b632f8edff733cfa76450)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * Authors:
6  *   Haiyang Zhang <haiyangz@microsoft.com>
7  *   Hank Janssen  <hjanssen@microsoft.com>
8  */
9 #include <linux/ethtool.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/wait.h>
13 #include <linux/highmem.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 #include <linux/if_ether.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_vlan.h>
19 #include <linux/nls.h>
20 #include <linux/vmalloc.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/ucs2_string.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 
26 #include "hyperv_net.h"
27 #include "netvsc_trace.h"
28 
29 static void rndis_set_multicast(struct work_struct *w);
30 
31 #define RNDIS_EXT_LEN HV_HYP_PAGE_SIZE
32 struct rndis_request {
33 	struct list_head list_ent;
34 	struct completion  wait_event;
35 
36 	struct rndis_message response_msg;
37 	/*
38 	 * The buffer for extended info after the RNDIS response message. It's
39 	 * referenced based on the data offset in the RNDIS message. Its size
40 	 * is enough for current needs, and should be sufficient for the near
41 	 * future.
42 	 */
43 	u8 response_ext[RNDIS_EXT_LEN];
44 
45 	/* Simplify allocation by having a netvsc packet inline */
46 	struct hv_netvsc_packet	pkt;
47 
48 	struct rndis_message request_msg;
49 	/*
50 	 * The buffer for the extended info after the RNDIS request message.
51 	 * It is referenced and sized in a similar way as response_ext.
52 	 */
53 	u8 request_ext[RNDIS_EXT_LEN];
54 };
55 
56 static const u8 netvsc_hash_key[NETVSC_HASH_KEYLEN] = {
57 	0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
58 	0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
59 	0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
60 	0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
61 	0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
62 };
63 
get_rndis_device(void)64 static struct rndis_device *get_rndis_device(void)
65 {
66 	struct rndis_device *device;
67 
68 	device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
69 	if (!device)
70 		return NULL;
71 
72 	spin_lock_init(&device->request_lock);
73 
74 	INIT_LIST_HEAD(&device->req_list);
75 	INIT_WORK(&device->mcast_work, rndis_set_multicast);
76 
77 	device->state = RNDIS_DEV_UNINITIALIZED;
78 
79 	return device;
80 }
81 
get_rndis_request(struct rndis_device * dev,u32 msg_type,u32 msg_len)82 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
83 					     u32 msg_type,
84 					     u32 msg_len)
85 {
86 	struct rndis_request *request;
87 	struct rndis_message *rndis_msg;
88 	struct rndis_set_request *set;
89 	unsigned long flags;
90 
91 	request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
92 	if (!request)
93 		return NULL;
94 
95 	init_completion(&request->wait_event);
96 
97 	rndis_msg = &request->request_msg;
98 	rndis_msg->ndis_msg_type = msg_type;
99 	rndis_msg->msg_len = msg_len;
100 
101 	request->pkt.q_idx = 0;
102 
103 	/*
104 	 * Set the request id. This field is always after the rndis header for
105 	 * request/response packet types so we just used the SetRequest as a
106 	 * template
107 	 */
108 	set = &rndis_msg->msg.set_req;
109 	set->req_id = atomic_inc_return(&dev->new_req_id);
110 
111 	/* Add to the request list */
112 	spin_lock_irqsave(&dev->request_lock, flags);
113 	list_add_tail(&request->list_ent, &dev->req_list);
114 	spin_unlock_irqrestore(&dev->request_lock, flags);
115 
116 	return request;
117 }
118 
put_rndis_request(struct rndis_device * dev,struct rndis_request * req)119 static void put_rndis_request(struct rndis_device *dev,
120 			    struct rndis_request *req)
121 {
122 	unsigned long flags;
123 
124 	spin_lock_irqsave(&dev->request_lock, flags);
125 	list_del(&req->list_ent);
126 	spin_unlock_irqrestore(&dev->request_lock, flags);
127 
128 	kfree(req);
129 }
130 
dump_rndis_message(struct net_device * netdev,const struct rndis_message * rndis_msg,const void * data)131 static void dump_rndis_message(struct net_device *netdev,
132 			       const struct rndis_message *rndis_msg,
133 			       const void *data)
134 {
135 	switch (rndis_msg->ndis_msg_type) {
136 	case RNDIS_MSG_PACKET:
137 		if (rndis_msg->msg_len - RNDIS_HEADER_SIZE >= sizeof(struct rndis_packet)) {
138 			const struct rndis_packet *pkt = data + RNDIS_HEADER_SIZE;
139 			netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
140 				   "data offset %u data len %u, # oob %u, "
141 				   "oob offset %u, oob len %u, pkt offset %u, "
142 				   "pkt len %u\n",
143 				   rndis_msg->msg_len,
144 				   pkt->data_offset,
145 				   pkt->data_len,
146 				   pkt->num_oob_data_elements,
147 				   pkt->oob_data_offset,
148 				   pkt->oob_data_len,
149 				   pkt->per_pkt_info_offset,
150 				   pkt->per_pkt_info_len);
151 		}
152 		break;
153 
154 	case RNDIS_MSG_INIT_C:
155 		if (rndis_msg->msg_len - RNDIS_HEADER_SIZE >=
156 				sizeof(struct rndis_initialize_complete)) {
157 			const struct rndis_initialize_complete *init_complete =
158 				data + RNDIS_HEADER_SIZE;
159 			netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
160 				"(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
161 				"device flags %d, max xfer size 0x%x, max pkts %u, "
162 				"pkt aligned %u)\n",
163 				rndis_msg->msg_len,
164 				init_complete->req_id,
165 				init_complete->status,
166 				init_complete->major_ver,
167 				init_complete->minor_ver,
168 				init_complete->dev_flags,
169 				init_complete->max_xfer_size,
170 				init_complete->max_pkt_per_msg,
171 				init_complete->pkt_alignment_factor);
172 		}
173 		break;
174 
175 	case RNDIS_MSG_QUERY_C:
176 		if (rndis_msg->msg_len - RNDIS_HEADER_SIZE >=
177 				sizeof(struct rndis_query_complete)) {
178 			const struct rndis_query_complete *query_complete =
179 				data + RNDIS_HEADER_SIZE;
180 			netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
181 				"(len %u, id 0x%x, status 0x%x, buf len %u, "
182 				"buf offset %u)\n",
183 				rndis_msg->msg_len,
184 				query_complete->req_id,
185 				query_complete->status,
186 				query_complete->info_buflen,
187 				query_complete->info_buf_offset);
188 		}
189 		break;
190 
191 	case RNDIS_MSG_SET_C:
192 		if (rndis_msg->msg_len - RNDIS_HEADER_SIZE + sizeof(struct rndis_set_complete)) {
193 			const struct rndis_set_complete *set_complete =
194 				data + RNDIS_HEADER_SIZE;
195 			netdev_dbg(netdev,
196 				"RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
197 				rndis_msg->msg_len,
198 				set_complete->req_id,
199 				set_complete->status);
200 		}
201 		break;
202 
203 	case RNDIS_MSG_INDICATE:
204 		if (rndis_msg->msg_len - RNDIS_HEADER_SIZE >=
205 				sizeof(struct rndis_indicate_status)) {
206 			const struct rndis_indicate_status *indicate_status =
207 				data + RNDIS_HEADER_SIZE;
208 			netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
209 				"(len %u, status 0x%x, buf len %u, buf offset %u)\n",
210 				rndis_msg->msg_len,
211 				indicate_status->status,
212 				indicate_status->status_buflen,
213 				indicate_status->status_buf_offset);
214 		}
215 		break;
216 
217 	default:
218 		netdev_dbg(netdev, "0x%x (len %u)\n",
219 			rndis_msg->ndis_msg_type,
220 			rndis_msg->msg_len);
221 		break;
222 	}
223 }
224 
rndis_filter_send_request(struct rndis_device * dev,struct rndis_request * req)225 static int rndis_filter_send_request(struct rndis_device *dev,
226 				  struct rndis_request *req)
227 {
228 	struct hv_netvsc_packet *packet;
229 	struct hv_page_buffer pb;
230 	int ret;
231 
232 	/* Setup the packet to send it */
233 	packet = &req->pkt;
234 
235 	packet->total_data_buflen = req->request_msg.msg_len;
236 	packet->page_buf_cnt = 1;
237 
238 	pb.pfn = virt_to_phys(&req->request_msg) >> HV_HYP_PAGE_SHIFT;
239 	pb.len = req->request_msg.msg_len;
240 	pb.offset = offset_in_hvpage(&req->request_msg);
241 
242 	trace_rndis_send(dev->ndev, 0, &req->request_msg);
243 
244 	rcu_read_lock_bh();
245 	ret = netvsc_send(dev->ndev, packet, NULL, &pb, NULL, false);
246 	rcu_read_unlock_bh();
247 
248 	return ret;
249 }
250 
rndis_set_link_state(struct rndis_device * rdev,struct rndis_request * request)251 static void rndis_set_link_state(struct rndis_device *rdev,
252 				 struct rndis_request *request)
253 {
254 	u32 link_status;
255 	struct rndis_query_complete *query_complete;
256 	u32 msg_len = request->response_msg.msg_len;
257 
258 	/* Ensure the packet is big enough to access its fields */
259 	if (msg_len - RNDIS_HEADER_SIZE < sizeof(struct rndis_query_complete))
260 		return;
261 
262 	query_complete = &request->response_msg.msg.query_complete;
263 
264 	if (query_complete->status == RNDIS_STATUS_SUCCESS &&
265 	    query_complete->info_buflen >= sizeof(u32) &&
266 	    query_complete->info_buf_offset >= sizeof(*query_complete) &&
267 	    msg_len - RNDIS_HEADER_SIZE >= query_complete->info_buf_offset &&
268 	    msg_len - RNDIS_HEADER_SIZE - query_complete->info_buf_offset
269 			>= query_complete->info_buflen) {
270 		memcpy(&link_status, (void *)((unsigned long)query_complete +
271 		       query_complete->info_buf_offset), sizeof(u32));
272 		rdev->link_state = link_status != 0;
273 	}
274 }
275 
rndis_filter_receive_response(struct net_device * ndev,struct netvsc_device * nvdev,struct rndis_message * resp,void * data)276 static void rndis_filter_receive_response(struct net_device *ndev,
277 					  struct netvsc_device *nvdev,
278 					  struct rndis_message *resp,
279 					  void *data)
280 {
281 	u32 *req_id = &resp->msg.init_complete.req_id;
282 	struct rndis_device *dev = nvdev->extension;
283 	struct rndis_request *request = NULL;
284 	bool found = false;
285 	unsigned long flags;
286 
287 	/* This should never happen, it means control message
288 	 * response received after device removed.
289 	 */
290 	if (dev->state == RNDIS_DEV_UNINITIALIZED) {
291 		netdev_err(ndev,
292 			   "got rndis message uninitialized\n");
293 		return;
294 	}
295 
296 	/* Ensure the packet is big enough to read req_id. Req_id is the 1st
297 	 * field in any request/response message, so the payload should have at
298 	 * least sizeof(u32) bytes
299 	 */
300 	if (resp->msg_len - RNDIS_HEADER_SIZE < sizeof(u32)) {
301 		netdev_err(ndev, "rndis msg_len too small: %u\n",
302 			   resp->msg_len);
303 		return;
304 	}
305 
306 	/* Copy the request ID into nvchan->recv_buf */
307 	*req_id = *(u32 *)(data + RNDIS_HEADER_SIZE);
308 
309 	spin_lock_irqsave(&dev->request_lock, flags);
310 	list_for_each_entry(request, &dev->req_list, list_ent) {
311 		/*
312 		 * All request/response message contains RequestId as the 1st
313 		 * field
314 		 */
315 		if (request->request_msg.msg.init_req.req_id == *req_id) {
316 			found = true;
317 			break;
318 		}
319 	}
320 	spin_unlock_irqrestore(&dev->request_lock, flags);
321 
322 	if (found) {
323 		if (resp->msg_len <=
324 		    sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
325 			memcpy(&request->response_msg, resp, RNDIS_HEADER_SIZE + sizeof(*req_id));
326 			unsafe_memcpy((void *)&request->response_msg + RNDIS_HEADER_SIZE + sizeof(*req_id),
327 			       data + RNDIS_HEADER_SIZE + sizeof(*req_id),
328 			       resp->msg_len - RNDIS_HEADER_SIZE - sizeof(*req_id),
329 			       "request->response_msg is followed by a padding of RNDIS_EXT_LEN inside rndis_request");
330 			if (request->request_msg.ndis_msg_type ==
331 			    RNDIS_MSG_QUERY && request->request_msg.msg.
332 			    query_req.oid == RNDIS_OID_GEN_MEDIA_CONNECT_STATUS)
333 				rndis_set_link_state(dev, request);
334 		} else {
335 			netdev_err(ndev,
336 				"rndis response buffer overflow "
337 				"detected (size %u max %zu)\n",
338 				resp->msg_len,
339 				sizeof(struct rndis_message));
340 
341 			if (resp->ndis_msg_type ==
342 			    RNDIS_MSG_RESET_C) {
343 				/* does not have a request id field */
344 				request->response_msg.msg.reset_complete.
345 					status = RNDIS_STATUS_BUFFER_OVERFLOW;
346 			} else {
347 				request->response_msg.msg.
348 				init_complete.status =
349 					RNDIS_STATUS_BUFFER_OVERFLOW;
350 			}
351 		}
352 
353 		netvsc_dma_unmap(((struct net_device_context *)
354 			netdev_priv(ndev))->device_ctx, &request->pkt);
355 		complete(&request->wait_event);
356 	} else {
357 		netdev_err(ndev,
358 			"no rndis request found for this response "
359 			"(id 0x%x res type 0x%x)\n",
360 			*req_id,
361 			resp->ndis_msg_type);
362 	}
363 }
364 
365 /*
366  * Get the Per-Packet-Info with the specified type
367  * return NULL if not found.
368  */
rndis_get_ppi(struct net_device * ndev,struct rndis_packet * rpkt,u32 rpkt_len,u32 type,u8 internal,u32 ppi_size,void * data)369 static inline void *rndis_get_ppi(struct net_device *ndev,
370 				  struct rndis_packet *rpkt,
371 				  u32 rpkt_len, u32 type, u8 internal,
372 				  u32 ppi_size, void *data)
373 {
374 	struct rndis_per_packet_info *ppi;
375 	int len;
376 
377 	if (rpkt->per_pkt_info_offset == 0)
378 		return NULL;
379 
380 	/* Validate info_offset and info_len */
381 	if (rpkt->per_pkt_info_offset < sizeof(struct rndis_packet) ||
382 	    rpkt->per_pkt_info_offset > rpkt_len) {
383 		netdev_err(ndev, "Invalid per_pkt_info_offset: %u\n",
384 			   rpkt->per_pkt_info_offset);
385 		return NULL;
386 	}
387 
388 	if (rpkt->per_pkt_info_len < sizeof(*ppi) ||
389 	    rpkt->per_pkt_info_len > rpkt_len - rpkt->per_pkt_info_offset) {
390 		netdev_err(ndev, "Invalid per_pkt_info_len: %u\n",
391 			   rpkt->per_pkt_info_len);
392 		return NULL;
393 	}
394 
395 	ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
396 		rpkt->per_pkt_info_offset);
397 	/* Copy the PPIs into nvchan->recv_buf */
398 	memcpy(ppi, data + RNDIS_HEADER_SIZE + rpkt->per_pkt_info_offset, rpkt->per_pkt_info_len);
399 	len = rpkt->per_pkt_info_len;
400 
401 	while (len > 0) {
402 		/* Validate ppi_offset and ppi_size */
403 		if (ppi->size > len) {
404 			netdev_err(ndev, "Invalid ppi size: %u\n", ppi->size);
405 			continue;
406 		}
407 
408 		if (ppi->ppi_offset >= ppi->size) {
409 			netdev_err(ndev, "Invalid ppi_offset: %u\n", ppi->ppi_offset);
410 			continue;
411 		}
412 
413 		if (ppi->type == type && ppi->internal == internal) {
414 			/* ppi->size should be big enough to hold the returned object. */
415 			if (ppi->size - ppi->ppi_offset < ppi_size) {
416 				netdev_err(ndev, "Invalid ppi: size %u ppi_offset %u\n",
417 					   ppi->size, ppi->ppi_offset);
418 				continue;
419 			}
420 			return (void *)((ulong)ppi + ppi->ppi_offset);
421 		}
422 		len -= ppi->size;
423 		ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
424 	}
425 
426 	return NULL;
427 }
428 
429 static inline
rsc_add_data(struct netvsc_channel * nvchan,const struct ndis_pkt_8021q_info * vlan,const struct ndis_tcp_ip_checksum_info * csum_info,const u32 * hash_info,void * data,u32 len)430 void rsc_add_data(struct netvsc_channel *nvchan,
431 		  const struct ndis_pkt_8021q_info *vlan,
432 		  const struct ndis_tcp_ip_checksum_info *csum_info,
433 		  const u32 *hash_info,
434 		  void *data, u32 len)
435 {
436 	u32 cnt = nvchan->rsc.cnt;
437 
438 	if (cnt) {
439 		nvchan->rsc.pktlen += len;
440 	} else {
441 		/* The data/values pointed by vlan, csum_info and hash_info are shared
442 		 * across the different 'fragments' of the RSC packet; store them into
443 		 * the packet itself.
444 		 */
445 		if (vlan != NULL) {
446 			memcpy(&nvchan->rsc.vlan, vlan, sizeof(*vlan));
447 			nvchan->rsc.ppi_flags |= NVSC_RSC_VLAN;
448 		} else {
449 			nvchan->rsc.ppi_flags &= ~NVSC_RSC_VLAN;
450 		}
451 		if (csum_info != NULL) {
452 			memcpy(&nvchan->rsc.csum_info, csum_info, sizeof(*csum_info));
453 			nvchan->rsc.ppi_flags |= NVSC_RSC_CSUM_INFO;
454 		} else {
455 			nvchan->rsc.ppi_flags &= ~NVSC_RSC_CSUM_INFO;
456 		}
457 		nvchan->rsc.pktlen = len;
458 		if (hash_info != NULL) {
459 			nvchan->rsc.hash_info = *hash_info;
460 			nvchan->rsc.ppi_flags |= NVSC_RSC_HASH_INFO;
461 		} else {
462 			nvchan->rsc.ppi_flags &= ~NVSC_RSC_HASH_INFO;
463 		}
464 	}
465 
466 	nvchan->rsc.data[cnt] = data;
467 	nvchan->rsc.len[cnt] = len;
468 	nvchan->rsc.cnt++;
469 }
470 
rndis_filter_receive_data(struct net_device * ndev,struct netvsc_device * nvdev,struct netvsc_channel * nvchan,struct rndis_message * msg,void * data,u32 data_buflen)471 static int rndis_filter_receive_data(struct net_device *ndev,
472 				     struct netvsc_device *nvdev,
473 				     struct netvsc_channel *nvchan,
474 				     struct rndis_message *msg,
475 				     void *data, u32 data_buflen)
476 {
477 	struct rndis_packet *rndis_pkt = &msg->msg.pkt;
478 	const struct ndis_tcp_ip_checksum_info *csum_info;
479 	const struct ndis_pkt_8021q_info *vlan;
480 	const struct rndis_pktinfo_id *pktinfo_id;
481 	const u32 *hash_info;
482 	u32 data_offset, rpkt_len;
483 	bool rsc_more = false;
484 	int ret;
485 
486 	/* Ensure data_buflen is big enough to read header fields */
487 	if (data_buflen < RNDIS_HEADER_SIZE + sizeof(struct rndis_packet)) {
488 		netdev_err(ndev, "invalid rndis pkt, data_buflen too small: %u\n",
489 			   data_buflen);
490 		return NVSP_STAT_FAIL;
491 	}
492 
493 	/* Copy the RNDIS packet into nvchan->recv_buf */
494 	memcpy(rndis_pkt, data + RNDIS_HEADER_SIZE, sizeof(*rndis_pkt));
495 
496 	/* Validate rndis_pkt offset */
497 	if (rndis_pkt->data_offset >= data_buflen - RNDIS_HEADER_SIZE) {
498 		netdev_err(ndev, "invalid rndis packet offset: %u\n",
499 			   rndis_pkt->data_offset);
500 		return NVSP_STAT_FAIL;
501 	}
502 
503 	/* Remove the rndis header and pass it back up the stack */
504 	data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
505 
506 	rpkt_len = data_buflen - RNDIS_HEADER_SIZE;
507 	data_buflen -= data_offset;
508 
509 	/*
510 	 * Make sure we got a valid RNDIS message, now total_data_buflen
511 	 * should be the data packet size plus the trailer padding size
512 	 */
513 	if (unlikely(data_buflen < rndis_pkt->data_len)) {
514 		netdev_err(ndev, "rndis message buffer "
515 			   "overflow detected (got %u, min %u)"
516 			   "...dropping this message!\n",
517 			   data_buflen, rndis_pkt->data_len);
518 		return NVSP_STAT_FAIL;
519 	}
520 
521 	vlan = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, IEEE_8021Q_INFO, 0, sizeof(*vlan),
522 			     data);
523 
524 	csum_info = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, TCPIP_CHKSUM_PKTINFO, 0,
525 				  sizeof(*csum_info), data);
526 
527 	hash_info = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, NBL_HASH_VALUE, 0,
528 				  sizeof(*hash_info), data);
529 
530 	pktinfo_id = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, RNDIS_PKTINFO_ID, 1,
531 				   sizeof(*pktinfo_id), data);
532 
533 	/* Identify RSC frags, drop erroneous packets */
534 	if (pktinfo_id && (pktinfo_id->flag & RNDIS_PKTINFO_SUBALLOC)) {
535 		if (pktinfo_id->flag & RNDIS_PKTINFO_1ST_FRAG)
536 			nvchan->rsc.cnt = 0;
537 		else if (nvchan->rsc.cnt == 0)
538 			goto drop;
539 
540 		rsc_more = true;
541 
542 		if (pktinfo_id->flag & RNDIS_PKTINFO_LAST_FRAG)
543 			rsc_more = false;
544 
545 		if (rsc_more && nvchan->rsc.is_last)
546 			goto drop;
547 	} else {
548 		nvchan->rsc.cnt = 0;
549 	}
550 
551 	if (unlikely(nvchan->rsc.cnt >= NVSP_RSC_MAX))
552 		goto drop;
553 
554 	/* Put data into per channel structure.
555 	 * Also, remove the rndis trailer padding from rndis packet message
556 	 * rndis_pkt->data_len tell us the real data length, we only copy
557 	 * the data packet to the stack, without the rndis trailer padding
558 	 */
559 	rsc_add_data(nvchan, vlan, csum_info, hash_info,
560 		     data + data_offset, rndis_pkt->data_len);
561 
562 	if (rsc_more)
563 		return NVSP_STAT_SUCCESS;
564 
565 	ret = netvsc_recv_callback(ndev, nvdev, nvchan);
566 	nvchan->rsc.cnt = 0;
567 
568 	return ret;
569 
570 drop:
571 	return NVSP_STAT_FAIL;
572 }
573 
rndis_filter_receive(struct net_device * ndev,struct netvsc_device * net_dev,struct netvsc_channel * nvchan,void * data,u32 buflen)574 int rndis_filter_receive(struct net_device *ndev,
575 			 struct netvsc_device *net_dev,
576 			 struct netvsc_channel *nvchan,
577 			 void *data, u32 buflen)
578 {
579 	struct net_device_context *net_device_ctx = netdev_priv(ndev);
580 	struct rndis_message *rndis_msg = nvchan->recv_buf;
581 
582 	if (buflen < RNDIS_HEADER_SIZE) {
583 		netdev_err(ndev, "Invalid rndis_msg (buflen: %u)\n", buflen);
584 		return NVSP_STAT_FAIL;
585 	}
586 
587 	/* Copy the RNDIS msg header into nvchan->recv_buf */
588 	memcpy(rndis_msg, data, RNDIS_HEADER_SIZE);
589 
590 	/* Validate incoming rndis_message packet */
591 	if (rndis_msg->msg_len < RNDIS_HEADER_SIZE ||
592 	    buflen < rndis_msg->msg_len) {
593 		netdev_err(ndev, "Invalid rndis_msg (buflen: %u, msg_len: %u)\n",
594 			   buflen, rndis_msg->msg_len);
595 		return NVSP_STAT_FAIL;
596 	}
597 
598 	if (netif_msg_rx_status(net_device_ctx))
599 		dump_rndis_message(ndev, rndis_msg, data);
600 
601 	switch (rndis_msg->ndis_msg_type) {
602 	case RNDIS_MSG_PACKET:
603 		return rndis_filter_receive_data(ndev, net_dev, nvchan,
604 						 rndis_msg, data, buflen);
605 	case RNDIS_MSG_INIT_C:
606 	case RNDIS_MSG_QUERY_C:
607 	case RNDIS_MSG_SET_C:
608 		/* completion msgs */
609 		rndis_filter_receive_response(ndev, net_dev, rndis_msg, data);
610 		break;
611 
612 	case RNDIS_MSG_INDICATE:
613 		/* notification msgs */
614 		netvsc_linkstatus_callback(ndev, rndis_msg, data, buflen);
615 		break;
616 	default:
617 		netdev_err(ndev,
618 			"unhandled rndis message (type %u len %u)\n",
619 			   rndis_msg->ndis_msg_type,
620 			   rndis_msg->msg_len);
621 		return NVSP_STAT_FAIL;
622 	}
623 
624 	return NVSP_STAT_SUCCESS;
625 }
626 
rndis_filter_query_device(struct rndis_device * dev,struct netvsc_device * nvdev,u32 oid,void * result,u32 * result_size)627 static int rndis_filter_query_device(struct rndis_device *dev,
628 				     struct netvsc_device *nvdev,
629 				     u32 oid, void *result, u32 *result_size)
630 {
631 	struct rndis_request *request;
632 	u32 inresult_size = *result_size;
633 	struct rndis_query_request *query;
634 	struct rndis_query_complete *query_complete;
635 	u32 msg_len;
636 	int ret = 0;
637 
638 	if (!result)
639 		return -EINVAL;
640 
641 	*result_size = 0;
642 	request = get_rndis_request(dev, RNDIS_MSG_QUERY,
643 			RNDIS_MESSAGE_SIZE(struct rndis_query_request));
644 	if (!request) {
645 		ret = -ENOMEM;
646 		goto cleanup;
647 	}
648 
649 	/* Setup the rndis query */
650 	query = &request->request_msg.msg.query_req;
651 	query->oid = oid;
652 	query->info_buf_offset = sizeof(struct rndis_query_request);
653 	query->info_buflen = 0;
654 	query->dev_vc_handle = 0;
655 
656 	if (oid == OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES) {
657 		struct ndis_offload *hwcaps;
658 		u32 nvsp_version = nvdev->nvsp_version;
659 		u8 ndis_rev;
660 		size_t size;
661 
662 		if (nvsp_version >= NVSP_PROTOCOL_VERSION_5) {
663 			ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
664 			size = NDIS_OFFLOAD_SIZE;
665 		} else if (nvsp_version >= NVSP_PROTOCOL_VERSION_4) {
666 			ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_2;
667 			size = NDIS_OFFLOAD_SIZE_6_1;
668 		} else {
669 			ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_1;
670 			size = NDIS_OFFLOAD_SIZE_6_0;
671 		}
672 
673 		request->request_msg.msg_len += size;
674 		query->info_buflen = size;
675 		hwcaps = (struct ndis_offload *)
676 			((unsigned long)query + query->info_buf_offset);
677 
678 		hwcaps->header.type = NDIS_OBJECT_TYPE_OFFLOAD;
679 		hwcaps->header.revision = ndis_rev;
680 		hwcaps->header.size = size;
681 
682 	} else if (oid == OID_GEN_RECEIVE_SCALE_CAPABILITIES) {
683 		struct ndis_recv_scale_cap *cap;
684 
685 		request->request_msg.msg_len +=
686 			sizeof(struct ndis_recv_scale_cap);
687 		query->info_buflen = sizeof(struct ndis_recv_scale_cap);
688 		cap = (struct ndis_recv_scale_cap *)((unsigned long)query +
689 						     query->info_buf_offset);
690 		cap->hdr.type = NDIS_OBJECT_TYPE_RSS_CAPABILITIES;
691 		cap->hdr.rev = NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2;
692 		cap->hdr.size = sizeof(struct ndis_recv_scale_cap);
693 	}
694 
695 	ret = rndis_filter_send_request(dev, request);
696 	if (ret != 0)
697 		goto cleanup;
698 
699 	wait_for_completion(&request->wait_event);
700 
701 	/* Copy the response back */
702 	query_complete = &request->response_msg.msg.query_complete;
703 	msg_len = request->response_msg.msg_len;
704 
705 	/* Ensure the packet is big enough to access its fields */
706 	if (msg_len - RNDIS_HEADER_SIZE < sizeof(struct rndis_query_complete)) {
707 		ret = -1;
708 		goto cleanup;
709 	}
710 
711 	if (query_complete->info_buflen > inresult_size ||
712 	    query_complete->info_buf_offset < sizeof(*query_complete) ||
713 	    msg_len - RNDIS_HEADER_SIZE < query_complete->info_buf_offset ||
714 	    msg_len - RNDIS_HEADER_SIZE - query_complete->info_buf_offset
715 			< query_complete->info_buflen) {
716 		ret = -1;
717 		goto cleanup;
718 	}
719 
720 	memcpy(result,
721 	       (void *)((unsigned long)query_complete +
722 			 query_complete->info_buf_offset),
723 	       query_complete->info_buflen);
724 
725 	*result_size = query_complete->info_buflen;
726 
727 cleanup:
728 	if (request)
729 		put_rndis_request(dev, request);
730 
731 	return ret;
732 }
733 
734 /* Get the hardware offload capabilities */
735 static int
rndis_query_hwcaps(struct rndis_device * dev,struct netvsc_device * net_device,struct ndis_offload * caps)736 rndis_query_hwcaps(struct rndis_device *dev, struct netvsc_device *net_device,
737 		   struct ndis_offload *caps)
738 {
739 	u32 caps_len = sizeof(*caps);
740 	int ret;
741 
742 	memset(caps, 0, sizeof(*caps));
743 
744 	ret = rndis_filter_query_device(dev, net_device,
745 					OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
746 					caps, &caps_len);
747 	if (ret)
748 		return ret;
749 
750 	if (caps->header.type != NDIS_OBJECT_TYPE_OFFLOAD) {
751 		netdev_warn(dev->ndev, "invalid NDIS objtype %#x\n",
752 			    caps->header.type);
753 		return -EINVAL;
754 	}
755 
756 	if (caps->header.revision < NDIS_OFFLOAD_PARAMETERS_REVISION_1) {
757 		netdev_warn(dev->ndev, "invalid NDIS objrev %x\n",
758 			    caps->header.revision);
759 		return -EINVAL;
760 	}
761 
762 	if (caps->header.size > caps_len ||
763 	    caps->header.size < NDIS_OFFLOAD_SIZE_6_0) {
764 		netdev_warn(dev->ndev,
765 			    "invalid NDIS objsize %u, data size %u\n",
766 			    caps->header.size, caps_len);
767 		return -EINVAL;
768 	}
769 
770 	return 0;
771 }
772 
rndis_filter_query_device_mac(struct rndis_device * dev,struct netvsc_device * net_device)773 static int rndis_filter_query_device_mac(struct rndis_device *dev,
774 					 struct netvsc_device *net_device)
775 {
776 	u32 size = ETH_ALEN;
777 
778 	return rndis_filter_query_device(dev, net_device,
779 				      RNDIS_OID_802_3_PERMANENT_ADDRESS,
780 				      dev->hw_mac_adr, &size);
781 }
782 
783 #define NWADR_STR "NetworkAddress"
784 #define NWADR_STRLEN 14
785 
rndis_filter_set_device_mac(struct netvsc_device * nvdev,const char * mac)786 int rndis_filter_set_device_mac(struct netvsc_device *nvdev,
787 				const char *mac)
788 {
789 	struct rndis_device *rdev = nvdev->extension;
790 	struct rndis_request *request;
791 	struct rndis_set_request *set;
792 	struct rndis_config_parameter_info *cpi;
793 	wchar_t *cfg_nwadr, *cfg_mac;
794 	struct rndis_set_complete *set_complete;
795 	char macstr[2*ETH_ALEN+1];
796 	u32 extlen = sizeof(struct rndis_config_parameter_info) +
797 		2*NWADR_STRLEN + 4*ETH_ALEN;
798 	int ret;
799 
800 	request = get_rndis_request(rdev, RNDIS_MSG_SET,
801 		RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
802 	if (!request)
803 		return -ENOMEM;
804 
805 	set = &request->request_msg.msg.set_req;
806 	set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
807 	set->info_buflen = extlen;
808 	set->info_buf_offset = sizeof(struct rndis_set_request);
809 	set->dev_vc_handle = 0;
810 
811 	cpi = (struct rndis_config_parameter_info *)((ulong)set +
812 		set->info_buf_offset);
813 	cpi->parameter_name_offset =
814 		sizeof(struct rndis_config_parameter_info);
815 	/* Multiply by 2 because host needs 2 bytes (utf16) for each char */
816 	cpi->parameter_name_length = 2*NWADR_STRLEN;
817 	cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
818 	cpi->parameter_value_offset =
819 		cpi->parameter_name_offset + cpi->parameter_name_length;
820 	/* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
821 	cpi->parameter_value_length = 4*ETH_ALEN;
822 
823 	cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
824 	cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
825 	ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
826 			      cfg_nwadr, NWADR_STRLEN);
827 	if (ret < 0)
828 		goto cleanup;
829 	snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
830 	ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
831 			      cfg_mac, 2*ETH_ALEN);
832 	if (ret < 0)
833 		goto cleanup;
834 
835 	ret = rndis_filter_send_request(rdev, request);
836 	if (ret != 0)
837 		goto cleanup;
838 
839 	wait_for_completion(&request->wait_event);
840 
841 	set_complete = &request->response_msg.msg.set_complete;
842 	if (set_complete->status != RNDIS_STATUS_SUCCESS)
843 		ret = -EIO;
844 
845 cleanup:
846 	put_rndis_request(rdev, request);
847 	return ret;
848 }
849 
850 int
rndis_filter_set_offload_params(struct net_device * ndev,struct netvsc_device * nvdev,struct ndis_offload_params * req_offloads)851 rndis_filter_set_offload_params(struct net_device *ndev,
852 				struct netvsc_device *nvdev,
853 				struct ndis_offload_params *req_offloads)
854 {
855 	struct rndis_device *rdev = nvdev->extension;
856 	struct rndis_request *request;
857 	struct rndis_set_request *set;
858 	struct ndis_offload_params *offload_params;
859 	struct rndis_set_complete *set_complete;
860 	u32 extlen = sizeof(struct ndis_offload_params);
861 	int ret;
862 	u32 vsp_version = nvdev->nvsp_version;
863 
864 	if (vsp_version <= NVSP_PROTOCOL_VERSION_4) {
865 		extlen = VERSION_4_OFFLOAD_SIZE;
866 		/* On NVSP_PROTOCOL_VERSION_4 and below, we do not support
867 		 * UDP checksum offload.
868 		 */
869 		req_offloads->udp_ip_v4_csum = 0;
870 		req_offloads->udp_ip_v6_csum = 0;
871 	}
872 
873 	request = get_rndis_request(rdev, RNDIS_MSG_SET,
874 		RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
875 	if (!request)
876 		return -ENOMEM;
877 
878 	set = &request->request_msg.msg.set_req;
879 	set->oid = OID_TCP_OFFLOAD_PARAMETERS;
880 	set->info_buflen = extlen;
881 	set->info_buf_offset = sizeof(struct rndis_set_request);
882 	set->dev_vc_handle = 0;
883 
884 	offload_params = (struct ndis_offload_params *)((ulong)set +
885 				set->info_buf_offset);
886 	*offload_params = *req_offloads;
887 	offload_params->header.type = NDIS_OBJECT_TYPE_DEFAULT;
888 	offload_params->header.revision = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
889 	offload_params->header.size = extlen;
890 
891 	ret = rndis_filter_send_request(rdev, request);
892 	if (ret != 0)
893 		goto cleanup;
894 
895 	wait_for_completion(&request->wait_event);
896 	set_complete = &request->response_msg.msg.set_complete;
897 	if (set_complete->status != RNDIS_STATUS_SUCCESS) {
898 		netdev_err(ndev, "Fail to set offload on host side:0x%x\n",
899 			   set_complete->status);
900 		ret = -EINVAL;
901 	}
902 
903 cleanup:
904 	put_rndis_request(rdev, request);
905 	return ret;
906 }
907 
rndis_set_rss_param_msg(struct rndis_device * rdev,const u8 * rss_key,u16 flag)908 static int rndis_set_rss_param_msg(struct rndis_device *rdev,
909 				   const u8 *rss_key, u16 flag)
910 {
911 	struct net_device *ndev = rdev->ndev;
912 	struct net_device_context *ndc = netdev_priv(ndev);
913 	struct rndis_request *request;
914 	struct rndis_set_request *set;
915 	struct rndis_set_complete *set_complete;
916 	u32 extlen = sizeof(struct ndis_recv_scale_param) +
917 		     4 * ndc->rx_table_sz + NETVSC_HASH_KEYLEN;
918 	struct ndis_recv_scale_param *rssp;
919 	u32 *itab;
920 	u8 *keyp;
921 	int i, ret;
922 
923 	request = get_rndis_request(
924 			rdev, RNDIS_MSG_SET,
925 			RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
926 	if (!request)
927 		return -ENOMEM;
928 
929 	set = &request->request_msg.msg.set_req;
930 	set->oid = OID_GEN_RECEIVE_SCALE_PARAMETERS;
931 	set->info_buflen = extlen;
932 	set->info_buf_offset = sizeof(struct rndis_set_request);
933 	set->dev_vc_handle = 0;
934 
935 	rssp = (struct ndis_recv_scale_param *)(set + 1);
936 	rssp->hdr.type = NDIS_OBJECT_TYPE_RSS_PARAMETERS;
937 	rssp->hdr.rev = NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2;
938 	rssp->hdr.size = sizeof(struct ndis_recv_scale_param);
939 	rssp->flag = flag;
940 	rssp->hashinfo = NDIS_HASH_FUNC_TOEPLITZ | NDIS_HASH_IPV4 |
941 			 NDIS_HASH_TCP_IPV4 | NDIS_HASH_IPV6 |
942 			 NDIS_HASH_TCP_IPV6;
943 	rssp->indirect_tabsize = 4 * ndc->rx_table_sz;
944 	rssp->indirect_taboffset = sizeof(struct ndis_recv_scale_param);
945 	rssp->hashkey_size = NETVSC_HASH_KEYLEN;
946 	rssp->hashkey_offset = rssp->indirect_taboffset +
947 			       rssp->indirect_tabsize;
948 
949 	/* Set indirection table entries */
950 	itab = (u32 *)(rssp + 1);
951 	for (i = 0; i < ndc->rx_table_sz; i++)
952 		itab[i] = ndc->rx_table[i];
953 
954 	/* Set hask key values */
955 	keyp = (u8 *)((unsigned long)rssp + rssp->hashkey_offset);
956 	memcpy(keyp, rss_key, NETVSC_HASH_KEYLEN);
957 
958 	ret = rndis_filter_send_request(rdev, request);
959 	if (ret != 0)
960 		goto cleanup;
961 
962 	wait_for_completion(&request->wait_event);
963 	set_complete = &request->response_msg.msg.set_complete;
964 	if (set_complete->status == RNDIS_STATUS_SUCCESS) {
965 		if (!(flag & NDIS_RSS_PARAM_FLAG_DISABLE_RSS) &&
966 		    !(flag & NDIS_RSS_PARAM_FLAG_HASH_KEY_UNCHANGED))
967 			memcpy(rdev->rss_key, rss_key, NETVSC_HASH_KEYLEN);
968 
969 	} else {
970 		netdev_err(ndev, "Fail to set RSS parameters:0x%x\n",
971 			   set_complete->status);
972 		ret = -EINVAL;
973 	}
974 
975 cleanup:
976 	put_rndis_request(rdev, request);
977 	return ret;
978 }
979 
rndis_filter_set_rss_param(struct rndis_device * rdev,const u8 * rss_key)980 int rndis_filter_set_rss_param(struct rndis_device *rdev,
981 			       const u8 *rss_key)
982 {
983 	/* Disable RSS before change */
984 	rndis_set_rss_param_msg(rdev, rss_key,
985 				NDIS_RSS_PARAM_FLAG_DISABLE_RSS);
986 
987 	return rndis_set_rss_param_msg(rdev, rss_key, 0);
988 }
989 
rndis_filter_query_device_link_status(struct rndis_device * dev,struct netvsc_device * net_device)990 static int rndis_filter_query_device_link_status(struct rndis_device *dev,
991 						 struct netvsc_device *net_device)
992 {
993 	u32 size = sizeof(u32);
994 	u32 link_status;
995 
996 	return rndis_filter_query_device(dev, net_device,
997 					 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
998 					 &link_status, &size);
999 }
1000 
rndis_filter_query_link_speed(struct rndis_device * dev,struct netvsc_device * net_device)1001 static int rndis_filter_query_link_speed(struct rndis_device *dev,
1002 					 struct netvsc_device *net_device)
1003 {
1004 	u32 size = sizeof(u32);
1005 	u32 link_speed;
1006 	struct net_device_context *ndc;
1007 	int ret;
1008 
1009 	ret = rndis_filter_query_device(dev, net_device,
1010 					RNDIS_OID_GEN_LINK_SPEED,
1011 					&link_speed, &size);
1012 
1013 	if (!ret) {
1014 		ndc = netdev_priv(dev->ndev);
1015 
1016 		/* The link speed reported from host is in 100bps unit, so
1017 		 * we convert it to Mbps here.
1018 		 */
1019 		ndc->speed = link_speed / 10000;
1020 	}
1021 
1022 	return ret;
1023 }
1024 
rndis_filter_set_packet_filter(struct rndis_device * dev,u32 new_filter)1025 static int rndis_filter_set_packet_filter(struct rndis_device *dev,
1026 					  u32 new_filter)
1027 {
1028 	struct rndis_request *request;
1029 	struct rndis_set_request *set;
1030 	int ret;
1031 
1032 	if (dev->filter == new_filter)
1033 		return 0;
1034 
1035 	request = get_rndis_request(dev, RNDIS_MSG_SET,
1036 			RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
1037 			sizeof(u32));
1038 	if (!request)
1039 		return -ENOMEM;
1040 
1041 	/* Setup the rndis set */
1042 	set = &request->request_msg.msg.set_req;
1043 	set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
1044 	set->info_buflen = sizeof(u32);
1045 	set->info_buf_offset = offsetof(typeof(*set), info_buf);
1046 	memcpy(set->info_buf, &new_filter, sizeof(u32));
1047 
1048 	ret = rndis_filter_send_request(dev, request);
1049 	if (ret == 0) {
1050 		wait_for_completion(&request->wait_event);
1051 		dev->filter = new_filter;
1052 	}
1053 
1054 	put_rndis_request(dev, request);
1055 
1056 	return ret;
1057 }
1058 
rndis_set_multicast(struct work_struct * w)1059 static void rndis_set_multicast(struct work_struct *w)
1060 {
1061 	struct rndis_device *rdev
1062 		= container_of(w, struct rndis_device, mcast_work);
1063 	u32 filter = NDIS_PACKET_TYPE_DIRECTED;
1064 	unsigned int flags = rdev->ndev->flags;
1065 
1066 	if (flags & IFF_PROMISC) {
1067 		filter = NDIS_PACKET_TYPE_PROMISCUOUS;
1068 	} else {
1069 		if (!netdev_mc_empty(rdev->ndev) || (flags & IFF_ALLMULTI))
1070 			filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
1071 		if (flags & IFF_BROADCAST)
1072 			filter |= NDIS_PACKET_TYPE_BROADCAST;
1073 	}
1074 
1075 	rndis_filter_set_packet_filter(rdev, filter);
1076 }
1077 
rndis_filter_update(struct netvsc_device * nvdev)1078 void rndis_filter_update(struct netvsc_device *nvdev)
1079 {
1080 	struct rndis_device *rdev = nvdev->extension;
1081 
1082 	schedule_work(&rdev->mcast_work);
1083 }
1084 
rndis_filter_init_device(struct rndis_device * dev,struct netvsc_device * nvdev)1085 static int rndis_filter_init_device(struct rndis_device *dev,
1086 				    struct netvsc_device *nvdev)
1087 {
1088 	struct rndis_request *request;
1089 	struct rndis_initialize_request *init;
1090 	struct rndis_initialize_complete *init_complete;
1091 	u32 status;
1092 	int ret;
1093 
1094 	request = get_rndis_request(dev, RNDIS_MSG_INIT,
1095 			RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
1096 	if (!request) {
1097 		ret = -ENOMEM;
1098 		goto cleanup;
1099 	}
1100 
1101 	/* Setup the rndis set */
1102 	init = &request->request_msg.msg.init_req;
1103 	init->major_ver = RNDIS_MAJOR_VERSION;
1104 	init->minor_ver = RNDIS_MINOR_VERSION;
1105 	init->max_xfer_size = 0x4000;
1106 
1107 	dev->state = RNDIS_DEV_INITIALIZING;
1108 
1109 	ret = rndis_filter_send_request(dev, request);
1110 	if (ret != 0) {
1111 		dev->state = RNDIS_DEV_UNINITIALIZED;
1112 		goto cleanup;
1113 	}
1114 
1115 	wait_for_completion(&request->wait_event);
1116 
1117 	init_complete = &request->response_msg.msg.init_complete;
1118 	status = init_complete->status;
1119 	if (status == RNDIS_STATUS_SUCCESS) {
1120 		dev->state = RNDIS_DEV_INITIALIZED;
1121 		nvdev->max_pkt = init_complete->max_pkt_per_msg;
1122 		nvdev->pkt_align = 1 << init_complete->pkt_alignment_factor;
1123 		ret = 0;
1124 	} else {
1125 		dev->state = RNDIS_DEV_UNINITIALIZED;
1126 		ret = -EINVAL;
1127 	}
1128 
1129 cleanup:
1130 	if (request)
1131 		put_rndis_request(dev, request);
1132 
1133 	return ret;
1134 }
1135 
netvsc_device_idle(const struct netvsc_device * nvdev)1136 static bool netvsc_device_idle(const struct netvsc_device *nvdev)
1137 {
1138 	int i;
1139 
1140 	for (i = 0; i < nvdev->num_chn; i++) {
1141 		const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1142 
1143 		if (nvchan->mrc.first != nvchan->mrc.next)
1144 			return false;
1145 
1146 		if (atomic_read(&nvchan->queue_sends) > 0)
1147 			return false;
1148 	}
1149 
1150 	return true;
1151 }
1152 
rndis_filter_halt_device(struct netvsc_device * nvdev,struct rndis_device * dev)1153 static void rndis_filter_halt_device(struct netvsc_device *nvdev,
1154 				     struct rndis_device *dev)
1155 {
1156 	struct rndis_request *request;
1157 	struct rndis_halt_request *halt;
1158 
1159 	/* Attempt to do a rndis device halt */
1160 	request = get_rndis_request(dev, RNDIS_MSG_HALT,
1161 				RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
1162 	if (!request)
1163 		goto cleanup;
1164 
1165 	/* Setup the rndis set */
1166 	halt = &request->request_msg.msg.halt_req;
1167 	halt->req_id = atomic_inc_return(&dev->new_req_id);
1168 
1169 	/* Ignore return since this msg is optional. */
1170 	rndis_filter_send_request(dev, request);
1171 
1172 	dev->state = RNDIS_DEV_UNINITIALIZED;
1173 
1174 cleanup:
1175 	nvdev->destroy = true;
1176 
1177 	/* Force flag to be ordered before waiting */
1178 	wmb();
1179 
1180 	/* Wait for all send completions */
1181 	wait_event(nvdev->wait_drain, netvsc_device_idle(nvdev));
1182 
1183 	if (request)
1184 		put_rndis_request(dev, request);
1185 }
1186 
rndis_filter_open_device(struct rndis_device * dev)1187 static int rndis_filter_open_device(struct rndis_device *dev)
1188 {
1189 	int ret;
1190 
1191 	if (dev->state != RNDIS_DEV_INITIALIZED)
1192 		return 0;
1193 
1194 	ret = rndis_filter_set_packet_filter(dev,
1195 					 NDIS_PACKET_TYPE_BROADCAST |
1196 					 NDIS_PACKET_TYPE_ALL_MULTICAST |
1197 					 NDIS_PACKET_TYPE_DIRECTED);
1198 	if (ret == 0)
1199 		dev->state = RNDIS_DEV_DATAINITIALIZED;
1200 
1201 	return ret;
1202 }
1203 
rndis_filter_close_device(struct rndis_device * dev)1204 static int rndis_filter_close_device(struct rndis_device *dev)
1205 {
1206 	int ret;
1207 
1208 	if (dev->state != RNDIS_DEV_DATAINITIALIZED)
1209 		return 0;
1210 
1211 	/* Make sure rndis_set_multicast doesn't re-enable filter! */
1212 	cancel_work_sync(&dev->mcast_work);
1213 
1214 	ret = rndis_filter_set_packet_filter(dev, 0);
1215 	if (ret == -ENODEV)
1216 		ret = 0;
1217 
1218 	if (ret == 0)
1219 		dev->state = RNDIS_DEV_INITIALIZED;
1220 
1221 	return ret;
1222 }
1223 
netvsc_sc_open(struct vmbus_channel * new_sc)1224 static void netvsc_sc_open(struct vmbus_channel *new_sc)
1225 {
1226 	struct net_device *ndev =
1227 		hv_get_drvdata(new_sc->primary_channel->device_obj);
1228 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1229 	struct netvsc_device *nvscdev;
1230 	u16 chn_index = new_sc->offermsg.offer.sub_channel_index;
1231 	struct netvsc_channel *nvchan;
1232 	int ret;
1233 
1234 	/* This is safe because this callback only happens when
1235 	 * new device is being setup and waiting on the channel_init_wait.
1236 	 */
1237 	nvscdev = rcu_dereference_raw(ndev_ctx->nvdev);
1238 	if (!nvscdev || chn_index >= nvscdev->num_chn)
1239 		return;
1240 
1241 	nvchan = nvscdev->chan_table + chn_index;
1242 
1243 	/* Because the device uses NAPI, all the interrupt batching and
1244 	 * control is done via Net softirq, not the channel handling
1245 	 */
1246 	set_channel_read_mode(new_sc, HV_CALL_ISR);
1247 
1248 	/* Set the channel before opening.*/
1249 	nvchan->channel = new_sc;
1250 
1251 	new_sc->next_request_id_callback = vmbus_next_request_id;
1252 	new_sc->request_addr_callback = vmbus_request_addr;
1253 	new_sc->rqstor_size = netvsc_rqstor_size(netvsc_ring_bytes);
1254 	new_sc->max_pkt_size = NETVSC_MAX_PKT_SIZE;
1255 
1256 	ret = vmbus_open(new_sc, netvsc_ring_bytes,
1257 			 netvsc_ring_bytes, NULL, 0,
1258 			 netvsc_channel_cb, nvchan);
1259 	if (ret == 0)
1260 		napi_enable(&nvchan->napi);
1261 	else
1262 		netdev_notice(ndev, "sub channel open failed: %d\n", ret);
1263 
1264 	if (atomic_inc_return(&nvscdev->open_chn) == nvscdev->num_chn)
1265 		wake_up(&nvscdev->subchan_open);
1266 }
1267 
1268 /* Open sub-channels after completing the handling of the device probe.
1269  * This breaks overlap of processing the host message for the
1270  * new primary channel with the initialization of sub-channels.
1271  */
rndis_set_subchannel(struct net_device * ndev,struct netvsc_device * nvdev,struct netvsc_device_info * dev_info)1272 int rndis_set_subchannel(struct net_device *ndev,
1273 			 struct netvsc_device *nvdev,
1274 			 struct netvsc_device_info *dev_info)
1275 {
1276 	struct nvsp_message *init_packet = &nvdev->channel_init_pkt;
1277 	struct net_device_context *ndev_ctx = netdev_priv(ndev);
1278 	struct hv_device *hv_dev = ndev_ctx->device_ctx;
1279 	struct rndis_device *rdev = nvdev->extension;
1280 	int i, ret;
1281 
1282 	ASSERT_RTNL();
1283 
1284 	memset(init_packet, 0, sizeof(struct nvsp_message));
1285 	init_packet->hdr.msg_type = NVSP_MSG5_TYPE_SUBCHANNEL;
1286 	init_packet->msg.v5_msg.subchn_req.op = NVSP_SUBCHANNEL_ALLOCATE;
1287 	init_packet->msg.v5_msg.subchn_req.num_subchannels =
1288 						nvdev->num_chn - 1;
1289 	trace_nvsp_send(ndev, init_packet);
1290 
1291 	ret = vmbus_sendpacket(hv_dev->channel, init_packet,
1292 			       sizeof(struct nvsp_message),
1293 			       (unsigned long)init_packet,
1294 			       VM_PKT_DATA_INBAND,
1295 			       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1296 	if (ret) {
1297 		netdev_err(ndev, "sub channel allocate send failed: %d\n", ret);
1298 		return ret;
1299 	}
1300 
1301 	wait_for_completion(&nvdev->channel_init_wait);
1302 	if (init_packet->msg.v5_msg.subchn_comp.status != NVSP_STAT_SUCCESS) {
1303 		netdev_err(ndev, "sub channel request failed\n");
1304 		return -EIO;
1305 	}
1306 
1307 	/* Check that number of allocated sub channel is within the expected range */
1308 	if (init_packet->msg.v5_msg.subchn_comp.num_subchannels > nvdev->num_chn - 1) {
1309 		netdev_err(ndev, "invalid number of allocated sub channel\n");
1310 		return -EINVAL;
1311 	}
1312 	nvdev->num_chn = 1 +
1313 		init_packet->msg.v5_msg.subchn_comp.num_subchannels;
1314 
1315 	/* wait for all sub channels to open */
1316 	wait_event(nvdev->subchan_open,
1317 		   atomic_read(&nvdev->open_chn) == nvdev->num_chn);
1318 
1319 	for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
1320 		ndev_ctx->tx_table[i] = i % nvdev->num_chn;
1321 
1322 	/* ignore failures from setting rss parameters, still have channels */
1323 	if (dev_info)
1324 		rndis_filter_set_rss_param(rdev, dev_info->rss_key);
1325 	else
1326 		rndis_filter_set_rss_param(rdev, netvsc_hash_key);
1327 
1328 	netif_set_real_num_tx_queues(ndev, nvdev->num_chn);
1329 	netif_set_real_num_rx_queues(ndev, nvdev->num_chn);
1330 
1331 	return 0;
1332 }
1333 
rndis_netdev_set_hwcaps(struct rndis_device * rndis_device,struct netvsc_device * nvdev)1334 static int rndis_netdev_set_hwcaps(struct rndis_device *rndis_device,
1335 				   struct netvsc_device *nvdev)
1336 {
1337 	struct net_device *net = rndis_device->ndev;
1338 	struct net_device_context *net_device_ctx = netdev_priv(net);
1339 	struct ndis_offload hwcaps;
1340 	struct ndis_offload_params offloads;
1341 	unsigned int gso_max_size = GSO_LEGACY_MAX_SIZE;
1342 	int ret;
1343 
1344 	/* Find HW offload capabilities */
1345 	ret = rndis_query_hwcaps(rndis_device, nvdev, &hwcaps);
1346 	if (ret != 0)
1347 		return ret;
1348 
1349 	/* A value of zero means "no change"; now turn on what we want. */
1350 	memset(&offloads, 0, sizeof(struct ndis_offload_params));
1351 
1352 	/* Linux does not care about IP checksum, always does in kernel */
1353 	offloads.ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED;
1354 
1355 	/* Reset previously set hw_features flags */
1356 	net->hw_features &= ~NETVSC_SUPPORTED_HW_FEATURES;
1357 	net_device_ctx->tx_checksum_mask = 0;
1358 
1359 	/* Compute tx offload settings based on hw capabilities */
1360 	net->hw_features |= NETIF_F_RXCSUM;
1361 	net->hw_features |= NETIF_F_SG;
1362 	net->hw_features |= NETIF_F_RXHASH;
1363 
1364 	if ((hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_ALL_TCP4) == NDIS_TXCSUM_ALL_TCP4) {
1365 		/* Can checksum TCP */
1366 		net->hw_features |= NETIF_F_IP_CSUM;
1367 		net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_TCP;
1368 
1369 		offloads.tcp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1370 
1371 		if (hwcaps.lsov2.ip4_encap & NDIS_OFFLOAD_ENCAP_8023) {
1372 			offloads.lso_v2_ipv4 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1373 			net->hw_features |= NETIF_F_TSO;
1374 
1375 			if (hwcaps.lsov2.ip4_maxsz < gso_max_size)
1376 				gso_max_size = hwcaps.lsov2.ip4_maxsz;
1377 		}
1378 
1379 		if (hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) {
1380 			offloads.udp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1381 			net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_UDP;
1382 		}
1383 	}
1384 
1385 	if ((hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_ALL_TCP6) == NDIS_TXCSUM_ALL_TCP6) {
1386 		net->hw_features |= NETIF_F_IPV6_CSUM;
1387 
1388 		offloads.tcp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1389 		net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_TCP;
1390 
1391 		if ((hwcaps.lsov2.ip6_encap & NDIS_OFFLOAD_ENCAP_8023) &&
1392 		    (hwcaps.lsov2.ip6_opts & NDIS_LSOV2_CAP_IP6) == NDIS_LSOV2_CAP_IP6) {
1393 			offloads.lso_v2_ipv6 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1394 			net->hw_features |= NETIF_F_TSO6;
1395 
1396 			if (hwcaps.lsov2.ip6_maxsz < gso_max_size)
1397 				gso_max_size = hwcaps.lsov2.ip6_maxsz;
1398 		}
1399 
1400 		if (hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_CAP_UDP6) {
1401 			offloads.udp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1402 			net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_UDP;
1403 		}
1404 	}
1405 
1406 	if (hwcaps.rsc.ip4 && hwcaps.rsc.ip6) {
1407 		net->hw_features |= NETIF_F_LRO;
1408 
1409 		if (net->features & NETIF_F_LRO) {
1410 			offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1411 			offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1412 		} else {
1413 			offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1414 			offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1415 		}
1416 	}
1417 
1418 	/* In case some hw_features disappeared we need to remove them from
1419 	 * net->features list as they're no longer supported.
1420 	 */
1421 	net->features &= ~NETVSC_SUPPORTED_HW_FEATURES | net->hw_features;
1422 
1423 	netif_set_tso_max_size(net, gso_max_size);
1424 
1425 	ret = rndis_filter_set_offload_params(net, nvdev, &offloads);
1426 
1427 	return ret;
1428 }
1429 
rndis_get_friendly_name(struct net_device * net,struct rndis_device * rndis_device,struct netvsc_device * net_device)1430 static void rndis_get_friendly_name(struct net_device *net,
1431 				    struct rndis_device *rndis_device,
1432 				    struct netvsc_device *net_device)
1433 {
1434 	ucs2_char_t wname[256];
1435 	unsigned long len;
1436 	u8 ifalias[256];
1437 	u32 size;
1438 
1439 	size = sizeof(wname);
1440 	if (rndis_filter_query_device(rndis_device, net_device,
1441 				      RNDIS_OID_GEN_FRIENDLY_NAME,
1442 				      wname, &size) != 0)
1443 		return;	/* ignore if host does not support */
1444 
1445 	if (size == 0)
1446 		return;	/* name not set */
1447 
1448 	/* Convert Windows Unicode string to UTF-8 */
1449 	len = ucs2_as_utf8(ifalias, wname, sizeof(ifalias));
1450 
1451 	/* ignore the default value from host */
1452 	if (strcmp(ifalias, "Network Adapter") != 0)
1453 		dev_set_alias(net, ifalias, len);
1454 }
1455 
rndis_filter_device_add(struct hv_device * dev,struct netvsc_device_info * device_info)1456 struct netvsc_device *rndis_filter_device_add(struct hv_device *dev,
1457 				      struct netvsc_device_info *device_info)
1458 {
1459 	struct net_device *net = hv_get_drvdata(dev);
1460 	struct net_device_context *ndc = netdev_priv(net);
1461 	struct netvsc_device *net_device;
1462 	struct rndis_device *rndis_device;
1463 	struct ndis_recv_scale_cap rsscap;
1464 	u32 rsscap_size = sizeof(struct ndis_recv_scale_cap);
1465 	u32 mtu, size;
1466 	u32 num_possible_rss_qs;
1467 	int i, ret;
1468 
1469 	rndis_device = get_rndis_device();
1470 	if (!rndis_device)
1471 		return ERR_PTR(-ENODEV);
1472 
1473 	/* Let the inner driver handle this first to create the netvsc channel
1474 	 * NOTE! Once the channel is created, we may get a receive callback
1475 	 * (RndisFilterOnReceive()) before this call is completed
1476 	 */
1477 	net_device = netvsc_device_add(dev, device_info);
1478 	if (IS_ERR(net_device)) {
1479 		kfree(rndis_device);
1480 		return net_device;
1481 	}
1482 
1483 	/* Initialize the rndis device */
1484 	net_device->max_chn = 1;
1485 	net_device->num_chn = 1;
1486 
1487 	net_device->extension = rndis_device;
1488 	rndis_device->ndev = net;
1489 
1490 	/* Send the rndis initialization message */
1491 	ret = rndis_filter_init_device(rndis_device, net_device);
1492 	if (ret != 0)
1493 		goto err_dev_remv;
1494 
1495 	/* Get the MTU from the host */
1496 	size = sizeof(u32);
1497 	ret = rndis_filter_query_device(rndis_device, net_device,
1498 					RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
1499 					&mtu, &size);
1500 	if (ret == 0 && size == sizeof(u32) && mtu < net->mtu)
1501 		net->mtu = mtu;
1502 
1503 	/* Get the mac address */
1504 	ret = rndis_filter_query_device_mac(rndis_device, net_device);
1505 	if (ret != 0)
1506 		goto err_dev_remv;
1507 
1508 	memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
1509 
1510 	/* Get friendly name as ifalias*/
1511 	if (!net->ifalias)
1512 		rndis_get_friendly_name(net, rndis_device, net_device);
1513 
1514 	/* Query and set hardware capabilities */
1515 	ret = rndis_netdev_set_hwcaps(rndis_device, net_device);
1516 	if (ret != 0)
1517 		goto err_dev_remv;
1518 
1519 	rndis_filter_query_device_link_status(rndis_device, net_device);
1520 
1521 	netdev_dbg(net, "Device MAC %pM link state %s\n",
1522 		   rndis_device->hw_mac_adr,
1523 		   rndis_device->link_state ? "down" : "up");
1524 
1525 	if (net_device->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1526 		goto out;
1527 
1528 	rndis_filter_query_link_speed(rndis_device, net_device);
1529 
1530 	/* vRSS setup */
1531 	memset(&rsscap, 0, rsscap_size);
1532 	ret = rndis_filter_query_device(rndis_device, net_device,
1533 					OID_GEN_RECEIVE_SCALE_CAPABILITIES,
1534 					&rsscap, &rsscap_size);
1535 	if (ret || rsscap.num_recv_que < 2)
1536 		goto out;
1537 
1538 	if (rsscap.num_indirect_tabent &&
1539 	    rsscap.num_indirect_tabent <= ITAB_NUM_MAX)
1540 		ndc->rx_table_sz = rsscap.num_indirect_tabent;
1541 	else
1542 		ndc->rx_table_sz = ITAB_NUM;
1543 
1544 	ndc->rx_table = kcalloc(ndc->rx_table_sz, sizeof(u16), GFP_KERNEL);
1545 	if (!ndc->rx_table) {
1546 		ret = -ENOMEM;
1547 		goto err_dev_remv;
1548 	}
1549 
1550 	/* This guarantees that num_possible_rss_qs <= num_online_cpus */
1551 	num_possible_rss_qs = min_t(u32, num_online_cpus(),
1552 				    rsscap.num_recv_que);
1553 
1554 	net_device->max_chn = min_t(u32, VRSS_CHANNEL_MAX, num_possible_rss_qs);
1555 
1556 	/* We will use the given number of channels if available. */
1557 	net_device->num_chn = min(net_device->max_chn, device_info->num_chn);
1558 
1559 	if (!netif_is_rxfh_configured(net)) {
1560 		for (i = 0; i < ndc->rx_table_sz; i++)
1561 			ndc->rx_table[i] = ethtool_rxfh_indir_default(
1562 						i, net_device->num_chn);
1563 	}
1564 
1565 	atomic_set(&net_device->open_chn, 1);
1566 	vmbus_set_sc_create_callback(dev->channel, netvsc_sc_open);
1567 
1568 	for (i = 1; i < net_device->num_chn; i++) {
1569 		ret = netvsc_alloc_recv_comp_ring(net_device, i);
1570 		if (ret) {
1571 			while (--i != 0)
1572 				vfree(net_device->chan_table[i].mrc.slots);
1573 			goto out;
1574 		}
1575 	}
1576 
1577 	for (i = 1; i < net_device->num_chn; i++)
1578 		netif_napi_add(net, &net_device->chan_table[i].napi,
1579 			       netvsc_poll);
1580 
1581 	return net_device;
1582 
1583 out:
1584 	/* setting up multiple channels failed */
1585 	net_device->max_chn = 1;
1586 	net_device->num_chn = 1;
1587 	return net_device;
1588 
1589 err_dev_remv:
1590 	rndis_filter_device_remove(dev, net_device);
1591 	return ERR_PTR(ret);
1592 }
1593 
rndis_filter_device_remove(struct hv_device * dev,struct netvsc_device * net_dev)1594 void rndis_filter_device_remove(struct hv_device *dev,
1595 				struct netvsc_device *net_dev)
1596 {
1597 	struct rndis_device *rndis_dev = net_dev->extension;
1598 	struct net_device *net = hv_get_drvdata(dev);
1599 	struct net_device_context *ndc;
1600 
1601 	ndc = netdev_priv(net);
1602 
1603 	/* Halt and release the rndis device */
1604 	rndis_filter_halt_device(net_dev, rndis_dev);
1605 
1606 	netvsc_device_remove(dev);
1607 
1608 	ndc->rx_table_sz = 0;
1609 	kfree(ndc->rx_table);
1610 	ndc->rx_table = NULL;
1611 }
1612 
rndis_filter_open(struct netvsc_device * nvdev)1613 int rndis_filter_open(struct netvsc_device *nvdev)
1614 {
1615 	if (!nvdev)
1616 		return -EINVAL;
1617 
1618 	return rndis_filter_open_device(nvdev->extension);
1619 }
1620 
rndis_filter_close(struct netvsc_device * nvdev)1621 int rndis_filter_close(struct netvsc_device *nvdev)
1622 {
1623 	if (!nvdev)
1624 		return -EINVAL;
1625 
1626 	return rndis_filter_close_device(nvdev->extension);
1627 }
1628