3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 #define UDP_FILTER_MASK (EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | \
24 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT | \
25 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP | \
26 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT | \
27 EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER \
30 #define PXE_BOOT_LAYER_MASK 0x7FFF
31 #define PXE_BOOT_LAYER_INITIAL 0x0000
32 #define PXE_BOOT_LAYER_CREDENTIAL_FLAG 0x8000
33 #define MAX_BOOT_SERVERS 32
36 // macro to evaluate IP address as TRUE if it is a multicast IP address
38 #define IS_MULTICAST(ptr) ((*((UINT8 *) ptr) & 0xf0) == 0xe0)
43 #define IP_ADDRESS_LENGTH(qp) (((qp)->UsingIpv6) ? sizeof (EFI_IPv6_ADDRESS) : sizeof (EFI_IPv4_ADDRESS))
45 #define MAX_FRAME_DATA_SIZE 1488
46 #define ALLOCATE_SIZE(X) (((X) + 7) & 0xfff8)
47 #define MODE_ALLOCATE_SIZE ALLOCATE_SIZE (sizeof (EFI_PXE_BASE_CODE_MODE))
48 #define BUFFER_ALLOCATE_SIZE (8192 + 512)
49 #define ROUTER_ALLOCATE_SIZE ALLOCATE_SIZE ((sizeof (EFI_PXE_BASE_CODE_ROUTE_ENTRY) * PXE_ROUTER_TABLE_SIZE))
50 #define ARP_ALLOCATE_SIZE ALLOCATE_SIZE ((sizeof (EFI_PXE_BASE_CODE_ARP_ENTRY) * PXE_ARP_CACHE_SIZE))
51 #define FILTER_ALLOCATE_SIZE ALLOCATE_SIZE ((sizeof (EFI_IP_ADDRESS) * PXE_IP_FILTER_SIZE))
52 #define PXE_ARP_CACHE_SIZE 8
53 #define PXE_ROUTER_TABLE_SIZE 8
54 #define PXE_IP_FILTER_SIZE 8
55 #define ICMP_ALLOCATE_SIZE ALLOCATE_SIZE (sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR))
56 #define TFTP_ERR_ALLOCATE_SIZE ALLOCATE_SIZE (sizeof (EFI_PXE_BASE_CODE_TFTP_ERROR))
59 // DHCP discover/request packets are sent to this UDP port. ProxyDHCP
60 // servers listen on this port for DHCP discover packets that have a
61 // class identifier (option 60) with 'PXEClient' in the first 9 bytes.
62 // Bootservers also listen on this port for PXE broadcast discover
63 // requests from PXE clients.
65 #define DHCP_SERVER_PORT 67
68 // When DHCP, proxyDHCP and Bootservers respond to DHCP and PXE broadcast
69 // discover requests by broadcasting the reply packet, the packet is
70 // broadcast to this port.
72 #define DHCP_CLIENT_PORT 68
75 // TFTP servers listen for TFTP open requests on this port.
77 #define TFTP_OPEN_PORT 69
80 // proxyDHCP and Bootservers listen on this port for a PXE unicast and/or
81 // multicast discover requests from PXE clients. A PXE discover request
82 // looks like a DHCP discover or DHCP request packet.
84 #define PXE_DISCOVERY_PORT 4011
87 // This port is used by the PXE client/server protocol tests.
89 #define PXE_PORT_PXETEST_PORT 0x8080
92 // Definitions for Ethertype protocol numbers and interface types
95 #define PXE_PROTOCOL_ETHERNET_IP 0x0800
96 #define PXE_PROTOCOL_ETHERNET_ARP 0x0806
97 #define PXE_PROTOCOL_ETHERNET_RARP 0x8035
99 #define PXE_IFTYPE_ETHERNET 0x01
100 #define PXE_IFTYPE_TOKENRING 0x04
101 #define PXE_IFTYPE_FIBRE_CHANNEL 0x12
104 // Definitions for internet protocol version 4 header
105 // Per RFC 791, September 1981.
109 #pragma pack(1) // make network structures packed byte alignment
115 #define IPV4_HEADER_LENGTH(IpHeaderPtr) (((IpHeaderPtr)->VersionIhl & 0xf) << 2)
117 #define SET_IPV4_VER_HDL(IpHeaderPtr, IpHeaderLen) { \
118 (IpHeaderPtr)->VersionIhl = (UINT8) ((IPVER4 << 4) | ((IpHeaderLen) >> 2)); \
126 UINT16 FragmentFields
;
129 UINT16 HeaderChecksum
;
133 // options are not implemented
137 #define IP_FRAG_RSVD 0x8000 // reserved bit - must be zero
138 #define IP_NO_FRAG 0x4000 // do not fragment bit
139 #define IP_MORE_FRAG 0x2000 // not last fragment
140 #define IP_FRAG_OFF_MSK 0x1fff // fragment offset in 8 byte chunks
141 #define DEFAULT_RFC_TTL 64
149 * Definitions for internet control message protocol version 4 message
150 * structure. Per RFC 792, September 1981.
154 // icmp header for all icmp messages
157 UINT8 Type
; // message type
158 UINT8 Code
; // type specific - 0 for types we implement
159 UINT16 Checksum
; // ones complement of ones complement sum of 16 bit words of message
162 #define ICMP_DEST_UNREACHABLE 3
163 #define ICMP_SOURCE_QUENCH 4
164 #define ICMP_REDIRECT 5
166 #define ICMP_ECHO_REPLY 0
167 #define ICMP_ROUTER_ADV 9
168 #define ICMP_ROUTER_SOLICIT 10
169 #define ICMP_TIME_EXCEEDED 11
170 #define ICMP_PARAMETER_PROBLEM 12
171 #define ICMP_TIMESTAMP 13
172 #define ICMP_TIMESTAMP_REPLY 14
173 #define ICMP_INFO_REQ 15
174 #define ICMP_INFO_REQ_REPLY 16
175 #define ICMP_SUBNET_MASK_REQ 17
176 #define ICMP_SUBNET_MASK_REPLY 18
178 // other ICMP message types ignored in this implementation
180 // icmp general messages
183 ICMPV4_HEADER Header
;
185 // generally unused except byte [0] for
186 // parameter problem message
188 UINT8 GenerallyUnused
[4];
190 // original message ip header of plus 64
193 IPV4_HEADER IpHeader
;
194 } ICMPV4_GENERAL_MESSAGE
;
197 // icmp req/rply message header
200 ICMPV4_HEADER Header
;
202 UINT16 SequenceNumber
;
203 } ICMPV4_REQUEST_REPLY_HEADER
;
209 ICMPV4_REQUEST_REPLY_HEADER Header
;
210 UINT8 EchoData
[1]; // variable length data to be echoed
211 } ICMPV4_ECHO_MESSAGE
;
214 // icmp timestamp message - times are milliseconds since midnight UT -
215 // if non std, set high order bit
218 ICMPV4_REQUEST_REPLY_HEADER Header
;
219 UINT32 OriginalTime
; // originating timestamp
220 UINT32 ReceiveTime
; // receiving timestamp
221 UINT32 TransmitTime
; // transmitting timestamp
222 } ICMPV4_TIMESTAMP_MESSAGE
;
225 // icmp info request structure - fill in source and dest net ip address on reply
228 ICMPV4_REQUEST_REPLY_HEADER Header
;
229 } ICMPV4_INFO_MESSAGE
;
232 // Definitions for internet control message protocol version 4 message structure
234 // Per RFC 1256, September 1991.
237 // icmp router advertisement message
240 ICMPV4_HEADER Header
;
241 UINT8 NumberEntries
; // number of address entries
242 UINT8 EntrySize
; // number of 32 bit words per address entry
243 UINT16 Lifetime
; // seconds to consider info valid
246 } ICMPV4_ROUTER_ADVERTISE_MESSAGE
;
249 // icmp router solicitation message
252 ICMPV4_HEADER Header
;
254 } ICMPV4_ROUTER_SOLICIT_MESSAGE
;
256 #define MAX_SOLICITATION_DELAY 1 // 1 second
257 #define SOLICITATION_INTERVAL 3 // 3 seconds
258 #define MAX_SOLICITATIONS 3 // 3 transmissions
259 #define V1ROUTER_PRESENT_TIMEOUT 400 // 400 second timeout until v2 reports can be sent
260 #define UNSOLICITED_REPORT_INTERVAL 10 // 10 seconds between unsolicited reports
261 #define BROADCAST_IPv4 0xffffffff
264 // Definitions for address resolution protocol message structure
265 // Per RFC 826, November 1982
268 UINT16 HwType
; // hardware type - e.g. ethernet (1)
269 UINT16 ProtType
; // protocol type - for ethernet, 0x800 for IP
270 UINT8 HwAddLen
; // byte length of a hardware address (e.g. 6 for ethernet)
271 UINT8 ProtAddLen
; // byte length of a protocol address (e.g. 4 for ipv4)
274 // source and dest hw and prot addresses follow - see example below
278 #define ETHERNET_ADD_SPC 1
280 #define ETHER_TYPE_IP 0x800
282 #define ARP_REQUEST 1
286 // generic ARP packet
289 ARP_HEADER ArpHeader
;
290 EFI_MAC_ADDRESS SrcHardwareAddr
;
291 EFI_IP_ADDRESS SrcProtocolAddr
;
292 EFI_MAC_ADDRESS DestHardwareAddr
;
293 EFI_IP_ADDRESS DestProtocolAddr
;
296 #define ENET_HWADDLEN 6
297 #define IPV4_PROTADDLEN 4
300 // Definitions for user datagram protocol version 4 pseudo header & header
301 // Per RFC 768, 28 August 1980
304 IPV4_ADDR SrcAddr
; // source ip address
305 IPV4_ADDR DestAddr
; // dest ip address
307 UINT8 Protocol
; // protocol
308 UINT16 TotalLength
; // UDP length - sizeof udpv4hdr + data length
309 } UDPV4_PSEUDO_HEADER
;
312 UINT16 SrcPort
; // source port identifier
313 UINT16 DestPort
; // destination port identifier
314 UINT16 TotalLength
; // total length header plus data
316 // ones complement of ones complement sum of 16 bit
317 // words of pseudo header, UDP header, and data
318 // zero checksum is transmitted as -0 (ones comp)
319 // zero transmitted means checksum not computed
326 UDPV4_PSEUDO_HEADER Udpv4PseudoHeader
;
327 UDPV4_HEADER Udpv4Header
;
331 // Definitions for transmission control protocol header
332 // Per RFC 793, September, 1981
335 IPV4_ADDR SrcAddr
; // source ip address
336 IPV4_ADDR DestAddr
; // dest ip address
338 UINT8 Protocol
; // protocol
339 UINT16 TotalLength
; // TCP length - TCP header length + data length
340 } TCPV4_PSEUDO_HEADER
;
343 UINT16 SrcPort
; // source port identifier
344 UINT16 DestPort
; // destination port identifier
345 UINT32 SeqNumber
; // Sequence number
346 UINT32 AckNumber
; // Acknowledgement Number
348 // Nibble of HLEN (length of header in 32-bit multiples)
350 // Nibble of Code Bits
353 UINT16 Window
; // Software buffer size (sliding window size) in network-standard byte order
355 // ones complement of ones complement sum of 16 bit words of
356 // pseudo header, TCP header, and data
357 // zero checksum is transmitted as -0 (ones comp)
358 // zero transmitted means checksum not computed
361 UINT16 UrgentPointer
; // pointer to urgent data (allows sender to specify urgent data)
365 TCPV4_PSEUDO_HEADER Tcpv4PseudoHeader
;
366 TCPV4_HEADER Tcpv4Header
;
370 UINT8 Kind
; // one of the following:
371 UINT8 Length
; // total option length including Kind and Lth
372 UINT8 Data
[1]; // length = Lth - 2
375 #define TCP_OP_END 0 // only used to pad to end of TCP header
376 #define TCP_NOP 1 // optional - may be used to pad between options to get alignment
377 #define TCP_MAX_SEG 2 // maximum receive segment size - only send at initial connection request
378 #define MAX_MEDIA_HDR_SIZE 64
379 #define MIN_ENET_DATA_SIZE 64
380 #define MAX_ENET_DATA_SIZE 1500 // temp def - make a network based var
381 #define MAX_IPV4_PKT_SIZE 65535 // maximum IP packet size
382 #define MAX_IPV4_DATA_SIZE (MAX_IPV4_PKT_SIZE - sizeof (IPV4_HEADER))
383 #define MAX_IPV4_FRAME_DATA_SIZE (MAX_FRAME_DATA_SIZE - sizeof (IPV4_HEADER))
384 #define REAS_IPV4_PKT_SIZE 576 // minimum IP packet size all IP host can handle
385 #define REAS_IPV4_DATA_SIZE (REAS_IPV4_PKT_SIZE - sizeof (IPV4_HEADER))
391 UINT8 Data
[MAX_ENET_DATA_SIZE
];
392 ICMPV4_HEADER IcmpHeader
;
393 IGMPV2_MESSAGE IgmpMessage
;
395 UDPV4_HEADER UdpHeader
;
399 TCPV4_HEADER TcpHeader
;
405 // out buffer structure
408 UINT8 MediaHeader
[MAX_MEDIA_HDR_SIZE
];
409 IPV4_HEADER IpHeader
;
411 // following union placement only valid if no option IP header
417 IPV4_HEADER IpHeader
;
419 // following union placement only valid if no option IP header
424 #pragma pack() // reset to default
426 ////////////////////////////////////////////////////////////
428 // BC IP Filter Routine
432 PXE_BASECODE_DEVICE
*Private
,
433 IN EFI_PXE_BASE_CODE_IP_FILTER
*Filter
438 // //////////////////////////////////////////////////////////////////////
440 // Udp Write Routine - called by base code - e.g. TFTP - already locked
444 IN PXE_BASECODE_DEVICE
*Private
,
446 IN EFI_IP_ADDRESS
*DestIpPtr
,
447 IN EFI_PXE_BASE_CODE_UDP_PORT
*DestPortptr
,
448 IN EFI_IP_ADDRESS
*GatewayIpPtr
, OPTIONAL
449 IN EFI_IP_ADDRESS
*SrcIpPtr
, OPTIONAL
450 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPortPtr
, OPTIONAL
451 IN UINTN
*HeaderSizePtr
, OPTIONAL
452 IN VOID
*HeaderPtr
, OPTIONAL
453 IN UINTN
*BufferSizePtr
,
459 // /////////////////////////////////////////////////////////////////////
461 // Udp Read Routine - called by base code - e.g. TFTP - already locked
465 IN PXE_BASECODE_DEVICE
*Private
,
467 IN OUT EFI_IP_ADDRESS
*DestIpPtr
, OPTIONAL
468 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPorPtrt
, OPTIONAL
469 IN OUT EFI_IP_ADDRESS
*SrcIpPtr
, OPTIONAL
470 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPortPtr
, OPTIONAL
471 IN UINTN
*HeaderSizePtr
, OPTIONAL
472 IN VOID
*HeaderPtr
, OPTIONAL
473 IN OUT UINTN
*BufferSizePtr
,
475 IN EFI_EVENT TimeoutEvent
481 PXE_BASECODE_DEVICE
*Private
,
488 PXE_BASECODE_DEVICE
*Private
,
494 // convert number to zero filled ascii value of length lth
505 // convert number to ascii string at ptr
515 // convert ascii numeric string to UINTN
530 // calculate the internet checksum (RFC 1071)
531 // return 16 bit ones complement of ones complement sum of 16 bit words
541 // do checksum on non contiguous header and data
553 // update checksum when only a single word changes
565 IN PXE_BASECODE_DEVICE
*Private
,
566 IN UINT16 InitialSeed
572 IN PXE_BASECODE_DEVICE
*Private
578 PXE_BASECODE_DEVICE
*Private
,
582 VOID
*HardwareAddress
,
583 UINT16 MediaProtocol
,
584 IN EFI_PXE_BASE_CODE_FUNCTION Function
590 PXE_BASECODE_DEVICE
*Private
,
591 ARP_PACKET
*ArpPacketPtr
,
598 PXE_BASECODE_DEVICE
*Private
,
599 IGMPV2_MESSAGE
*IgmpMessageptr
,
606 PXE_BASECODE_DEVICE
*Private
608 ; // poll when doing a receive
609 // return hw add of IP and TRUE if available, otherwise FALSE
613 IN PXE_BASECODE_DEVICE
*Private
,
614 EFI_IP_ADDRESS
*ProtocolAddressPtr
,
615 EFI_MAC_ADDRESS
*HardwareAddressPtr
621 IN PXE_BASECODE_DEVICE
*Private
,
622 IN EFI_IP_ADDRESS
*ProtocolAddressPtr
,
623 OUT EFI_MAC_ADDRESS
*HardwareAddressptr
632 EFI_IP_ADDRESS
*SubnetMask
638 PXE_BASECODE_DEVICE
*Private
,
639 EFI_IP_ADDRESS
*RouterIp
643 #define Ip4AddRouter(Private, Ipv4Ptr) IpAddRouter (Private, (EFI_IP_ADDRESS *) Ipv4Ptr)
646 // routine to send ipv4 packet
647 // ipv4 + upper protocol header for length TotHdrLth in xmtbuf, ipv4 header length IpHdrLth
648 // routine fills in ipv4hdr Ver_Hdl, TotLth, and Checksum, moves in Data, and gets dest MAC address
652 PXE_BASECODE_DEVICE
*Private
,
655 UINTN TotalHeaderLen
,
658 EFI_PXE_BASE_CODE_FUNCTION Function
663 // send ipv4 packet with ipv4 option
667 PXE_BASECODE_DEVICE
*Private
,
675 EFI_PXE_BASE_CODE_FUNCTION Function
680 // send MsgLth message at MsgPtr - higher level protocol header already in xmtbuf, length HdrSize
684 IN PXE_BASECODE_DEVICE
*Private
, // pointer to instance data
685 IN UINTN MayFragment
, //
686 IN UINT8 Protocol
, // protocol
687 IN UINT32 SrcIp
, // Source IP address
688 IN UINT32 DestIp
, // Destination IP address
689 IN UINT32 GatewayIp
, // used if not NULL and needed
690 IN UINTN HeaderSize
, // protocol header byte length
691 IN UINT8
*MsgPtr
, // pointer to data
694 ; // data byte length
695 // receive up to MsgLth message into MsgPtr for protocol Prot
696 // return message length, src/dest ips if select any, and pointer to protocol header
700 IN PXE_BASECODE_DEVICE
*Private
, // pointer to instance data
701 UINT16 OpFlags
, // Flags to determine if filtering on IP addresses
702 EFI_IP_ADDRESS
*SrcIpPtr
, // if filtering, O if accept any
703 EFI_IP_ADDRESS
*DstIpPtr
, // if filtering, O if accept any
704 UINT8 Protocol
, // protocol
705 VOID
*HeaderPtr
, // address of where to put protocol header
706 UINTN HeaderSize
, // protocol header byte length
707 UINT8
*MsgPtr
, // pointer to data buffer
708 UINTN
*MsgLenPtr
, // pointer to data buffer length/ O - returned data length
709 IN EFI_EVENT TimeoutEvent
716 IN PXE_BASECODE_DEVICE
*Private
721 // routine to cycle waiting for a receive or timeout
725 IN PXE_BASECODE_DEVICE
*Private
,
726 IN EFI_PXE_BASE_CODE_FUNCTION Function
,
727 IN EFI_EVENT TimeoutEvent
,
728 IN OUT UINTN
*HeaderSizePtr
,
729 IN OUT UINTN
*BufferSizePtr
,
730 IN OUT UINT16
*ProtocolPtr