sock.c revision 153881
1/* $FreeBSD: head/contrib/ipfilter/ipsend/sock.c 153881 2005-12-30 11:52:26Z guido $ */ 2/* 3 * sock.c (C) 1995-1998 Darren Reed 4 * 5 * See the IPFILTER.LICENCE file for details on licencing. 6 * 7 */ 8#if !defined(lint) 9static const char sccsid[] = "@(#)sock.c 1.2 1/11/96 (C)1995 Darren Reed"; 10static const char rcsid[] = "@(#)$Id: sock.c,v 2.8.4.1 2004/03/23 12:58:06 darrenr Exp $"; 11#endif 12#include <sys/param.h> 13#include <sys/types.h> 14#include <sys/time.h> 15#include <sys/stat.h> 16#ifndef ultrix 17#include <fcntl.h> 18#endif 19#if (__FreeBSD_version >= 300000) 20# include <sys/dirent.h> 21#else 22# include <sys/dir.h> 23#endif 24#if !defined(__osf__) 25# define _KERNEL 26# define KERNEL 27# ifdef ultrix 28# undef LOCORE 29# include <sys/smp_lock.h> 30# endif 31# include <sys/file.h> 32# undef _KERNEL 33# undef KERNEL 34#endif 35#include <nlist.h> 36#include <sys/user.h> 37#include <sys/socket.h> 38#include <sys/socketvar.h> 39#include <sys/proc.h> 40#if !defined(ultrix) && !defined(hpux) && !defined(__osf__) 41# include <kvm.h> 42#endif 43#ifdef sun 44#include <sys/systm.h> 45#include <sys/session.h> 46#endif 47#if BSD >= 199103 48#include <sys/sysctl.h> 49#include <sys/filedesc.h> 50#include <paths.h> 51#endif 52#include <math.h> 53#include <netinet/in.h> 54#include <netinet/in_systm.h> 55#include <netinet/ip.h> 56#include <netinet/tcp.h> 57#include <net/if.h> 58#if defined(__FreeBSD__) 59# include "radix_ipf.h" 60#endif 61#include <net/route.h> 62#include <netinet/ip_var.h> 63#include <netinet/in_pcb.h> 64#include <netinet/tcp_timer.h> 65#include <netinet/tcp_var.h> 66#include <stdio.h> 67#include <unistd.h> 68#include <string.h> 69#include <stdlib.h> 70#include <stddef.h> 71#include <pwd.h> 72#include "ipsend.h" 73 74 75int nproc; 76struct proc *proc; 77 78#ifndef KMEM 79# ifdef _PATH_KMEM 80# define KMEM _PATH_KMEM 81# endif 82#endif 83#ifndef KERNEL 84# ifdef _PATH_UNIX 85# define KERNEL _PATH_UNIX 86# endif 87#endif 88#ifndef KMEM 89# define KMEM "/dev/kmem" 90#endif 91#ifndef KERNEL 92# define KERNEL "/vmunix" 93#endif 94 95 96#if BSD < 199103 97static struct proc *getproc __P((void)); 98#else 99static struct kinfo_proc *getproc __P((void)); 100#endif 101 102 103int kmemcpy(buf, pos, n) 104char *buf; 105void *pos; 106int n; 107{ 108 static int kfd = -1; 109 off_t offset = (u_long)pos; 110 111 if (kfd == -1) 112 kfd = open(KMEM, O_RDONLY); 113 114 if (lseek(kfd, offset, SEEK_SET) == -1) 115 { 116 perror("lseek"); 117 return -1; 118 } 119 if (read(kfd, buf, n) == -1) 120 { 121 perror("read"); 122 return -1; 123 } 124 return n; 125} 126 127struct nlist names[4] = { 128 { "_proc" }, 129 { "_nproc" }, 130#ifdef ultrix 131 { "_u" }, 132#else 133 { NULL }, 134#endif 135 { NULL } 136 }; 137 138#if BSD < 199103 139static struct proc *getproc() 140{ 141 struct proc *p; 142 pid_t pid = getpid(); 143 int siz, n; 144 145 n = nlist(KERNEL, names); 146 if (n != 0) 147 { 148 fprintf(stderr, "nlist(%#x) == %d\n", names, n); 149 return NULL; 150 } 151 if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1) 152 { 153 fprintf(stderr, "read nproc (%#x)\n", names[1].n_value); 154 return NULL; 155 } 156 siz = nproc * sizeof(struct proc); 157 if (KMCPY(&p, names[0].n_value, sizeof(p)) == -1) 158 { 159 fprintf(stderr, "read(%#x,%#x,%d) proc\n", 160 names[0].n_value, &p, sizeof(p)); 161 return NULL; 162 } 163 proc = (struct proc *)malloc(siz); 164 if (KMCPY(proc, p, siz) == -1) 165 { 166 fprintf(stderr, "read(%#x,%#x,%d) proc\n", 167 p, proc, siz); 168 return NULL; 169 } 170 171 p = proc; 172 173 for (n = nproc; n; n--, p++) 174 if (p->p_pid == pid) 175 break; 176 if (!n) 177 return NULL; 178 179 return p; 180} 181 182 183struct tcpcb *find_tcp(fd, ti) 184int fd; 185struct tcpiphdr *ti; 186{ 187 struct tcpcb *t; 188 struct inpcb *i; 189 struct socket *s; 190 struct user *up; 191 struct proc *p; 192 struct file *f, **o; 193 194 if (!(p = getproc())) 195 return NULL; 196 up = (struct user *)malloc(sizeof(*up)); 197#ifndef ultrix 198 if (KMCPY(up, p->p_uarea, sizeof(*up)) == -1) 199 { 200 fprintf(stderr, "read(%#x,%#x) failed\n", p, p->p_uarea); 201 return NULL; 202 } 203#else 204 if (KMCPY(up, names[2].n_value, sizeof(*up)) == -1) 205 { 206 fprintf(stderr, "read(%#x,%#x) failed\n", p, names[2].n_value); 207 return NULL; 208 } 209#endif 210 211 o = (struct file **)calloc(1, sizeof(*o) * (up->u_lastfile + 1)); 212 if (KMCPY(o, up->u_ofile, (up->u_lastfile + 1) * sizeof(*o)) == -1) 213 { 214 fprintf(stderr, "read(%#x,%#x,%d) - u_ofile - failed\n", 215 up->u_ofile, o, sizeof(*o)); 216 return NULL; 217 } 218 f = (struct file *)calloc(1, sizeof(*f)); 219 if (KMCPY(f, o[fd], sizeof(*f)) == -1) 220 { 221 fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n", 222 up->u_ofile[fd], f, sizeof(*f)); 223 return NULL; 224 } 225 226 s = (struct socket *)calloc(1, sizeof(*s)); 227 if (KMCPY(s, f->f_data, sizeof(*s)) == -1) 228 { 229 fprintf(stderr, "read(%#x,%#x,%d) - f_data - failed\n", 230 o[fd], s, sizeof(*s)); 231 return NULL; 232 } 233 234 i = (struct inpcb *)calloc(1, sizeof(*i)); 235 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1) 236 { 237 fprintf(stderr, "kvm_read(%#x,%#x,%d) - so_pcb - failed\n", 238 s->so_pcb, i, sizeof(*i)); 239 return NULL; 240 } 241 242 t = (struct tcpcb *)calloc(1, sizeof(*t)); 243 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1) 244 { 245 fprintf(stderr, "read(%#x,%#x,%d) - inp_ppcb - failed\n", 246 i->inp_ppcb, t, sizeof(*t)); 247 return NULL; 248 } 249 return (struct tcpcb *)i->inp_ppcb; 250} 251#else 252static struct kinfo_proc *getproc() 253{ 254 static struct kinfo_proc kp; 255 pid_t pid = getpid(); 256 int mib[4]; 257 size_t n; 258 259 mib[0] = CTL_KERN; 260 mib[1] = KERN_PROC; 261 mib[2] = KERN_PROC_PID; 262 mib[3] = pid; 263 264 n = sizeof(kp); 265 if (sysctl(mib, 4, &kp, &n, NULL, 0) == -1) 266 { 267 perror("sysctl"); 268 return NULL; 269 } 270 return &kp; 271} 272 273 274struct tcpcb *find_tcp(tfd, ti) 275int tfd; 276struct tcpiphdr *ti; 277{ 278 struct tcpcb *t; 279 struct inpcb *i; 280 struct socket *s; 281 struct filedesc *fd; 282 struct kinfo_proc *p; 283 struct file *f, **o; 284 285 if (!(p = getproc())) 286 return NULL; 287 288 fd = (struct filedesc *)malloc(sizeof(*fd)); 289#if defined( __FreeBSD_version) && __FreeBSD_version >= 500013 290 if (KMCPY(fd, p->ki_fd, sizeof(*fd)) == -1) 291 { 292 fprintf(stderr, "read(%#lx,%#lx) failed\n", 293 (u_long)p, (u_long)p->ki_fd); 294 return NULL; 295 } 296#else 297 if (KMCPY(fd, p->kp_proc.p_fd, sizeof(*fd)) == -1) 298 { 299 fprintf(stderr, "read(%#lx,%#lx) failed\n", 300 (u_long)p, (u_long)p->kp_proc.p_fd); 301 return NULL; 302 } 303#endif 304 305 o = (struct file **)calloc(1, sizeof(*o) * (fd->fd_lastfile + 1)); 306 if (KMCPY(o, fd->fd_ofiles, (fd->fd_lastfile + 1) * sizeof(*o)) == -1) 307 { 308 fprintf(stderr, "read(%#lx,%#lx,%lu) - u_ofile - failed\n", 309 (u_long)fd->fd_ofiles, (u_long)o, (u_long)sizeof(*o)); 310 return NULL; 311 } 312 f = (struct file *)calloc(1, sizeof(*f)); 313 if (KMCPY(f, o[tfd], sizeof(*f)) == -1) 314 { 315 fprintf(stderr, "read(%#lx,%#lx,%lu) - o[tfd] - failed\n", 316 (u_long)o[tfd], (u_long)f, (u_long)sizeof(*f)); 317 return NULL; 318 } 319 320 s = (struct socket *)calloc(1, sizeof(*s)); 321 if (KMCPY(s, f->f_data, sizeof(*s)) == -1) 322 { 323 fprintf(stderr, "read(%#lx,%#lx,%lu) - f_data - failed\n", 324 (u_long)f->f_data, (u_long)s, 325 (u_long)sizeof(*s)); 326 return NULL; 327 } 328 329 i = (struct inpcb *)calloc(1, sizeof(*i)); 330 if (KMCPY(i, s->so_pcb, sizeof(*i)) == -1) 331 { 332 fprintf(stderr, "kvm_read(%#lx,%#lx,%lu) - so_pcb - failed\n", 333 (u_long)s->so_pcb, (u_long)i, (u_long)sizeof(*i)); 334 return NULL; 335 } 336 337 t = (struct tcpcb *)calloc(1, sizeof(*t)); 338 if (KMCPY(t, i->inp_ppcb, sizeof(*t)) == -1) 339 { 340 fprintf(stderr, "read(%#lx,%#lx,%lu) - inp_ppcb - failed\n", 341 (u_long)i->inp_ppcb, (u_long)t, (u_long)sizeof(*t)); 342 return NULL; 343 } 344 return (struct tcpcb *)i->inp_ppcb; 345} 346#endif /* BSD < 199301 */ 347 348int do_socket(dev, mtu, ti, gwip) 349char *dev; 350int mtu; 351struct tcpiphdr *ti; 352struct in_addr gwip; 353{ 354 struct sockaddr_in rsin, lsin; 355 struct tcpcb *t, tcb; 356 int fd, nfd, len; 357 358 printf("Dest. Port: %d\n", ti->ti_dport); 359 360 fd = socket(AF_INET, SOCK_STREAM, 0); 361 if (fd == -1) 362 { 363 perror("socket"); 364 return -1; 365 } 366 367 if (fcntl(fd, F_SETFL, FNDELAY) == -1) 368 { 369 perror("fcntl"); 370 return -1; 371 } 372 373 bzero((char *)&lsin, sizeof(lsin)); 374 lsin.sin_family = AF_INET; 375 bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr, 376 sizeof(struct in_addr)); 377 if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1) 378 { 379 perror("bind"); 380 return -1; 381 } 382 len = sizeof(lsin); 383 (void) getsockname(fd, (struct sockaddr *)&lsin, &len); 384 ti->ti_sport = lsin.sin_port; 385 printf("sport %d\n", ntohs(lsin.sin_port)); 386 nfd = initdevice(dev, 1); 387 388 if (!(t = find_tcp(fd, ti))) 389 return -1; 390 391 bzero((char *)&rsin, sizeof(rsin)); 392 rsin.sin_family = AF_INET; 393 bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr, 394 sizeof(struct in_addr)); 395 rsin.sin_port = ti->ti_dport; 396 if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 && 397 errno != EINPROGRESS) 398 { 399 perror("connect"); 400 return -1; 401 } 402 KMCPY(&tcb, t, sizeof(tcb)); 403 ti->ti_win = tcb.rcv_adv; 404 ti->ti_seq = tcb.snd_nxt - 1; 405 ti->ti_ack = tcb.rcv_nxt; 406 407 if (send_tcp(nfd, mtu, (ip_t *)ti, gwip) == -1) 408 return -1; 409 (void)write(fd, "Hello World\n", 12); 410 sleep(2); 411 close(fd); 412 return 0; 413} 414