af_mpls.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/skbuff.h>
  4. #include <linux/socket.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/net.h>
  7. #include <linux/module.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/ipv6.h>
  10. #include <linux/mpls.h>
  11. #include <linux/netconf.h>
  12. #include <linux/nospec.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/percpu.h>
  15. #include <net/ip.h>
  16. #include <net/dst.h>
  17. #include <net/sock.h>
  18. #include <net/arp.h>
  19. #include <net/ip_fib.h>
  20. #include <net/netevent.h>
  21. #include <net/ip_tunnels.h>
  22. #include <net/netns/generic.h>
  23. #if IS_ENABLED(CONFIG_IPV6)
  24. #include <net/ipv6.h>
  25. #endif
  26. #include <net/ipv6_stubs.h>
  27. #include <net/rtnh.h>
  28. #include "internal.h"
  29. /* max memory we will use for mpls_route */
  30. #define MAX_MPLS_ROUTE_MEM 4096
  31. /* Maximum number of labels to look ahead at when selecting a path of
  32. * a multipath route
  33. */
  34. #define MAX_MP_SELECT_LABELS 4
  35. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  36. static int label_limit = (1 << 20) - 1;
  37. static int ttl_max = 255;
  38. #if IS_ENABLED(CONFIG_NET_IP_TUNNEL)
  39. static size_t ipgre_mpls_encap_hlen(struct ip_tunnel_encap *e)
  40. {
  41. return sizeof(struct mpls_shim_hdr);
  42. }
  43. static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
  44. .encap_hlen = ipgre_mpls_encap_hlen,
  45. };
  46. static int ipgre_tunnel_encap_add_mpls_ops(void)
  47. {
  48. return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  49. }
  50. static void ipgre_tunnel_encap_del_mpls_ops(void)
  51. {
  52. ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  53. }
  54. #else
  55. static int ipgre_tunnel_encap_add_mpls_ops(void)
  56. {
  57. return 0;
  58. }
  59. static void ipgre_tunnel_encap_del_mpls_ops(void)
  60. {
  61. }
  62. #endif
  63. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  64. struct nlmsghdr *nlh, struct net *net, u32 portid,
  65. unsigned int nlm_flags);
  66. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  67. {
  68. struct mpls_route *rt = NULL;
  69. if (index < net->mpls.platform_labels) {
  70. struct mpls_route __rcu **platform_label =
  71. rcu_dereference(net->mpls.platform_label);
  72. rt = rcu_dereference(platform_label[index]);
  73. }
  74. return rt;
  75. }
  76. bool mpls_output_possible(const struct net_device *dev)
  77. {
  78. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  79. }
  80. EXPORT_SYMBOL_GPL(mpls_output_possible);
  81. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  82. {
  83. return (u8 *)nh + rt->rt_via_offset;
  84. }
  85. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  86. const struct mpls_nh *nh)
  87. {
  88. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  89. }
  90. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  91. {
  92. /* The size of the layer 2.5 labels to be added for this route */
  93. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  94. }
  95. unsigned int mpls_dev_mtu(const struct net_device *dev)
  96. {
  97. /* The amount of data the layer 2 frame can hold */
  98. return dev->mtu;
  99. }
  100. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  101. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  102. {
  103. if (skb->len <= mtu)
  104. return false;
  105. if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
  106. return false;
  107. return true;
  108. }
  109. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  110. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  111. const struct sk_buff *skb)
  112. {
  113. struct mpls_dev *mdev;
  114. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  115. mdev = mpls_dev_get(dev);
  116. if (mdev)
  117. MPLS_INC_STATS_LEN(mdev, skb->len,
  118. tx_packets,
  119. tx_bytes);
  120. } else if (skb->protocol == htons(ETH_P_IP)) {
  121. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  122. #if IS_ENABLED(CONFIG_IPV6)
  123. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  124. struct inet6_dev *in6dev = __in6_dev_get(dev);
  125. if (in6dev)
  126. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  127. IPSTATS_MIB_OUT, skb->len);
  128. #endif
  129. }
  130. }
  131. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  132. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  133. {
  134. struct mpls_entry_decoded dec;
  135. unsigned int mpls_hdr_len = 0;
  136. struct mpls_shim_hdr *hdr;
  137. bool eli_seen = false;
  138. int label_index;
  139. u32 hash = 0;
  140. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  141. label_index++) {
  142. mpls_hdr_len += sizeof(*hdr);
  143. if (!pskb_may_pull(skb, mpls_hdr_len))
  144. break;
  145. /* Read and decode the current label */
  146. hdr = mpls_hdr(skb) + label_index;
  147. dec = mpls_entry_decode(hdr);
  148. /* RFC6790 - reserved labels MUST NOT be used as keys
  149. * for the load-balancing function
  150. */
  151. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  152. hash = jhash_1word(dec.label, hash);
  153. /* The entropy label follows the entropy label
  154. * indicator, so this means that the entropy
  155. * label was just added to the hash - no need to
  156. * go any deeper either in the label stack or in the
  157. * payload
  158. */
  159. if (eli_seen)
  160. break;
  161. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  162. eli_seen = true;
  163. }
  164. if (!dec.bos)
  165. continue;
  166. /* found bottom label; does skb have room for a header? */
  167. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  168. const struct iphdr *v4hdr;
  169. v4hdr = (const struct iphdr *)(hdr + 1);
  170. if (v4hdr->version == 4) {
  171. hash = jhash_3words(ntohl(v4hdr->saddr),
  172. ntohl(v4hdr->daddr),
  173. v4hdr->protocol, hash);
  174. } else if (v4hdr->version == 6 &&
  175. pskb_may_pull(skb, mpls_hdr_len +
  176. sizeof(struct ipv6hdr))) {
  177. const struct ipv6hdr *v6hdr;
  178. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  179. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  180. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  181. hash = jhash_1word(v6hdr->nexthdr, hash);
  182. }
  183. }
  184. break;
  185. }
  186. return hash;
  187. }
  188. static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
  189. {
  190. return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
  191. }
  192. /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
  193. * a next hop (nh->nh_flags) are modified by netdev event handlers.
  194. * Since those fields can change at any moment, use READ_ONCE to
  195. * access both.
  196. */
  197. static const struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  198. struct sk_buff *skb)
  199. {
  200. u32 hash = 0;
  201. int nh_index = 0;
  202. int n = 0;
  203. u8 alive;
  204. /* No need to look further into packet if there's only
  205. * one path
  206. */
  207. if (rt->rt_nhn == 1)
  208. return rt->rt_nh;
  209. alive = READ_ONCE(rt->rt_nhn_alive);
  210. if (alive == 0)
  211. return NULL;
  212. hash = mpls_multipath_hash(rt, skb);
  213. nh_index = hash % alive;
  214. if (alive == rt->rt_nhn)
  215. goto out;
  216. for_nexthops(rt) {
  217. unsigned int nh_flags = READ_ONCE(nh->nh_flags);
  218. if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  219. continue;
  220. if (n == nh_index)
  221. return nh;
  222. n++;
  223. } endfor_nexthops(rt);
  224. out:
  225. return mpls_get_nexthop(rt, nh_index);
  226. }
  227. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  228. struct sk_buff *skb, struct mpls_entry_decoded dec)
  229. {
  230. enum mpls_payload_type payload_type;
  231. bool success = false;
  232. /* The IPv4 code below accesses through the IPv4 header
  233. * checksum, which is 12 bytes into the packet.
  234. * The IPv6 code below accesses through the IPv6 hop limit
  235. * which is 8 bytes into the packet.
  236. *
  237. * For all supported cases there should always be at least 12
  238. * bytes of packet data present. The IPv4 header is 20 bytes
  239. * without options and the IPv6 header is always 40 bytes
  240. * long.
  241. */
  242. if (!pskb_may_pull(skb, 12))
  243. return false;
  244. payload_type = rt->rt_payload_type;
  245. if (payload_type == MPT_UNSPEC)
  246. payload_type = ip_hdr(skb)->version;
  247. switch (payload_type) {
  248. case MPT_IPV4: {
  249. struct iphdr *hdr4 = ip_hdr(skb);
  250. u8 new_ttl;
  251. skb->protocol = htons(ETH_P_IP);
  252. /* If propagating TTL, take the decremented TTL from
  253. * the incoming MPLS header, otherwise decrement the
  254. * TTL, but only if not 0 to avoid underflow.
  255. */
  256. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  257. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  258. net->mpls.ip_ttl_propagate))
  259. new_ttl = dec.ttl;
  260. else
  261. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  262. csum_replace2(&hdr4->check,
  263. htons(hdr4->ttl << 8),
  264. htons(new_ttl << 8));
  265. hdr4->ttl = new_ttl;
  266. success = true;
  267. break;
  268. }
  269. case MPT_IPV6: {
  270. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  271. skb->protocol = htons(ETH_P_IPV6);
  272. /* If propagating TTL, take the decremented TTL from
  273. * the incoming MPLS header, otherwise decrement the
  274. * hop limit, but only if not 0 to avoid underflow.
  275. */
  276. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  277. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  278. net->mpls.ip_ttl_propagate))
  279. hdr6->hop_limit = dec.ttl;
  280. else if (hdr6->hop_limit)
  281. hdr6->hop_limit = hdr6->hop_limit - 1;
  282. success = true;
  283. break;
  284. }
  285. case MPT_UNSPEC:
  286. /* Should have decided which protocol it is by now */
  287. break;
  288. }
  289. return success;
  290. }
  291. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  292. struct packet_type *pt, struct net_device *orig_dev)
  293. {
  294. struct net *net = dev_net(dev);
  295. struct mpls_shim_hdr *hdr;
  296. const struct mpls_nh *nh;
  297. struct mpls_route *rt;
  298. struct mpls_entry_decoded dec;
  299. struct net_device *out_dev;
  300. struct mpls_dev *out_mdev;
  301. struct mpls_dev *mdev;
  302. unsigned int hh_len;
  303. unsigned int new_header_size;
  304. unsigned int mtu;
  305. int err;
  306. /* Careful this entire function runs inside of an rcu critical section */
  307. mdev = mpls_dev_get(dev);
  308. if (!mdev)
  309. goto drop;
  310. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  311. rx_bytes);
  312. if (!mdev->input_enabled) {
  313. MPLS_INC_STATS(mdev, rx_dropped);
  314. goto drop;
  315. }
  316. if (skb->pkt_type != PACKET_HOST)
  317. goto err;
  318. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  319. goto err;
  320. if (!pskb_may_pull(skb, sizeof(*hdr)))
  321. goto err;
  322. skb_dst_drop(skb);
  323. /* Read and decode the label */
  324. hdr = mpls_hdr(skb);
  325. dec = mpls_entry_decode(hdr);
  326. rt = mpls_route_input_rcu(net, dec.label);
  327. if (!rt) {
  328. MPLS_INC_STATS(mdev, rx_noroute);
  329. goto drop;
  330. }
  331. nh = mpls_select_multipath(rt, skb);
  332. if (!nh)
  333. goto err;
  334. /* Pop the label */
  335. skb_pull(skb, sizeof(*hdr));
  336. skb_reset_network_header(skb);
  337. skb_orphan(skb);
  338. if (skb_warn_if_lro(skb))
  339. goto err;
  340. skb_forward_csum(skb);
  341. /* Verify ttl is valid */
  342. if (dec.ttl <= 1)
  343. goto err;
  344. /* Find the output device */
  345. out_dev = nh->nh_dev;
  346. if (!mpls_output_possible(out_dev))
  347. goto tx_err;
  348. /* Verify the destination can hold the packet */
  349. new_header_size = mpls_nh_header_size(nh);
  350. mtu = mpls_dev_mtu(out_dev);
  351. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  352. goto tx_err;
  353. hh_len = LL_RESERVED_SPACE(out_dev);
  354. if (!out_dev->header_ops)
  355. hh_len = 0;
  356. /* Ensure there is enough space for the headers in the skb */
  357. if (skb_cow(skb, hh_len + new_header_size))
  358. goto tx_err;
  359. skb->dev = out_dev;
  360. skb->protocol = htons(ETH_P_MPLS_UC);
  361. dec.ttl -= 1;
  362. if (unlikely(!new_header_size && dec.bos)) {
  363. /* Penultimate hop popping */
  364. if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
  365. goto err;
  366. } else {
  367. bool bos;
  368. int i;
  369. skb_push(skb, new_header_size);
  370. skb_reset_network_header(skb);
  371. /* Push the new labels */
  372. hdr = mpls_hdr(skb);
  373. bos = dec.bos;
  374. for (i = nh->nh_labels - 1; i >= 0; i--) {
  375. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  376. dec.ttl, 0, bos);
  377. bos = false;
  378. }
  379. }
  380. mpls_stats_inc_outucastpkts(out_dev, skb);
  381. /* If via wasn't specified then send out using device address */
  382. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  383. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  384. out_dev->dev_addr, skb);
  385. else
  386. err = neigh_xmit(nh->nh_via_table, out_dev,
  387. mpls_nh_via(rt, nh), skb);
  388. if (err)
  389. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  390. __func__, err);
  391. return 0;
  392. tx_err:
  393. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  394. if (out_mdev)
  395. MPLS_INC_STATS(out_mdev, tx_errors);
  396. goto drop;
  397. err:
  398. MPLS_INC_STATS(mdev, rx_errors);
  399. drop:
  400. kfree_skb(skb);
  401. return NET_RX_DROP;
  402. }
  403. static struct packet_type mpls_packet_type __read_mostly = {
  404. .type = cpu_to_be16(ETH_P_MPLS_UC),
  405. .func = mpls_forward,
  406. };
  407. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  408. [RTA_DST] = { .type = NLA_U32 },
  409. [RTA_OIF] = { .type = NLA_U32 },
  410. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  411. };
  412. struct mpls_route_config {
  413. u32 rc_protocol;
  414. u32 rc_ifindex;
  415. u8 rc_via_table;
  416. u8 rc_via_alen;
  417. u8 rc_via[MAX_VIA_ALEN];
  418. u32 rc_label;
  419. u8 rc_ttl_propagate;
  420. u8 rc_output_labels;
  421. u32 rc_output_label[MAX_NEW_LABELS];
  422. u32 rc_nlflags;
  423. enum mpls_payload_type rc_payload_type;
  424. struct nl_info rc_nlinfo;
  425. struct rtnexthop *rc_mp;
  426. int rc_mp_len;
  427. };
  428. /* all nexthops within a route have the same size based on max
  429. * number of labels and max via length for a hop
  430. */
  431. static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
  432. {
  433. u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
  434. struct mpls_route *rt;
  435. size_t size;
  436. size = sizeof(*rt) + num_nh * nh_size;
  437. if (size > MAX_MPLS_ROUTE_MEM)
  438. return ERR_PTR(-EINVAL);
  439. rt = kzalloc(size, GFP_KERNEL);
  440. if (!rt)
  441. return ERR_PTR(-ENOMEM);
  442. rt->rt_nhn = num_nh;
  443. rt->rt_nhn_alive = num_nh;
  444. rt->rt_nh_size = nh_size;
  445. rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
  446. return rt;
  447. }
  448. static void mpls_rt_free(struct mpls_route *rt)
  449. {
  450. if (rt)
  451. kfree_rcu(rt, rt_rcu);
  452. }
  453. static void mpls_notify_route(struct net *net, unsigned index,
  454. struct mpls_route *old, struct mpls_route *new,
  455. const struct nl_info *info)
  456. {
  457. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  458. unsigned portid = info ? info->portid : 0;
  459. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  460. struct mpls_route *rt = new ? new : old;
  461. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  462. /* Ignore reserved labels for now */
  463. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  464. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  465. }
  466. static void mpls_route_update(struct net *net, unsigned index,
  467. struct mpls_route *new,
  468. const struct nl_info *info)
  469. {
  470. struct mpls_route __rcu **platform_label;
  471. struct mpls_route *rt;
  472. ASSERT_RTNL();
  473. platform_label = rtnl_dereference(net->mpls.platform_label);
  474. rt = rtnl_dereference(platform_label[index]);
  475. rcu_assign_pointer(platform_label[index], new);
  476. mpls_notify_route(net, index, rt, new, info);
  477. /* If we removed a route free it now */
  478. mpls_rt_free(rt);
  479. }
  480. static unsigned find_free_label(struct net *net)
  481. {
  482. struct mpls_route __rcu **platform_label;
  483. size_t platform_labels;
  484. unsigned index;
  485. platform_label = rtnl_dereference(net->mpls.platform_label);
  486. platform_labels = net->mpls.platform_labels;
  487. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  488. index++) {
  489. if (!rtnl_dereference(platform_label[index]))
  490. return index;
  491. }
  492. return LABEL_NOT_SPECIFIED;
  493. }
  494. #if IS_ENABLED(CONFIG_INET)
  495. static struct net_device *inet_fib_lookup_dev(struct net *net,
  496. const void *addr)
  497. {
  498. struct net_device *dev;
  499. struct rtable *rt;
  500. struct in_addr daddr;
  501. memcpy(&daddr, addr, sizeof(struct in_addr));
  502. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  503. if (IS_ERR(rt))
  504. return ERR_CAST(rt);
  505. dev = rt->dst.dev;
  506. dev_hold(dev);
  507. ip_rt_put(rt);
  508. return dev;
  509. }
  510. #else
  511. static struct net_device *inet_fib_lookup_dev(struct net *net,
  512. const void *addr)
  513. {
  514. return ERR_PTR(-EAFNOSUPPORT);
  515. }
  516. #endif
  517. #if IS_ENABLED(CONFIG_IPV6)
  518. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  519. const void *addr)
  520. {
  521. struct net_device *dev;
  522. struct dst_entry *dst;
  523. struct flowi6 fl6;
  524. if (!ipv6_stub)
  525. return ERR_PTR(-EAFNOSUPPORT);
  526. memset(&fl6, 0, sizeof(fl6));
  527. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  528. dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
  529. if (IS_ERR(dst))
  530. return ERR_CAST(dst);
  531. dev = dst->dev;
  532. dev_hold(dev);
  533. dst_release(dst);
  534. return dev;
  535. }
  536. #else
  537. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  538. const void *addr)
  539. {
  540. return ERR_PTR(-EAFNOSUPPORT);
  541. }
  542. #endif
  543. static struct net_device *find_outdev(struct net *net,
  544. struct mpls_route *rt,
  545. struct mpls_nh *nh, int oif)
  546. {
  547. struct net_device *dev = NULL;
  548. if (!oif) {
  549. switch (nh->nh_via_table) {
  550. case NEIGH_ARP_TABLE:
  551. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  552. break;
  553. case NEIGH_ND_TABLE:
  554. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  555. break;
  556. case NEIGH_LINK_TABLE:
  557. break;
  558. }
  559. } else {
  560. dev = dev_get_by_index(net, oif);
  561. }
  562. if (!dev)
  563. return ERR_PTR(-ENODEV);
  564. if (IS_ERR(dev))
  565. return dev;
  566. /* The caller is holding rtnl anyways, so release the dev reference */
  567. dev_put(dev);
  568. return dev;
  569. }
  570. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  571. struct mpls_nh *nh, int oif)
  572. {
  573. struct net_device *dev = NULL;
  574. int err = -ENODEV;
  575. dev = find_outdev(net, rt, nh, oif);
  576. if (IS_ERR(dev)) {
  577. err = PTR_ERR(dev);
  578. dev = NULL;
  579. goto errout;
  580. }
  581. /* Ensure this is a supported device */
  582. err = -EINVAL;
  583. if (!mpls_dev_get(dev))
  584. goto errout;
  585. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  586. (dev->addr_len != nh->nh_via_alen))
  587. goto errout;
  588. nh->nh_dev = dev;
  589. if (!(dev->flags & IFF_UP)) {
  590. nh->nh_flags |= RTNH_F_DEAD;
  591. } else {
  592. unsigned int flags;
  593. flags = dev_get_flags(dev);
  594. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  595. nh->nh_flags |= RTNH_F_LINKDOWN;
  596. }
  597. return 0;
  598. errout:
  599. return err;
  600. }
  601. static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
  602. u8 via_addr[], struct netlink_ext_ack *extack)
  603. {
  604. struct rtvia *via = nla_data(nla);
  605. int err = -EINVAL;
  606. int alen;
  607. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
  608. NL_SET_ERR_MSG_ATTR(extack, nla,
  609. "Invalid attribute length for RTA_VIA");
  610. goto errout;
  611. }
  612. alen = nla_len(nla) -
  613. offsetof(struct rtvia, rtvia_addr);
  614. if (alen > MAX_VIA_ALEN) {
  615. NL_SET_ERR_MSG_ATTR(extack, nla,
  616. "Invalid address length for RTA_VIA");
  617. goto errout;
  618. }
  619. /* Validate the address family */
  620. switch (via->rtvia_family) {
  621. case AF_PACKET:
  622. *via_table = NEIGH_LINK_TABLE;
  623. break;
  624. case AF_INET:
  625. *via_table = NEIGH_ARP_TABLE;
  626. if (alen != 4)
  627. goto errout;
  628. break;
  629. case AF_INET6:
  630. *via_table = NEIGH_ND_TABLE;
  631. if (alen != 16)
  632. goto errout;
  633. break;
  634. default:
  635. /* Unsupported address family */
  636. goto errout;
  637. }
  638. memcpy(via_addr, via->rtvia_addr, alen);
  639. *via_alen = alen;
  640. err = 0;
  641. errout:
  642. return err;
  643. }
  644. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  645. struct mpls_route *rt)
  646. {
  647. struct net *net = cfg->rc_nlinfo.nl_net;
  648. struct mpls_nh *nh = rt->rt_nh;
  649. int err;
  650. int i;
  651. if (!nh)
  652. return -ENOMEM;
  653. nh->nh_labels = cfg->rc_output_labels;
  654. for (i = 0; i < nh->nh_labels; i++)
  655. nh->nh_label[i] = cfg->rc_output_label[i];
  656. nh->nh_via_table = cfg->rc_via_table;
  657. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  658. nh->nh_via_alen = cfg->rc_via_alen;
  659. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  660. if (err)
  661. goto errout;
  662. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  663. rt->rt_nhn_alive--;
  664. return 0;
  665. errout:
  666. return err;
  667. }
  668. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  669. struct mpls_nh *nh, int oif, struct nlattr *via,
  670. struct nlattr *newdst, u8 max_labels,
  671. struct netlink_ext_ack *extack)
  672. {
  673. int err = -ENOMEM;
  674. if (!nh)
  675. goto errout;
  676. if (newdst) {
  677. err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
  678. nh->nh_label, extack);
  679. if (err)
  680. goto errout;
  681. }
  682. if (via) {
  683. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  684. __mpls_nh_via(rt, nh), extack);
  685. if (err)
  686. goto errout;
  687. } else {
  688. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  689. }
  690. err = mpls_nh_assign_dev(net, rt, nh, oif);
  691. if (err)
  692. goto errout;
  693. return 0;
  694. errout:
  695. return err;
  696. }
  697. static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  698. u8 cfg_via_alen, u8 *max_via_alen,
  699. u8 *max_labels)
  700. {
  701. int remaining = len;
  702. u8 nhs = 0;
  703. *max_via_alen = 0;
  704. *max_labels = 0;
  705. while (rtnh_ok(rtnh, remaining)) {
  706. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  707. int attrlen;
  708. u8 n_labels = 0;
  709. attrlen = rtnh_attrlen(rtnh);
  710. nla = nla_find(attrs, attrlen, RTA_VIA);
  711. if (nla && nla_len(nla) >=
  712. offsetof(struct rtvia, rtvia_addr)) {
  713. int via_alen = nla_len(nla) -
  714. offsetof(struct rtvia, rtvia_addr);
  715. if (via_alen <= MAX_VIA_ALEN)
  716. *max_via_alen = max_t(u16, *max_via_alen,
  717. via_alen);
  718. }
  719. nla = nla_find(attrs, attrlen, RTA_NEWDST);
  720. if (nla &&
  721. nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
  722. NULL, NULL) != 0)
  723. return 0;
  724. *max_labels = max_t(u8, *max_labels, n_labels);
  725. /* number of nexthops is tracked by a u8.
  726. * Check for overflow.
  727. */
  728. if (nhs == 255)
  729. return 0;
  730. nhs++;
  731. rtnh = rtnh_next(rtnh, &remaining);
  732. }
  733. /* leftover implies invalid nexthop configuration, discard it */
  734. return remaining > 0 ? 0 : nhs;
  735. }
  736. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  737. struct mpls_route *rt, u8 max_labels,
  738. struct netlink_ext_ack *extack)
  739. {
  740. struct rtnexthop *rtnh = cfg->rc_mp;
  741. struct nlattr *nla_via, *nla_newdst;
  742. int remaining = cfg->rc_mp_len;
  743. int err = 0;
  744. u8 nhs = 0;
  745. change_nexthops(rt) {
  746. int attrlen;
  747. nla_via = NULL;
  748. nla_newdst = NULL;
  749. err = -EINVAL;
  750. if (!rtnh_ok(rtnh, remaining))
  751. goto errout;
  752. /* neither weighted multipath nor any flags
  753. * are supported
  754. */
  755. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  756. goto errout;
  757. attrlen = rtnh_attrlen(rtnh);
  758. if (attrlen > 0) {
  759. struct nlattr *attrs = rtnh_attrs(rtnh);
  760. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  761. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  762. }
  763. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  764. rtnh->rtnh_ifindex, nla_via, nla_newdst,
  765. max_labels, extack);
  766. if (err)
  767. goto errout;
  768. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  769. rt->rt_nhn_alive--;
  770. rtnh = rtnh_next(rtnh, &remaining);
  771. nhs++;
  772. } endfor_nexthops(rt);
  773. rt->rt_nhn = nhs;
  774. return 0;
  775. errout:
  776. return err;
  777. }
  778. static bool mpls_label_ok(struct net *net, unsigned int *index,
  779. struct netlink_ext_ack *extack)
  780. {
  781. bool is_ok = true;
  782. /* Reserved labels may not be set */
  783. if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
  784. NL_SET_ERR_MSG(extack,
  785. "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
  786. is_ok = false;
  787. }
  788. /* The full 20 bit range may not be supported. */
  789. if (is_ok && *index >= net->mpls.platform_labels) {
  790. NL_SET_ERR_MSG(extack,
  791. "Label >= configured maximum in platform_labels");
  792. is_ok = false;
  793. }
  794. *index = array_index_nospec(*index, net->mpls.platform_labels);
  795. return is_ok;
  796. }
  797. static int mpls_route_add(struct mpls_route_config *cfg,
  798. struct netlink_ext_ack *extack)
  799. {
  800. struct mpls_route __rcu **platform_label;
  801. struct net *net = cfg->rc_nlinfo.nl_net;
  802. struct mpls_route *rt, *old;
  803. int err = -EINVAL;
  804. u8 max_via_alen;
  805. unsigned index;
  806. u8 max_labels;
  807. u8 nhs;
  808. index = cfg->rc_label;
  809. /* If a label was not specified during insert pick one */
  810. if ((index == LABEL_NOT_SPECIFIED) &&
  811. (cfg->rc_nlflags & NLM_F_CREATE)) {
  812. index = find_free_label(net);
  813. }
  814. if (!mpls_label_ok(net, &index, extack))
  815. goto errout;
  816. /* Append makes no sense with mpls */
  817. err = -EOPNOTSUPP;
  818. if (cfg->rc_nlflags & NLM_F_APPEND) {
  819. NL_SET_ERR_MSG(extack, "MPLS does not support route append");
  820. goto errout;
  821. }
  822. err = -EEXIST;
  823. platform_label = rtnl_dereference(net->mpls.platform_label);
  824. old = rtnl_dereference(platform_label[index]);
  825. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  826. goto errout;
  827. err = -EEXIST;
  828. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  829. goto errout;
  830. err = -ENOENT;
  831. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  832. goto errout;
  833. err = -EINVAL;
  834. if (cfg->rc_mp) {
  835. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  836. cfg->rc_via_alen, &max_via_alen,
  837. &max_labels);
  838. } else {
  839. max_via_alen = cfg->rc_via_alen;
  840. max_labels = cfg->rc_output_labels;
  841. nhs = 1;
  842. }
  843. if (nhs == 0) {
  844. NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
  845. goto errout;
  846. }
  847. rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
  848. if (IS_ERR(rt)) {
  849. err = PTR_ERR(rt);
  850. goto errout;
  851. }
  852. rt->rt_protocol = cfg->rc_protocol;
  853. rt->rt_payload_type = cfg->rc_payload_type;
  854. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  855. if (cfg->rc_mp)
  856. err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
  857. else
  858. err = mpls_nh_build_from_cfg(cfg, rt);
  859. if (err)
  860. goto freert;
  861. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  862. return 0;
  863. freert:
  864. mpls_rt_free(rt);
  865. errout:
  866. return err;
  867. }
  868. static int mpls_route_del(struct mpls_route_config *cfg,
  869. struct netlink_ext_ack *extack)
  870. {
  871. struct net *net = cfg->rc_nlinfo.nl_net;
  872. unsigned index;
  873. int err = -EINVAL;
  874. index = cfg->rc_label;
  875. if (!mpls_label_ok(net, &index, extack))
  876. goto errout;
  877. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  878. err = 0;
  879. errout:
  880. return err;
  881. }
  882. static void mpls_get_stats(struct mpls_dev *mdev,
  883. struct mpls_link_stats *stats)
  884. {
  885. struct mpls_pcpu_stats *p;
  886. int i;
  887. memset(stats, 0, sizeof(*stats));
  888. for_each_possible_cpu(i) {
  889. struct mpls_link_stats local;
  890. unsigned int start;
  891. p = per_cpu_ptr(mdev->stats, i);
  892. do {
  893. start = u64_stats_fetch_begin_irq(&p->syncp);
  894. local = p->stats;
  895. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  896. stats->rx_packets += local.rx_packets;
  897. stats->rx_bytes += local.rx_bytes;
  898. stats->tx_packets += local.tx_packets;
  899. stats->tx_bytes += local.tx_bytes;
  900. stats->rx_errors += local.rx_errors;
  901. stats->tx_errors += local.tx_errors;
  902. stats->rx_dropped += local.rx_dropped;
  903. stats->tx_dropped += local.tx_dropped;
  904. stats->rx_noroute += local.rx_noroute;
  905. }
  906. }
  907. static int mpls_fill_stats_af(struct sk_buff *skb,
  908. const struct net_device *dev)
  909. {
  910. struct mpls_link_stats *stats;
  911. struct mpls_dev *mdev;
  912. struct nlattr *nla;
  913. mdev = mpls_dev_get(dev);
  914. if (!mdev)
  915. return -ENODATA;
  916. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  917. sizeof(struct mpls_link_stats),
  918. MPLS_STATS_UNSPEC);
  919. if (!nla)
  920. return -EMSGSIZE;
  921. stats = nla_data(nla);
  922. mpls_get_stats(mdev, stats);
  923. return 0;
  924. }
  925. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  926. {
  927. struct mpls_dev *mdev;
  928. mdev = mpls_dev_get(dev);
  929. if (!mdev)
  930. return 0;
  931. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  932. }
  933. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  934. u32 portid, u32 seq, int event,
  935. unsigned int flags, int type)
  936. {
  937. struct nlmsghdr *nlh;
  938. struct netconfmsg *ncm;
  939. bool all = false;
  940. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  941. flags);
  942. if (!nlh)
  943. return -EMSGSIZE;
  944. if (type == NETCONFA_ALL)
  945. all = true;
  946. ncm = nlmsg_data(nlh);
  947. ncm->ncm_family = AF_MPLS;
  948. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  949. goto nla_put_failure;
  950. if ((all || type == NETCONFA_INPUT) &&
  951. nla_put_s32(skb, NETCONFA_INPUT,
  952. mdev->input_enabled) < 0)
  953. goto nla_put_failure;
  954. nlmsg_end(skb, nlh);
  955. return 0;
  956. nla_put_failure:
  957. nlmsg_cancel(skb, nlh);
  958. return -EMSGSIZE;
  959. }
  960. static int mpls_netconf_msgsize_devconf(int type)
  961. {
  962. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  963. + nla_total_size(4); /* NETCONFA_IFINDEX */
  964. bool all = false;
  965. if (type == NETCONFA_ALL)
  966. all = true;
  967. if (all || type == NETCONFA_INPUT)
  968. size += nla_total_size(4);
  969. return size;
  970. }
  971. static void mpls_netconf_notify_devconf(struct net *net, int event,
  972. int type, struct mpls_dev *mdev)
  973. {
  974. struct sk_buff *skb;
  975. int err = -ENOBUFS;
  976. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  977. if (!skb)
  978. goto errout;
  979. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
  980. if (err < 0) {
  981. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  982. WARN_ON(err == -EMSGSIZE);
  983. kfree_skb(skb);
  984. goto errout;
  985. }
  986. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  987. return;
  988. errout:
  989. if (err < 0)
  990. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  991. }
  992. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  993. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  994. };
  995. static int mpls_netconf_valid_get_req(struct sk_buff *skb,
  996. const struct nlmsghdr *nlh,
  997. struct nlattr **tb,
  998. struct netlink_ext_ack *extack)
  999. {
  1000. int i, err;
  1001. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
  1002. NL_SET_ERR_MSG_MOD(extack,
  1003. "Invalid header for netconf get request");
  1004. return -EINVAL;
  1005. }
  1006. if (!netlink_strict_get_check(skb))
  1007. return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
  1008. tb, NETCONFA_MAX,
  1009. devconf_mpls_policy, extack);
  1010. err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
  1011. tb, NETCONFA_MAX,
  1012. devconf_mpls_policy, extack);
  1013. if (err)
  1014. return err;
  1015. for (i = 0; i <= NETCONFA_MAX; i++) {
  1016. if (!tb[i])
  1017. continue;
  1018. switch (i) {
  1019. case NETCONFA_IFINDEX:
  1020. break;
  1021. default:
  1022. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
  1023. return -EINVAL;
  1024. }
  1025. }
  1026. return 0;
  1027. }
  1028. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  1029. struct nlmsghdr *nlh,
  1030. struct netlink_ext_ack *extack)
  1031. {
  1032. struct net *net = sock_net(in_skb->sk);
  1033. struct nlattr *tb[NETCONFA_MAX + 1];
  1034. struct net_device *dev;
  1035. struct mpls_dev *mdev;
  1036. struct sk_buff *skb;
  1037. int ifindex;
  1038. int err;
  1039. err = mpls_netconf_valid_get_req(in_skb, nlh, tb, extack);
  1040. if (err < 0)
  1041. goto errout;
  1042. err = -EINVAL;
  1043. if (!tb[NETCONFA_IFINDEX])
  1044. goto errout;
  1045. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  1046. dev = __dev_get_by_index(net, ifindex);
  1047. if (!dev)
  1048. goto errout;
  1049. mdev = mpls_dev_get(dev);
  1050. if (!mdev)
  1051. goto errout;
  1052. err = -ENOBUFS;
  1053. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  1054. if (!skb)
  1055. goto errout;
  1056. err = mpls_netconf_fill_devconf(skb, mdev,
  1057. NETLINK_CB(in_skb).portid,
  1058. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  1059. NETCONFA_ALL);
  1060. if (err < 0) {
  1061. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  1062. WARN_ON(err == -EMSGSIZE);
  1063. kfree_skb(skb);
  1064. goto errout;
  1065. }
  1066. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  1067. errout:
  1068. return err;
  1069. }
  1070. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  1071. struct netlink_callback *cb)
  1072. {
  1073. const struct nlmsghdr *nlh = cb->nlh;
  1074. struct net *net = sock_net(skb->sk);
  1075. struct hlist_head *head;
  1076. struct net_device *dev;
  1077. struct mpls_dev *mdev;
  1078. int idx, s_idx;
  1079. int h, s_h;
  1080. if (cb->strict_check) {
  1081. struct netlink_ext_ack *extack = cb->extack;
  1082. struct netconfmsg *ncm;
  1083. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
  1084. NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
  1085. return -EINVAL;
  1086. }
  1087. if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
  1088. NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
  1089. return -EINVAL;
  1090. }
  1091. }
  1092. s_h = cb->args[0];
  1093. s_idx = idx = cb->args[1];
  1094. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  1095. idx = 0;
  1096. head = &net->dev_index_head[h];
  1097. rcu_read_lock();
  1098. cb->seq = net->dev_base_seq;
  1099. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  1100. if (idx < s_idx)
  1101. goto cont;
  1102. mdev = mpls_dev_get(dev);
  1103. if (!mdev)
  1104. goto cont;
  1105. if (mpls_netconf_fill_devconf(skb, mdev,
  1106. NETLINK_CB(cb->skb).portid,
  1107. nlh->nlmsg_seq,
  1108. RTM_NEWNETCONF,
  1109. NLM_F_MULTI,
  1110. NETCONFA_ALL) < 0) {
  1111. rcu_read_unlock();
  1112. goto done;
  1113. }
  1114. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  1115. cont:
  1116. idx++;
  1117. }
  1118. rcu_read_unlock();
  1119. }
  1120. done:
  1121. cb->args[0] = h;
  1122. cb->args[1] = idx;
  1123. return skb->len;
  1124. }
  1125. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  1126. (&((struct mpls_dev *)0)->field)
  1127. static int mpls_conf_proc(struct ctl_table *ctl, int write,
  1128. void *buffer, size_t *lenp, loff_t *ppos)
  1129. {
  1130. int oval = *(int *)ctl->data;
  1131. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1132. if (write) {
  1133. struct mpls_dev *mdev = ctl->extra1;
  1134. int i = (int *)ctl->data - (int *)mdev;
  1135. struct net *net = ctl->extra2;
  1136. int val = *(int *)ctl->data;
  1137. if (i == offsetof(struct mpls_dev, input_enabled) &&
  1138. val != oval) {
  1139. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1140. NETCONFA_INPUT, mdev);
  1141. }
  1142. }
  1143. return ret;
  1144. }
  1145. static const struct ctl_table mpls_dev_table[] = {
  1146. {
  1147. .procname = "input",
  1148. .maxlen = sizeof(int),
  1149. .mode = 0644,
  1150. .proc_handler = mpls_conf_proc,
  1151. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  1152. },
  1153. { }
  1154. };
  1155. static int mpls_dev_sysctl_register(struct net_device *dev,
  1156. struct mpls_dev *mdev)
  1157. {
  1158. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  1159. struct net *net = dev_net(dev);
  1160. struct ctl_table *table;
  1161. int i;
  1162. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1163. if (!table)
  1164. goto out;
  1165. /* Table data contains only offsets relative to the base of
  1166. * the mdev at this point, so make them absolute.
  1167. */
  1168. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
  1169. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1170. table[i].extra1 = mdev;
  1171. table[i].extra2 = net;
  1172. }
  1173. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1174. mdev->sysctl = register_net_sysctl(net, path, table);
  1175. if (!mdev->sysctl)
  1176. goto free;
  1177. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
  1178. return 0;
  1179. free:
  1180. kfree(table);
  1181. out:
  1182. mdev->sysctl = NULL;
  1183. return -ENOBUFS;
  1184. }
  1185. static void mpls_dev_sysctl_unregister(struct net_device *dev,
  1186. struct mpls_dev *mdev)
  1187. {
  1188. struct net *net = dev_net(dev);
  1189. struct ctl_table *table;
  1190. if (!mdev->sysctl)
  1191. return;
  1192. table = mdev->sysctl->ctl_table_arg;
  1193. unregister_net_sysctl_table(mdev->sysctl);
  1194. kfree(table);
  1195. mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
  1196. }
  1197. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1198. {
  1199. struct mpls_dev *mdev;
  1200. int err = -ENOMEM;
  1201. int i;
  1202. ASSERT_RTNL();
  1203. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1204. if (!mdev)
  1205. return ERR_PTR(err);
  1206. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1207. if (!mdev->stats)
  1208. goto free;
  1209. for_each_possible_cpu(i) {
  1210. struct mpls_pcpu_stats *mpls_stats;
  1211. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1212. u64_stats_init(&mpls_stats->syncp);
  1213. }
  1214. mdev->dev = dev;
  1215. err = mpls_dev_sysctl_register(dev, mdev);
  1216. if (err)
  1217. goto free;
  1218. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1219. return mdev;
  1220. free:
  1221. free_percpu(mdev->stats);
  1222. kfree(mdev);
  1223. return ERR_PTR(err);
  1224. }
  1225. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1226. {
  1227. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1228. free_percpu(mdev->stats);
  1229. kfree(mdev);
  1230. }
  1231. static int mpls_ifdown(struct net_device *dev, int event)
  1232. {
  1233. struct mpls_route __rcu **platform_label;
  1234. struct net *net = dev_net(dev);
  1235. unsigned index;
  1236. platform_label = rtnl_dereference(net->mpls.platform_label);
  1237. for (index = 0; index < net->mpls.platform_labels; index++) {
  1238. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1239. bool nh_del = false;
  1240. u8 alive = 0;
  1241. if (!rt)
  1242. continue;
  1243. if (event == NETDEV_UNREGISTER) {
  1244. u8 deleted = 0;
  1245. for_nexthops(rt) {
  1246. if (!nh->nh_dev || nh->nh_dev == dev)
  1247. deleted++;
  1248. if (nh->nh_dev == dev)
  1249. nh_del = true;
  1250. } endfor_nexthops(rt);
  1251. /* if there are no more nexthops, delete the route */
  1252. if (deleted == rt->rt_nhn) {
  1253. mpls_route_update(net, index, NULL, NULL);
  1254. continue;
  1255. }
  1256. if (nh_del) {
  1257. size_t size = sizeof(*rt) + rt->rt_nhn *
  1258. rt->rt_nh_size;
  1259. struct mpls_route *orig = rt;
  1260. rt = kmemdup(orig, size, GFP_KERNEL);
  1261. if (!rt)
  1262. return -ENOMEM;
  1263. }
  1264. }
  1265. change_nexthops(rt) {
  1266. unsigned int nh_flags = nh->nh_flags;
  1267. if (nh->nh_dev != dev)
  1268. goto next;
  1269. switch (event) {
  1270. case NETDEV_DOWN:
  1271. case NETDEV_UNREGISTER:
  1272. nh_flags |= RTNH_F_DEAD;
  1273. fallthrough;
  1274. case NETDEV_CHANGE:
  1275. nh_flags |= RTNH_F_LINKDOWN;
  1276. break;
  1277. }
  1278. if (event == NETDEV_UNREGISTER)
  1279. nh->nh_dev = NULL;
  1280. if (nh->nh_flags != nh_flags)
  1281. WRITE_ONCE(nh->nh_flags, nh_flags);
  1282. next:
  1283. if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
  1284. alive++;
  1285. } endfor_nexthops(rt);
  1286. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1287. if (nh_del)
  1288. mpls_route_update(net, index, rt, NULL);
  1289. }
  1290. return 0;
  1291. }
  1292. static void mpls_ifup(struct net_device *dev, unsigned int flags)
  1293. {
  1294. struct mpls_route __rcu **platform_label;
  1295. struct net *net = dev_net(dev);
  1296. unsigned index;
  1297. u8 alive;
  1298. platform_label = rtnl_dereference(net->mpls.platform_label);
  1299. for (index = 0; index < net->mpls.platform_labels; index++) {
  1300. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1301. if (!rt)
  1302. continue;
  1303. alive = 0;
  1304. change_nexthops(rt) {
  1305. unsigned int nh_flags = nh->nh_flags;
  1306. if (!(nh_flags & flags)) {
  1307. alive++;
  1308. continue;
  1309. }
  1310. if (nh->nh_dev != dev)
  1311. continue;
  1312. alive++;
  1313. nh_flags &= ~flags;
  1314. WRITE_ONCE(nh->nh_flags, nh_flags);
  1315. } endfor_nexthops(rt);
  1316. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1317. }
  1318. }
  1319. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1320. void *ptr)
  1321. {
  1322. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1323. struct mpls_dev *mdev;
  1324. unsigned int flags;
  1325. int err;
  1326. if (event == NETDEV_REGISTER) {
  1327. mdev = mpls_add_dev(dev);
  1328. if (IS_ERR(mdev))
  1329. return notifier_from_errno(PTR_ERR(mdev));
  1330. return NOTIFY_OK;
  1331. }
  1332. mdev = mpls_dev_get(dev);
  1333. if (!mdev)
  1334. return NOTIFY_OK;
  1335. switch (event) {
  1336. case NETDEV_DOWN:
  1337. err = mpls_ifdown(dev, event);
  1338. if (err)
  1339. return notifier_from_errno(err);
  1340. break;
  1341. case NETDEV_UP:
  1342. flags = dev_get_flags(dev);
  1343. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1344. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1345. else
  1346. mpls_ifup(dev, RTNH_F_DEAD);
  1347. break;
  1348. case NETDEV_CHANGE:
  1349. flags = dev_get_flags(dev);
  1350. if (flags & (IFF_RUNNING | IFF_LOWER_UP)) {
  1351. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1352. } else {
  1353. err = mpls_ifdown(dev, event);
  1354. if (err)
  1355. return notifier_from_errno(err);
  1356. }
  1357. break;
  1358. case NETDEV_UNREGISTER:
  1359. err = mpls_ifdown(dev, event);
  1360. if (err)
  1361. return notifier_from_errno(err);
  1362. mdev = mpls_dev_get(dev);
  1363. if (mdev) {
  1364. mpls_dev_sysctl_unregister(dev, mdev);
  1365. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1366. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1367. }
  1368. break;
  1369. case NETDEV_CHANGENAME:
  1370. mdev = mpls_dev_get(dev);
  1371. if (mdev) {
  1372. mpls_dev_sysctl_unregister(dev, mdev);
  1373. err = mpls_dev_sysctl_register(dev, mdev);
  1374. if (err)
  1375. return notifier_from_errno(err);
  1376. }
  1377. break;
  1378. }
  1379. return NOTIFY_OK;
  1380. }
  1381. static struct notifier_block mpls_dev_notifier = {
  1382. .notifier_call = mpls_dev_notify,
  1383. };
  1384. static int nla_put_via(struct sk_buff *skb,
  1385. u8 table, const void *addr, int alen)
  1386. {
  1387. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1388. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1389. };
  1390. struct nlattr *nla;
  1391. struct rtvia *via;
  1392. int family = AF_UNSPEC;
  1393. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1394. if (!nla)
  1395. return -EMSGSIZE;
  1396. if (table <= NEIGH_NR_TABLES)
  1397. family = table_to_family[table];
  1398. via = nla_data(nla);
  1399. via->rtvia_family = family;
  1400. memcpy(via->rtvia_addr, addr, alen);
  1401. return 0;
  1402. }
  1403. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1404. u8 labels, const u32 label[])
  1405. {
  1406. struct nlattr *nla;
  1407. struct mpls_shim_hdr *nla_label;
  1408. bool bos;
  1409. int i;
  1410. nla = nla_reserve(skb, attrtype, labels*4);
  1411. if (!nla)
  1412. return -EMSGSIZE;
  1413. nla_label = nla_data(nla);
  1414. bos = true;
  1415. for (i = labels - 1; i >= 0; i--) {
  1416. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1417. bos = false;
  1418. }
  1419. return 0;
  1420. }
  1421. EXPORT_SYMBOL_GPL(nla_put_labels);
  1422. int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
  1423. u32 label[], struct netlink_ext_ack *extack)
  1424. {
  1425. unsigned len = nla_len(nla);
  1426. struct mpls_shim_hdr *nla_label;
  1427. u8 nla_labels;
  1428. bool bos;
  1429. int i;
  1430. /* len needs to be an even multiple of 4 (the label size). Number
  1431. * of labels is a u8 so check for overflow.
  1432. */
  1433. if (len & 3 || len / 4 > 255) {
  1434. NL_SET_ERR_MSG_ATTR(extack, nla,
  1435. "Invalid length for labels attribute");
  1436. return -EINVAL;
  1437. }
  1438. /* Limit the number of new labels allowed */
  1439. nla_labels = len/4;
  1440. if (nla_labels > max_labels) {
  1441. NL_SET_ERR_MSG(extack, "Too many labels");
  1442. return -EINVAL;
  1443. }
  1444. /* when label == NULL, caller wants number of labels */
  1445. if (!label)
  1446. goto out;
  1447. nla_label = nla_data(nla);
  1448. bos = true;
  1449. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1450. struct mpls_entry_decoded dec;
  1451. dec = mpls_entry_decode(nla_label + i);
  1452. /* Ensure the bottom of stack flag is properly set
  1453. * and ttl and tc are both clear.
  1454. */
  1455. if (dec.ttl) {
  1456. NL_SET_ERR_MSG_ATTR(extack, nla,
  1457. "TTL in label must be 0");
  1458. return -EINVAL;
  1459. }
  1460. if (dec.tc) {
  1461. NL_SET_ERR_MSG_ATTR(extack, nla,
  1462. "Traffic class in label must be 0");
  1463. return -EINVAL;
  1464. }
  1465. if (dec.bos != bos) {
  1466. NL_SET_BAD_ATTR(extack, nla);
  1467. if (bos) {
  1468. NL_SET_ERR_MSG(extack,
  1469. "BOS bit must be set in first label");
  1470. } else {
  1471. NL_SET_ERR_MSG(extack,
  1472. "BOS bit can only be set in first label");
  1473. }
  1474. return -EINVAL;
  1475. }
  1476. switch (dec.label) {
  1477. case MPLS_LABEL_IMPLNULL:
  1478. /* RFC3032: This is a label that an LSR may
  1479. * assign and distribute, but which never
  1480. * actually appears in the encapsulation.
  1481. */
  1482. NL_SET_ERR_MSG_ATTR(extack, nla,
  1483. "Implicit NULL Label (3) can not be used in encapsulation");
  1484. return -EINVAL;
  1485. }
  1486. label[i] = dec.label;
  1487. }
  1488. out:
  1489. *labels = nla_labels;
  1490. return 0;
  1491. }
  1492. EXPORT_SYMBOL_GPL(nla_get_labels);
  1493. static int rtm_to_route_config(struct sk_buff *skb,
  1494. struct nlmsghdr *nlh,
  1495. struct mpls_route_config *cfg,
  1496. struct netlink_ext_ack *extack)
  1497. {
  1498. struct rtmsg *rtm;
  1499. struct nlattr *tb[RTA_MAX+1];
  1500. int index;
  1501. int err;
  1502. err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  1503. rtm_mpls_policy, extack);
  1504. if (err < 0)
  1505. goto errout;
  1506. err = -EINVAL;
  1507. rtm = nlmsg_data(nlh);
  1508. if (rtm->rtm_family != AF_MPLS) {
  1509. NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
  1510. goto errout;
  1511. }
  1512. if (rtm->rtm_dst_len != 20) {
  1513. NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
  1514. goto errout;
  1515. }
  1516. if (rtm->rtm_src_len != 0) {
  1517. NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
  1518. goto errout;
  1519. }
  1520. if (rtm->rtm_tos != 0) {
  1521. NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
  1522. goto errout;
  1523. }
  1524. if (rtm->rtm_table != RT_TABLE_MAIN) {
  1525. NL_SET_ERR_MSG(extack,
  1526. "MPLS only supports the main route table");
  1527. goto errout;
  1528. }
  1529. /* Any value is acceptable for rtm_protocol */
  1530. /* As mpls uses destination specific addresses
  1531. * (or source specific address in the case of multicast)
  1532. * all addresses have universal scope.
  1533. */
  1534. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
  1535. NL_SET_ERR_MSG(extack,
  1536. "Invalid route scope - MPLS only supports UNIVERSE");
  1537. goto errout;
  1538. }
  1539. if (rtm->rtm_type != RTN_UNICAST) {
  1540. NL_SET_ERR_MSG(extack,
  1541. "Invalid route type - MPLS only supports UNICAST");
  1542. goto errout;
  1543. }
  1544. if (rtm->rtm_flags != 0) {
  1545. NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
  1546. goto errout;
  1547. }
  1548. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1549. cfg->rc_protocol = rtm->rtm_protocol;
  1550. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1551. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1552. cfg->rc_nlflags = nlh->nlmsg_flags;
  1553. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1554. cfg->rc_nlinfo.nlh = nlh;
  1555. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1556. for (index = 0; index <= RTA_MAX; index++) {
  1557. struct nlattr *nla = tb[index];
  1558. if (!nla)
  1559. continue;
  1560. switch (index) {
  1561. case RTA_OIF:
  1562. cfg->rc_ifindex = nla_get_u32(nla);
  1563. break;
  1564. case RTA_NEWDST:
  1565. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1566. &cfg->rc_output_labels,
  1567. cfg->rc_output_label, extack))
  1568. goto errout;
  1569. break;
  1570. case RTA_DST:
  1571. {
  1572. u8 label_count;
  1573. if (nla_get_labels(nla, 1, &label_count,
  1574. &cfg->rc_label, extack))
  1575. goto errout;
  1576. if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
  1577. &cfg->rc_label, extack))
  1578. goto errout;
  1579. break;
  1580. }
  1581. case RTA_GATEWAY:
  1582. NL_SET_ERR_MSG(extack, "MPLS does not support RTA_GATEWAY attribute");
  1583. goto errout;
  1584. case RTA_VIA:
  1585. {
  1586. if (nla_get_via(nla, &cfg->rc_via_alen,
  1587. &cfg->rc_via_table, cfg->rc_via,
  1588. extack))
  1589. goto errout;
  1590. break;
  1591. }
  1592. case RTA_MULTIPATH:
  1593. {
  1594. cfg->rc_mp = nla_data(nla);
  1595. cfg->rc_mp_len = nla_len(nla);
  1596. break;
  1597. }
  1598. case RTA_TTL_PROPAGATE:
  1599. {
  1600. u8 ttl_propagate = nla_get_u8(nla);
  1601. if (ttl_propagate > 1) {
  1602. NL_SET_ERR_MSG_ATTR(extack, nla,
  1603. "RTA_TTL_PROPAGATE can only be 0 or 1");
  1604. goto errout;
  1605. }
  1606. cfg->rc_ttl_propagate = ttl_propagate ?
  1607. MPLS_TTL_PROP_ENABLED :
  1608. MPLS_TTL_PROP_DISABLED;
  1609. break;
  1610. }
  1611. default:
  1612. NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
  1613. /* Unsupported attribute */
  1614. goto errout;
  1615. }
  1616. }
  1617. err = 0;
  1618. errout:
  1619. return err;
  1620. }
  1621. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1622. struct netlink_ext_ack *extack)
  1623. {
  1624. struct mpls_route_config *cfg;
  1625. int err;
  1626. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1627. if (!cfg)
  1628. return -ENOMEM;
  1629. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1630. if (err < 0)
  1631. goto out;
  1632. err = mpls_route_del(cfg, extack);
  1633. out:
  1634. kfree(cfg);
  1635. return err;
  1636. }
  1637. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1638. struct netlink_ext_ack *extack)
  1639. {
  1640. struct mpls_route_config *cfg;
  1641. int err;
  1642. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1643. if (!cfg)
  1644. return -ENOMEM;
  1645. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1646. if (err < 0)
  1647. goto out;
  1648. err = mpls_route_add(cfg, extack);
  1649. out:
  1650. kfree(cfg);
  1651. return err;
  1652. }
  1653. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1654. u32 label, struct mpls_route *rt, int flags)
  1655. {
  1656. struct net_device *dev;
  1657. struct nlmsghdr *nlh;
  1658. struct rtmsg *rtm;
  1659. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1660. if (nlh == NULL)
  1661. return -EMSGSIZE;
  1662. rtm = nlmsg_data(nlh);
  1663. rtm->rtm_family = AF_MPLS;
  1664. rtm->rtm_dst_len = 20;
  1665. rtm->rtm_src_len = 0;
  1666. rtm->rtm_tos = 0;
  1667. rtm->rtm_table = RT_TABLE_MAIN;
  1668. rtm->rtm_protocol = rt->rt_protocol;
  1669. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1670. rtm->rtm_type = RTN_UNICAST;
  1671. rtm->rtm_flags = 0;
  1672. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1673. goto nla_put_failure;
  1674. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1675. bool ttl_propagate =
  1676. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1677. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1678. ttl_propagate))
  1679. goto nla_put_failure;
  1680. }
  1681. if (rt->rt_nhn == 1) {
  1682. const struct mpls_nh *nh = rt->rt_nh;
  1683. if (nh->nh_labels &&
  1684. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1685. nh->nh_label))
  1686. goto nla_put_failure;
  1687. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1688. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1689. nh->nh_via_alen))
  1690. goto nla_put_failure;
  1691. dev = nh->nh_dev;
  1692. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1693. goto nla_put_failure;
  1694. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1695. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1696. if (nh->nh_flags & RTNH_F_DEAD)
  1697. rtm->rtm_flags |= RTNH_F_DEAD;
  1698. } else {
  1699. struct rtnexthop *rtnh;
  1700. struct nlattr *mp;
  1701. u8 linkdown = 0;
  1702. u8 dead = 0;
  1703. mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
  1704. if (!mp)
  1705. goto nla_put_failure;
  1706. for_nexthops(rt) {
  1707. dev = nh->nh_dev;
  1708. if (!dev)
  1709. continue;
  1710. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1711. if (!rtnh)
  1712. goto nla_put_failure;
  1713. rtnh->rtnh_ifindex = dev->ifindex;
  1714. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1715. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1716. linkdown++;
  1717. }
  1718. if (nh->nh_flags & RTNH_F_DEAD) {
  1719. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1720. dead++;
  1721. }
  1722. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1723. nh->nh_labels,
  1724. nh->nh_label))
  1725. goto nla_put_failure;
  1726. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1727. nla_put_via(skb, nh->nh_via_table,
  1728. mpls_nh_via(rt, nh),
  1729. nh->nh_via_alen))
  1730. goto nla_put_failure;
  1731. /* length of rtnetlink header + attributes */
  1732. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1733. } endfor_nexthops(rt);
  1734. if (linkdown == rt->rt_nhn)
  1735. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1736. if (dead == rt->rt_nhn)
  1737. rtm->rtm_flags |= RTNH_F_DEAD;
  1738. nla_nest_end(skb, mp);
  1739. }
  1740. nlmsg_end(skb, nlh);
  1741. return 0;
  1742. nla_put_failure:
  1743. nlmsg_cancel(skb, nlh);
  1744. return -EMSGSIZE;
  1745. }
  1746. #if IS_ENABLED(CONFIG_INET)
  1747. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1748. struct fib_dump_filter *filter,
  1749. struct netlink_callback *cb)
  1750. {
  1751. return ip_valid_fib_dump_req(net, nlh, filter, cb);
  1752. }
  1753. #else
  1754. static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
  1755. struct fib_dump_filter *filter,
  1756. struct netlink_callback *cb)
  1757. {
  1758. struct netlink_ext_ack *extack = cb->extack;
  1759. struct nlattr *tb[RTA_MAX + 1];
  1760. struct rtmsg *rtm;
  1761. int err, i;
  1762. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  1763. NL_SET_ERR_MSG_MOD(extack, "Invalid header for FIB dump request");
  1764. return -EINVAL;
  1765. }
  1766. rtm = nlmsg_data(nlh);
  1767. if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
  1768. rtm->rtm_table || rtm->rtm_scope || rtm->rtm_type ||
  1769. rtm->rtm_flags) {
  1770. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for FIB dump request");
  1771. return -EINVAL;
  1772. }
  1773. if (rtm->rtm_protocol) {
  1774. filter->protocol = rtm->rtm_protocol;
  1775. filter->filter_set = 1;
  1776. cb->answer_flags = NLM_F_DUMP_FILTERED;
  1777. }
  1778. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  1779. rtm_mpls_policy, extack);
  1780. if (err < 0)
  1781. return err;
  1782. for (i = 0; i <= RTA_MAX; ++i) {
  1783. int ifindex;
  1784. if (i == RTA_OIF) {
  1785. ifindex = nla_get_u32(tb[i]);
  1786. filter->dev = __dev_get_by_index(net, ifindex);
  1787. if (!filter->dev)
  1788. return -ENODEV;
  1789. filter->filter_set = 1;
  1790. } else if (tb[i]) {
  1791. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
  1792. return -EINVAL;
  1793. }
  1794. }
  1795. return 0;
  1796. }
  1797. #endif
  1798. static bool mpls_rt_uses_dev(struct mpls_route *rt,
  1799. const struct net_device *dev)
  1800. {
  1801. if (rt->rt_nhn == 1) {
  1802. struct mpls_nh *nh = rt->rt_nh;
  1803. if (nh->nh_dev == dev)
  1804. return true;
  1805. } else {
  1806. for_nexthops(rt) {
  1807. if (nh->nh_dev == dev)
  1808. return true;
  1809. } endfor_nexthops(rt);
  1810. }
  1811. return false;
  1812. }
  1813. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1814. {
  1815. const struct nlmsghdr *nlh = cb->nlh;
  1816. struct net *net = sock_net(skb->sk);
  1817. struct mpls_route __rcu **platform_label;
  1818. struct fib_dump_filter filter = {};
  1819. unsigned int flags = NLM_F_MULTI;
  1820. size_t platform_labels;
  1821. unsigned int index;
  1822. ASSERT_RTNL();
  1823. if (cb->strict_check) {
  1824. int err;
  1825. err = mpls_valid_fib_dump_req(net, nlh, &filter, cb);
  1826. if (err < 0)
  1827. return err;
  1828. /* for MPLS, there is only 1 table with fixed type and flags.
  1829. * If either are set in the filter then return nothing.
  1830. */
  1831. if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
  1832. (filter.rt_type && filter.rt_type != RTN_UNICAST) ||
  1833. filter.flags)
  1834. return skb->len;
  1835. }
  1836. index = cb->args[0];
  1837. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1838. index = MPLS_LABEL_FIRST_UNRESERVED;
  1839. platform_label = rtnl_dereference(net->mpls.platform_label);
  1840. platform_labels = net->mpls.platform_labels;
  1841. if (filter.filter_set)
  1842. flags |= NLM_F_DUMP_FILTERED;
  1843. for (; index < platform_labels; index++) {
  1844. struct mpls_route *rt;
  1845. rt = rtnl_dereference(platform_label[index]);
  1846. if (!rt)
  1847. continue;
  1848. if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
  1849. (filter.protocol && rt->rt_protocol != filter.protocol))
  1850. continue;
  1851. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1852. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1853. index, rt, flags) < 0)
  1854. break;
  1855. }
  1856. cb->args[0] = index;
  1857. return skb->len;
  1858. }
  1859. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1860. {
  1861. size_t payload =
  1862. NLMSG_ALIGN(sizeof(struct rtmsg))
  1863. + nla_total_size(4) /* RTA_DST */
  1864. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1865. if (rt->rt_nhn == 1) {
  1866. struct mpls_nh *nh = rt->rt_nh;
  1867. if (nh->nh_dev)
  1868. payload += nla_total_size(4); /* RTA_OIF */
  1869. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1870. payload += nla_total_size(2 + nh->nh_via_alen);
  1871. if (nh->nh_labels) /* RTA_NEWDST */
  1872. payload += nla_total_size(nh->nh_labels * 4);
  1873. } else {
  1874. /* each nexthop is packed in an attribute */
  1875. size_t nhsize = 0;
  1876. for_nexthops(rt) {
  1877. if (!nh->nh_dev)
  1878. continue;
  1879. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1880. /* RTA_VIA */
  1881. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1882. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1883. if (nh->nh_labels)
  1884. nhsize += nla_total_size(nh->nh_labels * 4);
  1885. } endfor_nexthops(rt);
  1886. /* nested attribute */
  1887. payload += nla_total_size(nhsize);
  1888. }
  1889. return payload;
  1890. }
  1891. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1892. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1893. unsigned int nlm_flags)
  1894. {
  1895. struct sk_buff *skb;
  1896. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1897. int err = -ENOBUFS;
  1898. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1899. if (skb == NULL)
  1900. goto errout;
  1901. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1902. if (err < 0) {
  1903. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1904. WARN_ON(err == -EMSGSIZE);
  1905. kfree_skb(skb);
  1906. goto errout;
  1907. }
  1908. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1909. return;
  1910. errout:
  1911. if (err < 0)
  1912. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1913. }
  1914. static int mpls_valid_getroute_req(struct sk_buff *skb,
  1915. const struct nlmsghdr *nlh,
  1916. struct nlattr **tb,
  1917. struct netlink_ext_ack *extack)
  1918. {
  1919. struct rtmsg *rtm;
  1920. int i, err;
  1921. if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
  1922. NL_SET_ERR_MSG_MOD(extack,
  1923. "Invalid header for get route request");
  1924. return -EINVAL;
  1925. }
  1926. if (!netlink_strict_get_check(skb))
  1927. return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
  1928. rtm_mpls_policy, extack);
  1929. rtm = nlmsg_data(nlh);
  1930. if ((rtm->rtm_dst_len && rtm->rtm_dst_len != 20) ||
  1931. rtm->rtm_src_len || rtm->rtm_tos || rtm->rtm_table ||
  1932. rtm->rtm_protocol || rtm->rtm_scope || rtm->rtm_type) {
  1933. NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
  1934. return -EINVAL;
  1935. }
  1936. if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
  1937. NL_SET_ERR_MSG_MOD(extack,
  1938. "Invalid flags for get route request");
  1939. return -EINVAL;
  1940. }
  1941. err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
  1942. rtm_mpls_policy, extack);
  1943. if (err)
  1944. return err;
  1945. if ((tb[RTA_DST] || tb[RTA_NEWDST]) && !rtm->rtm_dst_len) {
  1946. NL_SET_ERR_MSG_MOD(extack, "rtm_dst_len must be 20 for MPLS");
  1947. return -EINVAL;
  1948. }
  1949. for (i = 0; i <= RTA_MAX; i++) {
  1950. if (!tb[i])
  1951. continue;
  1952. switch (i) {
  1953. case RTA_DST:
  1954. case RTA_NEWDST:
  1955. break;
  1956. default:
  1957. NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
  1958. return -EINVAL;
  1959. }
  1960. }
  1961. return 0;
  1962. }
  1963. static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
  1964. struct netlink_ext_ack *extack)
  1965. {
  1966. struct net *net = sock_net(in_skb->sk);
  1967. u32 portid = NETLINK_CB(in_skb).portid;
  1968. u32 in_label = LABEL_NOT_SPECIFIED;
  1969. struct nlattr *tb[RTA_MAX + 1];
  1970. u32 labels[MAX_NEW_LABELS];
  1971. struct mpls_shim_hdr *hdr;
  1972. unsigned int hdr_size = 0;
  1973. const struct mpls_nh *nh;
  1974. struct net_device *dev;
  1975. struct mpls_route *rt;
  1976. struct rtmsg *rtm, *r;
  1977. struct nlmsghdr *nlh;
  1978. struct sk_buff *skb;
  1979. u8 n_labels;
  1980. int err;
  1981. err = mpls_valid_getroute_req(in_skb, in_nlh, tb, extack);
  1982. if (err < 0)
  1983. goto errout;
  1984. rtm = nlmsg_data(in_nlh);
  1985. if (tb[RTA_DST]) {
  1986. u8 label_count;
  1987. if (nla_get_labels(tb[RTA_DST], 1, &label_count,
  1988. &in_label, extack)) {
  1989. err = -EINVAL;
  1990. goto errout;
  1991. }
  1992. if (!mpls_label_ok(net, &in_label, extack)) {
  1993. err = -EINVAL;
  1994. goto errout;
  1995. }
  1996. }
  1997. rt = mpls_route_input_rcu(net, in_label);
  1998. if (!rt) {
  1999. err = -ENETUNREACH;
  2000. goto errout;
  2001. }
  2002. if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
  2003. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  2004. if (!skb) {
  2005. err = -ENOBUFS;
  2006. goto errout;
  2007. }
  2008. err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
  2009. RTM_NEWROUTE, in_label, rt, 0);
  2010. if (err < 0) {
  2011. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  2012. WARN_ON(err == -EMSGSIZE);
  2013. goto errout_free;
  2014. }
  2015. return rtnl_unicast(skb, net, portid);
  2016. }
  2017. if (tb[RTA_NEWDST]) {
  2018. if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
  2019. labels, extack) != 0) {
  2020. err = -EINVAL;
  2021. goto errout;
  2022. }
  2023. hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
  2024. }
  2025. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  2026. if (!skb) {
  2027. err = -ENOBUFS;
  2028. goto errout;
  2029. }
  2030. skb->protocol = htons(ETH_P_MPLS_UC);
  2031. if (hdr_size) {
  2032. bool bos;
  2033. int i;
  2034. if (skb_cow(skb, hdr_size)) {
  2035. err = -ENOBUFS;
  2036. goto errout_free;
  2037. }
  2038. skb_reserve(skb, hdr_size);
  2039. skb_push(skb, hdr_size);
  2040. skb_reset_network_header(skb);
  2041. /* Push new labels */
  2042. hdr = mpls_hdr(skb);
  2043. bos = true;
  2044. for (i = n_labels - 1; i >= 0; i--) {
  2045. hdr[i] = mpls_entry_encode(labels[i],
  2046. 1, 0, bos);
  2047. bos = false;
  2048. }
  2049. }
  2050. nh = mpls_select_multipath(rt, skb);
  2051. if (!nh) {
  2052. err = -ENETUNREACH;
  2053. goto errout_free;
  2054. }
  2055. if (hdr_size) {
  2056. skb_pull(skb, hdr_size);
  2057. skb_reset_network_header(skb);
  2058. }
  2059. nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
  2060. RTM_NEWROUTE, sizeof(*r), 0);
  2061. if (!nlh) {
  2062. err = -EMSGSIZE;
  2063. goto errout_free;
  2064. }
  2065. r = nlmsg_data(nlh);
  2066. r->rtm_family = AF_MPLS;
  2067. r->rtm_dst_len = 20;
  2068. r->rtm_src_len = 0;
  2069. r->rtm_table = RT_TABLE_MAIN;
  2070. r->rtm_type = RTN_UNICAST;
  2071. r->rtm_scope = RT_SCOPE_UNIVERSE;
  2072. r->rtm_protocol = rt->rt_protocol;
  2073. r->rtm_flags = 0;
  2074. if (nla_put_labels(skb, RTA_DST, 1, &in_label))
  2075. goto nla_put_failure;
  2076. if (nh->nh_labels &&
  2077. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  2078. nh->nh_label))
  2079. goto nla_put_failure;
  2080. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  2081. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  2082. nh->nh_via_alen))
  2083. goto nla_put_failure;
  2084. dev = nh->nh_dev;
  2085. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  2086. goto nla_put_failure;
  2087. nlmsg_end(skb, nlh);
  2088. err = rtnl_unicast(skb, net, portid);
  2089. errout:
  2090. return err;
  2091. nla_put_failure:
  2092. nlmsg_cancel(skb, nlh);
  2093. err = -EMSGSIZE;
  2094. errout_free:
  2095. kfree_skb(skb);
  2096. return err;
  2097. }
  2098. static int resize_platform_label_table(struct net *net, size_t limit)
  2099. {
  2100. size_t size = sizeof(struct mpls_route *) * limit;
  2101. size_t old_limit;
  2102. size_t cp_size;
  2103. struct mpls_route __rcu **labels = NULL, **old;
  2104. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  2105. unsigned index;
  2106. if (size) {
  2107. labels = kvzalloc(size, GFP_KERNEL);
  2108. if (!labels)
  2109. goto nolabels;
  2110. }
  2111. /* In case the predefined labels need to be populated */
  2112. if (limit > MPLS_LABEL_IPV4NULL) {
  2113. struct net_device *lo = net->loopback_dev;
  2114. rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
  2115. if (IS_ERR(rt0))
  2116. goto nort0;
  2117. rt0->rt_nh->nh_dev = lo;
  2118. rt0->rt_protocol = RTPROT_KERNEL;
  2119. rt0->rt_payload_type = MPT_IPV4;
  2120. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2121. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2122. rt0->rt_nh->nh_via_alen = lo->addr_len;
  2123. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  2124. lo->addr_len);
  2125. }
  2126. if (limit > MPLS_LABEL_IPV6NULL) {
  2127. struct net_device *lo = net->loopback_dev;
  2128. rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
  2129. if (IS_ERR(rt2))
  2130. goto nort2;
  2131. rt2->rt_nh->nh_dev = lo;
  2132. rt2->rt_protocol = RTPROT_KERNEL;
  2133. rt2->rt_payload_type = MPT_IPV6;
  2134. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  2135. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  2136. rt2->rt_nh->nh_via_alen = lo->addr_len;
  2137. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  2138. lo->addr_len);
  2139. }
  2140. rtnl_lock();
  2141. /* Remember the original table */
  2142. old = rtnl_dereference(net->mpls.platform_label);
  2143. old_limit = net->mpls.platform_labels;
  2144. /* Free any labels beyond the new table */
  2145. for (index = limit; index < old_limit; index++)
  2146. mpls_route_update(net, index, NULL, NULL);
  2147. /* Copy over the old labels */
  2148. cp_size = size;
  2149. if (old_limit < limit)
  2150. cp_size = old_limit * sizeof(struct mpls_route *);
  2151. memcpy(labels, old, cp_size);
  2152. /* If needed set the predefined labels */
  2153. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  2154. (limit > MPLS_LABEL_IPV6NULL)) {
  2155. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  2156. rt2 = NULL;
  2157. }
  2158. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  2159. (limit > MPLS_LABEL_IPV4NULL)) {
  2160. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  2161. rt0 = NULL;
  2162. }
  2163. /* Update the global pointers */
  2164. net->mpls.platform_labels = limit;
  2165. rcu_assign_pointer(net->mpls.platform_label, labels);
  2166. rtnl_unlock();
  2167. mpls_rt_free(rt2);
  2168. mpls_rt_free(rt0);
  2169. if (old) {
  2170. synchronize_rcu();
  2171. kvfree(old);
  2172. }
  2173. return 0;
  2174. nort2:
  2175. mpls_rt_free(rt0);
  2176. nort0:
  2177. kvfree(labels);
  2178. nolabels:
  2179. return -ENOMEM;
  2180. }
  2181. static int mpls_platform_labels(struct ctl_table *table, int write,
  2182. void *buffer, size_t *lenp, loff_t *ppos)
  2183. {
  2184. struct net *net = table->data;
  2185. int platform_labels = net->mpls.platform_labels;
  2186. int ret;
  2187. struct ctl_table tmp = {
  2188. .procname = table->procname,
  2189. .data = &platform_labels,
  2190. .maxlen = sizeof(int),
  2191. .mode = table->mode,
  2192. .extra1 = SYSCTL_ZERO,
  2193. .extra2 = &label_limit,
  2194. };
  2195. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2196. if (write && ret == 0)
  2197. ret = resize_platform_label_table(net, platform_labels);
  2198. return ret;
  2199. }
  2200. #define MPLS_NS_SYSCTL_OFFSET(field) \
  2201. (&((struct net *)0)->field)
  2202. static const struct ctl_table mpls_table[] = {
  2203. {
  2204. .procname = "platform_labels",
  2205. .data = NULL,
  2206. .maxlen = sizeof(int),
  2207. .mode = 0644,
  2208. .proc_handler = mpls_platform_labels,
  2209. },
  2210. {
  2211. .procname = "ip_ttl_propagate",
  2212. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  2213. .maxlen = sizeof(int),
  2214. .mode = 0644,
  2215. .proc_handler = proc_dointvec_minmax,
  2216. .extra1 = SYSCTL_ZERO,
  2217. .extra2 = SYSCTL_ONE,
  2218. },
  2219. {
  2220. .procname = "default_ttl",
  2221. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  2222. .maxlen = sizeof(int),
  2223. .mode = 0644,
  2224. .proc_handler = proc_dointvec_minmax,
  2225. .extra1 = SYSCTL_ONE,
  2226. .extra2 = &ttl_max,
  2227. },
  2228. { }
  2229. };
  2230. static int mpls_net_init(struct net *net)
  2231. {
  2232. struct ctl_table *table;
  2233. int i;
  2234. net->mpls.platform_labels = 0;
  2235. net->mpls.platform_label = NULL;
  2236. net->mpls.ip_ttl_propagate = 1;
  2237. net->mpls.default_ttl = 255;
  2238. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  2239. if (table == NULL)
  2240. return -ENOMEM;
  2241. /* Table data contains only offsets relative to the base of
  2242. * the mdev at this point, so make them absolute.
  2243. */
  2244. for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
  2245. table[i].data = (char *)net + (uintptr_t)table[i].data;
  2246. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  2247. if (net->mpls.ctl == NULL) {
  2248. kfree(table);
  2249. return -ENOMEM;
  2250. }
  2251. return 0;
  2252. }
  2253. static void mpls_net_exit(struct net *net)
  2254. {
  2255. struct mpls_route __rcu **platform_label;
  2256. size_t platform_labels;
  2257. struct ctl_table *table;
  2258. unsigned int index;
  2259. table = net->mpls.ctl->ctl_table_arg;
  2260. unregister_net_sysctl_table(net->mpls.ctl);
  2261. kfree(table);
  2262. /* An rcu grace period has passed since there was a device in
  2263. * the network namespace (and thus the last in flight packet)
  2264. * left this network namespace. This is because
  2265. * unregister_netdevice_many and netdev_run_todo has completed
  2266. * for each network device that was in this network namespace.
  2267. *
  2268. * As such no additional rcu synchronization is necessary when
  2269. * freeing the platform_label table.
  2270. */
  2271. rtnl_lock();
  2272. platform_label = rtnl_dereference(net->mpls.platform_label);
  2273. platform_labels = net->mpls.platform_labels;
  2274. for (index = 0; index < platform_labels; index++) {
  2275. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  2276. RCU_INIT_POINTER(platform_label[index], NULL);
  2277. mpls_notify_route(net, index, rt, NULL, NULL);
  2278. mpls_rt_free(rt);
  2279. }
  2280. rtnl_unlock();
  2281. kvfree(platform_label);
  2282. }
  2283. static struct pernet_operations mpls_net_ops = {
  2284. .init = mpls_net_init,
  2285. .exit = mpls_net_exit,
  2286. };
  2287. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  2288. .family = AF_MPLS,
  2289. .fill_stats_af = mpls_fill_stats_af,
  2290. .get_stats_af_size = mpls_get_stats_af_size,
  2291. };
  2292. static int __init mpls_init(void)
  2293. {
  2294. int err;
  2295. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  2296. err = register_pernet_subsys(&mpls_net_ops);
  2297. if (err)
  2298. goto out;
  2299. err = register_netdevice_notifier(&mpls_dev_notifier);
  2300. if (err)
  2301. goto out_unregister_pernet;
  2302. dev_add_pack(&mpls_packet_type);
  2303. rtnl_af_register(&mpls_af_ops);
  2304. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_NEWROUTE,
  2305. mpls_rtm_newroute, NULL, 0);
  2306. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_DELROUTE,
  2307. mpls_rtm_delroute, NULL, 0);
  2308. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETROUTE,
  2309. mpls_getroute, mpls_dump_routes, 0);
  2310. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
  2311. mpls_netconf_get_devconf,
  2312. mpls_netconf_dump_devconf, 0);
  2313. err = ipgre_tunnel_encap_add_mpls_ops();
  2314. if (err)
  2315. pr_err("Can't add mpls over gre tunnel ops\n");
  2316. err = 0;
  2317. out:
  2318. return err;
  2319. out_unregister_pernet:
  2320. unregister_pernet_subsys(&mpls_net_ops);
  2321. goto out;
  2322. }
  2323. module_init(mpls_init);
  2324. static void __exit mpls_exit(void)
  2325. {
  2326. rtnl_unregister_all(PF_MPLS);
  2327. rtnl_af_unregister(&mpls_af_ops);
  2328. dev_remove_pack(&mpls_packet_type);
  2329. unregister_netdevice_notifier(&mpls_dev_notifier);
  2330. unregister_pernet_subsys(&mpls_net_ops);
  2331. ipgre_tunnel_encap_del_mpls_ops();
  2332. }
  2333. module_exit(mpls_exit);
  2334. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  2335. MODULE_LICENSE("GPL v2");
  2336. MODULE_ALIAS_NETPROTO(PF_MPLS);