xref: /freebsd/stand/libsa/ip.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*
2  * Copyright (c) 1992 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * The send and receive functions were originally implemented in udp.c and
36  * moved here. Also it is likely some more cleanup can be done, especially
37  * once we will implement the support for tcp.
38  */
39 
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 
45 #include <string.h>
46 
47 #include <net/if.h>
48 #include <netinet/in.h>
49 #include <netinet/if_ether.h>
50 #include <netinet/in_systm.h>
51 
52 #include <netinet/ip.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/udp.h>
55 #include <netinet/udp_var.h>
56 
57 #include "stand.h"
58 #include "net.h"
59 
60 typedef STAILQ_HEAD(ipqueue, ip_queue) ip_queue_t;
61 struct ip_queue {
62 	void		*ipq_pkt;
63 	struct ip	*ipq_hdr;
64 	STAILQ_ENTRY(ip_queue) ipq_next;
65 };
66 
67 /*
68  * Fragment re-assembly queue.
69  */
70 struct ip_reasm {
71 	struct in_addr	ip_src;
72 	struct in_addr	ip_dst;
73 	uint16_t	ip_id;
74 	uint8_t		ip_proto;
75 	uint8_t		ip_ttl;
76 	size_t		ip_total_size;
77 	ip_queue_t	ip_queue;
78 	void		*ip_pkt;
79 	struct ip	*ip_hdr;
80 	STAILQ_ENTRY(ip_reasm) ip_next;
81 };
82 
83 STAILQ_HEAD(ire_list, ip_reasm) ire_list = STAILQ_HEAD_INITIALIZER(ire_list);
84 
85 /* Caller must leave room for ethernet and ip headers in front!! */
86 ssize_t
87 sendip(struct iodesc *d, void *pkt, size_t len, uint8_t proto)
88 {
89 	ssize_t cc;
90 	struct ip *ip;
91 	u_char *ea;
92 
93 #ifdef NET_DEBUG
94 	if (debug) {
95 		printf("sendip: proto: %x d=%p called.\n", proto, (void *)d);
96 		if (d) {
97 			printf("saddr: %s:%d",
98 			    inet_ntoa(d->myip), ntohs(d->myport));
99 			printf(" daddr: %s:%d\n",
100 			    inet_ntoa(d->destip), ntohs(d->destport));
101 		}
102 	}
103 #endif
104 
105 	ip = (struct ip *)pkt - 1;
106 	len += sizeof(*ip);
107 
108 	bzero(ip, sizeof(*ip));
109 
110 	ip->ip_v = IPVERSION;			/* half-char */
111 	ip->ip_hl = sizeof(*ip) >> 2;		/* half-char */
112 	ip->ip_len = htons(len);
113 	ip->ip_p = proto;			/* char */
114 	ip->ip_ttl = IPDEFTTL;			/* char */
115 	ip->ip_src = d->myip;
116 	ip->ip_dst = d->destip;
117 	ip->ip_sum = in_cksum(ip, sizeof(*ip));	 /* short, but special */
118 
119 	if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
120 	    netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
121 		ea = arpwhohas(d, ip->ip_dst);
122 	else
123 		ea = arpwhohas(d, gateip);
124 
125 	cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
126 	if (cc == -1)
127 		return (-1);
128 	if (cc != len)
129 		panic("sendip: bad write (%zd != %zd)", cc, len);
130 	return (cc - sizeof(*ip));
131 }
132 
133 static void
134 ip_reasm_free(struct ip_reasm *ipr)
135 {
136 	struct ip_queue *ipq;
137 
138 	while ((ipq = STAILQ_FIRST(&ipr->ip_queue)) != NULL) {
139 		STAILQ_REMOVE_HEAD(&ipr->ip_queue, ipq_next);
140 		free(ipq->ipq_pkt);
141 		free(ipq);
142 	}
143 	free(ipr->ip_pkt);
144 	free(ipr);
145 }
146 
147 static int
148 ip_reasm_add(struct ip_reasm *ipr, void *pkt, struct ip *ip)
149 {
150 	struct ip_queue *ipq, *prev, *p;
151 
152 	if ((ipq = calloc(1, sizeof (*ipq))) == NULL)
153 		return (1);
154 
155 	ipq->ipq_pkt = pkt;
156 	ipq->ipq_hdr = ip;
157 
158 	prev = NULL;
159 	STAILQ_FOREACH(p, &ipr->ip_queue, ipq_next) {
160 		if ((ntohs(p->ipq_hdr->ip_off) & IP_OFFMASK) <
161 		    (ntohs(ip->ip_off) & IP_OFFMASK)) {
162 			prev = p;
163 			continue;
164 		}
165 		if (prev == NULL)
166 			break;
167 
168 		STAILQ_INSERT_AFTER(&ipr->ip_queue, prev, ipq, ipq_next);
169 		return (0);
170 	}
171 	STAILQ_INSERT_HEAD(&ipr->ip_queue, ipq, ipq_next);
172 	return (0);
173 }
174 
175 /*
176  * Receive a IP packet and validate it is for us.
177  */
178 static ssize_t
179 readipv4(struct iodesc *d, void **pkt, void **payload, time_t tleft,
180     uint8_t proto)
181 {
182 	ssize_t n;
183 	size_t hlen;
184 	struct ether_header *eh;
185 	struct ip *ip;
186 	struct udphdr *uh;
187 	uint16_t etype;		/* host order */
188 	char *ptr;
189 	struct ip_reasm *ipr;
190 	struct ip_queue *ipq, *last;
191 
192 #ifdef NET_DEBUG
193 	if (debug)
194 		printf("readip: called\n");
195 #endif
196 
197 	ip = NULL;
198 	ptr = NULL;
199 	n = readether(d, (void **)&ptr, (void **)&ip, tleft, &etype);
200 	if (n == -1 || n < sizeof(*ip) + sizeof(*uh)) {
201 		free(ptr);
202 		return (-1);
203 	}
204 
205 	/* Ethernet address checks now in readether() */
206 
207 	/* Need to respond to ARP requests. */
208 	if (etype == ETHERTYPE_ARP) {
209 		struct arphdr *ah = (void *)ip;
210 		if (ah->ar_op == htons(ARPOP_REQUEST)) {
211 			/* Send ARP reply */
212 			arp_reply(d, ah);
213 		}
214 		free(ptr);
215 		errno = EAGAIN;	/* Call me again. */
216 		return (-1);
217 	}
218 
219 	if (etype != ETHERTYPE_IP) {
220 #ifdef NET_DEBUG
221 		if (debug)
222 			printf("readip: not IP. ether_type=%x\n", etype);
223 #endif
224 		free(ptr);
225 		return (-1);
226 	}
227 
228 	/* Check ip header */
229 	if (ip->ip_v != IPVERSION ||	/* half char */
230 	    ip->ip_p != proto) {
231 #ifdef NET_DEBUG
232 		if (debug) {
233 			printf("readip: IP version or proto. ip_v=%d ip_p=%d\n",
234 			    ip->ip_v, ip->ip_p);
235 		}
236 #endif
237 		free(ptr);
238 		return (-1);
239 	}
240 
241 	hlen = ip->ip_hl << 2;
242 	if (hlen < sizeof(*ip) ||
243 	    in_cksum(ip, hlen) != 0) {
244 #ifdef NET_DEBUG
245 		if (debug)
246 			printf("readip: short hdr or bad cksum.\n");
247 #endif
248 		free(ptr);
249 		return (-1);
250 	}
251 	if (n < ntohs(ip->ip_len)) {
252 #ifdef NET_DEBUG
253 		if (debug)
254 			printf("readip: bad length %d < %d.\n",
255 			       (int)n, ntohs(ip->ip_len));
256 #endif
257 		free(ptr);
258 		return (-1);
259 	}
260 	if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
261 #ifdef NET_DEBUG
262 		if (debug) {
263 			printf("readip: bad saddr %s != ", inet_ntoa(d->myip));
264 			printf("%s\n", inet_ntoa(ip->ip_dst));
265 		}
266 #endif
267 		free(ptr);
268 		return (-1);
269 	}
270 
271 	/* Unfragmented packet. */
272 	if ((ntohs(ip->ip_off) & IP_MF) == 0 &&
273 	    (ntohs(ip->ip_off) & IP_OFFMASK) == 0) {
274 		uh = (struct udphdr *)((uintptr_t)ip + sizeof (*ip));
275 		/* If there were ip options, make them go away */
276 		if (hlen != sizeof(*ip)) {
277 			bcopy(((u_char *)ip) + hlen, uh, uh->uh_ulen - hlen);
278 			ip->ip_len = htons(sizeof(*ip));
279 			n -= hlen - sizeof(*ip);
280 		}
281 
282 		n = (n > (ntohs(ip->ip_len) - sizeof(*ip))) ?
283 		    ntohs(ip->ip_len) - sizeof(*ip) : n;
284 		*pkt = ptr;
285 		*payload = (void *)((uintptr_t)ip + sizeof(*ip));
286 		return (n);
287 	}
288 
289 	STAILQ_FOREACH(ipr, &ire_list, ip_next) {
290 		if (ipr->ip_src.s_addr == ip->ip_src.s_addr &&
291 		    ipr->ip_dst.s_addr == ip->ip_dst.s_addr &&
292 		    ipr->ip_id == ip->ip_id &&
293 		    ipr->ip_proto == ip->ip_p)
294 			break;
295 	}
296 
297 	/* Allocate new reassembly entry */
298 	if (ipr == NULL) {
299 		if ((ipr = calloc(1, sizeof (*ipr))) == NULL) {
300 			free(ptr);
301 			return (-1);
302 		}
303 
304 		ipr->ip_src = ip->ip_src;
305 		ipr->ip_dst = ip->ip_dst;
306 		ipr->ip_id = ip->ip_id;
307 		ipr->ip_proto = ip->ip_p;
308 		ipr->ip_ttl = MAXTTL;
309 		STAILQ_INIT(&ipr->ip_queue);
310 		STAILQ_INSERT_TAIL(&ire_list, ipr, ip_next);
311 	}
312 
313 	if (ip_reasm_add(ipr, ptr, ip) != 0) {
314 		STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next);
315 		free(ipr);
316 		free(ptr);
317 		return (-1);
318 	}
319 
320 	if ((ntohs(ip->ip_off) & IP_MF) == 0) {
321 		ipr->ip_total_size = (8 * (ntohs(ip->ip_off) & IP_OFFMASK));
322 		ipr->ip_total_size += n + sizeof (*ip);
323 		ipr->ip_total_size += sizeof (struct ether_header);
324 
325 		ipr->ip_pkt = malloc(ipr->ip_total_size + 2);
326 		if (ipr->ip_pkt == NULL) {
327 			STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next);
328 			ip_reasm_free(ipr);
329 			return (-1);
330 		}
331 	}
332 
333 	/*
334 	 * If we do not have re-assembly buffer ipr->ip_pkt, we are still
335 	 * missing fragments, so just restart the read.
336 	 */
337 	if (ipr->ip_pkt == NULL) {
338 		errno = EAGAIN;
339 		return (-1);
340 	}
341 
342 	/*
343 	 * Walk the packet list in reassembly queue, if we got all the
344 	 * fragments, build the packet.
345 	 */
346 	n = 0;
347 	last = NULL;
348 	STAILQ_FOREACH(ipq, &ipr->ip_queue, ipq_next) {
349 		if ((ntohs(ipq->ipq_hdr->ip_off) & IP_OFFMASK) != n / 8) {
350 			STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next);
351 			ip_reasm_free(ipr);
352 			return (-1);
353 		}
354 
355 		n += ntohs(ipq->ipq_hdr->ip_len) - (ipq->ipq_hdr->ip_hl << 2);
356 		last = ipq;
357 	}
358 	if ((ntohs(last->ipq_hdr->ip_off) & IP_MF) != 0) {
359 		errno = EAGAIN;
360 		return (-1);
361 	}
362 
363 	ipq = STAILQ_FIRST(&ipr->ip_queue);
364 	/* Fabricate ethernet header */
365 	eh = (struct ether_header *)((uintptr_t)ipr->ip_pkt + 2);
366 	bcopy((void *)((uintptr_t)ipq->ipq_pkt + 2), eh, sizeof (*eh));
367 
368 	/* Fabricate IP header */
369 	ipr->ip_hdr = (struct ip *)((uintptr_t)eh + sizeof (*eh));
370 	bcopy(ipq->ipq_hdr, ipr->ip_hdr, sizeof (*ipr->ip_hdr));
371 	ipr->ip_hdr->ip_hl = sizeof (*ipr->ip_hdr) >> 2;
372 	ipr->ip_hdr->ip_len = htons(n);
373 	ipr->ip_hdr->ip_sum = 0;
374 	ipr->ip_hdr->ip_sum = in_cksum(ipr->ip_hdr, sizeof (*ipr->ip_hdr));
375 
376 	n = 0;
377 	ptr = (char *)((uintptr_t)ipr->ip_hdr + sizeof (*ipr->ip_hdr));
378 	STAILQ_FOREACH(ipq, &ipr->ip_queue, ipq_next) {
379 		char *data;
380 		size_t len;
381 
382 		hlen = ipq->ipq_hdr->ip_hl << 2;
383 		len = ntohs(ipq->ipq_hdr->ip_len) - hlen;
384 		data = (char *)((uintptr_t)ipq->ipq_hdr + hlen);
385 
386 		bcopy(data, ptr + n, len);
387 		n += len;
388 	}
389 
390 	*pkt = ipr->ip_pkt;
391 	ipr->ip_pkt = NULL;	/* Avoid free from ip_reasm_free() */
392 	*payload = ptr;
393 
394 	/* Clean up the reassembly list */
395 	while ((ipr = STAILQ_FIRST(&ire_list)) != NULL) {
396 		STAILQ_REMOVE_HEAD(&ire_list, ip_next);
397 		ip_reasm_free(ipr);
398 	}
399 	return (n);
400 }
401 
402 /*
403  * Receive a IP packet.
404  */
405 ssize_t
406 readip(struct iodesc *d, void **pkt, void **payload, time_t tleft,
407     uint8_t proto)
408 {
409 	time_t t;
410 	ssize_t ret = -1;
411 
412 	t = getsecs();
413 	while ((getsecs() - t) < tleft) {
414 		errno = 0;
415 		ret = readipv4(d, pkt, payload, tleft, proto);
416 		if (ret >= 0)
417 			return (ret);
418 		/* Bubble up the error if it wasn't successful */
419 		if (errno != EAGAIN)
420 			return (-1);
421 	}
422 	/* We've exhausted tleft; timeout */
423 	errno = ETIMEDOUT;
424 	return (-1);
425 }
426