Subject: null interface implementation
To: None <tech-net@netbsd.org>
From: Keijiro Ehara <popo@wide.ad.jp>
List: tech-net
Date: 08/22/2002 03:31:51
--Multipart_Thu_Aug_22_03:31:51_2002-1
Content-Type: text/plain; charset=US-ASCII

I attached a small patch to use null0 network interface
for -current. This interface is similar to Cisco's Null0
interface. Neither forward nor receive any packets.
But you can dump packets routed to this interface by tapping
bpf.

To use null0, just add "pseudo-device null" to your kernel
configuration file.

This interface is useful for route aggregation and simple
packet filtering. With zebra routing daemons or so, you can
make routes to this interface.
e.g. ip route 10.0.0.0/8 null0
In this case, if 10.0.0.0/8 is the longest match for packets,
these packets are sent to this interface and just discarded.
(simple filtering)

If you make a route like:
ipv6 route 3ffe:501:100c:d000::/52 null0
you can advertise this aggregated static route via IGP and so on.

By setting link0 flag you can return unreach messages.
This is also helpful in some case.

These can also be done with lo0 and ipf.
But null0 is simpler way to implement this.
How about using this?

If any suggestion or objection, please tell me.
Thank you.


--
Keijiro Ehara <popo@sfc.wide.ad.jp>



--Multipart_Thu_Aug_22_03:31:51_2002-1
Content-Type: application/octet-stream; type=patch
Content-Disposition: attachment; filename="if_null.diff"
Content-Transfer-Encoding: 7bit

Index: sys/conf/files
===================================================================
RCS file: /cvsroot/syssrc/sys/conf/files,v
retrieving revision 1.546
diff -c -r1.546 files
*** sys/conf/files	11 Aug 2002 17:00:04 -0000	1.546
--- sys/conf/files	21 Aug 2002 10:29:59 -0000
***************
*** 906,911 ****
--- 906,912 ----
  defpseudo rnd
  
  defpseudo loop:		ifnet
+ defpseudo null:		ifnet
  defpseudo sl:		ifnet
  defpseudo ppp:		ifnet, bpf_filter
  defpseudo pppoe:	ifnet, ether, sppp
***************
*** 1131,1136 ****
--- 1132,1138 ----
  file	net/if_ieee1394subr.c		ieee1394
  file	net/if_ieee80211subr.c		wlan
  file	net/if_loop.c			loop			needs-count
+ file	net/if_null.c			null
  file	net/if_media.c
  file	net/if_ppp.c			ppp			needs-count
  file	net/if_stf.c			stf & inet & inet6	needs-flag
Index: sys/net/if_null.c
===================================================================
RCS file: sys/net/if_null.c
diff -N sys/net/if_null.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- sys/net/if_null.c	21 Aug 2002 10:29:59 -0000
***************
*** 0 ****
--- 1,123 ----
+ /*
+  * Copyright (c) 2002 WIDE Project.
+  * All rights reserved.
+  *
+  * Author: Keijiro Ehara <popo@wide.ad.jp>
+  *
+  * Redistribution and use in source and binary forms, with or without
+  * modification, are permitted provided that the following conditions
+  * are met:
+  * 1. Redistributions of source code must retain the above copyright
+  *    notice, this list of conditions and the following disclaimer.
+  * 2. Redistributions in binary form must reproduce the above copyright
+  *    notice, this list of conditions and the following disclaimer in the
+  *    documentation and/or other materials provided with the distribution.
+  * 3. Neither the name of the project nor the names of its contributors
+  *    may be used to endorse or promote products derived from this software
+  *    without specific prior written permission.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+  * SUCH DAMAGE.
+  */
+ 
+ /*
+  * if_null.c - pseudo-device driver for "null" interface. This interface
+  * can never forward or receive traffic. Setting link0 enables sending
+  * ICMP unreachable messages.
+  */
+ 
+ #include <sys/cdefs.h>
+ 
+ #include "bpfilter.h"
+ 
+ #include <sys/param.h>
+ #include <sys/systm.h>
+ #include <sys/kernel.h>
+ #include <sys/mbuf.h>
+ #include <sys/socket.h>
+ #include <sys/syslog.h>
+ 
+ #include <net/if.h>
+ #include <net/if_types.h>
+ 
+ #if NBPFILTER > 0
+ #include <net/bpf.h>
+ #endif
+ 
+ #define	NULLMTU	1500
+ 
+ struct	ifnet nullif;
+ 
+ void  nullattach __P((int));
+ int   nulloutput __P((struct ifnet *, struct mbuf *,
+ 	    struct sockaddr *, struct rtentry *));
+ 
+ void
+ nullattach(n)
+ 	int n;
+ {
+ 	sprintf(nullif.if_xname, "null0");
+ 	nullif.if_softc = NULL;
+ 	nullif.if_mtu = NULLMTU;
+ 	nullif.if_flags = 0;
+ 	nullif.if_ioctl = if_nullioctl;
+ 	nullif.if_output = nulloutput;
+ 	nullif.if_type = IFT_OTHER;
+ 	nullif.if_hdrlen = 0;
+ 	nullif.if_addrlen = 0;
+ 	nullif.if_dlt = DLT_NULL;
+ 	IFQ_SET_READY(&nullif.if_snd);
+ 	if_attach(&nullif);
+ 	if_alloc_sadl(&nullif);
+ #if NBPFILTER > 0
+ 	bpfattach(&nullif, DLT_NULL, sizeof(u_int));
+ #endif
+ }
+ 
+ int
+ nulloutput(ifp, m, dst, rt)
+ 	struct ifnet *ifp;
+ 	struct mbuf *m;
+ 	struct sockaddr *dst;
+ 	struct rtentry *rt;
+ {
+ #if NBPFILTER > 0
+ 	if (ifp->if_bpf) {
+ 		/*
+ 		 * We need to prepend the address family as
+ 		 * a four byte field.  Cons up a dummy header
+ 		 * to pacify bpf.  This is safe because bpf
+ 		 * will only read from the mbuf (i.e., it won't
+ 		 * try to free it or keep a pointer to it).
+ 		 */
+ 		struct mbuf m0;
+ 		u_int32_t af = dst->sa_family;
+ 
+ 		m0.m_next = m;
+ 		m0.m_len = 4;
+ 		m0.m_data = (char *)&af;
+ 
+ 		bpf_mtap(ifp->if_bpf, &m0);
+ 	}
+ #endif
+ 
+ 	ifp->if_opackets++;
+ 	ifp->if_obytes += m->m_pkthdr.len;
+ 
+ 	m_freem(m);
+ 
+ 	/* IFF_LINK0 means return unreachable */
+ 	if (ifp->if_flags & IFF_LINK0)
+ 		return (EHOSTUNREACH);
+ 		
+ 	return (0);
+ }

--Multipart_Thu_Aug_22_03:31:51_2002-1--