Get rid of "Interrupted service call" messages in syslog
[tomato.git] / release / src / router / openvpn / tap-win32 / dhcp.c
blobb6b28bb7f32a74d0d1de998aa329dbc3f59247ae
1 /*
2 * TAP-Win32/TAP-Win64 -- A kernel driver to provide virtual tap
3 * device functionality on Windows.
5 * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
7 * This source code is Copyright (C) 2002-2009 OpenVPN Technologies, Inc.,
8 * and is released under the GPL version 2 (see below), however due
9 * to the extra costs of supporting Windows Vista, OpenVPN Solutions
10 * LLC reserves the right to change the terms of the TAP-Win32/TAP-Win64
11 * license for versions 9.1 and higher prior to the official release of
12 * OpenVPN 2.1.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program (see the file COPYING included with this
25 * distribution); if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 //=========================
30 // Code to set DHCP options
31 //=========================
33 VOID
34 SetDHCPOpt (DHCPMsg *m, void *data, unsigned int len)
36 if (!m->overflow)
38 if (m->optlen + len <= DHCP_OPTIONS_BUFFER_SIZE)
40 if (len)
42 NdisMoveMemory (m->msg.options + m->optlen, data, len);
43 m->optlen += len;
46 else
48 m->overflow = TRUE;
53 VOID
54 SetDHCPOpt0 (DHCPMsg *msg, int type)
56 DHCPOPT0 opt;
57 opt.type = (UCHAR) type;
58 SetDHCPOpt (msg, &opt, sizeof (opt));
61 VOID
62 SetDHCPOpt8 (DHCPMsg *msg, int type, ULONG data)
64 DHCPOPT8 opt;
65 opt.type = (UCHAR) type;
66 opt.len = sizeof (opt.data);
67 opt.data = (UCHAR) data;
68 SetDHCPOpt (msg, &opt, sizeof (opt));
71 VOID
72 SetDHCPOpt32 (DHCPMsg *msg, int type, ULONG data)
74 DHCPOPT32 opt;
75 opt.type = (UCHAR) type;
76 opt.len = sizeof (opt.data);
77 opt.data = data;
78 SetDHCPOpt (msg, &opt, sizeof (opt));
81 //==============
82 // Checksum code
83 //==============
85 USHORT
86 ip_checksum (const UCHAR *buf, const int len_ip_header)
88 USHORT word16;
89 ULONG sum = 0;
90 int i;
92 // make 16 bit words out of every two adjacent 8 bit words in the packet
93 // and add them up
94 for (i = 0; i < len_ip_header - 1; i += 2) {
95 word16 = ((buf[i] << 8) & 0xFF00) + (buf[i+1] & 0xFF);
96 sum += (ULONG) word16;
99 // take only 16 bits out of the 32 bit sum and add up the carries
100 while (sum >> 16)
101 sum = (sum & 0xFFFF) + (sum >> 16);
103 // one's complement the result
104 return ((USHORT) ~sum);
107 USHORT
108 udp_checksum (const UCHAR *buf,
109 const int len_udp,
110 const UCHAR *src_addr,
111 const UCHAR *dest_addr)
113 USHORT word16;
114 ULONG sum = 0;
115 int i;
117 // make 16 bit words out of every two adjacent 8 bit words and
118 // calculate the sum of all 16 bit words
119 for (i = 0; i < len_udp; i += 2){
120 word16 = ((buf[i] << 8) & 0xFF00) + ((i + 1 < len_udp) ? (buf[i+1] & 0xFF) : 0);
121 sum += word16;
124 // add the UDP pseudo header which contains the IP source and destination addresses
125 for (i = 0; i < 4; i += 2){
126 word16 =((src_addr[i] << 8) & 0xFF00) + (src_addr[i+1] & 0xFF);
127 sum += word16;
129 for (i = 0; i < 4; i += 2){
130 word16 =((dest_addr[i] << 8) & 0xFF00) + (dest_addr[i+1] & 0xFF);
131 sum += word16;
134 // the protocol number and the length of the UDP packet
135 sum += (USHORT) IPPROTO_UDP + (USHORT) len_udp;
137 // keep only the last 16 bits of the 32 bit calculated sum and add the carries
138 while (sum >> 16)
139 sum = (sum & 0xFFFF) + (sum >> 16);
141 // Take the one's complement of sum
142 return ((USHORT) ~sum);
145 //================================
146 // Set IP and UDP packet checksums
147 //================================
149 VOID
150 SetChecksumDHCPMsg (DHCPMsg *m)
152 // Set IP checksum
153 m->msg.pre.ip.check = htons (ip_checksum ((UCHAR *) &m->msg.pre.ip, sizeof (IPHDR)));
155 // Set UDP Checksum
156 m->msg.pre.udp.check = htons (udp_checksum ((UCHAR *) &m->msg.pre.udp,
157 sizeof (UDPHDR) + sizeof (DHCP) + m->optlen,
158 (UCHAR *)&m->msg.pre.ip.saddr,
159 (UCHAR *)&m->msg.pre.ip.daddr));
162 //===================
163 // DHCP message tests
164 //===================
167 GetDHCPMessageType (const DHCP *dhcp, const int optlen)
169 const UCHAR *p = (UCHAR *) (dhcp + 1);
170 int i;
172 for (i = 0; i < optlen; ++i)
174 const UCHAR type = p[i];
175 const int room = optlen - i - 1;
176 if (type == DHCP_END) // didn't find what we were looking for
177 return -1;
178 else if (type == DHCP_PAD) // no-operation
180 else if (type == DHCP_MSG_TYPE) // what we are looking for
182 if (room >= 2)
184 if (p[i+1] == 1) // message length should be 1
185 return p[i+2]; // return message type
187 return -1;
189 else // some other message
191 if (room >= 1)
193 const int len = p[i+1]; // get message length
194 i += (len + 1); // advance to next message
198 return -1;
201 BOOLEAN
202 DHCPMessageOurs (const TapAdapterPointer p_Adapter,
203 const ETH_HEADER *eth,
204 const IPHDR *ip,
205 const UDPHDR *udp,
206 const DHCP *dhcp)
208 // Must be UDPv4 protocol
209 if (!(eth->proto == htons (ETH_P_IP) && ip->protocol == IPPROTO_UDP))
210 return FALSE;
212 // Source MAC must be our adapter
213 if (!MAC_EQUAL (eth->src, p_Adapter->m_MAC))
214 return FALSE;
216 // Dest MAC must be either broadcast or our virtual DHCP server
217 if (!(MAC_EQUAL (eth->dest, p_Adapter->m_MAC_Broadcast)
218 || MAC_EQUAL (eth->dest, p_Adapter->m_dhcp_server_mac)))
219 return FALSE;
221 // Port numbers must be correct
222 if (!(udp->dest == htons (BOOTPS_PORT)
223 && udp->source == htons (BOOTPC_PORT)))
224 return FALSE;
226 // Hardware address must be MAC addr sized
227 if (!(dhcp->hlen == sizeof (MACADDR)))
228 return FALSE;
230 // Hardware address must match our adapter
231 if (!MAC_EQUAL (eth->src, dhcp->chaddr))
232 return FALSE;
234 return TRUE;
238 //=====================================================
239 // Build all of DHCP packet except for DHCP options.
240 // Assume that *p has been zeroed before we are called.
241 //=====================================================
243 VOID
244 BuildDHCPPre (const TapAdapterPointer a,
245 DHCPPre *p,
246 const ETH_HEADER *eth,
247 const IPHDR *ip,
248 const UDPHDR *udp,
249 const DHCP *dhcp,
250 const int optlen,
251 const int type)
253 // Should we broadcast or direct to a specific MAC / IP address?
254 const BOOLEAN broadcast = (type == DHCPNAK
255 || MAC_EQUAL (eth->dest, a->m_MAC_Broadcast));
256 // Build ethernet header
258 COPY_MAC (p->eth.src, a->m_dhcp_server_mac);
260 if (broadcast)
261 COPY_MAC (p->eth.dest, a->m_MAC_Broadcast);
262 else
263 COPY_MAC (p->eth.dest, eth->src);
265 p->eth.proto = htons (ETH_P_IP);
267 // Build IP header
269 p->ip.version_len = (4 << 4) | (sizeof (IPHDR) >> 2);
270 p->ip.tos = 0;
271 p->ip.tot_len = htons (sizeof (IPHDR) + sizeof (UDPHDR) + sizeof (DHCP) + optlen);
272 p->ip.id = 0;
273 p->ip.frag_off = 0;
274 p->ip.ttl = 16;
275 p->ip.protocol = IPPROTO_UDP;
276 p->ip.check = 0;
277 p->ip.saddr = a->m_dhcp_server_ip;
279 if (broadcast)
280 p->ip.daddr = ~0;
281 else
282 p->ip.daddr = a->m_dhcp_addr;
284 // Build UDP header
286 p->udp.source = htons (BOOTPS_PORT);
287 p->udp.dest = htons (BOOTPC_PORT);
288 p->udp.len = htons (sizeof (UDPHDR) + sizeof (DHCP) + optlen);
289 p->udp.check = 0;
291 // Build DHCP response
293 p->dhcp.op = BOOTREPLY;
294 p->dhcp.htype = 1;
295 p->dhcp.hlen = sizeof (MACADDR);
296 p->dhcp.hops = 0;
297 p->dhcp.xid = dhcp->xid;
298 p->dhcp.secs = 0;
299 p->dhcp.flags = 0;
300 p->dhcp.ciaddr = 0;
302 if (type == DHCPNAK)
303 p->dhcp.yiaddr = 0;
304 else
305 p->dhcp.yiaddr = a->m_dhcp_addr;
307 p->dhcp.siaddr = a->m_dhcp_server_ip;
308 p->dhcp.giaddr = 0;
309 COPY_MAC (p->dhcp.chaddr, eth->src);
310 p->dhcp.magic = htonl (0x63825363);
312 //=============================
313 // Build specific DHCP messages
314 //=============================
316 VOID
317 SendDHCPMsg (const TapAdapterPointer a,
318 const int type,
319 const ETH_HEADER *eth,
320 const IPHDR *ip,
321 const UDPHDR *udp,
322 const DHCP *dhcp)
324 DHCPMsg *pkt;
326 if (!(type == DHCPOFFER || type == DHCPACK || type == DHCPNAK))
328 DEBUGP (("[TAP] SendDHCPMsg: Bad DHCP type: %d\n", type));
329 return;
332 pkt = (DHCPMsg *) MemAlloc (sizeof (DHCPMsg), TRUE);
334 if (pkt)
336 //-----------------------
337 // Build DHCP options
338 //-----------------------
340 // Message Type
341 SetDHCPOpt8 (pkt, DHCP_MSG_TYPE, type);
343 // Server ID
344 SetDHCPOpt32 (pkt, DHCP_SERVER_ID, a->m_dhcp_server_ip);
346 if (type == DHCPOFFER || type == DHCPACK)
348 // Lease Time
349 SetDHCPOpt32 (pkt, DHCP_LEASE_TIME, htonl (a->m_dhcp_lease_time));
351 // Netmask
352 SetDHCPOpt32 (pkt, DHCP_NETMASK, a->m_dhcp_netmask);
354 // Other user-defined options
355 SetDHCPOpt (pkt,
356 a->m_dhcp_user_supplied_options_buffer,
357 a->m_dhcp_user_supplied_options_buffer_len);
360 // End
361 SetDHCPOpt0 (pkt, DHCP_END);
363 if (!DHCPMSG_OVERFLOW (pkt))
365 // The initial part of the DHCP message (not including options) gets built here
366 BuildDHCPPre (a,
367 &pkt->msg.pre,
368 eth,
370 udp,
371 dhcp,
372 DHCPMSG_LEN_OPT (pkt),
373 type);
375 SetChecksumDHCPMsg (pkt);
377 DUMP_PACKET ("DHCPMsg",
378 DHCPMSG_BUF (pkt),
379 DHCPMSG_LEN_FULL (pkt));
381 // Return DHCP response to kernel
382 InjectPacketDeferred (a,
383 DHCPMSG_BUF (pkt),
384 DHCPMSG_LEN_FULL (pkt));
386 else
388 DEBUGP (("[TAP] SendDHCPMsg: DHCP buffer overflow\n"));
391 MemFree (pkt, sizeof (DHCPMsg));
395 //===================================================================
396 // Handle a BOOTPS packet produced by the local system to
397 // resolve the address/netmask of this adapter.
398 // If we are in TAP_IOCTL_CONFIG_DHCP_MASQ mode, reply
399 // to the message. Return TRUE if we processed the passed
400 // message, so that downstream stages can ignore it.
401 //===================================================================
403 BOOLEAN
404 ProcessDHCP (TapAdapterPointer p_Adapter,
405 const ETH_HEADER *eth,
406 const IPHDR *ip,
407 const UDPHDR *udp,
408 const DHCP *dhcp,
409 int optlen)
411 int msg_type;
413 // Sanity check IP header
414 if (!(ntohs (ip->tot_len) == sizeof (IPHDR) + sizeof (UDPHDR) + sizeof (DHCP) + optlen
415 && (ntohs (ip->frag_off) & IP_OFFMASK) == 0))
416 return TRUE;
418 // Does this message belong to us?
419 if (!DHCPMessageOurs (p_Adapter, eth, ip, udp, dhcp))
420 return FALSE;
422 msg_type = GetDHCPMessageType (dhcp, optlen);
424 // Drop non-BOOTREQUEST messages
425 if (dhcp->op != BOOTREQUEST)
426 return TRUE;
428 // Drop any messages except DHCPDISCOVER or DHCPREQUEST
429 if (!(msg_type == DHCPDISCOVER || msg_type == DHCPREQUEST))
430 return TRUE;
432 // Should we reply with DHCPOFFER, DHCPACK, or DHCPNAK?
433 if (msg_type == DHCPREQUEST
434 && ((dhcp->ciaddr && dhcp->ciaddr != p_Adapter->m_dhcp_addr)
435 || !p_Adapter->m_dhcp_received_discover
436 || p_Adapter->m_dhcp_bad_requests >= BAD_DHCPREQUEST_NAK_THRESHOLD))
437 SendDHCPMsg (p_Adapter,
438 DHCPNAK,
439 eth, ip, udp, dhcp);
440 else
441 SendDHCPMsg (p_Adapter,
442 (msg_type == DHCPDISCOVER ? DHCPOFFER : DHCPACK),
443 eth, ip, udp, dhcp);
445 // Remember if we received a DHCPDISCOVER
446 if (msg_type == DHCPDISCOVER)
447 p_Adapter->m_dhcp_received_discover = TRUE;
449 // Is this a bad DHCPREQUEST?
450 if (msg_type == DHCPREQUEST && dhcp->ciaddr != p_Adapter->m_dhcp_addr)
451 ++p_Adapter->m_dhcp_bad_requests;
453 return TRUE;
456 #if DBG
458 const char *
459 message_op_text (int op)
461 switch (op)
463 case BOOTREQUEST:
464 return "BOOTREQUEST";
465 case BOOTREPLY:
466 return "BOOTREPLY";
467 default:
468 return "???";
472 const char *
473 message_type_text (int type)
475 switch (type)
477 case DHCPDISCOVER:
478 return "DHCPDISCOVER";
479 case DHCPOFFER:
480 return "DHCPOFFER";
481 case DHCPREQUEST:
482 return "DHCPREQUEST";
483 case DHCPDECLINE:
484 return "DHCPDECLINE";
485 case DHCPACK:
486 return "DHCPACK";
487 case DHCPNAK:
488 return "DHCPNAK";
489 case DHCPRELEASE:
490 return "DHCPRELEASE";
491 case DHCPINFORM:
492 return "DHCPINFORM";
493 default:
494 return "???";
498 const char *
499 port_name (int port)
501 switch (port)
503 case BOOTPS_PORT:
504 return "BOOTPS";
505 case BOOTPC_PORT:
506 return "BOOTPC";
507 default:
508 return "unknown";
512 VOID
513 DumpDHCP (const ETH_HEADER *eth,
514 const IPHDR *ip,
515 const UDPHDR *udp,
516 const DHCP *dhcp,
517 const int optlen)
519 DEBUGP ((" %s", message_op_text (dhcp->op)));
520 DEBUGP ((" %s ", message_type_text (GetDHCPMessageType (dhcp, optlen))));
521 PrIP (ip->saddr);
522 DEBUGP ((":%s[", port_name (ntohs (udp->source))));
523 PrMac (eth->src);
524 DEBUGP (("] -> "));
525 PrIP (ip->daddr);
526 DEBUGP ((":%s[", port_name (ntohs (udp->dest))));
527 PrMac (eth->dest);
528 DEBUGP (("]"));
529 if (dhcp->ciaddr)
531 DEBUGP ((" ci="));
532 PrIP (dhcp->ciaddr);
534 if (dhcp->yiaddr)
536 DEBUGP ((" yi="));
537 PrIP (dhcp->yiaddr);
539 if (dhcp->siaddr)
541 DEBUGP ((" si="));
542 PrIP (dhcp->siaddr);
544 if (dhcp->hlen == sizeof (MACADDR))
546 DEBUGP ((" ch="));
547 PrMac (dhcp->chaddr);
550 DEBUGP ((" xid=0x%08x", ntohl (dhcp->xid)));
552 if (ntohl (dhcp->magic) != 0x63825363)
553 DEBUGP ((" ma=0x%08x", ntohl (dhcp->magic)));
554 if (dhcp->htype != 1)
555 DEBUGP ((" htype=%d", dhcp->htype));
556 if (dhcp->hops)
557 DEBUGP ((" hops=%d", dhcp->hops));
558 if (ntohs (dhcp->secs))
559 DEBUGP ((" secs=%d", ntohs (dhcp->secs)));
560 if (ntohs (dhcp->flags))
561 DEBUGP ((" flags=0x%04x", ntohs (dhcp->flags)));
563 // extra stuff
565 if (ip->version_len != 0x45)
566 DEBUGP ((" vl=0x%02x", ip->version_len));
567 if (ntohs (ip->tot_len) != sizeof (IPHDR) + sizeof (UDPHDR) + sizeof (DHCP) + optlen)
568 DEBUGP ((" tl=%d", ntohs (ip->tot_len)));
569 if (ntohs (udp->len) != sizeof (UDPHDR) + sizeof (DHCP) + optlen)
570 DEBUGP ((" ul=%d", ntohs (udp->len)));
572 if (ip->tos)
573 DEBUGP ((" tos=0x%02x", ip->tos));
574 if (ntohs (ip->id))
575 DEBUGP ((" id=0x%04x", ntohs (ip->id)));
576 if (ntohs (ip->frag_off))
577 DEBUGP ((" frag_off=0x%04x", ntohs (ip->frag_off)));
579 DEBUGP ((" ttl=%d", ip->ttl));
580 DEBUGP ((" ic=0x%04x [0x%04x]", ntohs (ip->check),
581 ip_checksum ((UCHAR*)ip, sizeof (IPHDR))));
582 DEBUGP ((" uc=0x%04x [0x%04x/%d]", ntohs (udp->check),
583 udp_checksum ((UCHAR *) udp,
584 sizeof (UDPHDR) + sizeof (DHCP) + optlen,
585 (UCHAR *) &ip->saddr,
586 (UCHAR *) &ip->daddr),
587 optlen));
589 // Options
591 const UCHAR *opt = (UCHAR *) (dhcp + 1);
592 int i;
594 DEBUGP ((" OPT"));
595 for (i = 0; i < optlen; ++i)
597 const UCHAR data = opt[i];
598 DEBUGP ((".%d", data));
603 #endif /* DBG */