3 * sock.c (C) 1995-1998 Darren Reed
5 * See the IPFILTER.LICENCE file for details on licencing.
9 static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed";
10 static const char rcsid[] = "@(#)$Id: sock.c,v 2.8.4.7 2007/09/13 07:19:34 darrenr Exp $";
12 #include <sys/param.h>
13 #include <sys/types.h>
16 #if defined(__NetBSD__) && defined(__vax__)
18 * XXX need to declare boolean_t for _KERNEL <sys/files.h>
19 * which ends up including <sys/device.h> for vax. See PR#32907
20 * for further details.
22 typedef int boolean_t;
27 #if (__FreeBSD_version >= 300000)
28 # include <sys/dirent.h>
34 # include <machine/lock.h>
35 # include <machine/mutex.h>
45 # include <sys/smp_lock.h>
47 # include <sys/file.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
60 #if !defined(ultrix) && !defined(hpux) && !defined(__osf__)
64 #include <sys/systm.h>
65 #include <sys/session.h>
68 #include <sys/sysctl.h>
69 #include <sys/filedesc.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/tcp.h>
78 #if defined(__FreeBSD__)
79 # include "radix_ipf.h"
82 # include <net/route.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/tcp_timer.h>
87 #include <netinet/tcp_var.h>
102 # define KMEM _PATH_KMEM
107 # define KERNEL _PATH_UNIX
111 # define KMEM "/dev/kmem"
114 # define KERNEL "/vmunix"
119 static struct proc *getproc __P((void));
121 static struct kinfo_proc *getproc __P((void));
125 int kmemcpy(buf, pos, n)
131 off_t offset = (u_long)pos;
134 kfd = open(KMEM, O_RDONLY);
136 if (lseek(kfd, offset, SEEK_SET) == -1)
141 if (read(kfd, buf, n) == -1)
149 struct nlist names[4] = {
161 static struct proc *getproc()
164 pid_t pid = getpid();
167 n = nlist(KERNEL, names);
170 fprintf(stderr, "nlist(%#x) == %d\n", names, n);
173 if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1)
175 fprintf(stderr, "read nproc (%#x)\n", names[1].n_value);
178 siz = nproc * sizeof(struct proc);
179 if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1)
181 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
182 names[0].n_value, &p, sizeof(p));
185 proc = (struct proc *)malloc(siz);
186 if (KMCPY(proc, p, siz) == -1)
188 fprintf(stderr, "read(%#x,%#x,%d) proc\n",
195 for (n = nproc; n; n--, p++)
205 struct tcpcb *find_tcp(fd, ti)
216 if (!(p = getproc()))
218 up = (struct user *)malloc(sizeof(*up));
220 if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1)
222 fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea);
226 if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1)
228 fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value);
233 o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1));
234 if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1)
236 fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n",
237 up->u_ofile, o, sizeof(*o));
240 f = (struct file *)calloc(1, sizeof(*f));
241 if (KMCPY(f, o[fd], sizeof(*f)) == -1)
243 fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n",
244 up->u_ofile[fd], f, sizeof(*f));
248 s = (struct socket *)calloc(1, sizeof(*s));
249 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
251 fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n",
252 o[fd], s, sizeof(*s));
256 i = (struct inpcb *)calloc(1, sizeof(*i));
257 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
259 fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n",
260 s->so_pcb, i, sizeof(*i));
264 t = (struct tcpcb *)calloc(1, sizeof(*t));
265 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
267 fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n",
268 i->inp_ppcb, t, sizeof(*t));
271 return (struct tcpcb *)i->inp_ppcb;
274 static struct kinfo_proc *getproc()
276 static struct kinfo_proc kp;
277 pid_t pid = getpid();
283 mib[2] = KERN_PROC_PID;
287 if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1)
296 struct tcpcb *find_tcp(tfd, ti)
304 struct kinfo_proc *p;
307 if (!(p = getproc()))
310 fd = (struct filedesc *)malloc(sizeof(*fd));
313 #if defined( __FreeBSD_version) && __FreeBSD_version >= 500013
314 if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1)
316 fprintf(stderr, "read(%#lx,%#lx) failed\n",
317 (u_long)p, (u_long)p->ki_fd);
322 if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1)
324 fprintf(stderr, "read(%#lx,%#lx) failed\n",
325 (u_long)p, (u_long)p->kp_proc.p_fd);
337 o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1));
338 if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1)
340 fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n",
341 (u_long)fd->fd_ofiles, (u_long)o, (u_long)sizeof(*o));
344 f = (struct file *)calloc(1, sizeof(*f));
345 if (KMCPY(f, o[tfd], sizeof(*f)) == -1)
347 fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd] - failed\n",
348 (u_long)o[tfd], (u_long)f, (u_long)sizeof(*f));
352 s = (struct socket *)calloc(1, sizeof(*s));
353 if (KMCPY(s, f->f_data, sizeof(*s)) == -1)
355 fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n",
356 (u_long)f->f_data, (u_long)s, (u_long)sizeof(*s));
360 i = (struct inpcb *)calloc(1, sizeof(*i));
361 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1)
363 fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n",
364 (u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i));
368 t = (struct tcpcb *)calloc(1, sizeof(*t));
369 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1)
371 fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n",
372 (u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t));
375 return (struct tcpcb *)i->inp_ppcb;
390 #endif /* BSD < 199301 */
392 int do_socket(dev, mtu, ti, gwip)
398 struct sockaddr_in rsin, lsin;
399 struct tcpcb *t, tcb;
403 printf("Dest. Port: %d\n", ti->ti_dport);
405 fd = socket(AF_INET, SOCK_STREAM, 0);
412 if (fcntl(fd, F_SETFL, FNDELAY) == -1)
418 bzero((char *)&lsin, sizeof(lsin));
419 lsin.sin_family = AF_INET;
420 bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr,
421 sizeof(struct in_addr));
422 if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1)
428 (void) getsockname(fd, (struct sockaddr *)&lsin, &len);
429 ti->ti_sport = lsin.sin_port;
430 printf("sport %d\n", ntohs(lsin.sin_port));
432 nfd = initdevice(dev, 1);
436 if (!(t = find_tcp(fd, ti)))
439 bzero((char *)&rsin, sizeof(rsin));
440 rsin.sin_family = AF_INET;
441 bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr,
442 sizeof(struct in_addr));
443 rsin.sin_port = ti->ti_dport;
444 if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 &&
445 errno != EINPROGRESS)
450 KMCPY(&tcb, t, sizeof(tcb));
451 ti->ti_win = tcb.rcv_adv;
452 ti->ti_seq = tcb.snd_nxt - 1;
453 ti->ti_ack = tcb.rcv_nxt;
455 if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1)
457 (void)write(fd, "Hello World\n", 12);