2 * Copyright (c) 2014 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>
32 #include <sys/kernel.h>
34 #include <sys/libkern.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
40 #include <sys/protosw.h>
41 #include <sys/rmlock.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/systm.h>
50 #include <net/ethernet.h>
52 #include <net/if_var.h>
53 #include <net/if_clone.h>
54 #include <net/if_types.h>
55 #include <net/netisr.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/ip_encap.h>
66 #include <machine/in_cksum.h>
67 #include <security/mac/mac_framework.h>
70 static const char mename[] = "me";
71 static MALLOC_DEFINE(M_IFME, mename, "Minimal Encapsulation for IP");
72 static VNET_DEFINE(struct mtx, me_mtx);
73 #define V_me_mtx VNET(me_mtx)
74 /* Minimal forwarding header RFC 2004 */
76 uint8_t mob_proto; /* protocol */
77 uint8_t mob_flags; /* flags */
78 #define MOB_FLAGS_SP 0x80 /* source present */
79 uint16_t mob_csum; /* header checksum */
80 struct in_addr mob_dst; /* original destination address */
81 struct in_addr mob_src; /* original source addr (optional) */
86 LIST_ENTRY(me_softc) me_list;
87 struct rmlock me_lock;
89 const struct encaptab *me_ecookie;
90 struct in_addr me_src;
91 struct in_addr me_dst;
93 #define ME2IFP(sc) ((sc)->me_ifp)
94 #define ME_READY(sc) ((sc)->me_src.s_addr != 0)
95 #define ME_LOCK_INIT(sc) rm_init(&(sc)->me_lock, "me softc")
96 #define ME_LOCK_DESTROY(sc) rm_destroy(&(sc)->me_lock)
97 #define ME_RLOCK_TRACKER struct rm_priotracker me_tracker
98 #define ME_RLOCK(sc) rm_rlock(&(sc)->me_lock, &me_tracker)
99 #define ME_RUNLOCK(sc) rm_runlock(&(sc)->me_lock, &me_tracker)
100 #define ME_RLOCK_ASSERT(sc) rm_assert(&(sc)->me_lock, RA_RLOCKED)
101 #define ME_WLOCK(sc) rm_wlock(&(sc)->me_lock)
102 #define ME_WUNLOCK(sc) rm_wunlock(&(sc)->me_lock)
103 #define ME_WLOCK_ASSERT(sc) rm_assert(&(sc)->me_lock, RA_WLOCKED)
105 #define ME_LIST_LOCK_INIT(x) mtx_init(&V_me_mtx, "me_mtx", NULL, MTX_DEF)
106 #define ME_LIST_LOCK_DESTROY(x) mtx_destroy(&V_me_mtx)
107 #define ME_LIST_LOCK(x) mtx_lock(&V_me_mtx)
108 #define ME_LIST_UNLOCK(x) mtx_unlock(&V_me_mtx)
110 static VNET_DEFINE(LIST_HEAD(, me_softc), me_softc_list);
111 #define V_me_softc_list VNET(me_softc_list)
112 static struct sx me_ioctl_sx;
113 SX_SYSINIT(me_ioctl_sx, &me_ioctl_sx, "me_ioctl");
115 static int me_clone_create(struct if_clone *, int, caddr_t);
116 static void me_clone_destroy(struct ifnet *);
117 static VNET_DEFINE(struct if_clone *, me_cloner);
118 #define V_me_cloner VNET(me_cloner)
120 static void me_qflush(struct ifnet *);
121 static int me_transmit(struct ifnet *, struct mbuf *);
122 static int me_ioctl(struct ifnet *, u_long, caddr_t);
123 static int me_output(struct ifnet *, struct mbuf *,
124 const struct sockaddr *, struct route *);
125 static int me_input(struct mbuf **, int *, int);
127 static int me_set_tunnel(struct ifnet *, struct sockaddr_in *,
128 struct sockaddr_in *);
129 static void me_delete_tunnel(struct ifnet *);
131 SYSCTL_DECL(_net_link);
132 static SYSCTL_NODE(_net_link, IFT_TUNNEL, me, CTLFLAG_RW, 0,
133 "Minimal Encapsulation for IP (RFC 2004)");
135 #define MAX_ME_NEST 1
138 static VNET_DEFINE(int, max_me_nesting) = MAX_ME_NEST;
139 #define V_max_me_nesting VNET(max_me_nesting)
140 SYSCTL_INT(_net_link_me, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
141 &VNET_NAME(max_me_nesting), 0, "Max nested tunnels");
143 extern struct domain inetdomain;
144 static void me_input10(struct mbuf *, int);
145 static const struct protosw in_mobile_protosw = {
147 .pr_domain = &inetdomain,
148 .pr_protocol = IPPROTO_MOBILE,
149 .pr_flags = PR_ATOMIC|PR_ADDR,
150 .pr_input = me_input10,
151 .pr_output = (pr_output_t *)rip_output,
152 .pr_ctlinput = rip_ctlinput,
153 .pr_ctloutput = rip_ctloutput,
154 .pr_usrreqs = &rip_usrreqs
158 vnet_me_init(const void *unused __unused)
160 LIST_INIT(&V_me_softc_list);
162 V_me_cloner = if_clone_simple(mename, me_clone_create,
163 me_clone_destroy, 0);
165 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
169 vnet_me_uninit(const void *unused __unused)
172 if_clone_detach(V_me_cloner);
173 ME_LIST_LOCK_DESTROY();
175 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
176 vnet_me_uninit, NULL);
179 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
183 sc = malloc(sizeof(struct me_softc), M_IFME, M_WAITOK | M_ZERO);
184 sc->me_fibnum = curthread->td_proc->p_fibnum;
185 ME2IFP(sc) = if_alloc(IFT_TUNNEL);
187 ME2IFP(sc)->if_softc = sc;
188 if_initname(ME2IFP(sc), mename, unit);
190 ME2IFP(sc)->if_mtu = MEMTU - sizeof(struct mobhdr);
191 ME2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
192 ME2IFP(sc)->if_output = me_output;
193 ME2IFP(sc)->if_ioctl = me_ioctl;
194 ME2IFP(sc)->if_transmit = me_transmit;
195 ME2IFP(sc)->if_qflush = me_qflush;
196 ME2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
197 ME2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
198 if_attach(ME2IFP(sc));
199 bpfattach(ME2IFP(sc), DLT_NULL, sizeof(u_int32_t));
201 LIST_INSERT_HEAD(&V_me_softc_list, sc, me_list);
207 me_clone_destroy(struct ifnet *ifp)
211 sx_xlock(&me_ioctl_sx);
213 me_delete_tunnel(ifp);
215 LIST_REMOVE(sc, me_list);
219 ifp->if_softc = NULL;
220 sx_xunlock(&me_ioctl_sx);
228 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 - sizeof(struct mobhdr);
243 ifp->if_flags |= IFF_UP;
249 sx_xlock(&me_ioctl_sx);
258 src = (struct sockaddr_in *)
259 &(((struct in_aliasreq *)data)->ifra_addr);
260 dst = (struct sockaddr_in *)
261 &(((struct in_aliasreq *)data)->ifra_dstaddr);
262 if (src->sin_family != dst->sin_family ||
263 src->sin_family != AF_INET ||
264 src->sin_len != dst->sin_len ||
265 src->sin_len != sizeof(struct sockaddr_in)) {
269 if (src->sin_addr.s_addr == INADDR_ANY ||
270 dst->sin_addr.s_addr == INADDR_ANY) {
271 error = EADDRNOTAVAIL;
274 error = me_set_tunnel(ifp, src, dst);
277 me_delete_tunnel(ifp);
279 case SIOCGIFPSRCADDR:
280 case SIOCGIFPDSTADDR:
283 error = EADDRNOTAVAIL;
287 src = (struct sockaddr_in *)&ifr->ifr_addr;
288 memset(src, 0, sizeof(*src));
289 src->sin_family = AF_INET;
290 src->sin_len = sizeof(*src);
292 case SIOCGIFPSRCADDR:
293 src->sin_addr = sc->me_src;
295 case SIOCGIFPDSTADDR:
296 src->sin_addr = sc->me_dst;
300 error = prison_if(curthread->td_ucred, sintosa(src));
302 memset(src, 0, sizeof(*src));
305 ifr->ifr_fib = sc->me_fibnum;
308 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
310 if (ifr->ifr_fib >= rt_numfibs)
313 sc->me_fibnum = ifr->ifr_fib;
320 sx_xunlock(&me_ioctl_sx);
325 me_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
332 sc = (struct me_softc *)arg;
333 if ((ME2IFP(sc)->if_flags & IFF_UP) == 0)
338 if (m->m_pkthdr.len < sizeof(struct ip) + sizeof(struct mobhdr) -
339 sizeof(struct in_addr))
345 ip = mtod(m, struct ip *);
346 if (sc->me_src.s_addr == ip->ip_dst.s_addr &&
347 sc->me_dst.s_addr == ip->ip_src.s_addr)
355 me_set_tunnel(struct ifnet *ifp, struct sockaddr_in *src,
356 struct sockaddr_in *dst)
358 struct me_softc *sc, *tsc;
360 sx_assert(&me_ioctl_sx, SA_XLOCKED);
363 LIST_FOREACH(tsc, &V_me_softc_list, me_list) {
364 if (tsc == sc || !ME_READY(tsc))
366 if (tsc->me_src.s_addr == src->sin_addr.s_addr &&
367 tsc->me_dst.s_addr == dst->sin_addr.s_addr) {
369 return (EADDRNOTAVAIL);
375 sc->me_dst = dst->sin_addr;
376 sc->me_src = src->sin_addr;
379 if (sc->me_ecookie == NULL)
380 sc->me_ecookie = encap_attach_func(AF_INET, IPPROTO_MOBILE,
381 me_encapcheck, &in_mobile_protosw, sc);
382 if (sc->me_ecookie != NULL) {
383 ifp->if_drv_flags |= IFF_DRV_RUNNING;
384 if_link_state_change(ifp, LINK_STATE_UP);
390 me_delete_tunnel(struct ifnet *ifp)
392 struct me_softc *sc = ifp->if_softc;
394 sx_assert(&me_ioctl_sx, SA_XLOCKED);
395 if (sc->me_ecookie != NULL)
396 encap_detach(sc->me_ecookie);
397 sc->me_ecookie = NULL;
399 sc->me_src.s_addr = 0;
400 sc->me_dst.s_addr = 0;
402 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
403 if_link_state_change(ifp, LINK_STATE_DOWN);
407 me_in_cksum(uint16_t *p, int nwords)
413 sum = (sum >> 16) + (sum & 0xffff);
419 me_input10(struct mbuf *m, int off)
423 proto = (mtod(m, struct ip *))->ip_p;
424 me_input(&m, &off, proto);
428 me_input(struct mbuf **mp, int *offp, int proto)
438 sc = encap_getarg(m);
439 KASSERT(sc != NULL, ("encap_getarg returned NULL"));
442 /* checks for short packets */
443 hlen = sizeof(struct mobhdr);
444 if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
445 hlen -= sizeof(struct in_addr);
446 if (m->m_len < sizeof(struct ip) + hlen)
447 m = m_pullup(m, sizeof(struct ip) + hlen);
450 mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
451 /* check for wrong flags */
452 if (mh->mob_flags & (~MOB_FLAGS_SP)) {
457 if (hlen != sizeof(struct mobhdr)) {
462 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
463 /* check mobile header checksum */
464 if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
469 mac_ifnet_create_mbuf(ifp, m);
471 ip = mtod(m, struct ip *);
472 ip->ip_dst = mh->mob_dst;
473 ip->ip_p = mh->mob_proto;
475 ip->ip_len = htons(m->m_pkthdr.len - hlen);
477 ip->ip_src = mh->mob_src;
478 memmove(mtodo(m, hlen), ip, sizeof(struct ip));
481 m->m_pkthdr.rcvif = ifp;
482 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
483 M_SETFIB(m, ifp->if_fib);
485 BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
486 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
487 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
488 if ((ifp->if_flags & IFF_MONITOR) != 0)
491 netisr_dispatch(NETISR_IP, m);
492 return (IPPROTO_DONE);
494 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
495 return (IPPROTO_DONE);
498 #define MTAG_ME 1414491977
500 me_check_nesting(struct ifnet *ifp, struct mbuf *m)
507 while ((mtag = m_tag_locate(m, MTAG_ME, 0, mtag)) != NULL) {
508 if (*(struct ifnet **)(mtag + 1) == ifp) {
509 log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
514 if (count > V_max_me_nesting) {
516 "%s: if_output recursively called too many times(%d)\n",
517 ifp->if_xname, count);
520 mtag = m_tag_alloc(MTAG_ME, 0, sizeof(struct ifnet *), M_NOWAIT);
523 *(struct ifnet **)(mtag + 1) = ifp;
524 m_tag_prepend(m, mtag);
529 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
536 error = mac_ifnet_check_transmit(ifp, m);
540 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
541 (ifp->if_flags & IFF_UP) == 0) {
546 error = me_check_nesting(ifp, m);
550 m->m_flags &= ~(M_BCAST|M_MCAST);
551 if (dst->sa_family == AF_UNSPEC)
552 bcopy(dst->sa_data, &af, sizeof(af));
556 error = EAFNOSUPPORT;
559 BPF_MTAP2(ifp, &af, sizeof(af), m);
560 return (ifp->if_transmit(ifp, m));
563 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
568 me_transmit(struct ifnet *ifp, struct mbuf *m)
574 int error, hlen, plen;
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;
605 if (in_hosteq(sc->me_src, ip->ip_src)) {
606 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
609 hlen = sizeof(struct mobhdr);
610 mh.mob_flags = MOB_FLAGS_SP;
612 plen = m->m_pkthdr.len;
613 ip->ip_src = sc->me_src;
614 ip->ip_dst = sc->me_dst;
615 M_SETFIB(m, sc->me_fibnum);
617 M_PREPEND(m, hlen, M_NOWAIT);
622 if (m->m_len < sizeof(struct ip) + hlen)
623 m = m_pullup(m, sizeof(struct ip) + hlen);
628 memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
629 ip = mtod(m, struct ip *);
630 ip->ip_len = htons(m->m_pkthdr.len);
631 ip->ip_p = IPPROTO_MOBILE;
634 mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
635 bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
636 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
639 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
641 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
642 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
648 me_qflush(struct ifnet *ifp __unused)
654 memodevent(module_t mod, int type, void *data)
667 static moduledata_t me_mod = {
673 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
674 MODULE_VERSION(if_me, 1);