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