af_mpls.c 58 KB

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