1 /*-
2  * Copyright (c) 1999, 2000, 2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: stable/9/sys/netncp/ncp_rq.h 206361 2010-04-07 16:50:38Z joel $
27  */
28 #ifndef _NETNCP_NCP_RQ_H_
29 #define _NETNCP_NCP_RQ_H_
30 
31 #include <sys/endian.h>
32 
33 #define getb(buf,ofs) 		(((const u_int8_t *)(buf))[ofs])
34 #define setb(buf,ofs,val)	(((u_int8_t*)(buf))[ofs])=val
35 #define getbw(buf,ofs)		((u_int16_t)(getb(buf,ofs)))
36 
37 #define	getwle(buf,ofs) (le16toh(*((u_int16_t*)(&((u_int8_t*)(buf))[ofs]))))
38 #define	getdle(buf,ofs) (le32toh(*((u_int32_t*)(&((u_int8_t*)(buf))[ofs]))))
39 #define	getwbe(buf,ofs) (be16toh(*((u_int16_t*)(&((u_int8_t*)(buf))[ofs]))))
40 #define	getdbe(buf,ofs) (be32toh(*((u_int32_t*)(&((u_int8_t*)(buf))[ofs]))))
41 
42 #define	setwle(buf,ofs,val) \
43 	(*((u_int16_t*)(&((u_int8_t*)(buf))[ofs])))=htole16(val)
44 #define	setdle(buf,ofs,val) \
45 	(*((u_int32_t*)(&((u_int8_t*)(buf))[ofs])))=htole32(val)
46 #define	setwbe(buf,ofs,val) \
47 	(*((u_int16_t*)(&((u_int8_t*)(buf))[ofs])))=htobe16(val)
48 #define	setdbe(buf,ofs,val) \
49 	(*((u_int32_t*)(&((u_int8_t*)(buf))[ofs])))=htobe32(val)
50 
51 #ifdef _KERNEL
52 
53 #include <sys/mchain.h>
54 
55 #define	NCPR_ALLOCED		0x0001	/* request structure was allocated */
56 #define	NCPR_DONTFREEONERR	0x0002	/* do not free structure on error */
57 
58 /*
59  * Structure to prepare ncp request and receive reply
60  */
61 struct ncp_rq {
62 	int		nr_flags;
63 	struct mbchain	rq;
64 	struct mdchain	rp;
65 	int		nr_minrplen;	/* minimal rp size (-1 if not known) */
66 	int		nr_rpsize;	/* reply size minus ncp header */
67 	int		nr_cc;		/* completion code */
68 	int		nr_cs;		/* connection state */
69 	struct thread *	nr_td;		/* thread that did rq */
70 	struct ucred *	nr_cred;	/* user that did rq */
71 	int		rexmit;
72 	struct ncp_conn*nr_conn;	/* back link */
73 };
74 
75 int  ncp_rq_alloc(u_int8_t fn, struct ncp_conn *ncp, struct thread *td,
76 	struct ucred *cred, struct ncp_rq **rqpp);
77 int  ncp_rq_alloc_any(u_int32_t ptype, u_int8_t fn, struct ncp_conn *ncp,
78 	struct thread *td,	struct ucred *cred, struct ncp_rq **rqpp);
79 int  ncp_rq_alloc_subfn(u_int8_t fn, u_int8_t subfn, struct ncp_conn *ncp,
80 	struct thread *td,	struct ucred *cred, struct ncp_rq **rqpp);
81 int  ncp_rq_init_any(struct ncp_rq *rqp, u_int32_t ptype, u_int8_t fn,
82 	struct ncp_conn *ncp,
83 	struct thread *td, struct ucred *cred);
84 void ncp_rq_done(struct ncp_rq *rqp);
85 int  ncp_request(struct ncp_rq *rqp);
86 int  ncp_request_int(struct ncp_rq *rqp);
87 
88 struct ncp_nlstables;
89 
90 int  ncp_rq_pathstring(struct ncp_rq *rqp, int size, const char *name, struct ncp_nlstables*);
91 int  ncp_rq_dbase_path(struct ncp_rq *, u_int8_t vol_num,
92 		    u_int32_t dir_base, int namelen, u_char *name, struct ncp_nlstables *nt);
93 int  ncp_rq_pstring(struct ncp_rq *rqp, const char *s);
94 
95 void ncp_sign_init(const char *logindata, char *sign_root);
96 
97 #else /* ifdef _KERNEL */
98 
99 #define	DECLARE_RQ	struct ncp_buf conn1, *conn=&conn1
100 
101 #define	ncp_add_byte(conn,x) (conn)->packet[(conn)->rqsize++]=x
102 
103 struct ncp_buf;
104 
105 __BEGIN_DECLS
106 
107 void ncp_init_request(struct ncp_buf *);
108 void ncp_init_request_s(struct ncp_buf *, int);
109 void ncp_add_word_lh(struct ncp_buf *, u_int16_t);
110 void ncp_add_dword_lh(struct ncp_buf *, u_int32_t);
111 void ncp_add_word_hl(struct ncp_buf *, u_int16_t);
112 void ncp_add_dword_hl(struct ncp_buf *, u_int32_t);
113 void ncp_add_mem(struct ncp_buf *, const void *, int);
114 void ncp_add_mem_nls(struct ncp_buf *, const void *, int);
115 void ncp_add_pstring(struct ncp_buf *, const char *);
116 void ncp_add_handle_path(struct ncp_buf *, nuint32, nuint32, int, const char *);
117 
118 #define ncp_reply_data(conn,offset) ((conn)->packet+offset)
119 #define ncp_reply_byte(conn,offset) (*(u_int8_t*)(ncp_reply_data(conn, offset)))
120 
121 u_int16_t ncp_reply_word_hl(struct ncp_buf *, int);
122 u_int16_t ncp_reply_word_lh(struct ncp_buf *, int);
123 u_int32_t ncp_reply_dword_hl(struct ncp_buf *, int);
124 u_int32_t ncp_reply_dword_lh(struct ncp_buf *, int);
125 
126 static __inline void
ConvertToNWfromDWORD(u_int32_t sfd,ncp_fh * fh)127 ConvertToNWfromDWORD(u_int32_t sfd, ncp_fh *fh) {
128 	fh->val1 = (fh->val.val32 = sfd);
129 	return;
130 }
131 
132 __END_DECLS
133 
134 #endif /* ifdef _KERNEL */
135 
136 #endif	/* !_NETNCP_NCP_RQ_H_ */
137