ppp.c 61 KB

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