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;
87 int main(int argc, char *argv[]);
88 void Scan_Dir(tInterface **IfaceList, const char *Directory);
89 int Start_Interface(tInterface *Iface);
90 void Send_DHCPDISCOVER(tInterface *Iface);
91 void Send_DHCPREQUEST(tInterface *Iface, void *OfferBuffer, int TypeOffset);
92 int Handle_Packet(tInterface *Iface);
93 int Handle_Timeout(tInterface *Iface);
94 void Update_State(tInterface *Iface, int newState);
95 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router);
98 int main(int argc, char *argv[])
100 tInterface *ifaces = NULL;
103 fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
108 ifaces = malloc(sizeof(tInterface));
110 ifaces->Adapter = argv[1];
113 Scan_Dir( &ifaces, "/Devices/ip/adapters" );
116 for( tInterface *i = ifaces; i; i = i->Next )
118 if( Start_Interface(i) < 0 ) {
121 i->State = STATE_PREINIT;
122 i->StartTime = _SysTimestamp();
125 Send_DHCPDISCOVER(i);
132 int64_t timeout = 1000;
136 for( tInterface *i = ifaces; i; i = i->Next )
138 FD_SET(i->SocketFD, &fds);
139 if(maxfd < i->SocketFD) maxfd = i->SocketFD;
142 if( _SysSelect(maxfd+1, &fds, NULL, NULL, &timeout, 0) < 0 )
144 perror("_SysSelect returned error");
148 _SysDebug("select() returned");
150 // Check for changes (with magic to allow inline deletion)
151 for( tInterface *p = (void*)&ifaces, *i = ifaces; i; p=i,i = i->Next )
153 if( FD_ISSET(i->SocketFD, &fds) )
155 if( Handle_Packet( i ) )
159 if( _SysTimestamp() > i->Timeout )
161 if( Handle_Timeout(i) )
163 fprintf(stderr, "DHCP on %s timed out\n", i->Adapter);
169 _SysClose(i->SocketFD);
170 _SysClose(i->IfaceFD);
179 void Scan_Dir(tInterface **IfaceList, const char *Directory)
181 int dp = _SysOpen(Directory, OPENFLAG_READ);
182 char filename[FILENAME_MAX];
185 fprintf(stderr, "Unable to open directory '%s'\n", Directory);
189 while( _SysReadDir(dp, filename) )
191 if( filename[0] == '.' ) continue ;
192 if( strcmp(filename, "lo") == 0 ) continue ;
194 tInterface *new = malloc(sizeof(tInterface) + strlen(filename)+1);
195 new->Adapter = (void*)(new + 1);
196 strcpy(new->Adapter, filename);
197 new->Next = *IfaceList;
204 int Start_Interface(tInterface *Iface)
207 char path[] = "/Devices/ip/XXXXX/udpc";
208 char addr[4] = {0,0,0,0};
210 // TODO: Check that the adapter is not in use
212 // Request MAC address from network adapter
214 char path[] = "/Devices/ip/adapters/ethXXXX";
215 sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter);
216 fd = _SysOpen(path, 0);
218 perror("Opening adapter");
219 _SysDebug("Unable to open adapter %s", path);
222 if( _SysIOCtl(fd, 4, Iface->HWAddr) ) {
223 perror("Getting MAC address");
229 // Initialise an interface, with a dummy IP address (zero)
230 fd = _SysOpen("/Devices/ip", 0);
232 fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n");
240 ifinfo.Device = Iface->Adapter;
243 Iface->Num = _SysIOCtl(fd, 4, &ifinfo); // Create interface
244 if( Iface->Num == -1 ) {
245 fprintf(stderr, "ERROR: Unable to create new interface\n");
250 // Open new interface
251 snprintf(path, sizeof(path), "/Devices/ip/%i", Iface->Num);
252 Iface->IfaceFD = fd = _SysOpen(path, 0);
254 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
257 _SysIOCtl(fd, 6, addr); // Set address to 0.0.0.0
258 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Set subnet mask to 0
261 snprintf(path, sizeof(path), "/Devices/ip/%i/udp", Iface->Num);
262 Iface->SocketFD = fd = _SysOpen(path, OPENFLAG_READ|OPENFLAG_WRITE);
264 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
267 tmp = 68; _SysIOCtl(fd, UDP_IOCTL_GETSETLPORT, &tmp); // Local port
268 tmp = 67; _SysIOCtl(fd, UDP_IOCTL_GETSETRPORT, &tmp); // Remote port
269 tmp = 0; _SysIOCtl(fd, UDP_IOCTL_GETSETRMASK, &tmp); // Remote addr mask bits - don't care source
270 addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255
271 _SysIOCtl(fd, UDP_IOCTL_SETRADDR, addr); // Remote address
276 void Send_DHCPRELEASE(tInterface *Iface)
280 void Send_DHCPDISCOVER(tInterface *Iface)
282 uint32_t transaction_id;
283 struct sDHCP_Message *msg;
284 char data[8 + sizeof(struct sDHCP_Message) + 3 + 1];
285 msg = (void*)data + 8;
287 _SysDebug("DHCPDISCOVER to %s", Iface->Adapter);
289 transaction_id = rand();
290 Iface->TransactionID = transaction_id;
292 msg->op = htonb(1); // BOOTREQUEST
293 msg->htype = htonb(1); // 10mb Ethernet
294 msg->hlen = htonb(6); // 6 byte MAC
295 msg->hops = htonb(0); // Hop count so far
296 msg->xid = htonl(transaction_id); // Transaction ID
297 msg->secs = htons(0); // secs - No time has elapsed
298 msg->flags = htons(0x0000); // flags - Broadcast is unset
299 msg->ciaddr = htonl(0); // ciaddr - Zero, as we don't have one yet
300 msg->yiaddr = htonl(0); // yiaddr - Zero?
301 msg->siaddr = htonl(0); // siaddr - Zero? maybe -1
302 msg->giaddr = htonl(0); // giaddr - Zero?
303 memcpy(msg->chaddr, Iface->HWAddr, 6);
304 memset(msg->chaddr+6, 0, sizeof(msg->chaddr)-6);
306 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
307 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
308 msg->dhcp_magic = htonl(DHCP_MAGIC);
311 msg->options[i++] = 53; // DHCP Message Type
312 msg->options[i++] = 1;
313 msg->options[i++] = 1; // - DHCPDISCOVER
314 msg->options[i ] = 255; // End of list
318 data[0] = 67; data[1] = 0; // Port
319 data[2] = 4; data[3] = 0; // AddrType
320 data[4] = 255; data[5] = 255; data[6] = 255; data[7] = 255;
322 i = _SysWrite(Iface->SocketFD, data, sizeof(data));
323 if( i != sizeof(data) ) {
324 _SysDebug("_SysWrite failed (%i != %i): %s", i, sizeof(data),
327 Update_State(Iface, STATE_DISCOVER_SENT);
330 void Send_DHCPREQUEST(tInterface *Iface, void *OfferPacket, int TypeOffset)
332 struct sDHCP_Message *msg;
334 msg = (void*) ((char*)OfferPacket) + 8;
336 if( msg->xid != htonl(Iface->TransactionID) ) {
337 _SysDebug("DHCPREQUEST: Transaction ID mismatch");
341 // Reuses old data :)
343 msg->htype = htonb(1);
344 msg->hlen = htonb(6);
345 msg->hops = htonb(0);
346 msg->xid = htonl(Iface->TransactionID);
347 msg->secs = htons( (_SysTimestamp()-Iface->StartTime+499)/1000 ); // TODO: Maintain times
348 msg->flags = htons(0x0000);
349 memcpy(msg->chaddr, Iface->HWAddr, 6);
350 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
351 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
354 msg->options[i++] = 53; // Message type = DHCPREQUEST
355 msg->options[i++] = 1;
356 msg->options[i++] = 3;
357 msg->options[i++] = 50; // Requested Address
358 msg->options[i++] = 4;
359 memcpy(msg->options + i, &msg->yiaddr, 4); i += 4;
360 // msg->options[i++] = 54; // Server identifier
361 // msg->options[i++] = 4;
362 // memcpy(msg->options + i, (char*)OfferPacket + 4, 4); i += 4;
363 msg->options[i++] = 255;
365 // Clear last because yiaddr is needed in option setup
366 msg->ciaddr = htonl(0);
367 msg->yiaddr = htonl(0);
368 msg->siaddr = htonl(0);
369 msg->giaddr = htonl(0);
372 ((uint8_t*)OfferPacket)[4] = 255;
373 ((uint8_t*)OfferPacket)[5] = 255;
374 ((uint8_t*)OfferPacket)[6] = 255;
375 ((uint8_t*)OfferPacket)[7] = 255;
377 _SysWrite(Iface->SocketFD, OfferPacket, 8 + sizeof(*msg) + i);
378 Update_State(Iface, STATE_REQUEST_SENT);
381 int Handle_Packet(tInterface *Iface)
384 struct sDHCP_Message *msg = (void*)(data + 8);
387 int dhcp_msg_type = 0, dhcp_msg_type_ofs;
389 void *subnet_mask = NULL;
391 _SysDebug("Doing read on %s %i", Iface->Adapter, Iface->SocketFD);
392 len = _SysRead(Iface->SocketFD, data, sizeof(data));
393 _SysDebug("len = %i", len);
395 _SysDebug("*msg = {");
396 _SysDebug(" .op = %i", msg->op);
397 _SysDebug(" .htype = %i", msg->htype);
398 _SysDebug(" .ciaddr = 0x%x", ntohl(msg->ciaddr));
399 _SysDebug(" .yiaddr = 0x%x", ntohl(msg->yiaddr));
403 _SysDebug("Not a response message");
407 if( htonl(msg->dhcp_magic) != DHCP_MAGIC ) {
408 _SysDebug("DHCP magic doesn't match (got 0x%x, expected 0x%x)",
409 htonl(msg->dhcp_magic), DHCP_MAGIC);
414 // Check if the packet is related to our requests
415 if( ntohl(msg->xid) != Iface->TransactionID ) {
416 _SysDebug("Transaction ID mis-match, ignoring (0x%x != 0x%x)",
417 ntohl(msg->xid), Iface->TransactionID);
420 if( memcmp(msg->chaddr, Iface->HWAddr, 6) != 0 ) {
421 _SysDebug("Hardware address mis-match, ignoring");
427 while( i < len - sizeof(*msg) - 8 && msg->options[i] != 255 )
429 if( msg->options[i] == 0 ) {
433 _SysDebug("Option %i, %i bytes long", msg->options[i], msg->options[i+1]);
434 switch(msg->options[i])
437 subnet_mask = &msg->options[i+2];
440 router = &msg->options[i+2];
443 dhcp_msg_type_ofs = i;
444 dhcp_msg_type = msg->options[i+2];
447 i += msg->options[i+1]+2;
450 _SysDebug("dhcp_msg_type = %i", dhcp_msg_type);
452 switch( dhcp_msg_type )
454 case 1: // DHCPDISCOVER - wut?
457 // Send out request for this address
458 if( Iface->State != STATE_DISCOVER_SENT ) {
459 _SysDebug("Ignoring DHCPOFFER when not in STATE_DISCOVER_SENT");
462 Send_DHCPREQUEST(Iface, data, dhcp_msg_type_ofs);
464 case 3: // DHCPREQUEST - wut?
466 case 4: // DHCPDECLINE - ?
469 SetAddress(Iface, &msg->yiaddr, subnet_mask, router);
470 // Return 1 to remove from list
476 int Handle_Timeout(tInterface *Iface)
478 if( Iface->nTimeouts == 3 )
482 case STATE_DISCOVER_SENT:
483 fprintf(stderr, "DHCPDISCOVER timeout on %s, trying again\n", Iface->Adapter);
484 Send_DHCPDISCOVER(Iface);
486 case STATE_REQUEST_SENT:
487 fprintf(stderr, "DHCPREQUEST timeout on %s\n", Iface->Adapter);
490 _SysDebug("Timeout with state = %i (%s)", Iface->State, Iface->Adapter);
496 void Update_State(tInterface *Iface, int newState)
498 if( Iface->State != newState )
500 Iface->Timeout = _SysTimestamp() + 500;
501 Iface->State = newState;
502 Iface->nTimeouts = 0;
506 // TODO: Exponential backoff
507 Iface->Timeout = _SysTimestamp() + 3000;
509 _SysDebug("%s: State %i repeated, timeout is 3000ms now",
515 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router)
519 // Translate the mask
522 uint8_t *mask = Mask;
524 _SysDebug("Mask %i.%i.%i.%i", mask[0], mask[1], mask[2], mask[3]);
525 for( i = 0; i < 4 && mask[i] == 0xFF; i ++ ) ;
535 case 0x00: mask_bits += 0; break;
536 case 0x80: mask_bits += 1; break;
537 case 0xC0: mask_bits += 2; break;
538 case 0xE0: mask_bits += 3; break;
539 case 0xF0: mask_bits += 4; break;
540 case 0xF8: mask_bits += 5; break;
541 case 0xFC: mask_bits += 6; break;
542 case 0xFE: mask_bits += 7; break;
551 uint8_t *addr = Addr;
552 _SysDebug("Addr %i.%i.%i.%i/%i", addr[0], addr[1], addr[2], addr[3], mask_bits);
554 printf("Assigned %i.%i.%i.%i/%i to IF#%i (%s)\n",
555 addr[0], addr[1], addr[2], addr[3], mask_bits,
556 Iface->Num, Iface->Adapter
560 _SysIOCtl(Iface->IfaceFD, 6, Addr);
561 _SysIOCtl(Iface->IfaceFD, 7, &mask_bits);
565 uint8_t *addr = Router;
566 _SysDebug("Router %i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
570 fd = _SysOpen("/Devices/ip/routes/4:00000000:0:0", OPENFLAG_CREATE);
572 fprintf(stderr, "ERROR: Unable to open default route\n");
575 char ifname[snprintf(NULL,0,"%i",Iface->Num)+1];
576 sprintf(ifname, "%i", Iface->Num);
577 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_nexthop"), Router);
578 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_interface"), ifname);