Home | History | Annotate | Line # | Download | only in netinet
ip_var.h revision 1.75
      1  1.75  christos /*	$NetBSD: ip_var.h,v 1.75 2005/12/11 12:24:57 christos Exp $	*/
      2  1.10       cgd 
      3   1.1       cgd /*
      4   1.9   mycroft  * Copyright (c) 1982, 1986, 1993
      5   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.57       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  *
     31  1.24   thorpej  *	@(#)ip_var.h	8.2 (Berkeley) 1/9/95
     32   1.1       cgd  */
     33   1.1       cgd 
     34  1.25     perry #ifndef _NETINET_IP_VAR_H_
     35  1.25     perry #define _NETINET_IP_VAR_H_
     36  1.53    martin 
     37  1.15       cgd #include <sys/queue.h>
     38  1.30   thorpej #include <net/route.h>
     39  1.15       cgd 
     40   1.1       cgd /*
     41   1.1       cgd  * Overlay for ip header used by other protocols (tcp, udp).
     42   1.1       cgd  */
     43   1.1       cgd struct ipovly {
     44  1.18   thorpej 	u_int8_t  ih_x1[9];		/* (unused) */
     45  1.18   thorpej 	u_int8_t  ih_pr;		/* protocol */
     46  1.18   thorpej 	u_int16_t ih_len;		/* protocol length */
     47  1.18   thorpej 	struct	  in_addr ih_src;	/* source internet address */
     48  1.18   thorpej 	struct	  in_addr ih_dst;	/* destination internet address */
     49  1.40   thorpej } __attribute__((__packed__));
     50   1.1       cgd 
     51   1.1       cgd /*
     52  1.15       cgd  * Ip (reassembly or sequence) queue structures.
     53  1.15       cgd  *
     54  1.15       cgd  * XXX -- The following explains why the ipqe_m field is here, for TCP's use:
     55  1.15       cgd  * We want to avoid doing m_pullup on incoming packets but that
     56  1.15       cgd  * means avoiding dtom on the tcp reassembly code.  That in turn means
     57  1.15       cgd  * keeping an mbuf pointer in the reassembly queue (since we might
     58  1.15       cgd  * have a cluster).  As a quick hack, the source & destination
     59  1.15       cgd  * port numbers (which are no longer needed once we've located the
     60  1.15       cgd  * tcpcb) are overlayed with an mbuf pointer.
     61  1.15       cgd  */
     62  1.47      matt TAILQ_HEAD(ipqehead, ipqent);
     63  1.15       cgd struct ipqent {
     64  1.47      matt 	TAILQ_ENTRY(ipqent) ipqe_q;
     65  1.15       cgd 	union {
     66  1.15       cgd 		struct ip	*_ip;
     67  1.15       cgd 		struct tcpiphdr *_tcp;
     68  1.15       cgd 	} _ipqe_u1;
     69  1.47      matt 	struct mbuf	*ipqe_m;	/* point to first mbuf */
     70  1.47      matt 	struct mbuf	*ipre_mlast;	/* point to last mbuf */
     71  1.20   thorpej 	u_int8_t	ipqe_mff;	/* for IP fragmentation */
     72  1.28      matt 	/*
     73  1.28      matt 	 * The following are used in TCP reassembly
     74  1.28      matt 	 */
     75  1.47      matt 	TAILQ_ENTRY(ipqent) ipqe_timeq;
     76  1.28      matt 	u_int32_t ipqe_seq;
     77  1.28      matt 	u_int32_t ipqe_len;
     78  1.28      matt 	u_int32_t ipqe_flags;
     79  1.15       cgd };
     80  1.15       cgd #define	ipqe_ip		_ipqe_u1._ip
     81  1.15       cgd #define	ipqe_tcp	_ipqe_u1._tcp
     82  1.15       cgd 
     83  1.15       cgd /*
     84   1.1       cgd  * Ip reassembly queue structure.  Each fragment
     85   1.1       cgd  * being reassembled is attached to one of these structures.
     86   1.1       cgd  * They are timed out after ipq_ttl drops to 0, and may also
     87   1.1       cgd  * be reclaimed if memory becomes tight.
     88   1.1       cgd  */
     89   1.1       cgd struct ipq {
     90  1.15       cgd 	LIST_ENTRY(ipq) ipq_q;		/* to other reass headers */
     91  1.12       cgd 	u_int8_t  ipq_ttl;		/* time for reass q to live */
     92  1.12       cgd 	u_int8_t  ipq_p;		/* protocol of this fragment */
     93  1.12       cgd 	u_int16_t ipq_id;		/* sequence id for reassembly */
     94  1.15       cgd 	struct	  ipqehead ipq_fragq;	/* to ip fragment queue */
     95  1.14   mycroft 	struct	  in_addr ipq_src, ipq_dst;
     96  1.64  jonathan 	u_int16_t ipq_nfrags;		/* frags in this queue entry */
     97   1.1       cgd };
     98   1.1       cgd 
     99   1.1       cgd /*
    100   1.1       cgd  * Structure stored in mbuf in inpcb.ip_options
    101   1.1       cgd  * and passed to ip_output when ip options are in use.
    102   1.1       cgd  * The actual length of the options (including ipopt_dst)
    103   1.1       cgd  * is in m_len.
    104   1.1       cgd  */
    105   1.5   mycroft #define	MAX_IPOPTLEN	40
    106   1.1       cgd 
    107   1.1       cgd struct ipoption {
    108   1.1       cgd 	struct	in_addr ipopt_dst;	/* first-hop dst if source routed */
    109  1.12       cgd 	int8_t	ipopt_list[MAX_IPOPTLEN];	/* options proper */
    110   1.1       cgd };
    111   1.1       cgd 
    112   1.4   hpeyerl /*
    113   1.4   hpeyerl  * Structure attached to inpcb.ip_moptions and
    114   1.4   hpeyerl  * passed to ip_output when IP multicast options are in use.
    115   1.4   hpeyerl  */
    116   1.4   hpeyerl struct ip_moptions {
    117  1.12       cgd 	struct	  ifnet *imo_multicast_ifp; /* ifp for outgoing multicasts */
    118  1.44    itojun 	struct in_addr imo_multicast_addr; /* ifindex/addr on MULTICAST_IF */
    119  1.12       cgd 	u_int8_t  imo_multicast_ttl;	/* TTL for outgoing multicasts */
    120  1.12       cgd 	u_int8_t  imo_multicast_loop;	/* 1 => hear sends if a member */
    121  1.12       cgd 	u_int16_t imo_num_memberships;	/* no. memberships this socket */
    122  1.12       cgd 	struct	  in_multi *imo_membership[IP_MAX_MEMBERSHIPS];
    123   1.4   hpeyerl };
    124   1.4   hpeyerl 
    125   1.1       cgd struct	ipstat {
    126  1.39    bouyer 	u_quad_t ips_total;		/* total packets received */
    127  1.39    bouyer 	u_quad_t ips_badsum;		/* checksum bad */
    128  1.39    bouyer 	u_quad_t ips_tooshort;		/* packet too short */
    129  1.39    bouyer 	u_quad_t ips_toosmall;		/* not enough data */
    130  1.39    bouyer 	u_quad_t ips_badhlen;		/* ip header length < data size */
    131  1.39    bouyer 	u_quad_t ips_badlen;		/* ip length < ip header length */
    132  1.39    bouyer 	u_quad_t ips_fragments;		/* fragments received */
    133  1.39    bouyer 	u_quad_t ips_fragdropped;	/* frags dropped (dups, out of space) */
    134  1.39    bouyer 	u_quad_t ips_fragtimeout;	/* fragments timed out */
    135  1.39    bouyer 	u_quad_t ips_forward;		/* packets forwarded */
    136  1.39    bouyer 	u_quad_t ips_fastforward;	/* packets fast forwarded */
    137  1.39    bouyer 	u_quad_t ips_cantforward;	/* packets rcvd for unreachable dest */
    138  1.39    bouyer 	u_quad_t ips_redirectsent;	/* packets forwarded on same net */
    139  1.39    bouyer 	u_quad_t ips_noproto;		/* unknown or unsupported protocol */
    140  1.39    bouyer 	u_quad_t ips_delivered;		/* datagrams delivered to upper level*/
    141  1.39    bouyer 	u_quad_t ips_localout;		/* total ip packets generated here */
    142  1.39    bouyer 	u_quad_t ips_odropped;		/* lost packets due to nobufs, etc. */
    143  1.39    bouyer 	u_quad_t ips_reassembled;	/* total packets reassembled ok */
    144  1.50       wiz 	u_quad_t ips_fragmented;	/* datagrams successfully fragmented */
    145  1.39    bouyer 	u_quad_t ips_ofragments;	/* output fragments created */
    146  1.39    bouyer 	u_quad_t ips_cantfrag;		/* don't fragment flag was set, etc. */
    147  1.39    bouyer 	u_quad_t ips_badoptions;	/* error in option processing */
    148  1.39    bouyer 	u_quad_t ips_noroute;		/* packets discarded due to no route */
    149  1.39    bouyer 	u_quad_t ips_badvers;		/* ip version != 4 */
    150  1.39    bouyer 	u_quad_t ips_rawout;		/* total raw ip packets generated */
    151  1.39    bouyer 	u_quad_t ips_badfrags;		/* malformed fragments (bad length) */
    152  1.39    bouyer 	u_quad_t ips_rcvmemdrop;	/* frags dropped for lack of memory */
    153  1.39    bouyer 	u_quad_t ips_toolong;		/* ip length > max ip packet size */
    154  1.39    bouyer 	u_quad_t ips_nogif;		/* no match gif found */
    155  1.45    itojun 	u_quad_t ips_badaddr;		/* invalid address on header */
    156   1.1       cgd };
    157   1.1       cgd 
    158  1.29      matt #define	IPFLOW_HASHBITS			6 /* should not be a multiple of 8 */
    159  1.29      matt struct ipflow {
    160  1.34   thorpej 	LIST_ENTRY(ipflow) ipf_list;	/* next in active list */
    161  1.34   thorpej 	LIST_ENTRY(ipflow) ipf_hash;	/* next ipflow in bucket */
    162  1.29      matt 	struct in_addr ipf_dst;		/* destination address */
    163  1.29      matt 	struct in_addr ipf_src;		/* source address */
    164  1.29      matt 	u_int8_t ipf_tos;		/* type-of-service */
    165  1.29      matt 	struct route ipf_ro;		/* associated route entry */
    166  1.29      matt 	u_long ipf_uses;		/* number of uses in this period */
    167  1.29      matt 	u_long ipf_last_uses;		/* number of uses in last period */
    168  1.29      matt 	u_long ipf_dropped;		/* ENOBUFS returned by if_output */
    169  1.29      matt 	u_long ipf_errors;		/* other errors returned by if_output */
    170  1.34   thorpej 	u_int ipf_timer;		/* lifetime timer */
    171  1.29      matt 	time_t ipf_start;		/* creation time */
    172  1.29      matt };
    173  1.29      matt 
    174  1.11       jtc #ifdef _KERNEL
    175  1.54    martin 
    176  1.54    martin #ifdef _KERNEL_OPT
    177  1.54    martin #include "opt_gateway.h"
    178  1.54    martin #include "opt_mbuftrace.h"
    179  1.54    martin #endif
    180  1.54    martin 
    181   1.1       cgd /* flags passed to ip_output as last parameter */
    182   1.1       cgd #define	IP_FORWARDING		0x1		/* most of ip header exists */
    183   1.9   mycroft #define	IP_RAWOUTPUT		0x2		/* raw ip header exists */
    184  1.21   thorpej #define	IP_RETURNMTU		0x4		/* pass back mtu on EMSGSIZE */
    185   1.1       cgd #define	IP_ROUTETOIF		SO_DONTROUTE	/* bypass routing tables */
    186   1.1       cgd #define	IP_ALLOWBROADCAST	SO_BROADCAST	/* can send broadcast packets */
    187  1.43   thorpej #define	IP_MTUDISC		0x0400		/* Path MTU Discovery; set DF */
    188  1.48   thorpej 
    189  1.48   thorpej #ifdef __NO_STRICT_ALIGNMENT
    190  1.48   thorpej #define	IP_HDR_ALIGNED_P(ip)	1
    191  1.48   thorpej #else
    192  1.48   thorpej #define	IP_HDR_ALIGNED_P(ip)	((((vaddr_t) (ip)) & 3) == 0)
    193  1.48   thorpej #endif
    194   1.1       cgd 
    195  1.68      matt extern struct domain inetdomain;
    196  1.68      matt 
    197  1.29      matt extern struct ipstat ipstat;		/* ip statistics */
    198  1.63  jonathan extern LIST_HEAD(ipqhead, ipq) ipq[];	/* ip reass. queue */
    199  1.29      matt extern int   ip_defttl;			/* default IP ttl */
    200  1.29      matt extern int   ipforwarding;		/* ip forwarding */
    201  1.27       kml extern int   ip_mtudisc;		/* mtu discovery */
    202  1.49    itojun extern int   ip_mtudisc_timeout;	/* seconds to timeout mtu discovery */
    203  1.27       kml extern int   anonportmin;		/* minimum ephemeral port */
    204  1.27       kml extern int   anonportmax;		/* maximum ephemeral port */
    205  1.42      tron extern int   lowportmin;		/* minimum reserved port */
    206  1.42      tron extern int   lowportmax;		/* maximum reserved port */
    207  1.69   thorpej extern int   ip_do_loopback_cksum;	/* do IP checksum on loopback? */
    208  1.27       kml extern struct rttimer_queue *ip_mtudisc_timeout_q;
    209  1.51      matt #ifdef MBUFTRACE
    210  1.51      matt extern struct mowner ip_rx_mowner;
    211  1.51      matt extern struct mowner ip_tx_mowner;
    212  1.51      matt #endif
    213  1.31      matt #ifdef GATEWAY
    214  1.31      matt extern int ip_maxflows;
    215  1.31      matt #endif
    216  1.52      matt extern struct pool inmulti_pool;
    217  1.35   thorpej extern struct pool ipqent_pool;
    218  1.19   thorpej struct	 inpcb;
    219  1.19   thorpej 
    220  1.67    itojun int	 ip_ctloutput(int, struct socket *, int, int, struct mbuf **);
    221  1.67    itojun int	 ip_dooptions(struct mbuf *);
    222  1.67    itojun void	 ip_drain(void);
    223  1.67    itojun void	 ip_forward(struct mbuf *, int);
    224  1.67    itojun void	 ip_freef(struct ipq *);
    225  1.67    itojun void	 ip_freemoptions(struct ip_moptions *);
    226  1.67    itojun int	 ip_getmoptions(int, struct ip_moptions *, struct mbuf **);
    227  1.67    itojun void	 ip_init(void);
    228  1.67    itojun int	 ip_optcopy(struct ip *, struct ip *);
    229  1.67    itojun u_int	 ip_optlen(struct inpcb *);
    230  1.67    itojun int	 ip_output(struct mbuf *, ...);
    231  1.67    itojun int	 ip_fragment(struct mbuf *, struct ifnet *, u_long);
    232  1.67    itojun int	 ip_pcbopts(struct mbuf **, struct mbuf *);
    233  1.20   thorpej struct mbuf *
    234  1.67    itojun 	 ip_reass(struct ipqent *, struct ipq *, struct ipqhead *);
    235   1.6   mycroft struct in_ifaddr *
    236  1.67    itojun 	 ip_rtaddr(struct in_addr);
    237  1.67    itojun void	 ip_savecontrol(struct inpcb *, struct mbuf **, struct ip *,
    238  1.66      matt 	   struct mbuf *);
    239  1.67    itojun int	 ip_setmoptions(int, struct ip_moptions **, struct mbuf *);
    240  1.67    itojun void	 ip_slowtimo(void);
    241   1.6   mycroft struct mbuf *
    242  1.67    itojun 	 ip_srcroute(void);
    243  1.67    itojun void	 ip_stripoptions(struct mbuf *, struct mbuf *);
    244  1.67    itojun int	 ip_sysctl(int *, u_int, void *, size_t *, void *, size_t);
    245  1.67    itojun void	 ipintr(void);
    246  1.67    itojun void *	 rip_ctlinput(int, struct sockaddr *, void *);
    247  1.67    itojun int	 rip_ctloutput(int, struct socket *, int, int, struct mbuf **);
    248  1.67    itojun void	 rip_init(void);
    249  1.67    itojun void	 rip_input(struct mbuf *, ...);
    250  1.67    itojun int	 rip_output(struct mbuf *, ...);
    251  1.67    itojun int	 rip_usrreq(struct socket *,
    252  1.75  christos 	    int, struct mbuf *, struct mbuf *, struct mbuf *, struct lwp *);
    253  1.67    itojun void	ipflow_init(void);
    254  1.67    itojun struct	ipflow *ipflow_reap(int);
    255  1.67    itojun void	ipflow_create(const struct route *, struct mbuf *);
    256  1.67    itojun void	ipflow_slowtimo(void);
    257  1.67    itojun void	ipflow_invalidate_all(void);
    258  1.59    itojun 
    259  1.60  jonathan extern uint16_t	ip_id;
    260  1.67    itojun static __inline uint16_t ip_newid(void);
    261  1.60  jonathan 
    262  1.67    itojun u_int16_t ip_randomid(void);
    263  1.60  jonathan extern int ip_do_randomid;
    264  1.60  jonathan 
    265  1.70      yamt /*
    266  1.70      yamt  * ip_newid_range: "allocate" num contiguous ip_ids.
    267  1.70      yamt  *
    268  1.70      yamt  * => return the first id.
    269  1.70      yamt  */
    270  1.70      yamt 
    271  1.70      yamt static __inline uint16_t
    272  1.70      yamt ip_newid_range(unsigned int num)
    273  1.70      yamt {
    274  1.70      yamt 	uint16_t id;
    275  1.70      yamt 
    276  1.70      yamt 	if (ip_do_randomid) {
    277  1.70      yamt 		/* XXX ignore num */
    278  1.70      yamt 		return ip_randomid();
    279  1.70      yamt 	}
    280  1.70      yamt 
    281  1.70      yamt 	id = htons(ip_id);
    282  1.70      yamt 	ip_id += num;
    283  1.70      yamt 
    284  1.70      yamt 	return id;
    285  1.70      yamt }
    286  1.70      yamt 
    287  1.60  jonathan static __inline uint16_t
    288  1.60  jonathan ip_newid(void)
    289  1.60  jonathan {
    290  1.60  jonathan 
    291  1.70      yamt 	return ip_newid_range(1);
    292  1.60  jonathan }
    293  1.60  jonathan 
    294  1.60  jonathan #endif  /* _KERNEL */
    295  1.25     perry 
    296  1.74      elad #endif /* !_NETINET_IP_VAR_H_ */
    297