macsec.c 84 KB

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