linux内核netfilter实现url重定向

时间:2015-09-09 16:34:05   收藏:0   阅读:5396

url_redirect.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
#include <linux init.h="">
 
#include <linux types.h="">
 
#include <linux netdevice.h="">
 
#include <linux skbuff.h="">
 
#include <linux ip.h="">
 
#include <linux udp.h="">
 
#include <linux tcp.h="">
 
#include <net tcp.h="">
 
#include <linux netfilter_ipv4.h="">
 
#include <linux netfilter_bridge.h="">
 
#include "url_redirect.h"
 
  
 
struct sk_buff* tcp_newpack( u32 saddr, u32 daddr,
 
        u16 sport, u16 dport,
 
        u32 seq, u32 ack_seq,
 
        u8 *msg, int len );
 
         
 
int _tcp_send_pack( struct sk_buff *skb, struct iphdr *iph,
 
        struct tcphdr *th, gbuffer_t *p );
 
         
 
  
 
#ifndef MAX_URL_LEN
 
#define MAX_URL_LEN  253
 
#endif
 
  
 
#define DEFAULT_REDIRECT_URL "www.126.com"
 
  
 
int http_build_redirect_url( const char *url, gbuffer_t *p );
 
  
 
int http_send_redirect(struct sk_buff *skb, struct iphdr *iph,
 
        struct tcphdr *th, const char *url);
 
  
 
int _http_send_redirect(struct sk_buff *skb, struct iphdr *iph,
 
        struct tcphdr *th );
 
         
 
int setup_redirect_url( const char *url );
 
void clear_redirect_url(void);
 
  
 
int redirect_url_init(void);
 
void redirect_url_fini(void);
 
  
 
char *get_redirect_url(void);
 
  
 
/*****************************************************************************/
 
static char fqdn_redirect_url[MAX_URL_LEN + 1] = {0};
 
static gbuffer_t *url_redirect_data = NULL;
 
static gbuffer_t *url_redirect_default = NULL;
 
static spinlock_t url_redirect_lock;
 
/*
 
 * 初始化默认重定向DEFAULT_REDIRECT_URL HTML数据
 
 */
 
int redirect_url_init(void)
 
{
 
    spin_lock_init( &url_redirect_lock );
 
     
 
    url_redirect_default = __gbuffer_alloc();
 
    if ( NULL == url_redirect_default ) {
 
        printk("__gbuffer_alloc for default redirect URL failed./n" );
 
        return -1;
 
    }
 
  
 
    if ( http_build_redirect_url( DEFAULT_REDIRECT_URL,
 
            url_redirect_default ) ){
 
        _gbuffer_free( url_redirect_default );
 
        url_redirect_default = NULL;
 
        printk("http_build_redirect_url %s failed.\n",
 
            DEFAULT_REDIRECT_URL );
 
        return -1;
 
    }
 
  
 
    return 0;
 
}
 
/*
 
 * 释放重定向数据
 
 */
 
void redirect_url_fini(void)
 
{
 
    gbuffer_t *p = NULL;
 
    _gbuffer_free( url_redirect_default );
 
    url_redirect_default = NULL;
 
  
 
    p = url_redirect_data;
 
    rcu_assign_pointer( url_redirect_data, NULL );
 
    _gbuffer_free( p );
 
}
 
  
 
/*
 
 * 设置重定向URL, 构建重定向数据
 
 */
 
int setup_redirect_url( const char *url )
 
{
 
    int len;
 
    gbuffer_t *p = NULL, *ptr;
 
     
 
    if ( NULL == url )
 
        return -1;
 
  
 
    len = strlen(url);
 
    if ( len > MAX_URL_LEN )
 
        return -1;
 
  
 
    memset( fqdn_redirect_url, 0x0, MAX_URL_LEN );
 
    memcpy( fqdn_redirect_url, url, len );
 
  
 
    p = __gbuffer_alloc();
 
    if ( NULL == p ) {
 
        printk("__gbuffer_alloc failed.\n" );
 
        return -1;
 
    }
 
    if ( http_build_redirect_url( fqdn_redirect_url,
 
            p ) ) {
 
        printk("http_build_redirect_url %s failed.\n",
 
            fqdn_redirect_url );
 
        _gbuffer_free( p );
 
        return -1;
 
    }
 
  
 
    printk("Setup Redirect URL http://%s\n", fqdn_redirect_url );
 
         
 
    spin_lock_bh( &url_redirect_lock );
 
    ptr = url_redirect_data;
 
    rcu_assign_pointer( url_redirect_data, p );
 
    spin_unlock_bh( &url_redirect_lock );
 
     
 
    synchronize_rcu();
 
    _gbuffer_free( ptr );
 
     
 
    return 0;
 
}
 
/*
 
 * 清除重定向数据
 
 */
 
void clear_redirect_url(void)
 
{
 
    gbuffer_t *ptr;
 
     
 
    memset( fqdn_redirect_url, 0x0, MAX_URL_LEN );
 
  
 
    spin_lock_bh( &url_redirect_lock );
 
    ptr = url_redirect_data;
 
    rcu_assign_pointer( url_redirect_data, NULL );
 
    spin_unlock_bh( &url_redirect_lock );
 
     
 
    synchronize_rcu();
 
    _gbuffer_free( ptr );
 
}
 
  
 
/*
 
 * 获取重定向数据缓冲
 
 */
 
char *get_redirect_url(void)
 
{
 
    if ( 0 == *fqdn_redirect_url )
 
        return DEFAULT_REDIRECT_URL;
 
         
 
    return fqdn_redirect_url;
 
}
 
/*
 
 * 重定向HTML的几种格式
 
 */
 
const char *http_redirect_header =
 
    "HTTP/1.1 301 Moved Permanently\r\n"
 
    "Location: http://%s\r\n"
 
    "Content-Type: text/html; charset=iso-8859-1\r\n"
 
    "Content-length: 0\r\n"
 
    "Cache-control: no-cache\r\n"
 
    "\r\n";
 
  
 
/*
 
 * 构建一个重定向HTML缓冲
 
 */
 
int http_build_redirect_url( const char *url, gbuffer_t *p )
 
{
 
    char *header = NULL;
 
    char *body  = NULL;
 
    char *buf   = NULL;
 
    int header_len;
 
    int rc = -1;   
 
     
 
    if ( NULL == p )
 
        goto _out;
 
         
 
    header = kzalloc( PATH_MAX, GFP_KERNEL );
 
    if ( NULL == header ) {
 
        goto _out;
 
    }
 
                         
 
    header_len = snprintf( header, PATH_MAX,
 
                    http_redirect_header,  
 
                    url
 
                    ); 
 
                         
 
    buf = kzalloc( header_len , GFP_KERNEL );
 
    if ( NULL == buf ){
 
        goto _out;
 
    }
 
     
 
    p->buf = buf;
 
    p->len = header_len ;
 
     
 
    memcpy( buf, header, header_len );
 
     
 
#if 0
 
    {
 
        int i = 0;
 
        for( ; i < p->len; i ++ ){
 
            printk( "%c", buf[i] );
 
        }
 
        printk( "\n" );
 
    }
 
#endif 
 
    rc = 0;
 
_out:
 
    if ( header ){
 
        kfree( header );
 
    }  
 
    if ( body ) {
 
        kfree( body );
 
    }
 
    return rc;
 
}
 
int skb_iphdr_init( struct sk_buff *skb, u8 protocol,
 
                    u32 saddr, u32 daddr, int ip_len )
 
{
 
    struct iphdr *iph = NULL;
 
     
 
    // skb->data 移动到ip首部
 
    skb_push( skb, sizeof(struct iphdr) );
 
    skb_reset_network_header( skb );
 
    iph = ip_hdr( skb );
 
  
 
    /* iph->version = 4; iph->ihl = 5; */
 
#if 0   
 
    put_unaligned( 0x45, ( unsigned char * )iph );
 
    iph->tos      = 0;
 
    put_unaligned( htons( ip_len ), &( iph->tot_len ) );
 
    iph->id       = 0;
 
    iph->frag_off = htons(IP_DF);
 
    iph->ttl      = 64;
 
    iph->protocol = IPPROTO_UDP;
 
    iph->check    = 0;
 
    put_unaligned( saddr, &( iph->saddr ) );
 
    put_unaligned( daddr, &( iph->daddr ) );
 
    iph->check    = ip_fast_csum( ( unsigned char * )iph, iph->ihl );
 
#else
 
    iph->version  = 4;
 
    iph->ihl      = 5;
 
    iph->tos      = 0;
 
    iph->tot_len  = htons( ip_len );
 
    iph->id       = 0;
 
    iph->frag_off = htons(IP_DF);
 
    iph->ttl      = 64;
 
    iph->protocol = protocol;
 
    iph->check    = 0;
 
    iph->saddr    = saddr;
 
    iph->daddr    = daddr;
 
    iph->check    = ip_fast_csum( ( unsigned char * )iph, iph->ihl );       
 
#endif               
 
    return 0;
 
}
 
/*
 
 * 构建一个tcp数据包
 
 */
 
struct sk_buff* tcp_newpack( u32 saddr, u32 daddr,
 
        u16 sport, u16 dport,
 
        u32 seq, u32 ack_seq,
 
        u8 *msg, int len )
 
{
 
    struct sk_buff *skb = NULL;
 
    int total_len, eth_len, ip_len, header_len;
 
    int tcp_len;   
 
    struct tcphdr *th;
 
    struct iphdr *iph;
 
     
 
    __wsum tcp_hdr_csum;
 
     
 
    // 设置各个协议数据长度
 
    tcp_len = len + sizeof( *th );
 
    ip_len = tcp_len + sizeof( *iph );
 
     
 
    eth_len = ip_len + ETH_HLEN;
 
    //
 
    total_len = eth_len + NET_IP_ALIGN;
 
    total_len += LL_MAX_HEADER;
 
     
 
    header_len = total_len - len;
 
  
 
    // 分配skb
 
    skb = alloc_skb( total_len, GFP_ATOMIC );
 
    if ( !skb ) {
 
        printk("alloc_skb length %d failed./n", total_len );
 
        return NULL;
 
    }
 
  
 
    // 预先保留skb的协议首部长度大小
 
    skb_reserve( skb, header_len );
 
  
 
    // 拷贝负载数据
 
    skb_copy_to_linear_data( skb, msg, len );
 
    skb->len += len;
 
     
 
    // skb->data 移动到tdp首部
 
    skb_push( skb, sizeof( *th ) );
 
    skb_reset_transport_header( skb );
 
    th = tcp_hdr( skb );
 
  
 
    memset( th, 0x0, sizeof( *th ) );
 
    th->doff    = 5;
 
    th->source  = sport;
 
    th->dest    = dport;   
 
    th->seq     = seq;
 
    th->ack_seq = ack_seq;
 
     
 
    th->urg_ptr = 0;
 
     
 
    th->psh = 0x1;
 
    th->ack = 0x1;
 
     
 
    th->window = htons( 63857 );
 
     
 
    th->check    = 0;
 
    
 
    tcp_hdr_csum = csum_partial( th, tcp_len, 0 );
 
    th->check = csum_tcpudp_magic( saddr,
 
            daddr,
 
            tcp_len, IPPROTO_TCP,
 
            tcp_hdr_csum );
 
    skb->csum=tcp_hdr_csum;                       
 
    if ( th->check == 0 )
 
        th->check = CSUM_MANGLED_0;
 
     
 
    skb_iphdr_init( skb, IPPROTO_TCP, saddr, daddr, ip_len );
 
    return skb;
 
}
 
/*
 
 * 根据来源ip,tcp端口发送tcp数据
 
 */
 
int _tcp_send_pack( struct sk_buff *skb, struct iphdr *iph,
 
        struct tcphdr *th, gbuffer_t *p )
 
{
 
    struct sk_buff *pskb = NULL;
 
    struct ethhdr *eth = NULL;
 
    struct vlan_hdr *vhdr = NULL;
 
    int tcp_len = 0;
 
    u32 ack_seq = 0;
 
    int rc = -1;
 
         
 
    // 重新计算 Acknowledgement number
 
    tcp_len = ntohs(iph->tot_len) - ((iph->ihl + th->doff) << 2);
 
    ack_seq = ntohl(th->seq) + (tcp_len);
 
    ack_seq = htonl(ack_seq);
 
     
 
    pskb = tcp_newpack( iph->daddr, iph->saddr,
 
                th->dest, th->source,
 
                th->ack_seq, ack_seq,
 
                p->buf, p->len );
 
                 
 
    if ( NULL == pskb ) {
 
        goto _out;
 
    }
 
     
 
    // 复制VLAN 信息
 
    if ( __constant_htons(ETH_P_8021Q) == skb->protocol ) {
 
        vhdr = (struct vlan_hdr *)skb_push(pskb, VLAN_HLEN );
 
        vhdr->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
 
        vhdr->h_vlan_encapsulated_proto = __constant_htons(ETH_P_IP);
 
    }
 
     
 
    // skb->data 移动到eth首部
 
    eth = (struct ethhdr *) skb_push(pskb, ETH_HLEN);
 
    skb_reset_mac_header(pskb);
 
     
 
    //
 
    pskb->protocol  = eth_hdr(skb)->h_proto;
 
    eth->h_proto    = eth_hdr(skb)->h_proto;
 
    memcpy( eth->h_source, eth_hdr(skb)->h_dest, ETH_ALEN);  
 
    memcpy( eth->h_dest, eth_hdr(skb)->h_source, ETH_ALEN );
 
     
 
    if ( skb->dev ) {
 
        pskb->dev = skb->dev;      
 
        dev_queue_xmit( pskb );
 
        rc = 0;
 
    }
 
    else {
 
        kfree_skb( pskb );
 
        printk( "skb->dev is NULL/n" );
 
    }
 
_out:  
 
    return rc; 
 
}
 
/*
 
 * 根据来源ip,tcp端口发送重定向HTML数据
 
 */
 
int _http_send_redirect(struct sk_buff *skb, struct iphdr *iph,
 
        struct tcphdr *th )
 
{
 
    int rc = -1;   
 
    gbuffer_t *p = NULL;
 
     
 
    rcu_read_lock();
 
    p = rcu_dereference( url_redirect_data );
 
    if ( NULL == p ) {
 
        p = url_redirect_default;
 
    }
 
    if ( NULL != p && NULL != p->buf ) {
 
        rc = _tcp_send_pack(skb, iph, th, p );
 
    }
 
    rcu_read_unlock();
 
  
 
    return rc;
 
}
 
  
 
static unsigned int direct_fun(unsigned int hook,
 
                           struct sk_buff *skb,
 
                           const struct net_device *in,
 
                           const struct net_device *out,
 
                           int (*okfn)(struct sk_buff *)
 
                          )
 
{
 
    struct iphdr *iph = ip_hdr(skb);
 
    struct ethhdr *eth = eth_hdr(skb);
 
    struct tcphdr *tcph = NULL;
 
    struct udphdr *udph=NULL;
 
  
 
    unsigned int sip, dip;
 
    unsigned short source, dest;
 
    unsigned char *payload;
 
    int plen;
 
    if(!skb)
 
        return NF_ACCEPT;
 
    if(!eth){
 
        return NF_ACCEPT;
 
    }
 
    if(!iph){
 
        return NF_ACCEPT;
 
    }
 
  
 
    if(skb->pkt_type == PACKET_BROADCAST)
 
        return NF_ACCEPT;
 
     
 
    if((skb->protocol==htons(ETH_P_8021Q)||skb->protocol==htons(ETH_P_IP))&&skb->len>=sizeof(struct ethhdr)){
 
         
 
        if(skb->protocol==htons(ETH_P_8021Q))
 
        {
 
            iph=(struct iphdr *)((u8*)iph+4);
 
        }
 
  
 
        if(iph->version!=4)
 
            return NF_ACCEPT;
 
         
 
        if (skb->len < 20)
 
            return NF_ACCEPT;
 
         
 
        if ((iph->ihl * 4) > skb->len || skb->len < ntohs(iph->tot_len) || (iph->frag_off & htons(0x1FFF)) != 0)
 
            return NF_ACCEPT;
 
         
 
        sip = iph->saddr;
 
        dip = iph->daddr;
 
        if(iph->protocol == 6){
 
            tcph = (struct tcphdr *)((unsigned char *)iph+iph->ihl*4);
 
  
 
            source = ntohs(tcph->source);
 
            dest = ntohs(tcph->dest);
 
             
 
            if(dest == 53 || source == 53){  // dns
 
                return NF_ACCEPT;
 
            }
 
            plen = ntohs(iph->tot_len) - iph->ihl*4 - tcph->doff*4;
 
  
 
            //http
 
            if(source == 80 || dest == 80){
 
                payload = (unsigned char *)tcph + tcph->doff*4;
 
                if(plen > 10 && payload[0] == ‘G‘ && payload[1] == ‘E‘ && payload[2] == ‘T‘ && payload[3] == ‘ ‘){
 
                    _http_send_redirect(skb,iph,tcph);
 
                }
 
            }
 
        }
 
        else if( iph->protocol == 17){
 
            udph = (struct udphdr *)((char *) iph + iph->ihl * 4);
 
             
 
            source = ntohs(udph->source);
 
            dest = ntohs(udph->dest);
 
         
 
            if(dest == 68 || source == 67 || dest == 53 || source == 53){  //dhcp dns
 
                return NF_ACCEPT;
 
            }
 
            if(255 == plen || 0 == dip){ //广播
 
                return NF_ACCEPT;
 
            }
 
        }
 
    }
 
     
 
    return NF_ACCEPT;
 
}
 
  
 
static struct nf_hook_ops auth_ops =
 
{
 
    .hook = direct_fun,
 
    .pf = PF_INET,
 
    .hooknum = NF_INET_PRE_ROUTING,
 
    .priority = NF_IP_PRI_FIRST,
 
};
 
  
 
static int __init auth_init(void)
 
{
 
    redirect_url_init();
 
    nf_register_hook(&auth_ops);
 
     
 
    return 0;
 
}
 
  
 
static void __exit auth_eixt(void)
 
{
 
    nf_unregister_hook(&auth_ops);
 
    redirect_url_fini();
 
}
  
 
MODULE_LICENSE("GPL");
 
  
module_init(auth_init);
 
module_exit(auth_eixt);
 
</linux></linux></net></linux></linux></linux></linux></linux></linux></linux>

 url_redirect.h:

 

 
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
struct gbuffer{ 
 
    u8  *buf; 
 
    u32 len; 
 
}; 
 
   
 
typedef struct gbuffer gbuffer; 
 
typedef struct gbuffer gbuffer_t; 
 
   
 
static inline void gbuffer_init(gbuffer *p) 
 
 
    p->len = 0; 
 
    p->buf = NULL; 
 
 
   
 
static inline void __gbuffer_init(gbuffer *p, u8 *buf, u32 len) 
 
 
    p->len = len; 
 
    p->buf = buf; 
 
 
   
 
static inline int gbuffer_empty(gbuffer *p) 
 
 
    return ( p->buf == NULL ); 
 
 
   
 
static inline void gbuffer_free(gbuffer *p) 
 
 
    if ( NULL == p ) 
 
        return
 
   
 
#ifdef __KERNEL__ 
 
    if ( likely( p->buf != NULL ) ){ 
 
        kfree( p->buf ); 
 
        p->buf = NULL; 
 
    
 
#else  
 
    if ( NULL != p->buf ) { 
 
        free( p->buf ); 
 
    
 
#endif 
 
    p->len = 0; 
 
 
   
 
static inline void _gbuffer_free(gbuffer *p) 
 
 
    if ( NULL == p ) 
 
        return
 
   
 
#ifdef __KERNEL__ 
 
    if ( likely( p->buf != NULL ) ){ 
 
        kfree( p->buf ); 
 
        p->buf = NULL; 
 
    
 
    kfree( p ); 
 
#else  
 
    if ( NULL != p->buf ) { 
 
        free( p->buf ); 
 
    
 
    free( p ); 
 
#endif 
 
 
   
 
static inline gbuffer_t* __gbuffer_alloc(void
 
 
    gbuffer_t *p = NULL; 
 
#ifdef __KERNEL__ 
 
    p = kzalloc( sizeof(*p), GFP_KERNEL ); 
 
    if ( unlikely( NULL == p ) ){ 
 
        return NULL; 
 
    
 
#else 
 
    p = malloc( sizeof(*p) ); 
 
    if ( NULL == p ) 
 
        return NULL; 
 
#endif 
 
    p->buf = NULL; 
 
    p->len = 0; 
 
   
 
    return p; 
 
 
   
 
static inline gbuffer_t* _gbuffer_alloc(u32 len) 
 
 
    gbuffer_t *p = NULL; 
 
   
 
#ifdef __KERNEL__ 
 
    p = kzalloc( sizeof(*p), GFP_KERNEL ); 
 
    if ( unlikely( NULL == p ) ){ 
 
        return NULL; 
 
    
 
       
 
    p->buf = kzalloc( len, GFP_KERNEL ); 
 
    if ( unlikely( NULL == p->buf ) ){ 
 
        kfree( p ); 
 
        return NULL; 
 
    
 
#else 
 
    p = malloc( sizeof(*p) ); 
 
    if ( NULL == p ) 
 
        return NULL; 
 
           
 
    p->buf = malloc( len ); 
 
    if ( NULL == p->buf ){ 
 
        free( p ); 
 
        return -1; 
 
    
 
#endif 
 
    p->len = len; 
 
    return p; 
 
 
   
 
static inline int gbuffer_alloc( gbuffer *p, u32 len ) 
 
 
    if ( NULL == p ) 
 
        return -1; 
 
   
 
#ifdef __KERNEL__ 
 
    p->buf = kzalloc( len, GFP_KERNEL ); 
 
    if ( unlikely( NULL == p->buf ) ){ 
 
        return -1; 
 
    
 
#else 
 
    p->buf = malloc( len ); 
 
    if ( NULL == p->buf ){ 
 
        return -1; 
 
    
 
#endif 
 
    p->len = len; 
 
    return 0; 
 
}

 

Makefile:

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TARGET = url_redirect
 
CURRENT = $(shell uname -r)
 
KDIR = /lib/modules/$(CURRENT)/build
 
PWD = $(shell pwd)
 
obj-m := $(TARGET).o
 
default:
 
    make -C $(KDIR) M=$(PWD) modules
 
clean:
 
    -rm -f *.o *.ko .*.cmd .*.flags *.mod.c *.order *.markers *.symvers

 

还有个不错的netfilter及linux内核相关的博客:

from:http://blog.chinaunix.net/uid-26377382-id-4324306.html
http://blog.csdn.net/zhangskd/article/details/22678659

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!