1 /* $NetBSD: in_l2tp.c,v 1.15 2018/06/21 10:37:50 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 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: in_l2tp.c,v 1.15 2018/06/21 10:37:50 knakahara Exp $"); 31 32 #ifdef _KERNEL_OPT 33 #include "opt_l2tp.h" 34 #endif 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/socket.h> 39 #include <sys/sockio.h> 40 #include <sys/mbuf.h> 41 #include <sys/errno.h> 42 #include <sys/ioctl.h> 43 #include <sys/syslog.h> 44 #include <sys/kernel.h> 45 #include <sys/socketvar.h> /* For softnet_lock */ 46 47 #include <net/if.h> 48 #include <net/route.h> 49 #include <net/if_ether.h> 50 51 #include <netinet/in.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/ip.h> 54 #include <netinet/ip_var.h> 55 #include <netinet/ip_private.h> 56 #include <netinet/in_l2tp.h> 57 #include <netinet/in_var.h> 58 #include <netinet/ip_encap.h> 59 60 #ifdef ALTQ 61 #include <altq/altq.h> 62 #endif 63 64 /* TODO: IP_TCPMSS support */ 65 #undef IP_TCPMSS 66 #ifdef IP_TCPMSS 67 #include <netinet/ip_tcpmss.h> 68 #endif 69 70 #include <net/if_l2tp.h> 71 72 int ip_l2tp_ttl = L2TP_TTL; 73 74 static void in_l2tp_input(struct mbuf *, int, int, void *); 75 76 static const struct encapsw in_l2tp_encapsw = { 77 .encapsw4 = { 78 .pr_input = in_l2tp_input, 79 .pr_ctlinput = NULL, 80 } 81 }; 82 83 static int in_l2tp_match(struct mbuf *, int, int, void *); 84 85 int 86 in_l2tp_output(struct l2tp_variant *var, struct mbuf *m) 87 { 88 struct l2tp_softc *sc; 89 struct ifnet *ifp; 90 struct sockaddr_in *sin_src = satosin(var->lv_psrc); 91 struct sockaddr_in *sin_dst = satosin(var->lv_pdst); 92 struct ip iphdr; /* capsule IP header, host byte ordered */ 93 struct rtentry *rt; 94 struct l2tp_ro *lro; 95 int error; 96 uint32_t sess_id; 97 98 KASSERT(var != NULL); 99 KASSERT(l2tp_heldref_variant(var)); 100 KASSERT(sin_src != NULL && sin_dst != NULL); 101 KASSERT(sin_src->sin_family == AF_INET 102 && sin_dst->sin_family == AF_INET); 103 104 sc = var->lv_softc; 105 ifp = &sc->l2tp_ec.ec_if; 106 error = l2tp_check_nesting(ifp, m); 107 if (error) { 108 m_freem(m); 109 goto looped; 110 } 111 112 /* bidirectional configured tunnel mode */ 113 if (sin_dst->sin_addr.s_addr == INADDR_ANY) { 114 m_freem(m); 115 if ((ifp->if_flags & IFF_DEBUG) != 0) 116 log(LOG_DEBUG, "%s: ENETUNREACH\n", __func__); 117 error = ENETUNREACH; 118 goto out; 119 } 120 121 #ifdef NOTYET 122 /* TODO: support ALTQ for innner frame */ 123 #ifdef ALTQ 124 ALTQ_SAVE_PAYLOAD(m, AF_ETHER); 125 #endif 126 #endif 127 128 memset(&iphdr, 0, sizeof(iphdr)); 129 iphdr.ip_src = sin_src->sin_addr; 130 iphdr.ip_dst = sin_dst->sin_addr; 131 iphdr.ip_p = IPPROTO_L2TP; 132 /* version will be set in ip_output() */ 133 iphdr.ip_ttl = ip_l2tp_ttl; 134 /* outer IP header length */ 135 iphdr.ip_len = sizeof(struct ip); 136 /* session-id length */ 137 iphdr.ip_len += sizeof(uint32_t); 138 if (var->lv_use_cookie == L2TP_COOKIE_ON) { 139 /* cookie length */ 140 iphdr.ip_len += var->lv_peer_cookie_len; 141 } 142 143 /* TODO: IP_TCPMSS support */ 144 #ifdef IP_TCPMSS 145 m = l2tp_tcpmss_clamp(ifp, m); 146 if (m == NULL) { 147 error = EINVAL; 148 goto out; 149 } 150 #endif 151 152 /* 153 * Payload length. 154 * 155 * NOTE: payload length may be changed in ip_tcpmss(). Typical case 156 * is missing of TCP mss option in original TCP header. 157 */ 158 iphdr.ip_len += m->m_pkthdr.len; 159 HTONS(iphdr.ip_len); 160 161 if (var->lv_use_cookie == L2TP_COOKIE_ON) { 162 /* prepend session cookie */ 163 uint32_t cookie_32; 164 uint64_t cookie_64; 165 M_PREPEND(m, var->lv_peer_cookie_len, M_DONTWAIT); 166 if (m && m->m_len < var->lv_peer_cookie_len) 167 m = m_pullup(m, var->lv_peer_cookie_len); 168 if (m == NULL) { 169 error = ENOBUFS; 170 goto out; 171 } 172 if (var->lv_peer_cookie_len == 4) { 173 cookie_32 = htonl((uint32_t)var->lv_peer_cookie); 174 memcpy(mtod(m, void *), &cookie_32, sizeof(uint32_t)); 175 } else { 176 cookie_64 = htobe64(var->lv_peer_cookie); 177 memcpy(mtod(m, void *), &cookie_64, sizeof(uint64_t)); 178 } 179 } 180 181 /* prepend session-ID */ 182 sess_id = htonl(var->lv_peer_sess_id); 183 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT); 184 if (m && m->m_len < sizeof(uint32_t)) 185 m = m_pullup(m, sizeof(uint32_t)); 186 if (m == NULL) { 187 error = ENOBUFS; 188 goto out; 189 } 190 memcpy(mtod(m, uint32_t *), &sess_id, sizeof(uint32_t)); 191 192 /* prepend new IP header */ 193 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 194 if (m == NULL) { 195 error = ENOBUFS; 196 goto out; 197 } 198 if (IP_HDR_ALIGNED_P(mtod(m, void *)) == 0) { 199 m = m_copyup(m, sizeof(struct ip), 0); 200 } else { 201 if (m->m_len < sizeof(struct ip)) 202 m = m_pullup(m, sizeof(struct ip)); 203 } 204 if (m == NULL) { 205 error = ENOBUFS; 206 goto out; 207 } 208 memcpy(mtod(m, struct ip *), &iphdr, sizeof(struct ip)); 209 210 lro = percpu_getref(sc->l2tp_ro_percpu); 211 mutex_enter(lro->lr_lock); 212 if ((rt = rtcache_lookup(&lro->lr_ro, var->lv_pdst)) == NULL) { 213 mutex_exit(lro->lr_lock); 214 percpu_putref(sc->l2tp_ro_percpu); 215 m_freem(m); 216 error = ENETUNREACH; 217 goto out; 218 } 219 220 if (rt->rt_ifp == ifp) { 221 rtcache_unref(rt, &lro->lr_ro); 222 rtcache_free(&lro->lr_ro); 223 mutex_exit(lro->lr_lock); 224 percpu_putref(sc->l2tp_ro_percpu); 225 m_freem(m); 226 error = ENETUNREACH; /*XXX*/ 227 goto out; 228 } 229 rtcache_unref(rt, &lro->lr_ro); 230 231 /* 232 * To avoid inappropriate rewrite of checksum, 233 * clear csum flags. 234 */ 235 m->m_pkthdr.csum_flags = 0; 236 237 error = ip_output(m, NULL, &lro->lr_ro, 0, NULL, NULL); 238 mutex_exit(lro->lr_lock); 239 percpu_putref(sc->l2tp_ro_percpu); 240 return error; 241 242 looped: 243 if (error) 244 ifp->if_oerrors++; 245 246 out: 247 return error; 248 } 249 250 static void 251 in_l2tp_input(struct mbuf *m, int off, int proto, void *eparg __unused) 252 { 253 struct ifnet *l2tpp = NULL; 254 struct l2tp_softc *sc; 255 uint32_t sess_id; 256 uint32_t cookie_32; 257 uint64_t cookie_64; 258 struct psref psref; 259 struct l2tp_variant *var; 260 261 KASSERT((m->m_flags & M_PKTHDR) != 0); 262 263 if (m->m_pkthdr.len < off + sizeof(uint32_t)) { 264 m_freem(m); 265 return; 266 } 267 268 /* get L2TP session ID */ 269 m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id); 270 NTOHL(sess_id); 271 #ifdef L2TP_DEBUG 272 log(LOG_DEBUG, "%s: sess_id = %" PRIu32 "\n", __func__, sess_id); 273 #endif 274 if (sess_id == 0) { 275 /* 276 * L2TPv3 control packet received. 277 * userland daemon(l2tpd?) should process. 278 */ 279 SOFTNET_LOCK_IF_NET_MPSAFE(); 280 rip_input(m, off, proto); 281 SOFTNET_UNLOCK_IF_NET_MPSAFE(); 282 return; 283 } 284 285 var = l2tp_lookup_session_ref(sess_id, &psref); 286 if (var == NULL) { 287 m_freem(m); 288 ip_statinc(IP_STAT_NOL2TP); 289 return; 290 } 291 292 sc = var->lv_softc; 293 l2tpp = &(sc->l2tp_ec.ec_if); 294 295 if (l2tpp == NULL || (l2tpp->if_flags & IFF_UP) == 0) { 296 #ifdef L2TP_DEBUG 297 if (l2tpp == NULL) 298 log(LOG_DEBUG, "%s: l2tpp is NULL\n", __func__); 299 else 300 log(LOG_DEBUG, "%s: l2tpp is down\n", __func__); 301 #endif 302 m_freem(m); 303 ip_statinc(IP_STAT_NOL2TP); 304 goto out; 305 } 306 307 /* other CPU did l2tp_delete_tunnel */ 308 if (var->lv_psrc == NULL || var->lv_pdst == NULL) { 309 m_freem(m); 310 ip_statinc(IP_STAT_NOL2TP); 311 goto out; 312 } 313 314 if (var->lv_state != L2TP_STATE_UP) { 315 m_freem(m); 316 goto out; 317 } 318 319 m_adj(m, off + sizeof(uint32_t)); 320 321 if (var->lv_use_cookie == L2TP_COOKIE_ON) { 322 if (m->m_pkthdr.len < var->lv_my_cookie_len) { 323 m_freem(m); 324 goto out; 325 } 326 if (var->lv_my_cookie_len == 4) { 327 m_copydata(m, 0, sizeof(uint32_t), (void *)&cookie_32); 328 NTOHL(cookie_32); 329 if (cookie_32 != var->lv_my_cookie) { 330 m_freem(m); 331 goto out; 332 } 333 m_adj(m, sizeof(uint32_t)); 334 } else { 335 m_copydata(m, 0, sizeof(uint64_t), (void *)&cookie_64); 336 BE64TOH(cookie_64); 337 if (cookie_64 != var->lv_my_cookie) { 338 m_freem(m); 339 goto out; 340 } 341 m_adj(m, sizeof(uint64_t)); 342 } 343 } 344 345 /* TODO: IP_TCPMSS support */ 346 #ifdef IP_TCPMSS 347 m = l2tp_tcpmss_clamp(l2tpp, m); 348 if (m == NULL) 349 goto out; 350 #endif 351 l2tp_input(m, l2tpp); 352 353 out: 354 l2tp_putref_variant(var, &psref); 355 return; 356 } 357 358 /* 359 * This function is used by encap4_lookup() to decide priority of the encaptab. 360 * This priority is compared to the match length between mbuf's source/destination 361 * IPv4 address pair and encaptab's one. 362 * l2tp(4) does not use address pairs to search matched encaptab, so this 363 * function must return the length bigger than or equals to IPv4 address pair to 364 * avoid wrong encaptab. 365 */ 366 static int 367 in_l2tp_match(struct mbuf *m, int off, int proto, void *arg) 368 { 369 struct l2tp_variant *var = arg; 370 uint32_t sess_id; 371 372 KASSERT(proto == IPPROTO_L2TP); 373 374 /* 375 * If the packet contains no session ID it cannot match 376 */ 377 if (m_length(m) < off + sizeof(uint32_t)) 378 return 0; 379 380 /* get L2TP session ID */ 381 m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id); 382 NTOHL(sess_id); 383 if (sess_id == 0) { 384 /* 385 * L2TPv3 control packet received. 386 * userland daemon(l2tpd?) should process. 387 */ 388 return 32 * 2; 389 } else if (sess_id == var->lv_my_sess_id) 390 return 32 * 2; 391 else 392 return 0; 393 } 394 395 int 396 in_l2tp_attach(struct l2tp_variant *var) 397 { 398 399 var->lv_encap_cookie = encap_attach_func(AF_INET, IPPROTO_L2TP, 400 in_l2tp_match, &in_l2tp_encapsw, var); 401 if (var->lv_encap_cookie == NULL) 402 return EEXIST; 403 404 return 0; 405 } 406 407 int 408 in_l2tp_detach(struct l2tp_variant *var) 409 { 410 int error; 411 412 error = encap_detach(var->lv_encap_cookie); 413 if (error == 0) 414 var->lv_encap_cookie = NULL; 415 416 return error; 417 } 418