ppp.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867
  1. // L2TPNS PPP Stuff
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <netinet/ip6.h>
  8. #include "dhcp6.h"
  9. #include "l2tpns.h"
  10. #include "constants.h"
  11. #include "plugin.h"
  12. #include "util.h"
  13. #include "tbf.h"
  14. #include "cluster.h"
  15. #include "l2tplac.h"
  16. #include "pppoe.h"
  17. extern tunnelt *tunnel;
  18. extern bundlet *bundle;
  19. extern fragmentationt *frag;
  20. extern sessiont *session;
  21. extern radiust *radius;
  22. extern int tunfd;
  23. extern char hostname[];
  24. extern uint32_t eth_tx;
  25. extern time_t time_now;
  26. extern uint64_t time_now_ms;
  27. extern configt *config;
  28. static int add_lcp_auth(uint8_t *b, int size, int authtype);
  29. static bundleidt new_bundle(void);
  30. static int epdiscmp(epdist, epdist);
  31. static void setepdis(epdist *, epdist);
  32. static void ipcp_open(sessionidt s, tunnelidt t);
  33. static int first_session_in_bundle(sessionidt s)
  34. {
  35. bundleidt i;
  36. for (i = 1; i < MAXBUNDLE; i++)
  37. if (bundle[i].state != BUNDLEFREE)
  38. if (epdiscmp(session[s].epdis,bundle[i].epdis) && !strcmp(session[s].user, bundle[i].user))
  39. return 0;
  40. return 1;
  41. }
  42. // Process PAP messages
  43. void processpap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  44. {
  45. char user[MAXUSER];
  46. char pass[MAXPASS];
  47. uint16_t hl;
  48. uint16_t r;
  49. CSTAT(processpap);
  50. LOG_HEX(5, "PAP", p, l);
  51. if (l < 4)
  52. {
  53. LOG(1, s, t, "Short PAP %u bytes\n", l);
  54. STAT(tunnel_rx_errors);
  55. sessionshutdown(s, "Short PAP packet.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  56. return;
  57. }
  58. if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
  59. {
  60. LOG(1, s, t, "Length mismatch PAP %u/%u\n", hl, l);
  61. STAT(tunnel_rx_errors);
  62. sessionshutdown(s, "PAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  63. return;
  64. }
  65. l = hl;
  66. if (*p != 1)
  67. {
  68. LOG(1, s, t, "Unexpected PAP code %d\n", *p);
  69. STAT(tunnel_rx_errors);
  70. sessionshutdown(s, "Unexpected PAP code.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  71. return;
  72. }
  73. if (session[s].ppp.phase != Authenticate)
  74. {
  75. LOG(2, s, t, "PAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
  76. return;
  77. }
  78. {
  79. uint8_t *b = p;
  80. b += 4;
  81. user[0] = pass[0] = 0;
  82. if (*b && *b < sizeof(user))
  83. {
  84. memcpy(user, b + 1, *b);
  85. user[*b] = 0;
  86. b += 1 + *b;
  87. if (*b && *b < sizeof(pass))
  88. {
  89. memcpy(pass, b + 1, *b);
  90. pass[*b] = 0;
  91. }
  92. }
  93. LOG(3, s, t, "PAP login %s/%s\n", user, pass);
  94. }
  95. if ((!config->disable_lac_func) && lac_conf_forwardtoremotelns(s, user))
  96. {
  97. // Creating a tunnel/session has been started
  98. return;
  99. }
  100. if (session[s].ip || !(r = radiusnew(s)))
  101. {
  102. // respond now, either no RADIUS available or already authenticated
  103. uint8_t b[MAXETHER];
  104. uint8_t id = p[1];
  105. uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPPAP, 0, 0, 0);
  106. if (!p) return;
  107. if (session[s].ip)
  108. *p = 2; // ACK
  109. else
  110. *p = 3; // cant authorise
  111. p[1] = id;
  112. *(uint16_t *) (p + 2) = htons(5); // length
  113. p[4] = 0; // no message
  114. tunnelsend(b, 5 + (p - b), t); // send it
  115. if (session[s].ip)
  116. {
  117. LOG(3, s, t, "Already an IP allocated: %s (%d)\n",
  118. fmtaddr(htonl(session[s].ip), 0), session[s].ip_pool_index);
  119. }
  120. else
  121. {
  122. LOG(1, s, t, "No RADIUS session available to authenticate session...\n");
  123. sessionshutdown(s, "No free RADIUS sessions.", CDN_UNAVAILABLE, TERM_SERVICE_UNAVAILABLE);
  124. }
  125. }
  126. else
  127. {
  128. // Run PRE_AUTH plugins
  129. struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 };
  130. run_plugins(PLUGIN_PRE_AUTH, &packet);
  131. if (!packet.continue_auth)
  132. {
  133. LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
  134. if (packet.username) free(packet.username);
  135. if (packet.password) free(packet.password);
  136. return;
  137. }
  138. strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
  139. strncpy(radius[r].pass, packet.password, sizeof(radius[r].pass) - 1);
  140. free(packet.username);
  141. free(packet.password);
  142. radius[r].id = p[1];
  143. LOG(3, s, t, "Sending login for %s/%s to RADIUS\n", user, pass);
  144. if ((session[s].mrru) && (!first_session_in_bundle(s)))
  145. radiussend(r, RADIUSJUSTAUTH);
  146. else
  147. radiussend(r, RADIUSAUTH);
  148. }
  149. }
  150. // Process CHAP messages
  151. void processchap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  152. {
  153. uint16_t r;
  154. uint16_t hl;
  155. CSTAT(processchap);
  156. LOG_HEX(5, "CHAP", p, l);
  157. if (l < 4)
  158. {
  159. LOG(1, s, t, "Short CHAP %u bytes\n", l);
  160. STAT(tunnel_rx_errors);
  161. sessionshutdown(s, "Short CHAP packet.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  162. return;
  163. }
  164. if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
  165. {
  166. LOG(1, s, t, "Length mismatch CHAP %u/%u\n", hl, l);
  167. STAT(tunnel_rx_errors);
  168. sessionshutdown(s, "CHAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  169. return;
  170. }
  171. l = hl;
  172. if (*p != 2)
  173. {
  174. LOG(1, s, t, "Unexpected CHAP response code %d\n", *p);
  175. STAT(tunnel_rx_errors);
  176. sessionshutdown(s, "CHAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  177. return;
  178. }
  179. if (session[s].ppp.phase != Authenticate)
  180. {
  181. LOG(2, s, t, "CHAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
  182. return;
  183. }
  184. r = sess_local[s].radius;
  185. if (!r)
  186. {
  187. LOG(3, s, t, "Unexpected CHAP message\n");
  188. // Some modems (Netgear DM602, possibly others) persist in using CHAP even
  189. // after ACKing our ConfigReq for PAP.
  190. if (sess_local[s].lcp_authtype == AUTHPAP && config->radius_authtypes & AUTHCHAP)
  191. {
  192. sess_local[s].lcp_authtype = AUTHCHAP;
  193. sendchap(s, t);
  194. }
  195. return;
  196. }
  197. if (p[1] != radius[r].id)
  198. {
  199. LOG(1, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
  200. STAT(tunnel_rx_errors);
  201. sessionshutdown(s, "Unexpected CHAP response ID.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  202. return;
  203. }
  204. if (l < 5 || p[4] != 16)
  205. {
  206. LOG(1, s, t, "Bad CHAP response length %d\n", l < 5 ? -1 : p[4]);
  207. STAT(tunnel_rx_errors);
  208. sessionshutdown(s, "Bad CHAP response length.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  209. return;
  210. }
  211. l -= 5;
  212. p += 5;
  213. if (l < 16 || l - 16 >= sizeof(session[s].user))
  214. {
  215. LOG(1, s, t, "CHAP user too long %d\n", l - 16);
  216. STAT(tunnel_rx_errors);
  217. sessionshutdown(s, "CHAP username too long.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  218. return;
  219. }
  220. // Run PRE_AUTH plugins
  221. {
  222. struct param_pre_auth packet = { &tunnel[t], &session[s], NULL, NULL, PPPCHAP, 1 };
  223. packet.password = calloc(17, 1);
  224. memcpy(packet.password, p, 16);
  225. p += 16;
  226. l -= 16;
  227. packet.username = calloc(l + 1, 1);
  228. memcpy(packet.username, p, l);
  229. if ((!config->disable_lac_func) && lac_conf_forwardtoremotelns(s, packet.username))
  230. {
  231. free(packet.username);
  232. free(packet.password);
  233. // Creating a tunnel/session has been started
  234. return;
  235. }
  236. run_plugins(PLUGIN_PRE_AUTH, &packet);
  237. if (!packet.continue_auth)
  238. {
  239. LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
  240. if (packet.username) free(packet.username);
  241. if (packet.password) free(packet.password);
  242. return;
  243. }
  244. strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
  245. memcpy(radius[r].pass, packet.password, 16);
  246. free(packet.username);
  247. free(packet.password);
  248. }
  249. radius[r].chap = 1;
  250. LOG(3, s, t, "CHAP login %s\n", session[s].user);
  251. if ((session[s].mrru) && (!first_session_in_bundle(s)))
  252. radiussend(r, RADIUSJUSTAUTH);
  253. else
  254. radiussend(r, RADIUSAUTH);
  255. }
  256. static void dumplcp(uint8_t *p, int l)
  257. {
  258. int x = l - 4;
  259. uint8_t *o = (p + 4);
  260. LOG_HEX(5, "PPP LCP Packet", p, l);
  261. LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_code((int)*p), ntohs( ((uint16_t *) p)[1]) );
  262. LOG(4, 0, 0, "Length: %d\n", l);
  263. if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
  264. return;
  265. while (x > 2)
  266. {
  267. int type = o[0];
  268. int length = o[1];
  269. if (length < 2)
  270. {
  271. LOG(4, 0, 0, " Option length is %d...\n", length);
  272. break;
  273. }
  274. if (type == 0)
  275. {
  276. LOG(4, 0, 0, " Option type is 0...\n");
  277. x -= length;
  278. o += length;
  279. continue;
  280. }
  281. switch (type)
  282. {
  283. case 1: // Maximum-Receive-Unit
  284. if (length == 4)
  285. LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type), ntohs(*(uint16_t *)(o + 2)));
  286. else
  287. LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
  288. break;
  289. case 2: // Async-Control-Character-Map
  290. if (length == 6)
  291. {
  292. uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
  293. LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), asyncmap);
  294. }
  295. else
  296. LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
  297. break;
  298. case 3: // Authentication-Protocol
  299. if (length == 4)
  300. {
  301. int proto = ntohs(*(uint16_t *)(o + 2));
  302. LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type), proto,
  303. proto == PPPPAP ? "PAP" : "UNSUPPORTED");
  304. }
  305. else if (length == 5)
  306. {
  307. int proto = ntohs(*(uint16_t *)(o + 2));
  308. int algo = *(o + 4);
  309. LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type), proto, algo,
  310. (proto == PPPCHAP && algo == 5) ? "CHAP MD5" : "UNSUPPORTED");
  311. }
  312. else
  313. LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
  314. break;
  315. case 4: // Quality-Protocol
  316. {
  317. uint32_t qp = ntohl(*(uint32_t *)(o + 2));
  318. LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), qp);
  319. }
  320. break;
  321. case 5: // Magic-Number
  322. if (length == 6)
  323. {
  324. uint32_t magicno = ntohl(*(uint32_t *)(o + 2));
  325. LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), magicno);
  326. }
  327. else
  328. LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
  329. break;
  330. case 7: // Protocol-Field-Compression
  331. case 8: // Address-And-Control-Field-Compression
  332. LOG(4, 0, 0, " %s\n", ppp_lcp_option(type));
  333. break;
  334. default:
  335. LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type);
  336. break;
  337. }
  338. x -= length;
  339. o += length;
  340. }
  341. }
  342. void lcp_open(sessionidt s, tunnelidt t)
  343. {
  344. // transition to Authentication or Network phase:
  345. session[s].ppp.phase = sess_local[s].lcp_authtype ? Authenticate : Network;
  346. LOG(3, s, t, "LCP: Opened, phase %s\n", ppp_phase(session[s].ppp.phase));
  347. // LCP now Opened
  348. change_state(s, lcp, Opened);
  349. if (session[s].ppp.phase == Authenticate)
  350. {
  351. if (sess_local[s].lcp_authtype == AUTHCHAP)
  352. sendchap(s, t);
  353. }
  354. else
  355. {
  356. if(session[s].bundle == 0 || bundle[session[s].bundle].num_of_links == 1)
  357. {
  358. // This-Layer-Up
  359. sendipcp(s, t);
  360. change_state(s, ipcp, RequestSent);
  361. // move to passive state for IPv6 (if configured), CCP
  362. if (config->ipv6_prefix.s6_addr[0])
  363. change_state(s, ipv6cp, Stopped);
  364. else
  365. change_state(s, ipv6cp, Closed);
  366. change_state(s, ccp, Stopped);
  367. }
  368. else
  369. {
  370. sessionidt first_ses = bundle[session[s].bundle].members[0];
  371. LOG(3, s, t, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s,first_ses);
  372. ipcp_open(s, t);
  373. }
  374. }
  375. }
  376. void lcp_restart(sessionidt s)
  377. {
  378. session[s].ppp.phase = Establish;
  379. // This-Layer-Down
  380. change_state(s, ipcp, Dead);
  381. change_state(s, ipv6cp, Dead);
  382. change_state(s, ccp, Dead);
  383. }
  384. static uint8_t *ppp_conf_rej(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
  385. uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option)
  386. {
  387. if (!*response || **response != ConfigRej)
  388. {
  389. queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype, 0, 0, 0);
  390. if (!queued)
  391. return 0;
  392. *queued = ConfigRej;
  393. queued += 4;
  394. }
  395. if ((queued - buf + option[1]) > blen)
  396. {
  397. LOG(2, s, session[s].tunnel, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype, *option);
  398. return 0;
  399. }
  400. memcpy(queued, option, option[1]);
  401. return queued + option[1];
  402. }
  403. static uint8_t *ppp_conf_nak(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
  404. uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option,
  405. uint8_t *value, size_t vlen)
  406. {
  407. int *nak_sent;
  408. switch (mtype)
  409. {
  410. case PPPLCP: nak_sent = &sess_local[s].lcp.nak_sent; break;
  411. case PPPIPCP: nak_sent = &sess_local[s].ipcp.nak_sent; break;
  412. case PPPIPV6CP: nak_sent = &sess_local[s].ipv6cp.nak_sent; break;
  413. default: return 0; // ?
  414. }
  415. if (*response && **response != ConfigNak)
  416. {
  417. if (*nak_sent < config->ppp_max_failure) // reject queued
  418. return queued;
  419. return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
  420. }
  421. if (!*response)
  422. {
  423. if (*nak_sent >= config->ppp_max_failure)
  424. return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
  425. queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype, 0, 0, 0);
  426. if (!queued)
  427. return 0;
  428. (*nak_sent)++;
  429. *queued = ConfigNak;
  430. queued += 4;
  431. }
  432. if ((queued - buf + vlen + 2) > blen)
  433. {
  434. LOG(2, s, session[s].tunnel, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype, *option);
  435. return 0;
  436. }
  437. *queued++ = *option;
  438. *queued++ = vlen + 2;
  439. memcpy(queued, value, vlen);
  440. return queued + vlen;
  441. }
  442. static void ppp_code_rej(sessionidt s, tunnelidt t, uint16_t proto,
  443. char *pname, uint8_t *p, uint16_t l, uint8_t *buf, size_t size)
  444. {
  445. uint8_t *q;
  446. int mru = session[s].mru;
  447. if (mru < MINMTU) mru = MINMTU;
  448. if (mru > size) mru = size;
  449. l += 4;
  450. if (l > mru) l = mru;
  451. q = makeppp(buf, size, 0, 0, s, t, proto, 0, 0, 0);
  452. if (!q) return;
  453. *q = CodeRej;
  454. *(q + 1) = ++sess_local[s].lcp_ident;
  455. *(uint16_t *)(q + 2) = htons(l);
  456. memcpy(q + 4, p, l - 4);
  457. LOG(2, s, t, "Unexpected %s code %s\n", pname, ppp_code(*p));
  458. LOG(3, s, t, "%s: send %s\n", pname, ppp_code(*q));
  459. if (config->debug > 3) dumplcp(q, l);
  460. tunnelsend(buf, l + (q - buf), t);
  461. }
  462. // Process LCP messages
  463. void processlcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  464. {
  465. uint8_t b[MAXETHER];
  466. uint8_t *q = NULL;
  467. uint16_t hl;
  468. CSTAT(processlcp);
  469. LOG_HEX(5, "LCP", p, l);
  470. if (l < 4)
  471. {
  472. LOG(1, s, t, "Short LCP %d bytes\n", l);
  473. STAT(tunnel_rx_errors);
  474. return ;
  475. }
  476. if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
  477. {
  478. LOG(1, s, t, "Length mismatch LCP %u/%u\n", hl, l);
  479. STAT(tunnel_rx_errors);
  480. return ;
  481. }
  482. l = hl;
  483. if (session[s].die) // going down...
  484. return;
  485. LOG(((*p == EchoReq || *p == EchoReply) ? 4 : 3), s, t,
  486. "LCP: recv %s\n", ppp_code(*p));
  487. if (config->debug > 3) dumplcp(p, l);
  488. if (*p == ConfigAck)
  489. {
  490. int x = l - 4;
  491. uint8_t *o = (p + 4);
  492. int authtype = 0;
  493. while (x > 2)
  494. {
  495. int type = o[0];
  496. int length = o[1];
  497. if (length == 0 || type == 0 || x < length) break;
  498. switch (type)
  499. {
  500. case 3: // Authentication-Protocol
  501. {
  502. int proto = ntohs(*(uint16_t *)(o + 2));
  503. if (proto == PPPPAP)
  504. authtype = AUTHPAP;
  505. else if (proto == PPPCHAP && *(o + 4) == 5)
  506. authtype = AUTHCHAP;
  507. }
  508. break;
  509. }
  510. x -= length;
  511. o += length;
  512. }
  513. if (!session[s].ip && authtype)
  514. sess_local[s].lcp_authtype = authtype;
  515. switch (session[s].ppp.lcp)
  516. {
  517. case RequestSent:
  518. initialise_restart_count(s, lcp);
  519. change_state(s, lcp, AckReceived);
  520. break;
  521. case AckReceived:
  522. case Opened:
  523. LOG(2, s, t, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
  524. if (session[s].ppp.lcp == Opened)
  525. lcp_restart(s);
  526. sendlcp(s, t);
  527. change_state(s, lcp, RequestSent);
  528. break;
  529. case AckSent:
  530. lcp_open(s, t);
  531. break;
  532. default:
  533. LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
  534. }
  535. }
  536. else if (*p == ConfigReq)
  537. {
  538. int x = l - 4;
  539. uint8_t *o = (p + 4);
  540. uint8_t *response = 0;
  541. static uint8_t asyncmap[4] = { 0, 0, 0, 0 }; // all zero
  542. static uint8_t authproto[5];
  543. int changed = 0;
  544. while (x > 2)
  545. {
  546. int type = o[0];
  547. int length = o[1];
  548. if (length == 0 || type == 0 || x < length) break;
  549. switch (type)
  550. {
  551. case 1: // Maximum-Receive-Unit
  552. {
  553. uint16_t mru = ntohs(*(uint16_t *)(o + 2));
  554. if (mru >= MINMTU)
  555. {
  556. session[s].mru = mru;
  557. changed++;
  558. break;
  559. }
  560. LOG(3, s, t, " Remote requesting MRU of %u. Rejecting.\n", mru);
  561. mru = htons(MRU);
  562. q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, (uint8_t *) &mru, sizeof(mru));
  563. }
  564. break;
  565. case 2: // Async-Control-Character-Map
  566. if (!ntohl(*(uint32_t *)(o + 2))) // all bits zero is OK
  567. break;
  568. LOG(3, s, t, " Remote requesting asyncmap. Rejecting.\n");
  569. q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, asyncmap, sizeof(asyncmap));
  570. break;
  571. case 3: // Authentication-Protocol
  572. {
  573. int proto = ntohs(*(uint16_t *)(o + 2));
  574. char proto_name[] = "0x0000";
  575. int alen;
  576. if (proto == PPPPAP)
  577. {
  578. if (config->radius_authtypes & AUTHPAP)
  579. {
  580. sess_local[s].lcp_authtype = AUTHPAP;
  581. break;
  582. }
  583. strcpy(proto_name, "PAP");
  584. }
  585. else if (proto == PPPCHAP)
  586. {
  587. if (config->radius_authtypes & AUTHCHAP
  588. && *(o + 4) == 5) // MD5
  589. {
  590. sess_local[s].lcp_authtype = AUTHCHAP;
  591. break;
  592. }
  593. strcpy(proto_name, "CHAP");
  594. }
  595. else
  596. sprintf(proto_name, "%#4.4x", proto);
  597. LOG(3, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name);
  598. alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authprefer);
  599. if (alen < 2) break; // paranoia
  600. q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
  601. if (q && *response == ConfigNak &&
  602. config->radius_authtypes != config->radius_authprefer)
  603. {
  604. // alternate type
  605. alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authtypes & ~config->radius_authprefer);
  606. if (alen < 2) break;
  607. q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
  608. }
  609. break;
  610. }
  611. break;
  612. case 4: // Quality-Protocol
  613. case 5: // Magic-Number
  614. case 7: // Protocol-Field-Compression
  615. case 8: // Address-And-Control-Field-Compression
  616. break;
  617. case 17: // Multilink Max-Receive-Reconstructed-Unit
  618. {
  619. uint16_t mrru = ntohs(*(uint16_t *)(o + 2));
  620. session[s].mrru = mrru;
  621. changed++;
  622. LOG(3, s, t, " Received PPP LCP option MRRU: %d\n",mrru);
  623. }
  624. break;
  625. case 18: // Multilink Short Sequence Number Header Format
  626. {
  627. session[s].mssf = 1;
  628. changed++;
  629. LOG(3, s, t, " Received PPP LCP option MSSN format\n");
  630. }
  631. break;
  632. case 19: // Multilink Endpoint Discriminator
  633. {
  634. uint8_t epdis_class = o[2];
  635. int addr;
  636. session[s].epdis.addr_class = epdis_class;
  637. session[s].epdis.length = length - 3;
  638. if (session[s].epdis.length > 20)
  639. {
  640. LOG(1, s, t, "Error: received EndDis Address Length more than 20: %d\n", session[s].epdis.length);
  641. session[s].epdis.length = 20;
  642. }
  643. for (addr = 0; addr < session[s].epdis.length; addr++)
  644. session[s].epdis.address[addr] = o[3+addr];
  645. changed++;
  646. switch (epdis_class)
  647. {
  648. case LOCALADDR:
  649. LOG(3, s, t, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class);
  650. break;
  651. case IPADDR:
  652. LOG(3, s, t, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class);
  653. break;
  654. case IEEEMACADDR:
  655. LOG(3, s, t, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class);
  656. break;
  657. case PPPMAGIC:
  658. LOG(3, s, t, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class);
  659. break;
  660. case PSNDN:
  661. LOG(3, s, t, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class);
  662. break;
  663. default:
  664. LOG(3, s, t, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class);
  665. }
  666. }
  667. break;
  668. default: // Reject any unknown options
  669. LOG(3, s, t, " Rejecting unknown PPP LCP option %d\n", type);
  670. q = ppp_conf_rej(s, b, sizeof(b), PPPLCP, &response, q, p, o);
  671. }
  672. x -= length;
  673. o += length;
  674. }
  675. if (changed)
  676. cluster_send_session(s);
  677. if (response)
  678. {
  679. l = q - response; // LCP packet length
  680. *((uint16_t *) (response + 2)) = htons(l); // update header
  681. }
  682. else
  683. {
  684. // Send packet back as ConfigAck
  685. response = makeppp(b, sizeof(b), p, l, s, t, PPPLCP, 0, 0, 0);
  686. if (!response) return;
  687. *response = ConfigAck;
  688. }
  689. switch (session[s].ppp.lcp)
  690. {
  691. case Closed:
  692. response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP, 0, 0, 0);
  693. if (!response) return;
  694. *response = TerminateAck;
  695. *((uint16_t *) (response + 2)) = htons(l = 4);
  696. break;
  697. case Stopped:
  698. initialise_restart_count(s, lcp);
  699. sendlcp(s, t);
  700. if (*response == ConfigAck)
  701. change_state(s, lcp, AckSent);
  702. else
  703. change_state(s, lcp, RequestSent);
  704. break;
  705. case RequestSent:
  706. if (*response == ConfigAck)
  707. change_state(s, lcp, AckSent);
  708. break;
  709. case AckReceived:
  710. if (*response == ConfigAck)
  711. lcp_open(s, t);
  712. break;
  713. case Opened:
  714. lcp_restart(s);
  715. sendlcp(s, t);
  716. /* fallthrough */
  717. case AckSent:
  718. if (*response == ConfigAck)
  719. change_state(s, lcp, AckSent);
  720. else
  721. change_state(s, lcp, RequestSent);
  722. break;
  723. case Closing:
  724. sessionshutdown(s, "LCP: ConfigReq in state Closing. This should not happen. Killing session.", CDN_ADMIN_DISC, TERM_LOST_SERVICE);
  725. break;
  726. default:
  727. LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
  728. return;
  729. }
  730. LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
  731. if (config->debug > 3) dumplcp(response, l);
  732. tunnelsend(b, l + (response - b), t);
  733. }
  734. else if (*p == ConfigNak || *p == ConfigRej)
  735. {
  736. int x = l - 4;
  737. uint8_t *o = (p + 4);
  738. int authtype = -1;
  739. while (x > 2)
  740. {
  741. int type = o[0];
  742. int length = o[1];
  743. if (length == 0 || type == 0 || x < length) break;
  744. switch (type)
  745. {
  746. case 1: // Maximum-Receive-Unit
  747. if (*p == ConfigNak)
  748. {
  749. if (length < 4) break;
  750. sess_local[s].ppp_mru = ntohs(*(uint16_t *)(o + 2));
  751. LOG(3, s, t, " Remote requested MRU of %u\n", sess_local[s].ppp_mru);
  752. }
  753. else
  754. {
  755. sess_local[s].ppp_mru = 0;
  756. LOG(3, s, t, " Remote rejected MRU negotiation\n");
  757. }
  758. break;
  759. case 3: // Authentication-Protocol
  760. if (authtype > 0)
  761. break;
  762. if (*p == ConfigNak)
  763. {
  764. int proto;
  765. if (length < 4) break;
  766. proto = ntohs(*(uint16_t *)(o + 2));
  767. if (proto == PPPPAP)
  768. {
  769. authtype = config->radius_authtypes & AUTHPAP;
  770. LOG(3, s, t, " Remote requested PAP authentication...%sing\n",
  771. authtype ? "accept" : "reject");
  772. }
  773. else if (proto == PPPCHAP && length > 4 && *(o + 4) == 5)
  774. {
  775. authtype = config->radius_authtypes & AUTHCHAP;
  776. LOG(3, s, t, " Remote requested CHAP authentication...%sing\n",
  777. authtype ? "accept" : "reject");
  778. }
  779. else
  780. {
  781. LOG(3, s, t, " Rejecting unsupported authentication %#4x\n",
  782. proto);
  783. }
  784. }
  785. else
  786. {
  787. LOG(2, s, t, "LCP: remote rejected auth negotiation\n");
  788. authtype = 0; // shutdown
  789. }
  790. break;
  791. case 5: // Magic-Number
  792. session[s].magic = 0;
  793. if (*p == ConfigNak)
  794. {
  795. if (length < 6) break;
  796. session[s].magic = ntohl(*(uint32_t *)(o + 2));
  797. }
  798. if (session[s].magic)
  799. LOG(3, s, t, " Remote requested magic-no %x\n", session[s].magic);
  800. else
  801. LOG(3, s, t, " Remote rejected magic-no\n");
  802. cluster_send_session(s);
  803. break;
  804. case 17: // Multilink Max-Receive-Reconstructed-Unit
  805. {
  806. if (*p == ConfigNak)
  807. {
  808. sess_local[s].mp_mrru = ntohs(*(uint16_t *)(o + 2));
  809. LOG(3, s, t, " Remote requested MRRU of %u\n", sess_local[s].mp_mrru);
  810. }
  811. else
  812. {
  813. sess_local[s].mp_mrru = 0;
  814. LOG(3, s, t, " Remote rejected MRRU negotiation\n");
  815. }
  816. }
  817. break;
  818. case 18: // Multilink Short Sequence Number Header Format
  819. {
  820. if (*p == ConfigNak)
  821. {
  822. sess_local[s].mp_mssf = 0;
  823. LOG(3, s, t, " Remote requested Naked mssf\n");
  824. }
  825. else
  826. {
  827. sess_local[s].mp_mssf = 0;
  828. LOG(3, s, t, " Remote rejected mssf\n");
  829. }
  830. }
  831. break;
  832. case 19: // Multilink Endpoint Discriminator
  833. {
  834. if (*p == ConfigNak)
  835. {
  836. LOG(2, s, t, " Remote should not configNak Endpoint Dis!\n");
  837. }
  838. else
  839. {
  840. sess_local[s].mp_epdis = 0;
  841. LOG(3, s, t, " Remote rejected Endpoint Discriminator\n");
  842. }
  843. }
  844. break;
  845. default:
  846. LOG(2, s, t, "LCP: remote sent %s for type %u?\n", ppp_code(*p), type);
  847. sessionshutdown(s, "Unable to negotiate LCP.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  848. return;
  849. }
  850. x -= length;
  851. o += length;
  852. }
  853. if (!authtype)
  854. {
  855. sessionshutdown(s, "Unsupported authentication.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  856. return;
  857. }
  858. if (authtype > 0)
  859. sess_local[s].lcp_authtype = authtype;
  860. switch (session[s].ppp.lcp)
  861. {
  862. case Closed:
  863. case Stopped:
  864. {
  865. uint8_t *response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP, 0, 0, 0);
  866. if (!response) return;
  867. *response = TerminateAck;
  868. *((uint16_t *) (response + 2)) = htons(l = 4);
  869. LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
  870. if (config->debug > 3) dumplcp(response, l);
  871. tunnelsend(b, l + (response - b), t);
  872. }
  873. break;
  874. case RequestSent:
  875. case AckSent:
  876. initialise_restart_count(s, lcp);
  877. sendlcp(s, t);
  878. break;
  879. case AckReceived:
  880. LOG(2, s, t, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
  881. sendlcp(s, t);
  882. break;
  883. case Opened:
  884. lcp_restart(s);
  885. sendlcp(s, t);
  886. break;
  887. default:
  888. LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
  889. return;
  890. }
  891. }
  892. else if (*p == TerminateReq)
  893. {
  894. switch (session[s].ppp.lcp)
  895. {
  896. case Closed:
  897. case Stopped:
  898. case Closing:
  899. case Stopping:
  900. case RequestSent:
  901. case AckReceived:
  902. case AckSent:
  903. break;
  904. case Opened:
  905. lcp_restart(s);
  906. zero_restart_count(s, lcp);
  907. change_state(s, lcp, Closing);
  908. break;
  909. default:
  910. LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
  911. return;
  912. }
  913. *p = TerminateAck; // send ack
  914. q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP, 0, 0, 0);
  915. if (!q) return;
  916. LOG(3, s, t, "LCP: send %s\n", ppp_code(*q));
  917. if (config->debug > 3) dumplcp(q, l);
  918. tunnelsend(b, l + (q - b), t); // send it
  919. }
  920. else if (*p == ProtocolRej)
  921. {
  922. uint16_t proto = 0;
  923. if (l > 4)
  924. {
  925. proto = *(p+4);
  926. if (l > 5 && !(proto & 1))
  927. {
  928. proto <<= 8;
  929. proto |= *(p+5);
  930. }
  931. }
  932. if (proto == PPPIPV6CP)
  933. {
  934. LOG(3, s, t, "IPv6 rejected\n");
  935. change_state(s, ipv6cp, Closed);
  936. }
  937. else
  938. {
  939. LOG(3, s, t, "LCP protocol reject: 0x%04X\n", proto);
  940. }
  941. }
  942. else if (*p == EchoReq)
  943. {
  944. *p = EchoReply; // reply
  945. *(uint32_t *) (p + 4) = htonl(session[s].magic); // our magic number
  946. q = makeppp(b, sizeof(b), p, l, s, t, PPPLCP, 0, 0, 0);
  947. if (!q) return;
  948. LOG(4, s, t, "LCP: send %s\n", ppp_code(*q));
  949. if (config->debug > 3) dumplcp(q, l);
  950. tunnelsend(b, l + (q - b), t); // send it
  951. if (session[s].ppp.phase == Network && session[s].ppp.ipv6cp == Opened)
  952. send_ipv6_ra(s, t, NULL); // send a RA
  953. }
  954. else if (*p == EchoReply)
  955. {
  956. // Ignore it, last_packet time is set earlier than this.
  957. }
  958. else if (*p != CodeRej)
  959. {
  960. ppp_code_rej(s, t, PPPLCP, "LCP", p, l, b, sizeof(b));
  961. }
  962. }
  963. int join_bundle(sessionidt s)
  964. {
  965. // Search for a bundle to join
  966. bundleidt i;
  967. bundleidt b;
  968. for (i = 1; i < MAXBUNDLE; i++)
  969. {
  970. if (bundle[i].state != BUNDLEFREE)
  971. {
  972. if (epdiscmp(session[s].epdis,bundle[i].epdis) && !strcmp(session[s].user, bundle[i].user))
  973. {
  974. sessionidt first_ses = bundle[i].members[0];
  975. if (bundle[i].mssf != session[s].mssf)
  976. {
  977. // uniformity of sequence number format must be insured
  978. LOG(3, s, session[s].tunnel, "MPPP: unable to bundle session %d in bundle %d cause of different mssf\n", s, i);
  979. return -1;
  980. }
  981. session[s].bundle = i;
  982. session[s].ip = session[first_ses].ip;
  983. session[s].dns1 = session[first_ses].dns1;
  984. session[s].dns2 = session[first_ses].dns2;
  985. session[s].timeout = session[first_ses].timeout;
  986. if(session[s].epdis.length > 0)
  987. setepdis(&bundle[i].epdis, session[s].epdis);
  988. strcpy(bundle[i].user, session[s].user);
  989. bundle[i].members[bundle[i].num_of_links] = s;
  990. bundle[i].num_of_links++;
  991. LOG(3, s, session[s].tunnel, "MPPP: Bundling additional line in bundle (%d), lines:%d\n",i,bundle[i].num_of_links);
  992. return i;
  993. }
  994. }
  995. }
  996. // No previously created bundle was found for this session, so create a new one
  997. if (!(b = new_bundle())) return 0;
  998. session[s].bundle = b;
  999. bundle[b].mrru = session[s].mrru;
  1000. bundle[b].mssf = session[s].mssf;
  1001. // FIXME !!! to enable l2tpns reading mssf frames receiver_max_seq, sender_max_seq must be introduce
  1002. // now session[s].mssf flag indecates that the receiver wish to receive frames in mssf, so max_seq (i.e. recv_max_seq) = 1<<24
  1003. /*
  1004. if (bundle[b].mssf)
  1005. bundle[b].max_seq = 1 << 12;
  1006. else */
  1007. bundle[b].max_seq = 1 << 24;
  1008. if(session[s].epdis.length > 0)
  1009. setepdis(&bundle[b].epdis, session[s].epdis);
  1010. strcpy(bundle[b].user, session[s].user);
  1011. bundle[b].members[0] = s;
  1012. bundle[b].timeout = session[s].timeout;
  1013. LOG(3, s, session[s].tunnel, "MPPP: Created a new bundle (%d)\n", b);
  1014. return b;
  1015. }
  1016. static int epdiscmp(epdist ep1, epdist ep2)
  1017. {
  1018. int ad;
  1019. if (ep1.length != ep2.length)
  1020. return 0;
  1021. if (ep1.addr_class != ep2.addr_class)
  1022. return 0;
  1023. for (ad = 0; ad < ep1.length; ad++)
  1024. if (ep1.address[ad] != ep2.address[ad])
  1025. return 0;
  1026. return 1;
  1027. }
  1028. static void setepdis(epdist *ep1, epdist ep2)
  1029. {
  1030. int ad;
  1031. ep1->length = ep2.length;
  1032. ep1->addr_class = ep2.addr_class;
  1033. for (ad = 0; ad < ep2.length; ad++)
  1034. ep1->address[ad] = ep2.address[ad];
  1035. }
  1036. static bundleidt new_bundle()
  1037. {
  1038. bundleidt i;
  1039. for (i = 1; i < MAXBUNDLE; i++)
  1040. {
  1041. if (bundle[i].state == BUNDLEFREE)
  1042. {
  1043. LOG(4, 0, 0, "MPPP: Assigning bundle ID %d\n", i);
  1044. bundle[i].num_of_links = 1;
  1045. bundle[i].last_check = time_now; // Initialize last_check value
  1046. bundle[i].state = BUNDLEOPEN;
  1047. bundle[i].current_ses = -1; // This is to enforce the first session 0 to be used at first
  1048. memset(&frag[i], 0, sizeof(fragmentationt));
  1049. if (i > config->cluster_highest_bundleid)
  1050. config->cluster_highest_bundleid = i;
  1051. return i;
  1052. }
  1053. }
  1054. LOG(0, 0, 0, "MPPP: Can't find a free bundle! There shouldn't be this many in use!\n");
  1055. return 0;
  1056. }
  1057. static void ipcp_open(sessionidt s, tunnelidt t)
  1058. {
  1059. LOG(3, s, t, "IPCP: Opened, session is now active\n");
  1060. change_state(s, ipcp, Opened);
  1061. if (!(session[s].walled_garden || session[s].flags & SESSION_STARTED))
  1062. {
  1063. uint16_t r = radiusnew(s);
  1064. if (r)
  1065. {
  1066. radiussend(r, RADIUSSTART); // send radius start
  1067. // don't send further Start records if IPCP is restarted
  1068. session[s].flags |= SESSION_STARTED;
  1069. cluster_send_session(s);
  1070. }
  1071. }
  1072. // start IPv6 if configured and still in passive state
  1073. if (session[s].ppp.ipv6cp == Stopped)
  1074. {
  1075. sendipv6cp(s, t);
  1076. change_state(s, ipv6cp, RequestSent);
  1077. }
  1078. }
  1079. // Process IPCP messages
  1080. void processipcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  1081. {
  1082. uint8_t b[MAXETHER];
  1083. uint8_t *q = 0;
  1084. uint16_t hl;
  1085. CSTAT(processipcp);
  1086. LOG_HEX(5, "IPCP", p, l);
  1087. if (l < 4)
  1088. {
  1089. LOG(1, s, t, "Short IPCP %d bytes\n", l);
  1090. STAT(tunnel_rx_errors);
  1091. return ;
  1092. }
  1093. if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
  1094. {
  1095. LOG(1, s, t, "Length mismatch IPCP %u/%u\n", hl, l);
  1096. STAT(tunnel_rx_errors);
  1097. return ;
  1098. }
  1099. l = hl;
  1100. if (session[s].ppp.phase < Network)
  1101. {
  1102. LOG(2, s, t, "IPCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
  1103. return;
  1104. }
  1105. LOG(3, s, t, "IPCP: recv %s\n", ppp_code(*p));
  1106. if (*p == ConfigAck)
  1107. {
  1108. switch (session[s].ppp.ipcp)
  1109. {
  1110. case RequestSent:
  1111. initialise_restart_count(s, ipcp);
  1112. change_state(s, ipcp, AckReceived);
  1113. break;
  1114. case AckReceived:
  1115. case Opened:
  1116. LOG(2, s, t, "IPCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipcp));
  1117. sendipcp(s, t);
  1118. change_state(s, ipcp, RequestSent);
  1119. break;
  1120. case AckSent:
  1121. ipcp_open(s, t);
  1122. break;
  1123. default:
  1124. LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
  1125. }
  1126. }
  1127. else if (*p == ConfigReq)
  1128. {
  1129. uint8_t *response = 0;
  1130. uint8_t *o = p + 4;
  1131. int length = l - 4;
  1132. int gotip = 0;
  1133. in_addr_t addr;
  1134. while (length > 2)
  1135. {
  1136. if (!o[1] || o[1] > length) return;
  1137. switch (*o)
  1138. {
  1139. case 3: // ip address
  1140. gotip++; // seen address
  1141. if (o[1] != 6) return;
  1142. addr = htonl(session[s].ip);
  1143. if (memcmp(o + 2, &addr, (sizeof addr)))
  1144. {
  1145. uint8_t *oq = q;
  1146. q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
  1147. if (!q || (q != oq && *response == ConfigRej))
  1148. {
  1149. sessionshutdown(s, "Can't negotiate IPCP.", CDN_ADMIN_DISC, TERM_USER_ERROR);
  1150. return;
  1151. }
  1152. }
  1153. break;
  1154. case 129: // primary DNS
  1155. if (o[1] != 6) return;
  1156. addr = htonl(session[s].dns1);
  1157. if (memcmp(o + 2, &addr, (sizeof addr)))
  1158. {
  1159. q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
  1160. if (!q) return;
  1161. }
  1162. break;
  1163. case 131: // secondary DNS
  1164. if (o[1] != 6) return;
  1165. addr = htonl(session[s].dns2);
  1166. if (memcmp(o + 2, &addr, sizeof(addr)))
  1167. {
  1168. q = ppp_conf_nak(s, b, sizeof(b), PPPIPCP, &response, q, p, o, (uint8_t *) &addr, sizeof(addr));
  1169. if (!q) return;
  1170. }
  1171. break;
  1172. default:
  1173. LOG(2, s, t, " Rejecting PPP IPCP Option type %d\n", *o);
  1174. q = ppp_conf_rej(s, b, sizeof(b), PPPIPCP, &response, q, p, o);
  1175. if (!q) return;
  1176. }
  1177. length -= o[1];
  1178. o += o[1];
  1179. }
  1180. if (response)
  1181. {
  1182. l = q - response; // IPCP packet length
  1183. *((uint16_t *) (response + 2)) = htons(l); // update header
  1184. }
  1185. else if (gotip)
  1186. {
  1187. // Send packet back as ConfigAck
  1188. response = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP, 0, 0, 0);
  1189. if (!response) return;
  1190. *response = ConfigAck;
  1191. }
  1192. else
  1193. {
  1194. LOG(1, s, t, "No IP in IPCP request\n");
  1195. STAT(tunnel_rx_errors);
  1196. return;
  1197. }
  1198. switch (session[s].ppp.ipcp)
  1199. {
  1200. case Closed:
  1201. response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPCP, 0, 0, 0);
  1202. if (!response) return;
  1203. *response = TerminateAck;
  1204. *((uint16_t *) (response + 2)) = htons(l = 4);
  1205. break;
  1206. case Stopped:
  1207. initialise_restart_count(s, ipcp);
  1208. sendipcp(s, t);
  1209. if (*response == ConfigAck)
  1210. change_state(s, ipcp, AckSent);
  1211. else
  1212. change_state(s, ipcp, RequestSent);
  1213. break;
  1214. case RequestSent:
  1215. if (*response == ConfigAck)
  1216. change_state(s, ipcp, AckSent);
  1217. break;
  1218. case AckReceived:
  1219. if (*response == ConfigAck)
  1220. ipcp_open(s, t);
  1221. break;
  1222. case Opened:
  1223. initialise_restart_count(s, ipcp);
  1224. sendipcp(s, t);
  1225. /* fallthrough */
  1226. case AckSent:
  1227. if (*response == ConfigAck)
  1228. change_state(s, ipcp, AckSent);
  1229. else
  1230. change_state(s, ipcp, RequestSent);
  1231. break;
  1232. default:
  1233. LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
  1234. return;
  1235. }
  1236. LOG(3, s, t, "IPCP: send %s\n", ppp_code(*response));
  1237. tunnelsend(b, l + (response - b), t);
  1238. }
  1239. else if (*p == TerminateReq)
  1240. {
  1241. switch (session[s].ppp.ipcp)
  1242. {
  1243. case Closed:
  1244. case Stopped:
  1245. case Closing:
  1246. case Stopping:
  1247. case RequestSent:
  1248. case AckReceived:
  1249. case AckSent:
  1250. break;
  1251. case Opened:
  1252. zero_restart_count(s, ipcp);
  1253. change_state(s, ipcp, Closing);
  1254. break;
  1255. default:
  1256. LOG(2, s, t, "IPCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipcp));
  1257. return;
  1258. }
  1259. *p = TerminateAck; // send ack
  1260. q = makeppp(b, sizeof(b), p, l, s, t, PPPIPCP, 0, 0, 0);
  1261. if (!q) return;
  1262. LOG(3, s, t, "IPCP: send %s\n", ppp_code(*q));
  1263. tunnelsend(b, l + (q - b), t); // send it
  1264. }
  1265. else if (*p != CodeRej)
  1266. {
  1267. ppp_code_rej(s, t, PPPIPCP, "IPCP", p, l, b, sizeof(b));
  1268. }
  1269. }
  1270. static void ipv6cp_open(sessionidt s, tunnelidt t)
  1271. {
  1272. int i;
  1273. LOG(3, s, t, "IPV6CP: Opened\n");
  1274. change_state(s, ipv6cp, Opened);
  1275. for (i = 0; i < MAXROUTE6 && session[s].route6[i].ipv6prefixlen; i++)
  1276. {
  1277. route6set(s, session[s].route6[i].ipv6route, session[s].route6[i].ipv6prefixlen, 1);
  1278. }
  1279. if (session[s].ipv6address.s6_addr[0])
  1280. {
  1281. // Check if included in prefix
  1282. if (sessionbyipv6(session[s].ipv6address) != s)
  1283. route6set(s, session[s].ipv6address, 128, 1);
  1284. }
  1285. // Send an initial RA (TODO: Should we send these regularly?)
  1286. send_ipv6_ra(s, t, NULL);
  1287. }
  1288. // Process IPV6CP messages
  1289. void processipv6cp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  1290. {
  1291. uint8_t b[MAXETHER];
  1292. uint8_t *q = 0;
  1293. uint16_t hl;
  1294. CSTAT(processipv6cp);
  1295. LOG_HEX(5, "IPV6CP", p, l);
  1296. if (l < 4)
  1297. {
  1298. LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
  1299. STAT(tunnel_rx_errors);
  1300. return ;
  1301. }
  1302. if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
  1303. {
  1304. LOG(1, s, t, "Length mismatch IPV6CP %u/%u\n", hl, l);
  1305. STAT(tunnel_rx_errors);
  1306. return ;
  1307. }
  1308. l = hl;
  1309. if (session[s].ppp.phase < Network)
  1310. {
  1311. LOG(2, s, t, "IPV6CP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
  1312. return;
  1313. }
  1314. LOG(3, s, t, "IPV6CP: recv %s\n", ppp_code(*p));
  1315. if (!session[s].ip)
  1316. {
  1317. LOG(3, s, t, "IPV6CP: no IPv4 address (IPCP in state %s)\n", ppp_state(session[s].ppp.ipcp));
  1318. return; // need IPCP to complete...
  1319. }
  1320. if (*p == ConfigAck)
  1321. {
  1322. switch (session[s].ppp.ipv6cp)
  1323. {
  1324. case RequestSent:
  1325. initialise_restart_count(s, ipv6cp);
  1326. change_state(s, ipv6cp, AckReceived);
  1327. break;
  1328. case AckReceived:
  1329. case Opened:
  1330. LOG(2, s, t, "IPV6CP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ipv6cp));
  1331. sendipv6cp(s, t);
  1332. change_state(s, ipv6cp, RequestSent);
  1333. break;
  1334. case AckSent:
  1335. ipv6cp_open(s, t);
  1336. break;
  1337. default:
  1338. LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
  1339. }
  1340. }
  1341. else if (*p == ConfigReq)
  1342. {
  1343. uint8_t *response = 0;
  1344. uint8_t *o = p + 4;
  1345. int length = l - 4;
  1346. int gotip = 0;
  1347. uint32_t ident[2];
  1348. while (length > 2)
  1349. {
  1350. if (!o[1] || o[1] > length) return;
  1351. switch (*o)
  1352. {
  1353. case 1: // interface identifier
  1354. gotip++; // seen address
  1355. if (o[1] != 10) return;
  1356. if (session[s].ipv6address.s6_addr[0])
  1357. {
  1358. // LSB 64bits of assigned IPv6 address to user (see radius attribut Framed-IPv6-Address)
  1359. memcpy(&ident[0], &session[s].ipv6address.s6_addr[8], 8);
  1360. }
  1361. else
  1362. {
  1363. ident[0] = htonl(session[s].ip);
  1364. ident[1] = 0;
  1365. }
  1366. if (memcmp(o + 2, ident, sizeof(ident)))
  1367. {
  1368. q = ppp_conf_nak(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o, (uint8_t *)ident, sizeof(ident));
  1369. if (!q) return;
  1370. }
  1371. break;
  1372. default:
  1373. LOG(2, s, t, " Rejecting PPP IPV6CP Option type %d\n", *o);
  1374. q = ppp_conf_rej(s, b, sizeof(b), PPPIPV6CP, &response, q, p, o);
  1375. if (!q) return;
  1376. }
  1377. length -= o[1];
  1378. o += o[1];
  1379. }
  1380. if (response)
  1381. {
  1382. l = q - response; // IPV6CP packet length
  1383. *((uint16_t *) (response + 2)) = htons(l); // update header
  1384. }
  1385. else if (gotip)
  1386. {
  1387. // Send packet back as ConfigAck
  1388. response = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP, 0, 0, 0);
  1389. if (!response) return;
  1390. *response = ConfigAck;
  1391. }
  1392. else
  1393. {
  1394. LOG(1, s, t, "No interface identifier in IPV6CP request\n");
  1395. STAT(tunnel_rx_errors);
  1396. return;
  1397. }
  1398. switch (session[s].ppp.ipv6cp)
  1399. {
  1400. case Closed:
  1401. response = makeppp(b, sizeof(b), p, 2, s, t, PPPIPV6CP, 0, 0, 0);
  1402. if (!response) return;
  1403. *response = TerminateAck;
  1404. *((uint16_t *) (response + 2)) = htons(l = 4);
  1405. break;
  1406. case Stopped:
  1407. initialise_restart_count(s, ipv6cp);
  1408. sendipv6cp(s, t);
  1409. if (*response == ConfigAck)
  1410. change_state(s, ipv6cp, AckSent);
  1411. else
  1412. change_state(s, ipv6cp, RequestSent);
  1413. break;
  1414. case RequestSent:
  1415. if (*response == ConfigAck)
  1416. change_state(s, ipv6cp, AckSent);
  1417. break;
  1418. case AckReceived:
  1419. if (*response == ConfigAck)
  1420. ipv6cp_open(s, t);
  1421. break;
  1422. case Opened:
  1423. initialise_restart_count(s, ipv6cp);
  1424. sendipv6cp(s, t);
  1425. /* fallthrough */
  1426. case AckSent:
  1427. if (*response == ConfigAck)
  1428. change_state(s, ipv6cp, AckSent);
  1429. else
  1430. change_state(s, ipv6cp, RequestSent);
  1431. break;
  1432. default:
  1433. LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
  1434. return;
  1435. }
  1436. LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*response));
  1437. tunnelsend(b, l + (response - b), t);
  1438. }
  1439. else if (*p == TerminateReq)
  1440. {
  1441. switch (session[s].ppp.ipv6cp)
  1442. {
  1443. case Closed:
  1444. case Stopped:
  1445. case Closing:
  1446. case Stopping:
  1447. case RequestSent:
  1448. case AckReceived:
  1449. case AckSent:
  1450. break;
  1451. case Opened:
  1452. zero_restart_count(s, ipv6cp);
  1453. change_state(s, ipv6cp, Closing);
  1454. break;
  1455. default:
  1456. LOG(2, s, t, "IPV6CP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ipv6cp));
  1457. return;
  1458. }
  1459. *p = TerminateAck; // send ack
  1460. q = makeppp(b, sizeof(b), p, l, s, t, PPPIPV6CP, 0, 0, 0);
  1461. if (!q) return;
  1462. LOG(3, s, t, "IPV6CP: send %s\n", ppp_code(*q));
  1463. tunnelsend(b, l + (q - b), t); // send it
  1464. }
  1465. else if (*p != CodeRej)
  1466. {
  1467. ppp_code_rej(s, t, PPPIPV6CP, "IPV6CP", p, l, b, sizeof(b));
  1468. }
  1469. }
  1470. static void update_sessions_in_stat(sessionidt s, uint16_t l)
  1471. {
  1472. bundleidt b = session[s].bundle;
  1473. if (!b)
  1474. {
  1475. increment_counter(&session[s].cin, &session[s].cin_wrap, l);
  1476. session[s].cin_delta += l;
  1477. session[s].pin++;
  1478. sess_local[s].cin += l;
  1479. sess_local[s].pin++;
  1480. }
  1481. else
  1482. {
  1483. int i = frag[b].re_frame_begin_index;
  1484. int end = frag[b].re_frame_end_index;
  1485. for (;;)
  1486. {
  1487. l = frag[b].fragment[i].length;
  1488. s = frag[b].fragment[i].sid;
  1489. increment_counter(&session[s].cin, &session[s].cin_wrap, l);
  1490. session[s].cin_delta += l;
  1491. session[s].pin++;
  1492. sess_local[s].cin += l;
  1493. sess_local[s].pin++;
  1494. if (i == end)
  1495. return;
  1496. i = (i + 1) & MAXFRAGNUM_MASK;
  1497. }
  1498. }
  1499. }
  1500. // process IP packet received
  1501. //
  1502. // This MUST be called with at least 4 byte behind 'p'.
  1503. // (i.e. this routine writes to p[-4]).
  1504. void processipin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  1505. {
  1506. in_addr_t ip, ip_dst;
  1507. CSTAT(processipin);
  1508. LOG_HEX(5, "IP", p, l);
  1509. if (l < 20)
  1510. {
  1511. LOG(1, s, t, "IP packet too short %d\n", l);
  1512. STAT(tunnel_rx_errors);
  1513. return ;
  1514. }
  1515. ip = ntohl(*(uint32_t *)(p + 12));
  1516. ip_dst = *(uint32_t *)(p + 16);
  1517. if (l > MAXETHER)
  1518. {
  1519. LOG(1, s, t, "IP packet too long %d\n", l);
  1520. STAT(tunnel_rx_errors);
  1521. return ;
  1522. }
  1523. if (session[s].ppp.phase != Network || session[s].ppp.ipcp != Opened)
  1524. return;
  1525. if (!session[s].bundle || bundle[session[s].bundle].num_of_links < 2) // FIXME:
  1526. {
  1527. // no spoof (do sessionbyip to handled statically routed subnets)
  1528. if (!config->disable_no_spoof && ip != session[s].ip && sessionbyip(htonl(ip)) != s)
  1529. {
  1530. LOG(4, s, t, "Dropping packet with spoofed IP %s\n", fmtaddr(htonl(ip), 0));
  1531. return;
  1532. }
  1533. }
  1534. // run access-list if any
  1535. if (session[s].filter_in && !ip_filter(p, l, session[s].filter_in - 1))
  1536. return;
  1537. // adjust MSS on SYN and SYN,ACK packets with options
  1538. if ((ntohs(*(uint16_t *) (p + 6)) & 0x1fff) == 0 && p[9] == IPPROTO_TCP) // first tcp fragment
  1539. {
  1540. int ihl = (p[0] & 0xf) * 4; // length of IP header
  1541. if (l >= ihl + 20 && (p[ihl + 13] & TCP_FLAG_SYN) && ((p[ihl + 12] >> 4) > 5))
  1542. adjust_tcp_mss(s, t, p, l, p + ihl);
  1543. }
  1544. // Add on the tun header
  1545. p -= 4;
  1546. *(uint32_t *) p = htonl(PKTIP);
  1547. l += 4;
  1548. if (session[s].tbf_in)
  1549. {
  1550. if (!config->no_throttle_local_IP || !sessionbyip(ip_dst))
  1551. {
  1552. // Are we throttling this session?
  1553. if (config->cluster_iam_master)
  1554. tbf_queue_packet(session[s].tbf_in, p, l);
  1555. else
  1556. master_throttle_packet(session[s].tbf_in, p, l);
  1557. return;
  1558. }
  1559. }
  1560. // send to ethernet
  1561. if (tun_write(p, l) < 0)
  1562. {
  1563. STAT(tun_tx_errors);
  1564. LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
  1565. l, strerror(errno), tunfd, p);
  1566. return;
  1567. }
  1568. p += 4;
  1569. l -= 4;
  1570. if (session[s].snoop_ip && session[s].snoop_port)
  1571. {
  1572. // Snooping this session
  1573. snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
  1574. }
  1575. update_sessions_in_stat(s, l);
  1576. eth_tx += l;
  1577. STAT(tun_tx_packets);
  1578. INC_STAT(tun_tx_bytes, l);
  1579. }
  1580. // process Multilink PPP packet received
  1581. void processmpin(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  1582. {
  1583. bundleidt b = session[s].bundle;
  1584. bundlet * this_bundle = &bundle[b];
  1585. uint32_t maskSeq, max_seq;
  1586. int frag_offset;
  1587. uint16_t frag_index, frag_index_next, frag_index_prev;
  1588. fragmentationt *this_fragmentation = &frag[b];
  1589. uint8_t begin_frame = (*p & MP_BEGIN);
  1590. uint8_t end_frame = (*p & MP_END);
  1591. uint32_t seq_num, seq_num_next, seq_num_prev;
  1592. uint32_t i;
  1593. uint8_t flags = *p;
  1594. uint16_t begin_index, end_index;
  1595. // Perform length checking
  1596. if(l > MAXFRAGLEN)
  1597. {
  1598. LOG(2, s, t, "MPPP: discarding fragment larger than MAXFRAGLEN\n");
  1599. return;
  1600. }
  1601. if(!b)
  1602. {
  1603. LOG(2, s, t, "MPPP: Invalid bundle id: 0\n");
  1604. return;
  1605. }
  1606. // FIXME !! session[s].mssf means that the receiver wants to receive frames in mssf not means the receiver will send frames in mssf
  1607. /* if(session[s].mssf)
  1608. {
  1609. // Get 12 bit for seq number
  1610. seq_num = ntohs((*(uint16_t *) p) & 0xFF0F);
  1611. p += 2;
  1612. l -= 2;
  1613. // After this point the pointer should be advanced 2 bytes
  1614. LOG(3, s, t, "MPPP: 12 bits, sequence number: %d\n",seq_num);
  1615. }
  1616. else */
  1617. {
  1618. // Get 24 bit for seq number
  1619. seq_num = ntohl((*(uint32_t *) p) & 0xFFFFFF00);
  1620. p += 4;
  1621. l -= 4;
  1622. // After this point the pointer should be advanced 4 bytes
  1623. LOG(4, s, t, "MPPP: 24 bits sequence number:%d\n",seq_num);
  1624. }
  1625. max_seq = this_bundle->max_seq;
  1626. maskSeq = max_seq - 1;
  1627. /*
  1628. * Expand sequence number to 32 bits, making it as close
  1629. * as possible to this_fragmentation->M.
  1630. */
  1631. seq_num |= this_fragmentation->M & ~maskSeq;
  1632. if ((int)(this_fragmentation->M - seq_num) > (int)(maskSeq >> 1))
  1633. {
  1634. seq_num += maskSeq + 1;
  1635. }
  1636. else if ((int)(seq_num - this_fragmentation->M) > (int)(maskSeq >> 1))
  1637. {
  1638. seq_num -= maskSeq + 1; /* should never happen */
  1639. }
  1640. // calculate this fragment's offset from the begin seq in the bundle
  1641. frag_offset = (int) (seq_num - this_fragmentation->start_seq);
  1642. sess_local[s].last_seq = seq_num;
  1643. // calculate the jitter average
  1644. uint32_t ljitter = time_now_ms - sess_local[s].prev_time;
  1645. if (ljitter > 0)
  1646. {
  1647. sess_local[s].jitteravg = (sess_local[s].jitteravg + ljitter)>>1;
  1648. sess_local[s].prev_time = time_now_ms;
  1649. }
  1650. uint32_t Mmin;
  1651. if (seq_num < this_fragmentation->M)
  1652. {
  1653. Mmin = seq_num;
  1654. this_fragmentation->M = seq_num;
  1655. }
  1656. else
  1657. {
  1658. Mmin = sess_local[(this_bundle->members[0])].last_seq;
  1659. for (i = 1; i < this_bundle->num_of_links; i++)
  1660. {
  1661. uint32_t s_seq = sess_local[(this_bundle->members[i])].last_seq;
  1662. if (s_seq < Mmin)
  1663. Mmin = s_seq;
  1664. }
  1665. this_fragmentation->M = Mmin;
  1666. }
  1667. // calculate M offset of the M seq in the bundle
  1668. int M_offset = (int) (Mmin - this_fragmentation->start_seq);
  1669. if (M_offset >= MAXFRAGNUM)
  1670. {
  1671. // There have a long break of the link !!!!!!!!
  1672. // M_offset is bigger that the fragmentation buffer size
  1673. LOG(3, s, t, "MPPP: M_offset out of range, min:%d, begin_seq:%d\n", Mmin, this_fragmentation->start_seq);
  1674. // Calculate the new start index, the previous frag are lost
  1675. begin_index = (M_offset + this_fragmentation->start_index) & MAXFRAGNUM_MASK;
  1676. // Set new Start sequence
  1677. this_fragmentation->start_index = begin_index;
  1678. this_fragmentation->start_seq = Mmin;
  1679. M_offset = 0;
  1680. // recalculate the fragment offset from the new begin seq in the bundle
  1681. frag_offset = (int) (seq_num - Mmin);
  1682. }
  1683. // discard this fragment if the packet comes before the start sequence
  1684. if (frag_offset < 0)
  1685. {
  1686. // this packet comes before the next
  1687. LOG(3, s, t, "MPPP: (COMES BEFORE) the next, seq:%d, begin_seq:%d, size_frag:%d, flags:%02X is LOST\n", seq_num, this_fragmentation->start_seq, l, flags);
  1688. return;
  1689. }
  1690. // discard if frag_offset is bigger that the fragmentation buffer size
  1691. if (frag_offset >= MAXFRAGNUM)
  1692. {
  1693. // frag_offset is bigger that the fragmentation buffer size
  1694. LOG(3, s, t, "MPPP: Index out of range, seq:%d, begin_seq:%d\n", seq_num, this_fragmentation->start_seq);
  1695. return;
  1696. }
  1697. //caculate received fragment's index in the fragment array
  1698. frag_index = (frag_offset + this_fragmentation->start_index) & MAXFRAGNUM_MASK;
  1699. // insert the frame in it's place
  1700. fragmentt *this_frag = &this_fragmentation->fragment[frag_index];
  1701. if (this_frag->length > 0)
  1702. // This fragment is lost, It was around the buffer and it was never completed the packet.
  1703. LOG(3, this_frag->sid, this_frag->tid, "MPPP: (INSERT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
  1704. this_frag->seq, frag_index, this_frag->flags);
  1705. this_frag->length = l;
  1706. this_frag->sid = s;
  1707. this_frag->tid = t;
  1708. this_frag->flags = flags;
  1709. this_frag->seq = seq_num;
  1710. this_frag->jitteravg = sess_local[s].jitteravg;
  1711. memcpy(this_frag->data, p, l);
  1712. LOG(4, s, t, "MPPP: seq_num:%d frag_index:%d INSERTED flags: %02X\n", seq_num, frag_index, flags);
  1713. //next frag index
  1714. frag_index_next = (frag_index + 1) & MAXFRAGNUM_MASK;
  1715. //previous frag index
  1716. frag_index_prev = (frag_index - 1) & MAXFRAGNUM_MASK;
  1717. // next seq
  1718. seq_num_next = seq_num + 1;
  1719. // previous seq
  1720. seq_num_prev = seq_num - 1;
  1721. // Clean the buffer and log the lost fragments
  1722. if ((frag_index_next != this_fragmentation->start_index) && this_fragmentation->fragment[frag_index_next].length)
  1723. {
  1724. // check if the next frag is a lost fragment
  1725. if (this_fragmentation->fragment[frag_index_next].seq != seq_num_next)
  1726. {
  1727. // This fragment is lost, It was around the buffer and it was never completed the packet.
  1728. LOG(3, this_fragmentation->fragment[frag_index_next].sid, this_fragmentation->fragment[frag_index_next].tid,
  1729. "MPPP: (NEXT) seq_num:%d frag_index:%d flags:%02X is LOST\n",
  1730. this_fragmentation->fragment[frag_index_next].seq, frag_index_next,
  1731. this_fragmentation->fragment[frag_index_next].flags);
  1732. // this frag is lost
  1733. this_fragmentation->fragment[frag_index_next].length = 0;
  1734. this_fragmentation->fragment[frag_index_next].flags = 0;
  1735. if (begin_frame && (!end_frame)) return; // assembling frame failed
  1736. }
  1737. }
  1738. // Clean the buffer and log the lost fragments
  1739. if ((frag_index != this_fragmentation->start_index) && this_fragmentation->fragment[frag_index_prev].length)
  1740. {
  1741. // check if the next frag is a lost fragment
  1742. if (this_fragmentation->fragment[frag_index_prev].seq != seq_num_prev)
  1743. {
  1744. // This fragment is lost, It was around the buffer and it was never completed the packet.
  1745. LOG(3, this_fragmentation->fragment[frag_index_prev].sid, this_fragmentation->fragment[frag_index_prev].tid,
  1746. "MPPP: (PREV) seq_num:%d frag_index:%d flags:%02X is LOST\n",
  1747. this_fragmentation->fragment[frag_index_prev].seq, frag_index_prev,
  1748. this_fragmentation->fragment[frag_index_prev].flags);
  1749. this_fragmentation->fragment[frag_index_prev].length = 0;
  1750. this_fragmentation->fragment[frag_index_prev].flags = 0;
  1751. if (end_frame && (!begin_frame)) return; // assembling frame failed
  1752. }
  1753. }
  1754. find_frame:
  1755. begin_index = this_fragmentation->start_index;
  1756. uint32_t b_seq = this_fragmentation->start_seq;
  1757. // Try to find a Begin sequence from the start_seq sequence to M sequence
  1758. while (b_seq < Mmin)
  1759. {
  1760. if (this_fragmentation->fragment[begin_index].length)
  1761. {
  1762. if (b_seq == this_fragmentation->fragment[begin_index].seq)
  1763. {
  1764. if (this_fragmentation->fragment[begin_index].flags & MP_BEGIN)
  1765. {
  1766. int isfoundE = 0;
  1767. // Adjust the new start sequence and start index
  1768. this_fragmentation->start_index = begin_index;
  1769. this_fragmentation->start_seq = b_seq;
  1770. // Begin Sequence found, now try to found the End Sequence to complete the frame
  1771. end_index = begin_index;
  1772. while (b_seq < Mmin)
  1773. {
  1774. if (this_fragmentation->fragment[end_index].length)
  1775. {
  1776. if (b_seq == this_fragmentation->fragment[end_index].seq)
  1777. {
  1778. if (this_fragmentation->fragment[end_index].flags & MP_END)
  1779. {
  1780. // The End sequence was found and the frame is complete
  1781. isfoundE = 1;
  1782. break;
  1783. }
  1784. }
  1785. else
  1786. {
  1787. // This fragment is lost, it was never completed the packet.
  1788. LOG(3, this_fragmentation->fragment[end_index].sid, this_fragmentation->fragment[end_index].tid,
  1789. "MPPP: (FIND END) seq_num:%d frag_index:%d flags:%02X is LOST\n",
  1790. this_fragmentation->fragment[end_index].seq, begin_index,
  1791. this_fragmentation->fragment[end_index].flags);
  1792. // this frag is lost
  1793. this_fragmentation->fragment[end_index].length = 0;
  1794. this_fragmentation->fragment[end_index].flags = 0;
  1795. // This frame is not complete find the next Begin
  1796. break;
  1797. }
  1798. }
  1799. else
  1800. {
  1801. // This frame is not complete find the next Begin if exist
  1802. break;
  1803. }
  1804. end_index = (end_index +1) & MAXFRAGNUM_MASK;
  1805. b_seq++;
  1806. }
  1807. if (isfoundE)
  1808. // The End sequence was found and the frame is complete
  1809. break;
  1810. else
  1811. // find the next Begin
  1812. begin_index = end_index;
  1813. }
  1814. }
  1815. else
  1816. {
  1817. // This fragment is lost, it was never completed the packet.
  1818. LOG(3, this_fragmentation->fragment[begin_index].sid, this_fragmentation->fragment[begin_index].tid,
  1819. "MPPP: (FIND BEGIN) seq_num:%d frag_index:%d flags:%02X is LOST\n",
  1820. this_fragmentation->fragment[begin_index].seq, begin_index,
  1821. this_fragmentation->fragment[begin_index].flags);
  1822. // this frag is lost
  1823. this_fragmentation->fragment[begin_index].length = 0;
  1824. this_fragmentation->fragment[begin_index].flags = 0;
  1825. }
  1826. }
  1827. begin_index = (begin_index +1) & MAXFRAGNUM_MASK;
  1828. b_seq++;
  1829. }
  1830. assembling_frame:
  1831. // try to assemble the frame that has the received fragment as a member
  1832. // get the beginning of this frame
  1833. begin_index = end_index = this_fragmentation->start_index;
  1834. if (this_fragmentation->fragment[begin_index].length)
  1835. {
  1836. if (!(this_fragmentation->fragment[begin_index].flags & MP_BEGIN))
  1837. {
  1838. LOG(3, this_fragmentation->fragment[begin_index].sid, this_fragmentation->fragment[begin_index].tid,
  1839. "MPPP: (NOT BEGIN) seq_num:%d frag_index:%d flags:%02X\n",
  1840. this_fragmentation->fragment[begin_index].seq, begin_index,
  1841. this_fragmentation->fragment[begin_index].flags);
  1842. // should occur only after an "M_Offset out of range"
  1843. // The start sequence must be a begin sequence
  1844. this_fragmentation->start_index = (begin_index +1) & MAXFRAGNUM_MASK;
  1845. this_fragmentation->start_seq++;
  1846. return; // assembling frame failed
  1847. }
  1848. }
  1849. else
  1850. return; // assembling frame failed
  1851. // get the end of his frame
  1852. while (this_fragmentation->fragment[end_index].length)
  1853. {
  1854. if (this_fragmentation->fragment[end_index].flags & MP_END)
  1855. break;
  1856. end_index = (end_index +1) & MAXFRAGNUM_MASK;
  1857. if (end_index == this_fragmentation->start_index)
  1858. return; // assembling frame failed
  1859. }
  1860. // return if a lost fragment is found
  1861. if (!(this_fragmentation->fragment[end_index].length))
  1862. return; // assembling frame failed
  1863. // assemble the packet
  1864. //assemble frame, process it, reset fragmentation
  1865. uint16_t cur_len = 4; // This is set to 4 to leave 4 bytes for function processipin
  1866. LOG(4, s, t, "MPPP: processing fragments from %d to %d\n", begin_index, end_index);
  1867. // Push to the receive buffer
  1868. for (i = begin_index;; i = (i + 1) & MAXFRAGNUM_MASK)
  1869. {
  1870. this_frag = &this_fragmentation->fragment[i];
  1871. if(cur_len + this_frag->length > MAXETHER)
  1872. {
  1873. LOG(2, s, t, "MPPP: discarding reassembled frames larger than MAXETHER\n");
  1874. break;
  1875. }
  1876. memcpy(this_fragmentation->reassembled_frame+cur_len, this_frag->data, this_frag->length);
  1877. LOG(5, s, t, "MPPP: processing frame at %d, with len %d\n", i, this_frag->length);
  1878. cur_len += this_frag->length;
  1879. if (i == end_index)
  1880. {
  1881. this_fragmentation->re_frame_len = cur_len;
  1882. this_fragmentation->re_frame_begin_index = begin_index;
  1883. this_fragmentation->re_frame_end_index = end_index;
  1884. // Process the resassembled frame
  1885. LOG(5, s, t, "MPPP: Process the reassembled frame, len=%d\n",cur_len);
  1886. processmpframe(s, t, this_fragmentation->reassembled_frame, this_fragmentation->re_frame_len, 1);
  1887. break;
  1888. }
  1889. }
  1890. // Set reassembled frame length to zero after processing it
  1891. this_fragmentation->re_frame_len = 0;
  1892. for (i = begin_index;; i = (i + 1) & MAXFRAGNUM_MASK)
  1893. {
  1894. this_fragmentation->fragment[i].length = 0; // Indicates that this fragment has been consumed
  1895. this_fragmentation->fragment[i].flags = 0;
  1896. if (i == end_index)
  1897. break;
  1898. }
  1899. // Set the new start_index and start_seq
  1900. this_fragmentation->start_index = (end_index + 1) & MAXFRAGNUM_MASK;
  1901. this_fragmentation->start_seq = this_fragmentation->fragment[end_index].seq + 1;
  1902. LOG(4, s, t, "MPPP after assembling: start index is = %d, start seq=%d\n", this_fragmentation->start_index, this_fragmentation->start_seq);
  1903. begin_index = this_fragmentation->start_index;
  1904. if ((this_fragmentation->fragment[begin_index].length) &&
  1905. (this_fragmentation->fragment[begin_index].seq != this_fragmentation->start_seq))
  1906. {
  1907. LOG(3, this_fragmentation->fragment[begin_index].sid, this_fragmentation->fragment[begin_index].tid,
  1908. "MPPP: (START) seq_num:%d frag_index:%d flags:%02X is LOST\n",
  1909. this_fragmentation->fragment[begin_index].seq, begin_index,
  1910. this_fragmentation->fragment[begin_index].flags);
  1911. this_fragmentation->fragment[begin_index].length = 0;
  1912. this_fragmentation->fragment[begin_index].flags = 0;
  1913. }
  1914. if (this_fragmentation->start_seq <= Mmin)
  1915. // It's possible to find other complete frame or lost frame.
  1916. goto find_frame;
  1917. else if ((this_fragmentation->fragment[begin_index].length) &&
  1918. (this_fragmentation->fragment[begin_index].flags & MP_BEGIN))
  1919. // may be that the next frame is completed
  1920. goto assembling_frame;
  1921. return;
  1922. }
  1923. // process IPv6 packet received
  1924. //
  1925. // This MUST be called with at least 4 byte behind 'p'.
  1926. // (i.e. this routine writes to p[-4]).
  1927. void processipv6in(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  1928. {
  1929. struct in6_addr ip;
  1930. in_addr_t ipv4;
  1931. CSTAT(processipv6in);
  1932. LOG_HEX(5, "IPv6", p, l);
  1933. ip = *(struct in6_addr *) (p + 8);
  1934. ipv4 = ntohl(*(uint32_t *)(p + 16));
  1935. if (l > MAXETHER)
  1936. {
  1937. LOG(1, s, t, "IP packet too long %d\n", l);
  1938. STAT(tunnel_rx_errors);
  1939. return ;
  1940. }
  1941. if (session[s].ppp.phase != Network || session[s].ppp.ipv6cp != Opened)
  1942. return;
  1943. // no spoof
  1944. if (session[s].ipv6address.s6_addr[0])
  1945. {
  1946. if ((sessionbyipv6new(ip) != s) &&
  1947. (ip.s6_addr[0] != 0xFE || ip.s6_addr[1] != 0x80 || ip.s6_addr16[1] != 0 || ip.s6_addr16[2] != 0 || ip.s6_addr16[3] != 0))
  1948. {
  1949. char str[INET6_ADDRSTRLEN];
  1950. LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
  1951. inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
  1952. return;
  1953. }
  1954. }
  1955. else if ((ipv4 != session[s].ip || memcmp(&config->ipv6_prefix, &ip, 8)) && sessionbyipv6(ip) != s)
  1956. {
  1957. char str[INET6_ADDRSTRLEN];
  1958. LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
  1959. inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
  1960. return;
  1961. }
  1962. // Check if it's a Router Solicition message.
  1963. if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
  1964. *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
  1965. *(uint32_t *)(p + 34) == 0 &&
  1966. *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
  1967. LOG(3, s, t, "Got IPv6 RS\n");
  1968. send_ipv6_ra(s, t, &ip);
  1969. return;
  1970. }
  1971. // Check if DhcpV6, IP dst: FF02::1:2, Src Port 0x0222 (546), Dst Port 0x0223 (547)
  1972. if (*(p + 6) == 17 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
  1973. *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
  1974. *(uint16_t *)(p + 34) == 0 && *(p + 36) == 0 && *(p + 37) == 1 && *(p + 38) == 0 && *(p + 39) == 2 &&
  1975. *(p + 40) == 2 && *(p + 41) == 0x22 && *(p + 42) == 2 && *(p + 43) == 0x23)
  1976. {
  1977. dhcpv6_process(s, t, p, l);
  1978. return;
  1979. }
  1980. // Add on the tun header
  1981. p -= 4;
  1982. *(uint32_t *) p = htonl(PKTIPV6);
  1983. l += 4;
  1984. // Are we throttled and a slave?
  1985. if (session[s].tbf_in && !config->cluster_iam_master) {
  1986. // Pass it to the master for handling.
  1987. master_throttle_packet(session[s].tbf_in, p, l);
  1988. return;
  1989. }
  1990. // Are we throttled and a master??
  1991. if (session[s].tbf_in && config->cluster_iam_master) {
  1992. // Actually handle the throttled packets.
  1993. tbf_queue_packet(session[s].tbf_in, p, l);
  1994. return;
  1995. }
  1996. // send to ethernet
  1997. if (tun_write(p, l) < 0)
  1998. {
  1999. STAT(tun_tx_errors);
  2000. LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
  2001. l, strerror(errno), tunfd, p);
  2002. return;
  2003. }
  2004. p += 4;
  2005. l -= 4;
  2006. if (session[s].snoop_ip && session[s].snoop_port)
  2007. {
  2008. // Snooping this session
  2009. snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
  2010. }
  2011. update_sessions_in_stat(s, l);
  2012. eth_tx += l;
  2013. STAT(tun_tx_packets);
  2014. INC_STAT(tun_tx_bytes, l);
  2015. }
  2016. //
  2017. // Helper routine for the TBF filters.
  2018. // Used to send queued data in from the user.
  2019. //
  2020. void send_ipin(sessionidt s, uint8_t *buf, int len)
  2021. {
  2022. LOG_HEX(5, "IP in throttled", buf, len);
  2023. if (write(tunfd, buf, len) < 0)
  2024. {
  2025. STAT(tun_tx_errors);
  2026. LOG(0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
  2027. len, strerror(errno), tunfd, buf);
  2028. return;
  2029. }
  2030. buf += 4;
  2031. len -= 4;
  2032. if (session[s].snoop_ip && session[s].snoop_port)
  2033. {
  2034. // Snooping this session
  2035. snoop_send_packet(buf, len, session[s].snoop_ip, session[s].snoop_port);
  2036. }
  2037. // Increment packet counters
  2038. increment_counter(&session[s].cin, &session[s].cin_wrap, len);
  2039. session[s].cin_delta += len;
  2040. session[s].pin++;
  2041. sess_local[s].cin += len;
  2042. sess_local[s].pin++;
  2043. eth_tx += len;
  2044. STAT(tun_tx_packets);
  2045. INC_STAT(tun_tx_bytes, len - 4);
  2046. }
  2047. // Process CCP messages
  2048. void processccp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
  2049. {
  2050. uint8_t b[MAXETHER];
  2051. uint8_t *q;
  2052. CSTAT(processccp);
  2053. LOG_HEX(5, "CCP", p, l);
  2054. if (session[s].ppp.phase < Network)
  2055. {
  2056. LOG(2, s, t, "CCP %s ignored in %s phase\n", ppp_code(*p), ppp_phase(session[s].ppp.phase));
  2057. return;
  2058. }
  2059. if (l < 1)
  2060. {
  2061. LOG(1, s, t, "Short CCP packet\n");
  2062. STAT(tunnel_rx_errors);
  2063. }
  2064. LOG(4, s, t, "CCP: recv %s\n", ppp_code(*p));
  2065. if (*p == ConfigAck)
  2066. {
  2067. switch (session[s].ppp.ccp)
  2068. {
  2069. case RequestSent:
  2070. initialise_restart_count(s, ccp);
  2071. change_state(s, ccp, AckReceived);
  2072. break;
  2073. case AckReceived:
  2074. case Opened:
  2075. LOG(2, s, t, "CCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.ccp));
  2076. sendccp(s, t);
  2077. change_state(s, ccp, RequestSent);
  2078. break;
  2079. case AckSent:
  2080. LOG(3, s, t, "CCP: Opened\n");
  2081. change_state(s, ccp, Opened);
  2082. break;
  2083. default:
  2084. LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
  2085. }
  2086. }
  2087. else if (*p == ConfigReq)
  2088. {
  2089. if (l < 6) // accept no compression
  2090. *p = ConfigAck;
  2091. else // compression requested--reject
  2092. *p = ConfigRej;
  2093. q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
  2094. if (!q) return;
  2095. switch (session[s].ppp.ccp)
  2096. {
  2097. case Closed:
  2098. q = makeppp(b, sizeof(b), p, 2, s, t, PPPCCP, 0, 0, 0);
  2099. if (!q) return;
  2100. *q = TerminateAck;
  2101. *((uint16_t *) (q + 2)) = htons(l = 4);
  2102. break;
  2103. case Stopped:
  2104. initialise_restart_count(s, ccp);
  2105. sendccp(s, t);
  2106. if (*q == ConfigAck)
  2107. change_state(s, ccp, AckSent);
  2108. else
  2109. change_state(s, ccp, RequestSent);
  2110. break;
  2111. case RequestSent:
  2112. if (*q == ConfigAck)
  2113. change_state(s, ccp, AckSent);
  2114. break;
  2115. case AckReceived:
  2116. if (*q == ConfigAck)
  2117. change_state(s, ccp, Opened);
  2118. break;
  2119. case Opened:
  2120. initialise_restart_count(s, ccp);
  2121. sendccp(s, t);
  2122. /* fallthrough */
  2123. case AckSent:
  2124. if (*q == ConfigAck)
  2125. change_state(s, ccp, AckSent);
  2126. else
  2127. change_state(s, ccp, RequestSent);
  2128. break;
  2129. default:
  2130. LOG(2, s, t, "CCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.ccp));
  2131. return;
  2132. }
  2133. LOG(4, s, t, "CCP: send %s\n", ppp_code(*q));
  2134. tunnelsend(b, l + (q - b), t);
  2135. }
  2136. else if (*p == TerminateReq)
  2137. {
  2138. *p = TerminateAck;
  2139. q = makeppp(b, sizeof(b), p, l, s, t, PPPCCP, 0, 0, 0);
  2140. if (!q) return;
  2141. LOG(3, s, t, "CCP: send %s\n", ppp_code(*q));
  2142. tunnelsend(b, l + (q - b), t);
  2143. change_state(s, ccp, Stopped);
  2144. }
  2145. else if (*p != CodeRej)
  2146. {
  2147. ppp_code_rej(s, t, PPPCCP, "CCP", p, l, b, sizeof(b));
  2148. }
  2149. }
  2150. // send a CHAP challenge
  2151. void sendchap(sessionidt s, tunnelidt t)
  2152. {
  2153. uint8_t b[MAXETHER];
  2154. uint16_t r;
  2155. uint8_t *q;
  2156. CSTAT(sendchap);
  2157. r = radiusnew(s);
  2158. if (!r)
  2159. {
  2160. LOG(1, s, t, "No RADIUS to send challenge\n");
  2161. STAT(tunnel_tx_errors);
  2162. return;
  2163. }
  2164. LOG(1, s, t, "Send CHAP challenge\n");
  2165. radius[r].chap = 1; // CHAP not PAP
  2166. radius[r].id++;
  2167. if (radius[r].state != RADIUSCHAP)
  2168. radius[r].try = 0;
  2169. radius[r].state = RADIUSCHAP;
  2170. radius[r].retry = backoff(radius[r].try++);
  2171. if (radius[r].try > 5)
  2172. {
  2173. sessionshutdown(s, "CHAP timeout.", CDN_ADMIN_DISC, TERM_REAUTHENTICATION_FAILURE);
  2174. STAT(tunnel_tx_errors);
  2175. return ;
  2176. }
  2177. q = makeppp(b, sizeof(b), 0, 0, s, t, PPPCHAP, 0, 0, 0);
  2178. if (!q) return;
  2179. *q = 1; // challenge
  2180. q[1] = radius[r].id; // ID
  2181. q[4] = 16; // value size (size of challenge)
  2182. memcpy(q + 5, radius[r].auth, 16); // challenge
  2183. strcpy((char *) q + 21, config->multi_n_hostname[tunnel[t].indexudp][0]?config->multi_n_hostname[tunnel[t].indexudp]:hostname); // our name
  2184. *(uint16_t *) (q + 2) = htons(strlen(config->multi_n_hostname[tunnel[t].indexudp][0]?config->multi_n_hostname[tunnel[t].indexudp]:hostname) + 21); // length
  2185. tunnelsend(b, strlen(config->multi_n_hostname[tunnel[t].indexudp][0]?config->multi_n_hostname[tunnel[t].indexudp]:hostname) + 21 + (q - b), t); // send it
  2186. }
  2187. // fill in a L2TP message with a PPP frame,
  2188. // returns start of PPP frame
  2189. uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits)
  2190. {
  2191. uint16_t hdr = 0x0002; // L2TP with no options
  2192. uint16_t type = mtype;
  2193. uint8_t *start = b;
  2194. if (t == TUNNEL_ID_PPPOE)
  2195. {
  2196. return pppoe_makeppp(b, size, p, l, s, t, mtype, prio, bid, mp_bits);
  2197. }
  2198. if (size < 16) // Need more space than this!!
  2199. {
  2200. LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
  2201. return NULL;
  2202. }
  2203. if (prio) hdr |= 0x0100; // set priority bit
  2204. *(uint16_t *) (b + 0) = htons(hdr);
  2205. *(uint16_t *) (b + 2) = htons(tunnel[t].far); // tunnel
  2206. *(uint16_t *) (b + 4) = htons(session[s].far); // session
  2207. b += 6;
  2208. // Check whether this session is part of multilink
  2209. if (bid)
  2210. {
  2211. if (bundle[bid].num_of_links > 1)
  2212. type = PPPMP; // Change PPP message type to the PPPMP
  2213. else
  2214. bid = 0;
  2215. }
  2216. if (type == PPPLCP || !(session[s].flags & SESSION_ACFC))
  2217. {
  2218. *(uint16_t *) b = htons(0xFF03); // HDLC header
  2219. b += 2;
  2220. }
  2221. if (type < 0x100 && session[s].flags & SESSION_PFC)
  2222. {
  2223. *b++ = type;
  2224. }
  2225. else
  2226. {
  2227. *(uint16_t *) b = htons(type);
  2228. b += 2;
  2229. }
  2230. if (bid)
  2231. {
  2232. // Set the sequence number and (B)egin (E)nd flags
  2233. if (session[s].mssf)
  2234. {
  2235. // Set the multilink bits
  2236. uint16_t bits_send = mp_bits;
  2237. *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
  2238. b += 2;
  2239. }
  2240. else
  2241. {
  2242. *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
  2243. // Set the multilink bits
  2244. *b = mp_bits;
  2245. b += 4;
  2246. }
  2247. bundle[bid].seq_num_t++;
  2248. // Add the message type if this fragment has the begin bit set
  2249. if (mp_bits & MP_BEGIN)
  2250. {
  2251. //*b++ = mtype; // The next two lines are instead of this
  2252. *(uint16_t *) b = htons(mtype); // Message type
  2253. b += 2;
  2254. }
  2255. }
  2256. if ((b - start) + l > size)
  2257. {
  2258. LOG(2, s, t, "makeppp would overflow buffer (size=%d, header+payload=%td)\n", size, (b - start) + l);
  2259. return NULL;
  2260. }
  2261. // Copy the payload
  2262. if (p && l)
  2263. memcpy(b, p, l);
  2264. return b;
  2265. }
  2266. // fill in a L2TP message with a PPP frame,
  2267. // returns start of PPP frame
  2268. //(note: THIS ROUTINE CAN WRITES TO p[-28]).
  2269. uint8_t *opt_makeppp(uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits)
  2270. {
  2271. uint16_t hdr = 0x0002; // L2TP with no options
  2272. uint16_t type = mtype;
  2273. uint8_t *b = p;
  2274. if (t == TUNNEL_ID_PPPOE)
  2275. {
  2276. return opt_pppoe_makeppp(p, l, s, t, mtype, prio, bid, mp_bits);
  2277. }
  2278. // Check whether this session is part of multilink
  2279. if (bid)
  2280. {
  2281. if (bundle[bid].num_of_links > 1)
  2282. type = PPPMP; // Change PPP message type to the PPPMP
  2283. else
  2284. bid = 0;
  2285. }
  2286. if (bid)
  2287. {
  2288. // Add the message type if this fragment has the begin bit set
  2289. if (mp_bits & MP_BEGIN)
  2290. {
  2291. b -= 2;
  2292. *(uint16_t *) b = htons(mtype); // Message type
  2293. }
  2294. // Set the sequence number and (B)egin (E)nd flags
  2295. if (session[s].mssf)
  2296. {
  2297. // Set the multilink bits
  2298. uint16_t bits_send = mp_bits;
  2299. b -= 2;
  2300. *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
  2301. }
  2302. else
  2303. {
  2304. b -= 4;
  2305. *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
  2306. // Set the multilink bits
  2307. *b = mp_bits;
  2308. }
  2309. bundle[bid].seq_num_t++;
  2310. }
  2311. if (type < 0x100 && session[s].flags & SESSION_PFC)
  2312. {
  2313. b--;
  2314. *b = type;
  2315. }
  2316. else
  2317. {
  2318. b -= 2;
  2319. *(uint16_t *) b = htons(type);
  2320. }
  2321. if (type == PPPLCP || !(session[s].flags & SESSION_ACFC))
  2322. {
  2323. b -= 2;
  2324. *(uint16_t *) b = htons(0xFF03); // HDLC header
  2325. }
  2326. if (prio) hdr |= 0x0100; // set priority bit
  2327. b -= 2;
  2328. *(uint16_t *) b = htons(session[s].far); // session
  2329. b -= 2;
  2330. *(uint16_t *) b = htons(tunnel[t].far); // tunnel
  2331. b -= 2;
  2332. *(uint16_t *) b = htons(hdr);
  2333. return b;
  2334. }
  2335. static int add_lcp_auth(uint8_t *b, int size, int authtype)
  2336. {
  2337. int len = 0;
  2338. if ((authtype == AUTHCHAP && size < 5) || size < 4)
  2339. return 0;
  2340. *b++ = 3; // Authentication-Protocol
  2341. if (authtype == AUTHCHAP)
  2342. {
  2343. len = *b++ = 5; // length
  2344. *(uint16_t *) b = htons(PPPCHAP); b += 2;
  2345. *b++ = 5; // MD5
  2346. }
  2347. else if (authtype == AUTHPAP)
  2348. {
  2349. len = *b++ = 4; // length
  2350. *(uint16_t *) b = htons(PPPPAP); b += 2;
  2351. }
  2352. else
  2353. {
  2354. LOG(0, 0, 0, "add_lcp_auth called with unsupported auth type %d\n", authtype);
  2355. }
  2356. return len;
  2357. }
  2358. // Send LCP ConfigReq for MRU, authentication type and magic no
  2359. void sendlcp(sessionidt s, tunnelidt t)
  2360. {
  2361. uint8_t b[500], *q, *l;
  2362. int authtype = sess_local[s].lcp_authtype;
  2363. if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPLCP, 0, 0, 0)))
  2364. return;
  2365. LOG(3, s, t, "LCP: send ConfigReq%s%s%s including MP options\n",
  2366. authtype ? " (" : "",
  2367. authtype ? (authtype == AUTHCHAP ? "CHAP" : "PAP") : "",
  2368. authtype ? ")" : "");
  2369. l = q;
  2370. *l++ = ConfigReq;
  2371. *l++ = ++sess_local[s].lcp_ident; // ID
  2372. l += 2; //Save space for length
  2373. if (sess_local[s].ppp_mru)
  2374. {
  2375. *l++ = 1; *l++ = 4; // Maximum-Receive-Unit (length 4)
  2376. *(uint16_t *) l = htons(sess_local[s].ppp_mru); l += 2;
  2377. }
  2378. if (authtype)
  2379. l += add_lcp_auth(l, sizeof(b) - (l - b), authtype);
  2380. if (session[s].magic)
  2381. {
  2382. *l++ = 5; *l++ = 6; // Magic-Number (length 6)
  2383. *(uint32_t *) l = htonl(session[s].magic);
  2384. l += 4;
  2385. }
  2386. if (sess_local[s].mp_mrru)
  2387. {
  2388. *l++ = 17; *l++ = 4; // Multilink Max-Receive-Reconstructed-Unit (length 4)
  2389. *(uint16_t *) l = htons(sess_local[s].mp_mrru); l += 2;
  2390. }
  2391. if (sess_local[s].mp_epdis)
  2392. {
  2393. *l++ = 19; *l++ = 7; // Multilink Endpoint Discriminator (length 7)
  2394. *l++ = IPADDR; // Endpoint Discriminator class
  2395. *(uint32_t *) l = htonl(sess_local[s].mp_epdis);
  2396. l += 4;
  2397. }
  2398. *(uint16_t *)(q + 2) = htons(l - q); // Length
  2399. LOG_HEX(5, "PPPLCP", q, l - q);
  2400. if (config->debug > 3) dumplcp(q, l - q);
  2401. tunnelsend(b, (l - b), t);
  2402. restart_timer(s, lcp);
  2403. }
  2404. // Send CCP request for no compression
  2405. void sendccp(sessionidt s, tunnelidt t)
  2406. {
  2407. uint8_t b[500], *q;
  2408. if (!(q = makeppp(b, sizeof(b), NULL, 0, s, t, PPPCCP, 0, 0, 0)))
  2409. return;
  2410. LOG(3, s, t, "CCP: send ConfigReq (no compression)\n");
  2411. *q = ConfigReq;
  2412. *(q + 1) = ++sess_local[s].lcp_ident; // ID
  2413. *(uint16_t *)(q + 2) = htons(4); // Length
  2414. LOG_HEX(5, "PPPCCP", q, 4);
  2415. tunnelsend(b, (q - b) + 4 , t);
  2416. restart_timer(s, ccp);
  2417. }
  2418. // Reject unknown/unconfigured protocols
  2419. void protoreject(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l, uint16_t proto)
  2420. {
  2421. uint8_t buf[MAXETHER];
  2422. uint8_t *q;
  2423. int mru = session[s].mru;
  2424. if (mru < MINMTU) mru = MINMTU;
  2425. if (mru > sizeof(buf)) mru = sizeof(buf);
  2426. l += 6;
  2427. if (l > mru) l = mru;
  2428. q = makeppp(buf, sizeof(buf), 0, 0, s, t, PPPLCP, 0, 0, 0);
  2429. if (!q) return;
  2430. *q = ProtocolRej;
  2431. *(q + 1) = ++sess_local[s].lcp_ident;
  2432. *(uint16_t *)(q + 2) = htons(l);
  2433. *(uint16_t *)(q + 4) = htons(proto);
  2434. memcpy(q + 6, p, l - 6);
  2435. if (proto == PPPIPV6CP)
  2436. LOG(3, s, t, "LCP: send ProtocolRej (IPV6CP: not configured)\n");
  2437. else
  2438. LOG(2, s, t, "LCP: sent ProtocolRej (0x%04X: unsupported)\n", proto);
  2439. tunnelsend(buf, l + (q - buf), t);
  2440. }