xref: /freebsd/share/man/man9/domain.9 (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
1.\"
2.\" Copyright (C) 2001 Chad David <davidc@acns.ab.ca>. All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice(s), this list of conditions and the following disclaimer as
9.\"    the first lines of this file unmodified other than the possible
10.\"    addition of one or more copyright notices.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice(s), this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25.\" DAMAGE.
26.\"
27.\" $FreeBSD$
28.\"
29.Dd January 3, 2022
30.Dt DOMAIN 9
31.Os
32.Sh NAME
33.Nm domain_add ,
34.Nm domain_init ,
35.Nm pfctlinput ,
36.Nm pffinddomain ,
37.Nm pffindproto ,
38.Nm pffindtype ,
39.Nm DOMAIN_SET
40.Nd "network domain management"
41.Sh SYNOPSIS
42.In sys/param.h
43.In sys/kernel.h
44.In sys/protosw.h
45.In sys/domain.h
46.Ft void
47.Fn domain_add "void *data"
48.Ft void
49.Fn domain_init "void *data"
50.Ft void
51.Fn pfctlinput "int cmd" "struct sockaddr *sa"
52.Ft struct domain *
53.Fn pffinddomain "int family"
54.Ft struct protosw *
55.Fn pffindproto "int family" "int protocol" "int type"
56.Ft struct protosw *
57.Fn pffindtype "int family" "int type"
58.Ft void
59.Fn DOMAIN_SET "name"
60.Sh DESCRIPTION
61Network protocols installed in the system are maintained within what
62are called domains
63(for example the
64.Va inetdomain
65and
66.Va localdomain ) .
67.Bd -literal
68struct domain {
69	int	dom_family;		/* AF_xxx */
70	char	*dom_name;
71	int	dom_flags;
72	int	(*dom_probe)(void);	/* check for support (optional) */
73	int	(*dom_externalize)	/* externalize access rights */
74		(struct mbuf *, struct mbuf **);
75	void	(*dom_dispose)		/* dispose of internalized rights */
76		(struct mbuf *);
77	struct	protosw *dom_protosw, *dom_protoswNPROTOSW;
78	struct	domain *dom_next;
79	int	(*dom_rtattach)		/* initialize routing table */
80		(void **, int);
81	int	(*dom_rtdetach)		/* clean up routing table */
82		(void **, int);
83	void	*(*dom_ifattach)(struct ifnet *);
84	void	(*dom_ifdetach)(struct ifnet *, void *);
85	int	(*dom_ifmtu)(struct ifnet *);
86					/* af-dependent data on ifnet */
87};
88.Ed
89.Pp
90Each domain contains an array of protocol switch structures
91.Pq Vt "struct protosw *" ,
92one for each socket type supported.
93.Bd -literal
94struct protosw {
95	short	pr_type;		/* socket type used for */
96	struct	domain *pr_domain;	/* domain protocol a member of */
97	short	pr_protocol;		/* protocol number */
98	short	pr_flags;		/* see below */
99/* protocol-protocol hooks */
100	pr_input_t *pr_input;		/* input to protocol (from below) */
101	pr_output_t *pr_output;		/* output to protocol (from above) */
102	pr_ctlinput_t *pr_ctlinput;	/* control input (from below) */
103	pr_ctloutput_t *pr_ctloutput;	/* control output (from above) */
104/* utility hooks */
105	pr_fasttimo_t *pr_fasttimo;	/* fast timeout (200ms) */
106	pr_slowtimo_t *pr_slowtimo;	/* slow timeout (500ms) */
107	pr_drain_t *pr_drain;		/* flush any excess space possible */
108
109	struct	pr_usrreqs *pr_usrreqs;	/* user-protocol hook */
110};
111.Ed
112.Pp
113The following functions handle the registration of a new domain,
114lookups of specific protocols and protocol types within those domains,
115and handle control messages from the system.
116.Pp
117.Fn pfctlinput
118is called by the system whenever an event occurs that could affect every
119domain.
120Examples of those types of events are routing table changes, interface
121shutdowns or certain
122.Tn ICMP
123message types.
124When called,
125.Fn pfctlinput
126calls the protocol specific
127.Fn pr_ctlinput
128function for each protocol in that has defined one, in every domain.
129.Pp
130.Fn domain_add
131adds a new protocol domain to the system.
132The argument
133.Fa data
134is cast directly to
135.Vt "struct domain *"
136within the function, but is declared
137.Vt "void *"
138in order to prevent compiler warnings when new domains are registered with
139.Fn SYSINIT .
140In most cases
141.Fn domain_add
142is not called directly, instead
143.Fn DOMAIN_SET
144is used.
145.Pp
146If the new domain has defined a probe routine, it is called first in
147.Fn domain_add
148to determine if the domain should be supported on the current system.
149If the probe routine returns a non-0 value, then the domain will not be
150marked as supported.
151Unsupported domains do not proceed with the initialization process and are not
152discoverable by
153.Fn pffinddomain ,
154.Fn pffindtype ,
155or
156.Fn pffindproto .
157.Pp
158.Fn domain_init
159is called after
160.Fn domain_add
161during boot and for each
162.Xr vnet 9 .
163If the new domain has defined an initialization routine, it is called during
164.Fn domain_init ;
165as well, each of the protocols within the domain that have defined an
166initialization routine will have theirs called.
167Note that domain initialization cannot fail at this time.
168.Pp
169Once a domain is added it cannot be completely unloaded.
170This is because there is
171no reference counting system in place to determine if there are any
172active references from sockets within that domain.
173.Pp
174.Fn pffinddomain
175finds a domain by family.
176If the domain cannot be found,
177.Dv NULL
178is returned.
179.Pp
180.Fn pffindtype
181and
182.Fn pffindproto
183look up a protocol by its number or by its type.
184In most cases, if the protocol or type cannot be found,
185.Dv NULL
186is returned, but
187.Fn pffindproto
188may return the default if the requested type is
189.Dv SOCK_RAW ,
190a protocol switch type of
191.Dv SOCK_RAW
192is found, and the domain has a default raw protocol.
193.Pp
194Both functions are called by
195.Fn socreate
196in order to resolve the protocol for the socket currently being created.
197.Pp
198.Fn DOMAIN_SET
199is a macro that simplifies the registration of a domain via
200.Fn SYSINIT .
201The code resulting from the macro expects there to be a domain structure
202named
203.Dq Fa name Ns Li domain
204where
205.Fa name
206is the argument to
207.Fn DOMAIN_SET :
208.Bd -literal
209struct domain localdomain =
210{ AF_LOCAL, "local", unp_init, unp_externalize, unp_dispose,
211  localsw, &localsw[sizeof(localsw)/sizeof(localsw[0])] };
212
213DOMAIN_SET(local);
214.Ed
215.Sh RETURN VALUES
216Both
217.Fn pffindtype
218and
219.Fn pffindproto
220return a
221.Vt "struct protosw *"
222for the protocol requested.
223If the protocol or socket type is not found,
224.Dv NULL
225is returned.
226In the case of
227.Fn pffindproto ,
228the default protocol may be returned for
229.Dv SOCK_RAW
230types if the domain has a default raw protocol.
231.Sh SEE ALSO
232.Xr socket 2
233.Sh HISTORY
234The functions
235.Fn domain_add ,
236.Fn pfctlinput ,
237.Fn pffinddomain ,
238.Fn pffindproto ,
239.Fn pffindtype
240and
241.Fn DOMAIN_SET
242first appeared in
243.Fx 4.4 .
244.Sh AUTHORS
245This manual page was written by
246.An Chad David Aq Mt davidc@acns.ab.ca .
247