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