system-linux.c 99 KB

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