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
|
#include <linux/capability.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/in.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/if_arp.h>
#include <linux/mroute.h>
#include <linux/init.h>
#include <linux/if_ether.h>
#include <linux/semaphore.h>
#include <linux/rbtree.h>
#include <linux/version.h>
#include <net/genetlink.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
#include "ipoe.h"
#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
#define HASH_SIZE 16
#define NEED_UPDATE 1
#define UPDATE 2
#ifndef DEFINE_SEMAPHORE
#define DEFINE_SEMAPHORE(name) struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
#endif
struct ipoe_session
{
struct rb_node node;
__be32 addr;
__be32 peer_addr;
__u8 hwaddr[ETH_ALEN];
struct net_device *dev;
struct net_device *link_dev;
/*struct tasklet_struct tasklet;
int tasklet_pending;
struct u64_status_sync rsync;
u64 rx_packets;
u64 rx_bytes;
struct sk_buff_head rq;
struct u64_status_sync tsync;
u64 tx_packets;
u64 tx_bytes;
struct sk_buff_head tq;*/
int drop:1;
};
struct rb_root ipoe_rbt = RB_ROOT;
static atomic_t ipoe_rlock;
static atomic_t ipoe_update;
static DEFINE_SEMAPHORE(ipoe_wlock);
struct sk_buff_head ipoe_rq;
struct tasklet_struct ipoe_rq_tasklet;
static struct ipoe_session *ipoe_lookup(__be32 addr, struct rb_node **r_parent, struct rb_node ***r_p);
static int ipoe_do_nat(struct sk_buff *skb, __be32 new_addr, int to_peer);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
static const struct net_device_ops ipoe_netdev_ops;
#endif
static struct genl_family ipoe_nl_family;
static void ipoe_recv_rq(unsigned long arg)
{
struct sk_buff *skb;
int upd;
struct iphdr *iph;
int noff;
struct ipoe_session *ses;
atomic_inc(&ipoe_rlock);
if (atomic_read(&ipoe_update) == UPDATE) {
atomic_dec(&ipoe_rlock);
tasklet_schedule(&ipoe_rq_tasklet);
return;
}
while ((skb = skb_dequeue(&ipoe_rq))) {
noff = skb_network_offset(skb);
iph = ip_hdr(skb);
ses = ipoe_lookup(iph->saddr, NULL, NULL);
if (!ses)
goto drop;
if (ses->drop)
goto drop;
if (ses->addr && ipoe_do_nat(skb, ses->addr, 0))
goto drop;
skb->dev = ses->dev;
//skb->skb_iif = ses->link_dev->ifindex;
netif_rx(skb);
if (atomic_read(&ipoe_update) == NEED_UPDATE)
break;
continue;
drop:
kfree_skb(skb);
}
upd = atomic_read(&ipoe_update);
if (atomic_dec_and_test(&ipoe_rlock) && upd == NEED_UPDATE)
atomic_set(&ipoe_update, UPDATE);
}
static int ipoe_do_nat(struct sk_buff *skb, __be32 new_addr, int to_peer)
{
struct iphdr *iph;
int noff;
int ihl;
__be32 addr;
noff = skb_network_offset(skb);
iph = ip_hdr(skb);
if (to_peer)
addr = iph->daddr;
else
addr = iph->saddr;
if (skb_cloned(skb) &&
!skb_clone_writable(skb, sizeof(*iph) + noff) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
return -1;
iph = ip_hdr(skb);
if (to_peer)
iph->daddr = new_addr;
else
iph->saddr = new_addr;
csum_replace4(&iph->check, addr, new_addr);
ihl = iph->ihl * 4;
switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
case IPPROTO_TCP:
{
struct tcphdr *tcph;
if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
(skb_cloned(skb) &&
!skb_clone_writable(skb, ihl + sizeof(*tcph) + noff) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
return -1;
tcph = (void *)(skb_network_header(skb) + ihl);
inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, 1);
break;
}
case IPPROTO_UDP:
{
struct udphdr *udph;
if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
(skb_cloned(skb) &&
!skb_clone_writable(skb, ihl + sizeof(*udph) + noff) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
return -1;
udph = (void *)(skb_network_header(skb) + ihl);
if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
inet_proto_csum_replace4(&udph->check, skb, addr, new_addr, 1);
if (!udph->check)
udph->check = CSUM_MANGLED_0;
}
break;
}
case IPPROTO_ICMP:
{
struct icmphdr *icmph;
if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
return -1;
icmph = (void *)(skb_network_header(skb) + ihl);
if ((icmph->type != ICMP_DEST_UNREACH) &&
(icmph->type != ICMP_TIME_EXCEEDED) &&
(icmph->type != ICMP_PARAMETERPROB))
break;
if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
noff))
return -1;
icmph = (void *)(skb_network_header(skb) + ihl);
iph = (void *)(icmph + 1);
if (skb_cloned(skb) &&
!skb_clone_writable(skb, ihl + sizeof(*icmph) +
sizeof(*iph) + noff) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
return -1;
icmph = (void *)(skb_network_header(skb) + ihl);
iph = (void *)(icmph + 1);
if (to_peer)
iph->saddr = new_addr;
else
iph->daddr = new_addr;
inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr, 0);
break;
}
default:
break;
}
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
static int ipoe_xmit(struct sk_buff *skb, struct net_device *dev)
#else
static netdev_tx_t ipoe_xmit(struct sk_buff *skb, struct net_device *dev)
#endif
{
struct ipoe_session *ses = netdev_priv(dev);
struct net_device_stats *stats = &dev->stats;
struct iphdr *iph;
struct ethhdr *eth;
/*struct arphdr *arp;
unsigned char *arp_ptr;
__be32 tip;*/
int noff;
noff = skb_network_offset(skb);
if (skb->protocol == htons(ETH_P_IP)) {
if (!pskb_may_pull(skb, sizeof(*iph) + noff))
goto drop;
iph = ip_hdr(skb);
//pr_info("ipoe: xmit %08x %08x\n", iph->saddr, iph->daddr);
/*u64_stats_update_begin(&ses->tsync);
ses->tx_packets++;
ses->tx_bytes += skb->len;
u64_stats_update_end(&ses->tsync);*/
if (iph->daddr == ses->addr) {
if (ipoe_do_nat(skb, ses->peer_addr, 1))
goto drop;
eth = (struct ethhdr *)skb->data;
memcpy(eth->h_dest, ses->hwaddr, ETH_ALEN);
memcpy(eth->h_source, ses->link_dev->dev_addr, ETH_ALEN);
}
} /*else if (skb->protocol == htons(ETH_P_ARP)) {
if (!pskb_may_pull(skb, arp_hdr_len(dev) + noff))
goto drop;
arp = arp_hdr(skb);
arp_ptr = (unsigned char *)(arp + 1);
if (arp->ar_op == htons(ARPOP_REQUEST)) {
memcpy(&tip, arp_ptr + ETH_ALEN + 4 + ETH_ALEN, 4);
if (tip == ses->addr) {
if (skb_cloned(skb) &&
!skb_clone_writable(skb, arp_hdr_len(dev) + noff) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
goto drop;
arp = arp_hdr(skb);
arp_ptr = (unsigned char *)(arp + 1);
memcpy(arp_ptr + ETH_ALEN + 4 + ETH_ALEN, &ses->peer_addr, 4);
}
}
}*/
skb->dev = ses->link_dev;
skb->skb_iif = dev->ifindex;
dev_queue_xmit(skb);
return NETDEV_TX_OK;
drop:
stats->tx_dropped++;
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}
static int ipoe_rcv_arp(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
struct ipoe_session *ses = NULL;
struct arphdr *arp;
unsigned char *arp_ptr;
int noff;
__be32 sip;
int upd;
struct sk_buff *skb1;
//pr_info("ipoe: recv arp\n");
if (skb->pkt_type == PACKET_OTHERHOST)
goto drop;
noff = skb_network_offset(skb);
if (!pskb_may_pull(skb, arp_hdr_len(dev) + noff))
goto drop;
arp = arp_hdr(skb);
arp_ptr = (unsigned char *)(arp + 1);
if (arp->ar_pro != htons(ETH_P_IP))
goto drop;
memcpy(&sip, arp_ptr + ETH_ALEN, 4);
//pr_info("ipoe: recv arp %08x\n", sip);
atomic_inc(&ipoe_rlock);
upd = atomic_read(&ipoe_update);
if (upd)
goto drop_unlock;
ses = ipoe_lookup(sip, NULL, NULL);
if (!ses)
goto drop_unlock;
if (ses->drop || ses->addr)
goto drop_unlock;
if (skb->dev == ses->dev)
goto drop_unlock;
/*if (ses->addr && arp->ar_op == htons(ARPOP_REPLY)) {
if (skb_cloned(skb) &&
!skb_clone_writable(skb, arp_hdr_len(dev) + noff) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
goto drop_unlock;
arp = arp_hdr(skb);
arp_ptr = (unsigned char *)(arp + 1);
memcpy(arp_ptr + ETH_ALEN, &ses->addr, 4);
}*/
skb1 = skb_clone(skb, GFP_ATOMIC);
if (!skb1)
goto drop_unlock;
skb1->dev = ses->dev;
skb1->skb_iif = ses->dev->ifindex;
netif_rx(skb1);
drop_unlock:
upd = atomic_read(&ipoe_update);
if (atomic_dec_and_test(&ipoe_rlock) && upd == NEED_UPDATE)
atomic_set(&ipoe_update, UPDATE);
drop:
kfree_skb(skb);
return NET_RX_DROP;
}
static int ipoe_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
struct ipoe_session *ses = NULL;
struct iphdr *iph;
int upd;
int noff;
struct sk_buff *skb1;
if (skb->pkt_type == PACKET_OTHERHOST)
goto drop;
noff = skb_network_offset(skb);
if (!pskb_may_pull(skb, sizeof(*iph) + noff))
goto drop;
iph = ip_hdr(skb);
//pr_info("ipoe: recv %08x %08x\n", iph->saddr, iph->daddr);
atomic_inc(&ipoe_rlock);
upd = atomic_read(&ipoe_update);
if (upd == NEED_UPDATE) {
skb_queue_tail(&ipoe_rq, skb);
if (atomic_dec_and_test(&ipoe_rlock))
atomic_set(&ipoe_update, UPDATE);
return NET_RX_SUCCESS;
} else if (upd == UPDATE) {
skb_queue_tail(&ipoe_rq, skb);
atomic_dec(&ipoe_rlock);
return NET_RX_SUCCESS;
}
ses = ipoe_lookup(iph->saddr, NULL, NULL);
if (!ses)
goto drop_unlock;
if (ses->drop)
goto drop_unlock;
if (skb->dev == ses->dev)
goto drop_unlock;
if (ses->addr && ipoe_do_nat(skb, ses->addr, 0))
goto drop_unlock;
skb1 = skb_clone(skb, GFP_ATOMIC);
if (!skb1)
goto drop_unlock;
skb1->dev = ses->dev;
skb1->skb_iif = ses->dev->ifindex;
netif_rx(skb1);
drop_unlock:
upd = atomic_read(&ipoe_update);
if (atomic_dec_and_test(&ipoe_rlock) && upd == NEED_UPDATE)
atomic_set(&ipoe_update, UPDATE);
drop:
kfree_skb(skb);
return NET_RX_DROP;
}
static int ipoe_hard_header(struct sk_buff *skb, struct net_device *dev,
unsigned short type, const void *daddr,
const void *saddr, unsigned len)
{
const struct ipoe_session *ses = netdev_priv(dev);
return dev_hard_header(skb, ses->link_dev, type, daddr,
saddr, len);
}
static const struct header_ops ipoe_hard_header_ops = {
.create = ipoe_hard_header,
.rebuild = eth_rebuild_header,
.parse = eth_header_parse,
.cache = eth_header_cache,
.cache_update = eth_header_cache_update,
};
static void ipoe_netdev_setup(struct net_device *dev)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
dev->hard_start_xmit = ipoe_xmit;
#else
dev->netdev_ops = &ipoe_netdev_ops;
#endif
dev->destructor = free_netdev;
dev->type = ARPHRD_ETHER;
dev->hard_header_len = 0;
dev->mtu = ETH_DATA_LEN;
dev->flags = 0;//IFF_NOARP | IFF_BROADCAST;
dev->iflink = 0;
dev->addr_len = ETH_ALEN;
dev->features = 0;//|= NETIF_F_NETNS_LOCAL;
dev->header_ops = &ipoe_hard_header_ops,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
#endif
}
static int ipoe_create(__be32 peer_addr, __be32 addr, const char *link_ifname, const __u8 *hwaddr)
{
struct ipoe_session *ses;
struct net_device *dev, *link_dev;
char name[IFNAMSIZ];
int r = 0;
struct rb_node **p;
struct rb_node *parent;
link_dev = dev_get_by_name(&init_net, link_ifname);
if (!link_dev)
return -EINVAL;
sprintf(name, "%s.ipoe%%d", link_ifname);
dev = alloc_netdev(sizeof(*ses), name, ipoe_netdev_setup);
if (dev == NULL)
goto failed;
dev_net_set(dev, &init_net);
r = dev_alloc_name(dev, name);
if (r < 0)
goto failed_free;
ses = netdev_priv(dev);
ses->dev = dev;
ses->addr = addr;
ses->peer_addr = peer_addr;
ses->link_dev = link_dev;
memcpy(ses->hwaddr, hwaddr, ETH_ALEN);
dev->features = link_dev->features;
memcpy(dev->dev_addr, link_dev->dev_addr, ETH_ALEN);
memcpy(dev->broadcast, link_dev->broadcast, ETH_ALEN);
if (addr)
dev->flags = IFF_NOARP;
else
dev->flags = IFF_BROADCAST;
rtnl_lock();
r = register_netdevice(dev);
rtnl_unlock();
if (r < 0)
goto failed_free;
down(&ipoe_wlock);
atomic_inc(&ipoe_rlock);
atomic_set(&ipoe_update, NEED_UPDATE);
if (atomic_dec_and_test(&ipoe_rlock))
atomic_set(&ipoe_update, UPDATE);
else {
while (atomic_read(&ipoe_update) != UPDATE)
schedule_timeout_uninterruptible(1);
}
if (ipoe_lookup(peer_addr, &parent, &p))
r = -EEXIST;
else {
rb_link_node(&ses->node, parent, p);
rb_insert_color(&ses->node, &ipoe_rbt);
}
up(&ipoe_wlock);
atomic_set(&ipoe_update, 0);
tasklet_schedule(&ipoe_rq_tasklet);
return r;
failed_free:
free_netdev(dev);
failed:
dev_put(link_dev);
return r;
}
static struct ipoe_session *ipoe_lookup(__be32 addr, struct rb_node **r_parent, struct rb_node ***r_p)
{
struct ipoe_session *ses;
struct rb_node **p = &ipoe_rbt.rb_node;
struct rb_node *parent = NULL;
while (*p) {
parent = *p;
ses = rb_entry(parent, typeof(*ses), node);
if (addr < ses->peer_addr)
p = &(*p)->rb_left;
else if (addr > ses->peer_addr)
p = &(*p)->rb_right;
else
return ses;
}
if (r_parent) {
*r_parent = parent;
*r_p = p;
}
return NULL;
}
static int ipoe_delete(__be32 addr)
{
struct ipoe_session *ses;
down(&ipoe_wlock);
atomic_inc(&ipoe_rlock);
atomic_set(&ipoe_update, NEED_UPDATE);
if (atomic_dec_and_test(&ipoe_rlock))
atomic_set(&ipoe_update, UPDATE);
else {
while (atomic_read(&ipoe_update) != UPDATE)
schedule_timeout_uninterruptible(1);
}
ses = ipoe_lookup(addr, NULL, NULL);
if (ses)
rb_erase(&ses->node, &ipoe_rbt);
up(&ipoe_wlock);
atomic_set(&ipoe_update, 0);
tasklet_schedule(&ipoe_rq_tasklet);
if (!ses)
return -EINVAL;
dev_put(ses->link_dev);
unregister_netdev(ses->dev);
return 0;
}
static int ipoe_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
{
struct sk_buff *msg;
void *hdr;
int ret = -ENOBUFS;
msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (!msg) {
ret = -ENOMEM;
goto out;
}
hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
&ipoe_nl_family, 0, IPOE_CMD_NOOP);
if (IS_ERR(hdr)) {
ret = PTR_ERR(hdr);
goto err_out;
}
genlmsg_end(msg, hdr);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
return genlmsg_unicast(msg, info->snd_pid);
#else
return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
#endif
err_out:
nlmsg_free(msg);
out:
return ret;
}
static int ipoe_nl_cmd_create(struct sk_buff *skb, struct genl_info *info)
{
__be32 peer_addr, addr = 0;
int ret = 0;
char ifname[IFNAMSIZ];
__u8 hwaddr[ETH_ALEN];
//struct net *net = genl_info_net(info);
if (!info->attrs[IPOE_ATTR_PEER_ADDR] || !info->attrs[IPOE_ATTR_IFNAME]) {
ret = -EINVAL;
goto out;
}
peer_addr = nla_get_be32(info->attrs[IPOE_ATTR_PEER_ADDR]);
if (info->attrs[IPOE_ATTR_ADDR])
addr = nla_get_be32(info->attrs[IPOE_ATTR_ADDR]);
nla_strlcpy(ifname, info->attrs[IPOE_ATTR_IFNAME], IFNAMSIZ - 1);
if (info->attrs[IPOE_ATTR_HWADDR])
nla_memcpy(hwaddr, info->attrs[IPOE_ATTR_HWADDR], ETH_ALEN);
else
memset(hwaddr, 0, sizeof(hwaddr));
pr_info("ipoe: create %08x %08x %s\n", peer_addr, addr, ifname);
ret = ipoe_create(peer_addr, addr, ifname, hwaddr);
out:
return ret;
}
static int ipoe_nl_cmd_delete(struct sk_buff *skb, struct genl_info *info)
{
__be32 addr;
//struct net *net = genl_info_net(info);
if (!info->attrs[IPOE_ATTR_PEER_ADDR])
return -EINVAL;
addr = nla_get_u32(info->attrs[IPOE_ATTR_PEER_ADDR]);
pr_info("ipoe: delete %08x\n", addr);
return ipoe_delete(addr);
}
static struct nla_policy ipoe_nl_policy[IPOE_ATTR_MAX + 1] = {
[IPOE_ATTR_NONE] = { .type = NLA_UNSPEC, },
[IPOE_ATTR_ADDR] = { .type = NLA_U32, },
[IPOE_ATTR_PEER_ADDR] = { .type = NLA_U32, },
[IPOE_ATTR_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
[IPOE_ATTR_HWADDR] = { .type = NLA_U64 },
};
static struct genl_ops ipoe_nl_ops[] = {
{
.cmd = IPOE_CMD_NOOP,
.doit = ipoe_nl_cmd_noop,
.policy = ipoe_nl_policy,
/* can be retrieved by unprivileged users */
},
{
.cmd = IPOE_CMD_CREATE,
.doit = ipoe_nl_cmd_create,
.policy = ipoe_nl_policy,
.flags = GENL_ADMIN_PERM,
},
{
.cmd = IPOE_CMD_DELETE,
.doit = ipoe_nl_cmd_delete,
.policy = ipoe_nl_policy,
.flags = GENL_ADMIN_PERM,
},
};
static struct genl_family ipoe_nl_family = {
.id = GENL_ID_GENERATE,
.name = IPOE_GENL_NAME,
.version = IPOE_GENL_VERSION,
.hdrsize = 0,
.maxattr = IPOE_ATTR_MAX,
};
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
static const struct net_device_ops ipoe_netdev_ops = {
.ndo_start_xmit = ipoe_xmit,
};
#endif
static struct packet_type ip_packet_type = {
.type = __constant_htons(ETH_P_IP),
.func = ipoe_rcv,
};
static struct packet_type arp_packet_type = {
.type = __constant_htons(ETH_P_ARP),
.func = ipoe_rcv_arp,
};
/*static struct pernet_operations ipoe_net_ops = {
.init = ipoe_init_net,
.exit = ipoe_exit_net,
.id = &ipoe_net_id,
.size = sizeof(struct ipoe_net),
};*/
static int __init ipoe_init(void)
{
int err;
printk("IPoE session driver v0.1\n");
/*err = register_pernet_device(&ipoe_net_ops);
if (err < 0)
return err;*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
err = genl_register_family(&ipoe_nl_family);
if (err < 0) {
printk(KERN_INFO "ipoe: can't register netlink interface\n");
goto out;
}
for (i = 0; i < ARRAY_SIZE(ipoe_nl_ops); i++) {
err = genl_register_ops(&ipoe_nl_family, &ipoe_nl_ops[i]);
if (err)
break;
}
if (err < 0) {
printk(KERN_INFO "ipoe: can't register netlink interface\n");
goto out_unreg;
}
#else
err = genl_register_family_with_ops(&ipoe_nl_family, ipoe_nl_ops,
ARRAY_SIZE(ipoe_nl_ops));
#endif
if (err < 0) {
printk(KERN_INFO "ipoe: can't register netlink interface\n");
goto out;
}
tasklet_init(&ipoe_rq_tasklet, ipoe_recv_rq, 0);
skb_queue_head_init(&ipoe_rq);
dev_add_pack(&ip_packet_type);
dev_add_pack(&arp_packet_type);
return 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
out_unreg:
#endif
genl_unregister_family(&ipoe_nl_family);
out:
return err;
}
static void __exit ipoe_fini(void)
{
dev_remove_pack(&ip_packet_type);
dev_remove_pack(&arp_packet_type);
genl_unregister_family(&ipoe_nl_family);
}
module_init(ipoe_init);
module_exit(ipoe_fini);
MODULE_LICENSE("GPL");
|