From 7d6b9345600c0fd8bf4c8879f5afae8f927e6490 Mon Sep 17 00:00:00 2001 From: mame82 Date: Tue, 8 May 2018 15:57:12 +0000 Subject: [PATCH] Started with dnsmasq config RPC, Makefile, systemd service file --- Makefile | 27 +++++ P4wnP1.service | 20 ++++ proto/grpc.pb.go | 283 +++++++++++++++++++++++++++++++++++---------- proto/grpc.proto | 47 +++++++- service/DHCPSrv.go | 109 +++++++++++++++++ service/network.go | 6 +- service/usb.go | 2 + 7 files changed, 433 insertions(+), 61 deletions(-) create mode 100644 Makefile create mode 100644 P4wnP1.service create mode 100644 service/DHCPSrv.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..554562e --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +all: build + +build: + go build P4wnP1_service.go + go build P4wnP1_cli.go + +install: + cp P4wnP1_service /usr/local/bin/ + cp P4wnP1_cli /usr/local/bin/ + cp P4wnP1.service /etc/systemd/system/P4wnP1.service + # reinit service daemon + systemctl daemon-reload + # enable service + systemctl enable P4wnP1.service + # start service + service P4wnP1 start + +remove: + # stop service + service P4wnP1 stop + # disable service + systemctl disable P4wnP1.service + rm -f /usr/local/bin/P4wnP1_service + rm -f /usr/local/bin/P4wnP1_cli + rm -f /etc/systemd/system/P4wnP1.service + # reinit service daemon + systemctl daemon-reload diff --git a/P4wnP1.service b/P4wnP1.service new file mode 100644 index 0000000..0b66172 --- /dev/null +++ b/P4wnP1.service @@ -0,0 +1,20 @@ +[Unit] +Description=P4wnP1 Startup Service +#After=systemd-modules-load.service +After=local-fs.target +DefaultDependencies=no +Before=sysinit.target + +[Service] +Type=simple +#Type=forking +#RemainAfterExit=yes +ExecStart=/usr/local/bin/P4wnP1_service +#StandardOutput=journal+console +#StandardError=journal+console +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=multi-user.target +#WantedBy=sysinit.target diff --git a/proto/grpc.pb.go b/proto/grpc.pb.go index a8f27da..bf70053 100644 --- a/proto/grpc.pb.go +++ b/proto/grpc.pb.go @@ -12,6 +12,9 @@ It has these top-level messages: GadgetSettings GadgetSettingsEthernet GadgetSettingsUMS + DHCPServerSettings + DHCPServerRange + DHCPServerStaticHost WiFiSettings Empty */ @@ -59,7 +62,7 @@ var WiFiSettings_Mode_value = map[string]int32{ func (x WiFiSettings_Mode) String() string { return proto.EnumName(WiFiSettings_Mode_name, int32(x)) } -func (WiFiSettings_Mode) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{4, 0} } +func (WiFiSettings_Mode) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{7, 0} } type WiFiSettings_APAuthMode int32 @@ -80,8 +83,9 @@ var WiFiSettings_APAuthMode_value = map[string]int32{ func (x WiFiSettings_APAuthMode) String() string { return proto.EnumName(WiFiSettings_APAuthMode_name, int32(x)) } -func (WiFiSettings_APAuthMode) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{4, 1} } +func (WiFiSettings_APAuthMode) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{7, 1} } +// LED type LEDSettings struct { BlinkCount uint32 `protobuf:"varint,1,opt,name=blink_count,json=blinkCount" json:"blink_count,omitempty"` } @@ -98,6 +102,7 @@ func (m *LEDSettings) GetBlinkCount() uint32 { return 0 } +// USB Gadget type GadgetSettings struct { Enabled bool `protobuf:"varint,1,opt,name=enabled" json:"enabled,omitempty"` Vid string `protobuf:"bytes,2,opt,name=vid" json:"vid,omitempty"` @@ -282,6 +287,147 @@ func (m *GadgetSettingsUMS) GetFile() string { return "" } +// DHCP +type DHCPServerSettings struct { + ListenPort uint32 `protobuf:"varint,1,opt,name=listenPort" json:"listenPort,omitempty"` + ListenInterface string `protobuf:"bytes,2,opt,name=listenInterface" json:"listenInterface,omitempty"` + LeaseFile string `protobuf:"bytes,3,opt,name=leaseFile" json:"leaseFile,omitempty"` + NotAuthoritative bool `protobuf:"varint,4,opt,name=notAuthoritative" json:"notAuthoritative,omitempty"` + DoNotBindInterface bool `protobuf:"varint,5,opt,name=doNotBindInterface" json:"doNotBindInterface,omitempty"` + CallbackScript string `protobuf:"bytes,6,opt,name=callbackScript" json:"callbackScript,omitempty"` + Ranges []*DHCPServerRange `protobuf:"bytes,7,rep,name=ranges" json:"ranges,omitempty"` + // repeated DHCPServerOption options = 8; + Options map[uint32]string `protobuf:"bytes,8,rep,name=options" json:"options,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` + StaticHosts []*DHCPServerStaticHost `protobuf:"bytes,9,rep,name=staticHosts" json:"staticHosts,omitempty"` +} + +func (m *DHCPServerSettings) Reset() { *m = DHCPServerSettings{} } +func (m *DHCPServerSettings) String() string { return proto.CompactTextString(m) } +func (*DHCPServerSettings) ProtoMessage() {} +func (*DHCPServerSettings) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } + +func (m *DHCPServerSettings) GetListenPort() uint32 { + if m != nil { + return m.ListenPort + } + return 0 +} + +func (m *DHCPServerSettings) GetListenInterface() string { + if m != nil { + return m.ListenInterface + } + return "" +} + +func (m *DHCPServerSettings) GetLeaseFile() string { + if m != nil { + return m.LeaseFile + } + return "" +} + +func (m *DHCPServerSettings) GetNotAuthoritative() bool { + if m != nil { + return m.NotAuthoritative + } + return false +} + +func (m *DHCPServerSettings) GetDoNotBindInterface() bool { + if m != nil { + return m.DoNotBindInterface + } + return false +} + +func (m *DHCPServerSettings) GetCallbackScript() string { + if m != nil { + return m.CallbackScript + } + return "" +} + +func (m *DHCPServerSettings) GetRanges() []*DHCPServerRange { + if m != nil { + return m.Ranges + } + return nil +} + +func (m *DHCPServerSettings) GetOptions() map[uint32]string { + if m != nil { + return m.Options + } + return nil +} + +func (m *DHCPServerSettings) GetStaticHosts() []*DHCPServerStaticHost { + if m != nil { + return m.StaticHosts + } + return nil +} + +// Could be used for IPv4 or IPv6 (refer dnsmasq docs), nor field for prefix length (default is 64) +type DHCPServerRange struct { + RangeLower string `protobuf:"bytes,1,opt,name=rangeLower" json:"rangeLower,omitempty"` + RangeUpper string `protobuf:"bytes,2,opt,name=rangeUpper" json:"rangeUpper,omitempty"` + LeaseTime string `protobuf:"bytes,3,opt,name=leaseTime" json:"leaseTime,omitempty"` +} + +func (m *DHCPServerRange) Reset() { *m = DHCPServerRange{} } +func (m *DHCPServerRange) String() string { return proto.CompactTextString(m) } +func (*DHCPServerRange) ProtoMessage() {} +func (*DHCPServerRange) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{5} } + +func (m *DHCPServerRange) GetRangeLower() string { + if m != nil { + return m.RangeLower + } + return "" +} + +func (m *DHCPServerRange) GetRangeUpper() string { + if m != nil { + return m.RangeUpper + } + return "" +} + +func (m *DHCPServerRange) GetLeaseTime() string { + if m != nil { + return m.LeaseTime + } + return "" +} + +// Used for static leases +type DHCPServerStaticHost struct { + Mac string `protobuf:"bytes,1,opt,name=mac" json:"mac,omitempty"` + Ip string `protobuf:"bytes,2,opt,name=ip" json:"ip,omitempty"` +} + +func (m *DHCPServerStaticHost) Reset() { *m = DHCPServerStaticHost{} } +func (m *DHCPServerStaticHost) String() string { return proto.CompactTextString(m) } +func (*DHCPServerStaticHost) ProtoMessage() {} +func (*DHCPServerStaticHost) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{6} } + +func (m *DHCPServerStaticHost) GetMac() string { + if m != nil { + return m.Mac + } + return "" +} + +func (m *DHCPServerStaticHost) GetIp() string { + if m != nil { + return m.Ip + } + return "" +} + +// WiFi type WiFiSettings struct { Diasabled bool `protobuf:"varint,1,opt,name=diasabled" json:"diasabled,omitempty"` Reg string `protobuf:"bytes,2,opt,name=reg" json:"reg,omitempty"` @@ -299,7 +445,7 @@ type WiFiSettings struct { func (m *WiFiSettings) Reset() { *m = WiFiSettings{} } func (m *WiFiSettings) String() string { return proto.CompactTextString(m) } func (*WiFiSettings) ProtoMessage() {} -func (*WiFiSettings) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } +func (*WiFiSettings) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{7} } func (m *WiFiSettings) GetDiasabled() bool { if m != nil { @@ -384,13 +530,16 @@ type Empty struct { func (m *Empty) Reset() { *m = Empty{} } func (m *Empty) String() string { return proto.CompactTextString(m) } func (*Empty) ProtoMessage() {} -func (*Empty) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{5} } +func (*Empty) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{8} } func init() { proto.RegisterType((*LEDSettings)(nil), "P4wnP1_grpc.LEDSettings") proto.RegisterType((*GadgetSettings)(nil), "P4wnP1_grpc.GadgetSettings") proto.RegisterType((*GadgetSettingsEthernet)(nil), "P4wnP1_grpc.GadgetSettingsEthernet") proto.RegisterType((*GadgetSettingsUMS)(nil), "P4wnP1_grpc.GadgetSettingsUMS") + proto.RegisterType((*DHCPServerSettings)(nil), "P4wnP1_grpc.DHCPServerSettings") + proto.RegisterType((*DHCPServerRange)(nil), "P4wnP1_grpc.DHCPServerRange") + proto.RegisterType((*DHCPServerStaticHost)(nil), "P4wnP1_grpc.DHCPServerStaticHost") proto.RegisterType((*WiFiSettings)(nil), "P4wnP1_grpc.WiFiSettings") proto.RegisterType((*Empty)(nil), "P4wnP1_grpc.Empty") proto.RegisterEnum("P4wnP1_grpc.WiFiSettings_Mode", WiFiSettings_Mode_name, WiFiSettings_Mode_value) @@ -670,59 +819,75 @@ var _P4WNP1_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("grpc.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 850 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x55, 0x5d, 0x6f, 0xdb, 0x36, - 0x14, 0x8d, 0x13, 0x7f, 0xe9, 0xca, 0x76, 0x15, 0x76, 0xeb, 0xb4, 0x76, 0x6d, 0x03, 0xad, 0x03, - 0xf2, 0x30, 0x18, 0x68, 0xd6, 0xd7, 0x01, 0xd3, 0x2c, 0x25, 0xf6, 0x1a, 0xc5, 0x82, 0x54, 0xcf, - 0xd8, 0x13, 0xc1, 0x88, 0xac, 0x4d, 0xc4, 0xfa, 0x80, 0x44, 0x65, 0xeb, 0xfb, 0xfe, 0xe8, 0x1e, - 0xf6, 0x3f, 0x06, 0x52, 0xb2, 0x63, 0xb7, 0x6e, 0x32, 0x6c, 0x6f, 0xba, 0xe7, 0x9c, 0x7b, 0x78, - 0x45, 0x1e, 0x4a, 0x00, 0x8b, 0x3c, 0x8b, 0x86, 0x59, 0x9e, 0x8a, 0x14, 0xe9, 0xfe, 0x9b, 0xdf, - 0x13, 0xff, 0x35, 0x96, 0x90, 0x35, 0x04, 0xfd, 0xd2, 0x75, 0x42, 0x26, 0x04, 0x4f, 0x16, 0x05, - 0x7a, 0x09, 0xfa, 0xf5, 0x8a, 0x27, 0x37, 0x38, 0x4a, 0xcb, 0x44, 0x98, 0x8d, 0x93, 0xc6, 0x69, - 0x3f, 0x00, 0x05, 0x8d, 0x24, 0x62, 0xfd, 0xdd, 0x84, 0xc1, 0x05, 0xa1, 0x0b, 0x26, 0x36, 0x3d, - 0x26, 0x74, 0x58, 0x42, 0xae, 0x57, 0x8c, 0x2a, 0x7d, 0x37, 0x58, 0x97, 0xc8, 0x80, 0xa3, 0x5b, - 0x4e, 0xcd, 0xc3, 0x93, 0xc6, 0xa9, 0x16, 0xc8, 0x47, 0x89, 0x64, 0x9c, 0x9a, 0x47, 0x15, 0x92, - 0x71, 0x8a, 0x2c, 0xe8, 0xc5, 0x24, 0x29, 0xdf, 0x93, 0x48, 0x94, 0x39, 0xcb, 0xcd, 0xa6, 0xa2, - 0x76, 0x30, 0xb9, 0x42, 0x96, 0xa7, 0xb4, 0x8c, 0x84, 0xd9, 0x52, 0xf4, 0xba, 0x44, 0x4f, 0xa0, - 0x5d, 0xb0, 0x9c, 0x93, 0x95, 0xd9, 0x56, 0x44, 0x5d, 0xa1, 0x17, 0xa0, 0x97, 0x05, 0xc3, 0x23, - 0x67, 0x84, 0xdd, 0x91, 0x67, 0x76, 0x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, 0xc7, 0xe1, 0x27, 0x3e, 0xf7, 0xf5, - 0x3c, 0x64, 0xf8, 0x13, 0x0c, 0x2e, 0x98, 0xd8, 0xfe, 0xab, 0xec, 0x9b, 0xca, 0xdc, 0xc1, 0xb6, - 0xd4, 0x95, 0x43, 0xb8, 0xeb, 0xf0, 0x59, 0xf5, 0xd3, 0x3d, 0xde, 0xd6, 0x01, 0x72, 0xa0, 0xe7, - 0xc9, 0xff, 0xd5, 0xcc, 0x0b, 0xcf, 0xf9, 0x8a, 0xa1, 0x07, 0xbe, 0x3d, 0xfb, 0x5d, 0xae, 0xdb, - 0xea, 0x87, 0xf9, 0xc3, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x56, 0x2c, 0x60, 0xd5, 0x3e, 0x07, - 0x00, 0x00, + // 1119 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x56, 0x5b, 0x6f, 0xdb, 0x46, + 0x13, 0xb5, 0xee, 0xd2, 0xe8, 0x62, 0x79, 0x93, 0x2f, 0x1f, 0xeb, 0x38, 0x8e, 0xcb, 0xa6, 0x85, + 0x50, 0x04, 0x02, 0xe2, 0xfa, 0x21, 0x08, 0x50, 0xa0, 0x8c, 0x44, 0x5b, 0x6a, 0x2c, 0x8b, 0x20, + 0xad, 0x1a, 0x7d, 0x22, 0xd6, 0xdc, 0xb5, 0xb4, 0xb0, 0x44, 0x12, 0xe4, 0x52, 0xa9, 0xdf, 0xfb, + 0x47, 0xfb, 0xd0, 0xdf, 0xd1, 0x62, 0x97, 0x17, 0x49, 0xb6, 0xec, 0x14, 0xed, 0xdb, 0xce, 0x99, + 0x33, 0x67, 0x67, 0x77, 0x66, 0x87, 0x04, 0x98, 0x06, 0xbe, 0xd3, 0xf5, 0x03, 0x8f, 0x7b, 0xa8, + 0x6e, 0x9c, 0x7c, 0x76, 0x8d, 0x77, 0xb6, 0x80, 0xd4, 0x2e,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c, 0xd1, 0x78, 0x62, 0xe9, 0x0a, 0x48, 0x5a, 0x3d, 0xa6, 0x49, 0x28, 0x4d, 0x45, 0x70, + 0x4c, 0xed, 0x4a, 0xa9, 0x67, 0xa9, 0x0c, 0x86, 0x7d, 0x53, 0xbb, 0x42, 0xff, 0x87, 0x8a, 0xf0, + 0x4f, 0x46, 0x96, 0xd2, 0x90, 0xbe, 0x72, 0x14, 0xd2, 0xc9, 0xc8, 0x42, 0xaf, 0x00, 0x84, 0xc3, + 0xd2, 0xcd, 0xa1, 0x76, 0xae, 0x34, 0xb3, 0xb8, 0x18, 0x40, 0x3f, 0x43, 0x2b, 0x70, 0x09, 0x0b, + 0xed, 0x30, 0x29, 0x84, 0xd2, 0x3a, 0xca, 0x75, 0xea, 0xc7, 0xdf, 0x74, 0xd7, 0xea, 0xdb, 0xdd, + 0xac, 0x95, 0xce, 0x67, 0x34, 0x70, 0x29, 0x37, 0x9b, 0x32, 0x34, 0x2b, 0xe1, 0x08, 0xda, 0x0e, + 0x71, 0x6c, 0xea, 0x2c, 0x56, 0x6a, 0xbb, 0xff, 0x5c, 0xad, 0xe5, 0x10, 0x47, 0x77, 0x16, 0x99, + 0x9c, 0x06, 0x8d, 0x68, 0xb1, 0x96, 0x58, 0x5b, 0x4a, 0x1d, 0x3e, 0x21, 0x35, 0x19, 0x59, 0x66, + 0x3d, 0x5a, 0x64, 0x19, 0xa9, 0x06, 0xbc, 0xd8, 0xbe, 0x99, 0x28, 0xdd, 0xcc, 0x0b, 0xb9, 0x8d, + 0x09, 0x09, 0x64, 0xc3, 0xd5, 0xcc, 0xaa, 0x00, 0x34, 0x42, 0x02, 0xf4, 0x15, 0x54, 0x09, 0x5d, + 0xc6, 0xbe, 0xb8, 0xed, 0x2a, 0x84, 0x2e, 0x85, 0x4b, 0xfd, 0x11, 0xf6, 0x1e, 0xec, 0x89, 0x9e, + 0x43, 0xc9, 0x21, 0x81, 0xb7, 0x48, 0x3a, 0x37, 0x36, 0x10, 0x82,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ac, 0x6f, 0x9a, 0x35, 0xec, 0xf7, 0x62, 0x00, 0xfd, 0x0f, + 0xca, 0xd8, 0xb7, 0xfd, 0xf0, 0x56, 0x4e, 0xfa, 0x9a, 0x59, 0xc2, 0xbe, 0x11, 0xde, 0xa2, 0x23, + 0x68, 0x60, 0xdf, 0x9e, 0x31, 0x42, 0xe3, 0xb4, 0xe2, 0x41, 0x0f, 0xd8, 0x1f, 0x30, 0x42, 0x65, + 0x6a, 0xaf, 0xa1, 0xee, 0xcc, 0x19, 0x75, 0x79, 0x4c, 0xa8, 0xc5, 0x15, 0x8a, 0x21, 0x49, 0x78, + 0x05, 0x89, 0x25, 0xd5, 0x21, 0x2e, 0x51, 0x8c, 0x88, 0x1d, 0xbe, 0x85, 0x16, 0x61, 0xf2, 0x16, + 0x6d, 0x97, 0xfe, 0xb6, 0xf0, 0xdc, 0x64, 0xbe, 0x37, 0x13, 0xf4, 0x42, 0x82, 0xea, 0x5b, 0x28, + 0xca, 0x63, 0x94, 0x21, 0xaf, 0x19, 0xed, 0x1d, 0x54, 0x81, 0x82, 0x75, 0xa9, 0xb5, 0x73, 0xe8, + 0x19, 0xec, 0x5a, 0x97, 0x9a, 0x7d, 0xaa, 0x0d, 0xcf, 0xc7, 0xbf, 0xe8, 0xa6, 0xad, 0x19, 0xed, + 0xbc, 0xfa, 0x06, 0x60, 0x75, 0x09, 0xa8, 0x01, 0xd5, 0x2b, 0x43, 0x3b, 0xb6, 0x0d, 0xeb, 0x53, + 0x7b, 0x07, 0x55, 0xa1, 0x38, 0x36, 0xf4, 0x8b, 0x76, 0x4e, 0xad, 0x40, 0x49, 0x5f, 0xf8, 0xfc, + 0xee, 0xf8, 0xf7, 0x22, 0x94, 0x8d, 0x93, 0xab, 0x0b, 0xe3, 0x1d, 0x1a, 0x81, 0x72, 0x46, 0x79, + 0x9f, 0xfa, 0x73, 0xef, 0x8e, 0x92, 0x8d, 0x71, 0x87, 0xd0, 0xc6, 0x95, 0xcb, 0xd0, 0xfd, 0x97, + 0x4f, 0x8c, 0x64, 0x75, 0x07, 0x0d, 0xe0, 0x59, 0xac, 0xf5, 0x9f, 0x95, 0x4e, 0x61, 0xef, 0x8c, + 0xf2, 0x7b, 0x3f, 0x0e, 0xff, 0x42, 0x67, 0x0c, 0x7b, 0xd6, 0x03, 0x9d, 0xa7, 0x62, 0xbe, 0x24, + 0xf8, 0x13, 0xb4, 0xce, 0x28, 0x5f, 0xff, 0x05, 0xda, 0x96, 0x95, 0xb2, 0x81, 0xad, 0xb1, 0x63, + 0x05, 0x6b, 0x53, 0xe1, 0x51, 0xf6, 0xfe, 0x16, 0x6d, 0x75, 0x07, 0xf5, 0xa1, 0x31, 0x12, 0x3f, + 0x57, 0x93, 0x91, 0x25, 0x67, 0xfd, 0x17, 0x3e, 0x94, 0xdb, 0x55, 0xae, 0xcb, 0xf2, 0xef, 0xee, + 0x87, 0xbf, 0x03, 0x00, 0x00, 0xff, 0xff, 0xe6, 0x51, 0x47, 0x3c, 0xeb, 0x09, 0x00, 0x00, } diff --git a/proto/grpc.proto b/proto/grpc.proto index 53e21ac..f0ee681 100644 --- a/proto/grpc.proto +++ b/proto/grpc.proto @@ -12,10 +12,13 @@ service P4WNP1 { rpc MountUMSFile (GadgetSettingsUMS) returns (Empty) {} } +/* LED */ message LEDSettings { uint32 blink_count = 1; } +/* End LED */ +/* USB Gadget */ message GadgetSettings { bool enabled = 1; string vid = 2; @@ -44,7 +47,49 @@ message GadgetSettingsUMS { bool cdrom = 1; string file = 2; } +/* End USB Gadget */ +/* DHCP */ +message DHCPServerSettings { + uint32 listenPort = 1; //if a port other thaan 0 is provided, a DNS server is bound to this port in addition to DHCP + string listenInterface = 2; + string leaseFile = 3; + bool notAuthoritative = 4; + bool doNotBindInterface = 5; //if true, DHCP server binds to 0.0.0.0, no matter which interface is given (no restart needed on interface up/down) + string callbackScript = 6; //Script executes when lease is added/deleted `scriptname ` + + repeated DHCPServerRange ranges = 7; + //repeated DHCPServerOption options = 8; + map options = 8; //Replaced by map (dealing with key duplicates is ONLY PARTIALY handled by proto3), See RFC 2132 for details of available options + repeated DHCPServerStaticHost staticHosts = 9; +} + +//Could be used for IPv4 or IPv6 (refer dnsmasq docs), nor field for prefix length (default is 64) +message DHCPServerRange { + string rangeLower = 1; //Lowest IP of DHCP range + string rangeUpper = 2; //Highest IP of DHCP range + string leaseTime = 3; +} + + +//Used for static leases +message DHCPServerStaticHost { + string mac = 1; + string ip = 2; +} + +/* +//See RFC 2132 for details of available options +//Note: dnsmasq default option have to be disabled explicitly if not needed, by setting an empty value (1, 3, 6, 12, 28) +message DHCPServerOption { + uint32 option = 1; + string value = 3; +} +*/ + +/* End DHCP */ + +/* WiFi */ message WiFiSettings { bool diasabled = 1; string reg = 2; //Regulatory domain per ISO/IEC 3166-1 alpha2 @@ -69,6 +114,6 @@ message WiFiSettings { bool disable_nexmon = 11; //if true, legacy driver / firmware is used instead of nexmon } - +/* End WiFI */ message Empty {} diff --git a/service/DHCPSrv.go b/service/DHCPSrv.go new file mode 100644 index 0000000..8732039 --- /dev/null +++ b/service/DHCPSrv.go @@ -0,0 +1,109 @@ +package service + +import ( + pb "../proto" + "fmt" + "io/ioutil" + "os" +) + +const ( + dnsmasq_conf_head = `port=0 + listen-address=$IF_IP + dhcp-range=$IF_DHCP_RANGE,$IF_MASK,5m + dhcp-option=252,http://$IF_IP/wpad.dat + # router + dhcp-option=3,$IF_IP + # DNS + dhcp-option=6,$IF_IP + # NETBIOS NS + dhcp-option=44,$IF_IP + dhcp-option=45,$IF_IP + # routes static (route 0.0.0.1 to 127.255.255.254 through our device) + dhcp-option=121,0.0.0.0/1,$IF_IP,128.0.0.0/1,$IF_IP + # routes static (route 128.0.0.1 to 255.255.255.254 through our device) + dhcp-option=249,0.0.0.0/1,$IF_IP,128.0.0.0/1,$IF_IP + dhcp-leasefile=/tmp/dnsmasq.leases + dhcp-authoritative + log-dhcp` + +) + +/* +The DHCP server part relies on "dnsmasq" and thus depends on the binary +Note: dnsmasq default option have to be disabled explicitly if not needed, by setting an empty value (1, 3, 6, 12, 28) + */ + +func DefaultDHCPConfigUSB() (settings *pb.DHCPServerSettings) { + settings = &pb.DHCPServerSettings{ + CallbackScript: "/bin/evilscript", + DoNotBindInterface: false, //only bind to given interface + ListenInterface: USB_ETHERNET_BRIDGE_NAME, + LeaseFile: "/tmp/dnsmasq_" + USB_ETHERNET_BRIDGE_NAME + ".leases", + ListenPort: 0, //No DNS, DHCP only + NotAuthoritative: false, //be authoritative + Ranges: []*pb.DHCPServerRange { + &pb.DHCPServerRange{ RangeLower:"172.16.0.2", RangeUpper: "172.16.0.3", LeaseTime: "5m"}, + &pb.DHCPServerRange{ RangeLower:"172.16.0.5", RangeUpper: "172.16.0.6", LeaseTime: "2m"}, + }, + Options: map[uint32]string { + 3: "", //Disable option: Router + 6: "", //Disable option: DNS + 252: "http://172.16.0.1/wpad.dat", + //Options 1 (Netmask), 12 (Hostname) and 28 (Broadcast Address) are still enabled + }, + } + return +} + +func defaultLeaseFile(s *pb.DHCPServerSettings) (lf string) { + return fmt.Sprintf("/tmp/dnsmasq_%s.leases", s.ListenInterface) //default lease file +} + +func DHCPCreateConfigFile(s *pb.DHCPServerSettings, filename string) (err error) { + file_content, err := DHCPCreateConfigFileString(s) + if err != nil {return} + err = ioutil.WriteFile(filename, []byte(file_content), os.ModePerm) + return +} + +func DHCPCreateConfigFileString(s *pb.DHCPServerSettings) (config string, err error) { + config = fmt.Sprintf("interface=%s\n", s.ListenInterface) + //bind only o the given interface, except suppressed by `doNotBindInterface` option + if !s.DoNotBindInterface { config += fmt.Sprintf("bind-interfaces\n") } + config += fmt.Sprintf("port=%d\n", s.ListenPort) + if len(s.CallbackScript) > 0 { config += fmt.Sprintf("dhcp-script=%s\n", s.CallbackScript) } + if len(s.LeaseFile) > 0 { + config += fmt.Sprintf("dhcp-leasefile=%s\n", s.LeaseFile) + } else { + config += fmt.Sprintf("dhcp-leasefile=%s\n", defaultLeaseFile(s)) //default lease file + } + + + //Iterate over Ranges + for _, pRange := range s.Ranges { + config += fmt.Sprintf("dhcp-range=%s,%s,%s\n", pRange.RangeLower, pRange.RangeUpper, pRange.LeaseTime) + } + + //Iterate over options + // + // Note: for duplicates only the last one should be used, but not in any case, see + // https://developers.google.com/protocol-buffers/docs/proto3#maps) + // + // "... When parsing from the wire or when merging, if there are duplicate map keys the last key seen is used. + // When parsing a map from text format, parsing may fail if there are duplicate keys...." + for o_num, o_val := range s.Options { + o_str := fmt.Sprintf("dhcp-option=%d", o_num) + if len(o_val) > 0 { + o_str += fmt.Sprintf(",%s\n", o_val) + } else { + o_str += "\n" + } + config += o_str + } + config += fmt.Sprintf("log-dhcp\n") //extensive logging by default + if (!s.NotAuthoritative) { config += fmt.Sprintf("dhcp-authoritative\n") } + + return +} + diff --git a/service/network.go b/service/network.go index 6dee5d7..35aa619 100644 --- a/service/network.go +++ b/service/network.go @@ -6,10 +6,14 @@ import ( "log" ) -func CreateBridge(name string) error { +func CreateBridge(name string) (err error) { return netlink.CreateBridge(name, false) } +func setInterfaceMac(name string, mac string) error { + return netlink.SetMacAddress(name, mac) +} + func DeleteBridge(name string) error { return netlink.DeleteBridge(name) } diff --git a/service/usb.go b/service/usb.go index eafce7e..b1265d7 100644 --- a/service/usb.go +++ b/service/usb.go @@ -29,6 +29,7 @@ const ( USB_GADGET_DIR = USB_GADGET_DIR_BASE + "/" + USB_GADGET_NAME USB_ETHERNET_BRIDGE_NAME = "usbeth" + USB_ETHERNET_BRIDGE_MAC = "24:22:26:12:14:16" USB_bcdDevice = "0x0100" //Version 1.00 USB_bcdUSB = "0x0200" //mode: USB 2.0 @@ -124,6 +125,7 @@ func ValidateGadgetSetting(gs pb.GadgetSettings) error { func addUSBEthernetBridge() { //Create the bridge CreateBridge(USB_ETHERNET_BRIDGE_NAME) + setInterfaceMac(USB_ETHERNET_BRIDGE_NAME, USB_ETHERNET_BRIDGE_MAC) //add the interfaces if err := AddInterfaceToBridgeIfExistent(USB_ETHERNET_BRIDGE_NAME, "usb0"); err != nil {