xref: /freebsd/sys/net/if_me.c (revision c1cdf6a42f0d951ba720688dfc6ce07608b02f6e)
1 /*-
2  * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/jail.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mbuf.h>
38 #include <sys/priv.h>
39 #include <sys/proc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sx.h>
43 #include <sys/sysctl.h>
44 #include <sys/syslog.h>
45 
46 #include <net/bpf.h>
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_clone.h>
51 #include <net/if_types.h>
52 #include <net/netisr.h>
53 #include <net/vnet.h>
54 #include <net/route.h>
55 
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>
62 
63 #include <machine/in_cksum.h>
64 #include <security/mac/mac_framework.h>
65 
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 */
70 struct mobhdr {
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) */
77 } __packed;
78 
79 struct me_softc {
80 	struct ifnet		*me_ifp;
81 	u_int			me_fibnum;
82 	struct in_addr		me_src;
83 	struct in_addr		me_dst;
84 
85 	CK_LIST_ENTRY(me_softc) chain;
86 	CK_LIST_ENTRY(me_softc) srchash;
87 };
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)
95 
96 #ifndef ME_HASH_SIZE
97 #define	ME_HASH_SIZE	(1 << 4)
98 #endif
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)])
107 
108 static struct sx me_ioctl_sx;
109 SX_SYSINIT(me_ioctl_sx, &me_ioctl_sx, "me_ioctl");
110 
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)
115 
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 *);
122 
123 static int	me_set_tunnel(struct me_softc *, in_addr_t, in_addr_t);
124 static void	me_delete_tunnel(struct me_softc *);
125 
126 SYSCTL_DECL(_net_link);
127 static SYSCTL_NODE(_net_link, IFT_TUNNEL, me, CTLFLAG_RW, 0,
128     "Minimal Encapsulation for IP (RFC 2004)");
129 #ifndef MAX_ME_NEST
130 #define MAX_ME_NEST 1
131 #endif
132 
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");
137 
138 static uint32_t
139 me_hashval(in_addr_t src, in_addr_t dst)
140 {
141 	uint32_t ret;
142 
143 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
144 	return (fnv_32_buf(&dst, sizeof(dst), ret));
145 }
146 
147 static struct me_list *
148 me_hashinit(void)
149 {
150 	struct me_list *hash;
151 	int i;
152 
153 	hash = malloc(sizeof(struct me_list) * ME_HASH_SIZE,
154 	    M_IFME, M_WAITOK);
155 	for (i = 0; i < ME_HASH_SIZE; i++)
156 		CK_LIST_INIT(&hash[i]);
157 
158 	return (hash);
159 }
160 
161 static void
162 vnet_me_init(const void *unused __unused)
163 {
164 	V_me_cloner = if_clone_simple(mename, me_clone_create,
165 	    me_clone_destroy, 0);
166 }
167 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
168     vnet_me_init, NULL);
169 
170 static void
171 vnet_me_uninit(const void *unused __unused)
172 {
173 
174 	if (V_me_hashtbl != NULL) {
175 		free(V_me_hashtbl, M_IFME);
176 		free(V_me_srchashtbl, M_IFME);
177 	}
178 	if_clone_detach(V_me_cloner);
179 }
180 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
181     vnet_me_uninit, NULL);
182 
183 static int
184 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
185 {
186 	struct me_softc *sc;
187 
188 	sc = malloc(sizeof(struct me_softc), M_IFME, M_WAITOK | M_ZERO);
189 	sc->me_fibnum = curthread->td_proc->p_fibnum;
190 	ME2IFP(sc) = if_alloc(IFT_TUNNEL);
191 	ME2IFP(sc)->if_softc = sc;
192 	if_initname(ME2IFP(sc), mename, unit);
193 
194 	ME2IFP(sc)->if_mtu = MEMTU;;
195 	ME2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
196 	ME2IFP(sc)->if_output = me_output;
197 	ME2IFP(sc)->if_ioctl = me_ioctl;
198 	ME2IFP(sc)->if_transmit = me_transmit;
199 	ME2IFP(sc)->if_qflush = me_qflush;
200 	ME2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
201 	ME2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
202 	if_attach(ME2IFP(sc));
203 	bpfattach(ME2IFP(sc), DLT_NULL, sizeof(u_int32_t));
204 	return (0);
205 }
206 
207 static void
208 me_clone_destroy(struct ifnet *ifp)
209 {
210 	struct me_softc *sc;
211 
212 	sx_xlock(&me_ioctl_sx);
213 	sc = ifp->if_softc;
214 	me_delete_tunnel(sc);
215 	bpfdetach(ifp);
216 	if_detach(ifp);
217 	ifp->if_softc = NULL;
218 	sx_xunlock(&me_ioctl_sx);
219 
220 	ME_WAIT();
221 	if_free(ifp);
222 	free(sc, M_IFME);
223 }
224 
225 static int
226 me_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
227 {
228 	struct ifreq *ifr = (struct ifreq *)data;
229 	struct sockaddr_in *src, *dst;
230 	struct me_softc *sc;
231 	int error;
232 
233 	switch (cmd) {
234 	case SIOCSIFMTU:
235 		if (ifr->ifr_mtu < 576)
236 			return (EINVAL);
237 		ifp->if_mtu = ifr->ifr_mtu;
238 		return (0);
239 	case SIOCSIFADDR:
240 		ifp->if_flags |= IFF_UP;
241 	case SIOCSIFFLAGS:
242 	case SIOCADDMULTI:
243 	case SIOCDELMULTI:
244 		return (0);
245 	}
246 	sx_xlock(&me_ioctl_sx);
247 	sc = ifp->if_softc;
248 	if (sc == NULL) {
249 		error = ENXIO;
250 		goto end;
251 	}
252 	error = 0;
253 	switch (cmd) {
254 	case SIOCSIFPHYADDR:
255 		src = &((struct in_aliasreq *)data)->ifra_addr;
256 		dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
257 		if (src->sin_family != dst->sin_family ||
258 		    src->sin_family != AF_INET ||
259 		    src->sin_len != dst->sin_len ||
260 		    src->sin_len != sizeof(struct sockaddr_in)) {
261 			error = EINVAL;
262 			break;
263 		}
264 		if (src->sin_addr.s_addr == INADDR_ANY ||
265 		    dst->sin_addr.s_addr == INADDR_ANY) {
266 			error = EADDRNOTAVAIL;
267 			break;
268 		}
269 		error = me_set_tunnel(sc, src->sin_addr.s_addr,
270 		    dst->sin_addr.s_addr);
271 		break;
272 	case SIOCDIFPHYADDR:
273 		me_delete_tunnel(sc);
274 		break;
275 	case SIOCGIFPSRCADDR:
276 	case SIOCGIFPDSTADDR:
277 		if (!ME_READY(sc)) {
278 			error = EADDRNOTAVAIL;
279 			break;
280 		}
281 		src = (struct sockaddr_in *)&ifr->ifr_addr;
282 		memset(src, 0, sizeof(*src));
283 		src->sin_family = AF_INET;
284 		src->sin_len = sizeof(*src);
285 		switch (cmd) {
286 		case SIOCGIFPSRCADDR:
287 			src->sin_addr = sc->me_src;
288 			break;
289 		case SIOCGIFPDSTADDR:
290 			src->sin_addr = sc->me_dst;
291 			break;
292 		}
293 		error = prison_if(curthread->td_ucred, sintosa(src));
294 		if (error != 0)
295 			memset(src, 0, sizeof(*src));
296 		break;
297 	case SIOCGTUNFIB:
298 		ifr->ifr_fib = sc->me_fibnum;
299 		break;
300 	case SIOCSTUNFIB:
301 		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
302 			break;
303 		if (ifr->ifr_fib >= rt_numfibs)
304 			error = EINVAL;
305 		else
306 			sc->me_fibnum = ifr->ifr_fib;
307 		break;
308 	default:
309 		error = EINVAL;
310 		break;
311 	}
312 end:
313 	sx_xunlock(&me_ioctl_sx);
314 	return (error);
315 }
316 
317 static int
318 me_lookup(const struct mbuf *m, int off, int proto, void **arg)
319 {
320 	const struct ip *ip;
321 	struct me_softc *sc;
322 
323 	if (V_me_hashtbl == NULL)
324 		return (0);
325 
326 	MPASS(in_epoch(net_epoch_preempt));
327 	ip = mtod(m, const struct ip *);
328 	CK_LIST_FOREACH(sc, &ME_HASH(ip->ip_dst.s_addr,
329 	    ip->ip_src.s_addr), chain) {
330 		if (sc->me_src.s_addr == ip->ip_dst.s_addr &&
331 		    sc->me_dst.s_addr == ip->ip_src.s_addr) {
332 			if ((ME2IFP(sc)->if_flags & IFF_UP) == 0)
333 				return (0);
334 			*arg = sc;
335 			return (ENCAP_DRV_LOOKUP);
336 		}
337 	}
338 	return (0);
339 }
340 
341 /*
342  * Check that ingress address belongs to local host.
343  */
344 static void
345 me_set_running(struct me_softc *sc)
346 {
347 
348 	if (in_localip(sc->me_src))
349 		ME2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
350 	else
351 		ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
352 }
353 
354 /*
355  * ifaddr_event handler.
356  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
357  * source address spoofing.
358  */
359 static void
360 me_srcaddr(void *arg __unused, const struct sockaddr *sa,
361     int event __unused)
362 {
363 	const struct sockaddr_in *sin;
364 	struct me_softc *sc;
365 
366 	if (V_me_srchashtbl == NULL)
367 		return;
368 
369 	MPASS(in_epoch(net_epoch_preempt));
370 	sin = (const struct sockaddr_in *)sa;
371 	CK_LIST_FOREACH(sc, &ME_SRCHASH(sin->sin_addr.s_addr), srchash) {
372 		if (sc->me_src.s_addr != sin->sin_addr.s_addr)
373 			continue;
374 		me_set_running(sc);
375 	}
376 }
377 
378 static int
379 me_set_tunnel(struct me_softc *sc, in_addr_t src, in_addr_t dst)
380 {
381 	struct me_softc *tmp;
382 
383 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
384 
385 	if (V_me_hashtbl == NULL) {
386 		V_me_hashtbl = me_hashinit();
387 		V_me_srchashtbl = me_hashinit();
388 	}
389 
390 	if (sc->me_src.s_addr == src && sc->me_dst.s_addr == dst)
391 		return (0);
392 
393 	CK_LIST_FOREACH(tmp, &ME_HASH(src, dst), chain) {
394 		if (tmp == sc)
395 			continue;
396 		if (tmp->me_src.s_addr == src &&
397 		    tmp->me_dst.s_addr == dst)
398 			return (EADDRNOTAVAIL);
399 	}
400 
401 	me_delete_tunnel(sc);
402 	sc->me_dst.s_addr = dst;
403 	sc->me_src.s_addr = src;
404 	CK_LIST_INSERT_HEAD(&ME_HASH(src, dst), sc, chain);
405 	CK_LIST_INSERT_HEAD(&ME_SRCHASH(src), sc, srchash);
406 
407 	me_set_running(sc);
408 	if_link_state_change(ME2IFP(sc), LINK_STATE_UP);
409 	return (0);
410 }
411 
412 static void
413 me_delete_tunnel(struct me_softc *sc)
414 {
415 
416 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
417 	if (ME_READY(sc)) {
418 		CK_LIST_REMOVE(sc, chain);
419 		CK_LIST_REMOVE(sc, srchash);
420 		ME_WAIT();
421 
422 		sc->me_src.s_addr = 0;
423 		sc->me_dst.s_addr = 0;
424 		ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
425 		if_link_state_change(ME2IFP(sc), LINK_STATE_DOWN);
426 	}
427 }
428 
429 static uint16_t
430 me_in_cksum(uint16_t *p, int nwords)
431 {
432 	uint32_t sum = 0;
433 
434 	while (nwords-- > 0)
435 		sum += *p++;
436 	sum = (sum >> 16) + (sum & 0xffff);
437 	sum += (sum >> 16);
438 	return (~sum);
439 }
440 
441 static int
442 me_input(struct mbuf *m, int off, int proto, void *arg)
443 {
444 	struct me_softc *sc = arg;
445 	struct mobhdr *mh;
446 	struct ifnet *ifp;
447 	struct ip *ip;
448 	int hlen;
449 
450 	ifp = ME2IFP(sc);
451 	/* checks for short packets */
452 	hlen = sizeof(struct mobhdr);
453 	if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
454 		hlen -= sizeof(struct in_addr);
455 	if (m->m_len < sizeof(struct ip) + hlen)
456 		m = m_pullup(m, sizeof(struct ip) + hlen);
457 	if (m == NULL)
458 		goto drop;
459 	mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
460 	/* check for wrong flags */
461 	if (mh->mob_flags & (~MOB_FLAGS_SP)) {
462 		m_freem(m);
463 		goto drop;
464 	}
465 	if (mh->mob_flags) {
466 	       if (hlen != sizeof(struct mobhdr)) {
467 			m_freem(m);
468 			goto drop;
469 	       }
470 	} else
471 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
472 	/* check mobile header checksum */
473 	if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
474 		m_freem(m);
475 		goto drop;
476 	}
477 #ifdef MAC
478 	mac_ifnet_create_mbuf(ifp, m);
479 #endif
480 	ip = mtod(m, struct ip *);
481 	ip->ip_dst = mh->mob_dst;
482 	ip->ip_p = mh->mob_proto;
483 	ip->ip_sum = 0;
484 	ip->ip_len = htons(m->m_pkthdr.len - hlen);
485 	if (mh->mob_flags)
486 		ip->ip_src = mh->mob_src;
487 	memmove(mtodo(m, hlen), ip, sizeof(struct ip));
488 	m_adj(m, hlen);
489 	m_clrprotoflags(m);
490 	m->m_pkthdr.rcvif = ifp;
491 	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
492 	M_SETFIB(m, ifp->if_fib);
493 	hlen = AF_INET;
494 	BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
495 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
496 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
497 	if ((ifp->if_flags & IFF_MONITOR) != 0)
498 		m_freem(m);
499 	else
500 		netisr_dispatch(NETISR_IP, m);
501 	return (IPPROTO_DONE);
502 drop:
503 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
504 	return (IPPROTO_DONE);
505 }
506 
507 static int
508 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
509    struct route *ro __unused)
510 {
511 	uint32_t af;
512 
513 	if (dst->sa_family == AF_UNSPEC)
514 		bcopy(dst->sa_data, &af, sizeof(af));
515 	else
516 		af = dst->sa_family;
517 	m->m_pkthdr.csum_data = af;
518 	return (ifp->if_transmit(ifp, m));
519 }
520 
521 #define	MTAG_ME	1414491977
522 static int
523 me_transmit(struct ifnet *ifp, struct mbuf *m)
524 {
525 	ME_RLOCK_TRACKER;
526 	struct mobhdr mh;
527 	struct me_softc *sc;
528 	struct ip *ip;
529 	uint32_t af;
530 	int error, hlen, plen;
531 
532 	ME_RLOCK();
533 #ifdef MAC
534 	error = mac_ifnet_check_transmit(ifp, m);
535 	if (error != 0)
536 		goto drop;
537 #endif
538 	error = ENETDOWN;
539 	sc = ifp->if_softc;
540 	if (sc == NULL || !ME_READY(sc) ||
541 	    (ifp->if_flags & IFF_MONITOR) != 0 ||
542 	    (ifp->if_flags & IFF_UP) == 0 ||
543 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
544 	    (error = if_tunnel_check_nesting(ifp, m, MTAG_ME,
545 		V_max_me_nesting)) != 0) {
546 		m_freem(m);
547 		goto drop;
548 	}
549 	af = m->m_pkthdr.csum_data;
550 	if (af != AF_INET) {
551 		error = EAFNOSUPPORT;
552 		m_freem(m);
553 		goto drop;
554 	}
555 	if (m->m_len < sizeof(struct ip))
556 		m = m_pullup(m, sizeof(struct ip));
557 	if (m == NULL) {
558 		error = ENOBUFS;
559 		goto drop;
560 	}
561 	ip = mtod(m, struct ip *);
562 	/* Fragmented datagramms shouldn't be encapsulated */
563 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
564 		error = EINVAL;
565 		m_freem(m);
566 		goto drop;
567 	}
568 	mh.mob_proto = ip->ip_p;
569 	mh.mob_src = ip->ip_src;
570 	mh.mob_dst = ip->ip_dst;
571 	if (in_hosteq(sc->me_src, ip->ip_src)) {
572 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
573 		mh.mob_flags = 0;
574 	} else {
575 		hlen = sizeof(struct mobhdr);
576 		mh.mob_flags = MOB_FLAGS_SP;
577 	}
578 	BPF_MTAP2(ifp, &af, sizeof(af), m);
579 	plen = m->m_pkthdr.len;
580 	ip->ip_src = sc->me_src;
581 	ip->ip_dst = sc->me_dst;
582 	m->m_flags &= ~(M_BCAST|M_MCAST);
583 	M_SETFIB(m, sc->me_fibnum);
584 	M_PREPEND(m, hlen, M_NOWAIT);
585 	if (m == NULL) {
586 		error = ENOBUFS;
587 		goto drop;
588 	}
589 	if (m->m_len < sizeof(struct ip) + hlen)
590 		m = m_pullup(m, sizeof(struct ip) + hlen);
591 	if (m == NULL) {
592 		error = ENOBUFS;
593 		goto drop;
594 	}
595 	memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
596 	ip = mtod(m, struct ip *);
597 	ip->ip_len = htons(m->m_pkthdr.len);
598 	ip->ip_p = IPPROTO_MOBILE;
599 	ip->ip_sum = 0;
600 	mh.mob_csum = 0;
601 	mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
602 	bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
603 	error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
604 drop:
605 	if (error)
606 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
607 	else {
608 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
609 		if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
610 	}
611 	ME_RUNLOCK();
612 	return (error);
613 }
614 
615 static void
616 me_qflush(struct ifnet *ifp __unused)
617 {
618 
619 }
620 
621 static const struct srcaddrtab *me_srcaddrtab = NULL;
622 static const struct encaptab *ecookie = NULL;
623 static const struct encap_config me_encap_cfg = {
624 	.proto = IPPROTO_MOBILE,
625 	.min_length = sizeof(struct ip) + sizeof(struct mobhdr) -
626 	    sizeof(in_addr_t),
627 	.exact_match = ENCAP_DRV_LOOKUP,
628 	.lookup = me_lookup,
629 	.input = me_input
630 };
631 
632 static int
633 memodevent(module_t mod, int type, void *data)
634 {
635 
636 	switch (type) {
637 	case MOD_LOAD:
638 		me_srcaddrtab = ip_encap_register_srcaddr(me_srcaddr,
639 		    NULL, M_WAITOK);
640 		ecookie = ip_encap_attach(&me_encap_cfg, NULL, M_WAITOK);
641 		break;
642 	case MOD_UNLOAD:
643 		ip_encap_detach(ecookie);
644 		ip_encap_unregister_srcaddr(me_srcaddrtab);
645 		break;
646 	default:
647 		return (EOPNOTSUPP);
648 	}
649 	return (0);
650 }
651 
652 static moduledata_t me_mod = {
653 	"if_me",
654 	memodevent,
655 	0
656 };
657 
658 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
659 MODULE_VERSION(if_me, 1);
660