ppp.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519
  1. /*
  2. * ppp.c - STREAMS multiplexing pseudo-device driver for PPP.
  3. *
  4. * Copyright (c) 1994 Paul Mackerras. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. The name(s) of the authors of this software must not be used to
  19. * endorse or promote products derived from this software without
  20. * prior written permission.
  21. *
  22. * 4. Redistributions of any form whatsoever must retain the following
  23. * acknowledgment:
  24. * "This product includes software developed by Paul Mackerras
  25. * <paulus@samba.org>".
  26. *
  27. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  28. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  29. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  30. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  31. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  32. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  33. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. * $Id: ppp.c,v 1.4 2005/06/27 00:59:57 carlsonj Exp $
  36. */
  37. /*
  38. * This file is used under Solaris 2, SVR4, SunOS 4, and Digital UNIX.
  39. */
  40. #include <sys/types.h>
  41. #include <sys/param.h>
  42. #include <sys/stat.h>
  43. #include <sys/stream.h>
  44. #include <sys/stropts.h>
  45. #include <sys/errno.h>
  46. #ifdef __osf__
  47. #include <sys/ioctl.h>
  48. #include <sys/cmn_err.h>
  49. #define queclass(mp) ((mp)->b_band & QPCTL)
  50. #else
  51. #include <sys/ioccom.h>
  52. #endif
  53. #include <sys/time.h>
  54. #ifdef SVR4
  55. #include <sys/cmn_err.h>
  56. #include <sys/conf.h>
  57. #include <sys/dlpi.h>
  58. #include <sys/ddi.h>
  59. #ifdef SOL2
  60. #include <sys/ksynch.h>
  61. #include <sys/kstat.h>
  62. #include <sys/sunddi.h>
  63. #include <sys/ethernet.h>
  64. #else
  65. #include <sys/socket.h>
  66. #include <sys/sockio.h>
  67. #include <net/if.h>
  68. #include <netinet/in.h>
  69. #endif /* SOL2 */
  70. #else /* not SVR4 */
  71. #include <sys/user.h>
  72. #endif /* SVR4 */
  73. #include <net/ppp_defs.h>
  74. #include <net/pppio.h>
  75. #include "ppp_mod.h"
  76. /*
  77. * Modifications marked with #ifdef PRIOQ are for priority queueing of
  78. * interactive traffic, and are due to Marko Zec <zec@japa.tel.fer.hr>.
  79. */
  80. #ifdef PRIOQ
  81. #endif /* PRIOQ */
  82. #include <netinet/in.h> /* leave this outside of PRIOQ for htons */
  83. #ifdef __STDC__
  84. #define __P(x) x
  85. #else
  86. #define __P(x) ()
  87. #endif
  88. /*
  89. * The IP module may use this SAP value for IP packets.
  90. */
  91. #ifndef ETHERTYPE_IP
  92. #define ETHERTYPE_IP 0x800
  93. #endif
  94. #if !defined(ETHERTYPE_IPV6)
  95. #define ETHERTYPE_IPV6 0x86dd
  96. #endif /* !defined(ETHERTYPE_IPV6) */
  97. #if !defined(ETHERTYPE_ALLSAP) && defined(SOL2)
  98. #define ETHERTYPE_ALLSAP 0
  99. #endif /* !defined(ETHERTYPE_ALLSAP) && defined(SOL2) */
  100. #if !defined(PPP_ALLSAP) && defined(SOL2)
  101. #define PPP_ALLSAP PPP_ALLSTATIONS
  102. #endif /* !defined(PPP_ALLSAP) && defined(SOL2) */
  103. extern time_t time;
  104. #ifdef SOL2
  105. /*
  106. * We use this reader-writer lock to ensure that the lower streams
  107. * stay connected to the upper streams while the lower-side put and
  108. * service procedures are running. Essentially it is an existence
  109. * lock for the upper stream associated with each lower stream.
  110. */
  111. krwlock_t ppp_lower_lock;
  112. #define LOCK_LOWER_W rw_enter(&ppp_lower_lock, RW_WRITER)
  113. #define LOCK_LOWER_R rw_enter(&ppp_lower_lock, RW_READER)
  114. #define TRYLOCK_LOWER_R rw_tryenter(&ppp_lower_lock, RW_READER)
  115. #define UNLOCK_LOWER rw_exit(&ppp_lower_lock)
  116. #define MT_ENTER(x) mutex_enter(x)
  117. #define MT_EXIT(x) mutex_exit(x)
  118. /*
  119. * Notes on multithreaded implementation for Solaris 2:
  120. *
  121. * We use an inner perimeter around each queue pair and an outer
  122. * perimeter around the whole driver. The inner perimeter is
  123. * entered exclusively for all entry points (open, close, put,
  124. * service). The outer perimeter is entered exclusively for open
  125. * and close and shared for put and service. This is all done for
  126. * us by the streams framework.
  127. *
  128. * I used to think that the perimeters were entered for the lower
  129. * streams' put and service routines as well as for the upper streams'.
  130. * Because of problems experienced by people, and after reading the
  131. * documentation more closely, I now don't think that is true. So we
  132. * now use ppp_lower_lock to give us an existence guarantee on the
  133. * upper stream controlling each lower stream.
  134. *
  135. * Shared entry to the outer perimeter protects the existence of all
  136. * the upper streams and their upperstr_t structures, and guarantees
  137. * that the following fields of any upperstr_t won't change:
  138. * nextmn, next, nextppa. It guarantees that the lowerq field of an
  139. * upperstr_t won't go from non-zero to zero, that the global `ppas'
  140. * won't change and that the no lower stream will get unlinked.
  141. *
  142. * Shared (reader) access to ppa_lower_lock guarantees that no lower
  143. * stream will be unlinked and that the lowerq field of all upperstr_t
  144. * structures won't change.
  145. */
  146. #else /* SOL2 */
  147. #define LOCK_LOWER_W 0
  148. #define LOCK_LOWER_R 0
  149. #define TRYLOCK_LOWER_R 1
  150. #define UNLOCK_LOWER 0
  151. #define MT_ENTER(x) 0
  152. #define MT_EXIT(x) 0
  153. #endif /* SOL2 */
  154. /*
  155. * Private information; one per upper stream.
  156. */
  157. typedef struct upperstr {
  158. minor_t mn; /* minor device number */
  159. struct upperstr *nextmn; /* next minor device */
  160. queue_t *q; /* read q associated with this upper stream */
  161. int flags; /* flag bits, see below */
  162. int state; /* current DLPI state */
  163. int sap; /* service access point */
  164. int req_sap; /* which SAP the DLPI client requested */
  165. struct upperstr *ppa; /* control stream for our ppa */
  166. struct upperstr *next; /* next stream for this ppa */
  167. uint ioc_id; /* last ioctl ID for this stream */
  168. enum NPmode npmode; /* what to do with packets on this SAP */
  169. unsigned char rblocked; /* flow control has blocked upper read strm */
  170. /* N.B. rblocked is only changed by control stream's put/srv procs */
  171. /*
  172. * There is exactly one control stream for each PPA.
  173. * The following fields are only used for control streams.
  174. */
  175. int ppa_id;
  176. queue_t *lowerq; /* write queue attached below this PPA */
  177. struct upperstr *nextppa; /* next control stream */
  178. int mru;
  179. int mtu;
  180. struct pppstat stats; /* statistics */
  181. time_t last_sent; /* time last NP packet sent */
  182. time_t last_recv; /* time last NP packet rcvd */
  183. #ifdef SOL2
  184. kmutex_t stats_lock; /* lock for stats updates */
  185. kstat_t *kstats; /* stats for netstat */
  186. #endif /* SOL2 */
  187. #ifdef LACHTCP
  188. int ifflags;
  189. char ifname[IFNAMSIZ];
  190. struct ifstats ifstats;
  191. #endif /* LACHTCP */
  192. } upperstr_t;
  193. /* Values for flags */
  194. #define US_PRIV 1 /* stream was opened by superuser */
  195. #define US_CONTROL 2 /* stream is a control stream */
  196. #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
  197. #define US_LASTMOD 8 /* no PPP modules below us */
  198. #define US_DBGLOG 0x10 /* log various occurrences */
  199. #define US_RBLOCKED 0x20 /* flow ctrl has blocked upper read stream */
  200. #if defined(SOL2)
  201. #if DL_CURRENT_VERSION >= 2
  202. #define US_PROMISC 0x40 /* stream is promiscuous */
  203. #endif /* DL_CURRENT_VERSION >= 2 */
  204. #define US_RAWDATA 0x80 /* raw M_DATA, no DLPI header */
  205. #endif /* defined(SOL2) */
  206. #ifdef PRIOQ
  207. static u_char max_band=0;
  208. static u_char def_band=0;
  209. #define IPPORT_DEFAULT 65535
  210. /*
  211. * Port priority table
  212. * Highest priority ports are listed first, lowest are listed last.
  213. * ICMP & packets using unlisted ports will be treated as "default".
  214. * If IPPORT_DEFAULT is not listed here, "default" packets will be
  215. * assigned lowest priority.
  216. * Each line should be terminated with "0".
  217. * Line containing only "0" marks the end of the list.
  218. */
  219. static u_short prioq_table[]= {
  220. 113, 53, 0,
  221. 22, 23, 513, 517, 518, 0,
  222. 514, 21, 79, 111, 0,
  223. 25, 109, 110, 0,
  224. IPPORT_DEFAULT, 0,
  225. 20, 70, 80, 8001, 8008, 8080, 0, /* 8001,8008,8080 - common proxy ports */
  226. 0 };
  227. #endif /* PRIOQ */
  228. static upperstr_t *minor_devs = NULL;
  229. static upperstr_t *ppas = NULL;
  230. #ifdef SVR4
  231. static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
  232. static int pppclose __P((queue_t *, int, cred_t *));
  233. #else
  234. static int pppopen __P((queue_t *, int, int, int));
  235. static int pppclose __P((queue_t *, int));
  236. #endif /* SVR4 */
  237. static int pppurput __P((queue_t *, mblk_t *));
  238. static int pppuwput __P((queue_t *, mblk_t *));
  239. static int pppursrv __P((queue_t *));
  240. static int pppuwsrv __P((queue_t *));
  241. static int ppplrput __P((queue_t *, mblk_t *));
  242. static int ppplwput __P((queue_t *, mblk_t *));
  243. static int ppplrsrv __P((queue_t *));
  244. static int ppplwsrv __P((queue_t *));
  245. #ifndef NO_DLPI
  246. static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
  247. static void dlpi_error __P((queue_t *, upperstr_t *, int, int, int));
  248. static void dlpi_ok __P((queue_t *, int));
  249. #endif
  250. static int send_data __P((mblk_t *, upperstr_t *));
  251. static void new_ppa __P((queue_t *, mblk_t *));
  252. static void attach_ppa __P((queue_t *, mblk_t *));
  253. #ifndef NO_DLPI
  254. static void detach_ppa __P((queue_t *, mblk_t *));
  255. #endif
  256. static void detach_lower __P((queue_t *, mblk_t *));
  257. static void debug_dump __P((queue_t *, mblk_t *));
  258. static upperstr_t *find_dest __P((upperstr_t *, int));
  259. #if defined(SOL2)
  260. static upperstr_t *find_promisc __P((upperstr_t *, int));
  261. static mblk_t *prepend_ether __P((upperstr_t *, mblk_t *, int));
  262. static mblk_t *prepend_udind __P((upperstr_t *, mblk_t *, int));
  263. static void promisc_sendup __P((upperstr_t *, mblk_t *, int, int));
  264. #endif /* defined(SOL2) */
  265. static int putctl2 __P((queue_t *, int, int, int));
  266. static int putctl4 __P((queue_t *, int, int, int));
  267. static int pass_packet __P((upperstr_t *ppa, mblk_t *mp, int outbound));
  268. #ifdef FILTER_PACKETS
  269. static int ip_hard_filter __P((upperstr_t *ppa, mblk_t *mp, int outbound));
  270. #endif /* FILTER_PACKETS */
  271. #define PPP_ID 0xb1a6
  272. static struct module_info ppp_info = {
  273. #ifdef PRIOQ
  274. PPP_ID, "ppp", 0, 512, 512, 384
  275. #else
  276. PPP_ID, "ppp", 0, 512, 512, 128
  277. #endif /* PRIOQ */
  278. };
  279. static struct qinit pppurint = {
  280. pppurput, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
  281. };
  282. static struct qinit pppuwint = {
  283. pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
  284. };
  285. static struct qinit ppplrint = {
  286. ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
  287. };
  288. static struct qinit ppplwint = {
  289. ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
  290. };
  291. #ifdef LACHTCP
  292. extern struct ifstats *ifstats;
  293. int pppdevflag = 0;
  294. #endif
  295. struct streamtab pppinfo = {
  296. &pppurint, &pppuwint,
  297. &ppplrint, &ppplwint
  298. };
  299. int ppp_count;
  300. /*
  301. * How we maintain statistics.
  302. */
  303. #ifdef SOL2
  304. #define INCR_IPACKETS(ppa) \
  305. if (ppa->kstats != 0) { \
  306. KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
  307. }
  308. #define INCR_IERRORS(ppa) \
  309. if (ppa->kstats != 0) { \
  310. KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
  311. }
  312. #define INCR_OPACKETS(ppa) \
  313. if (ppa->kstats != 0) { \
  314. KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
  315. }
  316. #define INCR_OERRORS(ppa) \
  317. if (ppa->kstats != 0) { \
  318. KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
  319. }
  320. #endif
  321. #ifdef LACHTCP
  322. #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
  323. #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
  324. #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
  325. #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
  326. #endif
  327. /*
  328. * STREAMS driver entry points.
  329. */
  330. static int
  331. #ifdef SVR4
  332. pppopen(q, devp, oflag, sflag, credp)
  333. queue_t *q;
  334. dev_t *devp;
  335. int oflag, sflag;
  336. cred_t *credp;
  337. #else
  338. pppopen(q, dev, oflag, sflag)
  339. queue_t *q;
  340. int dev; /* really dev_t */
  341. int oflag, sflag;
  342. #endif
  343. {
  344. upperstr_t *up;
  345. upperstr_t **prevp;
  346. minor_t mn;
  347. #ifdef PRIOQ
  348. u_short *ptr;
  349. u_char new_band;
  350. #endif /* PRIOQ */
  351. if (q->q_ptr)
  352. DRV_OPEN_OK(dev); /* device is already open */
  353. #ifdef PRIOQ
  354. /* Calculate max_bband & def_band from definitions in prioq.h
  355. This colud be done at some more approtiate time (less often)
  356. but this way it works well so I'll just leave it here */
  357. max_band = 1;
  358. def_band = 0;
  359. ptr = prioq_table;
  360. while (*ptr) {
  361. new_band = 1;
  362. while (*ptr)
  363. if (*ptr++ == IPPORT_DEFAULT) {
  364. new_band = 0;
  365. def_band = max_band;
  366. }
  367. max_band += new_band;
  368. ptr++;
  369. }
  370. if (def_band)
  371. def_band = max_band - def_band;
  372. --max_band;
  373. #endif /* PRIOQ */
  374. if (sflag == CLONEOPEN) {
  375. mn = 0;
  376. for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
  377. if (up->mn != mn)
  378. break;
  379. ++mn;
  380. }
  381. } else {
  382. #ifdef SVR4
  383. mn = getminor(*devp);
  384. #else
  385. mn = minor(dev);
  386. #endif
  387. for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
  388. if (up->mn >= mn)
  389. break;
  390. }
  391. if (up->mn == mn) {
  392. /* this can't happen */
  393. q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
  394. DRV_OPEN_OK(dev);
  395. }
  396. }
  397. /*
  398. * Construct a new minor node.
  399. */
  400. up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
  401. bzero((caddr_t) up, sizeof(upperstr_t));
  402. if (up == 0) {
  403. DPRINT("pppopen: out of kernel memory\n");
  404. OPEN_ERROR(ENXIO);
  405. }
  406. up->nextmn = *prevp;
  407. *prevp = up;
  408. up->mn = mn;
  409. #ifdef SVR4
  410. *devp = makedevice(getmajor(*devp), mn);
  411. #endif
  412. up->q = q;
  413. if (NOTSUSER() == 0)
  414. up->flags |= US_PRIV;
  415. #ifndef NO_DLPI
  416. up->state = DL_UNATTACHED;
  417. #endif
  418. #ifdef LACHTCP
  419. up->ifflags = IFF_UP | IFF_POINTOPOINT;
  420. #endif
  421. up->sap = -1;
  422. up->last_sent = up->last_recv = time;
  423. up->npmode = NPMODE_DROP;
  424. q->q_ptr = (caddr_t) up;
  425. WR(q)->q_ptr = (caddr_t) up;
  426. noenable(WR(q));
  427. #ifdef SOL2
  428. mutex_init(&up->stats_lock, NULL, MUTEX_DRIVER, NULL);
  429. #endif
  430. ++ppp_count;
  431. qprocson(q);
  432. DRV_OPEN_OK(makedev(major(dev), mn));
  433. }
  434. static int
  435. #ifdef SVR4
  436. pppclose(q, flag, credp)
  437. queue_t *q;
  438. int flag;
  439. cred_t *credp;
  440. #else
  441. pppclose(q, flag)
  442. queue_t *q;
  443. int flag;
  444. #endif
  445. {
  446. upperstr_t *up, **upp;
  447. upperstr_t *as, *asnext;
  448. upperstr_t **prevp;
  449. qprocsoff(q);
  450. up = (upperstr_t *) q->q_ptr;
  451. if (up == 0) {
  452. DPRINT("pppclose: q_ptr = 0\n");
  453. return 0;
  454. }
  455. if (up->flags & US_DBGLOG)
  456. DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
  457. if (up->flags & US_CONTROL) {
  458. #ifdef LACHTCP
  459. struct ifstats *ifp, *pifp;
  460. #endif
  461. if (up->lowerq != 0) {
  462. /* Gack! the lower stream should have be unlinked earlier! */
  463. DPRINT1("ppp%d: lower stream still connected on close?\n",
  464. up->mn);
  465. LOCK_LOWER_W;
  466. up->lowerq->q_ptr = 0;
  467. RD(up->lowerq)->q_ptr = 0;
  468. up->lowerq = 0;
  469. UNLOCK_LOWER;
  470. }
  471. /*
  472. * This stream represents a PPA:
  473. * For all streams attached to the PPA, clear their
  474. * references to this PPA.
  475. * Then remove this PPA from the list of PPAs.
  476. */
  477. for (as = up->next; as != 0; as = asnext) {
  478. asnext = as->next;
  479. as->next = 0;
  480. as->ppa = 0;
  481. if (as->flags & US_BLOCKED) {
  482. as->flags &= ~US_BLOCKED;
  483. flushq(WR(as->q), FLUSHDATA);
  484. }
  485. }
  486. for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
  487. if (*upp == up) {
  488. *upp = up->nextppa;
  489. break;
  490. }
  491. #ifdef LACHTCP
  492. /* Remove the statistics from the active list. */
  493. for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
  494. if (ifp == &up->ifstats) {
  495. if (pifp)
  496. pifp->ifs_next = ifp->ifs_next;
  497. else
  498. ifstats = ifp->ifs_next;
  499. break;
  500. }
  501. pifp = ifp;
  502. }
  503. #endif
  504. } else {
  505. /*
  506. * If this stream is attached to a PPA,
  507. * remove it from the PPA's list.
  508. */
  509. if ((as = up->ppa) != 0) {
  510. for (; as->next != 0; as = as->next)
  511. if (as->next == up) {
  512. as->next = up->next;
  513. break;
  514. }
  515. }
  516. }
  517. #ifdef SOL2
  518. if (up->kstats)
  519. kstat_delete(up->kstats);
  520. mutex_destroy(&up->stats_lock);
  521. #endif
  522. q->q_ptr = NULL;
  523. WR(q)->q_ptr = NULL;
  524. for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
  525. if (*prevp == up) {
  526. *prevp = up->nextmn;
  527. break;
  528. }
  529. }
  530. FREE(up, sizeof(upperstr_t));
  531. --ppp_count;
  532. return 0;
  533. }
  534. /*
  535. * A message from on high. We do one of three things:
  536. * - qreply()
  537. * - put the message on the lower write stream
  538. * - queue it for our service routine
  539. */
  540. static int
  541. pppuwput(q, mp)
  542. queue_t *q;
  543. mblk_t *mp;
  544. {
  545. upperstr_t *us, *ppa, *nps;
  546. struct iocblk *iop;
  547. struct linkblk *lb;
  548. #ifdef LACHTCP
  549. struct ifreq *ifr;
  550. int i;
  551. #endif
  552. queue_t *lq;
  553. int error, n, sap;
  554. mblk_t *mq;
  555. struct ppp_idle *pip;
  556. #ifdef PRIOQ
  557. queue_t *tlq;
  558. #endif /* PRIOQ */
  559. #ifdef NO_DLPI
  560. upperstr_t *os;
  561. #endif
  562. us = (upperstr_t *) q->q_ptr;
  563. if (us == 0) {
  564. DPRINT("pppuwput: q_ptr = 0!\n");
  565. return 0;
  566. }
  567. if (mp == 0) {
  568. DPRINT1("pppuwput/%d: mp = 0!\n", us->mn);
  569. return 0;
  570. }
  571. if (mp->b_datap == 0) {
  572. DPRINT1("pppuwput/%d: mp->b_datap = 0!\n", us->mn);
  573. return 0;
  574. }
  575. switch (mp->b_datap->db_type) {
  576. #ifndef NO_DLPI
  577. case M_PCPROTO:
  578. case M_PROTO:
  579. dlpi_request(q, mp, us);
  580. break;
  581. #endif /* NO_DLPI */
  582. case M_DATA:
  583. if (us->flags & US_DBGLOG)
  584. DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
  585. us->mn, msgdsize(mp), us->flags);
  586. if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
  587. #ifndef NO_DLPI
  588. || (us->flags & US_CONTROL) == 0
  589. #endif /* NO_DLPI */
  590. ) {
  591. DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
  592. freemsg(mp);
  593. break;
  594. }
  595. #ifdef NO_DLPI
  596. /* pass_packet frees the packet on returning 0 */
  597. if ((us->flags & US_CONTROL) == 0 && !pass_packet(us, mp, 1))
  598. break;
  599. #endif
  600. if (!send_data(mp, us) && !putq(q, mp))
  601. freemsg(mp);
  602. break;
  603. case M_IOCTL:
  604. iop = (struct iocblk *) mp->b_rptr;
  605. error = EINVAL;
  606. if (us->flags & US_DBGLOG)
  607. DPRINT3("ppp/%d: ioctl %x count=%d\n",
  608. us->mn, iop->ioc_cmd, iop->ioc_count);
  609. switch (iop->ioc_cmd) {
  610. #if defined(SOL2)
  611. case DLIOCRAW: /* raw M_DATA mode */
  612. us->flags |= US_RAWDATA;
  613. error = 0;
  614. break;
  615. #endif /* defined(SOL2) */
  616. case I_LINK:
  617. if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
  618. break;
  619. if (mp->b_cont == 0) {
  620. DPRINT1("pppuwput/%d: ioctl I_LINK b_cont = 0!\n", us->mn);
  621. break;
  622. }
  623. lb = (struct linkblk *) mp->b_cont->b_rptr;
  624. lq = lb->l_qbot;
  625. if (lq == 0) {
  626. DPRINT1("pppuwput/%d: ioctl I_LINK l_qbot = 0!\n", us->mn);
  627. break;
  628. }
  629. LOCK_LOWER_W;
  630. us->lowerq = lq;
  631. lq->q_ptr = (caddr_t) q;
  632. RD(lq)->q_ptr = (caddr_t) us->q;
  633. UNLOCK_LOWER;
  634. iop->ioc_count = 0;
  635. error = 0;
  636. us->flags &= ~US_LASTMOD;
  637. /* Unblock upper streams which now feed this lower stream. */
  638. qenable(q);
  639. /* Send useful information down to the modules which
  640. are now linked below us. */
  641. putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
  642. putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
  643. putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
  644. #ifdef PRIOQ
  645. /* Lower tty driver's queue hiwat/lowat from default 4096/128
  646. to 256/128 since we don't want queueing of data on
  647. output to physical device */
  648. freezestr(lq);
  649. for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
  650. ;
  651. strqset(tlq, QHIWAT, 0, 256);
  652. strqset(tlq, QLOWAT, 0, 128);
  653. unfreezestr(lq);
  654. #endif /* PRIOQ */
  655. break;
  656. case I_UNLINK:
  657. if (mp->b_cont == 0) {
  658. DPRINT1("pppuwput/%d: ioctl I_UNLINK b_cont = 0!\n", us->mn);
  659. break;
  660. }
  661. lb = (struct linkblk *) mp->b_cont->b_rptr;
  662. #if DEBUG
  663. if (us->lowerq != lb->l_qbot) {
  664. DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
  665. us->lowerq, lb->l_qbot);
  666. break;
  667. }
  668. #endif
  669. iop->ioc_count = 0;
  670. qwriter(q, mp, detach_lower, PERIM_OUTER);
  671. /* mp is now gone */
  672. error = -1;
  673. break;
  674. case PPPIO_NEWPPA:
  675. if (us->flags & US_CONTROL)
  676. break;
  677. if ((us->flags & US_PRIV) == 0) {
  678. error = EPERM;
  679. break;
  680. }
  681. /* Arrange to return an int */
  682. if ((mq = mp->b_cont) == 0
  683. || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
  684. mq = allocb(sizeof(int), BPRI_HI);
  685. if (mq == 0) {
  686. error = ENOSR;
  687. break;
  688. }
  689. if (mp->b_cont != 0)
  690. freemsg(mp->b_cont);
  691. mp->b_cont = mq;
  692. mq->b_cont = 0;
  693. }
  694. iop->ioc_count = sizeof(int);
  695. mq->b_wptr = mq->b_rptr + sizeof(int);
  696. qwriter(q, mp, new_ppa, PERIM_OUTER);
  697. /* mp is now gone */
  698. error = -1;
  699. break;
  700. case PPPIO_ATTACH:
  701. /* like dlpi_attach, for programs which can't write to
  702. the stream (like pppstats) */
  703. if (iop->ioc_count != sizeof(int) || us->ppa != 0)
  704. break;
  705. if (mp->b_cont == 0) {
  706. DPRINT1("pppuwput/%d: ioctl PPPIO_ATTACH b_cont = 0!\n", us->mn);
  707. break;
  708. }
  709. n = *(int *)mp->b_cont->b_rptr;
  710. for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
  711. if (ppa->ppa_id == n)
  712. break;
  713. if (ppa == 0)
  714. break;
  715. us->ppa = ppa;
  716. iop->ioc_count = 0;
  717. qwriter(q, mp, attach_ppa, PERIM_OUTER);
  718. /* mp is now gone */
  719. error = -1;
  720. break;
  721. #ifdef NO_DLPI
  722. case PPPIO_BIND:
  723. /* Attach to a given SAP. */
  724. if (iop->ioc_count != sizeof(int) || us->ppa == 0)
  725. break;
  726. if (mp->b_cont == 0) {
  727. DPRINT1("pppuwput/%d: ioctl PPPIO_BIND b_cont = 0!\n", us->mn);
  728. break;
  729. }
  730. n = *(int *)mp->b_cont->b_rptr;
  731. /* n must be a valid PPP network protocol number. */
  732. if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
  733. break;
  734. /* check that no other stream is bound to this sap already. */
  735. for (os = us->ppa; os != 0; os = os->next)
  736. if (os->sap == n)
  737. break;
  738. if (os != 0)
  739. break;
  740. us->sap = n;
  741. iop->ioc_count = 0;
  742. error = 0;
  743. break;
  744. #endif /* NO_DLPI */
  745. case PPPIO_MRU:
  746. if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
  747. break;
  748. if (mp->b_cont == 0) {
  749. DPRINT1("pppuwput/%d: ioctl PPPIO_MRU b_cont = 0!\n", us->mn);
  750. break;
  751. }
  752. n = *(int *)mp->b_cont->b_rptr;
  753. if (n <= 0 || n > PPP_MAXMRU)
  754. break;
  755. if (n < PPP_MRU)
  756. n = PPP_MRU;
  757. us->mru = n;
  758. if (us->lowerq)
  759. putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
  760. error = 0;
  761. iop->ioc_count = 0;
  762. break;
  763. case PPPIO_MTU:
  764. if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
  765. break;
  766. if (mp->b_cont == 0) {
  767. DPRINT1("pppuwput/%d: ioctl PPPIO_MTU b_cont = 0!\n", us->mn);
  768. break;
  769. }
  770. n = *(int *)mp->b_cont->b_rptr;
  771. if (n <= 0 || n > PPP_MAXMTU)
  772. break;
  773. us->mtu = n;
  774. #ifdef LACHTCP
  775. /* The MTU reported in netstat, not used as IP max packet size! */
  776. us->ifstats.ifs_mtu = n;
  777. #endif
  778. if (us->lowerq)
  779. putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
  780. error = 0;
  781. iop->ioc_count = 0;
  782. break;
  783. case PPPIO_LASTMOD:
  784. us->flags |= US_LASTMOD;
  785. error = 0;
  786. break;
  787. case PPPIO_DEBUG:
  788. if (iop->ioc_count != sizeof(int))
  789. break;
  790. if (mp->b_cont == 0) {
  791. DPRINT1("pppuwput/%d: ioctl PPPIO_DEBUG b_cont = 0!\n", us->mn);
  792. break;
  793. }
  794. n = *(int *)mp->b_cont->b_rptr;
  795. if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
  796. qwriter(q, mp, debug_dump, PERIM_OUTER);
  797. /* mp is now gone */
  798. error = -1;
  799. } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
  800. DPRINT1("ppp/%d: debug log enabled\n", us->mn);
  801. us->flags |= US_DBGLOG;
  802. iop->ioc_count = 0;
  803. error = 0;
  804. } else {
  805. if (us->ppa == 0 || us->ppa->lowerq == 0)
  806. break;
  807. putnext(us->ppa->lowerq, mp);
  808. /* mp is now gone */
  809. error = -1;
  810. }
  811. break;
  812. case PPPIO_NPMODE:
  813. if (iop->ioc_count != 2 * sizeof(int))
  814. break;
  815. if ((us->flags & US_CONTROL) == 0)
  816. break;
  817. if (mp->b_cont == 0) {
  818. DPRINT1("pppuwput/%d: ioctl PPPIO_NPMODE b_cont = 0!\n", us->mn);
  819. break;
  820. }
  821. sap = ((int *)mp->b_cont->b_rptr)[0];
  822. for (nps = us->next; nps != 0; nps = nps->next) {
  823. if (us->flags & US_DBGLOG)
  824. DPRINT2("us = 0x%x, us->next->sap = 0x%x\n", nps, nps->sap);
  825. if (nps->sap == sap)
  826. break;
  827. }
  828. if (nps == 0) {
  829. if (us->flags & US_DBGLOG)
  830. DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
  831. break;
  832. }
  833. /* XXX possibly should use qwriter here */
  834. nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
  835. if (nps->npmode != NPMODE_QUEUE && (nps->flags & US_BLOCKED) != 0)
  836. qenable(WR(nps->q));
  837. iop->ioc_count = 0;
  838. error = 0;
  839. break;
  840. case PPPIO_GIDLE:
  841. if ((ppa = us->ppa) == 0)
  842. break;
  843. mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
  844. if (mq == 0) {
  845. error = ENOSR;
  846. break;
  847. }
  848. if (mp->b_cont != 0)
  849. freemsg(mp->b_cont);
  850. mp->b_cont = mq;
  851. mq->b_cont = 0;
  852. pip = (struct ppp_idle *) mq->b_wptr;
  853. pip->xmit_idle = time - ppa->last_sent;
  854. pip->recv_idle = time - ppa->last_recv;
  855. mq->b_wptr += sizeof(struct ppp_idle);
  856. iop->ioc_count = sizeof(struct ppp_idle);
  857. error = 0;
  858. break;
  859. #ifdef LACHTCP
  860. case SIOCSIFNAME:
  861. /* Sent from IP down to us. Attach the ifstats structure. */
  862. if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
  863. break;
  864. ifr = (struct ifreq *)mp->b_cont->b_rptr;
  865. /* Find the unit number in the interface name. */
  866. for (i = 0; i < IFNAMSIZ; i++) {
  867. if (ifr->ifr_name[i] == 0 ||
  868. (ifr->ifr_name[i] >= '0' &&
  869. ifr->ifr_name[i] <= '9'))
  870. break;
  871. else
  872. us->ifname[i] = ifr->ifr_name[i];
  873. }
  874. us->ifname[i] = 0;
  875. /* Convert the unit number to binary. */
  876. for (n = 0; i < IFNAMSIZ; i++) {
  877. if (ifr->ifr_name[i] == 0) {
  878. break;
  879. }
  880. else {
  881. n = n * 10 + ifr->ifr_name[i] - '0';
  882. }
  883. }
  884. /* Verify the ppa. */
  885. if (us->ppa->ppa_id != n)
  886. break;
  887. ppa = us->ppa;
  888. /* Set up the netstat block. */
  889. strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
  890. ppa->ifstats.ifs_name = ppa->ifname;
  891. ppa->ifstats.ifs_unit = n;
  892. ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
  893. ppa->ifstats.ifs_mtu = ppa->mtu;
  894. /* Link in statistics used by netstat. */
  895. ppa->ifstats.ifs_next = ifstats;
  896. ifstats = &ppa->ifstats;
  897. iop->ioc_count = 0;
  898. error = 0;
  899. break;
  900. case SIOCGIFFLAGS:
  901. if (!(us->flags & US_CONTROL)) {
  902. if (us->ppa)
  903. us = us->ppa;
  904. else
  905. break;
  906. }
  907. ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
  908. error = 0;
  909. break;
  910. case SIOCSIFFLAGS:
  911. if (!(us->flags & US_CONTROL)) {
  912. if (us->ppa)
  913. us = us->ppa;
  914. else
  915. break;
  916. }
  917. us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
  918. error = 0;
  919. break;
  920. case SIOCSIFADDR:
  921. if (!(us->flags & US_CONTROL)) {
  922. if (us->ppa)
  923. us = us->ppa;
  924. else
  925. break;
  926. }
  927. us->ifflags |= IFF_RUNNING;
  928. ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
  929. error = 0;
  930. break;
  931. case SIOCSIFMTU:
  932. /*
  933. * Vanilla SVR4 systems don't handle SIOCSIFMTU, rather
  934. * they take the MTU from the DL_INFO_ACK we sent in response
  935. * to their DL_INFO_REQ. Fortunately, they will update the
  936. * MTU if we send an unsolicited DL_INFO_ACK up.
  937. */
  938. if ((mq = allocb(sizeof(dl_info_req_t), BPRI_HI)) == 0)
  939. break; /* should do bufcall */
  940. ((union DL_primitives *)mq->b_rptr)->dl_primitive = DL_INFO_REQ;
  941. mq->b_wptr = mq->b_rptr + sizeof(dl_info_req_t);
  942. dlpi_request(q, mq, us);
  943. /* mp is now gone */
  944. error = -1;
  945. break;
  946. case SIOCGIFNETMASK:
  947. case SIOCSIFNETMASK:
  948. case SIOCGIFADDR:
  949. case SIOCGIFDSTADDR:
  950. case SIOCSIFDSTADDR:
  951. case SIOCGIFMETRIC:
  952. error = 0;
  953. break;
  954. #endif /* LACHTCP */
  955. default:
  956. if (us->ppa == 0 || us->ppa->lowerq == 0)
  957. break;
  958. us->ioc_id = iop->ioc_id;
  959. error = -1;
  960. switch (iop->ioc_cmd) {
  961. case PPPIO_GETSTAT:
  962. case PPPIO_GETCSTAT:
  963. if (us->flags & US_LASTMOD) {
  964. error = EINVAL;
  965. break;
  966. }
  967. putnext(us->ppa->lowerq, mp);
  968. break;
  969. default:
  970. if (us->flags & US_PRIV)
  971. putnext(us->ppa->lowerq, mp);
  972. else {
  973. DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
  974. error = EPERM;
  975. }
  976. break;
  977. }
  978. break;
  979. }
  980. if (error > 0) {
  981. iop->ioc_error = error;
  982. mp->b_datap->db_type = M_IOCNAK;
  983. qreply(q, mp);
  984. } else if (error == 0) {
  985. mp->b_datap->db_type = M_IOCACK;
  986. qreply(q, mp);
  987. }
  988. break;
  989. case M_FLUSH:
  990. if (us->flags & US_DBGLOG)
  991. DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
  992. if (*mp->b_rptr & FLUSHW)
  993. flushq(q, FLUSHDATA);
  994. if (*mp->b_rptr & FLUSHR) {
  995. *mp->b_rptr &= ~FLUSHW;
  996. qreply(q, mp);
  997. } else
  998. freemsg(mp);
  999. break;
  1000. default:
  1001. freemsg(mp);
  1002. break;
  1003. }
  1004. return 0;
  1005. }
  1006. #ifndef NO_DLPI
  1007. static void
  1008. dlpi_request(q, mp, us)
  1009. queue_t *q;
  1010. mblk_t *mp;
  1011. upperstr_t *us;
  1012. {
  1013. union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
  1014. int size = mp->b_wptr - mp->b_rptr;
  1015. mblk_t *reply, *np;
  1016. upperstr_t *ppa, *os;
  1017. int sap, len;
  1018. dl_info_ack_t *info;
  1019. dl_bind_ack_t *ackp;
  1020. #if DL_CURRENT_VERSION >= 2
  1021. dl_phys_addr_ack_t *paddrack;
  1022. static struct ether_addr eaddr = {0};
  1023. #endif
  1024. if (us->flags & US_DBGLOG)
  1025. DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
  1026. d->dl_primitive, size);
  1027. switch (d->dl_primitive) {
  1028. case DL_INFO_REQ:
  1029. if (size < sizeof(dl_info_req_t))
  1030. goto badprim;
  1031. if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
  1032. break; /* should do bufcall */
  1033. reply->b_datap->db_type = M_PCPROTO;
  1034. info = (dl_info_ack_t *) reply->b_wptr;
  1035. reply->b_wptr += sizeof(dl_info_ack_t);
  1036. bzero((caddr_t) info, sizeof(dl_info_ack_t));
  1037. info->dl_primitive = DL_INFO_ACK;
  1038. info->dl_max_sdu = us->ppa? us->ppa->mtu: PPP_MAXMTU;
  1039. info->dl_min_sdu = 1;
  1040. info->dl_addr_length = sizeof(uint);
  1041. info->dl_mac_type = DL_ETHER; /* a bigger lie */
  1042. info->dl_current_state = us->state;
  1043. info->dl_service_mode = DL_CLDLS;
  1044. info->dl_provider_style = DL_STYLE2;
  1045. #if DL_CURRENT_VERSION >= 2
  1046. info->dl_sap_length = sizeof(uint);
  1047. info->dl_version = DL_CURRENT_VERSION;
  1048. #endif
  1049. qreply(q, reply);
  1050. break;
  1051. case DL_ATTACH_REQ:
  1052. if (size < sizeof(dl_attach_req_t))
  1053. goto badprim;
  1054. if (us->state != DL_UNATTACHED || us->ppa != 0) {
  1055. dlpi_error(q, us, DL_ATTACH_REQ, DL_OUTSTATE, 0);
  1056. break;
  1057. }
  1058. for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
  1059. if (ppa->ppa_id == d->attach_req.dl_ppa)
  1060. break;
  1061. if (ppa == 0) {
  1062. dlpi_error(q, us, DL_ATTACH_REQ, DL_BADPPA, 0);
  1063. break;
  1064. }
  1065. us->ppa = ppa;
  1066. qwriter(q, mp, attach_ppa, PERIM_OUTER);
  1067. return;
  1068. case DL_DETACH_REQ:
  1069. if (size < sizeof(dl_detach_req_t))
  1070. goto badprim;
  1071. if (us->state != DL_UNBOUND || us->ppa == 0) {
  1072. dlpi_error(q, us, DL_DETACH_REQ, DL_OUTSTATE, 0);
  1073. break;
  1074. }
  1075. qwriter(q, mp, detach_ppa, PERIM_OUTER);
  1076. return;
  1077. case DL_BIND_REQ:
  1078. if (size < sizeof(dl_bind_req_t))
  1079. goto badprim;
  1080. if (us->state != DL_UNBOUND || us->ppa == 0) {
  1081. dlpi_error(q, us, DL_BIND_REQ, DL_OUTSTATE, 0);
  1082. break;
  1083. }
  1084. #if 0
  1085. /* apparently this test fails (unnecessarily?) on some systems */
  1086. if (d->bind_req.dl_service_mode != DL_CLDLS) {
  1087. dlpi_error(q, us, DL_BIND_REQ, DL_UNSUPPORTED, 0);
  1088. break;
  1089. }
  1090. #endif
  1091. /* saps must be valid PPP network protocol numbers,
  1092. except that we accept ETHERTYPE_IP in place of PPP_IP. */
  1093. sap = d->bind_req.dl_sap;
  1094. us->req_sap = sap;
  1095. #if defined(SOL2)
  1096. if (us->flags & US_DBGLOG)
  1097. DPRINT2("DL_BIND_REQ: ip gives sap = 0x%x, us = 0x%x", sap, us);
  1098. if (sap == ETHERTYPE_IP) /* normal IFF_IPV4 */
  1099. sap = PPP_IP;
  1100. else if (sap == ETHERTYPE_IPV6) /* when IFF_IPV6 is set */
  1101. sap = PPP_IPV6;
  1102. else if (sap == ETHERTYPE_ALLSAP) /* snoop gives sap of 0 */
  1103. sap = PPP_ALLSAP;
  1104. else {
  1105. DPRINT2("DL_BIND_REQ: unrecognized sap = 0x%x, us = 0x%x", sap, us);
  1106. dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
  1107. break;
  1108. }
  1109. #else
  1110. if (sap == ETHERTYPE_IP)
  1111. sap = PPP_IP;
  1112. if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
  1113. dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
  1114. break;
  1115. }
  1116. #endif /* defined(SOL2) */
  1117. /* check that no other stream is bound to this sap already. */
  1118. for (os = us->ppa; os != 0; os = os->next)
  1119. if (os->sap == sap)
  1120. break;
  1121. if (os != 0) {
  1122. dlpi_error(q, us, DL_BIND_REQ, DL_NOADDR, 0);
  1123. break;
  1124. }
  1125. us->sap = sap;
  1126. us->state = DL_IDLE;
  1127. if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(uint),
  1128. BPRI_HI)) == 0)
  1129. break; /* should do bufcall */
  1130. ackp = (dl_bind_ack_t *) reply->b_wptr;
  1131. reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(uint);
  1132. reply->b_datap->db_type = M_PCPROTO;
  1133. bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
  1134. ackp->dl_primitive = DL_BIND_ACK;
  1135. ackp->dl_sap = sap;
  1136. ackp->dl_addr_length = sizeof(uint);
  1137. ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
  1138. *(uint *)(ackp+1) = sap;
  1139. qreply(q, reply);
  1140. break;
  1141. case DL_UNBIND_REQ:
  1142. if (size < sizeof(dl_unbind_req_t))
  1143. goto badprim;
  1144. if (us->state != DL_IDLE) {
  1145. dlpi_error(q, us, DL_UNBIND_REQ, DL_OUTSTATE, 0);
  1146. break;
  1147. }
  1148. us->sap = -1;
  1149. us->state = DL_UNBOUND;
  1150. #ifdef LACHTCP
  1151. us->ppa->ifstats.ifs_active = 0;
  1152. #endif
  1153. dlpi_ok(q, DL_UNBIND_REQ);
  1154. break;
  1155. case DL_UNITDATA_REQ:
  1156. if (size < sizeof(dl_unitdata_req_t))
  1157. goto badprim;
  1158. if (us->state != DL_IDLE) {
  1159. dlpi_error(q, us, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
  1160. break;
  1161. }
  1162. if ((ppa = us->ppa) == 0) {
  1163. cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
  1164. break;
  1165. }
  1166. len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
  1167. if (len > ppa->mtu) {
  1168. DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
  1169. break;
  1170. }
  1171. #if defined(SOL2)
  1172. /*
  1173. * Should there be any promiscuous stream(s), send the data
  1174. * up for each promiscuous stream that we recognize.
  1175. */
  1176. if (mp->b_cont)
  1177. promisc_sendup(ppa, mp->b_cont, us->sap, 0);
  1178. #endif /* defined(SOL2) */
  1179. mp->b_band = 0;
  1180. #ifdef PRIOQ
  1181. /* Extract s_port & d_port from IP-packet, the code is a bit
  1182. dirty here, but so am I, too... */
  1183. if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
  1184. && mp->b_cont != 0) {
  1185. u_char *bb, *tlh;
  1186. int iphlen, len;
  1187. u_short *ptr;
  1188. u_char band_unset, cur_band, syn;
  1189. u_short s_port, d_port;
  1190. bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
  1191. len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
  1192. syn = 0;
  1193. s_port = IPPORT_DEFAULT;
  1194. d_port = IPPORT_DEFAULT;
  1195. if (len >= 20) { /* 20 = minimum length of IP header */
  1196. iphlen = (bb[0] & 0x0f) * 4;
  1197. tlh = bb + iphlen;
  1198. len -= iphlen;
  1199. switch (bb[9]) {
  1200. case IPPROTO_TCP:
  1201. if (len >= 20) { /* min length of TCP header */
  1202. s_port = (tlh[0] << 8) + tlh[1];
  1203. d_port = (tlh[2] << 8) + tlh[3];
  1204. syn = tlh[13] & 0x02;
  1205. }
  1206. break;
  1207. case IPPROTO_UDP:
  1208. if (len >= 8) { /* min length of UDP header */
  1209. s_port = (tlh[0] << 8) + tlh[1];
  1210. d_port = (tlh[2] << 8) + tlh[3];
  1211. }
  1212. break;
  1213. }
  1214. }
  1215. /*
  1216. * Now calculate b_band for this packet from the
  1217. * port-priority table.
  1218. */
  1219. ptr = prioq_table;
  1220. cur_band = max_band;
  1221. band_unset = 1;
  1222. while (*ptr) {
  1223. while (*ptr && band_unset)
  1224. if (s_port == *ptr || d_port == *ptr++) {
  1225. mp->b_band = cur_band;
  1226. band_unset = 0;
  1227. break;
  1228. }
  1229. ptr++;
  1230. cur_band--;
  1231. }
  1232. if (band_unset)
  1233. mp->b_band = def_band;
  1234. /* It may be usable to urge SYN packets a bit */
  1235. if (syn)
  1236. mp->b_band++;
  1237. }
  1238. #endif /* PRIOQ */
  1239. /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
  1240. if (mp->b_datap->db_ref > 1) {
  1241. np = allocb(PPP_HDRLEN, BPRI_HI);
  1242. if (np == 0)
  1243. break; /* gak! */
  1244. np->b_cont = mp->b_cont;
  1245. mp->b_cont = 0;
  1246. freeb(mp);
  1247. mp = np;
  1248. } else
  1249. mp->b_datap->db_type = M_DATA;
  1250. /* XXX should use dl_dest_addr_offset/length here,
  1251. but we would have to translate ETHERTYPE_IP -> PPP_IP */
  1252. mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
  1253. mp->b_rptr[0] = PPP_ALLSTATIONS;
  1254. mp->b_rptr[1] = PPP_UI;
  1255. mp->b_rptr[2] = us->sap >> 8;
  1256. mp->b_rptr[3] = us->sap;
  1257. /* pass_packet frees the packet on returning 0 */
  1258. if (pass_packet(us, mp, 1)) {
  1259. if (!send_data(mp, us) && !putq(q, mp))
  1260. freemsg(mp);
  1261. }
  1262. return;
  1263. #if DL_CURRENT_VERSION >= 2
  1264. case DL_PHYS_ADDR_REQ:
  1265. if (size < sizeof(dl_phys_addr_req_t))
  1266. goto badprim;
  1267. /*
  1268. * Don't check state because ifconfig sends this one down too
  1269. */
  1270. if ((reply = allocb(sizeof(dl_phys_addr_ack_t)+ETHERADDRL,
  1271. BPRI_HI)) == 0)
  1272. break; /* should do bufcall */
  1273. reply->b_datap->db_type = M_PCPROTO;
  1274. paddrack = (dl_phys_addr_ack_t *) reply->b_wptr;
  1275. reply->b_wptr += sizeof(dl_phys_addr_ack_t);
  1276. bzero((caddr_t) paddrack, sizeof(dl_phys_addr_ack_t)+ETHERADDRL);
  1277. paddrack->dl_primitive = DL_PHYS_ADDR_ACK;
  1278. paddrack->dl_addr_length = ETHERADDRL;
  1279. paddrack->dl_addr_offset = sizeof(dl_phys_addr_ack_t);
  1280. bcopy(&eaddr, reply->b_wptr, ETHERADDRL);
  1281. reply->b_wptr += ETHERADDRL;
  1282. qreply(q, reply);
  1283. break;
  1284. #if defined(SOL2)
  1285. case DL_PROMISCON_REQ:
  1286. if (size < sizeof(dl_promiscon_req_t))
  1287. goto badprim;
  1288. us->flags |= US_PROMISC;
  1289. dlpi_ok(q, DL_PROMISCON_REQ);
  1290. break;
  1291. case DL_PROMISCOFF_REQ:
  1292. if (size < sizeof(dl_promiscoff_req_t))
  1293. goto badprim;
  1294. us->flags &= ~US_PROMISC;
  1295. dlpi_ok(q, DL_PROMISCOFF_REQ);
  1296. break;
  1297. #else
  1298. case DL_PROMISCON_REQ: /* fall thru */
  1299. case DL_PROMISCOFF_REQ: /* fall thru */
  1300. #endif /* defined(SOL2) */
  1301. #endif /* DL_CURRENT_VERSION >= 2 */
  1302. #if DL_CURRENT_VERSION >= 2
  1303. case DL_SET_PHYS_ADDR_REQ:
  1304. case DL_SUBS_BIND_REQ:
  1305. case DL_SUBS_UNBIND_REQ:
  1306. case DL_ENABMULTI_REQ:
  1307. case DL_DISABMULTI_REQ:
  1308. case DL_XID_REQ:
  1309. case DL_TEST_REQ:
  1310. case DL_REPLY_UPDATE_REQ:
  1311. case DL_REPLY_REQ:
  1312. case DL_DATA_ACK_REQ:
  1313. #endif
  1314. case DL_CONNECT_REQ:
  1315. case DL_TOKEN_REQ:
  1316. dlpi_error(q, us, d->dl_primitive, DL_NOTSUPPORTED, 0);
  1317. break;
  1318. case DL_CONNECT_RES:
  1319. case DL_DISCONNECT_REQ:
  1320. case DL_RESET_REQ:
  1321. case DL_RESET_RES:
  1322. dlpi_error(q, us, d->dl_primitive, DL_OUTSTATE, 0);
  1323. break;
  1324. case DL_UDQOS_REQ:
  1325. dlpi_error(q, us, d->dl_primitive, DL_BADQOSTYPE, 0);
  1326. break;
  1327. #if DL_CURRENT_VERSION >= 2
  1328. case DL_TEST_RES:
  1329. case DL_XID_RES:
  1330. break;
  1331. #endif
  1332. default:
  1333. if (us->flags & US_DBGLOG)
  1334. DPRINT1("ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
  1335. /* fall through */
  1336. badprim:
  1337. dlpi_error(q, us, d->dl_primitive, DL_BADPRIM, 0);
  1338. break;
  1339. }
  1340. freemsg(mp);
  1341. }
  1342. static void
  1343. dlpi_error(q, us, prim, err, uerr)
  1344. queue_t *q;
  1345. upperstr_t *us;
  1346. int prim, err, uerr;
  1347. {
  1348. mblk_t *reply;
  1349. dl_error_ack_t *errp;
  1350. if (us->flags & US_DBGLOG)
  1351. DPRINT3("ppp/%d: dlpi error, prim=%x, err=%x\n", us->mn, prim, err);
  1352. reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
  1353. if (reply == 0)
  1354. return; /* XXX should do bufcall */
  1355. reply->b_datap->db_type = M_PCPROTO;
  1356. errp = (dl_error_ack_t *) reply->b_wptr;
  1357. reply->b_wptr += sizeof(dl_error_ack_t);
  1358. errp->dl_primitive = DL_ERROR_ACK;
  1359. errp->dl_error_primitive = prim;
  1360. errp->dl_errno = err;
  1361. errp->dl_unix_errno = uerr;
  1362. qreply(q, reply);
  1363. }
  1364. static void
  1365. dlpi_ok(q, prim)
  1366. queue_t *q;
  1367. int prim;
  1368. {
  1369. mblk_t *reply;
  1370. dl_ok_ack_t *okp;
  1371. reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
  1372. if (reply == 0)
  1373. return; /* XXX should do bufcall */
  1374. reply->b_datap->db_type = M_PCPROTO;
  1375. okp = (dl_ok_ack_t *) reply->b_wptr;
  1376. reply->b_wptr += sizeof(dl_ok_ack_t);
  1377. okp->dl_primitive = DL_OK_ACK;
  1378. okp->dl_correct_primitive = prim;
  1379. qreply(q, reply);
  1380. }
  1381. #endif /* NO_DLPI */
  1382. /*
  1383. * If return value is 0, then the packet has already been freed.
  1384. */
  1385. static int
  1386. pass_packet(us, mp, outbound)
  1387. upperstr_t *us;
  1388. mblk_t *mp;
  1389. int outbound;
  1390. {
  1391. int pass;
  1392. upperstr_t *ppa;
  1393. if ((ppa = us->ppa) == 0) {
  1394. freemsg(mp);
  1395. return 0;
  1396. }
  1397. #ifdef FILTER_PACKETS
  1398. pass = ip_hard_filter(us, mp, outbound);
  1399. #else
  1400. /*
  1401. * Here is where we might, in future, decide whether to pass
  1402. * or drop the packet, and whether it counts as link activity.
  1403. */
  1404. pass = 1;
  1405. #endif /* FILTER_PACKETS */
  1406. if (pass < 0) {
  1407. /* pass only if link already up, and don't update time */
  1408. if (ppa->lowerq == 0) {
  1409. freemsg(mp);
  1410. return 0;
  1411. }
  1412. pass = 1;
  1413. } else if (pass) {
  1414. if (outbound)
  1415. ppa->last_sent = time;
  1416. else
  1417. ppa->last_recv = time;
  1418. }
  1419. return pass;
  1420. }
  1421. /*
  1422. * We have some data to send down to the lower stream (or up the
  1423. * control stream, if we don't have a lower stream attached).
  1424. * Returns 1 if the message was dealt with, 0 if it wasn't able
  1425. * to be sent on and should therefore be queued up.
  1426. */
  1427. static int
  1428. send_data(mp, us)
  1429. mblk_t *mp;
  1430. upperstr_t *us;
  1431. {
  1432. upperstr_t *ppa;
  1433. if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
  1434. return 0;
  1435. ppa = us->ppa;
  1436. if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
  1437. if (us->flags & US_DBGLOG)
  1438. DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
  1439. freemsg(mp);
  1440. return 1;
  1441. }
  1442. if (ppa->lowerq == 0) {
  1443. /* try to send it up the control stream */
  1444. if (bcanputnext(ppa->q, mp->b_band)) {
  1445. /*
  1446. * The message seems to get corrupted for some reason if
  1447. * we just send the message up as it is, so we send a copy.
  1448. */
  1449. mblk_t *np = copymsg(mp);
  1450. freemsg(mp);
  1451. if (np != 0)
  1452. putnext(ppa->q, np);
  1453. return 1;
  1454. }
  1455. } else {
  1456. if (bcanputnext(ppa->lowerq, mp->b_band)) {
  1457. MT_ENTER(&ppa->stats_lock);
  1458. ppa->stats.ppp_opackets++;
  1459. ppa->stats.ppp_obytes += msgdsize(mp);
  1460. #ifdef INCR_OPACKETS
  1461. INCR_OPACKETS(ppa);
  1462. #endif
  1463. MT_EXIT(&ppa->stats_lock);
  1464. /*
  1465. * The lower queue is only ever detached while holding an
  1466. * exclusive lock on the whole driver. So we can be confident
  1467. * that the lower queue is still there.
  1468. */
  1469. putnext(ppa->lowerq, mp);
  1470. return 1;
  1471. }
  1472. }
  1473. us->flags |= US_BLOCKED;
  1474. return 0;
  1475. }
  1476. /*
  1477. * Allocate a new PPA id and link this stream into the list of PPAs.
  1478. * This procedure is called with an exclusive lock on all queues in
  1479. * this driver.
  1480. */
  1481. static void
  1482. new_ppa(q, mp)
  1483. queue_t *q;
  1484. mblk_t *mp;
  1485. {
  1486. upperstr_t *us, *up, **usp;
  1487. int ppa_id;
  1488. us = (upperstr_t *) q->q_ptr;
  1489. if (us == 0) {
  1490. DPRINT("new_ppa: q_ptr = 0!\n");
  1491. return;
  1492. }
  1493. usp = &ppas;
  1494. ppa_id = 0;
  1495. while ((up = *usp) != 0 && ppa_id == up->ppa_id) {
  1496. ++ppa_id;
  1497. usp = &up->nextppa;
  1498. }
  1499. us->ppa_id = ppa_id;
  1500. us->ppa = us;
  1501. us->next = 0;
  1502. us->nextppa = *usp;
  1503. *usp = us;
  1504. us->flags |= US_CONTROL;
  1505. us->npmode = NPMODE_PASS;
  1506. us->mtu = PPP_MTU;
  1507. us->mru = PPP_MRU;
  1508. #ifdef SOL2
  1509. /*
  1510. * Create a kstats record for our statistics, so netstat -i works.
  1511. */
  1512. if (us->kstats == 0) {
  1513. char unit[32];
  1514. sprintf(unit, "ppp%d", us->ppa->ppa_id);
  1515. us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
  1516. "net", KSTAT_TYPE_NAMED, 4, 0);
  1517. if (us->kstats != 0) {
  1518. kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
  1519. strcpy(kn[0].name, "ipackets");
  1520. kn[0].data_type = KSTAT_DATA_ULONG;
  1521. strcpy(kn[1].name, "ierrors");
  1522. kn[1].data_type = KSTAT_DATA_ULONG;
  1523. strcpy(kn[2].name, "opackets");
  1524. kn[2].data_type = KSTAT_DATA_ULONG;
  1525. strcpy(kn[3].name, "oerrors");
  1526. kn[3].data_type = KSTAT_DATA_ULONG;
  1527. kstat_install(us->kstats);
  1528. }
  1529. }
  1530. #endif /* SOL2 */
  1531. *(int *)mp->b_cont->b_rptr = ppa_id;
  1532. mp->b_datap->db_type = M_IOCACK;
  1533. qreply(q, mp);
  1534. }
  1535. static void
  1536. attach_ppa(q, mp)
  1537. queue_t *q;
  1538. mblk_t *mp;
  1539. {
  1540. upperstr_t *us, *t;
  1541. us = (upperstr_t *) q->q_ptr;
  1542. if (us == 0) {
  1543. DPRINT("attach_ppa: q_ptr = 0!\n");
  1544. return;
  1545. }
  1546. #ifndef NO_DLPI
  1547. us->state = DL_UNBOUND;
  1548. #endif
  1549. for (t = us->ppa; t->next != 0; t = t->next)
  1550. ;
  1551. t->next = us;
  1552. us->next = 0;
  1553. if (mp->b_datap->db_type == M_IOCTL) {
  1554. mp->b_datap->db_type = M_IOCACK;
  1555. qreply(q, mp);
  1556. } else {
  1557. #ifndef NO_DLPI
  1558. dlpi_ok(q, DL_ATTACH_REQ);
  1559. #endif
  1560. freemsg(mp);
  1561. }
  1562. }
  1563. #ifndef NO_DLPI
  1564. static void
  1565. detach_ppa(q, mp)
  1566. queue_t *q;
  1567. mblk_t *mp;
  1568. {
  1569. upperstr_t *us, *t;
  1570. us = (upperstr_t *) q->q_ptr;
  1571. if (us == 0) {
  1572. DPRINT("detach_ppa: q_ptr = 0!\n");
  1573. return;
  1574. }
  1575. for (t = us->ppa; t->next != 0; t = t->next)
  1576. if (t->next == us) {
  1577. t->next = us->next;
  1578. break;
  1579. }
  1580. us->next = 0;
  1581. us->ppa = 0;
  1582. us->state = DL_UNATTACHED;
  1583. dlpi_ok(q, DL_DETACH_REQ);
  1584. freemsg(mp);
  1585. }
  1586. #endif
  1587. /*
  1588. * We call this with qwriter in order to give the upper queue procedures
  1589. * the guarantee that the lower queue is not going to go away while
  1590. * they are executing.
  1591. */
  1592. static void
  1593. detach_lower(q, mp)
  1594. queue_t *q;
  1595. mblk_t *mp;
  1596. {
  1597. upperstr_t *us;
  1598. us = (upperstr_t *) q->q_ptr;
  1599. if (us == 0) {
  1600. DPRINT("detach_lower: q_ptr = 0!\n");
  1601. return;
  1602. }
  1603. LOCK_LOWER_W;
  1604. us->lowerq->q_ptr = 0;
  1605. RD(us->lowerq)->q_ptr = 0;
  1606. us->lowerq = 0;
  1607. UNLOCK_LOWER;
  1608. /* Unblock streams which now feed back up the control stream. */
  1609. qenable(us->q);
  1610. mp->b_datap->db_type = M_IOCACK;
  1611. qreply(q, mp);
  1612. }
  1613. static int
  1614. pppuwsrv(q)
  1615. queue_t *q;
  1616. {
  1617. upperstr_t *us, *as;
  1618. mblk_t *mp;
  1619. us = (upperstr_t *) q->q_ptr;
  1620. if (us == 0) {
  1621. DPRINT("pppuwsrv: q_ptr = 0!\n");
  1622. return 0;
  1623. }
  1624. /*
  1625. * If this is a control stream, then this service procedure
  1626. * probably got enabled because of flow control in the lower
  1627. * stream being enabled (or because of the lower stream going
  1628. * away). Therefore we enable the service procedure of all
  1629. * attached upper streams.
  1630. */
  1631. if (us->flags & US_CONTROL) {
  1632. for (as = us->next; as != 0; as = as->next)
  1633. qenable(WR(as->q));
  1634. }
  1635. /* Try to send on any data queued here. */
  1636. us->flags &= ~US_BLOCKED;
  1637. while ((mp = getq(q)) != 0) {
  1638. if (!send_data(mp, us)) {
  1639. putbq(q, mp);
  1640. break;
  1641. }
  1642. }
  1643. return 0;
  1644. }
  1645. /* should never get called... */
  1646. static int
  1647. ppplwput(q, mp)
  1648. queue_t *q;
  1649. mblk_t *mp;
  1650. {
  1651. putnext(q, mp);
  1652. return 0;
  1653. }
  1654. static int
  1655. ppplwsrv(q)
  1656. queue_t *q;
  1657. {
  1658. queue_t *uq;
  1659. /*
  1660. * Flow control has back-enabled this stream:
  1661. * enable the upper write service procedure for
  1662. * the upper control stream for this lower stream.
  1663. */
  1664. LOCK_LOWER_R;
  1665. uq = (queue_t *) q->q_ptr;
  1666. if (uq != 0)
  1667. qenable(uq);
  1668. UNLOCK_LOWER;
  1669. return 0;
  1670. }
  1671. /*
  1672. * This should only get called for control streams.
  1673. */
  1674. static int
  1675. pppurput(q, mp)
  1676. queue_t *q;
  1677. mblk_t *mp;
  1678. {
  1679. upperstr_t *ppa, *us;
  1680. int proto, len;
  1681. struct iocblk *iop;
  1682. ppa = (upperstr_t *) q->q_ptr;
  1683. if (ppa == 0) {
  1684. DPRINT("pppurput: q_ptr = 0!\n");
  1685. return 0;
  1686. }
  1687. switch (mp->b_datap->db_type) {
  1688. case M_CTL:
  1689. MT_ENTER(&ppa->stats_lock);
  1690. switch (*mp->b_rptr) {
  1691. case PPPCTL_IERROR:
  1692. #ifdef INCR_IERRORS
  1693. INCR_IERRORS(ppa);
  1694. #endif
  1695. ppa->stats.ppp_ierrors++;
  1696. break;
  1697. case PPPCTL_OERROR:
  1698. #ifdef INCR_OERRORS
  1699. INCR_OERRORS(ppa);
  1700. #endif
  1701. ppa->stats.ppp_oerrors++;
  1702. break;
  1703. }
  1704. MT_EXIT(&ppa->stats_lock);
  1705. freemsg(mp);
  1706. break;
  1707. case M_IOCACK:
  1708. case M_IOCNAK:
  1709. /*
  1710. * Attempt to match up the response with the stream
  1711. * that the request came from.
  1712. */
  1713. iop = (struct iocblk *) mp->b_rptr;
  1714. for (us = ppa; us != 0; us = us->next)
  1715. if (us->ioc_id == iop->ioc_id)
  1716. break;
  1717. if (us == 0)
  1718. freemsg(mp);
  1719. else
  1720. putnext(us->q, mp);
  1721. break;
  1722. case M_HANGUP:
  1723. /*
  1724. * The serial device has hung up. We don't want to send
  1725. * the M_HANGUP message up to pppd because that will stop
  1726. * us from using the control stream any more. Instead we
  1727. * send a zero-length message as an end-of-file indication.
  1728. */
  1729. freemsg(mp);
  1730. mp = allocb(1, BPRI_HI);
  1731. if (mp == 0) {
  1732. DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
  1733. break;
  1734. }
  1735. putnext(ppa->q, mp);
  1736. break;
  1737. case M_DATA:
  1738. len = msgdsize(mp);
  1739. if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
  1740. PULLUP(mp, PPP_HDRLEN);
  1741. if (mp == 0) {
  1742. DPRINT1("ppp_urput: msgpullup failed (len=%d)\n", len);
  1743. break;
  1744. }
  1745. }
  1746. MT_ENTER(&ppa->stats_lock);
  1747. ppa->stats.ppp_ipackets++;
  1748. ppa->stats.ppp_ibytes += len;
  1749. #ifdef INCR_IPACKETS
  1750. INCR_IPACKETS(ppa);
  1751. #endif
  1752. MT_EXIT(&ppa->stats_lock);
  1753. proto = PPP_PROTOCOL(mp->b_rptr);
  1754. #if defined(SOL2)
  1755. /*
  1756. * Should there be any promiscuous stream(s), send the data
  1757. * up for each promiscuous stream that we recognize.
  1758. */
  1759. promisc_sendup(ppa, mp, proto, 1);
  1760. #endif /* defined(SOL2) */
  1761. if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
  1762. /*
  1763. * A data packet for some network protocol.
  1764. * Queue it on the upper stream for that protocol.
  1765. * XXX could we just putnext it? (would require thought)
  1766. * The rblocked flag is there to ensure that we keep
  1767. * messages in order for each network protocol.
  1768. */
  1769. /* pass_packet frees the packet on returning 0 */
  1770. if (!pass_packet(us, mp, 0))
  1771. break;
  1772. if (!us->rblocked && !canput(us->q))
  1773. us->rblocked = 1;
  1774. if (!putq(us->rblocked ? q : us->q, mp))
  1775. freemsg(mp);
  1776. break;
  1777. }
  1778. /* FALLTHROUGH */
  1779. default:
  1780. /*
  1781. * A control frame, a frame for an unknown protocol,
  1782. * or some other message type.
  1783. * Send it up to pppd via the control stream.
  1784. */
  1785. if (queclass(mp) == QPCTL || canputnext(ppa->q))
  1786. putnext(ppa->q, mp);
  1787. else if (!putq(q, mp))
  1788. freemsg(mp);
  1789. break;
  1790. }
  1791. return 0;
  1792. }
  1793. static int
  1794. pppursrv(q)
  1795. queue_t *q;
  1796. {
  1797. upperstr_t *us, *as;
  1798. mblk_t *mp, *hdr;
  1799. #ifndef NO_DLPI
  1800. dl_unitdata_ind_t *ud;
  1801. #endif
  1802. int proto;
  1803. us = (upperstr_t *) q->q_ptr;
  1804. if (us == 0) {
  1805. DPRINT("pppursrv: q_ptr = 0!\n");
  1806. return 0;
  1807. }
  1808. if (us->flags & US_CONTROL) {
  1809. /*
  1810. * A control stream.
  1811. * If there is no lower queue attached, run the write service
  1812. * routines of other upper streams attached to this PPA.
  1813. */
  1814. if (us->lowerq == 0) {
  1815. as = us;
  1816. do {
  1817. if (as->flags & US_BLOCKED)
  1818. qenable(WR(as->q));
  1819. as = as->next;
  1820. } while (as != 0);
  1821. }
  1822. /*
  1823. * Messages get queued on this stream's read queue if they
  1824. * can't be queued on the read queue of the attached stream
  1825. * that they are destined for. This is for flow control -
  1826. * when this queue fills up, the lower read put procedure will
  1827. * queue messages there and the flow control will propagate
  1828. * down from there.
  1829. */
  1830. while ((mp = getq(q)) != 0) {
  1831. proto = PPP_PROTOCOL(mp->b_rptr);
  1832. if (proto < 0x8000 && (as = find_dest(us, proto)) != 0) {
  1833. if (!canput(as->q))
  1834. break;
  1835. if (!putq(as->q, mp))
  1836. freemsg(mp);
  1837. } else {
  1838. if (!canputnext(q))
  1839. break;
  1840. putnext(q, mp);
  1841. }
  1842. }
  1843. if (mp) {
  1844. putbq(q, mp);
  1845. } else {
  1846. /* can now put stuff directly on network protocol streams again */
  1847. for (as = us->next; as != 0; as = as->next)
  1848. as->rblocked = 0;
  1849. }
  1850. /*
  1851. * If this stream has a lower stream attached,
  1852. * enable the read queue's service routine.
  1853. * XXX we should really only do this if the queue length
  1854. * has dropped below the low-water mark.
  1855. */
  1856. if (us->lowerq != 0)
  1857. qenable(RD(us->lowerq));
  1858. } else {
  1859. /*
  1860. * A network protocol stream. Put a DLPI header on each
  1861. * packet and send it on.
  1862. * (Actually, it seems that the IP module will happily
  1863. * accept M_DATA messages without the DL_UNITDATA_IND header.)
  1864. */
  1865. while ((mp = getq(q)) != 0) {
  1866. if (!canputnext(q)) {
  1867. putbq(q, mp);
  1868. break;
  1869. }
  1870. #ifndef NO_DLPI
  1871. proto = PPP_PROTOCOL(mp->b_rptr);
  1872. mp->b_rptr += PPP_HDRLEN;
  1873. hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint),
  1874. BPRI_MED);
  1875. if (hdr == 0) {
  1876. /* XXX should put it back and use bufcall */
  1877. freemsg(mp);
  1878. continue;
  1879. }
  1880. hdr->b_datap->db_type = M_PROTO;
  1881. ud = (dl_unitdata_ind_t *) hdr->b_wptr;
  1882. hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint);
  1883. hdr->b_cont = mp;
  1884. ud->dl_primitive = DL_UNITDATA_IND;
  1885. ud->dl_dest_addr_length = sizeof(uint);
  1886. ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
  1887. ud->dl_src_addr_length = sizeof(uint);
  1888. ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(uint);
  1889. #if DL_CURRENT_VERSION >= 2
  1890. ud->dl_group_address = 0;
  1891. #endif
  1892. /* Send the DLPI client the data with the SAP they requested,
  1893. (e.g. ETHERTYPE_IP) rather than the PPP protocol number
  1894. (e.g. PPP_IP) */
  1895. ((uint *)(ud + 1))[0] = us->req_sap; /* dest SAP */
  1896. ((uint *)(ud + 1))[1] = us->req_sap; /* src SAP */
  1897. putnext(q, hdr);
  1898. #else /* NO_DLPI */
  1899. putnext(q, mp);
  1900. #endif /* NO_DLPI */
  1901. }
  1902. /*
  1903. * Now that we have consumed some packets from this queue,
  1904. * enable the control stream's read service routine so that we
  1905. * can process any packets for us that might have got queued
  1906. * there for flow control reasons.
  1907. */
  1908. if (us->ppa)
  1909. qenable(us->ppa->q);
  1910. }
  1911. return 0;
  1912. }
  1913. static upperstr_t *
  1914. find_dest(ppa, proto)
  1915. upperstr_t *ppa;
  1916. int proto;
  1917. {
  1918. upperstr_t *us;
  1919. for (us = ppa->next; us != 0; us = us->next)
  1920. if (proto == us->sap)
  1921. break;
  1922. return us;
  1923. }
  1924. #if defined (SOL2)
  1925. /*
  1926. * Test upstream promiscuous conditions. As of now, only pass IPv4 and
  1927. * Ipv6 packets upstream (let PPP packets be decoded elsewhere).
  1928. */
  1929. static upperstr_t *
  1930. find_promisc(us, proto)
  1931. upperstr_t *us;
  1932. int proto;
  1933. {
  1934. if ((proto != PPP_IP) && (proto != PPP_IPV6))
  1935. return (upperstr_t *)0;
  1936. for ( ; us; us = us->next) {
  1937. if ((us->flags & US_PROMISC) && (us->state == DL_IDLE))
  1938. return us;
  1939. }
  1940. return (upperstr_t *)0;
  1941. }
  1942. /*
  1943. * Prepend an empty Ethernet header to msg for snoop, et al.
  1944. */
  1945. static mblk_t *
  1946. prepend_ether(us, mp, proto)
  1947. upperstr_t *us;
  1948. mblk_t *mp;
  1949. int proto;
  1950. {
  1951. mblk_t *eh;
  1952. int type;
  1953. if ((eh = allocb(sizeof(struct ether_header), BPRI_HI)) == 0) {
  1954. freemsg(mp);
  1955. return (mblk_t *)0;
  1956. }
  1957. if (proto == PPP_IP)
  1958. type = ETHERTYPE_IP;
  1959. else if (proto == PPP_IPV6)
  1960. type = ETHERTYPE_IPV6;
  1961. else
  1962. type = proto; /* What else? Let decoder decide */
  1963. eh->b_wptr += sizeof(struct ether_header);
  1964. bzero((caddr_t)eh->b_rptr, sizeof(struct ether_header));
  1965. ((struct ether_header *)eh->b_rptr)->ether_type = htons((short)type);
  1966. eh->b_cont = mp;
  1967. return (eh);
  1968. }
  1969. /*
  1970. * Prepend DL_UNITDATA_IND mblk to msg
  1971. */
  1972. static mblk_t *
  1973. prepend_udind(us, mp, proto)
  1974. upperstr_t *us;
  1975. mblk_t *mp;
  1976. int proto;
  1977. {
  1978. dl_unitdata_ind_t *dlu;
  1979. mblk_t *dh;
  1980. size_t size;
  1981. size = sizeof(dl_unitdata_ind_t);
  1982. if ((dh = allocb(size, BPRI_MED)) == 0) {
  1983. freemsg(mp);
  1984. return (mblk_t *)0;
  1985. }
  1986. dh->b_datap->db_type = M_PROTO;
  1987. dh->b_wptr = dh->b_datap->db_lim;
  1988. dh->b_rptr = dh->b_wptr - size;
  1989. dlu = (dl_unitdata_ind_t *)dh->b_rptr;
  1990. dlu->dl_primitive = DL_UNITDATA_IND;
  1991. dlu->dl_dest_addr_length = 0;
  1992. dlu->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
  1993. dlu->dl_src_addr_length = 0;
  1994. dlu->dl_src_addr_offset = sizeof(dl_unitdata_ind_t);
  1995. dlu->dl_group_address = 0;
  1996. dh->b_cont = mp;
  1997. return (dh);
  1998. }
  1999. /*
  2000. * For any recognized promiscuous streams, send data upstream
  2001. */
  2002. static void
  2003. promisc_sendup(ppa, mp, proto, skip)
  2004. upperstr_t *ppa;
  2005. mblk_t *mp;
  2006. int proto, skip;
  2007. {
  2008. mblk_t *dup_mp, *dup_dup_mp;
  2009. upperstr_t *prus, *nprus;
  2010. if ((prus = find_promisc(ppa, proto)) != 0) {
  2011. if (dup_mp = dupmsg(mp)) {
  2012. if (skip)
  2013. dup_mp->b_rptr += PPP_HDRLEN;
  2014. for ( ; nprus = find_promisc(prus->next, proto);
  2015. prus = nprus) {
  2016. if (dup_dup_mp = dupmsg(dup_mp)) {
  2017. if (canputnext(prus->q)) {
  2018. if (prus->flags & US_RAWDATA) {
  2019. dup_dup_mp = prepend_ether(prus, dup_dup_mp, proto);
  2020. } else {
  2021. dup_dup_mp = prepend_udind(prus, dup_dup_mp, proto);
  2022. }
  2023. if (dup_dup_mp == 0)
  2024. continue;
  2025. putnext(prus->q, dup_dup_mp);
  2026. } else {
  2027. DPRINT("ppp_urput: data to promisc q dropped\n");
  2028. freemsg(dup_dup_mp);
  2029. }
  2030. }
  2031. }
  2032. if (canputnext(prus->q)) {
  2033. if (prus->flags & US_RAWDATA) {
  2034. dup_mp = prepend_ether(prus, dup_mp, proto);
  2035. } else {
  2036. dup_mp = prepend_udind(prus, dup_mp, proto);
  2037. }
  2038. if (dup_mp != 0)
  2039. putnext(prus->q, dup_mp);
  2040. } else {
  2041. DPRINT("ppp_urput: data to promisc q dropped\n");
  2042. freemsg(dup_mp);
  2043. }
  2044. }
  2045. }
  2046. }
  2047. #endif /* defined(SOL2) */
  2048. /*
  2049. * We simply put the message on to the associated upper control stream
  2050. * (either here or in ppplrsrv). That way we enter the perimeters
  2051. * before looking through the list of attached streams to decide which
  2052. * stream it should go up.
  2053. */
  2054. static int
  2055. ppplrput(q, mp)
  2056. queue_t *q;
  2057. mblk_t *mp;
  2058. {
  2059. queue_t *uq;
  2060. struct iocblk *iop;
  2061. switch (mp->b_datap->db_type) {
  2062. case M_IOCTL:
  2063. iop = (struct iocblk *) mp->b_rptr;
  2064. iop->ioc_error = EINVAL;
  2065. mp->b_datap->db_type = M_IOCNAK;
  2066. qreply(q, mp);
  2067. return 0;
  2068. case M_FLUSH:
  2069. if (*mp->b_rptr & FLUSHR)
  2070. flushq(q, FLUSHDATA);
  2071. if (*mp->b_rptr & FLUSHW) {
  2072. *mp->b_rptr &= ~FLUSHR;
  2073. qreply(q, mp);
  2074. } else
  2075. freemsg(mp);
  2076. return 0;
  2077. }
  2078. /*
  2079. * If we can't get the lower lock straight away, queue this one
  2080. * rather than blocking, to avoid the possibility of deadlock.
  2081. */
  2082. if (!TRYLOCK_LOWER_R) {
  2083. if (!putq(q, mp))
  2084. freemsg(mp);
  2085. return 0;
  2086. }
  2087. /*
  2088. * Check that we're still connected to the driver.
  2089. */
  2090. uq = (queue_t *) q->q_ptr;
  2091. if (uq == 0) {
  2092. UNLOCK_LOWER;
  2093. DPRINT1("ppplrput: q = %x, uq = 0??\n", q);
  2094. freemsg(mp);
  2095. return 0;
  2096. }
  2097. /*
  2098. * Try to forward the message to the put routine for the upper
  2099. * control stream for this lower stream.
  2100. * If there are already messages queued here, queue this one so
  2101. * they don't get out of order.
  2102. */
  2103. if (queclass(mp) == QPCTL || (qsize(q) == 0 && canput(uq)))
  2104. put(uq, mp);
  2105. else if (!putq(q, mp))
  2106. freemsg(mp);
  2107. UNLOCK_LOWER;
  2108. return 0;
  2109. }
  2110. static int
  2111. ppplrsrv(q)
  2112. queue_t *q;
  2113. {
  2114. mblk_t *mp;
  2115. queue_t *uq;
  2116. /*
  2117. * Packets get queued here for flow control reasons
  2118. * or if the lrput routine couldn't get the lower lock
  2119. * without blocking.
  2120. */
  2121. LOCK_LOWER_R;
  2122. uq = (queue_t *) q->q_ptr;
  2123. if (uq == 0) {
  2124. UNLOCK_LOWER;
  2125. flushq(q, FLUSHALL);
  2126. DPRINT1("ppplrsrv: q = %x, uq = 0??\n", q);
  2127. return 0;
  2128. }
  2129. while ((mp = getq(q)) != 0) {
  2130. if (queclass(mp) == QPCTL || canput(uq))
  2131. put(uq, mp);
  2132. else {
  2133. putbq(q, mp);
  2134. break;
  2135. }
  2136. }
  2137. UNLOCK_LOWER;
  2138. return 0;
  2139. }
  2140. static int
  2141. putctl2(q, type, code, val)
  2142. queue_t *q;
  2143. int type, code, val;
  2144. {
  2145. mblk_t *mp;
  2146. mp = allocb(2, BPRI_HI);
  2147. if (mp == 0)
  2148. return 0;
  2149. mp->b_datap->db_type = type;
  2150. mp->b_wptr[0] = code;
  2151. mp->b_wptr[1] = val;
  2152. mp->b_wptr += 2;
  2153. putnext(q, mp);
  2154. return 1;
  2155. }
  2156. static int
  2157. putctl4(q, type, code, val)
  2158. queue_t *q;
  2159. int type, code, val;
  2160. {
  2161. mblk_t *mp;
  2162. mp = allocb(4, BPRI_HI);
  2163. if (mp == 0)
  2164. return 0;
  2165. mp->b_datap->db_type = type;
  2166. mp->b_wptr[0] = code;
  2167. ((short *)mp->b_wptr)[1] = val;
  2168. mp->b_wptr += 4;
  2169. putnext(q, mp);
  2170. return 1;
  2171. }
  2172. static void
  2173. debug_dump(q, mp)
  2174. queue_t *q;
  2175. mblk_t *mp;
  2176. {
  2177. upperstr_t *us;
  2178. queue_t *uq, *lq;
  2179. DPRINT("ppp upper streams:\n");
  2180. for (us = minor_devs; us != 0; us = us->nextmn) {
  2181. uq = us->q;
  2182. DPRINT3(" %d: q=%x rlev=%d",
  2183. us->mn, uq, (uq? qsize(uq): 0));
  2184. DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
  2185. us->flags, "\020\1priv\2control\3blocked\4last");
  2186. DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
  2187. us->req_sap);
  2188. if (us->ppa == 0)
  2189. DPRINT(" ppa=?\n");
  2190. else
  2191. DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
  2192. if (us->flags & US_CONTROL) {
  2193. lq = us->lowerq;
  2194. DPRINT3(" control for %d lq=%x rlev=%d",
  2195. us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
  2196. DPRINT3(" wlev=%d mru=%d mtu=%d\n",
  2197. (lq? qsize(lq): 0), us->mru, us->mtu);
  2198. }
  2199. }
  2200. mp->b_datap->db_type = M_IOCACK;
  2201. qreply(q, mp);
  2202. }
  2203. #ifdef FILTER_PACKETS
  2204. #include <netinet/in_systm.h>
  2205. #include <netinet/ip.h>
  2206. #include <netinet/udp.h>
  2207. #include <netinet/tcp.h>
  2208. #define MAX_IPHDR 128 /* max TCP/IP header size */
  2209. /* The following table contains a hard-coded list of protocol/port pairs.
  2210. * Any matching packets are either discarded unconditionally, or,
  2211. * if ok_if_link_up is non-zero when a connection does not currently exist
  2212. * (i.e., they go through if the connection is present, but never initiate
  2213. * a dial-out).
  2214. * This idea came from a post by dm@garage.uun.org (David Mazieres)
  2215. */
  2216. static struct pktfilt_tab {
  2217. int proto;
  2218. u_short port;
  2219. u_short ok_if_link_up;
  2220. } pktfilt_tab[] = {
  2221. { IPPROTO_UDP, 520, 1 }, /* RIP, ok to pass if link is up */
  2222. { IPPROTO_UDP, 123, 1 }, /* NTP, don't keep up the link for it */
  2223. { -1, 0, 0 } /* terminator entry has port == -1 */
  2224. };
  2225. /*
  2226. * Packet has already been freed if return value is 0.
  2227. */
  2228. static int
  2229. ip_hard_filter(us, mp, outbound)
  2230. upperstr_t *us;
  2231. mblk_t *mp;
  2232. int outbound;
  2233. {
  2234. struct ip *ip;
  2235. struct pktfilt_tab *pft;
  2236. mblk_t *temp_mp;
  2237. int proto;
  2238. int len, hlen;
  2239. /* Note, the PPP header has already been pulled up in all cases */
  2240. proto = PPP_PROTOCOL(mp->b_rptr);
  2241. if (us->flags & US_DBGLOG)
  2242. DPRINT3("ppp/%d: filter, proto=0x%x, out=%d\n", us->mn, proto, outbound);
  2243. switch (proto)
  2244. {
  2245. case PPP_IP:
  2246. if ((mp->b_wptr - mp->b_rptr) == PPP_HDRLEN && mp->b_cont != 0) {
  2247. temp_mp = mp->b_cont;
  2248. len = msgdsize(temp_mp);
  2249. hlen = (len < MAX_IPHDR) ? len : MAX_IPHDR;
  2250. PULLUP(temp_mp, hlen);
  2251. if (temp_mp == 0) {
  2252. DPRINT2("ppp/%d: filter, pullup next failed, len=%d\n",
  2253. us->mn, hlen);
  2254. mp->b_cont = 0; /* PULLUP() freed the rest */
  2255. freemsg(mp);
  2256. return 0;
  2257. }
  2258. ip = (struct ip *)mp->b_cont->b_rptr;
  2259. }
  2260. else {
  2261. len = msgdsize(mp);
  2262. hlen = (len < (PPP_HDRLEN+MAX_IPHDR)) ? len : (PPP_HDRLEN+MAX_IPHDR);
  2263. PULLUP(mp, hlen);
  2264. if (mp == 0) {
  2265. DPRINT2("ppp/%d: filter, pullup failed, len=%d\n",
  2266. us->mn, hlen);
  2267. return 0;
  2268. }
  2269. ip = (struct ip *)(mp->b_rptr + PPP_HDRLEN);
  2270. }
  2271. /* For IP traffic, certain packets (e.g., RIP) may be either
  2272. * 1. ignored - dropped completely
  2273. * 2. will not initiate a connection, but
  2274. * will be passed if a connection is currently up.
  2275. */
  2276. for (pft=pktfilt_tab; pft->proto != -1; pft++) {
  2277. if (ip->ip_p == pft->proto) {
  2278. switch(pft->proto) {
  2279. case IPPROTO_UDP:
  2280. if (((struct udphdr *) &((int *)ip)[ip->ip_hl])->uh_dport
  2281. == htons(pft->port)) goto endfor;
  2282. break;
  2283. case IPPROTO_TCP:
  2284. if (((struct tcphdr *) &((int *)ip)[ip->ip_hl])->th_dport
  2285. == htons(pft->port)) goto endfor;
  2286. break;
  2287. }
  2288. }
  2289. }
  2290. endfor:
  2291. if (pft->proto != -1) {
  2292. if (us->flags & US_DBGLOG)
  2293. DPRINT3("ppp/%d: found IP pkt, proto=0x%x (%d)\n",
  2294. us->mn, pft->proto, pft->port);
  2295. /* Discard if not connected, or if not pass_with_link_up */
  2296. /* else, if link is up let go by, but don't update time */
  2297. if (pft->ok_if_link_up)
  2298. return -1;
  2299. freemsg(mp);
  2300. return 0;
  2301. }
  2302. break;
  2303. } /* end switch (proto) */
  2304. return 1;
  2305. }
  2306. #endif /* FILTER_PACKETS */