File: nll2tp.go

package info (click to toggle)
golang-github-katalix-go-l2tp 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: ansic: 127; sh: 10; makefile: 4
file content (735 lines) | stat: -rw-r--r-- 19,967 bytes parent folder | download
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
package nll2tp

import (
	"errors"
	"fmt"
	"sync"

	"github.com/mdlayher/genetlink"
	"github.com/mdlayher/netlink"
	"github.com/mdlayher/netlink/nlenc"
)

// L2tpProtocolVersion describes the RFC version of the tunnel:
// L2TPv2 is described by RFC2661, while L2TPv3 is described by
// RFC3931.
type L2tpProtocolVersion uint32

// L2tpTunnelID represents the numeric identifier of an L2TP tunnel.
// This ID is used in L2TP control and data packet headers and AVPs,
// and is unique to the host.
type L2tpTunnelID uint32

// L2tpSessionID represents the numeric identifier of an L2TP session.
// This ID is used in L2TP control and data packet headers and AVPs,
// and is unique to the tunnel for L2TPv2, or the host for L2TPv3.
type L2tpSessionID uint32

const (
	// ProtocolVersion2 specifies L2TPv2 RFC2661
	ProtocolVersion2 = 2
	// ProtocolVersion3 specifies L2TPv3 RFC3931
	ProtocolVersion3 = 3
)

// TunnelConfig encapsulates genetlink parameters for L2TP tunnel commands.
type TunnelConfig struct {
	// Tid is the host's L2TP ID for the tunnel.
	Tid L2tpTunnelID
	// Ptid is the peer's L2TP ID for the tunnel
	Ptid L2tpTunnelID
	// Version is the tunnel protocol version (L2TPv2 or L2TPv3)
	Version L2tpProtocolVersion
	// Encap specifies the tunnel encapsulation type.
	// For L2TPv3 this may be UDP or IP.
	// For L2TPv2 this may only be UDP.
	Encap L2tpEncapType
	// DebugFlags specifies the kernel debugging flags to use for the tunnel instance.
	DebugFlags L2tpDebugFlags
}

// SessionConfig encapsulates genetlink parameters for L2TP session commands.
type SessionConfig struct {
	// Tid is the host's L2TP ID for the tunnel containing the session.
	Tid L2tpTunnelID
	// Ptid is the peer's L2TP ID for the tunnel containing the session.
	Ptid L2tpTunnelID
	// Sid is the host's L2TP ID for the session.
	Sid L2tpSessionID
	// Psid is the peer's L2TP ID for the session.
	Psid L2tpSessionID
	// PseudowireType specifies the type of traffic carried by the session.
	// For L2TPv3 this may be PPP or Ethernet.
	// For L2TPv2 this may be PPP only.
	PseudowireType L2tpPwtype
	// SendSeq controls whether to send data packet sequence numbers per RFC2661 section 5.4.
	SendSeq bool
	// RecvSeq if set will cause data packets without sequence numbers to be dropped.
	RecvSeq bool
	// IsLNS if unset allows the LNS to enable data packet sequence numbers per RFC2661 section 5.4
	IsLNS bool
	// ReorderTimeout sets the maximum amount of time, in milliseconds, to hold a data packet
	// in the reorder queue when sequence numbers are enabled.
	ReorderTimeout uint64
	// LocalCookie sets the RFC3931 cookie for the session.
	// Transmitted data packets will include the cookie.
	// The LocalCookie may be either 4 or 8 bytes in length if set.
	LocalCookie []byte
	// PeerCookie sets the RFC3931 peer cookie for the session as negotiated by the control protocol.
	// Received data packets with a cookie mismatch are discarded.
	// The PeerCookie may be either 4 or 8 bytes in length if set.
	PeerCookie []byte
	// IfName use depends on the pseudowire type.
	// For an RFC3931 Ethernet pseudowire, IfName specifies the interface name to use for
	// the L2TP Ethernet interface.  By default the kernel generates a name "l2tpethX".
	// For an RFC2661 PPP/AC pseudowire, IfName specifies the name of the interface associated
	// with the PPPoE session.
	IfName string
	// L2SpecType specifies the Layer 2 specific sublayer field to be used in data packets
	// as per RFC3931 section 3.2.2
	L2SpecType L2tpL2specType
	// DebugFlags specifies the kernel debugging flags to use for the session instance.
	DebugFlags L2tpDebugFlags
}

// SessionStatistics includes statistics on dataplane receive and transmit.
type SessionStatistics struct {
	// TxPacketCount is the number of data packets the session has transmitted.
	TxPacketCount uint64
	// TxBytes is the number of data bytes the session has transmitted.
	TxBytes uint64
	// TxErrorCount is the number of transmission errors the session has recorded.
	TxErrorCount uint64
	// RxPacketCount is the number of data packets the session has received.
	RxPacketCount uint64
	// RxBytes is the number of data bytes the session has received.
	RxBytes uint64
	// RxErrorCount is the number of receive errors the session has recorded.
	RxErrorCount uint64
	// RxSeqDiscardCount is the number of packets the session has discarded due to sequence errors.
	// For example, if the session is in LNS mode, has requested sequence numbers, and the client
	// isn't sending them.
	RxSeqDiscardCount uint64
	// RxOOSCount is the number of packets the session has received out of sequence if data packet
	// reordering is enabled.
	RxOOSCount uint64
}

// SessionInfo encapsulates dataplane session information provided by the kernel.
type SessionInfo struct {
	// Tid is the host's L2TP ID for the tunnel containing the session.
	Tid L2tpTunnelID
	// Ptid is the peer's L2TP ID for the tunnel containing the session.
	Ptid L2tpTunnelID
	// Sid is the host's L2TP ID for the session.
	Sid L2tpSessionID
	// Psid is the peer's L2TP ID for the session.
	Psid L2tpSessionID
	// IfName is the assigned interface name for this session.
	IfName string
	// LocalCookie is the RFC3931 cookie for the session.
	LocalCookie []byte
	// PeerCookie is the RFC3931 peer cookie for the session.
	PeerCookie []byte
	// SendSeq is true if session is sending data packet sequence numbers per RFC2661 section 5.4.
	SendSeq bool
	// RecvSeq is true if session is dropping data packets received without sequence numbers.
	RecvSeq bool
	// LnsMode is true if the session is running as server.  If running as server
	// the session will not permit the peer to control data sequence number settings.
	LnsMode bool
	// UsingIPSec is true if the session is using IPSec.
	UsingIPSec bool
	// ReorderTimeout is the maximum amount of time to hold a data packet in the reorder
	// queue when sequence numbers are enabled.  This number is defined in milliseconds.
	ReorderTimeout uint64
	// Statistics is the current dataplane tx/rx stats.
	Statistics SessionStatistics
}

type msgRequest struct {
	msg    genetlink.Message
	family uint16
	flags  netlink.HeaderFlags
}

type msgResponse struct {
	msg []genetlink.Message
	err error
}

// Conn represents the genetlink L2TP connection to the kernel.
type Conn struct {
	genlFamily genetlink.Family
	c          *genetlink.Conn
	reqChan    chan *msgRequest
	rspChan    chan *msgResponse
	wg         sync.WaitGroup
}

// Dial creates a new genetlink L2TP connection to the kernel.
func Dial() (*Conn, error) {
	c, err := genetlink.Dial(nil)
	if err != nil {
		return nil, err
	}

	id, err := c.GetFamily(GenlName)
	if err != nil {
		c.Close()
		return nil, err
	}

	conn := &Conn{
		genlFamily: id,
		c:          c,
		reqChan:    make(chan *msgRequest),
		rspChan:    make(chan *msgResponse),
	}

	conn.wg.Add(1)
	go runConn(conn, &conn.wg)

	return conn, nil
}

// Close connection, releasing associated resources
func (c *Conn) Close() {
	close(c.reqChan)
	c.wg.Wait()
	c.c.Close()
}

// CreateManagedTunnel creates a new managed tunnel instance in the kernel.
// A "managed" tunnel is one whose tunnel socket fd is created and managed
// by a userspace process.  A managed tunnel's lifetime is bound by the lifetime
// of the tunnel socket fd, and may optionally be destroyed using explicit
// netlink commands.
func (c *Conn) CreateManagedTunnel(fd int, config *TunnelConfig) (err error) {
	if fd < 0 {
		return errors.New("managed tunnel needs a valid socket file descriptor")
	}

	attr, err := tunnelCreateAttr(config)
	if err != nil {
		return err
	}

	return c.createTunnel(append(attr, netlink.Attribute{
		Type: AttrFd,
		Data: nlenc.Uint32Bytes(uint32(fd)),
	}))
}

// CreateStaticTunnel creates a new static tunnel instance in the kernel.
// A "static" tunnel is one whose tunnel socket fd is implicitly created
// by the kernel.  A static tunnel must be explicitly deleted using netlink
// commands.
func (c *Conn) CreateStaticTunnel(
	localAddr []byte, localPort uint16,
	peerAddr []byte, peerPort uint16,
	config *TunnelConfig) (err error) {

	if config == nil {
		return errors.New("invalid nil tunnel config pointer")
	}
	if len(localAddr) == 0 {
		return errors.New("unmanaged tunnel needs a valid local address")
	}
	if len(peerAddr) == 0 {
		return errors.New("unmanaged tunnel needs a valid peer address")
	}
	if len(localAddr) != len(peerAddr) {
		return errors.New("local and peer IP addresses must be of the same address family")
	}
	if config.Encap == EncaptypeUdp {
		if localPort == 0 {
			return errors.New("unmanaged tunnel needs a valid local port")
		}
		if peerPort == 0 {
			return errors.New("unmanaged tunnel needs a valid peer port")
		}
	}

	attr, err := tunnelCreateAttr(config)
	if err != nil {
		return err
	}

	switch len(localAddr) {
	case 4:
		attr = append(attr, netlink.Attribute{
			Type: AttrIpSaddr,
			Data: localAddr,
		}, netlink.Attribute{
			Type: AttrIpDaddr,
			Data: peerAddr,
		})
	case 16:
		attr = append(attr, netlink.Attribute{
			Type: AttrIp6Saddr,
			Data: localAddr,
		}, netlink.Attribute{
			Type: AttrIp6Daddr,
			Data: peerAddr,
		})
	default:
		panic("unexpected address length")
	}

	return c.createTunnel(append(attr, netlink.Attribute{
		Type: AttrUdpSport,
		Data: nlenc.Uint16Bytes(localPort),
	}, netlink.Attribute{
		Type: AttrUdpDport,
		Data: nlenc.Uint16Bytes(peerPort),
	}))
}

// DeleteTunnel deletes a tunnel instance from the kernel.
// Deleting a tunnel instance implicitly destroys any sessions
// running in that tunnel.
func (c *Conn) DeleteTunnel(config *TunnelConfig) error {
	if config == nil {
		return errors.New("invalid nil tunnel config")
	}

	b, err := netlink.MarshalAttributes([]netlink.Attribute{
		{
			Type: AttrConnId,
			Data: nlenc.Uint32Bytes(uint32(config.Tid)),
		},
	})
	if err != nil {
		return err
	}

	req := genetlink.Message{
		Header: genetlink.Header{
			Command: CmdTunnelDelete,
			Version: c.genlFamily.Version,
		},
		Data: b,
	}

	_, err = c.execute(req, c.genlFamily.ID, netlink.Request|netlink.Acknowledge)
	return err
}

// CreateSession creates a session instance in the kernel.
// The parent tunnel instance referenced by the tunnel IDs in
// the session configuration must already exist in the kernel.
func (c *Conn) CreateSession(config *SessionConfig) error {
	attr, err := sessionCreateAttr(config)
	if err != nil {
		return err
	}

	b, err := netlink.MarshalAttributes(attr)
	if err != nil {
		return err
	}

	req := genetlink.Message{
		Header: genetlink.Header{
			Command: CmdSessionCreate,
			Version: c.genlFamily.Version,
		},
		Data: b,
	}

	_, err = c.execute(req, c.genlFamily.ID, netlink.Request|netlink.Acknowledge)
	return err
}

// DeleteSession deletes a session instance from the kernel.
func (c *Conn) DeleteSession(config *SessionConfig) error {
	if config == nil {
		return errors.New("invalid nil session config")
	}

	b, err := netlink.MarshalAttributes([]netlink.Attribute{
		{
			Type: AttrConnId,
			Data: nlenc.Uint32Bytes(uint32(config.Tid)),
		},
		{
			Type: AttrSessionId,
			Data: nlenc.Uint32Bytes(uint32(config.Sid)),
		},
	})
	if err != nil {
		return err
	}

	req := genetlink.Message{
		Header: genetlink.Header{
			Command: CmdSessionDelete,
			Version: c.genlFamily.Version,
		},
		Data: b,
	}

	_, err = c.execute(req, c.genlFamily.ID, netlink.Request|netlink.Acknowledge)
	return err
}

func (stats *SessionStatistics) decode(ad *netlink.AttributeDecoder) error {
	for ad.Next() {
		switch ad.Type() {
		case AttrTxPackets:
			stats.TxPacketCount = ad.Uint64()
		case AttrTxBytes:
			stats.TxBytes = ad.Uint64()
		case AttrTxErrors:
			stats.TxErrorCount = ad.Uint64()
		case AttrRxPackets:
			stats.RxPacketCount = ad.Uint64()
		case AttrRxBytes:
			stats.RxBytes = ad.Uint64()
		case AttrRxErrors:
			stats.RxErrorCount = ad.Uint64()
		case AttrRxSeqDiscards:
			stats.RxSeqDiscardCount = ad.Uint64()
		case AttrRxOosPackets:
			stats.RxOOSCount = ad.Uint64()
		}
	}
	return nil
}

func sessionInfo_decode(data []byte) (*SessionInfo, error) {

	ad, err := netlink.NewAttributeDecoder(data)
	if err != nil {
		return nil, fmt.Errorf("failed to create attribute decoder: %v", err)
	}

	var info SessionInfo
	for ad.Next() {
		switch ad.Type() {
		case AttrConnId:
			info.Tid = L2tpTunnelID(ad.Uint32())
		case AttrPeerConnId:
			info.Ptid = L2tpTunnelID(ad.Uint32())
		case AttrSessionId:
			info.Sid = L2tpSessionID(ad.Uint32())
		case AttrPeerSessionId:
			info.Psid = L2tpSessionID(ad.Uint32())
		case AttrIfname:
			info.IfName = ad.String()
		case AttrCookie:
			info.LocalCookie = ad.Bytes()
		case AttrPeerCookie:
			info.PeerCookie = ad.Bytes()
		case AttrSendSeq:
			info.SendSeq = ad.Uint8() != 0
		case AttrRecvSeq:
			info.RecvSeq = ad.Uint8() != 0
		case AttrLnsMode:
			info.LnsMode = ad.Uint8() != 0
		case AttrUsingIpsec:
			info.UsingIPSec = ad.Uint8() != 0
		case AttrRecvTimeout:
			info.ReorderTimeout = ad.Uint64()
		case AttrStats:
			ad.Nested(info.Statistics.decode)
		}
	}

	if err = ad.Err(); err != nil {
		return nil, fmt.Errorf("failed to decode attributes: %v", err)
	}

	return &info, nil
}

// GetSessionInfo retrieves dataplane session information from the kernel.
func (c *Conn) GetSessionInfo(config *SessionConfig) (*SessionInfo, error) {
	if config == nil {
		return nil, errors.New("invalid nil session config")
	}

	b, err := netlink.MarshalAttributes([]netlink.Attribute{
		{
			Type: AttrConnId,
			Data: nlenc.Uint32Bytes(uint32(config.Tid)),
		},
		{
			Type: AttrSessionId,
			Data: nlenc.Uint32Bytes(uint32(config.Sid)),
		},
	})
	if err != nil {
		return nil, err
	}

	req := genetlink.Message{
		Header: genetlink.Header{
			Command: CmdSessionGet,
			Version: c.genlFamily.Version,
		},
		Data: b,
	}

	msgs, err := c.execute(req, c.genlFamily.ID, netlink.Request)
	if err != nil {
		return nil, err
	}

	info := SessionInfo{}
	for _, rsp := range msgs {
		if rsp.Header.Command != CmdSessionGet {
			continue
		}

		attributes, err := netlink.UnmarshalAttributes(rsp.Data)
		if err != nil {
			return nil, err
		}

		for _, a := range attributes {
			switch a.Type {
			}
		}
	}
	return &info, nil
}

func (c *Conn) createTunnel(attr []netlink.Attribute) error {
	b, err := netlink.MarshalAttributes(attr)
	if err != nil {
		return err
	}

	req := genetlink.Message{
		Header: genetlink.Header{
			Command: CmdTunnelCreate,
			Version: c.genlFamily.Version,
		},
		Data: b,
	}

	_, err = c.execute(req, c.genlFamily.ID, netlink.Request|netlink.Acknowledge)
	return err
}

func (c *Conn) execute(msg genetlink.Message, family uint16, flags netlink.HeaderFlags) ([]genetlink.Message, error) {
	c.reqChan <- &msgRequest{
		msg:    msg,
		family: family,
		flags:  flags,
	}

	rsp, ok := <-c.rspChan
	if !ok {
		return nil, errors.New("netlink connection closed")
	}

	return rsp.msg, rsp.err
}

func tunnelCreateAttr(config *TunnelConfig) ([]netlink.Attribute, error) {

	// Basic error checking
	if config == nil {
		return nil, errors.New("invalid nil tunnel config")
	}
	if config.Tid == 0 {
		return nil, errors.New("tunnel config must have a non-zero tunnel ID")
	}
	if config.Ptid == 0 {
		return nil, errors.New("tunnel config must have a non-zero peer tunnel ID")
	}
	if config.Version < ProtocolVersion2 || config.Version > ProtocolVersion3 {
		return nil, fmt.Errorf("invalid tunnel protocol version %d", config.Version)
	}
	if config.Encap != EncaptypeUdp && config.Encap != EncaptypeIp {
		return nil, errors.New("invalid tunnel encap (expect IP or UDP)")
	}

	// Version-specific checks
	if config.Version == ProtocolVersion2 {
		if config.Tid > 65535 {
			return nil, errors.New("L2TPv2 tunnel ID can't exceed 16-bit limit")
		}
		if config.Ptid > 65535 {
			return nil, errors.New("L2TPv2 peer tunnel ID can't exceed 16-bit limit")
		}
		if config.Encap != EncaptypeUdp {
			return nil, errors.New("L2TPv2 only supports UDP encapsuation")
		}
	}

	return []netlink.Attribute{
		{
			Type: AttrConnId,
			Data: nlenc.Uint32Bytes(uint32(config.Tid)),
		},
		{
			Type: AttrPeerConnId,
			Data: nlenc.Uint32Bytes(uint32(config.Ptid)),
		},
		{
			Type: AttrProtoVersion,
			Data: nlenc.Uint8Bytes(uint8(config.Version)),
		},
		{
			Type: AttrEncapType,
			Data: nlenc.Uint16Bytes(uint16(config.Encap)),
		},
		{
			Type: AttrDebug,
			Data: nlenc.Uint32Bytes(uint32(config.DebugFlags)),
		},
	}, nil
}

func sessionCreateAttr(config *SessionConfig) ([]netlink.Attribute, error) {

	// Sanity checks
	if config == nil {
		return nil, errors.New("invalid nil session config")
	}
	if config.Tid == 0 {
		return nil, errors.New("session config must have a non-zero parent tunnel ID")
	}
	if config.Ptid == 0 {
		return nil, errors.New("session config must have a non-zero parent peer tunnel ID")
	}
	if config.Sid == 0 {
		return nil, errors.New("session config must have a non-zero session ID")
	}
	if config.Psid == 0 {
		return nil, errors.New("session config must have a non-zero peer session ID")
	}
	if config.PseudowireType == PwtypeNone {
		return nil, errors.New("session config must have a valid pseudowire type")
	}
	if len(config.LocalCookie) > 0 {
		if len(config.LocalCookie) != 4 && len(config.LocalCookie) != 8 {
			return nil, fmt.Errorf("session config has peer cookie of %d bytes: valid lengths are 4 or 8 bytes",
				len(config.LocalCookie))
		}
	}
	if len(config.PeerCookie) > 0 {
		if len(config.PeerCookie) != 4 && len(config.PeerCookie) != 8 {
			return nil, fmt.Errorf("session config has peer cookie of %d bytes: valid lengths are 4 or 8 bytes",
				len(config.PeerCookie))
		}
	}

	attr := []netlink.Attribute{
		{
			Type: AttrConnId,
			Data: nlenc.Uint32Bytes(uint32(config.Tid)),
		},
		{
			Type: AttrPeerConnId,
			Data: nlenc.Uint32Bytes(uint32(config.Ptid)),
		},
		{
			Type: AttrSessionId,
			Data: nlenc.Uint32Bytes(uint32(config.Sid)),
		},
		{
			Type: AttrPeerSessionId,
			Data: nlenc.Uint32Bytes(uint32(config.Psid)),
		},
	}

	// VLAN pseudowires use the kernel l2tp_eth driver
	if config.PseudowireType == PwtypeEthVlan {
		attr = append(attr, netlink.Attribute{
			Type: AttrPwType,
			Data: nlenc.Uint16Bytes(uint16(PwtypeEth)),
		})
	} else {
		attr = append(attr, netlink.Attribute{
			Type: AttrPwType,
			Data: nlenc.Uint16Bytes(uint16(config.PseudowireType)),
		})
	}

	if config.SendSeq {
		attr = append(attr, netlink.Attribute{
			Type: AttrSendSeq,
			Data: nlenc.Uint8Bytes(1),
		})
	}

	if config.RecvSeq {
		attr = append(attr, netlink.Attribute{
			Type: AttrRecvSeq,
			Data: nlenc.Uint8Bytes(1),
		})
	}

	if (config.SendSeq || config.RecvSeq) && config.IsLNS {
		attr = append(attr, netlink.Attribute{
			Type: AttrLnsMode,
			Data: nlenc.Uint8Bytes(1),
		})
	}

	if config.ReorderTimeout > 0 {
		attr = append(attr, netlink.Attribute{
			Type: AttrRecvTimeout,
			Data: nlenc.Uint64Bytes(config.ReorderTimeout),
		})
	}

	if len(config.LocalCookie) > 0 {
		attr = append(attr, netlink.Attribute{
			Type: AttrCookie,
			Data: config.LocalCookie,
		})
	}

	if len(config.PeerCookie) > 0 {
		attr = append(attr, netlink.Attribute{
			Type: AttrCookie,
			Data: config.PeerCookie,
		})
	}

	if config.IfName != "" {
		attr = append(attr, netlink.Attribute{
			Type: AttrIfname,
			Data: nlenc.Bytes(config.IfName),
		})
	}

	attr = append(attr, netlink.Attribute{
		Type: AttrL2specType,
		Data: nlenc.Uint8Bytes(uint8(config.L2SpecType)),
	})

	switch config.L2SpecType {
	case L2spectypeNone:
		attr = append(attr, netlink.Attribute{
			Type: AttrL2specLen,
			Data: nlenc.Uint8Bytes(0),
		})
	case L2spectypeDefault:
		attr = append(attr, netlink.Attribute{
			Type: AttrL2specLen,
			Data: nlenc.Uint8Bytes(4),
		})
	default:
		return nil, fmt.Errorf("unhandled L2 Spec Type %v", config.L2SpecType)
	}

	return attr, nil
}

func runConn(c *Conn, wg *sync.WaitGroup) {
	defer wg.Done()
	for req := range c.reqChan {
		m, err := c.c.Execute(req.msg, req.family, req.flags)
		c.rspChan <- &msgResponse{
			msg: m,
			err: err,
		}
	}
}