ppp_generic.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic PPP layer for Linux.
  4. *
  5. * Copyright 1999-2002 Paul Mackerras.
  6. *
  7. * The generic PPP layer handles the PPP network interfaces, the
  8. * /dev/ppp device, packet and VJ compression, and multilink.
  9. * It talks to PPP `channels' via the interface defined in
  10. * include/linux/ppp_channel.h. Channels provide the basic means for
  11. * sending and receiving PPP frames on some kind of communications
  12. * channel.
  13. *
  14. * Part of the code in this driver was inspired by the old async-only
  15. * PPP driver, written by Michael Callahan and Al Longyear, and
  16. * subsequently hacked by Paul Mackerras.
  17. *
  18. * ==FILEVERSION 20041108==
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/kmod.h>
  24. #include <linux/init.h>
  25. #include <linux/list.h>
  26. #include <linux/idr.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/poll.h>
  29. #include <linux/ppp_defs.h>
  30. #include <linux/filter.h>
  31. #include <linux/ppp-ioctl.h>
  32. #include <linux/ppp_channel.h>
  33. #include <linux/ppp-comp.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/if_arp.h>
  37. #include <linux/ip.h>
  38. #include <linux/tcp.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/rwsem.h>
  41. #include <linux/stddef.h>
  42. #include <linux/device.h>
  43. #include <linux/mutex.h>
  44. #include <linux/slab.h>
  45. #include <linux/file.h>
  46. #include <asm/unaligned.h>
  47. #include <net/slhc_vj.h>
  48. #include <linux/atomic.h>
  49. #include <linux/refcount.h>
  50. #include <linux/nsproxy.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #define PPP_VERSION "2.4.2"
  54. /*
  55. * Network protocols we support.
  56. */
  57. #define NP_IP 0 /* Internet Protocol V4 */
  58. #define NP_IPV6 1 /* Internet Protocol V6 */
  59. #define NP_IPX 2 /* IPX protocol */
  60. #define NP_AT 3 /* Appletalk protocol */
  61. #define NP_MPLS_UC 4 /* MPLS unicast */
  62. #define NP_MPLS_MC 5 /* MPLS multicast */
  63. #define NUM_NP 6 /* Number of NPs. */
  64. #define MPHDRLEN 6 /* multilink protocol header length */
  65. #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */
  66. #define PPP_PROTO_LEN 2
  67. /*
  68. * An instance of /dev/ppp can be associated with either a ppp
  69. * interface unit or a ppp channel. In both cases, file->private_data
  70. * points to one of these.
  71. */
  72. struct ppp_file {
  73. enum {
  74. INTERFACE=1, CHANNEL
  75. } kind;
  76. struct sk_buff_head xq; /* pppd transmit queue */
  77. struct sk_buff_head rq; /* receive queue for pppd */
  78. wait_queue_head_t rwait; /* for poll on reading /dev/ppp */
  79. refcount_t refcnt; /* # refs (incl /dev/ppp attached) */
  80. int hdrlen; /* space to leave for headers */
  81. int index; /* interface unit / channel number */
  82. int dead; /* unit/channel has been shut down */
  83. };
  84. #define PF_TO_X(pf, X) container_of(pf, X, file)
  85. #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp)
  86. #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel)
  87. /*
  88. * Data structure to hold primary network stats for which
  89. * we want to use 64 bit storage. Other network stats
  90. * are stored in dev->stats of the ppp strucute.
  91. */
  92. struct ppp_link_stats {
  93. u64 rx_packets;
  94. u64 tx_packets;
  95. u64 rx_bytes;
  96. u64 tx_bytes;
  97. };
  98. /*
  99. * Data structure describing one ppp unit.
  100. * A ppp unit corresponds to a ppp network interface device
  101. * and represents a multilink bundle.
  102. * It can have 0 or more ppp channels connected to it.
  103. */
  104. struct ppp {
  105. struct ppp_file file; /* stuff for read/write/poll 0 */
  106. struct file *owner; /* file that owns this unit 48 */
  107. struct list_head channels; /* list of attached channels 4c */
  108. int n_channels; /* how many channels are attached 54 */
  109. spinlock_t rlock; /* lock for receive side 58 */
  110. spinlock_t wlock; /* lock for transmit side 5c */
  111. int __percpu *xmit_recursion; /* xmit recursion detect */
  112. int mru; /* max receive unit 60 */
  113. unsigned int flags; /* control bits 64 */
  114. unsigned int xstate; /* transmit state bits 68 */
  115. unsigned int rstate; /* receive state bits 6c */
  116. int debug; /* debug flags 70 */
  117. struct slcompress *vj; /* state for VJ header compression */
  118. enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */
  119. struct sk_buff *xmit_pending; /* a packet ready to go out 88 */
  120. struct compressor *xcomp; /* transmit packet compressor 8c */
  121. void *xc_state; /* its internal state 90 */
  122. struct compressor *rcomp; /* receive decompressor 94 */
  123. void *rc_state; /* its internal state 98 */
  124. unsigned long last_xmit; /* jiffies when last pkt sent 9c */
  125. unsigned long last_recv; /* jiffies when last pkt rcvd a0 */
  126. struct net_device *dev; /* network interface device a4 */
  127. int closing; /* is device closing down? a8 */
  128. #ifdef CONFIG_PPP_MULTILINK
  129. int nxchan; /* next channel to send something on */
  130. u32 nxseq; /* next sequence number to send */
  131. int mrru; /* MP: max reconst. receive unit */
  132. u32 nextseq; /* MP: seq no of next packet */
  133. u32 minseq; /* MP: min of most recent seqnos */
  134. struct sk_buff_head mrq; /* MP: receive reconstruction queue */
  135. #endif /* CONFIG_PPP_MULTILINK */
  136. #ifdef CONFIG_PPP_FILTER
  137. struct bpf_prog *pass_filter; /* filter for packets to pass */
  138. struct bpf_prog *active_filter; /* filter for pkts to reset idle */
  139. #endif /* CONFIG_PPP_FILTER */
  140. struct net *ppp_net; /* the net we belong to */
  141. struct ppp_link_stats stats64; /* 64 bit network stats */
  142. };
  143. /*
  144. * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
  145. * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
  146. * SC_MUST_COMP
  147. * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
  148. * Bits in xstate: SC_COMP_RUN
  149. */
  150. #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
  151. |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
  152. |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
  153. /*
  154. * Private data structure for each channel.
  155. * This includes the data structure used for multilink.
  156. */
  157. struct channel {
  158. struct ppp_file file; /* stuff for read/write/poll */
  159. struct list_head list; /* link in all/new_channels list */
  160. struct ppp_channel *chan; /* public channel data structure */
  161. struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */
  162. spinlock_t downl; /* protects `chan', file.xq dequeue */
  163. struct ppp *ppp; /* ppp unit we're connected to */
  164. struct net *chan_net; /* the net channel belongs to */
  165. netns_tracker ns_tracker;
  166. struct list_head clist; /* link in list of channels per unit */
  167. rwlock_t upl; /* protects `ppp' and 'bridge' */
  168. struct channel __rcu *bridge; /* "bridged" ppp channel */
  169. #ifdef CONFIG_PPP_MULTILINK
  170. u8 avail; /* flag used in multilink stuff */
  171. u8 had_frag; /* >= 1 fragments have been sent */
  172. u32 lastseq; /* MP: last sequence # received */
  173. int speed; /* speed of the corresponding ppp channel*/
  174. #endif /* CONFIG_PPP_MULTILINK */
  175. };
  176. struct ppp_config {
  177. struct file *file;
  178. s32 unit;
  179. bool ifname_is_set;
  180. };
  181. /*
  182. * SMP locking issues:
  183. * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
  184. * list and the ppp.n_channels field, you need to take both locks
  185. * before you modify them.
  186. * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
  187. * channel.downl.
  188. */
  189. static DEFINE_MUTEX(ppp_mutex);
  190. static atomic_t ppp_unit_count = ATOMIC_INIT(0);
  191. static atomic_t channel_count = ATOMIC_INIT(0);
  192. /* per-net private data for this module */
  193. static unsigned int ppp_net_id __read_mostly;
  194. struct ppp_net {
  195. /* units to ppp mapping */
  196. struct idr units_idr;
  197. /*
  198. * all_ppp_mutex protects the units_idr mapping.
  199. * It also ensures that finding a ppp unit in the units_idr
  200. * map and updating its file.refcnt field is atomic.
  201. */
  202. struct mutex all_ppp_mutex;
  203. /* channels */
  204. struct list_head all_channels;
  205. struct list_head new_channels;
  206. int last_channel_index;
  207. /*
  208. * all_channels_lock protects all_channels and
  209. * last_channel_index, and the atomicity of find
  210. * a channel and updating its file.refcnt field.
  211. */
  212. spinlock_t all_channels_lock;
  213. };
  214. /* Get the PPP protocol number from a skb */
  215. #define PPP_PROTO(skb) get_unaligned_be16((skb)->data)
  216. /* We limit the length of ppp->file.rq to this (arbitrary) value */
  217. #define PPP_MAX_RQLEN 32
  218. /*
  219. * Maximum number of multilink fragments queued up.
  220. * This has to be large enough to cope with the maximum latency of
  221. * the slowest channel relative to the others. Strictly it should
  222. * depend on the number of channels and their characteristics.
  223. */
  224. #define PPP_MP_MAX_QLEN 128
  225. /* Multilink header bits. */
  226. #define B 0x80 /* this fragment begins a packet */
  227. #define E 0x40 /* this fragment ends a packet */
  228. /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
  229. #define seq_before(a, b) ((s32)((a) - (b)) < 0)
  230. #define seq_after(a, b) ((s32)((a) - (b)) > 0)
  231. /* Prototypes. */
  232. static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
  233. struct file *file, unsigned int cmd, unsigned long arg);
  234. static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb);
  235. static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
  236. static void ppp_push(struct ppp *ppp);
  237. static void ppp_channel_push(struct channel *pch);
  238. static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
  239. struct channel *pch);
  240. static void ppp_receive_error(struct ppp *ppp);
  241. static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
  242. static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
  243. struct sk_buff *skb);
  244. #ifdef CONFIG_PPP_MULTILINK
  245. static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
  246. struct channel *pch);
  247. static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
  248. static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
  249. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
  250. #endif /* CONFIG_PPP_MULTILINK */
  251. static int ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data);
  252. static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
  253. static void ppp_ccp_closed(struct ppp *ppp);
  254. static struct compressor *find_compressor(int type);
  255. static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
  256. static int ppp_create_interface(struct net *net, struct file *file, int *unit);
  257. static void init_ppp_file(struct ppp_file *pf, int kind);
  258. static void ppp_destroy_interface(struct ppp *ppp);
  259. static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
  260. static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
  261. static int ppp_connect_channel(struct channel *pch, int unit);
  262. static int ppp_disconnect_channel(struct channel *pch);
  263. static void ppp_destroy_channel(struct channel *pch);
  264. static int unit_get(struct idr *p, void *ptr, int min);
  265. static int unit_set(struct idr *p, void *ptr, int n);
  266. static void unit_put(struct idr *p, int n);
  267. static void *unit_find(struct idr *p, int n);
  268. static void ppp_setup(struct net_device *dev);
  269. static const struct net_device_ops ppp_netdev_ops;
  270. static struct class *ppp_class;
  271. /* per net-namespace data */
  272. static inline struct ppp_net *ppp_pernet(struct net *net)
  273. {
  274. return net_generic(net, ppp_net_id);
  275. }
  276. /* Translates a PPP protocol number to a NP index (NP == network protocol) */
  277. static inline int proto_to_npindex(int proto)
  278. {
  279. switch (proto) {
  280. case PPP_IP:
  281. return NP_IP;
  282. case PPP_IPV6:
  283. return NP_IPV6;
  284. case PPP_IPX:
  285. return NP_IPX;
  286. case PPP_AT:
  287. return NP_AT;
  288. case PPP_MPLS_UC:
  289. return NP_MPLS_UC;
  290. case PPP_MPLS_MC:
  291. return NP_MPLS_MC;
  292. }
  293. return -EINVAL;
  294. }
  295. /* Translates an NP index into a PPP protocol number */
  296. static const int npindex_to_proto[NUM_NP] = {
  297. PPP_IP,
  298. PPP_IPV6,
  299. PPP_IPX,
  300. PPP_AT,
  301. PPP_MPLS_UC,
  302. PPP_MPLS_MC,
  303. };
  304. /* Translates an ethertype into an NP index */
  305. static inline int ethertype_to_npindex(int ethertype)
  306. {
  307. switch (ethertype) {
  308. case ETH_P_IP:
  309. return NP_IP;
  310. case ETH_P_IPV6:
  311. return NP_IPV6;
  312. case ETH_P_IPX:
  313. return NP_IPX;
  314. case ETH_P_PPPTALK:
  315. case ETH_P_ATALK:
  316. return NP_AT;
  317. case ETH_P_MPLS_UC:
  318. return NP_MPLS_UC;
  319. case ETH_P_MPLS_MC:
  320. return NP_MPLS_MC;
  321. }
  322. return -1;
  323. }
  324. /* Translates an NP index into an ethertype */
  325. static const int npindex_to_ethertype[NUM_NP] = {
  326. ETH_P_IP,
  327. ETH_P_IPV6,
  328. ETH_P_IPX,
  329. ETH_P_PPPTALK,
  330. ETH_P_MPLS_UC,
  331. ETH_P_MPLS_MC,
  332. };
  333. /*
  334. * Locking shorthand.
  335. */
  336. #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock)
  337. #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock)
  338. #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock)
  339. #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock)
  340. #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \
  341. ppp_recv_lock(ppp); } while (0)
  342. #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \
  343. ppp_xmit_unlock(ppp); } while (0)
  344. /*
  345. * /dev/ppp device routines.
  346. * The /dev/ppp device is used by pppd to control the ppp unit.
  347. * It supports the read, write, ioctl and poll functions.
  348. * Open instances of /dev/ppp can be in one of three states:
  349. * unattached, attached to a ppp unit, or attached to a ppp channel.
  350. */
  351. static int ppp_open(struct inode *inode, struct file *file)
  352. {
  353. /*
  354. * This could (should?) be enforced by the permissions on /dev/ppp.
  355. */
  356. if (!ns_capable(file->f_cred->user_ns, CAP_NET_ADMIN))
  357. return -EPERM;
  358. return 0;
  359. }
  360. static int ppp_release(struct inode *unused, struct file *file)
  361. {
  362. struct ppp_file *pf = file->private_data;
  363. struct ppp *ppp;
  364. if (pf) {
  365. file->private_data = NULL;
  366. if (pf->kind == INTERFACE) {
  367. ppp = PF_TO_PPP(pf);
  368. rtnl_lock();
  369. if (file == ppp->owner)
  370. unregister_netdevice(ppp->dev);
  371. rtnl_unlock();
  372. }
  373. if (refcount_dec_and_test(&pf->refcnt)) {
  374. switch (pf->kind) {
  375. case INTERFACE:
  376. ppp_destroy_interface(PF_TO_PPP(pf));
  377. break;
  378. case CHANNEL:
  379. ppp_destroy_channel(PF_TO_CHANNEL(pf));
  380. break;
  381. }
  382. }
  383. }
  384. return 0;
  385. }
  386. static ssize_t ppp_read(struct file *file, char __user *buf,
  387. size_t count, loff_t *ppos)
  388. {
  389. struct ppp_file *pf = file->private_data;
  390. DECLARE_WAITQUEUE(wait, current);
  391. ssize_t ret;
  392. struct sk_buff *skb = NULL;
  393. struct iovec iov;
  394. struct iov_iter to;
  395. ret = count;
  396. if (!pf)
  397. return -ENXIO;
  398. add_wait_queue(&pf->rwait, &wait);
  399. for (;;) {
  400. set_current_state(TASK_INTERRUPTIBLE);
  401. skb = skb_dequeue(&pf->rq);
  402. if (skb)
  403. break;
  404. ret = 0;
  405. if (pf->dead)
  406. break;
  407. if (pf->kind == INTERFACE) {
  408. /*
  409. * Return 0 (EOF) on an interface that has no
  410. * channels connected, unless it is looping
  411. * network traffic (demand mode).
  412. */
  413. struct ppp *ppp = PF_TO_PPP(pf);
  414. ppp_recv_lock(ppp);
  415. if (ppp->n_channels == 0 &&
  416. (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
  417. ppp_recv_unlock(ppp);
  418. break;
  419. }
  420. ppp_recv_unlock(ppp);
  421. }
  422. ret = -EAGAIN;
  423. if (file->f_flags & O_NONBLOCK)
  424. break;
  425. ret = -ERESTARTSYS;
  426. if (signal_pending(current))
  427. break;
  428. schedule();
  429. }
  430. set_current_state(TASK_RUNNING);
  431. remove_wait_queue(&pf->rwait, &wait);
  432. if (!skb)
  433. goto out;
  434. ret = -EOVERFLOW;
  435. if (skb->len > count)
  436. goto outf;
  437. ret = -EFAULT;
  438. iov.iov_base = buf;
  439. iov.iov_len = count;
  440. iov_iter_init(&to, ITER_DEST, &iov, 1, count);
  441. if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
  442. goto outf;
  443. ret = skb->len;
  444. outf:
  445. kfree_skb(skb);
  446. out:
  447. return ret;
  448. }
  449. static ssize_t ppp_write(struct file *file, const char __user *buf,
  450. size_t count, loff_t *ppos)
  451. {
  452. struct ppp_file *pf = file->private_data;
  453. struct sk_buff *skb;
  454. ssize_t ret;
  455. if (!pf)
  456. return -ENXIO;
  457. /* All PPP packets should start with the 2-byte protocol */
  458. if (count < PPP_PROTO_LEN)
  459. return -EINVAL;
  460. ret = -ENOMEM;
  461. skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
  462. if (!skb)
  463. goto out;
  464. skb_reserve(skb, pf->hdrlen);
  465. ret = -EFAULT;
  466. if (copy_from_user(skb_put(skb, count), buf, count)) {
  467. kfree_skb(skb);
  468. goto out;
  469. }
  470. switch (pf->kind) {
  471. case INTERFACE:
  472. ppp_xmit_process(PF_TO_PPP(pf), skb);
  473. break;
  474. case CHANNEL:
  475. skb_queue_tail(&pf->xq, skb);
  476. ppp_channel_push(PF_TO_CHANNEL(pf));
  477. break;
  478. }
  479. ret = count;
  480. out:
  481. return ret;
  482. }
  483. /* No kernel lock - fine */
  484. static __poll_t ppp_poll(struct file *file, poll_table *wait)
  485. {
  486. struct ppp_file *pf = file->private_data;
  487. __poll_t mask;
  488. if (!pf)
  489. return 0;
  490. poll_wait(file, &pf->rwait, wait);
  491. mask = EPOLLOUT | EPOLLWRNORM;
  492. if (skb_peek(&pf->rq))
  493. mask |= EPOLLIN | EPOLLRDNORM;
  494. if (pf->dead)
  495. mask |= EPOLLHUP;
  496. else if (pf->kind == INTERFACE) {
  497. /* see comment in ppp_read */
  498. struct ppp *ppp = PF_TO_PPP(pf);
  499. ppp_recv_lock(ppp);
  500. if (ppp->n_channels == 0 &&
  501. (ppp->flags & SC_LOOP_TRAFFIC) == 0)
  502. mask |= EPOLLIN | EPOLLRDNORM;
  503. ppp_recv_unlock(ppp);
  504. }
  505. return mask;
  506. }
  507. #ifdef CONFIG_PPP_FILTER
  508. static struct bpf_prog *get_filter(struct sock_fprog *uprog)
  509. {
  510. struct sock_fprog_kern fprog;
  511. struct bpf_prog *res = NULL;
  512. int err;
  513. if (!uprog->len)
  514. return NULL;
  515. /* uprog->len is unsigned short, so no overflow here */
  516. fprog.len = uprog->len;
  517. fprog.filter = memdup_user(uprog->filter,
  518. uprog->len * sizeof(struct sock_filter));
  519. if (IS_ERR(fprog.filter))
  520. return ERR_CAST(fprog.filter);
  521. err = bpf_prog_create(&res, &fprog);
  522. kfree(fprog.filter);
  523. return err ? ERR_PTR(err) : res;
  524. }
  525. static struct bpf_prog *ppp_get_filter(struct sock_fprog __user *p)
  526. {
  527. struct sock_fprog uprog;
  528. if (copy_from_user(&uprog, p, sizeof(struct sock_fprog)))
  529. return ERR_PTR(-EFAULT);
  530. return get_filter(&uprog);
  531. }
  532. #ifdef CONFIG_COMPAT
  533. struct sock_fprog32 {
  534. unsigned short len;
  535. compat_caddr_t filter;
  536. };
  537. #define PPPIOCSPASS32 _IOW('t', 71, struct sock_fprog32)
  538. #define PPPIOCSACTIVE32 _IOW('t', 70, struct sock_fprog32)
  539. static struct bpf_prog *compat_ppp_get_filter(struct sock_fprog32 __user *p)
  540. {
  541. struct sock_fprog32 uprog32;
  542. struct sock_fprog uprog;
  543. if (copy_from_user(&uprog32, p, sizeof(struct sock_fprog32)))
  544. return ERR_PTR(-EFAULT);
  545. uprog.len = uprog32.len;
  546. uprog.filter = compat_ptr(uprog32.filter);
  547. return get_filter(&uprog);
  548. }
  549. #endif
  550. #endif
  551. /* Bridge one PPP channel to another.
  552. * When two channels are bridged, ppp_input on one channel is redirected to
  553. * the other's ops->start_xmit handler.
  554. * In order to safely bridge channels we must reject channels which are already
  555. * part of a bridge instance, or which form part of an existing unit.
  556. * Once successfully bridged, each channel holds a reference on the other
  557. * to prevent it being freed while the bridge is extant.
  558. */
  559. static int ppp_bridge_channels(struct channel *pch, struct channel *pchb)
  560. {
  561. write_lock_bh(&pch->upl);
  562. if (pch->ppp ||
  563. rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl))) {
  564. write_unlock_bh(&pch->upl);
  565. return -EALREADY;
  566. }
  567. refcount_inc(&pchb->file.refcnt);
  568. rcu_assign_pointer(pch->bridge, pchb);
  569. write_unlock_bh(&pch->upl);
  570. write_lock_bh(&pchb->upl);
  571. if (pchb->ppp ||
  572. rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl))) {
  573. write_unlock_bh(&pchb->upl);
  574. goto err_unset;
  575. }
  576. refcount_inc(&pch->file.refcnt);
  577. rcu_assign_pointer(pchb->bridge, pch);
  578. write_unlock_bh(&pchb->upl);
  579. return 0;
  580. err_unset:
  581. write_lock_bh(&pch->upl);
  582. /* Re-read pch->bridge with upl held in case it was modified concurrently */
  583. pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
  584. RCU_INIT_POINTER(pch->bridge, NULL);
  585. write_unlock_bh(&pch->upl);
  586. synchronize_rcu();
  587. if (pchb)
  588. if (refcount_dec_and_test(&pchb->file.refcnt))
  589. ppp_destroy_channel(pchb);
  590. return -EALREADY;
  591. }
  592. static int ppp_unbridge_channels(struct channel *pch)
  593. {
  594. struct channel *pchb, *pchbb;
  595. write_lock_bh(&pch->upl);
  596. pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
  597. if (!pchb) {
  598. write_unlock_bh(&pch->upl);
  599. return -EINVAL;
  600. }
  601. RCU_INIT_POINTER(pch->bridge, NULL);
  602. write_unlock_bh(&pch->upl);
  603. /* Only modify pchb if phcb->bridge points back to pch.
  604. * If not, it implies that there has been a race unbridging (and possibly
  605. * even rebridging) pchb. We should leave pchb alone to avoid either a
  606. * refcount underflow, or breaking another established bridge instance.
  607. */
  608. write_lock_bh(&pchb->upl);
  609. pchbb = rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl));
  610. if (pchbb == pch)
  611. RCU_INIT_POINTER(pchb->bridge, NULL);
  612. write_unlock_bh(&pchb->upl);
  613. synchronize_rcu();
  614. if (pchbb == pch)
  615. if (refcount_dec_and_test(&pch->file.refcnt))
  616. ppp_destroy_channel(pch);
  617. if (refcount_dec_and_test(&pchb->file.refcnt))
  618. ppp_destroy_channel(pchb);
  619. return 0;
  620. }
  621. static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  622. {
  623. struct ppp_file *pf;
  624. struct ppp *ppp;
  625. int err = -EFAULT, val, val2, i;
  626. struct ppp_idle32 idle32;
  627. struct ppp_idle64 idle64;
  628. struct npioctl npi;
  629. int unit, cflags;
  630. struct slcompress *vj;
  631. void __user *argp = (void __user *)arg;
  632. int __user *p = argp;
  633. mutex_lock(&ppp_mutex);
  634. pf = file->private_data;
  635. if (!pf) {
  636. err = ppp_unattached_ioctl(current->nsproxy->net_ns,
  637. pf, file, cmd, arg);
  638. goto out;
  639. }
  640. if (cmd == PPPIOCDETACH) {
  641. /*
  642. * PPPIOCDETACH is no longer supported as it was heavily broken,
  643. * and is only known to have been used by pppd older than
  644. * ppp-2.4.2 (released November 2003).
  645. */
  646. pr_warn_once("%s (%d) used obsolete PPPIOCDETACH ioctl\n",
  647. current->comm, current->pid);
  648. err = -EINVAL;
  649. goto out;
  650. }
  651. if (pf->kind == CHANNEL) {
  652. struct channel *pch, *pchb;
  653. struct ppp_channel *chan;
  654. struct ppp_net *pn;
  655. pch = PF_TO_CHANNEL(pf);
  656. switch (cmd) {
  657. case PPPIOCCONNECT:
  658. if (get_user(unit, p))
  659. break;
  660. err = ppp_connect_channel(pch, unit);
  661. break;
  662. case PPPIOCDISCONN:
  663. err = ppp_disconnect_channel(pch);
  664. break;
  665. case PPPIOCBRIDGECHAN:
  666. if (get_user(unit, p))
  667. break;
  668. err = -ENXIO;
  669. pn = ppp_pernet(current->nsproxy->net_ns);
  670. spin_lock_bh(&pn->all_channels_lock);
  671. pchb = ppp_find_channel(pn, unit);
  672. /* Hold a reference to prevent pchb being freed while
  673. * we establish the bridge.
  674. */
  675. if (pchb)
  676. refcount_inc(&pchb->file.refcnt);
  677. spin_unlock_bh(&pn->all_channels_lock);
  678. if (!pchb)
  679. break;
  680. err = ppp_bridge_channels(pch, pchb);
  681. /* Drop earlier refcount now bridge establishment is complete */
  682. if (refcount_dec_and_test(&pchb->file.refcnt))
  683. ppp_destroy_channel(pchb);
  684. break;
  685. case PPPIOCUNBRIDGECHAN:
  686. err = ppp_unbridge_channels(pch);
  687. break;
  688. default:
  689. down_read(&pch->chan_sem);
  690. chan = pch->chan;
  691. err = -ENOTTY;
  692. if (chan && chan->ops->ioctl)
  693. err = chan->ops->ioctl(chan, cmd, arg);
  694. up_read(&pch->chan_sem);
  695. }
  696. goto out;
  697. }
  698. if (pf->kind != INTERFACE) {
  699. /* can't happen */
  700. pr_err("PPP: not interface or channel??\n");
  701. err = -EINVAL;
  702. goto out;
  703. }
  704. ppp = PF_TO_PPP(pf);
  705. switch (cmd) {
  706. case PPPIOCSMRU:
  707. if (get_user(val, p))
  708. break;
  709. ppp->mru = val;
  710. err = 0;
  711. break;
  712. case PPPIOCSFLAGS:
  713. if (get_user(val, p))
  714. break;
  715. ppp_lock(ppp);
  716. cflags = ppp->flags & ~val;
  717. #ifdef CONFIG_PPP_MULTILINK
  718. if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
  719. ppp->nextseq = 0;
  720. #endif
  721. ppp->flags = val & SC_FLAG_BITS;
  722. ppp_unlock(ppp);
  723. if (cflags & SC_CCP_OPEN)
  724. ppp_ccp_closed(ppp);
  725. err = 0;
  726. break;
  727. case PPPIOCGFLAGS:
  728. val = ppp->flags | ppp->xstate | ppp->rstate;
  729. if (put_user(val, p))
  730. break;
  731. err = 0;
  732. break;
  733. case PPPIOCSCOMPRESS:
  734. {
  735. struct ppp_option_data data;
  736. if (copy_from_user(&data, argp, sizeof(data)))
  737. err = -EFAULT;
  738. else
  739. err = ppp_set_compress(ppp, &data);
  740. break;
  741. }
  742. case PPPIOCGUNIT:
  743. if (put_user(ppp->file.index, p))
  744. break;
  745. err = 0;
  746. break;
  747. case PPPIOCSDEBUG:
  748. if (get_user(val, p))
  749. break;
  750. ppp->debug = val;
  751. err = 0;
  752. break;
  753. case PPPIOCGDEBUG:
  754. if (put_user(ppp->debug, p))
  755. break;
  756. err = 0;
  757. break;
  758. case PPPIOCGIDLE32:
  759. idle32.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
  760. idle32.recv_idle = (jiffies - ppp->last_recv) / HZ;
  761. if (copy_to_user(argp, &idle32, sizeof(idle32)))
  762. break;
  763. err = 0;
  764. break;
  765. case PPPIOCGIDLE64:
  766. idle64.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
  767. idle64.recv_idle = (jiffies - ppp->last_recv) / HZ;
  768. if (copy_to_user(argp, &idle64, sizeof(idle64)))
  769. break;
  770. err = 0;
  771. break;
  772. case PPPIOCSMAXCID:
  773. if (get_user(val, p))
  774. break;
  775. val2 = 15;
  776. if ((val >> 16) != 0) {
  777. val2 = val >> 16;
  778. val &= 0xffff;
  779. }
  780. vj = slhc_init(val2+1, val+1);
  781. if (IS_ERR(vj)) {
  782. err = PTR_ERR(vj);
  783. break;
  784. }
  785. ppp_lock(ppp);
  786. if (ppp->vj)
  787. slhc_free(ppp->vj);
  788. ppp->vj = vj;
  789. ppp_unlock(ppp);
  790. err = 0;
  791. break;
  792. case PPPIOCGNPMODE:
  793. case PPPIOCSNPMODE:
  794. if (copy_from_user(&npi, argp, sizeof(npi)))
  795. break;
  796. err = proto_to_npindex(npi.protocol);
  797. if (err < 0)
  798. break;
  799. i = err;
  800. if (cmd == PPPIOCGNPMODE) {
  801. err = -EFAULT;
  802. npi.mode = ppp->npmode[i];
  803. if (copy_to_user(argp, &npi, sizeof(npi)))
  804. break;
  805. } else {
  806. ppp->npmode[i] = npi.mode;
  807. /* we may be able to transmit more packets now (??) */
  808. netif_wake_queue(ppp->dev);
  809. }
  810. err = 0;
  811. break;
  812. #ifdef CONFIG_PPP_FILTER
  813. case PPPIOCSPASS:
  814. case PPPIOCSACTIVE:
  815. {
  816. struct bpf_prog *filter = ppp_get_filter(argp);
  817. struct bpf_prog **which;
  818. if (IS_ERR(filter)) {
  819. err = PTR_ERR(filter);
  820. break;
  821. }
  822. if (cmd == PPPIOCSPASS)
  823. which = &ppp->pass_filter;
  824. else
  825. which = &ppp->active_filter;
  826. ppp_lock(ppp);
  827. if (*which)
  828. bpf_prog_destroy(*which);
  829. *which = filter;
  830. ppp_unlock(ppp);
  831. err = 0;
  832. break;
  833. }
  834. #endif /* CONFIG_PPP_FILTER */
  835. #ifdef CONFIG_PPP_MULTILINK
  836. case PPPIOCSMRRU:
  837. if (get_user(val, p))
  838. break;
  839. ppp_recv_lock(ppp);
  840. ppp->mrru = val;
  841. ppp_recv_unlock(ppp);
  842. err = 0;
  843. break;
  844. #endif /* CONFIG_PPP_MULTILINK */
  845. default:
  846. err = -ENOTTY;
  847. }
  848. out:
  849. mutex_unlock(&ppp_mutex);
  850. return err;
  851. }
  852. #ifdef CONFIG_COMPAT
  853. struct ppp_option_data32 {
  854. compat_uptr_t ptr;
  855. u32 length;
  856. compat_int_t transmit;
  857. };
  858. #define PPPIOCSCOMPRESS32 _IOW('t', 77, struct ppp_option_data32)
  859. static long ppp_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  860. {
  861. struct ppp_file *pf;
  862. int err = -ENOIOCTLCMD;
  863. void __user *argp = (void __user *)arg;
  864. mutex_lock(&ppp_mutex);
  865. pf = file->private_data;
  866. if (pf && pf->kind == INTERFACE) {
  867. struct ppp *ppp = PF_TO_PPP(pf);
  868. switch (cmd) {
  869. #ifdef CONFIG_PPP_FILTER
  870. case PPPIOCSPASS32:
  871. case PPPIOCSACTIVE32:
  872. {
  873. struct bpf_prog *filter = compat_ppp_get_filter(argp);
  874. struct bpf_prog **which;
  875. if (IS_ERR(filter)) {
  876. err = PTR_ERR(filter);
  877. break;
  878. }
  879. if (cmd == PPPIOCSPASS32)
  880. which = &ppp->pass_filter;
  881. else
  882. which = &ppp->active_filter;
  883. ppp_lock(ppp);
  884. if (*which)
  885. bpf_prog_destroy(*which);
  886. *which = filter;
  887. ppp_unlock(ppp);
  888. err = 0;
  889. break;
  890. }
  891. #endif /* CONFIG_PPP_FILTER */
  892. case PPPIOCSCOMPRESS32:
  893. {
  894. struct ppp_option_data32 data32;
  895. if (copy_from_user(&data32, argp, sizeof(data32))) {
  896. err = -EFAULT;
  897. } else {
  898. struct ppp_option_data data = {
  899. .ptr = compat_ptr(data32.ptr),
  900. .length = data32.length,
  901. .transmit = data32.transmit
  902. };
  903. err = ppp_set_compress(ppp, &data);
  904. }
  905. break;
  906. }
  907. }
  908. }
  909. mutex_unlock(&ppp_mutex);
  910. /* all other commands have compatible arguments */
  911. if (err == -ENOIOCTLCMD)
  912. err = ppp_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  913. return err;
  914. }
  915. #endif
  916. static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
  917. struct file *file, unsigned int cmd, unsigned long arg)
  918. {
  919. int unit, err = -EFAULT;
  920. struct ppp *ppp;
  921. struct channel *chan;
  922. struct ppp_net *pn;
  923. int __user *p = (int __user *)arg;
  924. switch (cmd) {
  925. case PPPIOCNEWUNIT:
  926. /* Create a new ppp unit */
  927. if (get_user(unit, p))
  928. break;
  929. err = ppp_create_interface(net, file, &unit);
  930. if (err < 0)
  931. break;
  932. err = -EFAULT;
  933. if (put_user(unit, p))
  934. break;
  935. err = 0;
  936. break;
  937. case PPPIOCATTACH:
  938. /* Attach to an existing ppp unit */
  939. if (get_user(unit, p))
  940. break;
  941. err = -ENXIO;
  942. pn = ppp_pernet(net);
  943. mutex_lock(&pn->all_ppp_mutex);
  944. ppp = ppp_find_unit(pn, unit);
  945. if (ppp) {
  946. refcount_inc(&ppp->file.refcnt);
  947. file->private_data = &ppp->file;
  948. err = 0;
  949. }
  950. mutex_unlock(&pn->all_ppp_mutex);
  951. break;
  952. case PPPIOCATTCHAN:
  953. if (get_user(unit, p))
  954. break;
  955. err = -ENXIO;
  956. pn = ppp_pernet(net);
  957. spin_lock_bh(&pn->all_channels_lock);
  958. chan = ppp_find_channel(pn, unit);
  959. if (chan) {
  960. refcount_inc(&chan->file.refcnt);
  961. file->private_data = &chan->file;
  962. err = 0;
  963. }
  964. spin_unlock_bh(&pn->all_channels_lock);
  965. break;
  966. default:
  967. err = -ENOTTY;
  968. }
  969. return err;
  970. }
  971. static const struct file_operations ppp_device_fops = {
  972. .owner = THIS_MODULE,
  973. .read = ppp_read,
  974. .write = ppp_write,
  975. .poll = ppp_poll,
  976. .unlocked_ioctl = ppp_ioctl,
  977. #ifdef CONFIG_COMPAT
  978. .compat_ioctl = ppp_compat_ioctl,
  979. #endif
  980. .open = ppp_open,
  981. .release = ppp_release,
  982. .llseek = noop_llseek,
  983. };
  984. static __net_init int ppp_init_net(struct net *net)
  985. {
  986. struct ppp_net *pn = net_generic(net, ppp_net_id);
  987. idr_init(&pn->units_idr);
  988. mutex_init(&pn->all_ppp_mutex);
  989. INIT_LIST_HEAD(&pn->all_channels);
  990. INIT_LIST_HEAD(&pn->new_channels);
  991. spin_lock_init(&pn->all_channels_lock);
  992. return 0;
  993. }
  994. static __net_exit void ppp_exit_net(struct net *net)
  995. {
  996. struct ppp_net *pn = net_generic(net, ppp_net_id);
  997. struct net_device *dev;
  998. struct net_device *aux;
  999. struct ppp *ppp;
  1000. LIST_HEAD(list);
  1001. int id;
  1002. rtnl_lock();
  1003. for_each_netdev_safe(net, dev, aux) {
  1004. if (dev->netdev_ops == &ppp_netdev_ops)
  1005. unregister_netdevice_queue(dev, &list);
  1006. }
  1007. idr_for_each_entry(&pn->units_idr, ppp, id)
  1008. /* Skip devices already unregistered by previous loop */
  1009. if (!net_eq(dev_net(ppp->dev), net))
  1010. unregister_netdevice_queue(ppp->dev, &list);
  1011. unregister_netdevice_many(&list);
  1012. rtnl_unlock();
  1013. mutex_destroy(&pn->all_ppp_mutex);
  1014. idr_destroy(&pn->units_idr);
  1015. WARN_ON_ONCE(!list_empty(&pn->all_channels));
  1016. WARN_ON_ONCE(!list_empty(&pn->new_channels));
  1017. }
  1018. static struct pernet_operations ppp_net_ops = {
  1019. .init = ppp_init_net,
  1020. .exit = ppp_exit_net,
  1021. .id = &ppp_net_id,
  1022. .size = sizeof(struct ppp_net),
  1023. };
  1024. static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
  1025. {
  1026. struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
  1027. int ret;
  1028. mutex_lock(&pn->all_ppp_mutex);
  1029. if (unit < 0) {
  1030. ret = unit_get(&pn->units_idr, ppp, 0);
  1031. if (ret < 0)
  1032. goto err;
  1033. if (!ifname_is_set) {
  1034. while (1) {
  1035. snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ret);
  1036. if (!netdev_name_in_use(ppp->ppp_net, ppp->dev->name))
  1037. break;
  1038. unit_put(&pn->units_idr, ret);
  1039. ret = unit_get(&pn->units_idr, ppp, ret + 1);
  1040. if (ret < 0)
  1041. goto err;
  1042. }
  1043. }
  1044. } else {
  1045. /* Caller asked for a specific unit number. Fail with -EEXIST
  1046. * if unavailable. For backward compatibility, return -EEXIST
  1047. * too if idr allocation fails; this makes pppd retry without
  1048. * requesting a specific unit number.
  1049. */
  1050. if (unit_find(&pn->units_idr, unit)) {
  1051. ret = -EEXIST;
  1052. goto err;
  1053. }
  1054. ret = unit_set(&pn->units_idr, ppp, unit);
  1055. if (ret < 0) {
  1056. /* Rewrite error for backward compatibility */
  1057. ret = -EEXIST;
  1058. goto err;
  1059. }
  1060. }
  1061. ppp->file.index = ret;
  1062. if (!ifname_is_set)
  1063. snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
  1064. mutex_unlock(&pn->all_ppp_mutex);
  1065. ret = register_netdevice(ppp->dev);
  1066. if (ret < 0)
  1067. goto err_unit;
  1068. atomic_inc(&ppp_unit_count);
  1069. return 0;
  1070. err_unit:
  1071. mutex_lock(&pn->all_ppp_mutex);
  1072. unit_put(&pn->units_idr, ppp->file.index);
  1073. err:
  1074. mutex_unlock(&pn->all_ppp_mutex);
  1075. return ret;
  1076. }
  1077. static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
  1078. const struct ppp_config *conf)
  1079. {
  1080. struct ppp *ppp = netdev_priv(dev);
  1081. int indx;
  1082. int err;
  1083. int cpu;
  1084. ppp->dev = dev;
  1085. ppp->ppp_net = src_net;
  1086. ppp->mru = PPP_MRU;
  1087. ppp->owner = conf->file;
  1088. init_ppp_file(&ppp->file, INTERFACE);
  1089. ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
  1090. for (indx = 0; indx < NUM_NP; ++indx)
  1091. ppp->npmode[indx] = NPMODE_PASS;
  1092. INIT_LIST_HEAD(&ppp->channels);
  1093. spin_lock_init(&ppp->rlock);
  1094. spin_lock_init(&ppp->wlock);
  1095. ppp->xmit_recursion = alloc_percpu(int);
  1096. if (!ppp->xmit_recursion) {
  1097. err = -ENOMEM;
  1098. goto err1;
  1099. }
  1100. for_each_possible_cpu(cpu)
  1101. (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;
  1102. #ifdef CONFIG_PPP_MULTILINK
  1103. ppp->minseq = -1;
  1104. skb_queue_head_init(&ppp->mrq);
  1105. #endif /* CONFIG_PPP_MULTILINK */
  1106. #ifdef CONFIG_PPP_FILTER
  1107. ppp->pass_filter = NULL;
  1108. ppp->active_filter = NULL;
  1109. #endif /* CONFIG_PPP_FILTER */
  1110. err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
  1111. if (err < 0)
  1112. goto err2;
  1113. conf->file->private_data = &ppp->file;
  1114. return 0;
  1115. err2:
  1116. free_percpu(ppp->xmit_recursion);
  1117. err1:
  1118. return err;
  1119. }
  1120. static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
  1121. [IFLA_PPP_DEV_FD] = { .type = NLA_S32 },
  1122. };
  1123. static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],
  1124. struct netlink_ext_ack *extack)
  1125. {
  1126. if (!data)
  1127. return -EINVAL;
  1128. if (!data[IFLA_PPP_DEV_FD])
  1129. return -EINVAL;
  1130. if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
  1131. return -EBADF;
  1132. return 0;
  1133. }
  1134. static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
  1135. struct nlattr *tb[], struct nlattr *data[],
  1136. struct netlink_ext_ack *extack)
  1137. {
  1138. struct ppp_config conf = {
  1139. .unit = -1,
  1140. .ifname_is_set = true,
  1141. };
  1142. struct file *file;
  1143. int err;
  1144. file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
  1145. if (!file)
  1146. return -EBADF;
  1147. /* rtnl_lock is already held here, but ppp_create_interface() locks
  1148. * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
  1149. * possible deadlock due to lock order inversion, at the cost of
  1150. * pushing the problem back to userspace.
  1151. */
  1152. if (!mutex_trylock(&ppp_mutex)) {
  1153. err = -EBUSY;
  1154. goto out;
  1155. }
  1156. if (file->f_op != &ppp_device_fops || file->private_data) {
  1157. err = -EBADF;
  1158. goto out_unlock;
  1159. }
  1160. conf.file = file;
  1161. /* Don't use device name generated by the rtnetlink layer when ifname
  1162. * isn't specified. Let ppp_dev_configure() set the device name using
  1163. * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
  1164. * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
  1165. */
  1166. if (!tb[IFLA_IFNAME] || !nla_len(tb[IFLA_IFNAME]) || !*(char *)nla_data(tb[IFLA_IFNAME]))
  1167. conf.ifname_is_set = false;
  1168. err = ppp_dev_configure(src_net, dev, &conf);
  1169. out_unlock:
  1170. mutex_unlock(&ppp_mutex);
  1171. out:
  1172. fput(file);
  1173. return err;
  1174. }
  1175. static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
  1176. {
  1177. unregister_netdevice_queue(dev, head);
  1178. }
  1179. static size_t ppp_nl_get_size(const struct net_device *dev)
  1180. {
  1181. return 0;
  1182. }
  1183. static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1184. {
  1185. return 0;
  1186. }
  1187. static struct net *ppp_nl_get_link_net(const struct net_device *dev)
  1188. {
  1189. struct ppp *ppp = netdev_priv(dev);
  1190. return ppp->ppp_net;
  1191. }
  1192. static struct rtnl_link_ops ppp_link_ops __read_mostly = {
  1193. .kind = "ppp",
  1194. .maxtype = IFLA_PPP_MAX,
  1195. .policy = ppp_nl_policy,
  1196. .priv_size = sizeof(struct ppp),
  1197. .setup = ppp_setup,
  1198. .validate = ppp_nl_validate,
  1199. .newlink = ppp_nl_newlink,
  1200. .dellink = ppp_nl_dellink,
  1201. .get_size = ppp_nl_get_size,
  1202. .fill_info = ppp_nl_fill_info,
  1203. .get_link_net = ppp_nl_get_link_net,
  1204. };
  1205. #define PPP_MAJOR 108
  1206. /* Called at boot time if ppp is compiled into the kernel,
  1207. or at module load time (from init_module) if compiled as a module. */
  1208. static int __init ppp_init(void)
  1209. {
  1210. int err;
  1211. pr_info("PPP generic driver version " PPP_VERSION "\n");
  1212. err = register_pernet_device(&ppp_net_ops);
  1213. if (err) {
  1214. pr_err("failed to register PPP pernet device (%d)\n", err);
  1215. goto out;
  1216. }
  1217. err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
  1218. if (err) {
  1219. pr_err("failed to register PPP device (%d)\n", err);
  1220. goto out_net;
  1221. }
  1222. ppp_class = class_create(THIS_MODULE, "ppp");
  1223. if (IS_ERR(ppp_class)) {
  1224. err = PTR_ERR(ppp_class);
  1225. goto out_chrdev;
  1226. }
  1227. err = rtnl_link_register(&ppp_link_ops);
  1228. if (err) {
  1229. pr_err("failed to register rtnetlink PPP handler\n");
  1230. goto out_class;
  1231. }
  1232. /* not a big deal if we fail here :-) */
  1233. device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
  1234. return 0;
  1235. out_class:
  1236. class_destroy(ppp_class);
  1237. out_chrdev:
  1238. unregister_chrdev(PPP_MAJOR, "ppp");
  1239. out_net:
  1240. unregister_pernet_device(&ppp_net_ops);
  1241. out:
  1242. return err;
  1243. }
  1244. /*
  1245. * Network interface unit routines.
  1246. */
  1247. static netdev_tx_t
  1248. ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1249. {
  1250. struct ppp *ppp = netdev_priv(dev);
  1251. int npi, proto;
  1252. unsigned char *pp;
  1253. npi = ethertype_to_npindex(ntohs(skb->protocol));
  1254. if (npi < 0)
  1255. goto outf;
  1256. /* Drop, accept or reject the packet */
  1257. switch (ppp->npmode[npi]) {
  1258. case NPMODE_PASS:
  1259. break;
  1260. case NPMODE_QUEUE:
  1261. /* it would be nice to have a way to tell the network
  1262. system to queue this one up for later. */
  1263. goto outf;
  1264. case NPMODE_DROP:
  1265. case NPMODE_ERROR:
  1266. goto outf;
  1267. }
  1268. /* Put the 2-byte PPP protocol number on the front,
  1269. making sure there is room for the address and control fields. */
  1270. if (skb_cow_head(skb, PPP_HDRLEN))
  1271. goto outf;
  1272. pp = skb_push(skb, 2);
  1273. proto = npindex_to_proto[npi];
  1274. put_unaligned_be16(proto, pp);
  1275. skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
  1276. ppp_xmit_process(ppp, skb);
  1277. return NETDEV_TX_OK;
  1278. outf:
  1279. kfree_skb(skb);
  1280. ++dev->stats.tx_dropped;
  1281. return NETDEV_TX_OK;
  1282. }
  1283. static int
  1284. ppp_net_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
  1285. void __user *addr, int cmd)
  1286. {
  1287. struct ppp *ppp = netdev_priv(dev);
  1288. int err = -EFAULT;
  1289. struct ppp_stats stats;
  1290. struct ppp_comp_stats cstats;
  1291. char *vers;
  1292. switch (cmd) {
  1293. case SIOCGPPPSTATS:
  1294. ppp_get_stats(ppp, &stats);
  1295. if (copy_to_user(addr, &stats, sizeof(stats)))
  1296. break;
  1297. err = 0;
  1298. break;
  1299. case SIOCGPPPCSTATS:
  1300. memset(&cstats, 0, sizeof(cstats));
  1301. if (ppp->xc_state)
  1302. ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
  1303. if (ppp->rc_state)
  1304. ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
  1305. if (copy_to_user(addr, &cstats, sizeof(cstats)))
  1306. break;
  1307. err = 0;
  1308. break;
  1309. case SIOCGPPPVER:
  1310. vers = PPP_VERSION;
  1311. if (copy_to_user(addr, vers, strlen(vers) + 1))
  1312. break;
  1313. err = 0;
  1314. break;
  1315. default:
  1316. err = -EINVAL;
  1317. }
  1318. return err;
  1319. }
  1320. static void
  1321. ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
  1322. {
  1323. struct ppp *ppp = netdev_priv(dev);
  1324. ppp_recv_lock(ppp);
  1325. stats64->rx_packets = ppp->stats64.rx_packets;
  1326. stats64->rx_bytes = ppp->stats64.rx_bytes;
  1327. ppp_recv_unlock(ppp);
  1328. ppp_xmit_lock(ppp);
  1329. stats64->tx_packets = ppp->stats64.tx_packets;
  1330. stats64->tx_bytes = ppp->stats64.tx_bytes;
  1331. ppp_xmit_unlock(ppp);
  1332. stats64->rx_errors = dev->stats.rx_errors;
  1333. stats64->tx_errors = dev->stats.tx_errors;
  1334. stats64->rx_dropped = dev->stats.rx_dropped;
  1335. stats64->tx_dropped = dev->stats.tx_dropped;
  1336. stats64->rx_length_errors = dev->stats.rx_length_errors;
  1337. }
  1338. static int ppp_dev_init(struct net_device *dev)
  1339. {
  1340. struct ppp *ppp;
  1341. netdev_lockdep_set_classes(dev);
  1342. ppp = netdev_priv(dev);
  1343. /* Let the netdevice take a reference on the ppp file. This ensures
  1344. * that ppp_destroy_interface() won't run before the device gets
  1345. * unregistered.
  1346. */
  1347. refcount_inc(&ppp->file.refcnt);
  1348. return 0;
  1349. }
  1350. static void ppp_dev_uninit(struct net_device *dev)
  1351. {
  1352. struct ppp *ppp = netdev_priv(dev);
  1353. struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
  1354. ppp_lock(ppp);
  1355. ppp->closing = 1;
  1356. ppp_unlock(ppp);
  1357. mutex_lock(&pn->all_ppp_mutex);
  1358. unit_put(&pn->units_idr, ppp->file.index);
  1359. mutex_unlock(&pn->all_ppp_mutex);
  1360. ppp->owner = NULL;
  1361. ppp->file.dead = 1;
  1362. wake_up_interruptible(&ppp->file.rwait);
  1363. }
  1364. static void ppp_dev_priv_destructor(struct net_device *dev)
  1365. {
  1366. struct ppp *ppp;
  1367. ppp = netdev_priv(dev);
  1368. if (refcount_dec_and_test(&ppp->file.refcnt))
  1369. ppp_destroy_interface(ppp);
  1370. }
  1371. static int ppp_fill_forward_path(struct net_device_path_ctx *ctx,
  1372. struct net_device_path *path)
  1373. {
  1374. struct ppp *ppp = netdev_priv(ctx->dev);
  1375. struct ppp_channel *chan;
  1376. struct channel *pch;
  1377. if (ppp->flags & SC_MULTILINK)
  1378. return -EOPNOTSUPP;
  1379. if (list_empty(&ppp->channels))
  1380. return -ENODEV;
  1381. pch = list_first_entry(&ppp->channels, struct channel, clist);
  1382. chan = pch->chan;
  1383. if (!chan->ops->fill_forward_path)
  1384. return -EOPNOTSUPP;
  1385. return chan->ops->fill_forward_path(ctx, path, chan);
  1386. }
  1387. static const struct net_device_ops ppp_netdev_ops = {
  1388. .ndo_init = ppp_dev_init,
  1389. .ndo_uninit = ppp_dev_uninit,
  1390. .ndo_start_xmit = ppp_start_xmit,
  1391. .ndo_siocdevprivate = ppp_net_siocdevprivate,
  1392. .ndo_get_stats64 = ppp_get_stats64,
  1393. .ndo_fill_forward_path = ppp_fill_forward_path,
  1394. };
  1395. static struct device_type ppp_type = {
  1396. .name = "ppp",
  1397. };
  1398. static void ppp_setup(struct net_device *dev)
  1399. {
  1400. dev->netdev_ops = &ppp_netdev_ops;
  1401. SET_NETDEV_DEVTYPE(dev, &ppp_type);
  1402. dev->features |= NETIF_F_LLTX;
  1403. dev->hard_header_len = PPP_HDRLEN;
  1404. dev->mtu = PPP_MRU;
  1405. dev->addr_len = 0;
  1406. dev->tx_queue_len = 3;
  1407. dev->type = ARPHRD_PPP;
  1408. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1409. dev->priv_destructor = ppp_dev_priv_destructor;
  1410. netif_keep_dst(dev);
  1411. }
  1412. /*
  1413. * Transmit-side routines.
  1414. */
  1415. /* Called to do any work queued up on the transmit side that can now be done */
  1416. static void __ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
  1417. {
  1418. ppp_xmit_lock(ppp);
  1419. if (!ppp->closing) {
  1420. ppp_push(ppp);
  1421. if (skb)
  1422. skb_queue_tail(&ppp->file.xq, skb);
  1423. while (!ppp->xmit_pending &&
  1424. (skb = skb_dequeue(&ppp->file.xq)))
  1425. ppp_send_frame(ppp, skb);
  1426. /* If there's no work left to do, tell the core net
  1427. code that we can accept some more. */
  1428. if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
  1429. netif_wake_queue(ppp->dev);
  1430. else
  1431. netif_stop_queue(ppp->dev);
  1432. } else {
  1433. kfree_skb(skb);
  1434. }
  1435. ppp_xmit_unlock(ppp);
  1436. }
  1437. static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
  1438. {
  1439. local_bh_disable();
  1440. if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
  1441. goto err;
  1442. (*this_cpu_ptr(ppp->xmit_recursion))++;
  1443. __ppp_xmit_process(ppp, skb);
  1444. (*this_cpu_ptr(ppp->xmit_recursion))--;
  1445. local_bh_enable();
  1446. return;
  1447. err:
  1448. local_bh_enable();
  1449. kfree_skb(skb);
  1450. if (net_ratelimit())
  1451. netdev_err(ppp->dev, "recursion detected\n");
  1452. }
  1453. static inline struct sk_buff *
  1454. pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
  1455. {
  1456. struct sk_buff *new_skb;
  1457. int len;
  1458. int new_skb_size = ppp->dev->mtu +
  1459. ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
  1460. int compressor_skb_size = ppp->dev->mtu +
  1461. ppp->xcomp->comp_extra + PPP_HDRLEN;
  1462. new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
  1463. if (!new_skb) {
  1464. if (net_ratelimit())
  1465. netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
  1466. return NULL;
  1467. }
  1468. if (ppp->dev->hard_header_len > PPP_HDRLEN)
  1469. skb_reserve(new_skb,
  1470. ppp->dev->hard_header_len - PPP_HDRLEN);
  1471. /* compressor still expects A/C bytes in hdr */
  1472. len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
  1473. new_skb->data, skb->len + 2,
  1474. compressor_skb_size);
  1475. if (len > 0 && (ppp->flags & SC_CCP_UP)) {
  1476. consume_skb(skb);
  1477. skb = new_skb;
  1478. skb_put(skb, len);
  1479. skb_pull(skb, 2); /* pull off A/C bytes */
  1480. } else if (len == 0) {
  1481. /* didn't compress, or CCP not up yet */
  1482. consume_skb(new_skb);
  1483. new_skb = skb;
  1484. } else {
  1485. /*
  1486. * (len < 0)
  1487. * MPPE requires that we do not send unencrypted
  1488. * frames. The compressor will return -1 if we
  1489. * should drop the frame. We cannot simply test
  1490. * the compress_proto because MPPE and MPPC share
  1491. * the same number.
  1492. */
  1493. if (net_ratelimit())
  1494. netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
  1495. kfree_skb(skb);
  1496. consume_skb(new_skb);
  1497. new_skb = NULL;
  1498. }
  1499. return new_skb;
  1500. }
  1501. /*
  1502. * Compress and send a frame.
  1503. * The caller should have locked the xmit path,
  1504. * and xmit_pending should be 0.
  1505. */
  1506. static void
  1507. ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
  1508. {
  1509. int proto = PPP_PROTO(skb);
  1510. struct sk_buff *new_skb;
  1511. int len;
  1512. unsigned char *cp;
  1513. skb->dev = ppp->dev;
  1514. if (proto < 0x8000) {
  1515. #ifdef CONFIG_PPP_FILTER
  1516. /* check if we should pass this packet */
  1517. /* the filter instructions are constructed assuming
  1518. a four-byte PPP header on each packet */
  1519. *(u8 *)skb_push(skb, 2) = 1;
  1520. if (ppp->pass_filter &&
  1521. bpf_prog_run(ppp->pass_filter, skb) == 0) {
  1522. if (ppp->debug & 1)
  1523. netdev_printk(KERN_DEBUG, ppp->dev,
  1524. "PPP: outbound frame "
  1525. "not passed\n");
  1526. kfree_skb(skb);
  1527. return;
  1528. }
  1529. /* if this packet passes the active filter, record the time */
  1530. if (!(ppp->active_filter &&
  1531. bpf_prog_run(ppp->active_filter, skb) == 0))
  1532. ppp->last_xmit = jiffies;
  1533. skb_pull(skb, 2);
  1534. #else
  1535. /* for data packets, record the time */
  1536. ppp->last_xmit = jiffies;
  1537. #endif /* CONFIG_PPP_FILTER */
  1538. }
  1539. ++ppp->stats64.tx_packets;
  1540. ppp->stats64.tx_bytes += skb->len - PPP_PROTO_LEN;
  1541. switch (proto) {
  1542. case PPP_IP:
  1543. if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
  1544. break;
  1545. /* try to do VJ TCP header compression */
  1546. new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
  1547. GFP_ATOMIC);
  1548. if (!new_skb) {
  1549. netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
  1550. goto drop;
  1551. }
  1552. skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
  1553. cp = skb->data + 2;
  1554. len = slhc_compress(ppp->vj, cp, skb->len - 2,
  1555. new_skb->data + 2, &cp,
  1556. !(ppp->flags & SC_NO_TCP_CCID));
  1557. if (cp == skb->data + 2) {
  1558. /* didn't compress */
  1559. consume_skb(new_skb);
  1560. } else {
  1561. if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
  1562. proto = PPP_VJC_COMP;
  1563. cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
  1564. } else {
  1565. proto = PPP_VJC_UNCOMP;
  1566. cp[0] = skb->data[2];
  1567. }
  1568. consume_skb(skb);
  1569. skb = new_skb;
  1570. cp = skb_put(skb, len + 2);
  1571. cp[0] = 0;
  1572. cp[1] = proto;
  1573. }
  1574. break;
  1575. case PPP_CCP:
  1576. /* peek at outbound CCP frames */
  1577. ppp_ccp_peek(ppp, skb, 0);
  1578. break;
  1579. }
  1580. /* try to do packet compression */
  1581. if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
  1582. proto != PPP_LCP && proto != PPP_CCP) {
  1583. if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
  1584. if (net_ratelimit())
  1585. netdev_err(ppp->dev,
  1586. "ppp: compression required but "
  1587. "down - pkt dropped.\n");
  1588. goto drop;
  1589. }
  1590. skb = pad_compress_skb(ppp, skb);
  1591. if (!skb)
  1592. goto drop;
  1593. }
  1594. /*
  1595. * If we are waiting for traffic (demand dialling),
  1596. * queue it up for pppd to receive.
  1597. */
  1598. if (ppp->flags & SC_LOOP_TRAFFIC) {
  1599. if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
  1600. goto drop;
  1601. skb_queue_tail(&ppp->file.rq, skb);
  1602. wake_up_interruptible(&ppp->file.rwait);
  1603. return;
  1604. }
  1605. ppp->xmit_pending = skb;
  1606. ppp_push(ppp);
  1607. return;
  1608. drop:
  1609. kfree_skb(skb);
  1610. ++ppp->dev->stats.tx_errors;
  1611. }
  1612. /*
  1613. * Try to send the frame in xmit_pending.
  1614. * The caller should have the xmit path locked.
  1615. */
  1616. static void
  1617. ppp_push(struct ppp *ppp)
  1618. {
  1619. struct list_head *list;
  1620. struct channel *pch;
  1621. struct sk_buff *skb = ppp->xmit_pending;
  1622. if (!skb)
  1623. return;
  1624. list = &ppp->channels;
  1625. if (list_empty(list)) {
  1626. /* nowhere to send the packet, just drop it */
  1627. ppp->xmit_pending = NULL;
  1628. kfree_skb(skb);
  1629. return;
  1630. }
  1631. if ((ppp->flags & SC_MULTILINK) == 0) {
  1632. /* not doing multilink: send it down the first channel */
  1633. list = list->next;
  1634. pch = list_entry(list, struct channel, clist);
  1635. spin_lock(&pch->downl);
  1636. if (pch->chan) {
  1637. if (pch->chan->ops->start_xmit(pch->chan, skb))
  1638. ppp->xmit_pending = NULL;
  1639. } else {
  1640. /* channel got unregistered */
  1641. kfree_skb(skb);
  1642. ppp->xmit_pending = NULL;
  1643. }
  1644. spin_unlock(&pch->downl);
  1645. return;
  1646. }
  1647. #ifdef CONFIG_PPP_MULTILINK
  1648. /* Multilink: fragment the packet over as many links
  1649. as can take the packet at the moment. */
  1650. if (!ppp_mp_explode(ppp, skb))
  1651. return;
  1652. #endif /* CONFIG_PPP_MULTILINK */
  1653. ppp->xmit_pending = NULL;
  1654. kfree_skb(skb);
  1655. }
  1656. #ifdef CONFIG_PPP_MULTILINK
  1657. static bool mp_protocol_compress __read_mostly = true;
  1658. module_param(mp_protocol_compress, bool, 0644);
  1659. MODULE_PARM_DESC(mp_protocol_compress,
  1660. "compress protocol id in multilink fragments");
  1661. /*
  1662. * Divide a packet to be transmitted into fragments and
  1663. * send them out the individual links.
  1664. */
  1665. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
  1666. {
  1667. int len, totlen;
  1668. int i, bits, hdrlen, mtu;
  1669. int flen;
  1670. int navail, nfree, nzero;
  1671. int nbigger;
  1672. int totspeed;
  1673. int totfree;
  1674. unsigned char *p, *q;
  1675. struct list_head *list;
  1676. struct channel *pch;
  1677. struct sk_buff *frag;
  1678. struct ppp_channel *chan;
  1679. totspeed = 0; /*total bitrate of the bundle*/
  1680. nfree = 0; /* # channels which have no packet already queued */
  1681. navail = 0; /* total # of usable channels (not deregistered) */
  1682. nzero = 0; /* number of channels with zero speed associated*/
  1683. totfree = 0; /*total # of channels available and
  1684. *having no queued packets before
  1685. *starting the fragmentation*/
  1686. hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1687. i = 0;
  1688. list_for_each_entry(pch, &ppp->channels, clist) {
  1689. if (pch->chan) {
  1690. pch->avail = 1;
  1691. navail++;
  1692. pch->speed = pch->chan->speed;
  1693. } else {
  1694. pch->avail = 0;
  1695. }
  1696. if (pch->avail) {
  1697. if (skb_queue_empty(&pch->file.xq) ||
  1698. !pch->had_frag) {
  1699. if (pch->speed == 0)
  1700. nzero++;
  1701. else
  1702. totspeed += pch->speed;
  1703. pch->avail = 2;
  1704. ++nfree;
  1705. ++totfree;
  1706. }
  1707. if (!pch->had_frag && i < ppp->nxchan)
  1708. ppp->nxchan = i;
  1709. }
  1710. ++i;
  1711. }
  1712. /*
  1713. * Don't start sending this packet unless at least half of
  1714. * the channels are free. This gives much better TCP
  1715. * performance if we have a lot of channels.
  1716. */
  1717. if (nfree == 0 || nfree < navail / 2)
  1718. return 0; /* can't take now, leave it in xmit_pending */
  1719. /* Do protocol field compression */
  1720. p = skb->data;
  1721. len = skb->len;
  1722. if (*p == 0 && mp_protocol_compress) {
  1723. ++p;
  1724. --len;
  1725. }
  1726. totlen = len;
  1727. nbigger = len % nfree;
  1728. /* skip to the channel after the one we last used
  1729. and start at that one */
  1730. list = &ppp->channels;
  1731. for (i = 0; i < ppp->nxchan; ++i) {
  1732. list = list->next;
  1733. if (list == &ppp->channels) {
  1734. i = 0;
  1735. break;
  1736. }
  1737. }
  1738. /* create a fragment for each channel */
  1739. bits = B;
  1740. while (len > 0) {
  1741. list = list->next;
  1742. if (list == &ppp->channels) {
  1743. i = 0;
  1744. continue;
  1745. }
  1746. pch = list_entry(list, struct channel, clist);
  1747. ++i;
  1748. if (!pch->avail)
  1749. continue;
  1750. /*
  1751. * Skip this channel if it has a fragment pending already and
  1752. * we haven't given a fragment to all of the free channels.
  1753. */
  1754. if (pch->avail == 1) {
  1755. if (nfree > 0)
  1756. continue;
  1757. } else {
  1758. pch->avail = 1;
  1759. }
  1760. /* check the channel's mtu and whether it is still attached. */
  1761. spin_lock(&pch->downl);
  1762. if (pch->chan == NULL) {
  1763. /* can't use this channel, it's being deregistered */
  1764. if (pch->speed == 0)
  1765. nzero--;
  1766. else
  1767. totspeed -= pch->speed;
  1768. spin_unlock(&pch->downl);
  1769. pch->avail = 0;
  1770. totlen = len;
  1771. totfree--;
  1772. nfree--;
  1773. if (--navail == 0)
  1774. break;
  1775. continue;
  1776. }
  1777. /*
  1778. *if the channel speed is not set divide
  1779. *the packet evenly among the free channels;
  1780. *otherwise divide it according to the speed
  1781. *of the channel we are going to transmit on
  1782. */
  1783. flen = len;
  1784. if (nfree > 0) {
  1785. if (pch->speed == 0) {
  1786. flen = len/nfree;
  1787. if (nbigger > 0) {
  1788. flen++;
  1789. nbigger--;
  1790. }
  1791. } else {
  1792. flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
  1793. ((totspeed*totfree)/pch->speed)) - hdrlen;
  1794. if (nbigger > 0) {
  1795. flen += ((totfree - nzero)*pch->speed)/totspeed;
  1796. nbigger -= ((totfree - nzero)*pch->speed)/
  1797. totspeed;
  1798. }
  1799. }
  1800. nfree--;
  1801. }
  1802. /*
  1803. *check if we are on the last channel or
  1804. *we exceded the length of the data to
  1805. *fragment
  1806. */
  1807. if ((nfree <= 0) || (flen > len))
  1808. flen = len;
  1809. /*
  1810. *it is not worth to tx on slow channels:
  1811. *in that case from the resulting flen according to the
  1812. *above formula will be equal or less than zero.
  1813. *Skip the channel in this case
  1814. */
  1815. if (flen <= 0) {
  1816. pch->avail = 2;
  1817. spin_unlock(&pch->downl);
  1818. continue;
  1819. }
  1820. /*
  1821. * hdrlen includes the 2-byte PPP protocol field, but the
  1822. * MTU counts only the payload excluding the protocol field.
  1823. * (RFC1661 Section 2)
  1824. */
  1825. mtu = pch->chan->mtu - (hdrlen - 2);
  1826. if (mtu < 4)
  1827. mtu = 4;
  1828. if (flen > mtu)
  1829. flen = mtu;
  1830. if (flen == len)
  1831. bits |= E;
  1832. frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
  1833. if (!frag)
  1834. goto noskb;
  1835. q = skb_put(frag, flen + hdrlen);
  1836. /* make the MP header */
  1837. put_unaligned_be16(PPP_MP, q);
  1838. if (ppp->flags & SC_MP_XSHORTSEQ) {
  1839. q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
  1840. q[3] = ppp->nxseq;
  1841. } else {
  1842. q[2] = bits;
  1843. q[3] = ppp->nxseq >> 16;
  1844. q[4] = ppp->nxseq >> 8;
  1845. q[5] = ppp->nxseq;
  1846. }
  1847. memcpy(q + hdrlen, p, flen);
  1848. /* try to send it down the channel */
  1849. chan = pch->chan;
  1850. if (!skb_queue_empty(&pch->file.xq) ||
  1851. !chan->ops->start_xmit(chan, frag))
  1852. skb_queue_tail(&pch->file.xq, frag);
  1853. pch->had_frag = 1;
  1854. p += flen;
  1855. len -= flen;
  1856. ++ppp->nxseq;
  1857. bits = 0;
  1858. spin_unlock(&pch->downl);
  1859. }
  1860. ppp->nxchan = i;
  1861. return 1;
  1862. noskb:
  1863. spin_unlock(&pch->downl);
  1864. if (ppp->debug & 1)
  1865. netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
  1866. ++ppp->dev->stats.tx_errors;
  1867. ++ppp->nxseq;
  1868. return 1; /* abandon the frame */
  1869. }
  1870. #endif /* CONFIG_PPP_MULTILINK */
  1871. /* Try to send data out on a channel */
  1872. static void __ppp_channel_push(struct channel *pch)
  1873. {
  1874. struct sk_buff *skb;
  1875. struct ppp *ppp;
  1876. spin_lock(&pch->downl);
  1877. if (pch->chan) {
  1878. while (!skb_queue_empty(&pch->file.xq)) {
  1879. skb = skb_dequeue(&pch->file.xq);
  1880. if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
  1881. /* put the packet back and try again later */
  1882. skb_queue_head(&pch->file.xq, skb);
  1883. break;
  1884. }
  1885. }
  1886. } else {
  1887. /* channel got deregistered */
  1888. skb_queue_purge(&pch->file.xq);
  1889. }
  1890. spin_unlock(&pch->downl);
  1891. /* see if there is anything from the attached unit to be sent */
  1892. if (skb_queue_empty(&pch->file.xq)) {
  1893. ppp = pch->ppp;
  1894. if (ppp)
  1895. __ppp_xmit_process(ppp, NULL);
  1896. }
  1897. }
  1898. static void ppp_channel_push(struct channel *pch)
  1899. {
  1900. read_lock_bh(&pch->upl);
  1901. if (pch->ppp) {
  1902. (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
  1903. __ppp_channel_push(pch);
  1904. (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
  1905. } else {
  1906. __ppp_channel_push(pch);
  1907. }
  1908. read_unlock_bh(&pch->upl);
  1909. }
  1910. /*
  1911. * Receive-side routines.
  1912. */
  1913. struct ppp_mp_skb_parm {
  1914. u32 sequence;
  1915. u8 BEbits;
  1916. };
  1917. #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
  1918. static inline void
  1919. ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1920. {
  1921. ppp_recv_lock(ppp);
  1922. if (!ppp->closing)
  1923. ppp_receive_frame(ppp, skb, pch);
  1924. else
  1925. kfree_skb(skb);
  1926. ppp_recv_unlock(ppp);
  1927. }
  1928. /**
  1929. * __ppp_decompress_proto - Decompress protocol field, slim version.
  1930. * @skb: Socket buffer where protocol field should be decompressed. It must have
  1931. * at least 1 byte of head room and 1 byte of linear data. First byte of
  1932. * data must be a protocol field byte.
  1933. *
  1934. * Decompress protocol field in PPP header if it's compressed, e.g. when
  1935. * Protocol-Field-Compression (PFC) was negotiated. No checks w.r.t. skb data
  1936. * length are done in this function.
  1937. */
  1938. static void __ppp_decompress_proto(struct sk_buff *skb)
  1939. {
  1940. if (skb->data[0] & 0x01)
  1941. *(u8 *)skb_push(skb, 1) = 0x00;
  1942. }
  1943. /**
  1944. * ppp_decompress_proto - Check skb data room and decompress protocol field.
  1945. * @skb: Socket buffer where protocol field should be decompressed. First byte
  1946. * of data must be a protocol field byte.
  1947. *
  1948. * Decompress protocol field in PPP header if it's compressed, e.g. when
  1949. * Protocol-Field-Compression (PFC) was negotiated. This function also makes
  1950. * sure that skb data room is sufficient for Protocol field, before and after
  1951. * decompression.
  1952. *
  1953. * Return: true - decompressed successfully, false - not enough room in skb.
  1954. */
  1955. static bool ppp_decompress_proto(struct sk_buff *skb)
  1956. {
  1957. /* At least one byte should be present (if protocol is compressed) */
  1958. if (!pskb_may_pull(skb, 1))
  1959. return false;
  1960. __ppp_decompress_proto(skb);
  1961. /* Protocol field should occupy 2 bytes when not compressed */
  1962. return pskb_may_pull(skb, 2);
  1963. }
  1964. /* Attempt to handle a frame via. a bridged channel, if one exists.
  1965. * If the channel is bridged, the frame is consumed by the bridge.
  1966. * If not, the caller must handle the frame by normal recv mechanisms.
  1967. * Returns true if the frame is consumed, false otherwise.
  1968. */
  1969. static bool ppp_channel_bridge_input(struct channel *pch, struct sk_buff *skb)
  1970. {
  1971. struct channel *pchb;
  1972. rcu_read_lock();
  1973. pchb = rcu_dereference(pch->bridge);
  1974. if (!pchb)
  1975. goto out_rcu;
  1976. spin_lock(&pchb->downl);
  1977. if (!pchb->chan) {
  1978. /* channel got unregistered */
  1979. kfree_skb(skb);
  1980. goto outl;
  1981. }
  1982. skb_scrub_packet(skb, !net_eq(pch->chan_net, pchb->chan_net));
  1983. if (!pchb->chan->ops->start_xmit(pchb->chan, skb))
  1984. kfree_skb(skb);
  1985. outl:
  1986. spin_unlock(&pchb->downl);
  1987. out_rcu:
  1988. rcu_read_unlock();
  1989. /* If pchb is set then we've consumed the packet */
  1990. return !!pchb;
  1991. }
  1992. void
  1993. ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
  1994. {
  1995. struct channel *pch = chan->ppp;
  1996. int proto;
  1997. if (!pch) {
  1998. kfree_skb(skb);
  1999. return;
  2000. }
  2001. /* If the channel is bridged, transmit via. bridge */
  2002. if (ppp_channel_bridge_input(pch, skb))
  2003. return;
  2004. read_lock_bh(&pch->upl);
  2005. if (!ppp_decompress_proto(skb)) {
  2006. kfree_skb(skb);
  2007. if (pch->ppp) {
  2008. ++pch->ppp->dev->stats.rx_length_errors;
  2009. ppp_receive_error(pch->ppp);
  2010. }
  2011. goto done;
  2012. }
  2013. proto = PPP_PROTO(skb);
  2014. if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
  2015. /* put it on the channel queue */
  2016. skb_queue_tail(&pch->file.rq, skb);
  2017. /* drop old frames if queue too long */
  2018. while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
  2019. (skb = skb_dequeue(&pch->file.rq)))
  2020. kfree_skb(skb);
  2021. wake_up_interruptible(&pch->file.rwait);
  2022. } else {
  2023. ppp_do_recv(pch->ppp, skb, pch);
  2024. }
  2025. done:
  2026. read_unlock_bh(&pch->upl);
  2027. }
  2028. /* Put a 0-length skb in the receive queue as an error indication */
  2029. void
  2030. ppp_input_error(struct ppp_channel *chan, int code)
  2031. {
  2032. struct channel *pch = chan->ppp;
  2033. struct sk_buff *skb;
  2034. if (!pch)
  2035. return;
  2036. read_lock_bh(&pch->upl);
  2037. if (pch->ppp) {
  2038. skb = alloc_skb(0, GFP_ATOMIC);
  2039. if (skb) {
  2040. skb->len = 0; /* probably unnecessary */
  2041. skb->cb[0] = code;
  2042. ppp_do_recv(pch->ppp, skb, pch);
  2043. }
  2044. }
  2045. read_unlock_bh(&pch->upl);
  2046. }
  2047. /*
  2048. * We come in here to process a received frame.
  2049. * The receive side of the ppp unit is locked.
  2050. */
  2051. static void
  2052. ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  2053. {
  2054. /* note: a 0-length skb is used as an error indication */
  2055. if (skb->len > 0) {
  2056. skb_checksum_complete_unset(skb);
  2057. #ifdef CONFIG_PPP_MULTILINK
  2058. /* XXX do channel-level decompression here */
  2059. if (PPP_PROTO(skb) == PPP_MP)
  2060. ppp_receive_mp_frame(ppp, skb, pch);
  2061. else
  2062. #endif /* CONFIG_PPP_MULTILINK */
  2063. ppp_receive_nonmp_frame(ppp, skb);
  2064. } else {
  2065. kfree_skb(skb);
  2066. ppp_receive_error(ppp);
  2067. }
  2068. }
  2069. static void
  2070. ppp_receive_error(struct ppp *ppp)
  2071. {
  2072. ++ppp->dev->stats.rx_errors;
  2073. if (ppp->vj)
  2074. slhc_toss(ppp->vj);
  2075. }
  2076. static void
  2077. ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
  2078. {
  2079. struct sk_buff *ns;
  2080. int proto, len, npi;
  2081. /*
  2082. * Decompress the frame, if compressed.
  2083. * Note that some decompressors need to see uncompressed frames
  2084. * that come in as well as compressed frames.
  2085. */
  2086. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
  2087. (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
  2088. skb = ppp_decompress_frame(ppp, skb);
  2089. if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
  2090. goto err;
  2091. /* At this point the "Protocol" field MUST be decompressed, either in
  2092. * ppp_input(), ppp_decompress_frame() or in ppp_receive_mp_frame().
  2093. */
  2094. proto = PPP_PROTO(skb);
  2095. switch (proto) {
  2096. case PPP_VJC_COMP:
  2097. /* decompress VJ compressed packets */
  2098. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  2099. goto err;
  2100. if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
  2101. /* copy to a new sk_buff with more tailroom */
  2102. ns = dev_alloc_skb(skb->len + 128);
  2103. if (!ns) {
  2104. netdev_err(ppp->dev, "PPP: no memory "
  2105. "(VJ decomp)\n");
  2106. goto err;
  2107. }
  2108. skb_reserve(ns, 2);
  2109. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  2110. consume_skb(skb);
  2111. skb = ns;
  2112. }
  2113. else
  2114. skb->ip_summed = CHECKSUM_NONE;
  2115. len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
  2116. if (len <= 0) {
  2117. netdev_printk(KERN_DEBUG, ppp->dev,
  2118. "PPP: VJ decompression error\n");
  2119. goto err;
  2120. }
  2121. len += 2;
  2122. if (len > skb->len)
  2123. skb_put(skb, len - skb->len);
  2124. else if (len < skb->len)
  2125. skb_trim(skb, len);
  2126. proto = PPP_IP;
  2127. break;
  2128. case PPP_VJC_UNCOMP:
  2129. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  2130. goto err;
  2131. /* Until we fix the decompressor need to make sure
  2132. * data portion is linear.
  2133. */
  2134. if (!pskb_may_pull(skb, skb->len))
  2135. goto err;
  2136. if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
  2137. netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
  2138. goto err;
  2139. }
  2140. proto = PPP_IP;
  2141. break;
  2142. case PPP_CCP:
  2143. ppp_ccp_peek(ppp, skb, 1);
  2144. break;
  2145. }
  2146. ++ppp->stats64.rx_packets;
  2147. ppp->stats64.rx_bytes += skb->len - 2;
  2148. npi = proto_to_npindex(proto);
  2149. if (npi < 0) {
  2150. /* control or unknown frame - pass it to pppd */
  2151. skb_queue_tail(&ppp->file.rq, skb);
  2152. /* limit queue length by dropping old frames */
  2153. while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
  2154. (skb = skb_dequeue(&ppp->file.rq)))
  2155. kfree_skb(skb);
  2156. /* wake up any process polling or blocking on read */
  2157. wake_up_interruptible(&ppp->file.rwait);
  2158. } else {
  2159. /* network protocol frame - give it to the kernel */
  2160. #ifdef CONFIG_PPP_FILTER
  2161. /* check if the packet passes the pass and active filters */
  2162. /* the filter instructions are constructed assuming
  2163. a four-byte PPP header on each packet */
  2164. if (ppp->pass_filter || ppp->active_filter) {
  2165. if (skb_unclone(skb, GFP_ATOMIC))
  2166. goto err;
  2167. *(u8 *)skb_push(skb, 2) = 0;
  2168. if (ppp->pass_filter &&
  2169. bpf_prog_run(ppp->pass_filter, skb) == 0) {
  2170. if (ppp->debug & 1)
  2171. netdev_printk(KERN_DEBUG, ppp->dev,
  2172. "PPP: inbound frame "
  2173. "not passed\n");
  2174. kfree_skb(skb);
  2175. return;
  2176. }
  2177. if (!(ppp->active_filter &&
  2178. bpf_prog_run(ppp->active_filter, skb) == 0))
  2179. ppp->last_recv = jiffies;
  2180. __skb_pull(skb, 2);
  2181. } else
  2182. #endif /* CONFIG_PPP_FILTER */
  2183. ppp->last_recv = jiffies;
  2184. if ((ppp->dev->flags & IFF_UP) == 0 ||
  2185. ppp->npmode[npi] != NPMODE_PASS) {
  2186. kfree_skb(skb);
  2187. } else {
  2188. /* chop off protocol */
  2189. skb_pull_rcsum(skb, 2);
  2190. skb->dev = ppp->dev;
  2191. skb->protocol = htons(npindex_to_ethertype[npi]);
  2192. skb_reset_mac_header(skb);
  2193. skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
  2194. dev_net(ppp->dev)));
  2195. netif_rx(skb);
  2196. }
  2197. }
  2198. return;
  2199. err:
  2200. kfree_skb(skb);
  2201. ppp_receive_error(ppp);
  2202. }
  2203. static struct sk_buff *
  2204. ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
  2205. {
  2206. int proto = PPP_PROTO(skb);
  2207. struct sk_buff *ns;
  2208. int len;
  2209. /* Until we fix all the decompressor's need to make sure
  2210. * data portion is linear.
  2211. */
  2212. if (!pskb_may_pull(skb, skb->len))
  2213. goto err;
  2214. if (proto == PPP_COMP) {
  2215. int obuff_size;
  2216. switch(ppp->rcomp->compress_proto) {
  2217. case CI_MPPE:
  2218. obuff_size = ppp->mru + PPP_HDRLEN + 1;
  2219. break;
  2220. default:
  2221. obuff_size = ppp->mru + PPP_HDRLEN;
  2222. break;
  2223. }
  2224. ns = dev_alloc_skb(obuff_size);
  2225. if (!ns) {
  2226. netdev_err(ppp->dev, "ppp_decompress_frame: "
  2227. "no memory\n");
  2228. goto err;
  2229. }
  2230. /* the decompressor still expects the A/C bytes in the hdr */
  2231. len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
  2232. skb->len + 2, ns->data, obuff_size);
  2233. if (len < 0) {
  2234. /* Pass the compressed frame to pppd as an
  2235. error indication. */
  2236. if (len == DECOMP_FATALERROR)
  2237. ppp->rstate |= SC_DC_FERROR;
  2238. kfree_skb(ns);
  2239. goto err;
  2240. }
  2241. consume_skb(skb);
  2242. skb = ns;
  2243. skb_put(skb, len);
  2244. skb_pull(skb, 2); /* pull off the A/C bytes */
  2245. /* Don't call __ppp_decompress_proto() here, but instead rely on
  2246. * corresponding algo (mppe/bsd/deflate) to decompress it.
  2247. */
  2248. } else {
  2249. /* Uncompressed frame - pass to decompressor so it
  2250. can update its dictionary if necessary. */
  2251. if (ppp->rcomp->incomp)
  2252. ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
  2253. skb->len + 2);
  2254. }
  2255. return skb;
  2256. err:
  2257. ppp->rstate |= SC_DC_ERROR;
  2258. ppp_receive_error(ppp);
  2259. return skb;
  2260. }
  2261. #ifdef CONFIG_PPP_MULTILINK
  2262. /*
  2263. * Receive a multilink frame.
  2264. * We put it on the reconstruction queue and then pull off
  2265. * as many completed frames as we can.
  2266. */
  2267. static void
  2268. ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  2269. {
  2270. u32 mask, seq;
  2271. struct channel *ch;
  2272. int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  2273. if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
  2274. goto err; /* no good, throw it away */
  2275. /* Decode sequence number and begin/end bits */
  2276. if (ppp->flags & SC_MP_SHORTSEQ) {
  2277. seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
  2278. mask = 0xfff;
  2279. } else {
  2280. seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
  2281. mask = 0xffffff;
  2282. }
  2283. PPP_MP_CB(skb)->BEbits = skb->data[2];
  2284. skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
  2285. /*
  2286. * Do protocol ID decompression on the first fragment of each packet.
  2287. * We have to do that here, because ppp_receive_nonmp_frame() expects
  2288. * decompressed protocol field.
  2289. */
  2290. if (PPP_MP_CB(skb)->BEbits & B)
  2291. __ppp_decompress_proto(skb);
  2292. /*
  2293. * Expand sequence number to 32 bits, making it as close
  2294. * as possible to ppp->minseq.
  2295. */
  2296. seq |= ppp->minseq & ~mask;
  2297. if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
  2298. seq += mask + 1;
  2299. else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
  2300. seq -= mask + 1; /* should never happen */
  2301. PPP_MP_CB(skb)->sequence = seq;
  2302. pch->lastseq = seq;
  2303. /*
  2304. * If this packet comes before the next one we were expecting,
  2305. * drop it.
  2306. */
  2307. if (seq_before(seq, ppp->nextseq)) {
  2308. kfree_skb(skb);
  2309. ++ppp->dev->stats.rx_dropped;
  2310. ppp_receive_error(ppp);
  2311. return;
  2312. }
  2313. /*
  2314. * Reevaluate minseq, the minimum over all channels of the
  2315. * last sequence number received on each channel. Because of
  2316. * the increasing sequence number rule, we know that any fragment
  2317. * before `minseq' which hasn't arrived is never going to arrive.
  2318. * The list of channels can't change because we have the receive
  2319. * side of the ppp unit locked.
  2320. */
  2321. list_for_each_entry(ch, &ppp->channels, clist) {
  2322. if (seq_before(ch->lastseq, seq))
  2323. seq = ch->lastseq;
  2324. }
  2325. if (seq_before(ppp->minseq, seq))
  2326. ppp->minseq = seq;
  2327. /* Put the fragment on the reconstruction queue */
  2328. ppp_mp_insert(ppp, skb);
  2329. /* If the queue is getting long, don't wait any longer for packets
  2330. before the start of the queue. */
  2331. if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
  2332. struct sk_buff *mskb = skb_peek(&ppp->mrq);
  2333. if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
  2334. ppp->minseq = PPP_MP_CB(mskb)->sequence;
  2335. }
  2336. /* Pull completed packets off the queue and receive them. */
  2337. while ((skb = ppp_mp_reconstruct(ppp))) {
  2338. if (pskb_may_pull(skb, 2))
  2339. ppp_receive_nonmp_frame(ppp, skb);
  2340. else {
  2341. ++ppp->dev->stats.rx_length_errors;
  2342. kfree_skb(skb);
  2343. ppp_receive_error(ppp);
  2344. }
  2345. }
  2346. return;
  2347. err:
  2348. kfree_skb(skb);
  2349. ppp_receive_error(ppp);
  2350. }
  2351. /*
  2352. * Insert a fragment on the MP reconstruction queue.
  2353. * The queue is ordered by increasing sequence number.
  2354. */
  2355. static void
  2356. ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
  2357. {
  2358. struct sk_buff *p;
  2359. struct sk_buff_head *list = &ppp->mrq;
  2360. u32 seq = PPP_MP_CB(skb)->sequence;
  2361. /* N.B. we don't need to lock the list lock because we have the
  2362. ppp unit receive-side lock. */
  2363. skb_queue_walk(list, p) {
  2364. if (seq_before(seq, PPP_MP_CB(p)->sequence))
  2365. break;
  2366. }
  2367. __skb_queue_before(list, p, skb);
  2368. }
  2369. /*
  2370. * Reconstruct a packet from the MP fragment queue.
  2371. * We go through increasing sequence numbers until we find a
  2372. * complete packet, or we get to the sequence number for a fragment
  2373. * which hasn't arrived but might still do so.
  2374. */
  2375. static struct sk_buff *
  2376. ppp_mp_reconstruct(struct ppp *ppp)
  2377. {
  2378. u32 seq = ppp->nextseq;
  2379. u32 minseq = ppp->minseq;
  2380. struct sk_buff_head *list = &ppp->mrq;
  2381. struct sk_buff *p, *tmp;
  2382. struct sk_buff *head, *tail;
  2383. struct sk_buff *skb = NULL;
  2384. int lost = 0, len = 0;
  2385. if (ppp->mrru == 0) /* do nothing until mrru is set */
  2386. return NULL;
  2387. head = __skb_peek(list);
  2388. tail = NULL;
  2389. skb_queue_walk_safe(list, p, tmp) {
  2390. again:
  2391. if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
  2392. /* this can't happen, anyway ignore the skb */
  2393. netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
  2394. "seq %u < %u\n",
  2395. PPP_MP_CB(p)->sequence, seq);
  2396. __skb_unlink(p, list);
  2397. kfree_skb(p);
  2398. continue;
  2399. }
  2400. if (PPP_MP_CB(p)->sequence != seq) {
  2401. u32 oldseq;
  2402. /* Fragment `seq' is missing. If it is after
  2403. minseq, it might arrive later, so stop here. */
  2404. if (seq_after(seq, minseq))
  2405. break;
  2406. /* Fragment `seq' is lost, keep going. */
  2407. lost = 1;
  2408. oldseq = seq;
  2409. seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
  2410. minseq + 1: PPP_MP_CB(p)->sequence;
  2411. if (ppp->debug & 1)
  2412. netdev_printk(KERN_DEBUG, ppp->dev,
  2413. "lost frag %u..%u\n",
  2414. oldseq, seq-1);
  2415. goto again;
  2416. }
  2417. /*
  2418. * At this point we know that all the fragments from
  2419. * ppp->nextseq to seq are either present or lost.
  2420. * Also, there are no complete packets in the queue
  2421. * that have no missing fragments and end before this
  2422. * fragment.
  2423. */
  2424. /* B bit set indicates this fragment starts a packet */
  2425. if (PPP_MP_CB(p)->BEbits & B) {
  2426. head = p;
  2427. lost = 0;
  2428. len = 0;
  2429. }
  2430. len += p->len;
  2431. /* Got a complete packet yet? */
  2432. if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
  2433. (PPP_MP_CB(head)->BEbits & B)) {
  2434. if (len > ppp->mrru + 2) {
  2435. ++ppp->dev->stats.rx_length_errors;
  2436. netdev_printk(KERN_DEBUG, ppp->dev,
  2437. "PPP: reconstructed packet"
  2438. " is too long (%d)\n", len);
  2439. } else {
  2440. tail = p;
  2441. break;
  2442. }
  2443. ppp->nextseq = seq + 1;
  2444. }
  2445. /*
  2446. * If this is the ending fragment of a packet,
  2447. * and we haven't found a complete valid packet yet,
  2448. * we can discard up to and including this fragment.
  2449. */
  2450. if (PPP_MP_CB(p)->BEbits & E) {
  2451. struct sk_buff *tmp2;
  2452. skb_queue_reverse_walk_from_safe(list, p, tmp2) {
  2453. if (ppp->debug & 1)
  2454. netdev_printk(KERN_DEBUG, ppp->dev,
  2455. "discarding frag %u\n",
  2456. PPP_MP_CB(p)->sequence);
  2457. __skb_unlink(p, list);
  2458. kfree_skb(p);
  2459. }
  2460. head = skb_peek(list);
  2461. if (!head)
  2462. break;
  2463. }
  2464. ++seq;
  2465. }
  2466. /* If we have a complete packet, copy it all into one skb. */
  2467. if (tail != NULL) {
  2468. /* If we have discarded any fragments,
  2469. signal a receive error. */
  2470. if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
  2471. skb_queue_walk_safe(list, p, tmp) {
  2472. if (p == head)
  2473. break;
  2474. if (ppp->debug & 1)
  2475. netdev_printk(KERN_DEBUG, ppp->dev,
  2476. "discarding frag %u\n",
  2477. PPP_MP_CB(p)->sequence);
  2478. __skb_unlink(p, list);
  2479. kfree_skb(p);
  2480. }
  2481. if (ppp->debug & 1)
  2482. netdev_printk(KERN_DEBUG, ppp->dev,
  2483. " missed pkts %u..%u\n",
  2484. ppp->nextseq,
  2485. PPP_MP_CB(head)->sequence-1);
  2486. ++ppp->dev->stats.rx_dropped;
  2487. ppp_receive_error(ppp);
  2488. }
  2489. skb = head;
  2490. if (head != tail) {
  2491. struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
  2492. p = skb_queue_next(list, head);
  2493. __skb_unlink(skb, list);
  2494. skb_queue_walk_from_safe(list, p, tmp) {
  2495. __skb_unlink(p, list);
  2496. *fragpp = p;
  2497. p->next = NULL;
  2498. fragpp = &p->next;
  2499. skb->len += p->len;
  2500. skb->data_len += p->len;
  2501. skb->truesize += p->truesize;
  2502. if (p == tail)
  2503. break;
  2504. }
  2505. } else {
  2506. __skb_unlink(skb, list);
  2507. }
  2508. ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
  2509. }
  2510. return skb;
  2511. }
  2512. #endif /* CONFIG_PPP_MULTILINK */
  2513. /*
  2514. * Channel interface.
  2515. */
  2516. /* Create a new, unattached ppp channel. */
  2517. int ppp_register_channel(struct ppp_channel *chan)
  2518. {
  2519. return ppp_register_net_channel(current->nsproxy->net_ns, chan);
  2520. }
  2521. /* Create a new, unattached ppp channel for specified net. */
  2522. int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
  2523. {
  2524. struct channel *pch;
  2525. struct ppp_net *pn;
  2526. pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
  2527. if (!pch)
  2528. return -ENOMEM;
  2529. pn = ppp_pernet(net);
  2530. pch->ppp = NULL;
  2531. pch->chan = chan;
  2532. pch->chan_net = get_net_track(net, &pch->ns_tracker, GFP_KERNEL);
  2533. chan->ppp = pch;
  2534. init_ppp_file(&pch->file, CHANNEL);
  2535. pch->file.hdrlen = chan->hdrlen;
  2536. #ifdef CONFIG_PPP_MULTILINK
  2537. pch->lastseq = -1;
  2538. #endif /* CONFIG_PPP_MULTILINK */
  2539. init_rwsem(&pch->chan_sem);
  2540. spin_lock_init(&pch->downl);
  2541. rwlock_init(&pch->upl);
  2542. spin_lock_bh(&pn->all_channels_lock);
  2543. pch->file.index = ++pn->last_channel_index;
  2544. list_add(&pch->list, &pn->new_channels);
  2545. atomic_inc(&channel_count);
  2546. spin_unlock_bh(&pn->all_channels_lock);
  2547. return 0;
  2548. }
  2549. /*
  2550. * Return the index of a channel.
  2551. */
  2552. int ppp_channel_index(struct ppp_channel *chan)
  2553. {
  2554. struct channel *pch = chan->ppp;
  2555. if (pch)
  2556. return pch->file.index;
  2557. return -1;
  2558. }
  2559. /*
  2560. * Return the PPP unit number to which a channel is connected.
  2561. */
  2562. int ppp_unit_number(struct ppp_channel *chan)
  2563. {
  2564. struct channel *pch = chan->ppp;
  2565. int unit = -1;
  2566. if (pch) {
  2567. read_lock_bh(&pch->upl);
  2568. if (pch->ppp)
  2569. unit = pch->ppp->file.index;
  2570. read_unlock_bh(&pch->upl);
  2571. }
  2572. return unit;
  2573. }
  2574. /*
  2575. * Return the PPP device interface name of a channel.
  2576. */
  2577. char *ppp_dev_name(struct ppp_channel *chan)
  2578. {
  2579. struct channel *pch = chan->ppp;
  2580. char *name = NULL;
  2581. if (pch) {
  2582. read_lock_bh(&pch->upl);
  2583. if (pch->ppp && pch->ppp->dev)
  2584. name = pch->ppp->dev->name;
  2585. read_unlock_bh(&pch->upl);
  2586. }
  2587. return name;
  2588. }
  2589. /*
  2590. * Disconnect a channel from the generic layer.
  2591. * This must be called in process context.
  2592. */
  2593. void
  2594. ppp_unregister_channel(struct ppp_channel *chan)
  2595. {
  2596. struct channel *pch = chan->ppp;
  2597. struct ppp_net *pn;
  2598. if (!pch)
  2599. return; /* should never happen */
  2600. chan->ppp = NULL;
  2601. /*
  2602. * This ensures that we have returned from any calls into
  2603. * the channel's start_xmit or ioctl routine before we proceed.
  2604. */
  2605. down_write(&pch->chan_sem);
  2606. spin_lock_bh(&pch->downl);
  2607. pch->chan = NULL;
  2608. spin_unlock_bh(&pch->downl);
  2609. up_write(&pch->chan_sem);
  2610. ppp_disconnect_channel(pch);
  2611. pn = ppp_pernet(pch->chan_net);
  2612. spin_lock_bh(&pn->all_channels_lock);
  2613. list_del(&pch->list);
  2614. spin_unlock_bh(&pn->all_channels_lock);
  2615. ppp_unbridge_channels(pch);
  2616. pch->file.dead = 1;
  2617. wake_up_interruptible(&pch->file.rwait);
  2618. if (refcount_dec_and_test(&pch->file.refcnt))
  2619. ppp_destroy_channel(pch);
  2620. }
  2621. /*
  2622. * Callback from a channel when it can accept more to transmit.
  2623. * This should be called at BH/softirq level, not interrupt level.
  2624. */
  2625. void
  2626. ppp_output_wakeup(struct ppp_channel *chan)
  2627. {
  2628. struct channel *pch = chan->ppp;
  2629. if (!pch)
  2630. return;
  2631. ppp_channel_push(pch);
  2632. }
  2633. /*
  2634. * Compression control.
  2635. */
  2636. /* Process the PPPIOCSCOMPRESS ioctl. */
  2637. static int
  2638. ppp_set_compress(struct ppp *ppp, struct ppp_option_data *data)
  2639. {
  2640. int err = -EFAULT;
  2641. struct compressor *cp, *ocomp;
  2642. void *state, *ostate;
  2643. unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
  2644. if (data->length > CCP_MAX_OPTION_LENGTH)
  2645. goto out;
  2646. if (copy_from_user(ccp_option, data->ptr, data->length))
  2647. goto out;
  2648. err = -EINVAL;
  2649. if (data->length < 2 || ccp_option[1] < 2 || ccp_option[1] > data->length)
  2650. goto out;
  2651. cp = try_then_request_module(
  2652. find_compressor(ccp_option[0]),
  2653. "ppp-compress-%d", ccp_option[0]);
  2654. if (!cp)
  2655. goto out;
  2656. err = -ENOBUFS;
  2657. if (data->transmit) {
  2658. state = cp->comp_alloc(ccp_option, data->length);
  2659. if (state) {
  2660. ppp_xmit_lock(ppp);
  2661. ppp->xstate &= ~SC_COMP_RUN;
  2662. ocomp = ppp->xcomp;
  2663. ostate = ppp->xc_state;
  2664. ppp->xcomp = cp;
  2665. ppp->xc_state = state;
  2666. ppp_xmit_unlock(ppp);
  2667. if (ostate) {
  2668. ocomp->comp_free(ostate);
  2669. module_put(ocomp->owner);
  2670. }
  2671. err = 0;
  2672. } else
  2673. module_put(cp->owner);
  2674. } else {
  2675. state = cp->decomp_alloc(ccp_option, data->length);
  2676. if (state) {
  2677. ppp_recv_lock(ppp);
  2678. ppp->rstate &= ~SC_DECOMP_RUN;
  2679. ocomp = ppp->rcomp;
  2680. ostate = ppp->rc_state;
  2681. ppp->rcomp = cp;
  2682. ppp->rc_state = state;
  2683. ppp_recv_unlock(ppp);
  2684. if (ostate) {
  2685. ocomp->decomp_free(ostate);
  2686. module_put(ocomp->owner);
  2687. }
  2688. err = 0;
  2689. } else
  2690. module_put(cp->owner);
  2691. }
  2692. out:
  2693. return err;
  2694. }
  2695. /*
  2696. * Look at a CCP packet and update our state accordingly.
  2697. * We assume the caller has the xmit or recv path locked.
  2698. */
  2699. static void
  2700. ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
  2701. {
  2702. unsigned char *dp;
  2703. int len;
  2704. if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
  2705. return; /* no header */
  2706. dp = skb->data + 2;
  2707. switch (CCP_CODE(dp)) {
  2708. case CCP_CONFREQ:
  2709. /* A ConfReq starts negotiation of compression
  2710. * in one direction of transmission,
  2711. * and hence brings it down...but which way?
  2712. *
  2713. * Remember:
  2714. * A ConfReq indicates what the sender would like to receive
  2715. */
  2716. if(inbound)
  2717. /* He is proposing what I should send */
  2718. ppp->xstate &= ~SC_COMP_RUN;
  2719. else
  2720. /* I am proposing to what he should send */
  2721. ppp->rstate &= ~SC_DECOMP_RUN;
  2722. break;
  2723. case CCP_TERMREQ:
  2724. case CCP_TERMACK:
  2725. /*
  2726. * CCP is going down, both directions of transmission
  2727. */
  2728. ppp->rstate &= ~SC_DECOMP_RUN;
  2729. ppp->xstate &= ~SC_COMP_RUN;
  2730. break;
  2731. case CCP_CONFACK:
  2732. if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
  2733. break;
  2734. len = CCP_LENGTH(dp);
  2735. if (!pskb_may_pull(skb, len + 2))
  2736. return; /* too short */
  2737. dp += CCP_HDRLEN;
  2738. len -= CCP_HDRLEN;
  2739. if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
  2740. break;
  2741. if (inbound) {
  2742. /* we will start receiving compressed packets */
  2743. if (!ppp->rc_state)
  2744. break;
  2745. if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
  2746. ppp->file.index, 0, ppp->mru, ppp->debug)) {
  2747. ppp->rstate |= SC_DECOMP_RUN;
  2748. ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
  2749. }
  2750. } else {
  2751. /* we will soon start sending compressed packets */
  2752. if (!ppp->xc_state)
  2753. break;
  2754. if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
  2755. ppp->file.index, 0, ppp->debug))
  2756. ppp->xstate |= SC_COMP_RUN;
  2757. }
  2758. break;
  2759. case CCP_RESETACK:
  2760. /* reset the [de]compressor */
  2761. if ((ppp->flags & SC_CCP_UP) == 0)
  2762. break;
  2763. if (inbound) {
  2764. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
  2765. ppp->rcomp->decomp_reset(ppp->rc_state);
  2766. ppp->rstate &= ~SC_DC_ERROR;
  2767. }
  2768. } else {
  2769. if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
  2770. ppp->xcomp->comp_reset(ppp->xc_state);
  2771. }
  2772. break;
  2773. }
  2774. }
  2775. /* Free up compression resources. */
  2776. static void
  2777. ppp_ccp_closed(struct ppp *ppp)
  2778. {
  2779. void *xstate, *rstate;
  2780. struct compressor *xcomp, *rcomp;
  2781. ppp_lock(ppp);
  2782. ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
  2783. ppp->xstate = 0;
  2784. xcomp = ppp->xcomp;
  2785. xstate = ppp->xc_state;
  2786. ppp->xc_state = NULL;
  2787. ppp->rstate = 0;
  2788. rcomp = ppp->rcomp;
  2789. rstate = ppp->rc_state;
  2790. ppp->rc_state = NULL;
  2791. ppp_unlock(ppp);
  2792. if (xstate) {
  2793. xcomp->comp_free(xstate);
  2794. module_put(xcomp->owner);
  2795. }
  2796. if (rstate) {
  2797. rcomp->decomp_free(rstate);
  2798. module_put(rcomp->owner);
  2799. }
  2800. }
  2801. /* List of compressors. */
  2802. static LIST_HEAD(compressor_list);
  2803. static DEFINE_SPINLOCK(compressor_list_lock);
  2804. struct compressor_entry {
  2805. struct list_head list;
  2806. struct compressor *comp;
  2807. };
  2808. static struct compressor_entry *
  2809. find_comp_entry(int proto)
  2810. {
  2811. struct compressor_entry *ce;
  2812. list_for_each_entry(ce, &compressor_list, list) {
  2813. if (ce->comp->compress_proto == proto)
  2814. return ce;
  2815. }
  2816. return NULL;
  2817. }
  2818. /* Register a compressor */
  2819. int
  2820. ppp_register_compressor(struct compressor *cp)
  2821. {
  2822. struct compressor_entry *ce;
  2823. int ret;
  2824. spin_lock(&compressor_list_lock);
  2825. ret = -EEXIST;
  2826. if (find_comp_entry(cp->compress_proto))
  2827. goto out;
  2828. ret = -ENOMEM;
  2829. ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
  2830. if (!ce)
  2831. goto out;
  2832. ret = 0;
  2833. ce->comp = cp;
  2834. list_add(&ce->list, &compressor_list);
  2835. out:
  2836. spin_unlock(&compressor_list_lock);
  2837. return ret;
  2838. }
  2839. /* Unregister a compressor */
  2840. void
  2841. ppp_unregister_compressor(struct compressor *cp)
  2842. {
  2843. struct compressor_entry *ce;
  2844. spin_lock(&compressor_list_lock);
  2845. ce = find_comp_entry(cp->compress_proto);
  2846. if (ce && ce->comp == cp) {
  2847. list_del(&ce->list);
  2848. kfree(ce);
  2849. }
  2850. spin_unlock(&compressor_list_lock);
  2851. }
  2852. /* Find a compressor. */
  2853. static struct compressor *
  2854. find_compressor(int type)
  2855. {
  2856. struct compressor_entry *ce;
  2857. struct compressor *cp = NULL;
  2858. spin_lock(&compressor_list_lock);
  2859. ce = find_comp_entry(type);
  2860. if (ce) {
  2861. cp = ce->comp;
  2862. if (!try_module_get(cp->owner))
  2863. cp = NULL;
  2864. }
  2865. spin_unlock(&compressor_list_lock);
  2866. return cp;
  2867. }
  2868. /*
  2869. * Miscelleneous stuff.
  2870. */
  2871. static void
  2872. ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  2873. {
  2874. struct slcompress *vj = ppp->vj;
  2875. memset(st, 0, sizeof(*st));
  2876. st->p.ppp_ipackets = ppp->stats64.rx_packets;
  2877. st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
  2878. st->p.ppp_ibytes = ppp->stats64.rx_bytes;
  2879. st->p.ppp_opackets = ppp->stats64.tx_packets;
  2880. st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
  2881. st->p.ppp_obytes = ppp->stats64.tx_bytes;
  2882. if (!vj)
  2883. return;
  2884. st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
  2885. st->vj.vjs_compressed = vj->sls_o_compressed;
  2886. st->vj.vjs_searches = vj->sls_o_searches;
  2887. st->vj.vjs_misses = vj->sls_o_misses;
  2888. st->vj.vjs_errorin = vj->sls_i_error;
  2889. st->vj.vjs_tossed = vj->sls_i_tossed;
  2890. st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
  2891. st->vj.vjs_compressedin = vj->sls_i_compressed;
  2892. }
  2893. /*
  2894. * Stuff for handling the lists of ppp units and channels
  2895. * and for initialization.
  2896. */
  2897. /*
  2898. * Create a new ppp interface unit. Fails if it can't allocate memory
  2899. * or if there is already a unit with the requested number.
  2900. * unit == -1 means allocate a new number.
  2901. */
  2902. static int ppp_create_interface(struct net *net, struct file *file, int *unit)
  2903. {
  2904. struct ppp_config conf = {
  2905. .file = file,
  2906. .unit = *unit,
  2907. .ifname_is_set = false,
  2908. };
  2909. struct net_device *dev;
  2910. struct ppp *ppp;
  2911. int err;
  2912. dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
  2913. if (!dev) {
  2914. err = -ENOMEM;
  2915. goto err;
  2916. }
  2917. dev_net_set(dev, net);
  2918. dev->rtnl_link_ops = &ppp_link_ops;
  2919. rtnl_lock();
  2920. err = ppp_dev_configure(net, dev, &conf);
  2921. if (err < 0)
  2922. goto err_dev;
  2923. ppp = netdev_priv(dev);
  2924. *unit = ppp->file.index;
  2925. rtnl_unlock();
  2926. return 0;
  2927. err_dev:
  2928. rtnl_unlock();
  2929. free_netdev(dev);
  2930. err:
  2931. return err;
  2932. }
  2933. /*
  2934. * Initialize a ppp_file structure.
  2935. */
  2936. static void
  2937. init_ppp_file(struct ppp_file *pf, int kind)
  2938. {
  2939. pf->kind = kind;
  2940. skb_queue_head_init(&pf->xq);
  2941. skb_queue_head_init(&pf->rq);
  2942. refcount_set(&pf->refcnt, 1);
  2943. init_waitqueue_head(&pf->rwait);
  2944. }
  2945. /*
  2946. * Free the memory used by a ppp unit. This is only called once
  2947. * there are no channels connected to the unit and no file structs
  2948. * that reference the unit.
  2949. */
  2950. static void ppp_destroy_interface(struct ppp *ppp)
  2951. {
  2952. atomic_dec(&ppp_unit_count);
  2953. if (!ppp->file.dead || ppp->n_channels) {
  2954. /* "can't happen" */
  2955. netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
  2956. "but dead=%d n_channels=%d !\n",
  2957. ppp, ppp->file.dead, ppp->n_channels);
  2958. return;
  2959. }
  2960. ppp_ccp_closed(ppp);
  2961. if (ppp->vj) {
  2962. slhc_free(ppp->vj);
  2963. ppp->vj = NULL;
  2964. }
  2965. skb_queue_purge(&ppp->file.xq);
  2966. skb_queue_purge(&ppp->file.rq);
  2967. #ifdef CONFIG_PPP_MULTILINK
  2968. skb_queue_purge(&ppp->mrq);
  2969. #endif /* CONFIG_PPP_MULTILINK */
  2970. #ifdef CONFIG_PPP_FILTER
  2971. if (ppp->pass_filter) {
  2972. bpf_prog_destroy(ppp->pass_filter);
  2973. ppp->pass_filter = NULL;
  2974. }
  2975. if (ppp->active_filter) {
  2976. bpf_prog_destroy(ppp->active_filter);
  2977. ppp->active_filter = NULL;
  2978. }
  2979. #endif /* CONFIG_PPP_FILTER */
  2980. kfree_skb(ppp->xmit_pending);
  2981. free_percpu(ppp->xmit_recursion);
  2982. free_netdev(ppp->dev);
  2983. }
  2984. /*
  2985. * Locate an existing ppp unit.
  2986. * The caller should have locked the all_ppp_mutex.
  2987. */
  2988. static struct ppp *
  2989. ppp_find_unit(struct ppp_net *pn, int unit)
  2990. {
  2991. return unit_find(&pn->units_idr, unit);
  2992. }
  2993. /*
  2994. * Locate an existing ppp channel.
  2995. * The caller should have locked the all_channels_lock.
  2996. * First we look in the new_channels list, then in the
  2997. * all_channels list. If found in the new_channels list,
  2998. * we move it to the all_channels list. This is for speed
  2999. * when we have a lot of channels in use.
  3000. */
  3001. static struct channel *
  3002. ppp_find_channel(struct ppp_net *pn, int unit)
  3003. {
  3004. struct channel *pch;
  3005. list_for_each_entry(pch, &pn->new_channels, list) {
  3006. if (pch->file.index == unit) {
  3007. list_move(&pch->list, &pn->all_channels);
  3008. return pch;
  3009. }
  3010. }
  3011. list_for_each_entry(pch, &pn->all_channels, list) {
  3012. if (pch->file.index == unit)
  3013. return pch;
  3014. }
  3015. return NULL;
  3016. }
  3017. /*
  3018. * Connect a PPP channel to a PPP interface unit.
  3019. */
  3020. static int
  3021. ppp_connect_channel(struct channel *pch, int unit)
  3022. {
  3023. struct ppp *ppp;
  3024. struct ppp_net *pn;
  3025. int ret = -ENXIO;
  3026. int hdrlen;
  3027. pn = ppp_pernet(pch->chan_net);
  3028. mutex_lock(&pn->all_ppp_mutex);
  3029. ppp = ppp_find_unit(pn, unit);
  3030. if (!ppp)
  3031. goto out;
  3032. write_lock_bh(&pch->upl);
  3033. ret = -EINVAL;
  3034. if (pch->ppp ||
  3035. rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl)))
  3036. goto outl;
  3037. ppp_lock(ppp);
  3038. spin_lock_bh(&pch->downl);
  3039. if (!pch->chan) {
  3040. /* Don't connect unregistered channels */
  3041. spin_unlock_bh(&pch->downl);
  3042. ppp_unlock(ppp);
  3043. ret = -ENOTCONN;
  3044. goto outl;
  3045. }
  3046. spin_unlock_bh(&pch->downl);
  3047. if (pch->file.hdrlen > ppp->file.hdrlen)
  3048. ppp->file.hdrlen = pch->file.hdrlen;
  3049. hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
  3050. if (hdrlen > ppp->dev->hard_header_len)
  3051. ppp->dev->hard_header_len = hdrlen;
  3052. list_add_tail(&pch->clist, &ppp->channels);
  3053. ++ppp->n_channels;
  3054. pch->ppp = ppp;
  3055. refcount_inc(&ppp->file.refcnt);
  3056. ppp_unlock(ppp);
  3057. ret = 0;
  3058. outl:
  3059. write_unlock_bh(&pch->upl);
  3060. out:
  3061. mutex_unlock(&pn->all_ppp_mutex);
  3062. return ret;
  3063. }
  3064. /*
  3065. * Disconnect a channel from its ppp unit.
  3066. */
  3067. static int
  3068. ppp_disconnect_channel(struct channel *pch)
  3069. {
  3070. struct ppp *ppp;
  3071. int err = -EINVAL;
  3072. write_lock_bh(&pch->upl);
  3073. ppp = pch->ppp;
  3074. pch->ppp = NULL;
  3075. write_unlock_bh(&pch->upl);
  3076. if (ppp) {
  3077. /* remove it from the ppp unit's list */
  3078. ppp_lock(ppp);
  3079. list_del(&pch->clist);
  3080. if (--ppp->n_channels == 0)
  3081. wake_up_interruptible(&ppp->file.rwait);
  3082. ppp_unlock(ppp);
  3083. if (refcount_dec_and_test(&ppp->file.refcnt))
  3084. ppp_destroy_interface(ppp);
  3085. err = 0;
  3086. }
  3087. return err;
  3088. }
  3089. /*
  3090. * Free up the resources used by a ppp channel.
  3091. */
  3092. static void ppp_destroy_channel(struct channel *pch)
  3093. {
  3094. put_net_track(pch->chan_net, &pch->ns_tracker);
  3095. pch->chan_net = NULL;
  3096. atomic_dec(&channel_count);
  3097. if (!pch->file.dead) {
  3098. /* "can't happen" */
  3099. pr_err("ppp: destroying undead channel %p !\n", pch);
  3100. return;
  3101. }
  3102. skb_queue_purge(&pch->file.xq);
  3103. skb_queue_purge(&pch->file.rq);
  3104. kfree(pch);
  3105. }
  3106. static void __exit ppp_cleanup(void)
  3107. {
  3108. /* should never happen */
  3109. if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
  3110. pr_err("PPP: removing module but units remain!\n");
  3111. rtnl_link_unregister(&ppp_link_ops);
  3112. unregister_chrdev(PPP_MAJOR, "ppp");
  3113. device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
  3114. class_destroy(ppp_class);
  3115. unregister_pernet_device(&ppp_net_ops);
  3116. }
  3117. /*
  3118. * Units handling. Caller must protect concurrent access
  3119. * by holding all_ppp_mutex
  3120. */
  3121. /* associate pointer with specified number */
  3122. static int unit_set(struct idr *p, void *ptr, int n)
  3123. {
  3124. int unit;
  3125. unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
  3126. if (unit == -ENOSPC)
  3127. unit = -EINVAL;
  3128. return unit;
  3129. }
  3130. /* get new free unit number and associate pointer with it */
  3131. static int unit_get(struct idr *p, void *ptr, int min)
  3132. {
  3133. return idr_alloc(p, ptr, min, 0, GFP_KERNEL);
  3134. }
  3135. /* put unit number back to a pool */
  3136. static void unit_put(struct idr *p, int n)
  3137. {
  3138. idr_remove(p, n);
  3139. }
  3140. /* get pointer associated with the number */
  3141. static void *unit_find(struct idr *p, int n)
  3142. {
  3143. return idr_find(p, n);
  3144. }
  3145. /* Module/initialization stuff */
  3146. module_init(ppp_init);
  3147. module_exit(ppp_cleanup);
  3148. EXPORT_SYMBOL(ppp_register_net_channel);
  3149. EXPORT_SYMBOL(ppp_register_channel);
  3150. EXPORT_SYMBOL(ppp_unregister_channel);
  3151. EXPORT_SYMBOL(ppp_channel_index);
  3152. EXPORT_SYMBOL(ppp_unit_number);
  3153. EXPORT_SYMBOL(ppp_dev_name);
  3154. EXPORT_SYMBOL(ppp_input);
  3155. EXPORT_SYMBOL(ppp_input_error);
  3156. EXPORT_SYMBOL(ppp_output_wakeup);
  3157. EXPORT_SYMBOL(ppp_register_compressor);
  3158. EXPORT_SYMBOL(ppp_unregister_compressor);
  3159. MODULE_LICENSE("GPL");
  3160. MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
  3161. MODULE_ALIAS_RTNL_LINK("ppp");
  3162. MODULE_ALIAS("devname:ppp");