ppp_generic.c 77 KB

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