ppp_generic.c 68 KB

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