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/rmlock.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
45 #include <sys/syslog.h>
46 #include <sys/systm.h>
49 #include <net/ethernet.h>
51 #include <net/if_var.h>
52 #include <net/if_clone.h>
53 #include <net/if_types.h>
54 #include <net/netisr.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/ip_encap.h>
65 #include <machine/in_cksum.h>
66 #include <security/mac/mac_framework.h>
68 #define MEMTU (1500 - sizeof(struct mobhdr))
69 static const char mename[] = "me";
70 static MALLOC_DEFINE(M_IFME, mename, "Minimal Encapsulation for IP");
71 static VNET_DEFINE(struct mtx, me_mtx);
72 #define V_me_mtx VNET(me_mtx)
73 /* Minimal forwarding header RFC 2004 */
75 uint8_t mob_proto; /* protocol */
76 uint8_t mob_flags; /* flags */
77 #define MOB_FLAGS_SP 0x80 /* source present */
78 uint16_t mob_csum; /* header checksum */
79 struct in_addr mob_dst; /* original destination address */
80 struct in_addr mob_src; /* original source addr (optional) */
85 LIST_ENTRY(me_softc) me_list;
86 struct rmlock me_lock;
88 const struct encaptab *me_ecookie;
89 struct in_addr me_src;
90 struct in_addr me_dst;
92 #define ME2IFP(sc) ((sc)->me_ifp)
93 #define ME_READY(sc) ((sc)->me_src.s_addr != 0)
94 #define ME_LOCK_INIT(sc) rm_init(&(sc)->me_lock, "me softc")
95 #define ME_LOCK_DESTROY(sc) rm_destroy(&(sc)->me_lock)
96 #define ME_RLOCK_TRACKER struct rm_priotracker me_tracker
97 #define ME_RLOCK(sc) rm_rlock(&(sc)->me_lock, &me_tracker)
98 #define ME_RUNLOCK(sc) rm_runlock(&(sc)->me_lock, &me_tracker)
99 #define ME_RLOCK_ASSERT(sc) rm_assert(&(sc)->me_lock, RA_RLOCKED)
100 #define ME_WLOCK(sc) rm_wlock(&(sc)->me_lock)
101 #define ME_WUNLOCK(sc) rm_wunlock(&(sc)->me_lock)
102 #define ME_WLOCK_ASSERT(sc) rm_assert(&(sc)->me_lock, RA_WLOCKED)
104 #define ME_LIST_LOCK_INIT(x) mtx_init(&V_me_mtx, "me_mtx", NULL, MTX_DEF)
105 #define ME_LIST_LOCK_DESTROY(x) mtx_destroy(&V_me_mtx)
106 #define ME_LIST_LOCK(x) mtx_lock(&V_me_mtx)
107 #define ME_LIST_UNLOCK(x) mtx_unlock(&V_me_mtx)
109 static VNET_DEFINE(LIST_HEAD(, me_softc), me_softc_list);
110 #define V_me_softc_list VNET(me_softc_list)
111 static struct sx me_ioctl_sx;
112 SX_SYSINIT(me_ioctl_sx, &me_ioctl_sx, "me_ioctl");
114 static int me_clone_create(struct if_clone *, int, caddr_t);
115 static void me_clone_destroy(struct ifnet *);
116 static VNET_DEFINE(struct if_clone *, me_cloner);
117 #define V_me_cloner VNET(me_cloner)
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 ifnet *, struct sockaddr_in *,
127 struct sockaddr_in *);
128 static void me_delete_tunnel(struct ifnet *);
129 static int me_encapcheck(const struct mbuf *, int, int, void *);
131 #define ME_MINLEN (sizeof(struct ip) + sizeof(struct mobhdr) -\
133 static const struct encap_config ipv4_encap_cfg = {
134 .proto = IPPROTO_MOBILE,
135 .min_length = ME_MINLEN,
136 .exact_match = (sizeof(in_addr_t) << 4) + 8,
137 .check = me_encapcheck,
141 SYSCTL_DECL(_net_link);
142 static SYSCTL_NODE(_net_link, IFT_TUNNEL, me, CTLFLAG_RW, 0,
143 "Minimal Encapsulation for IP (RFC 2004)");
145 #define MAX_ME_NEST 1
148 static VNET_DEFINE(int, max_me_nesting) = MAX_ME_NEST;
149 #define V_max_me_nesting VNET(max_me_nesting)
150 SYSCTL_INT(_net_link_me, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
151 &VNET_NAME(max_me_nesting), 0, "Max nested tunnels");
154 vnet_me_init(const void *unused __unused)
156 LIST_INIT(&V_me_softc_list);
158 V_me_cloner = if_clone_simple(mename, me_clone_create,
159 me_clone_destroy, 0);
161 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
165 vnet_me_uninit(const void *unused __unused)
168 if_clone_detach(V_me_cloner);
169 ME_LIST_LOCK_DESTROY();
171 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
172 vnet_me_uninit, NULL);
175 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
179 sc = malloc(sizeof(struct me_softc), M_IFME, M_WAITOK | M_ZERO);
180 sc->me_fibnum = curthread->td_proc->p_fibnum;
181 ME2IFP(sc) = if_alloc(IFT_TUNNEL);
183 ME2IFP(sc)->if_softc = sc;
184 if_initname(ME2IFP(sc), mename, unit);
186 ME2IFP(sc)->if_mtu = MEMTU;;
187 ME2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
188 ME2IFP(sc)->if_output = me_output;
189 ME2IFP(sc)->if_ioctl = me_ioctl;
190 ME2IFP(sc)->if_transmit = me_transmit;
191 ME2IFP(sc)->if_qflush = me_qflush;
192 ME2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
193 ME2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
194 if_attach(ME2IFP(sc));
195 bpfattach(ME2IFP(sc), DLT_NULL, sizeof(u_int32_t));
197 LIST_INSERT_HEAD(&V_me_softc_list, sc, me_list);
203 me_clone_destroy(struct ifnet *ifp)
207 sx_xlock(&me_ioctl_sx);
209 me_delete_tunnel(ifp);
211 LIST_REMOVE(sc, me_list);
215 ifp->if_softc = NULL;
216 sx_xunlock(&me_ioctl_sx);
224 me_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
227 struct ifreq *ifr = (struct ifreq *)data;
228 struct sockaddr_in *src, *dst;
234 if (ifr->ifr_mtu < 576)
236 ifp->if_mtu = ifr->ifr_mtu;
239 ifp->if_flags |= IFF_UP;
245 sx_xlock(&me_ioctl_sx);
254 src = (struct sockaddr_in *)
255 &(((struct in_aliasreq *)data)->ifra_addr);
256 dst = (struct sockaddr_in *)
257 &(((struct in_aliasreq *)data)->ifra_dstaddr);
258 if (src->sin_family != dst->sin_family ||
259 src->sin_family != AF_INET ||
260 src->sin_len != dst->sin_len ||
261 src->sin_len != sizeof(struct sockaddr_in)) {
265 if (src->sin_addr.s_addr == INADDR_ANY ||
266 dst->sin_addr.s_addr == INADDR_ANY) {
267 error = EADDRNOTAVAIL;
270 error = me_set_tunnel(ifp, src, dst);
273 me_delete_tunnel(ifp);
275 case SIOCGIFPSRCADDR:
276 case SIOCGIFPDSTADDR:
279 error = EADDRNOTAVAIL;
283 src = (struct sockaddr_in *)&ifr->ifr_addr;
284 memset(src, 0, sizeof(*src));
285 src->sin_family = AF_INET;
286 src->sin_len = sizeof(*src);
288 case SIOCGIFPSRCADDR:
289 src->sin_addr = sc->me_src;
291 case SIOCGIFPDSTADDR:
292 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_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
328 sc = (struct me_softc *)arg;
329 if ((ME2IFP(sc)->if_flags & IFF_UP) == 0)
337 ip = mtod(m, struct ip *);
338 if (sc->me_src.s_addr == ip->ip_dst.s_addr &&
339 sc->me_dst.s_addr == ip->ip_src.s_addr)
347 me_set_tunnel(struct ifnet *ifp, struct sockaddr_in *src,
348 struct sockaddr_in *dst)
350 struct me_softc *sc, *tsc;
352 sx_assert(&me_ioctl_sx, SA_XLOCKED);
355 LIST_FOREACH(tsc, &V_me_softc_list, me_list) {
356 if (tsc == sc || !ME_READY(tsc))
358 if (tsc->me_src.s_addr == src->sin_addr.s_addr &&
359 tsc->me_dst.s_addr == dst->sin_addr.s_addr) {
361 return (EADDRNOTAVAIL);
367 sc->me_dst = dst->sin_addr;
368 sc->me_src = src->sin_addr;
371 if (sc->me_ecookie == NULL)
372 sc->me_ecookie = ip_encap_attach(&ipv4_encap_cfg,
374 if (sc->me_ecookie != NULL) {
375 ifp->if_drv_flags |= IFF_DRV_RUNNING;
376 if_link_state_change(ifp, LINK_STATE_UP);
382 me_delete_tunnel(struct ifnet *ifp)
384 struct me_softc *sc = ifp->if_softc;
386 sx_assert(&me_ioctl_sx, SA_XLOCKED);
387 if (sc->me_ecookie != NULL)
388 ip_encap_detach(sc->me_ecookie);
389 sc->me_ecookie = NULL;
391 sc->me_src.s_addr = 0;
392 sc->me_dst.s_addr = 0;
394 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
395 if_link_state_change(ifp, LINK_STATE_DOWN);
399 me_in_cksum(uint16_t *p, int nwords)
405 sum = (sum >> 16) + (sum & 0xffff);
411 me_input(struct mbuf *m, int off, int proto, void *arg)
413 struct me_softc *sc = arg;
420 /* checks for short packets */
421 hlen = sizeof(struct mobhdr);
422 if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
423 hlen -= sizeof(struct in_addr);
424 if (m->m_len < sizeof(struct ip) + hlen)
425 m = m_pullup(m, sizeof(struct ip) + hlen);
428 mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
429 /* check for wrong flags */
430 if (mh->mob_flags & (~MOB_FLAGS_SP)) {
435 if (hlen != sizeof(struct mobhdr)) {
440 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
441 /* check mobile header checksum */
442 if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
447 mac_ifnet_create_mbuf(ifp, m);
449 ip = mtod(m, struct ip *);
450 ip->ip_dst = mh->mob_dst;
451 ip->ip_p = mh->mob_proto;
453 ip->ip_len = htons(m->m_pkthdr.len - hlen);
455 ip->ip_src = mh->mob_src;
456 memmove(mtodo(m, hlen), ip, sizeof(struct ip));
459 m->m_pkthdr.rcvif = ifp;
460 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
461 M_SETFIB(m, ifp->if_fib);
463 BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
464 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
465 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
466 if ((ifp->if_flags & IFF_MONITOR) != 0)
469 netisr_dispatch(NETISR_IP, m);
470 return (IPPROTO_DONE);
472 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
473 return (IPPROTO_DONE);
476 #define MTAG_ME 1414491977
478 me_check_nesting(struct ifnet *ifp, struct mbuf *m)
485 while ((mtag = m_tag_locate(m, MTAG_ME, 0, mtag)) != NULL) {
486 if (*(struct ifnet **)(mtag + 1) == ifp) {
487 log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
492 if (count > V_max_me_nesting) {
494 "%s: if_output recursively called too many times(%d)\n",
495 ifp->if_xname, count);
498 mtag = m_tag_alloc(MTAG_ME, 0, sizeof(struct ifnet *), M_NOWAIT);
501 *(struct ifnet **)(mtag + 1) = ifp;
502 m_tag_prepend(m, mtag);
507 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
514 error = mac_ifnet_check_transmit(ifp, m);
518 if ((ifp->if_flags & IFF_MONITOR) != 0 ||
519 (ifp->if_flags & IFF_UP) == 0) {
524 error = me_check_nesting(ifp, m);
528 m->m_flags &= ~(M_BCAST|M_MCAST);
529 if (dst->sa_family == AF_UNSPEC)
530 bcopy(dst->sa_data, &af, sizeof(af));
534 error = EAFNOSUPPORT;
537 BPF_MTAP2(ifp, &af, sizeof(af), m);
538 return (ifp->if_transmit(ifp, m));
541 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
546 me_transmit(struct ifnet *ifp, struct mbuf *m)
552 int error, hlen, plen;
560 if (m->m_len < sizeof(struct ip))
561 m = m_pullup(m, sizeof(struct ip));
566 ip = mtod(m, struct ip *);
567 /* Fragmented datagramms shouldn't be encapsulated */
568 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
573 mh.mob_proto = ip->ip_p;
574 mh.mob_src = ip->ip_src;
575 mh.mob_dst = ip->ip_dst;
583 if (in_hosteq(sc->me_src, ip->ip_src)) {
584 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
587 hlen = sizeof(struct mobhdr);
588 mh.mob_flags = MOB_FLAGS_SP;
590 plen = m->m_pkthdr.len;
591 ip->ip_src = sc->me_src;
592 ip->ip_dst = sc->me_dst;
593 M_SETFIB(m, sc->me_fibnum);
595 M_PREPEND(m, hlen, M_NOWAIT);
600 if (m->m_len < sizeof(struct ip) + hlen)
601 m = m_pullup(m, sizeof(struct ip) + hlen);
606 memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
607 ip = mtod(m, struct ip *);
608 ip->ip_len = htons(m->m_pkthdr.len);
609 ip->ip_p = IPPROTO_MOBILE;
612 mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
613 bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
614 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
617 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
619 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
620 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
626 me_qflush(struct ifnet *ifp __unused)
632 memodevent(module_t mod, int type, void *data)
645 static moduledata_t me_mod = {
651 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
652 MODULE_VERSION(if_me, 1);