xref: /freebsd/sys/net/if_me.c (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
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 	NET_EPOCH_ASSERT();
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 	NET_EPOCH_ASSERT();
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 	NET_EPOCH_ASSERT();
455 
456 	ifp = ME2IFP(sc);
457 	/* checks for short packets */
458 	hlen = sizeof(struct mobhdr);
459 	if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
460 		hlen -= sizeof(struct in_addr);
461 	if (m->m_len < sizeof(struct ip) + hlen)
462 		m = m_pullup(m, sizeof(struct ip) + hlen);
463 	if (m == NULL)
464 		goto drop;
465 	mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
466 	/* check for wrong flags */
467 	if (mh->mob_flags & (~MOB_FLAGS_SP)) {
468 		m_freem(m);
469 		goto drop;
470 	}
471 	if (mh->mob_flags) {
472 	       if (hlen != sizeof(struct mobhdr)) {
473 			m_freem(m);
474 			goto drop;
475 	       }
476 	} else
477 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
478 	/* check mobile header checksum */
479 	if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
480 		m_freem(m);
481 		goto drop;
482 	}
483 #ifdef MAC
484 	mac_ifnet_create_mbuf(ifp, m);
485 #endif
486 	ip = mtod(m, struct ip *);
487 	ip->ip_dst = mh->mob_dst;
488 	ip->ip_p = mh->mob_proto;
489 	ip->ip_sum = 0;
490 	ip->ip_len = htons(m->m_pkthdr.len - hlen);
491 	if (mh->mob_flags)
492 		ip->ip_src = mh->mob_src;
493 	memmove(mtodo(m, hlen), ip, sizeof(struct ip));
494 	m_adj(m, hlen);
495 	m_clrprotoflags(m);
496 	m->m_pkthdr.rcvif = ifp;
497 	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
498 	M_SETFIB(m, ifp->if_fib);
499 	hlen = AF_INET;
500 	BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
501 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
502 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
503 	if ((ifp->if_flags & IFF_MONITOR) != 0)
504 		m_freem(m);
505 	else
506 		netisr_dispatch(NETISR_IP, m);
507 	return (IPPROTO_DONE);
508 drop:
509 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
510 	return (IPPROTO_DONE);
511 }
512 
513 static int
514 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
515    struct route *ro __unused)
516 {
517 	uint32_t af;
518 
519 	if (dst->sa_family == AF_UNSPEC)
520 		bcopy(dst->sa_data, &af, sizeof(af));
521 	else
522 		af = dst->sa_family;
523 	m->m_pkthdr.csum_data = af;
524 	return (ifp->if_transmit(ifp, m));
525 }
526 
527 #define	MTAG_ME	1414491977
528 static int
529 me_transmit(struct ifnet *ifp, struct mbuf *m)
530 {
531 	ME_RLOCK_TRACKER;
532 	struct mobhdr mh;
533 	struct me_softc *sc;
534 	struct ip *ip;
535 	uint32_t af;
536 	int error, hlen, plen;
537 
538 	ME_RLOCK();
539 #ifdef MAC
540 	error = mac_ifnet_check_transmit(ifp, m);
541 	if (error != 0)
542 		goto drop;
543 #endif
544 	error = ENETDOWN;
545 	sc = ifp->if_softc;
546 	if (sc == NULL || !ME_READY(sc) ||
547 	    (ifp->if_flags & IFF_MONITOR) != 0 ||
548 	    (ifp->if_flags & IFF_UP) == 0 ||
549 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
550 	    (error = if_tunnel_check_nesting(ifp, m, MTAG_ME,
551 		V_max_me_nesting)) != 0) {
552 		m_freem(m);
553 		goto drop;
554 	}
555 	af = m->m_pkthdr.csum_data;
556 	if (af != AF_INET) {
557 		error = EAFNOSUPPORT;
558 		m_freem(m);
559 		goto drop;
560 	}
561 	if (m->m_len < sizeof(struct ip))
562 		m = m_pullup(m, sizeof(struct ip));
563 	if (m == NULL) {
564 		error = ENOBUFS;
565 		goto drop;
566 	}
567 	ip = mtod(m, struct ip *);
568 	/* Fragmented datagramms shouldn't be encapsulated */
569 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
570 		error = EINVAL;
571 		m_freem(m);
572 		goto drop;
573 	}
574 	mh.mob_proto = ip->ip_p;
575 	mh.mob_src = ip->ip_src;
576 	mh.mob_dst = ip->ip_dst;
577 	if (in_hosteq(sc->me_src, ip->ip_src)) {
578 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
579 		mh.mob_flags = 0;
580 	} else {
581 		hlen = sizeof(struct mobhdr);
582 		mh.mob_flags = MOB_FLAGS_SP;
583 	}
584 	BPF_MTAP2(ifp, &af, sizeof(af), m);
585 	plen = m->m_pkthdr.len;
586 	ip->ip_src = sc->me_src;
587 	ip->ip_dst = sc->me_dst;
588 	m->m_flags &= ~(M_BCAST|M_MCAST);
589 	M_SETFIB(m, sc->me_fibnum);
590 	M_PREPEND(m, hlen, M_NOWAIT);
591 	if (m == NULL) {
592 		error = ENOBUFS;
593 		goto drop;
594 	}
595 	if (m->m_len < sizeof(struct ip) + hlen)
596 		m = m_pullup(m, sizeof(struct ip) + hlen);
597 	if (m == NULL) {
598 		error = ENOBUFS;
599 		goto drop;
600 	}
601 	memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
602 	ip = mtod(m, struct ip *);
603 	ip->ip_len = htons(m->m_pkthdr.len);
604 	ip->ip_p = IPPROTO_MOBILE;
605 	ip->ip_sum = 0;
606 	mh.mob_csum = 0;
607 	mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
608 	bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
609 	error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
610 drop:
611 	if (error)
612 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
613 	else {
614 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
615 		if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
616 	}
617 	ME_RUNLOCK();
618 	return (error);
619 }
620 
621 static void
622 me_qflush(struct ifnet *ifp __unused)
623 {
624 
625 }
626 
627 static const struct srcaddrtab *me_srcaddrtab = NULL;
628 static const struct encaptab *ecookie = NULL;
629 static const struct encap_config me_encap_cfg = {
630 	.proto = IPPROTO_MOBILE,
631 	.min_length = sizeof(struct ip) + sizeof(struct mobhdr) -
632 	    sizeof(in_addr_t),
633 	.exact_match = ENCAP_DRV_LOOKUP,
634 	.lookup = me_lookup,
635 	.input = me_input
636 };
637 
638 static int
639 memodevent(module_t mod, int type, void *data)
640 {
641 
642 	switch (type) {
643 	case MOD_LOAD:
644 		me_srcaddrtab = ip_encap_register_srcaddr(me_srcaddr,
645 		    NULL, M_WAITOK);
646 		ecookie = ip_encap_attach(&me_encap_cfg, NULL, M_WAITOK);
647 		break;
648 	case MOD_UNLOAD:
649 		ip_encap_detach(ecookie);
650 		ip_encap_unregister_srcaddr(me_srcaddrtab);
651 		break;
652 	default:
653 		return (EOPNOTSUPP);
654 	}
655 	return (0);
656 }
657 
658 static moduledata_t me_mod = {
659 	"if_me",
660 	memodevent,
661 	0
662 };
663 
664 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
665 MODULE_VERSION(if_me, 1);
666