📄 uip.c
字号:
/*
* Copyright (c) 2001, Adam Dunkels.
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Adam Dunkels.
* 4. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
*
* This file is part of the uIP TCP/IP stack.
*
* $Id: uip.c,v 1.24 2001/11/23 06:50:07 adam Exp $
*
*/
/*
This is a small implementation of the IP and TCP protocols (as well as
some basic ICMP stuff). The implementation couples the IP, TCP and the
application layers very tightly. To keep the size of the compiled code
down, this code also features heavy usage of the goto statement.
The principle is that we have a small buffer, called the uip_buf, in which
the device driver puts an incoming packet. The TCP/IP stack parses the
headers in the packet, and calls upon the application. If the remote
host has sent data to the application, this data is present in the uip_buf
and the application read the data from there. It is up to the
application to put this data into a byte stream if needed. The
application will not be fed with data that is out of sequence.
If the application whishes to send data to the peer, it should put its
data into the uip_buf, 40 bytes from the start of the buffer. The TCP/IP
stack will calculate the checksums, and fill in the necessary header
fields and finally send the packet back to the peer. */
#include "uip.h"
#include "uipopt.h"
#include "uip_arch.h"
typedef struct {
/* IP header. */
u8_t vhl,
tos,
len[2],
ipid[2],
ipoffset[2],
ttl,
proto;
u16_t ipchksum;
u16_t srcipaddr[2],
destipaddr[2];
/* ICMP (echo) header. */
u8_t type, icode;
u16_t icmpchksum;
u16_t id, seqno;
} ipicmphdr;
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define IP_PROTO_ICMP 1
#define IP_PROTO_TCP 6
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO 8
u8_t uip_buf[UIP_BUFSIZE];
volatile u8_t *uip_appdata;
#if UIP_BUFSIZE > 255
volatile u16_t uip_len;
#else
volatile u8_t uip_len;
#endif /* UIP_BUFSIZE > 255 */
volatile u8_t uip_flags;
struct uip_conn *uip_conn;
static u16_t ipid;
void uip_process(u8_t flag);
struct uip_conn uip_conns[UIP_CONNS];
u16_t uip_listenports[UIP_LISTENPORTS];
/* Temporary variables. */
static u8_t tmpcnt;
static u16_t tmpport;
/* Macros. */
#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define ICMPBUF ((ipicmphdr *)&uip_buf[UIP_LLH_LEN])
#if UIP_STATISTICS == 1
struct uip_stats uip_stat;
#define UIP_STAT(s) s
#else
#define UIP_STAT(s)
#endif /* UIP_STATISTICS == 1 */
#if UIP_LOGGING == 1
#define UIP_LOG(m) uip_log(m)
#else
#define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */
/* The "rexmit_shift" table takes care of the exponential backoff -
the RTO will be shifted rexmit_shift[number of retransmissions]. */
static const u8_t rexmit_shift[8] = {0,1,2,3,4,4,4,4};
/*-----------------------------------------------------------------------------------*/
void
uip_init(void)
{
for(tmpcnt = 0; tmpcnt < UIP_LISTENPORTS; ++tmpcnt) {
uip_listenports[tmpcnt] = 0;
}
for(tmpcnt = 0; tmpcnt < UIP_CONNS; ++tmpcnt) {
uip_conns[tmpcnt].tcpstateflags = CLOSED;
}
}
/*-----------------------------------------------------------------------------------*/
void
uip_listen(u16_t port)
{
for(tmpcnt = 0; tmpcnt < UIP_LISTENPORTS; ++tmpcnt) {
if(uip_listenports[tmpcnt] == 0) {
uip_listenports[tmpcnt] = htons(port);
break;
}
}
}
/*-----------------------------------------------------------------------------------*/
void
uip_periodic(u8_t conn)
{
uip_conn = &uip_conns[conn];
uip_process(UIP_TIMER);
}
/*-----------------------------------------------------------------------------------*/
void
uip_process(u8_t flag)
{
uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
/* Check if we were invoked because of the perodic timer fireing. */
if(flag == UIP_TIMER) {
uip_len = 0;
if(uip_conn->tcpstateflags == TIME_WAIT ||
uip_conn->tcpstateflags == FIN_WAIT_2) {
++(uip_conn->timer);
if(uip_conn->timer == UIP_TIME_WAIT_TIMEOUT) {
uip_conn->tcpstateflags = CLOSED;
}
} else if(uip_conn->tcpstateflags != CLOSED) {
/* If the connection has outstanding data, we increase the
connection's timer and see if it has reached the RTO value
in which case we retransmit. */
if(uip_conn->tcpstateflags & UIP_OUTSTANDING) {
++(uip_conn->timer);
if(uip_conn->timer ==
(UIP_RTO << rexmit_shift[uip_conn->nrtx])) {
uip_conn->timer = 0;
++(uip_conn->nrtx);
if(uip_conn->nrtx == 8) {
/* XXX: we should call UIP_APPCALL() with uip_flags set to
UIP_ABORTED or similar. But this will do for now. */
uip_conn->tcpstateflags = CLOSED;
return;
}
/* Ok, so we need to retransmit. We do this differently
depending on which state we are in. In ESTABLISHED, we
call upon the application so that it may prepare the
data for the retransmit. In SYN_RCVD, we resend the
SYNACK that we sent earlier and in LAST_ACK we have to
retransmit our FINACK. */
UIP_STAT(++uip_stat.tcp.rexmit);
switch(uip_conn->tcpstateflags & TS_MASK) {
case SYN_RCVD:
/* In the SYN_RCVD state, we should retransmit our
SYNACK. */
goto tcp_send_synack;
case ESTABLISHED:
/* In the ESTABLISHED state, we call upon the application
to do the actual retransmit after which we jump into
the code for sending out the packet (the apprexmit
label). */
uip_len = 0;
uip_flags = UIP_REXMIT;
UIP_APPCALL();
goto apprexmit;
case FIN_WAIT_1:
case CLOSING:
case LAST_ACK:
/* In all these states we should retransmit a FINACK. */
goto tcp_send_finack;
}
}
} else if(uip_conn->tcpstateflags == ESTABLISHED) {
/* If there was no need for a retransmission, we poll the
application for new data. */
uip_len = 0;
uip_flags = UIP_POLL;
UIP_APPCALL();
goto appsend;
}
}
goto drop;
}
/* This is where the input processing starts. */
UIP_STAT(++uip_stat.ip.recv);
/* Check validity of the IP header. */
if(BUF->vhl != 0x45) { /* IP version and header length. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.vhlerr);
UIP_LOG("ip: invalid version or header length.");
goto drop;
}
/* Check the size of the packet. If the size reported to us in
uip_len doesn't match the size reported in the IP header, there
has been a transmission error and we drop the packet. */
#if UIP_BUFSIZE > 255
if(BUF->len[0] != (uip_len >> 8)) {
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.hblenerr);
UIP_LOG("ip: invalid length, high byte.");
/* IP length, high byte. */
goto drop;
}
if(BUF->len[1] != (uip_len & 0xff)) {
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.lblenerr);
UIP_LOG("ip: invalid length, low byte.");
/* IP length, low byte. */
goto drop;
}
#else
if(BUF->len[0] != 0) { /* IP length, high byte. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.hblenerr);
UIP_LOG("ip: invalid length, high byte.");
goto drop;
}
if(BUF->len[1] != uip_len) { /* IP length, low byte. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.lblenerr);
UIP_LOG("ip: invalid length, low byte.");
goto drop;
}
#endif /* UIP_BUFSIZE > 255 */
if(BUF->ipoffset[0] & 0x3f) { /* We don't allow IP fragments. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.fragerr);
UIP_LOG("ip: fragment dropped.");
goto drop;
}
/* Check if the packet is destined for our IP address. */
if(BUF->destipaddr[0] != htons(((u16_t)UIP_IPADDR0 << 8) | UIP_IPADDR1)) {
UIP_STAT(++uip_stat.ip.drop);
UIP_LOG("ip: packet not for us.");
goto drop;
}
if(BUF->destipaddr[1] != htons(((u16_t)UIP_IPADDR2 << 8) | UIP_IPADDR3)) {
UIP_STAT(++uip_stat.ip.drop);
UIP_LOG("ip: packet not for us.");
goto drop;
}
if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header
checksum. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.chkerr);
UIP_LOG("ip: bad checksum.");
goto drop;
}
if(BUF->proto == IP_PROTO_TCP) /* Check for TCP packet. If so, jump
to the tcp_input label. */
goto tcp_input;
if(BUF->proto != IP_PROTO_ICMP) { /* We only allow ICMP packets from
here. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.protoerr);
UIP_LOG("ip: neither tcp nor icmp.");
goto drop;
}
UIP_STAT(++uip_stat.icmp.recv);
/* ICMP echo (i.e., ping) processing. This is simple, we only change
the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
checksum before we return the packet. */
if(ICMPBUF->type != ICMP_ECHO) {
UIP_STAT(++uip_stat.icmp.drop);
UIP_STAT(++uip_stat.icmp.typeerr);
UIP_LOG("icmp: not icmp echo.");
goto drop;
}
ICMPBUF->type = ICMP_ECHO_REPLY;
if(ICMPBUF->icmpchksum >= htons(0xffff - (ICMP_ECHO << 8))) {
ICMPBUF->icmpchksum += htons(ICMP_ECHO << 8) + 1;
} else {
ICMPBUF->icmpchksum += htons(ICMP_ECHO << 8);
}
/* Swap IP addresses. */
tmpport = BUF->destipaddr[0];
BUF->destipaddr[0] = BUF->srcipaddr[0];
BUF->srcipaddr[0] = tmpport;
tmpport = BUF->destipaddr[1];
BUF->destipaddr[1] = BUF->srcipaddr[1];
BUF->srcipaddr[1] = tmpport;
UIP_STAT(++uip_stat.icmp.sent);
goto send;
tcp_input:
UIP_STAT(++uip_stat.tcp.recv);
if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
checksum. */
UIP_STAT(++uip_stat.tcp.drop);
UIP_STAT(++uip_stat.tcp.chkerr);
UIP_LOG("tcp: bad checksum.");
goto drop;
}
/* Demultiplex this segment. */
/* First check any active connections. */
for(uip_conn = &uip_conns[0]; uip_conn < &uip_conns[UIP_CONNS]; ++uip_conn) {
if(uip_conn->tcpstateflags != CLOSED &&
BUF->srcipaddr[0] == uip_conn->ripaddr[0] &&
BUF->srcipaddr[1] == uip_conn->ripaddr[1] &&
BUF->destport == uip_conn->lport &&
BUF->srcport == uip_conn->rport)
goto found;
}
/* If we didn't find and active connection that expected the packet,
either this packet is an old duplicate, or this is a SYN packet
destined for a connection in LISTEN. If the SYN flag isn't set,
it is an old packet and we send a RST. */
if(BUF->flags != TCP_SYN)
goto reset;
tmpport = BUF->destport;
/* Next, check listening connections. */
for(tmpcnt = 0; tmpcnt < UIP_LISTENPORTS &&
uip_listenports[tmpcnt] != 0;
++tmpcnt) {
if(tmpport == uip_listenports[tmpcnt])
goto found_listen;
}
/* No matching connection found, so we send a RST packet. */
UIP_STAT(++uip_stat.tcp.synrst);
reset:
/* We do not send resets in response to resets. */
if(BUF->flags & TCP_RST)
goto drop;
UIP_STAT(++uip_stat.tcp.rst);
BUF->flags = TCP_RST | TCP_ACK;
uip_len = 40;
BUF->tcpoffset = 5 << 4;
/* Flip the seqno and ackno fields in the TCP header. We also have
to increase the sequence number we are acknowledging. */
tmpcnt = BUF->seqno[3];
BUF->seqno[3] = BUF->ackno[3];
BUF->ackno[3] = tmpcnt + 1;
tmpcnt = BUF->seqno[2];
BUF->seqno[2] = BUF->ackno[2];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -