Repository navigation
Expand file tree
/
Copy pathrtsock.c
More file actions
1912 lines (1557 loc) · 50.1 KB
/
Copy pathrtsock.c
File metadata and controls
1912 lines (1557 loc) · 50.1 KB
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
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
1
10BC8
000
/* LWIP service - rtsock.c - routing sockets and route sysctl support */
/*
* In a nutshell, the intended abstraction is that only this module deals with
* route messages, message headers, and RTA arrays, whereas other modules
* (ifaddr, route) are responsible for parsing and providing sockaddr_* type
* addresses, with the exception of compression and expansion which is
* particular to routing sockets. Concretely, there should be no reference to
* (e.g.) rt_msghdr outside this module, and no mention of ip_addr_t inside it.
*/
#include "lwip.h"
#include "ifaddr.h"
#include "rtsock.h"
#include "route.h"
#include "lldata.h"
/* The number of routing sockets. */
#define NR_RTSOCK 8
/*
* The send buffer maximum determines the maximum size of requests. The
* maximum possible request size is the size of the routing message header plus
* RTAX_MAX times the maximum socket address size, including alignment. That
* currently works out to a number in the low 400s, so 512 should be fine for
* now. At this time we do not support changing the send buffer size, because
* there really is no point in doing so. Hence also no RT_SNDBUF_{MIN,DEF}.
*/
#define RT_SNDBUF_MAX 512 /* maximum RT send buffer size */
#define RT_RCVBUF_MIN 0 /* minimum RT receive buffer size */
#define RT_RCVBUF_DEF 16384 /* default RT receive buffer size */
#define RT_RCVBUF_MAX 65536 /* maximum RT receive buffer size */
/* Address length of routing socket address structures; two bytes only. */
#define RTSOCK_ADDR_LEN offsetof(struct sockaddr, sa_data)
struct rtsock_rta {
const void *rta_ptr[RTAX_MAX];
socklen_t rta_len[RTAX_MAX];
};
static const char rtsock_padbuf[RT_ROUNDUP(0)];
static struct rtsock {
struct sock rt_sock; /* socket object, MUST be first */
int rt_family; /* address family filter if not zero */
unsigned int rt_flags; /* routing socket flags (RTF_) */
struct pbuf *rt_rcvhead; /* receive buffer, first packet */
struct pbuf **rt_rcvtailp; /* receive buffer, last ptr-ptr */
size_t rt_rcvlen; /* receive buffer, length in bytes */
size_t rt_rcvbuf; /* receive buffer, maximum size */
TAILQ_ENTRY(rtsock) rt_next; /* next in active or free list */
} rt_array[NR_RTSOCK];
#define RTF_NOLOOPBACK 0x1 /* suppress reply messages */
static TAILQ_HEAD(, rtsock) rt_freelist; /* free routing sockets */
static TAILQ_HEAD(, rtsock) rt_activelist; /* active routing sockets */
struct rtsock_request {
struct rtsock *rtr_src; /* source socket of the request */
pid_t rtr_pid; /* process ID of requesting process */
int rtr_seq; /* sequence number from the request */
int rtr_getif; /* RTM_GET only: get interface info */
};
static const struct sockevent_ops rtsock_ops;
static ssize_t rtsock_info(struct rmib_call *, struct rmib_node *,
struct rmib_oldp *, struct rmib_newp *);
/* The CTL_NET PF_ROUTE subtree. */
static struct rmib_node net_route_table[] = {
[0] = RMIB_FUNC(RMIB_RO | CTLTYPE_NODE, 0, rtsock_info,
"rtable", "Routing table information"),
};
/* The CTL_NET PF_ROUTE node. */
static struct rmib_node net_route_node =
RMIB_NODE(RMIB_RO, net_route_table, "route", "PF_ROUTE information");
/*
* Initialize the routing sockets module.
*/
void
rtsock_init(void)
{
const int mib[] = { CTL_NET, PF_ROUTE };
unsigned int slot;
int r;
/* Initialize the list of free routing sockets. */
TAILQ_INIT(&rt_freelist);
for (slot = 0; slot < __arraycount(rt_array); slot++)
TAILQ_INSERT_TAIL(&rt_freelist, &rt_array[slot], rt_next);
/* Initialize the list of acive routing sockets. */
TAILQ_INIT(&rt_activelist);
/* Register the "net.route" subtree with the MIB service. */
if ((r = rmib_register(mib, __arraycount(mib), &net_route_node)) != OK)
panic("unable to register net.route RMIB tree: %d", r);
}
/*
* Allocate a pbuf suitable for storing a routing message of 'size' bytes.
* Return the allocated pbuf on success, or NULL on memory allocation failure.
*/
static struct pbuf *
rtsock_alloc(size_t size)
{
struct pbuf *pbuf;
/*
* The data will currently always fit in a single pool buffer. Just in
* case this changes in the future, warn and fail cleanly. The rest of
* the code is not able to deal with buffer chains as it is, although
* that can be changed if necessary.
*/
if (size > MEMPOOL_BUFSIZE) {
printf("LWIP: routing socket packet too large (%zu)\n", size);
return NULL;
}
pbuf = pbuf_alloc(PBUF_RAW, size, PBUF_RAM);
assert(pbuf == NULL || pbuf->tot_len == pbuf->len);
return pbuf;
}
/*
* Initialize a routing addresses map.
*/
static void
rtsock_rta_init(struct rtsock_rta * rta)
{
memset(rta, 0, sizeof(*rta));
}
/*
* Set an entry in a routing addresses map. When computing sizes, 'ptr' may be
* NULL.
*/
static void
rtsock_rta_set(struct rtsock_rta * rta, unsigned int rtax, const void * ptr,
socklen_t len)
{
assert(rtax < RTAX_MAX);
rta->rta_ptr[rtax] = ptr;
rta->rta_len[rtax] = len;
}
/*
* Copy out a message with a header and any entries in a routing addresses map,
* either into a pbuf allocated for this purpose, or to a RMIB (sysctl) caller,
* at the given offset. If no destination is given ('pbuf ' and 'oldp' are
* both NULL), compute just the size of the resulting data. Otherwise, set the
* length and address mask fields in the header as a side effect. Return the
* number of bytes copied on success, and if 'pbuf' is not NULL, it is filled
* with a pointer to the newly allocated pbuf. Return a negative error code on
* failure. Note that when computing the size only, any actual data pointers
* ('hdr', 'msglen', 'addrs', and the pointers in 'rta') may be NULL or even
* invalid, even though the corresponding sizes should still be supplied.
*/
static ssize_t
rtsock_rta_finalize(void * hdr, size_t hdrlen, u_short * msglen, int * addrs,
const struct rtsock_rta * rta, struct pbuf ** pbuf,
struct rmib_oldp * oldp, ssize_t off)
{
iovec_t iov[1 + RTAX_MAX * 2];
size_t len, padlen, totallen;
unsigned int i, iovcnt;
int mask;
assert(pbuf == NULL || oldp == NULL);
assert(pbuf == NULL || off == 0);
assert(RT_ROUNDUP(hdrlen) == hdrlen);
iov[0].iov_addr = (vir_bytes)hdr;
iov[0].iov_size = hdrlen;
iovcnt = 1;
totallen = hdrlen;
mask = 0;
/*
* The addresses in the given RTA map, as present, should be stored in
* the numbering order of the map.
*/
for (i = 0; i < RTAX_MAX; i++) {
if (rta->rta_ptr[i] == NULL)
continue;
if ((len = rta->rta_len[i]) > 0) {
assert(iovcnt < __arraycount(iov));
iov[iovcnt].iov_addr = (vir_bytes)rta->rta_ptr[i];
iov[iovcnt++].iov_size = len;
}
/* Note that RT_ROUNDUP(0) is not 0.. */
if ((padlen = RT_ROUNDUP(len) - len) > 0) {
assert(iovcnt < __arraycount(iov));
iov[iovcnt].iov_addr = (vir_bytes)rtsock_padbuf;
iov[iovcnt++].iov_size = padlen;
}
totallen += len + padlen;
mask |= (1 << i); /* convert RTAX_ to RTA_ */
}
/* If only the length was requested, return it now. */
if (pbuf == NULL && oldp == NULL)
return totallen;
/*
* Casting 'hdr' would violate C99 strict aliasing rules, but the
* address mask is not always at the same location anyway.
*/
*msglen = totallen;
*addrs = mask;
if (pbuf != NULL) {
if ((*pbuf = rtsock_alloc(totallen)) == NULL)
return ENOMEM;
return util_coalesce((char *)(*pbuf)->payload, totallen, iov,
iovcnt);
} else
return rmib_vcopyout(oldp, off, iov, iovcnt);
}
/*
* Reduce the size of a network mask to the bytes actually used. It is highly
* doubtful that this extra complexity pays off in any form, but it is what the
* BSDs historically do. We currently implement compression for IPv4 only.
*/
static void
rtsock_compress_netmask(struct sockaddr * sa)
{
struct sockaddr_in sin;
uint32_t addr;
if (sa->sa_family != AF_INET)
return; /* nothing to do */
memcpy(&sin, sa, sizeof(sin)); /* no type punning.. (sigh) */
addr = htonl(sin.sin_addr.s_addr);
if (addr & 0x000000ff)
sa->sa_len = 8;
else if (addr & 0x0000ffff)
sa->sa_len = 7;
else if (addr & 0x00ffffff)
sa->sa_len = 6;
else if (addr != 0)
sa->sa_len = 5;
else
sa->sa_len = 0;
}
/*
* Expand a possibly compressed IPv4 or IPv6 network mask, given as 'sa', into
* 'mask'. Return TRUE if expansion succeeded. In that case, the resulting
* mask must have sa.sa_len and sa.sa_family filled in correctly, and have the
* appropriate size for its address family. Return FALSE if expansion failed
* and an error should be returned to the caller.
*/
static int
rtsock_expand_netmask(union sockaddr_any * mask, const struct sockaddr * sa)
{
if (sa->sa_len > sizeof(*mask))
return FALSE;
memset(mask, 0, sizeof(*mask));
memcpy(mask, sa, sa->sa_len);
/*
* Amazingly, even the address family may be chopped off, in which case
* an IPv4 address is implied.
*/
if (sa->sa_len >= offsetof(struct sockaddr, sa_data) &&
sa->sa_family == AF_INET6) {
if (sa->sa_len > sizeof(struct sockaddr_in6))
return FALSE;
mask->sa.sa_len = sizeof(struct sockaddr_in6);
mask->sa.sa_family = AF_INET6;
} else {
if (sa->sa_len > sizeof(struct sockaddr_in))
return FALSE;
mask->sa.sa_len = sizeof(struct sockaddr_in);
mask->sa.sa_family = AF_INET;
}
return TRUE;
}
/*
* Create a routing socket.
*/
sockid_t
rtsock_socket(int type, int protocol, struct sock ** sockp,
const struct sockevent_ops ** ops)
{
struct rtsock *rt;
/*
* There is no superuser check here: regular users are allowed to issue
* (only) RTM_GET requests on routing sockets.
*/
if (type != SOCK_RAW)
return EPROTOTYPE;
/* We could accept only the protocols we know, but this is fine too. */
if (protocol < 0 || protocol >= AF_MAX)
return EPROTONOSUPPORT;
if (TAILQ_EMPTY(&rt_freelist))
return ENOBUFS;
rt = TAILQ_FIRST(&rt_freelist);
TAILQ_REMOVE(&rt_freelist, rt, rt_next);
rt->rt_flags = 0;
rt->rt_family = protocol;
rt->rt_rcvhead = NULL;
rt->rt_rcvtailp = &rt->rt_rcvhead;
rt->rt_rcvlen = 0;
rt->rt_rcvbuf = RT_RCVBUF_DEF;
TAILQ_INSERT_HEAD(&rt_activelist, rt, rt_next);
*sockp = &rt->rt_sock;
*ops = &rtsock_ops;
return SOCKID_RT | (sockid_t)(rt - rt_array);
}
/*
* Enqueue data on the receive queue of a routing socket. The caller must have
* checked whether the receive buffer size allows for the receipt of the data.
*/
static void
rtsock_enqueue(struct rtsock * rt, struct pbuf * pbuf)
{
*rt->rt_rcvtailp = pbuf;
rt->rt_rcvtailp = pchain_end(pbuf);
rt->rt_rcvlen += pchain_size(pbuf);
sockevent_raise(&rt->rt_sock, SEV_RECV);
}
/*
* Determine whether a routing message for address family 'family', originated
* from routing socket 'rtsrc' if not NULL, should be sent to routing socket
* 'rt'. Return TRUE if the message should be sent to this socket, or FALSE
* if it should not.
*/
static int
rtsock_can_send(struct rtsock *rt, struct rtsock *rtsrc, int family)
{
/* Do not send anything on sockets shut down for reading. */
if (sockevent_is_shutdown(&rt->rt_sock, SFL_SHUT_RD))
return FALSE;
/*
* Do not send a reply message to the source of the request if the
* source is not interested in replies to its own requests.
*/
if (rt == rtsrc && (rt->rt_flags & RTF_NOLOOPBACK))
return FALSE;
/*
* For address family specific messages, make sure the routing socket
* is interested in that family. Make an exception if the socket was
* the source of the request, though: we currently do not prevent user
* processes from issuing commands for the "wrong" family.
*/
if (rt->rt_family != AF_UNSPEC && family != AF_UNSPEC &&
rt->rt_family != family && rt != rtsrc)
return FALSE;
/*
* See whether the receive queue of the socket is already full. We do
* not consider the size of the current request, in order to not drop
* larger messages and then enqueue smaller ones.
*/
if (rt->rt_rcvlen >= rt->rt_rcvbuf)
return FALSE;
/* All is well: go on and deliver the message. */
return TRUE;
}
/*
* Send the routing message in 'pbuf' to the given routing socket if possible,
* or check whether such a message could be sent to that socket if 'pbuf' is
* NULL. In the former case, the function takes ownership of 'pbuf'. The
* given routing socket is assumed to be the source of the routing request that
* generated this message. In the latter case, the function returns TRUE if
* the socket would take the message or FALSE if not. If 'family' is not
* AF_UNSPEC, it is to be the address family of the message.
*/
static int
rtsock_msg_one(struct rtsock * rt, int family, struct pbuf * pbuf)
{
if (rtsock_can_send(rt, rt, family)) {
if (pbuf != NULL)
rtsock_enqueue(rt, pbuf);
return TRUE;
} else {
if (pbuf != NULL)
pbuf_free(pbuf);
return FALSE;
}
}
/*
* Send the routing message in 'pbuf' to all matching routing sockets, or check
* whether there are any such matching routing sockets if 'pbuf' is NULL. In
* the former case, the function takes ownership of 'pbuf'. In the latter
* case, the function returns TRUE if there are any matching sockets or FALSE
* if there are none. If 'rtsrc' is not NULL, it is to be the routing socket
* that is the source of the message. If 'family' is not AF_UNSPEC, it is to
* be the address family of the message.
*/
static int
rtsock_msg_match(struct rtsock * rtsrc, int family, struct pbuf * pbuf)
{
struct rtsock *rt, *rtprev;
struct pbuf *pcopy;
rtprev = NULL;
TAILQ_FOREACH(rt, &rt_activelist, rt_next) {
if (!rtsock_can_send(rt, rtsrc, family))
continue;
/*
* There is at least one routing socket that is interested in
* receiving this message, and able to receive it.
*/
if (pbuf == NULL)
return TRUE;
/*
* We need to make copies of the generated message for all but
* the last matching socket, which gets the original. If we're
* out of memory, free the original and stop: there are more
* important things to spend memory on than routing sockets.
*/
if (rtprev != NULL) {
if ((pcopy = rtsock_alloc(pbuf->tot_len)) == NULL) {
pbuf_free(pbuf);
return TRUE;
}
if (pbuf_copy(pcopy, pbuf) != ERR_OK)
panic("unexpected pbuf copy failure");
rtsock_enqueue(rtprev, pcopy);
}
rtprev = rt;
}
if (rtprev != NULL)
rtsock_enqueue(rtprev, pbuf);
else if (pbuf != NULL)
pbuf_free(pbuf);
return (rtprev != NULL);
}
/*
* Dequeue and free the head of the receive queue of a routing socket.
*/
static void
rtsock_dequeue(struct rtsock * rt)
{
struct pbuf *pbuf, **pnext;
size_t size;
pbuf = rt->rt_rcvhead;
assert(pbuf != NULL);
pnext = pchain_end(pbuf);
size = pchain_size(pbuf);
if ((rt->rt_rcvhead = *pnext) == NULL)
rt->rt_rcvtailp = &rt->rt_rcvhead;
assert(rt->rt_rcvlen >= size);
rt->rt_rcvlen -= size;
*pnext = NULL;
pbuf_free(pbuf);
}
/*
* Process a routing message sent on a socket. Return OK on success, in which
* case the caller assumes that the processing routine has sent a reply to the
* user and possibly other routing sockets. Return a negative error code on
* failure, in which case the caller will send the reply to the user instead.
*/
static int
rtsock_process(struct rtsock *rt, struct rt_msghdr * rtm, char * buf,
size_t len, int is_root)
{
struct rtsock_request rtr;
struct rtsock_rta rta;
const struct sockaddr *netmask;
struct sockaddr sa;
union sockaddr_any mask;
size_t off;
int i;
if (rtm->rtm_msglen != len)
return EINVAL;
if (rtm->rtm_version != RTM_VERSION) {
printf("LWIP: PID %d uses routing sockets version %u\n",
rtm->rtm_pid, rtm->rtm_version);
return EPROTONOSUPPORT;
}
/*
* Make sure that we won't misinterpret the rest of the message. While
* looking at the message type, also make
10BC8
sure non-root users can only
* ever issue RTM_GET requests.
*/
switch (rtm->rtm_type) {
case RTM_ADD:
case RTM_DELETE:
case RTM_CHANGE:
case RTM_LOCK:
if (!is_root)
return EPERM;
/* FALLTHROUGH */
case RTM_GET:
break;
default:
return EOPNOTSUPP;
}
/*
* Extract all given addresses. We do not actually support all types
* of entries, but we cannot skip the ones we do not need either.
*/
rtsock_rta_init(&rta);
off = sizeof(*rtm);
assert(off == RT_ROUNDUP(off));
for (i = 0; i < RTAX_MAX; i++) {
if (!(rtm->rtm_addrs & (1 << i)))
continue;
if (off + offsetof(struct sockaddr, sa_data) > len)
return EINVAL;
/*
* It is safe to access sa_len and even sa_family in all cases,
* in particular even when the structure is of size zero.
*/
assert(offsetof(struct sockaddr, sa_data) <= RT_ROUNDUP(0));
memcpy(&sa, &buf[off], offsetof(struct sockaddr, sa_data));
if (off + sa.sa_len > len)
return EINVAL;
rtsock_rta_set(&rta, i, &buf[off], sa.sa_len);
off += RT_ROUNDUP((size_t)sa.sa_len);
}
/*
* Expand the given netmask if it is in compressed IPv4 form. We do
* this here because it is particular to routing sockets; we also do
* the compression in this module. Note how the compression may even
* strip off the address family; really, who came up with this ****?
*/
netmask = (const struct sockaddr *)rta.rta_ptr[RTAX_NETMASK];
if (netmask != NULL) {
if (!rtsock_expand_netmask(&mask, netmask))
return EINVAL;
rtsock_rta_set(&rta, RTAX_NETMASK, &mask, mask.sa.sa_len);
}
/*
* Actually process the command. Pass on enough information so that a
* reply can be generated on success. The abstraction as sketched at
* the top of the file imposes that we pass quite a few parameters.
*/
rtr.rtr_src = rt;
rtr.rtr_pid = rtm->rtm_pid;
rtr.rtr_seq = rtm->rtm_seq;
rtr.rtr_getif = (rtm->rtm_type == RTM_GET &&
(rta.rta_ptr[RTAX_IFP] != NULL || rta.rta_ptr[RTAX_IFA] != NULL));
return route_process(rtm->rtm_type,
(const struct sockaddr *)rta.rta_ptr[RTAX_DST],
(const struct sockaddr *)rta.rta_ptr[RTAX_NETMASK],
(const struct sockaddr *)rta.rta_ptr[RTAX_GATEWAY],
(const struct sockaddr *)rta.rta_ptr[RTAX_IFP],
(const struct sockaddr *)rta.rta_ptr[RTAX_IFA],
rtm->rtm_flags, rtm->rtm_inits, &rtm->rtm_rmx, &rtr);
}
/*
* Perform preliminary checks on a send request.
*/
static int
rtsock_pre_send(struct sock * sock __unused, size_t len,
socklen_t ctl_len __unused, const struct sockaddr * addr,
socklen_t addr_len __unused, endpoint_t user_endpt __unused, int flags)
{
if (flags != 0)
return EOPNOTSUPP;
if (addr != NULL)
return EISCONN;
/*
* For the most basic failures - that is, we cannot even manage to
* receive the request - we do not generate a reply message.
*/
if (len < sizeof(struct rt_msghdr))
return ENOBUFS;
if (len > RT_SNDBUF_MAX)
return EMSGSIZE;
return OK;
}
/*
* Send data on a routing socket.
*/
static int
rtsock_send(struct sock * sock, const struct sockdriver_data * data,
size_t len, size_t * offp, const struct sockdriver_data * ctl __unused,
socklen_t ctl_len __unused, socklen_t * ctl_off __unused,
const struct sockaddr * addr __unused, socklen_t addr_len __unused,
endpoint_t user_endpt, int flags __unused, size_t min __unused)
{
struct rtsock *rt = (struct rtsock *)sock;
char buf[RT_SNDBUF_MAX] __aligned(4);
struct rt_msghdr rtm;
struct pbuf *pbuf;
uid_t euid;
int r, is_root;
/* Copy in the request, and adjust some fields right away. */
assert(len >= sizeof(rtm));
assert(len <= sizeof(buf));
if ((r = sockdriver_copyin(data, 0, buf, len)) != OK)
return r;
memcpy(&rtm, buf, sizeof(rtm));
rtm.rtm_errno = 0;
rtm.rtm_flags &= ~RTF_DONE;
rtm.rtm_pid = getepinfo(user_endpt, &euid, NULL /*gid*/);
is_root = (euid == ROOT_EUID);
/* Process the request. */
r = rtsock_process(rt, &rtm, buf, len, is_root);
/*
* If the request has been processed successfully, a reply has been
* sent already, possibly also to other routing sockets. Here, we
* handle the case that the request has resulted in failure, in which
* case we send a reply to the caller only. This behavior is different
* from the traditional BSD behavior, which also sends failure replies
* to other sockets. Our motivation is that while other parties are
* never going to be interested in failures anyway, it is in fact easy
* for an unprivileged user process to abuse the failure-reply system
* in order to fake other types of routing messages (e.g., RTM_IFINFO)
* to other parties. By sending failure replies only to the requestor,
* we eliminate the need for security-sensitive request validation.
*/
if (r != OK && rtsock_can_send(rt, rt, AF_UNSPEC)) {
rtm.rtm_errno = -r;
if ((pbuf = rtsock_alloc(len)) == NULL)
return ENOMEM;
/* For the reply, reuse the request message largely as is. */
memcpy(pbuf->payload, &rtm, sizeof(rtm));
if (len > sizeof(rtm))
memcpy((uint8_t *)pbuf->payload + sizeof(rtm),
buf + sizeof(rtm), len - sizeof(rtm));
rtsock_enqueue(rt, pbuf);
} else if (r == OK)
*offp = len;
return r;
}
/*
* Perform preliminary checks on a receive request.
*/
static int
rtsock_pre_recv(struct sock * sock __unused, endpoint_t user_endpt __unused,
int flags)
{
/*
* We accept the same flags across all socket types in LWIP, and then
* simply ignore the ones we do not support for routing sockets.
*/
if ((flags & ~(MSG_PEEK | MSG_WAITALL)) != 0)
return EOPNOTSUPP;
return OK;
}
/*
* Receive data on a routing socket.
*/
static int
rtsock_recv(struct sock * sock, const struct sockdriver_data * data,
size_t len, size_t * off, const struct sockdriver_data * ctl __unused,
socklen_t ctl_len __unused, socklen_t * ctl_off __unused,
struct sockaddr * addr, socklen_t * addr_len,
endpoint_t user_endpt __unused, int flags, size_t min __unused,
int * rflags)
{
struct rtsock *rt = (struct rtsock *)sock;
struct pbuf *pbuf;
int r;
if ((pbuf = rt->rt_rcvhead) == NULL)
return SUSPEND;
/* Copy out the data to the calling user process. */
if (len >= pbuf->tot_len)
len = pbuf->tot_len;
else
*rflags |= MSG_TRUNC;
r = util_copy_data(data, len, 0, pbuf, 0, FALSE /*copy_in*/);
if (r != OK)
return r;
/* Generate a dummy source address. */
addr->sa_len = RTSOCK_ADDR_LEN;
addr->sa_family = AF_ROUTE;
*addr_len = RTSOCK_ADDR_LEN;
/* Discard the data now, unless we were instructed to peek only. */
if (!(flags & MSG_PEEK))
rtsock_dequeue(rt);
/* Return the received part of the data length. */
*off = len;
return OK;
}
/*
* Test whether data can be received on a routing socket, and if so, how many
* bytes of data.
*/
static int
rtsock_test_recv(struct sock * sock, size_t min __unused, size_t * size)
{
struct rtsock *rt = (struct rtsock *)sock;
if (rt->rt_rcvhead == NULL)
return SUSPEND;
if (size != NULL)
*size = rt->rt_rcvhead->tot_len;
return OK;
}
/*
* Set socket options on a routing socket.
*/
static int
rtsock_setsockopt(struct sock * sock, int level, int name,
const struct sockdriver_data * data, socklen_t len)
{
struct rtsock *rt = (struct rtsock *)sock;
int r, val;
if (level == SOL_SOCKET) {
switch (name) {
case SO_USELOOPBACK:
if ((r = sockdriver_copyin_opt(data, &val, sizeof(val),
len)) != OK)
return r;
if (!val)
rt->rt_flags |= RTF_NOLOOPBACK;
else
rt->rt_flags &= ~RTF_NOLOOPBACK;
return OK;
case SO_RCVBUF:
if ((r = sockdriver_copyin_opt(data, &val, sizeof(val),
len)) != OK)
return r;
if (val < RT_RCVBUF_MIN || val > RT_RCVBUF_MAX)
return EINVAL;
rt->rt_rcvbuf = (size_t)val;
return OK;
}
}
return ENOPROTOOPT;
}
/*
* Retrieve socket options on a routing socket.
*/
static int
rtsock_getsockopt(struct sock * sock, int level, int name,
const struct sockdriver_data * data, socklen_t * len)
{
struct rtsock *rt = (struct rtsock *)sock;
int val;
if (level == SOL_SOCKET) {
switch (name) {
case SO_USELOOPBACK:
val = !(rt->rt_flags & RTF_NOLOOPBACK);
return sockdriver_copyout_opt(data, &val, sizeof(val),
len);
case SO_RCVBUF:
val = rt->rt_rcvbuf;
return sockdriver_copyout_opt(data, &val, sizeof(val),
len);
}
}
return ENOPROTOOPT;
}
/*
* Retrieve the local or remote socket address of a routing socket.
*/
static int
rtsock_getname(struct sock * sock __unused, struct sockaddr * addr,
socklen_t * addr_len)
{
/* This is entirely useless but apparently common between OSes. */
addr->sa_len = RTSOCK_ADDR_LEN;
addr->sa_family = AF_ROUTE;
*addr_len = RTSOCK_ADDR_LEN;
return OK;
}
/*
* Drain the receive queue of a routing socket.
*/
static void
rtsock_drain(struct rtsock * rt)
{
while (rt->rt_rcvhead != NULL)
rtsock_dequeue(rt);
}
/*
* Shut down a routing socket for reading and/or writing.
*/
static int
rtsock_shutdown(struct sock * sock, unsigned int mask)
{
struct rtsock *rt = (struct rtsock *)sock;
if (mask & SFL_SHUT_RD)
rtsock_drain(rt);
return OK;
}
/*
* Close a routing socket.
*/
static int
rtsock_close(struct sock * sock, int force __unused)
{
struct rtsock *rt = (struct rtsock *)sock;
rtsock_drain(rt);
return OK;
}
/*
* Free up a closed routing socket.
*/
static void
rtsock_free(struct sock * sock)
{
struct rtsock *rt = (struct rtsock *)sock;
TAILQ_REMOVE(&rt_activelist, rt, rt_next);
TAILQ_INSERT_HEAD(&rt_freelist, rt, rt_next);
}
static const struct sockevent_ops rtsock_ops = {
.sop_pre_send = rtsock_pre_send,
.sop_send = rtsock_send,
.sop_pre_recv = rtsock_pre_recv,
.sop_recv = rtsock_recv,
.sop_test_recv = rtsock_test_recv,
.sop_setsockopt = rtsock_setsockopt,
.sop_getsockopt = rtsock_getsockopt,
.sop_getsockname = rtsock_getname,
.sop_getpeername = rtsock_getname,
.sop_shutdown = rtsock_shutdown,
.sop_close = rtsock_close,
.sop_free = rtsock_free
};
/*
* Send an interface announcement message about the given interface. If
* 'arrival' is set, the interface has just been created; otherwise, the
* interface is about to be destroyed.
*/
void
rtsock_msg_ifannounce(struct ifdev * ifdev, int arrival)
{
struct if_announcemsghdr ifan;
struct pbuf *pbuf;
if (!rtsock_msg_match(NULL /*rtsrc*/, AF_UNSPEC, NULL /*pbuf*/))
return;
memset(&ifan, 0, sizeof(ifan));
ifan.ifan_msglen = sizeof(ifan);
ifan.ifan_version = RTM_VERSION;
ifan.ifan_type = RTM_IFANNOUNCE;
ifan.ifan_index = ifdev_get_index(ifdev);
strlcpy(ifan.ifan_name, ifdev_get_name(ifdev), sizeof(ifan.ifan_name));
ifan.ifan_what = (arrival) ? IFAN_ARRIVAL : IFAN_DEPARTURE;
if ((pbuf = rtsock_alloc(sizeof(ifan))) == NULL)
return;
memcpy(pbuf->payload, &ifan, sizeof(ifan));
rtsock_msg_match(NULL /*rtsrc*/, AF_UNSPEC, pbuf);
}
/*
* Send an interface information routing message.
*/
void
rtsock_msg_ifinfo(struct ifdev * ifdev)
{
struct if_msghdr ifm;
struct pbuf *pbuf;
if (!rtsock_msg_match(NULL /*rtsrc*/, AF_UNSPEC, NULL /*pbuf*/))
return;
memset(&ifm, 0, sizeof(ifm));
ifm.ifm_msglen = sizeof(ifm);
ifm.ifm_version = RTM_VERSION;
ifm.ifm_type = RTM_IFINFO;
ifm.ifm_addrs = 0;
ifm.ifm_flags = ifdev_get_ifflags(ifdev);
ifm.ifm_index = ifdev_get_index(ifdev);