-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
net_context.c
3806 lines (3130 loc) · 87.2 KB
/
net_context.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
/** @file
* @brief Network context API
*
* An API for applications to define a network connection.
*/
/*
* Copyright (c) 2016 Intel Corporation
* Copyright (c) 2021 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(net_ctx, CONFIG_NET_CONTEXT_LOG_LEVEL);
#include <zephyr/kernel.h>
#include <zephyr/random/random.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/net_ip.h>
#include <zephyr/net/socket.h>
#include <zephyr/net/net_context.h>
#include <zephyr/net/net_offload.h>
#include <zephyr/net/ethernet.h>
#include <zephyr/net/socketcan.h>
#include <zephyr/net/ieee802154.h>
#include "connection.h"
#include "net_private.h"
#include "ipv6.h"
#include "ipv4.h"
#include "udp_internal.h"
#include "tcp_internal.h"
#include "net_stats.h"
#include "pmtu.h"
#if defined(CONFIG_NET_TCP)
#include "tcp.h"
#endif
#ifdef CONFIG_NET_INITIAL_MCAST_TTL
#define INITIAL_MCAST_TTL CONFIG_NET_INITIAL_MCAST_TTL
#else
#define INITIAL_MCAST_TTL 1
#endif
#ifdef CONFIG_NET_INITIAL_TTL
#define INITIAL_TTL CONFIG_NET_INITIAL_TTL
#else
#define INITIAL_TTL 1
#endif
#ifdef CONFIG_NET_INITIAL_MCAST_HOP_LIMIT
#define INITIAL_MCAST_HOP_LIMIT CONFIG_NET_INITIAL_MCAST_HOP_LIMIT
#else
#define INITIAL_MCAST_HOP_LIMIT 1
#endif
#ifdef CONFIG_NET_INITIAL_HOP_LIMIT
#define INITIAL_HOP_LIMIT CONFIG_NET_INITIAL_HOP_LIMIT
#else
#define INITIAL_HOP_LIMIT 1
#endif
#ifndef EPFNOSUPPORT
/* Some old versions of newlib haven't got this defined in errno.h,
* Just use EPROTONOSUPPORT in this case
*/
#define EPFNOSUPPORT EPROTONOSUPPORT
#endif
#define PKT_WAIT_TIME K_SECONDS(1)
#define NET_MAX_CONTEXT CONFIG_NET_MAX_CONTEXTS
static struct net_context contexts[NET_MAX_CONTEXT];
/* We need to lock the contexts array as these APIs are typically called
* from applications which are usually run in task context.
*/
static struct k_sem contexts_lock;
bool net_context_is_reuseaddr_set(struct net_context *context)
{
#if defined(CONFIG_NET_CONTEXT_REUSEADDR)
return context->options.reuseaddr;
#else
return false;
#endif
}
bool net_context_is_reuseport_set(struct net_context *context)
{
#if defined(CONFIG_NET_CONTEXT_REUSEPORT)
return context->options.reuseport;
#else
return false;
#endif
}
bool net_context_is_v6only_set(struct net_context *context)
{
#if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
if (context == NULL) {
return false;
}
return context->options.ipv6_v6only;
#else
ARG_UNUSED(context);
return true;
#endif
}
bool net_context_is_recv_pktinfo_set(struct net_context *context)
{
#if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
return context->options.recv_pktinfo;
#else
ARG_UNUSED(context);
return false;
#endif
}
bool net_context_is_timestamping_set(struct net_context *context)
{
#if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
return (bool)(context->options.timestamping > 0);
#else
ARG_UNUSED(context);
return false;
#endif
}
#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_TCP)
static inline bool is_in_tcp_listen_state(struct net_context *context)
{
#if defined(CONFIG_NET_TCP)
if (net_context_get_type(context) == SOCK_STREAM &&
net_context_get_state(context) == NET_CONTEXT_LISTENING) {
return true;
}
return false;
#else
return false;
#endif
}
static inline bool is_in_tcp_time_wait_state(struct net_context *context)
{
#if defined(CONFIG_NET_TCP)
if (net_context_get_type(context) == SOCK_STREAM) {
const struct tcp *tcp_conn = context->tcp;
if (net_tcp_get_state(tcp_conn) == TCP_TIME_WAIT) {
return true;
}
}
return false;
#else
return false;
#endif
}
static int check_used_port(struct net_context *context,
struct net_if *iface,
enum net_ip_protocol proto,
uint16_t local_port,
const struct sockaddr *local_addr,
bool reuseaddr_set,
bool reuseport_set,
bool check_port_range)
{
int i;
for (i = 0; i < NET_MAX_CONTEXT; i++) {
if (!net_context_is_used(&contexts[i])) {
continue;
}
if (context != NULL && context == &contexts[i]) {
continue;
}
if (!(net_context_get_proto(&contexts[i]) == proto &&
net_sin((struct sockaddr *)&
contexts[i].local)->sin_port == local_port)) {
continue;
}
if (net_context_is_bound_to_iface(&contexts[i])) {
if (iface != NULL && iface != net_context_get_iface(&contexts[i])) {
continue;
}
}
if (IS_ENABLED(CONFIG_NET_IPV6) &&
local_addr->sa_family == AF_INET6) {
if (net_sin6_ptr(&contexts[i].local)->sin6_addr == NULL ||
net_sin6_ptr(&contexts[i].local)->sin6_family != AF_INET6) {
continue;
}
if ((net_ipv6_is_addr_unspecified(
net_sin6_ptr(&contexts[i].local)->sin6_addr) ||
net_ipv6_is_addr_unspecified(
&net_sin6(local_addr)->sin6_addr))) {
if (reuseport_set &&
net_context_is_reuseport_set(&contexts[i])) {
/* When both context have the REUSEPORT set, both
* may be unspecified.
*/
continue;
} else if (reuseaddr_set &&
!is_in_tcp_listen_state(&contexts[i]) &&
!(net_ipv6_is_addr_unspecified(
net_sin6_ptr(&contexts[i].local)->sin6_addr) &&
net_ipv6_is_addr_unspecified(
&net_sin6(local_addr)->sin6_addr))) {
/* In case of REUSEADDR, only one context may be
* bound to the unspecified address, but not both.
* Furthermore, in case the existing context is in
* TCP LISTEN state, we ignore the REUSEADDR option
* (Linux behavior).
*/
continue;
} else {
return -EEXIST;
}
}
if (net_ipv6_addr_cmp(
net_sin6_ptr(&contexts[i].local)->
sin6_addr,
&((struct sockaddr_in6 *)
local_addr)->sin6_addr)) {
if (reuseport_set &&
net_context_is_reuseport_set(&contexts[i])) {
/* When both context have the REUSEPORT set, both
* may be bound to exactly the same address.
*/
continue;
} else if (reuseaddr_set &&
is_in_tcp_time_wait_state(&contexts[i])) {
/* With REUSEADDR, the existing context must be
* in the TCP TIME_WAIT state.
*/
continue;
} else {
return -EEXIST;
}
}
} else if (IS_ENABLED(CONFIG_NET_IPV4) &&
local_addr->sa_family == AF_INET) {
/* If there is an IPv6 socket already bound and
* if v6only option is enabled, then it is possible to
* bind IPv4 address to it.
*/
if (net_sin_ptr(&contexts[i].local)->sin_addr == NULL ||
((IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6) ?
net_context_is_v6only_set(&contexts[i]) : true) &&
net_sin_ptr(&contexts[i].local)->sin_family != AF_INET)) {
continue;
}
if ((net_ipv4_is_addr_unspecified(
net_sin_ptr(&contexts[i].local)->sin_addr) ||
net_ipv4_is_addr_unspecified(
&net_sin(local_addr)->sin_addr))) {
if (reuseport_set &&
net_context_is_reuseport_set(&contexts[i])) {
/* When both context have the REUSEPORT set, both
* may be unspecified.
*/
continue;
} else if (reuseaddr_set &&
!is_in_tcp_listen_state(&contexts[i]) &&
!(net_ipv4_is_addr_unspecified(
net_sin_ptr(&contexts[i].local)->sin_addr) &&
net_ipv4_is_addr_unspecified(
&net_sin(local_addr)->sin_addr))) {
/* In case of REUSEADDR, only one context may be
* bound to the unspecified address, but not both.
* Furthermore, in case the existing context is in
* TCP LISTEN state, we ignore the REUSEADDR option
* (Linux behavior).
*/
continue;
} else {
return -EEXIST;
}
}
if (net_ipv4_addr_cmp(
net_sin_ptr(&contexts[i].local)->
sin_addr,
&((struct sockaddr_in *)
local_addr)->sin_addr)) {
if (reuseport_set &&
net_context_is_reuseport_set(&contexts[i])) {
/* When both context have the REUSEPORT set, both
* may be bound to exactly the same address.
*/
continue;
} else if (reuseaddr_set &&
is_in_tcp_time_wait_state(&contexts[i])) {
/* With REUSEADDR, the existing context must be
* in the TCP TIME_WAIT state.
*/
continue;
} else {
return -EEXIST;
}
}
}
}
/* Make sure that if the port range is active, the port is
* within the range.
*/
if (IS_ENABLED(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE) && check_port_range) {
uint16_t upper, lower;
upper = COND_CODE_1(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE,
(context->options.port_range >> 16),
(0));
lower = COND_CODE_1(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE,
(context->options.port_range & 0xffff),
(0));
if (upper != 0 && lower != 0 && lower < upper) {
if (ntohs(local_port) < lower || ntohs(local_port) > upper) {
return -ERANGE;
}
}
}
return 0;
}
/* How many times we try to find a free port */
#define MAX_PORT_RETRIES 5
static uint16_t find_available_port(struct net_context *context,
const struct sockaddr *addr)
{
uint16_t local_port;
int count = MAX_PORT_RETRIES;
do {
if (IS_ENABLED(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)) {
uint16_t upper, lower;
upper = COND_CODE_1(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE,
(context->options.port_range >> 16),
(0));
lower = COND_CODE_1(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE,
(context->options.port_range & 0xffff),
(0));
/* This works the same way as in Linux. If either port
* range is 0, then we use random port. If both are set,
* then we use the range. Also make sure that upper is
* greater than lower.
*/
if (upper == 0 || lower == 0 || upper <= lower) {
local_port = sys_rand16_get() | 0x8000;
} else {
local_port = lower + sys_rand16_get() % (upper - lower);
NET_DBG("Port range %d - %d, proposing port %d",
lower, upper, local_port);
}
} else {
local_port = sys_rand16_get() | 0x8000;
}
count--;
} while (count > 0 && check_used_port(context,
NULL,
net_context_get_proto(context),
htons(local_port),
addr,
false,
false,
false) == -EEXIST);
if (count == 0) {
return 0;
}
return htons(local_port);
}
#else
#define check_used_port(...) 0
#define find_available_port(...) 0
#endif
bool net_context_port_in_use(enum net_ip_protocol proto,
uint16_t local_port,
const struct sockaddr *local_addr)
{
return check_used_port(NULL, NULL, proto, htons(local_port),
local_addr, false, false, false) != 0;
}
#if defined(CONFIG_NET_CONTEXT_CHECK)
static int net_context_check(sa_family_t family, enum net_sock_type type,
uint16_t proto, struct net_context **context)
{
switch (family) {
case AF_INET:
case AF_INET6:
if (family == AF_INET && !IS_ENABLED(CONFIG_NET_IPV4)) {
NET_DBG("IPv4 disabled");
return -EPFNOSUPPORT;
}
if (family == AF_INET6 && !IS_ENABLED(CONFIG_NET_IPV6)) {
NET_DBG("IPv6 disabled");
return -EPFNOSUPPORT;
}
if (!IS_ENABLED(CONFIG_NET_UDP)) {
if (type == SOCK_DGRAM) {
NET_DBG("DGRAM socket type disabled.");
return -EPROTOTYPE;
}
if (proto == IPPROTO_UDP) {
NET_DBG("UDP disabled");
return -EPROTONOSUPPORT;
}
}
if (!IS_ENABLED(CONFIG_NET_TCP)) {
if (type == SOCK_STREAM) {
NET_DBG("STREAM socket type disabled.");
return -EPROTOTYPE;
}
if (proto == IPPROTO_TCP) {
NET_DBG("TCP disabled");
return -EPROTONOSUPPORT;
}
}
switch (type) {
case SOCK_DGRAM:
if (proto != IPPROTO_UDP) {
NET_DBG("Context type and protocol mismatch,"
" type %d proto %d", type, proto);
return -EPROTONOSUPPORT;
}
break;
case SOCK_STREAM:
if (proto != IPPROTO_TCP) {
NET_DBG("Context type and protocol mismatch,"
" type %d proto %d", type, proto);
return -EPROTONOSUPPORT;
}
break;
case SOCK_RAW:
break;
default:
NET_DBG("Unknown context type.");
return -EPROTOTYPE;
}
break;
case AF_PACKET:
if (!IS_ENABLED(CONFIG_NET_SOCKETS_PACKET)) {
NET_DBG("AF_PACKET disabled");
return -EPFNOSUPPORT;
}
if (type != SOCK_RAW && type != SOCK_DGRAM) {
NET_DBG("AF_PACKET only supports RAW and DGRAM socket "
"types.");
return -EPROTOTYPE;
}
break;
case AF_CAN:
if (!IS_ENABLED(CONFIG_NET_SOCKETS_CAN)) {
NET_DBG("AF_CAN disabled");
return -EPFNOSUPPORT;
}
if (type != SOCK_RAW) {
NET_DBG("AF_CAN only supports RAW socket type.");
return -EPROTOTYPE;
}
if (proto != CAN_RAW) {
NET_DBG("AF_CAN only supports RAW_CAN protocol.");
return -EPROTOTYPE;
}
break;
default:
NET_DBG("Unknown address family %d", family);
return -EAFNOSUPPORT;
}
if (!context) {
NET_DBG("Invalid context");
return -EINVAL;
}
return 0;
}
#endif /* CONFIG_NET_CONTEXT_CHECK */
int net_context_get(sa_family_t family, enum net_sock_type type, uint16_t proto,
struct net_context **context)
{
int i, ret;
if (IS_ENABLED(CONFIG_NET_CONTEXT_CHECK)) {
ret = net_context_check(family, type, proto, context);
if (ret < 0) {
return ret;
}
}
k_sem_take(&contexts_lock, K_FOREVER);
ret = -ENOENT;
for (i = 0; i < NET_MAX_CONTEXT; i++) {
if (net_context_is_used(&contexts[i])) {
continue;
}
memset(&contexts[i], 0, sizeof(contexts[i]));
/* FIXME - Figure out a way to get the correct network interface
* as it is not known at this point yet.
*/
if (!net_if_is_ip_offloaded(net_if_get_default())
&& proto == IPPROTO_TCP) {
if (net_tcp_get(&contexts[i]) < 0) {
break;
}
}
contexts[i].iface = -1;
contexts[i].flags = 0U;
atomic_set(&contexts[i].refcount, 1);
net_context_set_family(&contexts[i], family);
net_context_set_type(&contexts[i], type);
net_context_set_proto(&contexts[i], proto);
#if defined(CONFIG_NET_IPV6)
contexts[i].options.addr_preferences = IPV6_PREFER_SRC_PUBTMP_DEFAULT;
#endif
#if defined(CONFIG_NET_CONTEXT_RCVTIMEO)
contexts[i].options.rcvtimeo = K_FOREVER;
#endif
#if defined(CONFIG_NET_CONTEXT_SNDTIMEO)
contexts[i].options.sndtimeo = K_FOREVER;
#endif
#if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
/* By default IPv4 and IPv6 are in different port spaces */
contexts[i].options.ipv6_v6only = true;
#endif
if (IS_ENABLED(CONFIG_NET_IP)) {
(void)memset(&contexts[i].remote, 0, sizeof(struct sockaddr));
(void)memset(&contexts[i].local, 0, sizeof(struct sockaddr_ptr));
if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6) {
struct sockaddr_in6 *addr6 =
(struct sockaddr_in6 *)&contexts[i].local;
addr6->sin6_port =
find_available_port(&contexts[i], (struct sockaddr *)addr6);
if (!addr6->sin6_port) {
ret = -EADDRINUSE;
break;
}
contexts[i].ipv6_hop_limit = INITIAL_HOP_LIMIT;
contexts[i].ipv6_mcast_hop_limit = INITIAL_MCAST_HOP_LIMIT;
}
if (IS_ENABLED(CONFIG_NET_IPV4) && family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)&contexts[i].local;
addr->sin_port =
find_available_port(&contexts[i], (struct sockaddr *)addr);
if (!addr->sin_port) {
ret = -EADDRINUSE;
break;
}
contexts[i].ipv4_ttl = INITIAL_TTL;
contexts[i].ipv4_mcast_ttl = INITIAL_MCAST_TTL;
}
}
if (IS_ENABLED(CONFIG_NET_CONTEXT_SYNC_RECV)) {
k_sem_init(&contexts[i].recv_data_wait, 1, K_SEM_MAX_LIMIT);
}
k_mutex_init(&contexts[i].lock);
contexts[i].flags |= NET_CONTEXT_IN_USE;
*context = &contexts[i];
ret = 0;
break;
}
k_sem_give(&contexts_lock);
if (ret < 0) {
return ret;
}
/* FIXME - Figure out a way to get the correct network interface
* as it is not known at this point yet.
*/
if (IS_ENABLED(CONFIG_NET_OFFLOAD) && net_if_is_ip_offloaded(net_if_get_default())) {
ret = net_offload_get(net_if_get_default(), family, type, proto, context);
if (ret < 0) {
(*context)->flags &= ~NET_CONTEXT_IN_USE;
*context = NULL;
return ret;
}
net_context_set_iface(*context, net_if_get_default());
}
return 0;
}
int net_context_ref(struct net_context *context)
{
int old_rc = atomic_inc(&context->refcount);
return old_rc + 1;
}
int net_context_unref(struct net_context *context)
{
int old_rc = atomic_dec(&context->refcount);
if (old_rc != 1) {
return old_rc - 1;
}
k_mutex_lock(&context->lock, K_FOREVER);
if (context->conn_handler) {
if (IS_ENABLED(CONFIG_NET_TCP) || IS_ENABLED(CONFIG_NET_UDP) ||
IS_ENABLED(CONFIG_NET_SOCKETS_CAN) ||
IS_ENABLED(CONFIG_NET_SOCKETS_PACKET)) {
net_conn_unregister(context->conn_handler);
}
context->conn_handler = NULL;
}
net_context_set_state(context, NET_CONTEXT_UNCONNECTED);
context->flags &= ~NET_CONTEXT_IN_USE;
NET_DBG("Context %p released", context);
k_mutex_unlock(&context->lock);
return 0;
}
int net_context_put(struct net_context *context)
{
int ret = 0;
NET_ASSERT(context);
if (!PART_OF_ARRAY(contexts, context)) {
return -EINVAL;
}
k_mutex_lock(&context->lock, K_FOREVER);
if (IS_ENABLED(CONFIG_NET_OFFLOAD) &&
net_if_is_ip_offloaded(net_context_get_iface(context))) {
context->flags &= ~NET_CONTEXT_IN_USE;
ret = net_offload_put(net_context_get_iface(context), context);
goto unlock;
}
context->connect_cb = NULL;
context->recv_cb = NULL;
context->send_cb = NULL;
/* net_tcp_put() will handle decrementing refcount on stack's behalf */
net_tcp_put(context);
/* Decrement refcount on user app's behalf */
net_context_unref(context);
unlock:
k_mutex_unlock(&context->lock);
return ret;
}
/* If local address is not bound, bind it to INADDR_ANY and random port. */
static int bind_default(struct net_context *context)
{
sa_family_t family = net_context_get_family(context);
if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6) {
struct sockaddr_in6 addr6;
if (net_sin6_ptr(&context->local)->sin6_addr) {
return 0;
}
addr6.sin6_family = AF_INET6;
memcpy(&addr6.sin6_addr, net_ipv6_unspecified_address(),
sizeof(addr6.sin6_addr));
addr6.sin6_port =
find_available_port(context,
(struct sockaddr *)&addr6);
return net_context_bind(context, (struct sockaddr *)&addr6,
sizeof(addr6));
}
if (IS_ENABLED(CONFIG_NET_IPV4) && family == AF_INET) {
struct sockaddr_in addr4;
if (net_sin_ptr(&context->local)->sin_addr) {
return 0;
}
addr4.sin_family = AF_INET;
addr4.sin_addr.s_addr = INADDR_ANY;
addr4.sin_port =
find_available_port(context,
(struct sockaddr *)&addr4);
return net_context_bind(context, (struct sockaddr *)&addr4,
sizeof(addr4));
}
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) && family == AF_PACKET) {
struct sockaddr_ll ll_addr;
if (net_sll_ptr(&context->local)->sll_addr) {
return 0;
}
ll_addr.sll_family = AF_PACKET;
ll_addr.sll_protocol = htons(ETH_P_ALL);
ll_addr.sll_ifindex = net_if_get_by_iface(net_if_get_default());
return net_context_bind(context, (struct sockaddr *)&ll_addr,
sizeof(ll_addr));
}
if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) && family == AF_CAN) {
struct sockaddr_can can_addr;
if (context->iface >= 0) {
return 0;
} else {
#if defined(CONFIG_NET_L2_CANBUS_RAW)
struct net_if *iface;
iface = net_if_get_first_by_type(
&NET_L2_GET_NAME(CANBUS_RAW));
if (!iface) {
return -ENOENT;
}
can_addr.can_ifindex = net_if_get_by_iface(iface);
context->iface = can_addr.can_ifindex;
#else
return -ENOENT;
#endif
}
can_addr.can_family = AF_CAN;
return net_context_bind(context, (struct sockaddr *)&can_addr,
sizeof(can_addr));
}
return -EINVAL;
}
static int recheck_port(struct net_context *context,
struct net_if *iface,
int proto,
uint16_t port,
const struct sockaddr *addr)
{
int ret;
ret = check_used_port(context, iface,
proto,
net_sin(addr)->sin_port,
addr,
net_context_is_reuseaddr_set(context),
net_context_is_reuseport_set(context),
true);
if (ret != 0) {
if (IS_ENABLED(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE) && ret == -ERANGE) {
uint16_t re_port;
NET_DBG("Port %d is out of range, re-selecting!",
ntohs(net_sin(addr)->sin_port));
re_port = find_available_port(context, addr);
if (re_port == 0U) {
NET_ERR("No available port found (iface %d)",
iface ? net_if_get_by_iface(iface) : 0);
return -EADDRINUSE;
}
net_sin_ptr(&context->local)->sin_port = re_port;
net_sin(addr)->sin_port = re_port;
} else {
NET_ERR("Port %d is in use!", ntohs(net_sin(addr)->sin_port));
NET_DBG("Interface %d (%p)",
iface ? net_if_get_by_iface(iface) : 0, iface);
return -EADDRINUSE;
}
} else {
net_sin_ptr(&context->local)->sin_port = net_sin(addr)->sin_port;
}
return 0;
}
int net_context_bind(struct net_context *context, const struct sockaddr *addr,
socklen_t addrlen)
{
int ret;
NET_ASSERT(addr);
NET_ASSERT(PART_OF_ARRAY(contexts, context));
/* If we already have connection handler, then it effectively
* means that it's already bound to an interface/port, and we
* don't support rebinding connection to new address/port in
* the code below.
* TODO: Support rebinding.
*/
if (context->conn_handler) {
return -EISCONN;
}
if (IS_ENABLED(CONFIG_NET_IPV6) && addr->sa_family == AF_INET6) {
struct net_if *iface = NULL;
struct in6_addr *ptr;
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
if (addrlen < sizeof(struct sockaddr_in6)) {
return -EINVAL;
}
if (net_context_is_bound_to_iface(context)) {
iface = net_context_get_iface(context);
}
if (net_ipv6_is_addr_mcast(&addr6->sin6_addr)) {
struct net_if_mcast_addr *maddr;
if (IS_ENABLED(CONFIG_NET_UDP) &&
net_context_get_type(context) == SOCK_DGRAM) {
if (COND_CODE_1(CONFIG_NET_IPV6,
(context->options.ipv6_mcast_ifindex > 0),
(false))) {
IF_ENABLED(CONFIG_NET_IPV6,
(iface = net_if_get_by_index(
context->options.ipv6_mcast_ifindex)));
}
}
maddr = net_if_ipv6_maddr_lookup(&addr6->sin6_addr,
&iface);
if (!maddr) {
return -ENOENT;
}
ptr = &maddr->address.in6_addr;
} else if (net_ipv6_is_addr_unspecified(&addr6->sin6_addr)) {
if (iface == NULL) {
iface = net_if_ipv6_select_src_iface(
&net_sin6(&context->remote)->sin6_addr);
}
ptr = (struct in6_addr *)net_ipv6_unspecified_address();
} else {
struct net_if_addr *ifaddr;
ifaddr = net_if_ipv6_addr_lookup(
&addr6->sin6_addr,
iface == NULL ? &iface : NULL);
if (!ifaddr) {
return -ENOENT;
}
ptr = &ifaddr->address.in6_addr;
}
if (!iface) {
NET_ERR("Cannot bind to %s",
net_sprint_ipv6_addr(&addr6->sin6_addr));
return -EADDRNOTAVAIL;
}
k_mutex_lock(&context->lock, K_FOREVER);
net_context_set_iface(context, iface);
net_sin6_ptr(&context->local)->sin6_family = AF_INET6;
net_sin6_ptr(&context->local)->sin6_addr = ptr;
if (IS_ENABLED(CONFIG_NET_OFFLOAD) && net_if_is_ip_offloaded(iface)) {
k_mutex_unlock(&context->lock);
return net_offload_bind(iface, context, addr, addrlen);
}
ret = 0;
if (addr6->sin6_port) {
ret = recheck_port(context, iface, context->proto,
addr6->sin6_port, addr);
if (ret != 0) {
goto unlock_ipv6;
}
} else {
addr6->sin6_port =
net_sin6_ptr(&context->local)->sin6_port;
if (IS_ENABLED(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)) {
ret = recheck_port(context, iface, context->proto,
addr6->sin6_port, addr);
if (ret != 0) {
goto unlock_ipv6;
}
}
}
NET_DBG("Context %p binding to %s [%s]:%d iface %d (%p)",
context,
net_proto2str(AF_INET6,
net_context_get_proto(context)),
net_sprint_ipv6_addr(ptr),
ntohs(addr6->sin6_port),
net_if_get_by_iface(iface), iface);
unlock_ipv6:
k_mutex_unlock(&context->lock);
return ret;
}
if (IS_ENABLED(CONFIG_NET_IPV4) && addr->sa_family == AF_INET) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
struct net_if *iface = NULL;
struct net_if_addr *ifaddr;
struct in_addr *ptr;
if (addrlen < sizeof(struct sockaddr_in)) {
return -EINVAL;
}
if (net_context_is_bound_to_iface(context)) {
iface = net_context_get_iface(context);
}
if (net_ipv4_is_addr_mcast(&addr4->sin_addr)) {
struct net_if_mcast_addr *maddr;
if (IS_ENABLED(CONFIG_NET_UDP) &&
net_context_get_type(context) == SOCK_DGRAM) {
if (COND_CODE_1(CONFIG_NET_IPV4,
(context->options.ipv4_mcast_ifindex > 0),
(false))) {
IF_ENABLED(CONFIG_NET_IPV4,
(iface = net_if_get_by_index(
context->options.ipv4_mcast_ifindex)));
}
}
maddr = net_if_ipv4_maddr_lookup(&addr4->sin_addr,
&iface);
if (!maddr) {
return -ENOENT;
}