macsec.c 82 KB

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