1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Simple network protocol
4  * PXE base code protocol
5  *
6  * Copyright (c) 2016 Alexander Graf
7  *
8  * The simple network protocol has the following statuses and services
9  * to move between them:
10  *
11  * Start():	 EfiSimpleNetworkStopped     -> EfiSimpleNetworkStarted
12  * Initialize(): EfiSimpleNetworkStarted     -> EfiSimpleNetworkInitialized
13  * Shutdown():	 EfiSimpleNetworkInitialized -> EfiSimpleNetworkStarted
14  * Stop():	 EfiSimpleNetworkStarted     -> EfiSimpleNetworkStopped
15  * Reset():	 EfiSimpleNetworkInitialized -> EfiSimpleNetworkInitialized
16  */
17 
18 #include <common.h>
19 #include <efi_loader.h>
20 #include <malloc.h>
21 #include <net.h>
22 
23 static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
24 static const efi_guid_t efi_pxe_base_code_protocol_guid =
25 					EFI_PXE_BASE_CODE_PROTOCOL_GUID;
26 static struct efi_pxe_packet *dhcp_ack;
27 static void *new_tx_packet;
28 static void *transmit_buffer;
29 static uchar **receive_buffer;
30 static size_t *receive_lengths;
31 static int rx_packet_idx;
32 static int rx_packet_num;
33 static struct efi_net_obj *netobj;
34 
35 /*
36  * The notification function of this event is called in every timer cycle
37  * to check if a new network packet has been received.
38  */
39 static struct efi_event *network_timer_event;
40 /*
41  * This event is signaled when a packet has been received.
42  */
43 static struct efi_event *wait_for_packet;
44 
45 /**
46  * struct efi_net_obj - EFI object representing a network interface
47  *
48  * @header:	EFI object header
49  * @net:	simple network protocol interface
50  * @net_mode:	status of the network interface
51  * @pxe:	PXE base code protocol interface
52  * @pxe_mode:	status of the PXE base code protocol
53  */
54 struct efi_net_obj {
55 	struct efi_object header;
56 	struct efi_simple_network net;
57 	struct efi_simple_network_mode net_mode;
58 	struct efi_pxe_base_code_protocol pxe;
59 	struct efi_pxe_mode pxe_mode;
60 };
61 
62 /*
63  * efi_net_start() - start the network interface
64  *
65  * This function implements the Start service of the
66  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
67  * (UEFI) specification for details.
68  *
69  * @this:	pointer to the protocol instance
70  * Return:	status code
71  */
efi_net_start(struct efi_simple_network * this)72 static efi_status_t EFIAPI efi_net_start(struct efi_simple_network *this)
73 {
74 	efi_status_t ret = EFI_SUCCESS;
75 
76 	EFI_ENTRY("%p", this);
77 
78 	/* Check parameters */
79 	if (!this) {
80 		ret = EFI_INVALID_PARAMETER;
81 		goto out;
82 	}
83 
84 	if (this->mode->state != EFI_NETWORK_STOPPED) {
85 		ret = EFI_ALREADY_STARTED;
86 	} else {
87 		this->int_status = 0;
88 		wait_for_packet->is_signaled = false;
89 		this->mode->state = EFI_NETWORK_STARTED;
90 	}
91 out:
92 	return EFI_EXIT(ret);
93 }
94 
95 /*
96  * efi_net_stop() - stop the network interface
97  *
98  * This function implements the Stop service of the
99  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
100  * (UEFI) specification for details.
101  *
102  * @this:	pointer to the protocol instance
103  * Return:	status code
104  */
efi_net_stop(struct efi_simple_network * this)105 static efi_status_t EFIAPI efi_net_stop(struct efi_simple_network *this)
106 {
107 	efi_status_t ret = EFI_SUCCESS;
108 
109 	EFI_ENTRY("%p", this);
110 
111 	/* Check parameters */
112 	if (!this) {
113 		ret = EFI_INVALID_PARAMETER;
114 		goto out;
115 	}
116 
117 	if (this->mode->state == EFI_NETWORK_STOPPED) {
118 		ret = EFI_NOT_STARTED;
119 	} else {
120 		/* Disable hardware and put it into the reset state */
121 		eth_halt();
122 		/* Clear cache of packets */
123 		rx_packet_num = 0;
124 		this->mode->state = EFI_NETWORK_STOPPED;
125 	}
126 out:
127 	return EFI_EXIT(ret);
128 }
129 
130 /*
131  * efi_net_initialize() - initialize the network interface
132  *
133  * This function implements the Initialize service of the
134  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
135  * (UEFI) specification for details.
136  *
137  * @this:	pointer to the protocol instance
138  * @extra_rx:	extra receive buffer to be allocated
139  * @extra_tx:	extra transmit buffer to be allocated
140  * Return:	status code
141  */
efi_net_initialize(struct efi_simple_network * this,ulong extra_rx,ulong extra_tx)142 static efi_status_t EFIAPI efi_net_initialize(struct efi_simple_network *this,
143 					      ulong extra_rx, ulong extra_tx)
144 {
145 	int ret;
146 	efi_status_t r = EFI_SUCCESS;
147 
148 	EFI_ENTRY("%p, %lx, %lx", this, extra_rx, extra_tx);
149 
150 	/* Check parameters */
151 	if (!this) {
152 		r = EFI_INVALID_PARAMETER;
153 		goto out;
154 	}
155 
156 	switch (this->mode->state) {
157 	case EFI_NETWORK_INITIALIZED:
158 	case EFI_NETWORK_STARTED:
159 		break;
160 	default:
161 		r = EFI_NOT_STARTED;
162 		goto out;
163 	}
164 
165 	/* Setup packet buffers */
166 	net_init();
167 	/* Disable hardware and put it into the reset state */
168 	eth_halt();
169 	/* Clear cache of packets */
170 	rx_packet_num = 0;
171 	/* Set current device according to environment variables */
172 	eth_set_current();
173 	/* Get hardware ready for send and receive operations */
174 	ret = eth_init();
175 	if (ret < 0) {
176 		eth_halt();
177 		this->mode->state = EFI_NETWORK_STOPPED;
178 		r = EFI_DEVICE_ERROR;
179 		goto out;
180 	} else {
181 		this->int_status = 0;
182 		wait_for_packet->is_signaled = false;
183 		this->mode->state = EFI_NETWORK_INITIALIZED;
184 	}
185 out:
186 	return EFI_EXIT(r);
187 }
188 
189 /*
190  * efi_net_reset() - reinitialize the network interface
191  *
192  * This function implements the Reset service of the
193  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
194  * (UEFI) specification for details.
195  *
196  * @this:			pointer to the protocol instance
197  * @extended_verification:	execute exhaustive verification
198  * Return:			status code
199  */
efi_net_reset(struct efi_simple_network * this,int extended_verification)200 static efi_status_t EFIAPI efi_net_reset(struct efi_simple_network *this,
201 					 int extended_verification)
202 {
203 	efi_status_t ret;
204 
205 	EFI_ENTRY("%p, %x", this, extended_verification);
206 
207 	/* Check parameters */
208 	if (!this) {
209 		ret = EFI_INVALID_PARAMETER;
210 		goto out;
211 	}
212 
213 	switch (this->mode->state) {
214 	case EFI_NETWORK_INITIALIZED:
215 		break;
216 	case EFI_NETWORK_STOPPED:
217 		ret = EFI_NOT_STARTED;
218 		goto out;
219 	default:
220 		ret = EFI_DEVICE_ERROR;
221 		goto out;
222 	}
223 
224 	this->mode->state = EFI_NETWORK_STARTED;
225 	ret = EFI_CALL(efi_net_initialize(this, 0, 0));
226 out:
227 	return EFI_EXIT(ret);
228 }
229 
230 /*
231  * efi_net_shutdown() - shut down the network interface
232  *
233  * This function implements the Shutdown service of the
234  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
235  * (UEFI) specification for details.
236  *
237  * @this:	pointer to the protocol instance
238  * Return:	status code
239  */
efi_net_shutdown(struct efi_simple_network * this)240 static efi_status_t EFIAPI efi_net_shutdown(struct efi_simple_network *this)
241 {
242 	efi_status_t ret = EFI_SUCCESS;
243 
244 	EFI_ENTRY("%p", this);
245 
246 	/* Check parameters */
247 	if (!this) {
248 		ret = EFI_INVALID_PARAMETER;
249 		goto out;
250 	}
251 
252 	switch (this->mode->state) {
253 	case EFI_NETWORK_INITIALIZED:
254 		break;
255 	case EFI_NETWORK_STOPPED:
256 		ret = EFI_NOT_STARTED;
257 		goto out;
258 	default:
259 		ret = EFI_DEVICE_ERROR;
260 		goto out;
261 	}
262 
263 	eth_halt();
264 	this->int_status = 0;
265 	wait_for_packet->is_signaled = false;
266 	this->mode->state = EFI_NETWORK_STARTED;
267 
268 out:
269 	return EFI_EXIT(ret);
270 }
271 
272 /*
273  * efi_net_receive_filters() - mange multicast receive filters
274  *
275  * This function implements the ReceiveFilters service of the
276  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
277  * (UEFI) specification for details.
278  *
279  * @this:		pointer to the protocol instance
280  * @enable:		bit mask of receive filters to enable
281  * @disable:		bit mask of receive filters to disable
282  * @reset_mcast_filter:	true resets contents of the filters
283  * @mcast_filter_count:	number of hardware MAC addresses in the new filters list
284  * @mcast_filter:	list of new filters
285  * Return:		status code
286  */
efi_net_receive_filters(struct efi_simple_network * this,u32 enable,u32 disable,int reset_mcast_filter,ulong mcast_filter_count,struct efi_mac_address * mcast_filter)287 static efi_status_t EFIAPI efi_net_receive_filters
288 		(struct efi_simple_network *this, u32 enable, u32 disable,
289 		 int reset_mcast_filter, ulong mcast_filter_count,
290 		 struct efi_mac_address *mcast_filter)
291 {
292 	EFI_ENTRY("%p, %x, %x, %x, %lx, %p", this, enable, disable,
293 		  reset_mcast_filter, mcast_filter_count, mcast_filter);
294 
295 	return EFI_EXIT(EFI_UNSUPPORTED);
296 }
297 
298 /*
299  * efi_net_station_address() - set the hardware MAC address
300  *
301  * This function implements the StationAddress service of the
302  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
303  * (UEFI) specification for details.
304  *
305  * @this:	pointer to the protocol instance
306  * @reset:	if true reset the address to default
307  * @new_mac:	new MAC address
308  * Return:	status code
309  */
efi_net_station_address(struct efi_simple_network * this,int reset,struct efi_mac_address * new_mac)310 static efi_status_t EFIAPI efi_net_station_address
311 		(struct efi_simple_network *this, int reset,
312 		 struct efi_mac_address *new_mac)
313 {
314 	EFI_ENTRY("%p, %x, %p", this, reset, new_mac);
315 
316 	return EFI_EXIT(EFI_UNSUPPORTED);
317 }
318 
319 /*
320  * efi_net_statistics() - reset or collect statistics of the network interface
321  *
322  * This function implements the Statistics service of the
323  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
324  * (UEFI) specification for details.
325  *
326  * @this:	pointer to the protocol instance
327  * @reset:	if true, the statistics are reset
328  * @stat_size:	size of the statistics table
329  * @stat_table:	table to receive the statistics
330  * Return:	status code
331  */
efi_net_statistics(struct efi_simple_network * this,int reset,ulong * stat_size,void * stat_table)332 static efi_status_t EFIAPI efi_net_statistics(struct efi_simple_network *this,
333 					      int reset, ulong *stat_size,
334 					      void *stat_table)
335 {
336 	EFI_ENTRY("%p, %x, %p, %p", this, reset, stat_size, stat_table);
337 
338 	return EFI_EXIT(EFI_UNSUPPORTED);
339 }
340 
341 /*
342  * efi_net_mcastiptomac() - translate multicast IP address to MAC address
343  *
344  * This function implements the MCastIPtoMAC service of the
345  * EFI_SIMPLE_NETWORK_PROTOCOL. See the Unified Extensible Firmware Interface
346  * (UEFI) specification for details.
347  *
348  * @this:	pointer to the protocol instance
349  * @ipv6:	true if the IP address is an IPv6 address
350  * @ip:		IP address
351  * @mac:	MAC address
352  * Return:	status code
353  */
efi_net_mcastiptomac(struct efi_simple_network * this,int ipv6,struct efi_ip_address * ip,struct efi_mac_address * mac)354 static efi_status_t EFIAPI efi_net_mcastiptomac(struct efi_simple_network *this,
355 						int ipv6,
356 						struct efi_ip_address *ip,
357 						struct efi_mac_address *mac)
358 {
359 	efi_status_t ret = EFI_SUCCESS;
360 
361 	EFI_ENTRY("%p, %x, %p, %p", this, ipv6, ip, mac);
362 
363 	if (!this || !ip || !mac) {
364 		ret = EFI_INVALID_PARAMETER;
365 		goto out;
366 	}
367 
368 	if (ipv6) {
369 		ret = EFI_UNSUPPORTED;
370 		goto out;
371 	}
372 
373 	/* Multi-cast addresses are in the range 224.0.0.0 - 239.255.255.255 */
374 	if ((ip->ip_addr[0] & 0xf0) != 0xe0) {
375 		ret = EFI_INVALID_PARAMETER;
376 		goto out;
377 	};
378 
379 	switch (this->mode->state) {
380 	case EFI_NETWORK_INITIALIZED:
381 	case EFI_NETWORK_STARTED:
382 		break;
383 	default:
384 		ret = EFI_NOT_STARTED;
385 		goto out;
386 	}
387 
388 	memset(mac, 0, sizeof(struct efi_mac_address));
389 
390 	/*
391 	 * Copy lower 23 bits of IPv4 multi-cast address
392 	 * RFC 1112, RFC 7042 2.1.1.
393 	 */
394 	mac->mac_addr[0] = 0x01;
395 	mac->mac_addr[1] = 0x00;
396 	mac->mac_addr[2] = 0x5E;
397 	mac->mac_addr[3] = ip->ip_addr[1] & 0x7F;
398 	mac->mac_addr[4] = ip->ip_addr[2];
399 	mac->mac_addr[5] = ip->ip_addr[3];
400 out:
401 	return EFI_EXIT(ret);
402 }
403 
404 /**
405  * efi_net_nvdata() - read or write NVRAM
406  *
407  * This function implements the GetStatus service of the Simple Network
408  * Protocol. See the UEFI spec for details.
409  *
410  * @this:		the instance of the Simple Network Protocol
411  * @read_write:		true for read, false for write
412  * @offset:		offset in NVRAM
413  * @buffer_size:	size of buffer
414  * @buffer:		buffer
415  * Return:		status code
416  */
efi_net_nvdata(struct efi_simple_network * this,int read_write,ulong offset,ulong buffer_size,char * buffer)417 static efi_status_t EFIAPI efi_net_nvdata(struct efi_simple_network *this,
418 					  int read_write, ulong offset,
419 					  ulong buffer_size, char *buffer)
420 {
421 	EFI_ENTRY("%p, %x, %lx, %lx, %p", this, read_write, offset, buffer_size,
422 		  buffer);
423 
424 	return EFI_EXIT(EFI_UNSUPPORTED);
425 }
426 
427 /**
428  * efi_net_get_status() - get interrupt status
429  *
430  * This function implements the GetStatus service of the Simple Network
431  * Protocol. See the UEFI spec for details.
432  *
433  * @this:		the instance of the Simple Network Protocol
434  * @int_status:		interface status
435  * @txbuf:		transmission buffer
436  */
efi_net_get_status(struct efi_simple_network * this,u32 * int_status,void ** txbuf)437 static efi_status_t EFIAPI efi_net_get_status(struct efi_simple_network *this,
438 					      u32 *int_status, void **txbuf)
439 {
440 	efi_status_t ret = EFI_SUCCESS;
441 
442 	EFI_ENTRY("%p, %p, %p", this, int_status, txbuf);
443 
444 	efi_timer_check();
445 
446 	/* Check parameters */
447 	if (!this) {
448 		ret = EFI_INVALID_PARAMETER;
449 		goto out;
450 	}
451 
452 	switch (this->mode->state) {
453 	case EFI_NETWORK_STOPPED:
454 		ret = EFI_NOT_STARTED;
455 		goto out;
456 	case EFI_NETWORK_STARTED:
457 		ret = EFI_DEVICE_ERROR;
458 		goto out;
459 	default:
460 		break;
461 	}
462 
463 	if (int_status) {
464 		*int_status = this->int_status;
465 		this->int_status = 0;
466 	}
467 	if (txbuf)
468 		*txbuf = new_tx_packet;
469 
470 	new_tx_packet = NULL;
471 out:
472 	return EFI_EXIT(ret);
473 }
474 
475 /**
476  * efi_net_transmit() - transmit a packet
477  *
478  * This function implements the Transmit service of the Simple Network Protocol.
479  * See the UEFI spec for details.
480  *
481  * @this:		the instance of the Simple Network Protocol
482  * @header_size:	size of the media header
483  * @buffer_size:	size of the buffer to receive the packet
484  * @buffer:		buffer to receive the packet
485  * @src_addr:		source hardware MAC address
486  * @dest_addr:		destination hardware MAC address
487  * @protocol:		type of header to build
488  * Return:		status code
489  */
efi_net_transmit(struct efi_simple_network * this,size_t header_size,size_t buffer_size,void * buffer,struct efi_mac_address * src_addr,struct efi_mac_address * dest_addr,u16 * protocol)490 static efi_status_t EFIAPI efi_net_transmit
491 		(struct efi_simple_network *this, size_t header_size,
492 		 size_t buffer_size, void *buffer,
493 		 struct efi_mac_address *src_addr,
494 		 struct efi_mac_address *dest_addr, u16 *protocol)
495 {
496 	efi_status_t ret = EFI_SUCCESS;
497 
498 	EFI_ENTRY("%p, %lu, %lu, %p, %p, %p, %p", this,
499 		  (unsigned long)header_size, (unsigned long)buffer_size,
500 		  buffer, src_addr, dest_addr, protocol);
501 
502 	efi_timer_check();
503 
504 	/* Check parameters */
505 	if (!this || !buffer) {
506 		ret = EFI_INVALID_PARAMETER;
507 		goto out;
508 	}
509 
510 	/* We do not support jumbo packets */
511 	if (buffer_size > PKTSIZE_ALIGN) {
512 		ret = EFI_INVALID_PARAMETER;
513 		goto out;
514 	}
515 
516 	/* At least the IP header has to fit into the buffer */
517 	if (buffer_size < this->mode->media_header_size) {
518 		ret = EFI_BUFFER_TOO_SMALL;
519 		goto out;
520 	}
521 
522 	/*
523 	 * TODO:
524 	 * Support VLANs. Use net_set_ether() for copying the header. Use a
525 	 * U_BOOT_ENV_CALLBACK to update the media header size.
526 	 */
527 	if (header_size) {
528 		struct ethernet_hdr *header = buffer;
529 
530 		if (!dest_addr || !protocol ||
531 		    header_size != this->mode->media_header_size) {
532 			ret = EFI_INVALID_PARAMETER;
533 			goto out;
534 		}
535 		if (!src_addr)
536 			src_addr = &this->mode->current_address;
537 
538 		memcpy(header->et_dest, dest_addr, ARP_HLEN);
539 		memcpy(header->et_src, src_addr, ARP_HLEN);
540 		header->et_protlen = htons(*protocol);
541 	}
542 
543 	switch (this->mode->state) {
544 	case EFI_NETWORK_STOPPED:
545 		ret = EFI_NOT_STARTED;
546 		goto out;
547 	case EFI_NETWORK_STARTED:
548 		ret = EFI_DEVICE_ERROR;
549 		goto out;
550 	default:
551 		break;
552 	}
553 
554 	/* Ethernet packets always fit, just bounce */
555 	memcpy(transmit_buffer, buffer, buffer_size);
556 	net_send_packet(transmit_buffer, buffer_size);
557 
558 	new_tx_packet = buffer;
559 	this->int_status |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
560 out:
561 	return EFI_EXIT(ret);
562 }
563 
564 /**
565  * efi_net_receive() - receive a packet from a network interface
566  *
567  * This function implements the Receive service of the Simple Network Protocol.
568  * See the UEFI spec for details.
569  *
570  * @this:		the instance of the Simple Network Protocol
571  * @header_size:	size of the media header
572  * @buffer_size:	size of the buffer to receive the packet
573  * @buffer:		buffer to receive the packet
574  * @src_addr:		source MAC address
575  * @dest_addr:		destination MAC address
576  * @protocol:		protocol
577  * Return:		status code
578  */
efi_net_receive(struct efi_simple_network * this,size_t * header_size,size_t * buffer_size,void * buffer,struct efi_mac_address * src_addr,struct efi_mac_address * dest_addr,u16 * protocol)579 static efi_status_t EFIAPI efi_net_receive
580 		(struct efi_simple_network *this, size_t *header_size,
581 		 size_t *buffer_size, void *buffer,
582 		 struct efi_mac_address *src_addr,
583 		 struct efi_mac_address *dest_addr, u16 *protocol)
584 {
585 	efi_status_t ret = EFI_SUCCESS;
586 	struct ethernet_hdr *eth_hdr;
587 	size_t hdr_size = sizeof(struct ethernet_hdr);
588 	u16 protlen;
589 
590 	EFI_ENTRY("%p, %p, %p, %p, %p, %p, %p", this, header_size,
591 		  buffer_size, buffer, src_addr, dest_addr, protocol);
592 
593 	/* Execute events */
594 	efi_timer_check();
595 
596 	/* Check parameters */
597 	if (!this || !buffer || !buffer_size) {
598 		ret = EFI_INVALID_PARAMETER;
599 		goto out;
600 	}
601 
602 	switch (this->mode->state) {
603 	case EFI_NETWORK_STOPPED:
604 		ret = EFI_NOT_STARTED;
605 		goto out;
606 	case EFI_NETWORK_STARTED:
607 		ret = EFI_DEVICE_ERROR;
608 		goto out;
609 	default:
610 		break;
611 	}
612 
613 	if (!rx_packet_num) {
614 		ret = EFI_NOT_READY;
615 		goto out;
616 	}
617 	/* Fill export parameters */
618 	eth_hdr = (struct ethernet_hdr *)receive_buffer[rx_packet_idx];
619 	protlen = ntohs(eth_hdr->et_protlen);
620 	if (protlen == 0x8100) {
621 		hdr_size += 4;
622 		protlen = ntohs(*(u16 *)&receive_buffer[rx_packet_idx][hdr_size - 2]);
623 	}
624 	if (header_size)
625 		*header_size = hdr_size;
626 	if (dest_addr)
627 		memcpy(dest_addr, eth_hdr->et_dest, ARP_HLEN);
628 	if (src_addr)
629 		memcpy(src_addr, eth_hdr->et_src, ARP_HLEN);
630 	if (protocol)
631 		*protocol = protlen;
632 	if (*buffer_size < receive_lengths[rx_packet_idx]) {
633 		/* Packet doesn't fit, try again with bigger buffer */
634 		*buffer_size = receive_lengths[rx_packet_idx];
635 		ret = EFI_BUFFER_TOO_SMALL;
636 		goto out;
637 	}
638 	/* Copy packet */
639 	memcpy(buffer, receive_buffer[rx_packet_idx],
640 	       receive_lengths[rx_packet_idx]);
641 	*buffer_size = receive_lengths[rx_packet_idx];
642 	rx_packet_idx = (rx_packet_idx + 1) % ETH_PACKETS_BATCH_RECV;
643 	rx_packet_num--;
644 	if (rx_packet_num)
645 		wait_for_packet->is_signaled = true;
646 	else
647 		this->int_status &= ~EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
648 out:
649 	return EFI_EXIT(ret);
650 }
651 
652 /**
653  * efi_net_set_dhcp_ack() - take note of a selected DHCP IP address
654  *
655  * This function is called by dhcp_handler().
656  *
657  * @pkt:	packet received by dhcp_handler()
658  * @len:	length of the packet received
659  */
efi_net_set_dhcp_ack(void * pkt,int len)660 void efi_net_set_dhcp_ack(void *pkt, int len)
661 {
662 	int maxsize = sizeof(*dhcp_ack);
663 
664 	if (!dhcp_ack) {
665 		dhcp_ack = malloc(maxsize);
666 		if (!dhcp_ack)
667 			return;
668 	}
669 	memset(dhcp_ack, 0, maxsize);
670 	memcpy(dhcp_ack, pkt, min(len, maxsize));
671 
672 	if (netobj)
673 		netobj->pxe_mode.dhcp_ack = *dhcp_ack;
674 }
675 
676 /**
677  * efi_net_push() - callback for received network packet
678  *
679  * This function is called when a network packet is received by eth_rx().
680  *
681  * @pkt:	network packet
682  * @len:	length
683  */
efi_net_push(void * pkt,int len)684 static void efi_net_push(void *pkt, int len)
685 {
686 	int rx_packet_next;
687 
688 	/* Check that we at least received an Ethernet header */
689 	if (len < sizeof(struct ethernet_hdr))
690 		return;
691 
692 	/* Check that the buffer won't overflow */
693 	if (len > PKTSIZE_ALIGN)
694 		return;
695 
696 	/* Can't store more than pre-alloced buffer */
697 	if (rx_packet_num >= ETH_PACKETS_BATCH_RECV)
698 		return;
699 
700 	rx_packet_next = (rx_packet_idx + rx_packet_num) %
701 	    ETH_PACKETS_BATCH_RECV;
702 	memcpy(receive_buffer[rx_packet_next], pkt, len);
703 	receive_lengths[rx_packet_next] = len;
704 
705 	rx_packet_num++;
706 }
707 
708 /**
709  * efi_network_timer_notify() - check if a new network packet has been received
710  *
711  * This notification function is called in every timer cycle.
712  *
713  * @event:	the event for which this notification function is registered
714  * @context:	event context - not used in this function
715  */
efi_network_timer_notify(struct efi_event * event,void * context)716 static void EFIAPI efi_network_timer_notify(struct efi_event *event,
717 					    void *context)
718 {
719 	struct efi_simple_network *this = (struct efi_simple_network *)context;
720 
721 	EFI_ENTRY("%p, %p", event, context);
722 
723 	/*
724 	 * Some network drivers do not support calling eth_rx() before
725 	 * initialization.
726 	 */
727 	if (!this || this->mode->state != EFI_NETWORK_INITIALIZED)
728 		goto out;
729 
730 	if (!rx_packet_num) {
731 		push_packet = efi_net_push;
732 		eth_rx();
733 		push_packet = NULL;
734 		if (rx_packet_num) {
735 			this->int_status |=
736 				EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
737 			wait_for_packet->is_signaled = true;
738 		}
739 	}
740 out:
741 	EFI_EXIT(EFI_SUCCESS);
742 }
743 
efi_pxe_base_code_start(struct efi_pxe_base_code_protocol * this,u8 use_ipv6)744 static efi_status_t EFIAPI efi_pxe_base_code_start(
745 				struct efi_pxe_base_code_protocol *this,
746 				u8 use_ipv6)
747 {
748 	return EFI_UNSUPPORTED;
749 }
750 
efi_pxe_base_code_stop(struct efi_pxe_base_code_protocol * this)751 static efi_status_t EFIAPI efi_pxe_base_code_stop(
752 				struct efi_pxe_base_code_protocol *this)
753 {
754 	return EFI_UNSUPPORTED;
755 }
756 
efi_pxe_base_code_dhcp(struct efi_pxe_base_code_protocol * this,u8 sort_offers)757 static efi_status_t EFIAPI efi_pxe_base_code_dhcp(
758 				struct efi_pxe_base_code_protocol *this,
759 				u8 sort_offers)
760 {
761 	return EFI_UNSUPPORTED;
762 }
763 
efi_pxe_base_code_discover(struct efi_pxe_base_code_protocol * this,u16 type,u16 * layer,u8 bis,struct efi_pxe_base_code_discover_info * info)764 static efi_status_t EFIAPI efi_pxe_base_code_discover(
765 				struct efi_pxe_base_code_protocol *this,
766 				u16 type, u16 *layer, u8 bis,
767 				struct efi_pxe_base_code_discover_info *info)
768 {
769 	return EFI_UNSUPPORTED;
770 }
771 
efi_pxe_base_code_mtftp(struct efi_pxe_base_code_protocol * this,u32 operation,void * buffer_ptr,u8 overwrite,efi_uintn_t * buffer_size,struct efi_ip_address server_ip,char * filename,struct efi_pxe_base_code_mtftp_info * info,u8 dont_use_buffer)772 static efi_status_t EFIAPI efi_pxe_base_code_mtftp(
773 				struct efi_pxe_base_code_protocol *this,
774 				u32 operation, void *buffer_ptr,
775 				u8 overwrite, efi_uintn_t *buffer_size,
776 				struct efi_ip_address server_ip, char *filename,
777 				struct efi_pxe_base_code_mtftp_info *info,
778 				u8 dont_use_buffer)
779 {
780 	return EFI_UNSUPPORTED;
781 }
782 
efi_pxe_base_code_udp_write(struct efi_pxe_base_code_protocol * this,u16 op_flags,struct efi_ip_address * dest_ip,u16 * dest_port,struct efi_ip_address * gateway_ip,struct efi_ip_address * src_ip,u16 * src_port,efi_uintn_t * header_size,void * header_ptr,efi_uintn_t * buffer_size,void * buffer_ptr)783 static efi_status_t EFIAPI efi_pxe_base_code_udp_write(
784 				struct efi_pxe_base_code_protocol *this,
785 				u16 op_flags, struct efi_ip_address *dest_ip,
786 				u16 *dest_port,
787 				struct efi_ip_address *gateway_ip,
788 				struct efi_ip_address *src_ip, u16 *src_port,
789 				efi_uintn_t *header_size, void *header_ptr,
790 				efi_uintn_t *buffer_size, void *buffer_ptr)
791 {
792 	return EFI_UNSUPPORTED;
793 }
794 
efi_pxe_base_code_udp_read(struct efi_pxe_base_code_protocol * this,u16 op_flags,struct efi_ip_address * dest_ip,u16 * dest_port,struct efi_ip_address * src_ip,u16 * src_port,efi_uintn_t * header_size,void * header_ptr,efi_uintn_t * buffer_size,void * buffer_ptr)795 static efi_status_t EFIAPI efi_pxe_base_code_udp_read(
796 				struct efi_pxe_base_code_protocol *this,
797 				u16 op_flags, struct efi_ip_address *dest_ip,
798 				u16 *dest_port, struct efi_ip_address *src_ip,
799 				u16 *src_port, efi_uintn_t *header_size,
800 				void *header_ptr, efi_uintn_t *buffer_size,
801 				void *buffer_ptr)
802 {
803 	return EFI_UNSUPPORTED;
804 }
805 
efi_pxe_base_code_set_ip_filter(struct efi_pxe_base_code_protocol * this,struct efi_pxe_base_code_filter * new_filter)806 static efi_status_t EFIAPI efi_pxe_base_code_set_ip_filter(
807 				struct efi_pxe_base_code_protocol *this,
808 				struct efi_pxe_base_code_filter *new_filter)
809 {
810 	return EFI_UNSUPPORTED;
811 }
812 
efi_pxe_base_code_arp(struct efi_pxe_base_code_protocol * this,struct efi_ip_address * ip_addr,struct efi_mac_address * mac_addr)813 static efi_status_t EFIAPI efi_pxe_base_code_arp(
814 				struct efi_pxe_base_code_protocol *this,
815 				struct efi_ip_address *ip_addr,
816 				struct efi_mac_address *mac_addr)
817 {
818 	return EFI_UNSUPPORTED;
819 }
820 
efi_pxe_base_code_set_parameters(struct efi_pxe_base_code_protocol * this,u8 * new_auto_arp,u8 * new_send_guid,u8 * new_ttl,u8 * new_tos,u8 * new_make_callback)821 static efi_status_t EFIAPI efi_pxe_base_code_set_parameters(
822 				struct efi_pxe_base_code_protocol *this,
823 				u8 *new_auto_arp, u8 *new_send_guid,
824 				u8 *new_ttl, u8 *new_tos,
825 				u8 *new_make_callback)
826 {
827 	return EFI_UNSUPPORTED;
828 }
829 
efi_pxe_base_code_set_station_ip(struct efi_pxe_base_code_protocol * this,struct efi_ip_address * new_station_ip,struct efi_ip_address * new_subnet_mask)830 static efi_status_t EFIAPI efi_pxe_base_code_set_station_ip(
831 				struct efi_pxe_base_code_protocol *this,
832 				struct efi_ip_address *new_station_ip,
833 				struct efi_ip_address *new_subnet_mask)
834 {
835 	return EFI_UNSUPPORTED;
836 }
837 
efi_pxe_base_code_set_packets(struct efi_pxe_base_code_protocol * this,u8 * new_dhcp_discover_valid,u8 * new_dhcp_ack_received,u8 * new_proxy_offer_received,u8 * new_pxe_discover_valid,u8 * new_pxe_reply_received,u8 * new_pxe_bis_reply_received,EFI_PXE_BASE_CODE_PACKET * new_dchp_discover,EFI_PXE_BASE_CODE_PACKET * new_dhcp_acc,EFI_PXE_BASE_CODE_PACKET * new_proxy_offer,EFI_PXE_BASE_CODE_PACKET * new_pxe_discover,EFI_PXE_BASE_CODE_PACKET * new_pxe_reply,EFI_PXE_BASE_CODE_PACKET * new_pxe_bis_reply)838 static efi_status_t EFIAPI efi_pxe_base_code_set_packets(
839 				struct efi_pxe_base_code_protocol *this,
840 				u8 *new_dhcp_discover_valid,
841 				u8 *new_dhcp_ack_received,
842 				u8 *new_proxy_offer_received,
843 				u8 *new_pxe_discover_valid,
844 				u8 *new_pxe_reply_received,
845 				u8 *new_pxe_bis_reply_received,
846 				EFI_PXE_BASE_CODE_PACKET *new_dchp_discover,
847 				EFI_PXE_BASE_CODE_PACKET *new_dhcp_acc,
848 				EFI_PXE_BASE_CODE_PACKET *new_proxy_offer,
849 				EFI_PXE_BASE_CODE_PACKET *new_pxe_discover,
850 				EFI_PXE_BASE_CODE_PACKET *new_pxe_reply,
851 				EFI_PXE_BASE_CODE_PACKET *new_pxe_bis_reply)
852 {
853 	return EFI_UNSUPPORTED;
854 }
855 
856 /**
857  * efi_net_register() - register the simple network protocol
858  *
859  * This gets called from do_bootefi_exec().
860  */
efi_net_register(void)861 efi_status_t efi_net_register(void)
862 {
863 	efi_status_t r;
864 	int i;
865 
866 	if (!eth_get_dev()) {
867 		/* No network device active, don't expose any */
868 		return EFI_SUCCESS;
869 	}
870 
871 	/* We only expose the "active" network device, so one is enough */
872 	netobj = calloc(1, sizeof(*netobj));
873 	if (!netobj)
874 		goto out_of_resources;
875 
876 	/* Allocate an aligned transmit buffer */
877 	transmit_buffer = calloc(1, PKTSIZE_ALIGN + PKTALIGN);
878 	if (!transmit_buffer)
879 		goto out_of_resources;
880 	transmit_buffer = (void *)ALIGN((uintptr_t)transmit_buffer, PKTALIGN);
881 
882 	/* Allocate a number of receive buffers */
883 	receive_buffer = calloc(ETH_PACKETS_BATCH_RECV,
884 				sizeof(*receive_buffer));
885 	if (!receive_buffer)
886 		goto out_of_resources;
887 	for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
888 		receive_buffer[i] = malloc(PKTSIZE_ALIGN);
889 		if (!receive_buffer[i])
890 			goto out_of_resources;
891 	}
892 	receive_lengths = calloc(ETH_PACKETS_BATCH_RECV,
893 				 sizeof(*receive_lengths));
894 	if (!receive_lengths)
895 		goto out_of_resources;
896 
897 	/* Hook net up to the device list */
898 	efi_add_handle(&netobj->header);
899 
900 	/* Fill in object data */
901 	r = efi_add_protocol(&netobj->header, &efi_net_guid,
902 			     &netobj->net);
903 	if (r != EFI_SUCCESS)
904 		goto failure_to_add_protocol;
905 	r = efi_add_protocol(&netobj->header, &efi_guid_device_path,
906 			     efi_dp_from_eth());
907 	if (r != EFI_SUCCESS)
908 		goto failure_to_add_protocol;
909 	r = efi_add_protocol(&netobj->header, &efi_pxe_base_code_protocol_guid,
910 			     &netobj->pxe);
911 	if (r != EFI_SUCCESS)
912 		goto failure_to_add_protocol;
913 	netobj->net.revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION;
914 	netobj->net.start = efi_net_start;
915 	netobj->net.stop = efi_net_stop;
916 	netobj->net.initialize = efi_net_initialize;
917 	netobj->net.reset = efi_net_reset;
918 	netobj->net.shutdown = efi_net_shutdown;
919 	netobj->net.receive_filters = efi_net_receive_filters;
920 	netobj->net.station_address = efi_net_station_address;
921 	netobj->net.statistics = efi_net_statistics;
922 	netobj->net.mcastiptomac = efi_net_mcastiptomac;
923 	netobj->net.nvdata = efi_net_nvdata;
924 	netobj->net.get_status = efi_net_get_status;
925 	netobj->net.transmit = efi_net_transmit;
926 	netobj->net.receive = efi_net_receive;
927 	netobj->net.mode = &netobj->net_mode;
928 	netobj->net_mode.state = EFI_NETWORK_STOPPED;
929 	memcpy(netobj->net_mode.current_address.mac_addr, eth_get_ethaddr(), 6);
930 	netobj->net_mode.hwaddr_size = ARP_HLEN;
931 	netobj->net_mode.media_header_size = ETHER_HDR_SIZE;
932 	netobj->net_mode.max_packet_size = PKTSIZE;
933 	netobj->net_mode.if_type = ARP_ETHER;
934 
935 	netobj->pxe.revision = EFI_PXE_BASE_CODE_PROTOCOL_REVISION;
936 	netobj->pxe.start = efi_pxe_base_code_start;
937 	netobj->pxe.stop = efi_pxe_base_code_stop;
938 	netobj->pxe.dhcp = efi_pxe_base_code_dhcp;
939 	netobj->pxe.discover = efi_pxe_base_code_discover;
940 	netobj->pxe.mtftp = efi_pxe_base_code_mtftp;
941 	netobj->pxe.udp_write = efi_pxe_base_code_udp_write;
942 	netobj->pxe.udp_read = efi_pxe_base_code_udp_read;
943 	netobj->pxe.set_ip_filter = efi_pxe_base_code_set_ip_filter;
944 	netobj->pxe.arp = efi_pxe_base_code_arp;
945 	netobj->pxe.set_parameters = efi_pxe_base_code_set_parameters;
946 	netobj->pxe.set_station_ip = efi_pxe_base_code_set_station_ip;
947 	netobj->pxe.set_packets = efi_pxe_base_code_set_packets;
948 	netobj->pxe.mode = &netobj->pxe_mode;
949 	if (dhcp_ack)
950 		netobj->pxe_mode.dhcp_ack = *dhcp_ack;
951 
952 	/*
953 	 * Create WaitForPacket event.
954 	 */
955 	r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
956 			     efi_network_timer_notify, NULL, NULL,
957 			     &wait_for_packet);
958 	if (r != EFI_SUCCESS) {
959 		printf("ERROR: Failed to register network event\n");
960 		return r;
961 	}
962 	netobj->net.wait_for_packet = wait_for_packet;
963 	/*
964 	 * Create a timer event.
965 	 *
966 	 * The notification function is used to check if a new network packet
967 	 * has been received.
968 	 *
969 	 * iPXE is running at TPL_CALLBACK most of the time. Use a higher TPL.
970 	 */
971 	r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
972 			     efi_network_timer_notify, &netobj->net, NULL,
973 			     &network_timer_event);
974 	if (r != EFI_SUCCESS) {
975 		printf("ERROR: Failed to register network event\n");
976 		return r;
977 	}
978 	/* Network is time critical, create event in every timer cycle */
979 	r = efi_set_timer(network_timer_event, EFI_TIMER_PERIODIC, 0);
980 	if (r != EFI_SUCCESS) {
981 		printf("ERROR: Failed to set network timer\n");
982 		return r;
983 	}
984 
985 	return EFI_SUCCESS;
986 failure_to_add_protocol:
987 	printf("ERROR: Failure to add protocol\n");
988 	return r;
989 out_of_resources:
990 	free(netobj);
991 	netobj = NULL;
992 	free(transmit_buffer);
993 	if (receive_buffer)
994 		for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++)
995 			free(receive_buffer[i]);
996 	free(receive_buffer);
997 	free(receive_lengths);
998 	printf("ERROR: Out of memory\n");
999 	return EFI_OUT_OF_RESOURCES;
1000 }
1001