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