macsec.c 82 KB

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