Linux Audio

Check our new training course

Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
/** @file
 * @brief Network initialization
 *
 * Initialize the network IP stack. Create two fibers, one for reading data
 * from applications (Tx fiber) and one for reading data from IP stack
 * and passing that data to applications (Rx fiber).
 */

/*
 * Copyright (c) 2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef CONFIG_NETWORKING_WITH_LOGGING
#define DEBUG 1
#endif
#include "contiki/ip/uip-debug.h"

#include <nanokernel.h>
#include <toolchain.h>
#include <sections.h>
#include <string.h>
#include <errno.h>

#include <net/ip_buf.h>
#include <net/l2_buf.h>

#include <net/net_core.h>
#include <net/net_ip.h>
#include <net/net_socket.h>

#include "net_driver_15_4.h"
#include "net_driver_slip.h"
#include "net_driver_ethernet.h"
#include "net_driver_bt.h"

#include "contiki/os/sys/process.h"
#include "contiki/os/sys/etimer.h"
#include "contiki/os/sys/ctimer.h"
#include "contiki/netstack.h"
#include "contiki/ipv6/uip-ds6.h"
#include "contiki/ip/simple-udp.h"
#include "contiki/os/dev/slip.h"

#ifdef CONFIG_15_4_BEACON_SUPPORT
#include "contiki/mac/handler-802154.h"
#endif

/* Declare some private functions only to be used in this file so the
 * prototypes are not found in .h file.
 */
struct nano_fifo *net_context_get_queue(struct net_context *context);
struct simple_udp_connection *
	net_context_get_udp_connection(struct net_context *context);
int net_context_get_receiver_registered(struct net_context *context);
void net_context_set_receiver_registered(struct net_context *context);
int net_context_tcp_init(struct net_context *context,
			 enum net_tcp_type);
int net_context_tcp_send(struct net_buf *buf);

/* Stacks for the tx & rx fibers.
 * FIXME: stack size needs fine-tuning
 */
#define STACKSIZE_UNIT 1024
#ifndef CONFIG_IP_RX_STACK_SIZE
#define CONFIG_IP_RX_STACK_SIZE (STACKSIZE_UNIT * 1)
#endif
#ifndef CONFIG_IP_TX_STACK_SIZE
#define CONFIG_IP_TX_STACK_SIZE (STACKSIZE_UNIT * 1)
#endif
#ifndef CONFIG_IP_TIMER_STACK_SIZE
#define CONFIG_IP_TIMER_STACK_SIZE (STACKSIZE_UNIT * 3 / 2)
#endif
static char __noinit __stack rx_fiber_stack[CONFIG_IP_RX_STACK_SIZE];
static char __noinit __stack tx_fiber_stack[CONFIG_IP_TX_STACK_SIZE];
static char __noinit __stack timer_fiber_stack[CONFIG_IP_TIMER_STACK_SIZE];
static nano_thread_id_t timer_fiber_id, tx_fiber_id;

static struct net_dev {
	/* Queue for incoming packets from driver */
	struct nano_fifo rx_queue;

	/* Queue for outgoing packets from apps */
	struct nano_fifo tx_queue;

	/* Registered network driver */
	struct net_driver *drv;
} netdev;

/* Called by application to send a packet */
int net_send(struct net_buf *buf)
{
	if (ip_buf_len(buf) == 0) {
		return -ENODATA;
	}

#ifdef CONFIG_NETWORKING_WITH_TCP
	net_context_tcp_init(ip_buf_context(buf), NET_TCP_TYPE_CLIENT);
	if (ip_buf_context(buf)) {
		if (net_context_get_tuple(ip_buf_context(buf))->ip_proto ==
							    IPPROTO_TCP) {
			if (uip_conn(buf) && uip_conn(buf)->len > 0) {
				/* There is already pending packet to be sent.
				 * Application needs to try to send data
				 * a bit later.
				 */
				return -EAGAIN;
			}
		}
	}
#endif

	nano_fifo_put(&netdev.tx_queue, buf);

	/* Tell the IP stack it can proceed with the packet */
	fiber_wakeup(tx_fiber_id);

	return 0;
}

#ifdef CONFIG_NETWORKING_STATISTICS
#define STAT(s) uip_stat.s
#define PRINT_STATISTICS_INTERVAL (10 * sys_clock_ticks_per_sec)
#define net_print_statistics stats /* to make the debug print line shorter */

#if NET_MAC_CONF_STATS
#include "mac/mac.h"
#endif

#if RPL_CONF_STATS
#include "rpl/rpl-private.h"
#endif

#if NET_COAP_CONF_STATS
#include "er-coap/er-coap.h"
#endif

#if HANDLER_802154_CONF_STATS
#include "mac/handler-802154.h"
#endif

static void stats(void)
{
	static clock_time_t last_print;

	/* See contiki/ip/uip.h for descriptions of the different values */
	if (clock_time() > (last_print + PRINT_STATISTICS_INTERVAL)) {
#if NET_MAC_CONF_STATS
#define MAC_STAT(s) (net_mac_stats.s)
		NET_DBG("L2 bytes recv  %d\tsent\t%d\n",
			MAC_STAT(bytes_received),
			MAC_STAT(bytes_sent));
#endif
		NET_DBG("IP recv        %d\tsent\t%d\tdrop\t%d\tforwarded\t%d\n",
			STAT(ip.recv),
			STAT(ip.sent),
			STAT(ip.drop),
			STAT(ip.forwarded));
		NET_DBG("IP vhlerr      %d\thblener\t%d\tlblener\t%d\n",
			STAT(ip.vhlerr),
			STAT(ip.hblenerr),
			STAT(ip.lblenerr));
		NET_DBG("IP fragerr     %d\tchkerr\t%d\tprotoer\t%d\n",
			STAT(ip.fragerr),
			STAT(ip.chkerr),
			STAT(ip.protoerr));

#ifdef CONFIG_NETWORKING_WITH_TCP
		NET_DBG("TCP recv       %d\tsent\t%d\tdrop\t%d\n",
			STAT(tcp.recv),
			STAT(tcp.sent),
			STAT(tcp.drop));
		NET_DBG("TCP chkerr     %d\tackerr\t%d\trst\t%d\n",
			STAT(tcp.chkerr),
			STAT(tcp.ackerr),
			STAT(tcp.rst));
		NET_DBG("TCP rexmit     %d\tsyndrop\t%d\tsynrst\t%d\n",
			STAT(tcp.rexmit),
			STAT(tcp.syndrop),
			STAT(tcp.synrst));
#endif

		NET_DBG("ICMP recv      %d\tsent\t%d\tdrop\t%d\n",
			STAT(icmp.recv),
			STAT(icmp.sent),
			STAT(icmp.drop));
		NET_DBG("ICMP typeer    %d\tchkerr\t%d\n",
			STAT(icmp.typeerr),
			STAT(icmp.chkerr));

		NET_DBG("UDP recv       %d\tsent\t%d\tdrop\t%d\n",
			STAT(udp.recv),
			STAT(udp.sent),
			STAT(udp.drop));
		NET_DBG("UDP chkerr     %d\n",
			STAT(icmp.chkerr));

#if NET_COAP_CONF_STATS
		NET_DBG("CoAP recv      %d\terr\t%d\tsent\t%d\tre-sent\t%d\n",
			NET_COAP_STAT(recv),
			NET_COAP_STAT(recv_err),
			NET_COAP_STAT(sent),
			NET_COAP_STAT(re_sent));
#endif

#if NETSTACK_CONF_WITH_IPV6
		NET_DBG("ND recv        %d\tsent\t%d\tdrop\t%d\n",
			STAT(nd6.recv),
			STAT(nd6.sent),
			STAT(nd6.drop));
#endif

#if RPL_CONF_STATS
#define RSTAT(s) RPL_STAT(rpl_stats.s)
		NET_DBG("RPL overflows  %d\tl-repairs\t%d\tg-repairs\t%d\n",
			RSTAT(mem_overflows),
			RSTAT(local_repairs),
			RSTAT(global_repairs));
		NET_DBG("RPL malformed  %d\tresets   \t%d\tp-switch\t%d\n",
			RSTAT(malformed_msgs),
			RSTAT(resets),
			RSTAT(parent_switch));
		NET_DBG("RPL f-errors   %d\tl-errors\t%d\tl-warnings\t%d\n",
			RSTAT(forward_errors),
			RSTAT(loop_errors),
			RSTAT(loop_warnings));
		NET_DBG("RPL r-repairs  %d\n",
			RSTAT(root_repairs));
#endif

#if HANDLER_802154_CONF_STATS
#define IEEE802154_STAT(s) (handler_802154_stats.s)
		NET_DBG("802.15.4 beacons recv\t%d\tsent\t%d\treqs sent\t%d\n",
			IEEE802154_STAT(beacons_received),
			IEEE802154_STAT(beacons_sent),
			IEEE802154_STAT(beacons_reqs_sent));
#endif
		last_print = clock_time();
	}
}
#else
#define net_print_statistics()
#endif

/* Switch the ports and addresses and set route and neighbor cache.
 * Returns 1 if packet was sent properly, in this case it is the caller
 * that needs to release the net_buf. If 0 is returned, then uIP stack
 * has released the net_buf already because there was an some net related
 * error when sending the buffer.
 */
static inline int udp_prepare_and_send(struct net_context *context,
				       struct net_buf *buf)
{
#ifdef CONFIG_NETWORKING_IPV6_NO_ND
	uip_ds6_route_t *route_old, *route_new = NULL;
	uip_ds6_nbr_t *nbr;
#endif
	uip_ipaddr_t tmp;
	uint16_t port;
	uint8_t ret;

	if (uip_len(buf) == 0) {
		/* This is expected as uIP will typically set the
		 * packet length to 0 after receiving it. So we need
		 * to fix the length here. The protocol specific
		 * part is added also here.
		 */
		uip_len(buf) = ip_buf_len(buf);
	}

	ip_buf_appdata(buf) = &uip_buf(buf)[UIP_IPUDPH_LEN + UIP_LLH_LEN];

	port = NET_BUF_UDP(buf)->srcport;
	NET_BUF_UDP(buf)->srcport = NET_BUF_UDP(buf)->destport;
	NET_BUF_UDP(buf)->destport = port;

	uip_ipaddr_copy(&tmp, &NET_BUF_IP(buf)->srcipaddr);
	uip_ipaddr_copy(&NET_BUF_IP(buf)->srcipaddr,
			&NET_BUF_IP(buf)->destipaddr);
	uip_ipaddr_copy(&NET_BUF_IP(buf)->destipaddr, &tmp);

#ifdef CONFIG_NETWORKING_IPV6_NO_ND
	/* The peer needs to be in neighbor cache before route can be added.
	 */
	nbr = uip_ds6_nbr_lookup((uip_ipaddr_t *)&NET_BUF_IP(buf)->destipaddr);
	if (!nbr) {
		const uip_lladdr_t *lladdr =
			(const uip_lladdr_t *)&ip_buf_ll_src(buf);
		nbr = uip_ds6_nbr_add(
			(uip_ipaddr_t *)&NET_BUF_IP(buf)->destipaddr,
			lladdr, 0, NBR_REACHABLE);
		if (!nbr) {
			NET_DBG("Cannot add peer ");
			PRINT6ADDR(&NET_BUF_IP(buf)->destipaddr);
			PRINT(" to neighbor cache\n");
		}
	}

	/* Temporarily add route to peer, delete the route after
	 * sending the packet. Check if there was already a
	 * route and do not remove it if there was existing
	 * route to this peer.
	 */
	route_old = uip_ds6_route_lookup(&NET_BUF_IP(buf)->destipaddr);
	if (!route_old) {
		route_new = uip_ds6_route_add(&NET_BUF_IP(buf)->destipaddr,
					      128,
					      &NET_BUF_IP(buf)->destipaddr);
		if (!route_new) {
			NET_DBG("Cannot add route to peer ");
			PRINT6ADDR(&NET_BUF_IP(buf)->destipaddr);
			PRINT("\n");
		}
	}
#endif

	ret = simple_udp_sendto_port(buf,
				     net_context_get_udp_connection(context),
				     ip_buf_appdata(buf),
				     ip_buf_appdatalen(buf),
				     &NET_BUF_IP(buf)->destipaddr,
				     uip_ntohs(NET_BUF_UDP(buf)->destport));
	if (!ret) {
		NET_DBG("Packet could not be sent properly.\n");
	}

#ifdef CONFIG_NETWORKING_IPV6_NO_ND
	if (!route_old && route_new) {
		/* This will also remove the neighbor cache entry */
		uip_ds6_route_rm(route_new);
	}
#endif

	return !ret;
}

#ifdef CONFIG_NETWORKING_WITH_TCP
/* Switch the ports and addresses. Returns 1 if packet was sent properly,
 * in this case it is the caller that needs to release the net_buf.
 * If 0 is returned, then uIP stack has released the net_buf already
 * because there was an some net related error when sending the buffer.
 */
static inline int tcp_prepare_and_send(struct net_context *context,
				       struct net_buf *buf)
{
#ifdef CONFIG_NETWORKING_IPV6_NO_ND
	uip_ds6_route_t *route_old, *route_new = NULL;
	uip_ds6_nbr_t *nbr;
#endif
	uip_ipaddr_t tmp;
	uint16_t port;
	int ret;

	uip_len(buf) = uip_slen(buf) = ip_buf_len(buf);
	uip_flags(buf) |= UIP_NEWDATA;
	port = NET_BUF_UDP(buf)->srcport;
	NET_BUF_UDP(buf)->srcport = NET_BUF_UDP(buf)->destport;
	NET_BUF_UDP(buf)->destport = port;

	uip_ipaddr_copy(&tmp, &NET_BUF_IP(buf)->srcipaddr);
	uip_ipaddr_copy(&NET_BUF_IP(buf)->srcipaddr,
			&NET_BUF_IP(buf)->destipaddr);
	uip_ipaddr_copy(&NET_BUF_IP(buf)->destipaddr, &tmp);

#ifdef CONFIG_NETWORKING_IPV6_NO_ND
	/* The peer needs to be in neighbor cache before route can be added.
	 */
	nbr = uip_ds6_nbr_lookup((uip_ipaddr_t *)&NET_BUF_IP(buf)->destipaddr);
	if (!nbr) {
		const uip_lladdr_t *lladdr =
			(const uip_lladdr_t *)&ip_buf_ll_src(buf);
		nbr = uip_ds6_nbr_add(
			(uip_ipaddr_t *)&NET_BUF_IP(buf)->destipaddr,
			lladdr, 0, NBR_REACHABLE);
		if (!nbr) {
			NET_DBG("Cannot add peer ");
			PRINT6ADDR(&NET_BUF_IP(buf)->destipaddr);
			PRINT(" to neighbor cache\n");
		}
	}

	/* Temporarily add route to peer, delete the route after
	 * sending the packet. Check if there was already a
	 * route and do not remove it if there was existing
	 * route to this peer.
	 */
	route_old = uip_ds6_route_lookup(&NET_BUF_IP(buf)->destipaddr);
	if (!route_old) {
		route_new = uip_ds6_route_add(&NET_BUF_IP(buf)->destipaddr,
					      128,
					      &NET_BUF_IP(buf)->destipaddr);
		if (!route_new) {
			NET_DBG("Cannot add route to peer ");
			PRINT6ADDR(&NET_BUF_IP(buf)->destipaddr);
			PRINT("\n");
		}
	}
#endif

	NET_DBG("Packet output len %d\n", uip_len(buf));

	ret = net_context_tcp_send(buf);
	if (ret && ret != -EAGAIN) {
		NET_DBG("Packet could not be sent properly.\n");
	}

#ifdef CONFIG_NETWORKING_IPV6_NO_ND
	if (!route_old && route_new) {
		/* This will also remove the neighbor cache entry */
		uip_ds6_route_rm(route_new);
	}
#endif

	return ret;
}
#endif

/* Application wants to send a reply */
int net_reply(struct net_context *context, struct net_buf *buf)
{
	struct net_tuple *tuple;
	struct uip_udp_conn *udp;
	int ret = 0;

	if (!context || !buf) {
		return -EINVAL;
	}

	tuple = net_context_get_tuple(context);
	if (!tuple) {
		return -ENOENT;
	}

	switch (tuple->ip_proto) {
	case IPPROTO_UDP:
		udp = uip_udp_conn(buf);
		if (!udp) {
			NET_ERR("UDP connection missing\n");
			return -ESRCH;
		}

		ret = udp_prepare_and_send(context, buf);
		break;
	case IPPROTO_TCP:
#ifdef CONFIG_NETWORKING_WITH_TCP
		ret = tcp_prepare_and_send(context, buf);
#else
		NET_DBG("TCP not supported\n");
		return -EINVAL;
#endif
		break;
	case IPPROTO_ICMPV6:
		NET_DBG("ICMPv6 not yet supported\n");
		return -EINVAL;
	}

	return ret;
}

/* Called by driver when an IP packet has been received */
int net_recv(struct net_buf *buf)
{
	if (ip_buf_len(buf) == 0) {
		return -ENODATA;
	}

	nano_fifo_put(&netdev.rx_queue, buf);

	return 0;
}

static void udp_packet_receive(struct simple_udp_connection *c,
			       const uip_ipaddr_t *source_addr,
			       uint16_t source_port,
			       const uip_ipaddr_t *dest_addr,
			       uint16_t dest_port,
			       const uint8_t *data, uint16_t datalen,
			       void *user_data,
			       struct net_buf *buf)
{
	struct net_context *context = user_data;

	if (!context) {
		/* If the context is not there, then we must discard
		 * the buffer here, otherwise we have a buffer leak.
		 */
		ip_buf_unref(buf);
		return;
	}

	ip_buf_appdatalen(buf) = datalen;
	ip_buf_appdata(buf) = &uip_buf(buf)[UIP_IPUDPH_LEN + UIP_LLH_LEN];

	NET_DBG("packet received context %p len %d "
		"appdata %p appdatalen %d\n",
		context, ip_buf_len(buf),
		ip_buf_appdata(buf), ip_buf_appdatalen(buf));

	nano_fifo_put(net_context_get_queue(context), buf);
}

#ifdef CONFIG_NANO_TIMEOUTS
static inline struct net_buf *buf_wait_timeout(struct nano_fifo *queue,
					       int32_t timeout)
{
	switch (sys_execution_context_type_get()) {
	case NANO_CTX_FIBER:
		return nano_fiber_fifo_get(queue, timeout);
	case NANO_CTX_TASK:
		return nano_task_fifo_get(queue, timeout);
	case NANO_CTX_ISR:
	default:
		/* Invalid context type */
		break;
	}

	return NULL;
}
#endif

/* Called by application when it wants to receive network data */
struct net_buf *net_receive(struct net_context *context, int32_t timeout)
{
	struct nano_fifo *rx_queue = net_context_get_queue(context);
	struct net_buf *buf;
	struct net_tuple *tuple;
	int ret = 0;
	uint16_t reserve = 0;

	tuple = net_context_get_tuple(context);
	if (!tuple) {
		return NULL;
	}

	switch (tuple->ip_proto) {
	case IPPROTO_UDP:
		if (!net_context_get_receiver_registered(context)) {
			struct simple_udp_connection *udp =
				net_context_get_udp_connection(context);

			ret = simple_udp_register(udp, tuple->local_port,
#ifdef CONFIG_NETWORKING_WITH_IPV6
				(uip_ip6addr_t *)&tuple->remote_addr->in6_addr,
#else
				(uip_ip4addr_t *)&tuple->remote_addr->in_addr,
#endif
				tuple->remote_port,
				udp_packet_receive,
				context);
			if (!ret) {
				NET_DBG("UDP connection listener failed\n");
				ret = -ENOENT;
				break;
			}
		}
		net_context_set_receiver_registered(context);
		ret = 0;
		reserve = UIP_IPUDPH_LEN + UIP_LLH_LEN;
		break;
	case IPPROTO_TCP:
#ifdef CONFIG_NETWORKING_WITH_TCP
		ret = net_context_tcp_init(context, NET_TCP_TYPE_SERVER);
		if (ret) {
			NET_DBG("TCP connection init failed\n");
			ret = -ENOENT;
			break;
		}
		ret = 0;
#else
		NET_DBG("TCP not supported\n");
		ret = -EINVAL;
#endif
		break;
	case IPPROTO_ICMPV6:
		NET_DBG("ICMPv6 not yet supported\n");
		ret = -EINVAL;
		break;
	default:
		NET_ERR("Invalid IP protocol. "
			"Internal data structure corrupted!\n");
		ret = -EINVAL;
		break;
	}

	if (ret) {
		return NULL;
	}

	switch (timeout) {
	case TICKS_UNLIMITED:
		buf = nano_fifo_get(rx_queue, TICKS_UNLIMITED);
		break;
	case TICKS_NONE:
		buf = nano_fifo_get(rx_queue, TICKS_NONE);
		break;
	default:
#ifdef CONFIG_NANO_TIMEOUTS
		buf = buf_wait_timeout(rx_queue, timeout);
#else /* CONFIG_NANO_TIMEOUTS */
		buf = nano_fifo_get(rx_queue, TICKS_NONE);
#endif
		break;
	}

#ifdef CONFIG_NETWORKING_WITH_TCP
	if (tuple->ip_proto == IPPROTO_TCP &&
	    (ip_buf_appdata(buf) > (void *)buf->data)) {
		/* We need to skip the TCP header + possible extensions */
		reserve = ip_buf_appdata(buf) - (void *)buf->data;
	}
#endif

	if (buf && reserve) {
		ip_buf_appdatalen(buf) = ip_buf_len(buf) - reserve;
		ip_buf_appdata(buf) = &uip_buf(buf)[reserve];
	}

	return buf;
}

static void udp_packet_reply(struct simple_udp_connection *c,
			     const uip_ipaddr_t *source_addr,
			     uint16_t source_port,
			     const uip_ipaddr_t *dest_addr,
			     uint16_t dest_port,
			     const uint8_t *data, uint16_t datalen,
			     void *user_data,
			     struct net_buf *buf)
{
	struct net_context *context = user_data;
	struct nano_fifo *queue;

	if (!context) {
		/* If the context is not there, then we must discard
		 * the buffer here, otherwise we have a buffer leak.
		 */
		ip_buf_unref(buf);
		return;
	}

	queue = net_context_get_queue(context);

	/* Contiki stack will overwrite the uip_len(buf) and
	 * uip_appdatalen(buf) values, so in order to allow
	 * the application to use them, copy the values here.
	 */
	ip_buf_appdatalen(buf) = datalen;

	NET_DBG("packet reply context %p len %d "
		"appdata %p appdatalen %d queue %p\n",
		context, ip_buf_len(buf),
		ip_buf_appdata(buf), ip_buf_appdatalen(buf), queue);

	nano_fifo_put(queue, buf);
}

/* Internal function to send network data to uIP stack */
static int check_and_send_packet(struct net_buf *buf)
{
	struct net_tuple *tuple;
	struct simple_udp_connection *udp;
	int ret = 0;

	if (!netdev.drv) {
		return -EINVAL;
	}

	tuple = net_context_get_tuple(ip_buf_context(buf));
	if (!tuple) {
		return -EINVAL;
	}

	switch (tuple->ip_proto) {
	case IPPROTO_UDP:
		udp = net_context_get_udp_connection(ip_buf_context(buf));
		if (!net_context_get_receiver_registered(ip_buf_context(buf))) {
			ret = simple_udp_register(udp, tuple->local_port,
#ifdef CONFIG_NETWORKING_WITH_IPV6
				(uip_ip6addr_t *)&tuple->remote_addr->in6_addr,
#else
				(uip_ip4addr_t *)&tuple->remote_addr->in_addr,
#endif
				tuple->remote_port, udp_packet_reply,
				ip_buf_context(buf));
			if (!ret) {
				NET_DBG("UDP connection creation failed\n");
				ret = -ENOENT;
				break;
			}
			net_context_set_receiver_registered(ip_buf_context(buf));
		}

		if (ip_buf_appdatalen(buf) == 0) {
			/* User application has not set the application data
			 * length. The buffer will be discarded if we do not
			 * set the value correctly.
			 */
			uip_appdatalen(buf) = buf->len -
					      (UIP_IPUDPH_LEN + UIP_LLH_LEN);
		}

		ret = simple_udp_send(buf, udp, uip_appdata(buf),
				      uip_appdatalen(buf));
		break;
	case IPPROTO_TCP:
#ifdef CONFIG_NETWORKING_WITH_TCP
		if (ip_buf_appdatalen(buf) == 0) {
			/* User application has not set the application data
			 * length. The buffer will be discarded if we do not
			 * set the value correctly.
			 */
			uip_appdatalen(buf) = buf->len -
					      (UIP_IPTCPH_LEN + UIP_LLH_LEN);
		}
		if (uip_len(buf) == 0) {
			uip_len(buf) = buf->len;
		}
		ret = net_context_tcp_send(buf);
		if (ret && ret != -EAGAIN) {
			NET_DBG("Packet could not be sent properly.\n");
		}
#else
		NET_DBG("TCP not supported\n");
		ret = -EINVAL;
#endif
		break;
	case IPPROTO_ICMPV6:
		NET_DBG("ICMPv6 not yet supported\n");
		ret = -EINVAL;
		break;
	}

	return ret;
}

static void net_tx_fiber(void)
{
	NET_DBG("Starting TX fiber (stack %d bytes)\n",
		sizeof(tx_fiber_stack));

	while (1) {
		struct net_buf *buf;
		int ret;

		/* Get next packet from application - wait if necessary */
		buf = nano_fifo_get(&netdev.tx_queue, TICKS_UNLIMITED);

		NET_DBG("Sending (buf %p, len %u) to IP stack\n",
			buf, buf->len);

		/* What to do with the buffer:
		 *  <0: error, release the buffer
		 *   0: message was discarded by uIP, release the buffer here
		 *  >0: message was sent ok, buffer released already
		 */
		ret = check_and_send_packet(buf);
		if (ret < 0) {
			ip_buf_unref(buf);
			goto wait_next;
		} else if (ret > 0) {
			goto wait_next;
		}

		NET_BUF_CHECK_IF_NOT_IN_USE(buf);

		/* Check for any events that we might need to process */
		do {
			ret = process_run(buf);
		} while (ret > 0);

		ip_buf_unref(buf);

	wait_next:
		/* Check stack usage (no-op if not enabled) */
		net_analyze_stack("TX fiber", tx_fiber_stack,
				  sizeof(tx_fiber_stack));

		net_print_statistics();
	}
}

static void net_rx_fiber(void)
{
	struct net_buf *buf;

	NET_DBG("Starting RX fiber (stack %d bytes)\n",
		sizeof(rx_fiber_stack));

	while (1) {
		buf = nano_fifo_get(&netdev.rx_queue, TICKS_UNLIMITED);

		/* Check stack usage (no-op if not enabled) */
		net_analyze_stack("RX fiber", rx_fiber_stack,
				  sizeof(rx_fiber_stack));

		NET_DBG("Received buf %p\n", buf);

		if (!tcpip_input(buf)) {
			ip_buf_unref(buf);
		}
		/* The buffer is on to its way to receiver at this
		 * point. We must not remove it here.
		 */

		net_print_statistics();
	}
}

/*
 * Run various Contiki timers.
 */
#define MAX_TIMER_WAKEUP 0x7ffffff

static void net_timer_fiber(void)
{
	clock_time_t next_wakeup;

	NET_DBG("Starting net timer fiber (stack %d bytes)\n",
		sizeof(timer_fiber_stack));

	while (1) {
		/* Run various timers */
		next_wakeup = etimer_request_poll();

		if (next_wakeup == 0) {
			/* There was no timers, wait again */
			next_wakeup = MAX_TIMER_WAKEUP;
		} else {
			if (next_wakeup > MAX_TIMER_WAKEUP) {
				next_wakeup = MAX_TIMER_WAKEUP;
			}

#ifdef CONFIG_INIT_STACKS
			{
#define PRINT_CYCLE (60 * sys_clock_ticks_per_sec)

				static uint32_t next_print;
				uint32_t curr = sys_tick_get_32();

				/* Print stack usage every n. sec */
				if (!next_print ||
				    (next_print < curr &&
				     (!((curr - next_print) > PRINT_CYCLE)))) {
					uint32_t new_print;

					net_analyze_stack("timer fiber",
							  timer_fiber_stack,
						  sizeof(timer_fiber_stack));
					new_print = curr + PRINT_CYCLE;
					if (new_print > curr) {
						next_print = new_print;
					} else {
						/* Overflow */
						next_print = PRINT_CYCLE -
							(0xffffffff - curr);
					}
				}
			}
#endif
		}

		fiber_sleep(next_wakeup);
	}
}

static void init_rx_queue(void)
{
	nano_fifo_init(&netdev.rx_queue);

	fiber_start(rx_fiber_stack, sizeof(rx_fiber_stack),
		    (nano_fiber_entry_t)net_rx_fiber, 0, 0, 7, 0);
}

static void init_tx_queue(void)
{
	nano_fifo_init(&netdev.tx_queue);

	tx_fiber_id = fiber_start(tx_fiber_stack, sizeof(tx_fiber_stack),
				  (nano_fiber_entry_t)net_tx_fiber,
				  0, 0, 7, 0);
}

static void init_timer_fiber(void)
{
	timer_fiber_id = fiber_start(timer_fiber_stack,
				     sizeof(timer_fiber_stack),
				     (nano_fiber_entry_t)net_timer_fiber,
				     0, 0, 7, 0);
}

void net_timer_check(void)
{
	fiber_wakeup(timer_fiber_id);
}

int net_set_mac(uint8_t *mac, uint8_t len)
{
	if ((len > UIP_LLADDR_LEN) || (len != 6 && len != 8)) {
		NET_ERR("Wrong ll addr len, len %d, max %d\n",
			len, UIP_LLADDR_LEN);
		return -EINVAL;
	}

	linkaddr_set_node_addr((linkaddr_t *)mac);
	NET_DBG("MAC "); PRINTLLADDR((uip_lladdr_t *)&linkaddr_node_addr); PRINTF("\n");

#ifdef CONFIG_NETWORKING_WITH_IPV6
	{
		uip_ds6_addr_t *lladdr;

		uip_ds6_set_lladdr((uip_lladdr_t *)mac);

		lladdr = uip_ds6_get_link_local(-1);

		NET_DBG("Tentative link-local IPv6 address ");
		PRINT6ADDR(&lladdr->ipaddr);
		PRINTF("\n");

		lladdr->state = ADDR_AUTOCONF;
	}
#else
	memcpy(&uip_lladdr, mac, len);

	NET_DBG("IPv4 address ");
	PRINT6ADDR(&uip_hostaddr);
	PRINTF("\n");
#endif
	return 0;
}

static uint8_t net_tcpip_output(struct net_buf *buf, const uip_lladdr_t *lladdr)
{
	int res;

	if (!netdev.drv) {
		return 0;
	}

	if (lladdr) {
		linkaddr_copy(&ip_buf_ll_dest(buf),
			      (const linkaddr_t *)lladdr);
	} else {
		linkaddr_copy(&ip_buf_ll_dest(buf), &linkaddr_null);
	}

	if (ip_buf_len(buf) == 0) {
		return 0;
	}

	res = netdev.drv->send(buf);
	if (res < 0) {
		res = 0;
	}
	return (uint8_t)res;
}

static int network_initialization(void)
{
	/* Initialize and start Contiki uIP stack */
	clock_init();

	rtimer_init();
	ctimer_init();

	process_init();
	tcpip_set_outputfunc(net_tcpip_output);

	process_start(&tcpip_process, NULL, NULL);
	process_start(&simple_udp_process, NULL, NULL);
	process_start(&etimer_process, NULL, NULL);
	process_start(&ctimer_process, NULL, NULL);

	slip_start();

#if CONFIG_15_4_BEACON_SUPPORT && CONFIG_NETWORKING_WITH_15_4_PAN_ID
	handler_802154_join(CONFIG_NETWORKING_WITH_15_4_PAN_ID, 1);
#endif

#ifdef CONFIG_NETWORKING_WITH_15_4_PAN_ID
	NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID);
#endif

	return 0;
}

int net_register_driver(struct net_driver *drv)
{
	int r;

	if (netdev.drv) {
		return -EALREADY;
	}

	if (!drv->open || !drv->send) {
		return -EINVAL;
	}

	r = drv->open();
	if (r < 0) {
		return r;
	}

	netdev.drv = drv;

	return 0;
}

void net_unregister_driver(struct net_driver *drv)
{
	netdev.drv = NULL;
}

int net_init(void)
{
	static uint8_t initialized;

	if (initialized)
		return -EALREADY;

	initialized = 1;

#if UIP_STATISTICS == 1
	memset(&uip_stat, 0, sizeof(uip_stat));
#endif /* UIP_STATISTICS == 1 */

	net_context_init();

	ip_buf_init();
	l2_buf_init();

	init_tx_queue();
	init_rx_queue();
	init_timer_fiber();

#if defined(CONFIG_NETWORKING_WITH_15_4)
	net_driver_15_4_init();
#endif

#if defined(CONFIG_NETWORKING_WITH_BT)
	net_driver_bt_init();
#endif

	net_driver_slip_init();
	net_driver_ethernet_init();

	return network_initialization();
}