2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
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.
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.
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.
18 #include <sys/socket.h>
28 struct ether_header eth;
33 uint8_t op, htype, hlen, hops;
36 struct in_addr ciaddr, yiaddr, siaddr, giaddr;
37 unsigned char chaddr[16];
38 unsigned char sname[64];
39 unsigned char file[128];
43 chksum(uint16_t sum, const uint8_t *data, uint16_t len)
48 last = data + len - 1;
51 t = (data[0] << 8) + data[1];
59 t = (data[0] << 8) + 0;
68 bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward)
70 struct ip_packet *pkt = data;
72 struct dhcp_header *dhcp;
76 if (pkt->eth.ether_type != htons(ETH_P_IP))
79 if (pkt->iph.version != 4)
82 if (pkt->iph.protocol != IPPROTO_UDP)
85 udp = (void *) ((char *) &pkt->iph + (pkt->iph.ihl << 2));
86 dhcp = (void *) (udp + 1);
88 udplen = ntohs(udp->len);
89 if (udplen > len - ((char *) udp - (char *) data))
92 if (udp->dest != htons(67) && udp->source != htons(67))
95 if (dhcp->op != 1 && dhcp->op != 2)
102 relayd_refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
104 DPRINTF(2, "%s: handling DHCP %s\n", rif->ifname, (dhcp->op == 1 ? "request" : "response"));
106 dhcp->flags |= htons(DHCP_FLAG_BROADCAST);
109 sum = udplen + IPPROTO_UDP;
110 sum = chksum(sum, (void *) &pkt->iph.saddr, 8);
111 sum = chksum(sum, (void *) udp, udplen);
115 udp->check = htons(~sum);
117 relayd_forward_bcast_packet(rif, data, len);