FreeBSD/Linux Kernel Cross Reference
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 __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, 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_ME)) != 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 MPASS(in_epoch(net_epoch_preempt));
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 MPASS(in_epoch(net_epoch_preempt));
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 me_softc *tmp;
407
408 sx_assert(&me_ioctl_sx, SA_XLOCKED);
409
410 if (V_me_hashtbl == NULL) {
411 V_me_hashtbl = me_hashinit();
412 V_me_srchashtbl = me_hashinit();
413 }
414
415 if (sc->me_src.s_addr == src && sc->me_dst.s_addr == dst)
416 return (0);
417
418 CK_LIST_FOREACH(tmp, &ME_HASH(src, dst), chain) {
419 if (tmp == sc)
420 continue;
421 if (tmp->me_src.s_addr == src &&
422 tmp->me_dst.s_addr == dst)
423 return (EADDRNOTAVAIL);
424 }
425
426 me_delete_tunnel(sc);
427 sc->me_dst.s_addr = dst;
428 sc->me_src.s_addr = src;
429 CK_LIST_INSERT_HEAD(&ME_HASH(src, dst), sc, chain);
430 CK_LIST_INSERT_HEAD(&ME_SRCHASH(src), sc, srchash);
431
432 me_set_running(sc);
433 if_link_state_change(ME2IFP(sc), LINK_STATE_UP);
434 return (0);
435 }
436
437 static void
438 me_delete_tunnel(struct me_softc *sc)
439 {
440
441 sx_assert(&me_ioctl_sx, SA_XLOCKED);
442 if (ME_READY(sc)) {
443 CK_LIST_REMOVE(sc, chain);
444 CK_LIST_REMOVE(sc, srchash);
445 ME_WAIT();
446
447 sc->me_src.s_addr = 0;
448 sc->me_dst.s_addr = 0;
449 ME2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
450 if_link_state_change(ME2IFP(sc), LINK_STATE_DOWN);
451 }
452 }
453
454 static uint16_t
455 me_in_cksum(uint16_t *p, int nwords)
456 {
457 uint32_t sum = 0;
458
459 while (nwords-- > 0)
460 sum += *p++;
461 sum = (sum >> 16) + (sum & 0xffff);
462 sum += (sum >> 16);
463 return (~sum);
464 }
465
466 static int
467 me_input(struct mbuf *m, int off, int proto, void *arg)
468 {
469 struct me_softc *sc = arg;
470 struct mobhdr *mh;
471 struct ifnet *ifp;
472 struct ip *ip;
473 int hlen;
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 __unused)
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 = dst->sa_family;
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);
Cache object: b986881574050de7b11d919ee3513a78
|