2 * dhcp.c DHCP processing. Done poorly for now.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2008 The FreeRADIUS server project
21 * Copyright 2008 Alan DeKok <aland@deployingradius.com>
27 * Same contents as listen_socket_t.
29 typedef struct dhcp_socket_t {
35 const char *interface;
36 RADCLIENT_LIST *clients;
39 * DHCP-specific additions.
41 int suppress_responses;
42 RADCLIENT dhcp_client;
45 static int dhcp_process(REQUEST *request)
50 vp = pairfind(request->packet->vps, DHCP2ATTR(53)); /* DHCP-Message-Type */
52 DICT_VALUE *dv = dict_valbyattr(DHCP2ATTR(53), vp->vp_integer);
53 DEBUG("Trying sub-section dhcp %s {...}",
54 dv->name ? dv->name : "<unknown>");
55 rcode = module_post_auth(vp->vp_integer, request);
57 DEBUG("DHCP: Failed to find DHCP-Message-Type in packet!");
58 rcode = RLM_MODULE_FAIL;
62 * Look for Relay attribute, and forward it if necessary.
64 vp = pairfind(request->config_items, DHCP2ATTR(270));
67 RADIUS_PACKET relayed;
69 request->reply->code = 0; /* don't reply to the client */
72 * Find the original giaddr.
73 * FIXME: Maybe look in the original packet?
75 * It's invalid to have giaddr=0 AND a relay option
77 giaddr = pairfind(request->packet->vps, 266);
78 if (giaddr && (giaddr->vp_ipaddr == htonl(INADDR_ANY))) {
79 if (pairfind(request->packet->vps, DHCP2ATTR(82))) {
80 RDEBUG("DHCP: Received packet with giaddr = 0 and containing relay option: Discarding packet");
85 * FIXME: Add cache by XID.
87 RDEBUG("DHCP: Cannot yet relay packets with giaddr = 0");
91 if (request->packet->data[3] > 10) {
92 RDEBUG("DHCP: Number of hops is greater than 10: not relaying");
97 * Forward it VERBATIM to the next server, rather
100 memcpy(&relayed, request->packet, sizeof(relayed));
102 relayed.dst_ipaddr.af = AF_INET;
103 relayed.dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
104 relayed.dst_port = request->packet->dst_port;
106 relayed.src_ipaddr = request->packet->dst_ipaddr;
107 relayed.src_port = request->packet->dst_port;
109 relayed.data = rad_malloc(relayed.data_len);
110 memcpy(relayed.data, request->packet->data, request->packet->data_len);
114 * The only field that changes is the number of hops
116 relayed.data[3]++; /* number of hops */
119 * Forward the relayed packet VERBATIM, don't
120 * respond to the client, and forget completely
121 * about this request.
123 fr_dhcp_send(&relayed);
128 vp = pairfind(request->reply->vps, DHCP2ATTR(53)); /* DHCP-Message-Type */
130 request->reply->code = vp->vp_integer;
131 if ((request->reply->code != 0) &&
132 (request->reply->code < PW_DHCP_OFFSET)) {
133 request->reply->code += PW_DHCP_OFFSET;
136 else switch (rcode) {
138 case RLM_MODULE_UPDATED:
139 if (request->packet->code == PW_DHCP_DISCOVER) {
140 request->reply->code = PW_DHCP_OFFER;
143 } else if (request->packet->code == PW_DHCP_REQUEST) {
144 request->reply->code = PW_DHCP_ACK;
147 request->reply->code = PW_DHCP_NAK;
151 case RLM_MODULE_REJECT:
152 case RLM_MODULE_FAIL:
153 case RLM_MODULE_INVALID:
154 case RLM_MODULE_NOOP:
155 case RLM_MODULE_NOTFOUND:
156 if (request->packet->code == PW_DHCP_DISCOVER) {
157 request->reply->code = 0; /* ignore the packet */
159 request->reply->code = PW_DHCP_NAK;
163 case RLM_MODULE_HANDLED:
170 static int dhcp_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
178 rcode = common_socket_parse(cs, this);
179 if (rcode != 0) return rcode;
184 * Undocumented extension for testing without
185 * destroying your network!
187 sock->suppress_responses = FALSE;
188 cp = cf_pair_find(cs, "suppress_responses");
192 value = cf_pair_value(cp);
194 if (value && (strcmp(value, "yes") == 0)) {
195 sock->suppress_responses = TRUE;
200 * Initialize the fake client.
202 client = &sock->dhcp_client;
203 memset(client, 0, sizeof(*client));
204 client->ipaddr.af = AF_INET;
205 client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
207 client->longname = client->shortname = "dhcp";
208 client->secret = client->shortname;
209 client->nastype = strdup("none");
216 * Check if an incoming request is "ok"
218 * It takes packets, not requests. It sees if the packet looks
219 * OK. If so, it does a number of sanity checks on it.
221 static int dhcp_socket_recv(rad_listen_t *listener,
222 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
224 RADIUS_PACKET *packet;
227 packet = fr_dhcp_recv(listener->fd);
229 radlog(L_ERR, "%s", fr_strerror());
233 sock = listener->data;
234 if (!received_request(listener, packet, prequest, &sock->dhcp_client)) {
239 *pfun = dhcp_process;
246 * Send an authentication response packet
248 static int dhcp_socket_send(rad_listen_t *listener, REQUEST *request)
252 rad_assert(request->listener == listener);
253 rad_assert(listener->send == dhcp_socket_send);
255 if (request->reply->code == 0) return 0; /* don't reply */
257 if (fr_dhcp_encode(request->reply, request->packet) < 0) {
261 sock = listener->data;
262 if (sock->suppress_responses) return 0;
265 * Don't send anything
267 return fr_dhcp_send(request->reply);
271 static int dhcp_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
273 DEBUG2("NO ENCODE!");
275 return fr_dhcp_encode(request->reply, request->packet);
279 static int dhcp_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
281 return fr_dhcp_decode(request->packet);
283 #endif /* WITH_DCHP */