1 /* $NetBSD: pk_acct.c,v 1.17 2003/08/07 16:33:03 agc Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Laboratory for Computation Vision and the Computer Science Department
9 * of the University of British Columbia.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)pk_acct.c 8.2 (Berkeley) 5/14/95
36 */
37
38 /*
39 * Copyright (c) 1984 University of British Columbia.
40 *
41 * This code is derived from software contributed to Berkeley by
42 * the Laboratory for Computation Vision and the Computer Science Department
43 * of the University of British Columbia.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the University of
56 * California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)pk_acct.c 8.2 (Berkeley) 5/14/95
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: pk_acct.c,v 1.17 2003/08/07 16:33:03 agc Exp $");
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/namei.h>
82 #include <sys/proc.h>
83 #include <sys/vnode.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/socket.h>
87 #include <sys/socketvar.h>
88
89 #include <net/if.h>
90
91 #include <netccitt/x25.h>
92 #include <netccitt/pk.h>
93 #include <netccitt/pk_var.h>
94 #include <netccitt/pk_extern.h>
95 #include <netccitt/x25acct.h>
96
97
98 struct vnode *pkacctp;
99 /*
100 * Turn on packet accounting
101 */
102 int
103 pk_accton(path)
104 char *path;
105 {
106 struct vnode *vp = NULL;
107 struct nameidata nd;
108 struct vnode *oacctp = pkacctp;
109 struct proc *p = curproc; /* XXX */
110 int error;
111
112 if (path == 0)
113 goto close;
114 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
115 if ((error = vn_open (&nd, FWRITE, 0644)) != 0)
116 return (error);
117 vp = nd.ni_vp;
118 VOP_UNLOCK(vp, 0);
119 if (vp -> v_type != VREG) {
120 vrele (vp);
121 return (EACCES);
122 }
123 pkacctp = vp;
124 if (oacctp) {
125 close:
126 error = vn_close (oacctp, FWRITE, p -> p_ucred, p);
127 }
128 return (error);
129 }
130
131 /*
132 * Write a record on the accounting file.
133 */
134
135 void
136 pk_acct(lcp)
137 struct pklcd *lcp;
138 {
139 struct vnode *vp;
140 struct sockaddr_x25 *sa;
141 char *src, *dst;
142 int len;
143 static struct x25acct acbuf;
144
145 if ((vp = pkacctp) == 0)
146 return;
147 bzero ((caddr_t)&acbuf, sizeof (acbuf));
148 if (lcp -> lcd_ceaddr != 0)
149 sa = lcp -> lcd_ceaddr;
150 else if (lcp -> lcd_craddr != 0) {
151 sa = lcp -> lcd_craddr;
152 acbuf.x25acct_callin = 1;
153 } else
154 return;
155
156 if (sa -> x25_opts.op_flags & X25_REVERSE_CHARGE)
157 acbuf.x25acct_revcharge = 1;
158 acbuf.x25acct_stime = lcp -> lcd_stime;
159 acbuf.x25acct_etime = time.tv_sec - acbuf.x25acct_stime;
160 acbuf.x25acct_uid = curproc -> p_cred -> p_ruid;
161 acbuf.x25acct_psize = sa -> x25_opts.op_psize;
162 acbuf.x25acct_net = sa -> x25_net;
163 /*
164 * Convert address to bcd
165 */
166 src = sa -> x25_addr;
167 dst = acbuf.x25acct_addr;
168 for (len = 0; *src; len++)
169 if (len & 01)
170 *dst++ |= *src++ & 0xf;
171 else
172 *dst = *src++ << 4;
173 acbuf.x25acct_addrlen = len;
174
175 bcopy (sa -> x25_udata, acbuf.x25acct_udata,
176 sizeof (acbuf.x25acct_udata));
177 acbuf.x25acct_txcnt = lcp -> lcd_txcnt;
178 acbuf.x25acct_rxcnt = lcp -> lcd_rxcnt;
179
180 (void) vn_rdwr(UIO_WRITE, vp, (caddr_t)&acbuf, sizeof (acbuf),
181 (off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND,
182 curproc -> p_ucred, (size_t *)0,
183 (struct proc *)0);
184 }
Cache object: ba9e19e099bca8183519945161834134
|