1 | /* $NetBSD: if_l2tp.h,v 1.6 2018/10/19 00:12:56 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 l2tp_req) |
48 | #define SIOCDL2TPSESSION _IOW('i', 152, struct l2tp_req) |
49 | #define SIOCSL2TPCOOKIE _IOW('i', 153, struct l2tp_req) |
50 | #define SIOCDL2TPCOOKIE _IOW('i', 154, struct l2tp_req) |
51 | #define SIOCSL2TPSTATE _IOW('i', 155, struct l2tp_req) |
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_ro { |
95 | struct route lr_ro; |
96 | kmutex_t *lr_lock; |
97 | }; |
98 | |
99 | struct l2tp_softc { |
100 | struct ethercom l2tp_ec; /* common area - must be at the top */ |
101 | /* to use ether_input(), we must have this */ |
102 | percpu_t *l2tp_ro_percpu; /* struct l2tp_ro */ |
103 | struct l2tp_variant *l2tp_var; /* |
104 | * reader must use l2tp_getref_variant() |
105 | * instead of direct dereference. |
106 | */ |
107 | kmutex_t l2tp_lock; /* writer lock for l2tp_var */ |
108 | pserialize_t l2tp_psz; |
109 | |
110 | LIST_ENTRY(l2tp_softc) l2tp_list; /* list of all l2tps */ |
111 | struct pslist_entry l2tp_hash; /* hashed list to lookup by session id */ |
112 | }; |
113 | |
114 | #define L2TP_ROUTE_TTL 10 |
115 | |
116 | #define L2TP_MTU (1280) /* Default MTU */ |
117 | #define L2TP_MTU_MIN (1280) /* Minimum MTU */ |
118 | #define L2TP_MTU_MAX (8192) /* Maximum MTU */ |
119 | |
120 | /* |
121 | * Get l2tp_variant from l2tp_softc. |
122 | * |
123 | * l2tp_variant itself is protected not to be freed by lv_psref. |
124 | * In contrast, sc->sc_var can be changed to NULL even if reader critical |
125 | * section. see l2tp_variant_update(). |
126 | * So, once a reader dereference sc->sc_var by this API, the reader must not |
127 | * re-dereference form sc->sc_var. |
128 | */ |
129 | static __inline struct l2tp_variant * |
130 | l2tp_getref_variant(struct l2tp_softc *sc, struct psref *psref) |
131 | { |
132 | struct l2tp_variant *var; |
133 | int s; |
134 | |
135 | s = pserialize_read_enter(); |
136 | var = sc->l2tp_var; |
137 | if (var == NULL) { |
138 | pserialize_read_exit(s); |
139 | return NULL; |
140 | } |
141 | membar_datadep_consumer(); |
142 | psref_acquire(psref, &var->lv_psref, lv_psref_class); |
143 | pserialize_read_exit(s); |
144 | |
145 | return var; |
146 | } |
147 | |
148 | static __inline void |
149 | l2tp_putref_variant(struct l2tp_variant *var, struct psref *psref) |
150 | { |
151 | |
152 | if (var == NULL) |
153 | return; |
154 | psref_release(psref, &var->lv_psref, lv_psref_class); |
155 | } |
156 | |
157 | static __inline bool |
158 | l2tp_heldref_variant(struct l2tp_variant *var) |
159 | { |
160 | |
161 | return psref_held(&var->lv_psref, lv_psref_class); |
162 | } |
163 | |
164 | |
165 | /* Prototypes */ |
166 | void l2tpattach(int); |
167 | int l2tpattach0(struct l2tp_softc *); |
168 | void l2tp_input(struct mbuf *, struct ifnet *); |
169 | int l2tp_ioctl(struct ifnet *, u_long, void *); |
170 | |
171 | struct l2tp_variant *l2tp_lookup_session_ref(uint32_t, struct psref *); |
172 | int l2tp_check_nesting(struct ifnet *, struct mbuf *); |
173 | |
174 | /* TODO IP_TCPMSS support */ |
175 | #ifdef IP_TCPMSS |
176 | struct mbuf *l2tp_tcpmss_clamp(struct ifnet *, struct mbuf *); |
177 | #endif /* IP_TCPMSS */ |
178 | #endif /* _KERNEL */ |
179 | |
180 | /* |
181 | * Locking notes: |
182 | * + l2tp_softc_list is protected by l2tp_list_lock (an adaptive mutex) |
183 | * l2tp_softc_list is list of all l2tp_softcs, and it is used to avoid |
184 | * unload while busy. |
185 | * + l2tp_hashed_list is protected by |
186 | * - l2tp_hash_lock (an adaptive mutex) for writer |
187 | * - pserialize for reader |
188 | * l2tp_hashed_list is hashed list of all l2tp_softcs, and it is used by |
189 | * input processing to find appropriate softc. |
190 | * + l2tp_softc->l2tp_var is protected by |
191 | * - l2tp_softc->l2tp_lock (an adaptive mutex) for writer |
192 | * - l2tp_var->lv_psref for reader |
193 | * l2tp_softc->l2tp_var is used for variant values while the l2tp tunnel |
194 | * exists. |
195 | * + struct l2tp_ro->lr_ro is protected by struct l2tp_ro->lr_lock. |
196 | * This lock is required to exclude softnet/0 lwp(such as output |
197 | * processing softint) and processing lwp(such as DAD timer processing). |
198 | * |
199 | * Locking order: |
200 | * - encap_lock => struct l2tp_softc->l2tp_lock |
201 | * Other mutexes must not hold simultaneously. |
202 | * |
203 | * NOTICE |
204 | * - l2tp_softc must not have a variant value while the l2tp tunnel exists. |
205 | * Such variant values must be in l2tp_softc->l2tp_var. |
206 | * - l2tp_softc->l2tp_var is modified like read-copy-update. |
207 | * So, once we dereference l2tp_softc->l2tp_var, we must |
208 | * keep the pointer during the same context. If we re-derefence |
209 | * l2tp_softc->l2tp_var, the l2tp_var may be other one because of |
210 | * concurrent writer processing. |
211 | */ |
212 | #endif /* _NET_IF_L2TP_H_ */ |
213 | |