Changeset View
Changeset View
Standalone View
Standalone View
sys/net/panicnet_inet.c
- This file was added.
/*- | |||||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | |||||
* | |||||
* Copyright (c) 2019 Isilon Systems, LLC. | |||||
* Copyright (c) 2005-2014 Sandvine Incorporated. All rights reserved. | |||||
* Copyright (c) 2000 Darrell Anderson | |||||
* All rights reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include "opt_inet.h" | |||||
#include <sys/param.h> | |||||
#include <sys/systm.h> | |||||
#include <sys/errno.h> | |||||
#include <sys/socket.h> | |||||
#include <sys/sysctl.h> | |||||
#include <net/ethernet.h> | |||||
#include <net/if.h> | |||||
#include <net/if_arp.h> | |||||
#include <net/if_dl.h> | |||||
#include <net/if_types.h> | |||||
#include <net/if_var.h> | |||||
#include <netinet/in.h> | |||||
#include <netinet/in_systm.h> | |||||
#include <netinet/in_var.h> | |||||
#include <netinet/ip.h> | |||||
#include <netinet/ip_var.h> | |||||
#include <netinet/ip_options.h> | |||||
#include <netinet/udp.h> | |||||
#include <netinet/udp_var.h> | |||||
#include <machine/in_cksum.h> | |||||
#include <machine/pcb.h> | |||||
#include <net/panicnet.h> | |||||
#define PANICNET_INTERNAL | |||||
#include <net/panicnet_int.h> | |||||
int panicnet_arp_nretries = 3; | |||||
SYSCTL_INT(_net_panicnet, OID_AUTO, arp_nretries, CTLFLAG_RWTUN, | |||||
&panicnet_arp_nretries, 0, | |||||
"Number of ARP attempts before giving up"); | |||||
/* | |||||
* Builds and sends a single ARP request to locate the L2 address for a given | |||||
* INET address. | |||||
* | |||||
* Return value: | |||||
* 0 on success | |||||
* errno on error | |||||
*/ | |||||
static int | |||||
panicnet_send_arp(struct panicnet_pcb *pcb, in_addr_t dst) | |||||
{ | |||||
struct ether_addr bcast; | |||||
struct arphdr *ah; | |||||
struct ifnet *ifp; | |||||
struct mbuf *m; | |||||
int pktlen; | |||||
ifp = pcb->pp_ifp; | |||||
/* Fill-up a broadcast address. */ | |||||
memset(&bcast, 0xFF, ETHER_ADDR_LEN); | |||||
m = m_gethdr(M_NOWAIT, MT_DATA); | |||||
if (m == NULL) { | |||||
printf("%s: Out of mbufs\n", __func__); | |||||
return (ENOBUFS); | |||||
} | |||||
pktlen = arphdr_len2(ETHER_ADDR_LEN, sizeof(struct in_addr)); | |||||
m->m_len = pktlen; | |||||
m->m_pkthdr.len = pktlen; | |||||
MH_ALIGN(m, pktlen); | |||||
ah = mtod(m, struct arphdr *); | |||||
ah->ar_hrd = htons(ARPHRD_ETHER); | |||||
ah->ar_pro = htons(ETHERTYPE_IP); | |||||
ah->ar_hln = ETHER_ADDR_LEN; | |||||
ah->ar_pln = sizeof(struct in_addr); | |||||
ah->ar_op = htons(ARPOP_REQUEST); | |||||
memcpy(ar_sha(ah), IF_LLADDR(ifp), ETHER_ADDR_LEN); | |||||
((struct in_addr *)ar_spa(ah))->s_addr = pcb->pp_client; | |||||
bzero(ar_tha(ah), ETHER_ADDR_LEN); | |||||
((struct in_addr *)ar_tpa(ah))->s_addr = dst; | |||||
return (panicnet_ether_output(m, ifp, bcast, ETHERTYPE_ARP)); | |||||
} | |||||
/* | |||||
* Handler for ARP packets: checks their sanity and then | |||||
* 1. If the ARP is a request for our IP, respond with our MAC address | |||||
* 2. If the ARP is a response from our server, record its MAC address | |||||
* | |||||
* It needs to replicate partially the behaviour of arpintr() and | |||||
* in_arpinput(). | |||||
* | |||||
* Parameters: | |||||
* pcb a pointer to the live panicnet PCB | |||||
* mb a pointer to an mbuf * containing the packet received | |||||
* Updates *mb if m_pullup et al change the pointer | |||||
* Assumes the calling function will take care of freeing the mbuf | |||||
*/ | |||||
void | |||||
panicnet_handle_arp(struct panicnet_pcb *pcb, struct mbuf **mb) | |||||
{ | |||||
char buf[INET_ADDRSTRLEN]; | |||||
struct in_addr isaddr, itaddr; | |||||
struct ether_addr dst; | |||||
struct mbuf *m; | |||||
struct arphdr *ah; | |||||
struct ifnet *ifp; | |||||
uint8_t *enaddr; | |||||
int req_len, op; | |||||
m = *mb; | |||||
ifp = m->m_pkthdr.rcvif; | |||||
if (m->m_len < sizeof(struct arphdr)) { | |||||
m = m_pullup(m, sizeof(struct arphdr)); | |||||
*mb = m; | |||||
if (m == NULL) { | |||||
PNETDEBUG("runt packet: m_pullup failed\n"); | |||||
return; | |||||
} | |||||
} | |||||
ah = mtod(m, struct arphdr *); | |||||
if (ntohs(ah->ar_hrd) != ARPHRD_ETHER) { | |||||
PNETDEBUG("unknown hardware address 0x%2D)\n", | |||||
(unsigned char *)&ah->ar_hrd, ""); | |||||
return; | |||||
} | |||||
if (ntohs(ah->ar_pro) != ETHERTYPE_IP) { | |||||
PNETDEBUG("drop ARP for unknown protocol %d\n", | |||||
ntohs(ah->ar_pro)); | |||||
return; | |||||
} | |||||
req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); | |||||
if (m->m_len < req_len) { | |||||
m = m_pullup(m, req_len); | |||||
*mb = m; | |||||
if (m == NULL) { | |||||
PNETDEBUG("runt packet: m_pullup failed\n"); | |||||
return; | |||||
} | |||||
} | |||||
ah = mtod(m, struct arphdr *); | |||||
op = ntohs(ah->ar_op); | |||||
memcpy(&isaddr, ar_spa(ah), sizeof(isaddr)); | |||||
memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr)); | |||||
enaddr = (uint8_t *)IF_LLADDR(ifp); | |||||
if (memcmp(ar_sha(ah), enaddr, ifp->if_addrlen) == 0) { | |||||
PNETDEBUG("ignoring ARP from myself\n"); | |||||
return; | |||||
} | |||||
if (isaddr.s_addr == pcb->pp_client) { | |||||
printf("%s: %*D is using my IP address %s!\n", __func__, | |||||
ifp->if_addrlen, (u_char *)ar_sha(ah), ":", | |||||
inet_ntoa_r(isaddr, buf)); | |||||
return; | |||||
} | |||||
if (memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen) == 0) { | |||||
PNETDEBUG("ignoring ARP from broadcast address\n"); | |||||
return; | |||||
} | |||||
if (op == ARPOP_REPLY) { | |||||
if (isaddr.s_addr != pcb->pp_gateway && | |||||
isaddr.s_addr != pcb->pp_server) { | |||||
inet_ntoa_r(isaddr, buf); | |||||
PNETDEBUG("ignoring ARP reply from %s (not configured" | |||||
" server or gateway)\n", buf); | |||||
return; | |||||
} | |||||
memcpy(pcb->pp_gw_mac.octet, ar_sha(ah), | |||||
min(ah->ar_hln, ETHER_ADDR_LEN)); | |||||
PNETDEBUG("got server MAC address %6D\n", | |||||
pcb->pp_gw_mac.octet, ":"); | |||||
MPASS(pcb->pp_state == PN_STATE_INIT); | |||||
pcb->pp_state = PN_STATE_HAVE_GW_MAC; | |||||
return; | |||||
} | |||||
if (op != ARPOP_REQUEST) { | |||||
PNETDEBUG("ignoring ARP non-request/reply\n"); | |||||
return; | |||||
} | |||||
if (itaddr.s_addr != pcb->pp_client) { | |||||
PNETDEBUG("ignoring ARP not to our IP\n"); | |||||
return; | |||||
} | |||||
memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); | |||||
memcpy(ar_sha(ah), enaddr, ah->ar_hln); | |||||
memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); | |||||
memcpy(ar_spa(ah), &itaddr, ah->ar_pln); | |||||
ah->ar_op = htons(ARPOP_REPLY); | |||||
ah->ar_pro = htons(ETHERTYPE_IP); | |||||
m->m_flags &= ~(M_BCAST|M_MCAST); | |||||
m->m_len = arphdr_len(ah); | |||||
m->m_pkthdr.len = m->m_len; | |||||
memcpy(dst.octet, ar_tha(ah), ETHER_ADDR_LEN); | |||||
panicnet_ether_output(m, ifp, dst, ETHERTYPE_ARP); | |||||
*mb = NULL; | |||||
} | |||||
/* | |||||
* Sends ARP requests to locate the server and waits for a response. | |||||
* We first try to ARP the server itself, and fall back to the provided | |||||
* gateway if the server appears to be off-link. | |||||
* | |||||
* Return value: | |||||
* 0 on success | |||||
* errno on error | |||||
*/ | |||||
int | |||||
panicnet_arp_gw(struct panicnet_pcb *pcb) | |||||
{ | |||||
in_addr_t dst; | |||||
int error, polls, retries; | |||||
dst = pcb->pp_server; | |||||
restart: | |||||
for (retries = 0; retries < panicnet_arp_nretries; retries++) { | |||||
error = panicnet_send_arp(pcb, dst); | |||||
if (error != 0) | |||||
return (error); | |||||
for (polls = 0; polls < panicnet_npolls && | |||||
pcb->pp_state < PN_STATE_HAVE_GW_MAC; polls++) { | |||||
panicnet_network_poll(pcb->pp_ifp); | |||||
DELAY(500); | |||||
} | |||||
if (pcb->pp_state >= PN_STATE_HAVE_GW_MAC) | |||||
break; | |||||
printf("(ARP retry)"); | |||||
} | |||||
if (pcb->pp_state >= PN_STATE_HAVE_GW_MAC) | |||||
return (0); | |||||
if (dst == pcb->pp_server) { | |||||
printf("\nFailed to ARP server"); | |||||
if (pcb->pp_gateway != INADDR_ANY) { | |||||
printf(", trying to reach gateway...\n"); | |||||
dst = pcb->pp_gateway; | |||||
goto restart; | |||||
} else | |||||
printf(".\n"); | |||||
} else | |||||
printf("\nFailed to ARP gateway.\n"); | |||||
return (ETIMEDOUT); | |||||
} |