macsec.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597
  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. unsigned int len;
  916. sci_t sci;
  917. u32 pn;
  918. bool cbit;
  919. struct pcpu_rx_sc_stats *rxsc_stats;
  920. struct pcpu_secy_stats *secy_stats;
  921. bool pulled_sci;
  922. int ret;
  923. if (skb_headroom(skb) < ETH_HLEN)
  924. goto drop_direct;
  925. hdr = macsec_ethhdr(skb);
  926. if (hdr->eth.h_proto != htons(ETH_P_MACSEC)) {
  927. handle_not_macsec(skb);
  928. /* and deliver to the uncontrolled port */
  929. return RX_HANDLER_PASS;
  930. }
  931. skb = skb_unshare(skb, GFP_ATOMIC);
  932. *pskb = skb;
  933. if (!skb)
  934. return RX_HANDLER_CONSUMED;
  935. pulled_sci = pskb_may_pull(skb, macsec_extra_len(true));
  936. if (!pulled_sci) {
  937. if (!pskb_may_pull(skb, macsec_extra_len(false)))
  938. goto drop_direct;
  939. }
  940. hdr = macsec_ethhdr(skb);
  941. /* Frames with a SecTAG that has the TCI E bit set but the C
  942. * bit clear are discarded, as this reserved encoding is used
  943. * to identify frames with a SecTAG that are not to be
  944. * delivered to the Controlled Port.
  945. */
  946. if ((hdr->tci_an & (MACSEC_TCI_C | MACSEC_TCI_E)) == MACSEC_TCI_E)
  947. return RX_HANDLER_PASS;
  948. /* now, pull the extra length */
  949. if (hdr->tci_an & MACSEC_TCI_SC) {
  950. if (!pulled_sci)
  951. goto drop_direct;
  952. }
  953. /* ethernet header is part of crypto processing */
  954. skb_push(skb, ETH_HLEN);
  955. macsec_skb_cb(skb)->has_sci = !!(hdr->tci_an & MACSEC_TCI_SC);
  956. macsec_skb_cb(skb)->assoc_num = hdr->tci_an & MACSEC_AN_MASK;
  957. sci = macsec_frame_sci(hdr, macsec_skb_cb(skb)->has_sci);
  958. rcu_read_lock();
  959. rxd = macsec_data_rcu(skb->dev);
  960. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  961. struct macsec_rx_sc *sc = find_rx_sc(&macsec->secy, sci);
  962. sc = sc ? macsec_rxsc_get(sc) : NULL;
  963. if (sc) {
  964. secy = &macsec->secy;
  965. rx_sc = sc;
  966. break;
  967. }
  968. }
  969. if (!secy)
  970. goto nosci;
  971. dev = secy->netdev;
  972. macsec = macsec_priv(dev);
  973. secy_stats = this_cpu_ptr(macsec->stats);
  974. rxsc_stats = this_cpu_ptr(rx_sc->stats);
  975. if (!macsec_validate_skb(skb, secy->icv_len)) {
  976. u64_stats_update_begin(&secy_stats->syncp);
  977. secy_stats->stats.InPktsBadTag++;
  978. u64_stats_update_end(&secy_stats->syncp);
  979. goto drop_nosa;
  980. }
  981. rx_sa = macsec_rxsa_get(rx_sc->sa[macsec_skb_cb(skb)->assoc_num]);
  982. if (!rx_sa) {
  983. /* 10.6.1 if the SA is not in use */
  984. /* If validateFrames is Strict or the C bit in the
  985. * SecTAG is set, discard
  986. */
  987. if (hdr->tci_an & MACSEC_TCI_C ||
  988. secy->validate_frames == MACSEC_VALIDATE_STRICT) {
  989. u64_stats_update_begin(&rxsc_stats->syncp);
  990. rxsc_stats->stats.InPktsNotUsingSA++;
  991. u64_stats_update_end(&rxsc_stats->syncp);
  992. goto drop_nosa;
  993. }
  994. /* not Strict, the frame (with the SecTAG and ICV
  995. * removed) is delivered to the Controlled Port.
  996. */
  997. u64_stats_update_begin(&rxsc_stats->syncp);
  998. rxsc_stats->stats.InPktsUnusedSA++;
  999. u64_stats_update_end(&rxsc_stats->syncp);
  1000. goto deliver;
  1001. }
  1002. /* First, PN check to avoid decrypting obviously wrong packets */
  1003. pn = ntohl(hdr->packet_number);
  1004. if (secy->replay_protect) {
  1005. bool late;
  1006. spin_lock(&rx_sa->lock);
  1007. late = rx_sa->next_pn >= secy->replay_window &&
  1008. pn < (rx_sa->next_pn - secy->replay_window);
  1009. spin_unlock(&rx_sa->lock);
  1010. if (late) {
  1011. u64_stats_update_begin(&rxsc_stats->syncp);
  1012. rxsc_stats->stats.InPktsLate++;
  1013. u64_stats_update_end(&rxsc_stats->syncp);
  1014. goto drop;
  1015. }
  1016. }
  1017. macsec_skb_cb(skb)->rx_sa = rx_sa;
  1018. /* Disabled && !changed text => skip validation */
  1019. if (hdr->tci_an & MACSEC_TCI_C ||
  1020. secy->validate_frames != MACSEC_VALIDATE_DISABLED)
  1021. skb = macsec_decrypt(skb, dev, rx_sa, sci, secy);
  1022. if (IS_ERR(skb)) {
  1023. /* the decrypt callback needs the reference */
  1024. if (PTR_ERR(skb) != -EINPROGRESS) {
  1025. macsec_rxsa_put(rx_sa);
  1026. macsec_rxsc_put(rx_sc);
  1027. }
  1028. rcu_read_unlock();
  1029. *pskb = NULL;
  1030. return RX_HANDLER_CONSUMED;
  1031. }
  1032. if (!macsec_post_decrypt(skb, secy, pn))
  1033. goto drop;
  1034. deliver:
  1035. macsec_finalize_skb(skb, secy->icv_len,
  1036. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1037. macsec_reset_skb(skb, secy->netdev);
  1038. if (rx_sa)
  1039. macsec_rxsa_put(rx_sa);
  1040. macsec_rxsc_put(rx_sc);
  1041. skb_orphan(skb);
  1042. len = skb->len;
  1043. ret = gro_cells_receive(&macsec->gro_cells, skb);
  1044. if (ret == NET_RX_SUCCESS)
  1045. count_rx(dev, len);
  1046. else
  1047. macsec->secy.netdev->stats.rx_dropped++;
  1048. rcu_read_unlock();
  1049. *pskb = NULL;
  1050. return RX_HANDLER_CONSUMED;
  1051. drop:
  1052. macsec_rxsa_put(rx_sa);
  1053. drop_nosa:
  1054. macsec_rxsc_put(rx_sc);
  1055. rcu_read_unlock();
  1056. drop_direct:
  1057. kfree_skb(skb);
  1058. *pskb = NULL;
  1059. return RX_HANDLER_CONSUMED;
  1060. nosci:
  1061. /* 10.6.1 if the SC is not found */
  1062. cbit = !!(hdr->tci_an & MACSEC_TCI_C);
  1063. if (!cbit)
  1064. macsec_finalize_skb(skb, DEFAULT_ICV_LEN,
  1065. macsec_extra_len(macsec_skb_cb(skb)->has_sci));
  1066. list_for_each_entry_rcu(macsec, &rxd->secys, secys) {
  1067. struct sk_buff *nskb;
  1068. secy_stats = this_cpu_ptr(macsec->stats);
  1069. /* If validateFrames is Strict or the C bit in the
  1070. * SecTAG is set, discard
  1071. */
  1072. if (cbit ||
  1073. macsec->secy.validate_frames == MACSEC_VALIDATE_STRICT) {
  1074. u64_stats_update_begin(&secy_stats->syncp);
  1075. secy_stats->stats.InPktsNoSCI++;
  1076. u64_stats_update_end(&secy_stats->syncp);
  1077. continue;
  1078. }
  1079. /* not strict, the frame (with the SecTAG and ICV
  1080. * removed) is delivered to the Controlled Port.
  1081. */
  1082. nskb = skb_clone(skb, GFP_ATOMIC);
  1083. if (!nskb)
  1084. break;
  1085. macsec_reset_skb(nskb, macsec->secy.netdev);
  1086. ret = netif_rx(nskb);
  1087. if (ret == NET_RX_SUCCESS) {
  1088. u64_stats_update_begin(&secy_stats->syncp);
  1089. secy_stats->stats.InPktsUnknownSCI++;
  1090. u64_stats_update_end(&secy_stats->syncp);
  1091. } else {
  1092. macsec->secy.netdev->stats.rx_dropped++;
  1093. }
  1094. }
  1095. rcu_read_unlock();
  1096. *pskb = skb;
  1097. return RX_HANDLER_PASS;
  1098. }
  1099. static struct crypto_aead *macsec_alloc_tfm(char *key, int key_len, int icv_len)
  1100. {
  1101. struct crypto_aead *tfm;
  1102. int ret;
  1103. /* Pick a sync gcm(aes) cipher to ensure order is preserved. */
  1104. tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
  1105. if (IS_ERR(tfm))
  1106. return tfm;
  1107. ret = crypto_aead_setkey(tfm, key, key_len);
  1108. if (ret < 0)
  1109. goto fail;
  1110. ret = crypto_aead_setauthsize(tfm, icv_len);
  1111. if (ret < 0)
  1112. goto fail;
  1113. return tfm;
  1114. fail:
  1115. crypto_free_aead(tfm);
  1116. return ERR_PTR(ret);
  1117. }
  1118. static int init_rx_sa(struct macsec_rx_sa *rx_sa, char *sak, int key_len,
  1119. int icv_len)
  1120. {
  1121. rx_sa->stats = alloc_percpu(struct macsec_rx_sa_stats);
  1122. if (!rx_sa->stats)
  1123. return -ENOMEM;
  1124. rx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1125. if (IS_ERR(rx_sa->key.tfm)) {
  1126. free_percpu(rx_sa->stats);
  1127. return PTR_ERR(rx_sa->key.tfm);
  1128. }
  1129. rx_sa->active = false;
  1130. rx_sa->next_pn = 1;
  1131. refcount_set(&rx_sa->refcnt, 1);
  1132. spin_lock_init(&rx_sa->lock);
  1133. return 0;
  1134. }
  1135. static void clear_rx_sa(struct macsec_rx_sa *rx_sa)
  1136. {
  1137. rx_sa->active = false;
  1138. macsec_rxsa_put(rx_sa);
  1139. }
  1140. static void free_rx_sc(struct macsec_rx_sc *rx_sc)
  1141. {
  1142. int i;
  1143. for (i = 0; i < MACSEC_NUM_AN; i++) {
  1144. struct macsec_rx_sa *sa = rtnl_dereference(rx_sc->sa[i]);
  1145. RCU_INIT_POINTER(rx_sc->sa[i], NULL);
  1146. if (sa)
  1147. clear_rx_sa(sa);
  1148. }
  1149. macsec_rxsc_put(rx_sc);
  1150. }
  1151. static struct macsec_rx_sc *del_rx_sc(struct macsec_secy *secy, sci_t sci)
  1152. {
  1153. struct macsec_rx_sc *rx_sc, __rcu **rx_scp;
  1154. for (rx_scp = &secy->rx_sc, rx_sc = rtnl_dereference(*rx_scp);
  1155. rx_sc;
  1156. rx_scp = &rx_sc->next, rx_sc = rtnl_dereference(*rx_scp)) {
  1157. if (rx_sc->sci == sci) {
  1158. if (rx_sc->active)
  1159. secy->n_rx_sc--;
  1160. rcu_assign_pointer(*rx_scp, rx_sc->next);
  1161. return rx_sc;
  1162. }
  1163. }
  1164. return NULL;
  1165. }
  1166. static struct macsec_rx_sc *create_rx_sc(struct net_device *dev, sci_t sci)
  1167. {
  1168. struct macsec_rx_sc *rx_sc;
  1169. struct macsec_dev *macsec;
  1170. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  1171. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  1172. struct macsec_secy *secy;
  1173. list_for_each_entry(macsec, &rxd->secys, secys) {
  1174. if (find_rx_sc_rtnl(&macsec->secy, sci))
  1175. return ERR_PTR(-EEXIST);
  1176. }
  1177. rx_sc = kzalloc(sizeof(*rx_sc), GFP_KERNEL);
  1178. if (!rx_sc)
  1179. return ERR_PTR(-ENOMEM);
  1180. rx_sc->stats = netdev_alloc_pcpu_stats(struct pcpu_rx_sc_stats);
  1181. if (!rx_sc->stats) {
  1182. kfree(rx_sc);
  1183. return ERR_PTR(-ENOMEM);
  1184. }
  1185. rx_sc->sci = sci;
  1186. rx_sc->active = true;
  1187. refcount_set(&rx_sc->refcnt, 1);
  1188. secy = &macsec_priv(dev)->secy;
  1189. rcu_assign_pointer(rx_sc->next, secy->rx_sc);
  1190. rcu_assign_pointer(secy->rx_sc, rx_sc);
  1191. if (rx_sc->active)
  1192. secy->n_rx_sc++;
  1193. return rx_sc;
  1194. }
  1195. static int init_tx_sa(struct macsec_tx_sa *tx_sa, char *sak, int key_len,
  1196. int icv_len)
  1197. {
  1198. tx_sa->stats = alloc_percpu(struct macsec_tx_sa_stats);
  1199. if (!tx_sa->stats)
  1200. return -ENOMEM;
  1201. tx_sa->key.tfm = macsec_alloc_tfm(sak, key_len, icv_len);
  1202. if (IS_ERR(tx_sa->key.tfm)) {
  1203. free_percpu(tx_sa->stats);
  1204. return PTR_ERR(tx_sa->key.tfm);
  1205. }
  1206. tx_sa->active = false;
  1207. refcount_set(&tx_sa->refcnt, 1);
  1208. spin_lock_init(&tx_sa->lock);
  1209. return 0;
  1210. }
  1211. static void clear_tx_sa(struct macsec_tx_sa *tx_sa)
  1212. {
  1213. tx_sa->active = false;
  1214. macsec_txsa_put(tx_sa);
  1215. }
  1216. static struct genl_family macsec_fam;
  1217. static struct net_device *get_dev_from_nl(struct net *net,
  1218. struct nlattr **attrs)
  1219. {
  1220. int ifindex = nla_get_u32(attrs[MACSEC_ATTR_IFINDEX]);
  1221. struct net_device *dev;
  1222. dev = __dev_get_by_index(net, ifindex);
  1223. if (!dev)
  1224. return ERR_PTR(-ENODEV);
  1225. if (!netif_is_macsec(dev))
  1226. return ERR_PTR(-ENODEV);
  1227. return dev;
  1228. }
  1229. static sci_t nla_get_sci(const struct nlattr *nla)
  1230. {
  1231. return (__force sci_t)nla_get_u64(nla);
  1232. }
  1233. static int nla_put_sci(struct sk_buff *skb, int attrtype, sci_t value,
  1234. int padattr)
  1235. {
  1236. return nla_put_u64_64bit(skb, attrtype, (__force u64)value, padattr);
  1237. }
  1238. static struct macsec_tx_sa *get_txsa_from_nl(struct net *net,
  1239. struct nlattr **attrs,
  1240. struct nlattr **tb_sa,
  1241. struct net_device **devp,
  1242. struct macsec_secy **secyp,
  1243. struct macsec_tx_sc **scp,
  1244. u8 *assoc_num)
  1245. {
  1246. struct net_device *dev;
  1247. struct macsec_secy *secy;
  1248. struct macsec_tx_sc *tx_sc;
  1249. struct macsec_tx_sa *tx_sa;
  1250. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1251. return ERR_PTR(-EINVAL);
  1252. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1253. dev = get_dev_from_nl(net, attrs);
  1254. if (IS_ERR(dev))
  1255. return ERR_CAST(dev);
  1256. if (*assoc_num >= MACSEC_NUM_AN)
  1257. return ERR_PTR(-EINVAL);
  1258. secy = &macsec_priv(dev)->secy;
  1259. tx_sc = &secy->tx_sc;
  1260. tx_sa = rtnl_dereference(tx_sc->sa[*assoc_num]);
  1261. if (!tx_sa)
  1262. return ERR_PTR(-ENODEV);
  1263. *devp = dev;
  1264. *scp = tx_sc;
  1265. *secyp = secy;
  1266. return tx_sa;
  1267. }
  1268. static struct macsec_rx_sc *get_rxsc_from_nl(struct net *net,
  1269. struct nlattr **attrs,
  1270. struct nlattr **tb_rxsc,
  1271. struct net_device **devp,
  1272. struct macsec_secy **secyp)
  1273. {
  1274. struct net_device *dev;
  1275. struct macsec_secy *secy;
  1276. struct macsec_rx_sc *rx_sc;
  1277. sci_t sci;
  1278. dev = get_dev_from_nl(net, attrs);
  1279. if (IS_ERR(dev))
  1280. return ERR_CAST(dev);
  1281. secy = &macsec_priv(dev)->secy;
  1282. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1283. return ERR_PTR(-EINVAL);
  1284. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1285. rx_sc = find_rx_sc_rtnl(secy, sci);
  1286. if (!rx_sc)
  1287. return ERR_PTR(-ENODEV);
  1288. *secyp = secy;
  1289. *devp = dev;
  1290. return rx_sc;
  1291. }
  1292. static struct macsec_rx_sa *get_rxsa_from_nl(struct net *net,
  1293. struct nlattr **attrs,
  1294. struct nlattr **tb_rxsc,
  1295. struct nlattr **tb_sa,
  1296. struct net_device **devp,
  1297. struct macsec_secy **secyp,
  1298. struct macsec_rx_sc **scp,
  1299. u8 *assoc_num)
  1300. {
  1301. struct macsec_rx_sc *rx_sc;
  1302. struct macsec_rx_sa *rx_sa;
  1303. if (!tb_sa[MACSEC_SA_ATTR_AN])
  1304. return ERR_PTR(-EINVAL);
  1305. *assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1306. if (*assoc_num >= MACSEC_NUM_AN)
  1307. return ERR_PTR(-EINVAL);
  1308. rx_sc = get_rxsc_from_nl(net, attrs, tb_rxsc, devp, secyp);
  1309. if (IS_ERR(rx_sc))
  1310. return ERR_CAST(rx_sc);
  1311. rx_sa = rtnl_dereference(rx_sc->sa[*assoc_num]);
  1312. if (!rx_sa)
  1313. return ERR_PTR(-ENODEV);
  1314. *scp = rx_sc;
  1315. return rx_sa;
  1316. }
  1317. static const struct nla_policy macsec_genl_policy[NUM_MACSEC_ATTR] = {
  1318. [MACSEC_ATTR_IFINDEX] = { .type = NLA_U32 },
  1319. [MACSEC_ATTR_RXSC_CONFIG] = { .type = NLA_NESTED },
  1320. [MACSEC_ATTR_SA_CONFIG] = { .type = NLA_NESTED },
  1321. };
  1322. static const struct nla_policy macsec_genl_rxsc_policy[NUM_MACSEC_RXSC_ATTR] = {
  1323. [MACSEC_RXSC_ATTR_SCI] = { .type = NLA_U64 },
  1324. [MACSEC_RXSC_ATTR_ACTIVE] = { .type = NLA_U8 },
  1325. };
  1326. static const struct nla_policy macsec_genl_sa_policy[NUM_MACSEC_SA_ATTR] = {
  1327. [MACSEC_SA_ATTR_AN] = { .type = NLA_U8 },
  1328. [MACSEC_SA_ATTR_ACTIVE] = { .type = NLA_U8 },
  1329. [MACSEC_SA_ATTR_PN] = { .type = NLA_U32 },
  1330. [MACSEC_SA_ATTR_KEYID] = { .type = NLA_BINARY,
  1331. .len = MACSEC_KEYID_LEN, },
  1332. [MACSEC_SA_ATTR_KEY] = { .type = NLA_BINARY,
  1333. .len = MACSEC_MAX_KEY_LEN, },
  1334. };
  1335. static int parse_sa_config(struct nlattr **attrs, struct nlattr **tb_sa)
  1336. {
  1337. if (!attrs[MACSEC_ATTR_SA_CONFIG])
  1338. return -EINVAL;
  1339. if (nla_parse_nested(tb_sa, MACSEC_SA_ATTR_MAX,
  1340. attrs[MACSEC_ATTR_SA_CONFIG],
  1341. macsec_genl_sa_policy, NULL))
  1342. return -EINVAL;
  1343. return 0;
  1344. }
  1345. static int parse_rxsc_config(struct nlattr **attrs, struct nlattr **tb_rxsc)
  1346. {
  1347. if (!attrs[MACSEC_ATTR_RXSC_CONFIG])
  1348. return -EINVAL;
  1349. if (nla_parse_nested(tb_rxsc, MACSEC_RXSC_ATTR_MAX,
  1350. attrs[MACSEC_ATTR_RXSC_CONFIG],
  1351. macsec_genl_rxsc_policy, NULL))
  1352. return -EINVAL;
  1353. return 0;
  1354. }
  1355. static bool validate_add_rxsa(struct nlattr **attrs)
  1356. {
  1357. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1358. !attrs[MACSEC_SA_ATTR_KEY] ||
  1359. !attrs[MACSEC_SA_ATTR_KEYID])
  1360. return false;
  1361. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1362. return false;
  1363. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1364. return false;
  1365. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1366. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1367. return false;
  1368. }
  1369. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1370. return false;
  1371. return true;
  1372. }
  1373. static int macsec_add_rxsa(struct sk_buff *skb, struct genl_info *info)
  1374. {
  1375. struct net_device *dev;
  1376. struct nlattr **attrs = info->attrs;
  1377. struct macsec_secy *secy;
  1378. struct macsec_rx_sc *rx_sc;
  1379. struct macsec_rx_sa *rx_sa;
  1380. unsigned char assoc_num;
  1381. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1382. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1383. int err;
  1384. if (!attrs[MACSEC_ATTR_IFINDEX])
  1385. return -EINVAL;
  1386. if (parse_sa_config(attrs, tb_sa))
  1387. return -EINVAL;
  1388. if (parse_rxsc_config(attrs, tb_rxsc))
  1389. return -EINVAL;
  1390. if (!validate_add_rxsa(tb_sa))
  1391. return -EINVAL;
  1392. rtnl_lock();
  1393. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1394. if (IS_ERR(rx_sc)) {
  1395. rtnl_unlock();
  1396. return PTR_ERR(rx_sc);
  1397. }
  1398. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1399. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1400. pr_notice("macsec: nl: add_rxsa: bad key length: %d != %d\n",
  1401. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1402. rtnl_unlock();
  1403. return -EINVAL;
  1404. }
  1405. rx_sa = rtnl_dereference(rx_sc->sa[assoc_num]);
  1406. if (rx_sa) {
  1407. rtnl_unlock();
  1408. return -EBUSY;
  1409. }
  1410. rx_sa = kmalloc(sizeof(*rx_sa), GFP_KERNEL);
  1411. if (!rx_sa) {
  1412. rtnl_unlock();
  1413. return -ENOMEM;
  1414. }
  1415. err = init_rx_sa(rx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1416. secy->key_len, secy->icv_len);
  1417. if (err < 0) {
  1418. kfree(rx_sa);
  1419. rtnl_unlock();
  1420. return err;
  1421. }
  1422. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1423. spin_lock_bh(&rx_sa->lock);
  1424. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1425. spin_unlock_bh(&rx_sa->lock);
  1426. }
  1427. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1428. rx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1429. nla_memcpy(rx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1430. rx_sa->sc = rx_sc;
  1431. rcu_assign_pointer(rx_sc->sa[assoc_num], rx_sa);
  1432. rtnl_unlock();
  1433. return 0;
  1434. }
  1435. static bool validate_add_rxsc(struct nlattr **attrs)
  1436. {
  1437. if (!attrs[MACSEC_RXSC_ATTR_SCI])
  1438. return false;
  1439. if (attrs[MACSEC_RXSC_ATTR_ACTIVE]) {
  1440. if (nla_get_u8(attrs[MACSEC_RXSC_ATTR_ACTIVE]) > 1)
  1441. return false;
  1442. }
  1443. return true;
  1444. }
  1445. static int macsec_add_rxsc(struct sk_buff *skb, struct genl_info *info)
  1446. {
  1447. struct net_device *dev;
  1448. sci_t sci = MACSEC_UNDEF_SCI;
  1449. struct nlattr **attrs = info->attrs;
  1450. struct macsec_rx_sc *rx_sc;
  1451. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1452. if (!attrs[MACSEC_ATTR_IFINDEX])
  1453. return -EINVAL;
  1454. if (parse_rxsc_config(attrs, tb_rxsc))
  1455. return -EINVAL;
  1456. if (!validate_add_rxsc(tb_rxsc))
  1457. return -EINVAL;
  1458. rtnl_lock();
  1459. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1460. if (IS_ERR(dev)) {
  1461. rtnl_unlock();
  1462. return PTR_ERR(dev);
  1463. }
  1464. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1465. rx_sc = create_rx_sc(dev, sci);
  1466. if (IS_ERR(rx_sc)) {
  1467. rtnl_unlock();
  1468. return PTR_ERR(rx_sc);
  1469. }
  1470. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE])
  1471. rx_sc->active = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1472. rtnl_unlock();
  1473. return 0;
  1474. }
  1475. static bool validate_add_txsa(struct nlattr **attrs)
  1476. {
  1477. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1478. !attrs[MACSEC_SA_ATTR_PN] ||
  1479. !attrs[MACSEC_SA_ATTR_KEY] ||
  1480. !attrs[MACSEC_SA_ATTR_KEYID])
  1481. return false;
  1482. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1483. return false;
  1484. if (nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1485. return false;
  1486. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1487. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1488. return false;
  1489. }
  1490. if (nla_len(attrs[MACSEC_SA_ATTR_KEYID]) != MACSEC_KEYID_LEN)
  1491. return false;
  1492. return true;
  1493. }
  1494. static int macsec_add_txsa(struct sk_buff *skb, struct genl_info *info)
  1495. {
  1496. struct net_device *dev;
  1497. struct nlattr **attrs = info->attrs;
  1498. struct macsec_secy *secy;
  1499. struct macsec_tx_sc *tx_sc;
  1500. struct macsec_tx_sa *tx_sa;
  1501. unsigned char assoc_num;
  1502. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1503. int err;
  1504. if (!attrs[MACSEC_ATTR_IFINDEX])
  1505. return -EINVAL;
  1506. if (parse_sa_config(attrs, tb_sa))
  1507. return -EINVAL;
  1508. if (!validate_add_txsa(tb_sa))
  1509. return -EINVAL;
  1510. rtnl_lock();
  1511. dev = get_dev_from_nl(genl_info_net(info), attrs);
  1512. if (IS_ERR(dev)) {
  1513. rtnl_unlock();
  1514. return PTR_ERR(dev);
  1515. }
  1516. secy = &macsec_priv(dev)->secy;
  1517. tx_sc = &secy->tx_sc;
  1518. assoc_num = nla_get_u8(tb_sa[MACSEC_SA_ATTR_AN]);
  1519. if (nla_len(tb_sa[MACSEC_SA_ATTR_KEY]) != secy->key_len) {
  1520. pr_notice("macsec: nl: add_txsa: bad key length: %d != %d\n",
  1521. nla_len(tb_sa[MACSEC_SA_ATTR_KEY]), secy->key_len);
  1522. rtnl_unlock();
  1523. return -EINVAL;
  1524. }
  1525. tx_sa = rtnl_dereference(tx_sc->sa[assoc_num]);
  1526. if (tx_sa) {
  1527. rtnl_unlock();
  1528. return -EBUSY;
  1529. }
  1530. tx_sa = kmalloc(sizeof(*tx_sa), GFP_KERNEL);
  1531. if (!tx_sa) {
  1532. rtnl_unlock();
  1533. return -ENOMEM;
  1534. }
  1535. err = init_tx_sa(tx_sa, nla_data(tb_sa[MACSEC_SA_ATTR_KEY]),
  1536. secy->key_len, secy->icv_len);
  1537. if (err < 0) {
  1538. kfree(tx_sa);
  1539. rtnl_unlock();
  1540. return err;
  1541. }
  1542. nla_memcpy(tx_sa->key.id, tb_sa[MACSEC_SA_ATTR_KEYID], MACSEC_KEYID_LEN);
  1543. spin_lock_bh(&tx_sa->lock);
  1544. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1545. spin_unlock_bh(&tx_sa->lock);
  1546. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1547. tx_sa->active = !!nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1548. if (assoc_num == tx_sc->encoding_sa && tx_sa->active)
  1549. secy->operational = true;
  1550. rcu_assign_pointer(tx_sc->sa[assoc_num], tx_sa);
  1551. rtnl_unlock();
  1552. return 0;
  1553. }
  1554. static int macsec_del_rxsa(struct sk_buff *skb, struct genl_info *info)
  1555. {
  1556. struct nlattr **attrs = info->attrs;
  1557. struct net_device *dev;
  1558. struct macsec_secy *secy;
  1559. struct macsec_rx_sc *rx_sc;
  1560. struct macsec_rx_sa *rx_sa;
  1561. u8 assoc_num;
  1562. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1563. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1564. if (!attrs[MACSEC_ATTR_IFINDEX])
  1565. return -EINVAL;
  1566. if (parse_sa_config(attrs, tb_sa))
  1567. return -EINVAL;
  1568. if (parse_rxsc_config(attrs, tb_rxsc))
  1569. return -EINVAL;
  1570. rtnl_lock();
  1571. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1572. &dev, &secy, &rx_sc, &assoc_num);
  1573. if (IS_ERR(rx_sa)) {
  1574. rtnl_unlock();
  1575. return PTR_ERR(rx_sa);
  1576. }
  1577. if (rx_sa->active) {
  1578. rtnl_unlock();
  1579. return -EBUSY;
  1580. }
  1581. RCU_INIT_POINTER(rx_sc->sa[assoc_num], NULL);
  1582. clear_rx_sa(rx_sa);
  1583. rtnl_unlock();
  1584. return 0;
  1585. }
  1586. static int macsec_del_rxsc(struct sk_buff *skb, struct genl_info *info)
  1587. {
  1588. struct nlattr **attrs = info->attrs;
  1589. struct net_device *dev;
  1590. struct macsec_secy *secy;
  1591. struct macsec_rx_sc *rx_sc;
  1592. sci_t sci;
  1593. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1594. if (!attrs[MACSEC_ATTR_IFINDEX])
  1595. return -EINVAL;
  1596. if (parse_rxsc_config(attrs, tb_rxsc))
  1597. return -EINVAL;
  1598. if (!tb_rxsc[MACSEC_RXSC_ATTR_SCI])
  1599. return -EINVAL;
  1600. rtnl_lock();
  1601. dev = get_dev_from_nl(genl_info_net(info), info->attrs);
  1602. if (IS_ERR(dev)) {
  1603. rtnl_unlock();
  1604. return PTR_ERR(dev);
  1605. }
  1606. secy = &macsec_priv(dev)->secy;
  1607. sci = nla_get_sci(tb_rxsc[MACSEC_RXSC_ATTR_SCI]);
  1608. rx_sc = del_rx_sc(secy, sci);
  1609. if (!rx_sc) {
  1610. rtnl_unlock();
  1611. return -ENODEV;
  1612. }
  1613. free_rx_sc(rx_sc);
  1614. rtnl_unlock();
  1615. return 0;
  1616. }
  1617. static int macsec_del_txsa(struct sk_buff *skb, struct genl_info *info)
  1618. {
  1619. struct nlattr **attrs = info->attrs;
  1620. struct net_device *dev;
  1621. struct macsec_secy *secy;
  1622. struct macsec_tx_sc *tx_sc;
  1623. struct macsec_tx_sa *tx_sa;
  1624. u8 assoc_num;
  1625. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1626. if (!attrs[MACSEC_ATTR_IFINDEX])
  1627. return -EINVAL;
  1628. if (parse_sa_config(attrs, tb_sa))
  1629. return -EINVAL;
  1630. rtnl_lock();
  1631. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1632. &dev, &secy, &tx_sc, &assoc_num);
  1633. if (IS_ERR(tx_sa)) {
  1634. rtnl_unlock();
  1635. return PTR_ERR(tx_sa);
  1636. }
  1637. if (tx_sa->active) {
  1638. rtnl_unlock();
  1639. return -EBUSY;
  1640. }
  1641. RCU_INIT_POINTER(tx_sc->sa[assoc_num], NULL);
  1642. clear_tx_sa(tx_sa);
  1643. rtnl_unlock();
  1644. return 0;
  1645. }
  1646. static bool validate_upd_sa(struct nlattr **attrs)
  1647. {
  1648. if (!attrs[MACSEC_SA_ATTR_AN] ||
  1649. attrs[MACSEC_SA_ATTR_KEY] ||
  1650. attrs[MACSEC_SA_ATTR_KEYID])
  1651. return false;
  1652. if (nla_get_u8(attrs[MACSEC_SA_ATTR_AN]) >= MACSEC_NUM_AN)
  1653. return false;
  1654. if (attrs[MACSEC_SA_ATTR_PN] && nla_get_u32(attrs[MACSEC_SA_ATTR_PN]) == 0)
  1655. return false;
  1656. if (attrs[MACSEC_SA_ATTR_ACTIVE]) {
  1657. if (nla_get_u8(attrs[MACSEC_SA_ATTR_ACTIVE]) > 1)
  1658. return false;
  1659. }
  1660. return true;
  1661. }
  1662. static int macsec_upd_txsa(struct sk_buff *skb, struct genl_info *info)
  1663. {
  1664. struct nlattr **attrs = info->attrs;
  1665. struct net_device *dev;
  1666. struct macsec_secy *secy;
  1667. struct macsec_tx_sc *tx_sc;
  1668. struct macsec_tx_sa *tx_sa;
  1669. u8 assoc_num;
  1670. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1671. if (!attrs[MACSEC_ATTR_IFINDEX])
  1672. return -EINVAL;
  1673. if (parse_sa_config(attrs, tb_sa))
  1674. return -EINVAL;
  1675. if (!validate_upd_sa(tb_sa))
  1676. return -EINVAL;
  1677. rtnl_lock();
  1678. tx_sa = get_txsa_from_nl(genl_info_net(info), attrs, tb_sa,
  1679. &dev, &secy, &tx_sc, &assoc_num);
  1680. if (IS_ERR(tx_sa)) {
  1681. rtnl_unlock();
  1682. return PTR_ERR(tx_sa);
  1683. }
  1684. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1685. spin_lock_bh(&tx_sa->lock);
  1686. tx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1687. spin_unlock_bh(&tx_sa->lock);
  1688. }
  1689. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1690. tx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1691. if (assoc_num == tx_sc->encoding_sa)
  1692. secy->operational = tx_sa->active;
  1693. rtnl_unlock();
  1694. return 0;
  1695. }
  1696. static int macsec_upd_rxsa(struct sk_buff *skb, struct genl_info *info)
  1697. {
  1698. struct nlattr **attrs = info->attrs;
  1699. struct net_device *dev;
  1700. struct macsec_secy *secy;
  1701. struct macsec_rx_sc *rx_sc;
  1702. struct macsec_rx_sa *rx_sa;
  1703. u8 assoc_num;
  1704. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1705. struct nlattr *tb_sa[MACSEC_SA_ATTR_MAX + 1];
  1706. if (!attrs[MACSEC_ATTR_IFINDEX])
  1707. return -EINVAL;
  1708. if (parse_rxsc_config(attrs, tb_rxsc))
  1709. return -EINVAL;
  1710. if (parse_sa_config(attrs, tb_sa))
  1711. return -EINVAL;
  1712. if (!validate_upd_sa(tb_sa))
  1713. return -EINVAL;
  1714. rtnl_lock();
  1715. rx_sa = get_rxsa_from_nl(genl_info_net(info), attrs, tb_rxsc, tb_sa,
  1716. &dev, &secy, &rx_sc, &assoc_num);
  1717. if (IS_ERR(rx_sa)) {
  1718. rtnl_unlock();
  1719. return PTR_ERR(rx_sa);
  1720. }
  1721. if (tb_sa[MACSEC_SA_ATTR_PN]) {
  1722. spin_lock_bh(&rx_sa->lock);
  1723. rx_sa->next_pn = nla_get_u32(tb_sa[MACSEC_SA_ATTR_PN]);
  1724. spin_unlock_bh(&rx_sa->lock);
  1725. }
  1726. if (tb_sa[MACSEC_SA_ATTR_ACTIVE])
  1727. rx_sa->active = nla_get_u8(tb_sa[MACSEC_SA_ATTR_ACTIVE]);
  1728. rtnl_unlock();
  1729. return 0;
  1730. }
  1731. static int macsec_upd_rxsc(struct sk_buff *skb, struct genl_info *info)
  1732. {
  1733. struct nlattr **attrs = info->attrs;
  1734. struct net_device *dev;
  1735. struct macsec_secy *secy;
  1736. struct macsec_rx_sc *rx_sc;
  1737. struct nlattr *tb_rxsc[MACSEC_RXSC_ATTR_MAX + 1];
  1738. if (!attrs[MACSEC_ATTR_IFINDEX])
  1739. return -EINVAL;
  1740. if (parse_rxsc_config(attrs, tb_rxsc))
  1741. return -EINVAL;
  1742. if (!validate_add_rxsc(tb_rxsc))
  1743. return -EINVAL;
  1744. rtnl_lock();
  1745. rx_sc = get_rxsc_from_nl(genl_info_net(info), attrs, tb_rxsc, &dev, &secy);
  1746. if (IS_ERR(rx_sc)) {
  1747. rtnl_unlock();
  1748. return PTR_ERR(rx_sc);
  1749. }
  1750. if (tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]) {
  1751. bool new = !!nla_get_u8(tb_rxsc[MACSEC_RXSC_ATTR_ACTIVE]);
  1752. if (rx_sc->active != new)
  1753. secy->n_rx_sc += new ? 1 : -1;
  1754. rx_sc->active = new;
  1755. }
  1756. rtnl_unlock();
  1757. return 0;
  1758. }
  1759. static int copy_tx_sa_stats(struct sk_buff *skb,
  1760. struct macsec_tx_sa_stats __percpu *pstats)
  1761. {
  1762. struct macsec_tx_sa_stats sum = {0, };
  1763. int cpu;
  1764. for_each_possible_cpu(cpu) {
  1765. const struct macsec_tx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1766. sum.OutPktsProtected += stats->OutPktsProtected;
  1767. sum.OutPktsEncrypted += stats->OutPktsEncrypted;
  1768. }
  1769. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, sum.OutPktsProtected) ||
  1770. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, sum.OutPktsEncrypted))
  1771. return -EMSGSIZE;
  1772. return 0;
  1773. }
  1774. static int copy_rx_sa_stats(struct sk_buff *skb,
  1775. struct macsec_rx_sa_stats __percpu *pstats)
  1776. {
  1777. struct macsec_rx_sa_stats sum = {0, };
  1778. int cpu;
  1779. for_each_possible_cpu(cpu) {
  1780. const struct macsec_rx_sa_stats *stats = per_cpu_ptr(pstats, cpu);
  1781. sum.InPktsOK += stats->InPktsOK;
  1782. sum.InPktsInvalid += stats->InPktsInvalid;
  1783. sum.InPktsNotValid += stats->InPktsNotValid;
  1784. sum.InPktsNotUsingSA += stats->InPktsNotUsingSA;
  1785. sum.InPktsUnusedSA += stats->InPktsUnusedSA;
  1786. }
  1787. if (nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_OK, sum.InPktsOK) ||
  1788. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID, sum.InPktsInvalid) ||
  1789. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID, sum.InPktsNotValid) ||
  1790. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA, sum.InPktsNotUsingSA) ||
  1791. nla_put_u32(skb, MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA, sum.InPktsUnusedSA))
  1792. return -EMSGSIZE;
  1793. return 0;
  1794. }
  1795. static int copy_rx_sc_stats(struct sk_buff *skb,
  1796. struct pcpu_rx_sc_stats __percpu *pstats)
  1797. {
  1798. struct macsec_rx_sc_stats sum = {0, };
  1799. int cpu;
  1800. for_each_possible_cpu(cpu) {
  1801. const struct pcpu_rx_sc_stats *stats;
  1802. struct macsec_rx_sc_stats tmp;
  1803. unsigned int start;
  1804. stats = per_cpu_ptr(pstats, cpu);
  1805. do {
  1806. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1807. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1808. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1809. sum.InOctetsValidated += tmp.InOctetsValidated;
  1810. sum.InOctetsDecrypted += tmp.InOctetsDecrypted;
  1811. sum.InPktsUnchecked += tmp.InPktsUnchecked;
  1812. sum.InPktsDelayed += tmp.InPktsDelayed;
  1813. sum.InPktsOK += tmp.InPktsOK;
  1814. sum.InPktsInvalid += tmp.InPktsInvalid;
  1815. sum.InPktsLate += tmp.InPktsLate;
  1816. sum.InPktsNotValid += tmp.InPktsNotValid;
  1817. sum.InPktsNotUsingSA += tmp.InPktsNotUsingSA;
  1818. sum.InPktsUnusedSA += tmp.InPktsUnusedSA;
  1819. }
  1820. if (nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED,
  1821. sum.InOctetsValidated,
  1822. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1823. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED,
  1824. sum.InOctetsDecrypted,
  1825. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1826. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED,
  1827. sum.InPktsUnchecked,
  1828. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1829. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED,
  1830. sum.InPktsDelayed,
  1831. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1832. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK,
  1833. sum.InPktsOK,
  1834. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1835. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID,
  1836. sum.InPktsInvalid,
  1837. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1838. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE,
  1839. sum.InPktsLate,
  1840. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1841. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID,
  1842. sum.InPktsNotValid,
  1843. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1844. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA,
  1845. sum.InPktsNotUsingSA,
  1846. MACSEC_RXSC_STATS_ATTR_PAD) ||
  1847. nla_put_u64_64bit(skb, MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA,
  1848. sum.InPktsUnusedSA,
  1849. MACSEC_RXSC_STATS_ATTR_PAD))
  1850. return -EMSGSIZE;
  1851. return 0;
  1852. }
  1853. static int copy_tx_sc_stats(struct sk_buff *skb,
  1854. struct pcpu_tx_sc_stats __percpu *pstats)
  1855. {
  1856. struct macsec_tx_sc_stats sum = {0, };
  1857. int cpu;
  1858. for_each_possible_cpu(cpu) {
  1859. const struct pcpu_tx_sc_stats *stats;
  1860. struct macsec_tx_sc_stats tmp;
  1861. unsigned int start;
  1862. stats = per_cpu_ptr(pstats, cpu);
  1863. do {
  1864. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1865. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1866. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1867. sum.OutPktsProtected += tmp.OutPktsProtected;
  1868. sum.OutPktsEncrypted += tmp.OutPktsEncrypted;
  1869. sum.OutOctetsProtected += tmp.OutOctetsProtected;
  1870. sum.OutOctetsEncrypted += tmp.OutOctetsEncrypted;
  1871. }
  1872. if (nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED,
  1873. sum.OutPktsProtected,
  1874. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1875. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED,
  1876. sum.OutPktsEncrypted,
  1877. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1878. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED,
  1879. sum.OutOctetsProtected,
  1880. MACSEC_TXSC_STATS_ATTR_PAD) ||
  1881. nla_put_u64_64bit(skb, MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED,
  1882. sum.OutOctetsEncrypted,
  1883. MACSEC_TXSC_STATS_ATTR_PAD))
  1884. return -EMSGSIZE;
  1885. return 0;
  1886. }
  1887. static int copy_secy_stats(struct sk_buff *skb,
  1888. struct pcpu_secy_stats __percpu *pstats)
  1889. {
  1890. struct macsec_dev_stats sum = {0, };
  1891. int cpu;
  1892. for_each_possible_cpu(cpu) {
  1893. const struct pcpu_secy_stats *stats;
  1894. struct macsec_dev_stats tmp;
  1895. unsigned int start;
  1896. stats = per_cpu_ptr(pstats, cpu);
  1897. do {
  1898. start = u64_stats_fetch_begin_irq(&stats->syncp);
  1899. memcpy(&tmp, &stats->stats, sizeof(tmp));
  1900. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  1901. sum.OutPktsUntagged += tmp.OutPktsUntagged;
  1902. sum.InPktsUntagged += tmp.InPktsUntagged;
  1903. sum.OutPktsTooLong += tmp.OutPktsTooLong;
  1904. sum.InPktsNoTag += tmp.InPktsNoTag;
  1905. sum.InPktsBadTag += tmp.InPktsBadTag;
  1906. sum.InPktsUnknownSCI += tmp.InPktsUnknownSCI;
  1907. sum.InPktsNoSCI += tmp.InPktsNoSCI;
  1908. sum.InPktsOverrun += tmp.InPktsOverrun;
  1909. }
  1910. if (nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED,
  1911. sum.OutPktsUntagged,
  1912. MACSEC_SECY_STATS_ATTR_PAD) ||
  1913. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED,
  1914. sum.InPktsUntagged,
  1915. MACSEC_SECY_STATS_ATTR_PAD) ||
  1916. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG,
  1917. sum.OutPktsTooLong,
  1918. MACSEC_SECY_STATS_ATTR_PAD) ||
  1919. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG,
  1920. sum.InPktsNoTag,
  1921. MACSEC_SECY_STATS_ATTR_PAD) ||
  1922. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG,
  1923. sum.InPktsBadTag,
  1924. MACSEC_SECY_STATS_ATTR_PAD) ||
  1925. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI,
  1926. sum.InPktsUnknownSCI,
  1927. MACSEC_SECY_STATS_ATTR_PAD) ||
  1928. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI,
  1929. sum.InPktsNoSCI,
  1930. MACSEC_SECY_STATS_ATTR_PAD) ||
  1931. nla_put_u64_64bit(skb, MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN,
  1932. sum.InPktsOverrun,
  1933. MACSEC_SECY_STATS_ATTR_PAD))
  1934. return -EMSGSIZE;
  1935. return 0;
  1936. }
  1937. static int nla_put_secy(struct macsec_secy *secy, struct sk_buff *skb)
  1938. {
  1939. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1940. struct nlattr *secy_nest = nla_nest_start(skb, MACSEC_ATTR_SECY);
  1941. u64 csid;
  1942. if (!secy_nest)
  1943. return 1;
  1944. switch (secy->key_len) {
  1945. case MACSEC_GCM_AES_128_SAK_LEN:
  1946. csid = MACSEC_DEFAULT_CIPHER_ID;
  1947. break;
  1948. case MACSEC_GCM_AES_256_SAK_LEN:
  1949. csid = MACSEC_CIPHER_ID_GCM_AES_256;
  1950. break;
  1951. default:
  1952. goto cancel;
  1953. }
  1954. if (nla_put_sci(skb, MACSEC_SECY_ATTR_SCI, secy->sci,
  1955. MACSEC_SECY_ATTR_PAD) ||
  1956. nla_put_u64_64bit(skb, MACSEC_SECY_ATTR_CIPHER_SUITE,
  1957. csid, MACSEC_SECY_ATTR_PAD) ||
  1958. nla_put_u8(skb, MACSEC_SECY_ATTR_ICV_LEN, secy->icv_len) ||
  1959. nla_put_u8(skb, MACSEC_SECY_ATTR_OPER, secy->operational) ||
  1960. nla_put_u8(skb, MACSEC_SECY_ATTR_PROTECT, secy->protect_frames) ||
  1961. nla_put_u8(skb, MACSEC_SECY_ATTR_REPLAY, secy->replay_protect) ||
  1962. nla_put_u8(skb, MACSEC_SECY_ATTR_VALIDATE, secy->validate_frames) ||
  1963. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCRYPT, tx_sc->encrypt) ||
  1964. nla_put_u8(skb, MACSEC_SECY_ATTR_INC_SCI, tx_sc->send_sci) ||
  1965. nla_put_u8(skb, MACSEC_SECY_ATTR_ES, tx_sc->end_station) ||
  1966. nla_put_u8(skb, MACSEC_SECY_ATTR_SCB, tx_sc->scb) ||
  1967. nla_put_u8(skb, MACSEC_SECY_ATTR_ENCODING_SA, tx_sc->encoding_sa))
  1968. goto cancel;
  1969. if (secy->replay_protect) {
  1970. if (nla_put_u32(skb, MACSEC_SECY_ATTR_WINDOW, secy->replay_window))
  1971. goto cancel;
  1972. }
  1973. nla_nest_end(skb, secy_nest);
  1974. return 0;
  1975. cancel:
  1976. nla_nest_cancel(skb, secy_nest);
  1977. return 1;
  1978. }
  1979. static int dump_secy(struct macsec_secy *secy, struct net_device *dev,
  1980. struct sk_buff *skb, struct netlink_callback *cb)
  1981. {
  1982. struct macsec_rx_sc *rx_sc;
  1983. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  1984. struct nlattr *txsa_list, *rxsc_list;
  1985. int i, j;
  1986. void *hdr;
  1987. struct nlattr *attr;
  1988. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1989. &macsec_fam, NLM_F_MULTI, MACSEC_CMD_GET_TXSC);
  1990. if (!hdr)
  1991. return -EMSGSIZE;
  1992. genl_dump_check_consistent(cb, hdr);
  1993. if (nla_put_u32(skb, MACSEC_ATTR_IFINDEX, dev->ifindex))
  1994. goto nla_put_failure;
  1995. if (nla_put_secy(secy, skb))
  1996. goto nla_put_failure;
  1997. attr = nla_nest_start(skb, MACSEC_ATTR_TXSC_STATS);
  1998. if (!attr)
  1999. goto nla_put_failure;
  2000. if (copy_tx_sc_stats(skb, tx_sc->stats)) {
  2001. nla_nest_cancel(skb, attr);
  2002. goto nla_put_failure;
  2003. }
  2004. nla_nest_end(skb, attr);
  2005. attr = nla_nest_start(skb, MACSEC_ATTR_SECY_STATS);
  2006. if (!attr)
  2007. goto nla_put_failure;
  2008. if (copy_secy_stats(skb, macsec_priv(dev)->stats)) {
  2009. nla_nest_cancel(skb, attr);
  2010. goto nla_put_failure;
  2011. }
  2012. nla_nest_end(skb, attr);
  2013. txsa_list = nla_nest_start(skb, MACSEC_ATTR_TXSA_LIST);
  2014. if (!txsa_list)
  2015. goto nla_put_failure;
  2016. for (i = 0, j = 1; i < MACSEC_NUM_AN; i++) {
  2017. struct macsec_tx_sa *tx_sa = rtnl_dereference(tx_sc->sa[i]);
  2018. struct nlattr *txsa_nest;
  2019. if (!tx_sa)
  2020. continue;
  2021. txsa_nest = nla_nest_start(skb, j++);
  2022. if (!txsa_nest) {
  2023. nla_nest_cancel(skb, txsa_list);
  2024. goto nla_put_failure;
  2025. }
  2026. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2027. nla_put_u32(skb, MACSEC_SA_ATTR_PN, tx_sa->next_pn) ||
  2028. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, tx_sa->key.id) ||
  2029. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, tx_sa->active)) {
  2030. nla_nest_cancel(skb, txsa_nest);
  2031. nla_nest_cancel(skb, txsa_list);
  2032. goto nla_put_failure;
  2033. }
  2034. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  2035. if (!attr) {
  2036. nla_nest_cancel(skb, txsa_nest);
  2037. nla_nest_cancel(skb, txsa_list);
  2038. goto nla_put_failure;
  2039. }
  2040. if (copy_tx_sa_stats(skb, tx_sa->stats)) {
  2041. nla_nest_cancel(skb, attr);
  2042. nla_nest_cancel(skb, txsa_nest);
  2043. nla_nest_cancel(skb, txsa_list);
  2044. goto nla_put_failure;
  2045. }
  2046. nla_nest_end(skb, attr);
  2047. nla_nest_end(skb, txsa_nest);
  2048. }
  2049. nla_nest_end(skb, txsa_list);
  2050. rxsc_list = nla_nest_start(skb, MACSEC_ATTR_RXSC_LIST);
  2051. if (!rxsc_list)
  2052. goto nla_put_failure;
  2053. j = 1;
  2054. for_each_rxsc_rtnl(secy, rx_sc) {
  2055. int k;
  2056. struct nlattr *rxsa_list;
  2057. struct nlattr *rxsc_nest = nla_nest_start(skb, j++);
  2058. if (!rxsc_nest) {
  2059. nla_nest_cancel(skb, rxsc_list);
  2060. goto nla_put_failure;
  2061. }
  2062. if (nla_put_u8(skb, MACSEC_RXSC_ATTR_ACTIVE, rx_sc->active) ||
  2063. nla_put_sci(skb, MACSEC_RXSC_ATTR_SCI, rx_sc->sci,
  2064. MACSEC_RXSC_ATTR_PAD)) {
  2065. nla_nest_cancel(skb, rxsc_nest);
  2066. nla_nest_cancel(skb, rxsc_list);
  2067. goto nla_put_failure;
  2068. }
  2069. attr = nla_nest_start(skb, MACSEC_RXSC_ATTR_STATS);
  2070. if (!attr) {
  2071. nla_nest_cancel(skb, rxsc_nest);
  2072. nla_nest_cancel(skb, rxsc_list);
  2073. goto nla_put_failure;
  2074. }
  2075. if (copy_rx_sc_stats(skb, rx_sc->stats)) {
  2076. nla_nest_cancel(skb, attr);
  2077. nla_nest_cancel(skb, rxsc_nest);
  2078. nla_nest_cancel(skb, rxsc_list);
  2079. goto nla_put_failure;
  2080. }
  2081. nla_nest_end(skb, attr);
  2082. rxsa_list = nla_nest_start(skb, MACSEC_RXSC_ATTR_SA_LIST);
  2083. if (!rxsa_list) {
  2084. nla_nest_cancel(skb, rxsc_nest);
  2085. nla_nest_cancel(skb, rxsc_list);
  2086. goto nla_put_failure;
  2087. }
  2088. for (i = 0, k = 1; i < MACSEC_NUM_AN; i++) {
  2089. struct macsec_rx_sa *rx_sa = rtnl_dereference(rx_sc->sa[i]);
  2090. struct nlattr *rxsa_nest;
  2091. if (!rx_sa)
  2092. continue;
  2093. rxsa_nest = nla_nest_start(skb, k++);
  2094. if (!rxsa_nest) {
  2095. nla_nest_cancel(skb, rxsa_list);
  2096. nla_nest_cancel(skb, rxsc_nest);
  2097. nla_nest_cancel(skb, rxsc_list);
  2098. goto nla_put_failure;
  2099. }
  2100. attr = nla_nest_start(skb, MACSEC_SA_ATTR_STATS);
  2101. if (!attr) {
  2102. nla_nest_cancel(skb, rxsa_list);
  2103. nla_nest_cancel(skb, rxsc_nest);
  2104. nla_nest_cancel(skb, rxsc_list);
  2105. goto nla_put_failure;
  2106. }
  2107. if (copy_rx_sa_stats(skb, rx_sa->stats)) {
  2108. nla_nest_cancel(skb, attr);
  2109. nla_nest_cancel(skb, rxsa_list);
  2110. nla_nest_cancel(skb, rxsc_nest);
  2111. nla_nest_cancel(skb, rxsc_list);
  2112. goto nla_put_failure;
  2113. }
  2114. nla_nest_end(skb, attr);
  2115. if (nla_put_u8(skb, MACSEC_SA_ATTR_AN, i) ||
  2116. nla_put_u32(skb, MACSEC_SA_ATTR_PN, rx_sa->next_pn) ||
  2117. nla_put(skb, MACSEC_SA_ATTR_KEYID, MACSEC_KEYID_LEN, rx_sa->key.id) ||
  2118. nla_put_u8(skb, MACSEC_SA_ATTR_ACTIVE, rx_sa->active)) {
  2119. nla_nest_cancel(skb, rxsa_nest);
  2120. nla_nest_cancel(skb, rxsc_nest);
  2121. nla_nest_cancel(skb, rxsc_list);
  2122. goto nla_put_failure;
  2123. }
  2124. nla_nest_end(skb, rxsa_nest);
  2125. }
  2126. nla_nest_end(skb, rxsa_list);
  2127. nla_nest_end(skb, rxsc_nest);
  2128. }
  2129. nla_nest_end(skb, rxsc_list);
  2130. genlmsg_end(skb, hdr);
  2131. return 0;
  2132. nla_put_failure:
  2133. genlmsg_cancel(skb, hdr);
  2134. return -EMSGSIZE;
  2135. }
  2136. static int macsec_generation = 1; /* protected by RTNL */
  2137. static int macsec_dump_txsc(struct sk_buff *skb, struct netlink_callback *cb)
  2138. {
  2139. struct net *net = sock_net(skb->sk);
  2140. struct net_device *dev;
  2141. int dev_idx, d;
  2142. dev_idx = cb->args[0];
  2143. d = 0;
  2144. rtnl_lock();
  2145. cb->seq = macsec_generation;
  2146. for_each_netdev(net, dev) {
  2147. struct macsec_secy *secy;
  2148. if (d < dev_idx)
  2149. goto next;
  2150. if (!netif_is_macsec(dev))
  2151. goto next;
  2152. secy = &macsec_priv(dev)->secy;
  2153. if (dump_secy(secy, dev, skb, cb) < 0)
  2154. goto done;
  2155. next:
  2156. d++;
  2157. }
  2158. done:
  2159. rtnl_unlock();
  2160. cb->args[0] = d;
  2161. return skb->len;
  2162. }
  2163. static const struct genl_ops macsec_genl_ops[] = {
  2164. {
  2165. .cmd = MACSEC_CMD_GET_TXSC,
  2166. .dumpit = macsec_dump_txsc,
  2167. .policy = macsec_genl_policy,
  2168. },
  2169. {
  2170. .cmd = MACSEC_CMD_ADD_RXSC,
  2171. .doit = macsec_add_rxsc,
  2172. .policy = macsec_genl_policy,
  2173. .flags = GENL_ADMIN_PERM,
  2174. },
  2175. {
  2176. .cmd = MACSEC_CMD_DEL_RXSC,
  2177. .doit = macsec_del_rxsc,
  2178. .policy = macsec_genl_policy,
  2179. .flags = GENL_ADMIN_PERM,
  2180. },
  2181. {
  2182. .cmd = MACSEC_CMD_UPD_RXSC,
  2183. .doit = macsec_upd_rxsc,
  2184. .policy = macsec_genl_policy,
  2185. .flags = GENL_ADMIN_PERM,
  2186. },
  2187. {
  2188. .cmd = MACSEC_CMD_ADD_TXSA,
  2189. .doit = macsec_add_txsa,
  2190. .policy = macsec_genl_policy,
  2191. .flags = GENL_ADMIN_PERM,
  2192. },
  2193. {
  2194. .cmd = MACSEC_CMD_DEL_TXSA,
  2195. .doit = macsec_del_txsa,
  2196. .policy = macsec_genl_policy,
  2197. .flags = GENL_ADMIN_PERM,
  2198. },
  2199. {
  2200. .cmd = MACSEC_CMD_UPD_TXSA,
  2201. .doit = macsec_upd_txsa,
  2202. .policy = macsec_genl_policy,
  2203. .flags = GENL_ADMIN_PERM,
  2204. },
  2205. {
  2206. .cmd = MACSEC_CMD_ADD_RXSA,
  2207. .doit = macsec_add_rxsa,
  2208. .policy = macsec_genl_policy,
  2209. .flags = GENL_ADMIN_PERM,
  2210. },
  2211. {
  2212. .cmd = MACSEC_CMD_DEL_RXSA,
  2213. .doit = macsec_del_rxsa,
  2214. .policy = macsec_genl_policy,
  2215. .flags = GENL_ADMIN_PERM,
  2216. },
  2217. {
  2218. .cmd = MACSEC_CMD_UPD_RXSA,
  2219. .doit = macsec_upd_rxsa,
  2220. .policy = macsec_genl_policy,
  2221. .flags = GENL_ADMIN_PERM,
  2222. },
  2223. };
  2224. static struct genl_family macsec_fam __ro_after_init = {
  2225. .name = MACSEC_GENL_NAME,
  2226. .hdrsize = 0,
  2227. .version = MACSEC_GENL_VERSION,
  2228. .maxattr = MACSEC_ATTR_MAX,
  2229. .netnsok = true,
  2230. .module = THIS_MODULE,
  2231. .ops = macsec_genl_ops,
  2232. .n_ops = ARRAY_SIZE(macsec_genl_ops),
  2233. };
  2234. static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
  2235. struct net_device *dev)
  2236. {
  2237. struct macsec_dev *macsec = netdev_priv(dev);
  2238. struct macsec_secy *secy = &macsec->secy;
  2239. struct pcpu_secy_stats *secy_stats;
  2240. int ret, len;
  2241. /* 10.5 */
  2242. if (!secy->protect_frames) {
  2243. secy_stats = this_cpu_ptr(macsec->stats);
  2244. u64_stats_update_begin(&secy_stats->syncp);
  2245. secy_stats->stats.OutPktsUntagged++;
  2246. u64_stats_update_end(&secy_stats->syncp);
  2247. skb->dev = macsec->real_dev;
  2248. len = skb->len;
  2249. ret = dev_queue_xmit(skb);
  2250. count_tx(dev, ret, len);
  2251. return ret;
  2252. }
  2253. if (!secy->operational) {
  2254. kfree_skb(skb);
  2255. dev->stats.tx_dropped++;
  2256. return NETDEV_TX_OK;
  2257. }
  2258. skb = macsec_encrypt(skb, dev);
  2259. if (IS_ERR(skb)) {
  2260. if (PTR_ERR(skb) != -EINPROGRESS)
  2261. dev->stats.tx_dropped++;
  2262. return NETDEV_TX_OK;
  2263. }
  2264. macsec_count_tx(skb, &macsec->secy.tx_sc, macsec_skb_cb(skb)->tx_sa);
  2265. macsec_encrypt_finish(skb, dev);
  2266. len = skb->len;
  2267. ret = dev_queue_xmit(skb);
  2268. count_tx(dev, ret, len);
  2269. return ret;
  2270. }
  2271. #define MACSEC_FEATURES \
  2272. (NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
  2273. static struct lock_class_key macsec_netdev_addr_lock_key;
  2274. static int macsec_dev_init(struct net_device *dev)
  2275. {
  2276. struct macsec_dev *macsec = macsec_priv(dev);
  2277. struct net_device *real_dev = macsec->real_dev;
  2278. int err;
  2279. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  2280. if (!dev->tstats)
  2281. return -ENOMEM;
  2282. err = gro_cells_init(&macsec->gro_cells, dev);
  2283. if (err) {
  2284. free_percpu(dev->tstats);
  2285. return err;
  2286. }
  2287. dev->features = real_dev->features & MACSEC_FEATURES;
  2288. dev->features |= NETIF_F_LLTX | NETIF_F_GSO_SOFTWARE;
  2289. dev->needed_headroom = real_dev->needed_headroom +
  2290. MACSEC_NEEDED_HEADROOM;
  2291. dev->needed_tailroom = real_dev->needed_tailroom +
  2292. MACSEC_NEEDED_TAILROOM;
  2293. if (is_zero_ether_addr(dev->dev_addr))
  2294. eth_hw_addr_inherit(dev, real_dev);
  2295. if (is_zero_ether_addr(dev->broadcast))
  2296. memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len);
  2297. return 0;
  2298. }
  2299. static void macsec_dev_uninit(struct net_device *dev)
  2300. {
  2301. struct macsec_dev *macsec = macsec_priv(dev);
  2302. gro_cells_destroy(&macsec->gro_cells);
  2303. free_percpu(dev->tstats);
  2304. }
  2305. static netdev_features_t macsec_fix_features(struct net_device *dev,
  2306. netdev_features_t features)
  2307. {
  2308. struct macsec_dev *macsec = macsec_priv(dev);
  2309. struct net_device *real_dev = macsec->real_dev;
  2310. features &= (real_dev->features & MACSEC_FEATURES) |
  2311. NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES;
  2312. features |= NETIF_F_LLTX;
  2313. return features;
  2314. }
  2315. static int macsec_dev_open(struct net_device *dev)
  2316. {
  2317. struct macsec_dev *macsec = macsec_priv(dev);
  2318. struct net_device *real_dev = macsec->real_dev;
  2319. int err;
  2320. err = dev_uc_add(real_dev, dev->dev_addr);
  2321. if (err < 0)
  2322. return err;
  2323. if (dev->flags & IFF_ALLMULTI) {
  2324. err = dev_set_allmulti(real_dev, 1);
  2325. if (err < 0)
  2326. goto del_unicast;
  2327. }
  2328. if (dev->flags & IFF_PROMISC) {
  2329. err = dev_set_promiscuity(real_dev, 1);
  2330. if (err < 0)
  2331. goto clear_allmulti;
  2332. }
  2333. if (netif_carrier_ok(real_dev))
  2334. netif_carrier_on(dev);
  2335. return 0;
  2336. clear_allmulti:
  2337. if (dev->flags & IFF_ALLMULTI)
  2338. dev_set_allmulti(real_dev, -1);
  2339. del_unicast:
  2340. dev_uc_del(real_dev, dev->dev_addr);
  2341. netif_carrier_off(dev);
  2342. return err;
  2343. }
  2344. static int macsec_dev_stop(struct net_device *dev)
  2345. {
  2346. struct macsec_dev *macsec = macsec_priv(dev);
  2347. struct net_device *real_dev = macsec->real_dev;
  2348. netif_carrier_off(dev);
  2349. dev_mc_unsync(real_dev, dev);
  2350. dev_uc_unsync(real_dev, dev);
  2351. if (dev->flags & IFF_ALLMULTI)
  2352. dev_set_allmulti(real_dev, -1);
  2353. if (dev->flags & IFF_PROMISC)
  2354. dev_set_promiscuity(real_dev, -1);
  2355. dev_uc_del(real_dev, dev->dev_addr);
  2356. return 0;
  2357. }
  2358. static void macsec_dev_change_rx_flags(struct net_device *dev, int change)
  2359. {
  2360. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2361. if (!(dev->flags & IFF_UP))
  2362. return;
  2363. if (change & IFF_ALLMULTI)
  2364. dev_set_allmulti(real_dev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  2365. if (change & IFF_PROMISC)
  2366. dev_set_promiscuity(real_dev,
  2367. dev->flags & IFF_PROMISC ? 1 : -1);
  2368. }
  2369. static void macsec_dev_set_rx_mode(struct net_device *dev)
  2370. {
  2371. struct net_device *real_dev = macsec_priv(dev)->real_dev;
  2372. dev_mc_sync(real_dev, dev);
  2373. dev_uc_sync(real_dev, dev);
  2374. }
  2375. static sci_t dev_to_sci(struct net_device *dev, __be16 port)
  2376. {
  2377. return make_sci(dev->dev_addr, port);
  2378. }
  2379. static int macsec_set_mac_address(struct net_device *dev, void *p)
  2380. {
  2381. struct macsec_dev *macsec = macsec_priv(dev);
  2382. struct net_device *real_dev = macsec->real_dev;
  2383. struct sockaddr *addr = p;
  2384. int err;
  2385. if (!is_valid_ether_addr(addr->sa_data))
  2386. return -EADDRNOTAVAIL;
  2387. if (!(dev->flags & IFF_UP))
  2388. goto out;
  2389. err = dev_uc_add(real_dev, addr->sa_data);
  2390. if (err < 0)
  2391. return err;
  2392. dev_uc_del(real_dev, dev->dev_addr);
  2393. out:
  2394. ether_addr_copy(dev->dev_addr, addr->sa_data);
  2395. macsec->secy.sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2396. return 0;
  2397. }
  2398. static int macsec_change_mtu(struct net_device *dev, int new_mtu)
  2399. {
  2400. struct macsec_dev *macsec = macsec_priv(dev);
  2401. unsigned int extra = macsec->secy.icv_len + macsec_extra_len(true);
  2402. if (macsec->real_dev->mtu - extra < new_mtu)
  2403. return -ERANGE;
  2404. dev->mtu = new_mtu;
  2405. return 0;
  2406. }
  2407. static void macsec_get_stats64(struct net_device *dev,
  2408. struct rtnl_link_stats64 *s)
  2409. {
  2410. int cpu;
  2411. if (!dev->tstats)
  2412. return;
  2413. for_each_possible_cpu(cpu) {
  2414. struct pcpu_sw_netstats *stats;
  2415. struct pcpu_sw_netstats tmp;
  2416. int start;
  2417. stats = per_cpu_ptr(dev->tstats, cpu);
  2418. do {
  2419. start = u64_stats_fetch_begin_irq(&stats->syncp);
  2420. tmp.rx_packets = stats->rx_packets;
  2421. tmp.rx_bytes = stats->rx_bytes;
  2422. tmp.tx_packets = stats->tx_packets;
  2423. tmp.tx_bytes = stats->tx_bytes;
  2424. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  2425. s->rx_packets += tmp.rx_packets;
  2426. s->rx_bytes += tmp.rx_bytes;
  2427. s->tx_packets += tmp.tx_packets;
  2428. s->tx_bytes += tmp.tx_bytes;
  2429. }
  2430. s->rx_dropped = dev->stats.rx_dropped;
  2431. s->tx_dropped = dev->stats.tx_dropped;
  2432. }
  2433. static int macsec_get_iflink(const struct net_device *dev)
  2434. {
  2435. return macsec_priv(dev)->real_dev->ifindex;
  2436. }
  2437. static int macsec_get_nest_level(struct net_device *dev)
  2438. {
  2439. return macsec_priv(dev)->nest_level;
  2440. }
  2441. static const struct net_device_ops macsec_netdev_ops = {
  2442. .ndo_init = macsec_dev_init,
  2443. .ndo_uninit = macsec_dev_uninit,
  2444. .ndo_open = macsec_dev_open,
  2445. .ndo_stop = macsec_dev_stop,
  2446. .ndo_fix_features = macsec_fix_features,
  2447. .ndo_change_mtu = macsec_change_mtu,
  2448. .ndo_set_rx_mode = macsec_dev_set_rx_mode,
  2449. .ndo_change_rx_flags = macsec_dev_change_rx_flags,
  2450. .ndo_set_mac_address = macsec_set_mac_address,
  2451. .ndo_start_xmit = macsec_start_xmit,
  2452. .ndo_get_stats64 = macsec_get_stats64,
  2453. .ndo_get_iflink = macsec_get_iflink,
  2454. .ndo_get_lock_subclass = macsec_get_nest_level,
  2455. };
  2456. static const struct device_type macsec_type = {
  2457. .name = "macsec",
  2458. };
  2459. static const struct nla_policy macsec_rtnl_policy[IFLA_MACSEC_MAX + 1] = {
  2460. [IFLA_MACSEC_SCI] = { .type = NLA_U64 },
  2461. [IFLA_MACSEC_PORT] = { .type = NLA_U16 },
  2462. [IFLA_MACSEC_ICV_LEN] = { .type = NLA_U8 },
  2463. [IFLA_MACSEC_CIPHER_SUITE] = { .type = NLA_U64 },
  2464. [IFLA_MACSEC_WINDOW] = { .type = NLA_U32 },
  2465. [IFLA_MACSEC_ENCODING_SA] = { .type = NLA_U8 },
  2466. [IFLA_MACSEC_ENCRYPT] = { .type = NLA_U8 },
  2467. [IFLA_MACSEC_PROTECT] = { .type = NLA_U8 },
  2468. [IFLA_MACSEC_INC_SCI] = { .type = NLA_U8 },
  2469. [IFLA_MACSEC_ES] = { .type = NLA_U8 },
  2470. [IFLA_MACSEC_SCB] = { .type = NLA_U8 },
  2471. [IFLA_MACSEC_REPLAY_PROTECT] = { .type = NLA_U8 },
  2472. [IFLA_MACSEC_VALIDATION] = { .type = NLA_U8 },
  2473. };
  2474. static void macsec_free_netdev(struct net_device *dev)
  2475. {
  2476. struct macsec_dev *macsec = macsec_priv(dev);
  2477. free_percpu(macsec->stats);
  2478. free_percpu(macsec->secy.tx_sc.stats);
  2479. }
  2480. static void macsec_setup(struct net_device *dev)
  2481. {
  2482. ether_setup(dev);
  2483. dev->min_mtu = 0;
  2484. dev->max_mtu = ETH_MAX_MTU;
  2485. dev->priv_flags |= IFF_NO_QUEUE;
  2486. dev->netdev_ops = &macsec_netdev_ops;
  2487. dev->needs_free_netdev = true;
  2488. dev->priv_destructor = macsec_free_netdev;
  2489. SET_NETDEV_DEVTYPE(dev, &macsec_type);
  2490. eth_zero_addr(dev->broadcast);
  2491. }
  2492. static int macsec_changelink_common(struct net_device *dev,
  2493. struct nlattr *data[])
  2494. {
  2495. struct macsec_secy *secy;
  2496. struct macsec_tx_sc *tx_sc;
  2497. secy = &macsec_priv(dev)->secy;
  2498. tx_sc = &secy->tx_sc;
  2499. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2500. struct macsec_tx_sa *tx_sa;
  2501. tx_sc->encoding_sa = nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]);
  2502. tx_sa = rtnl_dereference(tx_sc->sa[tx_sc->encoding_sa]);
  2503. secy->operational = tx_sa && tx_sa->active;
  2504. }
  2505. if (data[IFLA_MACSEC_WINDOW])
  2506. secy->replay_window = nla_get_u32(data[IFLA_MACSEC_WINDOW]);
  2507. if (data[IFLA_MACSEC_ENCRYPT])
  2508. tx_sc->encrypt = !!nla_get_u8(data[IFLA_MACSEC_ENCRYPT]);
  2509. if (data[IFLA_MACSEC_PROTECT])
  2510. secy->protect_frames = !!nla_get_u8(data[IFLA_MACSEC_PROTECT]);
  2511. if (data[IFLA_MACSEC_INC_SCI])
  2512. tx_sc->send_sci = !!nla_get_u8(data[IFLA_MACSEC_INC_SCI]);
  2513. if (data[IFLA_MACSEC_ES])
  2514. tx_sc->end_station = !!nla_get_u8(data[IFLA_MACSEC_ES]);
  2515. if (data[IFLA_MACSEC_SCB])
  2516. tx_sc->scb = !!nla_get_u8(data[IFLA_MACSEC_SCB]);
  2517. if (data[IFLA_MACSEC_REPLAY_PROTECT])
  2518. secy->replay_protect = !!nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT]);
  2519. if (data[IFLA_MACSEC_VALIDATION])
  2520. secy->validate_frames = nla_get_u8(data[IFLA_MACSEC_VALIDATION]);
  2521. if (data[IFLA_MACSEC_CIPHER_SUITE]) {
  2522. switch (nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE])) {
  2523. case MACSEC_CIPHER_ID_GCM_AES_128:
  2524. case MACSEC_DEFAULT_CIPHER_ID:
  2525. secy->key_len = MACSEC_GCM_AES_128_SAK_LEN;
  2526. break;
  2527. case MACSEC_CIPHER_ID_GCM_AES_256:
  2528. secy->key_len = MACSEC_GCM_AES_256_SAK_LEN;
  2529. break;
  2530. default:
  2531. return -EINVAL;
  2532. }
  2533. }
  2534. return 0;
  2535. }
  2536. static int macsec_changelink(struct net_device *dev, struct nlattr *tb[],
  2537. struct nlattr *data[],
  2538. struct netlink_ext_ack *extack)
  2539. {
  2540. if (!data)
  2541. return 0;
  2542. if (data[IFLA_MACSEC_CIPHER_SUITE] ||
  2543. data[IFLA_MACSEC_ICV_LEN] ||
  2544. data[IFLA_MACSEC_SCI] ||
  2545. data[IFLA_MACSEC_PORT])
  2546. return -EINVAL;
  2547. return macsec_changelink_common(dev, data);
  2548. }
  2549. static void macsec_del_dev(struct macsec_dev *macsec)
  2550. {
  2551. int i;
  2552. while (macsec->secy.rx_sc) {
  2553. struct macsec_rx_sc *rx_sc = rtnl_dereference(macsec->secy.rx_sc);
  2554. rcu_assign_pointer(macsec->secy.rx_sc, rx_sc->next);
  2555. free_rx_sc(rx_sc);
  2556. }
  2557. for (i = 0; i < MACSEC_NUM_AN; i++) {
  2558. struct macsec_tx_sa *sa = rtnl_dereference(macsec->secy.tx_sc.sa[i]);
  2559. if (sa) {
  2560. RCU_INIT_POINTER(macsec->secy.tx_sc.sa[i], NULL);
  2561. clear_tx_sa(sa);
  2562. }
  2563. }
  2564. }
  2565. static void macsec_common_dellink(struct net_device *dev, struct list_head *head)
  2566. {
  2567. struct macsec_dev *macsec = macsec_priv(dev);
  2568. struct net_device *real_dev = macsec->real_dev;
  2569. unregister_netdevice_queue(dev, head);
  2570. list_del_rcu(&macsec->secys);
  2571. macsec_del_dev(macsec);
  2572. netdev_upper_dev_unlink(real_dev, dev);
  2573. macsec_generation++;
  2574. }
  2575. static void macsec_dellink(struct net_device *dev, struct list_head *head)
  2576. {
  2577. struct macsec_dev *macsec = macsec_priv(dev);
  2578. struct net_device *real_dev = macsec->real_dev;
  2579. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2580. macsec_common_dellink(dev, head);
  2581. if (list_empty(&rxd->secys)) {
  2582. netdev_rx_handler_unregister(real_dev);
  2583. kfree(rxd);
  2584. }
  2585. }
  2586. static int register_macsec_dev(struct net_device *real_dev,
  2587. struct net_device *dev)
  2588. {
  2589. struct macsec_dev *macsec = macsec_priv(dev);
  2590. struct macsec_rxh_data *rxd = macsec_data_rtnl(real_dev);
  2591. if (!rxd) {
  2592. int err;
  2593. rxd = kmalloc(sizeof(*rxd), GFP_KERNEL);
  2594. if (!rxd)
  2595. return -ENOMEM;
  2596. INIT_LIST_HEAD(&rxd->secys);
  2597. err = netdev_rx_handler_register(real_dev, macsec_handle_frame,
  2598. rxd);
  2599. if (err < 0) {
  2600. kfree(rxd);
  2601. return err;
  2602. }
  2603. }
  2604. list_add_tail_rcu(&macsec->secys, &rxd->secys);
  2605. return 0;
  2606. }
  2607. static bool sci_exists(struct net_device *dev, sci_t sci)
  2608. {
  2609. struct macsec_rxh_data *rxd = macsec_data_rtnl(dev);
  2610. struct macsec_dev *macsec;
  2611. list_for_each_entry(macsec, &rxd->secys, secys) {
  2612. if (macsec->secy.sci == sci)
  2613. return true;
  2614. }
  2615. return false;
  2616. }
  2617. static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
  2618. {
  2619. struct macsec_dev *macsec = macsec_priv(dev);
  2620. struct macsec_secy *secy = &macsec->secy;
  2621. macsec->stats = netdev_alloc_pcpu_stats(struct pcpu_secy_stats);
  2622. if (!macsec->stats)
  2623. return -ENOMEM;
  2624. secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
  2625. if (!secy->tx_sc.stats) {
  2626. free_percpu(macsec->stats);
  2627. return -ENOMEM;
  2628. }
  2629. if (sci == MACSEC_UNDEF_SCI)
  2630. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2631. secy->netdev = dev;
  2632. secy->operational = true;
  2633. secy->key_len = DEFAULT_SAK_LEN;
  2634. secy->icv_len = icv_len;
  2635. secy->validate_frames = MACSEC_VALIDATE_DEFAULT;
  2636. secy->protect_frames = true;
  2637. secy->replay_protect = false;
  2638. secy->sci = sci;
  2639. secy->tx_sc.active = true;
  2640. secy->tx_sc.encoding_sa = DEFAULT_ENCODING_SA;
  2641. secy->tx_sc.encrypt = DEFAULT_ENCRYPT;
  2642. secy->tx_sc.send_sci = DEFAULT_SEND_SCI;
  2643. secy->tx_sc.end_station = false;
  2644. secy->tx_sc.scb = false;
  2645. return 0;
  2646. }
  2647. static int macsec_newlink(struct net *net, struct net_device *dev,
  2648. struct nlattr *tb[], struct nlattr *data[],
  2649. struct netlink_ext_ack *extack)
  2650. {
  2651. struct macsec_dev *macsec = macsec_priv(dev);
  2652. rx_handler_func_t *rx_handler;
  2653. u8 icv_len = DEFAULT_ICV_LEN;
  2654. struct net_device *real_dev;
  2655. int err, mtu;
  2656. sci_t sci;
  2657. if (!tb[IFLA_LINK])
  2658. return -EINVAL;
  2659. real_dev = __dev_get_by_index(net, nla_get_u32(tb[IFLA_LINK]));
  2660. if (!real_dev)
  2661. return -ENODEV;
  2662. if (real_dev->type != ARPHRD_ETHER)
  2663. return -EINVAL;
  2664. dev->priv_flags |= IFF_MACSEC;
  2665. macsec->real_dev = real_dev;
  2666. if (data && data[IFLA_MACSEC_ICV_LEN])
  2667. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2668. mtu = real_dev->mtu - icv_len - macsec_extra_len(true);
  2669. if (mtu < 0)
  2670. dev->mtu = 0;
  2671. else
  2672. dev->mtu = mtu;
  2673. rx_handler = rtnl_dereference(real_dev->rx_handler);
  2674. if (rx_handler && rx_handler != macsec_handle_frame)
  2675. return -EBUSY;
  2676. err = register_netdevice(dev);
  2677. if (err < 0)
  2678. return err;
  2679. macsec->nest_level = dev_get_nest_level(real_dev) + 1;
  2680. netdev_lockdep_set_classes(dev);
  2681. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  2682. &macsec_netdev_addr_lock_key,
  2683. macsec_get_nest_level(dev));
  2684. err = netdev_upper_dev_link(real_dev, dev, extack);
  2685. if (err < 0)
  2686. goto unregister;
  2687. /* need to be already registered so that ->init has run and
  2688. * the MAC addr is set
  2689. */
  2690. if (data && data[IFLA_MACSEC_SCI])
  2691. sci = nla_get_sci(data[IFLA_MACSEC_SCI]);
  2692. else if (data && data[IFLA_MACSEC_PORT])
  2693. sci = dev_to_sci(dev, nla_get_be16(data[IFLA_MACSEC_PORT]));
  2694. else
  2695. sci = dev_to_sci(dev, MACSEC_PORT_ES);
  2696. if (rx_handler && sci_exists(real_dev, sci)) {
  2697. err = -EBUSY;
  2698. goto unlink;
  2699. }
  2700. err = macsec_add_dev(dev, sci, icv_len);
  2701. if (err)
  2702. goto unlink;
  2703. if (data) {
  2704. err = macsec_changelink_common(dev, data);
  2705. if (err)
  2706. goto del_dev;
  2707. }
  2708. err = register_macsec_dev(real_dev, dev);
  2709. if (err < 0)
  2710. goto del_dev;
  2711. netif_stacked_transfer_operstate(real_dev, dev);
  2712. linkwatch_fire_event(dev);
  2713. macsec_generation++;
  2714. return 0;
  2715. del_dev:
  2716. macsec_del_dev(macsec);
  2717. unlink:
  2718. netdev_upper_dev_unlink(real_dev, dev);
  2719. unregister:
  2720. unregister_netdevice(dev);
  2721. return err;
  2722. }
  2723. static int macsec_validate_attr(struct nlattr *tb[], struct nlattr *data[],
  2724. struct netlink_ext_ack *extack)
  2725. {
  2726. u64 csid = MACSEC_DEFAULT_CIPHER_ID;
  2727. u8 icv_len = DEFAULT_ICV_LEN;
  2728. int flag;
  2729. bool es, scb, sci;
  2730. if (!data)
  2731. return 0;
  2732. if (data[IFLA_MACSEC_CIPHER_SUITE])
  2733. csid = nla_get_u64(data[IFLA_MACSEC_CIPHER_SUITE]);
  2734. if (data[IFLA_MACSEC_ICV_LEN]) {
  2735. icv_len = nla_get_u8(data[IFLA_MACSEC_ICV_LEN]);
  2736. if (icv_len != DEFAULT_ICV_LEN) {
  2737. char dummy_key[DEFAULT_SAK_LEN] = { 0 };
  2738. struct crypto_aead *dummy_tfm;
  2739. dummy_tfm = macsec_alloc_tfm(dummy_key,
  2740. DEFAULT_SAK_LEN,
  2741. icv_len);
  2742. if (IS_ERR(dummy_tfm))
  2743. return PTR_ERR(dummy_tfm);
  2744. crypto_free_aead(dummy_tfm);
  2745. }
  2746. }
  2747. switch (csid) {
  2748. case MACSEC_CIPHER_ID_GCM_AES_128:
  2749. case MACSEC_CIPHER_ID_GCM_AES_256:
  2750. case MACSEC_DEFAULT_CIPHER_ID:
  2751. if (icv_len < MACSEC_MIN_ICV_LEN ||
  2752. icv_len > MACSEC_STD_ICV_LEN)
  2753. return -EINVAL;
  2754. break;
  2755. default:
  2756. return -EINVAL;
  2757. }
  2758. if (data[IFLA_MACSEC_ENCODING_SA]) {
  2759. if (nla_get_u8(data[IFLA_MACSEC_ENCODING_SA]) >= MACSEC_NUM_AN)
  2760. return -EINVAL;
  2761. }
  2762. for (flag = IFLA_MACSEC_ENCODING_SA + 1;
  2763. flag < IFLA_MACSEC_VALIDATION;
  2764. flag++) {
  2765. if (data[flag]) {
  2766. if (nla_get_u8(data[flag]) > 1)
  2767. return -EINVAL;
  2768. }
  2769. }
  2770. es = data[IFLA_MACSEC_ES] ? nla_get_u8(data[IFLA_MACSEC_ES]) : false;
  2771. sci = data[IFLA_MACSEC_INC_SCI] ? nla_get_u8(data[IFLA_MACSEC_INC_SCI]) : false;
  2772. scb = data[IFLA_MACSEC_SCB] ? nla_get_u8(data[IFLA_MACSEC_SCB]) : false;
  2773. if ((sci && (scb || es)) || (scb && es))
  2774. return -EINVAL;
  2775. if (data[IFLA_MACSEC_VALIDATION] &&
  2776. nla_get_u8(data[IFLA_MACSEC_VALIDATION]) > MACSEC_VALIDATE_MAX)
  2777. return -EINVAL;
  2778. if ((data[IFLA_MACSEC_REPLAY_PROTECT] &&
  2779. nla_get_u8(data[IFLA_MACSEC_REPLAY_PROTECT])) &&
  2780. !data[IFLA_MACSEC_WINDOW])
  2781. return -EINVAL;
  2782. return 0;
  2783. }
  2784. static struct net *macsec_get_link_net(const struct net_device *dev)
  2785. {
  2786. return dev_net(macsec_priv(dev)->real_dev);
  2787. }
  2788. static size_t macsec_get_size(const struct net_device *dev)
  2789. {
  2790. return nla_total_size_64bit(8) + /* IFLA_MACSEC_SCI */
  2791. nla_total_size(1) + /* IFLA_MACSEC_ICV_LEN */
  2792. nla_total_size_64bit(8) + /* IFLA_MACSEC_CIPHER_SUITE */
  2793. nla_total_size(4) + /* IFLA_MACSEC_WINDOW */
  2794. nla_total_size(1) + /* IFLA_MACSEC_ENCODING_SA */
  2795. nla_total_size(1) + /* IFLA_MACSEC_ENCRYPT */
  2796. nla_total_size(1) + /* IFLA_MACSEC_PROTECT */
  2797. nla_total_size(1) + /* IFLA_MACSEC_INC_SCI */
  2798. nla_total_size(1) + /* IFLA_MACSEC_ES */
  2799. nla_total_size(1) + /* IFLA_MACSEC_SCB */
  2800. nla_total_size(1) + /* IFLA_MACSEC_REPLAY_PROTECT */
  2801. nla_total_size(1) + /* IFLA_MACSEC_VALIDATION */
  2802. 0;
  2803. }
  2804. static int macsec_fill_info(struct sk_buff *skb,
  2805. const struct net_device *dev)
  2806. {
  2807. struct macsec_secy *secy = &macsec_priv(dev)->secy;
  2808. struct macsec_tx_sc *tx_sc = &secy->tx_sc;
  2809. u64 csid;
  2810. switch (secy->key_len) {
  2811. case MACSEC_GCM_AES_128_SAK_LEN:
  2812. csid = MACSEC_DEFAULT_CIPHER_ID;
  2813. break;
  2814. case MACSEC_GCM_AES_256_SAK_LEN:
  2815. csid = MACSEC_CIPHER_ID_GCM_AES_256;
  2816. break;
  2817. default:
  2818. goto nla_put_failure;
  2819. }
  2820. if (nla_put_sci(skb, IFLA_MACSEC_SCI, secy->sci,
  2821. IFLA_MACSEC_PAD) ||
  2822. nla_put_u8(skb, IFLA_MACSEC_ICV_LEN, secy->icv_len) ||
  2823. nla_put_u64_64bit(skb, IFLA_MACSEC_CIPHER_SUITE,
  2824. csid, IFLA_MACSEC_PAD) ||
  2825. nla_put_u8(skb, IFLA_MACSEC_ENCODING_SA, tx_sc->encoding_sa) ||
  2826. nla_put_u8(skb, IFLA_MACSEC_ENCRYPT, tx_sc->encrypt) ||
  2827. nla_put_u8(skb, IFLA_MACSEC_PROTECT, secy->protect_frames) ||
  2828. nla_put_u8(skb, IFLA_MACSEC_INC_SCI, tx_sc->send_sci) ||
  2829. nla_put_u8(skb, IFLA_MACSEC_ES, tx_sc->end_station) ||
  2830. nla_put_u8(skb, IFLA_MACSEC_SCB, tx_sc->scb) ||
  2831. nla_put_u8(skb, IFLA_MACSEC_REPLAY_PROTECT, secy->replay_protect) ||
  2832. nla_put_u8(skb, IFLA_MACSEC_VALIDATION, secy->validate_frames) ||
  2833. 0)
  2834. goto nla_put_failure;
  2835. if (secy->replay_protect) {
  2836. if (nla_put_u32(skb, IFLA_MACSEC_WINDOW, secy->replay_window))
  2837. goto nla_put_failure;
  2838. }
  2839. return 0;
  2840. nla_put_failure:
  2841. return -EMSGSIZE;
  2842. }
  2843. static struct rtnl_link_ops macsec_link_ops __read_mostly = {
  2844. .kind = "macsec",
  2845. .priv_size = sizeof(struct macsec_dev),
  2846. .maxtype = IFLA_MACSEC_MAX,
  2847. .policy = macsec_rtnl_policy,
  2848. .setup = macsec_setup,
  2849. .validate = macsec_validate_attr,
  2850. .newlink = macsec_newlink,
  2851. .changelink = macsec_changelink,
  2852. .dellink = macsec_dellink,
  2853. .get_size = macsec_get_size,
  2854. .fill_info = macsec_fill_info,
  2855. .get_link_net = macsec_get_link_net,
  2856. };
  2857. static bool is_macsec_master(struct net_device *dev)
  2858. {
  2859. return rcu_access_pointer(dev->rx_handler) == macsec_handle_frame;
  2860. }
  2861. static int macsec_notify(struct notifier_block *this, unsigned long event,
  2862. void *ptr)
  2863. {
  2864. struct net_device *real_dev = netdev_notifier_info_to_dev(ptr);
  2865. LIST_HEAD(head);
  2866. if (!is_macsec_master(real_dev))
  2867. return NOTIFY_DONE;
  2868. switch (event) {
  2869. case NETDEV_DOWN:
  2870. case NETDEV_UP:
  2871. case NETDEV_CHANGE: {
  2872. struct macsec_dev *m, *n;
  2873. struct macsec_rxh_data *rxd;
  2874. rxd = macsec_data_rtnl(real_dev);
  2875. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  2876. struct net_device *dev = m->secy.netdev;
  2877. netif_stacked_transfer_operstate(real_dev, dev);
  2878. }
  2879. break;
  2880. }
  2881. case NETDEV_UNREGISTER: {
  2882. struct macsec_dev *m, *n;
  2883. struct macsec_rxh_data *rxd;
  2884. rxd = macsec_data_rtnl(real_dev);
  2885. list_for_each_entry_safe(m, n, &rxd->secys, secys) {
  2886. macsec_common_dellink(m->secy.netdev, &head);
  2887. }
  2888. netdev_rx_handler_unregister(real_dev);
  2889. kfree(rxd);
  2890. unregister_netdevice_many(&head);
  2891. break;
  2892. }
  2893. case NETDEV_CHANGEMTU: {
  2894. struct macsec_dev *m;
  2895. struct macsec_rxh_data *rxd;
  2896. rxd = macsec_data_rtnl(real_dev);
  2897. list_for_each_entry(m, &rxd->secys, secys) {
  2898. struct net_device *dev = m->secy.netdev;
  2899. unsigned int mtu = real_dev->mtu - (m->secy.icv_len +
  2900. macsec_extra_len(true));
  2901. if (dev->mtu > mtu)
  2902. dev_set_mtu(dev, mtu);
  2903. }
  2904. }
  2905. }
  2906. return NOTIFY_OK;
  2907. }
  2908. static struct notifier_block macsec_notifier = {
  2909. .notifier_call = macsec_notify,
  2910. };
  2911. static int __init macsec_init(void)
  2912. {
  2913. int err;
  2914. pr_info("MACsec IEEE 802.1AE\n");
  2915. err = register_netdevice_notifier(&macsec_notifier);
  2916. if (err)
  2917. return err;
  2918. err = rtnl_link_register(&macsec_link_ops);
  2919. if (err)
  2920. goto notifier;
  2921. err = genl_register_family(&macsec_fam);
  2922. if (err)
  2923. goto rtnl;
  2924. return 0;
  2925. rtnl:
  2926. rtnl_link_unregister(&macsec_link_ops);
  2927. notifier:
  2928. unregister_netdevice_notifier(&macsec_notifier);
  2929. return err;
  2930. }
  2931. static void __exit macsec_exit(void)
  2932. {
  2933. genl_unregister_family(&macsec_fam);
  2934. rtnl_link_unregister(&macsec_link_ops);
  2935. unregister_netdevice_notifier(&macsec_notifier);
  2936. rcu_barrier();
  2937. }
  2938. module_init(macsec_init);
  2939. module_exit(macsec_exit);
  2940. MODULE_ALIAS_RTNL_LINK("macsec");
  2941. MODULE_ALIAS_GENL_FAMILY("macsec");
  2942. MODULE_DESCRIPTION("MACsec IEEE 802.1AE");
  2943. MODULE_LICENSE("GPL v2");