system-linux.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
  5. * Copyright (C) 2013 Steven Barth <steven@midlink.org>
  6. * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.org>
  7. * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
  8. * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define _GNU_SOURCE
  20. #include <sys/socket.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/stat.h>
  23. #include <sys/syscall.h>
  24. #include <net/if.h>
  25. #include <net/if_arp.h>
  26. #include <limits.h>
  27. #include <arpa/inet.h>
  28. #include <netinet/in.h>
  29. #include <netinet/ether.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/neighbour.h>
  32. #include <linux/sockios.h>
  33. #include <linux/ip.h>
  34. #include <linux/if_addr.h>
  35. #include <linux/if_link.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/if_bridge.h>
  38. #include <linux/if_tunnel.h>
  39. #include <linux/ip6_tunnel.h>
  40. #include <linux/ethtool.h>
  41. #include <linux/fib_rules.h>
  42. #include <linux/veth.h>
  43. #include <linux/version.h>
  44. #include <sched.h>
  45. #include "ethtool-modes.h"
  46. #ifndef RTN_FAILED_POLICY
  47. #define RTN_FAILED_POLICY 12
  48. #endif
  49. #ifndef IFA_F_NOPREFIXROUTE
  50. #define IFA_F_NOPREFIXROUTE 0x200
  51. #endif
  52. #ifndef IFA_FLAGS
  53. #define IFA_FLAGS (IFA_MULTICAST + 1)
  54. #endif
  55. #include <string.h>
  56. #include <fcntl.h>
  57. #include <glob.h>
  58. #include <time.h>
  59. #include <unistd.h>
  60. #include <netlink/msg.h>
  61. #include <netlink/attr.h>
  62. #include <netlink/socket.h>
  63. #include <libubox/uloop.h>
  64. #include "netifd.h"
  65. #include "device.h"
  66. #include "system.h"
  67. #include "utils.h"
  68. struct event_socket {
  69. struct uloop_fd uloop;
  70. struct nl_sock *sock;
  71. int bufsize;
  72. };
  73. static int sock_ioctl = -1;
  74. static struct nl_sock *sock_rtnl = NULL;
  75. static int cb_rtnl_event(struct nl_msg *msg, void *arg);
  76. static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
  77. static int system_add_proto_tunnel(const char *name, const uint8_t proto,
  78. const unsigned int link, struct blob_attr **tb);
  79. static char dev_buf[256];
  80. static const char *proc_path = "/proc";
  81. static const char *sysfs_path = "/sys";
  82. struct netdev_type {
  83. unsigned short id;
  84. const char *name;
  85. };
  86. static const struct netdev_type netdev_types[] = {
  87. {ARPHRD_NETROM, "netrom"},
  88. {ARPHRD_ETHER, "ethernet"},
  89. {ARPHRD_EETHER, "eethernet"},
  90. {ARPHRD_AX25, "ax25"},
  91. {ARPHRD_PRONET, "pronet"},
  92. {ARPHRD_CHAOS, "chaos"},
  93. {ARPHRD_IEEE802, "ieee802"},
  94. {ARPHRD_ARCNET, "arcnet"},
  95. {ARPHRD_APPLETLK, "appletlk"},
  96. {ARPHRD_DLCI, "dlci"},
  97. {ARPHRD_ATM, "atm"},
  98. {ARPHRD_METRICOM, "metricom"},
  99. {ARPHRD_IEEE1394, "ieee1394"},
  100. {ARPHRD_EUI64, "eui64"},
  101. {ARPHRD_INFINIBAND, "infiniband"},
  102. {ARPHRD_SLIP, "slip"},
  103. {ARPHRD_CSLIP, "cslip"},
  104. {ARPHRD_SLIP6, "slip6"},
  105. {ARPHRD_CSLIP6, "cslip6"},
  106. {ARPHRD_RSRVD, "rsrvd"},
  107. {ARPHRD_ADAPT, "adapt"},
  108. {ARPHRD_ROSE, "rose"},
  109. {ARPHRD_X25, "x25"},
  110. {ARPHRD_HWX25, "hwx25"},
  111. {ARPHRD_PPP, "ppp"},
  112. {ARPHRD_CISCO, "cisco"},
  113. {ARPHRD_LAPB, "lapb"},
  114. {ARPHRD_DDCMP, "ddcmp"},
  115. {ARPHRD_RAWHDLC, "rawhdlc"},
  116. {ARPHRD_TUNNEL, "tunnel"},
  117. {ARPHRD_TUNNEL6, "tunnel6"},
  118. {ARPHRD_FRAD, "frad"},
  119. {ARPHRD_SKIP, "skip"},
  120. {ARPHRD_LOOPBACK, "loopback"},
  121. {ARPHRD_LOCALTLK, "localtlk"},
  122. {ARPHRD_FDDI, "fddi"},
  123. {ARPHRD_BIF, "bif"},
  124. {ARPHRD_SIT, "sit"},
  125. {ARPHRD_IPDDP, "ipddp"},
  126. {ARPHRD_IPGRE, "ipgre"},
  127. {ARPHRD_PIMREG,"pimreg"},
  128. {ARPHRD_HIPPI, "hippi"},
  129. {ARPHRD_ASH, "ash"},
  130. {ARPHRD_ECONET, "econet"},
  131. {ARPHRD_IRDA, "irda"},
  132. {ARPHRD_FCPP, "fcpp"},
  133. {ARPHRD_FCAL, "fcal"},
  134. {ARPHRD_FCPL, "fcpl"},
  135. {ARPHRD_FCFABRIC, "fcfabric"},
  136. {ARPHRD_IEEE80211, "ieee80211"},
  137. {ARPHRD_IEEE80211_PRISM, "ie80211-prism"},
  138. {ARPHRD_IEEE80211_RADIOTAP, "ieee80211-radiotap"},
  139. #ifdef ARPHRD_PHONET
  140. {ARPHRD_PHONET, "phonet"},
  141. #endif
  142. #ifdef ARPHRD_PHONET_PIPE
  143. {ARPHRD_PHONET_PIPE, "phonet-pipe"},
  144. #endif
  145. {ARPHRD_IEEE802154, "ieee802154"},
  146. {ARPHRD_VOID, "void"},
  147. {ARPHRD_NONE, "none"}
  148. };
  149. static void
  150. handler_nl_event(struct uloop_fd *u, unsigned int events)
  151. {
  152. struct event_socket *ev = container_of(u, struct event_socket, uloop);
  153. int ret;
  154. ret = nl_recvmsgs_default(ev->sock);
  155. if (ret >= 0)
  156. return;
  157. switch (-ret) {
  158. case NLE_NOMEM:
  159. /* Increase rx buffer size on netlink socket */
  160. ev->bufsize *= 2;
  161. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  162. goto abort;
  163. /* Request full dump since some info got dropped */
  164. struct rtgenmsg msg = { .rtgen_family = AF_UNSPEC };
  165. nl_send_simple(ev->sock, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg));
  166. break;
  167. default:
  168. goto abort;
  169. }
  170. return;
  171. abort:
  172. uloop_fd_delete(&ev->uloop);
  173. return;
  174. }
  175. static void
  176. nl_udebug_cb(void *priv, struct nl_msg *msg)
  177. {
  178. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  179. udebug_netlink_msg(priv, nlmsg_get_proto(msg), nlh, nlh->nlmsg_len);
  180. }
  181. static struct nl_sock *
  182. create_socket(int protocol, int groups)
  183. {
  184. struct nl_sock *sock;
  185. sock = nl_socket_alloc();
  186. if (!sock)
  187. return NULL;
  188. if (groups)
  189. nl_join_groups(sock, groups);
  190. if (nl_connect(sock, protocol)) {
  191. nl_socket_free(sock);
  192. return NULL;
  193. }
  194. nl_socket_set_tx_debug_cb(sock, nl_udebug_cb, &udb_nl);
  195. nl_socket_set_rx_debug_cb(sock, nl_udebug_cb, &udb_nl);
  196. return sock;
  197. }
  198. static bool
  199. create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
  200. uloop_fd_handler cb, int flags)
  201. {
  202. ev->sock = create_socket(protocol, groups);
  203. if (!ev->sock)
  204. return false;
  205. ev->uloop.fd = nl_socket_get_fd(ev->sock);
  206. ev->uloop.cb = cb;
  207. if (uloop_fd_add(&ev->uloop, ULOOP_READ|flags))
  208. return false;
  209. return true;
  210. }
  211. static bool
  212. create_event_socket(struct event_socket *ev, int protocol,
  213. int (*cb)(struct nl_msg *msg, void *arg))
  214. {
  215. if (!create_raw_event_socket(ev, protocol, 0, handler_nl_event, ULOOP_ERROR_CB))
  216. return false;
  217. /* Install the valid custom callback handler */
  218. nl_socket_modify_cb(ev->sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
  219. /* Disable sequence number checking on event sockets */
  220. nl_socket_disable_seq_check(ev->sock);
  221. /* Increase rx buffer size to 65K on event sockets */
  222. ev->bufsize = 65535;
  223. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  224. return false;
  225. return true;
  226. }
  227. static bool
  228. create_hotplug_event_socket(struct event_socket *ev, int protocol,
  229. void (*cb)(struct uloop_fd *u, unsigned int events))
  230. {
  231. if (!create_raw_event_socket(ev, protocol, 1, cb, ULOOP_ERROR_CB))
  232. return false;
  233. /* Increase rx buffer size to 65K on event sockets */
  234. ev->bufsize = 65535;
  235. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  236. return false;
  237. return true;
  238. }
  239. static bool
  240. system_rtn_aton(const char *src, unsigned int *dst)
  241. {
  242. char *e;
  243. unsigned int n;
  244. if (!strcmp(src, "local"))
  245. n = RTN_LOCAL;
  246. else if (!strcmp(src, "nat"))
  247. n = RTN_NAT;
  248. else if (!strcmp(src, "broadcast"))
  249. n = RTN_BROADCAST;
  250. else if (!strcmp(src, "anycast"))
  251. n = RTN_ANYCAST;
  252. else if (!strcmp(src, "multicast"))
  253. n = RTN_MULTICAST;
  254. else if (!strcmp(src, "prohibit"))
  255. n = RTN_PROHIBIT;
  256. else if (!strcmp(src, "unreachable"))
  257. n = RTN_UNREACHABLE;
  258. else if (!strcmp(src, "blackhole"))
  259. n = RTN_BLACKHOLE;
  260. else if (!strcmp(src, "xresolve"))
  261. n = RTN_XRESOLVE;
  262. else if (!strcmp(src, "unicast"))
  263. n = RTN_UNICAST;
  264. else if (!strcmp(src, "throw"))
  265. n = RTN_THROW;
  266. else if (!strcmp(src, "failed_policy"))
  267. n = RTN_FAILED_POLICY;
  268. else {
  269. n = strtoul(src, &e, 0);
  270. if (!e || *e || e == src || n > 255)
  271. return false;
  272. }
  273. *dst = n;
  274. return true;
  275. }
  276. static bool
  277. system_tos_aton(const char *src, unsigned *dst)
  278. {
  279. char *e;
  280. *dst = strtoul(src, &e, 16);
  281. if (e == src || *e || *dst > 255)
  282. return false;
  283. return true;
  284. }
  285. int system_init(void)
  286. {
  287. static struct event_socket rtnl_event;
  288. static struct event_socket hotplug_event;
  289. sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
  290. system_fd_set_cloexec(sock_ioctl);
  291. /* Prepare socket for routing / address control */
  292. sock_rtnl = create_socket(NETLINK_ROUTE, 0);
  293. if (!sock_rtnl)
  294. return -1;
  295. if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
  296. return -1;
  297. if (!create_hotplug_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT,
  298. handle_hotplug_event))
  299. return -1;
  300. /* Receive network link events form kernel */
  301. nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
  302. return 0;
  303. }
  304. static void write_file(const char *path, const char *val)
  305. {
  306. int fd;
  307. fd = open(path, O_WRONLY);
  308. if (fd < 0)
  309. return;
  310. if (write(fd, val, strlen(val))) {}
  311. close(fd);
  312. }
  313. static int read_file(const char *path, char *buf, const size_t buf_sz)
  314. {
  315. int fd = -1, ret = -1;
  316. fd = open(path, O_RDONLY);
  317. if (fd < 0)
  318. goto out;
  319. ssize_t len = read(fd, buf, buf_sz - 1);
  320. if (len < 0)
  321. goto out;
  322. ret = buf[len] = 0;
  323. out:
  324. if (fd >= 0)
  325. close(fd);
  326. return ret;
  327. }
  328. static const char *
  329. dev_sysctl_path(const char *prefix, const char *ifname, const char *file)
  330. {
  331. snprintf(dev_buf, sizeof(dev_buf), "%s/sys/net/%s/%s/%s", proc_path, prefix, ifname, file);
  332. return dev_buf;
  333. }
  334. static const char *
  335. dev_sysfs_path(const char *ifname, const char *file)
  336. {
  337. snprintf(dev_buf, sizeof(dev_buf), "%s/class/net/%s/%s", sysfs_path, ifname, file);
  338. return dev_buf;
  339. }
  340. static void
  341. system_set_dev_sysctl(const char *prefix, const char *file, const char *ifname,
  342. const char *val)
  343. {
  344. write_file(dev_sysctl_path(prefix, ifname, file), val);
  345. }
  346. static int
  347. system_get_dev_sysctl(const char *prefix, const char *file, const char *ifname,
  348. char *buf, size_t buf_sz)
  349. {
  350. return read_file(dev_sysctl_path(prefix, ifname, file), buf, buf_sz);
  351. }
  352. static void
  353. system_set_dev_sysfs(const char *file, const char *ifname, const char *val)
  354. {
  355. if (!val)
  356. return;
  357. write_file(dev_sysfs_path(ifname, file), val);
  358. }
  359. static void
  360. system_set_dev_sysfs_int(const char *file, const char *ifname, int val)
  361. {
  362. char buf[16];
  363. snprintf(buf, sizeof(buf), "%d", val);
  364. system_set_dev_sysfs(file, ifname, buf);
  365. }
  366. static int
  367. system_get_dev_sysfs(const char *file, const char *ifname, char *buf, size_t buf_sz)
  368. {
  369. return read_file(dev_sysfs_path(ifname, file), buf, buf_sz);
  370. }
  371. static void system_set_disable_ipv6(struct device *dev, const char *val)
  372. {
  373. system_set_dev_sysctl("ipv6/conf", "disable_ipv6", dev->ifname, val);
  374. }
  375. static void system_set_ip6segmentrouting(struct device *dev, const char *val)
  376. {
  377. system_set_dev_sysctl("ipv6/conf", "seg6_enabled", dev->ifname, val);
  378. }
  379. static void system_set_rpfilter(struct device *dev, const char *val)
  380. {
  381. system_set_dev_sysctl("ipv4/conf", "rp_filter", dev->ifname, val);
  382. }
  383. static void system_set_acceptlocal(struct device *dev, const char *val)
  384. {
  385. system_set_dev_sysctl("ipv4/conf", "accept_local", dev->ifname, val);
  386. }
  387. static void system_set_igmpversion(struct device *dev, const char *val)
  388. {
  389. system_set_dev_sysctl("ipv4/conf", "force_igmp_version", dev->ifname, val);
  390. }
  391. static void system_set_mldversion(struct device *dev, const char *val)
  392. {
  393. system_set_dev_sysctl("ipv6/conf", "force_mld_version", dev->ifname, val);
  394. }
  395. static void system_set_neigh4reachabletime(struct device *dev, const char *val)
  396. {
  397. system_set_dev_sysctl("ipv4/neigh", "base_reachable_time_ms", dev->ifname, val);
  398. }
  399. static void system_set_neigh6reachabletime(struct device *dev, const char *val)
  400. {
  401. system_set_dev_sysctl("ipv6/neigh", "base_reachable_time_ms", dev->ifname, val);
  402. }
  403. static void system_set_neigh4gcstaletime(struct device *dev, const char *val)
  404. {
  405. system_set_dev_sysctl("ipv4/neigh", "gc_stale_time", dev->ifname, val);
  406. }
  407. static void system_set_neigh6gcstaletime(struct device *dev, const char *val)
  408. {
  409. system_set_dev_sysctl("ipv6/neigh", "gc_stale_time", dev->ifname, val);
  410. }
  411. static void system_set_neigh4locktime(struct device *dev, const char *val)
  412. {
  413. system_set_dev_sysctl("ipv4/neigh", "locktime", dev->ifname, val);
  414. }
  415. static void system_set_dadtransmits(struct device *dev, const char *val)
  416. {
  417. system_set_dev_sysctl("ipv6/conf", "dad_transmits", dev->ifname, val);
  418. }
  419. static void system_set_sendredirects(struct device *dev, const char *val)
  420. {
  421. system_set_dev_sysctl("ipv4/conf", "send_redirects", dev->ifname, val);
  422. }
  423. static void system_set_drop_v4_unicast_in_l2_multicast(struct device *dev, const char *val)
  424. {
  425. system_set_dev_sysctl("ipv4/conf", "drop_unicast_in_l2_multicast", dev->ifname, val);
  426. }
  427. static void system_set_drop_v6_unicast_in_l2_multicast(struct device *dev, const char *val)
  428. {
  429. system_set_dev_sysctl("ipv6/conf", "drop_unicast_in_l2_multicast", dev->ifname, val);
  430. }
  431. static void system_set_drop_gratuitous_arp(struct device *dev, const char *val)
  432. {
  433. system_set_dev_sysctl("ipv4/conf", "drop_gratuitous_arp", dev->ifname, val);
  434. }
  435. static void system_set_drop_unsolicited_na(struct device *dev, const char *val)
  436. {
  437. system_set_dev_sysctl("ipv6/conf", "drop_unsolicited_na", dev->ifname, val);
  438. }
  439. static void system_set_arp_accept(struct device *dev, const char *val)
  440. {
  441. system_set_dev_sysctl("ipv4/conf", "arp_accept", dev->ifname, val);
  442. }
  443. static void system_bridge_set_multicast_to_unicast(struct device *dev, const char *val)
  444. {
  445. system_set_dev_sysfs("brport/multicast_to_unicast", dev->ifname, val);
  446. }
  447. static void system_bridge_set_multicast_fast_leave(struct device *dev, const char *val)
  448. {
  449. system_set_dev_sysfs("brport/multicast_fast_leave", dev->ifname, val);
  450. }
  451. static void system_bridge_set_hairpin_mode(struct device *dev, const char *val)
  452. {
  453. system_set_dev_sysfs("brport/hairpin_mode", dev->ifname, val);
  454. }
  455. static void system_bridge_set_proxyarp_wifi(struct device *dev, const char *val)
  456. {
  457. system_set_dev_sysfs("brport/proxyarp_wifi", dev->ifname, val);
  458. }
  459. static void system_bridge_set_bpdu_filter(struct device *dev, const char *val)
  460. {
  461. system_set_dev_sysfs("brport/bpdu_filter", dev->ifname, val);
  462. }
  463. static void system_bridge_set_isolated(struct device *dev, const char *val)
  464. {
  465. system_set_dev_sysfs("brport/isolated", dev->ifname, val);
  466. }
  467. static void system_bridge_set_multicast_router(struct device *dev, const char *val)
  468. {
  469. system_set_dev_sysfs("brport/multicast_router", dev->ifname, val);
  470. }
  471. void system_bridge_set_stp_state(struct device *dev, bool val)
  472. {
  473. const char *valstr = val ? "1" : "0";
  474. system_set_dev_sysfs("bridge/stp_state", dev->ifname, valstr);
  475. }
  476. static void system_bridge_set_learning(struct device *dev, const char *val)
  477. {
  478. system_set_dev_sysfs("brport/learning", dev->ifname, val);
  479. }
  480. static void system_bridge_set_unicast_flood(struct device *dev, const char *val)
  481. {
  482. system_set_dev_sysfs("brport/unicast_flood", dev->ifname, val);
  483. }
  484. static int system_get_disable_ipv6(struct device *dev, char *buf, const size_t buf_sz)
  485. {
  486. return system_get_dev_sysctl("ipv6/conf", "disable_ipv6",
  487. dev->ifname, buf, buf_sz);
  488. }
  489. static int system_get_ip6segmentrouting(struct device *dev, char *buf, const size_t buf_sz)
  490. {
  491. return system_get_dev_sysctl("ipv6/conf", "seg6_enabled",
  492. dev->ifname, buf, buf_sz);
  493. }
  494. static int system_get_rpfilter(struct device *dev, char *buf, const size_t buf_sz)
  495. {
  496. return system_get_dev_sysctl("ipv4/conf", "rp_filter",
  497. dev->ifname, buf, buf_sz);
  498. }
  499. static int system_get_acceptlocal(struct device *dev, char *buf, const size_t buf_sz)
  500. {
  501. return system_get_dev_sysctl("ipv4/conf", "accept_local",
  502. dev->ifname, buf, buf_sz);
  503. }
  504. static int system_get_igmpversion(struct device *dev, char *buf, const size_t buf_sz)
  505. {
  506. return system_get_dev_sysctl("ipv4/conf", "force_igmp_version",
  507. dev->ifname, buf, buf_sz);
  508. }
  509. static int system_get_mldversion(struct device *dev, char *buf, const size_t buf_sz)
  510. {
  511. return system_get_dev_sysctl("ipv6/conf", "force_mld_version",
  512. dev->ifname, buf, buf_sz);
  513. }
  514. static int system_get_neigh4reachabletime(struct device *dev, char *buf, const size_t buf_sz)
  515. {
  516. return system_get_dev_sysctl("ipv4/neigh", "base_reachable_time_ms",
  517. dev->ifname, buf, buf_sz);
  518. }
  519. static int system_get_neigh6reachabletime(struct device *dev, char *buf, const size_t buf_sz)
  520. {
  521. return system_get_dev_sysctl("ipv6/neigh", "base_reachable_time_ms",
  522. dev->ifname, buf, buf_sz);
  523. }
  524. static int system_get_neigh4gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
  525. {
  526. return system_get_dev_sysctl("ipv4/neigh", "gc_stale_time",
  527. dev->ifname, buf, buf_sz);
  528. }
  529. static int system_get_neigh6gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
  530. {
  531. return system_get_dev_sysctl("ipv6/neigh", "gc_stale_time",
  532. dev->ifname, buf, buf_sz);
  533. }
  534. static int system_get_neigh4locktime(struct device *dev, char *buf, const size_t buf_sz)
  535. {
  536. return system_get_dev_sysctl("ipv4/neigh", "locktime",
  537. dev->ifname, buf, buf_sz);
  538. }
  539. static int system_get_dadtransmits(struct device *dev, char *buf, const size_t buf_sz)
  540. {
  541. return system_get_dev_sysctl("ipv6/conf", "dad_transmits",
  542. dev->ifname, buf, buf_sz);
  543. }
  544. static int system_get_sendredirects(struct device *dev, char *buf, const size_t buf_sz)
  545. {
  546. return system_get_dev_sysctl("ipv4/conf", "send_redirects",
  547. dev->ifname, buf, buf_sz);
  548. }
  549. static int system_get_drop_v4_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
  550. {
  551. return system_get_dev_sysctl("ipv4/conf", "drop_unicast_in_l2_multicast",
  552. dev->ifname, buf, buf_sz);
  553. }
  554. static int system_get_drop_v6_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
  555. {
  556. return system_get_dev_sysctl("ipv6/conf", "drop_unicast_in_l2_multicast",
  557. dev->ifname, buf, buf_sz);
  558. }
  559. static int system_get_drop_gratuitous_arp(struct device *dev, char *buf, const size_t buf_sz)
  560. {
  561. return system_get_dev_sysctl("ipv4/conf", "drop_gratuitous_arp",
  562. dev->ifname, buf, buf_sz);
  563. }
  564. static int system_get_drop_unsolicited_na(struct device *dev, char *buf, const size_t buf_sz)
  565. {
  566. return system_get_dev_sysctl("ipv6/conf", "drop_unsolicited_na",
  567. dev->ifname, buf, buf_sz);
  568. }
  569. static int system_get_arp_accept(struct device *dev, char *buf, const size_t buf_sz)
  570. {
  571. return system_get_dev_sysctl("ipv4/conf", "arp_accept",
  572. dev->ifname, buf, buf_sz);
  573. }
  574. #ifndef IFF_LOWER_UP
  575. #define IFF_LOWER_UP 0x10000
  576. #endif
  577. static void
  578. system_device_update_state(struct device *dev, unsigned int flags, unsigned int ifindex)
  579. {
  580. if (dev->type == &simple_device_type) {
  581. if (dev->external)
  582. device_set_disabled(dev, !(flags & IFF_UP));
  583. device_set_present(dev, ifindex > 0);
  584. }
  585. device_set_link(dev, flags & IFF_LOWER_UP ? true : false);
  586. }
  587. /* Evaluate netlink messages */
  588. static int cb_rtnl_event(struct nl_msg *msg, void *arg)
  589. {
  590. struct nlmsghdr *nh = nlmsg_hdr(msg);
  591. struct ifinfomsg *ifi = NLMSG_DATA(nh);
  592. struct nlattr *nla[__IFLA_MAX];
  593. struct device *dev;
  594. if (nh->nlmsg_type != RTM_NEWLINK)
  595. return 0;
  596. nlmsg_parse(nh, sizeof(struct ifinfomsg), nla, __IFLA_MAX - 1, NULL);
  597. if (!nla[IFLA_IFNAME])
  598. return 0;
  599. dev = device_find(nla_data(nla[IFLA_IFNAME]));
  600. if (!dev)
  601. return 0;
  602. system_device_update_state(dev, ifi->ifi_flags, ifi->ifi_index);
  603. return 0;
  604. }
  605. static void
  606. handle_hotplug_msg(char *data, int size)
  607. {
  608. const char *subsystem = NULL, *interface = NULL, *interface_old = NULL;
  609. char *cur, *end, *sep;
  610. int skip;
  611. bool add;
  612. if (!strncmp(data, "add@", 4) || !strncmp(data, "move@", 5))
  613. add = true;
  614. else if (!strncmp(data, "remove@", 7))
  615. add = false;
  616. else
  617. return;
  618. skip = strlen(data) + 1;
  619. end = data + size;
  620. for (cur = data + skip; cur < end; cur += skip) {
  621. skip = strlen(cur) + 1;
  622. sep = strchr(cur, '=');
  623. if (!sep)
  624. continue;
  625. *sep = 0;
  626. if (!strcmp(cur, "INTERFACE"))
  627. interface = sep + 1;
  628. else if (!strcmp(cur, "SUBSYSTEM")) {
  629. subsystem = sep + 1;
  630. if (strcmp(subsystem, "net") != 0)
  631. return;
  632. } else if (!strcmp(cur, "DEVPATH_OLD")) {
  633. interface_old = strrchr(sep + 1, '/');
  634. if (interface_old)
  635. interface_old++;
  636. }
  637. }
  638. if (!subsystem || !interface)
  639. return;
  640. if (interface_old)
  641. device_hotplug_event(interface_old, false);
  642. device_hotplug_event(interface, add);
  643. }
  644. static void
  645. handle_hotplug_event(struct uloop_fd *u, unsigned int events)
  646. {
  647. struct event_socket *ev = container_of(u, struct event_socket, uloop);
  648. struct sockaddr_nl nla;
  649. unsigned char *buf = NULL;
  650. int size;
  651. while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
  652. if (nla.nl_pid == 0)
  653. handle_hotplug_msg((char *) buf, size);
  654. free(buf);
  655. }
  656. switch (-size) {
  657. case 0:
  658. return;
  659. case NLE_NOMEM:
  660. /* Increase rx buffer size on netlink socket */
  661. ev->bufsize *= 2;
  662. if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
  663. goto abort;
  664. break;
  665. default:
  666. goto abort;
  667. }
  668. return;
  669. abort:
  670. uloop_fd_delete(&ev->uloop);
  671. return;
  672. }
  673. static int system_rtnl_call(struct nl_msg *msg)
  674. {
  675. int ret;
  676. ret = nl_send_auto_complete(sock_rtnl, msg);
  677. nlmsg_free(msg);
  678. if (ret < 0)
  679. return ret;
  680. return nl_wait_for_ack(sock_rtnl);
  681. }
  682. static struct nl_msg *__system_ifinfo_msg(int af, int index, const char *ifname, uint16_t type, uint16_t flags)
  683. {
  684. struct nl_msg *msg;
  685. struct ifinfomsg iim = {
  686. .ifi_family = af,
  687. .ifi_index = index,
  688. };
  689. msg = nlmsg_alloc_simple(type, flags | NLM_F_REQUEST);
  690. if (!msg)
  691. return NULL;
  692. nlmsg_append(msg, &iim, sizeof(iim), 0);
  693. if (ifname)
  694. nla_put_string(msg, IFLA_IFNAME, ifname);
  695. return msg;
  696. }
  697. static struct nl_msg *system_ifinfo_msg(const char *ifname, uint16_t type, uint16_t flags)
  698. {
  699. return __system_ifinfo_msg(AF_UNSPEC, 0, ifname, type, flags);
  700. }
  701. static int system_link_del(const char *ifname)
  702. {
  703. struct nl_msg *msg;
  704. msg = system_ifinfo_msg(ifname, RTM_DELLINK, 0);
  705. if (!msg)
  706. return -1;
  707. return system_rtnl_call(msg);
  708. }
  709. int system_bridge_delbr(struct device *bridge)
  710. {
  711. return system_link_del(bridge->ifname);
  712. }
  713. static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
  714. {
  715. struct ifreq ifr;
  716. memset(&ifr, 0, sizeof(ifr));
  717. if (dev)
  718. ifr.ifr_ifindex = dev->ifindex;
  719. else
  720. ifr.ifr_data = data;
  721. strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name) - 1);
  722. return ioctl(sock_ioctl, cmd, &ifr);
  723. }
  724. static bool system_is_bridge(const char *name)
  725. {
  726. struct stat st;
  727. return stat(dev_sysfs_path(name, "bridge"), &st) >= 0;
  728. }
  729. static char *system_get_bridge(const char *name, char *buf, int buflen)
  730. {
  731. char *path;
  732. ssize_t len = -1;
  733. glob_t gl;
  734. snprintf(buf, buflen, "%s/devices/virtual/net/*/brif/%s/bridge", sysfs_path, name);
  735. if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
  736. return NULL;
  737. if (gl.gl_pathc > 0)
  738. len = readlink(gl.gl_pathv[0], buf, buflen);
  739. globfree(&gl);
  740. if (len < 0)
  741. return NULL;
  742. buf[len] = 0;
  743. path = strrchr(buf, '/');
  744. if (!path)
  745. return NULL;
  746. return path + 1;
  747. }
  748. static void
  749. system_bridge_set_wireless(struct device *bridge, struct device *dev)
  750. {
  751. bool mcast_to_ucast = dev->wireless_ap;
  752. bool hairpin;
  753. if (dev->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST)
  754. mcast_to_ucast = dev->settings.multicast_to_unicast;
  755. else if (bridge->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST &&
  756. !bridge->settings.multicast_to_unicast)
  757. mcast_to_ucast = false;
  758. hairpin = mcast_to_ucast || dev->wireless_proxyarp;
  759. if (dev->wireless_isolate)
  760. hairpin = false;
  761. system_bridge_set_multicast_to_unicast(dev, mcast_to_ucast ? "1" : "0");
  762. system_bridge_set_hairpin_mode(dev, hairpin ? "1" : "0");
  763. system_bridge_set_proxyarp_wifi(dev, dev->wireless_proxyarp ? "1" : "0");
  764. }
  765. int system_bridge_addif(struct device *bridge, struct device *dev)
  766. {
  767. char buf[64];
  768. char *oldbr;
  769. int tries = 0;
  770. int ret;
  771. for (tries = 0; tries < 3; tries++) {
  772. ret = 0;
  773. oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
  774. if (oldbr && !strcmp(oldbr, bridge->ifname))
  775. break;
  776. ret = system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
  777. if (!ret)
  778. break;
  779. D(SYSTEM, "Failed to add device '%s' to bridge '%s' (tries=%d): %s",
  780. dev->ifname, bridge->ifname, tries, strerror(errno));
  781. }
  782. if (dev->wireless)
  783. system_bridge_set_wireless(bridge, dev);
  784. if (dev->settings.flags & DEV_OPT_MULTICAST_ROUTER) {
  785. snprintf(buf, sizeof(buf), "%u", dev->settings.multicast_router);
  786. system_bridge_set_multicast_router(dev, buf);
  787. }
  788. if (dev->settings.flags & DEV_OPT_MULTICAST_FAST_LEAVE &&
  789. dev->settings.multicast_fast_leave)
  790. system_bridge_set_multicast_fast_leave(dev, "1");
  791. if (dev->settings.flags & DEV_OPT_LEARNING &&
  792. !dev->settings.learning)
  793. system_bridge_set_learning(dev, "0");
  794. if (dev->settings.flags & DEV_OPT_UNICAST_FLOOD &&
  795. !dev->settings.unicast_flood)
  796. system_bridge_set_unicast_flood(dev, "0");
  797. if (dev->settings.flags & DEV_OPT_ISOLATE &&
  798. dev->settings.isolate)
  799. system_bridge_set_isolated(dev, "1");
  800. if (dev->bpdu_filter)
  801. system_bridge_set_bpdu_filter(dev, dev->bpdu_filter ? "1" : "0");
  802. return ret;
  803. }
  804. int system_bridge_delif(struct device *bridge, struct device *dev)
  805. {
  806. return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
  807. }
  808. int system_bridge_vlan(const char *iface, uint16_t vid, int16_t vid_end, bool add, unsigned int vflags)
  809. {
  810. struct bridge_vlan_info vinfo = { .vid = vid, };
  811. unsigned short flags = 0;
  812. struct nlattr *afspec;
  813. struct nl_msg *nlm;
  814. int index;
  815. int ret = 0;
  816. index = if_nametoindex(iface);
  817. if (!index)
  818. return -1;
  819. nlm = __system_ifinfo_msg(PF_BRIDGE, index, NULL, add ? RTM_SETLINK : RTM_DELLINK, 0);
  820. if (!nlm)
  821. return -1;
  822. if (vflags & BRVLAN_F_SELF)
  823. flags |= BRIDGE_FLAGS_SELF;
  824. if (vflags & BRVLAN_F_PVID)
  825. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  826. if (vflags & BRVLAN_F_UNTAGGED)
  827. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  828. afspec = nla_nest_start(nlm, IFLA_AF_SPEC);
  829. if (!afspec) {
  830. ret = -ENOMEM;
  831. goto failure;
  832. }
  833. if (flags)
  834. nla_put_u16(nlm, IFLA_BRIDGE_FLAGS, flags);
  835. if (vid_end > vid)
  836. vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
  837. nla_put(nlm, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
  838. if (vid_end > vid) {
  839. vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
  840. vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
  841. vinfo.vid = vid_end;
  842. nla_put(nlm, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
  843. }
  844. nla_nest_end(nlm, afspec);
  845. return system_rtnl_call(nlm);
  846. failure:
  847. nlmsg_free(nlm);
  848. return ret;
  849. }
  850. int system_bonding_set_device(struct device *dev, struct bonding_config *cfg)
  851. {
  852. const char *ifname = dev->ifname;
  853. struct blob_attr *cur;
  854. char op = cfg ? '+' : '-';
  855. char buf[64];
  856. size_t rem;
  857. snprintf(dev_buf, sizeof(dev_buf), "%s/class/net/bonding_masters", sysfs_path);
  858. snprintf(buf, sizeof(buf), "%c%s", op, ifname);
  859. write_file(dev_buf, buf);
  860. if (!cfg)
  861. return 0;
  862. system_set_dev_sysfs("bonding/mode", ifname, bonding_policy_str[cfg->policy]);
  863. system_set_dev_sysfs_int("bonding/all_ports_active", ifname, cfg->all_ports_active);
  864. if (cfg->policy == BONDING_MODE_BALANCE_XOR ||
  865. cfg->policy == BONDING_MODE_BALANCE_TLB ||
  866. cfg->policy == BONDING_MODE_8023AD)
  867. system_set_dev_sysfs("bonding/xmit_hash_policy", ifname, cfg->xmit_hash_policy);
  868. if (cfg->policy == BONDING_MODE_8023AD) {
  869. system_set_dev_sysfs("bonding/ad_actor_system", ifname, cfg->ad_actor_system);
  870. system_set_dev_sysfs_int("bonding/ad_actor_sys_prio", ifname, cfg->ad_actor_sys_prio);
  871. system_set_dev_sysfs("bonding/ad_select", ifname, cfg->ad_select);
  872. system_set_dev_sysfs("bonding/lacp_rate", ifname, cfg->lacp_rate);
  873. system_set_dev_sysfs_int("bonding/min_links", ifname, cfg->min_links);
  874. }
  875. if (cfg->policy == BONDING_MODE_BALANCE_RR)
  876. system_set_dev_sysfs_int("bonding/packets_per_slave", ifname, cfg->packets_per_port);
  877. if (cfg->policy == BONDING_MODE_BALANCE_TLB ||
  878. cfg->policy == BONDING_MODE_BALANCE_ALB)
  879. system_set_dev_sysfs_int("bonding/lp_interval", ifname, cfg->lp_interval);
  880. if (cfg->policy == BONDING_MODE_BALANCE_TLB)
  881. system_set_dev_sysfs_int("bonding/tlb_dynamic_lb", ifname, cfg->dynamic_lb);
  882. system_set_dev_sysfs_int("bonding/resend_igmp", ifname, cfg->resend_igmp);
  883. system_set_dev_sysfs_int("bonding/num_grat_arp", ifname, cfg->num_peer_notif);
  884. system_set_dev_sysfs("bonding/primary_reselect", ifname, cfg->primary_reselect);
  885. system_set_dev_sysfs("bonding/fail_over_mac", ifname, cfg->failover_mac);
  886. system_set_dev_sysfs_int((cfg->monitor_arp ?
  887. "bonding/arp_interval" :
  888. "bonding/miimon"), ifname, cfg->monitor_interval);
  889. blobmsg_for_each_attr(cur, cfg->arp_target, rem) {
  890. snprintf(buf, sizeof(buf), "+%s", blobmsg_get_string(cur));
  891. system_set_dev_sysfs("bonding/arp_ip_target", ifname, buf);
  892. }
  893. system_set_dev_sysfs_int("bonding/arp_all_targets", ifname, cfg->arp_all_targets);
  894. if (cfg->policy < BONDING_MODE_8023AD)
  895. system_set_dev_sysfs("bonding/arp_validate", ifname, cfg->arp_validate);
  896. system_set_dev_sysfs_int("bonding/use_carrier", ifname, cfg->use_carrier);
  897. if (!cfg->monitor_arp && cfg->monitor_interval) {
  898. system_set_dev_sysfs_int("bonding/updelay", ifname, cfg->updelay);
  899. system_set_dev_sysfs_int("bonding/downdelay", ifname, cfg->downdelay);
  900. }
  901. return 0;
  902. }
  903. int system_bonding_set_port(struct device *dev, struct device *port, bool add, bool primary)
  904. {
  905. const char *port_name = port->ifname;
  906. const char op_ch = add ? '+' : '-';
  907. char buf[IFNAMSIZ + 1];
  908. snprintf(buf, sizeof(buf), "%c%s", op_ch, port_name);
  909. system_if_down(port);
  910. system_set_dev_sysfs("bonding/slaves", dev->ifname, buf);
  911. system_if_up(port);
  912. if (primary)
  913. system_set_dev_sysfs("bonding/primary", dev->ifname,
  914. add ? port_name : "");
  915. return 0;
  916. }
  917. int system_if_resolve(struct device *dev)
  918. {
  919. struct ifreq ifr;
  920. memset(&ifr, 0, sizeof(ifr));
  921. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  922. if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
  923. return ifr.ifr_ifindex;
  924. else
  925. return 0;
  926. }
  927. static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
  928. {
  929. struct ifreq ifr;
  930. memset(&ifr, 0, sizeof(ifr));
  931. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  932. if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) < 0)
  933. return -1;
  934. ifr.ifr_flags |= add;
  935. ifr.ifr_flags &= ~rem;
  936. return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
  937. }
  938. struct clear_data {
  939. struct nl_msg *msg;
  940. struct device *dev;
  941. int type;
  942. int size;
  943. int af;
  944. };
  945. static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
  946. {
  947. struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
  948. return (long)ifa->ifa_index == ifindex;
  949. }
  950. static bool check_route(struct nlmsghdr *hdr, int ifindex)
  951. {
  952. struct rtmsg *r = NLMSG_DATA(hdr);
  953. struct nlattr *tb[__RTA_MAX];
  954. if (r->rtm_protocol == RTPROT_KERNEL &&
  955. r->rtm_family == AF_INET6)
  956. return false;
  957. nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
  958. if (!tb[RTA_OIF])
  959. return false;
  960. return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
  961. }
  962. static bool check_rule(struct nlmsghdr *hdr, int ifindex)
  963. {
  964. return true;
  965. }
  966. static int cb_clear_event(struct nl_msg *msg, void *arg)
  967. {
  968. struct clear_data *clr = arg;
  969. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  970. bool (*cb)(struct nlmsghdr *, int ifindex);
  971. int type, ret;
  972. switch(clr->type) {
  973. case RTM_GETADDR:
  974. type = RTM_DELADDR;
  975. if (hdr->nlmsg_type != RTM_NEWADDR)
  976. return NL_SKIP;
  977. cb = check_ifaddr;
  978. break;
  979. case RTM_GETROUTE:
  980. type = RTM_DELROUTE;
  981. if (hdr->nlmsg_type != RTM_NEWROUTE)
  982. return NL_SKIP;
  983. cb = check_route;
  984. break;
  985. case RTM_GETRULE:
  986. type = RTM_DELRULE;
  987. if (hdr->nlmsg_type != RTM_NEWRULE)
  988. return NL_SKIP;
  989. cb = check_rule;
  990. break;
  991. default:
  992. return NL_SKIP;
  993. }
  994. if (!cb(hdr, clr->dev ? clr->dev->ifindex : 0))
  995. return NL_SKIP;
  996. if (type == RTM_DELRULE)
  997. D(SYSTEM, "Remove a rule");
  998. else
  999. D(SYSTEM, "Remove %s from device %s",
  1000. type == RTM_DELADDR ? "an address" : "a route",
  1001. clr->dev->ifname);
  1002. memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
  1003. hdr = nlmsg_hdr(clr->msg);
  1004. hdr->nlmsg_type = type;
  1005. hdr->nlmsg_flags = NLM_F_REQUEST;
  1006. nl_socket_disable_auto_ack(sock_rtnl);
  1007. ret = nl_send_auto_complete(sock_rtnl, clr->msg);
  1008. if (ret < 0) {
  1009. if (type == RTM_DELRULE)
  1010. D(SYSTEM, "Error deleting a rule: %d", ret);
  1011. else
  1012. D(SYSTEM, "Error deleting %s from device '%s': %d",
  1013. type == RTM_DELADDR ? "an address" : "a route",
  1014. clr->dev->ifname, ret);
  1015. }
  1016. nl_socket_enable_auto_ack(sock_rtnl);
  1017. return NL_SKIP;
  1018. }
  1019. static int
  1020. cb_finish_event(struct nl_msg *msg, void *arg)
  1021. {
  1022. int *pending = arg;
  1023. *pending = 0;
  1024. return NL_STOP;
  1025. }
  1026. static int
  1027. error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
  1028. {
  1029. int *pending = arg;
  1030. *pending = err->error;
  1031. return NL_STOP;
  1032. }
  1033. static void
  1034. system_if_clear_entries(struct device *dev, int type, int af)
  1035. {
  1036. struct clear_data clr;
  1037. struct nl_cb *cb;
  1038. struct rtmsg rtm = {
  1039. .rtm_family = af,
  1040. .rtm_flags = RTM_F_CLONED,
  1041. };
  1042. int flags = NLM_F_DUMP;
  1043. int pending = 1;
  1044. clr.af = af;
  1045. clr.dev = dev;
  1046. clr.type = type;
  1047. switch (type) {
  1048. case RTM_GETADDR:
  1049. case RTM_GETRULE:
  1050. clr.size = sizeof(struct rtgenmsg);
  1051. break;
  1052. case RTM_GETROUTE:
  1053. clr.size = sizeof(struct rtmsg);
  1054. break;
  1055. default:
  1056. return;
  1057. }
  1058. cb = nl_cb_alloc(NL_CB_DEFAULT);
  1059. if (!cb)
  1060. return;
  1061. clr.msg = nlmsg_alloc_simple(type, flags);
  1062. if (!clr.msg)
  1063. goto out;
  1064. nlmsg_append(clr.msg, &rtm, clr.size, 0);
  1065. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
  1066. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
  1067. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
  1068. if (nl_send_auto_complete(sock_rtnl, clr.msg) < 0)
  1069. goto free;
  1070. while (pending > 0)
  1071. nl_recvmsgs(sock_rtnl, cb);
  1072. free:
  1073. nlmsg_free(clr.msg);
  1074. out:
  1075. nl_cb_put(cb);
  1076. }
  1077. /*
  1078. * Clear bridge (membership) state and bring down device
  1079. */
  1080. void system_if_clear_state(struct device *dev)
  1081. {
  1082. static char buf[256];
  1083. char *bridge;
  1084. device_set_ifindex(dev, system_if_resolve(dev));
  1085. if (dev->external || !dev->ifindex)
  1086. return;
  1087. system_if_flags(dev->ifname, 0, IFF_UP);
  1088. if (system_is_bridge(dev->ifname)) {
  1089. D(SYSTEM, "Delete existing bridge named '%s'", dev->ifname);
  1090. system_bridge_delbr(dev);
  1091. return;
  1092. }
  1093. bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
  1094. if (bridge) {
  1095. D(SYSTEM, "Remove device '%s' from bridge '%s'", dev->ifname, bridge);
  1096. system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
  1097. }
  1098. system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
  1099. system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
  1100. system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
  1101. system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
  1102. system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET);
  1103. system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET6);
  1104. system_set_disable_ipv6(dev, "0");
  1105. }
  1106. static inline unsigned long
  1107. sec_to_jiffies(int val)
  1108. {
  1109. return (unsigned long) val * 100;
  1110. }
  1111. int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
  1112. {
  1113. struct nlattr *linkinfo, *data;
  1114. struct nl_msg *msg;
  1115. uint64_t val;
  1116. int rv;
  1117. msg = system_ifinfo_msg(bridge->ifname, RTM_NEWLINK, NLM_F_CREATE | NLM_F_EXCL);
  1118. if (!msg)
  1119. return -1;
  1120. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1121. goto nla_put_failure;
  1122. nla_put_string(msg, IFLA_INFO_KIND, "bridge");
  1123. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1124. goto nla_put_failure;
  1125. nla_put_u32(msg, IFLA_BR_STP_STATE, cfg->stp);
  1126. nla_put_u32(msg, IFLA_BR_FORWARD_DELAY, sec_to_jiffies(cfg->forward_delay));
  1127. nla_put_u8(msg, IFLA_BR_MCAST_SNOOPING, !!cfg->igmp_snoop);
  1128. nla_put_u8(msg, IFLA_BR_MCAST_QUERIER, !!cfg->multicast_querier);
  1129. nla_put_u32(msg, IFLA_BR_MCAST_HASH_MAX, cfg->hash_max);
  1130. if (bridge->settings.flags & DEV_OPT_MULTICAST_ROUTER)
  1131. nla_put_u8(msg, IFLA_BR_MCAST_ROUTER, !!bridge->settings.multicast_router);
  1132. if (cfg->flags & BRIDGE_OPT_ROBUSTNESS) {
  1133. nla_put_u32(msg, IFLA_BR_MCAST_STARTUP_QUERY_CNT, cfg->robustness);
  1134. nla_put_u32(msg, IFLA_BR_MCAST_LAST_MEMBER_CNT, cfg->robustness);
  1135. }
  1136. if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL)
  1137. nla_put_u64(msg, IFLA_BR_MCAST_QUERY_INTVL, cfg->query_interval);
  1138. if (cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL)
  1139. nla_put_u64(msg, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, cfg->query_response_interval);
  1140. if (cfg->flags & BRIDGE_OPT_LAST_MEMBER_INTERVAL)
  1141. nla_put_u64(msg, IFLA_BR_MCAST_LAST_MEMBER_INTVL, cfg->last_member_interval);
  1142. if (cfg->flags & BRIDGE_OPT_ROBUSTNESS ||
  1143. cfg->flags & BRIDGE_OPT_QUERY_INTERVAL ||
  1144. cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL) {
  1145. val = cfg->robustness * cfg->query_interval +
  1146. cfg->query_response_interval;
  1147. nla_put_u64(msg, IFLA_BR_MCAST_MEMBERSHIP_INTVL, val);
  1148. val -= cfg->query_response_interval / 2;
  1149. nla_put_u64(msg, IFLA_BR_MCAST_QUERIER_INTVL, val);
  1150. }
  1151. if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL) {
  1152. val = cfg->query_interval / 4;
  1153. nla_put_u64(msg, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, val);
  1154. }
  1155. nla_put_u8(msg, IFLA_BR_VLAN_FILTERING, !!cfg->vlan_filtering);
  1156. nla_put_u16(msg, IFLA_BR_PRIORITY, cfg->priority);
  1157. nla_put_u32(msg, IFLA_BR_HELLO_TIME, sec_to_jiffies(cfg->hello_time));
  1158. nla_put_u32(msg, IFLA_BR_MAX_AGE, sec_to_jiffies(cfg->max_age));
  1159. if (cfg->flags & BRIDGE_OPT_AGEING_TIME)
  1160. nla_put_u32(msg, IFLA_BR_AGEING_TIME, sec_to_jiffies(cfg->ageing_time));
  1161. nla_nest_end(msg, data);
  1162. nla_nest_end(msg, linkinfo);
  1163. rv = system_rtnl_call(msg);
  1164. if (rv)
  1165. D(SYSTEM, "Error adding bridge '%s': %d", bridge->ifname, rv);
  1166. return rv;
  1167. nla_put_failure:
  1168. nlmsg_free(msg);
  1169. return -ENOMEM;
  1170. }
  1171. int system_macvlan_add(struct device *macvlan, struct device *dev, struct macvlan_config *cfg)
  1172. {
  1173. struct nl_msg *msg;
  1174. struct nlattr *linkinfo, *data;
  1175. size_t i;
  1176. int rv;
  1177. static const struct {
  1178. const char *name;
  1179. enum macvlan_mode val;
  1180. } modes[] = {
  1181. { "private", MACVLAN_MODE_PRIVATE },
  1182. { "vepa", MACVLAN_MODE_VEPA },
  1183. { "bridge", MACVLAN_MODE_BRIDGE },
  1184. { "passthru", MACVLAN_MODE_PASSTHRU },
  1185. };
  1186. msg = system_ifinfo_msg(macvlan->ifname, RTM_NEWLINK, NLM_F_CREATE | NLM_F_EXCL);
  1187. if (!msg)
  1188. return -1;
  1189. if (cfg->flags & MACVLAN_OPT_MACADDR)
  1190. nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
  1191. nla_put_u32(msg, IFLA_LINK, dev->ifindex);
  1192. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1193. goto nla_put_failure;
  1194. nla_put_string(msg, IFLA_INFO_KIND, "macvlan");
  1195. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1196. goto nla_put_failure;
  1197. if (cfg->mode) {
  1198. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  1199. if (strcmp(cfg->mode, modes[i].name) != 0)
  1200. continue;
  1201. nla_put_u32(msg, IFLA_MACVLAN_MODE, modes[i].val);
  1202. break;
  1203. }
  1204. }
  1205. nla_nest_end(msg, data);
  1206. nla_nest_end(msg, linkinfo);
  1207. rv = system_rtnl_call(msg);
  1208. if (rv)
  1209. D(SYSTEM, "Error adding macvlan '%s' over '%s': %d", macvlan->ifname, dev->ifname, rv);
  1210. return rv;
  1211. nla_put_failure:
  1212. nlmsg_free(msg);
  1213. return -ENOMEM;
  1214. }
  1215. int system_link_netns_move(struct device *dev, int netns_fd, const char *target_ifname)
  1216. {
  1217. struct nl_msg *msg;
  1218. int index;
  1219. if (!dev)
  1220. return -1;
  1221. index = system_if_resolve(dev);
  1222. msg = __system_ifinfo_msg(AF_UNSPEC, index, target_ifname, RTM_NEWLINK, 0);
  1223. if (!msg)
  1224. return -1;
  1225. nla_put_u32(msg, IFLA_NET_NS_FD, netns_fd);
  1226. return system_rtnl_call(msg);
  1227. }
  1228. int system_macvlan_del(struct device *macvlan)
  1229. {
  1230. return system_link_del(macvlan->ifname);
  1231. }
  1232. int system_netns_open(const pid_t target_ns)
  1233. {
  1234. char pid_net_path[PATH_MAX];
  1235. snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
  1236. return open(pid_net_path, O_RDONLY);
  1237. }
  1238. int system_netns_set(int netns_fd)
  1239. {
  1240. return setns(netns_fd, CLONE_NEWNET);
  1241. }
  1242. int system_veth_add(struct device *veth, struct veth_config *cfg)
  1243. {
  1244. struct nl_msg *msg;
  1245. struct ifinfomsg empty_iim = {0,};
  1246. struct nlattr *linkinfo, *data, *veth_info;
  1247. int rv;
  1248. msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
  1249. if (!msg)
  1250. return -1;
  1251. nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
  1252. if (cfg->flags & VETH_OPT_MACADDR)
  1253. nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
  1254. nla_put_string(msg, IFLA_IFNAME, veth->ifname);
  1255. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1256. goto nla_put_failure;
  1257. nla_put_string(msg, IFLA_INFO_KIND, "veth");
  1258. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1259. goto nla_put_failure;
  1260. if (!(veth_info = nla_nest_start(msg, VETH_INFO_PEER)))
  1261. goto nla_put_failure;
  1262. nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
  1263. if (cfg->flags & VETH_OPT_PEER_NAME)
  1264. nla_put_string(msg, IFLA_IFNAME, cfg->peer_name);
  1265. if (cfg->flags & VETH_OPT_PEER_MACADDR)
  1266. nla_put(msg, IFLA_ADDRESS, sizeof(cfg->peer_macaddr), cfg->peer_macaddr);
  1267. nla_nest_end(msg, veth_info);
  1268. nla_nest_end(msg, data);
  1269. nla_nest_end(msg, linkinfo);
  1270. rv = system_rtnl_call(msg);
  1271. if (rv) {
  1272. if (cfg->flags & VETH_OPT_PEER_NAME)
  1273. D(SYSTEM, "Error adding veth '%s' with peer '%s': %d", veth->ifname, cfg->peer_name, rv);
  1274. else
  1275. D(SYSTEM, "Error adding veth '%s': %d", veth->ifname, rv);
  1276. }
  1277. return rv;
  1278. nla_put_failure:
  1279. nlmsg_free(msg);
  1280. return -ENOMEM;
  1281. }
  1282. int system_veth_del(struct device *veth)
  1283. {
  1284. return system_link_del(veth->ifname);
  1285. }
  1286. static int system_vlan(struct device *dev, int id)
  1287. {
  1288. struct vlan_ioctl_args ifr = {
  1289. .cmd = SET_VLAN_NAME_TYPE_CMD,
  1290. .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
  1291. };
  1292. if (ioctl(sock_ioctl, SIOCSIFVLAN, &ifr) < 0)
  1293. return -1;
  1294. if (id < 0) {
  1295. ifr.cmd = DEL_VLAN_CMD;
  1296. ifr.u.VID = 0;
  1297. } else {
  1298. ifr.cmd = ADD_VLAN_CMD;
  1299. ifr.u.VID = id;
  1300. }
  1301. strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
  1302. return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
  1303. }
  1304. int system_vlan_add(struct device *dev, int id)
  1305. {
  1306. return system_vlan(dev, id);
  1307. }
  1308. int system_vlan_del(struct device *dev)
  1309. {
  1310. return system_vlan(dev, -1);
  1311. }
  1312. int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg)
  1313. {
  1314. struct nl_msg *msg;
  1315. struct nlattr *linkinfo, *data, *qos;
  1316. struct ifinfomsg iim = { .ifi_family = AF_UNSPEC };
  1317. struct vlan_qos_mapping *elem;
  1318. struct ifla_vlan_qos_mapping nl_qos_map;
  1319. int rv;
  1320. msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
  1321. if (!msg)
  1322. return -1;
  1323. nlmsg_append(msg, &iim, sizeof(iim), 0);
  1324. nla_put_string(msg, IFLA_IFNAME, vlandev->ifname);
  1325. nla_put_u32(msg, IFLA_LINK, dev->ifindex);
  1326. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
  1327. goto nla_put_failure;
  1328. nla_put_string(msg, IFLA_INFO_KIND, "vlan");
  1329. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
  1330. goto nla_put_failure;
  1331. nla_put_u16(msg, IFLA_VLAN_ID, cfg->vid);
  1332. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
  1333. nla_put_u16(msg, IFLA_VLAN_PROTOCOL, htons(cfg->proto));
  1334. #else
  1335. if(cfg->proto == VLAN_PROTO_8021AD)
  1336. netifd_log_message(L_WARNING, "%s Your kernel is older than linux 3.10.0, 802.1ad is not supported defaulting to 802.1q", vlandev->type->name);
  1337. #endif
  1338. if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS)))
  1339. goto nla_put_failure;
  1340. vlist_simple_for_each_element(&cfg->ingress_qos_mapping_list, elem, node) {
  1341. nl_qos_map.from = elem->from;
  1342. nl_qos_map.to = elem->to;
  1343. nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
  1344. }
  1345. nla_nest_end(msg, qos);
  1346. if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS)))
  1347. goto nla_put_failure;
  1348. vlist_simple_for_each_element(&cfg->egress_qos_mapping_list, elem, node) {
  1349. nl_qos_map.from = elem->from;
  1350. nl_qos_map.to = elem->to;
  1351. nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
  1352. }
  1353. nla_nest_end(msg, qos);
  1354. nla_nest_end(msg, data);
  1355. nla_nest_end(msg, linkinfo);
  1356. rv = system_rtnl_call(msg);
  1357. if (rv)
  1358. D(SYSTEM, "Error adding vlandev '%s' over '%s': %d", vlandev->ifname, dev->ifname, rv);
  1359. return rv;
  1360. nla_put_failure:
  1361. nlmsg_free(msg);
  1362. return -ENOMEM;
  1363. }
  1364. int system_vlandev_del(struct device *vlandev)
  1365. {
  1366. return system_link_del(vlandev->ifname);
  1367. }
  1368. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
  1369. struct if_get_master_data {
  1370. int ifindex;
  1371. int master_ifindex;
  1372. int pending;
  1373. };
  1374. static void if_get_master_dsa_linkinfo_attr(struct if_get_master_data *data,
  1375. struct rtattr *attr)
  1376. {
  1377. struct rtattr *cur;
  1378. int rem = RTA_PAYLOAD(attr);
  1379. for (cur = RTA_DATA(attr); RTA_OK(cur, rem); cur = RTA_NEXT(cur, rem)) {
  1380. if (cur->rta_type != IFLA_DSA_MASTER)
  1381. continue;
  1382. data->master_ifindex = *(__u32 *)RTA_DATA(cur);
  1383. }
  1384. }
  1385. static void if_get_master_linkinfo_attr(struct if_get_master_data *data,
  1386. struct rtattr *attr)
  1387. {
  1388. struct rtattr *cur;
  1389. int rem = RTA_PAYLOAD(attr);
  1390. for (cur = RTA_DATA(attr); RTA_OK(cur, rem); cur = RTA_NEXT(cur, rem)) {
  1391. if (cur->rta_type != IFLA_INFO_KIND && cur->rta_type != IFLA_INFO_DATA)
  1392. continue;
  1393. if (cur->rta_type == IFLA_INFO_KIND && strcmp("dsa", (char *)RTA_DATA(cur)))
  1394. break;
  1395. if (cur->rta_type == IFLA_INFO_DATA)
  1396. if_get_master_dsa_linkinfo_attr(data, cur);
  1397. }
  1398. }
  1399. static int cb_if_get_master_valid(struct nl_msg *msg, void *arg)
  1400. {
  1401. struct nlmsghdr *nh = nlmsg_hdr(msg);
  1402. struct ifinfomsg *ifi = NLMSG_DATA(nh);
  1403. struct if_get_master_data *data = (struct if_get_master_data *)arg;
  1404. struct rtattr *attr;
  1405. int rem;
  1406. if (nh->nlmsg_type != RTM_NEWLINK)
  1407. return NL_SKIP;
  1408. if (ifi->ifi_family != AF_UNSPEC)
  1409. return NL_SKIP;
  1410. if (ifi->ifi_index != data->ifindex)
  1411. return NL_SKIP;
  1412. attr = IFLA_RTA(ifi);
  1413. rem = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
  1414. while (RTA_OK(attr, rem)) {
  1415. if (attr->rta_type == IFLA_LINKINFO)
  1416. if_get_master_linkinfo_attr(data, attr);
  1417. attr = RTA_NEXT(attr, rem);
  1418. }
  1419. return NL_OK;
  1420. }
  1421. static int cb_if_get_master_ack(struct nl_msg *msg, void *arg)
  1422. {
  1423. struct if_get_master_data *data = (struct if_get_master_data *)arg;
  1424. data->pending = 0;
  1425. return NL_STOP;
  1426. }
  1427. static int cb_if_get_master_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
  1428. {
  1429. struct if_get_master_data *data = (struct if_get_master_data *)arg;
  1430. data->pending = 0;
  1431. return NL_STOP;
  1432. }
  1433. static int system_if_get_master_ifindex(struct device *dev)
  1434. {
  1435. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  1436. struct nl_msg *msg;
  1437. struct ifinfomsg ifi = {
  1438. .ifi_family = AF_UNSPEC,
  1439. .ifi_index = 0,
  1440. };
  1441. struct if_get_master_data data = {
  1442. .ifindex = if_nametoindex(dev->ifname),
  1443. .master_ifindex = -1,
  1444. .pending = 1,
  1445. };
  1446. int ret = -1;
  1447. if (!cb)
  1448. return ret;
  1449. msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_REQUEST);
  1450. if (!msg)
  1451. goto out;
  1452. if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
  1453. nla_put_string(msg, IFLA_IFNAME, dev->ifname))
  1454. goto free;
  1455. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_if_get_master_valid, &data);
  1456. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_if_get_master_ack, &data);
  1457. nl_cb_err(cb, NL_CB_CUSTOM, cb_if_get_master_error, &data);
  1458. ret = nl_send_auto_complete(sock_rtnl, msg);
  1459. if (ret < 0)
  1460. goto free;
  1461. while (data.pending > 0)
  1462. nl_recvmsgs(sock_rtnl, cb);
  1463. if (data.master_ifindex >= 0)
  1464. ret = data.master_ifindex;
  1465. free:
  1466. nlmsg_free(msg);
  1467. out:
  1468. nl_cb_put(cb);
  1469. return ret;
  1470. }
  1471. static void system_refresh_orig_macaddr(struct device *dev, struct device_settings *s)
  1472. {
  1473. struct ifreq ifr;
  1474. memset(&ifr, 0, sizeof(ifr));
  1475. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1476. if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0)
  1477. memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
  1478. }
  1479. static void system_set_master(struct device *dev, int master_ifindex)
  1480. {
  1481. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
  1482. struct nl_msg *nlm;
  1483. nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST);
  1484. if (!nlm)
  1485. return;
  1486. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  1487. nla_put_string(nlm, IFLA_IFNAME, dev->ifname);
  1488. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  1489. if (!linkinfo)
  1490. goto failure;
  1491. nla_put_string(nlm, IFLA_INFO_KIND, "dsa");
  1492. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  1493. if (!infodata)
  1494. goto failure;
  1495. nla_put_u32(nlm, IFLA_DSA_MASTER, master_ifindex);
  1496. nla_nest_end(nlm, infodata);
  1497. nla_nest_end(nlm, linkinfo);
  1498. system_rtnl_call(nlm);
  1499. return;
  1500. failure:
  1501. nlmsg_free(nlm);
  1502. }
  1503. #endif
  1504. static void ethtool_link_mode_clear_bit(__s8 nwords, int nr, __u32 *mask)
  1505. {
  1506. if (nr < 0)
  1507. return;
  1508. if (nr >= (nwords * 32))
  1509. return;
  1510. mask[nr / 32] &= ~(1U << (nr % 32));
  1511. }
  1512. static bool ethtool_link_mode_test_bit(__s8 nwords, int nr, const __u32 *mask)
  1513. {
  1514. if (nr < 0)
  1515. return false;
  1516. if (nr >= (nwords * 32))
  1517. return false;
  1518. return !!(mask[nr / 32] & (1U << (nr % 32)));
  1519. }
  1520. static int
  1521. system_get_ethtool_gro(struct device *dev)
  1522. {
  1523. struct ethtool_value ecmd;
  1524. struct ifreq ifr = {
  1525. .ifr_data = (caddr_t)&ecmd,
  1526. };
  1527. memset(&ecmd, 0, sizeof(ecmd));
  1528. ecmd.cmd = ETHTOOL_GGRO;
  1529. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1530. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr))
  1531. return -1;
  1532. return ecmd.data;
  1533. }
  1534. static void
  1535. system_set_ethtool_gro(struct device *dev, struct device_settings *s)
  1536. {
  1537. struct ethtool_value ecmd;
  1538. struct ifreq ifr = {
  1539. .ifr_data = (caddr_t)&ecmd,
  1540. };
  1541. memset(&ecmd, 0, sizeof(ecmd));
  1542. ecmd.cmd = ETHTOOL_SGRO;
  1543. ecmd.data = s->gro;
  1544. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1545. ioctl(sock_ioctl, SIOCETHTOOL, &ifr);
  1546. }
  1547. static void
  1548. system_set_ethtool_pause(struct device *dev, struct device_settings *s)
  1549. {
  1550. struct ethtool_pauseparam pp;
  1551. struct ifreq ifr = {
  1552. .ifr_data = (caddr_t)&pp,
  1553. };
  1554. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1555. memset(&pp, 0, sizeof(pp));
  1556. pp.cmd = ETHTOOL_GPAUSEPARAM;
  1557. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr))
  1558. return;
  1559. if (s->flags & DEV_OPT_RXPAUSE || s->flags & DEV_OPT_TXPAUSE) {
  1560. pp.autoneg = AUTONEG_DISABLE;
  1561. if (s->flags & DEV_OPT_PAUSE) {
  1562. if (s->flags & DEV_OPT_RXPAUSE)
  1563. pp.rx_pause = s->rxpause && s->pause;
  1564. else
  1565. pp.rx_pause = s->pause;
  1566. if (s->flags & DEV_OPT_TXPAUSE)
  1567. pp.tx_pause = s->txpause && s->pause;
  1568. else
  1569. pp.tx_pause = s->pause;
  1570. } else {
  1571. if (s->flags & DEV_OPT_RXPAUSE)
  1572. pp.rx_pause = s->rxpause;
  1573. if (s->flags & DEV_OPT_TXPAUSE)
  1574. pp.tx_pause = s->txpause;
  1575. }
  1576. if (s->flags & DEV_OPT_ASYM_PAUSE &&
  1577. !s->asym_pause && (pp.rx_pause != pp.tx_pause))
  1578. pp.rx_pause = pp.tx_pause = false;
  1579. } else {
  1580. pp.autoneg = AUTONEG_ENABLE;
  1581. /* Pause and Asym_Pause advertising bits will be set via
  1582. * ETHTOOL_SLINKSETTINGS in system_set_ethtool_settings()
  1583. */
  1584. }
  1585. pp.cmd = ETHTOOL_SPAUSEPARAM;
  1586. ioctl(sock_ioctl, SIOCETHTOOL, &ifr);
  1587. }
  1588. static void
  1589. system_set_ethtool_eee_settings(struct device *dev, struct device_settings *s)
  1590. {
  1591. struct ethtool_eee eeecmd;
  1592. struct ifreq ifr = {
  1593. .ifr_data = (caddr_t)&eeecmd,
  1594. };
  1595. memset(&eeecmd, 0, sizeof(eeecmd));
  1596. eeecmd.cmd = ETHTOOL_SEEE;
  1597. eeecmd.eee_enabled = s->eee;
  1598. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1599. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0)
  1600. netifd_log_message(L_WARNING, "cannot set eee %d for device %s", s->eee, dev->ifname);
  1601. }
  1602. static void
  1603. system_set_ethtool_settings(struct device *dev, struct device_settings *s)
  1604. {
  1605. struct {
  1606. struct ethtool_link_settings req;
  1607. __u32 link_mode_data[3 * 127];
  1608. } ecmd;
  1609. struct ifreq ifr = {
  1610. .ifr_data = (caddr_t)&ecmd,
  1611. };
  1612. size_t i;
  1613. __s8 nwords;
  1614. __u32 *supported, *advertising;
  1615. system_set_ethtool_pause(dev, s);
  1616. if (s->flags & DEV_OPT_EEE)
  1617. system_set_ethtool_eee_settings(dev, s);
  1618. memset(&ecmd, 0, sizeof(ecmd));
  1619. ecmd.req.cmd = ETHTOOL_GLINKSETTINGS;
  1620. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1621. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) < 0 ||
  1622. ecmd.req.link_mode_masks_nwords >= 0 ||
  1623. ecmd.req.cmd != ETHTOOL_GLINKSETTINGS)
  1624. return;
  1625. ecmd.req.link_mode_masks_nwords = -ecmd.req.link_mode_masks_nwords;
  1626. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) < 0 ||
  1627. ecmd.req.link_mode_masks_nwords <= 0 ||
  1628. ecmd.req.cmd != ETHTOOL_GLINKSETTINGS)
  1629. return;
  1630. nwords = ecmd.req.link_mode_masks_nwords;
  1631. supported = &ecmd.link_mode_data[0];
  1632. advertising = &ecmd.link_mode_data[nwords];
  1633. memcpy(advertising, supported, sizeof(__u32) * nwords);
  1634. for (i = 0; i < ARRAY_SIZE(ethtool_modes); i++) {
  1635. if (s->flags & DEV_OPT_DUPLEX) {
  1636. if (s->duplex)
  1637. ethtool_link_mode_clear_bit(nwords, ethtool_modes[i].bit_half, advertising);
  1638. else
  1639. ethtool_link_mode_clear_bit(nwords, ethtool_modes[i].bit_full, advertising);
  1640. }
  1641. if (!(s->flags & DEV_OPT_SPEED) ||
  1642. s->speed == ethtool_modes[i].speed)
  1643. continue;
  1644. ethtool_link_mode_clear_bit(nwords, ethtool_modes[i].bit_full, advertising);
  1645. ethtool_link_mode_clear_bit(nwords, ethtool_modes[i].bit_half, advertising);
  1646. }
  1647. if (s->flags & DEV_OPT_PAUSE)
  1648. if (!s->pause)
  1649. ethtool_link_mode_clear_bit(nwords, ETHTOOL_LINK_MODE_Pause_BIT, advertising);
  1650. if (s->flags & DEV_OPT_ASYM_PAUSE)
  1651. if (!s->asym_pause)
  1652. ethtool_link_mode_clear_bit(nwords, ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising);
  1653. if (s->flags & DEV_OPT_AUTONEG) {
  1654. ecmd.req.autoneg = s->autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  1655. if (!s->autoneg) {
  1656. if (s->flags & DEV_OPT_SPEED)
  1657. ecmd.req.speed = s->speed;
  1658. if (s->flags & DEV_OPT_DUPLEX)
  1659. ecmd.req.duplex = s->duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1660. }
  1661. }
  1662. ecmd.req.cmd = ETHTOOL_SLINKSETTINGS;
  1663. ioctl(sock_ioctl, SIOCETHTOOL, &ifr);
  1664. }
  1665. static void
  1666. system_set_ethtool_settings_after_up(struct device *dev, struct device_settings *s)
  1667. {
  1668. if (s->flags & DEV_OPT_GRO)
  1669. system_set_ethtool_gro(dev, s);
  1670. }
  1671. void
  1672. system_if_get_settings(struct device *dev, struct device_settings *s)
  1673. {
  1674. struct ifreq ifr;
  1675. char buf[10];
  1676. int ret;
  1677. memset(&ifr, 0, sizeof(ifr));
  1678. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1679. if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
  1680. s->mtu = ifr.ifr_mtu;
  1681. s->flags |= DEV_OPT_MTU;
  1682. }
  1683. s->mtu6 = system_update_ipv6_mtu(dev, 0);
  1684. if (s->mtu6 > 0)
  1685. s->flags |= DEV_OPT_MTU6;
  1686. if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
  1687. s->txqueuelen = ifr.ifr_qlen;
  1688. s->flags |= DEV_OPT_TXQUEUELEN;
  1689. }
  1690. if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
  1691. memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
  1692. s->flags |= DEV_OPT_MACADDR;
  1693. }
  1694. if (!system_get_disable_ipv6(dev, buf, sizeof(buf))) {
  1695. s->ipv6 = !strtoul(buf, NULL, 0);
  1696. s->flags |= DEV_OPT_IPV6;
  1697. }
  1698. if (!system_get_ip6segmentrouting(dev, buf, sizeof(buf))) {
  1699. s->ip6segmentrouting = strtoul(buf, NULL, 0);
  1700. s->flags |= DEV_OPT_IP6SEGMENTROUTING;
  1701. }
  1702. if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) == 0) {
  1703. s->promisc = ifr.ifr_flags & IFF_PROMISC;
  1704. s->flags |= DEV_OPT_PROMISC;
  1705. s->multicast = ifr.ifr_flags & IFF_MULTICAST;
  1706. s->flags |= DEV_OPT_MULTICAST;
  1707. }
  1708. if (!system_get_rpfilter(dev, buf, sizeof(buf))) {
  1709. s->rpfilter = strtoul(buf, NULL, 0);
  1710. s->flags |= DEV_OPT_RPFILTER;
  1711. }
  1712. if (!system_get_acceptlocal(dev, buf, sizeof(buf))) {
  1713. s->acceptlocal = strtoul(buf, NULL, 0);
  1714. s->flags |= DEV_OPT_ACCEPTLOCAL;
  1715. }
  1716. if (!system_get_igmpversion(dev, buf, sizeof(buf))) {
  1717. s->igmpversion = strtoul(buf, NULL, 0);
  1718. s->flags |= DEV_OPT_IGMPVERSION;
  1719. }
  1720. if (!system_get_mldversion(dev, buf, sizeof(buf))) {
  1721. s->mldversion = strtoul(buf, NULL, 0);
  1722. s->flags |= DEV_OPT_MLDVERSION;
  1723. }
  1724. if (!system_get_neigh4reachabletime(dev, buf, sizeof(buf))) {
  1725. s->neigh4reachabletime = strtoul(buf, NULL, 0);
  1726. s->flags |= DEV_OPT_NEIGHREACHABLETIME;
  1727. }
  1728. if (!system_get_neigh6reachabletime(dev, buf, sizeof(buf))) {
  1729. s->neigh6reachabletime = strtoul(buf, NULL, 0);
  1730. s->flags |= DEV_OPT_NEIGHREACHABLETIME;
  1731. }
  1732. if (!system_get_neigh4locktime(dev, buf, sizeof(buf))) {
  1733. s->neigh4locktime = strtol(buf, NULL, 0);
  1734. s->flags |= DEV_OPT_NEIGHLOCKTIME;
  1735. }
  1736. if (!system_get_neigh4gcstaletime(dev, buf, sizeof(buf))) {
  1737. s->neigh4gcstaletime = strtoul(buf, NULL, 0);
  1738. s->flags |= DEV_OPT_NEIGHGCSTALETIME;
  1739. }
  1740. if (!system_get_neigh6gcstaletime(dev, buf, sizeof(buf))) {
  1741. s->neigh6gcstaletime = strtoul(buf, NULL, 0);
  1742. s->flags |= DEV_OPT_NEIGHGCSTALETIME;
  1743. }
  1744. if (!system_get_dadtransmits(dev, buf, sizeof(buf))) {
  1745. s->dadtransmits = strtoul(buf, NULL, 0);
  1746. s->flags |= DEV_OPT_DADTRANSMITS;
  1747. }
  1748. if (!system_get_sendredirects(dev, buf, sizeof(buf))) {
  1749. s->sendredirects = strtoul(buf, NULL, 0);
  1750. s->flags |= DEV_OPT_SENDREDIRECTS;
  1751. }
  1752. if (!system_get_drop_v4_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
  1753. s->drop_v4_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
  1754. s->flags |= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST;
  1755. }
  1756. if (!system_get_drop_v6_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
  1757. s->drop_v6_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
  1758. s->flags |= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST;
  1759. }
  1760. if (!system_get_drop_gratuitous_arp(dev, buf, sizeof(buf))) {
  1761. s->drop_gratuitous_arp = strtoul(buf, NULL, 0);
  1762. s->flags |= DEV_OPT_DROP_GRATUITOUS_ARP;
  1763. }
  1764. if (!system_get_drop_unsolicited_na(dev, buf, sizeof(buf))) {
  1765. s->drop_unsolicited_na = strtoul(buf, NULL, 0);
  1766. s->flags |= DEV_OPT_DROP_UNSOLICITED_NA;
  1767. }
  1768. if (!system_get_arp_accept(dev, buf, sizeof(buf))) {
  1769. s->arp_accept = strtoul(buf, NULL, 0);
  1770. s->flags |= DEV_OPT_ARP_ACCEPT;
  1771. }
  1772. ret = system_get_ethtool_gro(dev);
  1773. if (ret >= 0) {
  1774. s->gro = ret;
  1775. s->flags |= DEV_OPT_GRO;
  1776. }
  1777. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
  1778. ret = system_if_get_master_ifindex(dev);
  1779. if (ret >= 0) {
  1780. s->master_ifindex = ret;
  1781. s->flags |= DEV_OPT_MASTER;
  1782. }
  1783. #endif
  1784. }
  1785. void
  1786. system_if_apply_settings(struct device *dev, struct device_settings *s, uint64_t apply_mask)
  1787. {
  1788. struct ifreq ifr;
  1789. char buf[12];
  1790. apply_mask &= s->flags;
  1791. if (apply_mask & DEV_OPT_MASTER) {
  1792. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
  1793. system_set_master(dev, s->master_ifindex);
  1794. if (!(apply_mask & (DEV_OPT_MACADDR | DEV_OPT_DEFAULT_MACADDR)) || dev->external)
  1795. system_refresh_orig_macaddr(dev, &dev->orig_settings);
  1796. #else
  1797. netifd_log_message(L_WARNING, "%s Your kernel is older than linux 6.1.0, changing DSA port conduit is not supported!", dev->ifname);
  1798. #endif
  1799. }
  1800. memset(&ifr, 0, sizeof(ifr));
  1801. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  1802. if (apply_mask & DEV_OPT_MTU) {
  1803. ifr.ifr_mtu = s->mtu;
  1804. if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
  1805. s->flags &= ~DEV_OPT_MTU;
  1806. }
  1807. if (apply_mask & DEV_OPT_MTU6) {
  1808. system_update_ipv6_mtu(dev, s->mtu6);
  1809. }
  1810. if (apply_mask & DEV_OPT_TXQUEUELEN) {
  1811. ifr.ifr_qlen = s->txqueuelen;
  1812. if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
  1813. s->flags &= ~DEV_OPT_TXQUEUELEN;
  1814. }
  1815. if ((apply_mask & (DEV_OPT_MACADDR | DEV_OPT_DEFAULT_MACADDR)) && !dev->external) {
  1816. ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  1817. memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
  1818. if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
  1819. s->flags &= ~DEV_OPT_MACADDR;
  1820. }
  1821. if (apply_mask & DEV_OPT_IPV6)
  1822. system_set_disable_ipv6(dev, s->ipv6 ? "0" : "1");
  1823. if (s->flags & DEV_OPT_IP6SEGMENTROUTING & apply_mask) {
  1824. struct device dummy = {
  1825. .ifname = "all",
  1826. };
  1827. bool ip6segmentrouting = device_check_ip6segmentrouting();
  1828. system_set_ip6segmentrouting(dev, s->ip6segmentrouting ? "1" : "0");
  1829. system_set_ip6segmentrouting(&dummy, ip6segmentrouting ? "1" : "0");
  1830. }
  1831. if (apply_mask & DEV_OPT_PROMISC) {
  1832. if (system_if_flags(dev->ifname, s->promisc ? IFF_PROMISC : 0,
  1833. !s->promisc ? IFF_PROMISC : 0) < 0)
  1834. s->flags &= ~DEV_OPT_PROMISC;
  1835. }
  1836. if (apply_mask & DEV_OPT_RPFILTER) {
  1837. snprintf(buf, sizeof(buf), "%u", s->rpfilter);
  1838. system_set_rpfilter(dev, buf);
  1839. }
  1840. if (apply_mask & DEV_OPT_ACCEPTLOCAL)
  1841. system_set_acceptlocal(dev, s->acceptlocal ? "1" : "0");
  1842. if (apply_mask & DEV_OPT_IGMPVERSION) {
  1843. snprintf(buf, sizeof(buf), "%u", s->igmpversion);
  1844. system_set_igmpversion(dev, buf);
  1845. }
  1846. if (apply_mask & DEV_OPT_MLDVERSION) {
  1847. snprintf(buf, sizeof(buf), "%u", s->mldversion);
  1848. system_set_mldversion(dev, buf);
  1849. }
  1850. if (apply_mask & DEV_OPT_NEIGHREACHABLETIME) {
  1851. snprintf(buf, sizeof(buf), "%u", s->neigh4reachabletime);
  1852. system_set_neigh4reachabletime(dev, buf);
  1853. snprintf(buf, sizeof(buf), "%u", s->neigh6reachabletime);
  1854. system_set_neigh6reachabletime(dev, buf);
  1855. }
  1856. if (apply_mask & DEV_OPT_NEIGHLOCKTIME) {
  1857. snprintf(buf, sizeof(buf), "%d", s->neigh4locktime);
  1858. system_set_neigh4locktime(dev, buf);
  1859. }
  1860. if (apply_mask & DEV_OPT_NEIGHGCSTALETIME) {
  1861. snprintf(buf, sizeof(buf), "%u", s->neigh4gcstaletime);
  1862. system_set_neigh4gcstaletime(dev, buf);
  1863. snprintf(buf, sizeof(buf), "%u", s->neigh6gcstaletime);
  1864. system_set_neigh6gcstaletime(dev, buf);
  1865. }
  1866. if (apply_mask & DEV_OPT_DADTRANSMITS) {
  1867. snprintf(buf, sizeof(buf), "%u", s->dadtransmits);
  1868. system_set_dadtransmits(dev, buf);
  1869. }
  1870. if (apply_mask & DEV_OPT_MULTICAST) {
  1871. if (system_if_flags(dev->ifname, s->multicast ? IFF_MULTICAST : 0,
  1872. !s->multicast ? IFF_MULTICAST : 0) < 0)
  1873. s->flags &= ~DEV_OPT_MULTICAST;
  1874. }
  1875. if (apply_mask & DEV_OPT_SENDREDIRECTS)
  1876. system_set_sendredirects(dev, s->sendredirects ? "1" : "0");
  1877. if (apply_mask & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST)
  1878. system_set_drop_v4_unicast_in_l2_multicast(dev, s->drop_v4_unicast_in_l2_multicast ? "1" : "0");
  1879. if (apply_mask & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST)
  1880. system_set_drop_v6_unicast_in_l2_multicast(dev, s->drop_v6_unicast_in_l2_multicast ? "1" : "0");
  1881. if (apply_mask & DEV_OPT_DROP_GRATUITOUS_ARP)
  1882. system_set_drop_gratuitous_arp(dev, s->drop_gratuitous_arp ? "1" : "0");
  1883. if (apply_mask & DEV_OPT_DROP_UNSOLICITED_NA)
  1884. system_set_drop_unsolicited_na(dev, s->drop_unsolicited_na ? "1" : "0");
  1885. if (apply_mask & DEV_OPT_ARP_ACCEPT)
  1886. system_set_arp_accept(dev, s->arp_accept ? "1" : "0");
  1887. system_set_ethtool_settings(dev, s);
  1888. }
  1889. void system_if_apply_settings_after_up(struct device *dev, struct device_settings *s)
  1890. {
  1891. system_set_ethtool_settings_after_up(dev, s);
  1892. }
  1893. int system_if_up(struct device *dev)
  1894. {
  1895. return system_if_flags(dev->ifname, IFF_UP, 0);
  1896. }
  1897. int system_if_down(struct device *dev)
  1898. {
  1899. return system_if_flags(dev->ifname, 0, IFF_UP);
  1900. }
  1901. struct if_check_data {
  1902. struct device *dev;
  1903. int pending;
  1904. int ret;
  1905. };
  1906. static int cb_if_check_valid(struct nl_msg *msg, void *arg)
  1907. {
  1908. struct nlmsghdr *nh = nlmsg_hdr(msg);
  1909. struct ifinfomsg *ifi = NLMSG_DATA(nh);
  1910. struct if_check_data *chk = (struct if_check_data *)arg;
  1911. if (nh->nlmsg_type != RTM_NEWLINK)
  1912. return NL_SKIP;
  1913. system_device_update_state(chk->dev, ifi->ifi_flags, ifi->ifi_index);
  1914. return NL_OK;
  1915. }
  1916. static int cb_if_check_ack(struct nl_msg *msg, void *arg)
  1917. {
  1918. struct if_check_data *chk = (struct if_check_data *)arg;
  1919. chk->pending = 0;
  1920. return NL_STOP;
  1921. }
  1922. static int cb_if_check_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
  1923. {
  1924. struct if_check_data *chk = (struct if_check_data *)arg;
  1925. if (chk->dev->type == &simple_device_type)
  1926. device_set_present(chk->dev, false);
  1927. device_set_link(chk->dev, false);
  1928. chk->pending = err->error;
  1929. return NL_STOP;
  1930. }
  1931. struct bridge_vlan_check_data {
  1932. struct device *check_dev;
  1933. int ifindex;
  1934. int ret;
  1935. bool pending;
  1936. };
  1937. static void bridge_vlan_check_port(struct bridge_vlan_check_data *data,
  1938. struct bridge_vlan_port *port,
  1939. struct bridge_vlan_info *vinfo)
  1940. {
  1941. uint16_t flags = 0, diff, mask;
  1942. if (port->flags & BRVLAN_F_PVID)
  1943. flags |= BRIDGE_VLAN_INFO_PVID;
  1944. if (port->flags & BRVLAN_F_UNTAGGED)
  1945. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  1946. diff = vinfo->flags ^ flags;
  1947. mask = BRVLAN_F_UNTAGGED | (flags & BRIDGE_VLAN_INFO_PVID);
  1948. if (diff & mask) {
  1949. data->ret = 1;
  1950. data->pending = false;
  1951. }
  1952. port->check = 1;
  1953. }
  1954. static void bridge_vlan_check_attr(struct bridge_vlan_check_data *data,
  1955. struct rtattr *attr)
  1956. {
  1957. struct bridge_vlan_hotplug_port *port;
  1958. struct bridge_vlan_info *vinfo;
  1959. struct bridge_vlan *vlan;
  1960. struct rtattr *cur;
  1961. int rem = RTA_PAYLOAD(attr);
  1962. int i;
  1963. for (cur = RTA_DATA(attr); RTA_OK(cur, rem); cur = RTA_NEXT(cur, rem)) {
  1964. if (cur->rta_type != IFLA_BRIDGE_VLAN_INFO)
  1965. continue;
  1966. vinfo = RTA_DATA(cur);
  1967. vlan = vlist_find(&data->check_dev->vlans, &vinfo->vid, vlan, node);
  1968. if (!vlan) {
  1969. data->ret = 1;
  1970. data->pending = false;
  1971. return;
  1972. }
  1973. for (i = 0; i < vlan->n_ports; i++)
  1974. if (!vlan->ports[i].check)
  1975. bridge_vlan_check_port(data, &vlan->ports[i], vinfo);
  1976. list_for_each_entry(port, &vlan->hotplug_ports, list)
  1977. if (!port->port.check)
  1978. bridge_vlan_check_port(data, &port->port, vinfo);
  1979. }
  1980. }
  1981. static int bridge_vlan_check_cb(struct nl_msg *msg, void *arg)
  1982. {
  1983. struct bridge_vlan_check_data *data = arg;
  1984. struct nlmsghdr *nh = nlmsg_hdr(msg);
  1985. struct ifinfomsg *ifi = NLMSG_DATA(nh);
  1986. struct rtattr *attr;
  1987. int rem;
  1988. if (nh->nlmsg_type != RTM_NEWLINK)
  1989. return NL_SKIP;
  1990. if (ifi->ifi_family != AF_BRIDGE)
  1991. return NL_SKIP;
  1992. if (ifi->ifi_index != data->ifindex)
  1993. return NL_SKIP;
  1994. attr = IFLA_RTA(ifi);
  1995. rem = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
  1996. while (RTA_OK(attr, rem)) {
  1997. if (attr->rta_type == IFLA_AF_SPEC)
  1998. bridge_vlan_check_attr(data, attr);
  1999. attr = RTA_NEXT(attr, rem);
  2000. }
  2001. return NL_SKIP;
  2002. }
  2003. static int bridge_vlan_ack_cb(struct nl_msg *msg, void *arg)
  2004. {
  2005. struct bridge_vlan_check_data *data = arg;
  2006. data->pending = false;
  2007. return NL_STOP;
  2008. }
  2009. static int bridge_vlan_error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
  2010. {
  2011. struct bridge_vlan_check_data *data = arg;
  2012. data->pending = false;
  2013. return NL_STOP;
  2014. }
  2015. int system_bridge_vlan_check(struct device *dev, char *ifname)
  2016. {
  2017. struct bridge_vlan_check_data data = {
  2018. .check_dev = dev,
  2019. .ifindex = if_nametoindex(ifname),
  2020. .ret = -1,
  2021. .pending = true,
  2022. };
  2023. static struct ifinfomsg ifi = {
  2024. .ifi_family = AF_BRIDGE
  2025. };
  2026. static struct rtattr ext_req = {
  2027. .rta_type = IFLA_EXT_MASK,
  2028. .rta_len = RTA_LENGTH(sizeof(uint32_t)),
  2029. };
  2030. uint32_t filter = RTEXT_FILTER_BRVLAN;
  2031. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  2032. struct bridge_vlan *vlan;
  2033. struct nl_msg *msg;
  2034. int i;
  2035. if (!data.ifindex)
  2036. return 0;
  2037. msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_DUMP);
  2038. if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
  2039. nlmsg_append(msg, &ext_req, sizeof(ext_req), NLMSG_ALIGNTO) ||
  2040. nlmsg_append(msg, &filter, sizeof(filter), 0))
  2041. goto free;
  2042. vlist_for_each_element(&dev->vlans, vlan, node) {
  2043. struct bridge_vlan_hotplug_port *port;
  2044. for (i = 0; i < vlan->n_ports; i++) {
  2045. if (!strcmp(vlan->ports[i].ifname, ifname))
  2046. vlan->ports[i].check = 0;
  2047. else
  2048. vlan->ports[i].check = -1;
  2049. }
  2050. list_for_each_entry(port, &vlan->hotplug_ports, list) {
  2051. if (!strcmp(port->port.ifname, ifname))
  2052. port->port.check = 0;
  2053. else
  2054. port->port.check = -1;
  2055. }
  2056. }
  2057. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, bridge_vlan_check_cb, &data);
  2058. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
  2059. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
  2060. nl_cb_err(cb, NL_CB_CUSTOM, bridge_vlan_error_cb, &data);
  2061. if (nl_send_auto_complete(sock_rtnl, msg) < 0)
  2062. goto free;
  2063. data.ret = 0;
  2064. while (data.pending)
  2065. nl_recvmsgs(sock_rtnl, cb);
  2066. vlist_for_each_element(&dev->vlans, vlan, node) {
  2067. struct bridge_vlan_hotplug_port *port;
  2068. for (i = 0; i < vlan->n_ports; i++) {
  2069. if (!vlan->ports[i].check) {
  2070. data.ret = 1;
  2071. break;
  2072. }
  2073. }
  2074. list_for_each_entry(port, &vlan->hotplug_ports, list) {
  2075. if (!port->port.check) {
  2076. data.ret = 1;
  2077. break;
  2078. }
  2079. }
  2080. }
  2081. free:
  2082. nlmsg_free(msg);
  2083. nl_cb_put(cb);
  2084. return data.ret;
  2085. }
  2086. int system_if_check(struct device *dev)
  2087. {
  2088. struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
  2089. struct nl_msg *msg;
  2090. struct ifinfomsg ifi = {
  2091. .ifi_family = AF_UNSPEC,
  2092. .ifi_index = 0,
  2093. };
  2094. struct if_check_data chk = {
  2095. .dev = dev,
  2096. .pending = 1,
  2097. };
  2098. int ret = 1;
  2099. if (!cb)
  2100. return ret;
  2101. msg = nlmsg_alloc_simple(RTM_GETLINK, 0);
  2102. if (!msg)
  2103. goto out;
  2104. if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
  2105. nla_put_string(msg, IFLA_IFNAME, dev->ifname))
  2106. goto free;
  2107. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_if_check_valid, &chk);
  2108. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_if_check_ack, &chk);
  2109. nl_cb_err(cb, NL_CB_CUSTOM, cb_if_check_error, &chk);
  2110. ret = nl_send_auto_complete(sock_rtnl, msg);
  2111. if (ret < 0)
  2112. goto free;
  2113. while (chk.pending > 0)
  2114. nl_recvmsgs(sock_rtnl, cb);
  2115. ret = chk.pending;
  2116. free:
  2117. nlmsg_free(msg);
  2118. out:
  2119. nl_cb_put(cb);
  2120. return ret;
  2121. }
  2122. struct device *
  2123. system_if_get_parent(struct device *dev)
  2124. {
  2125. char buf[64], *devname;
  2126. int ifindex, iflink;
  2127. if (system_get_dev_sysfs("iflink", dev->ifname, buf, sizeof(buf)) < 0)
  2128. return NULL;
  2129. iflink = strtoul(buf, NULL, 0);
  2130. ifindex = system_if_resolve(dev);
  2131. if (!iflink || iflink == ifindex)
  2132. return NULL;
  2133. devname = if_indextoname(iflink, buf);
  2134. if (!devname)
  2135. return NULL;
  2136. return device_get(devname, true);
  2137. }
  2138. static bool
  2139. read_string_file(int dir_fd, const char *file, char *buf, int len)
  2140. {
  2141. bool ret = false;
  2142. char *c;
  2143. int fd;
  2144. fd = openat(dir_fd, file, O_RDONLY);
  2145. if (fd < 0)
  2146. return false;
  2147. retry:
  2148. len = read(fd, buf, len - 1);
  2149. if (len < 0) {
  2150. if (errno == EINTR)
  2151. goto retry;
  2152. } else if (len > 0) {
  2153. buf[len] = 0;
  2154. c = strchr(buf, '\n');
  2155. if (c)
  2156. *c = 0;
  2157. ret = true;
  2158. }
  2159. close(fd);
  2160. return ret;
  2161. }
  2162. static bool
  2163. read_uint64_file(int dir_fd, const char *file, uint64_t *val)
  2164. {
  2165. char buf[64];
  2166. bool ret = false;
  2167. ret = read_string_file(dir_fd, file, buf, sizeof(buf));
  2168. if (ret)
  2169. *val = strtoull(buf, NULL, 0);
  2170. return ret;
  2171. }
  2172. bool
  2173. system_if_force_external(const char *ifname)
  2174. {
  2175. struct stat s;
  2176. return stat(dev_sysfs_path(ifname, "phy80211"), &s) == 0;
  2177. }
  2178. static const char *
  2179. system_netdevtype_name(unsigned short dev_type)
  2180. {
  2181. size_t i;
  2182. for (i = 0; i < ARRAY_SIZE(netdev_types); i++) {
  2183. if (netdev_types[i].id == dev_type)
  2184. return netdev_types[i].name;
  2185. }
  2186. /* the last key is used by default */
  2187. i = ARRAY_SIZE(netdev_types) - 1;
  2188. return netdev_types[i].name;
  2189. }
  2190. static void
  2191. system_add_devtype(struct blob_buf *b, const char *ifname)
  2192. {
  2193. char buf[100];
  2194. bool found = false;
  2195. if (!system_get_dev_sysfs("uevent", ifname, buf, sizeof(buf))) {
  2196. const char *info = "DEVTYPE=";
  2197. char *context = NULL;
  2198. const char *line = strtok_r(buf, "\r\n", &context);
  2199. while (line != NULL) {
  2200. char *index = strstr(line, info);
  2201. if (index != NULL) {
  2202. blobmsg_add_string(b, "devtype", index + strlen(info));
  2203. found = true;
  2204. break;
  2205. }
  2206. line = strtok_r(NULL, "\r\n", &context);
  2207. }
  2208. }
  2209. if (!found) {
  2210. unsigned short number = 0;
  2211. const char *name = NULL;
  2212. if (!system_get_dev_sysfs("type", ifname, buf, sizeof(buf))) {
  2213. number = strtoul(buf, NULL, 0);
  2214. name = system_netdevtype_name(number);
  2215. blobmsg_add_string(b, "devtype", name);
  2216. }
  2217. }
  2218. }
  2219. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  2220. static int32_t
  2221. ethtool_feature_count(const char *ifname)
  2222. {
  2223. struct {
  2224. struct ethtool_sset_info hdr;
  2225. uint32_t buf;
  2226. } req = {
  2227. .hdr = {
  2228. .cmd = ETHTOOL_GSSET_INFO,
  2229. .sset_mask = 1 << ETH_SS_FEATURES
  2230. }
  2231. };
  2232. struct ifreq ifr = {
  2233. .ifr_data = (void *)&req
  2234. };
  2235. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  2236. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0)
  2237. return -1;
  2238. if (!req.hdr.sset_mask)
  2239. return 0;
  2240. return req.buf;
  2241. }
  2242. static int32_t
  2243. ethtool_feature_index(const char *ifname, const char *keyname)
  2244. {
  2245. struct ethtool_gstrings *feature_names;
  2246. struct ifreq ifr = { 0 };
  2247. int32_t n_features;
  2248. uint32_t i;
  2249. n_features = ethtool_feature_count(ifname);
  2250. if (n_features <= 0)
  2251. return -1;
  2252. feature_names = calloc(1, sizeof(*feature_names) + n_features * ETH_GSTRING_LEN);
  2253. if (!feature_names)
  2254. return -1;
  2255. feature_names->cmd = ETHTOOL_GSTRINGS;
  2256. feature_names->string_set = ETH_SS_FEATURES;
  2257. feature_names->len = n_features;
  2258. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  2259. ifr.ifr_data = (void *)feature_names;
  2260. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0) {
  2261. free(feature_names);
  2262. return -1;
  2263. }
  2264. for (i = 0; i < feature_names->len; i++)
  2265. if (!strcmp((char *)&feature_names->data[i * ETH_GSTRING_LEN], keyname))
  2266. break;
  2267. if (i >= feature_names->len)
  2268. i = -1;
  2269. free(feature_names);
  2270. return i;
  2271. }
  2272. static bool
  2273. ethtool_feature_value(const char *ifname, const char *keyname)
  2274. {
  2275. struct ethtool_get_features_block *feature_block;
  2276. struct ethtool_gfeatures *feature_values;
  2277. struct ifreq ifr = { 0 };
  2278. int32_t feature_idx;
  2279. bool active;
  2280. feature_idx = ethtool_feature_index(ifname, keyname);
  2281. if (feature_idx < 0)
  2282. return false;
  2283. feature_values = calloc(1,
  2284. sizeof(*feature_values) +
  2285. sizeof(feature_values->features[0]) * DIV_ROUND_UP(feature_idx, 32));
  2286. if (!feature_values)
  2287. return false;
  2288. feature_values->cmd = ETHTOOL_GFEATURES;
  2289. feature_values->size = DIV_ROUND_UP(feature_idx, 32);
  2290. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  2291. ifr.ifr_data = (void *)feature_values;
  2292. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) != 0) {
  2293. free(feature_values);
  2294. return false;
  2295. }
  2296. feature_block = &feature_values->features[feature_idx / 32];
  2297. active = feature_block->active & (1U << feature_idx % 32);
  2298. free(feature_values);
  2299. return active;
  2300. }
  2301. static void
  2302. system_add_link_mode_name(struct blob_buf *b, int i, bool half)
  2303. {
  2304. char *buf;
  2305. /* allocate string buffer large enough for the mode name and a suffix
  2306. * "-F" or "-H" indicating full duplex or half duplex.
  2307. */
  2308. buf = blobmsg_alloc_string_buffer(b, NULL, strlen(ethtool_modes[i].name) + 3);
  2309. if (!buf)
  2310. return;
  2311. strcpy(buf, ethtool_modes[i].name);
  2312. if (half)
  2313. strcat(buf, "-H");
  2314. else
  2315. strcat(buf, "-F");
  2316. blobmsg_add_string_buffer(b);
  2317. }
  2318. static void
  2319. system_add_link_modes(__s8 nwords, struct blob_buf *b, __u32 *mask)
  2320. {
  2321. size_t i;
  2322. for (i = 0; i < ARRAY_SIZE(ethtool_modes); i++) {
  2323. if (ethtool_link_mode_test_bit(nwords, ethtool_modes[i].bit_half, mask))
  2324. system_add_link_mode_name(b, i, true);
  2325. if (ethtool_link_mode_test_bit(nwords, ethtool_modes[i].bit_full, mask))
  2326. system_add_link_mode_name(b, i, false);
  2327. }
  2328. }
  2329. static void
  2330. system_add_pause_modes(__s8 nwords, struct blob_buf *b, __u32 *mask)
  2331. {
  2332. if (ethtool_link_mode_test_bit(nwords, ETHTOOL_LINK_MODE_Pause_BIT, mask))
  2333. blobmsg_add_string(b, NULL, "pause");
  2334. if (ethtool_link_mode_test_bit(nwords, ETHTOOL_LINK_MODE_Asym_Pause_BIT, mask))
  2335. blobmsg_add_string(b, NULL, "asym_pause");
  2336. }
  2337. static void
  2338. system_add_ethtool_pause_an(struct blob_buf *b, __s8 nwords,
  2339. __u32 *advertising, __u32 *lp_advertising)
  2340. {
  2341. bool an_rx = false, an_tx = false;
  2342. void *d;
  2343. d = blobmsg_open_array(b, "negotiated");
  2344. /* Work out negotiated pause frame usage per
  2345. * IEEE 802.3-2005 table 28B-3.
  2346. */
  2347. if (ethtool_link_mode_test_bit(nwords,
  2348. ETHTOOL_LINK_MODE_Pause_BIT,
  2349. advertising) &&
  2350. ethtool_link_mode_test_bit(nwords,
  2351. ETHTOOL_LINK_MODE_Pause_BIT,
  2352. lp_advertising)) {
  2353. an_tx = true;
  2354. an_rx = true;
  2355. } else if (ethtool_link_mode_test_bit(nwords,
  2356. ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  2357. advertising) &&
  2358. ethtool_link_mode_test_bit(nwords,
  2359. ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  2360. lp_advertising)) {
  2361. if (ethtool_link_mode_test_bit(nwords,
  2362. ETHTOOL_LINK_MODE_Pause_BIT,
  2363. advertising))
  2364. an_rx = true;
  2365. else if (ethtool_link_mode_test_bit(nwords,
  2366. ETHTOOL_LINK_MODE_Pause_BIT,
  2367. lp_advertising))
  2368. an_tx = true;
  2369. }
  2370. if (an_tx)
  2371. blobmsg_add_string(b, NULL, "rx");
  2372. if (an_rx)
  2373. blobmsg_add_string(b, NULL, "tx");
  2374. blobmsg_close_array(b, d);
  2375. }
  2376. static void
  2377. system_get_ethtool_pause(struct device *dev, bool *rx_pause, bool *tx_pause, bool *pause_autoneg)
  2378. {
  2379. struct ethtool_pauseparam pp;
  2380. struct ifreq ifr = {
  2381. .ifr_data = (caddr_t)&pp,
  2382. };
  2383. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  2384. memset(&pp, 0, sizeof(pp));
  2385. pp.cmd = ETHTOOL_GPAUSEPARAM;
  2386. /* may fail */
  2387. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == -1) {
  2388. *pause_autoneg = true;
  2389. return;
  2390. }
  2391. *rx_pause = pp.rx_pause;
  2392. *tx_pause = pp.tx_pause;
  2393. *pause_autoneg = pp.autoneg;
  2394. }
  2395. int
  2396. system_if_dump_info(struct device *dev, struct blob_buf *b)
  2397. {
  2398. __u32 *supported, *advertising, *lp_advertising;
  2399. bool rx_pause, tx_pause, pause_autoneg;
  2400. struct {
  2401. struct ethtool_link_settings req;
  2402. __u32 link_mode_data[3 * 127];
  2403. } ecmd;
  2404. struct ifreq ifr = {
  2405. .ifr_data = (caddr_t)&ecmd,
  2406. };
  2407. __s8 nwords;
  2408. void *c, *d;
  2409. char *s;
  2410. system_get_ethtool_pause(dev, &rx_pause, &tx_pause, &pause_autoneg);
  2411. memset(&ecmd, 0, sizeof(ecmd));
  2412. ecmd.req.cmd = ETHTOOL_GLINKSETTINGS;
  2413. strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
  2414. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) < 0 ||
  2415. ecmd.req.link_mode_masks_nwords >= 0 ||
  2416. ecmd.req.cmd != ETHTOOL_GLINKSETTINGS)
  2417. return -EOPNOTSUPP;
  2418. ecmd.req.link_mode_masks_nwords = -ecmd.req.link_mode_masks_nwords;
  2419. if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) < 0 ||
  2420. ecmd.req.link_mode_masks_nwords <= 0 ||
  2421. ecmd.req.cmd != ETHTOOL_GLINKSETTINGS)
  2422. return -EIO;
  2423. nwords = ecmd.req.link_mode_masks_nwords;
  2424. supported = &ecmd.link_mode_data[0];
  2425. advertising = &ecmd.link_mode_data[nwords];
  2426. lp_advertising = &ecmd.link_mode_data[2 * nwords];
  2427. c = blobmsg_open_array(b, "link-advertising");
  2428. system_add_link_modes(nwords, b, advertising);
  2429. blobmsg_close_array(b, c);
  2430. c = blobmsg_open_array(b, "link-partner-advertising");
  2431. system_add_link_modes(nwords, b, lp_advertising);
  2432. blobmsg_close_array(b, c);
  2433. c = blobmsg_open_array(b, "link-supported");
  2434. system_add_link_modes(nwords, b, supported);
  2435. blobmsg_close_array(b, c);
  2436. if (ethtool_validate_speed(ecmd.req.speed) &&
  2437. (ecmd.req.speed != (__u32)SPEED_UNKNOWN) &&
  2438. (ecmd.req.speed != 0)) {
  2439. s = blobmsg_alloc_string_buffer(b, "speed", 10);
  2440. snprintf(s, 8, "%d%c", ecmd.req.speed,
  2441. ecmd.req.duplex == DUPLEX_HALF ? 'H' : 'F');
  2442. blobmsg_add_string_buffer(b);
  2443. }
  2444. blobmsg_add_u8(b, "autoneg", !!ecmd.req.autoneg);
  2445. c = blobmsg_open_table(b, "flow-control");
  2446. blobmsg_add_u8(b, "autoneg", pause_autoneg);
  2447. d = blobmsg_open_array(b, "supported");
  2448. system_add_pause_modes(nwords, b, supported);
  2449. blobmsg_close_array(b, d);
  2450. if (pause_autoneg) {
  2451. d = blobmsg_open_array(b, "link-advertising");
  2452. system_add_pause_modes(nwords, b, advertising);
  2453. blobmsg_close_array(b, d);
  2454. }
  2455. d = blobmsg_open_array(b, "link-partner-advertising");
  2456. system_add_pause_modes(nwords, b, lp_advertising);
  2457. blobmsg_close_array(b, d);
  2458. if (pause_autoneg) {
  2459. system_add_ethtool_pause_an(b, nwords, advertising,
  2460. lp_advertising);
  2461. } else {
  2462. d = blobmsg_open_array(b, "selected");
  2463. if (rx_pause)
  2464. blobmsg_add_string(b, NULL, "rx");
  2465. if (tx_pause)
  2466. blobmsg_add_string(b, NULL, "tx");
  2467. blobmsg_close_array(b, d);
  2468. }
  2469. blobmsg_close_table(b, c);
  2470. blobmsg_add_u8(b, "hw-tc-offload",
  2471. ethtool_feature_value(dev->ifname, "hw-tc-offload"));
  2472. system_add_devtype(b, dev->ifname);
  2473. return 0;
  2474. }
  2475. int
  2476. system_if_dump_stats(struct device *dev, struct blob_buf *b)
  2477. {
  2478. const char *const counters[] = {
  2479. "collisions", "rx_frame_errors", "tx_compressed",
  2480. "multicast", "rx_length_errors", "tx_dropped",
  2481. "rx_bytes", "rx_missed_errors", "tx_errors",
  2482. "rx_compressed", "rx_over_errors", "tx_fifo_errors",
  2483. "rx_crc_errors", "rx_packets", "tx_heartbeat_errors",
  2484. "rx_dropped", "tx_aborted_errors", "tx_packets",
  2485. "rx_errors", "tx_bytes", "tx_window_errors",
  2486. "rx_fifo_errors", "tx_carrier_errors",
  2487. };
  2488. int stats_dir;
  2489. size_t i;
  2490. uint64_t val = 0;
  2491. stats_dir = open(dev_sysfs_path(dev->ifname, "statistics"), O_DIRECTORY);
  2492. if (stats_dir < 0)
  2493. return -1;
  2494. for (i = 0; i < ARRAY_SIZE(counters); i++)
  2495. if (read_uint64_file(stats_dir, counters[i], &val))
  2496. blobmsg_add_u64(b, counters[i], val);
  2497. close(stats_dir);
  2498. return 0;
  2499. }
  2500. static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
  2501. {
  2502. bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
  2503. int alen = v4 ? 4 : 16;
  2504. unsigned int flags = 0;
  2505. struct ifaddrmsg ifa = {
  2506. .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
  2507. .ifa_prefixlen = addr->mask,
  2508. .ifa_index = dev->ifindex,
  2509. };
  2510. struct nl_msg *msg;
  2511. if (cmd == RTM_NEWADDR)
  2512. flags |= NLM_F_CREATE | NLM_F_REPLACE;
  2513. msg = nlmsg_alloc_simple(cmd, flags);
  2514. if (!msg)
  2515. return -1;
  2516. nlmsg_append(msg, &ifa, sizeof(ifa), 0);
  2517. nla_put(msg, IFA_LOCAL, alen, &addr->addr);
  2518. if (v4) {
  2519. if (addr->broadcast)
  2520. nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
  2521. if (addr->point_to_point)
  2522. nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
  2523. } else {
  2524. time_t now = system_get_rtime();
  2525. struct ifa_cacheinfo cinfo = {0xffffffffU, 0xffffffffU, 0, 0};
  2526. if (addr->preferred_until) {
  2527. int64_t preferred = addr->preferred_until - now;
  2528. if (preferred < 0)
  2529. preferred = 0;
  2530. else if (preferred > UINT32_MAX)
  2531. preferred = UINT32_MAX;
  2532. cinfo.ifa_prefered = preferred;
  2533. }
  2534. if (addr->valid_until) {
  2535. int64_t valid = addr->valid_until - now;
  2536. if (valid <= 0) {
  2537. nlmsg_free(msg);
  2538. return -1;
  2539. }
  2540. else if (valid > UINT32_MAX)
  2541. valid = UINT32_MAX;
  2542. cinfo.ifa_valid = valid;
  2543. }
  2544. nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
  2545. if (cmd == RTM_NEWADDR && (addr->flags & DEVADDR_OFFLINK))
  2546. nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE);
  2547. }
  2548. return system_rtnl_call(msg);
  2549. }
  2550. int system_add_address(struct device *dev, struct device_addr *addr)
  2551. {
  2552. return system_addr(dev, addr, RTM_NEWADDR);
  2553. }
  2554. int system_del_address(struct device *dev, struct device_addr *addr)
  2555. {
  2556. return system_addr(dev, addr, RTM_DELADDR);
  2557. }
  2558. static int system_neigh(struct device *dev, struct device_neighbor *neighbor, int cmd)
  2559. {
  2560. int alen = ((neighbor->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
  2561. unsigned int flags = 0;
  2562. struct ndmsg ndm = {
  2563. .ndm_family = (alen == 4) ? AF_INET : AF_INET6,
  2564. .ndm_ifindex = dev->ifindex,
  2565. .ndm_state = NUD_PERMANENT,
  2566. .ndm_flags = (neighbor->proxy ? NTF_PROXY : 0) | (neighbor->router ? NTF_ROUTER : 0),
  2567. };
  2568. struct nl_msg *msg;
  2569. if (cmd == RTM_NEWNEIGH)
  2570. flags |= NLM_F_CREATE | NLM_F_REPLACE;
  2571. msg = nlmsg_alloc_simple(cmd, flags);
  2572. if (!msg)
  2573. return -1;
  2574. nlmsg_append(msg, &ndm, sizeof(ndm), 0);
  2575. nla_put(msg, NDA_DST, alen, &neighbor->addr);
  2576. if (neighbor->flags & DEVNEIGH_MAC)
  2577. nla_put(msg, NDA_LLADDR, sizeof(neighbor->macaddr), &neighbor->macaddr);
  2578. return system_rtnl_call(msg);
  2579. }
  2580. int system_add_neighbor(struct device *dev, struct device_neighbor *neighbor)
  2581. {
  2582. return system_neigh(dev, neighbor, RTM_NEWNEIGH);
  2583. }
  2584. int system_del_neighbor(struct device *dev, struct device_neighbor *neighbor)
  2585. {
  2586. return system_neigh(dev, neighbor, RTM_DELNEIGH);
  2587. }
  2588. static int system_rt(struct device *dev, struct device_route *route, int cmd)
  2589. {
  2590. int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
  2591. bool have_gw;
  2592. unsigned int flags = 0;
  2593. if (alen == 4)
  2594. have_gw = !!route->nexthop.in.s_addr;
  2595. else
  2596. have_gw = route->nexthop.in6.s6_addr32[0] ||
  2597. route->nexthop.in6.s6_addr32[1] ||
  2598. route->nexthop.in6.s6_addr32[2] ||
  2599. route->nexthop.in6.s6_addr32[3];
  2600. unsigned int table = (route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))
  2601. ? route->table : RT_TABLE_MAIN;
  2602. struct rtmsg rtm = {
  2603. .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
  2604. .rtm_dst_len = route->mask,
  2605. .rtm_src_len = route->sourcemask,
  2606. .rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
  2607. .rtm_protocol = (route->flags & DEVROUTE_PROTO) ? route->proto : RTPROT_STATIC,
  2608. .rtm_scope = RT_SCOPE_NOWHERE,
  2609. .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
  2610. .rtm_flags = (route->flags & DEVROUTE_ONLINK) ? RTNH_F_ONLINK : 0,
  2611. };
  2612. struct nl_msg *msg;
  2613. if (cmd == RTM_NEWROUTE) {
  2614. flags |= NLM_F_CREATE | NLM_F_REPLACE;
  2615. if (!dev) { /* Add null-route */
  2616. rtm.rtm_scope = RT_SCOPE_UNIVERSE;
  2617. rtm.rtm_type = RTN_UNREACHABLE;
  2618. }
  2619. else
  2620. rtm.rtm_scope = (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
  2621. }
  2622. if (route->flags & DEVROUTE_TYPE) {
  2623. rtm.rtm_type = route->type;
  2624. if (!(route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))) {
  2625. if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_BROADCAST ||
  2626. rtm.rtm_type == RTN_NAT || rtm.rtm_type == RTN_ANYCAST)
  2627. rtm.rtm_table = RT_TABLE_LOCAL;
  2628. }
  2629. if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_NAT) {
  2630. rtm.rtm_scope = RT_SCOPE_HOST;
  2631. } else if (rtm.rtm_type == RTN_BROADCAST || rtm.rtm_type == RTN_MULTICAST ||
  2632. rtm.rtm_type == RTN_ANYCAST) {
  2633. rtm.rtm_scope = RT_SCOPE_LINK;
  2634. } else if (rtm.rtm_type == RTN_BLACKHOLE || rtm.rtm_type == RTN_UNREACHABLE ||
  2635. rtm.rtm_type == RTN_PROHIBIT || rtm.rtm_type == RTN_FAILED_POLICY ||
  2636. rtm.rtm_type == RTN_THROW) {
  2637. rtm.rtm_scope = RT_SCOPE_UNIVERSE;
  2638. dev = NULL;
  2639. }
  2640. }
  2641. if (route->flags & DEVROUTE_NODEV)
  2642. dev = NULL;
  2643. msg = nlmsg_alloc_simple(cmd, flags);
  2644. if (!msg)
  2645. return -1;
  2646. nlmsg_append(msg, &rtm, sizeof(rtm), 0);
  2647. if (route->mask)
  2648. nla_put(msg, RTA_DST, alen, &route->addr);
  2649. if (route->sourcemask) {
  2650. if (rtm.rtm_family == AF_INET)
  2651. nla_put(msg, RTA_PREFSRC, alen, &route->source);
  2652. else
  2653. nla_put(msg, RTA_SRC, alen, &route->source);
  2654. }
  2655. if (route->metric > 0)
  2656. nla_put_u32(msg, RTA_PRIORITY, route->metric);
  2657. if (have_gw)
  2658. nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
  2659. if (dev)
  2660. nla_put_u32(msg, RTA_OIF, dev->ifindex);
  2661. if (table >= 256)
  2662. nla_put_u32(msg, RTA_TABLE, table);
  2663. if (route->flags & DEVROUTE_MTU) {
  2664. struct nlattr *metrics;
  2665. if (!(metrics = nla_nest_start(msg, RTA_METRICS)))
  2666. goto nla_put_failure;
  2667. nla_put_u32(msg, RTAX_MTU, route->mtu);
  2668. nla_nest_end(msg, metrics);
  2669. }
  2670. return system_rtnl_call(msg);
  2671. nla_put_failure:
  2672. nlmsg_free(msg);
  2673. return -ENOMEM;
  2674. }
  2675. int system_add_route(struct device *dev, struct device_route *route)
  2676. {
  2677. return system_rt(dev, route, RTM_NEWROUTE);
  2678. }
  2679. int system_del_route(struct device *dev, struct device_route *route)
  2680. {
  2681. return system_rt(dev, route, RTM_DELROUTE);
  2682. }
  2683. int system_flush_routes(void)
  2684. {
  2685. const char *names[] = { "ipv4", "ipv6" };
  2686. size_t i;
  2687. int fd;
  2688. for (i = 0; i < ARRAY_SIZE(names); i++) {
  2689. snprintf(dev_buf, sizeof(dev_buf), "%s/sys/net/%s/route/flush", proc_path, names[i]);
  2690. fd = open(dev_buf, O_WRONLY);
  2691. if (fd < 0)
  2692. continue;
  2693. if (write(fd, "-1", 2)) {}
  2694. close(fd);
  2695. }
  2696. return 0;
  2697. }
  2698. bool system_resolve_rt_type(const char *type, unsigned int *id)
  2699. {
  2700. return system_rtn_aton(type, id);
  2701. }
  2702. bool system_resolve_rt_proto(const char *type, unsigned int *id)
  2703. {
  2704. FILE *f;
  2705. char *e, buf[128];
  2706. unsigned int n, proto = 256;
  2707. n = strtoul(type, &e, 0);
  2708. if (!*e && e != type)
  2709. proto = n;
  2710. else if (!strcmp(type, "unspec"))
  2711. proto = RTPROT_UNSPEC;
  2712. else if (!strcmp(type, "kernel"))
  2713. proto = RTPROT_KERNEL;
  2714. else if (!strcmp(type, "boot"))
  2715. proto = RTPROT_BOOT;
  2716. else if (!strcmp(type, "static"))
  2717. proto = RTPROT_STATIC;
  2718. else if ((f = fopen("/etc/iproute2/rt_protos", "r")) != NULL) {
  2719. while (fgets(buf, sizeof(buf) - 1, f) != NULL) {
  2720. if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
  2721. continue;
  2722. n = strtoul(e, NULL, 10);
  2723. e = strtok(NULL, " \t\n");
  2724. if (e && !strcmp(e, type)) {
  2725. proto = n;
  2726. break;
  2727. }
  2728. }
  2729. fclose(f);
  2730. }
  2731. if (proto > 255)
  2732. return false;
  2733. *id = proto;
  2734. return true;
  2735. }
  2736. bool system_resolve_rt_table(const char *name, unsigned int *id)
  2737. {
  2738. FILE *f;
  2739. char *e, buf[128];
  2740. unsigned int n, table = RT_TABLE_UNSPEC;
  2741. /* first try to parse table as number */
  2742. if ((n = strtoul(name, &e, 0)) > 0 && !*e)
  2743. table = n;
  2744. /* handle well known aliases */
  2745. else if (!strcmp(name, "default"))
  2746. table = RT_TABLE_DEFAULT;
  2747. else if (!strcmp(name, "main"))
  2748. table = RT_TABLE_MAIN;
  2749. else if (!strcmp(name, "local"))
  2750. table = RT_TABLE_LOCAL;
  2751. /* try to look up name in /etc/iproute2/rt_tables */
  2752. else if ((f = fopen("/etc/iproute2/rt_tables", "r")) != NULL)
  2753. {
  2754. while (fgets(buf, sizeof(buf) - 1, f) != NULL)
  2755. {
  2756. if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
  2757. continue;
  2758. n = strtoul(e, NULL, 10);
  2759. e = strtok(NULL, " \t\n");
  2760. if (e && !strcmp(e, name))
  2761. {
  2762. table = n;
  2763. break;
  2764. }
  2765. }
  2766. fclose(f);
  2767. }
  2768. if (table == RT_TABLE_UNSPEC)
  2769. return false;
  2770. *id = table;
  2771. return true;
  2772. }
  2773. bool system_is_default_rt_table(unsigned int id)
  2774. {
  2775. return (id == RT_TABLE_MAIN);
  2776. }
  2777. bool system_resolve_rpfilter(const char *filter, unsigned int *id)
  2778. {
  2779. char *e;
  2780. unsigned int n;
  2781. if (!strcmp(filter, "strict"))
  2782. n = 1;
  2783. else if (!strcmp(filter, "loose"))
  2784. n = 2;
  2785. else {
  2786. n = strtoul(filter, &e, 0);
  2787. if (*e || e == filter || n > 2)
  2788. return false;
  2789. }
  2790. *id = n;
  2791. return true;
  2792. }
  2793. static int system_iprule(struct iprule *rule, int cmd)
  2794. {
  2795. int alen = ((rule->flags & IPRULE_FAMILY) == IPRULE_INET4) ? 4 : 16;
  2796. struct nl_msg *msg;
  2797. struct rtmsg rtm = {
  2798. .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
  2799. .rtm_protocol = RTPROT_STATIC,
  2800. .rtm_scope = RT_SCOPE_UNIVERSE,
  2801. .rtm_table = RT_TABLE_UNSPEC,
  2802. .rtm_type = RTN_UNSPEC,
  2803. .rtm_flags = 0,
  2804. };
  2805. if (cmd == RTM_NEWRULE)
  2806. rtm.rtm_type = RTN_UNICAST;
  2807. if (rule->invert)
  2808. rtm.rtm_flags |= FIB_RULE_INVERT;
  2809. if (rule->flags & IPRULE_SRC)
  2810. rtm.rtm_src_len = rule->src_mask;
  2811. if (rule->flags & IPRULE_DEST)
  2812. rtm.rtm_dst_len = rule->dest_mask;
  2813. if (rule->flags & IPRULE_TOS)
  2814. rtm.rtm_tos = rule->tos;
  2815. if (rule->flags & IPRULE_LOOKUP) {
  2816. if (rule->lookup < 256)
  2817. rtm.rtm_table = rule->lookup;
  2818. }
  2819. if (rule->flags & IPRULE_ACTION)
  2820. rtm.rtm_type = rule->action;
  2821. else if (rule->flags & IPRULE_GOTO)
  2822. rtm.rtm_type = FR_ACT_GOTO;
  2823. else if (!(rule->flags & (IPRULE_LOOKUP | IPRULE_ACTION | IPRULE_GOTO)))
  2824. rtm.rtm_type = FR_ACT_NOP;
  2825. msg = nlmsg_alloc_simple(cmd, NLM_F_REQUEST);
  2826. if (!msg)
  2827. return -1;
  2828. nlmsg_append(msg, &rtm, sizeof(rtm), 0);
  2829. if (rule->flags & IPRULE_IN)
  2830. nla_put(msg, FRA_IFNAME, strlen(rule->in_dev) + 1, rule->in_dev);
  2831. if (rule->flags & IPRULE_OUT)
  2832. nla_put(msg, FRA_OIFNAME, strlen(rule->out_dev) + 1, rule->out_dev);
  2833. if (rule->flags & IPRULE_SRC)
  2834. nla_put(msg, FRA_SRC, alen, &rule->src_addr);
  2835. if (rule->flags & IPRULE_DEST)
  2836. nla_put(msg, FRA_DST, alen, &rule->dest_addr);
  2837. if (rule->flags & IPRULE_PRIORITY)
  2838. nla_put_u32(msg, FRA_PRIORITY, rule->priority);
  2839. else if (cmd == RTM_NEWRULE)
  2840. nla_put_u32(msg, FRA_PRIORITY, rule->order);
  2841. if (rule->flags & IPRULE_FWMARK)
  2842. nla_put_u32(msg, FRA_FWMARK, rule->fwmark);
  2843. if (rule->flags & IPRULE_FWMASK)
  2844. nla_put_u32(msg, FRA_FWMASK, rule->fwmask);
  2845. if (rule->flags & IPRULE_LOOKUP) {
  2846. if (rule->lookup >= 256)
  2847. nla_put_u32(msg, FRA_TABLE, rule->lookup);
  2848. }
  2849. if (rule->flags & IPRULE_SUP_PREFIXLEN)
  2850. nla_put_u32(msg, FRA_SUPPRESS_PREFIXLEN, rule->sup_prefixlen);
  2851. if (rule->flags & IPRULE_UIDRANGE) {
  2852. struct fib_rule_uid_range uidrange = {
  2853. .start = rule->uidrange_start,
  2854. .end = rule->uidrange_end
  2855. };
  2856. nla_put(msg, FRA_UID_RANGE, sizeof(uidrange), &uidrange);
  2857. }
  2858. if (rule->flags & IPRULE_GOTO)
  2859. nla_put_u32(msg, FRA_GOTO, rule->gotoid);
  2860. return system_rtnl_call(msg);
  2861. }
  2862. int system_add_iprule(struct iprule *rule)
  2863. {
  2864. return system_iprule(rule, RTM_NEWRULE);
  2865. }
  2866. int system_del_iprule(struct iprule *rule)
  2867. {
  2868. return system_iprule(rule, RTM_DELRULE);
  2869. }
  2870. int system_flush_iprules(void)
  2871. {
  2872. int rv = 0;
  2873. struct iprule rule;
  2874. system_if_clear_entries(NULL, RTM_GETRULE, AF_INET);
  2875. system_if_clear_entries(NULL, RTM_GETRULE, AF_INET6);
  2876. memset(&rule, 0, sizeof(rule));
  2877. rule.flags = IPRULE_INET4 | IPRULE_PRIORITY | IPRULE_LOOKUP;
  2878. rule.priority = 0;
  2879. rule.lookup = RT_TABLE_LOCAL;
  2880. rv |= system_iprule(&rule, RTM_NEWRULE);
  2881. rule.priority = 32766;
  2882. rule.lookup = RT_TABLE_MAIN;
  2883. rv |= system_iprule(&rule, RTM_NEWRULE);
  2884. rule.priority = 32767;
  2885. rule.lookup = RT_TABLE_DEFAULT;
  2886. rv |= system_iprule(&rule, RTM_NEWRULE);
  2887. rule.flags = IPRULE_INET6 | IPRULE_PRIORITY | IPRULE_LOOKUP;
  2888. rule.priority = 0;
  2889. rule.lookup = RT_TABLE_LOCAL;
  2890. rv |= system_iprule(&rule, RTM_NEWRULE);
  2891. rule.priority = 32766;
  2892. rule.lookup = RT_TABLE_MAIN;
  2893. rv |= system_iprule(&rule, RTM_NEWRULE);
  2894. return rv;
  2895. }
  2896. bool system_resolve_iprule_action(const char *action, unsigned int *id)
  2897. {
  2898. return system_rtn_aton(action, id);
  2899. }
  2900. time_t system_get_rtime(void)
  2901. {
  2902. struct timespec ts;
  2903. struct timeval tv;
  2904. if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  2905. return ts.tv_sec;
  2906. if (gettimeofday(&tv, NULL) == 0)
  2907. return tv.tv_sec;
  2908. return 0;
  2909. }
  2910. #ifndef IP_DF
  2911. #define IP_DF 0x4000
  2912. #endif
  2913. static int tunnel_ioctl(const char *name, int cmd, void *p)
  2914. {
  2915. struct ifreq ifr;
  2916. memset(&ifr, 0, sizeof(ifr));
  2917. strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
  2918. ifr.ifr_ifru.ifru_data = p;
  2919. return ioctl(sock_ioctl, cmd, &ifr);
  2920. }
  2921. #ifdef IFLA_IPTUN_MAX
  2922. static int system_add_ip6_tunnel(const char *name, const unsigned int link,
  2923. struct blob_attr **tb)
  2924. {
  2925. struct nl_msg *nlm = nlmsg_alloc_simple(RTM_NEWLINK,
  2926. NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  2927. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC };
  2928. struct blob_attr *cur;
  2929. int ret = 0, ttl = 0;
  2930. if (!nlm)
  2931. return -1;
  2932. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  2933. nla_put_string(nlm, IFLA_IFNAME, name);
  2934. if (link)
  2935. nla_put_u32(nlm, IFLA_LINK, link);
  2936. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  2937. if (!linkinfo) {
  2938. ret = -ENOMEM;
  2939. goto failure;
  2940. }
  2941. nla_put_string(nlm, IFLA_INFO_KIND, "ip6tnl");
  2942. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  2943. if (!infodata) {
  2944. ret = -ENOMEM;
  2945. goto failure;
  2946. }
  2947. if (link)
  2948. nla_put_u32(nlm, IFLA_IPTUN_LINK, link);
  2949. if ((cur = tb[TUNNEL_ATTR_TTL]))
  2950. ttl = blobmsg_get_u32(cur);
  2951. nla_put_u8(nlm, IFLA_IPTUN_PROTO, IPPROTO_IPIP);
  2952. nla_put_u8(nlm, IFLA_IPTUN_TTL, (ttl) ? ttl : 64);
  2953. struct in6_addr in6buf;
  2954. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  2955. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2956. ret = -EINVAL;
  2957. goto failure;
  2958. }
  2959. nla_put(nlm, IFLA_IPTUN_LOCAL, sizeof(in6buf), &in6buf);
  2960. }
  2961. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  2962. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  2963. ret = -EINVAL;
  2964. goto failure;
  2965. }
  2966. nla_put(nlm, IFLA_IPTUN_REMOTE, sizeof(in6buf), &in6buf);
  2967. }
  2968. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  2969. struct blob_attr *tb_data[__IPIP6_DATA_ATTR_MAX];
  2970. uint32_t tun_flags = IP6_TNL_F_IGN_ENCAP_LIMIT;
  2971. blobmsg_parse(ipip6_data_attr_list.params, __IPIP6_DATA_ATTR_MAX, tb_data,
  2972. blobmsg_data(cur), blobmsg_len(cur));
  2973. if ((cur = tb_data[IPIP6_DATA_ENCAPLIMIT])) {
  2974. char *str = blobmsg_get_string(cur);
  2975. if (strcmp(str, "ignore")) {
  2976. char *e;
  2977. unsigned encap_limit = strtoul(str, &e, 0);
  2978. if (e == str || *e || encap_limit > 255) {
  2979. ret = -EINVAL;
  2980. goto failure;
  2981. }
  2982. nla_put_u8(nlm, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
  2983. tun_flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
  2984. }
  2985. }
  2986. #ifdef IFLA_IPTUN_FMR_MAX
  2987. if ((cur = tb_data[IPIP6_DATA_FMRS])) {
  2988. struct blob_attr *rcur;
  2989. unsigned rrem, fmrcnt = 0;
  2990. struct nlattr *fmrs = nla_nest_start(nlm, IFLA_IPTUN_FMRS);
  2991. if (!fmrs) {
  2992. ret = -ENOMEM;
  2993. goto failure;
  2994. }
  2995. blobmsg_for_each_attr(rcur, cur, rrem) {
  2996. struct blob_attr *tb_fmr[__FMR_DATA_ATTR_MAX], *tb_cur;
  2997. struct in6_addr ip6prefix;
  2998. struct in_addr ip4prefix;
  2999. unsigned ip4len, ip6len, ealen, offset;
  3000. blobmsg_parse(fmr_data_attr_list.params, __FMR_DATA_ATTR_MAX, tb_fmr,
  3001. blobmsg_data(rcur), blobmsg_len(rcur));
  3002. if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX6]) ||
  3003. !parse_ip_and_netmask(AF_INET6,
  3004. blobmsg_data(tb_cur), &ip6prefix,
  3005. &ip6len)) {
  3006. ret = -EINVAL;
  3007. goto failure;
  3008. }
  3009. if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX4]) ||
  3010. !parse_ip_and_netmask(AF_INET,
  3011. blobmsg_data(tb_cur), &ip4prefix,
  3012. &ip4len)) {
  3013. ret = -EINVAL;
  3014. goto failure;
  3015. }
  3016. if (!(tb_cur = tb_fmr[FMR_DATA_EALEN])) {
  3017. ret = -EINVAL;
  3018. goto failure;
  3019. }
  3020. ealen = blobmsg_get_u32(tb_cur);
  3021. if (!(tb_cur = tb_fmr[FMR_DATA_OFFSET])) {
  3022. ret = -EINVAL;
  3023. goto failure;
  3024. }
  3025. offset = blobmsg_get_u32(tb_cur);
  3026. struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt);
  3027. if (!rule) {
  3028. ret = -ENOMEM;
  3029. goto failure;
  3030. }
  3031. nla_put(nlm, IFLA_IPTUN_FMR_IP6_PREFIX, sizeof(ip6prefix), &ip6prefix);
  3032. nla_put(nlm, IFLA_IPTUN_FMR_IP4_PREFIX, sizeof(ip4prefix), &ip4prefix);
  3033. nla_put_u8(nlm, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, ip6len);
  3034. nla_put_u8(nlm, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, ip4len);
  3035. nla_put_u8(nlm, IFLA_IPTUN_FMR_EA_LEN, ealen);
  3036. nla_put_u8(nlm, IFLA_IPTUN_FMR_OFFSET, offset);
  3037. nla_nest_end(nlm, rule);
  3038. }
  3039. nla_nest_end(nlm, fmrs);
  3040. }
  3041. #endif
  3042. if (tun_flags)
  3043. nla_put_u32(nlm, IFLA_IPTUN_FLAGS, tun_flags);
  3044. }
  3045. nla_nest_end(nlm, infodata);
  3046. nla_nest_end(nlm, linkinfo);
  3047. return system_rtnl_call(nlm);
  3048. failure:
  3049. nlmsg_free(nlm);
  3050. return ret;
  3051. }
  3052. #endif
  3053. #ifdef IFLA_IPTUN_MAX
  3054. #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
  3055. static int system_add_gre_tunnel(const char *name, const char *kind,
  3056. const unsigned int link, struct blob_attr **tb, bool v6)
  3057. {
  3058. struct nl_msg *nlm;
  3059. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
  3060. struct blob_attr *cur;
  3061. uint32_t ikey = 0, okey = 0, flowinfo = 0, flags6 = IP6_TNL_F_IGN_ENCAP_LIMIT;
  3062. uint16_t iflags = 0, oflags = 0;
  3063. uint8_t tos = 0;
  3064. int ret = 0, ttl = 0;
  3065. unsigned encap_limit = 0;
  3066. nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  3067. if (!nlm)
  3068. return -1;
  3069. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  3070. nla_put_string(nlm, IFLA_IFNAME, name);
  3071. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  3072. if (!linkinfo) {
  3073. ret = -ENOMEM;
  3074. goto failure;
  3075. }
  3076. nla_put_string(nlm, IFLA_INFO_KIND, kind);
  3077. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  3078. if (!infodata) {
  3079. ret = -ENOMEM;
  3080. goto failure;
  3081. }
  3082. if (link)
  3083. nla_put_u32(nlm, IFLA_GRE_LINK, link);
  3084. if ((cur = tb[TUNNEL_ATTR_TTL]))
  3085. ttl = blobmsg_get_u32(cur);
  3086. if ((cur = tb[TUNNEL_ATTR_TOS])) {
  3087. char *str = blobmsg_get_string(cur);
  3088. if (strcmp(str, "inherit")) {
  3089. unsigned uval;
  3090. if (!system_tos_aton(str, &uval)) {
  3091. ret = -EINVAL;
  3092. goto failure;
  3093. }
  3094. if (v6)
  3095. flowinfo |= htonl(uval << 20) & IP6_FLOWINFO_TCLASS;
  3096. else
  3097. tos = uval;
  3098. } else {
  3099. if (v6)
  3100. flags6 |= IP6_TNL_F_USE_ORIG_TCLASS;
  3101. else
  3102. tos = 1;
  3103. }
  3104. }
  3105. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  3106. struct blob_attr *tb_data[__GRE_DATA_ATTR_MAX];
  3107. blobmsg_parse(gre_data_attr_list.params, __GRE_DATA_ATTR_MAX, tb_data,
  3108. blobmsg_data(cur), blobmsg_len(cur));
  3109. if ((cur = tb_data[GRE_DATA_IKEY])) {
  3110. if ((ikey = blobmsg_get_u32(cur)))
  3111. iflags |= GRE_KEY;
  3112. }
  3113. if ((cur = tb_data[GRE_DATA_OKEY])) {
  3114. if ((okey = blobmsg_get_u32(cur)))
  3115. oflags |= GRE_KEY;
  3116. }
  3117. if ((cur = tb_data[GRE_DATA_ICSUM])) {
  3118. if (blobmsg_get_bool(cur))
  3119. iflags |= GRE_CSUM;
  3120. }
  3121. if ((cur = tb_data[GRE_DATA_OCSUM])) {
  3122. if (blobmsg_get_bool(cur))
  3123. oflags |= GRE_CSUM;
  3124. }
  3125. if ((cur = tb_data[GRE_DATA_ISEQNO])) {
  3126. if (blobmsg_get_bool(cur))
  3127. iflags |= GRE_SEQ;
  3128. }
  3129. if ((cur = tb_data[GRE_DATA_OSEQNO])) {
  3130. if (blobmsg_get_bool(cur))
  3131. oflags |= GRE_SEQ;
  3132. }
  3133. if ((cur = tb_data[GRE_DATA_ENCAPLIMIT])) {
  3134. char *str = blobmsg_get_string(cur);
  3135. if (strcmp(str, "ignore")) {
  3136. char *e;
  3137. encap_limit = strtoul(str, &e, 0);
  3138. if (e == str || *e || encap_limit > 255) {
  3139. ret = -EINVAL;
  3140. goto failure;
  3141. }
  3142. flags6 &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
  3143. }
  3144. }
  3145. }
  3146. if (v6) {
  3147. struct in6_addr in6buf;
  3148. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  3149. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  3150. ret = -EINVAL;
  3151. goto failure;
  3152. }
  3153. nla_put(nlm, IFLA_GRE_LOCAL, sizeof(in6buf), &in6buf);
  3154. }
  3155. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  3156. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  3157. ret = -EINVAL;
  3158. goto failure;
  3159. }
  3160. nla_put(nlm, IFLA_GRE_REMOTE, sizeof(in6buf), &in6buf);
  3161. }
  3162. if (!(flags6 & IP6_TNL_F_IGN_ENCAP_LIMIT))
  3163. nla_put_u8(nlm, IFLA_GRE_ENCAP_LIMIT, encap_limit);
  3164. if (flowinfo)
  3165. nla_put_u32(nlm, IFLA_GRE_FLOWINFO, flowinfo);
  3166. if (flags6)
  3167. nla_put_u32(nlm, IFLA_GRE_FLAGS, flags6);
  3168. if (!ttl)
  3169. ttl = 64;
  3170. } else {
  3171. struct in_addr inbuf;
  3172. bool set_df = true;
  3173. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  3174. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  3175. ret = -EINVAL;
  3176. goto failure;
  3177. }
  3178. nla_put(nlm, IFLA_GRE_LOCAL, sizeof(inbuf), &inbuf);
  3179. }
  3180. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  3181. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  3182. ret = -EINVAL;
  3183. goto failure;
  3184. }
  3185. nla_put(nlm, IFLA_GRE_REMOTE, sizeof(inbuf), &inbuf);
  3186. if (IN_MULTICAST(ntohl(inbuf.s_addr))) {
  3187. if (!okey) {
  3188. okey = inbuf.s_addr;
  3189. oflags |= GRE_KEY;
  3190. }
  3191. if (!ikey) {
  3192. ikey = inbuf.s_addr;
  3193. iflags |= GRE_KEY;
  3194. }
  3195. }
  3196. }
  3197. if ((cur = tb[TUNNEL_ATTR_DF]))
  3198. set_df = blobmsg_get_bool(cur);
  3199. if (!set_df) {
  3200. /* ttl != 0 and nopmtudisc are incompatible */
  3201. if (ttl) {
  3202. ret = -EINVAL;
  3203. goto failure;
  3204. }
  3205. } else if (!ttl)
  3206. ttl = 64;
  3207. nla_put_u8(nlm, IFLA_GRE_PMTUDISC, set_df ? 1 : 0);
  3208. nla_put_u8(nlm, IFLA_GRE_TOS, tos);
  3209. }
  3210. if (ttl)
  3211. nla_put_u8(nlm, IFLA_GRE_TTL, ttl);
  3212. if (oflags)
  3213. nla_put_u16(nlm, IFLA_GRE_OFLAGS, oflags);
  3214. if (iflags)
  3215. nla_put_u16(nlm, IFLA_GRE_IFLAGS, iflags);
  3216. if (okey)
  3217. nla_put_u32(nlm, IFLA_GRE_OKEY, htonl(okey));
  3218. if (ikey)
  3219. nla_put_u32(nlm, IFLA_GRE_IKEY, htonl(ikey));
  3220. nla_nest_end(nlm, infodata);
  3221. nla_nest_end(nlm, linkinfo);
  3222. return system_rtnl_call(nlm);
  3223. failure:
  3224. nlmsg_free(nlm);
  3225. return ret;
  3226. }
  3227. #endif
  3228. #ifdef IFLA_VTI_MAX
  3229. static int system_add_vti_tunnel(const char *name, const char *kind,
  3230. const unsigned int link, struct blob_attr **tb, bool v6)
  3231. {
  3232. struct nl_msg *nlm;
  3233. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
  3234. struct blob_attr *cur;
  3235. int ret = 0;
  3236. nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  3237. if (!nlm)
  3238. return -1;
  3239. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  3240. nla_put_string(nlm, IFLA_IFNAME, name);
  3241. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  3242. if (!linkinfo) {
  3243. ret = -ENOMEM;
  3244. goto failure;
  3245. }
  3246. nla_put_string(nlm, IFLA_INFO_KIND, kind);
  3247. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  3248. if (!infodata) {
  3249. ret = -ENOMEM;
  3250. goto failure;
  3251. }
  3252. if (link)
  3253. nla_put_u32(nlm, IFLA_VTI_LINK, link);
  3254. if (v6) {
  3255. struct in6_addr in6buf;
  3256. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  3257. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  3258. ret = -EINVAL;
  3259. goto failure;
  3260. }
  3261. nla_put(nlm, IFLA_VTI_LOCAL, sizeof(in6buf), &in6buf);
  3262. }
  3263. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  3264. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  3265. ret = -EINVAL;
  3266. goto failure;
  3267. }
  3268. nla_put(nlm, IFLA_VTI_REMOTE, sizeof(in6buf), &in6buf);
  3269. }
  3270. } else {
  3271. struct in_addr inbuf;
  3272. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  3273. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  3274. ret = -EINVAL;
  3275. goto failure;
  3276. }
  3277. nla_put(nlm, IFLA_VTI_LOCAL, sizeof(inbuf), &inbuf);
  3278. }
  3279. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  3280. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  3281. ret = -EINVAL;
  3282. goto failure;
  3283. }
  3284. nla_put(nlm, IFLA_VTI_REMOTE, sizeof(inbuf), &inbuf);
  3285. }
  3286. }
  3287. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  3288. struct blob_attr *tb_data[__VTI_DATA_ATTR_MAX];
  3289. uint32_t ikey = 0, okey = 0;
  3290. blobmsg_parse(vti_data_attr_list.params, __VTI_DATA_ATTR_MAX, tb_data,
  3291. blobmsg_data(cur), blobmsg_len(cur));
  3292. if ((cur = tb_data[VTI_DATA_IKEY])) {
  3293. if ((ikey = blobmsg_get_u32(cur)))
  3294. nla_put_u32(nlm, IFLA_VTI_IKEY, htonl(ikey));
  3295. }
  3296. if ((cur = tb_data[VTI_DATA_OKEY])) {
  3297. if ((okey = blobmsg_get_u32(cur)))
  3298. nla_put_u32(nlm, IFLA_VTI_OKEY, htonl(okey));
  3299. }
  3300. }
  3301. nla_nest_end(nlm, infodata);
  3302. nla_nest_end(nlm, linkinfo);
  3303. return system_rtnl_call(nlm);
  3304. failure:
  3305. nlmsg_free(nlm);
  3306. return ret;
  3307. }
  3308. #endif
  3309. #ifdef IFLA_XFRM_MAX
  3310. static int system_add_xfrm_tunnel(const char *name, const char *kind,
  3311. const unsigned int link, struct blob_attr **tb)
  3312. {
  3313. struct nl_msg *nlm;
  3314. struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
  3315. struct blob_attr *cur;
  3316. int ret = 0;
  3317. nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
  3318. if (!nlm)
  3319. return -1;
  3320. nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
  3321. nla_put_string(nlm, IFLA_IFNAME, name);
  3322. struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
  3323. if (!linkinfo) {
  3324. ret = -ENOMEM;
  3325. goto failure;
  3326. }
  3327. nla_put_string(nlm, IFLA_INFO_KIND, kind);
  3328. struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
  3329. if (!infodata) {
  3330. ret = -ENOMEM;
  3331. goto failure;
  3332. }
  3333. if (link)
  3334. nla_put_u32(nlm, IFLA_XFRM_LINK, link);
  3335. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  3336. struct blob_attr *tb_data[__XFRM_DATA_ATTR_MAX];
  3337. uint32_t if_id = 0;
  3338. blobmsg_parse(xfrm_data_attr_list.params, __XFRM_DATA_ATTR_MAX, tb_data,
  3339. blobmsg_data(cur), blobmsg_len(cur));
  3340. if ((cur = tb_data[XFRM_DATA_IF_ID])) {
  3341. if ((if_id = blobmsg_get_u32(cur)))
  3342. nla_put_u32(nlm, IFLA_XFRM_IF_ID, if_id);
  3343. }
  3344. }
  3345. nla_nest_end(nlm, infodata);
  3346. nla_nest_end(nlm, linkinfo);
  3347. return system_rtnl_call(nlm);
  3348. failure:
  3349. nlmsg_free(nlm);
  3350. return ret;
  3351. }
  3352. #endif
  3353. #ifdef IFLA_VXLAN_MAX
  3354. static void system_vxlan_map_bool_attr(struct nl_msg *msg, struct blob_attr **tb_data, int attrtype, int vxlandatatype, bool invert) {
  3355. struct blob_attr *cur;
  3356. if ((cur = tb_data[vxlandatatype])) {
  3357. bool val = blobmsg_get_bool(cur);
  3358. if (invert)
  3359. val = !val;
  3360. if ((attrtype == IFLA_VXLAN_GBP) && val)
  3361. nla_put_flag(msg, attrtype);
  3362. else
  3363. nla_put_u8(msg, attrtype, val);
  3364. }
  3365. }
  3366. static int system_add_vxlan(const char *name, const unsigned int link, struct blob_attr **tb, bool v6)
  3367. {
  3368. struct blob_attr *tb_data[__VXLAN_DATA_ATTR_MAX];
  3369. struct nl_msg *msg;
  3370. struct nlattr *linkinfo, *data;
  3371. struct ifinfomsg iim = { .ifi_family = AF_UNSPEC, };
  3372. struct blob_attr *cur;
  3373. int ret = 0;
  3374. if ((cur = tb[TUNNEL_ATTR_DATA]))
  3375. blobmsg_parse(vxlan_data_attr_list.params, __VXLAN_DATA_ATTR_MAX, tb_data,
  3376. blobmsg_data(cur), blobmsg_len(cur));
  3377. else
  3378. return -EINVAL;
  3379. msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
  3380. if (!msg)
  3381. return -1;
  3382. nlmsg_append(msg, &iim, sizeof(iim), 0);
  3383. nla_put_string(msg, IFLA_IFNAME, name);
  3384. if ((cur = tb_data[VXLAN_DATA_ATTR_MACADDR])) {
  3385. struct ether_addr *ea = ether_aton(blobmsg_get_string(cur));
  3386. if (!ea) {
  3387. ret = -EINVAL;
  3388. goto failure;
  3389. }
  3390. nla_put(msg, IFLA_ADDRESS, ETH_ALEN, ea);
  3391. }
  3392. if ((cur = tb[TUNNEL_ATTR_MTU])) {
  3393. uint32_t mtu = blobmsg_get_u32(cur);
  3394. nla_put_u32(msg, IFLA_MTU, mtu);
  3395. }
  3396. if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO))) {
  3397. ret = -ENOMEM;
  3398. goto failure;
  3399. }
  3400. nla_put_string(msg, IFLA_INFO_KIND, "vxlan");
  3401. if (!(data = nla_nest_start(msg, IFLA_INFO_DATA))) {
  3402. ret = -ENOMEM;
  3403. goto failure;
  3404. }
  3405. if (link)
  3406. nla_put_u32(msg, IFLA_VXLAN_LINK, link);
  3407. if ((cur = tb_data[VXLAN_DATA_ATTR_ID])) {
  3408. uint32_t id = blobmsg_get_u32(cur);
  3409. if (id >= (1u << 24) - 1) {
  3410. ret = -EINVAL;
  3411. goto failure;
  3412. }
  3413. nla_put_u32(msg, IFLA_VXLAN_ID, id);
  3414. }
  3415. if (v6) {
  3416. struct in6_addr in6buf;
  3417. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  3418. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  3419. ret = -EINVAL;
  3420. goto failure;
  3421. }
  3422. nla_put(msg, IFLA_VXLAN_LOCAL6, sizeof(in6buf), &in6buf);
  3423. }
  3424. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  3425. if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
  3426. ret = -EINVAL;
  3427. goto failure;
  3428. }
  3429. nla_put(msg, IFLA_VXLAN_GROUP6, sizeof(in6buf), &in6buf);
  3430. }
  3431. } else {
  3432. struct in_addr inbuf;
  3433. if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
  3434. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  3435. ret = -EINVAL;
  3436. goto failure;
  3437. }
  3438. nla_put(msg, IFLA_VXLAN_LOCAL, sizeof(inbuf), &inbuf);
  3439. }
  3440. if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
  3441. if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
  3442. ret = -EINVAL;
  3443. goto failure;
  3444. }
  3445. nla_put(msg, IFLA_VXLAN_GROUP, sizeof(inbuf), &inbuf);
  3446. }
  3447. }
  3448. uint32_t port = 4789;
  3449. if ((cur = tb_data[VXLAN_DATA_ATTR_PORT])) {
  3450. port = blobmsg_get_u32(cur);
  3451. if (port < 1 || port > 65535) {
  3452. ret = -EINVAL;
  3453. goto failure;
  3454. }
  3455. }
  3456. nla_put_u16(msg, IFLA_VXLAN_PORT, htons(port));
  3457. if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMIN])) {
  3458. struct ifla_vxlan_port_range srcports = {0,0};
  3459. uint32_t low = blobmsg_get_u32(cur);
  3460. if (low < 1 || low > 65535 - 1) {
  3461. ret = -EINVAL;
  3462. goto failure;
  3463. }
  3464. srcports.low = htons((uint16_t) low);
  3465. srcports.high = htons((uint16_t) (low+1));
  3466. if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMAX])) {
  3467. uint32_t high = blobmsg_get_u32(cur);
  3468. if (high < 1 || high > 65535) {
  3469. ret = -EINVAL;
  3470. goto failure;
  3471. }
  3472. if (high > low)
  3473. srcports.high = htons((uint16_t) high);
  3474. }
  3475. nla_put(msg, IFLA_VXLAN_PORT_RANGE, sizeof(srcports), &srcports);
  3476. }
  3477. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_CSUM, VXLAN_DATA_ATTR_TXCSUM, false);
  3478. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, VXLAN_DATA_ATTR_RXCSUM, true);
  3479. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, VXLAN_DATA_ATTR_TXCSUM, true);
  3480. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_LEARNING, VXLAN_DATA_ATTR_LEARNING, false);
  3481. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_RSC , VXLAN_DATA_ATTR_RSC, false);
  3482. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_PROXY , VXLAN_DATA_ATTR_PROXY, false);
  3483. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L2MISS , VXLAN_DATA_ATTR_L2MISS, false);
  3484. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L3MISS , VXLAN_DATA_ATTR_L3MISS, false);
  3485. system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_GBP , VXLAN_DATA_ATTR_GBP, false);
  3486. if ((cur = tb_data[VXLAN_DATA_ATTR_AGEING])) {
  3487. uint32_t ageing = blobmsg_get_u32(cur);
  3488. nla_put_u32(msg, IFLA_VXLAN_AGEING, ageing);
  3489. }
  3490. if ((cur = tb_data[VXLAN_DATA_ATTR_LIMIT])) {
  3491. uint32_t maxaddress = blobmsg_get_u32(cur);
  3492. nla_put_u32(msg, IFLA_VXLAN_LIMIT, maxaddress);
  3493. }
  3494. if ((cur = tb[TUNNEL_ATTR_TOS])) {
  3495. char *str = blobmsg_get_string(cur);
  3496. unsigned tos = 1;
  3497. if (strcmp(str, "inherit")) {
  3498. if (!system_tos_aton(str, &tos)) {
  3499. ret = -EINVAL;
  3500. goto failure;
  3501. }
  3502. }
  3503. nla_put_u8(msg, IFLA_VXLAN_TOS, tos);
  3504. }
  3505. if ((cur = tb[TUNNEL_ATTR_TTL])) {
  3506. uint32_t ttl = blobmsg_get_u32(cur);
  3507. if (ttl < 1 || ttl > 255) {
  3508. ret = -EINVAL;
  3509. goto failure;
  3510. }
  3511. nla_put_u8(msg, IFLA_VXLAN_TTL, ttl);
  3512. }
  3513. nla_nest_end(msg, data);
  3514. nla_nest_end(msg, linkinfo);
  3515. ret = system_rtnl_call(msg);
  3516. if (ret)
  3517. D(SYSTEM, "Error adding vxlan '%s': %d", name, ret);
  3518. return ret;
  3519. failure:
  3520. nlmsg_free(msg);
  3521. return ret;
  3522. }
  3523. #endif
  3524. static int system_add_sit_tunnel(const char *name, const unsigned int link, struct blob_attr **tb)
  3525. {
  3526. struct blob_attr *cur;
  3527. int ret = 0;
  3528. if (system_add_proto_tunnel(name, IPPROTO_IPV6, link, tb) < 0)
  3529. return -1;
  3530. #ifdef SIOCADD6RD
  3531. if ((cur = tb[TUNNEL_ATTR_DATA])) {
  3532. struct blob_attr *tb_data[__SIXRD_DATA_ATTR_MAX];
  3533. unsigned int mask;
  3534. struct ip_tunnel_6rd p6;
  3535. blobmsg_parse(sixrd_data_attr_list.params, __SIXRD_DATA_ATTR_MAX, tb_data,
  3536. blobmsg_data(cur), blobmsg_len(cur));
  3537. memset(&p6, 0, sizeof(p6));
  3538. if ((cur = tb_data[SIXRD_DATA_PREFIX])) {
  3539. if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
  3540. &p6.prefix, &mask) || mask > 128) {
  3541. ret = -EINVAL;
  3542. goto failure;
  3543. }
  3544. p6.prefixlen = mask;
  3545. }
  3546. if ((cur = tb_data[SIXRD_DATA_RELAY_PREFIX])) {
  3547. if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
  3548. &p6.relay_prefix, &mask) || mask > 32) {
  3549. ret = -EINVAL;
  3550. goto failure;
  3551. }
  3552. p6.relay_prefixlen = mask;
  3553. }
  3554. if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
  3555. ret = -1;
  3556. goto failure;
  3557. }
  3558. }
  3559. #endif
  3560. return ret;
  3561. failure:
  3562. system_link_del(name);
  3563. return ret;
  3564. }
  3565. static int system_add_proto_tunnel(const char *name, const uint8_t proto, const unsigned int link, struct blob_attr **tb)
  3566. {
  3567. struct blob_attr *cur;
  3568. bool set_df = true;
  3569. struct ip_tunnel_parm p = {
  3570. .link = link,
  3571. .iph = {
  3572. .version = 4,
  3573. .ihl = 5,
  3574. .protocol = proto,
  3575. }
  3576. };
  3577. if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
  3578. inet_pton(AF_INET, blobmsg_data(cur), &p.iph.saddr) < 1)
  3579. return -EINVAL;
  3580. if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
  3581. inet_pton(AF_INET, blobmsg_data(cur), &p.iph.daddr) < 1)
  3582. return -EINVAL;
  3583. if ((cur = tb[TUNNEL_ATTR_DF]))
  3584. set_df = blobmsg_get_bool(cur);
  3585. if ((cur = tb[TUNNEL_ATTR_TTL]))
  3586. p.iph.ttl = blobmsg_get_u32(cur);
  3587. if ((cur = tb[TUNNEL_ATTR_TOS])) {
  3588. char *str = blobmsg_get_string(cur);
  3589. if (strcmp(str, "inherit")) {
  3590. unsigned uval;
  3591. if (!system_tos_aton(str, &uval))
  3592. return -EINVAL;
  3593. p.iph.tos = uval;
  3594. } else
  3595. p.iph.tos = 1;
  3596. }
  3597. p.iph.frag_off = set_df ? htons(IP_DF) : 0;
  3598. /* ttl !=0 and nopmtudisc are incompatible */
  3599. if (p.iph.ttl && p.iph.frag_off == 0)
  3600. return -EINVAL;
  3601. strncpy(p.name, name, sizeof(p.name) - 1);
  3602. switch (p.iph.protocol) {
  3603. case IPPROTO_IPIP:
  3604. return tunnel_ioctl("tunl0", SIOCADDTUNNEL, &p);
  3605. case IPPROTO_IPV6:
  3606. return tunnel_ioctl("sit0", SIOCADDTUNNEL, &p);
  3607. default:
  3608. break;
  3609. }
  3610. return -1;
  3611. }
  3612. int system_del_ip_tunnel(const struct device *dev)
  3613. {
  3614. return system_link_del(dev->ifname);
  3615. }
  3616. int system_update_ipv6_mtu(struct device *dev, int mtu)
  3617. {
  3618. int ret = -1;
  3619. char buf[64];
  3620. int fd;
  3621. fd = open(dev_sysctl_path("ipv6/conf", dev->ifname, "mtu"), O_RDWR);
  3622. if (fd < 0)
  3623. return ret;
  3624. if (!mtu) {
  3625. ssize_t len = read(fd, buf, sizeof(buf) - 1);
  3626. if (len < 0)
  3627. goto out;
  3628. buf[len] = 0;
  3629. ret = atoi(buf);
  3630. } else {
  3631. if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) > 0)
  3632. ret = mtu;
  3633. }
  3634. out:
  3635. close(fd);
  3636. return ret;
  3637. }
  3638. int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr)
  3639. {
  3640. struct blob_attr *tb[__TUNNEL_ATTR_MAX];
  3641. struct blob_attr *cur;
  3642. const char *str;
  3643. blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
  3644. blob_data(attr), blob_len(attr));
  3645. system_link_del(dev->ifname);
  3646. if (!(cur = tb[TUNNEL_ATTR_TYPE]))
  3647. return -EINVAL;
  3648. str = blobmsg_data(cur);
  3649. unsigned int ttl = 0;
  3650. if ((cur = tb[TUNNEL_ATTR_TTL])) {
  3651. ttl = blobmsg_get_u32(cur);
  3652. if (ttl > 255)
  3653. return -EINVAL;
  3654. }
  3655. unsigned int link = 0;
  3656. if ((cur = tb[TUNNEL_ATTR_LINK])) {
  3657. struct interface *iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
  3658. if (!iface)
  3659. return -EINVAL;
  3660. if (iface->l3_dev.dev)
  3661. link = iface->l3_dev.dev->ifindex;
  3662. }
  3663. if (!strcmp(str, "sit"))
  3664. return system_add_sit_tunnel(dev->ifname, link, tb);
  3665. #ifdef IFLA_IPTUN_MAX
  3666. else if (!strcmp(str, "ipip6")) {
  3667. return system_add_ip6_tunnel(dev->ifname, link, tb);
  3668. } else if (!strcmp(str, "greip")) {
  3669. return system_add_gre_tunnel(dev->ifname, "gre", link, tb, false);
  3670. } else if (!strcmp(str, "gretapip")) {
  3671. return system_add_gre_tunnel(dev->ifname, "gretap", link, tb, false);
  3672. } else if (!strcmp(str, "greip6")) {
  3673. return system_add_gre_tunnel(dev->ifname, "ip6gre", link, tb, true);
  3674. } else if (!strcmp(str, "gretapip6")) {
  3675. return system_add_gre_tunnel(dev->ifname, "ip6gretap", link, tb, true);
  3676. #ifdef IFLA_VTI_MAX
  3677. } else if (!strcmp(str, "vtiip")) {
  3678. return system_add_vti_tunnel(dev->ifname, "vti", link, tb, false);
  3679. } else if (!strcmp(str, "vtiip6")) {
  3680. return system_add_vti_tunnel(dev->ifname, "vti6", link, tb, true);
  3681. #endif
  3682. #ifdef IFLA_XFRM_MAX
  3683. } else if (!strcmp(str, "xfrm")) {
  3684. return system_add_xfrm_tunnel(dev->ifname, "xfrm", link, tb);
  3685. #endif
  3686. #ifdef IFLA_VXLAN_MAX
  3687. } else if(!strcmp(str, "vxlan")) {
  3688. return system_add_vxlan(dev->ifname, link, tb, false);
  3689. } else if(!strcmp(str, "vxlan6")) {
  3690. return system_add_vxlan(dev->ifname, link, tb, true);
  3691. #endif
  3692. #endif
  3693. } else if (!strcmp(str, "ipip")) {
  3694. return system_add_proto_tunnel(dev->ifname, IPPROTO_IPIP, link, tb);
  3695. }
  3696. else
  3697. return -EINVAL;
  3698. return 0;
  3699. }