/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2010,2011 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include /* ARP header operation codes */ enum { ARP_REQUEST = 1, ARP_REPLY = 2 }; enum { /* IANA ARP constant to define hardware type as ethernet. */ VAS_EBOOT_NET_ARPHRD_ETHERNET = 1 }; struct arppkt { VasEBoot_uint16_t hrd; VasEBoot_uint16_t pro; VasEBoot_uint8_t hln; VasEBoot_uint8_t pln; VasEBoot_uint16_t op; VasEBoot_uint8_t sender_mac[6]; VasEBoot_uint32_t sender_ip; VasEBoot_uint8_t recv_mac[6]; VasEBoot_uint32_t recv_ip; } VAS_EBOOT_PACKED; static int have_pending; static VasEBoot_uint32_t pending_req; VasEBoot_err_t VasEBoot_net_arp_send_request (struct VasEBoot_net_network_level_interface *inf, const VasEBoot_net_network_level_address_t *proto_addr) { struct VasEBoot_net_buff nb; struct arppkt *arp_packet; VasEBoot_net_link_level_address_t target_mac_addr; VasEBoot_err_t err; int i; VasEBoot_uint8_t *nbd; VasEBoot_uint8_t arp_data[128]; if (proto_addr->type != VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4) return VasEBoot_error (VAS_EBOOT_ERR_BUG, "unsupported address family"); /* Build a request packet. */ nb.head = arp_data; nb.end = arp_data + sizeof (arp_data); VasEBoot_netbuff_clear (&nb); VasEBoot_netbuff_reserve (&nb, 128); err = VasEBoot_netbuff_push (&nb, sizeof (*arp_packet)); if (err) return err; arp_packet = (struct arppkt *) nb.data; arp_packet->hrd = VasEBoot_cpu_to_be16_compile_time (VAS_EBOOT_NET_ARPHRD_ETHERNET); arp_packet->hln = 6; arp_packet->pro = VasEBoot_cpu_to_be16_compile_time (VAS_EBOOT_NET_ETHERTYPE_IP); arp_packet->pln = 4; arp_packet->op = VasEBoot_cpu_to_be16_compile_time (ARP_REQUEST); /* Sender hardware address. */ VasEBoot_memcpy (arp_packet->sender_mac, &inf->hwaddress.mac, 6); arp_packet->sender_ip = inf->address.ipv4; VasEBoot_memset (arp_packet->recv_mac, 0, 6); arp_packet->recv_ip = proto_addr->ipv4; /* Target protocol address */ VasEBoot_memset (&target_mac_addr.mac, 0xff, 6); nbd = nb.data; send_ethernet_packet (inf, &nb, target_mac_addr, VAS_EBOOT_NET_ETHERTYPE_ARP); for (i = 0; i < VAS_EBOOT_NET_TRIES; i++) { if (VasEBoot_net_link_layer_resolve_check (inf, proto_addr)) return VAS_EBOOT_ERR_NONE; pending_req = proto_addr->ipv4; have_pending = 0; VasEBoot_net_poll_cards (VAS_EBOOT_NET_INTERVAL + (i * VAS_EBOOT_NET_INTERVAL_ADDITION), &have_pending); if (VasEBoot_net_link_layer_resolve_check (inf, proto_addr)) return VAS_EBOOT_ERR_NONE; nb.data = nbd; send_ethernet_packet (inf, &nb, target_mac_addr, VAS_EBOOT_NET_ETHERTYPE_ARP); } return VAS_EBOOT_ERR_NONE; } VasEBoot_err_t VasEBoot_net_arp_receive (struct VasEBoot_net_buff *nb, struct VasEBoot_net_card *card, VasEBoot_uint16_t *vlantag) { struct arppkt *arp_packet = (struct arppkt *) nb->data; VasEBoot_net_network_level_address_t sender_addr, target_addr; VasEBoot_net_link_level_address_t sender_mac_addr; struct VasEBoot_net_network_level_interface *inf; if (arp_packet->pro != VasEBoot_cpu_to_be16_compile_time (VAS_EBOOT_NET_ETHERTYPE_IP) || arp_packet->pln != 4 || arp_packet->hln != 6 || nb->tail - nb->data < (int) sizeof (*arp_packet)) return VAS_EBOOT_ERR_NONE; sender_addr.type = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4; target_addr.type = VAS_EBOOT_NET_NETWORK_LEVEL_PROTOCOL_IPV4; sender_addr.ipv4 = arp_packet->sender_ip; target_addr.ipv4 = arp_packet->recv_ip; sender_addr.option = 0; target_addr.option = 0; if (arp_packet->sender_ip == pending_req) have_pending = 1; sender_mac_addr.type = VAS_EBOOT_NET_LINK_LEVEL_PROTOCOL_ETHERNET; VasEBoot_memcpy (sender_mac_addr.mac, arp_packet->sender_mac, sizeof (sender_mac_addr.mac)); VasEBoot_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1); FOR_NET_NETWORK_LEVEL_INTERFACES (inf) { /* Verify vlantag id */ if (inf->card == card && inf->vlantag != *vlantag) { VasEBoot_dprintf ("net", "invalid vlantag! %x != %x\n", inf->vlantag, *vlantag); break; } /* Am I the protocol address target? */ if (VasEBoot_net_addr_cmp (&inf->address, &target_addr) == 0 && arp_packet->op == VasEBoot_cpu_to_be16_compile_time (ARP_REQUEST)) { VasEBoot_net_link_level_address_t target; struct VasEBoot_net_buff nb_reply; struct arppkt *arp_reply; VasEBoot_uint8_t arp_data[128]; VasEBoot_err_t err; nb_reply.head = arp_data; nb_reply.end = arp_data + sizeof (arp_data); VasEBoot_netbuff_clear (&nb_reply); VasEBoot_netbuff_reserve (&nb_reply, 128); err = VasEBoot_netbuff_push (&nb_reply, sizeof (*arp_packet)); if (err) return err; arp_reply = (struct arppkt *) nb_reply.data; arp_reply->hrd = VasEBoot_cpu_to_be16_compile_time (VAS_EBOOT_NET_ARPHRD_ETHERNET); arp_reply->pro = VasEBoot_cpu_to_be16_compile_time (VAS_EBOOT_NET_ETHERTYPE_IP); arp_reply->pln = 4; arp_reply->hln = 6; arp_reply->op = VasEBoot_cpu_to_be16_compile_time (ARP_REPLY); arp_reply->sender_ip = arp_packet->recv_ip; arp_reply->recv_ip = arp_packet->sender_ip; arp_reply->hln = 6; target.type = VAS_EBOOT_NET_LINK_LEVEL_PROTOCOL_ETHERNET; VasEBoot_memcpy (target.mac, arp_packet->sender_mac, 6); VasEBoot_memcpy (arp_reply->sender_mac, inf->hwaddress.mac, 6); VasEBoot_memcpy (arp_reply->recv_mac, arp_packet->sender_mac, 6); /* Change operation to REPLY and send packet */ send_ethernet_packet (inf, &nb_reply, target, VAS_EBOOT_NET_ETHERTYPE_ARP); } } return VAS_EBOOT_ERR_NONE; }