samanwita pal
Committed by Gerrit Code Review

ONOS-2926 Sending DHCPNAKs to failed ASSIGN request

Change-Id: I04658313c516934cd946733d8e30093523f83d7e
1 /* 1 /*
2 - * Copyright 2014 Open Networking Laboratory 2 + * Copyright 2015 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -154,6 +154,8 @@ public class DhcpManager implements DhcpService { ...@@ -154,6 +154,8 @@ public class DhcpManager implements DhcpService {
154 154
155 private static Ip4Address domainServer = Ip4Address.valueOf("10.0.0.2"); 155 private static Ip4Address domainServer = Ip4Address.valueOf("10.0.0.2");
156 156
157 + private static final Ip4Address IP_BROADCAST = Ip4Address.valueOf("255.255.255.255");
158 +
157 protected Timeout timeout; 159 protected Timeout timeout;
158 160
159 protected static int timerDelay = 2; 161 protected static int timerDelay = 2;
...@@ -290,12 +292,18 @@ public class DhcpManager implements DhcpService { ...@@ -290,12 +292,18 @@ public class DhcpManager implements DhcpService {
290 DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); 292 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
291 DHCP dhcpReply = new DHCP(); 293 DHCP dhcpReply = new DHCP();
292 dhcpReply.setOpCode(DHCP.OPCODE_REPLY); 294 dhcpReply.setOpCode(DHCP.OPCODE_REPLY);
293 - 295 + dhcpReply.setFlags(dhcpPacket.getFlags());
294 - dhcpReply.setYourIPAddress(ipOffered.toInt()); 296 + dhcpReply.setGatewayIPAddress(dhcpPacket.getGatewayIPAddress());
295 - dhcpReply.setServerIPAddress(myIP.toInt());
296 -
297 - dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
298 dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress()); 297 dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress());
298 + dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
299 +
300 + if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
301 + dhcpReply.setYourIPAddress(ipOffered.toInt());
302 + dhcpReply.setServerIPAddress(myIP.toInt());
303 + if (dhcpPacket.getGatewayIPAddress() == 0) {
304 + ipv4Reply.setDestinationAddress(IP_BROADCAST.toInt());
305 + }
306 + }
299 dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); 307 dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
300 dhcpReply.setHardwareAddressLength((byte) 6); 308 dhcpReply.setHardwareAddressLength((byte) 6);
301 309
...@@ -317,54 +325,57 @@ public class DhcpManager implements DhcpService { ...@@ -317,54 +325,57 @@ public class DhcpManager implements DhcpService {
317 option.setData(myIP.toOctets()); 325 option.setData(myIP.toOctets());
318 optionList.add(option); 326 optionList.add(option);
319 327
320 - // IP Address Lease Time. 328 + if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
321 - option = new DHCPOption(); 329 +
322 - option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue()); 330 + // IP Address Lease Time.
323 - option.setLength((byte) 4); 331 + option = new DHCPOption();
324 - option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array()); 332 + option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue());
325 - optionList.add(option); 333 + option.setLength((byte) 4);
326 - 334 + option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array());
327 - // IP Address Renewal Time. 335 + optionList.add(option);
328 - option = new DHCPOption(); 336 +
329 - option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue()); 337 + // IP Address Renewal Time.
330 - option.setLength((byte) 4); 338 + option = new DHCPOption();
331 - option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array()); 339 + option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue());
332 - optionList.add(option); 340 + option.setLength((byte) 4);
333 - 341 + option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array());
334 - // IP Address Rebinding Time. 342 + optionList.add(option);
335 - option = new DHCPOption(); 343 +
336 - option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue()); 344 + // IP Address Rebinding Time.
337 - option.setLength((byte) 4); 345 + option = new DHCPOption();
338 - option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array()); 346 + option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue());
339 - optionList.add(option); 347 + option.setLength((byte) 4);
340 - 348 + option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array());
341 - // Subnet Mask. 349 + optionList.add(option);
342 - option = new DHCPOption(); 350 +
343 - option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue()); 351 + // Subnet Mask.
344 - option.setLength((byte) 4); 352 + option = new DHCPOption();
345 - option.setData(subnetMask.toOctets()); 353 + option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue());
346 - optionList.add(option); 354 + option.setLength((byte) 4);
347 - 355 + option.setData(subnetMask.toOctets());
348 - // Broadcast Address. 356 + optionList.add(option);
349 - option = new DHCPOption(); 357 +
350 - option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue()); 358 + // Broadcast Address.
351 - option.setLength((byte) 4); 359 + option = new DHCPOption();
352 - option.setData(broadcastAddress.toOctets()); 360 + option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue());
353 - optionList.add(option); 361 + option.setLength((byte) 4);
354 - 362 + option.setData(broadcastAddress.toOctets());
355 - // Router Address. 363 + optionList.add(option);
356 - option = new DHCPOption(); 364 +
357 - option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue()); 365 + // Router Address.
358 - option.setLength((byte) 4); 366 + option = new DHCPOption();
359 - option.setData(routerAddress.toOctets()); 367 + option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue());
360 - optionList.add(option); 368 + option.setLength((byte) 4);
361 - 369 + option.setData(routerAddress.toOctets());
362 - // DNS Server Address. 370 + optionList.add(option);
363 - option = new DHCPOption(); 371 +
364 - option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue()); 372 + // DNS Server Address.
365 - option.setLength((byte) 4); 373 + option = new DHCPOption();
366 - option.setData(domainServer.toOctets()); 374 + option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue());
367 - optionList.add(option); 375 + option.setLength((byte) 4);
376 + option.setData(domainServer.toOctets());
377 + optionList.add(option);
378 + }
368 379
369 // End Option. 380 // End Option.
370 option = new DHCPOption(); 381 option = new DHCPOption();
...@@ -447,37 +458,44 @@ public class DhcpManager implements DhcpService { ...@@ -447,37 +458,44 @@ public class DhcpManager implements DhcpService {
447 458
448 } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPREQUEST.getValue()) { 459 } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPREQUEST.getValue()) {
449 460
450 - outgoingPacketType = DHCPPacketType.DHCPACK;
451 -
452 if (flagIfServerIP && flagIfRequestedIP) { 461 if (flagIfServerIP && flagIfRequestedIP) {
453 // SELECTING state 462 // SELECTING state
454 - if (myIP.equals(serverIP) && 463 + if (myIP.equals(serverIP)) {
455 - dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
456 464
457 - Ethernet ethReply = buildReply(packet, requestedIP, 465 + if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
458 - (byte) outgoingPacketType.getValue()); 466 + outgoingPacketType = DHCPPacketType.DHCPACK;
467 + discoverHost(context, requestedIP);
468 + } else {
469 + outgoingPacketType = DHCPPacketType.DHCPNAK;
470 + }
471 + Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue());
459 sendReply(context, ethReply); 472 sendReply(context, ethReply);
460 - discoverHost(context, requestedIP);
461 } 473 }
462 } else if (flagIfRequestedIP) { 474 } else if (flagIfRequestedIP) {
463 // INIT-REBOOT state 475 // INIT-REBOOT state
464 if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) { 476 if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
465 - Ethernet ethReply = buildReply(packet, requestedIP, 477 + outgoingPacketType = DHCPPacketType.DHCPACK;
466 - (byte) outgoingPacketType.getValue()); 478 + Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue());
467 sendReply(context, ethReply); 479 sendReply(context, ethReply);
468 discoverHost(context, requestedIP); 480 discoverHost(context, requestedIP);
469 } 481 }
482 +
470 } else { 483 } else {
471 // RENEWING and REBINDING state 484 // RENEWING and REBINDING state
472 int ciaadr = dhcpPayload.getClientIPAddress(); 485 int ciaadr = dhcpPayload.getClientIPAddress();
473 if (ciaadr != 0) { 486 if (ciaadr != 0) {
474 Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr); 487 Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr);
475 if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime)) { 488 if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime)) {
476 - Ethernet ethReply = buildReply(packet, clientIaddr, 489 + outgoingPacketType = DHCPPacketType.DHCPACK;
477 - (byte) outgoingPacketType.getValue());
478 - sendReply(context, ethReply);
479 discoverHost(context, clientIaddr); 490 discoverHost(context, clientIaddr);
491 + } else if (packet.getEtherType() == Ethernet.TYPE_IPV4 &&
492 + ((IPv4) packet.getPayload()).getDestinationAddress() == myIP.toInt()) {
493 + outgoingPacketType = DHCPPacketType.DHCPNAK;
494 + } else {
495 + return;
480 } 496 }
497 + Ethernet ethReply = buildReply(packet, clientIaddr, (byte) outgoingPacketType.getValue());
498 + sendReply(context, ethReply);
481 } 499 }
482 } 500 }
483 } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) { 501 } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) {
......