macsec.c 85 KB

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