xref: /netbsd-src/sys/net/if_gif.h (revision ffb7faba0eb527f28aa3903bb2356ae8032b94a7)
1 /*	$NetBSD: if_gif.h,v 1.35 2020/02/01 02:57:45 riastradh Exp $	*/
2 /*	$KAME: if_gif.h,v 1.23 2001/07/27 09:21:42 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * if_gif.h
35  */
36 
37 #ifndef _NET_IF_GIF_H_
38 #define _NET_IF_GIF_H_
39 
40 #include <sys/queue.h>
41 #include <sys/percpu.h>
42 #ifdef _KERNEL
43 #include <sys/pserialize.h>
44 #include <sys/psref.h>
45 #endif
46 
47 #ifdef _KERNEL_OPT
48 #include "opt_inet.h"
49 #endif
50 
51 #include <netinet/in.h>
52 /* xxx sigh, why route have struct route instead of pointer? */
53 
54 extern struct psref_class *gv_psref_class;
55 
56 struct encaptab;
57 
58 struct gif_variant {
59 	struct gif_softc *gv_softc;
60 	struct sockaddr	*gv_psrc; /* Physical src addr */
61 	struct sockaddr	*gv_pdst; /* Physical dst addr */
62 	const struct encaptab *gv_encap_cookie4;
63 	const struct encaptab *gv_encap_cookie6;
64 	int (*gv_output)(struct gif_variant *, int, struct mbuf *);
65 
66 	struct psref_target gv_psref;
67 };
68 
69 struct gif_softc {
70 	struct ifnet	gif_if;		/* common area - must be at the top */
71 	percpu_t *gif_ro_percpu;	/* struct tunnel_ro */
72 	struct gif_variant *gif_var;	/*
73 					 * reader must use gif_getref_variant()
74 					 * instead of direct dereference.
75 					 */
76 	kmutex_t gif_lock;		/* writer lock for gif_var */
77 	pserialize_t gif_psz;
78 
79 	int gif_pmtu;
80 
81 	LIST_ENTRY(gif_softc) gif_list;	/* list of all gifs */
82 };
83 #define GIF_ROUTE_TTL	10
84 
85 #define GIF_MTU		(1280)	/* Default MTU */
86 #define	GIF_MTU_MIN	(1280)	/* Minimum MTU */
87 #define	GIF_MTU_MAX	(8192)	/* Maximum MTU */
88 
89 /*
90  * Get gif_variant from gif_softc.
91  *
92  * Never return NULL by contract.
93  * gif_variant itself is protected not to be freed by gv_psref.
94  * Once a reader dereference sc->sc_var by this API, the reader must not
95  * re-dereference form sc->sc_var.
96  */
97 static __inline struct gif_variant *
gif_getref_variant(struct gif_softc * sc,struct psref * psref)98 gif_getref_variant(struct gif_softc *sc, struct psref *psref)
99 {
100 	struct gif_variant *var;
101 	int s;
102 
103 	s = pserialize_read_enter();
104 	var = atomic_load_consume(&sc->gif_var);
105 	KASSERT(var != NULL);
106 	psref_acquire(psref, &var->gv_psref, gv_psref_class);
107 	pserialize_read_exit(s);
108 
109 	return var;
110 }
111 
112 static __inline void
gif_putref_variant(struct gif_variant * var,struct psref * psref)113 gif_putref_variant(struct gif_variant *var, struct psref *psref)
114 {
115 
116 	KASSERT(var != NULL);
117 	psref_release(psref, &var->gv_psref, gv_psref_class);
118 }
119 
120 static __inline bool
gif_heldref_variant(struct gif_variant * var)121 gif_heldref_variant(struct gif_variant *var)
122 {
123 
124 	return psref_held(&var->gv_psref, gv_psref_class);
125 }
126 
127 /* Prototypes */
128 void	gif_input(struct mbuf *, int, struct ifnet *);
129 
130 #ifdef GIF_ENCAPCHECK
131 int	gif_encapcheck(struct mbuf *, int, int, void *);
132 #endif
133 
134 /*
135  * Locking notes:
136  * + gif_softc_list is protected by gif_softcs.lock (an adaptive mutex)
137  *       gif_softc_list is list of all gif_softcs. It is used by ioctl
138  *       context only.
139  * + gif_softc->gif_var is protected by
140  *   - gif_softc->gif_lock (an adaptive mutex) for writer
141  *   - gif_var->gv_psref for reader
142  *       gif_softc->gif_var is used for variant values while the gif tunnel
143  *       exists.
144  * + Each CPU's tunnel_ro.tr_ro of gif_ro_percpu are protected by
145  *   percpu'ed tunnel_ro.tr_lock.
146  *
147  * Locking order:
148  *     - encap_lock => gif_softc->gif_lock => gif_softcs.lock
149  */
150 #endif /* !_NET_IF_GIF_H_ */
151