networking: consolidate the IP checksum code. -129 bytes.
[platform/upstream/busybox.git] / networking / udhcp / packet.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Packet ops
4  *
5  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 #include "common.h"
10 #include "dhcpd.h"
11 #include <netinet/in.h>
12 #include <netinet/if_ether.h>
13 #include <netpacket/packet.h>
14
15 void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
16 {
17         memset(packet, 0, sizeof(*packet));
18         packet->op = BOOTREQUEST; /* if client to a server */
19         switch (type) {
20         case DHCPOFFER:
21         case DHCPACK:
22         case DHCPNAK:
23                 packet->op = BOOTREPLY; /* if server to client */
24         }
25         packet->htype = 1; /* ethernet */
26         packet->hlen = 6;
27         packet->cookie = htonl(DHCP_MAGIC);
28         if (DHCP_END != 0)
29                 packet->options[0] = DHCP_END;
30         udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
31 }
32
33 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
34 void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
35 {
36         char buf[sizeof(packet->chaddr)*2 + 1];
37
38         if (dhcp_verbose < 2)
39                 return;
40
41         bb_info_msg(
42                 //" op %x"
43                 //" htype %x"
44                 " hlen %x"
45                 //" hops %x"
46                 " xid %x"
47                 //" secs %x"
48                 //" flags %x"
49                 " ciaddr %x"
50                 " yiaddr %x"
51                 " siaddr %x"
52                 " giaddr %x"
53                 //" chaddr %s"
54                 //" sname %s"
55                 //" file %s"
56                 //" cookie %x"
57                 //" options %s"
58                 //, packet->op
59                 //, packet->htype
60                 , packet->hlen
61                 //, packet->hops
62                 , packet->xid
63                 //, packet->secs
64                 //, packet->flags
65                 , packet->ciaddr
66                 , packet->yiaddr
67                 , packet->siaddr_nip
68                 , packet->gateway_nip
69                 //, packet->chaddr[16]
70                 //, packet->sname[64]
71                 //, packet->file[128]
72                 //, packet->cookie
73                 //, packet->options[]
74         );
75         *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
76         bb_info_msg(" chaddr %s", buf);
77 }
78 #endif
79
80 /* Read a packet from socket fd, return -1 on read error, -2 on packet error */
81 int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
82 {
83         int bytes;
84         unsigned char *vendor;
85
86         memset(packet, 0, sizeof(*packet));
87         bytes = safe_read(fd, packet, sizeof(*packet));
88         if (bytes < 0) {
89                 log1("Packet read error, ignoring");
90                 return bytes; /* returns -1 */
91         }
92
93         if (packet->cookie != htonl(DHCP_MAGIC)) {
94                 bb_info_msg("Packet with bad magic, ignoring");
95                 return -2;
96         }
97         log1("Received a packet");
98         udhcp_dump_packet(packet);
99
100         if (packet->op == BOOTREQUEST) {
101                 vendor = udhcp_get_option(packet, DHCP_VENDOR);
102                 if (vendor) {
103 #if 0
104                         static const char broken_vendors[][8] = {
105                                 "MSFT 98",
106                                 ""
107                         };
108                         int i;
109                         for (i = 0; broken_vendors[i][0]; i++) {
110                                 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)strlen(broken_vendors[i])
111                                  && strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - OPT_DATA]) == 0
112                                 ) {
113                                         log1("Broken client (%s), forcing broadcast replies",
114                                                 broken_vendors[i]);
115                                         packet->flags |= htons(BROADCAST_FLAG);
116                                 }
117                         }
118 #else
119                         if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1)
120                          && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
121                         ) {
122                                 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
123                                 packet->flags |= htons(BROADCAST_FLAG);
124                         }
125 #endif
126                 }
127         }
128
129         return bytes;
130 }
131
132 /* Construct a ip/udp header for a packet, send packet */
133 int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
134                 uint32_t source_nip, int source_port,
135                 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
136                 int ifindex)
137 {
138         struct sockaddr_ll dest_sll;
139         struct ip_udp_dhcp_packet packet;
140         unsigned padding;
141         int fd;
142         int result = -1;
143         const char *msg;
144
145         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
146         if (fd < 0) {
147                 msg = "socket(%s)";
148                 goto ret_msg;
149         }
150
151         memset(&dest_sll, 0, sizeof(dest_sll));
152         memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
153         packet.data = *dhcp_pkt; /* struct copy */
154
155         dest_sll.sll_family = AF_PACKET;
156         dest_sll.sll_protocol = htons(ETH_P_IP);
157         dest_sll.sll_ifindex = ifindex;
158         dest_sll.sll_halen = 6;
159         memcpy(dest_sll.sll_addr, dest_arp, 6);
160
161         if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
162                 msg = "bind(%s)";
163                 goto ret_close;
164         }
165
166         /* We were sending full-sized DHCP packets (zero padded),
167          * but some badly configured servers were seen dropping them.
168          * Apparently they drop all DHCP packets >576 *ethernet* octets big,
169          * whereas they may only drop packets >576 *IP* octets big
170          * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
171          *
172          * In order to work with those buggy servers,
173          * we truncate packets after end option byte.
174          */
175         padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
176
177         packet.ip.protocol = IPPROTO_UDP;
178         packet.ip.saddr = source_nip;
179         packet.ip.daddr = dest_nip;
180         packet.udp.source = htons(source_port);
181         packet.udp.dest = htons(dest_port);
182         /* size, excluding IP header: */
183         packet.udp.len = htons(UDP_DHCP_SIZE - padding);
184         /* for UDP checksumming, ip.len is set to UDP packet len */
185         packet.ip.tot_len = packet.udp.len;
186         packet.udp.check = inet_cksum((uint16_t *)&packet,
187                         IP_UDP_DHCP_SIZE - padding);
188         /* but for sending, it is set to IP packet len */
189         packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
190         packet.ip.ihl = sizeof(packet.ip) >> 2;
191         packet.ip.version = IPVERSION;
192         packet.ip.ttl = IPDEFTTL;
193         packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
194
195         udhcp_dump_packet(dhcp_pkt);
196         result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
197                         (struct sockaddr *) &dest_sll, sizeof(dest_sll));
198         msg = "sendto";
199  ret_close:
200         close(fd);
201         if (result < 0) {
202  ret_msg:
203                 bb_perror_msg(msg, "PACKET");
204         }
205         return result;
206 }
207
208 /* Let the kernel do all the work for packet generation */
209 int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
210                 uint32_t source_nip, int source_port,
211                 uint32_t dest_nip, int dest_port)
212 {
213         struct sockaddr_in client;
214         unsigned padding;
215         int fd;
216         int result = -1;
217         const char *msg;
218
219         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
220         if (fd < 0) {
221                 msg = "socket(%s)";
222                 goto ret_msg;
223         }
224         setsockopt_reuseaddr(fd);
225
226         memset(&client, 0, sizeof(client));
227         client.sin_family = AF_INET;
228         client.sin_port = htons(source_port);
229         client.sin_addr.s_addr = source_nip;
230         if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
231                 msg = "bind(%s)";
232                 goto ret_close;
233         }
234
235         memset(&client, 0, sizeof(client));
236         client.sin_family = AF_INET;
237         client.sin_port = htons(dest_port);
238         client.sin_addr.s_addr = dest_nip;
239         if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
240                 msg = "connect";
241                 goto ret_close;
242         }
243
244         udhcp_dump_packet(dhcp_pkt);
245
246         padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
247         result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
248         msg = "write";
249  ret_close:
250         close(fd);
251         if (result < 0) {
252  ret_msg:
253                 bb_perror_msg(msg, "UDP");
254         }
255         return result;
256 }