macsec.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591
  1. /*
  2. * drivers/net/macsec.c - MACsec device
  3. *
  4. * Copyright (c) 2015 Sabrina Dubroca <sd@queasysnail.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/socket.h>
  14. #include <linux/module.h>
  15. #include <crypto/aead.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/refcount.h>
  19. #include <net/genetlink.h>
  20. #include <net/sock.h>
  21. #include <net/gro_cells.h>
  22. #include <linux/if_arp.h>
  23. #include <uapi/linux/if_macsec.h>
  24. typedef u64 __bitwise sci_t;
  25. #define MACSEC_SCI_LEN 8
  26. /* SecTAG length = macsec_eth_header without the optional SCI */
  27. #define MACSEC_TAG_LEN 6
  28. struct macsec_eth_header {
  29. struct ethhdr eth;
  30. /* SecTAG */
  31. u8 tci_an;
  32. #if defined(__LITTLE_ENDIAN_BITFIELD)
  33. u8 short_length:6,
  34. unused:2;
  35. #elif defined(__BIG_ENDIAN_BITFIELD)
  36. u8 unused:2,
  37. short_length:6;
  38. #else
  39. #error "Please fix <asm/byteorder.h>"
  40. #endif
  41. __be32 packet_number;
  42. u8 secure_channel_id[8]; /* optional */
  43. } __packed;
  44. #define MACSEC_TCI_VERSION 0x80
  45. #define MACSEC_TCI_ES 0x40 /* end station */
  46. #define MACSEC_TCI_SC 0x20 /* SCI present */
  47. #define MACSEC_TCI_SCB 0x10 /* epon */
  48. #define MACSEC_TCI_E 0x08 /* encryption */
  49. #define MACSEC_TCI_C 0x04 /* changed text */
  50. #define MACSEC_AN_MASK 0x03 /* association number */
  51. #define MACSEC_TCI_CONFID (MACSEC_TCI_E | MACSEC_TCI_C)
  52. /* minimum secure data length deemed "not short", see IEEE 802.1AE-2006 9.7 */
  53. #define MIN_NON_SHORT_LEN 48
  54. #define GCM_AES_IV_LEN 12
  55. #define DEFAULT_ICV_LEN 16
  56. #define MACSEC_NUM_AN 4 /* 2 bits for the association number */
  57. #define for_each_rxsc(secy, sc) \
  58. for (sc = rcu_dereference_bh(secy->rx_sc); \
  59. sc; \
  60. sc = rcu_dereference_bh(sc->next))
  61. #define for_each_rxsc_rtnl(secy, sc) \
  62. for (sc = rtnl_dereference(secy->rx_sc); \
  63. sc; \
  64. sc = rtnl_dereference(sc->next))
  65. struct gcm_iv {
  66. union {
  67. u8 secure_channel_id[8];
  68. sci_t sci;
  69. };
  70. __be32 pn;
  71. };
  72. /**
  73. * struct macsec_key - SA key
  74. * @id: user-provided key identifier
  75. * @tfm: crypto struct, key storage
  76. */
  77. struct macsec_key {
  78. u8 id[MACSEC_KEYID_LEN];
  79. struct crypto_aead *tfm;
  80. };
  81. struct macsec_rx_sc_stats {
  82. __u64 InOctetsValidated;
  83. __u64 InOctetsDecrypted;
  84. __u64 InPktsUnchecked;
  85. __u64 InPktsDelayed;
  86. __u64 InPktsOK;
  87. __u64 InPktsInvalid;
  88. __u64 InPktsLate;
  89. __u64 InPktsNotValid;
  90. __u64 InPktsNotUsingSA;
  91. __u64 InPktsUnusedSA;
  92. };
  93. struct macsec_rx_sa_stats {
  94. __u32 InPktsOK;
  95. __u32 InPktsInvalid;
  96. __u32 InPktsNotValid;
  97. __u32 InPktsNotUsingSA;
  98. __u32 InPktsUnusedSA;
  99. };
  100. struct macsec_tx_sa_stats {
  101. __u32 OutPktsProtected;
  102. __u32 OutPktsEncrypted;
  103. };
  104. struct macsec_tx_sc_stats {
  105. __u64 OutPktsProtected;
  106. __u64 OutPktsEncrypted;
  107. __u64 OutOctetsProtected;
  108. __u64 OutOctetsEncrypted;
  109. };
  110. struct macsec_dev_stats {
  111. __u64 OutPktsUntagged;
  112. __u64 InPktsUntagged;
  113. __u64 OutPktsTooLong;
  114. __u64 InPktsNoTag;
  115. __u64 InPktsBadTag;
  116. __u64 InPktsUnknownSCI;
  117. __u64 InPktsNoSCI;
  118. __u64 InPktsOverrun;
  119. };
  120. /**
  121. * struct macsec_rx_sa - receive secure association
  122. * @active:
  123. * @next_pn: packet number expected for the next packet
  124. * @lock: protects next_pn manipulations
  125. * @key: key structure
  126. * @stats: per-SA stats
  127. */
  128. struct macsec_rx_sa {
  129. struct macsec_key key;
  130. spinlock_t lock;
  131. u32 next_pn;
  132. refcount_t refcnt;
  133. bool active;
  134. struct macsec_rx_sa_stats __percpu *stats;
  135. struct macsec_rx_sc *sc;
  136. struct rcu_head rcu;
  137. };
  138. struct pcpu_rx_sc_stats {
  139. struct macsec_rx_sc_stats stats;
  140. struct u64_stats_sync syncp;
  141. };
  142. /**
  143. * struct macsec_rx_sc - receive secure channel
  144. * @sci: secure channel identifier for this SC
  145. * @active: channel is active
  146. * @sa: array of secure associations
  147. * @stats: per-SC stats
  148. */
  149. struct macsec_rx_sc {
  150. struct macsec_rx_sc __rcu *next;
  151. sci_t sci;
  152. bool active;
  153. struct macsec_rx_sa __rcu *sa[MACSEC_NUM_AN];
  154. struct pcpu_rx_sc_stats __percpu *stats;
  155. refcount_t refcnt;
  156. struct rcu_head rcu_head;
  157. };
  158. /**
  159. * struct macsec_tx_sa - transmit secure association
  160. * @active:
  161. * @next_pn: packet number to use for the next packet
  162. * @lock: protects next_pn manipulations
  163. * @key: key structure
  164. * @stats: per-SA stats
  165. */
  166. struct macsec_tx_sa {
  167. struct macsec_key key;
  168. spinlock_t lock;
  169. u32 next_pn;
  170. refcount_t refcnt;
  171. bool active;
  172. struct macsec_tx_sa_stats __percpu *stats;
  173. struct rcu_head rcu;
  174. };
  175. struct pcpu_tx_sc_stats {
  176. struct macsec_tx_sc_stats stats;
  177. struct u64_stats_sync syncp;
  178. };
  179. /**
  180. * struct macsec_tx_sc - transmit secure channel
  181. * @active:
  182. * @encoding_sa: association number of the SA currently in use
  183. * @encrypt: encrypt packets on transmit, or authenticate only
  184. * @send_sci: always include the SCI in the SecTAG
  185. * @end_station:
  186. * @scb: single copy broadcast flag
  187. * @sa: array of secure associations
  188. * @stats: stats for this TXSC
  189. */
  190. struct macsec_tx_sc {
  191. bool active;
  192. u8 encoding_sa;
  193. bool encrypt;
  194. bool send_sci;
  195. bool end_station;
  196. bool scb;
  197. struct macsec_tx_sa __rcu *sa[MACSEC_NUM_AN];
  198. struct pcpu_tx_sc_stats __percpu *stats;
  199. };
  200. #define MACSEC_VALIDATE_DEFAULT MACSEC_VALIDATE_STRICT
  201. /**
  202. * struct macsec_secy - MACsec Security Entity
  203. * @netdev: netdevice for this SecY
  204. * @n_rx_sc: number of receive secure channels configured on this SecY
  205. * @sci: secure channel identifier used for tx
  206. * @key_len: length of keys used by the cipher suite
  207. * @icv_len: length of ICV used by the cipher suite
  208. * @validate_frames: validation mode
  209. * @operational: MAC_Operational flag
  210. * @protect_frames: enable protection for this SecY
  211. * @replay_protect: enable packet number checks on receive
  212. * @replay_window: size of the replay window
  213. * @tx_sc: transmit secure channel
  214. * @rx_sc: linked list of receive secure channels
  215. */
  216. struct macsec_secy {
  217. struct net_device *netdev;
  218. unsigned int n_rx_sc;
  219. sci_t sci;
  220. u16 key_len;
  221. u16 icv_len;
  222. enum macsec_validation_type validate_frames;
  223. bool operational;
  224. bool protect_frames;
  225. bool replay_protect;
  226. u32 replay_window;
  227. struct macsec_tx_sc tx_sc;
  228. struct macsec_rx_sc __rcu *rx_sc;
  229. };
  230. struct pcpu_secy_stats {
  231. struct macsec_dev_stats stats;
  232. struct u64_stats_sync syncp;
  233. };
  234. /**
  235. * struct macsec_dev - private data
  236. * @secy: SecY config
  237. * @real_dev: pointer to underlying netdevice
  238. * @stats: MACsec device stats
  239. * @secys: linked list of SecY's on the underlying device
  240. */
  241. struct macsec_dev {
  242. struct macsec_secy secy;
  243. struct net_device *real_dev;
  244. struct pcpu_secy_stats __percpu *stats;
  245. struct list_head secys;
  246. struct gro_cells gro_cells;
  247. unsigned int nest_level;
  248. };
  249. /**
  250. * struct macsec_rxh_data - rx_handler private argument
  251. * @secys: linked list of SecY's on this underlying device
  252. */
  253. struct macsec_rxh_data {
  254. struct list_head secys;
  255. };
  256. static struct macsec_dev *macsec_priv(const struct net_device *dev)
  257. {
  258. return (struct macsec_dev *)netdev_priv(dev);
  259. }
  260. static struct macsec_rxh_data *macsec_data_rcu(const struct net_device *dev)
  261. {
  262. return rcu_dereference_bh(dev->rx_handler_data);
  263. }
  264. static struct macsec_rxh_data *macsec_data_rtnl(const struct net_device *dev)
  265. {
  266. return rtnl_dereference(dev->rx_handler_data);
  267. }
  268. struct macsec_cb {
  269. struct aead_request *req;
  270. union {
  271. struct macsec_tx_sa *tx_sa;
  272. struct macsec_rx_sa *rx_sa;
  273. };
  274. u8 assoc_num;
  275. bool valid;
  276. bool has_sci;
  277. };
  278. static struct macsec_rx_sa *macsec_rxsa_get(struct macsec_rx_sa __rcu *ptr)
  279. {
  280. struct macsec_rx_sa *sa = rcu_dereference_bh(ptr);
  281. if (!sa || !sa->active)
  282. return NULL;
  283. if (!refcount_inc_not_zero(&sa->refcnt))
  284. return NULL;
  285. return sa;
  286. }
  287. static void free_rx_sc_rcu(struct rcu_head *head)
  288. {
  289. struct macsec_rx_sc *rx_sc = container_of(head, struct macsec_rx_sc, rcu_head);
  290. free_percpu(rx_sc->stats);
  291. kfree(rx_sc);
  292. }
  293. static struct macsec_rx_sc *macsec_rxsc_get(struct macsec_rx_sc *sc)
  294. {
  295. return refcount_inc_not_zero(&sc->refcnt) ? sc : NULL;
  296. }
  297. static void macsec_rxsc_put(struct macsec_rx_sc *sc)
  298. {
  299. if (refcount_dec_and_test(&sc->refcnt))
  300. call_rcu(&sc->rcu_head, free_rx_sc_rcu);
  301. }
  302. static void free_rxsa(struct rcu_head *head)
  303. {
  304. struct macsec_rx_sa *sa = container_of(head, struct macsec_rx_sa, rcu);
  305. crypto_free_aead(sa->key.tfm);
  306. free_percpu(sa->stats);
  307. kfree(sa);
  308. }
  309. static void macsec_rxsa_put(struct macsec_rx_sa *sa)
  310. {
  311. if (refcount_dec_and_test(&sa->refcnt))
  312. call_rcu(&sa->rcu, free_rxsa);
  313. }
  314. static struct macsec_tx_sa *macsec_txsa_get(struct macsec_tx_sa __rcu *ptr)
  315. {
  316. struct macsec_tx_sa *sa = rcu_dereference_bh(ptr);
  317. if (!sa || !sa->active)
  318. return NULL;
  319. if (!refcount_inc_not_zero(&sa->refcnt))
  320. return NULL;
  321. return sa;
  322. }
  323. static void free_txsa(struct rcu_head *head)
  324. {
  325. struct macsec_tx_sa *sa = container_of(head, struct macsec_tx_sa, rcu);
  326. crypto_free_aead(sa->key.tfm);
  327. free_percpu(sa->stats);
  328. kfree(sa);
  329. }
  330. static void macsec_txsa_put(struct macsec_tx_sa *sa)
  331. {
  332. if (refcount_dec_and_test(&sa->refcnt))
  333. call_rcu(&sa->rcu, free_txsa);
  334. }
  335. static struct macsec_cb *macsec_skb_cb(struct sk_buff *skb)
  336. {
  337. BUILD_BUG_ON(sizeof(struct macsec_cb) > sizeof(skb->cb));
  338. return (struct macsec_cb *)skb->cb;
  339. }
  340. #define MACSEC_PORT_ES (htons(0x0001))
  341. #define MACSEC_PORT_SCB (0x0000)
  342. #define MACSEC_UNDEF_SCI ((__force sci_t)0xffffffffffffffffULL)
  343. #define MACSEC_GCM_AES_128_SAK_LEN 16
  344. #define MACSEC_GCM_AES_256_SAK_LEN 32
  345. #define DEFAULT_SAK_LEN MACSEC_GCM_AES_128_SAK_LEN
  346. #define DEFAULT_SEND_SCI true
  347. #define DEFAULT_ENCRYPT false
  348. #define DEFAULT_ENCODING_SA 0
  349. static bool send_sci(const struct macsec_secy *secy)
  350. {
  351. const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  352. return tx_sc->send_sci ||
  353. (secy->n_rx_sc > 1 && !tx_sc->end_station && !tx_sc->scb);
  354. }
  355. static sci_t make_sci(u8 *addr, __be16 port)
  356. {
  357. sci_t sci;
  358. memcpy(&sci, addr, ETH_ALEN);
  359. memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
  360. return sci;
  361. }
  362. static sci_t macsec_frame_sci(struct macsec_eth_header *hdr, bool sci_present)
  363. {
  364. sci_t sci;
  365. if (sci_present)
  366. memcpy(&sci, hdr->secure_channel_id,
  367. sizeof(hdr->secure_channel_id));
  368. else
  369. sci = make_sci(hdr->eth.h_source, MACSEC_PORT_ES);
  370. return sci;
  371. }
  372. static unsigned int macsec_sectag_len(bool sci_present)
  373. {
  374. return MACSEC_TAG_LEN + (sci_present ? MACSEC_SCI_LEN : 0);
  375. }
  376. static unsigned int macsec_hdr_len(bool sci_present)
  377. {
  378. return macsec_sectag_len(sci_present) + ETH_HLEN;
  379. }
  380. static unsigned int macsec_extra_len(bool sci_present)
  381. {
  382. return macsec_sectag_len(sci_present) + sizeof(__be16);
  383. }
  384. /* Fill SecTAG according to IEEE 802.1AE-2006 10.5.3 */
  385. static void macsec_fill_sectag(struct macsec_eth_header *h,
  386. const struct macsec_secy *secy, u32 pn,
  387. bool sci_present)
  388. {
  389. const struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  390. memset(&h->tci_an, 0, macsec_sectag_len(sci_present));
  391. h->eth.h_proto = htons(ETH_P_MACSEC);
  392. if (sci_present) {
  393. h->tci_an |= MACSEC_TCI_SC;
  394. memcpy(&h->secure_channel_id, &secy->sci,
  395. sizeof(h->secure_channel_id));
  396. } else {
  397. if (tx_sc->end_station)
  398. h->tci_an |= MACSEC_TCI_ES;
  399. if (tx_sc->scb)
  400. h->tci_an |= MACSEC_TCI_SCB;
  401. }
  402. h->packet_number = htonl(pn);
  403. /* with GCM, C/E clear for !encrypt, both set for encrypt */
  404. if (tx_sc->encrypt)
  405. h->tci_an |= MACSEC_TCI_CONFID;
  406. else if (secy->icv_len != DEFAULT_ICV_LEN)
  407. h->tci_an |= MACSEC_TCI_C;
  408. h->tci_an |= tx_sc->encoding_sa;
  409. }
  410. static void macsec_set_shortlen(struct macsec_eth_header *h, size_t data_len)
  411. {
  412. if (data_len < MIN_NON_SHORT_LEN)
  413. h->short_length = data_len;
  414. }
  415. /* validate MACsec packet according to IEEE 802.1AE-2006 9.12 */
  416. static bool macsec_validate_skb(struct sk_buff *skb, u16 icv_len)
  417. {
  418. struct macsec_eth_header *h = (struct macsec_eth_header *)skb->data;
  419. int len = skb->len - 2 * ETH_ALEN;
  420. int extra_len = macsec_extra_len(!!(h->tci_an & MACSEC_TCI_SC)) + icv_len;
  421. /* a) It comprises at least 17 octets */
  422. if (skb->len <= 16)
  423. return false;
  424. /* b) MACsec EtherType: already checked */
  425. /* c) V bit is clear */
  426. if (h->tci_an & MACSEC_TCI_VERSION)
  427. return false;
  428. /* d) ES or SCB => !SC */
  429. if ((h->tci_an & MACSEC_TCI_ES || h->tci_an & MACSEC_TCI_SCB) &&
  430. (h->tci_an & MACSEC_TCI_SC))
  431. return false;
  432. /* e) Bits 7 and 8 of octet 4 of the SecTAG are clear */
  433. if (h->unused)
  434. return false;
  435. /* rx.pn != 0 (figure 10-5) */
  436. if (!h->packet_number)
  437. return false;
  438. /* length check, f) g) h) i) */
  439. if (h->short_length)
  440. return len == extra_len + h->short_length;
  441. return len >= extra_len + MIN_NON_SHORT_LEN;
  442. }
  443. #define MACSEC_NEEDED_HEADROOM (macsec_extra_len(true))
  444. #define MACSEC_NEEDED_TAILROOM MACSEC_STD_ICV_LEN
  445. static void macsec_fill_iv(unsigned char *iv, sci_t sci, u32 pn)
  446. {
  447. struct gcm_iv *gcm_iv = (struct gcm_iv *)iv;
  448. gcm_iv->sci = sci;
  449. gcm_iv->pn = htonl(pn);
  450. }
  451. static struct macsec_eth_header *macsec_ethhdr(struct sk_buff *skb)
  452. {
  453. return (struct macsec_eth_header *)skb_mac_header(skb);
  454. }
  455. static u32 tx_sa_update_pn(struct macsec_tx_sa *tx_sa, struct macsec_secy *secy)
  456. {
  457. u32 pn;
  458. spin_lock_bh(&tx_sa->lock);
  459. pn = tx_sa->next_pn;
  460. tx_sa->next_pn++;
  461. if (tx_sa->next_pn == 0) {
  462. pr_debug("PN wrapped, transitioning to !oper\n");
  463. tx_sa->active = false;
  464. if (secy->protect_frames)
  465. secy->operational = false;
  466. }
  467. spin_unlock_bh(&tx_sa->lock);
  468. return pn;
  469. }
  470. static void macsec_encrypt_finish(struct sk_buff *skb, struct net_device *dev)
  471. {
  472. struct macsec_dev *macsec = netdev_priv(dev);
  473. skb->dev = macsec->real_dev;
  474. skb_reset_mac_header(skb);
  475. skb->protocol = eth_hdr(skb)->h_proto;
  476. }
  477. static void macsec_count_tx(struct sk_buff *skb, struct macsec_tx_sc *tx_sc,
  478. struct macsec_tx_sa *tx_sa)
  479. {
  480. struct pcpu_tx_sc_stats *txsc_stats = this_cpu_ptr(tx_sc->stats);
  481. u64_stats_update_begin(&txsc_stats->syncp);
  482. if (tx_sc->encrypt) {
  483. txsc_stats->stats.OutOctetsEncrypted += skb->len;
  484. txsc_stats->stats.OutPktsEncrypted++;
  485. this_cpu_inc(tx_sa->stats->OutPktsEncrypted);
  486. } else {
  487. txsc_stats->stats.OutOctetsProtected += skb->len;
  488. txsc_stats->stats.OutPktsProtected++;
  489. this_cpu_inc(tx_sa->stats->OutPktsProtected);
  490. }
  491. u64_stats_update_end(&txsc_stats->syncp);
  492. }
  493. static void count_tx(struct net_device *dev, int ret, int len)
  494. {
  495. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  496. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  497. u64_stats_update_begin(&stats->syncp);
  498. stats->tx_packets++;
  499. stats->tx_bytes += len;
  500. u64_stats_update_end(&stats->syncp);
  501. }
  502. }
  503. static void macsec_encrypt_done(struct crypto_async_request *base, int err)
  504. {
  505. struct sk_buff *skb = base->data;
  506. struct net_device *dev = skb->dev;
  507. struct macsec_dev *macsec = macsec_priv(dev);
  508. struct macsec_tx_sa *sa = macsec_skb_cb(skb)->tx_sa;
  509. int len, ret;
  510. aead_request_free(macsec_skb_cb(skb)->req);
  511. rcu_read_lock_bh();
  512. macsec_encrypt_finish(skb, dev);
  513. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  514. len = skb->len;
  515. ret = dev_queue_xmit(skb);
  516. count_tx(dev, ret, len);
  517. rcu_read_unlock_bh();
  518. macsec_txsa_put(sa);
  519. dev_put(dev);
  520. }
  521. static struct aead_request *macsec_alloc_req(struct crypto_aead *tfm,
  522. unsigned char **iv,
  523. struct scatterlist **sg,
  524. int num_frags)
  525. {
  526. size_t size, iv_offset, sg_offset;
  527. struct aead_request *req;
  528. void *tmp;
  529. size = sizeof(struct aead_request) + crypto_aead_reqsize(tfm);
  530. iv_offset = size;
  531. size += GCM_AES_IV_LEN;
  532. size = ALIGN(size, __alignof__(struct scatterlist));
  533. sg_offset = size;
  534. size += sizeof(struct scatterlist) * num_frags;
  535. tmp = kmalloc(size, GFP_ATOMIC);
  536. if (!tmp)
  537. return NULL;
  538. *iv = (unsigned char *)(tmp + iv_offset);
  539. *sg = (struct scatterlist *)(tmp + sg_offset);
  540. req = tmp;
  541. aead_request_set_tfm(req, tfm);
  542. return req;
  543. }
  544. static struct sk_buff *macsec_encrypt(struct sk_buff *skb,
  545. struct net_device *dev)
  546. {
  547. int ret;
  548. struct scatterlist *sg;
  549. struct sk_buff *trailer;
  550. unsigned char *iv;
  551. struct ethhdr *eth;
  552. struct macsec_eth_header *hh;
  553. size_t unprotected_len;
  554. struct aead_request *req;
  555. struct macsec_secy *secy;
  556. struct macsec_tx_sc *tx_sc;
  557. struct macsec_tx_sa *tx_sa;
  558. struct macsec_dev *macsec = macsec_priv(dev);
  559. bool sci_present;
  560. u32 pn;
  561. secy = &macsec->secy;
  562. tx_sc = &secy->tx_sc;
  563. /* 10.5.1 TX SA assignment */
  564. tx_sa = macsec_txsa_get(tx_sc->sa[tx_sc->encoding_sa]);
  565. if (!tx_sa) {
  566. secy->operational = false;
  567. kfree_skb(skb);
  568. return ERR_PTR(-EINVAL);
  569. }
  570. if (unlikely(skb_headroom(skb) < MACSEC_NEEDED_HEADROOM ||
  571. skb_tailroom(skb) < MACSEC_NEEDED_TAILROOM)) {
  572. struct sk_buff *nskb = skb_copy_expand(skb,
  573. MACSEC_NEEDED_HEADROOM,
  574. MACSEC_NEEDED_TAILROOM,
  575. GFP_ATOMIC);
  576. if (likely(nskb)) {
  577. consume_skb(skb);
  578. skb = nskb;
  579. } else {
  580. macsec_txsa_put(tx_sa);
  581. kfree_skb(skb);
  582. return ERR_PTR(-ENOMEM);
  583. }
  584. } else {
  585. skb = skb_unshare(skb, GFP_ATOMIC);
  586. if (!skb) {
  587. macsec_txsa_put(tx_sa);
  588. return ERR_PTR(-ENOMEM);
  589. }
  590. }
  591. unprotected_len = skb->len;
  592. eth = eth_hdr(skb);
  593. sci_present = send_sci(secy);
  594. hh = skb_push(skb, macsec_extra_len(sci_present));
  595. memmove(hh, eth, 2 * ETH_ALEN);
  596. pn = tx_sa_update_pn(tx_sa, secy);
  597. if (pn == 0) {
  598. macsec_txsa_put(tx_sa);
  599. kfree_skb(skb);
  600. return ERR_PTR(-ENOLINK);
  601. }
  602. macsec_fill_sectag(hh, secy, pn, sci_present);
  603. macsec_set_shortlen(hh, unprotected_len - 2 * ETH_ALEN);
  604. skb_put(skb, secy->icv_len);
  605. if (skb->len - ETH_HLEN > macsec_priv(dev)->real_dev->mtu) {
  606. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  607. u64_stats_update_begin(&secy_stats->syncp);
  608. secy_stats->stats.OutPktsTooLong++;
  609. u64_stats_update_end(&secy_stats->syncp);
  610. macsec_txsa_put(tx_sa);
  611. kfree_skb(skb);
  612. return ERR_PTR(-EINVAL);
  613. }
  614. ret = skb_cow_data(skb, 0, &trailer);
  615. if (unlikely(ret < 0)) {
  616. macsec_txsa_put(tx_sa);
  617. kfree_skb(skb);
  618. return ERR_PTR(ret);
  619. }
  620. req = macsec_alloc_req(tx_sa->key.tfm, &iv, &sg, ret);
  621. if (!req) {
  622. macsec_txsa_put(tx_sa);
  623. kfree_skb(skb);
  624. return ERR_PTR(-ENOMEM);
  625. }
  626. macsec_fill_iv(iv, secy->sci, pn);
  627. sg_init_table(sg, ret);
  628. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  629. if (unlikely(ret < 0)) {
  630. aead_request_free(req);
  631. macsec_txsa_put(tx_sa);
  632. kfree_skb(skb);
  633. return ERR_PTR(ret);
  634. }
  635. if (tx_sc->encrypt) {
  636. int len = skb->len - macsec_hdr_len(sci_present) -
  637. secy->icv_len;
  638. aead_request_set_crypt(req, sg, sg, len, iv);
  639. aead_request_set_ad(req, macsec_hdr_len(sci_present));
  640. } else {
  641. aead_request_set_crypt(req, sg, sg, 0, iv);
  642. aead_request_set_ad(req, skb->len - secy->icv_len);
  643. }
  644. macsec_skb_cb(skb)->req = req;
  645. macsec_skb_cb(skb)->tx_sa = tx_sa;
  646. aead_request_set_callback(req, 0, macsec_encrypt_done, skb);
  647. dev_hold(skb->dev);
  648. ret = crypto_aead_encrypt(req);
  649. if (ret == -EINPROGRESS) {
  650. return ERR_PTR(ret);
  651. } else if (ret != 0) {
  652. dev_put(skb->dev);
  653. kfree_skb(skb);
  654. aead_request_free(req);
  655. macsec_txsa_put(tx_sa);
  656. return ERR_PTR(-EINVAL);
  657. }
  658. dev_put(skb->dev);
  659. aead_request_free(req);
  660. macsec_txsa_put(tx_sa);
  661. return skb;
  662. }
  663. static bool macsec_post_decrypt(struct sk_buff *skb, struct macsec_secy *secy, u32 pn)
  664. {
  665. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  666. struct pcpu_rx_sc_stats *rxsc_stats = this_cpu_ptr(rx_sa->sc->stats);
  667. struct macsec_eth_header *hdr = macsec_ethhdr(skb);
  668. u32 lowest_pn = 0;
  669. spin_lock(&rx_sa->lock);
  670. if (rx_sa->next_pn >= secy->replay_window)
  671. lowest_pn = rx_sa->next_pn - secy->replay_window;
  672. /* Now perform replay protection check again
  673. * (see IEEE 802.1AE-2006 figure 10-5)
  674. */
  675. if (secy->replay_protect && pn < lowest_pn) {
  676. spin_unlock(&rx_sa->lock);
  677. u64_stats_update_begin(&rxsc_stats->syncp);
  678. rxsc_stats->stats.InPktsLate++;
  679. u64_stats_update_end(&rxsc_stats->syncp);
  680. return false;
  681. }
  682. if (secy->validate_frames != MACSEC_VALIDATE_DISABLED) {
  683. u64_stats_update_begin(&rxsc_stats->syncp);
  684. if (hdr->tci_an & MACSEC_TCI_E)
  685. rxsc_stats->stats.InOctetsDecrypted += skb->len;
  686. else
  687. rxsc_stats->stats.InOctetsValidated += skb->len;
  688. u64_stats_update_end(&rxsc_stats->syncp);
  689. }
  690. if (!macsec_skb_cb(skb)->valid) {
  691. spin_unlock(&rx_sa->lock);
  692. /* 10.6.5 */
  693. if (hdr->tci_an & MACSEC_TCI_C ||
  694. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  695. u64_stats_update_begin(&rxsc_stats->syncp);
  696. rxsc_stats->stats.InPktsNotValid++;
  697. u64_stats_update_end(&rxsc_stats->syncp);
  698. return false;
  699. }
  700. u64_stats_update_begin(&rxsc_stats->syncp);
  701. if (secy->validate_frames == MACSEC_VALIDATE_CHECK) {
  702. rxsc_stats->stats.InPktsInvalid++;
  703. this_cpu_inc(rx_sa->stats->InPktsInvalid);
  704. } else if (pn < lowest_pn) {
  705. rxsc_stats->stats.InPktsDelayed++;
  706. } else {
  707. rxsc_stats->stats.InPktsUnchecked++;
  708. }
  709. u64_stats_update_end(&rxsc_stats->syncp);
  710. } else {
  711. u64_stats_update_begin(&rxsc_stats->syncp);
  712. if (pn < lowest_pn) {
  713. rxsc_stats->stats.InPktsDelayed++;
  714. } else {
  715. rxsc_stats->stats.InPktsOK++;
  716. this_cpu_inc(rx_sa->stats->InPktsOK);
  717. }
  718. u64_stats_update_end(&rxsc_stats->syncp);
  719. if (pn >= rx_sa->next_pn)
  720. rx_sa->next_pn = pn + 1;
  721. spin_unlock(&rx_sa->lock);
  722. }
  723. return true;
  724. }
  725. static void macsec_reset_skb(struct sk_buff *skb, struct net_device *dev)
  726. {
  727. skb->pkt_type = PACKET_HOST;
  728. skb->protocol = eth_type_trans(skb, dev);
  729. skb_reset_network_header(skb);
  730. if (!skb_transport_header_was_set(skb))
  731. skb_reset_transport_header(skb);
  732. skb_reset_mac_len(skb);
  733. }
  734. static void macsec_finalize_skb(struct sk_buff *skb, u8 icv_len, u8 hdr_len)
  735. {
  736. skb->ip_summed = CHECKSUM_NONE;
  737. memmove(skb->data + hdr_len, skb->data, 2 * ETH_ALEN);
  738. skb_pull(skb, hdr_len);
  739. pskb_trim_unique(skb, skb->len - icv_len);
  740. }
  741. static void count_rx(struct net_device *dev, int len)
  742. {
  743. struct pcpu_sw_netstats *stats = this_cpu_ptr(dev->tstats);
  744. u64_stats_update_begin(&stats->syncp);
  745. stats->rx_packets++;
  746. stats->rx_bytes += len;
  747. u64_stats_update_end(&stats->syncp);
  748. }
  749. static void macsec_decrypt_done(struct crypto_async_request *base, int err)
  750. {
  751. struct sk_buff *skb = base->data;
  752. struct net_device *dev = skb->dev;
  753. struct macsec_dev *macsec = macsec_priv(dev);
  754. struct macsec_rx_sa *rx_sa = macsec_skb_cb(skb)->rx_sa;
  755. struct macsec_rx_sc *rx_sc = rx_sa->sc;
  756. int len;
  757. u32 pn;
  758. aead_request_free(macsec_skb_cb(skb)->req);
  759. if (!err)
  760. macsec_skb_cb(skb)->valid = true;
  761. rcu_read_lock_bh();
  762. pn = ntohl(macsec_ethhdr(skb)->packet_number);
  763. if (!macsec_post_decrypt(skb, &macsec->secy, pn)) {
  764. rcu_read_unlock_bh();
  765. kfree_skb(skb);
  766. goto out;
  767. }
  768. macsec_finalize_skb(skb, macsec->secy.icv_len,
  769. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  770. macsec_reset_skb(skb, macsec->secy.netdev);
  771. len = skb->len;
  772. if (gro_cells_receive(&macsec->gro_cells, skb) == NET_RX_SUCCESS)
  773. count_rx(dev, len);
  774. rcu_read_unlock_bh();
  775. out:
  776. macsec_rxsa_put(rx_sa);
  777. macsec_rxsc_put(rx_sc);
  778. dev_put(dev);
  779. }
  780. static struct sk_buff *macsec_decrypt(struct sk_buff *skb,
  781. struct net_device *dev,
  782. struct macsec_rx_sa *rx_sa,
  783. sci_t sci,
  784. struct macsec_secy *secy)
  785. {
  786. int ret;
  787. struct scatterlist *sg;
  788. struct sk_buff *trailer;
  789. unsigned char *iv;
  790. struct aead_request *req;
  791. struct macsec_eth_header *hdr;
  792. u16 icv_len = secy->icv_len;
  793. macsec_skb_cb(skb)->valid = false;
  794. skb = skb_share_check(skb, GFP_ATOMIC);
  795. if (!skb)
  796. return ERR_PTR(-ENOMEM);
  797. ret = skb_cow_data(skb, 0, &trailer);
  798. if (unlikely(ret < 0)) {
  799. kfree_skb(skb);
  800. return ERR_PTR(ret);
  801. }
  802. req = macsec_alloc_req(rx_sa->key.tfm, &iv, &sg, ret);
  803. if (!req) {
  804. kfree_skb(skb);
  805. return ERR_PTR(-ENOMEM);
  806. }
  807. hdr = (struct macsec_eth_header *)skb->data;
  808. macsec_fill_iv(iv, sci, ntohl(hdr->packet_number));
  809. sg_init_table(sg, ret);
  810. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  811. if (unlikely(ret < 0)) {
  812. aead_request_free(req);
  813. kfree_skb(skb);
  814. return ERR_PTR(ret);
  815. }
  816. if (hdr->tci_an & MACSEC_TCI_E) {
  817. /* confidentiality: ethernet + macsec header
  818. * authenticated, encrypted payload
  819. */
  820. int len = skb->len - macsec_hdr_len(macsec_skb_cb(skb)->has_sci);
  821. aead_request_set_crypt(req, sg, sg, len, iv);
  822. aead_request_set_ad(req, macsec_hdr_len(macsec_skb_cb(skb)->has_sci));
  823. skb = skb_unshare(skb, GFP_ATOMIC);
  824. if (!skb) {
  825. aead_request_free(req);
  826. return ERR_PTR(-ENOMEM);
  827. }
  828. } else {
  829. /* integrity only: all headers + data authenticated */
  830. aead_request_set_crypt(req, sg, sg, icv_len, iv);
  831. aead_request_set_ad(req, skb->len - icv_len);
  832. }
  833. macsec_skb_cb(skb)->req = req;
  834. skb->dev = dev;
  835. aead_request_set_callback(req, 0, macsec_decrypt_done, skb);
  836. dev_hold(dev);
  837. ret = crypto_aead_decrypt(req);
  838. if (ret == -EINPROGRESS) {
  839. return ERR_PTR(ret);
  840. } else if (ret != 0) {
  841. /* decryption/authentication failed
  842. * 10.6 if validateFrames is disabled, deliver anyway
  843. */
  844. if (ret != -EBADMSG) {
  845. kfree_skb(skb);
  846. skb = ERR_PTR(ret);
  847. }
  848. } else {
  849. macsec_skb_cb(skb)->valid = true;
  850. }
  851. dev_put(dev);
  852. aead_request_free(req);
  853. return skb;
  854. }
  855. static struct macsec_rx_sc *find_rx_sc(struct macsec_secy *secy, sci_t sci)
  856. {
  857. struct macsec_rx_sc *rx_sc;
  858. for_each_rxsc(secy, rx_sc) {
  859. if (rx_sc->sci == sci)
  860. return rx_sc;
  861. }
  862. return NULL;
  863. }
  864. static struct macsec_rx_sc *find_rx_sc_rtnl(struct macsec_secy *secy, sci_t sci)
  865. {
  866. struct macsec_rx_sc *rx_sc;
  867. for_each_rxsc_rtnl(secy, rx_sc) {
  868. if (rx_sc->sci == sci)
  869. return rx_sc;
  870. }
  871. return NULL;
  872. }
  873. static void handle_not_macsec(struct sk_buff *skb)
  874. {
  875. struct macsec_rxh_data *rxd;
  876. struct macsec_dev *macsec;
  877. rcu_read_lock();
  878. rxd = macsec_data_rcu(skb->dev);
  879. /* 10.6 If the management control validateFrames is not
  880. * Strict, frames without a SecTAG are received, counted, and
  881. * delivered to the Controlled Port
  882. */
  883. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  884. struct sk_buff *nskb;
  885. struct pcpu_secy_stats *secy_stats = this_cpu_ptr(macsec->stats);
  886. if (macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  887. u64_stats_update_begin(&secy_stats->syncp);
  888. secy_stats->stats.InPktsNoTag++;
  889. u64_stats_update_end(&secy_stats->syncp);
  890. continue;
  891. }
  892. /* deliver on this port */
  893. nskb = skb_clone(skb, GFP_ATOMIC);
  894. if (!nskb)
  895. break;
  896. nskb->dev = macsec->secy.netdev;
  897. if (netif_rx(nskb) == NET_RX_SUCCESS) {
  898. u64_stats_update_begin(&secy_stats->syncp);
  899. secy_stats->stats.InPktsUntagged++;
  900. u64_stats_update_end(&secy_stats->syncp);
  901. }
  902. }
  903. rcu_read_unlock();
  904. }
  905. static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
  906. {
  907. struct sk_buff *skb = *pskb;
  908. struct net_device *dev = skb->dev;
  909. struct macsec_eth_header *hdr;
  910. struct macsec_secy *secy = NULL;
  911. struct macsec_rx_sc *rx_sc;
  912. struct macsec_rx_sa *rx_sa;
  913. struct macsec_rxh_data *rxd;
  914. struct macsec_dev *macsec;
  915. sci_t sci;
  916. u32 pn;
  917. bool cbit;
  918. struct pcpu_rx_sc_stats *rxsc_stats;
  919. struct pcpu_secy_stats *secy_stats;
  920. bool pulled_sci;
  921. int ret;
  922. if (skb_headroom(skb) < ETH_HLEN)
  923. goto drop_direct;
  924. hdr = macsec_ethhdr(skb);
  925. if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
  926. handle_not_macsec(skb);
  927. /* and deliver to the uncontrolled port */
  928. return RX_HANDLER_PASS;
  929. }
  930. skb = skb_unshare(skb, GFP_ATOMIC);
  931. *pskb = skb;
  932. if (!skb)
  933. return RX_HANDLER_CONSUMED;
  934. pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
  935. if (!pulled_sci) {
  936. if (!pskb_may_pull(skb, macsec_extra_len(false)))
  937. goto drop_direct;
  938. }
  939. hdr = macsec_ethhdr(skb);
  940. /* Frames with a SecTAG that has the TCI E bit set but the C
  941. * bit clear are discarded, as this reserved encoding is used
  942. * to identify frames with a SecTAG that are not to be
  943. * delivered to the Controlled Port.
  944. */
  945. if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
  946. return RX_HANDLER_PASS;
  947. /* now, pull the extra length */
  948. if (hdr->tci_an & MACSEC_TCI_SC) {
  949. if (!pulled_sci)
  950. goto drop_direct;
  951. }
  952. /* ethernet header is part of crypto processing */
  953. skb_push(skb, ETH_HLEN);
  954. macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
  955. macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
  956. sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
  957. rcu_read_lock();
  958. rxd = macsec_data_rcu(skb->dev);
  959. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  960. struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
  961. sc = sc ? macsec_rxsc_get(sc) : NULL;
  962. if (sc) {
  963. secy = &macsec->secy;
  964. rx_sc = sc;
  965. break;
  966. }
  967. }
  968. if (!secy)
  969. goto nosci;
  970. dev = secy->netdev;
  971. macsec = macsec_priv(dev);
  972. secy_stats = this_cpu_ptr(macsec->stats);
  973. rxsc_stats = this_cpu_ptr(rx_sc->stats);
  974. if (!macsec_validate_skb(skb, secy->icv_len)) {
  975. u64_stats_update_begin(&secy_stats->syncp);
  976. secy_stats->stats.InPktsBadTag++;
  977. u64_stats_update_end(&secy_stats->syncp);
  978. goto drop_nosa;
  979. }
  980. rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
  981. if (!rx_sa) {
  982. /* 10.6.1 if the SA is not in use */
  983. /* If validateFrames is Strict or the C bit in the
  984. * SecTAG is set, discard
  985. */
  986. if (hdr->tci_an & MACSEC_TCI_C ||
  987. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  988. u64_stats_update_begin(&rxsc_stats->syncp);
  989. rxsc_stats->stats.InPktsNotUsingSA++;
  990. u64_stats_update_end(&rxsc_stats->syncp);
  991. goto drop_nosa;
  992. }
  993. /* not Strict, the frame (with the SecTAG and ICV
  994. * removed) is delivered to the Controlled Port.
  995. */
  996. u64_stats_update_begin(&rxsc_stats->syncp);
  997. rxsc_stats->stats.InPktsUnusedSA++;
  998. u64_stats_update_end(&rxsc_stats->syncp);
  999. goto deliver;
  1000. }
  1001. /* First, PN check to avoid decrypting obviously wrong packets */
  1002. pn = ntohl(hdr->packet_number);
  1003. if (secy->replay_protect) {
  1004. bool late;
  1005. spin_lock(&rx_sa->lock);
  1006. late = rx_sa->next_pn >= secy->replay_window &&
  1007. pn < (rx_sa->next_pn - secy->replay_window);
  1008. spin_unlock(&rx_sa->lock);
  1009. if (late) {
  1010. u64_stats_update_begin(&rxsc_stats->syncp);
  1011. rxsc_stats->stats.InPktsLate++;
  1012. u64_stats_update_end(&rxsc_stats->syncp);
  1013. goto drop;
  1014. }
  1015. }
  1016. macsec_skb_cb(skb)->rx_sa = rx_sa;
  1017. /* Disabled && !changed text => skip validation */
  1018. if (hdr->tci_an & MACSEC_TCI_C ||
  1019. secy->validate_frames != MACSEC_VALIDATE_DISABLED)
  1020. skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
  1021. if (IS_ERR(skb)) {
  1022. /* the decrypt callback needs the reference */
  1023. if (PTR_ERR(skb) != -EINPROGRESS) {
  1024. macsec_rxsa_put(rx_sa);
  1025. macsec_rxsc_put(rx_sc);
  1026. }
  1027. rcu_read_unlock();
  1028. *pskb = NULL;
  1029. return RX_HANDLER_CONSUMED;
  1030. }
  1031. if (!macsec_post_decrypt(skb, secy, pn))
  1032. goto drop;
  1033. deliver:
  1034. macsec_finalize_skb(skb, secy->icv_len,
  1035. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1036. macsec_reset_skb(skb, secy->netdev);
  1037. if (rx_sa)
  1038. macsec_rxsa_put(rx_sa);
  1039. macsec_rxsc_put(rx_sc);
  1040. skb_orphan(skb);
  1041. ret = gro_cells_receive(&macsec->gro_cells, skb);
  1042. if (ret == NET_RX_SUCCESS)
  1043. count_rx(dev, skb->len);
  1044. else
  1045. macsec->secy.netdev->stats.rx_dropped++;
  1046. rcu_read_unlock();
  1047. *pskb = NULL;
  1048. return RX_HANDLER_CONSUMED;
  1049. drop:
  1050. macsec_rxsa_put(rx_sa);
  1051. drop_nosa:
  1052. macsec_rxsc_put(rx_sc);
  1053. rcu_read_unlock();
  1054. drop_direct:
  1055. kfree_skb(skb);
  1056. *pskb = NULL;
  1057. return RX_HANDLER_CONSUMED;
  1058. nosci:
  1059. /* 10.6.1 if the SC is not found */
  1060. cbit = !!(hdr->tci_an & MACSEC_TCI_C);
  1061. if (!cbit)
  1062. macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
  1063. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1064. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  1065. struct sk_buff *nskb;
  1066. secy_stats = this_cpu_ptr(macsec->stats);
  1067. /* If validateFrames is Strict or the C bit in the
  1068. * SecTAG is set, discard
  1069. */
  1070. if (cbit ||
  1071. macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  1072. u64_stats_update_begin(&secy_stats->syncp);
  1073. secy_stats->stats.InPktsNoSCI++;
  1074. u64_stats_update_end(&secy_stats->syncp);
  1075. continue;
  1076. }
  1077. /* not strict, the frame (with the SecTAG and ICV
  1078. * removed) is delivered to the Controlled Port.
  1079. */
  1080. nskb = skb_clone(skb, GFP_ATOMIC);
  1081. if (!nskb)
  1082. break;
  1083. macsec_reset_skb(nskb, macsec->secy.netdev);
  1084. ret = netif_rx(nskb);
  1085. if (ret == NET_RX_SUCCESS) {
  1086. u64_stats_update_begin(&secy_stats->syncp);
  1087. secy_stats->stats.InPktsUnknownSCI++;
  1088. u64_stats_update_end(&secy_stats->syncp);
  1089. } else {
  1090. macsec->secy.netdev->stats.rx_dropped++;
  1091. }
  1092. }
  1093. rcu_read_unlock();
  1094. *pskb = skb;
  1095. return RX_HANDLER_PASS;
  1096. }
  1097. static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
  1098. {
  1099. struct crypto_aead *tfm;
  1100. int ret;
  1101. tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
  1102. if (IS_ERR(tfm))
  1103. return tfm;
  1104. ret = crypto_aead_setkey(tfm, key, key_len);
  1105. if (ret < 0)
  1106. goto fail;
  1107. ret = crypto_aead_setauthsize(tfm, icv_len);
  1108. if (ret < 0)
  1109. goto fail;
  1110. return tfm;
  1111. fail:
  1112. crypto_free_aead(tfm);
  1113. return ERR_PTR(ret);
  1114. }
  1115. static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
  1116. int icv_len)
  1117. {
  1118. rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
  1119. if (!rx_sa->stats)
  1120. return -ENOMEM;
  1121. rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1122. if (IS_ERR(rx_sa->key.tfm)) {
  1123. free_percpu(rx_sa->stats);
  1124. return PTR_ERR(rx_sa->key.tfm);
  1125. }
  1126. rx_sa->active = false;
  1127. rx_sa->next_pn = 1;
  1128. refcount_set(&rx_sa->refcnt, 1);
  1129. spin_lock_init(&rx_sa->lock);
  1130. return 0;
  1131. }
  1132. static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
  1133. {
  1134. rx_sa->active = false;
  1135. macsec_rxsa_put(rx_sa);
  1136. }
  1137. static void free_rx_sc(struct macsec_rx_sc *rx_sc)
  1138. {
  1139. int i;
  1140. for (i = 0; i < MACSEC_NUM_AN; i++) {
  1141. struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
  1142. RCU_INIT_POINTER(rx_sc->sa[i], NULL);
  1143. if (sa)
  1144. clear_rx_sa(sa);
  1145. }
  1146. macsec_rxsc_put(rx_sc);
  1147. }
  1148. static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
  1149. {
  1150. struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
  1151. for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
  1152. rx_sc;
  1153. rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
  1154. if (rx_sc->sci == sci) {
  1155. if (rx_sc->active)
  1156. secy->n_rx_sc--;
  1157. rcu_assign_pointer(*rx_scp, rx_sc->next);
  1158. return rx_sc;
  1159. }
  1160. }
  1161. return NULL;
  1162. }
  1163. static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
  1164. {
  1165. struct macsec_rx_sc *rx_sc;
  1166. struct macsec_dev *macsec;
  1167. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  1168. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  1169. struct macsec_secy *secy;
  1170. list_for_each_entry(macsec, &rxd->secys, secys) {
  1171. if (find_rx_sc_rtnl(&macsec->secy, sci))
  1172. return ERR_PTR(-EEXIST);
  1173. }
  1174. rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
  1175. if (!rx_sc)
  1176. return ERR_PTR(-ENOMEM);
  1177. rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
  1178. if (!rx_sc->stats) {
  1179. kfree(rx_sc);
  1180. return ERR_PTR(-ENOMEM);
  1181. }
  1182. rx_sc->sci = sci;
  1183. rx_sc->active = true;
  1184. refcount_set(&rx_sc->refcnt, 1);
  1185. secy = &macsec_priv(dev)->secy;
  1186. rcu_assign_pointer(rx_sc->next, secy->rx_sc);
  1187. rcu_assign_pointer(secy->rx_sc, rx_sc);
  1188. if (rx_sc->active)
  1189. secy->n_rx_sc++;
  1190. return rx_sc;
  1191. }
  1192. static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
  1193. int icv_len)
  1194. {
  1195. tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
  1196. if (!tx_sa->stats)
  1197. return -ENOMEM;
  1198. tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1199. if (IS_ERR(tx_sa->key.tfm)) {
  1200. free_percpu(tx_sa->stats);
  1201. return PTR_ERR(tx_sa->key.tfm);
  1202. }
  1203. tx_sa->active = false;
  1204. refcount_set(&tx_sa->refcnt, 1);
  1205. spin_lock_init(&tx_sa->lock);
  1206. return 0;
  1207. }
  1208. static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
  1209. {
  1210. tx_sa->active = false;
  1211. macsec_txsa_put(tx_sa);
  1212. }
  1213. static struct genl_family macsec_fam;
  1214. static struct net_device *get_dev_from_nl(struct net *net,
  1215. struct nlattr **attrs)
  1216. {
  1217. int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
  1218. struct net_device *dev;
  1219. dev = __dev_get_by_index(net, ifindex);
  1220. if (!dev)
  1221. return ERR_PTR(-ENODEV);
  1222. if (!netif_is_macsec(dev))
  1223. return ERR_PTR(-ENODEV);
  1224. return dev;
  1225. }
  1226. static sci_t nla_get_sci(const struct nlattr *nla)
  1227. {
  1228. return (__force sci_t)nla_get_u64(nla);
  1229. }
  1230. static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
  1231. int padattr)
  1232. {
  1233. return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
  1234. }
  1235. static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
  1236. struct nlattr **attrs,
  1237. struct nlattr **tb_sa,
  1238. struct net_device **devp,
  1239. struct macsec_secy **secyp,
  1240. struct macsec_tx_sc **scp,
  1241. u8 *assoc_num)
  1242. {
  1243. struct net_device *dev;
  1244. struct macsec_secy *secy;
  1245. struct macsec_tx_sc *tx_sc;
  1246. struct macsec_tx_sa *tx_sa;
  1247. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1248. return ERR_PTR(-EINVAL);
  1249. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1250. dev = get_dev_from_nl(net, attrs);
  1251. if (IS_ERR(dev))
  1252. return ERR_CAST(dev);
  1253. if (*assoc_num >= MACSEC_NUM_AN)
  1254. return ERR_PTR(-EINVAL);
  1255. secy = &macsec_priv(dev)->secy;
  1256. tx_sc = &secy->tx_sc;
  1257. tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
  1258. if (!tx_sa)
  1259. return ERR_PTR(-ENODEV);
  1260. *devp = dev;
  1261. *scp = tx_sc;
  1262. *secyp = secy;
  1263. return tx_sa;
  1264. }
  1265. static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
  1266. struct nlattr **attrs,
  1267. struct nlattr **tb_rxsc,
  1268. struct net_device **devp,
  1269. struct macsec_secy **secyp)
  1270. {
  1271. struct net_device *dev;
  1272. struct macsec_secy *secy;
  1273. struct macsec_rx_sc *rx_sc;
  1274. sci_t sci;
  1275. dev = get_dev_from_nl(net, attrs);
  1276. if (IS_ERR(dev))
  1277. return ERR_CAST(dev);
  1278. secy = &macsec_priv(dev)->secy;
  1279. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1280. return ERR_PTR(-EINVAL);
  1281. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1282. rx_sc = find_rx_sc_rtnl(secy, sci);
  1283. if (!rx_sc)
  1284. return ERR_PTR(-ENODEV);
  1285. *secyp = secy;
  1286. *devp = dev;
  1287. return rx_sc;
  1288. }
  1289. static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
  1290. struct nlattr **attrs,
  1291. struct nlattr **tb_rxsc,
  1292. struct nlattr **tb_sa,
  1293. struct net_device **devp,
  1294. struct macsec_secy **secyp,
  1295. struct macsec_rx_sc **scp,
  1296. u8 *assoc_num)
  1297. {
  1298. struct macsec_rx_sc *rx_sc;
  1299. struct macsec_rx_sa *rx_sa;
  1300. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1301. return ERR_PTR(-EINVAL);
  1302. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1303. if (*assoc_num >= MACSEC_NUM_AN)
  1304. return ERR_PTR(-EINVAL);
  1305. rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
  1306. if (IS_ERR(rx_sc))
  1307. return ERR_CAST(rx_sc);
  1308. rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
  1309. if (!rx_sa)
  1310. return ERR_PTR(-ENODEV);
  1311. *scp = rx_sc;
  1312. return rx_sa;
  1313. }
  1314. static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
  1315. [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
  1316. [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
  1317. [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
  1318. };
  1319. static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
  1320. [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
  1321. [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
  1322. };
  1323. static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
  1324. [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
  1325. [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
  1326. [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
  1327. [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
  1328. .len = MACSEC_KEYID_LEN, },
  1329. [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
  1330. .len = MACSEC_MAX_KEY_LEN, },
  1331. };
  1332. static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
  1333. {
  1334. if (!attrs[MACSEC_ATTR_SA_CONFIG])
  1335. return -EINVAL;
  1336. if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX,
  1337. attrs[MACSEC_ATTR_SA_CONFIG],
  1338. macsec_genl_sa_policy, NULL))
  1339. return -EINVAL;
  1340. return 0;
  1341. }
  1342. static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
  1343. {
  1344. if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
  1345. return -EINVAL;
  1346. if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX,
  1347. attrs[MACSEC_ATTR_RXSC_CONFIG],
  1348. macsec_genl_rxsc_policy, NULL))
  1349. return -EINVAL;
  1350. return 0;
  1351. }
  1352. static bool validate_add_rxsa(struct nlattr **attrs)
  1353. {
  1354. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1355. !attrs[MACSEC_SA_ATTR_KEY] ||
  1356. !attrs[MACSEC_SA_ATTR_KEYID])
  1357. return false;
  1358. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1359. return false;
  1360. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1361. return false;
  1362. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1363. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1364. return false;
  1365. }
  1366. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1367. return false;
  1368. return true;
  1369. }
  1370. static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
  1371. {
  1372. struct net_device *dev;
  1373. struct nlattr **attrs = info->attrs;
  1374. struct macsec_secy *secy;
  1375. struct macsec_rx_sc *rx_sc;
  1376. struct macsec_rx_sa *rx_sa;
  1377. unsigned char assoc_num;
  1378. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1379. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1380. int err;
  1381. if (!attrs[MACSEC_ATTR_IFINDEX])
  1382. return -EINVAL;
  1383. if (parse_sa_config(attrs, tb_sa))
  1384. return -EINVAL;
  1385. if (parse_rxsc_config(attrs, tb_rxsc))
  1386. return -EINVAL;
  1387. if (!validate_add_rxsa(tb_sa))
  1388. return -EINVAL;
  1389. rtnl_lock();
  1390. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1391. if (IS_ERR(rx_sc)) {
  1392. rtnl_unlock();
  1393. return PTR_ERR(rx_sc);
  1394. }
  1395. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1396. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1397. pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
  1398. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1399. rtnl_unlock();
  1400. return -EINVAL;
  1401. }
  1402. rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
  1403. if (rx_sa) {
  1404. rtnl_unlock();
  1405. return -EBUSY;
  1406. }
  1407. rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
  1408. if (!rx_sa) {
  1409. rtnl_unlock();
  1410. return -ENOMEM;
  1411. }
  1412. err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1413. secy->key_len, secy->icv_len);
  1414. if (err < 0) {
  1415. kfree(rx_sa);
  1416. rtnl_unlock();
  1417. return err;
  1418. }
  1419. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1420. spin_lock_bh(&rx_sa->lock);
  1421. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1422. spin_unlock_bh(&rx_sa->lock);
  1423. }
  1424. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1425. rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1426. nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1427. rx_sa->sc = rx_sc;
  1428. rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
  1429. rtnl_unlock();
  1430. return 0;
  1431. }
  1432. static bool validate_add_rxsc(struct nlattr **attrs)
  1433. {
  1434. if (!attrs[MACSEC_RXSC_ATTR_SCI])
  1435. return false;
  1436. if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
  1437. if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
  1438. return false;
  1439. }
  1440. return true;
  1441. }
  1442. static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
  1443. {
  1444. struct net_device *dev;
  1445. sci_t sci = MACSEC_UNDEF_SCI;
  1446. struct nlattr **attrs = info->attrs;
  1447. struct macsec_rx_sc *rx_sc;
  1448. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1449. if (!attrs[MACSEC_ATTR_IFINDEX])
  1450. return -EINVAL;
  1451. if (parse_rxsc_config(attrs, tb_rxsc))
  1452. return -EINVAL;
  1453. if (!validate_add_rxsc(tb_rxsc))
  1454. return -EINVAL;
  1455. rtnl_lock();
  1456. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1457. if (IS_ERR(dev)) {
  1458. rtnl_unlock();
  1459. return PTR_ERR(dev);
  1460. }
  1461. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1462. rx_sc = create_rx_sc(dev, sci);
  1463. if (IS_ERR(rx_sc)) {
  1464. rtnl_unlock();
  1465. return PTR_ERR(rx_sc);
  1466. }
  1467. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
  1468. rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1469. rtnl_unlock();
  1470. return 0;
  1471. }
  1472. static bool validate_add_txsa(struct nlattr **attrs)
  1473. {
  1474. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1475. !attrs[MACSEC_SA_ATTR_PN] ||
  1476. !attrs[MACSEC_SA_ATTR_KEY] ||
  1477. !attrs[MACSEC_SA_ATTR_KEYID])
  1478. return false;
  1479. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1480. return false;
  1481. if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1482. return false;
  1483. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1484. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1485. return false;
  1486. }
  1487. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1488. return false;
  1489. return true;
  1490. }
  1491. static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
  1492. {
  1493. struct net_device *dev;
  1494. struct nlattr **attrs = info->attrs;
  1495. struct macsec_secy *secy;
  1496. struct macsec_tx_sc *tx_sc;
  1497. struct macsec_tx_sa *tx_sa;
  1498. unsigned char assoc_num;
  1499. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1500. int err;
  1501. if (!attrs[MACSEC_ATTR_IFINDEX])
  1502. return -EINVAL;
  1503. if (parse_sa_config(attrs, tb_sa))
  1504. return -EINVAL;
  1505. if (!validate_add_txsa(tb_sa))
  1506. return -EINVAL;
  1507. rtnl_lock();
  1508. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1509. if (IS_ERR(dev)) {
  1510. rtnl_unlock();
  1511. return PTR_ERR(dev);
  1512. }
  1513. secy = &macsec_priv(dev)->secy;
  1514. tx_sc = &secy->tx_sc;
  1515. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1516. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1517. pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
  1518. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1519. rtnl_unlock();
  1520. return -EINVAL;
  1521. }
  1522. tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
  1523. if (tx_sa) {
  1524. rtnl_unlock();
  1525. return -EBUSY;
  1526. }
  1527. tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
  1528. if (!tx_sa) {
  1529. rtnl_unlock();
  1530. return -ENOMEM;
  1531. }
  1532. err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1533. secy->key_len, secy->icv_len);
  1534. if (err < 0) {
  1535. kfree(tx_sa);
  1536. rtnl_unlock();
  1537. return err;
  1538. }
  1539. nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1540. spin_lock_bh(&tx_sa->lock);
  1541. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1542. spin_unlock_bh(&tx_sa->lock);
  1543. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1544. tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1545. if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
  1546. secy->operational = true;
  1547. rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
  1548. rtnl_unlock();
  1549. return 0;
  1550. }
  1551. static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
  1552. {
  1553. struct nlattr **attrs = info->attrs;
  1554. struct net_device *dev;
  1555. struct macsec_secy *secy;
  1556. struct macsec_rx_sc *rx_sc;
  1557. struct macsec_rx_sa *rx_sa;
  1558. u8 assoc_num;
  1559. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1560. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1561. if (!attrs[MACSEC_ATTR_IFINDEX])
  1562. return -EINVAL;
  1563. if (parse_sa_config(attrs, tb_sa))
  1564. return -EINVAL;
  1565. if (parse_rxsc_config(attrs, tb_rxsc))
  1566. return -EINVAL;
  1567. rtnl_lock();
  1568. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1569. &dev, &secy, &rx_sc, &assoc_num);
  1570. if (IS_ERR(rx_sa)) {
  1571. rtnl_unlock();
  1572. return PTR_ERR(rx_sa);
  1573. }
  1574. if (rx_sa->active) {
  1575. rtnl_unlock();
  1576. return -EBUSY;
  1577. }
  1578. RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
  1579. clear_rx_sa(rx_sa);
  1580. rtnl_unlock();
  1581. return 0;
  1582. }
  1583. static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
  1584. {
  1585. struct nlattr **attrs = info->attrs;
  1586. struct net_device *dev;
  1587. struct macsec_secy *secy;
  1588. struct macsec_rx_sc *rx_sc;
  1589. sci_t sci;
  1590. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1591. if (!attrs[MACSEC_ATTR_IFINDEX])
  1592. return -EINVAL;
  1593. if (parse_rxsc_config(attrs, tb_rxsc))
  1594. return -EINVAL;
  1595. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1596. return -EINVAL;
  1597. rtnl_lock();
  1598. dev = get_dev_from_nl(genl_info_net(info), info->attrs);
  1599. if (IS_ERR(dev)) {
  1600. rtnl_unlock();
  1601. return PTR_ERR(dev);
  1602. }
  1603. secy = &macsec_priv(dev)->secy;
  1604. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1605. rx_sc = del_rx_sc(secy, sci);
  1606. if (!rx_sc) {
  1607. rtnl_unlock();
  1608. return -ENODEV;
  1609. }
  1610. free_rx_sc(rx_sc);
  1611. rtnl_unlock();
  1612. return 0;
  1613. }
  1614. static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
  1615. {
  1616. struct nlattr **attrs = info->attrs;
  1617. struct net_device *dev;
  1618. struct macsec_secy *secy;
  1619. struct macsec_tx_sc *tx_sc;
  1620. struct macsec_tx_sa *tx_sa;
  1621. u8 assoc_num;
  1622. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1623. if (!attrs[MACSEC_ATTR_IFINDEX])
  1624. return -EINVAL;
  1625. if (parse_sa_config(attrs, tb_sa))
  1626. return -EINVAL;
  1627. rtnl_lock();
  1628. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1629. &dev, &secy, &tx_sc, &assoc_num);
  1630. if (IS_ERR(tx_sa)) {
  1631. rtnl_unlock();
  1632. return PTR_ERR(tx_sa);
  1633. }
  1634. if (tx_sa->active) {
  1635. rtnl_unlock();
  1636. return -EBUSY;
  1637. }
  1638. RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
  1639. clear_tx_sa(tx_sa);
  1640. rtnl_unlock();
  1641. return 0;
  1642. }
  1643. static bool validate_upd_sa(struct nlattr **attrs)
  1644. {
  1645. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1646. attrs[MACSEC_SA_ATTR_KEY] ||
  1647. attrs[MACSEC_SA_ATTR_KEYID])
  1648. return false;
  1649. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1650. return false;
  1651. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1652. return false;
  1653. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1654. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1655. return false;
  1656. }
  1657. return true;
  1658. }
  1659. static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
  1660. {
  1661. struct nlattr **attrs = info->attrs;
  1662. struct net_device *dev;
  1663. struct macsec_secy *secy;
  1664. struct macsec_tx_sc *tx_sc;
  1665. struct macsec_tx_sa *tx_sa;
  1666. u8 assoc_num;
  1667. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1668. if (!attrs[MACSEC_ATTR_IFINDEX])
  1669. return -EINVAL;
  1670. if (parse_sa_config(attrs, tb_sa))
  1671. return -EINVAL;
  1672. if (!validate_upd_sa(tb_sa))
  1673. return -EINVAL;
  1674. rtnl_lock();
  1675. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1676. &dev, &secy, &tx_sc, &assoc_num);
  1677. if (IS_ERR(tx_sa)) {
  1678. rtnl_unlock();
  1679. return PTR_ERR(tx_sa);
  1680. }
  1681. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1682. spin_lock_bh(&tx_sa->lock);
  1683. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1684. spin_unlock_bh(&tx_sa->lock);
  1685. }
  1686. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1687. tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1688. if (assoc_num == tx_sc->encoding_sa)
  1689. secy->operational = tx_sa->active;
  1690. rtnl_unlock();
  1691. return 0;
  1692. }
  1693. static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
  1694. {
  1695. struct nlattr **attrs = info->attrs;
  1696. struct net_device *dev;
  1697. struct macsec_secy *secy;
  1698. struct macsec_rx_sc *rx_sc;
  1699. struct macsec_rx_sa *rx_sa;
  1700. u8 assoc_num;
  1701. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1702. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1703. if (!attrs[MACSEC_ATTR_IFINDEX])
  1704. return -EINVAL;
  1705. if (parse_rxsc_config(attrs, tb_rxsc))
  1706. return -EINVAL;
  1707. if (parse_sa_config(attrs, tb_sa))
  1708. return -EINVAL;
  1709. if (!validate_upd_sa(tb_sa))
  1710. return -EINVAL;
  1711. rtnl_lock();
  1712. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1713. &dev, &secy, &rx_sc, &assoc_num);
  1714. if (IS_ERR(rx_sa)) {
  1715. rtnl_unlock();
  1716. return PTR_ERR(rx_sa);
  1717. }
  1718. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1719. spin_lock_bh(&rx_sa->lock);
  1720. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1721. spin_unlock_bh(&rx_sa->lock);
  1722. }
  1723. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1724. rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1725. rtnl_unlock();
  1726. return 0;
  1727. }
  1728. static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
  1729. {
  1730. struct nlattr **attrs = info->attrs;
  1731. struct net_device *dev;
  1732. struct macsec_secy *secy;
  1733. struct macsec_rx_sc *rx_sc;
  1734. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1735. if (!attrs[MACSEC_ATTR_IFINDEX])
  1736. return -EINVAL;
  1737. if (parse_rxsc_config(attrs, tb_rxsc))
  1738. return -EINVAL;
  1739. if (!validate_add_rxsc(tb_rxsc))
  1740. return -EINVAL;
  1741. rtnl_lock();
  1742. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1743. if (IS_ERR(rx_sc)) {
  1744. rtnl_unlock();
  1745. return PTR_ERR(rx_sc);
  1746. }
  1747. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
  1748. bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1749. if (rx_sc->active != new)
  1750. secy->n_rx_sc += new ? 1 : -1;
  1751. rx_sc->active = new;
  1752. }
  1753. rtnl_unlock();
  1754. return 0;
  1755. }
  1756. static int copy_tx_sa_stats(struct sk_buff *skb,
  1757. struct macsec_tx_sa_stats __percpu *pstats)
  1758. {
  1759. struct macsec_tx_sa_stats sum = {0, };
  1760. int cpu;
  1761. for_each_possible_cpu(cpu) {
  1762. const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1763. sum.OutPktsProtected += stats->OutPktsProtected;
  1764. sum.OutPktsEncrypted += stats->OutPktsEncrypted;
  1765. }
  1766. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
  1767. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
  1768. return -EMSGSIZE;
  1769. return 0;
  1770. }
  1771. static int copy_rx_sa_stats(struct sk_buff *skb,
  1772. struct macsec_rx_sa_stats __percpu *pstats)
  1773. {
  1774. struct macsec_rx_sa_stats sum = {0, };
  1775. int cpu;
  1776. for_each_possible_cpu(cpu) {
  1777. const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1778. sum.InPktsOK += stats->InPktsOK;
  1779. sum.InPktsInvalid += stats->InPktsInvalid;
  1780. sum.InPktsNotValid += stats->InPktsNotValid;
  1781. sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
  1782. sum.InPktsUnusedSA += stats->InPktsUnusedSA;
  1783. }
  1784. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
  1785. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
  1786. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
  1787. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
  1788. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
  1789. return -EMSGSIZE;
  1790. return 0;
  1791. }
  1792. static int copy_rx_sc_stats(struct sk_buff *skb,
  1793. struct pcpu_rx_sc_stats __percpu *pstats)
  1794. {
  1795. struct macsec_rx_sc_stats sum = {0, };
  1796. int cpu;
  1797. for_each_possible_cpu(cpu) {
  1798. const struct pcpu_rx_sc_stats *stats;
  1799. struct macsec_rx_sc_stats tmp;
  1800. unsigned int start;
  1801. stats = per_cpu_ptr(pstats, cpu);
  1802. do {
  1803. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1804. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1805. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1806. sum.InOctetsValidated += tmp.InOctetsValidated;
  1807. sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
  1808. sum.InPktsUnchecked += tmp.InPktsUnchecked;
  1809. sum.InPktsDelayed += tmp.InPktsDelayed;
  1810. sum.InPktsOK += tmp.InPktsOK;
  1811. sum.InPktsInvalid += tmp.InPktsInvalid;
  1812. sum.InPktsLate += tmp.InPktsLate;
  1813. sum.InPktsNotValid += tmp.InPktsNotValid;
  1814. sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
  1815. sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
  1816. }
  1817. if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
  1818. sum.InOctetsValidated,
  1819. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1820. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
  1821. sum.InOctetsDecrypted,
  1822. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1823. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
  1824. sum.InPktsUnchecked,
  1825. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1826. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
  1827. sum.InPktsDelayed,
  1828. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1829. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
  1830. sum.InPktsOK,
  1831. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1832. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
  1833. sum.InPktsInvalid,
  1834. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1835. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
  1836. sum.InPktsLate,
  1837. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1838. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
  1839. sum.InPktsNotValid,
  1840. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1841. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
  1842. sum.InPktsNotUsingSA,
  1843. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1844. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
  1845. sum.InPktsUnusedSA,
  1846. MACSEC_RXSC_STATS_ATTR_PAD))
  1847. return -EMSGSIZE;
  1848. return 0;
  1849. }
  1850. static int copy_tx_sc_stats(struct sk_buff *skb,
  1851. struct pcpu_tx_sc_stats __percpu *pstats)
  1852. {
  1853. struct macsec_tx_sc_stats sum = {0, };
  1854. int cpu;
  1855. for_each_possible_cpu(cpu) {
  1856. const struct pcpu_tx_sc_stats *stats;
  1857. struct macsec_tx_sc_stats tmp;
  1858. unsigned int start;
  1859. stats = per_cpu_ptr(pstats, cpu);
  1860. do {
  1861. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1862. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1863. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1864. sum.OutPktsProtected += tmp.OutPktsProtected;
  1865. sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
  1866. sum.OutOctetsProtected += tmp.OutOctetsProtected;
  1867. sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
  1868. }
  1869. if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
  1870. sum.OutPktsProtected,
  1871. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1872. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
  1873. sum.OutPktsEncrypted,
  1874. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1875. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
  1876. sum.OutOctetsProtected,
  1877. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1878. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
  1879. sum.OutOctetsEncrypted,
  1880. MACSEC_TXSC_STATS_ATTR_PAD))
  1881. return -EMSGSIZE;
  1882. return 0;
  1883. }
  1884. static int copy_secy_stats(struct sk_buff *skb,
  1885. struct pcpu_secy_stats __percpu *pstats)
  1886. {
  1887. struct macsec_dev_stats sum = {0, };
  1888. int cpu;
  1889. for_each_possible_cpu(cpu) {
  1890. const struct pcpu_secy_stats *stats;
  1891. struct macsec_dev_stats tmp;
  1892. unsigned int start;
  1893. stats = per_cpu_ptr(pstats, cpu);
  1894. do {
  1895. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1896. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1897. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1898. sum.OutPktsUntagged += tmp.OutPktsUntagged;
  1899. sum.InPktsUntagged += tmp.InPktsUntagged;
  1900. sum.OutPktsTooLong += tmp.OutPktsTooLong;
  1901. sum.InPktsNoTag += tmp.InPktsNoTag;
  1902. sum.InPktsBadTag += tmp.InPktsBadTag;
  1903. sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
  1904. sum.InPktsNoSCI += tmp.InPktsNoSCI;
  1905. sum.InPktsOverrun += tmp.InPktsOverrun;
  1906. }
  1907. if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
  1908. sum.OutPktsUntagged,
  1909. MACSEC_SECY_STATS_ATTR_PAD) ||
  1910. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
  1911. sum.InPktsUntagged,
  1912. MACSEC_SECY_STATS_ATTR_PAD) ||
  1913. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
  1914. sum.OutPktsTooLong,
  1915. MACSEC_SECY_STATS_ATTR_PAD) ||
  1916. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
  1917. sum.InPktsNoTag,
  1918. MACSEC_SECY_STATS_ATTR_PAD) ||
  1919. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
  1920. sum.InPktsBadTag,
  1921. MACSEC_SECY_STATS_ATTR_PAD) ||
  1922. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
  1923. sum.InPktsUnknownSCI,
  1924. MACSEC_SECY_STATS_ATTR_PAD) ||
  1925. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
  1926. sum.InPktsNoSCI,
  1927. MACSEC_SECY_STATS_ATTR_PAD) ||
  1928. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
  1929. sum.InPktsOverrun,
  1930. MACSEC_SECY_STATS_ATTR_PAD))
  1931. return -EMSGSIZE;
  1932. return 0;
  1933. }
  1934. static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
  1935. {
  1936. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1937. struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
  1938. u64 csid;
  1939. if (!secy_nest)
  1940. return 1;
  1941. switch (secy->key_len) {
  1942. case MACSEC_GCM_AES_128_SAK_LEN:
  1943. csid = MACSEC_DEFAULT_CIPHER_ID;
  1944. break;
  1945. case MACSEC_GCM_AES_256_SAK_LEN:
  1946. csid = MACSEC_CIPHER_ID_GCM_AES_256;
  1947. break;
  1948. default:
  1949. goto cancel;
  1950. }
  1951. if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
  1952. MACSEC_SECY_ATTR_PAD) ||
  1953. nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
  1954. csid, MACSEC_SECY_ATTR_PAD) ||
  1955. nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
  1956. nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
  1957. nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
  1958. nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
  1959. nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
  1960. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
  1961. nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
  1962. nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
  1963. nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
  1964. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
  1965. goto cancel;
  1966. if (secy->replay_protect) {
  1967. if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
  1968. goto cancel;
  1969. }
  1970. nla_nest_end(skb, secy_nest);
  1971. return 0;
  1972. cancel:
  1973. nla_nest_cancel(skb, secy_nest);
  1974. return 1;
  1975. }
  1976. static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
  1977. struct sk_buff *skb, struct netlink_callback *cb)
  1978. {
  1979. struct macsec_rx_sc *rx_sc;
  1980. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1981. struct nlattr *txsa_list, *rxsc_list;
  1982. int i, j;
  1983. void *hdr;
  1984. struct nlattr *attr;
  1985. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1986. &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
  1987. if (!hdr)
  1988. return -EMSGSIZE;
  1989. genl_dump_check_consistent(cb, hdr);
  1990. if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
  1991. goto nla_put_failure;
  1992. if (nla_put_secy(secy, skb))
  1993. goto nla_put_failure;
  1994. attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
  1995. if (!attr)
  1996. goto nla_put_failure;
  1997. if (copy_tx_sc_stats(skb, tx_sc->stats)) {
  1998. nla_nest_cancel(skb, attr);
  1999. goto nla_put_failure;
  2000. }
  2001. nla_nest_end(skb, attr);
  2002. attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
  2003. if (!attr)
  2004. goto nla_put_failure;
  2005. if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
  2006. nla_nest_cancel(skb, attr);
  2007. goto nla_put_failure;
  2008. }
  2009. nla_nest_end(skb, attr);
  2010. txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
  2011. if (!txsa_list)
  2012. goto nla_put_failure;
  2013. for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
  2014. struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
  2015. struct nlattr *txsa_nest;
  2016. if (!tx_sa)
  2017. continue;
  2018. txsa_nest = nla_nest_start(skb, j++);
  2019. if (!txsa_nest) {
  2020. nla_nest_cancel(skb, txsa_list);
  2021. goto nla_put_failure;
  2022. }
  2023. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2024. nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
  2025. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
  2026. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
  2027. nla_nest_cancel(skb, txsa_nest);
  2028. nla_nest_cancel(skb, txsa_list);
  2029. goto nla_put_failure;
  2030. }
  2031. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  2032. if (!attr) {
  2033. nla_nest_cancel(skb, txsa_nest);
  2034. nla_nest_cancel(skb, txsa_list);
  2035. goto nla_put_failure;
  2036. }
  2037. if (copy_tx_sa_stats(skb, tx_sa->stats)) {
  2038. nla_nest_cancel(skb, attr);
  2039. nla_nest_cancel(skb, txsa_nest);
  2040. nla_nest_cancel(skb, txsa_list);
  2041. goto nla_put_failure;
  2042. }
  2043. nla_nest_end(skb, attr);
  2044. nla_nest_end(skb, txsa_nest);
  2045. }
  2046. nla_nest_end(skb, txsa_list);
  2047. rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
  2048. if (!rxsc_list)
  2049. goto nla_put_failure;
  2050. j = 1;
  2051. for_each_rxsc_rtnl(secy, rx_sc) {
  2052. int k;
  2053. struct nlattr *rxsa_list;
  2054. struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
  2055. if (!rxsc_nest) {
  2056. nla_nest_cancel(skb, rxsc_list);
  2057. goto nla_put_failure;
  2058. }
  2059. if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
  2060. nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
  2061. MACSEC_RXSC_ATTR_PAD)) {
  2062. nla_nest_cancel(skb, rxsc_nest);
  2063. nla_nest_cancel(skb, rxsc_list);
  2064. goto nla_put_failure;
  2065. }
  2066. attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
  2067. if (!attr) {
  2068. nla_nest_cancel(skb, rxsc_nest);
  2069. nla_nest_cancel(skb, rxsc_list);
  2070. goto nla_put_failure;
  2071. }
  2072. if (copy_rx_sc_stats(skb, rx_sc->stats)) {
  2073. nla_nest_cancel(skb, attr);
  2074. nla_nest_cancel(skb, rxsc_nest);
  2075. nla_nest_cancel(skb, rxsc_list);
  2076. goto nla_put_failure;
  2077. }
  2078. nla_nest_end(skb, attr);
  2079. rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
  2080. if (!rxsa_list) {
  2081. nla_nest_cancel(skb, rxsc_nest);
  2082. nla_nest_cancel(skb, rxsc_list);
  2083. goto nla_put_failure;
  2084. }
  2085. for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
  2086. struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
  2087. struct nlattr *rxsa_nest;
  2088. if (!rx_sa)
  2089. continue;
  2090. rxsa_nest = nla_nest_start(skb, k++);
  2091. if (!rxsa_nest) {
  2092. nla_nest_cancel(skb, rxsa_list);
  2093. nla_nest_cancel(skb, rxsc_nest);
  2094. nla_nest_cancel(skb, rxsc_list);
  2095. goto nla_put_failure;
  2096. }
  2097. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  2098. if (!attr) {
  2099. nla_nest_cancel(skb, rxsa_list);
  2100. nla_nest_cancel(skb, rxsc_nest);
  2101. nla_nest_cancel(skb, rxsc_list);
  2102. goto nla_put_failure;
  2103. }
  2104. if (copy_rx_sa_stats(skb, rx_sa->stats)) {
  2105. nla_nest_cancel(skb, attr);
  2106. nla_nest_cancel(skb, rxsa_list);
  2107. nla_nest_cancel(skb, rxsc_nest);
  2108. nla_nest_cancel(skb, rxsc_list);
  2109. goto nla_put_failure;
  2110. }
  2111. nla_nest_end(skb, attr);
  2112. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2113. nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
  2114. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
  2115. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
  2116. nla_nest_cancel(skb, rxsa_nest);
  2117. nla_nest_cancel(skb, rxsc_nest);
  2118. nla_nest_cancel(skb, rxsc_list);
  2119. goto nla_put_failure;
  2120. }
  2121. nla_nest_end(skb, rxsa_nest);
  2122. }
  2123. nla_nest_end(skb, rxsa_list);
  2124. nla_nest_end(skb, rxsc_nest);
  2125. }
  2126. nla_nest_end(skb, rxsc_list);
  2127. genlmsg_end(skb, hdr);
  2128. return 0;
  2129. nla_put_failure:
  2130. genlmsg_cancel(skb, hdr);
  2131. return -EMSGSIZE;
  2132. }
  2133. static int macsec_generation = 1; /* protected by RTNL */
  2134. static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
  2135. {
  2136. struct net *net = sock_net(skb->sk);
  2137. struct net_device *dev;
  2138. int dev_idx, d;
  2139. dev_idx = cb->args[0];
  2140. d = 0;
  2141. rtnl_lock();
  2142. cb->seq = macsec_generation;
  2143. for_each_netdev(net, dev) {
  2144. struct macsec_secy *secy;
  2145. if (d < dev_idx)
  2146. goto next;
  2147. if (!netif_is_macsec(dev))
  2148. goto next;
  2149. secy = &macsec_priv(dev)->secy;
  2150. if (dump_secy(secy, dev, skb, cb) < 0)
  2151. goto done;
  2152. next:
  2153. d++;
  2154. }
  2155. done:
  2156. rtnl_unlock();
  2157. cb->args[0] = d;
  2158. return skb->len;
  2159. }
  2160. static const struct genl_ops macsec_genl_ops[] = {
  2161. {
  2162. .cmd = MACSEC_CMD_GET_TXSC,
  2163. .dumpit = macsec_dump_txsc,
  2164. .policy = macsec_genl_policy,
  2165. },
  2166. {
  2167. .cmd = MACSEC_CMD_ADD_RXSC,
  2168. .doit = macsec_add_rxsc,
  2169. .policy = macsec_genl_policy,
  2170. .flags = GENL_ADMIN_PERM,
  2171. },
  2172. {
  2173. .cmd = MACSEC_CMD_DEL_RXSC,
  2174. .doit = macsec_del_rxsc,
  2175. .policy = macsec_genl_policy,
  2176. .flags = GENL_ADMIN_PERM,
  2177. },
  2178. {
  2179. .cmd = MACSEC_CMD_UPD_RXSC,
  2180. .doit = macsec_upd_rxsc,
  2181. .policy = macsec_genl_policy,
  2182. .flags = GENL_ADMIN_PERM,
  2183. },
  2184. {
  2185. .cmd = MACSEC_CMD_ADD_TXSA,
  2186. .doit = macsec_add_txsa,
  2187. .policy = macsec_genl_policy,
  2188. .flags = GENL_ADMIN_PERM,
  2189. },
  2190. {
  2191. .cmd = MACSEC_CMD_DEL_TXSA,
  2192. .doit = macsec_del_txsa,
  2193. .policy = macsec_genl_policy,
  2194. .flags = GENL_ADMIN_PERM,
  2195. },
  2196. {
  2197. .cmd = MACSEC_CMD_UPD_TXSA,
  2198. .doit = macsec_upd_txsa,
  2199. .policy = macsec_genl_policy,
  2200. .flags = GENL_ADMIN_PERM,
  2201. },
  2202. {
  2203. .cmd = MACSEC_CMD_ADD_RXSA,
  2204. .doit = macsec_add_rxsa,
  2205. .policy = macsec_genl_policy,
  2206. .flags = GENL_ADMIN_PERM,
  2207. },
  2208. {
  2209. .cmd = MACSEC_CMD_DEL_RXSA,
  2210. .doit = macsec_del_rxsa,
  2211. .policy = macsec_genl_policy,
  2212. .flags = GENL_ADMIN_PERM,
  2213. },
  2214. {
  2215. .cmd = MACSEC_CMD_UPD_RXSA,
  2216. .doit = macsec_upd_rxsa,
  2217. .policy = macsec_genl_policy,
  2218. .flags = GENL_ADMIN_PERM,
  2219. },
  2220. };
  2221. static struct genl_family macsec_fam __ro_after_init = {
  2222. .name = MACSEC_GENL_NAME,
  2223. .hdrsize = 0,
  2224. .version = MACSEC_GENL_VERSION,
  2225. .maxattr = MACSEC_ATTR_MAX,
  2226. .netnsok = true,
  2227. .module = THIS_MODULE,
  2228. .ops = macsec_genl_ops,
  2229. .n_ops = ARRAY_SIZE(macsec_genl_ops),
  2230. };
  2231. static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
  2232. struct net_device *dev)
  2233. {
  2234. struct macsec_dev *macsec = netdev_priv(dev);
  2235. struct macsec_secy *secy = &macsec->secy;
  2236. struct pcpu_secy_stats *secy_stats;
  2237. int ret, len;
  2238. /* 10.5 */
  2239. if (!secy->protect_frames) {
  2240. secy_stats = this_cpu_ptr(macsec->stats);
  2241. u64_stats_update_begin(&secy_stats->syncp);
  2242. secy_stats->stats.OutPktsUntagged++;
  2243. u64_stats_update_end(&secy_stats->syncp);
  2244. skb->dev = macsec->real_dev;
  2245. len = skb->len;
  2246. ret = dev_queue_xmit(skb);
  2247. count_tx(dev, ret, len);
  2248. return ret;
  2249. }
  2250. if (!secy->operational) {
  2251. kfree_skb(skb);
  2252. dev->stats.tx_dropped++;
  2253. return NETDEV_TX_OK;
  2254. }
  2255. skb = macsec_encrypt(skb, dev);
  2256. if (IS_ERR(skb)) {
  2257. if (PTR_ERR(skb) != -EINPROGRESS)
  2258. dev->stats.tx_dropped++;
  2259. return NETDEV_TX_OK;
  2260. }
  2261. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  2262. macsec_encrypt_finish(skb, dev);
  2263. len = skb->len;
  2264. ret = dev_queue_xmit(skb);
  2265. count_tx(dev, ret, len);
  2266. return ret;
  2267. }
  2268. #define MACSEC_FEATURES \
  2269. (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
  2270. static struct lock_class_key macsec_netdev_addr_lock_key;
  2271. static int macsec_dev_init(struct net_device *dev)
  2272. {
  2273. struct macsec_dev *macsec = macsec_priv(dev);
  2274. struct net_device *real_dev = macsec->real_dev;
  2275. int err;
  2276. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  2277. if (!dev->tstats)
  2278. return -ENOMEM;
  2279. err = gro_cells_init(&macsec->gro_cells, dev);
  2280. if (err) {
  2281. free_percpu(dev->tstats);
  2282. return err;
  2283. }
  2284. dev->features = real_dev->features & MACSEC_FEATURES;
  2285. dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
  2286. dev->needed_headroom = real_dev->needed_headroom +
  2287. MACSEC_NEEDED_HEADROOM;
  2288. dev->needed_tailroom = real_dev->needed_tailroom +
  2289. MACSEC_NEEDED_TAILROOM;
  2290. if (is_zero_ether_addr(dev->dev_addr))
  2291. eth_hw_addr_inherit(dev, real_dev);
  2292. if (is_zero_ether_addr(dev->broadcast))
  2293. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  2294. return 0;
  2295. }
  2296. static void macsec_dev_uninit(struct net_device *dev)
  2297. {
  2298. struct macsec_dev *macsec = macsec_priv(dev);
  2299. gro_cells_destroy(&macsec->gro_cells);
  2300. free_percpu(dev->tstats);
  2301. }
  2302. static netdev_features_t macsec_fix_features(struct net_device *dev,
  2303. netdev_features_t features)
  2304. {
  2305. struct macsec_dev *macsec = macsec_priv(dev);
  2306. struct net_device *real_dev = macsec->real_dev;
  2307. features &= (real_dev->features & MACSEC_FEATURES) |
  2308. NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
  2309. features |= NETIF_F_LLTX;
  2310. return features;
  2311. }
  2312. static int macsec_dev_open(struct net_device *dev)
  2313. {
  2314. struct macsec_dev *macsec = macsec_priv(dev);
  2315. struct net_device *real_dev = macsec->real_dev;
  2316. int err;
  2317. err = dev_uc_add(real_dev, dev->dev_addr);
  2318. if (err < 0)
  2319. return err;
  2320. if (dev->flags & IFF_ALLMULTI) {
  2321. err = dev_set_allmulti(real_dev, 1);
  2322. if (err < 0)
  2323. goto del_unicast;
  2324. }
  2325. if (dev->flags & IFF_PROMISC) {
  2326. err = dev_set_promiscuity(real_dev, 1);
  2327. if (err < 0)
  2328. goto clear_allmulti;
  2329. }
  2330. if (netif_carrier_ok(real_dev))
  2331. netif_carrier_on(dev);
  2332. return 0;
  2333. clear_allmulti:
  2334. if (dev->flags & IFF_ALLMULTI)
  2335. dev_set_allmulti(real_dev, -1);
  2336. del_unicast:
  2337. dev_uc_del(real_dev, dev->dev_addr);
  2338. netif_carrier_off(dev);
  2339. return err;
  2340. }
  2341. static int macsec_dev_stop(struct net_device *dev)
  2342. {
  2343. struct macsec_dev *macsec = macsec_priv(dev);
  2344. struct net_device *real_dev = macsec->real_dev;
  2345. netif_carrier_off(dev);
  2346. dev_mc_unsync(real_dev, dev);
  2347. dev_uc_unsync(real_dev, dev);
  2348. if (dev->flags & IFF_ALLMULTI)
  2349. dev_set_allmulti(real_dev, -1);
  2350. if (dev->flags & IFF_PROMISC)
  2351. dev_set_promiscuity(real_dev, -1);
  2352. dev_uc_del(real_dev, dev->dev_addr);
  2353. return 0;
  2354. }
  2355. static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
  2356. {
  2357. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2358. if (!(dev->flags & IFF_UP))
  2359. return;
  2360. if (change & IFF_ALLMULTI)
  2361. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  2362. if (change & IFF_PROMISC)
  2363. dev_set_promiscuity(real_dev,
  2364. dev->flags & IFF_PROMISC ? 1 : -1);
  2365. }
  2366. static void macsec_dev_set_rx_mode(struct net_device *dev)
  2367. {
  2368. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2369. dev_mc_sync(real_dev, dev);
  2370. dev_uc_sync(real_dev, dev);
  2371. }
  2372. static sci_t dev_to_sci(struct net_device *dev, __be16 port)
  2373. {
  2374. return make_sci(dev->dev_addr, port);
  2375. }
  2376. static int macsec_set_mac_address(struct net_device *dev, void *p)
  2377. {
  2378. struct macsec_dev *macsec = macsec_priv(dev);
  2379. struct net_device *real_dev = macsec->real_dev;
  2380. struct sockaddr *addr = p;
  2381. int err;
  2382. if (!is_valid_ether_addr(addr->sa_data))
  2383. return -EADDRNOTAVAIL;
  2384. if (!(dev->flags & IFF_UP))
  2385. goto out;
  2386. err = dev_uc_add(real_dev, addr->sa_data);
  2387. if (err < 0)
  2388. return err;
  2389. dev_uc_del(real_dev, dev->dev_addr);
  2390. out:
  2391. ether_addr_copy(dev->dev_addr, addr->sa_data);
  2392. macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2393. return 0;
  2394. }
  2395. static int macsec_change_mtu(struct net_device *dev, int new_mtu)
  2396. {
  2397. struct macsec_dev *macsec = macsec_priv(dev);
  2398. unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
  2399. if (macsec->real_dev->mtu - extra < new_mtu)
  2400. return -ERANGE;
  2401. dev->mtu = new_mtu;
  2402. return 0;
  2403. }
  2404. static void macsec_get_stats64(struct net_device *dev,
  2405. struct rtnl_link_stats64 *s)
  2406. {
  2407. int cpu;
  2408. if (!dev->tstats)
  2409. return;
  2410. for_each_possible_cpu(cpu) {
  2411. struct pcpu_sw_netstats *stats;
  2412. struct pcpu_sw_netstats tmp;
  2413. int start;
  2414. stats = per_cpu_ptr(dev->tstats, cpu);
  2415. do {
  2416. start = u64_stats_fetch_begin_irq(&stats->syncp);
  2417. tmp.rx_packets = stats->rx_packets;
  2418. tmp.rx_bytes = stats->rx_bytes;
  2419. tmp.tx_packets = stats->tx_packets;
  2420. tmp.tx_bytes = stats->tx_bytes;
  2421. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  2422. s->rx_packets += tmp.rx_packets;
  2423. s->rx_bytes += tmp.rx_bytes;
  2424. s->tx_packets += tmp.tx_packets;
  2425. s->tx_bytes += tmp.tx_bytes;
  2426. }
  2427. s->rx_dropped = dev->stats.rx_dropped;
  2428. s->tx_dropped = dev->stats.tx_dropped;
  2429. }
  2430. static int macsec_get_iflink(const struct net_device *dev)
  2431. {
  2432. return macsec_priv(dev)->real_dev->ifindex;
  2433. }
  2434. static int macsec_get_nest_level(struct net_device *dev)
  2435. {
  2436. return macsec_priv(dev)->nest_level;
  2437. }
  2438. static const struct net_device_ops macsec_netdev_ops = {
  2439. .ndo_init = macsec_dev_init,
  2440. .ndo_uninit = macsec_dev_uninit,
  2441. .ndo_open = macsec_dev_open,
  2442. .ndo_stop = macsec_dev_stop,
  2443. .ndo_fix_features = macsec_fix_features,
  2444. .ndo_change_mtu = macsec_change_mtu,
  2445. .ndo_set_rx_mode = macsec_dev_set_rx_mode,
  2446. .ndo_change_rx_flags = macsec_dev_change_rx_flags,
  2447. .ndo_set_mac_address = macsec_set_mac_address,
  2448. .ndo_start_xmit = macsec_start_xmit,
  2449. .ndo_get_stats64 = macsec_get_stats64,
  2450. .ndo_get_iflink = macsec_get_iflink,
  2451. .ndo_get_lock_subclass = macsec_get_nest_level,
  2452. };
  2453. static const struct device_type macsec_type = {
  2454. .name = "macsec",
  2455. };
  2456. static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
  2457. [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
  2458. [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
  2459. [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
  2460. [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
  2461. [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
  2462. [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
  2463. [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
  2464. [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
  2465. [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
  2466. [IFLA_MACSEC_ES] = { .type = NLA_U8 },
  2467. [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
  2468. [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
  2469. [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
  2470. };
  2471. static void macsec_free_netdev(struct net_device *dev)
  2472. {
  2473. struct macsec_dev *macsec = macsec_priv(dev);
  2474. free_percpu(macsec->stats);
  2475. free_percpu(macsec->secy.tx_sc.stats);
  2476. }
  2477. static void macsec_setup(struct net_device *dev)
  2478. {
  2479. ether_setup(dev);
  2480. dev->min_mtu = 0;
  2481. dev->max_mtu = ETH_MAX_MTU;
  2482. dev->priv_flags |= IFF_NO_QUEUE;
  2483. dev->netdev_ops = &macsec_netdev_ops;
  2484. dev->needs_free_netdev = true;
  2485. dev->priv_destructor = macsec_free_netdev;
  2486. SET_NETDEV_DEVTYPE(dev, &macsec_type);
  2487. eth_zero_addr(dev->broadcast);
  2488. }
  2489. static int macsec_changelink_common(struct net_device *dev,
  2490. struct nlattr *data[])
  2491. {
  2492. struct macsec_secy *secy;
  2493. struct macsec_tx_sc *tx_sc;
  2494. secy = &macsec_priv(dev)->secy;
  2495. tx_sc = &secy->tx_sc;
  2496. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2497. struct macsec_tx_sa *tx_sa;
  2498. tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
  2499. tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
  2500. secy->operational = tx_sa && tx_sa->active;
  2501. }
  2502. if (data[IFLA_MACSEC_WINDOW])
  2503. secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
  2504. if (data[IFLA_MACSEC_ENCRYPT])
  2505. tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
  2506. if (data[IFLA_MACSEC_PROTECT])
  2507. secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
  2508. if (data[IFLA_MACSEC_INC_SCI])
  2509. tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
  2510. if (data[IFLA_MACSEC_ES])
  2511. tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
  2512. if (data[IFLA_MACSEC_SCB])
  2513. tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
  2514. if (data[IFLA_MACSEC_REPLAY_PROTECT])
  2515. secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
  2516. if (data[IFLA_MACSEC_VALIDATION])
  2517. secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
  2518. if (data[IFLA_MACSEC_CIPHER_SUITE]) {
  2519. switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
  2520. case MACSEC_CIPHER_ID_GCM_AES_128:
  2521. case MACSEC_DEFAULT_CIPHER_ID:
  2522. secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
  2523. break;
  2524. case MACSEC_CIPHER_ID_GCM_AES_256:
  2525. secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
  2526. break;
  2527. default:
  2528. return -EINVAL;
  2529. }
  2530. }
  2531. return 0;
  2532. }
  2533. static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
  2534. struct nlattr *data[],
  2535. struct netlink_ext_ack *extack)
  2536. {
  2537. if (!data)
  2538. return 0;
  2539. if (data[IFLA_MACSEC_CIPHER_SUITE] ||
  2540. data[IFLA_MACSEC_ICV_LEN] ||
  2541. data[IFLA_MACSEC_SCI] ||
  2542. data[IFLA_MACSEC_PORT])
  2543. return -EINVAL;
  2544. return macsec_changelink_common(dev, data);
  2545. }
  2546. static void macsec_del_dev(struct macsec_dev *macsec)
  2547. {
  2548. int i;
  2549. while (macsec->secy.rx_sc) {
  2550. struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
  2551. rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
  2552. free_rx_sc(rx_sc);
  2553. }
  2554. for (i = 0; i < MACSEC_NUM_AN; i++) {
  2555. struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
  2556. if (sa) {
  2557. RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
  2558. clear_tx_sa(sa);
  2559. }
  2560. }
  2561. }
  2562. static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
  2563. {
  2564. struct macsec_dev *macsec = macsec_priv(dev);
  2565. struct net_device *real_dev = macsec->real_dev;
  2566. unregister_netdevice_queue(dev, head);
  2567. list_del_rcu(&macsec->secys);
  2568. macsec_del_dev(macsec);
  2569. netdev_upper_dev_unlink(real_dev, dev);
  2570. macsec_generation++;
  2571. }
  2572. static void macsec_dellink(struct net_device *dev, struct list_head *head)
  2573. {
  2574. struct macsec_dev *macsec = macsec_priv(dev);
  2575. struct net_device *real_dev = macsec->real_dev;
  2576. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2577. macsec_common_dellink(dev, head);
  2578. if (list_empty(&rxd->secys)) {
  2579. netdev_rx_handler_unregister(real_dev);
  2580. kfree(rxd);
  2581. }
  2582. }
  2583. static int register_macsec_dev(struct net_device *real_dev,
  2584. struct net_device *dev)
  2585. {
  2586. struct macsec_dev *macsec = macsec_priv(dev);
  2587. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2588. if (!rxd) {
  2589. int err;
  2590. rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
  2591. if (!rxd)
  2592. return -ENOMEM;
  2593. INIT_LIST_HEAD(&rxd->secys);
  2594. err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
  2595. rxd);
  2596. if (err < 0) {
  2597. kfree(rxd);
  2598. return err;
  2599. }
  2600. }
  2601. list_add_tail_rcu(&macsec->secys, &rxd->secys);
  2602. return 0;
  2603. }
  2604. static bool sci_exists(struct net_device *dev, sci_t sci)
  2605. {
  2606. struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
  2607. struct macsec_dev *macsec;
  2608. list_for_each_entry(macsec, &rxd->secys, secys) {
  2609. if (macsec->secy.sci == sci)
  2610. return true;
  2611. }
  2612. return false;
  2613. }
  2614. static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
  2615. {
  2616. struct macsec_dev *macsec = macsec_priv(dev);
  2617. struct macsec_secy *secy = &macsec->secy;
  2618. macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
  2619. if (!macsec->stats)
  2620. return -ENOMEM;
  2621. secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
  2622. if (!secy->tx_sc.stats) {
  2623. free_percpu(macsec->stats);
  2624. return -ENOMEM;
  2625. }
  2626. if (sci == MACSEC_UNDEF_SCI)
  2627. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2628. secy->netdev = dev;
  2629. secy->operational = true;
  2630. secy->key_len = DEFAULT_SAK_LEN;
  2631. secy->icv_len = icv_len;
  2632. secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
  2633. secy->protect_frames = true;
  2634. secy->replay_protect = false;
  2635. secy->sci = sci;
  2636. secy->tx_sc.active = true;
  2637. secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
  2638. secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
  2639. secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
  2640. secy->tx_sc.end_station = false;
  2641. secy->tx_sc.scb = false;
  2642. return 0;
  2643. }
  2644. static int macsec_newlink(struct net *net, struct net_device *dev,
  2645. struct nlattr *tb[], struct nlattr *data[],
  2646. struct netlink_ext_ack *extack)
  2647. {
  2648. struct macsec_dev *macsec = macsec_priv(dev);
  2649. struct net_device *real_dev;
  2650. int err;
  2651. sci_t sci;
  2652. u8 icv_len = DEFAULT_ICV_LEN;
  2653. rx_handler_func_t *rx_handler;
  2654. if (!tb[IFLA_LINK])
  2655. return -EINVAL;
  2656. real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
  2657. if (!real_dev)
  2658. return -ENODEV;
  2659. if (real_dev->type != ARPHRD_ETHER)
  2660. return -EINVAL;
  2661. dev->priv_flags |= IFF_MACSEC;
  2662. macsec->real_dev = real_dev;
  2663. if (data && data[IFLA_MACSEC_ICV_LEN])
  2664. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2665. dev->mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
  2666. rx_handler = rtnl_dereference(real_dev->rx_handler);
  2667. if (rx_handler && rx_handler != macsec_handle_frame)
  2668. return -EBUSY;
  2669. err = register_netdevice(dev);
  2670. if (err < 0)
  2671. return err;
  2672. macsec->nest_level = dev_get_nest_level(real_dev) + 1;
  2673. netdev_lockdep_set_classes(dev);
  2674. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  2675. &macsec_netdev_addr_lock_key,
  2676. macsec_get_nest_level(dev));
  2677. err = netdev_upper_dev_link(real_dev, dev, extack);
  2678. if (err < 0)
  2679. goto unregister;
  2680. /* need to be already registered so that ->init has run and
  2681. * the MAC addr is set
  2682. */
  2683. if (data && data[IFLA_MACSEC_SCI])
  2684. sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
  2685. else if (data && data[IFLA_MACSEC_PORT])
  2686. sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
  2687. else
  2688. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2689. if (rx_handler && sci_exists(real_dev, sci)) {
  2690. err = -EBUSY;
  2691. goto unlink;
  2692. }
  2693. err = macsec_add_dev(dev, sci, icv_len);
  2694. if (err)
  2695. goto unlink;
  2696. if (data) {
  2697. err = macsec_changelink_common(dev, data);
  2698. if (err)
  2699. goto del_dev;
  2700. }
  2701. err = register_macsec_dev(real_dev, dev);
  2702. if (err < 0)
  2703. goto del_dev;
  2704. netif_stacked_transfer_operstate(real_dev, dev);
  2705. linkwatch_fire_event(dev);
  2706. macsec_generation++;
  2707. return 0;
  2708. del_dev:
  2709. macsec_del_dev(macsec);
  2710. unlink:
  2711. netdev_upper_dev_unlink(real_dev, dev);
  2712. unregister:
  2713. unregister_netdevice(dev);
  2714. return err;
  2715. }
  2716. static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
  2717. struct netlink_ext_ack *extack)
  2718. {
  2719. u64 csid = MACSEC_DEFAULT_CIPHER_ID;
  2720. u8 icv_len = DEFAULT_ICV_LEN;
  2721. int flag;
  2722. bool es, scb, sci;
  2723. if (!data)
  2724. return 0;
  2725. if (data[IFLA_MACSEC_CIPHER_SUITE])
  2726. csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
  2727. if (data[IFLA_MACSEC_ICV_LEN]) {
  2728. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2729. if (icv_len != DEFAULT_ICV_LEN) {
  2730. char dummy_key[DEFAULT_SAK_LEN] = { 0 };
  2731. struct crypto_aead *dummy_tfm;
  2732. dummy_tfm = macsec_alloc_tfm(dummy_key,
  2733. DEFAULT_SAK_LEN,
  2734. icv_len);
  2735. if (IS_ERR(dummy_tfm))
  2736. return PTR_ERR(dummy_tfm);
  2737. crypto_free_aead(dummy_tfm);
  2738. }
  2739. }
  2740. switch (csid) {
  2741. case MACSEC_CIPHER_ID_GCM_AES_128:
  2742. case MACSEC_CIPHER_ID_GCM_AES_256:
  2743. case MACSEC_DEFAULT_CIPHER_ID:
  2744. if (icv_len < MACSEC_MIN_ICV_LEN ||
  2745. icv_len > MACSEC_STD_ICV_LEN)
  2746. return -EINVAL;
  2747. break;
  2748. default:
  2749. return -EINVAL;
  2750. }
  2751. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2752. if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
  2753. return -EINVAL;
  2754. }
  2755. for (flag = IFLA_MACSEC_ENCODING_SA + 1;
  2756. flag < IFLA_MACSEC_VALIDATION;
  2757. flag++) {
  2758. if (data[flag]) {
  2759. if (nla_get_u8(data[flag]) > 1)
  2760. return -EINVAL;
  2761. }
  2762. }
  2763. es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
  2764. sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
  2765. scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
  2766. if ((sci && (scb || es)) || (scb && es))
  2767. return -EINVAL;
  2768. if (data[IFLA_MACSEC_VALIDATION] &&
  2769. nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
  2770. return -EINVAL;
  2771. if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
  2772. nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
  2773. !data[IFLA_MACSEC_WINDOW])
  2774. return -EINVAL;
  2775. return 0;
  2776. }
  2777. static struct net *macsec_get_link_net(const struct net_device *dev)
  2778. {
  2779. return dev_net(macsec_priv(dev)->real_dev);
  2780. }
  2781. static size_t macsec_get_size(const struct net_device *dev)
  2782. {
  2783. return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
  2784. nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
  2785. nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
  2786. nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
  2787. nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
  2788. nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
  2789. nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
  2790. nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
  2791. nla_total_size(1) + /* IFLA_MACSEC_ES */
  2792. nla_total_size(1) + /* IFLA_MACSEC_SCB */
  2793. nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
  2794. nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
  2795. 0;
  2796. }
  2797. static int macsec_fill_info(struct sk_buff *skb,
  2798. const struct net_device *dev)
  2799. {
  2800. struct macsec_secy *secy = &macsec_priv(dev)->secy;
  2801. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  2802. u64 csid;
  2803. switch (secy->key_len) {
  2804. case MACSEC_GCM_AES_128_SAK_LEN:
  2805. csid = MACSEC_DEFAULT_CIPHER_ID;
  2806. break;
  2807. case MACSEC_GCM_AES_256_SAK_LEN:
  2808. csid = MACSEC_CIPHER_ID_GCM_AES_256;
  2809. break;
  2810. default:
  2811. goto nla_put_failure;
  2812. }
  2813. if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
  2814. IFLA_MACSEC_PAD) ||
  2815. nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
  2816. nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
  2817. csid, IFLA_MACSEC_PAD) ||
  2818. nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
  2819. nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
  2820. nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
  2821. nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
  2822. nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
  2823. nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
  2824. nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
  2825. nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
  2826. 0)
  2827. goto nla_put_failure;
  2828. if (secy->replay_protect) {
  2829. if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
  2830. goto nla_put_failure;
  2831. }
  2832. return 0;
  2833. nla_put_failure:
  2834. return -EMSGSIZE;
  2835. }
  2836. static struct rtnl_link_ops macsec_link_ops __read_mostly = {
  2837. .kind = "macsec",
  2838. .priv_size = sizeof(struct macsec_dev),
  2839. .maxtype = IFLA_MACSEC_MAX,
  2840. .policy = macsec_rtnl_policy,
  2841. .setup = macsec_setup,
  2842. .validate = macsec_validate_attr,
  2843. .newlink = macsec_newlink,
  2844. .changelink = macsec_changelink,
  2845. .dellink = macsec_dellink,
  2846. .get_size = macsec_get_size,
  2847. .fill_info = macsec_fill_info,
  2848. .get_link_net = macsec_get_link_net,
  2849. };
  2850. static bool is_macsec_master(struct net_device *dev)
  2851. {
  2852. return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
  2853. }
  2854. static int macsec_notify(struct notifier_block *this, unsigned long event,
  2855. void *ptr)
  2856. {
  2857. struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
  2858. LIST_HEAD(head);
  2859. if (!is_macsec_master(real_dev))
  2860. return NOTIFY_DONE;
  2861. switch (event) {
  2862. case NETDEV_DOWN:
  2863. case NETDEV_UP:
  2864. case NETDEV_CHANGE: {
  2865. struct macsec_dev *m, *n;
  2866. struct macsec_rxh_data *rxd;
  2867. rxd = macsec_data_rtnl(real_dev);
  2868. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  2869. struct net_device *dev = m->secy.netdev;
  2870. netif_stacked_transfer_operstate(real_dev, dev);
  2871. }
  2872. break;
  2873. }
  2874. case NETDEV_UNREGISTER: {
  2875. struct macsec_dev *m, *n;
  2876. struct macsec_rxh_data *rxd;
  2877. rxd = macsec_data_rtnl(real_dev);
  2878. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  2879. macsec_common_dellink(m->secy.netdev, &head);
  2880. }
  2881. netdev_rx_handler_unregister(real_dev);
  2882. kfree(rxd);
  2883. unregister_netdevice_many(&head);
  2884. break;
  2885. }
  2886. case NETDEV_CHANGEMTU: {
  2887. struct macsec_dev *m;
  2888. struct macsec_rxh_data *rxd;
  2889. rxd = macsec_data_rtnl(real_dev);
  2890. list_for_each_entry(m, &rxd->secys, secys) {
  2891. struct net_device *dev = m->secy.netdev;
  2892. unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
  2893. macsec_extra_len(true));
  2894. if (dev->mtu > mtu)
  2895. dev_set_mtu(dev, mtu);
  2896. }
  2897. }
  2898. }
  2899. return NOTIFY_OK;
  2900. }
  2901. static struct notifier_block macsec_notifier = {
  2902. .notifier_call = macsec_notify,
  2903. };
  2904. static int __init macsec_init(void)
  2905. {
  2906. int err;
  2907. pr_info("MACsec IEEE 802.1AE\n");
  2908. err = register_netdevice_notifier(&macsec_notifier);
  2909. if (err)
  2910. return err;
  2911. err = rtnl_link_register(&macsec_link_ops);
  2912. if (err)
  2913. goto notifier;
  2914. err = genl_register_family(&macsec_fam);
  2915. if (err)
  2916. goto rtnl;
  2917. return 0;
  2918. rtnl:
  2919. rtnl_link_unregister(&macsec_link_ops);
  2920. notifier:
  2921. unregister_netdevice_notifier(&macsec_notifier);
  2922. return err;
  2923. }
  2924. static void __exit macsec_exit(void)
  2925. {
  2926. genl_unregister_family(&macsec_fam);
  2927. rtnl_link_unregister(&macsec_link_ops);
  2928. unregister_netdevice_notifier(&macsec_notifier);
  2929. rcu_barrier();
  2930. }
  2931. module_init(macsec_init);
  2932. module_exit(macsec_exit);
  2933. MODULE_ALIAS_RTNL_LINK("macsec");
  2934. MODULE_ALIAS_GENL_FAMILY("macsec");
  2935. MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
  2936. MODULE_LICENSE("GPL v2");