if_l2tp.h revision 1.2 1 /* $NetBSD: if_l2tp.h,v 1.2 2017/05/31 08:19:44 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/psref.h>
40 #include <sys/pslist.h>
41 #endif
42
43 #include <net/if_ether.h>
44 #include <netinet/in.h>
45
46 #define SIOCSL2TPSESSION _IOW('i', 151, struct l2tp_req)
47 #define SIOCDL2TPSESSION _IOW('i', 152, struct l2tp_req)
48 #define SIOCSL2TPCOOKIE _IOW('i', 153, struct l2tp_req)
49 #define SIOCDL2TPCOOKIE _IOW('i', 154, struct l2tp_req)
50 #define SIOCSL2TPSTATE _IOW('i', 155, struct l2tp_req)
51 #define SIOCGL2TP SIOCGIFGENERIC
52
53 struct l2tp_req {
54 int state;
55 u_int my_cookie_len;
56 u_int peer_cookie_len;
57 uint32_t my_sess_id;
58 uint32_t peer_sess_id;
59 uint64_t my_cookie;
60 uint64_t peer_cookie;
61 };
62
63 #define L2TP_STATE_UP 1
64 #define L2TP_STATE_DOWN 0
65
66 #define L2TP_COOKIE_ON 1
67 #define L2TP_COOKIE_OFF 0
68
69 #ifdef _KERNEL
70 extern struct psref_class *lv_psref_class;
71
72 struct l2tp_variant {
73 struct l2tp_softc *lv_softc;
74
75 struct sockaddr *lv_psrc; /* Physical src addr */
76 struct sockaddr *lv_pdst; /* Physical dst addr */
77 const struct encaptab *lv_encap_cookie;
78
79 /* L2TP session info */
80 int lv_state;
81 uint32_t lv_my_sess_id; /* my session ID */
82 uint32_t lv_peer_sess_id; /* peer session ID */
83
84 int lv_use_cookie;
85 u_int lv_my_cookie_len;
86 u_int lv_peer_cookie_len;
87 uint64_t lv_my_cookie; /* my cookie */
88 uint64_t lv_peer_cookie; /* peer cookie */
89
90 struct psref_target lv_psref;
91 };
92
93 struct l2tp_ro {
94 struct route lr_ro;
95 kmutex_t lr_lock;
96 };
97
98 struct l2tp_softc {
99 struct ethercom l2tp_ec; /* common area - must be at the top */
100 /* to use ether_input(), we must have this */
101 percpu_t *l2tp_ro_percpu; /* struct l2tp_ro */
102 struct l2tp_variant *l2tp_var; /*
103 * reader must use l2tp_getref_variant()
104 * instead of direct dereference.
105 */
106 kmutex_t l2tp_lock; /* writer lock for l2tp_var */
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 *
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 = sc->l2tp_var;
135 if (var == NULL) {
136 pserialize_read_exit(s);
137 return NULL;
138 }
139 membar_datadep_consumer();
140 psref_acquire(psref, &var->lv_psref, lv_psref_class);
141 pserialize_read_exit(s);
142
143 return var;
144 }
145
146 static inline void
147 l2tp_putref_variant(struct l2tp_variant *var, struct psref *psref)
148 {
149
150 if (var == NULL)
151 return;
152 psref_release(psref, &var->lv_psref, lv_psref_class);
153 }
154
155 static inline bool
156 l2tp_heldref_variant(struct l2tp_variant *var)
157 {
158
159 return psref_held(&var->lv_psref, lv_psref_class);
160 }
161
162
163 /* Prototypes */
164 void l2tpattach(int);
165 void l2tpattach0(struct l2tp_softc *);
166 void l2tp_input(struct mbuf *, struct ifnet *);
167 int l2tp_ioctl(struct ifnet *, u_long, void *);
168
169 struct l2tp_variant *l2tp_lookup_session_ref(uint32_t, struct psref *);
170 int l2tp_check_nesting(struct ifnet *, struct mbuf *);
171
172 /* TODO IP_TCPMSS support */
173 #ifdef IP_TCPMSS
174 struct mbuf *l2tp_tcpmss_clamp(struct ifnet *, struct mbuf *);
175 #endif /* IP_TCPMSS */
176 #endif /* _KERNEL */
177
178 /*
179 * Locking notes:
180 * + l2tp_softc_list is protected by l2tp_list_lock (an adaptive mutex)
181 * l2tp_softc_list is list of all l2tp_softcs, and it is used to avoid
182 * unload while busy.
183 * + l2tp_hashed_list is protected by
184 * - l2tp_hash_lock (an adaptive mutex) for writer
185 * - pserialize for reader
186 * l2tp_hashed_list is hashed list of all l2tp_softcs, and it is used by
187 * input processing to find appropriate softc.
188 * + l2tp_softc->l2tp_var is protected by
189 * - l2tp_softc->l2tp_lock (an adaptive mutex) for writer
190 * - l2tp_var->lv_psref for reader
191 * l2tp_softc->l2tp_var is used for variant values while the l2tp tunnel
192 * exists.
193 * + struct l2tp_ro->lr_ro is protected by struct l2tp_ro->lr_lock.
194 * This lock is required to exclude softnet/0 lwp(such as output
195 * processing softint) and processing lwp(such as DAD timer processing).
196 *
197 * Locking order:
198 * - encap_lock => struct l2tp_softc->l2tp_lock
199 * Other mutexes must not hold simultaneously.
200 *
201 * NOTICE
202 * - l2tp_softc must not have a variant value while the l2tp tunnel exists.
203 * Such variant values must be in l2tp_softc->l2tp_var.
204 * - l2tp_softc->l2tp_var is modified like read-copy-update.
205 * So, once we dereference l2tp_softc->l2tp_var, we must
206 * keep the pointer during the same context. If we re-derefence
207 * l2tp_softc->l2tp_var, the l2tp_var may be other one because of
208 * concurrent writer processing.
209 */
210 #endif /* _NET_IF_L2TP_H_ */
211