]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_me.c
Fix linux_common module build with KTR option.
[FreeBSD/FreeBSD.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 const struct protosw in_mobile_protosw = {
145         .pr_type =              SOCK_RAW,
146         .pr_domain =            &inetdomain,
147         .pr_protocol =          IPPROTO_MOBILE,
148         .pr_flags =             PR_ATOMIC|PR_ADDR,
149         .pr_input =             me_input,
150         .pr_output =            rip_output,
151         .pr_ctlinput =          rip_ctlinput,
152         .pr_ctloutput =         rip_ctloutput,
153         .pr_usrreqs =           &rip_usrreqs
154 };
155
156 static void
157 vnet_me_init(const void *unused __unused)
158 {
159         LIST_INIT(&V_me_softc_list);
160         ME_LIST_LOCK_INIT();
161         V_me_cloner = if_clone_simple(mename, me_clone_create,
162             me_clone_destroy, 0);
163 }
164 VNET_SYSINIT(vnet_me_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
165     vnet_me_init, NULL);
166
167 static void
168 vnet_me_uninit(const void *unused __unused)
169 {
170
171         if_clone_detach(V_me_cloner);
172         ME_LIST_LOCK_DESTROY();
173 }
174 VNET_SYSUNINIT(vnet_me_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
175     vnet_me_uninit, NULL);
176
177 static int
178 me_clone_create(struct if_clone *ifc, int unit, caddr_t params)
179 {
180         struct me_softc *sc;
181
182         sc = malloc(sizeof(struct me_softc), M_IFME, M_WAITOK | M_ZERO);
183         sc->me_fibnum = curthread->td_proc->p_fibnum;
184         ME2IFP(sc) = if_alloc(IFT_TUNNEL);
185         ME_LOCK_INIT(sc);
186         ME2IFP(sc)->if_softc = sc;
187         if_initname(ME2IFP(sc), mename, unit);
188
189         ME2IFP(sc)->if_mtu = MEMTU - sizeof(struct mobhdr);
190         ME2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
191         ME2IFP(sc)->if_output = me_output;
192         ME2IFP(sc)->if_ioctl = me_ioctl;
193         ME2IFP(sc)->if_transmit = me_transmit;
194         ME2IFP(sc)->if_qflush = me_qflush;
195         if_attach(ME2IFP(sc));
196         bpfattach(ME2IFP(sc), DLT_NULL, sizeof(u_int32_t));
197         ME_LIST_LOCK();
198         LIST_INSERT_HEAD(&V_me_softc_list, sc, me_list);
199         ME_LIST_UNLOCK();
200         return (0);
201 }
202
203 static void
204 me_clone_destroy(struct ifnet *ifp)
205 {
206         struct me_softc *sc;
207
208         sx_xlock(&me_ioctl_sx);
209         sc = ifp->if_softc;
210         me_delete_tunnel(ifp);
211         ME_LIST_LOCK();
212         LIST_REMOVE(sc, me_list);
213         ME_LIST_UNLOCK();
214         bpfdetach(ifp);
215         if_detach(ifp);
216         ifp->if_softc = NULL;
217         sx_xunlock(&me_ioctl_sx);
218
219         if_free(ifp);
220         ME_LOCK_DESTROY(sc);
221         free(sc, M_IFME);
222 }
223
224 static int
225 me_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
226 {
227         ME_RLOCK_TRACKER;
228         struct ifreq *ifr = (struct ifreq *)data;
229         struct sockaddr_in *src, *dst;
230         struct me_softc *sc;
231         int error;
232
233         switch (cmd) {
234         case SIOCSIFMTU:
235                 if (ifr->ifr_mtu < 576)
236                         return (EINVAL);
237                 ifp->if_mtu = ifr->ifr_mtu - sizeof(struct mobhdr);
238                 return (0);
239         case SIOCSIFADDR:
240                 ifp->if_flags |= IFF_UP;
241         case SIOCSIFFLAGS:
242         case SIOCADDMULTI:
243         case SIOCDELMULTI:
244                 return (0);
245         }
246         sx_xlock(&me_ioctl_sx);
247         sc = ifp->if_softc;
248         if (sc == NULL) {
249                 error = ENXIO;
250                 goto end;
251         }
252         error = 0;
253         switch (cmd) {
254         case SIOCSIFPHYADDR:
255                 src = (struct sockaddr_in *)
256                         &(((struct in_aliasreq *)data)->ifra_addr);
257                 dst = (struct sockaddr_in *)
258                         &(((struct in_aliasreq *)data)->ifra_dstaddr);
259                 if (src->sin_family != dst->sin_family ||
260                     src->sin_family != AF_INET ||
261                     src->sin_len != dst->sin_len ||
262                     src->sin_len != sizeof(struct sockaddr_in)) {
263                         error = EINVAL;
264                         break;
265                 }
266                 if (src->sin_addr.s_addr == INADDR_ANY ||
267                     dst->sin_addr.s_addr == INADDR_ANY) {
268                         error = EADDRNOTAVAIL;
269                         break;
270                 }
271                 error = me_set_tunnel(ifp, src, dst);
272                 break;
273         case SIOCDIFPHYADDR:
274                 me_delete_tunnel(ifp);
275                 break;
276         case SIOCGIFPSRCADDR:
277         case SIOCGIFPDSTADDR:
278                 ME_RLOCK(sc);
279                 if (!ME_READY(sc)) {
280                         error = EADDRNOTAVAIL;
281                         ME_RUNLOCK(sc);
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                 ME_RUNLOCK(sc);
297                 error = prison_if(curthread->td_ucred, sintosa(src));
298                 if (error != 0)
299                         memset(src, 0, sizeof(*src));
300                 break;
301         case SIOCGTUNFIB:
302                 ifr->ifr_fib = sc->me_fibnum;
303                 break;
304         case SIOCSTUNFIB:
305                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
306                         break;
307                 if (ifr->ifr_fib >= rt_numfibs)
308                         error = EINVAL;
309                 else
310                         sc->me_fibnum = ifr->ifr_fib;
311                 break;
312         default:
313                 error = EINVAL;
314                 break;
315         }
316 end:
317         sx_xunlock(&me_ioctl_sx);
318         return (error);
319 }
320
321 static int
322 me_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
323 {
324         ME_RLOCK_TRACKER;
325         struct me_softc *sc;
326         struct ip *ip;
327         int ret;
328
329         sc = (struct me_softc *)arg;
330         if ((ME2IFP(sc)->if_flags & IFF_UP) == 0)
331                 return (0);
332
333         M_ASSERTPKTHDR(m);
334
335         if (m->m_pkthdr.len < sizeof(struct ip) + sizeof(struct mobhdr) -
336             sizeof(struct in_addr))
337                 return (0);
338
339         ret = 0;
340         ME_RLOCK(sc);
341         if (ME_READY(sc)) {
342                 ip = mtod(m, struct ip *);
343                 if (sc->me_src.s_addr == ip->ip_dst.s_addr &&
344                     sc->me_dst.s_addr == ip->ip_src.s_addr)
345                         ret = 32 * 2;
346         }
347         ME_RUNLOCK(sc);
348         return (ret);
349 }
350
351 static int
352 me_set_tunnel(struct ifnet *ifp, struct sockaddr_in *src,
353     struct sockaddr_in *dst)
354 {
355         struct me_softc *sc, *tsc;
356
357         sx_assert(&me_ioctl_sx, SA_XLOCKED);
358         ME_LIST_LOCK();
359         sc = ifp->if_softc;
360         LIST_FOREACH(tsc, &V_me_softc_list, me_list) {
361                 if (tsc == sc || !ME_READY(tsc))
362                         continue;
363                 if (tsc->me_src.s_addr == src->sin_addr.s_addr &&
364                     tsc->me_dst.s_addr == dst->sin_addr.s_addr) {
365                         ME_LIST_UNLOCK();
366                         return (EADDRNOTAVAIL);
367                 }
368         }
369         ME_LIST_UNLOCK();
370
371         ME_WLOCK(sc);
372         sc->me_dst = dst->sin_addr;
373         sc->me_src = src->sin_addr;
374         ME_WUNLOCK(sc);
375
376         if (sc->me_ecookie == NULL)
377                 sc->me_ecookie = encap_attach_func(AF_INET, IPPROTO_MOBILE,
378                     me_encapcheck, &in_mobile_protosw, sc);
379         if (sc->me_ecookie != NULL)
380                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
381         return (0);
382 }
383
384 static void
385 me_delete_tunnel(struct ifnet *ifp)
386 {
387         struct me_softc *sc = ifp->if_softc;
388
389         sx_assert(&me_ioctl_sx, SA_XLOCKED);
390         if (sc->me_ecookie != NULL)
391                 encap_detach(sc->me_ecookie);
392         sc->me_ecookie = NULL;
393         ME_WLOCK(sc);
394         sc->me_src.s_addr = 0;
395         sc->me_dst.s_addr = 0;
396         ME_WUNLOCK(sc);
397         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
398 }
399
400 static uint16_t
401 me_in_cksum(uint16_t *p, int nwords)
402 {
403         uint32_t sum = 0;
404
405         while (nwords-- > 0)
406                 sum += *p++;
407         sum = (sum >> 16) + (sum & 0xffff);
408         sum += (sum >> 16);
409         return (~sum);
410 }
411
412 int
413 me_input(struct mbuf **mp, int *offp, int proto)
414 {
415         struct me_softc *sc;
416         struct mobhdr *mh;
417         struct ifnet *ifp;
418         struct mbuf *m;
419         struct ip *ip;
420         int hlen;
421
422         m = *mp;
423         sc = encap_getarg(m);
424         KASSERT(sc != NULL, ("encap_getarg returned NULL"));
425
426         ifp = ME2IFP(sc);
427         /* checks for short packets */
428         hlen = sizeof(struct mobhdr);
429         if (m->m_pkthdr.len < sizeof(struct ip) + hlen)
430                 hlen -= sizeof(struct in_addr);
431         if (m->m_len < sizeof(struct ip) + hlen)
432                 m = m_pullup(m, sizeof(struct ip) + hlen);
433         if (m == NULL)
434                 goto drop;
435         mh = (struct mobhdr *)mtodo(m, sizeof(struct ip));
436         /* check for wrong flags */
437         if (mh->mob_flags & (~MOB_FLAGS_SP)) {
438                 m_freem(m);
439                 goto drop;
440         }
441         if (mh->mob_flags) {
442                if (hlen != sizeof(struct mobhdr)) {
443                         m_freem(m);
444                         goto drop;
445                }
446         } else
447                 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
448         /* check mobile header checksum */
449         if (me_in_cksum((uint16_t *)mh, hlen / sizeof(uint16_t)) != 0) {
450                 m_freem(m);
451                 goto drop;
452         }
453 #ifdef MAC
454         mac_ifnet_create_mbuf(ifp, m);
455 #endif
456         ip = mtod(m, struct ip *);
457         ip->ip_dst = mh->mob_dst;
458         ip->ip_p = mh->mob_proto;
459         ip->ip_sum = 0;
460         ip->ip_len = htons(m->m_pkthdr.len - hlen);
461         if (mh->mob_flags)
462                 ip->ip_src = mh->mob_src;
463         memmove(mtodo(m, hlen), ip, sizeof(struct ip));
464         m_adj(m, hlen);
465         m_clrprotoflags(m);
466         m->m_pkthdr.rcvif = ifp;
467         m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
468         M_SETFIB(m, ifp->if_fib);
469         hlen = AF_INET;
470         BPF_MTAP2(ifp, &hlen, sizeof(hlen), m);
471         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
472         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
473         if ((ifp->if_flags & IFF_MONITOR) != 0)
474                 m_freem(m);
475         else
476                 netisr_dispatch(NETISR_IP, m);
477         return (IPPROTO_DONE);
478 drop:
479         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
480         return (IPPROTO_DONE);
481 }
482
483 #define MTAG_ME 1414491977
484 static int
485 me_check_nesting(struct ifnet *ifp, struct mbuf *m)
486 {
487         struct m_tag *mtag;
488         int count;
489
490         count = 1;
491         mtag = NULL;
492         while ((mtag = m_tag_locate(m, MTAG_ME, 0, mtag)) != NULL) {
493                 if (*(struct ifnet **)(mtag + 1) == ifp) {
494                         log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
495                         return (EIO);
496                 }
497                 count++;
498         }
499         if (count > V_max_me_nesting) {
500                 log(LOG_NOTICE,
501                     "%s: if_output recursively called too many times(%d)\n",
502                     ifp->if_xname, count);
503                 return (EIO);
504         }
505         mtag = m_tag_alloc(MTAG_ME, 0, sizeof(struct ifnet *), M_NOWAIT);
506         if (mtag == NULL)
507                 return (ENOMEM);
508         *(struct ifnet **)(mtag + 1) = ifp;
509         m_tag_prepend(m, mtag);
510         return (0);
511 }
512
513 static int
514 me_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
515    struct route *ro)
516 {
517         uint32_t af;
518         int error;
519
520 #ifdef MAC
521         error = mac_ifnet_check_transmit(ifp, m);
522         if (error != 0)
523                 goto drop;
524 #endif
525         if ((ifp->if_flags & IFF_MONITOR) != 0 ||
526             (ifp->if_flags & IFF_UP) == 0) {
527                 error = ENETDOWN;
528                 goto drop;
529         }
530
531         error = me_check_nesting(ifp, m);
532         if (error != 0)
533                 goto drop;
534
535         m->m_flags &= ~(M_BCAST|M_MCAST);
536         if (dst->sa_family == AF_UNSPEC)
537                 bcopy(dst->sa_data, &af, sizeof(af));
538         else
539                 af = dst->sa_family;
540         if (af != AF_INET) {
541                 error = EAFNOSUPPORT;
542                 goto drop;
543         }
544         BPF_MTAP2(ifp, &af, sizeof(af), m);
545         return (ifp->if_transmit(ifp, m));
546 drop:
547         m_freem(m);
548         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
549         return (error);
550 }
551
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         int error, hlen, plen;
560
561         sc = ifp->if_softc;
562         if (sc == NULL) {
563                 error = ENETDOWN;
564                 m_freem(m);
565                 goto drop;
566         }
567         if (m->m_len < sizeof(struct ip))
568                 m = m_pullup(m, sizeof(struct ip));
569         if (m == NULL) {
570                 error = ENOBUFS;
571                 goto drop;
572         }
573         ip = mtod(m, struct ip *);
574         /* Fragmented datagramms shouldn't be encapsulated */
575         if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
576                 error = EINVAL;
577                 m_freem(m);
578                 goto drop;
579         }
580         mh.mob_proto = ip->ip_p;
581         mh.mob_src = ip->ip_src;
582         mh.mob_dst = ip->ip_dst;
583         ME_RLOCK(sc);
584         if (!ME_READY(sc)) {
585                 ME_RUNLOCK(sc);
586                 error = ENETDOWN;
587                 m_freem(m);
588                 goto drop;
589         }
590         if (in_hosteq(sc->me_src, ip->ip_src)) {
591                 hlen = sizeof(struct mobhdr) - sizeof(struct in_addr);
592                 mh.mob_flags = 0;
593         } else {
594                 hlen = sizeof(struct mobhdr);
595                 mh.mob_flags = MOB_FLAGS_SP;
596         }
597         plen = m->m_pkthdr.len;
598         ip->ip_src = sc->me_src;
599         ip->ip_dst = sc->me_dst;
600         M_SETFIB(m, sc->me_fibnum);
601         ME_RUNLOCK(sc);
602         M_PREPEND(m, hlen, M_NOWAIT);
603         if (m == NULL) {
604                 error = ENOBUFS;
605                 goto drop;
606         }
607         if (m->m_len < sizeof(struct ip) + hlen)
608                 m = m_pullup(m, sizeof(struct ip) + hlen);
609         if (m == NULL) {
610                 error = ENOBUFS;
611                 goto drop;
612         }
613         memmove(mtod(m, void *), mtodo(m, hlen), sizeof(struct ip));
614         ip = mtod(m, struct ip *);
615         ip->ip_len = htons(m->m_pkthdr.len);
616         ip->ip_p = IPPROTO_MOBILE;
617         ip->ip_sum = 0;
618         mh.mob_csum = 0;
619         mh.mob_csum = me_in_cksum((uint16_t *)&mh, hlen / sizeof(uint16_t));
620         bcopy(&mh, mtodo(m, sizeof(struct ip)), hlen);
621         error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
622 drop:
623         if (error)
624                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
625         else {
626                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
627                 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
628         }
629         return (error);
630 }
631
632 static void
633 me_qflush(struct ifnet *ifp __unused)
634 {
635
636 }
637
638 static int
639 memodevent(module_t mod, int type, void *data)
640 {
641
642         switch (type) {
643         case MOD_LOAD:
644         case MOD_UNLOAD:
645                 break;
646         default:
647                 return (EOPNOTSUPP);
648         }
649         return (0);
650 }
651
652 static moduledata_t me_mod = {
653         "if_me",
654         memodevent,
655         0
656 };
657
658 DECLARE_MODULE(if_me, me_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
659 MODULE_VERSION(if_me, 1);