macsec.c 83 KB

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