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