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.
19 #include <sys/socket.h>
29 struct ether_header eth
;
35 DHCP_OPTION_ROUTER
= 0x03,
36 DHCP_OPTION_ROUTES
= 0x79,
37 DHCP_OPTION_END
= 0xff,
47 uint8_t op
, htype
, hlen
, hops
;
50 struct in_addr ciaddr
, yiaddr
, siaddr
, giaddr
;
51 unsigned char chaddr
[16];
52 unsigned char sname
[64];
53 unsigned char file
[128];
55 uint8_t option_data
[];
59 chksum(uint16_t sum
, const uint8_t *data
, uint16_t len
)
64 last
= data
+ len
- 1;
67 t
= (data
[0] << 8) + data
[1];
75 t
= (data
[0] << 8) + 0;
85 parse_dhcp_options(struct relayd_host
*host
, struct dhcp_header
*dhcp
, int len
)
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 };
91 while((uint8_t *) opt
< end
) {
92 if ((uint8_t *) opt
+ opt
->len
> end
)
95 opt
= (void *) &opt
->data
[opt
->len
];
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);
102 relayd_add_pending_route(opt
->data
, dest
, 0, 10000);
104 case DHCP_OPTION_ROUTES
:
105 DPRINTF(2, "Found a DHCP static routes option, len=%d\n", opt
->len
);
107 case DHCP_OPTION_END
:
111 DPRINTF(3, "Skipping unknown DHCP option %02x\n", opt
->code
);
118 bool relayd_handle_dhcp_packet(struct relayd_interface
*rif
, void *data
, int len
, bool forward
, bool parse
)
120 struct ip_packet
*pkt
= data
;
122 struct dhcp_header
*dhcp
;
123 struct relayd_host
*host
;
127 if (pkt
->eth
.ether_type
!= htons(ETH_P_IP
))
130 if (pkt
->iph
.version
!= 4)
133 if (pkt
->iph
.protocol
!= IPPROTO_UDP
)
136 udp
= (void *) ((char *) &pkt
->iph
+ (pkt
->iph
.ihl
<< 2));
137 dhcp
= (void *) (udp
+ 1);
139 udplen
= ntohs(udp
->len
);
140 if (udplen
> len
- ((char *) udp
- (char *) data
))
143 if (udp
->dest
!= htons(67) && udp
->source
!= htons(67))
146 if (dhcp
->op
!= 1 && dhcp
->op
!= 2)
153 host
= relayd_refresh_host(rif
, pkt
->eth
.ether_shost
, (void *) &pkt
->iph
.saddr
);
155 parse_dhcp_options(host
, dhcp
, udplen
- sizeof(struct udphdr
));
158 DPRINTF(2, "%s: handling DHCP %s\n", rif
->ifname
, (dhcp
->op
== 1 ? "request" : "response"));
160 dhcp
->flags
|= htons(DHCP_FLAG_BROADCAST
);
163 sum
= udplen
+ IPPROTO_UDP
;
164 sum
= chksum(sum
, (void *) &pkt
->iph
.saddr
, 8);
165 sum
= chksum(sum
, (void *) udp
, udplen
);
169 udp
->check
= htons(~sum
);
171 relayd_forward_bcast_packet(rif
, data
, len
);