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