xref: /freebsd/share/man/man9/domain.9 (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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 April 13, 2021
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	void	(*dom_init)		/* initialize domain data structures */
73		(void);
74	int	(*dom_probe)(void);	/* check for support (optional) */
75	void	(*dom_destroy)		/* cleanup structures / state */
76		(void);
77	int	(*dom_externalize)	/* externalize access rights */
78		(struct mbuf *, struct mbuf **);
79	void	(*dom_dispose)		/* dispose of internalized rights */
80		(struct mbuf *);
81	struct	protosw *dom_protosw, *dom_protoswNPROTOSW;
82	struct	domain *dom_next;
83	int	(*dom_rtattach)		/* initialize routing table */
84		(void **, int);
85	int	(*dom_rtdetach)		/* clean up routing table */
86		(void **, int);
87	void	*(*dom_ifattach)(struct ifnet *);
88	void	(*dom_ifdetach)(struct ifnet *, void *);
89	int	(*dom_ifmtu)(struct ifnet *);
90					/* af-dependent data on ifnet */
91};
92.Ed
93.Pp
94Each domain contains an array of protocol switch structures
95.Pq Vt "struct protosw *" ,
96one for each socket type supported.
97.Bd -literal
98struct protosw {
99	short	pr_type;		/* socket type used for */
100	struct	domain *pr_domain;	/* domain protocol a member of */
101	short	pr_protocol;		/* protocol number */
102	short	pr_flags;		/* see below */
103/* protocol-protocol hooks */
104	pr_input_t *pr_input;		/* input to protocol (from below) */
105	pr_output_t *pr_output;		/* output to protocol (from above) */
106	pr_ctlinput_t *pr_ctlinput;	/* control input (from below) */
107	pr_ctloutput_t *pr_ctloutput;	/* control output (from above) */
108/* utility hooks */
109	pr_init_t *pr_init;
110	pr_fasttimo_t *pr_fasttimo;	/* fast timeout (200ms) */
111	pr_slowtimo_t *pr_slowtimo;	/* slow timeout (500ms) */
112	pr_drain_t *pr_drain;		/* flush any excess space possible */
113
114	struct	pr_usrreqs *pr_usrreqs;	/* user-protocol hook */
115};
116.Ed
117.Pp
118The following functions handle the registration of a new domain,
119lookups of specific protocols and protocol types within those domains,
120and handle control messages from the system.
121.Pp
122.Fn pfctlinput
123is called by the system whenever an event occurs that could affect every
124domain.
125Examples of those types of events are routing table changes, interface
126shutdowns or certain
127.Tn ICMP
128message types.
129When called,
130.Fn pfctlinput
131calls the protocol specific
132.Fn pr_ctlinput
133function for each protocol in that has defined one, in every domain.
134.Pp
135.Fn domain_add
136adds a new protocol domain to the system.
137The argument
138.Fa data
139is cast directly to
140.Vt "struct domain *"
141within the function, but is declared
142.Vt "void *"
143in order to prevent compiler warnings when new domains are registered with
144.Fn SYSINIT .
145In most cases
146.Fn domain_add
147is not called directly, instead
148.Fn DOMAIN_SET
149is used.
150.Pp
151If the new domain has defined a probe routine, it is called first in
152.Fn domain_add
153to determine if the domain should be supported on the current system.
154If the probe routine returns a non-0 value, then the domain will not be
155marked as supported.
156Unsupported domains do not proceed with the initialization process and are not
157discoverable by
158.Fn pffinddomain ,
159.Fn pffindtype ,
160or
161.Fn pffindproto .
162.Pp
163.Fn domain_init
164is called after
165.Fn domain_add
166during boot and for each
167.Xr vnet 9 .
168If the new domain has defined an initialization routine, it is called during
169.Fn domain_init ;
170as well, each of the protocols within the domain that have defined an
171initialization routine will have theirs called.
172Note that domain initialization cannot fail at this time.
173.Pp
174Once a domain is added it cannot be completely unloaded.
175This is because there is
176no reference counting system in place to determine if there are any
177active references from sockets within that domain.
178If the domain defines a
179.Fn dom_destroy
180routine, then it will be invoked during vnet teardown.
181.Pp
182.Fn pffinddomain
183finds a domain by family.
184If the domain cannot be found,
185.Dv NULL
186is returned.
187.Pp
188.Fn pffindtype
189and
190.Fn pffindproto
191look up a protocol by its number or by its type.
192In most cases, if the protocol or type cannot be found,
193.Dv NULL
194is returned, but
195.Fn pffindproto
196may return the default if the requested type is
197.Dv SOCK_RAW ,
198a protocol switch type of
199.Dv SOCK_RAW
200is found, and the domain has a default raw protocol.
201.Pp
202Both functions are called by
203.Fn socreate
204in order to resolve the protocol for the socket currently being created.
205.Pp
206.Fn DOMAIN_SET
207is a macro that simplifies the registration of a domain via
208.Fn SYSINIT .
209The code resulting from the macro expects there to be a domain structure
210named
211.Dq Fa name Ns Li domain
212where
213.Fa name
214is the argument to
215.Fn DOMAIN_SET :
216.Bd -literal
217struct domain localdomain =
218{ AF_LOCAL, "local", unp_init, unp_externalize, unp_dispose,
219  localsw, &localsw[sizeof(localsw)/sizeof(localsw[0])] };
220
221DOMAIN_SET(local);
222.Ed
223.Sh RETURN VALUES
224Both
225.Fn pffindtype
226and
227.Fn pffindproto
228return a
229.Vt "struct protosw *"
230for the protocol requested.
231If the protocol or socket type is not found,
232.Dv NULL
233is returned.
234In the case of
235.Fn pffindproto ,
236the default protocol may be returned for
237.Dv SOCK_RAW
238types if the domain has a default raw protocol.
239.Sh SEE ALSO
240.Xr socket 2
241.Sh HISTORY
242The functions
243.Fn domain_add ,
244.Fn pfctlinput ,
245.Fn pffinddomain ,
246.Fn pffindproto ,
247.Fn pffindtype
248and
249.Fn DOMAIN_SET
250first appeared in
251.Fx 4.4 .
252.Sh AUTHORS
253This manual page was written by
254.An Chad David Aq Mt davidc@acns.ab.ca .
255