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>
21 # include <net/route.h>
22 # include <netinet/if_ether.h>
23 # include <netinet/ip_var.h>
33 static char *ipbuf = NULL, *ethbuf = NULL;
36 u_short chksum(buf,len)
41 int nwords = len >> 1;
43 for(; nwords > 0; nwords--)
45 sum = (sum>>16) + (sum & 0xffff);
51 int send_ether(nfd, buf, len, gwip)
56 static struct in_addr last_gw;
57 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
63 ethbuf = (char *)calloc(1, 65536+1024);
65 eh = (ether_header_t *)s;
67 bcopy((char *)buf, s + sizeof(*eh), len);
68 if (gwip.s_addr == last_gw.s_addr)
70 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
72 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
77 eh->ether_type = htons(ETHERTYPE_IP);
78 last_gw.s_addr = gwip.s_addr;
79 err = sendip(nfd, s, sizeof(*eh) + len);
86 int send_ip(nfd, mtu, ip, gwip, frag)
92 static struct in_addr last_gw, local_ip;
93 static char local_arp[6] = { 0, 0, 0, 0, 0, 0};
94 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
95 static u_short id = 0;
102 ipbuf = (char *)malloc(65536);
105 perror("malloc failed");
110 eh = (ether_header_t *)ipbuf;
112 bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost));
113 if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr))
115 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
117 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
122 bcopy((char *)A_A eh->ether_dhost, last_arp, sizeof(last_arp));
123 eh->ether_type = htons(ETHERTYPE_IP);
125 bcopy((char *)ip, (char *)&ipsv, sizeof(*ip));
126 last_gw.s_addr = gwip.s_addr;
128 ip->ip_len = htons(iplen);
131 IP_V_A(ip, IPVERSION);
133 ip->ip_id = htons(id++);
138 if (ip->ip_src.s_addr != local_ip.s_addr) {
139 (void) arp((char *)&ip->ip_src, (char *)A_A local_arp);
140 bcopy(local_arp, (char *)A_A eh->ether_shost,sizeof(last_arp));
141 local_ip = ip->ip_src;
143 bcopy(local_arp, (char *)A_A eh->ether_shost, 6);
145 if (!frag || (sizeof(*eh) + iplen < mtu))
148 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
150 bcopy((char *)ip, ipbuf + sizeof(*eh), iplen);
151 err = sendip(nfd, ipbuf, sizeof(*eh) + iplen);
156 * Actually, this is bogus because we're putting all IP
157 * options in every packet, which isn't always what should be
158 * done. Will do for now.
163 int i, sent = 0, ts, hlen, olen;
165 hlen = IP_HL(ip) << 2;
166 if (mtu < (hlen + 8)) {
167 fprintf(stderr, "mtu (%d) < ip header size (%d) + 8\n",
169 fprintf(stderr, "can't fragment data\n");
172 ol = (IP_HL(ip) << 2) - sizeof(*ip);
173 for (i = 0, s = (char*)(ip + 1); ol > 0; )
174 if (*s == IPOPT_EOL) {
177 } else if (*s == IPOPT_NOP) {
182 olen = (int)(*(u_char *)(s + 1));
184 if (IPOPT_COPIED(*s))
186 bcopy(s, optcpy + i, olen);
196 while ((i & 3) && (i & 3) != 3)
197 optcpy[i++] = IPOPT_NOP;
199 optcpy[i++] = IPOPT_EOL;
202 bcopy((char *)eh, (char *)ð, sizeof(eth));
203 s = (char *)ip + hlen;
204 iplen = ntohs(ip->ip_len) - hlen;
205 ip->ip_off |= htons(IP_MF);
209 if ((sent + (mtu - hlen)) >= iplen)
211 ip->ip_off ^= htons(IP_MF);
216 ip->ip_off &= htons(0xe000);
217 ip->ip_off |= htons(sent >> 3);
219 ip->ip_len = htons(ts);
221 ip->ip_sum = chksum((u_short *)ip, hlen);
222 bcopy((char *)ip, ipbuf + sizeof(*eh), hlen);
223 bcopy(s + sent, ipbuf + sizeof(*eh) + hlen, ts - hlen);
224 err = sendip(nfd, ipbuf, sizeof(*eh) + ts);
226 bcopy((char *)ð, ipbuf, sizeof(eth));
228 if (!(ntohs(ip->ip_off) & IP_MF))
230 else if (!(ip->ip_off & htons(0x1fff)))
232 hlen = i + sizeof(*ip);
233 IP_HL_A(ip, (sizeof(*ip) + i) >> 2);
234 bcopy(optcpy, (char *)(ip + 1), i);
239 bcopy((char *)&ipsv, (char *)ip, sizeof(*ip));
247 int send_tcp(nfd, mtu, ip, gwip)
252 static tcp_seq iss = 2;
254 int thlen, i, iplen, hlen;
259 hlen = IP_HL(ip) << 2;
260 t = (tcphdr_t *)((char *)ip + hlen);
261 ip2 = (struct ip *)lbuf;
262 t2 = (tcphdr_t *)((char *)ip2 + hlen);
263 thlen = TCP_OFF(t) << 2;
265 thlen = sizeof(tcphdr_t);
266 bzero((char *)ip2, sizeof(*ip2) + sizeof(*t2));
267 ip->ip_p = IPPROTO_TCP;
268 ip2->ip_p = ip->ip_p;
269 ip2->ip_src = ip->ip_src;
270 ip2->ip_dst = ip->ip_dst;
271 bcopy((char *)ip + hlen, (char *)t2, thlen);
274 t2->th_win = htons(4096);
277 i = sizeof(struct tcpiphdr) / sizeof(long);
279 if ((t2->th_flags == TH_SYN) && !ntohs(ip->ip_off) &&
280 (lbuf[i] != htonl(0x020405b4))) {
281 lbuf[i] = htonl(0x020405b4);
282 bcopy((char *)ip + hlen + thlen, (char *)ip + hlen + thlen + 4,
283 iplen - thlen - hlen);
286 TCP_OFF_A(t2, thlen >> 2);
287 ip2->ip_len = htons(thlen);
288 ip->ip_len = hlen + thlen;
290 t2->th_sum = chksum((u_short *)ip2, thlen + sizeof(ip_t));
292 bcopy((char *)t2, (char *)ip + hlen, thlen);
293 return send_ip(nfd, mtu, ip, gwip, 1);
300 int send_udp(nfd, mtu, ip, gwip)
309 ti = (struct tcpiphdr *)lbuf;
310 bzero((char *)ti, sizeof(*ti));
311 thlen = sizeof(udphdr_t);
312 ti->ti_pr = ip->ip_p;
313 ti->ti_src = ip->ip_src;
314 ti->ti_dst = ip->ip_dst;
315 bcopy((char *)ip + (IP_HL(ip) << 2),
316 (char *)&ti->ti_sport, sizeof(udphdr_t));
318 ti->ti_len = htons(thlen);
319 ip->ip_len = (IP_HL(ip) << 2) + thlen;
321 ti->ti_sum = chksum((u_short *)ti, thlen + sizeof(ip_t));
323 bcopy((char *)&ti->ti_sport,
324 (char *)ip + (IP_HL(ip) << 2), sizeof(udphdr_t));
325 return send_ip(nfd, mtu, ip, gwip, 1);
330 * send an icmp packet.
332 int send_icmp(nfd, mtu, ip, gwip)
339 ic = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
342 ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp));
344 return send_ip(nfd, mtu, ip, gwip, 1);
348 int send_packet(nfd, mtu, ip, gwip)
356 return send_tcp(nfd, mtu, ip, gwip);
358 return send_udp(nfd, mtu, ip, gwip);
360 return send_icmp(nfd, mtu, ip, gwip);
362 return send_ip(nfd, mtu, ip, gwip, 1);