46
Issues and Solutions for DHCP AppNote-212 Version1.2 Page 1 of 46 September 2004 DHCP in VxWorks (updated October 2004) Issues and Solutions for DHCP &RS\ULJKWk:LQG5LYHU6\VWHPV,QF$// 5,*+765(6(59(' Abstract 7KHUHDUHWKUHH'+&3FRPSRQHQWVSURYLGHGLQ9[:RUNVWKH'+&3&OLHQW5HOD\$JHQWDQG6HUYHU7KH9[:RUNV1HWZRUNLQJ 3URJUDPPHUಬV*XLGHFRYHUVWKHEDVLFRSHUDWLRQRIWKHVHFRPSRQHQWV7KLVDSSQRWHLQFOXGHVFRQILJXUDWLRQGHWDLOVDQVZHUVWR FRPPRQTXHVWLRQVDQGWURXEOHVKRRWLQJVXJJHVWLRQV7KHPDMRULW\RIWKHQRWHDSSOLHVWR7RUQDGRDQG3ODWIRUP1(DQG 3ODWIRUP&'DQGDQG3ODWIRUP,$,I\RXDUHXVLQJ7RUQDGRDQGFDQQRWILQGLQIRUPDWLRQWKDWDQVZHUV\RXU TXHVWLRQVKHUHSOHDVHFRQWDFW7HFKQLFDO6XSSRUW

DHCP

Embed Size (px)

Citation preview

Page 1: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 1 of 46 September 2004

DHCP in VxWorks (updated October 2004)

Issues and Solutions for DHCP���������� ��������������������������� ��!"#��$�$�%$&�

Abstract

#����'������&"�(�������������������%)���*�+��&"�(�,���-��,'�����'���������#��%)���*�.�/��*���

(����'����0�!1�����������2'�������'����3��������������#���'��������,1������3��1�'�����'�,�-'��/����

������41������'����12,���������1���������#���'5�����3�����'��,����#���'�������'������-(,'3���.$���'��

���-(,'3����&���'�����'��(,'3����������3��1'��1����#���'�����'���'���3�����3���'����''��/�����1�

41����������-�,�'�����'�#������',�1�����

Page 2: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 2 of 46 September 2004

Table of Contents

..................................................................................................................................... 1Table of Contents ................................................................................................................................................... 21. DHCP Configuration Files ................................................................................................................................. 4

1.1. In Project Facility ........................................................................................................................................ 41.2. In Command Line........................................................................................................................................ 4

2. The DHCP Client ............................................................................................................................................... 42.1. Boot DHCP Client....................................................................................................................................... 42.2. Run time DHCP Client................................................................................................................................ 5

2.2.1. Command Line Build ........................................................................................................................... 52.2.2. Project Facility Build............................................................................................................................ 5

2.3. DHCP State Machine .................................................................................................................................. 62.3.1. DHCP Lease Negotiation.................................................................................................................... 62.3.2. DHCP Client Lease Selection .............................................................................................................. 62.3.3. DHCP Client Lease Renewal ............................................................................................................... 6

2.4. Miscellaneous DHCP Issues........................................................................................................................ 62.4.1. Adjusting Retransmission Times.......................................................................................................... 72.4.2. How Do I Change the DHCP Lease Interval?...................................................................................... 72.4.3. Using Boot Time DHCP (Command Line) ......................................................................................... 72.4.4. Why Does the DHCP Runtime Client Immediately Renew the Lease Acquired at Boot Time?......... 82.4.5. Why Can’t I Acquire an Address Dynamically When Booting Over a Non-Networked Device? ...... 82.4.6. Why is the DHCP Address Changing to a 10.0.x.x Address?.............................................................. 82.4.7. Specifying an IP Address at Lease Request Time................................................................................ 82.4.8. Incorporating Gateway Retrieval for DHCP for the boot ROM image................................................ 92.4.9. Why Does the boot ROM Have Its Own Version of DHCP? ............................................................ 102.4.10. What are the ARP Messages That I See Being Sent Out During Negotiation? ............................... 102.4.11.The DHCP Boot Client Does Not Process Incoming OFFER Packets When Using BSD Drivers .. 102.4.12. DHCP Event Hook ........................................................................................................................... 112.4.13. Avoiding the S_dhcpcLib_NOT_BOUND Error ............................................................................. 112.4.14. What Happens to Sockets that are Already Open When a DHCP Lease Expires or Renews? ........ 122.4.15. Why Does DHCP Begin a Full DHCP Negotiation Instead of Renewing the Lease? ..................... 122.4.16. The DHCP Client is not Detecting Renewals when the Lease is in RENEW or REBIND.............. 122.4.17. Restrictions on DHCP Packet Lengths (SPR #65264) Resulting in Dropped DHCP Offers........... 122.4.18. Why is the DHCP Client Sending INFORM Messages? ................................................................. 122.4.19. Timing issue after system tick rollover ............................................................................................ 122.4.20. At boot, DHCP client sends out extra ARP request in wait_offer state ........................................... 132.4.21. DHCP timeouts are incorrectly calculated after calls to clock_settime ........................................... 132.4.22. How long does the DHCP client wait for offers?............................................................................. 132.4.23 DHCP attempting to ARP on interfaces that have IFF_NOARP set in if_flags. .............................. 132.4.24. Why do RFC 1541 compliant DHCP servers respond only after the 5-th DHCPDISCOVER sentout by a vxWorks DHCP client? .................................................................................................................. 132.4.25. No access to possible duplicate IP address....................................................................................... 13

Page 3: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 3 of 46 September 2004

3. DHCP Server .................................................................................................................................................... 143.1. Configuration and Build ........................................................................................................................... 14

3.1.1. PROJECT FACILITY BUILD........................................................................................................... 143.1.2. COMMAND LINE BUILD................................................................................................................ 15

3.2. How Do I Specify a List of DNS Servers in the DHCP Lease Entry? ...................................................... 153.3. Configuring the DHCP Server as a BOOTP Server .................................................................................. 163.4. DHCP Server Features .............................................................................................................................. 16

3.4.1. Configuring Lease Hooks................................................................................................................... 163.4.2. Configuring Address Hooks ............................................................................................................... 173.5. Additional Configuration for DHCP server if Relay Agents are used .................................................. 17

4. The DHCP Relay Agent ................................................................................................................................... 184.1. Configuring and building the VxWorks DHCP Relay Agent ................................................................... 18

4.1.1. PROJECT FACILITY BUILD..................................................................................................... 184.1.2.COMMAND LINE BUILD................................................................................................................. 224.1.3. Adding Appropriate route................................................................................................................... 23

5. BOOTP in VxWorks ........................................................................................................................................ 245.1. Incorporating Gateway Retrieval for BOOTP for boot ROM and Runtime Images................................. 245.2. Known Bugs: BOOTP............................................................................................................................... 27

5.2.1. �������������� ���������������������������������������.......................................... 275.2.2. bootpMsgGet() returning errno S_bootpLib_BAD_DEVICE when using bootpLibInit() ................ 27

6. DHCP Source Code Policy............................................................................................................................... 276.1. Source Code Listing .................................................................................................................................. 276.2. DHCP Client Code Samples...................................................................................................................... 28

7. Debugging Techniques for DHCP.................................................................................................................... 287.1. PACKET TRACES ................................................................................................................................... 28

7.1.1. Ethereal............................................................................................................................................... 287.1.2. tcpdump .............................................................................................................................................. 287.1.3. snoop................................................................................................................................................... 28

7.2. DEBUGGING WITH SOURCE CODE ................................................................................................... 298. DHCP Runtime Client Sample Code ............................................................................................................... 30

8.1. Runtime DHCP Client............................................................................................................................... 308.2. DHCP Loop Sample Code......................................................................................................................... 398.3. DHCP Lease Storage Hook Sample Code................................................................................................. 42

Page 4: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 4 of 46 September 2004

1. DHCP Configuration Files#��&"�(���������'�����3��1�'2,�2��'������'��,���'��/������(��5��6'��,���

1.1. In Project Facility�3��1'��1������(��5��6'��,��-��1/�,,�����3�,,�/���%)���*����3��,��3�,��+

usrNetDhcpcCfg.c PCD1.0, PID2.0usrNetDhcpcBootSetup.cusrDhcpcCfg.cusrNetDhcprCfg.cusrNetDhcpsCfg.c

#����3�,��'������<WIND_BASE>/target/config/comps/src/net directory, except for PCD 1.1, in which case theyare in <WIND_BASE>/target/config/comps/src/net/apps.

1.2. In Command Line

�3��1'��21�,����3���������'��,���-��1/�,,������3�,�+/target/src/config/1��.�/��*���

2. The DHCP Client

2.1. Boot DHCP Client

The following are the steps to include DHCP client in the boot ROM:

1. In the target/config/<your_BSP>/config.h file, define the following:

#define INCLUDE_DHCPC#define INCLUDE_DHCPC_LEASE_GET#define INCLUDE_DHCPC_LEASE_TEST#define INCLUDE_DHCPC_LEASE_SAVE

right before

/* Now include the global configuration information. */

#include "configAll.h"

2. From the command line build the bootrom

<$WIND_BASE>\target\config\<your_BSP>\make bootrom_uncmp.hex

3. Re-flash the board according to the instructions found in target/config/<your_BSP>/target.nr

Page 5: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 5 of 46 September 2004

2.2. Run time DHCP Client

7�1�'�21�,����1����&"�(�,������'%)���*���'��3���������'��,�����3�����(��5��6'��,���

2.2.1. Command Line Build���,1����3�,,�/���,�������8��.&9:��$;<'���<���3��<8��1�:�(;<���3����3�,�+

#define INCLUDE_DHCPC before

#include configAll.h

���������������������� ���������� � !�"���������������#������$��%

nmppc –numeric-sort vxWorks | grep –i DHCP

2.2.2. Project Facility Build���,1����&"�(�,���������/��*���'��,��'�����������3��(��5��6'��,�������(��5��6'��,��-3��',,����1��

�)���(�&�����,��+

��/��*���������

�;��/��*������,�

�;��/��*'��,��'����

�;&"�(���1�����,���

6��(�&���-��,��

.�/��*���������

�;.�/��*���,��'����

�;&"�(���������

�;&"�(���1�����,���

7�1/�,,�����'���3��1�'��������/�����1���,1����&"�(�,�������(��5��6'��,���#�����'*��/����1�=�(���

>�?���@-'������1�������������#��/��*'��1����������������������'���A&"�(�,�������'�������',B

����2��,�����������������������#�3��������������-����/���(��5��6'��,�����3��',,����1���)���

(�&���+

��/��*���������

�;2'�����/��*����',�C'���

Page 6: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 6 of 46 September 2004

�;2��,����������������������

�;&"�(������'����������',�C'���

�;&"�(�,�������'�������',

6��(�&���+

.�/��*���������

�;.�/��*���,��'����

�;&"�(���������

�;����������'����������',�C'���

�;&"�(�,�������'�������',

2.3. DHCP State Machine#��&"�(�,����'�D'�����������2����������������3'&"�(�����'����#��%)���*��,���3�,,�/���&"�(�,���

�'�D'�����'����������2�����6���E��

2.3.1. DHCP Lease Negotiation#����������3'�41�����'&"�(,�'������'���3��/'��+��&"�(�,��������'&���F%$�����'��='2��'��'�@�

&"�(���������������������'��/��&"�(F66$�����'����

2.3.2. DHCP Client Lease SelectionF�����F66$��'����,,����-���,����'*��'��������'��/����,�'�����,��'�������'�$GH$�#����'���

#��������'�*��/,��������$GH$�#/��'���I����'��'�������',�'���

#��&"�(�,���/�,,',/'����������,�'���33��/����,�����,�'�����-���'��,����3���������''��������

�,���������3���33����,�'����'�����'������-�����,�'���33���''������3���/�,,2���,�����

#�������:���=8����3'��;-8����6,'�;@�',,3'�,��3����������'�'�,'2,���3�����:����'���8����6,'�;�'�'�������#�H$=���������1�@-���',,/'��1��,������1���1��-�����1���$��F���3��8����6,'������6� �$-���'1�����������'��������*�'��1����1����',,�����1'��������*-���',,/�,,�1�������3'�,��,��,��6��������3���'���'2�1$���"��*�-��������������,��&"�($���"��*�

#������1',�������������3������6���J��'����E��#��3���1,'��'��)������',2'�*�33-/��������1��,��

���1��

2.3.3. DHCP Client Lease Renewal#��,�'������/',��1������'�����3�3��'��3���,����'��'����������������/�����1������)��1��-'

&"�(,�'����',��'��'���������,����#��,�'������/',2�����/�����3���&"�(�����)�����=#�@�#������/',

�����'�$GH$�#����'��������1���������-/�����������/����������/�,,',,�/���'��,�'���be used. If the server does accept the renewal, the renewing routine returns and the client reenters the bound state.

If the renewal does not succeed, the client goes back to the first state, INIT. If a timeout occurs before the second timer (T2) expires, theDHCP client retransmits the REQUEST message and waits for a reply. If the timer does expire, the client enters a rebinding routine, whichsends a broadcast REQUEST to all servers. In this case, a different server may respond to the client.

In the VxWorks DHCP client, the period of time the client waits for offers after receiving the first offer from a server is thevalue for offerTimeout in the function arguments.

2.4. Miscellaneous DHCP Issues

Page 7: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 7 of 46 September 2004

2.4.1. Adjusting Retransmission Times�������41��������'�3���1�����������&"�(�,���������/'����3��'��������3��������������������

�������1�������'�51����',1��3������3��������3DISCOVER_RETRANS, FIRSTTIMER, MAXTIMER, andINIT_WAITING. In Tornado 2.0 they can be found in target/h/dhcp/dhcpcBoot.h and dhcpcInternal.h. In Tornado 2.0.2 and2.2 they are in target/h/dhcp/dhcpcCommonLib.h. The meaning of these defines is as follows:

&���F%$�9�$#��.�–�')��1��1�2���3���'�����������3&"�(&���F%$�����'��1��,���,��������1�

6���##�D$�–��'2,�����3������'��������������3&"�(&���F%$��#�������12,��3��'���12��41��������������'�����1��,D�K#�D$�����'����

�.�#9���#�.!–��'2,������������3����3���������������'���2����

�,,��������'���1���,������,��#�������3,������/���',,'����/�1,������1����'���'���'�����=3��

�)'��,�'3��'��/���1'��@-',,������'���'�����'���������'�'�������'��1�������',1���

2.4.2. How Do I Change the DHCP Lease Interval?#���,����������41���'�������',������3�������������,-/�����'�02���'���������,��#��2��/'��',��

����/',�������'�51���,�'�������',�

6��'���'2,�����,�'��-���,��������'��41������'�������/�'3���',3���1�'���-�����������'�����1�

,�'��/�1,��'1����3�������/','��������1�'J���1������',���,���'���,�-���������'�1����33����,�'��

�1�'����'�������3�'�����3��������'���',1�/����#�'��#�������3����,�'�����'���'��-�������0

'��������''2��,1�,��1'�'����'3�)�������',2�/�����41���-�������������'���/�,,'���������

3��41��,��3�����������������,����3�������/','����

#�������0'�������3��',������������',�3��41������'����1�����������'����������-�)���3����/�������

��1��������1����33�����',1���'������1,���������6������3��'����

2.4.3. Using Boot Time DHCP (Command Line)

#�1����&"�(�,���'2�����-���,1����&"�(�,�������2���FD��'���$��

<'���<���3��<8:�(��������;<���3����+

#define INCLUDE_DHCPC

before the following lines:

#include “configall.h”

#include “pc.h”

Page 8: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 8 of 46 September 2004

�3��'������',���-��'�������3'1,2��,������'�/�,,1��&"�(�

>��3���&$6�H #9:FF#9 �.$L“3��=�-�@','��'��'�+�+<#���'��<'���<���3��<��(���1�<��3'1,<�)���*��M��?����E��� 3M�)��”

#���)��3,'�����&"�(3,'���3��1��������,1����&"�(�,�������2����'��-��:FF#(�,���/�,,�)��1�'�'�1��

��21�,�����'���

2.4.4. Why Does the DHCP Runtime Client Immediately Renew the Lease Acquired atBoot Time?

#���2��'�������1��2��'1��������������2�3����'�������1������'�����'���1,-����������2,��

�'�*���,'��������������2����'���2'������,�'���#����C��3����/�,�'����1������'��'����

����3��������������21������'��'����#������/',����3�����'�������'���������'����,�'���)������

2.4.5. Why Can’t I Acquire an Address Dynamically When Booting Over a Non-Networked Device?

#����/�������2��1����&"�(����'2�������������'�����/��*�'���'��'����33��1,��#���'������'��

���'����3����������������&"�(31�����',��=�(�>?E?N�@3��������/��*��2�����������/��*'��1���)���3��

#���'������1����-211���'�����#���'������������������-'������������2����,1����

2.4.6. Why is the DHCP Address Changing to a 10.0.x.x Address?����',�'����������������32����'�41����-������3'���������'��,���1��/�'�����'����#����1��������

������3'�������'����������)�)�)-�������2��'��'�'������-�12���'�*-'��3,1��������1���'2,���#��

��1������',,��2�3�������'���',�'�������'����3���������3��'������*���������

�����',�1�������'������,'����'2�1��/��*'���������3,���2��'1���3�����3�)=�(�N � N@/���������'�������

�������/�,,2���������'��������)��,�'���

2.4.7. Specifying an IP Address at Lease Request Time#�'�����'�����3���('�������'��41���������3'��-���3��1����&"�(�,�����=�,��@������#�����'��,��'2,��#���'�����'��

����#����'��������33��1,���/���1��������������#���'�������(,�'��������,��'���.��>��3��������3���'����

#���,�����2����'�������'�������1����/����������������� ���������'��1�����'�����

dhcpcOptionAdd(pCookie, _DHCP_CLIENT_ID_TAG, 6, etherAddress)

�3��1'��1����'%)���*�&"�(������-��������/'���'*�����',,��+

dhcpcOptionAdd(pCookie, _DHCP_CLIENT_ID_TAG, 7, etherAddress)

�������������� ��',,��',,���',,��'���'��3�����,���9����1�-'��������'��������,��'�������������

�����3���3����&''����,���;�����2���������,��'���.��>��3�����,����'�����'�,��

Page 9: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 9 of 46 September 2004

2.4.8. Incorporating Gateway Retrieval for DHCP for the boot ROM imageH�����3��/��������������'�/'�'���������1��/��������&"�(�33��'�������

#������-����3�bootConfig.c. 6���-����31������������� ��������������� ������ ��������������� ������ ��������������� ������ ��������������� ������ �

���� �������

struct ifnet *pIf; /* pointer to network interface data*/

char serverAddr [INET_ADDR_LEN]; /* DHCP server address*/

char bootFile [BOOT_FILE_LEN]; /* name of boot file*/

subnetMask; /* subnet mask */

void * pCookie;

struct dhcp_param bootParams;struct in_addrs routerStruct; <-- add

������������������������������������

����������������������� ����������� ������� ���������������������������� ����������� ������� ���������������������������� ����������� ������� ���������������������������� ����������� ������� �����

��������������� � �������������!������"��������������� �

routerStruct.num = 1;

routerStruct.addr = &routerAddress;

bootParams.router = &routerStruct;

/* Need target IP address to boot over the backplane */

������������������������������������������������������������������������������������������������

dhcpcOptionSet (pCookie, _DHCP_ROUTER_TAG);

if (pBootDevAddr[0] == EOS)

{

/* Attempt to retrieve address information from a DHCP server.

���������������� ������ ���������������� ������ ���������������� ������ ���������������� ������ �6������,���+

/*

• Fill in the target's IP address, if needed. The status

• variable indicates the source of the IP address as follows:

�����3�,,�/���'2����+/* fill in gateway address */

if(pGateway [0] == EOS && routerAddress.s_addr != 0)

inet_ntoa_b (routerAddress, pGateway); printf ("Gateway: %s\n", pGateway); }

����������������������������������������������������������������������������������������������'�����,�����2�� �'�=@��2�����3����+

/* if a gateway was specified, extract the network part

Page 10: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 10 of 46 September 2004

• of the host's address and add a route to this network */

printf("Will try to add default route.\n");

if (params.gad[0] != EOS)

{

if(routeAdd("0.0.0.0", params.gad) == ERROR)

printf("Error adding gateway as default route.\n");

else

printf("Router's address: %s\n", params.gad);

}

2.4.9. Why Does the boot ROM Have Its Own Version of DHCP?#��2���FD����1���'��������/���������3��&"�(����2��'1��/���������/'�����,����-����/'�

',���'�����2���FD��C��#��2���FD��������3&"�(��,�������2'�����',�'��'�����������������/',

����2�������3,�'����&1�����1����-���'��'��&"�(�(����2�1�������'��3&"�(2��,�2�'���(��',,��

#���'�����2������-����3�target/config/all/bootConfig.c.

2.4.10. What are the ARP Messages That I See Being Sent Out During Negotiation?����'&"�(,�'�������'��������3�����-�'���,������3����'���(���2������3��'���33����'������3�����

������������1��2�'������'������#�����(���2����1��/���'&"�(�����������'�F66$�����'�����

&"�(�,����#���,������������(2��'��'�'���������/��*�

#��2����41����',�� ���,1�����'�����������3'��'1��1���(����'�� 3�� ��2��'�������#���/�,,�'����

/���������&"�(/'�1�����2'��'�'��������3�����'�����,��������2'�*'���,�������(����'��-�

�����'��'�'���������3,��������/��*�#����'1��1���(����'��������1��,�'3��'���I����������

3�����&"�(�������

#����(����'����������*��F66$�����'���('������������41����'�����������&"�(�6��

"�/����-���'������3�'1�����,��������&"�(�,�����#����'1��1���(����'����'3�'1���3���(

�'�*��3��1/�����������-��1/�,,�'����'*���'���������1��������

2.4.11.The DHCP Boot Client Does Not Process Incoming OFFER Packets When UsingBSD Drivers

�(�>? EE��)������#���'������O�(�>N�E��/������:��*�,��('�*�6�,��=:(6@�#��:(6����������2,�3��

�'�1�����'�*��'���������������&"�(�,���'��,��'������������3���21����'�3���,����������'

&"�(&���F%$�����'��-�����������,����/��'�F66$�-'�����,����������������F66$��#����(�

/'�3�)����#���'�����

Page 11: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 11 of 46 September 2004

2.4.12. DHCP Event Hook#��&"�($���"��*������'�'/'���'��,���'�����������3��1�'����'�'������#�����*������*��2�1����

�������$���"��*���=@��1�������'���������1���'���-�����*���,3��������'�',���'����3��1�'���

�����3��&"�(,�'���1�����������$���"��*��=@����3'���

#/�������'����1�+&"�(�9 $��$9.$�'��&"�(�9 $��$9�.%� �&�#��&"�(�9 $��$9.$�����

���1��/�������'��/,�'�����1�����31,,���'2,������#��&"�(�9 $��$9�.%� �&���������'���'��

�'�'�����3�����������������,�'���'���,�����2�1����

#����1��������'����*�������*3�,,�/�+

status = use_parameter (pLeaseData->dhcpcParam, pLeaseData);

semTake (dhcpcMutexSem, WAIT_FOREVER);

if (status != 0)

{#ifdef DHCPC_DEBUG

logMsg ("Error configuring network. Shutting down.\n", 0, 0, 0, 0, 0, 0);

#endif

pLeaseData->leaseGood = FALSE;

}

else

{

pLeaseData->leaseGood = TRUE;}

semGive (dhcpcMutexSem);

2.4.13. Avoiding the S_dhcpcLib_NOT_BOUND Error

If you attempt to access DHCP API functions which assume a good lease is in effect, such asdhcpcVerify(), dhcpcOptionGet(), dhcpcServerGet(), dhcpcTimerGet(), or dhcpcParamsGet() fromwithin the DHCP Event Hook, an error is returned: S_dhcpcLib_NOT_BOUND#�����*�)��1��2�3���������3��1��9�'�'����=@��1��������'��,�������'�)��1��''

�������,�/���'��'�3��&"�(�,���/�,,������������������3��1�'�����3���'�����,,��1�����'�������&"�(

,�'���'�'�����',���2'���������D1�)����1�)���'������#������'�������������1,��/�,,�1'�'�����

�'��,���)��1��������������,'��������/���1'���)�,�����,'�-'��������������2�,���3�'������������

�/'����'��1�������������������������'2��������'/�'��/'�*��'��'���-���1�����!���=@/������

���*���������,�'���'��,���������/���'����,���'�����/'�*�'�',�/���������-�/�,,�����������/

��3���'���������,��

Page 12: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 12 of 46 September 2004

2.4.14. What Happens to Sockets that are Already Open When a DHCP Lease Expiresor Renews?

F������*��'��'33����-�������������/�����*������3��1�������*�����3��1����1��'�����3���('������/�,,

��2�'������2,��3'��/�('��������'��������

�3�����*��/������3��1����1���.�&&�9�.7-���'��/�('������/�,,��'33�������*�����������-

�)���3�������2,��������������3'�,1����#���',,���'��,��'���/�1,��'����'��,��1��3'�,1����

2.4.15. Why Does DHCP Begin a Full DHCP Negotiation Instead of Renewing theLease?

��#���'�����'������-&"�(����/',�3'�,�3��������,��*����'����'3����&"�(�,���������',�C��=�(�>N� ��@�

#������2,�����1����2��'1����&"�(�,����',,����time() 31�����-/������1������������,��*�����,'���

�������32�����-�����'�1',����3�'���3'�'��,��'���������������,��*���'�1',����3�'�-�/'�

�����2,��'����33������2�/�����������',������,��*���'����'�1',����3�'�/�1,�3'��)������,�'������

#����3���-��&"�(�,���2��'���'���1����,�'���'��)�����-'��������',�C����,�'���#������2,�����1����,���

#���'�����-#���'���������'�2������'������#���'�������3��1�'��#���'�����������-���'����������'����3��

��(�>N� ���

2.4.16. The DHCP Client is not Detecting Renewals when the Lease is in RENEW orREBIND

#��&"�(�,����'�'21����������#�����'*���3������������I����'������'3��'����/',��'��2������

��41���#������2,���'�2���3�)����#���'������#�����'����'�'�,'2,�3���������������3����(�>?E��E�

2.4.17. Restrictions on DHCP Packet Lengths (SPR #65264) Resulting in DroppedDHCP Offers

�(�>NJ�N�'�������������2,���3&"�(F66$��'�*���''�������������2���&"�(�,������������3���

21������1�����31,�����3'&���F%$�-3�,,�/��2�'�1�����31,�����3'�F66$�3�����&"�(�������"�/����-

��&"�(�,������������*1���F66$��'�*��#�����2,����'��21'2,����&"�(�,���0�:(63�,����3�,�����

�'�*���������'�E��2�����3��������'������1������-���'����������3��'���'���#���33���',3�)3�����

���2,���'�2�����,�'�����#���'������

2.4.18. Why is the DHCP Client Sending INFORM Messages?#��&"�(�,���������.6F�D����'���/������,�������3'����',��'�����3��1���-21��41����'������',

�'�'�����3��,��',���3��1�'��������'��������������'&"�(�.6F�D����'��-�/�,,�����1�'&"�(��I

����'��/��'��,��',���3��1�'����'�'�����'�������'�3�����,����

#��&"�(������/�,,����'&"�(��I�������������,����'����������P��'���03��,��3��&"�(�.6F�D

����'���

2.4.19. Timing issue after system tick rollover

#������',�1,'��������'*����'���1����������*��,,��������1��-������'�����2�,���3,�'�����2����

����/��'3�����������*��,,��������������������*��'E�2�������-���/�1,��'���������'��,���(�

� N�3�)��������1��

Page 13: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 13 of 46 September 2004

2.4.20. At boot, DHCP client sends out extra ARP request in wait_offer state�����#9F66$��'�-1������������'&"�(F66$�-���,���������1'���(��41���#�����'�������3���'��

��6���E��������2,������41������1,�2��'���'���3'�1�,��'��('������=����/�������(��41����

'��/����@���,���/�,,�'������#9F66$��'�1��,������1'������'����,�'�������'���2���������1

&"�(&���F%$���(� ����3�)��������1��

2.4.21. DHCP timeouts are incorrectly calculated after calls to clock_settime#����'����1���#���'�����'���������3��1��'&"�(,�'��-��'��������=3��/'��@-'��/'�3����,�'���

�)��������������-��&"�(�,������*��'��'�����2����������/������'�,��',,�����/��,�'��-'��

�'������3������.�#��'���#���'�����3�)��������1��

2.4.22. How long does the DHCP client wait for offers?The collection time is configured by modifying DHCPC_OFFER_TIMEOUT.

Wind River implements DHCP according to RFC 2131. This RFC does not specify any time limit as for howmuch should the client wait in the SELECING state for DHCPOFFER messages from servers. As such, in theSELECTING state, the Wind River client code will stay until the collection time expires and the offer with thelongest lease is the one being selected.

SPR 99445 has been filed for an enhancement which will allow the DHCP client to pick the first offer arrivedand move right away into the REQUESTING state, without waiting for this timer to expire.

2.4.23 DHCP attempting to ARP on interfaces that have IFF_NOARP set in if_flags.Since the requirement for checking duplicate IP addresses in RFC2131 is a SHOULD, not a MUST, it means itis possible to run DHCP client on interfaces which do not support ARP. SPR 95666 has been filed for thisissue.

2.4.24. Why do RFC 1541 compliant DHCP servers respond only after the 5-thDHCPDISCOVER sent out by a vxWorks DHCP client?Our DHCP client is RFC 2131 compliant. That means it handles variable length option field. For backwardcompatibility with servers which still implement RFC 1541 (that means fixed 312 byte length option field), weprovide a workaround: after 4 attempts of DHCPDISCOVER with variable length, we set dhcpOldFlag toTRUE and we revert to the fixed size option field. SPR 95958 has been filed to make this option configurable.

2.4.25. No access to possible duplicate IP address

According to RFC2131, once the client receives a DHCPACK from the server, it has to ARP on the IP addressjust received. If it gets a response, it means the given IP address is already in use and the client must send aDHCPDECLINE thus refusing the given IP address. vxWorks DHCP client does exactly that and is compliantwith RFC 2131. However, right now, there is no way for the user (that is the task calling dhcpcBind) to knowthe reason why the lease negotiation failed, let alone know the value of the duplicate IP address. SPR 102116has been filed as an enhancement to make the value of the duplicate IP available to the user.

Page 14: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 14 of 46 September 2004

3. DHCP Server

3.1. Configuration and Build

�*���&"�(�,���-��&"�(�������'�2����3��1��������'������'��,�����/�������5��3'��,���

3.1.1. PROJECT FACILITY BUILD

���,1����&"�(����������(��5��6'��,�����+

6��',,����1���)���(�&���

��/��*���������

�;��/��*'��,�'����

�;&"�(������

6��(�&���+

.�/��*���,��'����

�;&"�(���������

�;&"�(������

:�,�/��'�����������3��/�'��,�'�����������&"�(�������7�1�'�'����������������'���',,�/��'��(�������

���3��,��

#�'���������������3��,��-���usrNetDhcpsCfg.c:

DHCPS_LEASE_DESC dhcpsLeaseTbl [] = {

/*

Name Start IP End IP Parameters

------ -------- -----------------------

Page 15: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 15 of 46 September 2004

see man pages (timers, bootfile, etc.)

*/

/*

* Host requirements defaults needed for RFC compliance - DO NOT REMOVE!! */

{"dflt", NULL, NULL, DHCPS_DEFAULT_ENTRY},

/* Sample database entries. */

/* {"ent1", "90.11.42.24", "90.11.42.24", clid=\"1:0x08003D21FE90\":maxl=90:dfll=60"}, */

/* {"ent2", "90.11.42.25", "90.11.42.26","snmk=255.255.255.0:maxl=90:dfll=70:file=

/vxWorks"}, */

/* {"ent3", "90.11.42.27", "90.11.42.27", “maxl=0xffffffff:file=/vxWorks”}, */

/* {"entry4", "90.11.42.28", "90.11.42.29", "albp=true:file=/vxWorks"} */

/* {"entry5", "90.11.42.28", "90.11.42.29", "dnsd=205.214.51.16"} */

3.1.2. COMMAND LINE BUILD#����,1����&"�(������/���21�,����3���������'��,���-���,1����3�,,�/������������������3����3�,�/����

��:�(��������+

#define INCLUDE_DHCPS

���3��1����&"�(,�'����������1��.�/��*���#��'2,������'��'����������������E�����

�3��1/����'������&"�('2,�������'3�����1������'���'��-1��+

dhcpsLeaseEntryAdd()

3.2. How Do I Specify a List of DNS Servers in the DHCP Lease Entry?

7�1�'������3����'���3'&.���������',���3�('�����������'�'��2�/�����'������

'�������'�������+

H���������3���1,��,�&.��������+

-> dhcpsLeaseEntryAdd (<...lease entry...>, "dnsd=205.214.51.16

dnsd=192.169.33.3")

H���������3��'����,�&.�������+

-> dhcpsLeaseEntryAdd (<...lease entry...>, "dnsv=205.214.51.16")

Page 16: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 16 of 46 September 2004

#�������'�����'��/����&.�������-��������J�

3.3. Configuring the DHCP Server as a BOOTP Server$������'��1�1����&"�(�������'�2������3�����,�����,��:FF#(���,����

������%)���*�&"�(����������������41���3���':FF#(�,���-�/�,,���,��3��������3,'���������&"�(

�����������

#��3,'���P',2�0

/* {"entry4", "90.11.42.28", "90.11.42.29", "albp=true:file=/vxWorks"}

3.4. DHCP Server FeaturesThe DHCP Server can store leases if the server is shutdown then restarted. The hook helps to store active lease configurations.

3.4.1. Configuring Lease Hooks#����3��1��,�'�����*�-3�,,�/������1�����2�,�/�

�@�3��1'��1������(��5��6'��,��-&�3�����3�,,�/����'�����'���<���3��<�����<���<��<1��.�&�����3���

�D(F�#�#�#H�&"�(�9 $��$9"FFI=��-��'�Q-��@R

�3��1'��21�,����3���������'��,����'����'���(��5��6'��,��-���<'���<���3��<',,<���3���,,��'����3�����3�,,�/���+

>��3���&"�(�9 $��$9"FFI

�@$������� �'��#2,ST'�����������1�������#���'2,����'�����&"�(�������������'��1������1���

E@����' �'��"��*�!1���,����'����������'������3������1����

������%)���*�&"�(�,����'��2'����',�'��3������)���*�������-������'�'�2'����,,�������1����'

�1������������1���'/�,,�',,�� �'��"��*��1����#�����*�����''�',��,�'����,�'����1'��/�,,�',,��

/�����1����3��,�'�����*�#��������/�,,2��'�������������1���3����'��2�����'�����7�1�'�',��

�'�1',,��',,��1����1�����dump_bind_db() ��1����'/�,,/����1'��/,�'��3�,�-�3�������''���',��

,�'����)���

#�������',�3�����1���'�2���'����2�'�51����!�9�.#$�.� -��3������'���<�<����<������������������

�������'������3'1,�',1���N���

Page 17: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 17 of 46 September 2004

3.4.2. Configuring Address Hooks#��&"�(�������������"��*1�'�������1���������.�/��*(����'����0�D'�1',�

=�@Write a storage hook routine.

=�@H����'%)���*� ��'�����'����� ��&"�(������'�� ���25�� 3�,�����1���/��������,��� ��''������1�������-��� ���'�1���41���� � ���',, �� �� ��1���'� 3�,,�/�2�3��� ��&"�(����������',�C'���+

dhcpsAddressHookAdd (sampleAddressStorageHook)

The DHCP Server Address Hook will not work with statically configured entries. Use dhcpsEntryAdd() for entries that the hook willaccess.

Changing the DHCP Configuration Will Not Appear on the DHCP Client Until a New Lease Is Negotiated.#��&"�(�,���'��,�����,��',�('������'���12���'�*'1��'��',,��3���',1��3��8'1����3��;3,'��������'

2��'������/�,,������'*�'��������'��������,�������',1������&"�(������3��,�-���,1��������3'1,�'�/'�

'�������#����3���-��&"�(�,���/�,,��������'�����'�����������������3���'���������&"�(�33��

�'�*��

3.5. Additional Configuration for DHCP server if Relay Agents are used

�3��&"�(���������1������������1,�'���3���,������1'������33�����12���-����1,�2��'�'2,��3�����������

��41�����������3�����,'�'���=�@��������������-��3�,,�/����'��2�3�,,����+

<Q

Q�3��&"�(������/�,,�����������'���3�����,'�'����-

Q��3�,,�/���'2,��1�2�3�,,����2���1����

Q<

&"�(�9�$ �79&$���������,'�#2,STM

U

<Q

�('�������3'����12��.1�2��

���������������������������������

Q<

<QUV ���������J�V-V ���������VW-Q<

U.H -.H W<Q �������'��-�1�2�,'�Q<

WR

#���('�������3��'�����������3'���'����'���'������13���������'�����������'����'��3���'&"�(

�������#���12���1�2�����1,��'������/��*�1�2���3��'����('�������

�3��121�,�3��������5��3'��,��-����''�'�2�3�1����'���<���3��<�����<���<��<1��.�&�����3����

Page 18: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 18 of 46 September 2004

6��21�,����3�������'��,�����'�2�3�1����'���<���<���3��<1��.�/��*��

4. The DHCP Relay AgentThe DHCP relay agent sends DISCOVER messages from a DHCP client on one subnet that lacks a DHCP server to specificDHCP servers on a different subnet. While some routers on the network may be capable of relaying messages, they do notforward DISCOVER packets (because they are broadcast messages). This is the reason relay agents should be implementedon the far side of the router.

4.1. Configuring and building the VxWorks DHCP Relay Agent#��3�,,�/�������1�����������2���/����3��1��'%)���*�&"�(��,'�'���'���������6��1����,,1��'����

���3��1�'����'������1�����������2��

Figure 1: DHCP Relay Agent Configuration

#$%&##&$#&'( #$%&##&$#&#'( #(&#&#&)( �*��������

�������������������������������������������������������������������������������������������������

&"�(������ X &"�(��,'����� X &"�(�,���

#��������1�����'��,��#���'��������������'�������3��1�'��'��'�,����������-��1�1�1���'���#���'������

3��'31��������&"�(��,'������

4.1.1. PROJECT FACILITY BUILD���'�'��/usrNetDhcprCfg.c 3�,�����'�����,����3��,��-usrNetDhcprCfg.c '��������'��/3�,�-

usrNetDhcprCfg.c

��������.��+������'��,�����,��#���'�������

D'*���'���������/3�,�+

/* IMPORT void dhcprLibInit (); */

IMPORT void dhcprLibInit (int);

��1��&�����'�=@31�����+

/* dhcprLibInit (); */

dhcprLibInit (DHCPS_MAX_MSGSIZE);

� � � ���51���&"�(��,'�'���'2,���usrNetDhcprCfg.c:

If you leave this table empty, the relay agent will not start.

DHCP_TARGET_DESC dhcpTargetTbl [] = {/*

IP address of DHCP target servers--------------------------------*/

/* {"90.11.42.2"}, */

Page 19: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 19 of 46 September 2004

{"147.11.38.50"} /* DHCP Server address

WR

����',�C�',,����3'�����1������1��������,'�'����#����'��/�/'�������',�C���

��,'������$'�����1,����2�,�/�

D$#"F&�

����3�,�../target/config/comps/src/net/usrNetEndBoot.c, '��'31������'����',�C��

������3'���6���)'��,�+

void addInterface(void)

{

ipAttach(1, “fei”);

usrNetIfConfig(“fei”, 1, “10.1.1.20”, “private”, 0xffffff00);

}

Call '������3'��=@'������31��.�$��&���'�=@

Example:

.

.

.

netAttachFlag = TRUE;

printf ("Attached TCP/IP interface to %s unit %d\n", pDevName, unitNum);

addInterface();

#endif /* INCLUDE_END */

return;

��../target/comps/src/net/usrNetDhcprCfg.c:

Modify

/* struct ifnet *devlist[1]; */ <---- original line

struct ifnet *devlist[2]; <---- two devices

��'���3��+

Page 20: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 20 of 46 September 2004

dhcprInit()

���+

devlist[1] = ifunit ("fei1");

�3��%)���*�'���/�,,2��������'���,'�'���'����1��-��'������1�2���3��������'����,'�'�������',�C����3��1'��/��*�����'����,��12��-����,'�'���/�,,����'�������������'�����'����,�����3'���

#���)'��,����/�/�����3'�������'��3���+

dhcprResult = dhcprInit (devlist, 2, dhcpTargetTbl, dhcpTargetTblSize);

Include the DHCP Relay Agent component with the Project Facility:

network components ->networking protocols -> network applications -> DHCP relay agent

D$#"F&�Make all the changes in usrNetDhcprCfg.c:

/* struct ifnet *devlist[1]; */ <---- original line

struct ifnet *devlist[2]; <---- two devices

ipAttach(1, "fei");

usrNetIfConfig("fei", 1, "10.1.1.20", "private", 0xffffff00);

devlist[1] = ifunit ("fei1");

��'������1�2���3����',�C���������3�������/��

/* dhcprResult=dhcprInit (devlist, 1, dhcpTargetTbl, dhcpTargetTblSize);*/

dhcprResult = dhcprInit (devlist, 2, dhcpTargetTbl, dhcpTargetTblSize);

���'�'��/��33�,�2'�����'���<���3��<�����<�)���*�<����/��*���3�

.'����3�,�50network.cdf. ��'�����50network.cdf 3�,�3������,'�'����

��../target/comps/src/net/50network.cdf

���&"�(�9D�K9D�!��Y$����6!9(���D�

Page 21: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 21 of 46 September 2004

�',,���3�,�60network.cdfComponent INCLUDE_DHCPR

{

NAME DHCP relay agent

CFG_PARAMS DHCPS_SPORT DHCPS_CPORT \

DHCP_MAX_HOPS DHCPS_MAX_MSGSIZE

REQUIRES INCLUDE_NET_SETUP

HDR_FILES ioLib.h

}

D'*��1���'��3�,,�/����'�'��������3�����

Parameter DHCPS_MAX_MSGSIZE{NAME DHCP Server/Relay Agent Maximum MessageDEFAULT 590TYPE uint}

���'������dhcpsRelayTbl[]. #��'2,�����usrNetDhcpsCfg.c

DHCPS_RELAY_DESC dhcpsRelayTbl [] =

{

/*

IP address of agent Subnet Number

----------------------- -----------------

*/

{"10.1.1.20", “10.1.1.0"} };

#���('�������3��'�����������3'���'����'���'������13���������'�����������'����'��3���'&"�(

�������#���12���1�2�����1,��'������/��*�1�2���3��'����('�������

&���������� !������'�������$���������������(���#�������%

#�����$����������$��!�)�)%

Page 22: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 22 of 46 September 2004

��/��*����������;��/��*���������,��;��/��*'��,��'�����;&"�(��,'�'���

6��(�&���+

.�/��*���,��'�����;&"�(����������;&"�(��,'�'���

4.1.2.COMMAND LINE BUILD+

�3��1'��1����������'��,���-D'*���'�����target/src/config/usrNetwork.c

1. In usrNetwork.c, add the following information to account for multiple devices on the relay agent.

/* struct ifnet *devlist[1]; */ <---- original line

struct ifnet *devlist[2]; <---- add this line for two devices

ipAttach(1, "fei");

usrNetIfConfig("fei", 1, "10.1.1.20", "private", 0xffffff00);

devlist[1] = ifunit ("fei1");

��'������1�2���3����',�C���������3�������/��

/* dhcprResult=dhcprInit (devlist, 1, dhcpTargetTbl, dhcpTargetTblSize); */ dhcprResult =

dhcprInit (devlist, 2, dhcpTargetTbl, dhcpTargetTblSize);

��������#������'��,����#���'��������,�����1'���1�����#���'�����-�,�'��������E�

��51���,�2�'������',�C'�����1��.�/��*��'�3�,,�/�+

/* IMPORT void dhcprLibInit (); */

IMPORT void dhcprLibInit (int);

/* dhcprLibInit (); */

dhcprLibInit (DHCPS_MAX_MSGSIZE);

��������.�/���1,'�����,'�'���'2,�-dhcpTargetTbl[]. #��dhcpTargetTbl[] /�,,���'��&"�(�������('���������3

����������'/�,,���������,'�������'�����'����,'�'����#��&"�(��,'�#'���#'2,�����usrNetwork.c

If you leave this table empty, the relay agent will not start.

DHCP_TARGET_DESC dhcpTargetTbl [] =

{/*

IP address of DHCP target servers --

------------------------------*/

Page 23: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 23 of 46 September 2004

/* {"90.11.42.2"}, */

{"147.11.38.50"} /* DHCP Server address */ };

���'������dhcpsRelayTbl[]. #��'2,�����usrNetwork.c

DHCPS_RELAY_DESC dhcpsRelayTbl [] =

{

/*

IP address of agent Subnet Number

----------------------- -----------------

*/

{"10.1.1.20", “10.1.1.0"} };

#���('�������3��'�����������3'���'����'���'������13���������'�����������'����'��3���'

&"�(�������#���12���1�2�����1,��'������/��*�1�2���3��'����('�������

���3��1����&"�(������/����'����������1/���������21�+

DHCPS_LEASE_DESC dhcpsLeaseTbl [] = {

/*

Name Start IP End IP Parameters

---- ------- ------- ----------

* see man pages

* (timers, bootfile,

etc.) */

/* Host requirements defaults needed for RFC compliance - DO NOT REMOVE!! */

{"dflt", NULL, NULL, DHCPS_DEFAULT_ENTRY},

/* Sample database entries. */

{"ent1", "10.1.1.1", "10.1.1.1", "snmk=255.255.255.0"}

WR

4.1.3. Adding Appropriate route������1�'���&"�(������-'����'�������'���1�=�@+

routeAdd (“10.1.1.20”, “147.11.41.150”);

#��'2���'���'�����1�����������=��&"�(��,'�'�����('��������������������/��*@/����

��?��������J�'����'�/'��

#���1�1�3�����('�'�����/�����,���'3����,�'��/'��2'����+

->dhcpcParamsShow

Page 24: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 24 of 46 September 2004

Router discovery enabled.

RFC 894 Ethernet encapsulation enabled.

Maximum datagram size: 576

Default IP Time-to-live: 64

Interface MTU: 576

ARP cache timeout: 60

Default TCP Time-to-live: 64

TCP keepalive interval: 7200

Client lease origin: 212

Client lease duration: 3600

Client renewal (T1) time value: 1800

Client rebinding (T2) time value: 3150

DHCP server: 147.11.41.176

Assigned IP address: 10.1.1.1

Relay agent IP address: 10.1.1.20

Client subnet mask: 255.255.255.0

Client broadcast address: 255.255.255.255

Client router solicitation address: 255.255.255.255

5. BOOTP in VxWorks#��:����'�(�����,=:FF#(@/'�'����1�����3&"�(��:FF#(�,�����41�����3���'���3���'

:FF#(�������#��:FF#(������*�����,�����3���'�����/��''2'����F���''2'���������D��

'���������3�,����'���������'��'3�,��'������,���'�������#��3�,��'��������P3�,�03��,�-'��

/�,,�����3�/����3�,��,�'��#��:FF#(�,����'�2�����*��'2�������'�1�����#�����':FF#(

����'����#���'�����'��#���'�������-1����2���D������=@��1����#�����':FF#(����'����

#���'�����1����2���D��!�=@��1���/�������2�����������'����'��'�������'�����������

������1����������(���3�������

#��2�����3����3�,�������������3��1�'������1,�3��2���FD��#��2�����3����3�,�����

8Z��.&9:��$;,L'���L���3��L',,

5.1. Incorporating Gateway Retrieval for BOOTP for boot ROM andRuntime Images

�*�&"�(-:FF#(������'1��'��',,������'�/'�'���������1��/����':FF#(��41��

���1�����&�'�,��������1���+

��'����������'���'�/'�/�,,2�������'�����3'1,��1�=�������@

'@$��bootConfig.c �����������'�/'�3����2���FD��'���

2@�3��1'��21�,����3���������'��,���-���/target/src/config/usrNetwork.c

�@�3��1'��21�,����/����(��5��6'��,��-���

/target/config/comps/src/net/usrNetRemoteCfg.c

/* if a gateway was specified, extract the network part of

* the host's address and add a route to this network

Page 25: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 25 of 46 September 2004

*/

if (sysBootParams.gad[0] != EOS)

{

inet_netof_string (sysBootParams.had, nad);

routeAdd (nad, sysBootParams.gad);

}

For example:

'���������'������M��?�������� �+333333��

���'������M��?���������

���'�'�/'���������M��?��������J�#���

����/�1,�'��'��1����'���������FH#$

.$##�: $

�����������������147.11.48.0

147.11.41.254 147.11.41.0

147.11.41.198

�3��1����������'�����'�/'�'�'��3'1,��1���������-��3�,,�/���'�������'��������'��+

if (sysBootParams.gad[0] != EOS)

{

/* Extract the network address in dot notation */

inet_netof_string (sysBootParams.had, nad); /* for host address */inet_netof_string (sysBootParams.gad, ngad);

/* for gateway address */

/* gateway and host are on same subnet */

if (strcmp(nad, ngad) == 0)

{

/* Add default route */

if (routeAdd ("0.0.0.0", sysBootParams.gad) == ERROR)

printf("Error adding default route: ");

}

else

{

/* Add network route */

if (routeAdd (nad, sysBootParams.gad) == ERROR)

printf("Error setting route to host ");

}

}

/������'���'�S��T�'�2���'�������'1��'���'��'2,��

Change bootConfig.c to retrieve gateway parameter and store it so that it will be retrieved by the runtimeimage.

Page 26: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 26 of 46 September 2004

'���'����������3��bootpGet() ����,1�����'�/'��'�'����+

LOCAL STATUS bootpGet (char *netDev, char *pBootDevAddr,

char *pBootFile, char *pHostAddr, int *pMask, char *pGateway);

2�('���'�'����'����'���'�/'��'�'����/�,,2������

/* Get boot parameters over the network if requested. */

if(sysFlags & SYSFLG_AUTOCONFIG)

{

if (bootpGet (netDev, pBootAddr, params.bootFile,

params.had, &netmask, params.gad ) == ERROR) return (ERROR);

�������3�,,�/����2���!�=@+

/************************************************************************

*

* bootpGet - get network parameters via BOOTP.

*

* RETURNS: OK if successful otherwise ERROR

*/

LOCAL STATUS bootpGet=char *netDev, /* boot device */

char *pBootDevAddr, /* device address

char *pBootFile, /* file name */

char *pHostAddr, /* host address */

int *pMask, /* mask */

char *pGateway /* subnet gateway */

)

...

struct in_addr_list routerlist;

struct in_addr gateway;

...

bzero ( (char *)&gateway, sizeof (struct in_addr));

...

/* Set pointers to retrieve needed boot parameters. */

...

routerlist.addr = &gateway; routerlist.num = 1;

bootParams.routers = &routerlist;

...

�6#$�

printf ("Getting boot parameters via network interface %s, pNetDev);

if (bootpParamsGet (pNetDev, 0, 0, &bootParams) == ERROR)

return (ERROR);

�&&+

if (pGateway [0] == EOS) /* fill in subnet gateway */

{

inet_ntoa_b(gateway, pGateway);

printf (" Subnet gateway: %s ", pGateway);

Page 27: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 27 of 46 September 2004

}

5.2. Known Bugs: BOOTP

5.2.1. !!!!�"������� ���� �������������������#$$�!��%� � �����������&��"������� ���� �������������������#$$�!��%� � �����������&��"������� ���� �������������������#$$�!��%� � �����������&��"������� ���� �������������������#$$�!��%� � �����������&�

#��21���'�����������(�>N?E���#�������������3�)���1���'���#���'�������3��1'��1�'2,�����3���'�1���'��-���'����������3��'�1��33���',�'���

5.2.2. bootpMsgGet() returning errno S_bootpLib_BAD_DEVICE when usingbootpLibInit()

This is an issue in Tornado 2.2.1. If an interface number other than 0 is used and there is no interface 0defined (e.g. fei1, but there is no fei0), using bootpMsgGet() will return error with the errno set toS_bootpLib_BAD_DEVICE. SPR 94241 fixes this problem.

6��������3���'���'2�1:FF#(-�����%)���*���3������D'�1',����3��2��� �2��

6. DHCP Source Code Policy���������/�,,�����������1���������1������/�������'��'��1�������,�������F��'����',,�-�)�������'��

�'��3���1������/��'��������21���'�'��*��/�3�)����/��*'��1�����1���)�������'�����������'��

����������3���1����������������'���1��'�'����

6.1. Source Code Listing#��3�,,�/�����',��'�������������3��&"�(��1����������1,��+

<��/��

�����:�� �2�� ����3'��3���,�������&"�(1����1���������2��

����������� �2�� ���'���������,�2�'����1����

����� �2�� �1�����'��������&"�(�,���

��������/�� &"�(���/��1����

����� �2�� &"�(��,'�'�����1����

����� �2�� &"�(��������1����

<����

������,'��� ��1������������'�*����'���1�3����,'�'���

������� �'��'��������3��1�'�������

�����:���� ����2��,�2�'������

������'���� �,����'��'�������1������������:FH.&�'�

Page 28: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 28 of 46 September 2004

������'���� �,����'��1����'��'3��:FH.&�'�

�����9�12��� ���'��������',��1����

������� ��,'�'�����1����'���������������������'���

������� �'����&"�(������

6.2. DHCP Client Code Samples�����'��,��'���������������������)�#�����'��,��'��1��1�������#���'����������2��)'��,���3

��/�1�����(�����'�����������3��1�'����33��1,�1������&"�(�(�0��

dhcpcRuntime.c - '��)'��,��3��/�1�������:���=@��2'��',�'����,�����,1���'��)'��,��3��/������&"�(,�'���

dhcpLoop.c - �����1',,������&���F%$��'�*��1��,'����������������,�����'����������*

1�'���

dhcpsSampleHook.c - �)'��,��3'&"�(,�'�����'�����*��1���

Note: Sample Code is unsupported

7. Debugging Techniques for DHCP

7.1. PACKET TRACES#������.�/��*����1�����'33��21����&"�(�,������1��/�������+���33���'���'����21�

����'���/��������1��������F2'��'�'�*��'���3�����2,����3��1'��1�3'��,�'�/�����33���'���-

���+��+<<///���2����'�'�����<�12�<���33����3'4���,

����������'33���3�������3��3�,,�/����'��3���'�+

7.1.1. Ethereal

#��������'���2'��������1��-21��'!H���������

��+<<///������',����<�

7�1�'�1�������',��2��H��)'�������/��'�������

7.1.2. tcpdump#���,���'�����������/���'������'��6�������3��'��1�'�����1������1��'������7�1�'������3�

�'33��2'�������������D��'��������3����&"�(�,���'����������3��1���0*��/�3��1�'�����1��

'�'�,'2,�����-����*/����1������'�������'���

7.1.3. snoop�������'��1�,��3���1���,'����'������

Page 29: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 29 of 46 September 2004

��+<<///�����������'����1<����2��<�3�<�3��?N����,

#���1�����'33���3�������������'�����2��'��3���'�#�����2��'1��2������'�����2���3����'��'�'�,'2,��

7�1�1�2�������'����������,������������1�������

"���0�'��)'��,�+

%snoop -o outputFile 147.11.38.89

�3�,�-�1�16�,�-�������'���#�'3�,�������'�'2,�'��

���6��'�����1�1+%snoop -i outputFile > outputFile.txt

6��'���2����1�1-���,1�����'�*���3���'���+

%snoop -i outputFile -v > outputFileVerbose.txt

�,���'����'�*��'��1�,����'��+��,�('�*��$���(��*���D�.�/��*D�����-/���������/����

D������3�����D'�'������������

Save the traces in the available binary format, rather than sending large text files.

7.2. DEBUGGING WITH SOURCE CODE#�����',/'��������2,�������&"�(�,���-������'����,'�'������'�41�������1�������'��1����

��21���������'����

�3��1�'������1�������-��������'������������1���'��-'����3�����3�,,�/���3,'����������+

DHCPC_DEBUG 1 For DHCP client

DHCPS_DEBUG 1 For DHCP server

DHCPR_DEBUG 1 For DHCP relay agent

�3����3�������3,'��-��1/�,,�����21��1�1'��������'������������

�3��1/�1,�,�*���������21���������'���-21���0�'������1�������-���'�������������41������

�����,���25�������'���,1�����21���������'����

�3����3�������3,'��-��1/�,,���,������'����'3�,,�/��&"�(�'��'������

Page 30: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 30 of 46 September 2004

8. DHCP Runtime Client Sample Code

8.1. Runtime DHCP ClientThe following demonstrates a runtime DHCP client negotiating a lease. To run it, simply type

-> getDhcpLease “<myinterface>

To stop the lease type

-> callStop “<IPaddress>”, <subnet mask>, “<default_route>”

/* dhcpcRuntime.c - Get lease after boot time */

/**************************************************************************** ** THIS CODE IS NOT SUPPORTED CODE. IT IS PROVIDED ON AN AS-IS BASIS.* ***************************************************************************** */

/* Copyright 1992 - 1998 Wind River Systems, Inc. */

/*modification history--------------------

Page 31: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 31 of 46 September 2004

01a,06jul00,ijm based on dhcpcConfigSet */

/*DESCRIPTIONThis file contains routine to get a DHCP lease after booting with a static IP address

NOMANUAL*/

#include "vxWorks.h"#include <ioctl.h>#include <inetLib.h>#include "dhcp/dhcpcInit.h"#include "dhcp/dhcpcCommonLib.h"#include "dhcpcLib.h"#include "bootLib.h"#include "stdio.h"#include "netinet/if_ether.h"#include "ifLib.h"

#define MAX_LEASES 10#define SERVER_PORT 67#define CLIENT_PORT 68#define OFFER_TIMEOUT 5/* #define OFFER_TIMEOUT 5 change to test */#define DEFAULT_LEASE 3600#define MINIMUM_LEASE 30#define MAX_SIZE 576

/* Uncomment the following if you want event hook routine and/or cache hook routine *//* #define DHCP_EVENT_HOOK_PRESENT *//* #define DHCP_CACHE_HOOK_PRESENT */

/* prototypes */#ifdef DHCP_EVENT_HOOK_PRESENTvoid myEventHook(int event, void *cookie);#endif

#ifdef DHCP_CACHE_HOOK_PRESENTSTATUS myCacheHookRtn(int command, unsigned long *pTimeStamp, int *pDataLen, char *pBuffer);#endif

/* IMPORT BOOL dhcpcInputHook(struct ifnet *pIf, char * ifName, int unit); */

/* global */void *pDhcpcRuntimeCookie;LEASE_DATA *pLeaseData;

Page 32: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 32 of 46 September 2004

/***************************************************************************** * ifEtherPrint - print Ethernet address in ":" notation * * RETURNS: N/A */

LOCAL void ifEtherAddressPrint(u_char enaddr[6] /* ethernet address */){unsigned char *en = (unsigned char *)enaddr;

printf("Ethernet address is %02x:%02x:%02x:%02x:%02x:%02x\n", en [0], en [1], en [2], en [3], en [4],en [5]);}/************************************************************************* *o getDhcpLease - get IP address from DHCP server. Configure interface *" This routine gets IP address at runtime after the target has booted" (perhaps with a static IP addresss). It shows how to use tags like the" ROUTER_TAG and CLIENT_ID_TAG. If this function is called for an" additional interface, dhcpcInit() call in this routine will setup the" necessary data structures and create a cookie identifying the" boot lease. The cookie is stored in the pDhcpcBootCookie global to" provide users with access to the boot-time lease. This routine is called" when initializing the network from usrNetInit() in usrNetwork.c and will" not function correctly if executed in any other context." usage: E.g. getDhcpLease "motfcc0"** RETURNS: OK if configuration completed, or ERROR otherwise*o ERRNO: N/A *o NOMANUAL */

STATUS getDhcpLease (char * netDev ){ struct dhcp_param dhcpParameters; struct ifnet * pIf; char tempString[30]; /* LEASE_DATA * pLeaseData; */

struct in_addrs routerStruct; /* gateway address */ struct in_addr routerAddress; struct in_addr dnsAddress;

Page 33: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 33 of 46 September 2004

char dhcpServerAddr[INET_ADDR_LEN]; char dnsServerAddr[INET_ADDR_LEN]; char targetAddr[INET_ADDR_LEN]; char gatewayAddr[INET_ADDR_LEN]; int netMask = 0;

struct in_addrs dnsServerStruct;

u_char etherAddress[7]; etherAddress[0] = 0x1;

/* Put your own Ethernwt address here*/ etherAddress[1] = 0; etherAddress[2] = 0x1e; etherAddress[3] = 0xa0; etherAddress[4] = 1; etherAddress[5] = 0x16; etherAddress[6] = 0;

/* DHCP Client Library initialization. Function returns TRUE if it has " been initialized previously. This assumes that the runtime image o was configured with DHCP client support. */

if ((dhcpcLibInit(SERVER_PORT, CLIENT_PORT, \ MAX_LEASES, MAX_SIZE, OFFER_TIMEOUT, \ DEFAULT_LEASE, MINIMUM_LEASE) == ERROR)) { printf("\ndhcpLibInit failed!"); return ERROR; }

/* For Tornado 2.0 use this call * if ((dhcpcLibInit(SERVER_PORT, CLIENT_PORT, MAX_LEASES, OFFER_TIMEOUT, * DEFAULT_LEASE, MINIMUM_LEASE) == ERROR)) * { * printf("\ndhcpLibInit * failed!"); * return ERROR;

*/

/* Must bzero structures! dhcpcParamsGet copies parameters for non-blank * fields. Exceptions may occur if memory has not been allocated. */

bzero ( (char *)&dhcpParameters, sizeof (struct dhcp_param)); bzero ((char *) &routerAddress, sizeof (struct in_addr)); bzero ((char *) &dnsAddress, sizeof (struct in_addr));

Page 34: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 34 of 46 September 2004

/* Get interfaces IP control structure */

pIf = ifunit (netDev); if (pIf == NULL) { printf ("Invalid device: %s\n", netDev); return (ERROR); }

/* If dhcpcLibInit was called at boot time, dhcpcInputHook was added to boot device. * If this code is run for additional interfaces, dhcpcInputHook must be * added to that device as well. etherInputHookAdd checks for duplicate hook routines. */

/* if (muxDevExists (pIf->if_name, pIf->if_unit)) etherInputHookAdd(dhcpcInputHook, pIf->if_name, pIf->if_unit); else etherInputHookAdd(dhcpcInputHook, NULL, pIf->if_unit); */

/* " Initialize required variables and get the lease identifier. " The resulting lease will always apply the address information o to the specified network interface. */

#ifdef DHCP_CACHE_HOOK_PRESENT dhcpcCacheHookAdd(myCacheHookRtn);#endif

pDhcpcRuntimeCookie = dhcpcInit (pIf, TRUE); if (pDhcpcRuntimeCookie == NULL) { printf ("Error initializing DHCP runtime lease.\n"); return (ERROR); }

#ifdef DHCP_EVENT_HOOK_PRESENT if(dhcpcEventHookAdd(pDhcpcRuntimeCookie,myEventHook) == ERROR) { printf("\n dhcpcEventHookAdd error \n"); return; }#endif

/* Use the cookie to access the lease-specific data structures. */ pLeaseData = (LEASE_DATA *)pDhcpcRuntimeCookie;

/* Set the lease descriptor. Interface will be initialized with lease values. */

Page 35: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 35 of 46 September 2004

pLeaseData->leaseType = DHCP_AUTOMATIC;

/* If you want a manual lease, set the lease type to DHCP_MANUAL */ /* pLeaseData->leaseType = DHCP_MANUAL;*/

/* Set some options, dhcpcInit gets subnet mask by default */ dhcpParameters.subnet_mask = (struct in_addr *) &netMask; /* Set DNS server tag */ dnsServerStruct.num = 1; dnsServerStruct.addr = &dnsAddress; dhcpParameters.dns_server = &dnsServerStruct;

dhcpcOptionAdd (pDhcpcRuntimeCookie,_DHCP_DNS_SERVER_TAG, 0, NULL); /* dhcpcOptionSet (pDhcpcRuntimeCookie, _DHCP_LEASE_TIME_TAG); */

/* Set ROUTER_TAG */ routerStruct.num = 1; routerStruct.addr = &routerAddress; dhcpParameters.router = &routerStruct;

dhcpcOptionAdd (pDhcpcRuntimeCookie,_DHCP_ROUTER_TAG, 0, NULL); dhcpcOptionAdd (pDhcpcRuntimeCookie,_DHCP_SUBNET_MASK_TAG, 0, NULL);

bcopy ((char *) ((struct arpcom *)pIf)->ac_enaddr, etherAddress + 1, 6);ifEtherAddressPrint(etherAddress);

/* To make this call, set up the etherAddress array above as the MAC address*/

printf("Adding CLIENT_ID tag.\n"); if (dhcpcOptionAdd (pDhcpcRuntimeCookie,_DHCP_CLIENT_ID_TAG, 7, etherAddress) ==ERROR) perror("CLIENT_ID_TAG");

/* Execute the bind call synchronously */ if (dhcpcBind (pDhcpcRuntimeCookie, TRUE) != OK) { printf ("Can't establish a DHCP runtime lease.\n"); return (ERROR); }

/* Get Parameters from runtime cookie */ if (dhcpcParamsGet (pDhcpcRuntimeCookie, &dhcpParameters) == ERROR) { printf ("Can't get network data from DHCP runtime lease.\n"); return (ERROR); }

Page 36: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 36 of 46 September 2004

/* Fill in server addresses. */ if (dhcpParameters.server_id.s_addr != 0) { inet_ntoa_b (dhcpParameters.server_id, dhcpServerAddr); printf("DHCP Server's address = %s\n", dhcpServerAddr); }

if (dnsAddress.s_addr != 0) { inet_ntoa_b (dnsAddress, dnsServerAddr); printf("DNS Server's address = %s\n", dnsServerAddr); }

/* Fill in router's information */ if (routerAddress.s_addr != 0) { inet_ntoa_b (routerAddress, gatewayAddr); printf("Router's address = %s\n", gatewayAddr); }

/* Fill in target address */ if (dhcpParameters.yiaddr.s_addr != 0) { inet_ntoa_b (dhcpParameters.yiaddr, targetAddr); printf("%s IP address %s \n", netDev, targetAddr); }

/* Display DHCP information */ printf("\nUse dhcpcParamsShow to show all available parameters: \n\n"); dhcpcParamsShow(pDhcpcRuntimeCookie); dhcpcTimersShow(pDhcpcRuntimeCookie); return (OK);}

/* Function to stop the DHCP lease. Uses dhcpcRelease function. */void dhcpStop( LEASE_DATA *pLeaseData, char *newIpAddress, int newSubnetMask, char*defaultRoute ){ struct if_info ifi; int isAuto; int sockfd; struct ifreq ridreq; char *bcastAddr;

Page 37: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 37 of 46 September 2004

memcpy( &ifi, &pLeaseData->ifData, sizeof( struct if_info ) );

/* Auto flag will cause the interface to go down after the lease is * relinquished, so wait for that to happen.... */

isAuto = pLeaseData->autoConfig | ( pLeaseData->leaseType == DHCP_AUTOMATIC ? 1 : 0 ); dhcpcRelease(pLeaseData);

bzero ( (char *)&ridreq, sizeof (struct ifreq)); sprintf (ridreq.ifr_name, "%s%d", ifi.name, ifi.unit); ridreq.ifr_addr.sa_len = sizeof (struct sockaddr_in);

if ( (sockfd = socket (AF_INET, SOCK_RAW, 0)) < 0) return;

if ( isAuto ) { /* wait for interface to shut down */ do { taskDelay( 1 ); ioctl (sockfd, SIOCGIFFLAGS, (int) (caddr_t) &ridreq); } while( ridreq.ifr_flags & IFF_UP); }

mRouteShow(); routeDelete( "10.0.0.0", newIpAddress ); ifMaskSet( ridreq.ifr_name, newSubnetMask ); ifAddrSet( ridreq.ifr_name, newIpAddress ); mRouteShow(); /* bcastAddr = inet_ntoa( ( inet_addr(newIpAddress) & newSubnetMask ) | ~newSubnetMask ); ifBroadcastSet( ridreq.ifr_name, bcastAddr ); */ ioctl (sockfd, SIOCGIFFLAGS, (int) (caddr_t) &ridreq); if ( ! (ridreq.ifr_flags & IFF_UP ) ) { ridreq.ifr_flags |= IFF_UP; ioctl (sockfd, SIOCSIFFLAGS, (int) (caddr_t) &ridreq); } close (sockfd);

if(!isAuto)

{ flushroutes (); } routeAdd( "0.0.0.0", defaultRoute );}

Page 38: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 38 of 46 September 2004

/* calls the dhcpStop routine */void callStop(char *newIpAddress, int newSubnetMask, char *defaultRoute){ dhcpStop(pLeaseData, newIpAddress, newSubnetMask, defaultRoute); routeAdd( "0.0.0.0", defaultRoute);}

#ifdef DHCP_EVENT_HOOK_PRESENT/* dhcp event hook : */void myEventHook(int event, void *cookie){ switch (event) {

case DHCPC_LEASE_NEW: printf("got lease new!\n"); break;

case DHCPC_LEASE_INVALID: printf("got lease invalid!\n"); break;

default: printf("got unknown!\n"); break;

}}#endif

#ifdef DHCP_CACHE_HOOK_PRESENTSTATUS myCacheHookRtn ( int command, /* requested cache operation */ unsigned long *pTimeStamp, /* lease timestamp data */ int *pDataLen, /* length of data to access */ char *pBuffer /* pointer to data buffer */ ){ switch (command) { case DHCP_CACHE_WRITE: printf("\n DHCP_CACHE_WRITE \n"); printf("\n timestamp = %lu \n", *pTimeStamp); printf("\n data buffer is %s \n", pBuffer); /* your code to write the cache record */ break; case DHCP_CACHE_READ: printf("\n DHCP_CACHE_READ %d \n", command); printf("\n timestamp = %lu \n", *pTimeStamp); printf("\n data buffer is %s \n", pBuffer); /* your code to read record from cache */ break;

Page 39: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 39 of 46 September 2004

case DHCP_CACHE_ERASE: printf("\n DHCP_CACHE_ERASE %d \n", command); printf("\n timestamp = %lu \n", *pTimeStamp); printf("\n data buffer is %s \n", pBuffer); /* your code to erase record from cache */ break; default: printf("\n bad cache hook command %d \n", command); }}#endif

8.2. DHCP Loop Sample Code

/* dhcpLoop.c - A DHCP example to send continuous DHCP messages */

/****************************************************************************** THIS CODE IS NOT SUPPORTED CODE. IT IS PROVIDED ON AN AS-IS BASIS.* ******************************************************************************//*modification history------------------- 01oct04, nk make interface an input parameter01a,06jul01, ds routine to repeatedly send DHCP packets*/

/* DESCRIPTIONThis file contains three routines:1. sysDhcpIpConfig(): a routine that initializes DHCP, then calls getIP()2. getIP(): A routine that sends DHCP discover packets until a server replies.3. dhcpEventHookRoutine(): A callback routine to notify users when a DHCP event occurs.*/#include <vxWorks.h>#include "dhcp/dhcpc.h"#include "dhcpcLib.h"#include "dhcp/dhcpcCommonLib.h"#include "dhcp/dhcpcInit.h"/* Define values for the DHCP*/

#define DHCPC_MAX_LEASES 10#define MAX_SIZE 576#define DHCPC_SPORT 67#define DHCPC_CPORT 68#define DHCPC_OFFER_TIMEOUT 5#define DHCPC_DEFAULT_LEASE 3600#define DHCPC_MIN_LEASE 30

Page 40: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 40 of 46 September 2004

/* Global variable */

struct ifnet * pIf;/* Function prototype for hook */void dhcpEventHookRoutine( int, void * );STATUS sysDhcpIpConfig(char * interface){ char ipAddr[20]; char ifName[10]; struct dhcp_param dhcpcParams; struct ifnet * pIf = NULL;

strcpy(ifName, interface); if(dhcpcLibInit(DHCPC_SPORT, DHCPC_CPORT, DHCPC_MAX_LEASES, MAX_SIZE, DHCPC_OFFER_TIMEOUT, DHCPC_DEFAULT_LEASE, DHCPC_MIN_LEASE) ==ERROR)

{ printf("sysDhcpIpConfig: dhcpcLibInit\n"); return ERROR; }

pIf = ifunit(ifName);

if (pIf == NULL) {

printf("sysDhcpIpConfig: incorrect boot device\n"); return ERROR;

}

pDhcpcBootCookie = dhcpcInit(pIf,TRUE);

if(dhcpcEventHookAdd(pDhcpcBootCookie, (FUNCPTR)dhcpEventHookRoutine) != OK) { printf("Unable to add hook.\n"); return ERROR; }

if(pDhcpcBootCookie == NULL) { printf("sysDhcpIpConfig: unable to setup lease\n"); return ERROR; } if(getIP(pIf) != OK) { printf("Unable to start DHCP process.\n"); return ERROR;

Page 41: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 41 of 46 September 2004

} return(OK);

}

STATUS getIP(struct ifnet *pIf){ int i; char ipAddr[20]; struct dhcp_param dhcpcResult;

bzero ( (char *)&dhcpcResult, sizeof (struct dhcp_param));

i = ERROR; printf("Starting DHCP process...\n"); /* Start DHCP process */ while(i != OK) if((i = dhcpcBind(pDhcpcBootCookie, TRUE))!=OK) printf("sysDhcpIpConfig: Unable to bind to the dhcpserver.\n"); printf("Bind was successful.\n");

if(dhcpcParamsGet(pDhcpcBootCookie, &dhcpcResult)== ERROR) { printf("Unable to get device parameters from DHCP\n"); return ERROR ; }

inet_ntoa_b (dhcpcResult.yiaddr, &ipAddr[0]); printf("ipAddress of the device = %s\n",ipAddr);

/* Show current DHCP parameters */ dhcpcParamsShow(pDhcpcBootCookie); return OK;}

void dhcpEventHookRoutine( int leaseEvent, void *pCookie ){ switch (leaseEvent) { case DHCPC_LEASE_NEW : /* this case gets called when a new lease is established */ printf(" DHCPC_LEASE_NEW \n "); break;

case DHCPC_LEASE_INVALID : /* this case gets called when the client gives up... */ printf(" DHCPC_LEASE_INVALID \n ");

Page 42: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 42 of 46 September 2004

break;

default: printf(" leaseEvent = %d \n ", leaseEvent ); break; }}

8.3. DHCP Lease Storage Hook Sample Code

/* dhcpsSampleHook.c - Sample DHCP server storage routine

/************************************************************************

* THIS CODE IS NOT SUPPORTED CODE. IT IS PROVIDED ON AN AS-IS BASIS.

****************************************************************************** */

/* Copyright 1984 - 1997 Wind River Systems, Inc. */

#include “copyright_wrs.h”

01c,16jan02,ds minor edits

01b,29apr97,spm written. */

/*

DESCRIPTIONThis code demonstrates one possible method of implementing the permanent

storage needed by the DHCP server. It uses an ATA device and a DOS file

system which spans the full device.

*/

#include “vxWorks.h”

#include “dosFsLib.h”

#include “ioLib.h”

#include <drv/hdisk/ataDrv.h>

#include “stdio.h” /* for sprintf() */

#include “stdlib.h” /* for atoi() */

extern usrFdiskPartRead;

extern ataDiskInit(void *);

/* DHCP include files */

#include “dhcp/dhcp.h”

#include “dhcp/common.h”

#include “dhcpsLib.h”

BOOL myFsInit = FALSE;

BOOL myFileInit = FALSE;

BOOL endOfData = FALSE;

Page 43: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 43 of 46 September 2004

BLK_DEV *pBlkDev0;void *blk_device, *pCbio, *cbio1;

/**scsi related code

SCSI_PHYS_DEV * pPhysDev;

BLK_DEV * pBlkDev;

DOS_VOL_DESC * pVolDesc;

DOS_VOL_CONFIG configStruct; */

int myWriteDesc;

int myReadDesc;

STATUS sampleLeaseStorageHook

(int op, /* lease operation */

char *recData, /* record of active lease */

int dataLen /* length of record data */

)

{

int result;

int offset; int length; char delim;

char buffer [10];

switch (op)

{

case DHCPS_STORAGE_START:

/* Prepare to store and retrieve records of active leases. */

printf (“START message\n”);

if (myFsInit == FALSE)

{

/* Set up the BLK_DEV structure to access the ATA device. */

printf(“Calling cbioLibInit()\n”);

cbioLibInit();

printf(“Calling dosFsLibInit()\n”);

dosFsLibInit(20);

printf(“Calling dosFsFatInit()\n”);

dosFsFatInit();

printf(“Calling dosFsFmtLibInit()\n”);

dosFsFmtLibInit();

printf(“Calling dosVDirLibInit()\n”);

dosVDirLibInit();

printf(“Calling dosChkLibInit()\n”);

dosChkLibInit();

Page 44: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 44 of 46 September 2004

printf(“Calling ataDevCreate()\n”);

blk_device = ataDevCreate(0,0,0,0);

printf(“Calling dcacheDevCreate()\n”);

pCbio = dcacheDevCreate(blk_device, 0, 0, “/ata0/”);

printf(“Calling dpartDevCreate()\n”);

cbio1 = dpartDevCreate( pCbio, 1, usrFdiskPartRead );

printf(“Calling dosFsDevCreate(a)\n”);

dosFsDevCreate(“/sd0a”, dpartPartGet(cbio1,0), 0, 0 );

pBlkDev0 = ataDevCreate(0,0,0,0);

ataDiskInit(pBlkDev0);

dosFsConfigShow();

/* Access an existing DOS file system. */

/*

pVolDesc = dosFsDevInit(“/sd0a”, pBlkDev, &configStruct);

if (pVolDesc == NULL)

return (ERROR);

myFsInit = TRUE;

}

if (myFileInit == FALSE)

{

myWriteDesc = open (“/sd0a/leases.dat”, O_WRONLY, 0);

if (myWriteDesc == ERROR) /* Try to create file. */

{

myWriteDesc = creat (“/sd0a/leases.dat”,O_WRONLY);

endOfData = TRUE;

}

if (myWriteDesc == ERROR)

return (ERROR);

myReadDesc = open (“/sd0a/leases.dat”, O_RDONLY, 0);

if (myReadDesc == ERROR)

return (ERROR);

myFileInit = TRUE;

}

break;

case DHCPS_STORAGE_WRITE:printf (“WRITE message\n”);

/* Store active leases. Return ERROR if START operation failed. */

if (myFsInit == FALSE || myFileInit == FALSE)

return (ERROR);

Page 45: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 45 of 46 September 2004

/* Store the length for later retrieval by READ operation. */

sprintf (buffer, “%d:”, dataLen);

result = write (myWriteDesc, buffer, strlen (buffer));

if (result != strlen (buffer))

return (ERROR);

/* Store the record of the lease. */

result = write (myWriteDesc, recData, dataLen);

if (result != dataLen)

return (ERROR);

/** Force write to file - each operation must be independent,

* so can’t rely on close() call in the DHCPS_STORAGE_STOP code. */

result = ioctl (myWriteDesc, FIOFLUSH, 0);

if (result == ERROR)

return (ERROR);

endOfData = FALSE; /* Data available. */

result = read(myReadDesc, recData, length);

if(result == 0) /* Unexpected end-of-file */

{

endOfData = TRUE

return (ERROR);

}

if (result == ERROR) r

return (ERROR);

if (result != length)

return (ERROR);

break;

case DHCPS_STORAGE_CLEAR:printf (“CLEAR message\n”);

myFileInit = FALSE;

endOfData = TRUE;

/* Remove and recreate underlying file. */

close (myWriteDesc);

close (myReadDesc);

result = remove (“/sd0a/leases.dat”);

if (result == ERROR)

return (ERROR);

Page 46: DHCP

Issues and Solutions for DHCP AppNote-212

Version1.2 Page 46 of 46 September 2004

myWriteDesc = creat (“/sd0a/leases.dat”, O_WRONLY);

if (myWriteDesc == ERROR)

return (ERROR);

myReadDesc = open (“/sd0a/leases.dat”, O_RDONLY, 0);

if (myReadDesc == ERROR)

return (ERROR);

myFileInit = TRUE;

break;

case DHCPS_STORAGE_STOP:

printf (“STOP message\n”);

myFileInit = FALSE;

endOfData = FALSE;

close (myWriteDesc);

close (myReadDesc);

break;

default:printf (“bogus message\n”);

break;

}

return (OK);

}