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: ip.c,v 2.8.2.1 2004/10/19 12:31:48 darrenr Exp $";
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 <netinet/if_ether.h>
22 # include <netinet/ip_var.h>
23 # if __FreeBSD_version >= 300000
24 # include <net/if_var.h>
35 static char *ipbuf = NULL, *ethbuf = NULL;
38 u_short chksum(buf,len)
43 int nwords = len >> 1;
45 for(; nwords > 0; nwords--)
47 sum = (sum>>16) + (sum & 0xffff);
53 int send_ether(nfd, buf, len, gwip)
58 static struct in_addr last_gw;
59 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
65 ethbuf = (char *)calloc(1, 65536+1024);
67 eh = (ether_header_t *)s;
69 bcopy((char *)buf, s + sizeof(*eh), len);
70 if (gwip.s_addr == last_gw.s_addr)
72 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
74 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
79 eh->ether_type = htons(ETHERTYPE_IP);
80 last_gw.s_addr = gwip.s_addr;
81 err = sendip(nfd, s, sizeof(*eh) + len);
88 int send_ip(nfd, mtu, ip, gwip, frag)
94 static struct in_addr last_gw, local_ip;
95 static char local_arp[6] = { 0, 0, 0, 0, 0, 0};
96 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
97 static u_short id = 0;
104 ipbuf = (char *)malloc(65536);
107 perror("malloc failed");
112 eh = (ether_header_t *)ipbuf;
114 bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost));
115 if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr))
117 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
119 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
124 bcopy((char *)A_A eh->ether_dhost, last_arp, sizeof(last_arp));
125 eh->ether_type = htons(ETHERTYPE_IP);
127 bcopy((char *)ip, (char *)&ipsv, sizeof(*ip));
128 last_gw.s_addr = gwip.s_addr;
130 ip->ip_len = htons(iplen);
133 IP_V_A(ip, IPVERSION);
135 ip->ip_id = htons(id++);
140 if (ip->ip_src.s_addr != local_ip.s_addr) {
141 (void) arp((char *)&ip->ip_src, (char *)A_A local_arp);
142 bcopy(local_arp, (char *)A_A eh->ether_shost,sizeof(last_arp));
143 local_ip = ip->ip_src;
145 bcopy(local_arp, (char *)A_A eh->ether_shost, 6);
147 if (!frag || (sizeof(*eh) + iplen < mtu))
150 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
152 bcopy((char *)ip, ipbuf + sizeof(*eh), iplen);
153 err = sendip(nfd, ipbuf, sizeof(*eh) + iplen);
158 * Actually, this is bogus because we're putting all IP
159 * options in every packet, which isn't always what should be
160 * done. Will do for now.
165 int i, sent = 0, ts, hlen, olen;
167 hlen = IP_HL(ip) << 2;
168 if (mtu < (hlen + 8)) {
169 fprintf(stderr, "mtu (%d) < ip header size (%d) + 8\n",
171 fprintf(stderr, "can't fragment data\n");
174 ol = (IP_HL(ip) << 2) - sizeof(*ip);
175 for (i = 0, s = (char*)(ip + 1); ol > 0; )
176 if (*s == IPOPT_EOL) {
179 } else if (*s == IPOPT_NOP) {
184 olen = (int)(*(u_char *)(s + 1));
186 if (IPOPT_COPIED(*s))
188 bcopy(s, optcpy + i, olen);
198 while ((i & 3) && (i & 3) != 3)
199 optcpy[i++] = IPOPT_NOP;
201 optcpy[i++] = IPOPT_EOL;
204 bcopy((char *)eh, (char *)ð, sizeof(eth));
205 s = (char *)ip + hlen;
206 iplen = ntohs(ip->ip_len) - hlen;
207 ip->ip_off |= htons(IP_MF);
211 if ((sent + (mtu - hlen)) >= iplen)
213 ip->ip_off ^= htons(IP_MF);
218 ip->ip_off &= htons(0xe000);
219 ip->ip_off |= htons(sent >> 3);
221 ip->ip_len = htons(ts);
223 ip->ip_sum = chksum((u_short *)ip, hlen);
224 bcopy((char *)ip, ipbuf + sizeof(*eh), hlen);
225 bcopy(s + sent, ipbuf + sizeof(*eh) + hlen, ts - hlen);
226 err = sendip(nfd, ipbuf, sizeof(*eh) + ts);
228 bcopy((char *)ð, ipbuf, sizeof(eth));
230 if (!(ntohs(ip->ip_off) & IP_MF))
232 else if (!(ip->ip_off & htons(0x1fff)))
234 hlen = i + sizeof(*ip);
235 IP_HL_A(ip, (sizeof(*ip) + i) >> 2);
236 bcopy(optcpy, (char *)(ip + 1), i);
241 bcopy((char *)&ipsv, (char *)ip, sizeof(*ip));
249 int send_tcp(nfd, mtu, ip, gwip)
254 static tcp_seq iss = 2;
256 int thlen, i, iplen, hlen;
261 hlen = IP_HL(ip) << 2;
262 t = (tcphdr_t *)((char *)ip + hlen);
263 ip2 = (struct ip *)lbuf;
264 t2 = (tcphdr_t *)((char *)ip2 + hlen);
265 thlen = TCP_OFF(t) << 2;
267 thlen = sizeof(tcphdr_t);
268 bzero((char *)ip2, sizeof(*ip2) + sizeof(*t2));
269 ip->ip_p = IPPROTO_TCP;
270 ip2->ip_p = ip->ip_p;
271 ip2->ip_src = ip->ip_src;
272 ip2->ip_dst = ip->ip_dst;
273 bcopy((char *)ip + hlen, (char *)t2, thlen);
276 t2->th_win = htons(4096);
279 i = sizeof(struct tcpiphdr) / sizeof(long);
281 if ((t2->th_flags == TH_SYN) && !ntohs(ip->ip_off) &&
282 (lbuf[i] != htonl(0x020405b4))) {
283 lbuf[i] = htonl(0x020405b4);
284 bcopy((char *)ip + hlen + thlen, (char *)ip + hlen + thlen + 4,
285 iplen - thlen - hlen);
288 TCP_OFF_A(t2, thlen >> 2);
289 ip2->ip_len = htons(thlen);
290 ip->ip_len = hlen + thlen;
292 t2->th_sum = chksum((u_short *)ip2, thlen + sizeof(ip_t));
294 bcopy((char *)t2, (char *)ip + hlen, thlen);
295 return send_ip(nfd, mtu, ip, gwip, 1);
302 int send_udp(nfd, mtu, ip, gwip)
311 ti = (struct tcpiphdr *)lbuf;
312 bzero((char *)ti, sizeof(*ti));
313 thlen = sizeof(udphdr_t);
314 ti->ti_pr = ip->ip_p;
315 ti->ti_src = ip->ip_src;
316 ti->ti_dst = ip->ip_dst;
317 bcopy((char *)ip + (IP_HL(ip) << 2),
318 (char *)&ti->ti_sport, sizeof(udphdr_t));
320 ti->ti_len = htons(thlen);
321 ip->ip_len = (IP_HL(ip) << 2) + thlen;
323 ti->ti_sum = chksum((u_short *)ti, thlen + sizeof(ip_t));
325 bcopy((char *)&ti->ti_sport,
326 (char *)ip + (IP_HL(ip) << 2), sizeof(udphdr_t));
327 return send_ip(nfd, mtu, ip, gwip, 1);
332 * send an icmp packet.
334 int send_icmp(nfd, mtu, ip, gwip)
341 ic = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
344 ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp));
346 return send_ip(nfd, mtu, ip, gwip, 1);
350 int send_packet(nfd, mtu, ip, gwip)
358 return send_tcp(nfd, mtu, ip, gwip);
360 return send_udp(nfd, mtu, ip, gwip);
362 return send_icmp(nfd, mtu, ip, gwip);
364 return send_ip(nfd, mtu, ip, gwip, 1);