ppp_generic.c 80 KB

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