Changes in uspace/srv/net/il/ip/ip.c [fd8e8e1:ccca251] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/il/ip/ip.c
rfd8e8e1 rccca251 36 36 */ 37 37 38 #include "ip.h"39 #include "ip_module.h"40 41 38 #include <async.h> 42 39 #include <errno.h> 43 #include <err.h>44 40 #include <fibril_synch.h> 45 41 #include <stdio.h> 46 42 #include <str.h> 47 #include <ipc/ipc.h>48 43 #include <ipc/services.h> 49 44 #include <ipc/net.h> … … 53 48 #include <sys/types.h> 54 49 #include <byteorder.h> 50 #include "ip.h" 55 51 56 52 #include <adt/measured_strings.h> … … 70 66 #include <net_checksum.h> 71 67 #include <icmp_client.h> 72 #include <icmp_interface.h> 73 #include <il_interface.h> 68 #include <icmp_remote.h> 74 69 #include <ip_client.h> 75 70 #include <ip_interface.h> 76 71 #include <ip_header.h> 77 72 #include <net_interface.h> 78 #include <nil_ interface.h>79 #include <tl_ interface.h>73 #include <nil_remote.h> 74 #include <tl_remote.h> 80 75 #include <packet_remote.h> 81 #include <il_local.h> 76 #include <il_remote.h> 77 #include <il_skel.h> 82 78 83 79 /** IP module name. */ … … 124 120 GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t); 125 121 122 static void ip_receiver(ipc_callid_t, ipc_call_t *); 123 126 124 /** Releases the packet and returns the result. 127 125 * … … 130 128 * @return The result parameter. 131 129 */ 132 static int ip_release_and_return(packet_t packet, int result)130 static int ip_release_and_return(packet_t *packet, int result) 133 131 { 134 132 pq_release_remote(ip_globals.net_phone, packet_get_id(packet)); … … 140 138 * Searches the registered protocols. 141 139 * 142 * @return sThe found ICMP phone.143 * @return sENOENT if the ICMP is not registered.140 * @return The found ICMP phone. 141 * @return ENOENT if the ICMP is not registered. 144 142 */ 145 143 static int ip_get_icmp_phone(void) 146 144 { 147 ip_proto_ refproto;145 ip_proto_t *proto; 148 146 int phone; 149 147 … … 161 159 * @param[in] packet The packet or the packet queue to be reported as faulty. 162 160 * @param[in] header The first packet IP header. May be NULL. 163 * @return sEOK on success.164 * @return sEINVAL if there are no data in the packet.165 * @return sEINVAL if the packet is a fragment.166 * @return sENOMEM if the packet is too short to contain the IP161 * @return EOK on success. 162 * @return EINVAL if there are no data in the packet. 163 * @return EINVAL if the packet is a fragment. 164 * @return ENOMEM if the packet is too short to contain the IP 167 165 * header. 168 * @return sEAFNOSUPPORT if the address family is not supported.169 * @return sEPERM if the protocol is not allowed to send ICMP166 * @return EAFNOSUPPORT if the address family is not supported. 167 * @return EPERM if the protocol is not allowed to send ICMP 170 168 * notifications. The ICMP protocol itself. 171 * @return sOther error codes as defined for the packet_set_addr().172 */ 173 static int ip_prepare_icmp(packet_t packet, ip_header_refheader)174 { 175 packet_t next;169 * @return Other error codes as defined for the packet_set_addr(). 170 */ 171 static int ip_prepare_icmp(packet_t *packet, ip_header_t *header) 172 { 173 packet_t *next; 176 174 struct sockaddr *dest; 177 175 struct sockaddr_in dest_in; 178 176 socklen_t addrlen; 179 177 180 / / detach the first packet and release the others178 /* Detach the first packet and release the others */ 181 179 next = pq_detach(packet); 182 180 if (next) … … 187 185 return ENOMEM; 188 186 189 / / get header190 header = (ip_header_ ref) packet_get_data(packet);187 /* Get header */ 188 header = (ip_header_t *) packet_get_data(packet); 191 189 if (!header) 192 190 return EINVAL; … … 194 192 } 195 193 196 / / only for the first fragment194 /* Only for the first fragment */ 197 195 if (IP_FRAGMENT_OFFSET(header)) 198 196 return EINVAL; 199 197 200 / / not for the ICMP protocol198 /* Not for the ICMP protocol */ 201 199 if (header->protocol == IPPROTO_ICMP) 202 200 return EPERM; 203 201 204 / / set the destination address202 /* Set the destination address */ 205 203 switch (header->version) { 206 204 case IPVERSION: … … 228 226 * @param[in] packet The packet or the packet queue to be reported as faulty. 229 227 * @param[in] header The first packet IP header. May be NULL. 230 * @return sThe found ICMP phone.231 * @return sEINVAL if the error parameter is set.232 * @return sEINVAL if the ICMP phone is not found.233 * @return sEINVAL if the ip_prepare_icmp() fails.228 * @return The found ICMP phone. 229 * @return EINVAL if the error parameter is set. 230 * @return EINVAL if the ICMP phone is not found. 231 * @return EINVAL if the ip_prepare_icmp() fails. 234 232 */ 235 233 static int 236 ip_prepare_icmp_and_get_phone(services_t error, packet_t packet,237 ip_header_ refheader)234 ip_prepare_icmp_and_get_phone(services_t error, packet_t *packet, 235 ip_header_t *header) 238 236 { 239 237 int phone; … … 245 243 } 246 244 247 /** Initializes the IP module. 248 * 249 * @param[in] client_connection The client connection processing function. The 250 * module skeleton propagates its own one. 251 * @returns EOK on success. 252 * @returns ENOMEM if there is not enough memory left. 253 */ 254 int ip_initialize(async_client_conn_t client_connection) 255 { 256 ERROR_DECLARE; 257 245 int il_initialize(int net_phone) 246 { 258 247 fibril_rwlock_initialize(&ip_globals.lock); 259 248 fibril_rwlock_write_lock(&ip_globals.lock); 260 249 fibril_rwlock_initialize(&ip_globals.protos_lock); 261 250 fibril_rwlock_initialize(&ip_globals.netifs_lock); 251 252 ip_globals.net_phone = net_phone; 262 253 ip_globals.packet_counter = 0; 263 254 ip_globals.gateway.address.s_addr = 0; … … 265 256 ip_globals.gateway.gateway.s_addr = 0; 266 257 ip_globals.gateway.netif = NULL; 267 ERROR_PROPAGATE(ip_netifs_initialize(&ip_globals.netifs)); 268 ERROR_PROPAGATE(ip_protos_initialize(&ip_globals.protos)); 269 ip_globals.client_connection = client_connection; 270 ERROR_PROPAGATE(modules_initialize(&ip_globals.modules)); 271 ERROR_PROPAGATE(add_module(NULL, &ip_globals.modules, ARP_NAME, 272 ARP_FILENAME, SERVICE_ARP, 0, arp_connect_module)); 258 259 int rc = ip_netifs_initialize(&ip_globals.netifs); 260 if (rc != EOK) 261 goto out; 262 rc = ip_protos_initialize(&ip_globals.protos); 263 if (rc != EOK) 264 goto out; 265 rc = modules_initialize(&ip_globals.modules); 266 if (rc != EOK) 267 goto out; 268 rc = add_module(NULL, &ip_globals.modules, (uint8_t *) ARP_NAME, 269 (uint8_t *) ARP_FILENAME, SERVICE_ARP, 0, arp_connect_module); 270 271 out: 273 272 fibril_rwlock_write_unlock(&ip_globals.lock); 274 273 275 return EOK;274 return rc; 276 275 } 277 276 … … 285 284 * 286 285 * @param[in,out] ip_netif Network interface specific data. 287 * @return sEOK on success.288 * @return sENOTSUP if DHCP is configured.289 * @return sENOTSUP if IPv6 is configured.290 * @return sEINVAL if any of the addresses is invalid.291 * @return sEINVAL if the used ARP module is not known.292 * @return sENOMEM if there is not enough memory left.293 * @return sOther error codes as defined for the286 * @return EOK on success. 287 * @return ENOTSUP if DHCP is configured. 288 * @return ENOTSUP if IPv6 is configured. 289 * @return EINVAL if any of the addresses is invalid. 290 * @return EINVAL if the used ARP module is not known. 291 * @return ENOMEM if there is not enough memory left. 292 * @return Other error codes as defined for the 294 293 * net_get_device_conf_req() function. 295 * @return sOther error codes as defined for the bind_service()294 * @return Other error codes as defined for the bind_service() 296 295 * function. 297 * @return sOther error codes as defined for the specific296 * @return Other error codes as defined for the specific 298 297 * arp_device_req() function. 299 * @return sOther error codes as defined for the298 * @return Other error codes as defined for the 300 299 * nil_packet_size_req() function. 301 300 */ 302 static int ip_netif_initialize(ip_netif_ref ip_netif) 303 { 304 ERROR_DECLARE; 305 301 static int ip_netif_initialize(ip_netif_t *ip_netif) 302 { 306 303 measured_string_t names[] = { 307 304 { 308 ( char*) "IPV",305 (uint8_t *) "IPV", 309 306 3 310 307 }, 311 308 { 312 ( char*) "IP_CONFIG",309 (uint8_t *) "IP_CONFIG", 313 310 9 314 311 }, 315 312 { 316 ( char*) "IP_ADDR",313 (uint8_t *) "IP_ADDR", 317 314 7 318 315 }, 319 316 { 320 ( char*) "IP_NETMASK",317 (uint8_t *) "IP_NETMASK", 321 318 10 322 319 }, 323 320 { 324 ( char*) "IP_GATEWAY",321 (uint8_t *) "IP_GATEWAY", 325 322 10 326 323 }, 327 324 { 328 ( char*) "IP_BROADCAST",325 (uint8_t *) "IP_BROADCAST", 329 326 12 330 327 }, 331 328 { 332 ( char*) "ARP",329 (uint8_t *) "ARP", 333 330 3 334 331 }, 335 332 { 336 ( char*) "IP_ROUTING",333 (uint8_t *) "IP_ROUTING", 337 334 10 338 335 } 339 336 }; 340 measured_string_ refconfiguration;337 measured_string_t *configuration; 341 338 size_t count = sizeof(names) / sizeof(measured_string_t); 342 char*data;339 uint8_t *data; 343 340 measured_string_t address; 341 ip_route_t *route; 342 in_addr_t gateway; 344 343 int index; 345 ip_route_ref route; 346 in_addr_t gateway; 344 int rc; 347 345 348 346 ip_netif->arp = NULL; … … 353 351 configuration = &names[0]; 354 352 355 // get configuration 356 ERROR_PROPAGATE(net_get_device_conf_req(ip_globals.net_phone, 357 ip_netif->device_id, &configuration, count, &data)); 353 /* Get configuration */ 354 rc = net_get_device_conf_req(ip_globals.net_phone, ip_netif->device_id, 355 &configuration, count, &data); 356 if (rc != EOK) 357 return rc; 358 358 359 if (configuration) { 359 360 if (configuration[0].value) 360 ip_netif->ipv = strtol( configuration[0].value, NULL, 0);361 362 ip_netif->dhcp = !str_lcmp( configuration[1].value, "dhcp",361 ip_netif->ipv = strtol((char *) configuration[0].value, NULL, 0); 362 363 ip_netif->dhcp = !str_lcmp((char *) configuration[1].value, "dhcp", 363 364 configuration[1].length); 364 365 … … 368 369 return ENOTSUP; 369 370 } else if (ip_netif->ipv == IPV4) { 370 route = (ip_route_ ref) malloc(sizeof(ip_route_t));371 route = (ip_route_t *) malloc(sizeof(ip_route_t)); 371 372 if (!route) { 372 373 net_free_settings(configuration, data); … … 383 384 return index; 384 385 } 385 if (ERROR_OCCURRED(inet_pton(AF_INET, 386 configuration[2].value, 387 (uint8_t *) &route->address.s_addr)) || 388 ERROR_OCCURRED(inet_pton(AF_INET, 389 configuration[3].value, 390 (uint8_t *) &route->netmask.s_addr)) || 391 (inet_pton(AF_INET, configuration[4].value, 386 387 if ((inet_pton(AF_INET, (char *) configuration[2].value, 388 (uint8_t *) &route->address.s_addr) != EOK) || 389 (inet_pton(AF_INET, (char *) configuration[3].value, 390 (uint8_t *) &route->netmask.s_addr) != EOK) || 391 (inet_pton(AF_INET, (char *) configuration[4].value, 392 392 (uint8_t *) &gateway.s_addr) == EINVAL) || 393 (inet_pton(AF_INET, configuration[5].value,393 (inet_pton(AF_INET, (char *) configuration[5].value, 394 394 (uint8_t *) &ip_netif->broadcast.s_addr) == EINVAL)) 395 395 { … … 419 419 } 420 420 421 / / binds the netif service which also initializes the device421 /* Bind netif service which also initializes the device */ 422 422 ip_netif->phone = nil_bind_service(ip_netif->service, 423 ( ipcarg_t) ip_netif->device_id, SERVICE_IP,424 ip_ globals.client_connection);423 (sysarg_t) ip_netif->device_id, SERVICE_IP, 424 ip_receiver); 425 425 if (ip_netif->phone < 0) { 426 426 printf("Failed to contact the nil service %d\n", … … 429 429 } 430 430 431 / / has to be after the device netif module initialization431 /* Has to be after the device netif module initialization */ 432 432 if (ip_netif->arp) { 433 433 if (route) { 434 address.value = (char *) &route->address.s_addr; 435 address.length = CONVERT_SIZE(in_addr_t, char, 1); 436 ERROR_PROPAGATE(arp_device_req(ip_netif->arp->phone, 434 address.value = (uint8_t *) &route->address.s_addr; 435 address.length = sizeof(in_addr_t); 436 437 rc = arp_device_req(ip_netif->arp->phone, 437 438 ip_netif->device_id, SERVICE_IP, ip_netif->service, 438 &address)); 439 &address); 440 if (rc != EOK) 441 return rc; 439 442 } else { 440 443 ip_netif->arp = 0; … … 442 445 } 443 446 444 // get packet dimensions 445 ERROR_PROPAGATE(nil_packet_size_req(ip_netif->phone, 446 ip_netif->device_id, &ip_netif->packet_dimension)); 447 /* Get packet dimensions */ 448 rc = nil_packet_size_req(ip_netif->phone, ip_netif->device_id, 449 &ip_netif->packet_dimension); 450 if (rc != EOK) 451 return rc; 452 447 453 if (ip_netif->packet_dimension.content < IP_MIN_CONTENT) { 448 printf("Maximum transmission unit % dbytes is too small, at "454 printf("Maximum transmission unit %zu bytes is too small, at " 449 455 "least %d bytes are needed\n", 450 456 ip_netif->packet_dimension.content, IP_MIN_CONTENT); … … 457 463 458 464 if (gateway.s_addr) { 459 / / the default gateway465 /* The default gateway */ 460 466 ip_globals.gateway.address.s_addr = 0; 461 467 ip_globals.gateway.netmask.s_addr = 0; 462 468 ip_globals.gateway.gateway.s_addr = gateway.s_addr; 463 469 ip_globals.gateway.netif = ip_netif; 470 471 char defgateway[INET_ADDRSTRLEN]; 472 inet_ntop(AF_INET, (uint8_t *) &gateway.s_addr, 473 defgateway, INET_ADDRSTRLEN); 474 printf("%s: Default gateway (%s)\n", NAME, defgateway); 464 475 } 465 476 … … 467 478 } 468 479 469 /** Updates the device content length according to the new MTU value. 470 * 471 * @param[in] device_id The device identifier. 472 * @param[in] mtu The new mtu value. 473 * @returns EOK on success. 474 * @returns ENOENT if device is not found. 475 */ 476 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu) 477 { 478 ip_netif_ref netif; 480 static int ip_device_req_local(int il_phone, device_id_t device_id, 481 services_t netif) 482 { 483 ip_netif_t *ip_netif; 484 ip_route_t *route; 485 int index; 486 int rc; 487 488 ip_netif = (ip_netif_t *) malloc(sizeof(ip_netif_t)); 489 if (!ip_netif) 490 return ENOMEM; 491 492 rc = ip_routes_initialize(&ip_netif->routes); 493 if (rc != EOK) { 494 free(ip_netif); 495 return rc; 496 } 497 498 ip_netif->device_id = device_id; 499 ip_netif->service = netif; 500 ip_netif->state = NETIF_STOPPED; 479 501 480 502 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 481 netif = ip_netifs_find(&ip_globals.netifs, device_id); 482 if (!netif) { 503 504 rc = ip_netif_initialize(ip_netif); 505 if (rc != EOK) { 483 506 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 484 return ENOENT; 485 } 486 netif->packet_dimension.content = mtu; 507 ip_routes_destroy(&ip_netif->routes, free); 508 free(ip_netif); 509 return rc; 510 } 511 if (ip_netif->arp) 512 ip_netif->arp->usage++; 513 514 /* Print the settings */ 515 printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n", 516 NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv, 517 ip_netif->dhcp ? "dhcp" : "static"); 518 519 // TODO ipv6 addresses 520 521 char address[INET_ADDRSTRLEN]; 522 char netmask[INET_ADDRSTRLEN]; 523 char gateway[INET_ADDRSTRLEN]; 524 525 for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) { 526 route = ip_routes_get_index(&ip_netif->routes, index); 527 if (route) { 528 inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, 529 address, INET_ADDRSTRLEN); 530 inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, 531 netmask, INET_ADDRSTRLEN); 532 inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, 533 gateway, INET_ADDRSTRLEN); 534 printf("%s: Route %d (address: %s, netmask: %s, " 535 "gateway: %s)\n", NAME, index, address, netmask, 536 gateway); 537 } 538 } 539 540 inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address, 541 INET_ADDRSTRLEN); 487 542 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 488 543 489 printf("%s: Device %d changed MTU to %d\n", NAME, device_id, mtu);544 printf("%s: Broadcast (%s)\n", NAME, address); 490 545 491 546 return EOK; 492 547 } 493 548 494 /** Updates the device state. 495 * 496 * @param[in] device_id The device identifier. 497 * @param[in] state The new state value. 498 * @returns EOK on success. 499 * @returns ENOENT if device is not found. 500 */ 501 static int ip_device_state_message(device_id_t device_id, device_state_t state) 502 { 503 ip_netif_ref netif; 504 505 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 506 // find the device 507 netif = ip_netifs_find(&ip_globals.netifs, device_id); 508 if (!netif) { 509 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 510 return ENOENT; 511 } 512 netif->state = state; 513 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 514 515 printf("%s: Device %d changed state to %d\n", NAME, device_id, state); 516 517 return EOK; 518 } 519 520 521 /** Prefixes a middle fragment header based on the last fragment header to the 522 * packet. 523 * 524 * @param[in] packet The packet to be prefixed. 525 * @param[in] last The last header to be copied. 526 * @returns The prefixed middle header. 527 * @returns NULL on error. 528 */ 529 static ip_header_ref 530 ip_create_middle_header(packet_t packet, ip_header_ref last) 531 { 532 ip_header_ref middle; 533 534 middle = (ip_header_ref) packet_suffix(packet, IP_HEADER_LENGTH(last)); 535 if (!middle) 549 /** Searches the network interfaces if there is a suitable route. 550 * 551 * @param[in] netif The network interface to be searched for routes. May be 552 * NULL. 553 * @param[in] destination The destination address. 554 * @return The found route. 555 * @return NULL if no route was found. 556 */ 557 static ip_route_t *ip_netif_find_route(ip_netif_t *netif, 558 in_addr_t destination) 559 { 560 int index; 561 ip_route_t *route; 562 563 if (!netif) 536 564 return NULL; 537 memcpy(middle, last, IP_HEADER_LENGTH(last)); 538 middle->flags |= IPFLAG_MORE_FRAGMENTS; 539 return middle; 565 566 /* Start with the first one (the direct route) */ 567 for (index = 0; index < ip_routes_count(&netif->routes); index++) { 568 route = ip_routes_get_index(&netif->routes, index); 569 if ((route) && 570 ((route->address.s_addr & route->netmask.s_addr) == 571 (destination.s_addr & route->netmask.s_addr))) 572 return route; 573 } 574 575 return NULL; 576 } 577 578 /** Searches all network interfaces if there is a suitable route. 579 * 580 * @param[in] destination The destination address. 581 * @return The found route. 582 * @return NULL if no route was found. 583 */ 584 static ip_route_t *ip_find_route(in_addr_t destination) { 585 int index; 586 ip_route_t *route; 587 ip_netif_t *netif; 588 589 /* Start with the last netif - the newest one */ 590 index = ip_netifs_count(&ip_globals.netifs) - 1; 591 while (index >= 0) { 592 netif = ip_netifs_get_index(&ip_globals.netifs, index); 593 if (netif && (netif->state == NETIF_ACTIVE)) { 594 route = ip_netif_find_route(netif, destination); 595 if (route) 596 return route; 597 } 598 index--; 599 } 600 601 return &ip_globals.gateway; 602 } 603 604 /** Returns the network interface's IP address. 605 * 606 * @param[in] netif The network interface. 607 * @return The IP address. 608 * @return NULL if no IP address was found. 609 */ 610 static in_addr_t *ip_netif_address(ip_netif_t *netif) 611 { 612 ip_route_t *route; 613 614 route = ip_routes_get_index(&netif->routes, 0); 615 return route ? &route->address : NULL; 540 616 } 541 617 … … 547 623 * @param[in] first The original header to be copied. 548 624 */ 549 static void ip_create_last_header(ip_header_ ref last, ip_header_reffirst)550 { 551 ip_option_ refoption;625 static void ip_create_last_header(ip_header_t *last, ip_header_t *first) 626 { 627 ip_option_t *option; 552 628 size_t next; 553 629 size_t length; 554 630 555 / / copy first itself631 /* Copy first itself */ 556 632 memcpy(last, first, sizeof(ip_header_t)); 557 633 length = sizeof(ip_header_t); 558 634 next = sizeof(ip_header_t); 559 635 560 / / process all ip options636 /* Process all IP options */ 561 637 while (next < first->header_length) { 562 option = (ip_option_ ref) (((uint8_t *) first) + next);563 / / skip end or noop638 option = (ip_option_t *) (((uint8_t *) first) + next); 639 /* Skip end or noop */ 564 640 if ((option->type == IPOPT_END) || 565 641 (option->type == IPOPT_NOOP)) { 566 642 next++; 567 643 } else { 568 / / copy if told so or skip644 /* Copy if told so or skip */ 569 645 if (IPOPT_COPIED(option->type)) { 570 646 memcpy(((uint8_t *) last) + length, … … 572 648 length += option->length; 573 649 } 574 / / next option650 /* Next option */ 575 651 next += option->length; 576 652 } 577 653 } 578 654 579 / / align 4 byte boundary655 /* Align 4 byte boundary */ 580 656 if (length % 4) { 581 657 bzero(((uint8_t *) last) + length, 4 - (length % 4)); … … 598 674 * @param[in,out] packet The packet to be sent. 599 675 * @param[in] destination The destination hardware address. 600 * @return sEOK on success.601 * @return sEINVAL if the packet is too small to contain the IP676 * @return EOK on success. 677 * @return EINVAL if the packet is too small to contain the IP 602 678 * header. 603 * @return sEINVAL if the packet is too long than the IP allows.604 * @return sENOMEM if there is not enough memory left.605 * @return sOther error codes as defined for the packet_set_addr()679 * @return EINVAL if the packet is too long than the IP allows. 680 * @return ENOMEM if there is not enough memory left. 681 * @return Other error codes as defined for the packet_set_addr() 606 682 * function. 607 683 */ 608 static int 609 ip_prepare_packet(in_addr_t *source, in_addr_t dest, packet_t packet, 610 measured_string_ref destination) 611 { 612 ERROR_DECLARE; 613 684 static int ip_prepare_packet(in_addr_t *source, in_addr_t dest, 685 packet_t *packet, measured_string_t *destination) 686 { 614 687 size_t length; 615 ip_header_ref header; 616 ip_header_ref last_header; 617 ip_header_ref middle_header; 618 packet_t next; 688 ip_header_t *header; 689 ip_header_t *last_header; 690 ip_header_t *middle_header; 691 packet_t *next; 692 int rc; 619 693 620 694 length = packet_get_data_length(packet); … … 622 696 return EINVAL; 623 697 624 header = (ip_header_ ref) packet_get_data(packet);698 header = (ip_header_t *) packet_get_data(packet); 625 699 if (destination) { 626 ERROR_PROPAGATE(packet_set_addr(packet, NULL, 627 (uint8_t *) destination->value, 628 CONVERT_SIZE(char, uint8_t, destination->length))); 700 rc = packet_set_addr(packet, NULL, (uint8_t *) destination->value, 701 destination->length); 629 702 } else { 630 ERROR_PROPAGATE(packet_set_addr(packet, NULL, NULL, 0)); 631 } 703 rc = packet_set_addr(packet, NULL, NULL, 0); 704 } 705 if (rc != EOK) 706 return rc; 707 632 708 header->version = IPV4; 633 709 header->fragment_offset_high = 0; … … 644 720 645 721 if (pq_next(packet)) { 646 last_header = (ip_header_ ref) malloc(IP_HEADER_LENGTH(header));722 last_header = (ip_header_t *) malloc(IP_HEADER_LENGTH(header)); 647 723 if (!last_header) 648 724 return ENOMEM; … … 650 726 next = pq_next(packet); 651 727 while (pq_next(next)) { 652 middle_header = (ip_header_ ref) packet_prefix(next,728 middle_header = (ip_header_t *) packet_prefix(next, 653 729 IP_HEADER_LENGTH(last_header)); 654 730 if (!middle_header) { … … 669 745 IP_HEADER_CHECKSUM(middle_header); 670 746 if (destination) { 671 if (ERROR_OCCURRED(packet_set_addr(next, NULL,747 rc = packet_set_addr(next, NULL, 672 748 (uint8_t *) destination->value, 673 CONVERT_SIZE(char, uint8_t,674 destination->length)))) {749 destination->length); 750 if (rc != EOK) { 675 751 free(last_header); 676 return ERROR_CODE;752 return rc; 677 753 } 678 754 } … … 681 757 } 682 758 683 middle_header = (ip_header_ ref) packet_prefix(next,759 middle_header = (ip_header_t *) packet_prefix(next, 684 760 IP_HEADER_LENGTH(last_header)); 685 761 if (!middle_header) { … … 699 775 IP_HEADER_CHECKSUM(middle_header); 700 776 if (destination) { 701 if (ERROR_OCCURRED(packet_set_addr(next, NULL,777 rc = packet_set_addr(next, NULL, 702 778 (uint8_t *) destination->value, 703 CONVERT_SIZE(char, uint8_t,704 destination->length)))) {779 destination->length); 780 if (rc != EOK) { 705 781 free(last_header); 706 return ERROR_CODE;707 782 return rc; 783 } 708 784 } 709 785 length += packet_get_data_length(next); … … 713 789 714 790 header->total_length = htons(length); 715 / / unnecessary for all protocols791 /* Unnecessary for all protocols */ 716 792 header->header_checksum = IP_HEADER_CHECKSUM(header); 717 793 … … 729 805 * @param[in] dest The destiantion address. 730 806 * @param[in] addrlen The address length. 731 * @return sEOK on success.732 * @return sENOMEM if the target packet is too small.733 * @return sOther error codes as defined for the packet_set_addr()807 * @return EOK on success. 808 * @return ENOMEM if the target packet is too small. 809 * @return Other error codes as defined for the packet_set_addr() 734 810 * function. 735 * @return sOther error codes as defined for the pq_insert_after()811 * @return Other error codes as defined for the pq_insert_after() 736 812 * function. 737 813 */ 738 static int 739 ip_fragment_packet_data(packet_t packet, packet_t new_packet, 740 ip_header_ref header, ip_header_ref new_header, size_t length, 814 static int ip_fragment_packet_data(packet_t *packet, packet_t *new_packet, 815 ip_header_t *header, ip_header_t *new_header, size_t length, 741 816 const struct sockaddr *src, const struct sockaddr *dest, socklen_t addrlen) 742 817 { 743 ERROR_DECLARE;744 745 818 void *data; 746 819 size_t offset; 820 int rc; 747 821 748 822 data = packet_suffix(new_packet, length); … … 752 826 memcpy(data, ((void *) header) + IP_TOTAL_LENGTH(header) - length, 753 827 length); 754 ERROR_PROPAGATE(packet_trim(packet, 0, length)); 828 829 rc = packet_trim(packet, 0, length); 830 if (rc != EOK) 831 return rc; 832 755 833 header->total_length = htons(IP_TOTAL_LENGTH(header) - length); 756 834 new_header->total_length = htons(IP_HEADER_LENGTH(new_header) + length); … … 761 839 IP_COMPUTE_FRAGMENT_OFFSET_LOW(offset); 762 840 new_header->header_checksum = IP_HEADER_CHECKSUM(new_header); 763 ERROR_PROPAGATE(packet_set_addr(new_packet, (const uint8_t *) src, 764 (const uint8_t *) dest, addrlen)); 841 842 rc = packet_set_addr(new_packet, (const uint8_t *) src, 843 (const uint8_t *) dest, addrlen); 844 if (rc != EOK) 845 return rc; 765 846 766 847 return pq_insert_after(packet, new_packet); 848 } 849 850 /** Prefixes a middle fragment header based on the last fragment header to the 851 * packet. 852 * 853 * @param[in] packet The packet to be prefixed. 854 * @param[in] last The last header to be copied. 855 * @return The prefixed middle header. 856 * @return NULL on error. 857 */ 858 static ip_header_t *ip_create_middle_header(packet_t *packet, 859 ip_header_t *last) 860 { 861 ip_header_t *middle; 862 863 middle = (ip_header_t *) packet_suffix(packet, IP_HEADER_LENGTH(last)); 864 if (!middle) 865 return NULL; 866 memcpy(middle, last, IP_HEADER_LENGTH(last)); 867 middle->flags |= IPFLAG_MORE_FRAGMENTS; 868 return middle; 767 869 } 768 870 … … 776 878 * @param[in] suffix The minimum suffix size. 777 879 * @param[in] addr_len The minimum address length. 778 * @return sEOK on success.779 * @return sEINVAL if the packet_get_addr() function fails.780 * @return sEINVAL if the packet does not contain the IP header.781 * @return sEPERM if the packet needs to be fragmented and the880 * @return EOK on success. 881 * @return EINVAL if the packet_get_addr() function fails. 882 * @return EINVAL if the packet does not contain the IP header. 883 * @return EPERM if the packet needs to be fragmented and the 782 884 * fragmentation is not allowed. 783 * @return sENOMEM if there is not enough memory left.784 * @return sENOMEM if there is no packet available.785 * @return sENOMEM if the packet is too small to contain the IP885 * @return ENOMEM if there is not enough memory left. 886 * @return ENOMEM if there is no packet available. 887 * @return ENOMEM if the packet is too small to contain the IP 786 888 * header. 787 * @return sOther error codes as defined for the packet_trim()889 * @return Other error codes as defined for the packet_trim() 788 890 * function. 789 * @return sOther error codes as defined for the891 * @return Other error codes as defined for the 790 892 * ip_create_middle_header() function. 791 * @return sOther error codes as defined for the893 * @return Other error codes as defined for the 792 894 * ip_fragment_packet_data() function. 793 895 */ 794 896 static int 795 ip_fragment_packet(packet_t packet, size_t length, size_t prefix, size_t suffix,897 ip_fragment_packet(packet_t *packet, size_t length, size_t prefix, size_t suffix, 796 898 socklen_t addr_len) 797 899 { 798 ERROR_DECLARE; 799 800 packet_t new_packet; 801 ip_header_ref header; 802 ip_header_ref middle_header; 803 ip_header_ref last_header; 900 packet_t *new_packet; 901 ip_header_t *header; 902 ip_header_t *middle_header; 903 ip_header_t *last_header; 804 904 struct sockaddr *src; 805 905 struct sockaddr *dest; 806 906 socklen_t addrlen; 807 907 int result; 908 int rc; 808 909 809 910 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest); … … 815 916 return ENOMEM; 816 917 817 / / get header818 header = (ip_header_ ref) packet_get_data(packet);918 /* Get header */ 919 header = (ip_header_t *) packet_get_data(packet); 819 920 if (!header) 820 921 return EINVAL; 821 922 822 / / fragmentation forbidden?923 /* Fragmentation forbidden? */ 823 924 if(header->flags & IPFLAG_DONT_FRAGMENT) 824 925 return EPERM; 825 926 826 / / create the last fragment927 /* Create the last fragment */ 827 928 new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, length, 828 929 suffix, ((addrlen > addr_len) ? addrlen : addr_len)); … … 830 931 return ENOMEM; 831 932 832 / / allocate as much as originally833 last_header = (ip_header_ ref) packet_suffix(new_packet,933 /* Allocate as much as originally */ 934 last_header = (ip_header_t *) packet_suffix(new_packet, 834 935 IP_HEADER_LENGTH(header)); 835 936 if (!last_header) … … 838 939 ip_create_last_header(last_header, header); 839 940 840 / / trim the unused space841 if (ERROR_OCCURRED(packet_trim(new_packet, 0,842 IP_HEADER_LENGTH(header) - IP_HEADER_LENGTH(last_header)) )) {843 return ip_release_and_return(packet, ERROR_CODE);844 }845 846 / / biggest multiple of 8 lower than content941 /* Trim the unused space */ 942 rc = packet_trim(new_packet, 0, 943 IP_HEADER_LENGTH(header) - IP_HEADER_LENGTH(last_header)); 944 if (rc != EOK) 945 return ip_release_and_return(packet, rc); 946 947 /* Greatest multiple of 8 lower than content */ 847 948 // TODO even fragmentation? 848 949 length = length & ~0x7; 849 if (ERROR_OCCURRED(ip_fragment_packet_data(packet, new_packet, header,850 950 951 rc = ip_fragment_packet_data(packet, new_packet, header, last_header, 851 952 ((IP_HEADER_DATA_LENGTH(header) - 852 953 ((length - IP_HEADER_LENGTH(header)) & ~0x7)) % 853 ((length - IP_HEADER_LENGTH(last_header)) & ~0x7)), src, dest,854 addrlen))) {855 return ip_release_and_return(packet, ERROR_CODE);856 }857 858 / / mark the first as fragmented954 ((length - IP_HEADER_LENGTH(last_header)) & ~0x7)), 955 src, dest, addrlen); 956 if (rc != EOK) 957 return ip_release_and_return(packet, rc); 958 959 /* Mark the first as fragmented */ 859 960 header->flags |= IPFLAG_MORE_FRAGMENTS; 860 961 861 / / create middle framgents962 /* Create middle fragments */ 862 963 while (IP_TOTAL_LENGTH(header) > length) { 863 964 new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, … … 872 973 return ip_release_and_return(packet, ENOMEM); 873 974 874 if (ERROR_OCCURRED(ip_fragment_packet_data(packet, new_packet,875 header,middle_header,876 (length - IP_HEADER_LENGTH(middle_header)) & ~0x7, src,877 dest, addrlen))) {878 return ip_release_and_return(packet, ERROR_CODE);879 }880 } 881 882 / / finish the first fragment975 rc = ip_fragment_packet_data(packet, new_packet, header, 976 middle_header, 977 (length - IP_HEADER_LENGTH(middle_header)) & ~0x7, 978 src, dest, addrlen); 979 if (rc != EOK) 980 return ip_release_and_return(packet, rc); 981 } 982 983 /* Finish the first fragment */ 883 984 header->header_checksum = IP_HEADER_CHECKSUM(header); 884 985 … … 897 998 * @param[in] addr_len The minimum address length. 898 999 * @param[in] error The error module service. 899 * @return sThe packet or the packet queue of the allowed length.900 * @return sNULL if there are no packets left.901 */ 902 static packet_t 903 ip_split_packet(packet_t packet, size_t prefix, size_t content, size_t suffix,1000 * @return The packet or the packet queue of the allowed length. 1001 * @return NULL if there are no packets left. 1002 */ 1003 static packet_t * 1004 ip_split_packet(packet_t *packet, size_t prefix, size_t content, size_t suffix, 904 1005 socklen_t addr_len, services_t error) 905 1006 { 906 1007 size_t length; 907 packet_t next;908 packet_t new_packet;1008 packet_t *next; 1009 packet_t *new_packet; 909 1010 int result; 910 1011 int phone; 911 1012 912 1013 next = packet; 913 / / check all packets1014 /* Check all packets */ 914 1015 while (next) { 915 1016 length = packet_get_data_length(next); … … 920 1021 } 921 1022 922 / / too long1023 /* Too long */ 923 1024 result = ip_fragment_packet(next, content, prefix, 924 1025 suffix, addr_len); … … 926 1027 new_packet = pq_detach(next); 927 1028 if (next == packet) { 928 / / the new first packet of the queue1029 /* The new first packet of the queue */ 929 1030 packet = new_packet; 930 1031 } 931 / / fragmentation needed?1032 /* Fragmentation needed? */ 932 1033 if (result == EPERM) { 933 1034 phone = ip_prepare_icmp_and_get_phone( 934 1035 error, next, NULL); 935 1036 if (phone >= 0) { 936 / / fragmentation necessary ICMP1037 /* Fragmentation necessary ICMP */ 937 1038 icmp_destination_unreachable_msg(phone, 938 1039 ICMP_FRAG_NEEDED, content, next); … … 964 1065 * @param[in] dest The destination address. 965 1066 * @param[in] error The error module service. 966 * @return sEOK on success.967 * @return sOther error codes as defined for the arp_translate_req()1067 * @return EOK on success. 1068 * @return Other error codes as defined for the arp_translate_req() 968 1069 * function. 969 * @return sOther error codes as defined for the ip_prepare_packet()1070 * @return Other error codes as defined for the ip_prepare_packet() 970 1071 * function. 971 1072 */ 972 static int 973 ip_send_route(packet_t packet, ip_netif_ref netif, ip_route_ref route, 974 in_addr_t *src, in_addr_t dest, services_t error) 975 { 976 ERROR_DECLARE; 977 1073 static int ip_send_route(packet_t *packet, ip_netif_t *netif, 1074 ip_route_t *route, in_addr_t *src, in_addr_t dest, services_t error) 1075 { 978 1076 measured_string_t destination; 979 measured_string_ reftranslation;980 char*data;1077 measured_string_t *translation; 1078 uint8_t *data; 981 1079 int phone; 982 983 // get destination hardware address 1080 int rc; 1081 1082 /* Get destination hardware address */ 984 1083 if (netif->arp && (route->address.s_addr != dest.s_addr)) { 985 1084 destination.value = route->gateway.s_addr ? 986 ( char *) &route->gateway.s_addr : (char*) &dest.s_addr;987 destination.length = CONVERT_SIZE(dest.s_addr, char, 1);988 989 if (ERROR_OCCURRED(arp_translate_req(netif->arp->phone,990 netif->device_id, SERVICE_IP, &destination, &translation,991 &data))) {1085 (uint8_t *) &route->gateway.s_addr : (uint8_t *) &dest.s_addr; 1086 destination.length = sizeof(dest.s_addr); 1087 1088 rc = arp_translate_req(netif->arp->phone, netif->device_id, 1089 SERVICE_IP, &destination, &translation, &data); 1090 if (rc != EOK) { 992 1091 pq_release_remote(ip_globals.net_phone, 993 1092 packet_get_id(packet)); 994 return ERROR_CODE;1093 return rc; 995 1094 } 996 1095 … … 1003 1102 NULL); 1004 1103 if (phone >= 0) { 1005 / / unreachable ICMP if no routing1104 /* Unreachable ICMP if no routing */ 1006 1105 icmp_destination_unreachable_msg(phone, 1007 1106 ICMP_HOST_UNREACH, 0, packet); … … 1014 1113 } 1015 1114 1016 if (ERROR_OCCURRED(ip_prepare_packet(src, dest, packet, translation))) { 1115 rc = ip_prepare_packet(src, dest, packet, translation); 1116 if (rc != EOK) { 1017 1117 pq_release_remote(ip_globals.net_phone, packet_get_id(packet)); 1018 1118 } else { … … 1032 1132 } 1033 1133 1034 return ERROR_CODE; 1035 } 1036 1037 /** Searches the network interfaces if there is a suitable route. 1038 * 1039 * @param[in] netif The network interface to be searched for routes. May be 1040 * NULL. 1041 * @param[in] destination The destination address. 1042 * @returns The found route. 1043 * @returns NULL if no route was found. 1044 */ 1045 static ip_route_ref 1046 ip_netif_find_route(ip_netif_ref netif, in_addr_t destination) 1047 { 1048 int index; 1049 ip_route_ref route; 1050 1051 if (!netif) 1052 return NULL; 1053 1054 // start with the first one - the direct route 1055 for (index = 0; index < ip_routes_count(&netif->routes); index++) { 1056 route = ip_routes_get_index(&netif->routes, index); 1057 if (route && 1058 ((route->address.s_addr & route->netmask.s_addr) == 1059 (destination.s_addr & route->netmask.s_addr))) { 1060 return route; 1061 } 1062 } 1063 1064 return NULL; 1065 } 1066 1067 /** Searches all network interfaces if there is a suitable route. 1068 * 1069 * @param[in] destination The destination address. 1070 * @returns The found route. 1071 * @returns NULL if no route was found. 1072 */ 1073 static ip_route_ref ip_find_route(in_addr_t destination) { 1074 int index; 1075 ip_route_ref route; 1076 ip_netif_ref netif; 1077 1078 // start with the last netif - the newest one 1079 index = ip_netifs_count(&ip_globals.netifs) - 1; 1080 while (index >= 0) { 1081 netif = ip_netifs_get_index(&ip_globals.netifs, index); 1082 if (netif && (netif->state == NETIF_ACTIVE)) { 1083 route = ip_netif_find_route(netif, destination); 1084 if (route) 1085 return route; 1086 } 1087 index--; 1088 } 1089 1090 return &ip_globals.gateway; 1091 } 1092 1093 /** Returns the network interface's IP address. 1094 * 1095 * @param[in] netif The network interface. 1096 * @returns The IP address. 1097 * @returns NULL if no IP address was found. 1098 */ 1099 static in_addr_t *ip_netif_address(ip_netif_ref netif) 1100 { 1101 ip_route_ref route; 1102 1103 route = ip_routes_get_index(&netif->routes, 0); 1104 return route ? &route->address : NULL; 1105 } 1106 1107 /** Registers the transport layer protocol. 1108 * 1109 * The traffic of this protocol will be supplied using either the receive 1110 * function or IPC message. 1111 * 1112 * @param[in] protocol The transport layer module protocol. 1113 * @param[in] service The transport layer module service. 1114 * @param[in] phone The transport layer module phone. 1115 * @param[in] received_msg The receiving function. 1116 * @returns EOK on success. 1117 * @returns EINVAL if the protocol parameter and/or the service 1118 * parameter is zero. 1119 * @returns EINVAL if the phone parameter is not a positive number 1120 * and the tl_receive_msg is NULL. 1121 * @returns ENOMEM if there is not enough memory left. 1122 */ 1123 static int 1124 ip_register(int protocol, services_t service, int phone, 1125 tl_received_msg_t received_msg) 1126 { 1127 ip_proto_ref proto; 1128 int index; 1129 1130 if (!protocol || !service || ((phone < 0) && !received_msg)) 1131 return EINVAL; 1132 1133 proto = (ip_proto_ref) malloc(sizeof(ip_protos_t)); 1134 if (!proto) 1135 return ENOMEM; 1136 1137 proto->protocol = protocol; 1138 proto->service = service; 1139 proto->phone = phone; 1140 proto->received_msg = received_msg; 1141 1142 fibril_rwlock_write_lock(&ip_globals.protos_lock); 1143 index = ip_protos_add(&ip_globals.protos, proto->protocol, proto); 1144 if (index < 0) { 1145 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1146 free(proto); 1147 return index; 1148 } 1149 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1150 1151 printf("%s: Protocol registered (protocol: %d, phone: %d)\n", 1152 NAME, proto->protocol, proto->phone); 1153 1154 return EOK; 1155 } 1156 1157 static int 1158 ip_device_req_local(int il_phone, device_id_t device_id, services_t netif) 1159 { 1160 ERROR_DECLARE; 1161 1162 ip_netif_ref ip_netif; 1163 ip_route_ref route; 1164 int index; 1165 1166 ip_netif = (ip_netif_ref) malloc(sizeof(ip_netif_t)); 1167 if (!ip_netif) 1168 return ENOMEM; 1169 1170 if (ERROR_OCCURRED(ip_routes_initialize(&ip_netif->routes))) { 1171 free(ip_netif); 1172 return ERROR_CODE; 1173 } 1174 1175 ip_netif->device_id = device_id; 1176 ip_netif->service = netif; 1177 ip_netif->state = NETIF_STOPPED; 1178 1179 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1180 if (ERROR_OCCURRED(ip_netif_initialize(ip_netif))) { 1181 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1182 ip_routes_destroy(&ip_netif->routes); 1183 free(ip_netif); 1184 return ERROR_CODE; 1185 } 1186 if (ip_netif->arp) 1187 ip_netif->arp->usage++; 1188 1189 // print the settings 1190 printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n", 1191 NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv, 1192 ip_netif->dhcp ? "dhcp" : "static"); 1193 1194 // TODO ipv6 addresses 1195 1196 char address[INET_ADDRSTRLEN]; 1197 char netmask[INET_ADDRSTRLEN]; 1198 char gateway[INET_ADDRSTRLEN]; 1199 1200 for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) { 1201 route = ip_routes_get_index(&ip_netif->routes, index); 1202 if (route) { 1203 inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, 1204 address, INET_ADDRSTRLEN); 1205 inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, 1206 netmask, INET_ADDRSTRLEN); 1207 inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, 1208 gateway, INET_ADDRSTRLEN); 1209 printf("%s: Route %d (address: %s, netmask: %s, " 1210 "gateway: %s)\n", NAME, index, address, netmask, 1211 gateway); 1212 } 1213 } 1214 1215 inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address, 1216 INET_ADDRSTRLEN); 1217 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1218 1219 printf("%s: Broadcast (%s)\n", NAME, address); 1220 1221 return EOK; 1222 } 1223 1224 static int 1225 ip_send_msg_local(int il_phone, device_id_t device_id, packet_t packet, 1226 services_t sender, services_t error) 1227 { 1228 ERROR_DECLARE; 1229 1134 return rc; 1135 } 1136 1137 static int ip_send_msg_local(int il_phone, device_id_t device_id, 1138 packet_t *packet, services_t sender, services_t error) 1139 { 1230 1140 int addrlen; 1231 ip_netif_ refnetif;1232 ip_route_ refroute;1141 ip_netif_t *netif; 1142 ip_route_t *route; 1233 1143 struct sockaddr *addr; 1234 1144 struct sockaddr_in *address_in; … … 1236 1146 in_addr_t *src; 1237 1147 int phone; 1238 1239 // addresses in the host byte order 1240 // should be the next hop address or the target destination address 1148 int rc; 1149 1150 /* 1151 * Addresses in the host byte order 1152 * Should be the next hop address or the target destination address 1153 */ 1241 1154 addrlen = packet_get_addr(packet, NULL, (uint8_t **) &addr); 1242 1155 if (addrlen < 0) … … 1263 1176 fibril_rwlock_read_lock(&ip_globals.netifs_lock); 1264 1177 1265 / / device specified?1178 /* Device specified? */ 1266 1179 if (device_id > 0) { 1267 1180 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1268 route = ip_netif_find_route(netif, * 1181 route = ip_netif_find_route(netif, *dest); 1269 1182 if (netif && !route && (ip_globals.gateway.netif == netif)) 1270 1183 route = &ip_globals.gateway; … … 1279 1192 phone = ip_prepare_icmp_and_get_phone(error, packet, NULL); 1280 1193 if (phone >= 0) { 1281 / / unreachable ICMP if no routing1194 /* Unreachable ICMP if no routing */ 1282 1195 icmp_destination_unreachable_msg(phone, 1283 1196 ICMP_NET_UNREACH, 0, packet); … … 1287 1200 1288 1201 if (error) { 1289 // do not send for broadcast, anycast packets or network 1290 // broadcast 1202 /* 1203 * Do not send for broadcast, anycast packets or network 1204 * broadcast. 1205 */ 1291 1206 if (!dest->s_addr || !(~dest->s_addr) || 1292 1207 !(~((dest->s_addr & ~route->netmask.s_addr) | … … 1296 1211 } 1297 1212 } 1298 1299 / / if the local host is the destination1213 1214 /* If the local host is the destination */ 1300 1215 if ((route->address.s_addr == dest->s_addr) && 1301 1216 (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) { 1302 / / find the loopback device to deliver1217 /* Find the loopback device to deliver */ 1303 1218 dest->s_addr = IPV4_LOCALHOST_ADDRESS; 1304 1219 route = ip_find_route(*dest); … … 1309 1224 NULL); 1310 1225 if (phone >= 0) { 1311 / / unreachable ICMP if no routing1226 /* Unreachable ICMP if no routing */ 1312 1227 icmp_destination_unreachable_msg(phone, 1313 1228 ICMP_HOST_UNREACH, 0, packet); … … 1323 1238 } 1324 1239 1325 ERROR_CODE= ip_send_route(packet, netif, route, src, *dest, error);1240 rc = ip_send_route(packet, netif, route, src, *dest, error); 1326 1241 fibril_rwlock_read_unlock(&ip_globals.netifs_lock); 1327 1242 1328 return ERROR_CODE; 1243 return rc; 1244 } 1245 1246 /** Updates the device state. 1247 * 1248 * @param[in] device_id The device identifier. 1249 * @param[in] state The new state value. 1250 * @return EOK on success. 1251 * @return ENOENT if device is not found. 1252 */ 1253 static int ip_device_state_message(device_id_t device_id, device_state_t state) 1254 { 1255 ip_netif_t *netif; 1256 1257 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1258 /* Find the device */ 1259 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1260 if (!netif) { 1261 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1262 return ENOENT; 1263 } 1264 netif->state = state; 1265 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1266 1267 printf("%s: Device %d changed state to %d\n", NAME, device_id, state); 1268 1269 return EOK; 1270 } 1271 1272 /** Returns the packet destination address from the IP header. 1273 * 1274 * @param[in] header The packet IP header to be read. 1275 * @return The packet destination address. 1276 */ 1277 static in_addr_t ip_get_destination(ip_header_t *header) 1278 { 1279 in_addr_t destination; 1280 1281 // TODO search set ipopt route? 1282 destination.s_addr = header->destination_address; 1283 return destination; 1284 } 1285 1286 /** Delivers the packet to the local host. 1287 * 1288 * The packet is either passed to another module or released on error. 1289 * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not 1290 * found. 1291 * 1292 * @param[in] device_id The source device identifier. 1293 * @param[in] packet The packet to be delivered. 1294 * @param[in] header The first packet IP header. May be NULL. 1295 * @param[in] error The packet error service. 1296 * @return EOK on success. 1297 * @return ENOTSUP if the packet is a fragment. 1298 * @return EAFNOSUPPORT if the address family is not supported. 1299 * @return ENOENT if the target protocol is not found. 1300 * @return Other error codes as defined for the packet_set_addr() 1301 * function. 1302 * @return Other error codes as defined for the packet_trim() 1303 * function. 1304 * @return Other error codes as defined for the protocol specific 1305 * tl_received_msg() function. 1306 */ 1307 static int ip_deliver_local(device_id_t device_id, packet_t *packet, 1308 ip_header_t *header, services_t error) 1309 { 1310 ip_proto_t *proto; 1311 int phone; 1312 services_t service; 1313 tl_received_msg_t received_msg; 1314 struct sockaddr *src; 1315 struct sockaddr *dest; 1316 struct sockaddr_in src_in; 1317 struct sockaddr_in dest_in; 1318 socklen_t addrlen; 1319 int rc; 1320 1321 if ((header->flags & IPFLAG_MORE_FRAGMENTS) || 1322 IP_FRAGMENT_OFFSET(header)) { 1323 // TODO fragmented 1324 return ENOTSUP; 1325 } 1326 1327 switch (header->version) { 1328 case IPVERSION: 1329 addrlen = sizeof(src_in); 1330 bzero(&src_in, addrlen); 1331 src_in.sin_family = AF_INET; 1332 memcpy(&dest_in, &src_in, addrlen); 1333 memcpy(&src_in.sin_addr.s_addr, &header->source_address, 1334 sizeof(header->source_address)); 1335 memcpy(&dest_in.sin_addr.s_addr, &header->destination_address, 1336 sizeof(header->destination_address)); 1337 src = (struct sockaddr *) &src_in; 1338 dest = (struct sockaddr *) &dest_in; 1339 break; 1340 1341 default: 1342 return ip_release_and_return(packet, EAFNOSUPPORT); 1343 } 1344 1345 rc = packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest, 1346 addrlen); 1347 if (rc != EOK) 1348 return ip_release_and_return(packet, rc); 1349 1350 /* Trim padding if present */ 1351 if (!error && 1352 (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) { 1353 rc = packet_trim(packet, 0, 1354 packet_get_data_length(packet) - IP_TOTAL_LENGTH(header)); 1355 if (rc != EOK) 1356 return ip_release_and_return(packet, rc); 1357 } 1358 1359 fibril_rwlock_read_lock(&ip_globals.protos_lock); 1360 1361 proto = ip_protos_find(&ip_globals.protos, header->protocol); 1362 if (!proto) { 1363 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1364 phone = ip_prepare_icmp_and_get_phone(error, packet, header); 1365 if (phone >= 0) { 1366 /* Unreachable ICMP */ 1367 icmp_destination_unreachable_msg(phone, 1368 ICMP_PROT_UNREACH, 0, packet); 1369 } 1370 return ENOENT; 1371 } 1372 1373 if (proto->received_msg) { 1374 service = proto->service; 1375 received_msg = proto->received_msg; 1376 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1377 rc = received_msg(device_id, packet, service, error); 1378 } else { 1379 rc = tl_received_msg(proto->phone, device_id, packet, 1380 proto->service, error); 1381 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1382 } 1383 1384 return rc; 1385 } 1386 1387 /** Processes the received packet. 1388 * 1389 * The packet is either passed to another module or released on error. 1390 * 1391 * The ICMP_PARAM_POINTER error notification may be sent if the checksum is 1392 * invalid. 1393 * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two. 1394 * The ICMP_HOST_UNREACH error notification may be sent if no route was found. 1395 * The ICMP_HOST_UNREACH error notification may be sent if the packet is for 1396 * another host and the routing is disabled. 1397 * 1398 * @param[in] device_id The source device identifier. 1399 * @param[in] packet The received packet to be processed. 1400 * @return EOK on success. 1401 * @return EINVAL if the TTL is less than two. 1402 * @return EINVAL if the checksum is invalid. 1403 * @return EAFNOSUPPORT if the address family is not supported. 1404 * @return ENOENT if no route was found. 1405 * @return ENOENT if the packet is for another host and the routing 1406 * is disabled. 1407 */ 1408 static int ip_process_packet(device_id_t device_id, packet_t *packet) 1409 { 1410 ip_header_t *header; 1411 in_addr_t dest; 1412 ip_route_t *route; 1413 int phone; 1414 struct sockaddr *addr; 1415 struct sockaddr_in addr_in; 1416 socklen_t addrlen; 1417 int rc; 1418 1419 header = (ip_header_t *) packet_get_data(packet); 1420 if (!header) 1421 return ip_release_and_return(packet, ENOMEM); 1422 1423 /* Checksum */ 1424 if ((header->header_checksum) && 1425 (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) { 1426 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1427 if (phone >= 0) { 1428 /* Checksum error ICMP */ 1429 icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER, 1430 ((size_t) ((void *) &header->header_checksum)) - 1431 ((size_t) ((void *) header)), packet); 1432 } 1433 return EINVAL; 1434 } 1435 1436 if (header->ttl <= 1) { 1437 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1438 if (phone >= 0) { 1439 /* TTL exceeded ICMP */ 1440 icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet); 1441 } 1442 return EINVAL; 1443 } 1444 1445 /* Process ipopt and get destination */ 1446 dest = ip_get_destination(header); 1447 1448 /* Set the destination address */ 1449 switch (header->version) { 1450 case IPVERSION: 1451 addrlen = sizeof(addr_in); 1452 bzero(&addr_in, addrlen); 1453 addr_in.sin_family = AF_INET; 1454 memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest)); 1455 addr = (struct sockaddr *) &addr_in; 1456 break; 1457 1458 default: 1459 return ip_release_and_return(packet, EAFNOSUPPORT); 1460 } 1461 1462 rc = packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen); 1463 if (rc != EOK) 1464 return rc; 1465 1466 route = ip_find_route(dest); 1467 if (!route) { 1468 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1469 if (phone >= 0) { 1470 /* Unreachable ICMP */ 1471 icmp_destination_unreachable_msg(phone, 1472 ICMP_HOST_UNREACH, 0, packet); 1473 } 1474 return ENOENT; 1475 } 1476 1477 if (route->address.s_addr == dest.s_addr) { 1478 /* Local delivery */ 1479 return ip_deliver_local(device_id, packet, header, 0); 1480 } 1481 1482 if (route->netif->routing) { 1483 header->ttl--; 1484 return ip_send_route(packet, route->netif, route, NULL, dest, 1485 0); 1486 } 1487 1488 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1489 if (phone >= 0) { 1490 /* Unreachable ICMP if no routing */ 1491 icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, 1492 packet); 1493 } 1494 1495 return ENOENT; 1329 1496 } 1330 1497 … … 1338 1505 * @param[out] content The maximum content size. 1339 1506 * @param[out] suffix The minimum reserved suffix size. 1340 * @returns EOK on success. 1341 */ 1342 static int 1343 ip_packet_size_message(device_id_t device_id, size_t *addr_len, size_t *prefix, 1344 size_t *content, size_t *suffix) 1345 { 1346 ip_netif_ref netif; 1507 * @return EOK on success. 1508 */ 1509 static int ip_packet_size_message(device_id_t device_id, size_t *addr_len, 1510 size_t *prefix, size_t *content, size_t *suffix) 1511 { 1512 ip_netif_t *netif; 1347 1513 int index; 1348 1514 … … 1392 1558 } 1393 1559 1394 /** Returns the packet destination address from the IP header. 1395 * 1396 * @param[in] header The packet IP header to be read. 1397 * @returns The packet destination address. 1398 */ 1399 static in_addr_t ip_get_destination(ip_header_ref header) 1400 { 1401 in_addr_t destination; 1402 1403 // TODO search set ipopt route? 1404 destination.s_addr = header->destination_address; 1405 return destination; 1406 } 1407 1408 /** Delivers the packet to the local host. 1409 * 1410 * The packet is either passed to another module or released on error. 1411 * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not 1412 * found. 1413 * 1414 * @param[in] device_id The source device identifier. 1415 * @param[in] packet The packet to be delivered. 1416 * @param[in] header The first packet IP header. May be NULL. 1417 * @param[in] error The packet error service. 1418 * @returns EOK on success. 1419 * @returns ENOTSUP if the packet is a fragment. 1420 * @returns EAFNOSUPPORT if the address family is not supported. 1421 * @returns ENOENT if the target protocol is not found. 1422 * @returns Other error codes as defined for the packet_set_addr() 1423 * function. 1424 * @returns Other error codes as defined for the packet_trim() 1425 * function. 1426 * @returns Other error codes as defined for the protocol specific 1427 * tl_received_msg() function. 1428 */ 1429 static int 1430 ip_deliver_local(device_id_t device_id, packet_t packet, ip_header_ref header, 1431 services_t error) 1432 { 1433 ERROR_DECLARE; 1434 1435 ip_proto_ref proto; 1436 int phone; 1437 services_t service; 1438 tl_received_msg_t received_msg; 1439 struct sockaddr *src; 1440 struct sockaddr *dest; 1441 struct sockaddr_in src_in; 1442 struct sockaddr_in dest_in; 1443 socklen_t addrlen; 1444 1445 if ((header->flags & IPFLAG_MORE_FRAGMENTS) || 1446 IP_FRAGMENT_OFFSET(header)) { 1447 // TODO fragmented 1448 return ENOTSUP; 1449 } 1450 1451 switch (header->version) { 1452 case IPVERSION: 1453 addrlen = sizeof(src_in); 1454 bzero(&src_in, addrlen); 1455 src_in.sin_family = AF_INET; 1456 memcpy(&dest_in, &src_in, addrlen); 1457 memcpy(&src_in.sin_addr.s_addr, &header->source_address, 1458 sizeof(header->source_address)); 1459 memcpy(&dest_in.sin_addr.s_addr, &header->destination_address, 1460 sizeof(header->destination_address)); 1461 src = (struct sockaddr *) &src_in; 1462 dest = (struct sockaddr *) &dest_in; 1463 break; 1464 1465 default: 1466 return ip_release_and_return(packet, EAFNOSUPPORT); 1467 } 1468 1469 if (ERROR_OCCURRED(packet_set_addr(packet, (uint8_t *) src, 1470 (uint8_t *) dest, addrlen))) { 1471 return ip_release_and_return(packet, ERROR_CODE); 1472 } 1473 1474 // trim padding if present 1475 if (!error && 1476 (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) { 1477 if (ERROR_OCCURRED(packet_trim(packet, 0, 1478 packet_get_data_length(packet) - IP_TOTAL_LENGTH(header)))) 1479 return ip_release_and_return(packet, ERROR_CODE); 1480 } 1481 1482 fibril_rwlock_read_lock(&ip_globals.protos_lock); 1483 1484 proto = ip_protos_find(&ip_globals.protos, header->protocol); 1485 if (!proto) { 1486 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1487 phone = ip_prepare_icmp_and_get_phone(error, packet, header); 1488 if (phone >= 0) { 1489 // unreachable ICMP 1490 icmp_destination_unreachable_msg(phone, 1491 ICMP_PROT_UNREACH, 0, packet); 1492 } 1493 return ENOENT; 1494 } 1495 1496 if (proto->received_msg) { 1497 service = proto->service; 1498 received_msg = proto->received_msg; 1499 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1500 ERROR_CODE = received_msg(device_id, packet, service, error); 1501 } else { 1502 ERROR_CODE = tl_received_msg(proto->phone, device_id, packet, 1503 proto->service, error); 1504 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1505 } 1506 1507 return ERROR_CODE; 1508 } 1509 1510 /** Processes the received packet. 1511 * 1512 * The packet is either passed to another module or released on error. 1513 * 1514 * The ICMP_PARAM_POINTER error notification may be sent if the checksum is 1515 * invalid. 1516 * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two. 1517 * The ICMP_HOST_UNREACH error notification may be sent if no route was found. 1518 * The ICMP_HOST_UNREACH error notification may be sent if the packet is for 1519 * another host and the routing is disabled. 1520 * 1521 * @param[in] device_id The source device identifier. 1522 * @param[in] packet The received packet to be processed. 1523 * @returns EOK on success. 1524 * @returns EINVAL if the TTL is less than two. 1525 * @returns EINVAL if the checksum is invalid. 1526 * @returns EAFNOSUPPORT if the address family is not supported. 1527 * @returns ENOENT if no route was found. 1528 * @returns ENOENT if the packet is for another host and the routing 1529 * is disabled. 1530 */ 1531 static int 1532 ip_process_packet(device_id_t device_id, packet_t packet) 1533 { 1534 ERROR_DECLARE; 1535 1536 ip_header_ref header; 1537 in_addr_t dest; 1538 ip_route_ref route; 1539 int phone; 1540 struct sockaddr *addr; 1541 struct sockaddr_in addr_in; 1542 socklen_t addrlen; 1543 1544 header = (ip_header_ref) packet_get_data(packet); 1545 if (!header) 1546 return ip_release_and_return(packet, ENOMEM); 1547 1548 // checksum 1549 if ((header->header_checksum) && 1550 (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) { 1551 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1552 if (phone >= 0) { 1553 // checksum error ICMP 1554 icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER, 1555 ((size_t) ((void *) &header->header_checksum)) - 1556 ((size_t) ((void *) header)), packet); 1557 } 1558 return EINVAL; 1559 } 1560 1561 if (header->ttl <= 1) { 1562 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1563 if (phone >= 0) { 1564 // ttl exceeded ICMP 1565 icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet); 1566 } 1567 return EINVAL; 1568 } 1569 1570 // process ipopt and get destination 1571 dest = ip_get_destination(header); 1572 1573 // set the addrination address 1574 switch (header->version) { 1575 case IPVERSION: 1576 addrlen = sizeof(addr_in); 1577 bzero(&addr_in, addrlen); 1578 addr_in.sin_family = AF_INET; 1579 memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest)); 1580 addr = (struct sockaddr *) &addr_in; 1581 break; 1582 1583 default: 1584 return ip_release_and_return(packet, EAFNOSUPPORT); 1585 } 1586 1587 ERROR_PROPAGATE(packet_set_addr(packet, NULL, (uint8_t *) &addr, 1588 addrlen)); 1589 1590 route = ip_find_route(dest); 1591 if (!route) { 1592 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1593 if (phone >= 0) { 1594 // unreachable ICMP 1595 icmp_destination_unreachable_msg(phone, 1596 ICMP_HOST_UNREACH, 0, packet); 1597 } 1598 return ENOENT; 1599 } 1600 1601 if (route->address.s_addr == dest.s_addr) { 1602 // local delivery 1603 return ip_deliver_local(device_id, packet, header, 0); 1604 } 1605 1606 if (route->netif->routing) { 1607 header->ttl--; 1608 return ip_send_route(packet, route->netif, route, NULL, dest, 1609 0); 1610 } 1611 1612 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1613 if (phone >= 0) { 1614 // unreachable ICMP if no routing 1615 icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, 1616 packet); 1617 } 1618 1619 return ENOENT; 1620 } 1621 1622 static int 1623 ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address, 1624 in_addr_t netmask, in_addr_t gateway) 1625 { 1626 ip_route_ref route; 1627 ip_netif_ref netif; 1628 int index; 1560 /** Updates the device content length according to the new MTU value. 1561 * 1562 * @param[in] device_id The device identifier. 1563 * @param[in] mtu The new mtu value. 1564 * @return EOK on success. 1565 * @return ENOENT if device is not found. 1566 */ 1567 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu) 1568 { 1569 ip_netif_t *netif; 1629 1570 1630 1571 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1631 1632 1572 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1633 1573 if (!netif) { … … 1635 1575 return ENOENT; 1636 1576 } 1637 1638 route = (ip_route_ref) malloc(sizeof(ip_route_t)); 1577 netif->packet_dimension.content = mtu; 1578 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1579 1580 printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu); 1581 1582 return EOK; 1583 } 1584 1585 /** Process IPC messages from the registered device driver modules 1586 * 1587 * @param[in] iid Message identifier. 1588 * @param[in,out] icall Message parameters. 1589 * 1590 */ 1591 static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall) 1592 { 1593 packet_t *packet; 1594 int rc; 1595 1596 while (true) { 1597 switch (IPC_GET_IMETHOD(*icall)) { 1598 case NET_IL_DEVICE_STATE: 1599 rc = ip_device_state_message(IPC_GET_DEVICE(*icall), 1600 IPC_GET_STATE(*icall)); 1601 async_answer_0(iid, (sysarg_t) rc); 1602 break; 1603 1604 case NET_IL_RECEIVED: 1605 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1606 IPC_GET_PACKET(*icall)); 1607 if (rc == EOK) { 1608 do { 1609 packet_t *next = pq_detach(packet); 1610 ip_process_packet(IPC_GET_DEVICE(*icall), packet); 1611 packet = next; 1612 } while (packet); 1613 } 1614 1615 async_answer_0(iid, (sysarg_t) rc); 1616 break; 1617 1618 case NET_IL_MTU_CHANGED: 1619 rc = ip_mtu_changed_message(IPC_GET_DEVICE(*icall), 1620 IPC_GET_MTU(*icall)); 1621 async_answer_0(iid, (sysarg_t) rc); 1622 break; 1623 1624 default: 1625 async_answer_0(iid, (sysarg_t) ENOTSUP); 1626 } 1627 1628 iid = async_get_call(icall); 1629 } 1630 } 1631 1632 /** Registers the transport layer protocol. 1633 * 1634 * The traffic of this protocol will be supplied using either the receive 1635 * function or IPC message. 1636 * 1637 * @param[in] protocol The transport layer module protocol. 1638 * @param[in] service The transport layer module service. 1639 * @param[in] phone The transport layer module phone. 1640 * @param[in] received_msg The receiving function. 1641 * @return EOK on success. 1642 * @return EINVAL if the protocol parameter and/or the service 1643 * parameter is zero. 1644 * @return EINVAL if the phone parameter is not a positive number 1645 * and the tl_receive_msg is NULL. 1646 * @return ENOMEM if there is not enough memory left. 1647 */ 1648 static int 1649 ip_register(int protocol, services_t service, int phone, 1650 tl_received_msg_t received_msg) 1651 { 1652 ip_proto_t *proto; 1653 int index; 1654 1655 if (!protocol || !service || ((phone < 0) && !received_msg)) 1656 return EINVAL; 1657 1658 proto = (ip_proto_t *) malloc(sizeof(ip_protos_t)); 1659 if (!proto) 1660 return ENOMEM; 1661 1662 proto->protocol = protocol; 1663 proto->service = service; 1664 proto->phone = phone; 1665 proto->received_msg = received_msg; 1666 1667 fibril_rwlock_write_lock(&ip_globals.protos_lock); 1668 index = ip_protos_add(&ip_globals.protos, proto->protocol, proto); 1669 if (index < 0) { 1670 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1671 free(proto); 1672 return index; 1673 } 1674 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1675 1676 printf("%s: Protocol registered (protocol: %d, phone: %d)\n", 1677 NAME, proto->protocol, proto->phone); 1678 1679 return EOK; 1680 } 1681 1682 1683 static int 1684 ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address, 1685 in_addr_t netmask, in_addr_t gateway) 1686 { 1687 ip_route_t *route; 1688 ip_netif_t *netif; 1689 int index; 1690 1691 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1692 1693 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1694 if (!netif) { 1695 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1696 return ENOENT; 1697 } 1698 1699 route = (ip_route_t *) malloc(sizeof(ip_route_t)); 1639 1700 if (!route) { 1640 1701 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); … … 1658 1719 ip_set_gateway_req_local(int ip_phone, device_id_t device_id, in_addr_t gateway) 1659 1720 { 1660 ip_netif_ refnetif;1721 ip_netif_t *netif; 1661 1722 1662 1723 fibril_rwlock_write_lock(&ip_globals.netifs_lock); … … 1692 1753 static int 1693 1754 ip_received_error_msg_local(int ip_phone, device_id_t device_id, 1694 packet_t packet, services_t target, services_t error)1755 packet_t *packet, services_t target, services_t error) 1695 1756 { 1696 1757 uint8_t *data; … … 1698 1759 icmp_type_t type; 1699 1760 icmp_code_t code; 1700 ip_netif_ refnetif;1761 ip_netif_t *netif; 1701 1762 measured_string_t address; 1702 ip_route_ refroute;1703 ip_header_ refheader;1763 ip_route_t *route; 1764 ip_header_t *header; 1704 1765 1705 1766 switch (error) { … … 1711 1772 1712 1773 data = packet_get_data(packet); 1713 header = (ip_header_ ref)(data + offset);1714 1715 / / destination host unreachable?1774 header = (ip_header_t *)(data + offset); 1775 1776 /* Destination host unreachable? */ 1716 1777 if ((type != ICMP_DEST_UNREACH) || 1717 1778 (code != ICMP_HOST_UNREACH)) { 1718 // no, something else1779 /* No, something else */ 1719 1780 break; 1720 1781 } … … 1730 1791 route = ip_routes_get_index(&netif->routes, 0); 1731 1792 1732 / / from the same network?1793 /* From the same network? */ 1733 1794 if (route && ((route->address.s_addr & route->netmask.s_addr) == 1734 1795 (header->destination_address & route->netmask.s_addr))) { 1735 // clear the ARP mapping if any 1736 address.value = (char *) &header->destination_address; 1737 address.length = CONVERT_SIZE(uint8_t, char, 1738 sizeof(header->destination_address)); 1796 /* Clear the ARP mapping if any */ 1797 address.value = (uint8_t *) &header->destination_address; 1798 address.length = sizeof(header->destination_address); 1739 1799 arp_clear_address_req(netif->arp->phone, 1740 1800 netif->device_id, SERVICE_IP, &address); … … 1759 1819 in_addr_t *dest; 1760 1820 in_addr_t *src; 1761 ip_route_ refroute;1762 ipv4_pseudo_header_ refheader_in;1821 ip_route_t *route; 1822 ipv4_pseudo_header_t *header_in; 1763 1823 1764 1824 if (!destination || (addrlen <= 0)) … … 1788 1848 fibril_rwlock_read_lock(&ip_globals.lock); 1789 1849 route = ip_find_route(*dest); 1790 / / if the local host is the destination1850 /* If the local host is the destination */ 1791 1851 if (route && (route->address.s_addr == dest->s_addr) && 1792 1852 (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) { 1793 / / find the loopback device to deliver1853 /* Find the loopback device to deliver */ 1794 1854 dest->s_addr = IPV4_LOCALHOST_ADDRESS; 1795 1855 route = ip_find_route(*dest); … … 1806 1866 1807 1867 *headerlen = sizeof(*header_in); 1808 header_in = (ipv4_pseudo_header_ ref) malloc(*headerlen);1868 header_in = (ipv4_pseudo_header_t *) malloc(*headerlen); 1809 1869 if (!header_in) 1810 1870 return ENOMEM; … … 1820 1880 } 1821 1881 1822 /** Processes the received IP packet or the packet queue one by one.1823 *1824 * The packet is either passed to another module or released on error.1825 *1826 * @param[in] device_id The source device identifier.1827 * @param[in,out] packet The received packet.1828 * @returns EOK on success and the packet is no longer needed.1829 * @returns EINVAL if the packet is too small to carry the IP1830 * packet.1831 * @returns EINVAL if the received address lengths differs from the1832 * registered values.1833 * @returns ENOENT if the device is not found in the cache.1834 * @returns ENOENT if the protocol for the device is not found in1835 * the cache.1836 * @returns ENOMEM if there is not enough memory left.1837 */1838 static int ip_receive_message(device_id_t device_id, packet_t packet)1839 {1840 packet_t next;1841 1842 do {1843 next = pq_detach(packet);1844 ip_process_packet(device_id, packet);1845 packet = next;1846 } while (packet);1847 1848 return EOK;1849 }1850 1851 1882 /** Processes the IP message. 1852 1883 * … … 1856 1887 * @param[out] answer_count The last parameter for the actual answer in the 1857 1888 * answer parameter. 1858 * @return sEOK on success.1859 * @return sENOTSUP if the message is not known.1889 * @return EOK on success. 1890 * @return ENOTSUP if the message is not known. 1860 1891 * 1861 1892 * @see ip_interface.h 1862 * @see il_ interface.h1893 * @see il_remote.h 1863 1894 * @see IS_NET_IP_MESSAGE() 1864 1895 */ 1865 int 1866 ip_message_standalone(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 1867 int *answer_count) 1868 { 1869 ERROR_DECLARE; 1870 1871 packet_t packet; 1896 int il_module_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 1897 size_t *answer_count) 1898 { 1899 packet_t *packet; 1872 1900 struct sockaddr *addr; 1901 void *header; 1902 size_t headerlen; 1873 1903 size_t addrlen; 1874 1904 size_t prefix; 1875 1905 size_t suffix; 1876 1906 size_t content; 1877 void *header;1878 size_t headerlen;1879 1907 device_id_t device_id; 1908 int rc; 1880 1909 1881 1910 *answer_count = 0; 1882 switch (IPC_GET_ METHOD(*call)) {1911 switch (IPC_GET_IMETHOD(*call)) { 1883 1912 case IPC_M_PHONE_HUNGUP: 1884 1913 return EOK; 1885 1914 1886 1915 case IPC_M_CONNECT_TO_ME: 1887 return ip_register(IL_GET_PROTO(call), IL_GET_SERVICE(call), 1888 IPC_GET_PHONE(call), NULL); 1889 1890 case NET_IL_DEVICE: 1891 return ip_device_req_local(0, IPC_GET_DEVICE(call), 1892 IPC_GET_SERVICE(call)); 1893 1894 case NET_IL_SEND: 1895 ERROR_PROPAGATE(packet_translate_remote(ip_globals.net_phone, 1896 &packet, IPC_GET_PACKET(call))); 1897 return ip_send_msg_local(0, IPC_GET_DEVICE(call), packet, 0, 1898 IPC_GET_ERROR(call)); 1899 1900 case NET_IL_DEVICE_STATE: 1901 return ip_device_state_message(IPC_GET_DEVICE(call), 1902 IPC_GET_STATE(call)); 1903 1904 case NET_IL_RECEIVED: 1905 ERROR_PROPAGATE(packet_translate_remote(ip_globals.net_phone, 1906 &packet, IPC_GET_PACKET(call))); 1907 return ip_receive_message(IPC_GET_DEVICE(call), packet); 1916 return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call), 1917 IPC_GET_PHONE(*call), NULL); 1918 1919 case NET_IP_DEVICE: 1920 return ip_device_req_local(0, IPC_GET_DEVICE(*call), 1921 IPC_GET_SERVICE(*call)); 1908 1922 1909 1923 case NET_IP_RECEIVED_ERROR: 1910 ERROR_PROPAGATE(packet_translate_remote(ip_globals.net_phone, 1911 &packet, IPC_GET_PACKET(call))); 1912 return ip_received_error_msg_local(0, IPC_GET_DEVICE(call), 1913 packet, IPC_GET_TARGET(call), IPC_GET_ERROR(call)); 1924 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1925 IPC_GET_PACKET(*call)); 1926 if (rc != EOK) 1927 return rc; 1928 return ip_received_error_msg_local(0, IPC_GET_DEVICE(*call), 1929 packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call)); 1914 1930 1915 1931 case NET_IP_ADD_ROUTE: 1916 return ip_add_route_req_local(0, IPC_GET_DEVICE( call),1917 IP_GET_ADDRESS( call), IP_GET_NETMASK(call),1918 IP_GET_GATEWAY( call));1932 return ip_add_route_req_local(0, IPC_GET_DEVICE(*call), 1933 IP_GET_ADDRESS(*call), IP_GET_NETMASK(*call), 1934 IP_GET_GATEWAY(*call)); 1919 1935 1920 1936 case NET_IP_SET_GATEWAY: 1921 return ip_set_gateway_req_local(0, IPC_GET_DEVICE( call),1922 IP_GET_GATEWAY( call));1937 return ip_set_gateway_req_local(0, IPC_GET_DEVICE(*call), 1938 IP_GET_GATEWAY(*call)); 1923 1939 1924 1940 case NET_IP_GET_ROUTE: 1925 ERROR_PROPAGATE(data_receive((void **) &addr, &addrlen)); 1926 ERROR_PROPAGATE(ip_get_route_req_local(0, IP_GET_PROTOCOL(call), 1927 addr, (socklen_t) addrlen, &device_id, &header, 1928 &headerlen)); 1929 IPC_SET_DEVICE(answer, device_id); 1930 IP_SET_HEADERLEN(answer, headerlen); 1941 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, 1942 &addrlen); 1943 if (rc != EOK) 1944 return rc; 1945 1946 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL(*call), addr, 1947 (socklen_t) addrlen, &device_id, &header, &headerlen); 1948 if (rc != EOK) 1949 return rc; 1950 1951 IPC_SET_DEVICE(*answer, device_id); 1952 IP_SET_HEADERLEN(*answer, headerlen); 1931 1953 1932 1954 *answer_count = 2; 1933 1934 if (ERROR_NONE(data_reply(&headerlen, sizeof(headerlen)))) 1935 ERROR_CODE = data_reply(header, headerlen); 1955 1956 rc = data_reply(&headerlen, sizeof(headerlen)); 1957 if (rc == EOK) 1958 rc = data_reply(header, headerlen); 1936 1959 1937 1960 free(header); 1938 return ERROR_CODE; 1939 1940 case NET_IL_PACKET_SPACE: 1941 ERROR_PROPAGATE(ip_packet_size_message(IPC_GET_DEVICE(call), 1942 &addrlen, &prefix, &content, &suffix)); 1943 IPC_SET_ADDR(answer, addrlen); 1944 IPC_SET_PREFIX(answer, prefix); 1945 IPC_SET_CONTENT(answer, content); 1946 IPC_SET_SUFFIX(answer, suffix); 1961 return rc; 1962 1963 case NET_IP_PACKET_SPACE: 1964 rc = ip_packet_size_message(IPC_GET_DEVICE(*call), &addrlen, 1965 &prefix, &content, &suffix); 1966 if (rc != EOK) 1967 return rc; 1968 1969 IPC_SET_ADDR(*answer, addrlen); 1970 IPC_SET_PREFIX(*answer, prefix); 1971 IPC_SET_CONTENT(*answer, content); 1972 IPC_SET_SUFFIX(*answer, suffix); 1947 1973 *answer_count = 4; 1948 1974 return EOK; 1949 1975 1950 case NET_IL_MTU_CHANGED: 1951 return ip_mtu_changed_message(IPC_GET_DEVICE(call), 1952 IPC_GET_MTU(call)); 1976 case NET_IP_SEND: 1977 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1978 IPC_GET_PACKET(*call)); 1979 if (rc != EOK) 1980 return rc; 1981 1982 return ip_send_msg_local(0, IPC_GET_DEVICE(*call), packet, 0, 1983 IPC_GET_ERROR(*call)); 1953 1984 } 1954 1985 … … 1956 1987 } 1957 1988 1958 /** Default thread for new connections.1959 *1960 * @param[in] iid The initial message identifier.1961 * @param[in] icall The initial message call structure.1962 */1963 static void il_client_connection(ipc_callid_t iid, ipc_call_t *icall)1964 {1965 /*1966 * Accept the connection1967 * - Answer the first IPC_M_CONNECT_ME_TO call.1968 */1969 ipc_answer_0(iid, EOK);1970 1971 while (true) {1972 ipc_call_t answer;1973 int answer_count;1974 1975 /* Clear the answer structure */1976 refresh_answer(&answer, &answer_count);1977 1978 /* Fetch the next message */1979 ipc_call_t call;1980 ipc_callid_t callid = async_get_call(&call);1981 1982 /* Process the message */1983 int res = il_module_message_standalone(callid, &call, &answer,1984 &answer_count);1985 1986 /*1987 * End if told to either by the message or the processing1988 * result.1989 */1990 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||1991 (res == EHANGUP)) {1992 return;1993 }1994 1995 /* Answer the message */1996 answer_call(callid, res, &answer, answer_count);1997 }1998 }1999 2000 /** Starts the module.2001 *2002 * @returns EOK on success.2003 * @returns Other error codes as defined for each specific module start function.2004 */2005 1989 int main(int argc, char *argv[]) 2006 1990 { 2007 ERROR_DECLARE;2008 2009 1991 /* Start the module */ 2010 ERROR_PROPAGATE(il_module_start_standalone(il_client_connection)); 2011 return EOK; 1992 return il_module_start(SERVICE_IP); 2012 1993 } 2013 1994
Note:
See TracChangeset
for help on using the changeset viewer.