summaryrefslogtreecommitdiff
path: root/proto/wireguard/wireguard.c
blob: 6ac235f77965afb512dc14f133614586dc0c4347 (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
843
844
845
846
847
848
849
850
851
// Based on proto/rip/rip.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "lib/lists.h"
#include "lib/ip.h"
#include "lib/tunnel_encaps.h"
#include "nest/protocol.h"
#include "nest/iface.h"
#include "sysdep/linux/wireguard.h"
#include "sysdep/unix/unix.h"
#include "sysdep/unix/wg_user.h"
#include "wireguard.h"

static ip_addr allowedip_to_ipa(struct wg_allowedip *allowedip);

static
int get_device(struct wg_proto *p, wg_device **pdev, const char *device_name)
{
  struct wg_config *c = (struct wg_config *) p->p.cf;

  /* if (has_user_space(p)) */
    /* return user_get_device(p, dev, device_name); */
  /* else */
  /*   return wg_get_device(dev, device_name); */

  if (p->dev)
    {
      wg_free_device(p->dev);
      p->dev = NULL;
    }

  wg_device *dev = calloc(1, sizeof(wg_device));
  strncpy(dev->name, device_name, sizeof(dev->name));
//  dev->flags = WGDEVICE_REPLACE_PEERS;
  if (c->private_key)
    {
      dev->flags |= WGDEVICE_HAS_PRIVATE_KEY | WGDEVICE_HAS_PUBLIC_KEY;
      wg_key_from_base64(dev->private_key, c->private_key);
      wg_generate_public_key(dev->public_key, dev->private_key);
    }
  if (c->listen_port)
    dev->flags |= WGDEVICE_HAS_LISTEN_PORT;
  dev->listen_port = c->listen_port;
  DBG("listen port %d\n", c->listen_port);

  struct peer_config *pc = NULL;
  WALK_LIST(pc,c->peers)
    {
      wg_peer *peer = calloc(1, sizeof(wg_peer));
      if (!dev->first_peer)
	dev->first_peer = peer;
      if (dev->last_peer)
	dev->last_peer->next_peer = peer;
      dev->last_peer = peer;

      peer->flags = WGPEER_REPLACE_ALLOWEDIPS;

      if (pc->public_key)
	{
	  peer->flags = WGPEER_HAS_PUBLIC_KEY;
	  wg_key_from_base64(peer->public_key, pc->public_key);
	}
      peer->next_peer = NULL;

      if (!ip6_equal(pc->endpoint, IPA_NONE))
        sockaddr_fill((sockaddr*)&peer->endpoint.addr,
                      ipa_is_ip4(pc->endpoint) ? AF_INET : AF_INET6,
                      pc->endpoint, NULL, pc->remote_port);

      wg_allowedip *allowedip = calloc(1, sizeof(wg_allowedip));
      peer->first_allowedip = allowedip;
      peer->last_allowedip = allowedip;

      switch (pc->allowedip.type)
	{
	case NET_IP4:
	  allowedip->family = AF_INET;
	  allowedip->ip4 = ipa_to_in4(net_prefix(&pc->allowedip));
	  allowedip->cidr = net_pxlen(&pc->allowedip);
	  break;
	case NET_IP6:
	  allowedip->family = AF_INET6;
	  allowedip->ip6 = ipa_to_in6(net_prefix(&pc->allowedip));
	  allowedip->cidr = net_pxlen(&pc->allowedip);
	  break;
	default:
	  break;
	}

      allowedip->next_allowedip = NULL;
    }

  *pdev = dev;
  return 0;
}

static int
set_device(struct wg_proto *p)
{
  struct wg_config *c = (struct wg_config *) p->p.cf;

  if (wg_has_userspace(c->ifname))
    return wg_user_set_device(p->p.pool, c->ifname, p->dev);
  else
    {
      WG_TRACE(D_EVENTS, "WG: wg_set_device");
      return wg_set_device(p->dev);
    }
}

static void
wg_init_entry(void *e_)
{
  struct wg_entry *e UNUSED = e_;
//  DBG("wg_init_entry\n");

  memset(e, 0, sizeof(struct wg_entry) - sizeof(struct fib_node));
}

static void
wg_postconfig(struct proto_config *C UNUSED)
{
  DBG("postconfig\n");
}

static void
wg_if_notify(struct proto *P, unsigned flags, struct iface *i)
{
  struct wg_proto *p = (struct wg_proto *) P;
  struct wg_config *c = (struct wg_config *) P->cf;

  DBG("WG: if_notify %p %s %d %s\n", i, i->name, flags, c->ifname);
  if (c->ifname && !strcmp(i->name, c->ifname)) {
    DBG("WG: found ifname\n");
    p->iface = i;
  }

  if (flags & IF_CHANGE_UP)
    {
      DBG("WG: IF_CHANGE_UP %s\n", i->name);

      int res = set_device(p);
      WG_TRACE(D_EVENTS, "WG: wg_set_device %d", res);
    }
}

static void
dump(void *ptr, size_t len)
{
  unsigned char *data = ptr;

  for (size_t i=0; i<len; i++) {
    fprintf(stderr, "%02x ", data[i]);
  }
  fprintf(stderr, "\n");
}

static void
dump_peer(struct wg_peer *peer)
{
  wg_key_b64_string base64;
  wg_key_to_base64(base64, peer->public_key);
  DBG("WG: peer %s\n", base64);

  struct wg_allowedip *allowedip = NULL;
  wg_for_each_allowedip(peer, allowedip) {
    ip_addr ip = allowedip_to_ipa(allowedip);

    DBG("allowedip %I/%d\n", ip, allowedip->cidr);
  }
}

static wg_peer *
add_peer(wg_device *dev, const wg_key pubkey)
{
  struct wg_peer *peer = malloc(sizeof(struct wg_peer));
  memset(peer, 0, sizeof(struct wg_peer));

  peer->flags = WGPEER_HAS_PUBLIC_KEY;
  memcpy(peer->public_key, pubkey, sizeof(wg_key));

  if (dev->first_peer && dev->last_peer)
    dev->last_peer->next_peer = peer;
  else
    dev->first_peer = peer;
  dev->last_peer = peer;
  return peer;
}

static void
remove_marked_peer(struct wg_proto *p)
{
  wg_device *dev = p->dev;
  struct wg_peer *peer = NULL;
  struct wg_peer *prevpeer = NULL;

  WG_TRACE(D_EVENTS, "WG: remove_marked_peer");
  wg_for_each_peer(dev, peer) {
    if (peer->flags & WGPEER_REMOVE_ME) {
      if (!prevpeer) {
	DBG("WG: remove first peer\n");
	dev->first_peer = peer->next_peer;
	if (dev->last_peer == peer) {
	  dev->last_peer = NULL;
	  dev->first_peer = NULL;
	}
      } else {
	DBG("WG: remove middle peer\n");
	// Remove
	if (dev->last_peer == peer)
	  dev->last_peer = prevpeer;

	prevpeer->next_peer = peer->next_peer;
      }

      free(peer);
      return;
    }

    prevpeer = peer;
  }
  log(L_WARN "WG: marked peer not found");
}

static int
set_peer_tunnel_ep(struct wg_proto *p, wg_peer *peer, ip_addr tunnel_ep_addr, u16 udp_dest_port)
{
  if (udp_dest_port != 0 && ipa_nonzero(tunnel_ep_addr) ) {
    if (ipa_is_ip4(tunnel_ep_addr)) {
      WG_TRACE(D_EVENTS, "WG:     found ip4 ep");
      peer->endpoint.addr4.sin_family = AF_INET;
      put_ip4(&peer->endpoint.addr4.sin_addr.s_addr, ipa_to_ip4(tunnel_ep_addr));
      put_u16(&peer->endpoint.addr4.sin_port, udp_dest_port);
    } else {
      WG_TRACE(D_EVENTS, "WG:     found ip6 ep");
      peer->endpoint.addr6.sin6_family = AF_INET6;
      put_ip6(&peer->endpoint.addr6.sin6_addr, ipa_to_ip6(tunnel_ep_addr));
      put_u16(&peer->endpoint.addr6.sin6_port, udp_dest_port);
    }
  }

  return 0;
}

static ip_addr
allowedip_to_ipa(struct wg_allowedip *allowedip)
{
  switch (allowedip->family) {
  case AF_INET:
    return ipa_from_in4(allowedip->ip4);
    break;
  case AF_INET6:
    return ipa_from_in6(allowedip->ip6);
  }

  return IPA_NONE;
}

static void
init_allowed_ip(struct wg_allowedip *allowedip, u8 net_type, struct network *n)
{
  memset(allowedip, 0, sizeof(struct wg_allowedip));

  if (net_type == NET_IP4) {
    allowedip->family = AF_INET;
    allowedip->ip4.s_addr = ip4_to_u32(ip4_hton(net4_prefix(n->n.addr)));
  } else if (net_type == NET_IP6) {
    allowedip->family = AF_INET6;
    ip6_addr addr = ip6_hton(net6_prefix(n->n.addr));
    memcpy(allowedip->ip6.s6_addr, &addr, 16);
  }

  allowedip->cidr = net_pxlen(n->n.addr);
}

static struct wg_allowedip *
create_allowed_ip_network(u8 net_type, struct network *n)
{
  struct wg_allowedip *allowedip = malloc(sizeof(struct wg_allowedip));
  init_allowed_ip(allowedip, net_type, n);
  return allowedip;
}

static void
init_allowed_ip_addr(struct wg_allowedip *allowedip, ip_addr addr)
{
  memset(allowedip, 0, sizeof(struct wg_allowedip));

  if (ipa_is_ip4(addr)) {
    allowedip->family = AF_INET;
    allowedip->ip4.s_addr = ip4_to_u32(ip4_hton(ipa_to_ip4(addr)));
    allowedip->cidr = IP4_MAX_PREFIX_LENGTH;
  } else {
    allowedip->family = AF_INET6;
    ip6_addr netaddr = ip6_hton(ipa_to_ip6(addr));
    memcpy(allowedip->ip6.s6_addr, &netaddr, 16);
    allowedip->cidr = IP6_MAX_PREFIX_LENGTH;
  }
}

static struct wg_allowedip *
create_allowed_ip_addr(ip_addr addr)
{
  struct wg_allowedip *allowedip = malloc(sizeof(struct wg_allowedip));
  init_allowed_ip_addr(allowedip, addr);
  return allowedip;
}

static int
add_allowed_ip(struct wg_allowedip *allowedip, wg_peer *peer)
{
  if (peer->first_allowedip && peer->last_allowedip)
    peer->last_allowedip->next_allowedip = allowedip;
  else
    peer->first_allowedip = allowedip;
  peer->last_allowedip = allowedip;

  return 0;
}

static bool
remove_allowed_ip(wg_peer *peer, struct wg_allowedip *allowedip)
{
  struct wg_allowedip *ip = NULL;
  struct wg_allowedip *previp = NULL;

  wg_for_each_allowedip(peer, ip) {
    if (allowedip->family != ip->family) {
      DBG("WG: family no match\n");
      previp = ip;
      continue;
    }

    if (allowedip->cidr != ip->cidr) {
      DBG("WG: cidr no match\n");
      previp = ip;
      continue;
    }

    if (memcmp(&allowedip->ip6, &ip->ip6, sizeof(struct in6_addr))) {
      DBG("WG: ip no match\n");
#if defined(LOCAL_DEBUG) || defined(GLOBAL_DEBUG)
      dump(&allowedip->ip6, sizeof(struct in6_addr));
      dump(&ip->ip6, sizeof(struct in6_addr));
#endif
      previp = ip;
      continue;
    }

    DBG("WG: found ip\n");

    if (!previp) {
      DBG("WG: remove first\n");
      peer->first_allowedip = ip->next_allowedip;
      if (peer->last_allowedip == ip) {
	peer->last_allowedip = NULL;
	peer->first_allowedip = NULL;
      }
    } else {
      DBG("WG: remove middle\n");
      // Remove
      if (peer->last_allowedip == ip)
	peer->last_allowedip = previp;

      previp->next_allowedip = ip->next_allowedip;
    }

    free(ip);
    return true;
  }

  return false;
}

static void
wg_rt_notify(struct proto *P, struct channel *CH, struct network *n,
  struct rte *new, struct rte *old UNUSED)
{
  struct wg_proto *p = (struct wg_proto *) P;
  struct wg_config *c = (struct wg_config *) P->cf;
  struct wg_channel *ch = (struct wg_channel *) CH;
  struct iface *iface = NULL;
  const char *ifname = NULL;

//  DBG("WG: notify\n");

  if (new) {
    struct nexthop *nh = &new->attrs->nh;
    iface = nh->iface;
    ifname = iface ? iface->name : NULL;

    if (iface && iface == p->iface) {
      struct eattr *t;

      DBG("WG: found %p iface %I %p\n", new->attrs, nh->gw, nh->next);

      struct hostentry *he = new->attrs->hostentry;

      DBG("WG: he %p src %p\n", he, he?he->src:NULL);
      if (he && he->src) {
	struct eattr *t = ea_find(he->src->eattrs, EA_CODE(PROTOCOL_BGP, BA_TUNNEL_ENCAP));
	DBG("WG: he %p %I %I %p %p %I\n", t, he->addr, he->link, he->next, he->src->hostentry, he->src->nh.gw);
      }

      DBG("WG: notify new %d %N\n",
        new->attrs->dest, n->n.addr);

      bool is_tunnel_ep = false;
      struct tunnel_encap encap;
      memset(&encap, 0, sizeof(encap));
      encap.ep.ip = IPA_NONE;

      t = ea_find(new->attrs->eattrs, EA_CODE(PROTOCOL_BGP, BA_TUNNEL_ENCAP));
      if (t) {
	WG_TRACE(D_EVENTS, "WG: Set is tunnel");
	is_tunnel_ep = true;
      }
      if (!t && he && he->src) {
	t = ea_find(he->src->eattrs, EA_CODE(PROTOCOL_BGP, BA_TUNNEL_ENCAP));
      }
      if (t && t->u.ptr && decode_tunnel_encap(t, &encap, P->pool) == 0 && encap.type == c->tunnel_type && encap.encap_len == sizeof(wg_key)) {
	const wg_key *pubkey = encap.encap;
	bool add_ip = true;
	struct wg_entry *en = fib_find(&ch->rtable, n->n.addr);

	if (en) {
	  if (memcpy(en->public_key, pubkey, sizeof(wg_key) == 0)) {
	    add_ip = false;
	  }
	} else {
	  struct wg_entry *en = fib_get(&ch->rtable, n->n.addr);
	  en->is_tunnel_ep = is_tunnel_ep;
	  memcpy(en->public_key, pubkey, sizeof(wg_key));
	}

	WG_TRACE(D_EVENTS, "WG:     Attr %x %x %d %04x", t->flags, t->type, t->u.ptr->length, encap.flags);

	struct wg_device *dev = p->dev;

	if (dev != NULL) {
	  bool dirty = false;
	  bool found = false;
	  struct wg_peer *peer = NULL;
	  wg_for_each_peer(dev, peer) {
	    // Look for public key
	    size_t len = 32; // FIXME
	    // MIN(32, t->u.ptr->length)
	    if (memcmp(peer->public_key, pubkey, len) != 0) {
	      WG_TRACE(D_EVENTS, "WG:     Not found");
	      continue;
	    }

	    WG_TRACE(D_EVENTS, "WG:     Found");
	    found = true;
	    dirty = true;
	    break;
	  }

	  if (!found) {
	    peer = add_peer(dev, *pubkey);
	  }

	  dump_peer(peer);
	  if (is_tunnel_ep)
	    set_peer_tunnel_ep(p, peer, encap.ep.ip, encap.udp_dest_port);
	  if (add_ip) {
	    struct wg_allowedip *allowed_n =
	      create_allowed_ip_network(ch->c.net_type, n);
	    add_allowed_ip(allowed_n, peer);
	  }
	  dirty = true;

	  if (dirty) {
	    int res = set_device(p);
	    WG_TRACE(D_EVENTS, "WG: wg_set_device %d", res);
	  }
	}

	
/*
	struct sub_tlv_remove_endpoint {
		u32 asn;
		u8 address_family;
		union {
			ip4_addr ip4;
			ip6_addr ip6;
		};
	};

	struct sub_tlv_udp_dest_port {
		u16 port;
	};

	struct sub_tlv_wireguard {
		u8 pubkey[32];
	};

	struct sub_tlv {
		u8 type;
		union {
			struct {
				u8 length;
				struct sub_tlv_value value[];
			} s8;
			struct {
				u16 length;
				struct sub_tlv_value value[];
			} s16;
		} u;
	};

	struct bgp_tunnel_encap_attr {
		u16 type;
		u16 length;
		struct sub_tlv stlv[];
	};
*/
      } else {
        WG_TRACE(D_EVENTS, "WG:     No Attr");
      }

      mb_free(encap.encap);
      encap.encap = NULL;

  //  old_metric = en->valid ? en->metric : -1;

//  en->valid = RIP_ENTRY_VALID;
//  en->metric = rt_metric;
//  en->tag = rt_tag;
//  en->from = (new->attrs->src->proto == P) ? new->u.rip.from : NULL;
//  en->iface = new->attrs->iface;
//  en->next_hop = new->attrs->gw;
      }

  } else {
    DBG("WG: notify withdraw %N\n",
	  n->n.addr);

    /* Withdraw */
    struct wg_entry *en = fib_find(&ch->rtable, n->n.addr);

    if (!en) { // || en->valid != RIP_ENTRY_VALID)
      DBG("WG: fib not found\n");
      return;
    }

	struct wg_device *dev = p->dev;

	if (dev != NULL) {
	  bool marked_peer = false;
	  bool found = false;
	  struct wg_peer *peer = NULL;
	  wg_for_each_peer(dev, peer) {
	    if (en->is_tunnel_ep && !marked_peer) {
	      WG_TRACE(D_EVENTS, "WG: Is tunnel");
	      if (memcmp(peer->public_key, en->public_key, sizeof(wg_key)) == 0) {
		struct peer_config *pc = NULL;
		bool remove_me = true;
		WALK_LIST(pc,c->peers)
		{
		  wg_key pc_key;
		  if (pc->public_key) {
		    wg_key_from_base64(pc_key, pc->public_key);
		    if (memcmp(pc_key, peer->public_key, sizeof(wg_key)) == 0) {
		      /* Don't remove preconfigured peer */
		      remove_me = false;
		      break;
		    }
		  }
		}

		if (remove_me) {
		  WG_TRACE(D_EVENTS, "WG: Remove peer");
		  peer->flags |= WGPEER_REMOVE_ME;
		  marked_peer = true;
		  continue;
		}
	      }
	    }
	    // Remove from all peers
	    found = true;

	    if (found) {
	      struct wg_allowedip *allowedip = alloca(sizeof(struct wg_allowedip));
	      init_allowed_ip(allowedip, ch->c.net_type, n);
	      dump_peer(peer);
	      if (remove_allowed_ip(peer, allowedip)) {
		ip_addr ip = allowedip_to_ipa(allowedip);
		WG_TRACE(D_EVENTS, "WG: removed %I/%d", ip, allowedip->cidr);
		peer->flags |= WGPEER_REPLACE_ALLOWEDIPS;

		dump_peer(peer);
	      }
	    }
	  }

	  if (marked_peer) {
	    remove_marked_peer(p);
	  }
	  int res = set_device(p);
	  WG_TRACE(D_EVENTS, "WG: wg_set_device %d", res);

	  fib_delete(&ch->rtable, en);
	  en = NULL;
	  /*
    old_metric = en->metric;

    en->valid = RIP_ENTRY_STALE;
    en->metric = p->infinity;
    en->tag = 0;
    en->from = NULL;
    en->iface = NULL;
    en->next_hop = IPA_NONE;
*/
	}
  }

//  TRACE(D_EVENTS, "wg notify %s %s", src_table->name, ifname?ifname:"(null)");
}

static void
wg_reload_routes(struct channel *C)
{
  struct wg_proto *p UNUSED = (struct wg_proto *) C->proto;

  DBG("reload routes\n");

// TODO
//  WALK_LIST(c, p->channels)
//    channel_request_feeding(c);
}


static struct proto *
wg_init(struct proto_config *C)
{
  struct wg_config *c UNUSED = (struct wg_config *) C;
  struct proto *P = proto_new(C);
  struct wg_proto *p UNUSED = (struct wg_proto *) P;

  DBG("init\n");

  P->if_notify = wg_if_notify;
  P->rt_notify = wg_rt_notify;
  P->reload_routes = wg_reload_routes;
//  P->accept_ra_types = RA_ANY;

  /* Add all channels */
  struct wg_channel_config *cc;
  WALK_LIST(cc, C->channels)
    proto_add_channel(P, &cc->c);

  return P;
}


static int
wg_start(struct proto *P)
{
  struct wg_config *cf UNUSED = (struct wg_config *) P->cf;
  struct wg_proto *p = (struct wg_proto *) P;

  WG_TRACE(D_EVENTS, "WG: start");

  if (get_device(p, &p->dev, cf->ifname) >= 0)
    {
      int res = set_device(p);
      WG_TRACE(D_EVENTS, "WG: wg_set_device %d", res);
    }

  struct wg_channel *ch;
  WALK_LIST(ch,p->p.channels) {
    fib_init(&ch->rtable, P->pool, ch->c.net_type, sizeof(struct wg_entry),
	     OFFSETOF(struct wg_entry, n), 0, wg_init_entry);
  }
  return PS_UP;
}

static int
wg_shutdown(struct proto *P)
{
  struct wg_config *cf =  (struct wg_config*)P->cf;
  struct wg_proto *p = (struct wg_proto*)P;

  WG_TRACE(D_EVENTS, "WG: wg_shutdown");
  if (get_device(p, &p->dev, cf->ifname) >= 0)
    {
      int res = set_device(p);
      WG_TRACE(D_EVENTS, "WG: flush wg_set_device %d", res);
    }

  return PS_DOWN;
}

static void
wg_dump(struct proto *P)
{
  struct wg_proto *p = (struct wg_proto *) P;
  int i;

  i = 0;

  struct wg_channel *ch;
  WALK_LIST(ch,p->p.channels) {
    FIB_WALK(&ch->rtable, struct wg_entry, en)
      {
	//    struct wg_entry *en = (struct wg_entry *) e;
	DBG("WG: entry #%d:\n",
	      i++);
      }
    FIB_WALK_END;
  }

  struct wg_peer *peer = NULL;

  WG_TRACE(D_EVENTS, "WG: dump peers");
  wg_for_each_peer(p->dev, peer) {
    dump_peer(peer);
  }
}

static void
wg_copy_config(struct proto_config *DEST, struct proto_config *SRC)
{
  struct wg_config *dest = (struct wg_config *)DEST;
  struct wg_config *src = (struct wg_config *)SRC;

  dest->ifname = src->ifname;
  dest->socket_path = src->socket_path;
  dest->private_key = src->private_key;
  dest->listen_port = src->listen_port;

  struct peer_config *spc = NULL;
  WALK_LIST(spc,src->peers)
    {
      struct peer_config *dpc = cfg_allocz(sizeof(struct peer_config));
      dpc->public_key = spc->public_key;
      dpc->listen_port = spc->listen_port;
      dpc->endpoint = spc->endpoint;
      dpc->remote_port = spc->remote_port;
      dpc->allowedip = spc->allowedip;
      add_tail(&dest->peers, (node*)spc);
    }
}

struct peer_config *peer_new(struct wg_config *c)
{
  struct peer_config *pc = cfg_allocz(sizeof(struct peer_config));
  DBG("peer_new %p\n", pc);
  add_tail(&c->peers, (node*)pc);
  return pc;
}


static void
wg_channel_init(struct channel *CH, struct channel_config *CHC UNUSED)
{
  struct proto *P = CH->proto;
  struct wg_proto *p = (struct wg_proto *) P;

  /* Create new instance */
  WG_TRACE(D_EVENTS, "WG: wg_channel_init");
}

static int
wg_channel_reconfigure(struct channel *CH, struct channel_config *CHC UNUSED,
		       int *import_changed UNUSED, int *export_changed UNUSED)
{
  struct proto *P = CH->proto;
  struct wg_proto *p = (struct wg_proto *) P;

  /* Try to reconfigure instance, returns success */
  WG_TRACE(D_EVENTS, "WG: wg_channel_reconfigure");
  return 1;
}

static int
wg_channel_start(struct channel *CH)
{
  struct wg_channel *ch UNUSED = (struct wg_channel*)CH;
  struct proto *P = CH->proto;
  struct wg_proto *p = (struct wg_proto *) P;

  /* Start the instance */
  WG_TRACE(D_EVENTS, "WG: wg_channel_start");
#if 0
  fib_init(&ch->rtable, P->pool, ch->c.net_type, sizeof(struct wg_entry),
	   OFFSETOF(struct wg_entry, n), 0, wg_init_entry);
#endif
  return 1;
}

static void
wg_channel_shutdown(struct channel *CH)
{
  struct wg_channel *ch UNUSED = (struct wg_channel*)CH;
  struct proto *P = CH->proto;
  struct wg_proto *p = (struct wg_proto *) P;

  /* Stop the instance */
  WG_TRACE(D_EVENTS, "WG: wg_channel_shutdown");
}

static void
wg_channel_cleanup(struct channel *CH)
{
  struct wg_channel *ch UNUSED = (struct wg_channel*)CH;
  struct proto *P = CH->proto;
  struct wg_proto *p = (struct wg_proto *) P;

  /* Channel finished flush */
  WG_TRACE(D_EVENTS, "WG: wg_channel_cleanup");
}


struct channel_class channel_wg = {
  .channel_size =       sizeof(struct wg_channel),
  .config_size =        sizeof(struct wg_channel_config),
  .init =               wg_channel_init,
  .start =              wg_channel_start,
  .shutdown =           wg_channel_shutdown,
  .cleanup =            wg_channel_cleanup,
  .reconfigure =        wg_channel_reconfigure,
};

struct protocol proto_wireguard = {
  .name =		"Wireguard",
  .template =		"wg%d",
  .class =              PROTOCOL_WG,
  .channel_mask =       NB_IP,
  .proto_size =         sizeof(struct wg_proto),
  .config_size =	sizeof(struct wg_config),
  .postconfig =		wg_postconfig,
  .init =		wg_init,
  .start =		wg_start,
  .shutdown =		wg_shutdown,
  .dump = 		wg_dump,
  .copy_config = 	wg_copy_config,
/*  .multitable =		1,
  .preference =		DEF_PREF_PIPE,
  .cleanup =		wg_cleanup,
  .reconfigure =	wg_reconfigure,
  .copy_config = 	wg_copy_config,
  .get_status = 	wg_get_status,
  .show_proto_info = 	wg_show_proto_info*/
};