xref: /freebsd/stand/libsa/ip.c (revision 47dd1d1b619cc035b82b49a91a25544309ff95ae)
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 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/queue.h>
46 
47 #include <string.h>
48 
49 #include <net/if.h>
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/in_systm.h>
53 
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/udp.h>
58 #include <netinet/udp_var.h>
59 
60 #include "stand.h"
61 #include "net.h"
62 
63 typedef STAILQ_HEAD(ipqueue, ip_queue) ip_queue_t;
64 struct ip_queue {
65 	void		*ipq_pkt;
66 	struct ip	*ipq_hdr;
67 	STAILQ_ENTRY(ip_queue) ipq_next;
68 };
69 
70 /*
71  * Fragment re-assembly queue.
72  */
73 struct ip_reasm {
74 	struct in_addr	ip_src;
75 	struct in_addr	ip_dst;
76 	uint16_t	ip_id;
77 	uint8_t		ip_proto;
78 	uint8_t		ip_ttl;
79 	size_t		ip_total_size;
80 	ip_queue_t	ip_queue;
81 	void		*ip_pkt;
82 	struct ip	*ip_hdr;
83 	STAILQ_ENTRY(ip_reasm) ip_next;
84 };
85 
86 STAILQ_HEAD(ire_list, ip_reasm) ire_list = STAILQ_HEAD_INITIALIZER(ire_list);
87 
88 /* Caller must leave room for ethernet and ip headers in front!! */
89 ssize_t
90 sendip(struct iodesc *d, void *pkt, size_t len, uint8_t proto)
91 {
92 	ssize_t cc;
93 	struct ip *ip;
94 	u_char *ea;
95 
96 #ifdef NET_DEBUG
97 	if (debug) {
98 		printf("sendip: proto: %x d=%p called.\n", proto, (void *)d);
99 		if (d) {
100 			printf("saddr: %s:%d",
101 			    inet_ntoa(d->myip), ntohs(d->myport));
102 			printf(" daddr: %s:%d\n",
103 			    inet_ntoa(d->destip), ntohs(d->destport));
104 		}
105 	}
106 #endif
107 
108 	ip = (struct ip *)pkt - 1;
109 	len += sizeof(*ip);
110 
111 	bzero(ip, sizeof(*ip));
112 
113 	ip->ip_v = IPVERSION;			/* half-char */
114 	ip->ip_hl = sizeof(*ip) >> 2;		/* half-char */
115 	ip->ip_len = htons(len);
116 	ip->ip_p = proto;			/* char */
117 	ip->ip_ttl = IPDEFTTL;			/* char */
118 	ip->ip_src = d->myip;
119 	ip->ip_dst = d->destip;
120 	ip->ip_sum = in_cksum(ip, sizeof(*ip));	 /* short, but special */
121 
122 	if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
123 	    netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask))
124 		ea = arpwhohas(d, ip->ip_dst);
125 	else
126 		ea = arpwhohas(d, gateip);
127 
128 	cc = sendether(d, ip, len, ea, ETHERTYPE_IP);
129 	if (cc == -1)
130 		return (-1);
131 	if (cc != len)
132 		panic("sendip: bad write (%zd != %zd)", cc, len);
133 	return (cc - sizeof(*ip));
134 }
135 
136 static void
137 ip_reasm_free(struct ip_reasm *ipr)
138 {
139 	struct ip_queue *ipq;
140 
141 	while ((ipq = STAILQ_FIRST(&ipr->ip_queue)) != NULL) {
142 		STAILQ_REMOVE_HEAD(&ipr->ip_queue, ipq_next);
143 		free(ipq->ipq_pkt);
144 		free(ipq);
145 	}
146 	free(ipr->ip_pkt);
147 	free(ipr);
148 }
149 
150 static int
151 ip_reasm_add(struct ip_reasm *ipr, void *pkt, struct ip *ip)
152 {
153 	struct ip_queue *ipq, *prev, *p;
154 
155 	if ((ipq = calloc(1, sizeof (*ipq))) == NULL)
156 		return (1);
157 
158 	ipq->ipq_pkt = pkt;
159 	ipq->ipq_hdr = ip;
160 
161 	prev = NULL;
162 	STAILQ_FOREACH(p, &ipr->ip_queue, ipq_next) {
163 		if ((ntohs(p->ipq_hdr->ip_off) & IP_OFFMASK) <
164 		    (ntohs(ip->ip_off) & IP_OFFMASK)) {
165 			prev = p;
166 			continue;
167 		}
168 		if (prev == NULL)
169 			break;
170 
171 		STAILQ_INSERT_AFTER(&ipr->ip_queue, prev, ipq, ipq_next);
172 		return (0);
173 	}
174 	STAILQ_INSERT_HEAD(&ipr->ip_queue, ipq, ipq_next);
175 	return (0);
176 }
177 
178 /*
179  * Receive a IP packet and validate it is for us.
180  */
181 static ssize_t
182 readipv4(struct iodesc *d, void **pkt, void **payload, time_t tleft,
183     uint8_t proto)
184 {
185 	ssize_t n;
186 	size_t hlen;
187 	struct ether_header *eh;
188 	struct ip *ip;
189 	struct udphdr *uh;
190 	uint16_t etype;		/* host order */
191 	char *ptr;
192 	struct ip_reasm *ipr;
193 	struct ip_queue *ipq, *last;
194 
195 #ifdef NET_DEBUG
196 	if (debug)
197 		printf("readip: called\n");
198 #endif
199 
200 	ip = NULL;
201 	ptr = NULL;
202 	n = readether(d, (void **)&ptr, (void **)&ip, tleft, &etype);
203 	if (n == -1 || n < sizeof(*ip) + sizeof(*uh)) {
204 		free(ptr);
205 		return (-1);
206 	}
207 
208 	/* Ethernet address checks now in readether() */
209 
210 	/* Need to respond to ARP requests. */
211 	if (etype == ETHERTYPE_ARP) {
212 		struct arphdr *ah = (void *)ip;
213 		if (ah->ar_op == htons(ARPOP_REQUEST)) {
214 			/* Send ARP reply */
215 			arp_reply(d, ah);
216 		}
217 		free(ptr);
218 		errno = EAGAIN;	/* Call me again. */
219 		return (-1);
220 	}
221 
222 	if (etype != ETHERTYPE_IP) {
223 #ifdef NET_DEBUG
224 		if (debug)
225 			printf("readip: not IP. ether_type=%x\n", etype);
226 #endif
227 		free(ptr);
228 		return (-1);
229 	}
230 
231 	/* Check ip header */
232 	if (ip->ip_v != IPVERSION ||	/* half char */
233 	    ip->ip_p != proto) {
234 #ifdef NET_DEBUG
235 		if (debug) {
236 			printf("readip: IP version or proto. ip_v=%d ip_p=%d\n",
237 			    ip->ip_v, ip->ip_p);
238 		}
239 #endif
240 		free(ptr);
241 		return (-1);
242 	}
243 
244 	hlen = ip->ip_hl << 2;
245 	if (hlen < sizeof(*ip) ||
246 	    in_cksum(ip, hlen) != 0) {
247 #ifdef NET_DEBUG
248 		if (debug)
249 			printf("readip: short hdr or bad cksum.\n");
250 #endif
251 		free(ptr);
252 		return (-1);
253 	}
254 	if (n < ntohs(ip->ip_len)) {
255 #ifdef NET_DEBUG
256 		if (debug)
257 			printf("readip: bad length %d < %d.\n",
258 			       (int)n, ntohs(ip->ip_len));
259 #endif
260 		free(ptr);
261 		return (-1);
262 	}
263 	if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
264 #ifdef NET_DEBUG
265 		if (debug) {
266 			printf("readip: bad saddr %s != ", inet_ntoa(d->myip));
267 			printf("%s\n", inet_ntoa(ip->ip_dst));
268 		}
269 #endif
270 		free(ptr);
271 		return (-1);
272 	}
273 
274 	/* Unfragmented packet. */
275 	if ((ntohs(ip->ip_off) & IP_MF) == 0 &&
276 	    (ntohs(ip->ip_off) & IP_OFFMASK) == 0) {
277 		uh = (struct udphdr *)((uintptr_t)ip + sizeof (*ip));
278 		/* If there were ip options, make them go away */
279 		if (hlen != sizeof(*ip)) {
280 			bcopy(((u_char *)ip) + hlen, uh, uh->uh_ulen - hlen);
281 			ip->ip_len = htons(sizeof(*ip));
282 			n -= hlen - sizeof(*ip);
283 		}
284 
285 		n = (n > (ntohs(ip->ip_len) - sizeof(*ip))) ?
286 		    ntohs(ip->ip_len) - sizeof(*ip) : n;
287 		*pkt = ptr;
288 		*payload = (void *)((uintptr_t)ip + sizeof(*ip));
289 		return (n);
290 	}
291 
292 	STAILQ_FOREACH(ipr, &ire_list, ip_next) {
293 		if (ipr->ip_src.s_addr == ip->ip_src.s_addr &&
294 		    ipr->ip_dst.s_addr == ip->ip_dst.s_addr &&
295 		    ipr->ip_id == ip->ip_id &&
296 		    ipr->ip_proto == ip->ip_p)
297 			break;
298 	}
299 
300 	/* Allocate new reassembly entry */
301 	if (ipr == NULL) {
302 		if ((ipr = calloc(1, sizeof (*ipr))) == NULL) {
303 			free(ptr);
304 			return (-1);
305 		}
306 
307 		ipr->ip_src = ip->ip_src;
308 		ipr->ip_dst = ip->ip_dst;
309 		ipr->ip_id = ip->ip_id;
310 		ipr->ip_proto = ip->ip_p;
311 		ipr->ip_ttl = MAXTTL;
312 		STAILQ_INIT(&ipr->ip_queue);
313 		STAILQ_INSERT_TAIL(&ire_list, ipr, ip_next);
314 	}
315 
316 	if (ip_reasm_add(ipr, ptr, ip) != 0) {
317 		STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next);
318 		free(ipr);
319 		free(ptr);
320 		return (-1);
321 	}
322 
323 	if ((ntohs(ip->ip_off) & IP_MF) == 0) {
324 		ipr->ip_total_size = (8 * (ntohs(ip->ip_off) & IP_OFFMASK));
325 		ipr->ip_total_size += n + sizeof (*ip);
326 		ipr->ip_total_size += sizeof (struct ether_header);
327 
328 		ipr->ip_pkt = malloc(ipr->ip_total_size + 2);
329 		if (ipr->ip_pkt == NULL) {
330 			STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next);
331 			ip_reasm_free(ipr);
332 			return (-1);
333 		}
334 	}
335 
336 	/*
337 	 * If we do not have re-assembly buffer ipr->ip_pkt, we are still
338 	 * missing fragments, so just restart the read.
339 	 */
340 	if (ipr->ip_pkt == NULL) {
341 		errno = EAGAIN;
342 		return (-1);
343 	}
344 
345 	/*
346 	 * Walk the packet list in reassembly queue, if we got all the
347 	 * fragments, build the packet.
348 	 */
349 	n = 0;
350 	last = NULL;
351 	STAILQ_FOREACH(ipq, &ipr->ip_queue, ipq_next) {
352 		if ((ntohs(ipq->ipq_hdr->ip_off) & IP_OFFMASK) != n / 8) {
353 			STAILQ_REMOVE(&ire_list, ipr, ip_reasm, ip_next);
354 			ip_reasm_free(ipr);
355 			return (-1);
356 		}
357 
358 		n += ntohs(ipq->ipq_hdr->ip_len) - (ipq->ipq_hdr->ip_hl << 2);
359 		last = ipq;
360 	}
361 	if ((ntohs(last->ipq_hdr->ip_off) & IP_MF) != 0) {
362 		errno = EAGAIN;
363 		return (-1);
364 	}
365 
366 	ipq = STAILQ_FIRST(&ipr->ip_queue);
367 	/* Fabricate ethernet header */
368 	eh = (struct ether_header *)((uintptr_t)ipr->ip_pkt + 2);
369 	bcopy((void *)((uintptr_t)ipq->ipq_pkt + 2), eh, sizeof (*eh));
370 
371 	/* Fabricate IP header */
372 	ipr->ip_hdr = (struct ip *)((uintptr_t)eh + sizeof (*eh));
373 	bcopy(ipq->ipq_hdr, ipr->ip_hdr, sizeof (*ipr->ip_hdr));
374 	ipr->ip_hdr->ip_hl = sizeof (*ipr->ip_hdr) >> 2;
375 	ipr->ip_hdr->ip_len = htons(n);
376 	ipr->ip_hdr->ip_sum = 0;
377 	ipr->ip_hdr->ip_sum = in_cksum(ipr->ip_hdr, sizeof (*ipr->ip_hdr));
378 
379 	n = 0;
380 	ptr = (char *)((uintptr_t)ipr->ip_hdr + sizeof (*ipr->ip_hdr));
381 	STAILQ_FOREACH(ipq, &ipr->ip_queue, ipq_next) {
382 		char *data;
383 		size_t len;
384 
385 		hlen = ipq->ipq_hdr->ip_hl << 2;
386 		len = ntohs(ipq->ipq_hdr->ip_len) - hlen;
387 		data = (char *)((uintptr_t)ipq->ipq_hdr + hlen);
388 
389 		bcopy(data, ptr + n, len);
390 		n += len;
391 	}
392 
393 	*pkt = ipr->ip_pkt;
394 	ipr->ip_pkt = NULL;	/* Avoid free from ip_reasm_free() */
395 	*payload = ptr;
396 
397 	/* Clean up the reassembly list */
398 	while ((ipr = STAILQ_FIRST(&ire_list)) != NULL) {
399 		STAILQ_REMOVE_HEAD(&ire_list, ip_next);
400 		ip_reasm_free(ipr);
401 	}
402 	return (n);
403 }
404 
405 /*
406  * Receive a IP packet.
407  */
408 ssize_t
409 readip(struct iodesc *d, void **pkt, void **payload, time_t tleft,
410     uint8_t proto)
411 {
412 	time_t t;
413 	ssize_t ret = -1;
414 
415 	t = getsecs();
416 	while ((getsecs() - t) < tleft) {
417 		errno = 0;
418 		ret = readipv4(d, pkt, payload, tleft, proto);
419 		if (ret >= 0)
420 			return (ret);
421 		/* Bubble up the error if it wasn't successful */
422 		if (errno != EAGAIN)
423 			return (-1);
424 	}
425 	/* We've exhausted tleft; timeout */
426 	errno = ETIMEDOUT;
427 	return (-1);
428 }
429