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