141edb306SCy Schubert
241edb306SCy Schubert /*
341edb306SCy Schubert * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert *
541edb306SCy Schubert * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert */
741edb306SCy Schubert /*
841edb306SCy Schubert * kmemcpy() - copies n bytes from kernel memory into user buffer.
941edb306SCy Schubert * returns 0 on success, -1 on error.
1041edb306SCy Schubert */
1141edb306SCy Schubert
1241edb306SCy Schubert #include <stdio.h>
1341edb306SCy Schubert #include <sys/param.h>
1441edb306SCy Schubert #include <sys/types.h>
1541edb306SCy Schubert #include <sys/uio.h>
1641edb306SCy Schubert #include <unistd.h>
1741edb306SCy Schubert #include <string.h>
1841edb306SCy Schubert #include <fcntl.h>
1941edb306SCy Schubert #include <sys/file.h>
2041edb306SCy Schubert #include <kvm.h>
2141edb306SCy Schubert #include <fcntl.h>
2241edb306SCy Schubert #include <sys/socket.h>
2341edb306SCy Schubert #include <sys/ioctl.h>
2441edb306SCy Schubert #include <netinet/in.h>
2541edb306SCy Schubert #include <arpa/inet.h>
2641edb306SCy Schubert #include <netinet/in_systm.h>
2741edb306SCy Schubert #include <netinet/ip.h>
2841edb306SCy Schubert #include <net/if.h>
2941edb306SCy Schubert
3041edb306SCy Schubert #include "kmem.h"
3141edb306SCy Schubert
3241edb306SCy Schubert
3341edb306SCy Schubert
3441edb306SCy Schubert
3541edb306SCy Schubert static kvm_t *kvm_f = NULL;
3641edb306SCy Schubert
3741edb306SCy Schubert
38efeb8bffSCy Schubert int
openkmem(char * kern,char * core)39efeb8bffSCy Schubert openkmem(char *kern, char *core)
4041edb306SCy Schubert {
4141edb306SCy Schubert kvm_f = kvm_open(kern, core, NULL, O_RDONLY, NULL);
4241edb306SCy Schubert if (kvm_f == NULL)
4341edb306SCy Schubert {
4441edb306SCy Schubert perror("openkmem:open");
45*2582ae57SCy Schubert return (-1);
4641edb306SCy Schubert }
47*2582ae57SCy Schubert return (kvm_f != NULL);
4841edb306SCy Schubert }
4941edb306SCy Schubert
50efeb8bffSCy Schubert int
kmemcpy(register char * buf,long pos,register int n)51efeb8bffSCy Schubert kmemcpy(register char *buf, long pos, register int n)
5241edb306SCy Schubert {
5341edb306SCy Schubert register int r;
5441edb306SCy Schubert
5541edb306SCy Schubert if (!n)
56*2582ae57SCy Schubert return (0);
5741edb306SCy Schubert
5841edb306SCy Schubert if (kvm_f == NULL)
5941edb306SCy Schubert if (openkmem(NULL, NULL) == -1)
60*2582ae57SCy Schubert return (-1);
6141edb306SCy Schubert
6241edb306SCy Schubert while ((r = kvm_read(kvm_f, pos, buf, n)) < n)
6341edb306SCy Schubert if (r <= 0)
6441edb306SCy Schubert {
6541edb306SCy Schubert fprintf(stderr, "pos=0x%lx ", (u_long)pos);
6641edb306SCy Schubert perror("kmemcpy:read");
67*2582ae57SCy Schubert return (-1);
6841edb306SCy Schubert }
6941edb306SCy Schubert else
7041edb306SCy Schubert {
7141edb306SCy Schubert buf += r;
7241edb306SCy Schubert pos += r;
7341edb306SCy Schubert n -= r;
7441edb306SCy Schubert }
75*2582ae57SCy Schubert return (0);
7641edb306SCy Schubert }
7741edb306SCy Schubert
78efeb8bffSCy Schubert int
kstrncpy(register char * buf,long pos,register int n)79efeb8bffSCy Schubert kstrncpy(register char *buf, long pos, register int n)
8041edb306SCy Schubert {
8141edb306SCy Schubert register int r;
8241edb306SCy Schubert
8341edb306SCy Schubert if (!n)
84*2582ae57SCy Schubert return (0);
8541edb306SCy Schubert
8641edb306SCy Schubert if (kvm_f == NULL)
8741edb306SCy Schubert if (openkmem(NULL, NULL) == -1)
88*2582ae57SCy Schubert return (-1);
8941edb306SCy Schubert
9041edb306SCy Schubert while (n > 0)
9141edb306SCy Schubert {
9241edb306SCy Schubert r = kvm_read(kvm_f, pos, buf, 1);
9341edb306SCy Schubert if (r <= 0)
9441edb306SCy Schubert {
9541edb306SCy Schubert fprintf(stderr, "pos=0x%lx ", (u_long)pos);
9641edb306SCy Schubert perror("kmemcpy:read");
97*2582ae57SCy Schubert return (-1);
9841edb306SCy Schubert }
9941edb306SCy Schubert else
10041edb306SCy Schubert {
10141edb306SCy Schubert if (*buf == '\0')
10241edb306SCy Schubert break;
10341edb306SCy Schubert buf++;
10441edb306SCy Schubert pos++;
10541edb306SCy Schubert n--;
10641edb306SCy Schubert }
10741edb306SCy Schubert }
108*2582ae57SCy Schubert return (0);
10941edb306SCy Schubert }
110