xref: /freebsd/sys/net/if_me.c (revision c6db8143eda5c775467145ac73e8ebec47afdd8f)
1 /*-
2  * Copyright (c) 2014 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/jail.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/libkern.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/protosw.h>
41 #include <sys/rmlock.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/sx.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/systm.h>
48 
49 #include <net/bpf.h>
50 #include <net/ethernet.h>
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_clone.h>
54 #include <net/if_types.h>
55 #include <net/netisr.h>
56 #include <net/vnet.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/ip_encap.h>
64 
65 #include <machine/in_cksum.h>
66 #include <security/mac/mac_framework.h>
67 
68 #define	MEMTU			1500
69 static const char mename[] = "me";
70 static MALLOC_DEFINE(M_IFME, mename, "Minimal Encapsulation for IP");
71 static VNET_DEFINE(struct mtx, me_mtx);
72 #define	V_me_mtx	VNET(me_mtx)
73 /* Minimal forwarding header RFC 2004 */
74 struct mobhdr {
75 	uint8_t		mob_proto;	/* protocol */
76 	uint8_t		mob_flags;	/* flags */
77 #define	MOB_FLAGS_SP	0x80		/* source present */
78 	uint16_t	mob_csum;	/* header checksum */
79 	struct in_addr	mob_dst;	/* original destination address */
80 	struct in_addr	mob_src;	/* original source addr (optional) */
81 } __packed;
82 
83 struct me_softc {
84 	struct ifnet		*me_ifp;
85 	LIST_ENTRY(me_softc)	me_list;
86 	struct rmlock		me_lock;
87 	u_int			me_fibnum;
88 	const struct encaptab	*me_ecookie;
89 	struct in_addr		me_src;
90 	struct in_addr		me_dst;
91 };
92 #define	ME2IFP(sc)		((sc)->me_ifp)
93 #define	ME_READY(sc)		((sc)->me_src.s_addr != 0)
94 #define	ME_LOCK_INIT(sc)	rm_init(&(sc)->me_lock, "me softc")
95 #define	ME_LOCK_DESTROY(sc)	rm_destroy(&(sc)->me_lock)
96 #define	ME_RLOCK_TRACKER	struct rm_priotracker me_tracker
97 #define	ME_RLOCK(sc)		rm_rlock(&(sc)->me_lock, &me_tracker)
98 #define	ME_RUNLOCK(sc)		rm_runlock(&(sc)->me_lock, &me_tracker)
99 #define	ME_RLOCK_ASSERT(sc)	rm_assert(&(sc)->me_lock, RA_RLOCKED)
100 #define	ME_WLOCK(sc)		rm_wlock(&(sc)->me_lock)
101 #define	ME_WUNLOCK(sc)		rm_wunlock(&(sc)->me_lock)
102 #define	ME_WLOCK_ASSERT(sc)	rm_assert(&(sc)->me_lock, RA_WLOCKED)
103 
104 #define	ME_LIST_LOCK_INIT(x)	mtx_init(&V_me_mtx, "me_mtx", NULL, MTX_DEF)
105 #define	ME_LIST_LOCK_DESTROY(x)	mtx_destroy(&V_me_mtx)
106 #define	ME_LIST_LOCK(x)		mtx_lock(&V_me_mtx)
107 #define	ME_LIST_UNLOCK(x)	mtx_unlock(&V_me_mtx)
108 
109 static VNET_DEFINE(LIST_HEAD(, me_softc), me_softc_list);
110 #define	V_me_softc_list	VNET(me_softc_list)
111 static struct sx me_ioctl_sx;
112 SX_SYSINIT(me_ioctl_sx, &me_ioctl_sx, "me_ioctl");
113 
114 static int	me_clone_create(struct if_clone *, int, caddr_t);
115 static void	me_clone_destroy(struct ifnet *);
116 static VNET_DEFINE(struct if_clone *, me_cloner);
117 #define	V_me_cloner	VNET(me_cloner)
118 
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);
125 
126 static int	me_set_tunnel(struct ifnet *, struct sockaddr_in *,
127     struct sockaddr_in *);
128 static void	me_delete_tunnel(struct ifnet *);
129 
130 SYSCTL_DECL(_net_link);
131 static SYSCTL_NODE(_net_link, IFT_TUNNEL, me, CTLFLAG_RW, 0,
132     "Minimal Encapsulation for IP (RFC 2004)");
133 #ifndef MAX_ME_NEST
134 #define MAX_ME_NEST 1
135 #endif
136 
137 static VNET_DEFINE(int, max_me_nesting) = MAX_ME_NEST;
138 #define	V_max_me_nesting	VNET(max_me_nesting)
139 SYSCTL_INT(_net_link_me, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
140     &VNET_NAME(max_me_nesting), 0, "Max nested tunnels");
141 
142 extern struct domain inetdomain;
143 static const struct protosw in_mobile_protosw = {
144 	.pr_type =		SOCK_RAW,
145 	.pr_domain =		&inetdomain,
146 	.pr_protocol =		IPPROTO_MOBILE,
147 	.pr_flags =		PR_ATOMIC|PR_ADDR,
148 	.pr_input =		me_input,
149 	.pr_output =		rip_output,
150 	.pr_ctlinput =		rip_ctlinput,
151 	.pr_ctloutput =		rip_ctloutput,
152 	.pr_usrreqs =		&rip_usrreqs
153 };
154 
155 static void
156 vnet_me_init(const void *unused __unused)
157 {
158 	LIST_INIT(&V_me_softc_list);
159 	ME_LIST_LOCK_INIT();
160 	V_me_cloner = if_clone_simple(mename, me_clone_create,
161 	    me_clone_destroy, 0);
162 }
163 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
164     vnet_me_init, NULL);
165 
166 static void
167 vnet_me_uninit(const void *unused __unused)
168 {
169 
170 	if_clone_detach(V_me_cloner);
171 	ME_LIST_LOCK_DESTROY();
172 }
173 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
174     vnet_me_uninit, NULL);
175 
176 static int
177 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
178 {
179 	struct me_softc *sc;
180 
181 	sc = malloc(sizeof(struct me_softc), M_IFME, M_WAITOK | M_ZERO);
182 	sc->me_fibnum = curthread->td_proc->p_fibnum;
183 	ME2IFP(sc) = if_alloc(IFT_TUNNEL);
184 	ME_LOCK_INIT(sc);
185 	ME2IFP(sc)->if_softc = sc;
186 	if_initname(ME2IFP(sc), mename, unit);
187 
188 	ME2IFP(sc)->if_mtu = MEMTU - sizeof(struct mobhdr);
189 	ME2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
190 	ME2IFP(sc)->if_output = me_output;
191 	ME2IFP(sc)->if_ioctl = me_ioctl;
192 	ME2IFP(sc)->if_transmit = me_transmit;
193 	ME2IFP(sc)->if_qflush = me_qflush;
194 	if_attach(ME2IFP(sc));
195 	bpfattach(ME2IFP(sc), DLT_NULL, sizeof(u_int32_t));
196 	ME_LIST_LOCK();
197 	LIST_INSERT_HEAD(&V_me_softc_list, sc, me_list);
198 	ME_LIST_UNLOCK();
199 	return (0);
200 }
201 
202 static void
203 me_clone_destroy(struct ifnet *ifp)
204 {
205 	struct me_softc *sc;
206 
207 	sx_xlock(&me_ioctl_sx);
208 	sc = ifp->if_softc;
209 	me_delete_tunnel(ifp);
210 	ME_LIST_LOCK();
211 	LIST_REMOVE(sc, me_list);
212 	ME_LIST_UNLOCK();
213 	bpfdetach(ifp);
214 	if_detach(ifp);
215 	ifp->if_softc = NULL;
216 	sx_xunlock(&me_ioctl_sx);
217 
218 	if_free(ifp);
219 	ME_LOCK_DESTROY(sc);
220 	free(sc, M_IFME);
221 }
222 
223 static int
224 me_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
225 {
226 	ME_RLOCK_TRACKER;
227 	struct ifreq *ifr = (struct ifreq *)data;
228 	struct sockaddr_in *src, *dst;
229 	struct me_softc *sc;
230 	int error;
231 
232 	switch (cmd) {
233 	case SIOCSIFMTU:
234 		if (ifr->ifr_mtu < 576)
235 			return (EINVAL);
236 		ifp->if_mtu = ifr->ifr_mtu - sizeof(struct mobhdr);
237 		return (0);
238 	case SIOCSIFADDR:
239 		ifp->if_flags |= IFF_UP;
240 	case SIOCSIFFLAGS:
241 	case SIOCADDMULTI:
242 	case SIOCDELMULTI:
243 		return (0);
244 	}
245 	sx_xlock(&me_ioctl_sx);
246 	sc = ifp->if_softc;
247 	if (sc == NULL) {
248 		error = ENXIO;
249 		goto end;
250 	}
251 	error = 0;
252 	switch (cmd) {
253 	case SIOCSIFPHYADDR:
254 		src = (struct sockaddr_in *)
255 			&(((struct in_aliasreq *)data)->ifra_addr);
256 		dst = (struct sockaddr_in *)
257 			&(((struct in_aliasreq *)data)->ifra_dstaddr);
258 		if (src->sin_family != dst->sin_family ||
259 		    src->sin_family != AF_INET ||
260 		    src->sin_len != dst->sin_len ||
261 		    src->sin_len != sizeof(struct sockaddr_in)) {
262 			error = EINVAL;
263 			break;
264 		}
265 		if (src->sin_addr.s_addr == INADDR_ANY ||
266 		    dst->sin_addr.s_addr == INADDR_ANY) {
267 			error = EADDRNOTAVAIL;
268 			break;
269 		}
270 		error = me_set_tunnel(ifp, src, dst);
271 		break;
272 	case SIOCDIFPHYADDR:
273 		me_delete_tunnel(ifp);
274 		break;
275 	case SIOCGIFPSRCADDR:
276 	case SIOCGIFPDSTADDR:
277 		ME_RLOCK(sc);
278 		if (!ME_READY(sc)) {
279 			error = EADDRNOTAVAIL;
280 			ME_RUNLOCK(sc);
281 			break;
282 		}
283 		src = (struct sockaddr_in *)&ifr->ifr_addr;
284 		memset(src, 0, sizeof(*src));
285 		src->sin_family = AF_INET;
286 		src->sin_len = sizeof(*src);
287 		switch (cmd) {
288 		case SIOCGIFPSRCADDR:
289 			src->sin_addr = sc->me_src;
290 			break;
291 		case SIOCGIFPDSTADDR:
292 			src->sin_addr = sc->me_dst;
293 			break;
294 		}
295 		ME_RUNLOCK(sc);
296 		error = prison_if(curthread->td_ucred, sintosa(src));
297 		if (error != 0)
298 			memset(src, 0, sizeof(*src));
299 		break;
300 	default:
301 		error = EINVAL;
302 		break;
303 	}
304 end:
305 	sx_xunlock(&me_ioctl_sx);
306 	return (error);
307 }
308 
309 static int
310 me_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
311 {
312 	ME_RLOCK_TRACKER;
313 	struct me_softc *sc;
314 	struct ip *ip;
315 	int ret;
316 
317 	sc = (struct me_softc *)arg;
318 	if ((ME2IFP(sc)->if_flags & IFF_UP) == 0)
319 		return (0);
320 
321 	M_ASSERTPKTHDR(m);
322 
323 	if (m->m_pkthdr.len < sizeof(struct ip) + sizeof(struct mobhdr) -
324 	    sizeof(struct in_addr))
325 		return (0);
326 
327 	ret = 0;
328 	ME_RLOCK(sc);
329 	if (ME_READY(sc)) {
330 		ip = mtod(m, struct ip *);
331 		if (sc->me_src.s_addr == ip->ip_dst.s_addr &&
332 		    sc->me_dst.s_addr == ip->ip_src.s_addr)
333 			ret = 32 * 2;
334 	}
335 	ME_RUNLOCK(sc);
336 	return (ret);
337 }
338 
339 static int
340 me_set_tunnel(struct ifnet *ifp, struct sockaddr_in *src,
341     struct sockaddr_in *dst)
342 {
343 	struct me_softc *sc, *tsc;
344 
345 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
346 	ME_LIST_LOCK();
347 	sc = ifp->if_softc;
348 	LIST_FOREACH(tsc, &V_me_softc_list, me_list) {
349 		if (tsc == sc || !ME_READY(tsc))
350 			continue;
351 		if (tsc->me_src.s_addr == src->sin_addr.s_addr &&
352 		    tsc->me_dst.s_addr == dst->sin_addr.s_addr) {
353 			ME_LIST_UNLOCK();
354 			return (EADDRNOTAVAIL);
355 		}
356 	}
357 	ME_LIST_UNLOCK();
358 
359 	ME_WLOCK(sc);
360 	sc->me_dst = dst->sin_addr;
361 	sc->me_src = src->sin_addr;
362 	ME_WUNLOCK(sc);
363 
364 	if (sc->me_ecookie == NULL)
365 		sc->me_ecookie = encap_attach_func(AF_INET, IPPROTO_MOBILE,
366 		    me_encapcheck, &in_mobile_protosw, sc);
367 	if (sc->me_ecookie != NULL)
368 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
369 	return (0);
370 }
371 
372 static void
373 me_delete_tunnel(struct ifnet *ifp)
374 {
375 	struct me_softc *sc = ifp->if_softc;
376 
377 	sx_assert(&me_ioctl_sx, SA_XLOCKED);
378 	if (sc->me_ecookie != NULL)
379 		encap_detach(sc->me_ecookie);
380 	sc->me_ecookie = NULL;
381 	ME_WLOCK(sc);
382 	sc->me_src.s_addr = 0;
383 	sc->me_dst.s_addr = 0;
384 	ME_WUNLOCK(sc);
385 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
386 }
387 
388 static uint16_t
389 me_in_cksum(uint16_t *p, int nwords)
390 {
391 	uint32_t sum = 0;
392 
393 	while (nwords-- > 0)
394 		sum += *p++;
395 	sum = (sum >> 16) + (sum & 0xffff);
396 	sum += (sum >> 16);
397 	return (~sum);
398 }
399 
400 int
401 me_input(struct mbuf **mp, int *offp, int proto)
402 {
403 	struct me_softc *sc;
404 	struct mobhdr *mh;
405 	struct ifnet *ifp;
406 	struct mbuf *m;
407 	struct ip *ip;
408 	int hlen;
409 
410 	m = *mp;
411 	sc = encap_getarg(m);
412 	KASSERT(sc != NULL, ("encap_getarg returned NULL"));
413 
414 	ifp = ME2IFP(sc);
415 	/* checks for short packets */
416 	hlen = sizeof(struct mobhdr);
417 	if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
418 		hlen -= sizeof(struct in_addr);
419 	if (m->m_len < sizeof(struct ip) + hlen)
420 		m = m_pullup(m, sizeof(struct ip) + hlen);
421 	if (m == NULL)
422 		goto drop;
423 	mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
424 	/* check for wrong flags */
425 	if (mh->mob_flags & (~MOB_FLAGS_SP)) {
426 		m_freem(m);
427 		goto drop;
428 	}
429 	if (mh->mob_flags) {
430 	       if (hlen != sizeof(struct mobhdr)) {
431 			m_freem(m);
432 			goto drop;
433 	       }
434 	} else
435 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
436 	/* check mobile header checksum */
437 	if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
438 		m_freem(m);
439 		goto drop;
440 	}
441 #ifdef MAC
442 	mac_ifnet_create_mbuf(ifp, m);
443 #endif
444 	ip = mtod(m, struct ip *);
445 	ip->ip_dst = mh->mob_dst;
446 	ip->ip_p = mh->mob_proto;
447 	ip->ip_sum = 0;
448 	ip->ip_len = htons(m->m_pkthdr.len - hlen);
449 	if (mh->mob_flags)
450 		ip->ip_src = mh->mob_src;
451 	memmove(mtodo(m, hlen), ip, sizeof(struct ip));
452 	m_adj(m, hlen);
453 	m_clrprotoflags(m);
454 	m->m_pkthdr.rcvif = ifp;
455 	m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
456 	M_SETFIB(m, sc->me_fibnum);
457 	hlen = AF_INET;
458 	BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
459 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
460 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
461 	if ((ifp->if_flags & IFF_MONITOR) != 0)
462 		m_freem(m);
463 	else
464 		netisr_dispatch(NETISR_IP, m);
465 	return (IPPROTO_DONE);
466 drop:
467 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
468 	return (IPPROTO_DONE);
469 }
470 
471 #define	MTAG_ME	1414491977
472 static int
473 me_check_nesting(struct ifnet *ifp, struct mbuf *m)
474 {
475 	struct m_tag *mtag;
476 	int count;
477 
478 	count = 1;
479 	mtag = NULL;
480 	while ((mtag = m_tag_locate(m, MTAG_ME, 0, NULL)) != NULL) {
481 		if (*(struct ifnet **)(mtag + 1) == ifp) {
482 			log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
483 			return (EIO);
484 		}
485 		count++;
486 	}
487 	if (count > V_max_me_nesting) {
488 		log(LOG_NOTICE,
489 		    "%s: if_output recursively called too many times(%d)\n",
490 		    ifp->if_xname, count);
491 		return (EIO);
492 	}
493 	mtag = m_tag_alloc(MTAG_ME, 0, sizeof(struct ifnet *), M_NOWAIT);
494 	if (mtag == NULL)
495 		return (ENOMEM);
496 	*(struct ifnet **)(mtag + 1) = ifp;
497 	m_tag_prepend(m, mtag);
498 	return (0);
499 }
500 
501 static int
502 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
503    struct route *ro)
504 {
505 	uint32_t af;
506 	int error;
507 
508 #ifdef MAC
509 	error = mac_ifnet_check_transmit(ifp, m);
510 	if (error != 0)
511 		goto drop;
512 #endif
513 	if ((ifp->if_flags & IFF_MONITOR) != 0 ||
514 	    (ifp->if_flags & IFF_UP) == 0) {
515 		error = ENETDOWN;
516 		goto drop;
517 	}
518 
519 	error = me_check_nesting(ifp, m);
520 	if (error != 0)
521 		goto drop;
522 
523 	m->m_flags &= ~(M_BCAST|M_MCAST);
524 	if (dst->sa_family == AF_UNSPEC)
525 		bcopy(dst->sa_data, &af, sizeof(af));
526 	else
527 		af = dst->sa_family;
528 	if (af != AF_INET) {
529 		error = EAFNOSUPPORT;
530 		goto drop;
531 	}
532 	BPF_MTAP2(ifp, &af, sizeof(af), m);
533 	return (ifp->if_transmit(ifp, m));
534 drop:
535 	m_freem(m);
536 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
537 	return (error);
538 }
539 
540 static int
541 me_transmit(struct ifnet *ifp, struct mbuf *m)
542 {
543 	ME_RLOCK_TRACKER;
544 	struct mobhdr mh;
545 	struct me_softc *sc;
546 	struct ip *ip;
547 	int error, hlen, plen;
548 
549 	sc = ifp->if_softc;
550 	if (sc == NULL) {
551 		error = ENETDOWN;
552 		m_freem(m);
553 		goto drop;
554 	}
555 	if (m->m_len < sizeof(struct ip))
556 		m = m_pullup(m, sizeof(struct ip));
557 	if (m == NULL) {
558 		error = ENOBUFS;
559 		goto drop;
560 	}
561 	ip = mtod(m, struct ip *);
562 	/* Fragmented datagramms shouldn't be encapsulated */
563 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
564 		error = EINVAL;
565 		m_freem(m);
566 		goto drop;
567 	}
568 	mh.mob_proto = ip->ip_p;
569 	mh.mob_src = ip->ip_src;
570 	mh.mob_dst = ip->ip_dst;
571 	ME_RLOCK(sc);
572 	if (!ME_READY(sc)) {
573 		ME_RUNLOCK(sc);
574 		error = ENETDOWN;
575 		m_freem(m);
576 		goto drop;
577 	}
578 	if (in_hosteq(sc->me_src, ip->ip_src)) {
579 		hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
580 		mh.mob_flags = 0;
581 	} else {
582 		hlen = sizeof(struct mobhdr);
583 		mh.mob_flags = MOB_FLAGS_SP;
584 	}
585 	plen = m->m_pkthdr.len;
586 	ip->ip_src = sc->me_src;
587 	ip->ip_dst = sc->me_dst;
588 	M_SETFIB(m, sc->me_fibnum);
589 	ME_RUNLOCK(sc);
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 	return (error);
618 }
619 
620 static void
621 me_qflush(struct ifnet *ifp __unused)
622 {
623 
624 }
625 
626 static int
627 memodevent(module_t mod, int type, void *data)
628 {
629 
630 	switch (type) {
631 	case MOD_LOAD:
632 	case MOD_UNLOAD:
633 		break;
634 	default:
635 		return (EOPNOTSUPP);
636 	}
637 	return (0);
638 }
639 
640 static moduledata_t me_mod = {
641 	"if_me",
642 	memodevent,
643 	0
644 };
645 
646 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
647 MODULE_VERSION(if_me, 1);
648