4 * ip.c (C) 1995-1998 Darren Reed
6 * See the IPFILTER.LICENCE file for details on licencing.
9 static const char sccsid[] = "%W% %G% (C)1995";
10 static const char rcsid[] = "@(#)$Id$";
12 #include <sys/param.h>
13 #include <sys/types.h>
14 #include <netinet/in_systm.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <netinet/ip.h>
19 #include <sys/param.h>
20 # include <net/route.h>
21 # include <netinet/if_ether.h>
22 # include <netinet/ip_var.h>
31 static char *ipbuf = NULL, *ethbuf = NULL;
34 u_short chksum(buf,len)
39 int nwords = len >> 1;
41 for(; nwords > 0; nwords--)
43 sum = (sum>>16) + (sum & 0xffff);
49 int send_ether(nfd, buf, len, gwip)
54 static struct in_addr last_gw;
55 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
61 ethbuf = (char *)calloc(1, 65536+1024);
63 eh = (ether_header_t *)s;
65 bcopy((char *)buf, s + sizeof(*eh), len);
66 if (gwip.s_addr == last_gw.s_addr)
68 bcopy(last_arp, (char *) &eh->ether_dhost, 6);
70 else if (arp((char *)&gwip, (char *) &eh->ether_dhost) == -1)
75 eh->ether_type = htons(ETHERTYPE_IP);
76 last_gw.s_addr = gwip.s_addr;
77 err = sendip(nfd, s, sizeof(*eh) + len);
84 int send_ip(nfd, mtu, ip, gwip, frag)
90 static struct in_addr last_gw, local_ip;
91 static char local_arp[6] = { 0, 0, 0, 0, 0, 0};
92 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
93 static u_short id = 0;
100 ipbuf = (char *)malloc(65536);
103 perror("malloc failed");
108 eh = (ether_header_t *)ipbuf;
110 bzero((char *) &eh->ether_shost, sizeof(eh->ether_shost));
111 if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr))
113 bcopy(last_arp, (char *) &eh->ether_dhost, 6);
115 else if (arp((char *)&gwip, (char *) &eh->ether_dhost) == -1)
120 bcopy((char *) &eh->ether_dhost, last_arp, sizeof(last_arp));
121 eh->ether_type = htons(ETHERTYPE_IP);
123 bcopy((char *)ip, (char *)&ipsv, sizeof(*ip));
124 last_gw.s_addr = gwip.s_addr;
126 ip->ip_len = htons(iplen);
129 IP_V_A(ip, IPVERSION);
131 ip->ip_id = htons(id++);
136 if (ip->ip_src.s_addr != local_ip.s_addr) {
137 (void) arp((char *)&ip->ip_src, (char *) &local_arp);
138 bcopy(local_arp, (char *) &eh->ether_shost,sizeof(last_arp));
139 local_ip = ip->ip_src;
141 bcopy(local_arp, (char *) &eh->ether_shost, 6);
143 if (!frag || (sizeof(*eh) + iplen < mtu))
146 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
148 bcopy((char *)ip, ipbuf + sizeof(*eh), iplen);
149 err = sendip(nfd, ipbuf, sizeof(*eh) + iplen);
154 * Actually, this is bogus because we're putting all IP
155 * options in every packet, which isn't always what should be
156 * done. Will do for now.
161 int i, sent = 0, ts, hlen, olen;
163 hlen = IP_HL(ip) << 2;
164 if (mtu < (hlen + 8)) {
165 fprintf(stderr, "mtu (%d) < ip header size (%d) + 8\n",
167 fprintf(stderr, "can't fragment data\n");
170 ol = (IP_HL(ip) << 2) - sizeof(*ip);
171 for (i = 0, s = (char*)(ip + 1); ol > 0; )
172 if (*s == IPOPT_EOL) {
175 } else if (*s == IPOPT_NOP) {
180 olen = (int)(*(u_char *)(s + 1));
182 if (IPOPT_COPIED(*s))
184 bcopy(s, optcpy + i, olen);
194 while ((i & 3) && (i & 3) != 3)
195 optcpy[i++] = IPOPT_NOP;
197 optcpy[i++] = IPOPT_EOL;
200 bcopy((char *)eh, (char *)ð, sizeof(eth));
201 s = (char *)ip + hlen;
202 iplen = ntohs(ip->ip_len) - hlen;
203 ip->ip_off |= htons(IP_MF);
207 if ((sent + (mtu - hlen)) >= iplen)
209 ip->ip_off ^= htons(IP_MF);
214 ip->ip_off &= htons(0xe000);
215 ip->ip_off |= htons(sent >> 3);
217 ip->ip_len = htons(ts);
219 ip->ip_sum = chksum((u_short *)ip, hlen);
220 bcopy((char *)ip, ipbuf + sizeof(*eh), hlen);
221 bcopy(s + sent, ipbuf + sizeof(*eh) + hlen, ts - hlen);
222 err = sendip(nfd, ipbuf, sizeof(*eh) + ts);
224 bcopy((char *)ð, ipbuf, sizeof(eth));
226 if (!(ntohs(ip->ip_off) & IP_MF))
228 else if (!(ip->ip_off & htons(0x1fff)))
230 hlen = i + sizeof(*ip);
231 IP_HL_A(ip, (sizeof(*ip) + i) >> 2);
232 bcopy(optcpy, (char *)(ip + 1), i);
237 bcopy((char *)&ipsv, (char *)ip, sizeof(*ip));
245 int send_tcp(nfd, mtu, ip, gwip)
250 static tcp_seq iss = 2;
252 int thlen, i, iplen, hlen;
257 hlen = IP_HL(ip) << 2;
258 t = (tcphdr_t *)((char *)ip + hlen);
259 ip2 = (struct ip *)lbuf;
260 t2 = (tcphdr_t *)((char *)ip2 + hlen);
261 thlen = TCP_OFF(t) << 2;
263 thlen = sizeof(tcphdr_t);
264 bzero((char *)ip2, sizeof(*ip2) + sizeof(*t2));
265 ip->ip_p = IPPROTO_TCP;
266 ip2->ip_p = ip->ip_p;
267 ip2->ip_src = ip->ip_src;
268 ip2->ip_dst = ip->ip_dst;
269 bcopy((char *)ip + hlen, (char *)t2, thlen);
272 t2->th_win = htons(4096);
275 i = sizeof(struct tcpiphdr) / sizeof(long);
277 if ((t2->th_flags == TH_SYN) && !ntohs(ip->ip_off) &&
278 (lbuf[i] != htonl(0x020405b4))) {
279 lbuf[i] = htonl(0x020405b4);
280 bcopy((char *)ip + hlen + thlen, (char *)ip + hlen + thlen + 4,
281 iplen - thlen - hlen);
284 TCP_OFF_A(t2, thlen >> 2);
285 ip2->ip_len = htons(thlen);
286 ip->ip_len = hlen + thlen;
288 t2->th_sum = chksum((u_short *)ip2, thlen + sizeof(ip_t));
290 bcopy((char *)t2, (char *)ip + hlen, thlen);
291 return send_ip(nfd, mtu, ip, gwip, 1);
298 int send_udp(nfd, mtu, ip, gwip)
307 ti = (struct tcpiphdr *)lbuf;
308 bzero((char *)ti, sizeof(*ti));
309 thlen = sizeof(udphdr_t);
310 ti->ti_pr = ip->ip_p;
311 ti->ti_src = ip->ip_src;
312 ti->ti_dst = ip->ip_dst;
313 bcopy((char *)ip + (IP_HL(ip) << 2),
314 (char *)&ti->ti_sport, sizeof(udphdr_t));
316 ti->ti_len = htons(thlen);
317 ip->ip_len = (IP_HL(ip) << 2) + thlen;
319 ti->ti_sum = chksum((u_short *)ti, thlen + sizeof(ip_t));
321 bcopy((char *)&ti->ti_sport,
322 (char *)ip + (IP_HL(ip) << 2), sizeof(udphdr_t));
323 return send_ip(nfd, mtu, ip, gwip, 1);
328 * send an icmp packet.
330 int send_icmp(nfd, mtu, ip, gwip)
337 ic = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
340 ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp));
342 return send_ip(nfd, mtu, ip, gwip, 1);
346 int send_packet(nfd, mtu, ip, gwip)
354 return send_tcp(nfd, mtu, ip, gwip);
356 return send_udp(nfd, mtu, ip, gwip);
358 return send_icmp(nfd, mtu, ip, gwip);
360 return send_ip(nfd, mtu, ip, gwip, 1);