Browse Source

DHCP: Add UEFI support; TFTP: Stop dev.

HOME 2 years ago
parent
commit
4cf6a49065

+ 57 - 45
DhcpServer/App.config

@@ -1,48 +1,60 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
-  <configSections>
-    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
-      <section name="DhcpServer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
-    </sectionGroup>
-  </configSections>
-  <startup>
-    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
-  </startup>
-  <applicationSettings>
-    <DhcpServer.Properties.Settings>
-      <setting name="PoolSize" serializeAs="String">
-        <value>100</value>
-      </setting>
-      <setting name="SubNet" serializeAs="String">
-        <value>255.255.255.0</value>
-      </setting>
-      <setting name="Dns" serializeAs="String">
-        <value>223.5.5.5</value>
-      </setting>
-      <setting name="PxeFileName" serializeAs="String">
-        <value>undionly-http.kpxe</value>
-      </setting>
-      <setting name="ListenOn" serializeAs="String">
-        <value>192.168.233.233</value>
-      </setting>
-      <setting name="PoolStart" serializeAs="String">
-        <value>192.168.233.100</value>
-      </setting>
-      <setting name="Router" serializeAs="String">
-        <value>192.168.233.1</value>
-      </setting>
-      <setting name="BroadcastAddress" serializeAs="String">
-        <value>192.168.233.255</value>
-      </setting>
-      <setting name="PxeTftp" serializeAs="String">
-        <value>192.168.233.233</value>
-      </setting>
-      <setting name="IpxeScriptUri" serializeAs="String">
-        <value>http://192.168.233.233:8233/boot/ipxe</value>
-      </setting>
-      <setting name="IpxeRouter" serializeAs="String">
-        <value>192.168.233.1</value>
-      </setting>
-    </DhcpServer.Properties.Settings>
-  </applicationSettings>
+	<configSections>
+		<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
+			<section name="DhcpServer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
+		</sectionGroup>
+	</configSections>
+	<startup>
+		<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
+	</startup>
+	<applicationSettings>
+		<DhcpServer.Properties.Settings>
+			<setting name="PoolSize" serializeAs="String">
+				<value>100</value>
+			</setting>
+			<setting name="SubNet" serializeAs="String">
+				<value>255.255.255.0</value>
+			</setting>
+			<setting name="Dns" serializeAs="String">
+				<value>223.5.5.5</value>
+			</setting>
+			<setting name="ListenOn" serializeAs="String">
+				<value>192.168.233.233</value>
+			</setting>
+			<setting name="PoolStart" serializeAs="String">
+				<value>192.168.233.233</value>
+			</setting>
+			<setting name="Router" serializeAs="String">
+				<value>192.168.233.1</value>
+			</setting>
+			<setting name="BroadcastAddress" serializeAs="String">
+				<value>192.168.233.255</value>
+			</setting>
+			<setting name="PxeRouter" serializeAs="String">
+				<value>192.168.233.1</value>
+			</setting>
+			<setting name="PxeTftp" serializeAs="String">
+				<value>192.168.233.233</value>
+			</setting>
+			<setting name="IpxeRouter" serializeAs="String">
+				<value>192.168.233.1</value>
+			</setting>
+			<setting name="FileName" serializeAs="String">
+				<value>undionly-http.kpxe</value>
+			</setting>
+			<setting name="FileName_iPXE_HTTP" serializeAs="String">
+				<value>http://192.168.233.233:8233/boot/ipxe</value>
+			</setting>
+			<setting name="FileName_UEFI_I386" serializeAs="String">
+				<value>i386_ipxe-http.efi</value>
+			</setting>
+			<setting name="FileName_UEFI_x64" serializeAs="String">
+				<value>x86_64_ipxe-http.efi</value>
+			</setting>
+			<setting name="FileName_UEFI_HTTP" serializeAs="String">
+				<value>http://192.168.5.66:8233/ipxe.efi</value>
+			</setting>
+		</DhcpServer.Properties.Settings>
+	</applicationSettings>
 </configuration>

+ 41 - 146
DhcpServer/DhcpProgram.cs

@@ -30,6 +30,10 @@ namespace DhcpServer
         private static string _pxeFileName;
         private static string _ipxeScriptUri;
 
+        private static string _u64FileName;
+        private static string _u86FileName;
+        private static string _uHttpFileName;
+
         private static IReadOnlyCollection<PoolSlot> _pool;
         private static IPAddress _ipxeRouter;
         private static IPAddress _pxeRouter;
@@ -131,6 +135,7 @@ namespace DhcpServer
 
             var clientMac = packet.ClientMacAddressHex;
             Console.WriteLine($" {packet.MessageType} by {clientMac}");
+            Console.WriteLine($" {packet.Vendor ?? "Unknown Vendor"} / {packet.UserClass ?? "Unknown Class"}");
 
             DhcpMessageType reply;
             switch (packet.MessageType)
@@ -158,7 +163,13 @@ namespace DhcpServer
                 var requestParameters = packet.Options[DhcpOption.ParameterRequestList];
                 var hostName = packet.HostName;
                 var userClass = packet.UserClass;
-                var ven = packet.Vendor;
+                var vendor = packet.Vendor;
+
+                //byte arch = 0;
+                //if (packet.Options.TryGetValue(DhcpOption.ClientIdentifier, out var bufArch))
+                //{
+                //    arch = bufArch?.FirstOrDefault() ?? 0;
+                //}
 
                 // Allocate ip address
 
@@ -222,19 +233,38 @@ namespace DhcpServer
 
                     packet.DhcpServerIdentifier = _listenOn.Address;
 
-                    // Extend Process
+                    // vendor spec
+
+                    packet.Router = _pxeRouter;
+                    packet.TftpServer = _pxeTftp.ToString();
+                    packet.NextServerIpAddress = _pxeTftp;
 
                     if (userClass == "iPXE")
                     {
                         packet.BootFileName = _ipxeScriptUri;
                         packet.Router = _ipxeRouter;
                     }
-                    else if (requestParameters != null && requestParameters.Contains((byte)67))
+                    else
                     {
-                        packet.Router = _pxeRouter;
-                        packet.BootFileName = _pxeFileName;
-                        packet.TftpServer = _pxeTftp.ToString();
-                        packet.NextServerIpAddress = _pxeTftp;
+                        if (true == vendor?.StartsWith("PXEClient:Arch:00000"))
+                        {
+                            //Legacy
+                            packet.BootFileName = _pxeFileName;
+                        }
+                        else if (true == vendor?.StartsWith("PXEClient:Arch:00002")
+                              || true == vendor?.StartsWith("PXEClient:Arch:00006"))
+                        {
+                            //UEFI x86
+                            packet.BootFileName = _u86FileName;
+                        }
+                        else if (true == vendor?.StartsWith("PXEClient:Arch:00007")
+                              || true == vendor?.StartsWith("PXEClient:Arch:00008")
+                              || true == vendor?.StartsWith("PXEClient:Arch:00009")
+                         )
+                        {
+                            //UEFI x64
+                            packet.BootFileName = _u64FileName;
+                        }
                     }
                 }
             }
@@ -280,32 +310,6 @@ namespace DhcpServer
             slots[0] = new PoolSlot(_poolStart);
             for (var i = 1; i < _poolSize; i++) slots[i] = new PoolSlot(slots[i - 1].Address.NextAddress());
 
-            //Task.Factory.StartNew(() =>
-            //{
-            //    foreach (var item in slots)
-            //    {
-            //        item.MacStatus = "Requesting...";
-            //        var mac = GetMac(item.Address, out var err);
-            //        if (null != mac)
-            //        {
-            //            item.Mac = mac;
-            //            item.LastConfirm = DateTime.Now;
-            //            item.MacStatus = "OK";
-            //        }
-            //        else
-            //        {
-            //            item.MacStatus = err ?? "Fail";
-            //        }
-            //    }
-
-            //    foreach (var slot in slots)
-            //    {
-            //        slot.HostNameStatus = "Requesting...";
-            //        slot.HostName = GetHostName(slot.Address, out var err);
-            //        slot.HostNameStatus = err ?? "OK";
-            //    }
-            //});
-
             _pool = slots;
         }
 
@@ -319,8 +323,10 @@ namespace DhcpServer
             _dns = IPAddress.Parse(Settings.Default.Dns);
             _pxeRouter = IPAddress.Parse(Settings.Default.PxeRouter);
             _pxeTftp = IPAddress.Parse(Settings.Default.PxeTftp);
-            _pxeFileName = Settings.Default.PxeFileName;
-            _ipxeScriptUri = Settings.Default.IpxeScriptUri;
+            _pxeFileName = Settings.Default.FileName;
+            _u64FileName = Settings.Default.FileName_UEFI_x64;
+            _u86FileName = Settings.Default.FileName_UEFI_I386;
+            _ipxeScriptUri = Settings.Default.FileName_iPXE_HTTP;
             _ipxeRouter = IPAddress.Parse(Settings.Default.IpxeRouter);
             _broadcastAddress = IPAddress.Parse(Settings.Default.BroadcastAddress);
         }
@@ -393,116 +399,5 @@ namespace DhcpServer
 
             return msg;
         }
-
-        //-----------------------------
-
-        private static void Working()
-        {
-            if (DhcpEntryManager.CreateDefaultEntryIfNoExist()) Console.WriteLine("Default client entry config created.");
-
-            var listenEndPoint = new IPEndPoint(IPAddress.Parse(Settings.Default.ListenOn), 67);
-
-            var socket = new Socket(listenEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
-            {
-                EnableBroadcast = true,
-                SendBufferSize = 65536,
-                ReceiveBufferSize = 65536
-            };
-            socket.Bind(listenEndPoint);
-
-            var upTime = DateTime.Now;
-
-            Console.WriteLine($"DHCP Server started, listing on: {listenEndPoint}");
-
-            var buffer = new byte[65536];
-            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 68);
-            var polling = DateTime.Now;
-
-            var pooled = false;
-            while (_isRunning)
-            {
-                Console.CursorLeft = 0;
-                if (false == socket.Poll(500 * 1000, SelectMode.SelectRead))
-                {
-                    var timeSpan = DateTime.Now - polling;
-                    var up = DateTime.Now - upTime;
-                    Console.Write("Polling sockets..." +
-                                  $" {timeSpan.Days:00}D {timeSpan.Hours:00}H {timeSpan.Minutes:00}M {timeSpan.Seconds:00}S {timeSpan.Milliseconds:000}" +
-                                  $" / UP {up.Days:00}D {up.Hours:00}H {up.Minutes:00}M {up.Seconds:00}S {up.Milliseconds:000}");
-                    pooled = true;
-                }
-                else // polled
-                {
-                    if (pooled)
-                    {
-                        pooled = false;
-                        Console.WriteLine();
-                    }
-
-                    var bytes = socket.ReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEndPoint);
-                    Console.Write($"Receive {bytes} byte From {remoteEndPoint}");
-
-                    try
-                    {
-                        var packet = new DhcpPacket(buffer);
-
-                        var mac = packet.ClientMacAddressHex;
-                        Console.Write($" {packet.MessageType} by {mac}");
-
-                        var entry = DhcpEntryManager.GetClientEntry(mac);
-                        if (entry.Enable == false)
-                        {
-                            Console.WriteLine(" ** No enabled, please edit config file.");
-                        }
-                        else
-                        {
-                            packet.OpCode = DhcpOpCode.BootReply;
-                            DhcpMessageType reply;
-                            switch (packet.MessageType)
-                            {
-                                default:
-                                    reply = DhcpMessageType.Unknown;
-                                    break;
-
-                                case DhcpMessageType.Discover:
-                                    reply = DhcpMessageType.Offer;
-                                    break;
-
-                                case DhcpMessageType.Request:
-                                    reply = DhcpMessageType.Ack;
-                                    break;
-                            }
-
-                            if (reply != DhcpMessageType.Unknown)
-                            {
-                                var userClass = packet.UserClass;
-                                packet.Options.Clear();
-                                packet.DhcpServerIdentifier = listenEndPoint.Address;
-                                packet.MessageType = reply;
-
-                                packet.LoadClientEntry(DhcpEntryManager.GetDefaultEntry());
-                                packet.LoadClientEntry(DhcpEntryManager.GetDefaultEntry(userClass));
-
-                                packet.LoadClientEntry(entry);
-                                packet.LoadClientEntry(DhcpEntryManager.GetClientEntry(mac, userClass));
-
-                                bytes = packet.WriteToBuffer(buffer);
-                                var to = new IPEndPoint(IPAddress.Broadcast, 68);
-                                Console.WriteLine();
-                                Console.WriteLine($"Send {bytes} bytes to {to} {packet.MessageType} by {mac}");
-                                socket.SendTo(buffer, 0, bytes, SocketFlags.None, to);
-                            }
-                        }
-                    }
-                    catch (Exception e)
-                    {
-                        Console.WriteLine(e);
-                    }
-                    polling = DateTime.Now;
-                }// end if poll
-            }
-
-            socket.Close();
-        }
     }
 }

+ 33 - 15
DhcpServer/Properties/Settings.Designer.cs

@@ -12,7 +12,7 @@ namespace DhcpServer.Properties {
     
     
     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.5.0.0")]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0")]
     internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
         
         private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
@@ -52,15 +52,6 @@ namespace DhcpServer.Properties {
         
         [global::System.Configuration.ApplicationScopedSettingAttribute()]
         [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("undionly-http.kpxe")]
-        public string PxeFileName {
-            get {
-                return ((string)(this["PxeFileName"]));
-            }
-        }
-        
-        [global::System.Configuration.ApplicationScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
         [global::System.Configuration.DefaultSettingValueAttribute("192.168.233.233")]
         public string ListenOn {
             get {
@@ -115,19 +106,46 @@ namespace DhcpServer.Properties {
         
         [global::System.Configuration.ApplicationScopedSettingAttribute()]
         [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("192.168.233.1")]
+        public string IpxeRouter {
+            get {
+                return ((string)(this["IpxeRouter"]));
+            }
+        }
+        
+        [global::System.Configuration.ApplicationScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("undionly-http.kpxe")]
+        public string FileName {
+            get {
+                return ((string)(this["FileName"]));
+            }
+        }
+        
+        [global::System.Configuration.ApplicationScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
         [global::System.Configuration.DefaultSettingValueAttribute("http://192.168.233.233:8233/boot/ipxe")]
-        public string IpxeScriptUri {
+        public string FileName_iPXE_HTTP {
             get {
-                return ((string)(this["IpxeScriptUri"]));
+                return ((string)(this["FileName_iPXE_HTTP"]));
             }
         }
         
         [global::System.Configuration.ApplicationScopedSettingAttribute()]
         [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("192.168.233.1")]
-        public string IpxeRouter {
+        [global::System.Configuration.DefaultSettingValueAttribute("i386_ipxe.efi")]
+        public string FileName_UEFI_I386 {
             get {
-                return ((string)(this["IpxeRouter"]));
+                return ((string)(this["FileName_UEFI_I386"]));
+            }
+        }
+        
+        [global::System.Configuration.ApplicationScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("x86_64_ipxe.efi")]
+        public string FileName_UEFI_x64 {
+            get {
+                return ((string)(this["FileName_UEFI_x64"]));
             }
         }
     }

+ 12 - 6
DhcpServer/Properties/Settings.settings

@@ -11,9 +11,6 @@
     <Setting Name="Dns" Type="System.String" Scope="Application">
       <Value Profile="(Default)">223.5.5.5</Value>
     </Setting>
-    <Setting Name="PxeFileName" Type="System.String" Scope="Application">
-      <Value Profile="(Default)">undionly-http.kpxe</Value>
-    </Setting>
     <Setting Name="ListenOn" Type="System.String" Scope="Application">
       <Value Profile="(Default)">192.168.233.233</Value>
     </Setting>
@@ -32,11 +29,20 @@
     <Setting Name="PxeTftp" Type="System.String" Scope="Application">
       <Value Profile="(Default)">192.168.233.233</Value>
     </Setting>
-    <Setting Name="IpxeScriptUri" Type="System.String" Scope="Application">
-      <Value Profile="(Default)">http://192.168.233.233:8233/boot/ipxe</Value>
-    </Setting>
     <Setting Name="IpxeRouter" Type="System.String" Scope="Application">
       <Value Profile="(Default)">192.168.233.1</Value>
     </Setting>
+    <Setting Name="FileName" Type="System.String" Scope="Application">
+      <Value Profile="(Default)">undionly-http.kpxe</Value>
+    </Setting>
+    <Setting Name="FileName_iPXE_HTTP" Type="System.String" Scope="Application">
+      <Value Profile="(Default)">http://192.168.233.233:8233/boot/ipxe</Value>
+    </Setting>
+    <Setting Name="FileName_UEFI_I386" Type="System.String" Scope="Application">
+      <Value Profile="(Default)">i386_ipxe.efi</Value>
+    </Setting>
+    <Setting Name="FileName_UEFI_x64" Type="System.String" Scope="Application">
+      <Value Profile="(Default)">x86_64_ipxe.efi</Value>
+    </Setting>
   </Settings>
 </SettingsFile>

+ 1 - 1
DhcpServer/Protocol/DhcpOption.cs

@@ -19,7 +19,7 @@
         ClientIdentifier = 61, // var MAC
         TftpServer = 66, // 4b ip address
         UserClass = 77, // var ascii
-
+        ClientArchitecture = 93, // RFC 4578
         End = 255,
     }
 }

+ 2 - 1
NetBootServer.sln.DotSettings

@@ -8,4 +8,5 @@
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=tsih/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=tsize/@EntryIndexedValue">True</s:Boolean>
 	<s:Boolean x:Key="/Default/UserDictionary/Words/=ipxe/@EntryIndexedValue">True</s:Boolean>
-	<s:Boolean x:Key="/Default/UserDictionary/Words/=Iscsi/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
+	<s:Boolean x:Key="/Default/UserDictionary/Words/=Iscsi/@EntryIndexedValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/UserDictionary/Words/=UEFI/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

+ 7 - 0
TftpServer/README.md

@@ -0,0 +1,7 @@
+# TftpServer
+
+Broke, DONT USE.
+
+For keep iPXE files only.
+
+Use TFTPD64 instead of it.

BIN
TftpServer/TftpRoot/i386_ipxe-http.efi


BIN
TftpServer/TftpRoot/x86_64_ipxe-http.efi


+ 7 - 0
TftpServer/TftpServer.csproj

@@ -61,6 +61,10 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />
+    <None Include="README.md" />
+    <None Include="TftpRoot\i386_ipxe-http.efi">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
     <None Include="TftpRoot\undionly-http.kpxe">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
@@ -68,6 +72,9 @@
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
     </None>
+    <None Include="TftpRoot\x86_64_ipxe-http.efi">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Utils\Utils.csproj">