xref: /minix3/sys/lib/libsa/ip.c (revision 58a2b0008e28f606a7f7f5faaeaba4faac57a1ea)
1*58a2b000SEvgeniy Ivanov /* $NetBSD: ip.c,v 1.2 2011/05/13 23:35:09 nakayama Exp $ */
2*58a2b000SEvgeniy Ivanov 
3*58a2b000SEvgeniy Ivanov /*
4*58a2b000SEvgeniy Ivanov  * Copyright (c) 1992 Regents of the University of California.
5*58a2b000SEvgeniy Ivanov  * Copyright (c) 2010 Zoltan Arnold NAGY
6*58a2b000SEvgeniy Ivanov  * All rights reserved.
7*58a2b000SEvgeniy Ivanov  *
8*58a2b000SEvgeniy Ivanov  * This software was developed by the Computer Systems Engineering group
9*58a2b000SEvgeniy Ivanov  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10*58a2b000SEvgeniy Ivanov  * contributed to Berkeley.
11*58a2b000SEvgeniy Ivanov  *
12*58a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
13*58a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
14*58a2b000SEvgeniy Ivanov  * are met:
15*58a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
16*58a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
17*58a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
18*58a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
19*58a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
20*58a2b000SEvgeniy Ivanov  * 3. All advertising materials mentioning features or use of this software
21*58a2b000SEvgeniy Ivanov  *    must display the following acknowledgement:
22*58a2b000SEvgeniy Ivanov  *	This product includes software developed by the University of
23*58a2b000SEvgeniy Ivanov  *	California, Lawrence Berkeley Laboratory and its contributors.
24*58a2b000SEvgeniy Ivanov  * 4. Neither the name of the University nor the names of its contributors
25*58a2b000SEvgeniy Ivanov  *    may be used to endorse or promote products derived from this software
26*58a2b000SEvgeniy Ivanov  *    without specific prior written permission.
27*58a2b000SEvgeniy Ivanov  *
28*58a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29*58a2b000SEvgeniy Ivanov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30*58a2b000SEvgeniy Ivanov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31*58a2b000SEvgeniy Ivanov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32*58a2b000SEvgeniy Ivanov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33*58a2b000SEvgeniy Ivanov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34*58a2b000SEvgeniy Ivanov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35*58a2b000SEvgeniy Ivanov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36*58a2b000SEvgeniy Ivanov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37*58a2b000SEvgeniy Ivanov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38*58a2b000SEvgeniy Ivanov  * SUCH DAMAGE.
39*58a2b000SEvgeniy Ivanov  *
40*58a2b000SEvgeniy Ivanov  */
41*58a2b000SEvgeniy Ivanov 
42*58a2b000SEvgeniy Ivanov #include <sys/param.h>
43*58a2b000SEvgeniy Ivanov #include <sys/socket.h>
44*58a2b000SEvgeniy Ivanov #include <net/if.h>
45*58a2b000SEvgeniy Ivanov #include <net/if_ether.h>
46*58a2b000SEvgeniy Ivanov #include <netinet/in.h>
47*58a2b000SEvgeniy Ivanov #include <netinet/in_systm.h>
48*58a2b000SEvgeniy Ivanov #include <netinet/ip.h>
49*58a2b000SEvgeniy Ivanov #include <netinet/ip_var.h>
50*58a2b000SEvgeniy Ivanov #include <netinet/udp.h>
51*58a2b000SEvgeniy Ivanov #include <netinet/udp_var.h>
52*58a2b000SEvgeniy Ivanov 
53*58a2b000SEvgeniy Ivanov #ifdef _STANDALONE
54*58a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
55*58a2b000SEvgeniy Ivanov #else
56*58a2b000SEvgeniy Ivanov #include <string.h>
57*58a2b000SEvgeniy Ivanov #endif
58*58a2b000SEvgeniy Ivanov 
59*58a2b000SEvgeniy Ivanov #include "stand.h"
60*58a2b000SEvgeniy Ivanov #include "net.h"
61*58a2b000SEvgeniy Ivanov 
62*58a2b000SEvgeniy Ivanov /*
63*58a2b000SEvgeniy Ivanov  * sends an IP packet, if it's alredy constructed
64*58a2b000SEvgeniy Ivanov */
65*58a2b000SEvgeniy Ivanov static ssize_t
_sendip(struct iodesc * d,struct ip * ip,size_t len)66*58a2b000SEvgeniy Ivanov _sendip(struct iodesc * d, struct ip * ip, size_t len)
67*58a2b000SEvgeniy Ivanov {
68*58a2b000SEvgeniy Ivanov 	u_char *ea;
69*58a2b000SEvgeniy Ivanov 
70*58a2b000SEvgeniy Ivanov 	if (ip->ip_dst.s_addr == INADDR_BROADCAST || ip->ip_src.s_addr == 0 ||
71*58a2b000SEvgeniy Ivanov 	    netmask == 0 || SAMENET(ip->ip_src, ip->ip_dst, netmask)) {
72*58a2b000SEvgeniy Ivanov 		ea = arpwhohas(d, ip->ip_dst);
73*58a2b000SEvgeniy Ivanov 	} else {
74*58a2b000SEvgeniy Ivanov 		ea = arpwhohas(d, gateip);
75*58a2b000SEvgeniy Ivanov 	}
76*58a2b000SEvgeniy Ivanov 
77*58a2b000SEvgeniy Ivanov 	return sendether(d, ip, len, ea, ETHERTYPE_IP);
78*58a2b000SEvgeniy Ivanov }
79*58a2b000SEvgeniy Ivanov 
80*58a2b000SEvgeniy Ivanov /*
81*58a2b000SEvgeniy Ivanov  * fills out the IP header
82*58a2b000SEvgeniy Ivanov  * Caller must leave room for ethernet, ip and udp headers in front!
83*58a2b000SEvgeniy Ivanov */
84*58a2b000SEvgeniy Ivanov ssize_t
sendip(struct iodesc * d,void * pkt,size_t len,u_int8_t proto)85*58a2b000SEvgeniy Ivanov sendip(struct iodesc * d, void *pkt, size_t len, u_int8_t proto)
86*58a2b000SEvgeniy Ivanov {
87*58a2b000SEvgeniy Ivanov 	ssize_t cc;
88*58a2b000SEvgeniy Ivanov 	struct ip *ip;
89*58a2b000SEvgeniy Ivanov 
90*58a2b000SEvgeniy Ivanov 	ip = (struct ip *) pkt - 1;
91*58a2b000SEvgeniy Ivanov 	len += sizeof(*ip);
92*58a2b000SEvgeniy Ivanov 
93*58a2b000SEvgeniy Ivanov 	(void) memset(ip, 0, sizeof(*ip));
94*58a2b000SEvgeniy Ivanov 
95*58a2b000SEvgeniy Ivanov 	ip->ip_v = IPVERSION;
96*58a2b000SEvgeniy Ivanov 	ip->ip_hl = sizeof(*ip) >> 2;
97*58a2b000SEvgeniy Ivanov 	ip->ip_len = htons(len);
98*58a2b000SEvgeniy Ivanov 	ip->ip_p = proto;
99*58a2b000SEvgeniy Ivanov 	ip->ip_ttl = IPDEFTTL;
100*58a2b000SEvgeniy Ivanov 	ip->ip_src = d->myip;
101*58a2b000SEvgeniy Ivanov 	ip->ip_dst = d->destip;
102*58a2b000SEvgeniy Ivanov 	ip->ip_sum = ip_cksum(ip, sizeof(*ip));
103*58a2b000SEvgeniy Ivanov 
104*58a2b000SEvgeniy Ivanov 	cc = _sendip(d, ip, len);
105*58a2b000SEvgeniy Ivanov 
106*58a2b000SEvgeniy Ivanov 	if (cc == -1)
107*58a2b000SEvgeniy Ivanov 		return -1;
108*58a2b000SEvgeniy Ivanov 	if ((size_t) cc != len)
109*58a2b000SEvgeniy Ivanov 		panic("sendip: bad write (%zd != %zu)", cc, len);
110*58a2b000SEvgeniy Ivanov 	return (cc - (sizeof(*ip)));
111*58a2b000SEvgeniy Ivanov }
112*58a2b000SEvgeniy Ivanov 
113*58a2b000SEvgeniy Ivanov /*
114*58a2b000SEvgeniy Ivanov  * reads an IP packet
115*58a2b000SEvgeniy Ivanov  * WARNING: the old version stripped the IP options, if there were
116*58a2b000SEvgeniy Ivanov  * any. Because we have absolutely no idea if the upper layer needs
117*58a2b000SEvgeniy Ivanov  * these or not, it's best to leave them there.
118*58a2b000SEvgeniy Ivanov  *
119*58a2b000SEvgeniy Ivanov  * The size returned is the size indicated in the header.
120*58a2b000SEvgeniy Ivanov  */
121*58a2b000SEvgeniy Ivanov ssize_t
readip(struct iodesc * d,void * pkt,size_t len,time_t tleft,u_int8_t proto)122*58a2b000SEvgeniy Ivanov readip(struct iodesc * d, void *pkt, size_t len, time_t tleft, u_int8_t proto)
123*58a2b000SEvgeniy Ivanov {
124*58a2b000SEvgeniy Ivanov 	ssize_t n;
125*58a2b000SEvgeniy Ivanov 	size_t hlen;
126*58a2b000SEvgeniy Ivanov 	struct ip *ip;
127*58a2b000SEvgeniy Ivanov 	u_int16_t etype;
128*58a2b000SEvgeniy Ivanov 
129*58a2b000SEvgeniy Ivanov 	ip = (struct ip *) pkt - 1;
130*58a2b000SEvgeniy Ivanov 
131*58a2b000SEvgeniy Ivanov 	n = readether(d, ip, len + sizeof(*ip), tleft, &etype);
132*58a2b000SEvgeniy Ivanov 	if (n == -1 || (size_t) n < sizeof(*ip))
133*58a2b000SEvgeniy Ivanov 		return -1;
134*58a2b000SEvgeniy Ivanov 
135*58a2b000SEvgeniy Ivanov 	if (etype == ETHERTYPE_ARP) {
136*58a2b000SEvgeniy Ivanov 		struct arphdr *ah = (void *) ip;
137*58a2b000SEvgeniy Ivanov 		if (ah->ar_op == htons(ARPOP_REQUEST)) {
138*58a2b000SEvgeniy Ivanov 			/* Send ARP reply */
139*58a2b000SEvgeniy Ivanov 			arp_reply(d, ah);
140*58a2b000SEvgeniy Ivanov 		}
141*58a2b000SEvgeniy Ivanov 		return -1;
142*58a2b000SEvgeniy Ivanov 	}
143*58a2b000SEvgeniy Ivanov 
144*58a2b000SEvgeniy Ivanov 	if (etype != ETHERTYPE_IP) {
145*58a2b000SEvgeniy Ivanov #ifdef NET_DEBUG
146*58a2b000SEvgeniy Ivanov 		if (debug)
147*58a2b000SEvgeniy Ivanov 			printf("readip: not IP. ether_type=%x\n", etype);
148*58a2b000SEvgeniy Ivanov #endif
149*58a2b000SEvgeniy Ivanov 		return -1;
150*58a2b000SEvgeniy Ivanov 	}
151*58a2b000SEvgeniy Ivanov 
152*58a2b000SEvgeniy Ivanov 	/* Check ip header */
153*58a2b000SEvgeniy Ivanov 	if (ip->ip_v != IPVERSION ||
154*58a2b000SEvgeniy Ivanov 	    ip->ip_p != proto) { /* half char */
155*58a2b000SEvgeniy Ivanov #ifdef NET_DEBUG
156*58a2b000SEvgeniy Ivanov 		if (debug) {
157*58a2b000SEvgeniy Ivanov 			printf("readip: wrong IP version or wrong proto "
158*58a2b000SEvgeniy Ivanov 			        "ip_v=%d ip_p=%d\n", ip->ip_v, ip->ip_p);
159*58a2b000SEvgeniy Ivanov 		}
160*58a2b000SEvgeniy Ivanov #endif
161*58a2b000SEvgeniy Ivanov 		return -1;
162*58a2b000SEvgeniy Ivanov 	}
163*58a2b000SEvgeniy Ivanov 
164*58a2b000SEvgeniy Ivanov 	hlen = ip->ip_hl << 2;
165*58a2b000SEvgeniy Ivanov 	if (hlen < sizeof(*ip) || ip_cksum(ip, hlen) != 0) {
166*58a2b000SEvgeniy Ivanov #ifdef NET_DEBUG
167*58a2b000SEvgeniy Ivanov 		if (debug)
168*58a2b000SEvgeniy Ivanov 			printf("readip: short hdr or bad cksum.\n");
169*58a2b000SEvgeniy Ivanov #endif
170*58a2b000SEvgeniy Ivanov 		return -1;
171*58a2b000SEvgeniy Ivanov 	}
172*58a2b000SEvgeniy Ivanov 	if (n < ntohs(ip->ip_len)) {
173*58a2b000SEvgeniy Ivanov #ifdef NET_DEBUG
174*58a2b000SEvgeniy Ivanov 		if (debug)
175*58a2b000SEvgeniy Ivanov 			printf("readip: bad length %d < %d.\n",
176*58a2b000SEvgeniy Ivanov 			       (int) n, ntohs(ip->ip_len));
177*58a2b000SEvgeniy Ivanov #endif
178*58a2b000SEvgeniy Ivanov 		return -1;
179*58a2b000SEvgeniy Ivanov 	}
180*58a2b000SEvgeniy Ivanov 	if (d->myip.s_addr && ip->ip_dst.s_addr != d->myip.s_addr) {
181*58a2b000SEvgeniy Ivanov #ifdef NET_DEBUG
182*58a2b000SEvgeniy Ivanov 		if (debug) {
183*58a2b000SEvgeniy Ivanov 			printf("readip: bad saddr %s != ", inet_ntoa(d->myip));
184*58a2b000SEvgeniy Ivanov 			printf("%s\n", inet_ntoa(ip->ip_dst));
185*58a2b000SEvgeniy Ivanov 		}
186*58a2b000SEvgeniy Ivanov #endif
187*58a2b000SEvgeniy Ivanov 		return -1;
188*58a2b000SEvgeniy Ivanov 	}
189*58a2b000SEvgeniy Ivanov 	return (ntohs(ip->ip_len) - 20);
190*58a2b000SEvgeniy Ivanov }
191