diff options
Diffstat (limited to 'c')
| -rw-r--r-- | c/dhcp.c | 72 | ||||
| -rw-r--r-- | c/sh.c | 6 | ||||
| -rw-r--r-- | c/sock.h | 7 | ||||
| -rw-r--r-- | c/tcp.c | 107 |
4 files changed, 192 insertions, 0 deletions
diff --git a/c/dhcp.c b/c/dhcp.c new file mode 100644 index 0000000..e4186f4 --- /dev/null +++ b/c/dhcp.c @@ -0,0 +1,72 @@ +#include "sock.h" +/* dhcp - a minimal DHCP client. DISCOVER -> OFFER -> REQUEST -> ACK over UDP + (kernel owns UDP; this owns BOOTP/DHCP), then hands the leased address to the + kernel with net_setip(). Run at boot before the shell. Times out gracefully + if there's no server (the shell then comes up unconfigured). */ + +static unsigned char pkt[288]; /* BOOTP is ~240B + options (frames > 255) */ +static unsigned char ip[4]; +static unsigned char bcast[4]; + +/* fill the fixed BOOTP request header + magic cookie; options start at 240 */ +static void bootp(void) { + unsigned int i; + for (i = 0; i < 240; i++) pkt[i] = 0; + pkt[0] = 1; pkt[1] = 1; pkt[2] = 6; /* op=request htype=eth hlen=6 */ + pkt[4] = 0x12; pkt[5] = 0x34; pkt[6] = 0x56; pkt[7] = 0x78; /* xid */ + pkt[10] = 0x80; /* flags: broadcast reply */ + pkt[28] = 0x02; pkt[29] = 0x47; pkt[30] = 0x42; pkt[33] = 0x01; /* client MAC */ + pkt[236] = 0x63; pkt[237] = 0x82; pkt[238] = 0x53; pkt[239] = 0x63; /* magic */ +} + +/* DHCP message type from the received packet (option 53), or 0 */ +static unsigned char mtype(void) { + unsigned int p = 240; + while (p < 280 && pkt[p] != 255) { + if (pkt[p] == 0) { p++; continue; } /* pad */ + if (pkt[p] == 53) return pkt[p + 2]; + p += 2 + pkt[p + 1]; + } + return 0; +} + +void main(void) { + unsigned char s, i, n; + unsigned int p; + bcast[0] = 255; bcast[1] = 255; bcast[2] = 255; bcast[3] = 255; + + puts("dhcp: discovering"); nl(); + s = net_socket(SOCK_UDP); + if (s == 0xFF) { puts("dhcp: no socket"); nl(); return; } + net_bind(s, 68); + net_connect(s, bcast, 67); + + /* DISCOVER -> OFFER (retry a few times for startup timing) */ + bootp(); + p = 240; + pkt[p++] = 53; pkt[p++] = 1; pkt[p++] = 1; /* DHCPDISCOVER */ + pkt[p++] = 255; + n = 0xFF; + for (i = 0; i < 3 && n == 0xFF; i++) { + net_send(s, pkt, (unsigned char)p); + n = net_recv(s, pkt, 255); + } + if (n == 0xFF || mtype() != 2) { puts("dhcp: no offer"); nl(); net_close(s); return; } + ip[0] = pkt[16]; ip[1] = pkt[17]; ip[2] = pkt[18]; ip[3] = pkt[19]; /* yiaddr */ + + /* REQUEST -> ACK */ + bootp(); + p = 240; + pkt[p++] = 53; pkt[p++] = 1; pkt[p++] = 3; /* DHCPREQUEST */ + pkt[p++] = 50; pkt[p++] = 4; /* requested IP */ + pkt[p++] = ip[0]; pkt[p++] = ip[1]; pkt[p++] = ip[2]; pkt[p++] = ip[3]; + pkt[p++] = 255; + net_send(s, pkt, (unsigned char)p); + n = net_recv(s, pkt, 255); + net_close(s); + if (n == 0xFF || mtype() != 5) { puts("dhcp: request failed"); nl(); return; } + ip[0] = pkt[16]; ip[1] = pkt[17]; ip[2] = pkt[18]; ip[3] = pkt[19]; + + net_setip(ip); + puts("dhcp: leased "); for (i = 0; i < 4; i++) { putu(ip[i]); if (i < 3) putc('.'); } nl(); +} @@ -155,6 +155,12 @@ void main(void) { char *tok[16]; char cwd[40]; cwd[0] = '/'; cwd[1] = 0; + /* bring up networking first: run the DHCP client and wait for it to finish + (got a lease, or gave up). Only then drop to the prompt. */ + { + unsigned char d = lookup("dhcp"); + if (d != NOFD) { if (fork() == 0) exec(d); else wait(); } + } for (;;) { unsigned char nt, i, pipepos, c, bg; /* reap finished background jobs */ @@ -55,4 +55,11 @@ static void net_close(unsigned char s) { static void net_poll(void) { _nr.op = 6; sys_net(&_nr); } +/* set our IPv4 address (the DHCP client calls this once it has a lease) */ +static void net_setip(const unsigned char *ip) { + unsigned char i; + _nr.op = 7; + for (i = 0; i < 4; i++) _nr.ip[i] = ip[i]; + sys_net(&_nr); +} #endif @@ -0,0 +1,107 @@ +#include "netlib.h" +/* tcp - a minimal TCP client over SLIP (milestone C). Connects to 10.0.0.1:7, + sends a line, prints the reply, closes. No retransmission: the GB<->gateway + link is lossless, and the gateway's real socket handles the lossy side. + Us = 10.0.0.2, peer = 10.0.0.1. */ + +static unsigned char pkt[256]; +static unsigned long snd_nxt, rcv_nxt; +#define LPORT 40000u +#define RPORT 7u + +/* SYN 0x02, ACK 0x10, FIN 0x01, PSH 0x08, RST 0x04 */ + +static unsigned int sum16(unsigned char *d, unsigned int len, unsigned int sum) { + unsigned int w, i = 0; + while (i + 1 < len) { w = ((unsigned int)d[i] << 8) | d[i + 1]; sum += w; if (sum < w) sum++; i += 2; } + if (len & 1) { w = (unsigned int)d[len - 1] << 8; sum += w; if (sum < w) sum++; } + return sum; +} +static void put32(unsigned char *p, unsigned long v) { + p[0] = (unsigned char)(v >> 24); p[1] = (unsigned char)(v >> 16); + p[2] = (unsigned char)(v >> 8); p[3] = (unsigned char)v; +} +static unsigned long get32(unsigned char *p) { + return ((unsigned long)p[0] << 24) | ((unsigned long)p[1] << 16) + | ((unsigned long)p[2] << 8) | p[3]; +} + +/* build+send IP+TCP with 'flags' and 'dlen' payload bytes already at pkt[40..] */ +static void tcp_send(unsigned char flags, unsigned char dlen) { + unsigned int c, psum, tcplen = 20 + dlen, total = 20 + tcplen; + pkt[0] = 0x45; pkt[1] = 0; pkt[2] = (unsigned char)(total >> 8); pkt[3] = (unsigned char)total; + pkt[4] = 0; pkt[5] = 1; pkt[6] = 0x40; pkt[7] = 0; pkt[8] = 64; pkt[9] = 6; + pkt[10] = 0; pkt[11] = 0; + pkt[12] = 10; pkt[13] = 0; pkt[14] = 0; pkt[15] = 2; + pkt[16] = 10; pkt[17] = 0; pkt[18] = 0; pkt[19] = 1; + pkt[20] = LPORT >> 8; pkt[21] = (unsigned char)LPORT; + pkt[22] = RPORT >> 8; pkt[23] = (unsigned char)RPORT; + put32(pkt + 24, snd_nxt); + put32(pkt + 28, rcv_nxt); + pkt[32] = 0x50; pkt[33] = flags; pkt[34] = 2; pkt[35] = 0; /* window 512 */ + pkt[36] = 0; pkt[37] = 0; pkt[38] = 0; pkt[39] = 0; + psum = sum16(pkt + 12, 8, 0); + c = 6; psum += c; if (psum < c) psum++; + c = tcplen; psum += c; if (psum < c) psum++; + c = ~sum16(pkt + 20, tcplen, psum); + pkt[36] = (unsigned char)(c >> 8); pkt[37] = (unsigned char)c; + c = ~sum16(pkt, 20, 0); + pkt[10] = (unsigned char)(c >> 8); pkt[11] = (unsigned char)c; + slip_send((char *)pkt, (unsigned char)total); +} + +/* receive the next TCP segment for our connection; returns TCP header offset, + sets *flags,*seq,*dlen,*doff. */ +static unsigned char tcp_recv(unsigned char *flags, unsigned long *seq, + unsigned char *dlen, unsigned char *doff) { + unsigned char len, ihl, hl; + for (;;) { + len = slip_recv((char *)pkt, 255); + if (len < 40 || (pkt[0] >> 4) != 4 || pkt[9] != 6) continue; + ihl = (pkt[0] & 0x0f) << 2; + if (((unsigned int)pkt[22] << 8 | pkt[23]) != LPORT) continue; /* not our port */ + hl = (pkt[ihl + 12] >> 4) << 2; + *flags = pkt[ihl + 13]; + *seq = get32(pkt + ihl + 4); + *doff = (unsigned char)(ihl + hl); + *dlen = (unsigned char)(len - ihl - hl); + return ihl; + } +} + +void main(void) { + unsigned char flags, dlen, doff, i; + unsigned long seq; + puts("tcp: SYN -> 10.0.0.1:7"); nl(); + snd_nxt = 1000; rcv_nxt = 0; + tcp_send(0x02, 0); /* SYN */ + snd_nxt = 1001; + + tcp_recv(&flags, &seq, &dlen, &doff); /* expect SYN|ACK */ + if (!(flags & 0x02)) { puts("tcp: no SYN-ACK"); nl(); return; } + rcv_nxt = seq + 1; + tcp_send(0x10, 0); /* ACK -> ESTABLISHED */ + + pkt[40] = 'h'; pkt[41] = 'i'; pkt[42] = ' '; pkt[43] = 'g'; pkt[44] = 'b'; + tcp_send(0x18, 5); /* PSH|ACK "hi gb" */ + snd_nxt += 5; + + for (;;) { + tcp_recv(&flags, &seq, &dlen, &doff); + if (dlen) { + puts("tcp: recv '"); + for (i = 0; i < dlen; i++) putc(pkt[doff + i]); + puts("'"); nl(); + rcv_nxt = seq + dlen; + tcp_send(0x10, 0); /* ACK the data */ + tcp_send(0x11, 0); /* FIN|ACK - start closing */ + snd_nxt += 1; + } + if (flags & 0x01) { /* their FIN */ + rcv_nxt = seq + dlen + 1; + tcp_send(0x10, 0); /* ACK it */ + puts("tcp: closed"); nl(); + return; + } + } +} |
