-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaf_name.c
More file actions
2016 lines (1807 loc) · 50.8 KB
/
af_name.c
File metadata and controls
2016 lines (1807 loc) · 50.8 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
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
#include <linux/ctype.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/ipv6.h>
#include <linux/net.h>
#include <linux/module.h>
#include <net/sock.h>
#include <net/inet_connection_sock.h>
#include <net/inet6_hashtables.h>
#include <net/ipv6.h>
#include <net/tcp_states.h>
#include <net/transp_v6.h>
#include <linux/inname.h>
#include "dns.h"
#include "nameser.h"
#include "namestack_priv.h"
enum {
NAME_RESOLVING = TCP_MAX_STATES, /* Don't overlap with TCP states */
NAME_BINDING,
NAME_CONNECTING,
};
enum {
NAMEF_RESOLVING = (1 << NAME_RESOLVING),
NAMEF_BINDING = (1 << NAME_BINDING),
NAMEF_CONNECTING = (1 << NAME_CONNECTING),
};
static void name_stream_state_change(struct sock *sk)
{
struct name_stream_sock *name;
read_lock(&sk->sk_callback_lock);
if (!(name = sk->sk_user_data))
goto out;
printk(KERN_INFO "sk_state is %d\n", sk->sk_state);
switch (sk->sk_state) {
case TCP_ESTABLISHED:
name->sk.sk_state = TCP_ESTABLISHED;
name->sk.sk_state_change(&name->sk);
break;
case TCP_FIN_WAIT1:
/* The client initiated a shutdown of the socket */
break;
case TCP_CLOSE_WAIT:
/* The server initiated a shutdown of the socket */
case TCP_SYN_SENT:
case TCP_CLOSING:
/*
* If the server closed down the connection, make sure that
* we back off before reconnecting
*/
break;
case TCP_LAST_ACK:
break;
case TCP_CLOSE:
break;
}
out:
read_unlock(&sk->sk_callback_lock);
}
static int name_is_local(const char *name)
{
const char *p;
if (!name[0])
return 0;
p = name + strlen(name) - 1;
if (*p != '.')
return 0;
for (p = p - 1; *p != '.' && p >= name; p--)
;
if (p == name)
return 0;
return !strcasecmp(p + 1, "localhost.");
}
/* If name ends in the IPv4 canonical suffix .in-addr.arpa., returns a
* pointer to the suffix, beginning with the dot. Otherwise returns NULL.
*/
static const char *name_find_v4_canonical_suffix(const char *name)
{
static const char canon_v4_suffix[] = ".in-addr.arpa.";
if (strlen(name) > strlen(canon_v4_suffix)) {
const char *p = name + strlen(name) - strlen(canon_v4_suffix);
if (!strcasecmp(p, canon_v4_suffix))
return p;
}
return NULL;
}
/* If name ends in the IPv6 canonical suffix .ip6.arpa., returns a
* pointer to the suffix, beginning with the dot. Otherwise returns NULL.
*/
static const char *name_find_v6_canonical_suffix(const char *name)
{
static const char canon_v6_suffix[] = ".ip6.arpa.";
if (strlen(name) > strlen(canon_v6_suffix)) {
const char *p = name + strlen(name) - strlen(canon_v6_suffix);
if (!strcasecmp(p, canon_v6_suffix))
return p;
}
return NULL;
}
static inline int name_is_canonical(const char *name)
{
return name_find_v4_canonical_suffix(name) != NULL ||
name_find_v6_canonical_suffix(name) != NULL;
}
static int name_stream_release(struct socket *sock)
{
struct sock *sk = sock->sk;
struct name_stream_sock *name = name_stream_sk(sk);
if (!sk)
goto out;
if (!sock_flag(sk, SOCK_DEAD))
sk->sk_state_change(sk);
if (name->dname_answer) {
kfree(name->dname_answer);
name->dname_answer = NULL;
name->dname_answer_len = 0;
name->dname_answer_index = 0;
}
if (name->sname.sname_addr.name[0]) {
name_cache_delete(name->sname.sname_addr.name);
if (!name_is_local(name->sname.sname_addr.name) &&
!name_is_canonical(name->sname.sname_addr.name))
name_delete_registration(name->sname.sname_addr.name);
}
if (name->ipv6_sock) {
kernel_sock_shutdown(name->ipv6_sock, SHUT_WR);
sock_release(name->ipv6_sock);
name->ipv6_sock = NULL;
}
if (name->ipv4_sock) {
kernel_sock_shutdown(name->ipv4_sock, SHUT_WR);
sock_release(name->ipv4_sock);
name->ipv4_sock = NULL;
}
sock_set_flag(sk, SOCK_DEAD);
sock->sk = NULL;
sk_refcnt_debug_release(sk);
sock_put(sk);
out:
return 0;
}
/* Stolen from net/ipv6/ipv6_sockglue.c */
static
struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
struct ipv6_txoptions *opt)
{
if (inet_sk(sk)->is_icsk) {
/* The original version of this only updates the options if the
* socket is not listening or closed, but I want the options to
* be set even on SYN/SYN-ACK packets, so I update the socket
* irrespective of state.
*/
if (opt) {
struct inet_connection_sock *icsk = inet_csk(sk);
icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
}
opt = xchg(&inet6_sk(sk)->opt, opt);
} else {
write_lock(&sk->sk_dst_lock);
opt = xchg(&inet6_sk(sk)->opt, opt);
write_unlock(&sk->sk_dst_lock);
}
sk_dst_reset(sk);
return opt;
}
/* Stolen from net/ipv6/exthdrs.c. That one takes an ipv6_opt_hdr from user-
* space, but this doesn't, so the copy_from_user is removed.
*/
static int ipv6_renew_option(void *ohdr,
struct ipv6_opt_hdr *newopt, int newoptlen,
int inherit,
struct ipv6_opt_hdr **hdr,
char **p)
{
if (inherit) {
if (ohdr) {
memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr));
*hdr = (struct ipv6_opt_hdr *)*p;
*p += CMSG_ALIGN(ipv6_optlen(*(struct ipv6_opt_hdr **)hdr));
}
} else {
if (newopt) {
memcpy(*p, newopt, newoptlen);
*hdr = (struct ipv6_opt_hdr *)*p;
*p += CMSG_ALIGN(newoptlen);
}
}
return 0;
}
/* Identical to ipv6_renew_options in net/ipv6/exthdrs.c, but calls the
* modified ipv6_renew_option (above).
*/
struct ipv6_txoptions *
namestack_ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt,
int newtype,
struct ipv6_opt_hdr *newopt, int newoptlen)
{
int tot_len = 0;
char *p;
struct ipv6_txoptions *opt2;
int err;
if (opt) {
if (newtype != IPV6_HOPOPTS && opt->hopopt)
tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt));
if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt)
tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt));
if (newtype != IPV6_RTHDR && opt->srcrt)
tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt));
if (newtype != IPV6_DSTOPTS && opt->dst1opt)
tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt));
}
if (newopt && newoptlen)
tot_len += CMSG_ALIGN(newoptlen);
if (!tot_len)
return NULL;
tot_len += sizeof(*opt2);
opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC);
if (!opt2)
return ERR_PTR(-ENOBUFS);
memset(opt2, 0, tot_len);
opt2->tot_len = tot_len;
p = (char *)(opt2 + 1);
err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen,
newtype != IPV6_HOPOPTS,
&opt2->hopopt, &p);
if (err)
goto out;
err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen,
newtype != IPV6_RTHDRDSTOPTS,
&opt2->dst0opt, &p);
if (err)
goto out;
err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen,
newtype != IPV6_RTHDR,
(struct ipv6_opt_hdr **)&opt2->srcrt, &p);
if (err)
goto out;
err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen,
newtype != IPV6_DSTOPTS,
&opt2->dst1opt, &p);
if (err)
goto out;
opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) +
(opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) +
(opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0);
opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0);
return opt2;
out:
sock_kfree_s(sk, opt2, opt2->tot_len);
return ERR_PTR(err);
}
struct name_opt_hdr
{
__u8 type;
__u8 len;
/* Followed by the actual name */
};
/* FIXME: Change name options to the "real" values once they're known. Must
* <= 63.
*/
#define NAME_OPTION_SOURCE_NAME 17
#define NAME_OPTION_DEST_NAME 18
static void rfc1035_encode_name(char *dst, const char *name)
{
const char *p = name;
while (p && *p)
{
const char *dot = strchr(p, '.');
if (dot)
{
unsigned char len = dot - p;
*dst = len;
memcpy(dst + 1, p, len);
dst += len + 1;
p = dot + 1;
}
else
p = NULL;
}
*dst = 0;
}
static int set_name_option(struct socket *sock, const char *name, __u8 opt_type)
{
struct sock *sk = sock->sk;
struct ipv6_pinfo *np = inet6_sk(sk);
struct ipv6_txoptions *opt;
char *name_opt_buf;
struct ipv6_opt_hdr *opt_hdr;
struct name_opt_hdr *name_opt_hdr;
int err, name_opt_len;
if (np->opt && np->opt->dst1opt) {
name_opt_len = ipv6_optlen(np->opt->dst1opt);
name_opt_len += sizeof(struct name_opt_hdr) + strlen(name) + 1;
err = -ENOMEM;
name_opt_buf = kmalloc(name_opt_len, GFP_ATOMIC);
if (!name_opt_buf)
goto out;
memset(name_opt_buf, 0, name_opt_len);
memcpy(name_opt_buf, np->opt->dst1opt,
ipv6_optlen(np->opt->dst1opt));
opt_hdr = (struct ipv6_opt_hdr *)name_opt_buf;
name_opt_hdr = (struct name_opt_hdr *)(opt_hdr + 1);
name_opt_hdr = (struct name_opt_hdr *)((char *)name_opt_hdr +
sizeof(struct name_opt_hdr) + name_opt_hdr->len);
name_opt_hdr->type = opt_type;
/* Happily the RFC1035-encoded name has the same length as the
* C string.
*/
name_opt_hdr->len = strlen(name) + 1;
rfc1035_encode_name((char *)(name_opt_hdr + 1), name);
opt_hdr->nexthdr = 0;
opt_hdr->hdrlen = (name_opt_len + 1) >> 3;
}
else {
struct ipv6_opt_hdr tmp_opt_hdr;
/* Use to calculate the required length */
tmp_opt_hdr.nexthdr = 0;
/* FIXME: this is the reverse of ipv6_optlen, used to calculate
* name_opt_len. Are you sure it's correct? Is there a nice
* macro/calculation somewhere?
*/
tmp_opt_hdr.hdrlen =
(sizeof(struct name_opt_hdr) + strlen(name) + 1) >> 3;
name_opt_len = ipv6_optlen(&tmp_opt_hdr);
err = -ENOMEM;
name_opt_buf = kmalloc(name_opt_len, GFP_ATOMIC);
if (!name_opt_buf)
goto out;
memset(name_opt_buf, 0, name_opt_len);
opt_hdr = (struct ipv6_opt_hdr *)name_opt_buf;
name_opt_hdr = (struct name_opt_hdr *)(opt_hdr + 1);
name_opt_hdr->type = opt_type;
/* Happily the RFC1035-encoded name has the same length as the
* C string.
*/
name_opt_hdr->len = strlen(name) + 1;
rfc1035_encode_name((char *)(name_opt_hdr + 1), name);
opt_hdr->nexthdr = 0;
opt_hdr->hdrlen =
(sizeof(struct name_opt_hdr) + name_opt_hdr->len) >> 3;
}
/* Rather than calling kernel_setsockopt, set the option directly to
* avoid a permissions check on the calling process.
*/
opt = namestack_ipv6_renew_options(sk, np->opt, IPV6_DSTOPTS,
(struct ipv6_opt_hdr *)name_opt_buf,
name_opt_len);
if (IS_ERR(opt)) {
err = PTR_ERR(opt);
goto out;
}
err = 0;
opt = ipv6_update_options(sk, opt);
if (opt)
sock_kfree_s(sk, opt, opt->tot_len);
out:
if (name_opt_buf)
kfree(name_opt_buf);
return err;
}
#if defined(CONFIG_NAMESTACK_MODULE)
/* Stolen from net/ipv6/exthdrs.c */
int ipv6_find_tlv(struct sk_buff *skb, int offset, int type)
{
const unsigned char *nh = skb_network_header(skb);
int packet_len = skb->tail - skb->network_header;
struct ipv6_opt_hdr *hdr;
int len;
if (offset + 2 > packet_len)
goto bad;
hdr = (struct ipv6_opt_hdr *)(nh + offset);
len = ((hdr->hdrlen + 1) << 3);
if (offset + len > packet_len)
goto bad;
offset += 2;
len -= 2;
while (len > 0) {
int opttype = nh[offset];
int optlen;
if (opttype == type)
return offset;
switch (opttype) {
case IPV6_TLV_PAD0:
optlen = 1;
break;
default:
optlen = nh[offset + 1] + 2;
if (optlen > len)
goto bad;
break;
}
offset += optlen;
len -= optlen;
}
/* not_found */
bad:
return -1;
}
#endif
static char *rfc1035_decode_name(const u8 *p, int len)
{
const u8 *q;
int name_len = 0;
char *name = NULL;
for (q = p; *q && q - p <= len; q += *q + 1)
name_len += *q + 1;
if (!*q && q - p <= len) {
name_len += 1;
name = kmalloc(name_len, GFP_ATOMIC);
if (name) {
char *dst;
for (q = p, dst = name; *q && q - p <= len;
dst += *q + 1, q += *q + 1) {
memcpy(dst, q + 1, *q);
dst[*q] = '.';
}
*dst = 0;
}
}
return name;
}
static inline char *name_option_to_str(struct sk_buff *skb, u16 offset)
{
const unsigned char *nh = skb_network_header(skb);
const struct name_opt_hdr *name_hdr =
(const struct name_opt_hdr *)(nh + offset);
const u8 *name_ptr = (const u8 *)(name_hdr + 1);
return rfc1035_decode_name(name_ptr, name_hdr->len);
}
static int name_option_matches(struct sk_buff *skb, u16 offset,
const char *name)
{
int matches = 0;
char *option_name = name_option_to_str(skb, offset);
if (option_name) {
matches = !strcmp(name, option_name);
printk(KERN_INFO "destination name %s %s %s\n", option_name,
matches ? "matches" : "doesn't match", name);
kfree(option_name);
}
return matches;
}
struct syn_entry
{
struct in6_addr peer_addr;
__be16 peer_port;
struct name_addr name;
struct hlist_node entry;
};
/* NAME_SYN_BUCKETS must be a power of 2, or the "& (NAME_SYN_BUCKETS - 1)"
* below must be changed to "% NAME_SYN_BUCKETS".
*/
#define NAME_SYN_BUCKETS 16
static struct hlist_head name_stream_syns[NAME_SYN_BUCKETS];
static DEFINE_SPINLOCK(name_stream_syn_lock);
static void name_stream_store_syn(struct sock *sk, struct sk_buff *skb,
int source_name_offset)
{
u32 bucket;
char *source_name;
const struct inet_sock *inet = inet_sk(sk);
const struct ipv6_pinfo *np = inet6_sk(sk);
const struct in6_addr *saddr = &np->rcv_saddr;
const __u16 port = inet->dport;
bucket = inet6_sk_ehashfn(sk) & (NAME_SYN_BUCKETS - 1);
source_name = name_option_to_str(skb, source_name_offset);
if (source_name) {
struct syn_entry *entry, *found = NULL;
struct hlist_node *node;
printk(KERN_INFO "see source name option %s\n",
(char *)source_name);
printk(KERN_INFO "port is %d, bucket is %d\n", port, bucket);
/* FIXME: lock each bucket rather than the whole table. */
spin_lock_irq(&name_stream_syn_lock);
hlist_for_each_entry(entry,
node,
&name_stream_syns[bucket],
entry)
{
if (!memcmp(saddr, &entry->peer_addr, sizeof(saddr))
&& port == entry->peer_port)
{
found = entry;
break;
}
}
if (found)
{
/* An entry with the same IP address and port exists,
* replace its name.
*/
strcpy(found->name.name, source_name);
}
else
{
found = kzalloc(sizeof(struct syn_entry), GFP_ATOMIC);
/* No entry was found, insert a new entry into the
* list.
*/
if (found)
{
strcpy(found->name.name, source_name);
memcpy(&found->peer_addr, saddr, sizeof(saddr));
found->peer_port = port;
hlist_add_head(&found->entry,
&name_stream_syns[bucket]);
}
}
spin_unlock_irq(&name_stream_syn_lock);
kfree(source_name);
}
}
static struct sock *name_v6_recv_syn(struct sock *sk, struct sk_buff *skb,
struct request_sock *req,
struct dst_entry *dst)
{
struct name_stream_sock *name = sk->sk_user_data;
struct sock *ret = NULL;
u16 offset;
struct ipv6_opt_hdr *exthdr =
(struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
const unsigned char *nh = skb_network_header(skb);
unsigned int packet_len = skb->tail - skb->network_header;
u8 *nexthdr;
int source_name_offset = -1, dest_name_offset = -1;
nexthdr = &ipv6_hdr(skb)->nexthdr;
offset = sizeof(struct ipv6hdr);
while (offset + 1 <= packet_len) {
switch (*nexthdr) {
case NEXTHDR_DEST:
if (dest_name_offset == -1)
dest_name_offset = ipv6_find_tlv(skb, offset,
NAME_OPTION_DEST_NAME);
if (source_name_offset == -1)
source_name_offset = ipv6_find_tlv(skb, offset,
NAME_OPTION_SOURCE_NAME);
break;
}
offset += ipv6_optlen(exthdr);
nexthdr = &exthdr->nexthdr;
exthdr = (struct ipv6_opt_hdr *)(nh + offset);
}
/* Only accept if there's no dest name option or if the dest name
* matches our (source) name.
*/
if (dest_name_offset == -1 ||
name_option_matches(skb, dest_name_offset,
name->sname.sname_addr.name)) {
ret = name->orig_syn_recv_sock(sk, skb, req, dst);
if (ret) {
if (source_name_offset != -1) {
/* The SYN packet contains a source name option,
* so store it for subsequent use by
* name_stream_accept.
* (The more obvious thing to do would be to
* return a struct sock * that contained the
* name in it directly, but the kernel makes
* assumptions about the return type of this
* function that I never fully understood. The
* effect was to hang or crash the kernel.
* This approach works around my own lack of
* understanding.)
*/
name_stream_store_syn(ret, skb,
source_name_offset);
}
}
}
return ret;
}
static struct inet_connection_sock_af_ops name_tcp6_af_ops;
static int name_tcp6_af_ops_init;
static int name_create_v6_sock(int type, int protocol, struct socket **sock,
struct name_stream_sock *name)
{
int err = sock_create_kern(PF_INET6, type, protocol, sock);
printk("%s:%d - %s : sock_create_kern() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, err);
if (!err) {
err = set_name_option(*sock, name->sname.sname_addr.name,
NAME_OPTION_SOURCE_NAME);
}
if (!err) {
int on = 1;
err = kernel_setsockopt(*sock, IPPROTO_IPV6, IPV6_V6ONLY,
(char *)&on, sizeof(on));
}
if (!err) {
struct inet_connection_sock *icsk = inet_csk((*sock)->sk);
(*sock)->sk->sk_user_data = name;
(*sock)->sk->sk_state_change = name_stream_state_change;
if (!name_tcp6_af_ops_init) {
memcpy(&name_tcp6_af_ops, icsk->icsk_af_ops,
sizeof(struct inet_connection_sock_af_ops));
name_tcp6_af_ops.syn_recv_sock = name_v6_recv_syn;
name_tcp6_af_ops_init = 1;
}
name->orig_syn_recv_sock = icsk->icsk_af_ops->syn_recv_sock;
icsk->icsk_af_ops = &name_tcp6_af_ops;
}
return err;
}
static int name_create_v4_sock(int type, int protocol, struct socket **sock,
struct name_stream_sock *name)
{
int err = sock_create_kern(PF_INET, type, protocol, sock);
printk("%s:%d - %s : sock_create_kern() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, err);
if (!err) {
(*sock)->sk->sk_user_data = name;
(*sock)->sk->sk_state_change = name_stream_state_change;
}
return err;
}
static int name_bind_to_fqdn(struct name_stream_sock *name, const char *fqdn,
const __be32 *v4addr,
const struct in6_addr *v6addr)
{
int err = 0;
printk(KERN_INFO "bound to %s\n", fqdn);
/* If a particular port or address is specified, bind() must fail if
* the port or address is unavailable, hence we must create the
* transport sockets if they don't already exist so we may attempt to
* bind to the specified address and port. If no address or port is
* specified, name_register() has already checked that the name is
* available, so bind() succeeds without needing to create the sockets
* yet. (The sockets will be created as necessary during connect())
*/
// v4addr and v6addr are _not_ set when called from name_register_cb
printk("%s:%d - %s : v4addr: %p\n", __FILE__, __LINE__, __FUNCTION__, v4addr);
if (name->sname.sname_port || v4addr) {
struct sockaddr_in sin;
if (!name->ipv4_sock) {
err = name_create_v4_sock(SOCK_STREAM, 0,
&name->ipv4_sock, name);
printk("%s:%d - %s : name_create_v4_sock() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, err);
if (err)
goto out;
}
memset(&sin, 0, sizeof(sin));
if (v4addr)
memcpy(&sin.sin_addr.s_addr, &v4addr, sizeof(v4addr));
sin.sin_port = name->sname.sname_port;
err = kernel_bind(name->ipv4_sock, (struct sockaddr *)&sin,
sizeof(sin));
if (err)
goto out;
}
if (name->sname.sname_port || v6addr) {
struct sockaddr_in6 sin;
if (!name->ipv6_sock) {
err = name_create_v6_sock(SOCK_STREAM, 0,
&name->ipv6_sock, name);
printk("%s:%d - %s : name_create_v6_sock() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, err);
if (err)
goto out;
}
memset(&sin, 0, sizeof(sin));
if (v6addr) {
memcpy(&sin.sin6_addr, v6addr, sizeof(sin.sin6_addr));
/* If it's a link-local address, match the address to
* a scope id that defines the interface on which it'll
* be used.
*/
if (sin.sin6_addr.s6_addr[0] == 0xfe &&
sin.sin6_addr.s6_addr[1] == 0x80)
{
err = match_v6_address_to_scope(&sin);
if (err)
goto out;
}
}
sin.sin6_port = name->sname.sname_port;
err = kernel_bind(name->ipv6_sock, (struct sockaddr *)&sin,
sizeof(sin));
}
out:
return err;
}
static void name_register_cb(int result, const char *bound_name, void *data)
{
struct socket *sock = data;
struct sock *sk = sock->sk;
struct name_stream_sock *name = name_stream_sk(sk);
if (!result) {
result = name_bind_to_fqdn(name, bound_name, NULL, NULL);
printk("%s:%d - %s : name_bind_to_fqdn() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, result);
}
sk->sk_state &= ~NAMEF_BINDING;
name->async_error = -result;
}
/* Parses the canonical name into the IPv4 address it represents, in host
* byte order.
* Returns -EINVAL if the name is not an IPv4 address, and 0 otherwise.
*/
static int name_parse_canonical_v4(const char *name, unsigned int *addr)
{
const char *p;
int i, r;
unsigned int a1, a2, a3, a4;
p = name_find_v4_canonical_suffix(name);
if (!p)
return -EINVAL;
/* Skip past the 4 octets of the IP address */
for (i = 0; i < 4; i++) {
for (--p; p > name && isdigit(*p); --p)
;
if (p > name && *p != '.')
return -EINVAL;
}
if (p > name)
++p;
r = sscanf(p, "%u.%u.%u.%u.", &a4, &a3, &a2, &a1);
if (r != 4)
return -EINVAL;
if (a1 > 255 || a2 > 255 || a3 > 255 || a4 > 255)
return -EINVAL;
*addr = (a4 << 24) | (a3 << 16) | (a2 << 8) | a1;
return 0;
}
static int name_parse_v6_label(const char *label, uint8_t addr[16],
int *bytesParsed, const char **endPtr)
{
const char *p = label;
uint8_t *dst = addr;
int nibbleCount = 0;
memset(addr, 0, 16 * sizeof(uint8_t));
*bytesParsed = 0;
if (*(p++) != '\\') return -EINVAL;
if (*(p++) != '[') return -EINVAL;
/* Only hexadecimal labels are supported */
if (*(p++) != 'x') return -EINVAL;
for (; isalnum(*p); p++)
{
uint8_t nibble;
if (isdigit(*p))
nibble = *p - '0';
else if (*p >= 'a' && *p <= 'f')
nibble = 10 + *p - 'a';
else if (*p >= 'A' && *p <= 'F')
nibble = 10 + *p - 'A';
else
return -EINVAL;
if (nibbleCount & 1)
*(dst++) |= nibble;
else
*dst = nibble << 4;
nibbleCount++;
}
if (*p == ']')
{
*bytesParsed = nibbleCount >> 1;
*endPtr = p + 1;
return 0;
}
else if (*p == '/')
{
int bitCount = 0;
for (++p; isdigit(*p); ++p) {
bitCount *= 10;
bitCount += *p - '0';
}
if (*p != ']')
return -EINVAL;
if (bitCount >> 3 != nibbleCount >> 1)
return -EINVAL;
*bytesParsed = bitCount >> 3;
*endPtr = p + 1;
return 0;
}
else
return -EINVAL;
}
/* Parses the canonical name into the IPv6 address it represents, in host
* byte order.
* Returns -EINVAL if the name is not an IPv6 address, and 0 otherwise.
*/
static int name_parse_canonical_v6(const char *name, struct in6_addr *v6addr)
{
const char *next, *p;
uint8_t labelAddr[16];
int r, bytesParsed;
p = name_find_v6_canonical_suffix(name);
if (!p)
return -EINVAL;
for (--p; p > name && *p != '.'; --p)
;
if (*p == '.')
++p;
/* This only parses a single label, because the canonical form of an
* address requires the fewest labels (1) possible to specify the
* address (see RFC2673.)
*/
r = name_parse_v6_label(p, labelAddr, &bytesParsed, &next);
if (!r) {
if (bytesParsed != sizeof(labelAddr))
r = -EINVAL;
else
memcpy(v6addr->s6_addr, labelAddr, sizeof(labelAddr));
}
return r;
}
static int name_register(struct socket *sock, const char *fully_qualified_name,
__be16 port)
{
struct sock *sk = sock->sk;
struct name_stream_sock *name = name_stream_sk(sk);
int err;
printk(KERN_INFO "name qualified as %s\n", fully_qualified_name);
strcpy(name->sname.sname_addr.name, fully_qualified_name);
name->sname.sname_port = port;
err = name_cache_add(fully_qualified_name, sock);
if (err)
goto out;
//assert(strlen(fully_qualified_name) > 1);
if (!strchr(fully_qualified_name, '.')) {
/* FIXME: name doesn't exist in any domain. Do I need to make
* a canonical name out of it?
*/
name_cache_delete(fully_qualified_name);
err = -EINVAL;
goto out;
}
if (name_is_local(fully_qualified_name)) {
__be32 v4loopback = htonl(INADDR_LOOPBACK);
struct in6_addr v6loopback = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
err = name_bind_to_fqdn(name, fully_qualified_name, &v4loopback,
&v6loopback);
printk("%s:%d - %s : name_bind_to_fqdn() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, err);
}
else if (name_find_v4_canonical_suffix(fully_qualified_name) != NULL) {
__be32 v4addr;
err = name_parse_canonical_v4(fully_qualified_name, &v4addr);
if (!err) {
err = name_bind_to_fqdn(name, fully_qualified_name,
&v4addr, NULL);
printk("%s:%d - %s : name_bind_to_fqdn() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, err);
}
}
else if (name_find_v6_canonical_suffix(fully_qualified_name) != NULL) {
struct in6_addr v6addr;
err = name_parse_canonical_v6(fully_qualified_name, &v6addr);
if (!err) {
err = name_bind_to_fqdn(name, fully_qualified_name,
NULL, &v6addr);
printk("%s:%d - %s : name_bind_to_fqdn() : err: %d\n", __FILE__, __LINE__, __FUNCTION__, err);
}
}
else {
struct in6_addr *v6_addresses;
__be32 *v4_addresses;
int num_v6_addresses;
int num_v4_addresses;
err = choose_addresses(&num_v6_addresses, &v6_addresses,
&num_v4_addresses, &v4_addresses);
if (!err) {
err = name_send_registration(fully_qualified_name,
v6_addresses,
num_v6_addresses,
v4_addresses,
num_v4_addresses,
name_register_cb, sock);
kfree(v6_addresses);
kfree(v4_addresses);
}
}
if (err)
name_cache_delete(fully_qualified_name);
out:
if (err) {
name->async_error = -err;
sk->sk_state &= ~NAMEF_BINDING;
sk->sk_state_change(sk);
}
return err;
}
static void name_qualify_cb(const char *fully_qualified_name, void *data)
{
struct socket *sock = data;
struct sock *sk = sock->sk;
struct name_stream_sock *name = name_stream_sk(sk);
name_register(sock, fully_qualified_name, name->sname.sname_port);
}
static long name_wait_for_bind(struct sock *sk, long timeo)
{
DEFINE_WAIT(wait);
prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
while ((1 << sk->sk_state) & NAMEF_BINDING) {
release_sock(sk);
timeo = schedule_timeout(timeo);
lock_sock(sk);
if (signal_pending(current) || !timeo)
break;
prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
}
finish_wait(sk->sk_sleep, &wait);
return timeo;
}