xref: /netbsd-src/sys/net/if_gif.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: if_gif.h,v 1.31 2018/04/27 09:55:27 knakahara 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/psref.h>
44 #endif
45 
46 #ifdef _KERNEL_OPT
47 #include "opt_inet.h"
48 #endif
49 
50 #include <netinet/in.h>
51 /* xxx sigh, why route have struct route instead of pointer? */
52 
53 extern struct psref_class *gv_psref_class;
54 
55 struct encaptab;
56 
57 struct gif_ro {
58 	struct route gr_ro;
59 	kmutex_t *gr_lock;
60 };
61 
62 struct gif_variant {
63 	struct gif_softc *gv_softc;
64 	struct sockaddr	*gv_psrc; /* Physical src addr */
65 	struct sockaddr	*gv_pdst; /* Physical dst addr */
66 	const struct encaptab *gv_encap_cookie4;
67 	const struct encaptab *gv_encap_cookie6;
68 	int (*gv_output)(struct gif_variant *, int, struct mbuf *);
69 
70 	struct psref_target gv_psref;
71 };
72 
73 struct gif_softc {
74 	struct ifnet	gif_if;		/* common area - must be at the top */
75 	percpu_t *gif_ro_percpu;	/* struct gif_ro */
76 	struct gif_variant *gif_var;	/*
77 					 * reader must use gif_getref_variant()
78 					 * instead of direct dereference.
79 					 */
80 	kmutex_t gif_lock;		/* writer lock for gif_var */
81 
82 	LIST_ENTRY(gif_softc) gif_list;	/* list of all gifs */
83 };
84 #define GIF_ROUTE_TTL	10
85 
86 #define GIF_MTU		(1280)	/* Default MTU */
87 #define	GIF_MTU_MIN	(1280)	/* Minimum MTU */
88 #define	GIF_MTU_MAX	(8192)	/* Maximum MTU */
89 
90 /*
91  * Get gif_variant from gif_softc.
92  *
93  * Never return NULL by contract.
94  * gif_variant itself is protected not to be freed by gv_psref.
95  * Once a reader dereference sc->sc_var by this API, the reader must not
96  * re-dereference form sc->sc_var.
97  */
98 static __inline struct gif_variant *
99 gif_getref_variant(struct gif_softc *sc, struct psref *psref)
100 {
101 	struct gif_variant *var;
102 	int s;
103 
104 	s = pserialize_read_enter();
105 	var = sc->gif_var;
106 	KASSERT(var != NULL);
107 	membar_datadep_consumer();
108 	psref_acquire(psref, &var->gv_psref, gv_psref_class);
109 	pserialize_read_exit(s);
110 
111 	return var;
112 }
113 
114 static __inline void
115 gif_putref_variant(struct gif_variant *var, struct psref *psref)
116 {
117 
118 	KASSERT(var != NULL);
119 	psref_release(psref, &var->gv_psref, gv_psref_class);
120 }
121 
122 static __inline bool
123 gif_heldref_variant(struct gif_variant *var)
124 {
125 
126 	return psref_held(&var->gv_psref, gv_psref_class);
127 }
128 
129 /* Prototypes */
130 void	gif_input(struct mbuf *, int, struct ifnet *);
131 
132 void	gif_rtcache_free_pc(void *, void *, struct cpu_info *);
133 
134 #ifdef GIF_ENCAPCHECK
135 int	gif_encapcheck(struct mbuf *, int, int, void *);
136 #endif
137 
138 /*
139  * Locking notes:
140  * + gif_softc_list is protected by gif_softcs.lock (an adaptive mutex)
141  *       gif_softc_list is list of all gif_softcs. It is used by ioctl
142  *       context only.
143  * + gif_softc->gif_var is protected by
144  *   - gif_softc->gif_lock (an adaptive mutex) for writer
145  *   - gif_var->gv_psref for reader
146  *       gif_softc->gif_var is used for variant values while the gif tunnel
147  *       exists.
148  * + Each CPU's gif_ro.gr_ro of gif_ro_percpu are protected by
149  *   percpu'ed gif_ro.gr_lock.
150  *
151  * Locking order:
152  *     - encap_lock => gif_softc->gif_lock => gif_softcs.lock
153  */
154 #endif /* !_NET_IF_GIF_H_ */
155