macsec.c 81 KB

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