xref: /haiku/src/add-ons/kernel/network/ppp/ipcp/Protocol.h (revision 95bac3fda53a4cb21880712d7b43f8c21db32a2e)
1 /*
2  * Copyright 2003-2004, Waldemar Kornewald <Waldemar.Kornewald@web.de>
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 #include <Locker.h>
13 
14 #include <arpa/inet.h>
15 
16 
17 #define IPCP_PROTOCOL	0x8021
18 #define IP_PROTOCOL		0x0021
19 
20 
21 typedef struct ip_item {
22 	uint8 type;
23 	uint8 length;
24 	in_addr_t address;
25 } _PACKED ip_item;
26 
27 
28 enum ipcp_configure_item_codes {
29 	IPCP_ADDRESSES = 1,
30 	IPCP_COMPRESSION_PROTOCOL = 2,
31 	IPCP_ADDRESS = 3,
32 	IPCP_PRIMARY_DNS = 129,
33 	IPCP_SECONDARY_DNS = 131
34 };
35 
36 
37 typedef struct ipcp_requests {
38 	// let peer suggest value if ours equals 0.0.0.0
39 	in_addr_t address;
40 	in_addr_t netmask;
41 		// if equal 0.0.0.0 it will be chosen automatically
42 	in_addr_t primaryDNS;
43 	in_addr_t secondaryDNS;
44 } ipcp_requests;
45 
46 
47 // the values that were negotiated
48 typedef struct ipcp_configuration {
49 	in_addr_t address;
50 	in_addr_t primaryDNS;
51 	in_addr_t secondaryDNS;
52 } ipcp_configuration;
53 
54 
55 extern struct protosw *gProto[];
56 	// defined in ipcp.cpp
57 
58 
59 class IPCP : public KPPPProtocol {
60 	public:
61 		IPCP(KPPPInterface& interface, driver_parameter *settings);
62 		virtual ~IPCP();
63 
64 		virtual void Uninit();
65 
66 		ppp_state State() const
67 			{ return fState; }
68 
69 		virtual status_t StackControl(uint32 op, void *data);
70 
71 		virtual void ProfileChanged();
72 
73 		virtual bool Up();
74 		virtual bool Down();
75 
76 		virtual status_t Send(struct mbuf *packet,
77 			uint16 protocolNumber = IPCP_PROTOCOL);
78 		virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber);
79 		status_t ReceiveIPPacket(struct mbuf *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(struct mbuf *packet);
96 		void RCRGoodEvent(struct mbuf *packet);
97 		void RCRBadEvent(struct mbuf *nak, struct mbuf *reject);
98 		void RCAEvent(struct mbuf *packet);
99 		void RCNEvent(struct mbuf *packet);
100 		void RTREvent(struct mbuf *packet);
101 		void RTAEvent(struct mbuf *packet);
102 		void RUCEvent(struct mbuf *packet);
103 		void RXJBadEvent(struct mbuf *packet);
104 
105 		// actions
106 		void IllegalEvent(ppp_event event);
107 		void ReportUpFailedEvent();
108 		void ReportUpEvent();
109 		void ReportDownEvent();
110 		void InitializeRestartCount();
111 		void ZeroRestartCount();
112 		bool SendConfigureRequest();
113 		bool SendConfigureAck(struct mbuf *packet);
114 		bool SendConfigureNak(struct mbuf *packet);
115 		bool SendTerminateRequest();
116 		bool SendTerminateAck(struct mbuf *request = NULL);
117 		bool SendCodeReject(struct mbuf *packet);
118 
119 	private:
120 		ipcp_configuration fLocalConfiguration, fPeerConfiguration;
121 		ipcp_requests fLocalRequests, fPeerRequests;
122 
123 		// default route
124 		struct sockaddr_in fGateway;
125 		rtentry *fDefaultRoute;
126 
127 		// used for local requests
128 		bool fRequestPrimaryDNS, fRequestSecondaryDNS;
129 
130 		// for state machine
131 		ppp_state fState;
132 		vint32 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 		BLocker fLock;
142 };
143 
144 
145 #endif
146