/// <summary> /// Find the current binding, if any, for the given client identity association (IA). /// </summary> /// <param name="clientLink">link for the client request message</param> /// <param name="duid">DUID of the client</param> /// <param name="iatype">the IAID of the client request</param> /// <param name="iaid"> IAID of the client request</param> /// <param name="requestMsg">requestMsg the client request message</param> /// <returns>return the existing Binding for this client request</returns> protected Binding FindCurrentBinding(DhcpLink clientLink, byte[] duid, byte iatype, long iaid, DhcpMessage requestMsg) { Binding binding = null; lock (_lock) { try { IdentityAssoc ia = iaMgr.FindIA(duid, iatype, iaid); if (ia != null) { _log.Info("Found current binding for " + IdentityAssoc.KeyToString(duid, iatype, iaid) + " state=" + ia.GetState()); binding = BuildBindingFromIa(ia, clientLink, requestMsg); if (binding != null) { _log.Info("Successfully built Binding object: " + binding); } else { _log.Error("Failed to build Binding object"); } } else { _log.Info("No current binding found for " + IdentityAssoc.KeyToString(duid, iatype, iaid)); } } catch (Exception ex) { _log.Error("Failed to find current binding"); } } return(binding); }
private void SetIaPdT1(DhcpLink clientLink, DhcpV6IaPdOption iaPdOption, long minPreferredLifetime) { float t1 = DhcpServerPolicies.EffectivePolicyAsFloat(this.clientLink.GetLink(), Property.IA_PD_T1); if ((t1 > 1)) { log.Debug(("Setting IA_PD T1 to configured number of seconds: " + t1)); // if T1 is greater than one, then treat it as an // absolute value which specifies the number of seconds iaPdOption.SetT1(((long)(t1))); } else { // if T1 is less than one and greater than or equal to zero, // then treat is as a percentage of the minimum preferred lifetime // unless the minimum preferred lifetime is infinity (0xffffffff) if ((minPreferredLifetime == 4294967295)) { log.Debug(("Setting IA_PD T1 to minPreferredLifetime of infinity: " + minPreferredLifetime)); iaPdOption.SetT1(minPreferredLifetime); } else if ((t1 >= 0)) { // zero means let the client decide log.Debug(("Setting IA_PD T1 to configured ratio=" + (t1 + (" of minPreferredLifetime=" + minPreferredLifetime)))); iaPdOption.SetT1(((long)((t1 * minPreferredLifetime)))); } else { log.Debug(("Setting IA_PD T1 to standard ratio=0.5" + (" of minPreferredLifetime=" + minPreferredLifetime))); iaPdOption.SetT1(((long)((0.5 * minPreferredLifetime)))); } } }
//Populate v4 options. //@param link the link //@param configObj the config object or null if none protected void PopulateV4Reply(DhcpLink dhcpLink, DhcpV4OptionConfigObject configObj) { string sname = DhcpServerPolicies.EffectivePolicy(_requestMsg, configObj, dhcpLink.GetLink(), Property.V4_HEADER_SNAME); if (!String.IsNullOrEmpty(sname)) { _replyMsg.SetsName(sname); } string filename = DhcpServerPolicies.EffectivePolicy(_requestMsg, configObj, dhcpLink.GetLink(), Property.V4_HEADER_FILENAME); if (!String.IsNullOrEmpty(filename)) { _replyMsg.SetFile(filename); } Dictionary <int, DhcpOption> optionMap = _dhcpServerConfig.EffectiveV4AddrOptions(_requestMsg, dhcpLink, configObj); if (DhcpServerPolicies.EffectivePolicyAsBoolean(configObj, dhcpLink.GetLink(), Property.SEND_REQUESTED_OPTIONS_ONLY)) { optionMap = RequestedOptions(optionMap, _requestMsg); } _replyMsg.PutAllDhcpOptions(optionMap); // copy the relay agent info option from request to reply // in order to echo option back to router as required if (_requestMsg.HasOption(DhcpConstants.V4OPTION_RELAY_INFO)) { _requestMsg.PutDhcpOption(_requestMsg.GetDhcpOption(DhcpConstants.V4OPTION_RELAY_INFO)); } }
/// <summary> /// Builds the set of binding objects for the client request. /// </summary> /// <param name="clientLink">client link</param> /// <param name="inetAddrs">the list of IP addresses for the client binding</param> /// <param name="requestMsg">request message</param> /// <param name="state">binding state</param> /// <returns>the HashSet<binding object></returns> private HashSet <BindingObject> BuildBindingObjects(DhcpLink clientLink, List <IPAddress> inetAddrs, DhcpMessage requestMsg, byte state) { HashSet <BindingObject> bindingObjs = new HashSet <BindingObject>(); foreach (IPAddress inetAddr in inetAddrs) { if (_log.IsDebugEnabled) { _log.Debug("Building BindingObject for IP=" + inetAddr.ToString()); } BindingObject bindingObj = BuildBindingObject(inetAddr, clientLink, requestMsg); if (bindingObj != null) { bindingObj.SetState(state); bindingObjs.Add(bindingObj); } else { _log.Warn("Failed to build BindingObject for IP=" + inetAddr.ToString()); } } return(bindingObjs); }
/** * Build a BindingAddress for the given InetAddress and DhcpLink. * * @param inetAddr the inet addr * @param clientLink the client link * @param requestMsg the request msg * * @return the binding address */ protected override BindingObject BuildBindingObject(IPAddress inetAddr, DhcpLink clientLink, DhcpMessage requestMsg) { V6AddressBindingPool bp = (V6AddressBindingPool)FindBindingPool(clientLink.GetLink(), inetAddr, requestMsg); if (bp != null) { bp.SetUsed(inetAddr); // TODO check if this is necessary IaAddress iaAddr = new IaAddress(); iaAddr.SetIpAddress(inetAddr); V6BindingAddress bindingAddr = new V6BindingAddress(iaAddr, bp); SetBindingObjectTimes(bindingAddr, bp.GetPreferredLifetimeMs(), bp.GetPreferredLifetimeMs()); // TODO store the configured options in the persisted binding? // bindingAddr.setDhcpOptions(bp.getDhcpOptions()); return(bindingAddr); } else { log.Error("Failed to create BindingAddress: No BindingPool found for IP=" + inetAddr.ToString()); } // MUST have a BindingPool, otherwise something's broke return(null); }
/// <summary> /// Update an existing static binding. /// </summary> /// <param name="binding">existing client binding</param> /// <param name="clientLink">link for the client request message</param> /// <param name="duid">DUID of the client</param> /// <param name="iatype">IA type of the client request</param> /// <param name="iaid">IAID of the client request</param> /// <param name="staticBinding">static binding</param> /// <param name="requestMsg">client request message</param> /// <returns>updated Binding</returns> protected Binding UpdateStaticBinding(Binding binding, DhcpLink clientLink, byte[] duid, byte iatype, long iaid, StaticBinding staticBinding, DhcpMessage requestMsg) { List <IaAddress> addIaAddresses = null; List <IaAddress> updateIaAddresses = null; List <IaAddress> delIaAddresses = null; // not used currently if (staticBinding != null) { _log.Info("Updating static binding: " + binding); HashSet <BindingObject> bindingObjs = binding.GetBindingObjects(); if ((bindingObjs != null) && bindingObjs.Count > 0) { foreach (BindingObject bindingObj in bindingObjs) { if (bindingObj.GetIpAddress().Equals(staticBinding.GetIpAddress())) { SetBindingObjectTimes(bindingObj, staticBinding.GetPreferredLifetimeMs(), staticBinding.GetPreferredLifetimeMs()); break; } //TODO: what about bindingObjs that do NOT match the static binding? } // the existing IaAddress binding objects will be updated updateIaAddresses = binding.GetIaAddresses(); } else { IPAddress inetAddr = staticBinding.GetInetAddress(); if (inetAddr != null) { BindingObject bindingObj = BuildStaticBindingObject(inetAddr, staticBinding); bindingObjs = new HashSet <BindingObject>(); bindingObjs.Add(bindingObj); binding.SetBindingObjects(bindingObjs); // this new IaAddress binding object will be added addIaAddresses = binding.GetIaAddresses(); } } } else { _log.Error("StaticBindingObject is null"); } binding.SetState(IaAddress.STATIC); try { iaMgr.UpdateIA(binding, addIaAddresses, updateIaAddresses, delIaAddresses); return(binding); // if we get here, it worked } catch (Exception ex) { _log.Error("Failed to update binding"); return(null); } }
protected void AddStaticBindings(DhcpLink dhcpLink, v4AddressBinding binding) { if (staticBindingMap.ContainsKey(dhcpLink.GetLinkAddress())) { V4StaticAddressBinding sb = new V4StaticAddressBinding(binding); staticBindingMap[dhcpLink.GetLinkAddress()].Add(sb); } }
public Binding FindCurrentBinding(DhcpLink clientLink, DhcpV6ClientIdOption clientIdOption, DhcpV6IaNaOption iaNaOption, DhcpMessage requestMsg) { byte[] duid = clientIdOption.GetDuid(); long iaid = iaNaOption.GetIaId(); return(base.FindCurrentBinding(clientLink, duid, IdentityAssoc.NA_TYPE, iaid, requestMsg)); }
public Binding FindCurrentBinding(DhcpLink clientLink, byte[] macAddr, DhcpMessage requestMsg) { lock (_lock) { return(base.FindCurrentBinding(clientLink, macAddr, IdentityAssoc.V4_TYPE, 0, requestMsg)); } }
/// <summary> /// Create a binding in from a StaticBinding /// </summary> /// <param name="clientLink">link for the client request message</param> /// <param name="duid">DUID of the client</param> /// <param name="iatype">IA type of the client request</param> /// <param name="iaid">IAID of the client request</param> /// <param name="staticBinding">static binding</param> /// <param name="requestMsg">client request message</param> /// <returns>created Binding</returns> protected Binding CreateStaticBinding(DhcpLink clientLink, byte[] duid, byte iatype, long iaid, StaticBinding staticBinding, DhcpMessage requestMsg) { Binding binding = null; if (staticBinding != null) { binding = BuildBinding(clientLink, duid, iatype, iaid, IaAddress.STATIC); IPAddress inetAddr = staticBinding.GetInetAddress(); if (inetAddr != null) { IaAddress iaAddr = new IaAddress(); iaAddr.SetIpAddress(inetAddr); iaAddr.SetState(IaAddress.STATIC); V6BindingAddress bindingAddr = new V6BindingAddress(iaAddr, staticBinding); SetBindingObjectTimes(bindingAddr, staticBinding.GetPreferredLifetimeMs(), staticBinding.GetPreferredLifetimeMs()); // TODO store the configured options in the persisted binding? // bindingAddr.setDhcpOptions(bp.getDhcpOptions()); HashSet <BindingObject> bindingObjs = new HashSet <BindingObject>(); bindingObjs.Add(bindingAddr); binding.SetBindingObjects(bindingObjs); try { iaMgr.CreateIA(binding); } catch (Exception ex) { _log.Error("Failed to create persistent binding"); return(null); } } else { _log.Error("Failed to build binding object(s)"); return(null); } } else { _log.Error("StaticBinding object is null"); } String bindingType = (iatype == IdentityAssoc.V4_TYPE) ? "discover" : "solicit"; if (binding != null) { _log.Info("Created static " + bindingType + " binding: " + binding.ToString()); } else { _log.Warn("Failed to create static " + bindingType + " binding"); } return(binding); }
/// <summary> /// Create a binding in response to a Solicit/Discover request for the given client IA. /// </summary> /// <param name="clientLink">link for the client request message</param> /// <param name="duid">DUID of the client</param> /// <param name="iatype"> IA type of the client request</param> /// <param name="iaid">IAID of the client request</param> /// <param name="requestAddrs"></param> /// <param name="requestMsg"></param> /// <param name="state"></param> /// <returns>created Binding</returns> protected Binding CreateBinding(DhcpLink clientLink, byte[] duid, byte iatype, long iaid, List <IPAddress> requestAddrs, DhcpMessage requestMsg, byte state, IPAddress clientV4IPAddress) { Binding binding = null; lock (_lock) { try { _log.Debug("Getting addresses for new binding"); List <IPAddress> inetAddrs = GetInetAddrs(clientLink, duid, iatype, iaid, requestAddrs, requestMsg, clientV4IPAddress); if ((inetAddrs != null) && inetAddrs.Count > 0) { _log.Debug("Got " + inetAddrs.Count + " addresses, building binding"); binding = BuildBinding(clientLink, duid, iatype, iaid, state); _log.Debug("Building binding objects"); HashSet <BindingObject> bindingObjs = BuildBindingObjects(clientLink, inetAddrs, requestMsg, state); if ((bindingObjs != null) && bindingObjs.Count > 0) { binding.SetBindingObjects(bindingObjs); _log.Info("Creating new binding"); try { iaMgr.CreateIA(binding); } catch (Exception ex) { _log.Error("Failed to create persistent binding"); return(null); } } else { _log.Error("Failed to build binding object(s)"); return(null); } } string bindingType = (iatype == IdentityAssoc.V4_TYPE) ? "discover" : "solicit"; if (binding != null) { _log.Info("Created " + bindingType + " binding: " + binding.ToString()); } else { _log.Warn("Failed to create " + bindingType + " binding"); } } catch (Exception ex) { _log.WarnFormat("BaseDhcpV4Processor ProcessMessage Faile. exMessage:{0} exStackTrace:{1}", ex.Message, ex.StackTrace); return(null); } return(binding); } }
/// <summary> /// Builds a new Binding.Create a new IdentityAssoc from the given /// tuple and wrap that IdentityAssoc in a Binding. /// </summary> /// <param name="clientLink">client link</param> /// <param name="duid">DUID</param> /// <param name="iatype">IA type</param> /// <param name="iaid">IAID</param> /// <param name="state"></param> /// <returns> binding (a wrapped IdentityAssoc)</returns> private Binding BuildBinding(DhcpLink clientLink, byte[] duid, byte iatype, long iaid, byte state) { IdentityAssoc ia = new IdentityAssoc(); ia.SetDuid(duid); ia.SetIatype(iatype); ia.SetIaid(iaid); ia.SetState(state); return(new Binding(ia, clientLink)); }
/// <summary> /// Populate reply msg options. /// </summary> /// <param name="dhcpLink">the link</param> protected void PopulateReplyMsgOptions(DhcpLink dhcpLink) { Dictionary <int, DhcpOption> optionMap = dhcpServerConfig.EffectiveMsgOptions(this.requestMsg, dhcpLink); if (DhcpServerPolicies.EffectivePolicyAsBoolean(dhcpLink.GetLink(), Property.SEND_REQUESTED_OPTIONS_ONLY)) { optionMap = this.RequestedOptions(optionMap, this.requestMsg); } replyMsg.PutAllDhcpOptions(optionMap); }
protected void PopulatePrefixOptions(DhcpV6IaPrefixOption iaPrefixOption, DhcpLink dhcpLink, DhcpV6OptionConfigObject configObj) { Dictionary <int, DhcpOption> optionMap = dhcpServerConfig.EffectivePrefixOptions(this.requestMsg, dhcpLink, configObj); if (DhcpServerPolicies.EffectivePolicyAsBoolean(configObj, dhcpLink.GetLink(), Property.SEND_REQUESTED_OPTIONS_ONLY)) { optionMap = this.RequestedOptions(optionMap, this.requestMsg); } iaPrefixOption.PutAllDhcpOptions(optionMap); }
protected void AddTaBindingToReply(DhcpLink clientLink, Binding binding) { DhcpV6IaTaOption dhcpIaTaOption = new DhcpV6IaTaOption(); dhcpIaTaOption.SetIaId(binding.GetIaid()); HashSet <BindingObject> bindingObjs = binding.GetBindingObjects(); if (((bindingObjs != null) && bindingObjs.Count > 0)) { List <DhcpV6IaAddrOption> dhcpIaAddrOptions = new List <DhcpV6IaAddrOption>(); foreach (BindingObject bindingObj in bindingObjs) { DhcpV6IaAddrOption dhcpIaAddrOption = new DhcpV6IaAddrOption(); IPAddress inetAddr = bindingObj.GetIpAddress(); if ((inetAddr != null)) { dhcpIaAddrOption.SetIpAddress(inetAddr); // must be an DhcpOptionConfigObject for IA_TA binding DhcpV6OptionConfigObject configObj = ((DhcpV6OptionConfigObject)(bindingObj.GetConfigObj())); if ((configObj != null)) { dhcpIaAddrOption.SetPreferredLifetime(configObj.GetPreferredLifetime()); dhcpIaAddrOption.SetValidLifetime(configObj.GetValidLifetime()); this.PopulateTaAddrOptions(dhcpIaAddrOption, this.clientLink, configObj); dhcpIaAddrOptions.Add(dhcpIaAddrOption); // TODO when do actually start the timer? currently, two get // created - one during advertise, one during reply // policy to allow real-time expiration? // bp.startExpireTimerTask(bindingAddr, iaAddrOption.getValidLifetime()); } else { log.Error(("Null binding pool in binding: " + binding.ToString())); } } else { log.Error(("Null address in binding: " + binding.ToString())); } } dhcpIaTaOption.SetIaAddrOptions(dhcpIaAddrOptions); } else { log.Error("No IA_TA bindings in binding object!"); } this.PopulateIaTaOptions(dhcpIaTaOption, this.clientLink); this.replyMsg.AddIaTaOption(dhcpIaTaOption); }
public bool AddrOnLink(DhcpV4RequestedIpAddressOption requestedIpOption, DhcpLink clientLink) { bool onLink = true; if (requestedIpOption != null) { IPAddress requestedIp = IPAddress.Parse(requestedIpOption.GetIpAddress()); if (!clientLink.GetSubnet().Contains(requestedIp)) { onLink = false; } } return(onLink); }
public Binding(IdentityAssoc ia, DhcpLink dhcpLink) { this.origIa = ia; // save a reference to the original IA this.SetDhcpOptions(ia.GetDhcpOptions()); this.SetDuid(ia.GetDuid()); this.SetIaAddresses(ia.GetIaAddresses()); this.SetIaid(ia.GetIaid()); this.SetIatype(ia.GetIatype()); this.SetId(ia.GetId()); this.SetState(ia.GetState()); this.dhcpLink = dhcpLink; }
/** * Adds the v4 binding to reply. * * @param clientLink the client link * @param binding the binding */ protected void AddBindingToReply(DhcpLink clientLink, Binding binding) { HashSet <BindingObject> bindingObjs = binding.GetBindingObjects(); if ((bindingObjs != null) && bindingObjs.Count > 0) { if (bindingObjs.Count == 1) { BindingObject bindingObj = bindingObjs.First(); IPAddress inetAddr = bindingObj.GetIpAddress(); if (inetAddr != null) { _replyMsg.SetYiAddr(inetAddr); // must be an DhcpV4OptionConfigObject for v4 binding DhcpV4OptionConfigObject configObj = (DhcpV4OptionConfigObject)bindingObj.GetConfigObj(); if (configObj != null) { long preferred = configObj.GetPreferredLifetime(); DhcpV4LeaseTimeOption dhcpV4LeaseTimeOption = new DhcpV4LeaseTimeOption(); dhcpV4LeaseTimeOption.SetUnsignedInt(preferred); _replyMsg.PutDhcpOption(dhcpV4LeaseTimeOption); PopulateV4Reply(clientLink, configObj); //TODO when do actually start the timer? currently, two get // created - one during advertise, one during reply // policy to allow real-time expiration? // bp.startExpireTimerTask(bindingAddr, iaAddrOption.getValidLifetime()); } else { log.Error("Null binding pool in binding: " + binding.ToString()); } } else { log.Error("Null address in binding: " + binding.ToString()); } } else { log.Error("Expected only one bindingObject in v4 Binding, but found " + bindingObjs.Count + "bindingObjects"); } } else { log.Error("No V4 bindings in binding object!"); } }
protected void AddBindingToReply(DhcpLink clientLink, Binding binding) { if ((binding.GetIatype() == Binding.NA_TYPE)) { this.AddNaBindingToReply(this.clientLink, binding); } else if ((binding.GetIatype() == Binding.TA_TYPE)) { this.AddTaBindingToReply(this.clientLink, binding); } else if ((binding.GetIatype() == Binding.PD_TYPE)) { this.AddPdBindingToReply(this.clientLink, binding); } }
public virtual bool PreProcess() { IPAddress localSocketAddr = this.requestMsg.GetLocalAddress(); this.clientLink = dhcpServerConfig.FindDhcpLinkV6(localSocketAddr, this.clientLinkAddress); if (this.clientLink == null) { log.ErrorFormat("No Link configured for DHCPv6 client request: localAddress={0} clientLinkAddress={1}" , localSocketAddr.ToString(), this.clientLinkAddress.ToString()); return(false); // must configure link for server to reply } return(true); // ok to process }
public Binding CreateSolicitBinding(DhcpLink clientLink, DhcpV6ClientIdOption clientIdOption, DhcpV6IaNaOption iaNaOption, DhcpMessage requestMsg, byte state, IPAddress clientV4IPAddress) { byte[] duid = clientIdOption.GetDuid(); long iaid = iaNaOption.GetIaId(); StaticBinding staticBinding = FindStaticBinding(clientLink.GetLink(), duid, IdentityAssoc.NA_TYPE, iaid, requestMsg); if (staticBinding != null) { return(base.CreateStaticBinding(clientLink, duid, IdentityAssoc.NA_TYPE, iaid, staticBinding, requestMsg)); } else { return(base.CreateBinding(clientLink, duid, IdentityAssoc.NA_TYPE, iaid, GetInetAddrs(iaNaOption), requestMsg, state, clientV4IPAddress)); } }
public Binding UpdateBinding(Binding binding, DhcpLink clientLink, byte[] macAddr, DhcpMessage requestMsg, byte state) { StaticBinding staticBinding = FindStaticBinding(clientLink.GetLink(), macAddr, IdentityAssoc.V4_TYPE, 0, requestMsg); if (staticBinding != null) { return(base.UpdateStaticBinding(binding, clientLink, macAddr, IdentityAssoc.V4_TYPE, 0, staticBinding, requestMsg)); } else { return(base.UpdateBinding(binding, clientLink, macAddr, IdentityAssoc.V4_TYPE, 0, GetInetAddrs(requestMsg), requestMsg, state, null)); } }
/// <summary> /// Update an existing client binding. Addresses in the current binding that are appropriate /// for the client's link are simply updated with new lifetimes. If no current bindings are /// appropriate for the client link, new binding addresses will be created and added to the /// existing binding, leaving any other addresses alone. /// </summary> /// <param name="binding">existing client binding</param> /// <param name="clientLink">link for the client request message</param> /// <param name="duid">DUID of the client</param> /// <param name="iatype">IA type of the client request</param> /// <param name="iaid">IAID of the client request</param> /// <param name="requestAddrs">list of requested IP addresses, if any</param> /// <param name="requestMsg">client request message</param> /// <param name="state">the new state for the binding</param> /// <returns>updated Binding</returns> protected Binding UpdateBinding(Binding binding, DhcpLink clientLink, byte[] duid, byte iatype, long iaid, List <IPAddress> requestAddrs, DhcpMessage requestMsg, byte state, IPAddress clientV4IPAddress) { List <IaAddress> addIaAddresses = null; List <IaAddress> updateIaAddresses = null; List <IaAddress> delIaAddresses = null; // not used currently // log.info("Updating dynamic binding: " + binding); HashSet <BindingObject> bindingObjs = binding.GetBindingObjects(); if ((bindingObjs != null) && bindingObjs.Count > 0) { // current binding has addresses, so update times SetBindingObjsTimes(bindingObjs); // the existing IaAddress binding objects will be updated updateIaAddresses = binding.GetIaAddresses(); } else { _log.Warn("Existing binding has no on-link addresses, allocating new address(es)"); // current binding has no addresses, add new one(s) List <IPAddress> inetAddrs = GetInetAddrs(clientLink, duid, iatype, iaid, requestAddrs, requestMsg, clientV4IPAddress); if ((inetAddrs == null) || inetAddrs.Count == 0) { _log.Error("Failed to update binding, no addresses available"); return(null); } bindingObjs = BuildBindingObjects(clientLink, inetAddrs, requestMsg, state); binding.SetBindingObjects(bindingObjs); // these new IaAddress binding objects will be added addIaAddresses = binding.GetIaAddresses(); } binding.SetState(state); try { _log.Info("Updating binding"); iaMgr.UpdateIA(binding, addIaAddresses, updateIaAddresses, delIaAddresses); _log.Info("Binding updated: " + binding.ToString()); return(binding); // if we get here, it worked } catch (Exception ex) { _log.Error("Failed to update binding"); return(null); } }
/** * Create a Binding given an IdentityAssoc loaded from the database. * * @param ia the ia * @param clientLink the client link * @param requestMsg the request msg * * @return the binding */ protected override Binding BuildBindingFromIa(IdentityAssoc ia, DhcpLink clientLink, DhcpMessage requestMsg) { Binding binding = new Binding(ia, clientLink); List <IaAddress> iaAddrs = ia.GetIaAddresses(); if ((iaAddrs != null) && iaAddrs.Count > 0) { List <IaAddress> bindingAddrs = new List <IaAddress>(); foreach (IaAddress iaAddr in iaAddrs) { if (!clientLink.GetSubnet().Contains(iaAddr.GetIpAddress())) { log.Info("Ignoring off-link binding address: " + iaAddr.GetIpAddress().ToString()); continue; } V4BindingAddress bindingAddr = null; StaticBinding staticBinding = FindStaticBinding(clientLink.GetLink(), ia.GetDuid(), ia.GetIatype(), ia.GetIaid(), requestMsg); if (staticBinding != null) { bindingAddr = BuildV4StaticBindingFromIaAddr(iaAddr, staticBinding); } else { bindingAddr = BuildV4BindingAddressFromIaAddr(iaAddr, clientLink.GetLink(), requestMsg); } if (bindingAddr != null) { bindingAddrs.Add(bindingAddr); } } // replace the collection of IaAddresses with BindingAddresses binding.SetIaAddresses(bindingAddrs); } else { log.Warn("IA has no addresses, binding is empty."); } return(binding); }
protected bool AllIaAddrsOnLink(DhcpV6IaNaOption dhcpIaNaOption, DhcpLink clientLink) { bool onLink = true; // assume all IPs are on link if ((dhcpIaNaOption != null)) { List <DhcpV6IaAddrOption> iaAddrOpts = dhcpIaNaOption.GetIaAddrOptions(); if ((iaAddrOpts != null)) { foreach (DhcpV6IaAddrOption iaAddrOpt in iaAddrOpts) { if (this.clientLink.GetSubnet().GetSubnetAddress().IsIPv6LinkLocal) { // if the Link address is link-local, then check if the // address is within one of the pools configured for this // local Link, which automatically makes this server // "authoritative" (in ISC parlance) for this local net v6AddressPool p = DhcpServerConfiguration.FindNaAddrPool(this.clientLink.GetLink(), iaAddrOpt.GetInetAddress()); if ((p == null)) { log.Info(("No local address pool found for requested IA_NA: " + (iaAddrOpt.ToString() + " - considered to be off link"))); iaAddrOpt.SetPreferredLifetime(0); iaAddrOpt.SetValidLifetime(0); onLink = false; } } else { // it the Link address is remote, then check // if the address is valid for that link if (!this.clientLink.GetSubnet().Contains(iaAddrOpt.GetInetAddress())) { log.Info("Setting zero(0) lifetimes for off link address: " + iaAddrOpt.GetIpAddress()); iaAddrOpt.SetPreferredLifetime(0); iaAddrOpt.SetValidLifetime(0); onLink = false; } } } } } return(onLink); }
/** * Create a Binding given an IdentityAssoc loaded from the database. * * @param ia the ia * @param clientLink the client link * @param requestMsg the request msg * * @return the binding */ protected override Binding BuildBindingFromIa(IdentityAssoc ia, DhcpLink clientLink, DhcpMessage requestMsg) { Binding binding = new Binding(ia, clientLink); List <IaAddress> iaPrefs = ia.GetIaAddresses(); if ((iaPrefs != null) && iaPrefs.Count > 0) { List <IaAddress> bindingPrefixes = new List <IaAddress>(); foreach (IaAddress iaAddr in iaPrefs) { // off-link check needed only for v4? // if (!clientLink.getSubnet().contains(iaAddr.getIpAddress())) { // log.info("Ignoring off-link binding address: " + // iaAddr.getIpAddress().getHostAddress()); // continue; // } V6BindingPrefix bindingPrefix = null; StaticBinding staticBinding = FindStaticBinding(clientLink.GetLink(), ia.GetDuid(), ia.GetIatype(), ia.GetIaid(), requestMsg); if (staticBinding != null) { bindingPrefix = BuildV6BindingPrefixFromIaPrefix((IaPrefix)iaAddr, staticBinding); } else { bindingPrefix = BuildBindingAddrFromIaPrefix((IaPrefix)iaAddr, clientLink.GetLink(), requestMsg); } if (bindingPrefix != null) { bindingPrefixes.Add(bindingPrefix); } } // replace the collection of IaPrefixes with BindingPrefixes binding.SetIaAddresses(bindingPrefixes); } else { log.Warn("IA has no prefixes, binding is empty."); } return(binding); }
private void SetIaPdT2(DhcpLink clientLink, DhcpV6IaPdOption iaPdOption, long minPreferredLifetime) { float t2 = DhcpServerPolicies.EffectivePolicyAsFloat(this.clientLink.GetLink(), Property.IA_PD_T2); if ((t2 > 1)) { log.Debug(("Setting IA_PD T2 to configured number of seconds: " + t2)); iaPdOption.SetT2(((long)(t2))); } else { // if T2 is less than one and greater than or equal to zero, // then treat is as a percentage of the minimum preferred lifetime // unless the minimum preferred lifetime is infinity (0xffffffff) if ((minPreferredLifetime == 4294967295)) { log.Debug(("Setting IA_PD T2 to minPreferredLifetime of infinity: " + minPreferredLifetime)); iaPdOption.SetT2(minPreferredLifetime); } else if ((t2 >= 0)) { // zero means let the client decide log.Debug(("Setting IA_PD T2 to configured ratio=" + (t2 + (" of minPreferredLifetime=" + minPreferredLifetime)))); iaPdOption.SetT2(((long)((t2 * minPreferredLifetime)))); } else { log.Debug(("Setting IA_PD T2 to standard ratio=0.8" + (" of minPreferredLifetime=" + minPreferredLifetime))); iaPdOption.SetT2(((long)((0.8 * minPreferredLifetime)))); } } // ensure that T2 >= T1 if ((iaPdOption.GetT2() < iaPdOption.GetT1())) { log.Warn((" IA_PD T2(" + (iaPdOption.GetT2() + (")" + (" < IA_PD T1(" + (iaPdOption.GetT1() + "): setting T2=T1")))))); iaPdOption.SetT2(iaPdOption.GetT1()); } }
/** * Create a V6BindingAddress given an IaAddress loaded from the database. * * @param iaAddr the ia addr * @param clientLink the client link * @param requestMsg the request msg * * @return the binding address */ private V6BindingAddress BuildV6BindingAddressFromIaAddr(IaAddress iaAddr, DhcpLink clientLink, DhcpMessage requestMsg) { IPAddress inetAddr = iaAddr.GetIpAddress(); BindingPool bp = FindBindingPool(clientLink.GetLink(), inetAddr, requestMsg); if (bp != null) { // TODO store the configured options in the persisted binding? // ipAddr.setDhcpOptions(bp.getDhcpOptions()); return(new V6BindingAddress(iaAddr, (V6AddressBindingPool)bp)); } else { log.Error("Failed to create BindingAddress: No BindingPool found for IP=" + inetAddr.ToString()); } // MUST have a BindingPool, otherwise something's broke return(null); }
public Binding CreateDiscoverBinding(DhcpLink clientLink, byte[] macAddr, DhcpMessage requestMsg, byte state) { lock (_lock) { StaticBinding staticBinding = FindStaticBinding(clientLink.GetLink(), macAddr, IdentityAssoc.V4_TYPE, 0, requestMsg); if (staticBinding != null) { return(base.CreateStaticBinding(clientLink, macAddr, IdentityAssoc.V4_TYPE, 0, staticBinding, requestMsg)); } else { return(base.CreateBinding(clientLink, macAddr, IdentityAssoc.V4_TYPE, 0, GetInetAddrs(requestMsg), requestMsg, state, null)); } } }
public virtual bool PreProcess() { IPAddress localSocketAddr = _requestMsg.GetLocalAddress(); byte[] chAddr = _requestMsg.GetChAddr(); //判斷是否有MAC if ((chAddr == null) || (chAddr.Length == 0) || IsIgnoredMac(chAddr)) { //log.Warn("Ignorning request message from client: mac=" + Util.toHexString(chAddr)); return(false); } _clientLink = _dhcpServerConfig.FindDhcpLinkV4(localSocketAddr, (IPAddress)_clientLinkAddress); if (_clientLink == null) { //log.Error("No Link configured for DHCPv4 client request: " + " localAddress=" + localSocketAddr.GetAddress().GetHostAddress() +" clientLinkAddress=" + clientLinkAddress.getHostAddress()); return(false); // must configure link for server to reply } /* TODO: check if this DOS mitigation is useful * * boolean isNew = recentMsgs.add(requestMsg); * if (!isNew) { * if (log.isDebugEnabled()) * log.debug("Dropping recent message"); * return false; // don't process * } * * if (log.isDebugEnabled()) * log.debug("Processing new message"); * * long timer = DhcpServerPolicies.effectivePolicyAsLong(clientLink.getLink(), * Property.DHCP_PROCESSOR_RECENT_MESSAGE_TIMER); * if (timer > 0) { * recentMsgPruner.schedule(new RecentMsgTimerTask(requestMsg), timer); * } */ return(true); // ok to process }