xref: /haiku/src/add-ons/kernel/network/ppp/ipcp/Protocol.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
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 #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 bool Up();
72 		virtual bool Down();
73 
74 		virtual status_t Send(struct mbuf *packet,
75 			uint16 protocolNumber = IPCP_PROTOCOL);
76 		virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber);
77 		status_t ReceiveIPPacket(struct mbuf *packet, uint16 protocolNumber);
78 		virtual void Pulse();
79 
80 	private:
81 		bool ParseSideRequests(const driver_parameter *requests, ppp_side side);
82 		void UpdateAddresses();
83 		void RemoveRoutes();
84 
85 		// for state machine
86 		void NewState(ppp_state next);
87 		uint8 NextID();
88 			// return the next id for IPCP packets
89 
90 		// events
91 		void TOGoodEvent();
92 		void TOBadEvent();
93 		void RCREvent(struct mbuf *packet);
94 		void RCRGoodEvent(struct mbuf *packet);
95 		void RCRBadEvent(struct mbuf *nak, struct mbuf *reject);
96 		void RCAEvent(struct mbuf *packet);
97 		void RCNEvent(struct mbuf *packet);
98 		void RTREvent(struct mbuf *packet);
99 		void RTAEvent(struct mbuf *packet);
100 		void RUCEvent(struct mbuf *packet);
101 		void RXJBadEvent(struct mbuf *packet);
102 
103 		// actions
104 		void IllegalEvent(ppp_event event);
105 		void ReportUpFailedEvent();
106 		void ReportUpEvent();
107 		void ReportDownEvent();
108 		void InitializeRestartCount();
109 		void ResetRestartCount();
110 		bool SendConfigureRequest();
111 		bool SendConfigureAck(struct mbuf *packet);
112 		bool SendConfigureNak(struct mbuf *packet);
113 		bool SendTerminateRequest();
114 		bool SendTerminateAck(struct mbuf *request = NULL);
115 		bool SendCodeReject(struct mbuf *packet);
116 
117 	private:
118 		ipcp_configuration fLocalConfiguration, fPeerConfiguration;
119 		ipcp_requests fLocalRequests, fPeerRequests;
120 
121 		// default route
122 		struct sockaddr_in fGateway;
123 		rtentry *fDefaultRoute;
124 
125 		// used for local requests
126 		bool fRequestPrimaryDNS, fRequestSecondaryDNS;
127 
128 		// for state machine
129 		ppp_state fState;
130 		vint32 fID;
131 
132 		// counters and timers
133 		int32 fMaxRequest, fMaxTerminate, fMaxNak;
134 		int32 fRequestCounter, fTerminateCounter, fNakCounter;
135 		uint8 fRequestID, fTerminateID;
136 			// the ID we used for the last configure/terminate request
137 		bigtime_t fNextTimeout;
138 };
139 
140 
141 #endif
142