-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathnet_if.c
5970 lines (4702 loc) · 125 KB
/
net_if.c
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
/*
* Copyright (c) 2016 Intel Corporation.
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(net_if, CONFIG_NET_IF_LOG_LEVEL);
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/linker/sections.h>
#include <zephyr/random/random.h>
#include <zephyr/internal/syscall_handler.h>
#include <stdlib.h>
#include <string.h>
#include <zephyr/net/igmp.h>
#include <zephyr/net/ipv4_autoconf.h>
#include <zephyr/net/net_core.h>
#include <zephyr/net/net_event.h>
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/net/ethernet.h>
#ifdef CONFIG_WIFI_NM
#include <zephyr/net/wifi_nm.h>
#endif
#include <zephyr/net/offloaded_netdev.h>
#include <zephyr/net/virtual.h>
#include <zephyr/net/socket.h>
#include <zephyr/sys/iterable_sections.h>
#include "net_private.h"
#include "ipv4.h"
#include "ipv6.h"
#include "net_stats.h"
#define REACHABLE_TIME (MSEC_PER_SEC * 30) /* in ms */
/*
* split the min/max random reachable factors into numerator/denominator
* so that integer-based math works better
*/
#define MIN_RANDOM_NUMER (1)
#define MIN_RANDOM_DENOM (2)
#define MAX_RANDOM_NUMER (3)
#define MAX_RANDOM_DENOM (2)
static K_MUTEX_DEFINE(lock);
/* net_if dedicated section limiters */
extern struct net_if _net_if_list_start[];
extern struct net_if _net_if_list_end[];
static struct net_if *default_iface;
#if defined(CONFIG_NET_NATIVE_IPV4) || defined(CONFIG_NET_NATIVE_IPV6)
static struct net_if_router routers[CONFIG_NET_MAX_ROUTERS];
static struct k_work_delayable router_timer;
static sys_slist_t active_router_timers;
#endif
#if defined(CONFIG_NET_NATIVE_IPV6)
/* Timer that triggers network address renewal */
static struct k_work_delayable address_lifetime_timer;
/* Track currently active address lifetime timers */
static sys_slist_t active_address_lifetime_timers;
/* Timer that triggers IPv6 prefix lifetime */
static struct k_work_delayable prefix_lifetime_timer;
/* Track currently active IPv6 prefix lifetime timers */
static sys_slist_t active_prefix_lifetime_timers;
#if defined(CONFIG_NET_IPV6_DAD)
/** Duplicate address detection (DAD) timer */
static struct k_work_delayable dad_timer;
static sys_slist_t active_dad_timers;
#endif
#if defined(CONFIG_NET_IPV6_ND)
static struct k_work_delayable rs_timer;
static sys_slist_t active_rs_timers;
#endif
static struct {
struct net_if_ipv6 ipv6;
struct net_if *iface;
} ipv6_addresses[CONFIG_NET_IF_MAX_IPV6_COUNT];
#endif /* CONFIG_NET_NATIVE_IPV6 */
#if defined(CONFIG_NET_NATIVE_IPV4)
static struct {
struct net_if_ipv4 ipv4;
struct net_if *iface;
} ipv4_addresses[CONFIG_NET_IF_MAX_IPV4_COUNT];
#endif /* CONFIG_NET_NATIVE_IPV4 */
/* We keep track of the link callbacks in this list.
*/
static sys_slist_t link_callbacks;
#if defined(CONFIG_NET_NATIVE_IPV4) || defined(CONFIG_NET_NATIVE_IPV6)
/* Multicast join/leave tracking.
*/
static sys_slist_t mcast_monitor_callbacks;
#endif
#if defined(CONFIG_NET_PKT_TIMESTAMP_THREAD)
#if !defined(CONFIG_NET_PKT_TIMESTAMP_STACK_SIZE)
#define CONFIG_NET_PKT_TIMESTAMP_STACK_SIZE 1024
#endif
K_KERNEL_STACK_DEFINE(tx_ts_stack, CONFIG_NET_PKT_TIMESTAMP_STACK_SIZE);
K_FIFO_DEFINE(tx_ts_queue);
static struct k_thread tx_thread_ts;
/* We keep track of the timestamp callbacks in this list.
*/
static sys_slist_t timestamp_callbacks;
#endif /* CONFIG_NET_PKT_TIMESTAMP_THREAD */
#if CONFIG_NET_IF_LOG_LEVEL >= LOG_LEVEL_DBG
#define debug_check_packet(pkt) \
do { \
NET_DBG("Processing (pkt %p, prio %d) network packet " \
"iface %d (%p)", \
pkt, net_pkt_priority(pkt), \
net_if_get_by_iface(net_pkt_iface(pkt)), \
net_pkt_iface(pkt)); \
\
NET_ASSERT(pkt->frags); \
} while (false)
#else
#define debug_check_packet(...)
#endif /* CONFIG_NET_IF_LOG_LEVEL >= LOG_LEVEL_DBG */
struct net_if *z_impl_net_if_get_by_index(int index)
{
if (index <= 0) {
return NULL;
}
if (&_net_if_list_start[index - 1] >= _net_if_list_end) {
NET_DBG("Index %d is too large", index);
return NULL;
}
return &_net_if_list_start[index - 1];
}
#ifdef CONFIG_USERSPACE
struct net_if *z_vrfy_net_if_get_by_index(int index)
{
struct net_if *iface;
iface = net_if_get_by_index(index);
if (!iface) {
return NULL;
}
if (!k_object_is_valid(iface, K_OBJ_NET_IF)) {
return NULL;
}
return iface;
}
#include <zephyr/syscalls/net_if_get_by_index_mrsh.c>
#endif
static inline void net_context_send_cb(struct net_context *context,
int status)
{
if (!context) {
return;
}
if (context->send_cb) {
context->send_cb(context, status, context->user_data);
}
if (IS_ENABLED(CONFIG_NET_UDP) &&
net_context_get_proto(context) == IPPROTO_UDP) {
net_stats_update_udp_sent(net_context_get_iface(context));
} else if (IS_ENABLED(CONFIG_NET_TCP) &&
net_context_get_proto(context) == IPPROTO_TCP) {
net_stats_update_tcp_seg_sent(net_context_get_iface(context));
}
}
static void update_txtime_stats_detail(struct net_pkt *pkt,
uint32_t start_time, uint32_t stop_time)
{
uint32_t val, prev = start_time;
int i;
for (i = 0; i < net_pkt_stats_tick_count(pkt); i++) {
if (!net_pkt_stats_tick(pkt)[i]) {
break;
}
val = net_pkt_stats_tick(pkt)[i] - prev;
prev = net_pkt_stats_tick(pkt)[i];
net_pkt_stats_tick(pkt)[i] = val;
}
}
static bool net_if_tx(struct net_if *iface, struct net_pkt *pkt)
{
struct net_linkaddr ll_dst = {
.addr = NULL
};
struct net_linkaddr_storage ll_dst_storage;
struct net_context *context;
uint32_t create_time;
int status;
/* We collect send statistics for each socket priority if enabled */
uint8_t pkt_priority;
if (!pkt) {
return false;
}
create_time = net_pkt_create_time(pkt);
debug_check_packet(pkt);
/* If there're any link callbacks, with such a callback receiving
* a destination address, copy that address out of packet, just in
* case packet is freed before callback is called.
*/
if (!sys_slist_is_empty(&link_callbacks)) {
if (net_linkaddr_set(&ll_dst_storage,
net_pkt_lladdr_dst(pkt)->addr,
net_pkt_lladdr_dst(pkt)->len) == 0) {
ll_dst.addr = ll_dst_storage.addr;
ll_dst.len = ll_dst_storage.len;
ll_dst.type = net_pkt_lladdr_dst(pkt)->type;
}
}
context = net_pkt_context(pkt);
if (net_if_flag_is_set(iface, NET_IF_LOWER_UP)) {
if (IS_ENABLED(CONFIG_NET_PKT_TXTIME_STATS)) {
pkt_priority = net_pkt_priority(pkt);
if (IS_ENABLED(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)) {
/* Make sure the statistics information is not
* lost by keeping the net_pkt over L2 send.
*/
net_pkt_ref(pkt);
}
}
net_if_tx_lock(iface);
status = net_if_l2(iface)->send(iface, pkt);
net_if_tx_unlock(iface);
if (IS_ENABLED(CONFIG_NET_PKT_TXTIME_STATS)) {
uint32_t end_tick = k_cycle_get_32();
net_pkt_set_tx_stats_tick(pkt, end_tick);
net_stats_update_tc_tx_time(iface,
pkt_priority,
create_time,
end_tick);
if (IS_ENABLED(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)) {
update_txtime_stats_detail(
pkt,
create_time,
end_tick);
net_stats_update_tc_tx_time_detail(
iface, pkt_priority,
net_pkt_stats_tick(pkt));
/* For TCP connections, we might keep the pkt
* longer so that we can resend it if needed.
* Because of that we need to clear the
* statistics here.
*/
net_pkt_stats_tick_reset(pkt);
net_pkt_unref(pkt);
}
}
} else {
/* Drop packet if interface is not up */
NET_WARN("iface %p is down", iface);
status = -ENETDOWN;
}
if (status < 0) {
net_pkt_unref(pkt);
} else {
net_stats_update_bytes_sent(iface, status);
}
if (context) {
NET_DBG("Calling context send cb %p status %d",
context, status);
net_context_send_cb(context, status);
}
if (ll_dst.addr) {
net_if_call_link_cb(iface, &ll_dst, status);
}
return true;
}
void net_process_tx_packet(struct net_pkt *pkt)
{
struct net_if *iface;
net_pkt_set_tx_stats_tick(pkt, k_cycle_get_32());
iface = net_pkt_iface(pkt);
net_if_tx(iface, pkt);
#if defined(CONFIG_NET_POWER_MANAGEMENT)
iface->tx_pending--;
#endif
}
void net_if_queue_tx(struct net_if *iface, struct net_pkt *pkt)
{
if (!net_pkt_filter_send_ok(pkt)) {
/* silently drop the packet */
net_pkt_unref(pkt);
return;
}
uint8_t prio = net_pkt_priority(pkt);
uint8_t tc = net_tx_priority2tc(prio);
net_stats_update_tc_sent_pkt(iface, tc);
net_stats_update_tc_sent_bytes(iface, tc, net_pkt_get_len(pkt));
net_stats_update_tc_sent_priority(iface, tc, prio);
/* For highest priority packet, skip the TX queue and push directly to
* the driver. Also if there are no TX queue/thread, push the packet
* directly to the driver.
*/
if ((IS_ENABLED(CONFIG_NET_TC_SKIP_FOR_HIGH_PRIO) &&
prio >= NET_PRIORITY_CA) || NET_TC_TX_COUNT == 0) {
net_pkt_set_tx_stats_tick(pkt, k_cycle_get_32());
net_if_tx(net_pkt_iface(pkt), pkt);
return;
}
#if NET_TC_TX_COUNT > 1
NET_DBG("TC %d with prio %d pkt %p", tc, prio, pkt);
#endif
#if defined(CONFIG_NET_POWER_MANAGEMENT)
iface->tx_pending++;
#endif
if (!net_tc_submit_to_tx_queue(tc, pkt)) {
#if defined(CONFIG_NET_POWER_MANAGEMENT)
iface->tx_pending--
#endif
;
}
}
void net_if_stats_reset(struct net_if *iface)
{
#if defined(CONFIG_NET_STATISTICS_PER_INTERFACE)
STRUCT_SECTION_FOREACH(net_if, tmp) {
if (iface == tmp) {
net_if_lock(iface);
memset(&iface->stats, 0, sizeof(iface->stats));
net_if_unlock(iface);
return;
}
}
#else
ARG_UNUSED(iface);
#endif
}
void net_if_stats_reset_all(void)
{
#if defined(CONFIG_NET_STATISTICS_PER_INTERFACE)
STRUCT_SECTION_FOREACH(net_if, iface) {
net_if_lock(iface);
memset(&iface->stats, 0, sizeof(iface->stats));
net_if_unlock(iface);
}
#endif
}
static inline void init_iface(struct net_if *iface)
{
const struct net_if_api *api = net_if_get_device(iface)->api;
if (!api || !api->init) {
NET_ERR("Iface %p driver API init NULL", iface);
return;
}
/* By default IPv4 and IPv6 are enabled for a given network interface.
* These can be turned off later if needed.
*/
#if defined(CONFIG_NET_NATIVE_IPV4)
net_if_flag_set(iface, NET_IF_IPV4);
#endif
#if defined(CONFIG_NET_NATIVE_IPV6)
net_if_flag_set(iface, NET_IF_IPV6);
#endif
net_virtual_init(iface);
NET_DBG("On iface %p", iface);
#ifdef CONFIG_USERSPACE
k_object_init(iface);
#endif
k_mutex_init(&iface->lock);
k_mutex_init(&iface->tx_lock);
api->init(iface);
net_ipv6_pe_init(iface);
}
enum net_verdict net_if_send_data(struct net_if *iface, struct net_pkt *pkt)
{
const struct net_l2 *l2;
struct net_context *context = net_pkt_context(pkt);
struct net_linkaddr *dst = net_pkt_lladdr_dst(pkt);
enum net_verdict verdict = NET_OK;
int status = -EIO;
if (!net_if_flag_is_set(iface, NET_IF_LOWER_UP) ||
net_if_flag_is_set(iface, NET_IF_SUSPENDED)) {
/* Drop packet if interface is not up */
NET_WARN("iface %p is down", iface);
verdict = NET_DROP;
status = -ENETDOWN;
goto done;
}
/* The check for CONFIG_NET_*_OFFLOAD here is an optimization;
* This is currently the only way for net_if_l2 to be NULL or missing send().
*/
if (IS_ENABLED(CONFIG_NET_OFFLOAD) || IS_ENABLED(CONFIG_NET_SOCKETS_OFFLOAD)) {
l2 = net_if_l2(iface);
if (l2 == NULL) {
/* Offloaded ifaces may choose not to use an L2 at all. */
NET_WARN("no l2 for iface %p, discard pkt", iface);
verdict = NET_DROP;
goto done;
} else if (l2->send == NULL) {
/* Or, their chosen L2 (for example, OFFLOADED_NETDEV_L2)
* might simply not implement send.
*/
NET_WARN("l2 for iface %p cannot send, discard pkt", iface);
verdict = NET_DROP;
goto done;
}
}
/* If the ll address is not set at all, then we must set
* it here.
* Workaround Linux bug, see:
* https://github.com/zephyrproject-rtos/zephyr/issues/3111
*/
if (!net_if_flag_is_set(iface, NET_IF_POINTOPOINT) &&
!net_pkt_lladdr_src(pkt)->addr) {
net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_if(pkt)->addr;
net_pkt_lladdr_src(pkt)->len = net_pkt_lladdr_if(pkt)->len;
}
#if defined(CONFIG_NET_LOOPBACK)
/* If the packet is destined back to us, then there is no need to do
* additional checks, so let the packet through.
*/
if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
goto done;
}
#endif
/* Bypass the IP stack with SOCK_RAW/IPPROTO_RAW sockets */
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
context && net_context_get_type(context) == SOCK_RAW &&
net_context_get_proto(context) == IPPROTO_RAW) {
goto done;
}
/* If the ll dst address is not set check if it is present in the nbr
* cache.
*/
if (IS_ENABLED(CONFIG_NET_IPV6) && net_pkt_family(pkt) == AF_INET6) {
verdict = net_ipv6_prepare_for_send(pkt);
}
#if defined(CONFIG_NET_IPV4_FRAGMENT)
if (net_pkt_family(pkt) == AF_INET) {
verdict = net_ipv4_prepare_for_send(pkt);
}
#endif
done:
/* NET_OK in which case packet has checked successfully. In this case
* the net_context callback is called after successful delivery in
* net_if_tx_thread().
*
* NET_DROP in which case we call net_context callback that will
* give the status to user application.
*
* NET_CONTINUE in which case the sending of the packet is delayed.
* This can happen for example if we need to do IPv6 ND to figure
* out link layer address.
*/
if (verdict == NET_DROP) {
if (context) {
NET_DBG("Calling ctx send cb %p verdict %d",
context, verdict);
net_context_send_cb(context, status);
}
if (dst->addr) {
net_if_call_link_cb(iface, dst, status);
}
} else if (verdict == NET_OK) {
/* Packet is ready to be sent by L2, let's queue */
net_if_queue_tx(iface, pkt);
}
return verdict;
}
int net_if_set_link_addr_locked(struct net_if *iface,
uint8_t *addr, uint8_t len,
enum net_link_type type)
{
int ret;
net_if_lock(iface);
ret = net_if_set_link_addr_unlocked(iface, addr, len, type);
net_if_unlock(iface);
return ret;
}
struct net_if *net_if_get_by_link_addr(struct net_linkaddr *ll_addr)
{
STRUCT_SECTION_FOREACH(net_if, iface) {
net_if_lock(iface);
if (!memcmp(net_if_get_link_addr(iface)->addr, ll_addr->addr,
ll_addr->len)) {
net_if_unlock(iface);
return iface;
}
net_if_unlock(iface);
}
return NULL;
}
struct net_if *net_if_lookup_by_dev(const struct device *dev)
{
STRUCT_SECTION_FOREACH(net_if, iface) {
if (net_if_get_device(iface) == dev) {
return iface;
}
}
return NULL;
}
void net_if_set_default(struct net_if *iface)
{
default_iface = iface;
}
struct net_if *net_if_get_default(void)
{
struct net_if *iface = NULL;
if (&_net_if_list_start[0] == &_net_if_list_end[0]) {
NET_WARN("No default interface found!");
return NULL;
}
if (default_iface != NULL) {
return default_iface;
}
#if defined(CONFIG_NET_DEFAULT_IF_ETHERNET)
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET));
#endif
#if defined(CONFIG_NET_DEFAULT_IF_IEEE802154)
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(IEEE802154));
#endif
#if defined(CONFIG_NET_DEFAULT_IF_DUMMY)
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
#endif
#if defined(CONFIG_NET_DEFAULT_IF_OFFLOAD)
iface = net_if_get_first_by_type(NULL);
#endif
#if defined(CONFIG_NET_DEFAULT_IF_CANBUS_RAW)
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(CANBUS_RAW));
#endif
#if defined(CONFIG_NET_DEFAULT_IF_PPP)
iface = net_if_get_first_by_type(&NET_L2_GET_NAME(PPP));
#endif
#if defined(CONFIG_NET_DEFAULT_IF_UP)
iface = net_if_get_first_up();
#endif
#if defined(CONFIG_NET_DEFAULT_IF_WIFI)
iface = net_if_get_first_wifi();
#endif
return iface ? iface : _net_if_list_start;
}
struct net_if *net_if_get_first_by_type(const struct net_l2 *l2)
{
STRUCT_SECTION_FOREACH(net_if, iface) {
if (IS_ENABLED(CONFIG_NET_OFFLOAD) &&
!l2 && net_if_offload(iface)) {
return iface;
}
if (net_if_l2(iface) == l2) {
return iface;
}
}
return NULL;
}
struct net_if *net_if_get_first_up(void)
{
STRUCT_SECTION_FOREACH(net_if, iface) {
if (net_if_flag_is_set(iface, NET_IF_UP)) {
return iface;
}
}
return NULL;
}
static enum net_l2_flags l2_flags_get(struct net_if *iface)
{
enum net_l2_flags flags = 0;
if (net_if_l2(iface) && net_if_l2(iface)->get_flags) {
flags = net_if_l2(iface)->get_flags(iface);
}
return flags;
}
#if defined(CONFIG_NET_NATIVE_IPV4) || defined(CONFIG_NET_NATIVE_IPV6)
/* Return how many bits are shared between two IP addresses */
static uint8_t get_ipaddr_diff(const uint8_t *src, const uint8_t *dst, int addr_len)
{
uint8_t j, k, xor;
uint8_t len = 0U;
for (j = 0U; j < addr_len; j++) {
if (src[j] == dst[j]) {
len += 8U;
} else {
xor = src[j] ^ dst[j];
for (k = 0U; k < 8; k++) {
if (!(xor & 0x80)) {
len++;
xor <<= 1;
} else {
break;
}
}
break;
}
}
return len;
}
static struct net_if_router *iface_router_lookup(struct net_if *iface,
uint8_t family, void *addr)
{
struct net_if_router *router = NULL;
int i;
k_mutex_lock(&lock, K_FOREVER);
for (i = 0; i < CONFIG_NET_MAX_ROUTERS; i++) {
if (!routers[i].is_used ||
routers[i].address.family != family ||
routers[i].iface != iface) {
continue;
}
if ((IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6 &&
net_ipv6_addr_cmp(net_if_router_ipv6(&routers[i]),
(struct in6_addr *)addr)) ||
(IS_ENABLED(CONFIG_NET_IPV4) && family == AF_INET &&
net_ipv4_addr_cmp(net_if_router_ipv4(&routers[i]),
(struct in_addr *)addr))) {
router = &routers[i];
goto out;
}
}
out:
k_mutex_unlock(&lock);
return router;
}
static void iface_router_notify_deletion(struct net_if_router *router,
const char *delete_reason)
{
if (IS_ENABLED(CONFIG_NET_IPV6) &&
router->address.family == AF_INET6) {
NET_DBG("IPv6 router %s %s",
net_sprint_ipv6_addr(net_if_router_ipv6(router)),
delete_reason);
net_mgmt_event_notify_with_info(NET_EVENT_IPV6_ROUTER_DEL,
router->iface,
&router->address.in6_addr,
sizeof(struct in6_addr));
} else if (IS_ENABLED(CONFIG_NET_IPV4) &&
router->address.family == AF_INET) {
NET_DBG("IPv4 router %s %s",
net_sprint_ipv4_addr(net_if_router_ipv4(router)),
delete_reason);
net_mgmt_event_notify_with_info(NET_EVENT_IPV4_ROUTER_DEL,
router->iface,
&router->address.in_addr,
sizeof(struct in6_addr));
}
}
static inline int32_t iface_router_ends(const struct net_if_router *router,
uint32_t now)
{
uint32_t ends = router->life_start;
ends += MSEC_PER_SEC * router->lifetime;
/* Signed number of ms until router lifetime ends */
return (int32_t)(ends - now);
}
static void iface_router_update_timer(uint32_t now)
{
struct net_if_router *router, *next;
uint32_t new_delay = UINT32_MAX;
k_mutex_lock(&lock, K_FOREVER);
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&active_router_timers,
router, next, node) {
int32_t ends = iface_router_ends(router, now);
if (ends <= 0) {
new_delay = 0;
break;
}
new_delay = MIN((uint32_t)ends, new_delay);
}
if (new_delay == UINT32_MAX) {
k_work_cancel_delayable(&router_timer);
} else {
k_work_reschedule(&router_timer, K_MSEC(new_delay));
}
k_mutex_unlock(&lock);
}
static void iface_router_expired(struct k_work *work)
{
uint32_t current_time = k_uptime_get_32();
struct net_if_router *router, *next;
sys_snode_t *prev_node = NULL;
ARG_UNUSED(work);
k_mutex_lock(&lock, K_FOREVER);
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&active_router_timers,
router, next, node) {
int32_t ends = iface_router_ends(router, current_time);
if (ends > 0) {
/* We have to loop on all active routers as their
* lifetime differ from each other.
*/
prev_node = &router->node;
continue;
}
iface_router_notify_deletion(router, "has expired");
sys_slist_remove(&active_router_timers,
prev_node, &router->node);
router->is_used = false;
}
iface_router_update_timer(current_time);
k_mutex_unlock(&lock);
}
static struct net_if_router *iface_router_add(struct net_if *iface,
uint8_t family, void *addr,
bool is_default,
uint16_t lifetime)
{
struct net_if_router *router = NULL;
int i;
k_mutex_lock(&lock, K_FOREVER);
for (i = 0; i < CONFIG_NET_MAX_ROUTERS; i++) {
if (routers[i].is_used) {
continue;
}
routers[i].is_used = true;
routers[i].iface = iface;
routers[i].address.family = family;
if (lifetime) {
routers[i].is_default = true;
routers[i].is_infinite = false;
routers[i].lifetime = lifetime;
routers[i].life_start = k_uptime_get_32();
sys_slist_append(&active_router_timers,
&routers[i].node);
iface_router_update_timer(routers[i].life_start);
} else {
routers[i].is_default = false;
routers[i].is_infinite = true;
routers[i].lifetime = 0;
}
if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6) {
memcpy(net_if_router_ipv6(&routers[i]), addr,
sizeof(struct in6_addr));
net_mgmt_event_notify_with_info(
NET_EVENT_IPV6_ROUTER_ADD, iface,
&routers[i].address.in6_addr,
sizeof(struct in6_addr));
NET_DBG("interface %p router %s lifetime %u default %d "
"added", iface,
net_sprint_ipv6_addr((struct in6_addr *)addr),
lifetime, routers[i].is_default);
} else if (IS_ENABLED(CONFIG_NET_IPV4) && family == AF_INET) {
memcpy(net_if_router_ipv4(&routers[i]), addr,
sizeof(struct in_addr));
routers[i].is_default = is_default;
net_mgmt_event_notify_with_info(
NET_EVENT_IPV4_ROUTER_ADD, iface,
&routers[i].address.in_addr,
sizeof(struct in_addr));
NET_DBG("interface %p router %s lifetime %u default %d "
"added", iface,
net_sprint_ipv4_addr((struct in_addr *)addr),
lifetime, is_default);
}
router = &routers[i];
goto out;
}
out:
k_mutex_unlock(&lock);
return router;
}
static bool iface_router_rm(struct net_if_router *router)
{
bool ret = false;
k_mutex_lock(&lock, K_FOREVER);
if (!router->is_used) {
goto out;
}
iface_router_notify_deletion(router, "has been removed");
/* We recompute the timer if only the router was time limited */
if (sys_slist_find_and_remove(&active_router_timers, &router->node)) {
iface_router_update_timer(k_uptime_get_32());
}
router->is_used = false;
ret = true;
out:
k_mutex_unlock(&lock);
return ret;
}
void net_if_router_rm(struct net_if_router *router)
{
k_mutex_lock(&lock, K_FOREVER);
router->is_used = false;
/* FIXME - remove timer */
k_mutex_unlock(&lock);
}
static struct net_if_router *iface_router_find_default(struct net_if *iface,
uint8_t family, void *addr)
{
struct net_if_router *router = NULL;
int i;
/* Todo: addr will need to be handled */
ARG_UNUSED(addr);
k_mutex_lock(&lock, K_FOREVER);
for (i = 0; i < CONFIG_NET_MAX_ROUTERS; i++) {
if (!routers[i].is_used ||
!routers[i].is_default ||
routers[i].address.family != family) {
continue;
}
if (iface && iface != routers[i].iface) {
continue;
}
router = &routers[i];
goto out;
}
out:
k_mutex_unlock(&lock);
return router;
}
static void iface_router_init(void)
{
k_work_init_delayable(&router_timer, iface_router_expired);
sys_slist_init(&active_router_timers);
}
#else
#define iface_router_init(...)
#endif /* CONFIG_NET_NATIVE_IPV4 || CONFIG_NET_NATIVE_IPV6 */
#if defined(CONFIG_NET_NATIVE_IPV4) || defined(CONFIG_NET_NATIVE_IPV6)
void net_if_mcast_mon_register(struct net_if_mcast_monitor *mon,
struct net_if *iface,
net_if_mcast_callback_t cb)
{
k_mutex_lock(&lock, K_FOREVER);
sys_slist_find_and_remove(&mcast_monitor_callbacks, &mon->node);
sys_slist_prepend(&mcast_monitor_callbacks, &mon->node);
mon->iface = iface;
mon->cb = cb;
k_mutex_unlock(&lock);
}
void net_if_mcast_mon_unregister(struct net_if_mcast_monitor *mon)
{
k_mutex_lock(&lock, K_FOREVER);