2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2010,2011 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/net.h>
2N/A#include <grub/net/ip.h>
2N/A#include <grub/net/netbuff.h>
2N/A
2N/Astruct icmp_header
2N/A{
2N/A grub_uint8_t type;
2N/A grub_uint8_t code;
2N/A grub_uint16_t checksum;
2N/A} __attribute__ ((packed));
2N/A
2N/Astruct ping_header
2N/A{
2N/A grub_uint16_t id;
2N/A grub_uint16_t seq;
2N/A} __attribute__ ((packed));
2N/A
2N/Aenum
2N/A {
2N/A ICMP_ECHO_REPLY = 0,
2N/A ICMP_ECHO = 8,
2N/A };
2N/A
2N/Agrub_err_t
2N/Agrub_net_recv_icmp_packet (struct grub_net_buff *nb,
2N/A struct grub_net_network_level_interface *inf,
2N/A const grub_net_link_level_address_t *ll_src,
2N/A const grub_net_network_level_address_t *src)
2N/A{
2N/A struct icmp_header *icmph;
2N/A grub_err_t err;
2N/A grub_uint16_t checksum;
2N/A
2N/A /* Ignore broadcast. */
2N/A if (!inf)
2N/A {
2N/A grub_netbuff_free (nb);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A
2N/A icmph = (struct icmp_header *) nb->data;
2N/A
2N/A if (nb->tail - nb->data < (grub_ssize_t) sizeof (*icmph))
2N/A {
2N/A grub_netbuff_free (nb);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A
2N/A checksum = icmph->checksum;
2N/A icmph->checksum = 0;
2N/A if (checksum != grub_net_ip_chksum (nb->data, nb->tail - nb->data))
2N/A {
2N/A icmph->checksum = checksum;
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A icmph->checksum = checksum;
2N/A
2N/A err = grub_netbuff_pull (nb, sizeof (*icmph));
2N/A if (err)
2N/A return err;
2N/A
2N/A switch (icmph->type)
2N/A {
2N/A case ICMP_ECHO:
2N/A {
2N/A struct grub_net_buff *nb_reply;
2N/A struct icmp_header *icmphr;
2N/A if (icmph->code)
2N/A break;
2N/A nb_reply = grub_netbuff_alloc (nb->tail - nb->data + 512);
2N/A if (!nb_reply)
2N/A {
2N/A grub_netbuff_free (nb);
2N/A return grub_errno;
2N/A }
2N/A err = grub_netbuff_reserve (nb_reply, nb->tail - nb->data + 512);
2N/A if (err)
2N/A goto ping_fail;
2N/A err = grub_netbuff_push (nb_reply, nb->tail - nb->data);
2N/A if (err)
2N/A goto ping_fail;
2N/A grub_memcpy (nb_reply->data, nb->data, nb->tail - nb->data);
2N/A err = grub_netbuff_push (nb_reply, sizeof (*icmphr));
2N/A if (err)
2N/A goto ping_fail;
2N/A icmphr = (struct icmp_header *) nb_reply->data;
2N/A icmphr->type = ICMP_ECHO_REPLY;
2N/A icmphr->code = 0;
2N/A icmphr->checksum = 0;
2N/A icmphr->checksum = grub_net_ip_chksum ((void *) nb_reply->data,
2N/A nb_reply->tail - nb_reply->data);
2N/A err = grub_net_send_ip_packet (inf, src, ll_src,
2N/A nb_reply, GRUB_NET_IP_ICMP);
2N/A
2N/A ping_fail:
2N/A grub_netbuff_free (nb);
2N/A grub_netbuff_free (nb_reply);
2N/A return err;
2N/A }
2N/A };
2N/A
2N/A grub_netbuff_free (nb);
2N/A return GRUB_ERR_NONE;
2N/A}