ppp_generic.c 71 KB

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