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