Vodz, last_patch_104
[oweals/busybox.git] / networking / udhcp / serverpacket.c
1 /* serverpacket.c
2  *
3  * Constuct and send DHCP server packets
4  *
5  * Russ Dill <Russ.Dill@asu.edu> July 2001
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26 #include <time.h>
27
28 #include "dhcpd.h"
29 #include "options.h"
30 #include "common.h"
31
32 /* send a packet to giaddr using the kernel ip stack */
33 static int send_packet_to_relay(struct dhcpMessage *payload)
34 {
35         DEBUG(LOG_INFO, "Forwarding packet to relay");
36
37         return kernel_packet(payload, server_config.server, SERVER_PORT,
38                         payload->giaddr, SERVER_PORT);
39 }
40
41
42 /* send a packet to a specific arp address and ip address by creating our own ip packet */
43 static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
44 {
45         unsigned char *chaddr;
46         u_int32_t ciaddr;
47         
48         if (force_broadcast) {
49                 DEBUG(LOG_INFO, "broadcasting packet to client (NAK)");
50                 ciaddr = INADDR_BROADCAST;
51                 chaddr = MAC_BCAST_ADDR;
52         } else if (payload->ciaddr) {
53                 DEBUG(LOG_INFO, "unicasting packet to client ciaddr");
54                 ciaddr = payload->ciaddr;
55                 chaddr = payload->chaddr;
56         } else if (ntohs(payload->flags) & BROADCAST_FLAG) {
57                 DEBUG(LOG_INFO, "broadcasting packet to client (requested)");
58                 ciaddr = INADDR_BROADCAST;
59                 chaddr = MAC_BCAST_ADDR;
60         } else {
61                 DEBUG(LOG_INFO, "unicasting packet to client yiaddr");
62                 ciaddr = payload->yiaddr;
63                 chaddr = payload->chaddr;
64         }
65         return raw_packet(payload, server_config.server, SERVER_PORT, 
66                         ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
67 }
68
69
70 /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
71 static int send_packet(struct dhcpMessage *payload, int force_broadcast)
72 {
73         int ret;
74
75         if (payload->giaddr)
76                 ret = send_packet_to_relay(payload);
77         else ret = send_packet_to_client(payload, force_broadcast);
78         return ret;
79 }
80
81
82 static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
83 {
84         init_header(packet, type);
85         packet->xid = oldpacket->xid;
86         memcpy(packet->chaddr, oldpacket->chaddr, 16);
87         packet->flags = oldpacket->flags;
88         packet->giaddr = oldpacket->giaddr;
89         packet->ciaddr = oldpacket->ciaddr;
90         add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server);
91 }
92
93
94 /* add in the bootp options */
95 static void add_bootp_options(struct dhcpMessage *packet)
96 {
97         packet->siaddr = server_config.siaddr;
98         if (server_config.sname)
99                 strncpy(packet->sname, server_config.sname, sizeof(packet->sname) - 1);
100         if (server_config.boot_file)
101                 strncpy(packet->file, server_config.boot_file, sizeof(packet->file) - 1);
102 }
103         
104
105 /* send a DHCP OFFER to a DHCP DISCOVER */
106 int sendOffer(struct dhcpMessage *oldpacket)
107 {
108         struct dhcpMessage packet;
109         struct dhcpOfferedAddr *lease = NULL;
110         u_int32_t req_align, lease_time_align = server_config.lease;
111         unsigned char *req, *lease_time;
112         struct option_set *curr;
113         struct in_addr addr;
114
115         init_packet(&packet, oldpacket, DHCPOFFER);
116         
117         /* ADDME: if static, short circuit */
118         /* the client is in our lease/offered table */
119         if ((lease = find_lease_by_chaddr(oldpacket->chaddr))) {
120                 if (!lease_expired(lease)) 
121                         lease_time_align = lease->expires - time(0);
122                 packet.yiaddr = lease->yiaddr;
123                 
124         /* Or the client has a requested ip */
125         } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) &&
126
127                    /* Don't look here (ugly hackish thing to do) */
128                    memcpy(&req_align, req, 4) &&
129
130                    /* and the ip is in the lease range */
131                    ntohl(req_align) >= ntohl(server_config.start) &&
132                    ntohl(req_align) <= ntohl(server_config.end) &&
133                    
134                    /* and its not already taken/offered */ /* ADDME: check that its not a static lease */
135                    ((!(lease = find_lease_by_yiaddr(req_align)) ||
136                    
137                    /* or its taken, but expired */ /* ADDME: or maybe in here */
138                    lease_expired(lease)))) {
139                                 packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
140
141         /* otherwise, find a free IP */ /*ADDME: is it a static lease? */
142         } else {
143                 packet.yiaddr = find_address(0);
144                 
145                 /* try for an expired lease */
146                 if (!packet.yiaddr) packet.yiaddr = find_address(1);
147         }
148         
149         if(!packet.yiaddr) {
150                 LOG(LOG_WARNING, "no IP addresses to give -- OFFER abandoned");
151                 return -1;
152         }
153         
154         if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
155                 LOG(LOG_WARNING, "lease pool is full -- OFFER abandoned");
156                 return -1;
157         }               
158
159         if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
160                 memcpy(&lease_time_align, lease_time, 4);
161                 lease_time_align = ntohl(lease_time_align);
162                 if (lease_time_align > server_config.lease) 
163                         lease_time_align = server_config.lease;
164         }
165
166         /* Make sure we aren't just using the lease time from the previous offer */
167         if (lease_time_align < server_config.min_lease) 
168                 lease_time_align = server_config.lease;
169         /* ADDME: end of short circuit */               
170         add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
171
172         curr = server_config.options;
173         while (curr) {
174                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
175                         add_option_string(packet.options, curr->data);
176                 curr = curr->next;
177         }
178
179         add_bootp_options(&packet);
180         
181         addr.s_addr = packet.yiaddr;
182         LOG(LOG_INFO, "sending OFFER of %s", inet_ntoa(addr));
183         return send_packet(&packet, 0);
184 }
185
186
187 int sendNAK(struct dhcpMessage *oldpacket)
188 {
189         struct dhcpMessage packet;
190
191         init_packet(&packet, oldpacket, DHCPNAK);
192         
193         DEBUG(LOG_INFO, "sending NAK");
194         return send_packet(&packet, 1);
195 }
196
197
198 int sendACK(struct dhcpMessage *oldpacket, u_int32_t yiaddr)
199 {
200         struct dhcpMessage packet;
201         struct option_set *curr;
202         unsigned char *lease_time;
203         u_int32_t lease_time_align = server_config.lease;
204         struct in_addr addr;
205
206         init_packet(&packet, oldpacket, DHCPACK);
207         packet.yiaddr = yiaddr;
208         
209         if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
210                 memcpy(&lease_time_align, lease_time, 4);
211                 lease_time_align = ntohl(lease_time_align);
212                 if (lease_time_align > server_config.lease) 
213                         lease_time_align = server_config.lease;
214                 else if (lease_time_align < server_config.min_lease) 
215                         lease_time_align = server_config.lease;
216         }
217         
218         add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
219         
220         curr = server_config.options;
221         while (curr) {
222                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
223                         add_option_string(packet.options, curr->data);
224                 curr = curr->next;
225         }
226
227         add_bootp_options(&packet);
228
229         addr.s_addr = packet.yiaddr;
230         LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr));
231
232         if (send_packet(&packet, 0) < 0) 
233                 return -1;
234
235         add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
236
237         return 0;
238 }
239
240
241 int send_inform(struct dhcpMessage *oldpacket)
242 {
243         struct dhcpMessage packet;
244         struct option_set *curr;
245
246         init_packet(&packet, oldpacket, DHCPACK);
247         
248         curr = server_config.options;
249         while (curr) {
250                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
251                         add_option_string(packet.options, curr->data);
252                 curr = curr->next;
253         }
254
255         add_bootp_options(&packet);
256
257         return send_packet(&packet, 0);
258 }