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