Home | History | Annotate | Line # | Download | only in netinet
ip_proxy.h revision 1.3
      1 /*	$NetBSD: ip_proxy.h,v 1.3 2012/07/22 14:27:51 darrenr Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2012 by Darren Reed.
      5  *
      6  * See the IPFILTER.LICENCE file for details on licencing.
      7  *
      8  * Id: ip_proxy.h,v 1.1.1.2 2012/07/22 13:45:33 darrenr Exp
      9  */
     10 
     11 #ifndef _NETINET_IP_PROXY_H_
     12 #define _NETINET_IP_PROXY_H_
     13 
     14 #ifndef SOLARIS
     15 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
     16 #endif
     17 
     18 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51)
     19 #define	SIOCPROXY	_IOWR('r', 64, struct ap_control)
     20 #else
     21 #define	SIOCPROXY	_IOWR(r, 64, struct ap_control)
     22 #endif
     23 
     24 #ifndef	APR_LABELLEN
     25 #define	APR_LABELLEN	16
     26 #endif
     27 #define	AP_SESS_SIZE	53
     28 
     29 struct	nat;
     30 struct	ipnat;
     31 struct	ipstate;
     32 
     33 typedef	struct	ap_tcp {
     34 	u_short	apt_sport;	/* source port */
     35 	u_short	apt_dport;	/* destination port */
     36 	short	apt_sel[2];	/* {seq,ack}{off,min} set selector */
     37 	short	apt_seqoff[2];	/* sequence # difference */
     38 	u_32_t	apt_seqmin[2];	/* don't change seq-off until after this */
     39 	short	apt_ackoff[2];	/* sequence # difference */
     40 	u_32_t	apt_ackmin[2];	/* don't change seq-off until after this */
     41 	u_char	apt_state[2];	/* connection state */
     42 } ap_tcp_t;
     43 
     44 typedef	struct	ap_udp {
     45 	u_short	apu_sport;	/* source port */
     46 	u_short	apu_dport;	/* destination port */
     47 } ap_udp_t;
     48 
     49 typedef	struct ap_session {
     50 	struct	aproxy	*aps_apr;
     51 	union {
     52 		struct	ap_tcp	apu_tcp;
     53 		struct	ap_udp	apu_udp;
     54 	} aps_un;
     55 	U_QUAD_T aps_bytes;	/* bytes sent */
     56 	U_QUAD_T aps_pkts;	/* packets sent */
     57 	void	*aps_nat;	/* pointer back to nat struct */
     58 	void	*aps_data;	/* private data */
     59 	int	aps_psiz;	/* size of private data */
     60 	struct	ap_session	*aps_next;
     61 } ap_session_t;
     62 
     63 #define	aps_sport	aps_un.apu_tcp.apt_sport
     64 #define	aps_dport	aps_un.apu_tcp.apt_dport
     65 #define	aps_sel		aps_un.apu_tcp.apt_sel
     66 #define	aps_seqoff	aps_un.apu_tcp.apt_seqoff
     67 #define	aps_seqmin	aps_un.apu_tcp.apt_seqmin
     68 #define	aps_state	aps_un.apu_tcp.apt_state
     69 #define	aps_ackoff	aps_un.apu_tcp.apt_ackoff
     70 #define	aps_ackmin	aps_un.apu_tcp.apt_ackmin
     71 
     72 
     73 typedef	struct	ap_control {
     74 	char	apc_label[APR_LABELLEN];
     75 	char	apc_config[APR_LABELLEN];
     76 	u_char	apc_p;
     77 	/*
     78 	 * The following fields are upto the proxy's apr_ctl routine to deal
     79 	 * with.  When the proxy gets this in kernel space, apc_data will
     80 	 * point to a malloc'd region of memory of apc_dsize bytes.  If the
     81 	 * proxy wants to keep that memory, it must set apc_data to NULL
     82 	 * before it returns.  It is expected if this happens that it will
     83 	 * take care to free it in apr_fini or otherwise as appropriate.
     84 	 * apc_cmd is provided as a standard place to put simple commands,
     85 	 * with apc_arg being available to put a simple arg.
     86 	 */
     87 	u_long	apc_cmd;
     88 	u_long	apc_arg;
     89 	void	*apc_data;
     90 	size_t	apc_dsize;
     91 } ap_ctl_t;
     92 
     93 #define	APC_CMD_ADD	0
     94 #define	APC_CMD_DEL	1
     95 
     96 
     97 typedef	struct	aproxy	{
     98 	struct	aproxy	*apr_next;
     99 	struct	aproxy	*apr_parent;
    100 	char	apr_label[APR_LABELLEN];	/* Proxy label # */
    101 	u_char	apr_p;				/* protocol */
    102 	int	apr_flags;
    103 	int	apr_ref;
    104 	int	apr_clones;
    105 	void	(* apr_load)(void);
    106 	void	(* apr_unload)(void);
    107 	void	*(* apr_create)(ipf_main_softc_t *);
    108 	void	(* apr_destroy)(ipf_main_softc_t *, void *);
    109 	int	(* apr_init)(ipf_main_softc_t *, void *);
    110 	void	(* apr_fini)(ipf_main_softc_t *, void *);
    111 	int	(* apr_new)(void *, fr_info_t *, ap_session_t *,
    112 				 struct nat *);
    113 	void	(* apr_del)(ipf_main_softc_t *, ap_session_t *);
    114 	int	(* apr_inpkt)(void *, fr_info_t *, ap_session_t *,
    115 				   struct nat *);
    116 	int	(* apr_outpkt)(void *, fr_info_t *, ap_session_t *,
    117 				    struct nat *);
    118 	int	(* apr_match)(fr_info_t *, ap_session_t *, struct nat *);
    119 	int	(* apr_ctl)(ipf_main_softc_t *, void *, ap_ctl_t *);
    120 	int	(* apr_clear)(struct aproxy *);
    121 	int	(* apr_flush)(struct aproxy *, int);
    122 	void	*apr_soft;
    123 } aproxy_t;
    124 
    125 #define	APR_DELETE	1
    126 
    127 #define	APR_ERR(x)	((x) << 16)
    128 #define	APR_EXIT(x)	(((x) >> 16) & 0xffff)
    129 #define	APR_INC(x)	((x) & 0xffff)
    130 
    131 
    132 #ifdef _KERNEL
    133 /*
    134  * Generic #define's to cover missing things in the kernel
    135  */
    136 # ifndef isdigit
    137 #  define isdigit(x)	((x) >= '0' && (x) <= '9')
    138 # endif
    139 # ifndef isupper
    140 #  define isupper(x)	(((unsigned)(x) >= 'A') && ((unsigned)(x) <= 'Z'))
    141 # endif
    142 # ifndef islower
    143 #  define islower(x)	(((unsigned)(x) >= 'a') && ((unsigned)(x) <= 'z'))
    144 # endif
    145 # ifndef isalpha
    146 #  define isalpha(x)	(isupper(x) || islower(x))
    147 # endif
    148 # ifndef toupper
    149 #  define toupper(x)	(isupper(x) ? (x) : (x) - 'a' + 'A')
    150 # endif
    151 # ifndef isspace
    152 #  define isspace(x)	(((x) == ' ') || ((x) == '\r') || ((x) == '\n') || \
    153 			 ((x) == '\t') || ((x) == '\b'))
    154 # endif
    155 #endif /* _KERNEL */
    156 
    157 /*
    158  * For the ftp proxy.
    159  */
    160 #define	FTP_BUFSZ	160
    161 #define	IPF_FTPBUFSZ	160
    162 
    163 typedef struct  ftpside {
    164 	char	*ftps_rptr;
    165 	char	*ftps_wptr;
    166 	void	*ftps_ifp;
    167 	u_32_t	ftps_seq[2];
    168 	u_32_t	ftps_len;
    169 	int	ftps_junk;
    170 	int	ftps_cmds;
    171 	int	ftps_cmd;
    172 	char	ftps_buf[FTP_BUFSZ];
    173 } ftpside_t;
    174 
    175 typedef struct  ftpinfo {
    176 	int 	  	ftp_passok;
    177 	int		ftp_incok;
    178 	void		*ftp_pendstate;
    179 	nat_t		*ftp_pendnat;
    180 	ftpside_t	ftp_side[2];
    181 } ftpinfo_t;
    182 
    183 
    184 /*
    185  * IPsec proxy
    186  */
    187 typedef u_32_t		ipsec_cookie_t[2];
    188 
    189 typedef struct ipsec_pxy {
    190 	ipsec_cookie_t	ipsc_icookie;
    191 	ipsec_cookie_t	ipsc_rcookie;
    192 	int		ipsc_rckset;
    193 	nat_t		*ipsc_nat;
    194 	struct ipstate	*ipsc_state;
    195 	ipnat_t		*ipsc_rule;
    196 } ipsec_pxy_t;
    197 
    198 
    199 /*
    200  * For the irc proxy.
    201  */
    202 typedef	struct	ircinfo {
    203 	size_t	irc_len;
    204 	char	*irc_snick;
    205 	char	*irc_dnick;
    206 	char	*irc_type;
    207 	char	*irc_arg;
    208 	char	*irc_addr;
    209 	u_32_t	irc_ipnum;
    210 	u_short	irc_port;
    211 } ircinfo_t;
    212 
    213 
    214 /*
    215  * For the DNS "proxy"
    216  */
    217 typedef struct dnsinfo {
    218         ipfmutex_t	dnsi_lock;
    219 	u_short		dnsi_id;
    220 	char		dnsi_buffer[512];
    221 } dnsinfo_t;
    222 
    223 
    224 /*
    225  * Real audio proxy structure and #defines
    226  */
    227 typedef	struct	raudio_s {
    228 	int	rap_seenpna;
    229 	int	rap_seenver;
    230 	int	rap_version;
    231 	int	rap_eos;	/* End Of Startup */
    232 	int	rap_gotid;
    233 	int	rap_gotlen;
    234 	int	rap_mode;
    235 	int	rap_sdone;
    236 	u_short	rap_plport;
    237 	u_short	rap_prport;
    238 	u_short	rap_srport;
    239 	char	rap_svr[19];
    240 	u_32_t	rap_sbf;	/* flag to indicate which of the 19 bytes have
    241 				 * been filled
    242 				 */
    243 	u_32_t	rap_sseq;
    244 } raudio_t;
    245 
    246 #define	RA_ID_END	0
    247 #define	RA_ID_UDP	1
    248 #define	RA_ID_ROBUST	7
    249 
    250 #define	RAP_M_UDP	1
    251 #define	RAP_M_ROBUST	2
    252 #define	RAP_M_TCP	4
    253 #define	RAP_M_UDP_ROBUST	(RAP_M_UDP|RAP_M_ROBUST)
    254 
    255 
    256 /*
    257  * MSN RPC proxy
    258  */
    259 typedef	struct	msnrpcinfo	{
    260 	u_int		mri_flags;
    261 	int		mri_cmd[2];
    262 	u_int		mri_valid;
    263 	struct	in_addr	mri_raddr;
    264 	u_short		mri_rport;
    265 } msnrpcinfo_t;
    266 
    267 
    268 /*
    269  * Sun RPCBIND proxy
    270  */
    271 #define RPCB_MAXMSG	888
    272 #define RPCB_RES_PMAP	0	/* Response contains a v2 port. */
    273 #define RPCB_RES_STRING	1	/* " " " v3 (GETADDR) string. */
    274 #define RPCB_RES_LIST	2	/* " " " v4 (GETADDRLIST) list. */
    275 #define RPCB_MAXREQS	32	/* Arbitrary limit on tracked transactions */
    276 
    277 #define RPCB_REQMIN	40
    278 #define RPCB_REQMAX	888
    279 #define RPCB_REPMIN	20
    280 #define	RPCB_REPMAX	604	/* XXX double check this! */
    281 
    282 /*
    283  * These macros determine the number of bytes between p and the end of
    284  * r->rs_buf relative to l.
    285  */
    286 #define RPCB_BUF_END(r) (char *)((r)->rm_msgbuf + (r)->rm_buflen)
    287 #define RPCB_BUF_GEQ(r, p, l)   \
    288         ((RPCB_BUF_END((r)) > (char *)(p)) &&           \
    289          ((RPCB_BUF_END((r)) - (char *)(p)) >= (l)))
    290 #define	RPCB_BUF_EQ(r, p, l)                            \
    291         (RPCB_BUF_END((r)) == ((char *)(p) + (l)))
    292 
    293 /*
    294  * The following correspond to RPC(B) detailed in RFC183[13].
    295  */
    296 #define RPCB_CALL		0
    297 #define RPCB_REPLY		1
    298 #define RPCB_MSG_VERSION	2
    299 #define RPCB_PROG		100000
    300 #define RPCB_GETPORT		3
    301 #define RPCB_GETADDR		3
    302 #define RPCB_GETADDRLIST	11
    303 #define RPCB_MSG_ACCEPTED	0
    304 #define RPCB_MSG_DENIED		1
    305 
    306 /* BEGIN (Generic XDR structures) */
    307 typedef struct xdr_string {
    308 	u_32_t	*xs_len;
    309 	char	*xs_str;
    310 } xdr_string_t;
    311 
    312 typedef struct xdr_auth {
    313 	/* u_32_t	xa_flavor; */
    314 	xdr_string_t	xa_string;
    315 } xdr_auth_t;
    316 
    317 typedef struct xdr_uaddr {
    318 	u_32_t		xu_ip;
    319 	u_short         xu_port;
    320 	xdr_string_t	xu_str;
    321 } xdr_uaddr_t;
    322 
    323 typedef	struct xdr_proto {
    324 	u_int		xp_proto;
    325 	xdr_string_t	xp_str;
    326 } xdr_proto_t;
    327 
    328 #define xu_xslen	xu_str.xs_len
    329 #define xu_xsstr	xu_str.xs_str
    330 #define	xp_xslen	xp_str.xs_len
    331 #define xp_xsstr	xp_str.xs_str
    332 /* END (Generic XDR structures) */
    333 
    334 /* BEGIN (RPC call structures) */
    335 typedef struct pmap_args {
    336 	/* u_32_t	pa_prog; */
    337 	/* u_32_t	pa_vers; */
    338 	u_32_t		*pa_prot;
    339 	/* u_32_t	pa_port; */
    340 } pmap_args_t;
    341 
    342 typedef struct rpcb_args {
    343 	/* u_32_t	*ra_prog; */
    344 	/* u_32_t	*ra_vers; */
    345 	xdr_proto_t	ra_netid;
    346 	xdr_uaddr_t	ra_maddr;
    347 	/* xdr_string_t	ra_owner; */
    348 } rpcb_args_t;
    349 
    350 typedef struct rpc_call {
    351 	/* u_32_t	rc_rpcvers; */
    352 	/* u_32_t	rc_prog; */
    353 	u_32_t	*rc_vers;
    354 	u_32_t	*rc_proc;
    355 	xdr_auth_t	rc_authcred;
    356 	xdr_auth_t	rc_authverf;
    357 	union {
    358 		pmap_args_t	ra_pmapargs;
    359 		rpcb_args_t	ra_rpcbargs;
    360 	} rpcb_args;
    361 } rpc_call_t;
    362 
    363 #define	rc_pmapargs	rpcb_args.ra_pmapargs
    364 #define rc_rpcbargs	rpcb_args.ra_rpcbargs
    365 /* END (RPC call structures) */
    366 
    367 /* BEGIN (RPC reply structures) */
    368 typedef struct rpcb_entry {
    369 	xdr_uaddr_t	re_maddr;
    370 	xdr_proto_t	re_netid;
    371 	/* u_32_t	re_semantics; */
    372 	xdr_string_t	re_family;
    373 	xdr_proto_t	re_proto;
    374 	u_32_t		*re_more; /* 1 == another entry follows */
    375 } rpcb_entry_t;
    376 
    377 typedef struct rpcb_listp {
    378 	u_32_t		*rl_list; /* 1 == list follows */
    379 	int		rl_cnt;
    380 	rpcb_entry_t	rl_entries[2]; /* TCP / UDP only */
    381 } rpcb_listp_t;
    382 
    383 typedef struct rpc_resp {
    384 	/* u_32_t	rr_acceptdeny; */
    385 	/* Omitted 'message denied' fork; we don't care about rejects. */
    386 	xdr_auth_t	rr_authverf;
    387 	/* u_32_t		*rr_astat;	*/
    388 	union {
    389 		u_32_t		*resp_pmap;
    390 		xdr_uaddr_t	resp_getaddr;
    391 		rpcb_listp_t	resp_getaddrlist;
    392 	} rpcb_reply;
    393 } rpc_resp_t;
    394 
    395 #define	rr_v2	rpcb_reply.resp_pmap
    396 #define rr_v3	rpcb_reply.resp_getaddr
    397 #define	rr_v4	rpcb_reply.resp_getaddrlist
    398 /* END (RPC reply structures) */
    399 
    400 /* BEGIN (RPC message structure & macros) */
    401 typedef struct rpc_msg {
    402 	char	rm_msgbuf[RPCB_MAXMSG];	/* RPCB data buffer */
    403 	u_int	rm_buflen;
    404 	u_32_t	*rm_xid;
    405 	/* u_32_t Call vs Reply */
    406 	union {
    407 		rpc_call_t	rb_call;
    408 		rpc_resp_t	rb_resp;
    409 	} rm_body;
    410 } rpc_msg_t;
    411 
    412 #define rm_call		rm_body.rb_call
    413 #define rm_resp		rm_body.rb_resp
    414 /* END (RPC message structure & macros) */
    415 
    416 /*
    417  * These code paths aren't hot enough to warrant per transaction
    418  * mutexes.
    419  */
    420 typedef struct rpcb_xact {
    421 	struct	rpcb_xact	*rx_next;
    422 	struct	rpcb_xact	**rx_pnext;
    423 	u_32_t	rx_xid;		/* RPC transmission ID */
    424 	u_int	rx_type;	/* RPCB response type */
    425 	u_int	rx_ref;         /* reference count */
    426 	u_int	rx_proto;	/* transport protocol (v2 only) */
    427 } rpcb_xact_t;
    428 
    429 typedef struct rpcb_session {
    430         ipfmutex_t	rs_rxlock;
    431 	rpcb_xact_t	*rs_rxlist;
    432 } rpcb_session_t;
    433 
    434 /*
    435  * For an explanation, please see the following:
    436  *   RFC1832 - Sections 3.11, 4.4, and 4.5.
    437  */
    438 #define XDRALIGN(x)	((((x) % 4) != 0) ? ((((x) + 3) / 4) * 4) : (x))
    439 
    440 extern	int	ipf_proxy_add(void *, aproxy_t *);
    441 extern	int	ipf_proxy_check(fr_info_t *, struct nat *);
    442 extern	int	ipf_proxy_ctl(ipf_main_softc_t *, void *, ap_ctl_t *);
    443 extern	int	ipf_proxy_del(aproxy_t *);
    444 extern	void	ipf_proxy_deref(aproxy_t *);
    445 extern	void	ipf_proxy_flush(void *, int);
    446 extern	void	ipf_proxy_free(ipf_main_softc_t *, ap_session_t *);
    447 extern	int	ipf_proxy_init(void);
    448 extern	int	ipf_proxy_ioctl(ipf_main_softc_t *, void *, ioctlcmd_t, int, void *);
    449 extern	aproxy_t	*ipf_proxy_lookup(void *, u_int, char *);
    450 extern	int	ipf_proxy_match(fr_info_t *, struct nat *);
    451 extern	int	ipf_proxy_new(fr_info_t *, struct nat *);
    452 extern	int	ipf_proxy_ok(fr_info_t *, tcphdr_t *, struct ipnat *);
    453 extern	void	aps_free(ipf_main_softc_t *, void *, ap_session_t *);
    454 extern	int	ipf_proxy_main_load(void);
    455 extern	int	ipf_proxy_main_unload(void);
    456 extern	ipnat_t	*ipf_proxy_rule_fwd(nat_t *);
    457 extern	ipnat_t	*ipf_proxy_rule_rev(nat_t *);
    458 extern	void	*ipf_proxy_soft_create(ipf_main_softc_t *);
    459 extern	void	ipf_proxy_soft_destroy(ipf_main_softc_t *, void *);
    460 extern	int	ipf_proxy_soft_fini(ipf_main_softc_t *, void *);
    461 extern	int	ipf_proxy_soft_init(ipf_main_softc_t *, void *);
    462 
    463 #endif /* _NETINET_IP_PROXY_H_ */
    464