12632SN/A/*
22632SN/A * ip.h
32632SN/A *
42632SN/A * Internet Protocol (RFC 791).
52632SN/A *
62632SN/A * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
72632SN/A *
82632SN/A * $Id: ip.h,v 1.23 2003/03/16 17:39:17 dugsong Exp $
92632SN/A */
102632SN/A
112632SN/A#ifndef DNET_IP_H
122632SN/A#define DNET_IP_H
132632SN/A
142632SN/A#define IP_ADDR_LEN	4		/* IP address length */
152632SN/A#define IP_ADDR_BITS	32		/* IP address bits */
162632SN/A
172632SN/A#define IP_HDR_LEN	20		/* base IP header length */
182632SN/A#define IP_OPT_LEN	2		/* base IP option length */
192632SN/A#define IP_OPT_LEN_MAX	40
202632SN/A#define IP_HDR_LEN_MAX	(IP_HDR_LEN + IP_OPT_LEN_MAX)
212632SN/A
222632SN/A#define IP_LEN_MAX	65535
232632SN/A#define IP_LEN_MIN	IP_HDR_LEN
242632SN/A
252632SN/Atypedef uint32_t	ip_addr_t;
262632SN/A
272632SN/A#ifndef __GNUC__
282632SN/A# define __attribute__(x)
292632SN/A# pragma pack(1)
302632SN/A#endif
312632SN/A
322632SN/A/*
332632SN/A * IP header, without options
342632SN/A */
352632SN/Astruct ip_hdr {
362632SN/A#if DNET_BYTESEX == DNET_BIG_ENDIAN
372632SN/A        uint8_t		ip_v:4,		/* version */
382632SN/A                        ip_hl:4;	/* header length (incl any options) */
392632SN/A#elif DNET_BYTESEX == DNET_LIL_ENDIAN
402632SN/A        uint8_t		ip_hl:4,
412632SN/A                        ip_v:4;
422632SN/A#else
432632SN/A# error "need to include <dnet.h>"
442632SN/A#endif
452632SN/A        uint8_t		ip_tos;		/* type of service */
462632SN/A        uint16_t	ip_len;		/* total length (incl header) */
472632SN/A        uint16_t	ip_id;		/* identification */
482632SN/A        uint16_t	ip_off;		/* fragment offset and flags */
492632SN/A        uint8_t		ip_ttl;		/* time to live */
502632SN/A        uint8_t		ip_p;		/* protocol */
512632SN/A        uint16_t	ip_sum;		/* checksum */
522632SN/A        ip_addr_t	ip_src;		/* source address */
532632SN/A        ip_addr_t	ip_dst;		/* destination address */
542632SN/A};
552632SN/A
562632SN/A/*
572632SN/A * Type of service (ip_tos), RFC 1349 ("obsoleted by RFC 2474")
582632SN/A */
592632SN/A#define IP_TOS_DEFAULT		0x00	/* default */
602632SN/A#define IP_TOS_LOWDELAY		0x10	/* low delay */
612632SN/A#define IP_TOS_THROUGHPUT	0x08	/* high throughput */
622632SN/A#define IP_TOS_RELIABILITY	0x04	/* high reliability */
632632SN/A#define IP_TOS_LOWCOST		0x02	/* low monetary cost - XXX */
642632SN/A#define IP_TOS_ECT		0x02	/* ECN-capable transport */
652632SN/A#define IP_TOS_CE		0x01	/* congestion experienced */
662632SN/A
672632SN/A/*
682632SN/A * IP precedence (high 3 bits of ip_tos), hopefully unused
692632SN/A */
702632SN/A#define IP_TOS_PREC_ROUTINE		0x00
712632SN/A#define IP_TOS_PREC_PRIORITY		0x20
722632SN/A#define IP_TOS_PREC_IMMEDIATE		0x40
732632SN/A#define IP_TOS_PREC_FLASH		0x60
742632SN/A#define IP_TOS_PREC_FLASHOVERRIDE	0x80
752632SN/A#define IP_TOS_PREC_CRITIC_ECP		0xa0
762632SN/A#define IP_TOS_PREC_INTERNETCONTROL	0xc0
772632SN/A#define IP_TOS_PREC_NETCONTROL		0xe0
782632SN/A
792632SN/A/*
802632SN/A * Fragmentation flags (ip_off)
812632SN/A */
822632SN/A#define IP_RF		0x8000		/* reserved */
832632SN/A#define IP_DF		0x4000		/* don't fragment */
842632SN/A#define IP_MF		0x2000		/* more fragments (not last frag) */
852632SN/A#define IP_OFFMASK	0x1fff		/* mask for fragment offset */
862632SN/A
872632SN/A/*
882632SN/A * Time-to-live (ip_ttl), seconds
892632SN/A */
902632SN/A#define IP_TTL_DEFAULT	64		/* default ttl, RFC 1122, RFC 1340 */
912632SN/A#define IP_TTL_MAX	255		/* maximum ttl */
922632SN/A
932632SN/A/*
942632SN/A * Protocol (ip_p) - http://www.iana.org/assignments/protocol-numbers
952632SN/A */
962632SN/A#define	IP_PROTO_IP		0		/* dummy for IP */
972632SN/A#define IP_PROTO_HOPOPTS	IP_PROTO_IP	/* IPv6 hop-by-hop options */
982632SN/A#define	IP_PROTO_ICMP		1		/* ICMP */
992632SN/A#define	IP_PROTO_IGMP		2		/* IGMP */
1002632SN/A#define IP_PROTO_GGP		3		/* gateway-gateway protocol */
1012632SN/A#define	IP_PROTO_IPIP		4		/* IP in IP */
1022632SN/A#define IP_PROTO_ST		5		/* ST datagram mode */
1032632SN/A#define	IP_PROTO_TCP		6		/* TCP */
1042632SN/A#define IP_PROTO_CBT		7		/* CBT */
1052632SN/A#define	IP_PROTO_EGP		8		/* exterior gateway protocol */
1062632SN/A#define IP_PROTO_IGP		9		/* interior gateway protocol */
1072632SN/A#define IP_PROTO_BBNRCC		10		/* BBN RCC monitoring */
1082632SN/A#define IP_PROTO_NVP		11		/* Network Voice Protocol */
1092632SN/A#define	IP_PROTO_PUP		12		/* PARC universal packet */
1102632SN/A#define IP_PROTO_ARGUS		13		/* ARGUS */
1112632SN/A#define IP_PROTO_EMCON		14		/* EMCON */
1122632SN/A#define IP_PROTO_XNET		15		/* Cross Net Debugger */
1132632SN/A#define IP_PROTO_CHAOS		16		/* Chaos */
1142632SN/A#define	IP_PROTO_UDP		17		/* UDP */
1152632SN/A#define IP_PROTO_MUX		18		/* multiplexing */
1162632SN/A#define IP_PROTO_DCNMEAS	19		/* DCN measurement */
1172632SN/A#define IP_PROTO_HMP		20		/* Host Monitoring Protocol */
1182632SN/A#define IP_PROTO_PRM		21		/* Packet Radio Measurement */
1192632SN/A#define	IP_PROTO_IDP		22		/* Xerox NS IDP */
1202632SN/A#define IP_PROTO_TRUNK1		23		/* Trunk-1 */
1212632SN/A#define IP_PROTO_TRUNK2		24		/* Trunk-2 */
1222632SN/A#define IP_PROTO_LEAF1		25		/* Leaf-1 */
1232632SN/A#define IP_PROTO_LEAF2		26		/* Leaf-2 */
1242632SN/A#define IP_PROTO_RDP		27		/* "Reliable Datagram" proto */
1252632SN/A#define IP_PROTO_IRTP		28		/* Inet Reliable Transaction */
1262632SN/A#define	IP_PROTO_TP		29 		/* ISO TP class 4 */
1272632SN/A#define IP_PROTO_NETBLT		30		/* Bulk Data Transfer */
1282632SN/A#define IP_PROTO_MFPNSP		31		/* MFE Network Services */
1292632SN/A#define IP_PROTO_MERITINP	32		/* Merit Internodal Protocol */
1302632SN/A#define IP_PROTO_SEP		33		/* Sequential Exchange proto */
1312632SN/A#define IP_PROTO_3PC		34		/* Third Party Connect proto */
1322632SN/A#define IP_PROTO_IDPR		35		/* Interdomain Policy Route */
1332632SN/A#define IP_PROTO_XTP		36		/* Xpress Transfer Protocol */
1342632SN/A#define IP_PROTO_DDP		37		/* Datagram Delivery Proto */
1352632SN/A#define IP_PROTO_CMTP		38		/* IDPR Ctrl Message Trans */
1362632SN/A#define IP_PROTO_TPPP		39		/* TP++ Transport Protocol */
1372632SN/A#define IP_PROTO_IL		40		/* IL Transport Protocol */
1382632SN/A#define IP_PROTO_IPV6		41		/* IPv6 */
1392632SN/A#define IP_PROTO_SDRP		42		/* Source Demand Routing */
1402632SN/A#define IP_PROTO_ROUTING	43		/* IPv6 routing header */
1412632SN/A#define IP_PROTO_FRAGMENT	44		/* IPv6 fragmentation header */
1422632SN/A#define IP_PROTO_RSVP		46		/* Reservation protocol */
1432632SN/A#define	IP_PROTO_GRE		47		/* General Routing Encap */
1442632SN/A#define IP_PROTO_MHRP		48		/* Mobile Host Routing */
1452632SN/A#define IP_PROTO_ENA		49		/* ENA */
1462632SN/A#define	IP_PROTO_ESP		50		/* Encap Security Payload */
1472632SN/A#define	IP_PROTO_AH		51		/* Authentication Header */
1482632SN/A#define IP_PROTO_INLSP		52		/* Integated Net Layer Sec */
1492632SN/A#define IP_PROTO_SWIPE		53		/* SWIPE */
1502632SN/A#define IP_PROTO_NARP		54		/* NBMA Address Resolution */
1512632SN/A#define	IP_PROTO_MOBILE		55		/* Mobile IP, RFC 2004 */
1522632SN/A#define IP_PROTO_TLSP		56		/* Transport Layer Security */
1532632SN/A#define IP_PROTO_SKIP		57		/* SKIP */
1542632SN/A#define IP_PROTO_ICMPV6		58		/* ICMP for IPv6 */
1552632SN/A#define IP_PROTO_NONE		59		/* IPv6 no next header */
1562632SN/A#define IP_PROTO_DSTOPTS	60		/* IPv6 destination options */
1572632SN/A#define IP_PROTO_ANYHOST	61		/* any host internal proto */
1582632SN/A#define IP_PROTO_CFTP		62		/* CFTP */
1592632SN/A#define IP_PROTO_ANYNET		63		/* any local network */
1602632SN/A#define IP_PROTO_EXPAK		64		/* SATNET and Backroom EXPAK */
1612632SN/A#define IP_PROTO_KRYPTOLAN	65		/* Kryptolan */
1622632SN/A#define IP_PROTO_RVD		66		/* MIT Remote Virtual Disk */
1632632SN/A#define IP_PROTO_IPPC		67		/* Inet Pluribus Packet Core */
1642632SN/A#define IP_PROTO_DISTFS		68		/* any distributed fs */
1652632SN/A#define IP_PROTO_SATMON		69		/* SATNET Monitoring */
1662632SN/A#define IP_PROTO_VISA		70		/* VISA Protocol */
1672632SN/A#define IP_PROTO_IPCV		71		/* Inet Packet Core Utility */
1682632SN/A#define IP_PROTO_CPNX		72		/* Comp Proto Net Executive */
1692632SN/A#define IP_PROTO_CPHB		73		/* Comp Protocol Heart Beat */
1702632SN/A#define IP_PROTO_WSN		74		/* Wang Span Network */
1712632SN/A#define IP_PROTO_PVP		75		/* Packet Video Protocol */
1722632SN/A#define IP_PROTO_BRSATMON	76		/* Backroom SATNET Monitor */
1732632SN/A#define IP_PROTO_SUNND		77		/* SUN ND Protocol */
1742632SN/A#define IP_PROTO_WBMON		78		/* WIDEBAND Monitoring */
1752632SN/A#define IP_PROTO_WBEXPAK	79		/* WIDEBAND EXPAK */
1762632SN/A#define	IP_PROTO_EON		80		/* ISO CNLP */
1772632SN/A#define IP_PROTO_VMTP		81		/* Versatile Msg Transport*/
1782632SN/A#define IP_PROTO_SVMTP		82		/* Secure VMTP */
1792632SN/A#define IP_PROTO_VINES		83		/* VINES */
1802632SN/A#define IP_PROTO_TTP		84		/* TTP */
1812632SN/A#define IP_PROTO_NSFIGP		85		/* NSFNET-IGP */
1822632SN/A#define IP_PROTO_DGP		86		/* Dissimilar Gateway Proto */
1832632SN/A#define IP_PROTO_TCF		87		/* TCF */
1842632SN/A#define IP_PROTO_EIGRP		88		/* EIGRP */
1852632SN/A#define IP_PROTO_OSPF		89		/* Open Shortest Path First */
1862632SN/A#define IP_PROTO_SPRITERPC	90		/* Sprite RPC Protocol */
1872632SN/A#define IP_PROTO_LARP		91		/* Locus Address Resolution */
1882632SN/A#define IP_PROTO_MTP		92		/* Multicast Transport Proto */
1892632SN/A#define IP_PROTO_AX25		93		/* AX.25 Frames */
1902632SN/A#define IP_PROTO_IPIPENCAP	94		/* yet-another IP encap */
1912632SN/A#define IP_PROTO_MICP		95		/* Mobile Internet Ctrl */
1922632SN/A#define IP_PROTO_SCCSP		96		/* Semaphore Comm Sec Proto */
1932632SN/A#define IP_PROTO_ETHERIP	97		/* Ethernet in IPv4 */
1942632SN/A#define	IP_PROTO_ENCAP		98		/* encapsulation header */
1952632SN/A#define IP_PROTO_ANYENC		99		/* private encryption scheme */
1962632SN/A#define IP_PROTO_GMTP		100		/* GMTP */
1972632SN/A#define IP_PROTO_IFMP		101		/* Ipsilon Flow Mgmt Proto */
1982632SN/A#define IP_PROTO_PNNI		102		/* PNNI over IP */
1992632SN/A#define IP_PROTO_PIM		103		/* Protocol Indep Multicast */
2002632SN/A#define IP_PROTO_ARIS		104		/* ARIS */
2012632SN/A#define IP_PROTO_SCPS		105		/* SCPS */
2022632SN/A#define IP_PROTO_QNX		106		/* QNX */
2032632SN/A#define IP_PROTO_AN		107		/* Active Networks */
2042632SN/A#define IP_PROTO_IPCOMP		108		/* IP Payload Compression */
2052632SN/A#define IP_PROTO_SNP		109		/* Sitara Networks Protocol */
2062632SN/A#define IP_PROTO_COMPAQPEER	110		/* Compaq Peer Protocol */
2072632SN/A#define IP_PROTO_IPXIP		111		/* IPX in IP */
2082632SN/A#define IP_PROTO_VRRP		112		/* Virtual Router Redundancy */
2092632SN/A#define IP_PROTO_PGM		113		/* PGM Reliable Transport */
2102632SN/A#define IP_PROTO_ANY0HOP	114		/* 0-hop protocol */
2112632SN/A#define IP_PROTO_L2TP		115		/* Layer 2 Tunneling Proto */
2122632SN/A#define IP_PROTO_DDX		116		/* D-II Data Exchange (DDX) */
2132632SN/A#define IP_PROTO_IATP		117		/* Interactive Agent Xfer */
2142632SN/A#define IP_PROTO_STP		118		/* Schedule Transfer Proto */
2152632SN/A#define IP_PROTO_SRP		119		/* SpectraLink Radio Proto */
2162632SN/A#define IP_PROTO_UTI		120		/* UTI */
2172632SN/A#define IP_PROTO_SMP		121		/* Simple Message Protocol */
2182632SN/A#define IP_PROTO_SM		122		/* SM */
2192632SN/A#define IP_PROTO_PTP		123		/* Performance Transparency */
2202632SN/A#define IP_PROTO_ISIS		124		/* ISIS over IPv4 */
2212632SN/A#define IP_PROTO_FIRE		125		/* FIRE */
2222632SN/A#define IP_PROTO_CRTP		126		/* Combat Radio Transport */
2232632SN/A#define IP_PROTO_CRUDP		127		/* Combat Radio UDP */
2242632SN/A#define IP_PROTO_SSCOPMCE	128		/* SSCOPMCE */
2252632SN/A#define IP_PROTO_IPLT		129		/* IPLT */
2262632SN/A#define IP_PROTO_SPS		130		/* Secure Packet Shield */
2272632SN/A#define IP_PROTO_PIPE		131		/* Private IP Encap in IP */
2282632SN/A#define IP_PROTO_SCTP		132		/* Stream Ctrl Transmission */
2292632SN/A#define IP_PROTO_FC		133		/* Fibre Channel */
2302632SN/A#define IP_PROTO_RSVPIGN	134		/* RSVP-E2E-IGNORE */
2312632SN/A#define	IP_PROTO_RAW		255		/* Raw IP packets */
2322632SN/A#define IP_PROTO_RESERVED	IP_PROTO_RAW	/* Reserved */
2332632SN/A#define	IP_PROTO_MAX		255
2342632SN/A
2352632SN/A/*
2362632SN/A * Option types (opt_type) - http://www.iana.org/assignments/ip-parameters
2372632SN/A */
2382632SN/A#define IP_OPT_CONTROL		0x00		/* control */
2392632SN/A#define IP_OPT_DEBMEAS		0x40		/* debugging & measurement */
2402632SN/A#define IP_OPT_COPY		0x80		/* copy into all fragments */
2412632SN/A#define IP_OPT_RESERVED1	0x20
2422632SN/A#define IP_OPT_RESERVED2	0x60
2432632SN/A
2442632SN/A#define IP_OPT_EOL	  0			/* end of option list */
2452632SN/A#define IP_OPT_NOP	  1			/* no operation */
2462632SN/A#define IP_OPT_SEC	 (2|IP_OPT_COPY)	/* DoD basic security */
2472632SN/A#define IP_OPT_LSRR	 (3|IP_OPT_COPY)	/* loose source route */
2482632SN/A#define IP_OPT_TS	 (4|IP_OPT_DEBMEAS)	/* timestamp */
2492632SN/A#define IP_OPT_ESEC	 (5|IP_OPT_COPY)	/* DoD extended security */
2502632SN/A#define IP_OPT_CIPSO	 (6|IP_OPT_COPY)	/* commercial security */
2512632SN/A#define IP_OPT_RR	  7			/* record route */
2522632SN/A#define IP_OPT_SATID	 (8|IP_OPT_COPY)	/* stream ID (obsolete) */
2532632SN/A#define IP_OPT_SSRR	 (9|IP_OPT_COPY)	/* strict source route */
2542632SN/A#define IP_OPT_ZSU	 10			/* experimental measurement */
2552632SN/A#define IP_OPT_MTUP	 11			/* MTU probe */
2562632SN/A#define IP_OPT_MTUR	 12			/* MTU reply */
2572632SN/A#define IP_OPT_FINN	(13|IP_OPT_COPY|IP_OPT_DEBMEAS)	/* exp flow control */
2582632SN/A#define IP_OPT_VISA	(14|IP_OPT_COPY)	/* exp access control */
2592632SN/A#define IP_OPT_ENCODE	 15			/* ??? */
2602632SN/A#define IP_OPT_IMITD	(16|IP_OPT_COPY)	/* IMI traffic descriptor */
2612632SN/A#define IP_OPT_EIP	(17|IP_OPT_COPY)	/* extended IP, RFC 1385 */
2622632SN/A#define IP_OPT_TR	(18|IP_OPT_DEBMEAS)	/* traceroute */
2632632SN/A#define IP_OPT_ADDEXT	(19|IP_OPT_COPY)	/* IPv7 ext addr, RFC 1475 */
2642632SN/A#define IP_OPT_RTRALT	(20|IP_OPT_COPY)	/* router alert, RFC 2113 */
2652632SN/A#define IP_OPT_SDB	(21|IP_OPT_COPY)	/* directed bcast, RFC 1770 */
2662632SN/A#define IP_OPT_NSAPA	(22|IP_OPT_COPY)	/* NSAP addresses */
2672632SN/A#define IP_OPT_DPS	(23|IP_OPT_COPY)	/* dynamic packet state */
2682632SN/A#define IP_OPT_UMP	(24|IP_OPT_COPY)	/* upstream multicast */
2692632SN/A#define IP_OPT_MAX	 25
2702632SN/A
2712632SN/A#define IP_OPT_COPIED(o)	((o) & 0x80)
2722632SN/A#define IP_OPT_CLASS(o)		((o) & 0x60)
2732632SN/A#define IP_OPT_NUMBER(o)	((o) & 0x1f)
2742632SN/A#define IP_OPT_TYPEONLY(o)	((o) == IP_OPT_EOL || (o) == IP_OPT_NOP)
2752632SN/A
2762632SN/A/*
2772632SN/A * Security option data - RFC 791, 3.1
2782632SN/A */
2792632SN/Astruct ip_opt_data_sec {
2802632SN/A        uint16_t	s;		/* security */
2812632SN/A        uint16_t	c;		/* compartments */
2822632SN/A        uint16_t	h;		/* handling restrictions */
2832632SN/A        uint8_t		tcc[3];		/* transmission control code */
2842632SN/A} __attribute__((__packed__));
2852632SN/A
2862632SN/A#define IP_OPT_SEC_UNCLASS	0x0000	/* unclassified */
2872632SN/A#define IP_OPT_SEC_CONFID	0xf135	/* confidential */
2882632SN/A#define IP_OPT_SEC_EFTO		0x789a	/* EFTO */
2892632SN/A#define IP_OPT_SEC_MMMM		0xbc4d	/* MMMM */
2902632SN/A#define IP_OPT_SEC_PROG		0x5e26	/* PROG */
2912632SN/A#define IP_OPT_SEC_RESTR	0xaf13	/* restricted */
2922632SN/A#define IP_OPT_SEC_SECRET	0xd788	/* secret */
2932632SN/A#define IP_OPT_SEC_TOPSECRET	0x6bc5	/* top secret */
2942632SN/A
2952632SN/A/*
2962632SN/A * {Loose Source, Record, Strict Source} Route option data - RFC 791, 3.1
2972632SN/A */
2982632SN/Astruct ip_opt_data_rr {
2992632SN/A        uint8_t		ptr;		/* from start of option, >= 4 */
3002632SN/A        uint32_t	iplist __flexarr; /* list of IP addresses */
3012632SN/A} __attribute__((__packed__));
3022632SN/A
3032632SN/A/*
3042632SN/A * Timestamp option data - RFC 791, 3.1
3052632SN/A */
3062632SN/Astruct ip_opt_data_ts {
3072632SN/A        uint8_t		ptr;		/* from start of option, >= 5 */
3082632SN/A#if DNET_BYTESEX == DNET_BIG_ENDIAN
3092632SN/A        uint8_t		oflw:4,		/* number of IPs skipped */
3102632SN/A                        flg:4;		/* address[ / timestamp] flag */
3112632SN/A#elif DNET_BYTESEX == DNET_LIL_ENDIAN
3122632SN/A        uint8_t		flg:4,
3132632SN/A                        oflw:4;
3142632SN/A#endif
3152632SN/A        uint32_t	ipts __flexarr;	/* IP address [/ timestamp] pairs */
3162632SN/A} __attribute__((__packed__));
3172632SN/A
3182632SN/A#define IP_OPT_TS_TSONLY	0	/* timestamps only */
3192632SN/A#define IP_OPT_TS_TSADDR	1	/* IP address / timestamp pairs */
3202632SN/A#define IP_OPT_TS_PRESPEC	3	/* IP address / zero timestamp pairs */
3212632SN/A
3222632SN/A/*
3232632SN/A * Traceroute option data - RFC 1393, 2.2
3242632SN/A */
3252632SN/Astruct ip_opt_data_tr {
3262632SN/A        uint16_t	id;		/* ID number */
3272632SN/A        uint16_t	ohc;		/* outbound hop count */
3282632SN/A        uint16_t	rhc;		/* return hop count */
3292632SN/A        uint32_t	origip;		/* originator IP address */
3302632SN/A} __attribute__((__packed__));
3312632SN/A
3322632SN/A/*
3332632SN/A * IP option (following IP header)
3342632SN/A */
3352632SN/Astruct ip_opt {
3362632SN/A        uint8_t		opt_type;	/* option type */
3372632SN/A        uint8_t		opt_len;	/* option length >= IP_OPT_LEN */
3382632SN/A        union ip_opt_data {
3392632SN/A                struct ip_opt_data_sec	sec;	   /* IP_OPT_SEC */
3402632SN/A                struct ip_opt_data_rr	rr;	   /* IP_OPT_{L,S}RR */
3412632SN/A                struct ip_opt_data_ts	ts;	   /* IP_OPT_TS */
3422632SN/A                uint16_t		satid;	   /* IP_OPT_SATID */
3432632SN/A                uint16_t		mtu;	   /* IP_OPT_MTU{P,R} */
3442632SN/A                struct ip_opt_data_tr	tr;	   /* IP_OPT_TR */
3452632SN/A                uint32_t		addext[2]; /* IP_OPT_ADDEXT */
3462632SN/A                uint16_t		rtralt;    /* IP_OPT_RTRALT */
3472632SN/A                uint32_t		sdb[9];    /* IP_OPT_SDB */
3482632SN/A                uint8_t			data8[IP_OPT_LEN_MAX - IP_OPT_LEN];
3492632SN/A        } opt_data;
3502632SN/A} __attribute__((__packed__));
3512632SN/A
3522632SN/A#ifndef __GNUC__
3532632SN/A# pragma pack()
3542632SN/A#endif
3552632SN/A
3562632SN/A/*
3572632SN/A * Classful addressing
3582632SN/A */
3592632SN/A#define	IP_CLASSA(i)		(((uint32_t)(i) & htonl(0x80000000)) == \
3602632SN/A                                 htonl(0x00000000))
3612632SN/A#define	IP_CLASSA_NET		(htonl(0xff000000))
3622632SN/A#define	IP_CLASSA_NSHIFT	24
3632632SN/A#define	IP_CLASSA_HOST		(htonl(0x00ffffff))
3642632SN/A#define	IP_CLASSA_MAX		128
3652632SN/A
3662632SN/A#define	IP_CLASSB(i)		(((uint32_t)(i) & htonl(0xc0000000)) == \
3672632SN/A                                 htonl(0x80000000))
3682632SN/A#define	IP_CLASSB_NET		(htonl(0xffff0000))
3692632SN/A#define	IP_CLASSB_NSHIFT	16
3702632SN/A#define	IP_CLASSB_HOST		(htonl(0x0000ffff))
3712632SN/A#define	IP_CLASSB_MAX		65536
3722632SN/A
3732632SN/A#define	IP_CLASSC(i)		(((uint32_t)(i) & htonl(0xe0000000)) == \
3742632SN/A                                 htonl(0xc0000000))
3752632SN/A#define	IP_CLASSC_NET		(htonl(0xffffff00))
3762632SN/A#define	IP_CLASSC_NSHIFT	8
3772632SN/A#define	IP_CLASSC_HOST		(htonl(0x000000ff))
3782632SN/A
3792632SN/A#define	IP_CLASSD(i)		(((uint32_t)(i) & htonl(0xf0000000)) == \
3802632SN/A                                 htonl(0xe0000000))
3812632SN/A/* These ones aren't really net and host fields, but routing needn't know. */
3822632SN/A#define	IP_CLASSD_NET		(htonl(0xf0000000))
3832632SN/A#define	IP_CLASSD_NSHIFT	28
3842632SN/A#define	IP_CLASSD_HOST		(htonl(0x0fffffff))
3852632SN/A#define	IP_MULTICAST(i)		IP_CLASSD(i)
3862632SN/A
3872632SN/A#define	IP_EXPERIMENTAL(i)	(((uint32_t)(i) & htonl(0xf0000000)) == \
3882632SN/A                                 htonl(0xf0000000))
3892632SN/A#define	IP_BADCLASS(i)		(((uint32_t)(i) & htonl(0xf0000000)) == \
3902632SN/A                                 htonl(0xf0000000))
3912632SN/A#define	IP_LOCAL_GROUP(i)	(((uint32_t)(i) & htonl(0xffffff00)) == \
3922632SN/A                                 htonl(0xe0000000))
3932632SN/A/*
3942632SN/A * Reserved addresses
3952632SN/A */
3962632SN/A#define IP_ADDR_ANY		(htonl(0x00000000))	/* 0.0.0.0 */
3972632SN/A#define IP_ADDR_BROADCAST	(htonl(0xffffffff))	/* 255.255.255.255 */
3982632SN/A#define IP_ADDR_LOOPBACK	(htonl(0x7f000001))	/* 127.0.0.1 */
3992632SN/A#define IP_ADDR_MCAST_ALL	(htonl(0xe0000001))	/* 224.0.0.1 */
4002632SN/A#define IP_ADDR_MCAST_LOCAL	(htonl(0xe00000ff))	/* 224.0.0.225 */
4012632SN/A
4022632SN/A#define ip_pack_hdr(hdr, tos, len, id, off, ttl, p, src, dst) do {	\
4032632SN/A        struct ip_hdr *ip_pack_p = (struct ip_hdr *)(hdr);		\
4042632SN/A        ip_pack_p->ip_v = 4; ip_pack_p->ip_hl = 5;			\
4052632SN/A        ip_pack_p->ip_tos = tos; ip_pack_p->ip_len = htons(len);	\
4062632SN/A        ip_pack_p->ip_id = htons(id); ip_pack_p->ip_off = htons(off);	\
4072632SN/A        ip_pack_p->ip_ttl = ttl; ip_pack_p->ip_p = p;			\
4082632SN/A        ip_pack_p->ip_src = src; ip_pack_p->ip_dst = dst;		\
4092632SN/A} while (0)
4102632SN/A
4112632SN/Atypedef struct ip_handle ip_t;
4122632SN/A
4132632SN/A__BEGIN_DECLS
4142632SN/Aip_t	*ip_open(void);
4152632SN/Asize_t	 ip_send(ip_t *i, const void *buf, size_t len);
4162632SN/Aip_t	*ip_close(ip_t *i);
4172632SN/A
4182632SN/Achar	*ip_ntop(const ip_addr_t *ip, char *dst, size_t len);
4192632SN/Aint	 ip_pton(const char *src, ip_addr_t *dst);
4202632SN/Achar	*ip_ntoa(const ip_addr_t *ip);
4212632SN/A#define	 ip_aton ip_pton
4222632SN/A
4232632SN/Asize_t	 ip_add_option(void *buf, size_t len,
4242632SN/A            int proto, const void *optbuf, size_t optlen);
4252632SN/Avoid	 ip_checksum(void *buf, size_t len);
4262632SN/A
4272632SN/Ainline int
4282632SN/Aip_cksum_add(const void *buf, size_t len, int cksum)
4292632SN/A{
4302632SN/A        uint16_t *sp = (uint16_t *)buf;
43112392Sjason@lowepower.com        int sn;
4322632SN/A
4332632SN/A        sn = len / 2;
4342632SN/A
43512392Sjason@lowepower.com        do {
43612392Sjason@lowepower.com            cksum += *sp++;
43712392Sjason@lowepower.com        } while (--sn > 0);
4382632SN/A        if (len & 1)
4392632SN/A                cksum += htons(*(u_char *)sp << 8);
4402632SN/A
4412632SN/A        return (cksum);
4422632SN/A}
4432632SN/A
4442632SN/Ainline uint16_t
4452632SN/Aip_cksum_carry(int x)
4462632SN/A{
4472632SN/A        x = (x >> 16) + (x & 0xffff);
4482632SN/A        return ~(x + (x >> 16)) & 0xffff;
4492632SN/A}
4502632SN/A
4512632SN/A__END_DECLS
4522632SN/A
4532632SN/A#endif /* DNET_IP_H */
454