2 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
44 #include <sys/syslog.h>
47 #include <net/ethernet.h>
49 #include <net/if_var.h>
50 #include <net/if_clone.h>
51 #include <net/if_types.h>
52 #include <net/netisr.h>
54 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_encap.h>
63 #include <machine/in_cksum.h>
64 #include <security/mac/mac_framework.h>
66 #define MEMTU (1500 - sizeof(struct mobhdr))
67 static const char mename[] = "me";
68 static MALLOC_DEFINE(M_IFME, mename, "Minimal Encapsulation for IP");
69 /* Minimal forwarding header RFC 2004 */
71 uint8_t mob_proto; /* protocol */
72 uint8_t mob_flags; /* flags */
73 #define MOB_FLAGS_SP 0x80 /* source present */
74 uint16_t mob_csum; /* header checksum */
75 struct in_addr mob_dst; /* original destination address */
76 struct in_addr mob_src; /* original source addr (optional) */
82 struct in_addr me_src;
83 struct in_addr me_dst;
85 CK_LIST_ENTRY(me_softc) chain;
86 CK_LIST_ENTRY(me_softc) srchash;
88 CK_LIST_HEAD(me_list, me_softc);
89 #define ME2IFP(sc) ((sc)->me_ifp)
90 #define ME_READY(sc) ((sc)->me_src.s_addr != 0)
91 #define ME_RLOCK_TRACKER struct epoch_tracker me_et
92 #define ME_RLOCK() epoch_enter_preempt(net_epoch_preempt, &me_et)
93 #define ME_RUNLOCK() epoch_exit_preempt(net_epoch_preempt, &me_et)
94 #define ME_WAIT() epoch_wait_preempt(net_epoch_preempt)
97 #define ME_HASH_SIZE (1 << 4)
99 VNET_DEFINE_STATIC(struct me_list *, me_hashtbl) = NULL;
100 VNET_DEFINE_STATIC(struct me_list *, me_srchashtbl) = NULL;
101 #define V_me_hashtbl VNET(me_hashtbl)
102 #define V_me_srchashtbl VNET(me_srchashtbl)
103 #define ME_HASH(src, dst) (V_me_hashtbl[\
104 me_hashval((src), (dst)) & (ME_HASH_SIZE - 1)])
105 #define ME_SRCHASH(src) (V_me_srchashtbl[\
106 fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (ME_HASH_SIZE - 1)])
108 static struct sx me_ioctl_sx;
109 SX_SYSINIT(me_ioctl_sx, &me_ioctl_sx, "me_ioctl");
111 static int me_clone_create(struct if_clone *, int, caddr_t);
112 static void me_clone_destroy(struct ifnet *);
113 VNET_DEFINE_STATIC(struct if_clone *, me_cloner);
114 #define V_me_cloner VNET(me_cloner)
116 static void me_qflush(struct ifnet *);
117 static int me_transmit(struct ifnet *, struct mbuf *);
118 static int me_ioctl(struct ifnet *, u_long, caddr_t);
119 static int me_output(struct ifnet *, struct mbuf *,
120 const struct sockaddr *, struct route *);
121 static int me_input(struct mbuf *, int, int, void *);
123 static int me_set_tunnel(struct me_softc *, in_addr_t, in_addr_t);
124 static void me_delete_tunnel(struct me_softc *);
126 SYSCTL_DECL(_net_link);
127 static SYSCTL_NODE(_net_link, IFT_TUNNEL, me, CTLFLAG_RW, 0,
128 "Minimal Encapsulation for IP (RFC 2004)");
130 #define MAX_ME_NEST 1
133 VNET_DEFINE_STATIC(int, max_me_nesting) = MAX_ME_NEST;
134 #define V_max_me_nesting VNET(max_me_nesting)
135 SYSCTL_INT(_net_link_me, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
136 &VNET_NAME(max_me_nesting), 0, "Max nested tunnels");
139 me_hashval(in_addr_t src, in_addr_t dst)
143 ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
144 return (fnv_32_buf(&dst, sizeof(dst), ret));
147 static struct me_list *
150 struct me_list *hash;
153 hash = malloc(sizeof(struct me_list) * ME_HASH_SIZE,
155 for (i = 0; i < ME_HASH_SIZE; i++)
156 CK_LIST_INIT(&hash[i]);
162 vnet_me_init(const void *unused __unused)
165 V_me_cloner = if_clone_simple(mename, me_clone_create,
166 me_clone_destroy, 0);
168 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
172 vnet_me_uninit(const void *unused __unused)
175 if (V_me_hashtbl != NULL) {
176 free(V_me_hashtbl, M_IFME);
179 free(V_me_srchashtbl, M_IFME);
181 if_clone_detach(V_me_cloner);
183 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
184 vnet_me_uninit, NULL);
187 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
191 sc = malloc(sizeof(struct me_softc), M_IFME, M_WAITOK | M_ZERO);
192 sc->me_fibnum = curthread->td_proc->p_fibnum;
193 ME2IFP(sc) = if_alloc(IFT_TUNNEL);
194 ME2IFP(sc)->if_softc = sc;
195 if_initname(ME2IFP(sc), mename, unit);
197 ME2IFP(sc)->if_mtu = MEMTU;;
198 ME2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
199 ME2IFP(sc)->if_output = me_output;
200 ME2IFP(sc)->if_ioctl = me_ioctl;
201 ME2IFP(sc)->if_transmit = me_transmit;
202 ME2IFP(sc)->if_qflush = me_qflush;
203 ME2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
204 ME2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
205 if_attach(ME2IFP(sc));
206 bpfattach(ME2IFP(sc), DLT_NULL, sizeof(u_int32_t));
211 me_clone_destroy(struct ifnet *ifp)
215 sx_xlock(&me_ioctl_sx);
217 me_delete_tunnel(sc);
220 ifp->if_softc = NULL;
221 sx_xunlock(&me_ioctl_sx);
229 me_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
231 struct ifreq *ifr = (struct ifreq *)data;
232 struct sockaddr_in *src, *dst;
238 if (ifr->ifr_mtu < 576)
240 ifp->if_mtu = ifr->ifr_mtu;
243 ifp->if_flags |= IFF_UP;
249 sx_xlock(&me_ioctl_sx);
258 src = &((struct in_aliasreq *)data)->ifra_addr;
259 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
260 if (src->sin_family != dst->sin_family ||
261 src->sin_family != AF_INET ||
262 src->sin_len != dst->sin_len ||
263 src->sin_len != sizeof(struct sockaddr_in)) {
267 if (src->sin_addr.s_addr == INADDR_ANY ||
268 dst->sin_addr.s_addr == INADDR_ANY) {
269 error = EADDRNOTAVAIL;
272 error = me_set_tunnel(sc, src->sin_addr.s_addr,
273 dst->sin_addr.s_addr);
276 me_delete_tunnel(sc);
278 case SIOCGIFPSRCADDR:
279 case SIOCGIFPDSTADDR:
281 error = EADDRNOTAVAIL;
284 src = (struct sockaddr_in *)&ifr->ifr_addr;
285 memset(src, 0, sizeof(*src));
286 src->sin_family = AF_INET;
287 src->sin_len = sizeof(*src);
289 case SIOCGIFPSRCADDR:
290 src->sin_addr = sc->me_src;
292 case SIOCGIFPDSTADDR:
293 src->sin_addr = sc->me_dst;
296 error = prison_if(curthread->td_ucred, sintosa(src));
298 memset(src, 0, sizeof(*src));
301 ifr->ifr_fib = sc->me_fibnum;
304 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
306 if (ifr->ifr_fib >= rt_numfibs)
309 sc->me_fibnum = ifr->ifr_fib;
316 sx_xunlock(&me_ioctl_sx);
321 me_lookup(const struct mbuf *m, int off, int proto, void **arg)
326 if (V_me_hashtbl == NULL)
329 MPASS(in_epoch(net_epoch_preempt));
330 ip = mtod(m, const struct ip *);
331 CK_LIST_FOREACH(sc, &ME_HASH(ip->ip_dst.s_addr,
332 ip->ip_src.s_addr), chain) {
333 if (sc->me_src.s_addr == ip->ip_dst.s_addr &&
334 sc->me_dst.s_addr == ip->ip_src.s_addr) {
335 if ((ME2IFP(sc)->if_flags & IFF_UP) == 0)
338 return (ENCAP_DRV_LOOKUP);
345 * Check that ingress address belongs to local host.
348 me_set_running(struct me_softc *sc)
351 if (in_localip(sc->me_src))
352 ME2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
354 ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
358 * ifaddr_event handler.
359 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
360 * source address spoofing.
363 me_srcaddr(void *arg __unused, const struct sockaddr *sa,
366 const struct sockaddr_in *sin;
369 /* Check that VNET is ready */
370 if (V_me_hashtbl == NULL)
373 MPASS(in_epoch(net_epoch_preempt));
374 sin = (const struct sockaddr_in *)sa;
375 CK_LIST_FOREACH(sc, &ME_SRCHASH(sin->sin_addr.s_addr), srchash) {
376 if (sc->me_src.s_addr != sin->sin_addr.s_addr)
383 me_set_tunnel(struct me_softc *sc, in_addr_t src, in_addr_t dst)
385 struct me_softc *tmp;
387 sx_assert(&me_ioctl_sx, SA_XLOCKED);
389 if (V_me_hashtbl == NULL) {
390 V_me_hashtbl = me_hashinit();
391 V_me_srchashtbl = me_hashinit();
394 if (sc->me_src.s_addr == src && sc->me_dst.s_addr == dst)
397 CK_LIST_FOREACH(tmp, &ME_HASH(src, dst), chain) {
400 if (tmp->me_src.s_addr == src &&
401 tmp->me_dst.s_addr == dst)
402 return (EADDRNOTAVAIL);
405 me_delete_tunnel(sc);
406 sc->me_dst.s_addr = dst;
407 sc->me_src.s_addr = src;
408 CK_LIST_INSERT_HEAD(&ME_HASH(src, dst), sc, chain);
409 CK_LIST_INSERT_HEAD(&ME_SRCHASH(src), sc, srchash);
412 if_link_state_change(ME2IFP(sc), LINK_STATE_UP);
417 me_delete_tunnel(struct me_softc *sc)
420 sx_assert(&me_ioctl_sx, SA_XLOCKED);
422 CK_LIST_REMOVE(sc, chain);
423 CK_LIST_REMOVE(sc, srchash);
426 sc->me_src.s_addr = 0;
427 sc->me_dst.s_addr = 0;
428 ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
429 if_link_state_change(ME2IFP(sc), LINK_STATE_DOWN);
434 me_in_cksum(uint16_t *p, int nwords)
440 sum = (sum >> 16) + (sum & 0xffff);
446 me_input(struct mbuf *m, int off, int proto, void *arg)
448 struct me_softc *sc = arg;
455 /* checks for short packets */
456 hlen = sizeof(struct mobhdr);
457 if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
458 hlen -= sizeof(struct in_addr);
459 if (m->m_len < sizeof(struct ip) + hlen)
460 m = m_pullup(m, sizeof(struct ip) + hlen);
463 mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
464 /* check for wrong flags */
465 if (mh->mob_flags & (~MOB_FLAGS_SP)) {
470 if (hlen != sizeof(struct mobhdr)) {
475 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
476 /* check mobile header checksum */
477 if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
482 mac_ifnet_create_mbuf(ifp, m);
484 ip = mtod(m, struct ip *);
485 ip->ip_dst = mh->mob_dst;
486 ip->ip_p = mh->mob_proto;
488 ip->ip_len = htons(m->m_pkthdr.len - hlen);
490 ip->ip_src = mh->mob_src;
491 memmove(mtodo(m, hlen), ip, sizeof(struct ip));
494 m->m_pkthdr.rcvif = ifp;
495 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
496 M_SETFIB(m, ifp->if_fib);
498 BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
499 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
500 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
501 if ((ifp->if_flags & IFF_MONITOR) != 0)
504 netisr_dispatch(NETISR_IP, m);
505 return (IPPROTO_DONE);
507 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
508 return (IPPROTO_DONE);
512 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
513 struct route *ro __unused)
517 if (dst->sa_family == AF_UNSPEC)
518 bcopy(dst->sa_data, &af, sizeof(af));
521 m->m_pkthdr.csum_data = af;
522 return (ifp->if_transmit(ifp, m));
525 #define MTAG_ME 1414491977
527 me_transmit(struct ifnet *ifp, struct mbuf *m)
534 int error, hlen, plen;
538 error = mac_ifnet_check_transmit(ifp, m);
544 if (sc == NULL || !ME_READY(sc) ||
545 (ifp->if_flags & IFF_MONITOR) != 0 ||
546 (ifp->if_flags & IFF_UP) == 0 ||
547 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
548 (error = if_tunnel_check_nesting(ifp, m, MTAG_ME,
549 V_max_me_nesting)) != 0) {
553 af = m->m_pkthdr.csum_data;
555 error = EAFNOSUPPORT;
559 if (m->m_len < sizeof(struct ip))
560 m = m_pullup(m, sizeof(struct ip));
565 ip = mtod(m, struct ip *);
566 /* Fragmented datagramms shouldn't be encapsulated */
567 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
572 mh.mob_proto = ip->ip_p;
573 mh.mob_src = ip->ip_src;
574 mh.mob_dst = ip->ip_dst;
575 if (in_hosteq(sc->me_src, ip->ip_src)) {
576 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
579 hlen = sizeof(struct mobhdr);
580 mh.mob_flags = MOB_FLAGS_SP;
582 BPF_MTAP2(ifp, &af, sizeof(af), m);
583 plen = m->m_pkthdr.len;
584 ip->ip_src = sc->me_src;
585 ip->ip_dst = sc->me_dst;
586 m->m_flags &= ~(M_BCAST|M_MCAST);
587 M_SETFIB(m, sc->me_fibnum);
588 M_PREPEND(m, hlen, M_NOWAIT);
593 if (m->m_len < sizeof(struct ip) + hlen)
594 m = m_pullup(m, sizeof(struct ip) + hlen);
599 memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
600 ip = mtod(m, struct ip *);
601 ip->ip_len = htons(m->m_pkthdr.len);
602 ip->ip_p = IPPROTO_MOBILE;
605 mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
606 bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
607 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
610 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
612 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
613 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
620 me_qflush(struct ifnet *ifp __unused)
625 static const struct srcaddrtab *me_srcaddrtab = NULL;
626 static const struct encaptab *ecookie = NULL;
627 static const struct encap_config me_encap_cfg = {
628 .proto = IPPROTO_MOBILE,
629 .min_length = sizeof(struct ip) + sizeof(struct mobhdr) -
631 .exact_match = ENCAP_DRV_LOOKUP,
637 memodevent(module_t mod, int type, void *data)
642 me_srcaddrtab = ip_encap_register_srcaddr(me_srcaddr,
644 ecookie = ip_encap_attach(&me_encap_cfg, NULL, M_WAITOK);
647 ip_encap_detach(ecookie);
648 ip_encap_unregister_srcaddr(me_srcaddrtab);
656 static moduledata_t me_mod = {
662 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
663 MODULE_VERSION(if_me, 1);