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);
295 + dhcpReply.setFlags(dhcpPacket.getFlags());
296 + dhcpReply.setGatewayIPAddress(dhcpPacket.getGatewayIPAddress());
297 + dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress());
298 + dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
293 299
300 + if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
294 dhcpReply.setYourIPAddress(ipOffered.toInt()); 301 dhcpReply.setYourIPAddress(ipOffered.toInt());
295 dhcpReply.setServerIPAddress(myIP.toInt()); 302 dhcpReply.setServerIPAddress(myIP.toInt());
296 - 303 + if (dhcpPacket.getGatewayIPAddress() == 0) {
297 - dhcpReply.setTransactionId(dhcpPacket.getTransactionId()); 304 + ipv4Reply.setDestinationAddress(IP_BROADCAST.toInt());
298 - dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress()); 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,6 +325,8 @@ public class DhcpManager implements DhcpService { ...@@ -317,6 +325,8 @@ 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
328 + if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
329 +
320 // IP Address Lease Time. 330 // IP Address Lease Time.
321 option = new DHCPOption(); 331 option = new DHCPOption();
322 option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue()); 332 option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue());
...@@ -365,6 +375,7 @@ public class DhcpManager implements DhcpService { ...@@ -365,6 +375,7 @@ public class DhcpManager implements DhcpService {
365 option.setLength((byte) 4); 375 option.setLength((byte) 4);
366 option.setData(domainServer.toOctets()); 376 option.setData(domainServer.toOctets());
367 optionList.add(option); 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;
459 - sendReply(context, ethReply);
460 discoverHost(context, requestedIP); 467 discoverHost(context, requestedIP);
468 + } else {
469 + outgoingPacketType = DHCPPacketType.DHCPNAK;
470 + }
471 + Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue());
472 + sendReply(context, ethReply);
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()) {
......