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