1 /*****************************************************************************
3 * Monitoring check_dhcp plugin
6 * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2001-2007 Monitoring Plugins Development Team
11 * This file contains the check_dhcp plugin
13 * This plugin tests the availability of DHCP servers on a network.
15 * Unicast mode was originally implemented by Heiti of Boras Kommun with
16 * general improvements as well as usability fixes and "forward"-porting by
17 * Andreas Ericsson of OP5 AB.
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 *****************************************************************************/
36 const char *progname
= "check_dhcp";
37 const char *copyright
= "2001-2007";
38 const char *email
= "devel@monitoring-plugins.org";
51 #include <sys/ioctl.h>
54 #include <sys/socket.h>
55 #include <sys/types.h>
57 #include <netinet/in.h>
59 #include <arpa/inet.h>
61 #include <sys/sockio.h>
64 #if defined( __linux__ )
66 #include <linux/if_ether.h>
69 #elif defined (__bsd__)
71 #include <netinet/if_ether.h>
72 #include <sys/param.h>
73 #include <sys/sysctl.h>
74 #include <net/if_dl.h>
76 #elif defined(__sun__) || defined(__solaris__) || defined(__hpux__)
83 #include <sys/stropts.h>
87 #define bcopy(source, destination, length) memcpy(destination, source, length)
89 #define AREA_SZ 5000 /* buffer length in bytes */
90 static u_long ctl_area
[AREA_SZ
];
91 static u_long dat_area
[AREA_SZ
];
92 static struct strbuf ctl
= {AREA_SZ
, 0, (char *)ctl_area
};
93 static struct strbuf dat
= {AREA_SZ
, 0, (char *)dat_area
};
101 #define u_int8_t uint8_t
102 #define u_int16_t uint16_t
103 #define u_int32_t uint32_t
105 static int get_msg(int);
106 static int check_ctrl(int);
107 static int put_ctrl(int, int, int);
108 static int put_both(int, int, int, int);
109 static int dl_open(const char *, int, int *);
110 static int dl_bind(int, int, u_char
*);
111 long mac_addr_dlpi( const char *, int, u_char
*);
117 /**** Common definitions ****/
126 /**** DHCP definitions ****/
128 #define MAX_DHCP_CHADDR_LENGTH 16
129 #define MAX_DHCP_SNAME_LENGTH 64
130 #define MAX_DHCP_FILE_LENGTH 128
131 #define MAX_DHCP_OPTIONS_LENGTH 312
134 typedef struct dhcp_packet_struct
{
135 u_int8_t op
; /* packet type */
136 u_int8_t htype
; /* type of hardware address for this machine (Ethernet, etc) */
137 u_int8_t hlen
; /* length of hardware address (of this machine) */
138 u_int8_t hops
; /* hops */
139 u_int32_t xid
; /* random transaction id number - chosen by this machine */
140 u_int16_t secs
; /* seconds used in timing */
141 u_int16_t flags
; /* flags */
142 struct in_addr ciaddr
; /* IP address of this machine (if we already have one) */
143 struct in_addr yiaddr
; /* IP address of this machine (offered by the DHCP server) */
144 struct in_addr siaddr
; /* IP address of next server */
145 struct in_addr giaddr
; /* IP address of DHCP relay */
146 unsigned char chaddr
[MAX_DHCP_CHADDR_LENGTH
]; /* hardware address of this machine */
147 char sname
[MAX_DHCP_SNAME_LENGTH
]; /* name of DHCP server */
148 char file
[MAX_DHCP_FILE_LENGTH
]; /* boot file name (used for diskless booting?) */
149 char options
[MAX_DHCP_OPTIONS_LENGTH
]; /* options */
153 typedef struct dhcp_offer_struct
{
154 struct in_addr server_address
; /* address of DHCP server that sent this offer */
155 struct in_addr offered_address
; /* the IP address that was offered to us */
156 u_int32_t lease_time
; /* lease time in seconds */
157 u_int32_t renewal_time
; /* renewal time in seconds */
158 u_int32_t rebinding_time
; /* rebinding time in seconds */
159 struct dhcp_offer_struct
*next
;
163 typedef struct requested_server_struct
{
164 struct in_addr server_address
;
166 struct requested_server_struct
*next
;
170 #define BOOTREQUEST 1
173 #define DHCPDISCOVER 1
175 #define DHCPREQUEST 3
176 #define DHCPDECLINE 4
179 #define DHCPRELEASE 7
181 #define DHCP_OPTION_MESSAGE_TYPE 53
182 #define DHCP_OPTION_HOST_NAME 12
183 #define DHCP_OPTION_BROADCAST_ADDRESS 28
184 #define DHCP_OPTION_REQUESTED_ADDRESS 50
185 #define DHCP_OPTION_LEASE_TIME 51
186 #define DHCP_OPTION_SERVER_IDENTIFIER 54
187 #define DHCP_OPTION_RENEWAL_TIME 58
188 #define DHCP_OPTION_REBINDING_TIME 59
189 #define DHCP_OPTION_END 255
191 #define DHCP_INFINITE_TIME 0xFFFFFFFF
193 #define DHCP_BROADCAST_FLAG 32768
195 #define DHCP_SERVER_PORT 67
196 #define DHCP_CLIENT_PORT 68
198 #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
199 #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */
201 u_int8_t unicast
= 0; /* unicast mode: mimic a DHCP relay */
202 struct in_addr my_ip
; /* our address (required for relay) */
203 struct in_addr dhcp_ip
; /* server to query (if in unicast mode) */
204 unsigned char client_hardware_address
[MAX_DHCP_CHADDR_LENGTH
]="";
205 unsigned char *user_specified_mac
=NULL
;
207 char network_interface_name
[IFNAMSIZ
]="eth0";
209 u_int32_t packet_xid
=0;
211 u_int32_t dhcp_lease_time
=0;
212 u_int32_t dhcp_renewal_time
=0;
213 u_int32_t dhcp_rebinding_time
=0;
215 int dhcpoffer_timeout
=2;
217 dhcp_offer
*dhcp_offer_list
=NULL
;
218 requested_server
*requested_server_list
=NULL
;
220 int valid_responses
=0; /* number of valid DHCPOFFERs we received */
221 int requested_servers
=0;
222 int requested_responses
=0;
224 int request_specific_address
=FALSE
;
225 int received_requested_address
=FALSE
;
227 struct in_addr requested_address
;
230 int process_arguments(int, char **);
231 int call_getopt(int, char **);
232 int validate_arguments(int, int);
233 void print_usage(void);
234 void print_help(void);
236 void resolve_host(const char *in
,struct in_addr
*out
);
237 unsigned char *mac_aton(const char *);
238 void print_hardware_address(const unsigned char *);
239 int get_hardware_address(int,char *);
240 int get_ip_address(int,char *);
242 int send_dhcp_discover(int);
243 int get_dhcp_offer(int);
245 int get_results(void);
247 int add_dhcp_offer(struct in_addr
,dhcp_packet
*);
248 int free_dhcp_offer_list(void);
249 int free_requested_server_list(void);
251 int create_dhcp_socket(void);
252 int close_dhcp_socket(int);
253 int send_dhcp_packet(void *,int,int,struct sockaddr_in
*);
254 int receive_dhcp_packet(void *,int,int,int,struct sockaddr_in
*);
258 int main(int argc
, char **argv
){
260 int result
= STATE_UNKNOWN
;
262 setlocale (LC_ALL
, "");
263 bindtextdomain (PACKAGE
, LOCALEDIR
);
264 textdomain (PACKAGE
);
266 /* Parse extra opts if any */
267 argv
=np_extra_opts(&argc
, argv
, progname
);
269 if(process_arguments(argc
,argv
)!=OK
){
270 usage4 (_("Could not parse arguments"));
273 /* create socket for DHCP communications */
274 dhcp_socket
=create_dhcp_socket();
276 /* get hardware address of client machine */
277 if(user_specified_mac
!=NULL
)
278 memcpy(client_hardware_address
,user_specified_mac
,6);
280 get_hardware_address(dhcp_socket
,network_interface_name
);
282 if(unicast
) /* get IP address of client machine */
283 get_ip_address(dhcp_socket
,network_interface_name
);
285 /* send DHCPDISCOVER packet */
286 send_dhcp_discover(dhcp_socket
);
288 /* wait for a DHCPOFFER packet */
289 get_dhcp_offer(dhcp_socket
);
291 /* close socket we created */
292 close_dhcp_socket(dhcp_socket
);
294 /* determine state/plugin output to return */
295 result
=get_results();
297 /* free allocated memory */
298 free_dhcp_offer_list();
299 free_requested_server_list();
306 /* determines hardware address on client machine */
307 int get_hardware_address(int sock
,char *interface_name
){
309 #if defined(__linux__)
312 strncpy((char *)&ifr
.ifr_name
,interface_name
,sizeof(ifr
.ifr_name
)-1);
313 ifr
.ifr_name
[sizeof(ifr
.ifr_name
)-1]='\0';
315 /* try and grab hardware address of requested interface */
316 if(ioctl(sock
,SIOCGIFHWADDR
,&ifr
)<0){
317 printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name
);
321 memcpy(&client_hardware_address
[0],&ifr
.ifr_hwaddr
.sa_data
,6);
323 #elif defined(__bsd__)
324 /* King 2004 see ACKNOWLEDGEMENTS */
329 struct if_msghdr
*ifm
;
330 struct sockaddr_dl
*sdl
;
336 mib
[4] = NET_RT_IFLIST
;
338 if((mib
[5] = if_nametoindex(interface_name
)) == 0){
339 printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno
));
343 if(sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0){
344 printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name
, strerror(errno
));
348 if((buf
= malloc(len
)) == NULL
){
349 printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name
, strerror(errno
));
353 if(sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0){
354 printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name
, strerror(errno
));
358 ifm
= (struct if_msghdr
*)buf
;
359 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
360 ptr
= (unsigned char *)LLADDR(sdl
);
361 memcpy(&client_hardware_address
[0], ptr
, 6) ;
364 #elif defined(__sun__) || defined(__solaris__)
366 /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
368 char dev
[20] = "/dev/";
372 /* get last number from interfacename, eg lnc0, e1000g0*/
374 p
= interface_name
+ strlen(interface_name
) -1;
375 for(i
= strlen(interface_name
) -1; i
> 0; p
--) {
380 if( p
!= interface_name
){
382 strncat(dev
, interface_name
, 6) ;
385 printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name
);
388 stat
= mac_addr_dlpi(dev
, unit
, client_hardware_address
);
390 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev
, unit
);
394 #elif defined(__hpux__)
397 char dev
[20] = "/dev/dlpi" ;
400 stat
= mac_addr_dlpi(dev
, unit
, client_hardware_address
);
402 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev
, unit
);
405 /* Kompf 2000-2003 */
408 printf(_("Error: can't get MAC address for this architecture. Use the --mac option.\n"));
413 print_hardware_address(client_hardware_address
);
418 /* determines IP address of the client interface */
419 int get_ip_address(int sock
,char *interface_name
){
420 #if defined(SIOCGIFADDR)
423 strncpy((char *)&ifr
.ifr_name
,interface_name
,sizeof(ifr
.ifr_name
)-1);
424 ifr
.ifr_name
[sizeof(ifr
.ifr_name
)-1]='\0';
426 if(ioctl(sock
,SIOCGIFADDR
,&ifr
)<0){
427 printf(_("Error: Cannot determine IP address of interface %s\n"),
432 my_ip
=((struct sockaddr_in
*)&ifr
.ifr_addr
)->sin_addr
;
435 printf(_("Error: Cannot get interface IP address on this platform.\n"));
440 printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip
));
445 /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
446 int send_dhcp_discover(int sock
){
447 dhcp_packet discover_packet
;
448 struct sockaddr_in sockaddr_broadcast
;
452 /* clear the packet data structure */
453 bzero(&discover_packet
,sizeof(discover_packet
));
456 /* boot request flag (backward compatible with BOOTP servers) */
457 discover_packet
.op
=BOOTREQUEST
;
459 /* hardware address type */
460 discover_packet
.htype
=ETHERNET_HARDWARE_ADDRESS
;
462 /* length of our hardware address */
463 discover_packet
.hlen
=ETHERNET_HARDWARE_ADDRESS_LENGTH
;
466 * transaction ID is supposed to be random.
468 srand(time(NULL
)^getpid());
470 discover_packet
.xid
=htonl(packet_xid
);
472 /**** WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
473 /* downright bizzarre... */
474 ntohl(discover_packet
.xid
);
476 /*discover_packet.secs=htons(65535);*/
477 discover_packet
.secs
=0xFF;
480 * server needs to know if it should broadcast or unicast its response:
481 * 0x8000L == 32768 == 1 << 15 == broadcast, 0 == unicast
483 discover_packet
.flags
= unicast
? 0 : htons(DHCP_BROADCAST_FLAG
);
485 /* our hardware address */
486 memcpy(discover_packet
.chaddr
,client_hardware_address
,ETHERNET_HARDWARE_ADDRESS_LENGTH
);
488 /* first four bytes of options field is magic cookie (as per RFC 2132) */
489 discover_packet
.options
[0]='\x63';
490 discover_packet
.options
[1]='\x82';
491 discover_packet
.options
[2]='\x53';
492 discover_packet
.options
[3]='\x63';
495 /* DHCP message type is embedded in options field */
496 discover_packet
.options
[opts
++]=DHCP_OPTION_MESSAGE_TYPE
; /* DHCP message type option identifier */
497 discover_packet
.options
[opts
++]='\x01'; /* DHCP message option length in bytes */
498 discover_packet
.options
[opts
++]=DHCPDISCOVER
;
500 /* the IP address we're requesting */
501 if(request_specific_address
==TRUE
){
502 discover_packet
.options
[opts
++]=DHCP_OPTION_REQUESTED_ADDRESS
;
503 discover_packet
.options
[opts
++]='\x04';
504 memcpy(&discover_packet
.options
[opts
],&requested_address
,sizeof(requested_address
));
505 opts
+= sizeof(requested_address
);
507 discover_packet
.options
[opts
++]=DHCP_OPTION_END
;
511 discover_packet
.giaddr
.s_addr
= my_ip
.s_addr
;
513 /* see RFC 1542, 4.1.1 */
514 discover_packet
.hops
= unicast
? 1 : 0;
516 /* send the DHCPDISCOVER packet to broadcast address */
517 sockaddr_broadcast
.sin_family
=AF_INET
;
518 sockaddr_broadcast
.sin_port
=htons(DHCP_SERVER_PORT
);
519 sockaddr_broadcast
.sin_addr
.s_addr
= unicast
? dhcp_ip
.s_addr
: INADDR_BROADCAST
;
520 bzero(&sockaddr_broadcast
.sin_zero
,sizeof(sockaddr_broadcast
.sin_zero
));
524 printf(_("DHCPDISCOVER to %s port %d\n"),inet_ntoa(sockaddr_broadcast
.sin_addr
),ntohs(sockaddr_broadcast
.sin_port
));
525 printf("DHCPDISCOVER XID: %u (0x%X)\n",ntohl(discover_packet
.xid
),ntohl(discover_packet
.xid
));
526 printf("DHCDISCOVER ciaddr: %s\n",inet_ntoa(discover_packet
.ciaddr
));
527 printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet
.yiaddr
));
528 printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet
.siaddr
));
529 printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet
.giaddr
));
532 /* send the DHCPDISCOVER packet out */
533 send_dhcp_packet(&discover_packet
,sizeof(discover_packet
),sock
,&sockaddr_broadcast
);
544 /* waits for a DHCPOFFER message from one or more DHCP servers */
545 int get_dhcp_offer(int sock
){
546 dhcp_packet offer_packet
;
547 struct sockaddr_in source
;
548 struct sockaddr_in via
;
557 /* receive as many responses as we can */
558 for(responses
=0,valid_responses
=0;;){
561 if((current_time
-start_time
)>=dhcpoffer_timeout
)
567 bzero(&source
,sizeof(source
));
568 bzero(&via
,sizeof(via
));
569 bzero(&offer_packet
,sizeof(offer_packet
));
572 result
=receive_dhcp_packet(&offer_packet
,sizeof(offer_packet
),sock
,dhcpoffer_timeout
,&source
);
576 printf(_("Result=ERROR\n"));
582 printf(_("Result=OK\n"));
587 /* The "source" is either a server or a relay. */
588 /* Save a copy of "source" into "via" even if it's via itself */
589 memcpy(&via
,&source
,sizeof(source
)) ;
592 printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source
.sin_addr
));
593 printf(_(" via %s\n"),inet_ntoa(via
.sin_addr
));
594 printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet
.xid
),ntohl(offer_packet
.xid
));
597 /* check packet xid to see if its the same as the one we used in the discover packet */
598 if(ntohl(offer_packet
.xid
)!=packet_xid
){
600 printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet
.xid
),packet_xid
);
605 /* check hardware address */
608 printf("DHCPOFFER chaddr: ");
610 for(x
=0;x
<ETHERNET_HARDWARE_ADDRESS_LENGTH
;x
++){
612 printf("%02X",(unsigned char)offer_packet
.chaddr
[x
]);
614 if(offer_packet
.chaddr
[x
]!=client_hardware_address
[x
])
622 printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n"));
628 printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet
.ciaddr
));
629 printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet
.yiaddr
));
630 printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet
.siaddr
));
631 printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet
.giaddr
));
634 add_dhcp_offer(source
.sin_addr
,&offer_packet
);
640 printf(_("Total responses seen on the wire: %d\n"),responses
);
641 printf(_("Valid responses for this machine: %d\n"),valid_responses
);
649 /* sends a DHCP packet */
650 int send_dhcp_packet(void *buffer
, int buffer_size
, int sock
, struct sockaddr_in
*dest
){
653 result
=sendto(sock
,(char *)buffer
,buffer_size
,0,(struct sockaddr
*)dest
,sizeof(*dest
));
656 printf(_("send_dhcp_packet result: %d\n"),result
);
666 /* receives a DHCP packet */
667 int receive_dhcp_packet(void *buffer
, int buffer_size
, int sock
, int timeout
, struct sockaddr_in
*address
){
672 socklen_t address_size
;
673 struct sockaddr_in source_address
;
677 /* wait for data to arrive (up time timeout) */
682 FD_SET(sock
,&readfds
);
683 FD_SET(sock
,&oobfds
);
684 nfound
= select(sock
+1,&readfds
,NULL
,&oobfds
,&tv
);
686 /* make sure some data has arrived */
687 if(!FD_ISSET(sock
,&readfds
)){
689 printf(_("No (more) data received (nfound: %d)\n"), nfound
);
694 bzero(&source_address
,sizeof(source_address
));
695 address_size
=sizeof(source_address
);
696 recv_result
=recvfrom(sock
,(char *)buffer
,buffer_size
,MSG_PEEK
,(struct sockaddr
*)&source_address
,&address_size
);
698 printf("recv_result: %d\n",recv_result
);
702 printf(_("recvfrom() failed, "));
703 printf("errno: (%d) -> %s\n",errno
,strerror(errno
));
709 printf(_("receive_dhcp_packet() result: %d\n"),recv_result
);
710 printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address
.sin_addr
));
713 memcpy(address
,&source_address
,sizeof(source_address
));
722 /* creates a socket for DHCP communication */
723 int create_dhcp_socket(void){
724 struct sockaddr_in myname
;
725 struct ifreq interface
;
729 /* Set up the address we're going to bind to. */
730 bzero(&myname
,sizeof(myname
));
731 myname
.sin_family
=AF_INET
;
732 /* listen to DHCP server port if we're in unicast mode */
733 myname
.sin_port
= htons(unicast
? DHCP_SERVER_PORT
: DHCP_CLIENT_PORT
);
734 myname
.sin_addr
.s_addr
= unicast
? my_ip
.s_addr
: INADDR_ANY
;
735 bzero(&myname
.sin_zero
,sizeof(myname
.sin_zero
));
737 /* create a socket for DHCP communications */
738 sock
=socket(AF_INET
,SOCK_DGRAM
,IPPROTO_UDP
);
740 printf(_("Error: Could not create socket!\n"));
745 printf("DHCP socket: %d\n",sock
);
747 /* set the reuse address flag so we don't get errors when restarting */
749 if(setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&flag
,sizeof(flag
))<0){
750 printf(_("Error: Could not set reuse address option on DHCP socket!\n"));
754 /* set the broadcast option - we need this to listen to DHCP broadcast messages */
755 if(!unicast
&& setsockopt(sock
,SOL_SOCKET
,SO_BROADCAST
,(char *)&flag
,sizeof flag
)<0){
756 printf(_("Error: Could not set broadcast option on DHCP socket!\n"));
760 /* bind socket to interface */
761 #if defined(__linux__)
762 strncpy(interface
.ifr_ifrn
.ifrn_name
,network_interface_name
,IFNAMSIZ
-1);
763 interface
.ifr_ifrn
.ifrn_name
[IFNAMSIZ
-1]='\0';
764 if(setsockopt(sock
,SOL_SOCKET
,SO_BINDTODEVICE
,(char *)&interface
,sizeof(interface
))<0){
765 printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"),network_interface_name
);
770 strncpy(interface
.ifr_name
,network_interface_name
,IFNAMSIZ
-1);
771 interface
.ifr_name
[IFNAMSIZ
-1]='\0';
774 /* bind the socket */
775 if(bind(sock
,(struct sockaddr
*)&myname
,sizeof(myname
))<0){
776 printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"),DHCP_CLIENT_PORT
);
784 /* closes DHCP socket */
785 int close_dhcp_socket(int sock
){
793 /* adds a requested server address to list in memory */
794 int add_requested_server(struct in_addr server_address
){
795 requested_server
*new_server
;
797 new_server
=(requested_server
*)malloc(sizeof(requested_server
));
801 new_server
->server_address
=server_address
;
802 new_server
->answered
=FALSE
;
804 new_server
->next
=requested_server_list
;
805 requested_server_list
=new_server
;
810 printf(_("Requested server address: %s\n"),inet_ntoa(new_server
->server_address
));
818 /* adds a DHCP OFFER to list in memory */
819 int add_dhcp_offer(struct in_addr source
,dhcp_packet
*offer_packet
){
820 dhcp_offer
*new_offer
;
822 unsigned option_type
;
823 unsigned option_length
;
824 struct in_addr serv_ident
= {0};
826 if(offer_packet
==NULL
)
829 /* process all DHCP options present in the packet */
830 for(x
=4;x
<MAX_DHCP_OPTIONS_LENGTH
-1;){
832 if((int)offer_packet
->options
[x
]==-1)
835 /* get option type */
836 option_type
=offer_packet
->options
[x
++];
838 /* get option length */
839 option_length
=offer_packet
->options
[x
++];
842 printf("Option: %d (0x%02X)\n",option_type
,option_length
);
844 /* get option data */
846 case DHCP_OPTION_LEASE_TIME
:
847 memcpy(&dhcp_lease_time
, &offer_packet
->options
[x
],sizeof(dhcp_lease_time
));
848 dhcp_lease_time
= ntohl(dhcp_lease_time
);
850 case DHCP_OPTION_RENEWAL_TIME
:
851 memcpy(&dhcp_renewal_time
, &offer_packet
->options
[x
],sizeof(dhcp_renewal_time
));
852 dhcp_renewal_time
= ntohl(dhcp_renewal_time
);
854 case DHCP_OPTION_REBINDING_TIME
:
855 memcpy(&dhcp_rebinding_time
, &offer_packet
->options
[x
],sizeof(dhcp_rebinding_time
));
856 dhcp_rebinding_time
= ntohl(dhcp_rebinding_time
);
858 case DHCP_OPTION_SERVER_IDENTIFIER
:
859 memcpy(&serv_ident
.s_addr
, &offer_packet
->options
[x
],sizeof(serv_ident
.s_addr
));
863 /* skip option data we're ignoring */
864 if(option_type
==0) /* "pad" option, see RFC 2132 (3.1) */
871 if(dhcp_lease_time
==DHCP_INFINITE_TIME
)
872 printf(_("Lease Time: Infinite\n"));
874 printf(_("Lease Time: %lu seconds\n"),(unsigned long)dhcp_lease_time
);
875 if(dhcp_renewal_time
==DHCP_INFINITE_TIME
)
876 printf(_("Renewal Time: Infinite\n"));
878 printf(_("Renewal Time: %lu seconds\n"),(unsigned long)dhcp_renewal_time
);
879 if(dhcp_rebinding_time
==DHCP_INFINITE_TIME
)
880 printf(_("Rebinding Time: Infinite\n"));
881 printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time
);
884 new_offer
=(dhcp_offer
*)malloc(sizeof(dhcp_offer
));
890 * RFC 2131 (2.) says: "DHCP clarifies the interpretation of the
891 * 'siaddr' field as the address of the server to use in the next step
892 * of the client's bootstrap process. A DHCP server may return its own
893 * address in the 'siaddr' field, if the server is prepared to supply
894 * the next bootstrap service (e.g., delivery of an operating system
895 * executable image). A DHCP server always returns its own address in
896 * the 'server identifier' option." 'serv_ident' is the 'server
897 * identifier' option, 'source' is the IP address we received the
898 * DHCPOFFER from. If 'serv_ident' isn't available for some reason, we
901 new_offer
->server_address
=serv_ident
.s_addr
?serv_ident
:source
;
902 new_offer
->offered_address
=offer_packet
->yiaddr
;
903 new_offer
->lease_time
=dhcp_lease_time
;
904 new_offer
->renewal_time
=dhcp_renewal_time
;
905 new_offer
->rebinding_time
=dhcp_rebinding_time
;
909 printf(_("Added offer from server @ %s"),inet_ntoa(new_offer
->server_address
));
910 printf(_(" of IP address %s\n"),inet_ntoa(new_offer
->offered_address
));
913 /* add new offer to head of list */
914 new_offer
->next
=dhcp_offer_list
;
915 dhcp_offer_list
=new_offer
;
921 /* frees memory allocated to DHCP OFFER list */
922 int free_dhcp_offer_list(void){
923 dhcp_offer
*this_offer
;
924 dhcp_offer
*next_offer
;
926 for(this_offer
=dhcp_offer_list
;this_offer
!=NULL
;this_offer
=next_offer
){
927 next_offer
=this_offer
->next
;
935 /* frees memory allocated to requested server list */
936 int free_requested_server_list(void){
937 requested_server
*this_server
;
938 requested_server
*next_server
;
940 for(this_server
=requested_server_list
;this_server
!=NULL
;this_server
=next_server
){
941 next_server
=this_server
->next
;
949 /* gets state and plugin output to return */
950 int get_results(void){
951 dhcp_offer
*temp_offer
;
952 requested_server
*temp_server
;
954 u_int32_t max_lease_time
=0;
956 received_requested_address
=FALSE
;
958 /* checks responses from requested servers */
959 requested_responses
=0;
960 if(requested_servers
>0){
962 for(temp_server
=requested_server_list
;temp_server
!=NULL
;temp_server
=temp_server
->next
){
964 for(temp_offer
=dhcp_offer_list
;temp_offer
!=NULL
;temp_offer
=temp_offer
->next
){
966 /* get max lease time we were offered */
967 if(temp_offer
->lease_time
>max_lease_time
|| temp_offer
->lease_time
==DHCP_INFINITE_TIME
)
968 max_lease_time
=temp_offer
->lease_time
;
970 /* see if we got the address we requested */
971 if(!memcmp(&requested_address
,&temp_offer
->offered_address
,sizeof(requested_address
)))
972 received_requested_address
=TRUE
;
974 /* see if the servers we wanted a response from talked to us or not */
975 if(!memcmp(&temp_offer
->server_address
,&temp_server
->server_address
,sizeof(temp_server
->server_address
))){
977 printf(_("DHCP Server Match: Offerer=%s"),inet_ntoa(temp_offer
->server_address
));
978 printf(_(" Requested=%s"),inet_ntoa(temp_server
->server_address
));
979 if(temp_server
->answered
)
980 printf(_(" (duplicate)"));
983 if(temp_server
->answered
== FALSE
){
984 requested_responses
++;
985 temp_server
->answered
=TRUE
;
993 /* else check and see if we got our requested address from any server */
996 for(temp_offer
=dhcp_offer_list
;temp_offer
!=NULL
;temp_offer
=temp_offer
->next
){
998 /* get max lease time we were offered */
999 if(temp_offer
->lease_time
>max_lease_time
|| temp_offer
->lease_time
==DHCP_INFINITE_TIME
)
1000 max_lease_time
=temp_offer
->lease_time
;
1002 /* see if we got the address we requested */
1003 if(!memcmp(&requested_address
,&temp_offer
->offered_address
,sizeof(requested_address
)))
1004 received_requested_address
=TRUE
;
1009 if(valid_responses
==0)
1010 result
=STATE_CRITICAL
;
1011 else if(requested_servers
>0 && requested_responses
==0)
1012 result
=STATE_CRITICAL
;
1013 else if(requested_responses
<requested_servers
)
1014 result
=STATE_WARNING
;
1015 else if(request_specific_address
==TRUE
&& received_requested_address
==FALSE
)
1016 result
=STATE_WARNING
;
1018 if(result
==0) /* garrett honeycutt 2005 */
1021 printf("WARNING: ");
1023 printf("CRITICAL: ");
1025 printf("UNKNOWN: ");
1027 /* we didn't receive any DHCPOFFERs */
1028 if(dhcp_offer_list
==NULL
){
1029 printf(_("No DHCPOFFERs were received.\n"));
1033 printf(_("Received %d DHCPOFFER(s)"),valid_responses
);
1035 if(requested_servers
>0)
1036 printf(_(", %s%d of %d requested servers responded"),((requested_responses
<requested_servers
) && requested_responses
>0)?"only ":"",requested_responses
,requested_servers
);
1038 if(request_specific_address
==TRUE
)
1039 printf(_(", requested address (%s) was %soffered"),inet_ntoa(requested_address
),(received_requested_address
==TRUE
)?"":_("not "));
1041 printf(_(", max lease time = "));
1042 if(max_lease_time
==DHCP_INFINITE_TIME
)
1043 printf(_("Infinity"));
1045 printf("%lu sec",(unsigned long)max_lease_time
);
1053 /* process command-line arguments */
1054 int process_arguments(int argc
, char **argv
){
1060 arg_index
= call_getopt(argc
,argv
);
1061 return validate_arguments(argc
,arg_index
);
1066 int call_getopt(int argc
, char **argv
){
1068 int option_index
= 0;
1069 static struct option long_options
[] =
1071 {"serverip", required_argument
,0,'s'},
1072 {"requestedip", required_argument
,0,'r'},
1073 {"timeout", required_argument
,0,'t'},
1074 {"interface", required_argument
,0,'i'},
1075 {"mac", required_argument
,0,'m'},
1076 {"unicast", no_argument
, 0,'u'},
1077 {"verbose", no_argument
, 0,'v'},
1078 {"version", no_argument
, 0,'V'},
1079 {"help", no_argument
, 0,'h'},
1086 c
=getopt_long(argc
,argv
,"+hVvt:s:r:t:i:m:u",long_options
,&option_index
);
1088 if(c
==-1||c
==EOF
||c
==1)
1093 case 's': /* DHCP server address */
1094 resolve_host(optarg
,&dhcp_ip
);
1095 add_requested_server(dhcp_ip
);
1098 case 'r': /* address we are requested from DHCP servers */
1099 resolve_host(optarg
,&requested_address
);
1100 request_specific_address
=TRUE
;
1103 case 't': /* timeout */
1106 if(is_intnonneg(optarg))
1109 dhcpoffer_timeout
=atoi(optarg
);
1112 usage("Time interval must be a nonnegative integer\n");
1116 case 'm': /* MAC address */
1118 if((user_specified_mac
=mac_aton(optarg
)) == NULL
)
1119 usage("Cannot parse MAC address.\n");
1121 print_hardware_address(user_specified_mac
);
1125 case 'i': /* interface name */
1127 strncpy(network_interface_name
,optarg
,sizeof(network_interface_name
)-1);
1128 network_interface_name
[sizeof(network_interface_name
)-1]='\x0';
1132 case 'u': /* unicast testing */
1136 case 'V': /* version */
1137 print_revision(progname
, NP_VERSION
);
1138 exit(STATE_UNKNOWN
);
1140 case 'h': /* help */
1142 exit(STATE_UNKNOWN
);
1144 case 'v': /* verbose */
1148 case '?': /* help */
1160 int validate_arguments(int argc
, int arg_index
){
1163 usage(_("Got unexpected non-option argument"));
1169 #if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
1170 /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
1172 /* get a message from a stream; return type of message */
1173 static int get_msg(int fd
){
1179 res
= getmsg(fd
, &ctl
, &dat
, &flags
);
1186 printf("%s\n", "get_msg FAILED.");
1200 /* verify that dl_primitive in ctl_area = prim */
1201 static int check_ctrl(int prim
){
1202 dl_error_ack_t
*err_ack
= (dl_error_ack_t
*)ctl_area
;
1204 if(err_ack
->dl_primitive
!= prim
){
1205 printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno
));
1206 exit(STATE_UNKNOWN
);
1212 /* put a control message on a stream */
1213 static int put_ctrl(int fd
, int len
, int pri
){
1216 if(putmsg(fd
, &ctl
, 0, pri
) < 0){
1217 printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno
));
1218 exit(STATE_UNKNOWN
);
1224 /* put a control + data message on a stream */
1225 static int put_both(int fd
, int clen
, int dlen
, int pri
){
1229 if(putmsg(fd
, &ctl
, &dat
, pri
) < 0){
1230 printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno
));
1231 exit(STATE_UNKNOWN
);
1237 /* open file descriptor and attach */
1238 static int dl_open(const char *dev
, int unit
, int *fd
){
1239 dl_attach_req_t
*attach_req
= (dl_attach_req_t
*)ctl_area
;
1241 if((*fd
= open(dev
, O_RDWR
)) == -1){
1242 printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev
, strerror(errno
));
1243 exit(STATE_UNKNOWN
);
1245 attach_req
->dl_primitive
= DL_ATTACH_REQ
;
1246 attach_req
->dl_ppa
= unit
;
1247 put_ctrl(*fd
, sizeof(dl_attach_req_t
), 0);
1249 return check_ctrl(DL_OK_ACK
);
1252 /* send DL_BIND_REQ */
1253 static int dl_bind(int fd
, int sap
, u_char
*addr
){
1254 dl_bind_req_t
*bind_req
= (dl_bind_req_t
*)ctl_area
;
1255 dl_bind_ack_t
*bind_ack
= (dl_bind_ack_t
*)ctl_area
;
1257 bind_req
->dl_primitive
= DL_BIND_REQ
;
1258 bind_req
->dl_sap
= sap
;
1259 bind_req
->dl_max_conind
= 1;
1260 bind_req
->dl_service_mode
= DL_CLDLS
;
1261 bind_req
->dl_conn_mgmt
= 0;
1262 bind_req
->dl_xidtest_flg
= 0;
1263 put_ctrl(fd
, sizeof(dl_bind_req_t
), 0);
1265 if (GOT_ERR
== check_ctrl(DL_BIND_ACK
)){
1266 printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno
));
1267 exit(STATE_UNKNOWN
);
1269 bcopy((u_char
*)bind_ack
+ bind_ack
->dl_addr_offset
, addr
,
1270 bind_ack
->dl_addr_length
);
1275 /***********************************************************************
1277 * function mac_addr_dlpi - get the mac address of the interface with
1278 * type dev (eg lnc, hme) and unit (0, 1 ..)
1280 * parameter: addr: an array of six bytes, has to be allocated by the caller
1282 * return: 0 if OK, -1 if the address could not be determined
1285 ***********************************************************************/
1287 long mac_addr_dlpi( const char *dev
, int unit
, u_char
*addr
){
1289 u_char mac_addr
[25];
1291 if(GOT_ERR
!= dl_open(dev
, unit
, &fd
)){
1292 if(GOT_ERR
!= dl_bind(fd
, INSAP
, mac_addr
)){
1293 bcopy( mac_addr
, addr
, 6);
1302 /* Kompf 2000-2003 */
1306 /* resolve host name or die (TODO: move this to netutils.c!) */
1307 void resolve_host(const char *in
,struct in_addr
*out
){
1308 struct addrinfo hints
, *ai
;
1310 memset(&hints
,0,sizeof(hints
));
1311 hints
.ai_family
=PF_INET
;
1312 if (getaddrinfo(in
,NULL
,&hints
,&ai
) != 0)
1313 usage_va(_("Invalid hostname/address - %s"),optarg
);
1315 memcpy(out
,&((struct sockaddr_in
*)ai
->ai_addr
)->sin_addr
,sizeof(*out
));
1320 /* parse MAC address string, return 6 bytes (unterminated) or NULL */
1321 unsigned char *mac_aton(const char *string
){
1322 static unsigned char result
[6];
1326 for(i
=0, j
=0; string
[i
] != '\0' && j
< sizeof(result
); i
++){
1327 /* ignore ':' and any other non-hex character */
1328 if(!isxdigit(string
[i
]) || !isxdigit(string
[i
+1]))
1333 result
[j
]=strtol(tmp
,(char **)NULL
,16);
1338 return (j
==6) ? result
: NULL
;
1342 void print_hardware_address(const unsigned char *address
){
1345 printf(_("Hardware address: "));
1347 printf("%2.2x:", address
[i
]);
1348 printf("%2.2x", address
[i
]);
1353 /* print usage help */
1354 void print_help(void){
1356 print_revision(progname
, NP_VERSION
);
1358 printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n");
1359 printf (COPYRIGHT
, copyright
, email
);
1361 printf("%s\n", _("This plugin tests the availability of DHCP servers on a network."));
1367 printf (UT_HELP_VRSN
);
1368 printf (UT_EXTRA_OPTS
);
1370 printf (UT_VERBOSE
);
1372 printf (" %s\n", "-s, --serverip=IPADDRESS");
1373 printf (" %s\n", _("IP address of DHCP server that we must hear from"));
1374 printf (" %s\n", "-r, --requestedip=IPADDRESS");
1375 printf (" %s\n", _("IP address that should be offered by at least one DHCP server"));
1376 printf (" %s\n", "-t, --timeout=INTEGER");
1377 printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
1378 printf (" %s\n", "-i, --interface=STRING");
1379 printf (" %s\n", _("Interface to to use for listening (i.e. eth0)"));
1380 printf (" %s\n", "-m, --mac=STRING");
1381 printf (" %s\n", _("MAC address to use in the DHCP request"));
1382 printf (" %s\n", "-u, --unicast");
1383 printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
1385 printf (UT_SUPPORT
);
1393 printf ("%s\n", _("Usage:"));
1394 printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname
);
1395 printf (" [-i interface] [-m mac]\n");