xref: /freebsd/sys/net/if_me.c (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
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 epoch_tracker et;
407 	struct me_softc *tmp;
408 
409 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
410 
411 	if (V_me_hashtbl == NULL) {
412 		V_me_hashtbl = me_hashinit();
413 		V_me_srchashtbl = me_hashinit();
414 	}
415 
416 	if (sc->me_src.s_addr == src && sc->me_dst.s_addr == dst)
417 		return (0);
418 
419 	CK_LIST_FOREACH(tmp, &ME_HASH(src, dst), chain) {
420 		if (tmp == sc)
421 			continue;
422 		if (tmp->me_src.s_addr == src &&
423 		    tmp->me_dst.s_addr == dst)
424 			return (EADDRNOTAVAIL);
425 	}
426 
427 	me_delete_tunnel(sc);
428 	sc->me_dst.s_addr = dst;
429 	sc->me_src.s_addr = src;
430 	CK_LIST_INSERT_HEAD(&ME_HASH(src, dst), sc, chain);
431 	CK_LIST_INSERT_HEAD(&ME_SRCHASH(src), sc, srchash);
432 
433 	NET_EPOCH_ENTER(et);
434 	me_set_running(sc);
435 	NET_EPOCH_EXIT(et);
436 	if_link_state_change(ME2IFP(sc), LINK_STATE_UP);
437 	return (0);
438 }
439 
440 static void
441 me_delete_tunnel(struct me_softc *sc)
442 {
443 
444 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
445 	if (ME_READY(sc)) {
446 		CK_LIST_REMOVE(sc, chain);
447 		CK_LIST_REMOVE(sc, srchash);
448 		ME_WAIT();
449 
450 		sc->me_src.s_addr = 0;
451 		sc->me_dst.s_addr = 0;
452 		ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
453 		if_link_state_change(ME2IFP(sc), LINK_STATE_DOWN);
454 	}
455 }
456 
457 static uint16_t
458 me_in_cksum(uint16_t *p, int nwords)
459 {
460 	uint32_t sum = 0;
461 
462 	while (nwords-- > 0)
463 		sum += *p++;
464 	sum = (sum >> 16) + (sum & 0xffff);
465 	sum += (sum >> 16);
466 	return (~sum);
467 }
468 
469 static int
470 me_input(struct mbuf *m, int off, int proto, void *arg)
471 {
472 	struct me_softc *sc = arg;
473 	struct mobhdr *mh;
474 	struct ifnet *ifp;
475 	struct ip *ip;
476 	int hlen;
477 
478 	NET_EPOCH_ASSERT();
479 
480 	ifp = ME2IFP(sc);
481 	/* checks for short packets */
482 	hlen = sizeof(struct mobhdr);
483 	if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
484 		hlen -= sizeof(struct in_addr);
485 	if (m->m_len < sizeof(struct ip) + hlen)
486 		m = m_pullup(m, sizeof(struct ip) + hlen);
487 	if (m == NULL)
488 		goto drop;
489 	mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
490 	/* check for wrong flags */
491 	if (mh->mob_flags & (~MOB_FLAGS_SP)) {
492 		m_freem(m);
493 		goto drop;
494 	}
495 	if (mh->mob_flags) {
496 	       if (hlen != sizeof(struct mobhdr)) {
497 			m_freem(m);
498 			goto drop;
499 	       }
500 	} else
501 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
502 	/* check mobile header checksum */
503 	if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
504 		m_freem(m);
505 		goto drop;
506 	}
507 #ifdef MAC
508 	mac_ifnet_create_mbuf(ifp, m);
509 #endif
510 	ip = mtod(m, struct ip *);
511 	ip->ip_dst = mh->mob_dst;
512 	ip->ip_p = mh->mob_proto;
513 	ip->ip_sum = 0;
514 	ip->ip_len = htons(m->m_pkthdr.len - hlen);
515 	if (mh->mob_flags)
516 		ip->ip_src = mh->mob_src;
517 	memmove(mtodo(m, hlen), ip, sizeof(struct ip));
518 	m_adj(m, hlen);
519 	m_clrprotoflags(m);
520 	m->m_pkthdr.rcvif = ifp;
521 	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
522 	M_SETFIB(m, ifp->if_fib);
523 	hlen = AF_INET;
524 	BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
525 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
526 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
527 	if ((ifp->if_flags & IFF_MONITOR) != 0)
528 		m_freem(m);
529 	else
530 		netisr_dispatch(NETISR_IP, m);
531 	return (IPPROTO_DONE);
532 drop:
533 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
534 	return (IPPROTO_DONE);
535 }
536 
537 static int
538 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
539    struct route *ro)
540 {
541 	uint32_t af;
542 
543 	if (dst->sa_family == AF_UNSPEC)
544 		bcopy(dst->sa_data, &af, sizeof(af));
545 	else
546 		af = RO_GET_FAMILY(ro, dst);
547 	m->m_pkthdr.csum_data = af;
548 	return (ifp->if_transmit(ifp, m));
549 }
550 
551 #define	MTAG_ME	1414491977
552 static int
553 me_transmit(struct ifnet *ifp, struct mbuf *m)
554 {
555 	ME_RLOCK_TRACKER;
556 	struct mobhdr mh;
557 	struct me_softc *sc;
558 	struct ip *ip;
559 	uint32_t af;
560 	int error, hlen, plen;
561 
562 	ME_RLOCK();
563 #ifdef MAC
564 	error = mac_ifnet_check_transmit(ifp, m);
565 	if (error != 0)
566 		goto drop;
567 #endif
568 	error = ENETDOWN;
569 	sc = ifp->if_softc;
570 	if (sc == NULL || !ME_READY(sc) ||
571 	    (ifp->if_flags & IFF_MONITOR) != 0 ||
572 	    (ifp->if_flags & IFF_UP) == 0 ||
573 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
574 	    (error = if_tunnel_check_nesting(ifp, m, MTAG_ME,
575 		V_max_me_nesting)) != 0) {
576 		m_freem(m);
577 		goto drop;
578 	}
579 	af = m->m_pkthdr.csum_data;
580 	if (af != AF_INET) {
581 		error = EAFNOSUPPORT;
582 		m_freem(m);
583 		goto drop;
584 	}
585 	if (m->m_len < sizeof(struct ip))
586 		m = m_pullup(m, sizeof(struct ip));
587 	if (m == NULL) {
588 		error = ENOBUFS;
589 		goto drop;
590 	}
591 	ip = mtod(m, struct ip *);
592 	/* Fragmented datagramms shouldn't be encapsulated */
593 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
594 		error = EINVAL;
595 		m_freem(m);
596 		goto drop;
597 	}
598 	mh.mob_proto = ip->ip_p;
599 	mh.mob_src = ip->ip_src;
600 	mh.mob_dst = ip->ip_dst;
601 	if (in_hosteq(sc->me_src, ip->ip_src)) {
602 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
603 		mh.mob_flags = 0;
604 	} else {
605 		hlen = sizeof(struct mobhdr);
606 		mh.mob_flags = MOB_FLAGS_SP;
607 	}
608 	BPF_MTAP2(ifp, &af, sizeof(af), m);
609 	plen = m->m_pkthdr.len;
610 	ip->ip_src = sc->me_src;
611 	ip->ip_dst = sc->me_dst;
612 	m->m_flags &= ~(M_BCAST|M_MCAST);
613 	M_SETFIB(m, sc->me_fibnum);
614 	M_PREPEND(m, hlen, M_NOWAIT);
615 	if (m == NULL) {
616 		error = ENOBUFS;
617 		goto drop;
618 	}
619 	if (m->m_len < sizeof(struct ip) + hlen)
620 		m = m_pullup(m, sizeof(struct ip) + hlen);
621 	if (m == NULL) {
622 		error = ENOBUFS;
623 		goto drop;
624 	}
625 	memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
626 	ip = mtod(m, struct ip *);
627 	ip->ip_len = htons(m->m_pkthdr.len);
628 	ip->ip_p = IPPROTO_MOBILE;
629 	ip->ip_sum = 0;
630 	mh.mob_csum = 0;
631 	mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
632 	bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
633 	error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
634 drop:
635 	if (error)
636 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
637 	else {
638 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
639 		if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
640 	}
641 	ME_RUNLOCK();
642 	return (error);
643 }
644 
645 static void
646 me_qflush(struct ifnet *ifp __unused)
647 {
648 
649 }
650 
651 static const struct srcaddrtab *me_srcaddrtab = NULL;
652 static const struct encaptab *ecookie = NULL;
653 static const struct encap_config me_encap_cfg = {
654 	.proto = IPPROTO_MOBILE,
655 	.min_length = sizeof(struct ip) + sizeof(struct mobhdr) -
656 	    sizeof(in_addr_t),
657 	.exact_match = ENCAP_DRV_LOOKUP,
658 	.lookup = me_lookup,
659 	.input = me_input
660 };
661 
662 static int
663 memodevent(module_t mod, int type, void *data)
664 {
665 
666 	switch (type) {
667 	case MOD_LOAD:
668 		me_srcaddrtab = ip_encap_register_srcaddr(me_srcaddr,
669 		    NULL, M_WAITOK);
670 		ecookie = ip_encap_attach(&me_encap_cfg, NULL, M_WAITOK);
671 		break;
672 	case MOD_UNLOAD:
673 		ip_encap_detach(ecookie);
674 		ip_encap_unregister_srcaddr(me_srcaddrtab);
675 		break;
676 	default:
677 		return (EOPNOTSUPP);
678 	}
679 	return (0);
680 }
681 
682 static moduledata_t me_mod = {
683 	"if_me",
684 	memodevent,
685 	0
686 };
687 
688 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
689 MODULE_VERSION(if_me, 1);
690