[9] | 1 | /* dhcpd.h - Dynamic Host Configuration Protocol daemon.
|
---|
| 2 | * Author: Kees J. Bot
|
---|
| 3 | * 16 Dec 2000
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | #define nil ((void*)0)
|
---|
| 7 |
|
---|
| 8 | #include <minix/paths.h>
|
---|
| 9 |
|
---|
| 10 | /* Paths to files. */
|
---|
| 11 | #define PATH_DHCPCONF _PATH_DHCPCONF
|
---|
| 12 | #define PATH_DHCPPID _PATH_DHCPPID
|
---|
| 13 | #define PATH_DHCPCACHE _PATH_DHCPCACHE
|
---|
| 14 | #define PATH_DHCPPOOL _PATH_DHCPPOOL
|
---|
| 15 |
|
---|
| 16 | #define CLID_MAX 32 /* Maximum client ID length. */
|
---|
| 17 |
|
---|
| 18 | #ifndef EXTERN
|
---|
| 19 | #define EXTERN extern
|
---|
| 20 | #endif
|
---|
| 21 |
|
---|
| 22 | EXTERN char *program; /* This program's name. */
|
---|
| 23 | extern char *configfile; /* Configuration file. */
|
---|
| 24 | extern char *poolfile; /* Dynamic address pool. */
|
---|
| 25 | EXTERN int serving; /* True if being a DHCP server. */
|
---|
| 26 | EXTERN unsigned test; /* Test level. */
|
---|
| 27 | EXTERN unsigned debug; /* Debug level. */
|
---|
| 28 | EXTERN asynchio_t asyn; /* Bookkeeping for all async I/O. */
|
---|
| 29 |
|
---|
| 30 | /* BOOTP UDP ports: (That they are different is quite stupid.) */
|
---|
| 31 | EXTERN u16_t port_server; /* Port server listens on. */
|
---|
| 32 | EXTERN u16_t port_client; /* Port client listens on. */
|
---|
| 33 |
|
---|
| 34 | #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
|
---|
| 35 | #define arraylimit(a) ((a) + arraysize(a))
|
---|
| 36 | #define between(a,c,z) (sizeof(c) <= sizeof(unsigned) ? \
|
---|
| 37 | (unsigned) (c) - (a) <= (unsigned) (z) - (a) : \
|
---|
| 38 | (unsigned long) (c) - (a) <= (unsigned long) (z) - (a))
|
---|
| 39 |
|
---|
| 40 | /* To treat objects as octet arrays: */
|
---|
| 41 | #define B(a) ((u8_t *) (a))
|
---|
| 42 |
|
---|
| 43 | /* Times. */
|
---|
| 44 | EXTERN time_t start, now; /* Start and current time. */
|
---|
| 45 | EXTERN time_t event; /* Time of the next timed event. */
|
---|
| 46 |
|
---|
| 47 | /* Special times and periods: */
|
---|
| 48 | #define NEVER (sizeof(time_t) <= sizeof(int) ? INT_MAX : LONG_MAX)
|
---|
| 49 | #define DELTA_FIRST 4 /* Between first and second query. */
|
---|
| 50 | #define DELTA_FAST 64 /* Unbound queries this often. */
|
---|
| 51 | #define DELTA_SLOW 512 /* Bound queries are more relaxed. */
|
---|
| 52 | #define N_SOLICITS 3 /* Number of solicitations. */
|
---|
| 53 | #define DELTA_SOL 3 /* Time between solicitations. */
|
---|
| 54 | #define DELTA_ADV 2048 /* Router adverts to self lifetime. */
|
---|
| 55 |
|
---|
| 56 | /* Buffers for packets. */
|
---|
| 57 | typedef struct buf {
|
---|
| 58 | eth_hdr_t *eth; /* Ethernet header in payload. */
|
---|
| 59 | ip_hdr_t *ip; /* IP header in payload. */
|
---|
| 60 | udp_hdr_t *udp; /* UDP header in payload. */
|
---|
| 61 | udp_io_hdr_t *udpio; /* UDP I/O header in payload. */
|
---|
| 62 | dhcp_t *dhcp; /* DHCP data in payload. */
|
---|
| 63 | u8_t pad[2]; /* buf[] must start at 2 mod 4. */
|
---|
| 64 | /* Payload: */
|
---|
| 65 | u8_t buf[ETH_MAX_PACK_SIZE];
|
---|
| 66 | } buf_t;
|
---|
| 67 |
|
---|
| 68 | #define BUF_ETH_SIZE (ETH_MAX_PACK_SIZE)
|
---|
| 69 | #define BUF_IP_SIZE (BUF_ETH_SIZE - sizeof(eth_hdr_t))
|
---|
| 70 | #define BUF_UDP_SIZE (BUF_IP_SIZE - sizeof(ip_hdr_t) - sizeof(udp_hdr_t) \
|
---|
| 71 | + sizeof(udp_io_hdr_t))
|
---|
| 72 |
|
---|
| 73 | /* Type of network device open: Ethernet, ICMP, BOOTP client, BOOTP server. */
|
---|
| 74 | typedef enum { FT_CLOSED, FT_ETHERNET, FT_ICMP, FT_BOOTPC, FT_BOOTPS } fdtype_t;
|
---|
| 75 |
|
---|
| 76 | #define FT_ALL FT_CLOSED /* To close all open descriptors at once. */
|
---|
| 77 |
|
---|
| 78 | typedef struct fd { /* An open descriptor. */
|
---|
| 79 | i8_t fd; /* Open descriptor. */
|
---|
| 80 | u8_t fdtype; /* Type of network open. */
|
---|
| 81 | char device[sizeof("/dev/eth###")]; /* Device name. */
|
---|
| 82 | u8_t n; /* Network that owns it. */
|
---|
| 83 | buf_t *bp; /* Associated packet buffer. */
|
---|
| 84 | time_t since; /* Open since when? */
|
---|
| 85 | } fd_t;
|
---|
| 86 |
|
---|
| 87 | /* Network state: Any IP device, Ethernet in sink mode, True Ethernet. */
|
---|
| 88 | typedef enum { NT_IP, NT_SINK, NT_ETHERNET } nettype_t;
|
---|
| 89 |
|
---|
| 90 | typedef struct network { /* Information on a network. */
|
---|
| 91 | u8_t n; /* Network number. */
|
---|
| 92 | ether_addr_t eth; /* Ethernet address of this net. */
|
---|
| 93 | u8_t type; /* What kind of net is this? */
|
---|
| 94 | i8_t sol_ct; /* Router solicitation count. */
|
---|
| 95 | ether_addr_t conflict; /* Address conflict with this one. */
|
---|
| 96 | unsigned flags; /* Various flags. */
|
---|
| 97 | fd_t *fdp; /* Current open device. */
|
---|
| 98 | struct network *wait; /* Wait for a resource list. */
|
---|
| 99 | ipaddr_t ip; /* IP address of this net. */
|
---|
| 100 | ipaddr_t mask; /* Associated netmask. */
|
---|
| 101 | ipaddr_t gateway; /* My router. */
|
---|
| 102 | ipaddr_t server; /* My DHCP server. */
|
---|
| 103 | const char *hostname; /* Optional hostname to query for. */
|
---|
| 104 | time_t start; /* Query or lease start time. */
|
---|
| 105 | time_t delta; /* Query again after delta seconds. */
|
---|
| 106 | time_t renew; /* Next query or go into renewal. */
|
---|
| 107 | time_t rebind; /* When to go into rebind. */
|
---|
| 108 | time_t lease; /* When our lease expires. */
|
---|
| 109 | time_t solicit; /* Time to do a router solicitation. */
|
---|
| 110 | } network_t;
|
---|
| 111 |
|
---|
| 112 | /* Flags. */
|
---|
| 113 | #define NF_NEGOTIATING 0x001 /* Negotiating with a DHCP server. */
|
---|
| 114 | #define NF_BOUND 0x002 /* Address configured through DHCP. */
|
---|
| 115 | #define NF_SERVING 0x004 /* I'm a server on this network. */
|
---|
| 116 | #define NF_RELAYING 0x008 /* I'm relaying for this network. */
|
---|
| 117 | #define NF_WAIT 0x010 /* Wait for a resource to free up. */
|
---|
| 118 | #define NF_IRDP 0x020 /* IRDP is used on this net. */
|
---|
| 119 | #define NF_CONFLICT 0x040 /* There is an address conflict. */
|
---|
| 120 | #define NF_POSSESSIVE 0x080 /* Keep address if lease expires. */
|
---|
| 121 | #define NF_INFORM 0x100 /* It's ok to answer DHCPINFORM. */
|
---|
| 122 |
|
---|
| 123 | /* Functions defined in dhcpd.c. */
|
---|
| 124 | void report(const char *label);
|
---|
| 125 | void fatal(const char *label);
|
---|
| 126 | void *allocate(size_t size);
|
---|
| 127 | int ifname2if(const char *name);
|
---|
| 128 | network_t *if2net(int n);
|
---|
| 129 |
|
---|
| 130 | /* Devices.c */
|
---|
| 131 | void get_buf(buf_t **bp);
|
---|
| 132 | void put_buf(buf_t **bp);
|
---|
| 133 | void give_buf(buf_t **dbp, buf_t **sbp);
|
---|
| 134 | network_t *newnetwork(void);
|
---|
| 135 | void closefd(fd_t *fdp);
|
---|
| 136 | int opendev(network_t *np, fdtype_t fdtype, int compete);
|
---|
| 137 | void closedev(network_t *np, fdtype_t fdtype);
|
---|
| 138 | char *ipdev(int n);
|
---|
| 139 | void set_ipconf(char *device, ipaddr_t ip, ipaddr_t mask, unsigned mtu);
|
---|
| 140 |
|
---|
| 141 | /* Ether.c */
|
---|
| 142 | void udp2ether(buf_t *bp, network_t *np);
|
---|
| 143 | int ether2udp(buf_t *bp);
|
---|
| 144 | void make_arp(buf_t *bp, network_t *np);
|
---|
| 145 | int is_arp_me(buf_t *bp, network_t *np);
|
---|
| 146 | void icmp_solicit(buf_t *bp);
|
---|
| 147 | void icmp_advert(buf_t *bp, network_t *np);
|
---|
| 148 | ipaddr_t icmp_is_advert(buf_t *bp);
|
---|
| 149 |
|
---|
| 150 | /* Tags.c */
|
---|
| 151 | #define gettag(dp, st, pd, pl) dhcp_gettag((dp), (st), (pd), (pl))
|
---|
| 152 | void settag(dhcp_t *dp, int tag, void *data, size_t len);
|
---|
| 153 | char *cidr_ntoa(ipaddr_t addr, ipaddr_t mask);
|
---|
| 154 | void ether2clid(u8_t *clid, ether_addr_t *eth);
|
---|
| 155 | void initdhcpconf(void);
|
---|
| 156 | int makedhcp(dhcp_t *dp, u8_t *class, size_t calen, u8_t *client, size_t cilen,
|
---|
| 157 | ipaddr_t ip, ipaddr_t ifip, network_t *np);
|
---|
| 158 | char *dhcptypename(int type);
|
---|
| 159 | void printdhcp(dhcp_t *dp);
|
---|