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