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