Commit Graph

44 Commits

Author SHA1 Message Date
Javier Martinez Canillas a6838bbc67 tftp: Roll-over block counter to prevent data packets timeouts
Commit 781b3e5efc (tftp: Do not use priority queue) caused a regression
when fetching files over TFTP whose size is bigger than 65535 * block size.

  grub> linux /images/pxeboot/vmlinuz
  grub> echo $?
  0
  grub> initrd /images/pxeboot/initrd.img
  error: timeout reading '/images/pxeboot/initrd.img'.
  grub> echo $?
  28

It is caused by the block number counter being a 16-bit field, which leads
to a maximum file size of ((1 << 16) - 1) * block size. Because GRUB sets
the block size to 1024 octets (by using the TFTP Blocksize Option from RFC
2348 [0]), the maximum file size that can be transferred is 67107840 bytes.

The TFTP PROTOCOL (REVISION 2) RFC 1350 [1] does not mention what a client
should do when a file size is bigger than the maximum, but most TFTP hosts
support the block number counter to be rolled over. That is, acking a data
packet with a block number of 0 is taken as if the 65356th block was acked.

It was working before because the block counter roll-over was happening due
an overflow. But that got fixed by the mentioned commit, which led to the
regression when attempting to fetch files larger than the maximum size.

To allow TFTP file transfers of unlimited size again, re-introduce a block
counter roll-over so the data packets are acked preventing the timeouts.

[0]: https://tools.ietf.org/html/rfc2348
[1]: https://tools.ietf.org/html/rfc1350

Fixes: 781b3e5efc (tftp: Do not use priority queue)

Suggested-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-09-11 15:52:07 +02:00
Alexey Makhalov 781b3e5efc tftp: Do not use priority queue
There is not need to reassemble the order of blocks. Per RFC 1350,
server must wait for the ACK, before sending next block. Data packets
can be served immediately without putting them to priority queue.

Logic to handle incoming packet is this:
  - if packet block id equal to expected block id, then
    process the packet,
  - if packet block id is less than expected - this is retransmit
    of old packet, then ACK it and drop the packet,
  - if packet block id is more than expected - that shouldn't
    happen, just drop the packet.

It makes the tftp receive path code simpler, smaller and faster.
As a benefit, this change fixes CID# 73624 and CID# 96690, caused
by following while loop:

  while (cmp_block (grub_be_to_cpu16 (tftph->u.data.block), data->block + 1) == 0)

where tftph pointer is not moving from one iteration to another, causing
to serve same packet again. Luckily, double serving didn't happen due to
data->block++ during the first iteration.

Fixes: CID 73624, CID 96690

Signed-off-by: Alexey Makhalov <amakhalov@vmware.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-07-29 16:55:48 +02:00
Lenny Szubowicz e2c09aed97 tftp: Normalize slashes in TFTP paths
Some TFTP servers do not handle multiple consecutive slashes correctly.
This patch avoids sending TFTP requests with non-normalized paths.

Signed-off-by: Lenny Szubowicz <lszubowi@redhat.com>
Signed-off-by: Mark Salter <msalter@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2019-12-06 20:26:36 +01:00
Andrei Borzenkov 77002c65d3 tftp: fix memory leaks in open
If protocol open fails, file is immediately freed, so data was leaked.

Found by: Coverity scan.
CID: 96659
2016-01-16 21:27:57 +03:00
Andrey Borzenkov 954fe77163 cleanup: grub_cpu_to_XXX_compile_time for constants
This tries to catch all cases where grub_cpu_to_XXX was used for constant
expressions (including sizeof).
2014-09-22 20:47:10 +04:00
Paulo Flabiano Smorigo 6f65e36cc4 increase network try interval gradually
* grub-core/net/arp.c (grub_net_arp_send_request): Increase network try
interval gradually.
* grub-core/net/icmp6.c (grub_net_icmp6_send_request): Likewise.
* grub-core/net/net.c (grub_net_fs_read_real): Likewise.
* grub-core/net/tftp.c (tftp_open): Likewise.
* include/grub/net.h (GRUB_NET_INTERVAL_ADDITION): New define.
2014-01-21 11:06:35 -02:00
Vladimir Serbinenko 7e47e27bd8 Add gcc_struct to all packed structures when compiling with mingw.
Just "packed" doesn't always pack the way we expect.
2013-12-15 14:14:30 +01:00
Vladimir Serbinenko cf8d6bbd9e * grub-core/net/tftp.c: Retransmit ack when rereceiving old packet.
Try to handle more than 0xFFFF packets.
2013-10-27 19:15:37 +01:00
Avik Sil 369508b3cb * grub-core/net/tftp.c: Send tftp ack packet before closing the socket. 2013-08-14 20:32:42 -03:00
Vladimir 'phcoder' Serbinenko a706f4cc6b * grub-core/net/tftp.c (ack): Fix endianness problem.
(tftp_receive): Likewise.
	Reported by: Michael Davidsaver.
2012-07-02 11:22:50 +02:00
Vladimir 'phcoder' Serbinenko bd407d6e5e * grub-core/net/tftp.c: Decrease stall to 50 packets. 2012-06-22 22:04:16 +02:00
Vladimir 'phcoder' Serbinenko b27069e06d Implement flow control for tftp.
* grub-core/net/net.c (receive_packets): Decrease the stop to 10
	packets but stop only if stop condition is satisfied.
	(grub_net_fs_read_real): Call packets_pulled after real read. Use
	`stall' instead of `eof' as stop condition.
	* grub-core/net/http.c (parse_line): Set `stall' on EOF.
	(http_err): Likewise.
	* grub-core/net/tftp.c (ack): Replace the first argument with data
	instead of socket.
	(tftp_receive): Stall if too many packets are in wait queue.
	(tftp_packets_pulled): New function.
	(grub_tftp_protocol): Set packets_pulled.
	* include/grub/net.h (grub_net_packets): New field count.
	(grub_net_put_packet): Increment count.
	(grub_net_remove_packet): Likewise.
	(grub_net_app_protocol): New field `packets_pulled'.
	(grub_net): New field `stall'.
2012-06-22 14:17:46 +02:00
Vladimir 'phcoder' Serbinenko 96f7e60eae Stop polling as soon as we have the packet we were waiting for.
* include/grub/net.h (grub_net_poll_cards): New argument stop_condition.
	All users updated.
	* grub-core/net/arp.c (have_pending): New var.
	(pending_req): Likewise.
	(grub_net_arp_send_request): Fill pending_req and use have_pending as
	stop indicator.
	(grub_net_arp_receive): Set have_pending.
	* grub-core/net/dns.c (recv_data): New field stop.
	(recv_hook): Set stop.
	(grub_net_dns_lookup): Init stop and use as stop condition.
	* grub-core/net/http.c (http_establish): Use headers_recv as stop
	condition.
	* grub-core/net/net.c (grub_net_poll_cards): New argument
	stop_condition. Stop when it goes true.
	* grub-core/net/tcp.c (grub_net_tcp_open): Use `established' as stop
	indicator.
	* grub-core/net/tftp.c (tftp_open): Use `have_oack' as stop indicator.
2012-06-09 11:06:55 +02:00
Bean 5efb817d64 * grub-core/net/ip.c (reassemble): Make asm_buffer into asm_netbuff.
All users updated.
	(free_rsm): Free header as well.
	(free_old_fragments): Fix memory leak.
	* grub-core/net/netbuff.c (grub_netbuff_free): Make return void.
	* grub-core/net/tftp.c (tftp_receive): Fix memory leak.
	(destroy_pq): Likewise.
	* include/grub/net/netbuff.h (grub_netbuff_free): Make return void.
2012-05-08 16:03:02 +02:00
Vladimir 'phcoder' Serbinenko 0cf69874ee * grub-core/net/tftp.c (tftp_receive): Silently discard too short
packets rather than raising an error.
2012-02-12 19:11:06 +01:00
Vladimir 'phcoder' Serbinenko 9c4b5c13e6 Improve gettext support. Stylistic fixes and error handling fixes while
on it.
2012-02-08 19:26:01 +01:00
Vladimir 'phcoder' Serbinenko aca002f7e4 * grub-core/kern/err.c (GRUB_MAX_ERRMSG): Move to ...
* include/grub/err.h (GRUB_MAX_ERRMSG): ... here.
	* include/grub/err.h (grub_error_saved): New struct.
	(grub_errmsg): Make array size explicit.
	* include/grub/misc.h (grub_error_save): New function.
	(grub_error_load): Likewise.
	* grub-core/kern/err.c (grub_error_stack_items): Use grub_error_saved.
	(grub_error_push): Update `errno' member name.
	(grub_error_pop): Likewise
	* grub-core/net/tftp.c (tftp_data): New member save_err.
	(tftp_receive): Save error.
	(tftp_open): Restore error.
2012-02-05 10:24:53 +01:00
Vladimir 'phcoder' Serbinenko 75b49ebed9 Remove defines pertaining to arbitrary limits not affecting GRUB
anymore.

	* grub-core/fs/ext2.c (EXT2_PATH_MAX): Removed.
	(EXT2_MAX_SYMLINKCNT): Likewise.
	* grub-core/fs/nilfs2.c (NILFS_BTREE_LEVEL_MAX): Likewise.
	* grub-core/net/tftp.c (TFTP_MAX_PACKET): Likewise.
	* include/grub/i386/pc/pxe.h (GRUB_PXE_MIN_BLKSIZE): Likewise.
	(GRUB_PXE_MAX_BLKSIZE): Likewise.
	* include/grub/normal.h (GRUB_MAX_CMDLINE): Likewise.
	* include/grub/zfs/dnode.h (DN_MAX_INDBLKSHIFT): Likewise.
	(DN_MAX_OBJECT_SHIFT): Likewise.
	(DN_MAX_OFFSET_SHIFT): Likewise.
	(DN_MAX_OBJECT): Likewise.
	(DNODES_PER_LEVEL_SHIFT): Likewise.
	* include/grub/zfs/spa.h (SPA_MAXBLOCKSHIFT): Likewise.
	(SPA_MAXBLOCKSIZE): Likewise.
	(SPA_BLOCKSIZES): Likewise.
	* include/grub/zfs/zap_impl.h (MZAP_MAX_BLKSHIFT): Likewise.
	(MZAP_MAX_BLKSZ): Likewise.
2012-01-14 11:30:43 +01:00
Vladimir 'phcoder' Serbinenko 198e150aaf IPv6, TCP, HTTP, ICMP and DNS support. Several cleanups and bugfixes.
* grub-core/Makefile.core.def (net): Add net/dns.c, net/tcp.c,
	net/icmp.c and net/icmp6.c.
	(http): New module.
	(priority_queue): Likewise.
	* grub-core/io/bufio.c: Rewritten.
	* grub-core/lib/legacy_parse.c (legacy_command): New argument type
	TYPE_WITH_CONFIGFILE_OPTION.
	(legacy_commands): Add bootp and dhcp.
	(is_option): Handle TYPE_WITH_CONFIGFILE_OPTION.
	(grub_legacy_parse): Likewise.
	* grub-core/lib/priority_queue.c: New file.
	* grub-core/net/arp.c: Add missing license header.
	(arp_find_entry): Removed.
	(arp_find_entry): Likewise.
	(grub_net_arp_resolve): Rename to ...
	(grub_net_arp_send_request): ...this.
	(grub_net_arp_receive): New card argument.
	* grub-core/net/bootp.c (parse_dhcp_vendor): Clean up.
	Set router and DNS server.
	(grub_net_configure_by_dhcp_ack): Handle routing information.
	(grub_cmd_bootp): Set checksum.
	(grub_bootp_init): Remove net_dhcp.
	* grub-core/net/dns.c: New file.
	* grub-core/net/drivers/efi/efinet.c (send_card_buffer): Wait for
	completion.
	(get_card_packet): Handle allocation.
	(grub_efinet_findcards): Set mtu.
	* grub-core/net/drivers/emu/emunet.c: Add missing license header.
	(get_card_packet): Handle allocation.
	(emucard): Set mtu.
	* grub-core/net/drivers/i386/pc/pxe.c (grub_pxe_recv): Handle allocation
	(GRUB_MOD_INIT): Set mtu.
	* grub-core/net/drivers/ieee1275/ofnet.c (grub_ofnetcard_data): Remove
	mtu.
	(get_card_packet): Handle allocation.
	(grub_ofnet_findcards): Set mtu.
	* grub-core/net/ethernet.c (send_ethernet_packet): Add compile time
	assert.
	(grub_net_recv_ethernet_packet): Handle IPv6.
	* grub-core/net/http.c: New file.
	* grub-core/net/icmp.c: Likewise.
	* grub-core/net/icmp6.c: Likewise.
	* grub-core/net/ip.c (ip6addr): New type.
	(ip6hdr): Likewise.
	(reassemble): Likewise.
	(cmp): New function.
	(reassembles): New variable.
	(grub_net_ip_chksum): Handle 0xffff sum and unaligned buffers.
	(id): New variable.
	(send_fragmented): New function.
	(grub_net_send_ip_packet): Rename to ...
	(grub_net_send_ip4_packet): ... this. Send fragmented if needed.
	Handle non-UDP.
	(grub_net_recv_ip_packets): Rename to ...
	(handle_dgram): ... this. Check checksum. Handle non-UDP.
	(free_rsm): New function.
	(free_old_fragments): Likewise.
	(grub_net_recv_ip4_packets): New function.
	(grub_net_send_ip6_packet): Likewise.
	(grub_net_send_ip_packet): Likewise.
	(grub_net_recv_ip6_packets): Likewise.
	(grub_net_recv_ip_packets): Likewise.
	* grub-core/net/net.c (grub_net_link_layer_entry): New struct.
	(LINK_LAYER_CACHE_SIZE): New const.
	(link_layer_find_entry): New function.
	(grub_net_link_layer_add_address): Likewise.
	(grub_net_link_layer_resolve_check): Likewise.
	(grub_net_link_layer_resolve): Likewise.
	(grub_net_ipv6_get_slaac): Likewise.
	(grub_net_ipv6_get_link_local): Likewise.
	(grub_cmd_ipv6_autoconf): Likewise.
	(parse_ip): Handle one number representation.
	(parse_ip6): New functoion.
	(match_net): Handle IPv6.
	(grub_net_resolve_address): Handle IPv6 and DNS.
	(grub_net_resolve_net_address): Handle IPv6.
	(route_cmp): New function.
	(grub_net_route_address): Find best route.
	(grub_net_addr_to_str): Handle IPv6.
	(grub_net_addr_cmp): New function.
	(grub_net_add_addr): Register local route.
	(print_net_address): Handle net address.
	(grub_net_poll_cards): Retransmit TCP.
	(grub_net_poll_cards_idle_real): Likewise.
	(have_ahead): New function.
	(grub_net_seek_real): Use underlying seek.
	(GRUB_MOD_INIT): Register net_ipv6_autoconf and init dns.
	* grub-core/net/tcp.c: New file.
	* grub-core/net/tftp.c (tftp_data): Add priority_queue.
	(cmp): New function.
	(ack): Likewise.
	(tftp_receive): Handle unordered input.
	(destroy_pq): New function.
	(tftp_close): Close pq.
	* grub-core/net/udp.c: Put missing license header.
	(grub_net_udp_socket): New function.
	(udp_socket_register): Likewise.
	(grub_net_udp_close): Likewise.
	(grub_net_recv_udp_packet): Check checksum.
	* include/grub/efi/api.h (grub_efi_simple_network): Add status.
	* include/grub/misc.h (grub_memchr): New function.
	* include/grub/net.h (GRUB_NET_*_SIZE): New enum.
	(grub_net_card_driver): Return buf in recv.
	(grub_net_slaac_mac_list): New struct.
	(grub_network_level_protocol_id): Add ipv6.
	(grub_net_network_level_addr): Likewise.
	(grub_net_network_level_net_addr): Likewise.
	(grub_net_app_protocol): Add seek.
	(grub_net_socket): Removed.
	(grub_net_sockets): Likewise.
	(grub_net_socket_register): Likewise.
	(grub_net_socket_unregister): Likewise.
	(FOR_NET_SOCKETS): Likewise.
	(grub_net_add_addr): Add const.
	(GRUB_NET_BOOTP_*): New enum.
	(grub_net_addr_cmp): New proto.
	(GRUB_NET_MAX_STR_ADDR_LEN): Take IPV6 into account.
	(GRUB_NET_MAX_STR_HWADDR_LEN): New define.
	(grub_net_hwaddr_to_str): NEw proto.
	(FOR_NET_NETWORK_LEVEL_INTERFACES): New macro.
	(FOR_NET_NETWORK_LEVEL_INTERFACES_SAFE): Handle NULL.
	(grub_dns_init): New proto.
	(grub_dns_fini): Likewise.
	(grub_net_tcp_retransmit): Likewise.
	(grub_net_link_layer_add_address): Likewise.
	(grub_net_link_layer_resolve_check): Likewise.
	(grub_net_link_layer_resolve): Likewise.
	(grub_net_dns_lookup): Likewise.
	(grub_net_add_dns_server): Likewise.
	(grub_net_remove_dns_server): Likewise.
	(GRUB_NET_TRIES): New const.
	(GRUB_NET_INTERVAL): Likewise.
	* include/grub/net/arp.h: Mostly rewritten.
	* include/grub/net/ethernet.h (grub_net_ethertype_t): New enum.
	* include/grub/net/ip.h: Mostly rewritten.
	* include/grub/net/netbuff.h: Indent.
	* include/grub/net/tcp.h: New file.
	* include/grub/net/udp.h: Mostly rewritten.
	* include/grub/priority_queue.h: New file.
	* include/grub/types.h (PRIdGRUB_SSIZE): New define.
	(grub_swap_bytes64_compile_time): Likewise.
	(grub_cpu_to_be16_compile_time): Likewise.
	(grub_cpu_to_be32_compile_time): Likewise.
	(grub_cpu_to_be64_compile_time): Likewise.
	(grub_be_to_cpu64_compile_time): Likewise.
2011-12-20 18:17:07 +01:00
Vladimir 'phcoder' Serbinenko 038ec56a31 nslookup implementation 2011-10-14 19:21:59 +02:00
Vladimir 'phcoder' Serbinenko 9aad3cd91d IPv6 support. Several fixes and unifications 2011-10-05 22:15:30 +02:00
Vladimir 'phcoder' Serbinenko 4ebb4c616f Remove useless include in tftp 2011-07-23 03:47:10 +02:00
Vladimir 'phcoder' Serbinenko 3a7af37260 basic tcp implementation 2011-07-10 08:46:48 +02:00
Vladimir 'phcoder' Serbinenko 92bb3cfc17 Use priority queue to reassemble TFTP packets 2011-07-09 01:28:47 +02:00
Vladimir 'phcoder' Serbinenko fecdbd6b17 support ip fragmentation 2011-07-09 00:27:27 +02:00
Vladimir 'phcoder' Serbinenko 1367c143dd several cleanups. Ping reply support 2011-07-08 14:41:52 +02:00
Vladimir 'phcoder' Serbinenko b975df6348 Send TFTP_ERROR on tftp premature close. Several cleanups 2011-07-06 12:53:37 +02:00
Vladimir 'phcoder' Serbinenko 7e0c2d162a Restructurisations, cleanups and few bugfixes 2011-07-02 22:13:33 +02:00
Vladimir 'phcoder' Serbinenko cbe20661f6 minor stylistic cleanup 2011-07-02 18:05:40 +02:00
Vladimir 'phcoder' Serbinenko 6708faafde Fix broken blksize negotiation, fix broken seek and change a way net device is filled n i386-pc 2011-07-02 17:58:23 +02:00
Vladimir 'phcoder' Serbinenko a8fae12c5b minor cleanups 2011-06-26 17:18:53 +02:00
Vladimir 'phcoder' Serbinenko a057797f18 merge mine and abranches' branches. Fix several issues 2011-06-24 21:51:57 +02:00
Vladimir 'phcoder' Serbinenko 8b51fd98b9 bootp support 2011-06-24 20:35:25 +02:00
Manoel R. Abranches d855fbcf37 Add error verification in netbuff operations. 2011-06-18 20:20:53 -03:00
Manoel Rebelo Abranches 4700d08bb4 Run indent on files. 2011-06-07 21:59:53 -03:00
Manoel Rebelo Abranches 423a1849ef Write ChangeLog. 2011-06-07 11:47:31 -03:00
Vladimir 'phcoder' Serbinenko eea841440d fix several bugs 2011-05-19 15:39:34 +02:00
Manoel R. Abranches 48ac061ab6 Prevent "incompatible license" error. 2011-05-12 15:40:54 -03:00
Manoel Rebelo Abranches 31b5172bbd Implement close in TFTP 2011-05-10 14:07:54 -03:00
Manoel Rebelo Abranches 0696f08f92 Prevente error in broken cards by limiting data size. 2011-05-10 14:07:20 -03:00
Manoel Rebelo Abranches 25f1579b43 Adapt protocols to new network struct. 2011-04-01 05:42:34 -03:00
Vladimir 'phcoder' Serbinenko 04d22dddd9 Fix a bunch of net issues 2010-09-23 00:45:39 +02:00
Vladimir 'phcoder' Serbinenko ce3a2ec025 Remove some dead code 2010-09-22 20:34:20 +02:00
Manoel R. Abranches 7bb47706c9 Merge grub_net into net. Compiles but is broken right now. 2010-09-16 21:57:31 +02:00