1 /*	$FreeBSD: stable/9/contrib/ipfilter/ipsend/ip.c 170268 2007-06-04 02:54:36Z darrenr $	*/
2 
3 /*
4  * ip.c (C) 1995-1998 Darren Reed
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  */
8 #if !defined(lint)
9 static const char sccsid[] = "%W% %G% (C)1995";
10 static const char rcsid[] = "@(#)$Id: ip.c,v 2.8.2.2 2007/02/17 12:41:51 darrenr Exp $";
11 #endif
12 #include <sys/param.h>
13 #include <sys/types.h>
14 #include <netinet/in_systm.h>
15 #include <sys/socket.h>
16 #ifdef __osf__
17 # include "radix_ipf_local.h"
18 #endif
19 #include <net/if.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
22 #include <sys/param.h>
23 #ifndef	linux
24 # include <netinet/if_ether.h>
25 # include <netinet/ip_var.h>
26 # if __FreeBSD_version >= 300000
27 #  include <net/if_var.h>
28 # endif
29 #endif
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include "ipsend.h"
36 
37 
38 static	char	*ipbuf = NULL, *ethbuf = NULL;
39 
40 
chksum(buf,len)41 u_short	chksum(buf,len)
42 u_short	*buf;
43 int	len;
44 {
45 	u_long	sum = 0;
46 	int	nwords = len >> 1;
47 
48 	for(; nwords > 0; nwords--)
49 		sum += *buf++;
50 	sum = (sum>>16) + (sum & 0xffff);
51 	sum += (sum >>16);
52 	return (~sum);
53 }
54 
55 
send_ether(nfd,buf,len,gwip)56 int	send_ether(nfd, buf, len, gwip)
57 int	nfd, len;
58 char	*buf;
59 struct	in_addr	gwip;
60 {
61 	static	struct	in_addr	last_gw;
62 	static	char	last_arp[6] = { 0, 0, 0, 0, 0, 0};
63 	ether_header_t	*eh;
64 	char	*s;
65 	int	err;
66 
67 	if (!ethbuf)
68 		ethbuf = (char *)calloc(1, 65536+1024);
69 	s = ethbuf;
70 	eh = (ether_header_t *)s;
71 
72 	bcopy((char *)buf, s + sizeof(*eh), len);
73 	if (gwip.s_addr == last_gw.s_addr)
74 	    {
75 		bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
76 	    }
77 	else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
78 	    {
79 		perror("arp");
80 		return -2;
81 	    }
82 	eh->ether_type = htons(ETHERTYPE_IP);
83 	last_gw.s_addr = gwip.s_addr;
84 	err = sendip(nfd, s, sizeof(*eh) + len);
85 	return err;
86 }
87 
88 
89 /*
90  */
send_ip(nfd,mtu,ip,gwip,frag)91 int	send_ip(nfd, mtu, ip, gwip, frag)
92 int	nfd, mtu;
93 ip_t	*ip;
94 struct	in_addr	gwip;
95 int	frag;
96 {
97 	static	struct	in_addr	last_gw, local_ip;
98 	static	char	local_arp[6] = { 0, 0, 0, 0, 0, 0};
99 	static	char	last_arp[6] = { 0, 0, 0, 0, 0, 0};
100 	static	u_short	id = 0;
101 	ether_header_t	*eh;
102 	ip_t	ipsv;
103 	int	err, iplen;
104 
105 	if (!ipbuf)
106 	  {
107 		ipbuf = (char *)malloc(65536);
108 		if (!ipbuf)
109 		  {
110 			perror("malloc failed");
111 			return -2;
112 		  }
113 	  }
114 
115 	eh = (ether_header_t *)ipbuf;
116 
117 	bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost));
118 	if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr))
119 	    {
120 		bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
121 	    }
122 	else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
123 	    {
124 		perror("arp");
125 		return -2;
126 	    }
127 	bcopy((char *)A_A eh->ether_dhost, last_arp, sizeof(last_arp));
128 	eh->ether_type = htons(ETHERTYPE_IP);
129 
130 	bcopy((char *)ip, (char *)&ipsv, sizeof(*ip));
131 	last_gw.s_addr = gwip.s_addr;
132 	iplen = ip->ip_len;
133 	ip->ip_len = htons(iplen);
134 	if (!(frag & 2)) {
135 		if (!IP_V(ip))
136 			IP_V_A(ip, IPVERSION);
137 		if (!ip->ip_id)
138 			ip->ip_id  = htons(id++);
139 		if (!ip->ip_ttl)
140 			ip->ip_ttl = 60;
141 	}
142 
143 	if (ip->ip_src.s_addr != local_ip.s_addr) {
144 		(void) arp((char *)&ip->ip_src, (char *)A_A local_arp);
145 		bcopy(local_arp, (char *)A_A eh->ether_shost,sizeof(last_arp));
146 		local_ip = ip->ip_src;
147 	} else
148 		bcopy(local_arp, (char *)A_A eh->ether_shost, 6);
149 
150 	if (!frag || (sizeof(*eh) + iplen < mtu))
151 	    {
152 		ip->ip_sum = 0;
153 		ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
154 
155 		bcopy((char *)ip, ipbuf + sizeof(*eh), iplen);
156 		err =  sendip(nfd, ipbuf, sizeof(*eh) + iplen);
157 	    }
158 	else
159 	    {
160 		/*
161 		 * Actually, this is bogus because we're putting all IP
162 		 * options in every packet, which isn't always what should be
163 		 * done.  Will do for now.
164 		 */
165 		ether_header_t	eth;
166 		char	optcpy[48], ol;
167 		char	*s;
168 		int	i, sent = 0, ts, hlen, olen;
169 
170 		hlen = IP_HL(ip) << 2;
171 		if (mtu < (hlen + 8)) {
172 			fprintf(stderr, "mtu (%d) < ip header size (%d) + 8\n",
173 				mtu, hlen);
174 			fprintf(stderr, "can't fragment data\n");
175 			return -2;
176 		}
177 		ol = (IP_HL(ip) << 2) - sizeof(*ip);
178 		for (i = 0, s = (char*)(ip + 1); ol > 0; )
179 			if (*s == IPOPT_EOL) {
180 				optcpy[i++] = *s;
181 				break;
182 			} else if (*s == IPOPT_NOP) {
183 				s++;
184 				ol--;
185 			} else
186 			    {
187 				olen = (int)(*(u_char *)(s + 1));
188 				ol -= olen;
189 				if (IPOPT_COPIED(*s))
190 				    {
191 					bcopy(s, optcpy + i, olen);
192 					i += olen;
193 					s += olen;
194 				    }
195 			    }
196 		if (i)
197 		    {
198 			/*
199 			 * pad out
200 			 */
201 			while ((i & 3) && (i & 3) != 3)
202 				optcpy[i++] = IPOPT_NOP;
203 			if ((i & 3) == 3)
204 				optcpy[i++] = IPOPT_EOL;
205 		    }
206 
207 		bcopy((char *)eh, (char *)&eth, sizeof(eth));
208 		s = (char *)ip + hlen;
209 		iplen = ntohs(ip->ip_len) - hlen;
210 		ip->ip_off |= htons(IP_MF);
211 
212 		while (1)
213 		    {
214 			if ((sent + (mtu - hlen)) >= iplen)
215 			    {
216 				ip->ip_off ^= htons(IP_MF);
217 				ts = iplen - sent;
218 			    }
219 			else
220 				ts = (mtu - hlen);
221 			ip->ip_off &= htons(0xe000);
222 			ip->ip_off |= htons(sent >> 3);
223 			ts += hlen;
224 			ip->ip_len = htons(ts);
225 			ip->ip_sum = 0;
226 			ip->ip_sum = chksum((u_short *)ip, hlen);
227 			bcopy((char *)ip, ipbuf + sizeof(*eh), hlen);
228 			bcopy(s + sent, ipbuf + sizeof(*eh) + hlen, ts - hlen);
229 			err =  sendip(nfd, ipbuf, sizeof(*eh) + ts);
230 
231 			bcopy((char *)&eth, ipbuf, sizeof(eth));
232 			sent += (ts - hlen);
233 			if (!(ntohs(ip->ip_off) & IP_MF))
234 				break;
235 			else if (!(ip->ip_off & htons(0x1fff)))
236 			    {
237 				hlen = i + sizeof(*ip);
238 				IP_HL_A(ip, (sizeof(*ip) + i) >> 2);
239 				bcopy(optcpy, (char *)(ip + 1), i);
240 			    }
241 		    }
242 	    }
243 
244 	bcopy((char *)&ipsv, (char *)ip, sizeof(*ip));
245 	return err;
246 }
247 
248 
249 /*
250  * send a tcp packet.
251  */
send_tcp(nfd,mtu,ip,gwip)252 int	send_tcp(nfd, mtu, ip, gwip)
253 int	nfd, mtu;
254 ip_t	*ip;
255 struct	in_addr	gwip;
256 {
257 	static	tcp_seq	iss = 2;
258 	tcphdr_t *t, *t2;
259 	int	thlen, i, iplen, hlen;
260 	u_32_t	lbuf[20];
261 	ip_t	*ip2;
262 
263 	iplen = ip->ip_len;
264 	hlen = IP_HL(ip) << 2;
265 	t = (tcphdr_t *)((char *)ip + hlen);
266 	ip2 = (struct ip *)lbuf;
267 	t2 = (tcphdr_t *)((char *)ip2 + hlen);
268 	thlen = TCP_OFF(t) << 2;
269 	if (!thlen)
270 		thlen = sizeof(tcphdr_t);
271 	bzero((char *)ip2, sizeof(*ip2) + sizeof(*t2));
272 	ip->ip_p = IPPROTO_TCP;
273 	ip2->ip_p = ip->ip_p;
274 	ip2->ip_src = ip->ip_src;
275 	ip2->ip_dst = ip->ip_dst;
276 	bcopy((char *)ip + hlen, (char *)t2, thlen);
277 
278 	if (!t2->th_win)
279 		t2->th_win = htons(4096);
280 	iss += 63;
281 
282 	i = sizeof(struct tcpiphdr) / sizeof(long);
283 
284 	if ((t2->th_flags == TH_SYN) && !ntohs(ip->ip_off) &&
285 	    (lbuf[i] != htonl(0x020405b4))) {
286 		lbuf[i] = htonl(0x020405b4);
287 		bcopy((char *)ip + hlen + thlen, (char *)ip + hlen + thlen + 4,
288 		      iplen - thlen - hlen);
289 		thlen += 4;
290 	    }
291 	TCP_OFF_A(t2, thlen >> 2);
292 	ip2->ip_len = htons(thlen);
293 	ip->ip_len = hlen + thlen;
294 	t2->th_sum = 0;
295 	t2->th_sum = chksum((u_short *)ip2, thlen + sizeof(ip_t));
296 
297 	bcopy((char *)t2, (char *)ip + hlen, thlen);
298 	return send_ip(nfd, mtu, ip, gwip, 1);
299 }
300 
301 
302 /*
303  * send a udp packet.
304  */
send_udp(nfd,mtu,ip,gwip)305 int	send_udp(nfd, mtu, ip, gwip)
306 int	nfd, mtu;
307 ip_t	*ip;
308 struct	in_addr	gwip;
309 {
310 	struct	tcpiphdr *ti;
311 	int	thlen;
312 	u_long	lbuf[20];
313 
314 	ti = (struct tcpiphdr *)lbuf;
315 	bzero((char *)ti, sizeof(*ti));
316 	thlen = sizeof(udphdr_t);
317 	ti->ti_pr = ip->ip_p;
318 	ti->ti_src = ip->ip_src;
319 	ti->ti_dst = ip->ip_dst;
320 	bcopy((char *)ip + (IP_HL(ip) << 2),
321 	      (char *)&ti->ti_sport, sizeof(udphdr_t));
322 
323 	ti->ti_len = htons(thlen);
324 	ip->ip_len = (IP_HL(ip) << 2) + thlen;
325 	ti->ti_sum = 0;
326 	ti->ti_sum = chksum((u_short *)ti, thlen + sizeof(ip_t));
327 
328 	bcopy((char *)&ti->ti_sport,
329 	      (char *)ip + (IP_HL(ip) << 2), sizeof(udphdr_t));
330 	return send_ip(nfd, mtu, ip, gwip, 1);
331 }
332 
333 
334 /*
335  * send an icmp packet.
336  */
send_icmp(nfd,mtu,ip,gwip)337 int	send_icmp(nfd, mtu, ip, gwip)
338 int	nfd, mtu;
339 ip_t	*ip;
340 struct	in_addr	gwip;
341 {
342 	struct	icmp	*ic;
343 
344 	ic = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
345 
346 	ic->icmp_cksum = 0;
347 	ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp));
348 
349 	return send_ip(nfd, mtu, ip, gwip, 1);
350 }
351 
352 
send_packet(nfd,mtu,ip,gwip)353 int	send_packet(nfd, mtu, ip, gwip)
354 int	nfd, mtu;
355 ip_t	*ip;
356 struct	in_addr	gwip;
357 {
358         switch (ip->ip_p)
359         {
360         case IPPROTO_TCP :
361                 return send_tcp(nfd, mtu, ip, gwip);
362         case IPPROTO_UDP :
363                 return send_udp(nfd, mtu, ip, gwip);
364         case IPPROTO_ICMP :
365                 return send_icmp(nfd, mtu, ip, gwip);
366         default :
367                 return send_ip(nfd, mtu, ip, gwip, 1);
368         }
369 }
370