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