macsec.c 84 KB

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