ppp_generic.c 74 KB

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