1 /* $NetBSD: if_l2tp.h,v 1.10 2021/03/16 07:00:38 knakahara Exp $ */
2
3 /*
4 * Copyright (c) 2017 Internet Initiative Japan Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * L2TPv3 kernel interface
31 */
32
33 #ifndef _NET_IF_L2TP_H_
34 #define _NET_IF_L2TP_H_
35
36 #include <sys/queue.h>
37 #include <sys/ioccom.h>
38 #ifdef _KERNEL
39 #include <sys/pserialize.h>
40 #include <sys/psref.h>
41 #include <sys/pslist.h>
42 #endif
43
44 #include <net/if_ether.h>
45 #include <netinet/in.h>
46
47 #define SIOCSL2TPSESSION _IOW('i', 151, struct ifreq)
48 #define SIOCDL2TPSESSION _IOW('i', 152, struct ifreq)
49 #define SIOCSL2TPCOOKIE _IOW('i', 153, struct ifreq)
50 #define SIOCDL2TPCOOKIE _IOW('i', 154, struct ifreq)
51 #define SIOCSL2TPSTATE _IOW('i', 155, struct ifreq)
52 #define SIOCGL2TP SIOCGIFGENERIC
53
54 struct l2tp_req {
55 int state;
56 u_int my_cookie_len;
57 u_int peer_cookie_len;
58 uint32_t my_sess_id;
59 uint32_t peer_sess_id;
60 uint64_t my_cookie;
61 uint64_t peer_cookie;
62 };
63
64 #define L2TP_STATE_UP 1
65 #define L2TP_STATE_DOWN 0
66
67 #define L2TP_COOKIE_ON 1
68 #define L2TP_COOKIE_OFF 0
69
70 #ifdef _KERNEL
71 extern struct psref_class *lv_psref_class;
72
73 struct l2tp_variant {
74 struct l2tp_softc *lv_softc;
75
76 struct sockaddr *lv_psrc; /* Physical src addr */
77 struct sockaddr *lv_pdst; /* Physical dst addr */
78 const struct encaptab *lv_encap_cookie;
79
80 /* L2TP session info */
81 int lv_state;
82 uint32_t lv_my_sess_id; /* my session ID */
83 uint32_t lv_peer_sess_id; /* peer session ID */
84
85 int lv_use_cookie;
86 u_int lv_my_cookie_len;
87 u_int lv_peer_cookie_len;
88 uint64_t lv_my_cookie; /* my cookie */
89 uint64_t lv_peer_cookie; /* peer cookie */
90
91 struct psref_target lv_psref;
92 };
93
94 struct l2tp_softc {
95 struct ethercom l2tp_ec; /* common area - must be at the top */
96 /* to use ether_input(), we must have this */
97 percpu_t *l2tp_ro_percpu; /* struct tunnel_ro */
98 struct l2tp_variant *l2tp_var; /*
99 * reader must use l2tp_getref_variant()
100 * instead of direct dereference.
101 */
102 kmutex_t l2tp_lock; /* writer lock for l2tp_var */
103 pserialize_t l2tp_psz;
104
105 void *l2tp_si;
106 percpu_t *l2tp_ifq_percpu;
107
108 LIST_ENTRY(l2tp_softc) l2tp_list; /* list of all l2tps */
109 struct pslist_entry l2tp_hash; /* hashed list to lookup by session id */
110 };
111
112 #define L2TP_ROUTE_TTL 10
113
114 #define L2TP_MTU (1280) /* Default MTU */
115 #define L2TP_MTU_MIN (1280) /* Minimum MTU */
116 #define L2TP_MTU_MAX (8192) /* Maximum MTU */
117
118 /*
119 * Get l2tp_variant from l2tp_softc.
120 *
121 * l2tp_variant itself is protected not to be freed by lv_psref.
122 * In contrast, sc->sc_var can be changed to NULL even if reader critical
123 * section. see l2tp_variant_update().
124 * So, once a reader dereference sc->sc_var by this API, the reader must not
125 * re-dereference form sc->sc_var.
126 */
127 static __inline struct l2tp_variant *
l2tp_getref_variant(struct l2tp_softc * sc,struct psref * psref)128 l2tp_getref_variant(struct l2tp_softc *sc, struct psref *psref)
129 {
130 struct l2tp_variant *var;
131 int s;
132
133 s = pserialize_read_enter();
134 var = atomic_load_consume(&sc->l2tp_var);
135 if (var == NULL) {
136 pserialize_read_exit(s);
137 return NULL;
138 }
139 psref_acquire(psref, &var->lv_psref, lv_psref_class);
140 pserialize_read_exit(s);
141
142 return var;
143 }
144
145 static __inline void
l2tp_putref_variant(struct l2tp_variant * var,struct psref * psref)146 l2tp_putref_variant(struct l2tp_variant *var, struct psref *psref)
147 {
148
149 if (var == NULL)
150 return;
151 psref_release(psref, &var->lv_psref, lv_psref_class);
152 }
153
154 static __inline bool
l2tp_heldref_variant(struct l2tp_variant * var)155 l2tp_heldref_variant(struct l2tp_variant *var)
156 {
157
158 return psref_held(&var->lv_psref, lv_psref_class);
159 }
160
161
162 /* Prototypes */
163 void l2tpattach(int);
164 int l2tpattach0(struct l2tp_softc *);
165 void l2tp_input(struct mbuf *, struct ifnet *);
166 int l2tp_ioctl(struct ifnet *, u_long, void *);
167
168 struct l2tp_variant *l2tp_lookup_session_ref(uint32_t, struct psref *);
169 int l2tp_check_nesting(struct ifnet *, struct mbuf *);
170
171 /* TODO IP_TCPMSS support */
172 #ifdef IP_TCPMSS
173 struct mbuf *l2tp_tcpmss_clamp(struct ifnet *, struct mbuf *);
174 #endif /* IP_TCPMSS */
175 #endif /* _KERNEL */
176
177 /*
178 * Locking notes:
179 * + l2tp_softc_list is protected by l2tp_list_lock (an adaptive mutex)
180 * l2tp_softc_list is list of all l2tp_softcs, and it is used to avoid
181 * unload while busy.
182 * + l2tp_hashed_list is protected by
183 * - l2tp_hash_lock (an adaptive mutex) for writer
184 * - pserialize for reader
185 * l2tp_hashed_list is hashed list of all l2tp_softcs, and it is used by
186 * input processing to find appropriate softc.
187 * + l2tp_softc->l2tp_var is protected by
188 * - l2tp_softc->l2tp_lock (an adaptive mutex) for writer
189 * - l2tp_var->lv_psref for reader
190 * l2tp_softc->l2tp_var is used for variant values while the l2tp tunnel
191 * exists.
192 * + struct l2tp_ro->lr_ro is protected by struct tunnel_ro->tr_lock.
193 * This lock is required to exclude softnet/0 lwp(such as output
194 * processing softint) and processing lwp(such as DAD timer processing).
195 *
196 * Locking order:
197 * - encap_lock => struct l2tp_softc->l2tp_lock
198 * Other mutexes must not hold simultaneously.
199 *
200 * NOTICE
201 * - l2tp_softc must not have a variant value while the l2tp tunnel exists.
202 * Such variant values must be in l2tp_softc->l2tp_var.
203 * - l2tp_softc->l2tp_var is modified like read-copy-update.
204 * So, once we dereference l2tp_softc->l2tp_var, we must
205 * keep the pointer during the same context. If we re-derefence
206 * l2tp_softc->l2tp_var, the l2tp_var may be other one because of
207 * concurrent writer processing.
208 */
209 #endif /* _NET_IF_L2TP_H_ */
210