Changeset 80cd7cd in mainline for uspace/srv/net
- Timestamp:
- 2011-01-13T20:58:24Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 87e373b
- Parents:
- eaef141 (diff), a613fea1 (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. - Location:
- uspace/srv/net
- Files:
-
- 12 deleted
- 22 edited
- 2 moved
-
cfg/lo (moved) (moved from uspace/srv/net/cfg/lo.netif_standalone )
-
cfg/ne2k (moved) (moved from uspace/srv/net/cfg/ne2k.netif_standalone ) (2 diffs)
-
il/arp/Makefile (modified) (1 diff)
-
il/arp/arp.c (modified) (22 diffs)
-
il/arp/arp.h (modified) (4 diffs)
-
il/arp/arp_header.h (deleted)
-
il/arp/arp_module.c (deleted)
-
il/arp/arp_oc.h (deleted)
-
il/ip/Makefile (modified) (1 diff)
-
il/ip/ip.c (modified) (33 diffs)
-
il/ip/ip.h (modified) (1 diff)
-
il/ip/ip_module.c (deleted)
-
il/ip/ip_module.h (deleted)
-
net/net.c (modified) (31 diffs)
-
net/net.h (modified) (3 diffs)
-
net/net_standalone.c (modified) (2 diffs)
-
netif/lo/lo.c (modified) (7 diffs)
-
nil/eth/Makefile (modified) (1 diff)
-
nil/eth/eth.c (modified) (15 diffs)
-
nil/eth/eth.h (modified) (2 diffs)
-
nil/eth/eth_header.h (deleted)
-
nil/nildummy/Makefile (modified) (1 diff)
-
nil/nildummy/nildummy.c (modified) (21 diffs)
-
nil/nildummy/nildummy.h (modified) (4 diffs)
-
nil/nildummy/nildummy_module.c (deleted)
-
tl/icmp/Makefile (modified) (1 diff)
-
tl/icmp/icmp.c (modified) (9 diffs)
-
tl/icmp/icmp_module.c (deleted)
-
tl/icmp/icmp_module.h (deleted)
-
tl/tcp/Makefile (modified) (1 diff)
-
tl/tcp/tcp.c (modified) (13 diffs)
-
tl/tcp/tcp_module.h (deleted)
-
tl/udp/Makefile (modified) (1 diff)
-
tl/udp/udp.c (modified) (9 diffs)
-
tl/udp/udp_module.c (deleted)
-
tl/udp/udp_module.h (deleted)
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/cfg/ne2k
reaef141 r80cd7cd 3 3 NAME=ne2k 4 4 5 NETIF= dp83905 NETIF=ne2000 6 6 NIL=eth 7 7 IL=ip 8 8 9 IRQ= 99 IRQ=5 10 10 IO=300 11 11 … … 17 17 IP_ADDR=10.0.2.15 18 18 IP_ROUTING=yes 19 IP_NETMASK=255.255.255. 24019 IP_NETMASK=255.255.255.0 20 20 IP_BROADCAST=10.0.2.255 21 21 IP_GATEWAY=10.0.2.2 22 22 ARP=arp 23 23 24 MTU=1 49224 MTU=1500 -
uspace/srv/net/il/arp/Makefile
reaef141 r80cd7cd 34 34 35 35 SOURCES = \ 36 arp.c \ 37 arp_module.c 36 arp.c 38 37 39 38 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/il/arp/arp.c
reaef141 r80cd7cd 35 35 * @see arp.h 36 36 */ 37 38 #include "arp.h"39 #include "arp_header.h"40 #include "arp_oc.h"41 #include "arp_module.h"42 37 43 38 #include <async.h> … … 54 49 #include <ipc/arp.h> 55 50 #include <ipc/il.h> 51 #include <ipc/nil.h> 56 52 #include <byteorder.h> 57 53 #include <errno.h> 58 59 54 #include <net/modules.h> 60 55 #include <net/device.h> 61 56 #include <net/packet.h> 62 63 #include <nil_interface.h> 57 #include <nil_remote.h> 64 58 #include <protocol_map.h> 65 59 #include <packet_client.h> 66 60 #include <packet_remote.h> 67 #include <il_ interface.h>68 #include <il_ local.h>69 61 #include <il_remote.h> 62 #include <il_skel.h> 63 #include "arp.h" 70 64 71 65 /** ARP module name. */ … … 73 67 74 68 /** Number of microseconds to wait for an ARP reply. */ 75 #define ARP_TRANS_WAIT 1000000 69 #define ARP_TRANS_WAIT 1000000 70 71 /** @name ARP operation codes definitions */ 72 /*@{*/ 73 74 /** REQUEST operation code. */ 75 #define ARPOP_REQUEST 1 76 77 /** REPLY operation code. */ 78 #define ARPOP_REPLY 2 79 80 /*@}*/ 81 82 /** Type definition of an ARP protocol header. 83 * @see arp_header 84 */ 85 typedef struct arp_header arp_header_t; 86 87 /** ARP protocol header. */ 88 struct arp_header { 89 /** 90 * Hardware type identifier. 91 * @see hardware.h 92 */ 93 uint16_t hardware; 94 95 /** Protocol identifier. */ 96 uint16_t protocol; 97 /** Hardware address length in bytes. */ 98 uint8_t hardware_length; 99 /** Protocol address length in bytes. */ 100 uint8_t protocol_length; 101 102 /** 103 * ARP packet type. 104 * @see arp_oc.h 105 */ 106 uint16_t operation; 107 } __attribute__ ((packed)); 76 108 77 109 /** ARP global data. */ … … 88 120 trans->hw_addr = NULL; 89 121 } 122 90 123 fibril_condvar_broadcast(&trans->cv); 91 124 } … … 94 127 { 95 128 int count; 96 arp_trans_t *trans; 97 129 98 130 for (count = arp_addr_count(addresses) - 1; count >= 0; count--) { 99 trans = arp_addr_items_get_index(&addresses->values, count); 131 arp_trans_t *trans = arp_addr_items_get_index(&addresses->values, 132 count); 100 133 if (trans) 101 134 arp_clear_trans(trans); … … 103 136 } 104 137 105 106 /** Clears the device specific data. 107 * 108 * @param[in] device The device specific data. 138 /** Clear the device specific data. 139 * 140 * @param[in] device Device specific data. 109 141 */ 110 142 static void arp_clear_device(arp_device_t *device) 111 143 { 112 144 int count; 113 arp_proto_t *proto; 114 145 115 146 for (count = arp_protos_count(&device->protos) - 1; count >= 0; 116 147 count--) { 117 proto = arp_protos_get_index(&device->protos, count); 148 arp_proto_t *proto = arp_protos_get_index(&device->protos, 149 count); 150 118 151 if (proto) { 119 152 if (proto->addr) 120 153 free(proto->addr); 154 121 155 if (proto->addr_data) 122 156 free(proto->addr_data); 157 123 158 arp_clear_addr(&proto->addresses); 124 159 arp_addr_destroy(&proto->addresses); 125 160 } 126 161 } 162 127 163 arp_protos_clear(&device->protos); 128 164 } … … 131 167 { 132 168 int count; 133 arp_device_t *device; 134 169 135 170 fibril_mutex_lock(&arp_globals.lock); 136 171 for (count = arp_cache_count(&arp_globals.cache) - 1; count >= 0; 137 172 count--) { 138 device = arp_cache_get_index(&arp_globals.cache, count); 173 arp_device_t *device = arp_cache_get_index(&arp_globals.cache, 174 count); 175 139 176 if (device) { 140 177 arp_clear_device(device); 141 178 if (device->addr_data) 142 179 free(device->addr_data); 180 143 181 if (device->broadcast_data) 144 182 free(device->broadcast_data); 145 183 } 146 184 } 185 147 186 arp_cache_clear(&arp_globals.cache); 148 187 fibril_mutex_unlock(&arp_globals.lock); 149 printf("Cache cleaned\n");188 150 189 return EOK; 151 190 } … … 154 193 services_t protocol, measured_string_t *address) 155 194 { 156 arp_device_t *device;157 arp_proto_t *proto;158 arp_trans_t *trans;159 160 195 fibril_mutex_lock(&arp_globals.lock); 161 device = arp_cache_find(&arp_globals.cache, device_id); 196 197 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id); 162 198 if (!device) { 163 199 fibril_mutex_unlock(&arp_globals.lock); 164 200 return ENOENT; 165 201 } 166 proto = arp_protos_find(&device->protos, protocol); 202 203 arp_proto_t *proto = arp_protos_find(&device->protos, protocol); 167 204 if (!proto) { 168 205 fibril_mutex_unlock(&arp_globals.lock); 169 206 return ENOENT; 170 207 } 171 trans = arp_addr_find(&proto->addresses, address->value, address->length); 208 209 arp_trans_t *trans = arp_addr_find(&proto->addresses, address->value, 210 address->length); 172 211 if (trans) 173 212 arp_clear_trans(trans); 213 174 214 arp_addr_exclude(&proto->addresses, address->value, address->length); 215 175 216 fibril_mutex_unlock(&arp_globals.lock); 176 217 return EOK; 177 218 } 178 219 179 180 220 static int arp_clear_device_req(int arp_phone, device_id_t device_id) 181 221 { 182 arp_device_t *device;183 184 222 fibril_mutex_lock(&arp_globals.lock); 185 device = arp_cache_find(&arp_globals.cache, device_id); 223 224 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id); 186 225 if (!device) { 187 226 fibril_mutex_unlock(&arp_globals.lock); 188 227 return ENOENT; 189 228 } 229 190 230 arp_clear_device(device); 191 printf("Device %d cleared\n", device_id);231 192 232 fibril_mutex_unlock(&arp_globals.lock); 193 233 return EOK; 194 234 } 195 235 196 /** Creates new protocol specific data. 197 * 198 * Allocates and returns the needed memory block as the proto parameter. 199 * 200 * @param[out] proto The allocated protocol specific data. 201 * @param[in] service The protocol module service. 202 * @param[in] address The actual protocol device address. 203 * @return EOK on success. 204 * @return ENOMEM if there is not enough memory left. 236 /** Create new protocol specific data. 237 * 238 * Allocate and return the needed memory block as the proto parameter. 239 * 240 * @param[out] proto Allocated protocol specific data. 241 * @param[in] service Protocol module service. 242 * @param[in] address Actual protocol device address. 243 * 244 * @return EOK on success. 245 * @return ENOMEM if there is not enough memory left. 246 * 205 247 */ 206 248 static int arp_proto_create(arp_proto_t **proto, services_t service, 207 249 measured_string_t *address) 208 250 { 209 int rc;210 211 251 *proto = (arp_proto_t *) malloc(sizeof(arp_proto_t)); 212 252 if (!*proto) … … 217 257 (*proto)->addr_data = address->value; 218 258 219 rc = arp_addr_initialize(&(*proto)->addresses);259 int rc = arp_addr_initialize(&(*proto)->addresses); 220 260 if (rc != EOK) { 221 261 free(*proto); … … 226 266 } 227 267 228 /** Registers the device. 229 * 230 * Creates new device entry in the cache or updates the protocol address if the 231 * device with the device identifier and the driver service exists. 232 * 233 * @param[in] device_id The device identifier. 234 * @param[in] service The device driver service. 235 * @param[in] protocol The protocol service. 236 * @param[in] address The actual device protocol address. 237 * @return EOK on success. 238 * @return EEXIST if another device with the same device identifier 239 * and different driver service exists. 240 * @return ENOMEM if there is not enough memory left. 241 * @return Other error codes as defined for the 242 * measured_strings_return() function. 243 */ 244 static int arp_device_message(device_id_t device_id, services_t service, 245 services_t protocol, measured_string_t *address) 246 { 247 arp_device_t *device; 248 arp_proto_t *proto; 249 hw_type_t hardware; 250 int index; 268 /** Process the received ARP packet. 269 * 270 * Update the source hardware address if the source entry exists or the packet 271 * is targeted to my protocol address. 272 * 273 * Respond to the ARP request if the packet is the ARP request and is 274 * targeted to my address. 275 * 276 * @param[in] device_id Source device identifier. 277 * @param[in,out] packet Received packet. 278 * 279 * @return EOK on success and the packet is no longer needed. 280 * @return One on success and the packet has been reused. 281 * @return EINVAL if the packet is too small to carry an ARP 282 * packet. 283 * @return EINVAL if the received address lengths differs from 284 * the registered values. 285 * @return ENOENT if the device is not found in the cache. 286 * @return ENOENT if the protocol for the device is not found in 287 * the cache. 288 * @return ENOMEM if there is not enough memory left. 289 * 290 */ 291 static int arp_receive_message(device_id_t device_id, packet_t *packet) 292 { 251 293 int rc; 252 253 fibril_mutex_lock(&arp_globals.lock); 254 255 /* An existing device? */ 256 device = arp_cache_find(&arp_globals.cache, device_id); 257 258 if (device) { 259 if (device->service != service) { 260 printf("Device %d already exists\n", device->device_id); 261 fibril_mutex_unlock(&arp_globals.lock); 262 return EEXIST; 263 } 264 proto = arp_protos_find(&device->protos, protocol); 265 if (proto) { 266 free(proto->addr); 267 free(proto->addr_data); 268 proto->addr = address; 269 proto->addr_data = address->value; 270 } else { 271 rc = arp_proto_create(&proto, protocol, address); 272 if (rc != EOK) { 273 fibril_mutex_unlock(&arp_globals.lock); 274 return rc; 275 } 276 index = arp_protos_add(&device->protos, proto->service, 277 proto); 278 if (index < 0) { 279 fibril_mutex_unlock(&arp_globals.lock); 280 free(proto); 281 return index; 282 } 283 printf("New protocol added:\n\tdevice id\t= " 284 "%d\n\tproto\t= %d", device_id, protocol); 285 } 286 } else { 287 hardware = hardware_map(service); 288 if (!hardware) 289 return ENOENT; 290 291 /* Create a new device */ 292 device = (arp_device_t *) malloc(sizeof(arp_device_t)); 293 if (!device) { 294 fibril_mutex_unlock(&arp_globals.lock); 295 return ENOMEM; 296 } 297 device->hardware = hardware; 298 device->device_id = device_id; 299 rc = arp_protos_initialize(&device->protos); 300 if (rc != EOK) { 301 fibril_mutex_unlock(&arp_globals.lock); 302 free(device); 303 return rc; 304 } 305 rc = arp_proto_create(&proto, protocol, address); 306 if (rc != EOK) { 307 fibril_mutex_unlock(&arp_globals.lock); 308 free(device); 309 return rc; 310 } 311 index = arp_protos_add(&device->protos, proto->service, proto); 312 if (index < 0) { 313 fibril_mutex_unlock(&arp_globals.lock); 314 arp_protos_destroy(&device->protos); 315 free(device); 316 return index; 317 } 318 device->service = service; 319 320 /* Bind the new one */ 321 device->phone = nil_bind_service(device->service, 322 (sysarg_t) device->device_id, SERVICE_ARP, 323 arp_globals.client_connection); 324 if (device->phone < 0) { 325 fibril_mutex_unlock(&arp_globals.lock); 326 arp_protos_destroy(&device->protos); 327 free(device); 328 return EREFUSED; 329 } 330 331 /* Get packet dimensions */ 332 rc = nil_packet_size_req(device->phone, device_id, 333 &device->packet_dimension); 334 if (rc != EOK) { 335 fibril_mutex_unlock(&arp_globals.lock); 336 arp_protos_destroy(&device->protos); 337 free(device); 338 return rc; 339 } 340 341 /* Get hardware address */ 342 rc = nil_get_addr_req(device->phone, device_id, &device->addr, 343 &device->addr_data); 344 if (rc != EOK) { 345 fibril_mutex_unlock(&arp_globals.lock); 346 arp_protos_destroy(&device->protos); 347 free(device); 348 return rc; 349 } 350 351 /* Get broadcast address */ 352 rc = nil_get_broadcast_addr_req(device->phone, device_id, 353 &device->broadcast_addr, &device->broadcast_data); 354 if (rc != EOK) { 355 fibril_mutex_unlock(&arp_globals.lock); 356 free(device->addr); 357 free(device->addr_data); 358 arp_protos_destroy(&device->protos); 359 free(device); 360 return rc; 361 } 362 363 rc = arp_cache_add(&arp_globals.cache, device->device_id, 364 device); 365 if (rc != EOK) { 366 fibril_mutex_unlock(&arp_globals.lock); 367 free(device->addr); 368 free(device->addr_data); 369 free(device->broadcast_addr); 370 free(device->broadcast_data); 371 arp_protos_destroy(&device->protos); 372 free(device); 373 return rc; 374 } 375 printf("%s: Device registered (id: %d, type: 0x%x, service: %d," 376 " proto: %d)\n", NAME, device->device_id, device->hardware, 377 device->service, protocol); 378 } 379 fibril_mutex_unlock(&arp_globals.lock); 380 381 return EOK; 382 } 383 384 /** Initializes the ARP module. 385 * 386 * @param[in] client_connection The client connection processing function. 387 * The module skeleton propagates its own one. 388 * @return EOK on success. 389 * @return ENOMEM if there is not enough memory left. 390 */ 391 int arp_initialize(async_client_conn_t client_connection) 392 { 393 int rc; 394 395 fibril_mutex_initialize(&arp_globals.lock); 396 fibril_mutex_lock(&arp_globals.lock); 397 arp_globals.client_connection = client_connection; 398 rc = arp_cache_initialize(&arp_globals.cache); 399 fibril_mutex_unlock(&arp_globals.lock); 400 401 return rc; 402 } 403 404 /** Updates the device content length according to the new MTU value. 405 * 406 * @param[in] device_id The device identifier. 407 * @param[in] mtu The new mtu value. 408 * @return ENOENT if device is not found. 409 * @return EOK on success. 410 */ 411 static int arp_mtu_changed_message(device_id_t device_id, size_t mtu) 412 { 413 arp_device_t *device; 414 415 fibril_mutex_lock(&arp_globals.lock); 416 device = arp_cache_find(&arp_globals.cache, device_id); 417 if (!device) { 418 fibril_mutex_unlock(&arp_globals.lock); 419 return ENOENT; 420 } 421 device->packet_dimension.content = mtu; 422 fibril_mutex_unlock(&arp_globals.lock); 423 printf("arp - device %d changed mtu to %zu\n\n", device_id, mtu); 424 return EOK; 425 } 426 427 /** Processes the received ARP packet. 428 * 429 * Updates the source hardware address if the source entry exists or the packet 430 * is targeted to my protocol address. 431 * Responses to the ARP request if the packet is the ARP request and is 432 * targeted to my address. 433 * 434 * @param[in] device_id The source device identifier. 435 * @param[in,out] packet The received packet. 436 * @return EOK on success and the packet is no longer needed. 437 * @return One on success and the packet has been reused. 438 * @return EINVAL if the packet is too small to carry an ARP 439 * packet. 440 * @return EINVAL if the received address lengths differs from 441 * the registered values. 442 * @return ENOENT if the device is not found in the cache. 443 * @return ENOENT if the protocol for the device is not found in 444 * the cache. 445 * @return ENOMEM if there is not enough memory left. 446 */ 447 static int arp_receive_message(device_id_t device_id, packet_t *packet) 448 { 449 size_t length; 450 arp_header_t *header; 451 arp_device_t *device; 452 arp_proto_t *proto; 453 arp_trans_t *trans; 454 uint8_t *src_hw; 455 uint8_t *src_proto; 456 uint8_t *des_hw; 457 uint8_t *des_proto; 458 int rc; 459 460 length = packet_get_data_length(packet); 294 295 size_t length = packet_get_data_length(packet); 461 296 if (length <= sizeof(arp_header_t)) 462 297 return EINVAL; 463 464 device = arp_cache_find(&arp_globals.cache, device_id);298 299 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id); 465 300 if (!device) 466 301 return ENOENT; 467 468 header = (arp_header_t *) packet_get_data(packet);302 303 arp_header_t *header = (arp_header_t *) packet_get_data(packet); 469 304 if ((ntohs(header->hardware) != device->hardware) || 470 305 (length < sizeof(arp_header_t) + header->hardware_length * 2U + … … 472 307 return EINVAL; 473 308 } 474 475 proto = arp_protos_find(&device->protos,309 310 arp_proto_t *proto = arp_protos_find(&device->protos, 476 311 protocol_unmap(device->service, ntohs(header->protocol))); 477 312 if (!proto) 478 313 return ENOENT; 479 480 src_hw = ((uint8_t *) header) + sizeof(arp_header_t); 481 src_proto = src_hw + header->hardware_length; 482 des_hw = src_proto + header->protocol_length; 483 des_proto = des_hw + header->hardware_length; 484 trans = arp_addr_find(&proto->addresses, (char *) src_proto, 314 315 uint8_t *src_hw = ((uint8_t *) header) + sizeof(arp_header_t); 316 uint8_t *src_proto = src_hw + header->hardware_length; 317 uint8_t *des_hw = src_proto + header->protocol_length; 318 uint8_t *des_proto = des_hw + header->hardware_length; 319 320 arp_trans_t *trans = arp_addr_find(&proto->addresses, src_proto, 485 321 header->protocol_length); 486 /* Exists? */ 487 if (trans && trans->hw_addr) { 322 323 if ((trans) && (trans->hw_addr)) { 324 /* Translation exists */ 488 325 if (trans->hw_addr->length != header->hardware_length) 489 326 return EINVAL; 327 490 328 memcpy(trans->hw_addr->value, src_hw, trans->hw_addr->length); 491 329 } 330 492 331 /* Is my protocol address? */ 493 332 if (proto->addr->length != header->protocol_length) 494 333 return EINVAL; 495 if (!str_lcmp(proto->addr->value, (char *) des_proto, 496 proto->addr->length)) { 497 /* Not already updated? */ 334 335 if (!bcmp(proto->addr->value, des_proto, proto->addr->length)) { 498 336 if (!trans) { 337 /* Update the translation */ 499 338 trans = (arp_trans_t *) malloc(sizeof(arp_trans_t)); 500 339 if (!trans) 501 340 return ENOMEM; 341 502 342 trans->hw_addr = NULL; 503 343 fibril_condvar_initialize(&trans->cv); 504 rc = arp_addr_add(&proto->addresses, (char *)src_proto,344 rc = arp_addr_add(&proto->addresses, src_proto, 505 345 header->protocol_length, trans); 506 346 if (rc != EOK) { … … 509 349 } 510 350 } 351 511 352 if (!trans->hw_addr) { 512 trans->hw_addr = measured_string_create_bulk( 513 (char *) src_hw,header->hardware_length);353 trans->hw_addr = measured_string_create_bulk(src_hw, 354 header->hardware_length); 514 355 if (!trans->hw_addr) 515 356 return ENOMEM; 516 357 517 358 /* Notify the fibrils that wait for the translation. */ 518 359 fibril_condvar_broadcast(&trans->cv); 519 360 } 361 520 362 if (ntohs(header->operation) == ARPOP_REQUEST) { 521 363 header->operation = htons(ARPOP_REPLY); … … 538 380 } 539 381 } 540 382 541 383 return EOK; 542 384 } 543 385 544 545 /** Returns the hardware address for the given protocol address. 546 * 547 * Sends the ARP request packet if the hardware address is not found in the 548 * cache. 549 * 550 * @param[in] device_id The device identifier. 551 * @param[in] protocol The protocol service. 552 * @param[in] target The target protocol address. 553 * @param[out] translation Where the hardware address of the target is stored. 554 * @return EOK on success. 555 * @return EAGAIN if the caller should try again. 556 * @return Other error codes in case of error. 557 */ 558 static int 559 arp_translate_message(device_id_t device_id, services_t protocol, 560 measured_string_t *target, measured_string_t **translation) 561 { 562 arp_device_t *device; 563 arp_proto_t *proto; 564 arp_trans_t *trans; 565 size_t length; 386 /** Update the device content length according to the new MTU value. 387 * 388 * @param[in] device_id Device identifier. 389 * @param[in] mtu New MTU value. 390 * 391 * @return ENOENT if device is not found. 392 * @return EOK on success. 393 * 394 */ 395 static int arp_mtu_changed_message(device_id_t device_id, size_t mtu) 396 { 397 fibril_mutex_lock(&arp_globals.lock); 398 399 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id); 400 if (!device) { 401 fibril_mutex_unlock(&arp_globals.lock); 402 return ENOENT; 403 } 404 405 device->packet_dimension.content = mtu; 406 407 fibril_mutex_unlock(&arp_globals.lock); 408 409 printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu); 410 411 return EOK; 412 } 413 414 /** Process IPC messages from the registered device driver modules 415 * 416 * @param[in] iid Message identifier. 417 * @param[in,out] icall Message parameters. 418 * 419 */ 420 static void arp_receiver(ipc_callid_t iid, ipc_call_t *icall) 421 { 566 422 packet_t *packet; 567 arp_header_t *header;568 bool retry = false;569 423 int rc; 570 571 restart: 572 if (!target || !translation) 573 return EBADMEM; 574 575 device = arp_cache_find(&arp_globals.cache, device_id); 576 if (!device) 577 return ENOENT; 578 579 proto = arp_protos_find(&device->protos, protocol); 580 if (!proto || (proto->addr->length != target->length)) 581 return ENOENT; 582 583 trans = arp_addr_find(&proto->addresses, target->value, target->length); 584 if (trans) { 585 if (trans->hw_addr) { 586 *translation = trans->hw_addr; 587 return EOK; 588 } 589 if (retry) 590 return EAGAIN; 591 rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock, 592 ARP_TRANS_WAIT); 593 if (rc == ETIMEOUT) 424 425 while (true) { 426 switch (IPC_GET_IMETHOD(*icall)) { 427 case NET_IL_DEVICE_STATE: 428 /* Do nothing - keep the cache */ 429 ipc_answer_0(iid, (sysarg_t) EOK); 430 break; 431 432 case NET_IL_RECEIVED: 433 rc = packet_translate_remote(arp_globals.net_phone, &packet, 434 IPC_GET_PACKET(*icall)); 435 if (rc == EOK) { 436 fibril_mutex_lock(&arp_globals.lock); 437 do { 438 packet_t *next = pq_detach(packet); 439 rc = arp_receive_message(IPC_GET_DEVICE(*icall), packet); 440 if (rc != 1) { 441 pq_release_remote(arp_globals.net_phone, 442 packet_get_id(packet)); 443 } 444 445 packet = next; 446 } while (packet); 447 fibril_mutex_unlock(&arp_globals.lock); 448 } 449 ipc_answer_0(iid, (sysarg_t) rc); 450 break; 451 452 case NET_IL_MTU_CHANGED: 453 rc = arp_mtu_changed_message(IPC_GET_DEVICE(*icall), 454 IPC_GET_MTU(*icall)); 455 ipc_answer_0(iid, (sysarg_t) rc); 456 break; 457 458 default: 459 ipc_answer_0(iid, (sysarg_t) ENOTSUP); 460 } 461 462 iid = async_get_call(icall); 463 } 464 } 465 466 /** Register the device. 467 * 468 * Create new device entry in the cache or update the protocol address if the 469 * device with the device identifier and the driver service exists. 470 * 471 * @param[in] device_id Device identifier. 472 * @param[in] service Device driver service. 473 * @param[in] protocol Protocol service. 474 * @param[in] address Actual device protocol address. 475 * 476 * @return EOK on success. 477 * @return EEXIST if another device with the same device identifier 478 * and different driver service exists. 479 * @return ENOMEM if there is not enough memory left. 480 * @return Other error codes as defined for the 481 * measured_strings_return() function. 482 * 483 */ 484 static int arp_device_message(device_id_t device_id, services_t service, 485 services_t protocol, measured_string_t *address) 486 { 487 int index; 488 int rc; 489 490 fibril_mutex_lock(&arp_globals.lock); 491 492 /* An existing device? */ 493 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id); 494 if (device) { 495 if (device->service != service) { 496 printf("%s: Device %d already exists\n", NAME, 497 device->device_id); 498 fibril_mutex_unlock(&arp_globals.lock); 499 return EEXIST; 500 } 501 502 arp_proto_t *proto = arp_protos_find(&device->protos, protocol); 503 if (proto) { 504 free(proto->addr); 505 free(proto->addr_data); 506 proto->addr = address; 507 proto->addr_data = address->value; 508 } else { 509 rc = arp_proto_create(&proto, protocol, address); 510 if (rc != EOK) { 511 fibril_mutex_unlock(&arp_globals.lock); 512 return rc; 513 } 514 515 index = arp_protos_add(&device->protos, proto->service, 516 proto); 517 if (index < 0) { 518 fibril_mutex_unlock(&arp_globals.lock); 519 free(proto); 520 return index; 521 } 522 523 printf("%s: New protocol added (id: %d, proto: %d)\n", NAME, 524 device_id, protocol); 525 } 526 } else { 527 hw_type_t hardware = hardware_map(service); 528 if (!hardware) 594 529 return ENOENT; 595 retry = true; 596 goto restart; 597 } 598 if (retry) 599 return EAGAIN; 600 530 531 /* Create new device */ 532 device = (arp_device_t *) malloc(sizeof(arp_device_t)); 533 if (!device) { 534 fibril_mutex_unlock(&arp_globals.lock); 535 return ENOMEM; 536 } 537 538 device->hardware = hardware; 539 device->device_id = device_id; 540 rc = arp_protos_initialize(&device->protos); 541 if (rc != EOK) { 542 fibril_mutex_unlock(&arp_globals.lock); 543 free(device); 544 return rc; 545 } 546 547 arp_proto_t *proto; 548 rc = arp_proto_create(&proto, protocol, address); 549 if (rc != EOK) { 550 fibril_mutex_unlock(&arp_globals.lock); 551 free(device); 552 return rc; 553 } 554 555 index = arp_protos_add(&device->protos, proto->service, proto); 556 if (index < 0) { 557 fibril_mutex_unlock(&arp_globals.lock); 558 arp_protos_destroy(&device->protos); 559 free(device); 560 return index; 561 } 562 563 device->service = service; 564 565 /* Bind */ 566 device->phone = nil_bind_service(device->service, 567 (sysarg_t) device->device_id, SERVICE_ARP, 568 arp_receiver); 569 if (device->phone < 0) { 570 fibril_mutex_unlock(&arp_globals.lock); 571 arp_protos_destroy(&device->protos); 572 free(device); 573 return EREFUSED; 574 } 575 576 /* Get packet dimensions */ 577 rc = nil_packet_size_req(device->phone, device_id, 578 &device->packet_dimension); 579 if (rc != EOK) { 580 fibril_mutex_unlock(&arp_globals.lock); 581 arp_protos_destroy(&device->protos); 582 free(device); 583 return rc; 584 } 585 586 /* Get hardware address */ 587 rc = nil_get_addr_req(device->phone, device_id, &device->addr, 588 &device->addr_data); 589 if (rc != EOK) { 590 fibril_mutex_unlock(&arp_globals.lock); 591 arp_protos_destroy(&device->protos); 592 free(device); 593 return rc; 594 } 595 596 /* Get broadcast address */ 597 rc = nil_get_broadcast_addr_req(device->phone, device_id, 598 &device->broadcast_addr, &device->broadcast_data); 599 if (rc != EOK) { 600 fibril_mutex_unlock(&arp_globals.lock); 601 free(device->addr); 602 free(device->addr_data); 603 arp_protos_destroy(&device->protos); 604 free(device); 605 return rc; 606 } 607 608 rc = arp_cache_add(&arp_globals.cache, device->device_id, 609 device); 610 if (rc != EOK) { 611 fibril_mutex_unlock(&arp_globals.lock); 612 free(device->addr); 613 free(device->addr_data); 614 free(device->broadcast_addr); 615 free(device->broadcast_data); 616 arp_protos_destroy(&device->protos); 617 free(device); 618 return rc; 619 } 620 printf("%s: Device registered (id: %d, type: 0x%x, service: %d," 621 " proto: %d)\n", NAME, device->device_id, device->hardware, 622 device->service, protocol); 623 } 624 625 fibril_mutex_unlock(&arp_globals.lock); 626 return EOK; 627 } 628 629 int il_initialize(int net_phone) 630 { 631 fibril_mutex_initialize(&arp_globals.lock); 632 633 fibril_mutex_lock(&arp_globals.lock); 634 arp_globals.net_phone = net_phone; 635 int rc = arp_cache_initialize(&arp_globals.cache); 636 fibril_mutex_unlock(&arp_globals.lock); 637 638 return rc; 639 } 640 641 static int arp_send_request(device_id_t device_id, services_t protocol, 642 measured_string_t *target, arp_device_t *device, arp_proto_t *proto) 643 { 601 644 /* ARP packet content size = header + (address + translation) * 2 */ 602 length = 8 + 2 * (proto->addr->length + device->addr->length);645 size_t length = 8 + 2 * (proto->addr->length + device->addr->length); 603 646 if (length > device->packet_dimension.content) 604 647 return ELIMIT; 605 606 packet = packet_get_4_remote(arp_globals.net_phone,648 649 packet_t *packet = packet_get_4_remote(arp_globals.net_phone, 607 650 device->packet_dimension.addr_len, device->packet_dimension.prefix, 608 651 length, device->packet_dimension.suffix); 609 652 if (!packet) 610 653 return ENOMEM; 611 612 header = (arp_header_t *) packet_suffix(packet, length);654 655 arp_header_t *header = (arp_header_t *) packet_suffix(packet, length); 613 656 if (!header) { 614 657 pq_release_remote(arp_globals.net_phone, packet_get_id(packet)); 615 658 return ENOMEM; 616 659 } 617 660 618 661 header->hardware = htons(device->hardware); 619 662 header->hardware_length = (uint8_t) device->addr->length; … … 621 664 header->protocol_length = (uint8_t) proto->addr->length; 622 665 header->operation = htons(ARPOP_REQUEST); 666 623 667 length = sizeof(arp_header_t); 668 624 669 memcpy(((uint8_t *) header) + length, device->addr->value, 625 670 device->addr->length); … … 631 676 length += device->addr->length; 632 677 memcpy(((uint8_t *) header) + length, target->value, target->length); 633 634 rc = packet_set_addr(packet, (uint8_t *) device->addr->value,678 679 int rc = packet_set_addr(packet, (uint8_t *) device->addr->value, 635 680 (uint8_t *) device->broadcast_addr->value, device->addr->length); 636 681 if (rc != EOK) { … … 638 683 return rc; 639 684 } 640 685 641 686 nil_send_msg(device->phone, device_id, packet, SERVICE_ARP); 642 687 return EOK; 688 } 689 690 /** Return the hardware address for the given protocol address. 691 * 692 * Send the ARP request packet if the hardware address is not found in the 693 * cache. 694 * 695 * @param[in] device_id Device identifier. 696 * @param[in] protocol Protocol service. 697 * @param[in] target Target protocol address. 698 * @param[out] translation Where the hardware address of the target is stored. 699 * 700 * @return EOK on success. 701 * @return EAGAIN if the caller should try again. 702 * @return Other error codes in case of error. 703 * 704 */ 705 static int arp_translate_message(device_id_t device_id, services_t protocol, 706 measured_string_t *target, measured_string_t **translation) 707 { 708 bool retry = false; 709 int rc; 710 711 restart: 712 if ((!target) || (!translation)) 713 return EBADMEM; 714 715 arp_device_t *device = arp_cache_find(&arp_globals.cache, device_id); 716 if (!device) 717 return ENOENT; 718 719 arp_proto_t *proto = arp_protos_find(&device->protos, protocol); 720 if ((!proto) || (proto->addr->length != target->length)) 721 return ENOENT; 722 723 arp_trans_t *trans = arp_addr_find(&proto->addresses, target->value, 724 target->length); 725 if (trans) { 726 if (trans->hw_addr) { 727 *translation = trans->hw_addr; 728 return EOK; 729 } 730 731 if (retry) { 732 /* Remove the translation from the map */ 733 arp_clear_trans(trans); 734 arp_addr_exclude(&proto->addresses, target->value, 735 target->length); 736 return EAGAIN; 737 } 738 739 rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock, 740 ARP_TRANS_WAIT); 741 if (rc == ETIMEOUT) 742 return ENOENT; 743 744 retry = true; 745 goto restart; 746 } 747 748 if (retry) 749 return EAGAIN; 750 643 751 trans = (arp_trans_t *) malloc(sizeof(arp_trans_t)); 644 752 if (!trans) 645 753 return ENOMEM; 754 646 755 trans->hw_addr = NULL; 647 756 fibril_condvar_initialize(&trans->cv); 757 648 758 rc = arp_addr_add(&proto->addresses, target->value, target->length, 649 759 trans); … … 653 763 } 654 764 765 rc = arp_send_request(device_id, protocol, target, device, proto); 766 if (rc != EOK) 767 return rc; 768 655 769 rc = fibril_condvar_wait_timeout(&trans->cv, &arp_globals.lock, 656 770 ARP_TRANS_WAIT); 657 771 if (rc == ETIMEOUT) 658 772 return ENOENT; 773 659 774 retry = true; 660 775 goto restart; 661 776 } 662 777 663 664 /** Processes the ARP message. 665 * 666 * @param[in] callid The message identifier. 667 * @param[in] call The message parameters. 668 * @param[out] answer The message answer parameters. 669 * @param[out] answer_count The last parameter for the actual answer in the 670 * answer parameter. 671 * @return EOK on success. 672 * @return ENOTSUP if the message is not known. 778 /** Process the ARP message. 779 * 780 * @param[in] callid Message identifier. 781 * @param[in] call Message parameters. 782 * @param[out] answer Answer. 783 * @param[out] count Number of arguments of the answer. 784 * 785 * @return EOK on success. 786 * @return ENOTSUP if the message is not known. 673 787 * 674 788 * @see arp_interface.h 675 789 * @see IS_NET_ARP_MESSAGE() 676 * /677 int 678 arp_message_standalone(ipc_callid_t callid, ipc_call_t *call,679 ipc_call_t *answer, int *answer_count)790 * 791 */ 792 int il_module_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 793 size_t *count) 680 794 { 681 795 measured_string_t *address; 682 796 measured_string_t *translation; 683 char *data; 684 packet_t *packet; 685 packet_t *next; 797 uint8_t *data; 686 798 int rc; 687 799 688 * answer_count = 0;800 *count = 0; 689 801 switch (IPC_GET_IMETHOD(*call)) { 690 802 case IPC_M_PHONE_HUNGUP: … … 696 808 return rc; 697 809 698 rc = arp_device_message(IPC_GET_DEVICE( call),699 IPC_GET_SERVICE( call), ARP_GET_NETIF(call), address);810 rc = arp_device_message(IPC_GET_DEVICE(*call), 811 IPC_GET_SERVICE(*call), ARP_GET_NETIF(*call), address); 700 812 if (rc != EOK) { 701 813 free(address); 702 814 free(data); 703 815 } 816 704 817 return rc; 705 818 … … 710 823 711 824 fibril_mutex_lock(&arp_globals.lock); 712 rc = arp_translate_message(IPC_GET_DEVICE( call),713 IPC_GET_SERVICE( call), address, &translation);825 rc = arp_translate_message(IPC_GET_DEVICE(*call), 826 IPC_GET_SERVICE(*call), address, &translation); 714 827 free(address); 715 828 free(data); 829 716 830 if (rc != EOK) { 717 831 fibril_mutex_unlock(&arp_globals.lock); 718 832 return rc; 719 833 } 834 720 835 if (!translation) { 721 836 fibril_mutex_unlock(&arp_globals.lock); 722 837 return ENOENT; 723 838 } 839 724 840 rc = measured_strings_reply(translation, 1); 725 841 fibril_mutex_unlock(&arp_globals.lock); 726 842 return rc; 727 843 728 844 case NET_ARP_CLEAR_DEVICE: 729 return arp_clear_device_req(0, IPC_GET_DEVICE( call));730 845 return arp_clear_device_req(0, IPC_GET_DEVICE(*call)); 846 731 847 case NET_ARP_CLEAR_ADDRESS: 732 848 rc = measured_strings_receive(&address, &data, 1); … … 734 850 return rc; 735 851 736 arp_clear_address_req(0, IPC_GET_DEVICE( call),737 IPC_GET_SERVICE( call), address);852 arp_clear_address_req(0, IPC_GET_DEVICE(*call), 853 IPC_GET_SERVICE(*call), address); 738 854 free(address); 739 855 free(data); … … 742 858 case NET_ARP_CLEAN_CACHE: 743 859 return arp_clean_cache_req(0); 744 745 case NET_IL_DEVICE_STATE:746 /* Do nothing - keep the cache */747 return EOK;748 749 case NET_IL_RECEIVED:750 rc = packet_translate_remote(arp_globals.net_phone, &packet,751 IPC_GET_PACKET(call));752 if (rc != EOK)753 return rc;754 755 fibril_mutex_lock(&arp_globals.lock);756 do {757 next = pq_detach(packet);758 rc = arp_receive_message(IPC_GET_DEVICE(call), packet);759 if (rc != 1) {760 pq_release_remote(arp_globals.net_phone,761 packet_get_id(packet));762 }763 packet = next;764 } while (packet);765 fibril_mutex_unlock(&arp_globals.lock);766 767 return EOK;768 769 case NET_IL_MTU_CHANGED:770 return arp_mtu_changed_message(IPC_GET_DEVICE(call),771 IPC_GET_MTU(call));772 860 } 773 861 … … 775 863 } 776 864 777 /** Default thread for new connections.778 *779 * @param[in] iid The initial message identifier.780 * @param[in] icall The initial message call structure.781 */782 static void il_client_connection(ipc_callid_t iid, ipc_call_t *icall)783 {784 /*785 * Accept the connection786 * - Answer the first IPC_M_CONNECT_ME_TO call.787 */788 ipc_answer_0(iid, EOK);789 790 while (true) {791 ipc_call_t answer;792 int answer_count;793 794 /* Clear the answer structure */795 refresh_answer(&answer, &answer_count);796 797 /* Fetch the next message */798 ipc_call_t call;799 ipc_callid_t callid = async_get_call(&call);800 801 /* Process the message */802 int res = il_module_message_standalone(callid, &call, &answer,803 &answer_count);804 805 /*806 * End if told to either by the message or the processing807 * result.808 */809 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||810 (res == EHANGUP))811 return;812 813 /* Answer the message */814 answer_call(callid, res, &answer, answer_count);815 }816 }817 818 /** Starts the module.819 *820 * @return EOK on success.821 * @return Other error codes as defined for each specific module822 * start function.823 */824 865 int main(int argc, char *argv[]) 825 866 { 826 int rc;827 828 867 /* Start the module */ 829 rc = il_module_start_standalone(il_client_connection); 830 return rc; 868 return il_module_start(SERVICE_ARP); 831 869 } 832 870 833 871 /** @} 834 872 */ 835 -
uspace/srv/net/il/arp/arp.h
reaef141 r80cd7cd 96 96 measured_string_t *addr; 97 97 /** Actual device hardware address data. */ 98 char*addr_data;98 uint8_t *addr_data; 99 99 /** Broadcast device hardware address. */ 100 100 measured_string_t *broadcast_addr; 101 101 /** Broadcast device hardware address data. */ 102 char*broadcast_data;102 uint8_t *broadcast_data; 103 103 /** Device identifier. */ 104 104 device_id_t device_id; … … 125 125 arp_cache_t cache; 126 126 127 /**128 * The client connection processing function.129 * The module skeleton propagates its own one.130 */131 async_client_conn_t client_connection;132 133 127 /** Networking module phone. */ 134 128 int net_phone; … … 142 136 measured_string_t *addr; 143 137 /** Actual device protocol address data. */ 144 char*addr_data;138 uint8_t *addr_data; 145 139 /** Address map. */ 146 140 arp_addr_t addresses; … … 154 148 * Hardware address for the translation. NULL denotes an incomplete 155 149 * record with possible waiters. 156 */ 150 */ 157 151 measured_string_t *hw_addr; 158 152 /** Condition variable used for waiting for completion of the record. */ -
uspace/srv/net/il/ip/Makefile
reaef141 r80cd7cd 34 34 35 35 SOURCES = \ 36 ip.c \ 37 ip_module.c 36 ip.c 38 37 39 38 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/il/ip/ip.c
reaef141 r80cd7cd 35 35 * @see arp.h 36 36 */ 37 38 #include "ip.h"39 #include "ip_module.h"40 37 41 38 #include <async.h> … … 52 49 #include <sys/types.h> 53 50 #include <byteorder.h> 51 #include "ip.h" 54 52 55 53 #include <adt/measured_strings.h> … … 70 68 #include <icmp_client.h> 71 69 #include <icmp_interface.h> 72 #include <il_interface.h>73 70 #include <ip_client.h> 74 71 #include <ip_interface.h> 75 72 #include <ip_header.h> 76 73 #include <net_interface.h> 77 #include <nil_ interface.h>78 #include <tl_ interface.h>74 #include <nil_remote.h> 75 #include <tl_remote.h> 79 76 #include <packet_remote.h> 80 #include <il_local.h> 77 #include <il_remote.h> 78 #include <il_skel.h> 81 79 82 80 /** IP module name. */ … … 122 120 INT_MAP_IMPLEMENT(ip_protos, ip_proto_t); 123 121 GENERIC_FIELD_IMPLEMENT(ip_routes, ip_route_t); 122 123 static void ip_receiver(ipc_callid_t, ipc_call_t *); 124 124 125 125 /** Releases the packet and returns the result. … … 244 244 } 245 245 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 * @return EOK on success. 251 * @return ENOMEM if there is not enough memory left. 252 */ 253 int ip_initialize(async_client_conn_t client_connection) 254 { 255 int rc; 256 246 int il_initialize(int net_phone) 247 { 257 248 fibril_rwlock_initialize(&ip_globals.lock); 258 249 fibril_rwlock_write_lock(&ip_globals.lock); 259 250 fibril_rwlock_initialize(&ip_globals.protos_lock); 260 251 fibril_rwlock_initialize(&ip_globals.netifs_lock); 252 253 ip_globals.net_phone = net_phone; 261 254 ip_globals.packet_counter = 0; 262 255 ip_globals.gateway.address.s_addr = 0; … … 264 257 ip_globals.gateway.gateway.s_addr = 0; 265 258 ip_globals.gateway.netif = NULL; 266 ip_globals.client_connection = client_connection; 267 268 rc = ip_netifs_initialize(&ip_globals.netifs); 259 260 int rc = ip_netifs_initialize(&ip_globals.netifs); 269 261 if (rc != EOK) 270 262 goto out; … … 275 267 if (rc != EOK) 276 268 goto out; 277 rc = add_module(NULL, &ip_globals.modules, ARP_NAME, ARP_FILENAME,278 SERVICE_ARP, 0, arp_connect_module);269 rc = add_module(NULL, &ip_globals.modules, (uint8_t *) ARP_NAME, 270 (uint8_t *) ARP_FILENAME, SERVICE_ARP, 0, arp_connect_module); 279 271 280 272 out: … … 312 304 measured_string_t names[] = { 313 305 { 314 ( char*) "IPV",306 (uint8_t *) "IPV", 315 307 3 316 308 }, 317 309 { 318 ( char*) "IP_CONFIG",310 (uint8_t *) "IP_CONFIG", 319 311 9 320 312 }, 321 313 { 322 ( char*) "IP_ADDR",314 (uint8_t *) "IP_ADDR", 323 315 7 324 316 }, 325 317 { 326 ( char*) "IP_NETMASK",318 (uint8_t *) "IP_NETMASK", 327 319 10 328 320 }, 329 321 { 330 ( char*) "IP_GATEWAY",322 (uint8_t *) "IP_GATEWAY", 331 323 10 332 324 }, 333 325 { 334 ( char*) "IP_BROADCAST",326 (uint8_t *) "IP_BROADCAST", 335 327 12 336 328 }, 337 329 { 338 ( char*) "ARP",330 (uint8_t *) "ARP", 339 331 3 340 332 }, 341 333 { 342 ( char*) "IP_ROUTING",334 (uint8_t *) "IP_ROUTING", 343 335 10 344 336 } … … 346 338 measured_string_t *configuration; 347 339 size_t count = sizeof(names) / sizeof(measured_string_t); 348 char*data;340 uint8_t *data; 349 341 measured_string_t address; 350 342 ip_route_t *route; … … 368 360 if (configuration) { 369 361 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",362 ip_netif->ipv = strtol((char *) configuration[0].value, NULL, 0); 363 364 ip_netif->dhcp = !str_lcmp((char *) configuration[1].value, "dhcp", 373 365 configuration[1].length); 374 366 … … 394 386 } 395 387 396 if ((inet_pton(AF_INET, configuration[2].value,388 if ((inet_pton(AF_INET, (char *) configuration[2].value, 397 389 (uint8_t *) &route->address.s_addr) != EOK) || 398 (inet_pton(AF_INET, configuration[3].value,390 (inet_pton(AF_INET, (char *) configuration[3].value, 399 391 (uint8_t *) &route->netmask.s_addr) != EOK) || 400 (inet_pton(AF_INET, configuration[4].value,392 (inet_pton(AF_INET, (char *) configuration[4].value, 401 393 (uint8_t *) &gateway.s_addr) == EINVAL) || 402 (inet_pton(AF_INET, configuration[5].value,394 (inet_pton(AF_INET, (char *) configuration[5].value, 403 395 (uint8_t *) &ip_netif->broadcast.s_addr) == EINVAL)) 404 396 { … … 431 423 ip_netif->phone = nil_bind_service(ip_netif->service, 432 424 (sysarg_t) ip_netif->device_id, SERVICE_IP, 433 ip_ globals.client_connection);425 ip_receiver); 434 426 if (ip_netif->phone < 0) { 435 427 printf("Failed to contact the nil service %d\n", … … 441 433 if (ip_netif->arp) { 442 434 if (route) { 443 address.value = ( char*) &route->address.s_addr;435 address.value = (uint8_t *) &route->address.s_addr; 444 436 address.length = sizeof(in_addr_t); 445 437 … … 477 469 ip_globals.gateway.gateway.s_addr = gateway.s_addr; 478 470 ip_globals.gateway.netif = ip_netif; 471 472 char defgateway[INET_ADDRSTRLEN]; 473 inet_ntop(AF_INET, (uint8_t *) &gateway.s_addr, 474 defgateway, INET_ADDRSTRLEN); 475 printf("%s: Default gateway (%s)\n", NAME, defgateway); 479 476 } 480 477 … … 482 479 } 483 480 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 * @return EOK on success. 489 * @return ENOENT if device is not found. 490 */ 491 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu) 492 { 481 static int ip_device_req_local(int il_phone, device_id_t device_id, 482 services_t netif) 483 { 484 ip_netif_t *ip_netif; 485 ip_route_t *route; 486 int index; 487 int rc; 488 489 ip_netif = (ip_netif_t *) malloc(sizeof(ip_netif_t)); 490 if (!ip_netif) 491 return ENOMEM; 492 493 rc = ip_routes_initialize(&ip_netif->routes); 494 if (rc != EOK) { 495 free(ip_netif); 496 return rc; 497 } 498 499 ip_netif->device_id = device_id; 500 ip_netif->service = netif; 501 ip_netif->state = NETIF_STOPPED; 502 503 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 504 505 rc = ip_netif_initialize(ip_netif); 506 if (rc != EOK) { 507 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 508 ip_routes_destroy(&ip_netif->routes); 509 free(ip_netif); 510 return rc; 511 } 512 if (ip_netif->arp) 513 ip_netif->arp->usage++; 514 515 // print the settings 516 printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n", 517 NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv, 518 ip_netif->dhcp ? "dhcp" : "static"); 519 520 // TODO ipv6 addresses 521 522 char address[INET_ADDRSTRLEN]; 523 char netmask[INET_ADDRSTRLEN]; 524 char gateway[INET_ADDRSTRLEN]; 525 526 for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) { 527 route = ip_routes_get_index(&ip_netif->routes, index); 528 if (route) { 529 inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, 530 address, INET_ADDRSTRLEN); 531 inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, 532 netmask, INET_ADDRSTRLEN); 533 inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, 534 gateway, INET_ADDRSTRLEN); 535 printf("%s: Route %d (address: %s, netmask: %s, " 536 "gateway: %s)\n", NAME, index, address, netmask, 537 gateway); 538 } 539 } 540 541 inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address, 542 INET_ADDRSTRLEN); 543 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 544 545 printf("%s: Broadcast (%s)\n", NAME, address); 546 547 return EOK; 548 } 549 550 /** Searches the network interfaces if there is a suitable route. 551 * 552 * @param[in] netif The network interface to be searched for routes. May be 553 * NULL. 554 * @param[in] destination The destination address. 555 * @return The found route. 556 * @return NULL if no route was found. 557 */ 558 static ip_route_t *ip_netif_find_route(ip_netif_t *netif, 559 in_addr_t destination) 560 { 561 int index; 562 ip_route_t *route; 563 564 if (!netif) 565 return NULL; 566 567 /* Start with the first one (the direct route) */ 568 for (index = 0; index < ip_routes_count(&netif->routes); index++) { 569 route = ip_routes_get_index(&netif->routes, index); 570 if ((route) && 571 ((route->address.s_addr & route->netmask.s_addr) == 572 (destination.s_addr & route->netmask.s_addr))) 573 return route; 574 } 575 576 return NULL; 577 } 578 579 /** Searches all network interfaces if there is a suitable route. 580 * 581 * @param[in] destination The destination address. 582 * @return The found route. 583 * @return NULL if no route was found. 584 */ 585 static ip_route_t *ip_find_route(in_addr_t destination) { 586 int index; 587 ip_route_t *route; 493 588 ip_netif_t *netif; 494 589 495 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 496 netif = ip_netifs_find(&ip_globals.netifs, device_id); 497 if (!netif) { 498 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 499 return ENOENT; 500 } 501 netif->packet_dimension.content = mtu; 502 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 503 504 printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu); 505 506 return EOK; 507 } 508 509 /** Updates the device state. 510 * 511 * @param[in] device_id The device identifier. 512 * @param[in] state The new state value. 513 * @return EOK on success. 514 * @return 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_t *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 * @return The prefixed middle header. 542 * @return NULL on error. 543 */ 544 static ip_header_t * 545 ip_create_middle_header(packet_t *packet, ip_header_t *last) 546 { 547 ip_header_t *middle; 548 549 middle = (ip_header_t *) packet_suffix(packet, IP_HEADER_LENGTH(last)); 550 if (!middle) 551 return NULL; 552 memcpy(middle, last, IP_HEADER_LENGTH(last)); 553 middle->flags |= IPFLAG_MORE_FRAGMENTS; 554 return middle; 590 // start with the last netif - the newest one 591 index = ip_netifs_count(&ip_globals.netifs) - 1; 592 while (index >= 0) { 593 netif = ip_netifs_get_index(&ip_globals.netifs, index); 594 if (netif && (netif->state == NETIF_ACTIVE)) { 595 route = ip_netif_find_route(netif, destination); 596 if (route) 597 return route; 598 } 599 index--; 600 } 601 602 return &ip_globals.gateway; 603 } 604 605 /** Returns the network interface's IP address. 606 * 607 * @param[in] netif The network interface. 608 * @return The IP address. 609 * @return NULL if no IP address was found. 610 */ 611 static in_addr_t *ip_netif_address(ip_netif_t *netif) 612 { 613 ip_route_t *route; 614 615 route = ip_routes_get_index(&netif->routes, 0); 616 return route ? &route->address : NULL; 555 617 } 556 618 … … 621 683 * function. 622 684 */ 623 static int 624 ip_prepare_packet(in_addr_t *source, in_addr_t dest, packet_t *packet, 625 measured_string_t *destination) 685 static int ip_prepare_packet(in_addr_t *source, in_addr_t dest, 686 packet_t *packet, measured_string_t *destination) 626 687 { 627 688 size_t length; … … 752 813 * function. 753 814 */ 754 static int 755 ip_fragment_packet_data(packet_t *packet, packet_t *new_packet, 815 static int ip_fragment_packet_data(packet_t *packet, packet_t *new_packet, 756 816 ip_header_t *header, ip_header_t *new_header, size_t length, 757 817 const struct sockaddr *src, const struct sockaddr *dest, socklen_t addrlen) … … 787 847 788 848 return pq_insert_after(packet, new_packet); 849 } 850 851 /** Prefixes a middle fragment header based on the last fragment header to the 852 * packet. 853 * 854 * @param[in] packet The packet to be prefixed. 855 * @param[in] last The last header to be copied. 856 * @return The prefixed middle header. 857 * @return NULL on error. 858 */ 859 static ip_header_t *ip_create_middle_header(packet_t *packet, 860 ip_header_t *last) 861 { 862 ip_header_t *middle; 863 864 middle = (ip_header_t *) packet_suffix(packet, IP_HEADER_LENGTH(last)); 865 if (!middle) 866 return NULL; 867 memcpy(middle, last, IP_HEADER_LENGTH(last)); 868 middle->flags |= IPFLAG_MORE_FRAGMENTS; 869 return middle; 789 870 } 790 871 … … 991 1072 * function. 992 1073 */ 993 static int 994 ip_send_route(packet_t *packet, ip_netif_t *netif, ip_route_t *route, 995 in_addr_t *src, in_addr_t dest, services_t error) 1074 static int ip_send_route(packet_t *packet, ip_netif_t *netif, 1075 ip_route_t *route, in_addr_t *src, in_addr_t dest, services_t error) 996 1076 { 997 1077 measured_string_t destination; 998 1078 measured_string_t *translation; 999 char*data;1079 uint8_t *data; 1000 1080 int phone; 1001 1081 int rc; … … 1004 1084 if (netif->arp && (route->address.s_addr != dest.s_addr)) { 1005 1085 destination.value = route->gateway.s_addr ? 1006 ( char *) &route->gateway.s_addr : (char*) &dest.s_addr;1086 (uint8_t *) &route->gateway.s_addr : (uint8_t *) &dest.s_addr; 1007 1087 destination.length = sizeof(dest.s_addr); 1008 1088 … … 1056 1136 } 1057 1137 1058 /** Searches the network interfaces if there is a suitable route. 1059 * 1060 * @param[in] netif The network interface to be searched for routes. May be 1061 * NULL. 1062 * @param[in] destination The destination address. 1063 * @return The found route. 1064 * @return NULL if no route was found. 1065 */ 1066 static ip_route_t * 1067 ip_netif_find_route(ip_netif_t *netif, in_addr_t destination) 1068 { 1069 int index; 1070 ip_route_t *route; 1071 1072 if (!netif) 1073 return NULL; 1074 1075 // start with the first one - the direct route 1076 for (index = 0; index < ip_routes_count(&netif->routes); index++) { 1077 route = ip_routes_get_index(&netif->routes, index); 1078 if (route && 1079 ((route->address.s_addr & route->netmask.s_addr) == 1080 (destination.s_addr & route->netmask.s_addr))) { 1081 return route; 1082 } 1083 } 1084 1085 return NULL; 1086 } 1087 1088 /** Searches all network interfaces if there is a suitable route. 1089 * 1090 * @param[in] destination The destination address. 1091 * @return The found route. 1092 * @return NULL if no route was found. 1093 */ 1094 static ip_route_t *ip_find_route(in_addr_t destination) { 1095 int index; 1096 ip_route_t *route; 1097 ip_netif_t *netif; 1098 1099 // start with the last netif - the newest one 1100 index = ip_netifs_count(&ip_globals.netifs) - 1; 1101 while (index >= 0) { 1102 netif = ip_netifs_get_index(&ip_globals.netifs, index); 1103 if (netif && (netif->state == NETIF_ACTIVE)) { 1104 route = ip_netif_find_route(netif, destination); 1105 if (route) 1106 return route; 1107 } 1108 index--; 1109 } 1110 1111 return &ip_globals.gateway; 1112 } 1113 1114 /** Returns the network interface's IP address. 1115 * 1116 * @param[in] netif The network interface. 1117 * @return The IP address. 1118 * @return NULL if no IP address was found. 1119 */ 1120 static in_addr_t *ip_netif_address(ip_netif_t *netif) 1121 { 1122 ip_route_t *route; 1123 1124 route = ip_routes_get_index(&netif->routes, 0); 1125 return route ? &route->address : NULL; 1126 } 1127 1128 /** Registers the transport layer protocol. 1129 * 1130 * The traffic of this protocol will be supplied using either the receive 1131 * function or IPC message. 1132 * 1133 * @param[in] protocol The transport layer module protocol. 1134 * @param[in] service The transport layer module service. 1135 * @param[in] phone The transport layer module phone. 1136 * @param[in] received_msg The receiving function. 1137 * @return EOK on success. 1138 * @return EINVAL if the protocol parameter and/or the service 1139 * parameter is zero. 1140 * @return EINVAL if the phone parameter is not a positive number 1141 * and the tl_receive_msg is NULL. 1142 * @return ENOMEM if there is not enough memory left. 1143 */ 1144 static int 1145 ip_register(int protocol, services_t service, int phone, 1146 tl_received_msg_t received_msg) 1147 { 1148 ip_proto_t *proto; 1149 int index; 1150 1151 if (!protocol || !service || ((phone < 0) && !received_msg)) 1152 return EINVAL; 1153 1154 proto = (ip_proto_t *) malloc(sizeof(ip_protos_t)); 1155 if (!proto) 1156 return ENOMEM; 1157 1158 proto->protocol = protocol; 1159 proto->service = service; 1160 proto->phone = phone; 1161 proto->received_msg = received_msg; 1162 1163 fibril_rwlock_write_lock(&ip_globals.protos_lock); 1164 index = ip_protos_add(&ip_globals.protos, proto->protocol, proto); 1165 if (index < 0) { 1166 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1167 free(proto); 1168 return index; 1169 } 1170 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1171 1172 printf("%s: Protocol registered (protocol: %d, phone: %d)\n", 1173 NAME, proto->protocol, proto->phone); 1174 1175 return EOK; 1176 } 1177 1178 static int 1179 ip_device_req_local(int il_phone, device_id_t device_id, services_t netif) 1180 { 1181 ip_netif_t *ip_netif; 1182 ip_route_t *route; 1183 int index; 1184 int rc; 1185 1186 ip_netif = (ip_netif_t *) malloc(sizeof(ip_netif_t)); 1187 if (!ip_netif) 1188 return ENOMEM; 1189 1190 rc = ip_routes_initialize(&ip_netif->routes); 1191 if (rc != EOK) { 1192 free(ip_netif); 1193 return rc; 1194 } 1195 1196 ip_netif->device_id = device_id; 1197 ip_netif->service = netif; 1198 ip_netif->state = NETIF_STOPPED; 1199 1200 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1201 1202 rc = ip_netif_initialize(ip_netif); 1203 if (rc != EOK) { 1204 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1205 ip_routes_destroy(&ip_netif->routes); 1206 free(ip_netif); 1207 return rc; 1208 } 1209 if (ip_netif->arp) 1210 ip_netif->arp->usage++; 1211 1212 // print the settings 1213 printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n", 1214 NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv, 1215 ip_netif->dhcp ? "dhcp" : "static"); 1216 1217 // TODO ipv6 addresses 1218 1219 char address[INET_ADDRSTRLEN]; 1220 char netmask[INET_ADDRSTRLEN]; 1221 char gateway[INET_ADDRSTRLEN]; 1222 1223 for (index = 0; index < ip_routes_count(&ip_netif->routes); index++) { 1224 route = ip_routes_get_index(&ip_netif->routes, index); 1225 if (route) { 1226 inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, 1227 address, INET_ADDRSTRLEN); 1228 inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, 1229 netmask, INET_ADDRSTRLEN); 1230 inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, 1231 gateway, INET_ADDRSTRLEN); 1232 printf("%s: Route %d (address: %s, netmask: %s, " 1233 "gateway: %s)\n", NAME, index, address, netmask, 1234 gateway); 1235 } 1236 } 1237 1238 inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address, 1239 INET_ADDRSTRLEN); 1240 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1241 1242 printf("%s: Broadcast (%s)\n", NAME, address); 1243 1244 return EOK; 1245 } 1246 1247 static int 1248 ip_send_msg_local(int il_phone, device_id_t device_id, packet_t *packet, 1249 services_t sender, services_t error) 1138 static int ip_send_msg_local(int il_phone, device_id_t device_id, 1139 packet_t *packet, services_t sender, services_t error) 1250 1140 { 1251 1141 int addrlen; … … 1288 1178 if (device_id > 0) { 1289 1179 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1290 route = ip_netif_find_route(netif, * dest);1180 route = ip_netif_find_route(netif, *dest); 1291 1181 if (netif && !route && (ip_globals.gateway.netif == netif)) 1292 1182 route = &ip_globals.gateway; … … 1318 1208 } 1319 1209 } 1320 1210 1321 1211 // if the local host is the destination 1322 1212 if ((route->address.s_addr == dest->s_addr) && … … 1351 1241 } 1352 1242 1243 /** Updates the device state. 1244 * 1245 * @param[in] device_id The device identifier. 1246 * @param[in] state The new state value. 1247 * @return EOK on success. 1248 * @return ENOENT if device is not found. 1249 */ 1250 static int ip_device_state_message(device_id_t device_id, device_state_t state) 1251 { 1252 ip_netif_t *netif; 1253 1254 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1255 // find the device 1256 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1257 if (!netif) { 1258 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1259 return ENOENT; 1260 } 1261 netif->state = state; 1262 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1263 1264 printf("%s: Device %d changed state to %d\n", NAME, device_id, state); 1265 1266 return EOK; 1267 } 1268 1269 /** Returns the packet destination address from the IP header. 1270 * 1271 * @param[in] header The packet IP header to be read. 1272 * @return The packet destination address. 1273 */ 1274 static in_addr_t ip_get_destination(ip_header_t *header) 1275 { 1276 in_addr_t destination; 1277 1278 // TODO search set ipopt route? 1279 destination.s_addr = header->destination_address; 1280 return destination; 1281 } 1282 1283 /** Delivers the packet to the local host. 1284 * 1285 * The packet is either passed to another module or released on error. 1286 * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not 1287 * found. 1288 * 1289 * @param[in] device_id The source device identifier. 1290 * @param[in] packet The packet to be delivered. 1291 * @param[in] header The first packet IP header. May be NULL. 1292 * @param[in] error The packet error service. 1293 * @return EOK on success. 1294 * @return ENOTSUP if the packet is a fragment. 1295 * @return EAFNOSUPPORT if the address family is not supported. 1296 * @return ENOENT if the target protocol is not found. 1297 * @return Other error codes as defined for the packet_set_addr() 1298 * function. 1299 * @return Other error codes as defined for the packet_trim() 1300 * function. 1301 * @return Other error codes as defined for the protocol specific 1302 * tl_received_msg() function. 1303 */ 1304 static int ip_deliver_local(device_id_t device_id, packet_t *packet, 1305 ip_header_t *header, services_t error) 1306 { 1307 ip_proto_t *proto; 1308 int phone; 1309 services_t service; 1310 tl_received_msg_t received_msg; 1311 struct sockaddr *src; 1312 struct sockaddr *dest; 1313 struct sockaddr_in src_in; 1314 struct sockaddr_in dest_in; 1315 socklen_t addrlen; 1316 int rc; 1317 1318 if ((header->flags & IPFLAG_MORE_FRAGMENTS) || 1319 IP_FRAGMENT_OFFSET(header)) { 1320 // TODO fragmented 1321 return ENOTSUP; 1322 } 1323 1324 switch (header->version) { 1325 case IPVERSION: 1326 addrlen = sizeof(src_in); 1327 bzero(&src_in, addrlen); 1328 src_in.sin_family = AF_INET; 1329 memcpy(&dest_in, &src_in, addrlen); 1330 memcpy(&src_in.sin_addr.s_addr, &header->source_address, 1331 sizeof(header->source_address)); 1332 memcpy(&dest_in.sin_addr.s_addr, &header->destination_address, 1333 sizeof(header->destination_address)); 1334 src = (struct sockaddr *) &src_in; 1335 dest = (struct sockaddr *) &dest_in; 1336 break; 1337 1338 default: 1339 return ip_release_and_return(packet, EAFNOSUPPORT); 1340 } 1341 1342 rc = packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest, 1343 addrlen); 1344 if (rc != EOK) 1345 return ip_release_and_return(packet, rc); 1346 1347 // trim padding if present 1348 if (!error && 1349 (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) { 1350 rc = packet_trim(packet, 0, 1351 packet_get_data_length(packet) - IP_TOTAL_LENGTH(header)); 1352 if (rc != EOK) 1353 return ip_release_and_return(packet, rc); 1354 } 1355 1356 fibril_rwlock_read_lock(&ip_globals.protos_lock); 1357 1358 proto = ip_protos_find(&ip_globals.protos, header->protocol); 1359 if (!proto) { 1360 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1361 phone = ip_prepare_icmp_and_get_phone(error, packet, header); 1362 if (phone >= 0) { 1363 // unreachable ICMP 1364 icmp_destination_unreachable_msg(phone, 1365 ICMP_PROT_UNREACH, 0, packet); 1366 } 1367 return ENOENT; 1368 } 1369 1370 if (proto->received_msg) { 1371 service = proto->service; 1372 received_msg = proto->received_msg; 1373 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1374 rc = received_msg(device_id, packet, service, error); 1375 } else { 1376 rc = tl_received_msg(proto->phone, device_id, packet, 1377 proto->service, error); 1378 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1379 } 1380 1381 return rc; 1382 } 1383 1384 /** Processes the received packet. 1385 * 1386 * The packet is either passed to another module or released on error. 1387 * 1388 * The ICMP_PARAM_POINTER error notification may be sent if the checksum is 1389 * invalid. 1390 * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two. 1391 * The ICMP_HOST_UNREACH error notification may be sent if no route was found. 1392 * The ICMP_HOST_UNREACH error notification may be sent if the packet is for 1393 * another host and the routing is disabled. 1394 * 1395 * @param[in] device_id The source device identifier. 1396 * @param[in] packet The received packet to be processed. 1397 * @return EOK on success. 1398 * @return EINVAL if the TTL is less than two. 1399 * @return EINVAL if the checksum is invalid. 1400 * @return EAFNOSUPPORT if the address family is not supported. 1401 * @return ENOENT if no route was found. 1402 * @return ENOENT if the packet is for another host and the routing 1403 * is disabled. 1404 */ 1405 static int ip_process_packet(device_id_t device_id, packet_t *packet) 1406 { 1407 ip_header_t *header; 1408 in_addr_t dest; 1409 ip_route_t *route; 1410 int phone; 1411 struct sockaddr *addr; 1412 struct sockaddr_in addr_in; 1413 socklen_t addrlen; 1414 int rc; 1415 1416 header = (ip_header_t *) packet_get_data(packet); 1417 if (!header) 1418 return ip_release_and_return(packet, ENOMEM); 1419 1420 // checksum 1421 if ((header->header_checksum) && 1422 (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) { 1423 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1424 if (phone >= 0) { 1425 // checksum error ICMP 1426 icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER, 1427 ((size_t) ((void *) &header->header_checksum)) - 1428 ((size_t) ((void *) header)), packet); 1429 } 1430 return EINVAL; 1431 } 1432 1433 if (header->ttl <= 1) { 1434 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1435 if (phone >= 0) { 1436 // ttl exceeded ICMP 1437 icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet); 1438 } 1439 return EINVAL; 1440 } 1441 1442 // process ipopt and get destination 1443 dest = ip_get_destination(header); 1444 1445 // set the addrination address 1446 switch (header->version) { 1447 case IPVERSION: 1448 addrlen = sizeof(addr_in); 1449 bzero(&addr_in, addrlen); 1450 addr_in.sin_family = AF_INET; 1451 memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest)); 1452 addr = (struct sockaddr *) &addr_in; 1453 break; 1454 1455 default: 1456 return ip_release_and_return(packet, EAFNOSUPPORT); 1457 } 1458 1459 rc = packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen); 1460 if (rc != EOK) 1461 return rc; 1462 1463 route = ip_find_route(dest); 1464 if (!route) { 1465 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1466 if (phone >= 0) { 1467 // unreachable ICMP 1468 icmp_destination_unreachable_msg(phone, 1469 ICMP_HOST_UNREACH, 0, packet); 1470 } 1471 return ENOENT; 1472 } 1473 1474 if (route->address.s_addr == dest.s_addr) { 1475 // local delivery 1476 return ip_deliver_local(device_id, packet, header, 0); 1477 } 1478 1479 if (route->netif->routing) { 1480 header->ttl--; 1481 return ip_send_route(packet, route->netif, route, NULL, dest, 1482 0); 1483 } 1484 1485 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1486 if (phone >= 0) { 1487 // unreachable ICMP if no routing 1488 icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, 1489 packet); 1490 } 1491 1492 return ENOENT; 1493 } 1494 1353 1495 /** Returns the device packet dimensions for sending. 1354 1496 * … … 1362 1504 * @return EOK on success. 1363 1505 */ 1364 static int 1365 ip_packet_size_message(device_id_t device_id, size_t *addr_len, size_t *prefix, 1366 size_t *content, size_t *suffix) 1506 static int ip_packet_size_message(device_id_t device_id, size_t *addr_len, 1507 size_t *prefix, size_t *content, size_t *suffix) 1367 1508 { 1368 1509 ip_netif_t *netif; … … 1414 1555 } 1415 1556 1416 /** Returns the packet destination address from the IP header. 1417 * 1418 * @param[in] header The packet IP header to be read. 1419 * @return The packet destination address. 1420 */ 1421 static in_addr_t ip_get_destination(ip_header_t *header) 1422 { 1423 in_addr_t destination; 1424 1425 // TODO search set ipopt route? 1426 destination.s_addr = header->destination_address; 1427 return destination; 1428 } 1429 1430 /** Delivers the packet to the local host. 1431 * 1432 * The packet is either passed to another module or released on error. 1433 * The ICMP_PROT_UNREACH error notification may be sent if the protocol is not 1434 * found. 1435 * 1436 * @param[in] device_id The source device identifier. 1437 * @param[in] packet The packet to be delivered. 1438 * @param[in] header The first packet IP header. May be NULL. 1439 * @param[in] error The packet error service. 1557 /** Updates the device content length according to the new MTU value. 1558 * 1559 * @param[in] device_id The device identifier. 1560 * @param[in] mtu The new mtu value. 1440 1561 * @return EOK on success. 1441 * @return ENOTSUP if the packet is a fragment. 1442 * @return EAFNOSUPPORT if the address family is not supported. 1443 * @return ENOENT if the target protocol is not found. 1444 * @return Other error codes as defined for the packet_set_addr() 1445 * function. 1446 * @return Other error codes as defined for the packet_trim() 1447 * function. 1448 * @return Other error codes as defined for the protocol specific 1449 * tl_received_msg() function. 1562 * @return ENOENT if device is not found. 1563 */ 1564 static int ip_mtu_changed_message(device_id_t device_id, size_t mtu) 1565 { 1566 ip_netif_t *netif; 1567 1568 fibril_rwlock_write_lock(&ip_globals.netifs_lock); 1569 netif = ip_netifs_find(&ip_globals.netifs, device_id); 1570 if (!netif) { 1571 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1572 return ENOENT; 1573 } 1574 netif->packet_dimension.content = mtu; 1575 fibril_rwlock_write_unlock(&ip_globals.netifs_lock); 1576 1577 printf("%s: Device %d changed MTU to %zu\n", NAME, device_id, mtu); 1578 1579 return EOK; 1580 } 1581 1582 /** Process IPC messages from the registered device driver modules 1583 * 1584 * @param[in] iid Message identifier. 1585 * @param[in,out] icall Message parameters. 1586 * 1587 */ 1588 static void ip_receiver(ipc_callid_t iid, ipc_call_t *icall) 1589 { 1590 packet_t *packet; 1591 int rc; 1592 1593 while (true) { 1594 switch (IPC_GET_IMETHOD(*icall)) { 1595 case NET_IL_DEVICE_STATE: 1596 rc = ip_device_state_message(IPC_GET_DEVICE(*icall), 1597 IPC_GET_STATE(*icall)); 1598 ipc_answer_0(iid, (sysarg_t) rc); 1599 break; 1600 1601 case NET_IL_RECEIVED: 1602 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1603 IPC_GET_PACKET(*icall)); 1604 if (rc == EOK) { 1605 do { 1606 packet_t *next = pq_detach(packet); 1607 ip_process_packet(IPC_GET_DEVICE(*icall), packet); 1608 packet = next; 1609 } while (packet); 1610 } 1611 1612 ipc_answer_0(iid, (sysarg_t) rc); 1613 break; 1614 1615 case NET_IL_MTU_CHANGED: 1616 rc = ip_mtu_changed_message(IPC_GET_DEVICE(*icall), 1617 IPC_GET_MTU(*icall)); 1618 ipc_answer_0(iid, (sysarg_t) rc); 1619 break; 1620 1621 default: 1622 ipc_answer_0(iid, (sysarg_t) ENOTSUP); 1623 } 1624 1625 iid = async_get_call(icall); 1626 } 1627 } 1628 1629 /** Registers the transport layer protocol. 1630 * 1631 * The traffic of this protocol will be supplied using either the receive 1632 * function or IPC message. 1633 * 1634 * @param[in] protocol The transport layer module protocol. 1635 * @param[in] service The transport layer module service. 1636 * @param[in] phone The transport layer module phone. 1637 * @param[in] received_msg The receiving function. 1638 * @return EOK on success. 1639 * @return EINVAL if the protocol parameter and/or the service 1640 * parameter is zero. 1641 * @return EINVAL if the phone parameter is not a positive number 1642 * and the tl_receive_msg is NULL. 1643 * @return ENOMEM if there is not enough memory left. 1450 1644 */ 1451 1645 static int 1452 ip_ deliver_local(device_id_t device_id, packet_t *packet, ip_header_t *header,1453 services_t error)1646 ip_register(int protocol, services_t service, int phone, 1647 tl_received_msg_t received_msg) 1454 1648 { 1455 1649 ip_proto_t *proto; 1456 int phone; 1457 services_t service; 1458 tl_received_msg_t received_msg; 1459 struct sockaddr *src; 1460 struct sockaddr *dest; 1461 struct sockaddr_in src_in; 1462 struct sockaddr_in dest_in; 1463 socklen_t addrlen; 1464 int rc; 1465 1466 if ((header->flags & IPFLAG_MORE_FRAGMENTS) || 1467 IP_FRAGMENT_OFFSET(header)) { 1468 // TODO fragmented 1469 return ENOTSUP; 1470 } 1471 1472 switch (header->version) { 1473 case IPVERSION: 1474 addrlen = sizeof(src_in); 1475 bzero(&src_in, addrlen); 1476 src_in.sin_family = AF_INET; 1477 memcpy(&dest_in, &src_in, addrlen); 1478 memcpy(&src_in.sin_addr.s_addr, &header->source_address, 1479 sizeof(header->source_address)); 1480 memcpy(&dest_in.sin_addr.s_addr, &header->destination_address, 1481 sizeof(header->destination_address)); 1482 src = (struct sockaddr *) &src_in; 1483 dest = (struct sockaddr *) &dest_in; 1484 break; 1485 1486 default: 1487 return ip_release_and_return(packet, EAFNOSUPPORT); 1488 } 1489 1490 rc = packet_set_addr(packet, (uint8_t *) src, (uint8_t *) dest, 1491 addrlen); 1492 if (rc != EOK) 1493 return ip_release_and_return(packet, rc); 1494 1495 // trim padding if present 1496 if (!error && 1497 (IP_TOTAL_LENGTH(header) < packet_get_data_length(packet))) { 1498 rc = packet_trim(packet, 0, 1499 packet_get_data_length(packet) - IP_TOTAL_LENGTH(header)); 1500 if (rc != EOK) 1501 return ip_release_and_return(packet, rc); 1502 } 1503 1504 fibril_rwlock_read_lock(&ip_globals.protos_lock); 1505 1506 proto = ip_protos_find(&ip_globals.protos, header->protocol); 1507 if (!proto) { 1508 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1509 phone = ip_prepare_icmp_and_get_phone(error, packet, header); 1510 if (phone >= 0) { 1511 // unreachable ICMP 1512 icmp_destination_unreachable_msg(phone, 1513 ICMP_PROT_UNREACH, 0, packet); 1514 } 1515 return ENOENT; 1516 } 1517 1518 if (proto->received_msg) { 1519 service = proto->service; 1520 received_msg = proto->received_msg; 1521 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1522 rc = received_msg(device_id, packet, service, error); 1523 } else { 1524 rc = tl_received_msg(proto->phone, device_id, packet, 1525 proto->service, error); 1526 fibril_rwlock_read_unlock(&ip_globals.protos_lock); 1527 } 1528 1529 return rc; 1530 } 1531 1532 /** Processes the received packet. 1533 * 1534 * The packet is either passed to another module or released on error. 1535 * 1536 * The ICMP_PARAM_POINTER error notification may be sent if the checksum is 1537 * invalid. 1538 * The ICMP_EXC_TTL error notification may be sent if the TTL is less than two. 1539 * The ICMP_HOST_UNREACH error notification may be sent if no route was found. 1540 * The ICMP_HOST_UNREACH error notification may be sent if the packet is for 1541 * another host and the routing is disabled. 1542 * 1543 * @param[in] device_id The source device identifier. 1544 * @param[in] packet The received packet to be processed. 1545 * @return EOK on success. 1546 * @return EINVAL if the TTL is less than two. 1547 * @return EINVAL if the checksum is invalid. 1548 * @return EAFNOSUPPORT if the address family is not supported. 1549 * @return ENOENT if no route was found. 1550 * @return ENOENT if the packet is for another host and the routing 1551 * is disabled. 1552 */ 1553 static int 1554 ip_process_packet(device_id_t device_id, packet_t *packet) 1555 { 1556 ip_header_t *header; 1557 in_addr_t dest; 1558 ip_route_t *route; 1559 int phone; 1560 struct sockaddr *addr; 1561 struct sockaddr_in addr_in; 1562 socklen_t addrlen; 1563 int rc; 1564 1565 header = (ip_header_t *) packet_get_data(packet); 1566 if (!header) 1567 return ip_release_and_return(packet, ENOMEM); 1568 1569 // checksum 1570 if ((header->header_checksum) && 1571 (IP_HEADER_CHECKSUM(header) != IP_CHECKSUM_ZERO)) { 1572 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1573 if (phone >= 0) { 1574 // checksum error ICMP 1575 icmp_parameter_problem_msg(phone, ICMP_PARAM_POINTER, 1576 ((size_t) ((void *) &header->header_checksum)) - 1577 ((size_t) ((void *) header)), packet); 1578 } 1650 int index; 1651 1652 if (!protocol || !service || ((phone < 0) && !received_msg)) 1579 1653 return EINVAL; 1580 } 1581 1582 if (header->ttl <= 1) { 1583 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1584 if (phone >= 0) { 1585 // ttl exceeded ICMP 1586 icmp_time_exceeded_msg(phone, ICMP_EXC_TTL, packet); 1587 } 1588 return EINVAL; 1589 } 1590 1591 // process ipopt and get destination 1592 dest = ip_get_destination(header); 1593 1594 // set the addrination address 1595 switch (header->version) { 1596 case IPVERSION: 1597 addrlen = sizeof(addr_in); 1598 bzero(&addr_in, addrlen); 1599 addr_in.sin_family = AF_INET; 1600 memcpy(&addr_in.sin_addr.s_addr, &dest, sizeof(dest)); 1601 addr = (struct sockaddr *) &addr_in; 1602 break; 1603 1604 default: 1605 return ip_release_and_return(packet, EAFNOSUPPORT); 1606 } 1607 1608 rc = packet_set_addr(packet, NULL, (uint8_t *) &addr, addrlen); 1609 if (rc != EOK) 1610 return rc; 1611 1612 route = ip_find_route(dest); 1613 if (!route) { 1614 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1615 if (phone >= 0) { 1616 // unreachable ICMP 1617 icmp_destination_unreachable_msg(phone, 1618 ICMP_HOST_UNREACH, 0, packet); 1619 } 1620 return ENOENT; 1621 } 1622 1623 if (route->address.s_addr == dest.s_addr) { 1624 // local delivery 1625 return ip_deliver_local(device_id, packet, header, 0); 1626 } 1627 1628 if (route->netif->routing) { 1629 header->ttl--; 1630 return ip_send_route(packet, route->netif, route, NULL, dest, 1631 0); 1632 } 1633 1634 phone = ip_prepare_icmp_and_get_phone(0, packet, header); 1635 if (phone >= 0) { 1636 // unreachable ICMP if no routing 1637 icmp_destination_unreachable_msg(phone, ICMP_HOST_UNREACH, 0, 1638 packet); 1639 } 1640 1641 return ENOENT; 1642 } 1654 1655 proto = (ip_proto_t *) malloc(sizeof(ip_protos_t)); 1656 if (!proto) 1657 return ENOMEM; 1658 1659 proto->protocol = protocol; 1660 proto->service = service; 1661 proto->phone = phone; 1662 proto->received_msg = received_msg; 1663 1664 fibril_rwlock_write_lock(&ip_globals.protos_lock); 1665 index = ip_protos_add(&ip_globals.protos, proto->protocol, proto); 1666 if (index < 0) { 1667 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1668 free(proto); 1669 return index; 1670 } 1671 fibril_rwlock_write_unlock(&ip_globals.protos_lock); 1672 1673 printf("%s: Protocol registered (protocol: %d, phone: %d)\n", 1674 NAME, proto->protocol, proto->phone); 1675 1676 return EOK; 1677 } 1678 1643 1679 1644 1680 static int … … 1756 1792 (header->destination_address & route->netmask.s_addr))) { 1757 1793 // clear the ARP mapping if any 1758 address.value = ( char*) &header->destination_address;1794 address.value = (uint8_t *) &header->destination_address; 1759 1795 address.length = sizeof(header->destination_address); 1760 1796 arp_clear_address_req(netif->arp->phone, … … 1841 1877 } 1842 1878 1843 /** Processes the received IP packet or the packet queue one by one.1844 *1845 * The packet is either passed to another module or released on error.1846 *1847 * @param[in] device_id The source device identifier.1848 * @param[in,out] packet The received packet.1849 * @return EOK on success and the packet is no longer needed.1850 * @return EINVAL if the packet is too small to carry the IP1851 * packet.1852 * @return EINVAL if the received address lengths differs from the1853 * registered values.1854 * @return ENOENT if the device is not found in the cache.1855 * @return ENOENT if the protocol for the device is not found in1856 * the cache.1857 * @return ENOMEM if there is not enough memory left.1858 */1859 static int ip_receive_message(device_id_t device_id, packet_t *packet)1860 {1861 packet_t *next;1862 1863 do {1864 next = pq_detach(packet);1865 ip_process_packet(device_id, packet);1866 packet = next;1867 } while (packet);1868 1869 return EOK;1870 }1871 1872 1879 /** Processes the IP message. 1873 1880 * … … 1881 1888 * 1882 1889 * @see ip_interface.h 1883 * @see il_ interface.h1890 * @see il_remote.h 1884 1891 * @see IS_NET_IP_MESSAGE() 1885 1892 */ 1886 int 1887 ip_message_standalone(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 1888 int *answer_count) 1893 int il_module_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 1894 size_t *answer_count) 1889 1895 { 1890 1896 packet_t *packet; 1891 1897 struct sockaddr *addr; 1898 void *header; 1899 size_t headerlen; 1892 1900 size_t addrlen; 1893 1901 size_t prefix; 1894 1902 size_t suffix; 1895 1903 size_t content; 1896 void *header;1897 size_t headerlen;1898 1904 device_id_t device_id; 1899 1905 int rc; … … 1905 1911 1906 1912 case IPC_M_CONNECT_TO_ME: 1907 return ip_register(IL_GET_PROTO( call), IL_GET_SERVICE(call),1908 IPC_GET_PHONE( call), NULL);1909 1910 case NET_I L_DEVICE:1911 return ip_device_req_local(0, IPC_GET_DEVICE( call),1912 IPC_GET_SERVICE( call));1913 1914 case NET_I L_SEND:1913 return ip_register(IL_GET_PROTO(*call), IL_GET_SERVICE(*call), 1914 IPC_GET_PHONE(*call), NULL); 1915 1916 case NET_IP_DEVICE: 1917 return ip_device_req_local(0, IPC_GET_DEVICE(*call), 1918 IPC_GET_SERVICE(*call)); 1919 1920 case NET_IP_RECEIVED_ERROR: 1915 1921 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1916 IPC_GET_PACKET( call));1922 IPC_GET_PACKET(*call)); 1917 1923 if (rc != EOK) 1918 1924 return rc; 1919 return ip_send_msg_local(0, IPC_GET_DEVICE(call), packet, 0, 1920 IPC_GET_ERROR(call)); 1921 1922 case NET_IL_DEVICE_STATE: 1923 return ip_device_state_message(IPC_GET_DEVICE(call), 1924 IPC_GET_STATE(call)); 1925 1926 case NET_IL_RECEIVED: 1927 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1928 IPC_GET_PACKET(call)); 1929 if (rc != EOK) 1930 return rc; 1931 return ip_receive_message(IPC_GET_DEVICE(call), packet); 1932 1933 case NET_IP_RECEIVED_ERROR: 1934 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1935 IPC_GET_PACKET(call)); 1936 if (rc != EOK) 1937 return rc; 1938 return ip_received_error_msg_local(0, IPC_GET_DEVICE(call), 1939 packet, IPC_GET_TARGET(call), IPC_GET_ERROR(call)); 1925 return ip_received_error_msg_local(0, IPC_GET_DEVICE(*call), 1926 packet, IPC_GET_TARGET(*call), IPC_GET_ERROR(*call)); 1940 1927 1941 1928 case NET_IP_ADD_ROUTE: 1942 return ip_add_route_req_local(0, IPC_GET_DEVICE( call),1943 IP_GET_ADDRESS( call), IP_GET_NETMASK(call),1944 IP_GET_GATEWAY( call));1929 return ip_add_route_req_local(0, IPC_GET_DEVICE(*call), 1930 IP_GET_ADDRESS(*call), IP_GET_NETMASK(*call), 1931 IP_GET_GATEWAY(*call)); 1945 1932 1946 1933 case NET_IP_SET_GATEWAY: 1947 return ip_set_gateway_req_local(0, IPC_GET_DEVICE( call),1948 IP_GET_GATEWAY( call));1934 return ip_set_gateway_req_local(0, IPC_GET_DEVICE(*call), 1935 IP_GET_GATEWAY(*call)); 1949 1936 1950 1937 case NET_IP_GET_ROUTE: … … 1954 1941 return rc; 1955 1942 1956 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL( call), addr,1943 rc = ip_get_route_req_local(0, IP_GET_PROTOCOL(*call), addr, 1957 1944 (socklen_t) addrlen, &device_id, &header, &headerlen); 1958 1945 if (rc != EOK) 1959 1946 return rc; 1960 1947 1961 IPC_SET_DEVICE( answer, device_id);1962 IP_SET_HEADERLEN( answer, headerlen);1948 IPC_SET_DEVICE(*answer, device_id); 1949 IP_SET_HEADERLEN(*answer, headerlen); 1963 1950 1964 1951 *answer_count = 2; … … 1971 1958 return rc; 1972 1959 1973 case NET_I L_PACKET_SPACE:1974 rc = ip_packet_size_message(IPC_GET_DEVICE( call), &addrlen,1960 case NET_IP_PACKET_SPACE: 1961 rc = ip_packet_size_message(IPC_GET_DEVICE(*call), &addrlen, 1975 1962 &prefix, &content, &suffix); 1976 1963 if (rc != EOK) 1977 1964 return rc; 1978 1965 1979 IPC_SET_ADDR( answer, addrlen);1980 IPC_SET_PREFIX( answer, prefix);1981 IPC_SET_CONTENT( answer, content);1982 IPC_SET_SUFFIX( answer, suffix);1966 IPC_SET_ADDR(*answer, addrlen); 1967 IPC_SET_PREFIX(*answer, prefix); 1968 IPC_SET_CONTENT(*answer, content); 1969 IPC_SET_SUFFIX(*answer, suffix); 1983 1970 *answer_count = 4; 1984 1971 return EOK; 1985 1972 1986 case NET_IL_MTU_CHANGED: 1987 return ip_mtu_changed_message(IPC_GET_DEVICE(call), 1988 IPC_GET_MTU(call)); 1973 case NET_IP_SEND: 1974 rc = packet_translate_remote(ip_globals.net_phone, &packet, 1975 IPC_GET_PACKET(*call)); 1976 if (rc != EOK) 1977 return rc; 1978 1979 return ip_send_msg_local(0, IPC_GET_DEVICE(*call), packet, 0, 1980 IPC_GET_ERROR(*call)); 1989 1981 } 1990 1982 … … 1992 1984 } 1993 1985 1994 /** Default thread for new connections.1995 *1996 * @param[in] iid The initial message identifier.1997 * @param[in] icall The initial message call structure.1998 */1999 static void il_client_connection(ipc_callid_t iid, ipc_call_t *icall)2000 {2001 /*2002 * Accept the connection2003 * - Answer the first IPC_M_CONNECT_ME_TO call.2004 */2005 ipc_answer_0(iid, EOK);2006 2007 while (true) {2008 ipc_call_t answer;2009 int answer_count;2010 2011 /* Clear the answer structure */2012 refresh_answer(&answer, &answer_count);2013 2014 /* Fetch the next message */2015 ipc_call_t call;2016 ipc_callid_t callid = async_get_call(&call);2017 2018 /* Process the message */2019 int res = il_module_message_standalone(callid, &call, &answer,2020 &answer_count);2021 2022 /*2023 * End if told to either by the message or the processing2024 * result.2025 */2026 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||2027 (res == EHANGUP)) {2028 return;2029 }2030 2031 /* Answer the message */2032 answer_call(callid, res, &answer, answer_count);2033 }2034 }2035 2036 /** Starts the module.2037 *2038 * @return EOK on success.2039 * @return Other error codes as defined for each specific module start function.2040 */2041 1986 int main(int argc, char *argv[]) 2042 1987 { 2043 int rc;2044 2045 1988 /* Start the module */ 2046 rc = il_module_start_standalone(il_client_connection); 2047 return rc; 1989 return il_module_start(SERVICE_IP); 2048 1990 } 2049 1991 -
uspace/srv/net/il/ip/ip.h
reaef141 r80cd7cd 138 138 /** IP global data. */ 139 139 struct ip_globals { 140 /** Default client connection function for support modules. */141 async_client_conn_t client_connection;142 140 /** Default gateway. */ 143 141 ip_route_t gateway; -
uspace/srv/net/net/net.c
reaef141 r80cd7cd 45 45 #include <stdio.h> 46 46 #include <str.h> 47 #include <str_error.h> 47 48 48 49 #include <ipc/ipc.h> … … 51 52 #include <ipc/net_net.h> 52 53 #include <ipc/il.h> 54 #include <ipc/nil.h> 53 55 54 56 #include <net/modules.h> … … 62 64 63 65 #include <netif_remote.h> 64 #include <nil_ interface.h>66 #include <nil_remote.h> 65 67 #include <net_interface.h> 66 68 #include <ip_interface.h> … … 90 92 * 91 93 */ 92 int add_configuration(measured_strings_t *configuration, const char*name,93 const char*value)94 int add_configuration(measured_strings_t *configuration, const uint8_t *name, 95 const uint8_t *value) 94 96 { 95 97 int rc; … … 119 121 } 120 122 121 static int parse_line(measured_strings_t *configuration, char*line)123 static int parse_line(measured_strings_t *configuration, uint8_t *line) 122 124 { 123 125 int rc; 124 126 125 127 /* From the beginning */ 126 char*name = line;128 uint8_t *name = line; 127 129 128 130 /* Skip comments and blank lines */ … … 135 137 136 138 /* Remember the name start */ 137 char*value = name;139 uint8_t *value = name; 138 140 139 141 /* Skip the name */ … … 186 188 187 189 /* Construct the full filename */ 188 char line[BUFFER_SIZE];189 if (snprintf( line, BUFFER_SIZE, "%s/%s", directory, filename) > BUFFER_SIZE)190 char fname[BUFFER_SIZE]; 191 if (snprintf(fname, BUFFER_SIZE, "%s/%s", directory, filename) > BUFFER_SIZE) 190 192 return EOVERFLOW; 191 193 192 194 /* Open the file */ 193 FILE *cfg = fopen( line, "r");195 FILE *cfg = fopen(fname, "r"); 194 196 if (!cfg) 195 197 return ENOENT; … … 201 203 unsigned int line_number = 0; 202 204 size_t index = 0; 205 uint8_t line[BUFFER_SIZE]; 206 203 207 while (!ferror(cfg) && !feof(cfg)) { 204 208 int read = fgetc(cfg); … … 207 211 line[BUFFER_SIZE - 1] = '\0'; 208 212 fprintf(stderr, "%s: Configuration line %u too " 209 "long: %s\n", NAME, line_number, line);213 "long: %s\n", NAME, line_number, (char *) line); 210 214 211 215 /* No space left in the line buffer */ … … 213 217 } 214 218 /* Append the character */ 215 line[index] = ( char) read;219 line[index] = (uint8_t) read; 216 220 index++; 217 221 } else { … … 221 225 if (parse_line(configuration, line) != EOK) { 222 226 fprintf(stderr, "%s: Configuration error on " 223 "line %u: %s\n", NAME, line_number, line);227 "line %u: %s\n", NAME, line_number, (char *) line); 224 228 } 225 229 … … 282 286 return rc; 283 287 284 rc = add_module(NULL, &net_globals.modules, LO_NAME, LO_FILENAME,285 SERVICE_LO, 0, connect_to_service);286 if (rc != EOK) 287 return rc; 288 rc = add_module(NULL, &net_globals.modules, DP8390_NAME,289 DP8390_FILENAME, SERVICE_DP8390, 0, connect_to_service);290 if (rc != EOK) 291 return rc; 292 rc = add_module(NULL, &net_globals.modules, ETHERNET_NAME,293 ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service);294 if (rc != EOK) 295 return rc; 296 rc = add_module(NULL, &net_globals.modules, NILDUMMY_NAME,297 NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service);288 rc = add_module(NULL, &net_globals.modules, (uint8_t *) LO_NAME, 289 (uint8_t *) LO_FILENAME, SERVICE_LO, 0, connect_to_service); 290 if (rc != EOK) 291 return rc; 292 rc = add_module(NULL, &net_globals.modules, (uint8_t *) NE2000_NAME, 293 (uint8_t *) NE2000_FILENAME, SERVICE_NE2000, 0, connect_to_service); 294 if (rc != EOK) 295 return rc; 296 rc = add_module(NULL, &net_globals.modules, (uint8_t *) ETHERNET_NAME, 297 (uint8_t *) ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service); 298 if (rc != EOK) 299 return rc; 300 rc = add_module(NULL, &net_globals.modules, (uint8_t *) NILDUMMY_NAME, 301 (uint8_t *) NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service); 298 302 if (rc != EOK) 299 303 return rc; … … 330 334 return rc; 331 335 332 333 336 rc = net_initialize(client_connection); 334 337 if (rc != EOK) … … 364 367 */ 365 368 static int net_get_conf(measured_strings_t *netif_conf, 366 measured_string_t *configuration, size_t count, char**data)369 measured_string_t *configuration, size_t count, uint8_t **data) 367 370 { 368 371 if (data) … … 390 393 391 394 int net_get_conf_req(int net_phone, measured_string_t **configuration, 392 size_t count, char**data)395 size_t count, uint8_t **data) 393 396 { 394 397 if (!configuration || (count <= 0)) … … 399 402 400 403 int net_get_device_conf_req(int net_phone, device_id_t device_id, 401 measured_string_t **configuration, size_t count, char**data)404 measured_string_t **configuration, size_t count, uint8_t **data) 402 405 { 403 406 if ((!configuration) || (count == 0)) … … 411 414 } 412 415 413 void net_free_settings(measured_string_t *settings, char*data)416 void net_free_settings(measured_string_t *settings, uint8_t *data) 414 417 { 415 418 } … … 437 440 /* Mandatory netif */ 438 441 measured_string_t *setting = 439 measured_strings_find(&netif->configuration, CONF_NETIF, 0);442 measured_strings_find(&netif->configuration, (uint8_t *) CONF_NETIF, 0); 440 443 441 444 netif->driver = get_running_module(&net_globals.modules, setting->value); … … 447 450 448 451 /* Optional network interface layer */ 449 setting = measured_strings_find(&netif->configuration, CONF_NIL, 0);452 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_NIL, 0); 450 453 if (setting) { 451 454 netif->nil = get_running_module(&net_globals.modules, setting->value); … … 459 462 460 463 /* Mandatory internet layer */ 461 setting = measured_strings_find(&netif->configuration, CONF_IL, 0);464 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IL, 0); 462 465 netif->il = get_running_module(&net_globals.modules, setting->value); 463 466 if (!netif->il) { … … 468 471 469 472 /* Hardware configuration */ 470 setting = measured_strings_find(&netif->configuration, CONF_IRQ, 0);471 int irq = setting ? strtol( setting->value, NULL, 10) : 0;472 473 setting = measured_strings_find(&netif->configuration, CONF_IO, 0);474 int io = setting ? strtol(setting->value, NULL, 16) : 0;475 476 rc = netif_probe_req _remote(netif->driver->phone, netif->id, irq,io);473 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IRQ, 0); 474 int irq = setting ? strtol((char *) setting->value, NULL, 10) : 0; 475 476 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_IO, 0); 477 uintptr_t io = setting ? strtol((char *) setting->value, NULL, 16) : 0; 478 479 rc = netif_probe_req(netif->driver->phone, netif->id, irq, (void *) io); 477 480 if (rc != EOK) 478 481 return rc; … … 481 484 services_t internet_service; 482 485 if (netif->nil) { 483 setting = measured_strings_find(&netif->configuration, CONF_MTU, 0);486 setting = measured_strings_find(&netif->configuration, (uint8_t *) CONF_MTU, 0); 484 487 if (!setting) 485 488 setting = measured_strings_find(&net_globals.configuration, 486 CONF_MTU, 0);487 488 int mtu = setting ? strtol( setting->value, NULL, 10) : 0;489 (uint8_t *) CONF_MTU, 0); 490 491 int mtu = setting ? strtol((char *) setting->value, NULL, 10) : 0; 489 492 490 493 rc = nil_device_req(netif->nil->phone, netif->id, mtu, … … 509 512 } 510 513 511 return netif_start_req _remote(netif->driver->phone, netif->id);514 return netif_start_req(netif->driver->phone, netif->id); 512 515 } 513 516 … … 558 561 /* Mandatory name */ 559 562 measured_string_t *setting = 560 measured_strings_find(&netif->configuration, CONF_NAME, 0);563 measured_strings_find(&netif->configuration, (uint8_t *) CONF_NAME, 0); 561 564 if (!setting) { 562 565 fprintf(stderr, "%s: Network interface name is missing\n", NAME); … … 589 592 rc = start_device(netif); 590 593 if (rc != EOK) { 594 printf("%s: Error starting interface %s (%s)\n", NAME, 595 netif->name, str_error(rc)); 591 596 measured_strings_destroy(&netif->configuration); 592 597 netifs_exclude_index(&net_globals.netifs, index); 598 593 599 return rc; 594 600 } … … 602 608 printf("%s: Network interface started (name: %s, id: %d, driver: %s, " 603 609 "nil: %s, il: %s)\n", NAME, netif->name, netif->id, 604 netif->driver->name, netif->nil ?netif->nil->name : "[none]",610 netif->driver->name, netif->nil ? (char *) netif->nil->name : "[none]", 605 611 netif->il->name); 606 612 } … … 611 617 /** Process the networking message. 612 618 * 613 * @param[in] callidThe message identifier.614 * @param[in] callThe message parameters.619 * @param[in] callid The message identifier. 620 * @param[in] call The message parameters. 615 621 * @param[out] answer The message answer parameters. 616 622 * @param[out] answer_count The last parameter for the actual answer … … 625 631 */ 626 632 int net_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer, 627 int *answer_count)633 size_t *answer_count) 628 634 { 629 635 measured_string_t *strings; 630 char*data;636 uint8_t *data; 631 637 int rc; 632 638 … … 637 643 case NET_NET_GET_DEVICE_CONF: 638 644 rc = measured_strings_receive(&strings, &data, 639 IPC_GET_COUNT( call));645 IPC_GET_COUNT(*call)); 640 646 if (rc != EOK) 641 647 return rc; 642 net_get_device_conf_req(0, IPC_GET_DEVICE( call), &strings,643 IPC_GET_COUNT( call), NULL);648 net_get_device_conf_req(0, IPC_GET_DEVICE(*call), &strings, 649 IPC_GET_COUNT(*call), NULL); 644 650 645 651 /* Strings should not contain received data anymore */ 646 652 free(data); 647 653 648 rc = measured_strings_reply(strings, IPC_GET_COUNT( call));654 rc = measured_strings_reply(strings, IPC_GET_COUNT(*call)); 649 655 free(strings); 650 656 return rc; 651 657 case NET_NET_GET_CONF: 652 658 rc = measured_strings_receive(&strings, &data, 653 IPC_GET_COUNT( call));659 IPC_GET_COUNT(*call)); 654 660 if (rc != EOK) 655 661 return rc; 656 net_get_conf_req(0, &strings, IPC_GET_COUNT( call), NULL);662 net_get_conf_req(0, &strings, IPC_GET_COUNT(*call), NULL); 657 663 658 664 /* Strings should not contain received data anymore */ 659 665 free(data); 660 666 661 rc = measured_strings_reply(strings, IPC_GET_COUNT( call));667 rc = measured_strings_reply(strings, IPC_GET_COUNT(*call)); 662 668 free(strings); 663 669 return rc; … … 686 692 /* Clear the answer structure */ 687 693 ipc_call_t answer; 688 int answer_count;694 size_t answer_count; 689 695 refresh_answer(&answer, &answer_count); 690 696 … … 707 713 int main(int argc, char *argv[]) 708 714 { 709 int rc; 710 711 rc = net_module_start(net_client_connection); 712 if (rc != EOK) { 713 fprintf(stderr, "%s: net_module_start error %i\n", NAME, rc); 714 return rc; 715 } 716 717 return EOK; 715 return net_module_start(net_client_connection); 718 716 } 719 717 -
uspace/srv/net/net/net.h
reaef141 r80cd7cd 52 52 */ 53 53 54 #define DP8390_FILENAME "/srv/dp8390"55 #define DP8390_NAME "dp8390"54 #define NE2000_FILENAME "/srv/ne2000" 55 #define NE2000_NAME "ne2000" 56 56 57 57 #define ETHERNET_FILENAME "/srv/eth" … … 105 105 module_t *driver; 106 106 107 device_id_t id; /**< System-unique network interface identifier. */108 module_t *il; /**< Serving internet layer module index. */109 char *name;/**< System-unique network interface name. */110 module_t *nil; /**< Serving link layer module index. */107 device_id_t id; /**< System-unique network interface identifier. */ 108 module_t *il; /**< Serving internet layer module index. */ 109 uint8_t *name; /**< System-unique network interface name. */ 110 module_t *nil; /**< Serving link layer module index. */ 111 111 } netif_t; 112 112 … … 133 133 } net_globals_t; 134 134 135 extern int add_configuration(measured_strings_t *, const char *, const char *); 136 extern int net_module_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, int *); 135 extern int add_configuration(measured_strings_t *, const uint8_t *, 136 const uint8_t *); 137 extern int net_module_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, size_t *); 137 138 extern int net_initialize_build(async_client_conn_t); 138 extern int net_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, int *);139 extern int net_message(ipc_callid_t, ipc_call_t *, ipc_call_t *, size_t *); 139 140 140 141 #endif -
uspace/srv/net/net/net_standalone.c
reaef141 r80cd7cd 63 63 int rc; 64 64 65 task_id_t task_id = spawn("/srv/ip");65 task_id_t task_id = net_spawn((uint8_t *) "/srv/ip"); 66 66 if (!task_id) 67 67 return EINVAL; 68 68 69 rc = add_module(NULL, &net_globals.modules, IP_NAME,70 IP_FILENAME, SERVICE_IP, task_id, ip_connect_module);69 rc = add_module(NULL, &net_globals.modules, (uint8_t *) IP_NAME, 70 (uint8_t *) IP_FILENAME, SERVICE_IP, task_id, ip_connect_module); 71 71 if (rc != EOK) 72 72 return rc; 73 73 74 if (! spawn("/srv/icmp"))74 if (!net_spawn((uint8_t *) "/srv/icmp")) 75 75 return EINVAL; 76 76 77 if (! spawn("/srv/udp"))77 if (!net_spawn((uint8_t *) "/srv/udp")) 78 78 return EINVAL; 79 79 80 if (! spawn("/srv/tcp"))80 if (!net_spawn((uint8_t *) "/srv/tcp")) 81 81 return EINVAL; 82 82 … … 100 100 */ 101 101 int net_module_message(ipc_callid_t callid, ipc_call_t *call, 102 ipc_call_t *answer, int *answer_count)102 ipc_call_t *answer, size_t *count) 103 103 { 104 if (IS_NET_PACKET_MESSAGE( call))105 return packet_server_message(callid, call, answer, answer_count);104 if (IS_NET_PACKET_MESSAGE(*call)) 105 return packet_server_message(callid, call, answer, count); 106 106 107 return net_message(callid, call, answer, answer_count);107 return net_message(callid, call, answer, count); 108 108 } 109 109 -
uspace/srv/net/netif/lo/lo.c
reaef141 r80cd7cd 48 48 #include <packet_client.h> 49 49 #include <net/device.h> 50 #include <nil_interface.h> 51 #include <netif_interface.h> 52 #include <netif_local.h> 53 54 /** Default hardware address. */ 55 #define DEFAULT_ADDR "\0\0\0\0\0\0" 50 #include <netif_skel.h> 51 #include <nil_remote.h> 56 52 57 53 /** Default address length. */ 58 #define DEFAULT_ADDR_LEN (sizeof(DEFAULT_ADDR) / sizeof(char))54 #define DEFAULT_ADDR_LEN 6 59 55 60 56 /** Loopback module name. */ 61 57 #define NAME "lo" 62 58 63 /** Network interface global data. */ 64 netif_globals_t netif_globals;59 static uint8_t default_addr[DEFAULT_ADDR_LEN] = 60 {0, 0, 0, 0, 0, 0}; 65 61 66 62 int netif_specific_message(ipc_callid_t callid, ipc_call_t *call, 67 ipc_call_t *answer, int *answer_count)63 ipc_call_t *answer, size_t *count) 68 64 { 69 65 return ENOTSUP; … … 74 70 if (!address) 75 71 return EBADMEM; 76 77 address->value = str_dup(DEFAULT_ADDR);72 73 address->value = default_addr; 78 74 address->length = DEFAULT_ADDR_LEN; 79 75 80 76 return EOK; 81 77 } … … 83 79 int netif_get_device_stats(device_id_t device_id, device_stats_t *stats) 84 80 { 85 netif_device_t *device;86 int rc;87 88 81 if (!stats) 89 82 return EBADMEM; 90 91 rc = find_device(device_id, &device); 83 84 netif_device_t *device; 85 int rc = find_device(device_id, &device); 92 86 if (rc != EOK) 93 87 return rc; 94 88 95 89 memcpy(stats, (device_stats_t *) device->specific, 96 90 sizeof(device_stats_t)); 97 98 return EOK; 99 } 100 101 /** Changes the loopback state. 102 * 103 * @param[in] device The device structure. 104 * @param[in] state The new device state. 105 * @return The new state if changed. 106 * @return EOK otherwise. 107 */ 108 static int change_state_message(netif_device_t *device, device_state_t state) 91 92 return EOK; 93 } 94 95 /** Change the loopback state. 96 * 97 * @param[in] device The device structure. 98 * @param[in] state The new device state. 99 * 100 * @return New state if changed. 101 * @return EOK otherwise. 102 * 103 */ 104 static void change_state_message(netif_device_t *device, device_state_t state) 109 105 { 110 106 if (device->state != state) { 111 107 device->state = state; 112 108 113 printf("%s: State changed to %s\n", NAME, 114 (state == NETIF_ACTIVE) ? "active" : "stopped"); 109 const char *desc; 110 switch (state) { 111 case NETIF_ACTIVE: 112 desc = "active"; 113 break; 114 case NETIF_STOPPED: 115 desc = "stopped"; 116 break; 117 default: 118 desc = "unknown"; 119 } 115 120 116 return state; 117 } 118 119 return EOK; 120 } 121 122 /** Creates and returns the loopback network interface structure. 123 * 124 * @param[in] device_id The new devce identifier. 125 * @param[out] device The device structure. 126 * @return EOK on success. 127 * @return EXDEV if one loopback network interface already exists. 128 * @return ENOMEM if there is not enough memory left. 129 */ 130 static int create(device_id_t device_id, netif_device_t **device) 131 { 132 int index; 133 121 printf("%s: State changed to %s\n", NAME, desc); 122 } 123 } 124 125 /** Create and return the loopback network interface structure. 126 * 127 * @param[in] device_id New devce identifier. 128 * @param[out] device Device structure. 129 * 130 * @return EOK on success. 131 * @return EXDEV if one loopback network interface already exists. 132 * @return ENOMEM if there is not enough memory left. 133 * 134 */ 135 static int lo_create(device_id_t device_id, netif_device_t **device) 136 { 134 137 if (netif_device_map_count(&netif_globals.device_map) > 0) 135 138 return EXDEV; 136 139 137 140 *device = (netif_device_t *) malloc(sizeof(netif_device_t)); 138 141 if (!*device) 139 142 return ENOMEM; 140 143 141 144 (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t)); 142 145 if (!(*device)->specific) { … … 144 147 return ENOMEM; 145 148 } 146 149 147 150 null_device_stats((device_stats_t *) (*device)->specific); 148 151 (*device)->device_id = device_id; 149 152 (*device)->nil_phone = -1; 150 153 (*device)->state = NETIF_STOPPED; 151 in dex = netif_device_map_add(&netif_globals.device_map,154 int index = netif_device_map_add(&netif_globals.device_map, 152 155 (*device)->device_id, *device); 153 156 154 157 if (index < 0) { 155 158 free(*device); … … 165 168 { 166 169 sysarg_t phonehash; 167 168 170 return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, &phonehash); 169 171 } 170 172 171 int netif_probe_message(device_id_t device_id, int irq, uintptr_t io) 172 { 173 int netif_probe_message(device_id_t device_id, int irq, void *io) 174 { 175 /* Create a new device */ 173 176 netif_device_t *device; 174 int rc; 175 176 /* Create a new device */ 177 rc = create(device_id, &device); 177 int rc = lo_create(device_id, &device); 178 178 if (rc != EOK) 179 179 return rc; 180 181 /* Print the settings */ 180 182 181 printf("%s: Device created (id: %d)\n", NAME, device->device_id); 183 184 182 return EOK; 185 183 } … … 188 186 { 189 187 netif_device_t *device; 190 size_t length; 191 packet_t *next; 192 int phone; 193 int rc; 194 195 rc = find_device(device_id, &device); 188 int rc = find_device(device_id, &device); 196 189 if (rc != EOK) 197 190 return EOK; 198 191 199 192 if (device->state != NETIF_ACTIVE) { 200 193 netif_pq_release(packet_get_id(packet)); 201 194 return EFORWARD; 202 195 } 203 204 next = packet;196 197 packet_t *next = packet; 205 198 do { 206 199 ((device_stats_t *) device->specific)->send_packets++; 207 200 ((device_stats_t *) device->specific)->receive_packets++; 208 length = packet_get_data_length(next);201 size_t length = packet_get_data_length(next); 209 202 ((device_stats_t *) device->specific)->send_bytes += length; 210 203 ((device_stats_t *) device->specific)->receive_bytes += length; 211 204 next = pq_next(next); 212 } while (next);213 214 phone = device->nil_phone;205 } while (next); 206 207 int phone = device->nil_phone; 215 208 fibril_rwlock_write_unlock(&netif_globals.lock); 209 216 210 nil_received_msg(phone, device_id, packet, sender); 211 217 212 fibril_rwlock_write_lock(&netif_globals.lock); 218 219 213 return EOK; 220 214 } … … 222 216 int netif_start_message(netif_device_t *device) 223 217 { 224 return change_state_message(device, NETIF_ACTIVE); 218 change_state_message(device, NETIF_ACTIVE); 219 return device->state; 225 220 } 226 221 227 222 int netif_stop_message(netif_device_t *device) 228 223 { 229 return change_state_message(device, NETIF_STOPPED); 230 } 231 232 /** Default thread for new connections. 233 * 234 * @param[in] iid The initial message identifier. 235 * @param[in] icall The initial message call structure. 236 */ 237 static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall) 238 { 239 /* 240 * Accept the connection 241 * - Answer the first IPC_M_CONNECT_ME_TO call. 242 */ 243 ipc_answer_0(iid, EOK); 244 245 while (true) { 246 ipc_call_t answer; 247 int answer_count; 248 249 /* Clear the answer structure */ 250 refresh_answer(&answer, &answer_count); 251 252 /* Fetch the next message */ 253 ipc_call_t call; 254 ipc_callid_t callid = async_get_call(&call); 255 256 /* Process the message */ 257 int res = netif_module_message(NAME, callid, &call, &answer, 258 &answer_count); 259 260 /* 261 * End if told to either by the message or the processing 262 * result. 263 */ 264 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) || 265 (res == EHANGUP)) 266 return; 267 268 /* Answer the message */ 269 answer_call(callid, res, &answer, answer_count); 270 } 224 change_state_message(device, NETIF_STOPPED); 225 return device->state; 271 226 } 272 227 273 228 int main(int argc, char *argv[]) 274 229 { 275 int rc;276 277 230 /* Start the module */ 278 rc = netif_module_start(netif_client_connection); 279 return rc; 231 return netif_module_start(); 280 232 } 281 233 -
uspace/srv/net/nil/eth/Makefile
reaef141 r80cd7cd 42 42 43 43 SOURCES = \ 44 eth.c \ 45 eth_module.c 44 eth.c 46 45 47 46 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/nil/eth/eth.c
reaef141 r80cd7cd 45 45 46 46 #include <ipc/ipc.h> 47 #include <ipc/nil.h> 47 48 #include <ipc/net.h> 48 49 #include <ipc/services.h> … … 54 55 #include <protocol_map.h> 55 56 #include <net/device.h> 56 #include <netif_ interface.h>57 #include <netif_remote.h> 57 58 #include <net_interface.h> 58 #include <nil_interface.h> 59 #include <il_interface.h> 59 #include <il_remote.h> 60 60 #include <adt/measured_strings.h> 61 61 #include <packet_client.h> 62 62 #include <packet_remote.h> 63 #include <nil_ local.h>63 #include <nil_skel.h> 64 64 65 65 #include "eth.h" 66 #include "eth_header.h"67 66 68 67 /** The module name. */ … … 72 71 #define ETH_PREFIX \ 73 72 (sizeof(eth_header_t) + sizeof(eth_header_lsap_t) + \ 74 sizeof(eth_header_snap_t))73 sizeof(eth_header_snap_t)) 75 74 76 75 /** Reserved packet suffix length. */ 77 #define ETH_SUFFIX \ 78 sizeof(eth_fcs_t) 76 #define ETH_SUFFIX (sizeof(eth_fcs_t)) 79 77 80 78 /** Maximum packet content length. */ 81 #define ETH_MAX_CONTENT 1500u79 #define ETH_MAX_CONTENT 1500u 82 80 83 81 /** Minimum packet content length. */ 84 #define ETH_MIN_CONTENT 46u82 #define ETH_MIN_CONTENT 46u 85 83 86 84 /** Maximum tagged packet content length. */ 87 85 #define ETH_MAX_TAGGED_CONTENT(flags) \ 88 86 (ETH_MAX_CONTENT - \ 89 ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \90 sizeof(eth_header_lsap_t) : 0) - \91 (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))87 ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \ 88 sizeof(eth_header_lsap_t) : 0) - \ 89 (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0)) 92 90 93 91 /** Minimum tagged packet content length. */ 94 92 #define ETH_MIN_TAGGED_CONTENT(flags) \ 95 93 (ETH_MIN_CONTENT - \ 96 ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \97 sizeof(eth_header_lsap_t) : 0) - \98 (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))94 ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? \ 95 sizeof(eth_header_lsap_t) : 0) - \ 96 (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0)) 99 97 100 98 /** Dummy flag shift value. */ 101 #define ETH_DUMMY_SHIFT 099 #define ETH_DUMMY_SHIFT 0 102 100 103 101 /** Mode flag shift value. */ 104 #define ETH_MODE_SHIFT 1102 #define ETH_MODE_SHIFT 1 105 103 106 104 /** Dummy device flag. 107 105 * Preamble and FCS are mandatory part of the packets. 108 106 */ 109 #define ETH_DUMMY (1 << ETH_DUMMY_SHIFT)107 #define ETH_DUMMY (1 << ETH_DUMMY_SHIFT) 110 108 111 109 /** Returns the dummy flag. 112 110 * @see ETH_DUMMY 113 111 */ 114 #define IS_DUMMY(flags) ((flags) & ETH_DUMMY)112 #define IS_DUMMY(flags) ((flags) & ETH_DUMMY) 115 113 116 114 /** Device mode flags. … … 119 117 * @see ETH_8023_2_SNAP 120 118 */ 121 #define ETH_MODE_MASK (3 << ETH_MODE_SHIFT)119 #define ETH_MODE_MASK (3 << ETH_MODE_SHIFT) 122 120 123 121 /** DIX Ethernet mode flag. */ 124 #define ETH_DIX (1 << ETH_MODE_SHIFT)125 126 /** Return swhether the DIX Ethernet mode flag is set.127 * 128 * @param[in] flags The ethernet flags.122 #define ETH_DIX (1 << ETH_MODE_SHIFT) 123 124 /** Return whether the DIX Ethernet mode flag is set. 125 * 126 * @param[in] flags Ethernet flags. 129 127 * @see ETH_DIX 130 */ 131 #define IS_DIX(flags) (((flags) & ETH_MODE_MASK) == ETH_DIX) 128 * 129 */ 130 #define IS_DIX(flags) (((flags) & ETH_MODE_MASK) == ETH_DIX) 132 131 133 132 /** 802.3 + 802.2 + LSAP mode flag. */ 134 #define ETH_8023_2_LSAP (2 << ETH_MODE_SHIFT)135 136 /** Return swhether the 802.3 + 802.2 + LSAP mode flag is set.137 * 138 * @param[in] flags The ethernet flags.133 #define ETH_8023_2_LSAP (2 << ETH_MODE_SHIFT) 134 135 /** Return whether the 802.3 + 802.2 + LSAP mode flag is set. 136 * 137 * @param[in] flags Ethernet flags. 139 138 * @see ETH_8023_2_LSAP 140 */ 141 #define IS_8023_2_LSAP(flags) (((flags) & ETH_MODE_MASK) == ETH_8023_2_LSAP) 139 * 140 */ 141 #define IS_8023_2_LSAP(flags) (((flags) & ETH_MODE_MASK) == ETH_8023_2_LSAP) 142 142 143 143 /** 802.3 + 802.2 + LSAP + SNAP mode flag. */ 144 #define ETH_8023_2_SNAP (3 << ETH_MODE_SHIFT)145 146 /** Return swhether the 802.3 + 802.2 + LSAP + SNAP mode flag is set.147 * 148 * @param[in] flags The ethernet flags.144 #define ETH_8023_2_SNAP (3 << ETH_MODE_SHIFT) 145 146 /** Return whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set. 147 * 148 * @param[in] flags Ethernet flags. 149 149 * @see ETH_8023_2_SNAP 150 */ 151 #define IS_8023_2_SNAP(flags) (((flags) & ETH_MODE_MASK) == ETH_8023_2_SNAP) 150 * 151 */ 152 #define IS_8023_2_SNAP(flags) (((flags) & ETH_MODE_MASK) == ETH_8023_2_SNAP) 152 153 153 154 /** Type definition of the ethernet address type. … … 201 202 202 203 eth_globals.broadcast_addr = 203 measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", ETH_ADDR);204 measured_string_create_bulk((uint8_t *) "\xFF\xFF\xFF\xFF\xFF\xFF", ETH_ADDR); 204 205 if (!eth_globals.broadcast_addr) { 205 206 rc = ENOMEM; … … 239 240 switch (IPC_GET_IMETHOD(*icall)) { 240 241 case NET_NIL_DEVICE_STATE: 241 nil_device_state_msg_local(0, IPC_GET_DEVICE( icall),242 IPC_GET_STATE( icall));242 nil_device_state_msg_local(0, IPC_GET_DEVICE(*icall), 243 IPC_GET_STATE(*icall)); 243 244 ipc_answer_0(iid, EOK); 244 245 break; 245 246 case NET_NIL_RECEIVED: 246 247 rc = packet_translate_remote(eth_globals.net_phone, 247 &packet, IPC_GET_PACKET( icall));248 if (rc == EOK) {248 &packet, IPC_GET_PACKET(*icall)); 249 if (rc == EOK) 249 250 rc = nil_received_msg_local(0, 250 IPC_GET_DEVICE( icall), packet, 0);251 }251 IPC_GET_DEVICE(*icall), packet, 0); 252 252 253 ipc_answer_0(iid, (sysarg_t) rc); 253 254 break; … … 284 285 measured_string_t names[2] = { 285 286 { 286 ( char*) "ETH_MODE",287 (uint8_t *) "ETH_MODE", 287 288 8 288 289 }, 289 290 { 290 ( char*) "ETH_DUMMY",291 (uint8_t *) "ETH_DUMMY", 291 292 9 292 293 } … … 294 295 measured_string_t *configuration; 295 296 size_t count = sizeof(names) / sizeof(measured_string_t); 296 char*data;297 uint8_t *data; 297 298 eth_proto_t *proto; 298 299 int rc; … … 358 359 359 360 if (configuration) { 360 if (!str_lcmp( configuration[0].value, "DIX",361 if (!str_lcmp((char *) configuration[0].value, "DIX", 361 362 configuration[0].length)) { 362 363 device->flags |= ETH_DIX; 363 } else if(!str_lcmp( configuration[0].value, "8023_2_LSAP",364 } else if(!str_lcmp((char *) configuration[0].value, "8023_2_LSAP", 364 365 configuration[0].length)) { 365 366 device->flags |= ETH_8023_2_LSAP; … … 407 408 408 409 printf("%s: Device registered (id: %d, service: %d: mtu: %zu, " 409 "mac: % x:%x:%x:%x:%x:%x, flags: 0x%x)\n",410 "mac: %02x:%02x:%02x:%02x:%02x:%02x, flags: 0x%x)\n", 410 411 NAME, device->device_id, device->service, device->mtu, 411 412 device->addr_data[0], device->addr_data[1], … … 836 837 } 837 838 838 int nil_m essage_standalone(const char *name, ipc_callid_t callid,839 ipc_call_t * call, ipc_call_t *answer, int *answer_count)839 int nil_module_message(ipc_callid_t callid, ipc_call_t *call, 840 ipc_call_t *answer, size_t *answer_count) 840 841 { 841 842 measured_string_t *address; … … 853 854 854 855 case NET_NIL_DEVICE: 855 return eth_device_message(IPC_GET_DEVICE( call),856 IPC_GET_SERVICE( call), IPC_GET_MTU(call));856 return eth_device_message(IPC_GET_DEVICE(*call), 857 IPC_GET_SERVICE(*call), IPC_GET_MTU(*call)); 857 858 case NET_NIL_SEND: 858 859 rc = packet_translate_remote(eth_globals.net_phone, &packet, 859 IPC_GET_PACKET( call));860 IPC_GET_PACKET(*call)); 860 861 if (rc != EOK) 861 862 return rc; 862 return eth_send_message(IPC_GET_DEVICE( call), packet,863 IPC_GET_SERVICE( call));863 return eth_send_message(IPC_GET_DEVICE(*call), packet, 864 IPC_GET_SERVICE(*call)); 864 865 case NET_NIL_PACKET_SPACE: 865 rc = eth_packet_space_message(IPC_GET_DEVICE( call), &addrlen,866 rc = eth_packet_space_message(IPC_GET_DEVICE(*call), &addrlen, 866 867 &prefix, &content, &suffix); 867 868 if (rc != EOK) 868 869 return rc; 869 IPC_SET_ADDR( answer, addrlen);870 IPC_SET_PREFIX( answer, prefix);871 IPC_SET_CONTENT( answer, content);872 IPC_SET_SUFFIX( answer, suffix);870 IPC_SET_ADDR(*answer, addrlen); 871 IPC_SET_PREFIX(*answer, prefix); 872 IPC_SET_CONTENT(*answer, content); 873 IPC_SET_SUFFIX(*answer, suffix); 873 874 *answer_count = 4; 874 875 return EOK; 875 876 case NET_NIL_ADDR: 876 rc = eth_addr_message(IPC_GET_DEVICE( call), ETH_LOCAL_ADDR,877 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_LOCAL_ADDR, 877 878 &address); 878 879 if (rc != EOK) … … 880 881 return measured_strings_reply(address, 1); 881 882 case NET_NIL_BROADCAST_ADDR: 882 rc = eth_addr_message(IPC_GET_DEVICE( call), ETH_BROADCAST_ADDR,883 rc = eth_addr_message(IPC_GET_DEVICE(*call), ETH_BROADCAST_ADDR, 883 884 &address); 884 885 if (rc != EOK) … … 886 887 return measured_strings_reply(address, 1); 887 888 case IPC_M_CONNECT_TO_ME: 888 return eth_register_message(NIL_GET_PROTO( call),889 IPC_GET_PHONE( call));889 return eth_register_message(NIL_GET_PROTO(*call), 890 IPC_GET_PHONE(*call)); 890 891 } 891 892 … … 893 894 } 894 895 895 /** Default thread for new connections.896 *897 * @param[in] iid The initial message identifier.898 * @param[in] icall The initial message call structure.899 */900 static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall)901 {902 /*903 * Accept the connection904 * - Answer the first IPC_M_CONNECT_ME_TO call.905 */906 ipc_answer_0(iid, EOK);907 908 while (true) {909 ipc_call_t answer;910 int answer_count;911 912 /* Clear the answer structure */913 refresh_answer(&answer, &answer_count);914 915 /* Fetch the next message */916 ipc_call_t call;917 ipc_callid_t callid = async_get_call(&call);918 919 /* Process the message */920 int res = nil_module_message_standalone(NAME, callid, &call,921 &answer, &answer_count);922 923 /*924 * End if told to either by the message or the processing925 * result.926 */927 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||928 (res == EHANGUP))929 return;930 931 /* Answer the message */932 answer_call(callid, res, &answer, answer_count);933 }934 }935 936 896 int main(int argc, char *argv[]) 937 897 { 938 int rc;939 940 898 /* Start the module */ 941 rc = nil_module_start_standalone(nil_client_connection); 942 return rc; 899 return nil_module_start(SERVICE_ETHERNET); 943 900 } 944 901 -
uspace/srv/net/nil/eth/eth.h
reaef141 r80cd7cd 44 44 #include <adt/measured_strings.h> 45 45 46 /** Ethernet address length. */ 47 #define ETH_ADDR 6 48 49 /** Ethernet header preamble value. */ 50 #define ETH_PREAMBLE 0x55 51 52 /** Ethernet header start of frame value. */ 53 #define ETH_SFD 0xD5 54 55 /** IEEE 802.2 unordered information control field. */ 56 #define IEEE_8023_2_UI 0x03 57 58 /** Type definition of the Ethernet header IEEE 802.3 + 802.2 + SNAP extensions. 59 * @see eth_header_snap 60 */ 61 typedef struct eth_header_snap eth_header_snap_t; 62 63 /** Type definition of the Ethernet header IEEE 802.3 + 802.2 + SNAP extensions. 64 * @see eth_header_lsap 65 */ 66 typedef struct eth_header_lsap eth_header_lsap_t; 67 68 /** Type definition of the Ethernet header LSAP extension. 69 * @see eth_ieee_lsap 70 */ 71 typedef struct eth_ieee_lsap eth_ieee_lsap_t; 72 73 /** Type definition of the Ethernet header SNAP extension. 74 * @see eth_snap 75 */ 76 typedef struct eth_snap eth_snap_t; 77 78 /** Type definition of the Ethernet header preamble. 79 * @see preamble 80 */ 81 typedef struct eth_preamble eth_preamble_t; 82 83 /** Type definition of the Ethernet header. 84 * @see eth_header 85 */ 86 typedef struct eth_header eth_header_t; 87 88 /** Ethernet header Link Service Access Point extension. */ 89 struct eth_ieee_lsap { 90 /** 91 * Destination Service Access Point identifier. 92 * The possible values are assigned by an IEEE committee. 93 */ 94 uint8_t dsap; 95 96 /** 97 * Source Service Access Point identifier. 98 * The possible values are assigned by an IEEE committee. 99 */ 100 uint8_t ssap; 101 102 /** 103 * Control parameter. 104 * The possible values are assigned by an IEEE committee. 105 */ 106 uint8_t ctrl; 107 } __attribute__ ((packed)); 108 109 /** Ethernet header SNAP extension. */ 110 struct eth_snap { 111 /** Protocol identifier or organization code. */ 112 uint8_t protocol[3]; 113 114 /** 115 * Ethernet protocol identifier in the network byte order (big endian). 116 * @see ethernet_protocols.h 117 */ 118 uint16_t ethertype; 119 } __attribute__ ((packed)); 120 121 /** Ethernet header preamble. 122 * 123 * Used for dummy devices. 124 */ 125 struct eth_preamble { 126 /** 127 * Controlling preamble used for the frame transmission synchronization. 128 * All should be set to ETH_PREAMBLE. 129 */ 130 uint8_t preamble[7]; 131 132 /** 133 * Start of Frame Delimiter used for the frame transmission 134 * synchronization. 135 * Should be set to ETH_SFD. 136 */ 137 uint8_t sfd; 138 } __attribute__ ((packed)); 139 140 /** Ethernet header. */ 141 struct eth_header { 142 /** Destination host Ethernet address (MAC address). */ 143 uint8_t destination_address[ETH_ADDR]; 144 /** Source host Ethernet address (MAC address). */ 145 uint8_t source_address[ETH_ADDR]; 146 147 /** 148 * Ethernet protocol identifier in the network byte order (big endian). 149 * @see ethernet_protocols.h 150 */ 151 uint16_t ethertype; 152 } __attribute__ ((packed)); 153 154 /** Ethernet header IEEE 802.3 + 802.2 extension. */ 155 struct eth_header_lsap { 156 /** Ethernet header. */ 157 eth_header_t header; 158 159 /** 160 * LSAP extension. 161 * If DSAP and SSAP are set to ETH_LSAP_SNAP the SNAP extension is being 162 * used. 163 * If DSAP and SSAP fields are equal to ETH_RAW the raw Ethernet packet 164 * without any extensions is being used and the frame content starts 165 * rigth after the two fields. 166 */ 167 eth_ieee_lsap_t lsap; 168 } __attribute__ ((packed)); 169 170 /** Ethernet header IEEE 802.3 + 802.2 + SNAP extensions. */ 171 struct eth_header_snap { 172 /** Ethernet header. */ 173 eth_header_t header; 174 175 /** 176 * LSAP extension. 177 * If DSAP and SSAP are set to ETH_LSAP_SNAP the SNAP extension is being 178 * used. 179 * If DSAP and SSAP fields are equal to ETH_RAW the raw Ethernet packet 180 * without any extensions is being used and the frame content starts 181 * rigth after the two fields. 182 */ 183 eth_ieee_lsap_t lsap; 184 185 /** SNAP extension. */ 186 eth_snap_t snap; 187 } __attribute__ ((packed)); 188 189 /** Ethernet Frame Check Sequence. */ 190 typedef uint32_t eth_fcs_t; 191 46 192 /** Type definition of the Ethernet global data. 47 193 * @see eth_globals … … 91 237 /** Actual device hardware address. */ 92 238 measured_string_t *addr; 239 93 240 /** Actual device hardware address data. */ 94 char*addr_data;241 uint8_t *addr_data; 95 242 }; 96 243 -
uspace/srv/net/nil/nildummy/Makefile
reaef141 r80cd7cd 42 42 43 43 SOURCES = \ 44 nildummy.c \ 45 nildummy_module.c 44 nildummy.c 46 45 47 46 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/nil/nildummy/nildummy.c
reaef141 r80cd7cd 42 42 #include <str.h> 43 43 #include <ipc/ipc.h> 44 #include <ipc/nil.h> 44 45 #include <ipc/net.h> 45 46 #include <ipc/services.h> … … 47 48 #include <net/modules.h> 48 49 #include <net/device.h> 49 #include <netif_interface.h> 50 #include <nil_interface.h> 51 #include <il_interface.h> 50 #include <il_remote.h> 52 51 #include <adt/measured_strings.h> 53 52 #include <net/packet.h> 54 53 #include <packet_remote.h> 55 #include <nil_local.h> 54 #include <netif_remote.h> 55 #include <nil_skel.h> 56 56 57 57 #include "nildummy.h" … … 81 81 int nil_initialize(int net_phone) 82 82 { 83 int rc;84 85 83 fibril_rwlock_initialize(&nildummy_globals.devices_lock); 86 84 fibril_rwlock_initialize(&nildummy_globals.protos_lock); … … 90 88 nildummy_globals.net_phone = net_phone; 91 89 nildummy_globals.proto.phone = 0; 92 rc = nildummy_devices_initialize(&nildummy_globals.devices);90 int rc = nildummy_devices_initialize(&nildummy_globals.devices); 93 91 94 92 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock); … … 98 96 } 99 97 100 /** Process IPC messages from the registered device driver modules in an101 * infinite loop.102 * 103 * @param[in ] iid The message identifier.104 * @param[in,out] icall The message parameters.98 /** Process IPC messages from the registered device driver modules 99 * 100 * @param[in] iid Message identifier. 101 * @param[in,out] icall Message parameters. 102 * 105 103 */ 106 104 static void nildummy_receiver(ipc_callid_t iid, ipc_call_t *icall) … … 108 106 packet_t *packet; 109 107 int rc; 110 108 111 109 while (true) { 112 110 switch (IPC_GET_IMETHOD(*icall)) { 113 111 case NET_NIL_DEVICE_STATE: 114 112 rc = nil_device_state_msg_local(0, 115 IPC_GET_DEVICE( icall), IPC_GET_STATE(icall));113 IPC_GET_DEVICE(*icall), IPC_GET_STATE(*icall)); 116 114 ipc_answer_0(iid, (sysarg_t) rc); 117 115 break; … … 119 117 case NET_NIL_RECEIVED: 120 118 rc = packet_translate_remote(nildummy_globals.net_phone, 121 &packet, IPC_GET_PACKET( icall));122 if (rc == EOK) {119 &packet, IPC_GET_PACKET(*icall)); 120 if (rc == EOK) 123 121 rc = nil_received_msg_local(0, 124 IPC_GET_DEVICE( icall), packet, 0);125 }122 IPC_GET_DEVICE(*icall), packet, 0); 123 126 124 ipc_answer_0(iid, (sysarg_t) rc); 127 125 break; … … 139 137 * Determine the device local hardware address. 140 138 * 141 * @param[in] device_id The new device identifier. 142 * @param[in] service The device driver service. 143 * @param[in] mtu The device maximum transmission unit. 144 * @return EOK on success. 145 * @return EEXIST if the device with the different service exists. 146 * @return ENOMEM if there is not enough memory left. 147 * @return Other error codes as defined for the 148 * netif_bind_service() function. 149 * @return Other error codes as defined for the 150 * netif_get_addr_req() function. 139 * @param[in] device_id New device identifier. 140 * @param[in] service Device driver service. 141 * @param[in] mtu Device maximum transmission unit. 142 * 143 * @return EOK on success. 144 * @return EEXIST if the device with the different service exists. 145 * @return ENOMEM if there is not enough memory left. 146 * @return Other error codes as defined for the 147 * netif_bind_service() function. 148 * @return Other error codes as defined for the 149 * netif_get_addr_req() function. 150 * 151 151 */ 152 152 static int nildummy_device_message(device_id_t device_id, services_t service, 153 153 size_t mtu) 154 154 { 155 nildummy_device_t *device;156 int index;157 int rc;158 159 155 fibril_rwlock_write_lock(&nildummy_globals.devices_lock); 160 156 161 157 /* An existing device? */ 162 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 158 nildummy_device_t *device = 159 nildummy_devices_find(&nildummy_globals.devices, device_id); 163 160 if (device) { 164 161 if (device->service != service) { … … 213 210 214 211 /* Get hardware address */ 215 rc = netif_get_addr_req(device->phone, device->device_id, &device->addr,216 &device->addr _data);212 int rc = netif_get_addr_req(device->phone, device->device_id, 213 &device->addr, &device->addr_data); 217 214 if (rc != EOK) { 218 215 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock); … … 222 219 223 220 /* Add to the cache */ 224 in dex = nildummy_devices_add(&nildummy_globals.devices,221 int index = nildummy_devices_add(&nildummy_globals.devices, 225 222 device->device_id, device); 226 223 if (index < 0) { … … 240 237 /** Return the device hardware address. 241 238 * 242 * @param[in] device_id The device identifier. 243 * @param[out] address The device hardware address. 244 * @return EOK on success. 245 * @return EBADMEM if the address parameter is NULL. 246 * @return ENOENT if there no such device. 239 * @param[in] device_id Device identifier. 240 * @param[out] address Device hardware address. 241 * 242 * @return EOK on success. 243 * @return EBADMEM if the address parameter is NULL. 244 * @return ENOENT if there no such device. 247 245 * 248 246 */ … … 250 248 measured_string_t **address) 251 249 { 252 nildummy_device_t *device;253 254 250 if (!address) 255 251 return EBADMEM; 256 252 257 253 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 258 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 254 255 nildummy_device_t *device = 256 nildummy_devices_find(&nildummy_globals.devices, device_id); 259 257 if (!device) { 260 258 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 261 259 return ENOENT; 262 260 } 261 263 262 *address = device->addr; 263 264 264 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 265 265 … … 269 269 /** Return the device packet dimensions for sending. 270 270 * 271 * @param[in] device_id The device identifier. 272 * @param[out] addr_len The minimum reserved address length. 273 * @param[out] prefix The minimum reserved prefix size. 274 * @param[out] content The maximum content size. 275 * @param[out] suffix The minimum reserved suffix size. 276 * @return EOK on success. 277 * @return EBADMEM if either one of the parameters is NULL. 278 * @return ENOENT if there is no such device. 271 * @param[in] device_id Device identifier. 272 * @param[out] addr_len Minimum reserved address length. 273 * @param[out] prefix Minimum reserved prefix size. 274 * @param[out] content Maximum content size. 275 * @param[out] suffix Minimum reserved suffix size. 276 * 277 * @return EOK on success. 278 * @return EBADMEM if either one of the parameters is NULL. 279 * @return ENOENT if there is no such device. 279 280 * 280 281 */ … … 282 283 size_t *prefix, size_t *content, size_t *suffix) 283 284 { 284 nildummy_device_t *device; 285 286 if (!addr_len || !prefix || !content || !suffix) 285 if ((!addr_len) || (!prefix) || (!content) || (!suffix)) 287 286 return EBADMEM; 288 287 289 288 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 290 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 289 290 nildummy_device_t *device = 291 nildummy_devices_find(&nildummy_globals.devices, device_id); 291 292 if (!device) { 292 293 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 293 294 return ENOENT; 294 295 } 295 296 296 297 *content = device->mtu; 298 297 299 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 298 300 … … 306 308 packet_t *packet, services_t target) 307 309 { 308 packet_t *next;309 310 310 fibril_rwlock_read_lock(&nildummy_globals.protos_lock); 311 311 312 if (nildummy_globals.proto.phone) { 312 313 do { 313 next = pq_detach(packet);314 packet_t *next = pq_detach(packet); 314 315 il_received_msg(nildummy_globals.proto.phone, device_id, 315 316 packet, nildummy_globals.proto.service); 316 317 packet = next; 317 } while(packet); 318 } 318 } while (packet); 319 } 320 319 321 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock); 320 322 … … 326 328 * Pass received packets for this service. 327 329 * 328 * @param[in] service The module service. 329 * @param[in] phone The service phone. 330 * @return EOK on success. 331 * @return ENOENT if the service is not known. 332 * @return ENOMEM if there is not enough memory left. 330 * @param[in] service Module service. 331 * @param[in] phone Service phone. 332 * 333 * @return EOK on success. 334 * @return ENOENT if the service is not known. 335 * @return ENOMEM if there is not enough memory left. 336 * 333 337 */ 334 338 static int nildummy_register_message(services_t service, int phone) … … 347 351 /** Send the packet queue. 348 352 * 349 * @param[in] device_id The device identifier. 350 * @param[in] packet The packet queue. 351 * @param[in] sender The sending module service. 352 * @return EOK on success. 353 * @return ENOENT if there no such device. 354 * @return EINVAL if the service parameter is not known. 353 * @param[in] device_id Device identifier. 354 * @param[in] packet Packet queue. 355 * @param[in] sender Sending module service. 356 * 357 * @return EOK on success. 358 * @return ENOENT if there no such device. 359 * @return EINVAL if the service parameter is not known. 360 * 355 361 */ 356 362 static int nildummy_send_message(device_id_t device_id, packet_t *packet, 357 363 services_t sender) 358 364 { 359 nildummy_device_t *device;360 361 365 fibril_rwlock_read_lock(&nildummy_globals.devices_lock); 362 device = nildummy_devices_find(&nildummy_globals.devices, device_id); 366 367 nildummy_device_t *device = 368 nildummy_devices_find(&nildummy_globals.devices, device_id); 363 369 if (!device) { 364 370 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 365 371 return ENOENT; 366 372 } 367 373 368 374 /* Send packet queue */ 369 375 if (packet) 370 376 netif_send_msg(device->phone, device_id, packet, 371 377 SERVICE_NILDUMMY); 378 372 379 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock); 373 return EOK; 374 } 375 376 int nil_message_standalone(const char *name, ipc_callid_t callid, 377 ipc_call_t *call, ipc_call_t *answer, int *answer_count) 380 381 return EOK; 382 } 383 384 int nil_module_message(ipc_callid_t callid, ipc_call_t *call, 385 ipc_call_t *answer, size_t *answer_count) 378 386 { 379 387 measured_string_t *address; … … 391 399 392 400 case NET_NIL_DEVICE: 393 return nildummy_device_message(IPC_GET_DEVICE( call),394 IPC_GET_SERVICE( call), IPC_GET_MTU(call));401 return nildummy_device_message(IPC_GET_DEVICE(*call), 402 IPC_GET_SERVICE(*call), IPC_GET_MTU(*call)); 395 403 396 404 case NET_NIL_SEND: 397 405 rc = packet_translate_remote(nildummy_globals.net_phone, 398 &packet, IPC_GET_PACKET( call));406 &packet, IPC_GET_PACKET(*call)); 399 407 if (rc != EOK) 400 408 return rc; 401 return nildummy_send_message(IPC_GET_DEVICE( call), packet,402 IPC_GET_SERVICE( call));409 return nildummy_send_message(IPC_GET_DEVICE(*call), packet, 410 IPC_GET_SERVICE(*call)); 403 411 404 412 case NET_NIL_PACKET_SPACE: 405 rc = nildummy_packet_space_message(IPC_GET_DEVICE( call),413 rc = nildummy_packet_space_message(IPC_GET_DEVICE(*call), 406 414 &addrlen, &prefix, &content, &suffix); 407 415 if (rc != EOK) 408 416 return rc; 409 IPC_SET_ADDR( answer, addrlen);410 IPC_SET_PREFIX( answer, prefix);411 IPC_SET_CONTENT( answer, content);412 IPC_SET_SUFFIX( answer, suffix);417 IPC_SET_ADDR(*answer, addrlen); 418 IPC_SET_PREFIX(*answer, prefix); 419 IPC_SET_CONTENT(*answer, content); 420 IPC_SET_SUFFIX(*answer, suffix); 413 421 *answer_count = 4; 414 422 return EOK; 415 423 416 424 case NET_NIL_ADDR: 417 rc = nildummy_addr_message(IPC_GET_DEVICE( call), &address);425 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 418 426 if (rc != EOK) 419 427 return rc; … … 421 429 422 430 case NET_NIL_BROADCAST_ADDR: 423 rc = nildummy_addr_message(IPC_GET_DEVICE( call), &address);431 rc = nildummy_addr_message(IPC_GET_DEVICE(*call), &address); 424 432 if (rc != EOK) 425 433 return rc; … … 427 435 428 436 case IPC_M_CONNECT_TO_ME: 429 return nildummy_register_message(NIL_GET_PROTO( call),430 IPC_GET_PHONE( call));437 return nildummy_register_message(NIL_GET_PROTO(*call), 438 IPC_GET_PHONE(*call)); 431 439 } 432 440 … … 434 442 } 435 443 436 /** Default thread for new connections.437 *438 * @param[in] iid The initial message identifier.439 * @param[in] icall The initial message call structure.440 */441 static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall)442 {443 /*444 * Accept the connection445 * - Answer the first IPC_M_CONNECT_ME_TO call.446 */447 ipc_answer_0(iid, EOK);448 449 while (true) {450 ipc_call_t answer;451 int answer_count;452 453 /* Clear the answer structure */454 refresh_answer(&answer, &answer_count);455 456 /* Fetch the next message */457 ipc_call_t call;458 ipc_callid_t callid = async_get_call(&call);459 460 /* Process the message */461 int res = nil_module_message_standalone(NAME, callid, &call,462 &answer, &answer_count);463 464 /*465 * End if told to either by the message or the processing466 * result.467 */468 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||469 (res == EHANGUP))470 return;471 472 /* Answer the message */473 answer_call(callid, res, &answer, answer_count);474 }475 }476 477 444 int main(int argc, char *argv[]) 478 445 { 479 int rc;480 481 446 /* Start the module */ 482 rc = nil_module_start_standalone(nil_client_connection); 483 return rc; 447 return nil_module_start(SERVICE_NILDUMMY); 484 448 } 485 449 -
uspace/srv/net/nil/nildummy/nildummy.h
reaef141 r80cd7cd 45 45 46 46 /** Type definition of the dummy nil global data. 47 * 47 48 * @see nildummy_globals 49 * 48 50 */ 49 51 typedef struct nildummy_globals nildummy_globals_t; 50 52 51 53 /** Type definition of the dummy nil device specific data. 54 * 52 55 * @see nildummy_device 56 * 53 57 */ 54 58 typedef struct nildummy_device nildummy_device_t; 55 59 56 60 /** Type definition of the dummy nil protocol specific data. 61 * 57 62 * @see nildummy_proto 63 * 58 64 */ 59 65 typedef struct nildummy_proto nildummy_proto_t; 60 66 61 67 /** Dummy nil device map. 62 * Maps devices to the dummy nil device specific data. 68 * 69 * Map devices to the dummy nil device specific data. 63 70 * @see device.h 71 * 64 72 */ 65 73 DEVICE_MAP_DECLARE(nildummy_devices, nildummy_device_t); … … 69 77 /** Device identifier. */ 70 78 device_id_t device_id; 79 71 80 /** Device driver service. */ 72 81 services_t service; 82 73 83 /** Driver phone. */ 74 84 int phone; 85 75 86 /** Maximal transmission unit. */ 76 87 size_t mtu; 88 77 89 /** Actual device hardware address. */ 78 90 measured_string_t *addr; 91 79 92 /** Actual device hardware address data. */ 80 char*addr_data;93 uint8_t *addr_data; 81 94 }; 82 95 … … 85 98 /** Protocol service. */ 86 99 services_t service; 100 87 101 /** Protocol module phone. */ 88 102 int phone; … … 93 107 /** Networking module phone. */ 94 108 int net_phone; 95 /** Safety lock for devices. */ 109 110 /** Lock for devices. */ 96 111 fibril_rwlock_t devices_lock; 112 97 113 /** All known Ethernet devices. */ 98 114 nildummy_devices_t devices; 115 99 116 /** Safety lock for protocols. */ 100 117 fibril_rwlock_t protos_lock; 118 101 119 /** Default protocol. */ 102 120 nildummy_proto_t proto; -
uspace/srv/net/tl/icmp/Makefile
reaef141 r80cd7cd 34 34 35 35 SOURCES = \ 36 icmp.c \ 37 icmp_module.c 36 icmp.c 38 37 39 38 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/tl/icmp/icmp.c
reaef141 r80cd7cd 35 35 * @see icmp.h 36 36 */ 37 38 #include "icmp.h"39 #include "icmp_module.h"40 37 41 38 #include <async.h> … … 68 65 #include <icmp_client.h> 69 66 #include <icmp_interface.h> 70 #include <il_ interface.h>67 #include <il_remote.h> 71 68 #include <ip_client.h> 72 69 #include <ip_interface.h> 73 70 #include <net_interface.h> 74 #include <tl_ interface.h>75 #include <tl_ local.h>71 #include <tl_remote.h> 72 #include <tl_skel.h> 76 73 #include <icmp_header.h> 77 74 75 #include "icmp.h" 76 78 77 /** ICMP module name. */ 79 #define NAME "ICMP protocol"78 #define NAME "icmp" 80 79 81 80 /** Default ICMP error reporting. */ … … 394 393 } 395 394 396 /** Initializes the ICMP module.397 *398 * @param[in] client_connection The client connection processing function. The399 * module skeleton propagates its own one.400 * @return EOK on success.401 * @return ENOMEM if there is not enough memory left.402 */403 int icmp_initialize(async_client_conn_t client_connection)404 {405 measured_string_t names[] = {406 {407 (char *) "ICMP_ERROR_REPORTING",408 20409 },410 {411 (char *) "ICMP_ECHO_REPLYING",412 18413 }414 };415 measured_string_t *configuration;416 size_t count = sizeof(names) / sizeof(measured_string_t);417 char *data;418 int rc;419 420 fibril_rwlock_initialize(&icmp_globals.lock);421 fibril_rwlock_write_lock(&icmp_globals.lock);422 icmp_replies_initialize(&icmp_globals.replies);423 icmp_echo_data_initialize(&icmp_globals.echo_data);424 425 icmp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_ICMP,426 SERVICE_ICMP, client_connection);427 if (icmp_globals.ip_phone < 0) {428 fibril_rwlock_write_unlock(&icmp_globals.lock);429 return icmp_globals.ip_phone;430 }431 432 rc = ip_packet_size_req(icmp_globals.ip_phone, -1,433 &icmp_globals.packet_dimension);434 if (rc != EOK) {435 fibril_rwlock_write_unlock(&icmp_globals.lock);436 return rc;437 }438 439 icmp_globals.packet_dimension.prefix += ICMP_HEADER_SIZE;440 icmp_globals.packet_dimension.content -= ICMP_HEADER_SIZE;441 442 icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING;443 icmp_globals.echo_replying = NET_DEFAULT_ICMP_ECHO_REPLYING;444 445 /* Get configuration */446 configuration = &names[0];447 rc = net_get_conf_req(icmp_globals.net_phone, &configuration, count,448 &data);449 if (rc != EOK) {450 fibril_rwlock_write_unlock(&icmp_globals.lock);451 return rc;452 }453 454 if (configuration) {455 if (configuration[0].value) {456 icmp_globals.error_reporting =457 (configuration[0].value[0] == 'y');458 }459 if (configuration[1].value) {460 icmp_globals.echo_replying =461 (configuration[1].value[0] == 'y');462 }463 net_free_settings(configuration, data);464 }465 466 fibril_rwlock_write_unlock(&icmp_globals.lock);467 return EOK;468 }469 470 395 /** Tries to set the pending reply result as the received message type. 471 396 * … … 529 454 icmp_code_t code; 530 455 int rc; 531 456 532 457 switch (error) { 533 458 case SERVICE_NONE: … … 670 595 } 671 596 597 /** Process IPC messages from the IP module 598 * 599 * @param[in] iid Message identifier. 600 * @param[in,out] icall Message parameters. 601 * 602 */ 603 static void icmp_receiver(ipc_callid_t iid, ipc_call_t *icall) 604 { 605 packet_t *packet; 606 int rc; 607 608 while (true) { 609 switch (IPC_GET_IMETHOD(*icall)) { 610 case NET_TL_RECEIVED: 611 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 612 IPC_GET_PACKET(*icall)); 613 if (rc == EOK) 614 rc = icmp_received_msg_local(IPC_GET_DEVICE(*icall), packet, 615 SERVICE_ICMP, IPC_GET_ERROR(*icall)); 616 617 ipc_answer_0(iid, (sysarg_t) rc); 618 break; 619 default: 620 ipc_answer_0(iid, (sysarg_t) ENOTSUP); 621 } 622 623 iid = async_get_call(icall); 624 } 625 } 626 627 /** Initialize the ICMP module. 628 * 629 * @param[in] net_phone Network module phone. 630 * 631 * @return EOK on success. 632 * @return ENOMEM if there is not enough memory left. 633 * 634 */ 635 int tl_initialize(int net_phone) 636 { 637 measured_string_t names[] = { 638 { 639 (uint8_t *) "ICMP_ERROR_REPORTING", 640 20 641 }, 642 { 643 (uint8_t *) "ICMP_ECHO_REPLYING", 644 18 645 } 646 }; 647 measured_string_t *configuration; 648 size_t count = sizeof(names) / sizeof(measured_string_t); 649 uint8_t *data; 650 651 fibril_rwlock_initialize(&icmp_globals.lock); 652 fibril_rwlock_write_lock(&icmp_globals.lock); 653 icmp_replies_initialize(&icmp_globals.replies); 654 icmp_echo_data_initialize(&icmp_globals.echo_data); 655 656 icmp_globals.net_phone = net_phone; 657 658 icmp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_ICMP, 659 SERVICE_ICMP, icmp_receiver); 660 if (icmp_globals.ip_phone < 0) { 661 fibril_rwlock_write_unlock(&icmp_globals.lock); 662 return icmp_globals.ip_phone; 663 } 664 665 int rc = ip_packet_size_req(icmp_globals.ip_phone, -1, 666 &icmp_globals.packet_dimension); 667 if (rc != EOK) { 668 fibril_rwlock_write_unlock(&icmp_globals.lock); 669 return rc; 670 } 671 672 icmp_globals.packet_dimension.prefix += ICMP_HEADER_SIZE; 673 icmp_globals.packet_dimension.content -= ICMP_HEADER_SIZE; 674 675 icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING; 676 icmp_globals.echo_replying = NET_DEFAULT_ICMP_ECHO_REPLYING; 677 678 /* Get configuration */ 679 configuration = &names[0]; 680 rc = net_get_conf_req(icmp_globals.net_phone, &configuration, count, 681 &data); 682 if (rc != EOK) { 683 fibril_rwlock_write_unlock(&icmp_globals.lock); 684 return rc; 685 } 686 687 if (configuration) { 688 if (configuration[0].value) { 689 icmp_globals.error_reporting = 690 (configuration[0].value[0] == 'y'); 691 } 692 if (configuration[1].value) { 693 icmp_globals.echo_replying = 694 (configuration[1].value[0] == 'y'); 695 } 696 net_free_settings(configuration, data); 697 } 698 699 fibril_rwlock_write_unlock(&icmp_globals.lock); 700 return EOK; 701 } 702 672 703 /** Processes the generic client messages. 673 704 * … … 696 727 case NET_ICMP_DEST_UNREACH: 697 728 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 698 IPC_GET_PACKET( call));729 IPC_GET_PACKET(*call)); 699 730 if (rc != EOK) 700 731 return rc; 701 732 return icmp_destination_unreachable_msg_local(0, 702 ICMP_GET_CODE( call), ICMP_GET_MTU(call), packet);733 ICMP_GET_CODE(*call), ICMP_GET_MTU(*call), packet); 703 734 case NET_ICMP_SOURCE_QUENCH: 704 735 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 705 IPC_GET_PACKET( call));736 IPC_GET_PACKET(*call)); 706 737 if (rc != EOK) 707 738 return rc; … … 709 740 case NET_ICMP_TIME_EXCEEDED: 710 741 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 711 IPC_GET_PACKET( call));742 IPC_GET_PACKET(*call)); 712 743 if (rc != EOK) 713 744 return rc; 714 return icmp_time_exceeded_msg_local(0, ICMP_GET_CODE( call),745 return icmp_time_exceeded_msg_local(0, ICMP_GET_CODE(*call), 715 746 packet); 716 747 case NET_ICMP_PARAMETERPROB: 717 748 rc = packet_translate_remote(icmp_globals.net_phone, &packet, 718 IPC_GET_PACKET( call));749 IPC_GET_PACKET(*call)); 719 750 if (rc != EOK) 720 751 return rc; 721 return icmp_parameter_problem_msg_local(0, ICMP_GET_CODE( call),722 ICMP_GET_POINTER( call), packet);752 return icmp_parameter_problem_msg_local(0, ICMP_GET_CODE(*call), 753 ICMP_GET_POINTER(*call), packet); 723 754 default: 724 755 return ENOTSUP; … … 787 818 bool keep_on_going = true; 788 819 ipc_call_t answer; 789 int answer_count;820 size_t answer_count; 790 821 size_t length; 791 822 struct sockaddr *addr; … … 893 924 * @see IS_NET_ICMP_MESSAGE() 894 925 */ 895 int icmp_message_standalone(ipc_callid_t callid, ipc_call_t *call, 896 ipc_call_t *answer, int *answer_count) 897 { 898 packet_t *packet; 899 int rc; 900 926 int tl_module_message (ipc_callid_t callid, ipc_call_t *call, 927 ipc_call_t *answer, size_t *answer_count) 928 { 901 929 *answer_count = 0; 902 930 switch (IPC_GET_IMETHOD(*call)) { 903 case NET_TL_RECEIVED:904 rc = packet_translate_remote(icmp_globals.net_phone, &packet,905 IPC_GET_PACKET(call));906 if (rc != EOK)907 return rc;908 return icmp_received_msg_local(IPC_GET_DEVICE(call), packet,909 SERVICE_ICMP, IPC_GET_ERROR(call));910 911 931 case NET_ICMP_INIT: 912 return icmp_process_client_messages(callid, * call); 913 932 return icmp_process_client_messages(callid, *call); 914 933 default: 915 934 return icmp_process_message(call); 916 935 } 917 936 918 937 return ENOTSUP; 919 938 } 920 939 921 922 /** Default thread for new connections.923 *924 * @param[in] iid The initial message identifier.925 * @param[in] icall The initial message call structure.926 *927 */928 static void tl_client_connection(ipc_callid_t iid, ipc_call_t *icall)929 {930 /*931 * Accept the connection932 * - Answer the first IPC_M_CONNECT_ME_TO call.933 */934 ipc_answer_0(iid, EOK);935 936 while (true) {937 ipc_call_t answer;938 int answer_count;939 940 /* Clear the answer structure */941 refresh_answer(&answer, &answer_count);942 943 /* Fetch the next message */944 ipc_call_t call;945 ipc_callid_t callid = async_get_call(&call);946 947 /* Process the message */948 int res = tl_module_message_standalone(callid, &call, &answer,949 &answer_count);950 951 /*952 * End if told to either by the message or the processing953 * result.954 */955 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||956 (res == EHANGUP))957 return;958 959 /* Answer the message */960 answer_call(callid, res, &answer, answer_count);961 }962 }963 964 /** Starts the module.965 *966 * @return EOK on success.967 * @return Other error codes as defined for each specific module968 * start function.969 */970 940 int main(int argc, char *argv[]) 971 941 { 972 int rc;973 974 942 /* Start the module */ 975 rc = tl_module_start_standalone(tl_client_connection); 976 return rc; 943 return tl_module_start(SERVICE_ICMP); 977 944 } 978 945 979 946 /** @} 980 947 */ 981 -
uspace/srv/net/tl/tcp/Makefile
reaef141 r80cd7cd 34 34 35 35 SOURCES = \ 36 tcp.c \ 37 tcp_module.c 36 tcp.c 38 37 39 38 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/tl/tcp/tcp.c
reaef141 r80cd7cd 36 36 */ 37 37 38 #include "tcp.h"39 #include "tcp_header.h"40 #include "tcp_module.h"41 42 38 #include <assert.h> 43 39 #include <async.h> … … 72 68 #include <socket_core.h> 73 69 #include <tl_common.h> 74 #include <tl_local.h> 75 #include <tl_interface.h> 70 #include <tl_remote.h> 71 #include <tl_skel.h> 72 73 #include "tcp.h" 74 #include "tcp_header.h" 76 75 77 76 /** TCP module name. */ 78 #define NAME "TCP protocol"77 #define NAME "tcp" 79 78 80 79 /** The TCP window default value. */ … … 154 153 155 154 /** Port map key. */ 156 char*key;155 uint8_t *key; 157 156 158 157 /** Port map key length. */ … … 220 219 /** TCP global data. */ 221 220 tcp_globals_t tcp_globals; 222 223 /** Initializes the TCP module.224 *225 * @param[in] client_connection The client connection processing function. The226 * module skeleton propagates its own one.227 * @return EOK on success.228 * @return ENOMEM if there is not enough memory left.229 */230 int tcp_initialize(async_client_conn_t client_connection)231 {232 int rc;233 234 assert(client_connection);235 236 fibril_rwlock_initialize(&tcp_globals.lock);237 fibril_rwlock_write_lock(&tcp_globals.lock);238 239 tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,240 ICMP_CONNECT_TIMEOUT);241 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,242 SERVICE_TCP, client_connection);243 if (tcp_globals.ip_phone < 0) {244 fibril_rwlock_write_unlock(&tcp_globals.lock);245 return tcp_globals.ip_phone;246 }247 248 rc = socket_ports_initialize(&tcp_globals.sockets);249 if (rc != EOK)250 goto out;251 252 rc = packet_dimensions_initialize(&tcp_globals.dimensions);253 if (rc != EOK) {254 socket_ports_destroy(&tcp_globals.sockets);255 goto out;256 }257 258 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;259 260 out:261 fibril_rwlock_write_unlock(&tcp_globals.lock);262 return rc;263 }264 221 265 222 int tcp_received_msg(device_id_t device_id, packet_t *packet, … … 358 315 /* Find the destination socket */ 359 316 socket = socket_port_find(&tcp_globals.sockets, 360 ntohs(header->destination_port), ( const char*) src, addrlen);317 ntohs(header->destination_port), (uint8_t *) src, addrlen); 361 318 if (!socket) { 362 319 /* Find the listening destination socket */ 363 320 socket = socket_port_find(&tcp_globals.sockets, 364 ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,365 0);321 ntohs(header->destination_port), 322 (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0); 366 323 } 367 324 … … 998 955 /* Find the destination socket */ 999 956 listening_socket = socket_port_find(&tcp_globals.sockets, 1000 listening_port, SOCKET_MAP_KEY_LISTENING, 0);957 listening_port, (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0); 1001 958 if (!listening_socket || 1002 959 (listening_socket->socket_id != listening_socket_id)) { … … 1022 979 1023 980 rc = socket_port_add(&tcp_globals.sockets, listening_port, socket, 1024 ( const char*) socket_data->addr, socket_data->addrlen);981 (uint8_t *) socket_data->addr, socket_data->addrlen); 1025 982 assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, 1026 ( const char*) socket_data->addr, socket_data->addrlen));983 (uint8_t *) socket_data->addr, socket_data->addrlen)); 1027 984 1028 985 // rc = socket_bind_free_port(&tcp_globals.sockets, socket, … … 1260 1217 * @see IS_NET_TCP_MESSAGE() 1261 1218 */ 1262 int 1263 tcp_message_standalone(ipc_callid_t callid, ipc_call_t *call, 1264 ipc_call_t *answer, int *answer_count) 1265 { 1266 packet_t *packet; 1267 int rc; 1268 1219 int tl_module_message(ipc_callid_t callid, ipc_call_t *call, 1220 ipc_call_t *answer, size_t *answer_count) 1221 { 1269 1222 assert(call); 1270 1223 assert(answer); … … 1273 1226 *answer_count = 0; 1274 1227 switch (IPC_GET_IMETHOD(*call)) { 1275 case NET_TL_RECEIVED:1276 // fibril_rwlock_read_lock(&tcp_globals.lock);1277 rc = packet_translate_remote(tcp_globals.net_phone, &packet,1278 IPC_GET_PACKET(call));1279 if (rc != EOK) {1280 // fibril_rwlock_read_unlock(&tcp_globals.lock);1281 return rc;1282 }1283 rc = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP,1284 IPC_GET_ERROR(call));1285 // fibril_rwlock_read_unlock(&tcp_globals.lock);1286 return rc;1287 1228 case IPC_M_CONNECT_TO_ME: 1288 1229 return tcp_process_client_messages(callid, *call); … … 1323 1264 bool keep_on_going = true; 1324 1265 socket_cores_t local_sockets; 1325 int app_phone = IPC_GET_PHONE( &call);1266 int app_phone = IPC_GET_PHONE(call); 1326 1267 struct sockaddr *addr; 1327 1268 int socket_id; … … 1330 1271 fibril_rwlock_t lock; 1331 1272 ipc_call_t answer; 1332 int answer_count;1273 size_t answer_count; 1333 1274 tcp_socket_data_t *socket_data; 1334 1275 socket_core_t *socket; … … 2109 2050 2110 2051 /* Copy the key */ 2111 operation_timeout->key = (( char*) operation_timeout) +2052 operation_timeout->key = ((uint8_t *) operation_timeout) + 2112 2053 sizeof(*operation_timeout); 2113 2054 operation_timeout->key_length = socket->key_length; … … 2486 2427 } 2487 2428 2488 /** Default thread for new connections.2429 /** Process IPC messages from the IP module 2489 2430 * 2490 * @param[in] iid The initial message identifier.2491 * @param[in ] icall The initial message call structure.2431 * @param[in] iid Message identifier. 2432 * @param[in,out] icall Message parameters. 2492 2433 * 2493 2434 */ 2494 static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall) 2495 { 2496 /* 2497 * Accept the connection 2498 * - Answer the first IPC_M_CONNECT_ME_TO call. 2499 */ 2500 ipc_answer_0(iid, EOK); 2501 2435 static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall) 2436 { 2437 packet_t *packet; 2438 int rc; 2439 2502 2440 while (true) { 2503 ipc_call_t answer; 2504 int answer_count; 2505 2506 /* Clear the answer structure */ 2507 refresh_answer(&answer, &answer_count); 2508 2509 /* Fetch the next message */ 2510 ipc_call_t call; 2511 ipc_callid_t callid = async_get_call(&call); 2512 2513 /* Process the message */ 2514 int res = tl_module_message_standalone(callid, &call, &answer, 2515 &answer_count); 2516 2517 /* 2518 * End if told to either by the message or the processing 2519 * result. 2520 */ 2521 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) || 2522 (res == EHANGUP)) 2523 return; 2524 2525 /* 2526 * Answer the message 2527 */ 2528 answer_call(callid, res, &answer, answer_count); 2529 } 2530 } 2531 2532 /** Starts the module. 2441 switch (IPC_GET_IMETHOD(*icall)) { 2442 case NET_TL_RECEIVED: 2443 rc = packet_translate_remote(tcp_globals.net_phone, &packet, 2444 IPC_GET_PACKET(*icall)); 2445 if (rc == EOK) 2446 rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet, 2447 SERVICE_TCP, IPC_GET_ERROR(*icall)); 2448 2449 ipc_answer_0(iid, (sysarg_t) rc); 2450 break; 2451 default: 2452 ipc_answer_0(iid, (sysarg_t) ENOTSUP); 2453 } 2454 2455 iid = async_get_call(icall); 2456 } 2457 } 2458 2459 /** Initialize the TCP module. 2533 2460 * 2534 * @return EOK on success. 2535 * @return Other error codes as defined for each specific module 2536 * start function. 2461 * @param[in] net_phone Network module phone. 2462 * 2463 * @return EOK on success. 2464 * @return ENOMEM if there is not enough memory left. 2465 * 2537 2466 */ 2538 int 2539 main(int argc, char *argv[]) 2540 { 2541 int rc; 2542 2543 rc = tl_module_start_standalone(tl_client_connection); 2467 int tl_initialize(int net_phone) 2468 { 2469 fibril_rwlock_initialize(&tcp_globals.lock); 2470 fibril_rwlock_write_lock(&tcp_globals.lock); 2471 2472 tcp_globals.net_phone = net_phone; 2473 2474 tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, 2475 ICMP_CONNECT_TIMEOUT); 2476 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, 2477 SERVICE_TCP, tcp_receiver); 2478 if (tcp_globals.ip_phone < 0) { 2479 fibril_rwlock_write_unlock(&tcp_globals.lock); 2480 return tcp_globals.ip_phone; 2481 } 2482 2483 int rc = socket_ports_initialize(&tcp_globals.sockets); 2484 if (rc != EOK) 2485 goto out; 2486 2487 rc = packet_dimensions_initialize(&tcp_globals.dimensions); 2488 if (rc != EOK) { 2489 socket_ports_destroy(&tcp_globals.sockets); 2490 goto out; 2491 } 2492 2493 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; 2494 2495 out: 2496 fibril_rwlock_write_unlock(&tcp_globals.lock); 2544 2497 return rc; 2498 } 2499 2500 int main(int argc, char *argv[]) 2501 { 2502 return tl_module_start(SERVICE_TCP); 2545 2503 } 2546 2504 -
uspace/srv/net/tl/udp/Makefile
reaef141 r80cd7cd 34 34 35 35 SOURCES = \ 36 udp.c \ 37 udp_module.c 36 udp.c 38 37 39 38 include $(USPACE_PREFIX)/Makefile.common -
uspace/srv/net/tl/udp/udp.c
reaef141 r80cd7cd 35 35 * @see udp.h 36 36 */ 37 38 #include "udp.h"39 #include "udp_header.h"40 #include "udp_module.h"41 37 42 38 #include <async.h> … … 69 65 #include <socket_core.h> 70 66 #include <tl_common.h> 71 #include <tl_local.h> 72 #include <tl_interface.h> 67 #include <tl_remote.h> 68 #include <tl_skel.h> 69 70 #include "udp.h" 71 #include "udp_header.h" 73 72 74 73 /** UDP module name. */ 75 #define NAME "UDP protocol"74 #define NAME "udp" 76 75 77 76 /** Default UDP checksum computing. */ … … 92 91 /** UDP global data. */ 93 92 udp_globals_t udp_globals; 94 95 /** Initializes the UDP module.96 *97 * @param[in] client_connection The client connection processing function. The98 * module skeleton propagates its own one.99 * @return EOK on success.100 * @return ENOMEM if there is not enough memory left.101 */102 int udp_initialize(async_client_conn_t client_connection)103 {104 measured_string_t names[] = {105 {106 (char *) "UDP_CHECKSUM_COMPUTING",107 22108 },109 {110 (char *) "UDP_AUTOBINDING",111 15112 }113 };114 measured_string_t *configuration;115 size_t count = sizeof(names) / sizeof(measured_string_t);116 char *data;117 int rc;118 119 fibril_rwlock_initialize(&udp_globals.lock);120 fibril_rwlock_write_lock(&udp_globals.lock);121 122 udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,123 ICMP_CONNECT_TIMEOUT);124 125 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,126 SERVICE_UDP, client_connection);127 if (udp_globals.ip_phone < 0) {128 fibril_rwlock_write_unlock(&udp_globals.lock);129 return udp_globals.ip_phone;130 }131 132 /* Read default packet dimensions */133 rc = ip_packet_size_req(udp_globals.ip_phone, -1,134 &udp_globals.packet_dimension);135 if (rc != EOK) {136 fibril_rwlock_write_unlock(&udp_globals.lock);137 return rc;138 }139 140 rc = socket_ports_initialize(&udp_globals.sockets);141 if (rc != EOK) {142 fibril_rwlock_write_unlock(&udp_globals.lock);143 return rc;144 }145 146 rc = packet_dimensions_initialize(&udp_globals.dimensions);147 if (rc != EOK) {148 socket_ports_destroy(&udp_globals.sockets);149 fibril_rwlock_write_unlock(&udp_globals.lock);150 return rc;151 }152 153 udp_globals.packet_dimension.prefix += sizeof(udp_header_t);154 udp_globals.packet_dimension.content -= sizeof(udp_header_t);155 udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;156 157 udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;158 udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;159 160 /* Get configuration */161 configuration = &names[0];162 rc = net_get_conf_req(udp_globals.net_phone, &configuration, count,163 &data);164 if (rc != EOK) {165 socket_ports_destroy(&udp_globals.sockets);166 fibril_rwlock_write_unlock(&udp_globals.lock);167 return rc;168 }169 170 if (configuration) {171 if (configuration[0].value)172 udp_globals.checksum_computing =173 (configuration[0].value[0] == 'y');174 175 if (configuration[1].value)176 udp_globals.autobinding =177 (configuration[1].value[0] == 'y');178 179 net_free_settings(configuration, data);180 }181 182 fibril_rwlock_write_unlock(&udp_globals.lock);183 return EOK;184 }185 93 186 94 /** Releases the packet and returns the result. … … 283 191 /* Find the destination socket */ 284 192 socket = socket_port_find(&udp_globals.sockets, 285 ntohs(header->destination_port),SOCKET_MAP_KEY_LISTENING, 0);193 ntohs(header->destination_port), (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0); 286 194 if (!socket) { 287 195 if (tl_prepare_icmp_packet(udp_globals.net_phone, … … 426 334 } 427 335 336 /** Process IPC messages from the IP module 337 * 338 * @param[in] iid Message identifier. 339 * @param[in,out] icall Message parameters. 340 * 341 */ 342 static void udp_receiver(ipc_callid_t iid, ipc_call_t *icall) 343 { 344 packet_t *packet; 345 int rc; 346 347 while (true) { 348 switch (IPC_GET_IMETHOD(*icall)) { 349 case NET_TL_RECEIVED: 350 rc = packet_translate_remote(udp_globals.net_phone, &packet, 351 IPC_GET_PACKET(*icall)); 352 if (rc == EOK) 353 rc = udp_received_msg(IPC_GET_DEVICE(*icall), packet, 354 SERVICE_UDP, IPC_GET_ERROR(*icall)); 355 356 ipc_answer_0(iid, (sysarg_t) rc); 357 break; 358 default: 359 ipc_answer_0(iid, (sysarg_t) ENOTSUP); 360 } 361 362 iid = async_get_call(icall); 363 } 364 } 365 366 /** Initialize the UDP module. 367 * 368 * @param[in] net_phone Network module phone. 369 * 370 * @return EOK on success. 371 * @return ENOMEM if there is not enough memory left. 372 * 373 */ 374 int tl_initialize(int net_phone) 375 { 376 measured_string_t names[] = { 377 { 378 (uint8_t *) "UDP_CHECKSUM_COMPUTING", 379 22 380 }, 381 { 382 (uint8_t *) "UDP_AUTOBINDING", 383 15 384 } 385 }; 386 measured_string_t *configuration; 387 size_t count = sizeof(names) / sizeof(measured_string_t); 388 uint8_t *data; 389 390 fibril_rwlock_initialize(&udp_globals.lock); 391 fibril_rwlock_write_lock(&udp_globals.lock); 392 393 udp_globals.net_phone = net_phone; 394 395 udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, 396 ICMP_CONNECT_TIMEOUT); 397 398 udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, 399 SERVICE_UDP, udp_receiver); 400 if (udp_globals.ip_phone < 0) { 401 fibril_rwlock_write_unlock(&udp_globals.lock); 402 return udp_globals.ip_phone; 403 } 404 405 /* Read default packet dimensions */ 406 int rc = ip_packet_size_req(udp_globals.ip_phone, -1, 407 &udp_globals.packet_dimension); 408 if (rc != EOK) { 409 fibril_rwlock_write_unlock(&udp_globals.lock); 410 return rc; 411 } 412 413 rc = socket_ports_initialize(&udp_globals.sockets); 414 if (rc != EOK) { 415 fibril_rwlock_write_unlock(&udp_globals.lock); 416 return rc; 417 } 418 419 rc = packet_dimensions_initialize(&udp_globals.dimensions); 420 if (rc != EOK) { 421 socket_ports_destroy(&udp_globals.sockets); 422 fibril_rwlock_write_unlock(&udp_globals.lock); 423 return rc; 424 } 425 426 udp_globals.packet_dimension.prefix += sizeof(udp_header_t); 427 udp_globals.packet_dimension.content -= sizeof(udp_header_t); 428 udp_globals.last_used_port = UDP_FREE_PORTS_START - 1; 429 430 udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING; 431 udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING; 432 433 /* Get configuration */ 434 configuration = &names[0]; 435 rc = net_get_conf_req(udp_globals.net_phone, &configuration, count, 436 &data); 437 if (rc != EOK) { 438 socket_ports_destroy(&udp_globals.sockets); 439 fibril_rwlock_write_unlock(&udp_globals.lock); 440 return rc; 441 } 442 443 if (configuration) { 444 if (configuration[0].value) 445 udp_globals.checksum_computing = 446 (configuration[0].value[0] == 'y'); 447 448 if (configuration[1].value) 449 udp_globals.autobinding = 450 (configuration[1].value[0] == 'y'); 451 452 net_free_settings(configuration, data); 453 } 454 455 fibril_rwlock_write_unlock(&udp_globals.lock); 456 return EOK; 457 } 458 428 459 /** Sends data from the socket to the remote address. 429 460 * … … 707 738 bool keep_on_going = true; 708 739 socket_cores_t local_sockets; 709 int app_phone = IPC_GET_PHONE( &call);740 int app_phone = IPC_GET_PHONE(call); 710 741 struct sockaddr *addr; 711 742 int socket_id; … … 713 744 size_t size; 714 745 ipc_call_t answer; 715 int answer_count;746 size_t answer_count; 716 747 packet_dimension_t *packet_dimension; 717 748 … … 860 891 * @see IS_NET_UDP_MESSAGE() 861 892 */ 862 int udp_message_standalone(ipc_callid_t callid, ipc_call_t *call,863 ipc_call_t *answer, int *answer_count)893 int tl_module_message(ipc_callid_t callid, ipc_call_t *call, 894 ipc_call_t *answer, size_t *answer_count) 864 895 { 865 packet_t *packet;866 int rc;867 868 896 *answer_count = 0; 869 897 870 898 switch (IPC_GET_IMETHOD(*call)) { 871 case NET_TL_RECEIVED:872 rc = packet_translate_remote(udp_globals.net_phone, &packet,873 IPC_GET_PACKET(call));874 if (rc != EOK)875 return rc;876 return udp_received_msg(IPC_GET_DEVICE(call), packet,877 SERVICE_UDP, IPC_GET_ERROR(call));878 899 case IPC_M_CONNECT_TO_ME: 879 return udp_process_client_messages(callid, * call);900 return udp_process_client_messages(callid, *call); 880 901 } 881 902 … … 883 904 } 884 905 885 /** Default thread for new connections.886 *887 * @param[in] iid The initial message identifier.888 * @param[in] icall The initial message call structure.889 */890 static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall)891 {892 /*893 * Accept the connection894 * - Answer the first IPC_M_CONNECT_ME_TO call.895 */896 ipc_answer_0(iid, EOK);897 898 while (true) {899 ipc_call_t answer;900 int answer_count;901 902 /* Clear the answer structure */903 refresh_answer(&answer, &answer_count);904 905 /* Fetch the next message */906 ipc_call_t call;907 ipc_callid_t callid = async_get_call(&call);908 909 /* Process the message */910 int res = tl_module_message_standalone(callid, &call, &answer,911 &answer_count);912 913 /*914 * End if told to either by the message or the processing915 * result.916 */917 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||918 (res == EHANGUP))919 return;920 921 /* Answer the message */922 answer_call(callid, res, &answer, answer_count);923 }924 }925 926 /** Starts the module.927 *928 * @return EOK on success.929 * @return Other error codes as defined for each specific module930 * start function.931 */932 906 int main(int argc, char *argv[]) 933 907 { 934 int rc;935 936 908 /* Start the module */ 937 rc = tl_module_start_standalone(tl_client_connection); 938 return rc; 909 return tl_module_start(SERVICE_UDP); 939 910 } 940 911
Note:
See TracChangeset
for help on using the changeset viewer.
