aefe34fd80e6c5ab8fe5bc19581e12954e0e782a
[project/relayd.git] / dhcp.c
1 /*
2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <sys/socket.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25
26 #include "relayd.h"
27
28 struct ip_packet {
29 struct ether_header eth;
30 struct iphdr iph;
31 } __packed;
32
33
34 enum {
35 DHCP_OPTION_ROUTER = 0x03,
36 DHCP_OPTION_ROUTES = 0x79,
37 DHCP_OPTION_END = 0xff,
38 };
39
40 struct dhcp_option {
41 uint8_t code;
42 uint8_t len;
43 uint8_t data[];
44 };
45
46 struct dhcp_header {
47 uint8_t op, htype, hlen, hops;
48 uint32_t xit;
49 uint16_t secs, flags;
50 struct in_addr ciaddr, yiaddr, siaddr, giaddr;
51 unsigned char chaddr[16];
52 unsigned char sname[64];
53 unsigned char file[128];
54 uint32_t cookie;
55 uint8_t option_data[];
56 } __packed;
57
58 static uint16_t
59 chksum(uint16_t sum, const uint8_t *data, uint16_t len)
60 {
61 const uint8_t *last;
62 uint16_t t;
63
64 last = data + len - 1;
65
66 while(data < last) {
67 t = (data[0] << 8) + data[1];
68 sum += t;
69 if(sum < t)
70 sum++;
71 data += 2;
72 }
73
74 if(data == last) {
75 t = (data[0] << 8) + 0;
76 sum += t;
77 if(sum < t)
78 sum++;
79 }
80
81 return sum;
82 }
83
84 static void
85 parse_dhcp_options(struct relayd_host *host, struct dhcp_header *dhcp, int len)
86 {
87 uint8_t *end = (uint8_t *) dhcp + len;
88 struct dhcp_option *opt = (void *)dhcp->option_data;
89 static const uint8_t dest[4] = { 0, 0, 0, 0 };
90
91 while((uint8_t *) opt < end) {
92 if ((uint8_t *) opt + opt->len > end)
93 break;
94
95 opt = (void *) &opt->data[opt->len];
96 switch(opt->code) {
97 case DHCP_OPTION_ROUTER:
98 DPRINTF(2, "Found a DHCP router option, len=%d\n", opt->len);
99 if (!memcmp(opt->data, host->ipaddr, 4))
100 relayd_add_host_route(host, dest, 0);
101 else
102 relayd_add_pending_route(opt->data, dest, 0, 10000);
103 break;
104 case DHCP_OPTION_ROUTES:
105 DPRINTF(2, "Found a DHCP static routes option, len=%d\n", opt->len);
106 break;
107 case DHCP_OPTION_END:
108 opt = (void *) end;
109 continue;
110 default:
111 DPRINTF(3, "Skipping unknown DHCP option %02x\n", opt->code);
112 continue;
113 }
114
115 }
116 }
117
118 bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward, bool parse)
119 {
120 struct ip_packet *pkt = data;
121 struct udphdr *udp;
122 struct dhcp_header *dhcp;
123 struct relayd_host *host;
124 int udplen;
125 uint16_t sum;
126
127 if (pkt->eth.ether_type != htons(ETH_P_IP))
128 return false;
129
130 if (pkt->iph.version != 4)
131 return false;
132
133 if (pkt->iph.protocol != IPPROTO_UDP)
134 return false;
135
136 udp = (void *) ((char *) &pkt->iph + (pkt->iph.ihl << 2));
137 dhcp = (void *) (udp + 1);
138
139 udplen = ntohs(udp->len);
140 if (udplen > len - ((char *) udp - (char *) data))
141 return false;
142
143 if (udp->dest != htons(67) && udp->source != htons(67))
144 return false;
145
146 if (dhcp->op != 1 && dhcp->op != 2)
147 return false;
148
149 if (!forward)
150 return true;
151
152 if (dhcp->op == 2) {
153 host = relayd_refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
154 if (host && parse)
155 parse_dhcp_options(host, dhcp, udplen - sizeof(struct udphdr));
156 }
157
158 DPRINTF(2, "%s: handling DHCP %s\n", rif->ifname, (dhcp->op == 1 ? "request" : "response"));
159
160 dhcp->flags |= htons(DHCP_FLAG_BROADCAST);
161
162 udp->check = 0;
163 sum = udplen + IPPROTO_UDP;
164 sum = chksum(sum, (void *) &pkt->iph.saddr, 8);
165 sum = chksum(sum, (void *) udp, udplen);
166 if (sum == 0)
167 sum = 0xffff;
168
169 udp->check = htons(~sum);
170
171 relayd_forward_bcast_packet(rif, data, len);
172
173 return true;
174 }
175
176