xref: /freebsd/sys/net/if_me.c (revision 7cc42f6d)
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 #ifdef VIMAGE
117 static void	me_reassign(struct ifnet *, struct vnet *, char *);
118 #endif
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 *);
125 
126 static int	me_set_tunnel(struct me_softc *, in_addr_t, in_addr_t);
127 static void	me_delete_tunnel(struct me_softc *);
128 
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)");
132 #ifndef MAX_ME_NEST
133 #define MAX_ME_NEST 1
134 #endif
135 
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");
140 
141 static uint32_t
142 me_hashval(in_addr_t src, in_addr_t dst)
143 {
144 	uint32_t ret;
145 
146 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
147 	return (fnv_32_buf(&dst, sizeof(dst), ret));
148 }
149 
150 static struct me_list *
151 me_hashinit(void)
152 {
153 	struct me_list *hash;
154 	int i;
155 
156 	hash = malloc(sizeof(struct me_list) * ME_HASH_SIZE,
157 	    M_IFME, M_WAITOK);
158 	for (i = 0; i < ME_HASH_SIZE; i++)
159 		CK_LIST_INIT(&hash[i]);
160 
161 	return (hash);
162 }
163 
164 static void
165 vnet_me_init(const void *unused __unused)
166 {
167 
168 	V_me_cloner = if_clone_simple(mename, me_clone_create,
169 	    me_clone_destroy, 0);
170 }
171 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
172     vnet_me_init, NULL);
173 
174 static void
175 vnet_me_uninit(const void *unused __unused)
176 {
177 
178 	if (V_me_hashtbl != NULL) {
179 		free(V_me_hashtbl, M_IFME);
180 		V_me_hashtbl = NULL;
181 		ME_WAIT();
182 		free(V_me_srchashtbl, M_IFME);
183 	}
184 	if_clone_detach(V_me_cloner);
185 }
186 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
187     vnet_me_uninit, NULL);
188 
189 static int
190 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
191 {
192 	struct me_softc *sc;
193 
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);
199 
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;
206 #ifdef VIMAGE
207 	ME2IFP(sc)->if_reassign = me_reassign;
208 #endif
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));
213 	return (0);
214 }
215 
216 #ifdef VIMAGE
217 static void
218 me_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
219     char *unused __unused)
220 {
221 	struct me_softc *sc;
222 
223 	sx_xlock(&me_ioctl_sx);
224 	sc = ifp->if_softc;
225 	if (sc != NULL)
226 		me_delete_tunnel(sc);
227 	sx_xunlock(&me_ioctl_sx);
228 }
229 #endif /* VIMAGE */
230 
231 static void
232 me_clone_destroy(struct ifnet *ifp)
233 {
234 	struct me_softc *sc;
235 
236 	sx_xlock(&me_ioctl_sx);
237 	sc = ifp->if_softc;
238 	me_delete_tunnel(sc);
239 	bpfdetach(ifp);
240 	if_detach(ifp);
241 	ifp->if_softc = NULL;
242 	sx_xunlock(&me_ioctl_sx);
243 
244 	ME_WAIT();
245 	if_free(ifp);
246 	free(sc, M_IFME);
247 }
248 
249 static int
250 me_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
251 {
252 	struct ifreq *ifr = (struct ifreq *)data;
253 	struct sockaddr_in *src, *dst;
254 	struct me_softc *sc;
255 	int error;
256 
257 	switch (cmd) {
258 	case SIOCSIFMTU:
259 		if (ifr->ifr_mtu < 576)
260 			return (EINVAL);
261 		ifp->if_mtu = ifr->ifr_mtu;
262 		return (0);
263 	case SIOCSIFADDR:
264 		ifp->if_flags |= IFF_UP;
265 	case SIOCSIFFLAGS:
266 	case SIOCADDMULTI:
267 	case SIOCDELMULTI:
268 		return (0);
269 	}
270 	sx_xlock(&me_ioctl_sx);
271 	sc = ifp->if_softc;
272 	if (sc == NULL) {
273 		error = ENXIO;
274 		goto end;
275 	}
276 	error = 0;
277 	switch (cmd) {
278 	case SIOCSIFPHYADDR:
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)) {
285 			error = EINVAL;
286 			break;
287 		}
288 		if (src->sin_addr.s_addr == INADDR_ANY ||
289 		    dst->sin_addr.s_addr == INADDR_ANY) {
290 			error = EADDRNOTAVAIL;
291 			break;
292 		}
293 		error = me_set_tunnel(sc, src->sin_addr.s_addr,
294 		    dst->sin_addr.s_addr);
295 		break;
296 	case SIOCDIFPHYADDR:
297 		me_delete_tunnel(sc);
298 		break;
299 	case SIOCGIFPSRCADDR:
300 	case SIOCGIFPDSTADDR:
301 		if (!ME_READY(sc)) {
302 			error = EADDRNOTAVAIL;
303 			break;
304 		}
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);
309 		switch (cmd) {
310 		case SIOCGIFPSRCADDR:
311 			src->sin_addr = sc->me_src;
312 			break;
313 		case SIOCGIFPDSTADDR:
314 			src->sin_addr = sc->me_dst;
315 			break;
316 		}
317 		error = prison_if(curthread->td_ucred, sintosa(src));
318 		if (error != 0)
319 			memset(src, 0, sizeof(*src));
320 		break;
321 	case SIOCGTUNFIB:
322 		ifr->ifr_fib = sc->me_fibnum;
323 		break;
324 	case SIOCSTUNFIB:
325 		if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
326 			break;
327 		if (ifr->ifr_fib >= rt_numfibs)
328 			error = EINVAL;
329 		else
330 			sc->me_fibnum = ifr->ifr_fib;
331 		break;
332 	default:
333 		error = EINVAL;
334 		break;
335 	}
336 end:
337 	sx_xunlock(&me_ioctl_sx);
338 	return (error);
339 }
340 
341 static int
342 me_lookup(const struct mbuf *m, int off, int proto, void **arg)
343 {
344 	const struct ip *ip;
345 	struct me_softc *sc;
346 
347 	if (V_me_hashtbl == NULL)
348 		return (0);
349 
350 	NET_EPOCH_ASSERT();
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)
357 				return (0);
358 			*arg = sc;
359 			return (ENCAP_DRV_LOOKUP);
360 		}
361 	}
362 	return (0);
363 }
364 
365 /*
366  * Check that ingress address belongs to local host.
367  */
368 static void
369 me_set_running(struct me_softc *sc)
370 {
371 
372 	if (in_localip(sc->me_src))
373 		ME2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
374 	else
375 		ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
376 }
377 
378 /*
379  * ifaddr_event handler.
380  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
381  * source address spoofing.
382  */
383 static void
384 me_srcaddr(void *arg __unused, const struct sockaddr *sa,
385     int event __unused)
386 {
387 	const struct sockaddr_in *sin;
388 	struct me_softc *sc;
389 
390 	/* Check that VNET is ready */
391 	if (V_me_hashtbl == NULL)
392 		return;
393 
394 	NET_EPOCH_ASSERT();
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)
398 			continue;
399 		me_set_running(sc);
400 	}
401 }
402 
403 static int
404 me_set_tunnel(struct me_softc *sc, in_addr_t src, in_addr_t dst)
405 {
406 	struct me_softc *tmp;
407 
408 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
409 
410 	if (V_me_hashtbl == NULL) {
411 		V_me_hashtbl = me_hashinit();
412 		V_me_srchashtbl = me_hashinit();
413 	}
414 
415 	if (sc->me_src.s_addr == src && sc->me_dst.s_addr == dst)
416 		return (0);
417 
418 	CK_LIST_FOREACH(tmp, &ME_HASH(src, dst), chain) {
419 		if (tmp == sc)
420 			continue;
421 		if (tmp->me_src.s_addr == src &&
422 		    tmp->me_dst.s_addr == dst)
423 			return (EADDRNOTAVAIL);
424 	}
425 
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);
431 
432 	me_set_running(sc);
433 	if_link_state_change(ME2IFP(sc), LINK_STATE_UP);
434 	return (0);
435 }
436 
437 static void
438 me_delete_tunnel(struct me_softc *sc)
439 {
440 
441 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
442 	if (ME_READY(sc)) {
443 		CK_LIST_REMOVE(sc, chain);
444 		CK_LIST_REMOVE(sc, srchash);
445 		ME_WAIT();
446 
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);
451 	}
452 }
453 
454 static uint16_t
455 me_in_cksum(uint16_t *p, int nwords)
456 {
457 	uint32_t sum = 0;
458 
459 	while (nwords-- > 0)
460 		sum += *p++;
461 	sum = (sum >> 16) + (sum & 0xffff);
462 	sum += (sum >> 16);
463 	return (~sum);
464 }
465 
466 static int
467 me_input(struct mbuf *m, int off, int proto, void *arg)
468 {
469 	struct me_softc *sc = arg;
470 	struct mobhdr *mh;
471 	struct ifnet *ifp;
472 	struct ip *ip;
473 	int hlen;
474 
475 	NET_EPOCH_ASSERT();
476 
477 	ifp = ME2IFP(sc);
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);
484 	if (m == NULL)
485 		goto drop;
486 	mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
487 	/* check for wrong flags */
488 	if (mh->mob_flags & (~MOB_FLAGS_SP)) {
489 		m_freem(m);
490 		goto drop;
491 	}
492 	if (mh->mob_flags) {
493 	       if (hlen != sizeof(struct mobhdr)) {
494 			m_freem(m);
495 			goto drop;
496 	       }
497 	} else
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) {
501 		m_freem(m);
502 		goto drop;
503 	}
504 #ifdef MAC
505 	mac_ifnet_create_mbuf(ifp, m);
506 #endif
507 	ip = mtod(m, struct ip *);
508 	ip->ip_dst = mh->mob_dst;
509 	ip->ip_p = mh->mob_proto;
510 	ip->ip_sum = 0;
511 	ip->ip_len = htons(m->m_pkthdr.len - hlen);
512 	if (mh->mob_flags)
513 		ip->ip_src = mh->mob_src;
514 	memmove(mtodo(m, hlen), ip, sizeof(struct ip));
515 	m_adj(m, hlen);
516 	m_clrprotoflags(m);
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);
520 	hlen = AF_INET;
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)
525 		m_freem(m);
526 	else
527 		netisr_dispatch(NETISR_IP, m);
528 	return (IPPROTO_DONE);
529 drop:
530 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
531 	return (IPPROTO_DONE);
532 }
533 
534 static int
535 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
536    struct route *ro __unused)
537 {
538 	uint32_t af;
539 
540 	if (dst->sa_family == AF_UNSPEC)
541 		bcopy(dst->sa_data, &af, sizeof(af));
542 	else
543 		af = dst->sa_family;
544 	m->m_pkthdr.csum_data = af;
545 	return (ifp->if_transmit(ifp, m));
546 }
547 
548 #define	MTAG_ME	1414491977
549 static int
550 me_transmit(struct ifnet *ifp, struct mbuf *m)
551 {
552 	ME_RLOCK_TRACKER;
553 	struct mobhdr mh;
554 	struct me_softc *sc;
555 	struct ip *ip;
556 	uint32_t af;
557 	int error, hlen, plen;
558 
559 	ME_RLOCK();
560 #ifdef MAC
561 	error = mac_ifnet_check_transmit(ifp, m);
562 	if (error != 0)
563 		goto drop;
564 #endif
565 	error = ENETDOWN;
566 	sc = ifp->if_softc;
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) {
573 		m_freem(m);
574 		goto drop;
575 	}
576 	af = m->m_pkthdr.csum_data;
577 	if (af != AF_INET) {
578 		error = EAFNOSUPPORT;
579 		m_freem(m);
580 		goto drop;
581 	}
582 	if (m->m_len < sizeof(struct ip))
583 		m = m_pullup(m, sizeof(struct ip));
584 	if (m == NULL) {
585 		error = ENOBUFS;
586 		goto drop;
587 	}
588 	ip = mtod(m, struct ip *);
589 	/* Fragmented datagramms shouldn't be encapsulated */
590 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
591 		error = EINVAL;
592 		m_freem(m);
593 		goto drop;
594 	}
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);
600 		mh.mob_flags = 0;
601 	} else {
602 		hlen = sizeof(struct mobhdr);
603 		mh.mob_flags = MOB_FLAGS_SP;
604 	}
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);
612 	if (m == NULL) {
613 		error = ENOBUFS;
614 		goto drop;
615 	}
616 	if (m->m_len < sizeof(struct ip) + hlen)
617 		m = m_pullup(m, sizeof(struct ip) + hlen);
618 	if (m == NULL) {
619 		error = ENOBUFS;
620 		goto drop;
621 	}
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;
626 	ip->ip_sum = 0;
627 	mh.mob_csum = 0;
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);
631 drop:
632 	if (error)
633 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
634 	else {
635 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
636 		if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
637 	}
638 	ME_RUNLOCK();
639 	return (error);
640 }
641 
642 static void
643 me_qflush(struct ifnet *ifp __unused)
644 {
645 
646 }
647 
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) -
653 	    sizeof(in_addr_t),
654 	.exact_match = ENCAP_DRV_LOOKUP,
655 	.lookup = me_lookup,
656 	.input = me_input
657 };
658 
659 static int
660 memodevent(module_t mod, int type, void *data)
661 {
662 
663 	switch (type) {
664 	case MOD_LOAD:
665 		me_srcaddrtab = ip_encap_register_srcaddr(me_srcaddr,
666 		    NULL, M_WAITOK);
667 		ecookie = ip_encap_attach(&me_encap_cfg, NULL, M_WAITOK);
668 		break;
669 	case MOD_UNLOAD:
670 		ip_encap_detach(ecookie);
671 		ip_encap_unregister_srcaddr(me_srcaddrtab);
672 		break;
673 	default:
674 		return (EOPNOTSUPP);
675 	}
676 	return (0);
677 }
678 
679 static moduledata_t me_mod = {
680 	"if_me",
681 	memodevent,
682 	0
683 };
684 
685 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
686 MODULE_VERSION(if_me, 1);
687