-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnshkmod.c
1393 lines (1127 loc) · 32.4 KB
/
nshkmod.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
/* nshkmod.c
* NSH kernel module implementation.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/rculist.h>
#include <linux/hash.h>
#include <linux/udp.h>
#include <net/protocol.h>
#include <net/udp.h>
#include <net/sock.h>
#include <net/route.h>
#include <net/ip6_route.h>
#include <net/udp_tunnel.h>
#include <net/vxlan.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include <net/rtnetlink.h>
#include <net/genetlink.h>
#include "nshkmod.h"
/*
*
* Network Service Header format.
* https://tools.ietf.org/html/draft-ietf-sfc-nsh-01
*
* Base Heder
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Ver|O|C|R|R|R|R|R|R| Length | MD Type | Next Protocol |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Service Path Header
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Service Path ID | Service Index |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* MD-type 1, four Context Header, 4-byte each.
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Mandatory Context Header |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Mandatory Context Header |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Mandatory Context Header |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Mandatory Context Header |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*
* It only supports MD-type 1 and MD-type 2 with 0 byte (No) Conetxt
* header and Ethernet for next protocol (rtnl_link_ops).
*
*/
struct nsh_base_hdr {
__u8 flags; /* Ver, O, C, Rx4 */
__u8 length; /* Rx2, Length*/
__u8 mdtype;
__u8 protocol;
};
#define NSH_BASE_CHECK_VERSION(f, v) (((f) & 0xC0) == v)
#define NSH_BASE_OAM(f) ((f) & 0x20)
#define NSH_BASE_CRITCAL(f) ((f) & 0x10)
#define NSH_BASE_LENGTH(l) ((l) & 0x3F)
#define NSH_BASE_MDTYPE1 0x01
#define NSH_BASE_MDTYPE2 0x02
#define NSH_BASE_PROTO_IPV4 0x01 /* XXX: not supported */
#define NSH_BASE_PROTO_IPV6 0x02 /* XXX: not supported */
#define NSH_BASE_PROTO_ETH 0x03
struct nsh_path_hdr {
__be32 spisi; /* SPI + SI */
};
struct nsh_ctx_type1 {
__be32 ctx[4];
};
struct nsh_vlm_hdr {
__be16 class;
__u8 type; /* 1st bit is C */
__u8 length; /* first 3 bits are reserved */
};
#define NSHKMOD_VERSION "0.0"
MODULE_VERSION(NSHKMOD_VERSION);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Ryo Nakamura <[email protected]>");
MODULE_DESCRIPTION("network service header kernel module implementation");
MODULE_ALIAS_RTNL_LINK("nsh");
#define VXLAN_HLEN (sizeof(struct vxlanhdr) + sizeof(struct udphdr))
#define NSH_MDTYPE1_HLEN (sizeof(struct nsh_base_hdr) + \
sizeof(struct nsh_path_hdr) + \
sizeof(struct nsh_ctx_type1))
#define NSH_MDTYPE2_0_HLEN (sizeof(struct nsh_base_hdr) + \
sizeof(struct nsh_path_hdr))
#define NSH_VXLAN_TTL 64
#define VXLAN_GPE_PORT htons(4790)
#define VXLAN_GPE_FLAGS 0x0C000000 /* set next protocol */
#define VXLAN_GPE_PROTO_IPV4 0x01
#define VXLAN_GPE_PROTO_IPV6 0x02
#define VXLAN_GPE_PROTO_ETH 0x03
#define VXLAN_GPE_PROTO_NSH 0x04
#define VXLAN_GPE_PROTO_MPLS 0x05
#define ETH_P_NSH 0x894F /* Cisco vPath Network Service Header
* draft-ietf-sfc-nsh-01 sec 9.3 */
#define DEFAULT_MTU 1500
static int nsh_net_id;
static u32 nshkmod_salt __read_mostly;
/* Pseudo network device */
struct nsh_dev {
struct list_head list; /* nsh_net->dev_list */
struct rcu_head rcu;
struct net_device *dev;
__be32 key; /* SPI and SI. 0 means not assigned */
};
/* remote node (next node of the path) infromation */
struct nsh_dst {
/* vxlan */
__be32 remote_ip; /* XXX: should support IPv6 */
__be32 local_ip; /* XXX: should support IPv6 */
__be32 vni; /* vni for vxlan encap */
/* ether */
struct net_device *lowerdev;
__u8 eth_addr[ETH_ALEN]; /* encap ehter */
};
/* nsh_table entry. SPI+SI -> Dst (dev or remote) */
struct nsh_table {
struct hlist_node hlist; /* linked list of hashtable */
struct rcu_head rcu;
struct net *net;
unsigned long updated; /* jiffies */
__be32 key; /* SPI and SI */
__be32 spi; /* service path index */
__u8 si; /* service index */
__u8 mdtype; /* MD-type */
__u8 encap_type;
struct nsh_dev *rdev;
struct nsh_dst *rdst;
/* XXX: nsh_net->nsh_table[n] should be locked when add or delete */
};
/* per net_namespace structure */
struct nsh_net {
struct net *net;
#define NSH_HASH_BITS 8
#define NSH_HASH_SIZE (1 << NSH_HASH_BITS)
struct hlist_head nsh_table[NSH_HASH_SIZE]; /* nsh_table hash */
struct list_head dev_list; /* nsh_dev list*/
struct socket *sock; /* udp tunnel socket */
};
static inline struct hlist_head *nsh_table_head(struct nsh_net *nnet,
__be32 key) {
return &nnet->nsh_table[hash_32(key, NSH_HASH_BITS)];
}
static struct nsh_table *nsh_find_table(struct nsh_net *nnet, __be32 key)
{
struct hlist_head *head = nsh_table_head(nnet, key);
struct nsh_table *nt;
hlist_for_each_entry_rcu(nt, head, hlist) {
if (key == nt->key)
return nt;
}
return NULL;
}
static int nsh_add_table(struct nsh_net *nnet, __be32 key, __u8 mdtype,
__u8 encap_type, struct nsh_dev *rdev,
struct nsh_dst *rdst)
{
struct nsh_table *nt;
nt = kmalloc(sizeof(*nt), GFP_KERNEL);
if (!nt)
return -ENOMEM;
memset(nt, 0, sizeof(*nt));
nt->net = nnet->net;
nt->key = key;
nt->spi = ntohl(key) >> 8;
nt->si = ntohl(key) & 0x000000FF;
nt->mdtype = mdtype;
nt->encap_type = encap_type;
nt->rdev = rdev;
nt->rdst = rdst; /* which one (rdev or rdst) must be NULL */
hlist_add_head_rcu(&nt->hlist, nsh_table_head(nnet, key));
return 0;
}
static void nsh_free_table(struct rcu_head *head)
{
struct nsh_table *nt = container_of(head, struct nsh_table, rcu);
kfree(nt->rdst);
kfree(nt);
}
static void nsh_delete_table(struct nsh_table *nt)
{
hlist_del_rcu(&nt->hlist);
call_rcu(&nt->rcu, nsh_free_table);
}
static void nsh_destroy_table(struct nsh_net *nnet)
{
unsigned int n;
for (n = 0; n < NSH_HASH_SIZE; n++) {
struct hlist_node *ptr, *tmp;
hlist_for_each_safe(ptr, tmp, &nnet->nsh_table[n]) {
struct nsh_table *nt;
nt = container_of(ptr, struct nsh_table, hlist);
nsh_delete_table(nt);
}
}
}
static netdev_tx_t nsh_xmit_one(struct sk_buff *skb, struct net *net,
struct net_device *dev, __be32 key);
static int nsh_recv(struct net *net, struct sk_buff *skb)
{
int hdrlen;
__be32 key;
struct nsh_base_hdr *nbh;
struct nsh_path_hdr *nph;
struct nsh_table *nt;
struct nsh_net *nnet = net_generic(net, nsh_net_id);
struct pcpu_sw_netstats *stats;
nbh = (struct nsh_base_hdr *)skb->data;
nph = (struct nsh_path_hdr *)(nbh + 1);
if (unlikely(!NSH_BASE_CHECK_VERSION(nbh->flags, 0))) {
pr_debug("invalid nsh version flag %#x\n", nbh->flags);
return -1;
}
if (NSH_BASE_OAM(nbh->flags)) {
pr_debug("oam is not supported %#x\n", nbh->flags);
return -1;
}
/* XXX: C bit should be considered on software? */
if (nbh->protocol != NSH_BASE_PROTO_ETH) {
pr_debug("only supports ethrenet. protocol is %u.\n",
nbh->protocol);
return -1;
}
if ((ntohl(nph->spisi) & 0x000000FF) == 0) {
/* service index 0 packet is dropped (draft 4.3) */
return -1;
}
key = nph->spisi;
hdrlen = NSH_BASE_LENGTH(nbh->length) << 2;
__skb_pull(skb, hdrlen);
skb->encapsulation = 0;
skb_reset_mac_header(skb);
skb_reset_network_header(skb);
/* find own nsh interfaces (like connected interface) */
nt = nsh_find_table(nnet, key);
if (nt && nt->rdev) {
skb->protocol = eth_type_trans(skb, nt->rdev->dev);
skb_scrub_packet(skb, !net_eq(net, dev_net(nt->rdev->dev)));
stats = this_cpu_ptr(nt->rdev->dev->tstats);
u64_stats_update_begin(&stats->syncp);
stats->rx_packets++;
stats->rx_bytes += skb->len;
u64_stats_update_end(&stats->syncp);
netif_rx(skb);
} else {
/* Forward NSH packet in nsh layer.
* decrease service index try to xmit the packet */
skb_scrub_packet(skb, false);
key = htonl (ntohl (key) - 1); /* decrease service index */
nsh_xmit_one(skb, net, NULL, key);
}
return 0;
}
static int nsh_vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
{
/* pop udp and vxlan header. checking spi si and forwarding to
* appropriate interface are done by nsh_recv (outer
* encapsulation protocol independent). */
struct vxlanhdr *vxh;
if (!pskb_may_pull(skb, VXLAN_HLEN))
goto err;
vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
if (vxh->vx_flags != htonl(VXLAN_GPE_FLAGS | VXLAN_GPE_PROTO_NSH)) {
netdev_dbg(skb->dev, "invalid vxlan flags %#x\n",
ntohl(vxh->vx_flags));
goto err;
}
__skb_pull(skb, VXLAN_HLEN);
return nsh_recv(sock_net(sk), skb);
err:
return 1;
}
static int nsh_ether_encap_recv(struct sk_buff *skb, struct net_device *dev,
struct packet_type *pt,
struct net_device *orig_dev)
{
int err;
if (skb->pkt_type == PACKET_OTHERHOST)
goto drop;
skb = skb_share_check(skb, GFP_ATOMIC);
if (skb == NULL) {
IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
goto out;
}
/* XXX: ???
if (!pskb_may_pull (skb, ETH_HLEN))
goto inhdr_error;
__skb_pull (skb, ETH_HLEN);
*/
err = nsh_recv(dev_net(dev), skb);
if (err < 0)
goto out;
return 1;
/*
inhdr_error:
IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INHDRERRORS);
*/
drop:
kfree_skb(skb);
out:
return NET_RX_DROP;
}
static int nsh_xmit_vxlan_skb(struct socket *sock, struct net * net,
struct rtable *rt, struct sk_buff *skb,
__be32 src, __be32 dst, __be16 src_port,
__be16 dst_port, __be32 vni)
{
int err;
struct vxlanhdr *vxh;
bool udp_sum = !sock->sk->sk_no_check_tx;
skb = udp_tunnel_handle_offloads(skb, udp_sum);
err = skb_cow_head(skb, VXLAN_HEADROOM);
if (unlikely(err)) {
kfree_skb(skb);
return -ENOMEM;
}
vxh = (struct vxlanhdr *)__skb_push(skb, sizeof(*vxh));
vxh->vx_flags = htonl(VXLAN_GPE_FLAGS | VXLAN_GPE_PROTO_NSH);
vxh->vx_vni = htonl(vni << 8);
return udp_tunnel_xmit_skb(sock, rt, skb, src, dst, 0, NSH_VXLAN_TTL,
0, src_port, dst_port, net);
}
static int nsh_xmit_vxlan(struct sk_buff *skb, struct net_device *dev,
struct nsh_net *nnet, struct nsh_table *nt,
__be16 src_port)
{
struct flowi4 fl4;
struct rtable *rt;
memset(&fl4, 0, sizeof(fl4));
fl4.daddr = nt->rdst->remote_ip;
fl4.saddr = nt->rdst->local_ip;
if (nt->rdst->lowerdev)
fl4.flowi4_oif = nt->rdst->lowerdev->ifindex;
rt = ip_route_output_key(nnet->net, &fl4);
if (IS_ERR(rt)) {
if (net_ratelimit ())
pr_debug ("no route found to %pI4\n",
(struct in_addr *)&nt->rdst->remote_ip);
if (dev) {
dev->stats.tx_carrier_errors++;
dev->stats.tx_dropped++;
}
return -ENOENT;
}
return nsh_xmit_vxlan_skb(nnet->sock, nnet->net, rt,
skb, fl4.saddr, nt->rdst->remote_ip,
src_port, VXLAN_GPE_PORT, nt->rdst->vni);
}
static int nsh_xmit_ether(struct sk_buff *skb, struct nsh_table *nt)
{
int err;
struct ethhdr *eth;
struct nsh_dst *dst;
err = skb_cow_head(skb, ETH_HLEN);
if (unlikely(err)) {
kfree_skb(skb);
return -ENOMEM;
}
if (!nt->rdst || !nt->rdst->lowerdev) {
pr_debug("%s: invalid link\n", __func__);
return -ENODEV;
}
dst = nt->rdst;
eth = (struct ethhdr *)__skb_push(skb, sizeof(*eth));
ether_addr_copy(eth->h_dest, dst->eth_addr);
ether_addr_copy(eth->h_source, dst->lowerdev->dev_addr);
eth->h_proto = htons(ETH_P_NSH);
skb->protocol = eth->h_proto;
skb->dev = nt->rdst->lowerdev;
skb_reset_mac_header(skb);
return dev_queue_xmit(skb);
}
static netdev_tx_t nsh_xmit_one(struct sk_buff *skb, struct net *net,
struct net_device *dev, __be32 key)
{
int rc;
unsigned int len, nhlen;
__be16 src_port = VXLAN_GPE_PORT;
struct pcpu_sw_netstats *tx_stats;
struct nsh_net *nnet = net_generic(net, nsh_net_id);
struct nsh_table *nt;
struct nsh_base_hdr *nbh;
struct nsh_path_hdr *nph;
struct nsh_ctx_type1 *ctx;
nt = nsh_find_table(nnet, key);
if (!nt) {
if (net_ratelimit())
pr_debug("path is not assigned for %x\n", ntohl (key));
goto tx_err;
}
len = skb->len;
switch (nt->mdtype) {
case NSH_BASE_MDTYPE1:
nhlen = NSH_MDTYPE1_HLEN;
break;
case NSH_BASE_MDTYPE2:
nhlen = NSH_MDTYPE2_0_HLEN;
break;
default:
pr_debug("invalid MD-type %u\n", nt->mdtype);
goto tx_err;
}
if (nt->encap_type == NSH_ENCAP_TYPE_VXLAN) {
/* get udp src port for ether hash before encapsulation.
* XXX: src_port_max and _min should be implemented.
* 0, 0, means default src port range. */
src_port = udp_flow_src_port(net, skb, 0, 0, true);
}
rc = skb_cow_head(skb, nhlen);
if (unlikely(rc)) {
pr_debug("failed to skb_cow_head\n");
kfree_skb(skb);
goto tx_err;
}
if (nt->mdtype == NSH_BASE_MDTYPE1) {
ctx = (struct nsh_ctx_type1 *)__skb_push(skb, sizeof(*ctx));
/* XXX: no metadata. how to implement other drafts? */
ctx->ctx[0] = 0;
ctx->ctx[1] = 0;
ctx->ctx[2] = 0;
ctx->ctx[3] = 0;
}
nph = (struct nsh_path_hdr *)__skb_push(skb, sizeof(*nph));
nph->spisi = key;
nbh = (struct nsh_base_hdr *)__skb_push(skb, sizeof(*nbh));
nbh->flags = 0;
nbh->length = nhlen >> 2; /* 4byte word */
nbh->mdtype = nt->mdtype;
nbh->protocol = NSH_BASE_PROTO_ETH;
if (nt->rdev) {
/* nexthop is nsh interface in this machine. */
nsh_recv(net, skb);
goto update_stats;
}
if (nt->rdst) {
switch (nt->encap_type) {
case NSH_ENCAP_TYPE_VXLAN:
rc = nsh_xmit_vxlan(skb, dev, nnet, nt, src_port);
if (rc < 0)
goto tx_err;
break;
case NSH_ENCAP_TYPE_ETHER:
rc = nsh_xmit_ether(skb, nt);
if (rc < 0)
goto tx_err;
break;
default:
pr_debug("invalid encap type %d\n",
nt->encap_type);
goto tx_err;
}
}
update_stats:
if (dev) {
tx_stats = this_cpu_ptr(dev->tstats);
u64_stats_update_begin(&tx_stats->syncp);
tx_stats->tx_packets++;
tx_stats->tx_bytes += len;
u64_stats_update_end(&tx_stats->syncp);
}
return NETDEV_TX_OK;
tx_err:
if (dev)
dev->stats.tx_errors++;
return NETDEV_TX_OK;
}
static netdev_tx_t nsh_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct nsh_dev *ndev = netdev_priv(dev);
return nsh_xmit_one (skb, dev_net (dev), dev, ndev->key);
}
/* setup stats when device is created */
static int nsh_init(struct net_device *dev)
{
dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
if (!dev->tstats)
return -ENOMEM;
return 0;
}
static void nsh_uninit(struct net_device *dev)
{
free_percpu(dev->tstats);
}
static int nsh_open(struct net_device *dev)
{
return 0;
}
static int nsh_stop(struct net_device *dev)
{
return 0;
}
static const struct net_device_ops nsh_netdev_ops = {
.ndo_init = nsh_init,
.ndo_uninit = nsh_uninit,
.ndo_open = nsh_open,
.ndo_stop = nsh_stop,
.ndo_start_xmit = nsh_xmit,
.ndo_get_stats64 = ip_tunnel_get_stats64,
.ndo_change_mtu = eth_change_mtu,
.ndo_validate_addr = eth_validate_addr,
.ndo_set_mac_address = eth_mac_addr,
};
/* info for netdev */
static struct device_type nsh_type = {
.name = "nsh",
};
/* initialize the device structure */
static void nsh_setup(struct net_device *dev)
{
struct nsh_dev *ndev = netdev_priv(dev);
eth_hw_addr_random(dev);
ether_setup(dev);
dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM + NSH_MDTYPE1_HLEN;
dev->netdev_ops = &nsh_netdev_ops;
dev->destructor = free_netdev;
SET_NETDEV_DEVTYPE(dev, &nsh_type);
dev->tx_queue_len = 0;
dev->features |= NETIF_F_LLTX;
dev->features |= NETIF_F_NETNS_LOCAL;
netif_keep_dst(dev);
dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
INIT_LIST_HEAD(&ndev->list);
ndev->dev = dev;
ndev->key = 0;
}
static int nsh_newlink(struct net *net, struct net_device *dev,
struct nlattr *tb[], struct nlattr *data[])
{
int err;
__u32 spi;
__u8 si;
struct nsh_net *nnet = net_generic(net, nsh_net_id);
struct nsh_dev *ndev = netdev_priv(dev);
if (data && data[IFLA_NSHKMOD_SPI] && data[IFLA_NSHKMOD_SI]) {
spi = nla_get_u32(data[IFLA_NSHKMOD_SPI]);
si = nla_get_u8(data[IFLA_NSHKMOD_SI]);
ndev->key = htonl((spi << 8) | si);
}
err = register_netdevice(dev);
if (err) {
netdev_err(dev, "failed to register netdevice\n");
return err;
}
list_add_tail_rcu(&ndev->list, &nnet->dev_list);
return 0;
}
static void nsh_dellink(struct net_device *dev, struct list_head *head)
{
unsigned int n;
struct nsh_dev *ndev = netdev_priv(dev);
struct nsh_net *nnet = net_generic(dev_net(dev), nsh_net_id);
/* remove this device from nsh table */
for (n = 0; n < NSH_HASH_SIZE; n++) {
struct nsh_table *nt;
struct hlist_node *ptr, *tmp;
hlist_for_each_safe(ptr, tmp, &nnet->nsh_table[n]) {
nt = container_of(ptr, struct nsh_table, hlist);
if (nt->rdev == ndev)
nsh_delete_table(nt);
}
}
list_del_rcu(&ndev->list);
unregister_netdevice_queue(dev, head);
}
static size_t nsh_get_size(const struct net_device *dev)
{
return nla_total_size(sizeof(__u32)) + /* IFLA_NSHKMOD_SPI */
nla_total_size(sizeof(__u8)) + /* IFLA_NSHKMOD_SI */
0;
}
static int nsh_fill_info(struct sk_buff *skb, const struct net_device *dev)
{
__u8 si;
__u32 spi;
const struct nsh_dev *ndev = netdev_priv(dev);
spi = ntohl(ndev->key) >> 8;
si = ntohl(ndev->key) & 0x00000FF;
if (nla_put_u32(skb, IFLA_NSHKMOD_SPI, spi) ||
nla_put_u8(skb, IFLA_NSHKMOD_SI, si))
return -EMSGSIZE;
return 0;
}
static struct rtnl_link_ops nshkmod_link_ops __read_mostly = {
.kind = "nsh",
.maxtype = IFLA_NSHKMOD_MAX,
.priv_size = sizeof(struct nsh_dev),
.setup = nsh_setup,
.newlink = nsh_newlink,
.dellink = nsh_dellink,
.get_size = nsh_get_size,
.fill_info = nsh_fill_info,
};
static struct packet_type nshkmod_packet_type __read_mostly = {
.type = cpu_to_be16 (ETH_P_NSH),
.func = nsh_ether_encap_recv,
};
static void nsh_handle_lowerdev_unregister(struct nsh_net *nnet,
struct net_device *dev)
{
/* check ENCAP_TYPE_ETHER rdst */
unsigned int n;
for (n = 0; n < NSH_HASH_SIZE; n++) {
struct hlist_node *ptr, *tmp;
hlist_for_each_safe(ptr, tmp, &nnet->nsh_table[n]) {
struct nsh_table *nt;
nt = container_of(ptr, struct nsh_table, hlist);
if (nt->rdst && nt->rdst->lowerdev == dev)
nsh_delete_table(nt);
}
}
}
static int nsh_lowerdev_event(struct notifier_block *unused,
unsigned long event, void *ptr)
{
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
struct nsh_net *nnet = net_generic(dev_net(dev), nsh_net_id);
if (event == NETDEV_UNREGISTER)
nsh_handle_lowerdev_unregister(nnet, dev);
return NOTIFY_DONE;
}
static struct notifier_block nshkmod_notifier_block __read_mostly = {
.notifier_call = nsh_lowerdev_event,
};
static struct socket *nsh_vxlan_create_sock(struct net *net, __be16 port)
{
/* XXX: vxlan_skb_xmit does not have API to configure flags in
* vxlan header (kernel 3.19) that is needed for VXLAN-GPE.
* So, normal udp socket and udp_tunnel_xmit are used. */
int err;
struct socket *sock;
struct udp_port_cfg udp_conf;
struct udp_tunnel_sock_cfg tunnel_cfg;
memset(&udp_conf, 0, sizeof(udp_conf));
/* XXX: should support IPv6 */
udp_conf.family = AF_INET;
udp_conf.local_ip.s_addr = INADDR_ANY;
udp_conf.local_udp_port = port;
err = udp_sock_create(net, &udp_conf, &sock);
if (err < 0)
return ERR_PTR(err);
tunnel_cfg.encap_type = 1;
tunnel_cfg.encap_rcv = nsh_vxlan_udp_encap_recv;
tunnel_cfg.encap_destroy = NULL;
setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
return sock;
}
static __net_init int nshkmod_init_net(struct net *net)
{
unsigned int n;
struct nsh_net *nnet = net_generic(net, nsh_net_id);
for (n = 0; n < NSH_HASH_SIZE; n++)
INIT_HLIST_HEAD(&nnet->nsh_table[n]);
INIT_LIST_HEAD(&nnet->dev_list);
nnet->net = net;
nnet->sock = nsh_vxlan_create_sock(net, VXLAN_GPE_PORT);
if (IS_ERR(nnet->sock)) {
pr_err("nshkmod: failed to add vxlan udp socket\n");
return -EINVAL;
}
return 0;
}
static void __net_exit nshkmod_exit_net(struct net *net)
{
struct nsh_net *nnet = net_generic(net, nsh_net_id);
struct nsh_dev *ndev, *next;
struct net_device *dev, *aux;
LIST_HEAD(list);
rtnl_lock();
for_each_netdev_safe(net, dev, aux)
if (dev->rtnl_link_ops == &nshkmod_link_ops)
unregister_netdevice_queue(dev, &list);
list_for_each_entry_safe(ndev, next, &nnet->dev_list, list) {
if (!net_eq(dev_net(ndev->dev), net))
unregister_netdevice_queue(ndev->dev, &list);
}
unregister_netdevice_many(&list);
rtnl_unlock();
nsh_destroy_table(nnet);
udp_tunnel_sock_release(nnet->sock);
}
static struct pernet_operations nshkmod_net_ops = {
.init = nshkmod_init_net,
.exit = nshkmod_exit_net,
.id = &nsh_net_id,
.size = sizeof(struct nsh_net),
};
static struct genl_family nshkmod_nl_family = {
.id = GENL_ID_GENERATE,
.name = NSHKMOD_GENL_NAME,
.version = NSHKMOD_GENL_VERSION,
.maxattr = NSHKMOD_ATTR_MAX,
.hdrsize = 0,
};
static struct nla_policy nshkmod_nl_policy[NSHKMOD_ATTR_MAX + 1] = {
[NSHKMOD_ATTR_IFINDEX] = { .type = NLA_U32, },
[NSHKMOD_ATTR_SPI] = { .type = NLA_U32, },
[NSHKMOD_ATTR_SI] = { .type = NLA_U8, },
[NSHKMOD_ATTR_ENCAP] = { .type = NLA_U8, },
[NSHKMOD_ATTR_REMOTE] = { .type = NLA_U32, },
[NSHKMOD_ATTR_LOCAL] = { .type = NLA_U32, },
[NSHKMOD_ATTR_VNI] = { .type = NLA_U32, },
[NSHKMOD_ATTR_ETHADDR] = { .type = NLA_BINARY,
.len = ETH_ALEN },
};
static int nsh_nl_cmd_path_dst_set(struct sk_buff *skb,
struct genl_info *info)
{
/* set a path->remote or device mapping */
u8 si, encap_type, mdtype, eth_addr[ETH_ALEN];
__u32 spi, ifindex, vni, key;
__be32 remote, local;
struct net *net = sock_net(skb->sk);
struct nsh_net *nnet = net_generic(net, nsh_net_id);
struct nsh_table *nt;
struct nsh_dst *dst;
struct net_device *dev, *lowerdev;
if (!info->attrs[NSHKMOD_ATTR_SPI] || !info->attrs[NSHKMOD_ATTR_SI])
return -EINVAL;
spi = nla_get_u32(info->attrs[NSHKMOD_ATTR_SPI]);
si = nla_get_u8(info->attrs[NSHKMOD_ATTR_SI]);
key = htonl((spi << 8) | si);
nt = nsh_find_table(nnet, key);
if (nt)
return -EEXIST;
mdtype = (info->attrs[NSHKMOD_ATTR_MDTYPE]) ?
nla_get_u8(info->attrs[NSHKMOD_ATTR_MDTYPE]) :
NSH_BASE_MDTYPE1;
encap_type = 0;
ifindex = 0;
remote = 0;
local = 0;
vni = 0;
memset(eth_addr, 0, ETH_ALEN);
if (!info->attrs[NSHKMOD_ATTR_ENCAP])
encap_type = NSH_ENCAP_TYPE_NONE;
else
encap_type = nla_get_u8(info->attrs[NSHKMOD_ATTR_ENCAP]);
switch (encap_type) {
case NSH_ENCAP_TYPE_NONE: /* inner device */
if (!info->attrs[NSHKMOD_ATTR_IFINDEX])
return -EINVAL;
ifindex = nla_get_u32(info->attrs[NSHKMOD_ATTR_IFINDEX]);
dev = __dev_get_by_index(net, ifindex);
if (!dev) {
pr_debug("device for index %u does not exist\n",
ifindex);
return -ENODEV;
}
if (dev->netdev_ops != &nsh_netdev_ops) {
pr_debug("%s is not nsh interface\n", dev->name);
return -EINVAL;
}
nsh_add_table(nnet, key, mdtype, encap_type,
netdev_priv(dev), NULL);
break;
case NSH_ENCAP_TYPE_VXLAN:
if (!info->attrs[NSHKMOD_ATTR_ENCAP] ||
!info->attrs[NSHKMOD_ATTR_REMOTE])
return -EINVAL;
remote = nla_get_be32(info->attrs[NSHKMOD_ATTR_REMOTE]);
if (info->attrs[NSHKMOD_ATTR_LOCAL])
local = nla_get_be32(info->attrs[NSHKMOD_ATTR_LOCAL]);
if (info->attrs[NSHKMOD_ATTR_VNI])
vni = nla_get_u32(info->attrs[NSHKMOD_ATTR_VNI]);
dst = kmalloc(sizeof(*dst), GFP_KERNEL);
if (!dst)
return -ENOMEM;
dst->remote_ip = remote;
dst->local_ip = local;
dst->vni = vni;
if (info->attrs[NSHKMOD_ATTR_IFINDEX]) {
ifindex =
nla_get_u32(info->attrs[NSHKMOD_ATTR_IFINDEX]);
lowerdev = __dev_get_by_index (net, ifindex);
if (!lowerdev) {
pr_debug ("ifindex %d does not exist\n",
ifindex);
return -ENODEV;
}
if (lowerdev->netdev_ops == &nsh_netdev_ops) {
pr_debug("nsh device can't become "
"link of a path\n");
return -EINVAL;
}
dst->lowerdev = lowerdev;
}
nsh_add_table(nnet, key, mdtype, encap_type, NULL, dst);
break;
case NSH_ENCAP_TYPE_ETHER:
if (!info->attrs[NSHKMOD_ATTR_IFINDEX] ||
!info->attrs[NSHKMOD_ATTR_ETHADDR])
return -EINVAL;
ifindex = nla_get_u32(info->attrs[NSHKMOD_ATTR_IFINDEX]);
nla_memcpy(eth_addr, info->attrs[NSHKMOD_ATTR_ETHADDR],
ETH_ALEN);
lowerdev = __dev_get_by_index(net, ifindex);
if (!lowerdev) {
pr_debug("ifindex %d does not exist\n", ifindex);
return -ENODEV;
}
if (lowerdev->netdev_ops == &nsh_netdev_ops) {
pr_debug("nsh device can't become link of a path\n");
return -EINVAL;
}