1/*
2 * Copyright 2003-2006, Waldemar Kornewald <wkornew@gmx.net>
3 * Distributed under the terms of the MIT License.
4 */
5
6#ifndef PROTOCOL__H
7#define PROTOCOL__H
8
9#include <driver_settings.h>
10
11#include <KPPPProtocol.h>
12
13#include <arpa/inet.h>
14#include <net_datalink.h>
15#include <net_datalink_protocol.h>
16#include <net/route.h>
17
18
19#define IPCP_PROTOCOL	0x8021
20#define IP_PROTOCOL		0x0021
21
22
23typedef struct ip_item {
24	uint8 type;
25	uint8 length;
26	in_addr_t address;
27} _PACKED ip_item;
28
29
30enum ipcp_configure_item_codes {
31	IPCP_ADDRESSES = 1,
32	IPCP_COMPRESSION_PROTOCOL = 2,
33	IPCP_ADDRESS = 3,
34	IPCP_PRIMARY_DNS = 129,
35	IPCP_SECONDARY_DNS = 131
36};
37
38
39typedef struct ipcp_requests {
40	// let peer suggest value if ours equals 0.0.0.0
41	in_addr_t address;
42	in_addr_t netmask;
43		// if equal 0.0.0.0 it will be chosen automatically
44	in_addr_t primaryDNS;
45	in_addr_t secondaryDNS;
46} ipcp_requests;
47
48
49// the values that were negotiated
50typedef struct ipcp_configuration {
51	in_addr_t address;
52	in_addr_t primaryDNS;
53	in_addr_t secondaryDNS;
54} ipcp_configuration;
55
56
57extern struct protosw *gProto[];
58	// defined in ipcp.cpp
59
60
61class IPCP : public KPPPProtocol {
62	public:
63		IPCP(KPPPInterface& interface, driver_parameter *settings);
64		virtual ~IPCP();
65
66		virtual void Uninit();
67
68		ppp_state State() const
69			{ return fState; }
70
71		virtual status_t StackControl(uint32 op, void *data);
72
73		virtual bool Up();
74		virtual bool Down();
75
76		virtual status_t Send(net_buffer *packet,
77			uint16 protocolNumber = IPCP_PROTOCOL);
78		virtual status_t Receive(net_buffer *packet, uint16 protocolNumber);
79		status_t ReceiveIPPacket(net_buffer *packet, uint16 protocolNumber);
80		virtual void Pulse();
81
82	private:
83		bool ParseSideRequests(const driver_parameter *requests, ppp_side side);
84		void UpdateAddresses();
85		void RemoveRoutes();
86
87		// for state machine
88		void NewState(ppp_state next);
89		uint8 NextID();
90			// return the next id for IPCP packets
91
92		// events
93		void TOGoodEvent();
94		void TOBadEvent();
95		void RCREvent(net_buffer *packet);
96		void RCRGoodEvent(net_buffer *packet);
97		void RCRBadEvent(net_buffer *nak, net_buffer *reject);
98		void RCAEvent(net_buffer *packet);
99		void RCNEvent(net_buffer *packet);
100		void RTREvent(net_buffer *packet);
101		void RTAEvent(net_buffer *packet);
102		void RUCEvent(net_buffer *packet);
103		void RXJBadEvent(net_buffer *packet);
104
105		// actions
106		void IllegalEvent(ppp_event event);
107		void ReportUpFailedEvent();
108		void ReportUpEvent();
109		void ReportDownEvent();
110		void InitializeRestartCount();
111		void ResetRestartCount();
112		bool SendConfigureRequest();
113		bool SendConfigureAck(net_buffer *packet);
114		bool SendConfigureNak(net_buffer *packet);
115		bool SendTerminateRequest();
116		bool SendTerminateAck(net_buffer *request = NULL);
117		bool SendCodeReject(net_buffer *packet);
118
119	private:
120		ipcp_configuration fLocalConfiguration, fPeerConfiguration;
121		ipcp_requests fLocalRequests, fPeerRequests;
122
123		// default route
124		struct sockaddr_in fGateway;
125		net_route *fDefaultRoute;
126
127		// used for local requests
128		bool fRequestPrimaryDNS, fRequestSecondaryDNS;
129
130		// for state machine
131		ppp_state fState;
132		int32 fID;
133
134		// counters and timers
135		int32 fMaxRequest, fMaxTerminate, fMaxNak;
136		int32 fRequestCounter, fTerminateCounter, fNakCounter;
137		uint8 fRequestID, fTerminateID;
138			// the ID we used for the last configure/terminate request
139		bigtime_t fNextTimeout;
140};
141
142
143#endif
144