1 /******************************************************************************
3 * Nagios check_dhcp plugin
6 * Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2001-2006 Nagios Plugin Development Team
9 * Last Modified: $Date$
13 * This file contains the check_dhcp plugin
15 * This plugin tests the availability of DHCP servers on a network.
18 * License Information:
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 2 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, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 * ------------------------------------------------------------------------
37 * Unicast mode was originally implemented by Heiti of Boras Kommun with
38 * general improvements as well as usability fixes and "forward"-porting by
39 * Andreas Ericsson of OP5 AB.
40 * ------------------------------------------------------------------------
42 *****************************************************************************/
44 const char *progname
= "check_dhcp";
45 const char *revision
= "$Revision$";
46 const char *copyright
= "2001-2006";
47 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
60 #include <sys/ioctl.h>
63 #include <sys/socket.h>
64 #include <sys/types.h>
66 #include <netinet/in.h>
68 #include <arpa/inet.h>
70 #include <sys/sockio.h>
73 #if defined( __linux__ )
75 #include <linux/if_ether.h>
78 #elif defined (__bsd__)
80 #include <netinet/if_ether.h>
81 #include <sys/param.h>
82 #include <sys/sysctl.h>
83 #include <net/if_dl.h>
85 #elif defined(__sun__) || defined(__solaris__) || defined(__hpux__)
92 #include <sys/stropts.h>
96 #define bcopy(source, destination, length) memcpy(destination, source, length)
98 #define AREA_SZ 5000 /* buffer length in bytes */
99 static u_long ctl_area
[AREA_SZ
];
100 static u_long dat_area
[AREA_SZ
];
101 static struct strbuf ctl
= {AREA_SZ
, 0, (char *)ctl_area
};
102 static struct strbuf dat
= {AREA_SZ
, 0, (char *)dat_area
};
110 #define u_int8_t uint8_t
111 #define u_int16_t uint16_t
112 #define u_int32_t uint32_t
114 static int get_msg(int);
115 static int check_ctrl(int);
116 static int put_ctrl(int, int, int);
117 static int put_both(int, int, int, int);
118 static int dl_open(const char *, int, int *);
119 static int dl_bind(int, int, u_char
*);
120 long mac_addr_dlpi( const char *, int, u_char
*);
126 /**** Common definitions ****/
135 /**** DHCP definitions ****/
137 #define MAX_DHCP_CHADDR_LENGTH 16
138 #define MAX_DHCP_SNAME_LENGTH 64
139 #define MAX_DHCP_FILE_LENGTH 128
140 #define MAX_DHCP_OPTIONS_LENGTH 312
143 typedef struct dhcp_packet_struct
{
144 u_int8_t op
; /* packet type */
145 u_int8_t htype
; /* type of hardware address for this machine (Ethernet, etc) */
146 u_int8_t hlen
; /* length of hardware address (of this machine) */
147 u_int8_t hops
; /* hops */
148 u_int32_t xid
; /* random transaction id number - chosen by this machine */
149 u_int16_t secs
; /* seconds used in timing */
150 u_int16_t flags
; /* flags */
151 struct in_addr ciaddr
; /* IP address of this machine (if we already have one) */
152 struct in_addr yiaddr
; /* IP address of this machine (offered by the DHCP server) */
153 struct in_addr siaddr
; /* IP address of DHCP server */
154 struct in_addr giaddr
; /* IP address of DHCP relay */
155 unsigned char chaddr
[MAX_DHCP_CHADDR_LENGTH
]; /* hardware address of this machine */
156 char sname
[MAX_DHCP_SNAME_LENGTH
]; /* name of DHCP server */
157 char file
[MAX_DHCP_FILE_LENGTH
]; /* boot file name (used for diskless booting?) */
158 char options
[MAX_DHCP_OPTIONS_LENGTH
]; /* options */
162 typedef struct dhcp_offer_struct
{
163 struct in_addr server_address
; /* address of DHCP server that sent this offer */
164 struct in_addr offered_address
; /* the IP address that was offered to us */
165 u_int32_t lease_time
; /* lease time in seconds */
166 u_int32_t renewal_time
; /* renewal time in seconds */
167 u_int32_t rebinding_time
; /* rebinding time in seconds */
168 struct dhcp_offer_struct
*next
;
172 typedef struct requested_server_struct
{
173 struct in_addr server_address
;
175 struct requested_server_struct
*next
;
179 #define BOOTREQUEST 1
182 #define DHCPDISCOVER 1
184 #define DHCPREQUEST 3
185 #define DHCPDECLINE 4
188 #define DHCPRELEASE 7
190 #define DHCP_OPTION_MESSAGE_TYPE 53
191 #define DHCP_OPTION_HOST_NAME 12
192 #define DHCP_OPTION_BROADCAST_ADDRESS 28
193 #define DHCP_OPTION_REQUESTED_ADDRESS 50
194 #define DHCP_OPTION_LEASE_TIME 51
195 #define DHCP_OPTION_SERVER_IDENTIFIER 54
196 #define DHCP_OPTION_RENEWAL_TIME 58
197 #define DHCP_OPTION_REBINDING_TIME 59
198 #define DHCP_OPTION_END 255
200 #define DHCP_INFINITE_TIME 0xFFFFFFFF
202 #define DHCP_BROADCAST_FLAG 32768
203 #define DHCP_UNICAST_FLAG 0
205 #define DHCP_SERVER_PORT 67
206 #define DHCP_CLIENT_PORT 68
208 #define ETHERNET_HARDWARE_ADDRESS 1 /* used in htype field of dhcp packet */
209 #define ETHERNET_HARDWARE_ADDRESS_LENGTH 6 /* length of Ethernet hardware addresses */
211 u_int8_t unicast
= 0; /* unicast mode: mimic a DHCP relay */
212 struct in_addr my_ip
; /* our address (required for relay) */
213 struct in_addr dhcp_ip
; /* server to query (if in unicast mode) */
214 unsigned char client_hardware_address
[MAX_DHCP_CHADDR_LENGTH
]="";
215 unsigned char *user_specified_mac
=NULL
;
217 char network_interface_name
[IFNAMSIZ
]="eth0";
219 u_int32_t packet_xid
=0;
221 u_int32_t dhcp_lease_time
=0;
222 u_int32_t dhcp_renewal_time
=0;
223 u_int32_t dhcp_rebinding_time
=0;
225 int dhcpoffer_timeout
=2;
227 dhcp_offer
*dhcp_offer_list
=NULL
;
228 requested_server
*requested_server_list
=NULL
;
230 int valid_responses
=0; /* number of valid DHCPOFFERs we received */
231 int requested_servers
=0;
232 int requested_responses
=0;
234 int request_specific_address
=FALSE
;
235 int received_requested_address
=FALSE
;
237 struct in_addr requested_address
;
240 int process_arguments(int, char **);
241 int call_getopt(int, char **);
242 int validate_arguments(void);
243 void print_usage(void);
244 void print_help(void);
246 void resolve_host(const char *in
,struct in_addr
*out
);
247 unsigned char *mac_aton(const char *);
248 void print_hardware_address(const unsigned char *);
249 int get_hardware_address(int,char *);
250 int get_ip_address(int,char *);
252 int send_dhcp_discover(int);
253 int get_dhcp_offer(int);
255 int get_results(void);
257 int add_dhcp_offer(struct in_addr
,dhcp_packet
*);
258 int free_dhcp_offer_list(void);
259 int free_requested_server_list(void);
261 int create_dhcp_socket(void);
262 int close_dhcp_socket(int);
263 int send_dhcp_packet(void *,int,int,struct sockaddr_in
*);
264 int receive_dhcp_packet(void *,int,int,int,struct sockaddr_in
*);
268 int main(int argc
, char **argv
){
270 int result
= STATE_UNKNOWN
;
272 setlocale (LC_ALL
, "");
273 bindtextdomain (PACKAGE
, LOCALEDIR
);
274 textdomain (PACKAGE
);
276 if(process_arguments(argc
,argv
)!=OK
){
277 usage4 (_("Could not parse arguments"));
280 /* this plugin almost certainly needs root permissions. */
281 np_warn_if_not_root();
283 /* create socket for DHCP communications */
284 dhcp_socket
=create_dhcp_socket();
286 /* get hardware address of client machine */
287 if(user_specified_mac
!=NULL
)
288 memcpy(client_hardware_address
,user_specified_mac
,6);
290 get_hardware_address(dhcp_socket
,network_interface_name
);
292 if(unicast
) /* get IP address of client machine */
293 get_ip_address(dhcp_socket
,network_interface_name
);
295 /* send DHCPDISCOVER packet */
296 send_dhcp_discover(dhcp_socket
);
298 /* wait for a DHCPOFFER packet */
299 get_dhcp_offer(dhcp_socket
);
301 /* close socket we created */
302 close_dhcp_socket(dhcp_socket
);
304 /* determine state/plugin output to return */
305 result
=get_results();
307 /* free allocated memory */
308 free_dhcp_offer_list();
309 free_requested_server_list();
316 /* determines hardware address on client machine */
317 int get_hardware_address(int sock
,char *interface_name
){
319 #if defined(__linux__)
322 strncpy((char *)&ifr
.ifr_name
,interface_name
,sizeof(ifr
.ifr_name
)-1);
323 ifr
.ifr_name
[sizeof(ifr
.ifr_name
)-1]='\0';
325 /* try and grab hardware address of requested interface */
326 if(ioctl(sock
,SIOCGIFHWADDR
,&ifr
)<0){
327 printf(_("Error: Could not get hardware address of interface '%s'\n"),interface_name
);
331 memcpy(&client_hardware_address
[0],&ifr
.ifr_hwaddr
.sa_data
,6);
333 #elif defined(__bsd__)
334 /* King 2004 see ACKNOWLEDGEMENTS */
339 struct if_msghdr
*ifm
;
340 struct sockaddr_dl
*sdl
;
346 mib
[4] = NET_RT_IFLIST
;
348 if((mib
[5] = if_nametoindex(interface_name
)) == 0){
349 printf(_("Error: if_nametoindex error - %s.\n"), strerror(errno
));
353 if(sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0){
354 printf(_("Error: Couldn't get hardware address from %s. sysctl 1 error - %s.\n"), interface_name
, strerror(errno
));
358 if((buf
= malloc(len
)) == NULL
){
359 printf(_("Error: Couldn't get hardware address from interface %s. malloc error - %s.\n"), interface_name
, strerror(errno
));
363 if(sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0){
364 printf(_("Error: Couldn't get hardware address from %s. sysctl 2 error - %s.\n"), interface_name
, strerror(errno
));
368 ifm
= (struct if_msghdr
*)buf
;
369 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
370 ptr
= (unsigned char *)LLADDR(sdl
);
371 memcpy(&client_hardware_address
[0], ptr
, 6) ;
374 #elif defined(__sun__) || defined(__solaris__)
376 /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
378 char dev
[20] = "/dev/";
382 for(p
= interface_name
; *p
&& isalpha(*p
); p
++)
387 strncat(dev
, interface_name
, 6) ;
390 printf(_("Error: can't find unit number in interface_name (%s) - expecting TypeNumber eg lnc0.\n"), interface_name
);
393 stat
= mac_addr_dlpi(dev
, unit
, client_hardware_address
);
395 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev
, unit
);
399 #elif defined(__hpux__)
402 char dev
[20] = "/dev/dlpi" ;
405 stat
= mac_addr_dlpi(dev
, unit
, client_hardware_address
);
407 printf(_("Error: can't read MAC address from DLPI streams interface for device %s unit %d.\n"), dev
, unit
);
410 /* Kompf 2000-2003 */
413 printf(_("Error: can't get MAC address for this architecture. Use the --mac option.\n"));
418 print_hardware_address(client_hardware_address
);
423 /* determines IP address of the client interface */
424 int get_ip_address(int sock
,char *interface_name
){
425 #if defined(SIOCGIFADDR)
428 strncpy((char *)&ifr
.ifr_name
,interface_name
,sizeof(ifr
.ifr_name
)-1);
429 ifr
.ifr_name
[sizeof(ifr
.ifr_name
)-1]='\0';
431 if(ioctl(sock
,SIOCGIFADDR
,&ifr
)<0){
432 printf(_("Error: Cannot determine IP address of interface %s\n"),
437 my_ip
=((struct sockaddr_in
*)&ifr
.ifr_addr
)->sin_addr
;
440 printf(_("Error: Cannot get interface IP address on this platform.\n"));
445 printf(_("Pretending to be relay client %s\n"),inet_ntoa(my_ip
));
450 /* sends a DHCPDISCOVER broadcast message in an attempt to find DHCP servers */
451 int send_dhcp_discover(int sock
){
452 dhcp_packet discover_packet
;
453 struct sockaddr_in sockaddr_broadcast
;
457 /* clear the packet data structure */
458 bzero(&discover_packet
,sizeof(discover_packet
));
461 /* boot request flag (backward compatible with BOOTP servers) */
462 discover_packet
.op
=BOOTREQUEST
;
464 /* hardware address type */
465 discover_packet
.htype
=ETHERNET_HARDWARE_ADDRESS
;
467 /* length of our hardware address */
468 discover_packet
.hlen
=ETHERNET_HARDWARE_ADDRESS_LENGTH
;
471 * transaction ID is supposed to be random. We won't use the address so
472 * we don't care about high entropy here. time(2) is good enough.
476 discover_packet
.xid
=htonl(packet_xid
);
478 /**** WHAT THE HECK IS UP WITH THIS?!? IF I DON'T MAKE THIS CALL, ONLY ONE SERVER RESPONSE IS PROCESSED!!!! ****/
479 /* downright bizzarre... */
480 ntohl(discover_packet
.xid
);
482 /*discover_packet.secs=htons(65535);*/
483 discover_packet
.secs
=0xFF;
486 * server needs to know if it should broadcast or unicast its response:
487 * 0x8000L == 32768 == 1 << 15 == broadcast, 0 == unicast
489 discover_packet
.flags
= unicast
? 0 : htons(DHCP_BROADCAST_FLAG
);
491 /* our hardware address */
492 memcpy(discover_packet
.chaddr
,client_hardware_address
,ETHERNET_HARDWARE_ADDRESS_LENGTH
);
494 /* first four bytes of options field is magic cookie (as per RFC 2132) */
495 discover_packet
.options
[0]='\x63';
496 discover_packet
.options
[1]='\x82';
497 discover_packet
.options
[2]='\x53';
498 discover_packet
.options
[3]='\x63';
501 /* DHCP message type is embedded in options field */
502 discover_packet
.options
[opts
++]=DHCP_OPTION_MESSAGE_TYPE
; /* DHCP message type option identifier */
503 discover_packet
.options
[opts
++]='\x01'; /* DHCP message option length in bytes */
504 discover_packet
.options
[opts
++]=DHCPDISCOVER
;
506 /* the IP address we're requesting */
507 if(request_specific_address
==TRUE
){
508 discover_packet
.options
[opts
++]=DHCP_OPTION_REQUESTED_ADDRESS
;
509 discover_packet
.options
[opts
++]='\x04';
510 memcpy(&discover_packet
.options
[opts
],&requested_address
,sizeof(requested_address
));
511 opts
+= sizeof(requested_address
);
513 discover_packet
.options
[opts
++]=DHCP_OPTION_END
;
517 discover_packet
.giaddr
.s_addr
= my_ip
.s_addr
;
519 /* see RFC 1542, 4.1.1 */
520 discover_packet
.hops
= unicast
? 1 : 0;
522 /* send the DHCPDISCOVER packet to broadcast address */
523 sockaddr_broadcast
.sin_family
=AF_INET
;
524 sockaddr_broadcast
.sin_port
=htons(DHCP_SERVER_PORT
);
525 sockaddr_broadcast
.sin_addr
.s_addr
= unicast
? dhcp_ip
.s_addr
: INADDR_BROADCAST
;
526 bzero(&sockaddr_broadcast
.sin_zero
,sizeof(sockaddr_broadcast
.sin_zero
));
530 printf(_("DHCPDISCOVER to %s port %d\n"),inet_ntoa(sockaddr_broadcast
.sin_addr
),ntohs(sockaddr_broadcast
.sin_port
));
531 printf("DHCPDISCOVER XID: %u (0x%X)\n",ntohl(discover_packet
.xid
),ntohl(discover_packet
.xid
));
532 printf("DHCDISCOVER ciaddr: %s\n",inet_ntoa(discover_packet
.ciaddr
));
533 printf("DHCDISCOVER yiaddr: %s\n",inet_ntoa(discover_packet
.yiaddr
));
534 printf("DHCDISCOVER siaddr: %s\n",inet_ntoa(discover_packet
.siaddr
));
535 printf("DHCDISCOVER giaddr: %s\n",inet_ntoa(discover_packet
.giaddr
));
538 /* send the DHCPDISCOVER packet out */
539 send_dhcp_packet(&discover_packet
,sizeof(discover_packet
),sock
,&sockaddr_broadcast
);
550 /* waits for a DHCPOFFER message from one or more DHCP servers */
551 int get_dhcp_offer(int sock
){
552 dhcp_packet offer_packet
;
553 struct sockaddr_in source
;
554 struct sockaddr_in via
;
563 /* receive as many responses as we can */
564 for(responses
=0,valid_responses
=0;;){
567 if((current_time
-start_time
)>=dhcpoffer_timeout
)
573 bzero(&source
,sizeof(source
));
574 bzero(&via
,sizeof(via
));
575 bzero(&offer_packet
,sizeof(offer_packet
));
578 result
=receive_dhcp_packet(&offer_packet
,sizeof(offer_packet
),sock
,dhcpoffer_timeout
,&source
);
582 printf(_("Result=ERROR\n"));
588 printf(_("Result=OK\n"));
593 /* The "source" is either a server or a relay. */
594 /* Save a copy of "source" into "via" even if it's via itself */
595 memcpy(&via
,&source
,sizeof(source
)) ;
597 /* If siaddr is non-zero, set "source" to siaddr */
598 if(offer_packet
.siaddr
.s_addr
!= 0L){
599 source
.sin_addr
.s_addr
= offer_packet
.siaddr
.s_addr
;
603 printf(_("DHCPOFFER from IP address %s"),inet_ntoa(source
.sin_addr
));
604 printf(_(" via %s\n"),inet_ntoa(via
.sin_addr
));
605 printf("DHCPOFFER XID: %u (0x%X)\n",ntohl(offer_packet
.xid
),ntohl(offer_packet
.xid
));
608 /* check packet xid to see if its the same as the one we used in the discover packet */
609 if(ntohl(offer_packet
.xid
)!=packet_xid
){
611 printf(_("DHCPOFFER XID (%u) did not match DHCPDISCOVER XID (%u) - ignoring packet\n"),ntohl(offer_packet
.xid
),packet_xid
);
616 /* check hardware address */
619 printf("DHCPOFFER chaddr: ");
621 for(x
=0;x
<ETHERNET_HARDWARE_ADDRESS_LENGTH
;x
++){
623 printf("%02X",(unsigned char)offer_packet
.chaddr
[x
]);
625 if(offer_packet
.chaddr
[x
]!=client_hardware_address
[x
])
633 printf(_("DHCPOFFER hardware address did not match our own - ignoring packet\n"));
639 printf("DHCPOFFER ciaddr: %s\n",inet_ntoa(offer_packet
.ciaddr
));
640 printf("DHCPOFFER yiaddr: %s\n",inet_ntoa(offer_packet
.yiaddr
));
641 printf("DHCPOFFER siaddr: %s\n",inet_ntoa(offer_packet
.siaddr
));
642 printf("DHCPOFFER giaddr: %s\n",inet_ntoa(offer_packet
.giaddr
));
645 add_dhcp_offer(source
.sin_addr
,&offer_packet
);
651 printf(_("Total responses seen on the wire: %d\n"),responses
);
652 printf(_("Valid responses for this machine: %d\n"),valid_responses
);
660 /* sends a DHCP packet */
661 int send_dhcp_packet(void *buffer
, int buffer_size
, int sock
, struct sockaddr_in
*dest
){
664 result
=sendto(sock
,(char *)buffer
,buffer_size
,0,(struct sockaddr
*)dest
,sizeof(*dest
));
667 printf(_("send_dhcp_packet result: %d\n"),result
);
677 /* receives a DHCP packet */
678 int receive_dhcp_packet(void *buffer
, int buffer_size
, int sock
, int timeout
, struct sockaddr_in
*address
){
683 socklen_t address_size
;
684 struct sockaddr_in source_address
;
688 /* wait for data to arrive (up time timeout) */
693 FD_SET(sock
,&readfds
);
694 FD_SET(sock
,&oobfds
);
695 nfound
= select(sock
+1,&readfds
,NULL
,&oobfds
,&tv
);
697 /* make sure some data has arrived */
698 if(!FD_ISSET(sock
,&readfds
)){
700 printf(_("No (more) data received (nfound: %d)\n"), nfound
);
706 /* why do we need to peek first? i don't know, its a hack. without it, the source address of the first packet received was
707 not being interpreted correctly. sigh... */
708 bzero(&source_address
,sizeof(source_address
));
709 address_size
=sizeof(source_address
);
710 recv_result
=recvfrom(sock
,(char *)buffer
,buffer_size
,MSG_PEEK
,(struct sockaddr
*)&source_address
,&address_size
);
712 printf("recv_result_1: %d\n",recv_result
);
713 recv_result
=recvfrom(sock
,(char *)buffer
,buffer_size
,0,(struct sockaddr
*)&source_address
,&address_size
);
715 printf("recv_result_2: %d\n",recv_result
);
719 printf(_("recvfrom() failed, "));
720 printf("errno: (%d) -> %s\n",errno
,strerror(errno
));
726 printf(_("receive_dhcp_packet() result: %d\n"),recv_result
);
727 printf(_("receive_dhcp_packet() source: %s\n"),inet_ntoa(source_address
.sin_addr
));
730 memcpy(address
,&source_address
,sizeof(source_address
));
739 /* creates a socket for DHCP communication */
740 int create_dhcp_socket(void){
741 struct sockaddr_in myname
;
742 struct ifreq interface
;
746 /* Set up the address we're going to bind to. */
747 bzero(&myname
,sizeof(myname
));
748 myname
.sin_family
=AF_INET
;
749 /* listen to DHCP server port if we're in unicast mode */
750 myname
.sin_port
= htons(unicast
? DHCP_SERVER_PORT
: DHCP_CLIENT_PORT
);
751 myname
.sin_addr
.s_addr
= unicast
? my_ip
.s_addr
: INADDR_ANY
;
752 bzero(&myname
.sin_zero
,sizeof(myname
.sin_zero
));
754 /* create a socket for DHCP communications */
755 sock
=socket(AF_INET
,SOCK_DGRAM
,IPPROTO_UDP
);
757 printf(_("Error: Could not create socket!\n"));
762 printf("DHCP socket: %d\n",sock
);
764 /* set the reuse address flag so we don't get errors when restarting */
766 if(setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&flag
,sizeof(flag
))<0){
767 printf(_("Error: Could not set reuse address option on DHCP socket!\n"));
771 /* set the broadcast option - we need this to listen to DHCP broadcast messages */
772 if(setsockopt(sock
,SOL_SOCKET
,SO_BROADCAST
,(char *)&flag
,sizeof flag
)<0){
773 printf(_("Error: Could not set broadcast option on DHCP socket!\n"));
777 /* bind socket to interface */
778 #if defined(__linux__)
779 strncpy(interface
.ifr_ifrn
.ifrn_name
,network_interface_name
,IFNAMSIZ
-1);
780 interface
.ifr_ifrn
.ifrn_name
[IFNAMSIZ
-1]='\0';
781 if(setsockopt(sock
,SOL_SOCKET
,SO_BINDTODEVICE
,(char *)&interface
,sizeof(interface
))<0){
782 printf(_("Error: Could not bind socket to interface %s. Check your privileges...\n"),network_interface_name
);
787 strncpy(interface
.ifr_name
,network_interface_name
,IFNAMSIZ
-1);
788 interface
.ifr_name
[IFNAMSIZ
-1]='\0';
791 /* bind the socket */
792 if(bind(sock
,(struct sockaddr
*)&myname
,sizeof(myname
))<0){
793 printf(_("Error: Could not bind to DHCP socket (port %d)! Check your privileges...\n"),DHCP_CLIENT_PORT
);
801 /* closes DHCP socket */
802 int close_dhcp_socket(int sock
){
810 /* adds a requested server address to list in memory */
811 int add_requested_server(struct in_addr server_address
){
812 requested_server
*new_server
;
814 new_server
=(requested_server
*)malloc(sizeof(requested_server
));
818 new_server
->server_address
=server_address
;
819 new_server
->answered
=FALSE
;
821 new_server
->next
=requested_server_list
;
822 requested_server_list
=new_server
;
827 printf(_("Requested server address: %s\n"),inet_ntoa(new_server
->server_address
));
835 /* adds a DHCP OFFER to list in memory */
836 int add_dhcp_offer(struct in_addr source
,dhcp_packet
*offer_packet
){
837 dhcp_offer
*new_offer
;
839 unsigned option_type
;
840 unsigned option_length
;
841 struct in_addr serv_ident
= {0};
843 if(offer_packet
==NULL
)
846 /* process all DHCP options present in the packet */
847 for(x
=4;x
<MAX_DHCP_OPTIONS_LENGTH
;){
849 /* end of options (0 is really just a pad, but bail out anyway) */
850 if((int)offer_packet
->options
[x
]==-1 || (int)offer_packet
->options
[x
]==0)
853 /* get option type */
854 option_type
=offer_packet
->options
[x
++];
856 /* get option length */
857 option_length
=offer_packet
->options
[x
++];
860 printf("Option: %d (0x%02X)\n",option_type
,option_length
);
862 /* get option data */
864 case DHCP_OPTION_LEASE_TIME
:
865 memcpy(&dhcp_lease_time
, &offer_packet
->options
[x
],sizeof(dhcp_lease_time
));
866 dhcp_lease_time
= ntohl(dhcp_lease_time
);
868 case DHCP_OPTION_RENEWAL_TIME
:
869 memcpy(&dhcp_renewal_time
, &offer_packet
->options
[x
],sizeof(dhcp_renewal_time
));
870 dhcp_renewal_time
= ntohl(dhcp_renewal_time
);
872 case DHCP_OPTION_REBINDING_TIME
:
873 memcpy(&dhcp_rebinding_time
, &offer_packet
->options
[x
],sizeof(dhcp_rebinding_time
));
874 dhcp_rebinding_time
= ntohl(dhcp_rebinding_time
);
876 case DHCP_OPTION_SERVER_IDENTIFIER
:
877 memcpy(&serv_ident
.s_addr
, &offer_packet
->options
[x
],sizeof(serv_ident
.s_addr
));
881 /* skip option data we're ignoring */
882 if(option_type
!=DHCP_OPTION_REBINDING_TIME
)
887 if(dhcp_lease_time
==DHCP_INFINITE_TIME
)
888 printf(_("Lease Time: Infinite\n"));
890 printf(_("Lease Time: %lu seconds\n"),(unsigned long)dhcp_lease_time
);
891 if(dhcp_renewal_time
==DHCP_INFINITE_TIME
)
892 printf(_("Renewal Time: Infinite\n"));
894 printf(_("Renewal Time: %lu seconds\n"),(unsigned long)dhcp_renewal_time
);
895 if(dhcp_rebinding_time
==DHCP_INFINITE_TIME
)
896 printf(_("Rebinding Time: Infinite\n"));
897 printf(_("Rebinding Time: %lu seconds\n"),(unsigned long)dhcp_rebinding_time
);
900 new_offer
=(dhcp_offer
*)malloc(sizeof(dhcp_offer
));
906 * RFC 2131 (2.) says: "DHCP clarifies the interpretation of the
907 * 'siaddr' field as the address of the server to use in the next step
908 * of the client's bootstrap process. A DHCP server may return its own
909 * address in the 'siaddr' field, if the server is prepared to supply
910 * the next bootstrap service (e.g., delivery of an operating system
911 * executable image). A DHCP server always returns its own address in
912 * the 'server identifier' option." 'serv_ident' is the 'server
913 * identifier' option, 'source' is the 'siaddr' field or (if 'siaddr'
914 * wasn't available) the IP address we received the DHCPOFFER from. If
915 * 'serv_ident' isn't available for some reason, we use 'source'.
917 new_offer
->server_address
=serv_ident
.s_addr
?serv_ident
:source
;
918 new_offer
->offered_address
=offer_packet
->yiaddr
;
919 new_offer
->lease_time
=dhcp_lease_time
;
920 new_offer
->renewal_time
=dhcp_renewal_time
;
921 new_offer
->rebinding_time
=dhcp_rebinding_time
;
925 printf(_("Added offer from server @ %s"),inet_ntoa(new_offer
->server_address
));
926 printf(_(" of IP address %s\n"),inet_ntoa(new_offer
->offered_address
));
929 /* add new offer to head of list */
930 new_offer
->next
=dhcp_offer_list
;
931 dhcp_offer_list
=new_offer
;
937 /* frees memory allocated to DHCP OFFER list */
938 int free_dhcp_offer_list(void){
939 dhcp_offer
*this_offer
;
940 dhcp_offer
*next_offer
;
942 for(this_offer
=dhcp_offer_list
;this_offer
!=NULL
;this_offer
=next_offer
){
943 next_offer
=this_offer
->next
;
951 /* frees memory allocated to requested server list */
952 int free_requested_server_list(void){
953 requested_server
*this_server
;
954 requested_server
*next_server
;
956 for(this_server
=requested_server_list
;this_server
!=NULL
;this_server
=next_server
){
957 next_server
=this_server
->next
;
965 /* gets state and plugin output to return */
966 int get_results(void){
967 dhcp_offer
*temp_offer
;
968 requested_server
*temp_server
;
970 u_int32_t max_lease_time
=0;
972 received_requested_address
=FALSE
;
974 /* checks responses from requested servers */
975 requested_responses
=0;
976 if(requested_servers
>0){
978 for(temp_server
=requested_server_list
;temp_server
!=NULL
;temp_server
=temp_server
->next
){
980 for(temp_offer
=dhcp_offer_list
;temp_offer
!=NULL
;temp_offer
=temp_offer
->next
){
982 /* get max lease time we were offered */
983 if(temp_offer
->lease_time
>max_lease_time
|| temp_offer
->lease_time
==DHCP_INFINITE_TIME
)
984 max_lease_time
=temp_offer
->lease_time
;
986 /* see if we got the address we requested */
987 if(!memcmp(&requested_address
,&temp_offer
->offered_address
,sizeof(requested_address
)))
988 received_requested_address
=TRUE
;
990 /* see if the servers we wanted a response from talked to us or not */
991 if(!memcmp(&temp_offer
->server_address
,&temp_server
->server_address
,sizeof(temp_server
->server_address
))){
993 printf(_("DHCP Server Match: Offerer=%s"),inet_ntoa(temp_offer
->server_address
));
994 printf(_(" Requested=%s"),inet_ntoa(temp_server
->server_address
));
995 if(temp_server
->answered
)
996 printf(_(" (duplicate)"));
999 if(temp_server
->answered
== FALSE
){
1000 requested_responses
++;
1001 temp_server
->answered
=TRUE
;
1009 /* else check and see if we got our requested address from any server */
1012 for(temp_offer
=dhcp_offer_list
;temp_offer
!=NULL
;temp_offer
=temp_offer
->next
){
1014 /* get max lease time we were offered */
1015 if(temp_offer
->lease_time
>max_lease_time
|| temp_offer
->lease_time
==DHCP_INFINITE_TIME
)
1016 max_lease_time
=temp_offer
->lease_time
;
1018 /* see if we got the address we requested */
1019 if(!memcmp(&requested_address
,&temp_offer
->offered_address
,sizeof(requested_address
)))
1020 received_requested_address
=TRUE
;
1025 if(valid_responses
==0)
1026 result
=STATE_CRITICAL
;
1027 else if(requested_servers
>0 && requested_responses
==0)
1028 result
=STATE_CRITICAL
;
1029 else if(requested_responses
<requested_servers
)
1030 result
=STATE_WARNING
;
1031 else if(request_specific_address
==TRUE
&& received_requested_address
==FALSE
)
1032 result
=STATE_WARNING
;
1034 if(result
==0) /* garrett honeycutt 2005 */
1037 printf("WARNING: ");
1039 printf("CRITICAL: ");
1041 printf("UNKNOWN: ");
1043 /* we didn't receive any DHCPOFFERs */
1044 if(dhcp_offer_list
==NULL
){
1045 printf(_("No DHCPOFFERs were received.\n"));
1049 printf(_("Received %d DHCPOFFER(s)"),valid_responses
);
1051 if(requested_servers
>0)
1052 printf(_(", %s%d of %d requested servers responded"),((requested_responses
<requested_servers
) && requested_responses
>0)?"only ":"",requested_responses
,requested_servers
);
1054 if(request_specific_address
==TRUE
)
1055 printf(_(", requested address (%s) was %soffered"),inet_ntoa(requested_address
),(received_requested_address
==TRUE
)?"":_("not "));
1057 printf(_(", max lease time = "));
1058 if(max_lease_time
==DHCP_INFINITE_TIME
)
1059 printf(_("Infinity"));
1061 printf("%lu sec",(unsigned long)max_lease_time
);
1069 /* process command-line arguments */
1070 int process_arguments(int argc
, char **argv
){
1077 while((c
+=(call_getopt(argc
-c
,&argv
[c
])))<argc
){
1080 if(is_option(argv[c]))
1085 return validate_arguments();
1090 int call_getopt(int argc
, char **argv
){
1094 int option_index
= 0;
1095 static struct option long_options
[] =
1097 {"serverip", required_argument
,0,'s'},
1098 {"requestedip", required_argument
,0,'r'},
1099 {"timeout", required_argument
,0,'t'},
1100 {"interface", required_argument
,0,'i'},
1101 {"mac", required_argument
,0,'m'},
1102 {"unicast", no_argument
, 0,'u'},
1103 {"verbose", no_argument
, 0,'v'},
1104 {"version", no_argument
, 0,'V'},
1105 {"help", no_argument
, 0,'h'},
1110 c
=getopt_long(argc
,argv
,"+hVvt:s:r:t:i:m:u",long_options
,&option_index
);
1114 if(c
==-1||c
==EOF
||c
==1)
1130 case 's': /* DHCP server address */
1131 resolve_host(optarg
,&dhcp_ip
);
1132 add_requested_server(dhcp_ip
);
1135 case 'r': /* address we are requested from DHCP servers */
1136 resolve_host(optarg
,&requested_address
);
1137 request_specific_address
=TRUE
;
1140 case 't': /* timeout */
1143 if(is_intnonneg(optarg))
1146 dhcpoffer_timeout
=atoi(optarg
);
1149 usage("Time interval must be a nonnegative integer\n");
1153 case 'm': /* MAC address */
1155 if((user_specified_mac
=mac_aton(optarg
)) == NULL
)
1156 usage("Cannot parse MAC address.\n");
1158 print_hardware_address(user_specified_mac
);
1162 case 'i': /* interface name */
1164 strncpy(network_interface_name
,optarg
,sizeof(network_interface_name
)-1);
1165 network_interface_name
[sizeof(network_interface_name
)-1]='\x0';
1169 case 'u': /* unicast testing */
1173 case 'V': /* version */
1174 print_revision(progname
,revision
);
1177 case 'h': /* help */
1181 case 'v': /* verbose */
1185 case '?': /* help */
1198 int validate_arguments(void){
1204 #if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
1205 /* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
1207 /* get a message from a stream; return type of message */
1208 static int get_msg(int fd
){
1214 res
= getmsg(fd
, &ctl
, &dat
, &flags
);
1221 printf("%s\n", "get_msg FAILED.");
1235 /* verify that dl_primitive in ctl_area = prim */
1236 static int check_ctrl(int prim
){
1237 dl_error_ack_t
*err_ack
= (dl_error_ack_t
*)ctl_area
;
1239 if(err_ack
->dl_primitive
!= prim
){
1240 printf(_("Error: DLPI stream API failed to get MAC in check_ctrl: %s.\n"), strerror(errno
));
1241 exit(STATE_UNKNOWN
);
1247 /* put a control message on a stream */
1248 static int put_ctrl(int fd
, int len
, int pri
){
1251 if(putmsg(fd
, &ctl
, 0, pri
) < 0){
1252 printf(_("Error: DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n"), strerror(errno
));
1253 exit(STATE_UNKNOWN
);
1259 /* put a control + data message on a stream */
1260 static int put_both(int fd
, int clen
, int dlen
, int pri
){
1264 if(putmsg(fd
, &ctl
, &dat
, pri
) < 0){
1265 printf(_("Error: DLPI stream API failed to get MAC in put_both/putmsg().\n"), strerror(errno
));
1266 exit(STATE_UNKNOWN
);
1272 /* open file descriptor and attach */
1273 static int dl_open(const char *dev
, int unit
, int *fd
){
1274 dl_attach_req_t
*attach_req
= (dl_attach_req_t
*)ctl_area
;
1276 if((*fd
= open(dev
, O_RDWR
)) == -1){
1277 printf(_("Error: DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n"), dev
, strerror(errno
));
1278 exit(STATE_UNKNOWN
);
1280 attach_req
->dl_primitive
= DL_ATTACH_REQ
;
1281 attach_req
->dl_ppa
= unit
;
1282 put_ctrl(*fd
, sizeof(dl_attach_req_t
), 0);
1284 return check_ctrl(DL_OK_ACK
);
1287 /* send DL_BIND_REQ */
1288 static int dl_bind(int fd
, int sap
, u_char
*addr
){
1289 dl_bind_req_t
*bind_req
= (dl_bind_req_t
*)ctl_area
;
1290 dl_bind_ack_t
*bind_ack
= (dl_bind_ack_t
*)ctl_area
;
1292 bind_req
->dl_primitive
= DL_BIND_REQ
;
1293 bind_req
->dl_sap
= sap
;
1294 bind_req
->dl_max_conind
= 1;
1295 bind_req
->dl_service_mode
= DL_CLDLS
;
1296 bind_req
->dl_conn_mgmt
= 0;
1297 bind_req
->dl_xidtest_flg
= 0;
1298 put_ctrl(fd
, sizeof(dl_bind_req_t
), 0);
1300 if (GOT_ERR
== check_ctrl(DL_BIND_ACK
)){
1301 printf(_("Error: DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n"), strerror(errno
));
1302 exit(STATE_UNKNOWN
);
1304 bcopy((u_char
*)bind_ack
+ bind_ack
->dl_addr_offset
, addr
,
1305 bind_ack
->dl_addr_length
);
1310 /***********************************************************************
1312 * function mac_addr_dlpi - get the mac address of the interface with
1313 * type dev (eg lnc, hme) and unit (0, 1 ..)
1315 * parameter: addr: an array of six bytes, has to be allocated by the caller
1317 * return: 0 if OK, -1 if the address could not be determined
1320 ***********************************************************************/
1322 long mac_addr_dlpi( const char *dev
, int unit
, u_char
*addr
){
1324 u_char mac_addr
[25];
1326 if(GOT_ERR
!= dl_open(dev
, unit
, &fd
)){
1327 if(GOT_ERR
!= dl_bind(fd
, INSAP
, mac_addr
)){
1328 bcopy( mac_addr
, addr
, 6);
1337 /* Kompf 2000-2003 */
1341 /* resolve host name or die (TODO: move this to netutils.c!) */
1342 void resolve_host(const char *in
,struct in_addr
*out
){
1343 struct addrinfo hints
, *ai
;
1345 memset(&hints
,0,sizeof(hints
));
1346 hints
.ai_family
=PF_INET
;
1347 if (getaddrinfo(in
,NULL
,&hints
,&ai
) != 0)
1348 usage_va(_("Invalid hostname/address - %s"),optarg
);
1350 memcpy(out
,&((struct sockaddr_in
*)ai
->ai_addr
)->sin_addr
,sizeof(*out
));
1355 /* parse MAC address string, return 6 bytes (unterminated) or NULL */
1356 unsigned char *mac_aton(const char *string
){
1357 static unsigned char result
[6];
1361 for(i
=0, j
=0; string
[i
] != '\0' && j
< sizeof(result
); i
++){
1362 /* ignore ':' and any other non-hex character */
1363 if(!isxdigit(string
[i
]) || !isxdigit(string
[i
+1]))
1368 result
[j
]=strtol(tmp
,(char **)NULL
,16);
1373 return (j
==6) ? result
: NULL
;
1377 void print_hardware_address(const unsigned char *address
){
1380 printf(_("Hardware address: "));
1382 printf("%2.2x:", address
[i
]);
1383 printf("%2.2x", address
[i
]);
1388 /* print usage help */
1389 void print_help(void){
1391 print_revision(progname
,revision
);
1393 printf("Copyright (c) 2001-2004 Ethan Galstad (nagios@nagios.org)\n");
1394 printf (COPYRIGHT
, copyright
, email
);
1396 printf("%s\n", _("This plugin tests the availability of DHCP servers on a network."));
1402 printf (_(UT_HELP_VRSN
));
1404 printf (_(UT_VERBOSE
));
1406 printf (" %s\n", "-s, --serverip=IPADDRESS");
1407 printf (" %s\n", _("IP address of DHCP server that we must hear from"));
1408 printf (" %s\n", "-r, --requestedip=IPADDRESS");
1409 printf (" %s\n", _("IP address that should be offered by at least one DHCP server"));
1410 printf (" %s\n", "-t, --timeout=INTEGER");
1411 printf (" %s\n", _("Seconds to wait for DHCPOFFER before timeout occurs"));
1412 printf (" %s\n", "-i, --interface=STRING");
1413 printf (" %s\n", _("Interface to to use for listening (i.e. eth0)"));
1414 printf (" %s\n", "-m, --mac=STRING");
1415 printf (" %s\n", _("MAC address to use in the DHCP request"));
1416 printf (" %s\n", "-u, --unicast");
1417 printf (" %s\n", _("Unicast testing: mimic a DHCP relay, requires -s"));
1426 printf (_("Usage:"));
1427 printf (" %s [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]\n",progname
);
1428 printf (" [-i interface] [-m mac]\n");