Changeset eb522e8 in mainline for uspace/srv/net/il/ip/ip.c
- Timestamp:
- 2011-06-01T08:43:42Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8d6c1f1
- Parents:
- 9e2e715 (diff), e51a514 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
uspace/srv/net/il/ip/ip.c (modified) (72 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/il/ip/ip.c
r9e2e715 reb522e8 36 36 */ 37 37 38 #include "ip.h"39 #include "ip_module.h"40 41 38 #include <async.h> 42 39 #include <errno.h> … … 44 41 #include <stdio.h> 45 42 #include <str.h> 46 #include <ipc/ipc.h>47 43 #include <ipc/services.h> 48 44 #include <ipc/net.h> … … 52 48 #include <sys/types.h> 53 49 #include <byteorder.h> 50 #include "ip.h" 54 51 55 52 #include <adt/measured_strings.h> … … 69 66 #include <net_checksum.h> 70 67 #include <icmp_client.h> 71 #include <icmp_interface.h> 72 #include <il_interface.h> 68 #include <icmp_remote.h> 73 69 #include <ip_client.h> 74 70 #include <ip_interface.h> 75 71 #include <ip_header.h> 76 72 #include <net_interface.h> 77 #include <nil_ interface.h>78 #include <tl_ interface.h>73 #include <nil_remote.h> 74 #include <tl_remote.h> 79 75 #include <packet_remote.h> 80 #include <il_local.h> 76 #include <il_remote.h> 77 #include <il_skel.h> 81 78 82 79 /** IP module name. */ … … 123 120 GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t); 124 121 122 static void ip_receiver(ipc_callid_t, ipc_call_t *); 123 125 124 /** Releases the packet and returns the result. 126 125 * … … 129 128 * @return The result parameter. 130 129 */ 131 static int ip_release_and_return(packet_t packet, int result)130 static int ip_release_and_return(packet_t *packet, int result) 132 131 { 133 132 pq_release_remote(ip_globals.net_phone, packet_get_id(packet)); … … 139 138 * Searches the registered protocols. 140 139 * 141 * @return sThe found ICMP phone.142 * @return sENOENT if the ICMP is not registered.140 * @return The found ICMP phone. 141 * @return ENOENT if the ICMP is not registered. 143 142 */ 144 143 static int ip_get_icmp_phone(void) 145 144 { 146 ip_proto_ refproto;145 ip_proto_t *proto; 147 146 int phone; 148 147 … … 160 159 * @param[in] packet The packet or the packet queue to be reported as faulty. 161 160 * @param[in] header The first packet IP header. May be NULL. 162 * @return sEOK on success.163 * @return sEINVAL if there are no data in the packet.164 * @return sEINVAL if the packet is a fragment.165 * @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 166 165 * header. 167 * @return sEAFNOSUPPORT if the address family is not supported.168 * @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 169 168 * notifications. The ICMP protocol itself. 170 * @return sOther error codes as defined for the packet_set_addr().171 */ 172 static int ip_prepare_icmp(packet_t packet, ip_header_refheader)173 { 174 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; 175 174 struct sockaddr *dest; 176 175 struct sockaddr_in dest_in; 177 176 socklen_t addrlen; 178 177 179 / / detach the first packet and release the others178 /* Detach the first packet and release the others */ 180 179 next = pq_detach(packet); 181 180 if (next) … … 186 185 return ENOMEM; 187 186 188 / / get header189 header = (ip_header_ ref) packet_get_data(packet);187 /* Get header */ 188 header = (ip_header_t *) packet_get_data(packet); 190 189 if (!header) 191 190 return EINVAL; … … 193 192 } 194 193 195 / / only for the first fragment194 /* Only for the first fragment */ 196 195 if (IP_FRAGMENT_OFFSET(header)) 197 196 return EINVAL; 198 197 199 / / not for the ICMP protocol198 /* Not for the ICMP protocol */ 200 199 if (header->protocol == IPPROTO_ICMP) 201 200 return EPERM; 202 201 203 / / set the destination address202 /* Set the destination address */ 204 203 switch (header->version) { 205 204 case IPVERSION: … … 227 226 * @param[in] packet The packet or the packet queue to be reported as faulty. 228 227 * @param[in] header The first packet IP header. May be NULL. 229 * @return sThe found ICMP phone.230 * @return sEINVAL if the error parameter is set.231 * @return sEINVAL if the ICMP phone is not found.232 * @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. 233 232 */ 234 233 static int 235 ip_prepare_icmp_and_get_phone(services_t error, packet_t packet,236 ip_header_ refheader)234 ip_prepare_icmp_and_get_phone(services_t error, packet_t *packet, 235 ip_header_t *header) 237 236 { 238 237 int phone; … … 244 243 } 245 244 246 /** Initializes the IP module. 247 * 248 * @param[in] client_connection The client connection processing function. The 249 * module skeleton propagates its own one. 250 * @returns EOK on success. 251 * @returns ENOMEM if there is not enough memory left. 252 */ 253 int ip_initialize(async_client_conn_t client_connection) 254 { 255 int rc; 256 245 int il_initialize(int net_phone) 246 { 257 247 fibril_rwlock_initialize(&ip_globals.lock); 258 248 fibril_rwlock_write_lock(&ip_globals.lock); 259 249 fibril_rwlock_initialize(&ip_globals.protos_lock); 260 250 fibril_rwlock_initialize(&ip_globals.netifs_lock); 251 252 ip_globals.net_phone = net_phone; 261 253 ip_globals.packet_counter = 0; 262 254 ip_globals.gateway.address.s_addr = 0; … … 264 256 ip_globals.gateway.gateway.s_addr = 0; 265 257 ip_globals.gateway.netif = NULL; 266 ip_globals.client_connection = client_connection; 267 268 rc = ip_netifs_initialize(&ip_globals.netifs); 258 259 int rc = ip_netifs_initialize(&ip_globals.netifs); 269 260 if (rc != EOK) 270 261 goto out; … … 275 266 if (rc != EOK) 276 267 goto out; 277 rc = add_module(NULL, &ip_globals.modules, ARP_NAME, ARP_FILENAME,278 SERVICE_ARP, 0, arp_connect_module);268 rc = add_module(NULL, &ip_globals.modules, (uint8_t *) ARP_NAME, 269 (uint8_t *) ARP_FILENAME, SERVICE_ARP, 0, arp_connect_module); 279 270 280 271 out: … … 293 284 * 294 285 * @param[in,out] ip_netif Network interface specific data. 295 * @return sEOK on success.296 * @return sENOTSUP if DHCP is configured.297 * @return sENOTSUP if IPv6 is configured.298 * @return sEINVAL if any of the addresses is invalid.299 * @return sEINVAL if the used ARP module is not known.300 * @return sENOMEM if there is not enough memory left.301 * @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 302 293 * net_get_device_conf_req() function. 303 * @return sOther error codes as defined for the bind_service()294 * @return Other error codes as defined for the bind_service() 304 295 * function. 305 * @return sOther error codes as defined for the specific296 * @return Other error codes as defined for the specific 306 297 * arp_device_req() function. 307 * @return sOther error codes as defined for the298 * @return Other error codes as defined for the 308 299 * nil_packet_size_req() function. 309 300 */ 310 static int ip_netif_initialize(ip_netif_ refip_netif)301 static int ip_netif_initialize(ip_netif_t *ip_netif) 311 302 { 312 303 measured_string_t names[] = { 313 304 { 314 ( char*) "IPV",305 (uint8_t *) "IPV", 315 306 3 316 307 }, 317 308 { 318 ( char*) "IP_CONFIG",309 (uint8_t *) "IP_CONFIG", 319 310 9 320 311 }, 321 312 { 322 ( char*) "IP_ADDR",313 (uint8_t *) "IP_ADDR", 323 314 7 324 315 }, 325 316 { 326 ( char*) "IP_NETMASK",317 (uint8_t *) "IP_NETMASK", 327 318 10 328 319 }, 329 320 { 330 ( char*) "IP_GATEWAY",321 (uint8_t *) "IP_GATEWAY", 331 322 10 332 323 }, 333 324 { 334 ( char*) "IP_BROADCAST",325 (uint8_t *) "IP_BROADCAST", 335 326 12 336 327 }, 337 328 { 338 ( char*) "ARP",329 (uint8_t *) "ARP", 339 330 3 340 331 }, 341 332 { 342 ( char*) "IP_ROUTING",333 (uint8_t *) "IP_ROUTING", 343 334 10 344 335 } 345 336 }; 346 measured_string_ refconfiguration;337 measured_string_t *configuration; 347 338 size_t count = sizeof(names) / sizeof(measured_string_t); 348 char*data;339 uint8_t *data; 349 340 measured_string_t address; 350 ip_route_ refroute;341 ip_route_t *route; 351 342 in_addr_t gateway; 352 343 int index; … … 360 351 configuration = &names[0]; 361 352 362 / / get configuration353 /* Get configuration */ 363 354 rc = net_get_device_conf_req(ip_globals.net_phone, ip_netif->device_id, 364 355 &configuration, count, &data); … … 368 359 if (configuration) { 369 360 if (configuration[0].value) 370 ip_netif->ipv = strtol( configuration[0].value, NULL, 0);371 372 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", 373 364 configuration[1].length); 374 365 … … 378 369 return ENOTSUP; 379 370 } else if (ip_netif->ipv == IPV4) { 380 route = (ip_route_ ref) malloc(sizeof(ip_route_t));371 route = (ip_route_t *) malloc(sizeof(ip_route_t)); 381 372 if (!route) { 382 373 net_free_settings(configuration, data); … … 394 385 } 395 386 396 if ((inet_pton(AF_INET, configuration[2].value,387 if ((inet_pton(AF_INET, (char *) configuration[2].value, 397 388 (uint8_t *) &route->address.s_addr) != EOK) || 398 (inet_pton(AF_INET, configuration[3].value,389 (inet_pton(AF_INET, (char *) configuration[3].value, 399 390 (uint8_t *) &route->netmask.s_addr) != EOK) || 400 (inet_pton(AF_INET, configuration[4].value,391 (inet_pton(AF_INET, (char *) configuration[4].value, 401 392 (uint8_t *) &gateway.s_addr) == EINVAL) || 402 (inet_pton(AF_INET, configuration[5].value,393 (inet_pton(AF_INET, (char *) configuration[5].value, 403 394 (uint8_t *) &ip_netif->broadcast.s_addr) == EINVAL)) 404 395 { … … 428 419 } 429 420 430 / / binds the netif service which also initializes the device421 /* Bind netif service which also initializes the device */ 431 422 ip_netif->phone = nil_bind_service(ip_netif->service, 432 ( ipcarg_t) ip_netif->device_id, SERVICE_IP,433 ip_ globals.client_connection);423 (sysarg_t) ip_netif->device_id, SERVICE_IP, 424 ip_receiver); 434 425 if (ip_netif->phone < 0) { 435 426 printf("Failed to contact the nil service %d\n", … … 438 429 } 439 430 440 / / has to be after the device netif module initialization431 /* Has to be after the device netif module initialization */ 441 432 if (ip_netif->arp) { 442 433 if (route) { 443 address.value = ( char*) &route->address.s_addr;444 address.length = CONVERT_SIZE(in_addr_t, char, 1);434 address.value = (uint8_t *) &route->address.s_addr; 435 address.length = sizeof(in_addr_t); 445 436 446 437 rc = arp_device_req(ip_netif->arp->phone, … … 454 445 } 455 446 456 / / get packet dimensions447 /* Get packet dimensions */ 457 448 rc = nil_packet_size_req(ip_netif->phone, ip_netif->device_id, 458 449 &ip_netif->packet_dimension); … … 461 452 462 453 if (ip_netif->packet_dimension.content < IP_MIN_CONTENT) { 463 printf("Maximum transmission unit % dbytes is too small, at "454 printf("Maximum transmission unit %zu bytes is too small, at " 464 455 "least %d bytes are needed\n", 465 456 ip_netif->packet_dimension.content, IP_MIN_CONTENT); … … 472 463 473 464 if (gateway.s_addr) { 474 / / the default gateway465 /* The default gateway */ 475 466 ip_globals.gateway.address.s_addr = 0; 476 467 ip_globals.gateway.netmask.s_addr = 0; 477 468 ip_globals.gateway.gateway.s_addr = gateway.s_addr; 478 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); 479 475 } 480 476 … … 482 478 } 483 479 484 /** Updates the device content length according to the new MTU value. 485 * 486 * @param[in] device_id The device identifier. 487 * @param[in] mtu The new mtu value. 488 * @returns EOK on success. 489 * @returns ENOENT if device is not found. 490 */ 491 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu) 492 { 493 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; 494 501 495 502 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 496 netif = ip_netifs_find(&ip_globals.netifs, device_id); 497 if (!netif) { 503 504 rc = ip_netif_initialize(ip_netif); 505 if (rc != EOK) { 498 506 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 499 return ENOENT; 500 } 501 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); 502 542 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 503 543 504 printf("%s: Device %d changed MTU to %d\n", NAME, device_id, mtu);544 printf("%s: Broadcast (%s)\n", NAME, address); 505 545 506 546 return EOK; 507 547 } 508 548 509 /** Updates the device state. 510 * 511 * @param[in] device_id The device identifier. 512 * @param[in] state The new state value. 513 * @returns EOK on success. 514 * @returns ENOENT if device is not found. 515 */ 516 static int ip_device_state_message(device_id_t device_id, device_state_t state) 517 { 518 ip_netif_ref netif; 519 520 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 521 // find the device 522 netif = ip_netifs_find(&ip_globals.netifs, device_id); 523 if (!netif) { 524 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 525 return ENOENT; 526 } 527 netif->state = state; 528 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 529 530 printf("%s: Device %d changed state to %d\n", NAME, device_id, state); 531 532 return EOK; 533 } 534 535 536 /** Prefixes a middle fragment header based on the last fragment header to the 537 * packet. 538 * 539 * @param[in] packet The packet to be prefixed. 540 * @param[in] last The last header to be copied. 541 * @returns The prefixed middle header. 542 * @returns NULL on error. 543 */ 544 static ip_header_ref 545 ip_create_middle_header(packet_t packet, ip_header_ref last) 546 { 547 ip_header_ref middle; 548 549 middle = (ip_header_ref) packet_suffix(packet, IP_HEADER_LENGTH(last)); 550 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) 551 564 return NULL; 552 memcpy(middle, last, IP_HEADER_LENGTH(last)); 553 middle->flags |= IPFLAG_MORE_FRAGMENTS; 554 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; 555 616 } 556 617 … … 562 623 * @param[in] first The original header to be copied. 563 624 */ 564 static void ip_create_last_header(ip_header_ ref last, ip_header_reffirst)565 { 566 ip_option_ refoption;625 static void ip_create_last_header(ip_header_t *last, ip_header_t *first) 626 { 627 ip_option_t *option; 567 628 size_t next; 568 629 size_t length; 569 630 570 / / copy first itself631 /* Copy first itself */ 571 632 memcpy(last, first, sizeof(ip_header_t)); 572 633 length = sizeof(ip_header_t); 573 634 next = sizeof(ip_header_t); 574 635 575 / / process all ip options636 /* Process all IP options */ 576 637 while (next < first->header_length) { 577 option = (ip_option_ ref) (((uint8_t *) first) + next);578 / / skip end or noop638 option = (ip_option_t *) (((uint8_t *) first) + next); 639 /* Skip end or noop */ 579 640 if ((option->type == IPOPT_END) || 580 641 (option->type == IPOPT_NOOP)) { 581 642 next++; 582 643 } else { 583 / / copy if told so or skip644 /* Copy if told so or skip */ 584 645 if (IPOPT_COPIED(option->type)) { 585 646 memcpy(((uint8_t *) last) + length, … … 587 648 length += option->length; 588 649 } 589 / / next option650 /* Next option */ 590 651 next += option->length; 591 652 } 592 653 } 593 654 594 / / align 4 byte boundary655 /* Align 4 byte boundary */ 595 656 if (length % 4) { 596 657 bzero(((uint8_t *) last) + length, 4 - (length % 4)); … … 613 674 * @param[in,out] packet The packet to be sent. 614 675 * @param[in] destination The destination hardware address. 615 * @return sEOK on success.616 * @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 617 678 * header. 618 * @return sEINVAL if the packet is too long than the IP allows.619 * @return sENOMEM if there is not enough memory left.620 * @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() 621 682 * function. 622 683 */ 623 static int 624 ip_prepare_packet(in_addr_t *source, in_addr_t dest, packet_t packet, 625 measured_string_ref destination) 684 static int ip_prepare_packet(in_addr_t *source, in_addr_t dest, 685 packet_t *packet, measured_string_t *destination) 626 686 { 627 687 size_t length; 628 ip_header_ refheader;629 ip_header_ reflast_header;630 ip_header_ refmiddle_header;631 packet_t next;688 ip_header_t *header; 689 ip_header_t *last_header; 690 ip_header_t *middle_header; 691 packet_t *next; 632 692 int rc; 633 693 … … 636 696 return EINVAL; 637 697 638 header = (ip_header_ ref) packet_get_data(packet);698 header = (ip_header_t *) packet_get_data(packet); 639 699 if (destination) { 640 700 rc = packet_set_addr(packet, NULL, (uint8_t *) destination->value, 641 CONVERT_SIZE(char, uint8_t, destination->length));701 destination->length); 642 702 } else { 643 703 rc = packet_set_addr(packet, NULL, NULL, 0); … … 660 720 661 721 if (pq_next(packet)) { 662 last_header = (ip_header_ ref) malloc(IP_HEADER_LENGTH(header));722 last_header = (ip_header_t *) malloc(IP_HEADER_LENGTH(header)); 663 723 if (!last_header) 664 724 return ENOMEM; … … 666 726 next = pq_next(packet); 667 727 while (pq_next(next)) { 668 middle_header = (ip_header_ ref) packet_prefix(next,728 middle_header = (ip_header_t *) packet_prefix(next, 669 729 IP_HEADER_LENGTH(last_header)); 670 730 if (!middle_header) { … … 687 747 rc = packet_set_addr(next, NULL, 688 748 (uint8_t *) destination->value, 689 CONVERT_SIZE(char, uint8_t, 690 destination->length)); 749 destination->length); 691 750 if (rc != EOK) { 692 751 free(last_header); … … 698 757 } 699 758 700 middle_header = (ip_header_ ref) packet_prefix(next,759 middle_header = (ip_header_t *) packet_prefix(next, 701 760 IP_HEADER_LENGTH(last_header)); 702 761 if (!middle_header) { … … 718 777 rc = packet_set_addr(next, NULL, 719 778 (uint8_t *) destination->value, 720 CONVERT_SIZE(char, uint8_t, destination->length));779 destination->length); 721 780 if (rc != EOK) { 722 781 free(last_header); … … 730 789 731 790 header->total_length = htons(length); 732 / / unnecessary for all protocols791 /* Unnecessary for all protocols */ 733 792 header->header_checksum = IP_HEADER_CHECKSUM(header); 734 793 … … 746 805 * @param[in] dest The destiantion address. 747 806 * @param[in] addrlen The address length. 748 * @return sEOK on success.749 * @return sENOMEM if the target packet is too small.750 * @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() 751 810 * function. 752 * @return sOther error codes as defined for the pq_insert_after()811 * @return Other error codes as defined for the pq_insert_after() 753 812 * function. 754 813 */ 755 static int 756 ip_fragment_packet_data(packet_t packet, packet_t new_packet, 757 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, 758 816 const struct sockaddr *src, const struct sockaddr *dest, socklen_t addrlen) 759 817 { … … 790 848 } 791 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; 869 } 870 792 871 /** Checks the packet length and fragments it if needed. 793 872 * … … 799 878 * @param[in] suffix The minimum suffix size. 800 879 * @param[in] addr_len The minimum address length. 801 * @return sEOK on success.802 * @return sEINVAL if the packet_get_addr() function fails.803 * @return sEINVAL if the packet does not contain the IP header.804 * @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 805 884 * fragmentation is not allowed. 806 * @return sENOMEM if there is not enough memory left.807 * @return sENOMEM if there is no packet available.808 * @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 809 888 * header. 810 * @return sOther error codes as defined for the packet_trim()889 * @return Other error codes as defined for the packet_trim() 811 890 * function. 812 * @return sOther error codes as defined for the891 * @return Other error codes as defined for the 813 892 * ip_create_middle_header() function. 814 * @return sOther error codes as defined for the893 * @return Other error codes as defined for the 815 894 * ip_fragment_packet_data() function. 816 895 */ 817 896 static int 818 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, 819 898 socklen_t addr_len) 820 899 { 821 packet_t new_packet;822 ip_header_ refheader;823 ip_header_ refmiddle_header;824 ip_header_ reflast_header;900 packet_t *new_packet; 901 ip_header_t *header; 902 ip_header_t *middle_header; 903 ip_header_t *last_header; 825 904 struct sockaddr *src; 826 905 struct sockaddr *dest; … … 837 916 return ENOMEM; 838 917 839 / / get header840 header = (ip_header_ ref) packet_get_data(packet);918 /* Get header */ 919 header = (ip_header_t *) packet_get_data(packet); 841 920 if (!header) 842 921 return EINVAL; 843 922 844 / / fragmentation forbidden?923 /* Fragmentation forbidden? */ 845 924 if(header->flags & IPFLAG_DONT_FRAGMENT) 846 925 return EPERM; 847 926 848 / / create the last fragment927 /* Create the last fragment */ 849 928 new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, length, 850 929 suffix, ((addrlen > addr_len) ? addrlen : addr_len)); … … 852 931 return ENOMEM; 853 932 854 / / allocate as much as originally855 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, 856 935 IP_HEADER_LENGTH(header)); 857 936 if (!last_header) … … 860 939 ip_create_last_header(last_header, header); 861 940 862 / / trim the unused space941 /* Trim the unused space */ 863 942 rc = packet_trim(new_packet, 0, 864 943 IP_HEADER_LENGTH(header) - IP_HEADER_LENGTH(last_header)); … … 866 945 return ip_release_and_return(packet, rc); 867 946 868 / / biggest multiple of 8 lower than content947 /* Greatest multiple of 8 lower than content */ 869 948 // TODO even fragmentation? 870 949 length = length & ~0x7; … … 878 957 return ip_release_and_return(packet, rc); 879 958 880 / / mark the first as fragmented959 /* Mark the first as fragmented */ 881 960 header->flags |= IPFLAG_MORE_FRAGMENTS; 882 961 883 / / create middle framgents962 /* Create middle fragments */ 884 963 while (IP_TOTAL_LENGTH(header) > length) { 885 964 new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, … … 902 981 } 903 982 904 / / finish the first fragment983 /* Finish the first fragment */ 905 984 header->header_checksum = IP_HEADER_CHECKSUM(header); 906 985 … … 919 998 * @param[in] addr_len The minimum address length. 920 999 * @param[in] error The error module service. 921 * @return sThe packet or the packet queue of the allowed length.922 * @return sNULL if there are no packets left.923 */ 924 static packet_t 925 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, 926 1005 socklen_t addr_len, services_t error) 927 1006 { 928 1007 size_t length; 929 packet_t next;930 packet_t new_packet;1008 packet_t *next; 1009 packet_t *new_packet; 931 1010 int result; 932 1011 int phone; 933 1012 934 1013 next = packet; 935 / / check all packets1014 /* Check all packets */ 936 1015 while (next) { 937 1016 length = packet_get_data_length(next); … … 942 1021 } 943 1022 944 / / too long1023 /* Too long */ 945 1024 result = ip_fragment_packet(next, content, prefix, 946 1025 suffix, addr_len); … … 948 1027 new_packet = pq_detach(next); 949 1028 if (next == packet) { 950 / / the new first packet of the queue1029 /* The new first packet of the queue */ 951 1030 packet = new_packet; 952 1031 } 953 / / fragmentation needed?1032 /* Fragmentation needed? */ 954 1033 if (result == EPERM) { 955 1034 phone = ip_prepare_icmp_and_get_phone( 956 1035 error, next, NULL); 957 1036 if (phone >= 0) { 958 / / fragmentation necessary ICMP1037 /* Fragmentation necessary ICMP */ 959 1038 icmp_destination_unreachable_msg(phone, 960 1039 ICMP_FRAG_NEEDED, content, next); … … 986 1065 * @param[in] dest The destination address. 987 1066 * @param[in] error The error module service. 988 * @return sEOK on success.989 * @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() 990 1069 * function. 991 * @return sOther error codes as defined for the ip_prepare_packet()1070 * @return Other error codes as defined for the ip_prepare_packet() 992 1071 * function. 993 1072 */ 994 static int 995 ip_send_route(packet_t packet, ip_netif_ref netif, ip_route_ref route, 996 in_addr_t *src, in_addr_t dest, services_t error) 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) 997 1075 { 998 1076 measured_string_t destination; 999 measured_string_ reftranslation;1000 char*data;1077 measured_string_t *translation; 1078 uint8_t *data; 1001 1079 int phone; 1002 1080 int rc; 1003 1081 1004 / / get destination hardware address1082 /* Get destination hardware address */ 1005 1083 if (netif->arp && (route->address.s_addr != dest.s_addr)) { 1006 1084 destination.value = route->gateway.s_addr ? 1007 ( char *) &route->gateway.s_addr : (char*) &dest.s_addr;1008 destination.length = CONVERT_SIZE(dest.s_addr, char, 1);1085 (uint8_t *) &route->gateway.s_addr : (uint8_t *) &dest.s_addr; 1086 destination.length = sizeof(dest.s_addr); 1009 1087 1010 1088 rc = arp_translate_req(netif->arp->phone, netif->device_id, … … 1024 1102 NULL); 1025 1103 if (phone >= 0) { 1026 / / unreachable ICMP if no routing1104 /* Unreachable ICMP if no routing */ 1027 1105 icmp_destination_unreachable_msg(phone, 1028 1106 ICMP_HOST_UNREACH, 0, packet); … … 1057 1135 } 1058 1136 1059 /** Searches the network interfaces if there is a suitable route. 1060 * 1061 * @param[in] netif The network interface to be searched for routes. May be 1062 * NULL. 1063 * @param[in] destination The destination address. 1064 * @returns The found route. 1065 * @returns NULL if no route was found. 1066 */ 1067 static ip_route_ref 1068 ip_netif_find_route(ip_netif_ref netif, in_addr_t destination) 1069 { 1070 int index; 1071 ip_route_ref route; 1072 1073 if (!netif) 1074 return NULL; 1075 1076 // start with the first one - the direct route 1077 for (index = 0; index < ip_routes_count(&netif->routes); index++) { 1078 route = ip_routes_get_index(&netif->routes, index); 1079 if (route && 1080 ((route->address.s_addr & route->netmask.s_addr) == 1081 (destination.s_addr & route->netmask.s_addr))) { 1082 return route; 1083 } 1084 } 1085 1086 return NULL; 1087 } 1088 1089 /** Searches all network interfaces if there is a suitable route. 1090 * 1091 * @param[in] destination The destination address. 1092 * @returns The found route. 1093 * @returns NULL if no route was found. 1094 */ 1095 static ip_route_ref ip_find_route(in_addr_t destination) { 1096 int index; 1097 ip_route_ref route; 1098 ip_netif_ref netif; 1099 1100 // start with the last netif - the newest one 1101 index = ip_netifs_count(&ip_globals.netifs) - 1; 1102 while (index >= 0) { 1103 netif = ip_netifs_get_index(&ip_globals.netifs, index); 1104 if (netif && (netif->state == NETIF_ACTIVE)) { 1105 route = ip_netif_find_route(netif, destination); 1106 if (route) 1107 return route; 1108 } 1109 index--; 1110 } 1111 1112 return &ip_globals.gateway; 1113 } 1114 1115 /** Returns the network interface's IP address. 1116 * 1117 * @param[in] netif The network interface. 1118 * @returns The IP address. 1119 * @returns NULL if no IP address was found. 1120 */ 1121 static in_addr_t *ip_netif_address(ip_netif_ref netif) 1122 { 1123 ip_route_ref route; 1124 1125 route = ip_routes_get_index(&netif->routes, 0); 1126 return route ? &route->address : NULL; 1127 } 1128 1129 /** Registers the transport layer protocol. 1130 * 1131 * The traffic of this protocol will be supplied using either the receive 1132 * function or IPC message. 1133 * 1134 * @param[in] protocol The transport layer module protocol. 1135 * @param[in] service The transport layer module service. 1136 * @param[in] phone The transport layer module phone. 1137 * @param[in] received_msg The receiving function. 1138 * @returns EOK on success. 1139 * @returns EINVAL if the protocol parameter and/or the service 1140 * parameter is zero. 1141 * @returns EINVAL if the phone parameter is not a positive number 1142 * and the tl_receive_msg is NULL. 1143 * @returns ENOMEM if there is not enough memory left. 1144 */ 1145 static int 1146 ip_register(int protocol, services_t service, int phone, 1147 tl_received_msg_t received_msg) 1148 { 1149 ip_proto_ref proto; 1150 int index; 1151 1152 if (!protocol || !service || ((phone < 0) && !received_msg)) 1153 return EINVAL; 1154 1155 proto = (ip_proto_ref) malloc(sizeof(ip_protos_t)); 1156 if (!proto) 1157 return ENOMEM; 1158 1159 proto->protocol = protocol; 1160 proto->service = service; 1161 proto->phone = phone; 1162 proto->received_msg = received_msg; 1163 1164 fibril_rwlock_write_lock(&ip_globals.protos_lock); 1165 index = ip_protos_add(&ip_globals.protos, proto->protocol, proto); 1166 if (index < 0) { 1167 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1168 free(proto); 1169 return index; 1170 } 1171 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1172 1173 printf("%s: Protocol registered (protocol: %d, phone: %d)\n", 1174 NAME, proto->protocol, proto->phone); 1175 1176 return EOK; 1177 } 1178 1179 static int 1180 ip_device_req_local(int il_phone, device_id_t device_id, services_t netif) 1181 { 1182 ip_netif_ref ip_netif; 1183 ip_route_ref route; 1184 int index; 1185 int rc; 1186 1187 ip_netif = (ip_netif_ref) malloc(sizeof(ip_netif_t)); 1188 if (!ip_netif) 1189 return ENOMEM; 1190 1191 rc = ip_routes_initialize(&ip_netif->routes); 1192 if (rc != EOK) { 1193 free(ip_netif); 1194 return rc; 1195 } 1196 1197 ip_netif->device_id = device_id; 1198 ip_netif->service = netif; 1199 ip_netif->state = NETIF_STOPPED; 1200 1201 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1202 1203 rc = ip_netif_initialize(ip_netif); 1204 if (rc != EOK) { 1205 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1206 ip_routes_destroy(&ip_netif->routes); 1207 free(ip_netif); 1208 return rc; 1209 } 1210 if (ip_netif->arp) 1211 ip_netif->arp->usage++; 1212 1213 // print the settings 1214 printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n", 1215 NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv, 1216 ip_netif->dhcp ? "dhcp" : "static"); 1217 1218 // TODO ipv6 addresses 1219 1220 char address[INET_ADDRSTRLEN]; 1221 char netmask[INET_ADDRSTRLEN]; 1222 char gateway[INET_ADDRSTRLEN]; 1223 1224 for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) { 1225 route = ip_routes_get_index(&ip_netif->routes, index); 1226 if (route) { 1227 inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, 1228 address, INET_ADDRSTRLEN); 1229 inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, 1230 netmask, INET_ADDRSTRLEN); 1231 inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, 1232 gateway, INET_ADDRSTRLEN); 1233 printf("%s: Route %d (address: %s, netmask: %s, " 1234 "gateway: %s)\n", NAME, index, address, netmask, 1235 gateway); 1236 } 1237 } 1238 1239 inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address, 1240 INET_ADDRSTRLEN); 1241 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1242 1243 printf("%s: Broadcast (%s)\n", NAME, address); 1244 1245 return EOK; 1246 } 1247 1248 static int 1249 ip_send_msg_local(int il_phone, device_id_t device_id, packet_t packet, 1250 services_t sender, services_t error) 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) 1251 1139 { 1252 1140 int addrlen; 1253 ip_netif_ refnetif;1254 ip_route_ refroute;1141 ip_netif_t *netif; 1142 ip_route_t *route; 1255 1143 struct sockaddr *addr; 1256 1144 struct sockaddr_in *address_in; … … 1260 1148 int rc; 1261 1149 1262 // addresses in the host byte order 1263 // should be the next hop address or the target destination address 1150 /* 1151 * Addresses in the host byte order 1152 * Should be the next hop address or the target destination address 1153 */ 1264 1154 addrlen = packet_get_addr(packet, NULL, (uint8_t **) &addr); 1265 1155 if (addrlen < 0) … … 1286 1176 fibril_rwlock_read_lock(&ip_globals.netifs_lock); 1287 1177 1288 / / device specified?1178 /* Device specified? */ 1289 1179 if (device_id > 0) { 1290 1180 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1291 route = ip_netif_find_route(netif, * dest);1181 route = ip_netif_find_route(netif, *dest); 1292 1182 if (netif && !route && (ip_globals.gateway.netif == netif)) 1293 1183 route = &ip_globals.gateway; … … 1302 1192 phone = ip_prepare_icmp_and_get_phone(error, packet, NULL); 1303 1193 if (phone >= 0) { 1304 / / unreachable ICMP if no routing1194 /* Unreachable ICMP if no routing */ 1305 1195 icmp_destination_unreachable_msg(phone, 1306 1196 ICMP_NET_UNREACH, 0, packet); … … 1310 1200 1311 1201 if (error) { 1312 // do not send for broadcast, anycast packets or network 1313 // broadcast 1202 /* 1203 * Do not send for broadcast, anycast packets or network 1204 * broadcast. 1205 */ 1314 1206 if (!dest->s_addr || !(~dest->s_addr) || 1315 1207 !(~((dest->s_addr & ~route->netmask.s_addr) | … … 1319 1211 } 1320 1212 } 1321 1322 / / if the local host is the destination1213 1214 /* If the local host is the destination */ 1323 1215 if ((route->address.s_addr == dest->s_addr) && 1324 1216 (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) { 1325 / / find the loopback device to deliver1217 /* Find the loopback device to deliver */ 1326 1218 dest->s_addr = IPV4_LOCALHOST_ADDRESS; 1327 1219 route = ip_find_route(*dest); … … 1332 1224 NULL); 1333 1225 if (phone >= 0) { 1334 / / unreachable ICMP if no routing1226 /* Unreachable ICMP if no routing */ 1335 1227 icmp_destination_unreachable_msg(phone, 1336 1228 ICMP_HOST_UNREACH, 0, packet); … … 1350 1242 1351 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; 1352 1496 } 1353 1497 … … 1361 1505 * @param[out] content The maximum content size. 1362 1506 * @param[out] suffix The minimum reserved suffix size. 1363 * @returns EOK on success. 1364 */ 1365 static int 1366 ip_packet_size_message(device_id_t device_id, size_t *addr_len, size_t *prefix, 1367 size_t *content, size_t *suffix) 1368 { 1369 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; 1370 1513 int index; 1371 1514 … … 1415 1558 } 1416 1559 1417 /** Returns the packet destination address from the IP header. 1418 * 1419 * @param[in] header The packet IP header to be read. 1420 * @returns The packet destination address. 1421 */ 1422 static in_addr_t ip_get_destination(ip_header_ref header) 1423 { 1424 in_addr_t destination; 1425 1426 // TODO search set ipopt route? 1427 destination.s_addr = header->destination_address; 1428 return destination; 1429 } 1430 1431 /** Delivers the packet to the local host. 1432 * 1433 * The packet is either passed to another module or released on error. 1434 * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not 1435 * found. 1436 * 1437 * @param[in] device_id The source device identifier. 1438 * @param[in] packet The packet to be delivered. 1439 * @param[in] header The first packet IP header. May be NULL. 1440 * @param[in] error The packet error service. 1441 * @returns EOK on success. 1442 * @returns ENOTSUP if the packet is a fragment. 1443 * @returns EAFNOSUPPORT if the address family is not supported. 1444 * @returns ENOENT if the target protocol is not found. 1445 * @returns Other error codes as defined for the packet_set_addr() 1446 * function. 1447 * @returns Other error codes as defined for the packet_trim() 1448 * function. 1449 * @returns Other error codes as defined for the protocol specific 1450 * tl_received_msg() function. 1451 */ 1452 static int 1453 ip_deliver_local(device_id_t device_id, packet_t packet, ip_header_ref header, 1454 services_t error) 1455 { 1456 ip_proto_ref proto; 1457 int phone; 1458 services_t service; 1459 tl_received_msg_t received_msg; 1460 struct sockaddr *src; 1461 struct sockaddr *dest; 1462 struct sockaddr_in src_in; 1463 struct sockaddr_in dest_in; 1464 socklen_t addrlen; 1465 int rc; 1466 1467 if ((header->flags & IPFLAG_MORE_FRAGMENTS) || 1468 IP_FRAGMENT_OFFSET(header)) { 1469 // TODO fragmented 1470 return ENOTSUP; 1471 } 1472 1473 switch (header->version) { 1474 case IPVERSION: 1475 addrlen = sizeof(src_in); 1476 bzero(&src_in, addrlen); 1477 src_in.sin_family = AF_INET; 1478 memcpy(&dest_in, &src_in, addrlen); 1479 memcpy(&src_in.sin_addr.s_addr, &header->source_address, 1480 sizeof(header->source_address)); 1481 memcpy(&dest_in.sin_addr.s_addr, &header->destination_address, 1482 sizeof(header->destination_address)); 1483 src = (struct sockaddr *) &src_in; 1484 dest = (struct sockaddr *) &dest_in; 1485 break; 1486 1487 default: 1488 return ip_release_and_return(packet, EAFNOSUPPORT); 1489 } 1490 1491 rc = packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest, 1492 addrlen); 1493 if (rc != EOK) 1494 return ip_release_and_return(packet, rc); 1495 1496 // trim padding if present 1497 if (!error && 1498 (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) { 1499 rc = packet_trim(packet, 0, 1500 packet_get_data_length(packet) - IP_TOTAL_LENGTH(header)); 1501 if (rc != EOK) 1502 return ip_release_and_return(packet, rc); 1503 } 1504 1505 fibril_rwlock_read_lock(&ip_globals.protos_lock); 1506 1507 proto = ip_protos_find(&ip_globals.protos, header->protocol); 1508 if (!proto) { 1509 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1510 phone = ip_prepare_icmp_and_get_phone(error, packet, header); 1511 if (phone >= 0) { 1512 // unreachable ICMP 1513 icmp_destination_unreachable_msg(phone, 1514 ICMP_PROT_UNREACH, 0, packet); 1515 } 1516 return ENOENT; 1517 } 1518 1519 if (proto->received_msg) { 1520 service = proto->service; 1521 received_msg = proto->received_msg; 1522 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1523 rc = received_msg(device_id, packet, service, error); 1524 } else { 1525 rc = tl_received_msg(proto->phone, device_id, packet, 1526 proto->service, error); 1527 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1528 } 1529 1530 return rc; 1531 } 1532 1533 /** Processes the received packet. 1534 * 1535 * The packet is either passed to another module or released on error. 1536 * 1537 * The ICMP_PARAM_POINTER error notification may be sent if the checksum is 1538 * invalid. 1539 * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two. 1540 * The ICMP_HOST_UNREACH error notification may be sent if no route was found. 1541 * The ICMP_HOST_UNREACH error notification may be sent if the packet is for 1542 * another host and the routing is disabled. 1543 * 1544 * @param[in] device_id The source device identifier. 1545 * @param[in] packet The received packet to be processed. 1546 * @returns EOK on success. 1547 * @returns EINVAL if the TTL is less than two. 1548 * @returns EINVAL if the checksum is invalid. 1549 * @returns EAFNOSUPPORT if the address family is not supported. 1550 * @returns ENOENT if no route was found. 1551 * @returns ENOENT if the packet is for another host and the routing 1552 * is disabled. 1553 */ 1554 static int 1555 ip_process_packet(device_id_t device_id, packet_t packet) 1556 { 1557 ip_header_ref header; 1558 in_addr_t dest; 1559 ip_route_ref route; 1560 int phone; 1561 struct sockaddr *addr; 1562 struct sockaddr_in addr_in; 1563 socklen_t addrlen; 1564 int rc; 1565 1566 header = (ip_header_ref) packet_get_data(packet); 1567 if (!header) 1568 return ip_release_and_return(packet, ENOMEM); 1569 1570 // checksum 1571 if ((header->header_checksum) && 1572 (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) { 1573 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1574 if (phone >= 0) { 1575 // checksum error ICMP 1576 icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER, 1577 ((size_t) ((void *) &header->header_checksum)) - 1578 ((size_t) ((void *) header)), packet); 1579 } 1580 return EINVAL; 1581 } 1582 1583 if (header->ttl <= 1) { 1584 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1585 if (phone >= 0) { 1586 // ttl exceeded ICMP 1587 icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet); 1588 } 1589 return EINVAL; 1590 } 1591 1592 // process ipopt and get destination 1593 dest = ip_get_destination(header); 1594 1595 // set the addrination address 1596 switch (header->version) { 1597 case IPVERSION: 1598 addrlen = sizeof(addr_in); 1599 bzero(&addr_in, addrlen); 1600 addr_in.sin_family = AF_INET; 1601 memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest)); 1602 addr = (struct sockaddr *) &addr_in; 1603 break; 1604 1605 default: 1606 return ip_release_and_return(packet, EAFNOSUPPORT); 1607 } 1608 1609 rc = packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen); 1610 if (rc != EOK) 1611 return rc; 1612 1613 route = ip_find_route(dest); 1614 if (!route) { 1615 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1616 if (phone >= 0) { 1617 // unreachable ICMP 1618 icmp_destination_unreachable_msg(phone, 1619 ICMP_HOST_UNREACH, 0, packet); 1620 } 1621 return ENOENT; 1622 } 1623 1624 if (route->address.s_addr == dest.s_addr) { 1625 // local delivery 1626 return ip_deliver_local(device_id, packet, header, 0); 1627 } 1628 1629 if (route->netif->routing) { 1630 header->ttl--; 1631 return ip_send_route(packet, route->netif, route, NULL, dest, 1632 0); 1633 } 1634 1635 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1636 if (phone >= 0) { 1637 // unreachable ICMP if no routing 1638 icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, 1639 packet); 1640 } 1641 1642 return ENOENT; 1643 } 1644 1645 static int 1646 ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address, 1647 in_addr_t netmask, in_addr_t gateway) 1648 { 1649 ip_route_ref route; 1650 ip_netif_ref netif; 1651 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; 1652 1570 1653 1571 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1654 1655 1572 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1656 1573 if (!netif) { … … 1658 1575 return ENOENT; 1659 1576 } 1660 1661 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)); 1662 1700 if (!route) { 1663 1701 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); … … 1681 1719 ip_set_gateway_req_local(int ip_phone, device_id_t device_id, in_addr_t gateway) 1682 1720 { 1683 ip_netif_ refnetif;1721 ip_netif_t *netif; 1684 1722 1685 1723 fibril_rwlock_write_lock(&ip_globals.netifs_lock); … … 1715 1753 static int 1716 1754 ip_received_error_msg_local(int ip_phone, device_id_t device_id, 1717 packet_t packet, services_t target, services_t error)1755 packet_t *packet, services_t target, services_t error) 1718 1756 { 1719 1757 uint8_t *data; … … 1721 1759 icmp_type_t type; 1722 1760 icmp_code_t code; 1723 ip_netif_ refnetif;1761 ip_netif_t *netif; 1724 1762 measured_string_t address; 1725 ip_route_ refroute;1726 ip_header_ refheader;1763 ip_route_t *route; 1764 ip_header_t *header; 1727 1765 1728 1766 switch (error) { … … 1734 1772 1735 1773 data = packet_get_data(packet); 1736 header = (ip_header_ ref)(data + offset);1737 1738 / / destination host unreachable?1774 header = (ip_header_t *)(data + offset); 1775 1776 /* Destination host unreachable? */ 1739 1777 if ((type != ICMP_DEST_UNREACH) || 1740 1778 (code != ICMP_HOST_UNREACH)) { 1741 // no, something else1779 /* No, something else */ 1742 1780 break; 1743 1781 } … … 1753 1791 route = ip_routes_get_index(&netif->routes, 0); 1754 1792 1755 / / from the same network?1793 /* From the same network? */ 1756 1794 if (route && ((route->address.s_addr & route->netmask.s_addr) == 1757 1795 (header->destination_address & route->netmask.s_addr))) { 1758 // clear the ARP mapping if any 1759 address.value = (char *) &header->destination_address; 1760 address.length = CONVERT_SIZE(uint8_t, char, 1761 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); 1762 1799 arp_clear_address_req(netif->arp->phone, 1763 1800 netif->device_id, SERVICE_IP, &address); … … 1782 1819 in_addr_t *dest; 1783 1820 in_addr_t *src; 1784 ip_route_ refroute;1785 ipv4_pseudo_header_ refheader_in;1821 ip_route_t *route; 1822 ipv4_pseudo_header_t *header_in; 1786 1823 1787 1824 if (!destination || (addrlen <= 0)) … … 1811 1848 fibril_rwlock_read_lock(&ip_globals.lock); 1812 1849 route = ip_find_route(*dest); 1813 / / if the local host is the destination1850 /* If the local host is the destination */ 1814 1851 if (route && (route->address.s_addr == dest->s_addr) && 1815 1852 (dest->s_addr != IPV4_LOCALHOST_ADDRESS)) { 1816 / / find the loopback device to deliver1853 /* Find the loopback device to deliver */ 1817 1854 dest->s_addr = IPV4_LOCALHOST_ADDRESS; 1818 1855 route = ip_find_route(*dest); … … 1829 1866 1830 1867 *headerlen = sizeof(*header_in); 1831 header_in = (ipv4_pseudo_header_ ref) malloc(*headerlen);1868 header_in = (ipv4_pseudo_header_t *) malloc(*headerlen); 1832 1869 if (!header_in) 1833 1870 return ENOMEM; … … 1843 1880 } 1844 1881 1845 /** Processes the received IP packet or the packet queue one by one.1846 *1847 * The packet is either passed to another module or released on error.1848 *1849 * @param[in] device_id The source device identifier.1850 * @param[in,out] packet The received packet.1851 * @returns EOK on success and the packet is no longer needed.1852 * @returns EINVAL if the packet is too small to carry the IP1853 * packet.1854 * @returns EINVAL if the received address lengths differs from the1855 * registered values.1856 * @returns ENOENT if the device is not found in the cache.1857 * @returns ENOENT if the protocol for the device is not found in1858 * the cache.1859 * @returns ENOMEM if there is not enough memory left.1860 */1861 static int ip_receive_message(device_id_t device_id, packet_t packet)1862 {1863 packet_t next;1864 1865 do {1866 next = pq_detach(packet);1867 ip_process_packet(device_id, packet);1868 packet = next;1869 } while (packet);1870 1871 return EOK;1872 }1873 1874 1882 /** Processes the IP message. 1875 1883 * … … 1879 1887 * @param[out] answer_count The last parameter for the actual answer in the 1880 1888 * answer parameter. 1881 * @return sEOK on success.1882 * @return sENOTSUP if the message is not known.1889 * @return EOK on success. 1890 * @return ENOTSUP if the message is not known. 1883 1891 * 1884 1892 * @see ip_interface.h 1885 * @see il_ interface.h1893 * @see il_remote.h 1886 1894 * @see IS_NET_IP_MESSAGE() 1887 1895 */ 1888 int 1889 ip_message_standalone(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 1890 int *answer_count) 1891 { 1892 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; 1893 1900 struct sockaddr *addr; 1901 void *header; 1902 size_t headerlen; 1894 1903 size_t addrlen; 1895 1904 size_t prefix; 1896 1905 size_t suffix; 1897 1906 size_t content; 1898 void *header;1899 size_t headerlen;1900 1907 device_id_t device_id; 1901 1908 int rc; 1902 1909 1903 1910 *answer_count = 0; 1904 switch (IPC_GET_ METHOD(*call)) {1911 switch (IPC_GET_IMETHOD(*call)) { 1905 1912 case IPC_M_PHONE_HUNGUP: 1906 1913 return EOK; 1907 1914 1908 1915 case IPC_M_CONNECT_TO_ME: 1909 return ip_register(IL_GET_PROTO( call), IL_GET_SERVICE(call),1910 IPC_GET_PHONE( call), NULL);1911 1912 case NET_I L_DEVICE:1913 return ip_device_req_local(0, IPC_GET_DEVICE( call),1914 IPC_GET_SERVICE( call));1915 1916 case NET_I L_SEND: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)); 1922 1923 case NET_IP_RECEIVED_ERROR: 1917 1924 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1918 IPC_GET_PACKET( call));1925 IPC_GET_PACKET(*call)); 1919 1926 if (rc != EOK) 1920 1927 return rc; 1921 return ip_send_msg_local(0, IPC_GET_DEVICE(call), packet, 0, 1922 IPC_GET_ERROR(call)); 1923 1924 case NET_IL_DEVICE_STATE: 1925 return ip_device_state_message(IPC_GET_DEVICE(call), 1926 IPC_GET_STATE(call)); 1927 1928 case NET_IL_RECEIVED: 1929 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1930 IPC_GET_PACKET(call)); 1931 if (rc != EOK) 1932 return rc; 1933 return ip_receive_message(IPC_GET_DEVICE(call), packet); 1934 1935 case NET_IP_RECEIVED_ERROR: 1936 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1937 IPC_GET_PACKET(call)); 1938 if (rc != EOK) 1939 return rc; 1940 return ip_received_error_msg_local(0, IPC_GET_DEVICE(call), 1941 packet, IPC_GET_TARGET(call), IPC_GET_ERROR(call)); 1928 return ip_received_error_msg_local(0, IPC_GET_DEVICE(*call), 1929 packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call)); 1942 1930 1943 1931 case NET_IP_ADD_ROUTE: 1944 return ip_add_route_req_local(0, IPC_GET_DEVICE( call),1945 IP_GET_ADDRESS( call), IP_GET_NETMASK(call),1946 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)); 1947 1935 1948 1936 case NET_IP_SET_GATEWAY: 1949 return ip_set_gateway_req_local(0, IPC_GET_DEVICE( call),1950 IP_GET_GATEWAY( call));1937 return ip_set_gateway_req_local(0, IPC_GET_DEVICE(*call), 1938 IP_GET_GATEWAY(*call)); 1951 1939 1952 1940 case NET_IP_GET_ROUTE: 1953 rc = data_receive((void **) &addr, &addrlen); 1941 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, 1942 &addrlen); 1954 1943 if (rc != EOK) 1955 1944 return rc; 1956 1945 1957 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL( call), addr,1946 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL(*call), addr, 1958 1947 (socklen_t) addrlen, &device_id, &header, &headerlen); 1959 1948 if (rc != EOK) 1960 1949 return rc; 1961 1950 1962 IPC_SET_DEVICE( answer, device_id);1963 IP_SET_HEADERLEN( answer, headerlen);1951 IPC_SET_DEVICE(*answer, device_id); 1952 IP_SET_HEADERLEN(*answer, headerlen); 1964 1953 1965 1954 *answer_count = 2; … … 1972 1961 return rc; 1973 1962 1974 case NET_I L_PACKET_SPACE:1975 rc = ip_packet_size_message(IPC_GET_DEVICE( call), &addrlen,1963 case NET_IP_PACKET_SPACE: 1964 rc = ip_packet_size_message(IPC_GET_DEVICE(*call), &addrlen, 1976 1965 &prefix, &content, &suffix); 1977 1966 if (rc != EOK) 1978 1967 return rc; 1979 1968 1980 IPC_SET_ADDR( answer, addrlen);1981 IPC_SET_PREFIX( answer, prefix);1982 IPC_SET_CONTENT( answer, content);1983 IPC_SET_SUFFIX( answer, suffix);1969 IPC_SET_ADDR(*answer, addrlen); 1970 IPC_SET_PREFIX(*answer, prefix); 1971 IPC_SET_CONTENT(*answer, content); 1972 IPC_SET_SUFFIX(*answer, suffix); 1984 1973 *answer_count = 4; 1985 1974 return EOK; 1986 1975 1987 case NET_IL_MTU_CHANGED: 1988 return ip_mtu_changed_message(IPC_GET_DEVICE(call), 1989 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)); 1990 1984 } 1991 1985 … … 1993 1987 } 1994 1988 1995 /** Default thread for new connections.1996 *1997 * @param[in] iid The initial message identifier.1998 * @param[in] icall The initial message call structure.1999 */2000 static void il_client_connection(ipc_callid_t iid, ipc_call_t *icall)2001 {2002 /*2003 * Accept the connection2004 * - Answer the first IPC_M_CONNECT_ME_TO call.2005 */2006 ipc_answer_0(iid, EOK);2007 2008 while (true) {2009 ipc_call_t answer;2010 int answer_count;2011 2012 /* Clear the answer structure */2013 refresh_answer(&answer, &answer_count);2014 2015 /* Fetch the next message */2016 ipc_call_t call;2017 ipc_callid_t callid = async_get_call(&call);2018 2019 /* Process the message */2020 int res = il_module_message_standalone(callid, &call, &answer,2021 &answer_count);2022 2023 /*2024 * End if told to either by the message or the processing2025 * result.2026 */2027 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||2028 (res == EHANGUP)) {2029 return;2030 }2031 2032 /* Answer the message */2033 answer_call(callid, res, &answer, answer_count);2034 }2035 }2036 2037 /** Starts the module.2038 *2039 * @returns EOK on success.2040 * @returns Other error codes as defined for each specific module start function.2041 */2042 1989 int main(int argc, char *argv[]) 2043 1990 { 2044 int rc;2045 2046 1991 /* Start the module */ 2047 rc = il_module_start_standalone(il_client_connection); 2048 return rc; 1992 return il_module_start(SERVICE_IP); 2049 1993 } 2050 1994
Note:
See TracChangeset
for help on using the changeset viewer.
