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)
117 static void me_reassign(struct ifnet *, struct vnet *, char *);
119 static void me_qflush(struct ifnet *);
120 static int me_transmit(struct ifnet *, struct mbuf *);
121 static int me_ioctl(struct ifnet *, u_long, caddr_t);
122 static int me_output(struct ifnet *, struct mbuf *,
123 const struct sockaddr *, struct route *);
124 static int me_input(struct mbuf *, int, int, void *);
126 static int me_set_tunnel(struct me_softc *, in_addr_t, in_addr_t);
127 static void me_delete_tunnel(struct me_softc *);
129 SYSCTL_DECL(_net_link);
130 static SYSCTL_NODE(_net_link, IFT_TUNNEL, me, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
131 "Minimal Encapsulation for IP (RFC 2004)");
133 #define MAX_ME_NEST 1
136 VNET_DEFINE_STATIC(int, max_me_nesting) = MAX_ME_NEST;
137 #define V_max_me_nesting VNET(max_me_nesting)
138 SYSCTL_INT(_net_link_me, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
139 &VNET_NAME(max_me_nesting), 0, "Max nested tunnels");
142 me_hashval(in_addr_t src, in_addr_t dst)
146 ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
147 return (fnv_32_buf(&dst, sizeof(dst), ret));
150 static struct me_list *
153 struct me_list *hash;
156 hash = malloc(sizeof(struct me_list) * ME_HASH_SIZE,
158 for (i = 0; i < ME_HASH_SIZE; i++)
159 CK_LIST_INIT(&hash[i]);
165 vnet_me_init(const void *unused __unused)
168 V_me_cloner = if_clone_simple(mename, me_clone_create,
169 me_clone_destroy, 0);
171 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
175 vnet_me_uninit(const void *unused __unused)
178 if (V_me_hashtbl != NULL) {
179 free(V_me_hashtbl, M_IFME);
182 free(V_me_srchashtbl, M_IFME);
184 if_clone_detach(V_me_cloner);
186 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
187 vnet_me_uninit, NULL);
190 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
194 sc = malloc(sizeof(struct me_softc), M_IFME, M_WAITOK | M_ZERO);
195 sc->me_fibnum = curthread->td_proc->p_fibnum;
196 ME2IFP(sc) = if_alloc(IFT_TUNNEL);
197 ME2IFP(sc)->if_softc = sc;
198 if_initname(ME2IFP(sc), mename, unit);
200 ME2IFP(sc)->if_mtu = MEMTU;
201 ME2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
202 ME2IFP(sc)->if_output = me_output;
203 ME2IFP(sc)->if_ioctl = me_ioctl;
204 ME2IFP(sc)->if_transmit = me_transmit;
205 ME2IFP(sc)->if_qflush = me_qflush;
207 ME2IFP(sc)->if_reassign = me_reassign;
209 ME2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
210 ME2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
211 if_attach(ME2IFP(sc));
212 bpfattach(ME2IFP(sc), DLT_NULL, sizeof(u_int32_t));
218 me_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
219 char *unused __unused)
223 sx_xlock(&me_ioctl_sx);
226 me_delete_tunnel(sc);
227 sx_xunlock(&me_ioctl_sx);
232 me_clone_destroy(struct ifnet *ifp)
236 sx_xlock(&me_ioctl_sx);
238 me_delete_tunnel(sc);
241 ifp->if_softc = NULL;
242 sx_xunlock(&me_ioctl_sx);
250 me_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
252 struct ifreq *ifr = (struct ifreq *)data;
253 struct sockaddr_in *src, *dst;
259 if (ifr->ifr_mtu < 576)
261 ifp->if_mtu = ifr->ifr_mtu;
264 ifp->if_flags |= IFF_UP;
270 sx_xlock(&me_ioctl_sx);
279 src = &((struct in_aliasreq *)data)->ifra_addr;
280 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
281 if (src->sin_family != dst->sin_family ||
282 src->sin_family != AF_INET ||
283 src->sin_len != dst->sin_len ||
284 src->sin_len != sizeof(struct sockaddr_in)) {
288 if (src->sin_addr.s_addr == INADDR_ANY ||
289 dst->sin_addr.s_addr == INADDR_ANY) {
290 error = EADDRNOTAVAIL;
293 error = me_set_tunnel(sc, src->sin_addr.s_addr,
294 dst->sin_addr.s_addr);
297 me_delete_tunnel(sc);
299 case SIOCGIFPSRCADDR:
300 case SIOCGIFPDSTADDR:
302 error = EADDRNOTAVAIL;
305 src = (struct sockaddr_in *)&ifr->ifr_addr;
306 memset(src, 0, sizeof(*src));
307 src->sin_family = AF_INET;
308 src->sin_len = sizeof(*src);
310 case SIOCGIFPSRCADDR:
311 src->sin_addr = sc->me_src;
313 case SIOCGIFPDSTADDR:
314 src->sin_addr = sc->me_dst;
317 error = prison_if(curthread->td_ucred, sintosa(src));
319 memset(src, 0, sizeof(*src));
322 ifr->ifr_fib = sc->me_fibnum;
325 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
327 if (ifr->ifr_fib >= rt_numfibs)
330 sc->me_fibnum = ifr->ifr_fib;
337 sx_xunlock(&me_ioctl_sx);
342 me_lookup(const struct mbuf *m, int off, int proto, void **arg)
347 if (V_me_hashtbl == NULL)
351 ip = mtod(m, const struct ip *);
352 CK_LIST_FOREACH(sc, &ME_HASH(ip->ip_dst.s_addr,
353 ip->ip_src.s_addr), chain) {
354 if (sc->me_src.s_addr == ip->ip_dst.s_addr &&
355 sc->me_dst.s_addr == ip->ip_src.s_addr) {
356 if ((ME2IFP(sc)->if_flags & IFF_UP) == 0)
359 return (ENCAP_DRV_LOOKUP);
366 * Check that ingress address belongs to local host.
369 me_set_running(struct me_softc *sc)
372 if (in_localip(sc->me_src))
373 ME2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
375 ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
379 * ifaddr_event handler.
380 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
381 * source address spoofing.
384 me_srcaddr(void *arg __unused, const struct sockaddr *sa,
387 const struct sockaddr_in *sin;
390 /* Check that VNET is ready */
391 if (V_me_hashtbl == NULL)
395 sin = (const struct sockaddr_in *)sa;
396 CK_LIST_FOREACH(sc, &ME_SRCHASH(sin->sin_addr.s_addr), srchash) {
397 if (sc->me_src.s_addr != sin->sin_addr.s_addr)
404 me_set_tunnel(struct me_softc *sc, in_addr_t src, in_addr_t dst)
406 struct me_softc *tmp;
408 sx_assert(&me_ioctl_sx, SA_XLOCKED);
410 if (V_me_hashtbl == NULL) {
411 V_me_hashtbl = me_hashinit();
412 V_me_srchashtbl = me_hashinit();
415 if (sc->me_src.s_addr == src && sc->me_dst.s_addr == dst)
418 CK_LIST_FOREACH(tmp, &ME_HASH(src, dst), chain) {
421 if (tmp->me_src.s_addr == src &&
422 tmp->me_dst.s_addr == dst)
423 return (EADDRNOTAVAIL);
426 me_delete_tunnel(sc);
427 sc->me_dst.s_addr = dst;
428 sc->me_src.s_addr = src;
429 CK_LIST_INSERT_HEAD(&ME_HASH(src, dst), sc, chain);
430 CK_LIST_INSERT_HEAD(&ME_SRCHASH(src), sc, srchash);
433 if_link_state_change(ME2IFP(sc), LINK_STATE_UP);
438 me_delete_tunnel(struct me_softc *sc)
441 sx_assert(&me_ioctl_sx, SA_XLOCKED);
443 CK_LIST_REMOVE(sc, chain);
444 CK_LIST_REMOVE(sc, srchash);
447 sc->me_src.s_addr = 0;
448 sc->me_dst.s_addr = 0;
449 ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
450 if_link_state_change(ME2IFP(sc), LINK_STATE_DOWN);
455 me_in_cksum(uint16_t *p, int nwords)
461 sum = (sum >> 16) + (sum & 0xffff);
467 me_input(struct mbuf *m, int off, int proto, void *arg)
469 struct me_softc *sc = arg;
478 /* checks for short packets */
479 hlen = sizeof(struct mobhdr);
480 if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
481 hlen -= sizeof(struct in_addr);
482 if (m->m_len < sizeof(struct ip) + hlen)
483 m = m_pullup(m, sizeof(struct ip) + hlen);
486 mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
487 /* check for wrong flags */
488 if (mh->mob_flags & (~MOB_FLAGS_SP)) {
493 if (hlen != sizeof(struct mobhdr)) {
498 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
499 /* check mobile header checksum */
500 if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
505 mac_ifnet_create_mbuf(ifp, m);
507 ip = mtod(m, struct ip *);
508 ip->ip_dst = mh->mob_dst;
509 ip->ip_p = mh->mob_proto;
511 ip->ip_len = htons(m->m_pkthdr.len - hlen);
513 ip->ip_src = mh->mob_src;
514 memmove(mtodo(m, hlen), ip, sizeof(struct ip));
517 m->m_pkthdr.rcvif = ifp;
518 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
519 M_SETFIB(m, ifp->if_fib);
521 BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
522 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
523 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
524 if ((ifp->if_flags & IFF_MONITOR) != 0)
527 netisr_dispatch(NETISR_IP, m);
528 return (IPPROTO_DONE);
530 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
531 return (IPPROTO_DONE);
535 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
536 struct route *ro __unused)
540 if (dst->sa_family == AF_UNSPEC)
541 bcopy(dst->sa_data, &af, sizeof(af));
544 m->m_pkthdr.csum_data = af;
545 return (ifp->if_transmit(ifp, m));
548 #define MTAG_ME 1414491977
550 me_transmit(struct ifnet *ifp, struct mbuf *m)
557 int error, hlen, plen;
561 error = mac_ifnet_check_transmit(ifp, m);
567 if (sc == NULL || !ME_READY(sc) ||
568 (ifp->if_flags & IFF_MONITOR) != 0 ||
569 (ifp->if_flags & IFF_UP) == 0 ||
570 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
571 (error = if_tunnel_check_nesting(ifp, m, MTAG_ME,
572 V_max_me_nesting)) != 0) {
576 af = m->m_pkthdr.csum_data;
578 error = EAFNOSUPPORT;
582 if (m->m_len < sizeof(struct ip))
583 m = m_pullup(m, sizeof(struct ip));
588 ip = mtod(m, struct ip *);
589 /* Fragmented datagramms shouldn't be encapsulated */
590 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
595 mh.mob_proto = ip->ip_p;
596 mh.mob_src = ip->ip_src;
597 mh.mob_dst = ip->ip_dst;
598 if (in_hosteq(sc->me_src, ip->ip_src)) {
599 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
602 hlen = sizeof(struct mobhdr);
603 mh.mob_flags = MOB_FLAGS_SP;
605 BPF_MTAP2(ifp, &af, sizeof(af), m);
606 plen = m->m_pkthdr.len;
607 ip->ip_src = sc->me_src;
608 ip->ip_dst = sc->me_dst;
609 m->m_flags &= ~(M_BCAST|M_MCAST);
610 M_SETFIB(m, sc->me_fibnum);
611 M_PREPEND(m, hlen, M_NOWAIT);
616 if (m->m_len < sizeof(struct ip) + hlen)
617 m = m_pullup(m, sizeof(struct ip) + hlen);
622 memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
623 ip = mtod(m, struct ip *);
624 ip->ip_len = htons(m->m_pkthdr.len);
625 ip->ip_p = IPPROTO_MOBILE;
628 mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
629 bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
630 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
633 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
635 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
636 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
643 me_qflush(struct ifnet *ifp __unused)
648 static const struct srcaddrtab *me_srcaddrtab = NULL;
649 static const struct encaptab *ecookie = NULL;
650 static const struct encap_config me_encap_cfg = {
651 .proto = IPPROTO_MOBILE,
652 .min_length = sizeof(struct ip) + sizeof(struct mobhdr) -
654 .exact_match = ENCAP_DRV_LOOKUP,
660 memodevent(module_t mod, int type, void *data)
665 me_srcaddrtab = ip_encap_register_srcaddr(me_srcaddr,
667 ecookie = ip_encap_attach(&me_encap_cfg, NULL, M_WAITOK);
670 ip_encap_detach(ecookie);
671 ip_encap_unregister_srcaddr(me_srcaddrtab);
679 static moduledata_t me_mod = {
685 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
686 MODULE_VERSION(if_me, 1);