aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/interceptor/linux_hook_magic.c
blob: 20a6edbcba2e9cddb1e626ed26c0d37782eca142 (plain)
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
/* Netfilter Driver for IPSec VPN Client
 *
 * Copyright(c)   2012 Samsung Electronics
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * linux_hook_magic.c
 *
 * Linux netfilter hook probing.
 *
 */

#include "linux_internal.h"

#include <net/checksum.h>
#include <linux/udp.h>

extern SshInterceptor ssh_interceptor_context;

/***************************** Probe hooks ***********************************/

#define SSH_LINUX_HOOK_MAGIC_NUM_PROBES 10

typedef struct SshInterceptorHookMagicOpsRec
{
  struct nf_hook_ops ops;
  struct sk_buff *skbp[SSH_LINUX_HOOK_MAGIC_NUM_PROBES];
  int probe_count;
} SshInterceptorHookMagicOpsStruct;

static SshInterceptorHookMagicOpsStruct hook_magic_in4;
static SshInterceptorHookMagicOpsStruct hook_magic_out4;

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
static SshInterceptorHookMagicOpsStruct hook_magic_in6;
static SshInterceptorHookMagicOpsStruct hook_magic_out6;
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
static SshInterceptorHookMagicOpsStruct hook_magic_arp_in;
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */


static unsigned int
ssh_interceptor_hook_magic_hookfn(int pf,
				  unsigned int hooknum,
				  SshHookSkb *skb,
				  const struct net_device *in,
				  const struct net_device *out,
				  int (*okfn) (struct sk_buff *))
{
  struct sk_buff *skbp = SSH_HOOK_SKB_PTR(skb);
  int i;

  /* Grab lock */
  local_bh_disable();
  ssh_kernel_mutex_lock(ssh_interceptor_context->interceptor_lock);

  switch (pf)
    {
    case PF_INET:
      switch (hooknum)
	{
	case SSH_NF_IP_PRE_ROUTING:
	  /* Check if this is our hook_magic probe */
	  for (i = 0; i < hook_magic_in4.probe_count; i++)
	    {
	      if (hook_magic_in4.skbp[i] == skbp)
		{
		  SSH_ASSERT(okfn != NULL);

		  /* Check if we have probed this hook */
		  if (ssh_interceptor_context->linux_fn.ip_rcv_finish == NULL)
		    {
		      /* Make sure the assignments do not get reordered */
		      barrier();

		      ssh_interceptor_context->linux_fn.ip_rcv_finish = okfn;
		      SSH_DEBUG(SSH_D_LOWOK, ("ip_rcv_finish 0x%p", okfn));
		    }
		  else
		    {
		      SSH_DEBUG(SSH_D_LOWOK,
				("Double probe received for "
				 "NF_IP_PRE_ROUTING"));
		      SSH_ASSERT(ssh_interceptor_context->
				 linux_fn.ip_rcv_finish == okfn);
		    }

		  /* Mark packet as being handled */
		  hook_magic_in4.skbp[i] = NULL;
		  goto drop;
		}
	    }

	  /* Not our probe packet, let through */
	  SSH_DEBUG(10, ("Not our skbp 0x%p", skbp));
	  goto accept;

	case SSH_NF_IP_POST_ROUTING:
	  /* Check if this is our hook_magic probe */
	  for (i = 0; i < hook_magic_out4.probe_count; i++)
	    {
	      if (hook_magic_out4.skbp[i] == skbp)
		{
		  SSH_ASSERT(okfn != NULL);

		  /* Check if we have probed this hook */
		  if (ssh_interceptor_context->linux_fn.ip_finish_output
		      == NULL)
		    {
		      /* Make sure the assignments do not get reordered */
		      barrier();

		      ssh_interceptor_context->linux_fn.ip_finish_output = okfn;
		      SSH_DEBUG(SSH_D_LOWOK, ("ip_finish_output 0x%p", okfn));
		    }
		  else
		    {
		      SSH_DEBUG(SSH_D_LOWOK,
				("Double probe received for "
				 "NF_IP_POST_ROUTING"));
		      SSH_ASSERT(ssh_interceptor_context->
				 linux_fn.ip_finish_output == okfn);
		    }

		  /* Mark packet as being handled */
		  hook_magic_out4.skbp[i] = NULL;
		  goto drop;
		}
	    }

	  /* Not our probe packet, let through */
	  SSH_DEBUG(10, ("Not our skbp 0x%p", skbp));
	  goto accept;

	default:
	  SSH_NOTREACHED;
	  break;
	}
      break;

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
    case PF_INET6:
      switch (hooknum)
	{
	case SSH_NF_IP6_PRE_ROUTING:
	  /* Check if this is our hook_magic probe */
	  for (i = 0; i < hook_magic_in6.probe_count; i++)
	    {
	      if (hook_magic_in6.skbp[i] == skbp)
		{
		  SSH_ASSERT(okfn != NULL);

		  /* Check if we have probed this hook */
		  if (ssh_interceptor_context->linux_fn.ip6_rcv_finish == NULL)
		    {
		      /* Make sure the assignments do not get reordered */
		      barrier();

		      ssh_interceptor_context->linux_fn.ip6_rcv_finish = okfn;
		      SSH_DEBUG(SSH_D_LOWOK, ("ip6_rcv_finish 0x%p", okfn));
		    }
		  else
		    {
		      SSH_DEBUG(SSH_D_LOWOK,
				("Double probe received for "
				 "NF_IP6_PRE_ROUTING"));
		      SSH_ASSERT(ssh_interceptor_context->
				 linux_fn.ip6_rcv_finish == okfn);
		    }

		  /* Mark packet as being handled */
		  hook_magic_in6.skbp[i] = NULL;
		  goto drop;
		}
	    }

	  /* Not our probe packet, let through */
	  SSH_DEBUG(10, ("Not our skbp 0x%p", skbp));
	  goto accept;

	case SSH_NF_IP6_POST_ROUTING:
	  /* Check if this is our hook_magic probe */
	  for (i = 0; i < hook_magic_out6.probe_count; i++)
	    {
	      if (hook_magic_out6.skbp[i] == skbp)
		{
		  SSH_ASSERT(okfn != NULL);

		  /* Check if we have not yet probed this hook */
		  if (ssh_interceptor_context->linux_fn.ip6_output_finish
		      == NULL)
		    {
		      /* Make sure the assignments do not get reordered */
		      barrier();

		      ssh_interceptor_context->linux_fn.ip6_output_finish =
			okfn;
		      SSH_DEBUG(SSH_D_LOWOK, ("ip6_output_finish 0x%p", okfn));
		    }
		  else
		    {
		      SSH_DEBUG(SSH_D_LOWOK,
				("Double probe received for "
				 "NF_IP6_POST_ROUTING"));
		      SSH_ASSERT(ssh_interceptor_context->
				 linux_fn.ip6_output_finish == okfn);
		    }

		  /* Mark packet as being handled */
		  hook_magic_out6.skbp[i] = NULL;
		  goto drop;
		}
	    }

	  /* Not our probe packet, let through */
	  SSH_DEBUG(10, ("Not our skbp 0x%p", skbp));
	  goto accept;

	default:
	  SSH_NOTREACHED;
	  break;
	}
      break;
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
    case SSH_NFPROTO_ARP:
      /* Check if this is our hook_magic probe */
      for (i = 0; i < hook_magic_arp_in.probe_count; i++)
	{
	  if (hook_magic_arp_in.skbp[i] == skbp)
	    {
	      SSH_ASSERT(hooknum == NF_ARP_IN);
	      SSH_ASSERT(okfn != NULL);

	      /* Check if we have probed this hook */
	      if (ssh_interceptor_context->linux_fn.arp_process == NULL)
		{
		  /* Make sure the assignments do not get reordered */
		  barrier();

		  ssh_interceptor_context->linux_fn.arp_process = okfn;
		  SSH_DEBUG(SSH_D_LOWOK, ("arp_process 0x%p", okfn));
		}
	      else
		{
		  SSH_DEBUG(SSH_D_LOWOK,
			    ("Double probe received for NF_ARP_IN"));
		  SSH_ASSERT(ssh_interceptor_context->linux_fn.arp_process
			     == okfn);
		}

	      /* Mark packet as being handled */
	      hook_magic_arp_in.skbp[i] = NULL;
	      goto drop;
	    }
	}

      /* Not our probe packet, let through */
      SSH_DEBUG(10, ("Not our skbp 0x%p", skbp));
      goto accept;
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */

    default:
      SSH_NOTREACHED;
      break;
    }

  /* Unlock and drop the packet */
 drop:
  ssh_kernel_mutex_unlock(ssh_interceptor_context->interceptor_lock);
  local_bh_enable();
  return NF_DROP;

 accept:
  /* Unlock and let packet continue */
  ssh_kernel_mutex_unlock(ssh_interceptor_context->interceptor_lock);
  local_bh_enable();
  return NF_ACCEPT;
}

static unsigned int
ssh_interceptor_hook_magic_hookfn_ipv4(unsigned int hooknum,
				       SshHookSkb *skb,
				       const struct net_device *in,
				       const struct net_device *out,
				       int (*okfn) (struct sk_buff *))
{
  return ssh_interceptor_hook_magic_hookfn(PF_INET, hooknum, skb,
					   in, out, okfn);
}

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
static unsigned int
ssh_interceptor_hook_magic_hookfn_ipv6(unsigned int hooknum,
				       SshHookSkb *skb,
				       const struct net_device *in,
				       const struct net_device *out,
				       int (*okfn) (struct sk_buff *))
{
  return ssh_interceptor_hook_magic_hookfn(PF_INET6, hooknum, skb,
					   in, out, okfn);
}
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
static unsigned int
ssh_interceptor_hook_magic_hookfn_arp(unsigned int hooknum,
				      SshHookSkb *skb,
				      const struct net_device *in,
				      const struct net_device *out,
				      int (*okfn) (struct sk_buff *))
{
  return ssh_interceptor_hook_magic_hookfn(SSH_NFPROTO_ARP, hooknum, skb,
					   in, out, okfn);
}
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */


/***************************** Probe packet sending **************************/

/* Caller holds 'interceptor_lock'.
   This function will release it when calling the network stack functions,
   and will grab it back after the function has returned. */

static int
ssh_interceptor_hook_magic_send(SshInterceptorHookMagicOpsStruct *hook_magic)
{
  struct sk_buff *skbp;
  struct net_device *dev;
  struct in_device *inet_dev = NULL;
  struct iphdr *iph;
#ifdef SSH_LINUX_INTERCEPTOR_IPV6
  struct inet6_dev *inet6_dev = NULL;
  struct ipv6hdr *ip6h;
  struct in6_addr addr6;
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */
#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
  unsigned char *ptr;
  struct arphdr *arph;
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */
  struct udphdr *udph;

  /* Assert that we have the lock */
  ssh_kernel_mutex_assert_is_locked(ssh_interceptor_context->interceptor_lock);

  /* Find a suitable device to put on the probe skb */
  read_lock(&dev_base_lock);

  for (dev = SSH_FIRST_NET_DEVICE();
       dev != NULL;
       dev = SSH_NEXT_NET_DEVICE(dev))
    {
      if (!(dev->flags & IFF_UP))
	continue;

      inet_dev = in_dev_get(dev);
      if (inet_dev == NULL || inet_dev->ifa_list == NULL)
	goto next_dev;

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
      /* The device must have an IPv6 address. */
      inet6_dev = in6_dev_get(dev);
      memset(&addr6, 0, sizeof(addr6));

      if (inet6_dev != NULL)
	{
	  struct inet6_ifaddr *ifaddr6, *next_ifaddr6;
	  SSH_INET6_IFADDR_LIST_FOR_EACH(ifaddr6,
					 next_ifaddr6,
					 inet6_dev->addr_list)
	    {
	      addr6 = ifaddr6->addr;
	      break;
	    }
	}

      if (inet6_dev == NULL ||
	  memcmp(addr6.s6_addr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0)
	goto next_dev;
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
      /* The device must must support ARP. */
      if ((dev->flags & IFF_NOARP) || dev->addr_len != 6)
	goto next_dev;
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */

      /* This device is suitable for probing. */
      break;

    next_dev:
      if (inet_dev)
	in_dev_put(inet_dev);
      inet_dev = NULL;
#ifdef SSH_LINUX_INTERCEPTOR_IPV6
      if (inet6_dev)
	in6_dev_put(inet6_dev);
      inet6_dev = NULL;
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */
    }
  read_unlock(&dev_base_lock);
  if (dev == NULL)
    {
      SSH_DEBUG(SSH_D_ERROR,
		("Hook magic failed, could not find a suitable device"));
      goto out;
    }

  /* Allocate probe skb */
  /* Maximum size: IPv6 + UDP = 48 bytes
     ( + headroom for ethernet header + 2 bytes for proper alignment) */
  skbp = alloc_skb(48 + ETH_HLEN + 2, GFP_ATOMIC);
  if (skbp == NULL)
    {
      SSH_DEBUG(SSH_D_ERROR,
		("Hook magic failed, could not allocate skbuff"));
      goto out;
    }

  /* Reserve headroom for ethernet header.
     Make sure IP header will be properly aligned. */
  skb_reserve(skbp, ETH_HLEN + 2);

  SSH_SKB_SET_MACHDR(skbp, skbp->data);

#ifdef LINUX_HAS_SKB_MAC_LEN
  skbp->mac_len = 0;
#endif /* LINUX_HAS_SKB_MAC_LEN */
  skbp->pkt_type = PACKET_HOST;

  skbp->dev = dev;
  SSH_SKB_DST_SET(skbp, NULL);

  /* Store skb pointer to a free slot. */
  SSH_ASSERT(hook_magic->probe_count < SSH_LINUX_HOOK_MAGIC_NUM_PROBES);
  hook_magic->skbp[hook_magic->probe_count] = skbp;
  hook_magic->probe_count++;

  switch (hook_magic->ops.pf)
    {
    case PF_INET:

      /* Build an IP + UDP packet for probing */
      skbp->protocol = __constant_htons(ETH_P_IP);

      iph = (struct iphdr *) skb_put(skbp, sizeof(struct iphdr));
      if (iph == NULL)
	{
	  SSH_DEBUG(SSH_D_ERROR,
		    ("Hook magic failed, no space for IPv4 header"));
	  goto out;
	}
      SSH_SKB_SET_NETHDR(skbp, (unsigned char *) iph);

      memset(iph, 0, sizeof(struct iphdr));
      iph->version = 4;
      iph->ihl = 5;
      iph->ttl = 1;
      iph->protocol = IPPROTO_UDP;
      iph->tot_len = htons(28);

      /* Use the device addresses */
      SSH_ASSERT(inet_dev != NULL && inet_dev->ifa_list != NULL);
      iph->saddr = inet_dev->ifa_list->ifa_local;
      iph->daddr = inet_dev->ifa_list->ifa_local;

      /* Add UDP header */
      udph = (struct udphdr *) skb_put(skbp, sizeof(struct udphdr));
      if (udph == NULL)
	{
	  SSH_DEBUG(SSH_D_ERROR,
		    ("Hook magic failed, no space for UDP header"));
	  goto out;
	}
      SSH_SKB_SET_TRHDR(skbp, (unsigned char *) udph);

      udph->source = htons(9);
      udph->dest = htons(9);
      udph->len = 0;
      udph->check = 0;

      /* Checksum */
      iph->check = 0;
      iph->check = ip_fast_csum((unsigned char *) iph, iph->ihl);

      switch (hook_magic->ops.hooknum)
	{
	case SSH_NF_IP_PRE_ROUTING:
	  SSH_ASSERT(ssh_interceptor_context->
		     linux_fn.ip_rcv_finish == NULL);
	  SSH_DEBUG(SSH_D_HIGHOK,
		    ("Probing SSH_NF_IP_PRE_ROUTING with skbp 0x%p", skbp));
	  /* Release lock for duration of netif_rx() */
	  ssh_kernel_mutex_unlock(ssh_interceptor_context->interceptor_lock);
	  local_bh_enable();
	  netif_rx(skbp);
	  local_bh_disable();
	  ssh_kernel_mutex_lock(ssh_interceptor_context->interceptor_lock);
	  break;

	case SSH_NF_IP_POST_ROUTING:
	  SSH_ASSERT(ssh_interceptor_context->
		     linux_fn.ip_finish_output == NULL);
	  SSH_DEBUG(SSH_D_HIGHOK,
		    ("Probing SSH_NF_IP_POST_ROUTING with skbp 0x%p", skbp));
	  if (ssh_interceptor_reroute_skb_ipv4(ssh_interceptor_context,
					       skbp, 0, 0))
	    {
	      /* Release lock for the duration of dst->output() */
	      ssh_kernel_mutex_unlock(ssh_interceptor_context->
				      interceptor_lock);
	      dst_output(skbp);
	      ssh_kernel_mutex_lock(ssh_interceptor_context->interceptor_lock);
	    }
	  break;

	default:
	  SSH_NOTREACHED;
	  break;
	}
      break;

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
    case PF_INET6:

      /* Build an IPv6 + UDP packet for probing */
      skbp->protocol = __constant_htons(ETH_P_IPV6);
      ip6h = (struct ipv6hdr *) skb_put(skbp, sizeof(struct ipv6hdr));
      if (ip6h == NULL)
	{
	  SSH_DEBUG(SSH_D_ERROR,
		    ("Hook magic failed, no space for IPv6 header"));
	  goto out;
	}
      SSH_SKB_SET_NETHDR(skbp, (unsigned char *) ip6h);

      memset(ip6h, 0, sizeof(struct ipv6hdr));
      ip6h->version = 6;
      ip6h->hop_limit = 1;
      ip6h->nexthdr = NEXTHDR_UDP;

      /* Use the device addresses */
      SSH_ASSERT(memcmp(addr6.s6_addr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16)
		 != 0);

      ip6h->saddr = addr6;
      ip6h->daddr = addr6;

      /* Add UDP header */
      udph = (struct udphdr *) skb_put(skbp, sizeof(struct udphdr));
      if (udph == NULL)
	{
	  SSH_DEBUG(SSH_D_ERROR,
		    ("Hook magic failed, no space for UDP header"));
	  goto out;
	}
      SSH_SKB_SET_TRHDR(skbp, (unsigned char *) udph);

      udph->source = htons(9);
      udph->dest = htons(9);
      udph->len = 0;
      udph->check = 0;

      switch (hook_magic->ops.hooknum)
	{
	case SSH_NF_IP6_PRE_ROUTING:
	  SSH_ASSERT(ssh_interceptor_context->
		     linux_fn.ip6_rcv_finish == NULL);
	  SSH_DEBUG(SSH_D_HIGHOK,
		    ("Probing SSH_NF_IP6_PRE_ROUTING with skbp 0x%p", skbp));
	  /* Release lock for the duration of netif_rx() */
	  ssh_kernel_mutex_unlock(ssh_interceptor_context->interceptor_lock);
	  local_bh_enable();
	  netif_rx(skbp);
	  local_bh_disable();
	  ssh_kernel_mutex_lock(ssh_interceptor_context->interceptor_lock);
	  break;

	case SSH_NF_IP6_POST_ROUTING:
	  SSH_ASSERT(ssh_interceptor_context->
		     linux_fn.ip6_output_finish == NULL);
	  SSH_DEBUG(SSH_D_HIGHOK,
		    ("Probing SSH_NF_IP6_POST_ROUTING with skbp 0x%p", skbp));
	  if (ssh_interceptor_reroute_skb_ipv6(ssh_interceptor_context,
					       skbp, 0, 0))
	    {
	      /* Release lock for the duration of dst->output() */
	      ssh_kernel_mutex_unlock(ssh_interceptor_context->
				      interceptor_lock);
	      dst_output(skbp);
	      ssh_kernel_mutex_lock(ssh_interceptor_context->interceptor_lock);
	    }
	  break;

	default:
	  SSH_NOTREACHED;
	  break;
	}
      break;
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
    case SSH_NFPROTO_ARP:
      SSH_ASSERT(hook_magic->ops.hooknum == NF_ARP_IN);
      SSH_ASSERT(ssh_interceptor_context->linux_fn.arp_process == NULL);

      /* Build an ARP REQ packet for probing */
      skbp->protocol = __constant_htons(ETH_P_ARP);

      SSH_ASSERT(dev->addr_len == 6);
      SSH_ASSERT((sizeof(struct arphdr) + (2 * dev->addr_len) + 8) <= 48);

      arph = (struct arphdr *) skb_put(skbp, sizeof(struct arphdr) +
				       (2 * dev->addr_len) + 8);
      if (arph == NULL)
	{
	  SSH_DEBUG(SSH_D_ERROR,
		    ("Hook magic failed, no space for ARP header"));
	  goto out;
	}
      SSH_SKB_SET_NETHDR(skbp, (unsigned char *) arph);

      arph->ar_hrd = htons(ARPHRD_ETHER);
      arph->ar_pro = htons(ETH_P_IP);
      arph->ar_hln = dev->addr_len;
      arph->ar_pln = 4;
      arph->ar_op = htons(ARPOP_REQUEST);

      ptr = skbp->data + sizeof(*arph);

      /* Zero sender HA */
      memset(ptr, 0, dev->addr_len);
      ptr += dev->addr_len;

      /* Take sender IP from device */
      memcpy(ptr, &inet_dev->ifa_list->ifa_local, 4);
      ptr += 4;

      /* All f's target HA */
      memset(ptr, 0xff, dev->addr_len);
      ptr += dev->addr_len;

      /* Take target IP from device */
      memcpy(ptr, &inet_dev->ifa_list->ifa_local, 4);

      SSH_DEBUG(SSH_D_HIGHOK,
		("Probing NF_ARP_IN with skbp 0x%p", skbp));
      /* Release the lock for the duration of dst->output() */
      ssh_kernel_mutex_unlock(ssh_interceptor_context->interceptor_lock);
      local_bh_enable();
      netif_rx(skbp);
      local_bh_disable();
      ssh_kernel_mutex_lock(ssh_interceptor_context->interceptor_lock);
      break;
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */

    default:
      SSH_NOTREACHED;
      break;
    }

 out:
  if (inet_dev)
    in_dev_put(inet_dev);
#ifdef SSH_LINUX_INTERCEPTOR_IPV6
  if (inet6_dev)
    in6_dev_put(inet6_dev);
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

  return 1;
}

static void
ssh_interceptor_hook_magic_uninit(void)
{
  /* Unregister probe hooks */

  if (hook_magic_in4.ops.pf == PF_INET)
    {
      nf_unregister_hook(&hook_magic_in4.ops);
      hook_magic_in4.ops.pf = 0;
    }
  if (hook_magic_out4.ops.pf == PF_INET)
    {
      nf_unregister_hook(&hook_magic_out4.ops);
      hook_magic_out4.ops.pf = 0;
    }

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
  if (hook_magic_in6.ops.pf == PF_INET6)
    {
      nf_unregister_hook(&hook_magic_in6.ops);
      hook_magic_in6.ops.pf = 0;
    }
  if (hook_magic_out6.ops.pf == PF_INET6)
    {
      nf_unregister_hook(&hook_magic_out6.ops);
      hook_magic_out6.ops.pf = 0;
    }
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
  if (hook_magic_arp_in.ops.pf == SSH_NFPROTO_ARP)
    {
      nf_unregister_hook(&hook_magic_arp_in.ops);
      hook_magic_arp_in.ops.pf = 0;
    }
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */
}

int
ssh_interceptor_hook_magic_init()
{
  int ret;
  int i;

  /* Intialize and register probe hooks */

  /* SSH_NF_IP_PRE_ROUTING */
  ssh_interceptor_context->linux_fn.ip_rcv_finish = NULL;
  memset(&hook_magic_in4, 0, sizeof(hook_magic_in4));
  hook_magic_in4.ops.hook = ssh_interceptor_hook_magic_hookfn_ipv4;
  hook_magic_in4.ops.pf = PF_INET;
  hook_magic_in4.ops.hooknum = SSH_NF_IP_PRE_ROUTING;
  hook_magic_in4.ops.priority = SSH_NF_IP_PRI_FIRST;
  nf_register_hook(&hook_magic_in4.ops);

  /* SSH_NF_IP_POST_ROUTING */
  ssh_interceptor_context->linux_fn.ip_finish_output = NULL;
  memset(&hook_magic_out4, 0, sizeof(hook_magic_out4));
  hook_magic_out4.ops.hook = ssh_interceptor_hook_magic_hookfn_ipv4;
  hook_magic_out4.ops.pf = PF_INET;
  hook_magic_out4.ops.hooknum = SSH_NF_IP_POST_ROUTING;
  hook_magic_out4.ops.priority = SSH_NF_IP_PRI_FIRST;
  nf_register_hook(&hook_magic_out4.ops);

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
  /* SSH_NF_IP6_PRE_ROUTING */
  ssh_interceptor_context->linux_fn.ip6_rcv_finish = NULL;
  memset(&hook_magic_in6, 0, sizeof(hook_magic_in6));
  hook_magic_in6.ops.hook = ssh_interceptor_hook_magic_hookfn_ipv6;
  hook_magic_in6.ops.pf = PF_INET6;
  hook_magic_in6.ops.hooknum = SSH_NF_IP6_PRE_ROUTING;
  hook_magic_in6.ops.priority = SSH_NF_IP6_PRI_FIRST;
  nf_register_hook(&hook_magic_in6.ops);

  /* SSH_NF_IP6_POST_ROUTING */
  ssh_interceptor_context->linux_fn.ip6_output_finish = NULL;
  memset(&hook_magic_out6, 0, sizeof(hook_magic_out6));
  hook_magic_out6.ops.hook = ssh_interceptor_hook_magic_hookfn_ipv6;
  hook_magic_out6.ops.pf = PF_INET6;
  hook_magic_out6.ops.hooknum = SSH_NF_IP6_POST_ROUTING;
  hook_magic_out6.ops.priority = SSH_NF_IP6_PRI_FIRST;
  nf_register_hook(&hook_magic_out6.ops);
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
  /* NF_ARP_IN */
  ssh_interceptor_context->linux_fn.arp_process = NULL;
  memset(&hook_magic_arp_in, 0, sizeof(hook_magic_arp_in));
  hook_magic_arp_in.ops.hook = ssh_interceptor_hook_magic_hookfn_arp;
  hook_magic_arp_in.ops.pf = SSH_NFPROTO_ARP;
  hook_magic_arp_in.ops.hooknum = NF_ARP_IN;
  hook_magic_arp_in.ops.priority = 1;
  nf_register_hook(&hook_magic_arp_in.ops);
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */

  /* Send probe packets */

  /* Try maximum SSH_LINUX_HOOK_MAGIC_NUM_PROBES times, and give up */
  for (i = 0; i < SSH_LINUX_HOOK_MAGIC_NUM_PROBES; i++)
    {
      ret = 0;

      local_bh_disable();
      ssh_kernel_mutex_lock(ssh_interceptor_context->interceptor_lock);

      if (ssh_interceptor_context->linux_fn.ip_rcv_finish == NULL)
	ret += ssh_interceptor_hook_magic_send(&hook_magic_in4);
      if (ssh_interceptor_context->linux_fn.ip_finish_output == NULL)
	ret += ssh_interceptor_hook_magic_send(&hook_magic_out4);

#ifdef SSH_LINUX_INTERCEPTOR_IPV6
      if (ssh_interceptor_context->linux_fn.ip6_rcv_finish == NULL)
	ret += ssh_interceptor_hook_magic_send(&hook_magic_in6);
      if (ssh_interceptor_context->linux_fn.ip6_output_finish == NULL)
	ret += ssh_interceptor_hook_magic_send(&hook_magic_out6);
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */

#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
      if (ssh_interceptor_context->linux_fn.arp_process == NULL)
	ret += ssh_interceptor_hook_magic_send(&hook_magic_arp_in);
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */

      ssh_kernel_mutex_unlock(ssh_interceptor_context->interceptor_lock);
      local_bh_enable();

      if (ret == 0)
	break;

      set_current_state(TASK_UNINTERRUPTIBLE);
      if (i < 5)
	schedule_timeout((i + 5) * HZ / 10);
      else
	schedule_timeout(HZ);
    }

  /* Remove probe hooks */
  ssh_interceptor_hook_magic_uninit();

#ifdef DEBUG_LIGHT
  if (ret)
    {
      SSH_DEBUG(SSH_D_ERROR,
		("ssh_hook_magic_init exits with return value %d", -ret));
    }
  else
    {
      /* Assert that all function pointers have been resolved. */
      SSH_ASSERT(ssh_interceptor_context->linux_fn.ip_rcv_finish != NULL);
      SSH_ASSERT(ssh_interceptor_context->linux_fn.ip_finish_output != NULL);
#ifdef SSH_LINUX_INTERCEPTOR_IPV6
      SSH_ASSERT(ssh_interceptor_context->linux_fn.ip6_rcv_finish != NULL);
      SSH_ASSERT(ssh_interceptor_context->linux_fn.ip6_output_finish != NULL);
#endif /* SSH_LINUX_INTERCEPTOR_IPV6 */
#ifndef SSH_IPSEC_IP_ONLY_INTERCEPTOR
      SSH_ASSERT(ssh_interceptor_context->linux_fn.arp_process != NULL);
#endif /* !SSH_IPSEC_IP_ONLY_INTERCEPTOR */
    }

#endif /* DEBUG_LIGHT */

  return -ret;
}