3 * http://www.ietf.org/rfc/rfc2131.txt
10 #include <acess/sys.h>
14 UDP_IOCTL_GETSETLPORT = 4,
15 UDP_IOCTL_GETSETRPORT,
16 UDP_IOCTL_GETSETRMASK,
20 #define FILENAME_MAX 255
21 // --- Translation functions ---
22 static inline uint32_t htonl(uint32_t v)
24 return (((v >> 24) & 0xFF) << 0)
25 | (((v >> 16) & 0xFF) << 8)
26 | (((v >> 8) & 0xFF) << 16)
27 | (((v >> 0) & 0xFF) << 24);
29 static inline uint16_t htons(uint16_t v)
31 return (((v >> 8) & 0xFF) << 0)
32 | (((v >> 0) & 0xFF) << 8);
35 #define ntohl(v) htonl(v)
36 #define ntohs(v) htons(v)
48 #define DHCP_MAGIC 0x63825363
52 uint8_t htype; // 1 = Ethernet
53 uint8_t hlen; // 6 bytes for MAC
54 uint8_t hops; // Hop counting
65 uint32_t dhcp_magic; // 0x63 0x82 0x53 0x63
69 typedef struct sInterface
71 struct sInterface *Next;
78 uint32_t TransactionID;
86 int main(int argc, char *argv[]);
87 void Scan_Dir(tInterface **IfaceList, const char *Directory);
88 int Start_Interface(tInterface *Iface);
89 void Send_DHCPDISCOVER(tInterface *Iface);
90 void Send_DHCPREQUEST(tInterface *Iface, void *OfferBuffer, int TypeOffset);
91 int Handle_Packet(tInterface *Iface);
92 int Handle_Timeout(tInterface *Iface);
93 void Update_State(tInterface *Iface, int newState);
94 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router);
97 int main(int argc, char *argv[])
99 tInterface *ifaces = NULL;
102 fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
107 ifaces = malloc(sizeof(tInterface));
109 ifaces->Adapter = argv[1];
112 Scan_Dir( &ifaces, "/Devices/ip/adapters" );
115 for( tInterface *i = ifaces; i; i = i->Next )
117 if( Start_Interface(i) < 0 ) {
120 i->State = STATE_PREINIT;
123 Send_DHCPDISCOVER(i);
130 int64_t timeout = 1000;
134 for( tInterface *i = ifaces; i; i = i->Next )
136 FD_SET(i->SocketFD, &fds);
137 if(maxfd < i->SocketFD) maxfd = i->SocketFD;
140 if( _SysSelect(maxfd+1, &fds, NULL, NULL, &timeout, 0) < 0 )
142 perror("_SysSelect returned error");
146 _SysDebug("select() returned");
148 // Check for changes (with magic to allow inline deletion)
149 for( tInterface *p = (void*)&ifaces, *i = ifaces; i; p=i,i = i->Next )
151 if( FD_ISSET(i->SocketFD, &fds) )
153 if( Handle_Packet( i ) )
157 if( _SysTimestamp() > i->Timeout )
159 if( Handle_Timeout(i) )
161 fprintf(stderr, "DHCP on %s timed out\n", i->Adapter);
167 _SysClose(i->SocketFD);
168 _SysClose(i->IfaceFD);
177 void Scan_Dir(tInterface **IfaceList, const char *Directory)
179 int dp = _SysOpen(Directory, OPENFLAG_READ);
180 char filename[FILENAME_MAX];
183 fprintf(stderr, "Unable to open directory '%s'\n", Directory);
187 while( _SysReadDir(dp, filename) )
189 if( filename[0] == '.' ) continue ;
190 if( strcmp(filename, "lo") == 0 ) continue ;
192 tInterface *new = malloc(sizeof(tInterface) + strlen(filename)+1);
193 new->Adapter = (void*)(new + 1);
194 strcpy(new->Adapter, filename);
195 new->Next = *IfaceList;
202 int Start_Interface(tInterface *Iface)
205 char path[] = "/Devices/ip/XXXXX/udpc";
206 char addr[4] = {0,0,0,0};
208 // TODO: Check that the adapter is not in use
210 // Request MAC address from network adapter
212 char path[] = "/Devices/ip/adapters/ethXXXX";
213 sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter);
214 fd = _SysOpen(path, 0);
216 perror("Opening adapter");
217 _SysDebug("Unable to open adapter %s", path);
220 if( _SysIOCtl(fd, 4, Iface->HWAddr) ) {
221 perror("Getting MAC address");
227 // Initialise an interface, with a dummy IP address (zero)
228 fd = _SysOpen("/Devices/ip", 0);
230 fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n");
238 ifinfo.Device = Iface->Adapter;
241 Iface->Num = _SysIOCtl(fd, 4, &ifinfo); // Create interface
242 if( Iface->Num == -1 ) {
243 fprintf(stderr, "ERROR: Unable to create new interface\n");
248 // Open new interface
249 snprintf(path, sizeof(path), "/Devices/ip/%i", Iface->Num);
250 Iface->IfaceFD = fd = _SysOpen(path, 0);
252 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
255 _SysIOCtl(fd, 6, addr); // Set address to 0.0.0.0
256 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Set subnet mask to 0
259 snprintf(path, sizeof(path), "/Devices/ip/%i/udp", Iface->Num);
260 Iface->SocketFD = fd = _SysOpen(path, OPENFLAG_READ|OPENFLAG_WRITE);
262 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
265 tmp = 68; _SysIOCtl(fd, UDP_IOCTL_GETSETLPORT, &tmp); // Local port
266 tmp = 67; _SysIOCtl(fd, UDP_IOCTL_GETSETRPORT, &tmp); // Remote port
267 tmp = 0; _SysIOCtl(fd, UDP_IOCTL_GETSETRMASK, &tmp); // Remote addr mask bits - don't care source
268 addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255
269 _SysIOCtl(fd, UDP_IOCTL_SETRADDR, addr); // Remote address
274 void Send_DHCPRELEASE(tInterface *Iface)
278 void Send_DHCPDISCOVER(tInterface *Iface)
280 uint32_t transaction_id;
281 struct sDHCP_Message *msg;
282 char data[8 + sizeof(struct sDHCP_Message) + 3 + 1];
283 msg = (void*)data + 8;
285 _SysDebug("DHCPDISCOVER to %s", Iface->Adapter);
287 transaction_id = rand();
288 Iface->TransactionID = transaction_id;
290 msg->op = htonb(1); // BOOTREQUEST
291 msg->htype = htonb(1); // 10mb Ethernet
292 msg->hlen = htonb(6); // 6 byte MAC
293 msg->hops = htonb(0); // Hop count so far
294 msg->xid = htonl(transaction_id); // Transaction ID
295 msg->secs = htons(0); // secs - No time has elapsed
296 msg->flags = htons(0x0000); // flags - Broadcast is unset
297 msg->ciaddr = htonl(0); // ciaddr - Zero, as we don't have one yet
298 msg->yiaddr = htonl(0); // yiaddr - Zero?
299 msg->siaddr = htonl(0); // siaddr - Zero? maybe -1
300 msg->giaddr = htonl(0); // giaddr - Zero?
301 memcpy(msg->chaddr, Iface->HWAddr, 6);
303 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
304 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
305 msg->dhcp_magic = htonl(DHCP_MAGIC);
308 msg->options[i++] = 53; // DHCP Message Type
309 msg->options[i++] = 1;
310 msg->options[i++] = 1; // - DHCPDISCOVER
311 msg->options[i ] = 255; // End of list
315 data[0] = 67; data[1] = 0; // Port
316 data[2] = 4; data[3] = 0; // AddrType
317 data[4] = 255; data[5] = 255; data[6] = 255; data[7] = 255;
319 i = _SysWrite(Iface->SocketFD, data, sizeof(data));
320 if( i != sizeof(data) ) {
321 _SysDebug("_SysWrite failed (%i != %i): %s", i, sizeof(data),
324 Update_State(Iface, STATE_DISCOVER_SENT);
327 void Send_DHCPREQUEST(tInterface *Iface, void *OfferPacket, int TypeOffset)
329 struct sDHCP_Message *msg;
331 msg = (void*) ((char*)OfferPacket) + 8;
333 // Reuses old data :)
339 msg->secs = htons(0); // TODO: Maintain times
340 msg->flags = htons(0);
341 memcpy(msg->chaddr, Iface->HWAddr, 6);
342 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
343 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
346 msg->options[i++] = 53; // Message type = DHCPREQUEST
347 msg->options[i++] = 1;
348 msg->options[i++] = 3;
349 msg->options[i++] = 50; // Requested Address
350 msg->options[i++] = 4;
351 memcpy(msg->options + i, &msg->yiaddr, 4); i += 4;
352 // msg->options[i++] = 54; // Server identifier
353 // msg->options[i++] = 4;
354 // memcpy(msg->options + i, (char*)OfferPacket + 4, 4); i += 4;
355 msg->options[i++] = 255;
357 // Clear last because yiaddr is needed in option setup
358 msg->ciaddr = htonl(0);
359 msg->yiaddr = htonl(0);
360 msg->siaddr = htonl(0);
361 msg->giaddr = htonl(0);
364 ((uint8_t*)OfferPacket)[4] = 255;
365 ((uint8_t*)OfferPacket)[5] = 255;
366 ((uint8_t*)OfferPacket)[6] = 255;
367 ((uint8_t*)OfferPacket)[7] = 255;
369 _SysWrite(Iface->SocketFD, OfferPacket, 8 + sizeof(*msg) + i);
370 Update_State(Iface, STATE_REQUEST_SENT);
373 int Handle_Packet(tInterface *Iface)
376 struct sDHCP_Message *msg = (void*)(data + 8);
379 int dhcp_msg_type = 0, dhcp_msg_type_ofs;
381 void *subnet_mask = NULL;
383 _SysDebug("Doing read on %i", Iface->SocketFD);
384 len = _SysRead(Iface->SocketFD, data, sizeof(data));
385 _SysDebug("len = %i", len);
387 _SysDebug("*msg = {");
388 _SysDebug(" .op = %i", msg->op);
389 _SysDebug(" .htype = %i", msg->htype);
390 _SysDebug(" .ciaddr = 0x%x", ntohl(msg->ciaddr));
391 _SysDebug(" .yiaddr = 0x%x", ntohl(msg->yiaddr));
395 _SysDebug("Not a response message");
399 if( htonl(msg->dhcp_magic) != DHCP_MAGIC ) {
400 _SysDebug("DHCP magic doesn't match (got 0x%x, expected 0x%x)",
401 htonl(msg->dhcp_magic), DHCP_MAGIC);
406 // Check if the packet is related to our requests
407 if( ntohl(msg->xid) != Iface->TransactionID ) {
408 _SysDebug("Transaction ID mis-match, ignoring (0x%x != 0x%x)",
409 ntohl(msg->xid), Iface->TransactionID);
412 if( memcmp(msg->chaddr, Iface->HWAddr, 6) != 0 ) {
413 _SysDebug("Hardware address mis-match, ignoring");
419 while( i < len - sizeof(*msg) - 8 && msg->options[i] != 255 )
421 if( msg->options[i] == 0 ) {
425 _SysDebug("Option %i, %i bytes long", msg->options[i], msg->options[i+1]);
426 switch(msg->options[i])
429 subnet_mask = &msg->options[i+2];
432 router = &msg->options[i+2];
435 dhcp_msg_type_ofs = i;
436 dhcp_msg_type = msg->options[i+2];
439 i += msg->options[i+1]+2;
442 _SysDebug("dhcp_msg_type = %i", dhcp_msg_type);
444 switch( dhcp_msg_type )
446 case 1: // DHCPDISCOVER - wut?
449 // Send out request for this address
450 if( Iface->State != STATE_DISCOVER_SENT ) {
451 _SysDebug("Ignoring DHCPOFFER when not in STATE_DISCOVER_SENT");
454 Send_DHCPREQUEST(Iface, data, dhcp_msg_type_ofs);
456 case 3: // DHCPREQUEST - wut?
458 case 4: // DHCPDECLINE - ?
461 SetAddress(Iface, &msg->yiaddr, subnet_mask, router);
462 // Return 1 to remove from list
468 int Handle_Timeout(tInterface *Iface)
470 if( Iface->nTimeouts == 3 )
474 case STATE_DISCOVER_SENT:
475 Send_DHCPDISCOVER(Iface);
478 _SysDebug("Timeout with state = %i", Iface->State);
484 void Update_State(tInterface *Iface, int newState)
486 if( Iface->State != newState )
488 Iface->Timeout = _SysTimestamp() + 500;
489 Iface->State = newState;
490 Iface->nTimeouts = 0;
494 // TODO: Exponential backoff
495 Iface->Timeout = _SysTimestamp() + 3000;
497 _SysDebug("State %i repeated, timeout is 3000ms now", newState);
501 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router)
505 // Translate the mask
508 uint8_t *mask = Mask;
510 _SysDebug("Mask %i.%i.%i.%i", mask[0], mask[1], mask[2], mask[3]);
511 for( i = 0; i < 4 && mask[i] == 0xFF; i ++ ) ;
521 case 0x00: mask_bits += 0; break;
522 case 0x80: mask_bits += 1; break;
523 case 0xC0: mask_bits += 2; break;
524 case 0xE0: mask_bits += 3; break;
525 case 0xF0: mask_bits += 4; break;
526 case 0xF8: mask_bits += 5; break;
527 case 0xFC: mask_bits += 6; break;
528 case 0xFE: mask_bits += 7; break;
537 uint8_t *addr = Addr;
538 _SysDebug("Addr %i.%i.%i.%i/%i", addr[0], addr[1], addr[2], addr[3], mask_bits);
540 printf("Assigned %i.%i.%i.%i/%i to IF#%i (%s)\n",
541 addr[0], addr[1], addr[2], addr[3], mask_bits,
542 Iface->Num, Iface->Adapter
546 _SysIOCtl(Iface->IfaceFD, 6, Addr);
547 _SysIOCtl(Iface->IfaceFD, 7, &mask_bits);
551 uint8_t *addr = Router;
552 _SysDebug("Router %i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
556 fd = _SysOpen("/Devices/ip/routes/4:00000000:0:0", OPENFLAG_CREATE);
558 fprintf(stderr, "ERROR: Unable to open default route\n");
561 char ifname[snprintf(NULL,0,"%i",Iface->Num)+1];
562 sprintf(ifname, "%i", Iface->Num);
563 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_nexthop"), Router);
564 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_interface"), ifname);