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>
24 # if __FreeBSD_version >= 300000
25 # include <net/if_var.h>
36 static char *ipbuf = NULL, *ethbuf = NULL;
39 u_short chksum(buf,len)
44 int nwords = len >> 1;
46 for(; nwords > 0; nwords--)
48 sum = (sum>>16) + (sum & 0xffff);
54 int send_ether(nfd, buf, len, gwip)
59 static struct in_addr last_gw;
60 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
66 ethbuf = (char *)calloc(1, 65536+1024);
68 eh = (ether_header_t *)s;
70 bcopy((char *)buf, s + sizeof(*eh), len);
71 if (gwip.s_addr == last_gw.s_addr)
73 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
75 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
80 eh->ether_type = htons(ETHERTYPE_IP);
81 last_gw.s_addr = gwip.s_addr;
82 err = sendip(nfd, s, sizeof(*eh) + len);
89 int send_ip(nfd, mtu, ip, gwip, frag)
95 static struct in_addr last_gw, local_ip;
96 static char local_arp[6] = { 0, 0, 0, 0, 0, 0};
97 static char last_arp[6] = { 0, 0, 0, 0, 0, 0};
98 static u_short id = 0;
105 ipbuf = (char *)malloc(65536);
108 perror("malloc failed");
113 eh = (ether_header_t *)ipbuf;
115 bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost));
116 if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr))
118 bcopy(last_arp, (char *)A_A eh->ether_dhost, 6);
120 else if (arp((char *)&gwip, (char *)A_A eh->ether_dhost) == -1)
125 bcopy((char *)A_A eh->ether_dhost, last_arp, sizeof(last_arp));
126 eh->ether_type = htons(ETHERTYPE_IP);
128 bcopy((char *)ip, (char *)&ipsv, sizeof(*ip));
129 last_gw.s_addr = gwip.s_addr;
131 ip->ip_len = htons(iplen);
134 IP_V_A(ip, IPVERSION);
136 ip->ip_id = htons(id++);
141 if (ip->ip_src.s_addr != local_ip.s_addr) {
142 (void) arp((char *)&ip->ip_src, (char *)A_A local_arp);
143 bcopy(local_arp, (char *)A_A eh->ether_shost,sizeof(last_arp));
144 local_ip = ip->ip_src;
146 bcopy(local_arp, (char *)A_A eh->ether_shost, 6);
148 if (!frag || (sizeof(*eh) + iplen < mtu))
151 ip->ip_sum = chksum((u_short *)ip, IP_HL(ip) << 2);
153 bcopy((char *)ip, ipbuf + sizeof(*eh), iplen);
154 err = sendip(nfd, ipbuf, sizeof(*eh) + iplen);
159 * Actually, this is bogus because we're putting all IP
160 * options in every packet, which isn't always what should be
161 * done. Will do for now.
166 int i, sent = 0, ts, hlen, olen;
168 hlen = IP_HL(ip) << 2;
169 if (mtu < (hlen + 8)) {
170 fprintf(stderr, "mtu (%d) < ip header size (%d) + 8\n",
172 fprintf(stderr, "can't fragment data\n");
175 ol = (IP_HL(ip) << 2) - sizeof(*ip);
176 for (i = 0, s = (char*)(ip + 1); ol > 0; )
177 if (*s == IPOPT_EOL) {
180 } else if (*s == IPOPT_NOP) {
185 olen = (int)(*(u_char *)(s + 1));
187 if (IPOPT_COPIED(*s))
189 bcopy(s, optcpy + i, olen);
199 while ((i & 3) && (i & 3) != 3)
200 optcpy[i++] = IPOPT_NOP;
202 optcpy[i++] = IPOPT_EOL;
205 bcopy((char *)eh, (char *)ð, sizeof(eth));
206 s = (char *)ip + hlen;
207 iplen = ntohs(ip->ip_len) - hlen;
208 ip->ip_off |= htons(IP_MF);
212 if ((sent + (mtu - hlen)) >= iplen)
214 ip->ip_off ^= htons(IP_MF);
219 ip->ip_off &= htons(0xe000);
220 ip->ip_off |= htons(sent >> 3);
222 ip->ip_len = htons(ts);
224 ip->ip_sum = chksum((u_short *)ip, hlen);
225 bcopy((char *)ip, ipbuf + sizeof(*eh), hlen);
226 bcopy(s + sent, ipbuf + sizeof(*eh) + hlen, ts - hlen);
227 err = sendip(nfd, ipbuf, sizeof(*eh) + ts);
229 bcopy((char *)ð, ipbuf, sizeof(eth));
231 if (!(ntohs(ip->ip_off) & IP_MF))
233 else if (!(ip->ip_off & htons(0x1fff)))
235 hlen = i + sizeof(*ip);
236 IP_HL_A(ip, (sizeof(*ip) + i) >> 2);
237 bcopy(optcpy, (char *)(ip + 1), i);
242 bcopy((char *)&ipsv, (char *)ip, sizeof(*ip));
250 int send_tcp(nfd, mtu, ip, gwip)
255 static tcp_seq iss = 2;
257 int thlen, i, iplen, hlen;
262 hlen = IP_HL(ip) << 2;
263 t = (tcphdr_t *)((char *)ip + hlen);
264 ip2 = (struct ip *)lbuf;
265 t2 = (tcphdr_t *)((char *)ip2 + hlen);
266 thlen = TCP_OFF(t) << 2;
268 thlen = sizeof(tcphdr_t);
269 bzero((char *)ip2, sizeof(*ip2) + sizeof(*t2));
270 ip->ip_p = IPPROTO_TCP;
271 ip2->ip_p = ip->ip_p;
272 ip2->ip_src = ip->ip_src;
273 ip2->ip_dst = ip->ip_dst;
274 bcopy((char *)ip + hlen, (char *)t2, thlen);
277 t2->th_win = htons(4096);
280 i = sizeof(struct tcpiphdr) / sizeof(long);
282 if ((t2->th_flags == TH_SYN) && !ntohs(ip->ip_off) &&
283 (lbuf[i] != htonl(0x020405b4))) {
284 lbuf[i] = htonl(0x020405b4);
285 bcopy((char *)ip + hlen + thlen, (char *)ip + hlen + thlen + 4,
286 iplen - thlen - hlen);
289 TCP_OFF_A(t2, thlen >> 2);
290 ip2->ip_len = htons(thlen);
291 ip->ip_len = hlen + thlen;
293 t2->th_sum = chksum((u_short *)ip2, thlen + sizeof(ip_t));
295 bcopy((char *)t2, (char *)ip + hlen, thlen);
296 return send_ip(nfd, mtu, ip, gwip, 1);
303 int send_udp(nfd, mtu, ip, gwip)
312 ti = (struct tcpiphdr *)lbuf;
313 bzero((char *)ti, sizeof(*ti));
314 thlen = sizeof(udphdr_t);
315 ti->ti_pr = ip->ip_p;
316 ti->ti_src = ip->ip_src;
317 ti->ti_dst = ip->ip_dst;
318 bcopy((char *)ip + (IP_HL(ip) << 2),
319 (char *)&ti->ti_sport, sizeof(udphdr_t));
321 ti->ti_len = htons(thlen);
322 ip->ip_len = (IP_HL(ip) << 2) + thlen;
324 ti->ti_sum = chksum((u_short *)ti, thlen + sizeof(ip_t));
326 bcopy((char *)&ti->ti_sport,
327 (char *)ip + (IP_HL(ip) << 2), sizeof(udphdr_t));
328 return send_ip(nfd, mtu, ip, gwip, 1);
333 * send an icmp packet.
335 int send_icmp(nfd, mtu, ip, gwip)
342 ic = (struct icmp *)((char *)ip + (IP_HL(ip) << 2));
345 ic->icmp_cksum = chksum((u_short *)ic, sizeof(struct icmp));
347 return send_ip(nfd, mtu, ip, gwip, 1);
351 int send_packet(nfd, mtu, ip, gwip)
359 return send_tcp(nfd, mtu, ip, gwip);
361 return send_udp(nfd, mtu, ip, gwip);
363 return send_icmp(nfd, mtu, ip, gwip);
365 return send_ip(nfd, mtu, ip, gwip, 1);